[FFmpeg-devel] [PATCH 20/57] avcodec/motion_est: Factor one-time initialization out of ff_init_me
Andreas Rheinhardt
andreas.rheinhardt at outlook.com
Wed Jun 12 16:48:16 EEST 2024
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.
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
---
libavcodec/motion_est.c | 89 ++++++++++++++++++++------------------
libavcodec/motion_est.h | 9 +++-
libavcodec/mpegvideo_enc.c | 19 ++++++--
libavcodec/snowenc.c | 11 ++---
libavcodec/svq1enc.c | 5 ++-
5 files changed, 80 insertions(+), 53 deletions(-)
diff --git a/libavcodec/motion_est.c b/libavcodec/motion_est.c
index 162472d693..ee28a4a445 100644
--- a/libavcodec/motion_est.c
+++ b/libavcodec/motion_est.c
@@ -305,45 +305,40 @@ static int zero_cmp(MpegEncContext *s, const uint8_t *a, const uint8_t *b,
static void zero_hpel(uint8_t *a, const uint8_t *b, ptrdiff_t stride, int h){
}
-int ff_init_me(MpegEncContext *s){
- MotionEstContext * const c= &s->me;
- int cache_size= FFMIN(ME_MAP_SIZE>>ME_MAP_SHIFT, 1<<ME_MAP_SHIFT);
- int dia_size= FFMAX(FFABS(s->avctx->dia_size)&255, FFABS(s->avctx->pre_dia_size)&255);
+av_cold int ff_me_init(MotionEstContext *c, AVCodecContext *avctx, MECmpContext *mecc)
+{
+ int cache_size = FFMIN(ME_MAP_SIZE>>ME_MAP_SHIFT, 1<<ME_MAP_SHIFT);
+ int dia_size = FFMAX(FFABS(avctx->dia_size) & 255, FFABS(avctx->pre_dia_size) & 255);
int ret;
- if(FFMIN(s->avctx->dia_size, s->avctx->pre_dia_size) < -FFMIN(ME_MAP_SIZE, MAX_SAB_SIZE)){
- av_log(s->avctx, AV_LOG_ERROR, "ME_MAP size is too small for SAB diamond\n");
- return -1;
+ if (FFMIN(avctx->dia_size, avctx->pre_dia_size) < -FFMIN(ME_MAP_SIZE, MAX_SAB_SIZE)) {
+ av_log(avctx, AV_LOG_ERROR, "ME_MAP size is too small for SAB diamond\n");
+ return AVERROR(EINVAL);
}
- c->avctx= s->avctx;
+ c->avctx = avctx;
- if(s->codec_id == AV_CODEC_ID_H261)
- c->avctx->me_sub_cmp = c->avctx->me_cmp;
+ if (avctx->codec_id == AV_CODEC_ID_H261)
+ avctx->me_sub_cmp = avctx->me_cmp;
- if(cache_size < 2*dia_size && !c->stride){
- av_log(s->avctx, AV_LOG_INFO, "ME_MAP size may be a little small for the selected diamond size\n");
- }
+ if (cache_size < 2 * dia_size)
+ av_log(avctx, AV_LOG_INFO, "ME_MAP size may be a little small for the selected diamond size\n");
- ret = ff_set_cmp(&s->mecc, s->mecc.me_pre_cmp, c->avctx->me_pre_cmp);
- ret |= ff_set_cmp(&s->mecc, s->mecc.me_cmp, c->avctx->me_cmp);
- ret |= ff_set_cmp(&s->mecc, s->mecc.me_sub_cmp, c->avctx->me_sub_cmp);
- ret |= ff_set_cmp(&s->mecc, s->mecc.mb_cmp, c->avctx->mb_cmp);
+ ret = ff_set_cmp(mecc, mecc->me_pre_cmp, avctx->me_pre_cmp);
+ ret |= ff_set_cmp(mecc, mecc->me_cmp, avctx->me_cmp);
+ ret |= ff_set_cmp(mecc, mecc->me_sub_cmp, avctx->me_sub_cmp);
+ ret |= ff_set_cmp(mecc, mecc->mb_cmp, avctx->mb_cmp);
if (ret < 0)
return ret;
- c->flags = get_flags(c, 0, c->avctx->me_cmp &FF_CMP_CHROMA);
- c->sub_flags= get_flags(c, 0, c->avctx->me_sub_cmp&FF_CMP_CHROMA);
- c->mb_flags = get_flags(c, 0, c->avctx->mb_cmp &FF_CMP_CHROMA);
+ c->flags = get_flags(c, 0, avctx->me_cmp & FF_CMP_CHROMA);
+ c->sub_flags = get_flags(c, 0, avctx->me_sub_cmp & FF_CMP_CHROMA);
+ c->mb_flags = get_flags(c, 0, avctx->mb_cmp & FF_CMP_CHROMA);
-/*FIXME s->no_rounding b_type*/
- if (s->avctx->flags & AV_CODEC_FLAG_QPEL) {
+ if (avctx->codec_id == AV_CODEC_ID_H261) {
+ c->sub_motion_search = no_sub_motion_search;
+ } else if (avctx->flags & AV_CODEC_FLAG_QPEL) {
c->sub_motion_search= qpel_motion_search;
- c->qpel_avg = s->qdsp.avg_qpel_pixels_tab;
- if (s->no_rounding)
- c->qpel_put = s->qdsp.put_no_rnd_qpel_pixels_tab;
- else
- c->qpel_put = s->qdsp.put_qpel_pixels_tab;
}else{
if(c->avctx->me_sub_cmp&FF_CMP_CHROMA)
c->sub_motion_search= hpel_motion_search;
@@ -354,6 +349,32 @@ int ff_init_me(MpegEncContext *s){
else
c->sub_motion_search= hpel_motion_search;
}
+
+ /* 8x8 fullpel search would need a 4x4 chroma compare, which we do
+ * not have yet, and even if we had, the motion estimation code
+ * does not expect it. */
+ if (avctx->codec_id != AV_CODEC_ID_SNOW) {
+ if ((avctx->me_cmp & FF_CMP_CHROMA) /* && !s->mecc.me_cmp[2] */)
+ mecc->me_cmp[2] = zero_cmp;
+ if ((avctx->me_sub_cmp & FF_CMP_CHROMA) && !mecc->me_sub_cmp[2])
+ mecc->me_sub_cmp[2] = zero_cmp;
+ }
+
+ return 0;
+}
+
+void ff_me_init_pic(MpegEncContext *s)
+{
+ MotionEstContext * const c= &s->me;
+
+/*FIXME s->no_rounding b_type*/
+ if (s->avctx->flags & AV_CODEC_FLAG_QPEL) {
+ c->qpel_avg = s->qdsp.avg_qpel_pixels_tab;
+ if (s->no_rounding)
+ c->qpel_put = s->qdsp.put_no_rnd_qpel_pixels_tab;
+ else
+ c->qpel_put = s->qdsp.put_qpel_pixels_tab;
+ }
c->hpel_avg = s->hdsp.avg_pixels_tab;
if (s->no_rounding)
c->hpel_put = s->hdsp.put_no_rnd_pixels_tab;
@@ -367,24 +388,10 @@ int ff_init_me(MpegEncContext *s){
c->stride = 16*s->mb_width + 32;
c->uvstride= 8*s->mb_width + 16;
}
-
- /* 8x8 fullpel search would need a 4x4 chroma compare, which we do
- * not have yet, and even if we had, the motion estimation code
- * does not expect it. */
if (s->codec_id != AV_CODEC_ID_SNOW) {
- if ((c->avctx->me_cmp & FF_CMP_CHROMA) /* && !s->mecc.me_cmp[2] */)
- s->mecc.me_cmp[2] = zero_cmp;
- if ((c->avctx->me_sub_cmp & FF_CMP_CHROMA) && !s->mecc.me_sub_cmp[2])
- s->mecc.me_sub_cmp[2] = zero_cmp;
c->hpel_put[2][0]= c->hpel_put[2][1]=
c->hpel_put[2][2]= c->hpel_put[2][3]= zero_hpel;
}
-
- if(s->codec_id == AV_CODEC_ID_H261){
- c->sub_motion_search= no_sub_motion_search;
- }
-
- return 0;
}
#define CHECK_SAD_HALF_MV(suffix, x, y) \
diff --git a/libavcodec/motion_est.h b/libavcodec/motion_est.h
index f6a563b08c..feea9a473b 100644
--- a/libavcodec/motion_est.h
+++ b/libavcodec/motion_est.h
@@ -25,6 +25,7 @@
#include "avcodec.h"
#include "hpeldsp.h"
+#include "me_cmp.h"
#include "qpeldsp.h"
struct MpegEncContext;
@@ -105,7 +106,13 @@ static inline int ff_h263_round_chroma(int x)
return h263_chroma_roundtab[x & 0xf] + (x >> 3);
}
-int ff_init_me(struct MpegEncContext *s);
+/**
+ * Performs one-time initialization of the MotionEstContext.
+ */
+int ff_me_init(MotionEstContext *c, struct AVCodecContext *avctx,
+ struct MECmpContext *mecc);
+
+void ff_me_init_pic(struct MpegEncContext *s);
void ff_estimate_p_frame_motion(struct MpegEncContext *s, int mb_x, int mb_y);
void ff_estimate_b_frame_motion(struct MpegEncContext *s, int mb_x, int mb_y);
diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c
index c97120de21..5b8d877935 100644
--- a/libavcodec/mpegvideo_enc.c
+++ b/libavcodec/mpegvideo_enc.c
@@ -306,6 +306,18 @@ av_cold void ff_dct_encode_init(MpegEncContext *s)
s->dct_quantize = dct_quantize_trellis_c;
}
+static av_cold int me_cmp_init(MpegEncContext *s, AVCodecContext *avctx)
+{
+ int ret;
+
+ ff_me_cmp_init(&s->mecc, avctx);
+ ret = ff_me_init(&s->me, avctx, &s->mecc);
+ if (ret < 0)
+ return ret;
+
+ return 0;
+}
+
/* init video encoder */
av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
{
@@ -810,9 +822,11 @@ av_cold int ff_mpv_encode_init(AVCodecContext *avctx)
return ret;
ff_fdctdsp_init(&s->fdsp, avctx);
- ff_me_cmp_init(&s->mecc, avctx);
ff_mpegvideoencdsp_init(&s->mpvencdsp, avctx);
ff_pixblockdsp_init(&s->pdsp, avctx);
+ ret = me_cmp_init(s, avctx);
+ if (ret < 0)
+ return ret;
if (!(avctx->stats_out = av_mallocz(256)) ||
!FF_ALLOCZ_TYPED_ARRAY(s->q_intra_matrix, 32) ||
@@ -3603,8 +3617,7 @@ static int encode_picture(MpegEncContext *s, const AVPacket *pkt)
s->q_chroma_intra_matrix16 = s->q_intra_matrix16;
}
- if(ff_init_me(s)<0)
- return -1;
+ ff_me_init_pic(s);
s->mb_intra=0; //for the rate distortion & bit compare functions
for (int i = 0; i < context_count; i++) {
diff --git a/libavcodec/snowenc.c b/libavcodec/snowenc.c
index 8d6dabae65..f3c78cfb21 100644
--- a/libavcodec/snowenc.c
+++ b/libavcodec/snowenc.c
@@ -217,6 +217,9 @@ static av_cold int encode_init(AVCodecContext *avctx)
mcf(12,12)
ff_me_cmp_init(&enc->mecc, avctx);
+ ret = ff_me_init(&enc->m.me, avctx, &enc->mecc);
+ if (ret < 0)
+ return ret;
ff_mpegvideoencdsp_init(&enc->mpvencdsp, avctx);
ff_snow_alloc_blocks(s);
@@ -278,11 +281,6 @@ static av_cold int encode_init(AVCodecContext *avctx)
if (ret)
return ret;
- ret = ff_set_cmp(&enc->mecc, enc->mecc.me_cmp, s->avctx->me_cmp);
- ret |= ff_set_cmp(&enc->mecc, enc->mecc.me_sub_cmp, s->avctx->me_sub_cmp);
- if (ret < 0)
- return AVERROR(EINVAL);
-
s->input_picture = av_frame_alloc();
if (!s->input_picture)
return AVERROR(ENOMEM);
@@ -1874,9 +1872,8 @@ static int encode_frame(AVCodecContext *avctx, AVPacket *pkt,
mpv->mecc = enc->mecc; //move
mpv->qdsp = enc->qdsp; //move
mpv->hdsp = s->hdsp;
- ff_init_me(&enc->m);
+ ff_me_init_pic(&enc->m);
s->hdsp = mpv->hdsp;
- enc->mecc = mpv->mecc;
}
if (enc->pass1_rc) {
diff --git a/libavcodec/svq1enc.c b/libavcodec/svq1enc.c
index 35413b8afd..ceb8bf83c2 100644
--- a/libavcodec/svq1enc.c
+++ b/libavcodec/svq1enc.c
@@ -374,7 +374,7 @@ static int svq1_encode_plane(SVQ1EncContext *s, int plane,
s->m.p_mv_table = s->motion_val16[plane] +
s->m.mb_stride + 1;
s->m.mecc = s->mecc; // move
- ff_init_me(&s->m);
+ ff_me_init_pic(&s->m);
s->m.me.dia_size = s->avctx->dia_size;
s->m.first_slice_line = 1;
@@ -589,6 +589,9 @@ static av_cold int svq1_encode_init(AVCodecContext *avctx)
ff_hpeldsp_init(&s->hdsp, avctx->flags);
ff_me_cmp_init(&s->mecc, avctx);
+ ret = ff_me_init(&s->m.me, avctx, &s->mecc);
+ if (ret < 0)
+ return ret;
ff_mpegvideoencdsp_init(&s->m.mpvencdsp, avctx);
s->current_picture = av_frame_alloc();
--
2.40.1
More information about the ffmpeg-devel
mailing list