FFmpeg
rcwtdec.c
Go to the documentation of this file.
1 /*
2  * RCWT (Raw Captions With Time) demuxer
3  *
4  * This file is part of FFmpeg.
5  *
6  * FFmpeg is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * FFmpeg is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with FFmpeg; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20 
21 /*
22  * RCWT (Raw Captions With Time) is a format native to ccextractor, a commonly
23  * used open source tool for processing 608/708 Closed Captions (CC) sources.
24  *
25  * This demuxer implements the specification as of March 2024, which has
26  * been stable and unchanged since April 2014.
27  *
28  * A free specification of RCWT can be found here:
29  * @url{https://github.com/CCExtractor/ccextractor/blob/master/docs/BINARY_FILE_FORMAT.TXT}
30  */
31 
32 #include "avformat.h"
33 #include "demux.h"
34 #include "internal.h"
35 #include "subtitles.h"
36 #include "libavutil/intreadwrite.h"
37 
38 #define RCWT_HEADER_SIZE 11
39 
40 typedef struct RCWTContext {
42 } RCWTContext;
43 
45 {
46  RCWTContext *rcwt = avf->priv_data;
47 
48  AVStream *st;
49  uint8_t header[RCWT_HEADER_SIZE];
50  int ret;
51 
52  /* read header */
54  if (ret < 0)
55  return ret;
56 
57  if (AV_RB16(header + 6) != 0x0001) {
58  av_log(avf, AV_LOG_ERROR, "RCWT format version is not compatible "
59  "(only version 0.001 is known)\n");
60  return AVERROR_INVALIDDATA;
61  }
62 
63  av_log(avf, AV_LOG_DEBUG, "RCWT writer application: %02X version: %02x\n",
64  header[3], header[5]);
65 
66  /* setup stream */
67  st = avformat_new_stream(avf, NULL);
68  if (!st)
69  return AVERROR(ENOMEM);
70 
73 
74  avpriv_set_pts_info(st, 64, 1, 1000);
75 
76  /* demux */
77  while (!avio_feof(avf->pb)) {
78  AVPacket *sub;
79  int64_t cluster_pos = avio_tell(avf->pb);
80  int64_t cluster_pts = avio_rl64(avf->pb);
81  int cluster_nb_blocks = avio_rl16(avf->pb);
82 
83  if (cluster_nb_blocks == 0)
84  continue;
85 
86  sub = ff_subtitles_queue_insert(&rcwt->q, NULL, 0, 0);
87  if (!sub)
88  return AVERROR(ENOMEM);
89 
90  ret = av_get_packet(avf->pb, sub, cluster_nb_blocks * 3);
91  if (ret < 0)
92  return ret;
93 
94  sub->pos = cluster_pos;
95  sub->pts = cluster_pts;
96  }
97 
98  ff_subtitles_queue_finalize(avf, &rcwt->q);
99 
100  return 0;
101 }
102 
103 static int rcwt_probe(const AVProbeData *p)
104 {
105  return p->buf_size > RCWT_HEADER_SIZE &&
106  AV_RB16(p->buf) == 0xCCCC &&
107  AV_RB8(p->buf + 2) == 0xED &&
108  AV_RB16(p->buf + 6) == 0x0001 ? 50 : 0;
109 }
110 
112  .p.name = "rcwt",
113  .p.long_name = NULL_IF_CONFIG_SMALL("RCWT (Raw Captions With Time)"),
114  .p.flags = AVFMT_TS_DISCONT,
115  .priv_data_size = sizeof(RCWTContext),
116  .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
120  .read_seek2 = ff_subtitles_read_seek,
122 };
AV_CODEC_ID_EIA_608
@ AV_CODEC_ID_EIA_608
Definition: codec_id.h:566
AVMEDIA_TYPE_SUBTITLE
@ AVMEDIA_TYPE_SUBTITLE
Definition: avutil.h:204
ff_subtitles_read_close
int ff_subtitles_read_close(AVFormatContext *s)
Definition: subtitles.c:345
ff_rcwt_demuxer
const FFInputFormat ff_rcwt_demuxer
Definition: rcwtdec.c:111
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
AVCodecParameters::codec_type
enum AVMediaType codec_type
General type of the encoded data.
Definition: codec_par.h:51
avformat_new_stream
AVStream * avformat_new_stream(AVFormatContext *s, const struct AVCodec *c)
Add a new stream to a media file.
int64_t
long long int64_t
Definition: coverity.c:34
AVProbeData::buf_size
int buf_size
Size of buf except extra allocated bytes.
Definition: avformat.h:454
avpriv_set_pts_info
void avpriv_set_pts_info(AVStream *st, int pts_wrap_bits, unsigned int pts_num, unsigned int pts_den)
Set the time base and wrapping info for a given stream.
Definition: avformat.c:867
rcwt_probe
static int rcwt_probe(const AVProbeData *p)
Definition: rcwtdec.c:103
read_close
static av_cold int read_close(AVFormatContext *ctx)
Definition: libcdio.c:143
avio_tell
static av_always_inline int64_t avio_tell(AVIOContext *s)
ftell() equivalent for AVIOContext.
Definition: avio.h:494
avio_rl16
unsigned int avio_rl16(AVIOContext *s)
Definition: aviobuf.c:714
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:209
ff_subtitles_read_packet
int ff_subtitles_read_packet(AVFormatContext *s, AVPacket *pkt)
Definition: subtitles.c:331
read_packet
static int read_packet(void *opaque, uint8_t *buf, int buf_size)
Definition: avio_read_callback.c:42
intreadwrite.h
AVInputFormat::name
const char * name
A comma separated list of short names for the format.
Definition: avformat.h:553
AVProbeData::buf
unsigned char * buf
Buffer must have AVPROBE_PADDING_SIZE of extra allocated bytes filled with zero.
Definition: avformat.h:453
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:230
FF_INFMT_FLAG_INIT_CLEANUP
#define FF_INFMT_FLAG_INIT_CLEANUP
For an FFInputFormat with this flag set read_close() needs to be called by the caller upon read_heade...
Definition: demux.h:35
RCWT_HEADER_SIZE
#define RCWT_HEADER_SIZE
Definition: rcwtdec.c:38
AVFormatContext
Format I/O context.
Definition: avformat.h:1300
internal.h
AVStream::codecpar
AVCodecParameters * codecpar
Codec parameters associated with this stream.
Definition: avformat.h:771
read_header
static int read_header(FFV1Context *f)
Definition: ffv1dec.c:540
NULL
#define NULL
Definition: coverity.c:32
rcwt_read_header
static int rcwt_read_header(AVFormatContext *avf)
Definition: rcwtdec.c:44
AVFormatContext::pb
AVIOContext * pb
I/O context.
Definition: avformat.h:1342
AVProbeData
This structure contains the data a format has to probe a file.
Definition: avformat.h:451
ff_subtitles_queue_insert
AVPacket * ff_subtitles_queue_insert(FFDemuxSubtitlesQueue *q, const uint8_t *event, size_t len, int merge)
Insert a new subtitle event.
Definition: subtitles.c:111
NULL_IF_CONFIG_SMALL
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition: internal.h:94
ff_subtitles_queue_finalize
void ff_subtitles_queue_finalize(void *log_ctx, FFDemuxSubtitlesQueue *q)
Set missing durations, sort subtitles by PTS (and then byte position), and drop duplicated events.
Definition: subtitles.c:212
FFDemuxSubtitlesQueue
Definition: subtitles.h:103
FFInputFormat::p
AVInputFormat p
The public AVInputFormat.
Definition: demux.h:46
header
static const uint8_t header[24]
Definition: sdr2.c:68
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:532
RCWTContext
Definition: rcwtdec.c:40
RCWTContext::q
FFDemuxSubtitlesQueue q
Definition: rcwtdec.c:41
ff_subtitles_read_seek
int ff_subtitles_read_seek(AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
Definition: subtitles.c:337
demux.h
av_get_packet
int av_get_packet(AVIOContext *s, AVPacket *pkt, int size)
Allocate and read the payload of a packet and initialize its fields with default values.
Definition: utils.c:91
ret
ret
Definition: filter_design.txt:187
AVStream
Stream structure.
Definition: avformat.h:748
avformat.h
subtitles.h
AV_RB8
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_WB16 unsigned int_TMPL AV_RB8
Definition: bytestream.h:99
read_probe
static int read_probe(const AVProbeData *p)
Definition: cdg.c:30
AVFMT_TS_DISCONT
#define AVFMT_TS_DISCONT
Format allows timestamp discontinuities.
Definition: avformat.h:481
AVCodecParameters::codec_id
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
Definition: codec_par.h:55
AVPacket
This structure stores compressed data.
Definition: packet.h:516
AVPacket::pos
int64_t pos
byte position in stream, -1 if unknown
Definition: packet.h:559
FFInputFormat
Definition: demux.h:42
avio_rl64
uint64_t avio_rl64(AVIOContext *s)
Definition: aviobuf.c:738
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
ffio_read_size
int ffio_read_size(AVIOContext *s, unsigned char *buf, int size)
Read size bytes from AVIOContext into buf.
Definition: aviobuf.c:662
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1328
AV_RB16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
Definition: bytestream.h:98
avio_feof
int avio_feof(AVIOContext *s)
Similar to feof() but also returns nonzero on read errors.
Definition: aviobuf.c:346