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.

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