-static void metadata_streaminfo(AVCodecContext *avctx, FLACContext *s, const uint8_t *buffer);
-static void allocate_buffers(FLACContext *s);
-static int metadata_parse(FLACContext *s);
-
-static av_cold int flac_decode_init(AVCodecContext * avctx)
-{
- FLACContext *s = avctx->priv_data;
- s->avctx = avctx;
-
- if (avctx->extradata_size > 4) {
- /* initialize based on the demuxer-supplied streamdata header */
- init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size*8);
- if (avctx->extradata_size == FLAC_STREAMINFO_SIZE) {
- metadata_streaminfo(avctx, s, avctx->extradata);
- allocate_buffers(s);
- } else {
- metadata_parse(s);
- }
- }
-
- return 0;
-}
-
-static void dump_headers(AVCodecContext *avctx, FLACContext *s)
-{
- av_log(avctx, AV_LOG_DEBUG, " Blocksize: %d .. %d (%d)\n", s->min_blocksize, s->max_blocksize, s->blocksize);
- av_log(avctx, AV_LOG_DEBUG, " Max Framesize: %d\n", s->max_framesize);
- av_log(avctx, AV_LOG_DEBUG, " Samplerate: %d\n", s->samplerate);
- av_log(avctx, AV_LOG_DEBUG, " Channels: %d\n", s->channels);
- av_log(avctx, AV_LOG_DEBUG, " Bits: %d\n", s->bps);
-}
-
-static void allocate_buffers(FLACContext *s){
- int i;
-
- assert(s->max_blocksize);
-
- if(s->max_framesize == 0 && s->max_blocksize){
- s->max_framesize= (s->channels * s->bps * s->max_blocksize + 7)/ 8; //FIXME header overhead
- }
-
- for (i = 0; i < s->channels; i++)
- {
- s->decoded[i] = av_realloc(s->decoded[i], sizeof(int32_t)*s->max_blocksize);
- }
-
- s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);
-}
-
-static void metadata_streaminfo(AVCodecContext *avctx, FLACContext *s,
- const uint8_t *buffer)
-{
- GetBitContext gb;
- init_get_bits(&gb, buffer, FLAC_STREAMINFO_SIZE*8);
-
- /* mandatory streaminfo */
- s->min_blocksize = get_bits(&gb, 16);
- s->max_blocksize = get_bits(&gb, 16);
-
- skip_bits(&gb, 24); /* skip min frame size */
- s->max_framesize = get_bits_long(&gb, 24);
-
- s->samplerate = get_bits_long(&gb, 20);
- s->channels = get_bits(&gb, 3) + 1;
- s->bps = get_bits(&gb, 5) + 1;
-
- avctx->channels = s->channels;
- avctx->sample_rate = s->samplerate;
-
- skip_bits(&gb, 36); /* total num of samples */
-
- skip_bits(&gb, 64); /* md5 sum */
- skip_bits(&gb, 64); /* md5 sum */
-
- dump_headers(avctx, s);
-}
-
-/**
- * Parse a list of metadata blocks. This list of blocks must begin with
- * the fLaC marker.
- * @param s the flac decoding context containing the gb bit reader used to
- * parse metadata
- * @return 1 if some metadata was read, 0 if no fLaC marker was found
- */
-static int metadata_parse(FLACContext *s)
-{
- int i, metadata_last, metadata_type, metadata_size, streaminfo_updated=0;
-
- if (show_bits_long(&s->gb, 32) == MKBETAG('f','L','a','C')) {
- skip_bits(&s->gb, 32);
-
- av_log(s->avctx, AV_LOG_DEBUG, "STREAM HEADER\n");
- do {
- metadata_last = get_bits1(&s->gb);
- metadata_type = get_bits(&s->gb, 7);
- metadata_size = get_bits_long(&s->gb, 24);
-
- av_log(s->avctx, AV_LOG_DEBUG,
- " metadata block: flag = %d, type = %d, size = %d\n",
- metadata_last, metadata_type, metadata_size);
- if (metadata_size) {
- switch (metadata_type) {
- case METADATA_TYPE_STREAMINFO:
- metadata_streaminfo(s->avctx, s, s->gb.buffer+get_bits_count(&s->gb)/8);
- streaminfo_updated = 1;
-
- default:
- for (i=0; i<metadata_size; i++)
- skip_bits(&s->gb, 8);
- }
- }
- } while (!metadata_last);
-
- if (streaminfo_updated)
- allocate_buffers(s);
- return 1;
- }
- return 0;
-}
-
-static int decode_residuals(FLACContext *s, int channel, int pred_order)
-{
- int i, tmp, partition, method_type, rice_order;
- int sample = 0, samples;
-
- method_type = get_bits(&s->gb, 2);
- if (method_type > 1){
- av_log(s->avctx, AV_LOG_DEBUG, "illegal residual coding method %d\n", method_type);
- return -1;
- }
-
- rice_order = get_bits(&s->gb, 4);
-
- samples= s->blocksize >> rice_order;
- if (pred_order > samples) {
- av_log(s->avctx, AV_LOG_ERROR, "invalid predictor order: %i > %i\n", pred_order, samples);
- return -1;
- }
-
- sample=
- i= pred_order;
- for (partition = 0; partition < (1 << rice_order); partition++)
- {
- tmp = get_bits(&s->gb, method_type == 0 ? 4 : 5);
- if (tmp == (method_type == 0 ? 15 : 31))
- {
- av_log(s->avctx, AV_LOG_DEBUG, "fixed len partition\n");
- tmp = get_bits(&s->gb, 5);
- for (; i < samples; i++, sample++)
- s->decoded[channel][sample] = get_sbits(&s->gb, tmp);
- }
- else
- {
-// av_log(s->avctx, AV_LOG_DEBUG, "rice coded partition k=%d\n", tmp);
- for (; i < samples; i++, sample++){
- s->decoded[channel][sample] = get_sr_golomb_flac(&s->gb, tmp, INT_MAX, 0);
- }
- }
- i= 0;
- }
-
-// av_log(s->avctx, AV_LOG_DEBUG, "partitions: %d, samples: %d\n", 1 << rice_order, sample);
-
- return 0;
-}
-
-static int decode_subframe_fixed(FLACContext *s, int channel, int pred_order)