Skip to content

Commit caef259

Browse files
committed
Switch to new AVChannelLayout API
1 parent 773300d commit caef259

File tree

3 files changed

+27
-39
lines changed

3 files changed

+27
-39
lines changed

c_src/membrane_ffmpeg_swresample_plugin/converter.c

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
static int membrane_sample_fmt_to_av_sample_fmt(int in, char dir, char *s24le,
66
enum AVSampleFormat *out);
7-
static int nb_channels_to_av_layout(int channels, int64_t *av_layout);
7+
static int nb_channels_to_av_chlayout(int channels, AVChannelLayout *layout);
88

99
UNIFEX_TERM create(UnifexEnv *env, unsigned int src_format, int src_rate,
1010
int src_channels, unsigned int dst_format, int dst_rate,
@@ -25,19 +25,19 @@ UNIFEX_TERM create(UnifexEnv *env, unsigned int src_format, int src_rate,
2525
err_reason = "unsupported_dst_format";
2626
goto create_exit;
2727
}
28-
int64_t src_layout, dst_layout;
29-
if (nb_channels_to_av_layout(src_channels, &src_layout)) {
28+
AVChannelLayout src_layout, dst_layout;
29+
if (nb_channels_to_av_chlayout(src_channels, &src_layout)) {
3030
err_reason = "unsupported_src_channels_no";
3131
goto create_exit;
3232
}
33-
if (nb_channels_to_av_layout(dst_channels, &dst_layout)) {
33+
if (nb_channels_to_av_chlayout(dst_channels, &dst_layout)) {
3434
err_reason = "unsupported_dst_channels_no";
3535
goto create_exit;
3636
}
3737

3838
state = unifex_alloc_state(env);
39-
err_reason = lib_init(state, from_s24le, src_av_format, src_rate, src_layout,
40-
dst_av_format, dst_rate, dst_layout);
39+
err_reason = lib_init(state, from_s24le, src_av_format, src_rate, &src_layout,
40+
dst_av_format, dst_rate, &dst_layout);
4141
if (err_reason) {
4242
goto create_exit;
4343
}
@@ -121,17 +121,11 @@ static int membrane_sample_fmt_to_av_sample_fmt(int in, char dir, char *s24le,
121121
return error;
122122
}
123123

124-
static int nb_channels_to_av_layout(int channels, int64_t *av_layout) {
125-
int error = 0;
126-
switch (channels) {
127-
case 1:
128-
*av_layout = AV_CH_LAYOUT_MONO;
129-
break;
130-
case 2:
131-
*av_layout = AV_CH_LAYOUT_STEREO;
132-
break;
133-
default:
134-
error = -1;
135-
}
136-
return error;
124+
static int nb_channels_to_av_chlayout(int channels, AVChannelLayout *layout) {
125+
if (channels <= 0)
126+
return -1;
127+
av_channel_layout_default(layout, channels);
128+
if (layout->nb_channels != channels)
129+
return -1;
130+
return 0;
137131
}

c_src/membrane_ffmpeg_swresample_plugin/converter_lib.c

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,42 +3,35 @@
33

44
char *lib_init(ConverterState *state, char from_s24le,
55
enum AVSampleFormat src_sample_fmt, int src_rate,
6-
int64_t src_ch_layout, enum AVSampleFormat dst_sample_fmt,
7-
int dst_rate, int64_t dst_ch_layout)
6+
const AVChannelLayout *src_ch_layout,
7+
enum AVSampleFormat dst_sample_fmt, int dst_rate,
8+
const AVChannelLayout *dst_ch_layout)
89
{
910
state->swr_ctx = NULL;
1011

11-
struct SwrContext *swr_ctx = swr_alloc();
12-
if (!swr_ctx)
12+
struct SwrContext *swr_ctx = NULL;
13+
if (swr_alloc_set_opts2(&swr_ctx,
14+
dst_ch_layout, dst_sample_fmt, dst_rate,
15+
src_ch_layout, src_sample_fmt, src_rate,
16+
0, NULL) < 0)
1317
{
14-
return "swr_alloc";
18+
return "swr_alloc_set_opts2";
1519
}
1620

1721
/* set options */
18-
av_opt_set_int(swr_ctx, "in_channel_layout", src_ch_layout, 0);
19-
av_opt_set_int(swr_ctx, "in_sample_rate", src_rate, 0);
20-
av_opt_set_sample_fmt(swr_ctx, "in_sample_fmt", src_sample_fmt, 0);
21-
av_opt_set_int(swr_ctx, "out_channel_layout", dst_ch_layout, 0);
22-
av_opt_set_int(swr_ctx, "out_sample_rate", dst_rate, 0);
23-
av_opt_set_sample_fmt(swr_ctx, "out_sample_fmt", dst_sample_fmt, 0);
2422
av_opt_set_int(swr_ctx, "dither_method", SWR_DITHER_RECTANGULAR, 0);
2523

2624
if (swr_init(swr_ctx) < 0)
2725
return "swr_init";
2826

29-
AVChannelLayout src_ch_av_layout;
30-
AVChannelLayout dst_ch_av_layout;
31-
av_channel_layout_from_mask(&src_ch_av_layout, src_ch_layout);
32-
av_channel_layout_from_mask(&dst_ch_av_layout, dst_ch_layout);
33-
3427
*state = (ConverterState){
3528
.swr_ctx = swr_ctx,
3629
.src_rate = src_rate,
3730
.dst_rate = dst_rate,
3831
.src_sample_fmt = src_sample_fmt,
3932
.dst_sample_fmt = dst_sample_fmt,
40-
.src_nb_channels = src_ch_av_layout.nb_channels,
41-
.dst_nb_channels = dst_ch_av_layout.nb_channels,
33+
.src_nb_channels = src_ch_layout->nb_channels,
34+
.dst_nb_channels = dst_ch_layout->nb_channels,
4235
.from_s24le = from_s24le};
4336

4437
return NULL;

c_src/membrane_ffmpeg_swresample_plugin/converter_lib.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@ typedef struct ConverterState
1414

1515
char *lib_init(ConverterState *state, char from_s24le,
1616
enum AVSampleFormat src_sample_fmt, int src_rate,
17-
int64_t src_ch_layout, enum AVSampleFormat dst_sample_fmt,
18-
int dst_rate, int64_t dst_ch_layout);
17+
const AVChannelLayout *src_ch_layout,
18+
enum AVSampleFormat dst_sample_fmt, int dst_rate,
19+
const AVChannelLayout *dst_ch_layout);
1920

2021
char *lib_convert(ConverterState *state, uint8_t *input, int input_size,
2122
uint8_t **output, int *output_size);

0 commit comments

Comments
 (0)