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.

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