History

avcodec/mpeg12dec: extract only one type of CC substream

In MPEG-2 user data, there can be different types of Closed Captions formats embedded (A53, SCTE-20, or DVD). The current behavior of the CC extraction code in the MPEG-2 decoder is to not be aware of multiple formats if multiple exist, therefore allowing one format to overwrite the other during the extraction process since the CC extraction shares one output buffer for the normalized bytes. This causes sources that have two CC formats to produce flawed output. There exist real-world samples which contain both A53 and SCTE-20 captions in the same MPEG-2 stream, and that manifest this problem. Example of symptom: THANK YOU (expected) --> THTHANANK K YOYOUU (actual) The solution is to pick only the first CC substream observed with valid bytes, and ignore the other types. Additionally, provide an option for users to manually "force" a type in the event that this matters for a particular source.

Marth64 committed 2 years ago

Commit 0b342a21 parent 53dd3141 file · +61 −3Browse files

Showing the changes to libavcodec (1 of 1 file). Show all

Unified · Split

libavcodec/mpeg12dec.c+61−3
@@ -62,6 +62,13 @@ · 61 unmodified lines
6262
6363 #define A53_MAX_CC_COUNT 2000
6464
65+enum Mpeg2ClosedCaptionsFormat {
66+ CC_FORMAT_AUTO,
67+ CC_FORMAT_A53_PART4,
68+ CC_FORMAT_SCTE20,
69+ CC_FORMAT_DVD
70+};
71+
6572 typedef struct Mpeg1Context {
6673 MpegEncContext mpeg_enc_ctx;
6774 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
@@ -70,6 +77,7 @@ typedef struct Mpeg1Context { · 2 unmodified lines
7077 AVStereo3D stereo3d;
7178 int has_stereo3d;
7279 AVBufferRef *a53_buf_ref;
80+ enum Mpeg2ClosedCaptionsFormat cc_format;
7381 uint8_t afd;
7482 int has_afd;
7583 int slice_count;
@@ -1903,12 +1911,27 @@ static int vcr2_init_sequence(AVCodecContext *avctx) · 1827 unmodified lines
19031911 return 0;
19041912 }
19051913
1914+static void mpeg_set_cc_format(AVCodecContext *avctx, enum Mpeg2ClosedCaptionsFormat format,
1915+ const char *label)
1916+{
1917+ Mpeg1Context *s1 = avctx->priv_data;
1918+
1919+ av_assert2(format != CC_FORMAT_AUTO);
1920+
1921+ if (!s1->cc_format) {
1922+ s1->cc_format = format;
1923+
1924+ av_log(avctx, AV_LOG_DEBUG, "CC: first seen substream is %s format\n", label);
1925+ }
1926+}
1927+
19061928 static int mpeg_decode_a53_cc(AVCodecContext *avctx,
19071929 const uint8_t *p, int buf_size)
19081930 {
19091931 Mpeg1Context *s1 = avctx->priv_data;
19101932
1911− if (buf_size >= 6 &&
1933+ if ((!s1->cc_format || s1->cc_format == CC_FORMAT_A53_PART4) &&
1934+ buf_size >= 6 &&
19121935 p[0] == 'G' && p[1] == 'A' && p[2] == '9' && p[3] == '4' &&
19131936 p[4] == 3 && (p[5] & 0x40)) {
19141937 /* extract A53 Part 4 CC data */
@@ -1927,9 +1950,11 @@ static int mpeg_decode_a53_cc(AVCodecContext *avctx, · 12 unmodified lines
19271950 memcpy(s1->a53_buf_ref->data + old_size, p + 7, cc_count * UINT64_C(3));
19281951
19291952 avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;
1953+ mpeg_set_cc_format(avctx, CC_FORMAT_A53_PART4, "A/53 Part 4");
19301954 }
19311955 return 1;
1932− } else if (buf_size >= 2 &&
1956+ } else if ((!s1->cc_format || s1->cc_format == CC_FORMAT_SCTE20) &&
1957+ buf_size >= 2 &&
19331958 p[0] == 0x03 && (p[1]&0x7f) == 0x01) {
19341959 /* extract SCTE-20 CC data */
19351960 GetBitContext gb;
@@ -1973,10 +1998,13 @@ static int mpeg_decode_a53_cc(AVCodecContext *avctx, · 37 unmodified lines
19731998 cap += 3;
19741999 }
19752000 }
2001+
19762002 avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;
2003+ mpeg_set_cc_format(avctx, CC_FORMAT_SCTE20, "SCTE-20");
19772004 }
19782005 return 1;
1979− } else if (buf_size >= 11 &&
2006+ } else if ((!s1->cc_format || s1->cc_format == CC_FORMAT_DVD) &&
2007+ buf_size >= 11 &&
19802008 p[0] == 'C' && p[1] == 'C' && p[2] == 0x01 && p[3] == 0xf8) {
19812009 /* extract DVD CC data
19822010 *
@@ -2033,7 +2061,9 @@ static int mpeg_decode_a53_cc(AVCodecContext *avctx, · 50 unmodified lines
20332061 p += 6;
20342062 }
20352063 }
2064+
20362065 avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;
2066+ mpeg_set_cc_format(avctx, CC_FORMAT_DVD, "DVD");
20372067 }
20382068 return 1;
20392069 }
@@ -2598,11 +2628,39 @@ const FFCodec ff_mpeg1video_decoder = { · 558 unmodified lines
25982628 },
25992629 };
26002630
2631+#define M2V_OFFSET(x) offsetof(Mpeg1Context, x)
2632+#define M2V_PARAM AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2633+
2634+static const AVOption mpeg2video_options[] = {
2635+ { "cc_format", "extract a specific Closed Captions format",
2636+ M2V_OFFSET(cc_format), AV_OPT_TYPE_INT, { .i64 = CC_FORMAT_AUTO },
2637+ CC_FORMAT_AUTO, CC_FORMAT_DVD, M2V_PARAM, .unit = "cc_format" },
2638+
2639+ { "auto", "pick first seen CC substream", 0, AV_OPT_TYPE_CONST,
2640+ { .i64 = CC_FORMAT_AUTO }, .flags = M2V_PARAM, .unit = "cc_format" },
2641+ { "a53", "pick A/53 Part 4 CC substream", 0, AV_OPT_TYPE_CONST,
2642+ { .i64 = CC_FORMAT_A53_PART4 }, .flags = M2V_PARAM, .unit = "cc_format" },
2643+ { "scte20", "pick SCTE-20 CC substream", 0, AV_OPT_TYPE_CONST,
2644+ { .i64 = CC_FORMAT_SCTE20 }, .flags = M2V_PARAM, .unit = "cc_format" },
2645+ { "dvd", "pick DVD CC substream", 0, AV_OPT_TYPE_CONST,
2646+ { .i64 = CC_FORMAT_DVD }, .flags = M2V_PARAM, .unit = "cc_format" },
2647+ { NULL }
2648+};
2649+
2650+static const AVClass mpeg2video_class = {
2651+ .class_name = "MPEG-2 video",
2652+ .item_name = av_default_item_name,
2653+ .option = mpeg2video_options,
2654+ .version = LIBAVUTIL_VERSION_INT,
2655+ .category = AV_CLASS_CATEGORY_DECODER,
2656+};
2657+
26012658 const FFCodec ff_mpeg2video_decoder = {
26022659 .p.name = "mpeg2video",
26032660 CODEC_LONG_NAME("MPEG-2 video"),
26042661 .p.type = AVMEDIA_TYPE_VIDEO,
26052662 .p.id = AV_CODEC_ID_MPEG2VIDEO,
2663+ .p.priv_class = &mpeg2video_class,
26062664 .priv_data_size = sizeof(Mpeg1Context),
26072665 .init = mpeg_decode_init,
26082666 .close = mpeg_decode_end,

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t