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.

759 lines
22KB

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