- 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;
- }
- }
-
-
-#if 0
- /* fix the channel order here */
- if (s->order == MID_SIDE)
- {
- short *left = samples;
- short *right = samples + s->blocksize;
- for (i = 0; i < s->blocksize; i += 2)
- {
- uint32_t x = s->decoded[0][i];
- uint32_t y = s->decoded[0][i+1];
-
- right[i] = x - (y / 2);
- left[i] = right[i] + y;
- }
- *data_size = 2 * s->blocksize;
- }
- else
- {
- for (i = 0; i < s->channels; i++)
- {
- switch(s->order)
- {
- case INDEPENDENT:
- for (j = 0; j < s->blocksize; j++)
- samples[(s->blocksize*i)+j] = s->decoded[i][j];
- break;
- case LEFT_SIDE:
- case RIGHT_SIDE:
- if (i == 0)
- for (j = 0; j < s->blocksize; j++)
- samples[(s->blocksize*i)+j] = s->decoded[0][j];
- else
- for (j = 0; j < s->blocksize; j++)
- samples[(s->blocksize*i)+j] = s->decoded[0][j] - s->decoded[i][j];
- break;
-// case MID_SIDE:
-// av_log(s->avctx, AV_LOG_DEBUG, "mid-side unsupported\n");
- }
- *data_size += s->blocksize;
- }
- }
-#else
-#define DECORRELATE(left, right)\
- assert(s->channels == 2);\
- for (i = 0; i < s->blocksize; i++)\
- {\
- int a= s->decoded[0][i];\
- int b= s->decoded[1][i];\
- *samples++ = ((left) << (24 - s->bps)) >> 8;\
- *samples++ = ((right) << (24 - s->bps)) >> 8;\
- }\
- break;
-
- switch(s->decorrelation)
- {
- case INDEPENDENT:
- for (j = 0; j < s->blocksize; j++)
- {
- for (i = 0; i < s->channels; i++)
- *samples++ = (s->decoded[i][j] << (24 - s->bps)) >> 8;
- }
- break;
- case LEFT_SIDE:
- DECORRELATE(a,a-b)
- case RIGHT_SIDE:
- DECORRELATE(a+b,b)
- case MID_SIDE:
- DECORRELATE( (a-=b>>1) + b, a)
- }
-#endif
-
- *data_size = (int8_t *)samples - (int8_t *)data;
-// av_log(s->avctx, AV_LOG_DEBUG, "data size: %d\n", *data_size);
-
-// s->last_blocksize = s->blocksize;
-end:
- i= (get_bits_count(&s->gb)+7)/8;;
- if(i > buf_size){
- av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
- s->bitstream_size=0;
- s->bitstream_index=0;
- return -1;
- }
-
- if(s->bitstream_size){
- s->bitstream_index += i;
- s->bitstream_size -= i;
- return input_buf_size;
- }else
- return i;