]> git.sesse.net Git - ffmpeg/blob - libswresample/rematrix.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libswresample / rematrix.c
1 /*
2  * Copyright (C) 2011 Michael Niedermayer (michaelni@gmx.at)
3  *
4  * This file is part of libswresample
5  *
6  * libswresample is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * libswresample is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with libswresample; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19  */
20
21 #include "swresample_internal.h"
22 #include "libavutil/audioconvert.h"
23 #include "libavutil/avassert.h"
24
25 #define ONE (1.0)
26 #define R(x) x
27 #define SAMPLE float
28 #define COEFF float
29 #define RENAME(x) x ## _float
30 #include "rematrix_template.c"
31 #undef SAMPLE
32 #undef RENAME
33 #undef R
34 #undef ONE
35 #undef COEFF
36
37 #define ONE (-32768)
38 #define R(x) (((x) + 16384)>>15)
39 #define SAMPLE int16_t
40 #define COEFF int
41 #define RENAME(x) x ## _s16
42 #include "rematrix_template.c"
43
44
45 #define FRONT_LEFT             0
46 #define FRONT_RIGHT            1
47 #define FRONT_CENTER           2
48 #define LOW_FREQUENCY          3
49 #define BACK_LEFT              4
50 #define BACK_RIGHT             5
51 #define FRONT_LEFT_OF_CENTER   6
52 #define FRONT_RIGHT_OF_CENTER  7
53 #define BACK_CENTER            8
54 #define SIDE_LEFT              9
55 #define SIDE_RIGHT             10
56 #define TOP_CENTER             11
57 #define TOP_FRONT_LEFT         12
58 #define TOP_FRONT_CENTER       13
59 #define TOP_FRONT_RIGHT        14
60 #define TOP_BACK_LEFT          15
61 #define TOP_BACK_CENTER        16
62 #define TOP_BACK_RIGHT         17
63
64 int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride)
65 {
66     int nb_in, nb_out, in, out;
67
68     if (!s || s->in_convert) // s needs to be allocated but not initialized
69         return AVERROR(EINVAL);
70     memset(s->matrix, 0, sizeof(s->matrix));
71     nb_in  = av_get_channel_layout_nb_channels(s->in_ch_layout);
72     nb_out = av_get_channel_layout_nb_channels(s->out_ch_layout);
73     for (out = 0; out < nb_out; out++) {
74         for (in = 0; in < nb_in; in++)
75             s->matrix[out][in] = matrix[in];
76         matrix += stride;
77     }
78     s->rematrix_custom = 1;
79     return 0;
80 }
81
82 static int even(int64_t layout){
83     if(!layout) return 1;
84     if(layout&(layout-1)) return 1;
85     return 0;
86 }
87
88 static int sane_layout(int64_t layout){
89     if(!(layout & AV_CH_LAYOUT_SURROUND)) // at least 1 front speaker
90         return 0;
91     if(!even(layout & (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT))) // no asymetric front
92         return 0;
93     if(!even(layout & (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)))   // no asymetric side
94         return 0;
95     if(!even(layout & (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)))
96         return 0;
97     if(!even(layout & (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)))
98         return 0;
99     if(av_get_channel_layout_nb_channels(layout) >= SWR_CH_MAX)
100         return 0;
101
102     return 1;
103 }
104
105 static int auto_matrix(SwrContext *s)
106 {
107     int i, j, out_i;
108     double matrix[64][64]={{0}};
109     int64_t unaccounted= s->in_ch_layout & ~s->out_ch_layout;
110     double maxcoef=0;
111
112     memset(s->matrix, 0, sizeof(s->matrix));
113     for(i=0; i<64; i++){
114         if(s->in_ch_layout & s->out_ch_layout & (1LL<<i))
115             matrix[i][i]= 1.0;
116     }
117
118     if(!sane_layout(s->in_ch_layout)){
119         av_log(s, AV_LOG_ERROR, "Input channel layout isnt supported\n");
120         return AVERROR(EINVAL);
121     }
122     if(!sane_layout(s->out_ch_layout)){
123         av_log(s, AV_LOG_ERROR, "Output channel layout isnt supported\n");
124         return AVERROR(EINVAL);
125     }
126
127 //FIXME implement dolby surround
128 //FIXME implement full ac3
129
130
131     if(unaccounted & AV_CH_FRONT_CENTER){
132         if((s->out_ch_layout & AV_CH_LAYOUT_STEREO) == AV_CH_LAYOUT_STEREO){
133             matrix[ FRONT_LEFT][FRONT_CENTER]+= M_SQRT1_2;
134             matrix[FRONT_RIGHT][FRONT_CENTER]+= M_SQRT1_2;
135         }else
136             av_assert0(0);
137     }
138     if(unaccounted & AV_CH_LAYOUT_STEREO){
139         if(s->out_ch_layout & AV_CH_FRONT_CENTER){
140             matrix[FRONT_CENTER][ FRONT_LEFT]+= M_SQRT1_2;
141             matrix[FRONT_CENTER][FRONT_RIGHT]+= M_SQRT1_2;
142             if(s->in_ch_layout & AV_CH_FRONT_CENTER)
143                 matrix[FRONT_CENTER][ FRONT_CENTER] = s->clev*sqrt(2);
144         }else
145             av_assert0(0);
146     }
147
148     if(unaccounted & AV_CH_BACK_CENTER){
149         if(s->out_ch_layout & AV_CH_BACK_LEFT){
150             matrix[ BACK_LEFT][BACK_CENTER]+= M_SQRT1_2;
151             matrix[BACK_RIGHT][BACK_CENTER]+= M_SQRT1_2;
152         }else if(s->out_ch_layout & AV_CH_SIDE_LEFT){
153             matrix[ SIDE_LEFT][BACK_CENTER]+= M_SQRT1_2;
154             matrix[SIDE_RIGHT][BACK_CENTER]+= M_SQRT1_2;
155         }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
156             matrix[ FRONT_LEFT][BACK_CENTER]+= s->slev*M_SQRT1_2;
157             matrix[FRONT_RIGHT][BACK_CENTER]+= s->slev*M_SQRT1_2;
158         }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
159             matrix[ FRONT_CENTER][BACK_CENTER]+= s->slev*M_SQRT1_2;
160         }else
161             av_assert0(0);
162     }
163     if(unaccounted & AV_CH_BACK_LEFT){
164         if(s->out_ch_layout & AV_CH_BACK_CENTER){
165             matrix[BACK_CENTER][ BACK_LEFT]+= M_SQRT1_2;
166             matrix[BACK_CENTER][BACK_RIGHT]+= M_SQRT1_2;
167         }else if(s->out_ch_layout & AV_CH_SIDE_LEFT){
168             if(s->in_ch_layout & AV_CH_SIDE_LEFT){
169                 matrix[ SIDE_LEFT][ BACK_LEFT]+= M_SQRT1_2;
170                 matrix[SIDE_RIGHT][BACK_RIGHT]+= M_SQRT1_2;
171             }else{
172             matrix[ SIDE_LEFT][ BACK_LEFT]+= 1.0;
173             matrix[SIDE_RIGHT][BACK_RIGHT]+= 1.0;
174             }
175         }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
176             matrix[ FRONT_LEFT][ BACK_LEFT]+= s->slev;
177             matrix[FRONT_RIGHT][BACK_RIGHT]+= s->slev;
178         }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
179             matrix[ FRONT_CENTER][BACK_LEFT ]+= s->slev*M_SQRT1_2;
180             matrix[ FRONT_CENTER][BACK_RIGHT]+= s->slev*M_SQRT1_2;
181         }else
182             av_assert0(0);
183     }
184
185     if(unaccounted & AV_CH_SIDE_LEFT){
186         if(s->out_ch_layout & AV_CH_BACK_LEFT){
187             matrix[ BACK_LEFT][ SIDE_LEFT]+= 1.0;
188             matrix[BACK_RIGHT][SIDE_RIGHT]+= 1.0;
189         }else if(s->out_ch_layout & AV_CH_BACK_CENTER){
190             matrix[BACK_CENTER][ SIDE_LEFT]+= M_SQRT1_2;
191             matrix[BACK_CENTER][SIDE_RIGHT]+= M_SQRT1_2;
192         }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
193             matrix[ FRONT_LEFT][ SIDE_LEFT]+= s->slev;
194             matrix[FRONT_RIGHT][SIDE_RIGHT]+= s->slev;
195         }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
196             matrix[ FRONT_CENTER][SIDE_LEFT ]+= s->slev*M_SQRT1_2;
197             matrix[ FRONT_CENTER][SIDE_RIGHT]+= s->slev*M_SQRT1_2;
198         }else
199             av_assert0(0);
200     }
201
202     if(unaccounted & AV_CH_FRONT_LEFT_OF_CENTER){
203         if(s->out_ch_layout & AV_CH_FRONT_LEFT){
204             matrix[ FRONT_LEFT][ FRONT_LEFT_OF_CENTER]+= 1.0;
205             matrix[FRONT_RIGHT][FRONT_RIGHT_OF_CENTER]+= 1.0;
206         }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
207             matrix[ FRONT_CENTER][ FRONT_LEFT_OF_CENTER]+= M_SQRT1_2;
208             matrix[ FRONT_CENTER][FRONT_RIGHT_OF_CENTER]+= M_SQRT1_2;
209         }else
210             av_assert0(0);
211     }
212     for(out_i=i=0; i<64; i++){
213         double sum=0;
214         int in_i=0;
215         for(j=0; j<64; j++){
216             s->matrix[out_i][in_i]= matrix[i][j];
217             if(matrix[i][j]){
218                 sum += fabs(matrix[i][j]);
219             }
220             if(s->in_ch_layout & (1ULL<<j))
221                 in_i++;
222         }
223         maxcoef= FFMAX(maxcoef, sum);
224         if(s->out_ch_layout & (1ULL<<i))
225             out_i++;
226     }
227     if(s->rematrix_volume  < 0)
228         maxcoef = -s->rematrix_volume;
229
230     if((   s->out_sample_fmt < AV_SAMPLE_FMT_FLT
231         || s->int_sample_fmt < AV_SAMPLE_FMT_FLT) && maxcoef > 1.0){
232         for(i=0; i<SWR_CH_MAX; i++)
233             for(j=0; j<SWR_CH_MAX; j++){
234                 s->matrix[i][j] /= maxcoef;
235             }
236     }
237
238     if(s->rematrix_volume > 0){
239         for(i=0; i<SWR_CH_MAX; i++)
240             for(j=0; j<SWR_CH_MAX; j++){
241                 s->matrix[i][j] *= s->rematrix_volume;
242             }
243     }
244
245     for(i=0; i<av_get_channel_layout_nb_channels(s->out_ch_layout); i++){
246         for(j=0; j<av_get_channel_layout_nb_channels(s->in_ch_layout); j++){
247             av_log(NULL, AV_LOG_DEBUG, "%f ", s->matrix[i][j]);
248         }
249         av_log(NULL, AV_LOG_DEBUG, "\n");
250     }
251     return 0;
252 }
253
254 int swri_rematrix_init(SwrContext *s){
255     int i, j;
256
257     if (!s->rematrix_custom) {
258         int r = auto_matrix(s);
259         if (r)
260             return r;
261     }
262     //FIXME quantize for integeres
263     for (i = 0; i < SWR_CH_MAX; i++) {
264         int ch_in=0;
265         for (j = 0; j < SWR_CH_MAX; j++) {
266             s->matrix32[i][j]= lrintf(s->matrix[i][j] * 32768);
267             if(s->matrix[i][j])
268                 s->matrix_ch[i][++ch_in]= j;
269         }
270         s->matrix_ch[i][0]= ch_in;
271     }
272     return 0;
273 }
274
275 int swri_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy){
276     int out_i, in_i, i, j;
277
278     av_assert0(out->ch_count == av_get_channel_layout_nb_channels(s->out_ch_layout));
279     av_assert0(in ->ch_count == av_get_channel_layout_nb_channels(s-> in_ch_layout));
280
281     for(out_i=0; out_i<out->ch_count; out_i++){
282         switch(s->matrix_ch[out_i][0]){
283         case 0:
284             memset(out->ch[out_i], 0, len * (s->int_sample_fmt == AV_SAMPLE_FMT_FLT ? sizeof(float) : sizeof(int16_t)));
285             break;
286         case 1:
287             in_i= s->matrix_ch[out_i][1];
288             if(mustcopy || s->matrix[out_i][in_i]!=1.0){
289                 if(s->int_sample_fmt == AV_SAMPLE_FMT_FLT){
290                     copy_float((float  *)out->ch[out_i], (const float  *)in->ch[in_i], s->matrix  [out_i][in_i], len);
291                 }else
292                     copy_s16  ((int16_t*)out->ch[out_i], (const int16_t*)in->ch[in_i], s->matrix32[out_i][in_i], len);
293             }else{
294                 out->ch[out_i]= in->ch[in_i];
295             }
296             break;
297         case 2:
298             if(s->int_sample_fmt == AV_SAMPLE_FMT_FLT){
299                 sum2_float((float  *)out->ch[out_i], (const float  *)in->ch[ s->matrix_ch[out_i][1] ],           (const float  *)in->ch[ s->matrix_ch[out_i][2] ],
300                                  s->matrix[out_i][ s->matrix_ch[out_i][1] ], s->matrix[out_i][ s->matrix_ch[out_i][2] ],
301                            len);
302             }else{
303                 sum2_s16  ((int16_t*)out->ch[out_i], (const int16_t*)in->ch[ s->matrix_ch[out_i][1] ],           (const int16_t*)in->ch[ s->matrix_ch[out_i][2] ],
304                                  s->matrix32[out_i][ s->matrix_ch[out_i][1] ], s->matrix32[out_i][ s->matrix_ch[out_i][2] ],
305                            len);
306             }
307             break;
308         default:
309             if(s->int_sample_fmt == AV_SAMPLE_FMT_FLT){
310                 for(i=0; i<len; i++){
311                     float v=0;
312                     for(j=0; j<s->matrix_ch[out_i][0]; j++){
313                         in_i= s->matrix_ch[out_i][1+j];
314                         v+= ((float*)in->ch[in_i])[i] * s->matrix[out_i][in_i];
315                     }
316                     ((float*)out->ch[out_i])[i]= v;
317                 }
318             }else{
319                 for(i=0; i<len; i++){
320                     int v=0;
321                     for(j=0; j<s->matrix_ch[out_i][0]; j++){
322                         in_i= s->matrix_ch[out_i][1+j];
323                         v+= ((int16_t*)in->ch[in_i])[i] * s->matrix32[out_i][in_i];
324                     }
325                     ((int16_t*)out->ch[out_i])[i]= (v + 16384)>>15;
326                 }
327             }
328         }
329     }
330     return 0;
331 }