LCOV - code coverage report
Current view: top level - libavcodec - flacdec.c (source / functions) Hit Total Coverage
Test: coverage.info Lines: 219 346 63.3 %
Date: 2017-12-11 04:34:20 Functions: 12 15 80.0 %

          Line data    Source code
       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          69 : static void flac_set_bps(FLACContext *s)
      74             : {
      75          69 :     enum AVSampleFormat req = s->avctx->request_sample_fmt;
      76          69 :     int need32 = s->flac_stream_info.bps > 16;
      77          69 :     int want32 = av_get_bytes_per_sample(req) > 2;
      78          69 :     int planar = av_sample_fmt_is_planar(req);
      79             : 
      80          69 :     if (need32 || want32) {
      81           6 :         if (planar)
      82           0 :             s->avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
      83             :         else
      84           6 :             s->avctx->sample_fmt = AV_SAMPLE_FMT_S32;
      85           6 :         s->sample_shift = 32 - s->flac_stream_info.bps;
      86             :     } else {
      87          63 :         if (planar)
      88           0 :             s->avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
      89             :         else
      90          63 :             s->avctx->sample_fmt = AV_SAMPLE_FMT_S16;
      91          63 :         s->sample_shift = 16 - s->flac_stream_info.bps;
      92             :     }
      93          69 : }
      94             : 
      95          69 : static av_cold int flac_decode_init(AVCodecContext *avctx)
      96             : {
      97             :     enum FLACExtradataFormat format;
      98             :     uint8_t *streaminfo;
      99             :     int ret;
     100          69 :     FLACContext *s = avctx->priv_data;
     101          69 :     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          69 :     if (!avctx->extradata)
     106          16 :         return 0;
     107             : 
     108          53 :     if (!ff_flac_is_extradata_valid(avctx, &format, &streaminfo))
     109           0 :         return AVERROR_INVALIDDATA;
     110             : 
     111             :     /* initialize based on the demuxer-supplied streamdata header */
     112          53 :     ret = ff_flac_parse_streaminfo(avctx, &s->flac_stream_info, streaminfo);
     113          53 :     if (ret < 0)
     114           0 :         return ret;
     115          53 :     ret = allocate_buffers(s);
     116          53 :     if (ret < 0)
     117           0 :         return ret;
     118          53 :     flac_set_bps(s);
     119          53 :     ff_flacdsp_init(&s->dsp, avctx->sample_fmt,
     120             :                     s->flac_stream_info.channels, s->flac_stream_info.bps);
     121          53 :     s->got_streaminfo = 1;
     122             : 
     123          53 :     return 0;
     124             : }
     125             : 
     126          16 : static void dump_headers(AVCodecContext *avctx, FLACStreaminfo *s)
     127             : {
     128          16 :     av_log(avctx, AV_LOG_DEBUG, "  Max Blocksize: %d\n", s->max_blocksize);
     129          16 :     av_log(avctx, AV_LOG_DEBUG, "  Max Framesize: %d\n", s->max_framesize);
     130          16 :     av_log(avctx, AV_LOG_DEBUG, "  Samplerate: %d\n", s->samplerate);
     131          16 :     av_log(avctx, AV_LOG_DEBUG, "  Channels: %d\n", s->channels);
     132          16 :     av_log(avctx, AV_LOG_DEBUG, "  Bits: %d\n", s->bps);
     133          16 : }
     134             : 
     135          69 : static int allocate_buffers(FLACContext *s)
     136             : {
     137             :     int buf_size;
     138             :     int ret;
     139             : 
     140          69 :     av_assert0(s->flac_stream_info.max_blocksize);
     141             : 
     142          69 :     buf_size = av_samples_get_buffer_size(NULL, s->flac_stream_info.channels,
     143             :                                           s->flac_stream_info.max_blocksize,
     144             :                                           AV_SAMPLE_FMT_S32P, 0);
     145          69 :     if (buf_size < 0)
     146           0 :         return buf_size;
     147             : 
     148          69 :     av_fast_malloc(&s->decoded_buffer, &s->decoded_buffer_size, buf_size);
     149          69 :     if (!s->decoded_buffer)
     150           0 :         return AVERROR(ENOMEM);
     151             : 
     152         138 :     ret = av_samples_fill_arrays((uint8_t **)s->decoded, NULL,
     153          69 :                                  s->decoded_buffer,
     154             :                                  s->flac_stream_info.channels,
     155             :                                  s->flac_stream_info.max_blocksize,
     156             :                                  AV_SAMPLE_FMT_S32P, 0);
     157          69 :     return ret < 0 ? ret : 0;
     158             : }
     159             : 
     160             : /**
     161             :  * Parse the STREAMINFO from an inline header.
     162             :  * @param s the flac decoding context
     163             :  * @param buf input buffer, starting with the "fLaC" marker
     164             :  * @param buf_size buffer size
     165             :  * @return non-zero if metadata is invalid
     166             :  */
     167           0 : static int parse_streaminfo(FLACContext *s, const uint8_t *buf, int buf_size)
     168             : {
     169             :     int metadata_type, metadata_size, ret;
     170             : 
     171           0 :     if (buf_size < FLAC_STREAMINFO_SIZE+8) {
     172             :         /* need more data */
     173           0 :         return 0;
     174             :     }
     175           0 :     flac_parse_block_header(&buf[4], NULL, &metadata_type, &metadata_size);
     176           0 :     if (metadata_type != FLAC_METADATA_TYPE_STREAMINFO ||
     177           0 :         metadata_size != FLAC_STREAMINFO_SIZE) {
     178           0 :         return AVERROR_INVALIDDATA;
     179             :     }
     180           0 :     ret = ff_flac_parse_streaminfo(s->avctx, &s->flac_stream_info, &buf[8]);
     181           0 :     if (ret < 0)
     182           0 :         return ret;
     183           0 :     ret = allocate_buffers(s);
     184           0 :     if (ret < 0)
     185           0 :         return ret;
     186           0 :     flac_set_bps(s);
     187           0 :     ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt,
     188             :                     s->flac_stream_info.channels, s->flac_stream_info.bps);
     189           0 :     s->got_streaminfo = 1;
     190             : 
     191           0 :     return 0;
     192             : }
     193             : 
     194             : /**
     195             :  * Determine the size of an inline header.
     196             :  * @param buf input buffer, starting with the "fLaC" marker
     197             :  * @param buf_size buffer size
     198             :  * @return number of bytes in the header, or 0 if more data is needed
     199             :  */
     200           0 : static int get_metadata_size(const uint8_t *buf, int buf_size)
     201             : {
     202             :     int metadata_last, metadata_size;
     203           0 :     const uint8_t *buf_end = buf + buf_size;
     204             : 
     205           0 :     buf += 4;
     206             :     do {
     207           0 :         if (buf_end - buf < 4)
     208           0 :             return AVERROR_INVALIDDATA;
     209           0 :         flac_parse_block_header(buf, &metadata_last, NULL, &metadata_size);
     210           0 :         buf += 4;
     211           0 :         if (buf_end - buf < metadata_size) {
     212             :             /* need more data in order to read the complete header */
     213           0 :             return AVERROR_INVALIDDATA;
     214             :         }
     215           0 :         buf += metadata_size;
     216           0 :     } while (!metadata_last);
     217             : 
     218           0 :     return buf_size - (buf_end - buf);
     219             : }
     220             : 
     221        9823 : static int decode_residuals(FLACContext *s, int32_t *decoded, int pred_order)
     222             : {
     223        9823 :     GetBitContext gb = s->gb;
     224             :     int i, tmp, partition, method_type, rice_order;
     225             :     int rice_bits, rice_esc;
     226             :     int samples;
     227             : 
     228        9823 :     method_type = get_bits(&gb, 2);
     229        9823 :     rice_order  = get_bits(&gb, 4);
     230             : 
     231        9823 :     samples   = s->blocksize >> rice_order;
     232        9823 :     rice_bits = 4 + method_type;
     233        9823 :     rice_esc  = (1 << rice_bits) - 1;
     234             : 
     235        9823 :     decoded += pred_order;
     236        9823 :     i        = pred_order;
     237             : 
     238        9823 :     if (method_type > 1) {
     239           0 :         av_log(s->avctx, AV_LOG_ERROR, "illegal residual coding method %d\n",
     240             :                method_type);
     241           0 :         return AVERROR_INVALIDDATA;
     242             :     }
     243             : 
     244        9823 :     if (samples << rice_order != s->blocksize) {
     245           0 :         av_log(s->avctx, AV_LOG_ERROR, "invalid rice order: %i blocksize %i\n",
     246             :                rice_order, s->blocksize);
     247           0 :         return AVERROR_INVALIDDATA;
     248             :     }
     249             : 
     250        9823 :     if (pred_order > samples) {
     251           0 :         av_log(s->avctx, AV_LOG_ERROR, "invalid predictor order: %i > %i\n",
     252             :                pred_order, samples);
     253           0 :         return AVERROR_INVALIDDATA;
     254             :     }
     255             : 
     256      137218 :     for (partition = 0; partition < (1 << rice_order); partition++) {
     257      127395 :         tmp = get_bits(&gb, rice_bits);
     258      127395 :         if (tmp == rice_esc) {
     259           0 :             tmp = get_bits(&gb, 5);
     260           0 :             for (; i < samples; i++)
     261           0 :                 *decoded++ = get_sbits_long(&gb, tmp);
     262             :         } else {
     263      127395 :             int real_limit = tmp ? (INT_MAX >> tmp) + 2 : INT_MAX;
     264    54849575 :             for (; i < samples; i++) {
     265    54722180 :                 int v = get_sr_golomb_flac(&gb, tmp, real_limit, 0);
     266    54722180 :                 if (v == 0x80000000){
     267           0 :                     av_log(s->avctx, AV_LOG_ERROR, "invalid residual\n");
     268           0 :                     return AVERROR_INVALIDDATA;
     269             :                 }
     270             : 
     271    54722180 :                 *decoded++ = v;
     272             :             }
     273             :         }
     274      127395 :         i= 0;
     275             :     }
     276             : 
     277        9823 :     s->gb = gb;
     278             : 
     279        9823 :     return 0;
     280             : }
     281             : 
     282         930 : static int decode_subframe_fixed(FLACContext *s, int32_t *decoded,
     283             :                                  int pred_order, int bps)
     284             : {
     285         930 :     const int blocksize = s->blocksize;
     286         930 :     unsigned av_uninit(a), av_uninit(b), av_uninit(c), av_uninit(d);
     287             :     int i;
     288             :     int ret;
     289             : 
     290             :     /* warm up samples */
     291        3578 :     for (i = 0; i < pred_order; i++) {
     292        2648 :         decoded[i] = get_sbits_long(&s->gb, bps);
     293             :     }
     294             : 
     295         930 :     if ((ret = decode_residuals(s, decoded, pred_order)) < 0)
     296           0 :         return ret;
     297             : 
     298         930 :     if (pred_order > 0)
     299         832 :         a = decoded[pred_order-1];
     300         930 :     if (pred_order > 1)
     301         718 :         b = a - decoded[pred_order-2];
     302         930 :     if (pred_order > 2)
     303         588 :         c = b - decoded[pred_order-2] + decoded[pred_order-3];
     304         930 :     if (pred_order > 3)
     305         510 :         d = c - decoded[pred_order-2] + 2*decoded[pred_order-3] - decoded[pred_order-4];
     306             : 
     307         930 :     switch (pred_order) {
     308          98 :     case 0:
     309          98 :         break;
     310         114 :     case 1:
     311      496640 :         for (i = pred_order; i < blocksize; i++)
     312      496526 :             decoded[i] = a += decoded[i];
     313         114 :         break;
     314         130 :     case 2:
     315      549770 :         for (i = pred_order; i < blocksize; i++)
     316      549640 :             decoded[i] = a += b += decoded[i];
     317         130 :         break;
     318          78 :     case 3:
     319      117348 :         for (i = pred_order; i < blocksize; i++)
     320      117270 :             decoded[i] = a += b += c += decoded[i];
     321          78 :         break;
     322         510 :     case 4:
     323      584550 :         for (i = pred_order; i < blocksize; i++)
     324      584040 :             decoded[i] = a += b += c += d += decoded[i];
     325         510 :         break;
     326           0 :     default:
     327           0 :         av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order);
     328           0 :         return AVERROR_INVALIDDATA;
     329             :     }
     330             : 
     331         930 :     return 0;
     332             : }
     333             : 
     334        1901 : static void lpc_analyze_remodulate(SUINT32 *decoded, const int coeffs[32],
     335             :                                    int order, int qlevel, int len, int bps)
     336             : {
     337             :     int i, j;
     338        1901 :     int ebps = 1 << (bps-1);
     339        1901 :     unsigned sigma = 0;
     340             : 
     341     8342972 :     for (i = order; i < len; i++)
     342     8341071 :         sigma |= decoded[i] + ebps;
     343             : 
     344        1901 :     if (sigma < 2*ebps)
     345        1901 :         return;
     346             : 
     347           0 :     for (i = len - 1; i >= order; i--) {
     348           0 :         int64_t p = 0;
     349           0 :         for (j = 0; j < order; j++)
     350           0 :             p += coeffs[j] * (int64_t)(int32_t)decoded[i-order+j];
     351           0 :         decoded[i] -= p >> qlevel;
     352             :     }
     353           0 :     for (i = order; i < len; i++, decoded++) {
     354           0 :         int32_t p = 0;
     355           0 :         for (j = 0; j < order; j++)
     356           0 :             p += coeffs[j] * (uint32_t)decoded[j];
     357           0 :         decoded[j] += p >> qlevel;
     358             :     }
     359             : }
     360             : 
     361        8893 : static int decode_subframe_lpc(FLACContext *s, int32_t *decoded, int pred_order,
     362             :                                int bps)
     363             : {
     364             :     int i, ret;
     365             :     int coeff_prec, qlevel;
     366             :     int coeffs[32];
     367             : 
     368             :     /* warm up samples */
     369       44535 :     for (i = 0; i < pred_order; i++) {
     370       35642 :         decoded[i] = get_sbits_long(&s->gb, bps);
     371             :     }
     372             : 
     373        8893 :     coeff_prec = get_bits(&s->gb, 4) + 1;
     374        8893 :     if (coeff_prec == 16) {
     375           0 :         av_log(s->avctx, AV_LOG_ERROR, "invalid coeff precision\n");
     376           0 :         return AVERROR_INVALIDDATA;
     377             :     }
     378        8893 :     qlevel = get_sbits(&s->gb, 5);
     379        8893 :     if (qlevel < 0) {
     380           0 :         av_log(s->avctx, AV_LOG_ERROR, "qlevel %d not supported, maybe buggy stream\n",
     381             :                qlevel);
     382           0 :         return AVERROR_INVALIDDATA;
     383             :     }
     384             : 
     385       44535 :     for (i = 0; i < pred_order; i++) {
     386       35642 :         coeffs[pred_order - i - 1] = get_sbits(&s->gb, coeff_prec);
     387             :     }
     388             : 
     389        8893 :     if ((ret = decode_residuals(s, decoded, pred_order)) < 0)
     390           0 :         return ret;
     391             : 
     392        8893 :     if (   (    s->buggy_lpc && s->flac_stream_info.bps <= 16)
     393        8893 :         || (   !s->buggy_lpc && bps <= 16
     394        6830 :             && bps + coeff_prec + av_log2(pred_order) <= 32)) {
     395        6152 :         s->dsp.lpc16(decoded, coeffs, pred_order, qlevel, s->blocksize);
     396             :     } else {
     397        2741 :         s->dsp.lpc32(decoded, coeffs, pred_order, qlevel, s->blocksize);
     398        2741 :         if (s->flac_stream_info.bps <= 16)
     399        1901 :             lpc_analyze_remodulate(decoded, coeffs, pred_order, qlevel, s->blocksize, bps);
     400             :     }
     401             : 
     402        8893 :     return 0;
     403             : }
     404             : 
     405       15326 : static inline int decode_subframe(FLACContext *s, int channel)
     406             : {
     407       15326 :     int32_t *decoded = s->decoded[channel];
     408       15326 :     int type, wasted = 0;
     409       15326 :     int bps = s->flac_stream_info.bps;
     410             :     int i, tmp, ret;
     411             : 
     412       15326 :     if (channel == 0) {
     413        5668 :         if (s->ch_mode == FLAC_CHMODE_RIGHT_SIDE)
     414         548 :             bps++;
     415             :     } else {
     416        9658 :         if (s->ch_mode == FLAC_CHMODE_LEFT_SIDE || s->ch_mode == FLAC_CHMODE_MID_SIDE)
     417        2306 :             bps++;
     418             :     }
     419             : 
     420       15326 :     if (get_bits1(&s->gb)) {
     421           0 :         av_log(s->avctx, AV_LOG_ERROR, "invalid subframe padding\n");
     422           0 :         return AVERROR_INVALIDDATA;
     423             :     }
     424       15326 :     type = get_bits(&s->gb, 6);
     425             : 
     426       15326 :     if (get_bits1(&s->gb)) {
     427        5231 :         int left = get_bits_left(&s->gb);
     428        5231 :         if ( left <= 0 ||
     429        5231 :             (left < bps && !show_bits_long(&s->gb, left)) ||
     430        5231 :                            !show_bits_long(&s->gb, bps)) {
     431           0 :             av_log(s->avctx, AV_LOG_ERROR,
     432             :                    "Invalid number of wasted bits > available bits (%d) - left=%d\n",
     433             :                    bps, left);
     434           0 :             return AVERROR_INVALIDDATA;
     435             :         }
     436        5231 :         wasted = 1 + get_unary(&s->gb, 1, get_bits_left(&s->gb));
     437        5231 :         bps -= wasted;
     438             :     }
     439       15326 :     if (bps > 32) {
     440           0 :         avpriv_report_missing_feature(s->avctx, "Decorrelated bit depth > 32");
     441           0 :         return AVERROR_PATCHWELCOME;
     442             :     }
     443             : 
     444             : //FIXME use av_log2 for types
     445       15326 :     if (type == 0) {
     446        5503 :         tmp = get_sbits_long(&s->gb, bps);
     447    34607103 :         for (i = 0; i < s->blocksize; i++)
     448    34601600 :             decoded[i] = tmp;
     449        9823 :     } else if (type == 1) {
     450           0 :         for (i = 0; i < s->blocksize; i++)
     451           0 :             decoded[i] = get_sbits_long(&s->gb, bps);
     452        9823 :     } else if ((type >= 8) && (type <= 12)) {
     453        1860 :         if ((ret = decode_subframe_fixed(s, decoded, type & ~0x8, bps)) < 0)
     454           0 :             return ret;
     455        8893 :     } else if (type >= 32) {
     456        8893 :         if ((ret = decode_subframe_lpc(s, decoded, (type & ~0x20)+1, bps)) < 0)
     457           0 :             return ret;
     458             :     } else {
     459           0 :         av_log(s->avctx, AV_LOG_ERROR, "invalid coding type\n");
     460           0 :         return AVERROR_INVALIDDATA;
     461             :     }
     462             : 
     463       15326 :     if (wasted) {
     464             :         int i;
     465    34170231 :         for (i = 0; i < s->blocksize; i++)
     466    34165000 :             decoded[i] = (unsigned)decoded[i] << wasted;
     467             :     }
     468             : 
     469       15326 :     return 0;
     470             : }
     471             : 
     472        5668 : static int decode_frame(FLACContext *s)
     473             : {
     474             :     int i, ret;
     475        5668 :     GetBitContext *gb = &s->gb;
     476             :     FLACFrameInfo fi;
     477             : 
     478        5668 :     if ((ret = ff_flac_decode_frame_header(s->avctx, gb, &fi, 0)) < 0) {
     479           0 :         av_log(s->avctx, AV_LOG_ERROR, "invalid frame header\n");
     480           0 :         return ret;
     481             :     }
     482             : 
     483        5668 :     if (   s->flac_stream_info.channels
     484        5652 :         && fi.channels != s->flac_stream_info.channels
     485           0 :         && s->got_streaminfo) {
     486           0 :         s->flac_stream_info.channels = s->avctx->channels = fi.channels;
     487           0 :         ff_flac_set_channel_layout(s->avctx);
     488           0 :         ret = allocate_buffers(s);
     489           0 :         if (ret < 0)
     490           0 :             return ret;
     491             :     }
     492        5668 :     s->flac_stream_info.channels = s->avctx->channels = fi.channels;
     493        5668 :     if (!s->avctx->channel_layout)
     494           0 :         ff_flac_set_channel_layout(s->avctx);
     495        5668 :     s->ch_mode = fi.ch_mode;
     496             : 
     497        5668 :     if (!s->flac_stream_info.bps && !fi.bps) {
     498           0 :         av_log(s->avctx, AV_LOG_ERROR, "bps not found in STREAMINFO or frame header\n");
     499           0 :         return AVERROR_INVALIDDATA;
     500             :     }
     501        5668 :     if (!fi.bps) {
     502           0 :         fi.bps = s->flac_stream_info.bps;
     503        5668 :     } else if (s->flac_stream_info.bps && fi.bps != s->flac_stream_info.bps) {
     504           0 :         av_log(s->avctx, AV_LOG_ERROR, "switching bps mid-stream is not "
     505             :                                        "supported\n");
     506           0 :         return AVERROR_INVALIDDATA;
     507             :     }
     508             : 
     509        5668 :     if (!s->flac_stream_info.bps) {
     510          16 :         s->flac_stream_info.bps = s->avctx->bits_per_raw_sample = fi.bps;
     511          16 :         flac_set_bps(s);
     512             :     }
     513             : 
     514        5668 :     if (!s->flac_stream_info.max_blocksize)
     515          16 :         s->flac_stream_info.max_blocksize = FLAC_MAX_BLOCKSIZE;
     516        5668 :     if (fi.blocksize > s->flac_stream_info.max_blocksize) {
     517           0 :         av_log(s->avctx, AV_LOG_ERROR, "blocksize %d > %d\n", fi.blocksize,
     518             :                s->flac_stream_info.max_blocksize);
     519           0 :         return AVERROR_INVALIDDATA;
     520             :     }
     521        5668 :     s->blocksize = fi.blocksize;
     522             : 
     523        5668 :     if (!s->flac_stream_info.samplerate && !fi.samplerate) {
     524           0 :         av_log(s->avctx, AV_LOG_ERROR, "sample rate not found in STREAMINFO"
     525             :                                         " or frame header\n");
     526           0 :         return AVERROR_INVALIDDATA;
     527             :     }
     528        5668 :     if (fi.samplerate == 0)
     529           0 :         fi.samplerate = s->flac_stream_info.samplerate;
     530        5668 :     s->flac_stream_info.samplerate = s->avctx->sample_rate = fi.samplerate;
     531             : 
     532        5668 :     if (!s->got_streaminfo) {
     533          16 :         ret = allocate_buffers(s);
     534          16 :         if (ret < 0)
     535           0 :             return ret;
     536          16 :         s->got_streaminfo = 1;
     537          16 :         dump_headers(s->avctx, &s->flac_stream_info);
     538             :     }
     539        5668 :     ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt,
     540             :                     s->flac_stream_info.channels, s->flac_stream_info.bps);
     541             : 
     542             : //    dump_headers(s->avctx, &s->flac_stream_info);
     543             : 
     544             :     /* subframes */
     545       20994 :     for (i = 0; i < s->flac_stream_info.channels; i++) {
     546       15326 :         if ((ret = decode_subframe(s, i)) < 0)
     547           0 :             return ret;
     548             :     }
     549             : 
     550        5668 :     align_get_bits(gb);
     551             : 
     552             :     /* frame footer */
     553        5668 :     skip_bits(gb, 16); /* data crc */
     554             : 
     555        5668 :     return 0;
     556             : }
     557             : 
     558        5668 : static int flac_decode_frame(AVCodecContext *avctx, void *data,
     559             :                              int *got_frame_ptr, AVPacket *avpkt)
     560             : {
     561        5668 :     AVFrame *frame     = data;
     562        5668 :     ThreadFrame tframe = { .f = data };
     563        5668 :     const uint8_t *buf = avpkt->data;
     564        5668 :     int buf_size = avpkt->size;
     565        5668 :     FLACContext *s = avctx->priv_data;
     566        5668 :     int bytes_read = 0;
     567             :     int ret;
     568             : 
     569        5668 :     *got_frame_ptr = 0;
     570             : 
     571        5668 :     if (s->flac_stream_info.max_framesize == 0) {
     572          16 :         s->flac_stream_info.max_framesize =
     573          16 :             ff_flac_get_max_frame_size(s->flac_stream_info.max_blocksize ? s->flac_stream_info.max_blocksize : FLAC_MAX_BLOCKSIZE,
     574             :                                        FLAC_MAX_CHANNELS, 32);
     575             :     }
     576             : 
     577        5668 :     if (buf_size > 5 && !memcmp(buf, "\177FLAC", 5)) {
     578           0 :         av_log(s->avctx, AV_LOG_DEBUG, "skipping flac header packet 1\n");
     579           0 :         return buf_size;
     580             :     }
     581             : 
     582        5668 :     if (buf_size > 0 && (*buf & 0x7F) == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
     583           0 :         av_log(s->avctx, AV_LOG_DEBUG, "skipping vorbis comment\n");
     584           0 :         return buf_size;
     585             :     }
     586             : 
     587             :     /* check that there is at least the smallest decodable amount of data.
     588             :        this amount corresponds to the smallest valid FLAC frame possible.
     589             :        FF F8 69 02 00 00 9A 00 00 34 46 */
     590        5668 :     if (buf_size < FLAC_MIN_FRAME_SIZE)
     591           0 :         return buf_size;
     592             : 
     593             :     /* check for inline header */
     594        5668 :     if (AV_RB32(buf) == MKBETAG('f','L','a','C')) {
     595           0 :         if (!s->got_streaminfo && (ret = parse_streaminfo(s, buf, buf_size))) {
     596           0 :             av_log(s->avctx, AV_LOG_ERROR, "invalid header\n");
     597           0 :             return ret;
     598             :         }
     599           0 :         return get_metadata_size(buf, buf_size);
     600             :     }
     601             : 
     602             :     /* decode frame */
     603        5668 :     if ((ret = init_get_bits8(&s->gb, buf, buf_size)) < 0)
     604           0 :         return ret;
     605        5668 :     if ((ret = decode_frame(s)) < 0) {
     606           0 :         av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n");
     607           0 :         return ret;
     608             :     }
     609        5668 :     bytes_read = get_bits_count(&s->gb)/8;
     610             : 
     611        5668 :     if ((s->avctx->err_recognition & (AV_EF_CRCCHECK|AV_EF_COMPLIANT)) &&
     612           0 :         av_crc(av_crc_get_table(AV_CRC_16_ANSI),
     613             :                0, buf, bytes_read)) {
     614           0 :         av_log(s->avctx, AV_LOG_ERROR, "CRC error at PTS %"PRId64"\n", avpkt->pts);
     615           0 :         if (s->avctx->err_recognition & AV_EF_EXPLODE)
     616           0 :             return AVERROR_INVALIDDATA;
     617             :     }
     618             : 
     619             :     /* get output buffer */
     620        5668 :     frame->nb_samples = s->blocksize;
     621        5668 :     if ((ret = ff_thread_get_buffer(avctx, &tframe, 0)) < 0)
     622           0 :         return ret;
     623             : 
     624        5668 :     s->dsp.decorrelate[s->ch_mode](frame->data, s->decoded,
     625             :                                    s->flac_stream_info.channels,
     626             :                                    s->blocksize, s->sample_shift);
     627             : 
     628        5668 :     if (bytes_read > buf_size) {
     629           0 :         av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", bytes_read - buf_size);
     630           0 :         return AVERROR_INVALIDDATA;
     631             :     }
     632        5668 :     if (bytes_read < buf_size) {
     633           0 :         av_log(s->avctx, AV_LOG_DEBUG, "underread: %d orig size: %d\n",
     634             :                buf_size - bytes_read, buf_size);
     635             :     }
     636             : 
     637        5668 :     *got_frame_ptr = 1;
     638             : 
     639        5668 :     return bytes_read;
     640             : }
     641             : 
     642             : #if HAVE_THREADS
     643           0 : static int init_thread_copy(AVCodecContext *avctx)
     644             : {
     645           0 :     FLACContext *s = avctx->priv_data;
     646           0 :     s->decoded_buffer = NULL;
     647           0 :     s->decoded_buffer_size = 0;
     648           0 :     s->avctx = avctx;
     649           0 :     if (s->flac_stream_info.max_blocksize)
     650           0 :         return allocate_buffers(s);
     651           0 :     return 0;
     652             : }
     653             : #endif
     654             : 
     655          69 : static av_cold int flac_decode_close(AVCodecContext *avctx)
     656             : {
     657          69 :     FLACContext *s = avctx->priv_data;
     658             : 
     659          69 :     av_freep(&s->decoded_buffer);
     660             : 
     661          69 :     return 0;
     662             : }
     663             : 
     664             : static const AVOption options[] = {
     665             : { "use_buggy_lpc", "emulate old buggy lavc behavior", offsetof(FLACContext, buggy_lpc), AV_OPT_TYPE_BOOL, {.i64 = 0 }, 0, 1, AV_OPT_FLAG_DECODING_PARAM | AV_OPT_FLAG_AUDIO_PARAM },
     666             : { NULL },
     667             : };
     668             : 
     669             : static const AVClass flac_decoder_class = {
     670             :     "FLAC decoder",
     671             :     av_default_item_name,
     672             :     options,
     673             :     LIBAVUTIL_VERSION_INT,
     674             : };
     675             : 
     676             : AVCodec ff_flac_decoder = {
     677             :     .name           = "flac",
     678             :     .long_name      = NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"),
     679             :     .type           = AVMEDIA_TYPE_AUDIO,
     680             :     .id             = AV_CODEC_ID_FLAC,
     681             :     .priv_data_size = sizeof(FLACContext),
     682             :     .init           = flac_decode_init,
     683             :     .close          = flac_decode_close,
     684             :     .decode         = flac_decode_frame,
     685             :     .init_thread_copy = ONLY_IF_THREADS_ENABLED(init_thread_copy),
     686             :     .capabilities   = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_FRAME_THREADS,
     687             :     .sample_fmts    = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16,
     688             :                                                       AV_SAMPLE_FMT_S16P,
     689             :                                                       AV_SAMPLE_FMT_S32,
     690             :                                                       AV_SAMPLE_FMT_S32P,
     691             :                                                       AV_SAMPLE_FMT_NONE },
     692             :     .priv_class     = &flac_decoder_class,
     693             : };

Generated by: LCOV version 1.13