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  *
25  * This muxer implements the specification as of March 2024, which has
26  * been stable and unchanged since April 2014.
27  *
28  * This muxer will have some nuances from the way that ccextractor muxes RCWT.
29  * No compatibility issues when processing the output with ccextractor
30  * have been observed as a result of this so far, but mileage may vary
31  * and outputs will not be a bit-exact match.
32  *
33  * Specifically, the differences are:
34  * (1) This muxer will identify as "FF" as the writing program identifier, so
35  * as to be honest about the output's origin.
36  *
37  * (2) This muxer will not alter the extracted data except to remove invalid
38  * packets in between valid CC blocks. On the other hand, ccextractor
39  * will by default remove mid-stream padding, and add padding at the end
40  * of the stream (in order to convey the end time of the source video).
41  *
42  * A free specification of RCWT can be found here:
43  * @url{https://github.com/CCExtractor/ccextractor/blob/master/docs/BINARY_FILE_FORMAT.TXT}
44  */
45 
46 #include "avformat.h"
47 #include "internal.h"
48 #include "mux.h"
49 #include "libavutil/log.h"
50 #include "libavutil/intreadwrite.h"
51 
52 #define RCWT_CLUSTER_MAX_BLOCKS 65535
53 #define RCWT_BLOCK_SIZE 3
54 
55 typedef struct RCWTContext {
59 } RCWTContext;
60 
61 static void rcwt_init_cluster(RCWTContext *rcwt)
62 {
63  rcwt->cluster_pos = 0;
65 }
66 
68 {
69  RCWTContext *rcwt = avf->priv_data;
70 
71  if (rcwt->cluster_pos > 0) {
72  avio_wl64(avf->pb, rcwt->cluster_pts);
73  avio_wl16(avf->pb, rcwt->cluster_pos / RCWT_BLOCK_SIZE);
74  avio_write(avf->pb, rcwt->cluster_buf, rcwt->cluster_pos);
75  }
76 
77  rcwt_init_cluster(rcwt);
78 }
79 
81 {
82  avpriv_set_pts_info(avf->streams[0], 64, 1, 1000);
83 
84  /* magic number */
85  avio_wb16(avf->pb, 0xCCCC);
86  avio_w8(avf->pb, 0xED);
87 
88  /* program version (identify as ffmpeg) */
89  avio_wb16(avf->pb, 0xFF00);
90  avio_w8(avf->pb, 0x60);
91 
92  /* format version, only version 0.001 supported for now */
93  avio_wb16(avf->pb, 0x0001);
94 
95  /* reserved */
96  avio_wb16(avf->pb, 0x000);
97  avio_w8(avf->pb, 0x00);
98 
100 
101  return 0;
102 }
103 
105 {
106  RCWTContext *rcwt = avf->priv_data;
107 
108  if (pkt->size < RCWT_BLOCK_SIZE)
109  return 0;
110 
111  /* new PTS, new cluster */
112  if (pkt->pts != rcwt->cluster_pts) {
113  rcwt_flush_cluster(avf);
114  rcwt->cluster_pts = pkt->pts;
115  }
116 
117  if (pkt->pts == AV_NOPTS_VALUE) {
118  av_log(avf, AV_LOG_WARNING, "Ignoring CC packet with no PTS\n");
119  return 0;
120  }
121 
122  for (int i = 0; i <= pkt->size - RCWT_BLOCK_SIZE;) {
123  uint8_t cc_valid;
124  uint8_t cc_type;
125 
127  av_log(avf, AV_LOG_WARNING, "Starting new cluster due to size\n");
128  rcwt_flush_cluster(avf);
129  }
130 
131  cc_valid = (pkt->data[i] & 0x04) >> 2;
132  cc_type = pkt->data[i] & 0x03;
133 
134  if (!(cc_valid || cc_type == 3)) {
135  i++;
136  continue;
137  }
138 
139  memcpy(&rcwt->cluster_buf[rcwt->cluster_pos], &pkt->data[i], 3);
140  rcwt->cluster_pos += 3;
141  i += 3;
142  }
143 
144  return 0;
145 }
146 
148 {
149  rcwt_flush_cluster(avf);
150 
151  return 0;
152 }
153 
155  .p.name = "rcwt",
156  .p.long_name = NULL_IF_CONFIG_SMALL("RCWT (Raw Captions With Time)"),
158  .p.video_codec = AV_CODEC_ID_NONE,
159  .p.audio_codec = AV_CODEC_ID_NONE,
160  .p.subtitle_codec = AV_CODEC_ID_EIA_608,
161  .flags_internal = FF_OFMT_FLAG_MAX_ONE_OF_EACH |
163  .priv_data_size = sizeof(RCWTContext),
167 };
AV_CODEC_ID_EIA_608
@ AV_CODEC_ID_EIA_608
Definition: codec_id.h:566
AV_LOG_WARNING
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition: log.h:215
AVOutputFormat::name
const char * name
Definition: avformat.h:510
RCWT_BLOCK_SIZE
#define RCWT_BLOCK_SIZE
Definition: rcwtenc.c:53
ff_rcwt_muxer
const FFOutputFormat ff_rcwt_muxer
Definition: rcwtenc.c:154
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:1368
AVPacket::data
uint8_t * data
Definition: packet.h:539
avio_wl64
void avio_wl64(AVIOContext *s, uint64_t val)
Definition: aviobuf.c:425
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:147
FFOutputFormat::p
AVOutputFormat p
The public AVOutputFormat.
Definition: mux.h:65
avio_wl16
void avio_wl16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:437
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
pkt
AVPacket * pkt
Definition: movenc.c:60
intreadwrite.h
RCWTContext::cluster_buf
uint8_t cluster_buf[RCWT_CLUSTER_MAX_BLOCKS *RCWT_BLOCK_SIZE]
Definition: rcwtenc.c:58
RCWTContext::cluster_pts
int64_t cluster_pts
Definition: rcwtenc.c:57
rcwt_write_packet
static int rcwt_write_packet(AVFormatContext *avf, AVPacket *pkt)
Definition: rcwtenc.c:104
AVFormatContext
Format I/O context.
Definition: avformat.h:1300
internal.h
write_trailer
static int write_trailer(AVFormatContext *s1)
Definition: v4l2enc.c:101
AVFormatContext::pb
AVIOContext * pb
I/O context.
Definition: avformat.h:1342
rcwt_write_header
static int rcwt_write_header(AVFormatContext *avf)
Definition: rcwtenc.c:80
FFOutputFormat
Definition: mux.h:61
avio_w8
void avio_w8(AVIOContext *s, int b)
Definition: aviobuf.c:179
AVPacket::size
int size
Definition: packet.h:540
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
RCWT_CLUSTER_MAX_BLOCKS
#define RCWT_CLUSTER_MAX_BLOCKS
Definition: rcwtenc.c:52
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:201
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:256
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
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:516
RCWTContext::cluster_pos
int cluster_pos
Definition: rcwtenc.c:56
avio_wb16
void avio_wb16(AVIOContext *s, unsigned int val)
Definition: aviobuf.c:443
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
rcwt_flush_cluster
static void rcwt_flush_cluster(AVFormatContext *avf)
Definition: rcwtenc.c:67
write_header
static void write_header(FFV1Context *f)
Definition: ffv1enc.c:348
AVFormatContext::priv_data
void * priv_data
Format private data.
Definition: avformat.h:1328
rcwt_init_cluster
static void rcwt_init_cluster(RCWTContext *rcwt)
Definition: rcwtenc.c:61
mux.h