summaryrefslogtreecommitdiff
path: root/lib/rbcodec/codecs/libffmpegFLAC/shndec.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/rbcodec/codecs/libffmpegFLAC/shndec.c')
-rw-r--r--lib/rbcodec/codecs/libffmpegFLAC/shndec.c481
1 files changed, 481 insertions, 0 deletions
diff --git a/lib/rbcodec/codecs/libffmpegFLAC/shndec.c b/lib/rbcodec/codecs/libffmpegFLAC/shndec.c
new file mode 100644
index 0000000000..40e7211b87
--- /dev/null
+++ b/lib/rbcodec/codecs/libffmpegFLAC/shndec.c
@@ -0,0 +1,481 @@
1/*
2 * Shorten decoder
3 * Copyright (c) 2005 Jeff Muizelaar
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
14 *
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18 */
19
20/**
21 * @file shorten.c
22 * Shorten decoder
23 * @author Jeff Muizelaar
24 *
25 */
26
27#include "bitstream.h"
28#include "golomb.h"
29#include "shndec.h"
30#include "codeclib.h"
31
32#define ULONGSIZE 2
33
34#define WAVE_FORMAT_PCM 0x0001
35
36#define TYPESIZE 4
37#define CHANSIZE 0
38#define LPCQSIZE 2
39#define ENERGYSIZE 3
40#define BITSHIFTSIZE 2
41
42#define TYPE_S16HL 3 /* signed 16 bit shorts: high-low */
43#define TYPE_S16LH 5 /* signed 16 bit shorts: low-high */
44
45#define NWRAP 3
46#define NSKIPSIZE 1
47
48#define LPCQUANT 5
49#define V2LPCQOFFSET (1 << LPCQUANT)
50
51#define FNSIZE 2
52
53#define VERBATIM_CKSIZE_SIZE 5
54#define VERBATIM_BYTE_SIZE 8
55#define CANONICAL_HEADER_SIZE 44
56
57#define MKTAG(a,b,c,d) (a | (b << 8) | (c << 16) | (d << 24))
58
59#define get_le16(gb) bswap_16(get_bits_long(gb, 16))
60#define get_le32(gb) bswap_32(get_bits_long(gb, 32))
61
62/* converts fourcc string to int */
63static unsigned int ff_get_fourcc(const char *s){
64 //assert( strlen(s)==4 );
65 return (s[0]) + (s[1]<<8) + (s[2]<<16) + (s[3]<<24);
66}
67
68static unsigned int get_uint(ShortenContext *s, int k)
69{
70 if (s->version != 0)
71 k = get_ur_golomb_shorten(&s->gb, ULONGSIZE);
72 return get_ur_golomb_shorten(&s->gb, k);
73}
74
75#if defined(CPU_COLDFIRE)
76static inline void coldfire_lshift_samples(int n, int shift, int32_t *samples)
77{
78/*
79 for (i = 0; i < n; i++)
80 samples[i] =<< shift;
81*/
82 asm volatile (
83 "move.l %[n], %%d0 \n" /* d0 = loop counter */
84 "asr.l #2, %%d0 \n"
85 "beq 1f \n"
86 "2:" /* main loop (unroll by 4) */
87 "movem.l (%[x]), %%d4-%%d7 \n"
88 "asl.l %[s], %%d4 \n"
89 "asl.l %[s], %%d5 \n"
90 "asl.l %[s], %%d6 \n"
91 "asl.l %[s], %%d7 \n"
92 "movem.l %%d4-%%d7, (%[x]) \n"
93 "lea.l (16, %[x]), %[x] \n"
94
95 "subq.l #1, %%d0 \n"
96 "bne 2b \n"
97 "1:" /* any loops left? */
98 "and.l #3, %[n] \n"
99 "beq 4f \n"
100 "3:" /* remaining loops */
101 "move.l (%[x]), %%d4 \n"
102 "asl.l %[s], %%d4 \n"
103 "move.l %%d4, (%[x])+ \n"
104
105 "subq.l #1, %[n] \n"
106 "bne 3b \n"
107 "4:" /* exit */
108 : [n] "+d" (n),
109 [x] "+a" (samples)
110 : [s] "d" (shift)
111 : "%d0", "%d4", "%d5", "%d6", "%d7", "cc", "memory"
112 );
113}
114#endif
115
116static inline void fix_bitshift(ShortenContext *s, int32_t *samples)
117{
118 int i;
119
120 /* Wrapped samples don't get bitshifted, so we'll do them during
121 the next iteration. */
122 if (s->bitshift != 0) {
123#if defined(CPU_COLDFIRE)
124 coldfire_lshift_samples(s->blocksize, s->bitshift, samples - s->nwrap);
125#else
126 for (i = -s->nwrap; i < (s->blocksize - s->nwrap); i++)
127 samples[i] <<= s->bitshift;
128#endif
129 }
130
131 /* Also, when we have to remember to fix the wrapped samples when
132 the bitshift changes.*/
133 if (s->bitshift != s->last_bitshift) {
134 if (s->last_bitshift != 0)
135 for (i = -s->nwrap; i < 0; i++)
136 samples[i] <<= s->last_bitshift;
137
138 s->last_bitshift = s->bitshift;
139 }
140}
141
142static inline void decode_subframe_lpc(ShortenContext *s, int32_t *decoded,
143 int residual_size, int pred_order)
144{
145 int sum, i, j;
146 int coeffs[MAX_PRED_ORDER];
147
148 for (i=0; i<pred_order; i++) {
149 coeffs[i] = get_sr_golomb_shorten(&s->gb, LPCQUANT);
150 }
151
152 for (i=0; i < s->blocksize; i++) {
153 sum = s->lpcqoffset;
154 for (j=0; j<pred_order; j++)
155 sum += coeffs[j] * decoded[i-j-1];
156
157 decoded[i] =
158 get_sr_golomb_shorten(&s->gb, residual_size) + (sum >> LPCQUANT);
159 }
160}
161
162static inline int shorten_decode_frame(ShortenContext *s, int32_t *decoded,
163 int32_t *offset)
164{
165 int i;
166 int32_t sum;
167
168 int cmd = get_ur_golomb_shorten(&s->gb, FNSIZE);
169 switch (cmd) {
170 case FN_ZERO:
171 case FN_DIFF0:
172 case FN_DIFF1:
173 case FN_DIFF2:
174 case FN_DIFF3:
175 case FN_QLPC:
176 {
177 int residual_size = 0;
178 int32_t coffset;
179 if (cmd != FN_ZERO) {
180 residual_size = get_ur_golomb_shorten(&s->gb, ENERGYSIZE);
181 /* this is a hack as version 0 differed in defintion of
182 get_sr_golomb_shorten */
183 if (s->version == 0)
184 residual_size--;
185 }
186
187 if (s->nmean == 0) {
188 coffset = offset[0];
189 } else {
190 sum = (s->version < 2) ? 0 : s->nmean / 2;
191 for (i=0; i<s->nmean; i++)
192 sum += offset[i];
193
194 coffset = sum / s->nmean;
195 if (s->version >= 2)
196 coffset >>= FFMIN(1, s->bitshift);
197 }
198
199 switch (cmd) {
200 case FN_ZERO:
201 for (i=0; i<s->blocksize; i++)
202 decoded[i] = 0;
203 break;
204
205 case FN_DIFF0:
206 for (i=0; i<s->blocksize; i++)
207 decoded[i] =
208 get_sr_golomb_shorten(&s->gb, residual_size) +
209 coffset;
210 break;
211
212 case FN_DIFF1:
213 for (i=0; i<s->blocksize; i++)
214 decoded[i] =
215 get_sr_golomb_shorten(&s->gb, residual_size) +
216 decoded[i - 1];
217 break;
218
219 case FN_DIFF2:
220 for (i=0; i<s->blocksize; i++)
221 decoded[i] =
222 get_sr_golomb_shorten(&s->gb, residual_size) +
223 2*decoded[i-1] - decoded[i-2];
224 break;
225
226 case FN_DIFF3:
227 for (i=0; i<s->blocksize; i++)
228 decoded[i] =
229 get_sr_golomb_shorten(&s->gb, residual_size) +
230 3*decoded[i-1] - 3*decoded[i-2] + decoded[i-3];
231 break;
232
233 case FN_QLPC:
234 {
235 int pred_order = get_ur_golomb_shorten(&s->gb, LPCQSIZE);
236 for (i=0; i<pred_order; i++)
237 decoded[i - pred_order] -= coffset;
238 decode_subframe_lpc(s, decoded, residual_size, pred_order);
239 if (coffset != 0) {
240 for (i=0; i < s->blocksize; i++)
241 decoded[i] += coffset;
242 }
243 }
244 }
245
246 if (s->nmean > 0) {
247 sum = (s->version < 2) ? 0 : s->blocksize / 2;
248 for (i=0; i<s->blocksize; i++)
249 sum += decoded[i];
250
251 for (i=1; i<s->nmean; i++)
252 offset[i-1] = offset[i];
253
254 if (s->version < 2) {
255 offset[s->nmean - 1] = sum / s->blocksize;
256 } else {
257 offset[s->nmean - 1] =
258 (sum / s->blocksize) << s->bitshift;
259 }
260 }
261
262 fix_bitshift(s, decoded);
263 break;
264 }
265
266 case FN_VERBATIM:
267 i = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
268 while (i--)
269 get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
270 break;
271
272 case FN_BITSHIFT:
273 s->bitshift = get_ur_golomb_shorten(&s->gb, BITSHIFTSIZE);
274 break;
275
276 case FN_BLOCKSIZE:
277 s->blocksize = get_uint(s, av_log2(s->blocksize));
278 break;
279
280 case FN_QUIT:
281 break;
282
283 default:
284 return FN_ERROR;
285 break;
286 }
287
288 return cmd;
289}
290
291int shorten_decode_frames(ShortenContext *s, int *nsamples,
292 int32_t *decoded0, int32_t *decoded1,
293 int32_t *offset0, int32_t *offset1,
294 uint8_t *buf, int buf_size,
295 void (*yield)(void))
296{
297 int32_t *decoded, *offset;
298 int cmd;
299
300 *nsamples = 0;
301
302 init_get_bits(&s->gb, buf, buf_size*8);
303 get_bits(&s->gb, s->bitindex);
304
305 int n = 0;
306 while (n < NUM_DEC_LOOPS) {
307 int chan = n%2;
308 if (chan == 0) {
309 decoded = decoded0 + s->nwrap + *nsamples;
310 offset = offset0;
311 } else {
312 decoded = decoded1 + s->nwrap + *nsamples;
313 offset = offset1;
314 }
315
316 yield();
317
318 cmd = shorten_decode_frame(s, decoded, offset);
319
320 if (cmd == FN_VERBATIM || cmd == FN_BITSHIFT || cmd == FN_BLOCKSIZE) {
321 continue;
322 } else if (cmd == FN_QUIT || cmd == FN_ERROR) {
323 break;
324 }
325
326 *nsamples += chan * s->blocksize;
327 n++;
328 }
329
330 if (*nsamples) {
331 /* Wrap the samples for the next loop */
332 int i;
333 for (i = 0; i < s->nwrap; i++) {
334 decoded0[i] = decoded0[*nsamples + i];
335 decoded1[i] = decoded1[*nsamples + i];
336 }
337
338 /* Scale the samples for the pcmbuf */
339 int scale = SHN_OUTPUT_DEPTH - s->bits_per_sample;
340#if defined(CPU_COLDFIRE)
341 coldfire_lshift_samples(*nsamples, scale, decoded0 + s->nwrap);
342 coldfire_lshift_samples(*nsamples, scale, decoded1 + s->nwrap);
343#else
344 for (i = 0; i < *nsamples; i++) {
345 decoded0[i + s->nwrap] <<= scale;
346 decoded1[i + s->nwrap] <<= scale;
347 }
348#endif
349 }
350
351 return cmd;
352}
353
354static int decode_wave_header(ShortenContext *s,
355 uint8_t *header,
356 int header_size)
357{
358 GetBitContext hb;
359 int len;
360
361 init_get_bits(&hb, header, header_size*8);
362 if (get_le32(&hb) != MKTAG('R','I','F','F')) {
363 return -8;
364 }
365
366 int chunk_size = get_le32(&hb);
367
368 if (get_le32(&hb) != MKTAG('W','A','V','E')) {
369 return -9;
370 }
371
372 while (get_le32(&hb) != MKTAG('f','m','t',' ')) {
373 len = get_le32(&hb);
374 skip_bits(&hb, 8*len);
375 }
376
377 len = get_le32(&hb);
378 if (len < 16) {
379 return -10;
380 }
381
382 if (get_le16(&hb) != WAVE_FORMAT_PCM ) {
383 return -11;
384 }
385
386 s->channels = get_le16(&hb);
387 if (s->channels > MAX_CHANNELS) {
388 return -3;
389 }
390
391 s->sample_rate = get_le32(&hb);
392
393 skip_bits(&hb, 32);
394 //s->bit_rate = 8*get_le32(&hb);
395
396 int block_align = get_le16(&hb);
397 s->totalsamples = (chunk_size - 36) / block_align;
398
399 s->bits_per_sample = get_le16(&hb);
400 if (s->bits_per_sample != 16) {
401 return -12;
402 }
403
404 len -= 16;
405 if (len > 0) {
406 return len;
407 }
408
409 return 0;
410}
411
412int shorten_init(ShortenContext* s, uint8_t *buf, int buf_size)
413{
414 int i;
415
416 s->blocksize = DEFAULT_BLOCK_SIZE;
417 s->channels = 1;
418 s->nmean = -1;
419
420 init_get_bits(&s->gb, buf, buf_size*8);
421 get_bits(&s->gb, s->bitindex);
422
423 /* shorten signature */
424 if (get_bits_long(&s->gb, 32) != bswap_32(ff_get_fourcc("ajkg"))) {
425 return -1;
426 }
427
428 s->version = get_bits(&s->gb, 8);
429
430 int internal_ftype = get_uint(s, TYPESIZE);
431 if ((internal_ftype != TYPE_S16HL) && (internal_ftype != TYPE_S16LH)) {
432 return -2;
433 }
434
435 s->channels = get_uint(s, CHANSIZE);
436 if (s->channels > MAX_CHANNELS) {
437 return -3;
438 }
439
440 /* get blocksize if version > 0 */
441 int maxnlpc = 0;
442 if (s->version > 0) {
443 s->blocksize = get_uint(s, av_log2(DEFAULT_BLOCK_SIZE));
444 maxnlpc = get_uint(s, LPCQSIZE);
445 s->nmean = get_uint(s, 0);
446
447 int skip_bytes = get_uint(s, NSKIPSIZE);
448 for (i=0; i<skip_bytes; i++) {
449 skip_bits(&s->gb, 8);
450 }
451 }
452
453 if (s->nmean > MAX_NMEAN) {
454 return -4;
455 }
456
457 s->nwrap = FFMAX(NWRAP, maxnlpc);
458 if (s->nwrap > MAX_NWRAP) {
459 return -5;
460 }
461
462 if (s->version > 1)
463 s->lpcqoffset = V2LPCQOFFSET;
464
465 if (get_ur_golomb_shorten(&s->gb, FNSIZE) != FN_VERBATIM) {
466 return -6;
467 }
468
469 uint8_t header[MAX_HEADER_SIZE];
470 int header_size = get_ur_golomb_shorten(&s->gb, VERBATIM_CKSIZE_SIZE);
471 if (header_size >= MAX_HEADER_SIZE || header_size < CANONICAL_HEADER_SIZE) {
472 return -7;
473 }
474
475 for (i=0; i<header_size; i++)
476 header[i] = (char)get_ur_golomb_shorten(&s->gb, VERBATIM_BYTE_SIZE);
477
478 s->header_bits = s->gb.index;
479
480 return decode_wave_header(s, header, header_size);
481}