1
votes

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

1
.h264 is supposed to be a raw stream and contains no audio. What this is, is a MPEG-PS with a single stream. Remux to a raw stream: ffmpeg -i test.h264 -c copy raw.h264 and use that. - Gyan
I already did that and it worked. The problem is that I can not do the same with live stream. In live stream I get the bytes exactly as they are stored in test.h264, and I have those problems to decode. - Hudson Cavazin
So, you'll need to demux and read packets using mpegps - Gyan
Or, you can wrap your live stream in custom AVIOContext, similar to this example. - Alex Cohn
@Alex Cohn, it worked perfectly. Thank you so much. - Hudson Cavazin

1 Answers

2
votes

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)