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 static int even(int64_t layout){
66 if(layout&(layout-1)) return 1;
70 static int sane_layout(int64_t layout){
71 if(!(layout & AV_CH_LAYOUT_SURROUND)) // at least 1 front speaker
73 if(!even(layout & (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT))) // no asymetric front
75 if(!even(layout & (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT))) // no asymetric side
77 if(!even(layout & (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)))
79 if(!even(layout & (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)))
81 if(av_get_channel_layout_nb_channels(layout) >= SWR_CH_MAX)
87 int swr_rematrix_init(SwrContext *s){
89 double matrix[64][64]={{0}};
90 int64_t unaccounted= s->in_ch_layout & ~s->out_ch_layout;
94 if(s->in_ch_layout & s->out_ch_layout & (1LL<<i))
98 if(!sane_layout(s->in_ch_layout)){
99 av_log(s, AV_LOG_ERROR, "Input channel layout isnt supported\n");
100 return AVERROR(EINVAL);
102 if(!sane_layout(s->out_ch_layout)){
103 av_log(s, AV_LOG_ERROR, "Output channel layout isnt supported\n");
104 return AVERROR(EINVAL);
107 //FIXME implement dolby surround
108 //FIXME implement full ac3
111 if(unaccounted & AV_CH_FRONT_CENTER){
112 if((s->out_ch_layout & AV_CH_LAYOUT_STEREO) == AV_CH_LAYOUT_STEREO){
113 matrix[ FRONT_LEFT][FRONT_CENTER]+= M_SQRT1_2;
114 matrix[FRONT_RIGHT][FRONT_CENTER]+= M_SQRT1_2;
118 if(unaccounted & AV_CH_LAYOUT_STEREO){
119 if(s->out_ch_layout & AV_CH_FRONT_CENTER){
120 matrix[FRONT_CENTER][ FRONT_LEFT]+= M_SQRT1_2;
121 matrix[FRONT_CENTER][FRONT_RIGHT]+= M_SQRT1_2;
122 if(s->in_ch_layout & AV_CH_FRONT_CENTER)
123 matrix[FRONT_CENTER][ FRONT_CENTER] = s->clev*sqrt(2);
128 if(unaccounted & AV_CH_BACK_CENTER){
129 if(s->out_ch_layout & AV_CH_BACK_LEFT){
130 matrix[ BACK_LEFT][BACK_CENTER]+= M_SQRT1_2;
131 matrix[BACK_RIGHT][BACK_CENTER]+= M_SQRT1_2;
132 }else if(s->out_ch_layout & AV_CH_SIDE_LEFT){
133 matrix[ SIDE_LEFT][BACK_CENTER]+= M_SQRT1_2;
134 matrix[SIDE_RIGHT][BACK_CENTER]+= M_SQRT1_2;
135 }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
136 matrix[ FRONT_LEFT][BACK_CENTER]+= s->slev*M_SQRT1_2;
137 matrix[FRONT_RIGHT][BACK_CENTER]+= s->slev*M_SQRT1_2;
138 }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
139 matrix[ FRONT_CENTER][BACK_CENTER]+= s->slev*M_SQRT1_2;
143 if(unaccounted & AV_CH_BACK_LEFT){
144 if(s->out_ch_layout & AV_CH_BACK_CENTER){
145 matrix[BACK_CENTER][ BACK_LEFT]+= M_SQRT1_2;
146 matrix[BACK_CENTER][BACK_RIGHT]+= M_SQRT1_2;
147 }else if(s->out_ch_layout & AV_CH_SIDE_LEFT){
148 if(s->in_ch_layout & AV_CH_SIDE_LEFT){
149 matrix[ SIDE_LEFT][ BACK_LEFT]+= M_SQRT1_2;
150 matrix[SIDE_RIGHT][BACK_RIGHT]+= M_SQRT1_2;
152 matrix[ SIDE_LEFT][ BACK_LEFT]+= 1.0;
153 matrix[SIDE_RIGHT][BACK_RIGHT]+= 1.0;
155 }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
156 matrix[ FRONT_LEFT][ BACK_LEFT]+= s->slev;
157 matrix[FRONT_RIGHT][BACK_RIGHT]+= s->slev;
158 }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
159 matrix[ FRONT_CENTER][BACK_LEFT ]+= s->slev*M_SQRT1_2;
160 matrix[ FRONT_CENTER][BACK_RIGHT]+= s->slev*M_SQRT1_2;
165 if(unaccounted & AV_CH_SIDE_LEFT){
166 if(s->out_ch_layout & AV_CH_BACK_LEFT){
167 matrix[ BACK_LEFT][ SIDE_LEFT]+= 1.0;
168 matrix[BACK_RIGHT][SIDE_RIGHT]+= 1.0;
169 }else if(s->out_ch_layout & AV_CH_BACK_CENTER){
170 matrix[BACK_CENTER][ SIDE_LEFT]+= M_SQRT1_2;
171 matrix[BACK_CENTER][SIDE_RIGHT]+= M_SQRT1_2;
172 }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
173 matrix[ FRONT_LEFT][ SIDE_LEFT]+= s->slev;
174 matrix[FRONT_RIGHT][SIDE_RIGHT]+= s->slev;
175 }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
176 matrix[ FRONT_CENTER][SIDE_LEFT ]+= s->slev*M_SQRT1_2;
177 matrix[ FRONT_CENTER][SIDE_RIGHT]+= s->slev*M_SQRT1_2;
182 if(unaccounted & AV_CH_FRONT_LEFT_OF_CENTER){
183 if(s->out_ch_layout & AV_CH_FRONT_LEFT){
184 matrix[ FRONT_LEFT][ FRONT_LEFT_OF_CENTER]+= 1.0;
185 matrix[FRONT_RIGHT][FRONT_RIGHT_OF_CENTER]+= 1.0;
186 }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
187 matrix[ FRONT_CENTER][ FRONT_LEFT_OF_CENTER]+= M_SQRT1_2;
188 matrix[ FRONT_CENTER][FRONT_RIGHT_OF_CENTER]+= M_SQRT1_2;
193 //FIXME quantize for integeres
194 for(out_i=i=0; i<64; i++){
199 s->matrix[out_i][in_i]= matrix[i][j];
200 s->matrix32[out_i][in_i]= lrintf(matrix[i][j] * 32768);
202 s->matrix_ch[out_i][++ch_in]= in_i;
203 sum += fabs(matrix[i][j]);
205 if(s->in_ch_layout & (1ULL<<j))
208 s->matrix_ch[out_i][0]= ch_in;
209 maxcoef= FFMAX(maxcoef, sum);
210 if(s->out_ch_layout & (1ULL<<i))
213 if(s->rematrix_volume < 0)
214 maxcoef = -s->rematrix_volume;
216 if(( s->out_sample_fmt < AV_SAMPLE_FMT_FLT
217 || s->int_sample_fmt < AV_SAMPLE_FMT_FLT) && maxcoef > 1.0){
218 for(i=0; i<SWR_CH_MAX; i++)
219 for(j=0; j<SWR_CH_MAX; j++){
220 s->matrix[i][j] /= maxcoef;
221 s->matrix32[i][j]= lrintf(s->matrix[i][j] * 32768);
225 if(s->rematrix_volume > 0){
226 for(i=0; i<SWR_CH_MAX; i++)
227 for(j=0; j<SWR_CH_MAX; j++){
228 s->matrix[i][j] *= s->rematrix_volume;
229 s->matrix32[i][j]= lrintf(s->matrix[i][j] * 32768);
233 for(i=0; i<av_get_channel_layout_nb_channels(s->out_ch_layout); i++){
234 for(j=0; j<av_get_channel_layout_nb_channels(s->in_ch_layout); j++){
235 av_log(NULL, AV_LOG_DEBUG, "%f ", s->matrix[i][j]);
237 av_log(NULL, AV_LOG_DEBUG, "\n");
242 int swr_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy){
243 int out_i, in_i, i, j;
245 av_assert0(out->ch_count == av_get_channel_layout_nb_channels(s->out_ch_layout));
246 av_assert0(in ->ch_count == av_get_channel_layout_nb_channels(s-> in_ch_layout));
248 for(out_i=0; out_i<out->ch_count; out_i++){
249 switch(s->matrix_ch[out_i][0]){
251 in_i= s->matrix_ch[out_i][1];
252 if(mustcopy || s->matrix[out_i][in_i]!=1.0){
253 if(s->int_sample_fmt == AV_SAMPLE_FMT_FLT){
254 copy_float((float *)out->ch[out_i], (const float *)in->ch[in_i], s->matrix [out_i][in_i], len);
256 copy_s16 ((int16_t*)out->ch[out_i], (const int16_t*)in->ch[in_i], s->matrix32[out_i][in_i], len);
258 out->ch[out_i]= in->ch[in_i];
262 if(s->int_sample_fmt == AV_SAMPLE_FMT_FLT){
263 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] ],
264 s->matrix[out_i][ s->matrix_ch[out_i][1] ], s->matrix[out_i][ s->matrix_ch[out_i][2] ],
267 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] ],
268 s->matrix32[out_i][ s->matrix_ch[out_i][1] ], s->matrix32[out_i][ s->matrix_ch[out_i][2] ],
273 if(s->int_sample_fmt == AV_SAMPLE_FMT_FLT){
274 for(i=0; i<len; i++){
276 for(j=0; j<s->matrix_ch[out_i][0]; j++){
277 in_i= s->matrix_ch[out_i][1+j];
278 v+= ((float*)in->ch[in_i])[i] * s->matrix[out_i][in_i];
280 ((float*)out->ch[out_i])[i]= v;
283 for(i=0; i<len; i++){
285 for(j=0; j<s->matrix_ch[out_i][0]; j++){
286 in_i= s->matrix_ch[out_i][1+j];
287 v+= ((int16_t*)in->ch[in_i])[i] * s->matrix32[out_i][in_i];
289 ((int16_t*)out->ch[out_i])[i]= (v + 16384)>>15;