【发布时间】:2021-08-18 00:43:25
【问题描述】:
我的库(Linux、Debian)使用 FFMpeg 库(avformat、avcodec、swscale 等)从网络摄像机读取视频流。实际上,我需要从网络摄像机捕获每个视频帧,对其进行解码,缩放并存储在内存中 - 其他线程将这些数据传递给调用程序进行显示。
问题是,这一切都在 CPU 中工作并占用大量 CPU 资源。如何强制使用 GPU 加速器进行处理?
我有显卡:VGA 兼容控制器:Intel Corporation HD Graphics 620 (rev 02)
我的解码线程看起来像这样(我省略了声明、错误处理等,所以请不要寻找语法错误:)))
fmt = avformat_alloc_context();
//initialising, setting option by av_dict_set
// finding video stream index
***
// finding decoder and allocate its contexts
frame = av_frame_alloc();
while ( av_read_frame(ctx->fmt, &pkt) >= 0)
{
AVPacket orig_pkt = pkt;
avcodec_send_packet(ctx->dec_ctx, pkt);
avcodec_receive_frame(ctx->dec_ctx, frame);
***
// get buffer allocated for store of frame data
buff = get_free_buffer(ctx);
sws_scale(ctx->sws, (const uint8_t * const*)frame->data,
frame->linesize, 0, ctx->dec_ctx->height, buff->data,
buff->linesize);
ret = decode_packet(ctx, frame, &pkt, &got_frame);
if (ret < 0)
break;
pkt.data += ret;
pkt.size -= ret;
}
while (pkt.size > 0);
av_packet_unref(&orig_pkt);
}
*****
【问题讨论】:
-
这个问题不适合超级用户。它将很快迁移。为确保它不会被拒绝,您需要对其进行改进:提供可编译的代码,提供尽可能多的代码。
-
如果您有可用的硬件解码器,则需要分配它。英特尔 CPU 将提供 QSV 或 VAAPI 加速。如果您想通过 GPU 进行扩展,您将需要使用 libavfilter 和相应的 GPU 过滤器,例如scale_vaapi 或 scale_qsv
-
吉安谢谢。可能这就是解决方案。你能给我合适例子的链接吗?