Go to the documentation of this file.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 #include "internal.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 avctx->sample_rate = 48000;
00072 avctx->bit_rate = 48000 * avctx->channels * (avctx->bits_per_coded_sample + 4) +
00073 32 * (48000 / (buf_size * 8 /
00074 (avctx->channels *
00075 (avctx->bits_per_coded_sample + 4))));
00076
00077 return frame_size;
00078 }
00079
00080 static int s302m_decode_frame(AVCodecContext *avctx, void *data,
00081 int *got_frame_ptr, AVPacket *avpkt)
00082 {
00083 S302MDecodeContext *s = avctx->priv_data;
00084 const uint8_t *buf = avpkt->data;
00085 int buf_size = avpkt->size;
00086 int block_size, ret;
00087
00088 int frame_size = s302m_parse_frame_header(avctx, buf, buf_size);
00089 if (frame_size < 0)
00090 return frame_size;
00091
00092 buf_size -= AES3_HEADER_LEN;
00093 buf += AES3_HEADER_LEN;
00094
00095
00096 block_size = (avctx->bits_per_coded_sample + 4) / 4;
00097 s->frame.nb_samples = 2 * (buf_size / block_size) / avctx->channels;
00098 if ((ret = ff_get_buffer(avctx, &s->frame)) < 0) {
00099 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00100 return ret;
00101 }
00102
00103 buf_size = (s->frame.nb_samples * avctx->channels / 2) * block_size;
00104
00105 if (avctx->bits_per_coded_sample == 24) {
00106 uint32_t *o = (uint32_t *)s->frame.data[0];
00107 for (; buf_size > 6; buf_size -= 7) {
00108 *o++ = (av_reverse[buf[2]] << 24) |
00109 (av_reverse[buf[1]] << 16) |
00110 (av_reverse[buf[0]] << 8);
00111 *o++ = (av_reverse[buf[6] & 0xf0] << 28) |
00112 (av_reverse[buf[5]] << 20) |
00113 (av_reverse[buf[4]] << 12) |
00114 (av_reverse[buf[3] & 0x0f] << 4);
00115 buf += 7;
00116 }
00117 } else if (avctx->bits_per_coded_sample == 20) {
00118 uint32_t *o = (uint32_t *)s->frame.data[0];
00119 for (; buf_size > 5; buf_size -= 6) {
00120 *o++ = (av_reverse[buf[2] & 0xf0] << 28) |
00121 (av_reverse[buf[1]] << 20) |
00122 (av_reverse[buf[0]] << 12);
00123 *o++ = (av_reverse[buf[5] & 0xf0] << 28) |
00124 (av_reverse[buf[4]] << 20) |
00125 (av_reverse[buf[3]] << 12);
00126 buf += 6;
00127 }
00128 } else {
00129 uint16_t *o = (uint16_t *)s->frame.data[0];
00130 for (; buf_size > 4; buf_size -= 5) {
00131 *o++ = (av_reverse[buf[1]] << 8) |
00132 av_reverse[buf[0]];
00133 *o++ = (av_reverse[buf[4] & 0xf0] << 12) |
00134 (av_reverse[buf[3]] << 4) |
00135 (av_reverse[buf[2]] >> 4);
00136 buf += 5;
00137 }
00138 }
00139
00140 *got_frame_ptr = 1;
00141 *(AVFrame *)data = s->frame;
00142
00143 return avpkt->size;
00144 }
00145
00146 static int s302m_decode_init(AVCodecContext *avctx)
00147 {
00148 S302MDecodeContext *s = avctx->priv_data;
00149
00150 avcodec_get_frame_defaults(&s->frame);
00151 avctx->coded_frame = &s->frame;
00152
00153 return 0;
00154 }
00155
00156
00157 AVCodec ff_s302m_decoder = {
00158 .name = "s302m",
00159 .type = AVMEDIA_TYPE_AUDIO,
00160 .id = CODEC_ID_S302M,
00161 .priv_data_size = sizeof(S302MDecodeContext),
00162 .init = s302m_decode_init,
00163 .decode = s302m_decode_frame,
00164 .capabilities = CODEC_CAP_DR1,
00165 .long_name = NULL_IF_CONFIG_SMALL("SMPTE 302M"),
00166 };