]> git.sesse.net Git - ffmpeg/blob - libswresample/rematrix.c
Merge remote-tracking branch 'qatar/master'
[ffmpeg] / libswresample / rematrix.c
1 /*
2  * Copyright (C) 2011-2012 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 (1.0)
38 #define R(x) x
39 #define SAMPLE double
40 #define COEFF double
41 #define RENAME(x) x ## _double
42 #include "rematrix_template.c"
43 #undef SAMPLE
44 #undef RENAME
45 #undef R
46 #undef ONE
47 #undef COEFF
48
49 #define ONE (-32768)
50 #define R(x) (((x) + 16384)>>15)
51 #define SAMPLE int16_t
52 #define COEFF int
53 #define RENAME(x) x ## _s16
54 #include "rematrix_template.c"
55
56
57 #define FRONT_LEFT             0
58 #define FRONT_RIGHT            1
59 #define FRONT_CENTER           2
60 #define LOW_FREQUENCY          3
61 #define BACK_LEFT              4
62 #define BACK_RIGHT             5
63 #define FRONT_LEFT_OF_CENTER   6
64 #define FRONT_RIGHT_OF_CENTER  7
65 #define BACK_CENTER            8
66 #define SIDE_LEFT              9
67 #define SIDE_RIGHT             10
68 #define TOP_CENTER             11
69 #define TOP_FRONT_LEFT         12
70 #define TOP_FRONT_CENTER       13
71 #define TOP_FRONT_RIGHT        14
72 #define TOP_BACK_LEFT          15
73 #define TOP_BACK_CENTER        16
74 #define TOP_BACK_RIGHT         17
75
76 int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride)
77 {
78     int nb_in, nb_out, in, out;
79
80     if (!s || s->in_convert) // s needs to be allocated but not initialized
81         return AVERROR(EINVAL);
82     memset(s->matrix, 0, sizeof(s->matrix));
83     nb_in  = av_get_channel_layout_nb_channels(s->in_ch_layout);
84     nb_out = av_get_channel_layout_nb_channels(s->out_ch_layout);
85     for (out = 0; out < nb_out; out++) {
86         for (in = 0; in < nb_in; in++)
87             s->matrix[out][in] = matrix[in];
88         matrix += stride;
89     }
90     s->rematrix_custom = 1;
91     return 0;
92 }
93
94 static int even(int64_t layout){
95     if(!layout) return 1;
96     if(layout&(layout-1)) return 1;
97     return 0;
98 }
99
100 static int sane_layout(int64_t layout){
101     if(!(layout & AV_CH_LAYOUT_SURROUND)) // at least 1 front speaker
102         return 0;
103     if(!even(layout & (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT))) // no asymetric front
104         return 0;
105     if(!even(layout & (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)))   // no asymetric side
106         return 0;
107     if(!even(layout & (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)))
108         return 0;
109     if(!even(layout & (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)))
110         return 0;
111     if(av_get_channel_layout_nb_channels(layout) >= SWR_CH_MAX)
112         return 0;
113
114     return 1;
115 }
116
117 static int auto_matrix(SwrContext *s)
118 {
119     int i, j, out_i;
120     double matrix[64][64]={{0}};
121     int64_t unaccounted= s->in_ch_layout & ~s->out_ch_layout;
122     double maxcoef=0;
123
124     memset(s->matrix, 0, sizeof(s->matrix));
125     for(i=0; i<64; i++){
126         if(s->in_ch_layout & s->out_ch_layout & (1LL<<i))
127             matrix[i][i]= 1.0;
128     }
129
130     if(!sane_layout(s->in_ch_layout)){
131         av_log(s, AV_LOG_ERROR, "Input channel layout isnt supported\n");
132         return AVERROR(EINVAL);
133     }
134     if(!sane_layout(s->out_ch_layout)){
135         av_log(s, AV_LOG_ERROR, "Output channel layout isnt supported\n");
136         return AVERROR(EINVAL);
137     }
138
139 //FIXME implement dolby surround
140 //FIXME implement full ac3
141
142
143     if(unaccounted & AV_CH_FRONT_CENTER){
144         if((s->out_ch_layout & AV_CH_LAYOUT_STEREO) == AV_CH_LAYOUT_STEREO){
145             matrix[ FRONT_LEFT][FRONT_CENTER]+= M_SQRT1_2;
146             matrix[FRONT_RIGHT][FRONT_CENTER]+= M_SQRT1_2;
147         }else
148             av_assert0(0);
149     }
150     if(unaccounted & AV_CH_LAYOUT_STEREO){
151         if(s->out_ch_layout & AV_CH_FRONT_CENTER){
152             matrix[FRONT_CENTER][ FRONT_LEFT]+= M_SQRT1_2;
153             matrix[FRONT_CENTER][FRONT_RIGHT]+= M_SQRT1_2;
154             if(s->in_ch_layout & AV_CH_FRONT_CENTER)
155                 matrix[FRONT_CENTER][ FRONT_CENTER] = s->clev*sqrt(2);
156         }else
157             av_assert0(0);
158     }
159
160     if(unaccounted & AV_CH_BACK_CENTER){
161         if(s->out_ch_layout & AV_CH_BACK_LEFT){
162             matrix[ BACK_LEFT][BACK_CENTER]+= M_SQRT1_2;
163             matrix[BACK_RIGHT][BACK_CENTER]+= M_SQRT1_2;
164         }else if(s->out_ch_layout & AV_CH_SIDE_LEFT){
165             matrix[ SIDE_LEFT][BACK_CENTER]+= M_SQRT1_2;
166             matrix[SIDE_RIGHT][BACK_CENTER]+= M_SQRT1_2;
167         }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
168             matrix[ FRONT_LEFT][BACK_CENTER]+= s->slev*M_SQRT1_2;
169             matrix[FRONT_RIGHT][BACK_CENTER]+= s->slev*M_SQRT1_2;
170         }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
171             matrix[ FRONT_CENTER][BACK_CENTER]+= s->slev*M_SQRT1_2;
172         }else
173             av_assert0(0);
174     }
175     if(unaccounted & AV_CH_BACK_LEFT){
176         if(s->out_ch_layout & AV_CH_BACK_CENTER){
177             matrix[BACK_CENTER][ BACK_LEFT]+= M_SQRT1_2;
178             matrix[BACK_CENTER][BACK_RIGHT]+= M_SQRT1_2;
179         }else if(s->out_ch_layout & AV_CH_SIDE_LEFT){
180             if(s->in_ch_layout & AV_CH_SIDE_LEFT){
181                 matrix[ SIDE_LEFT][ BACK_LEFT]+= M_SQRT1_2;
182                 matrix[SIDE_RIGHT][BACK_RIGHT]+= M_SQRT1_2;
183             }else{
184             matrix[ SIDE_LEFT][ BACK_LEFT]+= 1.0;
185             matrix[SIDE_RIGHT][BACK_RIGHT]+= 1.0;
186             }
187         }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
188             matrix[ FRONT_LEFT][ BACK_LEFT]+= s->slev;
189             matrix[FRONT_RIGHT][BACK_RIGHT]+= s->slev;
190         }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
191             matrix[ FRONT_CENTER][BACK_LEFT ]+= s->slev*M_SQRT1_2;
192             matrix[ FRONT_CENTER][BACK_RIGHT]+= s->slev*M_SQRT1_2;
193         }else
194             av_assert0(0);
195     }
196
197     if(unaccounted & AV_CH_SIDE_LEFT){
198         if(s->out_ch_layout & AV_CH_BACK_LEFT){
199             /* if back channels do not exist in the input, just copy side
200                channels to back channels, otherwise mix side into back */
201             if (s->in_ch_layout & AV_CH_BACK_LEFT) {
202                 matrix[BACK_LEFT ][SIDE_LEFT ] += M_SQRT1_2;
203                 matrix[BACK_RIGHT][SIDE_RIGHT] += M_SQRT1_2;
204             } else {
205                 matrix[BACK_LEFT ][SIDE_LEFT ] += 1.0;
206                 matrix[BACK_RIGHT][SIDE_RIGHT] += 1.0;
207             }
208         }else if(s->out_ch_layout & AV_CH_BACK_CENTER){
209             matrix[BACK_CENTER][ SIDE_LEFT]+= M_SQRT1_2;
210             matrix[BACK_CENTER][SIDE_RIGHT]+= M_SQRT1_2;
211         }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
212             matrix[ FRONT_LEFT][ SIDE_LEFT]+= s->slev;
213             matrix[FRONT_RIGHT][SIDE_RIGHT]+= s->slev;
214         }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
215             matrix[ FRONT_CENTER][SIDE_LEFT ]+= s->slev*M_SQRT1_2;
216             matrix[ FRONT_CENTER][SIDE_RIGHT]+= s->slev*M_SQRT1_2;
217         }else
218             av_assert0(0);
219     }
220
221     if(unaccounted & AV_CH_FRONT_LEFT_OF_CENTER){
222         if(s->out_ch_layout & AV_CH_FRONT_LEFT){
223             matrix[ FRONT_LEFT][ FRONT_LEFT_OF_CENTER]+= 1.0;
224             matrix[FRONT_RIGHT][FRONT_RIGHT_OF_CENTER]+= 1.0;
225         }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
226             matrix[ FRONT_CENTER][ FRONT_LEFT_OF_CENTER]+= M_SQRT1_2;
227             matrix[ FRONT_CENTER][FRONT_RIGHT_OF_CENTER]+= M_SQRT1_2;
228         }else
229             av_assert0(0);
230     }
231     /* mix LFE into front left/right or center */
232     if (unaccounted & AV_CH_LOW_FREQUENCY) {
233         if (s->out_ch_layout & AV_CH_FRONT_CENTER) {
234             matrix[FRONT_CENTER][LOW_FREQUENCY] += s->lfe_mix_level;
235         } else if (s->out_ch_layout & AV_CH_FRONT_LEFT) {
236             matrix[FRONT_LEFT ][LOW_FREQUENCY] += s->lfe_mix_level * M_SQRT1_2;
237             matrix[FRONT_RIGHT][LOW_FREQUENCY] += s->lfe_mix_level * M_SQRT1_2;
238         } else
239             av_assert0(0);
240     }
241
242     for(out_i=i=0; i<64; i++){
243         double sum=0;
244         int in_i=0;
245         for(j=0; j<64; j++){
246             s->matrix[out_i][in_i]= matrix[i][j];
247             if(matrix[i][j]){
248                 sum += fabs(matrix[i][j]);
249             }
250             if(s->in_ch_layout & (1ULL<<j))
251                 in_i++;
252         }
253         maxcoef= FFMAX(maxcoef, sum);
254         if(s->out_ch_layout & (1ULL<<i))
255             out_i++;
256     }
257     if(s->rematrix_volume  < 0)
258         maxcoef = -s->rematrix_volume;
259
260     if((   av_get_packed_sample_fmt(s->out_sample_fmt) < AV_SAMPLE_FMT_FLT
261         || av_get_packed_sample_fmt(s->int_sample_fmt) < AV_SAMPLE_FMT_FLT) && maxcoef > 1.0){
262         for(i=0; i<SWR_CH_MAX; i++)
263             for(j=0; j<SWR_CH_MAX; j++){
264                 s->matrix[i][j] /= maxcoef;
265             }
266     }
267
268     if(s->rematrix_volume > 0){
269         for(i=0; i<SWR_CH_MAX; i++)
270             for(j=0; j<SWR_CH_MAX; j++){
271                 s->matrix[i][j] *= s->rematrix_volume;
272             }
273     }
274
275     for(i=0; i<av_get_channel_layout_nb_channels(s->out_ch_layout); i++){
276         for(j=0; j<av_get_channel_layout_nb_channels(s->in_ch_layout); j++){
277             av_log(NULL, AV_LOG_DEBUG, "%f ", s->matrix[i][j]);
278         }
279         av_log(NULL, AV_LOG_DEBUG, "\n");
280     }
281     return 0;
282 }
283
284 int swri_rematrix_init(SwrContext *s){
285     int i, j;
286     int nb_in  = av_get_channel_layout_nb_channels(s->in_ch_layout);
287     int nb_out = av_get_channel_layout_nb_channels(s->out_ch_layout);
288
289     if (!s->rematrix_custom) {
290         int r = auto_matrix(s);
291         if (r)
292             return r;
293     }
294     if (s->midbuf.fmt == AV_SAMPLE_FMT_S16P){
295         s->native_matrix = av_mallocz(nb_in * nb_out * sizeof(int));
296         s->native_one    = av_mallocz(sizeof(int));
297         for (i = 0; i < nb_out; i++)
298             for (j = 0; j < nb_in; j++)
299                 ((int*)s->native_matrix)[i * nb_in + j] = lrintf(s->matrix[i][j] * 32768);
300         *((int*)s->native_one) = 32768;
301         s->mix_1_1_f = copy_s16;
302         s->mix_2_1_f = sum2_s16;
303     }else if(s->midbuf.fmt == AV_SAMPLE_FMT_FLTP){
304         s->native_matrix = av_mallocz(nb_in * nb_out * sizeof(float));
305         s->native_one    = av_mallocz(sizeof(float));
306         for (i = 0; i < nb_out; i++)
307             for (j = 0; j < nb_in; j++)
308                 ((float*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
309         *((float*)s->native_one) = 1.0;
310         s->mix_1_1_f = copy_float;
311         s->mix_2_1_f = sum2_float;
312     }else if(s->midbuf.fmt == AV_SAMPLE_FMT_DBLP){
313         s->native_matrix = av_mallocz(nb_in * nb_out * sizeof(double));
314         s->native_one    = av_mallocz(sizeof(double));
315         for (i = 0; i < nb_out; i++)
316             for (j = 0; j < nb_in; j++)
317                 ((double*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
318         *((double*)s->native_one) = 1.0;
319         s->mix_1_1_f = copy_double;
320         s->mix_2_1_f = sum2_double;
321     }else
322         av_assert0(0);
323     //FIXME quantize for integeres
324     for (i = 0; i < SWR_CH_MAX; i++) {
325         int ch_in=0;
326         for (j = 0; j < SWR_CH_MAX; j++) {
327             s->matrix32[i][j]= lrintf(s->matrix[i][j] * 32768);
328             if(s->matrix[i][j])
329                 s->matrix_ch[i][++ch_in]= j;
330         }
331         s->matrix_ch[i][0]= ch_in;
332     }
333     return 0;
334 }
335
336 void swri_rematrix_free(SwrContext *s){
337     av_freep(&s->native_matrix);
338     av_freep(&s->native_one);
339 }
340
341 int swri_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy){
342     int out_i, in_i, i, j;
343
344     av_assert0(out->ch_count == av_get_channel_layout_nb_channels(s->out_ch_layout));
345     av_assert0(in ->ch_count == av_get_channel_layout_nb_channels(s-> in_ch_layout));
346
347     for(out_i=0; out_i<out->ch_count; out_i++){
348         switch(s->matrix_ch[out_i][0]){
349         case 0:
350             memset(out->ch[out_i], 0, len * av_get_bytes_per_sample(s->int_sample_fmt));
351             break;
352         case 1:
353             in_i= s->matrix_ch[out_i][1];
354             if(s->matrix[out_i][in_i]!=1.0){
355                 s->mix_1_1_f(out->ch[out_i], in->ch[in_i], s->native_matrix, in->ch_count*out_i + in_i, len);
356             }else if(mustcopy){
357                 memcpy(out->ch[out_i], in->ch[in_i], len*out->bps);
358             }else{
359                 out->ch[out_i]= in->ch[in_i];
360             }
361             break;
362         case 2: {
363             int in_i1 = s->matrix_ch[out_i][1];
364             int in_i2 = s->matrix_ch[out_i][2];
365             s->mix_2_1_f(out->ch[out_i], in->ch[in_i1], in->ch[in_i2], s->native_matrix, in->ch_count*out_i + in_i1, in->ch_count*out_i + in_i2, len);
366             break;}
367         default:
368             if(s->int_sample_fmt == AV_SAMPLE_FMT_FLTP){
369                 for(i=0; i<len; i++){
370                     float v=0;
371                     for(j=0; j<s->matrix_ch[out_i][0]; j++){
372                         in_i= s->matrix_ch[out_i][1+j];
373                         v+= ((float*)in->ch[in_i])[i] * s->matrix[out_i][in_i];
374                     }
375                     ((float*)out->ch[out_i])[i]= v;
376                 }
377             }else if(s->int_sample_fmt == AV_SAMPLE_FMT_DBLP){
378                 for(i=0; i<len; i++){
379                     double v=0;
380                     for(j=0; j<s->matrix_ch[out_i][0]; j++){
381                         in_i= s->matrix_ch[out_i][1+j];
382                         v+= ((double*)in->ch[in_i])[i] * s->matrix[out_i][in_i];
383                     }
384                     ((double*)out->ch[out_i])[i]= v;
385                 }
386             }else{
387                 for(i=0; i<len; i++){
388                     int v=0;
389                     for(j=0; j<s->matrix_ch[out_i][0]; j++){
390                         in_i= s->matrix_ch[out_i][1+j];
391                         v+= ((int16_t*)in->ch[in_i])[i] * s->matrix32[out_i][in_i];
392                     }
393                     ((int16_t*)out->ch[out_i])[i]= (v + 16384)>>15;
394                 }
395             }
396         }
397     }
398     return 0;
399 }