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