Commit b5875b91 authored by Michael Niedermayer's avatar Michael Niedermayer

Add libswresample.

Similar to libswscale this does resampling and format convertion, just for audio
instead of video.
changing sampling rate, sample formats, channel layouts and sample packing all
in one with a very simple public interface.
Signed-off-by: 's avatarMichael Niedermayer <michaelni@gmx.at>
parent 53e37840
......@@ -33,6 +33,7 @@ FFLIBS-$(CONFIG_AVFILTER) += avfilter
FFLIBS-$(CONFIG_AVFORMAT) += avformat
FFLIBS-$(CONFIG_AVCODEC) += avcodec
FFLIBS-$(CONFIG_POSTPROC) += postproc
FFLIBS-$(CONFIG_SWRESAMPLE)+= swresample
FFLIBS-$(CONFIG_SWSCALE) += swscale
FFLIBS := avutil
......
......@@ -20,7 +20,7 @@ $(foreach VAR,$(SILENT),$(eval override $(VAR) = @$($(VAR))))
$(eval INSTALL = @$(call ECHO,INSTALL,$$(^:$(SRC_DIR)/%=%)); $(INSTALL))
endif
ALLFFLIBS = avcodec avdevice avfilter avformat avutil postproc swscale
ALLFFLIBS = avcodec avdevice avfilter avformat avutil postproc swscale swresample
# NASM requires -I path terminated with /
IFLAGS := -I. -I$(SRC_PATH)/
......
......@@ -89,6 +89,7 @@ Configuration options:
--disable-avdevice disable libavdevice build
--disable-avcodec disable libavcodec build
--disable-avformat disable libavformat build
--disable-swresample disable libswresample build
--disable-swscale disable libswscale build
--disable-postproc disable libpostproc build
--disable-avfilter disable video filter support [no]
......@@ -1037,6 +1038,7 @@ CONFIG_LIST="
small
sram
static
swresample
swscale
swscale_alpha
thumb
......@@ -1603,7 +1605,7 @@ avformat_deps="avcodec"
postproc_deps="gpl"
# programs
ffmpeg_deps="avcodec avformat swscale"
ffmpeg_deps="avcodec avformat swscale swresample"
ffmpeg_select="buffer_filter buffersink_filter"
avconv_deps="avcodec avformat swscale"
avconv_select="buffer_filter"
......@@ -1766,6 +1768,7 @@ enable postproc
enable protocols
enable static
enable stripping
enable swresample
enable swscale
enable swscale_alpha
......@@ -3143,7 +3146,7 @@ enabled extra_warnings && check_cflags -Winline
# add some linker flags
check_ldflags -Wl,--warn-common
check_ldflags -Wl,-rpath-link=libpostproc:libswscale:libavfilter:libavdevice:libavformat:libavcodec:libavutil
check_ldflags -Wl,-rpath-link=libpostproc:libswresample:libswscale:libavfilter:libavdevice:libavformat:libavcodec:libavutil
test_ldflags -Wl,-Bsymbolic && append SHFLAGS -Wl,-Bsymbolic
echo "X{};" > $TMPV
......
......@@ -45,6 +45,7 @@
#include "libavutil/avstring.h"
#include "libavutil/libm.h"
#include "libavformat/os_support.h"
#include "libswresample/swresample.h"
#include "libavformat/ffm.h" // not public API
......@@ -229,15 +230,14 @@ typedef struct OutputStream {
/* audio only */
int audio_resample;
ReSampleContext *resample; /* for audio resampling */
int resample_sample_fmt;
int resample_channels;
int resample_sample_rate;
int reformat_pair;
AVAudioConvert *reformat_ctx;
AVFifoBuffer *fifo; /* for compression: one audio fifo per codec */
FILE *logfile;
struct SwrContext *swr;
#if CONFIG_AVFILTER
AVFilterContext *output_video_filter;
AVFilterContext *input_video_filter;
......@@ -843,14 +843,15 @@ need_realloc:
exit_program(1);
}
if (enc->channels != dec->channels)
if (enc->channels != dec->channels
|| enc->sample_fmt != dec->sample_fmt)
ost->audio_resample = 1;
resample_changed = ost->resample_sample_fmt != dec->sample_fmt ||
ost->resample_channels != dec->channels ||
ost->resample_sample_rate != dec->sample_rate;
if ((ost->audio_resample && !ost->resample) || resample_changed) {
if ((ost->audio_resample && !ost->swr) || resample_changed) {
if (resample_changed) {
av_log(NULL, AV_LOG_INFO, "Input stream #%d.%d frame changed from rate:%d fmt:%s ch:%d to rate:%d fmt:%s ch:%d\n",
ist->file_index, ist->st->index,
......@@ -859,24 +860,29 @@ need_realloc:
ost->resample_sample_fmt = dec->sample_fmt;
ost->resample_channels = dec->channels;
ost->resample_sample_rate = dec->sample_rate;
if (ost->resample)
audio_resample_close(ost->resample);
swr_free(&ost->swr);
}
/* if audio_sync_method is >1 the resampler is needed for audio drift compensation */
if (audio_sync_method <= 1 &&
ost->resample_sample_fmt == enc->sample_fmt &&
ost->resample_channels == enc->channels &&
ost->resample_sample_rate == enc->sample_rate) {
ost->resample = NULL;
//ost->swr = NULL;
ost->audio_resample = 0;
} else {
if (dec->sample_fmt != AV_SAMPLE_FMT_S16)
fprintf(stderr, "Warning, using s16 intermediate sample format for resampling\n");
ost->resample = av_audio_resample_init(enc->channels, dec->channels,
enc->sample_rate, dec->sample_rate,
enc->sample_fmt, dec->sample_fmt,
16, 10, 0, 0.8);
if (!ost->resample) {
ost->swr = swr_alloc2(ost->swr,
enc->channel_layout, enc->sample_fmt, enc->sample_rate,
dec->channel_layout, dec->sample_fmt, dec->sample_rate,
0, NULL);
av_set_int(ost->swr, "ich", dec->channels);
av_set_int(ost->swr, "och", enc->channels);
if(audio_sync_method>1) av_set_int(ost->swr, "flags", SWR_FLAG_RESAMPLE);
if(ost->swr && swr_init(ost->swr) < 0){
fprintf(stderr, "swr_init() failed\n");
swr_free(&ost->swr);
}
if (!ost->swr) {
fprintf(stderr, "Can not resample %d channels @ %d Hz to %d channels @ %d Hz\n",
dec->channels, dec->sample_rate,
enc->channels, enc->sample_rate);
......@@ -885,21 +891,7 @@ need_realloc:
}
}
#define MAKE_SFMT_PAIR(a,b) ((a)+AV_SAMPLE_FMT_NB*(b))
if (!ost->audio_resample && dec->sample_fmt!=enc->sample_fmt &&
MAKE_SFMT_PAIR(enc->sample_fmt,dec->sample_fmt)!=ost->reformat_pair) {
if (ost->reformat_ctx)
av_audio_convert_free(ost->reformat_ctx);
ost->reformat_ctx = av_audio_convert_alloc(enc->sample_fmt, 1,
dec->sample_fmt, 1, NULL, 0);
if (!ost->reformat_ctx) {
fprintf(stderr, "Cannot convert %s sample format to %s sample format\n",
av_get_sample_fmt_name(dec->sample_fmt),
av_get_sample_fmt_name(enc->sample_fmt));
exit_program(1);
}
ost->reformat_pair=MAKE_SFMT_PAIR(enc->sample_fmt,dec->sample_fmt);
}
av_assert0(ost->audio_resample || dec->sample_fmt==enc->sample_fmt);
if(audio_sync_method){
double delta = get_sync_ipts(ost) * enc->sample_rate - ost->sync_opts
......@@ -941,7 +933,7 @@ need_realloc:
if(verbose > 2)
fprintf(stderr, "compensating audio timestamp drift:%f compensation:%d in:%d\n", delta, comp, enc->sample_rate);
// fprintf(stderr, "drift:%f len:%d opts:%"PRId64" ipts:%"PRId64" fifo:%d\n", delta, -1, ost->sync_opts, (int64_t)(get_sync_ipts(ost) * enc->sample_rate), av_fifo_size(ost->fifo)/(ost->st->codec->channels * 2));
av_resample_compensate(*(struct AVResampleContext**)ost->resample, comp, enc->sample_rate);
swr_compensate(ost->swr, comp, enc->sample_rate);
}
}
}else
......@@ -950,30 +942,15 @@ need_realloc:
if (ost->audio_resample) {
buftmp = audio_buf;
size_out = audio_resample(ost->resample,
(short *)buftmp, (short *)buf,
size / (dec->channels * isize));
size_out = swr_convert(ost->swr, ( uint8_t*[]){buftmp}, audio_buf_size / (enc->channels * osize),
(const uint8_t*[]){buf }, size / (dec->channels * isize));
size_out = size_out * enc->channels * osize;
} else {
buftmp = buf;
size_out = size;
}
if (!ost->audio_resample && dec->sample_fmt!=enc->sample_fmt) {
const void *ibuf[6]= {buftmp};
void *obuf[6]= {audio_buf};
int istride[6]= {isize};
int ostride[6]= {osize};
int len= size_out/istride[0];
if (av_audio_convert(ost->reformat_ctx, obuf, ostride, ibuf, istride, len)<0) {
printf("av_audio_convert() failed\n");
if (exit_on_error)
exit_program(1);
return;
}
buftmp = audio_buf;
size_out = len*osize;
}
av_assert0(ost->audio_resample || dec->sample_fmt==enc->sample_fmt);
/* now encode as many frames as possible */
if (enc->frame_size > 1) {
......@@ -2133,7 +2110,6 @@ static int transcode_init(OutputFile *output_files, int nb_output_files,
if (!ost->fifo) {
return AVERROR(ENOMEM);
}
ost->reformat_pair = MAKE_SFMT_PAIR(AV_SAMPLE_FMT_NONE,AV_SAMPLE_FMT_NONE);
if (!codec->sample_rate) {
codec->sample_rate = icodec->sample_rate;
}
......@@ -2149,6 +2125,8 @@ static int transcode_init(OutputFile *output_files, int nb_output_files,
if (av_get_channel_layout_nb_channels(codec->channel_layout) != codec->channels)
codec->channel_layout = 0;
ost->audio_resample = codec->sample_rate != icodec->sample_rate || audio_sync_method > 1;
ost->audio_resample |= codec->sample_fmt != icodec->sample_fmt
|| codec->channel_layout != icodec->channel_layout;
icodec->request_channels = codec->channels;
ist->decoding_needed = 1;
ost->encoding_needed = 1;
......@@ -2679,10 +2657,7 @@ static int transcode(OutputFile *output_files, int nb_output_files,
av_free(ost->forced_kf_pts);
if (ost->video_resample)
sws_freeContext(ost->img_resample_ctx);
if (ost->resample)
audio_resample_close(ost->resample);
if (ost->reformat_ctx)
av_audio_convert_free(ost->reformat_ctx);
swr_free(&ost->swr);
av_dict_free(&ost->opts);
}
}
......
include $(SUBDIR)../config.mak
NAME = swresample
FFLIBS = avutil
HEADERS = swresample.h
OBJS = swresample.o audioconvert.o resample2.o rematrix.o
TESTPROGS = swresample_test
include $(SUBDIR)../subdir.mak
/*
* audio conversion
* Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
/**
* @file
* audio conversion
* @author Michael Niedermayer <michaelni@gmx.at>
*/
#include "libavutil/avstring.h"
#include "libavutil/avassert.h"
#include "libavutil/libm.h"
#include "libavutil/samplefmt.h"
#include "audioconvert.h"
struct AVAudioConvert {
int channels;
int fmt_pair;
};
AVAudioConvert *swr_audio_convert_alloc(enum AVSampleFormat out_fmt,
enum AVSampleFormat in_fmt,
int channels, int flags)
{
AVAudioConvert *ctx;
ctx = av_malloc(sizeof(AVAudioConvert));
if (!ctx)
return NULL;
ctx->channels = channels;
ctx->fmt_pair = out_fmt + AV_SAMPLE_FMT_NB*in_fmt;
return ctx;
}
void swr_audio_convert_free(AVAudioConvert **ctx)
{
av_freep(ctx);
}
int swr_audio_convert(AVAudioConvert *ctx, AudioData *out, AudioData*in, int len)
{
int ch;
av_assert0(ctx->channels == out->ch_count);
//FIXME optimize common cases
for(ch=0; ch<ctx->channels; ch++){
const int is= (in ->planar ? 1 : in->ch_count) * in->bps;
const int os= (out->planar ? 1 :out->ch_count) *out->bps;
const uint8_t *pi= in ->ch[ch];
uint8_t *po= out->ch[ch];
uint8_t *end= po + os*len;
if(!po)
continue;
#define CONV(ofmt, otype, ifmt, expr)\
if(ctx->fmt_pair == ofmt + AV_SAMPLE_FMT_NB*ifmt){\
do{\
*(otype*)po = expr; pi += is; po += os;\
}while(po < end);\
}
//FIXME put things below under ifdefs so we do not waste space for cases no codec will need
//FIXME rounding ?
CONV(AV_SAMPLE_FMT_U8 , uint8_t, AV_SAMPLE_FMT_U8 , *(const uint8_t*)pi)
else CONV(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)<<8)
else CONV(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)<<24)
else CONV(AV_SAMPLE_FMT_FLT, float , AV_SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)*(1.0 / (1<<7)))
else CONV(AV_SAMPLE_FMT_DBL, double , AV_SAMPLE_FMT_U8 , (*(const uint8_t*)pi - 0x80)*(1.0 / (1<<7)))
else CONV(AV_SAMPLE_FMT_U8 , uint8_t, AV_SAMPLE_FMT_S16, (*(const int16_t*)pi>>8) + 0x80)
else CONV(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_S16, *(const int16_t*)pi)
else CONV(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_S16, *(const int16_t*)pi<<16)
else CONV(AV_SAMPLE_FMT_FLT, float , AV_SAMPLE_FMT_S16, *(const int16_t*)pi*(1.0 / (1<<15)))
else CONV(AV_SAMPLE_FMT_DBL, double , AV_SAMPLE_FMT_S16, *(const int16_t*)pi*(1.0 / (1<<15)))
else CONV(AV_SAMPLE_FMT_U8 , uint8_t, AV_SAMPLE_FMT_S32, (*(const int32_t*)pi>>24) + 0x80)
else CONV(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_S32, *(const int32_t*)pi>>16)
else CONV(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_S32, *(const int32_t*)pi)
else CONV(AV_SAMPLE_FMT_FLT, float , AV_SAMPLE_FMT_S32, *(const int32_t*)pi*(1.0 / (1U<<31)))
else CONV(AV_SAMPLE_FMT_DBL, double , AV_SAMPLE_FMT_S32, *(const int32_t*)pi*(1.0 / (1U<<31)))
else CONV(AV_SAMPLE_FMT_U8 , uint8_t, AV_SAMPLE_FMT_FLT, av_clip_uint8( lrintf(*(const float*)pi * (1<<7)) + 0x80))
else CONV(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, av_clip_int16( lrintf(*(const float*)pi * (1<<15))))
else CONV(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, av_clipl_int32(llrintf(*(const float*)pi * (1U<<31))))
else CONV(AV_SAMPLE_FMT_FLT, float , AV_SAMPLE_FMT_FLT, *(const float*)pi)
else CONV(AV_SAMPLE_FMT_DBL, double , AV_SAMPLE_FMT_FLT, *(const float*)pi)
else CONV(AV_SAMPLE_FMT_U8 , uint8_t, AV_SAMPLE_FMT_DBL, av_clip_uint8( lrint(*(const double*)pi * (1<<7)) + 0x80))
else CONV(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, av_clip_int16( lrint(*(const double*)pi * (1<<15))))
else CONV(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, av_clipl_int32(llrint(*(const double*)pi * (1U<<31))))
else CONV(AV_SAMPLE_FMT_FLT, float , AV_SAMPLE_FMT_DBL, *(const double*)pi)
else CONV(AV_SAMPLE_FMT_DBL, double , AV_SAMPLE_FMT_DBL, *(const double*)pi)
else return -1;
}
return 0;
}
/*
* audio conversion
* Copyright (c) 2006 Michael Niedermayer <michaelni@gmx.at>
* Copyright (c) 2008 Peter Ross
*
* This file is part of FFmpeg.
*
* FFmpeg is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* FFmpeg is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with FFmpeg; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef SWR_AUDIOCONVERT_H
#define SWR_AUDIOCONVERT_H
/**
* @file
* Audio format conversion routines
*/
#include "swresample_internal.h"
#include "libavutil/cpu.h"
#include "libavutil/audioconvert.h"
struct AVAudioConvert;
typedef struct AVAudioConvert AVAudioConvert;
/**
* Create an audio sample format converter context
* @param out_fmt Output sample format
* @param in_fmt Input sample format
* @param channels Number of channels
* @param flags See AV_CPU_FLAG_xx
* @return NULL on error
*/
AVAudioConvert *swr_audio_convert_alloc(enum AVSampleFormat out_fmt,
enum AVSampleFormat in_fmt,
int channels, int flags);
/**
* Free audio sample format converter context.
* and set the pointer to NULL
*/
void swr_audio_convert_free(AVAudioConvert **ctx);
/**
* Convert between audio sample formats
* @param[in] out array of output buffers for each channel. set to NULL to ignore processing of the given channel.
* @param[in] in array of input buffers for each channel
* @param len length of audio frame size (measured in samples)
*/
int swr_audio_convert(AVAudioConvert *ctx, AudioData *out, AudioData *in, int len);
#endif /* AVCODEC_AUDIOCONVERT_H */
This diff is collapsed.
/*
* Copyright (C) 2011 Michael Niedermayer (michaelni@gmx.at)
*
* This file is part of libswresample
*
* libswresample is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* libswresample is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with libswresample; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
static void RENAME(sum2)(SAMPLE *out, const SAMPLE *in1, const SAMPLE *in2, float coeff1, float coeff2, int len){
int i;
for(i=0; i<len; i++)
out[i] = coeff1*in1[i] + coeff2*in2[i]; //FIXME better int16
}
static void RENAME(copy)(SAMPLE *out, const SAMPLE *in, float coeff, int len){
if(coeff == 1.0){
memcpy(out, in, sizeof(SAMPLE)*len);
}else{
int i;
for(i=0; i<len; i++)
out[i] = coeff*in[i]; //FIXME better int16
}
}
This diff is collapsed.
This diff is collapsed.
/*
* Copyright (C) 2011 Michael Niedermayer (michaelni@gmx.at)
*
* This file is part of libswresample
*
* libswresample is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* libswresample is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with libswresample; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef SWR_H
#define SWR_H
#include <inttypes.h>
#include "libavutil/samplefmt.h"
#define LIBSWRESAMPLE_VERSION_MAJOR 0
#define LIBSWRESAMPLE_VERSION_MINOR 0
#define LIBSWRESAMPLE_VERSION_MICRO 0
#define SWR_CH_MAX 16
#define SWR_FLAG_RESAMPLE 1///< Force resampling even if equal sample rate
//TODO use int resample ?
//long term TODO can we enable this dynamically?
struct SwrContext;
/**
* Allocate SwrContext.
* @see swr_init(),swr_free()
* @return NULL on error
*/
struct SwrContext *swr_alloc(void);
/**
* Initialize context after user parameters have been set.
* @return negativo n error
*/
int swr_init(struct SwrContext *s);
/**
* Allocate SwrContext.
* @see swr_init(),swr_free()
* @return NULL on error
*/
struct SwrContext *swr_alloc2(struct SwrContext *s, int64_t out_ch_layout, enum AVSampleFormat out_sample_fmt, int out_sample_rate,
int64_t in_ch_layout, enum AVSampleFormat in_sample_fmt, int in_sample_rate,
int log_offset, void *log_ctx);
/**
* Free the given SwrContext.
* And set the pointer to NULL
*/
void swr_free(struct SwrContext **s);
/**
* Convert audio.
* @param in_count Number of input samples available in one channel.
* @param out_count Amount of space available for output in samples per channel.
* @return number of samples output per channel
*/
int swr_convert(struct SwrContext *s, uint8_t *out[SWR_CH_MAX], int out_count,
const uint8_t *in [SWR_CH_MAX], int in_count);
void swr_compensate(struct SwrContext *s, int sample_delta, int compensation_distance);
#endif
/*
* Copyright (C) 2011 Michael Niedermayer (michaelni@gmx.at)
*
* This file is part of libswresample
*
* libswresample is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* libswresample is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with libswresample; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef SWR_INTERNAL_H
#define SWR_INTERNAL_H
#include "swresample.h"
typedef struct AudioData{
uint8_t *ch[SWR_CH_MAX];
uint8_t *data;
int ch_count;
int bps;
int count;
int planar;
} AudioData;
typedef struct SwrContext { //FIXME find unused fields
AVClass *av_class;
int log_level_offset;
void *log_ctx;
enum AVSampleFormat in_sample_fmt;
enum AVSampleFormat int_sample_fmt; ///<AV_SAMPLE_FMT_FLT OR AV_SAMPLE_FMT_S16
enum AVSampleFormat out_sample_fmt;
int64_t in_ch_layout;
int64_t out_ch_layout;
int in_sample_rate;
int out_sample_rate;
int flags;
float slev, clev;
//below are private
int int_bps;
int resample_first;
int rematrix; ///< flag to indicate if rematrixing is used
AudioData in, postin, midbuf, preout, out, in_buffer;
int in_buffer_index;
int in_buffer_count;
int resample_in_constraint;
struct AVAudioConvert *in_convert;
struct AVAudioConvert *out_convert;
struct AVResampleContext *resample;
float matrix[SWR_CH_MAX][SWR_CH_MAX];
uint8_t matrix_ch[SWR_CH_MAX][SWR_CH_MAX+1];
//TODO callbacks for asm optims
}SwrContext;
struct AVResampleContext *swr_resample_init(struct AVResampleContext *, int out_rate, int in_rate, int filter_size, int phase_shift, int linear, double cutoff);
void swr_resample_free(struct AVResampleContext **c);
int swr_multiple_resample(struct AVResampleContext *c, AudioData *dst, int dst_size, AudioData *src, int src_size, int *consumed);
void swr_resample_compensate(struct AVResampleContext *c, int sample_delta, int compensation_distance);
int swr_resample(struct AVResampleContext *c, short *dst, short *src, int *consumed, int src_size, int dst_size, int update_ctx);
int swr_rematrix_init(SwrContext *s);
int swr_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy);
#endif
/*
* Copyright (C) 2011 Michael Niedermayer (michaelni@gmx.at)
*
* This file is part of libswresample
*
* libswresample is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* libswresample is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with libswresample; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include "libavutil/avassert.h"
#include "libavutil/common.h"
#include "libavutil/audioconvert.h"
#include "swresample.h"
#undef fprintf
#define SAMPLES 1000
#define ASSERT_LEVEL 2
static double get(const void *p, int index, enum AVSampleFormat f){
switch(f){
case AV_SAMPLE_FMT_U8 : return ((const uint8_t*)p)[index]/255.0*2-1.0;
case AV_SAMPLE_FMT_S16: return ((const int16_t*)p)[index]/32767.0;
case AV_SAMPLE_FMT_S32: return ((const int32_t*)p)[index]/2147483647.0;
case AV_SAMPLE_FMT_FLT: return ((const float *)p)[index];
case AV_SAMPLE_FMT_DBL: return ((const double *)p)[index];
default: av_assert2(0);
}
}
static void set(void *p, int index, enum AVSampleFormat f, double v){
switch(f){
case AV_SAMPLE_FMT_U8 : ((uint8_t*)p)[index]= (v+1.0)*255.0/2; break;
case AV_SAMPLE_FMT_S16: ((int16_t*)p)[index]= v*32767; break;
case AV_SAMPLE_FMT_S32: ((int32_t*)p)[index]= v*2147483647; break;
case AV_SAMPLE_FMT_FLT: ((float *)p)[index]= v; break;
case AV_SAMPLE_FMT_DBL: ((double *)p)[index]= v; break;
default: av_assert2(0);
}
}
uint64_t layouts[]={
AV_CH_LAYOUT_MONO ,
AV_CH_LAYOUT_STEREO ,
AV_CH_LAYOUT_2_1 ,
AV_CH_LAYOUT_SURROUND ,
AV_CH_LAYOUT_4POINT0 ,
AV_CH_LAYOUT_2_2 ,
AV_CH_LAYOUT_QUAD ,
AV_CH_LAYOUT_5POINT0 ,
AV_CH_LAYOUT_5POINT1 ,
AV_CH_LAYOUT_5POINT0_BACK ,
AV_CH_LAYOUT_5POINT1_BACK ,
AV_CH_LAYOUT_7POINT0 ,
AV_CH_LAYOUT_7POINT1 ,
AV_CH_LAYOUT_7POINT1_WIDE ,
0
};
int main(int argc, char **argv){
int in_sample_rate, out_sample_rate, ch ,i, in_ch_layout_index, out_ch_layout_index, osr;
uint64_t in_ch_layout, out_ch_layout;
enum AVSampleFormat in_sample_fmt, out_sample_fmt;
int sample_rates[]={8000,11025,16000,22050,32000};
uint8_t array_in[SAMPLES*8*8];
uint8_t array_mid[SAMPLES*8*8*3];
uint8_t array_out[SAMPLES*8*8+100];
struct SwrContext * forw_ctx= NULL;
struct SwrContext *backw_ctx= NULL;
in_sample_rate=16000;
for(osr=0; osr<5; osr++){
out_sample_rate= sample_rates[osr];
for(in_sample_fmt= AV_SAMPLE_FMT_U8; in_sample_fmt<=AV_SAMPLE_FMT_DBL; in_sample_fmt++){
for(out_sample_fmt= AV_SAMPLE_FMT_U8; out_sample_fmt<=AV_SAMPLE_FMT_DBL; out_sample_fmt++){
for(in_ch_layout_index=0; layouts[in_ch_layout_index]; in_ch_layout_index++){
in_ch_layout= layouts[in_ch_layout_index];
int in_ch_count= av_get_channel_layout_nb_channels(in_ch_layout);
for(out_ch_layout_index=0; layouts[out_ch_layout_index]; out_ch_layout_index++){
int out_count, mid_count;
out_ch_layout= layouts[out_ch_layout_index];
int out_ch_count= av_get_channel_layout_nb_channels(out_ch_layout);
fprintf(stderr, "ch %d->%d, rate:%5d->%5d, fmt:%s->%s",
in_ch_count, out_ch_count,
in_sample_rate, out_sample_rate,
av_get_sample_fmt_name(in_sample_fmt), av_get_sample_fmt_name(out_sample_fmt));
forw_ctx = swr_alloc2(forw_ctx, out_ch_layout, out_sample_fmt, out_sample_rate,
in_ch_layout, in_sample_fmt, in_sample_rate, 0, 0);
backw_ctx = swr_alloc2(backw_ctx,in_ch_layout, in_sample_fmt, in_sample_rate,
out_ch_layout, out_sample_fmt, out_sample_rate, 0, 0);
if(swr_init( forw_ctx) < 0)
fprintf(stderr, "swr_init(->) failed\n");
if(swr_init(backw_ctx) < 0)
fprintf(stderr, "swr_init(<-) failed\n");
if(!forw_ctx)
fprintf(stderr, "Failed to init forw_cts\n");
if(!backw_ctx)
fprintf(stderr, "Failed to init backw_ctx\n");
//FIXME test planar
for(ch=0; ch<in_ch_count; ch++){
for(i=0; i<SAMPLES; i++)
set(array_in, ch + i*in_ch_count, in_sample_fmt, sin(i*i*3/SAMPLES));
}
mid_count= swr_convert(forw_ctx, ( uint8_t*[]){array_mid}, 3*SAMPLES,
(const uint8_t*[]){array_in }, SAMPLES);
out_count= swr_convert(backw_ctx,( uint8_t*[]){array_out}, 3*SAMPLES,
(const uint8_t*[]){array_mid}, mid_count);
for(ch=0; ch<in_ch_count; ch++){
double sse, x, maxdiff=0;
double sum_a= 0;
double sum_b= 0;
double sum_aa= 0;
double sum_bb= 0;
double sum_ab= 0;
for(i=0; i<SAMPLES; i++){
double a= get(array_in , ch + i*in_ch_count, in_sample_fmt);
double b= get(array_out, ch + i*in_ch_count, in_sample_fmt);
sum_a += a;
sum_b += b;
sum_aa+= a*a;
sum_bb+= b*b;
sum_ab+= a*b;
maxdiff= FFMAX(maxdiff, FFABS(a-b));
}
x = sum_ab/sum_bb;
sse= sum_aa + sum_bb*x*x - 2*x*sum_ab;
fprintf(stderr, "[%f %f %f] len:%5d\n", sqrt(sse/SAMPLES), x, maxdiff, out_count);
}
fprintf(stderr, "\n");
}
}
}
}
}
return 0;
}
fd090ddf05cc3401cc75c4a5ace1d05a *./tests/data/acodec/g726.wav
a76fc937faac62c5de057cd69191732a *./tests/data/acodec/g726.wav
24052 ./tests/data/acodec/g726.wav
74abea06027375111eeac1b2f8c7d3af *./tests/data/g726.acodec.out.wav
stddev: 8554.55 PSNR: 17.69 MAXDIFF:29353 bytes: 95984/ 1058400
124de13e6cb5af64ea8758aa49feb7fc *./tests/data/g726.acodec.out.wav
stddev: 8554.23 PSNR: 17.69 MAXDIFF:29353 bytes: 95984/ 1058400
......@@ -66,7 +66,7 @@ stddev: 0.00 PSNR:999.99 MAXDIFF: 0 bytes: 1058400/ 1058400
529256 ./tests/data/acodec/pcm_zork.wav
864c8c866ac25642c29a13b122c70709 *./tests/data/pcm.acodec.out.wav
stddev: 633.11 PSNR: 40.30 MAXDIFF:32768 bytes: 1058400/ 1058400
8168a5c1343553ef027541830f2cb879 *./tests/data/acodec/pcm_s24daud.302
1b75d5198ae789ab3c48f7024e08f4a9 *./tests/data/acodec/pcm_s24daud.302
10368730 ./tests/data/acodec/pcm_s24daud.302
f552afadfdfcd6348a07095da6382de5 *./tests/data/pcm.acodec.out.wav
stddev: 9416.28 PSNR: 16.85 MAXDIFF:42744 bytes: 6911796/ 1058400
4708f86529c594e29404603c64bb208c *./tests/data/pcm.acodec.out.wav
stddev: 8967.92 PSNR: 17.28 MAXDIFF:42548 bytes: 6911796/ 1058400
188f804bd2d10cd436c8a7b111bdcd2a *./tests/data/lavf/lavf.dv
6e716216d5f9e3819db8eb8796de9129 *./tests/data/lavf/lavf.dv
3600000 ./tests/data/lavf/lavf.dv
./tests/data/lavf/lavf.dv CRC=0x02c0af30
./tests/data/lavf/lavf.dv CRC=0x92d1e3f0
b3174e2db508564c1cce0b5e3c1bc1bd *./tests/data/lavf/lavf.mxf_d10
8eb67301f72f2b5860fafab422b920ad *./tests/data/lavf/lavf.mxf_d10
5330989 ./tests/data/lavf/lavf.mxf_d10
./tests/data/lavf/lavf.mxf_d10 CRC=0xc3f4f92e
./tests/data/lavf/lavf.mxf_d10 CRC=0x96c02dfd
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment