00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047 #include "avformat.h"
00048 #include "spdif.h"
00049 #include "libavcodec/ac3.h"
00050 #include "libavcodec/dca.h"
00051 #include "libavcodec/aacadtsdec.h"
00052
00053 typedef struct IEC61937Context {
00054 enum IEC61937DataType data_type;
00055 int length_code;
00056 int pkt_offset;
00057 uint8_t *buffer;
00058 int buffer_size;
00059
00060 uint8_t *out_buf;
00061 int out_bytes;
00062
00063 int use_preamble;
00064 int extra_bswap;
00065
00066 uint8_t *hd_buf;
00067 int hd_buf_size;
00068 int hd_buf_count;
00069 int hd_buf_filled;
00070
00073 int (*header_info) (AVFormatContext *s, AVPacket *pkt);
00074 } IEC61937Context;
00075
00076
00077 static int spdif_header_ac3(AVFormatContext *s, AVPacket *pkt)
00078 {
00079 IEC61937Context *ctx = s->priv_data;
00080 int bitstream_mode = pkt->data[5] & 0x7;
00081
00082 ctx->data_type = IEC61937_AC3 | (bitstream_mode << 8);
00083 ctx->pkt_offset = AC3_FRAME_SIZE << 2;
00084 return 0;
00085 }
00086
00087 static int spdif_header_eac3(AVFormatContext *s, AVPacket *pkt)
00088 {
00089 IEC61937Context *ctx = s->priv_data;
00090 static const uint8_t eac3_repeat[4] = {6, 3, 2, 1};
00091 int repeat = 1;
00092
00093 if ((pkt->data[4] & 0xc0) != 0xc0)
00094 repeat = eac3_repeat[(pkt->data[4] & 0x30) >> 4];
00095
00096 ctx->hd_buf = av_fast_realloc(ctx->hd_buf, &ctx->hd_buf_size, ctx->hd_buf_filled + pkt->size);
00097 if (!ctx->hd_buf)
00098 return AVERROR(ENOMEM);
00099
00100 memcpy(&ctx->hd_buf[ctx->hd_buf_filled], pkt->data, pkt->size);
00101
00102 ctx->hd_buf_filled += pkt->size;
00103 if (++ctx->hd_buf_count < repeat){
00104 ctx->pkt_offset = 0;
00105 return 0;
00106 }
00107 ctx->data_type = IEC61937_EAC3;
00108 ctx->pkt_offset = 24576;
00109 ctx->out_buf = ctx->hd_buf;
00110 ctx->out_bytes = ctx->hd_buf_filled;
00111 ctx->length_code = ctx->hd_buf_filled;
00112
00113 ctx->hd_buf_count = 0;
00114 ctx->hd_buf_filled = 0;
00115 return 0;
00116 }
00117
00118 static int spdif_header_dts(AVFormatContext *s, AVPacket *pkt)
00119 {
00120 IEC61937Context *ctx = s->priv_data;
00121 uint32_t syncword_dts = AV_RB32(pkt->data);
00122 int blocks;
00123
00124 switch (syncword_dts) {
00125 case DCA_MARKER_RAW_BE:
00126 blocks = (AV_RB16(pkt->data + 4) >> 2) & 0x7f;
00127 break;
00128 case DCA_MARKER_RAW_LE:
00129 blocks = (AV_RL16(pkt->data + 4) >> 2) & 0x7f;
00130 ctx->extra_bswap = 1;
00131 break;
00132 case DCA_MARKER_14B_BE:
00133 blocks =
00134 (((pkt->data[5] & 0x07) << 4) | ((pkt->data[6] & 0x3f) >> 2));
00135 break;
00136 case DCA_MARKER_14B_LE:
00137 blocks =
00138 (((pkt->data[4] & 0x07) << 4) | ((pkt->data[7] & 0x3f) >> 2));
00139 ctx->extra_bswap = 1;
00140 break;
00141 default:
00142 av_log(s, AV_LOG_ERROR, "bad DTS syncword 0x%x\n", syncword_dts);
00143 return AVERROR_INVALIDDATA;
00144 }
00145 blocks++;
00146 switch (blocks) {
00147 case 512 >> 5: ctx->data_type = IEC61937_DTS1; break;
00148 case 1024 >> 5: ctx->data_type = IEC61937_DTS2; break;
00149 case 2048 >> 5: ctx->data_type = IEC61937_DTS3; break;
00150 default:
00151 av_log(s, AV_LOG_ERROR, "%i samples in DTS frame not supported\n",
00152 blocks << 5);
00153 return AVERROR(ENOSYS);
00154 }
00155 ctx->pkt_offset = blocks << 7;
00156
00157 if (ctx->out_bytes == ctx->pkt_offset) {
00158
00159
00160
00161 ctx->use_preamble = 0;
00162 }
00163
00164 return 0;
00165 }
00166
00167 static const enum IEC61937DataType mpeg_data_type[2][3] = {
00168
00169 { IEC61937_MPEG2_LAYER1_LSF, IEC61937_MPEG2_LAYER2_LSF, IEC61937_MPEG2_LAYER3_LSF },
00170 { IEC61937_MPEG1_LAYER1, IEC61937_MPEG1_LAYER23, IEC61937_MPEG1_LAYER23 },
00171 };
00172
00173 static int spdif_header_mpeg(AVFormatContext *s, AVPacket *pkt)
00174 {
00175 IEC61937Context *ctx = s->priv_data;
00176 int version = (pkt->data[1] >> 3) & 3;
00177 int layer = 3 - ((pkt->data[1] >> 1) & 3);
00178 int extension = pkt->data[2] & 1;
00179
00180 if (layer == 3 || version == 1) {
00181 av_log(s, AV_LOG_ERROR, "Wrong MPEG file format\n");
00182 return AVERROR_INVALIDDATA;
00183 }
00184 av_log(s, AV_LOG_DEBUG, "version: %i layer: %i extension: %i\n", version, layer, extension);
00185 if (version == 2 && extension) {
00186 ctx->data_type = IEC61937_MPEG2_EXT;
00187 ctx->pkt_offset = 4608;
00188 } else {
00189 ctx->data_type = mpeg_data_type [version & 1][layer];
00190 ctx->pkt_offset = spdif_mpeg_pkt_offset[version & 1][layer];
00191 }
00192
00193 return 0;
00194 }
00195
00196 static int spdif_header_aac(AVFormatContext *s, AVPacket *pkt)
00197 {
00198 IEC61937Context *ctx = s->priv_data;
00199 AACADTSHeaderInfo hdr;
00200 GetBitContext gbc;
00201 int ret;
00202
00203 init_get_bits(&gbc, pkt->data, AAC_ADTS_HEADER_SIZE * 8);
00204 ret = ff_aac_parse_header(&gbc, &hdr);
00205 if (ret < 0) {
00206 av_log(s, AV_LOG_ERROR, "Wrong AAC file format\n");
00207 return AVERROR_INVALIDDATA;
00208 }
00209
00210 ctx->pkt_offset = hdr.samples << 2;
00211 switch (hdr.num_aac_frames) {
00212 case 1:
00213 ctx->data_type = IEC61937_MPEG2_AAC;
00214 break;
00215 case 2:
00216 ctx->data_type = IEC61937_MPEG2_AAC_LSF_2048;
00217 break;
00218 case 4:
00219 ctx->data_type = IEC61937_MPEG2_AAC_LSF_4096;
00220 break;
00221 default:
00222 av_log(s, AV_LOG_ERROR, "%i samples in AAC frame not supported\n",
00223 hdr.samples);
00224 return AVERROR(EINVAL);
00225 }
00226
00227 return 0;
00228 }
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240 #define MAT_FRAME_SIZE 61424
00241 #define TRUEHD_FRAME_OFFSET 2560
00242 #define MAT_MIDDLE_CODE_OFFSET -4
00243
00244 static int spdif_header_truehd(AVFormatContext *s, AVPacket *pkt)
00245 {
00246 IEC61937Context *ctx = s->priv_data;
00247 int mat_code_length = 0;
00248 const char mat_end_code[16] = { 0xC3, 0xC2, 0xC0, 0xC4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x97, 0x11 };
00249
00250 if (!ctx->hd_buf_count) {
00251 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 };
00252 mat_code_length = sizeof(mat_start_code) + BURST_HEADER_SIZE;
00253 memcpy(ctx->hd_buf, mat_start_code, sizeof(mat_start_code));
00254
00255 } else if (ctx->hd_buf_count == 12) {
00256 const char mat_middle_code[12] = { 0xC3, 0xC1, 0x42, 0x49, 0x3B, 0xFA, 0x82, 0x83, 0x49, 0x80, 0x77, 0xE0 };
00257 mat_code_length = sizeof(mat_middle_code) + MAT_MIDDLE_CODE_OFFSET;
00258 memcpy(&ctx->hd_buf[12 * TRUEHD_FRAME_OFFSET - BURST_HEADER_SIZE + MAT_MIDDLE_CODE_OFFSET],
00259 mat_middle_code, sizeof(mat_middle_code));
00260 }
00261
00262 if (pkt->size > TRUEHD_FRAME_OFFSET - mat_code_length) {
00263
00264
00265 av_log(s, AV_LOG_ERROR, "TrueHD frame too big, %d bytes\n", pkt->size);
00266 av_log_ask_for_sample(s, NULL);
00267 return AVERROR_INVALIDDATA;
00268 }
00269
00270 memcpy(&ctx->hd_buf[ctx->hd_buf_count * TRUEHD_FRAME_OFFSET - BURST_HEADER_SIZE + mat_code_length],
00271 pkt->data, pkt->size);
00272 memset(&ctx->hd_buf[ctx->hd_buf_count * TRUEHD_FRAME_OFFSET - BURST_HEADER_SIZE + mat_code_length + pkt->size],
00273 0, TRUEHD_FRAME_OFFSET - pkt->size - mat_code_length);
00274
00275 if (++ctx->hd_buf_count < 24){
00276 ctx->pkt_offset = 0;
00277 return 0;
00278 }
00279 memcpy(&ctx->hd_buf[MAT_FRAME_SIZE - sizeof(mat_end_code)], mat_end_code, sizeof(mat_end_code));
00280 ctx->hd_buf_count = 0;
00281
00282 ctx->data_type = IEC61937_TRUEHD;
00283 ctx->pkt_offset = 61440;
00284 ctx->out_buf = ctx->hd_buf;
00285 ctx->out_bytes = MAT_FRAME_SIZE;
00286 ctx->length_code = MAT_FRAME_SIZE;
00287 return 0;
00288 }
00289
00290 static int spdif_write_header(AVFormatContext *s)
00291 {
00292 IEC61937Context *ctx = s->priv_data;
00293
00294 switch (s->streams[0]->codec->codec_id) {
00295 case CODEC_ID_AC3:
00296 ctx->header_info = spdif_header_ac3;
00297 break;
00298 case CODEC_ID_EAC3:
00299 ctx->header_info = spdif_header_eac3;
00300 break;
00301 case CODEC_ID_MP1:
00302 case CODEC_ID_MP2:
00303 case CODEC_ID_MP3:
00304 ctx->header_info = spdif_header_mpeg;
00305 break;
00306 case CODEC_ID_DTS:
00307 ctx->header_info = spdif_header_dts;
00308 break;
00309 case CODEC_ID_AAC:
00310 ctx->header_info = spdif_header_aac;
00311 break;
00312 case CODEC_ID_TRUEHD:
00313 ctx->header_info = spdif_header_truehd;
00314 ctx->hd_buf = av_malloc(MAT_FRAME_SIZE);
00315 if (!ctx->hd_buf)
00316 return AVERROR(ENOMEM);
00317 break;
00318 default:
00319 av_log(s, AV_LOG_ERROR, "codec not supported\n");
00320 return AVERROR_PATCHWELCOME;
00321 }
00322 return 0;
00323 }
00324
00325 static int spdif_write_trailer(AVFormatContext *s)
00326 {
00327 IEC61937Context *ctx = s->priv_data;
00328 av_freep(&ctx->buffer);
00329 av_freep(&ctx->hd_buf);
00330 return 0;
00331 }
00332
00333 static int spdif_write_packet(struct AVFormatContext *s, AVPacket *pkt)
00334 {
00335 IEC61937Context *ctx = s->priv_data;
00336 int ret, padding;
00337
00338 ctx->out_buf = pkt->data;
00339 ctx->out_bytes = pkt->size;
00340 ctx->length_code = FFALIGN(pkt->size, 2) << 3;
00341 ctx->use_preamble = 1;
00342 ctx->extra_bswap = 0;
00343
00344 ret = ctx->header_info(s, pkt);
00345 if (ret < 0)
00346 return ret;
00347 if (!ctx->pkt_offset)
00348 return 0;
00349
00350 padding = (ctx->pkt_offset - ctx->use_preamble * BURST_HEADER_SIZE - ctx->out_bytes) & ~1;
00351 if (padding < 0) {
00352 av_log(s, AV_LOG_ERROR, "bitrate is too high\n");
00353 return AVERROR(EINVAL);
00354 }
00355
00356 if (ctx->use_preamble) {
00357 put_le16(s->pb, SYNCWORD1);
00358 put_le16(s->pb, SYNCWORD2);
00359 put_le16(s->pb, ctx->data_type);
00360 put_le16(s->pb, ctx->length_code);
00361 }
00362
00363 if (HAVE_BIGENDIAN ^ ctx->extra_bswap) {
00364 put_buffer(s->pb, ctx->out_buf, ctx->out_bytes & ~1);
00365 } else {
00366 av_fast_malloc(&ctx->buffer, &ctx->buffer_size, ctx->out_bytes + FF_INPUT_BUFFER_PADDING_SIZE);
00367 if (!ctx->buffer)
00368 return AVERROR(ENOMEM);
00369 ff_spdif_bswap_buf16((uint16_t *)ctx->buffer, (uint16_t *)ctx->out_buf, ctx->out_bytes >> 1);
00370 put_buffer(s->pb, ctx->buffer, ctx->out_bytes & ~1);
00371 }
00372
00373 if (ctx->out_bytes & 1)
00374 put_be16(s->pb, ctx->out_buf[ctx->out_bytes - 1]);
00375
00376 put_nbyte(s->pb, 0, padding);
00377
00378 av_log(s, AV_LOG_DEBUG, "type=%x len=%i pkt_offset=%i\n",
00379 ctx->data_type, ctx->out_bytes, ctx->pkt_offset);
00380
00381 put_flush_packet(s->pb);
00382 return 0;
00383 }
00384
00385 AVOutputFormat spdif_muxer = {
00386 "spdif",
00387 NULL_IF_CONFIG_SMALL("IEC 61937 (used on S/PDIF - IEC958)"),
00388 NULL,
00389 "spdif",
00390 sizeof(IEC61937Context),
00391 CODEC_ID_AC3,
00392 CODEC_ID_NONE,
00393 spdif_write_header,
00394 spdif_write_packet,
00395 spdif_write_trailer,
00396 };