13713
|
1 // Copyright (c) 2004 Michael Niedermayer <michaelni@gmx.at>
|
|
2 // #inlcude <GPL_v2.h>
|
|
3
|
|
4 #include <stdio.h>
|
|
5 #include <stdlib.h>
|
|
6 #include <string.h>
|
|
7 #include <inttypes.h>
|
|
8
|
|
9 #include "../config.h"
|
|
10 #include "af.h"
|
|
11
|
|
12 #ifdef USE_LIBAVCODEC
|
|
13
|
|
14 #include "../libavcodec/avcodec.h"
|
|
15 #include "../libavcodec/rational.h"
|
|
16
|
|
17 #define CHANS 6
|
|
18
|
13730
|
19 int64_t ff_gcd(int64_t a, int64_t b);
|
|
20
|
13713
|
21 // Data for specific instances of this filter
|
|
22 typedef struct af_resample_s{
|
|
23 struct AVResampleContext *avrctx;
|
|
24 int16_t *in[CHANS];
|
|
25 int in_alloc;
|
|
26 int index;
|
|
27
|
|
28 int filter_length;
|
|
29 int linear;
|
|
30 int phase_shift;
|
13730
|
31 double cutoff;
|
13713
|
32 }af_resample_t;
|
|
33
|
|
34
|
|
35 // Initialization and runtime control
|
|
36 static int control(struct af_instance_s* af, int cmd, void* arg)
|
|
37 {
|
13730
|
38 int g;
|
13713
|
39 af_resample_t* s = (af_resample_t*)af->setup;
|
|
40 af_data_t *data= (af_data_t*)arg;
|
|
41
|
|
42 switch(cmd){
|
|
43 case AF_CONTROL_REINIT:
|
|
44 if((af->data->rate == data->rate) || (af->data->rate == 0))
|
|
45 return AF_DETACH;
|
|
46
|
|
47 if(data->format != (AF_FORMAT_SI | AF_FORMAT_NE) || data->nch > CHANS)
|
|
48 return AF_ERROR;
|
|
49
|
|
50 af->data->nch = data->nch;
|
|
51 af->data->format = AF_FORMAT_SI | AF_FORMAT_NE;
|
|
52 af->data->bps = 2;
|
13730
|
53 g= ff_gcd(af->data->rate, data->rate);
|
|
54 af->mul.n = af->data->rate/g;
|
|
55 af->mul.d = data->rate/g;
|
|
56 af->delay = 500*s->filter_length/(double)min(af->data->rate, data->rate);
|
13713
|
57
|
|
58 if(s->avrctx) av_resample_close(s->avrctx);
|
13730
|
59 s->avrctx= av_resample_init(af->mul.n, /*in_rate*/af->mul.d, s->filter_length, s->phase_shift, s->linear, s->cutoff);
|
13713
|
60
|
|
61 return AF_OK;
|
|
62 case AF_CONTROL_COMMAND_LINE:{
|
13730
|
63 sscanf((char*)arg,"%d:%d:%d:%d:%lf", &af->data->rate, &s->filter_length, &s->linear, &s->phase_shift, &s->cutoff);
|
|
64 if(s->cutoff <= 0.0) s->cutoff= max(1.0 - 1.0/s->filter_length, 0.80);
|
13713
|
65 return AF_OK;
|
|
66 }
|
|
67 case AF_CONTROL_RESAMPLE_RATE | AF_CONTROL_SET:
|
|
68 af->data->rate = *(int*)arg;
|
|
69 return AF_OK;
|
|
70 }
|
|
71 return AF_UNKNOWN;
|
|
72 }
|
|
73
|
|
74 // Deallocate memory
|
|
75 static void uninit(struct af_instance_s* af)
|
|
76 {
|
|
77 if(af->data)
|
|
78 free(af->data);
|
|
79 if(af->setup){
|
|
80 af_resample_t *s = af->setup;
|
|
81 if(s->avrctx) av_resample_close(s->avrctx);
|
|
82 free(s);
|
|
83 }
|
|
84 }
|
|
85
|
|
86 // Filter data through filter
|
|
87 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
|
|
88 {
|
|
89 af_resample_t *s = af->setup;
|
|
90 int i, j, consumed, ret;
|
|
91 int16_t *in = (int16_t*)data->audio;
|
|
92 int16_t *out;
|
|
93 int chans = data->nch;
|
|
94 int in_len = data->len/(2*chans);
|
|
95 int out_len = (in_len*af->mul.n) / af->mul.d + 10;
|
|
96 int16_t tmp[CHANS][out_len];
|
|
97
|
|
98 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
|
|
99 return NULL;
|
|
100
|
|
101 out= (int16_t*)af->data->audio;
|
13730
|
102
|
|
103 out_len= min(out_len, af->data->len/(2*chans));
|
|
104
|
13713
|
105 if(s->in_alloc < in_len + s->index){
|
|
106 s->in_alloc= in_len + s->index;
|
|
107 for(i=0; i<chans; i++){
|
|
108 s->in[i]= realloc(s->in[i], s->in_alloc*sizeof(int16_t)); //FIXME free this maybe ;)
|
|
109 }
|
|
110 }
|
|
111
|
|
112 for(j=0; j<in_len; j++){
|
|
113 for(i=0; i<chans; i++){
|
|
114 s->in[i][j + s->index]= *(in++);
|
|
115 }
|
|
116 }
|
|
117 in_len += s->index;
|
|
118
|
|
119 for(i=0; i<chans; i++){
|
|
120 ret= av_resample(s->avrctx, tmp[i], s->in[i], &consumed, in_len, out_len, i+1 == chans);
|
|
121 }
|
|
122 out_len= ret;
|
|
123
|
|
124 s->index= in_len - consumed;
|
|
125 for(i=0; i<chans; i++){
|
|
126 memmove(s->in[i], s->in[i] + consumed, s->index*sizeof(int16_t));
|
|
127 }
|
|
128
|
|
129 for(j=0; j<out_len; j++){
|
|
130 for(i=0; i<chans; i++){
|
|
131 *(out++)= tmp[i][j];
|
|
132 }
|
|
133 }
|
|
134
|
|
135 data->audio = af->data->audio;
|
|
136 data->len = out_len*chans*2;
|
|
137 data->rate = af->data->rate;
|
|
138 return data;
|
|
139 }
|
|
140
|
|
141 static int open(af_instance_t* af){
|
|
142 af->control=control;
|
|
143 af->uninit=uninit;
|
|
144 af->play=play;
|
|
145 af->mul.n=1;
|
|
146 af->mul.d=1;
|
|
147 af->data=calloc(1,sizeof(af_data_t));
|
|
148 af->setup=calloc(1,sizeof(af_resample_t));
|
|
149 ((af_resample_t*)af->setup)->filter_length= 16;
|
|
150 ((af_resample_t*)af->setup)->phase_shift= 10;
|
|
151 // ((af_resample_t*)af->setup)->setup = RSMP_INT | FREQ_SLOPPY;
|
|
152 return AF_OK;
|
|
153 }
|
|
154
|
|
155 af_info_t af_info_lavcresample = {
|
|
156 "Sample frequency conversion using libavcodec",
|
|
157 "lavcresample",
|
|
158 "Michael Niedermayer",
|
|
159 "",
|
|
160 AF_FLAGS_REENTRANT,
|
|
161 open
|
|
162 };
|
|
163 #endif
|