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
62 62
63#define A53_MAX_CC_COUNT 200063#define A53_MAX_CC_COUNT 2000
64 64
65enum Mpeg2ClosedCaptionsFormat {
66 CC_FORMAT_AUTO,
67 CC_FORMAT_A53_PART4,
68 CC_FORMAT_SCTE20,
69 CC_FORMAT_DVD
70};
71
65typedef struct Mpeg1Context {72typedef struct Mpeg1Context {
66 MpegEncContext mpeg_enc_ctx;73 MpegEncContext mpeg_enc_ctx;
67 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */74 int mpeg_enc_ctx_allocated; /* true if decoding context allocated */
@@ -70,6 +77,7 @@ typedef struct Mpeg1Context { · 2 unmodified lines
70 AVStereo3D stereo3d;77 AVStereo3D stereo3d;
71 int has_stereo3d;78 int has_stereo3d;
72 AVBufferRef *a53_buf_ref;79 AVBufferRef *a53_buf_ref;
80 enum Mpeg2ClosedCaptionsFormat cc_format;
73 uint8_t afd;81 uint8_t afd;
74 int has_afd;82 int has_afd;
75 int slice_count;83 int slice_count;
@@ -1903,12 +1911,27 @@ static int vcr2_init_sequence(AVCodecContext *avctx) · 1827 unmodified lines
1903 return 0;1911 return 0;
1904}1912}
1905 1913
1914static 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
1906static int mpeg_decode_a53_cc(AVCodecContext *avctx,1928static int mpeg_decode_a53_cc(AVCodecContext *avctx,
1907 const uint8_t *p, int buf_size)1929 const uint8_t *p, int buf_size)
1908{1930{
1909 Mpeg1Context *s1 = avctx->priv_data;1931 Mpeg1Context *s1 = avctx->priv_data;
1910 1932
1911 if (buf_size >= 6 &&1933 if ((!s1->cc_format || s1->cc_format == CC_FORMAT_A53_PART4) &&
1934 buf_size >= 6 &&
1912 p[0] == 'G' && p[1] == 'A' && p[2] == '9' && p[3] == '4' &&1935 p[0] == 'G' && p[1] == 'A' && p[2] == '9' && p[3] == '4' &&
1913 p[4] == 3 && (p[5] & 0x40)) {1936 p[4] == 3 && (p[5] & 0x40)) {
1914 /* extract A53 Part 4 CC data */1937 /* extract A53 Part 4 CC data */
@@ -1927,9 +1950,11 @@ static int mpeg_decode_a53_cc(AVCodecContext *avctx, · 12 unmodified lines
1927 memcpy(s1->a53_buf_ref->data + old_size, p + 7, cc_count * UINT64_C(3));1950 memcpy(s1->a53_buf_ref->data + old_size, p + 7, cc_count * UINT64_C(3));
1928 1951
1929 avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;1952 avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;
1953 mpeg_set_cc_format(avctx, CC_FORMAT_A53_PART4, "A/53 Part 4");
1930 }1954 }
1931 return 1;1955 return 1;
1932 } else if (buf_size >= 2 &&1956 } else if ((!s1->cc_format || s1->cc_format == CC_FORMAT_SCTE20) &&
1957 buf_size >= 2 &&
1933 p[0] == 0x03 && (p[1]&0x7f) == 0x01) {1958 p[0] == 0x03 && (p[1]&0x7f) == 0x01) {
1934 /* extract SCTE-20 CC data */1959 /* extract SCTE-20 CC data */
1935 GetBitContext gb;1960 GetBitContext gb;
@@ -1973,10 +1998,13 @@ static int mpeg_decode_a53_cc(AVCodecContext *avctx, · 37 unmodified lines
1973 cap += 3;1998 cap += 3;
1974 }1999 }
1975 }2000 }
2001
1976 avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;2002 avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;
2003 mpeg_set_cc_format(avctx, CC_FORMAT_SCTE20, "SCTE-20");
1977 }2004 }
1978 return 1;2005 return 1;
1979 } else if (buf_size >= 11 &&2006 } else if ((!s1->cc_format || s1->cc_format == CC_FORMAT_DVD) &&
2007 buf_size >= 11 &&
1980 p[0] == 'C' && p[1] == 'C' && p[2] == 0x01 && p[3] == 0xf8) {2008 p[0] == 'C' && p[1] == 'C' && p[2] == 0x01 && p[3] == 0xf8) {
1981 /* extract DVD CC data2009 /* extract DVD CC data
1982 *2010 *
@@ -2033,7 +2061,9 @@ static int mpeg_decode_a53_cc(AVCodecContext *avctx, · 50 unmodified lines
2033 p += 6;2061 p += 6;
2034 }2062 }
2035 }2063 }
2064
2036 avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;2065 avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS;
2066 mpeg_set_cc_format(avctx, CC_FORMAT_DVD, "DVD");
2037 }2067 }
2038 return 1;2068 return 1;
2039 }2069 }
@@ -2598,11 +2628,39 @@ const FFCodec ff_mpeg1video_decoder = { · 558 unmodified lines
2598 },2628 },
2599};2629};
2600 2630
2631#define M2V_OFFSET(x) offsetof(Mpeg1Context, x)
2632#define M2V_PARAM AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM
2633
2634static 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
2650static 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
2601const FFCodec ff_mpeg2video_decoder = {2658const FFCodec ff_mpeg2video_decoder = {
2602 .p.name = "mpeg2video",2659 .p.name = "mpeg2video",
2603 CODEC_LONG_NAME("MPEG-2 video"),2660 CODEC_LONG_NAME("MPEG-2 video"),
2604 .p.type = AVMEDIA_TYPE_VIDEO,2661 .p.type = AVMEDIA_TYPE_VIDEO,
2605 .p.id = AV_CODEC_ID_MPEG2VIDEO,2662 .p.id = AV_CODEC_ID_MPEG2VIDEO,
2663 .p.priv_class = &mpeg2video_class,
2606 .priv_data_size = sizeof(Mpeg1Context),2664 .priv_data_size = sizeof(Mpeg1Context),
2607 .init = mpeg_decode_init,2665 .init = mpeg_decode_init,
2608 .close = mpeg_decode_end,2666 .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