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.

712 lines
21KB

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