title: ffmpeg memory increase when Playing a UDP-MJPEG-Stream
body: <p>im reading a udp-mjpeg-stream with the ffmpeg-API. When i read and display the Stream with an ARM-Processor i have 2 Problems:
  1- The Applikation is too slow and there is a big delay between network cam and displayed video.
  2- the memory usage increases every time when i call the function av_read_frame().</p>
  
  <p>The Source code </p>
  
  <pre><code>const char *cam1_url = "udp://192.168.1.1:1234";
   AVCodec *pCodec;
   AVFrame *pFrame, *pFrameRGB;
   AVCodecContext *pCodecCon;
   AVDictionary *pUdpStreamOptions = NULL;
   AVInputFormat *pMjpegFormat = av_find_input_format("mjpeg");
   av_dict_set(&amp;pUdpStreamOptions, "fifo_size", "5000000", 0);
   av_register_all();
   avdevice_register_all();
   avcodec_register_all();
   avformat_network_init();
   AVFormatContext *pFormatCont = avformat_alloc_context();
   if(avformat_open_input(&amp;pFormatCont,cam1_url,pMjpegFormat,&amp;pUdpStreamOptions) &lt; 0)
   {
     cout &lt;&lt; "!! Error !! - avformat_open_input(): failed to open input URL" &lt;&lt; endl;
   }
  
   if(avformat_find_stream_info(pFormatCont,NULL) &lt; 0)
   {
     cout &lt;&lt; "!! Error !! - avformat_find_stream_info(), Failed to retrieve stream info" &lt;&lt; endl;
   }
  
   av_dump_format(pFormatCont, 0, cam1_url, 0);
  
   int videoStream;
   for(int i=0; i&lt; pFormatCont-&gt;nb_streams; i++)
   {
      if(pFormatCont-&gt;streams[i]-&gt;codec-&gt;codec_type==AVMEDIA_TYPE_VIDEO)
      {
          videoStream=i;
          cout &lt;&lt; " videoStream = " &lt;&lt; videoStream &lt;&lt; endl;
      }
   }
   pCodecCon = pFormatCont-&gt;streams[videoStream]-&gt;codec;
   pCodec = avcodec_find_decoder(pCodecCon-&gt;codec_id);
   if(NULL == pCodec)
   {
     cout &lt;&lt; "couldnt find codec" &lt;&lt; endl;
     return EXIT_FAILURE;
   }
   if(avcodec_open2(pCodecCon,pCodec,NULL) &lt; 0)
   {
      cout &lt;&lt; "!! Error !! - in avcodec_open2()" &lt;&lt; endl;
      return EXIT_FAILURE;
   }
   uint8_t *frameBuffer;
   int numRxBytes = 0;
   AVPixelFormat pFormat =AV_PIX_FMT_BGR24;
   int width_rgb = (int)((float)pCodecCon-&gt;width);
   int height_rgb = (int)((float)pCodecCon-&gt;height);
   numRxBytes = avpicture_get_size(pFormat,width_rgb,height_rgb);
   frameBuffer = (uint8_t *) av_malloc(numRxBytes*sizeof(uint8_t));
   avpicture_fill((AVPicture *) pFrameRGB, frameBuffer, pFormat,width_rgb,height_rgb);
    AVPacket rx_pkt; // received packet
   int frameFinished = 0;
   struct SwsContext *imgConvertCtx;
   av_init_packet(&amp;rx_pkt);
   while(av_read_frame(pFormatCont, &amp;rx_pkt) &gt;= 0)
   {
     if(rx_pkt.stream_index == videoStream)
     {  
        av_frame_free(&amp;pFrame);
        pFrame = av_frame_alloc();
  
        av_frame_free(&amp;pFrameRGB);
        pFrameRGB = av_frame_alloc();
  
        avcodec_decode_video2(pCodecCon, pFrame, &amp;frameFinished,&amp;rx_pkt);
  
        if(frameFinished)
        {
  
          imgConvertCtx = sws_getCachedContext(NULL, pFrame-&gt;width,pFrame-&gt;height, AV_PIX_FMT_YUVJ420P,width_rgb,height_rgb,AV_PIX_FMT_BGR24, SWS_BICUBIC, NULL, NULL,NULL);
  
          sws_scale(imgConvertCtx, ((AVPicture*)pFrame)-&gt;data, ((AVPicture*)pFrame)-&gt;linesize, 0, pCodecCon-&gt;height, ((AVPicture *)pFrameRGB)-&gt;data, ((AVPicture *)pFrameRGB)-&gt;linesize);
  
          av_frame_unref(pFrame);
          av_frame_unref(pFrameRGB);
        }
     }
     av_free_packet(&amp;rx_pkt);
     av_packet_unref(&amp;rx_pkt);
   }
   //cvDestroyWindow("Cam1Video");
   av_free_packet(&amp;rx_pkt);
   avcodec_close(pCodecCon);
   av_free(pFrame);
   av_free(pFrameRGB);
   avformat_close_input(&amp;pFormatCont);
  </code></pre>
  
  <p>I have read, the reason could be that the ffmpeg-Libs saves the incomming frames in the cache but the arm-processor isnt fast enough to process them. After like 4 minutes the system craches.</p>
  
  <p>How could i solve the Problem.</p>
  
  <p>one option could be to tell ffmpeg to act as frame grabber, also to read frames in real time, with the flag "-re". How can i set this Flag in the c++ source code. Or can anybody help me to solve that Problem.</p>
  
  <p>Thank you very much </p>