3 * Copyright (c) 2009 Bartlomiej Wolowiec
4 * Copyright (c) 2010 Anssi Hannula
5 * Copyright (c) 2010 Carl Eugen Hoyos
7 * This file is part of FFmpeg.
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.
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.
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
26 * IEC-61937 encapsulation of various formats, used by S/PDIF
27 * @author Bartlomiej Wolowiec
28 * @author Anssi Hannula
29 * @author Carl Eugen Hoyos
33 * Terminology used in specification:
34 * data-burst - IEC958 frame, contains header and encapsuled frame
35 * burst-preambule - IEC958 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)
43 * IEC958 frames at normal usage start every specific count of bytes,
44 * dependent from data-type (spaces between packets are filled by zeros)
49 #include "libavcodec/ac3.h"
50 #include "libavcodec/dca.h"
51 #include "libavcodec/aacadtsdec.h"
53 typedef struct IEC958Context {
54 enum IEC958DataType data_type; ///< burst info - reference to type of payload of the data-burst
55 int length_code; ///< length code in bits or bytes, depending on data type
56 int pkt_offset; ///< data burst repetition period in bytes
57 uint8_t *buffer; ///< allocated buffer, used for swap bytes
58 int buffer_size; ///< size of allocated buffer
60 uint8_t *out_buf; ///< pointer to the outgoing data before byte-swapping
61 int out_bytes; ///< amount of outgoing bytes
63 uint8_t *hd_buf; ///< allocated buffer to concatenate hd audio frames
64 int hd_buf_size; ///< size of the hd audio buffer
65 int hd_buf_count; ///< number of frames in the hd audio buffer
66 int hd_buf_filled; ///< amount of bytes in the hd audio buffer
68 /// function, which generates codec dependent header information.
69 /// Sets data_type and pkt_offset, and length_code, out_bytes, out_buf if necessary
70 int (*header_info) (AVFormatContext *s, AVPacket *pkt);
74 static int spdif_header_ac3(AVFormatContext *s, AVPacket *pkt)
76 IEC958Context *ctx = s->priv_data;
77 int bitstream_mode = pkt->data[6] & 0x7;
79 ctx->data_type = IEC958_AC3 | (bitstream_mode << 8);
80 ctx->pkt_offset = AC3_FRAME_SIZE << 2;
84 static int spdif_header_eac3(AVFormatContext *s, AVPacket *pkt)
86 IEC958Context *ctx = s->priv_data;
87 static const uint8_t eac3_repeat[4] = {6, 3, 2, 1};
90 if ((pkt->data[4] & 0xc0) != 0xc0) /* fscod */
91 repeat = eac3_repeat[(pkt->data[4] & 0x30) >> 4]; /* numblkscod */
93 ctx->hd_buf = av_fast_realloc(ctx->hd_buf, &ctx->hd_buf_size, ctx->hd_buf_filled + pkt->size);
95 return AVERROR(ENOMEM);
97 memcpy(&ctx->hd_buf[ctx->hd_buf_filled], pkt->data, pkt->size);
99 ctx->hd_buf_filled += pkt->size;
100 if (++ctx->hd_buf_count < repeat){
104 ctx->data_type = IEC958_EAC3;
105 ctx->pkt_offset = 24576;
106 ctx->out_buf = ctx->hd_buf;
107 ctx->out_bytes = ctx->hd_buf_filled;
108 ctx->length_code = ctx->hd_buf_filled;
110 ctx->hd_buf_count = 0;
111 ctx->hd_buf_filled = 0;
115 static int spdif_header_dts(AVFormatContext *s, AVPacket *pkt)
117 IEC958Context *ctx = s->priv_data;
118 uint32_t syncword_dts = AV_RB32(pkt->data);
121 switch (syncword_dts) {
122 case DCA_MARKER_RAW_BE:
123 blocks = (AV_RB16(pkt->data + 4) >> 2) & 0x7f;
125 case DCA_MARKER_RAW_LE:
126 blocks = (AV_RL16(pkt->data + 4) >> 2) & 0x7f;
128 case DCA_MARKER_14B_BE:
130 (((pkt->data[5] & 0x07) << 4) | ((pkt->data[6] & 0x3f) >> 2));
132 case DCA_MARKER_14B_LE:
134 (((pkt->data[4] & 0x07) << 4) | ((pkt->data[7] & 0x3f) >> 2));
137 av_log(s, AV_LOG_ERROR, "bad DTS syncword 0x%x\n", syncword_dts);
142 case 512 >> 5: ctx->data_type = IEC958_DTS1; break;
143 case 1024 >> 5: ctx->data_type = IEC958_DTS2; break;
144 case 2048 >> 5: ctx->data_type = IEC958_DTS3; break;
146 av_log(s, AV_LOG_ERROR, "%i samples in DTS frame not supported\n",
150 ctx->pkt_offset = blocks << 7;
155 static const enum IEC958DataType mpeg_data_type[2][3] = {
156 // LAYER1 LAYER2 LAYER3
157 { IEC958_MPEG2_LAYER1_LSF, IEC958_MPEG2_LAYER2_LSF, IEC958_MPEG2_LAYER3_LSF }, //MPEG2 LSF
158 { IEC958_MPEG1_LAYER1, IEC958_MPEG1_LAYER23, IEC958_MPEG1_LAYER23 }, //MPEG1
161 static int spdif_header_mpeg(AVFormatContext *s, AVPacket *pkt)
163 IEC958Context *ctx = s->priv_data;
164 int version = (pkt->data[1] >> 3) & 3;
165 int layer = 3 - ((pkt->data[1] >> 1) & 3);
166 int extension = pkt->data[2] & 1;
168 if (layer == 3 || version == 1) {
169 av_log(s, AV_LOG_ERROR, "Wrong MPEG file format\n");
172 av_log(s, AV_LOG_DEBUG, "version: %i layer: %i extension: %i\n", version, layer, extension);
173 if (version == 2 && extension) {
174 ctx->data_type = IEC958_MPEG2_EXT;
175 ctx->pkt_offset = 4608;
177 ctx->data_type = mpeg_data_type [version & 1][layer];
178 ctx->pkt_offset = spdif_mpeg_pkt_offset[version & 1][layer];
180 // TODO Data type dependant info (normal/karaoke, dynamic range control)
184 static int spdif_header_aac(AVFormatContext *s, AVPacket *pkt)
186 IEC958Context *ctx = s->priv_data;
187 AACADTSHeaderInfo hdr;
191 init_get_bits(&gbc, pkt->data, AAC_ADTS_HEADER_SIZE * 8);
192 ret = ff_aac_parse_header(&gbc, &hdr);
194 av_log(s, AV_LOG_ERROR, "Wrong AAC file format\n");
198 ctx->pkt_offset = hdr.samples << 2;
199 switch (hdr.num_aac_frames) {
201 ctx->data_type = IEC958_MPEG2_AAC;
204 ctx->data_type = IEC958_MPEG2_AAC_LSF_2048;
207 ctx->data_type = IEC958_MPEG2_AAC_LSF_4096;
210 av_log(s, AV_LOG_ERROR, "%i samples in AAC frame not supported\n",
214 //TODO Data type dependent info (LC profile/SBR)
220 * It seems Dolby TrueHD frames have to be encapsulated in MAT frames before
221 * they can be encapsulated in IEC 61937.
222 * Here we encapsulate 24 TrueHD frames in a single MAT frame, padding them
223 * to achieve constant rate.
224 * The actual format of a MAT frame is unknown, but the below seems to work.
225 * However, it seems it is not actually necessary for the 24 TrueHD frames to
226 * be in an exact alignment with the MAT frame.
228 #define MAT_FRAME_SIZE 61424
229 #define TRUEHD_FRAME_OFFSET 2560
230 #define MAT_MIDDLE_CODE_OFFSET -4
232 static int spdif_header_truehd(AVFormatContext *s, AVPacket *pkt)
234 IEC958Context *ctx = s->priv_data;
235 int mat_code_length = 0;
236 const char mat_end_code[16] = { 0xC3, 0xC2, 0xC0, 0xC4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x97, 0x11 };
238 if (!ctx->hd_buf_count) {
239 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 };
240 mat_code_length = sizeof(mat_start_code) + BURST_HEADER_SIZE;
241 memcpy(ctx->hd_buf, mat_start_code, sizeof(mat_start_code));
243 } else if (ctx->hd_buf_count == 12) {
244 const char mat_middle_code[12] = { 0xC3, 0xC1, 0x42, 0x49, 0x3B, 0xFA, 0x82, 0x83, 0x49, 0x80, 0x77, 0xE0 };
245 mat_code_length = sizeof(mat_middle_code) + MAT_MIDDLE_CODE_OFFSET;
246 memcpy(&ctx->hd_buf[12 * TRUEHD_FRAME_OFFSET - BURST_HEADER_SIZE + MAT_MIDDLE_CODE_OFFSET],
247 mat_middle_code, sizeof(mat_middle_code));
250 if (pkt->size > TRUEHD_FRAME_OFFSET - mat_code_length) {
251 /* if such frames exist, we'd need some more complex logic to
252 * distribute the TrueHD frames in the MAT frame */
253 av_log(s, AV_LOG_ERROR, "TrueHD frame too big, %d bytes\n", pkt->size);
254 av_log_ask_for_sample(s, NULL);
258 memcpy(&ctx->hd_buf[ctx->hd_buf_count * TRUEHD_FRAME_OFFSET - BURST_HEADER_SIZE + mat_code_length],
259 pkt->data, pkt->size);
260 memset(&ctx->hd_buf[ctx->hd_buf_count * TRUEHD_FRAME_OFFSET - BURST_HEADER_SIZE + mat_code_length + pkt->size],
261 0, TRUEHD_FRAME_OFFSET - pkt->size - mat_code_length);
263 if (++ctx->hd_buf_count < 24){
267 memcpy(&ctx->hd_buf[MAT_FRAME_SIZE - sizeof(mat_end_code)], mat_end_code, sizeof(mat_end_code));
268 ctx->hd_buf_count = 0;
270 ctx->data_type = IEC958_TRUEHD;
271 ctx->pkt_offset = 61440;
272 ctx->out_buf = ctx->hd_buf;
273 ctx->out_bytes = MAT_FRAME_SIZE;
274 ctx->length_code = MAT_FRAME_SIZE;
278 static int spdif_write_header(AVFormatContext *s)
280 IEC958Context *ctx = s->priv_data;
282 switch (s->streams[0]->codec->codec_id) {
284 ctx->header_info = spdif_header_ac3;
287 ctx->header_info = spdif_header_eac3;
292 ctx->header_info = spdif_header_mpeg;
295 ctx->header_info = spdif_header_dts;
298 ctx->header_info = spdif_header_aac;
300 case CODEC_ID_TRUEHD:
301 ctx->header_info = spdif_header_truehd;
302 ctx->hd_buf = av_malloc(MAT_FRAME_SIZE);
304 return AVERROR(ENOMEM);
307 av_log(s, AV_LOG_ERROR, "codec not supported\n");
313 static int spdif_write_trailer(AVFormatContext *s)
315 IEC958Context *ctx = s->priv_data;
316 av_freep(&ctx->buffer);
317 av_freep(&ctx->hd_buf);
321 static int spdif_write_packet(struct AVFormatContext *s, AVPacket *pkt)
323 IEC958Context *ctx = s->priv_data;
326 ctx->out_buf = pkt->data;
327 ctx->out_bytes = pkt->size;
328 ctx->length_code = FFALIGN(pkt->size, 2) << 3;
330 ret = ctx->header_info(s, pkt);
333 if (!ctx->pkt_offset)
336 padding = (ctx->pkt_offset - BURST_HEADER_SIZE - ctx->out_bytes) >> 1;
338 av_log(s, AV_LOG_ERROR, "bitrate is too high\n");
342 put_le16(s->pb, SYNCWORD1); //Pa
343 put_le16(s->pb, SYNCWORD2); //Pb
344 put_le16(s->pb, ctx->data_type); //Pc
345 put_le16(s->pb, ctx->length_code);//Pd
348 put_buffer(s->pb, ctx->out_buf, ctx->out_bytes & ~1);
350 av_fast_malloc(&ctx->buffer, &ctx->buffer_size, ctx->out_bytes + FF_INPUT_BUFFER_PADDING_SIZE);
352 return AVERROR(ENOMEM);
353 ff_spdif_bswap_buf16((uint16_t *)ctx->buffer, (uint16_t *)ctx->out_buf, ctx->out_bytes >> 1);
354 put_buffer(s->pb, ctx->buffer, ctx->out_bytes & ~1);
357 if (ctx->out_bytes & 1)
358 put_be16(s->pb, ctx->out_buf[ctx->out_bytes - 1]);
360 for (; padding > 0; padding--)
363 av_log(s, AV_LOG_DEBUG, "type=%x len=%i pkt_offset=%i\n",
364 ctx->data_type, ctx->out_bytes, ctx->pkt_offset);
366 put_flush_packet(s->pb);
370 AVOutputFormat spdif_muxer = {
372 NULL_IF_CONFIG_SMALL("IEC958 - S/PDIF (IEC-61937)"),
375 sizeof(IEC958Context),