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.

744 lines
21KB

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