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), AV_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, AV_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), AV_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), AV_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 
00224     
00225 
00226     ctx->length_code = FFALIGN(ctx->out_bytes + 0x8, 0x10) - 0x8;
00227 
00228     av_fast_malloc(&ctx->hd_buf, &ctx->hd_buf_size, ctx->out_bytes);
00229     if (!ctx->hd_buf)
00230         return AVERROR(ENOMEM);
00231 
00232     ctx->out_buf = ctx->hd_buf;
00233 
00234     memcpy(ctx->hd_buf, dtshd_start_code, sizeof(dtshd_start_code));
00235     AV_WB16(ctx->hd_buf + sizeof(dtshd_start_code), pkt_size);
00236     memcpy(ctx->hd_buf + sizeof(dtshd_start_code) + 2, pkt->data, pkt_size);
00237 
00238     return 0;
00239 }
00240 
00241 static int spdif_header_dts(AVFormatContext *s, AVPacket *pkt)
00242 {
00243     IEC61937Context *ctx = s->priv_data;
00244     uint32_t syncword_dts = AV_RB32(pkt->data);
00245     int blocks;
00246     int sample_rate = 0;
00247     int core_size = 0;
00248 
00249     if (pkt->size < 9)
00250         return AVERROR_INVALIDDATA;
00251 
00252     switch (syncword_dts) {
00253     case DCA_MARKER_RAW_BE:
00254         blocks = (AV_RB16(pkt->data + 4) >> 2) & 0x7f;
00255         core_size = ((AV_RB24(pkt->data + 5) >> 4) & 0x3fff) + 1;
00256         sample_rate = dca_sample_rates[(pkt->data[8] >> 2) & 0x0f];
00257         break;
00258     case DCA_MARKER_RAW_LE:
00259         blocks = (AV_RL16(pkt->data + 4) >> 2) & 0x7f;
00260         ctx->extra_bswap = 1;
00261         break;
00262     case DCA_MARKER_14B_BE:
00263         blocks =
00264             (((pkt->data[5] & 0x07) << 4) | ((pkt->data[6] & 0x3f) >> 2));
00265         break;
00266     case DCA_MARKER_14B_LE:
00267         blocks =
00268             (((pkt->data[4] & 0x07) << 4) | ((pkt->data[7] & 0x3f) >> 2));
00269         ctx->extra_bswap = 1;
00270         break;
00271     case DCA_HD_MARKER:
00272         
00273 
00274 
00275         av_log(s, AV_LOG_ERROR, "stray DTS-HD frame\n");
00276         return AVERROR_INVALIDDATA;
00277     default:
00278         av_log(s, AV_LOG_ERROR, "bad DTS syncword 0x%x\n", syncword_dts);
00279         return AVERROR_INVALIDDATA;
00280     }
00281     blocks++;
00282 
00283     if (ctx->dtshd_rate)
00284         
00285         return spdif_header_dts4(s, pkt, core_size, sample_rate, blocks);
00286 
00287     switch (blocks) {
00288     case  512 >> 5: ctx->data_type = IEC61937_DTS1; break;
00289     case 1024 >> 5: ctx->data_type = IEC61937_DTS2; break;
00290     case 2048 >> 5: ctx->data_type = IEC61937_DTS3; break;
00291     default:
00292         av_log(s, AV_LOG_ERROR, "%i samples in DTS frame not supported\n",
00293                blocks << 5);
00294         return AVERROR(ENOSYS);
00295     }
00296 
00297     
00298     if (core_size && core_size < pkt->size) {
00299         ctx->out_bytes = core_size;
00300         ctx->length_code = core_size << 3;
00301     }
00302 
00303     ctx->pkt_offset = blocks << 7;
00304 
00305     if (ctx->out_bytes == ctx->pkt_offset) {
00306         
00307 
00308 
00309         ctx->use_preamble = 0;
00310     } else if (ctx->out_bytes > ctx->pkt_offset - BURST_HEADER_SIZE) {
00311         av_log_ask_for_sample(s, "Unrecognized large DTS frame.");
00312         
00313     }
00314 
00315     return 0;
00316 }
00317 
00318 static const enum IEC61937DataType mpeg_data_type[2][3] = {
00319     
00320     { IEC61937_MPEG2_LAYER1_LSF, IEC61937_MPEG2_LAYER2_LSF, IEC61937_MPEG2_LAYER3_LSF },
00321     { IEC61937_MPEG1_LAYER1,     IEC61937_MPEG1_LAYER23,    IEC61937_MPEG1_LAYER23 },   
00322 };
00323 
00324 static int spdif_header_mpeg(AVFormatContext *s, AVPacket *pkt)
00325 {
00326     IEC61937Context *ctx = s->priv_data;
00327     int version =      (pkt->data[1] >> 3) & 3;
00328     int layer   = 3 - ((pkt->data[1] >> 1) & 3);
00329     int extension = pkt->data[2] & 1;
00330 
00331     if (layer == 3 || version == 1) {
00332         av_log(s, AV_LOG_ERROR, "Wrong MPEG file format\n");
00333         return AVERROR_INVALIDDATA;
00334     }
00335     av_log(s, AV_LOG_DEBUG, "version: %i layer: %i extension: %i\n", version, layer, extension);
00336     if (version == 2 && extension) {
00337         ctx->data_type  = IEC61937_MPEG2_EXT;
00338         ctx->pkt_offset = 4608;
00339     } else {
00340         ctx->data_type  = mpeg_data_type [version & 1][layer];
00341         ctx->pkt_offset = spdif_mpeg_pkt_offset[version & 1][layer];
00342     }
00343     
00344     return 0;
00345 }
00346 
00347 static int spdif_header_aac(AVFormatContext *s, AVPacket *pkt)
00348 {
00349     IEC61937Context *ctx = s->priv_data;
00350     AACADTSHeaderInfo hdr;
00351     GetBitContext gbc;
00352     int ret;
00353 
00354     init_get_bits(&gbc, pkt->data, AAC_ADTS_HEADER_SIZE * 8);
00355     ret = avpriv_aac_parse_header(&gbc, &hdr);
00356     if (ret < 0) {
00357         av_log(s, AV_LOG_ERROR, "Wrong AAC file format\n");
00358         return AVERROR_INVALIDDATA;
00359     }
00360 
00361     ctx->pkt_offset = hdr.samples << 2;
00362     switch (hdr.num_aac_frames) {
00363     case 1:
00364         ctx->data_type = IEC61937_MPEG2_AAC;
00365         break;
00366     case 2:
00367         ctx->data_type = IEC61937_MPEG2_AAC_LSF_2048;
00368         break;
00369     case 4:
00370         ctx->data_type = IEC61937_MPEG2_AAC_LSF_4096;
00371         break;
00372     default:
00373         av_log(s, AV_LOG_ERROR, "%i samples in AAC frame not supported\n",
00374                hdr.samples);
00375         return AVERROR(EINVAL);
00376     }
00377     
00378     return 0;
00379 }
00380 
00381 
00382 
00383 
00384 
00385 
00386 
00387 
00388 
00389 
00390 
00391 #define MAT_FRAME_SIZE          61424
00392 #define TRUEHD_FRAME_OFFSET     2560
00393 #define MAT_MIDDLE_CODE_OFFSET  -4
00394 
00395 static int spdif_header_truehd(AVFormatContext *s, AVPacket *pkt)
00396 {
00397     IEC61937Context *ctx = s->priv_data;
00398     int mat_code_length = 0;
00399     const char mat_end_code[16] = { 0xC3, 0xC2, 0xC0, 0xC4, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x97, 0x11 };
00400 
00401     if (!ctx->hd_buf_count) {
00402         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 };
00403         mat_code_length = sizeof(mat_start_code) + BURST_HEADER_SIZE;
00404         memcpy(ctx->hd_buf, mat_start_code, sizeof(mat_start_code));
00405 
00406     } else if (ctx->hd_buf_count == 12) {
00407         const char mat_middle_code[12] = { 0xC3, 0xC1, 0x42, 0x49, 0x3B, 0xFA, 0x82, 0x83, 0x49, 0x80, 0x77, 0xE0 };
00408         mat_code_length = sizeof(mat_middle_code) + MAT_MIDDLE_CODE_OFFSET;
00409         memcpy(&ctx->hd_buf[12 * TRUEHD_FRAME_OFFSET - BURST_HEADER_SIZE + MAT_MIDDLE_CODE_OFFSET],
00410                mat_middle_code, sizeof(mat_middle_code));
00411     }
00412 
00413     if (pkt->size > TRUEHD_FRAME_OFFSET - mat_code_length) {
00414         
00415 
00416         av_log(s, AV_LOG_ERROR, "TrueHD frame too big, %d bytes\n", pkt->size);
00417         av_log_ask_for_sample(s, NULL);
00418         return AVERROR_INVALIDDATA;
00419     }
00420 
00421     memcpy(&ctx->hd_buf[ctx->hd_buf_count * TRUEHD_FRAME_OFFSET - BURST_HEADER_SIZE + mat_code_length],
00422            pkt->data, pkt->size);
00423     memset(&ctx->hd_buf[ctx->hd_buf_count * TRUEHD_FRAME_OFFSET - BURST_HEADER_SIZE + mat_code_length + pkt->size],
00424            0, TRUEHD_FRAME_OFFSET - pkt->size - mat_code_length);
00425 
00426     if (++ctx->hd_buf_count < 24){
00427         ctx->pkt_offset = 0;
00428         return 0;
00429     }
00430     memcpy(&ctx->hd_buf[MAT_FRAME_SIZE - sizeof(mat_end_code)], mat_end_code, sizeof(mat_end_code));
00431     ctx->hd_buf_count = 0;
00432 
00433     ctx->data_type   = IEC61937_TRUEHD;
00434     ctx->pkt_offset  = 61440;
00435     ctx->out_buf     = ctx->hd_buf;
00436     ctx->out_bytes   = MAT_FRAME_SIZE;
00437     ctx->length_code = MAT_FRAME_SIZE;
00438     return 0;
00439 }
00440 
00441 static int spdif_write_header(AVFormatContext *s)
00442 {
00443     IEC61937Context *ctx = s->priv_data;
00444 
00445     switch (s->streams[0]->codec->codec_id) {
00446     case CODEC_ID_AC3:
00447         ctx->header_info = spdif_header_ac3;
00448         break;
00449     case CODEC_ID_EAC3:
00450         ctx->header_info = spdif_header_eac3;
00451         break;
00452     case CODEC_ID_MP1:
00453     case CODEC_ID_MP2:
00454     case CODEC_ID_MP3:
00455         ctx->header_info = spdif_header_mpeg;
00456         break;
00457     case CODEC_ID_DTS:
00458         ctx->header_info = spdif_header_dts;
00459         break;
00460     case CODEC_ID_AAC:
00461         ctx->header_info = spdif_header_aac;
00462         break;
00463     case CODEC_ID_TRUEHD:
00464         ctx->header_info = spdif_header_truehd;
00465         ctx->hd_buf = av_malloc(MAT_FRAME_SIZE);
00466         if (!ctx->hd_buf)
00467             return AVERROR(ENOMEM);
00468         break;
00469     default:
00470         av_log(s, AV_LOG_ERROR, "codec not supported\n");
00471         return AVERROR_PATCHWELCOME;
00472     }
00473     return 0;
00474 }
00475 
00476 static int spdif_write_trailer(AVFormatContext *s)
00477 {
00478     IEC61937Context *ctx = s->priv_data;
00479     av_freep(&ctx->buffer);
00480     av_freep(&ctx->hd_buf);
00481     return 0;
00482 }
00483 
00484 static av_always_inline void spdif_put_16(IEC61937Context *ctx,
00485                                           AVIOContext *pb, unsigned int val)
00486 {
00487     if (ctx->spdif_flags & SPDIF_FLAG_BIGENDIAN)
00488         avio_wb16(pb, val);
00489     else
00490         avio_wl16(pb, val);
00491 }
00492 
00493 static int spdif_write_packet(struct AVFormatContext *s, AVPacket *pkt)
00494 {
00495     IEC61937Context *ctx = s->priv_data;
00496     int ret, padding;
00497 
00498     ctx->out_buf = pkt->data;
00499     ctx->out_bytes = pkt->size;
00500     ctx->length_code = FFALIGN(pkt->size, 2) << 3;
00501     ctx->use_preamble = 1;
00502     ctx->extra_bswap = 0;
00503 
00504     ret = ctx->header_info(s, pkt);
00505     if (ret < 0)
00506         return ret;
00507     if (!ctx->pkt_offset)
00508         return 0;
00509 
00510     padding = (ctx->pkt_offset - ctx->use_preamble * BURST_HEADER_SIZE - ctx->out_bytes) & ~1;
00511     if (padding < 0) {
00512         av_log(s, AV_LOG_ERROR, "bitrate is too high\n");
00513         return AVERROR(EINVAL);
00514     }
00515 
00516     if (ctx->use_preamble) {
00517         spdif_put_16(ctx, s->pb, SYNCWORD1);       
00518         spdif_put_16(ctx, s->pb, SYNCWORD2);       
00519         spdif_put_16(ctx, s->pb, ctx->data_type);  
00520         spdif_put_16(ctx, s->pb, ctx->length_code);
00521     }
00522 
00523     if (ctx->extra_bswap ^ (ctx->spdif_flags & SPDIF_FLAG_BIGENDIAN)) {
00524         avio_write(s->pb, ctx->out_buf, ctx->out_bytes & ~1);
00525     } else {
00526         av_fast_malloc(&ctx->buffer, &ctx->buffer_size, ctx->out_bytes + FF_INPUT_BUFFER_PADDING_SIZE);
00527         if (!ctx->buffer)
00528             return AVERROR(ENOMEM);
00529         ff_spdif_bswap_buf16((uint16_t *)ctx->buffer, (uint16_t *)ctx->out_buf, ctx->out_bytes >> 1);
00530         avio_write(s->pb, ctx->buffer, ctx->out_bytes & ~1);
00531     }
00532 
00533     
00534     if (ctx->out_bytes & 1)
00535         spdif_put_16(ctx, s->pb, ctx->out_buf[ctx->out_bytes - 1] << 8);
00536 
00537     ffio_fill(s->pb, 0, padding);
00538 
00539     av_log(s, AV_LOG_DEBUG, "type=%x len=%i pkt_offset=%i\n",
00540            ctx->data_type, ctx->out_bytes, ctx->pkt_offset);
00541 
00542     avio_flush(s->pb);
00543     return 0;
00544 }
00545 
00546 AVOutputFormat ff_spdif_muxer = {
00547     .name              = "spdif",
00548     .long_name         = NULL_IF_CONFIG_SMALL("IEC 61937 (used on S/PDIF - IEC958)"),
00549     .extensions        = "spdif",
00550     .priv_data_size    = sizeof(IEC61937Context),
00551     .audio_codec       = CODEC_ID_AC3,
00552     .video_codec       = CODEC_ID_NONE,
00553     .write_header      = spdif_write_header,
00554     .write_packet      = spdif_write_packet,
00555     .write_trailer     = spdif_write_trailer,
00556     .flags = AVFMT_NOTIMESTAMPS,
00557     .priv_class = &class,
00558 };