00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "libavutil/intreadwrite.h"
00023 #include "avcodec.h"
00024 #include "adx.h"
00025 #include "get_bits.h"
00026
00036 static av_cold int adx_decode_init(AVCodecContext *avctx)
00037 {
00038 ADXContext *c = avctx->priv_data;
00039 int ret, header_size;
00040
00041 if (avctx->extradata_size >= 24) {
00042 if ((ret = avpriv_adx_decode_header(avctx, avctx->extradata,
00043 avctx->extradata_size, &header_size,
00044 c->coeff)) < 0) {
00045 av_log(avctx, AV_LOG_ERROR, "error parsing ADX header\n");
00046 return AVERROR_INVALIDDATA;
00047 }
00048 c->channels = avctx->channels;
00049 }
00050
00051 avctx->sample_fmt = AV_SAMPLE_FMT_S16;
00052
00053 avcodec_get_frame_defaults(&c->frame);
00054 avctx->coded_frame = &c->frame;
00055
00056 return 0;
00057 }
00058
00066 static int adx_decode(ADXContext *c, int16_t *out, const uint8_t *in, int ch)
00067 {
00068 ADXChannelState *prev = &c->prev[ch];
00069 GetBitContext gb;
00070 int scale = AV_RB16(in);
00071 int i;
00072 int s0, s1, s2, d;
00073
00074
00075 if (scale & 0x8000)
00076 return -1;
00077
00078 init_get_bits(&gb, in + 2, (BLOCK_SIZE - 2) * 8);
00079 s1 = prev->s1;
00080 s2 = prev->s2;
00081 for (i = 0; i < BLOCK_SAMPLES; i++) {
00082 d = get_sbits(&gb, 4);
00083 s0 = ((d << COEFF_BITS) * scale + c->coeff[0] * s1 + c->coeff[1] * s2) >> COEFF_BITS;
00084 s2 = s1;
00085 s1 = av_clip_int16(s0);
00086 *out = s1;
00087 out += c->channels;
00088 }
00089 prev->s1 = s1;
00090 prev->s2 = s2;
00091
00092 return 0;
00093 }
00094
00095 static int adx_decode_frame(AVCodecContext *avctx, void *data,
00096 int *got_frame_ptr, AVPacket *avpkt)
00097 {
00098 int buf_size = avpkt->size;
00099 ADXContext *c = avctx->priv_data;
00100 int16_t *samples;
00101 const uint8_t *buf = avpkt->data;
00102 int num_blocks, ch, ret;
00103
00104 if (c->eof) {
00105 *got_frame_ptr = 0;
00106 return buf_size;
00107 }
00108
00109 if(AV_RB16(buf) == 0x8000){
00110 int header_size;
00111 if ((ret = avpriv_adx_decode_header(avctx, buf,
00112 buf_size, &header_size,
00113 c->coeff)) < 0) {
00114 av_log(avctx, AV_LOG_ERROR, "error parsing ADX header\n");
00115 return AVERROR_INVALIDDATA;
00116 }
00117 c->channels = avctx->channels;
00118 if(buf_size < header_size)
00119 return AVERROR_INVALIDDATA;
00120 buf += header_size;
00121 buf_size -= header_size;
00122 }
00123 if(c->channels <= 0)
00124 return AVERROR_INVALIDDATA;
00125
00126
00127 num_blocks = buf_size / (BLOCK_SIZE * c->channels);
00128
00129
00130
00131 if (!num_blocks || buf_size % (BLOCK_SIZE * avctx->channels)) {
00132 if (buf_size >= 4 && (AV_RB16(buf) & 0x8000)) {
00133 c->eof = 1;
00134 *got_frame_ptr = 0;
00135 return avpkt->size;
00136 }
00137 return AVERROR_INVALIDDATA;
00138 }
00139
00140
00141 c->frame.nb_samples = num_blocks * BLOCK_SAMPLES;
00142 if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0) {
00143 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00144 return ret;
00145 }
00146 samples = (int16_t *)c->frame.data[0];
00147
00148 while (num_blocks--) {
00149 for (ch = 0; ch < c->channels; ch++) {
00150 if (adx_decode(c, samples + ch, buf, ch)) {
00151 c->eof = 1;
00152 buf = avpkt->data + avpkt->size;
00153 break;
00154 }
00155 buf_size -= BLOCK_SIZE;
00156 buf += BLOCK_SIZE;
00157 }
00158 samples += BLOCK_SAMPLES * c->channels;
00159 }
00160
00161 *got_frame_ptr = 1;
00162 *(AVFrame *)data = c->frame;
00163
00164 return buf - avpkt->data;
00165 }
00166
00167 AVCodec ff_adpcm_adx_decoder = {
00168 .name = "adpcm_adx",
00169 .type = AVMEDIA_TYPE_AUDIO,
00170 .id = CODEC_ID_ADPCM_ADX,
00171 .priv_data_size = sizeof(ADXContext),
00172 .init = adx_decode_init,
00173 .decode = adx_decode_frame,
00174 .capabilities = CODEC_CAP_DR1,
00175 .long_name = NULL_IF_CONFIG_SMALL("SEGA CRI ADX ADPCM"),
00176 };