FFmpeg
Loading...
Searching...
No Matches
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#include "libavutil/mem.h"
25
26#define TEMPLATE_REMATRIX_FLT
27#include "rematrix_template.c"
28#undef TEMPLATE_REMATRIX_FLT
29
30#define TEMPLATE_REMATRIX_DBL
31#include "rematrix_template.c"
32#undef TEMPLATE_REMATRIX_DBL
33
34#define TEMPLATE_REMATRIX_S16
35#include "rematrix_template.c"
36#define TEMPLATE_CLIP
37#include "rematrix_template.c"
38#undef TEMPLATE_CLIP
39#undef TEMPLATE_REMATRIX_S16
40
41#define TEMPLATE_REMATRIX_S32
42#include "rematrix_template.c"
43#undef TEMPLATE_REMATRIX_S32
44
45#define FRONT_LEFT 0
46#define FRONT_RIGHT 1
47#define FRONT_CENTER 2
48#define LOW_FREQUENCY 3
49#define BACK_LEFT 4
50#define BACK_RIGHT 5
51#define FRONT_LEFT_OF_CENTER 6
52#define FRONT_RIGHT_OF_CENTER 7
53#define BACK_CENTER 8
54#define SIDE_LEFT 9
55#define SIDE_RIGHT 10
56#define TOP_CENTER 11
57#define TOP_FRONT_LEFT 12
58#define TOP_FRONT_CENTER 13
59#define TOP_FRONT_RIGHT 14
60#define TOP_BACK_LEFT 15
61#define TOP_BACK_CENTER 16
62#define TOP_BACK_RIGHT 17
63#define LOW_FREQUENCY_2 35
64#define TOP_SIDE_LEFT 36
65#define TOP_SIDE_RIGHT 37
66#define BOTTOM_FRONT_CENTER 38
67#define BOTTOM_FRONT_LEFT 39
68#define BOTTOM_FRONT_RIGHT 40
69#define NUM_NAMED_CHANNELS 41
70
71int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride)
72{
73 int nb_in, nb_out, in, out;
74
75 if (!s || s->in_convert || // s needs to be allocated but not initialized
76 swri_check_chlayout(s, &s->user_in_chlayout , "input") ||
77 swri_check_chlayout(s, &s->user_out_chlayout, "output")
78 )
79 return AVERROR(EINVAL);
80 memset(s->matrix, 0, sizeof(s->matrix));
81
82 nb_in = s->user_in_chlayout.nb_channels;
83 nb_out = s->user_out_chlayout.nb_channels;
84 for (out = 0; out < nb_out; out++) {
85 for (in = 0; in < nb_in; in++)
86 s->matrix[out][in] = matrix[in];
87 matrix += stride;
88 }
89 s->rematrix_custom = 1;
90 return 0;
91}
92
93static int even(int64_t layout){
94 if(!layout) return 1;
95 if(layout&(layout-1)) return 1;
96 return 0;
97}
98
99static int clean_layout(AVChannelLayout *out, const AVChannelLayout *in, void *s)
100{
101 int ret = 0;
102
104 char buf[128];
105 av_channel_layout_describe(in, buf, sizeof(buf));
106 av_log(s, AV_LOG_VERBOSE, "Treating %s as mono\n", buf);
108 } else
109 ret = av_channel_layout_copy(out, in);
110
111 return ret;
112}
113
114static int sane_layout(const AVChannelLayout *ch_layout) {
115 if(ch_layout->nb_channels >= SWR_CH_MAX)
116 return 0;
117 if(ch_layout->order == AV_CHANNEL_ORDER_CUSTOM)
118 for (int i = 0; i < ch_layout->nb_channels; i++) {
119 enum AVChannel id = ch_layout->u.map[i].id;
120
121 if (id == AV_CHAN_UNUSED)
122 continue;
123 if (id >= 64)
124 return 0;
125 }
126 else if (ch_layout->order != AV_CHANNEL_ORDER_NATIVE)
127 return 0;
128 uint64_t mask = av_channel_layout_subset(ch_layout, ~(uint64_t)0);
129 if(!(mask & AV_CH_LAYOUT_SURROUND)) // at least 1 front speaker
130 return 0;
131 if (!even(mask & (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT))) // no asymmetric front
132 return 0;
133 if (!even(mask & (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT))) // no asymmetric side
134 return 0;
136 return 0;
138 return 0;
140 return 0;
142 return 0;
144 return 0;
146 return 0;
147
148 return 1;
149}
150
151static void build_matrix(const AVChannelLayout *in_ch_layout, const AVChannelLayout *out_ch_layout,
152 double center_mix_level, double surround_mix_level,
153 double lfe_mix_level, double maxval, double rematrix_volume, double *matrix_param,
154 ptrdiff_t stride, enum AVMatrixEncoding matrix_encoding)
155{
157 uint64_t in_mask = av_channel_layout_subset(in_ch_layout, ~(uint64_t)0);
158 uint64_t out_mask = av_channel_layout_subset(out_ch_layout, ~(uint64_t)0);
159 uint64_t unaccounted = in_mask & ~out_mask;
160 double maxcoef=0;
161 int i, j;
162
163 if (in_ch_layout->order == AV_CHANNEL_ORDER_CUSTOM) {
164 for (j = 0; j < in_ch_layout->nb_channels; j++) {
165 if (in_ch_layout->u.map[j].id == AV_CHAN_UNUSED) {
166 /* the named-channel loop below cannot visit AV_CHAN_UNUSED.
167 * explicitly clear its column so callers may reuse a matrix. */
168 for (i = 0; i < out_ch_layout->nb_channels; i++)
169 matrix_param[stride * i + j] = 0.0;
170 }
171 }
172 }
173
174 if (out_ch_layout->order == AV_CHANNEL_ORDER_CUSTOM) {
175 for (i = 0; i < out_ch_layout->nb_channels; i++) {
176 if (out_ch_layout->u.map[i].id == AV_CHAN_UNUSED) {
177 /* the named-channel loop below cannot visit AV_CHAN_UNUSED.
178 * explicitly clear its row so callers may reuse a matrix. */
179 for (j = 0; j < in_ch_layout->nb_channels; j++)
180 matrix_param[stride * i + j] = 0.0;
181 }
182 }
183 }
184
185 for(i=0; i<FF_ARRAY_ELEMS(matrix); i++){
186 if (in_mask & out_mask & (1ULL << i))
187 matrix[i][i]= 1.0;
188 }
189
190//FIXME implement dolby surround
191//FIXME implement full ac3
192
193 if(unaccounted & AV_CH_FRONT_CENTER){
194 if ((out_mask & AV_CH_LAYOUT_STEREO) == AV_CH_LAYOUT_STEREO) {
195 if (in_mask & AV_CH_LAYOUT_STEREO) {
196 matrix[ FRONT_LEFT][FRONT_CENTER]+= center_mix_level;
197 matrix[FRONT_RIGHT][FRONT_CENTER]+= center_mix_level;
198 } else {
201 }
202 }else
203 av_assert0(0);
204 }
205 if(unaccounted & AV_CH_LAYOUT_STEREO){
206 if (out_mask & AV_CH_FRONT_CENTER) {
209 if (in_mask & AV_CH_FRONT_CENTER)
210 matrix[FRONT_CENTER][ FRONT_CENTER] = center_mix_level*sqrt(2);
211 }else
212 av_assert0(0);
213 }
214
215 if(unaccounted & AV_CH_BACK_CENTER){
216 if (out_mask & AV_CH_BACK_LEFT) {
219 } else if (out_mask & AV_CH_SIDE_LEFT) {
222 } else if (out_mask & AV_CH_FRONT_LEFT) {
223 if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY ||
224 matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
225 if (unaccounted & (AV_CH_BACK_LEFT | AV_CH_SIDE_LEFT)) {
226 matrix[FRONT_LEFT ][BACK_CENTER] -= surround_mix_level * M_SQRT1_2;
227 matrix[FRONT_RIGHT][BACK_CENTER] += surround_mix_level * M_SQRT1_2;
228 } else {
229 matrix[FRONT_LEFT ][BACK_CENTER] -= surround_mix_level;
230 matrix[FRONT_RIGHT][BACK_CENTER] += surround_mix_level;
231 }
232 } else {
233 matrix[ FRONT_LEFT][BACK_CENTER]+= surround_mix_level * M_SQRT1_2;
234 matrix[FRONT_RIGHT][BACK_CENTER]+= surround_mix_level * M_SQRT1_2;
235 }
236 } else if (out_mask & AV_CH_FRONT_CENTER) {
237 matrix[ FRONT_CENTER][BACK_CENTER]+= surround_mix_level * M_SQRT1_2;
238 }else
239 av_assert0(0);
240 }
241 if(unaccounted & AV_CH_BACK_LEFT){
242 if (out_mask & AV_CH_BACK_CENTER) {
245 } else if (out_mask & AV_CH_SIDE_LEFT) {
246 if (in_mask & AV_CH_SIDE_LEFT) {
249 }else{
250 matrix[ SIDE_LEFT][ BACK_LEFT]+= 1.0;
252 }
253 } else if (out_mask & AV_CH_FRONT_LEFT) {
254 if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY) {
255 matrix[FRONT_LEFT ][BACK_LEFT ] -= surround_mix_level * M_SQRT1_2;
256 matrix[FRONT_LEFT ][BACK_RIGHT] -= surround_mix_level * M_SQRT1_2;
257 matrix[FRONT_RIGHT][BACK_LEFT ] += surround_mix_level * M_SQRT1_2;
258 matrix[FRONT_RIGHT][BACK_RIGHT] += surround_mix_level * M_SQRT1_2;
259 } else if (matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
260 matrix[FRONT_LEFT ][BACK_LEFT ] -= surround_mix_level * SQRT3_2;
261 matrix[FRONT_LEFT ][BACK_RIGHT] -= surround_mix_level * M_SQRT1_2;
262 matrix[FRONT_RIGHT][BACK_LEFT ] += surround_mix_level * M_SQRT1_2;
263 matrix[FRONT_RIGHT][BACK_RIGHT] += surround_mix_level * SQRT3_2;
264 } else {
265 matrix[ FRONT_LEFT][ BACK_LEFT] += surround_mix_level;
266 matrix[FRONT_RIGHT][BACK_RIGHT] += surround_mix_level;
267 }
268 } else if (out_mask & AV_CH_FRONT_CENTER) {
269 matrix[ FRONT_CENTER][BACK_LEFT ]+= surround_mix_level*M_SQRT1_2;
270 matrix[ FRONT_CENTER][BACK_RIGHT]+= surround_mix_level*M_SQRT1_2;
271 }else
272 av_assert0(0);
273 }
274
275 if(unaccounted & AV_CH_SIDE_LEFT){
276 if (out_mask & AV_CH_BACK_LEFT) {
277 /* if back channels do not exist in the input, just copy side
278 channels to back channels, otherwise mix side into back */
279 if (in_mask & AV_CH_BACK_LEFT) {
282 } else {
283 matrix[BACK_LEFT ][SIDE_LEFT ] += 1.0;
285 }
286 } else if (out_mask & AV_CH_BACK_CENTER) {
289 } else if (out_mask & AV_CH_FRONT_LEFT) {
290 if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY) {
291 matrix[FRONT_LEFT ][SIDE_LEFT ] -= surround_mix_level * M_SQRT1_2;
292 matrix[FRONT_LEFT ][SIDE_RIGHT] -= surround_mix_level * M_SQRT1_2;
293 matrix[FRONT_RIGHT][SIDE_LEFT ] += surround_mix_level * M_SQRT1_2;
294 matrix[FRONT_RIGHT][SIDE_RIGHT] += surround_mix_level * M_SQRT1_2;
295 } else if (matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
296 matrix[FRONT_LEFT ][SIDE_LEFT ] -= surround_mix_level * SQRT3_2;
297 matrix[FRONT_LEFT ][SIDE_RIGHT] -= surround_mix_level * M_SQRT1_2;
298 matrix[FRONT_RIGHT][SIDE_LEFT ] += surround_mix_level * M_SQRT1_2;
299 matrix[FRONT_RIGHT][SIDE_RIGHT] += surround_mix_level * SQRT3_2;
300 } else {
301 matrix[ FRONT_LEFT][ SIDE_LEFT] += surround_mix_level;
302 matrix[FRONT_RIGHT][SIDE_RIGHT] += surround_mix_level;
303 }
304 } else if (out_mask & AV_CH_FRONT_CENTER) {
305 matrix[ FRONT_CENTER][SIDE_LEFT ]+= surround_mix_level * M_SQRT1_2;
306 matrix[ FRONT_CENTER][SIDE_RIGHT]+= surround_mix_level * M_SQRT1_2;
307 }else
308 av_assert0(0);
309 }
310
311 if(unaccounted & AV_CH_FRONT_LEFT_OF_CENTER){
312 if (out_mask & AV_CH_FRONT_LEFT) {
315 } else if (out_mask & AV_CH_FRONT_CENTER) {
318 }else
319 av_assert0(0);
320 }
321
322 if (unaccounted & AV_CH_TOP_FRONT_LEFT) {
323 if (out_mask & AV_CH_TOP_FRONT_CENTER) {
326 if (in_mask & AV_CH_TOP_FRONT_CENTER)
327 matrix[TOP_FRONT_CENTER][TOP_FRONT_CENTER] = center_mix_level * sqrt(2);
328 } else if (out_mask & AV_CH_FRONT_LEFT) {
329 /* U+030 -> M+030 in ITU-R BS.2127-1, Table 16. */
332 } else if (out_mask & AV_CH_FRONT_CENTER) {
335 } else
336 av_assert0(0);
337 }
338
339 if (unaccounted & AV_CH_TOP_FRONT_CENTER) {
340 if (out_mask & AV_CH_TOP_FRONT_LEFT) {
341 /* U+030 = U-030 = sqrt(1/2) */
344 } else if (out_mask & AV_CH_FRONT_CENTER) {
345 /* M+000 = 1 */
347 } else if (out_mask & AV_CH_FRONT_LEFT) {
348 /* M+030 = M-030 = sqrt(1/2) */
349 matrix[FRONT_LEFT ][TOP_FRONT_CENTER] += center_mix_level;
350 matrix[FRONT_RIGHT][TOP_FRONT_CENTER] += center_mix_level;
351 } else
352 av_assert0(0);
353 }
354
355 if (unaccounted & AV_CH_TOP_BACK_LEFT) {
356 if (out_mask & AV_CH_TOP_BACK_CENTER) {
359 } else if (out_mask & AV_CH_TOP_FRONT_LEFT) {
360 /* IAMF v1.1.0, Section 7.3.2.1.1. */
363 } else if (out_mask & AV_CH_BACK_LEFT) {
364 matrix[BACK_LEFT ][TOP_BACK_LEFT ] += 1.0;
366 } else if (out_mask & AV_CH_SIDE_LEFT) {
367 matrix[SIDE_LEFT ][TOP_BACK_LEFT ] += 1.0;
369 } else if (out_mask & AV_CH_FRONT_LEFT) {
370 matrix[FRONT_LEFT ][TOP_BACK_LEFT ] += surround_mix_level;
371 matrix[FRONT_RIGHT][TOP_BACK_RIGHT] += surround_mix_level;
372 } else if (out_mask & AV_CH_FRONT_CENTER) {
373 matrix[FRONT_CENTER][TOP_BACK_LEFT ] += surround_mix_level*M_SQRT1_2;
374 matrix[FRONT_CENTER][TOP_BACK_RIGHT] += surround_mix_level*M_SQRT1_2;
375 } else
376 av_assert0(0);
377 }
378
379 /* BS.2127-1 maps U+180 to rear outputs before front outputs. */
380 if (unaccounted & AV_CH_TOP_BACK_CENTER) {
381 if (out_mask & AV_CH_TOP_BACK_LEFT) {
384 } else if (out_mask & AV_CH_BACK_LEFT) {
387 } else if (out_mask & AV_CH_SIDE_LEFT) {
390 } else if (out_mask & AV_CH_FRONT_LEFT) {
393 } else if (out_mask & AV_CH_FRONT_CENTER)
395 else
396 av_assert0(0);
397 }
398
399
400 if (unaccounted & AV_CH_TOP_SIDE_LEFT) {
401 if ((out_mask & (AV_CH_TOP_FRONT_LEFT|AV_CH_TOP_BACK_CENTER)) ==
403 /* UH+180 = sqrt(1/3); U±045 = sqrt(2/3)*/
408 } else if ((out_mask & (AV_CH_TOP_FRONT_LEFT|AV_CH_TOP_BACK_LEFT)) ==
410 /* U±030 = U±110 = sqrt(1/2) */
415 } else if (out_mask & AV_CH_TOP_FRONT_LEFT &&
416 (out_mask & (AV_CH_BACK_LEFT|AV_CH_SIDE_LEFT))) {
417 /* U±030 = M±110 = sqrt(1/2) */
420 if (out_mask & AV_CH_BACK_LEFT) {
423 } else if (out_mask & AV_CH_SIDE_LEFT) {
426 }
427 } else if (out_mask & AV_CH_SIDE_LEFT) {
428 /* M±090 = 1 */
429 matrix[SIDE_LEFT ][TOP_SIDE_LEFT ] += 1.0;
431 } else if (out_mask & AV_CH_FRONT_LEFT) {
432 /* M±030 = M±110 = sqrt(1/2) */
433 matrix[FRONT_LEFT ][TOP_SIDE_LEFT ] += surround_mix_level;
434 matrix[FRONT_RIGHT][TOP_SIDE_RIGHT] += surround_mix_level;
435 if (out_mask & AV_CH_BACK_LEFT) {
438 }
439 } else if (out_mask & AV_CH_FRONT_CENTER) {
440 matrix[FRONT_CENTER][TOP_SIDE_LEFT ] += surround_mix_level*M_SQRT1_2;
441 matrix[FRONT_CENTER][TOP_SIDE_RIGHT] += surround_mix_level*M_SQRT1_2;
442 } else
443 av_assert0(0);
444 }
445
446 if (unaccounted & AV_CH_TOP_CENTER) {
447 if ((out_mask & (AV_CH_TOP_FRONT_LEFT|AV_CH_TOP_BACK_LEFT)) ==
449 /* U+045 = U-045 = U+135 = U-135 = sqrt(1/4) */
454 } else if ((out_mask & (AV_CH_TOP_FRONT_LEFT|AV_CH_TOP_BACK_CENTER)) ==
456 /* U+045 = U-045 = UH+180 = sqrt(1/3) */
460 } else if (out_mask & AV_CH_TOP_FRONT_LEFT &&
461 out_mask & (AV_CH_BACK_LEFT|AV_CH_SIDE_LEFT)) {
462 /* U+045 = U-045 = M+135 = M-135 = sqrt(1/4) *
463 * U+030 = U-030 = M+110 = M-110 = sqrt(1/4) */
466 if (out_mask & AV_CH_BACK_LEFT) {
467 matrix[BACK_LEFT ][TOP_CENTER] += 0.5;
469 } else if (out_mask & AV_CH_SIDE_LEFT) {
470 matrix[SIDE_LEFT ][TOP_CENTER] += 0.5;
472 }
473 } else if (out_mask & AV_CH_FRONT_LEFT) {
474 /* M+030 = M-030 = M+135 = M-135 = sqrt(1/4) */
475 /* M+030 = M-030 = sqrt(1/4) */
476 matrix[FRONT_LEFT ][TOP_CENTER] += 0.5;
478 if (out_mask & AV_CH_BACK_LEFT) {
479 matrix[BACK_LEFT ][TOP_CENTER] += 0.5;
481 } else if (out_mask & AV_CH_SIDE_LEFT) {
482 matrix[SIDE_LEFT ][TOP_CENTER] += 0.5;
484 }
485 } else if (out_mask & AV_CH_FRONT_CENTER) {
487 } else
488 av_assert0(0);
489 }
490
491 if (unaccounted & AV_CH_BOTTOM_FRONT_CENTER) {
492 if (out_mask & AV_CH_FRONT_CENTER) {
494 } else if (out_mask & AV_CH_FRONT_LEFT) {
495 matrix[FRONT_LEFT ][BOTTOM_FRONT_CENTER] += center_mix_level;
496 matrix[FRONT_RIGHT][BOTTOM_FRONT_CENTER] += center_mix_level;
497 } else
498 av_assert0(0);
499 }
500
501 if (unaccounted & AV_CH_BOTTOM_FRONT_LEFT) {
502 if (out_mask & AV_CH_BOTTOM_FRONT_CENTER) {
505 if (in_mask & AV_CH_BOTTOM_FRONT_CENTER)
506 matrix[BOTTOM_FRONT_CENTER][BOTTOM_FRONT_CENTER] = center_mix_level * sqrt(2);
507 } else if (out_mask & AV_CH_FRONT_LEFT) {
508 /* M±030 = 1 */
511 } else if (out_mask & AV_CH_FRONT_CENTER) {
514 } else
515 av_assert0(0);
516 }
517
518 /* mix LFE into front left/right or center */
519 if (unaccounted & AV_CH_LOW_FREQUENCY) {
520 if (out_mask & AV_CH_FRONT_CENTER) {
521 matrix[FRONT_CENTER][LOW_FREQUENCY] += lfe_mix_level;
522 } else if (out_mask & AV_CH_FRONT_LEFT) {
523 matrix[FRONT_LEFT ][LOW_FREQUENCY] += lfe_mix_level * M_SQRT1_2;
524 matrix[FRONT_RIGHT][LOW_FREQUENCY] += lfe_mix_level * M_SQRT1_2;
525 } else
526 av_assert0(0);
527 }
528
529 /* mix LFE2 into LFE, front left/right or center */
530 if (unaccounted & AV_CH_LOW_FREQUENCY_2) {
531 if (out_mask & AV_CH_LOW_FREQUENCY) {
533 } else if (out_mask & AV_CH_FRONT_CENTER) {
534 matrix[FRONT_CENTER][LOW_FREQUENCY_2] += lfe_mix_level;
535 } else if (out_mask & AV_CH_FRONT_LEFT) {
536 matrix[FRONT_LEFT ][LOW_FREQUENCY_2] += lfe_mix_level * M_SQRT1_2;
537 matrix[FRONT_RIGHT][LOW_FREQUENCY_2] += lfe_mix_level * M_SQRT1_2;
538 } else
539 av_assert0(0);
540 }
541
542
543 for (i = 0; i < 64; i++) {
544 double sum=0;
545 int out_i = av_channel_layout_index_from_channel(out_ch_layout, i);
546 if (out_i < 0)
547 continue;
548 for(j=0; j<64; j++){
549 int in_i = av_channel_layout_index_from_channel(in_ch_layout, j);
550 if (in_i < 0)
551 continue;
552 if (i < FF_ARRAY_ELEMS(matrix) && j < FF_ARRAY_ELEMS(matrix[0]))
553 matrix_param[stride*out_i + in_i] = matrix[i][j];
554 else
555 matrix_param[stride*out_i + in_i] = i == j && (in_mask & out_mask & (1ULL << i));
556 sum += fabs(matrix_param[stride*out_i + in_i]);
557 }
558 maxcoef= FFMAX(maxcoef, sum);
559 }
560 if(rematrix_volume < 0)
561 maxcoef = -rematrix_volume;
562
563 if(maxcoef > maxval || rematrix_volume < 0){
564 maxcoef /= maxval;
565 for(i=0; i<SWR_CH_MAX; i++)
566 for(j=0; j<SWR_CH_MAX; j++){
567 matrix_param[stride*i + j] /= maxcoef;
568 }
569 }
570}
571
572av_cold int swr_build_matrix2(const AVChannelLayout *in_layout, const AVChannelLayout *out_layout,
573 double center_mix_level, double surround_mix_level,
574 double lfe_mix_level, double maxval,
575 double rematrix_volume, double *matrix_param,
576 ptrdiff_t stride, enum AVMatrixEncoding matrix_encoding, void *log_context)
577{
578 int i, j, ret;
579 AVChannelLayout in_ch_layout = { 0 }, out_ch_layout = { 0 };
580 char buf[128];
581
582 ret = clean_layout(&in_ch_layout, in_layout, log_context);
583 ret |= clean_layout(&out_ch_layout, out_layout, log_context);
584 if (ret < 0)
585 goto fail;
586
589 ) {
590 av_channel_layout_uninit(&out_ch_layout);
592 }
595 ) {
596 av_channel_layout_uninit(&in_ch_layout);
598 }
599
600 if(!av_channel_layout_check(&in_ch_layout)) {
601 av_log(log_context, AV_LOG_ERROR, "Input channel layout is invalid\n");
602 ret = AVERROR(EINVAL);
603 goto fail;
604 }
605 if(!sane_layout(&in_ch_layout)) {
606 av_channel_layout_describe(&in_ch_layout, buf, sizeof(buf));
607 av_log(log_context, AV_LOG_ERROR, "Input channel layout '%s' is not supported\n", buf);
608 ret = AVERROR(EINVAL);
609 goto fail;
610 }
611
612 if(!av_channel_layout_check(&out_ch_layout)) {
613 av_log(log_context, AV_LOG_ERROR, "Output channel layout is invalid\n");
614 ret = AVERROR(EINVAL);
615 goto fail;
616 }
617 if(!sane_layout(&out_ch_layout)) {
618 av_channel_layout_describe(&out_ch_layout, buf, sizeof(buf));
619 av_log(log_context, AV_LOG_ERROR, "Output channel layout '%s' is not supported\n", buf);
620 ret = AVERROR(EINVAL);
621 goto fail;
622 }
623
624 build_matrix(&in_ch_layout, &out_ch_layout, center_mix_level,
625 surround_mix_level, lfe_mix_level, maxval, rematrix_volume,
626 matrix_param, stride, matrix_encoding);
627
628 if(rematrix_volume > 0){
629 for(i=0; i<SWR_CH_MAX; i++)
630 for(j=0; j<SWR_CH_MAX; j++){
631 matrix_param[stride*i + j] *= rematrix_volume;
632 }
633 }
634
635 av_log(log_context, AV_LOG_DEBUG, "Matrix coefficients:\n");
636 for (i = 0; i < out_ch_layout.nb_channels; i++){
637 av_channel_name(buf, sizeof(buf), av_channel_layout_channel_from_index(&out_ch_layout, i));
638 av_log(log_context, AV_LOG_DEBUG, "%s: ", buf);
639 for (j = 0; j < in_ch_layout.nb_channels; j++){
640 av_channel_name(buf, sizeof(buf), av_channel_layout_channel_from_index(&in_ch_layout, j));
641 av_log(log_context, AV_LOG_DEBUG, "%s:%f ", buf, matrix_param[stride*i + j]);
642 }
643 av_log(log_context, AV_LOG_DEBUG, "\n");
644 }
645
646 ret = 0;
647fail:
648 av_channel_layout_uninit(&in_ch_layout);
649 av_channel_layout_uninit(&out_ch_layout);
650
651 return ret;
652}
653
655{
656 double maxval;
657
658 if (s->rematrix_maxval > 0) {
659 maxval = s->rematrix_maxval;
660 } else if ( av_get_packed_sample_fmt(s->out_sample_fmt) < AV_SAMPLE_FMT_FLT
661 || av_get_packed_sample_fmt(s->int_sample_fmt) < AV_SAMPLE_FMT_FLT) {
662 maxval = 1.0;
663 } else
664 maxval = INT_MAX;
665
666 memset(s->matrix, 0, sizeof(s->matrix));
667 return swr_build_matrix2(&s->in_ch_layout, &s->out_ch_layout,
668 s->clev, s->slev, s->lfe_mix_level,
669 maxval, s->rematrix_volume, (double*)s->matrix,
670 s->matrix[1] - s->matrix[0], s->matrix_encoding, s);
671}
672
674 int i, j;
675 int nb_in = s->used_ch_layout.nb_channels;
676 int nb_out = s->out.ch_count;
677
678 s->mix_any_f = NULL;
679
680 if (!s->rematrix_custom) {
681 int r = auto_matrix(s);
682 if (r)
683 return r;
684 } else {
685 char buf[128];
686 av_log(s, AV_LOG_DEBUG, "Custom matrix coefficients:\n");
687 double *matrix_param = (double*)s->matrix;
688 ptrdiff_t stride = s->matrix[1] - s->matrix[0];
689 for (i = 0; i < s->out_ch_layout.nb_channels; i++) {
690 av_channel_name(buf, sizeof(buf), av_channel_layout_channel_from_index(&s->out_ch_layout, i));
691 av_log(s, AV_LOG_DEBUG, "%s: ", buf);
692 for (j = 0; j < s->in_ch_layout.nb_channels; j++){
693 av_channel_name(buf, sizeof(buf), av_channel_layout_channel_from_index(&s->in_ch_layout, j));
694 av_log(s, AV_LOG_DEBUG, "%s:%f ", buf, matrix_param[stride*i + j]);
695 }
696 av_log(s, AV_LOG_DEBUG, "\n");
697 }
698 }
699 if (s->midbuf.fmt == AV_SAMPLE_FMT_S16P){
700 int maxsum = 0;
701 s->native_matrix = av_calloc(nb_in * nb_out, sizeof(int));
702 if (!s->native_matrix)
703 return AVERROR(ENOMEM);
704 for (i = 0; i < nb_out; i++) {
705 double rem = 0;
706 int sum = 0;
707
708 for (j = 0; j < nb_in; j++) {
709 double target = s->matrix[i][j] * 32768 + rem;
710 ((int*)s->native_matrix)[i * nb_in + j] = lrintf(target);
711 rem += target - ((int*)s->native_matrix)[i * nb_in + j];
712 sum += FFABS(((int*)s->native_matrix)[i * nb_in + j]);
713 }
714 maxsum = FFMAX(maxsum, sum);
715 }
716 s->native_one.i = 32768;
717 if (maxsum <= 32768) {
718 s->mix_1_1_f = copy_s16;
719 s->mix_2_1_f = sum2_s16;
720 s->mix_any_f = get_mix_any_func_s16(s);
721 } else {
722 s->mix_1_1_f = copy_clip_s16;
723 s->mix_2_1_f = sum2_clip_s16;
724 s->mix_any_f = get_mix_any_func_clip_s16(s);
725 }
726 }else if(s->midbuf.fmt == AV_SAMPLE_FMT_FLTP){
727 s->native_matrix = av_calloc(nb_in * nb_out, sizeof(float));
728 if (!s->native_matrix)
729 return AVERROR(ENOMEM);
730 for (i = 0; i < nb_out; i++)
731 for (j = 0; j < nb_in; j++)
732 ((float*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
733 s->native_one.f = 1.0;
734 s->mix_1_1_f = copy_float;
735 s->mix_2_1_f = sum2_float;
736 s->mix_any_f = get_mix_any_func_float(s);
737 }else if(s->midbuf.fmt == AV_SAMPLE_FMT_DBLP){
738 s->native_matrix = av_calloc(nb_in * nb_out, sizeof(double));
739 if (!s->native_matrix)
740 return AVERROR(ENOMEM);
741 for (i = 0; i < nb_out; i++)
742 for (j = 0; j < nb_in; j++)
743 ((double*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
744 s->native_one.d = 1.0;
745 s->mix_1_1_f = copy_double;
746 s->mix_2_1_f = sum2_double;
747 s->mix_any_f = get_mix_any_func_double(s);
748 }else if(s->midbuf.fmt == AV_SAMPLE_FMT_S32P){
749 s->native_matrix = av_calloc(nb_in * nb_out, sizeof(int));
750 if (!s->native_matrix)
751 return AVERROR(ENOMEM);
752 for (i = 0; i < nb_out; i++) {
753 double rem = 0;
754
755 for (j = 0; j < nb_in; j++) {
756 double target = s->matrix[i][j] * 32768 + rem;
757 ((int*)s->native_matrix)[i * nb_in + j] = lrintf(target);
758 rem += target - ((int*)s->native_matrix)[i * nb_in + j];
759 }
760 }
761 s->native_one.i = 32768;
762 s->mix_1_1_f = copy_s32;
763 s->mix_2_1_f = sum2_s32;
764 s->mix_any_f = get_mix_any_func_s32(s);
765 }else
766 av_assert0(0);
767 //FIXME quantize for integeres
768 for (i = 0; i < SWR_CH_MAX; i++) {
769 int ch_in=0;
770 for (j = 0; j < SWR_CH_MAX; j++) {
771 const double coeff = s->matrix[i][j];
772 if (coeff)
773 s->matrix_ch[i][++ch_in]= j;
774 switch (s->int_sample_fmt) {
776 s->matrix_flt[i][j] = coeff;
777 break;
779 break;
780 default:
781 s->matrix32[i][j] = lrintf(coeff * 32768);
782 break;
783 }
784 }
785 s->matrix_ch[i][0]= ch_in;
786 }
787
788#if ARCH_X86 && HAVE_X86ASM
790#endif
791
792 return 0;
793}
794
796 av_freep(&s->native_matrix);
797 av_freep(&s->native_simd_matrix);
798}
799
800int swri_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy){
801 int out_i, in_i, i, j;
802 int len1 = 0;
803 int off = 0;
804
805 if(s->mix_any_f) {
806 s->mix_any_f(out->ch, (const uint8_t *const *)in->ch, s->native_matrix, len);
807 return 0;
808 }
809
810 if(s->mix_2_1_simd || s->mix_1_1_simd){
811 len1= len&~15;
812 off = len1 * out->bps;
813 }
814
815 av_assert0(s->out_ch_layout.order == AV_CHANNEL_ORDER_UNSPEC || out->ch_count == s->out_ch_layout.nb_channels);
816 av_assert0(s-> in_ch_layout.order == AV_CHANNEL_ORDER_UNSPEC || in ->ch_count == s->in_ch_layout.nb_channels);
817
818 for(out_i=0; out_i<out->ch_count; out_i++){
819 switch(s->matrix_ch[out_i][0]){
820 case 0:
821 if(mustcopy)
822 memset(out->ch[out_i], 0, len * av_get_bytes_per_sample(s->int_sample_fmt));
823 break;
824 case 1:
825 in_i= s->matrix_ch[out_i][1];
826 if(s->matrix[out_i][in_i]!=1.0){
827 if(s->mix_1_1_simd && len1)
828 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);
829 if(len != len1)
830 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);
831 }else if(mustcopy){
832 memcpy(out->ch[out_i], in->ch[in_i], len*out->bps);
833 }else{
834 out->ch[out_i]= in->ch[in_i];
835 }
836 break;
837 case 2: {
838 int in_i1 = s->matrix_ch[out_i][1];
839 int in_i2 = s->matrix_ch[out_i][2];
840 if(s->mix_2_1_simd && len1)
841 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);
842 else
843 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);
844 if(len != len1)
845 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);
846 break;}
847 default:
848 if(s->int_sample_fmt == AV_SAMPLE_FMT_FLTP){
849 for(i=0; i<len; i++){
850 float v=0;
851 for(j=0; j<s->matrix_ch[out_i][0]; j++){
852 in_i= s->matrix_ch[out_i][1+j];
853 v+= ((float*)in->ch[in_i])[i] * s->matrix_flt[out_i][in_i];
854 }
855 ((float*)out->ch[out_i])[i]= v;
856 }
857 }else if(s->int_sample_fmt == AV_SAMPLE_FMT_DBLP){
858 for(i=0; i<len; i++){
859 double v=0;
860 for(j=0; j<s->matrix_ch[out_i][0]; j++){
861 in_i= s->matrix_ch[out_i][1+j];
862 v+= ((double*)in->ch[in_i])[i] * s->matrix[out_i][in_i];
863 }
864 ((double*)out->ch[out_i])[i]= v;
865 }
866 }else{
867 for(i=0; i<len; i++){
868 int v=0;
869 for(j=0; j<s->matrix_ch[out_i][0]; j++){
870 in_i= s->matrix_ch[out_i][1+j];
871 v+= ((int16_t*)in->ch[in_i])[i] * s->matrix32[out_i][in_i];
872 }
873 ((int16_t*)out->ch[out_i])[i]= (v + 16384)>>15;
874 }
875 }
876 }
877 }
878 return 0;
879}
#define SWR_CH_MAX
Definition af_amerge.c:37
static FILE * out
simple assert() macros that are a bit more flexible than ISO C assert().
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition avassert.h:42
#define i(width, name, range_min, range_max)
Definition cbs_h264.c:63
#define s(width, name)
Definition cbs_vp9.c:198
Public libavutil channel layout APIs header.
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition common.h:74
#define NULL
Definition coverity.c:32
long long int64_t
Definition coverity.c:34
static __device__ float fabs(float a)
#define fail
Definition test.h:479
#define AV_CH_LAYOUT_STEREO_DOWNMIX
#define AV_CH_LAYOUT_SURROUND
#define AV_CH_LAYOUT_STEREO
AVMatrixEncoding
@ AV_MATRIX_ENCODING_DOLBY
@ AV_MATRIX_ENCODING_DPLII
#define AV_CH_SIDE_LEFT
#define AV_CH_TOP_FRONT_LEFT
#define AV_CH_FRONT_RIGHT
#define AV_CH_TOP_BACK_CENTER
#define AV_CH_BOTTOM_FRONT_CENTER
#define AV_CH_FRONT_RIGHT_OF_CENTER
#define AV_CH_BACK_CENTER
#define AV_CH_TOP_FRONT_CENTER
#define AV_CH_FRONT_LEFT_OF_CENTER
#define AV_CH_LOW_FREQUENCY_2
#define AV_CH_BACK_RIGHT
#define AV_CH_TOP_SIDE_RIGHT
#define AV_CH_FRONT_CENTER
#define AV_CH_TOP_BACK_RIGHT
#define AV_CH_TOP_CENTER
#define AV_CH_SIDE_RIGHT
#define AV_CH_BACK_LEFT
#define AV_CH_TOP_SIDE_LEFT
#define AV_CH_TOP_BACK_LEFT
#define AV_CH_LOW_FREQUENCY
#define AV_CH_BOTTOM_FRONT_RIGHT
#define AV_CH_TOP_FRONT_RIGHT
#define AV_CH_BOTTOM_FRONT_LEFT
#define AV_CH_FRONT_LEFT
int av_channel_layout_index_from_channel(const AVChannelLayout *channel_layout, enum AVChannel channel)
Get the index of a given channel in a channel layout.
#define AV_CHANNEL_LAYOUT_STEREO
int av_channel_layout_compare(const AVChannelLayout *chl, const AVChannelLayout *chl1)
Check whether two channel layouts are semantically the same, i.e.
int av_channel_name(char *buf, size_t buf_size, enum AVChannel channel_id)
Get a human readable string in an abbreviated form describing a given channel.
#define AV_CHANNEL_LAYOUT_MONO
enum AVChannel av_channel_layout_channel_from_index(const AVChannelLayout *channel_layout, unsigned int idx)
Get the channel with the given index in a channel layout.
void av_channel_layout_uninit(AVChannelLayout *channel_layout)
Free any allocated data in the channel layout and reset the channel count to 0.
AVChannel
int av_channel_layout_describe(const AVChannelLayout *channel_layout, char *buf, size_t buf_size)
Get a human-readable string describing the channel layout properties.
int av_channel_layout_check(const AVChannelLayout *channel_layout)
Check whether a channel layout is valid, i.e.
int av_channel_layout_copy(AVChannelLayout *dst, const AVChannelLayout *src)
Make a copy of a channel layout.
#define AV_CHANNEL_LAYOUT_STEREO_DOWNMIX
uint64_t av_channel_layout_subset(const AVChannelLayout *channel_layout, uint64_t mask)
Find out what channels from a given set are present in a channel layout, without regard for their pos...
@ AV_CHANNEL_ORDER_NATIVE
The native channel order, i.e.
@ AV_CHANNEL_ORDER_CUSTOM
The channel order does not correspond to any other predefined order and is stored as an explicit map.
@ AV_CHANNEL_ORDER_UNSPEC
Only the channel count is specified, without any further information about the channel order.
@ AV_CHAN_UNUSED
Channel is empty can be safely skipped.
@ AV_CHAN_FRONT_CENTER
#define AVERROR(e)
Definition error.h:45
#define AV_LOG_DEBUG
Stuff which is only useful for libav* developers.
Definition log.h:231
#define AV_LOG_VERBOSE
Detailed information.
Definition log.h:226
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition log.h:210
int av_get_bytes_per_sample(enum AVSampleFormat sample_fmt)
Return number of bytes per sample.
Definition samplefmt.c:108
enum AVSampleFormat av_get_packed_sample_fmt(enum AVSampleFormat sample_fmt)
Get the packed alternative form of the given sample format.
Definition samplefmt.c:77
@ AV_SAMPLE_FMT_FLTP
float, planar
Definition samplefmt.h:66
@ AV_SAMPLE_FMT_S16P
signed 16 bits, planar
Definition samplefmt.h:64
@ AV_SAMPLE_FMT_FLT
float
Definition samplefmt.h:60
@ AV_SAMPLE_FMT_S32P
signed 32 bits, planar
Definition samplefmt.h:65
@ AV_SAMPLE_FMT_DBLP
double, planar
Definition samplefmt.h:67
av_cold int swr_build_matrix2(const AVChannelLayout *in_layout, const AVChannelLayout *out_layout, double center_mix_level, double surround_mix_level, double lfe_mix_level, double maxval, double rematrix_volume, double *matrix_param, ptrdiff_t stride, enum AVMatrixEncoding matrix_encoding, void *log_context)
Generate a channel mixing matrix.
Definition rematrix.c:572
int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride)
Set a customized remix matrix.
Definition rematrix.c:71
#define r
Definition input.c:42
#define av_cold
Definition attributes.h:117
#define lrintf(x)
Definition libm_mips.h:72
static const uint16_t mask[17]
Definition lzw.c:38
#define FFMAX(a, b)
Definition macros.h:47
#define M_SQRT1_2
uint64_t layout
void * av_calloc(size_t nmemb, size_t size)
Definition mem.c:264
Memory handling functions.
#define TOP_CENTER
Definition movtextdec.c:51
int swri_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy)
Definition rematrix.c:800
#define BOTTOM_FRONT_LEFT
Definition rematrix.c:67
#define TOP_BACK_RIGHT
Definition rematrix.c:62
av_cold int swri_rematrix_init(SwrContext *s)
Definition rematrix.c:673
#define FRONT_LEFT_OF_CENTER
Definition rematrix.c:51
static int even(int64_t layout)
Definition rematrix.c:93
static void build_matrix(const AVChannelLayout *in_ch_layout, const AVChannelLayout *out_ch_layout, double center_mix_level, double surround_mix_level, double lfe_mix_level, double maxval, double rematrix_volume, double *matrix_param, ptrdiff_t stride, enum AVMatrixEncoding matrix_encoding)
Definition rematrix.c:151
#define BOTTOM_FRONT_RIGHT
Definition rematrix.c:68
#define TOP_BACK_LEFT
Definition rematrix.c:60
#define BACK_LEFT
Definition rematrix.c:49
#define NUM_NAMED_CHANNELS
Definition rematrix.c:69
static int clean_layout(AVChannelLayout *out, const AVChannelLayout *in, void *s)
Definition rematrix.c:99
#define SIDE_RIGHT
Definition rematrix.c:55
static av_cold int auto_matrix(SwrContext *s)
Definition rematrix.c:654
#define LOW_FREQUENCY_2
Definition rematrix.c:63
#define FRONT_LEFT
Definition rematrix.c:45
#define TOP_FRONT_CENTER
Definition rematrix.c:58
#define FRONT_RIGHT_OF_CENTER
Definition rematrix.c:52
#define TOP_SIDE_RIGHT
Definition rematrix.c:65
static int sane_layout(const AVChannelLayout *ch_layout)
Definition rematrix.c:114
#define TOP_FRONT_LEFT
Definition rematrix.c:57
#define BACK_RIGHT
Definition rematrix.c:50
#define FRONT_RIGHT
Definition rematrix.c:46
#define LOW_FREQUENCY
Definition rematrix.c:48
#define TOP_FRONT_RIGHT
Definition rematrix.c:59
#define TOP_BACK_CENTER
Definition rematrix.c:61
#define FRONT_CENTER
Definition rematrix.c:47
#define BOTTOM_FRONT_CENTER
Definition rematrix.c:66
#define SIDE_LEFT
Definition rematrix.c:54
av_cold void swri_rematrix_free(SwrContext *s)
Definition rematrix.c:795
#define BACK_CENTER
Definition rematrix.c:53
#define TOP_SIDE_LEFT
Definition rematrix.c:64
#define FF_ARRAY_ELEMS(a)
enum AVChannel id
An AVChannelLayout holds information about the channel layout of audio data.
enum AVChannelOrder order
Channel order used in this layout.
union AVChannelLayout::@162063043056170047076125117143030261346263330336 u
Details about which channels are present in this layout.
int nb_channels
Number of channels in this layout.
AVChannelCustom * map
This member must be used when the channel order is AV_CHANNEL_ORDER_CUSTOM.
int ch_count
number of channels
uint8_t * ch[SWR_CH_MAX]
samples buffer per channel
The libswresample context.
#define stride
int swri_check_chlayout(struct SwrContext *s, const AVChannelLayout *chl, const char *name)
Definition swresample.c:33
#define SQRT2_3
int swri_rematrix_init_x86(struct SwrContext *s)
#define SQRT1_3
#define SQRT3_2
#define av_freep(p)
#define av_log(a,...)
static const double coeff[2][5]
int len