Line |
Branch |
Exec |
Source |
1 |
|
|
/* |
2 |
|
|
* RKA decoder |
3 |
|
|
* Copyright (c) 2023 Paul B Mahol |
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/channel_layout.h" |
23 |
|
|
#include "libavutil/intreadwrite.h" |
24 |
|
|
|
25 |
|
|
#include "avcodec.h" |
26 |
|
|
#include "codec_internal.h" |
27 |
|
|
#include "bytestream.h" |
28 |
|
|
#include "decode.h" |
29 |
|
|
|
30 |
|
|
typedef struct ACoder { |
31 |
|
|
GetByteContext gb; |
32 |
|
|
uint32_t low, high; |
33 |
|
|
uint32_t value; |
34 |
|
|
} ACoder; |
35 |
|
|
|
36 |
|
|
typedef struct FiltCoeffs { |
37 |
|
|
int32_t coeffs[257]; |
38 |
|
|
unsigned size; |
39 |
|
|
} FiltCoeffs; |
40 |
|
|
|
41 |
|
|
typedef struct Model64 { |
42 |
|
|
uint32_t zero[2]; |
43 |
|
|
uint32_t sign[2]; |
44 |
|
|
unsigned size; |
45 |
|
|
int bits; |
46 |
|
|
|
47 |
|
|
uint16_t val4[65]; |
48 |
|
|
uint16_t val1[65]; |
49 |
|
|
} Model64; |
50 |
|
|
|
51 |
|
|
typedef struct AdaptiveModel { |
52 |
|
|
int last; |
53 |
|
|
int total; |
54 |
|
|
int buf_size; |
55 |
|
|
int16_t sum; |
56 |
|
|
uint16_t aprob0; |
57 |
|
|
uint16_t aprob1; |
58 |
|
|
uint16_t *prob[2]; |
59 |
|
|
} AdaptiveModel; |
60 |
|
|
|
61 |
|
|
typedef struct ChContext { |
62 |
|
|
int cmode; |
63 |
|
|
int cmode2; |
64 |
|
|
int last_nb_decoded; |
65 |
|
|
unsigned srate_pad; |
66 |
|
|
unsigned pos_idx; |
67 |
|
|
|
68 |
|
|
AdaptiveModel *filt_size; |
69 |
|
|
AdaptiveModel *filt_bits; |
70 |
|
|
|
71 |
|
|
uint32_t *bprob[2]; |
72 |
|
|
|
73 |
|
|
AdaptiveModel position; |
74 |
|
|
AdaptiveModel fshift; |
75 |
|
|
AdaptiveModel nb_segments; |
76 |
|
|
AdaptiveModel coeff_bits[11]; |
77 |
|
|
|
78 |
|
|
Model64 mdl64[4][11]; |
79 |
|
|
|
80 |
|
|
int32_t buf0[131072+2560]; |
81 |
|
|
int32_t buf1[131072+2560]; |
82 |
|
|
} ChContext; |
83 |
|
|
|
84 |
|
|
typedef struct RKAContext { |
85 |
|
|
AVClass *class; |
86 |
|
|
|
87 |
|
|
ACoder ac; |
88 |
|
|
ChContext ch[2]; |
89 |
|
|
|
90 |
|
|
int bps; |
91 |
|
|
int align; |
92 |
|
|
int channels; |
93 |
|
|
int correlated; |
94 |
|
|
int frame_samples; |
95 |
|
|
int last_nb_samples; |
96 |
|
|
uint32_t total_nb_samples; |
97 |
|
|
uint32_t samples_left; |
98 |
|
|
|
99 |
|
|
uint32_t bprob[2][257]; |
100 |
|
|
|
101 |
|
|
AdaptiveModel filt_size; |
102 |
|
|
AdaptiveModel filt_bits; |
103 |
|
|
} RKAContext; |
104 |
|
|
|
105 |
|
✗ |
static int adaptive_model_init(AdaptiveModel *am, int buf_size) |
106 |
|
|
{ |
107 |
|
✗ |
am->buf_size = buf_size; |
108 |
|
✗ |
am->sum = 2000; |
109 |
|
✗ |
am->aprob0 = 0; |
110 |
|
✗ |
am->aprob1 = 0; |
111 |
|
✗ |
am->total = 0; |
112 |
|
|
|
113 |
|
✗ |
if (!am->prob[0]) |
114 |
|
✗ |
am->prob[0] = av_malloc_array(buf_size + 5, sizeof(*am->prob[0])); |
115 |
|
✗ |
if (!am->prob[1]) |
116 |
|
✗ |
am->prob[1] = av_malloc_array(buf_size + 5, sizeof(*am->prob[1])); |
117 |
|
|
|
118 |
|
✗ |
if (!am->prob[0] || !am->prob[1]) |
119 |
|
✗ |
return AVERROR(ENOMEM); |
120 |
|
✗ |
memset(am->prob[0], 0, (buf_size + 5) * sizeof(*am->prob[0])); |
121 |
|
✗ |
memset(am->prob[1], 0, (buf_size + 5) * sizeof(*am->prob[1])); |
122 |
|
✗ |
return 0; |
123 |
|
|
} |
124 |
|
|
|
125 |
|
✗ |
static void adaptive_model_free(AdaptiveModel *am) |
126 |
|
|
{ |
127 |
|
✗ |
av_freep(&am->prob[0]); |
128 |
|
✗ |
av_freep(&am->prob[1]); |
129 |
|
|
} |
130 |
|
|
|
131 |
|
✗ |
static av_cold int rka_decode_init(AVCodecContext *avctx) |
132 |
|
|
{ |
133 |
|
✗ |
RKAContext *s = avctx->priv_data; |
134 |
|
|
int cmode; |
135 |
|
|
|
136 |
|
✗ |
if (avctx->extradata_size < 16) |
137 |
|
✗ |
return AVERROR_INVALIDDATA; |
138 |
|
|
|
139 |
|
✗ |
s->bps = avctx->bits_per_raw_sample = avctx->extradata[13]; |
140 |
|
|
|
141 |
|
✗ |
switch (s->bps) { |
142 |
|
✗ |
case 8: |
143 |
|
✗ |
avctx->sample_fmt = AV_SAMPLE_FMT_U8P; |
144 |
|
✗ |
break; |
145 |
|
✗ |
case 16: |
146 |
|
✗ |
avctx->sample_fmt = AV_SAMPLE_FMT_S16P; |
147 |
|
✗ |
break; |
148 |
|
✗ |
default: |
149 |
|
✗ |
return AVERROR_INVALIDDATA; |
150 |
|
|
} |
151 |
|
|
|
152 |
|
✗ |
av_channel_layout_uninit(&avctx->ch_layout); |
153 |
|
✗ |
s->channels = avctx->ch_layout.nb_channels = avctx->extradata[12]; |
154 |
|
✗ |
if (s->channels < 1 || s->channels > 2) |
155 |
|
✗ |
return AVERROR_INVALIDDATA; |
156 |
|
|
|
157 |
|
✗ |
s->align = (s->channels * (avctx->bits_per_raw_sample >> 3)); |
158 |
|
✗ |
s->samples_left = s->total_nb_samples = (AV_RL32(avctx->extradata + 4)) / s->align; |
159 |
|
✗ |
s->frame_samples = 131072 / s->align; |
160 |
|
✗ |
s->last_nb_samples = s->total_nb_samples % s->frame_samples; |
161 |
|
✗ |
s->correlated = avctx->extradata[15] & 1; |
162 |
|
|
|
163 |
|
✗ |
cmode = avctx->extradata[14] & 0xf; |
164 |
|
✗ |
if ((avctx->extradata[15] & 4) != 0) |
165 |
|
✗ |
cmode = -cmode; |
166 |
|
|
|
167 |
|
✗ |
s->ch[0].cmode = s->ch[1].cmode = cmode < 0 ? 2 : cmode; |
168 |
|
✗ |
s->ch[0].cmode2 = cmode < 0 ? FFABS(cmode) : 0; |
169 |
|
✗ |
s->ch[1].cmode2 = cmode < 0 ? FFABS(cmode) : 0; |
170 |
|
✗ |
av_log(avctx, AV_LOG_DEBUG, "cmode: %d\n", cmode); |
171 |
|
|
|
172 |
|
✗ |
return 0; |
173 |
|
|
} |
174 |
|
|
|
175 |
|
✗ |
static void model64_init(Model64 *m, unsigned bits) |
176 |
|
|
{ |
177 |
|
|
unsigned x; |
178 |
|
|
|
179 |
|
✗ |
m->bits = bits; |
180 |
|
✗ |
m->size = 64; |
181 |
|
✗ |
m->zero[0] = 1; |
182 |
|
|
|
183 |
|
✗ |
x = (1 << (bits >> 1)) + 3; |
184 |
|
✗ |
x = FFMIN(x, 20); |
185 |
|
|
|
186 |
|
✗ |
m->zero[1] = x; |
187 |
|
✗ |
m->sign[0] = 1; |
188 |
|
✗ |
m->sign[1] = 1; |
189 |
|
|
|
190 |
|
✗ |
for (int i = 0; i < FF_ARRAY_ELEMS(m->val4); i++) { |
191 |
|
✗ |
m->val4[i] = 4; |
192 |
|
✗ |
m->val1[i] = 1; |
193 |
|
|
} |
194 |
|
|
} |
195 |
|
|
|
196 |
|
✗ |
static int chctx_init(RKAContext *s, ChContext *c, |
197 |
|
|
int sample_rate, int bps) |
198 |
|
|
{ |
199 |
|
|
int ret; |
200 |
|
|
|
201 |
|
✗ |
memset(c->buf0, 0, sizeof(c->buf0)); |
202 |
|
✗ |
memset(c->buf1, 0, sizeof(c->buf1)); |
203 |
|
|
|
204 |
|
✗ |
c->filt_size = &s->filt_size; |
205 |
|
✗ |
c->filt_bits = &s->filt_bits; |
206 |
|
|
|
207 |
|
✗ |
c->bprob[0] = s->bprob[0]; |
208 |
|
✗ |
c->bprob[1] = s->bprob[1]; |
209 |
|
|
|
210 |
|
✗ |
c->srate_pad = (sample_rate << 13) / 44100 & 0xFFFFFFFCU; |
211 |
|
✗ |
c->pos_idx = 1; |
212 |
|
|
|
213 |
|
✗ |
for (int i = 0; i < FF_ARRAY_ELEMS(s->bprob[0]); i++) |
214 |
|
✗ |
c->bprob[0][i] = c->bprob[1][i] = 1; |
215 |
|
|
|
216 |
|
✗ |
for (int i = 0; i < 11; i++) { |
217 |
|
✗ |
ret = adaptive_model_init(&c->coeff_bits[i], 32); |
218 |
|
✗ |
if (ret < 0) |
219 |
|
✗ |
return ret; |
220 |
|
|
|
221 |
|
✗ |
model64_init(&c->mdl64[0][i], i); |
222 |
|
✗ |
model64_init(&c->mdl64[1][i], i); |
223 |
|
✗ |
model64_init(&c->mdl64[2][i], i+1); |
224 |
|
✗ |
model64_init(&c->mdl64[3][i], i+1); |
225 |
|
|
} |
226 |
|
|
|
227 |
|
✗ |
ret = adaptive_model_init(c->filt_size, 256); |
228 |
|
✗ |
if (ret < 0) |
229 |
|
✗ |
return ret; |
230 |
|
✗ |
ret = adaptive_model_init(c->filt_bits, 16); |
231 |
|
✗ |
if (ret < 0) |
232 |
|
✗ |
return ret; |
233 |
|
✗ |
ret = adaptive_model_init(&c->position, 16); |
234 |
|
✗ |
if (ret < 0) |
235 |
|
✗ |
return ret; |
236 |
|
✗ |
ret = adaptive_model_init(&c->nb_segments, 8); |
237 |
|
✗ |
if (ret < 0) |
238 |
|
✗ |
return ret; |
239 |
|
✗ |
return adaptive_model_init(&c->fshift, 32); |
240 |
|
|
} |
241 |
|
|
|
242 |
|
✗ |
static void init_acoder(ACoder *ac) |
243 |
|
|
{ |
244 |
|
✗ |
ac->low = 0x0; |
245 |
|
✗ |
ac->high = 0xffffffff; |
246 |
|
✗ |
ac->value = bytestream2_get_be32(&ac->gb); |
247 |
|
|
} |
248 |
|
|
|
249 |
|
✗ |
static int ac_decode_bool(ACoder *ac, int freq1, int freq2) |
250 |
|
|
{ |
251 |
|
|
unsigned help, add, high, value; |
252 |
|
|
int low; |
253 |
|
|
|
254 |
|
✗ |
low = ac->low; |
255 |
|
✗ |
help = ac->high / (unsigned)(freq2 + freq1); |
256 |
|
✗ |
value = ac->value; |
257 |
|
✗ |
add = freq1 * help; |
258 |
|
✗ |
ac->high = help; |
259 |
|
|
|
260 |
|
✗ |
if (value - low >= add) { |
261 |
|
✗ |
ac->low = low = add + low; |
262 |
|
✗ |
ac->high = high = freq2 * help; |
263 |
|
|
while (1) { |
264 |
|
✗ |
if ((low ^ (high + low)) > 0xFFFFFF) { |
265 |
|
✗ |
if (high > 0xFFFF) |
266 |
|
✗ |
return 1; |
267 |
|
✗ |
ac->high = (uint16_t)-(int16_t)low; |
268 |
|
|
} |
269 |
|
|
|
270 |
|
✗ |
if (bytestream2_get_bytes_left(&ac->gb) <= 0) |
271 |
|
✗ |
break; |
272 |
|
✗ |
ac->value = bytestream2_get_byteu(&ac->gb) | (ac->value << 8); |
273 |
|
✗ |
ac->high = high = ac->high << 8; |
274 |
|
✗ |
low = ac->low = ac->low << 8; |
275 |
|
|
} |
276 |
|
✗ |
return -1; |
277 |
|
|
} |
278 |
|
|
|
279 |
|
✗ |
ac->high = add; |
280 |
|
|
while (1) { |
281 |
|
✗ |
if ((low ^ (add + low)) > 0xFFFFFF) { |
282 |
|
✗ |
if (add > 0xFFFF) |
283 |
|
✗ |
return 0; |
284 |
|
✗ |
ac->high = (uint16_t)-(int16_t)low; |
285 |
|
|
} |
286 |
|
|
|
287 |
|
✗ |
if (bytestream2_get_bytes_left(&ac->gb) <= 0) |
288 |
|
✗ |
break; |
289 |
|
✗ |
ac->value = bytestream2_get_byteu(&ac->gb) | (ac->value << 8); |
290 |
|
✗ |
ac->high = add = ac->high << 8; |
291 |
|
✗ |
low = ac->low = ac->low << 8; |
292 |
|
|
} |
293 |
|
✗ |
return -1; |
294 |
|
|
} |
295 |
|
|
|
296 |
|
✗ |
static int decode_bool(ACoder *ac, ChContext *c, int idx) |
297 |
|
|
{ |
298 |
|
|
uint32_t x; |
299 |
|
|
int b; |
300 |
|
|
|
301 |
|
✗ |
x = c->bprob[0][idx]; |
302 |
|
✗ |
if (x + c->bprob[1][idx] > 4096) { |
303 |
|
✗ |
c->bprob[0][idx] = (x >> 1) + 1; |
304 |
|
✗ |
c->bprob[1][idx] = (c->bprob[1][idx] >> 1) + 1; |
305 |
|
|
} |
306 |
|
|
|
307 |
|
✗ |
b = ac_decode_bool(ac, c->bprob[0][idx], c->bprob[1][idx]); |
308 |
|
✗ |
if (b < 0) |
309 |
|
✗ |
return b; |
310 |
|
|
|
311 |
|
✗ |
c->bprob[b][idx]++; |
312 |
|
|
|
313 |
|
✗ |
return b; |
314 |
|
|
} |
315 |
|
|
|
316 |
|
✗ |
static int ac_get_freq(ACoder *ac, unsigned freq, int *result) |
317 |
|
|
{ |
318 |
|
|
uint32_t new_high; |
319 |
|
|
|
320 |
|
✗ |
if (freq == 0) |
321 |
|
✗ |
return -1; |
322 |
|
|
|
323 |
|
✗ |
new_high = ac->high / freq; |
324 |
|
✗ |
ac->high = new_high; |
325 |
|
|
|
326 |
|
✗ |
if (new_high == 0) |
327 |
|
✗ |
return -1; |
328 |
|
|
|
329 |
|
✗ |
*result = (ac->value - ac->low) / new_high; |
330 |
|
|
|
331 |
|
✗ |
return 0; |
332 |
|
|
} |
333 |
|
|
|
334 |
|
✗ |
static int ac_update(ACoder *ac, int freq, int mul) |
335 |
|
|
{ |
336 |
|
|
uint32_t low, high; |
337 |
|
|
|
338 |
|
✗ |
low = ac->low = ac->high * freq + ac->low; |
339 |
|
✗ |
high = ac->high = ac->high * mul; |
340 |
|
|
|
341 |
|
|
while (1) { |
342 |
|
✗ |
if (((high + low) ^ low) > 0xffffff) { |
343 |
|
✗ |
if (high > 0xffff) |
344 |
|
✗ |
return 0; |
345 |
|
✗ |
ac->high = (uint16_t)-(int16_t)low; |
346 |
|
|
} |
347 |
|
|
|
348 |
|
✗ |
if (bytestream2_get_bytes_left(&ac->gb) <= 0) |
349 |
|
✗ |
break; |
350 |
|
|
|
351 |
|
✗ |
ac->value = (ac->value << 8) | bytestream2_get_byteu(&ac->gb); |
352 |
|
✗ |
low = ac->low = ac->low << 8; |
353 |
|
✗ |
high = ac->high = ac->high << 8; |
354 |
|
|
} |
355 |
|
|
|
356 |
|
✗ |
return -1; |
357 |
|
|
} |
358 |
|
|
|
359 |
|
✗ |
static void amdl_update_prob(AdaptiveModel *am, int val, int diff) |
360 |
|
|
{ |
361 |
|
✗ |
am->aprob0 += diff; |
362 |
|
✗ |
if (val <= 0) { |
363 |
|
✗ |
am->prob[0][0] += diff; |
364 |
|
|
} else { |
365 |
|
|
do { |
366 |
|
✗ |
am->prob[0][val] += diff; |
367 |
|
✗ |
val += (val & -val); |
368 |
|
✗ |
} while (val < am->buf_size); |
369 |
|
|
} |
370 |
|
|
} |
371 |
|
|
|
372 |
|
✗ |
static void update_ch_subobj(AdaptiveModel *am) |
373 |
|
|
{ |
374 |
|
✗ |
int idx2, idx = am->buf_size - 1; |
375 |
|
|
|
376 |
|
✗ |
if (idx >= 0) { |
377 |
|
|
do { |
378 |
|
✗ |
uint16_t *prob = am->prob[0]; |
379 |
|
✗ |
int diff, prob_idx = prob[idx]; |
380 |
|
|
|
381 |
|
✗ |
idx2 = idx - 1; |
382 |
|
✗ |
if (idx > 0) { |
383 |
|
✗ |
int idx3 = idx - 1; |
384 |
|
|
|
385 |
|
✗ |
if ((idx2 & idx) != idx2) { |
386 |
|
|
do { |
387 |
|
✗ |
prob_idx -= prob[idx3]; |
388 |
|
✗ |
idx3 &= idx3 - 1; |
389 |
|
✗ |
} while ((idx2 & idx) != idx3); |
390 |
|
|
} |
391 |
|
|
} |
392 |
|
|
|
393 |
|
✗ |
diff = ((prob_idx > 0) - prob_idx) >> 1; |
394 |
|
✗ |
amdl_update_prob(am, idx, diff); |
395 |
|
✗ |
idx--; |
396 |
|
✗ |
} while (idx2 >= 0); |
397 |
|
|
} |
398 |
|
|
|
399 |
|
✗ |
if (am->sum < 8000) |
400 |
|
✗ |
am->sum += 200; |
401 |
|
|
|
402 |
|
✗ |
am->aprob1 = (am->aprob1 + 1) >> 1; |
403 |
|
|
} |
404 |
|
|
|
405 |
|
✗ |
static int amdl_decode_int(AdaptiveModel *am, ACoder *ac, unsigned *dst, unsigned size) |
406 |
|
|
{ |
407 |
|
|
unsigned freq, size2, val, mul; |
408 |
|
|
int j; |
409 |
|
|
|
410 |
|
✗ |
size = FFMIN(size, am->buf_size - 1); |
411 |
|
|
|
412 |
|
✗ |
if (am->aprob0 >= am->sum) |
413 |
|
✗ |
update_ch_subobj(am); |
414 |
|
|
|
415 |
|
✗ |
if (am->aprob1 && (am->total == am->buf_size || |
416 |
|
✗ |
ac_decode_bool(ac, am->aprob0, am->aprob1) == 0)) { |
417 |
|
✗ |
if (am->total <= 1) { |
418 |
|
✗ |
dst[0] = am->last; |
419 |
|
✗ |
amdl_update_prob(am, dst[0], 1); |
420 |
|
✗ |
return 0; |
421 |
|
|
} |
422 |
|
✗ |
if (size == am->buf_size - 1) { |
423 |
|
✗ |
freq = am->aprob0; |
424 |
|
|
} else { |
425 |
|
✗ |
freq = am->prob[0][0]; |
426 |
|
✗ |
for (int j = size; j > 0; j &= (j - 1) ) |
427 |
|
✗ |
freq += am->prob[0][j]; |
428 |
|
|
} |
429 |
|
✗ |
ac_get_freq(ac, freq, &freq); |
430 |
|
✗ |
size2 = am->buf_size >> 1; |
431 |
|
✗ |
val = am->prob[0][0]; |
432 |
|
✗ |
if (freq >= val) { |
433 |
|
✗ |
int sum = 0; |
434 |
|
✗ |
for (j = freq - val; size2; size2 >>= 1) { |
435 |
|
✗ |
unsigned v = am->prob[0][size2 + sum]; |
436 |
|
✗ |
if (j >= v) { |
437 |
|
✗ |
sum += size2; |
438 |
|
✗ |
j -= v; |
439 |
|
|
} |
440 |
|
|
} |
441 |
|
✗ |
freq -= j; |
442 |
|
✗ |
val = sum + 1; |
443 |
|
|
} else { |
444 |
|
✗ |
freq = 0; |
445 |
|
✗ |
val = 0; |
446 |
|
|
} |
447 |
|
✗ |
dst[0] = val; |
448 |
|
✗ |
mul = am->prob[0][val]; |
449 |
|
✗ |
if (val > 0) { |
450 |
|
✗ |
for (int k = val - 1; (val & (val - 1)) != k; k &= k - 1) |
451 |
|
✗ |
mul -= am->prob[0][k]; |
452 |
|
|
} |
453 |
|
✗ |
ac_update(ac, freq, mul); |
454 |
|
✗ |
amdl_update_prob(am, dst[0], 1); |
455 |
|
✗ |
return 0; |
456 |
|
|
} |
457 |
|
✗ |
am->aprob1++; |
458 |
|
✗ |
if (size == am->buf_size - 1) { |
459 |
|
✗ |
ac_get_freq(ac, am->buf_size - am->total, &val); |
460 |
|
|
} else { |
461 |
|
✗ |
freq = 1; |
462 |
|
✗ |
for (dst[0] = 0; dst[0] < size; dst[0]++) { |
463 |
|
✗ |
if (!am->prob[1][dst[0]]) |
464 |
|
✗ |
freq++; |
465 |
|
|
} |
466 |
|
✗ |
ac_get_freq(ac, freq, &val); |
467 |
|
|
} |
468 |
|
✗ |
freq = 0; |
469 |
|
✗ |
dst[0] = 0; |
470 |
|
✗ |
if (val > 0 && am->buf_size > 0) { |
471 |
|
✗ |
for (dst[0] = 0; dst[0] < size & freq < val; dst[0]++) { |
472 |
|
✗ |
if (!am->prob[1][dst[0]]) |
473 |
|
✗ |
freq++; |
474 |
|
|
} |
475 |
|
|
} |
476 |
|
✗ |
if (am->prob[1][dst[0]]) { |
477 |
|
|
do { |
478 |
|
✗ |
val = dst[0]++; |
479 |
|
✗ |
} while (val + 1 < am->buf_size && am->prob[1][val + 1]); |
480 |
|
|
} |
481 |
|
✗ |
ac_update(ac, freq, 1); |
482 |
|
✗ |
am->prob[1][dst[0]]++; |
483 |
|
✗ |
am->total++; |
484 |
|
✗ |
amdl_update_prob(am, dst[0], 1); |
485 |
|
✗ |
am->last = dst[0]; |
486 |
|
|
|
487 |
|
✗ |
return 0; |
488 |
|
|
} |
489 |
|
|
|
490 |
|
✗ |
static int decode_filt_coeffs(RKAContext *s, ChContext *ctx, ACoder *ac, FiltCoeffs *dst) |
491 |
|
|
{ |
492 |
|
|
unsigned val, bits; |
493 |
|
✗ |
int idx = 0; |
494 |
|
|
|
495 |
|
✗ |
if (amdl_decode_int(ctx->filt_size, ac, &dst->size, 256) < 0) |
496 |
|
✗ |
return -1; |
497 |
|
|
|
498 |
|
✗ |
if (dst->size == 0) |
499 |
|
✗ |
return 0; |
500 |
|
|
|
501 |
|
✗ |
if (amdl_decode_int(ctx->filt_bits, ac, &bits, 10) < 0) |
502 |
|
✗ |
return -1; |
503 |
|
|
|
504 |
|
|
do { |
505 |
|
✗ |
if (((idx == 8) || (idx == 20)) && (0 < bits)) |
506 |
|
✗ |
bits--; |
507 |
|
|
|
508 |
|
✗ |
if (bits > 10) |
509 |
|
✗ |
return -1; |
510 |
|
|
|
511 |
|
✗ |
if (amdl_decode_int(&ctx->coeff_bits[bits], ac, &val, 31) < 0) |
512 |
|
✗ |
return -1; |
513 |
|
|
|
514 |
|
✗ |
if (val == 31) { |
515 |
|
✗ |
ac_get_freq(ac, 65536, &val); |
516 |
|
✗ |
ac_update(ac, val, 1); |
517 |
|
|
} |
518 |
|
|
|
519 |
|
✗ |
if (val == 0) { |
520 |
|
✗ |
dst->coeffs[idx++] = 0; |
521 |
|
|
} else { |
522 |
|
✗ |
unsigned freq = 0; |
523 |
|
|
int sign; |
524 |
|
|
|
525 |
|
✗ |
if (bits > 0) { |
526 |
|
✗ |
ac_get_freq(ac, 1 << bits, &freq); |
527 |
|
✗ |
ac_update(ac, freq, 1); |
528 |
|
|
} |
529 |
|
✗ |
dst->coeffs[idx] = freq + 1 + ((val - 1U) << bits); |
530 |
|
✗ |
sign = decode_bool(ac, ctx, idx); |
531 |
|
✗ |
if (sign < 0) |
532 |
|
✗ |
return -1; |
533 |
|
✗ |
if (sign == 1) |
534 |
|
✗ |
dst->coeffs[idx] = -dst->coeffs[idx]; |
535 |
|
✗ |
idx++; |
536 |
|
|
} |
537 |
|
✗ |
} while (idx < dst->size); |
538 |
|
|
|
539 |
|
✗ |
return 0; |
540 |
|
|
} |
541 |
|
|
|
542 |
|
✗ |
static int ac_dec_bit(ACoder *ac) |
543 |
|
|
{ |
544 |
|
|
uint32_t high, low; |
545 |
|
|
|
546 |
|
✗ |
low = ac->low; |
547 |
|
✗ |
ac->high = high = ac->high >> 1; |
548 |
|
✗ |
if (ac->value - low < high) { |
549 |
|
|
do { |
550 |
|
✗ |
if (((high + low) ^ low) > 0xffffff) { |
551 |
|
✗ |
if (high > 0xffff) |
552 |
|
✗ |
return 0; |
553 |
|
✗ |
ac->high = (uint16_t)-(int16_t)low; |
554 |
|
|
} |
555 |
|
|
|
556 |
|
✗ |
if (bytestream2_get_bytes_left(&ac->gb) <= 0) |
557 |
|
✗ |
break; |
558 |
|
|
|
559 |
|
✗ |
ac->value = (ac->value << 8) | bytestream2_get_byteu(&ac->gb); |
560 |
|
✗ |
ac->high = high = ac->high << 8; |
561 |
|
✗ |
ac->low = low = ac->low << 8; |
562 |
|
|
} while (1); |
563 |
|
|
|
564 |
|
✗ |
return -1; |
565 |
|
|
} |
566 |
|
✗ |
ac->low = low = low + high; |
567 |
|
|
do { |
568 |
|
✗ |
if (((high + low) ^ low) > 0xffffff) { |
569 |
|
✗ |
if (high > 0xffff) |
570 |
|
✗ |
return 1; |
571 |
|
✗ |
ac->high = (uint16_t)-(int16_t)low; |
572 |
|
|
} |
573 |
|
|
|
574 |
|
✗ |
if (bytestream2_get_bytes_left(&ac->gb) <= 0) |
575 |
|
✗ |
break; |
576 |
|
|
|
577 |
|
✗ |
ac->value = (ac->value << 8) | bytestream2_get_byteu(&ac->gb); |
578 |
|
✗ |
ac->high = high = ac->high << 8; |
579 |
|
✗ |
ac->low = low = ac->low << 8; |
580 |
|
|
} while (1); |
581 |
|
|
|
582 |
|
✗ |
return -1; |
583 |
|
|
} |
584 |
|
|
|
585 |
|
✗ |
static int mdl64_decode(ACoder *ac, Model64 *ctx, int *dst) |
586 |
|
|
{ |
587 |
|
|
int sign, idx, bits; |
588 |
|
✗ |
unsigned val = 0; |
589 |
|
|
|
590 |
|
✗ |
if (ctx->zero[0] + ctx->zero[1] > 4000U) { |
591 |
|
✗ |
ctx->zero[0] = (ctx->zero[0] >> 1) + 1; |
592 |
|
✗ |
ctx->zero[1] = (ctx->zero[1] >> 1) + 1; |
593 |
|
|
} |
594 |
|
✗ |
if (ctx->sign[0] + ctx->sign[1] > 4000U) { |
595 |
|
✗ |
ctx->sign[0] = (ctx->sign[0] >> 1) + 1; |
596 |
|
✗ |
ctx->sign[1] = (ctx->sign[1] >> 1) + 1; |
597 |
|
|
} |
598 |
|
✗ |
sign = ac_decode_bool(ac, ctx->zero[0], ctx->zero[1]); |
599 |
|
✗ |
if (sign == 0) { |
600 |
|
✗ |
ctx->zero[0] += 2; |
601 |
|
✗ |
dst[0] = 0; |
602 |
|
✗ |
return 0; |
603 |
|
✗ |
} else if (sign < 0) { |
604 |
|
✗ |
return -1; |
605 |
|
|
} |
606 |
|
|
|
607 |
|
✗ |
ctx->zero[1] += 2; |
608 |
|
✗ |
sign = ac_decode_bool(ac, ctx->sign[0], ctx->sign[1]); |
609 |
|
✗ |
if (sign < 0) |
610 |
|
✗ |
return -1; |
611 |
|
✗ |
ctx->sign[sign]++; |
612 |
|
✗ |
bits = ctx->bits; |
613 |
|
✗ |
if (bits > 0) { |
614 |
|
✗ |
if (bits < 13) { |
615 |
|
✗ |
ac_get_freq(ac, 1 << bits, &val); |
616 |
|
✗ |
ac_update(ac, val, 1); |
617 |
|
|
} else { |
618 |
|
✗ |
int hbits = bits / 2; |
619 |
|
✗ |
ac_get_freq(ac, 1 << hbits, &val); |
620 |
|
✗ |
ac_update(ac, val, 1); |
621 |
|
✗ |
ac_get_freq(ac, 1 << (ctx->bits - (hbits)), &bits); |
622 |
|
✗ |
ac_update(ac, val, 1); |
623 |
|
✗ |
val += (bits << hbits); |
624 |
|
|
} |
625 |
|
|
} |
626 |
|
✗ |
bits = ctx->size; |
627 |
|
✗ |
idx = 0; |
628 |
|
✗ |
if (bits >= 0) { |
629 |
|
|
do { |
630 |
|
✗ |
uint16_t *val4 = ctx->val4; |
631 |
|
|
int b; |
632 |
|
|
|
633 |
|
✗ |
if (val4[idx] + ctx->val1[idx] > 2000U) { |
634 |
|
✗ |
val4[idx] = (val4[idx] >> 1) + 1; |
635 |
|
✗ |
ctx->val1[idx] = (ctx->val1[idx] >> 1) + 1; |
636 |
|
|
} |
637 |
|
✗ |
b = ac_decode_bool(ac, ctx->val4[idx], ctx->val1[idx]); |
638 |
|
✗ |
if (b == 1) { |
639 |
|
✗ |
ctx->val1[idx] += 4; |
640 |
|
✗ |
break; |
641 |
|
✗ |
} else if (b < 0) { |
642 |
|
✗ |
return -1; |
643 |
|
|
} |
644 |
|
✗ |
ctx->val4[idx] += 4; |
645 |
|
✗ |
idx++; |
646 |
|
✗ |
} while (idx <= ctx->size); |
647 |
|
✗ |
bits = ctx->size; |
648 |
|
✗ |
if (idx <= bits) { |
649 |
|
✗ |
dst[0] = val + 1 + (idx << ctx->bits); |
650 |
|
✗ |
if (sign) |
651 |
|
✗ |
dst[0] = -dst[0]; |
652 |
|
✗ |
return 0; |
653 |
|
|
} |
654 |
|
|
} |
655 |
|
✗ |
bits++; |
656 |
|
✗ |
while (ac_dec_bit(ac) == 0) |
657 |
|
✗ |
bits += 64; |
658 |
|
✗ |
ac_get_freq(ac, 64, &idx); |
659 |
|
✗ |
ac_update(ac, idx, 1); |
660 |
|
✗ |
idx += bits; |
661 |
|
✗ |
dst[0] = val + 1 + (idx << ctx->bits); |
662 |
|
✗ |
if (sign) |
663 |
|
✗ |
dst[0] = -dst[0]; |
664 |
|
|
|
665 |
|
✗ |
return 0; |
666 |
|
|
} |
667 |
|
|
|
668 |
|
|
static const uint8_t tab[16] = { |
669 |
|
|
0, 3, 3, 2, 2, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0 |
670 |
|
|
}; |
671 |
|
|
|
672 |
|
✗ |
static int decode_filter(RKAContext *s, ChContext *ctx, ACoder *ac, int off, unsigned size) |
673 |
|
|
{ |
674 |
|
|
FiltCoeffs filt; |
675 |
|
|
Model64 *mdl64; |
676 |
|
✗ |
int m = 0, split, val, last_val = 0, ret; |
677 |
|
✗ |
unsigned idx = 3, bits = 0; |
678 |
|
|
|
679 |
|
✗ |
if (ctx->cmode == 0) { |
680 |
|
✗ |
if (amdl_decode_int(&ctx->fshift, ac, &bits, 15) < 0) |
681 |
|
✗ |
return -1; |
682 |
|
✗ |
bits &= 31U; |
683 |
|
|
} |
684 |
|
|
|
685 |
|
✗ |
ret = decode_filt_coeffs(s, ctx, ac, &filt); |
686 |
|
✗ |
if (ret < 0) |
687 |
|
✗ |
return ret; |
688 |
|
|
|
689 |
|
✗ |
if (size < 512) |
690 |
|
✗ |
split = size / 2; |
691 |
|
|
else |
692 |
|
✗ |
split = size >> 4; |
693 |
|
|
|
694 |
|
✗ |
if (size <= 1) |
695 |
|
✗ |
return 0; |
696 |
|
|
|
697 |
|
✗ |
for (int x = 0; x < size;) { |
698 |
|
✗ |
if (amdl_decode_int(&ctx->position, ac, &idx, 10) < 0) |
699 |
|
✗ |
return -1; |
700 |
|
|
|
701 |
|
✗ |
idx = (ctx->pos_idx + idx) % 11; |
702 |
|
✗ |
ctx->pos_idx = idx; |
703 |
|
|
|
704 |
|
✗ |
for (int y = 0; y < FFMIN(split, size - x); y++, off++) { |
705 |
|
✗ |
int midx, shift = idx, *src, sum = 16; |
706 |
|
|
|
707 |
|
✗ |
if (off >= FF_ARRAY_ELEMS(ctx->buf0)) |
708 |
|
✗ |
return -1; |
709 |
|
|
|
710 |
|
✗ |
midx = FFABS(last_val) >> shift; |
711 |
|
✗ |
if (midx >= 15) { |
712 |
|
✗ |
mdl64 = &ctx->mdl64[3][idx]; |
713 |
|
✗ |
} else if (midx >= 7) { |
714 |
|
✗ |
mdl64 = &ctx->mdl64[2][idx]; |
715 |
|
✗ |
} else if (midx >= 4) { |
716 |
|
✗ |
mdl64 = &ctx->mdl64[1][idx]; |
717 |
|
|
} else { |
718 |
|
✗ |
mdl64 = &ctx->mdl64[0][idx]; |
719 |
|
|
} |
720 |
|
✗ |
ret = mdl64_decode(ac, mdl64, &val); |
721 |
|
✗ |
if (ret < 0) |
722 |
|
✗ |
return -1; |
723 |
|
✗ |
last_val = val; |
724 |
|
✗ |
src = &ctx->buf1[off + -1]; |
725 |
|
✗ |
for (int i = 0; i < filt.size && i < 15; i++) |
726 |
|
✗ |
sum += filt.coeffs[i] * (unsigned)src[-i]; |
727 |
|
✗ |
sum = sum * 2U; |
728 |
|
✗ |
for (int i = 15; i < filt.size; i++) |
729 |
|
✗ |
sum += filt.coeffs[i] * (unsigned)src[-i]; |
730 |
|
✗ |
sum = sum >> 6; |
731 |
|
✗ |
if (ctx->cmode == 0) { |
732 |
|
✗ |
if (bits == 0) { |
733 |
|
✗ |
ctx->buf1[off] = sum + val; |
734 |
|
|
} else { |
735 |
|
✗ |
ctx->buf1[off] = (val + (sum >> bits)) * (1 << bits) + |
736 |
|
✗ |
(((1U << bits) - 1U) & ctx->buf1[off + -1]); |
737 |
|
|
} |
738 |
|
✗ |
ctx->buf0[off] = ctx->buf1[off] + ctx->buf0[off + -1]; |
739 |
|
|
} else { |
740 |
|
✗ |
val *= 1 << ctx->cmode; |
741 |
|
✗ |
sum += ctx->buf0[off + -1] + val; |
742 |
|
✗ |
switch (s->bps) { |
743 |
|
✗ |
case 16: sum = av_clip_int16(sum); break; |
744 |
|
✗ |
case 8: sum = av_clip_int8(sum); break; |
745 |
|
|
} |
746 |
|
✗ |
ctx->buf1[off] = sum - ctx->buf0[off + -1]; |
747 |
|
✗ |
ctx->buf0[off] = sum; |
748 |
|
✗ |
m += FFABS(ctx->buf1[off]); |
749 |
|
|
} |
750 |
|
|
} |
751 |
|
✗ |
if (ctx->cmode2 != 0) { |
752 |
|
✗ |
int sum = 0; |
753 |
|
✗ |
for (int i = (m << 6) / split; i > 0; i = i >> 1) |
754 |
|
✗ |
sum++; |
755 |
|
✗ |
sum = sum - (ctx->cmode2 + 7); |
756 |
|
✗ |
ctx->cmode = FFMAX(sum, tab[ctx->cmode2]); |
757 |
|
|
} |
758 |
|
|
|
759 |
|
✗ |
x += split; |
760 |
|
|
} |
761 |
|
|
|
762 |
|
✗ |
return 0; |
763 |
|
|
} |
764 |
|
|
|
765 |
|
✗ |
static int decode_samples(AVCodecContext *avctx, ACoder *ac, ChContext *ctx, int offset) |
766 |
|
|
{ |
767 |
|
✗ |
RKAContext *s = avctx->priv_data; |
768 |
|
|
int segment_size, offset2, mode, ret; |
769 |
|
|
|
770 |
|
✗ |
ret = amdl_decode_int(&ctx->nb_segments, ac, &mode, 5); |
771 |
|
✗ |
if (ret < 0) |
772 |
|
✗ |
return ret; |
773 |
|
|
|
774 |
|
✗ |
if (mode == 5) { |
775 |
|
✗ |
ret = ac_get_freq(ac, ctx->srate_pad >> 2, &segment_size); |
776 |
|
✗ |
if (ret < 0) |
777 |
|
✗ |
return ret; |
778 |
|
✗ |
ac_update(ac, segment_size, 1); |
779 |
|
✗ |
segment_size *= 4; |
780 |
|
✗ |
ret = decode_filter(s, ctx, ac, offset, segment_size); |
781 |
|
✗ |
if (ret < 0) |
782 |
|
✗ |
return ret; |
783 |
|
|
} else { |
784 |
|
✗ |
segment_size = ctx->srate_pad; |
785 |
|
|
|
786 |
|
✗ |
if (mode) { |
787 |
|
✗ |
if (mode > 2) { |
788 |
|
✗ |
ret = decode_filter(s, ctx, ac, offset, segment_size / 4); |
789 |
|
✗ |
if (ret < 0) |
790 |
|
✗ |
return ret; |
791 |
|
✗ |
offset2 = segment_size / 4 + offset; |
792 |
|
✗ |
ret = decode_filter(s, ctx, ac, offset2, segment_size / 4); |
793 |
|
✗ |
if (ret < 0) |
794 |
|
✗ |
return ret; |
795 |
|
✗ |
offset2 = segment_size / 4 + offset2; |
796 |
|
|
} else { |
797 |
|
✗ |
ret = decode_filter(s, ctx, ac, offset, segment_size / 2); |
798 |
|
✗ |
if (ret < 0) |
799 |
|
✗ |
return ret; |
800 |
|
✗ |
offset2 = segment_size / 2 + offset; |
801 |
|
|
} |
802 |
|
✗ |
if (mode & 1) { |
803 |
|
✗ |
ret = decode_filter(s, ctx, ac, offset2, segment_size / 2); |
804 |
|
✗ |
if (ret < 0) |
805 |
|
✗ |
return ret; |
806 |
|
|
} else { |
807 |
|
✗ |
ret = decode_filter(s, ctx, ac, offset2, segment_size / 4); |
808 |
|
✗ |
if (ret < 0) |
809 |
|
✗ |
return ret; |
810 |
|
✗ |
ret = decode_filter(s, ctx, ac, segment_size / 4 + offset2, segment_size / 4); |
811 |
|
✗ |
if (ret < 0) |
812 |
|
✗ |
return ret; |
813 |
|
|
} |
814 |
|
|
} else { |
815 |
|
✗ |
ret = decode_filter(s, ctx, ac, offset, ctx->srate_pad); |
816 |
|
✗ |
if (ret < 0) |
817 |
|
✗ |
return ret; |
818 |
|
|
} |
819 |
|
|
} |
820 |
|
|
|
821 |
|
✗ |
return segment_size; |
822 |
|
|
} |
823 |
|
|
|
824 |
|
✗ |
static int decode_ch_samples(AVCodecContext *avctx, ChContext *c) |
825 |
|
|
{ |
826 |
|
✗ |
RKAContext *s = avctx->priv_data; |
827 |
|
✗ |
ACoder *ac = &s->ac; |
828 |
|
✗ |
int nb_decoded = 0; |
829 |
|
|
|
830 |
|
✗ |
if (bytestream2_get_bytes_left(&ac->gb) <= 0) |
831 |
|
✗ |
return 0; |
832 |
|
|
|
833 |
|
✗ |
memmove(c->buf0, &c->buf0[c->last_nb_decoded], 2560 * sizeof(*c->buf0)); |
834 |
|
✗ |
memmove(c->buf1, &c->buf1[c->last_nb_decoded], 2560 * sizeof(*c->buf1)); |
835 |
|
|
|
836 |
|
✗ |
nb_decoded = decode_samples(avctx, ac, c, 2560); |
837 |
|
✗ |
if (nb_decoded < 0) |
838 |
|
✗ |
return nb_decoded; |
839 |
|
✗ |
c->last_nb_decoded = nb_decoded; |
840 |
|
|
|
841 |
|
✗ |
return nb_decoded; |
842 |
|
|
} |
843 |
|
|
|
844 |
|
✗ |
static int rka_decode_frame(AVCodecContext *avctx, AVFrame *frame, |
845 |
|
|
int *got_frame_ptr, AVPacket *avpkt) |
846 |
|
|
{ |
847 |
|
✗ |
RKAContext *s = avctx->priv_data; |
848 |
|
✗ |
ACoder *ac = &s->ac; |
849 |
|
|
int ret; |
850 |
|
|
|
851 |
|
✗ |
bytestream2_init(&ac->gb, avpkt->data, avpkt->size); |
852 |
|
✗ |
init_acoder(ac); |
853 |
|
|
|
854 |
|
✗ |
for (int ch = 0; ch < s->channels; ch++) { |
855 |
|
✗ |
ret = chctx_init(s, &s->ch[ch], avctx->sample_rate, |
856 |
|
|
avctx->bits_per_raw_sample); |
857 |
|
✗ |
if (ret < 0) |
858 |
|
✗ |
return ret; |
859 |
|
|
} |
860 |
|
|
|
861 |
|
✗ |
frame->nb_samples = s->frame_samples; |
862 |
|
✗ |
if ((ret = ff_get_buffer(avctx, frame, 0)) < 0) |
863 |
|
✗ |
return ret; |
864 |
|
|
|
865 |
|
✗ |
if (s->channels == 2 && s->correlated) { |
866 |
|
✗ |
int16_t *l16 = (int16_t *)frame->extended_data[0]; |
867 |
|
✗ |
int16_t *r16 = (int16_t *)frame->extended_data[1]; |
868 |
|
✗ |
uint8_t *l8 = frame->extended_data[0]; |
869 |
|
✗ |
uint8_t *r8 = frame->extended_data[1]; |
870 |
|
|
|
871 |
|
✗ |
for (int n = 0; n < frame->nb_samples;) { |
872 |
|
✗ |
ret = decode_ch_samples(avctx, &s->ch[0]); |
873 |
|
✗ |
if (ret == 0) { |
874 |
|
✗ |
frame->nb_samples = n; |
875 |
|
✗ |
break; |
876 |
|
|
} |
877 |
|
✗ |
if (ret < 0 || n + ret > frame->nb_samples) |
878 |
|
✗ |
return AVERROR_INVALIDDATA; |
879 |
|
|
|
880 |
|
✗ |
ret = decode_ch_samples(avctx, &s->ch[1]); |
881 |
|
✗ |
if (ret == 0) { |
882 |
|
✗ |
frame->nb_samples = n; |
883 |
|
✗ |
break; |
884 |
|
|
} |
885 |
|
✗ |
if (ret < 0 || n + ret > frame->nb_samples) |
886 |
|
✗ |
return AVERROR_INVALIDDATA; |
887 |
|
|
|
888 |
|
✗ |
switch (avctx->sample_fmt) { |
889 |
|
✗ |
case AV_SAMPLE_FMT_S16P: |
890 |
|
✗ |
for (int i = 0; i < ret; i++) { |
891 |
|
✗ |
int l = s->ch[0].buf0[2560 + i]; |
892 |
|
✗ |
int r = s->ch[1].buf0[2560 + i]; |
893 |
|
|
|
894 |
|
✗ |
l16[n + i] = (l * 2 + r + 1) >> 1; |
895 |
|
✗ |
r16[n + i] = (l * 2 - r + 1) >> 1; |
896 |
|
|
} |
897 |
|
✗ |
break; |
898 |
|
✗ |
case AV_SAMPLE_FMT_U8P: |
899 |
|
✗ |
for (int i = 0; i < ret; i++) { |
900 |
|
✗ |
int l = s->ch[0].buf0[2560 + i]; |
901 |
|
✗ |
int r = s->ch[1].buf0[2560 + i]; |
902 |
|
|
|
903 |
|
✗ |
l8[n + i] = ((l * 2 + r + 1) >> 1) + 0x7f; |
904 |
|
✗ |
r8[n + i] = ((l * 2 - r + 1) >> 1) + 0x7f; |
905 |
|
|
} |
906 |
|
✗ |
break; |
907 |
|
✗ |
default: |
908 |
|
✗ |
return AVERROR_INVALIDDATA; |
909 |
|
|
} |
910 |
|
|
|
911 |
|
✗ |
n += ret; |
912 |
|
|
} |
913 |
|
|
} else { |
914 |
|
✗ |
for (int n = 0; n < frame->nb_samples;) { |
915 |
|
✗ |
for (int ch = 0; ch < s->channels; ch++) { |
916 |
|
✗ |
int16_t *m16 = (int16_t *)frame->data[ch]; |
917 |
|
✗ |
uint8_t *m8 = frame->data[ch]; |
918 |
|
|
|
919 |
|
✗ |
ret = decode_ch_samples(avctx, &s->ch[ch]); |
920 |
|
✗ |
if (ret == 0) { |
921 |
|
✗ |
frame->nb_samples = n; |
922 |
|
✗ |
break; |
923 |
|
|
} |
924 |
|
|
|
925 |
|
✗ |
if (ret < 0 || n + ret > frame->nb_samples) |
926 |
|
✗ |
return AVERROR_INVALIDDATA; |
927 |
|
|
|
928 |
|
✗ |
switch (avctx->sample_fmt) { |
929 |
|
✗ |
case AV_SAMPLE_FMT_S16P: |
930 |
|
✗ |
for (int i = 0; i < ret; i++) { |
931 |
|
✗ |
int m = s->ch[ch].buf0[2560 + i]; |
932 |
|
|
|
933 |
|
✗ |
m16[n + i] = m; |
934 |
|
|
} |
935 |
|
✗ |
break; |
936 |
|
✗ |
case AV_SAMPLE_FMT_U8P: |
937 |
|
✗ |
for (int i = 0; i < ret; i++) { |
938 |
|
✗ |
int m = s->ch[ch].buf0[2560 + i]; |
939 |
|
|
|
940 |
|
✗ |
m8[n + i] = m + 0x7f; |
941 |
|
|
} |
942 |
|
✗ |
break; |
943 |
|
✗ |
default: |
944 |
|
✗ |
return AVERROR_INVALIDDATA; |
945 |
|
|
} |
946 |
|
|
} |
947 |
|
|
|
948 |
|
✗ |
n += ret; |
949 |
|
|
} |
950 |
|
|
} |
951 |
|
|
|
952 |
|
✗ |
*got_frame_ptr = 1; |
953 |
|
|
|
954 |
|
✗ |
return avpkt->size; |
955 |
|
|
} |
956 |
|
|
|
957 |
|
✗ |
static av_cold int rka_decode_close(AVCodecContext *avctx) |
958 |
|
|
{ |
959 |
|
✗ |
RKAContext *s = avctx->priv_data; |
960 |
|
|
|
961 |
|
✗ |
for (int ch = 0; ch < 2; ch++) { |
962 |
|
✗ |
ChContext *c = &s->ch[ch]; |
963 |
|
|
|
964 |
|
✗ |
for (int i = 0; i < 11; i++) |
965 |
|
✗ |
adaptive_model_free(&c->coeff_bits[i]); |
966 |
|
|
|
967 |
|
✗ |
adaptive_model_free(&c->position); |
968 |
|
✗ |
adaptive_model_free(&c->nb_segments); |
969 |
|
✗ |
adaptive_model_free(&c->fshift); |
970 |
|
|
} |
971 |
|
|
|
972 |
|
✗ |
adaptive_model_free(&s->filt_size); |
973 |
|
✗ |
adaptive_model_free(&s->filt_bits); |
974 |
|
|
|
975 |
|
✗ |
return 0; |
976 |
|
|
} |
977 |
|
|
|
978 |
|
|
const FFCodec ff_rka_decoder = { |
979 |
|
|
.p.name = "rka", |
980 |
|
|
CODEC_LONG_NAME("RKA (RK Audio)"), |
981 |
|
|
.p.type = AVMEDIA_TYPE_AUDIO, |
982 |
|
|
.p.id = AV_CODEC_ID_RKA, |
983 |
|
|
.priv_data_size = sizeof(RKAContext), |
984 |
|
|
.init = rka_decode_init, |
985 |
|
|
.close = rka_decode_close, |
986 |
|
|
FF_CODEC_DECODE_CB(rka_decode_frame), |
987 |
|
|
.p.capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_CHANNEL_CONF, |
988 |
|
|
.caps_internal = FF_CODEC_CAP_INIT_CLEANUP, |
989 |
|
|
}; |
990 |
|
|
|