libavcodec/flacdec.c
4f52c312
 /*
  * FLAC (Free Lossless Audio Codec) decoder
  * Copyright (c) 2003 Alex Beregszaszi
  *
b78e7197
  * This file is part of FFmpeg.
  *
  * FFmpeg is free software; you can redistribute it and/or
4f52c312
  * modify it under the terms of the GNU Lesser General Public
  * License as published by the Free Software Foundation; either
b78e7197
  * version 2.1 of the License, or (at your option) any later version.
4f52c312
  *
b78e7197
  * FFmpeg is distributed in the hope that it will be useful,
4f52c312
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
b78e7197
  * License along with FFmpeg; if not, write to the Free Software
5509bffa
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
4f52c312
  */
 
 /**
ba87f080
  * @file
4f52c312
  * FLAC (Free Lossless Audio Codec) decoder
  * @author Alex Beregszaszi
ad4cd0c2
  * @see http://flac.sourceforge.net/
9eda2f94
  *
  * This decoder can be used in 1 of 2 ways: Either raw FLAC data can be fed
  * through, starting from the initial 'fLaC' signature; or by passing the
  * 34-byte streaminfo structure through avctx->extradata[_size] followed
  * by data starting with the 0xFFF8 marker.
4f52c312
  */
115329f1
 
ac2570a8
 #include <limits.h>
115329f1
 
4d38b838
 #include "libavutil/avassert.h"
a903f8f0
 #include "libavutil/channel_layout.h"
245976da
 #include "libavutil/crc.h"
4f52c312
 #include "avcodec.h"
5b37e2fc
 #include "internal.h"
9106a698
 #include "get_bits.h"
a8ec12bc
 #include "bytestream.h"
4f52c312
 #include "golomb.h"
9d48410f
 #include "flac.h"
d4df4e50
 #include "flacdata.h"
4a852834
 #include "flacdsp.h"
4f52c312
 
 typedef struct FLACContext {
9d48410f
     FLACSTREAMINFO
 
64cb3765
     AVCodecContext *avctx;                  ///< parent AVCodecContext
     GetBitContext gb;                       ///< GetBitContext initialized to start at the current frame
4f52c312
 
64cb3765
     int blocksize;                          ///< number of samples in the current frame
7f3a6a05
     int sample_shift;                       ///< shift required to make output samples 16-bit or 32-bit
2a346725
     int ch_mode;                            ///< channel decorrelation type in the current frame
7d030358
     int got_streaminfo;                     ///< indicates if the STREAMINFO has been read
4f52c312
 
07d16e2e
     int32_t *decoded[FLAC_MAX_CHANNELS];    ///< decoded samples
268f8ba1
     uint8_t *decoded_buffer;
     unsigned int decoded_buffer_size;
4a852834
 
     FLACDSPContext dsp;
4f52c312
 } FLACContext;
 
268f8ba1
 static int allocate_buffers(FLACContext *s);
59c6178a
 
87466f81
 static void flac_set_bps(FLACContext *s)
 {
784514a4
     enum AVSampleFormat req = s->avctx->request_sample_fmt;
     int need32 = s->bps > 16;
     int want32 = av_get_bytes_per_sample(req) > 2;
     int planar = av_sample_fmt_is_planar(req);
 
     if (need32 || want32) {
         if (planar)
             s->avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
         else
             s->avctx->sample_fmt = AV_SAMPLE_FMT_S32;
87466f81
         s->sample_shift = 32 - s->bps;
     } else {
784514a4
         if (planar)
             s->avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
         else
             s->avctx->sample_fmt = AV_SAMPLE_FMT_S16;
87466f81
         s->sample_shift = 16 - s->bps;
     }
 }
 
1bec121f
 static av_cold int flac_decode_init(AVCodecContext *avctx)
4f52c312
 {
59c6178a
     enum FLACExtradataFormat format;
     uint8_t *streaminfo;
268f8ba1
     int ret;
9eda2f94
     FLACContext *s = avctx->priv_data;
     s->avctx = avctx;
 
59c6178a
     /* for now, the raw FLAC header is allowed to be passed to the decoder as
        frame data instead of extradata. */
     if (!avctx->extradata)
         return 0;
 
d9cca9fc
     if (!avpriv_flac_is_extradata_valid(avctx, &format, &streaminfo))
59c6178a
         return -1;
 
26adc8d0
     /* initialize based on the demuxer-supplied streamdata header */
d9cca9fc
     avpriv_flac_parse_streaminfo(avctx, (FLACStreaminfo *)s, streaminfo);
268f8ba1
     ret = allocate_buffers(s);
     if (ret < 0)
         return ret;
87466f81
     flac_set_bps(s);
784514a4
     ff_flacdsp_init(&s->dsp, avctx->sample_fmt, s->bps);
7d030358
     s->got_streaminfo = 1;
9eda2f94
 
4f52c312
     return 0;
 }
 
9482171b
 static void dump_headers(AVCodecContext *avctx, FLACStreaminfo *s)
4f52c312
 {
95db6659
     av_log(avctx, AV_LOG_DEBUG, "  Max Blocksize: %d\n", s->max_blocksize);
4bc07e78
     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);
4f52c312
 }
 
268f8ba1
 static int allocate_buffers(FLACContext *s)
1bec121f
 {
268f8ba1
     int buf_size;
4f52c312
 
4d38b838
     av_assert0(s->max_blocksize);
ac2570a8
 
268f8ba1
     buf_size = av_samples_get_buffer_size(NULL, s->channels, s->max_blocksize,
                                           AV_SAMPLE_FMT_S32P, 0);
     if (buf_size < 0)
         return buf_size;
 
     av_fast_malloc(&s->decoded_buffer, &s->decoded_buffer_size, buf_size);
     if (!s->decoded_buffer)
         return AVERROR(ENOMEM);
 
     return av_samples_fill_arrays((uint8_t **)s->decoded, NULL,
                                   s->decoded_buffer, s->channels,
                                   s->max_blocksize, AV_SAMPLE_FMT_S32P, 0);
ac2570a8
 }
 
17c90b9d
 /**
a8ec12bc
  * Parse the STREAMINFO from an inline header.
  * @param s the flac decoding context
  * @param buf input buffer, starting with the "fLaC" marker
  * @param buf_size buffer size
55a72738
  * @return non-zero if metadata is invalid
17c90b9d
  */
a8ec12bc
 static int parse_streaminfo(FLACContext *s, const uint8_t *buf, int buf_size)
17c90b9d
 {
268f8ba1
     int metadata_type, metadata_size, ret;
17c90b9d
 
a8ec12bc
     if (buf_size < FLAC_STREAMINFO_SIZE+8) {
         /* need more data */
         return 0;
     }
d9cca9fc
     avpriv_flac_parse_block_header(&buf[4], NULL, &metadata_type, &metadata_size);
a8ec12bc
     if (metadata_type != FLAC_METADATA_TYPE_STREAMINFO ||
         metadata_size != FLAC_STREAMINFO_SIZE) {
         return AVERROR_INVALIDDATA;
     }
d9cca9fc
     avpriv_flac_parse_streaminfo(s->avctx, (FLACStreaminfo *)s, &buf[8]);
268f8ba1
     ret = allocate_buffers(s);
     if (ret < 0)
         return ret;
4a852834
     flac_set_bps(s);
784514a4
     ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt, s->bps);
a8ec12bc
     s->got_streaminfo = 1;
17c90b9d
 
a8ec12bc
     return 0;
 }
17c90b9d
 
a8ec12bc
 /**
  * Determine the size of an inline header.
  * @param buf input buffer, starting with the "fLaC" marker
  * @param buf_size buffer size
  * @return number of bytes in the header, or 0 if more data is needed
  */
 static int get_metadata_size(const uint8_t *buf, int buf_size)
 {
     int metadata_last, metadata_size;
     const uint8_t *buf_end = buf + buf_size;
e0168e3b
 
a8ec12bc
     buf += 4;
     do {
4c5e7b27
         if (buf_end - buf < 4)
             return 0;
d9cca9fc
         avpriv_flac_parse_block_header(buf, &metadata_last, NULL, &metadata_size);
5b63d33d
         buf += 4;
4c5e7b27
         if (buf_end - buf < metadata_size) {
a8ec12bc
             /* need more data in order to read the complete header */
             return 0;
6a85fb34
         }
a8ec12bc
         buf += metadata_size;
6a85fb34
     } while (!metadata_last);
17c90b9d
 
a8ec12bc
     return buf_size - (buf_end - buf);
4f52c312
 }
 
44c56a16
 static int decode_residuals(FLACContext *s, int32_t *decoded, int pred_order)
4f52c312
 {
     int i, tmp, partition, method_type, rice_order;
ea98507d
     int rice_bits, rice_esc;
44c56a16
     int samples;
4f52c312
 
     method_type = get_bits(&s->gb, 2);
1bec121f
     if (method_type > 1) {
9f3d3ecf
         av_log(s->avctx, AV_LOG_ERROR, "illegal residual coding method %d\n",
                method_type);
4f52c312
         return -1;
ac2570a8
     }
115329f1
 
4f52c312
     rice_order = get_bits(&s->gb, 4);
 
ac2570a8
     samples= s->blocksize >> rice_order;
5484dad7
     if (pred_order > samples) {
9f3d3ecf
         av_log(s->avctx, AV_LOG_ERROR, "invalid predictor order: %i > %i\n",
                pred_order, samples);
5484dad7
         return -1;
     }
4f52c312
 
ea98507d
     rice_bits = 4 + method_type;
     rice_esc  = (1 << rice_bits) - 1;
 
44c56a16
     decoded += pred_order;
ac2570a8
     i= pred_order;
1bec121f
     for (partition = 0; partition < (1 << rice_order); partition++) {
ea98507d
         tmp = get_bits(&s->gb, rice_bits);
         if (tmp == rice_esc) {
4f52c312
             tmp = get_bits(&s->gb, 5);
44c56a16
             for (; i < samples; i++)
                 *decoded++ = get_sbits_long(&s->gb, tmp);
1bec121f
         } else {
44c56a16
             for (; i < samples; i++) {
                 *decoded++ = get_sr_golomb_flac(&s->gb, tmp, INT_MAX, 0);
ac2570a8
             }
4f52c312
         }
ac2570a8
         i= 0;
4f52c312
     }
 
     return 0;
115329f1
 }
4f52c312
 
44c56a16
 static int decode_subframe_fixed(FLACContext *s, int32_t *decoded,
                                  int pred_order, int bps)
4f52c312
 {
08965b22
     const int blocksize = s->blocksize;
fc0d069f
     int av_uninit(a), av_uninit(b), av_uninit(c), av_uninit(d), i;
115329f1
 
4f52c312
     /* warm up samples */
1bec121f
     for (i = 0; i < pred_order; i++) {
0da301e1
         decoded[i] = get_sbits_long(&s->gb, bps);
4f52c312
     }
115329f1
 
44c56a16
     if (decode_residuals(s, decoded, pred_order) < 0)
4f52c312
         return -1;
 
1bec121f
     if (pred_order > 0)
1f4fa6a4
         a = decoded[pred_order-1];
1bec121f
     if (pred_order > 1)
1f4fa6a4
         b = a - decoded[pred_order-2];
1bec121f
     if (pred_order > 2)
1f4fa6a4
         c = b - decoded[pred_order-2] + decoded[pred_order-3];
1bec121f
     if (pred_order > 3)
1f4fa6a4
         d = c - decoded[pred_order-2] + 2*decoded[pred_order-3] - decoded[pred_order-4];
08965b22
 
1bec121f
     switch (pred_order) {
     case 0:
         break;
     case 1:
         for (i = pred_order; i < blocksize; i++)
             decoded[i] = a += decoded[i];
         break;
     case 2:
         for (i = pred_order; i < blocksize; i++)
             decoded[i] = a += b += decoded[i];
         break;
     case 3:
         for (i = pred_order; i < blocksize; i++)
             decoded[i] = a += b += c += decoded[i];
         break;
     case 4:
         for (i = pred_order; i < blocksize; i++)
             decoded[i] = a += b += c += d += decoded[i];
         break;
     default:
         av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order);
         return -1;
4f52c312
     }
ac2570a8
 
4f52c312
     return 0;
 }
 
44c56a16
 static int decode_subframe_lpc(FLACContext *s, int32_t *decoded, int pred_order,
0da301e1
                                int bps)
4f52c312
 {
25accf93
     int i;
4f52c312
     int coeff_prec, qlevel;
8313e179
     int coeffs[32];
115329f1
 
4f52c312
     /* warm up samples */
1bec121f
     for (i = 0; i < pred_order; i++) {
0da301e1
         decoded[i] = get_sbits_long(&s->gb, bps);
4f52c312
     }
115329f1
 
4f52c312
     coeff_prec = get_bits(&s->gb, 4) + 1;
1bec121f
     if (coeff_prec == 16) {
5305f40b
         av_log(s->avctx, AV_LOG_ERROR, "invalid coeff precision\n");
4f52c312
         return -1;
     }
ac2570a8
     qlevel = get_sbits(&s->gb, 5);
1bec121f
     if (qlevel < 0) {
9f3d3ecf
         av_log(s->avctx, AV_LOG_ERROR, "qlevel %d not supported, maybe buggy stream\n",
                qlevel);
9d656110
         return -1;
     }
 
1bec121f
     for (i = 0; i < pred_order; i++) {
bf1cf4d5
         coeffs[pred_order - i - 1] = get_sbits(&s->gb, coeff_prec);
4f52c312
     }
115329f1
 
44c56a16
     if (decode_residuals(s, decoded, pred_order) < 0)
4f52c312
         return -1;
 
25accf93
     s->dsp.lpc(decoded, coeffs, pred_order, qlevel, s->blocksize);
115329f1
 
4f52c312
     return 0;
 }
 
 static inline int decode_subframe(FLACContext *s, int channel)
 {
44c56a16
     int32_t *decoded = s->decoded[channel];
4f52c312
     int type, wasted = 0;
0da301e1
     int bps = s->bps;
4f52c312
     int i, tmp;
115329f1
 
1bec121f
     if (channel == 0) {
2a346725
         if (s->ch_mode == FLAC_CHMODE_RIGHT_SIDE)
0da301e1
             bps++;
1bec121f
     } else {
2a346725
         if (s->ch_mode == FLAC_CHMODE_LEFT_SIDE || s->ch_mode == FLAC_CHMODE_MID_SIDE)
0da301e1
             bps++;
ac2570a8
     }
 
1bec121f
     if (get_bits1(&s->gb)) {
1cef211d
         av_log(s->avctx, AV_LOG_ERROR, "invalid subframe padding\n");
4f52c312
         return -1;
     }
     type = get_bits(&s->gb, 6);
c5706efd
 
1bec121f
     if (get_bits1(&s->gb)) {
52e4018b
         int left = get_bits_left(&s->gb);
4f52c312
         wasted = 1;
52e4018b
         if ( left < 0 ||
0da301e1
             (left < bps && !show_bits_long(&s->gb, left)) ||
                            !show_bits_long(&s->gb, bps)) {
52e4018b
             av_log(s->avctx, AV_LOG_ERROR,
                    "Invalid number of wasted bits > available bits (%d) - left=%d\n",
0da301e1
                    bps, left);
52e4018b
             return AVERROR_INVALIDDATA;
         }
4f52c312
         while (!get_bits1(&s->gb))
             wasted++;
0da301e1
         bps -= wasted;
4f52c312
     }
0da301e1
     if (bps > 32) {
8f4c414d
         av_log_missing_feature(s->avctx, "Decorrelated bit depth > 32", 0);
717addec
         return AVERROR_PATCHWELCOME;
a3d2379b
     }
c5706efd
 
ac2570a8
 //FIXME use av_log2 for types
1bec121f
     if (type == 0) {
0da301e1
         tmp = get_sbits_long(&s->gb, bps);
4f52c312
         for (i = 0; i < s->blocksize; i++)
44c56a16
             decoded[i] = tmp;
1bec121f
     } else if (type == 1) {
4f52c312
         for (i = 0; i < s->blocksize; i++)
44c56a16
             decoded[i] = get_sbits_long(&s->gb, bps);
1bec121f
     } else if ((type >= 8) && (type <= 12)) {
44c56a16
         if (decode_subframe_fixed(s, decoded, type & ~0x8, bps) < 0)
4f52c312
             return -1;
1bec121f
     } else if (type >= 32) {
44c56a16
         if (decode_subframe_lpc(s, decoded, (type & ~0x20)+1, bps) < 0)
4f52c312
             return -1;
1bec121f
     } else {
1cef211d
         av_log(s->avctx, AV_LOG_ERROR, "invalid coding type\n");
4f52c312
         return -1;
     }
115329f1
 
1bec121f
     if (wasted) {
4f52c312
         int i;
         for (i = 0; i < s->blocksize; i++)
44c56a16
             decoded[i] <<= wasted;
4f52c312
     }
 
     return 0;
 }
 
cd98a030
 static int decode_frame(FLACContext *s)
 {
90fcac0e
     int i, ret;
cd98a030
     GetBitContext *gb = &s->gb;
     FLACFrameInfo fi;
 
3c795698
     if (ff_flac_decode_frame_header(s->avctx, gb, &fi, 0)) {
cd98a030
         av_log(s->avctx, AV_LOG_ERROR, "invalid frame header\n");
9d656110
         return -1;
     }
115329f1
 
90fcac0e
     if (s->channels && fi.channels != s->channels && s->got_streaminfo) {
         s->channels = s->avctx->channels = fi.channels;
         ff_flac_set_channel_layout(s->avctx);
         ret = allocate_buffers(s);
         if (ret < 0)
             return ret;
fbc4d9c9
     }
2e0559b7
     s->channels = s->avctx->channels = fi.channels;
08797c59
     if (!s->avctx->channel_layout)
aef51507
         ff_flac_set_channel_layout(s->avctx);
cd98a030
     s->ch_mode = fi.ch_mode;
fbc4d9c9
 
2e0559b7
     if (!s->bps && !fi.bps) {
         av_log(s->avctx, AV_LOG_ERROR, "bps not found in STREAMINFO or frame header\n");
         return -1;
     }
     if (!fi.bps) {
         fi.bps = s->bps;
     } else if (s->bps && fi.bps != s->bps) {
fbc4d9c9
         av_log(s->avctx, AV_LOG_ERROR, "switching bps mid-stream is not "
                                        "supported\n");
         return -1;
     }
2e0559b7
 
784514a4
     if (!s->bps) {
         s->bps = s->avctx->bits_per_raw_sample = fi.bps;
         flac_set_bps(s);
     }
fbc4d9c9
 
2e0559b7
     if (!s->max_blocksize)
         s->max_blocksize = FLAC_MAX_BLOCKSIZE;
cd98a030
     if (fi.blocksize > s->max_blocksize) {
         av_log(s->avctx, AV_LOG_ERROR, "blocksize %d > %d\n", fi.blocksize,
fbc4d9c9
                s->max_blocksize);
         return -1;
     }
cd98a030
     s->blocksize = fi.blocksize;
fbc4d9c9
 
2e0559b7
     if (!s->samplerate && !fi.samplerate) {
         av_log(s->avctx, AV_LOG_ERROR, "sample rate not found in STREAMINFO"
                                         " or frame header\n");
         return -1;
     }
99d86863
     if (fi.samplerate == 0)
cd98a030
         fi.samplerate = s->samplerate;
     s->samplerate = s->avctx->sample_rate = fi.samplerate;
4f52c312
 
2e0559b7
     if (!s->got_streaminfo) {
90fcac0e
         ret = allocate_buffers(s);
268f8ba1
         if (ret < 0)
             return ret;
784514a4
         ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt, s->bps);
2e0559b7
         s->got_streaminfo = 1;
         dump_headers(s->avctx, (FLACStreaminfo *)s);
     }
 
9482171b
 //    dump_headers(s->avctx, (FLACStreaminfo *)s);
4f52c312
 
     /* subframes */
1bec121f
     for (i = 0; i < s->channels; i++) {
4f52c312
         if (decode_subframe(s, i) < 0)
             return -1;
     }
115329f1
 
7e00bd84
     align_get_bits(gb);
4f52c312
 
     /* frame footer */
7e00bd84
     skip_bits(gb, 16); /* data crc */
4f52c312
 
     return 0;
 }
 
0eea2129
 static int flac_decode_frame(AVCodecContext *avctx, void *data,
                              int *got_frame_ptr, AVPacket *avpkt)
4f52c312
 {
b8e9c99e
     AVFrame *frame     = data;
7a00bbad
     const uint8_t *buf = avpkt->data;
     int buf_size = avpkt->size;
4f52c312
     FLACContext *s = avctx->priv_data;
4a852834
     int bytes_read = 0;
0eea2129
     int ret;
ac66834c
 
0eea2129
     *got_frame_ptr = 0;
4f52c312
 
1bec121f
     if (s->max_framesize == 0) {
2e0559b7
         s->max_framesize =
             ff_flac_get_max_frame_size(s->max_blocksize ? s->max_blocksize : FLAC_MAX_BLOCKSIZE,
                                        FLAC_MAX_CHANNELS, 32);
ac2570a8
     }
4f52c312
 
0e9b9a67
     if (buf_size > 5 && !memcmp(buf, "\177FLAC", 5)) {
         av_log(s->avctx, AV_LOG_DEBUG, "skiping flac header packet 1\n");
         return buf_size;
     }
 
     if (buf_size > 0 && (*buf & 0x7F) == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
         av_log(s->avctx, AV_LOG_DEBUG, "skiping vorbis comment\n");
         return buf_size;
     }
 
5ef4fa87
     /* check that there is at least the smallest decodable amount of data.
8d1e885f
        this amount corresponds to the smallest valid FLAC frame possible.
        FF F8 69 02 00 00 9A 00 00 34 46 */
a4151444
     if (buf_size < FLAC_MIN_FRAME_SIZE)
60a68493
         return buf_size;
5ef4fa87
 
55a72738
     /* check for inline header */
a8ec12bc
     if (AV_RB32(buf) == MKBETAG('f','L','a','C')) {
         if (!s->got_streaminfo && parse_streaminfo(s, buf, buf_size)) {
55a72738
             av_log(s->avctx, AV_LOG_ERROR, "invalid header\n");
             return -1;
         }
60a68493
         return get_metadata_size(buf, buf_size);
13de8a08
     }
fd6fd470
 
     /* decode frame */
     init_get_bits(&s->gb, buf, buf_size*8);
02b26d2d
     if (decode_frame(s) < 0) {
13de8a08
         av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n");
         return -1;
     }
1d5b35cc
     bytes_read = get_bits_count(&s->gb)/8;
 
     if ((s->avctx->err_recognition & AV_EF_CRCCHECK) &&
         av_crc(av_crc_get_table(AV_CRC_16_ANSI),
                0, buf, bytes_read)) {
         av_log(s->avctx, AV_LOG_ERROR, "CRC error at PTS %"PRId64"\n", avpkt->pts);
         if (s->avctx->err_recognition & AV_EF_EXPLODE)
             return AVERROR_INVALIDDATA;
     }
ac2570a8
 
0eea2129
     /* get output buffer */
b8e9c99e
     frame->nb_samples = s->blocksize;
     if ((ret = ff_get_buffer(avctx, frame)) < 0) {
0eea2129
         av_log(avctx, AV_LOG_ERROR, "get_buffer() failed\n");
         return ret;
02b26d2d
     }
4a852834
 
b8e9c99e
     s->dsp.decorrelate[s->ch_mode](frame->data, s->decoded, s->channels,
4a852834
                                    s->blocksize, s->sample_shift);
4f52c312
 
c5199729
     if (bytes_read > buf_size) {
         av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", bytes_read - buf_size);
ac2570a8
         return -1;
     }
60a68493
     if (bytes_read < buf_size) {
         av_log(s->avctx, AV_LOG_DEBUG, "underread: %d orig size: %d\n",
                buf_size - bytes_read, buf_size);
     }
4f52c312
 
b8e9c99e
     *got_frame_ptr = 1;
0eea2129
 
60a68493
     return bytes_read;
4f52c312
 }
 
98a6fff9
 static av_cold int flac_decode_close(AVCodecContext *avctx)
4f52c312
 {
     FLACContext *s = avctx->priv_data;
115329f1
 
268f8ba1
     av_freep(&s->decoded_buffer);
115329f1
 
4f52c312
     return 0;
 }
 
e7e2df27
 AVCodec ff_flac_decoder = {
ec6402b7
     .name           = "flac",
     .type           = AVMEDIA_TYPE_AUDIO,
36ef5369
     .id             = AV_CODEC_ID_FLAC,
ec6402b7
     .priv_data_size = sizeof(FLACContext),
     .init           = flac_decode_init,
     .close          = flac_decode_close,
     .decode         = flac_decode_frame,
0eea2129
     .capabilities   = CODEC_CAP_DR1,
00c3b67b
     .long_name      = NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"),
784514a4
     .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16,
                                                       AV_SAMPLE_FMT_S16P,
                                                       AV_SAMPLE_FMT_S32,
                                                       AV_SAMPLE_FMT_S32P,
5864fe62
                                                       AV_SAMPLE_FMT_NONE },
4f52c312
 };