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.

978 lines
25KB

  1. /*
  2. * Simple free lossless/lossy audio codec
  3. * Copyright (c) 2004 Alex Beregszaszi
  4. *
  5. * This file is part of FFmpeg.
  6. *
  7. * FFmpeg is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU Lesser General Public
  9. * License as published by the Free Software Foundation; either
  10. * version 2.1 of the License, or (at your option) any later version.
  11. *
  12. * FFmpeg is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * Lesser General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Lesser General Public
  18. * License along with FFmpeg; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. */
  21. #include "avcodec.h"
  22. #include "get_bits.h"
  23. #include "golomb.h"
  24. /**
  25. * @file
  26. * Simple free lossless/lossy audio codec
  27. * Based on Paul Francis Harrison's Bonk (http://www.logarithmic.net/pfh/bonk)
  28. * Written and designed by Alex Beregszaszi
  29. *
  30. * TODO:
  31. * - CABAC put/get_symbol
  32. * - independent quantizer for channels
  33. * - >2 channels support
  34. * - more decorrelation types
  35. * - more tap_quant tests
  36. * - selectable intlist writers/readers (bonk-style, golomb, cabac)
  37. */
  38. #define MAX_CHANNELS 2
  39. #define MID_SIDE 0
  40. #define LEFT_SIDE 1
  41. #define RIGHT_SIDE 2
  42. typedef struct SonicContext {
  43. int lossless, decorrelation;
  44. int num_taps, downsampling;
  45. double quantization;
  46. int channels, samplerate, block_align, frame_size;
  47. int *tap_quant;
  48. int *int_samples;
  49. int *coded_samples[MAX_CHANNELS];
  50. // for encoding
  51. int *tail;
  52. int tail_size;
  53. int *window;
  54. int window_size;
  55. // for decoding
  56. int *predictor_k;
  57. int *predictor_state[MAX_CHANNELS];
  58. } SonicContext;
  59. #define LATTICE_SHIFT 10
  60. #define SAMPLE_SHIFT 4
  61. #define LATTICE_FACTOR (1 << LATTICE_SHIFT)
  62. #define SAMPLE_FACTOR (1 << SAMPLE_SHIFT)
  63. #define BASE_QUANT 0.6
  64. #define RATE_VARIATION 3.0
  65. static inline int divide(int a, int b)
  66. {
  67. if (a < 0)
  68. return -( (-a + b/2)/b );
  69. else
  70. return (a + b/2)/b;
  71. }
  72. static inline int shift(int a,int b)
  73. {
  74. return (a+(1<<(b-1))) >> b;
  75. }
  76. static inline int shift_down(int a,int b)
  77. {
  78. return (a>>b)+((a<0)?1:0);
  79. }
  80. #if 1
  81. static inline int intlist_write(PutBitContext *pb, int *buf, int entries, int base_2_part)
  82. {
  83. int i;
  84. for (i = 0; i < entries; i++)
  85. set_se_golomb(pb, buf[i]);
  86. return 1;
  87. }
  88. static inline int intlist_read(GetBitContext *gb, int *buf, int entries, int base_2_part)
  89. {
  90. int i;
  91. for (i = 0; i < entries; i++)
  92. buf[i] = get_se_golomb(gb);
  93. return 1;
  94. }
  95. #else
  96. #define ADAPT_LEVEL 8
  97. static int bits_to_store(uint64_t x)
  98. {
  99. int res = 0;
  100. while(x)
  101. {
  102. res++;
  103. x >>= 1;
  104. }
  105. return res;
  106. }
  107. static void write_uint_max(PutBitContext *pb, unsigned int value, unsigned int max)
  108. {
  109. int i, bits;
  110. if (!max)
  111. return;
  112. bits = bits_to_store(max);
  113. for (i = 0; i < bits-1; i++)
  114. put_bits(pb, 1, value & (1 << i));
  115. if ( (value | (1 << (bits-1))) <= max)
  116. put_bits(pb, 1, value & (1 << (bits-1)));
  117. }
  118. static unsigned int read_uint_max(GetBitContext *gb, int max)
  119. {
  120. int i, bits, value = 0;
  121. if (!max)
  122. return 0;
  123. bits = bits_to_store(max);
  124. for (i = 0; i < bits-1; i++)
  125. if (get_bits1(gb))
  126. value += 1 << i;
  127. if ( (value | (1<<(bits-1))) <= max)
  128. if (get_bits1(gb))
  129. value += 1 << (bits-1);
  130. return value;
  131. }
  132. static int intlist_write(PutBitContext *pb, int *buf, int entries, int base_2_part)
  133. {
  134. int i, j, x = 0, low_bits = 0, max = 0;
  135. int step = 256, pos = 0, dominant = 0, any = 0;
  136. int *copy, *bits;
  137. copy = av_mallocz(4* entries);
  138. if (!copy)
  139. return -1;
  140. if (base_2_part)
  141. {
  142. int energy = 0;
  143. for (i = 0; i < entries; i++)
  144. energy += abs(buf[i]);
  145. low_bits = bits_to_store(energy / (entries * 2));
  146. if (low_bits > 15)
  147. low_bits = 15;
  148. put_bits(pb, 4, low_bits);
  149. }
  150. for (i = 0; i < entries; i++)
  151. {
  152. put_bits(pb, low_bits, abs(buf[i]));
  153. copy[i] = abs(buf[i]) >> low_bits;
  154. if (copy[i] > max)
  155. max = abs(copy[i]);
  156. }
  157. bits = av_mallocz(4* entries*max);
  158. if (!bits)
  159. {
  160. // av_free(copy);
  161. return -1;
  162. }
  163. for (i = 0; i <= max; i++)
  164. {
  165. for (j = 0; j < entries; j++)
  166. if (copy[j] >= i)
  167. bits[x++] = copy[j] > i;
  168. }
  169. // store bitstream
  170. while (pos < x)
  171. {
  172. int steplet = step >> 8;
  173. if (pos + steplet > x)
  174. steplet = x - pos;
  175. for (i = 0; i < steplet; i++)
  176. if (bits[i+pos] != dominant)
  177. any = 1;
  178. put_bits(pb, 1, any);
  179. if (!any)
  180. {
  181. pos += steplet;
  182. step += step / ADAPT_LEVEL;
  183. }
  184. else
  185. {
  186. int interloper = 0;
  187. while (((pos + interloper) < x) && (bits[pos + interloper] == dominant))
  188. interloper++;
  189. // note change
  190. write_uint_max(pb, interloper, (step >> 8) - 1);
  191. pos += interloper + 1;
  192. step -= step / ADAPT_LEVEL;
  193. }
  194. if (step < 256)
  195. {
  196. step = 65536 / step;
  197. dominant = !dominant;
  198. }
  199. }
  200. // store signs
  201. for (i = 0; i < entries; i++)
  202. if (buf[i])
  203. put_bits(pb, 1, buf[i] < 0);
  204. // av_free(bits);
  205. // av_free(copy);
  206. return 0;
  207. }
  208. static int intlist_read(GetBitContext *gb, int *buf, int entries, int base_2_part)
  209. {
  210. int i, low_bits = 0, x = 0;
  211. int n_zeros = 0, step = 256, dominant = 0;
  212. int pos = 0, level = 0;
  213. int *bits = av_mallocz(4* entries);
  214. if (!bits)
  215. return -1;
  216. if (base_2_part)
  217. {
  218. low_bits = get_bits(gb, 4);
  219. if (low_bits)
  220. for (i = 0; i < entries; i++)
  221. buf[i] = get_bits(gb, low_bits);
  222. }
  223. // av_log(NULL, AV_LOG_INFO, "entries: %d, low bits: %d\n", entries, low_bits);
  224. while (n_zeros < entries)
  225. {
  226. int steplet = step >> 8;
  227. if (!get_bits1(gb))
  228. {
  229. for (i = 0; i < steplet; i++)
  230. bits[x++] = dominant;
  231. if (!dominant)
  232. n_zeros += steplet;
  233. step += step / ADAPT_LEVEL;
  234. }
  235. else
  236. {
  237. int actual_run = read_uint_max(gb, steplet-1);
  238. // av_log(NULL, AV_LOG_INFO, "actual run: %d\n", actual_run);
  239. for (i = 0; i < actual_run; i++)
  240. bits[x++] = dominant;
  241. bits[x++] = !dominant;
  242. if (!dominant)
  243. n_zeros += actual_run;
  244. else
  245. n_zeros++;
  246. step -= step / ADAPT_LEVEL;
  247. }
  248. if (step < 256)
  249. {
  250. step = 65536 / step;
  251. dominant = !dominant;
  252. }
  253. }
  254. // reconstruct unsigned values
  255. n_zeros = 0;
  256. for (i = 0; n_zeros < entries; i++)
  257. {
  258. while(1)
  259. {
  260. if (pos >= entries)
  261. {
  262. pos = 0;
  263. level += 1 << low_bits;
  264. }
  265. if (buf[pos] >= level)
  266. break;
  267. pos++;
  268. }
  269. if (bits[i])
  270. buf[pos] += 1 << low_bits;
  271. else
  272. n_zeros++;
  273. pos++;
  274. }
  275. // av_free(bits);
  276. // read signs
  277. for (i = 0; i < entries; i++)
  278. if (buf[i] && get_bits1(gb))
  279. buf[i] = -buf[i];
  280. // av_log(NULL, AV_LOG_INFO, "zeros: %d pos: %d\n", n_zeros, pos);
  281. return 0;
  282. }
  283. #endif
  284. static void predictor_init_state(int *k, int *state, int order)
  285. {
  286. int i;
  287. for (i = order-2; i >= 0; i--)
  288. {
  289. int j, p, x = state[i];
  290. for (j = 0, p = i+1; p < order; j++,p++)
  291. {
  292. int tmp = x + shift_down(k[j] * state[p], LATTICE_SHIFT);
  293. state[p] += shift_down(k[j]*x, LATTICE_SHIFT);
  294. x = tmp;
  295. }
  296. }
  297. }
  298. static int predictor_calc_error(int *k, int *state, int order, int error)
  299. {
  300. int i, x = error - shift_down(k[order-1] * state[order-1], LATTICE_SHIFT);
  301. #if 1
  302. int *k_ptr = &(k[order-2]),
  303. *state_ptr = &(state[order-2]);
  304. for (i = order-2; i >= 0; i--, k_ptr--, state_ptr--)
  305. {
  306. int k_value = *k_ptr, state_value = *state_ptr;
  307. x -= shift_down(k_value * state_value, LATTICE_SHIFT);
  308. state_ptr[1] = state_value + shift_down(k_value * x, LATTICE_SHIFT);
  309. }
  310. #else
  311. for (i = order-2; i >= 0; i--)
  312. {
  313. x -= shift_down(k[i] * state[i], LATTICE_SHIFT);
  314. state[i+1] = state[i] + shift_down(k[i] * x, LATTICE_SHIFT);
  315. }
  316. #endif
  317. // don't drift too far, to avoid overflows
  318. if (x > (SAMPLE_FACTOR<<16)) x = (SAMPLE_FACTOR<<16);
  319. if (x < -(SAMPLE_FACTOR<<16)) x = -(SAMPLE_FACTOR<<16);
  320. state[0] = x;
  321. return x;
  322. }
  323. #if CONFIG_SONIC_ENCODER || CONFIG_SONIC_LS_ENCODER
  324. // Heavily modified Levinson-Durbin algorithm which
  325. // copes better with quantization, and calculates the
  326. // actual whitened result as it goes.
  327. static void modified_levinson_durbin(int *window, int window_entries,
  328. int *out, int out_entries, int channels, int *tap_quant)
  329. {
  330. int i;
  331. int *state = av_mallocz(4* window_entries);
  332. memcpy(state, window, 4* window_entries);
  333. for (i = 0; i < out_entries; i++)
  334. {
  335. int step = (i+1)*channels, k, j;
  336. double xx = 0.0, xy = 0.0;
  337. #if 1
  338. int *x_ptr = &(window[step]), *state_ptr = &(state[0]);
  339. j = window_entries - step;
  340. for (;j>=0;j--,x_ptr++,state_ptr++)
  341. {
  342. double x_value = *x_ptr, state_value = *state_ptr;
  343. xx += state_value*state_value;
  344. xy += x_value*state_value;
  345. }
  346. #else
  347. for (j = 0; j <= (window_entries - step); j++);
  348. {
  349. double stepval = window[step+j], stateval = window[j];
  350. // xx += (double)window[j]*(double)window[j];
  351. // xy += (double)window[step+j]*(double)window[j];
  352. xx += stateval*stateval;
  353. xy += stepval*stateval;
  354. }
  355. #endif
  356. if (xx == 0.0)
  357. k = 0;
  358. else
  359. k = (int)(floor(-xy/xx * (double)LATTICE_FACTOR / (double)(tap_quant[i]) + 0.5));
  360. if (k > (LATTICE_FACTOR/tap_quant[i]))
  361. k = LATTICE_FACTOR/tap_quant[i];
  362. if (-k > (LATTICE_FACTOR/tap_quant[i]))
  363. k = -(LATTICE_FACTOR/tap_quant[i]);
  364. out[i] = k;
  365. k *= tap_quant[i];
  366. #if 1
  367. x_ptr = &(window[step]);
  368. state_ptr = &(state[0]);
  369. j = window_entries - step;
  370. for (;j>=0;j--,x_ptr++,state_ptr++)
  371. {
  372. int x_value = *x_ptr, state_value = *state_ptr;
  373. *x_ptr = x_value + shift_down(k*state_value,LATTICE_SHIFT);
  374. *state_ptr = state_value + shift_down(k*x_value, LATTICE_SHIFT);
  375. }
  376. #else
  377. for (j=0; j <= (window_entries - step); j++)
  378. {
  379. int stepval = window[step+j], stateval=state[j];
  380. window[step+j] += shift_down(k * stateval, LATTICE_SHIFT);
  381. state[j] += shift_down(k * stepval, LATTICE_SHIFT);
  382. }
  383. #endif
  384. }
  385. av_free(state);
  386. }
  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 av_cold 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 AVERROR(ENOMEM);
  485. avctx->coded_frame->key_frame = 1;
  486. avctx->frame_size = s->block_align*s->downsampling;
  487. return 0;
  488. }
  489. static av_cold 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_SONIC_ENCODER || CONFIG_SONIC_LS_ENCODER */
  602. #if CONFIG_SONIC_DECODER
  603. static const int samplerate_table[] =
  604. { 44100, 22050, 11025, 96000, 48000, 32000, 24000, 16000, 8000 };
  605. static av_cold 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. avctx->sample_fmt = SAMPLE_FMT_S16;
  668. return 0;
  669. }
  670. static av_cold int sonic_decode_close(AVCodecContext *avctx)
  671. {
  672. SonicContext *s = avctx->priv_data;
  673. int i;
  674. av_free(s->int_samples);
  675. av_free(s->tap_quant);
  676. av_free(s->predictor_k);
  677. for (i = 0; i < s->channels; i++)
  678. {
  679. av_free(s->predictor_state[i]);
  680. av_free(s->coded_samples[i]);
  681. }
  682. return 0;
  683. }
  684. static int sonic_decode_frame(AVCodecContext *avctx,
  685. void *data, int *data_size,
  686. AVPacket *avpkt)
  687. {
  688. const uint8_t *buf = avpkt->data;
  689. int buf_size = avpkt->size;
  690. SonicContext *s = avctx->priv_data;
  691. GetBitContext gb;
  692. int i, quant, ch, j;
  693. short *samples = data;
  694. if (buf_size == 0) return 0;
  695. // av_log(NULL, AV_LOG_INFO, "buf_size: %d\n", buf_size);
  696. init_get_bits(&gb, buf, buf_size*8);
  697. intlist_read(&gb, s->predictor_k, s->num_taps, 0);
  698. // dequantize
  699. for (i = 0; i < s->num_taps; i++)
  700. s->predictor_k[i] *= s->tap_quant[i];
  701. if (s->lossless)
  702. quant = 1;
  703. else
  704. quant = get_ue_golomb(&gb) * SAMPLE_FACTOR;
  705. // av_log(NULL, AV_LOG_INFO, "quant: %d\n", quant);
  706. for (ch = 0; ch < s->channels; ch++)
  707. {
  708. int x = ch;
  709. predictor_init_state(s->predictor_k, s->predictor_state[ch], s->num_taps);
  710. intlist_read(&gb, s->coded_samples[ch], s->block_align, 1);
  711. for (i = 0; i < s->block_align; i++)
  712. {
  713. for (j = 0; j < s->downsampling - 1; j++)
  714. {
  715. s->int_samples[x] = predictor_calc_error(s->predictor_k, s->predictor_state[ch], s->num_taps, 0);
  716. x += s->channels;
  717. }
  718. s->int_samples[x] = predictor_calc_error(s->predictor_k, s->predictor_state[ch], s->num_taps, s->coded_samples[ch][i] * quant);
  719. x += s->channels;
  720. }
  721. for (i = 0; i < s->num_taps; i++)
  722. s->predictor_state[ch][i] = s->int_samples[s->frame_size - s->channels + ch - i*s->channels];
  723. }
  724. switch(s->decorrelation)
  725. {
  726. case MID_SIDE:
  727. for (i = 0; i < s->frame_size; i += s->channels)
  728. {
  729. s->int_samples[i+1] += shift(s->int_samples[i], 1);
  730. s->int_samples[i] -= s->int_samples[i+1];
  731. }
  732. break;
  733. case LEFT_SIDE:
  734. for (i = 0; i < s->frame_size; i += s->channels)
  735. s->int_samples[i+1] += s->int_samples[i];
  736. break;
  737. case RIGHT_SIDE:
  738. for (i = 0; i < s->frame_size; i += s->channels)
  739. s->int_samples[i] += s->int_samples[i+1];
  740. break;
  741. }
  742. if (!s->lossless)
  743. for (i = 0; i < s->frame_size; i++)
  744. s->int_samples[i] = shift(s->int_samples[i], SAMPLE_SHIFT);
  745. // internal -> short
  746. for (i = 0; i < s->frame_size; i++)
  747. samples[i] = av_clip_int16(s->int_samples[i]);
  748. align_get_bits(&gb);
  749. *data_size = s->frame_size * 2;
  750. return (get_bits_count(&gb)+7)/8;
  751. }
  752. AVCodec sonic_decoder = {
  753. "sonic",
  754. AVMEDIA_TYPE_AUDIO,
  755. CODEC_ID_SONIC,
  756. sizeof(SonicContext),
  757. sonic_decode_init,
  758. NULL,
  759. sonic_decode_close,
  760. sonic_decode_frame,
  761. .long_name = NULL_IF_CONFIG_SMALL("Sonic"),
  762. };
  763. #endif /* CONFIG_SONIC_DECODER */
  764. #if CONFIG_SONIC_ENCODER
  765. AVCodec sonic_encoder = {
  766. "sonic",
  767. AVMEDIA_TYPE_AUDIO,
  768. CODEC_ID_SONIC,
  769. sizeof(SonicContext),
  770. sonic_encode_init,
  771. sonic_encode_frame,
  772. sonic_encode_close,
  773. NULL,
  774. .long_name = NULL_IF_CONFIG_SMALL("Sonic"),
  775. };
  776. #endif
  777. #if CONFIG_SONIC_LS_ENCODER
  778. AVCodec sonic_ls_encoder = {
  779. "sonicls",
  780. AVMEDIA_TYPE_AUDIO,
  781. CODEC_ID_SONIC_LS,
  782. sizeof(SonicContext),
  783. sonic_encode_init,
  784. sonic_encode_frame,
  785. sonic_encode_close,
  786. NULL,
  787. .long_name = NULL_IF_CONFIG_SMALL("Sonic lossless"),
  788. };
  789. #endif