/* * 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(uint8_t *a[], int ch, int index, int ch_count, enum AVSampleFormat f){ const uint8_t *p; if(av_sample_fmt_is_planar(f)){ f= av_get_alt_sample_fmt(f, 0); p= a[ch]; }else{ p= a[0]; index= ch + index*ch_count; } 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_assert0(0); } } static void set(uint8_t *a[], int ch, int index, int ch_count, enum AVSampleFormat f, double v){ uint8_t *p; if(av_sample_fmt_is_planar(f)){ f= av_get_alt_sample_fmt(f, 0); p= a[ch]; }else{ p= a[0]; index= ch + index*ch_count; } 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 }; static void setup_array(uint8_t *out[SWR_CH_MAX], uint8_t *in, enum AVSampleFormat format, int samples){ if(av_sample_fmt_is_planar(format)){ int i; int plane_size= av_get_bytes_per_sample(format&0xFF)*samples; format&=0xFF; for(i=0; i%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_alloc_set_opts(forw_ctx, out_ch_layout, av_get_alt_sample_fmt(out_sample_fmt, 1), out_sample_rate, in_ch_layout, av_get_alt_sample_fmt( in_sample_fmt, 1), in_sample_rate, 0, 0); backw_ctx = swr_alloc_set_opts(backw_ctx, in_ch_layout, in_sample_fmt, in_sample_rate, out_ch_layout, av_get_alt_sample_fmt(out_sample_fmt, 1), 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 setup_array(ain , array_in , av_get_alt_sample_fmt( in_sample_fmt, 1), SAMPLES); setup_array(amid, array_mid, av_get_alt_sample_fmt(out_sample_fmt, 1), 3*SAMPLES); setup_array(aout, array_out, in_sample_fmt , SAMPLES); for(ch=0; ch