libavcodec/flacdec.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 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/mem.h" | ||
| 39 | #include "libavutil/opt.h" | ||
| 40 | #include "avcodec.h" | ||
| 41 | #include "codec_internal.h" | ||
| 42 | #include "get_bits.h" | ||
| 43 | #include "golomb.h" | ||
| 44 | #include "flac.h" | ||
| 45 | #include "flacdsp.h" | ||
| 46 | #include "flac_parse.h" | ||
| 47 | #include "thread.h" | ||
| 48 | #include "unary.h" | ||
| 49 | |||
| 50 | |||
| 51 | typedef struct FLACContext { | ||
| 52 | AVClass *class; | ||
| 53 | FLACStreaminfo 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 | unsigned streaminfo_serial; ///< counts the STREAMINFOs taken from packet side data | ||
| 63 | |||
| 64 | int32_t *decoded[FLAC_MAX_CHANNELS]; ///< decoded samples | ||
| 65 | uint8_t *decoded_buffer; | ||
| 66 | unsigned int decoded_buffer_size; | ||
| 67 | int64_t *decoded_33bps; ///< decoded samples for a 33 bps subframe | ||
| 68 | uint8_t *decoded_buffer_33bps; | ||
| 69 | unsigned int decoded_buffer_size_33bps; | ||
| 70 | int buggy_lpc; ///< use workaround for old lavc encoded files | ||
| 71 | |||
| 72 | FLACDSPContext dsp; | ||
| 73 | } FLACContext; | ||
| 74 | |||
| 75 | static int allocate_buffers(FLACContext *s); | ||
| 76 | |||
| 77 | 377 | static void flac_set_bps(FLACContext *s) | |
| 78 | { | ||
| 79 | 377 | enum AVSampleFormat req = s->avctx->request_sample_fmt; | |
| 80 | 377 | int need32 = s->stream_info.bps > 16; | |
| 81 | 377 | int want32 = av_get_bytes_per_sample(req) > 2; | |
| 82 | 377 | int planar = av_sample_fmt_is_planar(req); | |
| 83 | |||
| 84 | 377 | if (need32 || want32) { | |
| 85 | 28 | if (planar) | |
| 86 | ✗ | s->avctx->sample_fmt = AV_SAMPLE_FMT_S32P; | |
| 87 | else | ||
| 88 | 28 | s->avctx->sample_fmt = AV_SAMPLE_FMT_S32; | |
| 89 | 28 | s->sample_shift = 32 - s->stream_info.bps; | |
| 90 | } else { | ||
| 91 | 349 | if (planar) | |
| 92 | ✗ | s->avctx->sample_fmt = AV_SAMPLE_FMT_S16P; | |
| 93 | else | ||
| 94 | 349 | s->avctx->sample_fmt = AV_SAMPLE_FMT_S16; | |
| 95 | 349 | s->sample_shift = 16 - s->stream_info.bps; | |
| 96 | } | ||
| 97 | 377 | } | |
| 98 | |||
| 99 | /** | ||
| 100 | * Set the decoder up for s->stream_info, which it cannot decode with until the | ||
| 101 | * buffers are allocated. | ||
| 102 | */ | ||
| 103 | 361 | static int setup_streaminfo(FLACContext *s) | |
| 104 | { | ||
| 105 | int ret; | ||
| 106 | |||
| 107 | 361 | s->got_streaminfo = 0; | |
| 108 | 361 | ret = allocate_buffers(s); | |
| 109 | 361 | if (ret < 0) | |
| 110 | ✗ | return ret; | |
| 111 | 361 | flac_set_bps(s); | |
| 112 | 361 | ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt, | |
| 113 | s->stream_info.channels); | ||
| 114 | 361 | s->got_streaminfo = 1; | |
| 115 | |||
| 116 | 361 | return 0; | |
| 117 | } | ||
| 118 | |||
| 119 | 359 | static int apply_streaminfo(FLACContext *s, const uint8_t *streaminfo) | |
| 120 | { | ||
| 121 | FLACStreaminfo stream_info; | ||
| 122 | 359 | int ret = ff_flac_parse_streaminfo(s->avctx, &stream_info, streaminfo); | |
| 123 | 359 | if (ret < 0) | |
| 124 | ✗ | return ret; | |
| 125 | 359 | s->stream_info = stream_info; | |
| 126 | |||
| 127 | 359 | return setup_streaminfo(s); | |
| 128 | } | ||
| 129 | |||
| 130 | 367 | static av_cold int flac_decode_init(AVCodecContext *avctx) | |
| 131 | { | ||
| 132 | uint8_t *streaminfo; | ||
| 133 | 367 | FLACContext *s = avctx->priv_data; | |
| 134 | 367 | s->avctx = avctx; | |
| 135 | |||
| 136 | /* for now, the raw FLAC header is allowed to be passed to the decoder as | ||
| 137 | frame data instead of extradata. */ | ||
| 138 | 367 | if (!avctx->extradata) | |
| 139 | 16 | return 0; | |
| 140 | |||
| 141 | 351 | if (!ff_flac_is_extradata_valid(avctx, &streaminfo)) | |
| 142 | ✗ | return AVERROR_INVALIDDATA; | |
| 143 | |||
| 144 | /* initialize based on the demuxer-supplied streamdata header */ | ||
| 145 | 351 | return apply_streaminfo(s, streaminfo); | |
| 146 | } | ||
| 147 | |||
| 148 | 16 | static void dump_headers(AVCodecContext *avctx, FLACStreaminfo *s) | |
| 149 | { | ||
| 150 | 16 | av_log(avctx, AV_LOG_DEBUG, " Max Blocksize: %d\n", s->max_blocksize); | |
| 151 | 16 | av_log(avctx, AV_LOG_DEBUG, " Max Framesize: %d\n", s->max_framesize); | |
| 152 | 16 | av_log(avctx, AV_LOG_DEBUG, " Samplerate: %d\n", s->samplerate); | |
| 153 | 16 | av_log(avctx, AV_LOG_DEBUG, " Channels: %d\n", s->channels); | |
| 154 | 16 | av_log(avctx, AV_LOG_DEBUG, " Bits: %d\n", s->bps); | |
| 155 | 16 | } | |
| 156 | |||
| 157 | 377 | static int allocate_buffers(FLACContext *s) | |
| 158 | { | ||
| 159 | int buf_size; | ||
| 160 | int ret; | ||
| 161 | |||
| 162 | 377 | av_assert0(s->stream_info.max_blocksize); | |
| 163 | |||
| 164 | 377 | buf_size = av_samples_get_buffer_size(NULL, s->stream_info.channels, | |
| 165 | s->stream_info.max_blocksize, | ||
| 166 | AV_SAMPLE_FMT_S32P, 0); | ||
| 167 | 377 | if (buf_size < 0) | |
| 168 | ✗ | return buf_size; | |
| 169 | |||
| 170 | 377 | av_fast_malloc(&s->decoded_buffer, &s->decoded_buffer_size, buf_size); | |
| 171 | 377 | if (!s->decoded_buffer) | |
| 172 | ✗ | return AVERROR(ENOMEM); | |
| 173 | |||
| 174 | 377 | ret = av_samples_fill_arrays((uint8_t **)s->decoded, NULL, | |
| 175 | 377 | s->decoded_buffer, | |
| 176 | s->stream_info.channels, | ||
| 177 | s->stream_info.max_blocksize, | ||
| 178 | AV_SAMPLE_FMT_S32P, 0); | ||
| 179 | 377 | if (ret >= 0 && s->stream_info.bps == 32 && s->stream_info.channels == 2) { | |
| 180 | 2 | buf_size = av_samples_get_buffer_size(NULL, 1, | |
| 181 | s->stream_info.max_blocksize, | ||
| 182 | AV_SAMPLE_FMT_S64P, 0); | ||
| 183 | 2 | if (buf_size < 0) | |
| 184 | ✗ | return buf_size; | |
| 185 | |||
| 186 | 2 | av_fast_malloc(&s->decoded_buffer_33bps, &s->decoded_buffer_size_33bps, buf_size); | |
| 187 | 2 | if (!s->decoded_buffer_33bps) | |
| 188 | ✗ | return AVERROR(ENOMEM); | |
| 189 | |||
| 190 | 2 | ret = av_samples_fill_arrays((uint8_t **)&s->decoded_33bps, NULL, | |
| 191 | 2 | s->decoded_buffer_33bps, | |
| 192 | 1, | ||
| 193 | s->stream_info.max_blocksize, | ||
| 194 | AV_SAMPLE_FMT_S64P, 0); | ||
| 195 | |||
| 196 | } | ||
| 197 | 377 | return ret < 0 ? ret : 0; | |
| 198 | } | ||
| 199 | |||
| 200 | /** | ||
| 201 | * Parse the STREAMINFO from an inline header. | ||
| 202 | * @param s the flac decoding context | ||
| 203 | * @param buf input buffer, starting with the "fLaC" marker | ||
| 204 | * @param buf_size buffer size | ||
| 205 | * @return non-zero if metadata is invalid | ||
| 206 | */ | ||
| 207 | ✗ | static int parse_streaminfo(FLACContext *s, const uint8_t *buf, int buf_size) | |
| 208 | { | ||
| 209 | int metadata_type, metadata_size; | ||
| 210 | |||
| 211 | ✗ | if (buf_size < FLAC_STREAMINFO_SIZE+8) { | |
| 212 | /* need more data */ | ||
| 213 | ✗ | return 0; | |
| 214 | } | ||
| 215 | ✗ | flac_parse_block_header(&buf[4], NULL, &metadata_type, &metadata_size); | |
| 216 | ✗ | if (metadata_type != FLAC_METADATA_TYPE_STREAMINFO || | |
| 217 | ✗ | metadata_size != FLAC_STREAMINFO_SIZE) { | |
| 218 | ✗ | return AVERROR_INVALIDDATA; | |
| 219 | } | ||
| 220 | ✗ | return apply_streaminfo(s, &buf[8]); | |
| 221 | } | ||
| 222 | |||
| 223 | /** | ||
| 224 | * Determine the size of an inline header. | ||
| 225 | * @param buf input buffer, starting with the "fLaC" marker | ||
| 226 | * @param buf_size buffer size | ||
| 227 | * @return number of bytes in the header, or 0 if more data is needed | ||
| 228 | */ | ||
| 229 | ✗ | static int get_metadata_size(const uint8_t *buf, int buf_size) | |
| 230 | { | ||
| 231 | int metadata_last, metadata_size; | ||
| 232 | ✗ | const uint8_t *buf_end = buf + buf_size; | |
| 233 | |||
| 234 | ✗ | buf += 4; | |
| 235 | do { | ||
| 236 | ✗ | if (buf_end - buf < 4) | |
| 237 | ✗ | return AVERROR_INVALIDDATA; | |
| 238 | ✗ | flac_parse_block_header(buf, &metadata_last, NULL, &metadata_size); | |
| 239 | ✗ | buf += 4; | |
| 240 | ✗ | if (buf_end - buf < metadata_size) { | |
| 241 | /* need more data in order to read the complete header */ | ||
| 242 | ✗ | return AVERROR_INVALIDDATA; | |
| 243 | } | ||
| 244 | ✗ | buf += metadata_size; | |
| 245 | ✗ | } while (!metadata_last); | |
| 246 | |||
| 247 | ✗ | return buf_size - (buf_end - buf); | |
| 248 | } | ||
| 249 | |||
| 250 | 10968 | static int decode_residuals(FLACContext *s, int32_t *decoded, int pred_order) | |
| 251 | { | ||
| 252 | 10968 | GetBitContext gb = s->gb; | |
| 253 | int i, tmp, partition, method_type, rice_order; | ||
| 254 | int rice_bits, rice_esc; | ||
| 255 | int samples; | ||
| 256 | |||
| 257 | 10968 | method_type = get_bits(&gb, 2); | |
| 258 | 10968 | rice_order = get_bits(&gb, 4); | |
| 259 | |||
| 260 | 10968 | samples = s->blocksize >> rice_order; | |
| 261 | 10968 | rice_bits = 4 + method_type; | |
| 262 | 10968 | rice_esc = (1 << rice_bits) - 1; | |
| 263 | |||
| 264 | 10968 | decoded += pred_order; | |
| 265 | 10968 | i = pred_order; | |
| 266 | |||
| 267 | 10968 | if (method_type > 1) { | |
| 268 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "illegal residual coding method %d\n", | |
| 269 | method_type); | ||
| 270 | ✗ | return AVERROR_INVALIDDATA; | |
| 271 | } | ||
| 272 | |||
| 273 | 10968 | if (samples << rice_order != s->blocksize) { | |
| 274 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "invalid rice order: %i blocksize %i\n", | |
| 275 | rice_order, s->blocksize); | ||
| 276 | ✗ | return AVERROR_INVALIDDATA; | |
| 277 | } | ||
| 278 | |||
| 279 | 10968 | if (pred_order > samples) { | |
| 280 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "invalid predictor order: %i > %i\n", | |
| 281 | pred_order, samples); | ||
| 282 | ✗ | return AVERROR_INVALIDDATA; | |
| 283 | } | ||
| 284 | |||
| 285 | 149971 | for (partition = 0; partition < (1 << rice_order); partition++) { | |
| 286 | 139004 | tmp = get_bits(&gb, rice_bits); | |
| 287 | 139004 | if (tmp == rice_esc) { | |
| 288 | ✗ | tmp = get_bits(&gb, 5); | |
| 289 | ✗ | for (; i < samples; i++) | |
| 290 | ✗ | *decoded++ = get_sbits_long(&gb, tmp); | |
| 291 | } else { | ||
| 292 | 139004 | int real_limit = (tmp > 1) ? (INT_MAX >> (tmp - 1)) + 2 : INT_MAX; | |
| 293 | 59059292 | for (; i < samples; i++) { | |
| 294 | 58920289 | int v = get_sr_golomb_flac(&gb, tmp, real_limit, 1); | |
| 295 | 58920289 | if (v == 0x80000000){ | |
| 296 | 1 | av_log(s->avctx, AV_LOG_ERROR, "invalid residual\n"); | |
| 297 | 1 | return AVERROR_INVALIDDATA; | |
| 298 | } | ||
| 299 | |||
| 300 | 58920288 | *decoded++ = v; | |
| 301 | } | ||
| 302 | } | ||
| 303 | 139003 | i= 0; | |
| 304 | } | ||
| 305 | |||
| 306 | 10967 | s->gb = gb; | |
| 307 | |||
| 308 | 10967 | return 0; | |
| 309 | } | ||
| 310 | |||
| 311 | 463 | static int decode_subframe_fixed(FLACContext *s, int32_t *decoded, | |
| 312 | int pred_order, int bps) | ||
| 313 | { | ||
| 314 | 463 | const int blocksize = s->blocksize; | |
| 315 | 463 | unsigned av_uninit(a), av_uninit(b), av_uninit(c), av_uninit(d); | |
| 316 | int i; | ||
| 317 | int ret; | ||
| 318 | |||
| 319 | /* warm up samples */ | ||
| 320 | 1565 | for (i = 0; i < pred_order; i++) { | |
| 321 | 1102 | decoded[i] = get_sbits_long(&s->gb, bps); | |
| 322 | } | ||
| 323 | |||
| 324 | 463 | if ((ret = decode_residuals(s, decoded, pred_order)) < 0) | |
| 325 | ✗ | return ret; | |
| 326 | |||
| 327 | 463 | if (pred_order > 0) | |
| 328 | 437 | a = decoded[pred_order-1]; | |
| 329 | 463 | if (pred_order > 1) | |
| 330 | 311 | b = a - decoded[pred_order-2]; | |
| 331 | 463 | if (pred_order > 2) | |
| 332 | 183 | c = b - decoded[pred_order-2] + decoded[pred_order-3]; | |
| 333 | 463 | if (pred_order > 3) | |
| 334 | 171 | d = c - decoded[pred_order-2] + 2U*decoded[pred_order-3] - decoded[pred_order-4]; | |
| 335 | |||
| 336 | 463 | switch (pred_order) { | |
| 337 | 26 | case 0: | |
| 338 | 26 | break; | |
| 339 | 126 | case 1: | |
| 340 | 516096 | for (i = pred_order; i < blocksize; i++) | |
| 341 | 515970 | decoded[i] = a += decoded[i]; | |
| 342 | 126 | break; | |
| 343 | 128 | case 2: | |
| 344 | 518284 | for (i = pred_order; i < blocksize; i++) | |
| 345 | 518156 | decoded[i] = a += b += decoded[i]; | |
| 346 | 128 | break; | |
| 347 | 12 | case 3: | |
| 348 | 49128 | for (i = pred_order; i < blocksize; i++) | |
| 349 | 49116 | decoded[i] = a += b += c += decoded[i]; | |
| 350 | 12 | break; | |
| 351 | 171 | case 4: | |
| 352 | 693343 | for (i = pred_order; i < blocksize; i++) | |
| 353 | 693172 | decoded[i] = a += b += c += d += decoded[i]; | |
| 354 | 171 | break; | |
| 355 | ✗ | default: | |
| 356 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order); | |
| 357 | ✗ | return AVERROR_INVALIDDATA; | |
| 358 | } | ||
| 359 | |||
| 360 | 463 | return 0; | |
| 361 | } | ||
| 362 | |||
| 363 | #define DECODER_SUBFRAME_FIXED_WIDE(residual) { \ | ||
| 364 | const int blocksize = s->blocksize; \ | ||
| 365 | int ret; \ | ||
| 366 | \ | ||
| 367 | if ((ret = decode_residuals(s, residual, pred_order)) < 0) \ | ||
| 368 | return ret; \ | ||
| 369 | \ | ||
| 370 | switch (pred_order) { \ | ||
| 371 | case 0: \ | ||
| 372 | for (int i = pred_order; i < blocksize; i++) \ | ||
| 373 | decoded[i] = residual[i]; \ | ||
| 374 | break; \ | ||
| 375 | case 1: \ | ||
| 376 | for (int i = pred_order; i < blocksize; i++) \ | ||
| 377 | decoded[i] = (uint64_t)residual[i] + (uint64_t)decoded[i-1];\ | ||
| 378 | break; \ | ||
| 379 | case 2: \ | ||
| 380 | for (int i = pred_order; i < blocksize; i++) \ | ||
| 381 | decoded[i] = (uint64_t)residual[i] + 2*(uint64_t)decoded[i-1] - (uint64_t)decoded[i-2]; \ | ||
| 382 | break; \ | ||
| 383 | case 3: \ | ||
| 384 | for (int i = pred_order; i < blocksize; i++) \ | ||
| 385 | decoded[i] = (uint64_t)residual[i] + 3*(uint64_t)decoded[i-1] - 3*(uint64_t)decoded[i-2] + (uint64_t)decoded[i-3]; \ | ||
| 386 | break; \ | ||
| 387 | case 4: \ | ||
| 388 | for (int i = pred_order; i < blocksize; i++) \ | ||
| 389 | decoded[i] = (uint64_t)residual[i] + 4*(uint64_t)decoded[i-1] - 6*(uint64_t)decoded[i-2] + 4*(uint64_t)decoded[i-3] - (uint64_t)decoded[i-4]; \ | ||
| 390 | break; \ | ||
| 391 | default: \ | ||
| 392 | av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order); \ | ||
| 393 | return AVERROR_INVALIDDATA; \ | ||
| 394 | } \ | ||
| 395 | return 0; \ | ||
| 396 | } | ||
| 397 | |||
| 398 | ✗ | static int decode_subframe_fixed_wide(FLACContext *s, int32_t *decoded, | |
| 399 | int pred_order, int bps) | ||
| 400 | { | ||
| 401 | /* warm up samples */ | ||
| 402 | ✗ | for (int i = 0; i < pred_order; i++) { | |
| 403 | ✗ | decoded[i] = get_sbits_long(&s->gb, bps); | |
| 404 | } | ||
| 405 | ✗ | DECODER_SUBFRAME_FIXED_WIDE(decoded); | |
| 406 | } | ||
| 407 | |||
| 408 | |||
| 409 | ✗ | static int decode_subframe_fixed_33bps(FLACContext *s, int64_t *decoded, | |
| 410 | int32_t *residual, int pred_order) | ||
| 411 | { | ||
| 412 | /* warm up samples */ \ | ||
| 413 | ✗ | for (int i = 0; i < pred_order; i++) { \ | |
| 414 | ✗ | decoded[i] = get_sbits64(&s->gb, 33); \ | |
| 415 | } \ | ||
| 416 | ✗ | DECODER_SUBFRAME_FIXED_WIDE(residual); | |
| 417 | } | ||
| 418 | |||
| 419 | 2830 | static void lpc_analyze_remodulate(SUINT32 *decoded, const int coeffs[32], | |
| 420 | int order, int qlevel, int len, int bps) | ||
| 421 | { | ||
| 422 | int i, j; | ||
| 423 | 2830 | int ebps = 1 << (bps-1); | |
| 424 | 2830 | unsigned sigma = 0; | |
| 425 | |||
| 426 | 9553966 | for (i = order; i < len; i++) | |
| 427 | 9551136 | sigma |= decoded[i] + ebps; | |
| 428 | |||
| 429 | 2830 | if (sigma < 2*ebps) | |
| 430 | 2830 | return; | |
| 431 | |||
| 432 | ✗ | for (i = len - 1; i >= order; i--) { | |
| 433 | ✗ | int64_t p = 0; | |
| 434 | ✗ | for (j = 0; j < order; j++) | |
| 435 | ✗ | p += coeffs[j] * (int64_t)(int32_t)decoded[i-order+j]; | |
| 436 | ✗ | decoded[i] -= p >> qlevel; | |
| 437 | } | ||
| 438 | ✗ | for (i = order; i < len; i++, decoded++) { | |
| 439 | ✗ | int32_t p = 0; | |
| 440 | ✗ | for (j = 0; j < order; j++) | |
| 441 | ✗ | p += coeffs[j] * (uint32_t)decoded[j]; | |
| 442 | ✗ | decoded[j] += p >> qlevel; | |
| 443 | } | ||
| 444 | } | ||
| 445 | |||
| 446 | 10504 | static int decode_subframe_lpc(FLACContext *s, int32_t *decoded, int pred_order, | |
| 447 | int bps) | ||
| 448 | { | ||
| 449 | int i, ret; | ||
| 450 | int coeff_prec, qlevel; | ||
| 451 | int coeffs[32]; | ||
| 452 | |||
| 453 | /* warm up samples */ | ||
| 454 | 58721 | for (i = 0; i < pred_order; i++) { | |
| 455 | 48217 | decoded[i] = get_sbits_long(&s->gb, bps); | |
| 456 | } | ||
| 457 | |||
| 458 | 10504 | coeff_prec = get_bits(&s->gb, 4) + 1; | |
| 459 | 10504 | if (coeff_prec == 16) { | |
| 460 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "invalid coeff precision\n"); | |
| 461 | ✗ | return AVERROR_INVALIDDATA; | |
| 462 | } | ||
| 463 | 10504 | qlevel = get_sbits(&s->gb, 5); | |
| 464 | 10504 | if (qlevel < 0) { | |
| 465 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "qlevel %d not supported, maybe buggy stream\n", | |
| 466 | qlevel); | ||
| 467 | ✗ | return AVERROR_INVALIDDATA; | |
| 468 | } | ||
| 469 | |||
| 470 | 58721 | for (i = 0; i < pred_order; i++) { | |
| 471 | 48217 | coeffs[pred_order - i - 1] = get_sbits(&s->gb, coeff_prec); | |
| 472 | } | ||
| 473 | |||
| 474 | 10504 | if ((ret = decode_residuals(s, decoded, pred_order)) < 0) | |
| 475 | 1 | return ret; | |
| 476 | |||
| 477 | 10503 | if ( ( s->buggy_lpc && s->stream_info.bps <= 16) | |
| 478 | 10503 | || ( !s->buggy_lpc && bps <= 16 | |
| 479 | 7759 | && bps + coeff_prec + av_log2(pred_order) <= 32)) { | |
| 480 | 6181 | s->dsp.lpc16(decoded, coeffs, pred_order, qlevel, s->blocksize); | |
| 481 | } else { | ||
| 482 | 4322 | s->dsp.lpc32(decoded, coeffs, pred_order, qlevel, s->blocksize); | |
| 483 | 4322 | if (s->stream_info.bps <= 16) | |
| 484 | 2830 | lpc_analyze_remodulate(decoded, coeffs, pred_order, qlevel, s->blocksize, bps); | |
| 485 | } | ||
| 486 | |||
| 487 | 10503 | return 0; | |
| 488 | } | ||
| 489 | |||
| 490 | 1 | static int decode_subframe_lpc_33bps(FLACContext *s, int64_t *decoded, | |
| 491 | int32_t *residual, int pred_order) | ||
| 492 | { | ||
| 493 | int i, ret; | ||
| 494 | int coeff_prec, qlevel; | ||
| 495 | int coeffs[32]; | ||
| 496 | |||
| 497 | /* warm up samples */ | ||
| 498 | 9 | for (i = 0; i < pred_order; i++) { | |
| 499 | 8 | decoded[i] = get_sbits64(&s->gb, 33); | |
| 500 | } | ||
| 501 | |||
| 502 | 1 | coeff_prec = get_bits(&s->gb, 4) + 1; | |
| 503 | 1 | if (coeff_prec == 16) { | |
| 504 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "invalid coeff precision\n"); | |
| 505 | ✗ | return AVERROR_INVALIDDATA; | |
| 506 | } | ||
| 507 | 1 | qlevel = get_sbits(&s->gb, 5); | |
| 508 | 1 | if (qlevel < 0) { | |
| 509 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "qlevel %d not supported, maybe buggy stream\n", | |
| 510 | qlevel); | ||
| 511 | ✗ | return AVERROR_INVALIDDATA; | |
| 512 | } | ||
| 513 | |||
| 514 | 9 | for (i = 0; i < pred_order; i++) { | |
| 515 | 8 | coeffs[pred_order - i - 1] = get_sbits(&s->gb, coeff_prec); | |
| 516 | } | ||
| 517 | |||
| 518 | 1 | if ((ret = decode_residuals(s, residual, pred_order)) < 0) | |
| 519 | ✗ | return ret; | |
| 520 | |||
| 521 | 1 | s->dsp.lpc33(decoded, residual, coeffs, pred_order, qlevel, s->blocksize); | |
| 522 | |||
| 523 | 1 | return 0; | |
| 524 | } | ||
| 525 | |||
| 526 | 16668 | static inline int decode_subframe(FLACContext *s, int channel) | |
| 527 | { | ||
| 528 | 16668 | int32_t *decoded = s->decoded[channel]; | |
| 529 | 16668 | int type, wasted = 0; | |
| 530 | 16668 | int bps = s->stream_info.bps; | |
| 531 | int i, ret; | ||
| 532 | |||
| 533 | 16668 | if (channel == 0) { | |
| 534 | 6626 | if (s->ch_mode == FLAC_CHMODE_RIGHT_SIDE) | |
| 535 | 616 | bps++; | |
| 536 | } else { | ||
| 537 | 10042 | if (s->ch_mode == FLAC_CHMODE_LEFT_SIDE || s->ch_mode == FLAC_CHMODE_MID_SIDE) | |
| 538 | 2372 | bps++; | |
| 539 | } | ||
| 540 | |||
| 541 | 16668 | if (get_bits1(&s->gb)) { | |
| 542 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "invalid subframe padding\n"); | |
| 543 | ✗ | return AVERROR_INVALIDDATA; | |
| 544 | } | ||
| 545 | 16668 | type = get_bits(&s->gb, 6); | |
| 546 | |||
| 547 | 16668 | if (get_bits1(&s->gb)) { | |
| 548 | 5292 | int left = get_bits_left(&s->gb); | |
| 549 | 5292 | if ( left <= 0 || | |
| 550 | 5292 | (left < bps && !show_bits_long(&s->gb, left)) || | |
| 551 | 5292 | !show_bits_long(&s->gb, bps-1)) { | |
| 552 | ✗ | av_log(s->avctx, AV_LOG_ERROR, | |
| 553 | "Invalid number of wasted bits > available bits (%d) - left=%d\n", | ||
| 554 | bps, left); | ||
| 555 | ✗ | return AVERROR_INVALIDDATA; | |
| 556 | } | ||
| 557 | 5292 | wasted = 1 + get_unary(&s->gb, 1, get_bits_left(&s->gb)); | |
| 558 | 5292 | bps -= wasted; | |
| 559 | } | ||
| 560 | |||
| 561 | //FIXME use av_log2 for types | ||
| 562 | 16668 | if (type == 0) { | |
| 563 | 5699 | if (bps < 33) { | |
| 564 | 5699 | int32_t tmp = get_sbits_long(&s->gb, bps); | |
| 565 | 36060355 | for (i = 0; i < s->blocksize; i++) | |
| 566 | 36054656 | decoded[i] = tmp; | |
| 567 | } else { | ||
| 568 | ✗ | int64_t tmp = get_sbits64(&s->gb, 33); | |
| 569 | ✗ | for (i = 0; i < s->blocksize; i++) | |
| 570 | ✗ | s->decoded_33bps[i] = tmp; | |
| 571 | } | ||
| 572 | 10969 | } else if (type == 1) { | |
| 573 | 1 | if (bps < 33) { | |
| 574 | ✗ | for (i = 0; i < s->blocksize; i++) | |
| 575 | ✗ | decoded[i] = get_sbits_long(&s->gb, bps); | |
| 576 | } else { | ||
| 577 | 4097 | for (i = 0; i < s->blocksize; i++) | |
| 578 | 4096 | s->decoded_33bps[i] = get_sbits64(&s->gb, 33); | |
| 579 | } | ||
| 580 | 11431 | } else if ((type >= 8) && (type <= 12)) { | |
| 581 | 463 | int order = type & ~0x8; | |
| 582 | 463 | if (bps < 33) { | |
| 583 | 463 | if (bps + order <= 32) { | |
| 584 | 463 | if ((ret = decode_subframe_fixed(s, decoded, order, bps)) < 0) | |
| 585 | ✗ | return ret; | |
| 586 | } else { | ||
| 587 | ✗ | if ((ret = decode_subframe_fixed_wide(s, decoded, order, bps)) < 0) | |
| 588 | ✗ | return ret; | |
| 589 | } | ||
| 590 | } else { | ||
| 591 | ✗ | if ((ret = decode_subframe_fixed_33bps(s, s->decoded_33bps, decoded, order)) < 0) | |
| 592 | ✗ | return ret; | |
| 593 | } | ||
| 594 | 10505 | } else if (type >= 32) { | |
| 595 | 10505 | if (bps < 33) { | |
| 596 | 10504 | if ((ret = decode_subframe_lpc(s, decoded, (type & ~0x20)+1, bps)) < 0) | |
| 597 | 1 | return ret; | |
| 598 | } else { | ||
| 599 | 1 | if ((ret = decode_subframe_lpc_33bps(s, s->decoded_33bps, decoded, (type & ~0x20)+1)) < 0) | |
| 600 | ✗ | return ret; | |
| 601 | } | ||
| 602 | } else { | ||
| 603 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "invalid coding type\n"); | |
| 604 | ✗ | return AVERROR_INVALIDDATA; | |
| 605 | } | ||
| 606 | |||
| 607 | 16667 | if (wasted) { | |
| 608 | 5292 | if (wasted+bps == 33) { | |
| 609 | 7 | s->dsp.wasted33(s->decoded_33bps, decoded, wasted, s->blocksize); | |
| 610 | 5285 | } else if (wasted < 32) { | |
| 611 | 5285 | s->dsp.wasted32(decoded, wasted, s->blocksize); | |
| 612 | } | ||
| 613 | } | ||
| 614 | |||
| 615 | 16667 | return 0; | |
| 616 | } | ||
| 617 | |||
| 618 | 6683 | static int decode_frame_header(FLACContext *s) | |
| 619 | { | ||
| 620 | int ret; | ||
| 621 | 6683 | GetBitContext *gb = &s->gb; | |
| 622 | FLACFrameInfo fi; | ||
| 623 | |||
| 624 | 6683 | if ((ret = ff_flac_decode_frame_header(s->avctx, gb, &fi, 0)) < 0) { | |
| 625 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "invalid frame header\n"); | |
| 626 | ✗ | return ret; | |
| 627 | } | ||
| 628 | |||
| 629 | 6683 | if ( s->stream_info.channels | |
| 630 | 6667 | && fi.channels != s->stream_info.channels | |
| 631 | ✗ | && s->got_streaminfo) { | |
| 632 | ✗ | s->stream_info.channels = fi.channels; | |
| 633 | ✗ | ff_flac_set_channel_layout(s->avctx, fi.channels); | |
| 634 | ✗ | ret = allocate_buffers(s); | |
| 635 | ✗ | if (ret < 0) | |
| 636 | ✗ | return ret; | |
| 637 | } | ||
| 638 | 6683 | s->stream_info.channels = fi.channels; | |
| 639 | 6683 | ff_flac_set_channel_layout(s->avctx, fi.channels); | |
| 640 | 6683 | s->ch_mode = fi.ch_mode; | |
| 641 | |||
| 642 | 6683 | if (!s->stream_info.bps && !fi.bps) { | |
| 643 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "bps not found in STREAMINFO or frame header\n"); | |
| 644 | ✗ | return AVERROR_INVALIDDATA; | |
| 645 | } | ||
| 646 | 6683 | if (!fi.bps) { | |
| 647 | ✗ | fi.bps = s->stream_info.bps; | |
| 648 | 6683 | } else if (s->stream_info.bps && fi.bps != s->stream_info.bps) { | |
| 649 | 44 | av_log(s->avctx, AV_LOG_ERROR, "switching bps mid-stream is not " | |
| 650 | "supported\n"); | ||
| 651 | 44 | return AVERROR_INVALIDDATA; | |
| 652 | } | ||
| 653 | |||
| 654 | 6639 | if (!s->stream_info.bps) { | |
| 655 | 16 | s->stream_info.bps = s->avctx->bits_per_raw_sample = fi.bps; | |
| 656 | 16 | flac_set_bps(s); | |
| 657 | } | ||
| 658 | |||
| 659 | 6639 | if (!s->stream_info.max_blocksize) | |
| 660 | 16 | s->stream_info.max_blocksize = FLAC_MAX_BLOCKSIZE; | |
| 661 | 6639 | if (fi.blocksize > s->stream_info.max_blocksize) { | |
| 662 | 13 | av_log(s->avctx, AV_LOG_ERROR, "blocksize %d > %d\n", fi.blocksize, | |
| 663 | s->stream_info.max_blocksize); | ||
| 664 | 13 | return AVERROR_INVALIDDATA; | |
| 665 | } | ||
| 666 | 6626 | s->blocksize = fi.blocksize; | |
| 667 | |||
| 668 | 6626 | if (!s->stream_info.samplerate && !fi.samplerate) { | |
| 669 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "sample rate not found in STREAMINFO" | |
| 670 | " or frame header\n"); | ||
| 671 | ✗ | return AVERROR_INVALIDDATA; | |
| 672 | } | ||
| 673 | 6626 | if (fi.samplerate == 0) | |
| 674 | ✗ | fi.samplerate = s->stream_info.samplerate; | |
| 675 | 6626 | s->stream_info.samplerate = s->avctx->sample_rate = fi.samplerate; | |
| 676 | |||
| 677 | 6626 | if (!s->got_streaminfo) { | |
| 678 | 16 | ret = allocate_buffers(s); | |
| 679 | 16 | if (ret < 0) | |
| 680 | ✗ | return ret; | |
| 681 | 16 | s->got_streaminfo = 1; | |
| 682 | 16 | dump_headers(s->avctx, &s->stream_info); | |
| 683 | } | ||
| 684 | 6626 | ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt, | |
| 685 | s->stream_info.channels); | ||
| 686 | |||
| 687 | // dump_headers(s->avctx, &s->stream_info); | ||
| 688 | |||
| 689 | 6626 | return 0; | |
| 690 | } | ||
| 691 | |||
| 692 | 6626 | static int decode_subframes(FLACContext *s) | |
| 693 | { | ||
| 694 | int i, ret; | ||
| 695 | 6626 | GetBitContext *gb = &s->gb; | |
| 696 | |||
| 697 | /* subframes */ | ||
| 698 | 23293 | for (i = 0; i < s->stream_info.channels; i++) { | |
| 699 | 16668 | if ((ret = decode_subframe(s, i)) < 0) | |
| 700 | 1 | return ret; | |
| 701 | } | ||
| 702 | |||
| 703 | 6625 | align_get_bits(gb); | |
| 704 | |||
| 705 | /* frame footer */ | ||
| 706 | 6625 | skip_bits(gb, 16); /* data crc */ | |
| 707 | |||
| 708 | 6625 | return 0; | |
| 709 | } | ||
| 710 | |||
| 711 | 9 | static void decorrelate_33bps(int ch_mode, int32_t **decoded, int64_t *decoded_33bps, int len) | |
| 712 | { | ||
| 713 | int i; | ||
| 714 | 9 | if (ch_mode == FLAC_CHMODE_LEFT_SIDE ) { | |
| 715 | ✗ | for (i = 0; i < len; i++) | |
| 716 | ✗ | decoded[1][i] = decoded[0][i] - (uint64_t)decoded_33bps[i]; | |
| 717 | 9 | } else if (ch_mode == FLAC_CHMODE_RIGHT_SIDE ) { | |
| 718 | 31853 | for (i = 0; i < len; i++) | |
| 719 | 31845 | decoded[0][i] = decoded[1][i] + (uint64_t)decoded_33bps[i]; | |
| 720 | 1 | } else if (ch_mode == FLAC_CHMODE_MID_SIDE ) { | |
| 721 | 4097 | for (i = 0; i < len; i++) { | |
| 722 | 4096 | uint64_t a = decoded[0][i]; | |
| 723 | 4096 | int64_t b = decoded_33bps[i]; | |
| 724 | 4096 | a -= b >> 1; | |
| 725 | 4096 | decoded[0][i] = (a + b); | |
| 726 | 4096 | decoded[1][i] = a; | |
| 727 | } | ||
| 728 | } | ||
| 729 | 9 | } | |
| 730 | |||
| 731 | 6683 | static int flac_decode_frame(AVCodecContext *avctx, AVFrame *frame, | |
| 732 | int *got_frame_ptr, AVPacket *avpkt) | ||
| 733 | { | ||
| 734 | 6683 | const uint8_t *buf = avpkt->data; | |
| 735 | 6683 | int buf_size = avpkt->size; | |
| 736 | 6683 | FLACContext *s = avctx->priv_data; | |
| 737 | const uint8_t *streaminfo; | ||
| 738 | size_t streaminfo_size; | ||
| 739 | 6683 | int bytes_read = 0; | |
| 740 | int ret; | ||
| 741 | |||
| 742 | 6683 | *got_frame_ptr = 0; | |
| 743 | |||
| 744 | 6683 | if (!ff_thread_can_start_frame(avctx)) { | |
| 745 | ✗ | av_log(avctx, AV_LOG_ERROR, "frame threads need one FLAC frame per packet\n"); | |
| 746 | ✗ | return AVERROR_PATCHWELCOME; | |
| 747 | } | ||
| 748 | |||
| 749 | 6683 | streaminfo = av_packet_get_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA, | |
| 750 | &streaminfo_size); | ||
| 751 | 6683 | if (streaminfo) { | |
| 752 | 8 | if (streaminfo_size >= 8 + FLAC_STREAMINFO_SIZE && | |
| 753 | ✗ | AV_RB32(streaminfo) == MKBETAG('f','L','a','C')) | |
| 754 | ✗ | streaminfo += 8; | |
| 755 | 8 | else if (streaminfo_size != FLAC_STREAMINFO_SIZE) | |
| 756 | ✗ | return AVERROR_INVALIDDATA; | |
| 757 | 8 | if ((ret = apply_streaminfo(s, streaminfo)) < 0) | |
| 758 | ✗ | return ret; | |
| 759 | 8 | s->streaminfo_serial++; | |
| 760 | } | ||
| 761 | |||
| 762 | 6683 | if (buf_size > 5 && !memcmp(buf, "\177FLAC", 5)) { | |
| 763 | ✗ | av_log(s->avctx, AV_LOG_DEBUG, "skipping flac header packet 1\n"); | |
| 764 | ✗ | return buf_size; | |
| 765 | } | ||
| 766 | |||
| 767 | 6683 | if (buf_size > 0 && (*buf & 0x7F) == FLAC_METADATA_TYPE_VORBIS_COMMENT) { | |
| 768 | ✗ | av_log(s->avctx, AV_LOG_DEBUG, "skipping vorbis comment\n"); | |
| 769 | ✗ | return buf_size; | |
| 770 | } | ||
| 771 | |||
| 772 | /* check that there is at least the smallest decodable amount of data. | ||
| 773 | this amount corresponds to the smallest valid FLAC frame possible. | ||
| 774 | FF F8 69 02 00 00 9A 00 00 34 */ | ||
| 775 | 6683 | if (buf_size < FLAC_MIN_FRAME_SIZE) | |
| 776 | ✗ | return buf_size; | |
| 777 | |||
| 778 | /* check for inline header */ | ||
| 779 | 6683 | if (AV_RB32(buf) == MKBETAG('f','L','a','C')) { | |
| 780 | ✗ | if (!s->got_streaminfo && (ret = parse_streaminfo(s, buf, buf_size))) { | |
| 781 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "invalid header\n"); | |
| 782 | ✗ | return ret; | |
| 783 | } | ||
| 784 | ✗ | return get_metadata_size(buf, buf_size); | |
| 785 | } | ||
| 786 | |||
| 787 | /* decode frame */ | ||
| 788 | 6683 | if ((ret = init_get_bits8(&s->gb, buf, buf_size)) < 0) | |
| 789 | ✗ | return ret; | |
| 790 | 6683 | if ((ret = decode_frame_header(s)) < 0) { | |
| 791 | 57 | av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n"); | |
| 792 | 57 | return ret; | |
| 793 | } | ||
| 794 | |||
| 795 | /* get output buffer */ | ||
| 796 | 6626 | frame->nb_samples = s->blocksize; | |
| 797 | 6626 | if ((ret = ff_thread_get_buffer(avctx, frame, 0)) < 0) | |
| 798 | ✗ | return ret; | |
| 799 | |||
| 800 | 6626 | ff_thread_finish_setup(avctx); | |
| 801 | |||
| 802 | 6626 | if ((ret = decode_subframes(s)) < 0) { | |
| 803 | 1 | av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n"); | |
| 804 | 1 | return ret; | |
| 805 | } | ||
| 806 | 6625 | bytes_read = get_bits_count(&s->gb)/8; | |
| 807 | |||
| 808 | 6625 | if ((s->avctx->err_recognition & (AV_EF_CRCCHECK|AV_EF_COMPLIANT)) && | |
| 809 | ✗ | av_crc(av_crc_get_table(AV_CRC_16_ANSI), | |
| 810 | 0, buf, bytes_read)) { | ||
| 811 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "CRC error at PTS %"PRId64"\n", avpkt->pts); | |
| 812 | ✗ | if (s->avctx->err_recognition & AV_EF_EXPLODE) | |
| 813 | ✗ | return AVERROR_INVALIDDATA; | |
| 814 | } | ||
| 815 | |||
| 816 | 6625 | if (s->stream_info.bps == 32 && s->ch_mode > 0) { | |
| 817 | 9 | decorrelate_33bps(s->ch_mode, s->decoded, s->decoded_33bps, s->blocksize); | |
| 818 | 9 | s->dsp.decorrelate[0](frame->data, s->decoded, s->stream_info.channels, | |
| 819 | s->blocksize, s->sample_shift); | ||
| 820 | } else { | ||
| 821 | 6616 | s->dsp.decorrelate[s->ch_mode](frame->data, s->decoded, | |
| 822 | s->stream_info.channels, | ||
| 823 | s->blocksize, s->sample_shift); | ||
| 824 | } | ||
| 825 | |||
| 826 | 6625 | if (bytes_read > buf_size) { | |
| 827 | ✗ | av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", bytes_read - buf_size); | |
| 828 | ✗ | return AVERROR_INVALIDDATA; | |
| 829 | } | ||
| 830 | 6625 | if (bytes_read < buf_size) { | |
| 831 | ✗ | av_log(s->avctx, AV_LOG_DEBUG, "underread: %d orig size: %d\n", | |
| 832 | buf_size - bytes_read, buf_size); | ||
| 833 | } | ||
| 834 | |||
| 835 | 6625 | *got_frame_ptr = 1; | |
| 836 | |||
| 837 | 6625 | return bytes_read; | |
| 838 | } | ||
| 839 | |||
| 840 | #if HAVE_THREADS | ||
| 841 | /** | ||
| 842 | * Hand the stream parameters on to the next frame thread, so that a STREAMINFO | ||
| 843 | * taken from packet side data holds for the frames after the one carrying it. | ||
| 844 | */ | ||
| 845 | 439 | static int flac_update_thread_context(AVCodecContext *dst, | |
| 846 | const AVCodecContext *src) | ||
| 847 | { | ||
| 848 | 439 | FLACContext *s = dst->priv_data; | |
| 849 | 439 | const FLACContext *s1 = src->priv_data; | |
| 850 | |||
| 851 | 439 | if (s == s1 || s->streaminfo_serial == s1->streaminfo_serial) | |
| 852 | 437 | return 0; | |
| 853 | |||
| 854 | 2 | s->streaminfo_serial = s1->streaminfo_serial; | |
| 855 | 2 | s->stream_info = s1->stream_info; | |
| 856 | |||
| 857 | 2 | return setup_streaminfo(s); | |
| 858 | } | ||
| 859 | #endif | ||
| 860 | |||
| 861 | 367 | static av_cold int flac_decode_close(AVCodecContext *avctx) | |
| 862 | { | ||
| 863 | 367 | FLACContext *s = avctx->priv_data; | |
| 864 | |||
| 865 | 367 | av_freep(&s->decoded_buffer); | |
| 866 | 367 | av_freep(&s->decoded_buffer_33bps); | |
| 867 | |||
| 868 | 367 | return 0; | |
| 869 | } | ||
| 870 | |||
| 871 | static const AVOption options[] = { | ||
| 872 | { "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 }, | ||
| 873 | { NULL }, | ||
| 874 | }; | ||
| 875 | |||
| 876 | static const AVClass flac_decoder_class = { | ||
| 877 | .class_name = "FLAC decoder", | ||
| 878 | .item_name = av_default_item_name, | ||
| 879 | .option = options, | ||
| 880 | .version = LIBAVUTIL_VERSION_INT, | ||
| 881 | }; | ||
| 882 | |||
| 883 | const FFCodec ff_flac_decoder = { | ||
| 884 | .p.name = "flac", | ||
| 885 | CODEC_LONG_NAME("FLAC (Free Lossless Audio Codec)"), | ||
| 886 | .p.type = AVMEDIA_TYPE_AUDIO, | ||
| 887 | .p.id = AV_CODEC_ID_FLAC, | ||
| 888 | .priv_data_size = sizeof(FLACContext), | ||
| 889 | .init = flac_decode_init, | ||
| 890 | .close = flac_decode_close, | ||
| 891 | FF_CODEC_DECODE_CB(flac_decode_frame), | ||
| 892 | UPDATE_THREAD_CONTEXT(flac_update_thread_context), | ||
| 893 | .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF | | ||
| 894 | AV_CODEC_CAP_DR1 | | ||
| 895 | AV_CODEC_CAP_FRAME_THREADS, | ||
| 896 | CODEC_SAMPLEFMTS(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16P, | ||
| 897 | AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32P), | ||
| 898 | .p.priv_class = &flac_decoder_class, | ||
| 899 | }; | ||
| 900 |