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.

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