FFmpeg
Loading...
Searching...
No Matches
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"
51
52#define RCWT_CLUSTER_MAX_BLOCKS 65535
53#define RCWT_BLOCK_SIZE 3
54
55typedef struct RCWTContext {
60
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);
74 avio_write(avf->pb, rcwt->cluster_buf, rcwt->cluster_pos);
75 }
76
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) {
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");
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{
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};
const FFOutputFormat ff_rcwt_muxer
Definition rcwtenc.c:154
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:834
Main libavformat public API header.
#define AVFMT_VARIABLE_FPS
Format allows variable fps.
Definition avformat.h:501
#define AVFMT_TS_NONSTRICT
Format does not require strictly increasing timestamps, but they must still be monotonic.
Definition avformat.h:507
#define AVFMT_GLOBALHEADER
Format wants global header.
Definition avformat.h:497
void avio_wl64(AVIOContext *s, uint64_t val)
Definition aviobuf.c:428
void avio_wl16(AVIOContext *s, unsigned int val)
Definition aviobuf.c:440
void avio_w8(AVIOContext *s, int b)
Definition aviobuf.c:184
void avio_wb16(AVIOContext *s, unsigned int val)
Definition aviobuf.c:446
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
Definition aviobuf.c:206
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
long long int64_t
Definition coverity.c:34
static AVPacket * pkt
static int write_packet(Muxer *mux, OutputStream *ost, AVPacket *pkt)
Definition ffmpeg_mux.c:204
static void write_header(FFV1Context *f)
Definition ffv1enc.c:384
@ AV_CODEC_ID_NONE
Definition codec_id.h:48
@ AV_CODEC_ID_EIA_608
Definition codec_id.h:576
#define AV_LOG_WARNING
Something somehow does not look correct.
Definition log.h:216
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition avutil.h:247
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification.
Definition internal.h:88
#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
#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
#define RCWT_CLUSTER_MAX_BLOCKS
Definition rcwtenc.c:52
#define RCWT_BLOCK_SIZE
Definition rcwtenc.c:53
static int rcwt_write_header(AVFormatContext *avf)
Definition rcwtenc.c:80
static int rcwt_write_trailer(AVFormatContext *avf)
Definition rcwtenc.c:147
static void rcwt_flush_cluster(AVFormatContext *avf)
Definition rcwtenc.c:67
static int rcwt_write_packet(AVFormatContext *avf, AVPacket *pkt)
Definition rcwtenc.c:104
static void rcwt_init_cluster(RCWTContext *rcwt)
Definition rcwtenc.c:61
Format I/O context.
Definition avformat.h:1333
AVIOContext * pb
I/O context.
Definition avformat.h:1375
void * priv_data
Format private data.
Definition avformat.h:1361
AVStream ** streams
A list of all streams in the file.
Definition avformat.h:1401
This structure stores compressed data.
Definition packet.h:580
int cluster_pos
Definition rcwtenc.c:56
int64_t cluster_pts
Definition rcwtenc.c:57
uint8_t cluster_buf[RCWT_CLUSTER_MAX_BLOCKS *RCWT_BLOCK_SIZE]
Definition rcwtenc.c:58
#define av_log(a,...)
static int write_trailer(AVFormatContext *s1)
Definition v4l2enc.c:101