FFmpeg
flacdec.c
Go to the documentation of this file.
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 "codec_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 "flac_parse.h"
48 #include "thread.h"
49 #include "unary.h"
50 
51 
52 typedef struct FLACContext {
53  AVClass *class;
55 
56  AVCodecContext *avctx; ///< parent AVCodecContext
57  GetBitContext gb; ///< GetBitContext initialized to start at the current frame
58 
59  int blocksize; ///< number of samples in the current frame
60  int sample_shift; ///< shift required to make output samples 16-bit or 32-bit
61  int ch_mode; ///< channel decorrelation type in the current frame
62  int got_streaminfo; ///< indicates if the STREAMINFO has been read
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
70  int buggy_lpc; ///< use workaround for old lavc encoded files
71 
73 } FLACContext;
74 
75 static int allocate_buffers(FLACContext *s);
76 
77 static void flac_set_bps(FLACContext *s)
78 {
79  enum AVSampleFormat req = s->avctx->request_sample_fmt;
80  int need32 = s->stream_info.bps > 16;
81  int want32 = av_get_bytes_per_sample(req) > 2;
83 
84  if (need32 || want32) {
85  if (planar)
86  s->avctx->sample_fmt = AV_SAMPLE_FMT_S32P;
87  else
88  s->avctx->sample_fmt = AV_SAMPLE_FMT_S32;
89  s->sample_shift = 32 - s->stream_info.bps;
90  } else {
91  if (planar)
92  s->avctx->sample_fmt = AV_SAMPLE_FMT_S16P;
93  else
94  s->avctx->sample_fmt = AV_SAMPLE_FMT_S16;
95  s->sample_shift = 16 - s->stream_info.bps;
96  }
97 }
98 
100 {
101  uint8_t *streaminfo;
102  int ret;
103  FLACContext *s = avctx->priv_data;
104  s->avctx = avctx;
105 
106  /* for now, the raw FLAC header is allowed to be passed to the decoder as
107  frame data instead of extradata. */
108  if (!avctx->extradata)
109  return 0;
110 
111  if (!ff_flac_is_extradata_valid(avctx, &streaminfo))
112  return AVERROR_INVALIDDATA;
113 
114  /* initialize based on the demuxer-supplied streamdata header */
115  ret = ff_flac_parse_streaminfo(avctx, &s->stream_info, streaminfo);
116  if (ret < 0)
117  return ret;
119  if (ret < 0)
120  return ret;
121  flac_set_bps(s);
122  ff_flacdsp_init(&s->dsp, avctx->sample_fmt,
123  s->stream_info.channels);
124  s->got_streaminfo = 1;
125 
126  return 0;
127 }
128 
130 {
131  av_log(avctx, AV_LOG_DEBUG, " Max Blocksize: %d\n", s->max_blocksize);
132  av_log(avctx, AV_LOG_DEBUG, " Max Framesize: %d\n", s->max_framesize);
133  av_log(avctx, AV_LOG_DEBUG, " Samplerate: %d\n", s->samplerate);
134  av_log(avctx, AV_LOG_DEBUG, " Channels: %d\n", s->channels);
135  av_log(avctx, AV_LOG_DEBUG, " Bits: %d\n", s->bps);
136 }
137 
139 {
140  int buf_size;
141  int ret;
142 
143  av_assert0(s->stream_info.max_blocksize);
144 
145  buf_size = av_samples_get_buffer_size(NULL, s->stream_info.channels,
146  s->stream_info.max_blocksize,
147  AV_SAMPLE_FMT_S32P, 0);
148  if (buf_size < 0)
149  return buf_size;
150 
151  av_fast_malloc(&s->decoded_buffer, &s->decoded_buffer_size, buf_size);
152  if (!s->decoded_buffer)
153  return AVERROR(ENOMEM);
154 
155  ret = av_samples_fill_arrays((uint8_t **)s->decoded, NULL,
156  s->decoded_buffer,
157  s->stream_info.channels,
158  s->stream_info.max_blocksize,
159  AV_SAMPLE_FMT_S32P, 0);
160  if (ret >= 0 && s->stream_info.bps == 32 && s->stream_info.channels == 2) {
161  buf_size = av_samples_get_buffer_size(NULL, 1,
162  s->stream_info.max_blocksize,
163  AV_SAMPLE_FMT_S64P, 0);
164  if (buf_size < 0)
165  return buf_size;
166 
167  av_fast_malloc(&s->decoded_buffer_33bps, &s->decoded_buffer_size_33bps, buf_size);
168  if (!s->decoded_buffer_33bps)
169  return AVERROR(ENOMEM);
170 
171  ret = av_samples_fill_arrays((uint8_t **)&s->decoded_33bps, NULL,
172  s->decoded_buffer_33bps,
173  1,
174  s->stream_info.max_blocksize,
175  AV_SAMPLE_FMT_S64P, 0);
176 
177  }
178  return ret < 0 ? ret : 0;
179 }
180 
181 /**
182  * Parse the STREAMINFO from an inline header.
183  * @param s the flac decoding context
184  * @param buf input buffer, starting with the "fLaC" marker
185  * @param buf_size buffer size
186  * @return non-zero if metadata is invalid
187  */
188 static int parse_streaminfo(FLACContext *s, const uint8_t *buf, int buf_size)
189 {
190  int metadata_type, metadata_size, ret;
191 
192  if (buf_size < FLAC_STREAMINFO_SIZE+8) {
193  /* need more data */
194  return 0;
195  }
196  flac_parse_block_header(&buf[4], NULL, &metadata_type, &metadata_size);
197  if (metadata_type != FLAC_METADATA_TYPE_STREAMINFO ||
198  metadata_size != FLAC_STREAMINFO_SIZE) {
199  return AVERROR_INVALIDDATA;
200  }
201  ret = ff_flac_parse_streaminfo(s->avctx, &s->stream_info, &buf[8]);
202  if (ret < 0)
203  return ret;
205  if (ret < 0)
206  return ret;
207  flac_set_bps(s);
208  ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt,
209  s->stream_info.channels);
210  s->got_streaminfo = 1;
211 
212  return 0;
213 }
214 
215 /**
216  * Determine the size of an inline header.
217  * @param buf input buffer, starting with the "fLaC" marker
218  * @param buf_size buffer size
219  * @return number of bytes in the header, or 0 if more data is needed
220  */
221 static int get_metadata_size(const uint8_t *buf, int buf_size)
222 {
223  int metadata_last, metadata_size;
224  const uint8_t *buf_end = buf + buf_size;
225 
226  buf += 4;
227  do {
228  if (buf_end - buf < 4)
229  return AVERROR_INVALIDDATA;
230  flac_parse_block_header(buf, &metadata_last, NULL, &metadata_size);
231  buf += 4;
232  if (buf_end - buf < metadata_size) {
233  /* need more data in order to read the complete header */
234  return AVERROR_INVALIDDATA;
235  }
236  buf += metadata_size;
237  } while (!metadata_last);
238 
239  return buf_size - (buf_end - buf);
240 }
241 
242 static int decode_residuals(FLACContext *s, int32_t *decoded, int pred_order)
243 {
244  GetBitContext gb = s->gb;
245  int i, tmp, partition, method_type, rice_order;
246  int rice_bits, rice_esc;
247  int samples;
248 
249  method_type = get_bits(&gb, 2);
250  rice_order = get_bits(&gb, 4);
251 
252  samples = s->blocksize >> rice_order;
253  rice_bits = 4 + method_type;
254  rice_esc = (1 << rice_bits) - 1;
255 
256  decoded += pred_order;
257  i = pred_order;
258 
259  if (method_type > 1) {
260  av_log(s->avctx, AV_LOG_ERROR, "illegal residual coding method %d\n",
261  method_type);
262  return AVERROR_INVALIDDATA;
263  }
264 
265  if (samples << rice_order != s->blocksize) {
266  av_log(s->avctx, AV_LOG_ERROR, "invalid rice order: %i blocksize %i\n",
267  rice_order, s->blocksize);
268  return AVERROR_INVALIDDATA;
269  }
270 
271  if (pred_order > samples) {
272  av_log(s->avctx, AV_LOG_ERROR, "invalid predictor order: %i > %i\n",
273  pred_order, samples);
274  return AVERROR_INVALIDDATA;
275  }
276 
277  for (partition = 0; partition < (1 << rice_order); partition++) {
278  tmp = get_bits(&gb, rice_bits);
279  if (tmp == rice_esc) {
280  tmp = get_bits(&gb, 5);
281  for (; i < samples; i++)
282  *decoded++ = get_sbits_long(&gb, tmp);
283  } else {
284  int real_limit = (tmp > 1) ? (INT_MAX >> (tmp - 1)) + 2 : INT_MAX;
285  for (; i < samples; i++) {
286  int v = get_sr_golomb_flac(&gb, tmp, real_limit, 1);
287  if (v == 0x80000000){
288  av_log(s->avctx, AV_LOG_ERROR, "invalid residual\n");
289  return AVERROR_INVALIDDATA;
290  }
291 
292  *decoded++ = v;
293  }
294  }
295  i= 0;
296  }
297 
298  s->gb = gb;
299 
300  return 0;
301 }
302 
304  int pred_order, int bps)
305 {
306  const int blocksize = s->blocksize;
307  unsigned av_uninit(a), av_uninit(b), av_uninit(c), av_uninit(d);
308  int i;
309  int ret;
310 
311  /* warm up samples */
312  for (i = 0; i < pred_order; i++) {
313  decoded[i] = get_sbits_long(&s->gb, bps);
314  }
315 
316  if ((ret = decode_residuals(s, decoded, pred_order)) < 0)
317  return ret;
318 
319  if (pred_order > 0)
320  a = decoded[pred_order-1];
321  if (pred_order > 1)
322  b = a - decoded[pred_order-2];
323  if (pred_order > 2)
324  c = b - decoded[pred_order-2] + decoded[pred_order-3];
325  if (pred_order > 3)
326  d = c - decoded[pred_order-2] + 2U*decoded[pred_order-3] - decoded[pred_order-4];
327 
328  switch (pred_order) {
329  case 0:
330  break;
331  case 1:
332  for (i = pred_order; i < blocksize; i++)
333  decoded[i] = a += decoded[i];
334  break;
335  case 2:
336  for (i = pred_order; i < blocksize; i++)
337  decoded[i] = a += b += decoded[i];
338  break;
339  case 3:
340  for (i = pred_order; i < blocksize; i++)
341  decoded[i] = a += b += c += decoded[i];
342  break;
343  case 4:
344  for (i = pred_order; i < blocksize; i++)
345  decoded[i] = a += b += c += d += decoded[i];
346  break;
347  default:
348  av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order);
349  return AVERROR_INVALIDDATA;
350  }
351 
352  return 0;
353 }
354 
355 #define DECODER_SUBFRAME_FIXED_WIDE(residual) { \
356  const int blocksize = s->blocksize; \
357  int ret; \
358  \
359  if ((ret = decode_residuals(s, residual, pred_order)) < 0) \
360  return ret; \
361  \
362  switch (pred_order) { \
363  case 0: \
364  for (int i = pred_order; i < blocksize; i++) \
365  decoded[i] = residual[i]; \
366  break; \
367  case 1: \
368  for (int i = pred_order; i < blocksize; i++) \
369  decoded[i] = (uint64_t)residual[i] + (uint64_t)decoded[i-1];\
370  break; \
371  case 2: \
372  for (int i = pred_order; i < blocksize; i++) \
373  decoded[i] = (uint64_t)residual[i] + 2*(uint64_t)decoded[i-1] - (uint64_t)decoded[i-2]; \
374  break; \
375  case 3: \
376  for (int i = pred_order; i < blocksize; i++) \
377  decoded[i] = (uint64_t)residual[i] + 3*(uint64_t)decoded[i-1] - 3*(uint64_t)decoded[i-2] + (uint64_t)decoded[i-3]; \
378  break; \
379  case 4: \
380  for (int i = pred_order; i < blocksize; i++) \
381  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]; \
382  break; \
383  default: \
384  av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order); \
385  return AVERROR_INVALIDDATA; \
386  } \
387  return 0; \
388 }
389 
391  int pred_order, int bps)
392 {
393  /* warm up samples */
394  for (int i = 0; i < pred_order; i++) {
395  decoded[i] = get_sbits_long(&s->gb, bps);
396  }
398 }
399 
400 
401 static int decode_subframe_fixed_33bps(FLACContext *s, int64_t *decoded,
402  int32_t *residual, int pred_order)
403 {
404  /* warm up samples */ \
405  for (int i = 0; i < pred_order; i++) { \
406  decoded[i] = get_sbits64(&s->gb, 33); \
407  } \
408  DECODER_SUBFRAME_FIXED_WIDE(residual);
409 }
410 
411 static void lpc_analyze_remodulate(SUINT32 *decoded, const int coeffs[32],
412  int order, int qlevel, int len, int bps)
413 {
414  int i, j;
415  int ebps = 1 << (bps-1);
416  unsigned sigma = 0;
417 
418  for (i = order; i < len; i++)
419  sigma |= decoded[i] + ebps;
420 
421  if (sigma < 2*ebps)
422  return;
423 
424  for (i = len - 1; i >= order; i--) {
425  int64_t p = 0;
426  for (j = 0; j < order; j++)
427  p += coeffs[j] * (int64_t)(int32_t)decoded[i-order+j];
428  decoded[i] -= p >> qlevel;
429  }
430  for (i = order; i < len; i++, decoded++) {
431  int32_t p = 0;
432  for (j = 0; j < order; j++)
433  p += coeffs[j] * (uint32_t)decoded[j];
434  decoded[j] += p >> qlevel;
435  }
436 }
437 
438 static int decode_subframe_lpc(FLACContext *s, int32_t *decoded, int pred_order,
439  int bps)
440 {
441  int i, ret;
442  int coeff_prec, qlevel;
443  int coeffs[32];
444 
445  /* warm up samples */
446  for (i = 0; i < pred_order; i++) {
447  decoded[i] = get_sbits_long(&s->gb, bps);
448  }
449 
450  coeff_prec = get_bits(&s->gb, 4) + 1;
451  if (coeff_prec == 16) {
452  av_log(s->avctx, AV_LOG_ERROR, "invalid coeff precision\n");
453  return AVERROR_INVALIDDATA;
454  }
455  qlevel = get_sbits(&s->gb, 5);
456  if (qlevel < 0) {
457  av_log(s->avctx, AV_LOG_ERROR, "qlevel %d not supported, maybe buggy stream\n",
458  qlevel);
459  return AVERROR_INVALIDDATA;
460  }
461 
462  for (i = 0; i < pred_order; i++) {
463  coeffs[pred_order - i - 1] = get_sbits(&s->gb, coeff_prec);
464  }
465 
466  if ((ret = decode_residuals(s, decoded, pred_order)) < 0)
467  return ret;
468 
469  if ( ( s->buggy_lpc && s->stream_info.bps <= 16)
470  || ( !s->buggy_lpc && bps <= 16
471  && bps + coeff_prec + av_log2(pred_order) <= 32)) {
472  s->dsp.lpc16(decoded, coeffs, pred_order, qlevel, s->blocksize);
473  } else {
474  s->dsp.lpc32(decoded, coeffs, pred_order, qlevel, s->blocksize);
475  if (s->stream_info.bps <= 16)
476  lpc_analyze_remodulate(decoded, coeffs, pred_order, qlevel, s->blocksize, bps);
477  }
478 
479  return 0;
480 }
481 
482 static int decode_subframe_lpc_33bps(FLACContext *s, int64_t *decoded,
483  int32_t *residual, int pred_order)
484 {
485  int i, j, ret;
486  int coeff_prec, qlevel;
487  int coeffs[32];
488 
489  /* warm up samples */
490  for (i = 0; i < pred_order; i++) {
491  decoded[i] = get_sbits64(&s->gb, 33);
492  }
493 
494  coeff_prec = get_bits(&s->gb, 4) + 1;
495  if (coeff_prec == 16) {
496  av_log(s->avctx, AV_LOG_ERROR, "invalid coeff precision\n");
497  return AVERROR_INVALIDDATA;
498  }
499  qlevel = get_sbits(&s->gb, 5);
500  if (qlevel < 0) {
501  av_log(s->avctx, AV_LOG_ERROR, "qlevel %d not supported, maybe buggy stream\n",
502  qlevel);
503  return AVERROR_INVALIDDATA;
504  }
505 
506  for (i = 0; i < pred_order; i++) {
507  coeffs[pred_order - i - 1] = get_sbits(&s->gb, coeff_prec);
508  }
509 
510  if ((ret = decode_residuals(s, residual, pred_order)) < 0)
511  return ret;
512 
513  for (i = pred_order; i < s->blocksize; i++, decoded++) {
514  int64_t sum = 0;
515  for (j = 0; j < pred_order; j++)
516  sum += (int64_t)coeffs[j] * (uint64_t)decoded[j];
517  decoded[j] = residual[i] + (sum >> qlevel);
518  }
519 
520  return 0;
521 }
522 
523 static inline int decode_subframe(FLACContext *s, int channel)
524 {
525  int32_t *decoded = s->decoded[channel];
526  int type, wasted = 0;
527  int bps = s->stream_info.bps;
528  int i, ret;
529 
530  if (channel == 0) {
531  if (s->ch_mode == FLAC_CHMODE_RIGHT_SIDE)
532  bps++;
533  } else {
534  if (s->ch_mode == FLAC_CHMODE_LEFT_SIDE || s->ch_mode == FLAC_CHMODE_MID_SIDE)
535  bps++;
536  }
537 
538  if (get_bits1(&s->gb)) {
539  av_log(s->avctx, AV_LOG_ERROR, "invalid subframe padding\n");
540  return AVERROR_INVALIDDATA;
541  }
542  type = get_bits(&s->gb, 6);
543 
544  if (get_bits1(&s->gb)) {
545  int left = get_bits_left(&s->gb);
546  if ( left <= 0 ||
547  (left < bps && !show_bits_long(&s->gb, left)) ||
548  !show_bits_long(&s->gb, bps-1)) {
549  av_log(s->avctx, AV_LOG_ERROR,
550  "Invalid number of wasted bits > available bits (%d) - left=%d\n",
551  bps, left);
552  return AVERROR_INVALIDDATA;
553  }
554  wasted = 1 + get_unary(&s->gb, 1, get_bits_left(&s->gb));
555  bps -= wasted;
556  }
557 
558 //FIXME use av_log2 for types
559  if (type == 0) {
560  if (bps < 33) {
561  int32_t tmp = get_sbits_long(&s->gb, bps);
562  for (i = 0; i < s->blocksize; i++)
563  decoded[i] = tmp;
564  } else {
565  int64_t tmp = get_sbits64(&s->gb, 33);
566  for (i = 0; i < s->blocksize; i++)
567  s->decoded_33bps[i] = tmp;
568  }
569  } else if (type == 1) {
570  if (bps < 33) {
571  for (i = 0; i < s->blocksize; i++)
572  decoded[i] = get_sbits_long(&s->gb, bps);
573  } else {
574  for (i = 0; i < s->blocksize; i++)
575  s->decoded_33bps[i] = get_sbits64(&s->gb, 33);
576  }
577  } else if ((type >= 8) && (type <= 12)) {
578  int order = type & ~0x8;
579  if (bps < 33) {
580  if (bps + order <= 32) {
581  if ((ret = decode_subframe_fixed(s, decoded, order, bps)) < 0)
582  return ret;
583  } else {
584  if ((ret = decode_subframe_fixed_wide(s, decoded, order, bps)) < 0)
585  return ret;
586  }
587  } else {
588  if ((ret = decode_subframe_fixed_33bps(s, s->decoded_33bps, decoded, order)) < 0)
589  return ret;
590  }
591  } else if (type >= 32) {
592  if (bps < 33) {
593  if ((ret = decode_subframe_lpc(s, decoded, (type & ~0x20)+1, bps)) < 0)
594  return ret;
595  } else {
596  if ((ret = decode_subframe_lpc_33bps(s, s->decoded_33bps, decoded, (type & ~0x20)+1)) < 0)
597  return ret;
598  }
599  } else {
600  av_log(s->avctx, AV_LOG_ERROR, "invalid coding type\n");
601  return AVERROR_INVALIDDATA;
602  }
603 
604  if (wasted) {
605  if (wasted+bps == 33) {
606  int i;
607  for (i = 0; i < s->blocksize; i++)
608  s->decoded_33bps[i] = (uint64_t)decoded[i] << wasted;
609  } else if (wasted < 32) {
610  int i;
611  for (i = 0; i < s->blocksize; i++)
612  decoded[i] = (unsigned)decoded[i] << wasted;
613  }
614  }
615 
616  return 0;
617 }
618 
620 {
621  int i, ret;
622  GetBitContext *gb = &s->gb;
623  FLACFrameInfo fi;
624 
625  if ((ret = ff_flac_decode_frame_header(s->avctx, gb, &fi, 0)) < 0) {
626  av_log(s->avctx, AV_LOG_ERROR, "invalid frame header\n");
627  return ret;
628  }
629 
630  if ( s->stream_info.channels
631  && fi.channels != s->stream_info.channels
632  && s->got_streaminfo) {
633  s->stream_info.channels = fi.channels;
636  if (ret < 0)
637  return ret;
638  }
639  s->stream_info.channels = fi.channels;
641  s->ch_mode = fi.ch_mode;
642 
643  if (!s->stream_info.bps && !fi.bps) {
644  av_log(s->avctx, AV_LOG_ERROR, "bps not found in STREAMINFO or frame header\n");
645  return AVERROR_INVALIDDATA;
646  }
647  if (!fi.bps) {
648  fi.bps = s->stream_info.bps;
649  } else if (s->stream_info.bps && fi.bps != s->stream_info.bps) {
650  av_log(s->avctx, AV_LOG_ERROR, "switching bps mid-stream is not "
651  "supported\n");
652  return AVERROR_INVALIDDATA;
653  }
654 
655  if (!s->stream_info.bps) {
656  s->stream_info.bps = s->avctx->bits_per_raw_sample = fi.bps;
657  flac_set_bps(s);
658  }
659 
660  if (!s->stream_info.max_blocksize)
661  s->stream_info.max_blocksize = FLAC_MAX_BLOCKSIZE;
662  if (fi.blocksize > s->stream_info.max_blocksize) {
663  av_log(s->avctx, AV_LOG_ERROR, "blocksize %d > %d\n", fi.blocksize,
664  s->stream_info.max_blocksize);
665  return AVERROR_INVALIDDATA;
666  }
667  s->blocksize = fi.blocksize;
668 
669  if (!s->stream_info.samplerate && !fi.samplerate) {
670  av_log(s->avctx, AV_LOG_ERROR, "sample rate not found in STREAMINFO"
671  " or frame header\n");
672  return AVERROR_INVALIDDATA;
673  }
674  if (fi.samplerate == 0)
675  fi.samplerate = s->stream_info.samplerate;
676  s->stream_info.samplerate = s->avctx->sample_rate = fi.samplerate;
677 
678  if (!s->got_streaminfo) {
680  if (ret < 0)
681  return ret;
682  s->got_streaminfo = 1;
683  dump_headers(s->avctx, &s->stream_info);
684  }
685  ff_flacdsp_init(&s->dsp, s->avctx->sample_fmt,
686  s->stream_info.channels);
687 
688 // dump_headers(s->avctx, &s->stream_info);
689 
690  /* subframes */
691  for (i = 0; i < s->stream_info.channels; i++) {
692  if ((ret = decode_subframe(s, i)) < 0)
693  return ret;
694  }
695 
696  align_get_bits(gb);
697 
698  /* frame footer */
699  skip_bits(gb, 16); /* data crc */
700 
701  return 0;
702 }
703 
704 static void decorrelate_33bps(int ch_mode, int32_t **decoded, int64_t *decoded_33bps, int len)
705 {
706  int i;
707  if (ch_mode == FLAC_CHMODE_LEFT_SIDE ) {
708  for (i = 0; i < len; i++)
709  decoded[1][i] = decoded[0][i] - (uint64_t)decoded_33bps[i];
710  } else if (ch_mode == FLAC_CHMODE_RIGHT_SIDE ) {
711  for (i = 0; i < len; i++)
712  decoded[0][i] = decoded[1][i] + (uint64_t)decoded_33bps[i];
713  } else if (ch_mode == FLAC_CHMODE_MID_SIDE ) {
714  for (i = 0; i < len; i++) {
715  uint64_t a = decoded[0][i];
716  int64_t b = decoded_33bps[i];
717  a -= b >> 1;
718  decoded[0][i] = (a + b);
719  decoded[1][i] = a;
720  }
721  }
722 }
723 
725  int *got_frame_ptr, AVPacket *avpkt)
726 {
727  const uint8_t *buf = avpkt->data;
728  int buf_size = avpkt->size;
729  FLACContext *s = avctx->priv_data;
730  int bytes_read = 0;
731  int ret;
732 
733  *got_frame_ptr = 0;
734 
735  if (buf_size > 5 && !memcmp(buf, "\177FLAC", 5)) {
736  av_log(s->avctx, AV_LOG_DEBUG, "skipping flac header packet 1\n");
737  return buf_size;
738  }
739 
740  if (buf_size > 0 && (*buf & 0x7F) == FLAC_METADATA_TYPE_VORBIS_COMMENT) {
741  av_log(s->avctx, AV_LOG_DEBUG, "skipping vorbis comment\n");
742  return buf_size;
743  }
744 
745  /* check that there is at least the smallest decodable amount of data.
746  this amount corresponds to the smallest valid FLAC frame possible.
747  FF F8 69 02 00 00 9A 00 00 34 */
748  if (buf_size < FLAC_MIN_FRAME_SIZE)
749  return buf_size;
750 
751  /* check for inline header */
752  if (AV_RB32(buf) == MKBETAG('f','L','a','C')) {
753  if (!s->got_streaminfo && (ret = parse_streaminfo(s, buf, buf_size))) {
754  av_log(s->avctx, AV_LOG_ERROR, "invalid header\n");
755  return ret;
756  }
757  return get_metadata_size(buf, buf_size);
758  }
759 
760  /* decode frame */
761  if ((ret = init_get_bits8(&s->gb, buf, buf_size)) < 0)
762  return ret;
763  if ((ret = decode_frame(s)) < 0) {
764  av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n");
765  return ret;
766  }
767  bytes_read = get_bits_count(&s->gb)/8;
768 
769  if ((s->avctx->err_recognition & (AV_EF_CRCCHECK|AV_EF_COMPLIANT)) &&
771  0, buf, bytes_read)) {
772  av_log(s->avctx, AV_LOG_ERROR, "CRC error at PTS %"PRId64"\n", avpkt->pts);
773  if (s->avctx->err_recognition & AV_EF_EXPLODE)
774  return AVERROR_INVALIDDATA;
775  }
776 
777  /* get output buffer */
778  frame->nb_samples = s->blocksize;
779  if ((ret = ff_thread_get_buffer(avctx, frame, 0)) < 0)
780  return ret;
781 
782  if (s->stream_info.bps == 32 && s->ch_mode > 0) {
783  decorrelate_33bps(s->ch_mode, s->decoded, s->decoded_33bps, s->blocksize);
784  s->dsp.decorrelate[0](frame->data, s->decoded, s->stream_info.channels,
785  s->blocksize, s->sample_shift);
786  } else {
787  s->dsp.decorrelate[s->ch_mode](frame->data, s->decoded,
788  s->stream_info.channels,
789  s->blocksize, s->sample_shift);
790  }
791 
792  if (bytes_read > buf_size) {
793  av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", bytes_read - buf_size);
794  return AVERROR_INVALIDDATA;
795  }
796  if (bytes_read < buf_size) {
797  av_log(s->avctx, AV_LOG_DEBUG, "underread: %d orig size: %d\n",
798  buf_size - bytes_read, buf_size);
799  }
800 
801  *got_frame_ptr = 1;
802 
803  return bytes_read;
804 }
805 
807 {
808  FLACContext *s = avctx->priv_data;
809 
810  av_freep(&s->decoded_buffer);
811  av_freep(&s->decoded_buffer_33bps);
812 
813  return 0;
814 }
815 
816 static const AVOption options[] = {
817 { "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 },
818 { NULL },
819 };
820 
821 static const AVClass flac_decoder_class = {
822  .class_name = "FLAC decoder",
823  .item_name = av_default_item_name,
824  .option = options,
825  .version = LIBAVUTIL_VERSION_INT,
826 };
827 
829  .p.name = "flac",
830  CODEC_LONG_NAME("FLAC (Free Lossless Audio Codec)"),
831  .p.type = AVMEDIA_TYPE_AUDIO,
832  .p.id = AV_CODEC_ID_FLAC,
833  .priv_data_size = sizeof(FLACContext),
835  .close = flac_decode_close,
837  .p.capabilities = AV_CODEC_CAP_CHANNEL_CONF |
840  .p.sample_fmts = (const enum AVSampleFormat[]) { AV_SAMPLE_FMT_S16,
845  .p.priv_class = &flac_decoder_class,
846 };
decode_residuals
static int decode_residuals(FLACContext *s, int32_t *decoded, int pred_order)
Definition: flacdec.c:242
FLAC_CHMODE_MID_SIDE
@ FLAC_CHMODE_MID_SIDE
Definition: flac.h:42
AV_EF_EXPLODE
#define AV_EF_EXPLODE
abort decoding on minor error detection
Definition: defs.h:51
flac_decode_close
static av_cold int flac_decode_close(AVCodecContext *avctx)
Definition: flacdec.c:806
FLACContext::decoded_buffer_size
unsigned int decoded_buffer_size
Definition: flacdec.c:66
show_bits_long
static unsigned int show_bits_long(GetBitContext *s, int n)
Show 0-32 bits.
Definition: get_bits.h:495
get_bits_left
static int get_bits_left(GetBitContext *gb)
Definition: get_bits.h:694
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
opt.h
FLACContext::decoded_buffer_size_33bps
unsigned int decoded_buffer_size_33bps
Definition: flacdec.c:69
allocate_buffers
static int allocate_buffers(FLACContext *s)
Definition: flacdec.c:138
AV_EF_COMPLIANT
#define AV_EF_COMPLIANT
consider all spec non compliances as errors
Definition: defs.h:55
ff_flac_decoder
const FFCodec ff_flac_decoder
Definition: flacdec.c:828
av_samples_fill_arrays
int av_samples_fill_arrays(uint8_t **audio_data, int *linesize, const uint8_t *buf, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
Fill plane data pointers and linesize for samples with sample format sample_fmt.
Definition: samplefmt.c:153
get_bits_count
static int get_bits_count(const GetBitContext *s)
Definition: get_bits.h:266
FLACContext::ch_mode
int ch_mode
channel decorrelation type in the current frame
Definition: flacdec.c:61
AVFrame
This structure describes decoded (raw) audio or video data.
Definition: frame.h:340
tmp
static uint8_t tmp[11]
Definition: aes_ctr.c:28
AVPacket::data
uint8_t * data
Definition: packet.h:522
AVOption
AVOption.
Definition: opt.h:346
b
#define b
Definition: input.c:41
AV_SAMPLE_FMT_S32P
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
Definition: samplefmt.h:65
FLACContext::dsp
FLACDSPContext dsp
Definition: flacdec.c:72
ff_flac_parse_streaminfo
int ff_flac_parse_streaminfo(AVCodecContext *avctx, struct FLACStreaminfo *s, const uint8_t *buffer)
Parse the Streaminfo metadata block.
Definition: flac.c:187
FFCodec
Definition: codec_internal.h:127
AV_CODEC_ID_FLAC
@ AV_CODEC_ID_FLAC
Definition: codec_id.h:452
flacdsp.h
thread.h
AVFrame::data
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:361
ff_thread_get_buffer
the pkt_dts and pkt_pts fields in AVFrame will work as usual Restrictions on codec whose streams don t reset across will not work because their bitstreams cannot be decoded in parallel *The contents of buffers must not be read before as well as code calling up to before the decode process starts Call have so the codec calls ff_thread_report set FF_CODEC_CAP_ALLOCATE_PROGRESS in FFCodec caps_internal and use ff_thread_get_buffer() to allocate frames. Otherwise decode directly into the user-supplied frames. Call ff_thread_report_progress() after some part of the current picture has decoded. A good place to put this is where draw_horiz_band() is called - add this if it isn 't called anywhere
FLACContext::sample_shift
int sample_shift
shift required to make output samples 16-bit or 32-bit
Definition: flacdec.c:60
flac_decoder_class
static const AVClass flac_decoder_class
Definition: flacdec.c:821
crc.h
skip_bits
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:381
flac_parse.h
golomb.h
exp golomb vlc stuff
get_bits
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:335
FFCodec::p
AVCodec p
The public AVCodec.
Definition: codec_internal.h:131
ff_flac_set_channel_layout
void ff_flac_set_channel_layout(AVCodecContext *avctx, int channels)
Definition: flac.c:173
ff_flac_decode_frame_header
int ff_flac_decode_frame_header(void *logctx, GetBitContext *gb, FLACFrameInfo *fi, int log_level_offset)
Validate and decode a frame header.
Definition: flac.c:51
GetBitContext
Definition: get_bits.h:108
SUINT32
#define SUINT32
Definition: dct32_template.c:31
type
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
Definition: writing_filters.txt:86
AV_SAMPLE_FMT_S64P
@ AV_SAMPLE_FMT_S64P
signed 64 bits, planar
Definition: samplefmt.h:69
decode_subframe_lpc
static int decode_subframe_lpc(FLACContext *s, int32_t *decoded, int pred_order, int bps)
Definition: flacdec.c:438
planar
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(const uint8_t *) pi - 0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi - 0x80) *(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(const int16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1<< 16)) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(const int16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, *(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(const int32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(const int32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, *(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(const int64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0f/(UINT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64, *(const int64_t *) pi *(1.0/(UINT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(const float *) pi *(UINT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(const double *) pi *(UINT64_C(1)<< 63))) #define FMT_PAIR_FUNC(out, in) static conv_func_type *const fmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={ FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64), };static void cpy1(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, len);} static void cpy2(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 2 *len);} static void cpy4(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 4 *len);} static void cpy8(uint8_t **dst, const uint8_t **src, int len){ memcpy(*dst, *src, 8 *len);} AudioConvert *swri_audio_convert_alloc(enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, const int *ch_map, int flags) { AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) return NULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) return NULL;if(channels==1){ in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);} ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map) { switch(av_get_bytes_per_sample(in_fmt)){ case 1:ctx->simd_f=cpy1;break;case 2:ctx->simd_f=cpy2;break;case 4:ctx->simd_f=cpy4;break;case 8:ctx->simd_f=cpy8;break;} } return ctx;} void swri_audio_convert_free(AudioConvert **ctx) { av_freep(ctx);} int swri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, int len) { int ch;int off=0;const int os=(out->planar ? 1 :out->ch_count) *out->bps;unsigned misaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask) { int planes=in->planar ? in->ch_count :1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;} if(ctx->out_simd_align_mask) { int planes=out->planar ? out->ch_count :1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;} if(ctx->simd_f &&!ctx->ch_map &&!misaligned){ off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){ if(out->planar==in->planar){ int planes=out->planar ? out->ch_count :1;for(ch=0;ch< planes;ch++){ ctx->simd_f(out->ch+ch,(const uint8_t **) in->ch+ch, off *(out-> planar
Definition: audioconvert.c:56
AV_OPT_FLAG_AUDIO_PARAM
#define AV_OPT_FLAG_AUDIO_PARAM
Definition: opt.h:274
DECODER_SUBFRAME_FIXED_WIDE
#define DECODER_SUBFRAME_FIXED_WIDE(residual)
Definition: flacdec.c:355
avassert.h
get_sbits64
static int64_t get_sbits64(GetBitContext *s, int n)
Read 0-64 bits as a signed integer.
Definition: get_bits.h:483
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
av_cold
#define av_cold
Definition: attributes.h:90
init_get_bits8
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:545
FLACFrameInfo::channels
int channels
number of channels
Definition: flac_parse.h:44
FLACStreaminfo
Definition: flac_parse.h:33
decode_subframe_fixed_33bps
static int decode_subframe_fixed_33bps(FLACContext *s, int64_t *decoded, int32_t *residual, int pred_order)
Definition: flacdec.c:401
FLACContext::gb
GetBitContext gb
GetBitContext initialized to start at the current frame.
Definition: flacdec.c:57
FF_CODEC_DECODE_CB
#define FF_CODEC_DECODE_CB(func)
Definition: codec_internal.h:287
s
#define s(width, name)
Definition: cbs_vp9.c:198
flac_set_bps
static void flac_set_bps(FLACContext *s)
Definition: flacdec.c:77
FLACFrameInfo::blocksize
int blocksize
block size of the frame
Definition: flac_parse.h:46
AVMEDIA_TYPE_AUDIO
@ AVMEDIA_TYPE_AUDIO
Definition: avutil.h:202
dump_headers
static void dump_headers(AVCodecContext *avctx, FLACStreaminfo *s)
Definition: flacdec.c:129
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:40
av_sample_fmt_is_planar
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
Definition: samplefmt.c:114
get_sbits
static int get_sbits(GetBitContext *s, int n)
Definition: get_bits.h:320
AV_LOG_DEBUG
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:201
get_bits.h
limits.h
flac_decode_frame
static int flac_decode_frame(AVCodecContext *avctx, AVFrame *frame, int *got_frame_ptr, AVPacket *avpkt)
Definition: flacdec.c:724
CODEC_LONG_NAME
#define CODEC_LONG_NAME(str)
Definition: codec_internal.h:272
frame
static AVFrame * frame
Definition: demux_decode.c:54
ff_flac_is_extradata_valid
int ff_flac_is_extradata_valid(AVCodecContext *avctx, uint8_t **streaminfo_start)
Validate the FLAC extradata.
Definition: flac.c:149
FLAC_METADATA_TYPE_STREAMINFO
@ FLAC_METADATA_TYPE_STREAMINFO
Definition: flac.h:46
AV_CRC_16_ANSI
@ AV_CRC_16_ANSI
Definition: crc.h:50
AV_CODEC_CAP_FRAME_THREADS
#define AV_CODEC_CAP_FRAME_THREADS
Codec supports frame-level multithreading.
Definition: codec.h:110
LIBAVUTIL_VERSION_INT
#define LIBAVUTIL_VERSION_INT
Definition: version.h:85
AVClass
Describe the class of an AVClass context structure.
Definition: log.h:66
NULL
#define NULL
Definition: coverity.c:32
get_sr_golomb_flac
static int get_sr_golomb_flac(GetBitContext *gb, int k, int limit, int esc_len)
read signed golomb rice code (flac).
Definition: golomb.h:539
flac_parse_block_header
static av_always_inline void flac_parse_block_header(const uint8_t *block_header, int *last, int *type, int *size)
Parse the metadata block parameters from the header.
Definition: flac.h:63
av_default_item_name
const char * av_default_item_name(void *ptr)
Return the context name.
Definition: log.c:237
get_bits1
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:388
FLACContext::decoded
int32_t * decoded[FLAC_MAX_CHANNELS]
decoded samples
Definition: flacdec.c:64
FLAC_STREAMINFO_SIZE
#define FLAC_STREAMINFO_SIZE
Definition: flac.h:32
FLAC_CHMODE_RIGHT_SIDE
@ FLAC_CHMODE_RIGHT_SIDE
Definition: flac.h:41
FLACFrameInfo
Definition: flac_parse.h:42
AV_EF_CRCCHECK
#define AV_EF_CRCCHECK
Verify checksums embedded in the bitstream (could be of either encoded or decoded data,...
Definition: defs.h:48
decode_frame
static int decode_frame(FLACContext *s)
Definition: flacdec.c:619
c
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
get_unary
static int get_unary(GetBitContext *gb, int stop, int len)
Get unary code of limited length.
Definition: unary.h:46
decode_subframe_fixed_wide
static int decode_subframe_fixed_wide(FLACContext *s, int32_t *decoded, int pred_order, int bps)
Definition: flacdec.c:390
AV_CODEC_CAP_CHANNEL_CONF
#define AV_CODEC_CAP_CHANNEL_CONF
Codec should fill in channel configuration and samplerate instead of container.
Definition: codec.h:106
init
int(* init)(AVBSFContext *ctx)
Definition: dts2pts.c:365
AV_CODEC_CAP_DR1
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() or get_encode_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:52
AVPacket::size
int size
Definition: packet.h:523
get_metadata_size
static int get_metadata_size(const uint8_t *buf, int buf_size)
Determine the size of an inline header.
Definition: flacdec.c:221
codec_internal.h
ff_flacdsp_init
av_cold void ff_flacdsp_init(FLACDSPContext *c, enum AVSampleFormat fmt, int channels)
Definition: flacdsp.c:87
bps
unsigned bps
Definition: movenc.c:1787
AVCodecContext::sample_fmt
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1057
AV_SAMPLE_FMT_NONE
@ AV_SAMPLE_FMT_NONE
Definition: samplefmt.h:56
flacdata.h
MKBETAG
#define MKBETAG(a, b, c, d)
Definition: macros.h:56
AV_RB32
uint64_t_TMPL AV_WL64 unsigned int_TMPL AV_WL32 unsigned int_TMPL AV_WL24 unsigned int_TMPL AV_WL16 uint64_t_TMPL AV_WB64 unsigned int_TMPL AV_RB32
Definition: bytestream.h:96
FLACContext::buggy_lpc
int buggy_lpc
use workaround for old lavc encoded files
Definition: flacdec.c:70
decode_subframe_fixed
static int decode_subframe_fixed(FLACContext *s, int32_t *decoded, int pred_order, int bps)
Definition: flacdec.c:303
a
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:41
decode_subframe
static int decode_subframe(FLACContext *s, int channel)
Definition: flacdec.c:523
av_crc_get_table
const AVCRC * av_crc_get_table(AVCRCId crc_id)
Get an initialized standard CRC table.
Definition: crc.c:374
unary.h
AV_SAMPLE_FMT_S16P
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition: samplefmt.h:64
FLACContext
Definition: flacdec.c:52
AVFrame::nb_samples
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:420
FLACContext::got_streaminfo
int got_streaminfo
indicates if the STREAMINFO has been read
Definition: flacdec.c:62
parse_streaminfo
static int parse_streaminfo(FLACContext *s, const uint8_t *buf, int buf_size)
Parse the STREAMINFO from an inline header.
Definition: flacdec.c:188
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:255
AVPacket::pts
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: packet.h:515
av_get_bytes_per_sample
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:108
AVCodecContext::extradata
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:523
decorrelate_33bps
static void decorrelate_33bps(int ch_mode, int32_t **decoded, int64_t *decoded_33bps, int len)
Definition: flacdec.c:704
FLACFrameInfo::ch_mode
int ch_mode
channel decorrelation mode
Definition: flac_parse.h:47
AVSampleFormat
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:55
options
static const AVOption options[]
Definition: flacdec.c:816
AV_SAMPLE_FMT_S16
@ AV_SAMPLE_FMT_S16
signed 16 bits
Definition: samplefmt.h:58
AVCodec::name
const char * name
Name of the codec implementation.
Definition: codec.h:194
len
int len
Definition: vorbis_enc_data.h:426
av_samples_get_buffer_size
int av_samples_get_buffer_size(int *linesize, int nb_channels, int nb_samples, enum AVSampleFormat sample_fmt, int align)
Get the required buffer size for the given audio parameters.
Definition: samplefmt.c:121
FLACFrameInfo::samplerate
int samplerate
sample rate
Definition: flac_parse.h:43
avcodec.h
lpc_analyze_remodulate
static void lpc_analyze_remodulate(SUINT32 *decoded, const int coeffs[32], int order, int qlevel, int len, int bps)
Definition: flacdec.c:411
av_uninit
#define av_uninit(x)
Definition: attributes.h:154
FLACContext::avctx
AVCodecContext * avctx
parent AVCodecContext
Definition: flacdec.c:56
ret
ret
Definition: filter_design.txt:187
AVClass::class_name
const char * class_name
The name of the class; usually it is the same name as the context structure type to which the AVClass...
Definition: log.h:71
FLACContext::decoded_buffer_33bps
uint8_t * decoded_buffer_33bps
Definition: flacdec.c:68
FLAC_METADATA_TYPE_VORBIS_COMMENT
@ FLAC_METADATA_TYPE_VORBIS_COMMENT
Definition: flac.h:50
align_get_bits
static const uint8_t * align_get_bits(GetBitContext *s)
Definition: get_bits.h:561
FLAC_MIN_FRAME_SIZE
#define FLAC_MIN_FRAME_SIZE
Definition: flac.h:36
FLACContext::decoded_buffer
uint8_t * decoded_buffer
Definition: flacdec.c:65
left
Tag MUST be and< 10hcoeff half pel interpolation filter coefficients, hcoeff[0] are the 2 middle coefficients[1] are the next outer ones and so on, resulting in a filter like:...eff[2], hcoeff[1], hcoeff[0], hcoeff[0], hcoeff[1], hcoeff[2] ... the sign of the coefficients is not explicitly stored but alternates after each coeff and coeff[0] is positive, so ...,+,-,+,-,+,+,-,+,-,+,... hcoeff[0] is not explicitly stored but found by subtracting the sum of all stored coefficients with signs from 32 hcoeff[0]=32 - hcoeff[1] - hcoeff[2] - ... a good choice for hcoeff and htaps is htaps=6 hcoeff={40,-10, 2} an alternative which requires more computations at both encoder and decoder side and may or may not be better is htaps=8 hcoeff={42,-14, 6,-2}ref_frames minimum of the number of available reference frames and max_ref_frames for example the first frame after a key frame always has ref_frames=1spatial_decomposition_type wavelet type 0 is a 9/7 symmetric compact integer wavelet 1 is a 5/3 symmetric compact integer wavelet others are reserved stored as delta from last, last is reset to 0 if always_reset||keyframeqlog quality(logarithmic quantizer scale) stored as delta from last, last is reset to 0 if always_reset||keyframemv_scale stored as delta from last, last is reset to 0 if always_reset||keyframe FIXME check that everything works fine if this changes between framesqbias dequantization bias stored as delta from last, last is reset to 0 if always_reset||keyframeblock_max_depth maximum depth of the block tree stored as delta from last, last is reset to 0 if always_reset||keyframequant_table quantization tableHighlevel bitstream structure:==============================--------------------------------------------|Header|--------------------------------------------|------------------------------------|||Block0||||split?||||yes no||||......... intra?||||:Block01 :yes no||||:Block02 :....... ..........||||:Block03 ::y DC ::ref index:||||:Block04 ::cb DC ::motion x :||||......... :cr DC ::motion y :||||....... ..........|||------------------------------------||------------------------------------|||Block1|||...|--------------------------------------------|------------ ------------ ------------|||Y subbands||Cb subbands||Cr subbands||||--- ---||--- ---||--- ---|||||LL0||HL0||||LL0||HL0||||LL0||HL0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||LH0||HH0||||LH0||HH0||||LH0||HH0|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HL1||LH1||||HL1||LH1||||HL1||LH1|||||--- ---||--- ---||--- ---||||--- ---||--- ---||--- ---|||||HH1||HL2||||HH1||HL2||||HH1||HL2|||||...||...||...|||------------ ------------ ------------|--------------------------------------------Decoding process:=================------------|||Subbands|------------||||------------|Intra DC||||LL0 subband prediction ------------|\ Dequantization ------------------- \||Reference frames|\ IDWT|------- -------|Motion \|||Frame 0||Frame 1||Compensation . OBMC v -------|------- -------|--------------. \------> Frame n output Frame Frame<----------------------------------/|...|------------------- Range Coder:============Binary Range Coder:------------------- The implemented range coder is an adapted version based upon "Range encoding: an algorithm for removing redundancy from a digitised message." by G. N. N. Martin. The symbols encoded by the Snow range coder are bits(0|1). The associated probabilities are not fix but change depending on the symbol mix seen so far. bit seen|new state ---------+----------------------------------------------- 0|256 - state_transition_table[256 - old_state];1|state_transition_table[old_state];state_transition_table={ 0, 0, 0, 0, 0, 0, 0, 0, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 94, 95, 96, 97, 98, 99, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, 112, 113, 114, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 128, 129, 130, 131, 132, 133, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, 145, 146, 147, 148, 149, 150, 151, 152, 152, 153, 154, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167, 168, 169, 170, 171, 171, 172, 173, 174, 175, 176, 177, 178, 179, 180, 181, 182, 183, 184, 185, 186, 187, 188, 189, 190, 190, 191, 192, 194, 194, 195, 196, 197, 198, 199, 200, 201, 202, 202, 204, 205, 206, 207, 208, 209, 209, 210, 211, 212, 213, 215, 215, 216, 217, 218, 219, 220, 220, 222, 223, 224, 225, 226, 227, 227, 229, 229, 230, 231, 232, 234, 234, 235, 236, 237, 238, 239, 240, 241, 242, 243, 244, 245, 246, 247, 248, 248, 0, 0, 0, 0, 0, 0, 0};FIXME Range Coding of integers:------------------------- FIXME Neighboring Blocks:===================left and top are set to the respective blocks unless they are outside of the image in which case they are set to the Null block top-left is set to the top left block unless it is outside of the image in which case it is set to the left block if this block has no larger parent block or it is at the left side of its parent block and the top right block is not outside of the image then the top right block is used for top-right else the top-left block is used Null block y, cb, cr are 128 level, ref, mx and my are 0 Motion Vector Prediction:=========================1. the motion vectors of all the neighboring blocks are scaled to compensate for the difference of reference frames scaled_mv=(mv *(256 *(current_reference+1)/(mv.reference+1))+128)> the median of the scaled left
Definition: snow.txt:386
U
#define U(x)
Definition: vpx_arith.h:37
FLAC_MAX_CHANNELS
#define FLAC_MAX_CHANNELS
Definition: flac.h:33
AVCodecContext
main external API structure.
Definition: avcodec.h:445
FLACContext::decoded_33bps
int64_t * decoded_33bps
decoded samples for a 33 bps subframe
Definition: flacdec.c:67
av_crc
uint32_t av_crc(const AVCRC *ctx, uint32_t crc, const uint8_t *buffer, size_t length)
Calculate the CRC of a block.
Definition: crc.c:392
samples
Filter the word “frame” indicates either a video frame or a group of audio samples
Definition: filter_design.txt:8
flac_decode_init
static av_cold int flac_decode_init(AVCodecContext *avctx)
Definition: flacdec.c:99
decode_subframe_lpc_33bps
static int decode_subframe_lpc_33bps(FLACContext *s, int64_t *decoded, int32_t *residual, int pred_order)
Definition: flacdec.c:482
FLAC_MAX_BLOCKSIZE
#define FLAC_MAX_BLOCKSIZE
Definition: flac.h:35
AV_OPT_FLAG_DECODING_PARAM
#define AV_OPT_FLAG_DECODING_PARAM
A generic parameter which can be set by the user for demuxing or decoding.
Definition: opt.h:273
FLACContext::blocksize
int blocksize
number of samples in the current frame
Definition: flacdec.c:59
AVPacket
This structure stores compressed data.
Definition: packet.h:499
AVCodecContext::priv_data
void * priv_data
Definition: avcodec.h:472
AV_OPT_TYPE_BOOL
@ AV_OPT_TYPE_BOOL
Definition: opt.h:251
FLAC_CHMODE_LEFT_SIDE
@ FLAC_CHMODE_LEFT_SIDE
Definition: flac.h:40
av_freep
#define av_freep(p)
Definition: tableprint_vlc.h:34
av_fast_malloc
void av_fast_malloc(void *ptr, unsigned int *size, size_t min_size)
Allocate a buffer, reusing the given one if large enough.
Definition: mem.c:555
d
d
Definition: ffmpeg_filter.c:425
int32_t
int32_t
Definition: audioconvert.c:56
bytestream.h
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
AVERROR_INVALIDDATA
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:61
FLACFrameInfo::bps
int bps
bits-per-sample
Definition: flac_parse.h:45
flac.h
get_sbits_long
static int get_sbits_long(GetBitContext *s, int n)
Read 0-32 bits as a signed integer.
Definition: get_bits.h:471
AV_SAMPLE_FMT_S32
@ AV_SAMPLE_FMT_S32
signed 32 bits
Definition: samplefmt.h:59
FLACDSPContext
Definition: flacdsp.h:26
av_log2
int av_log2(unsigned v)
Definition: intmath.c:26
channel
channel
Definition: ebur128.h:39
FLACContext::stream_info
FLACStreaminfo stream_info
Definition: flacdec.c:54