H264:用ffmpeg解码nal单元系列 [英] H264: decode series of nal units with ffmpeg

查看:1412
本文介绍了H264:用ffmpeg解码nal单元系列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用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();
//每个nal单位:
int frameFinished = 0;
// nalData2是没有前4个字节的nalData
avcodec_decode_video(pCodecContext,pFrame,& frameFinished,(uint8_t *)nalData2,nalLength);

我传递了所有单位,我得到这个代码,但frameFinished保持0.我想有一些错误与pCodecContext设置。有人可以给我发一个工作示例吗?



谢谢

解决方案

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



http://dranger.com/ffmpeg/



我不知道究竟是什么导致问题,但我怀疑它有什么东西做一个事实,你不使用libavformat的 av_read_frame 一次解析一帧数据。 H.264能够将帧分割成多个片段,因此可以将多个NAL单元分割。



我非常确定x264编码器默认情况下不会执行此操作,并生成一个每帧NAL单位然而,存在需要将其馈送到解码器的其他流信息的NAL单元。过去我已经尝试了这一点,当 av_read_frame 解析出一帧数据时,它有时会包含多个NAL单元。我会建议紧跟教程,看看是否有效。



另外一件事我认为你需要将NAL单元的前4个字节传递到code> avcodec_decode_video 如果这是您正在谈论的开始代码( 0x00000001 )。调查了 av_read_frame 的输出后,起始码在传递给解码器时仍然在数据中。


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

解决方案

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.

这篇关于H264:用ffmpeg解码nal单元系列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆