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