title: ffmpeg library pcm to ac3 encoding
body: <p>I'm new to the ffmpeg library and Im working on a custom directshow filter. I decided to use the ffmpeg library for the encoding of what I need to achieve. I'm a little confused with some of the parameters and the correct values with what ffmpeg is expecting.</p>
  
  <p>I'm currently working on the ac3 part of the custom filter.
  I've looked through the example of the encoding audio ( for MP2 encoding ) in the ffmpeg doc and I understand it, but I don't understand how I should adapt it to my specific needs.</p>
  
  <p>The incoming samples are at 48K samples per second 16bit per sample and are stereo interleaved. The upstream filter is giving them to me at 25fps so I get an incoming 'audio sample packet' of 1920 bytes for each audio frame. I want to encode that data into an ac3 data packet that I pass on to the next process that I'll be doing myself.</p>
  
  <p>But I'm unsure of the correct parameters for each component in the following code...</p>
  
  <p>The code I have so far. There are several questions in the comments at key places.</p>
  
  <pre><code>AVCodec*         g_pCodec = nullptr;
  AVCodecContext*  g_pContext = nullptr;
  AVFrame*         g_pFrame = nullptr;
  AVPacket         g_pPacket;
  LPVOID           g_pInSampleBuffer;
  
  avcodec_register_all();
  g_pCodec = avcodec_find_encoder(CODEC_ID_AC3);
  
  // What am I'm describing here? the incoming sample params or the outgoing sample params?
  // An educated guess is the outgoing sample params
  g_pContext = avcodec_alloc_context3(g_pCodec);
  g_pContext-&gt;bit_rate = 448000;
  g_pContext-&gt;sample_rate = 48000;
  g_pContext-&gt;channels = 2;
  g_pContext-&gt;sample_fmt = AV_SAMPLE_FMT_FLTP;
  g_pContext-&gt;channel_layout = AV_CH_LAYOUT_STEREO;
  
  // And this is the incoming sample params?
  g_pFrame = av_frame_alloc();
  g_pFrame-&gt;nb_samples = 1920; ?? What figure is the codec expecting me to give it here? 1920 / bytes_per_sample? 
  g_pFrame-&gt;format = AV_SAMPLE_FMT_S16;
  g_pFrame-&gt;channel_layout = AV_CH_LAYOUT_STEREO;
  
  // I assume this going to give me the size of a buffer that I use to fill with my incoming samples? I get a dwSize of 15360 but my samples are only coming in at 1920, does this matter?
  dwSize = av_samples_get_buffer_size(nullptr,2,1920,AV_SAMPLE_FMT_S16,0);
  
  // do I need to use av_malloc and copy my samples into g_pInSampleBuffer or can I supply the address of my own buffer ( created outside of the libav framework ) ?
  g_pInSampleBuffer = (LPVOID)av_malloc(dwSize)
  avcodec_fill_audio_frame(g_pFrame,2,AV_SAMPLE_FMT_S16,(const uint8_t*)g_pInSampleBuffer,*dwSize,0);
  
  // Encoding loop - samples are given to me through a directshow interface - DSInbuffer is the buffer containing the incoming samples
  av_init_packet(&amp;g_pPacket);
  g_pPacket.data = nullptr;
  g_pPacket.size = 0;
  
  int gotpacket = 0;
  int ok = avcodec_encode_audio2(g_pContext,&amp;g_pPacket,g_pFrame,&amp;gotpacket);
  if((ok == 0) &amp;&amp; gotpacket){
     // I then copy from g_pPacket.data an amount of g_pPacket.size bytes into another directshow interface buffer that sends the encoded sample downstream.
  
      av_free_packet(&amp;g_pPacket);
  }
  </code></pre>
  
  <p>Currently it will crash at the avcodec_encode_audio2 call. If I change the format parameter to AV_SAMPLE_FMT_FLTP in the avcodec_fill_audio_frame call then it doesnt crash but it only encodes 1 frame of data and i get error -22 on the next frame. The pPacket.size parameter is 1792 ( 7 * 256 ) after the first avcode_encode_audio2 call.</p>
  
  <p>As I'm new to ffmpeg I'm sure it will probably be something quite straight forward that I've missed or I'm misunderstanding, and I'm confused at where the parameters for the incoming samples are and the outgoing samples.</p>
  
  <p>This is obviously and extract from the main function that I've created, and I've manually typed into the forum. If there are spelling mistakes that are by mistake here, then original code compiles and runs.</p>
  
  <p>Dave.</p>