/*
 * AviFile.cpp
 *
 *  Created on: 08/mar/2022
 *      Author: chessaa
 */

#include "AviFile.h"

#include <windows.h>
#include <mmsystem.h>
#include <vfw.h>

#define USE_QT

#ifdef USE_QT

#include <QDebug>
//static int cmdio_printf(const char* const fmt, va_list args)
#define cmdio_printf__(fmtformat,...) qDebug()<<"AVI:"<<QString::asprintf(fmtformat,##__VA_ARGS__)

#define cmdio_printf(L_, F_, ...) qDebug()<<"AVI:"<<QString::asprintf(F_,##__VA_ARGS__)
#if 0
static int cmdio_pdebug(const char* const /*fmt*/, ...)
{
    return 0;
}

static int cmdio_pinfo2(const char* const /*fmt*/, ...)
{
    return 0;
}

static int cmdio_pinfo1(const char* const /*fmt*/, ...)
{
    return 0;
}
#endif


#define cmdio_pinfo cmdio_printf  /*static int cmdio_pinfo(const char* const fmt, ...);*/
#define cmdio_psuccess cmdio_printf__  /*static int cmdio_psuccess(const char* const fmt, ...);*/
#define cmdio_pwarning cmdio_printf__  /*static int cmdio_pwarning(const char* const fmt, ...);*/
#define cmdio_perror cmdio_printf__  /*static int cmdio_perror(const char* const fmt, ...);*/
#define cmdio_pfail cmdio_printf__  /*static int cmdio_pfail(const char* const fmt, ...);*/
#define cmdio_oserror cmdio_printf__  /*static int cmdio_perror(const char* const fmt, ...);*/

#else
#include "gtl_cmdio.h"
#endif
#include <stdint.h>
//#include <atomic>

//Audio capture
#define BITS_PER_SAMPLE	8
#define BYTES_PER_SAMPLE	(BITS_PER_SAMPLE/8)

const int BUFFER_DURATION = 1;
const int CHANNEL_COUNT = 1;
const int SAMPLE_RATE = 22050; //48000; //44100;
const int DATA_SIZE = BYTES_PER_SAMPLE* SAMPLE_RATE * BUFFER_DURATION * CHANNEL_COUNT;
//const int BUFFER_SIZE = 10;
const int NUMBER_OF_BUFFERS = 8;

const unsigned int WAVE_MAPPED_DEFAULT_COMMUNICATION_DEVICE = 0x0010;

#define AVI_LOG cmdio_debug

class AudioCaptureImplementation
{
public:
    void(*callback)(void* cookie, WAVEHDR data);
    void* callback_cookie;
    bool running;

    MMRESULT res;
    HWAVEIN micHandle;
    WAVEFORMATEX format;
    WAVEHDR buffers[32];
    unsigned int buffers_data[32][DATA_SIZE];

    volatile uint64_t buffers_queue;

    bool addBuffer(WAVEHDR *buffer);

    static void CALLBACK waveInProc(
       HWAVEIN   hwi,
       UINT      uMsg,
       DWORD_PTR dwInstance,
       DWORD_PTR dwParam1,
       DWORD_PTR dwParam2);

    void Update();

    bool StartCapture();
    void StopCapture();
    void closeCapture();

    CRITICAL_SECTION myCs;

    unsigned int time;

    AudioCaptureImplementation():
    	callback(0),
    	callback_cookie(0),
    	running(false),
    	res(0),
    	micHandle(0),
    	buffers_queue(0),
    	time(0)
    {
    	memset(&format, 0, sizeof format);
    	memset(buffers, 0, sizeof buffers);

    	InitializeCriticalSection(&myCs);
    }

    virtual ~AudioCaptureImplementation()
    {
    }

    void lock()
    {
    	EnterCriticalSection(&myCs);
    }
    void unlock()
    {
    	LeaveCriticalSection(&myCs);
    }

    int flush(bool disregard=false);
};

void CALLBACK AudioCaptureImplementation::waveInProc(
   HWAVEIN   hwi,
   UINT      uMsg,
   DWORD_PTR dwInstance,
   DWORD_PTR dwParam1,
   DWORD_PTR dwParam2)
{
	if (uMsg == WIM_DATA)
	{
		WAVEHDR* hdr = (WAVEHDR*)dwParam1;
		int id = hdr->dwUser;

		AudioCaptureImplementation& instance=*reinterpret_cast<AudioCaptureImplementation*>(dwInstance);


		instance.lock();
			instance.buffers_queue<<=8;
			instance.buffers_queue|=(id+1);
#if 0
			WAVEHDR& buffer=instance.buffers[id];

			//cmdio_psuccess("Audio Buffer: %d 0x%X %u samples", id, instance.buffers_queue, buffer.dwBytesRecorded);
#endif
		instance.unlock();

	}
}

bool AudioCaptureImplementation::addBuffer(WAVEHDR* buffer)
{
	res = waveInPrepareHeader(micHandle, buffer, sizeof(WAVEHDR));
	if (res != MMSYSERR_NOERROR)
	{
		cmdio_pfail("waveInPrepareHeader() failed %u", res);
		return false;
	}

	res = waveInAddBuffer(micHandle, buffer, sizeof(WAVEHDR));
	if (res != MMSYSERR_NOERROR)
	{
		cmdio_pfail("waveInAddBuffer() failed %u", res);
		return false;
	}
	return true;
}

bool AudioCaptureImplementation::StartCapture()
{

	if (micHandle==0)
	{
		memset(&format, 0, sizeof format);
		format.nChannels = CHANNEL_COUNT;
		format.cbSize = 0;
		format.nSamplesPerSec = SAMPLE_RATE;
		format.wFormatTag = WAVE_FORMAT_PCM;
		format.wBitsPerSample = BITS_PER_SAMPLE;
		format.nBlockAlign = (CHANNEL_COUNT * format.wBitsPerSample) / 8;
		format.nAvgBytesPerSec = SAMPLE_RATE * format.nBlockAlign;

		res=waveInOpen(&micHandle, WAVE_MAPPER, &format, (DWORD_PTR)waveInProc, (DWORD_PTR)this, CALLBACK_FUNCTION|WAVE_MAPPED_DEFAULT_COMMUNICATION_DEVICE);
		if (res != MMSYSERR_NOERROR)
		{
			cmdio_perror("waveInOpen() failed");
			micHandle=0;
			return false;
		 }
		 for (int i = 0; i < NUMBER_OF_BUFFERS; i++)
			{
				WAVEHDR* header=&buffers[i]; //new WAVEHDR;
				ZeroMemory(header, sizeof(*header));


				header->lpData = (char*)buffers_data[i]; //(LPSTR)new short int[DATA_SIZE];
				header->dwBufferLength = DATA_SIZE;
				header->dwBytesRecorded = 0;
				header->dwUser = 0;
				header->dwFlags = 0;
				header->dwLoops = 0;
				header->dwUser = i;

				//buffers[i] = header;

				bool ok=addBuffer(&buffers[i]);
				if (ok)
					cmdio_printf(AVI_LOG, "Audio: adding buffer %d 0x%X", i, header->lpData);
			}
	 }
	 waveInStart(micHandle);
	 return true;
}

void AudioCaptureImplementation::StopCapture()
{
	if (micHandle)
		waveInStop(micHandle);
}

void AudioCaptureImplementation::closeCapture()
{
	if (micHandle)
	{
		waveInClose(micHandle);
	}
}

int AudioCaptureImplementation::flush(bool disregard)
{
	int one_sec=0;
	while(buffers_queue)
	{
		lock();
		unsigned int id=buffers_queue & 0x0FF;
		buffers_queue>>=8;
		unlock();

		if (id==0)
			continue;

		WAVEHDR& buffer=buffers[id-1];

		cmdio_printf(AVI_LOG, "Audio::flush(): %d 0x%p  %u", id, buffer.lpData, buffer.dwBytesRecorded);

	    if (!disregard)
	    {
	    	if (callback)
	    		callback(callback_cookie, buffer);
	    }
		addBuffer(&buffer);
		++one_sec;
	}
	return one_sec;
}

class AviFile::Implementation
{
public:
	struct stream_t
	{
		bool ready;

		PAVISTREAM ps;
		PAVISTREAM psCompressed;
		PAVISTREAM psWrite;
		AVISTREAMINFO strhdr;
		AVICOMPRESSOPTIONS opts;
		BITMAPINFOHEADER bi;
	    WAVEFORMATEX format;

		unsigned long time;


		void close()
		{
			if (ready)
			{
				ready=false;
				if (ps)
					AVIStreamClose(ps);
				if (psCompressed)
					AVIStreamClose(psCompressed);
			}
		}
	};

	PAVIFILE avif;
	char fname[512];

	HRESULT hr;

	stream_t videoStream;
	stream_t audioStream;

	AudioCaptureImplementation audioCapturer;

    uint32_t tmp_img[512][640];

	static DWORD myFOURCC()
	{
		//return mmioFOURCC('M', 'P', '4', 'V'); //('M', 'S', 'V', 'C');
		return mmioFOURCC('M','S','V','C');
	}

	Implementation():
		avif(0),
		hr(0)
	{
		memset(&videoStream, 0, sizeof videoStream);
		memset(&audioStream, 0, sizeof audioStream);
	}

	bool createVideoStream(int rate, int rectwidth, int rectheight, int key, int quality)
	{
		stream_t& s=videoStream;

		memset(&s, 0, sizeof s);

		if (!avif)
			return false;

		DWORD buffersize=rectwidth*rectheight*sizeof(uint32_t);

		s.strhdr.fccType                = streamtypeVIDEO;// stream type
		s.strhdr.fccHandler             = myFOURCC(); //mmioFOURCC('M','S','V','C'); // Microsoft video 1
		//strhdr.fccHandler             = mmioFOURCC('I','V','5','0'); // Intel video 5.0
		//strhdr.dwFlags                = AVISTREAMINFO_DISABLED;
		//strhdr.dwCaps                 =
		//strhdr.wPriority              =
		//strhdr.wLanguage              =
		s.strhdr.dwScale                = 1;
		s.strhdr.dwRate                 = rate;               // rate fps
		//strhdr.dwStart                =
		//strhdr.dwLength               =
		//strhdr.dwInitialFrames        =
		s.strhdr.dwSuggestedBufferSize  = buffersize;
		s.strhdr.dwQuality              = -1; // use the default
        //strcpy(s.strhdr.szName, "video");

		//strhdr.dwSampleSize           =
        SetRect(&s.strhdr.rcFrame, 0, 0,              // rectangle for stream
            (int) rectwidth,
            (int) rectheight);

        hr = AVIFileCreateStream(avif,             // file pointer
                                 &s.ps,                // returned stream pointer
                                 &s.strhdr);          // stream header
        if (hr != AVIERR_OK)
        {
        	cmdio_oserror("Video::AVIFileCreateStream()");
            return false;
        }

		s.opts.fccType = streamtypeVIDEO;
		s.opts.fccHandler             = myFOURCC();
		//opts.fccHandler  = 0;
		//opts.fccHandler            = mmioFOURCC('D','I','B',' '); // Uncompressed
		//opts.fccHandler             = mmioFOURCC('C','V','I','D'); // Cinpak
		//opts.fccHandler             = mmioFOURCC('I','V','3','2'); // Intel video 3.2
		//opts.fccHandler             = mmioFOURCC('M','S','V','C'); // Microsoft video 1
		//opts.fccHandler             = mmioFOURCC('I','V','5','0'); // Intel video 5.0
		s.opts.dwKeyFrameEvery = key; //-1; //50;
		s.opts.dwQuality=quality; //-1;
		//opts.dwBytesPerSecond
		s.opts.dwFlags                = AVICOMPRESSF_KEYFRAMES|AVICOMPRESSF_VALID;
		//opts.lpFormat
		//opts.cbFormat
		//opts.lpParms
		//opts.cbParms
		//opts.dwInterleaveEvery
		/* display the compression options dialog box if specified compressor is unknown */

        hr = AVIMakeCompressedStream(&s.psCompressed, s.ps, &s.opts, 0);
        if (hr != AVIERR_OK)
        {
        	cmdio_oserror("Video::AVIMakeCompressedStream()");
        	return false;
        }

        s.bi.biHeight=rectheight;
        s.bi.biWidth=rectwidth;
        s.bi.biCompression=0;
        s.bi.biPlanes=1;
        s.bi.biBitCount=32;
        s.bi.biSizeImage=s.bi.biHeight*s.bi.biWidth*4;
        s.bi.biSize=sizeof s.bi;

        hr = AVIStreamSetFormat(s.psCompressed, 0,
        		&s.bi, s.bi.biSize + s.bi.biClrUsed * sizeof(RGBQUAD));

        if (hr != AVIERR_OK)
        {
        	cmdio_oserror("Video::AVIStreamSetFormat()");
        	return false;
        }

        s.ready=true;
        return true;
	}

	bool addVideoFrame(const void* data)
	{
		stream_t& s=videoStream;

		if (!s.ready)
		{
			cmdio_pwarning("addVideoFrame(): stream not ready");
			return false;
		}

		const DWORD ImageSize=s.bi.biSizeImage;

		hr = AVIStreamWrite(s.psCompressed, // stream pointer
			s.time, // time of this frame
			1, // number to write
			const_cast<void*>(data),
			ImageSize, // lpbi->biSizeImage, // size of this frame
			s.time==0 ? AVIIF_KEYFRAME : 0, // flags....
			NULL,
			NULL);
		++s.time;
		if (hr != AVIERR_OK)
		{
			//CString strMsg;
			//strMsg.Format("Error: AVIStreamWrite, error %d",hr);
			cmdio_oserror("addVideoFrame()=%d",hr);
			return false;
		}
		return true;
	}


	bool createAudioStream(int rate, int ch, int bytes_per_sample)
	{

		stream_t& s=audioStream;

		memset(&s, 0, sizeof s);

		if (!avif)
			return false;

		s.format.nChannels=ch;
		s.format.nSamplesPerSec=rate;
		s.format.wFormatTag=WAVE_FORMAT_PCM;
		s.format.wBitsPerSample=bytes_per_sample*8;
		s.format.nBlockAlign=(ch*s.format.wBitsPerSample)/8;
		s.format.nAvgBytesPerSec=ch*rate*s.format.nBlockAlign;

		s.strhdr.fccType                = streamtypeAUDIO;// stream type
		s.strhdr.dwScale                = 1;
		s.strhdr.dwRate                 = rate;               // rate fps
		//strhdr.dwStart                =
		//strhdr.dwLength               =
		//strhdr.dwInitialFrames        =
		s.strhdr.dwSuggestedBufferSize  = 0; //rate;
		s.strhdr.dwQuality              = -1; // use the default
		s.strhdr.rcFrame=videoStream.strhdr.rcFrame;

        //strcpy(s.strhdr.szName, "audio");

        hr = AVIFileCreateStream(avif,             // file pointer
                                 &s.ps,                // returned stream pointer
                                 &s.strhdr);          // stream header
        if (hr != AVIERR_OK)
        {
        	cmdio_oserror("Audio::AVIFileCreateStream()");
            return false;
        }

        s.psWrite=s.ps;

		s.opts.fccType = streamtypeAUDIO;
		s.opts.dwKeyFrameEvery = 1;
		s.opts.dwQuality=-1;
		//opts.dwBytesPerSecond
		s.opts.dwFlags=0; //                = AVICOMPRESSF_KEYFRAMES|AVICOMPRESSF_VALID;
		s.opts.lpFormat=&s.format;
		s.opts.cbFormat=sizeof s.format;

		//opts.lpFormat
		//opts.cbFormat
		//opts.lpParms
		//opts.cbParms
		//opts.dwInterleaveEvery
		/* display the compression options dialog box if specified compressor is unknown */
#if 0
        hr = AVIMakeCompressedStream(&s.psCompressed, s.ps, &s.opts, 0);
        if (hr != AVIERR_OK)
        {
        	cmdio_oserror("Audio::AVIMakeCompressedStream()");
        	return false;
        }
        s.psWrite=s.psCompressed;
#endif

        hr = AVIStreamSetFormat(s.psWrite, 0, &s.format, sizeof s.format);
        if (hr != AVIERR_OK)
        {
        	cmdio_oserror("Audio::AVIStreamSetFormat()");
        	return false;
        }

        s.ready=true;
        audioCapturer.callback=audio_callback;
        audioCapturer.callback_cookie=this;
        return true;
	}

	bool addAudioSamples(int size, const void* data)
	{
		stream_t& s=audioStream;
		int numsmaples=size/(s.format.wBitsPerSample/8);
		if (!s.ready)
		{
			cmdio_pwarning("addAudioSamples(): stream not ready");
			return false;
		}

		if (!s.psWrite)
		{
			cmdio_pfail("addAudioSamples(): no write stream");
			return false;
		}

		hr = AVIStreamWrite(s.psWrite, // stream pointer
			s.time, // time of this frame
			numsmaples, // number to write
			const_cast<void*>(data),
			size, // lpbi->biSizeImage, // size of this frame
			0, 0, 0);

		s.time+=numsmaples;
		if (hr != AVIERR_OK)
		{
			//CString strMsg;
			//strMsg.Format("Error: AVIStreamWrite, error %d",hr);
			cmdio_oserror("addAudioSamples()=%d",hr);
			return false;
		}
		return true;
	}

	static void audio_callback(void* cookie, WAVEHDR hdr)
	{
	    cmdio_printf(AVI_LOG, "Audio::audio_callback(): %d", hdr.dwBytesRecorded);

		AviFile::Implementation* instance=reinterpret_cast<AviFile::Implementation*>(cookie);
		instance->addAudioSamples(hdr.dwBytesRecorded, hdr.lpData);
	}
};

AviFile::AviFile():
	p_(*new Implementation)
{
	AVIFileInit();
}

AviFile::~AviFile()
{
	close();
	delete &p_;
}

bool AviFile::open(const char* name, bool add_date)
{
	if (name)
	{
		strncpy(p_.fname, name, sizeof p_.fname);
	}
	else
		strcpy(p_.fname, "avi_file.avi");

	if (p_.avif)
		close();


    HRESULT hr = AVIFileOpenA(&p_.avif,           // returned file pointer
                   p_.fname,                  // file name
                   OF_WRITE | OF_CREATE,      // mode to open file with
                   0);                     // use handler determined
                                              // from file extension....
    if (hr != AVIERR_OK)
    {
    	cmdio_oserror("AviFile::open(%s)", p_.fname);
        return false;
    }

	cmdio_psuccess("AviFile::open(%s)", p_.fname);
    return true;
}

bool AviFile::isOpen() const
{
	return p_.avif!=0;
}

bool AviFile::close()
{
	p_.audioCapturer.StopCapture();
	p_.videoStream.close();
	p_.audioStream.close();

	if (p_.avif)
	{
		AVIFileClose(p_.avif);
		p_.avif=0;
	}
	return true;
}


bool AviFile::openVideoStream(int rate,int w, int h, int key, int quality)
{
	return p_.createVideoStream(rate, w, h,key, quality);
}

bool AviFile::addVideoFrame(const void* data, bool invert)
{
    if (invert)
    {
        const uint32_t* p=reinterpret_cast<const uint32_t*>(data);
        uint32_t* d=reinterpret_cast<uint32_t*>(p_.tmp_img[511]);

        for(int y=0; y<512; ++y)
        {
            memcpy(d, p, 640*4);
            p+=640;
            d-=640;
        }
        return p_.addVideoFrame(p_.tmp_img);

    }
	return p_.addVideoFrame(data);
}

bool AviFile::openAudioStream(int rate, int ch, int bytes_per_sample)
{
	return p_.createAudioStream(rate, ch, bytes_per_sample);
}
bool AviFile::openAudioStream()
{
	return p_.createAudioStream(SAMPLE_RATE, 1, BYTES_PER_SAMPLE);
}

bool AviFile::addAudioSamples(int samples, const void* data)
{
	return p_.addAudioSamples(samples, data);
}

bool AviFile::audioCapture(bool enable)
{
	if (enable)
		return p_.audioCapturer.StartCapture();
	else
	{
		p_.audioCapturer.StopCapture();
		return true;
	}
}

int AviFile::audioFlush(bool disregard)
{
	return p_.audioCapturer.flush(disregard);
}




