flacdec.c 22 KB

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