I am developing a Qt application to decode H.264 streams, but the CPU usage of the decoding function is very high.
QImage H264Decoder::Decode(const unsigned char *data, quint32 len, quint32 *consumed)
{
pkt->data = (uint8_t*)data;
pkt->size = len;
AVFrame* decoded_frame = av_frame_alloc();
if(consumed != NULL)
*consumed = pkt->size;
int ret1 = avcodec_send_packet(codec_ctx, pkt);
ret1 = avcodec_receive_frame(codec_ctx, decoded_frame);
int height = decoded_frame->height;
int width = decoded_frame->width;
if(height ==0 || width ==0 )
{
return QImage();
}
SwsContext* sws_ctx = sws_getContext(width, height, static_cast<AVPixelFormat>(decoded_frame->format), width, height, AV_PIX_FMT_RGB32, SWS_BILINEAR, 0, 0, 0);
QImage img(width, height, QImage::Format_RGB32);
uint8_t* data1[1] = { reinterpret_cast<uint8_t*>(img.bits()) };
int linesize[1] = { static_cast<int>(img.bytesPerLine()) };
ret1 = sws_scale(sws_ctx, decoded_frame->data, decoded_frame->linesize, 0, height, data1, linesize);
sws_freeContext(sws_ctx);
av_frame_free(&decoded_frame);
return img;
}
Is there any way to lower the CPU usage? Thanks!