From patchwork Thu Mar 21 04:16:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marth64 X-Patchwork-Id: 47284 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a21:3a4a:b0:1a3:31a3:7958 with SMTP id zu10csp315995pzb; Wed, 20 Mar 2024 21:16:33 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXETWxMw7KcW/hiokkyRkL16YcYdZVCvBGgWHQ+HIxqbkYjBhFyXvu4FS90G5RjjuZ6m8xw6k1BrQNX8uvNEuXezeh3DwLIZ53gkQ== X-Google-Smtp-Source: AGHT+IFeV+6/jmQsbVIPEDkyTQt1FD6smLY2KhcM7WnaspS9dqC2Nw2y38L+3RmqoN9qbBBuGDdU X-Received: by 2002:a17:907:86a8:b0:a46:f9a0:748 with SMTP id qa40-20020a17090786a800b00a46f9a00748mr2622180ejc.5.1710994593710; Wed, 20 Mar 2024 21:16:33 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1710994593; cv=none; d=google.com; s=arc-20160816; b=qdQYwOOIiZ6yp8fa5oPnEcVWJTt2iaxwcPg3PC9a7AM/YDRVRol2bWX0lbFpS3r6MN mWPeka1oDc01RDHfaQWkGl0BWbl0h3Fm5q1vWLnDYbiHnGV9mK6DtyTvtW++L1HnQ3RR ERsOjngKyKJjU6ySt28igFxB7AINZ3e8lHzI+vPJwXFUniuvXkSnCySwVzGb48ehorle 5vNzeh8Tlv+O91oURQjyNtR1J2Maovkq+oXdQzAedG8W4wRugJZTKDrHp6Ku1b+8MpIm 0XzUpXJpn0Cv6xTucXTc0aWnuZQFnoTAXqaB6rcIqfas/SpV4rvUTT67sLzyohokiDFb oAsg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:content-transfer-encoding:cc:reply-to :list-subscribe:list-help:list-post:list-archive:list-unsubscribe :list-id:precedence:subject:mime-version:references:in-reply-to :message-id:date:to:from:dkim-signature:delivered-to; bh=7otL2t3QGhSFPpIHKf+6AH7SCkZfOyaKQm7BdWbKdZI=; fh=PlWMzmI9LD2qGS7ipLrQl8z0iaQTLQLHzoGuXcBzpCg=; b=PNY3JRXlL3igheaSE4AHgrYXZ2+iiUJHQlcO11udSWl+dDnEc5FrRsAH/i4SMKKB/j otddQxsJhwd3Cdk6gq7L+qQ9WPVeNfqe2+7WpmFPcnoNiO1Z2VhjoiPujpZe2ej93MIF oSSFUVzizEuJ1u4DHL96PkcJPYqAXqUfI1nCRQfZRi3yyi0TfdeysoLOGlaF7RzDJSH0 mgXpSq059KY4gvMu6n4t+8gvp3hA0BtomG3fvmWtlG0MJLgl21S1zkdr0r5MobnVlE4u OsPfLmWy6IzAfROPlgO+Pllqp/DQIR8x5qrz/WHAQrxsDb7QqKdNQhR7HH2KEIKVyNlf oADg==; dara=google.com ARC-Authentication-Results: i=1; mx.google.com; dkim=neutral (body hash did not verify) header.i=@proxyid.net header.s=google header.b=N1ZFbOPA; 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 z7-20020a17090655c700b00a4666bb4878si6891093ejp.601.2024.03.20.21.16.33; Wed, 20 Mar 2024 21:16:33 -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=@proxyid.net header.s=google header.b=N1ZFbOPA; 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 877E268D54D; Thu, 21 Mar 2024 06:16:30 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org 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 2B10368D28D for ; Thu, 21 Mar 2024 06:16:24 +0200 (EET) Received: by mail-qv1-f99.google.com with SMTP id 6a1803df08f44-690bff9d4a6so3854466d6.1 for ; Wed, 20 Mar 2024 21:16:24 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proxyid.net; s=google; t=1710994583; x=1711599383; darn=ffmpeg.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=q0Ht9c4lrHqaYKdQg4Ftiwb1ODzEQ1PDb/Hb+X7qrrQ=; b=N1ZFbOPApMeNKsSoPnSTm/36llRTflJqG/M4qpu8AOMxwiZmCLDoVYCIscCLW99jj4 5ikdo6oV/9c40CvXhwClVd2gKgJbiFzWPkjrpaaUVvBSwfI9cO7ndUUat+jg1gArO8BG XAhldN6euF4pRvSNgXRViLYtbYrq/d5cDI2DSP4AyrAf7Qic0RtcSrGLPVO7YA1LBfv9 wEdwjdc1+t9jsXXlhC612c1WKXaTpk1ltg+RcXaOwJg2MMc/l44eLlYcVEvtzX/VaG1V 2sSic0XYdgDjZqEnqctnmz8RBvlBMMru26O/V4iNgFH57DiJ8lTiCPmhBtMBpxaasv6S s82w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710994583; x=1711599383; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=q0Ht9c4lrHqaYKdQg4Ftiwb1ODzEQ1PDb/Hb+X7qrrQ=; b=pW8wS++veQc3Z6Vc1MtF7BaONTmeNW1PCtTOfMD8s8PoYNds6QqwDay1Qy/JtmfT4y GpUlBOaLEkgWZ6YW8iQzVh8cF9fJURiUdCn88UtBU/24OqrKct6XvBZDhL0LbMsHBuuI vjQiXHjBYP6UBWD5ZsScW6BWji+Fu+t4yOK5+DdsjphqHO9hYivgXsQdb9TJlwrJQMUb ywmRlb+1y2WP5O5pvZMKjhstJucNH1c6rRG3PlxgTtnrBsewcZrvbwKkj2C3Qb95w/IL FZS5ludKE93Ykasx0U+RMxLfOFTl2MoRXfz0gG+GCebHMtoRs+lXVmz2lC+zaxK9v9k8 2X/A== X-Gm-Message-State: AOJu0YyZAPBTAGXf0qMU/GCbwJ2KNjOnVEIO5tSddqgdEFM32aNB7YmM fYN07y6/z+910QPdDUgO731D9dxLfP4Z+NoPvBWYfCcJr7ECYPWsy65mpur2d/fgTZH4Msp3wbz XUtng0QDi/NINf7ZFsTvxudKHaezHLB4IvRMqFJ94 X-Received: by 2002:a0c:aad7:0:b0:696:10e5:411 with SMTP id g23-20020a0caad7000000b0069610e50411mr4484395qvb.6.1710994582691; Wed, 20 Mar 2024 21:16:22 -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 9-20020a0562140cc900b0068f2e5f91e9sm923452qvx.38.2024.03.20.21.16.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 20 Mar 2024 21:16:22 -0700 (PDT) X-Relaying-Domain: proxyid.net From: Marth64 To: ffmpeg-devel@ffmpeg.org Date: Wed, 20 Mar 2024 23:16:13 -0500 Message-Id: <20240321041613.3850264-1-marth64@proxyid.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240321035207.3849560-3-marth64@proxyid.net> References: <20240321035207.3849560-3-marth64@proxyid.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v8] avformat/rcwtdec: add RCWT Closed Captions demuxer 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 Errors-To: ffmpeg-devel-bounces@ffmpeg.org Sender: "ffmpeg-devel" X-TUID: wJnETl0Dxp2r RCWT (Raw Captions With Time) is a format native to ccextractor, a commonly used OSS tool for processing 608/708 Closed Captions (CC). RCWT can be used to archive the original extracted CC bitstream. The muxer was added in January 2024. In this commit, add the demuxer. One can now demux RCWT files for rendering in ccaption_dec or interop with ccextractor (which produces RCWT). Using the muxer/demuxer combo, the CC bits can be kept for processing or rendering with either tool. This can be an effective way to backup an original CC stream, including format extensions like EIA-708 and overall original presentation. Signed-off-by: Marth64 --- Changelog | 2 +- doc/demuxers.texi | 30 ++++++++++ libavformat/Makefile | 1 + libavformat/allformats.c | 1 + libavformat/rcwtdec.c | 125 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 158 insertions(+), 1 deletion(-) create mode 100644 libavformat/rcwtdec.c diff --git a/Changelog b/Changelog index e3ca52430c..0ae05c6cce 100644 --- a/Changelog +++ b/Changelog @@ -19,7 +19,7 @@ version : - lavu/eval: introduce randomi() function in expressions - VVC decoder - fsync filter -- Raw Captions with Time (RCWT) closed caption muxer +- RCWT (Raw Captions with Time) Closed Captions muxer and demuxer - ffmpeg CLI -bsf option may now be used for input as well as output - ffmpeg CLI options may now be used as -/opt , which is equivalent to -opt > diff --git a/doc/demuxers.texi b/doc/demuxers.texi index b70f3a38d7..b4c4daacd9 100644 --- a/doc/demuxers.texi +++ b/doc/demuxers.texi @@ -1038,6 +1038,36 @@ the command: ffplay -f rawvideo -pixel_format rgb24 -video_size 320x240 -framerate 10 input.raw @end example +@anchor{rcwtdec} +@section rcwt + +RCWT (Raw Captions With Time) is a format native to ccextractor, a commonly +used open source tool for processing 608/708 Closed Captions (CC) sources. +For more information on the format, see @ref{rcwtenc,,,ffmpeg-formats}. + +This demuxer implements the specification as of March 2024, which has +been stable and unchanged since April 2014. + +@subsection Examples + +@itemize +@item +Render Closed Captions to ASS, using the CC decoder (cc_dec): +@example +ffmpeg -i CC.rcwt.bin CC.ass +@end example +Note that if your output appears to be empty, you may have to manually +set the decoder's @option{data_field} option to pick the desired CC substream. + +@item +Convert an RCWT backup to SCC format, using the SCC muxer (scc): +@example +ffmpeg -i CC.rcwt.bin -c:s copy CC.scc +@end example +Note that the SCC format does not support all of the possible CC formats +that can be stored in RCWT. +@end itemize + @section sbg SBaGen script demuxer. diff --git a/libavformat/Makefile b/libavformat/Makefile index 94a949f555..a6de720d8c 100644 --- a/libavformat/Makefile +++ b/libavformat/Makefile @@ -493,6 +493,7 @@ OBJS-$(CONFIG_QOA_DEMUXER) += qoadec.o OBJS-$(CONFIG_R3D_DEMUXER) += r3d.o OBJS-$(CONFIG_RAWVIDEO_DEMUXER) += rawvideodec.o OBJS-$(CONFIG_RAWVIDEO_MUXER) += rawenc.o +OBJS-$(CONFIG_RCWT_DEMUXER) += rcwtdec.o subtitles.o OBJS-$(CONFIG_RCWT_MUXER) += rcwtenc.o subtitles.o OBJS-$(CONFIG_REALTEXT_DEMUXER) += realtextdec.o subtitles.o OBJS-$(CONFIG_REDSPARK_DEMUXER) += redspark.o diff --git a/libavformat/allformats.c b/libavformat/allformats.c index e15d0fa6d7..3140018f8d 100644 --- a/libavformat/allformats.c +++ b/libavformat/allformats.c @@ -391,6 +391,7 @@ extern const FFInputFormat ff_qoa_demuxer; extern const FFInputFormat ff_r3d_demuxer; extern const FFInputFormat ff_rawvideo_demuxer; extern const FFOutputFormat ff_rawvideo_muxer; +extern const FFInputFormat ff_rcwt_demuxer; extern const FFOutputFormat ff_rcwt_muxer; extern const FFInputFormat ff_realtext_demuxer; extern const FFInputFormat ff_redspark_demuxer; diff --git a/libavformat/rcwtdec.c b/libavformat/rcwtdec.c new file mode 100644 index 0000000000..ca5bdc1940 --- /dev/null +++ b/libavformat/rcwtdec.c @@ -0,0 +1,125 @@ +/* + * RCWT (Raw Captions With Time) demuxer + * + * This file is part of FFmpeg. + * + * FFmpeg is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * FFmpeg is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with FFmpeg; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + */ + +/* + * RCWT (Raw Captions With Time) is a format native to ccextractor, a commonly + * used open source tool for processing 608/708 Closed Captions (CC) sources. + * + * This demuxer implements the specification as of March 2024, which has + * been stable and unchanged since April 2014. + * + * A free specification of RCWT can be found here: + * @url{https://github.com/CCExtractor/ccextractor/blob/master/docs/BINARY_FILE_FORMAT.TXT} + */ + +#include "avformat.h" +#include "demux.h" +#include "internal.h" +#include "subtitles.h" +#include "libavutil/intreadwrite.h" + +#define RCWT_HEADER_SIZE 11 + +typedef struct RCWTContext { + FFDemuxSubtitlesQueue q; +} RCWTContext; + +static int rcwt_read_header(AVFormatContext *avf) +{ + RCWTContext *rcwt = avf->priv_data; + + AVStream *st; + uint8_t header[RCWT_HEADER_SIZE]; + int nb_bytes; + + /* read header */ + nb_bytes = ffio_read_size(avf->pb, header, RCWT_HEADER_SIZE); + if (nb_bytes < 0) + return nb_bytes; + if (nb_bytes != RCWT_HEADER_SIZE) + return AVERROR_INVALIDDATA; + + if (AV_RB16(header + 6) != 0x0001) { + av_log(avf, AV_LOG_ERROR, "RCWT format version is not compatible " + "(only version 0.001 is known)\n"); + return AVERROR_INVALIDDATA; + } + + av_log(avf, AV_LOG_DEBUG, "RCWT writer application: %02X version: %02x\n", + header[3], header[5]); + + /* setup stream */ + st = avformat_new_stream(avf, NULL); + if (!st) + return AVERROR(ENOMEM); + + st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE; + st->codecpar->codec_id = AV_CODEC_ID_EIA_608; + + avpriv_set_pts_info(st, 64, 1, 1000); + + /* demux */ + while (!avio_feof(avf->pb)) { + AVPacket *sub; + int ret; + int64_t cluster_pos = avio_tell(avf->pb); + int64_t cluster_pts = avio_rl64(avf->pb); + int cluster_nb_blocks = avio_rl16(avf->pb); + + if (cluster_nb_blocks == 0) + continue; + + sub = ff_subtitles_queue_insert(&rcwt->q, NULL, 0, 0); + if (!sub) + return AVERROR(ENOMEM); + + if ((ret = av_get_packet(avf->pb, sub, cluster_nb_blocks * 3)) < 0) + return ret; + + sub->pos = cluster_pos; + sub->pts = cluster_pts; + } + + ff_subtitles_queue_finalize(avf, &rcwt->q); + + return 0; +} + +static int rcwt_probe(const AVProbeData *p) +{ + return p->buf_size > RCWT_HEADER_SIZE && + AV_RB16(p->buf) == 0xCCCC && + AV_RB8(p->buf + 2) == 0xED && + AV_RB16(p->buf + 6) == 0x0001 ? 50 : 0; +} + +const FFInputFormat ff_rcwt_demuxer = { + .p.name = "rcwt", + .p.long_name = NULL_IF_CONFIG_SMALL("RCWT (Raw Captions With Time)"), + .p.extensions = "bin", + .p.flags = AVFMT_TS_DISCONT, + .priv_data_size = sizeof(RCWTContext), + .flags_internal = FF_FMT_INIT_CLEANUP, + .read_probe = rcwt_probe, + .read_header = rcwt_read_header, + .read_packet = ff_subtitles_read_packet, + .read_seek2 = ff_subtitles_read_seek, + .read_close = ff_subtitles_read_close +};