1 /*
2 * IEC 61937 muxer
3 * Copyright (c) 2009 Bartlomiej Wolowiec
4 * Copyright (c) 2010 Anssi Hannula
5 * Copyright (c) 2010 Carl Eugen Hoyos
6 *
7 * This file is part of FFmpeg.
8 *
9 * FFmpeg is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
13 *
14 * FFmpeg is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
18 *
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with FFmpeg; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22 */
23
24 /**
25 * @file
26 * IEC-61937 encapsulation of various formats, used by S/PDIF
27 * @author Bartlomiej Wolowiec
28 * @author Anssi Hannula
29 * @author Carl Eugen Hoyos
30 */
31
32 /*
33 * Terminology used in specification:
34 * data-burst - IEC61937 frame, contains header and encapsuled frame
35 * burst-preambule - IEC61937 frame header, contains 16-bits words named Pa, Pb, Pc and Pd
36 * burst-payload - encapsuled frame
37 * Pa, Pb - syncword - 0xF872, 0x4E1F
38 * Pc - burst-info, contains data-type (bits 0-6), error flag (bit 7), data-type-dependent info (bits 8-12)
39 * and bitstream number (bits 13-15)
40 * data-type - determines type of encapsuled frames
41 * Pd - length code (number of bits or bytes of encapsuled frame - according to data_type)
42 *
43 * IEC 61937 frames at normal usage start every specific count of bytes,
44 * dependent from data-type (spaces between packets are filled by zeros)
45 */
46
54
58 int length_code;
///< length code in bits or bytes, depending on data type
59 int pkt_offset;
///< data burst repetition period in bytes
62
65
66 int use_preamble;
///< preamble enabled (disabled for exactly pre-padded DTS)
67 int extra_bswap;
///< extra bswap for payload (for LE DTS => standard BE DTS)
68
73
74 int dtshd_skip;
///< counter used for skipping DTS-HD frames
75
76 /* AVOptions: */
79 #define SPDIF_FLAG_BIGENDIAN 0x01
81
82 /// function, which generates codec dependent header information.
83 /// Sets data_type and pkt_offset, and length_code, out_bytes, out_buf if necessary
86
88 {
"spdif_flags",
"IEC 61937 encapsulation flags", offsetof(
IEC61937Context, spdif_flags),
AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX,
AV_OPT_FLAG_ENCODING_PARAM,
"spdif_flags" },
90 {
"dtshd_rate",
"mux complete DTS frames in HD mode at the specified IEC958 rate (in Hz, default 0=disabled)", offsetof(
IEC61937Context, dtshd_rate),
AV_OPT_TYPE_INT, {.i64 = 0}, 0, 768000,
AV_OPT_FLAG_ENCODING_PARAM },
91 {
"dtshd_fallback_time",
"min secs to strip HD for after an overflow (-1: till the end, default 60)", offsetof(
IEC61937Context, dtshd_fallback),
AV_OPT_TYPE_INT, {.i64 = 60}, -1, INT_MAX,
AV_OPT_FLAG_ENCODING_PARAM },
92 { NULL },
93 };
94
100 };
101
103 {
105 int bitstream_mode = pkt->
data[5] & 0x7;
106
109 return 0;
110 }
111
113 {
115 static const uint8_t eac3_repeat[4] = {6, 3, 2, 1};
116 int repeat = 1;
117
118 if ((pkt->
data[4] & 0xc0) != 0xc0)
/* fscod */
119 repeat = eac3_repeat[(pkt->
data[4] & 0x30) >> 4];
/* numblkscod */
120
124
126
130 return 0;
131 }
137
140 return 0;
141 }
142
143 /*
144 * DTS type IV (DTS-HD) can be transmitted with various frame repetition
145 * periods; longer repetition periods allow for longer packets and therefore
146 * higher bitrate. Longer repetition periods mean that the constant bitrate of
147 * the outputted IEC 61937 stream is higher.
148 * The repetition period is measured in IEC 60958 frames (4 bytes).
149 */
151 {
152 switch (period) {
153 case 512: return 0x0;
154 case 1024: return 0x1;
155 case 2048: return 0x2;
156 case 4096: return 0x3;
157 case 8192: return 0x4;
158 case 16384: return 0x5;
159 }
160 return -1;
161 }
162
165 {
167 static const char dtshd_start_code[10] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xfe };
168 int pkt_size = pkt->
size;
169 int period;
170 int subtype;
171
172 if (!core_size) {
175 }
176
177 if (!sample_rate) {
180 }
181
182 period = ctx->
dtshd_rate * (blocks << 5) / sample_rate;
184
185 if (subtype < 0) {
187 "impossible repetition period of %d for the current DTS stream"
188 " (blocks = %d, sample rate = %d)\n", ctx->
dtshd_rate, period,
189 blocks << 5, sample_rate);
191 }
192
193 /* set pkt_offset and DTS IV subtype according to the requested output
194 * rate */
197
198 /* If the bitrate is too high for transmitting at the selected
199 * repetition period setting, strip DTS-HD until a good amount
200 * of consecutive non-overflowing HD frames have been observed.
201 * This generally only happens if the caller is cramming a Master
202 * Audio stream into 192kHz IEC 60958 (which may or may not fit). */
203 if (sizeof(dtshd_start_code) + 2 + pkt_size
207 "temporarily sending core only\n");
210 else
211 /* skip permanently (dtshd_fallback == -1) or just once
212 * (dtshd_fallback == 0) */
214 }
216 pkt_size = core_size;
219 }
220
221 ctx->
out_bytes =
sizeof(dtshd_start_code) + 2 + pkt_size;
222
223 /* Align so that (length_code & 0xf) == 0x8. This is reportedly needed
224 * with some receivers, but the exact requirement is unconfirmed. */
226
230
232
233 memcpy(ctx->
hd_buf, dtshd_start_code,
sizeof(dtshd_start_code));
235 memcpy(ctx->
hd_buf +
sizeof(dtshd_start_code) + 2, pkt->
data, pkt_size);
236
237 return 0;
238 }
239
241 {
244 int blocks;
246 int core_size = 0;
247
250
251 switch (syncword_dts) {
254 core_size = ((
AV_RB24(pkt->
data + 5) >> 4) & 0x3fff) + 1;
256 break;
260 break;
262 blocks =
263 (((pkt->
data[5] & 0x07) << 4) | ((pkt->
data[6] & 0x3f) >> 2));
264 break;
266 blocks =
267 (((pkt->
data[4] & 0x07) << 4) | ((pkt->
data[7] & 0x3f) >> 2));
269 break;
271 /* We only handle HD frames that are paired with core. However,
272 sometimes DTS-HD streams with core have a stray HD frame without
273 core in the beginning of the stream. */
276 default:
279 }
280 blocks++;
281
283 /* DTS type IV output requested */
285
286 switch (blocks) {
290 default:
292 blocks << 5);
294 }
295
296 /* discard extraneous data by default */
297 if (core_size && core_size < pkt->
size) {
300 }
301
303
305 /* The DTS stream fits exactly into the output stream, so skip the
306 * preamble as it would not fit in there. This is the case for dts
307 * discs and dts-in-wav. */
311 /* This will fail with a "bitrate too high" in the caller */
312 }
313
314 return 0;
315 }
316
318 // LAYER1 LAYER2 LAYER3
321 };
322
324 {
327 int layer = 3 - ((pkt->
data[1] >> 1) & 3);
328 int extension = pkt->
data[2] & 1;
329
330 if (layer == 3 || version == 1) {
333 }
334 av_log(s,
AV_LOG_DEBUG,
"version: %i layer: %i extension: %i\n", version, layer, extension);
335 if (version == 2 && extension) {
338 } else {
341 }
342 // TODO Data type dependent info (normal/karaoke, dynamic range control)
343 return 0;
344 }
345
347 {
352
355 if (ret < 0) {
358 }
359
362 case 1:
364 break;
365 case 2:
367 break;
368 case 4:
370 break;
371 default:
375 }
376 //TODO Data type dependent info (LC profile/SBR)
377 return 0;
378 }
379
380
381 /*
382 * It seems Dolby TrueHD frames have to be encapsulated in MAT frames before
383 * they can be encapsulated in IEC 61937.
384 * Here we encapsulate 24 TrueHD frames in a single MAT frame, padding them
385 * to achieve constant rate.
386 * The actual format of a MAT frame is unknown, but the below seems to work.
387 * However, it seems it is not actually necessary for the 24 TrueHD frames to
388 * be in an exact alignment with the MAT frame.
389 */
390 #define MAT_FRAME_SIZE 61424
391 #define TRUEHD_FRAME_OFFSET 2560
392 #define MAT_MIDDLE_CODE_OFFSET -4
393
395 {
397 int mat_code_length = 0;
398 const char mat_end_code[16] = { 0xC3, 0xC2, 0xC0, 0xC4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x97, 0x11 };
399
401 const char mat_start_code[20] = { 0x07, 0x9E, 0x00, 0x03, 0x84, 0x01, 0x01, 0x01, 0x80, 0x00, 0x56, 0xA5, 0x3B, 0xF4, 0x81, 0x83, 0x49, 0x80, 0x77, 0xE0 };
403 memcpy(ctx->
hd_buf, mat_start_code,
sizeof(mat_start_code));
404
406 const char mat_middle_code[12] = { 0xC3, 0xC1, 0x42, 0x49, 0x3B, 0xFA, 0x82, 0x83, 0x49, 0x80, 0x77, 0xE0 };
409 mat_middle_code, sizeof(mat_middle_code));
410 }
411
413 /* if such frames exist, we'd need some more complex logic to
414 * distribute the TrueHD frames in the MAT frame */
418 }
419
424
427 return 0;
428 }
429 memcpy(&ctx->
hd_buf[
MAT_FRAME_SIZE -
sizeof(mat_end_code)], mat_end_code,
sizeof(mat_end_code));
431
437 return 0;
438 }
439
441 {
443
447 break;
450 break;
455 break;
458 break;
461 break;
467 break;
468 default:
471 }
472 return 0;
473 }
474
476 {
480 return 0;
481 }
482
485 {
488 else
490 }
491
493 {
496
502
504 if (ret < 0)
507 return 0;
508
510 if (padding < 0) {
513 }
514
520 }
521
524 } else {
530 }
531
532 /* a final lone byte has to be MSB aligned */
535
537
540
541 return 0;
542 }
543
547 .extensions = "spdif",
555 .priv_class = &spdif_class,
556 };