1 /*
2 * The simplest mpeg audio layer 2 encoder
3 * Copyright (c) 2000, 2001 Fabrice Bellard
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
22 /**
23 * @file
24 * The simplest mpeg audio layer 2 encoder.
25 */
26
28
32
33 #define FRAC_BITS 15 /* fractional bits for sb_samples and dct */
34 #define WFRAC_BITS 14 /* fractional bits for window */
35
40
41 /* currently, cannot change these constants (need to modify
42 quantization stage) */
43 #define MUL(a,b) (((int64_t)(a) * (int64_t)(b)) >> FRAC_BITS)
44
45 #define SAMPLES_BUF_SIZE 4096
46
50 int lsf;
/* 1 if mpeg2 low bitrate selected */
53 int frame_size;
/* frame size, in bits, without padding */
54 /* padding computation */
60 /* code to group 3 scale factors */
62 int sblimit;
/* number of used subbands */
67 #if USE_FLOATS
68 float scale_factor_inv_table[64];
69 #else
72 #endif
75
77 {
84
86 av_log(avctx,
AV_LOG_ERROR,
"encoding %d channel(s) is not allowed in mp2\n", channels);
88 }
89 bitrate = bitrate / 1000;
93
94 /* encoding freq */
96 for(i=0;i<3;i++) {
98 break;
101 break;
102 }
103 }
104 if (i == 3){
107 }
109
110 /* encoding bitrate & frequency */
111 for(i=1;i<15;i++) {
113 break;
114 }
116 i = 14;
119 }
120 if (i == 15){
123 }
125
126 /* compute total header size & pad bit */
127
130
131 /* frame fractional size to compute padding */
134
135 /* select the right allocation table */
137
138 /* number of used subbands */
141
142 ff_dlog(avctx,
"%d kb/s, %d Hz, frame_size=%d bits, table=%d, padincr=%x\n",
144
147
148 for(i=0;i<257;i++) {
149 int v;
151 #if WFRAC_BITS != 16
153 #endif
155 if ((i & 63) != 0)
156 v = -v;
157 if (i != 0)
159 }
160
161 for(i=0;i<64;i++) {
162 v = (
int)(
exp2((3 - i) / 3.0) * (1 << 20));
163 if (v <= 0)
164 v = 1;
166 #if USE_FLOATS
167 s->scale_factor_inv_table[i] =
exp2(-(3 - i) / 3.0) / (float)(1 << 20);
168 #else
169 #define P 15
172 #endif
173 }
174 for(i=0;i<128;i++) {
175 v = i - 64;
176 if (v <= -3)
177 v = 0;
178 else if (v < 0)
179 v = 1;
180 else if (v == 0)
181 v = 2;
182 else if (v < 3)
183 v = 3;
184 else
185 v = 4;
187 }
188
189 for(i=0;i<17;i++) {
191 if (v < 0)
192 v = -v;
193 else
194 v = v * 3;
196 }
197
198 return 0;
199 }
200
201 /* 32 point floating point IDCT without 1/sqrt(2) coef zero scaling */
203 {
204 int i, j;
207
208 for(j=31;j>=3;j-=2) tab[j] += tab[j - 2];
209
210 t = tab + 30;
211 t1 = tab + 2;
212 do {
213 t[0] += t[-4];
214 t[1] += t[1 - 4];
215 t -= 4;
216 } while (t != t1);
217
218 t = tab + 28;
219 t1 = tab + 4;
220 do {
221 t[0] += t[-8];
222 t[1] += t[1-8];
223 t[2] += t[2-8];
224 t[3] += t[3-8];
225 t -= 8;
226 } while (t != t1);
227
229 t1 = tab + 32;
230 do {
231 t[ 3] = -t[ 3];
232 t[ 6] = -t[ 6];
233
234 t[11] = -t[11];
235 t[12] = -t[12];
236 t[13] = -t[13];
237 t[15] = -t[15];
238 t += 16;
239 } while (t != t1);
240
241
243 t1 = tab + 8;
244 do {
245 int x1, x2, x3, x4;
246
248 x4 = t[0] - x3;
249 x3 = t[0] + x3;
250
252 x1 =
MUL((t[8] - x2), xp[0]);
253 x2 =
MUL((t[8] + x2), xp[1]);
254
255 t[ 0] = x3 + x1;
256 t[ 8] = x4 - x2;
257 t[16] = x4 + x2;
258 t[24] = x3 - x1;
259 t++;
260 } while (t != t1);
261
262 xp += 2;
264 t1 = tab + 4;
265 do {
266 xr =
MUL(t[28],xp[0]);
267 t[28] = (t[0] - xr);
268 t[0] = (t[0] + xr);
269
270 xr =
MUL(t[4],xp[1]);
271 t[ 4] = (t[24] - xr);
272 t[24] = (t[24] + xr);
273
274 xr =
MUL(t[20],xp[2]);
275 t[20] = (t[8] - xr);
276 t[ 8] = (t[8] + xr);
277
278 xr =
MUL(t[12],xp[3]);
279 t[12] = (t[16] - xr);
280 t[16] = (t[16] + xr);
281 t++;
282 } while (t != t1);
283 xp += 4;
284
285 for (i = 0; i < 4; i++) {
286 xr =
MUL(tab[30-i*4],xp[0]);
287 tab[30-i*4] = (tab[i*4] - xr);
288 tab[ i*4] = (tab[i*4] + xr);
289
290 xr =
MUL(tab[ 2+i*4],xp[1]);
291 tab[ 2+i*4] = (tab[28-i*4] - xr);
292 tab[28-i*4] = (tab[28-i*4] + xr);
293
294 xr =
MUL(tab[31-i*4],xp[0]);
295 tab[31-i*4] = (tab[1+i*4] - xr);
296 tab[ 1+i*4] = (tab[1+i*4] + xr);
297
298 xr =
MUL(tab[ 3+i*4],xp[1]);
299 tab[ 3+i*4] = (tab[29-i*4] - xr);
300 tab[29-i*4] = (tab[29-i*4] + xr);
301
302 xp += 2;
303 }
304
305 t = tab + 30;
306 t1 = tab + 1;
307 do {
308 xr =
MUL(t1[0], *xp);
309 t1[0] = (t[0] - xr);
310 t[0] = (t[0] + xr);
311 t -= 2;
312 t1 += 2;
313 xp++;
314 } while (t >= tab);
315
316 for(i=0;i<32;i++) {
318 }
319 }
320
321 #define WSHIFT (WFRAC_BITS + 15 - FRAC_BITS)
322
324 {
325 short *p, *q;
328 int tmp1[32];
330
333 for(j=0;j<36;j++) {
334 /* 32 samples at once */
335 for(i=0;i<32;i++) {
337 samples += incr;
338 }
339
340 /* filter */
343 /* maxsum = 23169 */
344 for(i=0;i<64;i++) {
345 sum = p[0*64] * q[0*64];
346 sum += p[1*64] * q[1*64];
347 sum += p[2*64] * q[2*64];
348 sum += p[3*64] * q[3*64];
349 sum += p[4*64] * q[4*64];
350 sum += p[5*64] * q[5*64];
351 sum += p[6*64] * q[6*64];
352 sum += p[7*64] * q[7*64];
353 tmp[i] = sum;
354 p++;
355 q++;
356 }
357 tmp1[0] = tmp[16] >>
WSHIFT;
358 for( i=1; i<=16; i++ ) tmp1[i] = (tmp[i+16]+tmp[16-i]) >>
WSHIFT;
359 for( i=17; i<=31; i++ ) tmp1[i] = (tmp[i+16]-tmp[80-i]) >>
WSHIFT;
360
362
363 /* advance of 32 samples */
364 offset -= 32;
365 out += 32;
366 /* handle the wrap around */
367 if (offset < 0) {
371 }
372 }
374 }
375
377 unsigned char scale_code[
SBLIMIT],
378 unsigned char scale_factors[SBLIMIT][3],
379 int sb_samples[3][12][SBLIMIT],
380 int sblimit)
381 {
382 int *p, vmax, v,
n, i, j, k, code;
384 unsigned char *sf = &scale_factors[0][0];
385
386 for(j=0;j<sblimit;j++) {
387 for(i=0;i<3;i++) {
388 /* find the max absolute value */
389 p = &sb_samples[i][0][j];
390 vmax = abs(*p);
391 for(k=1;k<12;k++) {
393 v = abs(*p);
394 if (v > vmax)
395 vmax = v;
396 }
397 /* compute the scale factor index using log 2 computations */
398 if (vmax > 1) {
400 /* n is the position of the MSB of vmax. now
401 use at most 2 compares to find the index */
402 index = (21 -
n) * 3 - 3;
403 if (index >= 0) {
404 while (vmax <= s->scale_factor_table[index+1])
405 index++;
406 } else {
407 index = 0; /* very unlikely case of overflow */
408 }
409 } else {
410 index = 62; /* value 63 is not allowed */
411 }
412
415 /* store the scale factor */
418 }
419
420 /* compute the transmission factor : look if the scale factors
421 are close enough to each other */
424
425 /* handle the 25 cases */
426 switch(d1 * 5 + d2) {
427 case 0*5+0:
428 case 0*5+4:
429 case 3*5+4:
430 case 4*5+0:
431 case 4*5+4:
432 code = 0;
433 break;
434 case 0*5+1:
435 case 0*5+2:
436 case 4*5+1:
437 case 4*5+2:
438 code = 3;
439 sf[2] = sf[1];
440 break;
441 case 0*5+3:
442 case 4*5+3:
443 code = 3;
444 sf[1] = sf[2];
445 break;
446 case 1*5+0:
447 case 1*5+4:
448 case 2*5+4:
449 code = 1;
450 sf[1] = sf[0];
451 break;
452 case 1*5+1:
453 case 1*5+2:
454 case 2*5+0:
455 case 2*5+1:
456 case 2*5+2:
457 code = 2;
458 sf[1] = sf[2] = sf[0];
459 break;
460 case 2*5+3:
461 case 3*5+3:
462 code = 2;
463 sf[0] = sf[1] = sf[2];
464 break;
465 case 3*5+0:
466 case 3*5+1:
467 case 3*5+2:
468 code = 2;
469 sf[0] = sf[2] = sf[1];
470 break;
471 case 1*5+3:
472 code = 2;
473 if (sf[0] > sf[2])
474 sf[0] = sf[2];
475 sf[1] = sf[2] = sf[0];
476 break;
477 default:
479 code = 0; /* kill warning */
480 }
481
483 sf[0], sf[1], sf[2], d1, d2, code);
484 scale_code[j] = code;
485 sf += 3;
486 }
487 }
488
489 /* The most important function : psycho acoustic module. In this
490 encoder there is basically none, so this is the worst you can do,
491 but also this is the simpler. */
493 {
494 int i;
495
498 }
499 }
500
501
502 #define SB_NOTALLOCATED 0
503 #define SB_ALLOCATED 1
505
506 /* Try to maximize the smr while using a number of bits inferior to
507 the frame size. I tried to make the code simpler, faster and
508 smaller than other encoders :-) */
512 int *padding)
513 {
514 int i,
ch,
b, max_smr, max_ch, max_sb, current_frame_size, max_frame_size;
515 int incr;
518 const unsigned char *alloc;
519
520 memcpy(smr, smr1, s->
nb_channels *
sizeof(
short) * SBLIMIT);
523
524 /* compute frame size and padding */
530 max_frame_size += 8;
531 } else {
533 }
534
535 /* compute the header + bit alloc size */
536 current_frame_size = 32;
539 incr = alloc[0];
541 alloc += 1 << incr;
542 }
543 for(;;) {
544 /* look for the subband with the largest signal to mask ratio */
545 max_sb = -1;
546 max_ch = -1;
547 max_smr = INT_MIN;
550 if (smr[ch][i] > max_smr && subband_status[ch][i] !=
SB_NOMORE) {
551 max_smr = smr[
ch][i];
552 max_sb = i;
554 }
555 }
556 }
557 if (max_sb < 0)
558 break;
559 ff_dlog(
NULL,
"current=%d max=%d max_sb=%d max_ch=%d alloc=%d\n",
560 current_frame_size, max_frame_size, max_sb, max_ch,
562
563 /* find alloc table entry (XXX: not optimal, should use
564 pointer table) */
566 for(i=0;i<max_sb;i++) {
567 alloc += 1 << alloc[0];
568 }
569
571 /* nothing was coded for this band: add the necessary bits */
574 } else {
575 /* increments bit allocation */
579 }
580
581 if (current_frame_size + incr <= max_frame_size) {
582 /* can increase size */
584 current_frame_size += incr;
585 /* decrease smr by the resolution we added */
586 smr[max_ch][max_sb] = smr1[max_ch][max_sb] -
quant_snr[alloc[
b]];
587 /* max allocation size reached ? */
588 if (b == ((1 << alloc[0]) - 1))
589 subband_status[max_ch][max_sb] =
SB_NOMORE;
590 else
592 } else {
593 /* cannot increase the size of this subband */
594 subband_status[max_ch][max_sb] =
SB_NOMORE;
595 }
596 }
597 *padding = max_frame_size - current_frame_size;
599 }
600
601 /*
602 * Output the MPEG audio layer 2 frame. Note how the code is small
603 * compared to other encoders :-)
604 */
607 int padding)
608 {
609 int i, j, k, l, bit_alloc_bits,
b,
ch;
610 unsigned char *sf;
611 int q[3];
613
614 /* header */
615
617 put_bits(p, 1, 1 - s->
lsf);
/* 1 = MPEG-1 ID, 0 = MPEG-2 lsf ID */
619 put_bits(p, 1, 1);
/* no error protection */
623 put_bits(p, 1, 0);
/* private_bit */
626 put_bits(p, 1, 0);
/* no copyright */
628 put_bits(p, 2, 0);
/* no emphasis */
629
630 /* bit allocation */
631 j = 0;
636 }
637 j += 1 << bit_alloc_bits;
638 }
639
640 /* scale codes */
645 }
646 }
647
648 /* scale factors */
654 case 0:
658 break;
659 case 3:
660 case 1:
663 break;
664 case 2:
666 break;
667 }
668 }
669 }
670 }
671
672 /* quantization & write sub band samples */
673
674 for(k=0;k<3;k++) {
675 for(l=0;l<12;l+=3) {
676 j = 0;
681 if (b) {
682 int qindex, steps, m,
sample, bits;
683 /* we encode 3 sub band samples of the same sub band at a time */
686 for(m=0;m<3;m++) {
688 /* divide by scale factor */
689 #if USE_FLOATS
690 {
692 a = (float)sample * s->scale_factor_inv_table[s->
scale_factors[ch][i][k]];
693 q[m] = (int)((a + 1.0) * steps * 0.5);
694 }
695 #else
696 {
701
702 /* normalize to P bits */
703 if (shift < 0)
704 q1 = sample << (-
shift);
705 else
706 q1 = sample >>
shift;
707 q1 = (q1 *
mult) >>
P;
709 if (q1 < 0)
710 q1 = 0;
711 q[m] = (q1 * (unsigned)steps) >> (
P + 1);
712 }
713 #endif
714 if (q[m] >= steps)
715 q[m] = steps - 1;
717 }
719 if (bits < 0) {
720 /* group the 3 values to save bits */
722 q[0] + steps * (q[1] + steps * q[2]));
723 } else {
727 }
728 }
729 }
730 /* next subband in alloc table */
731 j += 1 << bit_alloc_bits;
732 }
733 }
734 }
735
736 /* padding */
737 for(i=0;i<padding;i++)
739
740 /* flush */
742 }
743
746 {
748 const int16_t *samples = (
const int16_t *)frame->
data[0];
751 int padding, i, ret;
752
755 }
756
760 }
763 }
765
767 return ret;
768
770
772
775
777 *got_packet_ptr = 1;
778 return 0;
779 }
780
782 { "b", "0" },
784 };
785
#define MPA_MAX_CODED_FRAME_SIZE
static int shift(int a, int b)
This structure describes decoded (raw) audio or video data.
static void put_bits(Jpeg2000EncoderContext *s, int val, int n)
put n times val bit
int64_t bit_rate
the average bitrate
unsigned char scale_diff_table[128]
static const unsigned char nb_scale_factors[4]
unsigned short scale_factor_mult[64]
unsigned short total_quant_bits[17]
const int ff_mpa_quant_bits[17]
static const uint8_t q1[256]
mpeg audio layer common tables.
const int32_t ff_mpa_enwindow[257]
static int MPA_encode_frame(AVCodecContext *avctx, AVPacket *avpkt, const AVFrame *frame, int *got_packet_ptr)
#define av_assert0(cond)
assert() equivalent, that is always enabled.
int ff_alloc_packet2(AVCodecContext *avctx, AVPacket *avpkt, int64_t size, int64_t min_size)
Check AVPacket size and/or allocate data.
static const AVCodecDefault mp2_defaults[]
#define av_assert2(cond)
assert() equivalent, that does lie in speed critical code.
static const int costab32[30]
const int ff_mpa_quant_steps[17]
int scale_factor_table[64]
const uint16_t avpriv_mpa_freq_tab[3]
const unsigned char *const ff_mpa_alloc_tables[5]
int64_t pts
Presentation timestamp in time_base units (time when frame should be shown to user).
unsigned char scale_factors[MPA_MAX_CHANNELS][SBLIMIT][3]
mpeg audio layer 2 tables.
static void compute_bit_allocation(MpegAudioContext *s, short smr1[MPA_MAX_CHANNELS][SBLIMIT], unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT], int *padding)
unsigned char scale_code[MPA_MAX_CHANNELS][SBLIMIT]
static int bit_alloc(AC3EncodeContext *s, int snr_offset)
Run the bit allocation with a given SNR offset.
static const unsigned short quant_snr[17]
static av_cold int MPA_encode_init(AVCodecContext *avctx)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
static void compute_scale_factors(MpegAudioContext *s, unsigned char scale_code[SBLIMIT], unsigned char scale_factors[SBLIMIT][3], int sb_samples[3][12][SBLIMIT], int sblimit)
static const struct endianess table[]
int initial_padding
Audio only.
static const int bitinv32[32]
static const uint8_t offset[127][2]
static int put_bits_count(PutBitContext *s)
const unsigned char * alloc_table
int8_t scale_factor_shift[64]
audio channel layout utility functions
static int16_t mult(Float11 *f1, Float11 *f2)
static void encode_frame(MpegAudioContext *s, unsigned char bit_alloc[MPA_MAX_CHANNELS][SBLIMIT], int padding)
int frame_size
Number of samples per channel in an audio frame.
Libavcodec external API header.
int samples_offset[MPA_MAX_CHANNELS]
int sample_rate
samples per second
static const float fixed_smr[SBLIMIT]
main external API structure.
short samples_buf[MPA_MAX_CHANNELS][SAMPLES_BUF_SIZE]
static void idct32(int *out, int *tab)
uint8_t * data[AV_NUM_DATA_POINTERS]
pointer to the picture/channel planes.
static void filter(MpegAudioContext *s, int ch, const short *samples, int incr)
common internal api header.
static void flush_put_bits(PutBitContext *s)
Pad the end of the output stream with zeros.
mpeg audio declarations for both encoder and decoder.
static void init_put_bits(PutBitContext *s, uint8_t *buffer, int buffer_size)
Initialize the PutBitContext s.
const int ff_mpa_sblimit_table[5]
static void psycho_acoustic_model(MpegAudioContext *s, short smr[SBLIMIT])
int ff_mpa_l2_select_table(int bitrate, int nb_channels, int freq, int lsf)
int channels
number of audio channels
static const struct twinvq_data tab
const uint16_t avpriv_mpa_bitrate_tab[2][3][15]
static av_always_inline int64_t ff_samples_to_time_base(AVCodecContext *avctx, int64_t samples)
Rescale from sample rate to AVCodecContext.time_base.
int sb_samples[MPA_MAX_CHANNELS][3][12][SBLIMIT]
This structure stores compressed data.
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
#define AV_NOPTS_VALUE
Undefined timestamp value.
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