title: Writing alpha channel into decoded ffmpeg frame
body: <p>I am looking for a fast way to add my own alpha channel to a decoded ffmpeg frame.</p>
  
  <p>I have an AVI file with RGB information, and I have a synchronized video stream describing the transparency alpha channel (grayscale). While decoding the AVI file using ffmpeg, I want to convert the output frame to RGBA, while adding my own alpha information. In the end, I would obtain a semi-transparent video stream.</p>
  
  <p>Is there any optimized function, maybe in libswscale or libswresample, to do such a thing better than just iterating through pixels ?</p>
  
  <p>Basically I would like to be able to write such a function, if only I had such a function as <strong>sws_scale_and_add_alpha</strong>:</p>
  
  <pre><code>void* FFmpegLib_nextFrame_withAlpha(void* _handle, uint8_t* my_alpha_channel)
  {
      FFmpegLibHandle* handle = (FFmpegLibHandle*)_handle;
      AVPacket        packet;
      int             frameFinished;
  
      while(av_read_frame(handle-&gt;pFormatCtx, &amp;packet) &gt;= 0) {
          // Is this a packet from the video stream?
          if(packet.stream_index==handle-&gt;videoStream) {
              // Decode video frame
              avcodec_decode_video2(handle-&gt;pCodecCtx, handle-&gt;pFrame, &amp;frameFinished, &amp;packet);
              // Did we get a video frame?
              if(frameFinished) {
                  sws_scale_and_add_alpha
                  (
                      handle-&gt;sws_ctx,
                      (uint8_t const * const *)handle-&gt;pFrame-&gt;data,
                      handle-&gt;pFrame-&gt;linesize,
                      0,
                      handle-&gt;pCodecCtx-&gt;height,
                      handle-&gt;pFrameARGB-&gt;data,
                      handle-&gt;pFrameARGB-&gt;linesize,
                      my_alpha_channel
                  );
  
                  return handle-&gt;pFrameARGB-&gt;data;
              }
          }
      }
  
      return NULL;
  }
  </code></pre>