[FFmpeg-devel,1/8] cbs: Add function to update video codec parameters

Submitted by Andreas Rheinhardt on Dec. 1, 2018, 7:28 p.m.

Details

Message ID 20181201192810.852-2-andreas.rheinhardt@googlemail.com
State New
Headers show

Commit Message

Andreas Rheinhardt Dec. 1, 2018, 7:28 p.m.
If any of the *_metadata filter based upon cbs currently updates a video
codec parameter like the color information, the
AVCodecParameters are not updated accordingly, so that e.g. muxers write
header values based upon outdated information that may precede the new
values on the bitstream level.
This commit adds a function to also update the video codec
parameters so that the above situation can be fixed in a unified manner.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@googlemail.com>
---
 libavcodec/cbs.c | 35 +++++++++++++++++++++++++++++++++++
 libavcodec/cbs.h | 15 +++++++++++++++
 2 files changed, 50 insertions(+)

Patch hide | download patch | download mbox

diff --git a/libavcodec/cbs.c b/libavcodec/cbs.c
index ecbf57c293..d8f4b67a00 100644
--- a/libavcodec/cbs.c
+++ b/libavcodec/cbs.c
@@ -340,6 +340,41 @@  int ff_cbs_write_extradata(CodedBitstreamContext *ctx,
     return 0;
 }
 
+void ff_cbs_update_video_parameters(CodedBitstreamContext *ctx,
+                                    AVCodecParameters *par, int profile,
+                                    int level, int width, int height,
+                                    int field_order, int color_range,
+                                    int color_primaries, int color_trc,
+                                    int color_space, int chroma_location,
+                                    int video_delay)
+{
+    #define SET_IF_NONNEGATIVE(elem) \
+    if (elem >= 0) \
+        par->elem = elem;
+    SET_IF_NONNEGATIVE(profile)
+    SET_IF_NONNEGATIVE(level)
+    SET_IF_NONNEGATIVE(width)
+    SET_IF_NONNEGATIVE(height)
+    SET_IF_NONNEGATIVE(field_order)
+    SET_IF_NONNEGATIVE(video_delay)
+    #undef SET_IF_NONNEGATIVE
+
+    #define SET_IF_VALID(elem, upper_bound) \
+    if (0 <= elem && elem < upper_bound) \
+        par->elem = elem;
+    SET_IF_VALID(color_range,     AVCOL_RANGE_NB)
+    SET_IF_VALID(color_trc,       AVCOL_TRC_NB)
+    SET_IF_VALID(color_space,     AVCOL_SPC_NB)
+    SET_IF_VALID(chroma_location, AVCHROMA_LOC_NB)
+    #undef SET_IF_VALID
+
+    if (0 <= color_primaries && color_primaries <= AVCOL_PRI_SMPTE432
+                             || color_primaries == AVCOL_PRI_JEDEC_P22)
+        par->color_primaries = color_primaries;
+
+    return;
+}
+
 int ff_cbs_write_packet(CodedBitstreamContext *ctx,
                         AVPacket *pkt,
                         CodedBitstreamFragment *frag)
diff --git a/libavcodec/cbs.h b/libavcodec/cbs.h
index 53ac360bb1..068ed15e9c 100644
--- a/libavcodec/cbs.h
+++ b/libavcodec/cbs.h
@@ -285,6 +285,21 @@  int ff_cbs_write_extradata(CodedBitstreamContext *ctx,
                            AVCodecParameters *par,
                            CodedBitstreamFragment *frag);
 
+/**
+ * Update the parameters of an AVCodecParameters structure
+ *
+ * If a parameter is negative, the corresponding member is not
+ * modified. For the color/chroma parameters, only values that
+ * are part of the relevant enumeration are written.
+ */
+void ff_cbs_update_video_parameters(CodedBitstreamContext *ctx,
+                                    AVCodecParameters *par, int profile,
+                                    int level, int width, int height,
+                                    int field_order, int color_range,
+                                    int color_primaries, int color_trc,
+                                    int color_space, int chroma_location,
+                                    int video_delay);
+
 /**
  * Write the bitstream of a fragment to a packet.
  */