FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libswresample/rematrix.c
Date: 2026-08-28 22:32:55
Exec Total Coverage
Lines: 452 575 78.6%
Functions: 10 10 100.0%
Branches: 312 421 74.1%

Line Branch Exec Source
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 #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
71 10 int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride)
72 {
73 int nb_in, nb_out, in, out;
74
75
3/6
✓ Branch 0 taken 10 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 10 times.
✗ Branch 5 not taken.
20 if (!s || s->in_convert || // s needs to be allocated but not initialized
76
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 10 times.
20 swri_check_chlayout(s, &s->user_in_chlayout , "input") ||
77 10 swri_check_chlayout(s, &s->user_out_chlayout, "output")
78 )
79 return AVERROR(EINVAL);
80 10 memset(s->matrix, 0, sizeof(s->matrix));
81
82 10 nb_in = s->user_in_chlayout.nb_channels;
83 10 nb_out = s->user_out_chlayout.nb_channels;
84
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 10 times.
41 for (out = 0; out < nb_out; out++) {
85
2/2
✓ Branch 0 taken 147 times.
✓ Branch 1 taken 31 times.
178 for (in = 0; in < nb_in; in++)
86 147 s->matrix[out][in] = matrix[in];
87 31 matrix += stride;
88 }
89 10 s->rematrix_custom = 1;
90 10 return 0;
91 }
92
93 1408 static int even(int64_t layout){
94
2/2
✓ Branch 0 taken 858 times.
✓ Branch 1 taken 550 times.
1408 if(!layout) return 1;
95
1/2
✓ Branch 0 taken 550 times.
✗ Branch 1 not taken.
550 if(layout&(layout-1)) return 1;
96 return 0;
97 }
98
99 176 static int clean_layout(AVChannelLayout *out, const AVChannelLayout *in, void *s)
100 {
101 176 int ret = 0;
102
103
4/4
✓ Branch 1 taken 32 times.
✓ Branch 2 taken 144 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 25 times.
176 if (av_channel_layout_index_from_channel(in, AV_CHAN_FRONT_CENTER) < 0 && in->nb_channels == 1) {
104 char buf[128];
105 7 av_channel_layout_describe(in, buf, sizeof(buf));
106 7 av_log(s, AV_LOG_VERBOSE, "Treating %s as mono\n", buf);
107 7 *out = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
108 } else
109 169 ret = av_channel_layout_copy(out, in);
110
111 176 return ret;
112 }
113
114 176 static int sane_layout(const AVChannelLayout *ch_layout) {
115
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 176 times.
176 if(ch_layout->nb_channels >= SWR_CH_MAX)
116 return 0;
117
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 173 times.
176 if(ch_layout->order == AV_CHANNEL_ORDER_CUSTOM)
118
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 3 times.
19 for (int i = 0; i < ch_layout->nb_channels; i++) {
119 16 enum AVChannel id = ch_layout->u.map[i].id;
120
121
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 12 times.
16 if (id == AV_CHAN_UNUSED)
122 4 continue;
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 12 times.
12 if (id >= 64)
124 return 0;
125 }
126
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 173 times.
173 else if (ch_layout->order != AV_CHANNEL_ORDER_NATIVE)
127 return 0;
128 176 uint64_t mask = av_channel_layout_subset(ch_layout, ~(uint64_t)0);
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 176 times.
176 if(!(mask & AV_CH_LAYOUT_SURROUND)) // at least 1 front speaker
130 return 0;
131
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 176 times.
176 if (!even(mask & (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT))) // no asymmetric front
132 return 0;
133
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 176 times.
176 if (!even(mask & (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT))) // no asymmetric side
134 return 0;
135
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 176 times.
176 if (!even(mask & (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)))
136 return 0;
137
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 176 times.
176 if (!even(mask & (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)))
138 return 0;
139
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 176 times.
176 if (!even(mask & (AV_CH_TOP_FRONT_LEFT | AV_CH_TOP_FRONT_RIGHT)))
140 return 0;
141
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 176 times.
176 if (!even(mask & (AV_CH_TOP_BACK_LEFT | AV_CH_TOP_BACK_RIGHT)))
142 return 0;
143
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 176 times.
176 if (!even(mask & (AV_CH_TOP_SIDE_LEFT | AV_CH_TOP_SIDE_RIGHT)))
144 return 0;
145
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 176 times.
176 if (!even(mask & (AV_CH_BOTTOM_FRONT_LEFT | AV_CH_BOTTOM_FRONT_RIGHT)))
146 return 0;
147
148 176 return 1;
149 }
150
151 88 static 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 {
156 88 double matrix[NUM_NAMED_CHANNELS][NUM_NAMED_CHANNELS]={{0}};
157 88 uint64_t in_mask = av_channel_layout_subset(in_ch_layout, ~(uint64_t)0);
158 88 uint64_t out_mask = av_channel_layout_subset(out_ch_layout, ~(uint64_t)0);
159 88 uint64_t unaccounted = in_mask & ~out_mask;
160 88 double maxcoef=0;
161 int i, j;
162
163
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 86 times.
88 if (in_ch_layout->order == AV_CHANNEL_ORDER_CUSTOM) {
164
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 2 times.
14 for (j = 0; j < in_ch_layout->nb_channels; j++) {
165
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 10 times.
12 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
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 for (i = 0; i < out_ch_layout->nb_channels; i++)
169 4 matrix_param[stride * i + j] = 0.0;
170 }
171 }
172 }
173
174
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 87 times.
88 if (out_ch_layout->order == AV_CHANNEL_ORDER_CUSTOM) {
175
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1 times.
5 for (i = 0; i < out_ch_layout->nb_channels; i++) {
176
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 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
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 2 times.
14 for (j = 0; j < in_ch_layout->nb_channels; j++)
180 12 matrix_param[stride * i + j] = 0.0;
181 }
182 }
183 }
184
185
2/2
✓ Branch 0 taken 3608 times.
✓ Branch 1 taken 88 times.
3696 for(i=0; i<FF_ARRAY_ELEMS(matrix); i++){
186
2/2
✓ Branch 0 taken 451 times.
✓ Branch 1 taken 3157 times.
3608 if (in_mask & out_mask & (1ULL << i))
187 451 matrix[i][i]= 1.0;
188 }
189
190 //FIXME implement dolby surround
191 //FIXME implement full ac3
192
193
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 72 times.
88 if(unaccounted & AV_CH_FRONT_CENTER){
194
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 if ((out_mask & AV_CH_LAYOUT_STEREO) == AV_CH_LAYOUT_STEREO) {
195
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 3 times.
16 if (in_mask & AV_CH_LAYOUT_STEREO) {
196 13 matrix[ FRONT_LEFT][FRONT_CENTER]+= center_mix_level;
197 13 matrix[FRONT_RIGHT][FRONT_CENTER]+= center_mix_level;
198 } else {
199 3 matrix[ FRONT_LEFT][FRONT_CENTER]+= M_SQRT1_2;
200 3 matrix[FRONT_RIGHT][FRONT_CENTER]+= M_SQRT1_2;
201 }
202 }else
203 av_assert0(0);
204 }
205
2/2
✓ Branch 0 taken 16 times.
✓ Branch 1 taken 72 times.
88 if(unaccounted & AV_CH_LAYOUT_STEREO){
206
1/2
✓ Branch 0 taken 16 times.
✗ Branch 1 not taken.
16 if (out_mask & AV_CH_FRONT_CENTER) {
207 16 matrix[FRONT_CENTER][ FRONT_LEFT]+= M_SQRT1_2;
208 16 matrix[FRONT_CENTER][FRONT_RIGHT]+= M_SQRT1_2;
209
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 6 times.
16 if (in_mask & AV_CH_FRONT_CENTER)
210 10 matrix[FRONT_CENTER][ FRONT_CENTER] = center_mix_level*sqrt(2);
211 }else
212 av_assert0(0);
213 }
214
215
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 77 times.
88 if(unaccounted & AV_CH_BACK_CENTER){
216
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 4 times.
11 if (out_mask & AV_CH_BACK_LEFT) {
217 7 matrix[ BACK_LEFT][BACK_CENTER]+= M_SQRT1_2;
218 7 matrix[BACK_RIGHT][BACK_CENTER]+= M_SQRT1_2;
219
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 } else if (out_mask & AV_CH_SIDE_LEFT) {
220 2 matrix[ SIDE_LEFT][BACK_CENTER]+= M_SQRT1_2;
221 2 matrix[SIDE_RIGHT][BACK_CENTER]+= M_SQRT1_2;
222
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 } else if (out_mask & AV_CH_FRONT_LEFT) {
223
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 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 1 matrix[ FRONT_LEFT][BACK_CENTER]+= surround_mix_level * M_SQRT1_2;
234 1 matrix[FRONT_RIGHT][BACK_CENTER]+= surround_mix_level * M_SQRT1_2;
235 }
236
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 } else if (out_mask & AV_CH_FRONT_CENTER) {
237 1 matrix[ FRONT_CENTER][BACK_CENTER]+= surround_mix_level * M_SQRT1_2;
238 }else
239 av_assert0(0);
240 }
241
2/2
✓ Branch 0 taken 33 times.
✓ Branch 1 taken 55 times.
88 if(unaccounted & AV_CH_BACK_LEFT){
242
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 33 times.
33 if (out_mask & AV_CH_BACK_CENTER) {
243 matrix[BACK_CENTER][ BACK_LEFT]+= M_SQRT1_2;
244 matrix[BACK_CENTER][BACK_RIGHT]+= M_SQRT1_2;
245
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 19 times.
33 } else if (out_mask & AV_CH_SIDE_LEFT) {
246
2/2
✓ Branch 0 taken 13 times.
✓ Branch 1 taken 1 times.
14 if (in_mask & AV_CH_SIDE_LEFT) {
247 13 matrix[ SIDE_LEFT][ BACK_LEFT]+= M_SQRT1_2;
248 13 matrix[SIDE_RIGHT][BACK_RIGHT]+= M_SQRT1_2;
249 }else{
250 1 matrix[ SIDE_LEFT][ BACK_LEFT]+= 1.0;
251 1 matrix[SIDE_RIGHT][BACK_RIGHT]+= 1.0;
252 }
253
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 8 times.
19 } else if (out_mask & AV_CH_FRONT_LEFT) {
254
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 } 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 11 matrix[ FRONT_LEFT][ BACK_LEFT] += surround_mix_level;
266 11 matrix[FRONT_RIGHT][BACK_RIGHT] += surround_mix_level;
267 }
268
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 } else if (out_mask & AV_CH_FRONT_CENTER) {
269 8 matrix[ FRONT_CENTER][BACK_LEFT ]+= surround_mix_level*M_SQRT1_2;
270 8 matrix[ FRONT_CENTER][BACK_RIGHT]+= surround_mix_level*M_SQRT1_2;
271 }else
272 av_assert0(0);
273 }
274
275
2/2
✓ Branch 0 taken 29 times.
✓ Branch 1 taken 59 times.
88 if(unaccounted & AV_CH_SIDE_LEFT){
276
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 20 times.
29 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
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2 times.
9 if (in_mask & AV_CH_BACK_LEFT) {
280 7 matrix[BACK_LEFT ][SIDE_LEFT ] += M_SQRT1_2;
281 7 matrix[BACK_RIGHT][SIDE_RIGHT] += M_SQRT1_2;
282 } else {
283 2 matrix[BACK_LEFT ][SIDE_LEFT ] += 1.0;
284 2 matrix[BACK_RIGHT][SIDE_RIGHT] += 1.0;
285 }
286
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 } else if (out_mask & AV_CH_BACK_CENTER) {
287 matrix[BACK_CENTER][ SIDE_LEFT]+= M_SQRT1_2;
288 matrix[BACK_CENTER][SIDE_RIGHT]+= M_SQRT1_2;
289
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 9 times.
20 } else if (out_mask & AV_CH_FRONT_LEFT) {
290
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 } 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 11 matrix[ FRONT_LEFT][ SIDE_LEFT] += surround_mix_level;
302 11 matrix[FRONT_RIGHT][SIDE_RIGHT] += surround_mix_level;
303 }
304
1/2
✓ Branch 0 taken 9 times.
✗ Branch 1 not taken.
9 } else if (out_mask & AV_CH_FRONT_CENTER) {
305 9 matrix[ FRONT_CENTER][SIDE_LEFT ]+= surround_mix_level * M_SQRT1_2;
306 9 matrix[ FRONT_CENTER][SIDE_RIGHT]+= surround_mix_level * M_SQRT1_2;
307 }else
308 av_assert0(0);
309 }
310
311
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 61 times.
88 if(unaccounted & AV_CH_FRONT_LEFT_OF_CENTER){
312
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 3 times.
27 if (out_mask & AV_CH_FRONT_LEFT) {
313 24 matrix[ FRONT_LEFT][ FRONT_LEFT_OF_CENTER]+= 1.0;
314 24 matrix[FRONT_RIGHT][FRONT_RIGHT_OF_CENTER]+= 1.0;
315
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 } else if (out_mask & AV_CH_FRONT_CENTER) {
316 3 matrix[ FRONT_CENTER][ FRONT_LEFT_OF_CENTER]+= M_SQRT1_2;
317 3 matrix[ FRONT_CENTER][FRONT_RIGHT_OF_CENTER]+= M_SQRT1_2;
318 }else
319 av_assert0(0);
320 }
321
322
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 56 times.
88 if (unaccounted & AV_CH_TOP_FRONT_LEFT) {
323
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 32 times.
32 if (out_mask & AV_CH_TOP_FRONT_CENTER) {
324 matrix[TOP_FRONT_CENTER][TOP_FRONT_LEFT ] += M_SQRT1_2;
325 matrix[TOP_FRONT_CENTER][TOP_FRONT_RIGHT] += M_SQRT1_2;
326 if (in_mask & AV_CH_TOP_FRONT_CENTER)
327 matrix[TOP_FRONT_CENTER][TOP_FRONT_CENTER] = center_mix_level * sqrt(2);
328
2/2
✓ Branch 0 taken 24 times.
✓ Branch 1 taken 8 times.
32 } else if (out_mask & AV_CH_FRONT_LEFT) {
329 /* U+030 -> M+030 in ITU-R BS.2127-1, Table 16. */
330 24 matrix[FRONT_LEFT ][TOP_FRONT_LEFT ] += 1.0;
331 24 matrix[FRONT_RIGHT][TOP_FRONT_RIGHT] += 1.0;
332
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 } else if (out_mask & AV_CH_FRONT_CENTER) {
333 8 matrix[FRONT_CENTER][TOP_FRONT_LEFT ] += M_SQRT1_2;
334 8 matrix[FRONT_CENTER][TOP_FRONT_RIGHT] += M_SQRT1_2;
335 } else
336 av_assert0(0);
337 }
338
339
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 77 times.
88 if (unaccounted & AV_CH_TOP_FRONT_CENTER) {
340
2/2
✓ Branch 0 taken 7 times.
✓ Branch 1 taken 4 times.
11 if (out_mask & AV_CH_TOP_FRONT_LEFT) {
341 /* U+030 = U-030 = sqrt(1/2) */
342 7 matrix[TOP_FRONT_LEFT ][TOP_FRONT_CENTER]+= M_SQRT1_2;
343 7 matrix[TOP_FRONT_RIGHT][TOP_FRONT_CENTER]+= M_SQRT1_2;
344
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 } else if (out_mask & AV_CH_FRONT_CENTER) {
345 /* M+000 = 1 */
346 3 matrix[FRONT_CENTER][TOP_FRONT_CENTER] += 1.0;
347
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 } else if (out_mask & AV_CH_FRONT_LEFT) {
348 /* M+030 = M-030 = sqrt(1/2) */
349 1 matrix[FRONT_LEFT ][TOP_FRONT_CENTER] += center_mix_level;
350 1 matrix[FRONT_RIGHT][TOP_FRONT_CENTER] += center_mix_level;
351 } else
352 av_assert0(0);
353 }
354
355
2/2
✓ Branch 0 taken 32 times.
✓ Branch 1 taken 56 times.
88 if (unaccounted & AV_CH_TOP_BACK_LEFT) {
356
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 29 times.
32 if (out_mask & AV_CH_TOP_BACK_CENTER) {
357 3 matrix[TOP_BACK_CENTER][TOP_BACK_LEFT ] += M_SQRT1_2;
358 3 matrix[TOP_BACK_CENTER][TOP_BACK_RIGHT] += M_SQRT1_2;
359
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 20 times.
29 } else if (out_mask & AV_CH_TOP_FRONT_LEFT) {
360 /* IAMF v1.1.0, Section 7.3.2.1.1. */
361 9 matrix[TOP_FRONT_LEFT ][TOP_BACK_LEFT ] += M_SQRT1_2;
362 9 matrix[TOP_FRONT_RIGHT][TOP_BACK_RIGHT] += M_SQRT1_2;
363
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 10 times.
20 } else if (out_mask & AV_CH_BACK_LEFT) {
364 10 matrix[BACK_LEFT ][TOP_BACK_LEFT ] += 1.0;
365 10 matrix[BACK_RIGHT][TOP_BACK_RIGHT] += 1.0;
366
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 10 times.
10 } else if (out_mask & AV_CH_SIDE_LEFT) {
367 matrix[SIDE_LEFT ][TOP_BACK_LEFT ] += 1.0;
368 matrix[SIDE_RIGHT][TOP_BACK_RIGHT] += 1.0;
369
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 5 times.
10 } else if (out_mask & AV_CH_FRONT_LEFT) {
370 5 matrix[FRONT_LEFT ][TOP_BACK_LEFT ] += surround_mix_level;
371 5 matrix[FRONT_RIGHT][TOP_BACK_RIGHT] += surround_mix_level;
372
1/2
✓ Branch 0 taken 5 times.
✗ Branch 1 not taken.
5 } else if (out_mask & AV_CH_FRONT_CENTER) {
373 5 matrix[FRONT_CENTER][TOP_BACK_LEFT ] += surround_mix_level*M_SQRT1_2;
374 5 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
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 70 times.
88 if (unaccounted & AV_CH_TOP_BACK_CENTER) {
381
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 12 times.
18 if (out_mask & AV_CH_TOP_BACK_LEFT) {
382 6 matrix[TOP_BACK_LEFT ][TOP_BACK_CENTER] += M_SQRT1_2;
383 6 matrix[TOP_BACK_RIGHT][TOP_BACK_CENTER] += M_SQRT1_2;
384
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 6 times.
12 } else if (out_mask & AV_CH_BACK_LEFT) {
385 6 matrix[BACK_LEFT ][TOP_BACK_CENTER] += M_SQRT1_2;
386 6 matrix[BACK_RIGHT][TOP_BACK_CENTER] += M_SQRT1_2;
387
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 } else if (out_mask & AV_CH_SIDE_LEFT) {
388 2 matrix[SIDE_LEFT ][TOP_BACK_CENTER] += M_SQRT1_2;
389 2 matrix[SIDE_RIGHT][TOP_BACK_CENTER] += M_SQRT1_2;
390
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 } else if (out_mask & AV_CH_FRONT_LEFT) {
391 2 matrix[FRONT_LEFT ][TOP_BACK_CENTER] += 0.5;
392 2 matrix[FRONT_RIGHT][TOP_BACK_CENTER] += 0.5;
393
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 } else if (out_mask & AV_CH_FRONT_CENTER)
394 2 matrix[FRONT_CENTER][TOP_BACK_CENTER] += 0.5;
395 else
396 av_assert0(0);
397 }
398
399
400
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 68 times.
88 if (unaccounted & AV_CH_TOP_SIDE_LEFT) {
401
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 18 times.
20 if ((out_mask & (AV_CH_TOP_FRONT_LEFT|AV_CH_TOP_BACK_CENTER)) ==
402 (AV_CH_TOP_FRONT_LEFT|AV_CH_TOP_BACK_CENTER)) {
403 /* UH+180 = sqrt(1/3); U±045 = sqrt(2/3)*/
404 2 matrix[TOP_FRONT_LEFT ][TOP_SIDE_LEFT ] += SQRT2_3;
405 2 matrix[TOP_FRONT_RIGHT][TOP_SIDE_RIGHT] += SQRT2_3;
406 2 matrix[TOP_BACK_CENTER][TOP_SIDE_LEFT ] += SQRT1_3;
407 2 matrix[TOP_BACK_CENTER][TOP_SIDE_RIGHT] += SQRT1_3;
408
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 12 times.
18 } else if ((out_mask & (AV_CH_TOP_FRONT_LEFT|AV_CH_TOP_BACK_LEFT)) ==
409 (AV_CH_TOP_FRONT_LEFT|AV_CH_TOP_BACK_LEFT)) {
410 /* U±030 = U±110 = sqrt(1/2) */
411 6 matrix[TOP_FRONT_LEFT ][TOP_SIDE_LEFT ] += M_SQRT1_2;
412 6 matrix[TOP_FRONT_RIGHT][TOP_SIDE_RIGHT] += M_SQRT1_2;
413 6 matrix[TOP_BACK_LEFT ][TOP_SIDE_LEFT ] += M_SQRT1_2;
414 6 matrix[TOP_BACK_RIGHT][TOP_SIDE_RIGHT] += M_SQRT1_2;
415
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 8 times.
12 } else if (out_mask & AV_CH_TOP_FRONT_LEFT &&
416
1/2
✓ Branch 0 taken 4 times.
✗ Branch 1 not taken.
4 (out_mask & (AV_CH_BACK_LEFT|AV_CH_SIDE_LEFT))) {
417 /* U±030 = M±110 = sqrt(1/2) */
418 4 matrix[TOP_FRONT_LEFT ][TOP_SIDE_LEFT] += M_SQRT1_2;
419 4 matrix[TOP_FRONT_RIGHT][TOP_SIDE_RIGHT] += M_SQRT1_2;
420
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (out_mask & AV_CH_BACK_LEFT) {
421 2 matrix[BACK_LEFT ][TOP_SIDE_LEFT] += M_SQRT1_2;
422 2 matrix[BACK_RIGHT][TOP_SIDE_RIGHT] += M_SQRT1_2;
423
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 } else if (out_mask & AV_CH_SIDE_LEFT) {
424 2 matrix[SIDE_LEFT ][TOP_SIDE_LEFT] += M_SQRT1_2;
425 2 matrix[SIDE_RIGHT][TOP_SIDE_RIGHT] += M_SQRT1_2;
426 }
427
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 6 times.
8 } else if (out_mask & AV_CH_SIDE_LEFT) {
428 /* M±090 = 1 */
429 2 matrix[SIDE_LEFT ][TOP_SIDE_LEFT ] += 1.0;
430 2 matrix[SIDE_RIGHT][TOP_SIDE_RIGHT] += 1.0;
431
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 2 times.
6 } else if (out_mask & AV_CH_FRONT_LEFT) {
432 /* M±030 = M±110 = sqrt(1/2) */
433 4 matrix[FRONT_LEFT ][TOP_SIDE_LEFT ] += surround_mix_level;
434 4 matrix[FRONT_RIGHT][TOP_SIDE_RIGHT] += surround_mix_level;
435
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 if (out_mask & AV_CH_BACK_LEFT) {
436 2 matrix[BACK_LEFT ][TOP_SIDE_LEFT ] += M_SQRT1_2;
437 2 matrix[BACK_RIGHT][TOP_SIDE_RIGHT] += M_SQRT1_2;
438 }
439
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 } else if (out_mask & AV_CH_FRONT_CENTER) {
440 2 matrix[FRONT_CENTER][TOP_SIDE_LEFT ] += surround_mix_level*M_SQRT1_2;
441 2 matrix[FRONT_CENTER][TOP_SIDE_RIGHT] += surround_mix_level*M_SQRT1_2;
442 } else
443 av_assert0(0);
444 }
445
446
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 77 times.
88 if (unaccounted & AV_CH_TOP_CENTER) {
447
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 7 times.
11 if ((out_mask & (AV_CH_TOP_FRONT_LEFT|AV_CH_TOP_BACK_LEFT)) ==
448 (AV_CH_TOP_FRONT_LEFT|AV_CH_TOP_BACK_LEFT)) {
449 /* U+045 = U-045 = U+135 = U-135 = sqrt(1/4) */
450 4 matrix[TOP_FRONT_LEFT ][TOP_CENTER] += 0.5;
451 4 matrix[TOP_FRONT_RIGHT][TOP_CENTER] += 0.5;
452 4 matrix[TOP_BACK_LEFT ][TOP_CENTER] += 0.5;
453 4 matrix[TOP_BACK_RIGHT][TOP_CENTER] += 0.5;
454
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 6 times.
7 } else if ((out_mask & (AV_CH_TOP_FRONT_LEFT|AV_CH_TOP_BACK_CENTER)) ==
455 (AV_CH_TOP_FRONT_LEFT|AV_CH_TOP_BACK_CENTER)) {
456 /* U+045 = U-045 = UH+180 = sqrt(1/3) */
457 1 matrix[TOP_FRONT_LEFT ][TOP_CENTER] += SQRT1_3;
458 1 matrix[TOP_FRONT_RIGHT][TOP_CENTER] += SQRT1_3;
459 1 matrix[TOP_BACK_CENTER][TOP_CENTER] += SQRT1_3;
460
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 4 times.
6 } else if (out_mask & AV_CH_TOP_FRONT_LEFT &&
461
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 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) */
464 2 matrix[TOP_FRONT_LEFT ][TOP_CENTER] += 0.5;
465 2 matrix[TOP_FRONT_RIGHT][TOP_CENTER] += 0.5;
466
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
2 if (out_mask & AV_CH_BACK_LEFT) {
467 1 matrix[BACK_LEFT ][TOP_CENTER] += 0.5;
468 1 matrix[BACK_RIGHT][TOP_CENTER] += 0.5;
469
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 } else if (out_mask & AV_CH_SIDE_LEFT) {
470 1 matrix[SIDE_LEFT ][TOP_CENTER] += 0.5;
471 1 matrix[SIDE_RIGHT][TOP_CENTER] += 0.5;
472 }
473
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 1 times.
4 } 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 3 matrix[FRONT_LEFT ][TOP_CENTER] += 0.5;
477 3 matrix[FRONT_RIGHT][TOP_CENTER] += 0.5;
478
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 if (out_mask & AV_CH_BACK_LEFT) {
479 2 matrix[BACK_LEFT ][TOP_CENTER] += 0.5;
480 2 matrix[BACK_RIGHT][TOP_CENTER] += 0.5;
481
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 } else if (out_mask & AV_CH_SIDE_LEFT) {
482 matrix[SIDE_LEFT ][TOP_CENTER] += 0.5;
483 matrix[SIDE_RIGHT][TOP_CENTER] += 0.5;
484 }
485
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 } else if (out_mask & AV_CH_FRONT_CENTER) {
486 1 matrix[FRONT_CENTER][TOP_CENTER] += 0.5;
487 } else
488 av_assert0(0);
489 }
490
491
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 77 times.
88 if (unaccounted & AV_CH_BOTTOM_FRONT_CENTER) {
492
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1 times.
11 if (out_mask & AV_CH_FRONT_CENTER) {
493 10 matrix[FRONT_CENTER][BOTTOM_FRONT_CENTER] += 1.0;
494
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 } else if (out_mask & AV_CH_FRONT_LEFT) {
495 1 matrix[FRONT_LEFT ][BOTTOM_FRONT_CENTER] += center_mix_level;
496 1 matrix[FRONT_RIGHT][BOTTOM_FRONT_CENTER] += center_mix_level;
497 } else
498 av_assert0(0);
499 }
500
501
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 77 times.
88 if (unaccounted & AV_CH_BOTTOM_FRONT_LEFT) {
502
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 11 times.
11 if (out_mask & AV_CH_BOTTOM_FRONT_CENTER) {
503 matrix[BOTTOM_FRONT_CENTER][BOTTOM_FRONT_LEFT ] += M_SQRT1_2;
504 matrix[BOTTOM_FRONT_CENTER][BOTTOM_FRONT_RIGHT] += M_SQRT1_2;
505 if (in_mask & AV_CH_BOTTOM_FRONT_CENTER)
506 matrix[BOTTOM_FRONT_CENTER][BOTTOM_FRONT_CENTER] = center_mix_level * sqrt(2);
507
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 1 times.
11 } else if (out_mask & AV_CH_FRONT_LEFT) {
508 /* M±030 = 1 */
509 10 matrix[FRONT_LEFT ][BOTTOM_FRONT_LEFT ] += 1.0;
510 10 matrix[FRONT_RIGHT][BOTTOM_FRONT_RIGHT] += 1.0;
511
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 } else if (out_mask & AV_CH_FRONT_CENTER) {
512 1 matrix[FRONT_CENTER][BOTTOM_FRONT_LEFT ] += M_SQRT1_2;
513 1 matrix[FRONT_CENTER][BOTTOM_FRONT_RIGHT] += M_SQRT1_2;
514 } else
515 av_assert0(0);
516 }
517
518 /* mix LFE into front left/right or center */
519
2/2
✓ Branch 0 taken 23 times.
✓ Branch 1 taken 65 times.
88 if (unaccounted & AV_CH_LOW_FREQUENCY) {
520
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 13 times.
23 if (out_mask & AV_CH_FRONT_CENTER) {
521 10 matrix[FRONT_CENTER][LOW_FREQUENCY] += lfe_mix_level;
522
1/2
✓ Branch 0 taken 13 times.
✗ Branch 1 not taken.
13 } else if (out_mask & AV_CH_FRONT_LEFT) {
523 13 matrix[FRONT_LEFT ][LOW_FREQUENCY] += lfe_mix_level * M_SQRT1_2;
524 13 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
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 70 times.
88 if (unaccounted & AV_CH_LOW_FREQUENCY_2) {
531
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 4 times.
18 if (out_mask & AV_CH_LOW_FREQUENCY) {
532 14 matrix[LOW_FREQUENCY][LOW_FREQUENCY_2] += M_SQRT1_2;
533
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
4 } else if (out_mask & AV_CH_FRONT_CENTER) {
534 2 matrix[FRONT_CENTER][LOW_FREQUENCY_2] += lfe_mix_level;
535
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 } else if (out_mask & AV_CH_FRONT_LEFT) {
536 2 matrix[FRONT_LEFT ][LOW_FREQUENCY_2] += lfe_mix_level * M_SQRT1_2;
537 2 matrix[FRONT_RIGHT][LOW_FREQUENCY_2] += lfe_mix_level * M_SQRT1_2;
538 } else
539 av_assert0(0);
540 }
541
542
543
2/2
✓ Branch 0 taken 5632 times.
✓ Branch 1 taken 88 times.
5720 for (i = 0; i < 64; i++) {
544 5632 double sum=0;
545 5632 int out_i = av_channel_layout_index_from_channel(out_ch_layout, i);
546
2/2
✓ Branch 0 taken 5145 times.
✓ Branch 1 taken 487 times.
5632 if (out_i < 0)
547 5145 continue;
548
2/2
✓ Branch 0 taken 31168 times.
✓ Branch 1 taken 487 times.
31655 for(j=0; j<64; j++){
549 31168 int in_i = av_channel_layout_index_from_channel(in_ch_layout, j);
550
2/2
✓ Branch 0 taken 24279 times.
✓ Branch 1 taken 6889 times.
31168 if (in_i < 0)
551 24279 continue;
552
2/4
✓ Branch 0 taken 6889 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6889 times.
✗ Branch 3 not taken.
6889 if (i < FF_ARRAY_ELEMS(matrix) && j < FF_ARRAY_ELEMS(matrix[0]))
553 6889 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 6889 sum += fabs(matrix_param[stride*out_i + in_i]);
557 }
558
2/2
✓ Branch 0 taken 229 times.
✓ Branch 1 taken 258 times.
487 maxcoef= FFMAX(maxcoef, sum);
559 }
560
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 88 times.
88 if(rematrix_volume < 0)
561 maxcoef = -rematrix_volume;
562
563
3/4
✓ Branch 0 taken 80 times.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 80 times.
88 if(maxcoef > maxval || rematrix_volume < 0){
564 8 maxcoef /= maxval;
565
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 8 times.
520 for(i=0; i<SWR_CH_MAX; i++)
566
2/2
✓ Branch 0 taken 32768 times.
✓ Branch 1 taken 512 times.
33280 for(j=0; j<SWR_CH_MAX; j++){
567 32768 matrix_param[stride*i + j] /= maxcoef;
568 }
569 }
570 88 }
571
572 88 av_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 88 AVChannelLayout in_ch_layout = { 0 }, out_ch_layout = { 0 };
580 char buf[128];
581
582 88 ret = clean_layout(&in_ch_layout, in_layout, log_context);
583 88 ret |= clean_layout(&out_ch_layout, out_layout, log_context);
584
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 88 times.
88 if (ret < 0)
585 goto fail;
586
587
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 88 times.
88 if( !av_channel_layout_compare(&out_ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO_DOWNMIX)
588 && !av_channel_layout_subset(&in_ch_layout, AV_CH_LAYOUT_STEREO_DOWNMIX)
589 ) {
590 av_channel_layout_uninit(&out_ch_layout);
591 out_ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
592 }
593
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 88 times.
88 if( !av_channel_layout_compare(&in_ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO_DOWNMIX)
594 && !av_channel_layout_subset(&out_ch_layout, AV_CH_LAYOUT_STEREO_DOWNMIX)
595 ) {
596 av_channel_layout_uninit(&in_ch_layout);
597 in_ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
598 }
599
600
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 88 times.
88 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
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 88 times.
88 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
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 88 times.
88 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
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 88 times.
88 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 88 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
1/2
✓ Branch 0 taken 88 times.
✗ Branch 1 not taken.
88 if(rematrix_volume > 0){
629
2/2
✓ Branch 0 taken 5632 times.
✓ Branch 1 taken 88 times.
5720 for(i=0; i<SWR_CH_MAX; i++)
630
2/2
✓ Branch 0 taken 360448 times.
✓ Branch 1 taken 5632 times.
366080 for(j=0; j<SWR_CH_MAX; j++){
631 360448 matrix_param[stride*i + j] *= rematrix_volume;
632 }
633 }
634
635 88 av_log(log_context, AV_LOG_DEBUG, "Matrix coefficients:\n");
636
2/2
✓ Branch 0 taken 489 times.
✓ Branch 1 taken 88 times.
577 for (i = 0; i < out_ch_layout.nb_channels; i++){
637 489 av_channel_name(buf, sizeof(buf), av_channel_layout_channel_from_index(&out_ch_layout, i));
638 489 av_log(log_context, AV_LOG_DEBUG, "%s: ", buf);
639
2/2
✓ Branch 0 taken 6905 times.
✓ Branch 1 taken 489 times.
7394 for (j = 0; j < in_ch_layout.nb_channels; j++){
640 6905 av_channel_name(buf, sizeof(buf), av_channel_layout_channel_from_index(&in_ch_layout, j));
641 6905 av_log(log_context, AV_LOG_DEBUG, "%s:%f ", buf, matrix_param[stride*i + j]);
642 }
643 489 av_log(log_context, AV_LOG_DEBUG, "\n");
644 }
645
646 88 ret = 0;
647 88 fail:
648 88 av_channel_layout_uninit(&in_ch_layout);
649 88 av_channel_layout_uninit(&out_ch_layout);
650
651 88 return ret;
652 }
653
654 20 av_cold static int auto_matrix(SwrContext *s)
655 {
656 double maxval;
657
658
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (s->rematrix_maxval > 0) {
659 maxval = s->rematrix_maxval;
660
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 19 times.
20 } else if ( av_get_packed_sample_fmt(s->out_sample_fmt) < AV_SAMPLE_FMT_FLT
661
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 || av_get_packed_sample_fmt(s->int_sample_fmt) < AV_SAMPLE_FMT_FLT) {
662 19 maxval = 1.0;
663 } else
664 1 maxval = INT_MAX;
665
666 20 memset(s->matrix, 0, sizeof(s->matrix));
667 40 return swr_build_matrix2(&s->in_ch_layout, &s->out_ch_layout,
668 20 s->clev, s->slev, s->lfe_mix_level,
669 20 maxval, s->rematrix_volume, (double*)s->matrix,
670 20 s->matrix[1] - s->matrix[0], s->matrix_encoding, s);
671 }
672
673 30 av_cold int swri_rematrix_init(SwrContext *s){
674 int i, j;
675 30 int nb_in = s->used_ch_layout.nb_channels;
676 30 int nb_out = s->out.ch_count;
677
678 30 s->mix_any_f = NULL;
679
680
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 10 times.
30 if (!s->rematrix_custom) {
681 20 int r = auto_matrix(s);
682
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (r)
683 return r;
684 } else {
685 char buf[128];
686 10 av_log(s, AV_LOG_DEBUG, "Custom matrix coefficients:\n");
687 10 double *matrix_param = (double*)s->matrix;
688 10 ptrdiff_t stride = s->matrix[1] - s->matrix[0];
689
2/2
✓ Branch 0 taken 31 times.
✓ Branch 1 taken 10 times.
41 for (i = 0; i < s->out_ch_layout.nb_channels; i++) {
690 31 av_channel_name(buf, sizeof(buf), av_channel_layout_channel_from_index(&s->out_ch_layout, i));
691 31 av_log(s, AV_LOG_DEBUG, "%s: ", buf);
692
2/2
✓ Branch 0 taken 147 times.
✓ Branch 1 taken 31 times.
178 for (j = 0; j < s->in_ch_layout.nb_channels; j++){
693 147 av_channel_name(buf, sizeof(buf), av_channel_layout_channel_from_index(&s->in_ch_layout, j));
694 147 av_log(s, AV_LOG_DEBUG, "%s:%f ", buf, matrix_param[stride*i + j]);
695 }
696 31 av_log(s, AV_LOG_DEBUG, "\n");
697 }
698 }
699
2/2
✓ Branch 0 taken 20 times.
✓ Branch 1 taken 10 times.
30 if (s->midbuf.fmt == AV_SAMPLE_FMT_S16P){
700 20 int maxsum = 0;
701 20 s->native_matrix = av_calloc(nb_in * nb_out, sizeof(int));
702
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 20 times.
20 if (!s->native_matrix)
703 return AVERROR(ENOMEM);
704
2/2
✓ Branch 0 taken 48 times.
✓ Branch 1 taken 20 times.
68 for (i = 0; i < nb_out; i++) {
705 48 double rem = 0;
706 48 int sum = 0;
707
708
2/2
✓ Branch 0 taken 202 times.
✓ Branch 1 taken 48 times.
250 for (j = 0; j < nb_in; j++) {
709 202 double target = s->matrix[i][j] * 32768 + rem;
710 202 ((int*)s->native_matrix)[i * nb_in + j] = lrintf(target);
711 202 rem += target - ((int*)s->native_matrix)[i * nb_in + j];
712 202 sum += FFABS(((int*)s->native_matrix)[i * nb_in + j]);
713 }
714 48 maxsum = FFMAX(maxsum, sum);
715 }
716 20 s->native_one.i = 32768;
717
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 9 times.
20 if (maxsum <= 32768) {
718 11 s->mix_1_1_f = copy_s16;
719 11 s->mix_2_1_f = sum2_s16;
720 11 s->mix_any_f = get_mix_any_func_s16(s);
721 } else {
722 9 s->mix_1_1_f = copy_clip_s16;
723 9 s->mix_2_1_f = sum2_clip_s16;
724 9 s->mix_any_f = get_mix_any_func_clip_s16(s);
725 }
726
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
10 }else if(s->midbuf.fmt == AV_SAMPLE_FMT_FLTP){
727 9 s->native_matrix = av_calloc(nb_in * nb_out, sizeof(float));
728
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
9 if (!s->native_matrix)
729 return AVERROR(ENOMEM);
730
2/2
✓ Branch 0 taken 21 times.
✓ Branch 1 taken 9 times.
30 for (i = 0; i < nb_out; i++)
731
2/2
✓ Branch 0 taken 84 times.
✓ Branch 1 taken 21 times.
105 for (j = 0; j < nb_in; j++)
732 84 ((float*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
733 9 s->native_one.f = 1.0;
734 9 s->mix_1_1_f = copy_float;
735 9 s->mix_2_1_f = sum2_float;
736 9 s->mix_any_f = get_mix_any_func_float(s);
737
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 }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
1/2
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
1 }else if(s->midbuf.fmt == AV_SAMPLE_FMT_S32P){
749 1 s->native_matrix = av_calloc(nb_in * nb_out, sizeof(int));
750
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 if (!s->native_matrix)
751 return AVERROR(ENOMEM);
752
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
3 for (i = 0; i < nb_out; i++) {
753 2 double rem = 0;
754
755
2/2
✓ Branch 0 taken 12 times.
✓ Branch 1 taken 2 times.
14 for (j = 0; j < nb_in; j++) {
756 12 double target = s->matrix[i][j] * 32768 + rem;
757 12 ((int*)s->native_matrix)[i * nb_in + j] = lrintf(target);
758 12 rem += target - ((int*)s->native_matrix)[i * nb_in + j];
759 }
760 }
761 1 s->native_one.i = 32768;
762 1 s->mix_1_1_f = copy_s32;
763 1 s->mix_2_1_f = sum2_s32;
764 1 s->mix_any_f = get_mix_any_func_s32(s);
765 }else
766 av_assert0(0);
767 //FIXME quantize for integeres
768
2/2
✓ Branch 0 taken 1920 times.
✓ Branch 1 taken 30 times.
1950 for (i = 0; i < SWR_CH_MAX; i++) {
769 1920 int ch_in=0;
770
2/2
✓ Branch 0 taken 122880 times.
✓ Branch 1 taken 1920 times.
124800 for (j = 0; j < SWR_CH_MAX; j++) {
771 122880 const double coeff = s->matrix[i][j];
772
2/2
✓ Branch 0 taken 109 times.
✓ Branch 1 taken 122771 times.
122880 if (coeff)
773 109 s->matrix_ch[i][++ch_in]= j;
774
2/3
✓ Branch 0 taken 36864 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 86016 times.
122880 switch (s->int_sample_fmt) {
775 36864 case AV_SAMPLE_FMT_FLTP:
776 36864 s->matrix_flt[i][j] = coeff;
777 36864 break;
778 case AV_SAMPLE_FMT_DBLP:
779 break;
780 86016 default:
781 86016 s->matrix32[i][j] = lrintf(coeff * 32768);
782 86016 break;
783 }
784 }
785 1920 s->matrix_ch[i][0]= ch_in;
786 }
787
788 #if ARCH_X86 && HAVE_X86ASM
789 30 return swri_rematrix_init_x86(s);
790 #endif
791
792 return 0;
793 }
794
795 4241 av_cold void swri_rematrix_free(SwrContext *s){
796 4241 av_freep(&s->native_matrix);
797 4241 av_freep(&s->native_simd_matrix);
798 4241 }
799
800 1112 int swri_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy){
801 int out_i, in_i, i, j;
802 1112 int len1 = 0;
803 1112 int off = 0;
804
805
2/2
✓ Branch 0 taken 14 times.
✓ Branch 1 taken 1098 times.
1112 if(s->mix_any_f) {
806 14 s->mix_any_f(out->ch, (const uint8_t *const *)in->ch, s->native_matrix, len);
807 14 return 0;
808 }
809
810
3/4
✓ Branch 0 taken 1051 times.
✓ Branch 1 taken 47 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1051 times.
1098 if(s->mix_2_1_simd || s->mix_1_1_simd){
811 47 len1= len&~15;
812 47 off = len1 * out->bps;
813 }
814
815
3/4
✓ Branch 0 taken 1077 times.
✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1077 times.
1098 av_assert0(s->out_ch_layout.order == AV_CHANNEL_ORDER_UNSPEC || out->ch_count == s->out_ch_layout.nb_channels);
816
3/4
✓ Branch 0 taken 1077 times.
✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1077 times.
1098 av_assert0(s-> in_ch_layout.order == AV_CHANNEL_ORDER_UNSPEC || in ->ch_count == s->in_ch_layout.nb_channels);
817
818
2/2
✓ Branch 0 taken 2303 times.
✓ Branch 1 taken 1098 times.
3401 for(out_i=0; out_i<out->ch_count; out_i++){
819
4/4
✓ Branch 0 taken 534 times.
✓ Branch 1 taken 985 times.
✓ Branch 2 taken 654 times.
✓ Branch 3 taken 130 times.
2303 switch(s->matrix_ch[out_i][0]){
820 534 case 0:
821
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 534 times.
534 if(mustcopy)
822 memset(out->ch[out_i], 0, len * av_get_bytes_per_sample(s->int_sample_fmt));
823 534 break;
824 985 case 1:
825 985 in_i= s->matrix_ch[out_i][1];
826
2/2
✓ Branch 0 taken 158 times.
✓ Branch 1 taken 827 times.
985 if(s->matrix[out_i][in_i]!=1.0){
827
3/4
✓ Branch 0 taken 94 times.
✓ Branch 1 taken 64 times.
✓ Branch 2 taken 94 times.
✗ Branch 3 not taken.
158 if(s->mix_1_1_simd && len1)
828 94 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
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 94 times.
158 if(len != len1)
830 64 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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 827 times.
827 }else if(mustcopy){
832 memcpy(out->ch[out_i], in->ch[in_i], len*out->bps);
833 }else{
834 827 out->ch[out_i]= in->ch[in_i];
835 }
836 985 break;
837 654 case 2: {
838 654 int in_i1 = s->matrix_ch[out_i][1];
839 654 int in_i2 = s->matrix_ch[out_i][2];
840
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 654 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
654 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 654 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
2/2
✓ Branch 0 taken 652 times.
✓ Branch 1 taken 2 times.
654 if(len != len1)
845 652 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 654 break;}
847 130 default:
848
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 130 times.
130 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
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 130 times.
130 }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
2/2
✓ Branch 0 taken 529200 times.
✓ Branch 1 taken 130 times.
529330 for(i=0; i<len; i++){
868 529200 int v=0;
869
2/2
✓ Branch 0 taken 2116800 times.
✓ Branch 1 taken 529200 times.
2646000 for(j=0; j<s->matrix_ch[out_i][0]; j++){
870 2116800 in_i= s->matrix_ch[out_i][1+j];
871 2116800 v+= ((int16_t*)in->ch[in_i])[i] * s->matrix32[out_i][in_i];
872 }
873 529200 ((int16_t*)out->ch[out_i])[i]= (v + 16384)>>15;
874 }
875 }
876 }
877 }
878 1098 return 0;
879 }
880