1 /*
2 * MCC subtitle muxer
3 * Copyright (c) 2025 Jacob Lifshay
4 * Copyright (c) 2017 Paul B Mahol
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
27
30
41
52
54 {
60
62 "File Format=MacCaption_MCC V%c.0\n" \
63 "\n" \
64 "///////////////////////////////////////////////////////////////////////////////////\n" \
65 "// Computer Prompting and Captioning Company\n" \
66 "// Ancillary Data Packet Transfer File\n" \
67 "//\n" \
68 "// Permission to generate this format is granted provided that\n" \
69 "// 1. This ANC Transfer file format is used on an as-is basis and no warranty is given, and\n" \
70 "// 2. This entire descriptive information text is included in a generated .mcc file.\n" \
71 "//\n" \
72 "// General file format:\n" \
73 "// HH:MM:SS:FF(tab)[Hexadecimal ANC data in groups of 2 characters]\n" \
74 "// Hexadecimal data starts with the Ancillary Data Packet DID (Data ID defined in S291M)\n" \
75 "// and concludes with the Check Sum following the User Data Words.\n" \
76 "// Each time code line must contain at most one complete ancillary data packet.\n" \
77 "// To transfer additional ANC Data successive lines may contain identical time code.\n" \
78 "// Time Code Rate=[24, 25, 30, 30DF, 50, 60%s]\n" \
79 "//\n" \
80 "// ANC data bytes may be represented by one ASCII character according to the following schema:\n" \
81 "// G FAh 00h 00h\n" \
82 "// H 2 x (FAh 00h 00h)\n" \
83 "// I 3 x (FAh 00h 00h)\n" \
84 "// J 4 x (FAh 00h 00h)\n" \
85 "// K 5 x (FAh 00h 00h)\n" \
86 "// L 6 x (FAh 00h 00h)\n" \
87 "// M 7 x (FAh 00h 00h)\n" \
88 "// N 8 x (FAh 00h 00h)\n" \
89 "// O 9 x (FAh 00h 00h)\n" \
90 "// P FBh 80h 80h\n" \
91 "// Q FCh 80h 80h\n" \
92 "// R FDh 80h 80h\n" \
93 "// S 96h 69h\n" \
94 "// T 61h 01h\n" \
95 "// U E1h 00h 00h 00h\n" \
96 "// Z 00h\n" \
97 "//\n" \
98 "///////////////////////////////////////////////////////////////////////////////////\n"
99
100 #define MCC_HEADER_PRINTF_ARGS(mcc_version) (mcc_version) + '0', \
101 (mcc_version) == MCC_VERSION_1 ? "" : ", 60DF"
102
103 /**
104 * generated with the bash command:
105 * ```bash
106 * URL="https://code.ffmpeg.org/FFmpeg/FFmpeg/src/branch/master/libavformat/mccenc.c"
107 * python3 -c "from uuid import *; print(str(uuid5(NAMESPACE_URL, '$URL')).upper())"
108 * ```
109 */
111
113 { .
num = 24, .den = 1 },
114 { .num = 25, .den = 1 },
115 { .num = 30000, .den = 1001 },
116 { .num = 30, .den = 1 },
117 { .num = 50, .den = 1 },
118 { .num = 60000, .den = 1001 },
119 { .num = 60, .den = 1 },
120 };
121
123 {
126 if (!creation_program) {
128 creation_program = "Lavf";
129 else
131 } else if (strchr(creation_program, '\n')) {
132 av_log(avf,
AV_LOG_FATAL,
"creation_program must not contain multiple lines of text\n");
134 }
142 }
143 struct tm tm;
144 if (!
localtime_r((time_t[1]){ timeval / 1000000 }, &tm))
146 // we can't rely on having the C locale, so convert the date/time to a string ourselves:
147 static const char months[12][10] = {
148 "January",
149 "February",
150 "March",
151 "April",
152 "May",
153 "June",
154 "July",
155 "August",
156 "September",
157 "October",
158 "November",
159 "December",
160 };
161 // assert that values are sane so we don't index out of bounds
163 const char *month =
months[tm.tm_mon];
164
165 static const char weekdays[7][10] = {
166 "Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"
167 };
168 // assert that values are sane so we don't index out of bounds
170 const char *weekday = weekdays[tm.tm_wday];
171
174 "UUID=%s\n"
175 "Creation Program=%s\n"
176 "Creation Date=%s, %s %d, %d\n"
177 "Creation Time=%02d:%02d:%02d\n"
178 "Time Code Rate=%u%s\n\n",
181 creation_program,
182 weekday,
183 month,
184 tm.tm_mday,
185 tm.tm_year + 1900,
186 tm.tm_hour,
187 tm.tm_min,
188 tm.tm_sec,
191
192 return 0;
193 }
194
195 /// convert the input bytes to hexadecimal with mcc's aliases
196 static void mcc_bytes_to_hex(
char *dest,
const uint8_t *bytes,
size_t bytes_size,
int use_u_alias)
197 {
198 while (bytes_size != 0) {
199 switch (bytes[0]) {
200 case 0xFA:
201 *dest = '0円';
202 for (
unsigned char code =
'G';
code <= (
unsigned char)
'O';
code++) {
203 if (bytes_size < 3)
204 break;
205 if (bytes[0] != 0xFA || bytes[1] != 0 || bytes[2] != 0)
206 break;
208 bytes += 3;
209 bytes_size -= 3;
210 }
211 if (*dest) {
212 dest++;
213 continue;
214 }
215 break;
216 case 0xFB:
217 case 0xFC:
218 case 0xFD:
219 if (bytes_size >= 3 && bytes[1] == 0x80 && bytes[2] == 0x80) {
220 *dest++ = bytes[0] - 0xFB + 'P';
221 bytes += 3;
222 bytes_size -= 3;
223 continue;
224 }
225 break;
226 case 0x96:
227 if (bytes_size >= 2 && bytes[1] == 0x69) {
228 *dest++ = 'S';
229 bytes += 2;
230 bytes_size -= 2;
231 continue;
232 }
233 break;
234 case 0x61:
235 if (bytes_size >= 2 && bytes[1] == 0x01) {
236 *dest++ = 'T';
237 bytes += 2;
238 bytes_size -= 2;
239 continue;
240 }
241 break;
242 case 0xE1:
243 if (use_u_alias && bytes_size >= 4 && bytes[1] == 0 && bytes[2] == 0 && bytes[3] == 0) {
244 *dest++ = 'U';
245 bytes += 4;
246 bytes_size -= 4;
247 continue;
248 }
249 break;
250 case 0:
251 *dest++ = 'Z';
252 bytes++;
253 bytes_size--;
254 continue;
255 default:
256 // any other bytes falls through to writing hex
257 break;
258 }
260 int v = (bytes[0] >>
shift) & 0xF;
261 if (v < 0xA)
262 *dest++ = v + '0';
263 else
264 *dest++ = v - 0xA + 'A';
265 }
266 bytes++;
267 bytes_size--;
268 }
269 *dest = '0円';
270 }
271
273 {
277
280 return 0;
281 }
282
284
285 // wrap pts values at 24hr ourselves since they can be bigger than fits in an int
287
288 for (
char *
p = timecode_str; *
p;
p++) {
289 // .mcc doesn't use ; for drop-frame time codes
292 }
293
305 // 4 for did, sdid_or_dbn, data_count, and checksum fields.
307 size_t mcc_anc_len = 0;
308
309 mcc_anc[mcc_anc_len++] = anc.
did;
314 mcc_anc[mcc_anc_len++] = anc.
checksum;
315
316 unsigned field_number;
321 field_number = 0;
322 break;
324 field_number = 1;
325 break;
326 default:
329 "Unsupported SMPTE 436M ANC Wrapping Type %#x -- discarding ANC packet\n",
331 continue;
332 }
333
334 char field_and_line[32] = "";
336 snprintf(field_and_line,
sizeof(field_and_line),
".%u,%u", field_number, (
unsigned)coded_anc.
line_number);
337 } else if (field_number != 0) {
338 snprintf(field_and_line,
sizeof(field_and_line),
".%u", field_number);
339 }
340
343 if (field_and_line[0] != '0円') {
346 "MCC Version 1.0 doesn't support ANC packets where the field number (got %u) isn't 0 and "
347 "line number (got %u) isn't 9: discarding ANC packet\n",
348 field_number,
350 continue;
351 }
352 break;
354 break;
355 }
356
357 // 1 for terminating nul. 2 since there's 2 hex digits per byte.
358 char hex[1 + 2 * sizeof(mcc_anc)];
360 avio_printf(avf->
pb,
"%s%s\t%s\n", timecode_str, field_and_line, hex);
361 }
364 return 0;
365 }
366
368 {
371
375 }
376
379 int timecode_flags = 0;
381
384
386
390 break;
391 }
392 }
393
396 av_log(avf,
AV_LOG_FATAL,
"time code rate not set, you need to use -override_time_code_rate to set it\n");
397 } else {
400 "time code rate not supported by mcc: %d/%d\n",
403 }
405 }
406
408
409 if (time_code_rate.
den == 1001 && time_code_rate.
num % 30000 == 0) {
411 }
412
416
419 av_log(avf,
AV_LOG_FATAL,
"MCC Version 1.0 doesn't support 60DF (59.94 fps drop-frame)\n");
421 }
422 }
423
424 // get av_timecode to calculate how many frames are in 24hr
428
430
432 char args[64];
433 snprintf(args,
sizeof(args),
"cdp_frame_rate=%d/%d", time_code_rate.
num, time_code_rate.
den);
440 "mcc muxer supports only codec smpte_436m_anc or codec eia_608\n");
442 }
443
444 return 0;
445 }
446
448 {
449 (void)std_compliance;
451 return 1;
452 return 0;
453 }
454
455 #define OFFSET(x) offsetof(MCCContext, x)
456 #define ENC AV_OPT_FLAG_ENCODING_PARAM
457 // clang-format off
459 {
"override_time_code_rate",
"override the `Time Code Rate` value in the output",
OFFSET(override_time_code_rate),
AV_OPT_TYPE_STRING, { .str =
NULL }, 0, INT_MAX,
ENC },
460 {
"use_u_alias",
"use the U alias for E1h 00h 00h 00h, disabled by default because some .mcc files disagree on whether it has 2 or 3 zero bytes",
OFFSET(use_u_alias),
AV_OPT_TYPE_BOOL, { .i64 = 0 }, 0, 1,
ENC },
465 };
466 // clang-format on
467
473 };
474
478 .p.extensions = "mcc",
489 };