FFmpeg
rcwtenc.c
Go to the documentation of this file.
1 /*
2  * RCWT (Raw Captions With Time) muxer
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  * It can be used to archive the original, raw CC bitstream and to produce
25  * a source file for later CC processing or conversion. As a result,
26  * it also allows for interopability with ccextractor for processing CC data
27  * extracted via ffmpeg. The format is simple to parse and can be used
28  * to retain all lines and variants of CC.
29  *
30  * This muxer implements the specification as of March 2024, which has
31  * been stable and unchanged since April 2014.
32  *
33  * This muxer will have some nuances from the way that ccextractor muxes RCWT.
34  * No compatibility issues when processing the output with ccextractor
35  * have been observed as a result of this so far, but mileage may vary
36  * and outputs will not be a bit-exact match.
37  *
38  * Specifically, the differences are:
39  * (1) This muxer will identify as "FF" as the writing program identifier, so
40  * as to be honest about the output's origin.
41  *
42  * (2) This muxer will not alter the extracted data except to remove invalid
43  * packets in between valid CC blocks. On the other hand, ccextractor
44  * will by default remove mid-stream padding, and add padding at the end
45  * of the stream (in order to convey the end time of the source video).
46  *
47  * A free specification of RCWT can be found here:
48  * @url{https://github.com/CCExtractor/ccextractor/blob/master/docs/BINARY_FILE_FORMAT.TXT}
49  */
50 
51 #include "avformat.h"
52 #include "internal.h"
53 #include "mux.h"
54 #include "libavutil/log.h"
55 #include "libavutil/intreadwrite.h"
56 
57 #define RCWT_CLUSTER_MAX_BLOCKS 65535
58 #define RCWT_BLOCK_SIZE 3
59 
60 typedef struct RCWTContext {
64 } RCWTContext;
65 
66 static void rcwt_init_cluster(RCWTContext *rcwt)
67 {
68  rcwt->cluster_pos = 0;
70 }
71 
73 {
74  RCWTContext *rcwt = avf->priv_data;
75 
76  if (rcwt->cluster_pos > 0) {
77  avio_wl64(avf->pb, rcwt->cluster_pts);
78  avio_wl16(avf->pb, rcwt->cluster_pos / RCWT_BLOCK_SIZE);
79  avio_write(avf->pb, rcwt->cluster_buf, rcwt->cluster_pos);
80  }
81 
82  rcwt_init_cluster(rcwt);
83 }
84 
86 {
87  avpriv_set_pts_info(avf->streams[0], 64, 1, 1000);
88 
89  /* magic number */
90  avio_wb16(avf->pb, 0xCCCC);
91  avio_w8(avf->pb, 0xED);
92 
93  /* program version (identify as ffmpeg) */
94  avio_wb16(avf->pb, 0xFF00);
95  avio_w8(avf->pb, 0x60);
96 
97  /* format version, only version 0.001 supported for now */
98  avio_wb16(avf->pb, 0x0001);
99 
100  /* reserved */
101  avio_wb16(avf->pb, 0x000);
102  avio_w8(avf->pb, 0x00);
103 
105 
106  return 0;
107 }
108 
110 {
111  RCWTContext *rcwt = avf->priv_data;
112 
113  if (pkt->size < RCWT_BLOCK_SIZE)
114  return 0;
115 
116  /* new PTS, new cluster */
117  if (pkt->pts != rcwt->cluster_pts) {
118  rcwt_flush_cluster(avf);
119  rcwt->cluster_pts = pkt->pts;
120  }
121 
122  if (pkt->pts == AV_NOPTS_VALUE) {
123  av_log(avf, AV_LOG_WARNING, "Ignoring CC packet with no PTS\n");
124  return 0;
125  }
126 
127  for (int i = 0; i <= pkt->size - RCWT_BLOCK_SIZE;) {
128  uint8_t cc_valid;
129  uint8_t cc_type;
130 
132  av_log(avf, AV_LOG_WARNING, "Starting new cluster due to size\n");
133  rcwt_flush_cluster(avf);
134  }
135 
136  cc_valid = (pkt->data[i] & 0x04) >> 2;
137  cc_type = pkt->data[i] & 0x03;
138 
139  if (!(cc_valid || cc_type == 3)) {
140  i++;
141  continue;
142  }
143 
144  memcpy(&rcwt->cluster_buf[rcwt->cluster_pos], &pkt->data[i], 3);
145  rcwt->cluster_pos += 3;
146  i += 3;
147  }
148 
149  return 0;
150 }
151 
153 {
154  rcwt_flush_cluster(avf);
155 
156  return 0;
157 }
158 
160  .p.name = "rcwt",
161  .p.long_name = NULL_IF_CONFIG_SMALL("RCWT (Raw Captions With Time)"),
162  .p.extensions = "bin",
164  .p.video_codec = AV_CODEC_ID_NONE,
165  .p.audio_codec = AV_CODEC_ID_NONE,
166  .p.subtitle_codec = AV_CODEC_ID_EIA_608,
167  .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH |
169  .priv_data_size = sizeof(RCWTContext),
173 };
AV_CODEC_ID_EIA_608
@ AV_CODEC_ID_EIA_608
Definition: codec_id.h:559
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:186
AVOutputFormat::name
const char * name
Definition: avformat.h:510
RCWT_BLOCK_SIZE
#define RCWT_BLOCK_SIZE
Definition: rcwtenc.c:58
ff_rcwt_muxer
const FFOutputFormat ff_rcwt_muxer
Definition: rcwtenc.c:159
AVFMT_VARIABLE_FPS
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition: avformat.h:482
int64_t
long long int64_t
Definition: coverity.c:34
AVFormatContext::streams
AVStream ** streams
A list of all streams in the file.
Definition: avformat.h:1323
AVPacket::data
uint8_t * data
Definition: packet.h:522
avio_wl64
void avio_wl64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:424
FF_OFMT_FLAG_ONLY_DEFAULT_CODECS
#define FF_OFMT_FLAG_ONLY_DEFAULT_CODECS
If this flag is set, then the only permitted audio/video/subtitle codec ids are AVOutputFormat....
Definition: mux.h:59
rcwt_write_trailer
static int rcwt_write_trailer(AVFormatContext *avf)
Definition: rcwtenc.c:152
FFOutputFormat::p
AVOutputFormat p
The public AVOutputFormat.
Definition: mux.h:65
avio_wl16
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:436
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:853
pkt
AVPacket * pkt
Definition: movenc.c:59
intreadwrite.h
RCWTContext::cluster_buf
uint8_t cluster_buf[RCWT_CLUSTER_MAX_BLOCKS *RCWT_BLOCK_SIZE]
Definition: rcwtenc.c:63
RCWTContext::cluster_pts
int64_t cluster_pts
Definition: rcwtenc.c:62
rcwt_write_packet
static int rcwt_write_packet(AVFormatContext *avf, AVPacket *pkt)
Definition: rcwtenc.c:109
AVFormatContext
Format I/O context.
Definition: avformat.h:1255
internal.h
write_trailer
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:101
AVFormatContext::pb
AVIOContext * pb
I/O context.
Definition: avformat.h:1297
rcwt_write_header
static int rcwt_write_header(AVFormatContext *avf)
Definition: rcwtenc.c:85
FFOutputFormat
Definition: mux.h:61
avio_w8
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:178
AVPacket::size
int size
Definition: packet.h:523
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:106
RCWT_CLUSTER_MAX_BLOCKS
#define RCWT_CLUSTER_MAX_BLOCKS
Definition: rcwtenc.c:57
AV_NOPTS_VALUE
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:248
avio_write
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition: aviobuf.c:200
log.h
AVFMT_GLOBALHEADER
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition: avformat.h:478
AV_CODEC_ID_NONE
@ AV_CODEC_ID_NONE
Definition: codec_id.h:50
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:515
RCWTContext
Definition: rcwtenc.c:60
FF_OFMT_FLAG_MAX_ONE_OF_EACH
#define FF_OFMT_FLAG_MAX_ONE_OF_EACH
If this flag is set, it indicates that for each codec type whose corresponding default codec (i....
Definition: mux.h:50
write_packet
static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
Definition: ffmpeg_mux.c:209
AVFMT_TS_NONSTRICT
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition: avformat.h:491
avformat.h
AVPacket
This structure stores compressed data.
Definition: packet.h:499
RCWTContext::cluster_pos
int cluster_pos
Definition: rcwtenc.c:61
avio_wb16
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:442
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
rcwt_flush_cluster
static void rcwt_flush_cluster(AVFormatContext *avf)
Definition: rcwtenc.c:72
write_header
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:346
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1283
rcwt_init_cluster
static void rcwt_init_cluster(RCWTContext *rcwt)
Definition: rcwtenc.c:66
mux.h