FFmpeg coverage


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