本文介绍了h264 ffmpeg:如何初始化ffmpeg以解码使用x264创建的NAL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用x264,x264_encoder_encode编码了一些帧,然后使用如下函数创建了AVPackets:

I have encoded some frames using x264, using x264_encoder_encode and after that I have created AVPackets using a function like this:

bool PacketizeNals( uint8_t* a_pNalBuffer, int a_nNalBufferSize, AVPacket* a_pPacket )
{
    if ( !a_pPacket )
return false;
    a_pPacket->data = a_pNalBuffer;
    a_pPacket->size = a_nNalBufferSize;
    a_pPacket->stream_index = 0;
    a_pPacket->flags = AV_PKT_FLAG_KEY;

    a_pPacket->pts = int64_t(0x8000000000000000);
    a_pPacket->dts = int64_t(0x8000000000000000);
}

我这样称呼这个函数:

x264_nal_t* nals;
int num_nals = encode_frame(pic, &nals);
for (int i = 0; i < num_nals; i++)
{
    AVPacket* pPacket = ( AVPacket* )av_malloc( sizeof( AVPacket ) );
    av_init_packet( pPacket );
    if ( PacketizeNals( nals[i].p_payload, nals[i].i_payload, pPacket ) )
    {
        packets.push_back( pPacket );
    }
}

现在我要做的是使用avcodec_decode_video2解码这些AVPackets.我认为问题在于我没有正确初始化解码器,因为要进行编码,我使用了"ultrafast"配置文件和"zerolatency"调(x264),而对解码我不知道如何指定ffmpeg这些选项.在某些示例中,我读过人们使用存储视频的文件来初始化解码器,但是在这种情况下,我直接拥有了AVPackets.我正在尝试解码的是:

Now what I want to do is to decode these AVPackets using avcodec_decode_video2. I think the problem is that I haven't initialized properly the decoder because to encode I used "ultrafast" profile and "zerolatency" tune ( x264 ) and to decode I don't know how to specify to ffmpeg these options.In some examples I have read people initialize the decoder using the file where the video is stored, but in this case I have directly the AVPackets.What I'm doing to try to decode is:

avcodec_init();
avcodec_register_all();
AVCodec* pCodec;
pCodec=avcodec_find_decoder(CODEC_ID_H264);
AVCodecContext* pCodecContext;
pCodecContext=avcodec_alloc_context();
avcodec_open(pCodecContext,pCodec);
pCodecContext->width = 320;
pCodecContext->height = 200;
pCodecContext->extradata = NULL;
unsigned int nNumPackets = packets.size();
int frameFinished = 0;
for ( auto it = packets.begin(); it != packets.end(); it++ )
{
    AVFrame* pFrame;
    pFrame = avcodec_alloc_frame();
    AVPacket* pPacket = *it;
    int iReturn = avcodec_decode_video2( pCodecContext, pFrame, &frameFinished, pPacket );
}

但是在iReturn中,总为-1.

But in iReturn always is -1.

有人可以帮助我吗?抱歉,如果我在这方面的知识不足,我是新手.

Can anyone help me? Sorry if my knowledge in this area es low, I'm new.

谢谢.

推荐答案

我已经编写了一个简单的客户端/服务器应用程序,该应用程序使用lib x264进行编码和使用ffmpeg进行解码来传输原始RGB视频.您可以在此处找到代码: https://github.com/filippobrizzi/raw_rgb_straming

I have written a simple client/server application that streams raw RGB video using lib x264 for encoding and ffmpeg for decoding.You can find the code here: https://github.com/filippobrizzi/raw_rgb_straming

它显示了如何设置x264和ffmpeg进行编码/解码.

It shows how to setup x264 and ffmpeg to encode/decode.

这篇关于h264 ffmpeg:如何初始化ffmpeg以解码使用x264创建的NAL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

05-30 23:30