<div dir="ltr">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.<div><br></div><div>I'll post my code below. </div><div><br></div><div>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).</div><div><br></div><div>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.</div><div><br></div><div>Perhaps the problem is in how I invoke swr_convert function or avcodec_fill_audio_frame function.</div><div>Thanks</div><div><br></div><div>Here is the code</div><div><br></div><div><br></div><div><div>int channels = 2;</div><div>unsigned char** audio_dst_data = NULL;</div><div>int audio_dst_linesize = 0;</div><div>char *pData = NULL;</div><div>unsigned int numFramesToRead = 0;</div><div>int audio_dst_bufsize = 0;</div><div>static struct SwrContext *swrctx = NULL;</div><div>AVCodecContext *codecCtx;</div><div>FILE *f;</div><div><br></div><div><br></div><div>void write()</div><div>{</div><div><br></div><div>//numFramesToRead is 480</div><div><br></div><div><span class="" style="white-space:pre">       </span>int ret = av_samples_alloc(audio_dst_data, &audio_dst_linesize, channels,</div><div><span class="" style="white-space:pre">              </span>numFramesToRead,</div><div><span class="" style="white-space:pre">           </span>AV_SAMPLE_FMT_S16P, 0);</div><div><span class="" style="white-space:pre">    </span></div><div><span class="" style="white-space:pre">   </span>if (ret < 0)</div><div><span class="" style="white-space:pre">    </span>{</div><div><span class="" style="white-space:pre">          </span>LOG_ERROR(logger, "Could not allocate audio buffer");</div><div><span class="" style="white-space:pre">            </span>return;</div><div><span class="" style="white-space:pre">    </span>}</div><div><span class="" style="white-space:pre">  </span>int v = 0;</div><div><span class="" style="white-space:pre"> </span>audio_dst_bufsize = av_samples_get_buffer_size(&v, channels,</div><div><span class="" style="white-space:pre">           </span>numFramesToRead,</div><div><span class="" style="white-space:pre">           </span>AV_SAMPLE_FMT_S16P, 0);</div><div><br></div><div><span class="" style="white-space:pre">   </span>const unsigned char *srcBuffer[SWR_CH_MAX];</div><div><br></div><div>//the input audio comes in pData and its format is AV_SAMPLE_FMT_FLT </div><div><br></div><div><span class="" style="white-space:pre"> </span>srcBuffer[0] = (unsigned char*)pData;</div><div><span class="" style="white-space:pre">      </span>srcBuffer[1] = NULL;</div><div><br></div><div><span class="" style="white-space:pre">      </span>unsigned char *dstplanes[SWR_CH_MAX];</div><div><span class="" style="white-space:pre">      </span>dstplanes[0] = *audio_dst_data;</div><div><span class="" style="white-space:pre">    </span>dstplanes[1] = (*audio_dst_data) + v;</div><div><br></div><div><span class="" style="white-space:pre">     </span>ret = swr_convert(swrctx, dstplanes,</div><div><span class="" style="white-space:pre">               </span>numFramesToRead,</div><div><span class="" style="white-space:pre">           </span>srcBuffer,</div><div><span class="" style="white-space:pre">         </span>numFramesToRead);</div><div><span class="" style="white-space:pre">          </span></div><div><span class="" style="white-space:pre">   </span>AVPacket pkt;</div><div><span class="" style="white-space:pre">      </span>av_init_packet(&pkt);</div><div><br></div><div><span class="" style="white-space:pre"> </span>AVFrame *frame;</div><div><span class="" style="white-space:pre">    </span>frame = av_frame_alloc();</div><div><span class="" style="white-space:pre">  </span>frame->nb_samples = ret; //codecCtx->frame_size;</div><div><span class="" style="white-space:pre">     </span>codecCtx->frame_size = ret;</div><div><span class="" style="white-space:pre">     </span>frame->format = codecCtx->sample_fmt;</div><div><span class="" style="white-space:pre">        </span>frame->channel_layout = codecCtx->channel_layout;</div><div><span class="" style="white-space:pre">    </span></div><div><span class="" style="white-space:pre">   </span>int buf_size = audio_dst_bufsize * av_get_bytes_per_sample(codecCtx->sample_fmt) * codecCtx->channels;</div><div><br></div><div><span class="" style="white-space:pre">      </span>ret = avcodec_fill_audio_frame(frame, codecCtx->channels, codecCtx->sample_fmt,</div><div><span class="" style="white-space:pre">      </span>      (const uint8_t*)dstplanes[0], buf_size, 0);</div><div><span class="" style="white-space:pre">       </span></div><div><span class="" style="white-space:pre">   </span>pkt.data = NULL; // packet data will be allocated by the encoder</div><div><span class="" style="white-space:pre">   </span>pkt.size = 0;</div><div><br></div><div><span class="" style="white-space:pre">     </span>//* encode the samples */</div><div><span class="" style="white-space:pre">  </span>int got_output;</div><div><span class="" style="white-space:pre">    </span>avcodec_encode_audio2(codecCtx, &pkt, frame, &got_output);</div><div><span class="" style="white-space:pre"> </span>if (got_output != 0)</div><div><span class="" style="white-space:pre">       </span>{</div><div><span class="" style="white-space:pre">          </span>fwrite(pkt.data, 1, pkt.size, f);</div><div><span class="" style="white-space:pre">  </span>}</div><div><span class="" style="white-space:pre">  </span></div><div>}</div></div></div>