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