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 #include "internal.h"
00027
00037 static av_cold int adx_decode_init(AVCodecContext *avctx)
00038 {
00039 ADXContext *c = avctx->priv_data;
00040 int ret, header_size;
00041
00042 if (avctx->extradata_size >= 24) {
00043 if ((ret = avpriv_adx_decode_header(avctx, avctx->extradata,
00044 avctx->extradata_size, &header_size,
00045 c->coeff)) < 0) {
00046 av_log(avctx, AV_LOG_ERROR, "error parsing ADX header\n");
00047 return AVERROR_INVALIDDATA;
00048 }
00049 c->channels = avctx->channels;
00050 c->header_parsed = 1;
00051 }
00052
00053 avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
00054
00055 avcodec_get_frame_defaults(&c->frame);
00056 avctx->coded_frame = &c->frame;
00057
00058 return 0;
00059 }
00060
00068 static int adx_decode(ADXContext *c, int16_t *out, int offset,
00069 const uint8_t *in, int ch)
00070 {
00071 ADXChannelState *prev = &c->prev[ch];
00072 GetBitContext gb;
00073 int scale = AV_RB16(in);
00074 int i;
00075 int s0, s1, s2, d;
00076
00077
00078 if (scale & 0x8000)
00079 return -1;
00080
00081 init_get_bits(&gb, in + 2, (BLOCK_SIZE - 2) * 8);
00082 out += offset;
00083 s1 = prev->s1;
00084 s2 = prev->s2;
00085 for (i = 0; i < BLOCK_SAMPLES; i++) {
00086 d = get_sbits(&gb, 4);
00087 s0 = ((d << COEFF_BITS) * scale + c->coeff[0] * s1 + c->coeff[1] * s2) >> COEFF_BITS;
00088 s2 = s1;
00089 s1 = av_clip_int16(s0);
00090 *out++ = s1;
00091 }
00092 prev->s1 = s1;
00093 prev->s2 = s2;
00094
00095 return 0;
00096 }
00097
00098 static int adx_decode_frame(AVCodecContext *avctx, void *data,
00099 int *got_frame_ptr, AVPacket *avpkt)
00100 {
00101 int buf_size = avpkt->size;
00102 ADXContext *c = avctx->priv_data;
00103 int16_t **samples;
00104 int samples_offset;
00105 const uint8_t *buf = avpkt->data;
00106 const uint8_t *buf_end = buf + avpkt->size;
00107 int num_blocks, ch, ret;
00108
00109 if (c->eof) {
00110 *got_frame_ptr = 0;
00111 return buf_size;
00112 }
00113
00114 if (!c->header_parsed && buf_size >= 2 && AV_RB16(buf) == 0x8000) {
00115 int header_size;
00116 if ((ret = avpriv_adx_decode_header(avctx, buf, buf_size, &header_size,
00117 c->coeff)) < 0) {
00118 av_log(avctx, AV_LOG_ERROR, "error parsing ADX header\n");
00119 return AVERROR_INVALIDDATA;
00120 }
00121 c->channels = avctx->channels;
00122 c->header_parsed = 1;
00123 if (buf_size < header_size)
00124 return AVERROR_INVALIDDATA;
00125 buf += header_size;
00126 buf_size -= header_size;
00127 }
00128 if (!c->header_parsed)
00129 return AVERROR_INVALIDDATA;
00130
00131
00132 num_blocks = buf_size / (BLOCK_SIZE * c->channels);
00133
00134
00135
00136 if (!num_blocks || buf_size % (BLOCK_SIZE * avctx->channels)) {
00137 if (buf_size >= 4 && (AV_RB16(buf) & 0x8000)) {
00138 c->eof = 1;
00139 *got_frame_ptr = 0;
00140 return avpkt->size;
00141 }
00142 return AVERROR_INVALIDDATA;
00143 }
00144
00145
00146 c->frame.nb_samples = num_blocks * BLOCK_SAMPLES;
00147 if ((ret = ff_get_buffer(avctx, &c->frame)) < 0) {
00148 av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
00149 return ret;
00150 }
00151 samples = (int16_t **)c->frame.extended_data;
00152 samples_offset = 0;
00153
00154 while (num_blocks--) {
00155 for (ch = 0; ch < c->channels; ch++) {
00156 if (buf_end - buf < BLOCK_SIZE || adx_decode(c, samples[ch], samples_offset, buf, ch)) {
00157 c->eof = 1;
00158 buf = avpkt->data + avpkt->size;
00159 break;
00160 }
00161 buf_size -= BLOCK_SIZE;
00162 buf += BLOCK_SIZE;
00163 }
00164 samples_offset += BLOCK_SAMPLES;
00165 }
00166
00167 *got_frame_ptr = 1;
00168 *(AVFrame *)data = c->frame;
00169
00170 return buf - avpkt->data;
00171 }
00172
00173 static void adx_decode_flush(AVCodecContext *avctx)
00174 {
00175 ADXContext *c = avctx->priv_data;
00176 memset(c->prev, 0, sizeof(c->prev));
00177 c->eof = 0;
00178 }
00179
00180 AVCodec ff_adpcm_adx_decoder = {
00181 .name = "adpcm_adx",
00182 .type = AVMEDIA_TYPE_AUDIO,
00183 .id = AV_CODEC_ID_ADPCM_ADX,
00184 .priv_data_size = sizeof(ADXContext),
00185 .init = adx_decode_init,
00186 .decode = adx_decode_frame,
00187 .flush = adx_decode_flush,
00188 .capabilities = CODEC_CAP_DR1,
00189 .long_name = NULL_IF_CONFIG_SMALL("SEGA CRI ADX ADPCM"),
00190 .sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P,
00191 AV_SAMPLE_FMT_NONE },
00192 };