From patchwork Tue Mar 19 22:44:26 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marth64 X-Patchwork-Id: 47243 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:8ba5:b0:1a3:31a3:7958 with SMTP id m37csp48563pzh; Tue, 19 Mar 2024 15:45:38 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXQjxOqm+JHpKJ1d9z1zKp70pojuetl1lGo3mHGswBsK2sUBNBIZEsHtHJejayZ4TmKvdPhrubZskGJnz3sCwZdJp6K5gL01UQSNw== X-Google-Smtp-Source: AGHT+IGKyiKDvyo1cdrbVG8eBRHzfssri+AhB5AjQCVhAFvQocqsNw8+kzliF6JQiYCk1vDxTjkd X-Received: by 2002:a17:906:2c4a:b0:a46:4c8e:18a9 with SMTP id f10-20020a1709062c4a00b00a464c8e18a9mr140656ejh.48.1710888337723; Tue, 19 Mar 2024 15:45:37 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1710888337; cv=none; d=google.com; s=arc-20160816; b=If5IgObyh36nnZeErB/D3yssHZGTt0DeF1cbsH2Cail2Qvi6OUNvuQw1Bcz41VYelE fnSOBb5JLLFcnx6EQONFTX5bzZhvpN72wQ1BceZ3oDyfZ7GR9oErf44IagpgZeZ6Iszo E2Zo31RFhSwcvlni2kDBu5H7bOOjHW5+BK9mszKlN9bh5/PofnljNAXEiUDPZUIqngq7 7Np92Wwtr3gmhKU7oRNX2sCYwoBsf16By5xKVb4bY3uGaN7YmeIys1zGCOO5ubVcmIlA yk7JcoWatlD6NkksVQPtmGASApENjlYFJi0Dw57BGb4iDcrxEngpAv4tAdysZEuQHZVT RMQA== 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=7rwnEwDi569d0MBVFGzU3vwSnL9cFUz500yRlVB8jec=; fh=PlWMzmI9LD2qGS7ipLrQl8z0iaQTLQLHzoGuXcBzpCg=; b=ZunoA9+RD3hAeNmVDTenFXE4Hii6Y2ADP+UwKlaG8r/uHqG7qDCLC4gNYYEJE7TB4T BNAezXcFcxIr8GEgo99X9Rxvs7jqEi5t58XgKfBze9VMaeHw5VbdLDjS23hROL1HLp1k R6cCAIPIGsQt4DkaI0Ql3zA+CULJ441d6nyywk/hZ0cRfLg6TWiS8wpoBhNjFapVY+7h 9jd7o+k4at3ZDvJ/Z3Wah2+mDdykuyOuCqKeEuHuuOfjg7QGmYhMeV43tOdmX2Vyj5Ps Pv2PaZ1IzXlNs2ZA3LkLoaq2DVoAreUYDoXZhBtkl71wiursRVlUM7y4Np+Peqt4QrCP uFaQ==; 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="FB/dPdyZ"; 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 u23-20020a1709064ad700b00a465f6612acsi5971751ejt.193.2024.03.19.15.45.08; Tue, 19 Mar 2024 15:45:37 -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="FB/dPdyZ"; 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 76CD268D44C; Wed, 20 Mar 2024 00:44:42 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-io1-f99.google.com (mail-io1-f99.google.com [209.85.166.99]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 800E568D10C for ; Wed, 20 Mar 2024 00:44:34 +0200 (EET) Received: by mail-io1-f99.google.com with SMTP id ca18e2360f4ac-7cc05ce3c70so14092939f.0 for ; Tue, 19 Mar 2024 15:44:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proxyid.net; s=google; t=1710888273; x=1711493073; 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=6CAF8lVQIq9zjRqJjRpm+n2xCa11z9GXnxGFTY346qM=; b=FB/dPdyZk8fSx3HIZt4XUjykdPgoKIn22dEnHZgzNNJFhNTbqO3ygDkHoAlN30dgH1 eGm0G8Vx9X6yNDfRGPFmiKGRNtU18V3OL86z1TpJ3qNyZm72gHFKK7eI5EpVA70ojQ+E z0pQH33gvCrP3xS7S5J9JW6toctBokWpLmoq5B8BEw0QjMP8J2psOooEFlr+SPrwRtGO e2qyoSBqB/FnGccqZKV1zyyVJqiju+46jyUE7FeiNNqLmKiIXIWPn83gRZq2+rBTSI7H N08wL8HoeZMBOIiZ2x91FhPJ5mY9BU5fiJv85bhny94kW6FRdsIIQUVeevePfnbW3239 lrYQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710888273; x=1711493073; 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=6CAF8lVQIq9zjRqJjRpm+n2xCa11z9GXnxGFTY346qM=; b=AQNN1ZN2v9IPKIa/MvEQ9AZWJT1DHwJ++XKiNegkj3g72XpJdLlZJNT7STDWJDxdZR iYMZLM6JvYJSQYpPk/T78irCE81qoh6QC5CYWQdM7uYp1fXpXIyMl+zAsqfzxJEZchK0 yEcaDrYRgPKQsfx2XZ2cvc7MVXVo1oXXZI23Y5KQ7MLg/HyaQrJwTotkWst/CfMKZXtf JaQtI9sRB55jcItv/c94+/KQOojYHk0vlNlfE3THyX6bFMddgJ2pXqbmgo1TnoFsuIr0 f278McLyFPsCqaqtCJ+7wdcdqV77t0DoEdxZE5G7nOodjx8A06R6WsKgK/Vl6Nvr6pZY dlFg== X-Gm-Message-State: AOJu0YxjuMf/Ai76IJImdxyJE7NZ2RKSghlEUy7e9YPz4o/Nnw/dhecu k+sKBSWALmq8cAlB1pE72BTbCgk8gFEaWlGDkeNKbC+fgj5O2iyFYrb7ZmP6Ie3623TrvvfPJRh JDHAIq5+CwPybSgG4VKozhJlXcosdur89LebDgIhV X-Received: by 2002:a05:6602:3fd5:b0:7cf:1567:766c with SMTP id fc21-20020a0566023fd500b007cf1567766cmr1158889iob.2.1710888272788; Tue, 19 Mar 2024 15:44:32 -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 y22-20020a5d94d6000000b007c8c35f9b16sm465699ior.34.2024.03.19.15.44.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 19 Mar 2024 15:44:32 -0700 (PDT) X-Relaying-Domain: proxyid.net From: Marth64 To: ffmpeg-devel@ffmpeg.org Date: Tue, 19 Mar 2024 17:44:26 -0500 Message-Id: <20240319224429.3133701-2-marth64@proxyid.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240319224429.3133701-1-marth64@proxyid.net> References: <20240319224429.3133701-1-marth64@proxyid.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v6 1/4] 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: T2xrM8c0k5v0 Raw Captions With Time (RCWT) is a format native to ccextractor, a commonly used open source tool for processing 608/708 Closed Captions (CC) sources. RCWT can be used to archive the original 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 interoperate with ccextractor (which produces RCWT). Using the muxer/demuxer combination, the CC bits can be kept for further processing or rendering with either tool. This can be an effective approach to backup original CC presentations. Prior to this, the next best solution was FFmpeg's SCC muxer, but SCC itself is not compatible with ccextractor (which is a de facto OSS CC processing tool) and it is a proprietary format. Tests will follow. Signed-off-by: Marth64 --- Changelog | 2 +- libavformat/Makefile | 1 + libavformat/allformats.c | 1 + libavformat/rcwtdec.c | 150 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 153 insertions(+), 1 deletion(-) create mode 100644 libavformat/rcwtdec.c diff --git a/Changelog b/Changelog index e3ca52430c..abe5f79b85 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/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..0d9eba025a --- /dev/null +++ b/libavformat/rcwtdec.c @@ -0,0 +1,150 @@ +/* + * 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_CLUSTER_MAX_BLOCKS 65535 +#define RCWT_BLOCK_SIZE 3 +#define RCWT_HEADER_SIZE 11 + +typedef struct RCWTContext { + FFDemuxSubtitlesQueue q; +} RCWTContext; + +static int rcwt_read_header(AVFormatContext *avf) +{ + RCWTContext *rcwt = avf->priv_data; + + AVPacket *sub = NULL; + AVStream *st; + uint8_t header[RCWT_HEADER_SIZE] = {0}; + int nb_bytes = 0; + + /* validate the header */ + nb_bytes = avio_read(avf->pb, header, RCWT_HEADER_SIZE); + if (nb_bytes != RCWT_HEADER_SIZE) { + av_log(avf, AV_LOG_ERROR, "Header does not have the expected size " + "(expected=%d actual=%d)\n", + RCWT_HEADER_SIZE, nb_bytes); + 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 AVStream */ + 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)) { + int64_t cluster_pts = AV_NOPTS_VALUE; + int cluster_nb_blocks = 0; + int cluster_size = 0; + uint8_t *cluster_buf; + + cluster_pts = avio_rl64(avf->pb); + cluster_nb_blocks = avio_rl16(avf->pb); + if (cluster_nb_blocks == 0) + continue; + + cluster_size = cluster_nb_blocks * RCWT_BLOCK_SIZE; + cluster_buf = av_malloc(cluster_size); + if (!cluster_buf) + return AVERROR(ENOMEM); + + nb_bytes = avio_read(avf->pb, cluster_buf, cluster_size); + if (nb_bytes < 0) + return nb_bytes; + + if (nb_bytes != cluster_size) { + av_freep(&cluster_buf); + av_log(avf, AV_LOG_ERROR, "Cluster does not have the expected size " + "(expected=%d actual=%d pos=%ld)\n", + cluster_size, nb_bytes, avio_tell(avf->pb)); + return AVERROR_INVALIDDATA; + } + + sub = ff_subtitles_queue_insert(&rcwt->q, cluster_buf, cluster_size, 0); + if (!sub) { + av_freep(&cluster_buf); + return AVERROR(ENOMEM); + } + + sub->pos = avio_tell(avf->pb); + sub->pts = cluster_pts; + + av_freep(&cluster_buf); + cluster_buf = NULL; + } + + 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 +};