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.

780 lines
22KB

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