increasing sync tag to 1.15 after update by Diego
[mplayer/glamo.git] / libaf / af_lavcresample.c
blobc988aa671ae064dfbd85e9761b5ce3a6de83a277
1 // Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
2 // #inlcude <GPL_v2.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <inttypes.h>
9 #include "config.h"
10 #include "af.h"
12 #ifdef USE_LIBAVCODEC_SO
13 #include <ffmpeg/avcodec.h>
14 #include <ffmpeg/rational.h>
15 #else
16 #include "avcodec.h"
17 #include "rational.h"
18 #endif
20 #define CHANS 6
22 int64_t ff_gcd(int64_t a, int64_t b);
24 // Data for specific instances of this filter
25 typedef struct af_resample_s{
26 struct AVResampleContext *avrctx;
27 int16_t *in[CHANS];
28 int in_alloc;
29 int index;
31 int filter_length;
32 int linear;
33 int phase_shift;
34 double cutoff;
35 }af_resample_t;
38 // Initialization and runtime control
39 static int control(struct af_instance_s* af, int cmd, void* arg)
41 af_resample_t* s = (af_resample_t*)af->setup;
42 af_data_t *data= (af_data_t*)arg;
43 int out_rate, test_output_res; // helpers for checking input format
45 switch(cmd){
46 case AF_CONTROL_REINIT:
47 if((af->data->rate == data->rate) || (af->data->rate == 0))
48 return AF_DETACH;
50 af->data->nch = data->nch;
51 if (af->data->nch > CHANS) af->data->nch = CHANS;
52 af->data->format = AF_FORMAT_S16_NE;
53 af->data->bps = 2;
54 af->mul.n = af->data->rate;
55 af->mul.d = data->rate;
56 af_frac_cancel(&af->mul);
57 af->delay = 500*s->filter_length/(double)min(af->data->rate, data->rate);
59 if(s->avrctx) av_resample_close(s->avrctx);
60 s->avrctx= av_resample_init(af->mul.n, /*in_rate*/af->mul.d, s->filter_length, s->phase_shift, s->linear, s->cutoff);
62 // hack to make af_test_output ignore the samplerate change
63 out_rate = af->data->rate;
64 af->data->rate = data->rate;
65 test_output_res = af_test_output(af, (af_data_t*)arg);
66 af->data->rate = out_rate;
67 return test_output_res;
68 case AF_CONTROL_COMMAND_LINE:{
69 sscanf((char*)arg,"%d:%d:%d:%d:%lf", &af->data->rate, &s->filter_length, &s->linear, &s->phase_shift, &s->cutoff);
70 if(s->cutoff <= 0.0) s->cutoff= max(1.0 - 1.0/s->filter_length, 0.80);
71 return AF_OK;
73 case AF_CONTROL_RESAMPLE_RATE | AF_CONTROL_SET:
74 af->data->rate = *(int*)arg;
75 return AF_OK;
77 return AF_UNKNOWN;
80 // Deallocate memory
81 static void uninit(struct af_instance_s* af)
83 if(af->data)
84 free(af->data);
85 if(af->setup){
86 af_resample_t *s = af->setup;
87 if(s->avrctx) av_resample_close(s->avrctx);
88 free(s);
92 // Filter data through filter
93 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
95 af_resample_t *s = af->setup;
96 int i, j, consumed, ret;
97 int16_t *in = (int16_t*)data->audio;
98 int16_t *out;
99 int chans = data->nch;
100 int in_len = data->len/(2*chans);
101 int out_len = (in_len*af->mul.n) / af->mul.d + 10;
102 int16_t tmp[CHANS][out_len];
104 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
105 return NULL;
107 out= (int16_t*)af->data->audio;
109 out_len= min(out_len, af->data->len/(2*chans));
111 if(s->in_alloc < in_len + s->index){
112 s->in_alloc= in_len + s->index;
113 for(i=0; i<chans; i++){
114 s->in[i]= realloc(s->in[i], s->in_alloc*sizeof(int16_t)); //FIXME free this maybe ;)
118 if(chans==1){
119 memcpy(&s->in[0][s->index], in, in_len * sizeof(int16_t));
120 }else if(chans==2){
121 for(j=0; j<in_len; j++){
122 s->in[0][j + s->index]= *(in++);
123 s->in[1][j + s->index]= *(in++);
125 }else{
126 for(j=0; j<in_len; j++){
127 for(i=0; i<chans; i++){
128 s->in[i][j + s->index]= *(in++);
132 in_len += s->index;
134 for(i=0; i<chans; i++){
135 ret= av_resample(s->avrctx, tmp[i], s->in[i], &consumed, in_len, out_len, i+1 == chans);
137 out_len= ret;
139 s->index= in_len - consumed;
140 for(i=0; i<chans; i++){
141 memmove(s->in[i], s->in[i] + consumed, s->index*sizeof(int16_t));
144 if(chans==1){
145 memcpy(out, tmp[0], out_len*sizeof(int16_t));
146 }else if(chans==2){
147 for(j=0; j<out_len; j++){
148 *(out++)= tmp[0][j];
149 *(out++)= tmp[1][j];
151 }else{
152 for(j=0; j<out_len; j++){
153 for(i=0; i<chans; i++){
154 *(out++)= tmp[i][j];
159 data->audio = af->data->audio;
160 data->len = out_len*chans*2;
161 data->rate = af->data->rate;
162 return data;
165 static int open(af_instance_t* af){
166 af_resample_t *s = calloc(1,sizeof(af_resample_t));
167 af->control=control;
168 af->uninit=uninit;
169 af->play=play;
170 af->mul.n=1;
171 af->mul.d=1;
172 af->data=calloc(1,sizeof(af_data_t));
173 s->filter_length= 16;
174 s->cutoff= max(1.0 - 1.0/s->filter_length, 0.80);
175 s->phase_shift= 10;
176 // s->setup = RSMP_INT | FREQ_SLOPPY;
177 af->setup=s;
178 return AF_OK;
181 af_info_t af_info_lavcresample = {
182 "Sample frequency conversion using libavcodec",
183 "lavcresample",
184 "Michael Niedermayer",
186 AF_FLAGS_REENTRANT,
187 open