#include <string.h>
#include "H264SeqHdr.h"

static const unsigned char GL_BUF_SEQH_704x480[22] = {
	0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1e, 0x9a, 0x74,
	0x05, 0x81, 0xec, 0x80, 0x00, 0x00, 0x00, 0x01, 0x68, 0xce, 0x3a, 0x80
};

static const unsigned char GL_BUF_SEQH_704x240[21] = {
	0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1e, 0x9a, 0x74,
	0x05, 0x83, 0xf2,
	0x00, 0x00, 0x00, 0x01, 0x68, 0xce, 0x3a, 0x80
};

static const unsigned char GL_BUF_SEQH_352x240[21] = {
	0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1e, 0x9a, 0x74,
	0x0b, 0x0f, 0xc8,
	0x00, 0x00, 0x00, 0x01, 0x68, 0xce, 0x3a, 0x80
};

static const unsigned char GL_BUF_SEQH_704x576[22] = {
	0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1e, 0x9a, 0x74,
	0x05, 0x80, 0x93, 0x20,
	0x00, 0x00, 0x00, 0x01, 0x68, 0xce, 0x3a, 0x80
};

static const unsigned char GL_BUF_SEQH_704x288[22] = {
	0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1e, 0x9a, 0x74,
	0x05, 0x81, 0x2c, 0x80,
	0x00, 0x00, 0x00, 0x01, 0x68, 0xce, 0x3a, 0x80
};

static const unsigned char GL_BUF_SEQH_352x288[21] = {
	0x00, 0x00, 0x00, 0x01, 0x67, 0x42, 0x00, 0x1e, 0x9a, 0x74,
	0x0b, 0x04, 0xb2,
	0x00, 0x00, 0x00, 0x01, 0x68, 0xce, 0x3a, 0x80
};

/*! \brief : FFMpeg을 위한 Decode Frame을 각 해상도에 맞춰 헤더를 포함하여 만든다.(for I-Frame)
*  \param : buf  영상데이터가 저장된 버퍼
*  \param : dwsize : 영상 데이터의 크기
*  \param : width : 영상의 width
*  \param : height : 영상의 height
*  \return : 실제 decoder에 넘겨줘야하는 사이즈(헤더 + Raw데이터(dwSize))
*/
unsigned long MakeDecodeFrame(unsigned char* const buf, unsigned long dwSize, int width, int height)
{
	unsigned long dwHeaderSize;
	const unsigned char* header;

	if(width == 704 && height == 480){
		dwHeaderSize = sizeof(GL_BUF_SEQH_704x480);
		header = GL_BUF_SEQH_704x480;
	} else if(width == 704 && height == 240){
		dwHeaderSize = sizeof(GL_BUF_SEQH_704x240);
		header = GL_BUF_SEQH_704x240;
	} else if(width == 352 && height == 240){
		dwHeaderSize = sizeof(GL_BUF_SEQH_352x240);
		header = GL_BUF_SEQH_352x240;
	} else if(width == 704 && height == 576){
		dwHeaderSize = sizeof(GL_BUF_SEQH_704x576);
		header = GL_BUF_SEQH_704x576;
	} else if(width == 704 && height == 288){
		dwHeaderSize = sizeof(GL_BUF_SEQH_704x288);
		header = GL_BUF_SEQH_704x288;
	} else if(width == 352 && height == 288){
		dwHeaderSize = sizeof(GL_BUF_SEQH_352x288);
		header = GL_BUF_SEQH_352x288;
	} else {
		dwHeaderSize = 0;
		header = NULL;
	}

	memmove(buf + dwHeaderSize, buf, dwSize);
	memcpy(buf, header, dwHeaderSize);

	return (dwSize + dwHeaderSize);
}