annotate libaf/af_lavcresample.c @ 14022:803be9a78854

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