Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / third_party / ffmpeg / libswresample / rematrix.c
index 5c98e68..6552a2f 100644 (file)
@@ -56,6 +56,7 @@
 #define TOP_BACK_LEFT          15
 #define TOP_BACK_CENTER        16
 #define TOP_BACK_RIGHT         17
+#define NUM_NAMED_CHANNELS     18
 
 int swr_set_matrix(struct SwrContext *s, const double *matrix, int stride)
 {
@@ -112,7 +113,7 @@ static int sane_layout(int64_t layout){
 av_cold static int auto_matrix(SwrContext *s)
 {
     int i, j, out_i;
-    double matrix[64][64]={{0}};
+    double matrix[NUM_NAMED_CHANNELS][NUM_NAMED_CHANNELS]={{0}};
     int64_t unaccounted, in_ch_layout, out_ch_layout;
     double maxcoef=0;
     char buf[128];
@@ -127,6 +128,11 @@ av_cold static int auto_matrix(SwrContext *s)
     )
         out_ch_layout = AV_CH_LAYOUT_STEREO;
 
+    if(    in_ch_layout == AV_CH_LAYOUT_STEREO_DOWNMIX
+       && (out_ch_layout & AV_CH_LAYOUT_STEREO_DOWNMIX) == 0
+    )
+        in_ch_layout = AV_CH_LAYOUT_STEREO;
+
     if(!sane_layout(in_ch_layout)){
         av_get_channel_layout_string(buf, sizeof(buf), -1, s->in_ch_layout);
         av_log(s, AV_LOG_ERROR, "Input channel layout '%s' is not supported\n", buf);
@@ -140,7 +146,7 @@ av_cold static int auto_matrix(SwrContext *s)
     }
 
     memset(s->matrix, 0, sizeof(s->matrix));
-    for(i=0; i<64; i++){
+    for(i=0; i<FF_ARRAY_ELEMS(matrix); i++){
         if(in_ch_layout & out_ch_layout & (1ULL<<i))
             matrix[i][i]= 1.0;
     }
@@ -293,17 +299,20 @@ av_cold static int auto_matrix(SwrContext *s)
     for(out_i=i=0; i<64; i++){
         double sum=0;
         int in_i=0;
+        if((out_ch_layout & (1ULL<<i)) == 0)
+            continue;
         for(j=0; j<64; j++){
-            s->matrix[out_i][in_i]= matrix[i][j];
-            if(matrix[i][j]){
-                sum += fabs(matrix[i][j]);
-            }
-            if(in_ch_layout & (1ULL<<j))
-                in_i++;
+            if((in_ch_layout & (1ULL<<j)) == 0)
+               continue;
+            if (i < FF_ARRAY_ELEMS(matrix) && j < FF_ARRAY_ELEMS(matrix[0]))
+                s->matrix[out_i][in_i]= matrix[i][j];
+            else
+                s->matrix[out_i][in_i]= i == j && (in_ch_layout & out_ch_layout & (1ULL<<i));
+            sum += fabs(s->matrix[out_i][in_i]);
+            in_i++;
         }
         maxcoef= FFMAX(maxcoef, sum);
-        if(out_ch_layout & (1ULL<<i))
-            out_i++;
+        out_i++;
     }
     if(s->rematrix_volume  < 0)
         maxcoef = -s->rematrix_volume;
@@ -433,8 +442,8 @@ int swri_rematrix(SwrContext *s, AudioData *out, AudioData *in, int len, int mus
         off = len1 * out->bps;
     }
 
-    av_assert0(out->ch_count == av_get_channel_layout_nb_channels(s->out_ch_layout));
-    av_assert0(in ->ch_count == av_get_channel_layout_nb_channels(s-> in_ch_layout));
+    av_assert0(!s->out_ch_layout || out->ch_count == av_get_channel_layout_nb_channels(s->out_ch_layout));
+    av_assert0(!s-> in_ch_layout || in ->ch_count == av_get_channel_layout_nb_channels(s-> in_ch_layout));
 
     for(out_i=0; out_i<out->ch_count; out_i++){
         switch(s->matrix_ch[out_i][0]){