diff mbox series

[FFmpeg-devel,v2,05/69] avcodec/mpegvideo: Enable private contexts

Message ID AM7PR03MB6660073BC0E889BBEC79A3F58F269@AM7PR03MB6660.eurprd03.prod.outlook.com
State Accepted
Commit 725e2300af90eb005ecdde915ba59ee3a0dc5a7a
Headers show
Series [FFmpeg-devel,v2,01/69] avcodec/avcodec: Avoid MpegEncContext in AVHWAccel.decode_mb | expand


Context Check Description
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished
andriy/make_ppc success Make finished
andriy/make_fate_ppc success Make fate finished

Commit Message

Andreas Rheinhardt Feb. 1, 2022, 1:06 p.m. UTC
MpegEncContext is used by many different codecs and
every one of these uses just a subset of its fields.
If one tries to separate this and e.g. add a real MpegContext
and extension structures (say MpegDecContext and MpegEncContext),
one runs into two difficulties:

a) Some code is shared between decoder and encoder of
the same format and they therefore use the same contexts,
either MpegEncContext itself or identical extensions thereof.
The latter is the case for H.261 as well as WMV2.
b) In case of slice threading, the generic code can only allocate
and initialize the structure it knows about; right now this is
an MpegEncContext. If the codec has an even more extensive structure,
it is only available for the main thread's MpegEncContext.
Fixing this would involve making ff_mpv_common_init() aware
of the size the size of slice context to allocate and would be
part of separating the main thread's context from the slice contexts
in general.

This commit only intends to tackle the first issue by adding
a pointer to MpegEncContext that codecs can set to a common
context so that the aforementioned codecs can use this context
(together with the MpegEncContext) in their common code.
This will allow to move fields only used by the main thread
to more specialized contexts.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
 libavcodec/mpegvideo.h     | 3 +++
 libavcodec/mpegvideo_dec.c | 3 +++
 2 files changed, 6 insertions(+)
diff mbox series


diff --git a/libavcodec/mpegvideo.h b/libavcodec/mpegvideo.h
index 84d79d2e59..5e406b9a9c 100644
--- a/libavcodec/mpegvideo.h
+++ b/libavcodec/mpegvideo.h
@@ -84,6 +84,9 @@  typedef struct MpegEncContext {
      *          offsets used in ASM. */
     struct AVCodecContext *avctx;
+    /* The following pointer is intended for codecs sharing code
+     * between decoder and encoder and in need of a common context to do so. */
+    void *private_ctx;
     /* the following parameters must be initialized before encoding */
     int width, height;///< picture size. must be a multiple of 16
     int gop_size;
diff --git a/libavcodec/mpegvideo_dec.c b/libavcodec/mpegvideo_dec.c
index 1e238bd1e1..cc23e070ea 100644
--- a/libavcodec/mpegvideo_dec.c
+++ b/libavcodec/mpegvideo_dec.c
@@ -62,10 +62,12 @@  int ff_mpeg_update_thread_context(AVCodecContext *dst,
     // FIXME can parameters change on I-frames?
     // in that case dst may need a reinit
     if (!s->context_initialized) {
+        void *private_ctx = s->private_ctx;
         int err;
         memcpy(s, s1, sizeof(*s));
         s->avctx                 = dst;
+        s->private_ctx           = private_ctx;
         s->bitstream_buffer      = NULL;
         s->bitstream_buffer_size = s->allocated_bitstream_buffer_size = 0;
@@ -76,6 +78,7 @@  int ff_mpeg_update_thread_context(AVCodecContext *dst,
             if ((err = ff_mpv_common_init(s)) < 0) {
                 memset(s, 0, sizeof(*s));
                 s->avctx = dst;
+                s->private_ctx = private_ctx;
                 return err;