Line | Branch | Exec | Source |
---|---|---|---|
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 "codec_internal.h" | ||
26 | #include "decode.h" | ||
27 | #include "get_bits.h" | ||
28 | |||
29 | /** | ||
30 | * @file | ||
31 | * SEGA CRI adx codecs. | ||
32 | * | ||
33 | * Reference documents: | ||
34 | * http://ku-www.ss.titech.ac.jp/~yatsushi/adx.html | ||
35 | * adx2wav & wav2adx http://www.geocities.co.jp/Playtown/2004/ | ||
36 | */ | ||
37 | |||
38 | /** | ||
39 | * Decode ADX stream header. | ||
40 | * Sets avctx->channels and avctx->sample_rate. | ||
41 | * | ||
42 | * @param avctx codec context | ||
43 | * @param buf header data | ||
44 | * @param bufsize data size, should be at least 24 bytes | ||
45 | * @param[out] header_size size of ADX header | ||
46 | * @param[out] coeff 2 LPC coefficients, can be NULL | ||
47 | * @return data offset or negative error code if header is invalid | ||
48 | */ | ||
49 | 4 | static int adx_decode_header(AVCodecContext *avctx, const uint8_t *buf, | |
50 | int bufsize, int *header_size, int *coeff) | ||
51 | { | ||
52 | int offset, cutoff, channels; | ||
53 | |||
54 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (bufsize < 24) |
55 | ✗ | return AVERROR_INVALIDDATA; | |
56 | |||
57 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (AV_RB16(buf) != 0x8000) |
58 | ✗ | return AVERROR_INVALIDDATA; | |
59 | 4 | offset = AV_RB16(buf + 2) + 4; | |
60 | |||
61 | /* if copyright string is within the provided data, validate it */ | ||
62 |
3/6✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
|
4 | if (bufsize >= offset && offset >= 6 && memcmp(buf + offset - 6, "(c)CRI", 6)) |
63 | ✗ | return AVERROR_INVALIDDATA; | |
64 | |||
65 | /* check for encoding=3 block_size=18, sample_size=4 */ | ||
66 |
3/6✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 4 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
✓ Branch 5 taken 4 times.
|
4 | if (buf[4] != 3 || buf[5] != 18 || buf[6] != 4) { |
67 | ✗ | avpriv_request_sample(avctx, "Support for this ADX format"); | |
68 | ✗ | return AVERROR_PATCHWELCOME; | |
69 | } | ||
70 | |||
71 | /* channels */ | ||
72 | 4 | channels = buf[7]; | |
73 |
2/4✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 4 times.
|
4 | if (channels <= 0 || channels > MAX_CHANNELS) |
74 | ✗ | return AVERROR_INVALIDDATA; | |
75 | |||
76 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | if (avctx->ch_layout.nb_channels != channels) { |
77 | ✗ | av_channel_layout_uninit(&avctx->ch_layout); | |
78 | ✗ | avctx->ch_layout.order = AV_CHANNEL_ORDER_UNSPEC; | |
79 | ✗ | avctx->ch_layout.nb_channels = channels; | |
80 | } | ||
81 | |||
82 | /* sample rate */ | ||
83 | 4 | avctx->sample_rate = AV_RB32(buf + 8); | |
84 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | if (avctx->sample_rate < 1 || |
85 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 4 times.
|
4 | avctx->sample_rate > INT_MAX / (channels * BLOCK_SIZE * 8)) |
86 | ✗ | return AVERROR_INVALIDDATA; | |
87 | |||
88 | /* bit rate */ | ||
89 | 4 | avctx->bit_rate = avctx->sample_rate * channels * BLOCK_SIZE * 8 / BLOCK_SAMPLES; | |
90 | |||
91 | /* LPC coefficients */ | ||
92 |
1/2✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
|
4 | if (coeff) { |
93 | 4 | cutoff = AV_RB16(buf + 16); | |
94 | 4 | ff_adx_calculate_coeffs(cutoff, avctx->sample_rate, COEFF_BITS, coeff); | |
95 | } | ||
96 | |||
97 | 4 | *header_size = offset; | |
98 | 4 | return 0; | |
99 | } | ||
100 | |||
101 | 4 | static av_cold int adx_decode_init(AVCodecContext *avctx) | |
102 | { | ||
103 | 4 | ADXContext *c = avctx->priv_data; | |
104 | int ret, header_size; | ||
105 | |||
106 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | if (avctx->extradata_size >= 24) { |
107 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if ((ret = adx_decode_header(avctx, avctx->extradata, |
108 | avctx->extradata_size, &header_size, | ||
109 | 2 | c->coeff)) < 0) { | |
110 | ✗ | av_log(avctx, AV_LOG_ERROR, "error parsing ADX header\n"); | |
111 | ✗ | return AVERROR_INVALIDDATA; | |
112 | } | ||
113 | 2 | c->channels = avctx->ch_layout.nb_channels; | |
114 | 2 | c->header_parsed = 1; | |
115 | } | ||
116 | |||
117 | 4 | avctx->sample_fmt = AV_SAMPLE_FMT_S16P; | |
118 | |||
119 | 4 | return 0; | |
120 | } | ||
121 | |||
122 | /** | ||
123 | * Decode 32 samples from 18 bytes. | ||
124 | * | ||
125 | * A 16-bit scalar value is applied to 32 residuals, which then have a | ||
126 | * 2nd-order LPC filter applied to it to form the output signal for a single | ||
127 | * channel. | ||
128 | */ | ||
129 | 16798 | static int adx_decode(ADXContext *c, int16_t *out, int offset, | |
130 | const uint8_t *in, int ch) | ||
131 | { | ||
132 | 16798 | ADXChannelState *prev = &c->prev[ch]; | |
133 | GetBitContext gb; | ||
134 | 16798 | int scale = AV_RB16(in); | |
135 | int i; | ||
136 | int s0, s1, s2, d; | ||
137 | |||
138 | /* check if this is an EOF packet */ | ||
139 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 16798 times.
|
16798 | if (scale & 0x8000) |
140 | ✗ | return -1; | |
141 | |||
142 | 16798 | init_get_bits(&gb, in + 2, (BLOCK_SIZE - 2) * 8); | |
143 | 16798 | out += offset; | |
144 | 16798 | s1 = prev->s1; | |
145 | 16798 | s2 = prev->s2; | |
146 |
2/2✓ Branch 0 taken 537536 times.
✓ Branch 1 taken 16798 times.
|
554334 | for (i = 0; i < BLOCK_SAMPLES; i++) { |
147 | 537536 | d = get_sbits(&gb, 4); | |
148 | 537536 | s0 = d * scale + ((c->coeff[0] * s1 + c->coeff[1] * s2) >> COEFF_BITS); | |
149 | 537536 | s2 = s1; | |
150 | 537536 | s1 = av_clip_int16(s0); | |
151 | 537536 | *out++ = s1; | |
152 | } | ||
153 | 16798 | prev->s1 = s1; | |
154 | 16798 | prev->s2 = s2; | |
155 | |||
156 | 16798 | return 0; | |
157 | } | ||
158 | |||
159 | 68 | static int adx_decode_frame(AVCodecContext *avctx, AVFrame *frame, | |
160 | int *got_frame_ptr, AVPacket *avpkt) | ||
161 | { | ||
162 | 68 | int buf_size = avpkt->size; | |
163 | 68 | ADXContext *c = avctx->priv_data; | |
164 | int16_t **samples; | ||
165 | int samples_offset; | ||
166 | 68 | const uint8_t *buf = avpkt->data; | |
167 | 68 | const uint8_t *buf_end = buf + avpkt->size; | |
168 | int num_blocks, ch, ret; | ||
169 | size_t new_extradata_size; | ||
170 | uint8_t *new_extradata; | ||
171 | |||
172 | 68 | new_extradata = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA, | |
173 | &new_extradata_size); | ||
174 |
1/4✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
|
68 | if (new_extradata && new_extradata_size > 0) { |
175 | int header_size; | ||
176 | ✗ | if ((ret = adx_decode_header(avctx, new_extradata, | |
177 | new_extradata_size, &header_size, | ||
178 | ✗ | c->coeff)) < 0) { | |
179 | ✗ | av_log(avctx, AV_LOG_ERROR, "error parsing new ADX extradata\n"); | |
180 | ✗ | return AVERROR_INVALIDDATA; | |
181 | } | ||
182 | |||
183 | ✗ | c->eof = 0; | |
184 | } | ||
185 | |||
186 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
|
68 | if (c->eof) { |
187 | ✗ | *got_frame_ptr = 0; | |
188 | ✗ | return buf_size; | |
189 | } | ||
190 | |||
191 |
4/6✓ Branch 0 taken 2 times.
✓ Branch 1 taken 66 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
|
68 | if (!c->header_parsed && buf_size >= 2 && AV_RB16(buf) == 0x8000) { |
192 | int header_size; | ||
193 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if ((ret = adx_decode_header(avctx, buf, buf_size, &header_size, |
194 | 2 | c->coeff)) < 0) { | |
195 | ✗ | av_log(avctx, AV_LOG_ERROR, "error parsing ADX header\n"); | |
196 | ✗ | return AVERROR_INVALIDDATA; | |
197 | } | ||
198 | 2 | c->channels = avctx->ch_layout.nb_channels; | |
199 | 2 | c->header_parsed = 1; | |
200 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (buf_size < header_size) |
201 | ✗ | return AVERROR_INVALIDDATA; | |
202 | 2 | buf += header_size; | |
203 | 2 | buf_size -= header_size; | |
204 | } | ||
205 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 68 times.
|
68 | if (!c->header_parsed) |
206 | ✗ | return AVERROR_INVALIDDATA; | |
207 | |||
208 | /* calculate number of blocks in the packet */ | ||
209 | 68 | num_blocks = buf_size / (BLOCK_SIZE * c->channels); | |
210 | |||
211 | /* if the packet is not an even multiple of BLOCK_SIZE, check for an EOF | ||
212 | packet */ | ||
213 |
2/4✓ Branch 0 taken 68 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 68 times.
|
68 | if (!num_blocks || buf_size % (BLOCK_SIZE * c->channels)) { |
214 | ✗ | if (buf_size >= 4 && (AV_RB16(buf) & 0x8000)) { | |
215 | ✗ | c->eof = 1; | |
216 | ✗ | *got_frame_ptr = 0; | |
217 | ✗ | return avpkt->size; | |
218 | } | ||
219 | ✗ | return AVERROR_INVALIDDATA; | |
220 | } | ||
221 | |||
222 | /* get output buffer */ | ||
223 | 68 | frame->nb_samples = num_blocks * BLOCK_SAMPLES; | |
224 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 68 times.
|
68 | if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
225 | ✗ | return ret; | |
226 | 68 | samples = (int16_t **)frame->extended_data; | |
227 | 68 | samples_offset = 0; | |
228 | |||
229 |
2/2✓ Branch 0 taken 8399 times.
✓ Branch 1 taken 68 times.
|
8467 | while (num_blocks--) { |
230 |
2/2✓ Branch 0 taken 16798 times.
✓ Branch 1 taken 8399 times.
|
25197 | for (ch = 0; ch < c->channels; ch++) { |
231 |
2/4✓ Branch 0 taken 16798 times.
✗ Branch 1 not taken.
✗ Branch 3 not taken.
✓ Branch 4 taken 16798 times.
|
16798 | if (buf_end - buf < BLOCK_SIZE || adx_decode(c, samples[ch], samples_offset, buf, ch)) { |
232 | ✗ | c->eof = 1; | |
233 | ✗ | buf = avpkt->data + avpkt->size; | |
234 | ✗ | break; | |
235 | } | ||
236 | 16798 | buf_size -= BLOCK_SIZE; | |
237 | 16798 | buf += BLOCK_SIZE; | |
238 | } | ||
239 |
1/2✓ Branch 0 taken 8399 times.
✗ Branch 1 not taken.
|
8399 | if (!c->eof) |
240 | 8399 | samples_offset += BLOCK_SAMPLES; | |
241 | } | ||
242 | |||
243 | 68 | frame->nb_samples = samples_offset; | |
244 | 68 | *got_frame_ptr = 1; | |
245 | |||
246 | 68 | return buf - avpkt->data; | |
247 | } | ||
248 | |||
249 | ✗ | static void adx_decode_flush(AVCodecContext *avctx) | |
250 | { | ||
251 | ✗ | ADXContext *c = avctx->priv_data; | |
252 | ✗ | memset(c->prev, 0, sizeof(c->prev)); | |
253 | ✗ | c->eof = 0; | |
254 | ✗ | } | |
255 | |||
256 | const FFCodec ff_adpcm_adx_decoder = { | ||
257 | .p.name = "adpcm_adx", | ||
258 | CODEC_LONG_NAME("SEGA CRI ADX ADPCM"), | ||
259 | .p.type = AVMEDIA_TYPE_AUDIO, | ||
260 | .p.id = AV_CODEC_ID_ADPCM_ADX, | ||
261 | .priv_data_size = sizeof(ADXContext), | ||
262 | .init = adx_decode_init, | ||
263 | FF_CODEC_DECODE_CB(adx_decode_frame), | ||
264 | .flush = adx_decode_flush, | ||
265 | .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF | | ||
266 | AV_CODEC_CAP_DR1, | ||
267 | .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16P, | ||
268 | AV_SAMPLE_FMT_NONE }, | ||
269 | }; | ||
270 |