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.

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