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

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
305305 static void zero_hpel(uint8_t *a, const uint8_t *b, ptrdiff_t stride, int h){
306306 }
307307
308−int ff_init_me(MpegEncContext *s){
309− MotionEstContext * const c= &s->me;
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);
308+av_cold int ff_me_init(MotionEstContext *c, AVCodecContext *avctx, MECmpContext *mecc)
309+{
310+ int cache_size = FFMIN(ME_MAP_SIZE>>ME_MAP_SHIFT, 1<<ME_MAP_SHIFT);
311+ int dia_size = FFMAX(FFABS(avctx->dia_size) & 255, FFABS(avctx->pre_dia_size) & 255);
312312 int ret;
313313
314− if(FFMIN(s->avctx->dia_size, s->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");
316− return -1;
314+ if (FFMIN(avctx->dia_size, avctx->pre_dia_size) < -FFMIN(ME_MAP_SIZE, MAX_SAB_SIZE)) {
315+ av_log(avctx, AV_LOG_ERROR, "ME_MAP size is too small for SAB diamond\n");
316+ return AVERROR(EINVAL);
317317 }
318318
319− c->avctx= s->avctx;
319+ c->avctx = avctx;
320320
321− if(s->codec_id == AV_CODEC_ID_H261)
322− c->avctx->me_sub_cmp = c->avctx->me_cmp;
321+ if (avctx->codec_id == AV_CODEC_ID_H261)
322+ avctx->me_sub_cmp = avctx->me_cmp;
323323
324− if(cache_size < 2*dia_size && !c->stride){
325− av_log(s->avctx, AV_LOG_INFO, "ME_MAP size may be a little small for the selected diamond size\n");
326− }
324+ if (cache_size < 2 * dia_size)
325+ av_log(avctx, AV_LOG_INFO, "ME_MAP size may be a little small for the selected diamond size\n");
327326
328− ret = ff_set_cmp(&s->mecc, s->mecc.me_pre_cmp, c->avctx->me_pre_cmp);
329− ret |= ff_set_cmp(&s->mecc, s->mecc.me_cmp, c->avctx->me_cmp);
330− ret |= ff_set_cmp(&s->mecc, s->mecc.me_sub_cmp, c->avctx->me_sub_cmp);
331− ret |= ff_set_cmp(&s->mecc, s->mecc.mb_cmp, c->avctx->mb_cmp);
327+ ret = ff_set_cmp(mecc, mecc->me_pre_cmp, avctx->me_pre_cmp);
328+ ret |= ff_set_cmp(mecc, mecc->me_cmp, avctx->me_cmp);
329+ ret |= ff_set_cmp(mecc, mecc->me_sub_cmp, avctx->me_sub_cmp);
330+ ret |= ff_set_cmp(mecc, mecc->mb_cmp, avctx->mb_cmp);
332331 if (ret < 0)
333332 return ret;
334333
335− c->flags = get_flags(c, 0, c->avctx->me_cmp &FF_CMP_CHROMA);
336− c->sub_flags= get_flags(c, 0, c->avctx->me_sub_cmp&FF_CMP_CHROMA);
337− c->mb_flags = get_flags(c, 0, c->avctx->mb_cmp &FF_CMP_CHROMA);
334+ c->flags = get_flags(c, 0, avctx->me_cmp & FF_CMP_CHROMA);
335+ c->sub_flags = get_flags(c, 0, avctx->me_sub_cmp & FF_CMP_CHROMA);
336+ c->mb_flags = get_flags(c, 0, avctx->mb_cmp & FF_CMP_CHROMA);
338337
339−/*FIXME s->no_rounding b_type*/
340− if (s->avctx->flags & AV_CODEC_FLAG_QPEL) {
338+ if (avctx->codec_id == AV_CODEC_ID_H261) {
339+ c->sub_motion_search = no_sub_motion_search;
340+ } else if (avctx->flags & AV_CODEC_FLAG_QPEL) {
341341 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;
347342 }else{
348343 if(c->avctx->me_sub_cmp&FF_CMP_CHROMA)
349344 c->sub_motion_search= hpel_motion_search;
@@ -354,6 +349,32 @@ int ff_init_me(MpegEncContext *s){ · 4 unmodified lines
354349 else
355350 c->sub_motion_search= hpel_motion_search;
356351 }
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+
366+void 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+ }
357378 c->hpel_avg = s->hdsp.avg_pixels_tab;
358379 if (s->no_rounding)
359380 c->hpel_put = s->hdsp.put_no_rnd_pixels_tab;
@@ -367,24 +388,10 @@ int ff_init_me(MpegEncContext *s){ · 7 unmodified lines
367388 c->stride = 16*s->mb_width + 32;
368389 c->uvstride= 8*s->mb_width + 16;
369390 }
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. */
374391 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;
379392 c->hpel_put[2][0]= c->hpel_put[2][1]=
380393 c->hpel_put[2][2]= c->hpel_put[2][3]= zero_hpel;
381394 }
382−
383− if(s->codec_id == AV_CODEC_ID_H261){
384− c->sub_motion_search= no_sub_motion_search;
385− }
386−
387− return 0;
388395 }
389396
390397 #define CHECK_SAD_HALF_MV(suffix, x, y) \
libavcodec/motion_est.h+8−1
@@ -25,6 +25,7 @@ · 24 unmodified lines
2525
2626 #include "avcodec.h"
2727 #include "hpeldsp.h"
28+#include "me_cmp.h"
2829 #include "qpeldsp.h"
2930
3031 struct MpegEncContext;
@@ -105,7 +106,13 @@ static inline int ff_h263_round_chroma(int x) · 74 unmodified lines
105106 return h263_chroma_roundtab[x & 0xf] + (x >> 3);
106107 }
107108
108−int ff_init_me(struct MpegEncContext *s);
109+/**
110+ * Performs one-time initialization of the MotionEstContext.
111+ */
112+int ff_me_init(MotionEstContext *c, struct AVCodecContext *avctx,
113+ struct MECmpContext *mecc);
114+
115+void ff_me_init_pic(struct MpegEncContext *s);
109116
110117 void ff_estimate_p_frame_motion(struct MpegEncContext *s, int mb_x, int mb_y);
111118 void 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
306306 s->dct_quantize = dct_quantize_trellis_c;
307307 }
308308
309+static 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+
309321 /* init video encoder */
310322 av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
311323 {
@@ -810,9 +822,11 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx) · 498 unmodified lines
810822 return ret;
811823
812824 ff_fdctdsp_init(&s->fdsp, avctx);
813− ff_me_cmp_init(&s->mecc, avctx);
814825 ff_mpegvideoencdsp_init(&s->mpvencdsp, avctx);
815826 ff_pixblockdsp_init(&s->pdsp, avctx);
827+ ret = me_cmp_init(s, avctx);
828+ if (ret < 0)
829+ return ret;
816830
817831 if (!(avctx->stats_out = av_mallocz(256)) ||
818832 !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
36033617 s->q_chroma_intra_matrix16 = s->q_intra_matrix16;
36043618 }
36053619
3606− if(ff_init_me(s)<0)
3607− return -1;
3620+ ff_me_init_pic(s);
36083621
36093622 s->mb_intra=0; //for the rate distortion & bit compare functions
36103623 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
217217 mcf(12,12)
218218
219219 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;
220223 ff_mpegvideoencdsp_init(&enc->mpvencdsp, avctx);
221224
222225 ff_snow_alloc_blocks(s);
@@ -278,11 +281,6 @@ static av_cold int encode_init(AVCodecContext *avctx) · 55 unmodified lines
278281 if (ret)
279282 return ret;
280283
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−
286284 s->input_picture = av_frame_alloc();
287285 if (!s->input_picture)
288286 return AVERROR(ENOMEM);
@@ -1874,9 +1872,8 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt, · 1585 unmodified lines
18741872 mpv->mecc = enc->mecc; //move
18751873 mpv->qdsp = enc->qdsp; //move
18761874 mpv->hdsp = s->hdsp;
1877− ff_init_me(&enc->m);
1875+ ff_me_init_pic(&enc->m);
18781876 s->hdsp = mpv->hdsp;
1879− enc->mecc = mpv->mecc;
18801877 }
18811878
18821879 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
374374 s->m.p_mv_table = s->motion_val16[plane] +
375375 s->m.mb_stride + 1;
376376 s->m.mecc = s->mecc; // move
377− ff_init_me(&s->m);
377+ ff_me_init_pic(&s->m);
378378
379379 s->m.me.dia_size = s->avctx->dia_size;
380380 s->m.first_slice_line = 1;
@@ -589,6 +589,9 @@ static av_cold int svq1_encode_init(AVCodecContext *avctx) · 208 unmodified lines
589589
590590 ff_hpeldsp_init(&s->hdsp, avctx->flags);
591591 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;
592595 ff_mpegvideoencdsp_init(&s->m.mpvencdsp, avctx);
593596
594597 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