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 #define TEMPLATE_CLIP
36 #include "rematrix_template.c"
37 #undef TEMPLATE_CLIP
38 #undef TEMPLATE_REMATRIX_S16
39 
40 #define TEMPLATE_REMATRIX_S32
41 #include "rematrix_template.c"
42 #undef TEMPLATE_REMATRIX_S32
43 
44 #define FRONT_LEFT 0
45 #define FRONT_RIGHT 1
46 #define FRONT_CENTER 2
47 #define LOW_FREQUENCY 3
48 #define BACK_LEFT 4
49 #define BACK_RIGHT 5
50 #define FRONT_LEFT_OF_CENTER 6
51 #define FRONT_RIGHT_OF_CENTER 7
52 #define BACK_CENTER 8
53 #define SIDE_LEFT 9
54 #define SIDE_RIGHT 10
55 #define TOP_CENTER 11
56 #define TOP_FRONT_LEFT 12
57 #define TOP_FRONT_CENTER 13
58 #define TOP_FRONT_RIGHT 14
59 #define TOP_BACK_LEFT 15
60 #define TOP_BACK_CENTER 16
61 #define TOP_BACK_RIGHT 17
62 #define NUM_NAMED_CHANNELS 18
63 
64 int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride)
65 {
66  int nb_in, nb_out, in, out;
67 
68  if (!s || s->in_convert) // s needs to be allocated but not initialized
69  return AVERROR(EINVAL);
70  memset(s->matrix, 0, sizeof(s->matrix));
71  memset(s->matrix_flt, 0, sizeof(s->matrix_flt));
74  for (out = 0; out < nb_out; out++) {
75  for (in = 0; in < nb_in; in++)
76  s->matrix_flt[out][in] = s->matrix[out][in] = matrix[in];
77  matrix += stride;
78  }
79  s->rematrix_custom = 1;
80  return 0;
81 }
82 
83 static int even(int64_t layout){
84  if(!layout) return 1;
85  if(layout&(layout-1)) return 1;
86  return 0;
87 }
88 
89 static int clean_layout(void *s, int64_t layout){
90  if(layout && layout != AV_CH_FRONT_CENTER && !(layout&(layout-1))) {
91  char buf[128];
92  av_get_channel_layout_string(buf, sizeof(buf), -1, layout);
93  av_log(s, AV_LOG_VERBOSE, "Treating %s as mono\n", buf);
94  return AV_CH_FRONT_CENTER;
95  }
96 
97  return layout;
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;
110  return 0;
112  return 0;
113 
114  return 1;
115 }
116 
117 av_cold int swr_build_matrix(uint64_t in_ch_layout_param, uint64_t out_ch_layout_param,
118  double center_mix_level, double surround_mix_level,
119  double lfe_mix_level, double maxval,
120  double rematrix_volume, double *matrix_param,
121  int stride, enum AVMatrixEncoding matrix_encoding, void *log_context)
122 {
123  int i, j, out_i;
124  double matrix[NUM_NAMED_CHANNELS][NUM_NAMED_CHANNELS]={{0}};
125  int64_t unaccounted, in_ch_layout, out_ch_layout;
126  double maxcoef=0;
127  char buf[128];
128 
129  in_ch_layout = clean_layout(log_context, in_ch_layout_param);
130  out_ch_layout = clean_layout(log_context, out_ch_layout_param);
131 
132  if( out_ch_layout == AV_CH_LAYOUT_STEREO_DOWNMIX
133  && (in_ch_layout & AV_CH_LAYOUT_STEREO_DOWNMIX) == 0
134  )
135  out_ch_layout = AV_CH_LAYOUT_STEREO;
136 
137  if( in_ch_layout == AV_CH_LAYOUT_STEREO_DOWNMIX
138  && (out_ch_layout & AV_CH_LAYOUT_STEREO_DOWNMIX) == 0
139  )
140  in_ch_layout = AV_CH_LAYOUT_STEREO;
141 
142  if(!sane_layout(in_ch_layout)){
143  av_get_channel_layout_string(buf, sizeof(buf), -1, in_ch_layout_param);
144  av_log(log_context, AV_LOG_ERROR, "Input channel layout '%s' is not supported\n", buf);
145  return AVERROR(EINVAL);
146  }
147 
148  if(!sane_layout(out_ch_layout)){
149  av_get_channel_layout_string(buf, sizeof(buf), -1, out_ch_layout_param);
150  av_log(log_context, AV_LOG_ERROR, "Output channel layout '%s' is not supported\n", buf);
151  return AVERROR(EINVAL);
152  }
153 
154  for(i=0; i<FF_ARRAY_ELEMS(matrix); i++){
155  if(in_ch_layout & out_ch_layout & (1ULL<<i))
156  matrix[i][i]= 1.0;
157  }
158 
159  unaccounted= in_ch_layout & ~out_ch_layout;
160 
161 //FIXME implement dolby surround
162 //FIXME implement full ac3
163 
164 
165  if(unaccounted & AV_CH_FRONT_CENTER){
166  if((out_ch_layout & AV_CH_LAYOUT_STEREO) == AV_CH_LAYOUT_STEREO){
167  if(in_ch_layout & AV_CH_LAYOUT_STEREO) {
168  matrix[ FRONT_LEFT][FRONT_CENTER]+= center_mix_level;
169  matrix[FRONT_RIGHT][FRONT_CENTER]+= center_mix_level;
170  } else {
171  matrix[ FRONT_LEFT][FRONT_CENTER]+= M_SQRT1_2;
173  }
174  }else
175  av_assert0(0);
176  }
177  if(unaccounted & AV_CH_LAYOUT_STEREO){
178  if(out_ch_layout & AV_CH_FRONT_CENTER){
179  matrix[FRONT_CENTER][ FRONT_LEFT]+= M_SQRT1_2;
181  if(in_ch_layout & AV_CH_FRONT_CENTER)
182  matrix[FRONT_CENTER][ FRONT_CENTER] = center_mix_level*sqrt(2);
183  }else
184  av_assert0(0);
185  }
186 
187  if(unaccounted & AV_CH_BACK_CENTER){
188  if(out_ch_layout & AV_CH_BACK_LEFT){
189  matrix[ BACK_LEFT][BACK_CENTER]+= M_SQRT1_2;
190  matrix[BACK_RIGHT][BACK_CENTER]+= M_SQRT1_2;
191  }else if(out_ch_layout & AV_CH_SIDE_LEFT){
192  matrix[ SIDE_LEFT][BACK_CENTER]+= M_SQRT1_2;
193  matrix[SIDE_RIGHT][BACK_CENTER]+= M_SQRT1_2;
194  }else if(out_ch_layout & AV_CH_FRONT_LEFT){
195  if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY ||
196  matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
197  if (unaccounted & (AV_CH_BACK_LEFT | AV_CH_SIDE_LEFT)) {
198  matrix[FRONT_LEFT ][BACK_CENTER] -= surround_mix_level * M_SQRT1_2;
199  matrix[FRONT_RIGHT][BACK_CENTER] += surround_mix_level * M_SQRT1_2;
200  } else {
201  matrix[FRONT_LEFT ][BACK_CENTER] -= surround_mix_level;
202  matrix[FRONT_RIGHT][BACK_CENTER] += surround_mix_level;
203  }
204  } else {
205  matrix[ FRONT_LEFT][BACK_CENTER]+= surround_mix_level * M_SQRT1_2;
206  matrix[FRONT_RIGHT][BACK_CENTER]+= surround_mix_level * M_SQRT1_2;
207  }
208  }else if(out_ch_layout & AV_CH_FRONT_CENTER){
209  matrix[ FRONT_CENTER][BACK_CENTER]+= surround_mix_level * M_SQRT1_2;
210  }else
211  av_assert0(0);
212  }
213  if(unaccounted & AV_CH_BACK_LEFT){
214  if(out_ch_layout & AV_CH_BACK_CENTER){
215  matrix[BACK_CENTER][ BACK_LEFT]+= M_SQRT1_2;
216  matrix[BACK_CENTER][BACK_RIGHT]+= M_SQRT1_2;
217  }else if(out_ch_layout & AV_CH_SIDE_LEFT){
218  if(in_ch_layout & AV_CH_SIDE_LEFT){
219  matrix[ SIDE_LEFT][ BACK_LEFT]+= M_SQRT1_2;
220  matrix[SIDE_RIGHT][BACK_RIGHT]+= M_SQRT1_2;
221  }else{
222  matrix[ SIDE_LEFT][ BACK_LEFT]+= 1.0;
223  matrix[SIDE_RIGHT][BACK_RIGHT]+= 1.0;
224  }
225  }else if(out_ch_layout & AV_CH_FRONT_LEFT){
226  if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY) {
227  matrix[FRONT_LEFT ][BACK_LEFT ] -= surround_mix_level * M_SQRT1_2;
228  matrix[FRONT_LEFT ][BACK_RIGHT] -= surround_mix_level * M_SQRT1_2;
229  matrix[FRONT_RIGHT][BACK_LEFT ] += surround_mix_level * M_SQRT1_2;
230  matrix[FRONT_RIGHT][BACK_RIGHT] += surround_mix_level * M_SQRT1_2;
231  } else if (matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
232  matrix[FRONT_LEFT ][BACK_LEFT ] -= surround_mix_level * SQRT3_2;
233  matrix[FRONT_LEFT ][BACK_RIGHT] -= surround_mix_level * M_SQRT1_2;
234  matrix[FRONT_RIGHT][BACK_LEFT ] += surround_mix_level * M_SQRT1_2;
235  matrix[FRONT_RIGHT][BACK_RIGHT] += surround_mix_level * SQRT3_2;
236  } else {
237  matrix[ FRONT_LEFT][ BACK_LEFT] += surround_mix_level;
238  matrix[FRONT_RIGHT][BACK_RIGHT] += surround_mix_level;
239  }
240  }else if(out_ch_layout & AV_CH_FRONT_CENTER){
241  matrix[ FRONT_CENTER][BACK_LEFT ]+= surround_mix_level*M_SQRT1_2;
242  matrix[ FRONT_CENTER][BACK_RIGHT]+= surround_mix_level*M_SQRT1_2;
243  }else
244  av_assert0(0);
245  }
246 
247  if(unaccounted & AV_CH_SIDE_LEFT){
248  if(out_ch_layout & AV_CH_BACK_LEFT){
249  /* if back channels do not exist in the input, just copy side
250  channels to back channels, otherwise mix side into back */
251  if (in_ch_layout & AV_CH_BACK_LEFT) {
252  matrix[BACK_LEFT ][SIDE_LEFT ] += M_SQRT1_2;
253  matrix[BACK_RIGHT][SIDE_RIGHT] += M_SQRT1_2;
254  } else {
255  matrix[BACK_LEFT ][SIDE_LEFT ] += 1.0;
256  matrix[BACK_RIGHT][SIDE_RIGHT] += 1.0;
257  }
258  }else if(out_ch_layout & AV_CH_BACK_CENTER){
259  matrix[BACK_CENTER][ SIDE_LEFT]+= M_SQRT1_2;
260  matrix[BACK_CENTER][SIDE_RIGHT]+= M_SQRT1_2;
261  }else if(out_ch_layout & AV_CH_FRONT_LEFT){
262  if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY) {
263  matrix[FRONT_LEFT ][SIDE_LEFT ] -= surround_mix_level * M_SQRT1_2;
264  matrix[FRONT_LEFT ][SIDE_RIGHT] -= surround_mix_level * M_SQRT1_2;
265  matrix[FRONT_RIGHT][SIDE_LEFT ] += surround_mix_level * M_SQRT1_2;
266  matrix[FRONT_RIGHT][SIDE_RIGHT] += surround_mix_level * M_SQRT1_2;
267  } else if (matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
268  matrix[FRONT_LEFT ][SIDE_LEFT ] -= surround_mix_level * SQRT3_2;
269  matrix[FRONT_LEFT ][SIDE_RIGHT] -= surround_mix_level * M_SQRT1_2;
270  matrix[FRONT_RIGHT][SIDE_LEFT ] += surround_mix_level * M_SQRT1_2;
271  matrix[FRONT_RIGHT][SIDE_RIGHT] += surround_mix_level * SQRT3_2;
272  } else {
273  matrix[ FRONT_LEFT][ SIDE_LEFT] += surround_mix_level;
274  matrix[FRONT_RIGHT][SIDE_RIGHT] += surround_mix_level;
275  }
276  }else if(out_ch_layout & AV_CH_FRONT_CENTER){
277  matrix[ FRONT_CENTER][SIDE_LEFT ]+= surround_mix_level * M_SQRT1_2;
278  matrix[ FRONT_CENTER][SIDE_RIGHT]+= surround_mix_level * M_SQRT1_2;
279  }else
280  av_assert0(0);
281  }
282 
283  if(unaccounted & AV_CH_FRONT_LEFT_OF_CENTER){
284  if(out_ch_layout & AV_CH_FRONT_LEFT){
285  matrix[ FRONT_LEFT][ FRONT_LEFT_OF_CENTER]+= 1.0;
286  matrix[FRONT_RIGHT][FRONT_RIGHT_OF_CENTER]+= 1.0;
287  }else if(out_ch_layout & AV_CH_FRONT_CENTER){
290  }else
291  av_assert0(0);
292  }
293  /* mix LFE into front left/right or center */
294  if (unaccounted & AV_CH_LOW_FREQUENCY) {
295  if (out_ch_layout & AV_CH_FRONT_CENTER) {
296  matrix[FRONT_CENTER][LOW_FREQUENCY] += lfe_mix_level;
297  } else if (out_ch_layout & AV_CH_FRONT_LEFT) {
298  matrix[FRONT_LEFT ][LOW_FREQUENCY] += lfe_mix_level * M_SQRT1_2;
299  matrix[FRONT_RIGHT][LOW_FREQUENCY] += lfe_mix_level * M_SQRT1_2;
300  } else
301  av_assert0(0);
302  }
303 
304  for(out_i=i=0; i<64; i++){
305  double sum=0;
306  int in_i=0;
307  if((out_ch_layout & (1ULL<<i)) == 0)
308  continue;
309  for(j=0; j<64; j++){
310  if((in_ch_layout & (1ULL<<j)) == 0)
311  continue;
312  if (i < FF_ARRAY_ELEMS(matrix) && j < FF_ARRAY_ELEMS(matrix[0]))
313  matrix_param[stride*out_i + in_i] = matrix[i][j];
314  else
315  matrix_param[stride*out_i + in_i] = i == j && (in_ch_layout & out_ch_layout & (1ULL<<i));
316  sum += fabs(matrix_param[stride*out_i + in_i]);
317  in_i++;
318  }
319  maxcoef= FFMAX(maxcoef, sum);
320  out_i++;
321  }
322  if(rematrix_volume < 0)
323  maxcoef = -rematrix_volume;
324 
325  if(maxcoef > maxval || rematrix_volume < 0){
326  maxcoef /= maxval;
327  for(i=0; i<SWR_CH_MAX; i++)
328  for(j=0; j<SWR_CH_MAX; j++){
329  matrix_param[stride*i + j] /= maxcoef;
330  }
331  }
332 
333  if(rematrix_volume > 0){
334  for(i=0; i<SWR_CH_MAX; i++)
335  for(j=0; j<SWR_CH_MAX; j++){
336  matrix_param[stride*i + j] *= rematrix_volume;
337  }
338  }
339 
340  av_log(log_context, AV_LOG_DEBUG, "Matrix coefficients:\n");
341  for(i=0; i<av_get_channel_layout_nb_channels(out_ch_layout); i++){
342  const char *c =
344  av_log(log_context, AV_LOG_DEBUG, "%s: ", c ? c : "?");
345  for(j=0; j<av_get_channel_layout_nb_channels(in_ch_layout); j++){
347  av_log(log_context, AV_LOG_DEBUG, "%s:%f ", c ? c : "?", matrix_param[stride*i + j]);
348  }
349  av_log(log_context, AV_LOG_DEBUG, "\n");
350  }
351  return 0;
352 }
353 
355 {
356  double maxval;
357  int ret;
358 
359  if (s->rematrix_maxval > 0) {
360  maxval = s->rematrix_maxval;
363  maxval = 1.0;
364  } else
365  maxval = INT_MAX;
366 
367  memset(s->matrix, 0, sizeof(s->matrix));
369  s->clev, s->slev, s->lfe_mix_level,
370  maxval, s->rematrix_volume, (double*)s->matrix,
371  s->matrix[1] - s->matrix[0], s->matrix_encoding, s);
372 
373  if (ret >= 0 && s->int_sample_fmt == AV_SAMPLE_FMT_FLTP) {
374  int i;
375  for (i = 0; i < FF_ARRAY_ELEMS(s->matrix[0])*FF_ARRAY_ELEMS(s->matrix[0]); i++)
376  s->matrix_flt[0][i] = s->matrix[0][i];
377  }
378 
379  return ret;
380 }
381 
383  int i, j;
386 
387  s->mix_any_f = NULL;
388 
389  if (!s->rematrix_custom) {
390  int r = auto_matrix(s);
391  if (r)
392  return r;
393  }
394  if (s->midbuf.fmt == AV_SAMPLE_FMT_S16P){
395  int maxsum = 0;
396  s->native_matrix = av_calloc(nb_in * nb_out, sizeof(int));
397  s->native_one = av_mallocz(sizeof(int));
398  if (!s->native_matrix || !s->native_one)
399  return AVERROR(ENOMEM);
400  for (i = 0; i < nb_out; i++) {
401  double rem = 0;
402  int sum = 0;
403 
404  for (j = 0; j < nb_in; j++) {
405  double target = s->matrix[i][j] * 32768 + rem;
406  ((int*)s->native_matrix)[i * nb_in + j] = lrintf(target);
407  rem += target - ((int*)s->native_matrix)[i * nb_in + j];
408  sum += FFABS(((int*)s->native_matrix)[i * nb_in + j]);
409  }
410  maxsum = FFMAX(maxsum, sum);
411  }
412  *((int*)s->native_one) = 32768;
413  if (maxsum <= 32768) {
414  s->mix_1_1_f = (mix_1_1_func_type*)copy_s16;
415  s->mix_2_1_f = (mix_2_1_func_type*)sum2_s16;
416  s->mix_any_f = (mix_any_func_type*)get_mix_any_func_s16(s);
417  } else {
418  s->mix_1_1_f = (mix_1_1_func_type*)copy_clip_s16;
419  s->mix_2_1_f = (mix_2_1_func_type*)sum2_clip_s16;
420  s->mix_any_f = (mix_any_func_type*)get_mix_any_func_clip_s16(s);
421  }
422  }else if(s->midbuf.fmt == AV_SAMPLE_FMT_FLTP){
423  s->native_matrix = av_calloc(nb_in * nb_out, sizeof(float));
424  s->native_one = av_mallocz(sizeof(float));
425  if (!s->native_matrix || !s->native_one)
426  return AVERROR(ENOMEM);
427  for (i = 0; i < nb_out; i++)
428  for (j = 0; j < nb_in; j++)
429  ((float*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
430  *((float*)s->native_one) = 1.0;
431  s->mix_1_1_f = (mix_1_1_func_type*)copy_float;
432  s->mix_2_1_f = (mix_2_1_func_type*)sum2_float;
433  s->mix_any_f = (mix_any_func_type*)get_mix_any_func_float(s);
434  }else if(s->midbuf.fmt == AV_SAMPLE_FMT_DBLP){
435  s->native_matrix = av_calloc(nb_in * nb_out, sizeof(double));
436  s->native_one = av_mallocz(sizeof(double));
437  if (!s->native_matrix || !s->native_one)
438  return AVERROR(ENOMEM);
439  for (i = 0; i < nb_out; i++)
440  for (j = 0; j < nb_in; j++)
441  ((double*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
442  *((double*)s->native_one) = 1.0;
443  s->mix_1_1_f = (mix_1_1_func_type*)copy_double;
444  s->mix_2_1_f = (mix_2_1_func_type*)sum2_double;
445  s->mix_any_f = (mix_any_func_type*)get_mix_any_func_double(s);
446  }else if(s->midbuf.fmt == AV_SAMPLE_FMT_S32P){
447  // Only for dithering currently
448 // s->native_matrix = av_calloc(nb_in * nb_out, sizeof(double));
449  s->native_one = av_mallocz(sizeof(int));
450  if (!s->native_one)
451  return AVERROR(ENOMEM);
452 // for (i = 0; i < nb_out; i++)
453 // for (j = 0; j < nb_in; j++)
454 // ((double*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
455  *((int*)s->native_one) = 32768;
456  s->mix_1_1_f = (mix_1_1_func_type*)copy_s32;
457  s->mix_2_1_f = (mix_2_1_func_type*)sum2_s32;
458  s->mix_any_f = (mix_any_func_type*)get_mix_any_func_s32(s);
459  }else
460  av_assert0(0);
461  //FIXME quantize for integeres
462  for (i = 0; i < SWR_CH_MAX; i++) {
463  int ch_in=0;
464  for (j = 0; j < SWR_CH_MAX; j++) {
465  s->matrix32[i][j]= lrintf(s->matrix[i][j] * 32768);
466  if(s->matrix[i][j])
467  s->matrix_ch[i][++ch_in]= j;
468  }
469  s->matrix_ch[i][0]= ch_in;
470  }
471 
472  if(HAVE_YASM && HAVE_MMX)
473  return swri_rematrix_init_x86(s);
474 
475  return 0;
476 }
477 
479  av_freep(&s->native_matrix);
480  av_freep(&s->native_one);
483 }
484 
485 int swri_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy){
486  int out_i, in_i, i, j;
487  int len1 = 0;
488  int off = 0;
489 
490  if(s->mix_any_f) {
491  s->mix_any_f(out->ch, (const uint8_t **)in->ch, s->native_matrix, len);
492  return 0;
493  }
494 
495  if(s->mix_2_1_simd || s->mix_1_1_simd){
496  len1= len&~15;
497  off = len1 * out->bps;
498  }
499 
501  av_assert0(!s-> in_ch_layout || in ->ch_count == av_get_channel_layout_nb_channels(s-> in_ch_layout));
502 
503  for(out_i=0; out_i<out->ch_count; out_i++){
504  switch(s->matrix_ch[out_i][0]){
505  case 0:
506  if(mustcopy)
507  memset(out->ch[out_i], 0, len * av_get_bytes_per_sample(s->int_sample_fmt));
508  break;
509  case 1:
510  in_i= s->matrix_ch[out_i][1];
511  if(s->matrix[out_i][in_i]!=1.0){
512  if(s->mix_1_1_simd && len1)
513  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);
514  if(len != len1)
515  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);
516  }else if(mustcopy){
517  memcpy(out->ch[out_i], in->ch[in_i], len*out->bps);
518  }else{
519  out->ch[out_i]= in->ch[in_i];
520  }
521  break;
522  case 2: {
523  int in_i1 = s->matrix_ch[out_i][1];
524  int in_i2 = s->matrix_ch[out_i][2];
525  if(s->mix_2_1_simd && len1)
526  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);
527  else
528  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);
529  if(len != len1)
530  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);
531  break;}
532  default:
534  for(i=0; i<len; i++){
535  float v=0;
536  for(j=0; j<s->matrix_ch[out_i][0]; j++){
537  in_i= s->matrix_ch[out_i][1+j];
538  v+= ((float*)in->ch[in_i])[i] * s->matrix_flt[out_i][in_i];
539  }
540  ((float*)out->ch[out_i])[i]= v;
541  }
542  }else if(s->int_sample_fmt == AV_SAMPLE_FMT_DBLP){
543  for(i=0; i<len; i++){
544  double v=0;
545  for(j=0; j<s->matrix_ch[out_i][0]; j++){
546  in_i= s->matrix_ch[out_i][1+j];
547  v+= ((double*)in->ch[in_i])[i] * s->matrix[out_i][in_i];
548  }
549  ((double*)out->ch[out_i])[i]= v;
550  }
551  }else{
552  for(i=0; i<len; i++){
553  int v=0;
554  for(j=0; j<s->matrix_ch[out_i][0]; j++){
555  in_i= s->matrix_ch[out_i][1+j];
556  v+= ((int16_t*)in->ch[in_i])[i] * s->matrix32[out_i][in_i];
557  }
558  ((int16_t*)out->ch[out_i])[i]= (v + 16384)>>15;
559  }
560  }
561  }
562  }
563  return 0;
564 }
float, planar
Definition: samplefmt.h:69
struct AudioConvert * in_convert
input conversion context
#define NULL
Definition: coverity.c:32
#define BACK_RIGHT
Definition: rematrix.c:49
const char * s
Definition: avisynth_c.h:768
#define FRONT_RIGHT
Definition: rematrix.c:45
enum AVSampleFormat int_sample_fmt
internal sample format (AV_SAMPLE_FMT_FLTP or AV_SAMPLE_FMT_S16P)
Audio buffer used for intermediate storage between conversion phases.
Definition: audio_data.h:37
#define FRONT_RIGHT_OF_CENTER
Definition: rematrix.c:51
#define AV_CH_LAYOUT_SURROUND
int ch_count
number of channels
void( mix_2_1_func_type)(void *out, const void *in1, const void *in2, void *coeffp, integer index1, integer index2, integer len)
#define SWR_CH_MAX
Definition: af_amerge.c:35
#define M_SQRT1_2
Definition: mathematics.h:58
int rematrix_custom
flag to indicate that a custom matrix has been defined
int swri_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy)
Definition: rematrix.c:485
double, planar
Definition: samplefmt.h:70
void( mix_1_1_func_type)(void *out, const void *in, void *coeffp, integer index, integer len)
#define LOW_FREQUENCY
Definition: rematrix.c:47
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
Definition: mem.c:252
#define AV_CH_LAYOUT_STEREO
#define SQRT3_2
#define SIDE_RIGHT
Definition: rematrix.c:54
av_cold int swr_build_matrix(uint64_t in_ch_layout_param, uint64_t out_ch_layout_param, double center_mix_level, double surround_mix_level, double lfe_mix_level, double maxval, double rematrix_volume, double *matrix_param, int stride, enum AVMatrixEncoding matrix_encoding, void *log_context)
Generate a channel mixing matrix.
Definition: rematrix.c:117
static int clean_layout(void *s, int64_t layout)
Definition: rematrix.c:89
void * av_calloc(size_t nmemb, size_t size)
Non-inlined equivalent of av_mallocz_array().
Definition: mem.c:260
int av_get_channel_layout_nb_channels(uint64_t channel_layout)
Return the number of channels in the channel layout.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
av_cold int swri_rematrix_init(SwrContext *s)
Definition: rematrix.c:382
uint8_t
#define av_cold
Definition: attributes.h:82
enum AVSampleFormat fmt
sample format
#define BACK_CENTER
Definition: rematrix.c:52
#define AV_CH_LOW_FREQUENCY
uint8_t * native_simd_one
#define AV_LOG_VERBOSE
Detailed information.
Definition: log.h:192
#define lrintf(x)
Definition: libm_mips.h:70
#define AV_CH_BACK_LEFT
enum AVSampleFormat out_sample_fmt
output sample format
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:176
int matrix_encoding
matrixed stereo encoding
float slev
surround mixing level
#define FRONT_CENTER
Definition: rematrix.c:46
#define AVERROR(e)
Definition: error.h:43
int64_t user_in_ch_layout
User set input channel layout.
The libswresample context.
#define SIDE_LEFT
Definition: rematrix.c:53
int swri_rematrix_init_x86(struct SwrContext *s)
const char * r
Definition: vf_curves.c:111
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition: log.h:197
float clev
center mixing level
simple assert() macros that are a bit more flexible than ISO C assert().
mix_2_1_func_type * mix_2_1_simd
#define FFMAX(a, b)
Definition: common.h:94
#define NUM_NAMED_CHANNELS
Definition: rematrix.c:62
int32_t matrix32[SWR_CH_MAX][SWR_CH_MAX]
17.15 fixed point rematrixing coefficients
AudioData midbuf
intermediate audio data (postin/preout)
audio channel layout utility functions
#define AV_CH_LAYOUT_STEREO_DOWNMIX
#define FRONT_LEFT_OF_CENTER
Definition: rematrix.c:50
signed 32 bits, planar
Definition: samplefmt.h:68
int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride)
Set a customized remix matrix.
Definition: rematrix.c:64
mix_1_1_func_type * mix_1_1_f
mix_1_1_func_type * mix_1_1_simd
int64_t out_ch_layout
output channel layout
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
int bps
bytes per sample
#define AV_CH_FRONT_LEFT_OF_CENTER
uint8_t * native_matrix
mix_any_func_type * mix_any_f
#define AV_CH_FRONT_CENTER
#define FF_ARRAY_ELEMS(a)
#define AV_CH_FRONT_RIGHT_OF_CENTER
void av_get_channel_layout_string(char *buf, int buf_size, int nb_channels, uint64_t channel_layout)
Return a description of a channel layout.
static av_cold int auto_matrix(SwrContext *s)
Definition: rematrix.c:354
#define AV_CH_FRONT_LEFT
uint8_t pi<< 24) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0f/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_U8, uint8_t,(*(constuint8_t *) pi-0x80)*(1.0/(1<< 7))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S16, int16_t,(*(constint16_t *) pi >>8)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0f/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S16, int16_t,*(constint16_t *) pi *(1.0/(1<< 15))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_S32, int32_t,(*(constint32_t *) pi >>24)+0x80) CONV_FUNC_GROUP(AV_SAMPLE_FMT_FLT, float, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0f/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_DBL, double, AV_SAMPLE_FMT_S32, int32_t,*(constint32_t *) pi *(1.0/(1U<< 31))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_FLT, float, av_clip_uint8(lrintf(*(constfloat *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_FLT, float, av_clip_int16(lrintf(*(constfloat *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_FLT, float, av_clipl_int32(llrintf(*(constfloat *) pi *(1U<< 31)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_U8, uint8_t, AV_SAMPLE_FMT_DBL, double, av_clip_uint8(lrint(*(constdouble *) pi *(1<< 7))+0x80)) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S16, int16_t, AV_SAMPLE_FMT_DBL, double, av_clip_int16(lrint(*(constdouble *) pi *(1<< 15)))) CONV_FUNC_GROUP(AV_SAMPLE_FMT_S32, int32_t, AV_SAMPLE_FMT_DBL, double, av_clipl_int32(llrint(*(constdouble *) pi *(1U<< 31))))#defineSET_CONV_FUNC_GROUP(ofmt, ifmt) staticvoidset_generic_function(AudioConvert *ac){}voidff_audio_convert_free(AudioConvert **ac){if(!*ac) return;ff_dither_free(&(*ac) ->dc);av_freep(ac);}AudioConvert *ff_audio_convert_alloc(AVAudioResampleContext *avr, enumAVSampleFormatout_fmt, enumAVSampleFormatin_fmt, intchannels, intsample_rate, intapply_map){AudioConvert *ac;intin_planar, out_planar;ac=av_mallocz(sizeof(*ac));if(!ac) returnNULL;ac->avr=avr;ac->out_fmt=out_fmt;ac->in_fmt=in_fmt;ac->channels=channels;ac->apply_map=apply_map;if(avr->dither_method!=AV_RESAMPLE_DITHER_NONE &&av_get_packed_sample_fmt(out_fmt)==AV_SAMPLE_FMT_S16 &&av_get_bytes_per_sample(in_fmt)>2){ac->dc=ff_dither_alloc(avr, out_fmt, in_fmt, channels, sample_rate, apply_map);if(!ac->dc){av_free(ac);returnNULL;}returnac;}in_planar=ff_sample_fmt_is_planar(in_fmt, channels);out_planar=ff_sample_fmt_is_planar(out_fmt, channels);if(in_planar==out_planar){ac->func_type=CONV_FUNC_TYPE_FLAT;ac->planes=in_planar?ac->channels:1;}elseif(in_planar) ac->func_type=CONV_FUNC_TYPE_INTERLEAVE;elseac->func_type=CONV_FUNC_TYPE_DEINTERLEAVE;set_generic_function(ac);if(ARCH_AARCH64) ff_audio_convert_init_aarch64(ac);if(ARCH_ARM) ff_audio_convert_init_arm(ac);if(ARCH_X86) ff_audio_convert_init_x86(ac);returnac;}intff_audio_convert(AudioConvert *ac, AudioData *out, AudioData *in){intuse_generic=1;intlen=in->nb_samples;intp;if(ac->dc){av_log(ac->avr, AV_LOG_TRACE,"%dsamples-audio_convert:%sto%s(dithered)\n", len, av_get_sample_fmt_name(ac->in_fmt), av_get_sample_fmt_name(ac->out_fmt));returnff_convert_dither(ac-> in
void * buf
Definition: avisynth_c.h:690
float matrix_flt[SWR_CH_MAX][SWR_CH_MAX]
single precision floating point rematrixing coefficients
static int sane_layout(int64_t layout)
Definition: rematrix.c:100
#define BACK_LEFT
Definition: rematrix.c:48
#define AV_CH_BACK_CENTER
double matrix[SWR_CH_MAX][SWR_CH_MAX]
floating point rematrixing coefficients
uint8_t * native_one
#define AV_CH_SIDE_RIGHT
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition: samplefmt.c:106
int64_t in_ch_layout
input channel layout
GLint GLenum GLboolean GLsizei stride
Definition: opengl_enc.c:105
enum AVSampleFormat av_get_packed_sample_fmt(enum AVSampleFormat sample_fmt)
Get the packed alternative form of the given sample format.
Definition: samplefmt.c:75
uint8_t * native_simd_matrix
uint64_t av_channel_layout_extract_channel(uint64_t channel_layout, int index)
Get the channel with the given index in channel_layout.
float lfe_mix_level
LFE mixing level.
void( mix_any_func_type)(uint8_t **out, const uint8_t **in1, void *coeffp, integer len)
const char * av_get_channel_name(uint64_t channel)
Get the name of a given channel.
av_cold void swri_rematrix_free(SwrContext *s)
Definition: rematrix.c:478
int len
float rematrix_maxval
maximum value for rematrixing output
float rematrix_volume
rematrixing volume coefficient
static AVCodec * c
uint64_t layout
mix_2_1_func_type * mix_2_1_f
#define AV_CH_FRONT_RIGHT
FILE * out
Definition: movenc.c:54
#define av_freep(p)
signed 16 bits, planar
Definition: samplefmt.h:67
static int even(int64_t layout)
Definition: rematrix.c:83
AVMatrixEncoding
#define AV_CH_SIDE_LEFT
#define FRONT_LEFT
Definition: rematrix.c:44
uint8_t matrix_ch[SWR_CH_MAX][SWR_CH_MAX+1]
Lists of input channels per output channel that have non zero rematrixing coefficients.
uint8_t * ch[SWR_CH_MAX]
samples buffer per channel
int64_t user_out_ch_layout
User set output channel layout.
#define AV_CH_BACK_RIGHT