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.

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