﻿#ifndef CVIDEODECODER_H
#define CVIDEODECODER_H
#include "cdemux.h"
#include "safequeue.h"

class CPlayer;
class CVideoDecoder
{
public:
    CVideoDecoder(CPlayer* player);

    void Close();

    void SetParameter(int width, int height, AVPixelFormat swsfmt);

    bool Start(CDemux* demux);

    void SendPacket(AVPacket* pkt);

private:
    void VideoDecodeThread();

private:
    AVCodecContext* video_codec_ctx = nullptr;
    SwsContext* sws_ctx = nullptr;

    AVFrame* src_frame = nullptr;
    AVFrame* dst_frame = nullptr;
    uint8_t* dst_buffer = nullptr;

    SafeQueue<AVPacket*> video_pkt_queue;

    int m_swsWidth = 0;
    int m_swsHeight = 0;
    AVPixelFormat m_swsFmt = AV_PIX_FMT_NONE;

    double m_timebase = 0.0f;
    double m_duration = 0.0f;
    int m_rate = 0;

    bool m_bRun = false;
    CPlayer* m_player = nullptr;

    std::thread m_video_thread;
};

#endif // CVIDEODECODER_H
