162074f44bb5944593b65d6203deae6300bead42dYounes Manton/**************************************************************************
2dcf8ee7d6ac89bb2a9d608618a51604a3c78fe96Christian König *
362074f44bb5944593b65d6203deae6300bead42dYounes Manton * Copyright 2009 Younes Manton.
462074f44bb5944593b65d6203deae6300bead42dYounes Manton * All Rights Reserved.
5dcf8ee7d6ac89bb2a9d608618a51604a3c78fe96Christian König *
662074f44bb5944593b65d6203deae6300bead42dYounes Manton * Permission is hereby granted, free of charge, to any person obtaining a
762074f44bb5944593b65d6203deae6300bead42dYounes Manton * copy of this software and associated documentation files (the
862074f44bb5944593b65d6203deae6300bead42dYounes Manton * "Software"), to deal in the Software without restriction, including
962074f44bb5944593b65d6203deae6300bead42dYounes Manton * without limitation the rights to use, copy, modify, merge, publish,
1062074f44bb5944593b65d6203deae6300bead42dYounes Manton * distribute, sub license, and/or sell copies of the Software, and to
1162074f44bb5944593b65d6203deae6300bead42dYounes Manton * permit persons to whom the Software is furnished to do so, subject to
1262074f44bb5944593b65d6203deae6300bead42dYounes Manton * the following conditions:
13dcf8ee7d6ac89bb2a9d608618a51604a3c78fe96Christian König *
1462074f44bb5944593b65d6203deae6300bead42dYounes Manton * The above copyright notice and this permission notice (including the
1562074f44bb5944593b65d6203deae6300bead42dYounes Manton * next paragraph) shall be included in all copies or substantial portions
1662074f44bb5944593b65d6203deae6300bead42dYounes Manton * of the Software.
17dcf8ee7d6ac89bb2a9d608618a51604a3c78fe96Christian König *
1862074f44bb5944593b65d6203deae6300bead42dYounes Manton * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
1962074f44bb5944593b65d6203deae6300bead42dYounes Manton * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2062074f44bb5944593b65d6203deae6300bead42dYounes Manton * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
2162074f44bb5944593b65d6203deae6300bead42dYounes Manton * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
2262074f44bb5944593b65d6203deae6300bead42dYounes Manton * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
2362074f44bb5944593b65d6203deae6300bead42dYounes Manton * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
2462074f44bb5944593b65d6203deae6300bead42dYounes Manton * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25dcf8ee7d6ac89bb2a9d608618a51604a3c78fe96Christian König *
2662074f44bb5944593b65d6203deae6300bead42dYounes Manton **************************************************************************/
2762074f44bb5944593b65d6203deae6300bead42dYounes Manton
2862074f44bb5944593b65d6203deae6300bead42dYounes Manton#include <assert.h>
2962074f44bb5944593b65d6203deae6300bead42dYounes Manton#include <stdio.h>
3062074f44bb5944593b65d6203deae6300bead42dYounes Manton#include <string.h>
3162074f44bb5944593b65d6203deae6300bead42dYounes Manton#include <error.h>
3262074f44bb5944593b65d6203deae6300bead42dYounes Manton#include "testlib.h"
3362074f44bb5944593b65d6203deae6300bead42dYounes Manton
3462074f44bb5944593b65d6203deae6300bead42dYounes Manton#define BLOCK_WIDTH			8
3562074f44bb5944593b65d6203deae6300bead42dYounes Manton#define BLOCK_HEIGHT			8
3662074f44bb5944593b65d6203deae6300bead42dYounes Manton#define BLOCK_SIZE			(BLOCK_WIDTH * BLOCK_HEIGHT)
3762074f44bb5944593b65d6203deae6300bead42dYounes Manton#define MACROBLOCK_WIDTH		16
3862074f44bb5944593b65d6203deae6300bead42dYounes Manton#define MACROBLOCK_HEIGHT		16
3962074f44bb5944593b65d6203deae6300bead42dYounes Manton#define MACROBLOCK_WIDTH_IN_BLOCKS	(MACROBLOCK_WIDTH / BLOCK_WIDTH)
4062074f44bb5944593b65d6203deae6300bead42dYounes Manton#define MACROBLOCK_HEIGHT_IN_BLOCKS	(MACROBLOCK_HEIGHT / BLOCK_HEIGHT)
4162074f44bb5944593b65d6203deae6300bead42dYounes Manton#define BLOCKS_PER_MACROBLOCK		6
4262074f44bb5944593b65d6203deae6300bead42dYounes Manton
43110f846c2578357ebee4e6fab7d71e0e496493d3Maarten Lankhorst#define INPUT_WIDTH			64
44110f846c2578357ebee4e6fab7d71e0e496493d3Maarten Lankhorst#define INPUT_HEIGHT			64
4562074f44bb5944593b65d6203deae6300bead42dYounes Manton#define INPUT_WIDTH_IN_MACROBLOCKS	(INPUT_WIDTH / MACROBLOCK_WIDTH)
4662074f44bb5944593b65d6203deae6300bead42dYounes Manton#define INPUT_HEIGHT_IN_MACROBLOCKS	(INPUT_HEIGHT / MACROBLOCK_HEIGHT)
4762074f44bb5944593b65d6203deae6300bead42dYounes Manton#define NUM_MACROBLOCKS			(INPUT_WIDTH_IN_MACROBLOCKS * INPUT_HEIGHT_IN_MACROBLOCKS)
4862074f44bb5944593b65d6203deae6300bead42dYounes Manton
4962074f44bb5944593b65d6203deae6300bead42dYounes Manton#define DEFAULT_OUTPUT_WIDTH		INPUT_WIDTH
5062074f44bb5944593b65d6203deae6300bead42dYounes Manton#define DEFAULT_OUTPUT_HEIGHT		INPUT_HEIGHT
5162074f44bb5944593b65d6203deae6300bead42dYounes Manton#define DEFAULT_ACCEPTABLE_ERR		0.01
5262074f44bb5944593b65d6203deae6300bead42dYounes Manton
5362074f44bb5944593b65d6203deae6300bead42dYounes Mantonvoid ParseArgs(int argc, char **argv, unsigned int *output_width, unsigned int *output_height, double *acceptable_error, int *prompt);
5462074f44bb5944593b65d6203deae6300bead42dYounes Manton
5562074f44bb5944593b65d6203deae6300bead42dYounes Mantonvoid ParseArgs(int argc, char **argv, unsigned int *output_width, unsigned int *output_height, double *acceptable_error, int *prompt)
5662074f44bb5944593b65d6203deae6300bead42dYounes Manton{
5762074f44bb5944593b65d6203deae6300bead42dYounes Manton	int fail = 0;
5862074f44bb5944593b65d6203deae6300bead42dYounes Manton	int i;
5962074f44bb5944593b65d6203deae6300bead42dYounes Manton
6062074f44bb5944593b65d6203deae6300bead42dYounes Manton	*output_width = DEFAULT_OUTPUT_WIDTH;
61110f846c2578357ebee4e6fab7d71e0e496493d3Maarten Lankhorst	*output_height = DEFAULT_OUTPUT_HEIGHT;
6262074f44bb5944593b65d6203deae6300bead42dYounes Manton	*acceptable_error = DEFAULT_ACCEPTABLE_ERR;
6362074f44bb5944593b65d6203deae6300bead42dYounes Manton	*prompt = 1;
6462074f44bb5944593b65d6203deae6300bead42dYounes Manton
6562074f44bb5944593b65d6203deae6300bead42dYounes Manton	for (i = 1; i < argc && !fail; ++i)
6662074f44bb5944593b65d6203deae6300bead42dYounes Manton	{
6762074f44bb5944593b65d6203deae6300bead42dYounes Manton		if (!strcmp(argv[i], "-w"))
6862074f44bb5944593b65d6203deae6300bead42dYounes Manton		{
6962074f44bb5944593b65d6203deae6300bead42dYounes Manton			if (sscanf(argv[++i], "%u", output_width) != 1)
7062074f44bb5944593b65d6203deae6300bead42dYounes Manton				fail = 1;
7162074f44bb5944593b65d6203deae6300bead42dYounes Manton		}
7262074f44bb5944593b65d6203deae6300bead42dYounes Manton		else if (!strcmp(argv[i], "-h"))
7362074f44bb5944593b65d6203deae6300bead42dYounes Manton		{
7462074f44bb5944593b65d6203deae6300bead42dYounes Manton			if (sscanf(argv[++i], "%u", output_height) != 1)
7562074f44bb5944593b65d6203deae6300bead42dYounes Manton				fail = 1;
7662074f44bb5944593b65d6203deae6300bead42dYounes Manton		}
7762074f44bb5944593b65d6203deae6300bead42dYounes Manton		else if (!strcmp(argv[i], "-e"))
7862074f44bb5944593b65d6203deae6300bead42dYounes Manton		{
7962074f44bb5944593b65d6203deae6300bead42dYounes Manton			if (sscanf(argv[++i], "%lf", acceptable_error) != 1)
8062074f44bb5944593b65d6203deae6300bead42dYounes Manton				fail = 1;
8162074f44bb5944593b65d6203deae6300bead42dYounes Manton		}
8262074f44bb5944593b65d6203deae6300bead42dYounes Manton		else if (strcmp(argv[i], "-n"))
8362074f44bb5944593b65d6203deae6300bead42dYounes Manton			*prompt = 0;
8462074f44bb5944593b65d6203deae6300bead42dYounes Manton		else
8562074f44bb5944593b65d6203deae6300bead42dYounes Manton			fail = 1;
8662074f44bb5944593b65d6203deae6300bead42dYounes Manton	}
8762074f44bb5944593b65d6203deae6300bead42dYounes Manton
8862074f44bb5944593b65d6203deae6300bead42dYounes Manton	if (fail)
8962074f44bb5944593b65d6203deae6300bead42dYounes Manton		error
9062074f44bb5944593b65d6203deae6300bead42dYounes Manton		(
9162074f44bb5944593b65d6203deae6300bead42dYounes Manton			1, 0,
9262074f44bb5944593b65d6203deae6300bead42dYounes Manton			"Bad argument.\n"
9362074f44bb5944593b65d6203deae6300bead42dYounes Manton			"\n"
9462074f44bb5944593b65d6203deae6300bead42dYounes Manton			"Usage: %s [options]\n"
9562074f44bb5944593b65d6203deae6300bead42dYounes Manton			"\t-w <width>\tOutput width\n"
9662074f44bb5944593b65d6203deae6300bead42dYounes Manton			"\t-h <height>\tOutput height\n"
9762074f44bb5944593b65d6203deae6300bead42dYounes Manton			"\t-e <error>\tAcceptable margin of error per pixel, from 0 to 1\n"
9862074f44bb5944593b65d6203deae6300bead42dYounes Manton			"\t-n\tDon't prompt for quit\n",
9962074f44bb5944593b65d6203deae6300bead42dYounes Manton			argv[0]
10062074f44bb5944593b65d6203deae6300bead42dYounes Manton		);
10162074f44bb5944593b65d6203deae6300bead42dYounes Manton}
10262074f44bb5944593b65d6203deae6300bead42dYounes Manton
103110f846c2578357ebee4e6fab7d71e0e496493d3Maarten Lankhorststatic void Gradient(short *block, unsigned int start, unsigned int stop, int horizontal, unsigned int intra_unsigned)
10462074f44bb5944593b65d6203deae6300bead42dYounes Manton{
10562074f44bb5944593b65d6203deae6300bead42dYounes Manton	unsigned int x, y;
10662074f44bb5944593b65d6203deae6300bead42dYounes Manton	unsigned int range = stop - start;
10762074f44bb5944593b65d6203deae6300bead42dYounes Manton
10862074f44bb5944593b65d6203deae6300bead42dYounes Manton	if (horizontal)
10962074f44bb5944593b65d6203deae6300bead42dYounes Manton	{
11062074f44bb5944593b65d6203deae6300bead42dYounes Manton		for (y = 0; y < BLOCK_HEIGHT; ++y)
111110f846c2578357ebee4e6fab7d71e0e496493d3Maarten Lankhorst			for (x = 0; x < BLOCK_WIDTH; ++x) {
112110f846c2578357ebee4e6fab7d71e0e496493d3Maarten Lankhorst				*block = (short)(start + range * (x / (float)(BLOCK_WIDTH - 1)));
113110f846c2578357ebee4e6fab7d71e0e496493d3Maarten Lankhorst				if (intra_unsigned)
114110f846c2578357ebee4e6fab7d71e0e496493d3Maarten Lankhorst					*block += 1 << 10;
115110f846c2578357ebee4e6fab7d71e0e496493d3Maarten Lankhorst				block++;
116110f846c2578357ebee4e6fab7d71e0e496493d3Maarten Lankhorst			}
11762074f44bb5944593b65d6203deae6300bead42dYounes Manton	}
11862074f44bb5944593b65d6203deae6300bead42dYounes Manton	else
11962074f44bb5944593b65d6203deae6300bead42dYounes Manton	{
12062074f44bb5944593b65d6203deae6300bead42dYounes Manton		for (y = 0; y < BLOCK_HEIGHT; ++y)
121110f846c2578357ebee4e6fab7d71e0e496493d3Maarten Lankhorst			for (x = 0; x < BLOCK_WIDTH; ++x) {
122110f846c2578357ebee4e6fab7d71e0e496493d3Maarten Lankhorst				*block = (short)(start + range * (y / (float)(BLOCK_WIDTH - 1)));
123110f846c2578357ebee4e6fab7d71e0e496493d3Maarten Lankhorst				if (intra_unsigned)
124110f846c2578357ebee4e6fab7d71e0e496493d3Maarten Lankhorst					*block += 1 << 10;
125110f846c2578357ebee4e6fab7d71e0e496493d3Maarten Lankhorst				block++;
126110f846c2578357ebee4e6fab7d71e0e496493d3Maarten Lankhorst			}
12762074f44bb5944593b65d6203deae6300bead42dYounes Manton	}
12862074f44bb5944593b65d6203deae6300bead42dYounes Manton}
12962074f44bb5944593b65d6203deae6300bead42dYounes Manton
13062074f44bb5944593b65d6203deae6300bead42dYounes Mantonint main(int argc, char **argv)
13162074f44bb5944593b65d6203deae6300bead42dYounes Manton{
13262074f44bb5944593b65d6203deae6300bead42dYounes Manton	unsigned int		output_width;
13362074f44bb5944593b65d6203deae6300bead42dYounes Manton	unsigned int		output_height;
13462074f44bb5944593b65d6203deae6300bead42dYounes Manton	double			acceptable_error;
13562074f44bb5944593b65d6203deae6300bead42dYounes Manton	int			prompt;
13662074f44bb5944593b65d6203deae6300bead42dYounes Manton	Display			*display;
13762074f44bb5944593b65d6203deae6300bead42dYounes Manton	Window			root, window;
138110f846c2578357ebee4e6fab7d71e0e496493d3Maarten Lankhorst	const unsigned int	mc_types[] = {XVMC_MOCOMP | XVMC_MPEG_2};
13962074f44bb5944593b65d6203deae6300bead42dYounes Manton	XvPortID		port_num;
14062074f44bb5944593b65d6203deae6300bead42dYounes Manton	int			surface_type_id;
14162074f44bb5944593b65d6203deae6300bead42dYounes Manton	unsigned int		is_overlay, intra_unsigned;
14262074f44bb5944593b65d6203deae6300bead42dYounes Manton	int			colorkey;
14362074f44bb5944593b65d6203deae6300bead42dYounes Manton	XvMCContext		context;
14462074f44bb5944593b65d6203deae6300bead42dYounes Manton	XvMCSurface		surface;
14562074f44bb5944593b65d6203deae6300bead42dYounes Manton	XvMCBlockArray		block_array;
14662074f44bb5944593b65d6203deae6300bead42dYounes Manton	XvMCMacroBlockArray	mb_array;
14762074f44bb5944593b65d6203deae6300bead42dYounes Manton	int			mbx, mby, bx, by;
14862074f44bb5944593b65d6203deae6300bead42dYounes Manton	XvMCMacroBlock		*mb;
14962074f44bb5944593b65d6203deae6300bead42dYounes Manton	short			*blocks;
15062074f44bb5944593b65d6203deae6300bead42dYounes Manton	int			quit = 0;
15162074f44bb5944593b65d6203deae6300bead42dYounes Manton
15262074f44bb5944593b65d6203deae6300bead42dYounes Manton	ParseArgs(argc, argv, &output_width, &output_height, &acceptable_error, &prompt);
15362074f44bb5944593b65d6203deae6300bead42dYounes Manton
15462074f44bb5944593b65d6203deae6300bead42dYounes Manton	display = XOpenDisplay(NULL);
15562074f44bb5944593b65d6203deae6300bead42dYounes Manton
15662074f44bb5944593b65d6203deae6300bead42dYounes Manton	if (!GetPort
15762074f44bb5944593b65d6203deae6300bead42dYounes Manton	(
15862074f44bb5944593b65d6203deae6300bead42dYounes Manton		display,
15962074f44bb5944593b65d6203deae6300bead42dYounes Manton		INPUT_WIDTH,
16062074f44bb5944593b65d6203deae6300bead42dYounes Manton		INPUT_HEIGHT,
16162074f44bb5944593b65d6203deae6300bead42dYounes Manton		XVMC_CHROMA_FORMAT_420,
162dcf8ee7d6ac89bb2a9d608618a51604a3c78fe96Christian König		mc_types,
163110f846c2578357ebee4e6fab7d71e0e496493d3Maarten Lankhorst		sizeof(mc_types)/sizeof(*mc_types),
164dcf8ee7d6ac89bb2a9d608618a51604a3c78fe96Christian König		&port_num,
165dcf8ee7d6ac89bb2a9d608618a51604a3c78fe96Christian König		&surface_type_id,
166dcf8ee7d6ac89bb2a9d608618a51604a3c78fe96Christian König		&is_overlay,
167dcf8ee7d6ac89bb2a9d608618a51604a3c78fe96Christian König		&intra_unsigned
16862074f44bb5944593b65d6203deae6300bead42dYounes Manton	))
16962074f44bb5944593b65d6203deae6300bead42dYounes Manton	{
17062074f44bb5944593b65d6203deae6300bead42dYounes Manton		XCloseDisplay(display);
17162074f44bb5944593b65d6203deae6300bead42dYounes Manton		error(1, 0, "Error, unable to find a good port.\n");
17262074f44bb5944593b65d6203deae6300bead42dYounes Manton	}
17362074f44bb5944593b65d6203deae6300bead42dYounes Manton
17462074f44bb5944593b65d6203deae6300bead42dYounes Manton	if (is_overlay)
17562074f44bb5944593b65d6203deae6300bead42dYounes Manton	{
17662074f44bb5944593b65d6203deae6300bead42dYounes Manton		Atom xv_colorkey = XInternAtom(display, "XV_COLORKEY", 0);
17762074f44bb5944593b65d6203deae6300bead42dYounes Manton		XvGetPortAttribute(display, port_num, xv_colorkey, &colorkey);
17862074f44bb5944593b65d6203deae6300bead42dYounes Manton	}
17962074f44bb5944593b65d6203deae6300bead42dYounes Manton
18062074f44bb5944593b65d6203deae6300bead42dYounes Manton	root = XDefaultRootWindow(display);
18162074f44bb5944593b65d6203deae6300bead42dYounes Manton	window = XCreateSimpleWindow(display, root, 0, 0, output_width, output_height, 0, 0, colorkey);
18262074f44bb5944593b65d6203deae6300bead42dYounes Manton
18362074f44bb5944593b65d6203deae6300bead42dYounes Manton	assert(XvMCCreateContext(display, port_num, surface_type_id, INPUT_WIDTH, INPUT_HEIGHT, XVMC_DIRECT, &context) == Success);
18462074f44bb5944593b65d6203deae6300bead42dYounes Manton	assert(XvMCCreateSurface(display, &context, &surface) == Success);
18562074f44bb5944593b65d6203deae6300bead42dYounes Manton	assert(XvMCCreateBlocks(display, &context, NUM_MACROBLOCKS * BLOCKS_PER_MACROBLOCK, &block_array) == Success);
18662074f44bb5944593b65d6203deae6300bead42dYounes Manton	assert(XvMCCreateMacroBlocks(display, &context, NUM_MACROBLOCKS, &mb_array) == Success);
18762074f44bb5944593b65d6203deae6300bead42dYounes Manton
18862074f44bb5944593b65d6203deae6300bead42dYounes Manton	mb = mb_array.macro_blocks;
18962074f44bb5944593b65d6203deae6300bead42dYounes Manton	blocks = block_array.blocks;
19062074f44bb5944593b65d6203deae6300bead42dYounes Manton
19162074f44bb5944593b65d6203deae6300bead42dYounes Manton	for (mby = 0; mby < INPUT_HEIGHT_IN_MACROBLOCKS; ++mby)
19262074f44bb5944593b65d6203deae6300bead42dYounes Manton		for (mbx = 0; mbx < INPUT_WIDTH_IN_MACROBLOCKS; ++mbx)
19362074f44bb5944593b65d6203deae6300bead42dYounes Manton		{
19462074f44bb5944593b65d6203deae6300bead42dYounes Manton			mb->x = mbx;
19562074f44bb5944593b65d6203deae6300bead42dYounes Manton			mb->y = mby;
19662074f44bb5944593b65d6203deae6300bead42dYounes Manton			mb->macroblock_type = XVMC_MB_TYPE_INTRA;
19762074f44bb5944593b65d6203deae6300bead42dYounes Manton			/*mb->motion_type = ;*/
19862074f44bb5944593b65d6203deae6300bead42dYounes Manton			/*mb->motion_vertical_field_select = ;*/
19962074f44bb5944593b65d6203deae6300bead42dYounes Manton			mb->dct_type = XVMC_DCT_TYPE_FRAME;
20062074f44bb5944593b65d6203deae6300bead42dYounes Manton			/*mb->PMV[0][0][0] = ;
20162074f44bb5944593b65d6203deae6300bead42dYounes Manton			mb->PMV[0][0][1] = ;
20262074f44bb5944593b65d6203deae6300bead42dYounes Manton			mb->PMV[0][1][0] = ;
20362074f44bb5944593b65d6203deae6300bead42dYounes Manton			mb->PMV[0][1][1] = ;
20462074f44bb5944593b65d6203deae6300bead42dYounes Manton			mb->PMV[1][0][0] = ;
20562074f44bb5944593b65d6203deae6300bead42dYounes Manton			mb->PMV[1][0][1] = ;
20662074f44bb5944593b65d6203deae6300bead42dYounes Manton			mb->PMV[1][1][0] = ;
20762074f44bb5944593b65d6203deae6300bead42dYounes Manton			mb->PMV[1][1][1] = ;*/
20862074f44bb5944593b65d6203deae6300bead42dYounes Manton			mb->index = (mby * INPUT_WIDTH_IN_MACROBLOCKS + mbx) * BLOCKS_PER_MACROBLOCK;
20962074f44bb5944593b65d6203deae6300bead42dYounes Manton			mb->coded_block_pattern = 0x3F;
21062074f44bb5944593b65d6203deae6300bead42dYounes Manton
21162074f44bb5944593b65d6203deae6300bead42dYounes Manton			mb++;
21262074f44bb5944593b65d6203deae6300bead42dYounes Manton
21362074f44bb5944593b65d6203deae6300bead42dYounes Manton			for (by = 0; by < MACROBLOCK_HEIGHT_IN_BLOCKS; ++by)
21462074f44bb5944593b65d6203deae6300bead42dYounes Manton				for (bx = 0; bx < MACROBLOCK_WIDTH_IN_BLOCKS; ++bx)
21562074f44bb5944593b65d6203deae6300bead42dYounes Manton				{
21662074f44bb5944593b65d6203deae6300bead42dYounes Manton					const int start = 16, stop = 235, range = stop - start;
21762074f44bb5944593b65d6203deae6300bead42dYounes Manton
21862074f44bb5944593b65d6203deae6300bead42dYounes Manton					Gradient
21962074f44bb5944593b65d6203deae6300bead42dYounes Manton					(
22062074f44bb5944593b65d6203deae6300bead42dYounes Manton						blocks,
22162074f44bb5944593b65d6203deae6300bead42dYounes Manton						(short)(start + range * ((mbx * MACROBLOCK_WIDTH + bx * BLOCK_WIDTH) / (float)(INPUT_WIDTH - 1))),
22262074f44bb5944593b65d6203deae6300bead42dYounes Manton						(short)(start + range * ((mbx * MACROBLOCK_WIDTH + bx * BLOCK_WIDTH + BLOCK_WIDTH - 1) / (float)(INPUT_WIDTH - 1))),
223110f846c2578357ebee4e6fab7d71e0e496493d3Maarten Lankhorst						1,
224110f846c2578357ebee4e6fab7d71e0e496493d3Maarten Lankhorst						intra_unsigned
22562074f44bb5944593b65d6203deae6300bead42dYounes Manton					);
22662074f44bb5944593b65d6203deae6300bead42dYounes Manton
22762074f44bb5944593b65d6203deae6300bead42dYounes Manton					blocks += BLOCK_SIZE;
22862074f44bb5944593b65d6203deae6300bead42dYounes Manton				}
22962074f44bb5944593b65d6203deae6300bead42dYounes Manton
23062074f44bb5944593b65d6203deae6300bead42dYounes Manton			for (by = 0; by < MACROBLOCK_HEIGHT_IN_BLOCKS / 2; ++by)
23162074f44bb5944593b65d6203deae6300bead42dYounes Manton				for (bx = 0; bx < MACROBLOCK_WIDTH_IN_BLOCKS / 2; ++bx)
23262074f44bb5944593b65d6203deae6300bead42dYounes Manton				{
23362074f44bb5944593b65d6203deae6300bead42dYounes Manton					const int start = 16, stop = 240, range = stop - start;
23462074f44bb5944593b65d6203deae6300bead42dYounes Manton
23562074f44bb5944593b65d6203deae6300bead42dYounes Manton					Gradient
23662074f44bb5944593b65d6203deae6300bead42dYounes Manton					(
23762074f44bb5944593b65d6203deae6300bead42dYounes Manton						blocks,
23862074f44bb5944593b65d6203deae6300bead42dYounes Manton						(short)(start + range * ((mbx * MACROBLOCK_WIDTH + bx * BLOCK_WIDTH) / (float)(INPUT_WIDTH - 1))),
23962074f44bb5944593b65d6203deae6300bead42dYounes Manton						(short)(start + range * ((mbx * MACROBLOCK_WIDTH + bx * BLOCK_WIDTH + BLOCK_WIDTH - 1) / (float)(INPUT_WIDTH - 1))),
240110f846c2578357ebee4e6fab7d71e0e496493d3Maarten Lankhorst						1,
241110f846c2578357ebee4e6fab7d71e0e496493d3Maarten Lankhorst						intra_unsigned
24262074f44bb5944593b65d6203deae6300bead42dYounes Manton					);
24362074f44bb5944593b65d6203deae6300bead42dYounes Manton
24462074f44bb5944593b65d6203deae6300bead42dYounes Manton					blocks += BLOCK_SIZE;
24562074f44bb5944593b65d6203deae6300bead42dYounes Manton
24662074f44bb5944593b65d6203deae6300bead42dYounes Manton					Gradient
24762074f44bb5944593b65d6203deae6300bead42dYounes Manton					(
24862074f44bb5944593b65d6203deae6300bead42dYounes Manton						blocks,
24962074f44bb5944593b65d6203deae6300bead42dYounes Manton						(short)(start + range * ((mbx * MACROBLOCK_WIDTH + bx * BLOCK_WIDTH) / (float)(INPUT_WIDTH - 1))),
25062074f44bb5944593b65d6203deae6300bead42dYounes Manton						(short)(start + range * ((mbx * MACROBLOCK_WIDTH + bx * BLOCK_WIDTH + BLOCK_WIDTH - 1) / (float)(INPUT_WIDTH - 1))),
251110f846c2578357ebee4e6fab7d71e0e496493d3Maarten Lankhorst						1,
252110f846c2578357ebee4e6fab7d71e0e496493d3Maarten Lankhorst						intra_unsigned
25362074f44bb5944593b65d6203deae6300bead42dYounes Manton					);
25462074f44bb5944593b65d6203deae6300bead42dYounes Manton
25562074f44bb5944593b65d6203deae6300bead42dYounes Manton					blocks += BLOCK_SIZE;
25662074f44bb5944593b65d6203deae6300bead42dYounes Manton				}
25762074f44bb5944593b65d6203deae6300bead42dYounes Manton		}
25862074f44bb5944593b65d6203deae6300bead42dYounes Manton
25962074f44bb5944593b65d6203deae6300bead42dYounes Manton	XSelectInput(display, window, ExposureMask | KeyPressMask);
26062074f44bb5944593b65d6203deae6300bead42dYounes Manton	XMapWindow(display, window);
26162074f44bb5944593b65d6203deae6300bead42dYounes Manton	XSync(display, 0);
26262074f44bb5944593b65d6203deae6300bead42dYounes Manton
26362074f44bb5944593b65d6203deae6300bead42dYounes Manton	/* Test NULL context */
26462074f44bb5944593b65d6203deae6300bead42dYounes Manton	assert(XvMCRenderSurface(display, NULL, XVMC_FRAME_PICTURE, &surface, NULL, NULL, 0, NUM_MACROBLOCKS, 0, &mb_array, &block_array) == XvMCBadContext);
26562074f44bb5944593b65d6203deae6300bead42dYounes Manton	/* Test NULL surface */
26662074f44bb5944593b65d6203deae6300bead42dYounes Manton	assert(XvMCRenderSurface(display, &context, XVMC_FRAME_PICTURE, NULL, NULL, NULL, 0, NUM_MACROBLOCKS, 0, &mb_array, &block_array) == XvMCBadSurface);
26762074f44bb5944593b65d6203deae6300bead42dYounes Manton	/* Test bad picture structure */
26862074f44bb5944593b65d6203deae6300bead42dYounes Manton	assert(XvMCRenderSurface(display, &context, 0, &surface, NULL, NULL, 0, NUM_MACROBLOCKS, 0, &mb_array, &block_array) == BadValue);
26962074f44bb5944593b65d6203deae6300bead42dYounes Manton	/* Test valid params */
27062074f44bb5944593b65d6203deae6300bead42dYounes Manton	assert(XvMCRenderSurface(display, &context, XVMC_FRAME_PICTURE, &surface, NULL, NULL, 0, NUM_MACROBLOCKS, 0, &mb_array, &block_array) == Success);
27162074f44bb5944593b65d6203deae6300bead42dYounes Manton
27262074f44bb5944593b65d6203deae6300bead42dYounes Manton	/* Test NULL surface */
27362074f44bb5944593b65d6203deae6300bead42dYounes Manton	assert(XvMCPutSurface(display, NULL, window, 0, 0, INPUT_WIDTH, INPUT_HEIGHT, 0, 0, output_width, output_height, XVMC_FRAME_PICTURE) == XvMCBadSurface);
27462074f44bb5944593b65d6203deae6300bead42dYounes Manton	/* Test bad window */
27562074f44bb5944593b65d6203deae6300bead42dYounes Manton	/* XXX: X halts with a bad drawable for some reason, doesn't return BadDrawable as expected */
27662074f44bb5944593b65d6203deae6300bead42dYounes Manton	/*assert(XvMCPutSurface(display, &surface, 0, 0, 0, width, height, 0, 0, width, height, XVMC_FRAME_PICTURE) == BadDrawable);*/
27762074f44bb5944593b65d6203deae6300bead42dYounes Manton
27862074f44bb5944593b65d6203deae6300bead42dYounes Manton	if (prompt)
27962074f44bb5944593b65d6203deae6300bead42dYounes Manton	{
28062074f44bb5944593b65d6203deae6300bead42dYounes Manton		puts("Press any button to quit...");
28162074f44bb5944593b65d6203deae6300bead42dYounes Manton
28262074f44bb5944593b65d6203deae6300bead42dYounes Manton		while (!quit)
28362074f44bb5944593b65d6203deae6300bead42dYounes Manton		{
28462074f44bb5944593b65d6203deae6300bead42dYounes Manton			if (XPending(display) > 0)
28562074f44bb5944593b65d6203deae6300bead42dYounes Manton			{
28662074f44bb5944593b65d6203deae6300bead42dYounes Manton				XEvent event;
28762074f44bb5944593b65d6203deae6300bead42dYounes Manton
28862074f44bb5944593b65d6203deae6300bead42dYounes Manton				XNextEvent(display, &event);
28962074f44bb5944593b65d6203deae6300bead42dYounes Manton
29062074f44bb5944593b65d6203deae6300bead42dYounes Manton				switch (event.type)
29162074f44bb5944593b65d6203deae6300bead42dYounes Manton				{
29262074f44bb5944593b65d6203deae6300bead42dYounes Manton					case Expose:
29362074f44bb5944593b65d6203deae6300bead42dYounes Manton					{
29462074f44bb5944593b65d6203deae6300bead42dYounes Manton						/* Test valid params */
29562074f44bb5944593b65d6203deae6300bead42dYounes Manton						assert
29662074f44bb5944593b65d6203deae6300bead42dYounes Manton						(
29762074f44bb5944593b65d6203deae6300bead42dYounes Manton							XvMCPutSurface
29862074f44bb5944593b65d6203deae6300bead42dYounes Manton							(
29962074f44bb5944593b65d6203deae6300bead42dYounes Manton								display, &surface, window,
30062074f44bb5944593b65d6203deae6300bead42dYounes Manton								0, 0, INPUT_WIDTH, INPUT_HEIGHT,
30162074f44bb5944593b65d6203deae6300bead42dYounes Manton								0, 0, output_width, output_height,
30262074f44bb5944593b65d6203deae6300bead42dYounes Manton								XVMC_FRAME_PICTURE
30362074f44bb5944593b65d6203deae6300bead42dYounes Manton							) == Success
30462074f44bb5944593b65d6203deae6300bead42dYounes Manton						);
30562074f44bb5944593b65d6203deae6300bead42dYounes Manton						break;
30662074f44bb5944593b65d6203deae6300bead42dYounes Manton					}
30762074f44bb5944593b65d6203deae6300bead42dYounes Manton					case KeyPress:
30862074f44bb5944593b65d6203deae6300bead42dYounes Manton					{
30962074f44bb5944593b65d6203deae6300bead42dYounes Manton						quit = 1;
31062074f44bb5944593b65d6203deae6300bead42dYounes Manton						break;
31162074f44bb5944593b65d6203deae6300bead42dYounes Manton					}
31262074f44bb5944593b65d6203deae6300bead42dYounes Manton				}
31362074f44bb5944593b65d6203deae6300bead42dYounes Manton			}
31462074f44bb5944593b65d6203deae6300bead42dYounes Manton		}
31562074f44bb5944593b65d6203deae6300bead42dYounes Manton	}
31662074f44bb5944593b65d6203deae6300bead42dYounes Manton
31762074f44bb5944593b65d6203deae6300bead42dYounes Manton	assert(XvMCDestroyBlocks(display, &block_array) == Success);
31862074f44bb5944593b65d6203deae6300bead42dYounes Manton	assert(XvMCDestroyMacroBlocks(display, &mb_array) == Success);
31962074f44bb5944593b65d6203deae6300bead42dYounes Manton	assert(XvMCDestroySurface(display, &surface) == Success);
32062074f44bb5944593b65d6203deae6300bead42dYounes Manton	assert(XvMCDestroyContext(display, &context) == Success);
32162074f44bb5944593b65d6203deae6300bead42dYounes Manton
32262074f44bb5944593b65d6203deae6300bead42dYounes Manton	XvUngrabPort(display, port_num, CurrentTime);
32362074f44bb5944593b65d6203deae6300bead42dYounes Manton	XDestroyWindow(display, window);
32462074f44bb5944593b65d6203deae6300bead42dYounes Manton	XCloseDisplay(display);
32562074f44bb5944593b65d6203deae6300bead42dYounes Manton
32662074f44bb5944593b65d6203deae6300bead42dYounes Manton	return 0;
32762074f44bb5944593b65d6203deae6300bead42dYounes Manton}
328