<div dir="ltr"><div class="gmail_quote"><div dir="ltr">Hi,<div><br></div><div>I am using Libav* libraries to decode the frames from the byte stream data I am receiving from another thread.</div><div><br></div><div>I have been able to successfully parse the byte stream and render video correctly using the OpenCV API. <b>Basic flow in my code using libav is like:</b></div><div><b><u>init()</u> { </b></div><div><b>avcodec_find_decoder(AV_CODEC_ID_H264) ;</b></div><div><b>avcodec_alloc_context3(codec);</b></div><div><b>avcodec_open2(codec_context, codec, NULL)<br></b></div><div><b><div>picture = av_frame_alloc();</div><div>pictureBGR = av_frame_alloc();</div><div><b><div style="display:inline!important">parser = av_parser_init(AV_CODEC_ID_H264);</div></b></div><div><b><div style="display:inline!important">codec_context->thread_count = 2; // tried to increase threads<br></div></b></div><div><b><div style="display:inline!important">}</div></b></div><div><b><div style="display:inline!important"><br></div></b></div><div><b><div style="display:inline!important"><u>readFrame() </u>{</div></b></div><div><b><div style="display:inline!important"><div>int len = av_parser_parse2(parser, codec_context, &data, &size,</div><div>                             &buffer[0], buffer.size(), 0, 0, AV_NOPTS_VALUE); </div><div>...</div></div></b></div><div><b><div style="display:inline!important">int len_dec = avcodec_decode_video2(codec_context, picture, &got_picture, &pkt); </div></b><b><b><div style="display:inline!important">// takes most time (~77%)</div></b></b><b><div style="display:inline!important"><br></div></b></div><div><b><b><div style="display:inline!important">...</div></b></b></div><div><b><b><div style="display:inline!important">iResult = sws_scale(...)<br></div></b></b></div></b><b><div><b><div style="display:inline!important">}</div></b></div></b></div><div><br></div><div>The problem I am facing is related to performance of my decoding code. My program manages to do 10 frames per second (fps) for a 720p video. When I save the same video stream as a file and use OpenCV code to parse it, it is able to parse the same video at around 24 fps.</div><div><br></div><div>I did basic profiling of my code and found that most of the time is going in decoding call "<b>avcodec_decode_video2()</b>" (~77% time) and SWS rescaling call "<b>sws_scale()</b>" (~18% time).</div><div><br></div><div>I haven't profiled the OpenCV code, but one difference I noticed is that OpenCV code uses multiple cores from <b>top</b> and <b>time</b> command statistics(shared below). And, my code runs single threaded.</div><div>My question here is: How can I improve the performance of my code ? By making it multithreaded ? Are there any library specific tweaks that can help ?</div><div><br></div><div><b><u>Time command statistics</u></b></div><div><b>For OpenCV code:</b></div><div><div>real<span style="white-space:pre-wrap">      </span>0m17.496s</div><div>user<span style="white-space:pre-wrap">    </span>0m48.030s</div><div>sys<span style="white-space:pre-wrap">     </span>0m2.410s</div></div><div><br></div><div><b>For My code using Libav library:</b></div><div><div>real<span style="white-space:pre-wrap">   </span>0m39.478s</div><div>user<span style="white-space:pre-wrap">    </span>0m39.380s</div><div>sys<span style="white-space:pre-wrap">     </span>0m0.100s</div></div><div><br></div><div><br></div><div>Thanks</div><span class="HOEnZb"><font color="#888888"><div>Puneet</div></font></span></div>
</div><br></div>