1 /*
2 * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
23 // #include "libavutil/error.h"
28
35
37 {
38 int ret;
39
43 }
44
45 /* set channel mixing parameters */
51 }
57 }
64
65 /* set resampling parameters */
68
69 /* select internal sample format if not specified by the user */
76 if (max_bps <= 2) {
80 } else {
81 if (max_bps <= 4) {
86 /* if one is s32 and the other is flt, use dbl */
88 } else {
89 /* if one is s32 and the other is s32, s16, or u8, use s32 */
91 }
92 } else {
93 /* if one is flt and the other is flt, s16 or u8, use flt */
95 }
96 } else {
97 /* if either is dbl, use dbl */
99 }
100 }
103 }
104
105 /* we may need to add an extra conversion in order to remap channels if
106 the output format is not planar */
110 }
111
112 /* set sample format conversion parameters */
115 else
118
121 else
123
126
137 } else {
140 }
141
142 #ifdef DEBUG
143 {
149 else
156 else
163 else
170 }
171 #endif
172 } else
174
175 /* allocate buffers */
179 "in_buffer");
183 }
184 }
188 "resample_out_buffer");
192 }
193 }
200 }
201 }
203 1024);
207 }
208
209 /* setup contexts */
218 }
219 }
224 else
233 }
234 }
240 }
241 }
247 }
248 }
249
250 return 0;
251
254 return ret;
255 }
256
258 {
260 }
261
263 {
274
276 }
277
279 {
280 if (!*avr)
281 return;
285 }
286
289 {
290 int ret;
291
294 if (converted) {
295 /* if there are any samples in the output FIFO or if the
296 user-supplied output buffer is not large enough for all samples,
297 we add to the output FIFO */
301 if (ret < 0)
302 return ret;
303 }
304
305 /* if the user specified an output buffer, read samples from the output
306 FIFO to the user output */
312 }
313 } else if (converted) {
314 /* copy directly to output if it is large enough or there is not any
315 data in the output FIFO */
321 if (ret < 0)
322 return ret;
325 }
327 return 0;
328 }
329
331 uint8_t **output,
int out_plane_size,
332 int out_samples,
334 int in_plane_size, int in_samples)
335 {
339 int ret, direct_output;
340
341 /* reset internal buffers */
346 }
351 }
356 }
357
359
360 /* initialize output_buffer with output data */
362 if (output) {
366 if (ret < 0)
367 return ret;
369 }
370
371 if (input) {
372 /* initialize input_buffer with input data */
376 if (ret < 0)
377 return ret;
378 current_buffer = &input_buffer;
379
382 /* in some rare cases we can copy input to output and upmix
383 directly in the output buffer */
388 if (ret < 0)
389 return ret;
390 current_buffer = &output_buffer;
392 (!direct_output || out_samples < in_samples)) {
393 /* if remapping channels during output copy, we may need to
394 * use an intermediate buffer in order to remap before adding
395 * samples to the output fifo */
399 if (ret < 0)
400 return ret;
403 /* if needed, copy or convert input to in_buffer, and downmix if
404 applicable */
408 if (ret < 0)
409 return ret;
412 current_buffer);
413 if (ret < 0)
414 return ret;
415 } else {
420 if (ret < 0)
421 return ret;
422 }
427 if (ret < 0)
428 return ret;
429 }
431 }
432 } else {
433 /* flush resampling buffer and/or output FIFO if input is NULL */
436 NULL);
437 current_buffer =
NULL;
438 }
439
442
444 resample_out = &output_buffer;
445 else
448 current_buffer ? current_buffer->
name :
"null",
451 current_buffer);
452 if (ret < 0)
453 return ret;
454
455 /* if resampling did not produce any samples, just return 0 */
458 return 0;
459 }
460
461 current_buffer = resample_out;
462 }
463
467 if (ret < 0)
468 return ret;
469 }
470
471 /* if we resampled or upmixed directly to output, return here */
472 if (current_buffer == &output_buffer) {
475 }
476
478 if (direct_output && out_samples >= current_buffer->
nb_samples) {
479 /* convert directly to output */
482 if (ret < 0)
483 return ret;
484
487 } else {
490 if (ret < 0)
491 return ret;
494 current_buffer);
495 if (ret < 0)
496 return ret;
498 }
499 }
500
502 current_buffer);
503 }
504
506 {
509 }
510
511 if (in) {
515 }
516
517 if (out) {
521 }
522
523 return 0;
524 }
525
528 {
529 int ret = 0;
530
531 if (in) {
536 }
537 }
538
539 if (out) {
544 }
545 }
546
547 return ret;
548 }
549
552 {
553 int ret;
555 int out_linesize = 0, in_linesize = 0;
556 int out_nb_samples = 0, in_nb_samples = 0;
557
558 if (out) {
562 }
563
564 if (in) {
568 }
569
571 out_nb_samples,
572 in_data, in_linesize,
573 in_nb_samples);
574
575 if (ret < 0) {
576 if (out)
578 return ret;
579 }
580
581 if (out)
583
584 return 0;
585 }
586
588 {
589 int samples;
591 if (!bytes_per_sample)
593
594 samples = out->
linesize[0] / bytes_per_sample;
596 return samples;
597 } else {
600 }
601 }
602
605 {
606 int ret, setup = 0;
607
610 return ret;
612 return ret;
613 setup = 1;
614 } else {
615 // return as is or reconfigure for input changes?
617 return ret;
618 }
619
620 if (out) {
624 if (setup)
626 return ret;
627 }
628 } else {
631 }
632 }
633
635 }
636
639 {
640 int in_channels, out_channels, i, o;
641
644
647
652 }
653
657 }
658
659 for (o = 0; o < out_channels; o++)
660 for (i = 0; i < in_channels; i++)
661 matrix[o * stride + i] = avr->
mix_matrix[o * in_channels + i];
662
663 return 0;
664 }
665
668 {
669 int in_channels, out_channels, i, o;
670
673
676
681 }
682
689
690 for (o = 0; o < out_channels; o++)
691 for (i = 0; i < in_channels; i++)
692 avr->
mix_matrix[o * in_channels + i] = matrix[o * stride + i];
693
694 return 0;
695 }
696
698 const int *channel_map)
699 {
701 int in_channels,
ch, i;
702
707 }
708
709 memset(info, 0, sizeof(*info));
711
712 for (ch = 0; ch < in_channels; ch++) {
713 if (channel_map[ch] >= in_channels) {
716 }
717 if (channel_map[ch] < 0) {
721 }
else if (info->
input_map[channel_map[ch]] >= 0) {
725 } else {
729 }
730 }
731 /* Fill-in unmapped input channels with unmapped output channels.
732 This is used when remapping during conversion from interleaved to
733 planar format. */
734 for (ch = 0, i = 0; ch < in_channels && i < in_channels; ch++, i++) {
735 while (ch < in_channels && info->input_map[ch] >= 0)
736 ch++;
737 while (i < in_channels && info->channel_map[i] >= 0)
738 i++;
739 if (ch >= in_channels || i >= in_channels)
740 break;
742 }
743
745 return 0;
746 }
747
749 {
751 }
752
754 {
756
762 }
763
765
766 if (samples > INT_MAX)
768
769 return samples;
770 }
771
773 {
774 if (!output)
777 }
778
780 {
782 }
783
785 {
786 #define LICENSE_PREFIX "libavresample license: "
788 }
789
791 {
792 return FFMPEG_CONFIGURATION;
793 }
const char * avresample_license(void)
int in_channels
number of input channels
int avresample_set_matrix(AVAudioResampleContext *avr, const double *matrix, int stride)
AudioConvert * ac_in
input sample format conversion context
const char * name
name for debug logging
AVAudioFifo * av_audio_fifo_alloc(enum AVSampleFormat sample_fmt, int channels, int nb_samples)
Allocate an AVAudioFifo.
int av_audio_fifo_read(AVAudioFifo *af, void **data, int nb_samples)
Read data from an AVAudioFifo.
int ff_audio_data_realloc(AudioData *a, int nb_samples)
Reallocate AudioData.
This structure describes decoded (raw) audio or video data.
int avresample_open(AVAudioResampleContext *avr)
#define LIBAVRESAMPLE_VERSION_INT
int avresample_read(AVAudioResampleContext *avr, uint8_t **output, int nb_samples)
int input_map[AVRESAMPLE_MAX_CHANNELS]
dest index of each input channel
AudioData * out_buffer
buffer for converted output
Audio buffer used for intermediate storage between conversion phases.
attribute_deprecated int avresample_get_delay(AVAudioResampleContext *avr)
static int config_changed(AVAudioResampleContext *avr, AVFrame *out, AVFrame *in)
Memory handling functions.
int ff_audio_data_add_to_fifo(AVAudioFifo *af, AudioData *a, int offset, int nb_samples)
Add samples in AudioData to an AVAudioFifo.
int do_zero
zeroing needed
AudioData * ff_audio_data_alloc(int channels, int nb_samples, enum AVSampleFormat sample_fmt, const char *name)
Allocate AudioData.
static int convert_frame(AVAudioResampleContext *avr, AVFrame *out, AVFrame *in)
double * mix_matrix
mix matrix only used if avresample_set_matrix() is called before avresample_open() ...
uint64_t out_channel_layout
output channel layout
void av_audio_fifo_free(AVAudioFifo *af)
Free an AVAudioFifo.
int attribute_align_arg avresample_convert(AVAudioResampleContext *avr, uint8_t **output, int out_plane_size, int out_samples, uint8_t *const *input, int in_plane_size, int in_samples)
int ff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in)
Convert audio data from one sample format to another.
int ff_audio_mix(AudioMix *am, AudioData *src)
Apply channel mixing to audio data using the current mixing matrix.
int avresample_convert_frame(AVAudioResampleContext *avr, AVFrame *out, AVFrame *in)
int channel_zero[AVRESAMPLE_MAX_CHANNELS]
dest index to zero
int nb_samples
current number of samples
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
AudioData * in_buffer
buffer for converted input
int allocated_channels
allocated channel count
int out_channels
number of output channels
#define AV_LOG_TRACE
Extremely verbose debugging, useful for libav* development.
static int handle_buffered_output(AVAudioResampleContext *avr, AudioData *output, AudioData *converted)
int ff_audio_mix_set_matrix(AudioMix *am, const double *matrix, int stride)
Set the current mixing matrix.
int ff_sample_fmt_is_planar(enum AVSampleFormat sample_fmt, int channels)
enum AVSampleFormat av_get_planar_sample_fmt(enum AVSampleFormat sample_fmt)
Get the planar alternative form of the given sample format.
int avresample_get_out_samples(AVAudioResampleContext *avr, int in_nb_samples)
AudioConvert * ff_audio_convert_alloc(AVAudioResampleContext *avr, enum AVSampleFormat out_fmt, enum AVSampleFormat in_fmt, int channels, int sample_rate, int apply_map)
Allocate and initialize AudioConvert context for sample format conversion.
void avresample_close(AVAudioResampleContext *avr)
AudioMix * am
channel mixing context
int av_sample_fmt_is_planar(enum AVSampleFormat sample_fmt)
Check if the sample format is planar.
int ff_audio_data_set_channels(AudioData *a, int channels)
int ff_audio_resample(ResampleContext *c, AudioData *dst, AudioData *src)
Resample audio data.
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
int out_convert_needed
output sample format conversion is needed
int ff_audio_data_init(AudioData *a, uint8_t *const *src, int plane_size, int channels, int nb_samples, enum AVSampleFormat sample_fmt, int read_only, const char *name)
Initialize AudioData using a given source.
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
int avresample_config(AVAudioResampleContext *avr, AVFrame *out, AVFrame *in)
void ff_audio_convert_free(AudioConvert **ac)
Free AudioConvert.
const char * av_get_sample_fmt_name(enum AVSampleFormat sample_fmt)
Return the name of sample_fmt, or NULL if sample_fmt is not recognized.
AudioConvert * ac_out
output sample format conversion context
int ff_audio_data_read_from_fifo(AVAudioFifo *af, AudioData *a, int nb_samples)
Read samples from an AVAudioFifo to AudioData.
int avresample_available(AVAudioResampleContext *avr)
reference-counted frame API
int channel_copy[AVRESAMPLE_MAX_CHANNELS]
dest index to copy from
uint64_t channel_layout
Channel layout of the audio data.
int upmix_needed
upmixing is needed
ResampleContext * resample
resampling context
int avresample_get_matrix(AVAudioResampleContext *avr, double *matrix, int stride)
int av_audio_fifo_size(AVAudioFifo *af)
Get the current number of samples in the AVAudioFifo available for reading.
enum RemapPoint remap_point
const char * avresample_configuration(void)
unsigned avresample_version(void)
#define AVERROR_INPUT_CHANGED
Input changed between calls. Reconfiguration is required. (can be OR-ed with AVERROR_OUTPUT_CHANGED) ...
ChannelMapInfo ch_map_info
uint64_t in_channel_layout
input channel layout
int64_t av_rescale_rnd(int64_t a, int64_t b, int64_t c, enum AVRounding rnd)
Rescale a 64-bit integer with specified rounding.
static void error(const char *err)
int in_sample_rate
input sample rate
int format
format of the frame, -1 if unknown or unset Values correspond to enum AVPixelFormat for video frames...
int avresample_is_open(AVAudioResampleContext *avr)
void ff_audio_resample_free(ResampleContext **c)
Free a ResampleContext.
AVSampleFormat
Audio sample formats.
AVAudioFifo * out_fifo
FIFO for output samples.
int linesize[AV_NUM_DATA_POINTERS]
For video, size in bytes of each picture line.
int avresample_set_channel_mapping(AVAudioResampleContext *avr, const int *channel_map)
#define AVRESAMPLE_MAX_CHANNELS
enum AVSampleFormat internal_sample_fmt
internal sample format
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
int force_resampling
force resampling
void ff_audio_mix_free(AudioMix **am_p)
Free an AudioMix context.
int in_copy_needed
input data copy is needed
int sample_rate
Sample rate of the audio data.
ResampleContext * ff_audio_resample_init(AVAudioResampleContext *avr)
Allocate and initialize a ResampleContext.
enum AVSampleFormat in_sample_fmt
input sample format
int in_convert_needed
input sample format conversion is needed
int av_frame_get_buffer(AVFrame *frame, int align)
Allocate new buffer(s) for audio or video data.
int channel_map[AVRESAMPLE_MAX_CHANNELS]
source index of each output channel, -1 if not remapped
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
#define AVERROR_OUTPUT_CHANGED
Output changed between calls. Reconfiguration is required. (can be OR-ed with AVERROR_INPUT_CHANGED) ...
enum AVSampleFormat out_sample_fmt
output sample format
int av_audio_fifo_drain(AVAudioFifo *af, int nb_samples)
Drain data from an AVAudioFifo.
int ff_audio_data_copy(AudioData *dst, AudioData *src, ChannelMapInfo *map)
Copy data from one AudioData to another.
GLint GLenum GLboolean GLsizei stride
void av_opt_free(void *obj)
Free all allocated objects in obj.
common internal and external API header
int resample_channels
number of channels used for resampling
AudioData * resample_out_buffer
buffer for output from resampler
int resample_needed
resampling is needed
AudioMix * ff_audio_mix_alloc(AVAudioResampleContext *avr)
Allocate and initialize an AudioMix context.
void avresample_free(AVAudioResampleContext **avr)
int allocated_samples
number of samples the buffer can hold
int out_sample_rate
output sample rate
void ff_audio_data_free(AudioData **a)
Free AudioData.
int downmix_needed
downmixing is needed
static int available_samples(AVFrame *out)
uint8_t ** extended_data
pointers to the data planes/channels.
int nb_samples
number of audio samples (per channel) described by this frame
int mixing_needed
either upmixing or downmixing is needed
int ff_audio_mix_get_matrix(AudioMix *am, double *matrix, int stride)
Get the current mixing matrix.
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