Decoding H264 Frame Error

I am trying to decode a H264 frame using the libav library. After initialising the library by allocating frame and context, I am using the following code to decode:

AVPacket pkt;
int got_picture, len;
av_init_packet(&pkt);
pkt.size = size;
pkt.data = buffer;
while(pkt.size > 0) {
    if((len = avcodec_decode_video2(context, frame, &got_picture, &pkt)) < 0) {
        break;
    }

    if(got_picture) {
        // Do something with the picture...
    }

    avPkt.size -= len;
    avPkt.data += len;
}

However, whenever I call avcodec_decode_video2 it prints the following error in the console:

[...]    
[h264 @ 000000000126db40] AVC: The buffer size 210 is too short to read the nal length size 0 at the offset 210.
[h264 @ 000000000126db40] AVC: The buffer size 283997 is too short to read the nal length size 0 at the offset 283997.
[h264 @ 000000000126db40] AVC: The buffer size 17137 is too short to read the nal length size 0 at the offset 17137.
[...]

What am I missing? I tried searching for threads concerning a similar issue but nothing came up. Or is there a way I can debug the error to get more information about it?


First off, I assume you allocate the output frame correctly.

And @AntonAngelov, I am using 11.04. Do you know what the error is supposed to say? What buffer is the error talking about?

I just looked at 11.04's source (in /avcodec/h264.c) but I didn't see where this error is generated, while in older versions it is present.

It seems the error says that the size of the NALU packets, which you send to the decoder is 0 .

My guess is that you have to get the SPS and PPS headers somehow from LIVE555 and provide them to the decoder via it's extradata (also you have to set extradata_size ), before you call avcodec_open2().

Another idea is to just dump all the packets you receive into a single .h264 file. Then use a software for parsing h264 bitstreams (see here for example). Also try to play it with avplay or VLC to see if the bitstream is correct.

Edit: Here a similar question is answered.


AVPacket pkt;
int got_picture, len;
av_init_packet(&pkt);
pkt.size = size;
pkt.data = buffer;
while(pkt.size > 0) {
    if((len = avcodec_decode_video2(context, frame, &got_picture, &pkt)) < 0) {

Your code worries me, since you're manually initializing a AVPacket, but you're not telling us where buffer/size come from. I'm almost certain, given the error message, that you're reading raw data from a file, socket or something like that, as if it were a raw annexb stream.

FFmpeg (or Libav, for that matter) does not accept such data as input in its H.264 decoder. To solve this, use an AVParser, as explained previously in this post.

链接地址: http://www.djcxy.com/p/67556.html

上一篇: C ++ h264 ffmpeg / libav编码/解码(无损)问题

下一篇: 解码H264帧错误