]> git.sesse.net Git - ffmpeg/blob - libavcodec/flacdec.c
dnxhddec: better support for 4:4:4
[ffmpeg] / libavcodec / flacdec.c
1 /*
2  * FLAC (Free Lossless Audio Codec) decoder
3  * Copyright (c) 2003 Alex Beregszaszi
4  *
5  * This file is part of FFmpeg.
6  *
7  * FFmpeg is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * FFmpeg is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with FFmpeg; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20  */
21
22 /**
23  * @file
24  * FLAC (Free Lossless Audio Codec) decoder
25  * @author Alex Beregszaszi
26  * @see http://flac.sourceforge.net/
27  *
28  * This decoder can be used in 1 of 2 ways: Either raw FLAC data can be fed
29  * through, starting from the initial 'fLaC' signature; or by passing the
30  * 34-byte streaminfo structure through avctx->extradata[_size] followed
31  * by data starting with the 0xFFF8 marker.
32  */
33
34 #include <limits.h>
35
36 #include "libavutil/avassert.h"
37 #include "libavutil/crc.h"
38 #include "libavutil/opt.h"
39 #include "avcodec.h"
40 #include "internal.h"
41 #include "get_bits.h"
42 #include "bytestream.h"
43 #include "golomb.h"
44 #include "flac.h"
45 #include "flacdata.h"
46 #include "flacdsp.h"
47 #include "thread.h"
48 #include "unary.h"
49
50
51 typedef struct FLACContext {
52     AVClass *class;
53     struct FLACStreaminfo flac_stream_info;
54
55     AVCodecContext *avctx;                  ///< parent AVCodecContext
56     GetBitContext gb;                       ///< GetBitContext initialized to start at the current frame
57
58     int blocksize;                          ///< number of samples in the current frame
59     int sample_shift;                       ///< shift required to make output samples 16-bit or 32-bit
60     int ch_mode;                            ///< channel decorrelation type in the current frame
61     int got_streaminfo;                     ///< indicates if the STREAMINFO has been read
62
63     int32_t *decoded[FLAC_MAX_CHANNELS];    ///< decoded samples
64     uint8_t *decoded_buffer;
65     unsigned int decoded_buffer_size;
66     int buggy_lpc;                          ///< use workaround for old lavc encoded files
67
68     FLACDSPContext dsp;
69 } FLACContext;
70
71 static int allocate_buffers(FLACContext *s);
72
73 static void flac_set_bps(FLACContext *s)
74 {
75     enum AVSampleFormat req = s->avctx->request_sample_fmt;
76     int need32 = s->flac_stream_info.bps > 16;
77     int want32 = av_get_bytes_per_sample(req) > 2;
78     int planar = av_sample_fmt_is_planar(req);
79
80     if (need32 || want32) {
81         if (planar)
82             s->avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
83         else
84             s->avctx->sample_fmt = AV_SAMPLE_FMT_S32;
85         s->sample_shift = 32 - s->flac_stream_info.bps;
86     } else {
87         if (planar)
88             s->avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
89         else
90             s->avctx->sample_fmt = AV_SAMPLE_FMT_S16;
91         s->sample_shift = 16 - s->flac_stream_info.bps;
92     }
93 }
94
95 static av_cold int flac_decode_init(AVCodecContext *avctx)
96 {
97     enum FLACExtradataFormat format;
98     uint8_t *streaminfo;
99     int ret;
100     FLACContext *s = avctx->priv_data;
101     s->avctx = avctx;
102
103     /* for now, the raw FLAC header is allowed to be passed to the decoder as
104        frame data instead of extradata. */
105     if (!avctx->extradata)
106         return 0;
107
108     if (!ff_flac_is_extradata_valid(avctx, &format, &streaminfo))
109         return AVERROR_INVALIDDATA;
110
111     /* initialize based on the demuxer-supplied streamdata header */
112     ff_flac_parse_streaminfo(avctx, &s->flac_stream_info, streaminfo);
113     ret = allocate_buffers(s);
114     if (ret < 0)
115         return ret;
116     flac_set_bps(s);
117     ff_flacdsp_init(&s->dsp, avctx->sample_fmt,
118                     s->flac_stream_info.channels, s->flac_stream_info.bps);
119     s->got_streaminfo = 1;
120
121     return 0;
122 }
123
124 static void dump_headers(AVCodecContext *avctx, FLACStreaminfo *s)
125 {
126     av_log(avctx, AV_LOG_DEBUG, "  Max Blocksize: %d\n", s->max_blocksize);
127     av_log(avctx, AV_LOG_DEBUG, "  Max Framesize: %d\n", s->max_framesize);
128     av_log(avctx, AV_LOG_DEBUG, "  Samplerate: %d\n", s->samplerate);
129     av_log(avctx, AV_LOG_DEBUG, "  Channels: %d\n", s->channels);
130     av_log(avctx, AV_LOG_DEBUG, "  Bits: %d\n", s->bps);
131 }
132
133 static int allocate_buffers(FLACContext *s)
134 {
135     int buf_size;
136     int ret;
137
138     av_assert0(s->flac_stream_info.max_blocksize);
139
140     buf_size = av_samples_get_buffer_size(NULL, s->flac_stream_info.channels,
141                                           s->flac_stream_info.max_blocksize,
142                                           AV_SAMPLE_FMT_S32P, 0);
143     if (buf_size < 0)
144         return buf_size;
145
146     av_fast_malloc(&s->decoded_buffer, &s->decoded_buffer_size, buf_size);
147     if (!s->decoded_buffer)
148         return AVERROR(ENOMEM);
149
150     ret = av_samples_fill_arrays((uint8_t **)s->decoded, NULL,
151                                  s->decoded_buffer,
152                                  s->flac_stream_info.channels,
153                                  s->flac_stream_info.max_blocksize,
154                                  AV_SAMPLE_FMT_S32P, 0);
155     return ret < 0 ? ret : 0;
156 }
157
158 /**
159  * Parse the STREAMINFO from an inline header.
160  * @param s the flac decoding context
161  * @param buf input buffer, starting with the "fLaC" marker
162  * @param buf_size buffer size
163  * @return non-zero if metadata is invalid
164  */
165 static int parse_streaminfo(FLACContext *s, const uint8_t *buf, int buf_size)
166 {
167     int metadata_type, metadata_size, ret;
168
169     if (buf_size < FLAC_STREAMINFO_SIZE+8) {
170         /* need more data */
171         return 0;
172     }
173     flac_parse_block_header(&buf[4], NULL, &metadata_type, &metadata_size);
174     if (metadata_type != FLAC_METADATA_TYPE_STREAMINFO ||
175         metadata_size != FLAC_STREAMINFO_SIZE) {
176         return AVERROR_INVALIDDATA;
177     }
178     ff_flac_parse_streaminfo(s->avctx, &s->flac_stream_info, &buf[8]);
179     ret = allocate_buffers(s);
180     if (ret < 0)
181         return ret;
182     flac_set_bps(s);
183     ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt,
184                     s->flac_stream_info.channels, s->flac_stream_info.bps);
185     s->got_streaminfo = 1;
186
187     return 0;
188 }
189
190 /**
191  * Determine the size of an inline header.
192  * @param buf input buffer, starting with the "fLaC" marker
193  * @param buf_size buffer size
194  * @return number of bytes in the header, or 0 if more data is needed
195  */
196 static int get_metadata_size(const uint8_t *buf, int buf_size)
197 {
198     int metadata_last, metadata_size;
199     const uint8_t *buf_end = buf + buf_size;
200
201     buf += 4;
202     do {
203         if (buf_end - buf < 4)
204             return 0;
205         flac_parse_block_header(buf, &metadata_last, NULL, &metadata_size);
206         buf += 4;
207         if (buf_end - buf < metadata_size) {
208             /* need more data in order to read the complete header */
209             return 0;
210         }
211         buf += metadata_size;
212     } while (!metadata_last);
213
214     return buf_size - (buf_end - buf);
215 }
216
217 static int decode_residuals(FLACContext *s, int32_t *decoded, int pred_order)
218 {
219     int i, tmp, partition, method_type, rice_order;
220     int rice_bits, rice_esc;
221     int samples;
222
223     method_type = get_bits(&s->gb, 2);
224     if (method_type > 1) {
225         av_log(s->avctx, AV_LOG_ERROR, "illegal residual coding method %d\n",
226                method_type);
227         return AVERROR_INVALIDDATA;
228     }
229
230     rice_order = get_bits(&s->gb, 4);
231
232     samples= s->blocksize >> rice_order;
233     if (samples << rice_order != s->blocksize) {
234         av_log(s->avctx, AV_LOG_ERROR, "invalid rice order: %i blocksize %i\n",
235                rice_order, s->blocksize);
236         return AVERROR_INVALIDDATA;
237     }
238
239     if (pred_order > samples) {
240         av_log(s->avctx, AV_LOG_ERROR, "invalid predictor order: %i > %i\n",
241                pred_order, samples);
242         return AVERROR_INVALIDDATA;
243     }
244
245     rice_bits = 4 + method_type;
246     rice_esc  = (1 << rice_bits) - 1;
247
248     decoded += pred_order;
249     i= pred_order;
250     for (partition = 0; partition < (1 << rice_order); partition++) {
251         tmp = get_bits(&s->gb, rice_bits);
252         if (tmp == rice_esc) {
253             tmp = get_bits(&s->gb, 5);
254             for (; i < samples; i++)
255                 *decoded++ = get_sbits_long(&s->gb, tmp);
256         } else {
257             for (; i < samples; i++) {
258                 *decoded++ = get_sr_golomb_flac(&s->gb, tmp, INT_MAX, 0);
259             }
260         }
261         i= 0;
262     }
263
264     return 0;
265 }
266
267 static int decode_subframe_fixed(FLACContext *s, int32_t *decoded,
268                                  int pred_order, int bps)
269 {
270     const int blocksize = s->blocksize;
271     int av_uninit(a), av_uninit(b), av_uninit(c), av_uninit(d), i;
272     int ret;
273
274     /* warm up samples */
275     for (i = 0; i < pred_order; i++) {
276         decoded[i] = get_sbits_long(&s->gb, bps);
277     }
278
279     if ((ret = decode_residuals(s, decoded, pred_order)) < 0)
280         return ret;
281
282     if (pred_order > 0)
283         a = decoded[pred_order-1];
284     if (pred_order > 1)
285         b = a - decoded[pred_order-2];
286     if (pred_order > 2)
287         c = b - decoded[pred_order-2] + decoded[pred_order-3];
288     if (pred_order > 3)
289         d = c - decoded[pred_order-2] + 2*decoded[pred_order-3] - decoded[pred_order-4];
290
291     switch (pred_order) {
292     case 0:
293         break;
294     case 1:
295         for (i = pred_order; i < blocksize; i++)
296             decoded[i] = a += decoded[i];
297         break;
298     case 2:
299         for (i = pred_order; i < blocksize; i++)
300             decoded[i] = a += b += decoded[i];
301         break;
302     case 3:
303         for (i = pred_order; i < blocksize; i++)
304             decoded[i] = a += b += c += decoded[i];
305         break;
306     case 4:
307         for (i = pred_order; i < blocksize; i++)
308             decoded[i] = a += b += c += d += decoded[i];
309         break;
310     default:
311         av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order);
312         return AVERROR_INVALIDDATA;
313     }
314
315     return 0;
316 }
317
318 static void lpc_analyze_remodulate(int32_t *decoded, const int coeffs[32],
319                                    int order, int qlevel, int len, int bps)
320 {
321     int i, j;
322     int ebps = 1 << (bps-1);
323     unsigned sigma = 0;
324
325     for (i = order; i < len; i++)
326         sigma |= decoded[i] + ebps;
327
328     if (sigma < 2*ebps)
329         return;
330
331     for (i = len - 1; i >= order; i--) {
332         int64_t p = 0;
333         for (j = 0; j < order; j++)
334             p += coeffs[j] * (int64_t)decoded[i-order+j];
335         decoded[i] -= p >> qlevel;
336     }
337     for (i = order; i < len; i++, decoded++) {
338         int32_t p = 0;
339         for (j = 0; j < order; j++)
340             p += coeffs[j] * (uint32_t)decoded[j];
341         decoded[j] += p >> qlevel;
342     }
343 }
344
345 static int decode_subframe_lpc(FLACContext *s, int32_t *decoded, int pred_order,
346                                int bps)
347 {
348     int i, ret;
349     int coeff_prec, qlevel;
350     int coeffs[32];
351
352     /* warm up samples */
353     for (i = 0; i < pred_order; i++) {
354         decoded[i] = get_sbits_long(&s->gb, bps);
355     }
356
357     coeff_prec = get_bits(&s->gb, 4) + 1;
358     if (coeff_prec == 16) {
359         av_log(s->avctx, AV_LOG_ERROR, "invalid coeff precision\n");
360         return AVERROR_INVALIDDATA;
361     }
362     qlevel = get_sbits(&s->gb, 5);
363     if (qlevel < 0) {
364         av_log(s->avctx, AV_LOG_ERROR, "qlevel %d not supported, maybe buggy stream\n",
365                qlevel);
366         return AVERROR_INVALIDDATA;
367     }
368
369     for (i = 0; i < pred_order; i++) {
370         coeffs[pred_order - i - 1] = get_sbits(&s->gb, coeff_prec);
371     }
372
373     if ((ret = decode_residuals(s, decoded, pred_order)) < 0)
374         return ret;
375
376     if (   (    s->buggy_lpc && s->flac_stream_info.bps <= 16)
377         || (   !s->buggy_lpc && bps <= 16
378             && bps + coeff_prec + av_log2(pred_order) <= 32)) {
379         s->dsp.lpc16(decoded, coeffs, pred_order, qlevel, s->blocksize);
380     } else {
381         s->dsp.lpc32(decoded, coeffs, pred_order, qlevel, s->blocksize);
382         if (s->flac_stream_info.bps <= 16)
383             lpc_analyze_remodulate(decoded, coeffs, pred_order, qlevel, s->blocksize, bps);
384     }
385
386     return 0;
387 }
388
389 static inline int decode_subframe(FLACContext *s, int channel)
390 {
391     int32_t *decoded = s->decoded[channel];
392     int type, wasted = 0;
393     int bps = s->flac_stream_info.bps;
394     int i, tmp, ret;
395
396     if (channel == 0) {
397         if (s->ch_mode == FLAC_CHMODE_RIGHT_SIDE)
398             bps++;
399     } else {
400         if (s->ch_mode == FLAC_CHMODE_LEFT_SIDE || s->ch_mode == FLAC_CHMODE_MID_SIDE)
401             bps++;
402     }
403
404     if (get_bits1(&s->gb)) {
405         av_log(s->avctx, AV_LOG_ERROR, "invalid subframe padding\n");
406         return AVERROR_INVALIDDATA;
407     }
408     type = get_bits(&s->gb, 6);
409
410     if (get_bits1(&s->gb)) {
411         int left = get_bits_left(&s->gb);
412         if ( left <= 0 ||
413             (left < bps && !show_bits_long(&s->gb, left)) ||
414                            !show_bits_long(&s->gb, bps)) {
415             av_log(s->avctx, AV_LOG_ERROR,
416                    "Invalid number of wasted bits > available bits (%d) - left=%d\n",
417                    bps, left);
418             return AVERROR_INVALIDDATA;
419         }
420         wasted = 1 + get_unary(&s->gb, 1, get_bits_left(&s->gb));
421         bps -= wasted;
422     }
423     if (bps > 32) {
424         avpriv_report_missing_feature(s->avctx, "Decorrelated bit depth > 32");
425         return AVERROR_PATCHWELCOME;
426     }
427
428 //FIXME use av_log2 for types
429     if (type == 0) {
430         tmp = get_sbits_long(&s->gb, bps);
431         for (i = 0; i < s->blocksize; i++)
432             decoded[i] = tmp;
433     } else if (type == 1) {
434         for (i = 0; i < s->blocksize; i++)
435             decoded[i] = get_sbits_long(&s->gb, bps);
436     } else if ((type >= 8) && (type <= 12)) {
437         if ((ret = decode_subframe_fixed(s, decoded, type & ~0x8, bps)) < 0)
438             return ret;
439     } else if (type >= 32) {
440         if ((ret = decode_subframe_lpc(s, decoded, (type & ~0x20)+1, bps)) < 0)
441             return ret;
442     } else {
443         av_log(s->avctx, AV_LOG_ERROR, "invalid coding type\n");
444         return AVERROR_INVALIDDATA;
445     }
446
447     if (wasted) {
448         int i;
449         for (i = 0; i < s->blocksize; i++)
450             decoded[i] <<= wasted;
451     }
452
453     return 0;
454 }
455
456 static int decode_frame(FLACContext *s)
457 {
458     int i, ret;
459     GetBitContext *gb = &s->gb;
460     FLACFrameInfo fi;
461
462     if ((ret = ff_flac_decode_frame_header(s->avctx, gb, &fi, 0)) < 0) {
463         av_log(s->avctx, AV_LOG_ERROR, "invalid frame header\n");
464         return ret;
465     }
466
467     if (   s->flac_stream_info.channels
468         && fi.channels != s->flac_stream_info.channels
469         && s->got_streaminfo) {
470         s->flac_stream_info.channels = s->avctx->channels = fi.channels;
471         ff_flac_set_channel_layout(s->avctx);
472         ret = allocate_buffers(s);
473         if (ret < 0)
474             return ret;
475     }
476     s->flac_stream_info.channels = s->avctx->channels = fi.channels;
477     if (!s->avctx->channel_layout)
478         ff_flac_set_channel_layout(s->avctx);
479     s->ch_mode = fi.ch_mode;
480
481     if (!s->flac_stream_info.bps && !fi.bps) {
482         av_log(s->avctx, AV_LOG_ERROR, "bps not found in STREAMINFO or frame header\n");
483         return AVERROR_INVALIDDATA;
484     }
485     if (!fi.bps) {
486         fi.bps = s->flac_stream_info.bps;
487     } else if (s->flac_stream_info.bps && fi.bps != s->flac_stream_info.bps) {
488         av_log(s->avctx, AV_LOG_ERROR, "switching bps mid-stream is not "
489                                        "supported\n");
490         return AVERROR_INVALIDDATA;
491     }
492
493     if (!s->flac_stream_info.bps) {
494         s->flac_stream_info.bps = s->avctx->bits_per_raw_sample = fi.bps;
495         flac_set_bps(s);
496     }
497
498     if (!s->flac_stream_info.max_blocksize)
499         s->flac_stream_info.max_blocksize = FLAC_MAX_BLOCKSIZE;
500     if (fi.blocksize > s->flac_stream_info.max_blocksize) {
501         av_log(s->avctx, AV_LOG_ERROR, "blocksize %d > %d\n", fi.blocksize,
502                s->flac_stream_info.max_blocksize);
503         return AVERROR_INVALIDDATA;
504     }
505     s->blocksize = fi.blocksize;
506
507     if (!s->flac_stream_info.samplerate && !fi.samplerate) {
508         av_log(s->avctx, AV_LOG_ERROR, "sample rate not found in STREAMINFO"
509                                         " or frame header\n");
510         return AVERROR_INVALIDDATA;
511     }
512     if (fi.samplerate == 0)
513         fi.samplerate = s->flac_stream_info.samplerate;
514     s->flac_stream_info.samplerate = s->avctx->sample_rate = fi.samplerate;
515
516     if (!s->got_streaminfo) {
517         ret = allocate_buffers(s);
518         if (ret < 0)
519             return ret;
520         s->got_streaminfo = 1;
521         dump_headers(s->avctx, &s->flac_stream_info);
522     }
523     ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt,
524                     s->flac_stream_info.channels, s->flac_stream_info.bps);
525
526 //    dump_headers(s->avctx, &s->flac_stream_info);
527
528     /* subframes */
529     for (i = 0; i < s->flac_stream_info.channels; i++) {
530         if ((ret = decode_subframe(s, i)) < 0)
531             return ret;
532     }
533
534     align_get_bits(gb);
535
536     /* frame footer */
537     skip_bits(gb, 16); /* data crc */
538
539     return 0;
540 }
541
542 static int flac_decode_frame(AVCodecContext *avctx, void *data,
543                              int *got_frame_ptr, AVPacket *avpkt)
544 {
545     AVFrame *frame     = data;
546     ThreadFrame tframe = { .f = data };
547     const uint8_t *buf = avpkt->data;
548     int buf_size = avpkt->size;
549     FLACContext *s = avctx->priv_data;
550     int bytes_read = 0;
551     int ret;
552
553     *got_frame_ptr = 0;
554
555     if (s->flac_stream_info.max_framesize == 0) {
556         s->flac_stream_info.max_framesize =
557             ff_flac_get_max_frame_size(s->flac_stream_info.max_blocksize ? s->flac_stream_info.max_blocksize : FLAC_MAX_BLOCKSIZE,
558                                        FLAC_MAX_CHANNELS, 32);
559     }
560
561     if (buf_size > 5 && !memcmp(buf, "\177FLAC", 5)) {
562         av_log(s->avctx, AV_LOG_DEBUG, "skipping flac header packet 1\n");
563         return buf_size;
564     }
565
566     if (buf_size > 0 && (*buf & 0x7F) == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
567         av_log(s->avctx, AV_LOG_DEBUG, "skipping vorbis comment\n");
568         return buf_size;
569     }
570
571     /* check that there is at least the smallest decodable amount of data.
572        this amount corresponds to the smallest valid FLAC frame possible.
573        FF F8 69 02 00 00 9A 00 00 34 46 */
574     if (buf_size < FLAC_MIN_FRAME_SIZE)
575         return buf_size;
576
577     /* check for inline header */
578     if (AV_RB32(buf) == MKBETAG('f','L','a','C')) {
579         if (!s->got_streaminfo && (ret = parse_streaminfo(s, buf, buf_size))) {
580             av_log(s->avctx, AV_LOG_ERROR, "invalid header\n");
581             return ret;
582         }
583         return get_metadata_size(buf, buf_size);
584     }
585
586     /* decode frame */
587     if ((ret = init_get_bits8(&s->gb, buf, buf_size)) < 0)
588         return ret;
589     if ((ret = decode_frame(s)) < 0) {
590         av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n");
591         return ret;
592     }
593     bytes_read = get_bits_count(&s->gb)/8;
594
595     if ((s->avctx->err_recognition & (AV_EF_CRCCHECK|AV_EF_COMPLIANT)) &&
596         av_crc(av_crc_get_table(AV_CRC_16_ANSI),
597                0, buf, bytes_read)) {
598         av_log(s->avctx, AV_LOG_ERROR, "CRC error at PTS %"PRId64"\n", avpkt->pts);
599         if (s->avctx->err_recognition & AV_EF_EXPLODE)
600             return AVERROR_INVALIDDATA;
601     }
602
603     /* get output buffer */
604     frame->nb_samples = s->blocksize;
605     if ((ret = ff_thread_get_buffer(avctx, &tframe, 0)) < 0)
606         return ret;
607
608     s->dsp.decorrelate[s->ch_mode](frame->data, s->decoded,
609                                    s->flac_stream_info.channels,
610                                    s->blocksize, s->sample_shift);
611
612     if (bytes_read > buf_size) {
613         av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", bytes_read - buf_size);
614         return AVERROR_INVALIDDATA;
615     }
616     if (bytes_read < buf_size) {
617         av_log(s->avctx, AV_LOG_DEBUG, "underread: %d orig size: %d\n",
618                buf_size - bytes_read, buf_size);
619     }
620
621     *got_frame_ptr = 1;
622
623     return bytes_read;
624 }
625
626 #if HAVE_THREADS
627 static int init_thread_copy(AVCodecContext *avctx)
628 {
629     FLACContext *s = avctx->priv_data;
630     s->decoded_buffer = NULL;
631     s->decoded_buffer_size = 0;
632     s->avctx = avctx;
633     if (s->flac_stream_info.max_blocksize)
634         return allocate_buffers(s);
635     return 0;
636 }
637 #endif
638
639 static av_cold int flac_decode_close(AVCodecContext *avctx)
640 {
641     FLACContext *s = avctx->priv_data;
642
643     av_freep(&s->decoded_buffer);
644
645     return 0;
646 }
647
648 static const AVOption options[] = {
649 { "use_buggy_lpc", "emulate old buggy lavc behavior", offsetof(FLACContext, buggy_lpc), AV_OPT_TYPE_INT, {.i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM },
650 { NULL },
651 };
652
653 static const AVClass flac_decoder_class = {
654     "FLAC decoder",
655     av_default_item_name,
656     options,
657     LIBAVUTIL_VERSION_INT,
658 };
659
660 AVCodec ff_flac_decoder = {
661     .name           = "flac",
662     .long_name      = NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"),
663     .type           = AVMEDIA_TYPE_AUDIO,
664     .id             = AV_CODEC_ID_FLAC,
665     .priv_data_size = sizeof(FLACContext),
666     .init           = flac_decode_init,
667     .close          = flac_decode_close,
668     .decode         = flac_decode_frame,
669     .init_thread_copy = ONLY_IF_THREADS_ENABLED(init_thread_copy),
670     .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
671     .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16,
672                                                       AV_SAMPLE_FMT_S16P,
673                                                       AV_SAMPLE_FMT_S32,
674                                                       AV_SAMPLE_FMT_S32P,
675                                                       AV_SAMPLE_FMT_NONE },
676     .priv_class     = &flac_decoder_class,
677 };