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.

739 lines
22KB

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