使用FFmpeg,如何解码H264数据包 [英] using FFmpeg, how to decode H264 packets

查看:1779
本文介绍了使用FFmpeg,如何解码H264数据包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



经过多次调查,我认为应该能够实现这一点,我很高兴能够解码H264数据包,这可以通过uint8_t的数组获得。只需将阵列放入AVPacket,如下所示

  AVPacket * avpkt =(AVPacket *)malloc(sizeof(AVPacket)* 1); 
av_init_packet(avpkt);
avpkt-> data = ct; // ct是数组
avpkt-> length = ....

和由avcodec_decode_video2()解码。代码的一部分就像

  ... 
codec = avcodec_find_decoder(CODEC_ID_H264);
gVideoCodecCtx = avcodec_alloc_context();
gFrame = avcodec_alloc_frame();
avcodec_decode_video2(gVideoCodecCtx,gFrame,& frameFinished,packet);
...

我想我正确设置所有必需的属性,但这个函数只返回-1:(



我刚刚发现-1来自



ret = avctx-> codec- avcodec_decode_video2()中的解码(avctx,picture,got_picture_ptr,avpkt);





我想知道的是,如果我可以通过avcodec_decode_video2()解码H264数据包(没有RTP头)。



感谢您提前的帮助。






///////////添加



好的,我我仍然试图找到一个解决方案,现在我正在做的是下面的这个RTP流中的H264流由FU-A编码



p>


  1. 收到RTP包


  2. 查看第二个字节的RTP头是> 0,这意味着它是第一个数据包(可能会被遵循)


  3. 查看下一个RTP数据包的第二个数据包是否> 0字节也,然后这意味着前一帧是一个完整的NAL, 0,数据包应该附加到前一个数据包。


  4. 删除数据包的所有RTP头,因此它只有FU指示符| FU头| NAL


  5. 尝试使用av​​codec_decode_video2()播放它


但它只返回-1 .....我应该删除FU指标和标题吗?



任何建议将非常赞赏



提前谢谢

解决方案


其实,我想知道的是,如果我可以通过avcodec_decode_video2()解码H264数据包(没有RTP头)。


您可能需要预先如果使用单个NAL单元模式以外的分组模式,则在将NAL单元传递到解码器之前,处理RTP有效载荷(重新组合分段的NALU,拆分的聚合NALU)。流中允许的NAL单元类型(STAP,MTAP,FU)取决于打包模式。有关打包模式的更多信息,请参阅 RFC 6184



其次,虽然我对FFMPEG并不熟悉,但可能更多的是一般的H.264解码问题:您必须始终使用H.264序列(SPS)和图片来初始化解码器参数集(PPS),然后才能解码其他帧。你做过吗?


I'm new to FFmpeg struggling to decode H264 packets which can be obtained as an array of uint8_t.

After many of investigations, I think it should be able to just put the array into an AVPacket like the below

AVPacket *avpkt = (AVPacket *)malloc(sizeof(AVPacket) * 1);
av_init_packet(avpkt);  
avpkt->data = ct;   // ct is the array
avpkt->length =....

and decode by avcodec_decode_video2(). A part of the code is like

...
codec = avcodec_find_decoder(CODEC_ID_H264);
gVideoCodecCtx = avcodec_alloc_context();
gFrame = avcodec_alloc_frame();
avcodec_decode_video2(gVideoCodecCtx, gFrame, &frameFinished, packet);
...

I guess I set all required properties properly but this function is returning only -1 :(

I just found the -1 is coming from

ret = avctx->codec->decode(avctx, picture, got_picture_ptr, avpkt);

in the avcodec_decode_video2();

Actually, what I'm wondering is if I can decode H264 packets (without RTP header) by avcodec_decode_video2().

Thanks for the help in advance.


/////////// added

OK, I'm still trying to find a solution. What I'm doing now is the below

** the H264 stream in this RTP stream is encoded by FU-A

  1. receive a RTP packet

  2. look if the second byte of the RTP header is > 0 which means it's the first packet (and possibly will be followed)

  3. see if the next RTP packet has > 0 at its second byte also, then it means the previous frame was a complete NAL or if this is < 0, the packet should be appended to the previous packet.

  4. remove all RTP header of the packets so it has only like FU indicator | FU header | NAL

  5. try play it with avcodec_decode_video2()

but it's only returning -1..... am I supposed to remove FU indicator and header too??

any suggestion will be very appreciated

thanks in advance.

解决方案

Actually, what I'm wondering is if I can decode H264 packets (without RTP header) by avcodec_decode_video2().

You may need to pre-process the RTP payload(s) (re-assemble fragmented NALUs, split aggregated NALUs) before passing NAL units to the decoder if you use packetization modes other than single NAL unit mode. The NAL unit types (STAP, MTAP, FU) allowed in the stream depends on the packetization mode. Read RFC 6184 for more info on packetization modes.

Secondly, while I am not that familiar with FFMPEG, it could be more of a general H.264 decoding issue: you must always initialise the decoder with the H.264 sequence (SPS) and picture parameter sets (PPS) before you will be able to decode other frames. Have you done that?

这篇关于使用FFmpeg,如何解码H264数据包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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