[FFmpeg-devel,29/31] vp9_metadata: Update AVCodecParameters

Submitted by Andreas Rheinhardt on June 19, 2019, 11:45 p.m.

Details

Message ID 20190619234521.15619-21-andreas.rheinhardt@gmail.com
State New
Headers show

Commit Message

Andreas Rheinhardt June 19, 2019, 11:45 p.m.
This commit adds an option to not only update the bitstream parameters
when using the vp9_metadata bitstream filter, but also the relevant
AVCodecParameters. The new option is on by default.

This commit also adds documentation for this value.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
---
 doc/bitstream_filters.texi    |  7 +++++++
 libavcodec/vp9_metadata_bsf.c | 24 ++++++++++++++++++++++++
 2 files changed, 31 insertions(+)

Comments

James Almer July 7, 2019, 9:31 p.m.
On 6/19/2019 8:45 PM, Andreas Rheinhardt wrote:
> This commit adds an option to not only update the bitstream parameters
> when using the vp9_metadata bitstream filter, but also the relevant
> AVCodecParameters. The new option is on by default.
> 
> This commit also adds documentation for this value.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@gmail.com>
> ---
>  doc/bitstream_filters.texi    |  7 +++++++
>  libavcodec/vp9_metadata_bsf.c | 24 ++++++++++++++++++++++++
>  2 files changed, 31 insertions(+)
> 
> diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi
> index c9dff7ae03..80be525e0f 100644
> --- a/doc/bitstream_filters.texi
> +++ b/doc/bitstream_filters.texi
> @@ -680,6 +680,13 @@ imposed by the color space will take precedence over this value.
>  @item tv
>  @item pc
>  @end table
> +
> +@item full_update
> +If this is set, an effort is made to update the AVCodecParameters in addition
> +to the bitstream. If unset, muxers might add header information based upon
> +the old AVCodecParameters that contradicts and potentially precedes
> +the changes made at the bitstream level. On by default.
> +
>  @end table
>  
>  @section vp9_superframe
> diff --git a/libavcodec/vp9_metadata_bsf.c b/libavcodec/vp9_metadata_bsf.c
> index 52e962b1c0..338d6d2af3 100644
> --- a/libavcodec/vp9_metadata_bsf.c
> +++ b/libavcodec/vp9_metadata_bsf.c
> @@ -34,6 +34,8 @@ typedef struct VP9MetadataContext {
>      int color_range;
>  
>      int color_warnings;
> +
> +    int full_update;
>  } VP9MetadataContext;
>  
>  
> @@ -109,6 +111,25 @@ static int vp9_metadata_init(AVBSFContext *bsf)
>  {
>      VP9MetadataContext *ctx = bsf->priv_data;
>  
> +    if (ctx->full_update) {
> +        AVCodecParameters *par = bsf->par_out;
> +        static const uint8_t conversion_table[8] = {
> +            AVCOL_SPC_UNSPECIFIED, AVCOL_SPC_BT470BG,
> +            AVCOL_SPC_BT709,       AVCOL_SPC_SMPTE170M,
> +            AVCOL_SPC_SMPTE240M,   AVCOL_SPC_BT2020_NCL,
> +            AVCOL_SPC_RESERVED,    AVCOL_SPC_RGB
> +        };
> +
> +        if (ctx->color_space >= 0 && (par->profile & 1 ||
> +                                      ctx->color_space != VP9_CS_RGB))
> +            par->color_space = conversion_table[ctx->color_space];
> +
> +        if (par->color_space == AVCOL_SPC_RGB)
> +            par->color_range = AVCOL_RANGE_JPEG;
> +        else if (ctx->color_range >= 0)
> +            par->color_range = ctx->color_range + 1;
> +    }
> +
>      return ff_cbs_init(&ctx->cbc, AV_CODEC_ID_VP9, bsf);
>  }
>  
> @@ -149,6 +170,9 @@ static const AVOption vp9_metadata_options[] = {
>      { "pc", "PC (full) range",    0, AV_OPT_TYPE_CONST,
>          { .i64 = 1 }, .flags = FLAGS, .unit = "cr" },
>  
> +    { "full_update", "Update not only bitstream, but also AVCodecParameters",

I insist, this should not be an option. If you change the bitstream, you
should also change the stream's metadata. If you don't, the result will
be the muxer potentially writing values that differ from those in the
bitstream.

Look at other bsfs. None of those that change AVCodecParameters do it
optionally, because it's meant to accurately represent the contents of
the stream in question.

> +        OFFSET(full_update), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS},
> +
>      { NULL }
>  };
>  
>

Patch hide | download patch | download mbox

diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi
index c9dff7ae03..80be525e0f 100644
--- a/doc/bitstream_filters.texi
+++ b/doc/bitstream_filters.texi
@@ -680,6 +680,13 @@  imposed by the color space will take precedence over this value.
 @item tv
 @item pc
 @end table
+
+@item full_update
+If this is set, an effort is made to update the AVCodecParameters in addition
+to the bitstream. If unset, muxers might add header information based upon
+the old AVCodecParameters that contradicts and potentially precedes
+the changes made at the bitstream level. On by default.
+
 @end table
 
 @section vp9_superframe
diff --git a/libavcodec/vp9_metadata_bsf.c b/libavcodec/vp9_metadata_bsf.c
index 52e962b1c0..338d6d2af3 100644
--- a/libavcodec/vp9_metadata_bsf.c
+++ b/libavcodec/vp9_metadata_bsf.c
@@ -34,6 +34,8 @@  typedef struct VP9MetadataContext {
     int color_range;
 
     int color_warnings;
+
+    int full_update;
 } VP9MetadataContext;
 
 
@@ -109,6 +111,25 @@  static int vp9_metadata_init(AVBSFContext *bsf)
 {
     VP9MetadataContext *ctx = bsf->priv_data;
 
+    if (ctx->full_update) {
+        AVCodecParameters *par = bsf->par_out;
+        static const uint8_t conversion_table[8] = {
+            AVCOL_SPC_UNSPECIFIED, AVCOL_SPC_BT470BG,
+            AVCOL_SPC_BT709,       AVCOL_SPC_SMPTE170M,
+            AVCOL_SPC_SMPTE240M,   AVCOL_SPC_BT2020_NCL,
+            AVCOL_SPC_RESERVED,    AVCOL_SPC_RGB
+        };
+
+        if (ctx->color_space >= 0 && (par->profile & 1 ||
+                                      ctx->color_space != VP9_CS_RGB))
+            par->color_space = conversion_table[ctx->color_space];
+
+        if (par->color_space == AVCOL_SPC_RGB)
+            par->color_range = AVCOL_RANGE_JPEG;
+        else if (ctx->color_range >= 0)
+            par->color_range = ctx->color_range + 1;
+    }
+
     return ff_cbs_init(&ctx->cbc, AV_CODEC_ID_VP9, bsf);
 }
 
@@ -149,6 +170,9 @@  static const AVOption vp9_metadata_options[] = {
     { "pc", "PC (full) range",    0, AV_OPT_TYPE_CONST,
         { .i64 = 1 }, .flags = FLAGS, .unit = "cr" },
 
+    { "full_update", "Update not only bitstream, but also AVCodecParameters",
+        OFFSET(full_update), AV_OPT_TYPE_INT, { .i64 = 1 }, 0, 1, FLAGS},
+
     { NULL }
 };