From patchwork Mon Oct 12 21:07:21 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Dave Evans X-Patchwork-Id: 22892 Delivered-To: andriy.gelman@gmail.com Received: by 2002:a25:3414:0:0:0:0:0 with SMTP id b20csp1314627yba; Mon, 12 Oct 2020 14:07:47 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyCJKJz38FrkjIiEOk+67mOxoZd9a/QKOFZbjmlAADQaOY6h3t5W9ao0i+8QbhL8duaf0Aa X-Received: by 2002:a1c:6a0a:: with SMTP id f10mr12732877wmc.86.1602536867217; Mon, 12 Oct 2020 14:07:47 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1602536867; cv=none; d=google.com; s=arc-20160816; b=0CBTaT8bbWd9RsgjZJV/CrxOLxpoeo+D+KQh+10OAfZ5+25sxDJKSlI/pKOUPUBSb0 s99Pc1Lm/Su2sfHYhA3R3FtihcI9gBzxGmuW2yTekPF2JXc0v2z0F0atzwJ2r2tyyOhk DdJ/CwhtzxxBSF0STvbu0toVIy5u8J0f1vR7BYon4PLMhP5j/+yhRxBxgrT4X67KHPOL 86kXUNw9CmgVxAGD/aXCU0sVK5tGoPjR9wde6BYgPZXbO+eF7C9rfzpLnnejFBNnoGaS yX9BGbQ2zkAIvUPIcf1K9AOOWqtxFi/8oVGc0iz0r2066Tu42F7ch5Tk9o7DEm7duVds zxJA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence:subject:to :message-id:date:from:mime-version:dkim-signature:delivered-to; bh=YehaTmVjdC9RS8qVtn5bLxpL5uPkmvAyHq1xOmlhe1I=; b=W5oayXJcZ7a3kdZSIrM2/8EK1/LKI6/7ilM+O8vAwXktSdQaQViTmvF6Uwh2xn+yZ9 vUnK7MWJisOZgl3zqYqKYdK53g6BnHoj+4khbPq4kMXI+RLbG5zBVkW0hW7cGVkH/VLC pKm2tEnDKC4zLwqsTpc4JFpZ3hC3tLtkX0ORawobx679OMIXuBjPmd7ceY6oq34VllFW u3qEJE3OlcJJKsfHefCKhO/yAQCzWVFmNazF1u/K1TbdK3bwg0PWhxvSbikQfaXObXlh 4dekZn9iz7vf4yXs/L0igu43lNon26jGlQRCuk18F0JhzxU4q8wyNk7dzOqgNvzIbbmP bEnQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@m2amedia-tv.20150623.gappssmtp.com header.s=20150623 header.b=UDJKZXde; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Return-Path: Received: from ffbox0-bg.mplayerhq.hu (ffbox0-bg.ffmpeg.org. [79.124.17.100]) by mx.google.com with ESMTP id w17si16378041wrr.481.2020.10.12.14.07.46; Mon, 12 Oct 2020 14:07:47 -0700 (PDT) Received-SPF: pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) client-ip=79.124.17.100; Authentication-Results: mx.google.com; dkim=neutral (body hash did not verify) header.i=@m2amedia-tv.20150623.gappssmtp.com header.s=20150623 header.b=UDJKZXde; spf=pass (google.com: domain of ffmpeg-devel-bounces@ffmpeg.org designates 79.124.17.100 as permitted sender) smtp.mailfrom=ffmpeg-devel-bounces@ffmpeg.org Received: from [127.0.1.1] (localhost [127.0.0.1]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTP id 350C668BBB2; Tue, 13 Oct 2020 00:07:45 +0300 (EEST) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-pf1-f171.google.com (mail-pf1-f171.google.com [209.85.210.171]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id DBA1A68BAE3 for ; Tue, 13 Oct 2020 00:07:37 +0300 (EEST) Received: by mail-pf1-f171.google.com with SMTP id 144so14801570pfb.4 for ; Mon, 12 Oct 2020 14:07:37 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=m2amedia-tv.20150623.gappssmtp.com; s=20150623; h=mime-version:from:date:message-id:subject:to; bh=ESidMhkrVmcYP5gXFa2LY/9yN79s1fzgL/IbzYc84A8=; b=UDJKZXdeibTs8VWu5YX1bMmtDXgs6r713is7669IRaWbhCNtuJUnXt+bcqfEti083z 6XfBql2R0kBEyTYaabULEr2PM7VIyHPj37aJ3Vf1zlJyBX+c1emCLqMf0kmEuNFe7/8+ 3oQbOaVaTBRtJ+NKhnXl9+2fzwD302rOcegKa5OT428vVKLBh63T/WBEu14l213aInqK HMFxPMBpaYsG/dSAPaQNprWTHLuLJsx02PetxBOrrEf419WkwkixhyCcFbh0WulA9ADJ R9939Oz2mVFwtU33PwLXch1BqVJMbxBwSaSywRAbFdCoUc6Z2lEU/GUO3M3Oe0lYQpp2 B06Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:from:date:message-id:subject:to; bh=ESidMhkrVmcYP5gXFa2LY/9yN79s1fzgL/IbzYc84A8=; b=ll/1Ylh4n+QXcWOvRbNjfHRU+PLezQf1wWDf2YGqI8GSC98T31jkbhIPafV0djPtVF eIMhJRGBSiL7AIdaZ0QH7RgugMJoXsrUx+znZjK0FFhKEQVErOROJgDgHlCO2dNT3nOy OrlAO4jtaMGLbvHPyogBYA2zCT2a/Rh1Un2xqriSUJwNUhertNSWoQa1w+pcjdCkTd03 xBhc9Uu1JI0Ouu6ZDQAQugZ6mbou8t3ew5Z8IbWMXXk3PKi77GIBaGElqlvTaN7wAukD T1AJlrCTK/nu1U45N/jwzuqhnODvKtXpE+xq4l4OepcH+9DQ7nvzfRgjxPfnvGHCyuah aRaQ== X-Gm-Message-State: AOAM531WK4xI5jq3Zk1pAmQTLqUXZU0rFJPn/S5Xs/I6wxzFDO96ozJu owpZ6Pbs5ody/OJiWqU3cevO56+Ub/Q4TCnB6mqOqmykYrY= X-Received: by 2002:a62:158c:0:b029:152:6669:ac75 with SMTP id 134-20020a62158c0000b02901526669ac75mr24715297pfv.5.1602536855282; Mon, 12 Oct 2020 14:07:35 -0700 (PDT) MIME-Version: 1.0 From: Dave Evans Date: Mon, 12 Oct 2020 22:07:21 +0100 Message-ID: To: ffmpeg-devel@ffmpeg.org X-Content-Filtered-By: Mailman/MimeDel 2.1.20 Subject: [FFmpeg-devel] [PATCH] avformat/webvttdec, enc: correctly process files containing STYLE, REGION blocks X-BeenThere: ffmpeg-devel@ffmpeg.org X-Mailman-Version: 2.1.20 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: L6I+UzP/620K This patch fixes the total failure to parse cues when style and region definition blocks are contained in the input file, and ensures those blocks are written to the output when copying. The test attached needs to be added to samples at the path shown in the patch in order to validate that the original issue is fixed. First patch so please go easy :-) Cheers, Dave Subject: [PATCH] avformat/webvttdec,enc: correctly decode files containing STYLE, REGION blocks Add the ability to extract cues from files containing one or more WebVTT region definition block or WebVTT style block. Previously the decoder would misinterpret these and fail to parse any cues at all. Also add the ability to write these, or alternative header information, back out in the encoder using the codec extradata. --- libavformat/webvttdec.c | 16 ++++++++++++++-- libavformat/webvttenc.c | 12 ++++++++++++ tests/fate/subtitles.mak | 3 +++ tests/ref/fate/sub-webvtt-styleandregions | 19 +++++++++++++++++++ 4 files changed, 48 insertions(+), 2 deletions(-) create mode 100644 tests/ref/fate/sub-webvtt-styleandregions diff --git a/libavformat/webvttdec.c b/libavformat/webvttdec.c index 8d2fdfed37..c6cc367383 100644 --- a/libavformat/webvttdec.c +++ b/libavformat/webvttdec.c @@ -60,7 +60,7 @@ static int64_t read_ts(const char *s) static int webvtt_read_header(AVFormatContext *s) { WebVTTContext *webvtt = s->priv_data; - AVBPrint cue; + AVBPrint cue, header; int res = 0; AVStream *st = avformat_new_stream(s, NULL); @@ -72,6 +72,7 @@ static int webvtt_read_header(AVFormatContext *s) st->disposition |= webvtt->kind; av_bprint_init(&cue, 0, AV_BPRINT_SIZE_UNLIMITED); + av_bprint_init(&header, 0, AV_BPRINT_SIZE_UNLIMITED); for (;;) { int i; @@ -89,12 +90,18 @@ static int webvtt_read_header(AVFormatContext *s) p = identifier = cue.str; pos = avio_tell(s->pb); - /* ignore header chunk */ + /* ignore the magic word and any comments */ if (!strncmp(p, "\xEF\xBB\xBFWEBVTT", 9) || !strncmp(p, "WEBVTT", 6) || !strncmp(p, "NOTE", 4)) continue; + /* store the style and region blocks from the header */ + if (!strncmp(p, "STYLE", 5) || !strncmp(p, "REGION", 6)) { + av_bprintf(&header, "%s%s", header.len ? "\n\n" : "", p); + continue; + } + /* optional cue identifier (can be a number like in SRT or some kind of * chaptering id) */ for (i = 0; p[i] && p[i] != '\n' && p[i] != '\r'; i++) { @@ -161,12 +168,17 @@ static int webvtt_read_header(AVFormatContext *s) SET_SIDE_DATA(settings, AV_PKT_DATA_WEBVTT_SETTINGS); } + res = ff_bprint_to_codecpar_extradata(st->codecpar, &header); + if (res < 0) + goto end; + ff_subtitles_queue_finalize(s, &webvtt->q); end: if (res < 0) ff_subtitles_queue_clean(&webvtt->q); av_bprint_finalize(&cue, NULL); + av_bprint_finalize(&header, NULL); return res; } diff --git a/libavformat/webvttenc.c b/libavformat/webvttenc.c index cbd989dcb6..fcbd3ee10a 100644 --- a/libavformat/webvttenc.c +++ b/libavformat/webvttenc.c @@ -58,6 +58,18 @@ static int webvtt_write_header(AVFormatContext *ctx) avio_printf(pb, "WEBVTT\n"); + if (par->extradata_size > 0) { + size_t header_size = par->extradata_size; + + if (par->extradata[0] != '\n') + avio_printf(pb, "\n"); + + avio_write(pb, par->extradata, header_size); + + if (par->extradata[header_size - 1] != '\n') + avio_printf(pb, "\n"); + } + return 0; } diff --git a/tests/fate/subtitles.mak b/tests/fate/subtitles.mak index 6323d0f93d..41b496e46f 100644 --- a/tests/fate/subtitles.mak +++ b/tests/fate/subtitles.mak @@ -91,6 +91,9 @@ fate-sub-webvtt: CMD = fmtstdout ass -i $(TARGET_SAMPLES)/sub/WebVTT_capability_ FATE_SUBTITLES_ASS-$(call DEMDEC, WEBVTT, WEBVTT) += fate-sub-webvtt2 fate-sub-webvtt2: CMD = fmtstdout ass -i $(TARGET_SAMPLES)/sub/WebVTT_extended_tester.vtt +FATE_SUBTITLES_ASS-$(call DEMDEC, WEBVTT, WEBVTT) += fate-sub-webvtt-styleandregions +fate-sub-webvtt-styleandregions: CMD = fmtstdout ass -i $(TARGET_SAMPLES)/sub/webvtt_style_and_regions.vtt + FATE_SUBTITLES-$(call ALLYES, SRT_DEMUXER SUBRIP_DECODER WEBVTT_ENCODER WEBVTT_MUXER) += fate-sub-webvttenc fate-sub-webvttenc: CMD = fmtstdout webvtt -i $(TARGET_SAMPLES)/sub/SubRip_capability_tester.srt diff --git a/tests/ref/fate/sub-webvtt-styleandregions b/tests/ref/fate/sub-webvtt-styleandregions new file mode 100644 index 0000000000..ca3516fe02 --- /dev/null +++ b/tests/ref/fate/sub-webvtt-styleandregions @@ -0,0 +1,19 @@ +[Script Info] +; Script generated by FFmpeg/Lavc +ScriptType: v4.00+ +PlayResX: 384 +PlayResY: 288 +ScaledBorderAndShadow: yes + +[V4+ Styles] +Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, OutlineColour, BackColour, Bold, Italic, Underline, StrikeOut, ScaleX, ScaleY, Spacing, Angle, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, Encoding +Style: Default,Arial,16,&Hffffff,&Hffffff,&H0,&H0,0,0,0,0,100,100,0,0,1,1,0,2,10,10,10,0 + +[Events] +Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text +Dialogue: 0,0:00:10.00,0:00:25.00,Default,,0,0,0,,Can I tell you a joke, Dad? +Dialogue: 0,0:00:12.50,0:00:27.50,Default,,0,0,0,,Sure, I could do with a laugh. +Dialogue: 0,0:00:15.00,0:00:30.00,Default,,0,0,0,,Where do sheep go to get their hair cut? +Dialogue: 0,0:00:17.50,0:00:32.50,Default,,0,0,0,,I don't know, son. Where do sheep go to get their hair cut? +Dialogue: 0,0:00:20.00,0:00:35.00,Default,,0,0,0,,To the baa-baa shop! +Dialogue: 0,0:00:22.50,0:00:37.50,Default,,0,0,0,,{\i1}[facepalms]{\i0}