FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libswresample/rematrix.c
Date: 2025-10-10 03:51:19
Exec Total Coverage
Lines: 245 412 59.5%
Functions: 10 10 100.0%
Branches: 166 297 55.9%

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 NUM_NAMED_CHANNELS 18
64
65 8 int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride)
66 {
67 int nb_in, nb_out, in, out;
68
69
2/4
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 8 times.
8 if (!s || s->in_convert) // s needs to be allocated but not initialized
70 return AVERROR(EINVAL);
71 8 memset(s->matrix, 0, sizeof(s->matrix));
72
73 8 nb_in = s->user_in_chlayout.nb_channels;
74 8 nb_out = s->user_out_chlayout.nb_channels;
75
2/2
✓ Branch 0 taken 27 times.
✓ Branch 1 taken 8 times.
35 for (out = 0; out < nb_out; out++) {
76
2/2
✓ Branch 0 taken 123 times.
✓ Branch 1 taken 27 times.
150 for (in = 0; in < nb_in; in++)
77 123 s->matrix[out][in] = matrix[in];
78 27 matrix += stride;
79 }
80 8 s->rematrix_custom = 1;
81 8 return 0;
82 }
83
84 190 static int even(int64_t layout){
85
2/2
✓ Branch 0 taken 166 times.
✓ Branch 1 taken 24 times.
190 if(!layout) return 1;
86
1/2
✓ Branch 0 taken 24 times.
✗ Branch 1 not taken.
24 if(layout&(layout-1)) return 1;
87 return 0;
88 }
89
90 38 static int clean_layout(AVChannelLayout *out, const AVChannelLayout *in, void *s)
91 {
92 38 int ret = 0;
93
94
4/4
✓ Branch 1 taken 18 times.
✓ Branch 2 taken 20 times.
✓ Branch 3 taken 7 times.
✓ Branch 4 taken 11 times.
38 if (av_channel_layout_index_from_channel(in, AV_CHAN_FRONT_CENTER) < 0 && in->nb_channels == 1) {
95 char buf[128];
96 7 av_channel_layout_describe(in, buf, sizeof(buf));
97 7 av_log(s, AV_LOG_VERBOSE, "Treating %s as mono\n", buf);
98 7 *out = (AVChannelLayout)AV_CHANNEL_LAYOUT_MONO;
99 } else
100 31 ret = av_channel_layout_copy(out, in);
101
102 38 return ret;
103 }
104
105 38 static int sane_layout(AVChannelLayout *ch_layout) {
106
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 38 times.
38 if(ch_layout->nb_channels >= SWR_CH_MAX)
107 return 0;
108
2/2
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 37 times.
38 if(ch_layout->order == AV_CHANNEL_ORDER_CUSTOM)
109
2/2
✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
9 for (int i = 0; i < ch_layout->nb_channels; i++) {
110
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (ch_layout->u.map[i].id >= 64)
111 return 0;
112 }
113
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 37 times.
37 else if (ch_layout->order != AV_CHANNEL_ORDER_NATIVE)
114 return 0;
115
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 38 times.
38 if(!av_channel_layout_subset(ch_layout, AV_CH_LAYOUT_SURROUND)) // at least 1 front speaker
116 return 0;
117
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 38 times.
38 if(!even(av_channel_layout_subset(ch_layout, (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT)))) // no asymmetric front
118 return 0;
119
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 38 times.
38 if(!even(av_channel_layout_subset(ch_layout, (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)))) // no asymmetric side
120 return 0;
121
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 38 times.
38 if(!even(av_channel_layout_subset(ch_layout, (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT))))
122 return 0;
123
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 38 times.
38 if(!even(av_channel_layout_subset(ch_layout, (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER))))
124 return 0;
125
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 38 times.
38 if(!even(av_channel_layout_subset(ch_layout, (AV_CH_TOP_FRONT_LEFT | AV_CH_TOP_FRONT_RIGHT))))
126 return 0;
127
128 38 return 1;
129 }
130
131 19 static void build_matrix(const AVChannelLayout *in_ch_layout, const AVChannelLayout *out_ch_layout,
132 double center_mix_level, double surround_mix_level,
133 double lfe_mix_level, double maxval, double rematrix_volume, double *matrix_param,
134 ptrdiff_t stride, enum AVMatrixEncoding matrix_encoding)
135 {
136 19 double matrix[NUM_NAMED_CHANNELS][NUM_NAMED_CHANNELS]={{0}};
137 19 uint64_t unaccounted = av_channel_layout_subset(in_ch_layout, UINT64_MAX) &
138 19 ~av_channel_layout_subset(out_ch_layout, UINT64_MAX);
139 19 double maxcoef=0;
140 int i, j;
141
142
2/2
✓ Branch 0 taken 342 times.
✓ Branch 1 taken 19 times.
361 for(i=0; i<FF_ARRAY_ELEMS(matrix); i++){
143
2/2
✓ Branch 1 taken 46 times.
✓ Branch 2 taken 296 times.
342 if( av_channel_layout_index_from_channel(in_ch_layout, i) >= 0
144
2/2
✓ Branch 1 taken 19 times.
✓ Branch 2 taken 27 times.
46 && av_channel_layout_index_from_channel(out_ch_layout, i) >= 0)
145 19 matrix[i][i]= 1.0;
146 }
147
148 //FIXME implement dolby surround
149 //FIXME implement full ac3
150
151
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 14 times.
19 if(unaccounted & AV_CH_FRONT_CENTER){
152
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 if (av_channel_layout_subset(out_ch_layout, AV_CH_LAYOUT_STEREO) == AV_CH_LAYOUT_STEREO) {
153
2/2
✓ Branch 1 taken 2 times.
✓ Branch 2 taken 3 times.
5 if (av_channel_layout_subset(in_ch_layout, AV_CH_LAYOUT_STEREO)) {
154 2 matrix[ FRONT_LEFT][FRONT_CENTER]+= center_mix_level;
155 2 matrix[FRONT_RIGHT][FRONT_CENTER]+= center_mix_level;
156 } else {
157 3 matrix[ FRONT_LEFT][FRONT_CENTER]+= M_SQRT1_2;
158 3 matrix[FRONT_RIGHT][FRONT_CENTER]+= M_SQRT1_2;
159 }
160 }else
161 av_assert0(0);
162 }
163
2/2
✓ Branch 0 taken 5 times.
✓ Branch 1 taken 14 times.
19 if(unaccounted & AV_CH_LAYOUT_STEREO){
164
1/2
✓ Branch 1 taken 5 times.
✗ Branch 2 not taken.
5 if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_CENTER) >= 0) {
165 5 matrix[FRONT_CENTER][ FRONT_LEFT]+= M_SQRT1_2;
166 5 matrix[FRONT_CENTER][FRONT_RIGHT]+= M_SQRT1_2;
167
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 5 times.
5 if (av_channel_layout_index_from_channel(in_ch_layout, AV_CHAN_FRONT_CENTER) >= 0)
168 matrix[FRONT_CENTER][ FRONT_CENTER] = center_mix_level*sqrt(2);
169 }else
170 av_assert0(0);
171 }
172
173
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if(unaccounted & AV_CH_BACK_CENTER){
174 if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_BACK_LEFT) >= 0) {
175 matrix[ BACK_LEFT][BACK_CENTER]+= M_SQRT1_2;
176 matrix[BACK_RIGHT][BACK_CENTER]+= M_SQRT1_2;
177 } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_SIDE_LEFT) >= 0) {
178 matrix[ SIDE_LEFT][BACK_CENTER]+= M_SQRT1_2;
179 matrix[SIDE_RIGHT][BACK_CENTER]+= M_SQRT1_2;
180 } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_LEFT) >= 0) {
181 if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY ||
182 matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
183 if (unaccounted & (AV_CH_BACK_LEFT | AV_CH_SIDE_LEFT)) {
184 matrix[FRONT_LEFT ][BACK_CENTER] -= surround_mix_level * M_SQRT1_2;
185 matrix[FRONT_RIGHT][BACK_CENTER] += surround_mix_level * M_SQRT1_2;
186 } else {
187 matrix[FRONT_LEFT ][BACK_CENTER] -= surround_mix_level;
188 matrix[FRONT_RIGHT][BACK_CENTER] += surround_mix_level;
189 }
190 } else {
191 matrix[ FRONT_LEFT][BACK_CENTER]+= surround_mix_level * M_SQRT1_2;
192 matrix[FRONT_RIGHT][BACK_CENTER]+= surround_mix_level * M_SQRT1_2;
193 }
194 } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_CENTER) >= 0) {
195 matrix[ FRONT_CENTER][BACK_CENTER]+= surround_mix_level * M_SQRT1_2;
196 }else
197 av_assert0(0);
198 }
199
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 16 times.
19 if(unaccounted & AV_CH_BACK_LEFT){
200
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
3 if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_BACK_CENTER) >= 0) {
201 matrix[BACK_CENTER][ BACK_LEFT]+= M_SQRT1_2;
202 matrix[BACK_CENTER][BACK_RIGHT]+= M_SQRT1_2;
203
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
3 } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_SIDE_LEFT) >= 0) {
204
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
1 if (av_channel_layout_index_from_channel(in_ch_layout, AV_CHAN_SIDE_LEFT) >= 0) {
205 1 matrix[ SIDE_LEFT][ BACK_LEFT]+= M_SQRT1_2;
206 1 matrix[SIDE_RIGHT][BACK_RIGHT]+= M_SQRT1_2;
207 }else{
208 matrix[ SIDE_LEFT][ BACK_LEFT]+= 1.0;
209 matrix[SIDE_RIGHT][BACK_RIGHT]+= 1.0;
210 }
211
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_LEFT) >= 0) {
212
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY) {
213 matrix[FRONT_LEFT ][BACK_LEFT ] -= surround_mix_level * M_SQRT1_2;
214 matrix[FRONT_LEFT ][BACK_RIGHT] -= surround_mix_level * M_SQRT1_2;
215 matrix[FRONT_RIGHT][BACK_LEFT ] += surround_mix_level * M_SQRT1_2;
216 matrix[FRONT_RIGHT][BACK_RIGHT] += surround_mix_level * M_SQRT1_2;
217
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 } else if (matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
218 matrix[FRONT_LEFT ][BACK_LEFT ] -= surround_mix_level * SQRT3_2;
219 matrix[FRONT_LEFT ][BACK_RIGHT] -= surround_mix_level * M_SQRT1_2;
220 matrix[FRONT_RIGHT][BACK_LEFT ] += surround_mix_level * M_SQRT1_2;
221 matrix[FRONT_RIGHT][BACK_RIGHT] += surround_mix_level * SQRT3_2;
222 } else {
223 2 matrix[ FRONT_LEFT][ BACK_LEFT] += surround_mix_level;
224 2 matrix[FRONT_RIGHT][BACK_RIGHT] += surround_mix_level;
225 }
226 } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_CENTER) >= 0) {
227 matrix[ FRONT_CENTER][BACK_LEFT ]+= surround_mix_level*M_SQRT1_2;
228 matrix[ FRONT_CENTER][BACK_RIGHT]+= surround_mix_level*M_SQRT1_2;
229 }else
230 av_assert0(0);
231 }
232
233
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 17 times.
19 if(unaccounted & AV_CH_SIDE_LEFT){
234
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_BACK_LEFT) >= 0) {
235 /* if back channels do not exist in the input, just copy side
236 channels to back channels, otherwise mix side into back */
237 if (av_channel_layout_index_from_channel(in_ch_layout, AV_CHAN_BACK_LEFT) >= 0) {
238 matrix[BACK_LEFT ][SIDE_LEFT ] += M_SQRT1_2;
239 matrix[BACK_RIGHT][SIDE_RIGHT] += M_SQRT1_2;
240 } else {
241 matrix[BACK_LEFT ][SIDE_LEFT ] += 1.0;
242 matrix[BACK_RIGHT][SIDE_RIGHT] += 1.0;
243 }
244
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_BACK_CENTER) >= 0) {
245 matrix[BACK_CENTER][ SIDE_LEFT]+= M_SQRT1_2;
246 matrix[BACK_CENTER][SIDE_RIGHT]+= M_SQRT1_2;
247
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_LEFT) >= 0) {
248
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 if (matrix_encoding == AV_MATRIX_ENCODING_DOLBY) {
249 matrix[FRONT_LEFT ][SIDE_LEFT ] -= surround_mix_level * M_SQRT1_2;
250 matrix[FRONT_LEFT ][SIDE_RIGHT] -= surround_mix_level * M_SQRT1_2;
251 matrix[FRONT_RIGHT][SIDE_LEFT ] += surround_mix_level * M_SQRT1_2;
252 matrix[FRONT_RIGHT][SIDE_RIGHT] += surround_mix_level * M_SQRT1_2;
253
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 } else if (matrix_encoding == AV_MATRIX_ENCODING_DPLII) {
254 matrix[FRONT_LEFT ][SIDE_LEFT ] -= surround_mix_level * SQRT3_2;
255 matrix[FRONT_LEFT ][SIDE_RIGHT] -= surround_mix_level * M_SQRT1_2;
256 matrix[FRONT_RIGHT][SIDE_LEFT ] += surround_mix_level * M_SQRT1_2;
257 matrix[FRONT_RIGHT][SIDE_RIGHT] += surround_mix_level * SQRT3_2;
258 } else {
259 2 matrix[ FRONT_LEFT][ SIDE_LEFT] += surround_mix_level;
260 2 matrix[FRONT_RIGHT][SIDE_RIGHT] += surround_mix_level;
261 }
262 } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_CENTER) >= 0) {
263 matrix[ FRONT_CENTER][SIDE_LEFT ]+= surround_mix_level * M_SQRT1_2;
264 matrix[ FRONT_CENTER][SIDE_RIGHT]+= surround_mix_level * M_SQRT1_2;
265 }else
266 av_assert0(0);
267 }
268
269
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if(unaccounted & AV_CH_FRONT_LEFT_OF_CENTER){
270 if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_LEFT) >= 0) {
271 matrix[ FRONT_LEFT][ FRONT_LEFT_OF_CENTER]+= 1.0;
272 matrix[FRONT_RIGHT][FRONT_RIGHT_OF_CENTER]+= 1.0;
273 } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_CENTER) >= 0) {
274 matrix[ FRONT_CENTER][ FRONT_LEFT_OF_CENTER]+= M_SQRT1_2;
275 matrix[ FRONT_CENTER][FRONT_RIGHT_OF_CENTER]+= M_SQRT1_2;
276 }else
277 av_assert0(0);
278 }
279
280
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (unaccounted & AV_CH_TOP_FRONT_LEFT) {
281 if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_TOP_FRONT_CENTER) >= 0) {
282 matrix[TOP_FRONT_CENTER][TOP_FRONT_LEFT ] += M_SQRT1_2;
283 matrix[TOP_FRONT_CENTER][TOP_FRONT_RIGHT] += M_SQRT1_2;
284 if (av_channel_layout_index_from_channel(in_ch_layout, AV_CHAN_TOP_FRONT_CENTER) >= 0)
285 matrix[TOP_FRONT_CENTER][TOP_FRONT_CENTER] = center_mix_level * sqrt(2);
286 } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_LEFT) >= 0) {
287 if (av_channel_layout_index_from_channel(in_ch_layout, AV_CHAN_FRONT_LEFT) >= 0) {
288 matrix[FRONT_LEFT ][TOP_FRONT_LEFT ] += M_SQRT1_2;
289 matrix[FRONT_RIGHT][TOP_FRONT_RIGHT] += M_SQRT1_2;
290 } else {
291 matrix[FRONT_LEFT ][TOP_FRONT_LEFT ] += 1.0;
292 matrix[FRONT_RIGHT][TOP_FRONT_RIGHT] += 1.0;
293 }
294 } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_CENTER) >= 0) {
295 matrix[FRONT_CENTER][TOP_FRONT_LEFT ] += M_SQRT1_2;
296 matrix[FRONT_CENTER][TOP_FRONT_RIGHT] += M_SQRT1_2;
297 } else
298 av_assert0(0);
299 }
300
301 /* mix LFE into front left/right or center */
302
2/2
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 17 times.
19 if (unaccounted & AV_CH_LOW_FREQUENCY) {
303
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
2 if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_CENTER) >= 0) {
304 matrix[FRONT_CENTER][LOW_FREQUENCY] += lfe_mix_level;
305
1/2
✓ Branch 1 taken 2 times.
✗ Branch 2 not taken.
2 } else if (av_channel_layout_index_from_channel(out_ch_layout, AV_CHAN_FRONT_LEFT) >= 0) {
306 2 matrix[FRONT_LEFT ][LOW_FREQUENCY] += lfe_mix_level * M_SQRT1_2;
307 2 matrix[FRONT_RIGHT][LOW_FREQUENCY] += lfe_mix_level * M_SQRT1_2;
308 } else
309 av_assert0(0);
310 }
311
312
313
2/2
✓ Branch 0 taken 1216 times.
✓ Branch 1 taken 19 times.
1235 for (i = 0; i < 64; i++) {
314 1216 double sum=0;
315 1216 int out_i = av_channel_layout_index_from_channel(out_ch_layout, i);
316
2/2
✓ Branch 0 taken 1182 times.
✓ Branch 1 taken 34 times.
1216 if (out_i < 0)
317 1182 continue;
318
2/2
✓ Branch 0 taken 2176 times.
✓ Branch 1 taken 34 times.
2210 for(j=0; j<64; j++){
319 2176 int in_i = av_channel_layout_index_from_channel(in_ch_layout, j);
320
2/2
✓ Branch 0 taken 2061 times.
✓ Branch 1 taken 115 times.
2176 if (in_i < 0)
321 2061 continue;
322
2/4
✓ Branch 0 taken 115 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 115 times.
✗ Branch 3 not taken.
115 if (i < FF_ARRAY_ELEMS(matrix) && j < FF_ARRAY_ELEMS(matrix[0]))
323 115 matrix_param[stride*out_i + in_i] = matrix[i][j];
324 else
325 matrix_param[stride*out_i + in_i] = i == j &&
326 ( av_channel_layout_index_from_channel(in_ch_layout, i) >= 0
327 && av_channel_layout_index_from_channel(out_ch_layout, i) >= 0);
328 115 sum += fabs(matrix_param[stride*out_i + in_i]);
329 }
330
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 30 times.
34 maxcoef= FFMAX(maxcoef, sum);
331 }
332
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if(rematrix_volume < 0)
333 maxcoef = -rematrix_volume;
334
335
3/4
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 8 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 11 times.
19 if(maxcoef > maxval || rematrix_volume < 0){
336 8 maxcoef /= maxval;
337
2/2
✓ Branch 0 taken 512 times.
✓ Branch 1 taken 8 times.
520 for(i=0; i<SWR_CH_MAX; i++)
338
2/2
✓ Branch 0 taken 32768 times.
✓ Branch 1 taken 512 times.
33280 for(j=0; j<SWR_CH_MAX; j++){
339 32768 matrix_param[stride*i + j] /= maxcoef;
340 }
341 }
342 19 }
343
344 19 av_cold int swr_build_matrix2(const AVChannelLayout *in_layout, const AVChannelLayout *out_layout,
345 double center_mix_level, double surround_mix_level,
346 double lfe_mix_level, double maxval,
347 double rematrix_volume, double *matrix_param,
348 ptrdiff_t stride, enum AVMatrixEncoding matrix_encoding, void *log_context)
349 {
350 int i, j, ret;
351 19 AVChannelLayout in_ch_layout = { 0 }, out_ch_layout = { 0 };
352 char buf[128];
353
354 19 ret = clean_layout(&in_ch_layout, in_layout, log_context);
355 19 ret |= clean_layout(&out_ch_layout, out_layout, log_context);
356
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (ret < 0)
357 goto fail;
358
359
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
19 if( !av_channel_layout_compare(&out_ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO_DOWNMIX)
360 && !av_channel_layout_subset(&in_ch_layout, AV_CH_LAYOUT_STEREO_DOWNMIX)
361 ) {
362 av_channel_layout_uninit(&out_ch_layout);
363 out_ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
364 }
365
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
19 if( !av_channel_layout_compare(&in_ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO_DOWNMIX)
366 && !av_channel_layout_subset(&out_ch_layout, AV_CH_LAYOUT_STEREO_DOWNMIX)
367 ) {
368 av_channel_layout_uninit(&in_ch_layout);
369 in_ch_layout = (AVChannelLayout)AV_CHANNEL_LAYOUT_STEREO;
370 }
371
1/4
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
✗ Branch 3 not taken.
✗ Branch 4 not taken.
19 if (!av_channel_layout_compare(&in_ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_22POINT2) &&
372 av_channel_layout_compare(&out_ch_layout, &(AVChannelLayout)AV_CHANNEL_LAYOUT_22POINT2)) {
373 av_channel_layout_from_mask(&in_ch_layout, (AV_CH_LAYOUT_7POINT1_WIDE_BACK|AV_CH_BACK_CENTER));
374 av_channel_layout_describe(&in_ch_layout, buf, sizeof(buf));
375 av_log(log_context, AV_LOG_WARNING,
376 "Full-on remixing from 22.2 has not yet been implemented! "
377 "Processing the input as '%s'\n",
378 buf);
379 }
380
381
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
19 if(!av_channel_layout_check(&in_ch_layout)) {
382 av_log(log_context, AV_LOG_ERROR, "Input channel layout is invalid\n");
383 ret = AVERROR(EINVAL);
384 goto fail;
385 }
386
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
19 if(!sane_layout(&in_ch_layout)) {
387 av_channel_layout_describe(&in_ch_layout, buf, sizeof(buf));
388 av_log(log_context, AV_LOG_ERROR, "Input channel layout '%s' is not supported\n", buf);
389 ret = AVERROR(EINVAL);
390 goto fail;
391 }
392
393
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
19 if(!av_channel_layout_check(&out_ch_layout)) {
394 av_log(log_context, AV_LOG_ERROR, "Output channel layout is invalid\n");
395 ret = AVERROR(EINVAL);
396 goto fail;
397 }
398
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 19 times.
19 if(!sane_layout(&out_ch_layout)) {
399 av_channel_layout_describe(&out_ch_layout, buf, sizeof(buf));
400 av_log(log_context, AV_LOG_ERROR, "Output channel layout '%s' is not supported\n", buf);
401 ret = AVERROR(EINVAL);
402 goto fail;
403 }
404
405 19 build_matrix(&in_ch_layout, &out_ch_layout, center_mix_level,
406 surround_mix_level, lfe_mix_level, maxval, rematrix_volume,
407 matrix_param, stride, matrix_encoding);
408
409
1/2
✓ Branch 0 taken 19 times.
✗ Branch 1 not taken.
19 if(rematrix_volume > 0){
410
2/2
✓ Branch 0 taken 1216 times.
✓ Branch 1 taken 19 times.
1235 for(i=0; i<SWR_CH_MAX; i++)
411
2/2
✓ Branch 0 taken 77824 times.
✓ Branch 1 taken 1216 times.
79040 for(j=0; j<SWR_CH_MAX; j++){
412 77824 matrix_param[stride*i + j] *= rematrix_volume;
413 }
414 }
415
416 19 av_log(log_context, AV_LOG_DEBUG, "Matrix coefficients:\n");
417
2/2
✓ Branch 0 taken 34 times.
✓ Branch 1 taken 19 times.
53 for (i = 0; i < out_ch_layout.nb_channels; i++){
418 34 av_channel_name(buf, sizeof(buf), av_channel_layout_channel_from_index(&out_ch_layout, i));
419 34 av_log(log_context, AV_LOG_DEBUG, "%s: ", buf);
420
2/2
✓ Branch 0 taken 115 times.
✓ Branch 1 taken 34 times.
149 for (j = 0; j < in_ch_layout.nb_channels; j++){
421 115 av_channel_name(buf, sizeof(buf), av_channel_layout_channel_from_index(&in_ch_layout, j));
422 115 av_log(log_context, AV_LOG_DEBUG, "%s:%f ", buf, matrix_param[stride*i + j]);
423 }
424 34 av_log(log_context, AV_LOG_DEBUG, "\n");
425 }
426
427 19 ret = 0;
428 19 fail:
429 19 av_channel_layout_uninit(&in_ch_layout);
430 19 av_channel_layout_uninit(&out_ch_layout);
431
432 19 return ret;
433 }
434
435 19 av_cold static int auto_matrix(SwrContext *s)
436 {
437 double maxval;
438
439
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (s->rematrix_maxval > 0) {
440 maxval = s->rematrix_maxval;
441
2/2
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 18 times.
19 } else if ( av_get_packed_sample_fmt(s->out_sample_fmt) < AV_SAMPLE_FMT_FLT
442
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) {
443 18 maxval = 1.0;
444 } else
445 1 maxval = INT_MAX;
446
447 19 memset(s->matrix, 0, sizeof(s->matrix));
448 38 return swr_build_matrix2(&s->in_ch_layout, &s->out_ch_layout,
449 19 s->clev, s->slev, s->lfe_mix_level,
450 19 maxval, s->rematrix_volume, (double*)s->matrix,
451 19 s->matrix[1] - s->matrix[0], s->matrix_encoding, s);
452 }
453
454 27 av_cold int swri_rematrix_init(SwrContext *s){
455 int i, j;
456 27 int nb_in = s->used_ch_layout.nb_channels;
457 27 int nb_out = s->out.ch_count;
458
459 27 s->mix_any_f = NULL;
460
461
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 8 times.
27 if (!s->rematrix_custom) {
462 19 int r = auto_matrix(s);
463
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (r)
464 return r;
465 }
466
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 8 times.
27 if (s->midbuf.fmt == AV_SAMPLE_FMT_S16P){
467 19 int maxsum = 0;
468 19 s->native_matrix = av_calloc(nb_in * nb_out, sizeof(int));
469
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 19 times.
19 if (!s->native_matrix)
470 return AVERROR(ENOMEM);
471
2/2
✓ Branch 0 taken 42 times.
✓ Branch 1 taken 19 times.
61 for (i = 0; i < nb_out; i++) {
472 42 double rem = 0;
473 42 int sum = 0;
474
475
2/2
✓ Branch 0 taken 166 times.
✓ Branch 1 taken 42 times.
208 for (j = 0; j < nb_in; j++) {
476 166 double target = s->matrix[i][j] * 32768 + rem;
477 166 ((int*)s->native_matrix)[i * nb_in + j] = lrintf(target);
478 166 rem += target - ((int*)s->native_matrix)[i * nb_in + j];
479 166 sum += FFABS(((int*)s->native_matrix)[i * nb_in + j]);
480 }
481 42 maxsum = FFMAX(maxsum, sum);
482 }
483 19 s->native_one.i = 32768;
484
2/2
✓ Branch 0 taken 10 times.
✓ Branch 1 taken 9 times.
19 if (maxsum <= 32768) {
485 10 s->mix_1_1_f = copy_s16;
486 10 s->mix_2_1_f = sum2_s16;
487 10 s->mix_any_f = get_mix_any_func_s16(s);
488 } else {
489 9 s->mix_1_1_f = copy_clip_s16;
490 9 s->mix_2_1_f = sum2_clip_s16;
491 9 s->mix_any_f = get_mix_any_func_clip_s16(s);
492 }
493
1/2
✓ Branch 0 taken 8 times.
✗ Branch 1 not taken.
8 }else if(s->midbuf.fmt == AV_SAMPLE_FMT_FLTP){
494 8 s->native_matrix = av_calloc(nb_in * nb_out, sizeof(float));
495
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 8 times.
8 if (!s->native_matrix)
496 return AVERROR(ENOMEM);
497
2/2
✓ Branch 0 taken 19 times.
✓ Branch 1 taken 8 times.
27 for (i = 0; i < nb_out; i++)
498
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 19 times.
91 for (j = 0; j < nb_in; j++)
499 72 ((float*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
500 8 s->native_one.f = 1.0;
501 8 s->mix_1_1_f = copy_float;
502 8 s->mix_2_1_f = sum2_float;
503 8 s->mix_any_f = get_mix_any_func_float(s);
504 }else if(s->midbuf.fmt == AV_SAMPLE_FMT_DBLP){
505 s->native_matrix = av_calloc(nb_in * nb_out, sizeof(double));
506 if (!s->native_matrix)
507 return AVERROR(ENOMEM);
508 for (i = 0; i < nb_out; i++)
509 for (j = 0; j < nb_in; j++)
510 ((double*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
511 s->native_one.d = 1.0;
512 s->mix_1_1_f = copy_double;
513 s->mix_2_1_f = sum2_double;
514 s->mix_any_f = get_mix_any_func_double(s);
515 }else if(s->midbuf.fmt == AV_SAMPLE_FMT_S32P){
516 s->native_matrix = av_calloc(nb_in * nb_out, sizeof(int));
517 if (!s->native_matrix)
518 return AVERROR(ENOMEM);
519 for (i = 0; i < nb_out; i++) {
520 double rem = 0;
521
522 for (j = 0; j < nb_in; j++) {
523 double target = s->matrix[i][j] * 32768 + rem;
524 ((int*)s->native_matrix)[i * nb_in + j] = lrintf(target);
525 rem += target - ((int*)s->native_matrix)[i * nb_in + j];
526 }
527 }
528 s->native_one.i = 32768;
529 s->mix_1_1_f = copy_s32;
530 s->mix_2_1_f = sum2_s32;
531 s->mix_any_f = get_mix_any_func_s32(s);
532 }else
533 av_assert0(0);
534 //FIXME quantize for integeres
535
2/2
✓ Branch 0 taken 1728 times.
✓ Branch 1 taken 27 times.
1755 for (i = 0; i < SWR_CH_MAX; i++) {
536 1728 int ch_in=0;
537
2/2
✓ Branch 0 taken 110592 times.
✓ Branch 1 taken 1728 times.
112320 for (j = 0; j < SWR_CH_MAX; j++) {
538 110592 const double coeff = s->matrix[i][j];
539
2/2
✓ Branch 0 taken 95 times.
✓ Branch 1 taken 110497 times.
110592 if (coeff)
540 95 s->matrix_ch[i][++ch_in]= j;
541
2/3
✓ Branch 0 taken 32768 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 77824 times.
110592 switch (s->int_sample_fmt) {
542 32768 case AV_SAMPLE_FMT_FLTP:
543 32768 s->matrix_flt[i][j] = coeff;
544 32768 break;
545 case AV_SAMPLE_FMT_DBLP:
546 break;
547 77824 default:
548 77824 s->matrix32[i][j] = lrintf(coeff * 32768);
549 77824 break;
550 }
551 }
552 1728 s->matrix_ch[i][0]= ch_in;
553 }
554
555 #if ARCH_X86 && HAVE_X86ASM && HAVE_MMX
556 27 return swri_rematrix_init_x86(s);
557 #endif
558
559 return 0;
560 }
561
562 4044 av_cold void swri_rematrix_free(SwrContext *s){
563 4044 av_freep(&s->native_matrix);
564 4044 av_freep(&s->native_simd_matrix);
565 4044 }
566
567 1097 int swri_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy){
568 int out_i, in_i, i, j;
569 1097 int len1 = 0;
570 1097 int off = 0;
571
572
2/2
✓ Branch 0 taken 4 times.
✓ Branch 1 taken 1093 times.
1097 if(s->mix_any_f) {
573 4 s->mix_any_f(out->ch, (const uint8_t *const *)in->ch, s->native_matrix, len);
574 4 return 0;
575 }
576
577
3/4
✓ Branch 0 taken 1046 times.
✓ Branch 1 taken 47 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1046 times.
1093 if(s->mix_2_1_simd || s->mix_1_1_simd){
578 47 len1= len&~15;
579 47 off = len1 * out->bps;
580 }
581
582
3/4
✓ Branch 0 taken 1072 times.
✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1072 times.
1093 av_assert0(s->out_ch_layout.order == AV_CHANNEL_ORDER_UNSPEC || out->ch_count == s->out_ch_layout.nb_channels);
583
3/4
✓ Branch 0 taken 1072 times.
✓ Branch 1 taken 21 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1072 times.
1093 av_assert0(s-> in_ch_layout.order == AV_CHANNEL_ORDER_UNSPEC || in ->ch_count == s->in_ch_layout.nb_channels);
584
585
2/2
✓ Branch 0 taken 2263 times.
✓ Branch 1 taken 1093 times.
3356 for(out_i=0; out_i<out->ch_count; out_i++){
586
4/4
✓ Branch 0 taken 534 times.
✓ Branch 1 taken 943 times.
✓ Branch 2 taken 656 times.
✓ Branch 3 taken 130 times.
2263 switch(s->matrix_ch[out_i][0]){
587 534 case 0:
588
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 534 times.
534 if(mustcopy)
589 memset(out->ch[out_i], 0, len * av_get_bytes_per_sample(s->int_sample_fmt));
590 534 break;
591 943 case 1:
592 943 in_i= s->matrix_ch[out_i][1];
593
2/2
✓ Branch 0 taken 158 times.
✓ Branch 1 taken 785 times.
943 if(s->matrix[out_i][in_i]!=1.0){
594
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)
595 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);
596
2/2
✓ Branch 0 taken 64 times.
✓ Branch 1 taken 94 times.
158 if(len != len1)
597 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);
598
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 785 times.
785 }else if(mustcopy){
599 memcpy(out->ch[out_i], in->ch[in_i], len*out->bps);
600 }else{
601 785 out->ch[out_i]= in->ch[in_i];
602 }
603 943 break;
604 656 case 2: {
605 656 int in_i1 = s->matrix_ch[out_i][1];
606 656 int in_i2 = s->matrix_ch[out_i][2];
607
1/4
✗ Branch 0 not taken.
✓ Branch 1 taken 656 times.
✗ Branch 2 not taken.
✗ Branch 3 not taken.
656 if(s->mix_2_1_simd && len1)
608 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);
609 else
610 656 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);
611
2/2
✓ Branch 0 taken 654 times.
✓ Branch 1 taken 2 times.
656 if(len != len1)
612 654 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);
613 656 break;}
614 130 default:
615
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 130 times.
130 if(s->int_sample_fmt == AV_SAMPLE_FMT_FLTP){
616 for(i=0; i<len; i++){
617 float v=0;
618 for(j=0; j<s->matrix_ch[out_i][0]; j++){
619 in_i= s->matrix_ch[out_i][1+j];
620 v+= ((float*)in->ch[in_i])[i] * s->matrix_flt[out_i][in_i];
621 }
622 ((float*)out->ch[out_i])[i]= v;
623 }
624
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 130 times.
130 }else if(s->int_sample_fmt == AV_SAMPLE_FMT_DBLP){
625 for(i=0; i<len; i++){
626 double v=0;
627 for(j=0; j<s->matrix_ch[out_i][0]; j++){
628 in_i= s->matrix_ch[out_i][1+j];
629 v+= ((double*)in->ch[in_i])[i] * s->matrix[out_i][in_i];
630 }
631 ((double*)out->ch[out_i])[i]= v;
632 }
633 }else{
634
2/2
✓ Branch 0 taken 529200 times.
✓ Branch 1 taken 130 times.
529330 for(i=0; i<len; i++){
635 529200 int v=0;
636
2/2
✓ Branch 0 taken 2116800 times.
✓ Branch 1 taken 529200 times.
2646000 for(j=0; j<s->matrix_ch[out_i][0]; j++){
637 2116800 in_i= s->matrix_ch[out_i][1+j];
638 2116800 v+= ((int16_t*)in->ch[in_i])[i] * s->matrix32[out_i][in_i];
639 }
640 529200 ((int16_t*)out->ch[out_i])[i]= (v + 16384)>>15;
641 }
642 }
643 }
644 }
645 1093 return 0;
646 }
647