/*
MPEG 2 decoder bitstream xfer
*/

#include <stdio.h>
#include <stdlib.h>
#include <sys/file.h>

#define	TRUE		1
#define	FALSE		0

int main(int argc, char **argv)
{
	FILE	*fp;
	static unsigned char	input_buffer[4][1920 * 1088 * 2];
	static unsigned char	output_buffer[5][1920 * 1088 * 2];
	char	infilename[] = {"bond2.yuv"};
	char	outfilename[] = {"gold0000.yuv"};
	int	i, j, k, l, x, y, length;
	int	indexin, indexout, horz, vert;
	int	framein = 2;
	int	frameout = 0;
	char	s[50];

	if (argc != 3) {
		fprintf(stderr, "usage: ivtc <horz> <vert>\n");
		exit(-1);
	}

	horz = atoi(argv[1]);
	vert = atoi(argv[2]);

	while (1)  {
		for (i = 0, k = 0; i < 4; i++, k++)  {
			/*--- open binary file (for parsing) ---*/
			fp = fopen(&infilename[0], "rb");
			if (fp == 0) {
				fprintf(stderr, "Cannot open input file <%s>\n", &infilename[0]);
				break;
			}
			length = fread(&input_buffer[i][0], 1, horz * vert * 2, fp);
			fclose(fp);
			sprintf(s, "%d.yuv", ++framein);
			for (j = 0; j < (strlen(s)); j++) {
				infilename[j+4] = s[j];
			}
		}


		if (k == 0)  {
			l = 0;
		}
		else if (k == 1)  {
			l = 1;
		}
		else if (k == 2)  {
			l = 2;
		}
		else if (k == 3)  {
			l = 3;
		}
		else if (k == 4)  {
			l = 5;
		}


		/* frame 1 */
		indexin = 0;	/* f1 A */
		indexout = 0;
		for (x = 0; x < vert / 2; x++)  {
			for (y = 0; y < horz * 2; y++)  {
				output_buffer[0][indexout++] = input_buffer[0][indexin++];
			}
			indexin = indexin + horz * 2;
			indexout = indexout + horz * 2;
		}

		indexin = horz * 2;	/* f2 A */
		indexout = horz * 2;
		for (x = 0; x < vert / 2; x++)  {
			for (y = 0; y < horz * 2; y++)  {
				output_buffer[0][indexout++] = input_buffer[0][indexin++];
			}
			indexin = indexin + horz * 2;
			indexout = indexout + horz * 2;
		}

		/* frame 2 */
		indexin = 0;	/* f1 B */
		indexout = 0;
		for (x = 0; x < vert / 2; x++)  {
			for (y = 0; y < horz * 2; y++)  {
				output_buffer[1][indexout++] = input_buffer[1][indexin++];
			}
			indexin = indexin + horz * 2;
			indexout = indexout + horz * 2;
		}

		indexin = horz * 2;	/* f2 B */
		indexout = horz * 2;
		for (x = 0; x < vert / 2; x++)  {
			for (y = 0; y < horz * 2; y++)  {
				output_buffer[1][indexout++] = input_buffer[1][indexin++];
			}
			indexin = indexin + horz * 2;
			indexout = indexout + horz * 2;
		}

		/* frame 3 */
		indexin = 0;	/* f1 B */
		indexout = 0;
		for (x = 0; x < vert / 2; x++)  {
			for (y = 0; y < horz * 2; y++)  {
				output_buffer[2][indexout++] = input_buffer[1][indexin++];
			}
			indexin = indexin + horz * 2;
			indexout = indexout + horz * 2;
		}

		indexin = horz * 2;	/* f2 C */
		indexout = horz * 2;
		for (x = 0; x < vert / 2; x++)  {
			for (y = 0; y < horz * 2; y++)  {
				output_buffer[2][indexout++] = input_buffer[2][indexin++];
			}
			indexin = indexin + horz * 2;
			indexout = indexout + horz * 2;
		}

		/* frame 4 */
		indexin = 0;	/* f1 C */
		indexout = 0;
		for (x = 0; x < vert / 2; x++)  {
			for (y = 0; y < horz * 2; y++)  {
				output_buffer[3][indexout++] = input_buffer[2][indexin++];
			}
			indexin = indexin + horz * 2;
			indexout = indexout + horz * 2;
		}

		indexin = horz * 2;	/* f2 D */
		indexout = horz * 2;
		for (x = 0; x < vert / 2; x++)  {
			for (y = 0; y < horz * 2; y++)  {
				output_buffer[3][indexout++] = input_buffer[3][indexin++];
			}
			indexin = indexin + horz * 2;
			indexout = indexout + horz * 2;
		}

		/* frame 5 */
		indexin = 0;	/* f1 D */
		indexout = 0;
		for (x = 0; x < vert / 2; x++)  {
			for (y = 0; y < horz * 2; y++)  {
				output_buffer[4][indexout++] = input_buffer[3][indexin++];
			}
			indexin = indexin + horz * 2;
			indexout = indexout + horz * 2;
		}

		indexin = horz * 2;	/* f2 D */
		indexout = horz * 2;
		for (x = 0; x < vert / 2; x++)  {
			for (y = 0; y < horz * 2; y++)  {
			output_buffer[4][indexout++] = input_buffer[3][indexin++];
			}
			indexin = indexin + horz * 2;
			indexout = indexout + horz * 2;
		}

		for (i = 0; i < l; i++)  {
			/*--- open binary file (for parsing) ---*/
			fp = fopen(&outfilename[0], "wb");
			if (fp == 0) {
				fprintf(stderr, "Cannot open input file <%s>\n", &outfilename[0]);
				exit(-1);
			}
			length = fwrite(&output_buffer[i][0], 1, horz * vert * 2, fp);
			fclose(fp);
			sprintf(s, "%04d", ++frameout);
			for (j = 0; j < 4; j++) {
				outfilename[j+4] = s[j];
			}
		}
		if (l != 5)  {
			break;
		}
	}

	return 0;
}
