title: FFMPEG: While decoding video, is possible to generate result to user's provided buffer?
body: <p>In ffmpeg decoding video scenario, <strong>H264</strong> for example, typically we allocate an <code>AVFrame</code> and decode the compressed data, then we get the result from the member <code>data</code> and <code>linesize</code> of <code>AVFrame</code>. As following code:</p>
  
  <pre><code>// input setting: data and size are a H264 data.
  AVPacket avpkt;
  av_init_packet(&amp;avpkt);
  avpkt.data = const_cast&lt;uint8_t*&gt;(data);
  avpkt.size = size;
  
  // decode video: H264 ---&gt; YUV420
  AVFrame *picture = avcodec_alloc_frame();
  int len = avcodec_decode_video2(context, picture, &amp;got_picture, &amp;avpkt);
  </code></pre>
  
  <p>We may use the result to do something other tasks, for example, using DirectX9 to render. That is, to prepare buffers(DirectX9 Textures), and copy from the result of decoding.</p>
  
  <pre><code>D3DLOCKED_RECT lrY;
  D3DLOCKED_RECT lrU;
  D3DLOCKED_RECT lrV;
  textureY-&gt;LockRect(0, &amp;lrY, NULL, 0);
  textureU-&gt;LockRect(0, &amp;lrU, NULL, 0);
  textureV-&gt;LockRect(0, &amp;lrV, NULL, 0);
  
  // copy YUV420: picture-&gt;data ---&gt; lr.pBits.
  my_copy_image_function(picture-&gt;data[0], picture-&gt;linesize[0], lrY.pBits, lrY.Pitch, width, height);
  my_copy_image_function(picture-&gt;data[1], picture-&gt;linesize[1], lrU.pBits, lrU.Pitch, width / 2, height / 2);
  my_copy_image_function(picture-&gt;data[2], picture-&gt;linesize[2], lrV.pBits, lrV.Pitch, width / 2, height / 2);
  </code></pre>
  
  <p>This process is considered that <strong>2 copy</strong> happens(ffmpeg copy result to picture->data, and then copy picture->data to DirectX9 Texture).</p>
  
  <p>My question is: is it possible to improve the process to only <strong>1 copy</strong> ? On the other hand, can we provide buffers(<code>pBits</code>, the buffer of DirectX9 textures) to ffmpeg, and decode function results to buffer of DirectX9 texture, not to buffers of <code>AVFrame</code> ?</p>