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