title: swr_convert float planar to S16
body: <p>How convert using libav API <code>AV_SAMPLE_FMT_FLTP</code> to <code>AV_SAMPLE_FMT_S16</code></p>
  
  <p>I'm trying to figure out how to resample and encode PCM (captured from Microphone) 44.1KHz to AAC 48.0KHz</p>
  
  <p>That's my resampler initializer:</p>
  
  <pre><code>void initialize_resampler(SwrContext*&amp; resamplerCtx, AVCodecContext* encoder, AVFrame*&amp; rawResampledAudioFrame, AVStream* audioFormatStream)
  {
      int nb_samples = (encoder-&gt;codec-&gt;capabilities &amp; AV_CODEC_CAP_VARIABLE_FRAME_SIZE) ? encoder-&gt;sample_rate : encoder-&gt;frame_size;
  
      int encoderFrameSize = encoder-&gt;channels * av_get_bytes_per_sample(encoder-&gt;sample_fmt) * encoder-&gt;frame_size;
      rawResampledAudioFrame = allocate_audioframe(encoder-&gt;sample_fmt, encoder-&gt;channel_layout, encoder-&gt;sample_rate, nb_samples);
  
      // Copy the stream parameters to the muxer
      check(avcodec_parameters_from_context(audioFormatStream-&gt;codecpar, encoder));
  
      // Create resampler context
      resamplerCtx = swr_alloc();
      if (resamplerCtx == nullptr)
          throw std::runtime_error("Could not allocate resampler context");
  
      // Set options
      check(av_opt_set_int(resamplerCtx, "in_channel_count", 2, 0));
      check(av_opt_set_int(resamplerCtx, "in_sample_rate", 44100, 0));
      check(av_opt_set_sample_fmt(resamplerCtx, "in_sample_fmt", AV_SAMPLE_FMT_S16, 0));
      check(av_opt_set_int(resamplerCtx, "out_channel_count", encoder-&gt;channels, 0));
      check(av_opt_set_int(resamplerCtx, "out_sample_rate", encoder-&gt;sample_rate, 0));
      check(av_opt_set_sample_fmt(resamplerCtx, "out_sample_fmt", encoder-&gt;sample_fmt, 0));
  
      // initialize the resampling context
      check(swr_init(resamplerCtx));
  }
  </code></pre>
  
  <p>And for resample I've this code:</p>
  
  <pre><code>    AVPacket pkt{};
      while (true)
      {
          AVPacket input_packet;
          av_init_packet(&amp;input_packet);
  
          check(av_read_frame(inputContext, &amp;input_packet));
  
          check(avcodec_send_packet(decoderContext, &amp;input_packet));
          check(avcodec_receive_frame(decoderContext, decodedFrame));
  
  
          // WHAT DO HERE swr_convert(resamplerContext, )
          av_packet_unref(&amp;input_packet);
  
          av_init_packet(&amp;pkt);
          auto in_stream = inputContext-&gt;streams[pkt.stream_index];
          auto out_stream = outputContext-&gt;streams[pkt.stream_index];
  
          pkt.pts = av_rescale_q_rnd(pkt.pts, in_stream-&gt;time_base, out_stream-&gt;time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
          pkt.dts = av_rescale_q_rnd(pkt.dts, in_stream-&gt;time_base, out_stream-&gt;time_base, (AVRounding)(AV_ROUND_NEAR_INF | AV_ROUND_PASS_MINMAX));
          pkt.duration = av_rescale_q(pkt.duration, in_stream-&gt;time_base, out_stream-&gt;time_base);
          pkt.pos = -1;
  
          check(avcodec_send_frame(encoderContext, decodedFrame));
          check(avcodec_receive_packet(encoderContext, &amp;pkt));
  
          check(av_interleaved_write_frame(outputContext, &amp;pkt));
          av_packet_unref(&amp;pkt);
      }
  </code></pre>
  
  <p>REading the docs I can't figure out what exactly I  need to pass to function. I have this code to encode PCM to MP2 (output is <code>AV_SAMPLE_FMT_S16</code>)  </p>
  
  <pre><code>const uint8_t* inPtr[] { const_cast&lt;const uint8_t*&gt;(&amp;pcmData[0]), nullptr, nullptr,nullptr,nullptr,nullptr,nullptr,nullptr };
  uint8_t* outPtr[] { &amp;resampledAudioData[0], nullptr, nullptr,nullptr,nullptr,nullptr,nullptr,nullptr };
  
  int resampledSamplesCount{ swr_convert(
      resamplerCtx,
      outPtr,
      maxResampledSamplesCount,
      inPtr,
      inputSampleCount) };
  
  // Negativo indica erro.
  check(resampledSamplesCount);
  </code></pre>
  
  <p>pcmData is raw data from input <code>AVPacket</code> (PCM)</p>
  
  <p>What I get here is: MP2 isn't planar so it uses the same outPtr[0] different from plannar which needs two valid pointers to same writable data. But what I need to pass to inPtr, for example? </p>
  
  <p>When I try to use the same code, ffmpeg try to write on outPtr[1] which is nullptr.</p>