[Libav-user] decoding audio with libav

Gonzalo Garramuño ggarra13 at gmail.com
Mon May 1 00:09:34 EEST 2017



El 26/04/2017 a las 22:32, luigi escribió:
> Hello,
>
> I'm trying to decode effectively audio in an .avi video file with
>
> decode_audio4, etc.
>
> I succeed as long as the decoded frames are stereo, but when I bump 
> into avi files which have 5.1 channels I get weird audio reproduction.
>
> I have so far tried to resample (swr_convert) the decoded frames, but 
> with no success.
>
>     av_opt_set_int(swr_ctx, "in_channel_layout", src_ch_layout, 0);
>     av_opt_set_int(swr_ctx, "in_sample_rate",       src_rate, 0);
>     av_opt_set_sample_fmt(swr_ctx, "in_sample_fmt", src_sample_fmt, 0);
>     av_opt_set_int(swr_ctx, "in_channel_count", dec_fr->channels, 0);
>
>     av_opt_set_int(swr_ctx, "out_channel_layout", dst_ch_layout, 0);
>     av_opt_set_int(swr_ctx, "out_sample_rate",       dst_rate, 0);
>     av_opt_set_sample_fmt(swr_ctx, "out_sample_fmt", dst_sample_fmt, 0);
>     av_opt_set_int(swr_ctx, "out_channel_count", 2, 0);
>
> Here, I would like to ask a few questions:
>
> 1) is the "in_channel_layout" the layout of the decoded frame before 
> it is resampled?
>
Yes.
> 2) what is it that swr_convert really does?
It converts among different audio channels, mixing them sometimes.
For swr_convert to work you need to have called swr_init() before (after 
you set the opts).
Example:

                     forw_ctx  = swr_alloc_set_opts(NULL, out_ch_layout,
out_sample_fmt,  out_sample_rate,
in_ch_layout,  in_sample_fmt,
in_sample_rate,
                                                    0, NULL);
                     if(!forw_ctx) {
                         LOG_ERROR( _("Failed to alloc swresample 
library") );
                         return 0;
                     }

                     if(swr_init(forw_ctx) < 0)
                     {
                         char buf[256];
                         av_get_channel_layout_string(buf, 256, -1, 
in_ch_layout);
                         LOG_ERROR( _("Failed to init swresample library 
with ")
                                    << buf << " "
                                    << av_get_sample_fmt_name(in_sample_fmt)
                                    << _(" frequency: ") << 
in_sample_rate );
                         return 0;
                     }

     int len2 = swr_convert(forw_ctx, (uint8_t**)&samples,
                                        _aframe->nb_samples, // No 
resampling wanted
                                        (const uint8_t 
**)_aframe->extended_data,
                                        _aframe->nb_samples );

-- 
Gonzalo Garramuño



More information about the Libav-user mailing list