[pulseaudio-commits] r1714 - in /branches/lennart/src/pulsecore/ffmpeg: ./ avcodec.h dsputil.h resample2.c
svnmailer-noreply at 0pointer.de
svnmailer-noreply at 0pointer.de
Thu Aug 23 17:23:23 PDT 2007
Author: lennart
Date: Fri Aug 24 02:23:22 2007
New Revision: 1714
URL: http://0pointer.de/cgi-bin/viewcvs.cgi?rev=3D1714&root=3Dpulseaudio&vi=
ew=3Drev
Log:
Copy resampler from ffmpeg into our sources
Added:
branches/lennart/src/pulsecore/ffmpeg/
branches/lennart/src/pulsecore/ffmpeg/avcodec.h
branches/lennart/src/pulsecore/ffmpeg/dsputil.h
branches/lennart/src/pulsecore/ffmpeg/resample2.c
Added: branches/lennart/src/pulsecore/ffmpeg/avcodec.h
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/branches/lennart/src/pulsecore/=
ffmpeg/avcodec.h?rev=3D1714&root=3Dpulseaudio&view=3Dauto
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- branches/lennart/src/pulsecore/ffmpeg/avcodec.h (added)
+++ branches/lennart/src/pulsecore/ffmpeg/avcodec.h Fri Aug 24 02:23:22 2007
@@ -1,0 +1,71 @@
+/*
+ * copyright (c) 2001 Fabrice Bellard
+ *
+ * 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-130=
1 USA
+ */
+
+#ifndef AVCODEC_H
+#define AVCODEC_H
+
+/* Just a heavily bastardized version of the original file from
+ * ffmpeg, just enough to get resample2.c to compile without
+ * modification -- Lennart */
+
+#include <sys/types.h>
+#include <inttypes.h>
+#include <math.h>
+#include <string.h>
+#include <stdlib.h>
+#include <assert.h>
+
+#define av_mallocz(l) calloc(1, (l))
+#define av_malloc(l) malloc(l)
+#define av_realloc(p,l) realloc((p),(l))
+#define av_free(p) free(p)
+
+static inline void av_freep(void *k) {
+ void **p =3D k;
+ =
+ if (p) {
+ free(*p);
+ *p =3D NULL;
+ }
+}
+
+static inline int av_clip(int a, int amin, int amax)
+{
+ if (a < amin) return amin;
+ else if (a > amax) return amax;
+ else return a;
+}
+
+#define av_log(a,b,c)
+
+#define FFABS(a) ((a) >=3D 0 ? (a) : (-(a)))
+#define FFSIGN(a) ((a) > 0 ? 1 : -1)
+
+#define FFMAX(a,b) ((a) > (b) ? (a) : (b))
+#define FFMIN(a,b) ((a) > (b) ? (b) : (a))
+
+struct AVResampleContext;
+struct AVResampleContext *av_resample_init(int out_rate, int in_rate, int =
filter_length, int log2_phase_count, int linear, double cutoff);
+int av_resample(struct AVResampleContext *c, short *dst, short *src, int *=
consumed, int src_size, int dst_size, int update_ctx);
+void av_resample_compensate(struct AVResampleContext *c, int sample_delta,=
int compensation_distance);
+void av_resample_close(struct AVResampleContext *c);
+void av_build_filter(int16_t *filter, double factor, int tap_count, int ph=
ase_count, int scale, int type);
+
+#endif /* AVCODEC_H */
Added: branches/lennart/src/pulsecore/ffmpeg/dsputil.h
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/branches/lennart/src/pulsecore/=
ffmpeg/dsputil.h?rev=3D1714&root=3Dpulseaudio&view=3Dauto
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- branches/lennart/src/pulsecore/ffmpeg/dsputil.h (added)
+++ branches/lennart/src/pulsecore/ffmpeg/dsputil.h Fri Aug 24 02:23:22 2007
@@ -1,0 +1,1 @@
+/* empty file, just here to allow us to compile an unmodified resampler2.c=
*/
Added: branches/lennart/src/pulsecore/ffmpeg/resample2.c
URL: http://0pointer.de/cgi-bin/viewcvs.cgi/branches/lennart/src/pulsecore/=
ffmpeg/resample2.c?rev=3D1714&root=3Dpulseaudio&view=3Dauto
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=3D=
=3D=3D=3D
--- branches/lennart/src/pulsecore/ffmpeg/resample2.c (added)
+++ branches/lennart/src/pulsecore/ffmpeg/resample2.c Fri Aug 24 02:23:22 2=
007
@@ -1,0 +1,324 @@
+/*
+ * audio resampling
+ * Copyright (c) 2004 Michael Niedermayer <michaelni at 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-130=
1 USA
+ */
+
+/**
+ * @file resample2.c
+ * audio resampling
+ * @author Michael Niedermayer <michaelni at gmx.at>
+ */
+
+#include "avcodec.h"
+#include "dsputil.h"
+
+#ifndef CONFIG_RESAMPLE_HP
+#define FILTER_SHIFT 15
+
+#define FELEM int16_t
+#define FELEM2 int32_t
+#define FELEML int64_t
+#define FELEM_MAX INT16_MAX
+#define FELEM_MIN INT16_MIN
+#define WINDOW_TYPE 9
+#elif !defined(CONFIG_RESAMPLE_AUDIOPHILE_KIDDY_MODE)
+#define FILTER_SHIFT 30
+
+#define FELEM int32_t
+#define FELEM2 int64_t
+#define FELEML int64_t
+#define FELEM_MAX INT32_MAX
+#define FELEM_MIN INT32_MIN
+#define WINDOW_TYPE 12
+#else
+#define FILTER_SHIFT 0
+
+#define FELEM double
+#define FELEM2 double
+#define FELEML double
+#define WINDOW_TYPE 24
+#endif
+
+
+typedef struct AVResampleContext{
+ FELEM *filter_bank;
+ int filter_length;
+ int ideal_dst_incr;
+ int dst_incr;
+ int index;
+ int frac;
+ int src_incr;
+ int compensation_distance;
+ int phase_shift;
+ int phase_mask;
+ int linear;
+}AVResampleContext;
+
+/**
+ * 0th order modified bessel function of the first kind.
+ */
+static double bessel(double x){
+ double v=3D1;
+ double t=3D1;
+ int i;
+
+ x=3D x*x/4;
+ for(i=3D1; i<50; i++){
+ t *=3D x/(i*i);
+ v +=3D t;
+ }
+ return v;
+}
+
+/**
+ * builds a polyphase filterbank.
+ * @param factor resampling factor
+ * @param scale wanted sum of coefficients for each filter
+ * @param type 0->cubic, 1->blackman nuttall windowed sinc, 2..16->kaiser =
windowed sinc beta=3D2..16
+ */
+void av_build_filter(FELEM *filter, double factor, int tap_count, int phas=
e_count, int scale, int type){
+ int ph, i;
+ double x, y, w, tab[tap_count];
+ const int center=3D (tap_count-1)/2;
+
+ /* if upsampling, only need to interpolate, no filter */
+ if (factor > 1.0)
+ factor =3D 1.0;
+
+ for(ph=3D0;ph<phase_count;ph++) {
+ double norm =3D 0;
+ for(i=3D0;i<tap_count;i++) {
+ x =3D M_PI * ((double)(i - center) - (double)ph / phase_count)=
* factor;
+ if (x =3D=3D 0) y =3D 1.0;
+ else y =3D sin(x) / x;
+ switch(type){
+ case 0:{
+ const float d=3D -0.5; //first order derivative =3D -0.5
+ x =3D fabs(((double)(i - center) - (double)ph / phase_coun=
t) * factor);
+ if(x<1.0) y=3D 1 - 3*x*x + 2*x*x*x + d*( -x*x +=
x*x*x);
+ else y=3D d*(-4 + 8*x - 5*x*x +=
x*x*x);
+ break;}
+ case 1:
+ w =3D 2.0*x / (factor*tap_count) + M_PI;
+ y *=3D 0.3635819 - 0.4891775 * cos(w) + 0.1365995 * cos(2*=
w) - 0.0106411 * cos(3*w);
+ break;
+ default:
+ w =3D 2.0*x / (factor*tap_count*M_PI);
+ y *=3D bessel(type*sqrt(FFMAX(1-w*w, 0)));
+ break;
+ }
+
+ tab[i] =3D y;
+ norm +=3D y;
+ }
+
+ /* normalize so that an uniform color remains the same */
+ for(i=3D0;i<tap_count;i++) {
+#ifdef CONFIG_RESAMPLE_AUDIOPHILE_KIDDY_MODE
+ filter[ph * tap_count + i] =3D tab[i] / norm;
+#else
+ filter[ph * tap_count + i] =3D av_clip(lrintf(tab[i] * scale /=
norm), FELEM_MIN, FELEM_MAX);
+#endif
+ }
+ }
+#if 0
+ {
+#define LEN 1024
+ int j,k;
+ double sine[LEN + tap_count];
+ double filtered[LEN];
+ double maxff=3D-2, minff=3D2, maxsf=3D-2, minsf=3D2;
+ for(i=3D0; i<LEN; i++){
+ double ss=3D0, sf=3D0, ff=3D0;
+ for(j=3D0; j<LEN+tap_count; j++)
+ sine[j]=3D cos(i*j*M_PI/LEN);
+ for(j=3D0; j<LEN; j++){
+ double sum=3D0;
+ ph=3D0;
+ for(k=3D0; k<tap_count; k++)
+ sum +=3D filter[ph * tap_count + k] * sine[k+j];
+ filtered[j]=3D sum / (1<<FILTER_SHIFT);
+ ss+=3D sine[j + center] * sine[j + center];
+ ff+=3D filtered[j] * filtered[j];
+ sf+=3D sine[j + center] * filtered[j];
+ }
+ ss=3D sqrt(2*ss/LEN);
+ ff=3D sqrt(2*ff/LEN);
+ sf=3D 2*sf/LEN;
+ maxff=3D FFMAX(maxff, ff);
+ minff=3D FFMIN(minff, ff);
+ maxsf=3D FFMAX(maxsf, sf);
+ minsf=3D FFMIN(minsf, sf);
+ if(i%11=3D=3D0){
+ av_log(NULL, AV_LOG_ERROR, "i:%4d ss:%f ff:%13.6e-%13.6e s=
f:%13.6e-%13.6e\n", i, ss, maxff, minff, maxsf, minsf);
+ minff=3Dminsf=3D 2;
+ maxff=3Dmaxsf=3D -2;
+ }
+ }
+ }
+#endif
+}
+
+/**
+ * Initializes an audio resampler.
+ * Note, if either rate is not an integer then simply scale both rates up =
so they are.
+ */
+AVResampleContext *av_resample_init(int out_rate, int in_rate, int filter_=
size, int phase_shift, int linear, double cutoff){
+ AVResampleContext *c=3D av_mallocz(sizeof(AVResampleContext));
+ double factor=3D FFMIN(out_rate * cutoff / in_rate, 1.0);
+ int phase_count=3D 1<<phase_shift;
+
+ c->phase_shift=3D phase_shift;
+ c->phase_mask=3D phase_count-1;
+ c->linear=3D linear;
+
+ c->filter_length=3D FFMAX((int)ceil(filter_size/factor), 1);
+ c->filter_bank=3D av_mallocz(c->filter_length*(phase_count+1)*sizeof(F=
ELEM));
+ av_build_filter(c->filter_bank, factor, c->filter_length, phase_count,=
1<<FILTER_SHIFT, WINDOW_TYPE);
+ memcpy(&c->filter_bank[c->filter_length*phase_count+1], c->filter_bank=
, (c->filter_length-1)*sizeof(FELEM));
+ c->filter_bank[c->filter_length*phase_count]=3D c->filter_bank[c->filt=
er_length - 1];
+
+ c->src_incr=3D out_rate;
+ c->ideal_dst_incr=3D c->dst_incr=3D in_rate * phase_count;
+ c->index=3D -phase_count*((c->filter_length-1)/2);
+
+ return c;
+}
+
+void av_resample_close(AVResampleContext *c){
+ av_freep(&c->filter_bank);
+ av_freep(&c);
+}
+
+/**
+ * Compensates samplerate/timestamp drift. The compensation is done by cha=
nging
+ * the resampler parameters, so no audible clicks or similar distortions o=
cur
+ * @param compensation_distance distance in output samples over which the =
compensation should be performed
+ * @param sample_delta number of output samples which should be output less
+ *
+ * example: av_resample_compensate(c, 10, 500)
+ * here instead of 510 samples only 500 samples would be output
+ *
+ * note, due to rounding the actual compensation might be slightly differe=
nt,
+ * especially if the compensation_distance is large and the in_rate used d=
uring init is small
+ */
+void av_resample_compensate(AVResampleContext *c, int sample_delta, int co=
mpensation_distance){
+// sample_delta +=3D (c->ideal_dst_incr - c->dst_incr)*(int64_t)c->comp=
ensation_distance / c->ideal_dst_incr;
+ c->compensation_distance=3D compensation_distance;
+ c->dst_incr =3D c->ideal_dst_incr - c->ideal_dst_incr * (int64_t)sampl=
e_delta / compensation_distance;
+}
+
+/**
+ * resamples.
+ * @param src an array of unconsumed samples
+ * @param consumed the number of samples of src which have been consumed a=
re returned here
+ * @param src_size the number of unconsumed samples available
+ * @param dst_size the amount of space in samples available in dst
+ * @param update_ctx if this is 0 then the context wont be modified, that =
way several channels can be resampled with the same context
+ * @return the number of samples written in dst or -1 if an error occured
+ */
+int av_resample(AVResampleContext *c, short *dst, short *src, int *consume=
d, int src_size, int dst_size, int update_ctx){
+ int dst_index, i;
+ int index=3D c->index;
+ int frac=3D c->frac;
+ int dst_incr_frac=3D c->dst_incr % c->src_incr;
+ int dst_incr=3D c->dst_incr / c->src_incr;
+ int compensation_distance=3D c->compensation_distance;
+
+ if(compensation_distance =3D=3D 0 && c->filter_length =3D=3D 1 && c->pha=
se_shift=3D=3D0){
+ int64_t index2=3D ((int64_t)index)<<32;
+ int64_t incr=3D (1LL<<32) * c->dst_incr / c->src_incr;
+ dst_size=3D FFMIN(dst_size, (src_size-1-index) * (int64_t)c->src_i=
ncr / c->dst_incr);
+
+ for(dst_index=3D0; dst_index < dst_size; dst_index++){
+ dst[dst_index] =3D src[index2>>32];
+ index2 +=3D incr;
+ }
+ frac +=3D dst_index * dst_incr_frac;
+ index +=3D dst_index * dst_incr;
+ index +=3D frac / c->src_incr;
+ frac %=3D c->src_incr;
+ }else{
+ for(dst_index=3D0; dst_index < dst_size; dst_index++){
+ FELEM *filter=3D c->filter_bank + c->filter_length*(index & c->pha=
se_mask);
+ int sample_index=3D index >> c->phase_shift;
+ FELEM2 val=3D0;
+
+ if(sample_index < 0){
+ for(i=3D0; i<c->filter_length; i++)
+ val +=3D src[FFABS(sample_index + i) % src_size] * filter[=
i];
+ }else if(sample_index + c->filter_length > src_size){
+ break;
+ }else if(c->linear){
+ FELEM2 v2=3D0;
+ for(i=3D0; i<c->filter_length; i++){
+ val +=3D src[sample_index + i] * (FELEM2)filter[i];
+ v2 +=3D src[sample_index + i] * (FELEM2)filter[i + c->fil=
ter_length];
+ }
+ val+=3D(v2-val)*(FELEML)frac / c->src_incr;
+ }else{
+ for(i=3D0; i<c->filter_length; i++){
+ val +=3D src[sample_index + i] * (FELEM2)filter[i];
+ }
+ }
+
+#ifdef CONFIG_RESAMPLE_AUDIOPHILE_KIDDY_MODE
+ dst[dst_index] =3D av_clip_int16(lrintf(val));
+#else
+ val =3D (val + (1<<(FILTER_SHIFT-1)))>>FILTER_SHIFT;
+ dst[dst_index] =3D (unsigned)(val + 32768) > 65535 ? (val>>31) ^ 3=
2767 : val;
+#endif
+
+ frac +=3D dst_incr_frac;
+ index +=3D dst_incr;
+ if(frac >=3D c->src_incr){
+ frac -=3D c->src_incr;
+ index++;
+ }
+
+ if(dst_index + 1 =3D=3D compensation_distance){
+ compensation_distance=3D 0;
+ dst_incr_frac=3D c->ideal_dst_incr % c->src_incr;
+ dst_incr=3D c->ideal_dst_incr / c->src_incr;
+ }
+ }
+ }
+ *consumed=3D FFMAX(index, 0) >> c->phase_shift;
+ if(index>=3D0) index &=3D c->phase_mask;
+
+ if(compensation_distance){
+ compensation_distance -=3D dst_index;
+ assert(compensation_distance > 0);
+ }
+ if(update_ctx){
+ c->frac=3D frac;
+ c->index=3D index;
+ c->dst_incr=3D dst_incr_frac + c->src_incr*dst_incr;
+ c->compensation_distance=3D compensation_distance;
+ }
+#if 0
+ if(update_ctx && !c->compensation_distance){
+#undef rand
+ av_resample_compensate(c, rand() % (8000*2) - 8000, 8000*2);
+av_log(NULL, AV_LOG_DEBUG, "%d %d %d\n", c->dst_incr, c->ideal_dst_incr, c=
->compensation_distance);
+ }
+#endif
+
+ return dst_index;
+}
More information about the pulseaudio-commits
mailing list