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