[Libav-user] Fast parsing of H.264 video stream using libavcodec

Puneet Kapoor puneet.cse.iitd at gmail.com
Wed Jul 20 22:46:49 CEST 2016


I am using Libav* libraries to decode the frames from the byte stream data
I am receiving from another thread.

I have been able to successfully parse the byte stream and render video
correctly using the OpenCV API. *Basic flow in my code using libav is like:*
*init() { *
*avcodec_find_decoder(AV_CODEC_ID_H264) ;*

*avcodec_open2(codec_context, codec, NULL)*

*picture = av_frame_alloc();pictureBGR = av_frame_alloc();parser =
av_parser_init(AV_CODEC_ID_H264);codec_context->thread_count = 2; // tried
to increase threads}readFrame() {int len = av_parser_parse2(parser,
codec_context, &data, &size,                             &buffer[0],
buffer.size(), 0, 0, AV_NOPTS_VALUE); ...int len_dec =
avcodec_decode_video2(codec_context, picture, &got_picture, &pkt); // takes
most time (~77%)...iResult = sws_scale(...)*

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.

I did basic profiling of my code and found that most of the time is going
in decoding call "*avcodec_decode_video2()*" (~77% time) and SWS rescaling
call "*sws_scale()*" (~18% time).

I haven't profiled the OpenCV code, but one difference I noticed is that
OpenCV code uses multiple cores from *top* and *time* command
statistics(shared below). And, my code runs single threaded.
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 ?

*Time command statistics*
*For OpenCV code:*
real 0m17.496s
user 0m48.030s
sys 0m2.410s

*For My code using Libav library:*
real 0m39.478s
user 0m39.380s
sys 0m0.100s

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://ffmpeg.org/pipermail/libav-user/attachments/20160721/92996da1/attachment.html>

More information about the Libav-user mailing list