Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
 help / color / mirror / Atom feed
* [FFmpeg-devel] [PR] avcodec/error_resilience: Initialize error resilience context earlier (PR #22318)
@ 2026-02-27 17:13 mkver via ffmpeg-devel
  0 siblings, 0 replies; only message in thread
From: mkver via ffmpeg-devel @ 2026-02-27 17:13 UTC (permalink / raw)
  To: ffmpeg-devel; +Cc: mkver

PR #22318 opened by mkver
URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/22318
Patch URL: https://code.ffmpeg.org/FFmpeg/FFmpeg/pulls/22318.patch

(Btw: Could someone please port the me_cmp 16x16 sum-of-absolute-differences functions for arches != x86 to pixelutils, so that we can avoid the error_resilience->mc_cmp dependency?)


>From c822d0249ac9f05678a4b7f7ece94c55b57e2f32 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Fri, 27 Feb 2026 16:36:00 +0100
Subject: [PATCH 1/2] avcodec/error_resilience: Initialize error resilience
 context earlier

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/error_resilience.c | 16 +++++++++-------
 libavcodec/error_resilience.h |  3 ++-
 libavcodec/h264dec.c          |  2 ++
 libavcodec/mpeg_er.c          |  2 ++
 4 files changed, 15 insertions(+), 8 deletions(-)

diff --git a/libavcodec/error_resilience.c b/libavcodec/error_resilience.c
index cf6f83e096..76d5bfa73b 100644
--- a/libavcodec/error_resilience.c
+++ b/libavcodec/error_resilience.c
@@ -28,6 +28,7 @@
 #include <limits.h>
 
 #include "libavutil/avassert.h"
+#include "libavutil/attributes.h"
 #include "libavutil/mem.h"
 #include "avcodec.h"
 #include "error_resilience.h"
@@ -38,6 +39,14 @@
 #include "threadframe.h"
 #include "threadprogress.h"
 
+av_cold void ff_er_init(ERContext *const s)
+{
+    MECmpContext mecc;
+
+    ff_me_cmp_init(&mecc, s->avctx);
+    s->sad = mecc.sad[0];
+}
+
 /**
  * @param stride the number of MVs to get to the next row
  * @param mv_step the number of MVs per row or column in a macroblock
@@ -795,13 +804,6 @@ void ff_er_frame_start(ERContext *s)
     if (!s->avctx->error_concealment)
         return;
 
-    if (!s->mecc_inited) {
-        MECmpContext mecc;
-        ff_me_cmp_init(&mecc, s->avctx);
-        s->sad = mecc.sad[0];
-        s->mecc_inited = 1;
-    }
-
     memset(s->error_status_table, ER_MB_ERROR | VP_START | ER_MB_END,
            s->mb_stride * s->mb_height * sizeof(uint8_t));
     atomic_init(&s->error_count, 3 * s->mb_num);
diff --git a/libavcodec/error_resilience.h b/libavcodec/error_resilience.h
index 8e43219b09..6c8736a445 100644
--- a/libavcodec/error_resilience.h
+++ b/libavcodec/error_resilience.h
@@ -56,7 +56,6 @@ typedef struct ERContext {
 
     int (*sad)(MPVEncContext *unused, const uint8_t *blk1,
                const uint8_t *blk2, ptrdiff_t stride, int h);
-    int mecc_inited;
 
     int *mb_index2xy;
     int mb_num;
@@ -91,6 +90,8 @@ typedef struct ERContext {
     void *opaque;
 } ERContext;
 
+void ff_er_init(ERContext *const s);
+
 void ff_er_frame_start(ERContext *s);
 
 /**
diff --git a/libavcodec/h264dec.c b/libavcodec/h264dec.c
index ab31832308..e430bc58fc 100644
--- a/libavcodec/h264dec.c
+++ b/libavcodec/h264dec.c
@@ -255,6 +255,8 @@ int ff_h264_alloc_tables(H264Context *h)
         er->dc_val[2] = er->dc_val[1] + big_mb_num;
         for (int i = 0; i < yc_size; i++)
             h->dc_val_base[i] = 1024;
+
+        ff_er_init(er);
     }
 
     return 0;
diff --git a/libavcodec/mpeg_er.c b/libavcodec/mpeg_er.c
index 5df75d8e85..93eb9221d7 100644
--- a/libavcodec/mpeg_er.c
+++ b/libavcodec/mpeg_er.c
@@ -122,6 +122,8 @@ av_cold int ff_mpeg_er_init(MpegEncContext *s)
     er->decode_mb = mpeg_er_decode_mb;
     er->opaque    = s;
 
+    ff_er_init(er);
+
     return 0;
 fail:
     av_freep(&er->er_temp_buffer);
-- 
2.52.0


>From 18fd5b69ee87faf63deaf724f0fe6317ab28d9c2 Mon Sep 17 00:00:00 2001
From: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
Date: Fri, 27 Feb 2026 17:55:18 +0100
Subject: [PATCH 2/2] avcodec/h264dec,mpeg_er: Move allocating er buffers to
 ff_er_init()

(This also stops zero-allocating er_temp_buffer for H.264,
reverting back to the behavior from before commit
0a1dc817237b8546189960efd523257b4f62e1ab.)

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavcodec/error_resilience.c | 12 +++++++++++-
 libavcodec/error_resilience.h |  2 +-
 libavcodec/h264dec.c          |  6 +-----
 libavcodec/mpeg_er.c          | 14 +-------------
 4 files changed, 14 insertions(+), 20 deletions(-)

diff --git a/libavcodec/error_resilience.c b/libavcodec/error_resilience.c
index 76d5bfa73b..8cf5bc6a3c 100644
--- a/libavcodec/error_resilience.c
+++ b/libavcodec/error_resilience.c
@@ -39,12 +39,22 @@
 #include "threadframe.h"
 #include "threadprogress.h"
 
-av_cold void ff_er_init(ERContext *const s)
+av_cold int ff_er_init(ERContext *const s)
 {
     MECmpContext mecc;
+    unsigned mb_array_size = s->mb_height * s->mb_stride;
+
+    s->error_status_table = av_mallocz(mb_array_size);
+    if (!s->error_status_table)
+        return AVERROR(ENOMEM);
+    s->er_temp_buffer = av_malloc_array(mb_array_size, 4*sizeof(int) + 1);
+    if (!s->er_temp_buffer)
+        return AVERROR(ENOMEM);
 
     ff_me_cmp_init(&mecc, s->avctx);
     s->sad = mecc.sad[0];
+
+    return 0;
 }
 
 /**
diff --git a/libavcodec/error_resilience.h b/libavcodec/error_resilience.h
index 6c8736a445..1beae5a6b0 100644
--- a/libavcodec/error_resilience.h
+++ b/libavcodec/error_resilience.h
@@ -90,7 +90,7 @@ typedef struct ERContext {
     void *opaque;
 } ERContext;
 
-void ff_er_init(ERContext *const s);
+int ff_er_init(ERContext *const s);
 
 void ff_er_frame_start(ERContext *s);
 
diff --git a/libavcodec/h264dec.c b/libavcodec/h264dec.c
index e430bc58fc..809a938386 100644
--- a/libavcodec/h264dec.c
+++ b/libavcodec/h264dec.c
@@ -220,8 +220,6 @@ int ff_h264_alloc_tables(H264Context *h)
         }
 
     if (CONFIG_ERROR_RESILIENCE) {
-        const int er_size = h->mb_height * h->mb_stride * (4*sizeof(int) + 1);
-        int mb_array_size = h->mb_height * h->mb_stride;
         int y_size  = (2 * h->mb_width + 1) * (2 * h->mb_height + 1);
         int yc_size = y_size + 2 * big_mb_num;
 
@@ -239,8 +237,6 @@ int ff_h264_alloc_tables(H264Context *h)
 
         // error resilience code looks cleaner with this
         if (!FF_ALLOCZ_TYPED_ARRAY(er->mb_index2xy,        h->mb_num + 1) ||
-            !FF_ALLOCZ_TYPED_ARRAY(er->error_status_table, mb_array_size) ||
-            !FF_ALLOCZ_TYPED_ARRAY(er->er_temp_buffer,     er_size)       ||
             !FF_ALLOCZ_TYPED_ARRAY(h->dc_val_base,         yc_size))
             return AVERROR(ENOMEM); // ff_h264_free_tables will clean up for us
 
@@ -256,7 +252,7 @@ int ff_h264_alloc_tables(H264Context *h)
         for (int i = 0; i < yc_size; i++)
             h->dc_val_base[i] = 1024;
 
-        ff_er_init(er);
+        return ff_er_init(er);
     }
 
     return 0;
diff --git a/libavcodec/mpeg_er.c b/libavcodec/mpeg_er.c
index 93eb9221d7..5bb5ed8441 100644
--- a/libavcodec/mpeg_er.c
+++ b/libavcodec/mpeg_er.c
@@ -96,7 +96,6 @@ static void mpeg_er_decode_mb(void *opaque, int ref, int mv_dir, int mv_type,
 av_cold int ff_mpeg_er_init(MpegEncContext *s)
 {
     ERContext *er = &s->er;
-    int mb_array_size = s->mb_height * s->mb_stride;
 
     er->avctx       = s->avctx;
 
@@ -111,22 +110,11 @@ av_cold int ff_mpeg_er_init(MpegEncContext *s)
     er->dc_val[1] = er->dc_val[0] + s->b8_stride * 2 * s->buffer_pools.alloc_mb_height + s->mb_stride;
     er->dc_val[2] = er->dc_val[1] + s->mb_stride * (s->buffer_pools.alloc_mb_height + 1);
 
-    er->er_temp_buffer     = av_malloc(s->mb_height * s->mb_stride * (4*sizeof(int) + 1));
-    er->error_status_table = av_mallocz(mb_array_size);
-    if (!er->er_temp_buffer || !er->error_status_table)
-        goto fail;
-
     er->mbskip_table  = s->mbskip_table;
     er->mbintra_table = s->mbintra_table;
 
     er->decode_mb = mpeg_er_decode_mb;
     er->opaque    = s;
 
-    ff_er_init(er);
-
-    return 0;
-fail:
-    av_freep(&er->er_temp_buffer);
-    av_freep(&er->error_status_table);
-    return AVERROR(ENOMEM);
+    return ff_er_init(er);
 }
-- 
2.52.0

_______________________________________________
ffmpeg-devel mailing list -- ffmpeg-devel@ffmpeg.org
To unsubscribe send an email to ffmpeg-devel-leave@ffmpeg.org

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2026-02-27 17:14 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2026-02-27 17:13 [FFmpeg-devel] [PR] avcodec/error_resilience: Initialize error resilience context earlier (PR #22318) mkver via ffmpeg-devel

Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
		ffmpegdev@gitmailbox.com
	public-inbox-index ffmpegdev

Example config snippet for mirrors.


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git