From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org [79.124.17.100]) by master.gitmailbox.com (Postfix) with ESMTP id 744A249E58 for ; Tue, 12 Mar 2024 15:43:27 +0000 (UTC) Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 2D76E68D18B; Tue, 12 Mar 2024 17:43:26 +0200 (EET) Received: from mail-qv1-f99.google.com (mail-qv1-f99.google.com [209.85.219.99]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id F395668CC35 for ; Tue, 12 Mar 2024 17:43:19 +0200 (EET) Received: by mail-qv1-f99.google.com with SMTP id 6a1803df08f44-690cf6ecd3cso18487966d6.2 for ; Tue, 12 Mar 2024 08:43:19 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proxyid.net; s=google; t=1710258198; x=1710862998; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to; bh=q3QijPdEW7X67jtRgtc6SkH+NDAzjmMEztOkIQQYmks=; b=Qhqm7iaPgWRBgJ18f5x5KSBLXQYQO3B1Ow2dGe3J86WXnMrS2IQmKQAUIjxDMD+Kru mUid3BLXZ40dqRujrz+QeERB5o9QujVRUeLQfGDbHoNwDFy0Rivw0FPi9DsZzhmlL/tw fy6Q1GV/Ga18YNb59CrqBm5EJAHD3aGf2bsZU8qbPTevvArHTpdHDpA/qRo0C8AwCVkz FsUk0BjiMUZ7/edDtkIiVw6Aosff69K9485CjQu2cg4zRwvkk+cibF6Q8/nXWzdBn4ZJ IZaAA3my27WzDtYrKKvZyiNqStdsMXaWvwGI/18kTLIcyf8JAnIKi1R/GCzjU58Kavcl qGWg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710258198; x=1710862998; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-message-state:from:to:cc:subject:date:message-id :reply-to; bh=q3QijPdEW7X67jtRgtc6SkH+NDAzjmMEztOkIQQYmks=; b=GO0eh9g33Fl0Uw6oEm4OS4TSEns+3It9Id4bF0r9b6/7eq4Uw6gj8tvcOsveul+6tR puAR3HWthH+C68gtD7rx1tuNgpPFMnEhjINHiK4AGxQLGLPw/jEn8EarkrK5eG0G0fJK QxTyeMLz/Hvh//zZv5u2X6P0TbocnLZFfaro85S0txa3aengotbpP0Df4QusTKgmcVCI BcFAhFdNeHhaTgf1/ASGkBoZPQIhWomOJvsW4qHB2el4jOzf21FQb1H8BkhyIA1od8Og xNI/NB3jd//xYzBCDjMNCqXXSvSkXKCvs0QvNEa0j9qbh5iJIZIQ2A4oWBvmrwlEZMEx VYbQ== X-Gm-Message-State: AOJu0YwPJ+jGtIH5kCyZ1Q66Ff3nHwQtM4jnvaqnu8xcJvCsSp7FUT+D QYJ5WOGrrYrXjnguRy8+Rnq2FfywTzmX0u+XgasOOK5F3Qj3ahfpISmBx3+NGDDlXhJMewbRJdi jPCicQlQ3qJc7jUIfB47H7+31RKOycgGuAQPhsFT2 X-Google-Smtp-Source: AGHT+IHsQOqKQg+US0Ofb5hajlCaUxWt7Q/TJVHvqUEcZ3AHBNt2vFvWhQz4ZWzhEVYnszKuS4lVUWdmsKl5 X-Received: by 2002:a05:6214:5602:b0:690:50d1:43bd with SMTP id mg2-20020a056214560200b0069050d143bdmr10864894qvb.1.1710258197730; Tue, 12 Mar 2024 08:43:17 -0700 (PDT) Received: from wsx-cc1-001.. (c-76-141-249-38.hsd1.il.comcast.net. [76.141.249.38]) by smtp-relay.gmail.com with ESMTPS id eq5-20020ad45965000000b006904ad60c9fsm363398qvb.67.2024.03.12.08.43.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Mar 2024 08:43:17 -0700 (PDT) X-Relaying-Domain: proxyid.net From: Marth64 To: ffmpeg-devel@ffmpeg.org Date: Tue, 12 Mar 2024 10:43:11 -0500 Message-Id: <20240312154311.2345769-1-marth64@proxyid.net> X-Mailer: git-send-email 2.34.1 MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v4] avcodec/mpeg12dec: extract only one type of CC substream X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: FFmpeg development discussions and patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: FFmpeg development discussions and patches Cc: Marth64 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" Archived-At: List-Archive: List-Post: 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. Signed-off-by: Marth64 --- libavcodec/mpeg12dec.c | 64 ++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 61 insertions(+), 3 deletions(-) diff --git a/libavcodec/mpeg12dec.c b/libavcodec/mpeg12dec.c index 3a2f17e508..4ad1eb6572 100644 --- a/libavcodec/mpeg12dec.c +++ b/libavcodec/mpeg12dec.c @@ -62,6 +62,13 @@ #define A53_MAX_CC_COUNT 2000 +enum Mpeg2ClosedCaptionsFormat { + CC_FORMAT_AUTO, + CC_FORMAT_A53_PART4, + CC_FORMAT_SCTE20, + CC_FORMAT_DVD +}; + typedef struct Mpeg1Context { MpegEncContext mpeg_enc_ctx; int mpeg_enc_ctx_allocated; /* true if decoding context allocated */ @@ -70,6 +77,7 @@ typedef struct Mpeg1Context { AVStereo3D stereo3d; int has_stereo3d; AVBufferRef *a53_buf_ref; + enum Mpeg2ClosedCaptionsFormat cc_format; uint8_t afd; int has_afd; int slice_count; @@ -1903,12 +1911,27 @@ static int vcr2_init_sequence(AVCodecContext *avctx) return 0; } +static void mpeg_set_cc_format(AVCodecContext *avctx, enum Mpeg2ClosedCaptionsFormat format, + const char *label) +{ + Mpeg1Context *s1 = avctx->priv_data; + + av_assert2(format != CC_FORMAT_AUTO); + + if (!s1->cc_format) { + s1->cc_format = format; + + av_log(avctx, AV_LOG_DEBUG, "CC: first seen substream is %s format\n", label); + } +} + static int mpeg_decode_a53_cc(AVCodecContext *avctx, const uint8_t *p, int buf_size) { Mpeg1Context *s1 = avctx->priv_data; - if (buf_size >= 6 && + if ((!s1->cc_format || s1->cc_format == CC_FORMAT_A53_PART4) && + buf_size >= 6 && p[0] == 'G' && p[1] == 'A' && p[2] == '9' && p[3] == '4' && p[4] == 3 && (p[5] & 0x40)) { /* extract A53 Part 4 CC data */ @@ -1927,9 +1950,11 @@ static int mpeg_decode_a53_cc(AVCodecContext *avctx, memcpy(s1->a53_buf_ref->data + old_size, p + 7, cc_count * UINT64_C(3)); avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS; + mpeg_set_cc_format(avctx, CC_FORMAT_A53_PART4, "A/53 Part 4"); } return 1; - } else if (buf_size >= 2 && + } else if ((!s1->cc_format || s1->cc_format == CC_FORMAT_SCTE20) && + buf_size >= 2 && p[0] == 0x03 && (p[1]&0x7f) == 0x01) { /* extract SCTE-20 CC data */ GetBitContext gb; @@ -1973,10 +1998,13 @@ static int mpeg_decode_a53_cc(AVCodecContext *avctx, cap += 3; } } + avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS; + mpeg_set_cc_format(avctx, CC_FORMAT_SCTE20, "SCTE-20"); } return 1; - } else if (buf_size >= 11 && + } else if ((!s1->cc_format || s1->cc_format == CC_FORMAT_DVD) && + buf_size >= 11 && p[0] == 'C' && p[1] == 'C' && p[2] == 0x01 && p[3] == 0xf8) { /* extract DVD CC data * @@ -2033,7 +2061,9 @@ static int mpeg_decode_a53_cc(AVCodecContext *avctx, p += 6; } } + avctx->properties |= FF_CODEC_PROPERTY_CLOSED_CAPTIONS; + mpeg_set_cc_format(avctx, CC_FORMAT_DVD, "DVD"); } return 1; } @@ -2598,11 +2628,39 @@ const FFCodec ff_mpeg1video_decoder = { }, }; +#define M2V_OFFSET(x) offsetof(Mpeg1Context, x) +#define M2V_PARAM AV_OPT_FLAG_VIDEO_PARAM | AV_OPT_FLAG_DECODING_PARAM + +static const AVOption mpeg2video_options[] = { + { "cc_format", "extract a specific Closed Captions format", + M2V_OFFSET(cc_format), AV_OPT_TYPE_INT, { .i64 = CC_FORMAT_AUTO }, + CC_FORMAT_AUTO, CC_FORMAT_DVD, M2V_PARAM, .unit = "cc_format" }, + + { "auto", "pick first seen CC substream", 0, AV_OPT_TYPE_CONST, + { .i64 = CC_FORMAT_AUTO }, .flags = M2V_PARAM, .unit = "cc_format" }, + { "a53", "pick A/53 Part 4 CC substream", 0, AV_OPT_TYPE_CONST, + { .i64 = CC_FORMAT_A53_PART4 }, .flags = M2V_PARAM, .unit = "cc_format" }, + { "scte20", "pick SCTE-20 CC substream", 0, AV_OPT_TYPE_CONST, + { .i64 = CC_FORMAT_SCTE20 }, .flags = M2V_PARAM, .unit = "cc_format" }, + { "dvd", "pick DVD CC substream", 0, AV_OPT_TYPE_CONST, + { .i64 = CC_FORMAT_DVD }, .flags = M2V_PARAM, .unit = "cc_format" }, + { NULL } +}; + +static const AVClass mpeg2video_class = { + .class_name = "MPEG-2 video", + .item_name = av_default_item_name, + .option = mpeg2video_options, + .version = LIBAVUTIL_VERSION_INT, + .category = AV_CLASS_CATEGORY_DECODER, +}; + const FFCodec ff_mpeg2video_decoder = { .p.name = "mpeg2video", CODEC_LONG_NAME("MPEG-2 video"), .p.type = AVMEDIA_TYPE_VIDEO, .p.id = AV_CODEC_ID_MPEG2VIDEO, + .p.priv_class = &mpeg2video_class, .priv_data_size = sizeof(Mpeg1Context), .init = mpeg_decode_init, .close = mpeg_decode_end, -- 2.34.1 _______________________________________________ ffmpeg-devel mailing list ffmpeg-devel@ffmpeg.org https://ffmpeg.org/mailman/listinfo/ffmpeg-devel To unsubscribe, visit link above, or email ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".