#pragma once

/**
* @author Tophead, Jan Drabner
*/

#ifdef FFMPEG_DLL_EXPORTS
#define FFMPEG_DLL_API __declspec(dllexport)
#else
#define FFMPEG_DLL_API __declspec(dllimport)
#endif

//#include "libavcodec/avcodec.h"
//#include "C:/ffmpeg/include/stdint.h"
//#include "C:/ffmpeg/include/inttypes.h"

#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif

#define inline _inline
#define snprintf _snprintf

extern "C" {
#include "libavformat/avformat.h"
#include "libswscale/swscale.h"
}
#include <memory.h>
#include <windows.h>
#include <queue>
#include "FramesCollection.h"

struct Picture 
{
    int frame;
    void *pic;
    int pic_size;
    int vStart;
    int soundOffset;
    int lStart;
    int lEnd;
    int vEnd;
	bool bFree;

};

static void log_callback(void *ptr, int level, const char *fmt, va_list vargs);

// Diese Klasse wird aus ffmpeg_dll.dll exportiert.
class FFMPEG_DLL_API Cffmpeg_dll {
public:
	// constructor
	Cffmpeg_dll(void);
	// destructor
	~Cffmpeg_dll(void);
	// here comes the data

    /* encoder thread */
    int QueuePicture( Picture& pic );
    HANDLE thread;
    CRITICAL_SECTION queueLock;
    std::queue< Picture >* pictureQueue;

	/**************************************************************/
	/* audio data */

	bool skippedFirst; // Skip the first frame because of audio noise

	float t, tincr, tincr2;
	int16_t *samples;
	uint8_t *audio_outbuf;
	int audio_outbuf_size;
	int audio_input_frame_size;
	char soundInputPath[1024];
	FILE *fp_sound_input;

	enum AVSampleFormat sample_fmt;
	int audio_bit_rate;
	int sample_rate;
	int channels;

	/**************************************************************/
	/* video data */

	AVFrame *picture, *tmp_picture;
	uint8_t *video_outbuf;
	int frame_count, video_outbuf_size;
	int srcWidth, srcHeight;
	int dstWidth, dstHeight;
	struct SwsContext *img_convert_ctx;
	AVOutputFormat *fmt;
	AVFormatContext *oc;
	AVStream *audio_st, *video_st;
	char videoOutputPath[1024];
	double audio_pts, video_pts;
	int video_bit_rate;

    // h264 specific parameters
    int qmin, qmax;
    int me_method;
    int me_subpel_quality;
    float i_quant_factor;
    float qcompress;
    int max_qdiff;

	int fps;
	char audioCodec[512];
	CodecID audio_codec_id;
	void * audio_codec_ptr;
	char videoCodec[512];
	CodecID video_codec_id;
	bool video_finished;

    int threads;


	/**************************************************************/
	/* intro and outro data */
	FramesCollection* introVideo;
	FramesCollection* outroVideo;
};


extern "C" 
{
FFMPEG_DLL_API Cffmpeg_dll * Create_Cffmpeg_dll();

FFMPEG_DLL_API int InitVideo_ffmpeg_dll(Cffmpeg_dll * ptr, 
										const char * filename, 
										const char * soundname,
										const char * logfilePath);

FFMPEG_DLL_API int EncodePicture_ffmpeg_dll(Cffmpeg_dll * ptr, 
											int frame, 
											void * pic, int pic_size, 
											int vStart = 0, 
											int soundOffset = 0, 
											int lStart = 0, int lEnd = 0, 
											int vEnd = 0 );

FFMPEG_DLL_API int CloseVideo_ffmpeg_dll(Cffmpeg_dll * ptr);

FFMPEG_DLL_API int Delete_Cffmpeg_dll(Cffmpeg_dll * ptr);
}

