00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "swresample_internal.h"
00022 #include "libavutil/audioconvert.h"
00023 #include "libavutil/avassert.h"
00024
00025 #define ONE (1.0)
00026 #define R(x) x
00027 #define SAMPLE float
00028 #define COEFF float
00029 #define RENAME(x) x ## _float
00030 #include "rematrix_template.c"
00031 #undef SAMPLE
00032 #undef RENAME
00033 #undef R
00034 #undef ONE
00035 #undef COEFF
00036
00037 #define ONE (1.0)
00038 #define R(x) x
00039 #define SAMPLE double
00040 #define COEFF double
00041 #define RENAME(x) x ## _double
00042 #include "rematrix_template.c"
00043 #undef SAMPLE
00044 #undef RENAME
00045 #undef R
00046 #undef ONE
00047 #undef COEFF
00048
00049 #define ONE (-32768)
00050 #define R(x) (((x) + 16384)>>15)
00051 #define SAMPLE int16_t
00052 #define COEFF int
00053 #define RENAME(x) x ## _s16
00054 #include "rematrix_template.c"
00055
00056
00057 #define FRONT_LEFT 0
00058 #define FRONT_RIGHT 1
00059 #define FRONT_CENTER 2
00060 #define LOW_FREQUENCY 3
00061 #define BACK_LEFT 4
00062 #define BACK_RIGHT 5
00063 #define FRONT_LEFT_OF_CENTER 6
00064 #define FRONT_RIGHT_OF_CENTER 7
00065 #define BACK_CENTER 8
00066 #define SIDE_LEFT 9
00067 #define SIDE_RIGHT 10
00068 #define TOP_CENTER 11
00069 #define TOP_FRONT_LEFT 12
00070 #define TOP_FRONT_CENTER 13
00071 #define TOP_FRONT_RIGHT 14
00072 #define TOP_BACK_LEFT 15
00073 #define TOP_BACK_CENTER 16
00074 #define TOP_BACK_RIGHT 17
00075
00076 int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride)
00077 {
00078 int nb_in, nb_out, in, out;
00079
00080 if (!s || s->in_convert)
00081 return AVERROR(EINVAL);
00082 memset(s->matrix, 0, sizeof(s->matrix));
00083 nb_in = av_get_channel_layout_nb_channels(s->in_ch_layout);
00084 nb_out = av_get_channel_layout_nb_channels(s->out_ch_layout);
00085 for (out = 0; out < nb_out; out++) {
00086 for (in = 0; in < nb_in; in++)
00087 s->matrix[out][in] = matrix[in];
00088 matrix += stride;
00089 }
00090 s->rematrix_custom = 1;
00091 return 0;
00092 }
00093
00094 static int even(int64_t layout){
00095 if(!layout) return 1;
00096 if(layout&(layout-1)) return 1;
00097 return 0;
00098 }
00099
00100 static int sane_layout(int64_t layout){
00101 if(!(layout & AV_CH_LAYOUT_SURROUND))
00102 return 0;
00103 if(!even(layout & (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT)))
00104 return 0;
00105 if(!even(layout & (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)))
00106 return 0;
00107 if(!even(layout & (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)))
00108 return 0;
00109 if(!even(layout & (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)))
00110 return 0;
00111 if(av_get_channel_layout_nb_channels(layout) >= SWR_CH_MAX)
00112 return 0;
00113
00114 return 1;
00115 }
00116
00117 static int auto_matrix(SwrContext *s)
00118 {
00119 int i, j, out_i;
00120 double matrix[64][64]={{0}};
00121 int64_t unaccounted= s->in_ch_layout & ~s->out_ch_layout;
00122 double maxcoef=0;
00123
00124 memset(s->matrix, 0, sizeof(s->matrix));
00125 for(i=0; i<64; i++){
00126 if(s->in_ch_layout & s->out_ch_layout & (1LL<<i))
00127 matrix[i][i]= 1.0;
00128 }
00129
00130 if(!sane_layout(s->in_ch_layout)){
00131 av_log(s, AV_LOG_ERROR, "Input channel layout isnt supported\n");
00132 return AVERROR(EINVAL);
00133 }
00134 if(!sane_layout(s->out_ch_layout)){
00135 av_log(s, AV_LOG_ERROR, "Output channel layout isnt supported\n");
00136 return AVERROR(EINVAL);
00137 }
00138
00139
00140
00141
00142
00143 if(unaccounted & AV_CH_FRONT_CENTER){
00144 if((s->out_ch_layout & AV_CH_LAYOUT_STEREO) == AV_CH_LAYOUT_STEREO){
00145 matrix[ FRONT_LEFT][FRONT_CENTER]+= M_SQRT1_2;
00146 matrix[FRONT_RIGHT][FRONT_CENTER]+= M_SQRT1_2;
00147 }else
00148 av_assert0(0);
00149 }
00150 if(unaccounted & AV_CH_LAYOUT_STEREO){
00151 if(s->out_ch_layout & AV_CH_FRONT_CENTER){
00152 matrix[FRONT_CENTER][ FRONT_LEFT]+= M_SQRT1_2;
00153 matrix[FRONT_CENTER][FRONT_RIGHT]+= M_SQRT1_2;
00154 if(s->in_ch_layout & AV_CH_FRONT_CENTER)
00155 matrix[FRONT_CENTER][ FRONT_CENTER] = s->clev*sqrt(2);
00156 }else
00157 av_assert0(0);
00158 }
00159
00160 if(unaccounted & AV_CH_BACK_CENTER){
00161 if(s->out_ch_layout & AV_CH_BACK_LEFT){
00162 matrix[ BACK_LEFT][BACK_CENTER]+= M_SQRT1_2;
00163 matrix[BACK_RIGHT][BACK_CENTER]+= M_SQRT1_2;
00164 }else if(s->out_ch_layout & AV_CH_SIDE_LEFT){
00165 matrix[ SIDE_LEFT][BACK_CENTER]+= M_SQRT1_2;
00166 matrix[SIDE_RIGHT][BACK_CENTER]+= M_SQRT1_2;
00167 }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
00168 matrix[ FRONT_LEFT][BACK_CENTER]+= s->slev*M_SQRT1_2;
00169 matrix[FRONT_RIGHT][BACK_CENTER]+= s->slev*M_SQRT1_2;
00170 }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
00171 matrix[ FRONT_CENTER][BACK_CENTER]+= s->slev*M_SQRT1_2;
00172 }else
00173 av_assert0(0);
00174 }
00175 if(unaccounted & AV_CH_BACK_LEFT){
00176 if(s->out_ch_layout & AV_CH_BACK_CENTER){
00177 matrix[BACK_CENTER][ BACK_LEFT]+= M_SQRT1_2;
00178 matrix[BACK_CENTER][BACK_RIGHT]+= M_SQRT1_2;
00179 }else if(s->out_ch_layout & AV_CH_SIDE_LEFT){
00180 if(s->in_ch_layout & AV_CH_SIDE_LEFT){
00181 matrix[ SIDE_LEFT][ BACK_LEFT]+= M_SQRT1_2;
00182 matrix[SIDE_RIGHT][BACK_RIGHT]+= M_SQRT1_2;
00183 }else{
00184 matrix[ SIDE_LEFT][ BACK_LEFT]+= 1.0;
00185 matrix[SIDE_RIGHT][BACK_RIGHT]+= 1.0;
00186 }
00187 }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
00188 matrix[ FRONT_LEFT][ BACK_LEFT]+= s->slev;
00189 matrix[FRONT_RIGHT][BACK_RIGHT]+= s->slev;
00190 }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
00191 matrix[ FRONT_CENTER][BACK_LEFT ]+= s->slev*M_SQRT1_2;
00192 matrix[ FRONT_CENTER][BACK_RIGHT]+= s->slev*M_SQRT1_2;
00193 }else
00194 av_assert0(0);
00195 }
00196
00197 if(unaccounted & AV_CH_SIDE_LEFT){
00198 if(s->out_ch_layout & AV_CH_BACK_LEFT){
00199
00200
00201 if (s->in_ch_layout & AV_CH_BACK_LEFT) {
00202 matrix[BACK_LEFT ][SIDE_LEFT ] += M_SQRT1_2;
00203 matrix[BACK_RIGHT][SIDE_RIGHT] += M_SQRT1_2;
00204 } else {
00205 matrix[BACK_LEFT ][SIDE_LEFT ] += 1.0;
00206 matrix[BACK_RIGHT][SIDE_RIGHT] += 1.0;
00207 }
00208 }else if(s->out_ch_layout & AV_CH_BACK_CENTER){
00209 matrix[BACK_CENTER][ SIDE_LEFT]+= M_SQRT1_2;
00210 matrix[BACK_CENTER][SIDE_RIGHT]+= M_SQRT1_2;
00211 }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
00212 matrix[ FRONT_LEFT][ SIDE_LEFT]+= s->slev;
00213 matrix[FRONT_RIGHT][SIDE_RIGHT]+= s->slev;
00214 }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
00215 matrix[ FRONT_CENTER][SIDE_LEFT ]+= s->slev*M_SQRT1_2;
00216 matrix[ FRONT_CENTER][SIDE_RIGHT]+= s->slev*M_SQRT1_2;
00217 }else
00218 av_assert0(0);
00219 }
00220
00221 if(unaccounted & AV_CH_FRONT_LEFT_OF_CENTER){
00222 if(s->out_ch_layout & AV_CH_FRONT_LEFT){
00223 matrix[ FRONT_LEFT][ FRONT_LEFT_OF_CENTER]+= 1.0;
00224 matrix[FRONT_RIGHT][FRONT_RIGHT_OF_CENTER]+= 1.0;
00225 }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
00226 matrix[ FRONT_CENTER][ FRONT_LEFT_OF_CENTER]+= M_SQRT1_2;
00227 matrix[ FRONT_CENTER][FRONT_RIGHT_OF_CENTER]+= M_SQRT1_2;
00228 }else
00229 av_assert0(0);
00230 }
00231
00232 if (unaccounted & AV_CH_LOW_FREQUENCY) {
00233 if (s->out_ch_layout & AV_CH_FRONT_CENTER) {
00234 matrix[FRONT_CENTER][LOW_FREQUENCY] += s->lfe_mix_level;
00235 } else if (s->out_ch_layout & AV_CH_FRONT_LEFT) {
00236 matrix[FRONT_LEFT ][LOW_FREQUENCY] += s->lfe_mix_level * M_SQRT1_2;
00237 matrix[FRONT_RIGHT][LOW_FREQUENCY] += s->lfe_mix_level * M_SQRT1_2;
00238 } else
00239 av_assert0(0);
00240 }
00241
00242 for(out_i=i=0; i<64; i++){
00243 double sum=0;
00244 int in_i=0;
00245 for(j=0; j<64; j++){
00246 s->matrix[out_i][in_i]= matrix[i][j];
00247 if(matrix[i][j]){
00248 sum += fabs(matrix[i][j]);
00249 }
00250 if(s->in_ch_layout & (1ULL<<j))
00251 in_i++;
00252 }
00253 maxcoef= FFMAX(maxcoef, sum);
00254 if(s->out_ch_layout & (1ULL<<i))
00255 out_i++;
00256 }
00257 if(s->rematrix_volume < 0)
00258 maxcoef = -s->rematrix_volume;
00259
00260 if(( av_get_packed_sample_fmt(s->out_sample_fmt) < AV_SAMPLE_FMT_FLT
00261 || av_get_packed_sample_fmt(s->int_sample_fmt) < AV_SAMPLE_FMT_FLT) && maxcoef > 1.0){
00262 for(i=0; i<SWR_CH_MAX; i++)
00263 for(j=0; j<SWR_CH_MAX; j++){
00264 s->matrix[i][j] /= maxcoef;
00265 }
00266 }
00267
00268 if(s->rematrix_volume > 0){
00269 for(i=0; i<SWR_CH_MAX; i++)
00270 for(j=0; j<SWR_CH_MAX; j++){
00271 s->matrix[i][j] *= s->rematrix_volume;
00272 }
00273 }
00274
00275 for(i=0; i<av_get_channel_layout_nb_channels(s->out_ch_layout); i++){
00276 for(j=0; j<av_get_channel_layout_nb_channels(s->in_ch_layout); j++){
00277 av_log(NULL, AV_LOG_DEBUG, "%f ", s->matrix[i][j]);
00278 }
00279 av_log(NULL, AV_LOG_DEBUG, "\n");
00280 }
00281 return 0;
00282 }
00283
00284 int swri_rematrix_init(SwrContext *s){
00285 int i, j;
00286 int nb_in = av_get_channel_layout_nb_channels(s->in_ch_layout);
00287 int nb_out = av_get_channel_layout_nb_channels(s->out_ch_layout);
00288
00289 if (!s->rematrix_custom) {
00290 int r = auto_matrix(s);
00291 if (r)
00292 return r;
00293 }
00294 if (s->midbuf.fmt == AV_SAMPLE_FMT_S16P){
00295 s->native_matrix = av_mallocz(nb_in * nb_out * sizeof(int));
00296 s->native_one = av_mallocz(sizeof(int));
00297 for (i = 0; i < nb_out; i++)
00298 for (j = 0; j < nb_in; j++)
00299 ((int*)s->native_matrix)[i * nb_in + j] = lrintf(s->matrix[i][j] * 32768);
00300 *((int*)s->native_one) = 32768;
00301 s->mix_1_1_f = copy_s16;
00302 s->mix_2_1_f = sum2_s16;
00303 }else if(s->midbuf.fmt == AV_SAMPLE_FMT_FLTP){
00304 s->native_matrix = av_mallocz(nb_in * nb_out * sizeof(float));
00305 s->native_one = av_mallocz(sizeof(float));
00306 for (i = 0; i < nb_out; i++)
00307 for (j = 0; j < nb_in; j++)
00308 ((float*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
00309 *((float*)s->native_one) = 1.0;
00310 s->mix_1_1_f = copy_float;
00311 s->mix_2_1_f = sum2_float;
00312 }else if(s->midbuf.fmt == AV_SAMPLE_FMT_DBLP){
00313 s->native_matrix = av_mallocz(nb_in * nb_out * sizeof(double));
00314 s->native_one = av_mallocz(sizeof(double));
00315 for (i = 0; i < nb_out; i++)
00316 for (j = 0; j < nb_in; j++)
00317 ((double*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
00318 *((double*)s->native_one) = 1.0;
00319 s->mix_1_1_f = copy_double;
00320 s->mix_2_1_f = sum2_double;
00321 }else
00322 av_assert0(0);
00323
00324 for (i = 0; i < SWR_CH_MAX; i++) {
00325 int ch_in=0;
00326 for (j = 0; j < SWR_CH_MAX; j++) {
00327 s->matrix32[i][j]= lrintf(s->matrix[i][j] * 32768);
00328 if(s->matrix[i][j])
00329 s->matrix_ch[i][++ch_in]= j;
00330 }
00331 s->matrix_ch[i][0]= ch_in;
00332 }
00333 return 0;
00334 }
00335
00336 void swri_rematrix_free(SwrContext *s){
00337 av_freep(&s->native_matrix);
00338 av_freep(&s->native_one);
00339 }
00340
00341 int swri_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy){
00342 int out_i, in_i, i, j;
00343
00344 av_assert0(out->ch_count == av_get_channel_layout_nb_channels(s->out_ch_layout));
00345 av_assert0(in ->ch_count == av_get_channel_layout_nb_channels(s-> in_ch_layout));
00346
00347 for(out_i=0; out_i<out->ch_count; out_i++){
00348 switch(s->matrix_ch[out_i][0]){
00349 case 0:
00350 memset(out->ch[out_i], 0, len * av_get_bytes_per_sample(s->int_sample_fmt));
00351 break;
00352 case 1:
00353 in_i= s->matrix_ch[out_i][1];
00354 if(s->matrix[out_i][in_i]!=1.0){
00355 s->mix_1_1_f(out->ch[out_i], in->ch[in_i], s->native_matrix, in->ch_count*out_i + in_i, len);
00356 }else if(mustcopy){
00357 memcpy(out->ch[out_i], in->ch[in_i], len*out->bps);
00358 }else{
00359 out->ch[out_i]= in->ch[in_i];
00360 }
00361 break;
00362 case 2: {
00363 int in_i1 = s->matrix_ch[out_i][1];
00364 int in_i2 = s->matrix_ch[out_i][2];
00365 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);
00366 break;}
00367 default:
00368 if(s->int_sample_fmt == AV_SAMPLE_FMT_FLTP){
00369 for(i=0; i<len; i++){
00370 float v=0;
00371 for(j=0; j<s->matrix_ch[out_i][0]; j++){
00372 in_i= s->matrix_ch[out_i][1+j];
00373 v+= ((float*)in->ch[in_i])[i] * s->matrix[out_i][in_i];
00374 }
00375 ((float*)out->ch[out_i])[i]= v;
00376 }
00377 }else if(s->int_sample_fmt == AV_SAMPLE_FMT_DBLP){
00378 for(i=0; i<len; i++){
00379 double v=0;
00380 for(j=0; j<s->matrix_ch[out_i][0]; j++){
00381 in_i= s->matrix_ch[out_i][1+j];
00382 v+= ((double*)in->ch[in_i])[i] * s->matrix[out_i][in_i];
00383 }
00384 ((double*)out->ch[out_i])[i]= v;
00385 }
00386 }else{
00387 for(i=0; i<len; i++){
00388 int v=0;
00389 for(j=0; j<s->matrix_ch[out_i][0]; j++){
00390 in_i= s->matrix_ch[out_i][1+j];
00391 v+= ((int16_t*)in->ch[in_i])[i] * s->matrix32[out_i][in_i];
00392 }
00393 ((int16_t*)out->ch[out_i])[i]= (v + 16384)>>15;
00394 }
00395 }
00396 }
00397 }
00398 return 0;
00399 }