History

avcodec/motion_est: Factor one-time initialization out of ff_init_me

The majority of the stuff performed in it needs to be done only once; so factor it out into a function of its own to be called in the user's init function. Also avoid using MpegEncContext in it, to separate the two a bit more.

Andreas Rheinhardt committed 2 years ago

Commit bbd35531 parent 422711d5 files · +80 −53Browse files

Showing the changes to libavcodec (5 of 5 files). Show all

Unified · Split

5 files
  1. libavcodec/motion_est.c +48 −41
  2. libavcodec/motion_est.h +8 −1
  3. libavcodec/mpegvideo_enc.c +16 −3
  4. libavcodec/snowenc.c +4 −7
  5. libavcodec/svq1enc.c +4 −1
libavcodec/motion_est.c+48−41
@@ -305,45 +305,40 @@ static int zero_cmp(MpegEncContext *s, const uint8_t *a, const uint8_t *b, · 304 unmodified lines
305static void zero_hpel(uint8_t *a, const uint8_t *b, ptrdiff_t stride, int h){305static void zero_hpel(uint8_t *a, const uint8_t *b, ptrdiff_t stride, int h){
306}306}
307 307
308int ff_init_me(MpegEncContext *s){308av_cold int ff_me_init(MotionEstContext *c, AVCodecContext *avctx, MECmpContext *mecc)
309 MotionEstContext * const c= &s->me;309{
310 int cache_size= FFMIN(ME_MAP_SIZE>>ME_MAP_SHIFT, 1<<ME_MAP_SHIFT);310 int cache_size = FFMIN(ME_MAP_SIZE>>ME_MAP_SHIFT, 1<<ME_MAP_SHIFT);
311 int dia_size= FFMAX(FFABS(s->avctx->dia_size)&255, FFABS(s->avctx->pre_dia_size)&255);311 int dia_size = FFMAX(FFABS(avctx->dia_size) & 255, FFABS(avctx->pre_dia_size) & 255);
312 int ret;312 int ret;
313 313
314 if(FFMIN(s->avctx->dia_size, s->avctx->pre_dia_size) < -FFMIN(ME_MAP_SIZE, MAX_SAB_SIZE)){314 if (FFMIN(avctx->dia_size, avctx->pre_dia_size) < -FFMIN(ME_MAP_SIZE, MAX_SAB_SIZE)) {
315 av_log(s->avctx, AV_LOG_ERROR, "ME_MAP size is too small for SAB diamond\n");315 av_log(avctx, AV_LOG_ERROR, "ME_MAP size is too small for SAB diamond\n");
316 return -1;316 return AVERROR(EINVAL);
317 }317 }
318 318
319 c->avctx= s->avctx;319 c->avctx = avctx;
320 320
321 if(s->codec_id == AV_CODEC_ID_H261)321 if (avctx->codec_id == AV_CODEC_ID_H261)
322 c->avctx->me_sub_cmp = c->avctx->me_cmp;322 avctx->me_sub_cmp = avctx->me_cmp;
323 323
324 if(cache_size < 2*dia_size && !c->stride){324 if (cache_size < 2 * dia_size)
325 av_log(s->avctx, AV_LOG_INFO, "ME_MAP size may be a little small for the selected diamond size\n");325 av_log(avctx, AV_LOG_INFO, "ME_MAP size may be a little small for the selected diamond size\n");
326 }
327 326
328 ret = ff_set_cmp(&s->mecc, s->mecc.me_pre_cmp, c->avctx->me_pre_cmp);327 ret = ff_set_cmp(mecc, mecc->me_pre_cmp, avctx->me_pre_cmp);
329 ret |= ff_set_cmp(&s->mecc, s->mecc.me_cmp, c->avctx->me_cmp);328 ret |= ff_set_cmp(mecc, mecc->me_cmp, avctx->me_cmp);
330 ret |= ff_set_cmp(&s->mecc, s->mecc.me_sub_cmp, c->avctx->me_sub_cmp);329 ret |= ff_set_cmp(mecc, mecc->me_sub_cmp, avctx->me_sub_cmp);
331 ret |= ff_set_cmp(&s->mecc, s->mecc.mb_cmp, c->avctx->mb_cmp);330 ret |= ff_set_cmp(mecc, mecc->mb_cmp, avctx->mb_cmp);
332 if (ret < 0)331 if (ret < 0)
333 return ret;332 return ret;
334 333
335 c->flags = get_flags(c, 0, c->avctx->me_cmp &FF_CMP_CHROMA);334 c->flags = get_flags(c, 0, avctx->me_cmp & FF_CMP_CHROMA);
336 c->sub_flags= get_flags(c, 0, c->avctx->me_sub_cmp&FF_CMP_CHROMA);335 c->sub_flags = get_flags(c, 0, avctx->me_sub_cmp & FF_CMP_CHROMA);
337 c->mb_flags = get_flags(c, 0, c->avctx->mb_cmp &FF_CMP_CHROMA);336 c->mb_flags = get_flags(c, 0, avctx->mb_cmp & FF_CMP_CHROMA);
338 337
339/*FIXME s->no_rounding b_type*/338 if (avctx->codec_id == AV_CODEC_ID_H261) {
340 if (s->avctx->flags & AV_CODEC_FLAG_QPEL) {339 c->sub_motion_search = no_sub_motion_search;
340 } else if (avctx->flags & AV_CODEC_FLAG_QPEL) {
341 c->sub_motion_search= qpel_motion_search;341 c->sub_motion_search= qpel_motion_search;
342 c->qpel_avg = s->qdsp.avg_qpel_pixels_tab;
343 if (s->no_rounding)
344 c->qpel_put = s->qdsp.put_no_rnd_qpel_pixels_tab;
345 else
346 c->qpel_put = s->qdsp.put_qpel_pixels_tab;
347 }else{342 }else{
348 if(c->avctx->me_sub_cmp&FF_CMP_CHROMA)343 if(c->avctx->me_sub_cmp&FF_CMP_CHROMA)
349 c->sub_motion_search= hpel_motion_search;344 c->sub_motion_search= hpel_motion_search;
@@ -354,6 +349,32 @@ int ff_init_me(MpegEncContext *s){ · 4 unmodified lines
354 else349 else
355 c->sub_motion_search= hpel_motion_search;350 c->sub_motion_search= hpel_motion_search;
356 }351 }
352
353 /* 8x8 fullpel search would need a 4x4 chroma compare, which we do
354 * not have yet, and even if we had, the motion estimation code
355 * does not expect it. */
356 if (avctx->codec_id != AV_CODEC_ID_SNOW) {
357 if ((avctx->me_cmp & FF_CMP_CHROMA) /* && !s->mecc.me_cmp[2] */)
358 mecc->me_cmp[2] = zero_cmp;
359 if ((avctx->me_sub_cmp & FF_CMP_CHROMA) && !mecc->me_sub_cmp[2])
360 mecc->me_sub_cmp[2] = zero_cmp;
361 }
362
363 return 0;
364}
365
366void ff_me_init_pic(MpegEncContext *s)
367{
368 MotionEstContext * const c= &s->me;
369
370/*FIXME s->no_rounding b_type*/
371 if (s->avctx->flags & AV_CODEC_FLAG_QPEL) {
372 c->qpel_avg = s->qdsp.avg_qpel_pixels_tab;
373 if (s->no_rounding)
374 c->qpel_put = s->qdsp.put_no_rnd_qpel_pixels_tab;
375 else
376 c->qpel_put = s->qdsp.put_qpel_pixels_tab;
377 }
357 c->hpel_avg = s->hdsp.avg_pixels_tab;378 c->hpel_avg = s->hdsp.avg_pixels_tab;
358 if (s->no_rounding)379 if (s->no_rounding)
359 c->hpel_put = s->hdsp.put_no_rnd_pixels_tab;380 c->hpel_put = s->hdsp.put_no_rnd_pixels_tab;
@@ -367,24 +388,10 @@ int ff_init_me(MpegEncContext *s){ · 7 unmodified lines
367 c->stride = 16*s->mb_width + 32;388 c->stride = 16*s->mb_width + 32;
368 c->uvstride= 8*s->mb_width + 16;389 c->uvstride= 8*s->mb_width + 16;
369 }390 }
370
371 /* 8x8 fullpel search would need a 4x4 chroma compare, which we do
372 * not have yet, and even if we had, the motion estimation code
373 * does not expect it. */
374 if (s->codec_id != AV_CODEC_ID_SNOW) {391 if (s->codec_id != AV_CODEC_ID_SNOW) {
375 if ((c->avctx->me_cmp & FF_CMP_CHROMA) /* && !s->mecc.me_cmp[2] */)
376 s->mecc.me_cmp[2] = zero_cmp;
377 if ((c->avctx->me_sub_cmp & FF_CMP_CHROMA) && !s->mecc.me_sub_cmp[2])
378 s->mecc.me_sub_cmp[2] = zero_cmp;
379 c->hpel_put[2][0]= c->hpel_put[2][1]=392 c->hpel_put[2][0]= c->hpel_put[2][1]=
380 c->hpel_put[2][2]= c->hpel_put[2][3]= zero_hpel;393 c->hpel_put[2][2]= c->hpel_put[2][3]= zero_hpel;
381 }394 }
382
383 if(s->codec_id == AV_CODEC_ID_H261){
384 c->sub_motion_search= no_sub_motion_search;
385 }
386
387 return 0;
388}395}
389 396
390#define CHECK_SAD_HALF_MV(suffix, x, y) \397#define CHECK_SAD_HALF_MV(suffix, x, y) \
libavcodec/motion_est.h+8−1
@@ -25,6 +25,7 @@ · 24 unmodified lines
25 25
26#include "avcodec.h"26#include "avcodec.h"
27#include "hpeldsp.h"27#include "hpeldsp.h"
28#include "me_cmp.h"
28#include "qpeldsp.h"29#include "qpeldsp.h"
29 30
30struct MpegEncContext;31struct MpegEncContext;
@@ -105,7 +106,13 @@ static inline int ff_h263_round_chroma(int x) · 74 unmodified lines
105 return h263_chroma_roundtab[x & 0xf] + (x >> 3);106 return h263_chroma_roundtab[x & 0xf] + (x >> 3);
106}107}
107 108
108int ff_init_me(struct MpegEncContext *s);109/**
110 * Performs one-time initialization of the MotionEstContext.
111 */
112int ff_me_init(MotionEstContext *c, struct AVCodecContext *avctx,
113 struct MECmpContext *mecc);
114
115void ff_me_init_pic(struct MpegEncContext *s);
109 116
110void ff_estimate_p_frame_motion(struct MpegEncContext *s, int mb_x, int mb_y);117void ff_estimate_p_frame_motion(struct MpegEncContext *s, int mb_x, int mb_y);
111void ff_estimate_b_frame_motion(struct MpegEncContext *s, int mb_x, int mb_y);118void ff_estimate_b_frame_motion(struct MpegEncContext *s, int mb_x, int mb_y);
libavcodec/mpegvideo_enc.c+16−3
@@ -306,6 +306,18 @@ av_cold void ff_dct_encode_init(MpegEncContext *s) · 305 unmodified lines
306 s->dct_quantize = dct_quantize_trellis_c;306 s->dct_quantize = dct_quantize_trellis_c;
307}307}
308 308
309static av_cold int me_cmp_init(MpegEncContext *s, AVCodecContext *avctx)
310{
311 int ret;
312
313 ff_me_cmp_init(&s->mecc, avctx);
314 ret = ff_me_init(&s->me, avctx, &s->mecc);
315 if (ret < 0)
316 return ret;
317
318 return 0;
319}
320
309/* init video encoder */321/* init video encoder */
310av_cold int ff_mpv_encode_init(AVCodecContext *avctx)322av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
311{323{
@@ -810,9 +822,11 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx) · 498 unmodified lines
810 return ret;822 return ret;
811 823
812 ff_fdctdsp_init(&s->fdsp, avctx);824 ff_fdctdsp_init(&s->fdsp, avctx);
813 ff_me_cmp_init(&s->mecc, avctx);
814 ff_mpegvideoencdsp_init(&s->mpvencdsp, avctx);825 ff_mpegvideoencdsp_init(&s->mpvencdsp, avctx);
815 ff_pixblockdsp_init(&s->pdsp, avctx);826 ff_pixblockdsp_init(&s->pdsp, avctx);
827 ret = me_cmp_init(s, avctx);
828 if (ret < 0)
829 return ret;
816 830
817 if (!(avctx->stats_out = av_mallocz(256)) ||831 if (!(avctx->stats_out = av_mallocz(256)) ||
818 !FF_ALLOCZ_TYPED_ARRAY(s->q_intra_matrix, 32) ||832 !FF_ALLOCZ_TYPED_ARRAY(s->q_intra_matrix, 32) ||
@@ -3603,8 +3617,7 @@ static int encode_picture(MpegEncContext *s, const AVPacket *pkt) · 2784 unmodified lines
3603 s->q_chroma_intra_matrix16 = s->q_intra_matrix16;3617 s->q_chroma_intra_matrix16 = s->q_intra_matrix16;
3604 }3618 }
3605 3619
3606 if(ff_init_me(s)<0)3620 ff_me_init_pic(s);
3607 return -1;
3608 3621
3609 s->mb_intra=0; //for the rate distortion & bit compare functions3622 s->mb_intra=0; //for the rate distortion & bit compare functions
3610 for (int i = 0; i < context_count; i++) {3623 for (int i = 0; i < context_count; i++) {
libavcodec/snowenc.c+4−7
@@ -217,6 +217,9 @@ static av_cold int encode_init(AVCodecContext *avctx) · 216 unmodified lines
217 mcf(12,12)217 mcf(12,12)
218 218
219 ff_me_cmp_init(&enc->mecc, avctx);219 ff_me_cmp_init(&enc->mecc, avctx);
220 ret = ff_me_init(&enc->m.me, avctx, &enc->mecc);
221 if (ret < 0)
222 return ret;
220 ff_mpegvideoencdsp_init(&enc->mpvencdsp, avctx);223 ff_mpegvideoencdsp_init(&enc->mpvencdsp, avctx);
221 224
222 ff_snow_alloc_blocks(s);225 ff_snow_alloc_blocks(s);
@@ -278,11 +281,6 @@ static av_cold int encode_init(AVCodecContext *avctx) · 55 unmodified lines
278 if (ret)281 if (ret)
279 return ret;282 return ret;
280 283
281 ret = ff_set_cmp(&enc->mecc, enc->mecc.me_cmp, s->avctx->me_cmp);
282 ret |= ff_set_cmp(&enc->mecc, enc->mecc.me_sub_cmp, s->avctx->me_sub_cmp);
283 if (ret < 0)
284 return AVERROR(EINVAL);
285
286 s->input_picture = av_frame_alloc();284 s->input_picture = av_frame_alloc();
287 if (!s->input_picture)285 if (!s->input_picture)
288 return AVERROR(ENOMEM);286 return AVERROR(ENOMEM);
@@ -1874,9 +1872,8 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, · 1585 unmodified lines
1874 mpv->mecc = enc->mecc; //move1872 mpv->mecc = enc->mecc; //move
1875 mpv->qdsp = enc->qdsp; //move1873 mpv->qdsp = enc->qdsp; //move
1876 mpv->hdsp = s->hdsp;1874 mpv->hdsp = s->hdsp;
1877 ff_init_me(&enc->m);1875 ff_me_init_pic(&enc->m);
1878 s->hdsp = mpv->hdsp;1876 s->hdsp = mpv->hdsp;
1879 enc->mecc = mpv->mecc;
1880 }1877 }
1881 1878
1882 if (enc->pass1_rc) {1879 if (enc->pass1_rc) {
libavcodec/svq1enc.c+4−1
@@ -374,7 +374,7 @@ static int svq1_encode_plane(SVQ1EncContext *s, int plane, · 373 unmodified lines
374 s->m.p_mv_table = s->motion_val16[plane] +374 s->m.p_mv_table = s->motion_val16[plane] +
375 s->m.mb_stride + 1;375 s->m.mb_stride + 1;
376 s->m.mecc = s->mecc; // move376 s->m.mecc = s->mecc; // move
377 ff_init_me(&s->m);377 ff_me_init_pic(&s->m);
378 378
379 s->m.me.dia_size = s->avctx->dia_size;379 s->m.me.dia_size = s->avctx->dia_size;
380 s->m.first_slice_line = 1;380 s->m.first_slice_line = 1;
@@ -589,6 +589,9 @@ static av_cold int svq1_encode_init(AVCodecContext *avctx) · 208 unmodified lines
589 589
590 ff_hpeldsp_init(&s->hdsp, avctx->flags);590 ff_hpeldsp_init(&s->hdsp, avctx->flags);
591 ff_me_cmp_init(&s->mecc, avctx);591 ff_me_cmp_init(&s->mecc, avctx);
592 ret = ff_me_init(&s->m.me, avctx, &s->mecc);
593 if (ret < 0)
594 return ret;
592 ff_mpegvideoencdsp_init(&s->m.mpvencdsp, avctx);595 ff_mpegvideoencdsp_init(&s->m.mpvencdsp, avctx);
593 596
594 s->current_picture = av_frame_alloc();597 s->current_picture = av_frame_alloc();

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t