H264:使用 ffmpeg 解码 nal 单元系列

发布于 2024-09-11 05:07:07 字数 833 浏览 3 评论 0原文

我尝试使用 ffmpeg (libavcodec) 解码一系列 nal 单元,但出现“无帧”错误。我按照 如何使用 x264 C API 将一系列图像编码为 H264?。我尝试了以下解码策略:

avcodec_init();  
avcodec_register_all();  
AVCodec* pCodec;  
pCodec=lpavcodec_find_decoder(CODEC_ID_H264);  
AVCodecContext* pCodecContext;  
pCodecContext=lpavcodec_alloc_context();  
avcodec_open(pCodecContext,pCodec);  
AVFrame *pFrame;  
pFrame=avcodec_alloc_frame();
//for every nal unit:    
    int frameFinished=0;  
    //nalData2 is nalData without the first 4 bytes
    avcodec_decode_video(pCodecContext,pFrame,&frameFinished,(uint8_t*) nalData2,nalLength);

我将到达此代码的所有单元都传递了,但frameFinished 保持为0。我猜pCodecContext 设置一定有问题。有人可以给我发送一个工作示例吗?

谢谢

I tried to decode a series of nal units with ffmpeg (libavcodec) but I get a "no frame" error. I produced the nal units with the guideline at How does one encode a series of images into H264 using the x264 C API?. I tried the following strategy for decoding:

avcodec_init();  
avcodec_register_all();  
AVCodec* pCodec;  
pCodec=lpavcodec_find_decoder(CODEC_ID_H264);  
AVCodecContext* pCodecContext;  
pCodecContext=lpavcodec_alloc_context();  
avcodec_open(pCodecContext,pCodec);  
AVFrame *pFrame;  
pFrame=avcodec_alloc_frame();
//for every nal unit:    
    int frameFinished=0;  
    //nalData2 is nalData without the first 4 bytes
    avcodec_decode_video(pCodecContext,pFrame,&frameFinished,(uint8_t*) nalData2,nalLength);

I passed all units I got to this code but frameFinished stays 0. I guess there must be something wrong with the pCodecContext setup. Can someone send me a working example?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

你怎么敢 2024-09-18 05:07:07

查看本教程。它应该能够解码任何视频类型,包括 H.264:

http://dranger.com/ffmpeg/

我不知道到底是什么导致了这个问题,但我怀疑这与您没有使用 libavformat 中的 av_read_frame 来解析出一帧数据有关。时间。 H.264 能够将帧分割成多个切片,从而分割成多个 NAL 单元。

我非常确定 x264 编码器默认情况下不会执行此操作,而是每帧生成一个 NAL 单元。然而,还有一些带有其他流信息的 NAL 单元需要馈送到解码器。我过去曾对此进行过实验,当 av_read_frame 解析出一帧数据时,它有时包含多个 NAL 单元。我建议密切关注本教程,看看是否有效。

另一件事是,我认为您确实需要将 NAL 单元的前 4 个字节传递到 avcodec_decode_video 中,如果这是您正在讨论的起始代码 (0x00000001)。研究了 av_read_frame 的输出后,当传递给解码器时,起始码仍在数据中。

Check out this tutorial. It should be able to decode any video type including H.264:

http://dranger.com/ffmpeg/

I don't know exactly what is causing the problem, but I suspect it has something to do with the fact that you are not using the av_read_frame from libavformat to parse out a frames worth of data at a time. H.264 has the ability to split a frame into multiple slices and therefore multiple NAL units.

I am pretty sure the x264 encoder does not do this by default and produces one NAL unit per frame. However, there are NAL units with other stream information that need to be fed to the decoder. I have experimented with this in the past and when av_read_frame parses out a frames worth of data, it sometimes contains multiple NAL units. I would suggest following the tutorial closely and seeing if that works.

Another thing is that I think you do need to pass the first 4 bytes of the NAL unit into avcodec_decode_video if that is the start code you are talking about (0x00000001). Having investigated the output from av_read_frame, the start codes are still in the data when passed to the decoder.

可可 2024-09-18 05:07:07

在编解码器上下文实例化代码之后尝试此操作:

  if(pCodec->capabilities & CODEC_CAP_TRUNCATED)
      pCodecContext->flags |= CODEC_FLAG_TRUNCATED; /* We may send incomplete frames */
  if(pCodec->capabilities & CODEC_FLAG2_CHUNKS)
      pCodecContext->flags2 |= CODEC_FLAG2_CHUNKS;

Try this after the codec context instantiation code:

  if(pCodec->capabilities & CODEC_CAP_TRUNCATED)
      pCodecContext->flags |= CODEC_FLAG_TRUNCATED; /* We may send incomplete frames */
  if(pCodec->capabilities & CODEC_FLAG2_CHUNKS)
      pCodecContext->flags2 |= CODEC_FLAG2_CHUNKS;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文