#include <assert.h>
extern "C" {
#include <libavcodec/avcodec.h>
#include <libavcodec/packet.h>
#include <libavformat/avformat.h>
#include <libavutil/avutil.h>
#include <libavutil/frame.h>
}

static void check(int64_t ret, int64_t expected) {
    assert(ret == expected);
}

int main() {
    AVFormatContext* ic = NULL;
    check(avformat_open_input(&ic, "D:/videmo360/giftschrank/chromium_testdata/blackwhite_yuv444p.ogv", NULL, NULL), 0);
    check(avformat_find_stream_info(ic, NULL), 0);
    check(ic->nb_streams, 1);
    AVCodecContext* avctx = avcodec_alloc_context3(avcodec_find_decoder(ic->streams[0]->codecpar->codec_id));
    assert(avctx);
    avctx->thread_count = 0;
    check(avcodec_parameters_to_context(avctx, ic->streams[0]->codecpar), 0);
    check(avcodec_open2(avctx, NULL, NULL), 0);

    AVPacket *pkt = av_packet_alloc(), *pkt2 = av_packet_alloc();
    check(av_read_frame(ic, pkt), 0);
    check(av_read_frame(ic, pkt2), AVERROR_EOF);

    AVFrame* frame = av_frame_alloc();

    avcodec_flush_buffers(avctx);
    check(avcodec_send_packet(avctx, pkt), 0);
    check(avcodec_send_packet(avctx, nullptr), 0);
    check(avcodec_receive_frame(avctx, frame), 0);
    check(avcodec_receive_frame(avctx, frame), AVERROR_EOF);

    avcodec_flush_buffers(avctx);
    check(avcodec_send_packet(avctx, pkt), 0);
    check(avcodec_send_packet(avctx, nullptr), 0);
    check(avcodec_receive_frame(avctx, frame), 0);
    check(avcodec_receive_frame(avctx, frame), AVERROR_EOF);
}