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.

951 lines
21KB

  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. return -1; /* only stereo or mono for now */
  406. if (avctx->channels == 2)
  407. s->mid_side = 1;
  408. if (avctx->codec->id == CODEC_ID_SONIC_LS)
  409. {
  410. s->lossless = 1;
  411. s->num_taps = 32;
  412. s->downsampling = 1;
  413. s->quantization = 0.0;
  414. }
  415. else
  416. {
  417. s->num_taps = 128;
  418. s->downsampling = 2;
  419. s->quantization = 1.0;
  420. }
  421. // max tap 2048
  422. if ((s->num_taps < 32) || (s->num_taps > 1024) ||
  423. ((s->num_taps>>5)<<5 != s->num_taps))
  424. {
  425. av_log(avctx, AV_LOG_ERROR, "Invalid number of taps\n");
  426. return -1;
  427. }
  428. // generate taps
  429. s->tap_quant = av_mallocz(4* s->num_taps);
  430. for (i = 0; i < s->num_taps; i++)
  431. s->tap_quant[i] = (int)(sqrt(i+1));
  432. s->channels = avctx->channels;
  433. s->samplerate = avctx->sample_rate;
  434. s->block_align = (int)(2048.0*s->samplerate/44100)/s->downsampling;
  435. s->frame_size = s->channels*s->block_align*s->downsampling;
  436. s->tail = av_mallocz(4* s->num_taps*s->channels);
  437. if (!s->tail)
  438. return -1;
  439. s->tail_size = s->num_taps*s->channels;
  440. s->predictor_k = av_mallocz(4 * s->num_taps);
  441. if (!s->predictor_k)
  442. return -1;
  443. for (i = 0; i < s->channels; i++)
  444. {
  445. s->coded_samples[i] = av_mallocz(4* s->block_align);
  446. if (!s->coded_samples[i])
  447. return -1;
  448. }
  449. s->int_samples = av_mallocz(4* s->frame_size);
  450. s->window_size = ((2*s->tail_size)+s->frame_size);
  451. s->window = av_mallocz(4* s->window_size);
  452. if (!s->window)
  453. return -1;
  454. avctx->extradata = av_mallocz(16);
  455. if (!avctx->extradata)
  456. return -1;
  457. init_put_bits(&pb, avctx->extradata, 16*8);
  458. put_bits(&pb, 2, version); // version
  459. if (version == 1)
  460. {
  461. put_bits(&pb, 2, s->channels);
  462. put_bits(&pb, 4, code_samplerate(s->samplerate));
  463. }
  464. put_bits(&pb, 1, s->lossless);
  465. if (!s->lossless)
  466. put_bits(&pb, 3, SAMPLE_SHIFT); // XXX FIXME: sample precision
  467. put_bits(&pb, 1, s->mid_side);
  468. put_bits(&pb, 2, s->downsampling);
  469. put_bits(&pb, 5, (s->num_taps >> 5)-1); // 32..1024
  470. put_bits(&pb, 1, 0); // XXX FIXME: no custom tap quant table
  471. flush_put_bits(&pb);
  472. avctx->extradata_size = put_bits_count(&pb)/8;
  473. av_log(avctx, AV_LOG_INFO, "Sonic: ver: %d ls: %d ms: %d taps: %d block: %d frame: %d downsamp: %d\n",
  474. version, s->lossless, s->mid_side, s->num_taps, s->block_align, s->frame_size, s->downsampling);
  475. avctx->coded_frame = avcodec_alloc_frame();
  476. if (!avctx->coded_frame)
  477. return -ENOMEM;
  478. avctx->coded_frame->key_frame = 1;
  479. avctx->frame_size = s->block_align*s->downsampling;
  480. return 0;
  481. }
  482. static int sonic_encode_close(AVCodecContext *avctx)
  483. {
  484. SonicContext *s = avctx->priv_data;
  485. int i;
  486. av_freep(&avctx->coded_frame);
  487. for (i = 0; i < s->channels; i++)
  488. av_free(s->coded_samples[i]);
  489. av_free(s->predictor_k);
  490. av_free(s->tail);
  491. av_free(s->tap_quant);
  492. av_free(s->window);
  493. av_free(s->int_samples);
  494. return 0;
  495. }
  496. static int sonic_encode_frame(AVCodecContext *avctx,
  497. uint8_t *buf, int buf_size, void *data)
  498. {
  499. SonicContext *s = avctx->priv_data;
  500. PutBitContext pb;
  501. int i, j, ch, quant = 0, x = 0;
  502. short *samples = data;
  503. init_put_bits(&pb, buf, buf_size*8);
  504. // short -> internal
  505. for (i = 0; i < s->frame_size; i++)
  506. {
  507. if (samples[i] < 0)
  508. s->int_samples[i] = samples[i]+32768;
  509. else
  510. s->int_samples[i] = samples[i]-32768;
  511. }
  512. if (!s->lossless)
  513. for (i = 0; i < s->frame_size; i++)
  514. s->int_samples[i] = s->int_samples[i] << SAMPLE_SHIFT;
  515. if (s->mid_side)
  516. for (i = 0; i < s->frame_size; i += s->channels)
  517. {
  518. s->int_samples[i] += s->int_samples[i+1];
  519. s->int_samples[i+1] -= shift(s->int_samples[i], 1);
  520. }
  521. memset(s->window, 0, 4* s->window_size);
  522. for (i = 0; i < s->tail_size; i++)
  523. s->window[x++] = s->tail[i];
  524. for (i = 0; i < s->frame_size; i++)
  525. s->window[x++] = s->int_samples[i];
  526. for (i = 0; i < s->tail_size; i++)
  527. s->window[x++] = 0;
  528. for (i = 0; i < s->tail_size; i++)
  529. s->tail[i] = s->int_samples[s->frame_size - s->tail_size + i];
  530. // generate taps
  531. modified_levinson_durbin(s->window, s->window_size,
  532. s->predictor_k, s->num_taps, s->channels, s->tap_quant);
  533. if (intlist_write(&pb, s->predictor_k, s->num_taps, 0) < 0)
  534. return -1;
  535. for (ch = 0; ch < s->channels; ch++)
  536. {
  537. x = s->tail_size+ch;
  538. for (i = 0; i < s->block_align; i++)
  539. {
  540. int sum = 0;
  541. for (j = 0; j < s->downsampling; j++, x += s->channels)
  542. sum += s->window[x];
  543. s->coded_samples[ch][i] = sum;
  544. }
  545. }
  546. // simple rate control code
  547. if (!s->lossless)
  548. {
  549. double energy1 = 0.0, energy2 = 0.0;
  550. for (ch = 0; ch < s->channels; ch++)
  551. {
  552. for (i = 0; i < s->block_align; i++)
  553. {
  554. double sample = s->coded_samples[ch][i];
  555. energy2 += sample*sample;
  556. energy1 += fabs(sample);
  557. }
  558. }
  559. energy2 = sqrt(energy2/(s->channels*s->block_align));
  560. energy1 = sqrt(2.0)*energy1/(s->channels*s->block_align);
  561. // increase bitrate when samples are like a gaussian distribution
  562. // reduce bitrate when samples are like a two-tailed exponential distribution
  563. if (energy2 > energy1)
  564. energy2 += (energy2-energy1)*RATE_VARIATION;
  565. quant = (int)(BASE_QUANT*s->quantization*energy2/SAMPLE_FACTOR);
  566. // av_log(avctx, AV_LOG_DEBUG, "quant: %d energy: %f / %f\n", quant, energy1, energy2);
  567. if (quant < 1)
  568. quant = 1;
  569. if (quant > 65535)
  570. quant = 65535;
  571. set_ue_golomb(&pb, quant);
  572. quant *= SAMPLE_FACTOR;
  573. }
  574. // write out coded samples
  575. for (ch = 0; ch < s->channels; ch++)
  576. {
  577. if (!s->lossless)
  578. for (i = 0; i < s->block_align; i++)
  579. s->coded_samples[ch][i] = divide(s->coded_samples[ch][i], quant);
  580. if (intlist_write(&pb, s->coded_samples[ch], s->block_align, 1) < 0)
  581. return -1;
  582. }
  583. // av_log(avctx, AV_LOG_DEBUG, "used bytes: %d\n", (put_bits_count(&pb)+7)/8);
  584. flush_put_bits(&pb);
  585. return (put_bits_count(&pb)+7)/8;
  586. }
  587. #endif //CONFIG_ENCODERS
  588. static int sonic_decode_init(AVCodecContext *avctx)
  589. {
  590. SonicContext *s = avctx->priv_data;
  591. GetBitContext gb;
  592. int i, version;
  593. s->channels = avctx->channels;
  594. s->samplerate = avctx->sample_rate;
  595. if (!avctx->extradata)
  596. {
  597. av_log(avctx, AV_LOG_ERROR, "No mandatory headers present\n");
  598. return -1;
  599. }
  600. init_get_bits(&gb, avctx->extradata, avctx->extradata_size);
  601. version = get_bits(&gb, 2);
  602. if (version > 1)
  603. {
  604. av_log(avctx, AV_LOG_ERROR, "Unsupported Sonic version, please report\n");
  605. return -1;
  606. }
  607. if (version == 1)
  608. {
  609. s->channels = get_bits(&gb, 2);
  610. s->samplerate = samplerate_table[get_bits(&gb, 4)];
  611. av_log(avctx, AV_LOG_INFO, "Sonicv2 chans: %d samprate: %d\n",
  612. s->channels, s->samplerate);
  613. }
  614. if (s->channels > MAX_CHANNELS)
  615. {
  616. av_log(avctx, AV_LOG_ERROR, "Only mono and stereo streams are supported by now\n");
  617. return -1;
  618. }
  619. s->lossless = get_bits1(&gb);
  620. if (!s->lossless)
  621. skip_bits(&gb, 3); // XXX FIXME
  622. s->mid_side = get_bits1(&gb);
  623. s->downsampling = get_bits(&gb, 2);
  624. s->num_taps = (get_bits(&gb, 5)+1)<<5;
  625. if (get_bits1(&gb)) // XXX FIXME
  626. av_log(avctx, AV_LOG_INFO, "Custom quant table\n");
  627. s->block_align = (int)(2048.0*(s->samplerate/44100))/s->downsampling;
  628. s->frame_size = s->channels*s->block_align*s->downsampling;
  629. // avctx->frame_size = s->block_align;
  630. av_log(avctx, AV_LOG_INFO, "Sonic: ver: %d ls: %d ms: %d taps: %d block: %d frame: %d downsamp: %d\n",
  631. version, s->lossless, s->mid_side, s->num_taps, s->block_align, s->frame_size, s->downsampling);
  632. // generate taps
  633. s->tap_quant = av_mallocz(4* s->num_taps);
  634. for (i = 0; i < s->num_taps; i++)
  635. s->tap_quant[i] = (int)(sqrt(i+1));
  636. s->predictor_k = av_mallocz(4* s->num_taps);
  637. for (i = 0; i < s->channels; i++)
  638. {
  639. s->predictor_state[i] = av_mallocz(4* s->num_taps);
  640. if (!s->predictor_state[i])
  641. return -1;
  642. }
  643. for (i = 0; i < s->channels; i++)
  644. {
  645. s->coded_samples[i] = av_mallocz(4* s->block_align);
  646. if (!s->coded_samples[i])
  647. return -1;
  648. }
  649. s->int_samples = av_mallocz(4* s->frame_size);
  650. return 0;
  651. }
  652. static int sonic_decode_close(AVCodecContext *avctx)
  653. {
  654. SonicContext *s = avctx->priv_data;
  655. int i;
  656. av_free(s->int_samples);
  657. av_free(s->tap_quant);
  658. av_free(s->predictor_k);
  659. for (i = 0; i < s->channels; i++)
  660. {
  661. av_free(s->predictor_state[i]);
  662. av_free(s->coded_samples[i]);
  663. }
  664. return 0;
  665. }
  666. static int sonic_decode_frame(AVCodecContext *avctx,
  667. int16_t *data, int *data_size,
  668. uint8_t *buf, int buf_size)
  669. {
  670. SonicContext *s = avctx->priv_data;
  671. GetBitContext gb;
  672. int i, quant, ch, j;
  673. short *samples = data;
  674. if (buf_size == 0) return 0;
  675. // av_log(NULL, AV_LOG_INFO, "buf_size: %d\n", buf_size);
  676. init_get_bits(&gb, buf, buf_size*8);
  677. intlist_read(&gb, s->predictor_k, s->num_taps, 0);
  678. // dequantize
  679. for (i = 0; i < s->num_taps; i++)
  680. s->predictor_k[i] *= s->tap_quant[i];
  681. if (s->lossless)
  682. quant = 1;
  683. else
  684. quant = get_ue_golomb(&gb) * SAMPLE_FACTOR;
  685. // av_log(NULL, AV_LOG_INFO, "quant: %d\n", quant);
  686. for (ch = 0; ch < s->channels; ch++)
  687. {
  688. int x = ch;
  689. predictor_init_state(s->predictor_k, s->predictor_state[ch], s->num_taps);
  690. intlist_read(&gb, s->coded_samples[ch], s->block_align, 1);
  691. for (i = 0; i < s->block_align; i++)
  692. {
  693. for (j = 0; j < s->downsampling - 1; j++)
  694. {
  695. s->int_samples[x] = predictor_calc_error(s->predictor_k, s->predictor_state[ch], s->num_taps, 0);
  696. x += s->channels;
  697. }
  698. s->int_samples[x] = predictor_calc_error(s->predictor_k, s->predictor_state[ch], s->num_taps, s->coded_samples[ch][i] * quant);
  699. x += s->channels;
  700. }
  701. for (i = 0; i < s->num_taps; i++)
  702. s->predictor_state[ch][i] = s->int_samples[s->frame_size - s->channels + ch - i*s->channels];
  703. }
  704. if (s->mid_side)
  705. for (i = 0; i < s->frame_size; i += s->channels)
  706. {
  707. s->int_samples[i+1] += shift(s->int_samples[i], 1);
  708. s->int_samples[i] -= s->int_samples[i+1];
  709. }
  710. if (!s->lossless)
  711. for (i = 0; i < s->frame_size; i++)
  712. s->int_samples[i] = shift(s->int_samples[i], SAMPLE_SHIFT);
  713. // internal -> short
  714. for (i = 0; i < s->frame_size; i++)
  715. {
  716. if (s->int_samples[i] > 32767)
  717. samples[i] = 32767;
  718. else if (s->int_samples[i] < -32768)
  719. samples[i] = -32768;
  720. else
  721. samples[i] = s->int_samples[i];
  722. }
  723. align_get_bits(&gb);
  724. // if (buf_size != (get_bits_count(&gb)+7)/8)
  725. // av_log(NULL, AV_LOG_INFO, "buf_size (%d) and used bytes (%d) differs\n", buf_size, (get_bits_count(&gb)+7)/8);
  726. *data_size = s->frame_size * 2;
  727. return (get_bits_count(&gb)+7)/8;
  728. }
  729. #ifdef CONFIG_ENCODERS
  730. AVCodec sonic_encoder = {
  731. "sonic",
  732. CODEC_TYPE_AUDIO,
  733. CODEC_ID_SONIC,
  734. sizeof(SonicContext),
  735. sonic_encode_init,
  736. sonic_encode_frame,
  737. sonic_encode_close,
  738. NULL,
  739. };
  740. AVCodec sonic_ls_encoder = {
  741. "sonicls",
  742. CODEC_TYPE_AUDIO,
  743. CODEC_ID_SONIC_LS,
  744. sizeof(SonicContext),
  745. sonic_encode_init,
  746. sonic_encode_frame,
  747. sonic_encode_close,
  748. NULL,
  749. };
  750. #endif
  751. #ifdef CONFIG_DECODERS
  752. AVCodec sonic_decoder = {
  753. "sonic",
  754. CODEC_TYPE_AUDIO,
  755. CODEC_ID_SONIC,
  756. sizeof(SonicContext),
  757. sonic_decode_init,
  758. NULL,
  759. sonic_decode_close,
  760. sonic_decode_frame,
  761. };
  762. #endif