[Libav-user] Problem to get stereo audio. Converting FLT to S16P

Ignacio Barreto ignacio at radixcast.com
Tue Mar 29 05:00:41 CEST 2016

Hi I'm trying to convert an audio which format is AV_SAMPLE_FMT_FLT and I
want to convert it to MP3 audio file in AV_SAMPLE_FMT_S16P format.

I'll post my code below.

My problem is that this approach works fine, it generates the audio file
converted but when  play in Audacity I see that generates only the left
channel (I hear also only in the left speaker).

I've tried different approaches but none of them worked. So I'll post my
code in order to get an advice to solve this issue.

Perhaps the problem is in how I invoke swr_convert function or
avcodec_fill_audio_frame function.

Here is the code

int channels = 2;
unsigned char** audio_dst_data = NULL;
int audio_dst_linesize = 0;
char *pData = NULL;
unsigned int numFramesToRead = 0;
int audio_dst_bufsize = 0;
static struct SwrContext *swrctx = NULL;
AVCodecContext *codecCtx;
FILE *f;

void write()

//numFramesToRead is 480

int ret = av_samples_alloc(audio_dst_data, &audio_dst_linesize, channels,
if (ret < 0)
LOG_ERROR(logger, "Could not allocate audio buffer");
int v = 0;
audio_dst_bufsize = av_samples_get_buffer_size(&v, channels,

const unsigned char *srcBuffer[SWR_CH_MAX];

//the input audio comes in pData and its format is AV_SAMPLE_FMT_FLT

srcBuffer[0] = (unsigned char*)pData;
srcBuffer[1] = NULL;

unsigned char *dstplanes[SWR_CH_MAX];
dstplanes[0] = *audio_dst_data;
dstplanes[1] = (*audio_dst_data) + v;

ret = swr_convert(swrctx, dstplanes,
AVPacket pkt;

AVFrame *frame;
frame = av_frame_alloc();
frame->nb_samples = ret; //codecCtx->frame_size;
codecCtx->frame_size = ret;
frame->format = codecCtx->sample_fmt;
frame->channel_layout = codecCtx->channel_layout;
int buf_size = audio_dst_bufsize *
av_get_bytes_per_sample(codecCtx->sample_fmt) * codecCtx->channels;

ret = avcodec_fill_audio_frame(frame, codecCtx->channels,
     (const uint8_t*)dstplanes[0], buf_size, 0);
pkt.data = NULL; // packet data will be allocated by the encoder
pkt.size = 0;

//* encode the samples */
int got_output;
avcodec_encode_audio2(codecCtx, &pkt, frame, &got_output);
if (got_output != 0)
fwrite(pkt.data, 1, pkt.size, f);
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20160329/49f06995/attachment.html>

More information about the Libav-user mailing list