title: Fast way of opening Live Input with libavcodec
body: <p>I'm currently working with a live video input from the Hauppauge WinTV950Q, which gives me a live Video Input. </p>
  
  <p>I'm using the <code>libav*</code> and <code>Qt</code> library and am programming in <code>C/C++</code> on Linux. I use the recommended <code>avcodec_open_input()</code> for opening and processing (transcoding) the input for the use on mobile Devices. I want to be able to watch TV on my mobile Devices and also be able to switch channels remotely. Everything works fine so far.</p>
  
  <p>My only problem is that the opening of an input takes between 5 and 20 seconds.  Which is to slow for my use case, I would be okay with delays between 1-3 seconds. The v4l2-API which I formerly used for first tests actually manages to get switch times between 1-3seconds, I can not use it though because I need to do some transcoding so I can use the video data on my mobile devices.</p>
  
  <p><strong>Is there a way to improve the opening time of the <code>avformat_open_input()</code> call? Maybe some global variables in the library that need to be set before opening the input?</strong></p>
  
  <h2>Here's the relevant piece of code:</h2>
  
  <pre><code>const char* filename = "/dev/dvb/adapter0/dvr0";
  
  int main(void)
  {
      /*Qt-Variables*/
      QTime timer;
      /*General AV-IO*/
      int i, videoStream, audioStream, frameFinished;
      AVFormatContext *ptrFormatContext;
      AVPacket ptrPacket;
      /*Audio related*/
      AVCodecContext  *aCodecCtx;
      AVCodecContext  *aTargetCodecCtxt;
      AVCodec         *aCodec;
      AVCodec         *aTargetCodec;
      uint8_t         *audio_samples;
      int             frame_size_ptr;
      //    AVSampleFormat  ptrSampleFormats[2] = {AV_SAMPLE_FMT_S16, AV_SAMPLE_FMT_S32};
  
      /*Video related*/
      AVCodecContext *ptrCodecCtxt;
      AVCodecContext  *vTargetCodecCtxt;
      AVCodec *ptrCodec;
      AVCodec         *vTargetCodec;
      AVFrame *ptrFrame;
  
      /*Default Initialization of Variables*/
      audioStream = videoStream = -1;
      audio_samples = NULL;
  
  
      /*Registering and initialization of the libav* codecs, formats, filter etc.*/
  
      qDebug("Registering Codecs, Filter, Formats, Media Libraries etc...");
      timer.start();
      av_register_all();
      avcodec_register_all();
      qDebug()&lt;&lt;"Elapsed(ms): "&lt;&lt;timer.elapsed();
  
      /*Assigning a memory array to the Format Context and the Frame for the Video decoding, now we can start working*/
      qDebug("Allocating Codec Context and Frames for main Input...");
      timer.start();
      ptrFrame = avcodec_alloc_frame();
      ptrFormatContext = avformat_alloc_context();
      qDebug()&lt;&lt;"Elapsed(ms): "&lt;&lt;timer.elapsed();
  
      /*Opening the Live Video Input, sometimes very slow.*/
      qDebug("Trying to open the Input...");
      timer.start();
      if(avformat_open_input(&amp;ptrFormatContext, filename, NULL, NULL) != 0 )
      {
          qDebug("Error opening the input");
          exit(-1);
      } else qDebug()&lt;&lt;"Elapsed(ms): "&lt;&lt;timer.elapsed();
  </code></pre>
  
  <h2>Here's the output:</h2>
  
  <pre><code>Registering Codecs, Filter, Formats, Media Libraries etc...
  Elapsed(ms):  0 
  Allocating Codec Context and Frames for main Input...
  Elapsed(ms):  0 
  Trying to open the Input...
  Elapsed(ms):  14752 
  </code></pre>