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.

751 lines
23KB

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