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.

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