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.

652 lines
18KB

  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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. /**
  20. * @file flac.c
  21. * FLAC (Free Lossless Audio Codec) decoder
  22. * @author Alex Beregszaszi
  23. */
  24. #include "avcodec.h"
  25. #include "golomb.h"
  26. #define MAX_CHANNELS 8
  27. #define MAX_BLOCKSIZE 65535
  28. enum channel_order {
  29. INDEPENDENT,
  30. LEFT_SIDE,
  31. RIGHT_SIDE,
  32. MID_SIDE,
  33. };
  34. typedef struct FLACContext {
  35. AVCodecContext *avctx;
  36. GetBitContext gb;
  37. int min_blocksize, max_blocksize;
  38. int min_framesize, max_framesize;
  39. int samplerate, channels;
  40. int blocksize, last_blocksize;
  41. int bps, curr_bps;
  42. enum channel_order order;
  43. uint8_t *residual[MAX_CHANNELS];
  44. uint32_t *decoded[MAX_CHANNELS];
  45. } FLACContext;
  46. #define METADATA_TYPE_STREAMINFO 0
  47. static int sample_rate_table[] =
  48. { 0, 0, 0, 0,
  49. 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000,
  50. 0, 0, 0, 0 };
  51. static int sample_size_table[] =
  52. { 0, 8, 12, 0, 16, 20, 24, 0 };
  53. static uint64_t get_uvlc(GetBitContext *gb, int is64)
  54. {
  55. uint64_t val = 0;
  56. int i = 0;
  57. while(i++ < 5+is64)
  58. {
  59. const int tmp = get_bits(gb, 8);
  60. if (tmp & 0x80)
  61. val = (val << 7) + tmp - 0x80;
  62. else
  63. return (val << 7) + tmp;
  64. }
  65. return -1;
  66. }
  67. static int flac_decode_init(AVCodecContext * avctx)
  68. {
  69. return 0;
  70. }
  71. static void dump_headers(FLACContext *s)
  72. {
  73. printf(" Blocksize: %d .. %d (%d)\n", s->min_blocksize, s->max_blocksize, s->blocksize);
  74. printf(" Framesize: %d .. %d\n", s->min_framesize, s->max_framesize);
  75. printf(" Samplerate: %d\n", s->samplerate);
  76. printf(" Channels: %d\n", s->channels);
  77. printf(" Bits: %d\n", s->bps);
  78. }
  79. static void metadata_streaminfo(FLACContext *s)
  80. {
  81. int i;
  82. /* mandatory streaminfo */
  83. s->min_blocksize = get_bits(&s->gb, 16);
  84. s->max_blocksize = get_bits(&s->gb, 16);
  85. s->min_framesize = get_bits_long(&s->gb, 24);
  86. s->max_framesize = get_bits_long(&s->gb, 24);
  87. s->samplerate = get_bits_long(&s->gb, 20);
  88. s->channels = get_bits(&s->gb, 3) + 1;
  89. s->bps = get_bits(&s->gb, 5) + 1;
  90. s->avctx->channels = s->channels;
  91. s->avctx->sample_rate = s->samplerate;
  92. skip_bits(&s->gb, 36); /* total num of samples */
  93. skip_bits(&s->gb, 64); /* md5 sum */
  94. skip_bits(&s->gb, 64); /* md5 sum */
  95. for (i = 0; i < s->channels; i++)
  96. {
  97. s->decoded[i] = av_realloc(s->decoded[i], sizeof(uint32_t)*s->max_blocksize);
  98. s->residual[i] = av_realloc(s->residual[i], sizeof(uint8_t)*s->max_blocksize);
  99. }
  100. }
  101. static int decode_residuals(FLACContext *s, int channel, int pred_order)
  102. {
  103. int i, tmp, partition, method_type, rice_order;
  104. int sample = 0, samples;
  105. method_type = get_bits(&s->gb, 2);
  106. if (method_type != 0)
  107. return -1;
  108. rice_order = get_bits(&s->gb, 4);
  109. samples = (rice_order > 0) ?
  110. (s->blocksize >> rice_order) : (s->blocksize - pred_order);
  111. for (partition = 0; partition < (1 << rice_order); partition++)
  112. {
  113. tmp = get_bits(&s->gb, 4);
  114. if (tmp == 0)
  115. {
  116. i = (!rice_order || partition) ? 0 : pred_order;
  117. for (; i < samples; i++, sample++)
  118. s->residual[channel][sample] = get_sr_golomb_flac(&s->gb, tmp, 0, 0);
  119. printf("zero k\n");
  120. }
  121. else if (tmp == 15)
  122. {
  123. printf("fixed len partition\n");
  124. tmp = get_bits(&s->gb, 5);
  125. i = (!rice_order || partition) ? 0 : pred_order;
  126. for (; i < samples; i++, sample++)
  127. s->residual[channel][sample] = get_bits(&s->gb, tmp);
  128. }
  129. else
  130. {
  131. // printf("rice coded partition\n");
  132. #if 1
  133. i = (!rice_order || partition) ? 0 : pred_order;
  134. for (; i < samples; i++, sample++)
  135. s->residual[channel][sample] = get_sr_golomb_flac(&s->gb, tmp, 0, 0);
  136. #else
  137. i = ((!rice_order || partition) ? samples : samples - pred_order) + sample;
  138. for (; sample < i; sample++)
  139. s->residual[channel][sample] = get_ur_golomb(&s->gb, tmp, 0, 0);
  140. // s->residual[channel][sample] = get_se_golomb(&s->gb);
  141. #endif
  142. }
  143. }
  144. printf("partitions: %d, samples: %d\n", 1 << rice_order, sample);
  145. return 0;
  146. }
  147. static int decode_subframe_fixed(FLACContext *s, int channel, int pred_order)
  148. {
  149. int i;
  150. printf(" SUBFRAME FIXED\n");
  151. /* warm up samples */
  152. printf(" warm up samples: %d\n", pred_order);
  153. for (i = 0; i < pred_order; i++)
  154. {
  155. s->decoded[channel][i] = get_bits(&s->gb, s->curr_bps);
  156. printf(" %d: %d\n", i, s->decoded[channel][i]);
  157. }
  158. if (decode_residuals(s, channel, pred_order) < 0)
  159. return -1;
  160. switch(pred_order)
  161. {
  162. case 0:
  163. for (i = pred_order; i < s->blocksize; i++)
  164. s->decoded[channel][i] = s->residual[channel][i];
  165. break;
  166. case 1:
  167. for (i = pred_order; i < s->blocksize; i++)
  168. s->decoded[channel][i] = s->residual[channel][i] +
  169. s->decoded[channel][i-1];
  170. break;
  171. case 2:
  172. for (i = pred_order; i < s->blocksize; i++)
  173. s->decoded[channel][i] = s->residual[channel][i] +
  174. (s->decoded[channel][i-1] << 1) -
  175. s->decoded[channel][i-2];
  176. break;
  177. case 3:
  178. for (i = pred_order; i < s->blocksize; i++)
  179. s->decoded[channel][i] = s->residual[channel][i] +
  180. (((s->decoded[channel][i-1] -
  181. s->decoded[channel][i-2]) << 1) +
  182. (s->decoded[channel][i-1] -
  183. s->decoded[channel][i-2])) +
  184. s->decoded[channel][i-3];
  185. break;
  186. case 4:
  187. for (i = pred_order; i < s->blocksize; i++)
  188. s->decoded[channel][i] = s->residual[channel][i] +
  189. ((s->decoded[channel][i-1] +
  190. s->decoded[channel][i-3]) << 2) -
  191. ((s->decoded[channel][i-2] << 2) +
  192. (s->decoded[channel][i-2] << 1)) -
  193. s->decoded[channel][i-4];
  194. break;
  195. }
  196. return 0;
  197. }
  198. static int decode_subframe_lpc(FLACContext *s, int channel, int pred_order)
  199. {
  200. int sum, i, j;
  201. int coeff_prec, qlevel;
  202. int coeffs[pred_order];
  203. printf(" SUBFRAME LPC\n");
  204. /* warm up samples */
  205. printf(" warm up samples: %d\n", pred_order);
  206. for (i = 0; i < pred_order; i++)
  207. {
  208. s->decoded[channel][i] = get_bits(&s->gb, s->curr_bps);
  209. printf(" %d: %d\n", i, s->decoded[channel][i]);
  210. }
  211. coeff_prec = get_bits(&s->gb, 4) + 1;
  212. if (coeff_prec == 16)
  213. {
  214. printf("invalid coeff precision\n");
  215. return -1;
  216. }
  217. printf(" qlp coeff prec: %d\n", coeff_prec);
  218. qlevel = get_bits(&s->gb, 5);
  219. printf(" quant level: %d\n", qlevel);
  220. for (i = 0; i < pred_order; i++)
  221. {
  222. coeffs[i] = get_bits(&s->gb, coeff_prec);
  223. printf(" %d: %d\n", i, coeffs[i]);
  224. }
  225. if (decode_residuals(s, channel, pred_order) < 0)
  226. return -1;
  227. for (i = pred_order; i < s->blocksize; i++)
  228. {
  229. sum = 0;
  230. for (j = 0; j < pred_order; j++)
  231. sum += coeffs[j] * s->decoded[channel][i-j-1];
  232. s->decoded[channel][i] = s->residual[channel][i] + (sum >> qlevel);
  233. }
  234. return 0;
  235. }
  236. static inline int decode_subframe(FLACContext *s, int channel)
  237. {
  238. int type, wasted = 0;
  239. int i, tmp;
  240. s->curr_bps = s->bps;
  241. if (get_bits1(&s->gb))
  242. {
  243. printf("invalid subframe padding\n");
  244. return -1;
  245. }
  246. type = get_bits(&s->gb, 6);
  247. // wasted = get_bits1(&s->gb);
  248. // if (wasted)
  249. // {
  250. // while (!get_bits1(&s->gb))
  251. // wasted++;
  252. // if (wasted)
  253. // wasted++;
  254. // s->curr_bps -= wasted;
  255. // }
  256. if (get_bits1(&s->gb))
  257. {
  258. wasted = 1;
  259. while (!get_bits1(&s->gb))
  260. wasted++;
  261. s->curr_bps -= wasted;
  262. }
  263. if (type == 0)
  264. {
  265. printf("coding type: constant\n");
  266. tmp = get_bits(&s->gb, s->curr_bps);
  267. for (i = 0; i < s->blocksize; i++)
  268. s->decoded[channel][i] = tmp;
  269. }
  270. else if (type == 1)
  271. {
  272. printf("coding type: verbatim\n");
  273. for (i = 0; i < s->blocksize; i++)
  274. s->decoded[channel][i] = get_bits(&s->gb, s->curr_bps);
  275. }
  276. else if ((type >= 8) && (type <= 12))
  277. {
  278. printf("coding type: fixed\n");
  279. if (decode_subframe_fixed(s, channel, type & ~0x8) < 0)
  280. return -1;
  281. }
  282. else if (type >= 32)
  283. {
  284. printf("coding type: lpc\n");
  285. if (decode_subframe_lpc(s, channel, (type & ~0x20)+1) < 0)
  286. return -1;
  287. }
  288. else
  289. {
  290. printf("invalid coding type\n");
  291. return -1;
  292. }
  293. if (wasted)
  294. {
  295. int i;
  296. for (i = 0; i < s->blocksize; i++)
  297. s->decoded[channel][i] <<= wasted;
  298. }
  299. return 0;
  300. }
  301. static int decode_frame(FLACContext *s)
  302. {
  303. int blocksize_code, sample_rate_code, sample_size_code, assignment, i;
  304. blocksize_code = get_bits(&s->gb, 4);
  305. if (blocksize_code == 0)
  306. s->blocksize = s->min_blocksize;
  307. else if (blocksize_code == 1)
  308. s->blocksize = 192;
  309. else if (blocksize_code <= 5)
  310. s->blocksize = 576 << (blocksize_code - 2);
  311. else if (blocksize_code >= 8)
  312. s->blocksize = 256 << (blocksize_code - 8);
  313. sample_rate_code = get_bits(&s->gb, 4);
  314. if ((sample_rate_code > 3) && (sample_rate_code < 12))
  315. s->samplerate = sample_rate_table[sample_rate_code];
  316. assignment = get_bits(&s->gb, 4); /* channel assignment */
  317. if (assignment < 8)
  318. {
  319. s->order = INDEPENDENT;
  320. if (s->channels != assignment+1)
  321. printf("channel number and number of assigned channels differ!\n");
  322. printf("channels: %d\n", assignment+1);
  323. }
  324. else if (assignment == 8)
  325. {
  326. s->order = LEFT_SIDE;
  327. printf("left/side\n");
  328. }
  329. else if (assignment == 9)
  330. {
  331. s->order = RIGHT_SIDE;
  332. printf("right/side\n");
  333. }
  334. else if (assignment == 10)
  335. {
  336. s->order = MID_SIDE;
  337. printf("mid/side\n");
  338. }
  339. else
  340. {
  341. printf("unsupported channel assignment\n");
  342. return -1;
  343. }
  344. if ((assignment >= 8) && (s->channels != 2))
  345. {
  346. return -1;
  347. }
  348. sample_size_code = get_bits(&s->gb, 3);
  349. if (s->bps != 0)
  350. s->bps = sample_size_table[sample_size_code];
  351. if ((sample_size_code == 3) || (sample_size_code == 7))
  352. {
  353. printf("invalid sample size code (%d)\n", sample_size_code);
  354. return -1;
  355. }
  356. if (get_bits1(&s->gb))
  357. {
  358. printf("broken stream, invalid padding\n");
  359. // return -1;
  360. }
  361. if (((blocksize_code == 6) || (blocksize_code == 7)) &&
  362. (s->min_blocksize != s->max_blocksize))
  363. {
  364. get_uvlc(&s->gb, 1);
  365. }
  366. else
  367. get_uvlc(&s->gb, 0);
  368. if (blocksize_code == 6)
  369. s->blocksize = get_bits(&s->gb, 8)+1;
  370. if (blocksize_code == 7)
  371. s->blocksize = get_bits(&s->gb, 16)+1;
  372. if ((sample_rate_code > 11) && (sample_rate_code < 15))
  373. {
  374. switch(sample_rate_code)
  375. {
  376. case 12:
  377. s->samplerate = get_bits(&s->gb, 8) * 1000;
  378. break;
  379. case 13:
  380. s->samplerate = get_bits(&s->gb, 16);
  381. break;
  382. case 14:
  383. s->samplerate = get_bits(&s->gb, 16) * 10;
  384. break;
  385. }
  386. }
  387. skip_bits(&s->gb, 8); /* header crc */
  388. dump_headers(s);
  389. /* subframes */
  390. for (i = 0; i < s->channels; i++)
  391. {
  392. if (s->blocksize != s->last_blocksize)
  393. {
  394. s->decoded[i] = av_realloc(s->decoded[i], sizeof(uint32_t)*s->blocksize);
  395. s->residual[i] = av_realloc(s->residual[i], sizeof(uint8_t)*s->blocksize);
  396. }
  397. printf("decoded: %x residual: %x\n", s->decoded[i], s->residual[i]);
  398. if (decode_subframe(s, i) < 0)
  399. return -1;
  400. }
  401. align_get_bits(&s->gb);
  402. /* frame footer */
  403. skip_bits(&s->gb, 16); /* data crc */
  404. return 0;
  405. }
  406. static int flac_decode_frame(AVCodecContext *avctx,
  407. void *data, int *data_size,
  408. uint8_t *buf, int buf_size)
  409. {
  410. FLACContext *s = avctx->priv_data;
  411. int metadata_flag, metadata_type, metadata_size;
  412. int tmp = 0, i, j = 0;
  413. int16_t *samples = data, *left, *right;
  414. *data_size = 0;
  415. s->avctx = avctx;
  416. init_get_bits(&s->gb, buf, buf_size*8);
  417. /* fLaC signature (be) */
  418. if (get_bits_long(&s->gb, 32) == bswap_32(ff_get_fourcc("fLaC")))
  419. {
  420. printf("STREAM HEADER\n");
  421. do {
  422. metadata_flag = get_bits(&s->gb, 1);
  423. metadata_type = get_bits(&s->gb, 7);
  424. metadata_size = get_bits_long(&s->gb, 24);
  425. printf(" metadata block: flag = %d, type = %d, size = %d\n",
  426. metadata_flag, metadata_type,
  427. metadata_size);
  428. switch(metadata_type)
  429. {
  430. case METADATA_TYPE_STREAMINFO:
  431. metadata_streaminfo(s);
  432. dump_headers(s);
  433. break;
  434. default:
  435. while ((metadata_size -= 8) > 0)
  436. skip_bits(&s->gb, 8);
  437. }
  438. } while(metadata_flag != 1);
  439. }
  440. else
  441. {
  442. init_get_bits(&s->gb, buf, buf_size*8);
  443. tmp = get_bits(&s->gb, 16);
  444. if (tmp == 0xfff8)
  445. printf("FRAME HEADER\n");
  446. if (decode_frame(s) < 0)
  447. return -1;
  448. }
  449. #if 0
  450. /* fix the channel order here */
  451. if (s->order == MID_SIDE)
  452. {
  453. short *left = samples;
  454. short *right = samples + s->blocksize;
  455. for (i = 0; i < s->blocksize; i += 2)
  456. {
  457. uint32_t x = s->decoded[0][i];
  458. uint32_t y = s->decoded[0][i+1];
  459. right[i] = x - (y / 2);
  460. left[i] = right[i] + y;
  461. }
  462. *data_size = 2 * s->blocksize;
  463. }
  464. else
  465. {
  466. for (i = 0; i < s->channels; i++)
  467. {
  468. switch(s->order)
  469. {
  470. case INDEPENDENT:
  471. for (j = 0; j < s->blocksize; j++)
  472. samples[(s->blocksize*i)+j] = s->decoded[i][j];
  473. break;
  474. case LEFT_SIDE:
  475. case RIGHT_SIDE:
  476. if (i == 0)
  477. for (j = 0; j < s->blocksize; j++)
  478. samples[(s->blocksize*i)+j] = s->decoded[0][j];
  479. else
  480. for (j = 0; j < s->blocksize; j++)
  481. samples[(s->blocksize*i)+j] = s->decoded[0][j] - s->decoded[i][j];
  482. break;
  483. // case MID_SIDE:
  484. // printf("mid-side unsupported\n");
  485. }
  486. *data_size += s->blocksize;
  487. }
  488. }
  489. #else
  490. switch(s->order)
  491. {
  492. case INDEPENDENT:
  493. for (i = 0; i < s->channels; i++)
  494. {
  495. for (j = 0; j < s->blocksize; j++)
  496. *(samples++) = s->decoded[i][j];
  497. *data_size += s->blocksize;
  498. }
  499. break;
  500. case LEFT_SIDE:
  501. assert(s->channels == 2);
  502. for (i = 0; i < s->blocksize; i++)
  503. {
  504. *(samples++) = s->decoded[0][i];
  505. *(samples++) = s->decoded[0][i] - s->decoded[1][i];
  506. }
  507. *data_size = 2*s->blocksize;
  508. break;
  509. case RIGHT_SIDE:
  510. assert(s->channels == 2);
  511. for (i = 0; i < s->blocksize; i++)
  512. {
  513. *(samples++) = s->decoded[0][i] + s->decoded[1][i];
  514. *(samples++) = s->decoded[1][i];
  515. }
  516. *data_size = 2*s->blocksize;
  517. break;
  518. case MID_SIDE:
  519. assert(s->channels == 2);
  520. for (i = 0; i < s->blocksize; i++)
  521. {
  522. int16_t mid, side;
  523. mid = s->decoded[0][i];
  524. side = s->decoded[1][i];
  525. mid <<= 1;
  526. if (side & 1)
  527. mid++;
  528. *(samples++) = (mid + side) >> 1;
  529. *(samples++) = (mid - side) >> 1;
  530. }
  531. *data_size = 2*s->blocksize;
  532. break;
  533. }
  534. #endif
  535. // *data_size = (int8_t *)samples - (int8_t *)data;
  536. printf("data size: %d\n", *data_size);
  537. s->last_blocksize = s->blocksize;
  538. return (get_bits_count(&s->gb)+7)/8;
  539. }
  540. static int flac_decode_close(AVCodecContext *avctx)
  541. {
  542. FLACContext *s = avctx->priv_data;
  543. int i;
  544. for (i = 0; i < s->channels; i++)
  545. {
  546. if (s->decoded[i])
  547. av_free(s->decoded[i]);
  548. if (s->residual[i])
  549. av_free(s->residual[i]);
  550. }
  551. return 0;
  552. }
  553. AVCodec flac_decoder = {
  554. "flac",
  555. CODEC_TYPE_AUDIO,
  556. CODEC_ID_FLAC,
  557. sizeof(FLACContext),
  558. flac_decode_init,
  559. NULL,
  560. flac_decode_close,
  561. flac_decode_frame,
  562. };