2 * Copyright (C) 2011 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"
29 #define RENAME(x) x ## _float
30 #include "rematrix_template.c"
38 #define R(x) (((x) + 16384)>>15)
39 #define SAMPLE int16_t
41 #define RENAME(x) x ## _s16
42 #include "rematrix_template.c"
47 #define FRONT_CENTER 2
48 #define LOW_FREQUENCY 3
51 #define FRONT_LEFT_OF_CENTER 6
52 #define FRONT_RIGHT_OF_CENTER 7
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
64 int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride)
66 int nb_in, nb_out, in, out;
68 if (!s || s->in_convert) // s needs to be allocated but not initialized
69 return AVERROR(EINVAL);
70 memset(s->matrix, 0, sizeof(s->matrix));
71 nb_in = av_get_channel_layout_nb_channels(s->in_ch_layout);
72 nb_out = av_get_channel_layout_nb_channels(s->out_ch_layout);
73 for (out = 0; out < nb_out; out++) {
74 for (in = 0; in < nb_in; in++)
75 s->matrix[out][in] = matrix[in];
78 s->rematrix_custom = 1;
82 static int even(int64_t layout){
84 if(layout&(layout-1)) return 1;
88 static int sane_layout(int64_t layout){
89 if(!(layout & AV_CH_LAYOUT_SURROUND)) // at least 1 front speaker
91 if(!even(layout & (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT))) // no asymetric front
93 if(!even(layout & (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT))) // no asymetric side
95 if(!even(layout & (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)))
97 if(!even(layout & (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)))
99 if(av_get_channel_layout_nb_channels(layout) >= SWR_CH_MAX)
105 static int auto_matrix(SwrContext *s)
108 double matrix[64][64]={{0}};
109 int64_t unaccounted= s->in_ch_layout & ~s->out_ch_layout;
112 memset(s->matrix, 0, sizeof(s->matrix));
114 if(s->in_ch_layout & s->out_ch_layout & (1LL<<i))
118 if(!sane_layout(s->in_ch_layout)){
119 av_log(s, AV_LOG_ERROR, "Input channel layout isnt supported\n");
120 return AVERROR(EINVAL);
122 if(!sane_layout(s->out_ch_layout)){
123 av_log(s, AV_LOG_ERROR, "Output channel layout isnt supported\n");
124 return AVERROR(EINVAL);
127 //FIXME implement dolby surround
128 //FIXME implement full ac3
131 if(unaccounted & AV_CH_FRONT_CENTER){
132 if((s->out_ch_layout & AV_CH_LAYOUT_STEREO) == AV_CH_LAYOUT_STEREO){
133 matrix[ FRONT_LEFT][FRONT_CENTER]+= M_SQRT1_2;
134 matrix[FRONT_RIGHT][FRONT_CENTER]+= M_SQRT1_2;
138 if(unaccounted & AV_CH_LAYOUT_STEREO){
139 if(s->out_ch_layout & AV_CH_FRONT_CENTER){
140 matrix[FRONT_CENTER][ FRONT_LEFT]+= M_SQRT1_2;
141 matrix[FRONT_CENTER][FRONT_RIGHT]+= M_SQRT1_2;
142 if(s->in_ch_layout & AV_CH_FRONT_CENTER)
143 matrix[FRONT_CENTER][ FRONT_CENTER] = s->clev*sqrt(2);
148 if(unaccounted & AV_CH_BACK_CENTER){
149 if(s->out_ch_layout & AV_CH_BACK_LEFT){
150 matrix[ BACK_LEFT][BACK_CENTER]+= M_SQRT1_2;
151 matrix[BACK_RIGHT][BACK_CENTER]+= M_SQRT1_2;
152 }else if(s->out_ch_layout & AV_CH_SIDE_LEFT){
153 matrix[ SIDE_LEFT][BACK_CENTER]+= M_SQRT1_2;
154 matrix[SIDE_RIGHT][BACK_CENTER]+= M_SQRT1_2;
155 }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
156 matrix[ FRONT_LEFT][BACK_CENTER]+= s->slev*M_SQRT1_2;
157 matrix[FRONT_RIGHT][BACK_CENTER]+= s->slev*M_SQRT1_2;
158 }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
159 matrix[ FRONT_CENTER][BACK_CENTER]+= s->slev*M_SQRT1_2;
163 if(unaccounted & AV_CH_BACK_LEFT){
164 if(s->out_ch_layout & AV_CH_BACK_CENTER){
165 matrix[BACK_CENTER][ BACK_LEFT]+= M_SQRT1_2;
166 matrix[BACK_CENTER][BACK_RIGHT]+= M_SQRT1_2;
167 }else if(s->out_ch_layout & AV_CH_SIDE_LEFT){
168 if(s->in_ch_layout & AV_CH_SIDE_LEFT){
169 matrix[ SIDE_LEFT][ BACK_LEFT]+= M_SQRT1_2;
170 matrix[SIDE_RIGHT][BACK_RIGHT]+= M_SQRT1_2;
172 matrix[ SIDE_LEFT][ BACK_LEFT]+= 1.0;
173 matrix[SIDE_RIGHT][BACK_RIGHT]+= 1.0;
175 }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
176 matrix[ FRONT_LEFT][ BACK_LEFT]+= s->slev;
177 matrix[FRONT_RIGHT][BACK_RIGHT]+= s->slev;
178 }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
179 matrix[ FRONT_CENTER][BACK_LEFT ]+= s->slev*M_SQRT1_2;
180 matrix[ FRONT_CENTER][BACK_RIGHT]+= s->slev*M_SQRT1_2;
185 if(unaccounted & AV_CH_SIDE_LEFT){
186 if(s->out_ch_layout & AV_CH_BACK_LEFT){
187 matrix[ BACK_LEFT][ SIDE_LEFT]+= 1.0;
188 matrix[BACK_RIGHT][SIDE_RIGHT]+= 1.0;
189 }else if(s->out_ch_layout & AV_CH_BACK_CENTER){
190 matrix[BACK_CENTER][ SIDE_LEFT]+= M_SQRT1_2;
191 matrix[BACK_CENTER][SIDE_RIGHT]+= M_SQRT1_2;
192 }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
193 matrix[ FRONT_LEFT][ SIDE_LEFT]+= s->slev;
194 matrix[FRONT_RIGHT][SIDE_RIGHT]+= s->slev;
195 }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
196 matrix[ FRONT_CENTER][SIDE_LEFT ]+= s->slev*M_SQRT1_2;
197 matrix[ FRONT_CENTER][SIDE_RIGHT]+= s->slev*M_SQRT1_2;
202 if(unaccounted & AV_CH_FRONT_LEFT_OF_CENTER){
203 if(s->out_ch_layout & AV_CH_FRONT_LEFT){
204 matrix[ FRONT_LEFT][ FRONT_LEFT_OF_CENTER]+= 1.0;
205 matrix[FRONT_RIGHT][FRONT_RIGHT_OF_CENTER]+= 1.0;
206 }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
207 matrix[ FRONT_CENTER][ FRONT_LEFT_OF_CENTER]+= M_SQRT1_2;
208 matrix[ FRONT_CENTER][FRONT_RIGHT_OF_CENTER]+= M_SQRT1_2;
212 for(out_i=i=0; i<64; i++){
216 s->matrix[out_i][in_i]= matrix[i][j];
218 sum += fabs(matrix[i][j]);
220 if(s->in_ch_layout & (1ULL<<j))
223 maxcoef= FFMAX(maxcoef, sum);
224 if(s->out_ch_layout & (1ULL<<i))
227 if(s->rematrix_volume < 0)
228 maxcoef = -s->rematrix_volume;
230 if(( s->out_sample_fmt < AV_SAMPLE_FMT_FLT
231 || s->int_sample_fmt < AV_SAMPLE_FMT_FLT) && maxcoef > 1.0){
232 for(i=0; i<SWR_CH_MAX; i++)
233 for(j=0; j<SWR_CH_MAX; j++){
234 s->matrix[i][j] /= maxcoef;
238 if(s->rematrix_volume > 0){
239 for(i=0; i<SWR_CH_MAX; i++)
240 for(j=0; j<SWR_CH_MAX; j++){
241 s->matrix[i][j] *= s->rematrix_volume;
245 for(i=0; i<av_get_channel_layout_nb_channels(s->out_ch_layout); i++){
246 for(j=0; j<av_get_channel_layout_nb_channels(s->in_ch_layout); j++){
247 av_log(NULL, AV_LOG_DEBUG, "%f ", s->matrix[i][j]);
249 av_log(NULL, AV_LOG_DEBUG, "\n");
254 int swri_rematrix_init(SwrContext *s){
257 if (!s->rematrix_custom) {
258 int r = auto_matrix(s);
262 //FIXME quantize for integeres
263 for (i = 0; i < SWR_CH_MAX; i++) {
265 for (j = 0; j < SWR_CH_MAX; j++) {
266 s->matrix32[i][j]= lrintf(s->matrix[i][j] * 32768);
268 s->matrix_ch[i][++ch_in]= j;
270 s->matrix_ch[i][0]= ch_in;
275 int swri_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy){
276 int out_i, in_i, i, j;
278 av_assert0(out->ch_count == av_get_channel_layout_nb_channels(s->out_ch_layout));
279 av_assert0(in ->ch_count == av_get_channel_layout_nb_channels(s-> in_ch_layout));
281 for(out_i=0; out_i<out->ch_count; out_i++){
282 switch(s->matrix_ch[out_i][0]){
284 in_i= s->matrix_ch[out_i][1];
285 if(mustcopy || s->matrix[out_i][in_i]!=1.0){
286 if(s->int_sample_fmt == AV_SAMPLE_FMT_FLT){
287 copy_float((float *)out->ch[out_i], (const float *)in->ch[in_i], s->matrix [out_i][in_i], len);
289 copy_s16 ((int16_t*)out->ch[out_i], (const int16_t*)in->ch[in_i], s->matrix32[out_i][in_i], len);
291 out->ch[out_i]= in->ch[in_i];
295 if(s->int_sample_fmt == AV_SAMPLE_FMT_FLT){
296 sum2_float((float *)out->ch[out_i], (const float *)in->ch[ s->matrix_ch[out_i][1] ], (const float *)in->ch[ s->matrix_ch[out_i][2] ],
297 s->matrix[out_i][ s->matrix_ch[out_i][1] ], s->matrix[out_i][ s->matrix_ch[out_i][2] ],
300 sum2_s16 ((int16_t*)out->ch[out_i], (const int16_t*)in->ch[ s->matrix_ch[out_i][1] ], (const int16_t*)in->ch[ s->matrix_ch[out_i][2] ],
301 s->matrix32[out_i][ s->matrix_ch[out_i][1] ], s->matrix32[out_i][ s->matrix_ch[out_i][2] ],
306 if(s->int_sample_fmt == AV_SAMPLE_FMT_FLT){
307 for(i=0; i<len; i++){
309 for(j=0; j<s->matrix_ch[out_i][0]; j++){
310 in_i= s->matrix_ch[out_i][1+j];
311 v+= ((float*)in->ch[in_i])[i] * s->matrix[out_i][in_i];
313 ((float*)out->ch[out_i])[i]= v;
316 for(i=0; i<len; i++){
318 for(j=0; j<s->matrix_ch[out_i][0]; j++){
319 in_i= s->matrix_ch[out_i][1+j];
320 v+= ((int16_t*)in->ch[in_i])[i] * s->matrix32[out_i][in_i];
322 ((int16_t*)out->ch[out_i])[i]= (v + 16384)>>15;