From patchwork Tue Mar 12 06:00:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marth64 X-Patchwork-Id: 46983 Delivered-To: ffmpegpatchwork2@gmail.com Received: by 2002:a05:6a20:dc95:b0:1a1:738b:6bc0 with SMTP id ky21csp1663188pzb; Mon, 11 Mar 2024 23:01:13 -0700 (PDT) X-Forwarded-Encrypted: i=2; AJvYcCXAO24Al6XfmPHSNNf8dE7fQhdbbMLTcEzqjwLzsSK8Puf6bbUoxO/KQdNmPNl1tKaczcaFoEdSVI2oMiQ+CozY73N9TKa7BOT5NA== X-Google-Smtp-Source: AGHT+IFAkR+I22o6XnJLOA4vgKEAcNFtUOBcNwAe3rKIhwelh32X8GMG9Y1diY/cXiWznkjttJzq X-Received: by 2002:a2e:240b:0:b0:2d4:1fe1:b8d3 with SMTP id k11-20020a2e240b000000b002d41fe1b8d3mr474396ljk.50.1710223272696; Mon, 11 Mar 2024 23:01:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1710223272; cv=none; d=google.com; s=arc-20160816; b=HIzcCqF8rVLlq2uz2QNayeDsV2Y/ZCtbR6gxagIwx4ZIh/LpZJkIpRXlfLgHXn4+dJ pV/dRhQO3lk1uJGZyfLbkOB4SFODno0fqbq9eD5XtU3jzfwiAGQepCiNFv3pKTbS92BA +19gTWgcmH/NA/E7kC6KBHx4MZnBYZf+qNuymbBnz6Wh7JVFE7VjdhiicI5ObkyfFaL/ BUMLk7nZWbR4yLIT0ex8WmNzsSgmf6J0qIgVmtj9biF5aHLO2FwomMOaCfwq8huNFtZN 2+o+cE/Od6gLYB+Agw146eCRFmSkGSlKdF+ZfLZXIyCUqfzQu9DwAX0PiQVJSft0u7LH rVog== 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=Oh+jFKMHewiP7iguCRfY7/ossmf4Yoa1T2wzmENpND8=; fh=PlWMzmI9LD2qGS7ipLrQl8z0iaQTLQLHzoGuXcBzpCg=; b=FOjcO/vDuhJGbd6ITiooYUamZ/HvvKKr3bAkEMA68qW1W8ilQMcAs8Xeu/9BsSOm/j 7qgiDL9YiUyLKLLrvnef5vS9RA2SROb+6WUvfnPBRLuE5aBtT2ZEIa3C27HKwqGV1eUG 3103f5J6Xt0Hl0T6H2CZOjhyFegyeovZYaKGdlIjpubE7Go1oXlc6uFoA9e2ZykuBn1+ oxPEZYMR3y974cncKKj7HF+QaciZNZUfkLnZpksNKGYtNTRYm5TuAhsfqXBGQzcM5GNn ei6LiYcGOp/V13RYHTyy27stkReyzhZLW/uTldM9fn+IF1mFytGGBwu8FV87untyrhbO dVrw==; 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=eoSN5D+L; 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 dg17-20020a0564021d1100b005675073609bsi3319691edb.196.2024.03.11.23.01.12; Mon, 11 Mar 2024 23:01:12 -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=eoSN5D+L; 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 48A4B68D13D; Tue, 12 Mar 2024 08:00:25 +0200 (EET) X-Original-To: ffmpeg-devel@ffmpeg.org Delivered-To: ffmpeg-devel@ffmpeg.org Received: from mail-yw1-f225.google.com (mail-yw1-f225.google.com [209.85.128.225]) by ffbox0-bg.mplayerhq.hu (Postfix) with ESMTPS id 3C42A68C6A9 for ; Tue, 12 Mar 2024 08:00:13 +0200 (EET) Received: by mail-yw1-f225.google.com with SMTP id 00721157ae682-609fd5fbe50so47549667b3.0 for ; Mon, 11 Mar 2024 23:00:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=proxyid.net; s=google; t=1710223212; x=1710828012; 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=5Bb9f1sG3Ln52XswA5V9bNssFvWS5L3V2B2I70H7klM=; b=eoSN5D+LsJYWufFskkNnRcrTbqMJrUFxbHCa5bo9qabFlDayoNM99mDktOh42SbZ4W 1Sdyv8HykXgbNXmach6U/UKq1hOjDTdSwkD1eW7x+dIFxkfsuyQW2TvCXcKOVXhdBrwx 9h4gkQOa/9lNO2qqYT4afThzKgzGkPArK920Vq2UKztoNeZB9NrXRw58dMGtgmM3Ja1E wP6aSaNvnc6i3JDwEmCIgLwOChx6HqssLMpuvr/PLN9zhDwjMWRyN2bQWtSasL2+wzrR 1EUXIlZGU/fMgh9PYuoOkp952kEn4uUnB2U3/rLU7ZInsGaY53+1h4dyo5PZd5Znx8ti 0tVQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710223212; x=1710828012; 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=5Bb9f1sG3Ln52XswA5V9bNssFvWS5L3V2B2I70H7klM=; b=N6vfIsdvMdajNDQruYNJ+lsDzDqHBqCuqPJ6OcMKAR2UVdUnII6E1nlmnDBWRRvHjb MP/sUYJoeSt2WC7StncHVs6cd1rUkBPmYS58xZzLZLXv8U2H7kv3DdA9KpP2uAae7TKv R6OpM4Goxi+TvdJeszQzTrTqVt0ePlaIxHFrSHWc91+XIxEj2M1f/v4gMxNMnWLfLRnv iNp7vONKmv2YRiLQfXVJ/nRIP8Oa1pfK3wOvC1GYyg8aoEb2WI0CJx59POI6k8qfYVN7 bbqyEMbHtrTUuCUy7z1vsjRah0vBWcrM0cAkOhZuKnq3+EO+9Z3a/u9UYuXYPpk8QJdt CVsg== X-Gm-Message-State: AOJu0YyB1WhSyoYNNHrLQA1D4U7AVHhgYW+EgnnSKJ6H5zfAmFF6A8QC ow87DiO5qqMP9p2nLkuyj4Ru2k3Z3dzIBTG7u00klCGoSGYx9XFyuE5pUY9ShH1UJ63vSL4NL7y /JsbEL/Phfg7m9B/BQaNGBdeP5UkKRMphX6bvdN8t X-Received: by 2002:a25:b46:0:b0:dcc:1062:47c0 with SMTP id 67-20020a250b46000000b00dcc106247c0mr2414154ybl.56.1710223211930; Mon, 11 Mar 2024 23:00:11 -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 ds4-20020a056902248400b00dc6185d4494sm322329ybb.6.2024.03.11.23.00.11 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 11 Mar 2024 23:00:11 -0700 (PDT) X-Relaying-Domain: proxyid.net From: Marth64 To: ffmpeg-devel@ffmpeg.org Date: Tue, 12 Mar 2024 01:00:04 -0500 Message-Id: <20240312060005.2111135-6-marth64@proxyid.net> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240312060005.2111135-1-marth64@proxyid.net> References: <20240312060005.2111135-1-marth64@proxyid.net> MIME-Version: 1.0 Subject: [FFmpeg-devel] [PATCH v3 5/6] 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: D6S8TwO2p2ZC 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 | 158 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 160 insertions(+) create mode 100644 libavformat/rcwtdec.c diff --git a/libavformat/Makefile b/libavformat/Makefile index 8811a0ffc9..2092ca9f38 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 0a0e76138f..b89a49b6ec 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..f553f13366 --- /dev/null +++ b/libavformat/rcwtdec.c @@ -0,0 +1,158 @@ +/* + * 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/avstring.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; + + int64_t cluster_pts = AV_NOPTS_VALUE; + int cluster_nb_blocks = 0; + int cluster_size = 0; + uint8_t *cluster_buf; + + /* validate the header */ + nb_bytes = avio_read(avf->pb, header, RCWT_HEADER_SIZE); + if (nb_bytes != RCWT_HEADER_SIZE || AV_RB16(header) != 0xCCCC || header[2] != 0xED) { + av_log(avf, AV_LOG_ERROR, "Input is not an RCWT file\n"); + return AVERROR_INVALIDDATA; + } + + if ((header[3] != 0xCC && header[3] != 0xFF) || header[4] != 0x00) { + av_log(avf, AV_LOG_ERROR, "Input writing application is not supported, only " + "0xCC00 (ccextractor) or 0xFF00 (FFmpeg) are compatible\n"); + return AVERROR_INVALIDDATA; + } + + if (AV_RB16(header + 6) != 0x0001) { + av_log(avf, AV_LOG_ERROR, "Input RCWT version is not compatible " + "(only version 0.001 is known)\n"); + return AVERROR_INVALIDDATA; + } + + if (header[3] == 0xFF && header[5] != 0x60) { + av_log(avf, AV_LOG_ERROR, "Input was written by a different version of FFmpeg " + "and unsupported, consider upgrading\n"); + return AVERROR_INVALIDDATA; + } + + /* 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)) { + 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_calloc(cluster_nb_blocks, RCWT_BLOCK_SIZE); + if (!cluster_buf) + return AVERROR(ENOMEM); + + nb_bytes = avio_read(avf->pb, cluster_buf, cluster_size); + if (nb_bytes != cluster_size) { + av_freep(&cluster_buf); + av_log(avf, AV_LOG_ERROR, "Input cluster has invalid 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 +};