1 /*
2 * Copyright (C) 2011 Michael Niedermayer (michaelni@gmx.at)
3 * Copyright (c) 2012 Justin Ruggles <justin.ruggles@gmail.com>
4 *
5 * This file is part of FFmpeg.
6 *
7 * FFmpeg is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * FFmpeg is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with FFmpeg; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <stdint.h>
23
31
32 /* channel positions */
35 #define FRONT_CENTER 2
36 #define LOW_FREQUENCY 3
39 #define FRONT_LEFT_OF_CENTER 6
40 #define FRONT_RIGHT_OF_CENTER 7
45 #define TOP_FRONT_LEFT 12
46 #define TOP_FRONT_CENTER 13
47 #define TOP_FRONT_RIGHT 14
48 #define TOP_BACK_LEFT 15
49 #define TOP_BACK_CENTER 16
50 #define TOP_BACK_RIGHT 17
51 #define STEREO_LEFT 29
52 #define STEREO_RIGHT 30
55 #define SURROUND_DIRECT_LEFT 33
56 #define SURROUND_DIRECT_RIGHT 34
57 #define LOW_FREQUENCY_2 35
58
59 #define SQRT3_2 1.22474487139158904909 /* sqrt(3/2) */
60
62 {
63 return (!layout || (layout & (layout - 1)));
64 }
65
67 {
68 /* check that there is at least 1 front speaker */
70 return 0;
71
72 /* check for left/right symmetry */
82 return 0;
83
84 return 1;
85 }
86
88 double center_mix_level, double surround_mix_level,
89 double lfe_mix_level, int normalize,
90 double *matrix_out,
int stride,
92 {
93 int i, j, out_i, out_j;
94 double matrix[64][64] = {{0}};
95 int64_t unaccounted;
96 double maxcoef = 0;
97 int in_channels, out_channels;
98
101 }
102
103 unaccounted = in_layout & ~out_layout;
104
107
108 memset(matrix_out, 0, out_channels * stride * sizeof(*matrix_out));
109
110 /* check if layouts are supported */
115
116 /* check if layouts are unbalanced or abnormal */
119
120 /* route matching input/output channels */
121 for (i = 0; i < 64; i++) {
122 if (in_layout & out_layout & (1ULL << i))
123 matrix[i][i] = 1.0;
124 }
125
126 /* mix front center to front left/right */
131 } else
133 }
134 /* mix front left/right to center */
136 if (out_layout & AV_CH_FRONT_CENTER) {
139 /* mix left/right/center to center */
140 if (in_layout & AV_CH_FRONT_CENTER)
142 } else
144 }
145 /* mix back center to back, side, or front */
156 if (unaccounted & (AV_CH_BACK_LEFT | AV_CH_SIDE_LEFT)) {
159 } else {
162 }
163 } else {
166 }
167 } else if (out_layout & AV_CH_FRONT_CENTER) {
169 } else
171 }
172 /* mix back left/right to back center, side, or front */
174 if (out_layout & AV_CH_BACK_CENTER) {
178 /* if side channels do not exist in the input, just copy back
179 channels to side channels, otherwise mix back into side */
180 if (in_layout & AV_CH_SIDE_LEFT) {
183 } else {
186 }
198 } else {
201 }
202 } else if (out_layout & AV_CH_FRONT_CENTER) {
205 } else
207 }
208 /* mix side left/right into back or front */
210 if (out_layout & AV_CH_BACK_LEFT) {
211 /* if back channels do not exist in the input, just copy side
212 channels to back channels, otherwise mix side into back */
213 if (in_layout & AV_CH_BACK_LEFT) {
216 } else {
219 }
220 } else if (out_layout & AV_CH_BACK_CENTER) {
234 } else {
237 }
238 } else if (out_layout & AV_CH_FRONT_CENTER) {
241 } else
243 }
244 /* mix left-of-center/right-of-center into front left/right or center */
249 } else if (out_layout & AV_CH_FRONT_CENTER) {
252 } else
254 }
255 /* mix LFE into front left/right or center */
257 if (out_layout & AV_CH_FRONT_CENTER) {
262 } else
264 }
265
266 /* transfer internal matrix to output matrix and calculate maximum
267 per-channel coefficient sum */
268 for (out_i = i = 0; out_i < out_channels && i < 64; i++) {
269 double sum = 0;
270 for (out_j = j = 0; out_j < in_channels && j < 64; j++) {
271 matrix_out[out_i * stride + out_j] = matrix[i][j];
272 sum += fabs(matrix[i][j]);
273 if (in_layout & (1ULL << j))
274 out_j++;
275 }
276 maxcoef =
FFMAX(maxcoef, sum);
277 if (out_layout & (1ULL << i))
278 out_i++;
279 }
280
281 /* normalize */
282 if (normalize && maxcoef > 1.0) {
283 for (i = 0; i < out_channels; i++)
284 for (j = 0; j < in_channels; j++)
285 matrix_out[i * stride + j] /= maxcoef;
286 }
287
288 return 0;
289 }