You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

684 lines
20KB

  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. * @file flacdec.c
  23. * FLAC (Free Lossless Audio Codec) decoder
  24. * @author Alex Beregszaszi
  25. *
  26. * For more information on the FLAC format, visit:
  27. * http://flac.sourceforge.net/
  28. *
  29. * This decoder can be used in 1 of 2 ways: Either raw FLAC data can be fed
  30. * through, starting from the initial 'fLaC' signature; or by passing the
  31. * 34-byte streaminfo structure through avctx->extradata[_size] followed
  32. * by data starting with the 0xFFF8 marker.
  33. */
  34. #include <limits.h>
  35. #define ALT_BITSTREAM_READER
  36. #include "libavutil/crc.h"
  37. #include "avcodec.h"
  38. #include "bitstream.h"
  39. #include "golomb.h"
  40. #include "flac.h"
  41. #undef NDEBUG
  42. #include <assert.h>
  43. #define MAX_CHANNELS 8
  44. #define MAX_BLOCKSIZE 65535
  45. enum decorrelation_type {
  46. INDEPENDENT,
  47. LEFT_SIDE,
  48. RIGHT_SIDE,
  49. MID_SIDE,
  50. };
  51. typedef struct FLACContext {
  52. FLACSTREAMINFO
  53. AVCodecContext *avctx;
  54. GetBitContext gb;
  55. int blocksize/*, last_blocksize*/;
  56. int curr_bps;
  57. enum decorrelation_type decorrelation;
  58. int32_t *decoded[MAX_CHANNELS];
  59. uint8_t *bitstream;
  60. unsigned int bitstream_size;
  61. unsigned int bitstream_index;
  62. unsigned int allocated_bitstream_size;
  63. } FLACContext;
  64. static const int sample_rate_table[] =
  65. { 0,
  66. 88200, 176400, 192000,
  67. 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000,
  68. 0, 0, 0, 0 };
  69. static const int sample_size_table[] =
  70. { 0, 8, 12, 0, 16, 20, 24, 0 };
  71. static const int blocksize_table[] = {
  72. 0, 192, 576<<0, 576<<1, 576<<2, 576<<3, 0, 0,
  73. 256<<0, 256<<1, 256<<2, 256<<3, 256<<4, 256<<5, 256<<6, 256<<7
  74. };
  75. static int64_t get_utf8(GetBitContext *gb)
  76. {
  77. int64_t val;
  78. GET_UTF8(val, get_bits(gb, 8), return -1;)
  79. return val;
  80. }
  81. static void allocate_buffers(FLACContext *s);
  82. static int metadata_parse(FLACContext *s);
  83. static av_cold int flac_decode_init(AVCodecContext *avctx)
  84. {
  85. FLACContext *s = avctx->priv_data;
  86. s->avctx = avctx;
  87. if (avctx->extradata_size > 4) {
  88. /* initialize based on the demuxer-supplied streamdata header */
  89. if (avctx->extradata_size == FLAC_STREAMINFO_SIZE) {
  90. ff_flac_parse_streaminfo(avctx, (FLACStreaminfo *)s,
  91. avctx->extradata);
  92. allocate_buffers(s);
  93. } else {
  94. init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size*8);
  95. metadata_parse(s);
  96. }
  97. }
  98. avctx->sample_fmt = SAMPLE_FMT_S16;
  99. return 0;
  100. }
  101. static void dump_headers(AVCodecContext *avctx, FLACStreaminfo *s)
  102. {
  103. av_log(avctx, AV_LOG_DEBUG, " Blocksize: %d .. %d\n", s->min_blocksize,
  104. s->max_blocksize);
  105. av_log(avctx, AV_LOG_DEBUG, " Max Framesize: %d\n", s->max_framesize);
  106. av_log(avctx, AV_LOG_DEBUG, " Samplerate: %d\n", s->samplerate);
  107. av_log(avctx, AV_LOG_DEBUG, " Channels: %d\n", s->channels);
  108. av_log(avctx, AV_LOG_DEBUG, " Bits: %d\n", s->bps);
  109. }
  110. static void allocate_buffers(FLACContext *s)
  111. {
  112. int i;
  113. assert(s->max_blocksize);
  114. if (s->max_framesize == 0 && s->max_blocksize) {
  115. // FIXME header overhead
  116. s->max_framesize= (s->channels * s->bps * s->max_blocksize + 7)/ 8;
  117. }
  118. for (i = 0; i < s->channels; i++) {
  119. s->decoded[i] = av_realloc(s->decoded[i],
  120. sizeof(int32_t)*s->max_blocksize);
  121. }
  122. if (s->allocated_bitstream_size < s->max_framesize)
  123. s->bitstream= av_fast_realloc(s->bitstream,
  124. &s->allocated_bitstream_size,
  125. s->max_framesize);
  126. }
  127. void ff_flac_parse_streaminfo(AVCodecContext *avctx, struct FLACStreaminfo *s,
  128. const uint8_t *buffer)
  129. {
  130. GetBitContext gb;
  131. init_get_bits(&gb, buffer, FLAC_STREAMINFO_SIZE*8);
  132. /* mandatory streaminfo */
  133. s->min_blocksize = get_bits(&gb, 16);
  134. s->max_blocksize = get_bits(&gb, 16);
  135. skip_bits(&gb, 24); /* skip min frame size */
  136. s->max_framesize = get_bits_long(&gb, 24);
  137. s->samplerate = get_bits_long(&gb, 20);
  138. s->channels = get_bits(&gb, 3) + 1;
  139. s->bps = get_bits(&gb, 5) + 1;
  140. avctx->channels = s->channels;
  141. avctx->sample_rate = s->samplerate;
  142. s->samples = get_bits_long(&gb, 32) << 4;
  143. s->samples |= get_bits_long(&gb, 4);
  144. skip_bits(&gb, 64); /* md5 sum */
  145. skip_bits(&gb, 64); /* md5 sum */
  146. dump_headers(avctx, s);
  147. }
  148. /**
  149. * Parse a list of metadata blocks. This list of blocks must begin with
  150. * the fLaC marker.
  151. * @param s the flac decoding context containing the gb bit reader used to
  152. * parse metadata
  153. * @return 1 if some metadata was read, 0 if no fLaC marker was found
  154. */
  155. static int metadata_parse(FLACContext *s)
  156. {
  157. int i, metadata_last, metadata_type, metadata_size, streaminfo_updated=0;
  158. int initial_pos= get_bits_count(&s->gb);
  159. if (show_bits_long(&s->gb, 32) == MKBETAG('f','L','a','C')) {
  160. skip_bits(&s->gb, 32);
  161. do {
  162. metadata_last = get_bits1(&s->gb);
  163. metadata_type = get_bits(&s->gb, 7);
  164. metadata_size = get_bits_long(&s->gb, 24);
  165. if (get_bits_count(&s->gb) + 8*metadata_size > s->gb.size_in_bits) {
  166. skip_bits_long(&s->gb, initial_pos - get_bits_count(&s->gb));
  167. break;
  168. }
  169. if (metadata_size) {
  170. switch (metadata_type) {
  171. case FLAC_METADATA_TYPE_STREAMINFO:
  172. ff_flac_parse_streaminfo(s->avctx, (FLACStreaminfo *)s,
  173. s->gb.buffer+get_bits_count(&s->gb)/8);
  174. streaminfo_updated = 1;
  175. default:
  176. for (i = 0; i < metadata_size; i++)
  177. skip_bits(&s->gb, 8);
  178. }
  179. }
  180. } while (!metadata_last);
  181. if (streaminfo_updated)
  182. allocate_buffers(s);
  183. return 1;
  184. }
  185. return 0;
  186. }
  187. static int decode_residuals(FLACContext *s, int channel, int pred_order)
  188. {
  189. int i, tmp, partition, method_type, rice_order;
  190. int sample = 0, samples;
  191. method_type = get_bits(&s->gb, 2);
  192. if (method_type > 1) {
  193. av_log(s->avctx, AV_LOG_ERROR, "illegal residual coding method %d\n",
  194. method_type);
  195. return -1;
  196. }
  197. rice_order = get_bits(&s->gb, 4);
  198. samples= s->blocksize >> rice_order;
  199. if (pred_order > samples) {
  200. av_log(s->avctx, AV_LOG_ERROR, "invalid predictor order: %i > %i\n",
  201. pred_order, samples);
  202. return -1;
  203. }
  204. sample=
  205. i= pred_order;
  206. for (partition = 0; partition < (1 << rice_order); partition++) {
  207. tmp = get_bits(&s->gb, method_type == 0 ? 4 : 5);
  208. if (tmp == (method_type == 0 ? 15 : 31)) {
  209. tmp = get_bits(&s->gb, 5);
  210. for (; i < samples; i++, sample++)
  211. s->decoded[channel][sample] = get_sbits(&s->gb, tmp);
  212. } else {
  213. for (; i < samples; i++, sample++) {
  214. s->decoded[channel][sample] = get_sr_golomb_flac(&s->gb, tmp, INT_MAX, 0);
  215. }
  216. }
  217. i= 0;
  218. }
  219. return 0;
  220. }
  221. static int decode_subframe_fixed(FLACContext *s, int channel, int pred_order)
  222. {
  223. const int blocksize = s->blocksize;
  224. int32_t *decoded = s->decoded[channel];
  225. int a, b, c, d, i;
  226. /* warm up samples */
  227. for (i = 0; i < pred_order; i++) {
  228. decoded[i] = get_sbits(&s->gb, s->curr_bps);
  229. }
  230. if (decode_residuals(s, channel, pred_order) < 0)
  231. return -1;
  232. if (pred_order > 0)
  233. a = decoded[pred_order-1];
  234. if (pred_order > 1)
  235. b = a - decoded[pred_order-2];
  236. if (pred_order > 2)
  237. c = b - decoded[pred_order-2] + decoded[pred_order-3];
  238. if (pred_order > 3)
  239. d = c - decoded[pred_order-2] + 2*decoded[pred_order-3] - decoded[pred_order-4];
  240. switch (pred_order) {
  241. case 0:
  242. break;
  243. case 1:
  244. for (i = pred_order; i < blocksize; i++)
  245. decoded[i] = a += decoded[i];
  246. break;
  247. case 2:
  248. for (i = pred_order; i < blocksize; i++)
  249. decoded[i] = a += b += decoded[i];
  250. break;
  251. case 3:
  252. for (i = pred_order; i < blocksize; i++)
  253. decoded[i] = a += b += c += decoded[i];
  254. break;
  255. case 4:
  256. for (i = pred_order; i < blocksize; i++)
  257. decoded[i] = a += b += c += d += decoded[i];
  258. break;
  259. default:
  260. av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order);
  261. return -1;
  262. }
  263. return 0;
  264. }
  265. static int decode_subframe_lpc(FLACContext *s, int channel, int pred_order)
  266. {
  267. int i, j;
  268. int coeff_prec, qlevel;
  269. int coeffs[pred_order];
  270. int32_t *decoded = s->decoded[channel];
  271. /* warm up samples */
  272. for (i = 0; i < pred_order; i++) {
  273. decoded[i] = get_sbits(&s->gb, s->curr_bps);
  274. }
  275. coeff_prec = get_bits(&s->gb, 4) + 1;
  276. if (coeff_prec == 16) {
  277. av_log(s->avctx, AV_LOG_ERROR, "invalid coeff precision\n");
  278. return -1;
  279. }
  280. qlevel = get_sbits(&s->gb, 5);
  281. if (qlevel < 0) {
  282. av_log(s->avctx, AV_LOG_ERROR, "qlevel %d not supported, maybe buggy stream\n",
  283. qlevel);
  284. return -1;
  285. }
  286. for (i = 0; i < pred_order; i++) {
  287. coeffs[i] = get_sbits(&s->gb, coeff_prec);
  288. }
  289. if (decode_residuals(s, channel, pred_order) < 0)
  290. return -1;
  291. if (s->bps > 16) {
  292. int64_t sum;
  293. for (i = pred_order; i < s->blocksize; i++) {
  294. sum = 0;
  295. for (j = 0; j < pred_order; j++)
  296. sum += (int64_t)coeffs[j] * decoded[i-j-1];
  297. decoded[i] += sum >> qlevel;
  298. }
  299. } else {
  300. for (i = pred_order; i < s->blocksize-1; i += 2) {
  301. int c;
  302. int d = decoded[i-pred_order];
  303. int s0 = 0, s1 = 0;
  304. for (j = pred_order-1; j > 0; j--) {
  305. c = coeffs[j];
  306. s0 += c*d;
  307. d = decoded[i-j];
  308. s1 += c*d;
  309. }
  310. c = coeffs[0];
  311. s0 += c*d;
  312. d = decoded[i] += s0 >> qlevel;
  313. s1 += c*d;
  314. decoded[i+1] += s1 >> qlevel;
  315. }
  316. if (i < s->blocksize) {
  317. int sum = 0;
  318. for (j = 0; j < pred_order; j++)
  319. sum += coeffs[j] * decoded[i-j-1];
  320. decoded[i] += sum >> qlevel;
  321. }
  322. }
  323. return 0;
  324. }
  325. static inline int decode_subframe(FLACContext *s, int channel)
  326. {
  327. int type, wasted = 0;
  328. int i, tmp;
  329. s->curr_bps = s->bps;
  330. if (channel == 0) {
  331. if (s->decorrelation == RIGHT_SIDE)
  332. s->curr_bps++;
  333. } else {
  334. if (s->decorrelation == LEFT_SIDE || s->decorrelation == MID_SIDE)
  335. s->curr_bps++;
  336. }
  337. if (get_bits1(&s->gb)) {
  338. av_log(s->avctx, AV_LOG_ERROR, "invalid subframe padding\n");
  339. return -1;
  340. }
  341. type = get_bits(&s->gb, 6);
  342. if (get_bits1(&s->gb)) {
  343. wasted = 1;
  344. while (!get_bits1(&s->gb))
  345. wasted++;
  346. s->curr_bps -= wasted;
  347. }
  348. //FIXME use av_log2 for types
  349. if (type == 0) {
  350. tmp = get_sbits(&s->gb, s->curr_bps);
  351. for (i = 0; i < s->blocksize; i++)
  352. s->decoded[channel][i] = tmp;
  353. } else if (type == 1) {
  354. for (i = 0; i < s->blocksize; i++)
  355. s->decoded[channel][i] = get_sbits(&s->gb, s->curr_bps);
  356. } else if ((type >= 8) && (type <= 12)) {
  357. if (decode_subframe_fixed(s, channel, type & ~0x8) < 0)
  358. return -1;
  359. } else if (type >= 32) {
  360. if (decode_subframe_lpc(s, channel, (type & ~0x20)+1) < 0)
  361. return -1;
  362. } else {
  363. av_log(s->avctx, AV_LOG_ERROR, "invalid coding type\n");
  364. return -1;
  365. }
  366. if (wasted) {
  367. int i;
  368. for (i = 0; i < s->blocksize; i++)
  369. s->decoded[channel][i] <<= wasted;
  370. }
  371. return 0;
  372. }
  373. static int decode_frame(FLACContext *s, int alloc_data_size)
  374. {
  375. int blocksize_code, sample_rate_code, sample_size_code, assignment, i, crc8;
  376. int decorrelation, bps, blocksize, samplerate;
  377. blocksize_code = get_bits(&s->gb, 4);
  378. sample_rate_code = get_bits(&s->gb, 4);
  379. assignment = get_bits(&s->gb, 4); /* channel assignment */
  380. if (assignment < 8 && s->channels == assignment+1)
  381. decorrelation = INDEPENDENT;
  382. else if (assignment >=8 && assignment < 11 && s->channels == 2)
  383. decorrelation = LEFT_SIDE + assignment - 8;
  384. else {
  385. av_log(s->avctx, AV_LOG_ERROR, "unsupported channel assignment %d (channels=%d)\n",
  386. assignment, s->channels);
  387. return -1;
  388. }
  389. sample_size_code = get_bits(&s->gb, 3);
  390. if (sample_size_code == 0)
  391. bps= s->bps;
  392. else if ((sample_size_code != 3) && (sample_size_code != 7))
  393. bps = sample_size_table[sample_size_code];
  394. else {
  395. av_log(s->avctx, AV_LOG_ERROR, "invalid sample size code (%d)\n",
  396. sample_size_code);
  397. return -1;
  398. }
  399. if (get_bits1(&s->gb)) {
  400. av_log(s->avctx, AV_LOG_ERROR, "broken stream, invalid padding\n");
  401. return -1;
  402. }
  403. if (get_utf8(&s->gb) < 0) {
  404. av_log(s->avctx, AV_LOG_ERROR, "utf8 fscked\n");
  405. return -1;
  406. }
  407. if (blocksize_code == 0)
  408. blocksize = s->min_blocksize;
  409. else if (blocksize_code == 6)
  410. blocksize = get_bits(&s->gb, 8)+1;
  411. else if (blocksize_code == 7)
  412. blocksize = get_bits(&s->gb, 16)+1;
  413. else
  414. blocksize = blocksize_table[blocksize_code];
  415. if (blocksize > s->max_blocksize) {
  416. av_log(s->avctx, AV_LOG_ERROR, "blocksize %d > %d\n", blocksize,
  417. s->max_blocksize);
  418. return -1;
  419. }
  420. if (blocksize * s->channels * sizeof(int16_t) > alloc_data_size)
  421. return -1;
  422. if (sample_rate_code == 0)
  423. samplerate= s->samplerate;
  424. else if (sample_rate_code < 12)
  425. samplerate = sample_rate_table[sample_rate_code];
  426. else if (sample_rate_code == 12)
  427. samplerate = get_bits(&s->gb, 8) * 1000;
  428. else if (sample_rate_code == 13)
  429. samplerate = get_bits(&s->gb, 16);
  430. else if (sample_rate_code == 14)
  431. samplerate = get_bits(&s->gb, 16) * 10;
  432. else {
  433. av_log(s->avctx, AV_LOG_ERROR, "illegal sample rate code %d\n",
  434. sample_rate_code);
  435. return -1;
  436. }
  437. skip_bits(&s->gb, 8);
  438. crc8 = av_crc(av_crc_get_table(AV_CRC_8_ATM), 0,
  439. s->gb.buffer, get_bits_count(&s->gb)/8);
  440. if (crc8) {
  441. av_log(s->avctx, AV_LOG_ERROR, "header crc mismatch crc=%2X\n", crc8);
  442. return -1;
  443. }
  444. s->blocksize = blocksize;
  445. s->samplerate = samplerate;
  446. s->bps = bps;
  447. s->decorrelation= decorrelation;
  448. // dump_headers(s->avctx, (FLACStreaminfo *)s);
  449. /* subframes */
  450. for (i = 0; i < s->channels; i++) {
  451. if (decode_subframe(s, i) < 0)
  452. return -1;
  453. }
  454. align_get_bits(&s->gb);
  455. /* frame footer */
  456. skip_bits(&s->gb, 16); /* data crc */
  457. return 0;
  458. }
  459. static int flac_decode_frame(AVCodecContext *avctx,
  460. void *data, int *data_size,
  461. const uint8_t *buf, int buf_size)
  462. {
  463. FLACContext *s = avctx->priv_data;
  464. int tmp = 0, i, j = 0, input_buf_size = 0;
  465. int16_t *samples = data;
  466. int alloc_data_size= *data_size;
  467. *data_size=0;
  468. if (s->max_framesize == 0) {
  469. s->max_framesize= FFMAX(4, buf_size); // should hopefully be enough for the first header
  470. s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);
  471. }
  472. if (1 && s->max_framesize) { //FIXME truncated
  473. if (s->bitstream_size < 4 || AV_RL32(s->bitstream) != MKTAG('f','L','a','C'))
  474. buf_size= FFMIN(buf_size, s->max_framesize - FFMIN(s->bitstream_size, s->max_framesize));
  475. input_buf_size= buf_size;
  476. if (s->bitstream_size + buf_size < buf_size || s->bitstream_index + s->bitstream_size + buf_size < s->bitstream_index)
  477. return -1;
  478. if (s->allocated_bitstream_size < s->bitstream_size + buf_size)
  479. s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->bitstream_size + buf_size);
  480. if (s->bitstream_index + s->bitstream_size + buf_size > s->allocated_bitstream_size) {
  481. memmove(s->bitstream, &s->bitstream[s->bitstream_index],
  482. s->bitstream_size);
  483. s->bitstream_index=0;
  484. }
  485. memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size],
  486. buf, buf_size);
  487. buf= &s->bitstream[s->bitstream_index];
  488. buf_size += s->bitstream_size;
  489. s->bitstream_size= buf_size;
  490. if (buf_size < s->max_framesize && input_buf_size) {
  491. return input_buf_size;
  492. }
  493. }
  494. init_get_bits(&s->gb, buf, buf_size*8);
  495. if (metadata_parse(s))
  496. goto end;
  497. tmp = show_bits(&s->gb, 16);
  498. if ((tmp & 0xFFFE) != 0xFFF8) {
  499. av_log(s->avctx, AV_LOG_ERROR, "FRAME HEADER not here\n");
  500. while (get_bits_count(&s->gb)/8+2 < buf_size && (show_bits(&s->gb, 16) & 0xFFFE) != 0xFFF8)
  501. skip_bits(&s->gb, 8);
  502. goto end; // we may not have enough bits left to decode a frame, so try next time
  503. }
  504. skip_bits(&s->gb, 16);
  505. if (decode_frame(s, alloc_data_size) < 0) {
  506. av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n");
  507. s->bitstream_size=0;
  508. s->bitstream_index=0;
  509. return -1;
  510. }
  511. #define DECORRELATE(left, right)\
  512. assert(s->channels == 2);\
  513. for (i = 0; i < s->blocksize; i++) {\
  514. int a= s->decoded[0][i];\
  515. int b= s->decoded[1][i];\
  516. *samples++ = ((left) << (24 - s->bps)) >> 8;\
  517. *samples++ = ((right) << (24 - s->bps)) >> 8;\
  518. }\
  519. break;
  520. switch (s->decorrelation) {
  521. case INDEPENDENT:
  522. for (j = 0; j < s->blocksize; j++) {
  523. for (i = 0; i < s->channels; i++)
  524. *samples++ = (s->decoded[i][j] << (24 - s->bps)) >> 8;
  525. }
  526. break;
  527. case LEFT_SIDE:
  528. DECORRELATE(a,a-b)
  529. case RIGHT_SIDE:
  530. DECORRELATE(a+b,b)
  531. case MID_SIDE:
  532. DECORRELATE( (a-=b>>1) + b, a)
  533. }
  534. *data_size = (int8_t *)samples - (int8_t *)data;
  535. end:
  536. i= (get_bits_count(&s->gb)+7)/8;
  537. if (i > buf_size) {
  538. av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
  539. s->bitstream_size=0;
  540. s->bitstream_index=0;
  541. return -1;
  542. }
  543. if (s->bitstream_size) {
  544. s->bitstream_index += i;
  545. s->bitstream_size -= i;
  546. return input_buf_size;
  547. } else
  548. return i;
  549. }
  550. static av_cold int flac_decode_close(AVCodecContext *avctx)
  551. {
  552. FLACContext *s = avctx->priv_data;
  553. int i;
  554. for (i = 0; i < s->channels; i++) {
  555. av_freep(&s->decoded[i]);
  556. }
  557. av_freep(&s->bitstream);
  558. return 0;
  559. }
  560. static void flac_flush(AVCodecContext *avctx)
  561. {
  562. FLACContext *s = avctx->priv_data;
  563. s->bitstream_size=
  564. s->bitstream_index= 0;
  565. }
  566. AVCodec flac_decoder = {
  567. "flac",
  568. CODEC_TYPE_AUDIO,
  569. CODEC_ID_FLAC,
  570. sizeof(FLACContext),
  571. flac_decode_init,
  572. NULL,
  573. flac_decode_close,
  574. flac_decode_frame,
  575. CODEC_CAP_DELAY,
  576. .flush= flac_flush,
  577. .long_name= NULL_IF_CONFIG_SMALL("FLAC (Free Lossless Audio Codec)"),
  578. };