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.

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