1 /*
2 * IEC 61937 muxer
3 * Copyright (c) 2009 Bartlomiej Wolowiec
4 * Copyright (c) 2010, 2020 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-preamble - IEC61937 frame header, contains 16-bit 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
58
62 int length_code;
///< length code in bits or bytes, depending on data type
63 int pkt_offset;
///< data burst repetition period in bytes
64 uint8_t *
buffer;
///< allocated buffer, used for swap bytes
66
67 const uint8_t *
out_buf;
///< pointer to the outgoing data before byte-swapping
69
70 int use_preamble;
///< preamble enabled (disabled for exactly pre-padded DTS)
71 int extra_bswap;
///< extra bswap for payload (for LE DTS => standard BE DTS)
72
73 uint8_t *
hd_buf[2];
///< allocated buffers to concatenate hd audio frames
74 int hd_buf_size;
///< size of the hd audio buffer (eac3, dts4)
75 int hd_buf_count;
///< number of frames in the hd audio buffer (eac3)
76 int hd_buf_filled;
///< amount of bytes in the hd audio buffer (eac3, truehd)
78
79 int dtshd_skip;
///< counter used for skipping DTS-HD frames
80
84
85 /* AVOptions: */
88 #define SPDIF_FLAG_BIGENDIAN 0x01
90
91 /// function, which generates codec dependent header information.
92 /// Sets data_type and pkt_offset, and length_code, out_bytes, out_buf if necessary
95
97 {
"spdif_flags",
"IEC 61937 encapsulation flags", offsetof(
IEC61937Context, spdif_flags),
AV_OPT_TYPE_FLAGS, {.i64 = 0}, 0, INT_MAX,
AV_OPT_FLAG_ENCODING_PARAM, .unit =
"spdif_flags" },
99 {
"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 },
100 {
"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 },
102 };
103
109 };
110
112 {
114 int bitstream_mode =
pkt->
data[5] & 0x7;
115
118 return 0;
119 }
120
122 {
124 static const uint8_t eac3_repeat[4] = {6, 3, 2, 1};
125 int repeat = 1;
127
129 if (bsid > 10 && (
pkt->
data[4] & 0xc0) != 0xc0)
/* fscod */
130 repeat = eac3_repeat[(
pkt->
data[4] & 0x30) >> 4];
/* numblkscod */
131
136
138
140 if (++
ctx->hd_buf_count < repeat){
142 return 0;
143 }
145 ctx->pkt_offset = 24576;
146 ctx->out_buf =
ctx->hd_buf[0];
147 ctx->out_bytes =
ctx->hd_buf_filled;
148 ctx->length_code =
ctx->hd_buf_filled;
149
150 ctx->hd_buf_count = 0;
151 ctx->hd_buf_filled = 0;
152 return 0;
153 }
154
155 /*
156 * DTS type IV (DTS-HD) can be transmitted with various frame repetition
157 * periods; longer repetition periods allow for longer packets and therefore
158 * higher bitrate. Longer repetition periods mean that the constant bitrate of
159 * the output IEC 61937 stream is higher.
160 * The repetition period is measured in IEC 60958 frames (4 bytes).
161 */
163 {
165 case 512: return 0x0;
166 case 1024: return 0x1;
167 case 2048: return 0x2;
168 case 4096: return 0x3;
169 case 8192: return 0x4;
170 case 16384: return 0x5;
171 }
172 return -1;
173 }
174
177 {
179 static const char dtshd_start_code[10] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xfe };
182 int subtype;
183
184 if (!core_size) {
187 }
188
192 }
193
196
197 if (subtype < 0) {
199 "impossible repetition period of %d for the current DTS stream"
200 " (blocks = %d, sample rate = %d)\n",
ctx->dtshd_rate,
period,
203 }
204
205 /* set pkt_offset and DTS IV subtype according to the requested output
206 * rate */
209
210 /* If the bitrate is too high for transmitting at the selected
211 * repetition period setting, strip DTS-HD until a good amount
212 * of consecutive non-overflowing HD frames have been observed.
213 * This generally only happens if the caller is cramming a Master
214 * Audio stream into 192kHz IEC 60958 (which may or may not fit). */
215 if (sizeof(dtshd_start_code) + 2 + pkt_size
217 if (!
ctx->dtshd_skip)
219 "temporarily sending core only\n");
220 if (
ctx->dtshd_fallback > 0)
222 else
223 /* skip permanently (dtshd_fallback == -1) or just once
224 * (dtshd_fallback == 0) */
226 }
227 if (
ctx->dtshd_skip && core_size) {
228 pkt_size = core_size;
229 if (
ctx->dtshd_fallback >= 0)
231 }
232
233 ctx->out_bytes =
sizeof(dtshd_start_code) + 2 + pkt_size;
234
235 /* Align so that (length_code & 0xf) == 0x8. This is reportedly needed
236 * with some receivers, but the exact requirement is unconfirmed. */
237 ctx->length_code =
FFALIGN(
ctx->out_bytes + 0x8, 0x10) - 0x8;
238
242
243 ctx->out_buf =
ctx->hd_buf[0];
244
245 memcpy(
ctx->hd_buf[0], dtshd_start_code,
sizeof(dtshd_start_code));
246 AV_WB16(
ctx->hd_buf[0] +
sizeof(dtshd_start_code), pkt_size);
247 memcpy(
ctx->hd_buf[0] +
sizeof(dtshd_start_code) + 2,
pkt->
data, pkt_size);
248
249 return 0;
250 }
251
253 {
256 int blocks;
258 int core_size = 0;
259
262
263 switch (syncword_dts) {
268 break;
271 ctx->extra_bswap = 1;
272 break;
274 blocks =
276 break;
278 blocks =
280 ctx->extra_bswap = 1;
281 break;
283 /* We only handle HD frames that are paired with core. However,
284 sometimes DTS-HD streams with core have a stray HD frame without
285 core in the beginning of the stream. */
288 default:
291 }
292 blocks++;
293
295 /* DTS type IV output requested */
297
298 switch (blocks) {
302 default:
304 blocks << 5);
306 }
307
308 /* discard extraneous data by default */
309 if (core_size && core_size < pkt->
size) {
310 ctx->out_bytes = core_size;
311 ctx->length_code = core_size << 3;
312 }
313
314 ctx->pkt_offset = blocks << 7;
315
316 if (
ctx->out_bytes ==
ctx->pkt_offset) {
317 /* The DTS stream fits exactly into the output stream, so skip the
318 * preamble as it would not fit in there. This is the case for dts
319 * discs and dts-in-wav. */
320 ctx->use_preamble = 0;
323 /* This will fail with a "bitrate too high" in the caller */
324 }
325
326 return 0;
327 }
328
330 // LAYER1 LAYER2 LAYER3
333 };
334
336 {
339 int layer = 3 - ((
pkt->
data[1] >> 1) & 3);
340 int extension =
pkt->
data[2] & 1;
341
342 if (layer == 3 ||
version == 1) {
345 }
347 if (
version == 2 && extension) {
349 ctx->pkt_offset = 4608;
350 } else {
353 }
354 // TODO Data type dependent info (normal/karaoke, dynamic range control)
355 return 0;
356 }
357
359 {
364
369 }
370
373 case 1:
375 break;
376 case 2:
378 break;
379 case 4:
381 break;
382 default:
384 "%"PRIu32
" samples in AAC frame not supported\n",
samples);
386 }
387 //TODO Data type dependent info (LC profile/SBR)
388 return 0;
389 }
390
391
392 /*
393 * It seems Dolby TrueHD frames have to be encapsulated in MAT frames before
394 * they can be encapsulated in IEC 61937.
395 */
396 #define MAT_PKT_OFFSET 61440
397 #define MAT_FRAME_SIZE 61424
398
400 0x07, 0x9E, 0x00, 0x03, 0x84, 0x01, 0x01, 0x01, 0x80, 0x00, 0x56, 0xA5, 0x3B, 0xF4, 0x81, 0x83,
401 0x49, 0x80, 0x77, 0xE0,
402 };
404 0xC3, 0xC1, 0x42, 0x49, 0x3B, 0xFA, 0x82, 0x83, 0x49, 0x80, 0x77, 0xE0,
405 };
407 0xC3, 0xC2, 0xC0, 0xC4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x97, 0x11,
408 };
409
410 #define MAT_CODE(position, data) { .pos = position, .code = data, .len = sizeof(data) }
411
412 static const struct {
420 };
421
423 {
425 uint8_t *hd_buf =
ctx->hd_buf[
ctx->hd_buf_idx];
426 int ratebits;
427 int padding_remaining = 0;
428 uint16_t input_timing;
429 int total_frame_size =
pkt->
size;
430 const uint8_t *dataptr =
pkt->
data;
431 int data_remaining =
pkt->
size;
432 int have_pkt = 0;
433 int next_code_idx;
434
437
439 /* major sync unit, fetch sample rate */
444 else
446
447 ctx->truehd_samples_per_frame = 40 << (ratebits & 3);
449 ctx->truehd_samples_per_frame);
450 }
451
452 if (!
ctx->truehd_samples_per_frame)
454
456 if (
ctx->truehd_prev_size) {
457 uint16_t delta_samples = input_timing -
ctx->truehd_prev_time;
458 /*
459 * One multiple-of-48kHz frame is 1/1200 sec and the IEC 61937 rate
460 * is 768kHz = 768000*4 bytes/sec.
461 * The nominal space per frame is therefore
462 * (768000*4 bytes/sec) * (1/1200 sec) = 2560 bytes.
463 * For multiple-of-44.1kHz frames: 1/1102.5 sec, 705.6kHz, 2560 bytes.
464 *
465 * 2560 is divisible by truehd_samples_per_frame.
466 */
467 int delta_bytes = delta_samples * 2560 /
ctx->truehd_samples_per_frame;
468
469 /* padding needed before this frame */
470 padding_remaining = delta_bytes -
ctx->truehd_prev_size;
471
473 delta_samples, delta_bytes);
474
475 /* sanity check */
476 if (padding_remaining < 0 || padding_remaining >=
MAT_FRAME_SIZE / 2) {
478 ctx->truehd_prev_time, input_timing,
ctx->truehd_samples_per_frame);
479 padding_remaining = 0;
480 }
481 }
482
485 break;
486
489
490 while (padding_remaining || data_remaining ||
492
494 /* time to insert MAT code */
495 int code_len =
mat_codes[next_code_idx].len;
496 int code_len_remaining = code_len;
499 ctx->hd_buf_filled += code_len;
500
501 next_code_idx++;
503 next_code_idx = 0;
504
505 /* this was the last code, move to the next MAT frame */
506 have_pkt = 1;
507 ctx->out_buf = hd_buf;
508 ctx->hd_buf_idx ^= 1;
509 hd_buf =
ctx->hd_buf[
ctx->hd_buf_idx];
510 ctx->hd_buf_filled = 0;
511
512 /* inter-frame gap has to be counted as well, add it */
514 }
515
516 if (padding_remaining) {
517 /* consider the MAT code as padding */
518 int counted_as_padding =
FFMIN(padding_remaining,
519 code_len_remaining);
520 padding_remaining -= counted_as_padding;
521 code_len_remaining -= counted_as_padding;
522 }
523 /* count the remainder of the code as part of frame size */
524 if (code_len_remaining)
525 total_frame_size += code_len_remaining;
526 }
527
528 if (padding_remaining) {
530 padding_remaining);
531
532 memset(hd_buf +
ctx->hd_buf_filled, 0, padding_to_insert);
533 ctx->hd_buf_filled += padding_to_insert;
534 padding_remaining -= padding_to_insert;
535
536 if (padding_remaining)
537 continue; /* time to insert MAT code */
538 }
539
540 if (data_remaining) {
542 data_remaining);
543
544 memcpy(hd_buf +
ctx->hd_buf_filled, dataptr, data_to_insert);
545 ctx->hd_buf_filled += data_to_insert;
546 dataptr += data_to_insert;
547 data_remaining -= data_to_insert;
548 }
549 }
550
551 ctx->truehd_prev_size = total_frame_size;
552 ctx->truehd_prev_time = input_timing;
553
555 total_frame_size,
ctx->hd_buf_filled);
556
557 if (!have_pkt) {
559 return 0;
560 }
561
566 return 0;
567 }
568
570 {
572
573 switch (
s->streams[0]->codecpar->codec_id) {
576 break;
579 break;
584 break;
587 break;
590 break;
598 }
599 break;
600 default:
602 s->streams[0]->codecpar->codec_id);
604 }
605 return 0;
606 }
607
609 {
614 }
615
618 {
621 else
623 }
624
626 {
629
633 ctx->use_preamble = 1;
634 ctx->extra_bswap = 0;
635
639 if (!
ctx->pkt_offset)
640 return 0;
641
643 if (padding < 0) {
646 }
647
648 if (
ctx->use_preamble) {
653 }
654
657 } else {
663 }
664
665 /* a final lone byte has to be MSB aligned */
666 if (
ctx->out_bytes & 1)
668
670
672 ctx->data_type,
ctx->out_bytes,
ctx->pkt_offset);
673
674 return 0;
675 }
676
680 .p.extensions = "spdif",
691 };