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.

772 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_bits1(&s->gb);
  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 > 1){
  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. if (pred_order > samples) {
  188. av_log(s->avctx, AV_LOG_ERROR, "invalid predictor order: %i > %i\n", pred_order, samples);
  189. return -1;
  190. }
  191. sample=
  192. i= pred_order;
  193. for (partition = 0; partition < (1 << rice_order); partition++)
  194. {
  195. tmp = get_bits(&s->gb, method_type == 0 ? 4 : 5);
  196. if (tmp == (method_type == 0 ? 15 : 31))
  197. {
  198. av_log(s->avctx, AV_LOG_DEBUG, "fixed len partition\n");
  199. tmp = get_bits(&s->gb, 5);
  200. for (; i < samples; i++, sample++)
  201. s->decoded[channel][sample] = get_sbits(&s->gb, tmp);
  202. }
  203. else
  204. {
  205. // av_log(s->avctx, AV_LOG_DEBUG, "rice coded partition k=%d\n", tmp);
  206. for (; i < samples; i++, sample++){
  207. s->decoded[channel][sample] = get_sr_golomb_flac(&s->gb, tmp, INT_MAX, 0);
  208. }
  209. }
  210. i= 0;
  211. }
  212. // av_log(s->avctx, AV_LOG_DEBUG, "partitions: %d, samples: %d\n", 1 << rice_order, sample);
  213. return 0;
  214. }
  215. static int decode_subframe_fixed(FLACContext *s, int channel, int pred_order)
  216. {
  217. const int blocksize = s->blocksize;
  218. int32_t *decoded = s->decoded[channel];
  219. int a, b, c, d, i;
  220. // av_log(s->avctx, AV_LOG_DEBUG, " SUBFRAME FIXED\n");
  221. /* warm up samples */
  222. // av_log(s->avctx, AV_LOG_DEBUG, " warm up samples: %d\n", pred_order);
  223. for (i = 0; i < pred_order; i++)
  224. {
  225. decoded[i] = get_sbits(&s->gb, s->curr_bps);
  226. // av_log(s->avctx, AV_LOG_DEBUG, " %d: %d\n", i, s->decoded[channel][i]);
  227. }
  228. if (decode_residuals(s, channel, pred_order) < 0)
  229. return -1;
  230. a = decoded[pred_order-1];
  231. b = a - decoded[pred_order-2];
  232. c = b - decoded[pred_order-2] + decoded[pred_order-3];
  233. d = c - decoded[pred_order-2] + 2*decoded[pred_order-3] - decoded[pred_order-4];
  234. switch(pred_order)
  235. {
  236. case 0:
  237. break;
  238. case 1:
  239. for (i = pred_order; i < blocksize; i++)
  240. decoded[i] = a += decoded[i];
  241. break;
  242. case 2:
  243. for (i = pred_order; i < blocksize; i++)
  244. decoded[i] = a += b += decoded[i];
  245. break;
  246. case 3:
  247. for (i = pred_order; i < blocksize; i++)
  248. decoded[i] = a += b += c += decoded[i];
  249. break;
  250. case 4:
  251. for (i = pred_order; i < blocksize; i++)
  252. decoded[i] = a += b += c += d += decoded[i];
  253. break;
  254. default:
  255. av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order);
  256. return -1;
  257. }
  258. return 0;
  259. }
  260. static int decode_subframe_lpc(FLACContext *s, int channel, int pred_order)
  261. {
  262. int i, j;
  263. int coeff_prec, qlevel;
  264. int coeffs[pred_order];
  265. int32_t *decoded = s->decoded[channel];
  266. // av_log(s->avctx, AV_LOG_DEBUG, " SUBFRAME LPC\n");
  267. /* warm up samples */
  268. // av_log(s->avctx, AV_LOG_DEBUG, " warm up samples: %d\n", pred_order);
  269. for (i = 0; i < pred_order; i++)
  270. {
  271. decoded[i] = get_sbits(&s->gb, s->curr_bps);
  272. // av_log(s->avctx, AV_LOG_DEBUG, " %d: %d\n", i, decoded[i]);
  273. }
  274. coeff_prec = get_bits(&s->gb, 4) + 1;
  275. if (coeff_prec == 16)
  276. {
  277. av_log(s->avctx, AV_LOG_DEBUG, "invalid coeff precision\n");
  278. return -1;
  279. }
  280. // av_log(s->avctx, AV_LOG_DEBUG, " qlp coeff prec: %d\n", coeff_prec);
  281. qlevel = get_sbits(&s->gb, 5);
  282. // av_log(s->avctx, AV_LOG_DEBUG, " quant level: %d\n", qlevel);
  283. if(qlevel < 0){
  284. av_log(s->avctx, AV_LOG_DEBUG, "qlevel %d not supported, maybe buggy stream\n", qlevel);
  285. return -1;
  286. }
  287. for (i = 0; i < pred_order; i++)
  288. {
  289. coeffs[i] = get_sbits(&s->gb, coeff_prec);
  290. // av_log(s->avctx, AV_LOG_DEBUG, " %d: %d\n", i, coeffs[i]);
  291. }
  292. if (decode_residuals(s, channel, pred_order) < 0)
  293. return -1;
  294. if (s->bps > 16) {
  295. int64_t sum;
  296. for (i = pred_order; i < s->blocksize; i++)
  297. {
  298. sum = 0;
  299. for (j = 0; j < pred_order; j++)
  300. sum += (int64_t)coeffs[j] * decoded[i-j-1];
  301. decoded[i] += sum >> qlevel;
  302. }
  303. } else {
  304. for (i = pred_order; i < s->blocksize-1; i += 2)
  305. {
  306. int c;
  307. int d = decoded[i-pred_order];
  308. int s0 = 0, s1 = 0;
  309. for (j = pred_order-1; j > 0; j--)
  310. {
  311. c = coeffs[j];
  312. s0 += c*d;
  313. d = decoded[i-j];
  314. s1 += c*d;
  315. }
  316. c = coeffs[0];
  317. s0 += c*d;
  318. d = decoded[i] += s0 >> qlevel;
  319. s1 += c*d;
  320. decoded[i+1] += s1 >> qlevel;
  321. }
  322. if (i < s->blocksize)
  323. {
  324. int sum = 0;
  325. for (j = 0; j < pred_order; j++)
  326. sum += coeffs[j] * decoded[i-j-1];
  327. decoded[i] += sum >> qlevel;
  328. }
  329. }
  330. return 0;
  331. }
  332. static inline int decode_subframe(FLACContext *s, int channel)
  333. {
  334. int type, wasted = 0;
  335. int i, tmp;
  336. s->curr_bps = s->bps;
  337. if(channel == 0){
  338. if(s->decorrelation == RIGHT_SIDE)
  339. s->curr_bps++;
  340. }else{
  341. if(s->decorrelation == LEFT_SIDE || s->decorrelation == MID_SIDE)
  342. s->curr_bps++;
  343. }
  344. if (get_bits1(&s->gb))
  345. {
  346. av_log(s->avctx, AV_LOG_ERROR, "invalid subframe padding\n");
  347. return -1;
  348. }
  349. type = get_bits(&s->gb, 6);
  350. // wasted = get_bits1(&s->gb);
  351. // if (wasted)
  352. // {
  353. // while (!get_bits1(&s->gb))
  354. // wasted++;
  355. // if (wasted)
  356. // wasted++;
  357. // s->curr_bps -= wasted;
  358. // }
  359. #if 0
  360. wasted= 16 - av_log2(show_bits(&s->gb, 17));
  361. skip_bits(&s->gb, wasted+1);
  362. s->curr_bps -= wasted;
  363. #else
  364. if (get_bits1(&s->gb))
  365. {
  366. wasted = 1;
  367. while (!get_bits1(&s->gb))
  368. wasted++;
  369. s->curr_bps -= wasted;
  370. av_log(s->avctx, AV_LOG_DEBUG, "%d wasted bits\n", wasted);
  371. }
  372. #endif
  373. //FIXME use av_log2 for types
  374. if (type == 0)
  375. {
  376. av_log(s->avctx, AV_LOG_DEBUG, "coding type: constant\n");
  377. tmp = get_sbits(&s->gb, s->curr_bps);
  378. for (i = 0; i < s->blocksize; i++)
  379. s->decoded[channel][i] = tmp;
  380. }
  381. else if (type == 1)
  382. {
  383. av_log(s->avctx, AV_LOG_DEBUG, "coding type: verbatim\n");
  384. for (i = 0; i < s->blocksize; i++)
  385. s->decoded[channel][i] = get_sbits(&s->gb, s->curr_bps);
  386. }
  387. else if ((type >= 8) && (type <= 12))
  388. {
  389. // av_log(s->avctx, AV_LOG_DEBUG, "coding type: fixed\n");
  390. if (decode_subframe_fixed(s, channel, type & ~0x8) < 0)
  391. return -1;
  392. }
  393. else if (type >= 32)
  394. {
  395. // av_log(s->avctx, AV_LOG_DEBUG, "coding type: lpc\n");
  396. if (decode_subframe_lpc(s, channel, (type & ~0x20)+1) < 0)
  397. return -1;
  398. }
  399. else
  400. {
  401. av_log(s->avctx, AV_LOG_ERROR, "invalid coding type\n");
  402. return -1;
  403. }
  404. if (wasted)
  405. {
  406. int i;
  407. for (i = 0; i < s->blocksize; i++)
  408. s->decoded[channel][i] <<= wasted;
  409. }
  410. return 0;
  411. }
  412. static int decode_frame(FLACContext *s, int alloc_data_size)
  413. {
  414. int blocksize_code, sample_rate_code, sample_size_code, assignment, i, crc8;
  415. int decorrelation, bps, blocksize, samplerate;
  416. blocksize_code = get_bits(&s->gb, 4);
  417. sample_rate_code = get_bits(&s->gb, 4);
  418. assignment = get_bits(&s->gb, 4); /* channel assignment */
  419. if (assignment < 8 && s->channels == assignment+1)
  420. decorrelation = INDEPENDENT;
  421. else if (assignment >=8 && assignment < 11 && s->channels == 2)
  422. decorrelation = LEFT_SIDE + assignment - 8;
  423. else
  424. {
  425. av_log(s->avctx, AV_LOG_ERROR, "unsupported channel assignment %d (channels=%d)\n", assignment, s->channels);
  426. return -1;
  427. }
  428. sample_size_code = get_bits(&s->gb, 3);
  429. if(sample_size_code == 0)
  430. bps= s->bps;
  431. else if((sample_size_code != 3) && (sample_size_code != 7))
  432. bps = sample_size_table[sample_size_code];
  433. else
  434. {
  435. av_log(s->avctx, AV_LOG_ERROR, "invalid sample size code (%d)\n", sample_size_code);
  436. return -1;
  437. }
  438. if (get_bits1(&s->gb))
  439. {
  440. av_log(s->avctx, AV_LOG_ERROR, "broken stream, invalid padding\n");
  441. return -1;
  442. }
  443. if(get_utf8(&s->gb) < 0){
  444. av_log(s->avctx, AV_LOG_ERROR, "utf8 fscked\n");
  445. return -1;
  446. }
  447. #if 0
  448. if (/*((blocksize_code == 6) || (blocksize_code == 7)) &&*/
  449. (s->min_blocksize != s->max_blocksize)){
  450. }else{
  451. }
  452. #endif
  453. if (blocksize_code == 0)
  454. blocksize = s->min_blocksize;
  455. else if (blocksize_code == 6)
  456. blocksize = get_bits(&s->gb, 8)+1;
  457. else if (blocksize_code == 7)
  458. blocksize = get_bits(&s->gb, 16)+1;
  459. else
  460. blocksize = blocksize_table[blocksize_code];
  461. if(blocksize > s->max_blocksize){
  462. av_log(s->avctx, AV_LOG_ERROR, "blocksize %d > %d\n", blocksize, s->max_blocksize);
  463. return -1;
  464. }
  465. if(blocksize * s->channels * sizeof(int16_t) > alloc_data_size)
  466. return -1;
  467. if (sample_rate_code == 0){
  468. samplerate= s->samplerate;
  469. }else if ((sample_rate_code > 3) && (sample_rate_code < 12))
  470. samplerate = sample_rate_table[sample_rate_code];
  471. else if (sample_rate_code == 12)
  472. samplerate = get_bits(&s->gb, 8) * 1000;
  473. else if (sample_rate_code == 13)
  474. samplerate = get_bits(&s->gb, 16);
  475. else if (sample_rate_code == 14)
  476. samplerate = get_bits(&s->gb, 16) * 10;
  477. else{
  478. av_log(s->avctx, AV_LOG_ERROR, "illegal sample rate code %d\n", sample_rate_code);
  479. return -1;
  480. }
  481. skip_bits(&s->gb, 8);
  482. crc8 = av_crc(av_crc_get_table(AV_CRC_8_ATM), 0,
  483. s->gb.buffer, get_bits_count(&s->gb)/8);
  484. if(crc8){
  485. av_log(s->avctx, AV_LOG_ERROR, "header crc mismatch crc=%2X\n", crc8);
  486. return -1;
  487. }
  488. s->blocksize = blocksize;
  489. s->samplerate = samplerate;
  490. s->bps = bps;
  491. s->decorrelation= decorrelation;
  492. // dump_headers(s);
  493. /* subframes */
  494. for (i = 0; i < s->channels; i++)
  495. {
  496. // av_log(s->avctx, AV_LOG_DEBUG, "decoded: %x residual: %x\n", s->decoded[i], s->residual[i]);
  497. if (decode_subframe(s, i) < 0)
  498. return -1;
  499. }
  500. align_get_bits(&s->gb);
  501. /* frame footer */
  502. skip_bits(&s->gb, 16); /* data crc */
  503. return 0;
  504. }
  505. static int flac_decode_frame(AVCodecContext *avctx,
  506. void *data, int *data_size,
  507. const uint8_t *buf, int buf_size)
  508. {
  509. FLACContext *s = avctx->priv_data;
  510. int tmp = 0, i, j = 0, input_buf_size = 0;
  511. int16_t *samples = data;
  512. int alloc_data_size= *data_size;
  513. *data_size=0;
  514. if(s->max_framesize == 0){
  515. s->max_framesize= 65536; // should hopefully be enough for the first header
  516. s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);
  517. }
  518. if(1 && s->max_framesize){//FIXME truncated
  519. buf_size= FFMAX(FFMIN(buf_size, s->max_framesize - s->bitstream_size), 0);
  520. input_buf_size= buf_size;
  521. if(s->bitstream_index + s->bitstream_size + buf_size > s->allocated_bitstream_size){
  522. // printf("memmove\n");
  523. memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);
  524. s->bitstream_index=0;
  525. }
  526. memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf, buf_size);
  527. buf= &s->bitstream[s->bitstream_index];
  528. buf_size += s->bitstream_size;
  529. s->bitstream_size= buf_size;
  530. if(buf_size < s->max_framesize){
  531. // printf("wanna more data ...\n");
  532. return input_buf_size;
  533. }
  534. }
  535. init_get_bits(&s->gb, buf, buf_size*8);
  536. if (!metadata_parse(s))
  537. {
  538. tmp = show_bits(&s->gb, 16);
  539. if((tmp & 0xFFFE) != 0xFFF8){
  540. av_log(s->avctx, AV_LOG_ERROR, "FRAME HEADER not here\n");
  541. while(get_bits_count(&s->gb)/8+2 < buf_size && (show_bits(&s->gb, 16) & 0xFFFE) != 0xFFF8)
  542. skip_bits(&s->gb, 8);
  543. goto end; // we may not have enough bits left to decode a frame, so try next time
  544. }
  545. skip_bits(&s->gb, 16);
  546. if (decode_frame(s, alloc_data_size) < 0){
  547. av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n");
  548. s->bitstream_size=0;
  549. s->bitstream_index=0;
  550. return -1;
  551. }
  552. }
  553. #if 0
  554. /* fix the channel order here */
  555. if (s->order == MID_SIDE)
  556. {
  557. short *left = samples;
  558. short *right = samples + s->blocksize;
  559. for (i = 0; i < s->blocksize; i += 2)
  560. {
  561. uint32_t x = s->decoded[0][i];
  562. uint32_t y = s->decoded[0][i+1];
  563. right[i] = x - (y / 2);
  564. left[i] = right[i] + y;
  565. }
  566. *data_size = 2 * s->blocksize;
  567. }
  568. else
  569. {
  570. for (i = 0; i < s->channels; i++)
  571. {
  572. switch(s->order)
  573. {
  574. case INDEPENDENT:
  575. for (j = 0; j < s->blocksize; j++)
  576. samples[(s->blocksize*i)+j] = s->decoded[i][j];
  577. break;
  578. case LEFT_SIDE:
  579. case RIGHT_SIDE:
  580. if (i == 0)
  581. for (j = 0; j < s->blocksize; j++)
  582. samples[(s->blocksize*i)+j] = s->decoded[0][j];
  583. else
  584. for (j = 0; j < s->blocksize; j++)
  585. samples[(s->blocksize*i)+j] = s->decoded[0][j] - s->decoded[i][j];
  586. break;
  587. // case MID_SIDE:
  588. // av_log(s->avctx, AV_LOG_DEBUG, "mid-side unsupported\n");
  589. }
  590. *data_size += s->blocksize;
  591. }
  592. }
  593. #else
  594. #define DECORRELATE(left, right)\
  595. assert(s->channels == 2);\
  596. for (i = 0; i < s->blocksize; i++)\
  597. {\
  598. int a= s->decoded[0][i];\
  599. int b= s->decoded[1][i];\
  600. *samples++ = ((left) << (24 - s->bps)) >> 8;\
  601. *samples++ = ((right) << (24 - s->bps)) >> 8;\
  602. }\
  603. break;
  604. switch(s->decorrelation)
  605. {
  606. case INDEPENDENT:
  607. for (j = 0; j < s->blocksize; j++)
  608. {
  609. for (i = 0; i < s->channels; i++)
  610. *samples++ = (s->decoded[i][j] << (24 - s->bps)) >> 8;
  611. }
  612. break;
  613. case LEFT_SIDE:
  614. DECORRELATE(a,a-b)
  615. case RIGHT_SIDE:
  616. DECORRELATE(a+b,b)
  617. case MID_SIDE:
  618. DECORRELATE( (a-=b>>1) + b, a)
  619. }
  620. #endif
  621. *data_size = (int8_t *)samples - (int8_t *)data;
  622. // av_log(s->avctx, AV_LOG_DEBUG, "data size: %d\n", *data_size);
  623. // s->last_blocksize = s->blocksize;
  624. end:
  625. i= (get_bits_count(&s->gb)+7)/8;
  626. if(i > buf_size){
  627. av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
  628. s->bitstream_size=0;
  629. s->bitstream_index=0;
  630. return -1;
  631. }
  632. if(s->bitstream_size){
  633. s->bitstream_index += i;
  634. s->bitstream_size -= i;
  635. return input_buf_size;
  636. }else
  637. return i;
  638. }
  639. static int flac_decode_close(AVCodecContext *avctx)
  640. {
  641. FLACContext *s = avctx->priv_data;
  642. int i;
  643. for (i = 0; i < s->channels; i++)
  644. {
  645. av_freep(&s->decoded[i]);
  646. }
  647. av_freep(&s->bitstream);
  648. return 0;
  649. }
  650. static void flac_flush(AVCodecContext *avctx){
  651. FLACContext *s = avctx->priv_data;
  652. s->bitstream_size=
  653. s->bitstream_index= 0;
  654. }
  655. AVCodec flac_decoder = {
  656. "flac",
  657. CODEC_TYPE_AUDIO,
  658. CODEC_ID_FLAC,
  659. sizeof(FLACContext),
  660. flac_decode_init,
  661. NULL,
  662. flac_decode_close,
  663. flac_decode_frame,
  664. .flush= flac_flush,
  665. };