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.

976 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. // Heavily modified Levinson-Durbin algorithm which
  322. // copes better with quantization, and calculates the
  323. // actual whitened result as it goes.
  324. static void modified_levinson_durbin(int *window, int window_entries,
  325. int *out, int out_entries, int channels, int *tap_quant)
  326. {
  327. int i;
  328. int *state = av_mallocz(4* window_entries);
  329. memcpy(state, window, 4* window_entries);
  330. for (i = 0; i < out_entries; i++)
  331. {
  332. int step = (i+1)*channels, k, j;
  333. double xx = 0.0, xy = 0.0;
  334. #if 1
  335. int *x_ptr = &(window[step]), *state_ptr = &(state[0]);
  336. j = window_entries - step;
  337. for (;j>=0;j--,x_ptr++,state_ptr++)
  338. {
  339. double x_value = *x_ptr, state_value = *state_ptr;
  340. xx += state_value*state_value;
  341. xy += x_value*state_value;
  342. }
  343. #else
  344. for (j = 0; j <= (window_entries - step); j++);
  345. {
  346. double stepval = window[step+j], stateval = window[j];
  347. // xx += (double)window[j]*(double)window[j];
  348. // xy += (double)window[step+j]*(double)window[j];
  349. xx += stateval*stateval;
  350. xy += stepval*stateval;
  351. }
  352. #endif
  353. if (xx == 0.0)
  354. k = 0;
  355. else
  356. k = (int)(floor(-xy/xx * (double)LATTICE_FACTOR / (double)(tap_quant[i]) + 0.5));
  357. if (k > (LATTICE_FACTOR/tap_quant[i]))
  358. k = LATTICE_FACTOR/tap_quant[i];
  359. if (-k > (LATTICE_FACTOR/tap_quant[i]))
  360. k = -(LATTICE_FACTOR/tap_quant[i]);
  361. out[i] = k;
  362. k *= tap_quant[i];
  363. #if 1
  364. x_ptr = &(window[step]);
  365. state_ptr = &(state[0]);
  366. j = window_entries - step;
  367. for (;j>=0;j--,x_ptr++,state_ptr++)
  368. {
  369. int x_value = *x_ptr, state_value = *state_ptr;
  370. *x_ptr = x_value + shift_down(k*state_value,LATTICE_SHIFT);
  371. *state_ptr = state_value + shift_down(k*x_value, LATTICE_SHIFT);
  372. }
  373. #else
  374. for (j=0; j <= (window_entries - step); j++)
  375. {
  376. int stepval = window[step+j], stateval=state[j];
  377. window[step+j] += shift_down(k * stateval, LATTICE_SHIFT);
  378. state[j] += shift_down(k * stepval, LATTICE_SHIFT);
  379. }
  380. #endif
  381. }
  382. av_free(state);
  383. }
  384. static int samplerate_table[] =
  385. { 44100, 22050, 11025, 96000, 48000, 32000, 24000, 16000, 8000 };
  386. #ifdef CONFIG_ENCODERS
  387. static inline int code_samplerate(int samplerate)
  388. {
  389. switch (samplerate)
  390. {
  391. case 44100: return 0;
  392. case 22050: return 1;
  393. case 11025: return 2;
  394. case 96000: return 3;
  395. case 48000: return 4;
  396. case 32000: return 5;
  397. case 24000: return 6;
  398. case 16000: return 7;
  399. case 8000: return 8;
  400. }
  401. return -1;
  402. }
  403. static int sonic_encode_init(AVCodecContext *avctx)
  404. {
  405. SonicContext *s = avctx->priv_data;
  406. PutBitContext pb;
  407. int i, version = 0;
  408. if (avctx->channels > MAX_CHANNELS)
  409. {
  410. av_log(avctx, AV_LOG_ERROR, "Only mono and stereo streams are supported by now\n");
  411. return -1; /* only stereo or mono for now */
  412. }
  413. if (avctx->channels == 2)
  414. s->decorrelation = MID_SIDE;
  415. if (avctx->codec->id == CODEC_ID_SONIC_LS)
  416. {
  417. s->lossless = 1;
  418. s->num_taps = 32;
  419. s->downsampling = 1;
  420. s->quantization = 0.0;
  421. }
  422. else
  423. {
  424. s->num_taps = 128;
  425. s->downsampling = 2;
  426. s->quantization = 1.0;
  427. }
  428. // max tap 2048
  429. if ((s->num_taps < 32) || (s->num_taps > 1024) ||
  430. ((s->num_taps>>5)<<5 != s->num_taps))
  431. {
  432. av_log(avctx, AV_LOG_ERROR, "Invalid number of taps\n");
  433. return -1;
  434. }
  435. // generate taps
  436. s->tap_quant = av_mallocz(4* s->num_taps);
  437. for (i = 0; i < s->num_taps; i++)
  438. s->tap_quant[i] = (int)(sqrt(i+1));
  439. s->channels = avctx->channels;
  440. s->samplerate = avctx->sample_rate;
  441. s->block_align = (int)(2048.0*s->samplerate/44100)/s->downsampling;
  442. s->frame_size = s->channels*s->block_align*s->downsampling;
  443. s->tail = av_mallocz(4* s->num_taps*s->channels);
  444. if (!s->tail)
  445. return -1;
  446. s->tail_size = s->num_taps*s->channels;
  447. s->predictor_k = av_mallocz(4 * s->num_taps);
  448. if (!s->predictor_k)
  449. return -1;
  450. for (i = 0; i < s->channels; i++)
  451. {
  452. s->coded_samples[i] = av_mallocz(4* s->block_align);
  453. if (!s->coded_samples[i])
  454. return -1;
  455. }
  456. s->int_samples = av_mallocz(4* s->frame_size);
  457. s->window_size = ((2*s->tail_size)+s->frame_size);
  458. s->window = av_mallocz(4* s->window_size);
  459. if (!s->window)
  460. return -1;
  461. avctx->extradata = av_mallocz(16);
  462. if (!avctx->extradata)
  463. return -1;
  464. init_put_bits(&pb, avctx->extradata, 16*8);
  465. put_bits(&pb, 2, version); // version
  466. if (version == 1)
  467. {
  468. put_bits(&pb, 2, s->channels);
  469. put_bits(&pb, 4, code_samplerate(s->samplerate));
  470. }
  471. put_bits(&pb, 1, s->lossless);
  472. if (!s->lossless)
  473. put_bits(&pb, 3, SAMPLE_SHIFT); // XXX FIXME: sample precision
  474. put_bits(&pb, 2, s->decorrelation);
  475. put_bits(&pb, 2, s->downsampling);
  476. put_bits(&pb, 5, (s->num_taps >> 5)-1); // 32..1024
  477. put_bits(&pb, 1, 0); // XXX FIXME: no custom tap quant table
  478. flush_put_bits(&pb);
  479. avctx->extradata_size = put_bits_count(&pb)/8;
  480. av_log(avctx, AV_LOG_INFO, "Sonic: ver: %d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
  481. version, s->lossless, s->decorrelation, s->num_taps, s->block_align, s->frame_size, s->downsampling);
  482. avctx->coded_frame = avcodec_alloc_frame();
  483. if (!avctx->coded_frame)
  484. return -ENOMEM;
  485. avctx->coded_frame->key_frame = 1;
  486. avctx->frame_size = s->block_align*s->downsampling;
  487. return 0;
  488. }
  489. static int sonic_encode_close(AVCodecContext *avctx)
  490. {
  491. SonicContext *s = avctx->priv_data;
  492. int i;
  493. av_freep(&avctx->coded_frame);
  494. for (i = 0; i < s->channels; i++)
  495. av_free(s->coded_samples[i]);
  496. av_free(s->predictor_k);
  497. av_free(s->tail);
  498. av_free(s->tap_quant);
  499. av_free(s->window);
  500. av_free(s->int_samples);
  501. return 0;
  502. }
  503. static int sonic_encode_frame(AVCodecContext *avctx,
  504. uint8_t *buf, int buf_size, void *data)
  505. {
  506. SonicContext *s = avctx->priv_data;
  507. PutBitContext pb;
  508. int i, j, ch, quant = 0, x = 0;
  509. short *samples = data;
  510. init_put_bits(&pb, buf, buf_size*8);
  511. // short -> internal
  512. for (i = 0; i < s->frame_size; i++)
  513. s->int_samples[i] = samples[i];
  514. if (!s->lossless)
  515. for (i = 0; i < s->frame_size; i++)
  516. s->int_samples[i] = s->int_samples[i] << SAMPLE_SHIFT;
  517. switch(s->decorrelation)
  518. {
  519. case MID_SIDE:
  520. for (i = 0; i < s->frame_size; i += s->channels)
  521. {
  522. s->int_samples[i] += s->int_samples[i+1];
  523. s->int_samples[i+1] -= shift(s->int_samples[i], 1);
  524. }
  525. break;
  526. case LEFT_SIDE:
  527. for (i = 0; i < s->frame_size; i += s->channels)
  528. s->int_samples[i+1] -= s->int_samples[i];
  529. break;
  530. case RIGHT_SIDE:
  531. for (i = 0; i < s->frame_size; i += s->channels)
  532. s->int_samples[i] -= s->int_samples[i+1];
  533. break;
  534. }
  535. memset(s->window, 0, 4* s->window_size);
  536. for (i = 0; i < s->tail_size; i++)
  537. s->window[x++] = s->tail[i];
  538. for (i = 0; i < s->frame_size; i++)
  539. s->window[x++] = s->int_samples[i];
  540. for (i = 0; i < s->tail_size; i++)
  541. s->window[x++] = 0;
  542. for (i = 0; i < s->tail_size; i++)
  543. s->tail[i] = s->int_samples[s->frame_size - s->tail_size + i];
  544. // generate taps
  545. modified_levinson_durbin(s->window, s->window_size,
  546. s->predictor_k, s->num_taps, s->channels, s->tap_quant);
  547. if (intlist_write(&pb, s->predictor_k, s->num_taps, 0) < 0)
  548. return -1;
  549. for (ch = 0; ch < s->channels; ch++)
  550. {
  551. x = s->tail_size+ch;
  552. for (i = 0; i < s->block_align; i++)
  553. {
  554. int sum = 0;
  555. for (j = 0; j < s->downsampling; j++, x += s->channels)
  556. sum += s->window[x];
  557. s->coded_samples[ch][i] = sum;
  558. }
  559. }
  560. // simple rate control code
  561. if (!s->lossless)
  562. {
  563. double energy1 = 0.0, energy2 = 0.0;
  564. for (ch = 0; ch < s->channels; ch++)
  565. {
  566. for (i = 0; i < s->block_align; i++)
  567. {
  568. double sample = s->coded_samples[ch][i];
  569. energy2 += sample*sample;
  570. energy1 += fabs(sample);
  571. }
  572. }
  573. energy2 = sqrt(energy2/(s->channels*s->block_align));
  574. energy1 = sqrt(2.0)*energy1/(s->channels*s->block_align);
  575. // increase bitrate when samples are like a gaussian distribution
  576. // reduce bitrate when samples are like a two-tailed exponential distribution
  577. if (energy2 > energy1)
  578. energy2 += (energy2-energy1)*RATE_VARIATION;
  579. quant = (int)(BASE_QUANT*s->quantization*energy2/SAMPLE_FACTOR);
  580. // av_log(avctx, AV_LOG_DEBUG, "quant: %d energy: %f / %f\n", quant, energy1, energy2);
  581. if (quant < 1)
  582. quant = 1;
  583. if (quant > 65535)
  584. quant = 65535;
  585. set_ue_golomb(&pb, quant);
  586. quant *= SAMPLE_FACTOR;
  587. }
  588. // write out coded samples
  589. for (ch = 0; ch < s->channels; ch++)
  590. {
  591. if (!s->lossless)
  592. for (i = 0; i < s->block_align; i++)
  593. s->coded_samples[ch][i] = divide(s->coded_samples[ch][i], quant);
  594. if (intlist_write(&pb, s->coded_samples[ch], s->block_align, 1) < 0)
  595. return -1;
  596. }
  597. // av_log(avctx, AV_LOG_DEBUG, "used bytes: %d\n", (put_bits_count(&pb)+7)/8);
  598. flush_put_bits(&pb);
  599. return (put_bits_count(&pb)+7)/8;
  600. }
  601. #endif //CONFIG_ENCODERS
  602. static int sonic_decode_init(AVCodecContext *avctx)
  603. {
  604. SonicContext *s = avctx->priv_data;
  605. GetBitContext gb;
  606. int i, version;
  607. s->channels = avctx->channels;
  608. s->samplerate = avctx->sample_rate;
  609. if (!avctx->extradata)
  610. {
  611. av_log(avctx, AV_LOG_ERROR, "No mandatory headers present\n");
  612. return -1;
  613. }
  614. init_get_bits(&gb, avctx->extradata, avctx->extradata_size);
  615. version = get_bits(&gb, 2);
  616. if (version > 1)
  617. {
  618. av_log(avctx, AV_LOG_ERROR, "Unsupported Sonic version, please report\n");
  619. return -1;
  620. }
  621. if (version == 1)
  622. {
  623. s->channels = get_bits(&gb, 2);
  624. s->samplerate = samplerate_table[get_bits(&gb, 4)];
  625. av_log(avctx, AV_LOG_INFO, "Sonicv2 chans: %d samprate: %d\n",
  626. s->channels, s->samplerate);
  627. }
  628. if (s->channels > MAX_CHANNELS)
  629. {
  630. av_log(avctx, AV_LOG_ERROR, "Only mono and stereo streams are supported by now\n");
  631. return -1;
  632. }
  633. s->lossless = get_bits1(&gb);
  634. if (!s->lossless)
  635. skip_bits(&gb, 3); // XXX FIXME
  636. s->decorrelation = get_bits(&gb, 2);
  637. s->downsampling = get_bits(&gb, 2);
  638. s->num_taps = (get_bits(&gb, 5)+1)<<5;
  639. if (get_bits1(&gb)) // XXX FIXME
  640. av_log(avctx, AV_LOG_INFO, "Custom quant table\n");
  641. s->block_align = (int)(2048.0*(s->samplerate/44100))/s->downsampling;
  642. s->frame_size = s->channels*s->block_align*s->downsampling;
  643. // avctx->frame_size = s->block_align;
  644. av_log(avctx, AV_LOG_INFO, "Sonic: ver: %d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
  645. version, s->lossless, s->decorrelation, s->num_taps, s->block_align, s->frame_size, s->downsampling);
  646. // generate taps
  647. s->tap_quant = av_mallocz(4* s->num_taps);
  648. for (i = 0; i < s->num_taps; i++)
  649. s->tap_quant[i] = (int)(sqrt(i+1));
  650. s->predictor_k = av_mallocz(4* s->num_taps);
  651. for (i = 0; i < s->channels; i++)
  652. {
  653. s->predictor_state[i] = av_mallocz(4* s->num_taps);
  654. if (!s->predictor_state[i])
  655. return -1;
  656. }
  657. for (i = 0; i < s->channels; i++)
  658. {
  659. s->coded_samples[i] = av_mallocz(4* s->block_align);
  660. if (!s->coded_samples[i])
  661. return -1;
  662. }
  663. s->int_samples = av_mallocz(4* s->frame_size);
  664. return 0;
  665. }
  666. static int sonic_decode_close(AVCodecContext *avctx)
  667. {
  668. SonicContext *s = avctx->priv_data;
  669. int i;
  670. av_free(s->int_samples);
  671. av_free(s->tap_quant);
  672. av_free(s->predictor_k);
  673. for (i = 0; i < s->channels; i++)
  674. {
  675. av_free(s->predictor_state[i]);
  676. av_free(s->coded_samples[i]);
  677. }
  678. return 0;
  679. }
  680. static int sonic_decode_frame(AVCodecContext *avctx,
  681. void *data, int *data_size,
  682. uint8_t *buf, int buf_size)
  683. {
  684. SonicContext *s = avctx->priv_data;
  685. GetBitContext gb;
  686. int i, quant, ch, j;
  687. short *samples = data;
  688. if (buf_size == 0) return 0;
  689. // av_log(NULL, AV_LOG_INFO, "buf_size: %d\n", buf_size);
  690. init_get_bits(&gb, buf, buf_size*8);
  691. intlist_read(&gb, s->predictor_k, s->num_taps, 0);
  692. // dequantize
  693. for (i = 0; i < s->num_taps; i++)
  694. s->predictor_k[i] *= s->tap_quant[i];
  695. if (s->lossless)
  696. quant = 1;
  697. else
  698. quant = get_ue_golomb(&gb) * SAMPLE_FACTOR;
  699. // av_log(NULL, AV_LOG_INFO, "quant: %d\n", quant);
  700. for (ch = 0; ch < s->channels; ch++)
  701. {
  702. int x = ch;
  703. predictor_init_state(s->predictor_k, s->predictor_state[ch], s->num_taps);
  704. intlist_read(&gb, s->coded_samples[ch], s->block_align, 1);
  705. for (i = 0; i < s->block_align; i++)
  706. {
  707. for (j = 0; j < s->downsampling - 1; j++)
  708. {
  709. s->int_samples[x] = predictor_calc_error(s->predictor_k, s->predictor_state[ch], s->num_taps, 0);
  710. x += s->channels;
  711. }
  712. s->int_samples[x] = predictor_calc_error(s->predictor_k, s->predictor_state[ch], s->num_taps, s->coded_samples[ch][i] * quant);
  713. x += s->channels;
  714. }
  715. for (i = 0; i < s->num_taps; i++)
  716. s->predictor_state[ch][i] = s->int_samples[s->frame_size - s->channels + ch - i*s->channels];
  717. }
  718. switch(s->decorrelation)
  719. {
  720. case MID_SIDE:
  721. for (i = 0; i < s->frame_size; i += s->channels)
  722. {
  723. s->int_samples[i+1] += shift(s->int_samples[i], 1);
  724. s->int_samples[i] -= s->int_samples[i+1];
  725. }
  726. break;
  727. case LEFT_SIDE:
  728. for (i = 0; i < s->frame_size; i += s->channels)
  729. s->int_samples[i+1] += s->int_samples[i];
  730. break;
  731. case RIGHT_SIDE:
  732. for (i = 0; i < s->frame_size; i += s->channels)
  733. s->int_samples[i] += s->int_samples[i+1];
  734. break;
  735. }
  736. if (!s->lossless)
  737. for (i = 0; i < s->frame_size; i++)
  738. s->int_samples[i] = shift(s->int_samples[i], SAMPLE_SHIFT);
  739. // internal -> short
  740. for (i = 0; i < s->frame_size; i++)
  741. {
  742. if (s->int_samples[i] > 32767)
  743. samples[i] = 32767;
  744. else if (s->int_samples[i] < -32768)
  745. samples[i] = -32768;
  746. else
  747. samples[i] = s->int_samples[i];
  748. }
  749. align_get_bits(&gb);
  750. *data_size = s->frame_size * 2;
  751. return (get_bits_count(&gb)+7)/8;
  752. }
  753. #ifdef CONFIG_ENCODERS
  754. AVCodec sonic_encoder = {
  755. "sonic",
  756. CODEC_TYPE_AUDIO,
  757. CODEC_ID_SONIC,
  758. sizeof(SonicContext),
  759. sonic_encode_init,
  760. sonic_encode_frame,
  761. sonic_encode_close,
  762. NULL,
  763. };
  764. AVCodec sonic_ls_encoder = {
  765. "sonicls",
  766. CODEC_TYPE_AUDIO,
  767. CODEC_ID_SONIC_LS,
  768. sizeof(SonicContext),
  769. sonic_encode_init,
  770. sonic_encode_frame,
  771. sonic_encode_close,
  772. NULL,
  773. };
  774. #endif
  775. #ifdef CONFIG_DECODERS
  776. AVCodec sonic_decoder = {
  777. "sonic",
  778. CODEC_TYPE_AUDIO,
  779. CODEC_ID_SONIC,
  780. sizeof(SonicContext),
  781. sonic_decode_init,
  782. NULL,
  783. sonic_decode_close,
  784. sonic_decode_frame,
  785. };
  786. #endif