@@ -2,13 +2,13 @@ use crate::error::Error;
22
33use ffmpeg_sys_next:: {
44 self , av_frame_alloc, av_frame_free, av_frame_unref, av_freep, av_get_alt_sample_fmt,
5- av_get_bytes_per_sample, av_get_channel_layout_nb_channels , av_get_sample_fmt_name,
5+ av_get_bytes_per_sample, av_get_sample_fmt_name,
66 av_init_packet, av_packet_unref, av_read_frame, av_sample_fmt_is_planar,
77 av_samples_alloc, av_samples_get_buffer_size, avcodec_alloc_context3, avcodec_close,
88 avcodec_find_decoder, avcodec_free_context, avcodec_open2, avcodec_parameters_to_context,
99 avcodec_receive_frame, avcodec_send_packet, avformat_close_input, avformat_find_stream_info,
10- avformat_open_input, swr_alloc_set_opts , swr_convert, swr_get_out_samples, swr_init, AVCodec ,
11- AVCodecContext , AVFormatContext , AVFrame , AVMediaType , AVPacket , AVSampleFormat , AVStream ,
10+ avformat_open_input, swr_alloc_set_opts2 , swr_convert, swr_get_out_samples, swr_init, AVCodec ,
11+ AVCodecContext , AVFormatContext , AVFrame , AVMediaType , AVPacket , AVSampleFormat , AVStream , AVChannelLayout
1212} ;
1313use std:: ffi:: { CStr , CString } ;
1414use std:: path:: Path ;
@@ -117,7 +117,7 @@ impl Decoder {
117117 fn convert_and_store_frame ( & mut self ) {
118118 let num_samples = self . frame . num_samples ( ) ;
119119 let channel_layout = self . frame . channel_layout ( ) ;
120- let num_channels = unsafe { av_get_channel_layout_nb_channels ( channel_layout) } ;
120+ let num_channels = channel_layout. nb_channels ;
121121
122122 let extended_data = self . frame . extended_data ( ) ;
123123
@@ -190,10 +190,10 @@ impl Decoder {
190190 unsafe { av_packet_unref ( self . packet . inner . as_mut_ptr ( ) ) } ;
191191 }
192192
193- fn next_sample ( & mut self ) -> i16 {
193+ fn next_sample ( & mut self ) -> f32 {
194194 let sample_u8: [ u8 ; 2 ] = [ self . current_frame . remove ( 0 ) , self . current_frame . remove ( 0 ) ] ;
195195
196- ( ( sample_u8[ 1 ] as i16 ) << 8 ) | sample_u8[ 0 ] as i16
196+ ( ( ( sample_u8[ 1 ] as i16 ) << 8 ) | sample_u8[ 0 ] as i16 ) as _
197197 }
198198
199199 fn process_next_frame ( & mut self ) -> Option < Result < ( ) , Error > > {
@@ -277,7 +277,7 @@ impl Decoder {
277277unsafe impl Send for Decoder { }
278278
279279impl Iterator for Decoder {
280- type Item = i16 ;
280+ type Item = f32 ;
281281
282282 #[ inline]
283283 fn next ( & mut self ) -> Option < Self :: Item > {
@@ -371,26 +371,29 @@ struct SwrContext {
371371
372372impl SwrContext {
373373 fn new ( codec_ctx : & CodecContext ) -> Result < SwrContext , Error > {
374- let swr_ctx: * mut ffmpeg_sys_next:: SwrContext = unsafe {
375- swr_alloc_set_opts (
376- ptr:: null_mut ( ) ,
377- codec_ctx. channel_layout ( ) as i64 ,
374+ unsafe {
375+ let mut swr_ctx: * mut ffmpeg_sys_next:: SwrContext = std:: ptr:: null_mut ( ) ;
376+
377+ let channel_layout = codec_ctx. channel_layout ( ) ;
378+
379+ let ret = swr_alloc_set_opts2 (
380+ & mut swr_ctx,
381+ & channel_layout as * const AVChannelLayout ,
378382 DEFAULT_CONVERSION_FORMAT ,
379383 codec_ctx. sample_rate ( ) ,
380- codec_ctx . channel_layout ( ) as i64 ,
384+ & channel_layout as * const AVChannelLayout ,
381385 codec_ctx. sample_format ( ) ,
382386 codec_ctx. sample_rate ( ) ,
383387 0 ,
384388 ptr:: null_mut ( ) ,
385- )
386- } ;
389+ ) ;
390+
391+ if ret != 0 || swr_ctx. is_null ( ) {
392+ return Err ( Error :: InitializeSwr ) ;
393+ }
387394
388- let status = unsafe { swr_init ( swr_ctx) } ;
389- if status != 0 {
390- return Err ( Error :: InitializeSwr ) ;
395+ Ok ( SwrContext { inner : swr_ctx } )
391396 }
392-
393- Ok ( SwrContext { inner : swr_ctx } )
394397 }
395398}
396399
@@ -427,8 +430,8 @@ impl Frame {
427430 unsafe { self . inner . as_ref ( ) . unwrap ( ) . nb_samples }
428431 }
429432
430- fn channel_layout ( & self ) -> u64 {
431- unsafe { self . inner . as_ref ( ) . unwrap ( ) . channel_layout }
433+ fn channel_layout ( & self ) -> AVChannelLayout {
434+ unsafe { self . inner . as_ref ( ) . unwrap ( ) . ch_layout }
432435 }
433436
434437 fn extended_data ( & self ) -> * mut * const u8 {
@@ -531,11 +534,11 @@ impl CodecContext {
531534 }
532535
533536 fn channels ( & self ) -> i32 {
534- unsafe { self . inner . as_ref ( ) . unwrap ( ) . channels }
537+ unsafe { self . inner . as_ref ( ) . unwrap ( ) . ch_layout . nb_channels }
535538 }
536539
537- fn channel_layout ( & self ) -> u64 {
538- unsafe { self . inner . as_ref ( ) . unwrap ( ) . channel_layout }
540+ fn channel_layout ( & self ) -> AVChannelLayout {
541+ unsafe { self . inner . as_ref ( ) . unwrap ( ) . ch_layout }
539542 }
540543
541544 fn is_planar ( & self ) -> i32 {
0 commit comments