62
62
int min_blocksize, max_blocksize;
63
int min_framesize, max_framesize;
64
64
int samplerate, channels;
65
65
int blocksize/*, last_blocksize*/;
98
98
static void allocate_buffers(FLACContext *s);
99
99
static int metadata_parse(FLACContext *s);
101
static int flac_decode_init(AVCodecContext * avctx)
101
static av_cold int flac_decode_init(AVCodecContext * avctx)
103
103
FLACContext *s = avctx->priv_data;
104
104
s->avctx = avctx;
120
120
static void dump_headers(FLACContext *s)
122
122
av_log(s->avctx, AV_LOG_DEBUG, " Blocksize: %d .. %d (%d)\n", s->min_blocksize, s->max_blocksize, s->blocksize);
123
av_log(s->avctx, AV_LOG_DEBUG, " Framesize: %d .. %d\n", s->min_framesize, s->max_framesize);
123
av_log(s->avctx, AV_LOG_DEBUG, " Max Framesize: %d\n", s->max_framesize);
124
124
av_log(s->avctx, AV_LOG_DEBUG, " Samplerate: %d\n", s->samplerate);
125
125
av_log(s->avctx, AV_LOG_DEBUG, " Channels: %d\n", s->channels);
126
126
av_log(s->avctx, AV_LOG_DEBUG, " Bits: %d\n", s->bps);
149
149
s->min_blocksize = get_bits(&s->gb, 16);
150
150
s->max_blocksize = get_bits(&s->gb, 16);
152
s->min_framesize = get_bits_long(&s->gb, 24);
152
skip_bits(&s->gb, 24); /* skip min frame size */
153
153
s->max_framesize = get_bits_long(&s->gb, 24);
155
155
s->samplerate = get_bits_long(&s->gb, 20);
184
184
av_log(s->avctx, AV_LOG_DEBUG, "STREAM HEADER\n");
186
metadata_last = get_bits(&s->gb, 1);
186
metadata_last = get_bits1(&s->gb);
187
187
metadata_type = get_bits(&s->gb, 7);
188
188
metadata_size = get_bits_long(&s->gb, 24);
235
235
for (partition = 0; partition < (1 << rice_order); partition++)
237
tmp = get_bits(&s->gb, 4);
237
tmp = get_bits(&s->gb, method_type == 0 ? 4 : 5);
238
if (tmp == (method_type == 0 ? 15 : 31))
240
240
av_log(s->avctx, AV_LOG_DEBUG, "fixed len partition\n");
241
241
tmp = get_bits(&s->gb, 5);
260
260
static int decode_subframe_fixed(FLACContext *s, int channel, int pred_order)
262
const int blocksize = s->blocksize;
263
int32_t *decoded = s->decoded[channel];
264
266
// av_log(s->avctx, AV_LOG_DEBUG, " SUBFRAME FIXED\n");
269
271
for (i = 0; i < pred_order; i++)
271
s->decoded[channel][i] = get_sbits(&s->gb, s->curr_bps);
273
decoded[i] = get_sbits(&s->gb, s->curr_bps);
272
274
// av_log(s->avctx, AV_LOG_DEBUG, " %d: %d\n", i, s->decoded[channel][i]);
275
277
if (decode_residuals(s, channel, pred_order) < 0)
280
a = decoded[pred_order-1];
281
b = a - decoded[pred_order-2];
282
c = b - decoded[pred_order-2] + decoded[pred_order-3];
283
d = c - decoded[pred_order-2] + 2*decoded[pred_order-3] - decoded[pred_order-4];
278
285
switch(pred_order)
283
for (i = pred_order; i < s->blocksize; i++)
284
s->decoded[channel][i] += s->decoded[channel][i-1];
290
for (i = pred_order; i < blocksize; i++)
291
decoded[i] = a += decoded[i];
287
for (i = pred_order; i < s->blocksize; i++)
288
s->decoded[channel][i] += 2*s->decoded[channel][i-1]
289
- s->decoded[channel][i-2];
294
for (i = pred_order; i < blocksize; i++)
295
decoded[i] = a += b += decoded[i];
292
for (i = pred_order; i < s->blocksize; i++)
293
s->decoded[channel][i] += 3*s->decoded[channel][i-1]
294
- 3*s->decoded[channel][i-2]
295
+ s->decoded[channel][i-3];
298
for (i = pred_order; i < blocksize; i++)
299
decoded[i] = a += b += c += decoded[i];
298
for (i = pred_order; i < s->blocksize; i++)
299
s->decoded[channel][i] += 4*s->decoded[channel][i-1]
300
- 6*s->decoded[channel][i-2]
301
+ 4*s->decoded[channel][i-3]
302
- s->decoded[channel][i-4];
302
for (i = pred_order; i < blocksize; i++)
303
decoded[i] = a += b += c += d += decoded[i];
305
306
av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order);
323
325
for (i = 0; i < pred_order; i++)
325
s->decoded[channel][i] = get_sbits(&s->gb, s->curr_bps);
326
// av_log(s->avctx, AV_LOG_DEBUG, " %d: %d\n", i, s->decoded[channel][i]);
327
decoded[i] = get_sbits(&s->gb, s->curr_bps);
328
// av_log(s->avctx, AV_LOG_DEBUG, " %d: %d\n", i, decoded[i]);
329
331
coeff_prec = get_bits(&s->gb, 4) + 1;
357
359
for (j = 0; j < pred_order; j++)
358
sum += (int64_t)coeffs[j] * s->decoded[channel][i-j-1];
359
s->decoded[channel][i] += sum >> qlevel;
360
sum += (int64_t)coeffs[j] * decoded[i-j-1];
361
decoded[i] += sum >> qlevel;
363
for (i = pred_order; i < s->blocksize; i++)
364
for (i = pred_order; i < s->blocksize-1; i += 2)
367
int d = decoded[i-pred_order];
369
for (j = pred_order-1; j > 0; j--)
378
d = decoded[i] += s0 >> qlevel;
380
decoded[i+1] += s1 >> qlevel;
382
if (i < s->blocksize)
366
385
for (j = 0; j < pred_order; j++)
367
sum += coeffs[j] * s->decoded[channel][i-j-1];
368
s->decoded[channel][i] += sum >> qlevel;
386
sum += coeffs[j] * decoded[i-j-1];
387
decoded[i] += sum >> qlevel;
541
560
skip_bits(&s->gb, 8);
542
crc8= av_crc(av_crc07, 0, s->gb.buffer, get_bits_count(&s->gb)/8);
561
crc8 = av_crc(av_crc_get_table(AV_CRC_8_ATM), 0,
562
s->gb.buffer, get_bits_count(&s->gb)/8);
544
564
av_log(s->avctx, AV_LOG_ERROR, "header crc mismatch crc=%2X\n", crc8);
571
static inline int16_t shift_to_16_bits(int32_t data, int bps)
575
} else if (bps == 20) {
582
591
static int flac_decode_frame(AVCodecContext *avctx,
583
592
void *data, int *data_size,
584
uint8_t *buf, int buf_size)
593
const uint8_t *buf, int buf_size)
586
595
FLACContext *s = avctx->priv_data;
587
596
int tmp = 0, i, j = 0, input_buf_size = 0;
620
629
if (!metadata_parse(s))
622
631
tmp = show_bits(&s->gb, 16);
632
if((tmp & 0xFFFE) != 0xFFF8){
624
633
av_log(s->avctx, AV_LOG_ERROR, "FRAME HEADER not here\n");
625
while(get_bits_count(&s->gb)/8+2 < buf_size && show_bits(&s->gb, 16) != 0xFFF8)
634
while(get_bits_count(&s->gb)/8+2 < buf_size && (show_bits(&s->gb, 16) & 0xFFFE) != 0xFFF8)
626
635
skip_bits(&s->gb, 8);
627
636
goto end; // we may not have enough bits left to decode a frame, so try next time
685
694
int a= s->decoded[0][i];\
686
695
int b= s->decoded[1][i];\
687
*(samples++) = (left ) >> (16 - s->bps);\
688
*(samples++) = (right) >> (16 - s->bps);\
696
*samples++ = ((left) << (24 - s->bps)) >> 8;\
697
*samples++ = ((right) << (24 - s->bps)) >> 8;\
695
704
for (j = 0; j < s->blocksize; j++)
697
706
for (i = 0; i < s->channels; i++)
698
*(samples++) = shift_to_16_bits(s->decoded[i][j], s->bps);
707
*samples++ = (s->decoded[i][j] << (24 - s->bps)) >> 8;
713
722
// s->last_blocksize = s->blocksize;
715
i= (get_bits_count(&s->gb)+7)/8;;
724
i= (get_bits_count(&s->gb)+7)/8;
716
725
if(i > buf_size){
717
726
av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
718
727
s->bitstream_size=0;