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.

813 lines
25KB

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