[FFmpeg-devel] [PATCH 4/5] mpeg2_metadata: Add support for A/53 closed captions

Aman Gupta ffmpeg at tmm1.net
Tue Mar 27 20:54:58 EEST 2018


On Sun, Mar 25, 2018 at 10:41 AM, Mark Thompson <sw at jkqxz.net> wrote:

> Allows extraction (to side data) and removal of closed captions in
> user data blocks.
> ---
>  doc/bitstream_filters.texi      | 12 ++++++
>  libavcodec/Makefile             |  2 +-
>  libavcodec/mpeg2_metadata_bsf.c | 81 ++++++++++++++++++++++++++++++
> ++++++++++-
>  3 files changed, 93 insertions(+), 2 deletions(-)
>
> diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi
> index 41424cf42f..f115f7b0c5 100644
> --- a/doc/bitstream_filters.texi
> +++ b/doc/bitstream_filters.texi
> @@ -465,6 +465,18 @@ table 6-6).
>  Set the colour description in the stream (see H.262 section 6.3.6
>  and tables 6-7, 6-8 and 6-9).
>
> + at item a53_cc
> +Modify A/53 closed captions in user data blocks.
> +
> + at table @samp
> + at item remove
> +Remove all closed caption data from the stream.
> +
> + at item extract
> +Extract closed captions from the stream so that they are available as
> +as packet side data.
> + at end table
> +
>  @end table
>
>  @section mpeg4_unpack_bframes
> diff --git a/libavcodec/Makefile b/libavcodec/Makefile
> index cfde104055..e5430ab10b 100644
> --- a/libavcodec/Makefile
> +++ b/libavcodec/Makefile
> @@ -1057,7 +1057,7 @@ OBJS-$(CONFIG_MPEG4_UNPACK_BFRAMES_BSF)   +=
> mpeg4_unpack_bframes_bsf.o
>  OBJS-$(CONFIG_MOV2TEXTSUB_BSF)            += movsub_bsf.o
>  OBJS-$(CONFIG_MP3_HEADER_DECOMPRESS_BSF)  += mp3_header_decompress_bsf.o
> \
>                                               mpegaudiodata.o
> -OBJS-$(CONFIG_MPEG2_METADATA_BSF)         += mpeg2_metadata_bsf.o
> +OBJS-$(CONFIG_MPEG2_METADATA_BSF)         += mpeg2_metadata_bsf.o
> cbs_misc.o
>  OBJS-$(CONFIG_NOISE_BSF)                  += noise_bsf.o
>  OBJS-$(CONFIG_NULL_BSF)                   += null_bsf.o
>  OBJS-$(CONFIG_REMOVE_EXTRADATA_BSF)       += remove_extradata_bsf.o
> diff --git a/libavcodec/mpeg2_metadata_bsf.c b/libavcodec/mpeg2_metadata_
> bsf.c
> index e787cb3782..49335d7fcb 100644
> --- a/libavcodec/mpeg2_metadata_bsf.c
> +++ b/libavcodec/mpeg2_metadata_bsf.c
> @@ -22,9 +22,17 @@
>
>  #include "bsf.h"
>  #include "cbs.h"
> +#include "cbs_misc.h"
>  #include "cbs_mpeg2.h"
>  #include "mpeg12.h"
>
> +enum {
> +    PASS,
> +    INSERT,
> +    REMOVE,
> +    EXTRACT,
> +};
> +
>  typedef struct MPEG2MetadataContext {
>      const AVClass *class;
>
> @@ -42,6 +50,8 @@ typedef struct MPEG2MetadataContext {
>      int transfer_characteristics;
>      int matrix_coefficients;
>
> +    int a53_cc;
> +
>      int mpeg1_warned;
>  } MPEG2MetadataContext;
>
> @@ -184,7 +194,9 @@ static int mpeg2_metadata_filter(AVBSFContext *bsf,
> AVPacket *out)
>      MPEG2MetadataContext *ctx = bsf->priv_data;
>      AVPacket *in = NULL;
>      CodedBitstreamFragment *frag = &ctx->fragment;
> -    int err;
> +    int err, i;
> +    uint8_t *a53_side_data = NULL;
> +    size_t a53_side_data_size = 0;
>
>      err = ff_bsf_get_packet(bsf, &in);
>      if (err < 0)
> @@ -202,6 +214,51 @@ static int mpeg2_metadata_filter(AVBSFContext *bsf,
> AVPacket *out)
>          goto fail;
>      }
>
> +    if (ctx->a53_cc == REMOVE || ctx->a53_cc == EXTRACT) {
> +        for (i = 0; i < frag->nb_units; i++) {
> +            MPEG2RawUserData *ud;
> +            A53UserData a53_ud;
> +
> +            if (frag->units[i].type != MPEG2_START_USER_DATA)
> +                continue;
> +            ud = frag->units[i].content;
> +
> +            err = ff_cbs_read_a53_user_data(ctx->cbc, &a53_ud,
> ud->user_data,
> +                                            ud->user_data_length);
> +            if (err < 0) {
> +                // Invalid or something completely different.
> +                continue;
> +            }
> +            if (a53_ud.user_identifier != A53_USER_IDENTIFIER_ATSC ||
> +                a53_ud.atsc.user_data_type_code !=
> +                    A53_USER_DATA_TYPE_CODE_CC_DATA) {
> +                // Valid but something else (e.g. AFD).
> +                continue;
> +            }
> +
> +            if (ctx->a53_cc == REMOVE) {
> +                err = ff_cbs_delete_unit(ctx->cbc, frag, i);
> +                if (err < 0) {
> +                    av_log(bsf, AV_LOG_ERROR, "Failed to delete "
> +                           "A/53 CC user data.\n");
> +                    goto fail;
> +                }
> +                --i;
> +                break;
> +            } else if(ctx->a53_cc == EXTRACT) {
> +                err = ff_cbs_write_a53_cc_side_data(ctx->cbc,
> +                                                    &a53_side_data,
> +                                                    &a53_side_data_size,
> +                                                    &a53_ud);
> +                if (err < 0) {
> +                    av_log(bsf, AV_LOG_ERROR, "Failed to write "
> +                           "A/53 user data for packet side data.\n");
> +                    goto fail;
> +                }
> +            }
> +        }
> +    }
> +
>      err = ff_cbs_write_packet(ctx->cbc, out, frag);
>      if (err < 0) {
>          av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
> @@ -212,9 +269,21 @@ static int mpeg2_metadata_filter(AVBSFContext *bsf,
> AVPacket *out)
>      if (err < 0)
>          goto fail;
>
> +    if (a53_side_data) {
> +        err = av_packet_add_side_data(out, AV_PKT_DATA_A53_CC,
> +                                      a53_side_data, a53_side_data_size);
> +        if (err) {
> +            av_log(bsf, AV_LOG_ERROR, "Failed to attach extracted A/53 "
> +                   "side data to packet.\n");
> +            goto fail;
> +        }
> +        a53_side_data = NULL;
> +    }
> +
>      err = 0;
>  fail:
>      ff_cbs_fragment_uninit(ctx->cbc, frag);
> +    av_freep(&a53_side_data);
>
>      if (err < 0)
>          av_packet_unref(out);
> @@ -289,6 +358,16 @@ static const AVOption mpeg2_metadata_options[] = {
>          OFFSET(matrix_coefficients), AV_OPT_TYPE_INT,
>          { .i64 = -1 }, -1, 255, FLAGS },
>
> +    { "a53_cc", "A/53 Closed Captions in user data",
> +        OFFSET(a53_cc), AV_OPT_TYPE_INT,
> +        { .i64 = PASS }, PASS, EXTRACT, FLAGS, "a53_cc" },
> +    { "pass",    NULL, 0, AV_OPT_TYPE_CONST,
> +        { .i64 = PASS    }, .flags = FLAGS, .unit = "a53_cc" },
> +    { "remove",  NULL, 0, AV_OPT_TYPE_CONST,
> +        { .i64 = REMOVE  }, .flags = FLAGS, .unit = "a53_cc" },
> +    { "extract", NULL, 0, AV_OPT_TYPE_CONST,
> +        { .i64 = EXTRACT }, .flags = FLAGS, .unit = "a53_cc" },
> +
>      { NULL }
>  };
>

LGTM, thanks for refactoring!

Aman


>
> --
> 2.16.1
>
> _______________________________________________
> ffmpeg-devel mailing list
> ffmpeg-devel at ffmpeg.org
> http://ffmpeg.org/mailman/listinfo/ffmpeg-devel
>


More information about the ffmpeg-devel mailing list