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
56
60 int length_code;
///< length code in bits or bytes, depending on data type
61 int pkt_offset;
///< data burst repetition period in bytes
64
67
68 int use_preamble;
///< preamble enabled (disabled for exactly pre-padded DTS)
69 int extra_bswap;
///< extra bswap for payload (for LE DTS => standard BE DTS)
70
75
76 int dtshd_skip;
///< counter used for skipping DTS-HD frames
77
78 /* AVOptions: */
81 #define SPDIF_FLAG_BIGENDIAN 0x01
83
84 /// function, which generates codec dependent header information.
85 /// Sets data_type and pkt_offset, and length_code, out_bytes, out_buf if necessary
88
90 {
"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" },
92 {
"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 },
93 {
"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 },
94 { NULL },
95 };
96
102 };
103
105 {
107 int bitstream_mode = pkt->
data[5] & 0x7;
108
111 return 0;
112 }
113
115 {
117 static const uint8_t eac3_repeat[4] = {6, 3, 2, 1};
118 int repeat = 1;
119
120 if ((pkt->
data[4] & 0xc0) != 0xc0)
/* fscod */
121 repeat = eac3_repeat[(pkt->
data[4] & 0x30) >> 4];
/* numblkscod */
122
126
128
132 return 0;
133 }
139
142 return 0;
143 }
144
145 /*
146 * DTS type IV (DTS-HD) can be transmitted with various frame repetition
147 * periods; longer repetition periods allow for longer packets and therefore
148 * higher bitrate. Longer repetition periods mean that the constant bitrate of
149 * the outputted IEC 61937 stream is higher.
150 * The repetition period is measured in IEC 60958 frames (4 bytes).
151 */
153 {
154 switch (period) {
155 case 512: return 0x0;
156 case 1024: return 0x1;
157 case 2048: return 0x2;
158 case 4096: return 0x3;
159 case 8192: return 0x4;
160 case 16384: return 0x5;
161 }
162 return -1;
163 }
164
167 {
169 static const char dtshd_start_code[10] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xfe };
170 int pkt_size = pkt->
size;
171 int period;
172 int subtype;
173
174 if (!core_size) {
177 }
178
179 if (!sample_rate) {
182 }
183
184 period = ctx->
dtshd_rate * (blocks << 5) / sample_rate;
186
187 if (subtype < 0) {
189 "impossible repetition period of %d for the current DTS stream"
190 " (blocks = %d, sample rate = %d)\n", ctx->
dtshd_rate, period,
191 blocks << 5, sample_rate);
193 }
194
195 /* set pkt_offset and DTS IV subtype according to the requested output
196 * rate */
199
200 /* If the bitrate is too high for transmitting at the selected
201 * repetition period setting, strip DTS-HD until a good amount
202 * of consecutive non-overflowing HD frames have been observed.
203 * This generally only happens if the caller is cramming a Master
204 * Audio stream into 192kHz IEC 60958 (which may or may not fit). */
205 if (sizeof(dtshd_start_code) + 2 + pkt_size
209 "temporarily sending core only\n");
212 else
213 /* skip permanently (dtshd_fallback == -1) or just once
214 * (dtshd_fallback == 0) */
216 }
218 pkt_size = core_size;
221 }
222
223 ctx->
out_bytes =
sizeof(dtshd_start_code) + 2 + pkt_size;
224
225 /* Align so that (length_code & 0xf) == 0x8. This is reportedly needed
226 * with some receivers, but the exact requirement is unconfirmed. */
228
232
234
235 memcpy(ctx->
hd_buf, dtshd_start_code,
sizeof(dtshd_start_code));
237 memcpy(ctx->
hd_buf +
sizeof(dtshd_start_code) + 2, pkt->
data, pkt_size);
238
239 return 0;
240 }
241
243 {
246 int blocks;
248 int core_size = 0;
249
252
253 switch (syncword_dts) {
256 core_size = ((
AV_RB24(pkt->
data + 5) >> 4) & 0x3fff) + 1;
258 break;
262 break;
264 blocks =
265 (((pkt->
data[5] & 0x07) << 4) | ((pkt->
data[6] & 0x3f) >> 2));
266 break;
268 blocks =
269 (((pkt->
data[4] & 0x07) << 4) | ((pkt->
data[7] & 0x3f) >> 2));
271 break;
273 /* We only handle HD frames that are paired with core. However,
274 sometimes DTS-HD streams with core have a stray HD frame without
275 core in the beginning of the stream. */
278 default:
281 }
282 blocks++;
283
285 /* DTS type IV output requested */
287
288 switch (blocks) {
292 default:
294 blocks << 5);
296 }
297
298 /* discard extraneous data by default */
299 if (core_size && core_size < pkt->
size) {
302 }
303
305
307 /* The DTS stream fits exactly into the output stream, so skip the
308 * preamble as it would not fit in there. This is the case for dts
309 * discs and dts-in-wav. */
313 /* This will fail with a "bitrate too high" in the caller */
314 }
315
316 return 0;
317 }
318
320 // LAYER1 LAYER2 LAYER3
323 };
324
326 {
329 int layer = 3 - ((pkt->
data[1] >> 1) & 3);
330 int extension = pkt->
data[2] & 1;
331
332 if (layer == 3 || version == 1) {
335 }
336 av_log(s,
AV_LOG_DEBUG,
"version: %i layer: %i extension: %i\n", version, layer, extension);
337 if (version == 2 && extension) {
340 } else {
343 }
344 // TODO Data type dependent info (normal/karaoke, dynamic range control)
345 return 0;
346 }
347
349 {
354
357 if (ret < 0) {
360 }
361
364 case 1:
366 break;
367 case 2:
369 break;
370 case 4:
372 break;
373 default:
375 "%"PRIu32
" samples in AAC frame not supported\n", hdr.
samples);
377 }
378 //TODO Data type dependent info (LC profile/SBR)
379 return 0;
380 }
381
382
383 /*
384 * It seems Dolby TrueHD frames have to be encapsulated in MAT frames before
385 * they can be encapsulated in IEC 61937.
386 * Here we encapsulate 24 TrueHD frames in a single MAT frame, padding them
387 * to achieve constant rate.
388 * The actual format of a MAT frame is unknown, but the below seems to work.
389 * However, it seems it is not actually necessary for the 24 TrueHD frames to
390 * be in an exact alignment with the MAT frame.
391 */
392 #define MAT_FRAME_SIZE 61424
393 #define TRUEHD_FRAME_OFFSET 2560
394 #define MAT_MIDDLE_CODE_OFFSET -4
395
397 {
399 int mat_code_length = 0;
400 static const char mat_end_code[16] = { 0xC3, 0xC2, 0xC0, 0xC4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x97, 0x11 };
401
403 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 };
405 memcpy(ctx->
hd_buf, mat_start_code,
sizeof(mat_start_code));
406
408 static const char mat_middle_code[12] = { 0xC3, 0xC1, 0x42, 0x49, 0x3B, 0xFA, 0x82, 0x83, 0x49, 0x80, 0x77, 0xE0 };
411 mat_middle_code, sizeof(mat_middle_code));
412 }
413
415 /* if such frames exist, we'd need some more complex logic to
416 * distribute the TrueHD frames in the MAT frame */
420 }
421
426
429 return 0;
430 }
431 memcpy(&ctx->
hd_buf[
MAT_FRAME_SIZE -
sizeof(mat_end_code)], mat_end_code,
sizeof(mat_end_code));
433
439 return 0;
440 }
441
443 {
445
449 break;
452 break;
457 break;
460 break;
463 break;
469 break;
470 default:
473 }
474 return 0;
475 }
476
478 {
482 return 0;
483 }
484
487 {
490 else
492 }
493
495 {
498
504
506 if (ret < 0)
509 return 0;
510
512 if (padding < 0) {
515 }
516
522 }
523
526 } else {
532 }
533
534 /* a final lone byte has to be MSB aligned */
537
539
542
543 return 0;
544 }
545
549 .extensions = "spdif",
557 .priv_class = &spdif_class,
558 };