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.

742 lines
21KB

  1. /*
  2. * FLAC (Free Lossless Audio Codec) decoder
  3. * Copyright (c) 2003 Alex Beregszaszi
  4. *
  5. * This library is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2 of the License, or (at your option) any later version.
  9. *
  10. * This library is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with this library; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. /**
  20. * @file flac.c
  21. * FLAC (Free Lossless Audio Codec) decoder
  22. * @author Alex Beregszaszi
  23. *
  24. * For more information on the FLAC format, visit:
  25. * http://flac.sourceforge.net/
  26. *
  27. * This decoder can be used in 1 of 2 ways: Either raw FLAC data can be fed
  28. * through, starting from the initial 'fLaC' signature; or by passing the
  29. * 34-byte streaminfo structure through avctx->extradata[_size] followed
  30. * by data starting with the 0xFFF8 marker.
  31. */
  32. #include <limits.h>
  33. #define ALT_BITSTREAM_READER
  34. #include "avcodec.h"
  35. #include "bitstream.h"
  36. #include "golomb.h"
  37. #include "crc.h"
  38. #undef NDEBUG
  39. #include <assert.h>
  40. #define MAX_CHANNELS 8
  41. #define MAX_BLOCKSIZE 65535
  42. #define FLAC_STREAMINFO_SIZE 34
  43. enum decorrelation_type {
  44. INDEPENDENT,
  45. LEFT_SIDE,
  46. RIGHT_SIDE,
  47. MID_SIDE,
  48. };
  49. typedef struct FLACContext {
  50. AVCodecContext *avctx;
  51. GetBitContext gb;
  52. int min_blocksize, max_blocksize;
  53. int min_framesize, max_framesize;
  54. int samplerate, channels;
  55. int blocksize/*, last_blocksize*/;
  56. int bps, curr_bps;
  57. enum decorrelation_type decorrelation;
  58. int32_t *decoded[MAX_CHANNELS];
  59. uint8_t *bitstream;
  60. int bitstream_size;
  61. int bitstream_index;
  62. unsigned int allocated_bitstream_size;
  63. } FLACContext;
  64. #define METADATA_TYPE_STREAMINFO 0
  65. static int sample_rate_table[] =
  66. { 0, 0, 0, 0,
  67. 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000,
  68. 0, 0, 0, 0 };
  69. static int sample_size_table[] =
  70. { 0, 8, 12, 0, 16, 20, 24, 0 };
  71. static int blocksize_table[] = {
  72. 0, 192, 576<<0, 576<<1, 576<<2, 576<<3, 0, 0,
  73. 256<<0, 256<<1, 256<<2, 256<<3, 256<<4, 256<<5, 256<<6, 256<<7
  74. };
  75. static int64_t get_utf8(GetBitContext *gb){
  76. int64_t val;
  77. GET_UTF8(val, get_bits(gb, 8), return -1;)
  78. return val;
  79. }
  80. static void metadata_streaminfo(FLACContext *s);
  81. static void dump_headers(FLACContext *s);
  82. static int flac_decode_init(AVCodecContext * avctx)
  83. {
  84. FLACContext *s = avctx->priv_data;
  85. s->avctx = avctx;
  86. /* initialize based on the demuxer-supplied streamdata header */
  87. if (avctx->extradata_size == FLAC_STREAMINFO_SIZE) {
  88. init_get_bits(&s->gb, avctx->extradata, avctx->extradata_size*8);
  89. metadata_streaminfo(s);
  90. dump_headers(s);
  91. }
  92. return 0;
  93. }
  94. static void dump_headers(FLACContext *s)
  95. {
  96. av_log(s->avctx, AV_LOG_DEBUG, " Blocksize: %d .. %d (%d)\n", s->min_blocksize, s->max_blocksize, s->blocksize);
  97. av_log(s->avctx, AV_LOG_DEBUG, " Framesize: %d .. %d\n", s->min_framesize, s->max_framesize);
  98. av_log(s->avctx, AV_LOG_DEBUG, " Samplerate: %d\n", s->samplerate);
  99. av_log(s->avctx, AV_LOG_DEBUG, " Channels: %d\n", s->channels);
  100. av_log(s->avctx, AV_LOG_DEBUG, " Bits: %d\n", s->bps);
  101. }
  102. static void allocate_buffers(FLACContext *s){
  103. int i;
  104. assert(s->max_blocksize);
  105. if(s->max_framesize == 0 && s->max_blocksize){
  106. s->max_framesize= (s->channels * s->bps * s->max_blocksize + 7)/ 8; //FIXME header overhead
  107. }
  108. for (i = 0; i < s->channels; i++)
  109. {
  110. s->decoded[i] = av_realloc(s->decoded[i], sizeof(int32_t)*s->max_blocksize);
  111. }
  112. s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);
  113. }
  114. static void metadata_streaminfo(FLACContext *s)
  115. {
  116. /* mandatory streaminfo */
  117. s->min_blocksize = get_bits(&s->gb, 16);
  118. s->max_blocksize = get_bits(&s->gb, 16);
  119. s->min_framesize = get_bits_long(&s->gb, 24);
  120. s->max_framesize = get_bits_long(&s->gb, 24);
  121. s->samplerate = get_bits_long(&s->gb, 20);
  122. s->channels = get_bits(&s->gb, 3) + 1;
  123. s->bps = get_bits(&s->gb, 5) + 1;
  124. s->avctx->channels = s->channels;
  125. s->avctx->sample_rate = s->samplerate;
  126. skip_bits(&s->gb, 36); /* total num of samples */
  127. skip_bits(&s->gb, 64); /* md5 sum */
  128. skip_bits(&s->gb, 64); /* md5 sum */
  129. allocate_buffers(s);
  130. }
  131. static int decode_residuals(FLACContext *s, int channel, int pred_order)
  132. {
  133. int i, tmp, partition, method_type, rice_order;
  134. int sample = 0, samples;
  135. method_type = get_bits(&s->gb, 2);
  136. if (method_type != 0){
  137. av_log(s->avctx, AV_LOG_DEBUG, "illegal residual coding method %d\n", method_type);
  138. return -1;
  139. }
  140. rice_order = get_bits(&s->gb, 4);
  141. samples= s->blocksize >> rice_order;
  142. sample=
  143. i= pred_order;
  144. for (partition = 0; partition < (1 << rice_order); partition++)
  145. {
  146. tmp = get_bits(&s->gb, 4);
  147. if (tmp == 15)
  148. {
  149. av_log(s->avctx, AV_LOG_DEBUG, "fixed len partition\n");
  150. tmp = get_bits(&s->gb, 5);
  151. for (; i < samples; i++, sample++)
  152. s->decoded[channel][sample] = get_sbits(&s->gb, tmp);
  153. }
  154. else
  155. {
  156. // av_log(s->avctx, AV_LOG_DEBUG, "rice coded partition k=%d\n", tmp);
  157. for (; i < samples; i++, sample++){
  158. s->decoded[channel][sample] = get_sr_golomb_flac(&s->gb, tmp, INT_MAX, 0);
  159. }
  160. }
  161. i= 0;
  162. }
  163. // av_log(s->avctx, AV_LOG_DEBUG, "partitions: %d, samples: %d\n", 1 << rice_order, sample);
  164. return 0;
  165. }
  166. static int decode_subframe_fixed(FLACContext *s, int channel, int pred_order)
  167. {
  168. int i;
  169. // av_log(s->avctx, AV_LOG_DEBUG, " SUBFRAME FIXED\n");
  170. /* warm up samples */
  171. // av_log(s->avctx, AV_LOG_DEBUG, " warm up samples: %d\n", pred_order);
  172. for (i = 0; i < pred_order; i++)
  173. {
  174. s->decoded[channel][i] = get_sbits(&s->gb, s->curr_bps);
  175. // av_log(s->avctx, AV_LOG_DEBUG, " %d: %d\n", i, s->decoded[channel][i]);
  176. }
  177. if (decode_residuals(s, channel, pred_order) < 0)
  178. return -1;
  179. switch(pred_order)
  180. {
  181. case 0:
  182. break;
  183. case 1:
  184. for (i = pred_order; i < s->blocksize; i++)
  185. s->decoded[channel][i] += s->decoded[channel][i-1];
  186. break;
  187. case 2:
  188. for (i = pred_order; i < s->blocksize; i++)
  189. s->decoded[channel][i] += 2*s->decoded[channel][i-1]
  190. - s->decoded[channel][i-2];
  191. break;
  192. case 3:
  193. for (i = pred_order; i < s->blocksize; i++)
  194. s->decoded[channel][i] += 3*s->decoded[channel][i-1]
  195. - 3*s->decoded[channel][i-2]
  196. + s->decoded[channel][i-3];
  197. break;
  198. case 4:
  199. for (i = pred_order; i < s->blocksize; i++)
  200. s->decoded[channel][i] += 4*s->decoded[channel][i-1]
  201. - 6*s->decoded[channel][i-2]
  202. + 4*s->decoded[channel][i-3]
  203. - s->decoded[channel][i-4];
  204. break;
  205. default:
  206. av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order);
  207. return -1;
  208. }
  209. return 0;
  210. }
  211. static int decode_subframe_lpc(FLACContext *s, int channel, int pred_order)
  212. {
  213. int i, j;
  214. int coeff_prec, qlevel;
  215. int coeffs[pred_order];
  216. // av_log(s->avctx, AV_LOG_DEBUG, " SUBFRAME LPC\n");
  217. /* warm up samples */
  218. // av_log(s->avctx, AV_LOG_DEBUG, " warm up samples: %d\n", pred_order);
  219. for (i = 0; i < pred_order; i++)
  220. {
  221. s->decoded[channel][i] = get_sbits(&s->gb, s->curr_bps);
  222. // av_log(s->avctx, AV_LOG_DEBUG, " %d: %d\n", i, s->decoded[channel][i]);
  223. }
  224. coeff_prec = get_bits(&s->gb, 4) + 1;
  225. if (coeff_prec == 16)
  226. {
  227. av_log(s->avctx, AV_LOG_DEBUG, "invalid coeff precision\n");
  228. return -1;
  229. }
  230. // av_log(s->avctx, AV_LOG_DEBUG, " qlp coeff prec: %d\n", coeff_prec);
  231. qlevel = get_sbits(&s->gb, 5);
  232. // av_log(s->avctx, AV_LOG_DEBUG, " quant level: %d\n", qlevel);
  233. if(qlevel < 0){
  234. av_log(s->avctx, AV_LOG_DEBUG, "qlevel %d not supported, maybe buggy stream\n", qlevel);
  235. return -1;
  236. }
  237. for (i = 0; i < pred_order; i++)
  238. {
  239. coeffs[i] = get_sbits(&s->gb, coeff_prec);
  240. // av_log(s->avctx, AV_LOG_DEBUG, " %d: %d\n", i, coeffs[i]);
  241. }
  242. if (decode_residuals(s, channel, pred_order) < 0)
  243. return -1;
  244. if (s->bps > 16) {
  245. int64_t sum;
  246. for (i = pred_order; i < s->blocksize; i++)
  247. {
  248. sum = 0;
  249. for (j = 0; j < pred_order; j++)
  250. sum += (int64_t)coeffs[j] * s->decoded[channel][i-j-1];
  251. s->decoded[channel][i] += sum >> qlevel;
  252. }
  253. } else {
  254. int sum;
  255. for (i = pred_order; i < s->blocksize; i++)
  256. {
  257. sum = 0;
  258. for (j = 0; j < pred_order; j++)
  259. sum += coeffs[j] * s->decoded[channel][i-j-1];
  260. s->decoded[channel][i] += sum >> qlevel;
  261. }
  262. }
  263. return 0;
  264. }
  265. static inline int decode_subframe(FLACContext *s, int channel)
  266. {
  267. int type, wasted = 0;
  268. int i, tmp;
  269. s->curr_bps = s->bps;
  270. if(channel == 0){
  271. if(s->decorrelation == RIGHT_SIDE)
  272. s->curr_bps++;
  273. }else{
  274. if(s->decorrelation == LEFT_SIDE || s->decorrelation == MID_SIDE)
  275. s->curr_bps++;
  276. }
  277. if (get_bits1(&s->gb))
  278. {
  279. av_log(s->avctx, AV_LOG_ERROR, "invalid subframe padding\n");
  280. return -1;
  281. }
  282. type = get_bits(&s->gb, 6);
  283. // wasted = get_bits1(&s->gb);
  284. // if (wasted)
  285. // {
  286. // while (!get_bits1(&s->gb))
  287. // wasted++;
  288. // if (wasted)
  289. // wasted++;
  290. // s->curr_bps -= wasted;
  291. // }
  292. #if 0
  293. wasted= 16 - av_log2(show_bits(&s->gb, 17));
  294. skip_bits(&s->gb, wasted+1);
  295. s->curr_bps -= wasted;
  296. #else
  297. if (get_bits1(&s->gb))
  298. {
  299. wasted = 1;
  300. while (!get_bits1(&s->gb))
  301. wasted++;
  302. s->curr_bps -= wasted;
  303. av_log(s->avctx, AV_LOG_DEBUG, "%d wasted bits\n", wasted);
  304. }
  305. #endif
  306. //FIXME use av_log2 for types
  307. if (type == 0)
  308. {
  309. av_log(s->avctx, AV_LOG_DEBUG, "coding type: constant\n");
  310. tmp = get_sbits(&s->gb, s->curr_bps);
  311. for (i = 0; i < s->blocksize; i++)
  312. s->decoded[channel][i] = tmp;
  313. }
  314. else if (type == 1)
  315. {
  316. av_log(s->avctx, AV_LOG_DEBUG, "coding type: verbatim\n");
  317. for (i = 0; i < s->blocksize; i++)
  318. s->decoded[channel][i] = get_sbits(&s->gb, s->curr_bps);
  319. }
  320. else if ((type >= 8) && (type <= 12))
  321. {
  322. // av_log(s->avctx, AV_LOG_DEBUG, "coding type: fixed\n");
  323. if (decode_subframe_fixed(s, channel, type & ~0x8) < 0)
  324. return -1;
  325. }
  326. else if (type >= 32)
  327. {
  328. // av_log(s->avctx, AV_LOG_DEBUG, "coding type: lpc\n");
  329. if (decode_subframe_lpc(s, channel, (type & ~0x20)+1) < 0)
  330. return -1;
  331. }
  332. else
  333. {
  334. av_log(s->avctx, AV_LOG_ERROR, "invalid coding type\n");
  335. return -1;
  336. }
  337. if (wasted)
  338. {
  339. int i;
  340. for (i = 0; i < s->blocksize; i++)
  341. s->decoded[channel][i] <<= wasted;
  342. }
  343. return 0;
  344. }
  345. static int decode_frame(FLACContext *s)
  346. {
  347. int blocksize_code, sample_rate_code, sample_size_code, assignment, i, crc8;
  348. int decorrelation, bps, blocksize, samplerate;
  349. blocksize_code = get_bits(&s->gb, 4);
  350. sample_rate_code = get_bits(&s->gb, 4);
  351. assignment = get_bits(&s->gb, 4); /* channel assignment */
  352. if (assignment < 8 && s->channels == assignment+1)
  353. decorrelation = INDEPENDENT;
  354. else if (assignment >=8 && assignment < 11 && s->channels == 2)
  355. decorrelation = LEFT_SIDE + assignment - 8;
  356. else
  357. {
  358. av_log(s->avctx, AV_LOG_ERROR, "unsupported channel assignment %d (channels=%d)\n", assignment, s->channels);
  359. return -1;
  360. }
  361. sample_size_code = get_bits(&s->gb, 3);
  362. if(sample_size_code == 0)
  363. bps= s->bps;
  364. else if((sample_size_code != 3) && (sample_size_code != 7))
  365. bps = sample_size_table[sample_size_code];
  366. else
  367. {
  368. av_log(s->avctx, AV_LOG_ERROR, "invalid sample size code (%d)\n", sample_size_code);
  369. return -1;
  370. }
  371. if (get_bits1(&s->gb))
  372. {
  373. av_log(s->avctx, AV_LOG_ERROR, "broken stream, invalid padding\n");
  374. return -1;
  375. }
  376. if(get_utf8(&s->gb) < 0){
  377. av_log(s->avctx, AV_LOG_ERROR, "utf8 fscked\n");
  378. return -1;
  379. }
  380. #if 0
  381. if (/*((blocksize_code == 6) || (blocksize_code == 7)) &&*/
  382. (s->min_blocksize != s->max_blocksize)){
  383. }else{
  384. }
  385. #endif
  386. if (blocksize_code == 0)
  387. blocksize = s->min_blocksize;
  388. else if (blocksize_code == 6)
  389. blocksize = get_bits(&s->gb, 8)+1;
  390. else if (blocksize_code == 7)
  391. blocksize = get_bits(&s->gb, 16)+1;
  392. else
  393. blocksize = blocksize_table[blocksize_code];
  394. if(blocksize > s->max_blocksize){
  395. av_log(s->avctx, AV_LOG_ERROR, "blocksize %d > %d\n", blocksize, s->max_blocksize);
  396. return -1;
  397. }
  398. if (sample_rate_code == 0){
  399. samplerate= s->samplerate;
  400. }else if ((sample_rate_code > 3) && (sample_rate_code < 12))
  401. samplerate = sample_rate_table[sample_rate_code];
  402. else if (sample_rate_code == 12)
  403. samplerate = get_bits(&s->gb, 8) * 1000;
  404. else if (sample_rate_code == 13)
  405. samplerate = get_bits(&s->gb, 16);
  406. else if (sample_rate_code == 14)
  407. samplerate = get_bits(&s->gb, 16) * 10;
  408. else{
  409. av_log(s->avctx, AV_LOG_ERROR, "illegal sample rate code %d\n", sample_rate_code);
  410. return -1;
  411. }
  412. skip_bits(&s->gb, 8);
  413. crc8= av_crc(av_crc07, 0, s->gb.buffer, get_bits_count(&s->gb)/8);
  414. if(crc8){
  415. av_log(s->avctx, AV_LOG_ERROR, "header crc mismatch crc=%2X\n", crc8);
  416. return -1;
  417. }
  418. s->blocksize = blocksize;
  419. s->samplerate = samplerate;
  420. s->bps = bps;
  421. s->decorrelation= decorrelation;
  422. // dump_headers(s);
  423. /* subframes */
  424. for (i = 0; i < s->channels; i++)
  425. {
  426. // av_log(s->avctx, AV_LOG_DEBUG, "decoded: %x residual: %x\n", s->decoded[i], s->residual[i]);
  427. if (decode_subframe(s, i) < 0)
  428. return -1;
  429. }
  430. align_get_bits(&s->gb);
  431. /* frame footer */
  432. skip_bits(&s->gb, 16); /* data crc */
  433. return 0;
  434. }
  435. static inline int16_t shift_to_16_bits(int32_t data, int bps)
  436. {
  437. if (bps == 24) {
  438. return (data >> 8);
  439. } else if (bps == 20) {
  440. return (data >> 4);
  441. } else {
  442. return data;
  443. }
  444. }
  445. static int flac_decode_frame(AVCodecContext *avctx,
  446. void *data, int *data_size,
  447. uint8_t *buf, int buf_size)
  448. {
  449. FLACContext *s = avctx->priv_data;
  450. int metadata_last, metadata_type, metadata_size;
  451. int tmp = 0, i, j = 0, input_buf_size = 0;
  452. int16_t *samples = data;
  453. if(s->max_framesize == 0){
  454. s->max_framesize= 65536; // should hopefully be enough for the first header
  455. s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);
  456. }
  457. if(1 && s->max_framesize){//FIXME truncated
  458. buf_size= FFMAX(FFMIN(buf_size, s->max_framesize - s->bitstream_size), 0);
  459. input_buf_size= buf_size;
  460. if(s->bitstream_index + s->bitstream_size + buf_size > s->allocated_bitstream_size){
  461. // printf("memmove\n");
  462. memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);
  463. s->bitstream_index=0;
  464. }
  465. memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf, buf_size);
  466. buf= &s->bitstream[s->bitstream_index];
  467. buf_size += s->bitstream_size;
  468. s->bitstream_size= buf_size;
  469. if(buf_size < s->max_framesize){
  470. // printf("wanna more data ...\n");
  471. return input_buf_size;
  472. }
  473. }
  474. init_get_bits(&s->gb, buf, buf_size*8);
  475. /* fLaC signature (be) */
  476. if (show_bits_long(&s->gb, 32) == bswap_32(ff_get_fourcc("fLaC")))
  477. {
  478. skip_bits(&s->gb, 32);
  479. av_log(s->avctx, AV_LOG_DEBUG, "STREAM HEADER\n");
  480. do {
  481. metadata_last = get_bits(&s->gb, 1);
  482. metadata_type = get_bits(&s->gb, 7);
  483. metadata_size = get_bits_long(&s->gb, 24);
  484. av_log(s->avctx, AV_LOG_DEBUG, " metadata block: flag = %d, type = %d, size = %d\n",
  485. metadata_last, metadata_type,
  486. metadata_size);
  487. if(metadata_size){
  488. switch(metadata_type)
  489. {
  490. case METADATA_TYPE_STREAMINFO:{
  491. metadata_streaminfo(s);
  492. /* Buffer might have been reallocated, reinit bitreader */
  493. if(buf != &s->bitstream[s->bitstream_index])
  494. {
  495. int bits_count = get_bits_count(&s->gb);
  496. buf= &s->bitstream[s->bitstream_index];
  497. init_get_bits(&s->gb, buf, buf_size*8);
  498. skip_bits(&s->gb, bits_count);
  499. }
  500. dump_headers(s);
  501. break;}
  502. default:
  503. for(i=0; i<metadata_size; i++)
  504. skip_bits(&s->gb, 8);
  505. }
  506. }
  507. } while(!metadata_last);
  508. }
  509. else
  510. {
  511. tmp = show_bits(&s->gb, 16);
  512. if(tmp != 0xFFF8){
  513. av_log(s->avctx, AV_LOG_ERROR, "FRAME HEADER not here\n");
  514. while(get_bits_count(&s->gb)/8+2 < buf_size && show_bits(&s->gb, 16) != 0xFFF8)
  515. skip_bits(&s->gb, 8);
  516. goto end; // we may not have enough bits left to decode a frame, so try next time
  517. }
  518. skip_bits(&s->gb, 16);
  519. if (decode_frame(s) < 0){
  520. av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n");
  521. s->bitstream_size=0;
  522. s->bitstream_index=0;
  523. return -1;
  524. }
  525. }
  526. #if 0
  527. /* fix the channel order here */
  528. if (s->order == MID_SIDE)
  529. {
  530. short *left = samples;
  531. short *right = samples + s->blocksize;
  532. for (i = 0; i < s->blocksize; i += 2)
  533. {
  534. uint32_t x = s->decoded[0][i];
  535. uint32_t y = s->decoded[0][i+1];
  536. right[i] = x - (y / 2);
  537. left[i] = right[i] + y;
  538. }
  539. *data_size = 2 * s->blocksize;
  540. }
  541. else
  542. {
  543. for (i = 0; i < s->channels; i++)
  544. {
  545. switch(s->order)
  546. {
  547. case INDEPENDENT:
  548. for (j = 0; j < s->blocksize; j++)
  549. samples[(s->blocksize*i)+j] = s->decoded[i][j];
  550. break;
  551. case LEFT_SIDE:
  552. case RIGHT_SIDE:
  553. if (i == 0)
  554. for (j = 0; j < s->blocksize; j++)
  555. samples[(s->blocksize*i)+j] = s->decoded[0][j];
  556. else
  557. for (j = 0; j < s->blocksize; j++)
  558. samples[(s->blocksize*i)+j] = s->decoded[0][j] - s->decoded[i][j];
  559. break;
  560. // case MID_SIDE:
  561. // av_log(s->avctx, AV_LOG_DEBUG, "mid-side unsupported\n");
  562. }
  563. *data_size += s->blocksize;
  564. }
  565. }
  566. #else
  567. #define DECORRELATE(left, right)\
  568. assert(s->channels == 2);\
  569. for (i = 0; i < s->blocksize; i++)\
  570. {\
  571. int a= s->decoded[0][i];\
  572. int b= s->decoded[1][i];\
  573. *(samples++) = (left ) >> (16 - s->bps);\
  574. *(samples++) = (right) >> (16 - s->bps);\
  575. }\
  576. break;
  577. switch(s->decorrelation)
  578. {
  579. case INDEPENDENT:
  580. for (j = 0; j < s->blocksize; j++)
  581. {
  582. for (i = 0; i < s->channels; i++)
  583. *(samples++) = shift_to_16_bits(s->decoded[i][j], s->bps);
  584. }
  585. break;
  586. case LEFT_SIDE:
  587. DECORRELATE(a,a-b)
  588. case RIGHT_SIDE:
  589. DECORRELATE(a+b,b)
  590. case MID_SIDE:
  591. DECORRELATE( (a-=b>>1) + b, a)
  592. }
  593. #endif
  594. *data_size = (int8_t *)samples - (int8_t *)data;
  595. // av_log(s->avctx, AV_LOG_DEBUG, "data size: %d\n", *data_size);
  596. // s->last_blocksize = s->blocksize;
  597. end:
  598. i= (get_bits_count(&s->gb)+7)/8;;
  599. if(i > buf_size){
  600. av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
  601. s->bitstream_size=0;
  602. s->bitstream_index=0;
  603. return -1;
  604. }
  605. if(s->bitstream_size){
  606. s->bitstream_index += i;
  607. s->bitstream_size -= i;
  608. return input_buf_size;
  609. }else
  610. return i;
  611. }
  612. static int flac_decode_close(AVCodecContext *avctx)
  613. {
  614. FLACContext *s = avctx->priv_data;
  615. int i;
  616. for (i = 0; i < s->channels; i++)
  617. {
  618. av_freep(&s->decoded[i]);
  619. }
  620. av_freep(&s->bitstream);
  621. return 0;
  622. }
  623. static void flac_flush(AVCodecContext *avctx){
  624. FLACContext *s = avctx->priv_data;
  625. s->bitstream_size=
  626. s->bitstream_index= 0;
  627. }
  628. AVCodec flac_decoder = {
  629. "flac",
  630. CODEC_TYPE_AUDIO,
  631. CODEC_ID_FLAC,
  632. sizeof(FLACContext),
  633. flac_decode_init,
  634. NULL,
  635. flac_decode_close,
  636. flac_decode_frame,
  637. .flush= flac_flush,
  638. };