3

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?

chrisp
  • 569
  • 4
  • 24
  • What is the return value of avcodec_decode_video2()? – Anton Angelov Oct 02 '15 at 13:50
  • The return value equals the amount of bytes left. Also, the got_picture pointer is nonzero - therefore, I assume the decompression was successful. However, I always receive this error. – chrisp Oct 02 '15 at 21:25
  • I don't even know what the error message is trying to say. What buffer is the error talking about? – chrisp Oct 02 '15 at 21:26
  • Where do you get the nal packet (h264 frame) from? Do you use libavformat to read it from container? Also do you use actual version of libav? – Anton Angelov Oct 03 '15 at 06:14
  • I'm using LIVE555 to receive the H264 frame and libav to decode it afterwards. It's a live stream. – chrisp Oct 03 '15 at 11:09
  • And @AntonAngelov, I am using 11.04. Do you know what the error is supposed to say? What buffer is the error talking about? – chrisp Oct 03 '15 at 11:10

2 Answers2

1

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.

Community
  • 1
  • 1
Anton Angelov
  • 1,223
  • 7
  • 19
  • Thank you for your response. That narrows the error down for me. I will look into the decoding of the PPS and SPS units once again. At the moment I am using LIVE555's ´fmtp_spropparametersets´ function to receive the units. Maybe there's a problem when I attempt to decode the parameter set. Also, I will recheck the libav version I have in use. I will get back to you regarding all that tomorrow (not at home at the moment). Your second idea is good, however, I already did that in the past using ffplay. So the bitstream seems fine. – chrisp Oct 03 '15 at 14:53
  • @Chris. Did you find out what the problem was? I'm in the same situation Live555 + Libav. It is decoding fine, but it outputs the buffer size error constantly. – Sergio Basurco Nov 11 '15 at 15:46
  • 1
    @chuckleplant I re-downloaded libav + worked on fixing the SPS and PPS initialisation. – chrisp Nov 18 '15 at 02:29
  • @Chris. That's what I tried to do, I receive SPS PPS via Live and set it on Libav's `pCodecCtx->extradata`. That should be it but I still get the error output. Thanks! – Sergio Basurco Nov 18 '15 at 12:08
  • 1
    @chuckleplant Try to debug it. Search in the original h264 bitstream that is being sent over the network for the SPS and PPS values using a hex editor. Then encode it using base64 and compare it with what you have set in your client application. – chrisp Nov 18 '15 at 13:36
  • +1 For redownloading libav. The `buffer size too short` output was a bug, for me this was in version 11.4 of Libav. Nothing to do with SPS / PPS – Sergio Basurco Nov 25 '15 at 11:06
1
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.

Community
  • 1
  • 1
Ronald S. Bultje
  • 10,828
  • 26
  • 47
  • Thanks for your answer. However, this is not the case. The buffer includes one frame delivered by LIVE555. The problem seems to have something to do with the SPS and PPS values. I will post an update to my original thread in a moment. :) – chrisp Oct 12 '15 at 18:03