Search code examples
ffmpeglibavcodeclibav

ffmpeg decode raw h264 distorted or gray image


I need to decode an H264 stream that comes from a live DVR camera. To facilitate the example, I stored the RAW stream from the DVR camera in the following file (test.h264): http://f.zins.com.br/test.h264

To decode the live stream, I followed the following ffmpeg example: https://github.com/FFmpeg/FFmpeg/blob/master/doc/examples/decode_video.c

If I open the .h264 test with VLC, the images look perfect. If you decode the .h264 test with ffmpeg using avformat_open_input and avformat_find_stream_info, the images also look perfect.

But if I decode using the https://github.com/FFmpeg/FFmpeg/blob/master/doc/examples/decode_video.c example, the images are all distorted. I think this happens because along with the H264 stream can have audio together.

Enabling the debugging of ffmpeg, it shows a lot of the following errors:

[h264 @ 092a9b00] Invalid NAL unit 0, skipping.
[h264 @ 092a9b00] Invalid NAL unit 0, skipping.
[h264 @ 092a9b00] Invalid NAL unit 0, skipping.
[h264 @ 092a9b00] error while decoding MB 16 1, bytestream -28
[h264 @ 092a9b00] Invalid NAL unit 8, skipping.
[h264 @ 092a9b00] Invalid NAL unit 8, skipping.
[h264 @ 092a9b00] Invalid NAL unit 8, skipping.

Is there a way for me to only filter the video and ignore the audio from a live stream? Otherwise, is there any solution to decode the test.h264 using the decode_video.c example without distorting the frames?

The distorted frame sometimes looks like the image below, and sometimes it gets almost all gray. distorted frame


Solution

  • The stream has mpeg wrapper around raw h264 packets, and we need to demux them first. If you cannot provide a URL with some protocol supported by ffmpeg (e.g. udp://) like, you should build custom AVIOContext for your live stream and pass it to

    avformat_open_input(&fmt_ctx, NULL, NULL, NULL)
    

    similar to this example.

    Now you can start the usual demuxer loop with

    av_read_frame(fmt_ctx, &pkt)