[Libav-user] PCM interleaved audio

william keeling williamkeeling at hotmail.com
Thu Nov 25 18:17:53 EET 2021

I have 2 contexts defined and I and running the samples thru swresample before  sending to AAC encoder.  Bur I must have something define wrong as not interleaved I get only a tone.

Using the following code the define the RAW, PCM and resample contexts.

  int status=0;
//  setup RAW codec and context
  AVCodec *raw_codec = avcodec_find_encoder(AV_CODEC_ID_PCM_S32LE);

  if (!raw_codec)
    log_error("PCM_S32_LE codec id not found!");
    return -1;
  actx->rawctx = avcodec_alloc_context3(raw_codec);
  if (!actx->rawctx)
    log_error("Could not alloc RAW context");
    return -1;

  actx->rawctx->channels       = 2;
  actx->rawctx->channel_layout = av_get_default_channel_layout(2);
  actx->rawctx->sample_rate    = DEFAULT_SPEED;
  actx->rawctx->sample_fmt     = raw_codec->sample_fmts[0];  // AV_SAMPLE_FMT_S32
  actx->rawctx->bit_rate       = 2822400;  // or 64000
  actx->rawctx->time_base.num  = 1;
  actx->rawctx->time_base.den  = 44100;
  actx->rawctx->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;   // Allow the use of the experimental AAC encoder.

  //  setup AAC codec and stream context
  AVCodec *aac_codec = avcodec_find_encoder(AV_CODEC_ID_AAC);
  if (!aac_codec)
    log_error("AAC codec id not found!");
    return -1;

  actx->audctx = avcodec_alloc_context3(aac_codec);
  log_debug("encode aac contet %p", actx->audctx);

  if (!actx->audctx)
    log_error("Could not alloc an encoding context");
    return -1;

  actx->audctx->channels       =  2;     //use default channels???
  actx->audctx->channel_layout = av_get_default_channel_layout(2);  //use default channels???
  actx->audctx->sample_rate    = 44100;
  actx->audctx->sample_fmt     = aac_codec->sample_fmts[0];
  actx->audctx->bit_rate       = 64000;
  actx->audctx->strict_std_compliance = FF_COMPLIANCE_EXPERIMENTAL;  // Allow the use of the experimental AAC encoder.
  if ((status = avcodec_open2(actx->audctx, aac_codec, NULL) < 0))
    log_error("Could not open output codec (error '%s')", av_err2str(status));
    return -1;

//  setup resampler context
  actx->swrctx = swr_alloc_set_opts(NULL, av_get_default_channel_layout(actx->audctx->channels), actx->audctx->sample_fmt,
    actx->audctx->sample_rate, av_get_default_channel_layout(actx->rawctx->channels), actx->rawctx->sample_fmt,
    actx->rawctx->sample_rate, 0, NULL);
  if (!actx->swrctx)
    log_error("Could not allocate resample context");
    return -1;

  if ((status = swr_init(actx->swrctx)) < 0)
    log_error("Could not open resample context");
    return -1;
From: Libav-user <libav-user-bounces at ffmpeg.org> on behalf of Paul B Mahol <onemda at gmail.com>
Sent: Thursday, November 25, 2021 2:03 AM
To: This list is about using libavcodec, libavformat, libavutil, libavdevice and libavfilter. <libav-user at ffmpeg.org>
Subject: Re: [Libav-user] PCM interleaved audio

On Thu, Nov 25, 2021 at 3:38 AM william keeling <WilliamKeeling at hotmail.com<mailto:WilliamKeeling at hotmail.com>> wrote:

Does anyone have an example of encoding PCM interleaved audio stream?

I can  encode the stream if I manual un-interleave the PCM samples and put them in R/L planes.  I am using “avcodec_find_encoder(AV_CODEC_ID_PCM_S32LE_PLANAR)” for my manually un-interleaved stream and “avcodec_find_encoder(AV_CODEC_ID_PCM_S32LE)” to the native interleaved stream.  I am not sure what else I need to do to define the stream as 2 channel interleaved stream.

For converting between interleaved vs noninterleaved it can be done manually or via libraries like libswresample of libavfilter.


Libav-user mailing list
Libav-user at ffmpeg.org<mailto:Libav-user at ffmpeg.org>

To unsubscribe, visit link above, or email
libav-user-request at ffmpeg.org<mailto:libav-user-request at ffmpeg.org> with subject "unsubscribe".
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://ffmpeg.org/pipermail/libav-user/attachments/20211125/77867a31/attachment.htm>

More information about the Libav-user mailing list