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