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 static int s302m_parse_frame_header(AVCodecContext *avctx, const uint8_t *buf,
00029 int buf_size)
00030 {
00031 uint32_t h;
00032 int frame_size, channels, bits;
00033
00034 if (buf_size <= AES3_HEADER_LEN) {
00035 av_log(avctx, AV_LOG_ERROR, "frame is too short\n");
00036 return AVERROR_INVALIDDATA;
00037 }
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 h = AV_RB32(buf);
00049 frame_size = (h >> 16) & 0xffff;
00050 channels = ((h >> 14) & 0x0003) * 2 + 2;
00051 bits = ((h >> 4) & 0x0003) * 4 + 16;
00052
00053 if (AES3_HEADER_LEN + frame_size != buf_size || bits > 24) {
00054 av_log(avctx, AV_LOG_ERROR, "frame has invalid header\n");
00055 return AVERROR_INVALIDDATA;
00056 }
00057
00058
00059 avctx->bits_per_coded_sample = bits;
00060 if (bits > 16)
00061 avctx->sample_fmt = SAMPLE_FMT_S32;
00062 else
00063 avctx->sample_fmt = SAMPLE_FMT_S16;
00064
00065 avctx->channels = channels;
00066 switch(channels) {
00067 case 2:
00068 avctx->channel_layout = AV_CH_LAYOUT_STEREO;
00069 break;
00070 case 4:
00071 avctx->channel_layout = AV_CH_LAYOUT_QUAD;
00072 break;
00073 case 8:
00074 avctx->channel_layout = AV_CH_LAYOUT_5POINT1_BACK | AV_CH_LAYOUT_STEREO_DOWNMIX;
00075 }
00076 avctx->sample_rate = 48000;
00077 avctx->bit_rate = 48000 * avctx->channels * (avctx->bits_per_coded_sample + 4) +
00078 32 * (48000 / (buf_size * 8 /
00079 (avctx->channels *
00080 (avctx->bits_per_coded_sample + 4))));
00081
00082 return frame_size;
00083 }
00084
00085 static int s302m_decode_frame(AVCodecContext *avctx, void *data,
00086 int *data_size, AVPacket *avpkt)
00087 {
00088 const uint8_t *buf = avpkt->data;
00089 int buf_size = avpkt->size;
00090
00091 int frame_size = s302m_parse_frame_header(avctx, buf, buf_size);
00092 if (frame_size < 0)
00093 return frame_size;
00094
00095 buf_size -= AES3_HEADER_LEN;
00096 buf += AES3_HEADER_LEN;
00097
00098 if (*data_size < 4 * buf_size * 8 / (avctx->bits_per_coded_sample + 4))
00099 return -1;
00100
00101 if (avctx->bits_per_coded_sample == 24) {
00102 uint32_t *o = data;
00103 for (; buf_size > 6; buf_size -= 7) {
00104 *o++ = (av_reverse[buf[2]] << 24) |
00105 (av_reverse[buf[1]] << 16) |
00106 (av_reverse[buf[0]] << 8);
00107 *o++ = (av_reverse[buf[6] & 0xf0] << 28) |
00108 (av_reverse[buf[5]] << 20) |
00109 (av_reverse[buf[4]] << 12) |
00110 (av_reverse[buf[3] & 0x0f] << 4);
00111 buf += 7;
00112 }
00113 *data_size = (uint8_t*) o - (uint8_t*) data;
00114 } else if (avctx->bits_per_coded_sample == 20) {
00115 uint32_t *o = data;
00116 for (; buf_size > 5; buf_size -= 6) {
00117 *o++ = (av_reverse[buf[2] & 0xf0] << 28) |
00118 (av_reverse[buf[1]] << 20) |
00119 (av_reverse[buf[0]] << 12);
00120 *o++ = (av_reverse[buf[5] & 0xf0] << 28) |
00121 (av_reverse[buf[4]] << 20) |
00122 (av_reverse[buf[3]] << 12);
00123 buf += 6;
00124 }
00125 *data_size = (uint8_t*) o - (uint8_t*) data;
00126 } else {
00127 uint16_t *o = data;
00128 for (; buf_size > 4; buf_size -= 5) {
00129 *o++ = (av_reverse[buf[1]] << 8) |
00130 av_reverse[buf[0]];
00131 *o++ = (av_reverse[buf[4] & 0xf0] << 12) |
00132 (av_reverse[buf[3]] << 4) |
00133 (av_reverse[buf[2]] >> 4);
00134 buf += 5;
00135 }
00136 *data_size = (uint8_t*) o - (uint8_t*) data;
00137 }
00138
00139 return buf - avpkt->data;
00140 }
00141
00142
00143 AVCodec ff_s302m_decoder = {
00144 .name = "s302m",
00145 .type = AVMEDIA_TYPE_AUDIO,
00146 .id = CODEC_ID_S302M,
00147 .priv_data_size = 0,
00148 .decode = s302m_decode_frame,
00149 .long_name = NULL_IF_CONFIG_SMALL("SMPTE 302M"),
00150 };