summaryrefslogtreecommitdiff
path: root/audio_codec/libflac/flac_decode.c (plain)
blob: e1ddaaf4f2b01db83ebf0c7249f59f6bda2f4455
1/*
2* filename: flac_decode.c
3*
4*
5*
6*
7*
8*/
9
10#include <stdio.h>
11#include <stdlib.h>
12#include <limits.h>
13#include "codec.h"
14#ifndef __MW__
15#include <inttypes.h>
16#else
17#include "types.h"
18#endif
19#include "crc.h"
20#include "avcodec.h"
21#include "get_bits.h"
22#include "golomb.h"
23
24#include "flac.h"
25#include "flacdata.h"
26#include "../../amadec/adec-armdec-mgt.h"
27
28#include <android/log.h>
29
30#define LOG_TAG "FlacDecoder"
31#define audio_codec_print(...) __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
32
33#define DefaultReadSize 1024*10 //read count from kernel audio buf one time
34#define DefaultOutBufSize 1024*1024
35
36typedef struct OutData {
37 uint8_t * outb;
38 int byte;
39 int index;
40} OutData;
41
42typedef struct FLACContext {
43 FLACSTREAMINFO
44
45 AVCodecContext *avctx; ///< parent AVCodecContext
46 GetBitContext gb; ///< GetBitContext initialized to start at the current frame
47
48 int blocksize; ///< number of samples in the current frame
49 int curr_bps; ///< bps for current subframe, adjusted for channel correlation and wasted bits
50 int sample_shift; ///< shift required to make output samples 16-bit or 32-bit
51 int is32; ///< flag to indicate if output should be 32-bit instead of 16-bit
52 int ch_mode; ///< channel decorrelation type in the current frame
53 int got_streaminfo; ///< indicates if the STREAMINFO has been read
54
55 int32_t *decoded[FLAC_MAX_CHANNELS]; ///< decoded samples
56 uint8_t *bitstream;
57 unsigned int bitstream_size;
58 unsigned int bitstream_index;
59 unsigned int allocated_bitstream_size;
60} FLACContext;
61
62static const int sample_size_table[] =
63{ 0, 8, 12, 0, 16, 20, 24, 0 };
64
65static AVCodecContext acodec;
66static FLACContext flactext;
67static OutData outbuffer;
68#define INPUT_BUF_SIZE 32768
69static int64_t get_utf8(GetBitContext *gb)
70{
71 int64_t val;
72 GET_UTF8(val, get_bits(gb, 8), return -1;)
73 return val;
74}
75
76void *av_malloc(unsigned int size)
77{
78 void *ptr = NULL;
79
80 ptr = malloc(size);
81 return ptr;
82}
83
84void *av_realloc(void *ptr, unsigned int size)
85{
86 return realloc(ptr, size);
87}
88
89void av_freep(void **arg)
90{
91 if (*arg) {
92 free(*arg);
93 }
94 *arg = NULL;
95}
96void av_free(void *arg)
97{
98 if (arg) {
99 free(arg);
100 }
101}
102void *av_mallocz(unsigned int size)
103{
104 void *ptr = malloc(size);
105 if (ptr) {
106 memset(ptr, 0, size);
107 }
108 return ptr;
109}
110
111static inline uint32_t bytestream_get_be32(const uint8_t** ptr)
112{
113 uint32_t tmp;
114 tmp = (*ptr)[3] | ((*ptr)[2] << 8) | ((*ptr)[1] << 16) | ((*ptr)[0] << 24);
115 *ptr += 4;
116 return tmp;
117}
118
119static inline uint32_t bytestream_get_be24(const uint8_t** ptr)
120{
121 uint32_t tmp;
122 tmp = (*ptr)[2] | ((*ptr)[1] << 8) | ((*ptr)[0] << 16) ;
123 *ptr += 3;
124 return tmp;
125}
126
127static inline uint8_t bytestream_get_byte(const uint8_t** ptr)
128{
129 uint8_t tmp;
130 tmp = **ptr;
131 *ptr += 1;
132 return tmp;
133}
134
135
136
137void *av_fast_realloc(void *ptr, unsigned int *size, unsigned int min_size)
138{
139 if (min_size < *size) {
140 return ptr;
141 }
142
143 *size = FFMAX(17 * min_size / 16 + 32, min_size);
144
145 ptr = av_realloc(ptr, *size);
146 if (!ptr) { //we could set this to the unmodified min_size but this is safer if the user lost the ptr and uses NULL now
147 *size = 0;
148 }
149
150 return ptr;
151}
152
153static void allocate_buffers(FLACContext *s)
154{
155 int i;
156
157 if (s->max_framesize == 0 && s->max_blocksize) {
158 s->max_framesize = ff_flac_get_max_frame_size(s->max_blocksize,
159 s->channels, s->bps);
160 }
161
162 for (i = 0; i < s->channels; i++) {
163 s->decoded[i] = av_realloc(s->decoded[i],
164 sizeof(int32_t) * s->max_blocksize);
165 }
166}
167
168
169/**
170 * Parse the STREAMINFO from an inline header.
171 * @param s the flac decoding context
172 * @param buf input buffer, starting with the "fLaC" marker
173 * @param buf_size buffer size
174 * @return non-zero if metadata is invalid
175 */
176static int parse_streaminfo(FLACContext *s, const uint8_t *buf, int buf_size)
177{
178 int metadata_type, metadata_size;
179
180 if (buf_size < FLAC_STREAMINFO_SIZE + 8) {
181 /* need more data */
182 return 0;
183 }
184 ff_flac_parse_block_header(&buf[4], NULL, &metadata_type, &metadata_size);
185 if (metadata_type != FLAC_METADATA_TYPE_STREAMINFO ||
186 metadata_size != FLAC_STREAMINFO_SIZE) {
187 return -1;//AVERROR_INVALIDDATA;
188 }
189 ff_flac_parse_streaminfo(s->avctx, (FLACStreaminfo *)s, &buf[8]);
190 allocate_buffers(s);
191 s->got_streaminfo = 1;
192
193 return 0;
194}
195/**
196 * Determine the size of an inline header.
197 * @param buf input buffer, starting with the "fLaC" marker
198 * @param buf_size buffer size
199 * @return number of bytes in the header, or 0 if more data is needed
200 */
201static int get_metadata_size(const uint8_t *buf, int buf_size)
202{
203 int metadata_last, metadata_size;
204 const uint8_t *buf_end = buf + buf_size;
205
206 buf += 4;
207 do {
208 ff_flac_parse_block_header(buf, &metadata_last, NULL, &metadata_size);
209 buf += 4;
210 if (buf + metadata_size > buf_end) {
211 /* need more data in order to read the complete header */
212 return 0;
213 }
214 buf += metadata_size;
215 } while (!metadata_last);
216
217 return buf_size - (buf_end - buf);
218}
219void ff_flac_parse_streaminfo(AVCodecContext *avctx, struct FLACStreaminfo *s,
220 const uint8_t *buffer)
221{
222 GetBitContext gb;
223 init_get_bits(&gb, buffer, FLAC_STREAMINFO_SIZE * 8);
224
225 skip_bits(&gb, 16); /* skip min blocksize */
226 s->max_blocksize = get_bits(&gb, 16);
227 if (s->max_blocksize < FLAC_MIN_BLOCKSIZE) {
228 audio_codec_print("invalid max blocksize: %d\n", s->max_blocksize);
229 s->max_blocksize = 16;
230 }
231
232 skip_bits(&gb, 24); /* skip min frame size */
233 s->max_framesize = get_bits_long(&gb, 24);
234
235 s->samplerate = get_bits_long(&gb, 20);
236 s->channels = get_bits(&gb, 3) + 1;
237 s->bps = get_bits(&gb, 5) + 1;
238
239 audio_codec_print("## METADATA sp=%d, ch=%d, bps=%d,-------------\n",
240 s->samplerate, s->channels, s->bps);
241
242 avctx->channels = s->channels;
243 avctx->sample_rate = s->samplerate;
244 avctx->bits_per_raw_sample = s->bps;
245
246 s->samples = get_bits_long(&gb, 32) << 4;
247 s->samples |= get_bits(&gb, 4);
248
249 skip_bits_long(&gb, 64); /* md5 sum */
250 skip_bits_long(&gb, 64); /* md5 sum */
251
252}
253static void allocate_buffers(FLACContext *s);
254
255int ff_flac_is_extradata_valid(AVCodecContext *avctx,
256 enum FLACExtradataFormat *format,
257 uint8_t **streaminfo_start)
258{
259 if (!avctx->extradata || avctx->extradata_size < FLAC_STREAMINFO_SIZE) {
260 audio_codec_print("extradata NULL or too small.\n");
261 return 0;
262 }
263 if (AV_RL32(avctx->extradata) != MKTAG('f', 'L', 'a', 'C')) {
264 /* extradata contains STREAMINFO only */
265 if (avctx->extradata_size != FLAC_STREAMINFO_SIZE) {
266 audio_codec_print("extradata contains %d bytes too many.\n",
267 FLAC_STREAMINFO_SIZE - avctx->extradata_size);
268 }
269 *format = FLAC_EXTRADATA_FORMAT_STREAMINFO;
270 *streaminfo_start = avctx->extradata;
271 } else {
272 if (avctx->extradata_size < 8 + FLAC_STREAMINFO_SIZE) {
273 audio_codec_print("extradata too small.\n");
274 return 0;
275 }
276 *format = FLAC_EXTRADATA_FORMAT_FULL_HEADER;
277 *streaminfo_start = &avctx->extradata[8];
278 }
279 return 1;
280}
281static int decode_residuals(FLACContext *s, int channel, int pred_order)
282{
283 int i, tmp, partition, method_type, rice_order;
284 int sample = 0, samples;
285
286 method_type = get_bits(&s->gb, 2);
287 if (method_type > 1) {
288 audio_codec_print("illegal residual coding method %d\n", method_type);
289 return -1;
290 }
291
292 rice_order = get_bits(&s->gb, 4);
293
294 samples = s->blocksize >> rice_order;
295 if (pred_order > samples) {
296 audio_codec_print("invalid predictor order: %i > %i\n", pred_order, samples);
297 return -1;
298 }
299
300 sample =
301 i = pred_order;
302 for (partition = 0; partition < (1 << rice_order); partition++) {
303 tmp = get_bits(&s->gb, method_type == 0 ? 4 : 5);
304 if (tmp == (method_type == 0 ? 15 : 31)) {
305 tmp = get_bits(&s->gb, 5);
306 for (; i < samples; i++, sample++) {
307 s->decoded[channel][sample] = get_sbits_long(&s->gb, tmp);
308 }
309 } else {
310 for (; i < samples; i++, sample++) {
311 s->decoded[channel][sample] = get_sr_golomb_flac(&s->gb, tmp, INT_MAX, 0);
312 }
313 }
314 i = 0;
315 }
316
317 return 0;
318}
319
320
321static int decode_subframe_fixed(FLACContext *s, int channel, int pred_order)
322{
323 const int blocksize = s->blocksize;
324 int32_t *decoded = s->decoded[channel];
325 int av_uninit(a), av_uninit(b), av_uninit(c), av_uninit(d), i;
326
327 /* warm up samples */
328 for (i = 0; i < pred_order; i++) {
329 decoded[i] = get_sbits_long(&s->gb, s->curr_bps);
330 }
331
332 if (decode_residuals(s, channel, pred_order) < 0) {
333 return -1;
334 }
335
336 if (pred_order > 0) {
337 a = decoded[pred_order - 1];
338 }
339 if (pred_order > 1) {
340 b = a - decoded[pred_order - 2];
341 }
342 if (pred_order > 2) {
343 c = b - decoded[pred_order - 2] + decoded[pred_order - 3];
344 }
345 if (pred_order > 3) {
346 d = c - decoded[pred_order - 2] + 2 * decoded[pred_order - 3] - decoded[pred_order - 4];
347 }
348
349 switch (pred_order) {
350 case 0:
351 break;
352 case 1:
353 for (i = pred_order; i < blocksize; i++) {
354 decoded[i] = a += decoded[i];
355 }
356 break;
357 case 2:
358 for (i = pred_order; i < blocksize; i++) {
359 decoded[i] = a += b += decoded[i];
360 }
361 break;
362 case 3:
363 for (i = pred_order; i < blocksize; i++) {
364 decoded[i] = a += b += c += decoded[i];
365 }
366 break;
367 case 4:
368 for (i = pred_order; i < blocksize; i++) {
369 decoded[i] = a += b += c += d += decoded[i];
370 }
371 break;
372 default:
373 audio_codec_print("illegal pred order %d\n", pred_order);
374 return -1;
375 }
376
377 return 0;
378}
379
380
381static int decode_subframe_lpc(FLACContext *s, int channel, int pred_order)
382{
383 int i, j;
384 int coeff_prec, qlevel;
385 int coeffs[32];
386 int32_t *decoded = s->decoded[channel];
387
388 /* warm up samples */
389 for (i = 0; i < pred_order; i++) {
390 decoded[i] = get_sbits_long(&s->gb, s->curr_bps);
391 }
392
393 coeff_prec = get_bits(&s->gb, 4) + 1;
394 if (coeff_prec == 16) {
395 audio_codec_print("invalid coeff precision\n");
396 return -1;
397 }
398 qlevel = get_sbits(&s->gb, 5);
399 if (qlevel < 0) {
400 audio_codec_print("qlevel %d not supported, maybe buggy stream\n", qlevel);
401 return -1;
402 }
403
404 for (i = 0; i < pred_order; i++) {
405 coeffs[i] = get_sbits(&s->gb, coeff_prec);
406 }
407
408 if (decode_residuals(s, channel, pred_order) < 0) {
409 return -1;
410 }
411
412 if (s->bps > 16) {
413 int64_t sum;
414 for (i = pred_order; i < s->blocksize; i++) {
415 sum = 0;
416 for (j = 0; j < pred_order; j++) {
417 sum += (int64_t)coeffs[j] * decoded[i - j - 1];
418 }
419 decoded[i] += sum >> qlevel;
420 }
421 } else {
422 for (i = pred_order; i < s->blocksize - 1; i += 2) {
423 int c;
424 int d = decoded[i - pred_order];
425 int s0 = 0, s1 = 0;
426 for (j = pred_order - 1; j > 0; j--) {
427 c = coeffs[j];
428 s0 += c * d;
429 d = decoded[i - j];
430 s1 += c * d;
431 }
432 c = coeffs[0];
433 s0 += c * d;
434 d = decoded[i] += s0 >> qlevel;
435 s1 += c * d;
436 decoded[i + 1] += s1 >> qlevel;
437 }
438 if (i < s->blocksize) {
439 int sum = 0;
440 for (j = 0; j < pred_order; j++) {
441 sum += coeffs[j] * decoded[i - j - 1];
442 }
443 decoded[i] += sum >> qlevel;
444 }
445 }
446
447 return 0;
448}
449
450void ff_flac_parse_block_header(const uint8_t *block_header,
451 int *last, int *type, int *size)
452{
453 int tmp = bytestream_get_byte(&block_header);
454 if (last) {
455 *last = tmp & 0x80;
456 }
457 if (type) {
458 *type = tmp & 0x7F;
459 }
460 if (size) {
461 *size = bytestream_get_be24(&block_header);
462 }
463}
464
465static inline int decode_subframe(FLACContext *s, int channel)
466{
467 int type, wasted = 0;
468 int i, tmp;
469
470 s->curr_bps = s->bps;
471 if (channel == 0) {
472 if (s->ch_mode == FLAC_CHMODE_RIGHT_SIDE) {
473 s->curr_bps++;
474 }
475 } else {
476 if (s->ch_mode == FLAC_CHMODE_LEFT_SIDE || s->ch_mode == FLAC_CHMODE_MID_SIDE) {
477 s->curr_bps++;
478 }
479 }
480
481 if (get_bits1(&s->gb)) {
482 audio_codec_print("invalid subframe padding\n");
483 return -1;
484 }
485 type = get_bits(&s->gb, 6);
486
487 if (get_bits1(&s->gb)) {
488 wasted = 1;
489 while (!get_bits1(&s->gb)) {
490 wasted++;
491 }
492 s->curr_bps -= wasted;
493 }
494 if (s->curr_bps > 32) {
495 audio_codec_print("decorrelated bit depth > 32");
496 return -1;
497 }
498
499 //FIXME use av_log2 for types
500 if (type == 0) {
501 tmp = get_sbits_long(&s->gb, s->curr_bps);
502 for (i = 0; i < s->blocksize; i++) {
503 s->decoded[channel][i] = tmp;
504 }
505 } else if (type == 1) {
506 for (i = 0; i < s->blocksize; i++) {
507 s->decoded[channel][i] = get_sbits_long(&s->gb, s->curr_bps);
508 }
509 } else if ((type >= 8) && (type <= 12)) {
510 if (decode_subframe_fixed(s, channel, type & ~0x8) < 0) {
511 return -1;
512 }
513 } else if (type >= 32) {
514 if (decode_subframe_lpc(s, channel, (type & ~0x20) + 1) < 0) {
515 return -1;
516 }
517 } else {
518 audio_codec_print("invalid coding type\n");
519 return -1;
520 }
521
522 if (wasted) {
523 int i;
524 for (i = 0; i < s->blocksize; i++) {
525 s->decoded[channel][i] <<= wasted;
526 }
527 }
528
529 return 0;
530}
531
532static int decode_frame_header(GetBitContext *gb, FLACFrameInfo *fi)
533{
534 int bs_code, sr_code, bps_code;
535
536 /* frame sync code */
537 //skip_bits(gb, 16);
538 if ((get_bits(gb, 15) & 0x7FFF) != 0x7FFC) {
539 audio_codec_print("invalid sync code-----------------------\n");
540 return -1;
541 }
542 skip_bits(gb, 1);
543
544 /* block size and sample rate codes */
545 bs_code = get_bits(gb, 4);
546 sr_code = get_bits(gb, 4);
547
548 /* channels and decorrelation */
549 fi->ch_mode = get_bits(gb, 4);
550 if (fi->ch_mode < FLAC_MAX_CHANNELS) {
551 fi->channels = fi->ch_mode + 1;
552 fi->ch_mode = FLAC_CHMODE_INDEPENDENT;
553 } else if (fi->ch_mode <= FLAC_CHMODE_MID_SIDE) {
554 fi->channels = 2;
555 } else {
556 audio_codec_print("invalid channel mode: %d\n", fi->ch_mode);
557 return -1;
558 }
559
560 /* bits per sample */
561 bps_code = get_bits(gb, 3);
562 if (bps_code == 3 || bps_code == 7) {
563 audio_codec_print("invalid sample size code (%d)\n", bps_code);
564 return -1;
565 }
566 fi->bps = sample_size_table[bps_code];
567
568 /* reserved bit */
569 if (get_bits1(gb)) {
570 audio_codec_print("broken stream, invalid padding\n");
571 return -1;
572 }
573
574 /* sample or frame count */
575 if (get_utf8(gb) < 0) {
576 audio_codec_print("utf8 fscked\n");
577 return -1;
578 }
579
580 /* blocksize */
581 if (bs_code == 0) {
582 audio_codec_print("reserved blocksize code: 0\n");
583 return -1;
584 } else if (bs_code == 6) {
585 fi->blocksize = get_bits(gb, 8) + 1;
586 } else if (bs_code == 7) {
587 fi->blocksize = get_bits(gb, 16) + 1;
588 } else {
589 fi->blocksize = ff_flac_blocksize_table[bs_code];
590 }
591
592 /* sample rate */
593 if (sr_code < 12) {
594 fi->samplerate = ff_flac_sample_rate_table[sr_code];
595 } else if (sr_code == 12) {
596 fi->samplerate = get_bits(gb, 8) * 1000;
597 } else if (sr_code == 13) {
598 fi->samplerate = get_bits(gb, 16);
599 } else if (sr_code == 14) {
600 fi->samplerate = get_bits(gb, 16) * 10;
601 } else {
602 audio_codec_print("illegal sample rate code %d\n", sr_code);
603 return -1;
604 }
605
606 /* header CRC-8 check */
607 skip_bits(gb, 8);
608 if (av_crc(av_crc_get_table(AV_CRC_8_ATM), 0, gb->buffer,
609 get_bits_count(gb) / 8)) {
610 audio_codec_print("header crc mismatch\n");
611 return -1;
612 }
613
614 return 0;
615}
616
617static int decode_frame(FLACContext *s)
618{
619 int i;
620 GetBitContext *gb = &s->gb;
621 FLACFrameInfo fi;
622
623 if (decode_frame_header(gb, &fi)) {
624 //audio_codec_print("invalid frame header\n");
625 return -1;
626 }
627
628 if (fi.channels != s->channels) {
629 audio_codec_print("switching channel layout mid-stream is not supported\n");
630 return -1;
631 }
632 s->ch_mode = fi.ch_mode;
633
634 if (fi.bps && fi.bps != s->bps) {
635 audio_codec_print("switching bps mid-stream is not supported\n");
636 return -1;
637 }
638 if (0/*s->bps > 16*/) {
639 s->avctx->sample_fmt = SAMPLE_FMT_S32;
640 s->sample_shift = 32 - s->bps;
641 s->is32 = 1;
642 } else {
643 // s->avctx->sample_fmt = SAMPLE_FMT_S16;
644 s->sample_shift = 16 - s->bps;
645 s->is32 = 0;
646 }
647
648 if (fi.blocksize > s->max_blocksize) {
649 audio_codec_print("blocksize %d > %d\n", fi.blocksize, s->max_blocksize);
650 return -1;
651 }
652 s->blocksize = fi.blocksize;
653
654 if (fi.samplerate == 0) {
655 fi.samplerate = s->samplerate;
656 } else if (fi.samplerate != s->samplerate) {
657 audio_codec_print("sample rate changed from %d to %d\n", s->samplerate, fi.samplerate);
658 }
659 s->samplerate = s->avctx->sample_rate = fi.samplerate;
660
661
662 /* subframes */
663 for (i = 0; i < s->channels; i++) {
664 if (decode_subframe(s, i) < 0) {
665 return -1;
666 }
667 }
668
669 align_get_bits(gb);
670
671 /* frame footer */
672 skip_bits(gb, 16); /* data crc */
673
674 return 0;
675}
676
677static inline int av_clipf1(float a, int amin, int amax)
678{
679 if (a < amin) {
680 return (int)amin;
681 } else if (a > amax) {
682 return (int)amax;
683 } else {
684 return (int)a;
685 }
686}
687int audio_dec_decode(audio_decoder_operations_t *adec_ops, char *outbuf, int *outlen, char *inbuf, int inlen)
688{
689 AVCodecContext *avctx = &acodec;
690 FLACContext *s = &flactext;
691 int i, j = 0, input_buf_size = 0, bytes_read = 0;
692 int16_t *samples_16 = (int16_t*)outbuf;
693 int32_t *samples_32 = (int32_t*)outbuf;
694 int output_size = 0;
695 unsigned char *buf;
696 unsigned buf_size;
697 int last_sync_pos = 0;
698
699 unsigned int max_framesize = FFMAX(17 * s->max_framesize / 16 + 32, s->max_framesize);
700 if (inlen < max_framesize) {
701 int inbufindex = 0;
702 char *pinbufptr = inbuf;
703 char para = *(pinbufptr + 2);
704 int frame_num = 0;
705
706 inbufindex += 11;
707 pinbufptr += 11;
708 while (inbufindex < inlen - FLAC_MIN_FRAME_SIZE + 2) {
709 if ((AV_RB16(pinbufptr) & 0xFFFF) != 0xFFF8 || AV_RB8(pinbufptr + 2) != para) {
710 pinbufptr++;
711 inbufindex++;
712 } else {
713 inlen = inbufindex;
714 frame_num++;
715 if (frame_num > 1) {
716 audio_codec_print("## next frame found, inbufindex=%d, frame_num=%d,-------\n", inbufindex, frame_num);
717 goto decodecontinue;
718 }
719 }
720 }
721 //audio_codec_print("##inlen=%d, diff=%d,-------------------\n", inlen, pinbufptr-inbuf);
722 return -1;
723 }
724
725decodecontinue:
726 s->bitstream = inbuf;
727 s->bitstream_size = inlen;
728 s->bitstream_index = 0;
729
730 buf = s->bitstream;
731 buf_size = s->bitstream_size;
732
733 /* check for inline header */
734 if (AV_RB32(buf) == MKBETAG('f', 'L', 'a', 'C')) {
735 if (!s->got_streaminfo && parse_streaminfo(s, buf, buf_size)) {
736 audio_codec_print("invalid header\n");
737 return -1;
738 }
739 bytes_read = get_metadata_size(buf, buf_size);
740 goto end;
741 }
742FIND_SYNC_WORD:
743 /* check for frame sync code and resync stream if necessary */
744 if ((AV_RB16(buf) & 0xFFFE) != 0xFFF8) {
745 const uint8_t *buf_end = buf + buf_size;
746 // av_log(s->avctx, AV_LOG_ERROR, "FRAME HEADER not here\n");
747 while (buf + 2 < buf_end && (AV_RB16(buf) & 0xFFFE) != 0xFFF8) {
748 buf++;
749 last_sync_pos++;
750 }
751 bytes_read = buf_size - (buf_end - buf);
752 goto end; // we may not have enough bits left to decode a frame, so try next time
753 }
754
755 /* decode frame */
756 init_get_bits(&s->gb, buf, buf_size * 8);
757 if (decode_frame(s) < 0) {
758 audio_codec_print("decode_frame() failed\n");
759 //some times , flac may seek to fake sync word pos, caused decode failed
760 //call resync to fix this issue
761 buf = s->bitstream + last_sync_pos + 1;
762 buf_size = s->bitstream_size - (last_sync_pos + 1);
763 last_sync_pos++;
764 goto FIND_SYNC_WORD;
765 }
766 bytes_read = (get_bits_count(&s->gb) + 7) / 8;
767
768 /* check if allocated data size is large enough for output */
769 output_size = s->blocksize * (s->channels > 2 ? 2 : s->channels) * (s->is32 ? 4 : 2);
770 if (output_size > DefaultOutBufSize) {
771 audio_codec_print("output data size is larger than allocated data size\n");
772 goto end;
773 }
774
775#define DECORRELATE(left, right)\
776 for (i = 0; i < s->blocksize; i++) {\
777 int a= s->decoded[0][i];\
778 int b= s->decoded[1][i];\
779 if (s->is32) {\
780 *samples_32++ = (left) << s->sample_shift;\
781 *samples_32++ = (right) << s->sample_shift;\
782 } else {\
783 if (s->sample_shift >= 0) \
784 {\
785 *samples_16++ = (left) << s->sample_shift;\
786 *samples_16++ = (right) << s->sample_shift;\
787 }\
788 else\
789 {\
790 *samples_16++ = (left) >> (-1*s->sample_shift);\
791 *samples_16++ = (right) >> (-1*s->sample_shift);\
792 }\
793 }\
794 }\
795 break;
796
797 switch (s->ch_mode) {
798 case FLAC_CHMODE_INDEPENDENT:
799 if (s->channels <= 2) {
800 for (j = 0; j < s->blocksize; j++) {
801 for (i = 0; i < s->channels; i++) {
802 if (s->is32) {
803 *samples_32++ = s->decoded[i][j] << s->sample_shift;
804 } else {
805 if (s->sample_shift >= 0) {
806 *samples_16++ = s->decoded[i][j] << s->sample_shift;
807 } else {
808 *samples_16++ = s->decoded[i][j] >> (-1 * s->sample_shift);
809 }
810 }
811 }
812 }
813 } else {
814 float sum0 = 0, sum1 = 0;
815 //downmix here
816 //FL + (sum of other surround channels >> 1)
817 //FR + (sum of other surround channels >> 1)
818 for (j = 0; j < s->blocksize; j++) {
819 if (s->is32) {
820 sum0 = s->decoded[0][j] << s->sample_shift;
821 sum1 = s->decoded[1][j] << s->sample_shift;
822 } else {
823 if (s->sample_shift >= 0) {
824 sum0 = s->decoded[0][j] << s->sample_shift;
825 sum1 = s->decoded[1][j] << s->sample_shift;
826 } else {
827 sum0 = s->decoded[0][j] >> (-1 * s->sample_shift);
828 sum1 = s->decoded[1][j] >> (-1 * s->sample_shift);
829 }
830 }
831 for (i = 2; i < s->channels; i++) {
832 if (s->is32) {
833 sum0 += (s->decoded[i][j] << s->sample_shift) >> 1;
834 sum1 += (s->decoded[i][j] << s->sample_shift) >> 1;
835 } else {
836 if (s->sample_shift >= 0) {
837 sum0 += (s->decoded[i][j] << s->sample_shift) >> 1;
838 sum1 += (s->decoded[i][j] << s->sample_shift) >> 1;
839 } else {
840 sum0 += (s->decoded[i][j] >> (-1 * s->sample_shift)) >> 1;
841 sum1 += (s->decoded[i][j] >> (-1 * s->sample_shift)) >> 1;
842 }
843 }
844 }
845
846 if (s->is32) {
847 *samples_32++ = av_clipf1(sum0, -0x80000000, 0x7fffffff);
848 *samples_32++ = av_clipf1(sum1, -0x80000000, 0x7fffffff);
849 } else {
850 *samples_16++ = av_clipf1(sum0, -32768, 32767);
851 *samples_16++ = av_clipf1(sum1, -32768, 32767);
852 }
853 }
854 }
855 break;
856 case FLAC_CHMODE_LEFT_SIDE:
857 DECORRELATE(a, a - b)
858 case FLAC_CHMODE_RIGHT_SIDE:
859 DECORRELATE(a + b, b)
860 case FLAC_CHMODE_MID_SIDE:
861 DECORRELATE((a -= b >> 1) + b, a)
862 }
863
864end:
865 if (bytes_read > inlen) {
866 // av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", bytes_read - buf_size);
867 s->bitstream_size = 0;
868 s->bitstream_index = 0;
869 return -1;
870 }
871
872 if (s->bitstream_size) {
873 s->bitstream_index += bytes_read;
874 s->bitstream_size -= bytes_read;
875 }
876
877 *outlen = output_size;
878
879 return bytes_read;
880}
881
882int audio_dec_init(audio_decoder_operations_t *adec_ops)
883{
884 memset(&flactext , 0, sizeof(flactext));
885 memset(&acodec, 0, sizeof(acodec));
886 enum FLACExtradataFormat format;
887 uint8_t *streaminfo;
888 AVCodecContext *avctx = &acodec;
889 FLACContext *s = &flactext;
890 s->avctx = &acodec;
891 //audio_codec_print("\n\n[%s]BuildDate--%s BuildTime--%s", __FUNCTION__, __DATE__, __TIME__);
892 avctx->sample_fmt = SAMPLE_FMT_S16;
893 avctx->extradata = adec_ops->extradata;
894 avctx->extradata_size = adec_ops->extradata_size;
895
896 if (!avctx->extradata_size) {
897 return 0;
898 }
899 if (!ff_flac_is_extradata_valid(avctx, &format, &streaminfo)) {
900 return -1;
901 }
902
903 /* initialize based on the demuxer-supplied streamdata header */
904 ff_flac_parse_streaminfo(avctx, (FLACStreaminfo *)s, streaminfo);
905 if (s->bps > 16) {
906 avctx->sample_fmt = SAMPLE_FMT_S32;
907 } else {
908 avctx->sample_fmt = SAMPLE_FMT_S16;
909 }
910 allocate_buffers(s);
911 s->got_streaminfo = 1;
912 avctx->channels = (avctx->channels > 2 ? 2 : avctx->channels);
913 audio_codec_print("applied flac sr %d,ch num %d\n", avctx->sample_rate, avctx->channels);
914
915 adec_ops->nInBufSize = DefaultReadSize;
916 adec_ops->nOutBufSize = DefaultOutBufSize;
917 audio_codec_print("ape_Init.--------------------------------\n");
918
919 return 0;
920}
921
922int audio_dec_release(audio_decoder_operations_t *adec_ops)
923{
924 int i;
925 audio_codec_print("audio_dec_release.--------------------------------\n");
926
927 if (outbuffer.outb == NULL) {
928 av_freep(&outbuffer.outb);
929 }
930
931 for (i = 0; i < flactext.channels; i++) {
932 av_freep(&flactext.decoded[i]);
933 }
934 // av_free(flactext.bitstream);
935
936 return 0;
937}
938
939int audio_dec_getinfo(audio_decoder_operations_t *adec_ops, void *pAudioInfo)
940{
941 return 0;
942}
943
944