[Libav-user] Audio conversion from planar to non-planar formats

Stefano Sabatini stefasab at gmail.com
Wed Jul 3 19:24:04 CEST 2013


In data Wednesday 2013-06-26 13:07:05 +0500, Taha Ansari ha scritto:
> Hi everyone!
> 
> >>It should be exactly the same as FLTP to S16. I'm not willing to play
> >>guess games. Please provide source code you use for convert audio if you
> >>expect someone will help you.
> 
> @Paul:
> 
> My code is similar to what I have been nagging since few days on the
> mailing list, so sorry for not providing it (attached now near end of
> email)!
> 
> While experimenting, I have made significant progress: now, my conversions
> from S16P to S16 are apparently fine (I was making a grave mistake while
> calling swr_convert() function). So current status is: I am able to convert
> between these two formats, but! only about 90% of audio is converted, and
> any media player just jumps ahead after reaching this mark.
> 
> Kindly review my updated code (note: I am also writing delayed frames in
> end, but that does not help):
[...] 
> int decode_packet()
> {
>     int rv = 0;
>     int ret = 0;
> 
>     //audio stream?
>     if(input_packet.stream_index == audio_stream_index)
>     {
>         /* decode audio frame */
>         rv = avcodec_decode_audio4(codec_ctx_audio, decoded_frame,
> &got_frame, &input_packet);
>         if (rv < 0)
>         {
>             fprintf(stderr, "Error decoding audio frame\n");
>             //return ret;
>         }
>         else
>         {
>             if (got_frame)
>             {
>                 if ( audio_dst_data[0] == NULL )
>                 {
>                      ret = av_samples_alloc(audio_dst_data,
> &audio_dst_linesize, decoded_frame->channels,
>                         decoded_frame->nb_samples,
> (AVSampleFormat)decoded_frame->format, 1);
>                     if (ret < 0)
>                     {
>                         fprintf(stderr, "Could not allocate audio
> buffer\n");
>                         return AVERROR(ENOMEM);
>                     }
>                     /* TODO: extend return code of the av_samples_*
> functions so that this call is not needed */
>                     audio_dst_bufsize = av_samples_get_buffer_size(NULL,
> audio_st->codec->channels,
>                         decoded_frame->nb_samples,
> (AVSampleFormat)decoded_frame->format, 1);
> 
>                     //int16_t* outputBuffer = ...;
>                     swr_convert( swr, audio_dst_data,
> out_frame->nb_samples, (const uint8_t**) decoded_frame->extended_data,
> decoded_frame->nb_samples );
>                 }

You may need to compute the output number of samples, indeed the
resampler can not guarantee that the number of samples requested will
be returned due to caching or missing input data (e.g. when
downsampling). Check the logic in resampling_audio.c, and check the
swr_convert() return value in order to understand how many samples
have been converted in your buffer.

A more high-level conversion function may help to simplify the code.

[...]


More information about the Libav-user mailing list