- for (i = pred_order; i < s->blocksize-1; i += 2)
- {
- int c;
- int d = decoded[i-pred_order];
- int s0 = 0, s1 = 0;
- for (j = pred_order-1; j > 0; j--)
- {
- c = coeffs[j];
- s0 += c*d;
- d = decoded[i-j];
- s1 += c*d;
- }
- c = coeffs[0];
- s0 += c*d;
- d = decoded[i] += s0 >> qlevel;
- s1 += c*d;
- decoded[i+1] += s1 >> qlevel;
- }
- if (i < s->blocksize)
- {
- int sum = 0;
- for (j = 0; j < pred_order; j++)
- sum += coeffs[j] * decoded[i-j-1];
- decoded[i] += sum >> qlevel;
- }
- }
-
- return 0;
-}
-
-static inline int decode_subframe(FLACContext *s, int channel)
-{
- int type, wasted = 0;
- int i, tmp;
-
- s->curr_bps = s->bps;
- if(channel == 0){
- if(s->decorrelation == RIGHT_SIDE)
- s->curr_bps++;
- }else{
- if(s->decorrelation == LEFT_SIDE || s->decorrelation == MID_SIDE)
- s->curr_bps++;
- }
-
- if (get_bits1(&s->gb))
- {
- av_log(s->avctx, AV_LOG_ERROR, "invalid subframe padding\n");
- return -1;
- }
- type = get_bits(&s->gb, 6);
-// wasted = get_bits1(&s->gb);
-
-// if (wasted)
-// {
-// while (!get_bits1(&s->gb))
-// wasted++;
-// if (wasted)
-// wasted++;
-// s->curr_bps -= wasted;
-// }
-#if 0
- wasted= 16 - av_log2(show_bits(&s->gb, 17));
- skip_bits(&s->gb, wasted+1);
- s->curr_bps -= wasted;
-#else
- if (get_bits1(&s->gb))
- {
- wasted = 1;
- while (!get_bits1(&s->gb))
- wasted++;
- s->curr_bps -= wasted;
- av_log(s->avctx, AV_LOG_DEBUG, "%d wasted bits\n", wasted);
- }
-#endif
-//FIXME use av_log2 for types
- if (type == 0)
- {
- av_log(s->avctx, AV_LOG_DEBUG, "coding type: constant\n");
- tmp = get_sbits(&s->gb, s->curr_bps);
- for (i = 0; i < s->blocksize; i++)
- s->decoded[channel][i] = tmp;
- }
- else if (type == 1)
- {
- av_log(s->avctx, AV_LOG_DEBUG, "coding type: verbatim\n");
- for (i = 0; i < s->blocksize; i++)
- s->decoded[channel][i] = get_sbits(&s->gb, s->curr_bps);
- }
- else if ((type >= 8) && (type <= 12))
- {
-// av_log(s->avctx, AV_LOG_DEBUG, "coding type: fixed\n");
- if (decode_subframe_fixed(s, channel, type & ~0x8) < 0)
- return -1;
- }
- else if (type >= 32)
- {
-// av_log(s->avctx, AV_LOG_DEBUG, "coding type: lpc\n");
- if (decode_subframe_lpc(s, channel, (type & ~0x20)+1) < 0)
- return -1;
- }
- else
- {
- av_log(s->avctx, AV_LOG_ERROR, "invalid coding type\n");
- return -1;
- }
-
- if (wasted)
- {
- int i;
- for (i = 0; i < s->blocksize; i++)
- s->decoded[channel][i] <<= wasted;
- }
-
- return 0;
-}
-
-static int decode_frame(FLACContext *s, int alloc_data_size)
-{
- int blocksize_code, sample_rate_code, sample_size_code, assignment, i, crc8;
- int decorrelation, bps, blocksize, samplerate;
-
- blocksize_code = get_bits(&s->gb, 4);
-
- sample_rate_code = get_bits(&s->gb, 4);
-
- assignment = get_bits(&s->gb, 4); /* channel assignment */
- if (assignment < 8 && s->channels == assignment+1)
- decorrelation = INDEPENDENT;
- else if (assignment >=8 && assignment < 11 && s->channels == 2)
- decorrelation = LEFT_SIDE + assignment - 8;
- else
- {
- av_log(s->avctx, AV_LOG_ERROR, "unsupported channel assignment %d (channels=%d)\n", assignment, s->channels);
- return -1;
- }
-
- sample_size_code = get_bits(&s->gb, 3);
- if(sample_size_code == 0)
- bps= s->bps;
- else if((sample_size_code != 3) && (sample_size_code != 7))
- bps = sample_size_table[sample_size_code];
- else
- {
- av_log(s->avctx, AV_LOG_ERROR, "invalid sample size code (%d)\n", sample_size_code);
- return -1;
- }
-
- if (get_bits1(&s->gb))
- {
- av_log(s->avctx, AV_LOG_ERROR, "broken stream, invalid padding\n");
- return -1;
- }
-
- if(get_utf8(&s->gb) < 0){
- av_log(s->avctx, AV_LOG_ERROR, "utf8 fscked\n");
- return -1;
- }
-#if 0
- if (/*((blocksize_code == 6) || (blocksize_code == 7)) &&*/
- (s->min_blocksize != s->max_blocksize)){
- }else{
- }
-#endif
-
- if (blocksize_code == 0)
- blocksize = s->min_blocksize;
- else if (blocksize_code == 6)
- blocksize = get_bits(&s->gb, 8)+1;
- else if (blocksize_code == 7)
- blocksize = get_bits(&s->gb, 16)+1;
- else
- blocksize = blocksize_table[blocksize_code];
-
- if(blocksize > s->max_blocksize){
- av_log(s->avctx, AV_LOG_ERROR, "blocksize %d > %d\n", blocksize, s->max_blocksize);
- return -1;
- }
-
- if(blocksize * s->channels * sizeof(int16_t) > alloc_data_size)
- return -1;
-
- if (sample_rate_code == 0){
- samplerate= s->samplerate;
- }else if ((sample_rate_code > 3) && (sample_rate_code < 12))
- samplerate = sample_rate_table[sample_rate_code];
- else if (sample_rate_code == 12)
- samplerate = get_bits(&s->gb, 8) * 1000;
- else if (sample_rate_code == 13)
- samplerate = get_bits(&s->gb, 16);
- else if (sample_rate_code == 14)
- samplerate = get_bits(&s->gb, 16) * 10;
- else{
- av_log(s->avctx, AV_LOG_ERROR, "illegal sample rate code %d\n", sample_rate_code);
- return -1;
- }
-
- skip_bits(&s->gb, 8);
- crc8 = av_crc(av_crc_get_table(AV_CRC_8_ATM), 0,
- s->gb.buffer, get_bits_count(&s->gb)/8);
- if(crc8){
- av_log(s->avctx, AV_LOG_ERROR, "header crc mismatch crc=%2X\n", crc8);
- return -1;
- }
-
- s->blocksize = blocksize;
- s->samplerate = samplerate;
- s->bps = bps;
- s->decorrelation= decorrelation;
-
-// dump_headers(s);
-
- /* subframes */
- for (i = 0; i < s->channels; i++)
- {
-// av_log(s->avctx, AV_LOG_DEBUG, "decoded: %x residual: %x\n", s->decoded[i], s->residual[i]);
- if (decode_subframe(s, i) < 0)
- return -1;
- }
-
- align_get_bits(&s->gb);
-
- /* frame footer */
- skip_bits(&s->gb, 16); /* data crc */
-
- return 0;
-}
-
-static int flac_decode_frame(AVCodecContext *avctx,
- void *data, int *data_size,
- const uint8_t *buf, int buf_size)
-{
- FLACContext *s = avctx->priv_data;
- int tmp = 0, i, j = 0, input_buf_size = 0;
- int16_t *samples = data;
- int alloc_data_size= *data_size;
-
- *data_size=0;
-
- if(s->max_framesize == 0){
- s->max_framesize= 65536; // should hopefully be enough for the first header
- s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);
- }
-
- if(1 && s->max_framesize){//FIXME truncated
- buf_size= FFMAX(FFMIN(buf_size, s->max_framesize - s->bitstream_size), 0);
- input_buf_size= buf_size;
-
- if(s->bitstream_index + s->bitstream_size + buf_size > s->allocated_bitstream_size){
-// printf("memmove\n");
- memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);
- s->bitstream_index=0;
- }
- memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf, buf_size);
- buf= &s->bitstream[s->bitstream_index];
- buf_size += s->bitstream_size;
- s->bitstream_size= buf_size;
-
- if(buf_size < s->max_framesize){
-// printf("wanna more data ...\n");
- return input_buf_size;
- }
- }
-
- init_get_bits(&s->gb, buf, buf_size*8);
-
- if (!metadata_parse(s))
- {
- tmp = show_bits(&s->gb, 16);
- if((tmp & 0xFFFE) != 0xFFF8){
- av_log(s->avctx, AV_LOG_ERROR, "FRAME HEADER not here\n");
- while(get_bits_count(&s->gb)/8+2 < buf_size && (show_bits(&s->gb, 16) & 0xFFFE) != 0xFFF8)
- skip_bits(&s->gb, 8);
- goto end; // we may not have enough bits left to decode a frame, so try next time
- }
- skip_bits(&s->gb, 16);
- if (decode_frame(s, alloc_data_size) < 0){
- av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n");
- s->bitstream_size=0;
- s->bitstream_index=0;
- return -1;
- }