comparison libaf/af_lavcresample.c @ 13713:28bb0f15ac91

libavcodec resampling ... libaf doesnt seem to support planar audio, so we need to convert it :(
author michael
date Thu, 21 Oct 2004 03:32:31 +0000
parents
children a45e901cc870
comparison
equal deleted inserted replaced
13712:f6ef5a0ad7e4 13713:28bb0f15ac91
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
19 // Data for specific instances of this filter
20 typedef struct af_resample_s{
21 struct AVResampleContext *avrctx;
22 int16_t *in[CHANS];
23 int in_alloc;
24 int index;
25
26 int filter_length;
27 int linear;
28 int phase_shift;
29 }af_resample_t;
30
31
32 // Initialization and runtime control
33 static int control(struct af_instance_s* af, int cmd, void* arg)
34 {
35 af_resample_t* s = (af_resample_t*)af->setup;
36 af_data_t *data= (af_data_t*)arg;
37
38 switch(cmd){
39 case AF_CONTROL_REINIT:
40 if((af->data->rate == data->rate) || (af->data->rate == 0))
41 return AF_DETACH;
42
43 if(data->format != (AF_FORMAT_SI | AF_FORMAT_NE) || data->nch > CHANS)
44 return AF_ERROR;
45
46 af->data->nch = data->nch;
47 af->data->format = AF_FORMAT_SI | AF_FORMAT_NE;
48 af->data->bps = 2;
49 af->mul.n = af->data->rate;
50 af->mul.d = data->rate;
51 af->delay = 500*s->filter_length/(double)min(af->mul.n, af->mul.d);
52
53 if(s->avrctx) av_resample_close(s->avrctx);
54 s->avrctx= av_resample_init(af->mul.n, /*in_rate*/af->mul.d, s->filter_length, s->phase_shift, s->linear);
55
56 return AF_OK;
57 case AF_CONTROL_COMMAND_LINE:{
58 sscanf((char*)arg,"%d:%d:%d:%d", &af->data->rate, &s->filter_length, &s->linear, &s->phase_shift);
59 return AF_OK;
60 }
61 case AF_CONTROL_RESAMPLE_RATE | AF_CONTROL_SET:
62 af->data->rate = *(int*)arg;
63 return AF_OK;
64 }
65 return AF_UNKNOWN;
66 }
67
68 // Deallocate memory
69 static void uninit(struct af_instance_s* af)
70 {
71 if(af->data)
72 free(af->data);
73 if(af->setup){
74 af_resample_t *s = af->setup;
75 if(s->avrctx) av_resample_close(s->avrctx);
76 free(s);
77 }
78 }
79
80 // Filter data through filter
81 static af_data_t* play(struct af_instance_s* af, af_data_t* data)
82 {
83 af_resample_t *s = af->setup;
84 int i, j, consumed, ret;
85 int16_t *in = (int16_t*)data->audio;
86 int16_t *out;
87 int chans = data->nch;
88 int in_len = data->len/(2*chans);
89 int out_len = (in_len*af->mul.n) / af->mul.d + 10;
90 int16_t tmp[CHANS][out_len];
91
92 if(AF_OK != RESIZE_LOCAL_BUFFER(af,data))
93 return NULL;
94
95 out= (int16_t*)af->data->audio;
96
97 if(s->in_alloc < in_len + s->index){
98 s->in_alloc= in_len + s->index;
99 for(i=0; i<chans; i++){
100 s->in[i]= realloc(s->in[i], s->in_alloc*sizeof(int16_t)); //FIXME free this maybe ;)
101 }
102 }
103
104 for(j=0; j<in_len; j++){
105 for(i=0; i<chans; i++){
106 s->in[i][j + s->index]= *(in++);
107 }
108 }
109 in_len += s->index;
110
111 for(i=0; i<chans; i++){
112 ret= av_resample(s->avrctx, tmp[i], s->in[i], &consumed, in_len, out_len, i+1 == chans);
113 }
114 out_len= ret;
115
116 s->index= in_len - consumed;
117 for(i=0; i<chans; i++){
118 memmove(s->in[i], s->in[i] + consumed, s->index*sizeof(int16_t));
119 }
120
121 for(j=0; j<out_len; j++){
122 for(i=0; i<chans; i++){
123 *(out++)= tmp[i][j];
124 }
125 }
126
127 data->audio = af->data->audio;
128 data->len = out_len*chans*2;
129 data->rate = af->data->rate;
130 return data;
131 }
132
133 static int open(af_instance_t* af){
134 af->control=control;
135 af->uninit=uninit;
136 af->play=play;
137 af->mul.n=1;
138 af->mul.d=1;
139 af->data=calloc(1,sizeof(af_data_t));
140 af->setup=calloc(1,sizeof(af_resample_t));
141 ((af_resample_t*)af->setup)->filter_length= 16;
142 ((af_resample_t*)af->setup)->phase_shift= 10;
143 // ((af_resample_t*)af->setup)->setup = RSMP_INT | FREQ_SLOPPY;
144 return AF_OK;
145 }
146
147 af_info_t af_info_lavcresample = {
148 "Sample frequency conversion using libavcodec",
149 "lavcresample",
150 "Michael Niedermayer",
151 "",
152 AF_FLAGS_REENTRANT,
153 open
154 };
155 #endif