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.

957 lines
22KB

  1. /*
  2. * Simple free lossless/lossy audio codec
  3. * Copyright (c) 2004 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. #include "avcodec.h"
  20. #include "golomb.h"
  21. /**
  22. * @file sonic.c
  23. * Simple free lossless/lossy audio codec
  24. * Based on Paul Francis Harrison's Bonk (http://www.logarithmic.net/pfh/bonk)
  25. * Written and designed by Alex Beregszaszi
  26. *
  27. * TODO:
  28. * - CABAC put/get_symbol
  29. * - independent quantizer for channels
  30. * - >2 channels support
  31. * - more decorrelation types
  32. * - more tap_quant tests
  33. * - selectable intlist writers/readers (bonk-style, golomb, cabac)
  34. */
  35. #define MAX_CHANNELS 2
  36. typedef struct SonicContext {
  37. int lossless, mid_side;
  38. int num_taps, downsampling;
  39. double quantization;
  40. int channels, samplerate, block_align, frame_size;
  41. int *tap_quant;
  42. int *int_samples;
  43. int *coded_samples[MAX_CHANNELS];
  44. // for encoding
  45. int *tail;
  46. int tail_size;
  47. int *window;
  48. int window_size;
  49. // for decoding
  50. int *predictor_k;
  51. int *predictor_state[MAX_CHANNELS];
  52. } SonicContext;
  53. #define LATTICE_SHIFT 10
  54. #define SAMPLE_SHIFT 4
  55. #define LATTICE_FACTOR (1 << LATTICE_SHIFT)
  56. #define SAMPLE_FACTOR (1 << SAMPLE_SHIFT)
  57. #define BASE_QUANT 0.6
  58. #define RATE_VARIATION 3.0
  59. static inline int divide(int a, int b)
  60. {
  61. if (a < 0)
  62. return -( (-a + b/2)/b );
  63. else
  64. return (a + b/2)/b;
  65. }
  66. static inline int shift(int a,int b)
  67. {
  68. return (a+(1<<(b-1))) >> b;
  69. }
  70. static inline int shift_down(int a,int b)
  71. {
  72. return (a>>b)+((a<0)?1:0);
  73. }
  74. #if 1
  75. static inline int intlist_write(PutBitContext *pb, int *buf, int entries, int base_2_part)
  76. {
  77. int i;
  78. for (i = 0; i < entries; i++)
  79. set_se_golomb(pb, buf[i]);
  80. return 1;
  81. }
  82. static inline int intlist_read(GetBitContext *gb, int *buf, int entries, int base_2_part)
  83. {
  84. int i;
  85. for (i = 0; i < entries; i++)
  86. buf[i] = get_se_golomb(gb);
  87. return 1;
  88. }
  89. #else
  90. #define ADAPT_LEVEL 8
  91. static int bits_to_store(uint64_t x)
  92. {
  93. int res = 0;
  94. while(x)
  95. {
  96. res++;
  97. x >>= 1;
  98. }
  99. return res;
  100. }
  101. static void write_uint_max(PutBitContext *pb, unsigned int value, unsigned int max)
  102. {
  103. int i, bits;
  104. if (!max)
  105. return;
  106. bits = bits_to_store(max);
  107. for (i = 0; i < bits-1; i++)
  108. put_bits(pb, 1, value & (1 << i));
  109. if ( (value | (1 << (bits-1))) <= max)
  110. put_bits(pb, 1, value & (1 << (bits-1)));
  111. }
  112. static unsigned int read_uint_max(GetBitContext *gb, int max)
  113. {
  114. int i, bits, value = 0;
  115. if (!max)
  116. return 0;
  117. bits = bits_to_store(max);
  118. for (i = 0; i < bits-1; i++)
  119. if (get_bits1(gb))
  120. value += 1 << i;
  121. if ( (value | (1<<(bits-1))) <= max)
  122. if (get_bits1(gb))
  123. value += 1 << (bits-1);
  124. return value;
  125. }
  126. static int intlist_write(PutBitContext *pb, int *buf, int entries, int base_2_part)
  127. {
  128. int i, j, x = 0, low_bits = 0, max = 0;
  129. int step = 256, pos = 0, dominant = 0, any = 0;
  130. int *copy, *bits;
  131. copy = av_mallocz(4* entries);
  132. if (!copy)
  133. return -1;
  134. if (base_2_part)
  135. {
  136. int energy = 0;
  137. for (i = 0; i < entries; i++)
  138. energy += abs(buf[i]);
  139. low_bits = bits_to_store(energy / (entries * 2));
  140. if (low_bits > 15)
  141. low_bits = 15;
  142. put_bits(pb, 4, low_bits);
  143. }
  144. for (i = 0; i < entries; i++)
  145. {
  146. put_bits(pb, low_bits, abs(buf[i]));
  147. copy[i] = abs(buf[i]) >> low_bits;
  148. if (copy[i] > max)
  149. max = abs(copy[i]);
  150. }
  151. bits = av_mallocz(4* entries*max);
  152. if (!bits)
  153. {
  154. // av_free(copy);
  155. return -1;
  156. }
  157. for (i = 0; i <= max; i++)
  158. {
  159. for (j = 0; j < entries; j++)
  160. if (copy[j] >= i)
  161. bits[x++] = copy[j] > i;
  162. }
  163. // store bitstream
  164. while (pos < x)
  165. {
  166. int steplet = step >> 8;
  167. if (pos + steplet > x)
  168. steplet = x - pos;
  169. for (i = 0; i < steplet; i++)
  170. if (bits[i+pos] != dominant)
  171. any = 1;
  172. put_bits(pb, 1, any);
  173. if (!any)
  174. {
  175. pos += steplet;
  176. step += step / ADAPT_LEVEL;
  177. }
  178. else
  179. {
  180. int interloper = 0;
  181. while (((pos + interloper) < x) && (bits[pos + interloper] == dominant))
  182. interloper++;
  183. // note change
  184. write_uint_max(pb, interloper, (step >> 8) - 1);
  185. pos += interloper + 1;
  186. step -= step / ADAPT_LEVEL;
  187. }
  188. if (step < 256)
  189. {
  190. step = 65536 / step;
  191. dominant = !dominant;
  192. }
  193. }
  194. // store signs
  195. for (i = 0; i < entries; i++)
  196. if (buf[i])
  197. put_bits(pb, 1, buf[i] < 0);
  198. // av_free(bits);
  199. // av_free(copy);
  200. return 0;
  201. }
  202. static int intlist_read(GetBitContext *gb, int *buf, int entries, int base_2_part)
  203. {
  204. int i, low_bits = 0, x = 0;
  205. int n_zeros = 0, step = 256, dominant = 0;
  206. int pos = 0, level = 0;
  207. int *bits = av_mallocz(4* entries);
  208. if (!bits)
  209. return -1;
  210. if (base_2_part)
  211. {
  212. low_bits = get_bits(gb, 4);
  213. if (low_bits)
  214. for (i = 0; i < entries; i++)
  215. buf[i] = get_bits(gb, low_bits);
  216. }
  217. // av_log(NULL, AV_LOG_INFO, "entries: %d, low bits: %d\n", entries, low_bits);
  218. while (n_zeros < entries)
  219. {
  220. int steplet = step >> 8;
  221. if (!get_bits1(gb))
  222. {
  223. for (i = 0; i < steplet; i++)
  224. bits[x++] = dominant;
  225. if (!dominant)
  226. n_zeros += steplet;
  227. step += step / ADAPT_LEVEL;
  228. }
  229. else
  230. {
  231. int actual_run = read_uint_max(gb, steplet-1);
  232. // av_log(NULL, AV_LOG_INFO, "actual run: %d\n", actual_run);
  233. for (i = 0; i < actual_run; i++)
  234. bits[x++] = dominant;
  235. bits[x++] = !dominant;
  236. if (!dominant)
  237. n_zeros += actual_run;
  238. else
  239. n_zeros++;
  240. step -= step / ADAPT_LEVEL;
  241. }
  242. if (step < 256)
  243. {
  244. step = 65536 / step;
  245. dominant = !dominant;
  246. }
  247. }
  248. // reconstruct unsigned values
  249. n_zeros = 0;
  250. for (i = 0; n_zeros < entries; i++)
  251. {
  252. while(1)
  253. {
  254. if (pos >= entries)
  255. {
  256. pos = 0;
  257. level += 1 << low_bits;
  258. }
  259. if (buf[pos] >= level)
  260. break;
  261. pos++;
  262. }
  263. if (bits[i])
  264. buf[pos] += 1 << low_bits;
  265. else
  266. n_zeros++;
  267. pos++;
  268. }
  269. // av_free(bits);
  270. // read signs
  271. for (i = 0; i < entries; i++)
  272. if (buf[i] && get_bits1(gb))
  273. buf[i] = -buf[i];
  274. // av_log(NULL, AV_LOG_INFO, "zeros: %d pos: %d\n", n_zeros, pos);
  275. return 0;
  276. }
  277. #endif
  278. static void predictor_init_state(int *k, int *state, int order)
  279. {
  280. int i;
  281. for (i = order-2; i >= 0; i--)
  282. {
  283. int j, p, x = state[i];
  284. for (j = 0, p = i+1; p < order; j++,p++)
  285. {
  286. int tmp = x + shift_down(k[j] * state[p], LATTICE_SHIFT);
  287. state[p] += shift_down(k[j]*x, LATTICE_SHIFT);
  288. x = tmp;
  289. }
  290. }
  291. }
  292. static int predictor_calc_error(int *k, int *state, int order, int error)
  293. {
  294. int i, x = error - shift_down(k[order-1] * state[order-1], LATTICE_SHIFT);
  295. #if 1
  296. int *k_ptr = &(k[order-2]),
  297. *state_ptr = &(state[order-2]);
  298. for (i = order-2; i >= 0; i--, k_ptr--, state_ptr--)
  299. {
  300. int k_value = *k_ptr, state_value = *state_ptr;
  301. x -= shift_down(k_value * state_value, LATTICE_SHIFT);
  302. state_ptr[1] = state_value + shift_down(k_value * x, LATTICE_SHIFT);
  303. }
  304. #else
  305. for (i = order-2; i >= 0; i--)
  306. {
  307. x -= shift_down(k[i] * state[i], LATTICE_SHIFT);
  308. state[i+1] = state[i] + shift_down(k[i] * x, LATTICE_SHIFT);
  309. }
  310. #endif
  311. // don't drift too far, to avoid overflows
  312. if (x > (SAMPLE_FACTOR<<16)) x = (SAMPLE_FACTOR<<16);
  313. if (x < -(SAMPLE_FACTOR<<16)) x = -(SAMPLE_FACTOR<<16);
  314. state[0] = x;
  315. return x;
  316. }
  317. // Heavily modified Levinson-Durbin algorithm which
  318. // copes better with quantization, and calculates the
  319. // actual whitened result as it goes.
  320. static void modified_levinson_durbin(int *window, int window_entries,
  321. int *out, int out_entries, int channels, int *tap_quant)
  322. {
  323. int i;
  324. int *state = av_mallocz(4* window_entries);
  325. memcpy(state, window, 4* window_entries);
  326. for (i = 0; i < out_entries; i++)
  327. {
  328. int step = (i+1)*channels, k, j;
  329. double xx = 0.0, xy = 0.0;
  330. #if 1
  331. int *x_ptr = &(window[step]), *state_ptr = &(state[0]);
  332. j = window_entries - step;
  333. for (;j>=0;j--,x_ptr++,state_ptr++)
  334. {
  335. double x_value = *x_ptr, state_value = *state_ptr;
  336. xx += state_value*state_value;
  337. xy += x_value*state_value;
  338. }
  339. #else
  340. for (j = 0; j <= (window_entries - step); j++);
  341. {
  342. double stepval = window[step+j], stateval = window[j];
  343. // xx += (double)window[j]*(double)window[j];
  344. // xy += (double)window[step+j]*(double)window[j];
  345. xx += stateval*stateval;
  346. xy += stepval*stateval;
  347. }
  348. #endif
  349. if (xx == 0.0)
  350. k = 0;
  351. else
  352. k = (int)(floor(-xy/xx * (double)LATTICE_FACTOR / (double)(tap_quant[i]) + 0.5));
  353. if (k > (LATTICE_FACTOR/tap_quant[i]))
  354. k = LATTICE_FACTOR/tap_quant[i];
  355. if (-k > (LATTICE_FACTOR/tap_quant[i]))
  356. k = -(LATTICE_FACTOR/tap_quant[i]);
  357. out[i] = k;
  358. k *= tap_quant[i];
  359. #if 1
  360. x_ptr = &(window[step]);
  361. state_ptr = &(state[0]);
  362. j = window_entries - step;
  363. for (;j>=0;j--,x_ptr++,state_ptr++)
  364. {
  365. int x_value = *x_ptr, state_value = *state_ptr;
  366. *x_ptr = x_value + shift_down(k*state_value,LATTICE_SHIFT);
  367. *state_ptr = state_value + shift_down(k*x_value, LATTICE_SHIFT);
  368. }
  369. #else
  370. for (j=0; j <= (window_entries - step); j++)
  371. {
  372. int stepval = window[step+j], stateval=state[j];
  373. window[step+j] += shift_down(k * stateval, LATTICE_SHIFT);
  374. state[j] += shift_down(k * stepval, LATTICE_SHIFT);
  375. }
  376. #endif
  377. }
  378. av_free(state);
  379. }
  380. static int samplerate_table[] =
  381. { 44100, 22050, 11025, 96000, 48000, 32000, 24000, 16000, 8000 };
  382. #ifdef CONFIG_ENCODERS
  383. static inline int code_samplerate(int samplerate)
  384. {
  385. switch (samplerate)
  386. {
  387. case 44100: return 0;
  388. case 22050: return 1;
  389. case 11025: return 2;
  390. case 96000: return 3;
  391. case 48000: return 4;
  392. case 32000: return 5;
  393. case 24000: return 6;
  394. case 16000: return 7;
  395. case 8000: return 8;
  396. }
  397. return -1;
  398. }
  399. static int sonic_encode_init(AVCodecContext *avctx)
  400. {
  401. SonicContext *s = avctx->priv_data;
  402. PutBitContext pb;
  403. int i, version = 0;
  404. if (avctx->channels > MAX_CHANNELS)
  405. {
  406. av_log(avctx, AV_LOG_ERROR, "Only mono and stereo streams are supported by now\n");
  407. return -1; /* only stereo or mono for now */
  408. }
  409. if (avctx->channels == 2)
  410. s->mid_side = 1;
  411. if (avctx->codec->id == CODEC_ID_SONIC_LS)
  412. {
  413. s->lossless = 1;
  414. s->num_taps = 32;
  415. s->downsampling = 1;
  416. s->quantization = 0.0;
  417. }
  418. else
  419. {
  420. s->num_taps = 128;
  421. s->downsampling = 2;
  422. s->quantization = 1.0;
  423. }
  424. // max tap 2048
  425. if ((s->num_taps < 32) || (s->num_taps > 1024) ||
  426. ((s->num_taps>>5)<<5 != s->num_taps))
  427. {
  428. av_log(avctx, AV_LOG_ERROR, "Invalid number of taps\n");
  429. return -1;
  430. }
  431. // generate taps
  432. s->tap_quant = av_mallocz(4* s->num_taps);
  433. for (i = 0; i < s->num_taps; i++)
  434. s->tap_quant[i] = (int)(sqrt(i+1));
  435. s->channels = avctx->channels;
  436. s->samplerate = avctx->sample_rate;
  437. s->block_align = (int)(2048.0*s->samplerate/44100)/s->downsampling;
  438. s->frame_size = s->channels*s->block_align*s->downsampling;
  439. s->tail = av_mallocz(4* s->num_taps*s->channels);
  440. if (!s->tail)
  441. return -1;
  442. s->tail_size = s->num_taps*s->channels;
  443. s->predictor_k = av_mallocz(4 * s->num_taps);
  444. if (!s->predictor_k)
  445. return -1;
  446. for (i = 0; i < s->channels; i++)
  447. {
  448. s->coded_samples[i] = av_mallocz(4* s->block_align);
  449. if (!s->coded_samples[i])
  450. return -1;
  451. }
  452. s->int_samples = av_mallocz(4* s->frame_size);
  453. s->window_size = ((2*s->tail_size)+s->frame_size);
  454. s->window = av_mallocz(4* s->window_size);
  455. if (!s->window)
  456. return -1;
  457. avctx->extradata = av_mallocz(16);
  458. if (!avctx->extradata)
  459. return -1;
  460. init_put_bits(&pb, avctx->extradata, 16*8);
  461. put_bits(&pb, 2, version); // version
  462. if (version == 1)
  463. {
  464. put_bits(&pb, 2, s->channels);
  465. put_bits(&pb, 4, code_samplerate(s->samplerate));
  466. }
  467. put_bits(&pb, 1, s->lossless);
  468. if (!s->lossless)
  469. put_bits(&pb, 3, SAMPLE_SHIFT); // XXX FIXME: sample precision
  470. put_bits(&pb, 1, s->mid_side);
  471. put_bits(&pb, 2, s->downsampling);
  472. put_bits(&pb, 5, (s->num_taps >> 5)-1); // 32..1024
  473. put_bits(&pb, 1, 0); // XXX FIXME: no custom tap quant table
  474. flush_put_bits(&pb);
  475. avctx->extradata_size = put_bits_count(&pb)/8;
  476. av_log(avctx, AV_LOG_INFO, "Sonic: ver: %d ls: %d ms: %d taps: %d block: %d frame: %d downsamp: %d\n",
  477. version, s->lossless, s->mid_side, s->num_taps, s->block_align, s->frame_size, s->downsampling);
  478. avctx->coded_frame = avcodec_alloc_frame();
  479. if (!avctx->coded_frame)
  480. return -ENOMEM;
  481. avctx->coded_frame->key_frame = 1;
  482. avctx->frame_size = s->block_align*s->downsampling;
  483. return 0;
  484. }
  485. static int sonic_encode_close(AVCodecContext *avctx)
  486. {
  487. SonicContext *s = avctx->priv_data;
  488. int i;
  489. av_freep(&avctx->coded_frame);
  490. for (i = 0; i < s->channels; i++)
  491. av_free(s->coded_samples[i]);
  492. av_free(s->predictor_k);
  493. av_free(s->tail);
  494. av_free(s->tap_quant);
  495. av_free(s->window);
  496. av_free(s->int_samples);
  497. return 0;
  498. }
  499. static int sonic_encode_frame(AVCodecContext *avctx,
  500. uint8_t *buf, int buf_size, void *data)
  501. {
  502. SonicContext *s = avctx->priv_data;
  503. PutBitContext pb;
  504. int i, j, ch, quant = 0, x = 0;
  505. short *samples = data;
  506. init_put_bits(&pb, buf, buf_size*8);
  507. // short -> internal
  508. for (i = 0; i < s->frame_size; i++)
  509. {
  510. // if (samples[i] < 0)
  511. // s->int_samples[i] = samples[i]+32768;
  512. // else
  513. // s->int_samples[i] = samples[i]-32768;
  514. s->int_samples[i] = samples[i];
  515. // av_log(NULL, AV_LOG_INFO, "%d\n", s->int_samples[i]);
  516. }
  517. if (!s->lossless)
  518. for (i = 0; i < s->frame_size; i++)
  519. s->int_samples[i] = s->int_samples[i] << SAMPLE_SHIFT;
  520. if (s->mid_side)
  521. for (i = 0; i < s->frame_size; i += s->channels)
  522. {
  523. s->int_samples[i] += s->int_samples[i+1];
  524. s->int_samples[i+1] -= shift(s->int_samples[i], 1);
  525. }
  526. memset(s->window, 0, 4* s->window_size);
  527. for (i = 0; i < s->tail_size; i++)
  528. s->window[x++] = s->tail[i];
  529. for (i = 0; i < s->frame_size; i++)
  530. s->window[x++] = s->int_samples[i];
  531. for (i = 0; i < s->tail_size; i++)
  532. s->window[x++] = 0;
  533. for (i = 0; i < s->tail_size; i++)
  534. s->tail[i] = s->int_samples[s->frame_size - s->tail_size + i];
  535. // generate taps
  536. modified_levinson_durbin(s->window, s->window_size,
  537. s->predictor_k, s->num_taps, s->channels, s->tap_quant);
  538. if (intlist_write(&pb, s->predictor_k, s->num_taps, 0) < 0)
  539. return -1;
  540. for (ch = 0; ch < s->channels; ch++)
  541. {
  542. x = s->tail_size+ch;
  543. for (i = 0; i < s->block_align; i++)
  544. {
  545. int sum = 0;
  546. for (j = 0; j < s->downsampling; j++, x += s->channels)
  547. sum += s->window[x];
  548. s->coded_samples[ch][i] = sum;
  549. }
  550. }
  551. // simple rate control code
  552. if (!s->lossless)
  553. {
  554. double energy1 = 0.0, energy2 = 0.0;
  555. for (ch = 0; ch < s->channels; ch++)
  556. {
  557. for (i = 0; i < s->block_align; i++)
  558. {
  559. double sample = s->coded_samples[ch][i];
  560. energy2 += sample*sample;
  561. energy1 += fabs(sample);
  562. }
  563. }
  564. energy2 = sqrt(energy2/(s->channels*s->block_align));
  565. energy1 = sqrt(2.0)*energy1/(s->channels*s->block_align);
  566. // increase bitrate when samples are like a gaussian distribution
  567. // reduce bitrate when samples are like a two-tailed exponential distribution
  568. if (energy2 > energy1)
  569. energy2 += (energy2-energy1)*RATE_VARIATION;
  570. quant = (int)(BASE_QUANT*s->quantization*energy2/SAMPLE_FACTOR);
  571. // av_log(avctx, AV_LOG_DEBUG, "quant: %d energy: %f / %f\n", quant, energy1, energy2);
  572. if (quant < 1)
  573. quant = 1;
  574. if (quant > 65535)
  575. quant = 65535;
  576. set_ue_golomb(&pb, quant);
  577. quant *= SAMPLE_FACTOR;
  578. }
  579. // write out coded samples
  580. for (ch = 0; ch < s->channels; ch++)
  581. {
  582. if (!s->lossless)
  583. for (i = 0; i < s->block_align; i++)
  584. s->coded_samples[ch][i] = divide(s->coded_samples[ch][i], quant);
  585. if (intlist_write(&pb, s->coded_samples[ch], s->block_align, 1) < 0)
  586. return -1;
  587. }
  588. // av_log(avctx, AV_LOG_DEBUG, "used bytes: %d\n", (put_bits_count(&pb)+7)/8);
  589. flush_put_bits(&pb);
  590. return (put_bits_count(&pb)+7)/8;
  591. }
  592. #endif //CONFIG_ENCODERS
  593. static int sonic_decode_init(AVCodecContext *avctx)
  594. {
  595. SonicContext *s = avctx->priv_data;
  596. GetBitContext gb;
  597. int i, version;
  598. s->channels = avctx->channels;
  599. s->samplerate = avctx->sample_rate;
  600. if (!avctx->extradata)
  601. {
  602. av_log(avctx, AV_LOG_ERROR, "No mandatory headers present\n");
  603. return -1;
  604. }
  605. init_get_bits(&gb, avctx->extradata, avctx->extradata_size);
  606. version = get_bits(&gb, 2);
  607. if (version > 1)
  608. {
  609. av_log(avctx, AV_LOG_ERROR, "Unsupported Sonic version, please report\n");
  610. return -1;
  611. }
  612. if (version == 1)
  613. {
  614. s->channels = get_bits(&gb, 2);
  615. s->samplerate = samplerate_table[get_bits(&gb, 4)];
  616. av_log(avctx, AV_LOG_INFO, "Sonicv2 chans: %d samprate: %d\n",
  617. s->channels, s->samplerate);
  618. }
  619. if (s->channels > MAX_CHANNELS)
  620. {
  621. av_log(avctx, AV_LOG_ERROR, "Only mono and stereo streams are supported by now\n");
  622. return -1;
  623. }
  624. s->lossless = get_bits1(&gb);
  625. if (!s->lossless)
  626. skip_bits(&gb, 3); // XXX FIXME
  627. s->mid_side = get_bits1(&gb);
  628. s->downsampling = get_bits(&gb, 2);
  629. s->num_taps = (get_bits(&gb, 5)+1)<<5;
  630. if (get_bits1(&gb)) // XXX FIXME
  631. av_log(avctx, AV_LOG_INFO, "Custom quant table\n");
  632. s->block_align = (int)(2048.0*(s->samplerate/44100))/s->downsampling;
  633. s->frame_size = s->channels*s->block_align*s->downsampling;
  634. // avctx->frame_size = s->block_align;
  635. av_log(avctx, AV_LOG_INFO, "Sonic: ver: %d ls: %d ms: %d taps: %d block: %d frame: %d downsamp: %d\n",
  636. version, s->lossless, s->mid_side, s->num_taps, s->block_align, s->frame_size, s->downsampling);
  637. // generate taps
  638. s->tap_quant = av_mallocz(4* s->num_taps);
  639. for (i = 0; i < s->num_taps; i++)
  640. s->tap_quant[i] = (int)(sqrt(i+1));
  641. s->predictor_k = av_mallocz(4* s->num_taps);
  642. for (i = 0; i < s->channels; i++)
  643. {
  644. s->predictor_state[i] = av_mallocz(4* s->num_taps);
  645. if (!s->predictor_state[i])
  646. return -1;
  647. }
  648. for (i = 0; i < s->channels; i++)
  649. {
  650. s->coded_samples[i] = av_mallocz(4* s->block_align);
  651. if (!s->coded_samples[i])
  652. return -1;
  653. }
  654. s->int_samples = av_mallocz(4* s->frame_size);
  655. return 0;
  656. }
  657. static int sonic_decode_close(AVCodecContext *avctx)
  658. {
  659. SonicContext *s = avctx->priv_data;
  660. int i;
  661. av_free(s->int_samples);
  662. av_free(s->tap_quant);
  663. av_free(s->predictor_k);
  664. for (i = 0; i < s->channels; i++)
  665. {
  666. av_free(s->predictor_state[i]);
  667. av_free(s->coded_samples[i]);
  668. }
  669. return 0;
  670. }
  671. static int sonic_decode_frame(AVCodecContext *avctx,
  672. int16_t *data, int *data_size,
  673. uint8_t *buf, int buf_size)
  674. {
  675. SonicContext *s = avctx->priv_data;
  676. GetBitContext gb;
  677. int i, quant, ch, j;
  678. short *samples = data;
  679. if (buf_size == 0) return 0;
  680. // av_log(NULL, AV_LOG_INFO, "buf_size: %d\n", buf_size);
  681. init_get_bits(&gb, buf, buf_size*8);
  682. intlist_read(&gb, s->predictor_k, s->num_taps, 0);
  683. // dequantize
  684. for (i = 0; i < s->num_taps; i++)
  685. s->predictor_k[i] *= s->tap_quant[i];
  686. if (s->lossless)
  687. quant = 1;
  688. else
  689. quant = get_ue_golomb(&gb) * SAMPLE_FACTOR;
  690. // av_log(NULL, AV_LOG_INFO, "quant: %d\n", quant);
  691. for (ch = 0; ch < s->channels; ch++)
  692. {
  693. int x = ch;
  694. predictor_init_state(s->predictor_k, s->predictor_state[ch], s->num_taps);
  695. intlist_read(&gb, s->coded_samples[ch], s->block_align, 1);
  696. for (i = 0; i < s->block_align; i++)
  697. {
  698. for (j = 0; j < s->downsampling - 1; j++)
  699. {
  700. s->int_samples[x] = predictor_calc_error(s->predictor_k, s->predictor_state[ch], s->num_taps, 0);
  701. x += s->channels;
  702. }
  703. s->int_samples[x] = predictor_calc_error(s->predictor_k, s->predictor_state[ch], s->num_taps, s->coded_samples[ch][i] * quant);
  704. x += s->channels;
  705. }
  706. for (i = 0; i < s->num_taps; i++)
  707. s->predictor_state[ch][i] = s->int_samples[s->frame_size - s->channels + ch - i*s->channels];
  708. }
  709. if (s->mid_side)
  710. for (i = 0; i < s->frame_size; i += s->channels)
  711. {
  712. s->int_samples[i+1] += shift(s->int_samples[i], 1);
  713. s->int_samples[i] -= s->int_samples[i+1];
  714. }
  715. if (!s->lossless)
  716. for (i = 0; i < s->frame_size; i++)
  717. s->int_samples[i] = shift(s->int_samples[i], SAMPLE_SHIFT);
  718. // internal -> short
  719. for (i = 0; i < s->frame_size; i++)
  720. {
  721. if (s->int_samples[i] > 32767)
  722. samples[i] = 32767;
  723. else if (s->int_samples[i] < -32768)
  724. samples[i] = -32768;
  725. else
  726. samples[i] = s->int_samples[i];
  727. }
  728. align_get_bits(&gb);
  729. // if (buf_size != (get_bits_count(&gb)+7)/8)
  730. // av_log(NULL, AV_LOG_INFO, "buf_size (%d) and used bytes (%d) differs\n", buf_size, (get_bits_count(&gb)+7)/8);
  731. *data_size = s->frame_size * 2;
  732. return (get_bits_count(&gb)+7)/8;
  733. }
  734. #ifdef CONFIG_ENCODERS
  735. AVCodec sonic_encoder = {
  736. "sonic",
  737. CODEC_TYPE_AUDIO,
  738. CODEC_ID_SONIC,
  739. sizeof(SonicContext),
  740. sonic_encode_init,
  741. sonic_encode_frame,
  742. sonic_encode_close,
  743. NULL,
  744. };
  745. AVCodec sonic_ls_encoder = {
  746. "sonicls",
  747. CODEC_TYPE_AUDIO,
  748. CODEC_ID_SONIC_LS,
  749. sizeof(SonicContext),
  750. sonic_encode_init,
  751. sonic_encode_frame,
  752. sonic_encode_close,
  753. NULL,
  754. };
  755. #endif
  756. #ifdef CONFIG_DECODERS
  757. AVCodec sonic_decoder = {
  758. "sonic",
  759. CODEC_TYPE_AUDIO,
  760. CODEC_ID_SONIC,
  761. sizeof(SonicContext),
  762. sonic_decode_init,
  763. NULL,
  764. sonic_decode_close,
  765. sonic_decode_frame,
  766. };
  767. #endif