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