]> 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 static int even(int64_t layout){
65     if(!layout) return 1;
66     if(layout&(layout-1)) return 1;
67     return 0;
68 }
69
70 static int sane_layout(int64_t layout){
71     if(!(layout & AV_CH_LAYOUT_SURROUND)) // at least 1 front speaker
72         return 0;
73     if(!even(layout & (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT))) // no asymetric front
74         return 0;
75     if(!even(layout & (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)))   // no asymetric side
76         return 0;
77     if(!even(layout & (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)))
78         return 0;
79     if(!even(layout & (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)))
80         return 0;
81     if(av_get_channel_layout_nb_channels(layout) >= SWR_CH_MAX)
82         return 0;
83
84     return 1;
85 }
86
87 int swri_rematrix_init(SwrContext *s){
88     int i, j, out_i;
89     double matrix[64][64]={{0}};
90     int64_t unaccounted= s->in_ch_layout & ~s->out_ch_layout;
91     double maxcoef=0;
92
93     for(i=0; i<64; i++){
94         if(s->in_ch_layout & s->out_ch_layout & (1LL<<i))
95             matrix[i][i]= 1.0;
96     }
97
98     if(!sane_layout(s->in_ch_layout)){
99         av_log(s, AV_LOG_ERROR, "Input channel layout isnt supported\n");
100         return AVERROR(EINVAL);
101     }
102     if(!sane_layout(s->out_ch_layout)){
103         av_log(s, AV_LOG_ERROR, "Output channel layout isnt supported\n");
104         return AVERROR(EINVAL);
105     }
106
107 //FIXME implement dolby surround
108 //FIXME implement full ac3
109
110
111     if(unaccounted & AV_CH_FRONT_CENTER){
112         if((s->out_ch_layout & AV_CH_LAYOUT_STEREO) == AV_CH_LAYOUT_STEREO){
113             matrix[ FRONT_LEFT][FRONT_CENTER]+= M_SQRT1_2;
114             matrix[FRONT_RIGHT][FRONT_CENTER]+= M_SQRT1_2;
115         }else
116             av_assert0(0);
117     }
118     if(unaccounted & AV_CH_LAYOUT_STEREO){
119         if(s->out_ch_layout & AV_CH_FRONT_CENTER){
120             matrix[FRONT_CENTER][ FRONT_LEFT]+= M_SQRT1_2;
121             matrix[FRONT_CENTER][FRONT_RIGHT]+= M_SQRT1_2;
122             if(s->in_ch_layout & AV_CH_FRONT_CENTER)
123                 matrix[FRONT_CENTER][ FRONT_CENTER] = s->clev*sqrt(2);
124         }else
125             av_assert0(0);
126     }
127
128     if(unaccounted & AV_CH_BACK_CENTER){
129         if(s->out_ch_layout & AV_CH_BACK_LEFT){
130             matrix[ BACK_LEFT][BACK_CENTER]+= M_SQRT1_2;
131             matrix[BACK_RIGHT][BACK_CENTER]+= M_SQRT1_2;
132         }else if(s->out_ch_layout & AV_CH_SIDE_LEFT){
133             matrix[ SIDE_LEFT][BACK_CENTER]+= M_SQRT1_2;
134             matrix[SIDE_RIGHT][BACK_CENTER]+= M_SQRT1_2;
135         }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
136             matrix[ FRONT_LEFT][BACK_CENTER]+= s->slev*M_SQRT1_2;
137             matrix[FRONT_RIGHT][BACK_CENTER]+= s->slev*M_SQRT1_2;
138         }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
139             matrix[ FRONT_CENTER][BACK_CENTER]+= s->slev*M_SQRT1_2;
140         }else
141             av_assert0(0);
142     }
143     if(unaccounted & AV_CH_BACK_LEFT){
144         if(s->out_ch_layout & AV_CH_BACK_CENTER){
145             matrix[BACK_CENTER][ BACK_LEFT]+= M_SQRT1_2;
146             matrix[BACK_CENTER][BACK_RIGHT]+= M_SQRT1_2;
147         }else if(s->out_ch_layout & AV_CH_SIDE_LEFT){
148             if(s->in_ch_layout & AV_CH_SIDE_LEFT){
149                 matrix[ SIDE_LEFT][ BACK_LEFT]+= M_SQRT1_2;
150                 matrix[SIDE_RIGHT][BACK_RIGHT]+= M_SQRT1_2;
151             }else{
152             matrix[ SIDE_LEFT][ BACK_LEFT]+= 1.0;
153             matrix[SIDE_RIGHT][BACK_RIGHT]+= 1.0;
154             }
155         }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
156             matrix[ FRONT_LEFT][ BACK_LEFT]+= s->slev;
157             matrix[FRONT_RIGHT][BACK_RIGHT]+= s->slev;
158         }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
159             matrix[ FRONT_CENTER][BACK_LEFT ]+= s->slev*M_SQRT1_2;
160             matrix[ FRONT_CENTER][BACK_RIGHT]+= s->slev*M_SQRT1_2;
161         }else
162             av_assert0(0);
163     }
164
165     if(unaccounted & AV_CH_SIDE_LEFT){
166         if(s->out_ch_layout & AV_CH_BACK_LEFT){
167             matrix[ BACK_LEFT][ SIDE_LEFT]+= 1.0;
168             matrix[BACK_RIGHT][SIDE_RIGHT]+= 1.0;
169         }else if(s->out_ch_layout & AV_CH_BACK_CENTER){
170             matrix[BACK_CENTER][ SIDE_LEFT]+= M_SQRT1_2;
171             matrix[BACK_CENTER][SIDE_RIGHT]+= M_SQRT1_2;
172         }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
173             matrix[ FRONT_LEFT][ SIDE_LEFT]+= s->slev;
174             matrix[FRONT_RIGHT][SIDE_RIGHT]+= s->slev;
175         }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
176             matrix[ FRONT_CENTER][SIDE_LEFT ]+= s->slev*M_SQRT1_2;
177             matrix[ FRONT_CENTER][SIDE_RIGHT]+= s->slev*M_SQRT1_2;
178         }else
179             av_assert0(0);
180     }
181
182     if(unaccounted & AV_CH_FRONT_LEFT_OF_CENTER){
183         if(s->out_ch_layout & AV_CH_FRONT_LEFT){
184             matrix[ FRONT_LEFT][ FRONT_LEFT_OF_CENTER]+= 1.0;
185             matrix[FRONT_RIGHT][FRONT_RIGHT_OF_CENTER]+= 1.0;
186         }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
187             matrix[ FRONT_CENTER][ FRONT_LEFT_OF_CENTER]+= M_SQRT1_2;
188             matrix[ FRONT_CENTER][FRONT_RIGHT_OF_CENTER]+= M_SQRT1_2;
189         }else
190             av_assert0(0);
191     }
192
193     //FIXME quantize for integeres
194     for(out_i=i=0; i<64; i++){
195         double sum=0;
196         int in_i=0;
197         int ch_in=0;
198         for(j=0; j<64; j++){
199             s->matrix[out_i][in_i]= matrix[i][j];
200             s->matrix32[out_i][in_i]= lrintf(matrix[i][j] * 32768);
201             if(matrix[i][j]){
202                 s->matrix_ch[out_i][++ch_in]= in_i;
203                 sum += fabs(matrix[i][j]);
204             }
205             if(s->in_ch_layout & (1ULL<<j))
206                 in_i++;
207         }
208         s->matrix_ch[out_i][0]= ch_in;
209         maxcoef= FFMAX(maxcoef, sum);
210         if(s->out_ch_layout & (1ULL<<i))
211             out_i++;
212     }
213     if(s->rematrix_volume  < 0)
214         maxcoef = -s->rematrix_volume;
215
216     if((   s->out_sample_fmt < AV_SAMPLE_FMT_FLT
217         || s->int_sample_fmt < AV_SAMPLE_FMT_FLT) && maxcoef > 1.0){
218         for(i=0; i<SWR_CH_MAX; i++)
219             for(j=0; j<SWR_CH_MAX; j++){
220                 s->matrix[i][j] /= maxcoef;
221                 s->matrix32[i][j]= lrintf(s->matrix[i][j] * 32768);
222             }
223     }
224
225     if(s->rematrix_volume > 0){
226         for(i=0; i<SWR_CH_MAX; i++)
227             for(j=0; j<SWR_CH_MAX; j++){
228                 s->matrix[i][j] *= s->rematrix_volume;
229                 s->matrix32[i][j]= lrintf(s->matrix[i][j] * 32768);
230             }
231     }
232
233     for(i=0; i<av_get_channel_layout_nb_channels(s->out_ch_layout); i++){
234         for(j=0; j<av_get_channel_layout_nb_channels(s->in_ch_layout); j++){
235             av_log(NULL, AV_LOG_DEBUG, "%f ", s->matrix[i][j]);
236         }
237         av_log(NULL, AV_LOG_DEBUG, "\n");
238     }
239     return 0;
240 }
241
242 int swri_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy){
243     int out_i, in_i, i, j;
244
245     av_assert0(out->ch_count == av_get_channel_layout_nb_channels(s->out_ch_layout));
246     av_assert0(in ->ch_count == av_get_channel_layout_nb_channels(s-> in_ch_layout));
247
248     for(out_i=0; out_i<out->ch_count; out_i++){
249         switch(s->matrix_ch[out_i][0]){
250         case 1:
251             in_i= s->matrix_ch[out_i][1];
252             if(mustcopy || s->matrix[out_i][in_i]!=1.0){
253                 if(s->int_sample_fmt == AV_SAMPLE_FMT_FLT){
254                     copy_float((float  *)out->ch[out_i], (const float  *)in->ch[in_i], s->matrix  [out_i][in_i], len);
255                 }else
256                     copy_s16  ((int16_t*)out->ch[out_i], (const int16_t*)in->ch[in_i], s->matrix32[out_i][in_i], len);
257             }else{
258                 out->ch[out_i]= in->ch[in_i];
259             }
260             break;
261         case 2:
262             if(s->int_sample_fmt == AV_SAMPLE_FMT_FLT){
263                 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] ],
264                                  s->matrix[out_i][ s->matrix_ch[out_i][1] ], s->matrix[out_i][ s->matrix_ch[out_i][2] ],
265                            len);
266             }else{
267                 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] ],
268                                  s->matrix32[out_i][ s->matrix_ch[out_i][1] ], s->matrix32[out_i][ s->matrix_ch[out_i][2] ],
269                            len);
270             }
271             break;
272         default:
273             if(s->int_sample_fmt == AV_SAMPLE_FMT_FLT){
274                 for(i=0; i<len; i++){
275                     float v=0;
276                     for(j=0; j<s->matrix_ch[out_i][0]; j++){
277                         in_i= s->matrix_ch[out_i][1+j];
278                         v+= ((float*)in->ch[in_i])[i] * s->matrix[out_i][in_i];
279                     }
280                     ((float*)out->ch[out_i])[i]= v;
281                 }
282             }else{
283                 for(i=0; i<len; i++){
284                     int v=0;
285                     for(j=0; j<s->matrix_ch[out_i][0]; j++){
286                         in_i= s->matrix_ch[out_i][1+j];
287                         v+= ((int16_t*)in->ch[in_i])[i] * s->matrix32[out_i][in_i];
288                     }
289                     ((int16_t*)out->ch[out_i])[i]= (v + 16384)>>15;
290                 }
291             }
292         }
293     }
294     return 0;
295 }