libswresample/rematrix.c
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2011 Michael Niedermayer (michaelni@gmx.at)
00003  *
00004  * This file is part of libswresample
00005  *
00006  * libswresample is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Lesser General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2.1 of the License, or (at your option) any later version.
00010  *
00011  * libswresample is distributed in the hope that it will be useful,
00012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014  * Lesser General Public License for more details.
00015  *
00016  * You should have received a copy of the GNU Lesser General Public
00017  * License along with libswresample; if not, write to the Free Software
00018  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
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 (-32768)
00038 #define R(x) (((x) + 16384)>>15)
00039 #define SAMPLE int16_t
00040 #define COEFF int
00041 #define RENAME(x) x ## _s16
00042 #include "rematrix_template.c"
00043 
00044 
00045 #define FRONT_LEFT             0
00046 #define FRONT_RIGHT            1
00047 #define FRONT_CENTER           2
00048 #define LOW_FREQUENCY          3
00049 #define BACK_LEFT              4
00050 #define BACK_RIGHT             5
00051 #define FRONT_LEFT_OF_CENTER   6
00052 #define FRONT_RIGHT_OF_CENTER  7
00053 #define BACK_CENTER            8
00054 #define SIDE_LEFT              9
00055 #define SIDE_RIGHT             10
00056 #define TOP_CENTER             11
00057 #define TOP_FRONT_LEFT         12
00058 #define TOP_FRONT_CENTER       13
00059 #define TOP_FRONT_RIGHT        14
00060 #define TOP_BACK_LEFT          15
00061 #define TOP_BACK_CENTER        16
00062 #define TOP_BACK_RIGHT         17
00063 
00064 static int even(int64_t layout){
00065     if(!layout) return 1;
00066     if(layout&(layout-1)) return 1;
00067     return 0;
00068 }
00069 
00070 static int sane_layout(int64_t layout){
00071     if(!(layout & AV_CH_LAYOUT_SURROUND)) // at least 1 front speaker
00072         return 0;
00073     if(!even(layout & (AV_CH_FRONT_LEFT | AV_CH_FRONT_RIGHT))) // no asymetric front
00074         return 0;
00075     if(!even(layout & (AV_CH_SIDE_LEFT | AV_CH_SIDE_RIGHT)))   // no asymetric side
00076         return 0;
00077     if(!even(layout & (AV_CH_BACK_LEFT | AV_CH_BACK_RIGHT)))
00078         return 0;
00079     if(!even(layout & (AV_CH_FRONT_LEFT_OF_CENTER | AV_CH_FRONT_RIGHT_OF_CENTER)))
00080         return 0;
00081     if(av_get_channel_layout_nb_channels(layout) >= SWR_CH_MAX)
00082         return 0;
00083 
00084     return 1;
00085 }
00086 
00087 int swri_rematrix_init(SwrContext *s){
00088     int i, j, out_i;
00089     double matrix[64][64]={{0}};
00090     int64_t unaccounted= s->in_ch_layout & ~s->out_ch_layout;
00091     double maxcoef=0;
00092 
00093     for(i=0; i<64; i++){
00094         if(s->in_ch_layout & s->out_ch_layout & (1LL<<i))
00095             matrix[i][i]= 1.0;
00096     }
00097 
00098     if(!sane_layout(s->in_ch_layout)){
00099         av_log(s, AV_LOG_ERROR, "Input channel layout isnt supported\n");
00100         return AVERROR(EINVAL);
00101     }
00102     if(!sane_layout(s->out_ch_layout)){
00103         av_log(s, AV_LOG_ERROR, "Output channel layout isnt supported\n");
00104         return AVERROR(EINVAL);
00105     }
00106 
00107 //FIXME implement dolby surround
00108 //FIXME implement full ac3
00109 
00110 
00111     if(unaccounted & AV_CH_FRONT_CENTER){
00112         if((s->out_ch_layout & AV_CH_LAYOUT_STEREO) == AV_CH_LAYOUT_STEREO){
00113             matrix[ FRONT_LEFT][FRONT_CENTER]+= M_SQRT1_2;
00114             matrix[FRONT_RIGHT][FRONT_CENTER]+= M_SQRT1_2;
00115         }else
00116             av_assert0(0);
00117     }
00118     if(unaccounted & AV_CH_LAYOUT_STEREO){
00119         if(s->out_ch_layout & AV_CH_FRONT_CENTER){
00120             matrix[FRONT_CENTER][ FRONT_LEFT]+= M_SQRT1_2;
00121             matrix[FRONT_CENTER][FRONT_RIGHT]+= M_SQRT1_2;
00122             if(s->in_ch_layout & AV_CH_FRONT_CENTER)
00123                 matrix[FRONT_CENTER][ FRONT_CENTER] = s->clev*sqrt(2);
00124         }else
00125             av_assert0(0);
00126     }
00127 
00128     if(unaccounted & AV_CH_BACK_CENTER){
00129         if(s->out_ch_layout & AV_CH_BACK_LEFT){
00130             matrix[ BACK_LEFT][BACK_CENTER]+= M_SQRT1_2;
00131             matrix[BACK_RIGHT][BACK_CENTER]+= M_SQRT1_2;
00132         }else if(s->out_ch_layout & AV_CH_SIDE_LEFT){
00133             matrix[ SIDE_LEFT][BACK_CENTER]+= M_SQRT1_2;
00134             matrix[SIDE_RIGHT][BACK_CENTER]+= M_SQRT1_2;
00135         }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
00136             matrix[ FRONT_LEFT][BACK_CENTER]+= s->slev*M_SQRT1_2;
00137             matrix[FRONT_RIGHT][BACK_CENTER]+= s->slev*M_SQRT1_2;
00138         }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
00139             matrix[ FRONT_CENTER][BACK_CENTER]+= s->slev*M_SQRT1_2;
00140         }else
00141             av_assert0(0);
00142     }
00143     if(unaccounted & AV_CH_BACK_LEFT){
00144         if(s->out_ch_layout & AV_CH_BACK_CENTER){
00145             matrix[BACK_CENTER][ BACK_LEFT]+= M_SQRT1_2;
00146             matrix[BACK_CENTER][BACK_RIGHT]+= M_SQRT1_2;
00147         }else if(s->out_ch_layout & AV_CH_SIDE_LEFT){
00148             if(s->in_ch_layout & AV_CH_SIDE_LEFT){
00149                 matrix[ SIDE_LEFT][ BACK_LEFT]+= M_SQRT1_2;
00150                 matrix[SIDE_RIGHT][BACK_RIGHT]+= M_SQRT1_2;
00151             }else{
00152             matrix[ SIDE_LEFT][ BACK_LEFT]+= 1.0;
00153             matrix[SIDE_RIGHT][BACK_RIGHT]+= 1.0;
00154             }
00155         }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
00156             matrix[ FRONT_LEFT][ BACK_LEFT]+= s->slev;
00157             matrix[FRONT_RIGHT][BACK_RIGHT]+= s->slev;
00158         }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
00159             matrix[ FRONT_CENTER][BACK_LEFT ]+= s->slev*M_SQRT1_2;
00160             matrix[ FRONT_CENTER][BACK_RIGHT]+= s->slev*M_SQRT1_2;
00161         }else
00162             av_assert0(0);
00163     }
00164 
00165     if(unaccounted & AV_CH_SIDE_LEFT){
00166         if(s->out_ch_layout & AV_CH_BACK_LEFT){
00167             matrix[ BACK_LEFT][ SIDE_LEFT]+= 1.0;
00168             matrix[BACK_RIGHT][SIDE_RIGHT]+= 1.0;
00169         }else if(s->out_ch_layout & AV_CH_BACK_CENTER){
00170             matrix[BACK_CENTER][ SIDE_LEFT]+= M_SQRT1_2;
00171             matrix[BACK_CENTER][SIDE_RIGHT]+= M_SQRT1_2;
00172         }else if(s->out_ch_layout & AV_CH_FRONT_LEFT){
00173             matrix[ FRONT_LEFT][ SIDE_LEFT]+= s->slev;
00174             matrix[FRONT_RIGHT][SIDE_RIGHT]+= s->slev;
00175         }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
00176             matrix[ FRONT_CENTER][SIDE_LEFT ]+= s->slev*M_SQRT1_2;
00177             matrix[ FRONT_CENTER][SIDE_RIGHT]+= s->slev*M_SQRT1_2;
00178         }else
00179             av_assert0(0);
00180     }
00181 
00182     if(unaccounted & AV_CH_FRONT_LEFT_OF_CENTER){
00183         if(s->out_ch_layout & AV_CH_FRONT_LEFT){
00184             matrix[ FRONT_LEFT][ FRONT_LEFT_OF_CENTER]+= 1.0;
00185             matrix[FRONT_RIGHT][FRONT_RIGHT_OF_CENTER]+= 1.0;
00186         }else if(s->out_ch_layout & AV_CH_FRONT_CENTER){
00187             matrix[ FRONT_CENTER][ FRONT_LEFT_OF_CENTER]+= M_SQRT1_2;
00188             matrix[ FRONT_CENTER][FRONT_RIGHT_OF_CENTER]+= M_SQRT1_2;
00189         }else
00190             av_assert0(0);
00191     }
00192 
00193     //FIXME quantize for integeres
00194     for(out_i=i=0; i<64; i++){
00195         double sum=0;
00196         int in_i=0;
00197         int ch_in=0;
00198         for(j=0; j<64; j++){
00199             s->matrix[out_i][in_i]= matrix[i][j];
00200             s->matrix32[out_i][in_i]= lrintf(matrix[i][j] * 32768);
00201             if(matrix[i][j]){
00202                 s->matrix_ch[out_i][++ch_in]= in_i;
00203                 sum += fabs(matrix[i][j]);
00204             }
00205             if(s->in_ch_layout & (1ULL<<j))
00206                 in_i++;
00207         }
00208         s->matrix_ch[out_i][0]= ch_in;
00209         maxcoef= FFMAX(maxcoef, sum);
00210         if(s->out_ch_layout & (1ULL<<i))
00211             out_i++;
00212     }
00213     if(s->rematrix_volume  < 0)
00214         maxcoef = -s->rematrix_volume;
00215 
00216     if((   s->out_sample_fmt < AV_SAMPLE_FMT_FLT
00217         || s->int_sample_fmt < AV_SAMPLE_FMT_FLT) && maxcoef > 1.0){
00218         for(i=0; i<SWR_CH_MAX; i++)
00219             for(j=0; j<SWR_CH_MAX; j++){
00220                 s->matrix[i][j] /= maxcoef;
00221                 s->matrix32[i][j]= lrintf(s->matrix[i][j] * 32768);
00222             }
00223     }
00224 
00225     if(s->rematrix_volume > 0){
00226         for(i=0; i<SWR_CH_MAX; i++)
00227             for(j=0; j<SWR_CH_MAX; j++){
00228                 s->matrix[i][j] *= s->rematrix_volume;
00229                 s->matrix32[i][j]= lrintf(s->matrix[i][j] * 32768);
00230             }
00231     }
00232 
00233     for(i=0; i<av_get_channel_layout_nb_channels(s->out_ch_layout); i++){
00234         for(j=0; j<av_get_channel_layout_nb_channels(s->in_ch_layout); j++){
00235             av_log(NULL, AV_LOG_DEBUG, "%f ", s->matrix[i][j]);
00236         }
00237         av_log(NULL, AV_LOG_DEBUG, "\n");
00238     }
00239     return 0;
00240 }
00241 
00242 int swri_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mustcopy){
00243     int out_i, in_i, i, j;
00244 
00245     av_assert0(out->ch_count == av_get_channel_layout_nb_channels(s->out_ch_layout));
00246     av_assert0(in ->ch_count == av_get_channel_layout_nb_channels(s-> in_ch_layout));
00247 
00248     for(out_i=0; out_i<out->ch_count; out_i++){
00249         switch(s->matrix_ch[out_i][0]){
00250         case 1:
00251             in_i= s->matrix_ch[out_i][1];
00252             if(mustcopy || s->matrix[out_i][in_i]!=1.0){
00253                 if(s->int_sample_fmt == AV_SAMPLE_FMT_FLT){
00254                     copy_float((float  *)out->ch[out_i], (const float  *)in->ch[in_i], s->matrix  [out_i][in_i], len);
00255                 }else
00256                     copy_s16  ((int16_t*)out->ch[out_i], (const int16_t*)in->ch[in_i], s->matrix32[out_i][in_i], len);
00257             }else{
00258                 out->ch[out_i]= in->ch[in_i];
00259             }
00260             break;
00261         case 2:
00262             if(s->int_sample_fmt == AV_SAMPLE_FMT_FLT){
00263                 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] ],
00264                                  s->matrix[out_i][ s->matrix_ch[out_i][1] ], s->matrix[out_i][ s->matrix_ch[out_i][2] ],
00265                            len);
00266             }else{
00267                 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] ],
00268                                  s->matrix32[out_i][ s->matrix_ch[out_i][1] ], s->matrix32[out_i][ s->matrix_ch[out_i][2] ],
00269                            len);
00270             }
00271             break;
00272         default:
00273             if(s->int_sample_fmt == AV_SAMPLE_FMT_FLT){
00274                 for(i=0; i<len; i++){
00275                     float v=0;
00276                     for(j=0; j<s->matrix_ch[out_i][0]; j++){
00277                         in_i= s->matrix_ch[out_i][1+j];
00278                         v+= ((float*)in->ch[in_i])[i] * s->matrix[out_i][in_i];
00279                     }
00280                     ((float*)out->ch[out_i])[i]= v;
00281                 }
00282             }else{
00283                 for(i=0; i<len; i++){
00284                     int v=0;
00285                     for(j=0; j<s->matrix_ch[out_i][0]; j++){
00286                         in_i= s->matrix_ch[out_i][1+j];
00287                         v+= ((int16_t*)in->ch[in_i])[i] * s->matrix32[out_i][in_i];
00288                     }
00289                     ((int16_t*)out->ch[out_i])[i]= (v + 16384)>>15;
00290                 }
00291             }
00292         }
00293     }
00294     return 0;
00295 }