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.

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