Skip to content

Commit b9c99ba

Browse files
committed
libswresample/rematrix: add support for custom order channel layouts
Limited to the same channels as a native layout, but not constrained by channel ordering. Signed-off-by: James Almer <[email protected]>
1 parent e52701d commit b9c99ba

File tree

2 files changed

+19
-12
lines changed

2 files changed

+19
-12
lines changed

Changelog

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@ version 7.1:
4949
error, as such a specifier makes no sense
5050
- Mastering Display and Content Light Level metadata support in hevc_nvenc
5151
and av1_nvenc encoders
52+
- libswresample now accepts custom order channel layouts as input, with some
53+
constrains
5254

5355

5456
version 7.0:

libswresample/rematrix.c

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,14 @@ static int clean_layout(AVChannelLayout *out, const AVChannelLayout *in, void *s
104104
}
105105

106106
static int sane_layout(AVChannelLayout *ch_layout) {
107-
if (ch_layout->order != AV_CHANNEL_ORDER_NATIVE)
107+
if(ch_layout->nb_channels >= SWR_CH_MAX)
108+
return 0;
109+
if(ch_layout->order == AV_CHANNEL_ORDER_CUSTOM)
110+
for (int i = 0; i < ch_layout->nb_channels; i++) {
111+
if (ch_layout->u.map[i].id >= 64)
112+
return 0;
113+
}
114+
else if (ch_layout->order != AV_CHANNEL_ORDER_NATIVE)
108115
return 0;
109116
if(!av_channel_layout_subset(ch_layout, AV_CH_LAYOUT_SURROUND)) // at least 1 front speaker
110117
return 0;
@@ -118,8 +125,6 @@ static int sane_layout(AVChannelLayout *ch_layout) {
118125
return 0;
119126
if(!even(av_channel_layout_subset(ch_layout, (AV_CH_TOP_FRONT_LEFT | AV_CH_TOP_FRONT_RIGHT))))
120127
return 0;
121-
if(ch_layout->nb_channels >= SWR_CH_MAX)
122-
return 0;
123128

124129
return 1;
125130
}
@@ -130,9 +135,10 @@ static void build_matrix(const AVChannelLayout *in_ch_layout, const AVChannelLay
130135
ptrdiff_t stride, enum AVMatrixEncoding matrix_encoding)
131136
{
132137
double matrix[NUM_NAMED_CHANNELS][NUM_NAMED_CHANNELS]={{0}};
133-
uint64_t unaccounted = in_ch_layout->u.mask & ~out_ch_layout->u.mask;
138+
uint64_t unaccounted = av_channel_layout_subset(in_ch_layout, UINT64_MAX) &
139+
~av_channel_layout_subset(out_ch_layout, UINT64_MAX);
134140
double maxcoef=0;
135-
int i, j, out_i;
141+
int i, j;
136142

137143
for(i=0; i<FF_ARRAY_ELEMS(matrix); i++){
138144
if( av_channel_layout_index_from_channel(in_ch_layout, i) >= 0
@@ -305,25 +311,24 @@ static void build_matrix(const AVChannelLayout *in_ch_layout, const AVChannelLay
305311
}
306312

307313

308-
for(out_i=i=0; i<64; i++){
314+
for (i = 0; i < 64; i++) {
309315
double sum=0;
310-
int in_i=0;
311-
if (av_channel_layout_index_from_channel(out_ch_layout, i) < 0)
316+
int out_i = av_channel_layout_index_from_channel(out_ch_layout, i);
317+
if (out_i < 0)
312318
continue;
313319
for(j=0; j<64; j++){
314-
if (av_channel_layout_index_from_channel(in_ch_layout, j) < 0)
315-
continue;
320+
int in_i = av_channel_layout_index_from_channel(in_ch_layout, j);
321+
if (in_i < 0)
322+
continue;
316323
if (i < FF_ARRAY_ELEMS(matrix) && j < FF_ARRAY_ELEMS(matrix[0]))
317324
matrix_param[stride*out_i + in_i] = matrix[i][j];
318325
else
319326
matrix_param[stride*out_i + in_i] = i == j &&
320327
( av_channel_layout_index_from_channel(in_ch_layout, i) >= 0
321328
&& av_channel_layout_index_from_channel(out_ch_layout, i) >= 0);
322329
sum += fabs(matrix_param[stride*out_i + in_i]);
323-
in_i++;
324330
}
325331
maxcoef= FFMAX(maxcoef, sum);
326-
out_i++;
327332
}
328333
if(rematrix_volume < 0)
329334
maxcoef = -rematrix_volume;

0 commit comments

Comments
 (0)