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 */
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
48
53
56
58
62
63 // for encoding
68
69 // for decoding
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
83 {
84 return (a+(1<<(b-1))) >>
b;
85 }
86
88 {
89 return (a>>b)+(a<0);
90 }
91
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){
108 if(e<=9){
109 for(i=0; i<e; i++){
110 put_rac(c, state+1+i, 1);
//1..10
111 }
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++){
123 }
125
126 for(i=e-1; i>=0; i--){
128 }
129
130 if(is_signed)
131 put_rac(c, state+11 + 10, v < 0);
//11..21
132 }
133 }else{
135 }
136 #undef put_rac
137 }
138
141 return 0;
142 else{
144 e= 0;
146 e++;
147 }
148
149 a= 1;
150 for(i=e-1; i>=0; i--){
152 }
153
154 e= -(is_signed &&
get_rac(c, state+11 +
FFMIN(e, 10)));
//11..21
155 return (a^e)-e;
156 }
157 }
158
159 #if 1
161 {
162 int i;
163
164 for (i = 0; i < entries; i++)
166
167 return 1;
168 }
169
171 {
172 int i;
173
174 for (i = 0; i < entries; i++)
176
177 return 1;
178 }
179 #elif 1
181 {
182 int i;
183
184 for (i = 0; i < entries; i++)
186
187 return 1;
188 }
189
191 {
192 int i;
193
194 for (i = 0; i < entries; i++)
196
197 return 1;
198 }
199
200 #else
201
202 #define ADAPT_LEVEL 8
203
204 static int bits_to_store(uint64_t x)
205 {
206 int res = 0;
207
208 while(x)
209 {
210 res++;
211 x >>= 1;
212 }
213 return res;
214 }
215
217 {
218 int i, bits;
219
220 if (!max)
221 return;
222
223 bits = bits_to_store(max);
224
225 for (i = 0; i < bits-1; i++)
227
228 if ( (value | (1 << (bits-1))) <= max)
229 put_bits(pb, 1, value & (1 << (bits-1)));
230 }
231
232 static unsigned int read_uint_max(
GetBitContext *gb,
int max)
233 {
234 int i, bits, value = 0;
235
236 if (!max)
237 return 0;
238
239 bits = bits_to_store(max);
240
241 for (i = 0; i < bits-1; i++)
243 value += 1 << i;
244
245 if ( (value | (1<<(bits-1))) <= max)
247 value += 1 << (bits-1);
248
250 }
251
253 {
254 int i, j, x = 0, low_bits = 0, max = 0;
255 int step = 256, pos = 0, dominant = 0, any = 0;
257
258 copy =
av_calloc(entries,
sizeof(*copy));
259 if (!copy)
261
262 if (base_2_part)
263 {
264 int energy = 0;
265
266 for (i = 0; i < entries; i++)
267 energy += abs(buf[i]);
268
269 low_bits = bits_to_store(energy / (entries * 2));
270 if (low_bits > 15)
271 low_bits = 15;
272
274 }
275
276 for (i = 0; i < entries; i++)
277 {
278 put_bits(pb, low_bits, abs(buf[i]));
279 copy[i] = abs(buf[i]) >> low_bits;
280 if (copy[i] > max)
281 max = abs(copy[i]);
282 }
283
284 bits =
av_calloc(entries*max,
sizeof(*bits));
285 if (!bits)
286 {
289 }
290
291 for (i = 0; i <= max; i++)
292 {
293 for (j = 0; j < entries; j++)
294 if (copy[j] >= i)
295 bits[x++] = copy[j] > i;
296 }
297
298 // store bitstream
299 while (pos < x)
300 {
301 int steplet = step >> 8;
302
303 if (pos + steplet > x)
304 steplet = x - pos;
305
306 for (i = 0; i < steplet; i++)
307 if (bits[i+pos] != dominant)
308 any = 1;
309
311
312 if (!any)
313 {
314 pos += steplet;
315 step += step / ADAPT_LEVEL;
316 }
317 else
318 {
319 int interloper = 0;
320
321 while (((pos + interloper) < x) && (bits[pos + interloper] == dominant))
322 interloper++;
323
324 // note change
325 write_uint_max(pb, interloper, (step >> 8) - 1);
326
327 pos += interloper + 1;
328 step -= step / ADAPT_LEVEL;
329 }
330
331 if (step < 256)
332 {
333 step = 65536 / step;
334 dominant = !dominant;
335 }
336 }
337
338 // store signs
339 for (i = 0; i < entries; i++)
340 if (buf[i])
342
345
346 return 0;
347 }
348
350 {
351 int i, low_bits = 0, x = 0;
352 int n_zeros = 0, step = 256, dominant = 0;
353 int pos = 0,
level = 0;
354 int *bits =
av_calloc(entries,
sizeof(*bits));
355
356 if (!bits)
358
359 if (base_2_part)
360 {
362
363 if (low_bits)
364 for (i = 0; i < entries; i++)
366 }
367
368 // av_log(NULL, AV_LOG_INFO, "entries: %d, low bits: %d\n", entries, low_bits);
369
370 while (n_zeros < entries)
371 {
372 int steplet = step >> 8;
373
375 {
376 for (i = 0; i < steplet; i++)
377 bits[x++] = dominant;
378
379 if (!dominant)
380 n_zeros += steplet;
381
382 step += step / ADAPT_LEVEL;
383 }
384 else
385 {
386 int actual_run = read_uint_max(gb, steplet-1);
387
388 // av_log(NULL, AV_LOG_INFO, "actual run: %d\n", actual_run);
389
390 for (i = 0; i < actual_run; i++)
391 bits[x++] = dominant;
392
393 bits[x++] = !dominant;
394
395 if (!dominant)
396 n_zeros += actual_run;
397 else
398 n_zeros++;
399
400 step -= step / ADAPT_LEVEL;
401 }
402
403 if (step < 256)
404 {
405 step = 65536 / step;
406 dominant = !dominant;
407 }
408 }
409
410 // reconstruct unsigned values
411 n_zeros = 0;
412 for (i = 0; n_zeros < entries; i++)
413 {
414 while(1)
415 {
416 if (pos >= entries)
417 {
418 pos = 0;
419 level += 1 << low_bits;
420 }
421
422 if (buf[pos] >=
level)
423 break;
424
425 pos++;
426 }
427
428 if (bits[i])
429 buf[pos] += 1 << low_bits;
430 else
431 n_zeros++;
432
433 pos++;
434 }
436
437 // read signs
438 for (i = 0; i < entries; i++)
440 buf[i] = -buf[i];
441
442 // av_log(NULL, AV_LOG_INFO, "zeros: %d pos: %d\n", n_zeros, pos);
443
444 return 0;
445 }
446 #endif
447
449 {
450 int i;
451
452 for (i = order-2; i >= 0; i--)
453 {
454 int j, p, x = state[i];
455
456 for (j = 0, p = i+1; p < order; j++,p++)
457 {
461 }
462 }
463 }
464
466 {
468
469 #if 1
470 int *k_ptr = &(k[order-2]),
471 *state_ptr = &(state[order-2]);
472 for (i = order-2; i >= 0; i--, k_ptr--, state_ptr--)
473 {
474 int k_value = *k_ptr, state_value = *state_ptr;
477 }
478 #else
479 for (i = order-2; i >= 0; i--)
480 {
483 }
484 #endif
485
486 // don't drift too far, to avoid overflows
489
490 state[0] = x;
491
492 return x;
493 }
494
495 #if CONFIG_SONIC_ENCODER || CONFIG_SONIC_LS_ENCODER
496 // Heavily modified Levinson-Durbin algorithm which
497 // copes better with quantization, and calculates the
498 // actual whitened result as it goes.
499
500 static int modified_levinson_durbin(
int *
window,
int window_entries,
501 int *
out,
int out_entries,
int channels,
int *tap_quant)
502 {
503 int i;
505
506 if (!state)
508
509 memcpy(state, window, 4* window_entries);
510
511 for (i = 0; i < out_entries; i++)
512 {
513 int step = (i+1)*channels, k, j;
514 double xx = 0.0, xy = 0.0;
515 #if 1
516 int *x_ptr = &(window[step]);
517 int *state_ptr = &(state[0]);
518 j = window_entries - step;
519 for (;j>0;j--,x_ptr++,state_ptr++)
520 {
521 double x_value = *x_ptr;
522 double state_value = *state_ptr;
523 xx += state_value*state_value;
524 xy += x_value*state_value;
525 }
526 #else
527 for (j = 0; j <= (window_entries - step); j++);
528 {
529 double stepval = window[step+j];
530 double stateval = window[j];
531 // xx += (double)window[j]*(double)window[j];
532 // xy += (double)window[step+j]*(double)window[j];
533 xx += stateval*stateval;
534 xy += stepval*stateval;
535 }
536 #endif
537 if (xx == 0.0)
538 k = 0;
539 else
540 k = (
int)(floor(-xy/xx * (
double)
LATTICE_FACTOR / (double)(tap_quant[i]) + 0.5));
541
546
547 out[i] = k;
548 k *= tap_quant[i];
549
550 #if 1
551 x_ptr = &(window[step]);
552 state_ptr = &(state[0]);
553 j = window_entries - step;
554 for (;j>0;j--,x_ptr++,state_ptr++)
555 {
556 int x_value = *x_ptr;
557 int state_value = *state_ptr;
560 }
561 #else
562 for (j=0; j <= (window_entries - step); j++)
563 {
564 int stepval = window[step+j];
565 int stateval=state[j];
568 }
569 #endif
570 }
571
573 return 0;
574 }
575
576 static inline int code_samplerate(int samplerate)
577 {
578 switch (samplerate)
579 {
580 case 44100: return 0;
581 case 22050: return 1;
582 case 11025: return 2;
583 case 96000: return 3;
584 case 48000: return 4;
585 case 32000: return 5;
586 case 24000: return 6;
587 case 16000: return 7;
588 case 8000: return 8;
589 }
591 }
592
594 {
597 int i;
598
600
602 {
604 return AVERROR(EINVAL);
/* only stereo or mono for now */
605 }
606
609 else
611
613 {
618 }
619 else
620 {
624 }
625
626 // max tap 2048
630 }
631
632 // generate taps
636
639
642
645
650
654
656 {
660 }
661
663
668
673
676 {
680 }
683 }
690 put_bits(&pb, 1, 0);
// XXX FIXME: no custom tap quant table
691
694
695 av_log(avctx,
AV_LOG_INFO,
"Sonic: ver: %d.%d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
697
699
700 return 0;
701 }
702
704 {
706 int i;
707
710
716
717 return 0;
718 }
719
722 {
725 int i, j,
ch,
quant = 0, x = 0;
726 int ret;
727 const short *samples = (
const int16_t*)frame->
data[0];
729
731 return ret;
732
735 memset(state, 128, sizeof(state));
736
737 // short -> internal
740
744
746 {
749 {
752 }
753 break;
757 break;
761 break;
762 }
763
765
768
771
774
777
778 // generate taps
781 if (ret < 0)
782 return ret;
783
785 return ret;
786
787 for (ch = 0; ch < s->
channels; ch++)
788 {
791 {
792 int sum = 0;
796 }
797 }
798
799 // simple rate control code
801 {
802 double energy1 = 0.0, energy2 = 0.0;
803 for (ch = 0; ch < s->
channels; ch++)
804 {
806 {
809 energy1 += fabs(sample);
810 }
811 }
812
815
816 // increase bitrate when samples are like a gaussian distribution
817 // reduce bitrate when samples are like a two-tailed exponential distribution
818
819 if (energy2 > energy1)
821
823 // av_log(avctx, AV_LOG_DEBUG, "quant: %d energy: %f / %f\n", quant, energy1, energy2);
824
825 quant = av_clip(quant, 1, 65534);
826
828
830 }
831
832 // write out coded samples
833 for (ch = 0; ch < s->
channels; ch++)
834 {
838
840 return ret;
841 }
842
843 // av_log(avctx, AV_LOG_DEBUG, "used bytes: %d\n", (put_bits_count(&pb)+7)/8);
844
846 *got_packet_ptr = 1;
847 return 0;
848
849 }
850 #endif /* CONFIG_SONIC_ENCODER || CONFIG_SONIC_LS_ENCODER */
851
852 #if CONFIG_SONIC_DECODER
853 static const int samplerate_table[] =
854 { 44100, 22050, 11025, 96000, 48000, 32000, 24000, 16000, 8000 };
855
857 {
860 int i;
861 int ret;
862
865
867 {
870 }
871
873 if (ret < 0)
874 return ret;
875
880 }
882 {
885 }
886
888 {
889 int sample_rate_index;
891 sample_rate_index =
get_bits(&gb, 4);
895 }
896 s->
samplerate = samplerate_table[sample_rate_index];
899 }
900
902 {
905 }
907
915 }
916
921 }
922
926
929 // avctx->frame_size = s->block_align;
930
933 "number of taps times channels (%d * %d) larger than frame size %d\n",
936 }
937
938 av_log(avctx,
AV_LOG_INFO,
"Sonic: ver: %d.%d ls: %d dr: %d taps: %d block: %d frame: %d downsamp: %d\n",
940
941 // generate taps
945
948
950
952 {
956 }
957
959 {
963 }
967
969 return 0;
970 }
971
973 {
975 int i;
976
980
982 {
985 }
986
987 return 0;
988 }
989
991 void *
data,
int *got_frame_ptr,
993 {
995 int buf_size = avpkt->
size;
1000 int16_t *samples;
1002
1003 if (buf_size == 0) return 0;
1004
1007 return ret;
1008 samples = (int16_t *)frame->
data[0];
1009
1010 // av_log(NULL, AV_LOG_INFO, "buf_size: %d\n", buf_size);
1011
1012 memset(state, 128, sizeof(state));
1015
1017
1018 // dequantize
1021
1023 quant = 1;
1024 else
1026
1027 // av_log(NULL, AV_LOG_INFO, "quant: %d\n", quant);
1028
1029 for (ch = 0; ch < s->
channels; ch++)
1030 {
1032
1034
1036
1038 {
1040 {
1043 }
1044
1047 }
1048
1051 }
1052
1054 {
1057 {
1060 }
1061 break;
1065 break;
1069 break;
1070 }
1071
1075
1076 // internal -> short
1079
1080 *got_frame_ptr = 1;
1081
1082 return buf_size;
1083 }
1084
1091 .
init = sonic_decode_init,
1092 .close = sonic_decode_close,
1093 .
decode = sonic_decode_frame,
1095 };
1096 #endif /* CONFIG_SONIC_DECODER */
1097
1098 #if CONFIG_SONIC_ENCODER
1105 .
init = sonic_encode_init,
1106 .encode2 = sonic_encode_frame,
1109 .close = sonic_encode_close,
1110 };
1111 #endif
1112
1113 #if CONFIG_SONIC_LS_ENCODER
1120 .
init = sonic_encode_init,
1121 .encode2 = sonic_encode_frame,
1124 .close = sonic_encode_close,
1125 };
1126 #endif
const struct AVCodec * codec
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
static int shift(int a, int b)
static void copy(const float *p1, float *p2, const int length)
This structure describes decoded (raw) audio or video data.
ptrdiff_t const GLvoid * data
static int get_se_golomb(GetBitContext *gb)
read signed exp golomb code.
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
static unsigned int get_bits(GetBitContext *s, int n)
Read 1-25 bits.
int * predictor_state[MAX_CHANNELS]
static av_cold int init(AVCodecContext *avctx)
#define AV_CODEC_CAP_EXPERIMENTAL
Codec is experimental and is thus avoided in favor of non experimental encoders.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
static void decode(AVCodecContext *dec_ctx, AVPacket *pkt, AVFrame *frame, FILE *outfile)
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
int ff_rac_terminate(RangeCoder *c)
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
AVCodec ff_sonic_ls_encoder
enum AVSampleFormat sample_fmt
audio sample format
static int get_rac(RangeCoder *c, uint8_t *const state)
uint8_t * extradata
some codecs need / can use extradata like Huffman tables.
bitstream reader API header.
#define ROUNDED_DIV(a, b)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
#define NULL_IF_CONFIG_SMALL(x)
Return NULL if CONFIG_SMALL is true, otherwise the argument without modification. ...
const char * name
Name of the codec implementation.
static int put_bits_count(PutBitContext *s)
static SDL_Window * window
static av_flatten int get_symbol(RangeCoder *c, uint8_t *state, int is_signed)
GLsizei GLboolean const GLfloat * value
void ff_build_rac_states(RangeCoder *c, int factor, int max_p)
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
static void error(const char *err)
#define FF_ARRAY_ELEMS(a)
int frame_size
Number of samples per channel in an audio frame.
#define AV_LOG_INFO
Standard information.
Libavcodec external API header.
static void set_se_golomb(PutBitContext *pb, int i)
write signed exp golomb code.
AVSampleFormat
Audio sample formats.
int sample_rate
samples per second
static int init_get_bits8(GetBitContext *s, const uint8_t *buffer, int byte_size)
Initialize GetBitContext.
main external API structure.
int ff_get_buffer(AVCodecContext *avctx, AVFrame *frame, int flags)
Get a buffer for a frame.
static unsigned int get_bits1(GetBitContext *s)
static void skip_bits(GetBitContext *s, int n)
av_cold void ff_init_range_encoder(RangeCoder *c, uint8_t *buf, int buf_size)
av_cold void ff_init_range_decoder(RangeCoder *c, const uint8_t *buf, int buf_size)
static int intlist_write(RangeCoder *c, uint8_t *state, int *buf, int entries, int base_2_part)
static void predictor_init_state(int *k, int *state, int order)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
int channels
number of audio channels
int * coded_samples[MAX_CHANNELS]
static int predictor_calc_error(int *k, int *state, int order, int error)
static enum AVSampleFormat sample_fmts[]
static int shift_down(int a, int b)
This structure stores compressed data.
int nb_samples
number of audio samples (per channel) described by this frame
#define AV_CODEC_CAP_DR1
Codec uses get_buffer() for allocating buffers and supports custom allocators.
static int intlist_read(RangeCoder *c, uint8_t *state, int *buf, int entries, int base_2_part)
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])
uint8_t pi<< 24) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_U8,(uint64_t)((*(constuint8_t *) pi-0x80U))<< 56) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S16,(uint64_t)(*(constint16_t *) pi)<< 48) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_S32,(uint64_t)(*(constint32_t *) pi)<< 32) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S64,(*(constint64_t *) pi >>56)+0x80) CONV_FUNC(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0f/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S64,*(constint64_t *) pi *(1.0/(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_FLT, llrintf(*(constfloat *) pi *(INT64_C(1)<< 63))) CONV_FUNC(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31)))) CONV_FUNC(AV_SAMPLE_FMT_S64, int64_t, AV_SAMPLE_FMT_DBL, llrint(*(constdouble *) pi *(INT64_C(1)<< 63)))#defineFMT_PAIR_FUNC(out, in) staticconv_func_type *constfmt_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),};staticvoidcpy1(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, len);}staticvoidcpy2(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 2 *len);}staticvoidcpy4(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 4 *len);}staticvoidcpy8(uint8_t **dst, constuint8_t **src, intlen){memcpy(*dst,*src, 8 *len);}AudioConvert *swri_audio_convert_alloc(enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, constint *ch_map, intflags){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) returnNULL;ctx=av_mallocz(sizeof(*ctx));if(!ctx) returnNULL;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)){case1:ctx->simd_f=cpy1;break;case2:ctx->simd_f=cpy2;break;case4:ctx->simd_f=cpy4;break;case8: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);returnctx;}voidswri_audio_convert_free(AudioConvert **ctx){av_freep(ctx);}intswri_audio_convert(AudioConvert *ctx, AudioData *out, AudioData *in, intlen){intch;intoff=0;constintos=(out->planar?1:out->ch_count)*out->bps;unsignedmisaligned=0;av_assert0(ctx->channels==out->ch_count);if(ctx->in_simd_align_mask){intplanes=in->planar?in->ch_count:1;unsignedm=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){intplanes=out->planar?out->ch_count:1;unsignedm=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){intplanes=out->planar?out->ch_count:1;for(ch=0;ch< planes;ch++){ctx->simd_f(out-> ch ch