]> git.sesse.net Git - ffmpeg/blob - libavcodec/adxdec.c
Merge remote-tracking branch 'shariman/wmall'
[ffmpeg] / libavcodec / adxdec.c
1 /*
2  * ADX ADPCM codecs
3  * Copyright (c) 2001,2003 BERO
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 #include "libavutil/intreadwrite.h"
23 #include "avcodec.h"
24 #include "adx.h"
25 #include "get_bits.h"
26
27 /**
28  * @file
29  * SEGA CRI adx codecs.
30  *
31  * Reference documents:
32  * http://ku-www.ss.titech.ac.jp/~yatsushi/adx.html
33  * adx2wav & wav2adx http://www.geocities.co.jp/Playtown/2004/
34  */
35
36 static av_cold int adx_decode_init(AVCodecContext *avctx)
37 {
38     ADXContext *c = avctx->priv_data;
39     int ret, header_size;
40
41     if (avctx->extradata_size >= 24) {
42         if ((ret = avpriv_adx_decode_header(avctx, avctx->extradata,
43                                             avctx->extradata_size, &header_size,
44                                             c->coeff)) < 0) {
45             av_log(avctx, AV_LOG_ERROR, "error parsing ADX header\n");
46             return AVERROR_INVALIDDATA;
47         }
48         c->channels = avctx->channels;
49     }
50
51     avctx->sample_fmt = AV_SAMPLE_FMT_S16;
52
53     avcodec_get_frame_defaults(&c->frame);
54     avctx->coded_frame = &c->frame;
55
56     return 0;
57 }
58
59 /**
60  * Decode 32 samples from 18 bytes.
61  *
62  * A 16-bit scalar value is applied to 32 residuals, which then have a
63  * 2nd-order LPC filter applied to it to form the output signal for a single
64  * channel.
65  */
66 static int adx_decode(ADXContext *c, int16_t *out, const uint8_t *in, int ch)
67 {
68     ADXChannelState *prev = &c->prev[ch];
69     GetBitContext gb;
70     int scale = AV_RB16(in);
71     int i;
72     int s0, s1, s2, d;
73
74     /* check if this is an EOF packet */
75     if (scale & 0x8000)
76         return -1;
77
78     init_get_bits(&gb, in + 2, (BLOCK_SIZE - 2) * 8);
79     s1 = prev->s1;
80     s2 = prev->s2;
81     for (i = 0; i < BLOCK_SAMPLES; i++) {
82         d  = get_sbits(&gb, 4);
83         s0 = ((d << COEFF_BITS) * scale + c->coeff[0] * s1 + c->coeff[1] * s2) >> COEFF_BITS;
84         s2 = s1;
85         s1 = av_clip_int16(s0);
86         *out = s1;
87         out += c->channels;
88     }
89     prev->s1 = s1;
90     prev->s2 = s2;
91
92     return 0;
93 }
94
95 static int adx_decode_frame(AVCodecContext *avctx, void *data,
96                             int *got_frame_ptr, AVPacket *avpkt)
97 {
98     int buf_size        = avpkt->size;
99     ADXContext *c       = avctx->priv_data;
100     int16_t *samples;
101     const uint8_t *buf  = avpkt->data;
102     int num_blocks, ch, ret;
103
104     if (c->eof) {
105         *got_frame_ptr = 0;
106         return buf_size;
107     }
108
109     if(AV_RB16(buf) == 0x8000){
110         int header_size;
111         if ((ret = avpriv_adx_decode_header(avctx, buf,
112                                             buf_size, &header_size,
113                                             c->coeff)) < 0) {
114             av_log(avctx, AV_LOG_ERROR, "error parsing ADX header\n");
115             return AVERROR_INVALIDDATA;
116         }
117         c->channels = avctx->channels;
118         if(buf_size < header_size)
119             return AVERROR_INVALIDDATA;
120         buf += header_size;
121         buf_size -= header_size;
122     }
123     if(c->channels <= 0)
124         return AVERROR_INVALIDDATA;
125
126     /* calculate number of blocks in the packet */
127     num_blocks = buf_size / (BLOCK_SIZE * c->channels);
128
129     /* if the packet is not an even multiple of BLOCK_SIZE, check for an EOF
130        packet */
131     if (!num_blocks || buf_size % (BLOCK_SIZE * avctx->channels)) {
132         if (buf_size >= 4 && (AV_RB16(buf) & 0x8000)) {
133             c->eof = 1;
134             *got_frame_ptr = 0;
135             return avpkt->size;
136         }
137         return AVERROR_INVALIDDATA;
138     }
139
140     /* get output buffer */
141     c->frame.nb_samples = num_blocks * BLOCK_SAMPLES;
142     if ((ret = avctx->get_buffer(avctx, &c->frame)) < 0) {
143         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
144         return ret;
145     }
146     samples = (int16_t *)c->frame.data[0];
147
148     while (num_blocks--) {
149         for (ch = 0; ch < c->channels; ch++) {
150             if (adx_decode(c, samples + ch, buf, ch)) {
151                 c->eof = 1;
152                 buf = avpkt->data + avpkt->size;
153                 break;
154             }
155             buf_size -= BLOCK_SIZE;
156             buf      += BLOCK_SIZE;
157         }
158         samples += BLOCK_SAMPLES * c->channels;
159     }
160
161     *got_frame_ptr   = 1;
162     *(AVFrame *)data = c->frame;
163
164     return buf - avpkt->data;
165 }
166
167 AVCodec ff_adpcm_adx_decoder = {
168     .name           = "adpcm_adx",
169     .type           = AVMEDIA_TYPE_AUDIO,
170     .id             = CODEC_ID_ADPCM_ADX,
171     .priv_data_size = sizeof(ADXContext),
172     .init           = adx_decode_init,
173     .decode         = adx_decode_frame,
174     .capabilities   = CODEC_CAP_DR1,
175     .long_name      = NULL_IF_CONFIG_SMALL("SEGA CRI ADX ADPCM"),
176 };