
#include <libavcodec/avcodec.h>
#include <libavformat/avformat.h>

#include <stdio.h>
#include <assert.h>

void printAndExit(const char* msg) {
    printf("%s\n",msg);
    exit(1);
}

void exitIfError(int av_error) {
    if (av_error < 0)
    {
        char buffer[AV_ERROR_MAX_STRING_SIZE] = {0};
        av_strerror(av_error,buffer,sizeof(buffer));
        printAndExit(buffer);
    }
}


int main()
{
    AVFormatContext* m_formatCtx = NULL;
    unsigned int m_videoStreamIdx = -1;
    AVCodec* m_codec = NULL;
    AVCodecContext* m_codecCtx = NULL;
    AVFrame* m_lastFrame = NULL;

    const char* filename = "cant_read_2nd_frame.flv";
//    av_log_set_level(AV_LOG_TRACE);

    av_register_all();

    exitIfError( avformat_open_input(&m_formatCtx,filename,NULL,NULL) );

    // Find stream info
    exitIfError( avformat_find_stream_info(m_formatCtx,NULL));

    // Find the first video stream
    m_videoStreamIdx = 0;
    for (; m_videoStreamIdx < m_formatCtx->nb_streams; ++m_videoStreamIdx)
        if (m_formatCtx->streams[m_videoStreamIdx]->codec->codec_type == AVMEDIA_TYPE_VIDEO)
            break;

    if (m_videoStreamIdx == m_formatCtx->nb_streams) printAndExit("Can't find video stream!");

    m_codec = avcodec_find_decoder(m_formatCtx->streams[m_videoStreamIdx]->codec->codec_id);
    if (m_codec == NULL) printAndExit("Unsupported codec!");

    m_codecCtx = avcodec_alloc_context3(m_codec);
    if (m_codecCtx == NULL) printAndExit("Error while creating codec context!");

    exitIfError( avcodec_copy_context(m_codecCtx, m_formatCtx->streams[m_videoStreamIdx]->codec) );

    m_codecCtx->refcounted_frames = 0; //the decoded frames must not be freed by the caller and are only valid until the next decode call.

    exitIfError( avcodec_open2(m_codecCtx,m_codec,NULL) );

    m_lastFrame = av_frame_alloc();
    if (m_lastFrame == NULL) printAndExit("Couldn't alloc frame!");

    AVPacket packet;
    int isFrameFinished = 0;

    // Read 1st frame
    exitIfError( av_read_frame(m_formatCtx, &packet) );
    assert(packet.stream_index == (signed)m_videoStreamIdx);
    exitIfError( avcodec_decode_video2(m_codecCtx,m_lastFrame,&isFrameFinished,&packet) );
    assert(isFrameFinished == 1);
    av_free_packet(&packet);

    // Read 2nd frame
    exitIfError( av_read_frame(m_formatCtx, &packet) );
    assert(packet.stream_index == (signed)m_videoStreamIdx);
    exitIfError( avcodec_decode_video2(m_codecCtx,m_lastFrame,&isFrameFinished,&packet) );
    assert(isFrameFinished == 0);  // No frame decoded this time
    av_free_packet(&packet);

    // Seek to the beginning
    avcodec_flush_buffers(m_codecCtx);
    exitIfError(av_seek_frame(m_formatCtx,m_videoStreamIdx,0,AVSEEK_FLAG_BACKWARD));

    // Read 1st frame
    exitIfError( av_read_frame(m_formatCtx, &packet) );
    assert(packet.stream_index == (signed)m_videoStreamIdx);
    exitIfError( avcodec_decode_video2(m_codecCtx,m_lastFrame,&isFrameFinished,&packet) );
    assert(isFrameFinished == 1);
    av_free_packet(&packet);

    // Read 2nd frame
    exitIfError( av_read_frame(m_formatCtx, &packet) );
    assert(packet.stream_index == (signed)m_videoStreamIdx);
    exitIfError( avcodec_decode_video2(m_codecCtx,m_lastFrame,&isFrameFinished,&packet) );
    assert(isFrameFinished == 1);  // Success
    av_free_packet(&packet);


    printf("Done.\n");
    return 0;
}

