gpt4 book ai didi

ffmpeg - 如何将FFmpeg的AVPacket转入CUVID的CUVIDSOURCEDATAPACKET?或者如何使用 FFmpeg 的 CUVID,任何演示?

转载 作者:行者123 更新时间:2023-12-04 23:15:39 33 4
gpt4 key购买 nike

正如标题所说,我被困在How to transfer FFmpeg's AVPacket to CUVID's CUVIDSOURCEDATAPACKET,我关于这个问题的主要代码如下:`

AVPacket* avpkt;
avpkt = (AVPacket*)av_malloc(sizeof(AVPacket));
CUVIDSOURCEDATAPACKET cupkt;
int iPkt = 0;
while (av_read_frame(pFormatCtx, avpkt) >= 0) {
if (avpkt->stream_index == videoindex) {

cuCtxPushCurrent(g_oContext);

if (avpkt && avpkt->size) {
cupkt.payload_size = (unsigned long)avpkt->size;
cupkt.payload = (const unsigned char*)avpkt->data;

if (avpkt->pts != AV_NOPTS_VALUE) {
cupkt.flags = CUVID_PKT_TIMESTAMP;
if (pCodecCtx->pkt_timebase.num && pCodecCtx->pkt_timebase.den) {
AVRational tb;
tb.num = 1;
tb.den = AV_TIME_BASE;
cupkt.timestamp = av_rescale_q(avpkt->pts, pCodecCtx->pkt_timebase, tb);
}
else
cupkt.timestamp = avpkt->pts;
}
}
else {
cupkt.flags = CUVID_PKT_ENDOFSTREAM;
}

oResult = cuvidParseVideoData(hParser_, &cupkt);
if ((cupkt.flags & CUVID_PKT_ENDOFSTREAM) || (oResult != CUDA_SUCCESS)) {
break;
}
iPkt++;
printf("Succeed to read avpkt %d !\n", iPkt);
checkCudaErrors(cuCtxPopCurrent(NULL));
}
av_free_packet(avpkt);
}

如您所见,代码
cupkt.payload_size = (unsigned long)avpkt->size;
cupkt.payload = (const unsigned char*)avpkt->data;

需要更正。
我英语很差,我希望我已经清楚地表达了我自己。

最佳答案

今天很开心。昨晚问了这个问题后,我去翻了FFmpeg的源代码,经过几个小时的努力,我发现我的代码和FFmpeg的代码有点不同。所以我会回答这个问题我。

AVBitStreamFilterContext* h264bsfc = NULL;
if (pCodecCtx->codec_id == AV_CODEC_ID_H264 || pCodecCtx->codec_id == AV_CODEC_ID_HEVC) {
if (pCodecCtx->codec_id == AV_CODEC_ID_H264)
h264bsfc = av_bitstream_filter_init("h264_mp4toannexb");
else
h264bsfc = av_bitstream_filter_init("hevc_mp4toannexb");
}

AVPacket *avpkt;
avpkt = (AVPacket *)av_malloc(sizeof(AVPacket));
CUVIDSOURCEDATAPACKET cupkt;
int iPkt = 0;
while (av_read_frame(pFormatCtx, avpkt) >= 0){
if (avpkt->stream_index == videoindex){

cuCtxPushCurrent(g_oContext);

if (avpkt && avpkt->size) {
if (h264bsfc)
{
av_bitstream_filter_filter(h264bsfc, pFormatCtx->streams[videoindex]->codec, NULL, &avpkt->data, &avpkt->size, avpkt->data, avpkt->size, 0);
}

cupkt.payload_size = (unsigned long)avpkt->size;
cupkt.payload = (const unsigned char*)avpkt->data;

if (avpkt->pts != AV_NOPTS_VALUE) {
cupkt.flags = CUVID_PKT_TIMESTAMP;
if (pCodecCtx->pkt_timebase.num && pCodecCtx->pkt_timebase.den){
AVRational tb;
tb.num = 1;
tb.den = AV_TIME_BASE;
cupkt.timestamp = av_rescale_q(avpkt->pts, pCodecCtx->pkt_timebase, tb);
}
else
cupkt.timestamp = avpkt->pts;
}
}
else {
cupkt.flags = CUVID_PKT_ENDOFSTREAM;
}

oResult = cuvidParseVideoData(hParser_, &cupkt);
if ((cupkt.flags & CUVID_PKT_ENDOFSTREAM) || (oResult != CUDA_SUCCESS)){
break;
}
iPkt++;
//printf("Succeed to read avpkt %d !\n", iPkt);
checkCudaErrors(cuCtxPopCurrent(NULL));
}
av_free_packet(avpkt);
}

if (h264bsfc)
{
av_bitstream_filter_close(h264bsfc);
}

主要区别如下:
AVBitStreamFilterContext* h264bsfc = NULL;
if (pCodecCtx->codec_id == AV_CODEC_ID_H264 || pCodecCtx->codec_id == AV_CODEC_ID_HEVC) {
if (pCodecCtx->codec_id == AV_CODEC_ID_H264)
h264bsfc = av_bitstream_filter_init("h264_mp4toannexb");
else
h264bsfc = av_bitstream_filter_init("hevc_mp4toannexb");
}
········
if (h264bsfc)
{
av_bitstream_filter_filter(h264bsfc, pFormatCtx->streams[videoindex]->codec, NULL, &avpkt->data, &avpkt->size, avpkt->data, avpkt->size, 0);
}
·········
if (h264bsfc)
{
av_bitstream_filter_close(h264bsfc);
}

这是因为我使用的文件是H264格式的,必须用H264的NALU做一些事情(我不知道它是什么,我是从其他人的问题的答案中得到的,我也不是很清楚,也许问题不像我说的那样)。我添加的代码对我有用,我希望我的anwser可以帮助你。正如我所提到的,我的英语很差,如果你对我说的话感到困惑,那就去吧到代码。

关于ffmpeg - 如何将FFmpeg的AVPacket转入CUVID的CUVIDSOURCEDATAPACKET?或者如何使用 FFmpeg 的 CUVID,任何演示?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42830033/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com