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 "avio_internal.h"
00049 #include "spdif.h"
00050 #include "libavcodec/ac3.h"
00051 #include "libavcodec/dca.h"
00052 #include "libavcodec/dcadata.h"
00053 #include "libavcodec/aacadtsdec.h"
00054 #include "libavutil/opt.h"
00055
00056 typedef struct IEC61937Context {
00057 const AVClass *av_class;
00058 enum IEC61937DataType data_type;
00059 int length_code;
00060 int pkt_offset;
00061 uint8_t *buffer;
00062 int buffer_size;
00063
00064 uint8_t *out_buf;
00065 int out_bytes;
00066
00067 int use_preamble;
00068 int extra_bswap;
00069
00070 uint8_t *hd_buf;
00071 int hd_buf_size;
00072 int hd_buf_count;
00073 int hd_buf_filled;
00074
00075 int dtshd_skip;
00076
00077
00078 int dtshd_rate;
00079 int dtshd_fallback;
00080 #define SPDIF_FLAG_BIGENDIAN 0x01
00081 int spdif_flags;
00082
00085 int (*header_info) (AVFormatContext *s, AVPacket *pkt);
00086 } IEC61937Context;
00087
00088 static const AVOption options[] = {
00089 { "spdif_flags", "IEC 61937 encapsulation flags", offsetof(IEC61937Context, spdif_flags), FF_OPT_TYPE_FLAGS, 0, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "spdif_flags" },
00090 { "be", "output in big-endian format (for use as s16be)", 0, FF_OPT_TYPE_CONST, SPDIF_FLAG_BIGENDIAN, 0, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM, "spdif_flags" },
00091 { "dtshd_rate", "mux complete DTS frames in HD mode at the specified IEC958 rate (in Hz, default 0=disabled)", offsetof(IEC61937Context, dtshd_rate), FF_OPT_TYPE_INT, 0, 0, 768000, AV_OPT_FLAG_ENCODING_PARAM },
00092 { "dtshd_fallback_time", "min secs to strip HD for after an overflow (-1: till the end, default 60)", offsetof(IEC61937Context, dtshd_fallback), FF_OPT_TYPE_INT, 60, -1, INT_MAX, AV_OPT_FLAG_ENCODING_PARAM },
00093 { NULL },
00094 };
00095
00096 static const AVClass class = { "spdif", av_default_item_name, options, LIBAVUTIL_VERSION_INT };
00097
00098 static int spdif_header_ac3(AVFormatContext *s, AVPacket *pkt)
00099 {
00100 IEC61937Context *ctx = s->priv_data;
00101 int bitstream_mode = pkt->data[5] & 0x7;
00102
00103 ctx->data_type = IEC61937_AC3 | (bitstream_mode << 8);
00104 ctx->pkt_offset = AC3_FRAME_SIZE << 2;
00105 return 0;
00106 }
00107
00108 static int spdif_header_eac3(AVFormatContext *s, AVPacket *pkt)
00109 {
00110 IEC61937Context *ctx = s->priv_data;
00111 static const uint8_t eac3_repeat[4] = {6, 3, 2, 1};
00112 int repeat = 1;
00113
00114 if ((pkt->data[4] & 0xc0) != 0xc0)
00115 repeat = eac3_repeat[(pkt->data[4] & 0x30) >> 4];
00116
00117 ctx->hd_buf = av_fast_realloc(ctx->hd_buf, &ctx->hd_buf_size, ctx->hd_buf_filled + pkt->size);
00118 if (!ctx->hd_buf)
00119 return AVERROR(ENOMEM);
00120
00121 memcpy(&ctx->hd_buf[ctx->hd_buf_filled], pkt->data, pkt->size);
00122
00123 ctx->hd_buf_filled += pkt->size;
00124 if (++ctx->hd_buf_count < repeat){
00125 ctx->pkt_offset = 0;
00126 return 0;
00127 }
00128 ctx->data_type = IEC61937_EAC3;
00129 ctx->pkt_offset = 24576;
00130 ctx->out_buf = ctx->hd_buf;
00131 ctx->out_bytes = ctx->hd_buf_filled;
00132 ctx->length_code = ctx->hd_buf_filled;
00133
00134 ctx->hd_buf_count = 0;
00135 ctx->hd_buf_filled = 0;
00136 return 0;
00137 }
00138
00139
00140
00141
00142
00143
00144
00145
00146 static int spdif_dts4_subtype(int period)
00147 {
00148 switch (period) {
00149 case 512: return 0x0;
00150 case 1024: return 0x1;
00151 case 2048: return 0x2;
00152 case 4096: return 0x3;
00153 case 8192: return 0x4;
00154 case 16384: return 0x5;
00155 }
00156 return -1;
00157 }
00158
00159 static int spdif_header_dts4(AVFormatContext *s, AVPacket *pkt, int core_size,
00160 int sample_rate, int blocks)
00161 {
00162 IEC61937Context *ctx = s->priv_data;
00163 static const char dtshd_start_code[10] = { 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfe, 0xfe };
00164 int pkt_size = pkt->size;
00165 int period;
00166 int subtype;
00167
00168 if (!core_size) {
00169 av_log(s, AV_LOG_ERROR, "HD mode not supported for this format\n");
00170 return AVERROR(EINVAL);
00171 }
00172
00173 if (!sample_rate) {
00174 av_log(s, AV_LOG_ERROR, "Unknown DTS sample rate for HD\n");
00175 return AVERROR_INVALIDDATA;
00176 }
00177
00178 period = ctx->dtshd_rate * (blocks << 5) / sample_rate;
00179 subtype = spdif_dts4_subtype(period);
00180
00181 if (subtype < 0) {
00182 av_log(s, AV_LOG_ERROR, "Specified HD rate of %d Hz would require an "
00183 "impossible repetition period of %d for the current DTS stream"
00184 " (blocks = %d, sample rate = %d)\n", ctx->dtshd_rate, period,
00185 blocks << 5, sample_rate);
00186 return AVERROR(EINVAL);
00187 }
00188
00189
00190
00191 ctx->pkt_offset = period * 4;
00192 ctx->data_type = IEC61937_DTSHD | subtype << 8;
00193
00194
00195
00196
00197
00198
00199 if (sizeof(dtshd_start_code) + 2 + pkt_size
00200 > ctx->pkt_offset - BURST_HEADER_SIZE && core_size) {
00201 if (!ctx->dtshd_skip)
00202 av_log(s, AV_LOG_WARNING, "DTS-HD bitrate too high, "
00203 "temporarily sending core only\n");
00204 if (ctx->dtshd_fallback > 0)
00205 ctx->dtshd_skip = sample_rate * ctx->dtshd_fallback / (blocks << 5);
00206 else
00207
00208
00209 ctx->dtshd_skip = 1;
00210 }
00211 if (ctx->dtshd_skip && core_size) {
00212 pkt_size = core_size;
00213 if (ctx->dtshd_fallback >= 0)
00214 --ctx->dtshd_skip;
00215 }
00216
00217 ctx->out_bytes = sizeof(dtshd_start_code) + 2 + pkt_size;
00218 ctx->length_code = ctx->out_bytes;
00219
00220 av_fast_malloc(&ctx->hd_buf, &ctx->hd_buf_size, ctx->out_bytes);
00221 if (!ctx->hd_buf)
00222 return AVERROR(ENOMEM);
00223
00224 ctx->out_buf = ctx->hd_buf;
00225
00226 memcpy(ctx->hd_buf, dtshd_start_code, sizeof(dtshd_start_code));
00227 AV_WB16(ctx->hd_buf + sizeof(dtshd_start_code), pkt_size);
00228 memcpy(ctx->hd_buf + sizeof(dtshd_start_code) + 2, pkt->data, pkt_size);
00229
00230 return 0;
00231 }
00232
00233 static int spdif_header_dts(AVFormatContext *s, AVPacket *pkt)
00234 {
00235 IEC61937Context *ctx = s->priv_data;
00236 uint32_t syncword_dts = AV_RB32(pkt->data);
00237 int blocks;
00238 int sample_rate = 0;
00239 int core_size = 0;
00240
00241 if (pkt->size < 9)
00242 return AVERROR_INVALIDDATA;
00243
00244 switch (syncword_dts) {
00245 case DCA_MARKER_RAW_BE:
00246 blocks = (AV_RB16(pkt->data + 4) >> 2) & 0x7f;
00247 core_size = ((AV_RB24(pkt->data + 5) >> 4) & 0x3fff) + 1;
00248 sample_rate = dca_sample_rates[(pkt->data[8] >> 2) & 0x0f];
00249 break;
00250 case DCA_MARKER_RAW_LE:
00251 blocks = (AV_RL16(pkt->data + 4) >> 2) & 0x7f;
00252 ctx->extra_bswap = 1;
00253 break;
00254 case DCA_MARKER_14B_BE:
00255 blocks =
00256 (((pkt->data[5] & 0x07) << 4) | ((pkt->data[6] & 0x3f) >> 2));
00257 break;
00258 case DCA_MARKER_14B_LE:
00259 blocks =
00260 (((pkt->data[4] & 0x07) << 4) | ((pkt->data[7] & 0x3f) >> 2));
00261 ctx->extra_bswap = 1;
00262 break;
00263 case DCA_HD_MARKER:
00264
00265
00266
00267 av_log(s, AV_LOG_ERROR, "stray DTS-HD frame\n");
00268 return AVERROR_INVALIDDATA;
00269 default:
00270 av_log(s, AV_LOG_ERROR, "bad DTS syncword 0x%x\n", syncword_dts);
00271 return AVERROR_INVALIDDATA;
00272 }
00273 blocks++;
00274
00275 if (ctx->dtshd_rate)
00276
00277 return spdif_header_dts4(s, pkt, core_size, sample_rate, blocks);
00278
00279 switch (blocks) {
00280 case 512 >> 5: ctx->data_type = IEC61937_DTS1; break;
00281 case 1024 >> 5: ctx->data_type = IEC61937_DTS2; break;
00282 case 2048 >> 5: ctx->data_type = IEC61937_DTS3; break;
00283 default:
00284 av_log(s, AV_LOG_ERROR, "%i samples in DTS frame not supported\n",
00285 blocks << 5);
00286 return AVERROR(ENOSYS);
00287 }
00288
00289
00290 if (core_size && core_size < pkt->size) {
00291 ctx->out_bytes = core_size;
00292 ctx->length_code = core_size << 3;
00293 }
00294
00295 ctx->pkt_offset = blocks << 7;
00296
00297 if (ctx->out_bytes == ctx->pkt_offset) {
00298
00299
00300
00301 ctx->use_preamble = 0;
00302 } else if (ctx->out_bytes > ctx->pkt_offset - BURST_HEADER_SIZE) {
00303 av_log_ask_for_sample(s, "Unrecognized large DTS frame.");
00304
00305 }
00306
00307 return 0;
00308 }
00309
00310 static const enum IEC61937DataType mpeg_data_type[2][3] = {
00311
00312 { IEC61937_MPEG2_LAYER1_LSF, IEC61937_MPEG2_LAYER2_LSF, IEC61937_MPEG2_LAYER3_LSF },
00313 { IEC61937_MPEG1_LAYER1, IEC61937_MPEG1_LAYER23, IEC61937_MPEG1_LAYER23 },
00314 };
00315
00316 static int spdif_header_mpeg(AVFormatContext *s, AVPacket *pkt)
00317 {
00318 IEC61937Context *ctx = s->priv_data;
00319 int version = (pkt->data[1] >> 3) & 3;
00320 int layer = 3 - ((pkt->data[1] >> 1) & 3);
00321 int extension = pkt->data[2] & 1;
00322
00323 if (layer == 3 || version == 1) {
00324 av_log(s, AV_LOG_ERROR, "Wrong MPEG file format\n");
00325 return AVERROR_INVALIDDATA;
00326 }
00327 av_log(s, AV_LOG_DEBUG, "version: %i layer: %i extension: %i\n", version, layer, extension);
00328 if (version == 2 && extension) {
00329 ctx->data_type = IEC61937_MPEG2_EXT;
00330 ctx->pkt_offset = 4608;
00331 } else {
00332 ctx->data_type = mpeg_data_type [version & 1][layer];
00333 ctx->pkt_offset = spdif_mpeg_pkt_offset[version & 1][layer];
00334 }
00335
00336 return 0;
00337 }
00338
00339 static int spdif_header_aac(AVFormatContext *s, AVPacket *pkt)
00340 {
00341 IEC61937Context *ctx = s->priv_data;
00342 AACADTSHeaderInfo hdr;
00343 GetBitContext gbc;
00344 int ret;
00345
00346 init_get_bits(&gbc, pkt->data, AAC_ADTS_HEADER_SIZE * 8);
00347 ret = ff_aac_parse_header(&gbc, &hdr);
00348 if (ret < 0) {
00349 av_log(s, AV_LOG_ERROR, "Wrong AAC file format\n");
00350 return AVERROR_INVALIDDATA;
00351 }
00352
00353 ctx->pkt_offset = hdr.samples << 2;
00354 switch (hdr.num_aac_frames) {
00355 case 1:
00356 ctx->data_type = IEC61937_MPEG2_AAC;
00357 break;
00358 case 2:
00359 ctx->data_type = IEC61937_MPEG2_AAC_LSF_2048;
00360 break;
00361 case 4:
00362 ctx->data_type = IEC61937_MPEG2_AAC_LSF_4096;
00363 break;
00364 default:
00365 av_log(s, AV_LOG_ERROR, "%i samples in AAC frame not supported\n",
00366 hdr.samples);
00367 return AVERROR(EINVAL);
00368 }
00369
00370 return 0;
00371 }
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383 #define MAT_FRAME_SIZE 61424
00384 #define TRUEHD_FRAME_OFFSET 2560
00385 #define MAT_MIDDLE_CODE_OFFSET -4
00386
00387 static int spdif_header_truehd(AVFormatContext *s, AVPacket *pkt)
00388 {
00389 IEC61937Context *ctx = s->priv_data;
00390 int mat_code_length = 0;
00391 const char mat_end_code[16] = { 0xC3, 0xC2, 0xC0, 0xC4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x97, 0x11 };
00392
00393 if (!ctx->hd_buf_count) {
00394 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 };
00395 mat_code_length = sizeof(mat_start_code) + BURST_HEADER_SIZE;
00396 memcpy(ctx->hd_buf, mat_start_code, sizeof(mat_start_code));
00397
00398 } else if (ctx->hd_buf_count == 12) {
00399 const char mat_middle_code[12] = { 0xC3, 0xC1, 0x42, 0x49, 0x3B, 0xFA, 0x82, 0x83, 0x49, 0x80, 0x77, 0xE0 };
00400 mat_code_length = sizeof(mat_middle_code) + MAT_MIDDLE_CODE_OFFSET;
00401 memcpy(&ctx->hd_buf[12 * TRUEHD_FRAME_OFFSET - BURST_HEADER_SIZE + MAT_MIDDLE_CODE_OFFSET],
00402 mat_middle_code, sizeof(mat_middle_code));
00403 }
00404
00405 if (pkt->size > TRUEHD_FRAME_OFFSET - mat_code_length) {
00406
00407
00408 av_log(s, AV_LOG_ERROR, "TrueHD frame too big, %d bytes\n", pkt->size);
00409 av_log_ask_for_sample(s, NULL);
00410 return AVERROR_INVALIDDATA;
00411 }
00412
00413 memcpy(&ctx->hd_buf[ctx->hd_buf_count * TRUEHD_FRAME_OFFSET - BURST_HEADER_SIZE + mat_code_length],
00414 pkt->data, pkt->size);
00415 memset(&ctx->hd_buf[ctx->hd_buf_count * TRUEHD_FRAME_OFFSET - BURST_HEADER_SIZE + mat_code_length + pkt->size],
00416 0, TRUEHD_FRAME_OFFSET - pkt->size - mat_code_length);
00417
00418 if (++ctx->hd_buf_count < 24){
00419 ctx->pkt_offset = 0;
00420 return 0;
00421 }
00422 memcpy(&ctx->hd_buf[MAT_FRAME_SIZE - sizeof(mat_end_code)], mat_end_code, sizeof(mat_end_code));
00423 ctx->hd_buf_count = 0;
00424
00425 ctx->data_type = IEC61937_TRUEHD;
00426 ctx->pkt_offset = 61440;
00427 ctx->out_buf = ctx->hd_buf;
00428 ctx->out_bytes = MAT_FRAME_SIZE;
00429 ctx->length_code = MAT_FRAME_SIZE;
00430 return 0;
00431 }
00432
00433 static int spdif_write_header(AVFormatContext *s)
00434 {
00435 IEC61937Context *ctx = s->priv_data;
00436
00437 switch (s->streams[0]->codec->codec_id) {
00438 case CODEC_ID_AC3:
00439 ctx->header_info = spdif_header_ac3;
00440 break;
00441 case CODEC_ID_EAC3:
00442 ctx->header_info = spdif_header_eac3;
00443 break;
00444 case CODEC_ID_MP1:
00445 case CODEC_ID_MP2:
00446 case CODEC_ID_MP3:
00447 ctx->header_info = spdif_header_mpeg;
00448 break;
00449 case CODEC_ID_DTS:
00450 ctx->header_info = spdif_header_dts;
00451 break;
00452 case CODEC_ID_AAC:
00453 ctx->header_info = spdif_header_aac;
00454 break;
00455 case CODEC_ID_TRUEHD:
00456 ctx->header_info = spdif_header_truehd;
00457 ctx->hd_buf = av_malloc(MAT_FRAME_SIZE);
00458 if (!ctx->hd_buf)
00459 return AVERROR(ENOMEM);
00460 break;
00461 default:
00462 av_log(s, AV_LOG_ERROR, "codec not supported\n");
00463 return AVERROR_PATCHWELCOME;
00464 }
00465 return 0;
00466 }
00467
00468 static int spdif_write_trailer(AVFormatContext *s)
00469 {
00470 IEC61937Context *ctx = s->priv_data;
00471 av_freep(&ctx->buffer);
00472 av_freep(&ctx->hd_buf);
00473 return 0;
00474 }
00475
00476 static av_always_inline void spdif_put_16(IEC61937Context *ctx,
00477 AVIOContext *pb, unsigned int val)
00478 {
00479 if (ctx->spdif_flags & SPDIF_FLAG_BIGENDIAN)
00480 avio_wb16(pb, val);
00481 else
00482 avio_wl16(pb, val);
00483 }
00484
00485 static int spdif_write_packet(struct AVFormatContext *s, AVPacket *pkt)
00486 {
00487 IEC61937Context *ctx = s->priv_data;
00488 int ret, padding;
00489
00490 ctx->out_buf = pkt->data;
00491 ctx->out_bytes = pkt->size;
00492 ctx->length_code = FFALIGN(pkt->size, 2) << 3;
00493 ctx->use_preamble = 1;
00494 ctx->extra_bswap = 0;
00495
00496 ret = ctx->header_info(s, pkt);
00497 if (ret < 0)
00498 return ret;
00499 if (!ctx->pkt_offset)
00500 return 0;
00501
00502 padding = (ctx->pkt_offset - ctx->use_preamble * BURST_HEADER_SIZE - ctx->out_bytes) & ~1;
00503 if (padding < 0) {
00504 av_log(s, AV_LOG_ERROR, "bitrate is too high\n");
00505 return AVERROR(EINVAL);
00506 }
00507
00508 if (ctx->use_preamble) {
00509 spdif_put_16(ctx, s->pb, SYNCWORD1);
00510 spdif_put_16(ctx, s->pb, SYNCWORD2);
00511 spdif_put_16(ctx, s->pb, ctx->data_type);
00512 spdif_put_16(ctx, s->pb, ctx->length_code);
00513 }
00514
00515 if (ctx->extra_bswap ^ (ctx->spdif_flags & SPDIF_FLAG_BIGENDIAN)) {
00516 avio_write(s->pb, ctx->out_buf, ctx->out_bytes & ~1);
00517 } else {
00518 av_fast_malloc(&ctx->buffer, &ctx->buffer_size, ctx->out_bytes + FF_INPUT_BUFFER_PADDING_SIZE);
00519 if (!ctx->buffer)
00520 return AVERROR(ENOMEM);
00521 ff_spdif_bswap_buf16((uint16_t *)ctx->buffer, (uint16_t *)ctx->out_buf, ctx->out_bytes >> 1);
00522 avio_write(s->pb, ctx->buffer, ctx->out_bytes & ~1);
00523 }
00524
00525
00526 if (ctx->out_bytes & 1)
00527 spdif_put_16(ctx, s->pb, ctx->out_buf[ctx->out_bytes - 1] << 8);
00528
00529 ffio_fill(s->pb, 0, padding);
00530
00531 av_log(s, AV_LOG_DEBUG, "type=%x len=%i pkt_offset=%i\n",
00532 ctx->data_type, ctx->out_bytes, ctx->pkt_offset);
00533
00534 put_flush_packet(s->pb);
00535 return 0;
00536 }
00537
00538 AVOutputFormat ff_spdif_muxer = {
00539 "spdif",
00540 NULL_IF_CONFIG_SMALL("IEC 61937 (used on S/PDIF - IEC958)"),
00541 NULL,
00542 "spdif",
00543 sizeof(IEC61937Context),
00544 CODEC_ID_AC3,
00545 CODEC_ID_NONE,
00546 spdif_write_header,
00547 spdif_write_packet,
00548 spdif_write_trailer,
00549 .flags = AVFMT_NOTIMESTAMPS,
00550 .priv_class = &class,
00551 };