A modern Music Player Daemon based on Rockbox open source high quality audio player
libadwaita
audio
rust
zig
deno
mpris
rockbox
mpd
1/*
2 * FLAC (Free Lossless Audio Codec) decoder
3 * Copyright (c) 2003 Alex Beregszaszi
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 flac.c
22 * FLAC (Free Lossless Audio Codec) decoder
23 * @author Alex Beregszaszi
24 *
25 * For more information on the FLAC format, visit:
26 * http://flac.sourceforge.net/
27 *
28 * This decoder can be used in 1 of 2 ways: Either raw FLAC data can be fed
29 * through, starting from the initial 'fLaC' signature; or by passing the
30 * 34-byte streaminfo structure through avctx->extradata[_size] followed
31 * by data starting with the 0xFFF8 marker.
32 */
33
34#include <inttypes.h>
35#include <stdbool.h>
36#ifndef BUILD_STANDALONE
37#include "codeclib.h"
38#endif
39
40#include "bitstream.h"
41#include "golomb.h"
42
43#include "decoder.h"
44
45#if defined(CPU_COLDFIRE)
46#include "coldfire.h"
47#elif defined(CPU_ARM)
48#include "arm.h"
49#endif
50
51static const int sample_rate_table[] ICONST_ATTR =
52{ 0, 88200, 176400, 192000,
53 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000,
54 0, 0, 0, 0 };
55
56static const int sample_size_table[] ICONST_ATTR =
57{ 0, 8, 12, 0, 16, 20, 24, 0 };
58
59static const int blocksize_table[] ICONST_ATTR = {
60 0, 192, 576<<0, 576<<1, 576<<2, 576<<3, 0, 0,
61256<<0, 256<<1, 256<<2, 256<<3, 256<<4, 256<<5, 256<<6, 256<<7
62};
63
64static const uint8_t table_crc8[256] ICONST_ATTR = {
65 0x00, 0x07, 0x0e, 0x09, 0x1c, 0x1b, 0x12, 0x15,
66 0x38, 0x3f, 0x36, 0x31, 0x24, 0x23, 0x2a, 0x2d,
67 0x70, 0x77, 0x7e, 0x79, 0x6c, 0x6b, 0x62, 0x65,
68 0x48, 0x4f, 0x46, 0x41, 0x54, 0x53, 0x5a, 0x5d,
69 0xe0, 0xe7, 0xee, 0xe9, 0xfc, 0xfb, 0xf2, 0xf5,
70 0xd8, 0xdf, 0xd6, 0xd1, 0xc4, 0xc3, 0xca, 0xcd,
71 0x90, 0x97, 0x9e, 0x99, 0x8c, 0x8b, 0x82, 0x85,
72 0xa8, 0xaf, 0xa6, 0xa1, 0xb4, 0xb3, 0xba, 0xbd,
73 0xc7, 0xc0, 0xc9, 0xce, 0xdb, 0xdc, 0xd5, 0xd2,
74 0xff, 0xf8, 0xf1, 0xf6, 0xe3, 0xe4, 0xed, 0xea,
75 0xb7, 0xb0, 0xb9, 0xbe, 0xab, 0xac, 0xa5, 0xa2,
76 0x8f, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9d, 0x9a,
77 0x27, 0x20, 0x29, 0x2e, 0x3b, 0x3c, 0x35, 0x32,
78 0x1f, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0d, 0x0a,
79 0x57, 0x50, 0x59, 0x5e, 0x4b, 0x4c, 0x45, 0x42,
80 0x6f, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7d, 0x7a,
81 0x89, 0x8e, 0x87, 0x80, 0x95, 0x92, 0x9b, 0x9c,
82 0xb1, 0xb6, 0xbf, 0xb8, 0xad, 0xaa, 0xa3, 0xa4,
83 0xf9, 0xfe, 0xf7, 0xf0, 0xe5, 0xe2, 0xeb, 0xec,
84 0xc1, 0xc6, 0xcf, 0xc8, 0xdd, 0xda, 0xd3, 0xd4,
85 0x69, 0x6e, 0x67, 0x60, 0x75, 0x72, 0x7b, 0x7c,
86 0x51, 0x56, 0x5f, 0x58, 0x4d, 0x4a, 0x43, 0x44,
87 0x19, 0x1e, 0x17, 0x10, 0x05, 0x02, 0x0b, 0x0c,
88 0x21, 0x26, 0x2f, 0x28, 0x3d, 0x3a, 0x33, 0x34,
89 0x4e, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5c, 0x5b,
90 0x76, 0x71, 0x78, 0x7f, 0x6a, 0x6d, 0x64, 0x63,
91 0x3e, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2c, 0x2b,
92 0x06, 0x01, 0x08, 0x0f, 0x1a, 0x1d, 0x14, 0x13,
93 0xae, 0xa9, 0xa0, 0xa7, 0xb2, 0xb5, 0xbc, 0xbb,
94 0x96, 0x91, 0x98, 0x9f, 0x8a, 0x8d, 0x84, 0x83,
95 0xde, 0xd9, 0xd0, 0xd7, 0xc2, 0xc5, 0xcc, 0xcb,
96 0xe6, 0xe1, 0xe8, 0xef, 0xfa, 0xfd, 0xf4, 0xf3
97};
98
99static int64_t get_utf8(GetBitContext *gb) ICODE_ATTR_FLAC;
100static int64_t get_utf8(GetBitContext *gb)
101{
102 uint64_t val;
103 int ones=0, bytes;
104
105 while(get_bits1(gb))
106 ones++;
107
108 if (ones==0) bytes=0;
109 else if(ones==1) return -1;
110 else bytes= ones - 1;
111
112 val= get_bits(gb, 7-ones);
113 while(bytes--){
114 const int tmp = get_bits(gb, 8);
115
116 if((tmp>>6) != 2)
117 return -2;
118 val<<=6;
119 val|= tmp&0x3F;
120 }
121 return val;
122}
123
124static int get_crc8(const uint8_t *buf, int count) ICODE_ATTR_FLAC;
125static int get_crc8(const uint8_t *buf, int count)
126{
127 int crc=0;
128 int i;
129
130 for(i=0; i<count; i++){
131 crc = table_crc8[crc ^ buf[i]];
132 }
133
134 return crc;
135}
136
137static int decode_residuals(FLACContext *s, int32_t* decoded, int pred_order) ICODE_ATTR_FLAC;
138static int decode_residuals(FLACContext *s, int32_t *decoded, int pred_order)
139{
140 GetBitContext gb = s->gb;
141 int i, tmp, partition, method_type, rice_order;
142 int rice_bits, rice_esc;
143 int samples;
144
145 method_type = get_bits(&gb, 2);
146 rice_order = get_bits(&gb, 4);
147
148 samples = s->blocksize >> rice_order;
149 rice_bits = 4 + method_type;
150 rice_esc = (1 << rice_bits) - 1;
151
152 decoded += pred_order;
153 i = pred_order;
154
155 if (method_type > 1 || (samples << rice_order != s->blocksize) || pred_order > samples)
156 {
157 return -3;
158 }
159
160 for (partition = 0; partition < (1 << rice_order); partition++) {
161 tmp = get_bits(&gb, rice_bits);
162 if (tmp == rice_esc) {
163 tmp = get_bits(&gb, 5);
164 for (; i < samples; i++)
165 *decoded++ = get_sbits(&gb, tmp);
166 } else {
167 int real_limit = tmp ? (INT_MAX >> tmp) + 2 : INT_MAX;
168 for (; i < samples; i++) {
169 int v = get_sr_golomb_flac(&gb, tmp, real_limit, 0);
170 if ((unsigned) v == 0x80000000){
171 return -3;
172 }
173
174 *decoded++ = v;
175 }
176 }
177 i= 0;
178 }
179
180 s->gb = gb;
181
182 return 0;
183}
184
185//static int decode_subframe_fixed(FLACContext *s, int32_t* decoded, int pred_order, int bps) ICODE_ATTR_FLAC;
186int decode_subframe_fixed(FLACContext *s, int32_t* decoded, int pred_order, int bps)
187{
188 const int blocksize = s->blocksize;
189 unsigned a, b, c, d;
190 int i;
191
192 /* warm up samples */
193 for (i = 0; i < pred_order; i++)
194 {
195 decoded[i] = get_sbits(&s->gb, bps);
196 }
197
198 if (decode_residuals(s, decoded, pred_order) < 0)
199 return -4;
200
201 switch(pred_order)
202 {
203 case 0:
204 break;
205 case 1:
206 a = decoded[pred_order-1];
207 for (i = pred_order; i < blocksize; i++)
208 decoded[i] = a += decoded[i];
209 break;
210 case 2:
211 a = decoded[pred_order-1];
212 b = a - decoded[pred_order-2];
213 for (i = pred_order; i < blocksize; i++)
214 decoded[i] = a += b += decoded[i];
215 break;
216 case 3:
217 a = decoded[pred_order-1];
218 b = a - decoded[pred_order-2];
219 c = b - decoded[pred_order-2] + decoded[pred_order-3];
220 for (i = pred_order; i < blocksize; i++)
221 decoded[i] = a += b += c += decoded[i];
222 break;
223 case 4:
224 a = decoded[pred_order-1];
225 b = a - decoded[pred_order-2];
226 c = b - decoded[pred_order-2] + decoded[pred_order-3];
227 d = c - decoded[pred_order-2] + 2U*decoded[pred_order-3] - decoded[pred_order-4];
228 for (i = pred_order; i < blocksize; i++)
229 decoded[i] = a += b += c += d += decoded[i];
230 break;
231 default:
232 return -5;
233 }
234
235 return 0;
236}
237
238#if !defined(CPU_COLDFIRE)
239static void flac_lpc_32_c(int32_t *decoded, int coeffs[],
240 int pred_order, int qlevel, int len) ICODE_ATTR_FLAC;
241static void flac_lpc_32_c(int32_t *decoded, int coeffs[],
242 int pred_order, int qlevel, int len)
243{
244 int i, j;
245 /*NOTE coeffs[] is in reverse order compared to upstream*/
246 for (i = pred_order; i < len; i++, decoded++) {
247 int64_t sum = 0;
248 for (j = 0; j < pred_order; j++)
249 sum += (int64_t)coeffs[pred_order-j-1] * decoded[j];
250 decoded[j] += sum >> qlevel;
251 }
252
253}
254
255static void lpc_analyze_remodulate(int32_t *decoded, int coeffs[],
256 int order, int qlevel, int len, int bps)
257{
258 /*NOTE coeffs[] is in reverse order compared to upstream*/
259 int i, j;
260 int ebps = 1 << (bps-1);
261 unsigned sigma = 0;
262
263 for (i = order; i < len; i++)
264 sigma |= decoded[i] + ebps;
265
266 if (sigma < 2U*ebps)
267 return;
268
269 for (i = len - 1; i >= order; i--) {
270 int64_t p = 0;
271 for (j = 0; j < order; j++)
272 p += coeffs[order-j-1] * (int64_t)(int32_t)decoded[i-order+j];
273 decoded[i] -= p >> qlevel;
274 }
275 for (i = order; i < len; i++, decoded++) {
276 int32_t p = 0;
277 for (j = 0; j < order; j++)
278 p += coeffs[order-j-1] * (uint32_t)decoded[j];
279 decoded[j] += p >> qlevel;
280 }
281}
282#endif
283
284static int decode_subframe_lpc(FLACContext *s, int32_t* decoded, int pred_order, int bps) ICODE_ATTR_FLAC;
285static int decode_subframe_lpc(FLACContext *s, int32_t* decoded, int pred_order, int bps)
286{
287 int sum, i, j;
288 int coeff_prec, qlevel;
289 int coeffs[pred_order];
290
291 /* warm up samples */
292 for (i = 0; i < pred_order; i++)
293 {
294 decoded[i] = get_sbits(&s->gb, bps);
295 }
296
297 coeff_prec = get_bits(&s->gb, 4) + 1;
298 if (coeff_prec == 16)
299 {
300 //fprintf(stderr,"invalid coeff precision\n");
301 return -6;
302 }
303 qlevel = get_sbits(&s->gb, 5);
304 if (qlevel < 0)
305 {
306 //fprintf(stderr,"qlevel %d not supported, maybe buggy stream\n", qlevel);
307 return -7;
308 }
309
310 for (j = 0; j < pred_order; j++)
311 {
312 coeffs[j] = get_sbits(&s->gb, coeff_prec);
313 }
314
315 if (decode_residuals(s, decoded, pred_order) < 0)
316 return -8;
317
318 if ((s->bps + coeff_prec + av_log2(pred_order)) <= 32) {
319 #if defined(CPU_COLDFIRE)
320 (void)sum;
321 lpc_decode_emac(s->blocksize - pred_order, qlevel, pred_order,
322 decoded + pred_order, coeffs);
323 #elif defined(CPU_ARM_CLASSIC)
324 (void)sum;
325 lpc_decode_arm(s->blocksize - pred_order, qlevel, pred_order,
326 decoded + pred_order, coeffs);
327 #else
328 for (i = pred_order; i < s->blocksize; i++)
329 {
330 sum = 0;
331 for (j = 0; j < pred_order; j++)
332 sum += coeffs[j] * decoded[i-j-1];
333 decoded[i] += sum >> qlevel;
334 }
335 #endif
336 } else {
337 #if defined(CPU_COLDFIRE)
338 lpc_decode_emac_wide(s->blocksize - pred_order, qlevel, pred_order,
339 decoded + pred_order, coeffs);
340 #else
341 flac_lpc_32_c(decoded, coeffs, pred_order, qlevel, s->blocksize);
342
343 if (bps <= 16)
344 lpc_analyze_remodulate(decoded, coeffs, pred_order, qlevel, s->blocksize, bps);
345 #endif
346 }
347
348 return 0;
349}
350
351static inline int decode_subframe(FLACContext *s, int channel, int32_t* decoded)
352{
353 int type, wasted = 0;
354 int i, tmp;
355 int bps = s->bps;
356 if(channel == 0){
357 if(s->ch_mode == RIGHT_SIDE)
358 bps++;
359 }else{
360 if(s->ch_mode == LEFT_SIDE || s->ch_mode == MID_SIDE)
361 bps++;
362 }
363
364 if (get_bits1(&s->gb))
365 {
366 //fprintf(stderr,"invalid subframe padding\n");
367 return -9;
368 }
369 type = get_bits(&s->gb, 6);
370
371 if (get_bits1(&s->gb))
372 {
373 wasted = 1;
374 while (!get_bits1(&s->gb))
375 wasted++;
376 bps -= wasted;
377 //fprintf(stderr,"%d wasted bits\n", wasted);
378 }
379
380//FIXME use av_log2 for types
381 if (type == 0)
382 {
383 //fprintf(stderr,"coding type: constant\n");
384 tmp = get_sbits(&s->gb, bps);
385 for (i = 0; i < s->blocksize; i++)
386 decoded[i] = tmp;
387 }
388 else if (type == 1)
389 {
390 //fprintf(stderr,"coding type: verbatim\n");
391 for (i = 0; i < s->blocksize; i++)
392 decoded[i] = get_sbits(&s->gb, bps);
393 }
394 else if ((type >= 8) && (type <= 12))
395 {
396 //fprintf(stderr,"coding type: fixed\n");
397 if (decode_subframe_fixed(s, decoded, type & ~0x8, bps) < 0)
398 return -10;
399 }
400 else if (type >= 32)
401 {
402 //fprintf(stderr,"coding type: lpc\n");
403 if (decode_subframe_lpc(s, decoded, (type & ~0x20)+1, bps) < 0)
404 return -11;
405 }
406 else
407 {
408 //fprintf(stderr,"Unknown coding type: %d\n",type);
409 return -12;
410 }
411
412 if (wasted && wasted < 32)
413 {
414 int i;
415 for (i = 0; i < s->blocksize; i++)
416 decoded[i] = (unsigned)decoded[i] << wasted;
417 }
418
419 return 0;
420}
421
422static int decode_frame(FLACContext *s,
423 void (*yield)(void)) ICODE_ATTR_FLAC;
424static int decode_frame(FLACContext *s,
425 void (*yield)(void))
426{
427 int blocksize_code, sample_rate_code, sample_size_code, crc8;
428 int ch_mode, bps, blocksize, samplerate;
429 int res, ch;
430 GetBitContext *gb = &s->gb;
431
432 blocksize_code = get_bits(gb, 4);
433
434 sample_rate_code = get_bits(gb, 4);
435
436 ch_mode = get_bits(gb, 4); /* channel assignment */
437 if (ch_mode < 8 && s->channels == ch_mode+1)
438 ch_mode = INDEPENDENT;
439 else if (ch_mode >=8 && ch_mode < 11 && s->channels == 2)
440 ch_mode = LEFT_SIDE + ch_mode - 8;
441 else
442 {
443 return -13;
444 }
445
446 sample_size_code = get_bits(gb, 3);
447 if(sample_size_code == 0)
448 bps= s->bps;
449 else if((sample_size_code != 3) && (sample_size_code != 7))
450 bps = sample_size_table[sample_size_code];
451 else
452 {
453 return -14;
454 }
455
456 if (get_bits1(gb))
457 {
458 return -15;
459 }
460
461 /* Get the samplenumber of the first sample in this block */
462 s->samplenumber=get_utf8(gb);
463
464 /* samplenumber actually contains the frame number for streams
465 with a constant block size - so we multiply by blocksize to
466 get the actual sample number */
467 if (s->min_blocksize == s->max_blocksize) {
468 s->samplenumber*=s->min_blocksize;
469 }
470
471#if 0
472 if (/*((blocksize_code == 6) || (blocksize_code == 7)) &&*/
473 (s->min_blocksize != s->max_blocksize)){
474 }else{
475 }
476#endif
477
478 if (blocksize_code == 0)
479 blocksize = s->min_blocksize;
480 else if (blocksize_code == 6)
481 blocksize = get_bits(gb, 8)+1;
482 else if (blocksize_code == 7)
483 blocksize = get_bits(gb, 16)+1;
484 else
485 blocksize = blocksize_table[blocksize_code];
486
487 if(blocksize > s->max_blocksize){
488 return -16;
489 }
490
491 if (sample_rate_code == 0){
492 samplerate= s->samplerate;
493 }else if ((sample_rate_code < 12))
494 samplerate = sample_rate_table[sample_rate_code];
495 else if (sample_rate_code == 12)
496 samplerate = get_bits(gb, 8) * 1000;
497 else if (sample_rate_code == 13)
498 samplerate = get_bits(gb, 16);
499 else if (sample_rate_code == 14)
500 samplerate = get_bits(gb, 16) * 10;
501 else{
502 return -17;
503 }
504
505 skip_bits(gb, 8);
506 crc8= get_crc8(s->gb.buffer, get_bits_count(gb)/8);
507 if(crc8){
508 return -18;
509 }
510
511 s->blocksize = blocksize;
512 s->samplerate = samplerate;
513 s->bps = bps;
514 s->ch_mode = ch_mode;
515
516 for (ch=0; ch<s->channels; ++ch) {
517 yield();
518 if ((res=decode_subframe(s, ch, s->decoded[ch])) < 0)
519 return res-100;
520 }
521
522 yield();
523 align_get_bits(gb);
524
525 /* frame footer */
526 skip_bits(gb, 16); /* data crc */
527
528 return 0;
529}
530
531static int flac_downmix(FLACContext *s) ICODE_ATTR_FLAC;
532static int flac_downmix(FLACContext *s)
533{
534 int32_t *FL, *FR, *FC, *SB, *RL, *RR, *RC;
535 int32_t *outL = s->decoded[0];
536 int32_t *outR = s->decoded[1];
537 int i, scale=FLAC_OUTPUT_DEPTH-s->bps;
538
539 switch(s->channels)
540 {
541 case 3: /* 3.0 channel order: FL FR FC */
542 FL = s->decoded[0];
543 FR = s->decoded[1];
544 FC = s->decoded[2];
545 /* LF = 0.66 LF + 0.33 FC
546 LR = 0.66 LR + 0.33 FC */
547 for (i=0; i<s->blocksize; ++i) {
548 int32_t a = *(FL)*2 + *(FC);
549 int32_t b = *(FR)*2 + *(FC);
550 *outL++ = ((a + (a<<2))>>4) << scale; /* 1/3 ~= 5>>4 */
551 *outR++ = ((b + (b<<2))>>4) << scale; /* 1/3 ~= 5>>4 */
552 FL++; FR++; FC++;
553 }
554 break;
555 case 4: /* 4.0 channel order: FL FR RL RR */
556 FL = s->decoded[0];
557 FR = s->decoded[1];
558 RL = s->decoded[2];
559 RR = s->decoded[3];
560 /* LF = 0.50 LF + 0.50 RL + 0.00 RR
561 LR = 0.50 LR + 0.00 RL + 0.50 RR */
562 for (i=0; i<s->blocksize; ++i) {
563 int32_t a = *(FL) + *(RL);
564 int32_t b = *(FR) + *(RR);
565 *outL++ = (a>>1) << scale;
566 *outR++ = (b>>1) << scale;
567 FL++; FR++; RL++; RR++;
568 }
569 break;
570 case 5: /* 5.0 channel order: FL FR FC RL RR */
571 FL = s->decoded[0];
572 FR = s->decoded[1];
573 FC = s->decoded[2];
574 RL = s->decoded[3];
575 RR = s->decoded[4];
576 /* LF = 0.40 LF + 0.20 FC + 0.40 RL + 0.00 RR
577 LR = 0.40 LR + 0.20 FC + 0.00 RL + 0.40 RR */
578 for (i=0; i<s->blocksize; ++i) {
579 int32_t a = *(FL)*2 + *(FC) + *(RL)*2;
580 int32_t b = *(FR)*2 + *(FC) + *(RR)*2;
581 *outL++ = ((a + (a<<1))>>4) << scale; /* 3>>4 ~= 1/5 */
582 *outR++ = ((b + (b<<1))>>4) << scale; /* 3>>4 ~= 1/5 */
583 FL++; FR++; FC++; RL++; RR++;
584 }
585 break;
586 case 6: /* 5.1 channel order: FL FR FC SUB RL RR */
587 FL = s->decoded[0];
588 FR = s->decoded[1];
589 FC = s->decoded[2];
590 SB = s->decoded[3];
591 RL = s->decoded[4];
592 RR = s->decoded[5];
593 /* LF = 0.33 LF + 0.16 SUB + 0.16 FC + 0.33 RL + 0.00 RR
594 LR = 0.33 LR + 0.16 SUB + 0.16 FC + 0.00 RL + 0.33 RR */
595 for (i=0; i<s->blocksize; ++i) {
596 int32_t a = *(FL)*2 + *(SB) + *(FC) + *(RL)*2;
597 int32_t b = *(FR)*2 + *(SB) + *(FC) + *(RR)*2;
598 *outL++ = ((a + (a<<2))>>5) << scale; /* 5>>5 ~= 1/6 */
599 *outR++ = ((b + (b<<2))>>5) << scale; /* 5>>5 ~= 1/6 */
600 FL++; FR++; SB++; FC++; RL++; RR++;
601 }
602 break;
603 case 7: /* 6.1 channel order: FL FR FC SUB RL RR RC */
604 FL = s->decoded[0];
605 FR = s->decoded[1];
606 FC = s->decoded[2];
607 SB = s->decoded[3];
608 RL = s->decoded[4];
609 RR = s->decoded[5];
610 RC = s->decoded[6];
611 /* LF = 0.28 LF + 0.14 SUB + 0.14 FC + 0.28 RL + 0.00 RR + 0.14 CR
612 LR = 0.28 LR + 0.14 SUB + 0.14 FC + 0.00 RL + 0.28 RR + 0.14 CR */
613 for (i=0; i<s->blocksize; ++i) {
614 int32_t a = *(FL)*2 + *(SB) + *(FC) + *(RL)*2 + *(RC);
615 int32_t b = *(FR)*2 + *(SB) + *(FC) + *(RR)*2 + *(RC);
616 *outL++ = ((a + (a<<4))>>6) << scale; /* 9>>6 ~= 1/7 */
617 *outR++ = ((b + (b<<4))>>6) << scale; /* 9>>6 ~= 1/7 */
618 FL++; FR++; SB++; FC++; RL++; RR++; RC++;
619 }
620 break;
621 default: /* 1.0 and 2.0 do not need downmix, other formats unknown. */
622 return -501;
623 break;
624 }
625 return 0;
626}
627
628int flac_decode_frame(FLACContext *s,
629 uint8_t *buf, int buf_size,
630 void (*yield)(void))
631{
632 int tmp;
633 int i;
634 int framesize;
635 int scale;
636
637 /* check that there is at least the smallest decodable amount of data.
638 this amount corresponds to the smallest valid FLAC frame possible.
639 this amount corresponds to the smallest valid FLAC frame possible.
640 FF F8 69 02 00 00 9A 00 00 34 46 */
641 if (buf_size < MIN_FRAME_SIZE)
642 return buf_size;
643
644 init_get_bits(&s->gb, buf, buf_size*8);
645
646 tmp = get_bits(&s->gb, 16);
647 if ((tmp & 0xFFFE) != 0xFFF8){
648 return -41;
649 }
650
651 if ((framesize=decode_frame(s,yield)) < 0){
652 s->bitstream_size=0;
653 s->bitstream_index=0;
654 return framesize;
655 }
656
657 yield();
658
659#define DECORRELATE(left, right)\
660 for (i = 0; i < s->blocksize; i++) {\
661 int32_t a = s->decoded[0][i];\
662 int32_t b = s->decoded[1][i];\
663 s->decoded[0][i] = (left) << scale;\
664 s->decoded[1][i] = (right) << scale;\
665 }\
666
667 scale=FLAC_OUTPUT_DEPTH-s->bps;
668 switch(s->ch_mode)
669 {
670 case INDEPENDENT:
671 if (s->channels <= 2) {
672 DECORRELATE(a, b) /* Always decorrelate exactly the two supported channels. */
673 } else {
674 if ((tmp=flac_downmix(s)) != 0)
675 return tmp;
676 }
677 break;
678 case LEFT_SIDE:
679 DECORRELATE(a, a-b)
680 break;
681 case RIGHT_SIDE:
682 DECORRELATE(a+b, b)
683 break;
684 case MID_SIDE:
685 DECORRELATE( (a-=b>>1) + b, a)
686 break;
687 }
688
689 s->framesize = (get_bits_count(&s->gb)+7)>>3;
690
691 return 0;
692}