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-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
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 int bsid = pkt->
data[5] >> 3;
122 if (bsid > 10 && (pkt->
data[4] & 0xc0) != 0xc0)
/* fscod */
123 repeat = eac3_repeat[(pkt->
data[4] & 0x30) >> 4];
/* numblkscod */
124
128
130
134 return 0;
135 }
141
144 return 0;
145 }
146
147 /*
148 * DTS type IV (DTS-HD) can be transmitted with various frame repetition
149 * periods; longer repetition periods allow for longer packets and therefore
150 * higher bitrate. Longer repetition periods mean that the constant bitrate of
151 * the output IEC 61937 stream is higher.
152 * The repetition period is measured in IEC 60958 frames (4 bytes).
153 */
155 {
156 switch (period) {
157 case 512: return 0x0;
158 case 1024: return 0x1;
159 case 2048: return 0x2;
160 case 4096: return 0x3;
161 case 8192: return 0x4;
162 case 16384: return 0x5;
163 }
164 return -1;
165 }
166
169 {
171 static const char dtshd_start_code[10] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xfe };
172 int pkt_size = pkt->
size;
173 int period;
174 int subtype;
175
176 if (!core_size) {
179 }
180
181 if (!sample_rate) {
184 }
185
186 period = ctx->
dtshd_rate * (blocks << 5) / sample_rate;
188
189 if (subtype < 0) {
191 "impossible repetition period of %d for the current DTS stream"
192 " (blocks = %d, sample rate = %d)\n", ctx->
dtshd_rate, period,
193 blocks << 5, sample_rate);
195 }
196
197 /* set pkt_offset and DTS IV subtype according to the requested output
198 * rate */
201
202 /* If the bitrate is too high for transmitting at the selected
203 * repetition period setting, strip DTS-HD until a good amount
204 * of consecutive non-overflowing HD frames have been observed.
205 * This generally only happens if the caller is cramming a Master
206 * Audio stream into 192kHz IEC 60958 (which may or may not fit). */
207 if (sizeof(dtshd_start_code) + 2 + pkt_size
211 "temporarily sending core only\n");
214 else
215 /* skip permanently (dtshd_fallback == -1) or just once
216 * (dtshd_fallback == 0) */
218 }
220 pkt_size = core_size;
223 }
224
225 ctx->
out_bytes =
sizeof(dtshd_start_code) + 2 + pkt_size;
226
227 /* Align so that (length_code & 0xf) == 0x8. This is reportedly needed
228 * with some receivers, but the exact requirement is unconfirmed. */
230
234
236
237 memcpy(ctx->
hd_buf, dtshd_start_code,
sizeof(dtshd_start_code));
239 memcpy(ctx->
hd_buf +
sizeof(dtshd_start_code) + 2, pkt->
data, pkt_size);
240
241 return 0;
242 }
243
245 {
248 int blocks;
250 int core_size = 0;
251
254
255 switch (syncword_dts) {
258 core_size = ((
AV_RB24(pkt->
data + 5) >> 4) & 0x3fff) + 1;
260 break;
264 break;
266 blocks =
267 (((pkt->
data[5] & 0x07) << 4) | ((pkt->
data[6] & 0x3f) >> 2));
268 break;
270 blocks =
271 (((pkt->
data[4] & 0x07) << 4) | ((pkt->
data[7] & 0x3f) >> 2));
273 break;
275 /* We only handle HD frames that are paired with core. However,
276 sometimes DTS-HD streams with core have a stray HD frame without
277 core in the beginning of the stream. */
280 default:
283 }
284 blocks++;
285
287 /* DTS type IV output requested */
289
290 switch (blocks) {
294 default:
296 blocks << 5);
298 }
299
300 /* discard extraneous data by default */
301 if (core_size && core_size < pkt->
size) {
304 }
305
307
309 /* The DTS stream fits exactly into the output stream, so skip the
310 * preamble as it would not fit in there. This is the case for dts
311 * discs and dts-in-wav. */
315 /* This will fail with a "bitrate too high" in the caller */
316 }
317
318 return 0;
319 }
320
322 // LAYER1 LAYER2 LAYER3
325 };
326
328 {
331 int layer = 3 - ((pkt->
data[1] >> 1) & 3);
332 int extension = pkt->
data[2] & 1;
333
334 if (layer == 3 || version == 1) {
337 }
338 av_log(s,
AV_LOG_DEBUG,
"version: %i layer: %i extension: %i\n", version, layer, extension);
339 if (version == 2 && extension) {
342 } else {
345 }
346 // TODO Data type dependent info (normal/karaoke, dynamic range control)
347 return 0;
348 }
349
351 {
353 uint32_t samples;
355 int ret;
356
358 if (ret < 0) {
360 return ret;
361 }
362
364 switch (frames) {
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", 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;
471 break;
472 default:
476 }
477 return 0;
478 }
479
481 {
485 return 0;
486 }
487
490 {
493 else
495 }
496
498 {
500 int ret, padding;
501
507
509 if (ret < 0)
510 return ret;
512 return 0;
513
515 if (padding < 0) {
518 }
519
525 }
526
529 } else {
535 }
536
537 /* a final lone byte has to be MSB aligned */
540
542
545
546 return 0;
547 }
548
552 .extensions = "spdif",
560 .priv_class = &spdif_class,
561 };
MPEG-2 AAC ADTS half-rate low sampling frequency.
uint8_t * out_buf
pointer to the outgoing data before byte-swapping
static void write_packet(OutputFile *of, AVPacket *pkt, OutputStream *ost, int unqueue)
const char const char void * val
void avio_wl16(AVIOContext *s, unsigned int val)
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
int pkt_offset
data burst repetition period in bytes
static int spdif_header_mpeg(AVFormatContext *s, AVPacket *pkt)
#define AV_LOG_WARNING
Something somehow does not look correct.
#define LIBAVUTIL_VERSION_INT
#define BURST_HEADER_SIZE
MPEG-2, layer-1 low sampling frequency.
static int spdif_dts4_subtype(int period)
enum AVCodecID codec_id
Specific type of the encoded data (the codec used).
const char * av_default_item_name(void *ptr)
Return the context name.
static int spdif_write_packet(struct AVFormatContext *s, AVPacket *pkt)
static int spdif_header_dts(AVFormatContext *s, AVPacket *pkt)
void ff_spdif_bswap_buf16(uint16_t *dst, const uint16_t *src, int w)
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_WB24 unsigned int_TMPL AV_RB16
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_RL16
static int spdif_header_eac3(AVFormatContext *s, AVPacket *pkt)
int buffer_size
size of allocated buffer
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
int av_adts_header_parse(const uint8_t *buf, uint32_t *samples, uint8_t *frames)
Extract the number of samples and frames from AAC data.
static const AVClass spdif_class
void void avpriv_request_sample(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
#define DCA_SYNCWORD_CORE_14B_BE
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
AVStream ** streams
A list of all streams in the file.
AVOutputFormat ff_spdif_muxer
#define MAT_MIDDLE_CODE_OFFSET
#define SPDIF_FLAG_BIGENDIAN
DTS type II (1024 samples)
void avio_write(AVIOContext *s, const unsigned char *buf, int size)
static int spdif_write_trailer(AVFormatContext *s)
#define AV_OPT_FLAG_ENCODING_PARAM
a generic parameter which can be set by the user for muxing or encoding
#define DCA_SYNCWORD_CORE_BE
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
DTS type III (2048 samples)
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
preferred ID for decoding MPEG audio layer 1, 2 or 3
MPEG-1 layer 2 or 3 data or MPEG-2 without extension.
MPEG-2, layer-3 low sampling frequency.
int hd_buf_count
number of frames in the hd audio buffer
#define DCA_SYNCWORD_CORE_14B_LE
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
void ffio_fill(AVIOContext *s, int b, int count)
uint8_t * buffer
allocated buffer, used for swap bytes
int out_bytes
amount of outgoing bytes
int(* header_info)(AVFormatContext *s, AVPacket *pkt)
function, which generates codec dependent header information.
static int write_trailer(AVFormatContext *s1)
#define TRUEHD_FRAME_OFFSET
void * av_fast_realloc(void *ptr, unsigned int *size, size_t min_size)
Reallocate the given buffer if it is not large enough, otherwise do nothing.
const uint32_t avpriv_dca_sample_rates[16]
static av_always_inline void spdif_put_16(IEC61937Context *ctx, AVIOContext *pb, unsigned int val)
enum IEC61937DataType data_type
burst info - reference to type of payload of the data-burst
static enum IEC61937DataType mpeg_data_type[2][3]
#define AVERROR_PATCHWELCOME
Not yet implemented in FFmpeg, patches welcome.
static const AVOption options[]
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_WB32 unsigned int_TMPL AV_RB24
static int spdif_write_header(AVFormatContext *s)
AVIOContext * pb
I/O context.
int use_preamble
preamble enabled (disabled for exactly pre-padded DTS)
#define DCA_SYNCWORD_CORE_LE
int hd_buf_filled
amount of bytes in the hd audio buffer
Describe the class of an AVClass context structure.
int length_code
length code in bits or bytes, depending on data type
static int spdif_header_truehd(AVFormatContext *s, AVPacket *pkt)
void avpriv_report_missing_feature(void *avc, const char *msg,...) av_printf_format(2
Log a generic warning message about a missing feature.
void avio_wb16(AVIOContext *s, unsigned int val)
MPEG-2, layer-2 low sampling frequency.
static int spdif_header_aac(AVFormatContext *s, AVPacket *pkt)
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
MPEG-2 data with extension.
int dtshd_skip
counter used for skipping DTS-HD frames
int hd_buf_size
size of the hd audio buffer
MPEG-2 AAC ADTS quarter-rate low sampling frequency.
static const uint16_t spdif_mpeg_pkt_offset[2][3]
void * priv_data
Format private data.
#define DCA_SYNCWORD_SUBSTREAM
static void write_header(FFV1Context *f)
static int spdif_header_ac3(AVFormatContext *s, AVPacket *pkt)
static int spdif_header_dts4(AVFormatContext *s, AVPacket *pkt, int core_size, int sample_rate, int blocks)
AVCodecParameters * codecpar
Codec parameters associated with this stream.
int extra_bswap
extra bswap for payload (for LE DTS => standard BE DTS)
This structure stores compressed data.
uint8_t * hd_buf
allocated buffer to concatenate hd audio frames
Common code between the AC-3 encoder and decoder.