FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavformat/mccdec.c
Date: 2025-08-19 23:55:23
Exec Total Coverage
Lines: 150 191 78.5%
Functions: 10 11 90.9%
Branches: 74 114 64.9%

Line Branch Exec Source
1 /*
2 * MCC subtitle demuxer
3 * Copyright (c) 2020 Paul B Mahol
4 * Copyright (c) 2025 Jacob Lifshay
5 *
6 * This file is part of FFmpeg.
7 *
8 * FFmpeg is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * FFmpeg is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with FFmpeg; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include "avformat.h"
24 #include "demux.h"
25 #include "internal.h"
26 #include "libavcodec/bytestream.h"
27 #include "libavcodec/codec_id.h"
28 #include "libavcodec/smpte_436m.h"
29 #include "libavutil/avstring.h"
30 #include "libavutil/avutil.h"
31 #include "libavutil/error.h"
32 #include "libavutil/internal.h"
33 #include "libavutil/log.h"
34 #include "libavutil/macros.h"
35 #include "libavutil/opt.h"
36 #include "libavutil/rational.h"
37 #include "libavutil/timecode.h"
38 #include "subtitles.h"
39 #include <inttypes.h>
40 #include <stdbool.h>
41 #include <string.h>
42
43 typedef struct MCCContext {
44 const AVClass *class;
45 int eia608_extract;
46 FFDemuxSubtitlesQueue q;
47 } MCCContext;
48
49 7260 static int mcc_probe(const AVProbeData *p)
50 {
51 char buf[28];
52 FFTextReader tr;
53
54 7260 ff_text_init_buf(&tr, p->buf, p->buf_size);
55
56
4/4
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 7263 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 7260 times.
7267 while (ff_text_peek_r8(&tr) == '\r' || ff_text_peek_r8(&tr) == '\n')
57 7 ff_text_r8(&tr);
58
59 7260 ff_text_read(&tr, buf, sizeof(buf));
60
61
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 7260 times.
7260 if (!memcmp(buf, "File Format=MacCaption_MCC V", 28))
62 return AVPROBE_SCORE_MAX;
63
64 7260 return 0;
65 }
66
67 4864 static int convert(uint8_t x)
68 {
69
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4864 times.
4864 if (x >= 'a')
70 x -= 87;
71
2/2
✓ Branch 0 taken 1900 times.
✓ Branch 1 taken 2964 times.
4864 else if (x >= 'A')
72 1900 x -= 55;
73 else
74 2964 x -= '0';
75 4864 return x;
76 }
77
78 typedef struct alias {
79 uint8_t key;
80 int len;
81 const char *value;
82 } alias;
83
84 #define CCPAD "\xFA\x0\x0"
85 #define CCPAD3 CCPAD CCPAD CCPAD
86
87 static const char cc_pad[27] = CCPAD3 CCPAD3 CCPAD3;
88
89 static const alias aliases[20] = {
90 // clang-format off
91 { .key = 16, .len = 3, .value = cc_pad, },
92 { .key = 17, .len = 6, .value = cc_pad, },
93 { .key = 18, .len = 9, .value = cc_pad, },
94 { .key = 19, .len = 12, .value = cc_pad, },
95 { .key = 20, .len = 15, .value = cc_pad, },
96 { .key = 21, .len = 18, .value = cc_pad, },
97 { .key = 22, .len = 21, .value = cc_pad, },
98 { .key = 23, .len = 24, .value = cc_pad, },
99 { .key = 24, .len = 27, .value = cc_pad, },
100 { .key = 25, .len = 3, .value = "\xFB\x80\x80", },
101 { .key = 26, .len = 3, .value = "\xFC\x80\x80", },
102 { .key = 27, .len = 3, .value = "\xFD\x80\x80", },
103 { .key = 28, .len = 2, .value = "\x96\x69", },
104 { .key = 29, .len = 2, .value = "\x61\x01", },
105 { .key = 30, .len = 3, .value = "\xFC\x80\x80", },
106 { .key = 31, .len = 3, .value = "\xFC\x80\x80", },
107 { .key = 32, .len = 4, .value = "\xE1\x00\x00\x00", },
108 { .key = 33, .len = 0, .value = NULL, },
109 { .key = 34, .len = 0, .value = NULL, },
110 { .key = 35, .len = 1, .value = "\x0", },
111 // clang-format on
112 };
113
114 typedef struct TimeTracker {
115 int64_t last_ts;
116 int64_t twenty_four_hr;
117 AVTimecode timecode;
118 } TimeTracker;
119
120 8 static int time_tracker_init(TimeTracker *tt, AVStream *st, AVRational rate, void *log_ctx)
121 {
122 8 *tt = (TimeTracker){ .last_ts = 0 };
123 8 int ret = av_timecode_init(&tt->timecode, rate, rate.den == 1001 ? AV_TIMECODE_FLAG_DROPFRAME : 0, 0, log_ctx);
124
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (ret < 0)
125 return ret;
126 // wrap pts values at 24hr ourselves since they can be bigger than fits in an int
127 AVTimecode twenty_four_hr;
128 8 ret = av_timecode_init_from_components(&twenty_four_hr, rate, tt->timecode.flags, 24, 0, 0, 0, log_ctx);
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (ret < 0)
130 return ret;
131 8 tt->twenty_four_hr = twenty_four_hr.start;
132 // timecode uses reciprocal of timebase
133 8 avpriv_set_pts_info(st, 64, rate.den, rate.num);
134 8 return 0;
135 }
136
137 typedef struct MCCTimecode {
138 unsigned hh, mm, ss, ff, field, line_number;
139 } MCCTimecode;
140
141 192 static int time_tracker_set_time(TimeTracker *tt, const MCCTimecode *tc, void *log_ctx)
142 {
143 192 AVTimecode last = tt->timecode;
144 192 int ret = av_timecode_init_from_components(&tt->timecode, last.rate, last.flags, tc->hh, tc->mm, tc->ss, tc->ff, log_ctx);
145
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 192 times.
192 if (ret < 0) {
146 tt->timecode = last;
147 return ret;
148 }
149 192 tt->last_ts -= last.start;
150 192 tt->last_ts += tt->timecode.start;
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 192 times.
192 if (tt->timecode.start < last.start)
152 tt->last_ts += tt->twenty_four_hr;
153 192 return 0;
154 }
155
156 struct ValidTimeCodeRate {
157 AVRational rate;
158 const char *str;
159 };
160
161 static struct ValidTimeCodeRate valid_time_code_rates[] = {
162 { .rate = { .num = 24, .den = 1 }, .str = "24" },
163 { .rate = { .num = 25, .den = 1 }, .str = "25" },
164 { .rate = { .num = 30000, .den = 1001 }, .str = "30DF" },
165 { .rate = { .num = 30, .den = 1 }, .str = "30" },
166 { .rate = { .num = 50, .den = 1 }, .str = "50" },
167 { .rate = { .num = 60000, .den = 1001 }, .str = "60DF" },
168 { .rate = { .num = 60, .den = 1 }, .str = "60" },
169 };
170
171 4 static int parse_time_code_rate(AVFormatContext *s, AVStream *st, TimeTracker *tt, const char *time_code_rate)
172 {
173
1/2
✓ Branch 0 taken 12 times.
✗ Branch 1 not taken.
12 for (size_t i = 0; i < FF_ARRAY_ELEMS(valid_time_code_rates); i++) {
174 const char *after;
175
2/2
✓ Branch 1 taken 4 times.
✓ Branch 2 taken 8 times.
12 if (av_stristart(time_code_rate, valid_time_code_rates[i].str, &after) != 0) {
176 4 bool bad_after = false;
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 for (; *after; after++) {
178 if (!av_isspace(*after)) {
179 bad_after = true;
180 break;
181 }
182 }
183
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (bad_after)
184 continue;
185 4 return time_tracker_init(tt, st, valid_time_code_rates[i].rate, s);
186 }
187 }
188 av_log(s, AV_LOG_FATAL, "invalid mcc time code rate: %s", time_code_rate);
189 return AVERROR_INVALIDDATA;
190 }
191
192 896 static int mcc_parse_time_code_part(char **line_left, unsigned *value, unsigned max, const char *after_set)
193 {
194 896 *value = 0;
195
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 896 times.
896 if (!av_isdigit(**line_left))
196 return AVERROR_INVALIDDATA;
197
2/2
✓ Branch 0 taken 1728 times.
✓ Branch 1 taken 896 times.
2624 while (av_isdigit(**line_left)) {
198 1728 unsigned digit = **line_left - '0';
199 1728 *value = *value * 10 + digit;
200 1728 ++*line_left;
201
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1728 times.
1728 if (*value > max)
202 return AVERROR_INVALIDDATA;
203 }
204 896 unsigned char after = **line_left;
205
2/4
✓ Branch 0 taken 896 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 896 times.
896 if (!after || !strchr(after_set, after))
206 return AVERROR_INVALIDDATA;
207 896 ++*line_left;
208 896 return after;
209 }
210
211 192 static int mcc_parse_time_code(char **line_left, MCCTimecode *tc)
212 {
213 192 *tc = (MCCTimecode){ .field = 0, .line_number = 9 };
214 192 int ret = mcc_parse_time_code_part(line_left, &tc->hh, 23, ":");
215
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 192 times.
192 if (ret < 0)
216 return ret;
217 192 ret = mcc_parse_time_code_part(line_left, &tc->mm, 59, ":");
218
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 192 times.
192 if (ret < 0)
219 return ret;
220 192 ret = mcc_parse_time_code_part(line_left, &tc->ss, 59, ":;");
221
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 192 times.
192 if (ret < 0)
222 return ret;
223 192 ret = mcc_parse_time_code_part(line_left, &tc->ff, 59, ".\t");
224
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 192 times.
192 if (ret < 0)
225 return ret;
226
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 128 times.
192 if (ret == '.') {
227 64 ret = mcc_parse_time_code_part(line_left, &tc->field, 1, ",\t");
228
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
64 if (ret < 0)
229 return ret;
230
1/2
✓ Branch 0 taken 64 times.
✗ Branch 1 not taken.
64 if (ret == ',') {
231 64 ret = mcc_parse_time_code_part(line_left, &tc->line_number, 0xFFFF, "\t");
232
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
64 if (ret < 0)
233 return ret;
234 }
235 }
236
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 192 times.
192 if (ret != '\t')
237 return AVERROR_INVALIDDATA;
238 192 return 0;
239 }
240
241 4 static int mcc_read_header(AVFormatContext *s)
242 {
243 4 MCCContext *mcc = s->priv_data;
244 4 AVStream *st = avformat_new_stream(s, NULL);
245 int64_t pos;
246 4 AVSmpte436mCodedAnc coded_anc = {
247 .payload_sample_coding = AV_SMPTE_436M_PAYLOAD_SAMPLE_CODING_8BIT_LUMA,
248 };
249 char line[4096];
250 FFTextReader tr;
251 4 int ret = 0;
252
253 4 ff_text_init_avio(s, &tr, s->pb);
254
255
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (!st)
256 return AVERROR(ENOMEM);
257
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (mcc->eia608_extract) {
258 2 st->codecpar->codec_type = AVMEDIA_TYPE_SUBTITLE;
259 2 st->codecpar->codec_id = AV_CODEC_ID_EIA_608;
260 } else {
261 2 st->codecpar->codec_type = AVMEDIA_TYPE_DATA;
262 2 st->codecpar->codec_id = AV_CODEC_ID_SMPTE_436M_ANC;
263 2 av_dict_set(&st->metadata, "data_type", "vbi_vanc_smpte_436M", 0);
264 }
265
266 TimeTracker tt;
267 4 ret = time_tracker_init(&tt, st, (AVRational){ .num = 30, .den = 1 }, s);
268
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
269 return ret;
270
271
2/2
✓ Branch 1 taken 356 times.
✓ Branch 2 taken 4 times.
360 while (!ff_text_eof(&tr)) {
272 356 pos = ff_text_pos(&tr);
273 356 ff_subtitles_read_line(&tr, line, sizeof(line));
274
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 352 times.
356 if (!strncmp(line, "File Format=MacCaption_MCC V", 28))
275 356 continue;
276
2/2
✓ Branch 0 taken 140 times.
✓ Branch 1 taken 212 times.
352 if (!strncmp(line, "//", 2))
277 140 continue;
278
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 208 times.
212 if (!strncmp(line, "Time Code Rate=", 15)) {
279 4 ret = parse_time_code_rate(s, st, &tt, line + 15);
280
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
4 if (ret < 0)
281 return ret;
282 4 continue;
283 }
284
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 192 times.
208 if (strchr(line, '='))
285 16 continue; // skip attributes
286 192 char *line_left = line;
287
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 192 times.
192 while (av_isspace(*line_left))
288 line_left++;
289
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 192 times.
192 if (!*line_left) // skip empty lines
290 continue;
291 MCCTimecode tc;
292 192 ret = mcc_parse_time_code(&line_left, &tc);
293
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 192 times.
192 if (ret < 0) {
294 av_log(s, AV_LOG_ERROR, "can't parse mcc time code");
295 continue;
296 }
297
298 192 int64_t last_pts = tt.last_ts;
299 192 ret = time_tracker_set_time(&tt, &tc, s);
300
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 192 times.
192 if (ret < 0)
301 continue;
302 192 bool merge = last_pts == tt.last_ts;
303
304 192 coded_anc.line_number = tc.line_number;
305
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 160 times.
192 coded_anc.wrapping_type = tc.field ? AV_SMPTE_436M_WRAPPING_TYPE_VANC_FIELD_2 : AV_SMPTE_436M_WRAPPING_TYPE_VANC_FRAME;
306
307 PutByteContext pb;
308 192 bytestream2_init_writer(&pb, coded_anc.payload, AV_SMPTE_436M_CODED_ANC_PAYLOAD_CAPACITY);
309
310
2/2
✓ Branch 0 taken 2944 times.
✓ Branch 1 taken 192 times.
3136 while (*line_left) {
311 2944 uint8_t v = convert(*line_left++);
312
313
3/4
✓ Branch 0 taken 1024 times.
✓ Branch 1 taken 1920 times.
✓ Branch 2 taken 1024 times.
✗ Branch 3 not taken.
3968 if (v >= 16 && v <= 35) {
314 1024 int idx = v - 16;
315 1024 bytestream2_put_buffer(&pb, aliases[idx].value, aliases[idx].len);
316 } else {
317 uint8_t vv;
318
319
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1920 times.
1920 if (!*line_left)
320 break;
321 1920 vv = convert(*line_left++);
322 1920 bytestream2_put_byte(&pb, vv | (v << 4));
323 }
324 }
325
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 192 times.
192 if (pb.eof)
326 continue;
327 // remove trailing ANC checksum byte (not to be confused with the CDP checksum byte),
328 // it's not included in 8-bit sample encodings. see section 6.2 (page 14) of:
329 // https://pub.smpte.org/latest/st436/s436m-2006.pdf
330 192 bytestream2_seek_p(&pb, -1, SEEK_CUR);
331 192 coded_anc.payload_sample_count = bytestream2_tell_p(&pb);
332
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 192 times.
192 if (coded_anc.payload_sample_count == 0)
333 continue; // ignore if too small
334 // add padding to align to 4 bytes
335
3/4
✓ Branch 0 taken 256 times.
✗ Branch 1 not taken.
✓ Branch 3 taken 64 times.
✓ Branch 4 taken 192 times.
256 while (!pb.eof && bytestream2_tell_p(&pb) % 4)
336 64 bytestream2_put_byte(&pb, 0);
337
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 192 times.
192 if (pb.eof)
338 continue;
339 192 coded_anc.payload_array_length = bytestream2_tell_p(&pb);
340
341 AVPacket *sub;
342
2/2
✓ Branch 0 taken 96 times.
✓ Branch 1 taken 96 times.
192 if (mcc->eia608_extract) {
343 AVSmpte291mAnc8bit anc;
344
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
96 if (av_smpte_291m_anc_8bit_decode(
345 96 &anc, coded_anc.payload_sample_coding, coded_anc.payload_sample_count, coded_anc.payload, s)
346 < 0)
347 32 continue;
348 // reuse line
349 96 int cc_count = av_smpte_291m_anc_8bit_extract_cta_708(&anc, line, s);
350
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 64 times.
96 if (cc_count < 0) // continue if error or if it's not a closed captions packet
351 32 continue;
352 64 int len = cc_count * 3;
353
354 64 sub = ff_subtitles_queue_insert(&mcc->q, line, len, merge);
355
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 64 times.
64 if (!sub)
356 return AVERROR(ENOMEM);
357 } else {
358 96 sub = ff_subtitles_queue_insert(&mcc->q, NULL, 0, merge);
359
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
96 if (!sub)
360 return AVERROR(ENOMEM);
361
362 96 ret = av_smpte_436m_anc_append(sub, 1, &coded_anc);
363
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 96 times.
96 if (ret < 0)
364 return ret;
365 }
366
367 160 sub->pos = pos;
368 160 sub->pts = tt.last_ts;
369 160 sub->duration = 1;
370 160 continue;
371 }
372
373 4 ff_subtitles_queue_finalize(s, &mcc->q);
374
375 4 return ret;
376 }
377
378 132 static int mcc_read_packet(AVFormatContext *s, AVPacket *pkt)
379 {
380 132 MCCContext *mcc = s->priv_data;
381 132 return ff_subtitles_queue_read_packet(&mcc->q, pkt);
382 }
383
384 static int mcc_read_seek(AVFormatContext *s, int stream_index, int64_t min_ts, int64_t ts, int64_t max_ts, int flags)
385 {
386 MCCContext *mcc = s->priv_data;
387 return ff_subtitles_queue_seek(&mcc->q, s, stream_index, min_ts, ts, max_ts, flags);
388 }
389
390 4 static int mcc_read_close(AVFormatContext *s)
391 {
392 4 MCCContext *mcc = s->priv_data;
393 4 ff_subtitles_queue_clean(&mcc->q);
394 4 return 0;
395 }
396
397 #define OFFSET(x) offsetof(MCCContext, x)
398 #define SD AV_OPT_FLAG_SUBTITLE_PARAM | AV_OPT_FLAG_DECODING_PARAM
399 // clang-format off
400 static const AVOption mcc_options[] = {
401 { "eia608_extract", "extract EIA-608/708 captions from VANC packets", OFFSET(eia608_extract), AV_OPT_TYPE_BOOL, {.i64 = 1}, 0, 1, SD },
402 { NULL },
403 };
404 // clang-format on
405
406 static const AVClass mcc_class = {
407 .class_name = "mcc demuxer",
408 .item_name = av_default_item_name,
409 .option = mcc_options,
410 .version = LIBAVUTIL_VERSION_INT,
411 .category = AV_CLASS_CATEGORY_DEMUXER,
412 };
413
414 const FFInputFormat ff_mcc_demuxer = {
415 .p.name = "mcc",
416 .p.long_name = NULL_IF_CONFIG_SMALL("MacCaption"),
417 .p.extensions = "mcc",
418 .p.priv_class = &mcc_class,
419 .priv_data_size = sizeof(MCCContext),
420 .flags_internal = FF_INFMT_FLAG_INIT_CLEANUP,
421 .read_probe = mcc_probe,
422 .read_header = mcc_read_header,
423 .read_packet = mcc_read_packet,
424 .read_seek2 = mcc_read_seek,
425 .read_close = mcc_read_close,
426 };
427