From patchwork Sun Mar 17 04:29:41 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marth64 X-Patchwork-Id: 47125 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:958e:b0:1a3:31a3:7958 with SMTP id iu14csp335958pzb; Sat, 16 Mar 2024 21:30:45 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCU8jHVCBwVBQPuBAzOcaY0SucV9ilrf4r3E09kMvoM+JgtTXWw8vXUlKzUM71e15E/zQKh9VdpAG1qG10PjbxKluPFi6daW+ezBKw== X-Google-Smtp-Source: AGHT+IGF61a0Q5iMI0BjpVX/L02qYpKEhctC8PMOF1Lc5Axdfo4TPvb17A6sQpU1ZaFKnv1wemDY X-Received: by 2002:a2e:91cd:0:b0:2d2:4783:872a with SMTP id u13-20020a2e91cd000000b002d24783872amr5414482ljg.29.1710649845123; Sat, 16 Mar 2024 21:30:45 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1710649845; cv=none; d=google.com; s=arc-20160816; b=HZjHMhqcpFc6ow3qWXI9P3r1G4r2/Uvhz9EnhgWclEzJ44N0XmQkRgOzNJ6bKtQ84f d1Lw0QzDY/miohMQ0WDxUJ7A3z8M2ZMVXx06JpcOB4A821Ro5N/t4JrtZNEIpK7R4k8M LSuDULUVV+ayb8a7p1GbQGncWbs6cz1o7wBgQtd2Eb+jcUnz7C8sqRtpAnVrfVk87oGq /uBsNgbLqLk/68FiSTSp3FT2cp2jqnQU0xOcJ56jn+ooYJDiF3e3/TvPJ9+bmM5OhOQi eB+rwJZ8zghTcdakapCFwkrfjiCEjhnM1MJzocH9ErJKHirUetCUoslf55vMoEZQsgQT jEVQ== 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=QXomtcwwfn1YeUy0UAlhAgQiMUfpu1e3kX0GRKMhQeI=; fh=PlWMzmI9LD2qGS7ipLrQl8z0iaQTLQLHzoGuXcBzpCg=; b=oGK/DYSAkY/ul3DFSal7LLAoNZdqIrM9VtaWyep314ZWMj+Ce9umuEj/AsmCIYlgnp MzSHSNZB5Fuajjfsijbr4F5WOvYAW/vVYCwK7cPjlGpphfJ4pAkEEBQedAYpsUbD6HE2 PYkBX2Q4SIGasrqlIq/MElpyBFbDJwcLWkyEmxdDOIhk0Ablfn33bWQKCy6v4sIyHiTH Du0BevH34H41a10pCFmUcFAxsUshdvx+Am1DWJaq7CAl99r3JpUFWNWCJx5JNa8MJIFc LSjs+G9hWyNFRQK7u0Yrmc0VggG4tb1QhJLH35x+FJEAuAXfYBx6n0fmdSPLmcV6UwEP 0XpA==; 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="V/Sj/+cP"; 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 j15-20020aa7c34f000000b0056889f485e7si3139258edr.200.2024.03.16.21.30.44; Sat, 16 Mar 2024 21:30:45 -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="V/Sj/+cP"; 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 B7A6F68D18B; Sun, 17 Mar 2024 06:30:41 +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 43BF168C1C6 for ; Sun, 17 Mar 2024 06:30:35 +0200 (EET) Received: by mail-qv1-f99.google.com with SMTP id 6a1803df08f44-690cbf99143so19848436d6.3 for ; Sat, 16 Mar 2024 21:30:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proxyid.net; s=google; t=1710649834; x=1711254634; 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=E4MwOTm6lhOV7tkitRkX+inajZBJ3UeDSlzm2WOx2Og=; b=V/Sj/+cPX2+Dmk7RjrXgqAtQcEU5r6lY5TNf2MuVcmTu2b80UuoVxetJ2L/NapPFvp l9/yGDkYAEXQ/wz0yXKuMmu/BGVcycUeBI62JlsOQ0VNhJLV2zhbDB18KNQGBZdly1hn eNAVPnywRSnDUUdAe7Zr4ImrM+sKQyk7DXTKoLPTP0VEPFamrm0BcmEmcoJbbTdk6Ms4 N7MDqLYXGM6wpnScJDa83yktDYnRFWvzNof0ujcJjWQ6TCNn4guw+sYYQ9ewU2pWUc3/ id4B4kAS4t/b2J8CDDLrj9rJOKqoWL1osamRamLCk6cChrdyAax2frbnP2IDDPyNOsUZ F0RQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710649834; x=1711254634; 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=E4MwOTm6lhOV7tkitRkX+inajZBJ3UeDSlzm2WOx2Og=; b=IxqqKcEDgVICm6nvcsf3EP/MdPzV0rOU0xjXZPbyXDV8kT6G09dEqylluFr7bO35Gm NduBJgk7KBxK2oSG1nSertkQVcC4I3p1GAl86HpvfJYbqMz79EOM4gnHC7L9ykJClcPd iGfDLRPaMqyZS2Rki0bCeFgfpdDYKeVS0RepIiuL+nDK/GPc5Fr0LnFTPwiEBdxEAVEb dL0GCabe0XOkqnTWNJl16Lg/Pn58gJ4VWapbyjn/zk4HXjlKlCa2EoWh7V85dGWM5C01 O5dbpSxUOXi1/qW5VBV+MVPg7ONbE7aCEwNKHBrPe6CPwInEN0tX4KLicRG7iUeFbDVi zkhA== X-Gm-Message-State: AOJu0YwSi4N5Q83MlAi4RS5BeYCpR+sDXLYsa9nWEYLgj/FHX6A2umbH 0Eqm2HCtc4Rxzg2S5z7wGaheU+kinn1S97Ure9DKJxTv3Jitte6guT2vL1VsbGhP42yjpWYDLao L/YY1Ytl1mVvWUz2DkB5qUA6emzl05BoDlEM8nJwR X-Received: by 2002:a0c:ecc2:0:b0:690:ca04:cec4 with SMTP id o2-20020a0cecc2000000b00690ca04cec4mr11102289qvq.52.1710649833755; Sat, 16 Mar 2024 21:30:33 -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 k9-20020ad45be9000000b006912140e82dsm341565qvc.30.2024.03.16.21.30.33 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 16 Mar 2024 21:30:33 -0700 (PDT) X-Relaying-Domain: proxyid.net From: Marth64 To: ffmpeg-devel@ffmpeg.org Date: Sat, 16 Mar 2024 23:29:41 -0500 Message-Id: <20240317042940.2545620-1-marth64@proxyid.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: References: MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v4] 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: eIGxjNge8UKx 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 --- libavformat/Makefile | 1 + libavformat/allformats.c | 1 + libavformat/rcwtdec.c | 153 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 155 insertions(+) create mode 100644 libavformat/rcwtdec.c 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..1fd35be41b --- /dev/null +++ b/libavformat/rcwtdec.c @@ -0,0 +1,153 @@ +/* + * 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. + * It can be used to archive the original, raw CC bitstream and to produce + * a source file for later CC processing or conversion. As a result, + * it also allows for interopability with ccextractor for processing CC data + * extracted via ffmpeg. The format is simple to parse and can be used + * to retain all lines and variants of CC. + * + * 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 ? 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 +};