00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043 #include "avformat.h"
00044 #include "libavcodec/ac3.h"
00045 #include "libavcodec/dca.h"
00046 #include "libavcodec/aac_parser.h"
00047
00048 #define SYNCWORD1 0xF872
00049 #define SYNCWORD2 0x4E1F
00050 #define BURST_HEADER_SIZE 0x8
00051
00052 enum IEC958DataType {
00053 IEC958_AC3 = 0x01,
00054 IEC958_MPEG1_LAYER1 = 0x04,
00055 IEC958_MPEG1_LAYER23 = 0x05,
00056 IEC958_MPEG2_EXT = 0x06,
00057 IEC958_MPEG2_AAC = 0x07,
00058 IEC958_MPEG2_LAYER1_LSF = 0x08,
00059 IEC958_MPEG2_LAYER2_LSF = 0x09,
00060 IEC958_MPEG2_LAYER3_LSF = 0x0A,
00061 IEC958_DTS1 = 0x0B,
00062 IEC958_DTS2 = 0x0C,
00063 IEC958_DTS3 = 0x0D,
00064 IEC958_MPEG2_AAC_LSF_2048 = 0x13,
00065 IEC958_MPEG2_AAC_LSF_4096 = 0x13 | 0x20,
00066 };
00067
00068 typedef struct IEC958Context {
00069 enum IEC958DataType data_type;
00070 int pkt_size;
00071 int pkt_offset;
00072 uint8_t *buffer;
00073 int buffer_size;
00074
00077 int (*header_info) (AVFormatContext *s, AVPacket *pkt);
00078 } IEC958Context;
00079
00080
00081 static void bswap_buf16(uint16_t *dst, const uint16_t *src, int w)
00082 {
00083 int i;
00084
00085 for (i = 0; i + 8 <= w; i += 8) {
00086 dst[i + 0] = bswap_16(src[i + 0]);
00087 dst[i + 1] = bswap_16(src[i + 1]);
00088 dst[i + 2] = bswap_16(src[i + 2]);
00089 dst[i + 3] = bswap_16(src[i + 3]);
00090 dst[i + 4] = bswap_16(src[i + 4]);
00091 dst[i + 5] = bswap_16(src[i + 5]);
00092 dst[i + 6] = bswap_16(src[i + 6]);
00093 dst[i + 7] = bswap_16(src[i + 7]);
00094 }
00095 for (; i < w; i++)
00096 dst[i + 0] = bswap_16(src[i + 0]);
00097 }
00098
00099 static int spdif_header_ac3(AVFormatContext *s, AVPacket *pkt)
00100 {
00101 IEC958Context *ctx = s->priv_data;
00102 int bitstream_mode = pkt->data[6] & 0x7;
00103
00104 ctx->data_type = IEC958_AC3 | (bitstream_mode << 8);
00105 ctx->pkt_offset = AC3_FRAME_SIZE << 2;
00106 return 0;
00107 }
00108
00109 static int spdif_header_dts(AVFormatContext *s, AVPacket *pkt)
00110 {
00111 IEC958Context *ctx = s->priv_data;
00112 uint32_t syncword_dts = AV_RB32(pkt->data);
00113 int blocks;
00114
00115 switch (syncword_dts) {
00116 case DCA_MARKER_RAW_BE:
00117 blocks = (AV_RB16(pkt->data + 4) >> 2) & 0x7f;
00118 break;
00119 case DCA_MARKER_RAW_LE:
00120 blocks = (AV_RL16(pkt->data + 4) >> 2) & 0x7f;
00121 break;
00122 case DCA_MARKER_14B_BE:
00123 blocks =
00124 (((pkt->data[5] & 0x07) << 4) | ((pkt->data[6] & 0x3f) >> 2));
00125 break;
00126 case DCA_MARKER_14B_LE:
00127 blocks =
00128 (((pkt->data[4] & 0x07) << 4) | ((pkt->data[7] & 0x3f) >> 2));
00129 break;
00130 default:
00131 av_log(s, AV_LOG_ERROR, "bad DTS syncword 0x%x\n", syncword_dts);
00132 return -1;
00133 }
00134 blocks++;
00135 switch (blocks) {
00136 case 512 >> 5: ctx->data_type = IEC958_DTS1; break;
00137 case 1024 >> 5: ctx->data_type = IEC958_DTS2; break;
00138 case 2048 >> 5: ctx->data_type = IEC958_DTS3; break;
00139 default:
00140 av_log(s, AV_LOG_ERROR, "%i samples in DTS frame not supported\n",
00141 blocks << 5);
00142 return -1;
00143 }
00144 ctx->pkt_offset = blocks << 7;
00145
00146 return 0;
00147 }
00148
00149 static const enum IEC958DataType mpeg_data_type[2][3] = {
00150
00151 { IEC958_MPEG2_LAYER1_LSF, IEC958_MPEG2_LAYER2_LSF, IEC958_MPEG2_LAYER3_LSF },
00152 { IEC958_MPEG1_LAYER1, IEC958_MPEG1_LAYER23, IEC958_MPEG1_LAYER23 },
00153 };
00154
00155 static const uint16_t mpeg_pkt_offset[2][3] = {
00156
00157 { 3072, 9216, 4608 },
00158 { 1536, 4608, 4608 },
00159 };
00160
00161 static int spdif_header_mpeg(AVFormatContext *s, AVPacket *pkt)
00162 {
00163 IEC958Context *ctx = s->priv_data;
00164 int version = (pkt->data[1] >> 3) & 3;
00165 int layer = 3 - ((pkt->data[1] >> 1) & 3);
00166 int extension = pkt->data[2] & 1;
00167
00168 if (layer == 3 || version == 1) {
00169 av_log(s, AV_LOG_ERROR, "Wrong MPEG file format\n");
00170 return -1;
00171 }
00172 av_log(s, AV_LOG_DEBUG, "version: %i layer: %i extension: %i\n", version, layer, extension);
00173 if (version == 2 && extension) {
00174 ctx->data_type = IEC958_MPEG2_EXT;
00175 ctx->pkt_offset = 4608;
00176 } else {
00177 ctx->data_type = mpeg_data_type [version & 1][layer];
00178 ctx->pkt_offset = mpeg_pkt_offset[version & 1][layer];
00179 }
00180
00181 return 0;
00182 }
00183
00184 static int spdif_header_aac(AVFormatContext *s, AVPacket *pkt)
00185 {
00186 IEC958Context *ctx = s->priv_data;
00187 AACADTSHeaderInfo hdr;
00188 GetBitContext gbc;
00189 int ret;
00190
00191 init_get_bits(&gbc, pkt->data, AAC_ADTS_HEADER_SIZE * 8);
00192 ret = ff_aac_parse_header(&gbc, &hdr);
00193 if (ret < 0) {
00194 av_log(s, AV_LOG_ERROR, "Wrong AAC file format\n");
00195 return -1;
00196 }
00197
00198 ctx->pkt_offset = hdr.samples << 2;
00199 switch (hdr.num_aac_frames) {
00200 case 1:
00201 ctx->data_type = IEC958_MPEG2_AAC;
00202 break;
00203 case 2:
00204 ctx->data_type = IEC958_MPEG2_AAC_LSF_2048;
00205 break;
00206 case 4:
00207 ctx->data_type = IEC958_MPEG2_AAC_LSF_4096;
00208 break;
00209 default:
00210 av_log(s, AV_LOG_ERROR, "%i samples in AAC frame not supported\n",
00211 hdr.samples);
00212 return -1;
00213 }
00214
00215 return 0;
00216 }
00217
00218 static int spdif_write_header(AVFormatContext *s)
00219 {
00220 IEC958Context *ctx = s->priv_data;
00221
00222 switch (s->streams[0]->codec->codec_id) {
00223 case CODEC_ID_AC3:
00224 ctx->header_info = spdif_header_ac3;
00225 break;
00226 case CODEC_ID_MP1:
00227 case CODEC_ID_MP2:
00228 case CODEC_ID_MP3:
00229 ctx->header_info = spdif_header_mpeg;
00230 break;
00231 case CODEC_ID_DTS:
00232 ctx->header_info = spdif_header_dts;
00233 break;
00234 case CODEC_ID_AAC:
00235 ctx->header_info = spdif_header_aac;
00236 break;
00237 default:
00238 av_log(s, AV_LOG_ERROR, "codec not supported\n");
00239 return -1;
00240 }
00241 return 0;
00242 }
00243
00244 static int spdif_write_trailer(AVFormatContext *s)
00245 {
00246 IEC958Context *ctx = s->priv_data;
00247 av_freep(&ctx->buffer);
00248 return 0;
00249 }
00250
00251 static int spdif_write_packet(struct AVFormatContext *s, AVPacket *pkt)
00252 {
00253 IEC958Context *ctx = s->priv_data;
00254 int ret, padding;
00255
00256 ctx->pkt_size = FFALIGN(pkt->size, 2) << 3;
00257 ret = ctx->header_info(s, pkt);
00258 if (ret < 0)
00259 return -1;
00260
00261 padding = (ctx->pkt_offset - BURST_HEADER_SIZE - pkt->size) >> 1;
00262 if (padding < 0) {
00263 av_log(s, AV_LOG_ERROR, "bitrate is too high\n");
00264 return -1;
00265 }
00266
00267 put_le16(s->pb, SYNCWORD1);
00268 put_le16(s->pb, SYNCWORD2);
00269 put_le16(s->pb, ctx->data_type);
00270 put_le16(s->pb, ctx->pkt_size);
00271
00272 #if HAVE_BIGENDIAN
00273 put_buffer(s->pb, pkt->data, pkt->size & ~1);
00274 #else
00275 av_fast_malloc(&ctx->buffer, &ctx->buffer_size, pkt->size + FF_INPUT_BUFFER_PADDING_SIZE);
00276 if (!ctx->buffer)
00277 return AVERROR(ENOMEM);
00278 bswap_buf16((uint16_t *)ctx->buffer, (uint16_t *)pkt->data, pkt->size >> 1);
00279 put_buffer(s->pb, ctx->buffer, pkt->size & ~1);
00280 #endif
00281
00282 if (pkt->size & 1)
00283 put_be16(s->pb, pkt->data[pkt->size - 1]);
00284
00285 for (; padding > 0; padding--)
00286 put_be16(s->pb, 0);
00287
00288 av_log(s, AV_LOG_DEBUG, "type=%x len=%i pkt_offset=%i\n",
00289 ctx->data_type, pkt->size, ctx->pkt_offset);
00290
00291 put_flush_packet(s->pb);
00292 return 0;
00293 }
00294
00295 AVOutputFormat spdif_muxer = {
00296 "spdif",
00297 NULL_IF_CONFIG_SMALL("IEC958 - S/PDIF (IEC-61937)"),
00298 NULL,
00299 "spdif",
00300 sizeof(IEC958Context),
00301 CODEC_ID_AC3,
00302 CODEC_ID_NONE,
00303 spdif_write_header,
00304 spdif_write_packet,
00305 spdif_write_trailer,
00306 };