FFmpeg  1.2.4
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(!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  if(!sane_layout(out_ch_layout)){
132  av_get_channel_layout_string(buf, sizeof(buf), -1, s->out_ch_layout);
133  av_log(s, AV_LOG_ERROR, "Output channel layout '%s' is not supported\n", buf);
134  return AVERROR(EINVAL);
135  }
136 
137  memset(s->matrix, 0, sizeof(s->matrix));
138  for(i=0; i<64; i++){
139  if(in_ch_layout & out_ch_layout & (1ULL<<i))
140  matrix[i][i]= 1.0;
141  }
142 
143  unaccounted= in_ch_layout & ~out_ch_layout;
144 
145 //FIXME implement dolby surround
146 //FIXME implement full ac3
147 
148 
149  if(unaccounted & AV_CH_FRONT_CENTER){
150  if((out_ch_layout & AV_CH_LAYOUT_STEREO) == AV_CH_LAYOUT_STEREO){
151  if(in_ch_layout & AV_CH_LAYOUT_STEREO) {
152  matrix[ FRONT_LEFT][FRONT_CENTER]+= s->clev;
153  matrix[FRONT_RIGHT][FRONT_CENTER]+= s->clev;
154  } else {
155  matrix[ FRONT_LEFT][FRONT_CENTER]+= M_SQRT1_2;
157  }
158  }else
159  av_assert0(0);
160  }
161  if(unaccounted & AV_CH_LAYOUT_STEREO){
162  if(out_ch_layout & AV_CH_FRONT_CENTER){
163  matrix[FRONT_CENTER][ FRONT_LEFT]+= M_SQRT1_2;
165  if(in_ch_layout & AV_CH_FRONT_CENTER)
166  matrix[FRONT_CENTER][ FRONT_CENTER] = s->clev*sqrt(2);
167  }else
168  av_assert0(0);
169  }
170 
171  if(unaccounted & AV_CH_BACK_CENTER){
172  if(out_ch_layout & AV_CH_BACK_LEFT){
173  matrix[ BACK_LEFT][BACK_CENTER]+= M_SQRT1_2;
174  matrix[BACK_RIGHT][BACK_CENTER]+= M_SQRT1_2;
175  }else if(out_ch_layout & AV_CH_SIDE_LEFT){
176  matrix[ SIDE_LEFT][BACK_CENTER]+= M_SQRT1_2;
177  matrix[SIDE_RIGHT][BACK_CENTER]+= M_SQRT1_2;
178  }else if(out_ch_layout & AV_CH_FRONT_LEFT){
179  if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY ||
180  matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
181  if (unaccounted & (AV_CH_BACK_LEFT | AV_CH_SIDE_LEFT)) {
182  matrix[FRONT_LEFT ][BACK_CENTER] -= s->slev * M_SQRT1_2;
183  matrix[FRONT_RIGHT][BACK_CENTER] += s->slev * M_SQRT1_2;
184  } else {
185  matrix[FRONT_LEFT ][BACK_CENTER] -= s->slev;
186  matrix[FRONT_RIGHT][BACK_CENTER] += s->slev;
187  }
188  } else {
189  matrix[ FRONT_LEFT][BACK_CENTER]+= s->slev*M_SQRT1_2;
190  matrix[FRONT_RIGHT][BACK_CENTER]+= s->slev*M_SQRT1_2;
191  }
192  }else if(out_ch_layout & AV_CH_FRONT_CENTER){
193  matrix[ FRONT_CENTER][BACK_CENTER]+= s->slev*M_SQRT1_2;
194  }else
195  av_assert0(0);
196  }
197  if(unaccounted & AV_CH_BACK_LEFT){
198  if(out_ch_layout & AV_CH_BACK_CENTER){
199  matrix[BACK_CENTER][ BACK_LEFT]+= M_SQRT1_2;
200  matrix[BACK_CENTER][BACK_RIGHT]+= M_SQRT1_2;
201  }else if(out_ch_layout & AV_CH_SIDE_LEFT){
202  if(in_ch_layout & AV_CH_SIDE_LEFT){
203  matrix[ SIDE_LEFT][ BACK_LEFT]+= M_SQRT1_2;
204  matrix[SIDE_RIGHT][BACK_RIGHT]+= M_SQRT1_2;
205  }else{
206  matrix[ SIDE_LEFT][ BACK_LEFT]+= 1.0;
207  matrix[SIDE_RIGHT][BACK_RIGHT]+= 1.0;
208  }
209  }else if(out_ch_layout & AV_CH_FRONT_LEFT){
210  if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY) {
211  matrix[FRONT_LEFT ][BACK_LEFT ] -= s->slev * M_SQRT1_2;
212  matrix[FRONT_LEFT ][BACK_RIGHT] -= s->slev * M_SQRT1_2;
213  matrix[FRONT_RIGHT][BACK_LEFT ] += s->slev * M_SQRT1_2;
214  matrix[FRONT_RIGHT][BACK_RIGHT] += s->slev * M_SQRT1_2;
215  } else if (matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
216  matrix[FRONT_LEFT ][BACK_LEFT ] -= s->slev * SQRT3_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 * SQRT3_2;
220  } else {
221  matrix[ FRONT_LEFT][ BACK_LEFT] += s->slev;
222  matrix[FRONT_RIGHT][BACK_RIGHT] += s->slev;
223  }
224  }else if(out_ch_layout & AV_CH_FRONT_CENTER){
225  matrix[ FRONT_CENTER][BACK_LEFT ]+= s->slev*M_SQRT1_2;
226  matrix[ FRONT_CENTER][BACK_RIGHT]+= s->slev*M_SQRT1_2;
227  }else
228  av_assert0(0);
229  }
230 
231  if(unaccounted & AV_CH_SIDE_LEFT){
232  if(out_ch_layout & AV_CH_BACK_LEFT){
233  /* if back channels do not exist in the input, just copy side
234  channels to back channels, otherwise mix side into back */
235  if (in_ch_layout & AV_CH_BACK_LEFT) {
236  matrix[BACK_LEFT ][SIDE_LEFT ] += M_SQRT1_2;
237  matrix[BACK_RIGHT][SIDE_RIGHT] += M_SQRT1_2;
238  } else {
239  matrix[BACK_LEFT ][SIDE_LEFT ] += 1.0;
240  matrix[BACK_RIGHT][SIDE_RIGHT] += 1.0;
241  }
242  }else if(out_ch_layout & AV_CH_BACK_CENTER){
243  matrix[BACK_CENTER][ SIDE_LEFT]+= M_SQRT1_2;
244  matrix[BACK_CENTER][SIDE_RIGHT]+= M_SQRT1_2;
245  }else if(out_ch_layout & AV_CH_FRONT_LEFT){
246  if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY) {
247  matrix[FRONT_LEFT ][SIDE_LEFT ] -= s->slev * M_SQRT1_2;
248  matrix[FRONT_LEFT ][SIDE_RIGHT] -= s->slev * M_SQRT1_2;
249  matrix[FRONT_RIGHT][SIDE_LEFT ] += s->slev * M_SQRT1_2;
250  matrix[FRONT_RIGHT][SIDE_RIGHT] += s->slev * M_SQRT1_2;
251  } else if (matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
252  matrix[FRONT_LEFT ][SIDE_LEFT ] -= s->slev * SQRT3_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 * SQRT3_2;
256  } else {
257  matrix[ FRONT_LEFT][ SIDE_LEFT] += s->slev;
258  matrix[FRONT_RIGHT][SIDE_RIGHT] += s->slev;
259  }
260  }else if(out_ch_layout & AV_CH_FRONT_CENTER){
261  matrix[ FRONT_CENTER][SIDE_LEFT ]+= s->slev*M_SQRT1_2;
262  matrix[ FRONT_CENTER][SIDE_RIGHT]+= s->slev*M_SQRT1_2;
263  }else
264  av_assert0(0);
265  }
266 
267  if(unaccounted & AV_CH_FRONT_LEFT_OF_CENTER){
268  if(out_ch_layout & AV_CH_FRONT_LEFT){
269  matrix[ FRONT_LEFT][ FRONT_LEFT_OF_CENTER]+= 1.0;
270  matrix[FRONT_RIGHT][FRONT_RIGHT_OF_CENTER]+= 1.0;
271  }else if(out_ch_layout & AV_CH_FRONT_CENTER){
274  }else
275  av_assert0(0);
276  }
277  /* mix LFE into front left/right or center */
278  if (unaccounted & AV_CH_LOW_FREQUENCY) {
279  if (out_ch_layout & AV_CH_FRONT_CENTER) {
281  } else if (out_ch_layout & AV_CH_FRONT_LEFT) {
284  } else
285  av_assert0(0);
286  }
287 
288  for(out_i=i=0; i<64; i++){
289  double sum=0;
290  int in_i=0;
291  for(j=0; j<64; j++){
292  s->matrix[out_i][in_i]= matrix[i][j];
293  if(matrix[i][j]){
294  sum += fabs(matrix[i][j]);
295  }
296  if(in_ch_layout & (1ULL<<j))
297  in_i++;
298  }
299  maxcoef= FFMAX(maxcoef, sum);
300  if(out_ch_layout & (1ULL<<i))
301  out_i++;
302  }
303  if(s->rematrix_volume < 0)
304  maxcoef = -s->rematrix_volume;
305 
307  || av_get_packed_sample_fmt(s->int_sample_fmt) < AV_SAMPLE_FMT_FLT) && maxcoef > 1.0){
308  for(i=0; i<SWR_CH_MAX; i++)
309  for(j=0; j<SWR_CH_MAX; j++){
310  s->matrix[i][j] /= maxcoef;
311  }
312  }
313 
314  if(s->rematrix_volume > 0){
315  for(i=0; i<SWR_CH_MAX; i++)
316  for(j=0; j<SWR_CH_MAX; j++){
317  s->matrix[i][j] *= s->rematrix_volume;
318  }
319  }
320 
321  for(i=0; i<av_get_channel_layout_nb_channels(out_ch_layout); i++){
322  for(j=0; j<av_get_channel_layout_nb_channels(in_ch_layout); j++){
323  av_log(NULL, AV_LOG_DEBUG, "%f ", s->matrix[i][j]);
324  }
325  av_log(NULL, AV_LOG_DEBUG, "\n");
326  }
327  return 0;
328 }
329 
331  int i, j;
334 
335  s->mix_any_f = NULL;
336 
337  if (!s->rematrix_custom) {
338  int r = auto_matrix(s);
339  if (r)
340  return r;
341  }
342  if (s->midbuf.fmt == AV_SAMPLE_FMT_S16P){
343  s->native_matrix = av_mallocz(nb_in * nb_out * sizeof(int));
344  s->native_one = av_mallocz(sizeof(int));
345  for (i = 0; i < nb_out; i++)
346  for (j = 0; j < nb_in; j++)
347  ((int*)s->native_matrix)[i * nb_in + j] = lrintf(s->matrix[i][j] * 32768);
348  *((int*)s->native_one) = 32768;
349  s->mix_1_1_f = (mix_1_1_func_type*)copy_s16;
350  s->mix_2_1_f = (mix_2_1_func_type*)sum2_s16;
351  s->mix_any_f = (mix_any_func_type*)get_mix_any_func_s16(s);
352  }else if(s->midbuf.fmt == AV_SAMPLE_FMT_FLTP){
353  s->native_matrix = av_mallocz(nb_in * nb_out * sizeof(float));
354  s->native_one = av_mallocz(sizeof(float));
355  for (i = 0; i < nb_out; i++)
356  for (j = 0; j < nb_in; j++)
357  ((float*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
358  *((float*)s->native_one) = 1.0;
359  s->mix_1_1_f = (mix_1_1_func_type*)copy_float;
360  s->mix_2_1_f = (mix_2_1_func_type*)sum2_float;
361  s->mix_any_f = (mix_any_func_type*)get_mix_any_func_float(s);
362  }else if(s->midbuf.fmt == AV_SAMPLE_FMT_DBLP){
363  s->native_matrix = av_mallocz(nb_in * nb_out * sizeof(double));
364  s->native_one = av_mallocz(sizeof(double));
365  for (i = 0; i < nb_out; i++)
366  for (j = 0; j < nb_in; j++)
367  ((double*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
368  *((double*)s->native_one) = 1.0;
369  s->mix_1_1_f = (mix_1_1_func_type*)copy_double;
370  s->mix_2_1_f = (mix_2_1_func_type*)sum2_double;
371  s->mix_any_f = (mix_any_func_type*)get_mix_any_func_double(s);
372  }else
373  av_assert0(0);
374  //FIXME quantize for integeres
375  for (i = 0; i < SWR_CH_MAX; i++) {
376  int ch_in=0;
377  for (j = 0; j < SWR_CH_MAX; j++) {
378  s->matrix32[i][j]= lrintf(s->matrix[i][j] * 32768);
379  if(s->matrix[i][j])
380  s->matrix_ch[i][++ch_in]= j;
381  }
382  s->matrix_ch[i][0]= ch_in;
383  }
384 
386 
387  return 0;
388 }
389 
391  av_freep(&s->native_matrix);
392  av_freep(&s->native_one);
394 }
395 
396 int swri_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy){
397  int out_i, in_i, i, j;
398  int len1 = 0;
399  int off = 0;
400 
401  if(s->mix_any_f) {
402  s->mix_any_f(out->ch, (const uint8_t **)in->ch, s->native_matrix, len);
403  return 0;
404  }
405 
406  if(s->mix_2_1_simd || s->mix_1_1_simd){
407  len1= len&~15;
408  off = len1 * out->bps;
409  }
410 
412  av_assert0(in ->ch_count == av_get_channel_layout_nb_channels(s-> in_ch_layout));
413 
414  for(out_i=0; out_i<out->ch_count; out_i++){
415  switch(s->matrix_ch[out_i][0]){
416  case 0:
417  if(mustcopy)
418  memset(out->ch[out_i], 0, len * av_get_bytes_per_sample(s->int_sample_fmt));
419  break;
420  case 1:
421  in_i= s->matrix_ch[out_i][1];
422  if(s->matrix[out_i][in_i]!=1.0){
423  if(s->mix_1_1_simd && len1)
424  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);
425  if(len != len1)
426  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);
427  }else if(mustcopy){
428  memcpy(out->ch[out_i], in->ch[in_i], len*out->bps);
429  }else{
430  out->ch[out_i]= in->ch[in_i];
431  }
432  break;
433  case 2: {
434  int in_i1 = s->matrix_ch[out_i][1];
435  int in_i2 = s->matrix_ch[out_i][2];
436  if(s->mix_2_1_simd && len1)
437  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);
438  else
439  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);
440  if(len != len1)
441  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);
442  break;}
443  default:
445  for(i=0; i<len; i++){
446  float v=0;
447  for(j=0; j<s->matrix_ch[out_i][0]; j++){
448  in_i= s->matrix_ch[out_i][1+j];
449  v+= ((float*)in->ch[in_i])[i] * s->matrix[out_i][in_i];
450  }
451  ((float*)out->ch[out_i])[i]= v;
452  }
453  }else if(s->int_sample_fmt == AV_SAMPLE_FMT_DBLP){
454  for(i=0; i<len; i++){
455  double v=0;
456  for(j=0; j<s->matrix_ch[out_i][0]; j++){
457  in_i= s->matrix_ch[out_i][1+j];
458  v+= ((double*)in->ch[in_i])[i] * s->matrix[out_i][in_i];
459  }
460  ((double*)out->ch[out_i])[i]= v;
461  }
462  }else{
463  for(i=0; i<len; i++){
464  int v=0;
465  for(j=0; j<s->matrix_ch[out_i][0]; j++){
466  in_i= s->matrix_ch[out_i][1+j];
467  v+= ((int16_t*)in->ch[in_i])[i] * s->matrix32[out_i][in_i];
468  }
469  ((int16_t*)out->ch[out_i])[i]= (v + 16384)>>15;
470  }
471  }
472  }
473  }
474  return 0;
475 }