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
47 #include <inttypes.h>
48
57
61 int length_code;
///< length code in bits or bytes, depending on data type
62 int pkt_offset;
///< data burst repetition period in bytes
65
68
69 int use_preamble;
///< preamble enabled (disabled for exactly pre-padded DTS)
70 int extra_bswap;
///< extra bswap for payload (for LE DTS => standard BE DTS)
71
76
77 int dtshd_skip;
///< counter used for skipping DTS-HD frames
78
79 /* AVOptions: */
82 #define SPDIF_FLAG_BIGENDIAN 0x01
84
85 /// function, which generates codec dependent header information.
86 /// Sets data_type and pkt_offset, and length_code, out_bytes, out_buf if necessary
89
91 {
"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" },
93 {
"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 },
94 {
"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 },
96 };
97
103 };
104
106 {
108 int bitstream_mode = pkt->
data[5] & 0x7;
109
112 return 0;
113 }
114
116 {
118 static const uint8_t eac3_repeat[4] = {6, 3, 2, 1};
119 int repeat = 1;
120
121 if ((pkt->
data[4] & 0xc0) != 0xc0)
/* fscod */
122 repeat = eac3_repeat[(pkt->
data[4] & 0x30) >> 4];
/* numblkscod */
123
127
129
133 return 0;
134 }
140
143 return 0;
144 }
145
146 /*
147 * DTS type IV (DTS-HD) can be transmitted with various frame repetition
148 * periods; longer repetition periods allow for longer packets and therefore
149 * higher bitrate. Longer repetition periods mean that the constant bitrate of
150 * the outputted IEC 61937 stream is higher.
151 * The repetition period is measured in IEC 60958 frames (4 bytes).
152 */
154 {
155 switch (period) {
156 case 512: return 0x0;
157 case 1024: return 0x1;
158 case 2048: return 0x2;
159 case 4096: return 0x3;
160 case 8192: return 0x4;
161 case 16384: return 0x5;
162 }
163 return -1;
164 }
165
168 {
170 static const char dtshd_start_code[10] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xfe };
171 int pkt_size = pkt->
size;
172 int period;
173 int subtype;
174
175 if (!core_size) {
178 }
179
180 if (!sample_rate) {
183 }
184
185 period = ctx->
dtshd_rate * (blocks << 5) / sample_rate;
187
188 if (subtype < 0) {
190 "impossible repetition period of %d for the current DTS stream"
191 " (blocks = %d, sample rate = %d)\n", ctx->
dtshd_rate, period,
192 blocks << 5, sample_rate);
194 }
195
196 /* set pkt_offset and DTS IV subtype according to the requested output
197 * rate */
200
201 /* If the bitrate is too high for transmitting at the selected
202 * repetition period setting, strip DTS-HD until a good amount
203 * of consecutive non-overflowing HD frames have been observed.
204 * This generally only happens if the caller is cramming a Master
205 * Audio stream into 192kHz IEC 60958 (which may or may not fit). */
206 if (sizeof(dtshd_start_code) + 2 + pkt_size
210 "temporarily sending core only\n");
213 else
214 /* skip permanently (dtshd_fallback == -1) or just once
215 * (dtshd_fallback == 0) */
217 }
219 pkt_size = core_size;
222 }
223
224 ctx->
out_bytes =
sizeof(dtshd_start_code) + 2 + pkt_size;
225
226 /* Align so that (length_code & 0xf) == 0x8. This is reportedly needed
227 * with some receivers, but the exact requirement is unconfirmed. */
229
233
235
236 memcpy(ctx->
hd_buf, dtshd_start_code,
sizeof(dtshd_start_code));
238 memcpy(ctx->
hd_buf +
sizeof(dtshd_start_code) + 2, pkt->
data, pkt_size);
239
240 return 0;
241 }
242
244 {
247 int blocks;
249 int core_size = 0;
250
253
254 switch (syncword_dts) {
257 core_size = ((
AV_RB24(pkt->
data + 5) >> 4) & 0x3fff) + 1;
259 break;
263 break;
265 blocks =
266 (((pkt->
data[5] & 0x07) << 4) | ((pkt->
data[6] & 0x3f) >> 2));
267 break;
269 blocks =
270 (((pkt->
data[4] & 0x07) << 4) | ((pkt->
data[7] & 0x3f) >> 2));
272 break;
274 /* We only handle HD frames that are paired with core. However,
275 sometimes DTS-HD streams with core have a stray HD frame without
276 core in the beginning of the stream. */
279 default:
282 }
283 blocks++;
284
286 /* DTS type IV output requested */
288
289 switch (blocks) {
293 default:
295 blocks << 5);
297 }
298
299 /* discard extraneous data by default */
300 if (core_size && core_size < pkt->
size) {
303 }
304
306
308 /* The DTS stream fits exactly into the output stream, so skip the
309 * preamble as it would not fit in there. This is the case for dts
310 * discs and dts-in-wav. */
314 /* This will fail with a "bitrate too high" in the caller */
315 }
316
317 return 0;
318 }
319
321 // LAYER1 LAYER2 LAYER3
324 };
325
327 {
330 int layer = 3 - ((pkt->
data[1] >> 1) & 3);
331 int extension = pkt->
data[2] & 1;
332
333 if (layer == 3 || version == 1) {
336 }
337 av_log(s,
AV_LOG_DEBUG,
"version: %i layer: %i extension: %i\n", version, layer, extension);
338 if (version == 2 && extension) {
341 } else {
344 }
345 // TODO Data type dependent info (normal/karaoke, dynamic range control)
346 return 0;
347 }
348
350 {
355
358 if (ret < 0) {
361 }
362
365 case 1:
367 break;
368 case 2:
370 break;
371 case 4:
373 break;
374 default:
376 "%"PRIu32
" samples in AAC frame not supported\n", hdr.
samples);
378 }
379 //TODO Data type dependent info (LC profile/SBR)
380 return 0;
381 }
382
383
384 /*
385 * It seems Dolby TrueHD frames have to be encapsulated in MAT frames before
386 * they can be encapsulated in IEC 61937.
387 * Here we encapsulate 24 TrueHD frames in a single MAT frame, padding them
388 * to achieve constant rate.
389 * The actual format of a MAT frame is unknown, but the below seems to work.
390 * However, it seems it is not actually necessary for the 24 TrueHD frames to
391 * be in an exact alignment with the MAT frame.
392 */
393 #define MAT_FRAME_SIZE 61424
394 #define TRUEHD_FRAME_OFFSET 2560
395 #define MAT_MIDDLE_CODE_OFFSET -4
396
398 {
400 int mat_code_length = 0;
401 static const char mat_end_code[16] = { 0xC3, 0xC2, 0xC0, 0xC4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x97, 0x11 };
402
404 static 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 };
406 memcpy(ctx->
hd_buf, mat_start_code,
sizeof(mat_start_code));
407
409 static const char mat_middle_code[12] = { 0xC3, 0xC1, 0x42, 0x49, 0x3B, 0xFA, 0x82, 0x83, 0x49, 0x80, 0x77, 0xE0 };
412 mat_middle_code, sizeof(mat_middle_code));
413 }
414
416 /* if such frames exist, we'd need some more complex logic to
417 * distribute the TrueHD frames in the MAT frame */
421 }
422
427
430 return 0;
431 }
432 memcpy(&ctx->
hd_buf[
MAT_FRAME_SIZE -
sizeof(mat_end_code)], mat_end_code,
sizeof(mat_end_code));
434
440 return 0;
441 }
442
444 {
446
450 break;
453 break;
458 break;
461 break;
464 break;
470 break;
471 default:
474 }
475 return 0;
476 }
477
479 {
483 return 0;
484 }
485
488 {
491 else
493 }
494
496 {
499
505
507 if (ret < 0)
510 return 0;
511
513 if (padding < 0) {
516 }
517
523 }
524
527 } else {
533 }
534
535 /* a final lone byte has to be MSB aligned */
538
540
543
544 return 0;
545 }
546
550 .extensions = "spdif",
558 .priv_class = &spdif_class,
559 };