[FFmpeg-cvslog] avcodec/{ituh263,mpeg4video}enc: Simplify out-of-slice DC prediction
Andreas Rheinhardt
git at videolan.org
Sat Jun 21 23:21:30 EEST 2025
ffmpeg | branch: master | Andreas Rheinhardt <andreas.rheinhardt at outlook.com> | Fri Jun 13 22:41:02 2025 +0200| [56fd45d2a9387f5cbc6e254eb78e2f58674bb8f4] | committer: Andreas Rheinhardt
avcodec/{ituh263,mpeg4video}enc: Simplify out-of-slice DC prediction
Now that every slice (and therefore every thread) uses its own
DC array, one can simply set the out-of-slice DC values to its
expected values (this is not possible for the decoders due
to error resilience).
Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt at outlook.com>
> http://git.videolan.org/gitweb.cgi/ffmpeg.git/?a=commit;h=56fd45d2a9387f5cbc6e254eb78e2f58674bb8f4
---
libavcodec/h263enc.h | 1 +
libavcodec/ituh263enc.c | 21 ++++++++++++++++-----
libavcodec/mpeg4videoenc.c | 32 +++++++++++++++++++++++++++++++-
libavcodec/mpegvideo_enc.c | 8 +++++++-
4 files changed, 55 insertions(+), 7 deletions(-)
diff --git a/libavcodec/h263enc.h b/libavcodec/h263enc.h
index 63af3a576f..20e0c57326 100644
--- a/libavcodec/h263enc.h
+++ b/libavcodec/h263enc.h
@@ -27,6 +27,7 @@
const uint8_t (*ff_h263_get_mv_penalty(void))[MAX_DMV*2+1];
void ff_h263_encode_init(MPVMainEncContext *m);
+void ff_h263_mpeg4_reset_dc(MPVEncContext *s);
void ff_h263_encode_gob_header(MPVEncContext *s, int mb_line);
void ff_h263_encode_mba(MPVEncContext *s);
diff --git a/libavcodec/ituh263enc.c b/libavcodec/ituh263enc.c
index 9a8671588c..d1d07bc5f8 100644
--- a/libavcodec/ituh263enc.c
+++ b/libavcodec/ituh263enc.c
@@ -361,6 +361,22 @@ static int h263_encode_picture_header(MPVMainEncContext *const m)
return 0;
}
+void ff_h263_mpeg4_reset_dc(MPVEncContext *s)
+{
+ int16_t *dc = s->c.dc_val;
+
+ // The "- 1" is for the top-left entry
+ const int l_xy = s->c.block_index[2];
+ for (int i = l_xy - 2 * s->c.b8_stride - 1; i < l_xy; i += 2)
+ AV_WN32A(dc + i, 1024 << 16 | 1024);
+
+ const int u_xy = s->c.block_index[4];
+ const int v_xy = s->c.block_index[5];
+ int16_t *dc2 = dc + v_xy - u_xy;
+ for (int i = u_xy - s->c.mb_stride - 1; i < u_xy; ++i)
+ dc[i] = dc2[i] = 1024;
+}
+
/**
* Encode a group of blocks header.
*/
@@ -606,11 +622,6 @@ static int h263_pred_dc(MPVEncContext *const s, int n, int16_t **dc_val_ptr)
int a = dc_val[-1];
int c = dc_val[-wrap];
- /* No prediction outside GOB boundary */
- if (s->c.first_slice_line && n != 3) {
- if (n != 2) c = 1024;
- if (n != 1 && s->c.mb_x == s->c.resync_mb_x) a = 1024;
- }
/* just DC prediction */
if (a != 1024 && c != 1024)
pred_dc = (a + c) >> 1;
diff --git a/libavcodec/mpeg4videoenc.c b/libavcodec/mpeg4videoenc.c
index 27c0ee475f..1b8ccad380 100644
--- a/libavcodec/mpeg4videoenc.c
+++ b/libavcodec/mpeg4videoenc.c
@@ -145,6 +145,36 @@ static inline void restore_ac_coeffs(MPVEncContext *const s, int16_t block[6][64
}
}
+/**
+ * Predict the dc.
+ * @param n block index (0-3 are luma, 4-5 are chroma)
+ * @param dir_ptr pointer to an integer where the prediction direction will be stored
+ */
+static int mpeg4_pred_dc(MpegEncContext *s, int n, int *dir_ptr)
+{
+ const int16_t *const dc_val = s->dc_val + s->block_index[n];
+ const int wrap = s->block_wrap[n];
+
+ /* B C
+ * A X
+ */
+ const int a = dc_val[-1];
+ const int b = dc_val[-1 - wrap];
+ const int c = dc_val[-wrap];
+ int pred;
+
+ // There is no need for out-of-slice handling here, as all values are set
+ // appropriately when a new slice is opened.
+ if (abs(a - b) < abs(b - c)) {
+ pred = c;
+ *dir_ptr = 1; /* top */
+ } else {
+ pred = a;
+ *dir_ptr = 0; /* left */
+ }
+ return pred;
+}
+
/**
* Return the optimal value (0 or 1) for the ac_pred element for the given MB in MPEG-4.
* This function will also update s->c.block_last_index and s->c.ac_val.
@@ -737,7 +767,7 @@ static void mpeg4_encode_mb(MPVEncContext *const s, int16_t block[][64],
int i;
for (int i = 0; i < 6; i++) {
- int pred = ff_mpeg4_pred_dc(&s->c, i, &dir[i]);
+ int pred = mpeg4_pred_dc(&s->c, i, &dir[i]);
int scale = i < 4 ? s->c.y_dc_scale : s->c.c_dc_scale;
pred = FASTDIV((pred + (scale >> 1)), scale);
diff --git a/libavcodec/mpegvideo_enc.c b/libavcodec/mpegvideo_enc.c
index 1f581ec500..1f51f237fa 100644
--- a/libavcodec/mpegvideo_enc.c
+++ b/libavcodec/mpegvideo_enc.c
@@ -3140,6 +3140,7 @@ static int encode_thread(AVCodecContext *c, void *arg){
if (CONFIG_MPEG4_ENCODER) {
ff_mpeg4_encode_video_packet_header(s);
ff_mpeg4_clean_buffers(&s->c);
+ ff_h263_mpeg4_reset_dc(s);
}
break;
case AV_CODEC_ID_MPEG1VIDEO:
@@ -3149,8 +3150,13 @@ static int encode_thread(AVCodecContext *c, void *arg){
ff_mpeg1_clean_buffers(&s->c);
}
break;
- case AV_CODEC_ID_H263:
+#if CONFIG_H263P_ENCODER
case AV_CODEC_ID_H263P:
+ if (s->c.dc_val)
+ ff_h263_mpeg4_reset_dc(s);
+ // fallthrough
+#endif
+ case AV_CODEC_ID_H263:
if (CONFIG_H263_ENCODER) {
update_mb_info(s, 1);
ff_h263_encode_gob_header(s, mb_y);
More information about the ffmpeg-cvslog
mailing list