FFmpeg
sonic.c
Go to the documentation of this file.
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 #include "internal.h"
25 #include "rangecoder.h"
26 
27 
28 /**
29  * @file
30  * Simple free lossless/lossy audio codec
31  * Based on Paul Francis Harrison's Bonk (http://www.logarithmic.net/pfh/bonk)
32  * Written and designed by Alex Beregszaszi
33  *
34  * TODO:
35  * - CABAC put/get_symbol
36  * - independent quantizer for channels
37  * - >2 channels support
38  * - more decorrelation types
39  * - more tap_quant tests
40  * - selectable intlist writers/readers (bonk-style, golomb, cabac)
41  */
42 
43 #define MAX_CHANNELS 2
44 
45 #define MID_SIDE 0
46 #define LEFT_SIDE 1
47 #define RIGHT_SIDE 2
48 
49 typedef struct SonicContext {
50  int version;
53 
55  double quantization;
56 
58 
59  int *tap_quant;
62 
63  // for encoding
64  int *tail;
65  int tail_size;
66  int *window;
68 
69  // for decoding
72 } SonicContext;
73 
74 #define LATTICE_SHIFT 10
75 #define SAMPLE_SHIFT 4
76 #define LATTICE_FACTOR (1 << LATTICE_SHIFT)
77 #define SAMPLE_FACTOR (1 << SAMPLE_SHIFT)
78 
79 #define BASE_QUANT 0.6
80 #define RATE_VARIATION 3.0
81 
82 static inline int shift(int a,int b)
83 {
84  return (a+(1<<(b-1))) >> b;
85 }
86 
87 static inline int shift_down(int a,int b)
88 {
89  return (a>>b)+(a<0);
90 }
91 
92 static av_always_inline av_flatten void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed, uint64_t rc_stat[256][2], uint64_t rc_stat2[32][2]){
93  int i;
94 
95 #define put_rac(C,S,B) \
96 do{\
97  if(rc_stat){\
98  rc_stat[*(S)][B]++;\
99  rc_stat2[(S)-state][B]++;\
100  }\
101  put_rac(C,S,B);\
102 }while(0)
103 
104  if(v){
105  const int a= FFABS(v);
106  const int e= av_log2(a);
107  put_rac(c, state+0, 0);
108  if(e<=9){
109  for(i=0; i<e; i++){
110  put_rac(c, state+1+i, 1); //1..10
111  }
112  put_rac(c, state+1+i, 0);
113 
114  for(i=e-1; i>=0; i--){
115  put_rac(c, state+22+i, (a>>i)&1); //22..31
116  }
117 
118  if(is_signed)
119  put_rac(c, state+11 + e, v < 0); //11..21
120  }else{
121  for(i=0; i<e; i++){
122  put_rac(c, state+1+FFMIN(i,9), 1); //1..10
123  }
124  put_rac(c, state+1+9, 0);
125 
126  for(i=e-1; i>=0; i--){
127  put_rac(c, state+22+FFMIN(i,9), (a>>i)&1); //22..31
128  }
129 
130  if(is_signed)
131  put_rac(c, state+11 + 10, v < 0); //11..21
132  }
133  }else{
134  put_rac(c, state+0, 1);
135  }
136 #undef put_rac
137 }
138 
139 static inline av_flatten int get_symbol(RangeCoder *c, uint8_t *state, int is_signed){
140  if(get_rac(c, state+0))
141  return 0;
142  else{
143  int i, e;
144  unsigned a;
145  e= 0;
146  while(get_rac(c, state+1 + FFMIN(e,9))){ //1..10
147  e++;
148  if (e > 31)
149  return AVERROR_INVALIDDATA;
150  }
151 
152  a= 1;
153  for(i=e-1; i>=0; i--){
154  a += a + get_rac(c, state+22 + FFMIN(i,9)); //22..31
155  }
156 
157  e= -(is_signed && get_rac(c, state+11 + FFMIN(e, 10))); //11..21
158  return (a^e)-e;
159  }
160 }
161 
162 #if 1
163 static inline int intlist_write(RangeCoder *c, uint8_t *state, int *buf, int entries, int base_2_part)
164 {
165  int i;
166 
167  for (i = 0; i < entries; i++)
168  put_symbol(c, state, buf[i], 1, NULL, NULL);
169 
170  return 1;
171 }
172 
173 static inline int intlist_read(RangeCoder *c, uint8_t *state, int *buf, int entries, int base_2_part)
174 {
175  int i;
176 
177  for (i = 0; i < entries; i++)
178  buf[i] = get_symbol(c, state, 1);
179 
180  return 1;
181 }
182 #elif 1
183 static inline int intlist_write(PutBitContext *pb, int *buf, int entries, int base_2_part)
184 {
185  int i;
186 
187  for (i = 0; i < entries; i++)
188  set_se_golomb(pb, buf[i]);
189 
190  return 1;
191 }
192 
193 static inline int intlist_read(GetBitContext *gb, int *buf, int entries, int base_2_part)
194 {
195  int i;
196 
197  for (i = 0; i < entries; i++)
198  buf[i] = get_se_golomb(gb);
199 
200  return 1;
201 }
202 
203 #else
204 
205 #define ADAPT_LEVEL 8
206 
207 static int bits_to_store(uint64_t x)
208 {
209  int res = 0;
210 
211  while(x)
212  {
213  res++;
214  x >>= 1;
215  }
216  return res;
217 }
218 
219 static void write_uint_max(PutBitContext *pb, unsigned int value, unsigned int max)
220 {
221  int i, bits;
222 
223  if (!max)
224  return;
225 
226  bits = bits_to_store(max);
227 
228  for (i = 0; i < bits-1; i++)
229  put_bits(pb, 1, value & (1 << i));
230 
231  if ( (value | (1 << (bits-1))) <= max)
232  put_bits(pb, 1, value & (1 << (bits-1)));
233 }
234 
235 static unsigned int read_uint_max(GetBitContext *gb, int max)
236 {
237  int i, bits, value = 0;
238 
239  if (!max)
240  return 0;
241 
242  bits = bits_to_store(max);
243 
244  for (i = 0; i < bits-1; i++)
245  if (get_bits1(gb))
246  value += 1 << i;
247 
248  if ( (value | (1<<(bits-1))) <= max)
249  if (get_bits1(gb))
250  value += 1 << (bits-1);
251 
252  return value;
253 }
254 
255 static int intlist_write(PutBitContext *pb, int *buf, int entries, int base_2_part)
256 {
257  int i, j, x = 0, low_bits = 0, max = 0;
258  int step = 256, pos = 0, dominant = 0, any = 0;
259  int *copy, *bits;
260 
261  copy = av_calloc(entries, sizeof(*copy));
262  if (!copy)
263  return AVERROR(ENOMEM);
264 
265  if (base_2_part)
266  {
267  int energy = 0;
268 
269  for (i = 0; i < entries; i++)
270  energy += abs(buf[i]);
271 
272  low_bits = bits_to_store(energy / (entries * 2));
273  if (low_bits > 15)
274  low_bits = 15;
275 
276  put_bits(pb, 4, low_bits);
277  }
278 
279  for (i = 0; i < entries; i++)
280  {
281  put_bits(pb, low_bits, abs(buf[i]));
282  copy[i] = abs(buf[i]) >> low_bits;
283  if (copy[i] > max)
284  max = abs(copy[i]);
285  }
286 
287  bits = av_calloc(entries*max, sizeof(*bits));
288  if (!bits)
289  {
290  av_free(copy);
291  return AVERROR(ENOMEM);
292  }
293 
294  for (i = 0; i <= max; i++)
295  {
296  for (j = 0; j < entries; j++)
297  if (copy[j] >= i)
298  bits[x++] = copy[j] > i;
299  }
300 
301  // store bitstream
302  while (pos < x)
303  {
304  int steplet = step >> 8;
305 
306  if (pos + steplet > x)
307  steplet = x - pos;
308 
309  for (i = 0; i < steplet; i++)
310  if (bits[i+pos] != dominant)
311  any = 1;
312 
313  put_bits(pb, 1, any);
314 
315  if (!any)
316  {
317  pos += steplet;
318  step += step / ADAPT_LEVEL;
319  }
320  else
321  {
322  int interloper = 0;
323 
324  while (((pos + interloper) < x) && (bits[pos + interloper] == dominant))
325  interloper++;
326 
327  // note change
328  write_uint_max(pb, interloper, (step >> 8) - 1);
329 
330  pos += interloper + 1;
331  step -= step / ADAPT_LEVEL;
332  }
333 
334  if (step < 256)
335  {
336  step = 65536 / step;
337  dominant = !dominant;
338  }
339  }
340 
341  // store signs
342  for (i = 0; i < entries; i++)
343  if (buf[i])
344  put_bits(pb, 1, buf[i] < 0);
345 
346  av_free(bits);
347  av_free(copy);
348 
349  return 0;
350 }
351 
352 static int intlist_read(GetBitContext *gb, int *buf, int entries, int base_2_part)
353 {
354  int i, low_bits = 0, x = 0;
355  int n_zeros = 0, step = 256, dominant = 0;
356  int pos = 0, level = 0;
357  int *bits = av_calloc(entries, sizeof(*bits));
358 
359  if (!bits)
360  return AVERROR(ENOMEM);
361 
362  if (base_2_part)
363  {
364  low_bits = get_bits(gb, 4);
365 
366  if (low_bits)
367  for (i = 0; i < entries; i++)
368  buf[i] = get_bits(gb, low_bits);
369  }
370 
371 // av_log(NULL, AV_LOG_INFO, "entries: %d, low bits: %d\n", entries, low_bits);
372 
373  while (n_zeros < entries)
374  {
375  int steplet = step >> 8;
376 
377  if (!get_bits1(gb))
378  {
379  for (i = 0; i < steplet; i++)
380  bits[x++] = dominant;
381 
382  if (!dominant)
383  n_zeros += steplet;
384 
385  step += step / ADAPT_LEVEL;
386  }
387  else
388  {
389  int actual_run = read_uint_max(gb, steplet-1);
390 
391 // av_log(NULL, AV_LOG_INFO, "actual run: %d\n", actual_run);
392 
393  for (i = 0; i < actual_run; i++)
394  bits[x++] = dominant;
395 
396  bits[x++] = !dominant;
397 
398  if (!dominant)
399  n_zeros += actual_run;
400  else
401  n_zeros++;
402 
403  step -= step / ADAPT_LEVEL;
404  }
405 
406  if (step < 256)
407  {
408  step = 65536 / step;
409  dominant = !dominant;
410  }
411  }
412 
413  // reconstruct unsigned values
414  n_zeros = 0;
415  for (i = 0; n_zeros < entries; i++)
416  {
417  while(1)
418  {
419  if (pos >= entries)
420  {
421  pos = 0;
422  level += 1 << low_bits;
423  }
424 
425  if (buf[pos] >= level)
426  break;
427 
428  pos++;
429  }
430 
431  if (bits[i])
432  buf[pos] += 1 << low_bits;
433  else
434  n_zeros++;
435 
436  pos++;
437  }
438  av_free(bits);
439 
440  // read signs
441  for (i = 0; i < entries; i++)
442  if (buf[i] && get_bits1(gb))
443  buf[i] = -buf[i];
444 
445 // av_log(NULL, AV_LOG_INFO, "zeros: %d pos: %d\n", n_zeros, pos);
446 
447  return 0;
448 }
449 #endif
450 
451 static void predictor_init_state(int *k, int *state, int order)
452 {
453  int i;
454 
455  for (i = order-2; i >= 0; i--)
456  {
457  int j, p, x = state[i];
458 
459  for (j = 0, p = i+1; p < order; j++,p++)
460  {
461  int tmp = x + shift_down(k[j] * (unsigned)state[p], LATTICE_SHIFT);
462  state[p] += shift_down(k[j]* (unsigned)x, LATTICE_SHIFT);
463  x = tmp;
464  }
465  }
466 }
467 
468 static int predictor_calc_error(int *k, int *state, int order, int error)
469 {
470  int i, x = error - shift_down(k[order-1] * (unsigned)state[order-1], LATTICE_SHIFT);
471 
472 #if 1
473  int *k_ptr = &(k[order-2]),
474  *state_ptr = &(state[order-2]);
475  for (i = order-2; i >= 0; i--, k_ptr--, state_ptr--)
476  {
477  int k_value = *k_ptr, state_value = *state_ptr;
478  x -= shift_down(k_value * (unsigned)state_value, LATTICE_SHIFT);
479  state_ptr[1] = state_value + shift_down(k_value * (unsigned)x, LATTICE_SHIFT);
480  }
481 #else
482  for (i = order-2; i >= 0; i--)
483  {
484  x -= shift_down(k[i] * state[i], LATTICE_SHIFT);
485  state[i+1] = state[i] + shift_down(k[i] * x, LATTICE_SHIFT);
486  }
487 #endif
488 
489  // don't drift too far, to avoid overflows
490  if (x > (SAMPLE_FACTOR<<16)) x = (SAMPLE_FACTOR<<16);
491  if (x < -(SAMPLE_FACTOR<<16)) x = -(SAMPLE_FACTOR<<16);
492 
493  state[0] = x;
494 
495  return x;
496 }
497 
498 #if CONFIG_SONIC_ENCODER || CONFIG_SONIC_LS_ENCODER
499 // Heavily modified Levinson-Durbin algorithm which
500 // copes better with quantization, and calculates the
501 // actual whitened result as it goes.
502 
503 static int modified_levinson_durbin(int *window, int window_entries,
504  int *out, int out_entries, int channels, int *tap_quant)
505 {
506  int i;
507  int *state = av_calloc(window_entries, sizeof(*state));
508 
509  if (!state)
510  return AVERROR(ENOMEM);
511 
512  memcpy(state, window, 4* window_entries);
513 
514  for (i = 0; i < out_entries; i++)
515  {
516  int step = (i+1)*channels, k, j;
517  double xx = 0.0, xy = 0.0;
518 #if 1
519  int *x_ptr = &(window[step]);
520  int *state_ptr = &(state[0]);
521  j = window_entries - step;
522  for (;j>0;j--,x_ptr++,state_ptr++)
523  {
524  double x_value = *x_ptr;
525  double state_value = *state_ptr;
526  xx += state_value*state_value;
527  xy += x_value*state_value;
528  }
529 #else
530  for (j = 0; j <= (window_entries - step); j++);
531  {
532  double stepval = window[step+j];
533  double stateval = window[j];
534 // xx += (double)window[j]*(double)window[j];
535 // xy += (double)window[step+j]*(double)window[j];
536  xx += stateval*stateval;
537  xy += stepval*stateval;
538  }
539 #endif
540  if (xx == 0.0)
541  k = 0;
542  else
543  k = (int)(floor(-xy/xx * (double)LATTICE_FACTOR / (double)(tap_quant[i]) + 0.5));
544 
545  if (k > (LATTICE_FACTOR/tap_quant[i]))
546  k = LATTICE_FACTOR/tap_quant[i];
547  if (-k > (LATTICE_FACTOR/tap_quant[i]))
548  k = -(LATTICE_FACTOR/tap_quant[i]);
549 
550  out[i] = k;
551  k *= tap_quant[i];
552 
553 #if 1
554  x_ptr = &(window[step]);
555  state_ptr = &(state[0]);
556  j = window_entries - step;
557  for (;j>0;j--,x_ptr++,state_ptr++)
558  {
559  int x_value = *x_ptr;
560  int state_value = *state_ptr;
561  *x_ptr = x_value + shift_down(k*state_value,LATTICE_SHIFT);
562  *state_ptr = state_value + shift_down(k*x_value, LATTICE_SHIFT);
563  }
564 #else
565  for (j=0; j <= (window_entries - step); j++)
566  {
567  int stepval = window[step+j];
568  int stateval=state[j];
569  window[step+j] += shift_down(k * stateval, LATTICE_SHIFT);
570  state[j] += shift_down(k * stepval, LATTICE_SHIFT);
571  }
572 #endif
573  }
574 
575  av_free(state);
576  return 0;
577 }
578 
579 static inline int code_samplerate(int samplerate)
580 {
581  switch (samplerate)
582  {
583  case 44100: return 0;
584  case 22050: return 1;
585  case 11025: return 2;
586  case 96000: return 3;
587  case 48000: return 4;
588  case 32000: return 5;
589  case 24000: return 6;
590  case 16000: return 7;
591  case 8000: return 8;
592  }
593  return AVERROR(EINVAL);
594 }
595 
596 static av_cold int sonic_encode_init(AVCodecContext *avctx)
597 {
598  SonicContext *s = avctx->priv_data;
599  PutBitContext pb;
600  int i;
601 
602  s->version = 2;
603 
604  if (avctx->channels > MAX_CHANNELS)
605  {
606  av_log(avctx, AV_LOG_ERROR, "Only mono and stereo streams are supported by now\n");
607  return AVERROR(EINVAL); /* only stereo or mono for now */
608  }
609 
610  if (avctx->channels == 2)
611  s->decorrelation = MID_SIDE;
612  else
613  s->decorrelation = 3;
614 
615  if (avctx->codec->id == AV_CODEC_ID_SONIC_LS)
616  {
617  s->lossless = 1;
618  s->num_taps = 32;
619  s->downsampling = 1;
620  s->quantization = 0.0;
621  }
622  else
623  {
624  s->num_taps = 128;
625  s->downsampling = 2;
626  s->quantization = 1.0;
627  }
628 
629  // max tap 2048
630  if (s->num_taps < 32 || s->num_taps > 1024 || s->num_taps % 32) {
631  av_log(avctx, AV_LOG_ERROR, "Invalid number of taps\n");
632  return AVERROR_INVALIDDATA;
633  }
634 
635  // generate taps
636  s->tap_quant = av_calloc(s->num_taps, sizeof(*s->tap_quant));
637  if (!s->tap_quant)
638  return AVERROR(ENOMEM);
639 
640  for (i = 0; i < s->num_taps; i++)
641  s->tap_quant[i] = ff_sqrt(i+1);
642 
643  s->channels = avctx->channels;
644  s->samplerate = avctx->sample_rate;
645 
646  s->block_align = 2048LL*s->samplerate/(44100*s->downsampling);
648 
649  s->tail_size = s->num_taps*s->channels;
650  s->tail = av_calloc(s->tail_size, sizeof(*s->tail));
651  if (!s->tail)
652  return AVERROR(ENOMEM);
653 
654  s->predictor_k = av_calloc(s->num_taps, sizeof(*s->predictor_k) );
655  if (!s->predictor_k)
656  return AVERROR(ENOMEM);
657 
658  for (i = 0; i < s->channels; i++)
659  {
660  s->coded_samples[i] = av_calloc(s->block_align, sizeof(**s->coded_samples));
661  if (!s->coded_samples[i])
662  return AVERROR(ENOMEM);
663  }
664 
665  s->int_samples = av_calloc(s->frame_size, sizeof(*s->int_samples));
666 
667  s->window_size = ((2*s->tail_size)+s->frame_size);
668  s->window = av_calloc(s->window_size, sizeof(*s->window));
669  if (!s->window || !s->int_samples)
670  return AVERROR(ENOMEM);
671 
672  avctx->extradata = av_mallocz(16);
673  if (!avctx->extradata)
674  return AVERROR(ENOMEM);
675  init_put_bits(&pb, avctx->extradata, 16*8);
676 
677  put_bits(&pb, 2, s->version); // version
678  if (s->version >= 1)
679  {
680  if (s->version >= 2) {
681  put_bits(&pb, 8, s->version);
682  put_bits(&pb, 8, s->minor_version);
683  }
684  put_bits(&pb, 2, s->channels);
685  put_bits(&pb, 4, code_samplerate(s->samplerate));
686  }
687  put_bits(&pb, 1, s->lossless);
688  if (!s->lossless)
689  put_bits(&pb, 3, SAMPLE_SHIFT); // XXX FIXME: sample precision
690  put_bits(&pb, 2, s->decorrelation);
691  put_bits(&pb, 2, s->downsampling);
692  put_bits(&pb, 5, (s->num_taps >> 5)-1); // 32..1024
693  put_bits(&pb, 1, 0); // XXX FIXME: no custom tap quant table
694 
695  flush_put_bits(&pb);
696  avctx->extradata_size = put_bits_count(&pb)/8;
697 
698  av_log(avctx, AV_LOG_INFO, "Sonic: ver: %d.%d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
700 
701  avctx->frame_size = s->block_align*s->downsampling;
702 
703  return 0;
704 }
705 
706 static av_cold int sonic_encode_close(AVCodecContext *avctx)
707 {
708  SonicContext *s = avctx->priv_data;
709  int i;
710 
711  for (i = 0; i < s->channels; i++)
712  av_freep(&s->coded_samples[i]);
713 
714  av_freep(&s->predictor_k);
715  av_freep(&s->tail);
716  av_freep(&s->tap_quant);
717  av_freep(&s->window);
718  av_freep(&s->int_samples);
719 
720  return 0;
721 }
722 
723 static int sonic_encode_frame(AVCodecContext *avctx, AVPacket *avpkt,
724  const AVFrame *frame, int *got_packet_ptr)
725 {
726  SonicContext *s = avctx->priv_data;
727  RangeCoder c;
728  int i, j, ch, quant = 0, x = 0;
729  int ret;
730  const short *samples = (const int16_t*)frame->data[0];
731  uint8_t state[32];
732 
733  if ((ret = ff_alloc_packet2(avctx, avpkt, s->frame_size * 5 + 1000, 0)) < 0)
734  return ret;
735 
736  ff_init_range_encoder(&c, avpkt->data, avpkt->size);
737  ff_build_rac_states(&c, 0.05*(1LL<<32), 256-8);
738  memset(state, 128, sizeof(state));
739 
740  // short -> internal
741  for (i = 0; i < s->frame_size; i++)
742  s->int_samples[i] = samples[i];
743 
744  if (!s->lossless)
745  for (i = 0; i < s->frame_size; i++)
746  s->int_samples[i] = s->int_samples[i] << SAMPLE_SHIFT;
747 
748  switch(s->decorrelation)
749  {
750  case MID_SIDE:
751  for (i = 0; i < s->frame_size; i += s->channels)
752  {
753  s->int_samples[i] += s->int_samples[i+1];
754  s->int_samples[i+1] -= shift(s->int_samples[i], 1);
755  }
756  break;
757  case LEFT_SIDE:
758  for (i = 0; i < s->frame_size; i += s->channels)
759  s->int_samples[i+1] -= s->int_samples[i];
760  break;
761  case RIGHT_SIDE:
762  for (i = 0; i < s->frame_size; i += s->channels)
763  s->int_samples[i] -= s->int_samples[i+1];
764  break;
765  }
766 
767  memset(s->window, 0, 4* s->window_size);
768 
769  for (i = 0; i < s->tail_size; i++)
770  s->window[x++] = s->tail[i];
771 
772  for (i = 0; i < s->frame_size; i++)
773  s->window[x++] = s->int_samples[i];
774 
775  for (i = 0; i < s->tail_size; i++)
776  s->window[x++] = 0;
777 
778  for (i = 0; i < s->tail_size; i++)
779  s->tail[i] = s->int_samples[s->frame_size - s->tail_size + i];
780 
781  // generate taps
782  ret = modified_levinson_durbin(s->window, s->window_size,
783  s->predictor_k, s->num_taps, s->channels, s->tap_quant);
784  if (ret < 0)
785  return ret;
786 
787  if ((ret = intlist_write(&c, state, s->predictor_k, s->num_taps, 0)) < 0)
788  return ret;
789 
790  for (ch = 0; ch < s->channels; ch++)
791  {
792  x = s->tail_size+ch;
793  for (i = 0; i < s->block_align; i++)
794  {
795  int sum = 0;
796  for (j = 0; j < s->downsampling; j++, x += s->channels)
797  sum += s->window[x];
798  s->coded_samples[ch][i] = sum;
799  }
800  }
801 
802  // simple rate control code
803  if (!s->lossless)
804  {
805  double energy1 = 0.0, energy2 = 0.0;
806  for (ch = 0; ch < s->channels; ch++)
807  {
808  for (i = 0; i < s->block_align; i++)
809  {
810  double sample = s->coded_samples[ch][i];
811  energy2 += sample*sample;
812  energy1 += fabs(sample);
813  }
814  }
815 
816  energy2 = sqrt(energy2/(s->channels*s->block_align));
817  energy1 = M_SQRT2*energy1/(s->channels*s->block_align);
818 
819  // increase bitrate when samples are like a gaussian distribution
820  // reduce bitrate when samples are like a two-tailed exponential distribution
821 
822  if (energy2 > energy1)
823  energy2 += (energy2-energy1)*RATE_VARIATION;
824 
825  quant = (int)(BASE_QUANT*s->quantization*energy2/SAMPLE_FACTOR);
826 // av_log(avctx, AV_LOG_DEBUG, "quant: %d energy: %f / %f\n", quant, energy1, energy2);
827 
828  quant = av_clip(quant, 1, 65534);
829 
830  put_symbol(&c, state, quant, 0, NULL, NULL);
831 
832  quant *= SAMPLE_FACTOR;
833  }
834 
835  // write out coded samples
836  for (ch = 0; ch < s->channels; ch++)
837  {
838  if (!s->lossless)
839  for (i = 0; i < s->block_align; i++)
840  s->coded_samples[ch][i] = ROUNDED_DIV(s->coded_samples[ch][i], quant);
841 
842  if ((ret = intlist_write(&c, state, s->coded_samples[ch], s->block_align, 1)) < 0)
843  return ret;
844  }
845 
846 // av_log(avctx, AV_LOG_DEBUG, "used bytes: %d\n", (put_bits_count(&pb)+7)/8);
847 
848  avpkt->size = ff_rac_terminate(&c, 0);
849  *got_packet_ptr = 1;
850  return 0;
851 
852 }
853 #endif /* CONFIG_SONIC_ENCODER || CONFIG_SONIC_LS_ENCODER */
854 
855 #if CONFIG_SONIC_DECODER
856 static const int samplerate_table[] =
857  { 44100, 22050, 11025, 96000, 48000, 32000, 24000, 16000, 8000 };
858 
859 static av_cold int sonic_decode_init(AVCodecContext *avctx)
860 {
861  SonicContext *s = avctx->priv_data;
862  GetBitContext gb;
863  int i;
864  int ret;
865 
866  s->channels = avctx->channels;
867  s->samplerate = avctx->sample_rate;
868 
869  if (!avctx->extradata)
870  {
871  av_log(avctx, AV_LOG_ERROR, "No mandatory headers present\n");
872  return AVERROR_INVALIDDATA;
873  }
874 
875  ret = init_get_bits8(&gb, avctx->extradata, avctx->extradata_size);
876  if (ret < 0)
877  return ret;
878 
879  s->version = get_bits(&gb, 2);
880  if (s->version >= 2) {
881  s->version = get_bits(&gb, 8);
882  s->minor_version = get_bits(&gb, 8);
883  }
884  if (s->version != 2)
885  {
886  av_log(avctx, AV_LOG_ERROR, "Unsupported Sonic version, please report\n");
887  return AVERROR_INVALIDDATA;
888  }
889 
890  if (s->version >= 1)
891  {
892  int sample_rate_index;
893  s->channels = get_bits(&gb, 2);
894  sample_rate_index = get_bits(&gb, 4);
895  if (sample_rate_index >= FF_ARRAY_ELEMS(samplerate_table)) {
896  av_log(avctx, AV_LOG_ERROR, "Invalid sample_rate_index %d\n", sample_rate_index);
897  return AVERROR_INVALIDDATA;
898  }
899  s->samplerate = samplerate_table[sample_rate_index];
900  av_log(avctx, AV_LOG_INFO, "Sonicv2 chans: %d samprate: %d\n",
901  s->channels, s->samplerate);
902  }
903 
904  if (s->channels > MAX_CHANNELS || s->channels < 1)
905  {
906  av_log(avctx, AV_LOG_ERROR, "Only mono and stereo streams are supported by now\n");
907  return AVERROR_INVALIDDATA;
908  }
909  avctx->channels = s->channels;
910 
911  s->lossless = get_bits1(&gb);
912  if (!s->lossless)
913  skip_bits(&gb, 3); // XXX FIXME
914  s->decorrelation = get_bits(&gb, 2);
915  if (s->decorrelation != 3 && s->channels != 2) {
916  av_log(avctx, AV_LOG_ERROR, "invalid decorrelation %d\n", s->decorrelation);
917  return AVERROR_INVALIDDATA;
918  }
919 
920  s->downsampling = get_bits(&gb, 2);
921  if (!s->downsampling) {
922  av_log(avctx, AV_LOG_ERROR, "invalid downsampling value\n");
923  return AVERROR_INVALIDDATA;
924  }
925 
926  s->num_taps = (get_bits(&gb, 5)+1)<<5;
927  if (get_bits1(&gb)) // XXX FIXME
928  av_log(avctx, AV_LOG_INFO, "Custom quant table\n");
929 
930  s->block_align = 2048LL*s->samplerate/(44100*s->downsampling);
932 // avctx->frame_size = s->block_align;
933 
934  if (s->num_taps * s->channels > s->frame_size) {
935  av_log(avctx, AV_LOG_ERROR,
936  "number of taps times channels (%d * %d) larger than frame size %d\n",
937  s->num_taps, s->channels, s->frame_size);
938  return AVERROR_INVALIDDATA;
939  }
940 
941  av_log(avctx, AV_LOG_INFO, "Sonic: ver: %d.%d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
943 
944  // generate taps
945  s->tap_quant = av_calloc(s->num_taps, sizeof(*s->tap_quant));
946  if (!s->tap_quant)
947  return AVERROR(ENOMEM);
948 
949  for (i = 0; i < s->num_taps; i++)
950  s->tap_quant[i] = ff_sqrt(i+1);
951 
952  s->predictor_k = av_calloc(s->num_taps, sizeof(*s->predictor_k));
953 
954  for (i = 0; i < s->channels; i++)
955  {
956  s->predictor_state[i] = av_calloc(s->num_taps, sizeof(**s->predictor_state));
957  if (!s->predictor_state[i])
958  return AVERROR(ENOMEM);
959  }
960 
961  for (i = 0; i < s->channels; i++)
962  {
963  s->coded_samples[i] = av_calloc(s->block_align, sizeof(**s->coded_samples));
964  if (!s->coded_samples[i])
965  return AVERROR(ENOMEM);
966  }
967  s->int_samples = av_calloc(s->frame_size, sizeof(*s->int_samples));
968  if (!s->int_samples)
969  return AVERROR(ENOMEM);
970 
971  avctx->sample_fmt = AV_SAMPLE_FMT_S16;
972  return 0;
973 }
974 
975 static av_cold int sonic_decode_close(AVCodecContext *avctx)
976 {
977  SonicContext *s = avctx->priv_data;
978  int i;
979 
980  av_freep(&s->int_samples);
981  av_freep(&s->tap_quant);
982  av_freep(&s->predictor_k);
983 
984  for (i = 0; i < s->channels; i++)
985  {
986  av_freep(&s->predictor_state[i]);
987  av_freep(&s->coded_samples[i]);
988  }
989 
990  return 0;
991 }
992 
993 static int sonic_decode_frame(AVCodecContext *avctx,
994  void *data, int *got_frame_ptr,
995  AVPacket *avpkt)
996 {
997  const uint8_t *buf = avpkt->data;
998  int buf_size = avpkt->size;
999  SonicContext *s = avctx->priv_data;
1000  RangeCoder c;
1001  uint8_t state[32];
1002  int i, quant, ch, j, ret;
1003  int16_t *samples;
1004  AVFrame *frame = data;
1005 
1006  if (buf_size == 0) return 0;
1007 
1008  frame->nb_samples = s->frame_size / avctx->channels;
1009  if ((ret = ff_get_buffer(avctx, frame, 0)) < 0)
1010  return ret;
1011  samples = (int16_t *)frame->data[0];
1012 
1013 // av_log(NULL, AV_LOG_INFO, "buf_size: %d\n", buf_size);
1014 
1015  memset(state, 128, sizeof(state));
1016  ff_init_range_decoder(&c, buf, buf_size);
1017  ff_build_rac_states(&c, 0.05*(1LL<<32), 256-8);
1018 
1019  intlist_read(&c, state, s->predictor_k, s->num_taps, 0);
1020 
1021  // dequantize
1022  for (i = 0; i < s->num_taps; i++)
1023  s->predictor_k[i] *= s->tap_quant[i];
1024 
1025  if (s->lossless)
1026  quant = 1;
1027  else
1028  quant = get_symbol(&c, state, 0) * SAMPLE_FACTOR;
1029 
1030 // av_log(NULL, AV_LOG_INFO, "quant: %d\n", quant);
1031 
1032  for (ch = 0; ch < s->channels; ch++)
1033  {
1034  int x = ch;
1035 
1037 
1038  intlist_read(&c, state, s->coded_samples[ch], s->block_align, 1);
1039 
1040  for (i = 0; i < s->block_align; i++)
1041  {
1042  for (j = 0; j < s->downsampling - 1; j++)
1043  {
1045  x += s->channels;
1046  }
1047 
1048  s->int_samples[x] = predictor_calc_error(s->predictor_k, s->predictor_state[ch], s->num_taps, s->coded_samples[ch][i] * (unsigned)quant);
1049  x += s->channels;
1050  }
1051 
1052  for (i = 0; i < s->num_taps; i++)
1053  s->predictor_state[ch][i] = s->int_samples[s->frame_size - s->channels + ch - i*s->channels];
1054  }
1055 
1056  switch(s->decorrelation)
1057  {
1058  case MID_SIDE:
1059  for (i = 0; i < s->frame_size; i += s->channels)
1060  {
1061  s->int_samples[i+1] += shift(s->int_samples[i], 1);
1062  s->int_samples[i] -= s->int_samples[i+1];
1063  }
1064  break;
1065  case LEFT_SIDE:
1066  for (i = 0; i < s->frame_size; i += s->channels)
1067  s->int_samples[i+1] += s->int_samples[i];
1068  break;
1069  case RIGHT_SIDE:
1070  for (i = 0; i < s->frame_size; i += s->channels)
1071  s->int_samples[i] += s->int_samples[i+1];
1072  break;
1073  }
1074 
1075  if (!s->lossless)
1076  for (i = 0; i < s->frame_size; i++)
1077  s->int_samples[i] = shift(s->int_samples[i], SAMPLE_SHIFT);
1078 
1079  // internal -> short
1080  for (i = 0; i < s->frame_size; i++)
1081  samples[i] = av_clip_int16(s->int_samples[i]);
1082 
1083  *got_frame_ptr = 1;
1084 
1085  return buf_size;
1086 }
1087 
1089  .name = "sonic",
1090  .long_name = NULL_IF_CONFIG_SMALL("Sonic"),
1091  .type = AVMEDIA_TYPE_AUDIO,
1092  .id = AV_CODEC_ID_SONIC,
1093  .priv_data_size = sizeof(SonicContext),
1094  .init = sonic_decode_init,
1095  .close = sonic_decode_close,
1096  .decode = sonic_decode_frame,
1097  .capabilities = AV_CODEC_CAP_DR1 | AV_CODEC_CAP_EXPERIMENTAL,
1098 };
1099 #endif /* CONFIG_SONIC_DECODER */
1100 
1101 #if CONFIG_SONIC_ENCODER
1103  .name = "sonic",
1104  .long_name = NULL_IF_CONFIG_SMALL("Sonic"),
1105  .type = AVMEDIA_TYPE_AUDIO,
1106  .id = AV_CODEC_ID_SONIC,
1107  .priv_data_size = sizeof(SonicContext),
1108  .init = sonic_encode_init,
1109  .encode2 = sonic_encode_frame,
1111  .capabilities = AV_CODEC_CAP_EXPERIMENTAL,
1112  .close = sonic_encode_close,
1113 };
1114 #endif
1115 
1116 #if CONFIG_SONIC_LS_ENCODER
1118  .name = "sonicls",
1119  .long_name = NULL_IF_CONFIG_SMALL("Sonic lossless"),
1120  .type = AVMEDIA_TYPE_AUDIO,
1121  .id = AV_CODEC_ID_SONIC_LS,
1122  .priv_data_size = sizeof(SonicContext),
1123  .init = sonic_encode_init,
1124  .encode2 = sonic_encode_frame,
1126  .capabilities = AV_CODEC_CAP_EXPERIMENTAL,
1127  .close = sonic_encode_close,
1128 };
1129 #endif
#define NULL
Definition: coverity.c:32
const struct AVCodec * codec
Definition: avcodec.h:535
int * int_samples
Definition: sonic.c:60
int * tail
Definition: sonic.c:64
int samplerate
Definition: sonic.c:57
#define LATTICE_FACTOR
Definition: sonic.c:76
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
Definition: error.h:59
static int shift(int a, int b)
Definition: sonic.c:82
static void copy(const float *p1, float *p2, const int length)
This structure describes decoded (raw) audio or video data.
Definition: frame.h:308
int lossless
Definition: sonic.c:52
ptrdiff_t const GLvoid * data
Definition: opengl_enc.c:100
static int get_se_golomb(GetBitContext *gb)
read signed exp golomb code.
Definition: golomb.h:239
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
Definition: j2kenc.c:208
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
Definition: get_bits.h:379
int * predictor_state[MAX_CHANNELS]
Definition: sonic.c:71
static av_cold int init(AVCodecContext *avctx)
Definition: avrndec.c:35
Range coder.
int size
Definition: packet.h:356
#define LATTICE_SHIFT
Definition: sonic.c:74
int av_log2(unsigned v)
Definition: intmath.c:26
The reader does not expect b to be semantically here and if the code is changed by maybe adding a a division or other the signedness will almost certainly be mistaken To avoid this confusion a new type was SUINT is the C unsigned type but it holds a signed int to use the same example SUINT a
Definition: undefined.txt:36
#define AV_CODEC_CAP_EXPERIMENTAL
Codec is experimental and is thus avoided in favor of non experimental encoders.
Definition: codec.h:98
int version
Definition: sonic.c:50
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:237
int * tap_quant
Definition: sonic.c:59
static void error(const char *err)
#define sample
AVCodec.
Definition: codec.h:190
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
Definition: decode_audio.c:71
static struct @315 state
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:245
#define MID_SIDE
Definition: sonic.c:45
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
Definition: encode.c:33
AVCodec ff_sonic_ls_encoder
enum AVSampleFormat sample_fmt
audio sample format
Definition: avcodec.h:1194
uint8_t
#define av_cold
Definition: attributes.h:88
static int get_rac(RangeCoder *c, uint8_t *const state)
Definition: rangecoder.h:136
#define MAX_CHANNELS
Definition: sonic.c:43
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
GLsizei GLboolean const GLfloat * value
Definition: opengl_enc.c:108
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
Definition: avcodec.h:627
uint8_t * data
Definition: packet.h:355
bitstream reader API header.
#define max(a, b)
Definition: cuda_runtime.h:33
#define RIGHT_SIDE
Definition: sonic.c:47
channels
Definition: aptx.h:33
#define av_log(a,...)
#define ff_sqrt
Definition: mathops.h:206
#define ROUNDED_DIV(a, b)
Definition: common.h:56
enum AVCodecID id
Definition: codec.h:204
int channels
Definition: sonic.c:57
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
Definition: internal.h:153
unsigned int pos
Definition: spdifenc.c:410
const char * name
Name of the codec implementation.
Definition: codec.h:197
uint8_t bits
Definition: vp3data.h:202
static int put_bits_count(PutBitContext *s)
Definition: put_bits.h:67
AVCodec ff_sonic_decoder
#define av_flatten
Definition: attributes.h:94
#define b
Definition: input.c:41
#define FFMIN(a, b)
Definition: common.h:96
static av_flatten int get_symbol(RangeCoder *c, uint8_t *state, int is_signed)
Definition: sonic.c:139
int block_align
Definition: sonic.c:57
these buffered frames must be flushed immediately if a new input produces new the filter must not call request_frame to get more It must just process the frame or queue it The task of requesting more frames is left to the filter s request_frame method or the application If a filter has several the filter must be ready for frames arriving randomly on any input any filter with several inputs will most likely require some kind of queuing mechanism It is perfectly acceptable to have a limited queue and to drop frames when the inputs are too unbalanced request_frame For filters that do not use the this method is called when a frame is wanted on an output For a it should directly call filter_frame on the corresponding output For a if there are queued frames already one of these frames should be pushed If the filter should request a frame on one of its repeatedly until at least one frame has been pushed Return or at least make progress towards producing a frame
void ff_build_rac_states(RangeCoder *c, int factor, int max_p)
Definition: rangecoder.c:68
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
#define s(width, name)
Definition: cbs_vp9.c:257
#define RATE_VARIATION
Definition: sonic.c:80
#define FF_ARRAY_ELEMS(a)
if(ret)
int frame_size
Number of samples per channel in an audio frame.
Definition: avcodec.h:1206
AVCodec ff_sonic_encoder
#define AV_LOG_INFO
Standard information.
Definition: log.h:205
Libavcodec external API header.
static void set_se_golomb(PutBitContext *pb, int i)
write signed exp golomb code.
Definition: golomb.h:665
AVSampleFormat
Audio sample formats.
Definition: samplefmt.h:58
int sample_rate
samples per second
Definition: avcodec.h:1186
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
Definition: get_bits.h:677
#define abs(x)
Definition: cuda_runtime.h:35
int * predictor_k
Definition: sonic.c:70
main external API structure.
Definition: avcodec.h:526
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
Definition: decode.c:1854
int tail_size
Definition: sonic.c:65
int extradata_size
Definition: avcodec.h:628
static unsigned int get_bits1(GetBitContext *s)
Definition: get_bits.h:498
static void skip_bits(GetBitContext *s, int n)
Definition: get_bits.h:467
av_cold void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size)
Definition: rangecoder.c:42
av_cold void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size)
Definition: rangecoder.c:53
#define LEFT_SIDE
Definition: sonic.c:46
static int intlist_write(RangeCoder *c, uint8_t *state, int *buf, int entries, int base_2_part)
Definition: sonic.c:163
static void predictor_init_state(int *k, int *state, int order)
Definition: sonic.c:451
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(const uint8_t *) pi-0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(const uint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(const int16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(const int16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16,*(const int16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16,*(const int16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(const int32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(const int32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32,*(const int32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32,*(const int32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(const int64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64,*(const int64_t *) pi *(1.0f/(UINT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64,*(const int64_t *) pi *(1.0/(UINT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(const float *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(const float *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(const float *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(const float *) pi *(UINT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(const double *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(const double *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(const double *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(const double *) pi *(UINT64_C(1)<< 63)))#define FMT_PAIR_FUNC(out, in) static conv_func_type *const fmt_pair_to_conv_functions[AV_SAMPLE_FMT_NB *AV_SAMPLE_FMT_NB]={FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_U8), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S16), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S32), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_FLT), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_DBL), FMT_PAIR_FUNC(AV_SAMPLE_FMT_U8, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S32, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_FLT, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_DBL, AV_SAMPLE_FMT_S64), FMT_PAIR_FUNC(AV_SAMPLE_FMT_S64, AV_SAMPLE_FMT_S64),};static void cpy1(uint8_t **dst, const uint8_t **src, int len){memcpy(*dst,*src, len);}static void cpy2(uint8_t **dst, const uint8_t **src, int len){memcpy(*dst,*src, 2 *len);}static void cpy4(uint8_t **dst, const uint8_t **src, int len){memcpy(*dst,*src, 4 *len);}static void cpy8(uint8_t **dst, const uint8_t **src, int len){memcpy(*dst,*src, 8 *len);}AudioConvert *swri_audio_convert_alloc(enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, const int *ch_map, int flags){AudioConvert *ctx;conv_func_type *f=fmt_pair_to_conv_functions[av_get_packed_sample_fmt(out_fmt)+AV_SAMPLE_FMT_NB *av_get_packed_sample_fmt(in_fmt)];if(!f) return NULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) return NULL;if(channels==1){in_fmt=av_get_planar_sample_fmt(in_fmt);out_fmt=av_get_planar_sample_fmt(out_fmt);}ctx->channels=channels;ctx->conv_f=f;ctx->ch_map=ch_map;if(in_fmt==AV_SAMPLE_FMT_U8||in_fmt==AV_SAMPLE_FMT_U8P) memset(ctx->silence, 0x80, sizeof(ctx->silence));if(out_fmt==in_fmt &&!ch_map){switch(av_get_bytes_per_sample(in_fmt)){case 1:ctx->simd_f=cpy1;break;case 2:ctx->simd_f=cpy2;break;case 4:ctx->simd_f=cpy4;break;case 8:ctx->simd_f=cpy8;break;}}if(HAVE_X86ASM &&1) swri_audio_convert_init_x86(ctx, out_fmt, in_fmt, channels);if(ARCH_ARM) swri_audio_convert_init_arm(ctx, out_fmt, in_fmt, channels);if(ARCH_AARCH64) swri_audio_convert_init_aarch64(ctx, out_fmt, in_fmt, channels);return ctx;}void swri_audio_convert_free(AudioConvert **ctx){av_freep(ctx);}int swri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, int len){int ch;int off=0;const int os=(out->planar?1:out->ch_count)*out->bps;unsigned misaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask){int planes=in->planar?in->ch_count:1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) in->ch[ch];misaligned|=m &ctx->in_simd_align_mask;}if(ctx->out_simd_align_mask){int planes=out->planar?out->ch_count:1;unsigned m=0;for(ch=0;ch< planes;ch++) m|=(intptr_t) out->ch[ch];misaligned|=m &ctx->out_simd_align_mask;}if(ctx->simd_f &&!ctx->ch_map &&!misaligned){off=len &~15;av_assert1(off >=0);av_assert1(off<=len);av_assert2(ctx->channels==SWR_CH_MAX||!in->ch[ctx->channels]);if(off >0){if(out->planar==in->planar){int planes=out->planar?out->ch_count:1;for(ch=0;ch< planes;ch++){ctx->simd_f(out-> ch ch
Definition: audioconvert.c:56
const uint8_t * quant
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
Definition: frame.h:322
uint8_t level
Definition: svq3.c:209
#define BASE_QUANT
Definition: sonic.c:79
#define SAMPLE_SHIFT
Definition: sonic.c:75
#define put_rac(C, S, B)
int ff_rac_terminate(RangeCoder *c, int version)
Terminates the range coder.
Definition: rangecoder.c:109
#define M_SQRT2
Definition: mathematics.h:61
int
int downsampling
Definition: sonic.c:54
int decorrelation
Definition: sonic.c:52
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
Definition: put_bits.h:101
signed 16 bits
Definition: samplefmt.h:61
int window_size
Definition: sonic.c:67
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
Definition: put_bits.h:48
void * priv_data
Definition: avcodec.h:553
#define av_free(p)
int channels
number of audio channels
Definition: avcodec.h:1187
double quantization
Definition: sonic.c:55
int * coded_samples[MAX_CHANNELS]
Definition: sonic.c:61
static int predictor_calc_error(int *k, int *state, int order, int error)
Definition: sonic.c:468
int frame_size
Definition: sonic.c:57
int num_taps
Definition: sonic.c:54
static enum AVSampleFormat sample_fmts[]
Definition: adpcmenc.c:723
int minor_version
Definition: sonic.c:51
FILE * out
Definition: movenc.c:54
#define SAMPLE_FACTOR
Definition: sonic.c:77
Filter the word “frame” indicates either a video frame or a group of audio samples
#define av_freep(p)
#define av_always_inline
Definition: attributes.h:45
static int shift_down(int a, int b)
Definition: sonic.c:87
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later.That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another.Frame references ownership and permissions
exp golomb vlc stuff
This structure stores compressed data.
Definition: packet.h:332
int nb_samples
number of audio samples (per channel) described by this frame
Definition: frame.h:374
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
Definition: codec.h:50
static int intlist_read(RangeCoder *c, uint8_t *state, int *buf, int entries, int base_2_part)
Definition: sonic.c:173
for(j=16;j >0;--j)
int i
Definition: input.c:406
static av_always_inline av_flatten void put_symbol(RangeCoder *c, uint8_t *state, int v, int is_signed, uint64_t rc_stat[256][2], uint64_t rc_stat2[32][2])
Definition: sonic.c:92
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about which is also called distortion Distortion can be quantified by almost any quality measurement one chooses the sum of squared differences is used but more complex methods that consider psychovisual effects can be used as well It makes no difference in this discussion First step
int * window
Definition: sonic.c:66
static uint8_t tmp[11]
Definition: aes_ctr.c:26