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.

995 lines
26KB

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