title: FFMpeg How to use multithreading?
body: <p>I want to decode H264 by ffmpeg, BUT finally I found the decode function only used one cpu core  </p>
  
  <p><a href="https://i.stack.imgur.com/z1qqk.png" rel="nofollow noreferrer">system monitor</a>  </p>
  
  <p>env: Ubuntu 14.04 FFMpeg 3.2.4 CPU i7-7500U
  <hr>
  So, I search ffmpeg multithreading and decide using all cpu cores for decoding.<br>
  I set AVCodecContext as this:</p>
  
  <pre><code>//Init works
  //codecId=AV_CODEC_ID_H264;
  avcodec_register_all();
  pCodec = avcodec_find_decoder(codecId);
  if (!pCodec)
  {
      printf("Codec not found\n");
      return -1;
  }
  pCodecCtx = avcodec_alloc_context3(pCodec);
  if (!pCodecCtx)
  {
      printf("Could not allocate video codec context\n");
      return -1;
  }
  
  pCodecParserCtx=av_parser_init(codecId);
  if (!pCodecParserCtx)
  {
      printf("Could not allocate video parser context\n");
      return -1;
  }
  pCodecCtx-&gt;thread_count = 4;
  pCodecCtx-&gt;thread_type = FF_THREAD_FRAME;
  
  pCodec-&gt;capabilities &amp;= CODEC_CAP_TRUNCATED;
  pCodecCtx-&gt;flags |= CODEC_FLAG_TRUNCATED;
  
  if (avcodec_open2(pCodecCtx, pCodec, NULL) &lt; 0)
  {
      printf("Could not open codec\n");
      return -1;
  }
  av_log_set_level(AV_LOG_QUIET);
  av_init_packet(&amp;packet);
  </code></pre>
  
  <hr>
  
  <pre><code>//parse and decode
  //after av_parser_parse2, the packet has a complete frame data
  //in decode function, I just call avcodec_decode_video2 and do some frame copy work
  while (cur_size&gt;0)
  {
      int len = av_parser_parse2(
                    pCodecParserCtx, pCodecCtx,
                    &amp;packet.data, &amp;packet.size,
                    cur_ptr, cur_size,
                    AV_NOPTS_VALUE, AV_NOPTS_VALUE, AV_NOPTS_VALUE);
  
      cur_ptr += len;
      cur_size -= len;
      if(GetPacketSize()==0)
          continue;
  
      AVFrame *pFrame = av_frame_alloc();
      int ret = Decode(pFrame);
      if (ret &lt; 0)
      {
          continue;
      }
      if (ret)
      {
        //some works
      }
  }
  </code></pre>
  
  <p>But nothing different with before.<br>
  How can I use multithreading in FFMpeg? Any advise?  </p>