1 /*
2 * Copyright (C) 2011-2012 Michael Niedermayer (michaelni@gmx.at)
3 *
4 * This file is part of libswresample
5 *
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.
10 *
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.
15 *
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
19 */
20
24
25 #define TEMPLATE_REMATRIX_FLT
27 #undef TEMPLATE_REMATRIX_FLT
28
29 #define TEMPLATE_REMATRIX_DBL
31 #undef TEMPLATE_REMATRIX_DBL
32
33 #define TEMPLATE_REMATRIX_S16
35 #undef TEMPLATE_REMATRIX_S16
36
37 #define TEMPLATE_REMATRIX_S32
39 #undef TEMPLATE_REMATRIX_S32
40
43 #define FRONT_CENTER 2
44 #define LOW_FREQUENCY 3
47 #define FRONT_LEFT_OF_CENTER 6
48 #define FRONT_RIGHT_OF_CENTER 7
53 #define TOP_FRONT_LEFT 12
54 #define TOP_FRONT_CENTER 13
55 #define TOP_FRONT_RIGHT 14
56 #define TOP_BACK_LEFT 15
57 #define TOP_BACK_CENTER 16
58 #define TOP_BACK_RIGHT 17
59
61 {
62 int nb_in, nb_out,
in,
out;
63
64 if (!s || s->
in_convert)
// s needs to be allocated but not initialized
69 for (out = 0; out < nb_out; out++) {
70 for (in = 0; in < nb_in; in++)
71 s->
matrix[out][in] = matrix[in];
72 matrix += stride;
73 }
75 return 0;
76 }
77
79 if(!layout) return 1;
80 if(layout&(layout-1)) return 1;
81 return 0;
82 }
83
90 }
91
93 }
94
97 return 0;
99 return 0;
101 return 0;
103 return 0;
105 return 0;
107 return 0;
108
109 return 1;
110 }
111
113 {
114 int i, j, out_i;
115 double matrix[64][64]={{0}};
116 int64_t unaccounted, in_ch_layout, out_ch_layout;
117 double maxcoef=0;
120 float maxval;
121
124
127 )
129
134 }
135
140 }
141
143 for(i=0; i<64; i++){
144 if(in_ch_layout & out_ch_layout & (1ULL<<i))
145 matrix[i][i]= 1.0;
146 }
147
148 unaccounted= in_ch_layout & ~out_ch_layout;
149
150 //FIXME implement dolby surround
151 //FIXME implement full ac3
152
153
156 if(in_ch_layout & AV_CH_LAYOUT_STEREO) {
159 } else {
162 }
163 }else
165 }
167 if(out_ch_layout & AV_CH_FRONT_CENTER){
170 if(in_ch_layout & AV_CH_FRONT_CENTER)
172 }else
174 }
175
186 if (unaccounted & (AV_CH_BACK_LEFT | AV_CH_SIDE_LEFT)) {
189 } else {
192 }
193 } else {
196 }
197 }else if(out_ch_layout & AV_CH_FRONT_CENTER){
199 }else
201 }
203 if(out_ch_layout & AV_CH_BACK_CENTER){
207 if(in_ch_layout & AV_CH_SIDE_LEFT){
210 }else{
213 }
225 } else {
228 }
229 }else if(out_ch_layout & AV_CH_FRONT_CENTER){
232 }else
234 }
235
237 if(out_ch_layout & AV_CH_BACK_LEFT){
238 /* if back channels do not exist in the input, just copy side
239 channels to back channels, otherwise mix side into back */
240 if (in_ch_layout & AV_CH_BACK_LEFT) {
243 } else {
246 }
247 }else if(out_ch_layout & AV_CH_BACK_CENTER){
261 } else {
264 }
265 }else if(out_ch_layout & AV_CH_FRONT_CENTER){
268 }else
270 }
271
276 }else if(out_ch_layout & AV_CH_FRONT_CENTER){
279 }else
281 }
282 /* mix LFE into front left/right or center */
284 if (out_ch_layout & AV_CH_FRONT_CENTER) {
289 } else
291 }
292
293 for(out_i=i=0; i<64; i++){
294 double sum=0;
295 int in_i=0;
296 for(j=0; j<64; j++){
297 s->
matrix[out_i][in_i]= matrix[i][j];
298 if(matrix[i][j]){
299 sum += fabs(matrix[i][j]);
300 }
301 if(in_ch_layout & (1ULL<<j))
302 in_i++;
303 }
304 maxcoef=
FFMAX(maxcoef, sum);
305 if(out_ch_layout & (1ULL<<i))
306 out_i++;
307 }
310
315 maxval = 1.0;
316 } else
317 maxval = INT_MAX;
318
320 maxcoef /= maxval;
323 s->
matrix[i][j] /= maxcoef;
324 }
325 }
326
331 }
332 }
333
337 }
339 }
340 return 0;
341 }
342
344 int i, j;
347
349
352 if (r)
354 }
358 for (i = 0; i < nb_out; i++)
359 for (j = 0; j < nb_in; j++)
368 for (i = 0; i < nb_out; i++)
369 for (j = 0; j < nb_in; j++)
378 for (i = 0; i < nb_out; i++)
379 for (j = 0; j < nb_in; j++)
386 // Only for dithering currently
387 // s->native_matrix = av_calloc(nb_in * nb_out, sizeof(double));
389 // for (i = 0; i < nb_out; i++)
390 // for (j = 0; j < nb_in; j++)
391 // ((double*)s->native_matrix)[i * nb_in + j] = s->matrix[i][j];
396 }else
398 //FIXME quantize for integeres
400 int ch_in=0;
405 }
407 }
408
410
411 return 0;
412 }
413
419 }
420
422 int out_i, in_i, i, j;
423 int len1 = 0;
425
428 return 0;
429 }
430
432 len1= len&~15;
433 off = len1 * out->
bps;
434 }
435
438
439 for(out_i=0; out_i<out->
ch_count; out_i++){
441 case 0:
442 if(mustcopy)
444 break;
445 case 1:
447 if(s->
matrix[out_i][in_i]!=1.0){
450 if(len != len1)
452 }else if(mustcopy){
453 memcpy(out->
ch[out_i], in->
ch[in_i], len*out->
bps);
454 }else{
455 out->
ch[out_i]= in->
ch[in_i];
456 }
457 break;
458 case 2: {
463 else
465 if(len != len1)
467 break;}
468 default:
470 for(i=0; i<
len; i++){
474 v+= ((
float*)in->
ch[in_i])[i] * s->
matrix[out_i][in_i];
475 }
476 ((
float*)out->
ch[out_i])[i]=
v;
477 }
479 for(i=0; i<
len; i++){
483 v+= ((
double*)in->
ch[in_i])[i] * s->
matrix[out_i][in_i];
484 }
485 ((
double*)out->
ch[out_i])[i]=
v;
486 }
487 }else{
488 for(i=0; i<
len; i++){
492 v+= ((int16_t*)in->
ch[in_i])[i] * s->
matrix32[out_i][in_i];
493 }
494 ((int16_t*)out->
ch[out_i])[i]= (v + 16384)>>15;
495 }
496 }
497 }
498 }
499 return 0;
500 }