00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023 #include "libavutil/intreadwrite.h"
00024 #include "avcodec.h"
00025
00026 #define AES3_HEADER_LEN 4
00027
00028 typedef struct S302MDecodeContext {
00029 AVFrame frame;
00030 } S302MDecodeContext;
00031
00032 static int s302m_parse_frame_header(AVCodecContext *avctx, const uint8_t *buf,
00033 int buf_size)
00034 {
00035 uint32_t h;
00036 int frame_size, channels, bits;
00037
00038 if (buf_size <= AES3_HEADER_LEN) {
00039 av_log(avctx, AV_LOG_ERROR, "frame is too short\n");
00040 return AVERROR_INVALIDDATA;
00041 }
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052 h = AV_RB32(buf);
00053 frame_size = (h >> 16) & 0xffff;
00054 channels = ((h >> 14) & 0x0003) * 2 + 2;
00055 bits = ((h >> 4) & 0x0003) * 4 + 16;
00056
00057 if (AES3_HEADER_LEN + frame_size != buf_size || bits > 24) {
00058 av_log(avctx, AV_LOG_ERROR, "frame has invalid header\n");
00059 return AVERROR_INVALIDDATA;
00060 }
00061
00062
00063 avctx->bits_per_coded_sample = bits;
00064 if (bits > 16)
00065 avctx->sample_fmt = AV_SAMPLE_FMT_S32;
00066 else
00067 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00068
00069 avctx->channels = channels;
00070 switch(channels) {
00071 case 2:
00072 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
00073 break;
00074 case 4:
00075 avctx->channel_layout = AV_CH_LAYOUT_QUAD;
00076 break;
00077 case 8:
00078 avctx->channel_layout = AV_CH_LAYOUT_5POINT1_BACK | AV_CH_LAYOUT_STEREO_DOWNMIX;
00079 }
00080 avctx->sample_rate = 48000;
00081 avctx->bit_rate = 48000 * avctx->channels * (avctx->bits_per_coded_sample + 4) +
00082 32 * (48000 / (buf_size * 8 /
00083 (avctx->channels *
00084 (avctx->bits_per_coded_sample + 4))));
00085
00086 return frame_size;
00087 }
00088
00089 static int s302m_decode_frame(AVCodecContext *avctx, void *data,
00090 int *got_frame_ptr, AVPacket *avpkt)
00091 {
00092 S302MDecodeContext *s = avctx->priv_data;
00093 const uint8_t *buf = avpkt->data;
00094 int buf_size = avpkt->size;
00095 int block_size, ret;
00096
00097 int frame_size = s302m_parse_frame_header(avctx, buf, buf_size);
00098 if (frame_size < 0)
00099 return frame_size;
00100
00101 buf_size -= AES3_HEADER_LEN;
00102 buf += AES3_HEADER_LEN;
00103
00104
00105 block_size = (avctx->bits_per_coded_sample + 4) / 4;
00106 s->frame.nb_samples = 2 * (buf_size / block_size) / avctx->channels;
00107 if ((ret = avctx->get_buffer(avctx, &s->frame)) < 0) {
00108 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00109 return ret;
00110 }
00111
00112 buf_size = (s->frame.nb_samples * avctx->channels / 2) * block_size;
00113
00114 if (avctx->bits_per_coded_sample == 24) {
00115 uint32_t *o = (uint32_t *)s->frame.data[0];
00116 for (; buf_size > 6; buf_size -= 7) {
00117 *o++ = (av_reverse[buf[2]] << 24) |
00118 (av_reverse[buf[1]] << 16) |
00119 (av_reverse[buf[0]] << 8);
00120 *o++ = (av_reverse[buf[6] & 0xf0] << 28) |
00121 (av_reverse[buf[5]] << 20) |
00122 (av_reverse[buf[4]] << 12) |
00123 (av_reverse[buf[3] & 0x0f] << 4);
00124 buf += 7;
00125 }
00126 } else if (avctx->bits_per_coded_sample == 20) {
00127 uint32_t *o = (uint32_t *)s->frame.data[0];
00128 for (; buf_size > 5; buf_size -= 6) {
00129 *o++ = (av_reverse[buf[2] & 0xf0] << 28) |
00130 (av_reverse[buf[1]] << 20) |
00131 (av_reverse[buf[0]] << 12);
00132 *o++ = (av_reverse[buf[5] & 0xf0] << 28) |
00133 (av_reverse[buf[4]] << 20) |
00134 (av_reverse[buf[3]] << 12);
00135 buf += 6;
00136 }
00137 } else {
00138 uint16_t *o = (uint16_t *)s->frame.data[0];
00139 for (; buf_size > 4; buf_size -= 5) {
00140 *o++ = (av_reverse[buf[1]] << 8) |
00141 av_reverse[buf[0]];
00142 *o++ = (av_reverse[buf[4] & 0xf0] << 12) |
00143 (av_reverse[buf[3]] << 4) |
00144 (av_reverse[buf[2]] >> 4);
00145 buf += 5;
00146 }
00147 }
00148
00149 *got_frame_ptr = 1;
00150 *(AVFrame *)data = s->frame;
00151
00152 return avpkt->size;
00153 }
00154
00155 static int s302m_decode_init(AVCodecContext *avctx)
00156 {
00157 S302MDecodeContext *s = avctx->priv_data;
00158
00159 avcodec_get_frame_defaults(&s->frame);
00160 avctx->coded_frame = &s->frame;
00161
00162 return 0;
00163 }
00164
00165
00166 AVCodec ff_s302m_decoder = {
00167 .name = "s302m",
00168 .type = AVMEDIA_TYPE_AUDIO,
00169 .id = CODEC_ID_S302M,
00170 .priv_data_size = sizeof(S302MDecodeContext),
00171 .init = s302m_decode_init,
00172 .decode = s302m_decode_frame,
00173 .capabilities = CODEC_CAP_DR1,
00174 .long_name = NULL_IF_CONFIG_SMALL("SMPTE 302M"),
00175 };