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