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.

980 lines
24KB

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