2 * Copyright (C) 2011-2012 Michael Niedermayer (michaelni@gmx.at)
4 * This file is part of libswresample
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.
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.
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
21 #include "swresample_internal.h"
22 #include "libavutil/audioconvert.h"
23 #include "libavutil/avassert.h"
30 #define RENAME(x) x ## _float
31 #include "rematrix_template.c"
44 #define RENAME(x) x ## _double
45 #include "rematrix_template.c"
54 #define R(x) (((x) + 16384)>>15)
55 #define SAMPLE int16_t
58 #define RENAME(x) x ## _s16
59 #include "rematrix_template.c"
64 #define FRONT_CENTER 2
65 #define LOW_FREQUENCY 3
68 #define FRONT_LEFT_OF_CENTER 6
69 #define FRONT_RIGHT_OF_CENTER 7
74 #define TOP_FRONT_LEFT 12
75 #define TOP_FRONT_CENTER 13
76 #define TOP_FRONT_RIGHT 14
77 #define TOP_BACK_LEFT 15
78 #define TOP_BACK_CENTER 16
79 #define TOP_BACK_RIGHT 17
81 int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride)
83 int nb_in, nb_out, in, out;
85 if (!s || s->in_convert) // s needs to be allocated but not initialized
86 return AVERROR(EINVAL);
87 memset(s->matrix, 0, sizeof(s->matrix));
88 nb_in = av_get_channel_layout_nb_channels(s->in_ch_layout);
89 nb_out = av_get_channel_layout_nb_channels(s->out_ch_layout);
90 for (out = 0; out < nb_out; out++) {
91 for (in = 0; in < nb_in; in++)
92 s->matrix[out][in] = matrix[in];
95 s->rematrix_custom = 1;
99 static int even(int64_t layout){
100 if(!layout) return 1;
101 if(layout&(layout-1)) return 1;
105 static int sane_layout(int64_t layout){
106 if(!(layout & AV_CH_LAYOUT_SURROUND)) // at least 1 front speaker
108 if(!even(layout & (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT))) // no asymetric front
110 if(!even(layout & (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT))) // no asymetric side
112 if(!even(layout & (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)))
114 if(!even(layout & (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)))
116 if(av_get_channel_layout_nb_channels(layout) >= SWR_CH_MAX)
122 static int auto_matrix(SwrContext *s)
125 double matrix[64][64]={{0}};
126 int64_t unaccounted= s->in_ch_layout & ~s->out_ch_layout;
129 memset(s->matrix, 0, sizeof(s->matrix));
131 if(s->in_ch_layout & s->out_ch_layout & (1LL<<i))
135 if(!sane_layout(s->in_ch_layout)){
136 av_log(s, AV_LOG_ERROR, "Input channel layout isnt supported\n");
137 return AVERROR(EINVAL);
139 if(!sane_layout(s->out_ch_layout)){
140 av_log(s, AV_LOG_ERROR, "Output channel layout isnt supported\n");
141 return AVERROR(EINVAL);
144 //FIXME implement dolby surround
145 //FIXME implement full ac3
148 if(unaccounted & AV_CH_FRONT_CENTER){
149 if((s->out_ch_layout & AV_CH_LAYOUT_STEREO) == AV_CH_LAYOUT_STEREO){
150 matrix[ FRONT_LEFT][FRONT_CENTER]+= M_SQRT1_2;
151 matrix[FRONT_RIGHT][FRONT_CENTER]+= M_SQRT1_2;
155 if(unaccounted & AV_CH_LAYOUT_STEREO){
156 if(s->out_ch_layout & AV_CH_FRONT_CENTER){
157 matrix[FRONT_CENTER][ FRONT_LEFT]+= M_SQRT1_2;
158 matrix[FRONT_CENTER][FRONT_RIGHT]+= M_SQRT1_2;
159 if(s->in_ch_layout & AV_CH_FRONT_CENTER)
160 matrix[FRONT_CENTER][ FRONT_CENTER] = s->clev*sqrt(2);
165 if(unaccounted & AV_CH_BACK_CENTER){
166 if(s->out_ch_layout & AV_CH_BACK_LEFT){
167 matrix[ BACK_LEFT][BACK_CENTER]+= M_SQRT1_2;
168 matrix[BACK_RIGHT][BACK_CENTER]+= M_SQRT1_2;
169 }else if(s->out_ch_layout & AV_CH_SIDE_LEFT){
170 matrix[ SIDE_LEFT][BACK_CENTER]+= M_SQRT1_2;
171 matrix[SIDE_RIGHT][BACK_CENTER]+= M_SQRT1_2;
172 }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
173 matrix[ FRONT_LEFT][BACK_CENTER]+= s->slev*M_SQRT1_2;
174 matrix[FRONT_RIGHT][BACK_CENTER]+= s->slev*M_SQRT1_2;
175 }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
176 matrix[ FRONT_CENTER][BACK_CENTER]+= s->slev*M_SQRT1_2;
180 if(unaccounted & AV_CH_BACK_LEFT){
181 if(s->out_ch_layout & AV_CH_BACK_CENTER){
182 matrix[BACK_CENTER][ BACK_LEFT]+= M_SQRT1_2;
183 matrix[BACK_CENTER][BACK_RIGHT]+= M_SQRT1_2;
184 }else if(s->out_ch_layout & AV_CH_SIDE_LEFT){
185 if(s->in_ch_layout & AV_CH_SIDE_LEFT){
186 matrix[ SIDE_LEFT][ BACK_LEFT]+= M_SQRT1_2;
187 matrix[SIDE_RIGHT][BACK_RIGHT]+= M_SQRT1_2;
189 matrix[ SIDE_LEFT][ BACK_LEFT]+= 1.0;
190 matrix[SIDE_RIGHT][BACK_RIGHT]+= 1.0;
192 }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
193 matrix[ FRONT_LEFT][ BACK_LEFT]+= s->slev;
194 matrix[FRONT_RIGHT][BACK_RIGHT]+= s->slev;
195 }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
196 matrix[ FRONT_CENTER][BACK_LEFT ]+= s->slev*M_SQRT1_2;
197 matrix[ FRONT_CENTER][BACK_RIGHT]+= s->slev*M_SQRT1_2;
202 if(unaccounted & AV_CH_SIDE_LEFT){
203 if(s->out_ch_layout & AV_CH_BACK_LEFT){
204 /* if back channels do not exist in the input, just copy side
205 channels to back channels, otherwise mix side into back */
206 if (s->in_ch_layout & AV_CH_BACK_LEFT) {
207 matrix[BACK_LEFT ][SIDE_LEFT ] += M_SQRT1_2;
208 matrix[BACK_RIGHT][SIDE_RIGHT] += M_SQRT1_2;
210 matrix[BACK_LEFT ][SIDE_LEFT ] += 1.0;
211 matrix[BACK_RIGHT][SIDE_RIGHT] += 1.0;
213 }else if(s->out_ch_layout & AV_CH_BACK_CENTER){
214 matrix[BACK_CENTER][ SIDE_LEFT]+= M_SQRT1_2;
215 matrix[BACK_CENTER][SIDE_RIGHT]+= M_SQRT1_2;
216 }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
217 matrix[ FRONT_LEFT][ SIDE_LEFT]+= s->slev;
218 matrix[FRONT_RIGHT][SIDE_RIGHT]+= s->slev;
219 }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
220 matrix[ FRONT_CENTER][SIDE_LEFT ]+= s->slev*M_SQRT1_2;
221 matrix[ FRONT_CENTER][SIDE_RIGHT]+= s->slev*M_SQRT1_2;
226 if(unaccounted & AV_CH_FRONT_LEFT_OF_CENTER){
227 if(s->out_ch_layout & AV_CH_FRONT_LEFT){
228 matrix[ FRONT_LEFT][ FRONT_LEFT_OF_CENTER]+= 1.0;
229 matrix[FRONT_RIGHT][FRONT_RIGHT_OF_CENTER]+= 1.0;
230 }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
231 matrix[ FRONT_CENTER][ FRONT_LEFT_OF_CENTER]+= M_SQRT1_2;
232 matrix[ FRONT_CENTER][FRONT_RIGHT_OF_CENTER]+= M_SQRT1_2;
236 /* mix LFE into front left/right or center */
237 if (unaccounted & AV_CH_LOW_FREQUENCY) {
238 if (s->out_ch_layout & AV_CH_FRONT_CENTER) {
239 matrix[FRONT_CENTER][LOW_FREQUENCY] += s->lfe_mix_level;
240 } else if (s->out_ch_layout & AV_CH_FRONT_LEFT) {
241 matrix[FRONT_LEFT ][LOW_FREQUENCY] += s->lfe_mix_level * M_SQRT1_2;
242 matrix[FRONT_RIGHT][LOW_FREQUENCY] += s->lfe_mix_level * M_SQRT1_2;
247 for(out_i=i=0; i<64; i++){
251 s->matrix[out_i][in_i]= matrix[i][j];
253 sum += fabs(matrix[i][j]);
255 if(s->in_ch_layout & (1ULL<<j))
258 maxcoef= FFMAX(maxcoef, sum);
259 if(s->out_ch_layout & (1ULL<<i))
262 if(s->rematrix_volume < 0)
263 maxcoef = -s->rematrix_volume;
265 if(( av_get_packed_sample_fmt(s->out_sample_fmt) < AV_SAMPLE_FMT_FLT
266 || av_get_packed_sample_fmt(s->int_sample_fmt) < AV_SAMPLE_FMT_FLT) && maxcoef > 1.0){
267 for(i=0; i<SWR_CH_MAX; i++)
268 for(j=0; j<SWR_CH_MAX; j++){
269 s->matrix[i][j] /= maxcoef;
273 if(s->rematrix_volume > 0){
274 for(i=0; i<SWR_CH_MAX; i++)
275 for(j=0; j<SWR_CH_MAX; j++){
276 s->matrix[i][j] *= s->rematrix_volume;
280 for(i=0; i<av_get_channel_layout_nb_channels(s->out_ch_layout); i++){
281 for(j=0; j<av_get_channel_layout_nb_channels(s->in_ch_layout); j++){
282 av_log(NULL, AV_LOG_DEBUG, "%f ", s->matrix[i][j]);
284 av_log(NULL, AV_LOG_DEBUG, "\n");
289 int swri_rematrix_init(SwrContext *s){
291 int nb_in = av_get_channel_layout_nb_channels(s->in_ch_layout);
292 int nb_out = av_get_channel_layout_nb_channels(s->out_ch_layout);
296 if (!s->rematrix_custom) {
297 int r = auto_matrix(s);
301 if (s->midbuf.fmt == AV_SAMPLE_FMT_S16P){
302 s->native_matrix = av_mallocz(nb_in * nb_out * sizeof(int));
303 s->native_one = av_mallocz(sizeof(int));
304 for (i = 0; i < nb_out; i++)
305 for (j = 0; j < nb_in; j++)
306 ((int*)s->native_matrix)[i * nb_in + j] = lrintf(s->matrix[i][j] * 32768);
307 *((int*)s->native_one) = 32768;
308 s->mix_1_1_f = copy_s16;
309 s->mix_2_1_f = sum2_s16;
310 s->mix_any_f = get_mix_any_func_s16(s);
311 }else if(s->midbuf.fmt == AV_SAMPLE_FMT_FLTP){
312 s->native_matrix = av_mallocz(nb_in * nb_out * sizeof(float));
313 s->native_one = av_mallocz(sizeof(float));
314 for (i = 0; i < nb_out; i++)
315 for (j = 0; j < nb_in; j++)
316 ((float*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
317 *((float*)s->native_one) = 1.0;
318 s->mix_1_1_f = copy_float;
319 s->mix_2_1_f = sum2_float;
320 s->mix_any_f = get_mix_any_func_float(s);
321 }else if(s->midbuf.fmt == AV_SAMPLE_FMT_DBLP){
322 s->native_matrix = av_mallocz(nb_in * nb_out * sizeof(double));
323 s->native_one = av_mallocz(sizeof(double));
324 for (i = 0; i < nb_out; i++)
325 for (j = 0; j < nb_in; j++)
326 ((double*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
327 *((double*)s->native_one) = 1.0;
328 s->mix_1_1_f = copy_double;
329 s->mix_2_1_f = sum2_double;
330 s->mix_any_f = get_mix_any_func_double(s);
333 //FIXME quantize for integeres
334 for (i = 0; i < SWR_CH_MAX; i++) {
336 for (j = 0; j < SWR_CH_MAX; j++) {
337 s->matrix32[i][j]= lrintf(s->matrix[i][j] * 32768);
339 s->matrix_ch[i][++ch_in]= j;
341 s->matrix_ch[i][0]= ch_in;
346 void swri_rematrix_free(SwrContext *s){
347 av_freep(&s->native_matrix);
348 av_freep(&s->native_one);
351 int swri_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy){
352 int out_i, in_i, i, j;
355 s->mix_any_f(out->ch, in->ch, s->native_matrix, len);
359 av_assert0(out->ch_count == av_get_channel_layout_nb_channels(s->out_ch_layout));
360 av_assert0(in ->ch_count == av_get_channel_layout_nb_channels(s-> in_ch_layout));
362 for(out_i=0; out_i<out->ch_count; out_i++){
363 switch(s->matrix_ch[out_i][0]){
366 memset(out->ch[out_i], 0, len * av_get_bytes_per_sample(s->int_sample_fmt));
369 in_i= s->matrix_ch[out_i][1];
370 if(s->matrix[out_i][in_i]!=1.0){
371 s->mix_1_1_f(out->ch[out_i], in->ch[in_i], s->native_matrix, in->ch_count*out_i + in_i, len);
373 memcpy(out->ch[out_i], in->ch[in_i], len*out->bps);
375 out->ch[out_i]= in->ch[in_i];
379 int in_i1 = s->matrix_ch[out_i][1];
380 int in_i2 = s->matrix_ch[out_i][2];
381 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, len);
384 if(s->int_sample_fmt == AV_SAMPLE_FMT_FLTP){
385 for(i=0; i<len; i++){
387 for(j=0; j<s->matrix_ch[out_i][0]; j++){
388 in_i= s->matrix_ch[out_i][1+j];
389 v+= ((float*)in->ch[in_i])[i] * s->matrix[out_i][in_i];
391 ((float*)out->ch[out_i])[i]= v;
393 }else if(s->int_sample_fmt == AV_SAMPLE_FMT_DBLP){
394 for(i=0; i<len; i++){
396 for(j=0; j<s->matrix_ch[out_i][0]; j++){
397 in_i= s->matrix_ch[out_i][1+j];
398 v+= ((double*)in->ch[in_i])[i] * s->matrix[out_i][in_i];
400 ((double*)out->ch[out_i])[i]= v;
403 for(i=0; i<len; i++){
405 for(j=0; j<s->matrix_ch[out_i][0]; j++){
406 in_i= s->matrix_ch[out_i][1+j];
407 v+= ((int16_t*)in->ch[in_i])[i] * s->matrix32[out_i][in_i];
409 ((int16_t*)out->ch[out_i])[i]= (v + 16384)>>15;