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.

805 lines
23KB

  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 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. if (s->bps > 16) {
  276. int64_t sum;
  277. for (i = pred_order; i < s->blocksize; i++)
  278. {
  279. sum = 0;
  280. for (j = 0; j < pred_order; j++)
  281. sum += (int64_t)coeffs[j] * s->decoded[channel][i-j-1];
  282. s->decoded[channel][i] += sum >> qlevel;
  283. }
  284. } else {
  285. int sum;
  286. for (i = pred_order; i < s->blocksize; i++)
  287. {
  288. sum = 0;
  289. for (j = 0; j < pred_order; j++)
  290. sum += coeffs[j] * s->decoded[channel][i-j-1];
  291. s->decoded[channel][i] += sum >> qlevel;
  292. }
  293. }
  294. return 0;
  295. }
  296. static inline int decode_subframe(FLACContext *s, int channel)
  297. {
  298. int type, wasted = 0;
  299. int i, tmp;
  300. s->curr_bps = s->bps;
  301. if(channel == 0){
  302. if(s->decorrelation == RIGHT_SIDE)
  303. s->curr_bps++;
  304. }else{
  305. if(s->decorrelation == LEFT_SIDE || s->decorrelation == MID_SIDE)
  306. s->curr_bps++;
  307. }
  308. if (get_bits1(&s->gb))
  309. {
  310. av_log(s->avctx, AV_LOG_ERROR, "invalid subframe padding\n");
  311. return -1;
  312. }
  313. type = get_bits(&s->gb, 6);
  314. // wasted = get_bits1(&s->gb);
  315. // if (wasted)
  316. // {
  317. // while (!get_bits1(&s->gb))
  318. // wasted++;
  319. // if (wasted)
  320. // wasted++;
  321. // s->curr_bps -= wasted;
  322. // }
  323. #if 0
  324. wasted= 16 - av_log2(show_bits(&s->gb, 17));
  325. skip_bits(&s->gb, wasted+1);
  326. s->curr_bps -= wasted;
  327. #else
  328. if (get_bits1(&s->gb))
  329. {
  330. wasted = 1;
  331. while (!get_bits1(&s->gb))
  332. wasted++;
  333. s->curr_bps -= wasted;
  334. av_log(s->avctx, AV_LOG_DEBUG, "%d wasted bits\n", wasted);
  335. }
  336. #endif
  337. //FIXME use av_log2 for types
  338. if (type == 0)
  339. {
  340. av_log(s->avctx, AV_LOG_DEBUG, "coding type: constant\n");
  341. tmp = get_sbits(&s->gb, s->curr_bps);
  342. for (i = 0; i < s->blocksize; i++)
  343. s->decoded[channel][i] = tmp;
  344. }
  345. else if (type == 1)
  346. {
  347. av_log(s->avctx, AV_LOG_DEBUG, "coding type: verbatim\n");
  348. for (i = 0; i < s->blocksize; i++)
  349. s->decoded[channel][i] = get_sbits(&s->gb, s->curr_bps);
  350. }
  351. else if ((type >= 8) && (type <= 12))
  352. {
  353. // av_log(s->avctx, AV_LOG_DEBUG, "coding type: fixed\n");
  354. if (decode_subframe_fixed(s, channel, type & ~0x8) < 0)
  355. return -1;
  356. }
  357. else if (type >= 32)
  358. {
  359. // av_log(s->avctx, AV_LOG_DEBUG, "coding type: lpc\n");
  360. if (decode_subframe_lpc(s, channel, (type & ~0x20)+1) < 0)
  361. return -1;
  362. }
  363. else
  364. {
  365. av_log(s->avctx, AV_LOG_ERROR, "invalid coding type\n");
  366. return -1;
  367. }
  368. if (wasted)
  369. {
  370. int i;
  371. for (i = 0; i < s->blocksize; i++)
  372. s->decoded[channel][i] <<= wasted;
  373. }
  374. return 0;
  375. }
  376. static int decode_frame(FLACContext *s)
  377. {
  378. int blocksize_code, sample_rate_code, sample_size_code, assignment, i, crc8;
  379. int decorrelation, bps, blocksize, samplerate;
  380. blocksize_code = get_bits(&s->gb, 4);
  381. sample_rate_code = get_bits(&s->gb, 4);
  382. assignment = get_bits(&s->gb, 4); /* channel assignment */
  383. if (assignment < 8 && s->channels == assignment+1)
  384. decorrelation = INDEPENDENT;
  385. else if (assignment >=8 && assignment < 11 && s->channels == 2)
  386. decorrelation = LEFT_SIDE + assignment - 8;
  387. else
  388. {
  389. av_log(s->avctx, AV_LOG_ERROR, "unsupported channel assignment %d (channels=%d)\n", assignment, s->channels);
  390. return -1;
  391. }
  392. sample_size_code = get_bits(&s->gb, 3);
  393. if(sample_size_code == 0)
  394. bps= s->bps;
  395. else if((sample_size_code != 3) && (sample_size_code != 7))
  396. bps = sample_size_table[sample_size_code];
  397. else
  398. {
  399. av_log(s->avctx, AV_LOG_ERROR, "invalid sample size code (%d)\n", sample_size_code);
  400. return -1;
  401. }
  402. if (get_bits1(&s->gb))
  403. {
  404. av_log(s->avctx, AV_LOG_ERROR, "broken stream, invalid padding\n");
  405. return -1;
  406. }
  407. if(get_utf8(&s->gb) < 0){
  408. av_log(s->avctx, AV_LOG_ERROR, "utf8 fscked\n");
  409. return -1;
  410. }
  411. #if 0
  412. if (/*((blocksize_code == 6) || (blocksize_code == 7)) &&*/
  413. (s->min_blocksize != s->max_blocksize)){
  414. }else{
  415. }
  416. #endif
  417. if (blocksize_code == 0)
  418. blocksize = s->min_blocksize;
  419. else if (blocksize_code == 6)
  420. blocksize = get_bits(&s->gb, 8)+1;
  421. else if (blocksize_code == 7)
  422. blocksize = get_bits(&s->gb, 16)+1;
  423. else
  424. blocksize = blocksize_table[blocksize_code];
  425. if(blocksize > s->max_blocksize){
  426. av_log(s->avctx, AV_LOG_ERROR, "blocksize %d > %d\n", blocksize, s->max_blocksize);
  427. return -1;
  428. }
  429. if (sample_rate_code == 0){
  430. samplerate= s->samplerate;
  431. }else if ((sample_rate_code > 3) && (sample_rate_code < 12))
  432. samplerate = sample_rate_table[sample_rate_code];
  433. else if (sample_rate_code == 12)
  434. samplerate = get_bits(&s->gb, 8) * 1000;
  435. else if (sample_rate_code == 13)
  436. samplerate = get_bits(&s->gb, 16);
  437. else if (sample_rate_code == 14)
  438. samplerate = get_bits(&s->gb, 16) * 10;
  439. else{
  440. av_log(s->avctx, AV_LOG_ERROR, "illegal sample rate code %d\n", sample_rate_code);
  441. return -1;
  442. }
  443. skip_bits(&s->gb, 8);
  444. crc8= av_crc(av_crc07, 0, s->gb.buffer, get_bits_count(&s->gb)/8);
  445. if(crc8){
  446. av_log(s->avctx, AV_LOG_ERROR, "header crc mismatch crc=%2X\n", crc8);
  447. return -1;
  448. }
  449. s->blocksize = blocksize;
  450. s->samplerate = samplerate;
  451. s->bps = bps;
  452. s->decorrelation= decorrelation;
  453. // dump_headers(s);
  454. /* subframes */
  455. for (i = 0; i < s->channels; i++)
  456. {
  457. // av_log(s->avctx, AV_LOG_DEBUG, "decoded: %x residual: %x\n", s->decoded[i], s->residual[i]);
  458. if (decode_subframe(s, i) < 0)
  459. return -1;
  460. }
  461. align_get_bits(&s->gb);
  462. /* frame footer */
  463. skip_bits(&s->gb, 16); /* data crc */
  464. return 0;
  465. }
  466. static inline int16_t shift_to_16_bits(int32_t data, int bps)
  467. {
  468. if (bps == 24) {
  469. return (data >> 8);
  470. } else if (bps == 20) {
  471. return (data >> 4);
  472. } else {
  473. return data;
  474. }
  475. }
  476. static int flac_decode_frame(AVCodecContext *avctx,
  477. void *data, int *data_size,
  478. uint8_t *buf, int buf_size)
  479. {
  480. FLACContext *s = avctx->priv_data;
  481. int metadata_last, metadata_type, metadata_size;
  482. int tmp = 0, i, j = 0, input_buf_size = 0;
  483. int16_t *samples = data;
  484. if(s->max_framesize == 0){
  485. s->max_framesize= 65536; // should hopefully be enough for the first header
  486. s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);
  487. }
  488. if(1 && s->max_framesize){//FIXME truncated
  489. buf_size= FFMAX(FFMIN(buf_size, s->max_framesize - s->bitstream_size), 0);
  490. input_buf_size= buf_size;
  491. if(s->bitstream_index + s->bitstream_size + buf_size > s->allocated_bitstream_size){
  492. // printf("memmove\n");
  493. memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);
  494. s->bitstream_index=0;
  495. }
  496. memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf, buf_size);
  497. buf= &s->bitstream[s->bitstream_index];
  498. buf_size += s->bitstream_size;
  499. s->bitstream_size= buf_size;
  500. if(buf_size < s->max_framesize){
  501. // printf("wanna more data ...\n");
  502. return input_buf_size;
  503. }
  504. }
  505. init_get_bits(&s->gb, buf, buf_size*8);
  506. /* fLaC signature (be) */
  507. if (show_bits_long(&s->gb, 32) == bswap_32(ff_get_fourcc("fLaC")))
  508. {
  509. skip_bits(&s->gb, 32);
  510. av_log(s->avctx, AV_LOG_DEBUG, "STREAM HEADER\n");
  511. do {
  512. metadata_last = get_bits(&s->gb, 1);
  513. metadata_type = get_bits(&s->gb, 7);
  514. metadata_size = get_bits_long(&s->gb, 24);
  515. av_log(s->avctx, AV_LOG_DEBUG, " metadata block: flag = %d, type = %d, size = %d\n",
  516. metadata_last, metadata_type,
  517. metadata_size);
  518. if(metadata_size){
  519. switch(metadata_type)
  520. {
  521. case METADATA_TYPE_STREAMINFO:{
  522. metadata_streaminfo(s);
  523. /* Buffer might have been reallocated, reinit bitreader */
  524. if(buf != &s->bitstream[s->bitstream_index])
  525. {
  526. int bits_count = get_bits_count(&s->gb);
  527. buf= &s->bitstream[s->bitstream_index];
  528. init_get_bits(&s->gb, buf, buf_size*8);
  529. skip_bits(&s->gb, bits_count);
  530. }
  531. dump_headers(s);
  532. break;}
  533. default:
  534. for(i=0; i<metadata_size; i++)
  535. skip_bits(&s->gb, 8);
  536. }
  537. }
  538. } while(!metadata_last);
  539. }
  540. else
  541. {
  542. tmp = show_bits(&s->gb, 16);
  543. if(tmp != 0xFFF8){
  544. av_log(s->avctx, AV_LOG_ERROR, "FRAME HEADER not here\n");
  545. while(get_bits_count(&s->gb)/8+2 < buf_size && show_bits(&s->gb, 16) != 0xFFF8)
  546. skip_bits(&s->gb, 8);
  547. goto end; // we may not have enough bits left to decode a frame, so try next time
  548. }
  549. skip_bits(&s->gb, 16);
  550. if (decode_frame(s) < 0){
  551. av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n");
  552. s->bitstream_size=0;
  553. s->bitstream_index=0;
  554. return -1;
  555. }
  556. }
  557. #if 0
  558. /* fix the channel order here */
  559. if (s->order == MID_SIDE)
  560. {
  561. short *left = samples;
  562. short *right = samples + s->blocksize;
  563. for (i = 0; i < s->blocksize; i += 2)
  564. {
  565. uint32_t x = s->decoded[0][i];
  566. uint32_t y = s->decoded[0][i+1];
  567. right[i] = x - (y / 2);
  568. left[i] = right[i] + y;
  569. }
  570. *data_size = 2 * s->blocksize;
  571. }
  572. else
  573. {
  574. for (i = 0; i < s->channels; i++)
  575. {
  576. switch(s->order)
  577. {
  578. case INDEPENDENT:
  579. for (j = 0; j < s->blocksize; j++)
  580. samples[(s->blocksize*i)+j] = s->decoded[i][j];
  581. break;
  582. case LEFT_SIDE:
  583. case RIGHT_SIDE:
  584. if (i == 0)
  585. for (j = 0; j < s->blocksize; j++)
  586. samples[(s->blocksize*i)+j] = s->decoded[0][j];
  587. else
  588. for (j = 0; j < s->blocksize; j++)
  589. samples[(s->blocksize*i)+j] = s->decoded[0][j] - s->decoded[i][j];
  590. break;
  591. // case MID_SIDE:
  592. // av_log(s->avctx, AV_LOG_DEBUG, "mid-side unsupported\n");
  593. }
  594. *data_size += s->blocksize;
  595. }
  596. }
  597. #else
  598. switch(s->decorrelation)
  599. {
  600. case INDEPENDENT:
  601. for (j = 0; j < s->blocksize; j++)
  602. {
  603. for (i = 0; i < s->channels; i++)
  604. *(samples++) = shift_to_16_bits(s->decoded[i][j], s->bps);
  605. }
  606. break;
  607. case LEFT_SIDE:
  608. assert(s->channels == 2);
  609. for (i = 0; i < s->blocksize; i++)
  610. {
  611. *(samples++) = shift_to_16_bits(s->decoded[0][i], s->bps);
  612. *(samples++) = shift_to_16_bits(s->decoded[0][i]
  613. - s->decoded[1][i], s->bps);
  614. }
  615. break;
  616. case RIGHT_SIDE:
  617. assert(s->channels == 2);
  618. for (i = 0; i < s->blocksize; i++)
  619. {
  620. *(samples++) = shift_to_16_bits(s->decoded[0][i]
  621. + s->decoded[1][i], s->bps);
  622. *(samples++) = shift_to_16_bits(s->decoded[1][i], s->bps);
  623. }
  624. break;
  625. case MID_SIDE:
  626. assert(s->channels == 2);
  627. for (i = 0; i < s->blocksize; i++)
  628. {
  629. int mid, side;
  630. mid = s->decoded[0][i];
  631. side = s->decoded[1][i];
  632. #if 1 //needs to be checked but IMHO it should be binary identical
  633. mid -= side>>1;
  634. *(samples++) = shift_to_16_bits(mid + side, s->bps);
  635. *(samples++) = shift_to_16_bits(mid, s->bps);
  636. #else
  637. mid <<= 1;
  638. if (side & 1)
  639. mid++;
  640. *(samples++) = (mid + side) >> 1;
  641. *(samples++) = (mid - side) >> 1;
  642. #endif
  643. }
  644. break;
  645. }
  646. #endif
  647. *data_size = (int8_t *)samples - (int8_t *)data;
  648. // av_log(s->avctx, AV_LOG_DEBUG, "data size: %d\n", *data_size);
  649. // s->last_blocksize = s->blocksize;
  650. end:
  651. i= (get_bits_count(&s->gb)+7)/8;;
  652. if(i > buf_size){
  653. av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
  654. s->bitstream_size=0;
  655. s->bitstream_index=0;
  656. return -1;
  657. }
  658. if(s->bitstream_size){
  659. s->bitstream_index += i;
  660. s->bitstream_size -= i;
  661. return input_buf_size;
  662. }else
  663. return i;
  664. }
  665. static int flac_decode_close(AVCodecContext *avctx)
  666. {
  667. FLACContext *s = avctx->priv_data;
  668. int i;
  669. for (i = 0; i < s->channels; i++)
  670. {
  671. av_freep(&s->decoded[i]);
  672. }
  673. av_freep(&s->bitstream);
  674. return 0;
  675. }
  676. static void flac_flush(AVCodecContext *avctx){
  677. FLACContext *s = avctx->priv_data;
  678. s->bitstream_size=
  679. s->bitstream_index= 0;
  680. }
  681. AVCodec flac_decoder = {
  682. "flac",
  683. CODEC_TYPE_AUDIO,
  684. CODEC_ID_FLAC,
  685. sizeof(FLACContext),
  686. flac_decode_init,
  687. NULL,
  688. flac_decode_close,
  689. flac_decode_frame,
  690. .flush= flac_flush,
  691. };