[FFmpeg-devel,26/31] av1_metadata: Update codec parameters

Submitted by Andreas Rheinhardt on July 9, 2019, 1:10 a.m.

Details

Message ID 20190709011048.29601-1-andreas.rheinhardt@gmail.com
State New
Headers show

Commit Message

Andreas Rheinhardt July 9, 2019, 1:10 a.m.
Up until now, this BSF only changed the bitstream and the extradata,
not the rest of the AVCodecParameters. The result is that e.g. some
muxers use outdated information to write header information that
conflicts with (and potentially precedes) the new information at the
bitstream level, so that using the bitstream filter might not have the
desired effect.

This commit changes this and thereby brings av1_metadata in line with
the expected behaviour for bitstream filters.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 libavcodec/av1_metadata_bsf.c | 32 ++++++++++++++++++++++++++++----
 1 file changed, 28 insertions(+), 4 deletions(-)

Patch hide | download patch | download mbox

diff --git a/libavcodec/av1_metadata_bsf.c b/libavcodec/av1_metadata_bsf.c
index dd0c9b6148..6821a3e102 100644
--- a/libavcodec/av1_metadata_bsf.c
+++ b/libavcodec/av1_metadata_bsf.c
@@ -52,7 +52,9 @@  typedef struct AV1MetadataContext {
 
 
 static int av1_metadata_update_sequence_header(AVBSFContext *bsf,
-                                               AV1RawSequenceHeader *seq)
+                                               AV1RawSequenceHeader *seq,
+                                               int *color_range,
+                                               int *chroma_sample_position)
 {
     AV1MetadataContext *ctx = bsf->priv_data;
     AV1RawColorConfig  *clc = &seq->color_config;
@@ -79,6 +81,8 @@  static int av1_metadata_update_sequence_header(AVBSFContext *bsf,
                    "on RGB streams encoded in BT.709 sRGB.\n");
         } else {
             clc->color_range = ctx->color_range;
+            if (color_range)
+                *color_range = ctx->color_range + 1;
         }
     }
 
@@ -88,6 +92,8 @@  static int av1_metadata_update_sequence_header(AVBSFContext *bsf,
                    "can only be set for 4:2:0 streams.\n");
         } else {
             clc->chroma_sample_position = ctx->chroma_sample_position;
+            if (chroma_sample_position)
+                *chroma_sample_position = ctx->chroma_sample_position;
         }
     }
 
@@ -137,7 +143,8 @@  static int av1_metadata_filter(AVBSFContext *bsf, AVPacket *pkt)
     for (i = 0; i < frag->nb_units; i++) {
         if (frag->units[i].type == AV1_OBU_SEQUENCE_HEADER) {
             obu = frag->units[i].content;
-            err = av1_metadata_update_sequence_header(bsf, &obu->obu.sequence_header);
+            err = av1_metadata_update_sequence_header(bsf, &obu->obu.sequence_header,
+                                                      NULL, NULL);
             if (err < 0)
                 goto fail;
         }
@@ -188,7 +195,7 @@  static int av1_metadata_init(AVBSFContext *bsf)
     AV1MetadataContext *ctx = bsf->priv_data;
     CodedBitstreamFragment *frag = &ctx->access_unit;
     AV1RawOBU *obu;
-    int err, i;
+    int err, i, color_range = -1, chroma_sample_position = -1;
 
     err = ff_cbs_init(&ctx->cbc, AV_CODEC_ID_AV1, bsf);
     if (err < 0)
@@ -204,7 +211,8 @@  static int av1_metadata_init(AVBSFContext *bsf)
         for (i = 0; i < frag->nb_units; i++) {
             if (frag->units[i].type == AV1_OBU_SEQUENCE_HEADER) {
                 obu = frag->units[i].content;
-                err = av1_metadata_update_sequence_header(bsf, &obu->obu.sequence_header);
+                err = av1_metadata_update_sequence_header(bsf, &obu->obu.sequence_header,
+                                                          &color_range, &chroma_sample_position);
                 if (err < 0)
                     goto fail;
             }
@@ -217,6 +225,22 @@  static int av1_metadata_init(AVBSFContext *bsf)
         }
     }
 
+    if (chroma_sample_position >= 0) {
+        static const uint8_t conversion_table[4] = {
+            AVCHROMA_LOC_UNSPECIFIED,
+            AVCHROMA_LOC_LEFT,
+            AVCHROMA_LOC_TOPLEFT,
+            AVCHROMA_LOC_UNSPECIFIED
+        };
+        chroma_sample_position = conversion_table[chroma_sample_position];
+    }
+
+    ff_cbs_update_video_parameters(ctx->cbc, bsf->par_out, -1, -1, -1, -1,
+                                   -1, color_range, ctx->color_primaries,
+                                   ctx->transfer_characteristics,
+                                   ctx->matrix_coefficients,
+                                   chroma_sample_position, -1);
+
     err = 0;
 fail:
     ff_cbs_fragment_reset(ctx->cbc, frag);