Search code examples
c#ffmpeg

How to set AV_FRAME_FLAG_DISCARD with avcodec_open2


I want to discard corrupted H.264 frames, but I've been unable to achieve this. I've tried setting flags after creating the context, as well as using options, but neither method has worked, and I still see the corrupted frames. examples online demonstrate this with a format context, but in my case, I'm feeding a bytestream directly from a socket.

Options version

ffmpeg.av_dict_set(&options, "fflags", "discardcorrupt", 0);
var ret = ffmpeg.avcodec_open2(_CodecContext, _decodingCodec, &options);
ffmpeg.av_dict_free(&options);

Flags version

var ret = ffmpeg.avcodec_open2(_CodecContext, _decodingCodec, null);
_CodecContext->flags |= ffmpeg.AV_FRAME_FLAG_DISCARD;

I want to avoid errors like:

[h264 @ 000001fb6302dfc0] decode_slice_header error
[h264 @ 000001fb6302dfc0] decode_slice_header error
[H264 Decoder @ 000001fb70778740] Broken frame packetizing
[h264 @ 000001fb6302dfc0] illegal short term buffer state detected

Either avcodec_send_packet or avcodec_receive_frame return an error, return value is always 0. Currently i cant catch this and i dont want using a global logging checking because it could be that i have 16 instances for my decoder. (Cctv grid). I using ffmpeg.autogen (c#)


Solution

  • discardcorrupt applies to corrupt packets in container, not decoding errors.

    To detect decoding corruption, test for decode_error_flags or AVFrame->flags & AV_FRAME_FLAG_CORRUPT in the returned AVFrame.