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

Aman Gupta ffmpeg at tmm1.net
Wed Sep 11 21:56:10 EEST 2019


From: Mark Thompson <sw at jkqxz.net>

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 | 76 ++++++++++++++++++++++++++++++++-
 3 files changed, 88 insertions(+), 2 deletions(-)

diff --git a/doc/bitstream_filters.texi b/doc/bitstream_filters.texi
index 8e957ae4a8..05f88e466d 100644
--- a/doc/bitstream_filters.texi
+++ b/doc/bitstream_filters.texi
@@ -548,6 +548,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 b10064776c..e691181996 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -1092,7 +1092,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_PRORES_METADATA_BSF)        += prores_metadata_bsf.o
diff --git a/libavcodec/mpeg2_metadata_bsf.c b/libavcodec/mpeg2_metadata_bsf.c
index 3f371a028d..e82758d2eb 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;
 
@@ -173,7 +183,9 @@ static int mpeg2_metadata_filter(AVBSFContext *bsf, AVPacket *pkt)
 {
     MPEG2MetadataContext *ctx = bsf->priv_data;
     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_ref(bsf, pkt);
     if (err < 0)
@@ -191,6 +203,57 @@ static int mpeg2_metadata_filter(AVBSFContext *bsf, AVPacket *pkt)
         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) {
+                ff_cbs_delete_unit(ctx->cbc, frag, i);
+                --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;
+                }
+
+                if (a53_side_data) {
+                    err = av_packet_add_side_data(pkt, 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 = ff_cbs_write_packet(ctx->cbc, pkt, frag);
     if (err < 0) {
         av_log(bsf, AV_LOG_ERROR, "Failed to write packet.\n");
@@ -200,6 +263,7 @@ static int mpeg2_metadata_filter(AVBSFContext *bsf, AVPacket *pkt)
     err = 0;
 fail:
     ff_cbs_fragment_reset(ctx->cbc, frag);
+    av_freep(&a53_side_data);
 
     if (err < 0)
         av_packet_unref(pkt);
@@ -287,6 +351,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 }
 };
 
-- 
2.20.1



More information about the ffmpeg-devel mailing list