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.

773 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., 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 <limits.h>
  25. #include "avcodec.h"
  26. #include "golomb.h"
  27. #undef NDEBUG
  28. #include <assert.h>
  29. #define MAX_CHANNELS 8
  30. #define MAX_BLOCKSIZE 65535
  31. enum decorrelation_type {
  32. INDEPENDENT,
  33. LEFT_SIDE,
  34. RIGHT_SIDE,
  35. MID_SIDE,
  36. };
  37. typedef struct FLACContext {
  38. AVCodecContext *avctx;
  39. GetBitContext gb;
  40. int min_blocksize, max_blocksize;
  41. int min_framesize, max_framesize;
  42. int samplerate, channels;
  43. int blocksize/*, last_blocksize*/;
  44. int bps, curr_bps;
  45. enum decorrelation_type decorrelation;
  46. int32_t *decoded[MAX_CHANNELS];
  47. uint8_t *bitstream;
  48. int bitstream_size;
  49. int bitstream_index;
  50. int allocated_bitstream_size;
  51. } FLACContext;
  52. #define METADATA_TYPE_STREAMINFO 0
  53. static int sample_rate_table[] =
  54. { 0, 0, 0, 0,
  55. 8000, 16000, 22050, 24000, 32000, 44100, 48000, 96000,
  56. 0, 0, 0, 0 };
  57. static int sample_size_table[] =
  58. { 0, 8, 12, 0, 16, 20, 24, 0 };
  59. static int blocksize_table[] = {
  60. 0, 192, 576<<0, 576<<1, 576<<2, 576<<3, 0, 0,
  61. 256<<0, 256<<1, 256<<2, 256<<3, 256<<4, 256<<5, 256<<6, 256<<7
  62. };
  63. static const uint8_t table_crc8[256] = {
  64. 0x00, 0x07, 0x0e, 0x09, 0x1c, 0x1b, 0x12, 0x15,
  65. 0x38, 0x3f, 0x36, 0x31, 0x24, 0x23, 0x2a, 0x2d,
  66. 0x70, 0x77, 0x7e, 0x79, 0x6c, 0x6b, 0x62, 0x65,
  67. 0x48, 0x4f, 0x46, 0x41, 0x54, 0x53, 0x5a, 0x5d,
  68. 0xe0, 0xe7, 0xee, 0xe9, 0xfc, 0xfb, 0xf2, 0xf5,
  69. 0xd8, 0xdf, 0xd6, 0xd1, 0xc4, 0xc3, 0xca, 0xcd,
  70. 0x90, 0x97, 0x9e, 0x99, 0x8c, 0x8b, 0x82, 0x85,
  71. 0xa8, 0xaf, 0xa6, 0xa1, 0xb4, 0xb3, 0xba, 0xbd,
  72. 0xc7, 0xc0, 0xc9, 0xce, 0xdb, 0xdc, 0xd5, 0xd2,
  73. 0xff, 0xf8, 0xf1, 0xf6, 0xe3, 0xe4, 0xed, 0xea,
  74. 0xb7, 0xb0, 0xb9, 0xbe, 0xab, 0xac, 0xa5, 0xa2,
  75. 0x8f, 0x88, 0x81, 0x86, 0x93, 0x94, 0x9d, 0x9a,
  76. 0x27, 0x20, 0x29, 0x2e, 0x3b, 0x3c, 0x35, 0x32,
  77. 0x1f, 0x18, 0x11, 0x16, 0x03, 0x04, 0x0d, 0x0a,
  78. 0x57, 0x50, 0x59, 0x5e, 0x4b, 0x4c, 0x45, 0x42,
  79. 0x6f, 0x68, 0x61, 0x66, 0x73, 0x74, 0x7d, 0x7a,
  80. 0x89, 0x8e, 0x87, 0x80, 0x95, 0x92, 0x9b, 0x9c,
  81. 0xb1, 0xb6, 0xbf, 0xb8, 0xad, 0xaa, 0xa3, 0xa4,
  82. 0xf9, 0xfe, 0xf7, 0xf0, 0xe5, 0xe2, 0xeb, 0xec,
  83. 0xc1, 0xc6, 0xcf, 0xc8, 0xdd, 0xda, 0xd3, 0xd4,
  84. 0x69, 0x6e, 0x67, 0x60, 0x75, 0x72, 0x7b, 0x7c,
  85. 0x51, 0x56, 0x5f, 0x58, 0x4d, 0x4a, 0x43, 0x44,
  86. 0x19, 0x1e, 0x17, 0x10, 0x05, 0x02, 0x0b, 0x0c,
  87. 0x21, 0x26, 0x2f, 0x28, 0x3d, 0x3a, 0x33, 0x34,
  88. 0x4e, 0x49, 0x40, 0x47, 0x52, 0x55, 0x5c, 0x5b,
  89. 0x76, 0x71, 0x78, 0x7f, 0x6a, 0x6d, 0x64, 0x63,
  90. 0x3e, 0x39, 0x30, 0x37, 0x22, 0x25, 0x2c, 0x2b,
  91. 0x06, 0x01, 0x08, 0x0f, 0x1a, 0x1d, 0x14, 0x13,
  92. 0xae, 0xa9, 0xa0, 0xa7, 0xb2, 0xb5, 0xbc, 0xbb,
  93. 0x96, 0x91, 0x98, 0x9f, 0x8a, 0x8d, 0x84, 0x83,
  94. 0xde, 0xd9, 0xd0, 0xd7, 0xc2, 0xc5, 0xcc, 0xcb,
  95. 0xe6, 0xe1, 0xe8, 0xef, 0xfa, 0xfd, 0xf4, 0xf3
  96. };
  97. static int64_t get_utf8(GetBitContext *gb)
  98. {
  99. uint64_t val;
  100. int ones=0, bytes;
  101. while(get_bits1(gb))
  102. ones++;
  103. if (ones==0) bytes=0;
  104. else if(ones==1) return -1;
  105. else bytes= ones - 1;
  106. val= get_bits(gb, 7-ones);
  107. while(bytes--){
  108. const int tmp = get_bits(gb, 8);
  109. if((tmp>>6) != 2)
  110. return -1;
  111. val<<=6;
  112. val|= tmp&0x3F;
  113. }
  114. return val;
  115. }
  116. static int get_crc8(const uint8_t *buf, int count){
  117. int crc=0;
  118. int i;
  119. for(i=0; i<count; i++){
  120. crc = table_crc8[crc ^ buf[i]];
  121. }
  122. return crc;
  123. }
  124. static int flac_decode_init(AVCodecContext * avctx)
  125. {
  126. return 0;
  127. }
  128. static void dump_headers(FLACContext *s)
  129. {
  130. av_log(s->avctx, AV_LOG_DEBUG, " Blocksize: %d .. %d (%d)\n", s->min_blocksize, s->max_blocksize, s->blocksize);
  131. av_log(s->avctx, AV_LOG_DEBUG, " Framesize: %d .. %d\n", s->min_framesize, s->max_framesize);
  132. av_log(s->avctx, AV_LOG_DEBUG, " Samplerate: %d\n", s->samplerate);
  133. av_log(s->avctx, AV_LOG_DEBUG, " Channels: %d\n", s->channels);
  134. av_log(s->avctx, AV_LOG_DEBUG, " Bits: %d\n", s->bps);
  135. }
  136. static void allocate_buffers(FLACContext *s){
  137. int i;
  138. assert(s->max_blocksize);
  139. if(s->max_framesize == 0 && s->max_blocksize){
  140. s->max_framesize= (s->channels * s->bps * s->max_blocksize + 7)/ 8; //FIXME header overhead
  141. }
  142. for (i = 0; i < s->channels; i++)
  143. {
  144. s->decoded[i] = av_realloc(s->decoded[i], sizeof(int32_t)*s->max_blocksize);
  145. }
  146. s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);
  147. }
  148. static void metadata_streaminfo(FLACContext *s)
  149. {
  150. /* mandatory streaminfo */
  151. s->min_blocksize = get_bits(&s->gb, 16);
  152. s->max_blocksize = get_bits(&s->gb, 16);
  153. s->min_framesize = get_bits_long(&s->gb, 24);
  154. s->max_framesize = get_bits_long(&s->gb, 24);
  155. s->samplerate = get_bits_long(&s->gb, 20);
  156. s->channels = get_bits(&s->gb, 3) + 1;
  157. s->bps = get_bits(&s->gb, 5) + 1;
  158. s->avctx->channels = s->channels;
  159. s->avctx->sample_rate = s->samplerate;
  160. skip_bits(&s->gb, 36); /* total num of samples */
  161. skip_bits(&s->gb, 64); /* md5 sum */
  162. skip_bits(&s->gb, 64); /* md5 sum */
  163. allocate_buffers(s);
  164. }
  165. static int decode_residuals(FLACContext *s, int channel, int pred_order)
  166. {
  167. int i, tmp, partition, method_type, rice_order;
  168. int sample = 0, samples;
  169. method_type = get_bits(&s->gb, 2);
  170. if (method_type != 0){
  171. av_log(s->avctx, AV_LOG_DEBUG, "illegal residual coding method %d\n", method_type);
  172. return -1;
  173. }
  174. rice_order = get_bits(&s->gb, 4);
  175. samples= s->blocksize >> rice_order;
  176. sample=
  177. i= pred_order;
  178. for (partition = 0; partition < (1 << rice_order); partition++)
  179. {
  180. tmp = get_bits(&s->gb, 4);
  181. if (tmp == 15)
  182. {
  183. av_log(s->avctx, AV_LOG_DEBUG, "fixed len partition\n");
  184. tmp = get_bits(&s->gb, 5);
  185. for (; i < samples; i++, sample++)
  186. s->decoded[channel][sample] = get_sbits(&s->gb, tmp);
  187. }
  188. else
  189. {
  190. // av_log(s->avctx, AV_LOG_DEBUG, "rice coded partition k=%d\n", tmp);
  191. for (; i < samples; i++, sample++){
  192. s->decoded[channel][sample] = get_sr_golomb_flac(&s->gb, tmp, INT_MAX, 0);
  193. }
  194. }
  195. i= 0;
  196. }
  197. // av_log(s->avctx, AV_LOG_DEBUG, "partitions: %d, samples: %d\n", 1 << rice_order, sample);
  198. return 0;
  199. }
  200. static int decode_subframe_fixed(FLACContext *s, int channel, int pred_order)
  201. {
  202. int i;
  203. // av_log(s->avctx, AV_LOG_DEBUG, " SUBFRAME FIXED\n");
  204. /* warm up samples */
  205. // av_log(s->avctx, AV_LOG_DEBUG, " warm up samples: %d\n", pred_order);
  206. for (i = 0; i < pred_order; i++)
  207. {
  208. s->decoded[channel][i] = get_sbits(&s->gb, s->curr_bps);
  209. // av_log(s->avctx, AV_LOG_DEBUG, " %d: %d\n", i, s->decoded[channel][i]);
  210. }
  211. if (decode_residuals(s, channel, pred_order) < 0)
  212. return -1;
  213. switch(pred_order)
  214. {
  215. case 0:
  216. break;
  217. case 1:
  218. for (i = pred_order; i < s->blocksize; i++)
  219. s->decoded[channel][i] += s->decoded[channel][i-1];
  220. break;
  221. case 2:
  222. for (i = pred_order; i < s->blocksize; i++)
  223. s->decoded[channel][i] += 2*s->decoded[channel][i-1]
  224. - s->decoded[channel][i-2];
  225. break;
  226. case 3:
  227. for (i = pred_order; i < s->blocksize; i++)
  228. s->decoded[channel][i] += 3*s->decoded[channel][i-1]
  229. - 3*s->decoded[channel][i-2]
  230. + s->decoded[channel][i-3];
  231. break;
  232. case 4:
  233. for (i = pred_order; i < s->blocksize; i++)
  234. s->decoded[channel][i] += 4*s->decoded[channel][i-1]
  235. - 6*s->decoded[channel][i-2]
  236. + 4*s->decoded[channel][i-3]
  237. - s->decoded[channel][i-4];
  238. break;
  239. default:
  240. av_log(s->avctx, AV_LOG_ERROR, "illegal pred order %d\n", pred_order);
  241. return -1;
  242. }
  243. return 0;
  244. }
  245. static int decode_subframe_lpc(FLACContext *s, int channel, int pred_order)
  246. {
  247. int sum, i, j;
  248. int coeff_prec, qlevel;
  249. int coeffs[pred_order];
  250. // av_log(s->avctx, AV_LOG_DEBUG, " SUBFRAME LPC\n");
  251. /* warm up samples */
  252. // av_log(s->avctx, AV_LOG_DEBUG, " warm up samples: %d\n", pred_order);
  253. for (i = 0; i < pred_order; i++)
  254. {
  255. s->decoded[channel][i] = get_sbits(&s->gb, s->curr_bps);
  256. // av_log(s->avctx, AV_LOG_DEBUG, " %d: %d\n", i, s->decoded[channel][i]);
  257. }
  258. coeff_prec = get_bits(&s->gb, 4) + 1;
  259. if (coeff_prec == 16)
  260. {
  261. av_log(s->avctx, AV_LOG_DEBUG, "invalid coeff precision\n");
  262. return -1;
  263. }
  264. // av_log(s->avctx, AV_LOG_DEBUG, " qlp coeff prec: %d\n", coeff_prec);
  265. qlevel = get_sbits(&s->gb, 5);
  266. // av_log(s->avctx, AV_LOG_DEBUG, " quant level: %d\n", qlevel);
  267. if(qlevel < 0){
  268. av_log(s->avctx, AV_LOG_DEBUG, "qlevel %d not supported, maybe buggy stream\n", qlevel);
  269. return -1;
  270. }
  271. for (i = 0; i < pred_order; i++)
  272. {
  273. coeffs[i] = get_sbits(&s->gb, coeff_prec);
  274. // av_log(s->avctx, AV_LOG_DEBUG, " %d: %d\n", i, coeffs[i]);
  275. }
  276. if (decode_residuals(s, channel, pred_order) < 0)
  277. return -1;
  278. for (i = pred_order; i < s->blocksize; i++)
  279. {
  280. sum = 0;
  281. for (j = 0; j < pred_order; j++)
  282. sum += coeffs[j] * s->decoded[channel][i-j-1];
  283. s->decoded[channel][i] += sum >> qlevel;
  284. }
  285. return 0;
  286. }
  287. static inline int decode_subframe(FLACContext *s, int channel)
  288. {
  289. int type, wasted = 0;
  290. int i, tmp;
  291. s->curr_bps = s->bps;
  292. if(channel == 0){
  293. if(s->decorrelation == RIGHT_SIDE)
  294. s->curr_bps++;
  295. }else{
  296. if(s->decorrelation == LEFT_SIDE || s->decorrelation == MID_SIDE)
  297. s->curr_bps++;
  298. }
  299. if (get_bits1(&s->gb))
  300. {
  301. av_log(s->avctx, AV_LOG_DEBUG, "invalid subframe padding\n");
  302. return -1;
  303. }
  304. type = get_bits(&s->gb, 6);
  305. // wasted = get_bits1(&s->gb);
  306. // if (wasted)
  307. // {
  308. // while (!get_bits1(&s->gb))
  309. // wasted++;
  310. // if (wasted)
  311. // wasted++;
  312. // s->curr_bps -= wasted;
  313. // }
  314. #if 0
  315. wasted= 16 - av_log2(show_bits(&s->gb, 17));
  316. skip_bits(&s->gb, wasted+1);
  317. s->curr_bps -= wasted;
  318. #else
  319. if (get_bits1(&s->gb))
  320. {
  321. wasted = 1;
  322. while (!get_bits1(&s->gb))
  323. wasted++;
  324. s->curr_bps -= wasted;
  325. av_log(s->avctx, AV_LOG_DEBUG, "%d wasted bits\n", wasted);
  326. }
  327. #endif
  328. //FIXME use av_log2 for types
  329. if (type == 0)
  330. {
  331. av_log(s->avctx, AV_LOG_DEBUG, "coding type: constant\n");
  332. tmp = get_sbits(&s->gb, s->curr_bps);
  333. for (i = 0; i < s->blocksize; i++)
  334. s->decoded[channel][i] = tmp;
  335. }
  336. else if (type == 1)
  337. {
  338. av_log(s->avctx, AV_LOG_DEBUG, "coding type: verbatim\n");
  339. for (i = 0; i < s->blocksize; i++)
  340. s->decoded[channel][i] = get_sbits(&s->gb, s->curr_bps);
  341. }
  342. else if ((type >= 8) && (type <= 12))
  343. {
  344. // av_log(s->avctx, AV_LOG_DEBUG, "coding type: fixed\n");
  345. if (decode_subframe_fixed(s, channel, type & ~0x8) < 0)
  346. return -1;
  347. }
  348. else if (type >= 32)
  349. {
  350. // av_log(s->avctx, AV_LOG_DEBUG, "coding type: lpc\n");
  351. if (decode_subframe_lpc(s, channel, (type & ~0x20)+1) < 0)
  352. return -1;
  353. }
  354. else
  355. {
  356. av_log(s->avctx, AV_LOG_DEBUG, "invalid coding type\n");
  357. return -1;
  358. }
  359. if (wasted)
  360. {
  361. int i;
  362. for (i = 0; i < s->blocksize; i++)
  363. s->decoded[channel][i] <<= wasted;
  364. }
  365. return 0;
  366. }
  367. static int decode_frame(FLACContext *s)
  368. {
  369. int blocksize_code, sample_rate_code, sample_size_code, assignment, i, crc8;
  370. int decorrelation, bps, blocksize, samplerate;
  371. blocksize_code = get_bits(&s->gb, 4);
  372. sample_rate_code = get_bits(&s->gb, 4);
  373. assignment = get_bits(&s->gb, 4); /* channel assignment */
  374. if (assignment < 8 && s->channels == assignment+1)
  375. decorrelation = INDEPENDENT;
  376. else if (assignment >=8 && assignment < 11 && s->channels == 2)
  377. decorrelation = LEFT_SIDE + assignment - 8;
  378. else
  379. {
  380. av_log(s->avctx, AV_LOG_DEBUG, "unsupported channel assignment %d (channels=%d)\n", assignment, s->channels);
  381. return -1;
  382. }
  383. sample_size_code = get_bits(&s->gb, 3);
  384. if(sample_size_code == 0)
  385. bps= s->bps;
  386. else if((sample_size_code != 3) && (sample_size_code != 7))
  387. bps = sample_size_table[sample_size_code];
  388. else
  389. {
  390. av_log(s->avctx, AV_LOG_DEBUG, "invalid sample size code (%d)\n", sample_size_code);
  391. return -1;
  392. }
  393. if (get_bits1(&s->gb))
  394. {
  395. av_log(s->avctx, AV_LOG_DEBUG, "broken stream, invalid padding\n");
  396. return -1;
  397. }
  398. if(get_utf8(&s->gb) < 0){
  399. av_log(s->avctx, AV_LOG_ERROR, "utf8 fscked\n");
  400. return -1;
  401. }
  402. #if 0
  403. if (/*((blocksize_code == 6) || (blocksize_code == 7)) &&*/
  404. (s->min_blocksize != s->max_blocksize)){
  405. }else{
  406. }
  407. #endif
  408. if (blocksize_code == 0)
  409. blocksize = s->min_blocksize;
  410. else if (blocksize_code == 6)
  411. blocksize = get_bits(&s->gb, 8)+1;
  412. else if (blocksize_code == 7)
  413. blocksize = get_bits(&s->gb, 16)+1;
  414. else
  415. blocksize = blocksize_table[blocksize_code];
  416. if(blocksize > s->max_blocksize){
  417. av_log(s->avctx, AV_LOG_ERROR, "blocksize %d > %d\n", blocksize, s->max_blocksize);
  418. return -1;
  419. }
  420. if (sample_rate_code == 0){
  421. samplerate= s->samplerate;
  422. }else if ((sample_rate_code > 3) && (sample_rate_code < 12))
  423. samplerate = sample_rate_table[sample_rate_code];
  424. else if (sample_rate_code == 12)
  425. samplerate = get_bits(&s->gb, 8) * 1000;
  426. else if (sample_rate_code == 13)
  427. samplerate = get_bits(&s->gb, 16);
  428. else if (sample_rate_code == 14)
  429. samplerate = get_bits(&s->gb, 16) * 10;
  430. else{
  431. av_log(s->avctx, AV_LOG_ERROR, "illegal sample rate code %d\n", sample_rate_code);
  432. return -1;
  433. }
  434. skip_bits(&s->gb, 8);
  435. crc8= get_crc8(s->gb.buffer, get_bits_count(&s->gb)/8);
  436. if(crc8){
  437. av_log(s->avctx, AV_LOG_ERROR, "header crc missmatch crc=%2X\n", crc8);
  438. return -1;
  439. }
  440. s->blocksize = blocksize;
  441. s->samplerate = samplerate;
  442. s->bps = bps;
  443. s->decorrelation= decorrelation;
  444. // dump_headers(s);
  445. /* subframes */
  446. for (i = 0; i < s->channels; i++)
  447. {
  448. // av_log(s->avctx, AV_LOG_DEBUG, "decoded: %x residual: %x\n", s->decoded[i], s->residual[i]);
  449. if (decode_subframe(s, i) < 0)
  450. return -1;
  451. }
  452. align_get_bits(&s->gb);
  453. /* frame footer */
  454. skip_bits(&s->gb, 16); /* data crc */
  455. return 0;
  456. }
  457. static int flac_decode_frame(AVCodecContext *avctx,
  458. void *data, int *data_size,
  459. uint8_t *buf, int buf_size)
  460. {
  461. FLACContext *s = avctx->priv_data;
  462. int metadata_last, metadata_type, metadata_size;
  463. int tmp = 0, i, j = 0, input_buf_size;
  464. int16_t *samples = data, *left, *right;
  465. *data_size = 0;
  466. s->avctx = avctx;
  467. if(s->max_framesize == 0){
  468. s->max_framesize= 8192; // should hopefully be enough for the first header
  469. s->bitstream= av_fast_realloc(s->bitstream, &s->allocated_bitstream_size, s->max_framesize);
  470. }
  471. if(1 && s->max_framesize){//FIXME truncated
  472. buf_size= FFMIN(buf_size, s->max_framesize - s->bitstream_size);
  473. input_buf_size= buf_size;
  474. if(s->bitstream_index + s->bitstream_size + buf_size > s->allocated_bitstream_size){
  475. // printf("memmove\n");
  476. memmove(s->bitstream, &s->bitstream[s->bitstream_index], s->bitstream_size);
  477. s->bitstream_index=0;
  478. }
  479. memcpy(&s->bitstream[s->bitstream_index + s->bitstream_size], buf, buf_size);
  480. buf= &s->bitstream[s->bitstream_index];
  481. buf_size += s->bitstream_size;
  482. s->bitstream_size= buf_size;
  483. if(buf_size < s->max_framesize){
  484. // printf("wanna more data ...\n");
  485. return input_buf_size;
  486. }
  487. }
  488. init_get_bits(&s->gb, buf, buf_size*8);
  489. /* fLaC signature (be) */
  490. if (show_bits_long(&s->gb, 32) == bswap_32(ff_get_fourcc("fLaC")))
  491. {
  492. skip_bits(&s->gb, 32);
  493. av_log(s->avctx, AV_LOG_DEBUG, "STREAM HEADER\n");
  494. do {
  495. metadata_last = get_bits(&s->gb, 1);
  496. metadata_type = get_bits(&s->gb, 7);
  497. metadata_size = get_bits_long(&s->gb, 24);
  498. av_log(s->avctx, AV_LOG_DEBUG, " metadata block: flag = %d, type = %d, size = %d\n",
  499. metadata_last, metadata_type,
  500. metadata_size);
  501. if(metadata_size){
  502. switch(metadata_type)
  503. {
  504. case METADATA_TYPE_STREAMINFO:
  505. metadata_streaminfo(s);
  506. dump_headers(s);
  507. break;
  508. default:
  509. for(i=0; i<metadata_size; i++)
  510. skip_bits(&s->gb, 8);
  511. }
  512. }
  513. } while(!metadata_last);
  514. }
  515. else
  516. {
  517. tmp = show_bits(&s->gb, 16);
  518. if(tmp != 0xFFF8){
  519. av_log(s->avctx, AV_LOG_ERROR, "FRAME HEADER not here\n");
  520. while(get_bits_count(&s->gb)/8+2 < buf_size && show_bits(&s->gb, 16) != 0xFFF8)
  521. skip_bits(&s->gb, 8);
  522. goto end; // we may not have enough bits left to decode a frame, so try next time
  523. }
  524. skip_bits(&s->gb, 16);
  525. if (decode_frame(s) < 0){
  526. av_log(s->avctx, AV_LOG_ERROR, "decode_frame() failed\n");
  527. s->bitstream_size=0;
  528. s->bitstream_index=0;
  529. return -1;
  530. }
  531. }
  532. #if 0
  533. /* fix the channel order here */
  534. if (s->order == MID_SIDE)
  535. {
  536. short *left = samples;
  537. short *right = samples + s->blocksize;
  538. for (i = 0; i < s->blocksize; i += 2)
  539. {
  540. uint32_t x = s->decoded[0][i];
  541. uint32_t y = s->decoded[0][i+1];
  542. right[i] = x - (y / 2);
  543. left[i] = right[i] + y;
  544. }
  545. *data_size = 2 * s->blocksize;
  546. }
  547. else
  548. {
  549. for (i = 0; i < s->channels; i++)
  550. {
  551. switch(s->order)
  552. {
  553. case INDEPENDENT:
  554. for (j = 0; j < s->blocksize; j++)
  555. samples[(s->blocksize*i)+j] = s->decoded[i][j];
  556. break;
  557. case LEFT_SIDE:
  558. case RIGHT_SIDE:
  559. if (i == 0)
  560. for (j = 0; j < s->blocksize; j++)
  561. samples[(s->blocksize*i)+j] = s->decoded[0][j];
  562. else
  563. for (j = 0; j < s->blocksize; j++)
  564. samples[(s->blocksize*i)+j] = s->decoded[0][j] - s->decoded[i][j];
  565. break;
  566. // case MID_SIDE:
  567. // av_log(s->avctx, AV_LOG_DEBUG, "mid-side unsupported\n");
  568. }
  569. *data_size += s->blocksize;
  570. }
  571. }
  572. #else
  573. switch(s->decorrelation)
  574. {
  575. case INDEPENDENT:
  576. for (j = 0; j < s->blocksize; j++)
  577. {
  578. for (i = 0; i < s->channels; i++)
  579. *(samples++) = s->decoded[i][j];
  580. }
  581. break;
  582. case LEFT_SIDE:
  583. assert(s->channels == 2);
  584. for (i = 0; i < s->blocksize; i++)
  585. {
  586. *(samples++) = s->decoded[0][i];
  587. *(samples++) = s->decoded[0][i] - s->decoded[1][i];
  588. }
  589. break;
  590. case RIGHT_SIDE:
  591. assert(s->channels == 2);
  592. for (i = 0; i < s->blocksize; i++)
  593. {
  594. *(samples++) = s->decoded[0][i] + s->decoded[1][i];
  595. *(samples++) = s->decoded[1][i];
  596. }
  597. break;
  598. case MID_SIDE:
  599. assert(s->channels == 2);
  600. for (i = 0; i < s->blocksize; i++)
  601. {
  602. int mid, side;
  603. mid = s->decoded[0][i];
  604. side = s->decoded[1][i];
  605. #if 1 //needs to be checked but IMHO it should be binary identical
  606. mid -= side>>1;
  607. *(samples++) = mid + side;
  608. *(samples++) = mid;
  609. #else
  610. mid <<= 1;
  611. if (side & 1)
  612. mid++;
  613. *(samples++) = (mid + side) >> 1;
  614. *(samples++) = (mid - side) >> 1;
  615. #endif
  616. }
  617. break;
  618. }
  619. #endif
  620. *data_size = (int8_t *)samples - (int8_t *)data;
  621. // av_log(s->avctx, AV_LOG_DEBUG, "data size: %d\n", *data_size);
  622. // s->last_blocksize = s->blocksize;
  623. end:
  624. i= (get_bits_count(&s->gb)+7)/8;;
  625. if(i > buf_size){
  626. av_log(s->avctx, AV_LOG_ERROR, "overread: %d\n", i - buf_size);
  627. s->bitstream_size=0;
  628. s->bitstream_index=0;
  629. return -1;
  630. }
  631. if(s->bitstream_size){
  632. s->bitstream_index += i;
  633. s->bitstream_size -= i;
  634. return input_buf_size;
  635. }else
  636. return i;
  637. }
  638. static int flac_decode_close(AVCodecContext *avctx)
  639. {
  640. FLACContext *s = avctx->priv_data;
  641. int i;
  642. for (i = 0; i < s->channels; i++)
  643. {
  644. av_freep(&s->decoded[i]);
  645. }
  646. av_freep(&s->bitstream);
  647. return 0;
  648. }
  649. static void flac_flush(AVCodecContext *avctx){
  650. FLACContext *s = avctx->priv_data;
  651. s->bitstream_size=
  652. s->bitstream_index= 0;
  653. }
  654. AVCodec flac_decoder = {
  655. "flac",
  656. CODEC_TYPE_AUDIO,
  657. CODEC_ID_FLAC,
  658. sizeof(FLACContext),
  659. flac_decode_init,
  660. NULL,
  661. flac_decode_close,
  662. flac_decode_frame,
  663. .flush= flac_flush,
  664. };