/* TheorafileGMS - YUV decoder for Game Maker
 *
 * Copyright (c) 2021 Evan Hemsley
 *
 * This software is provided 'as-is', without any express or implied warranty.
 * In no event will the authors be held liable for any damages arising from
 * the use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you must not
 * claim that you wrote the original software. If you use this software in a
 * product, an acknowledgment in the product documentation would be
 * appreciated but is not required.
 *
 * 2. Altered source versions must be plainly marked as such, and must not be
 * misrepresented as being the original software.
 *
 * 3. This notice may not be removed or altered from any source distribution.
 *
 * Evan "cosmonaut" Hemsley <evan@moonside.games>
 *
 */

#include "TheorafileGMS.h"
#include "theorafile.h"
#include "SDL.h"

static inline void LogError(const char* string)
{
	SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "%s\n", string);
}

char* TheorafileGMS_Open(char* filename)
{
	OggTheora_File* file = SDL_malloc(sizeof(OggTheora_File));
	int err = tf_fopen(filename, file);
	if (err < 0)
	{
		if (err == TF_EUNKNOWN)
		{
			LogError("An unknown error occurred! Something very wrong happened!");
		}
		else if (err == TF_EUNSUPPORTED)
		{
			LogError("Unsupported theorafile type! Bailing!");
		}
		else if (err == TF_ENODATASOURCE)
		{
			LogError("Unknown data source! Bailing!");
		}
		else
		{
			LogError("An even more unknown error occurred!");
		}

		return NULL;
	}

	static char retaddr[64];
	SDL_memset(retaddr, 0, sizeof(retaddr));
	SDL_snprintf(retaddr, sizeof(retaddr) - 1, "%p%p", (void*)((uintptr_t)file >> 32), file); /* cursed hack for exporting 64-bit pointer */
	return retaddr;
}

void TheorafileGMS_Close(char* filePointer)
{
	OggTheora_File* file = (OggTheora_File*)filePointer;
	tf_close(file);
	/* SDL_free(file); */ /* FIXME: why the hell is this crashing game maker */
}

double TheorafileGMS_HasVideo(char* filePointer)
{
	OggTheora_File* file = (OggTheora_File*)filePointer;
	return (double)tf_hasvideo(file);
}

double TheorafileGMS_Width(char* filePointer)
{
	OggTheora_File* file = (OggTheora_File*)filePointer;
	int width;
	tf_videoinfo(file, &width, NULL, NULL, NULL);
	return (double)width;
}

double TheorafileGMS_Height(char* filePointer)
{
	OggTheora_File* file = (OggTheora_File*)filePointer;
	int height;
	tf_videoinfo(file, NULL, &height, NULL, NULL);
	return (double)height;
}

int TheorafileGMS_INTERNAL_UVWidthAndHeight(OggTheora_File* file, int *uvWidth, int *uvHeight)
{
	int yWidth;
	int yHeight;
	th_pixel_fmt format;
	tf_videoinfo(file, &yWidth, &yHeight, NULL, &format);

	int width;
	int height;

	if (format == TH_PF_420)
	{
		width = yWidth / 2;
		height = yHeight / 2;
	}
	else if (format == TH_PF_422)
	{
		width = yWidth / 2;
		height = yHeight;
	}
	else if (format == TH_PF_444)
	{
		width = yWidth;
		height = yHeight;
	}
	else
	{
		LogError("Unrecognized YUV format!");
		return 1;
	}

	if (uvWidth != NULL)
	{
		*uvWidth = width;
	}

	if (uvHeight != NULL)
	{
		*uvHeight = height;
	}

	return 0;
}

double TheorafileGMS_UVWidth(char* filePointer)
{
	OggTheora_File* file = (OggTheora_File*)filePointer;

	int uvWidth;
	int err = TheorafileGMS_INTERNAL_UVWidthAndHeight(file, &uvWidth, NULL);

	if (err)
	{
		return 0;
	}

	return (double)uvWidth;
}

double TheorafileGMS_UVHeight(char* filePointer)
{
	OggTheora_File* file = (OggTheora_File*)filePointer;

	int uvHeight;
	int err = TheorafileGMS_INTERNAL_UVWidthAndHeight(file, NULL, &uvHeight);

	if (err)
	{
		return 0;
	}

	return (double)uvHeight;
}

double TheorafileGMS_FPS(char* filePointer)
{
	OggTheora_File* file = (OggTheora_File*)filePointer;
	double fps;
	tf_videoinfo(file, NULL, NULL, &fps, NULL);
	return fps;
}

int TheorafileGMS_INTERNAL_RequiredBufferSizeInBytes(OggTheora_File* file)
{
	int yWidth;
	int yHeight;
	th_pixel_fmt format;
	tf_videoinfo(file, &yWidth, &yHeight, NULL, &format);

	int uvWidth;
	int uvHeight;

	int err = TheorafileGMS_INTERNAL_UVWidthAndHeight(file, &uvWidth, &uvHeight);

	if (err)
	{
		return -1;
	}

	int yuvDataLen = (
		(yWidth * yHeight) +
		(uvWidth * uvHeight * 2)
		);

	return yuvDataLen;
}

double TheorafileGMS_RequiredBufferSizeInBytes(char* filePointer)
{
	OggTheora_File* file = (OggTheora_File*)filePointer;
	return TheorafileGMS_INTERNAL_RequiredBufferSizeInBytes(file) * 4; /* game maker can only take RGBA32 lol */
}

double TheorafileGMS_EndOfStream(char* filePointer)
{
	OggTheora_File* file = (OggTheora_File*)filePointer;
	return (double)tf_eos(file);
}

void TheorafileGMS_Reset(char* filePointer)
{
	OggTheora_File* file = (OggTheora_File*)filePointer;
	tf_reset(file);
}

double TheorafileGMS_ReadVideo(char* filePointer, char* buffer, double numFrames)
{
	OggTheora_File* file = (OggTheora_File*)filePointer;
	int yWidth;
	int yHeight;
	th_pixel_fmt format;
	tf_videoinfo(file, &yWidth, &yHeight, NULL, &format);

	int requiredBufferSize = TheorafileGMS_INTERNAL_RequiredBufferSizeInBytes(file);
	if (requiredBufferSize <= 0)
	{
		LogError("Unrecognized format! Bailing!");
		return -1;
	}

	/* Game Maker doesnt have R8 textures so we have to do this padding bullshit */
	char* yuvBuffer = SDL_malloc(requiredBufferSize * 4);

	int updated = tf_readvideo(file, yuvBuffer, (int)numFrames);

	for (int i = 0; i < requiredBufferSize; i += 1)
	{
		buffer[i * 4] = yuvBuffer[i];
	}

	SDL_free(yuvBuffer);
	return (double)updated;
}
