@@ -13,13 +13,14 @@ void free_frames(AVFrame **frames, int size) {
1313}
1414
1515ERL_NIF_TERM new (ErlNifEnv * env , int argc , const ERL_NIF_TERM argv []) {
16- if (argc != 6 ) {
16+ if (argc != 7 ) {
1717 return xav_nif_raise (env , "invalid_arg_count" );
1818 }
1919
2020 ERL_NIF_TERM ret ;
2121 char * codec_name = NULL ;
2222 char * out_format = NULL ;
23+ int channels ;
2324
2425 // resolve codec
2526 if (!xav_nif_get_atom (env , argv [0 ], & codec_name )) {
@@ -37,8 +38,13 @@ ERL_NIF_TERM new (ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) {
3738 goto clean ;
3839 }
3940
41+ if (!enif_get_int (env , argv [1 ], & channels )) {
42+ ret = xav_nif_raise (env , "failed_to_get_int" );
43+ goto clean ;
44+ }
45+
4046 // resolve output format
41- if (!xav_nif_get_atom (env , argv [1 ], & out_format )) {
47+ if (!xav_nif_get_atom (env , argv [2 ], & out_format )) {
4248 ret = xav_nif_raise (env , "failed_to_get_atom" );
4349 goto clean ;
4450 }
@@ -61,25 +67,25 @@ ERL_NIF_TERM new (ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) {
6167
6268 // resolve other params
6369 int out_sample_rate ;
64- if (!enif_get_int (env , argv [2 ], & out_sample_rate )) {
70+ if (!enif_get_int (env , argv [3 ], & out_sample_rate )) {
6571 ret = xav_nif_raise (env , "invalid_out_sample_rate" );
6672 goto clean ;
6773 }
6874
6975 int out_channels ;
70- if (!enif_get_int (env , argv [3 ], & out_channels )) {
76+ if (!enif_get_int (env , argv [4 ], & out_channels )) {
7177 ret = xav_nif_raise (env , "invalid_out_channels" );
7278 goto clean ;
7379 }
7480
7581 int out_width ;
76- if (!enif_get_int (env , argv [4 ], & out_width )) {
82+ if (!enif_get_int (env , argv [5 ], & out_width )) {
7783 ret = xav_nif_raise (env , "failed_to_get_int" );
7884 goto clean ;
7985 }
8086
8187 int out_height ;
82- if (!enif_get_int (env , argv [5 ], & out_height )) {
88+ if (!enif_get_int (env , argv [6 ], & out_height )) {
8389 ret = xav_nif_raise (env , "failed_to_get_int" );
8490 goto clean ;
8591 }
@@ -102,7 +108,7 @@ ERL_NIF_TERM new (ErlNifEnv *env, int argc, const ERL_NIF_TERM argv[]) {
102108 goto clean ;
103109 }
104110
105- if (decoder_init (xav_decoder -> decoder , codec ) != 0 ) {
111+ if (decoder_init (xav_decoder -> decoder , codec , channels ) ) {
106112 ret = xav_nif_raise (env , "failed_to_init_decoder" );
107113 goto clean ;
108114 }
@@ -341,21 +347,13 @@ static int init_audio_converter(struct XavDecoder *xav_decoder) {
341347 }
342348
343349 struct ChannelLayout in_chlayout , out_chlayout ;
344- #if LIBAVUTIL_VERSION_MAJOR >= 58
345- in_chlayout .layout = xav_decoder -> decoder -> c -> ch_layout ;
346- if (xav_decoder -> out_channels == 0 ) {
347- out_chlayout .layout = in_chlayout .layout ;
348- } else {
349- av_channel_layout_default (& out_chlayout .layout , xav_decoder -> out_channels );
350- }
351- #else
352- in_chlayout .layout = xav_decoder -> decoder -> c -> channel_layout ;
350+ xav_get_channel_layout_from_context (& in_chlayout , xav_decoder -> decoder -> c );
351+
353352 if (xav_decoder -> out_channels == 0 ) {
354- out_chlayout . layout = in_chlayout . layout ;
353+ xav_get_channel_layout_from_context ( & out_chlayout , xav_decoder -> decoder -> c ) ;
355354 } else {
356- out_chlayout . layout = av_get_default_channel_layout ( xav_decoder -> out_channels );
355+ xav_set_default_channel_layout ( & out_chlayout , xav_decoder -> out_channels );
357356 }
358- #endif
359357
360358 return audio_converter_init (xav_decoder -> ac , in_chlayout , xav_decoder -> decoder -> c -> sample_rate ,
361359 xav_decoder -> decoder -> c -> sample_fmt , out_chlayout , out_sample_rate ,
@@ -393,7 +391,7 @@ void free_xav_decoder(ErlNifEnv *env, void *obj) {
393391 }
394392}
395393
396- static ErlNifFunc xav_funcs [] = {{"new" , 6 , new },
394+ static ErlNifFunc xav_funcs [] = {{"new" , 7 , new },
397395 {"decode" , 4 , decode , ERL_NIF_DIRTY_JOB_CPU_BOUND },
398396 {"flush" , 1 , flush , ERL_NIF_DIRTY_JOB_CPU_BOUND },
399397 {"pixel_formats" , 0 , pixel_formats },
0 commit comments