all repos — mgba @ 95f07c3131eb44a17283f8754c750a68a9a07476

mGBA Game Boy Advance Emulator

src/platform/test/cinema-main.c (view raw)

   1/* Copyright (c) 2013-2020 Jeffrey Pfau
   2 *
   3 * This Source Code Form is subject to the terms of the Mozilla Public
   4 * License, v. 2.0. If a copy of the MPL was not distributed with this
   5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
   6#include <mgba/core/config.h>
   7#include <mgba/core/core.h>
   8#include <mgba/core/log.h>
   9#include <mgba/core/version.h>
  10#include <mgba/feature/commandline.h>
  11#include <mgba/feature/video-logger.h>
  12
  13#include <mgba-util/png-io.h>
  14#include <mgba-util/string.h>
  15#include <mgba-util/table.h>
  16#include <mgba-util/threading.h>
  17#include <mgba-util/vector.h>
  18#include <mgba-util/vfs.h>
  19
  20#ifdef USE_FFMPEG
  21#include "feature/ffmpeg/ffmpeg-decoder.h"
  22#include "feature/ffmpeg/ffmpeg-encoder.h"
  23#endif
  24
  25#ifdef _MSC_VER
  26#include <mgba-util/platform/windows/getopt.h>
  27#else
  28#include <getopt.h>
  29#endif
  30
  31#include <stdlib.h>
  32#include <sys/stat.h>
  33#include <sys/types.h>
  34
  35#define MAX_TEST 200
  36#define MAX_JOBS 128
  37#define LOG_THRESHOLD 1000000
  38
  39static const struct option longOpts[] = {
  40	{ "base",       required_argument, 0, 'b' },
  41	{ "diffs",      no_argument, 0, 'd' },
  42	{ "help",       no_argument, 0, 'h' },
  43	{ "jobs",       required_argument, 0, 'j' },
  44	{ "dry-run",    no_argument, 0, 'n' },
  45	{ "outdir",     required_argument, 0, 'o' },
  46	{ "quiet",      no_argument, 0, 'q' },
  47	{ "rebaseline", no_argument, 0, 'r' },
  48	{ "rebaseline-missing", no_argument, 0, 'R' },
  49	{ "verbose",    no_argument, 0, 'v' },
  50	{ "xbaseline",  no_argument, 0, 'x' },
  51	{ "version",    no_argument, 0, '\0' },
  52	{ 0, 0, 0, 0 }
  53};
  54
  55static const char shortOpts[] = "b:dhj:no:qRrvx";
  56
  57enum CInemaStatus {
  58	CI_PASS,
  59	CI_FAIL,
  60	CI_XPASS,
  61	CI_XFAIL,
  62	CI_ERROR,
  63	CI_SKIP
  64};
  65
  66enum CInemaRebaseline {
  67	CI_R_NONE = 0,
  68	CI_R_FAILING,
  69	CI_R_MISSING,
  70};
  71
  72struct CInemaTest {
  73	char directory[PATH_MAX];
  74	char filename[MAX_TEST];
  75	char name[MAX_TEST];
  76	enum CInemaStatus status;
  77	unsigned failedFrames;
  78	uint64_t failedPixels;
  79	unsigned totalFrames;
  80	uint64_t totalDistance;
  81	uint64_t totalPixels;
  82};
  83
  84struct CInemaImage {
  85	void* data;
  86	unsigned width;
  87	unsigned height;
  88	unsigned stride;
  89};
  90
  91DECLARE_VECTOR(CInemaTestList, struct CInemaTest)
  92DEFINE_VECTOR(CInemaTestList, struct CInemaTest)
  93
  94DECLARE_VECTOR(ImageList, void*)
  95DEFINE_VECTOR(ImageList, void*)
  96
  97struct StringBuilder {
  98	struct StringList lines;
  99	struct StringList partial;
 100	unsigned repeat;
 101
 102};
 103
 104struct CInemaLogStream {
 105	struct StringBuilder err;
 106	struct StringBuilder out;
 107};
 108
 109static bool showVersion = false;
 110static bool showUsage = false;
 111static char base[PATH_MAX] = {0};
 112static char outdir[PATH_MAX] = {'.'};
 113static bool dryRun = false;
 114static bool diffs = false;
 115static enum CInemaRebaseline rebaseline = CI_R_NONE;
 116static enum CInemaRebaseline xbaseline = CI_R_NONE;
 117static int verbosity = 0;
 118
 119static struct Table configTree;
 120static Mutex configMutex;
 121
 122static int jobs = 1;
 123static size_t jobIndex = 0;
 124static Mutex jobMutex;
 125static Thread jobThreads[MAX_JOBS];
 126static int jobStatus;
 127static ThreadLocal logStream;
 128static ThreadLocal currentTest;
 129
 130bool CInemaTestInit(struct CInemaTest*, const char* directory, const char* filename);
 131void CInemaTestRun(struct CInemaTest*);
 132
 133bool CInemaConfigGetUInt(struct Table* configTree, const char* testName, const char* key, unsigned* value);
 134void CInemaConfigLoad(struct Table* configTree, const char* testName, struct mCore* core);
 135
 136static void _log(struct mLogger* log, int category, enum mLogLevel level, const char* format, va_list args);
 137
 138void CIflush(struct StringBuilder* list, FILE* file);
 139
 140static char* _compileStringList(struct StringList* list) {
 141	size_t len = 0;
 142	size_t i;
 143	for (i = 0; i < StringListSize(list); ++i) {
 144		len += strlen(*StringListGetPointer(list, i));
 145	}
 146	char* string = calloc(len + 1, sizeof(char));
 147	char* cur = string;
 148	for (i = 0; i < StringListSize(list); ++i) {
 149		char* brick = *StringListGetPointer(list, i);
 150		size_t portion = strlen(brick);
 151		memcpy(cur, brick, portion);
 152		free(brick);
 153		cur += portion;
 154	}
 155	StringListClear(list);
 156	return string;
 157}
 158
 159static void _logToStream(FILE* file, const char* format, va_list args) {
 160#ifdef HAVE_VASPRINTF
 161	struct CInemaLogStream* stream = ThreadLocalGetValue(logStream);
 162	if (!stream) {
 163		vfprintf(file, format, args);
 164	} else {
 165		struct StringBuilder* builder = &stream->out;
 166		if (file == stderr) {
 167			builder = &stream->err;
 168		}
 169		if (StringListSize(&builder->lines) > LOG_THRESHOLD) {
 170			CIflush(builder, file);
 171		}
 172		char** line = StringListAppend(&builder->partial);
 173		vasprintf(line, format, args);
 174		size_t len = strlen(*line);
 175		if (len && (*line)[len - 1] == '\n') {
 176			char* string = _compileStringList(&builder->partial);
 177			size_t linecount = StringListSize(&builder->lines);
 178			if (linecount && strcmp(string, *StringListGetPointer(&builder->lines, linecount - 1)) == 0) {
 179				++builder->repeat;
 180				free(string);
 181			} else {
 182				if (builder->repeat > 1) {
 183					asprintf(StringListAppend(&builder->lines), "The previous message was repeated %u times.\n", builder->repeat);
 184				}
 185				*StringListAppend(&builder->lines) = string;
 186				builder->repeat = 1;
 187			}
 188		}
 189	}
 190#else
 191	vfprintf(file, format, args);
 192#endif
 193}
 194
 195ATTRIBUTE_FORMAT(printf, 2, 3) void CIlog(int minlevel, const char* format, ...) {
 196	if (verbosity < minlevel) {
 197		return;
 198	}
 199	va_list args;
 200	va_start(args, format);
 201	_logToStream(stdout, format, args);
 202	va_end(args);
 203}
 204
 205ATTRIBUTE_FORMAT(printf, 2, 3) void CIerr(int minlevel, const char* format, ...) {
 206	if (verbosity < minlevel) {
 207		return;
 208	}
 209	va_list args;
 210	va_start(args, format);
 211	_logToStream(stderr, format, args);
 212	va_end(args);
 213}
 214
 215void CIflush(struct StringBuilder* builder, FILE* out) {
 216	if (StringListSize(&builder->partial)) {
 217		*StringListAppend(&builder->lines) = _compileStringList(&builder->partial);
 218	}
 219#ifdef HAVE_VASPRINTF
 220	if (builder->repeat > 1) {
 221		asprintf(StringListAppend(&builder->lines), "The previous message was repeated %u times.\n", builder->repeat);
 222	}
 223#endif
 224
 225	char* string = _compileStringList(&builder->lines);
 226	builder->repeat = 0;
 227	fputs(string, out);
 228	free(string);
 229	fflush(out);
 230}
 231
 232static bool parseCInemaArgs(int argc, char* const* argv) {
 233	int ch;
 234	int index = 0;
 235	while ((ch = getopt_long(argc, argv, shortOpts, longOpts, &index)) != -1) {
 236		const struct option* opt = &longOpts[index];
 237		switch (ch) {
 238		case '\0':
 239			if (strcmp(opt->name, "version") == 0) {
 240				showVersion = true;
 241			} else {
 242				return false;
 243			}
 244			break;
 245		case 'b':
 246			strlcpy(base, optarg, sizeof(base));
 247			// TODO: Verify path exists
 248			break;
 249		case 'd':
 250			diffs = true;
 251			break;
 252		case 'h':
 253			showUsage = true;
 254			break;
 255		case 'j':
 256			jobs = atoi(optarg);
 257			if (jobs > MAX_JOBS) {
 258				jobs = MAX_JOBS;
 259			}
 260			if (jobs < 1) {
 261				jobs = 1;
 262			}
 263			break;
 264		case 'n':
 265			dryRun = true;
 266			break;
 267		case 'o':
 268			strlcpy(outdir, optarg, sizeof(outdir));
 269			// TODO: Make directory
 270			break;
 271		case 'q':
 272			--verbosity;
 273			break;
 274		case 'r':
 275			rebaseline = CI_R_FAILING;
 276			break;
 277		case 'R':
 278			rebaseline = CI_R_MISSING;
 279			break;
 280		case 'v':
 281			++verbosity;
 282			break;
 283		case 'x':
 284			xbaseline = CI_R_FAILING;
 285			break;
 286		default:
 287			return false;
 288		}
 289	}
 290
 291	return true;
 292}
 293
 294static void usageCInema(const char* arg0) {
 295	printf("usage: %s [-dhnqrRv] [-j JOBS] [-b BASE] [-o DIR] [--version] [test...]\n", arg0);
 296	puts("  -b, --base BASE            Path to the CInema base directory");
 297	puts("  -d, --diffs                Output image diffs from failures");
 298	puts("  -h, --help                 Print this usage and exit");
 299	puts("  -j, --jobs JOBS            Run a number of jobs in parallel");
 300	puts("  -n, --dry-run              List all collected tests instead of running them");
 301	puts("  -o, --output DIR           Path to output applicable results");
 302	puts("  -q, --quiet                Decrease log verbosity (can be repeated)");
 303	puts("  -r, --rebaseline           Rewrite the baseline for failing tests");
 304	puts("  -R, --rebaseline-missing   Write missing baselines tests only");
 305	puts("  -v, --verbose              Increase log verbosity (can be repeated)");
 306	puts("  -x, --xbaseline            Rewrite the xfail baselines for failing tests");
 307	puts("  --version                  Print version and exit");
 308}
 309
 310static bool determineBase(int argc, char* const* argv) {
 311	// TODO: Better dynamic detection
 312	separatePath(__FILE__, base, NULL, NULL);
 313	strncat(base, PATH_SEP ".." PATH_SEP ".." PATH_SEP ".." PATH_SEP "cinema", sizeof(base) - strlen(base) - 1);
 314	return true;
 315}
 316
 317static bool collectTests(struct CInemaTestList* tests, const char* path) {
 318	CIerr(2, "Considering path %s\n", path);
 319	struct VDir* dir = VDirOpen(path);
 320	if (!dir) {
 321		return false;
 322	}
 323	struct VDirEntry* entry = dir->listNext(dir);
 324	while (entry) {
 325		char subpath[PATH_MAX];
 326		snprintf(subpath, sizeof(subpath), "%s" PATH_SEP "%s", path, entry->name(entry));
 327		if (entry->type(entry) == VFS_DIRECTORY && strncmp(entry->name(entry), ".", 2) != 0 && strncmp(entry->name(entry), "..", 3) != 0) {
 328			if (!collectTests(tests, subpath)) {
 329				dir->close(dir);
 330				return false;
 331			}
 332		} else if (entry->type(entry) == VFS_FILE && strncmp(entry->name(entry), "test.", 5) == 0) {
 333			CIerr(3, "Found potential test %s\n", subpath);
 334			struct VFile* vf = dir->openFile(dir, entry->name(entry), O_RDONLY);
 335			if (vf) {
 336				if (mCoreIsCompatible(vf) != PLATFORM_NONE || mVideoLogIsCompatible(vf) != PLATFORM_NONE) {
 337					struct CInemaTest* test = CInemaTestListAppend(tests);
 338					if (!CInemaTestInit(test, path, entry->name(entry))) {
 339						CIerr(3, "Failed to create test\n");
 340						CInemaTestListResize(tests, -1);
 341					} else {
 342						CIerr(2, "Found test %s\n", test->name);
 343					}
 344				} else {
 345					CIerr(3, "Not a compatible file\n");
 346				}
 347				vf->close(vf);
 348			} else {
 349				CIerr(3, "Failed to open file\n");
 350			}
 351		}
 352		entry = dir->listNext(dir);
 353	}
 354	dir->close(dir);
 355	return true;
 356}
 357
 358static int _compareNames(const void* a, const void* b) {
 359	const struct CInemaTest* ta = a;
 360	const struct CInemaTest* tb = b;
 361
 362	return strncmp(ta->name, tb->name, sizeof(ta->name));
 363}
 364
 365static void reduceTestList(struct CInemaTestList* tests) {
 366	qsort(CInemaTestListGetPointer(tests, 0), CInemaTestListSize(tests), sizeof(struct CInemaTest), _compareNames);
 367
 368	size_t i;
 369	for (i = 1; i < CInemaTestListSize(tests);) {
 370		struct CInemaTest* cur = CInemaTestListGetPointer(tests, i);
 371		struct CInemaTest* prev = CInemaTestListGetPointer(tests, i - 1);
 372		if (strncmp(cur->name, prev->name, sizeof(cur->name)) != 0) {
 373			++i;
 374			continue;
 375		}
 376		CInemaTestListShift(tests, i, 1);
 377	}
 378}
 379
 380static void testToPath(const char* testName, char* path) {
 381	strlcpy(path, base, PATH_MAX);
 382
 383	bool dotSeen = true;
 384	size_t i;
 385	for (i = strlen(path); testName[0] && i < PATH_MAX; ++testName) {
 386		if (testName[0] == '.') {
 387			dotSeen = true;
 388		} else {
 389			if (dotSeen) {
 390				strlcpy(&path[i], PATH_SEP, PATH_MAX - i);
 391				i += strlen(PATH_SEP);
 392				dotSeen = false;
 393				if (!i) {
 394					break;
 395				}
 396			}
 397			path[i] = testName[0];
 398			++i;
 399		}
 400	}
 401	if (i == PATH_MAX) {
 402		--i;
 403	}
 404	path[i] = '\0';
 405}
 406
 407static bool globTests(struct CInemaTestList* tests, const char* glob, const char* ancestors) {
 408	bool success = true;
 409	const char* next = strpbrk(glob, "*.");
 410
 411	char path[PATH_MAX];
 412	if (!next) {
 413		testToPath(glob, path);
 414		return collectTests(tests, path);
 415	} else if (next[0] == '.') {
 416		char subtest[MAX_TEST];
 417		if (!ancestors) {
 418			strncpy(subtest, glob, next - glob);
 419		} else {
 420			size_t len = strlen(ancestors) + (next - glob) + 2;
 421			if (len > sizeof(subtest)) {
 422				len = sizeof(subtest);
 423			}
 424			snprintf(subtest, len, "%s.%s", ancestors, glob);
 425		}
 426		return globTests(tests, next + 1, subtest);
 427	} else if (next[0] == '*') {
 428		char globBuffer[MAX_TEST];
 429		const char* subglob;
 430
 431		next = strchr(next, '.');
 432		if (!next) {
 433			subglob = glob;
 434		} else {
 435			size_t len = next - glob + 1;
 436			if (len > sizeof(globBuffer)) {
 437				len = sizeof(globBuffer);
 438			}
 439			strncpy(globBuffer, glob, len - 1);
 440			subglob = globBuffer;
 441		}
 442		bool hasMoreGlobs = next && strchr(next, '*');
 443
 444		struct VDir* dir;
 445		if (ancestors) {
 446			testToPath(ancestors, path);
 447			dir = VDirOpen(path);
 448		} else {
 449			dir = VDirOpen(base);
 450		}
 451		if (!dir) {
 452			return false;
 453		}
 454
 455		struct VDirEntry* dirent = dir->listNext(dir);
 456		while (dirent) {
 457			const char* name = dirent->name(dirent);
 458			if (dirent->type(dirent) != VFS_DIRECTORY || strncmp(name, ".", 2) == 0 || strncmp(name, "..", 3) == 0) {
 459				dirent = dir->listNext(dir);
 460				continue;
 461			}
 462			if (wildcard(subglob, name)) {
 463				char newgen[MAX_TEST];
 464				if (ancestors) {
 465					snprintf(newgen, sizeof(newgen), "%s.%s", ancestors, name);
 466				} else {
 467					strlcpy(newgen, name, sizeof(newgen));
 468				}
 469				if (next && hasMoreGlobs) {
 470					globTests(tests, next + 1, newgen);
 471				} else {
 472					testToPath(newgen, path);
 473					collectTests(tests, path);
 474				}
 475			}
 476			dirent = dir->listNext(dir);
 477		}
 478
 479		return true;
 480	} else {
 481		abort();
 482	}
 483}
 484
 485static void _loadConfigTree(struct Table* configTree, const char* testName) {
 486	char key[MAX_TEST];
 487	strlcpy(key, testName, sizeof(key));
 488
 489	struct mCoreConfig* config;
 490	while (!(config = HashTableLookup(configTree, key))) {
 491		char path[PATH_MAX];
 492		config = malloc(sizeof(*config));
 493		mCoreConfigInit(config, "cinema");
 494		testToPath(key, path);
 495		strncat(path, PATH_SEP, sizeof(path) - 1);
 496		strncat(path, "config.ini", sizeof(path) - 1);
 497		mCoreConfigLoadPath(config, path);
 498		HashTableInsert(configTree, key, config);
 499		char* pos = strrchr(key, '.');
 500		if (pos) {
 501			pos[0] = '\0';
 502		} else if (key[0]) {
 503			key[0] = '\0';
 504		} else {
 505			break;
 506		}
 507	}
 508}
 509
 510static void _unloadConfigTree(const char* key, void* value, void* user) {
 511	UNUSED(key);
 512	UNUSED(user);
 513	mCoreConfigDeinit(value);
 514}
 515
 516static const char* CInemaConfigGet(struct Table* configTree, const char* testName, const char* key) {
 517	_loadConfigTree(configTree, testName);
 518
 519	char testKey[MAX_TEST];
 520	strlcpy(testKey, testName, sizeof(testKey));
 521
 522	struct mCoreConfig* config;
 523	while (true) {
 524		config = HashTableLookup(configTree, testKey);
 525		if (!config) {
 526			continue;
 527		}
 528		const char* str = ConfigurationGetValue(&config->configTable, "testinfo", key);
 529		if (str) {
 530			return str;
 531		}
 532		char* pos = strrchr(testKey, '.');
 533		if (pos) {
 534			pos[0] = '\0';
 535		} else if (testKey[0]) {
 536			testKey[0] = '\0';
 537		} else {
 538			break;
 539		}
 540	}
 541	return NULL;
 542}
 543
 544bool CInemaConfigGetUInt(struct Table* configTree, const char* testName, const char* key, unsigned* out) {
 545	const char* charValue = CInemaConfigGet(configTree, testName, key);
 546	if (!charValue) {
 547		return false;
 548	}
 549	char* end;
 550	unsigned long value = strtoul(charValue, &end, 10);
 551	if (*end) {
 552		return false;
 553	}
 554	*out = value;
 555	return true;
 556}
 557
 558void CInemaConfigLoad(struct Table* configTree, const char* testName, struct mCore* core) {
 559	_loadConfigTree(configTree, testName);
 560
 561	char testKey[MAX_TEST] = {0};
 562	char* keyEnd = testKey;
 563	const char* pos;
 564	while (true) {
 565		pos = strchr(testName, '.');
 566		size_t maxlen = sizeof(testKey) - (keyEnd - testKey) - 1;
 567		size_t len;
 568		if (pos) {
 569			len = pos - testName;
 570		} else {
 571			len = strlen(testName);
 572		}
 573		if (len > maxlen) {
 574			len = maxlen;
 575		}
 576		strncpy(keyEnd, testName, len);
 577		keyEnd += len;
 578
 579		struct mCoreConfig* config = HashTableLookup(configTree, testKey);
 580		if (config) {
 581			core->loadConfig(core, config);
 582		}
 583		if (!pos) {
 584			break;
 585		}
 586		testName = pos + 1;
 587		keyEnd[0] = '.';
 588		++keyEnd;
 589	}
 590}
 591
 592bool CInemaTestInit(struct CInemaTest* test, const char* directory, const char* filename) {
 593	if (strncmp(base, directory, strlen(base)) != 0) {
 594		return false;
 595	}
 596	memset(test, 0, sizeof(*test));
 597	strlcpy(test->directory, directory, sizeof(test->directory));
 598	strlcpy(test->filename, filename, sizeof(test->filename));
 599	directory += strlen(base) + 1;
 600	strlcpy(test->name, directory, sizeof(test->name));
 601	char* str = strstr(test->name, PATH_SEP);
 602	while (str) {
 603		str[0] = '.';
 604		str = strstr(str, PATH_SEP);
 605	}
 606	return true;
 607}
 608
 609static bool _loadBaselinePNG(struct VDir* dir, const char* type, struct CInemaImage* image, size_t frame, enum CInemaStatus* status) {
 610	char baselineName[32];
 611	snprintf(baselineName, sizeof(baselineName), "%s_%04" PRIz "u.png", type, frame);
 612	struct VFile* baselineVF = dir->openFile(dir, baselineName, O_RDONLY);
 613	if (!baselineVF) {
 614		if (*status == CI_PASS) {
 615			*status = CI_FAIL;
 616		}
 617		return false;
 618	}
 619
 620	png_structp png = PNGReadOpen(baselineVF, 0);
 621	png_infop info = png_create_info_struct(png);
 622	png_infop end = png_create_info_struct(png);
 623	if (!png || !info || !end || !PNGReadHeader(png, info)) {
 624		PNGReadClose(png, info, end);
 625		baselineVF->close(baselineVF);
 626		CIerr(1, "Failed to load %s\n", baselineName);
 627		*status = CI_ERROR;
 628		return false;
 629	}
 630
 631	unsigned pwidth = png_get_image_width(png, info);
 632	unsigned pheight = png_get_image_height(png, info);
 633	if (pheight != image->height || pwidth != image->width) {
 634		PNGReadClose(png, info, end);
 635		baselineVF->close(baselineVF);
 636		CIlog(1, "Size mismatch for %s, expected %ux%u, got %ux%u\n", baselineName, pwidth, pheight, image->width, image->height);
 637		if (*status == CI_PASS) {
 638			*status = CI_FAIL;
 639		}
 640		return false;
 641	}
 642
 643	image->data = malloc(pwidth * pheight * BYTES_PER_PIXEL);
 644	if (!image->data) {
 645		CIerr(1, "Failed to allocate baseline buffer\n");
 646		*status = CI_ERROR;
 647		PNGReadClose(png, info, end);
 648		baselineVF->close(baselineVF);
 649		return false;
 650	}
 651	if (!PNGReadPixels(png, info, image->data, pwidth, pheight, pwidth) || !PNGReadFooter(png, end)) {
 652		CIerr(1, "Failed to read %s\n", baselineName);
 653		*status = CI_ERROR;
 654		free(image->data);
 655		return false;
 656	}
 657	PNGReadClose(png, info, end);
 658	baselineVF->close(baselineVF);
 659	image->stride = pwidth;
 660	return true;
 661}
 662
 663#ifdef USE_FFMPEG
 664struct CInemaStream {
 665	struct mAVStream d;
 666	struct CInemaImage* image;
 667	enum CInemaStatus* status;
 668};
 669
 670static void _cinemaDimensionsChanged(struct mAVStream* stream, unsigned width, unsigned height) {
 671	struct CInemaStream* cistream = (struct CInemaStream*) stream;
 672	if (height != cistream->image->height || width != cistream->image->width) {
 673		CIlog(1, "Size mismatch for video, expected %ux%u, got %ux%u\n", width, height, cistream->image->width, cistream->image->height);
 674		if (*cistream->status == CI_PASS) {
 675			*cistream->status = CI_FAIL;
 676		}
 677	}
 678}
 679
 680static void _cinemaVideoFrame(struct mAVStream* stream, const color_t* pixels, size_t stride) {
 681	struct CInemaStream* cistream = (struct CInemaStream*) stream;
 682	cistream->image->stride = stride;
 683	size_t bufferSize = cistream->image->stride * cistream->image->height * BYTES_PER_PIXEL;
 684	cistream->image->data = malloc(bufferSize);
 685	memcpy(cistream->image->data, pixels, bufferSize);
 686}
 687#endif
 688
 689static struct VDir* _makeOutDir(const char* testName) {
 690	char path[PATH_MAX] = {0};
 691	strlcpy(path, outdir, sizeof(path));
 692	char* pathEnd = path + strlen(path);
 693	const char* pos;
 694	while (true) {
 695		pathEnd[0] = PATH_SEP[0];
 696		++pathEnd;
 697		pos = strchr(testName, '.');
 698		size_t maxlen = sizeof(path) - (pathEnd - path) - 1;
 699		size_t len;
 700		if (pos) {
 701			len = pos - testName;
 702		} else {
 703			len = strlen(testName);
 704		}
 705		if (len > maxlen) {
 706			len = maxlen;
 707		}
 708		strncpy(pathEnd, testName, len);
 709		pathEnd += len;
 710
 711		mkdir(path, 0777);
 712
 713		if (!pos) {
 714			break;
 715		}
 716		testName = pos + 1;
 717	}
 718	return VDirOpen(path);
 719}
 720
 721static void _writeImage(struct VFile* vf, const struct CInemaImage* image) {
 722	png_structp png = PNGWriteOpen(vf);
 723	png_infop info = PNGWriteHeader(png, image->width, image->height);
 724	if (!PNGWritePixels(png, image->width, image->height, image->stride, image->data)) {
 725		CIerr(0, "Could not write output image\n");
 726	}
 727	PNGWriteClose(png, info);
 728
 729	vf->close(vf);
 730}
 731
 732static void _writeDiff(const char* testName, const struct CInemaImage* image, size_t frame, const char* type) {
 733	struct VDir* dir = _makeOutDir(testName);
 734	if (!dir) {
 735		CIerr(0, "Could not open directory for %s\n", testName);
 736		return;
 737	}
 738	char name[32];
 739	snprintf(name, sizeof(name), "%s_%04" PRIz "u.png", type, frame);
 740	struct VFile* vf = dir->openFile(dir, name, O_CREAT | O_TRUNC | O_WRONLY);
 741	if (!vf) {
 742		CIerr(0, "Could not open output file %s\n", name);
 743		dir->close(dir);
 744		return;
 745	}
 746	_writeImage(vf, image);
 747	dir->close(dir);
 748}
 749
 750static void _writeBaseline(struct VDir* dir, const char* type, const struct CInemaImage* image, size_t frame) {
 751	char baselineName[32];
 752	snprintf(baselineName, sizeof(baselineName), "%s_%04" PRIz "u.png", type, frame);
 753	struct VFile* baselineVF = dir->openFile(dir, baselineName, O_CREAT | O_TRUNC | O_WRONLY);
 754	if (baselineVF) {
 755		_writeImage(baselineVF, image);
 756	} else {
 757		CIerr(0, "Could not open output file %s\n", baselineName);
 758	}
 759}
 760
 761static bool _updateInput(struct mCore* core, size_t frame, const char** input) {
 762	if (!*input || !*input[0]) {
 763		return false;
 764	}
 765	char* end;
 766	uint32_t start = strtoul(*input, &end, 10);
 767	if (end[0] != ':') {
 768		return false;
 769	}
 770	if (start != frame) {
 771		return true;
 772	}
 773	++end;
 774	*input = end;
 775	uint32_t keys = strtoul(*input, &end, 16);
 776	if (end[0] == ',') {
 777		++end;
 778	}
 779	*input = end;
 780	core->setKeys(core, keys);
 781	return true;
 782}
 783
 784static bool _compareImages(struct CInemaTest* restrict test, const struct CInemaImage* restrict image, const struct CInemaImage* restrict expected, int* restrict max, uint8_t** restrict outdiff) {
 785	const uint8_t* testPixels = image->data;
 786	const uint8_t* expectPixels = expected->data;
 787	uint8_t* diff = NULL;
 788	size_t x;
 789	size_t y;
 790	bool failed = false;
 791	for (y = 0; y < image->height; ++y) {
 792		for (x = 0; x < image->width; ++x) {
 793			size_t pix = expected->stride * y + x;
 794			size_t tpix = image->stride * y + x;
 795			int testR = testPixels[tpix * 4 + 0];
 796			int testG = testPixels[tpix * 4 + 1];
 797			int testB = testPixels[tpix * 4 + 2];
 798			int expectR = expectPixels[pix * 4 + 0];
 799			int expectG = expectPixels[pix * 4 + 1];
 800			int expectB = expectPixels[pix * 4 + 2];
 801			int r = expectR - testR;
 802			int g = expectG - testG;
 803			int b = expectB - testB;
 804			if (r | g | b) {
 805				failed = true;
 806				if (outdiff && !diff) {
 807					diff = calloc(expected->stride * expected->height, BYTES_PER_PIXEL);
 808					*outdiff = diff;
 809				}
 810				test->status = CI_FAIL;
 811				if (r < 0) {
 812					r = -r;
 813				}
 814				if (g < 0) {
 815					g = -g;
 816				}
 817				if (b < 0) {
 818					b = -b;
 819				}
 820
 821				if (diff) {
 822					if (r > *max) {
 823						*max = r;
 824					}
 825					if (g > *max) {
 826						*max = g;
 827					}
 828					if (b > *max) {
 829						*max = b;
 830					}
 831					diff[pix * 4 + 0] = r;
 832					diff[pix * 4 + 1] = g;
 833					diff[pix * 4 + 2] = b;
 834				}
 835
 836				if (test) {
 837					test->totalDistance += r + g + b;
 838					++test->failedPixels;
 839				}
 840			}
 841		}
 842	}
 843	return !failed;
 844}
 845
 846void _writeDiffSet(struct CInemaImage* expected, const char* name, uint8_t* diff, int frame, int max, bool xfail) {
 847	struct CInemaImage outdiff = {
 848		.data = diff,
 849		.width = expected->width,
 850		.height = expected->height,
 851		.stride = expected->stride,
 852	};
 853
 854	if (xfail) {
 855		_writeDiff(name, expected, frame, "xexpected");
 856		_writeDiff(name, &outdiff, frame, "xdiff");
 857	} else {
 858		_writeDiff(name, expected, frame, "expected");
 859		_writeDiff(name, &outdiff, frame, "diff");
 860	}
 861
 862	size_t x;
 863	size_t y;
 864	for (y = 0; y < outdiff.height; ++y) {
 865		for (x = 0; x < outdiff.width; ++x) {
 866			size_t pix = outdiff.stride * y + x;
 867			diff[pix * 4 + 0] = diff[pix * 4 + 0] * 255 / max;
 868			diff[pix * 4 + 1] = diff[pix * 4 + 1] * 255 / max;
 869			diff[pix * 4 + 2] = diff[pix * 4 + 2] * 255 / max;
 870		}
 871	}
 872	if (xfail) {
 873		_writeDiff(name, &outdiff, frame, "xnormalized");
 874	}
 875}
 876
 877#ifdef USE_FFMPEG
 878static void _replayBaseline(struct CInemaTest* test, struct FFmpegEncoder* encoder, const struct CInemaImage* image, int frame) {
 879	char baselineName[PATH_MAX];
 880	snprintf(baselineName, sizeof(baselineName), "%s" PATH_SEP ".baseline.avi", test->directory);
 881
 882	if (!FFmpegEncoderOpen(encoder, baselineName)) {
 883		CIerr(1, "Failed to save baseline video\n");
 884		test->status = CI_ERROR;
 885		return;
 886	}
 887	encoder->d.videoDimensionsChanged(&encoder->d, image->width, image->height);
 888
 889	snprintf(baselineName, sizeof(baselineName), "%s" PATH_SEP "baseline.avi", test->directory);
 890
 891	struct CInemaImage buffer = {
 892		.data = NULL,
 893		.width = image->width,
 894		.height = image->height,
 895		.stride = image->width,
 896	};
 897	struct FFmpegDecoder decoder;
 898	struct CInemaStream stream = {0};
 899	stream.d.postVideoFrame = _cinemaVideoFrame;
 900	stream.d.videoDimensionsChanged = _cinemaDimensionsChanged;
 901	stream.status = &test->status;
 902	stream.image = &buffer;
 903
 904	FFmpegDecoderInit(&decoder);
 905	decoder.out = &stream.d;
 906
 907	if (!FFmpegDecoderOpen(&decoder, baselineName)) {
 908		CIerr(1, "Failed to load baseline video\n");
 909		test->status = CI_ERROR;
 910		return;
 911	}
 912
 913	int i;
 914	for (i = 0; i < frame; ++i) {
 915		while (!buffer.data) {
 916			if (!FFmpegDecoderRead(&decoder)) {
 917				CIlog(1, "Failed to read more frames. EOF?\n");
 918				test->status = CI_FAIL;
 919				break;
 920			}
 921		}
 922		encoder->d.postVideoFrame(&encoder->d, buffer.data, buffer.stride);
 923		free(buffer.data);
 924		buffer.data = NULL;
 925	}
 926	FFmpegDecoderClose(&decoder);
 927}
 928#endif
 929
 930void CInemaTestRun(struct CInemaTest* test) {
 931	unsigned ignore = 0;
 932	MutexLock(&configMutex);
 933	CInemaConfigGetUInt(&configTree, test->name, "ignore", &ignore);
 934	MutexUnlock(&configMutex);
 935	if (ignore) {
 936		test->status = CI_SKIP;
 937		return;
 938	}
 939
 940	struct VDir* dir = VDirOpen(test->directory);
 941	if (!dir) {
 942		CIerr(0, "Failed to open test directory\n");
 943		test->status = CI_ERROR;
 944		return;
 945	}
 946	struct VFile* rom = dir->openFile(dir, test->filename, O_RDONLY);
 947	if (!rom) {
 948		CIerr(0, "Failed to open test\n");
 949		test->status = CI_ERROR;
 950		return;
 951	}
 952	struct mCore* core = mCoreFindVF(rom);
 953	if (!core) {
 954		CIerr(0, "Failed to load test\n");
 955		test->status = CI_ERROR;
 956		rom->close(rom);
 957		return;
 958	}
 959	if (!core->init(core)) {
 960		CIerr(0, "Failed to init test\n");
 961		test->status = CI_ERROR;
 962		core->deinit(core);
 963		return;
 964	}
 965	struct CInemaImage image;
 966	core->desiredVideoDimensions(core, &image.width, &image.height);
 967	ssize_t bufferSize = image.width * image.height * BYTES_PER_PIXEL;
 968	image.data = malloc(bufferSize);
 969	image.stride = image.width;
 970	if (!image.data) {
 971		CIerr(0, "Failed to allocate video buffer\n");
 972		test->status = CI_ERROR;
 973		core->deinit(core);
 974	}
 975	core->setVideoBuffer(core, image.data, image.stride);
 976	mCoreConfigInit(&core->config, "cinema");
 977
 978	unsigned limit = 9999;
 979	unsigned skip = 0;
 980	unsigned fail = 0;
 981	unsigned video = 0;
 982	const char* input = NULL;
 983
 984	MutexLock(&configMutex);
 985	CInemaConfigGetUInt(&configTree, test->name, "frames", &limit);
 986	CInemaConfigGetUInt(&configTree, test->name, "skip", &skip);
 987	CInemaConfigGetUInt(&configTree, test->name, "fail", &fail);
 988	CInemaConfigGetUInt(&configTree, test->name, "video", &video);
 989	input = CInemaConfigGet(&configTree, test->name, "input");
 990	CInemaConfigLoad(&configTree, test->name, core);
 991	MutexUnlock(&configMutex);
 992
 993	struct VFile* save = VFileMemChunk(NULL, 0);
 994	core->loadROM(core, rom);
 995	if (!core->loadSave(core, save)) {
 996		save->close(save);
 997	}
 998	core->rtc.override = RTC_FAKE_EPOCH;
 999	core->rtc.value = 1200000000;
1000	core->reset(core);
1001
1002	test->status = CI_PASS;
1003
1004	unsigned minFrame = core->frameCounter(core);
1005	size_t frame;
1006	for (frame = 0; frame < skip; ++frame) {
1007		core->runFrame(core);
1008	}
1009	core->desiredVideoDimensions(core, &image.width, &image.height);
1010
1011#ifdef USE_FFMPEG
1012	struct FFmpegDecoder decoder;
1013	struct FFmpegEncoder encoder;
1014	struct CInemaStream stream = {0};
1015
1016	char baselineName[PATH_MAX];
1017	snprintf(baselineName, sizeof(baselineName), "%s" PATH_SEP "baseline.avi", test->directory);
1018	bool exists = access(baselineName, 0) == 0;
1019
1020	if (video) {
1021		FFmpegEncoderInit(&encoder);
1022		FFmpegDecoderInit(&decoder);
1023
1024		FFmpegEncoderSetAudio(&encoder, NULL, 0);
1025		FFmpegEncoderSetVideo(&encoder, "zmbv", 0, 0);
1026		FFmpegEncoderSetContainer(&encoder, "avi");
1027		FFmpegEncoderSetDimensions(&encoder, image.width, image.height);
1028
1029		if (rebaseline && !exists) {
1030			if (!FFmpegEncoderOpen(&encoder, baselineName)) {
1031				CIerr(1, "Failed to save baseline video\n");
1032			} else {
1033				core->setAVStream(core, &encoder.d);
1034			}
1035		}
1036
1037		if (exists) {
1038			stream.d.postVideoFrame = _cinemaVideoFrame;
1039			stream.d.videoDimensionsChanged = _cinemaDimensionsChanged;
1040			stream.status = &test->status;
1041			decoder.out = &stream.d;
1042
1043			if (!FFmpegDecoderOpen(&decoder, baselineName)) {
1044				CIerr(1, "Failed to load baseline video\n");
1045			}
1046		} else if (!rebaseline) {
1047			test->status = CI_FAIL;
1048		}
1049	}
1050#else
1051	if (video) {
1052		CIerr(0, "Failed to run video test without ffmpeg linked in\n");
1053		test->status = CI_ERROR;
1054	}
1055#endif
1056
1057	bool xdiff = false;
1058	for (frame = 0; limit; ++frame, --limit) {
1059		_updateInput(core, frame, &input);
1060		core->runFrame(core);
1061		++test->totalFrames;
1062		unsigned frameCounter = core->frameCounter(core);
1063		if (frameCounter <= minFrame) {
1064			break;
1065		}
1066		if (test->status == CI_ERROR) {
1067			break;
1068		}
1069		CIlog(3, "Test frame: %u\n", frameCounter);
1070		core->desiredVideoDimensions(core, &image.width, &image.height);
1071		uint8_t* diff = NULL;
1072		struct CInemaImage expected = {
1073			.data = NULL,
1074			.width = image.width,
1075			.height = image.height,
1076			.stride = image.width,
1077		};
1078		bool baselineFound;
1079		if (video) {
1080			baselineFound = false;
1081#ifdef USE_FFMPEG
1082			if (FFmpegDecoderIsOpen(&decoder)) {
1083				stream.image = &expected;
1084				while (!expected.data) {
1085					if (!FFmpegDecoderRead(&decoder)) {
1086						CIlog(1, "Failed to read more frames. EOF?\n");
1087						test->status = CI_FAIL;
1088						if (rebaseline && !FFmpegEncoderIsOpen(&encoder)) {
1089							_replayBaseline(test, &encoder, &image, frame);
1090							if (test->status == CI_ERROR) {
1091								break;
1092							}
1093							encoder.d.postVideoFrame(&encoder.d, image.data, image.stride);
1094							core->setAVStream(core, &encoder.d);
1095						}
1096						break;
1097					}
1098				}
1099				baselineFound = expected.data;
1100			}
1101#endif
1102		} else {
1103			baselineFound = _loadBaselinePNG(dir, "baseline", &expected, frame, &test->status);
1104		}
1105		if (test->status == CI_ERROR) {
1106			break;
1107		}
1108		bool failed = false;
1109		if (baselineFound) {
1110			int max = 0;
1111			failed = !_compareImages(test, &image, &expected, &max, diffs ? &diff : NULL);
1112			if (failed) {
1113				++test->failedFrames;
1114#ifdef USE_FFMPEG
1115				if (video && exists && rebaseline && !FFmpegEncoderIsOpen(&encoder)) {
1116					_replayBaseline(test, &encoder, &image, frame);
1117					if (test->status == CI_ERROR) {
1118						break;
1119					}
1120					encoder.d.postVideoFrame(&encoder.d, image.data, image.stride);
1121					core->setAVStream(core, &encoder.d);
1122				}
1123#endif
1124			}
1125			test->totalPixels += image.height * image.width;
1126			if (rebaseline == CI_R_FAILING && !video && failed) {
1127				_writeBaseline(dir, "baseline", &image, frame);
1128			}
1129			if (diff) {
1130				if (failed) {
1131					_writeDiff(test->name, &image, frame, "result");
1132					_writeDiffSet(&expected, test->name, diff, frame, max, false);
1133				}
1134				free(diff);
1135				diff = NULL;
1136			}
1137			free(expected.data);
1138		} else if (rebaseline && !video) {
1139			_writeBaseline(dir, "baseline", &image, frame);
1140		} else if (!rebaseline) {
1141			test->status = CI_FAIL;
1142		}
1143
1144		if (fail && failed) {
1145			if (video) {
1146				// TODO
1147				baselineFound = false;
1148			} else {
1149				baselineFound = _loadBaselinePNG(dir, "xbaseline", &expected, frame, &test->status);
1150			}
1151
1152			if (baselineFound) {
1153				int max = 0;
1154				failed = !_compareImages(test, &image, &expected, &max, diffs ? &diff : NULL);
1155				if (diff) {
1156					if (failed) {
1157						_writeDiffSet(&expected, test->name, diff, frame, max, true);
1158					}
1159					free(diff);
1160					diff = NULL;
1161				}
1162				if (failed) {
1163					if (xbaseline == CI_R_FAILING && !video) {
1164						_writeBaseline(dir, "xbaseline", &image, frame);
1165					}
1166					xdiff = true;
1167				}
1168				free(expected.data);
1169			} else if (xbaseline && !video) {
1170				_writeBaseline(dir, "xbaseline", &image, frame);
1171			}
1172		}
1173	}
1174
1175#ifdef USE_FFMPEG
1176	if (video) {
1177		if (FFmpegEncoderIsOpen(&encoder)) {
1178			FFmpegEncoderClose(&encoder);
1179			if (exists && rebaseline) {
1180				char tmpBaselineName[PATH_MAX];
1181				snprintf(tmpBaselineName, sizeof(tmpBaselineName), "%s" PATH_SEP ".baseline.avi", test->directory);
1182#ifdef _WIN32
1183				MoveFileEx(tmpBaselineName, baselineName, MOVEFILE_REPLACE_EXISTING);
1184#else
1185				rename(tmpBaselineName, baselineName);
1186#endif
1187			}
1188		}
1189		if (FFmpegDecoderIsOpen(&decoder)) {
1190			FFmpegDecoderClose(&decoder);
1191		}
1192	}
1193#endif
1194
1195	if (fail) {
1196		if (test->status == CI_FAIL && !xdiff) {
1197			test->status = CI_XFAIL;
1198		} else if (test->status == CI_PASS) {
1199			test->status = CI_XPASS;
1200		}
1201	}
1202
1203	free(image.data);
1204	mCoreConfigDeinit(&core->config);
1205	core->deinit(core);
1206	dir->close(dir);
1207}
1208
1209static bool CInemaTask(struct CInemaTestList* tests, size_t i) {
1210	bool success = true;
1211	struct CInemaTest* test = CInemaTestListGetPointer(tests, i);
1212	if (dryRun) {
1213		CIlog(-1, "%s\n", test->name);
1214	} else {
1215		CIlog(1, "%s: ", test->name);
1216		fflush(stdout);
1217		ThreadLocalSetKey(currentTest, test);
1218		CInemaTestRun(test);
1219		ThreadLocalSetKey(currentTest, NULL);
1220
1221		switch (test->status) {
1222		case CI_PASS:
1223			CIlog(1, "pass\n");
1224			break;
1225		case CI_FAIL:
1226			success = false;
1227			CIlog(1, "fail\n");
1228			break;
1229		case CI_XPASS:
1230			CIlog(1, "xpass\n");
1231			break;
1232		case CI_XFAIL:
1233			CIlog(1, "xfail\n");
1234			break;
1235		case CI_SKIP:
1236			CIlog(1, "skip\n");
1237			break;
1238		case CI_ERROR:
1239			success = false;
1240			CIlog(1, "error\n");
1241			break;
1242		}
1243		if (test->failedFrames) {
1244			CIlog(2, "\tfailed frames: %u/%u (%1.3g%%)\n", test->failedFrames, test->totalFrames, test->failedFrames / (test->totalFrames * 0.01));
1245			CIlog(2, "\tfailed pixels: %" PRIu64 "/%" PRIu64 " (%1.3g%%)\n", test->failedPixels, test->totalPixels, test->failedPixels / (test->totalPixels * 0.01));
1246			CIlog(2, "\tdistance: %" PRIu64 "/%" PRIu64 " (%1.3g%%)\n", test->totalDistance, test->totalPixels * 765, test->totalDistance / (test->totalPixels * 7.65));
1247		}
1248	}
1249	return success;
1250}
1251
1252static THREAD_ENTRY CInemaJob(void* context) {
1253	struct CInemaTestList* tests = context;
1254	struct CInemaLogStream stream;
1255	StringListInit(&stream.out.lines, 0);
1256	StringListInit(&stream.out.partial, 0);
1257	stream.out.repeat = 0;
1258	StringListInit(&stream.err.lines, 0);
1259	StringListInit(&stream.err.partial, 0);
1260	stream.err.repeat = 0;
1261	ThreadLocalSetKey(logStream, &stream);
1262
1263	bool success = true;
1264	while (true) {
1265		size_t i;
1266		MutexLock(&jobMutex);
1267		i = jobIndex;
1268		++jobIndex;
1269		MutexUnlock(&jobMutex);
1270		if (i >= CInemaTestListSize(tests)) {
1271			break;
1272		}
1273		if (!CInemaTask(tests, i)) {
1274			success = false;
1275		}
1276		CIflush(&stream.out, stdout);
1277		CIflush(&stream.err, stderr);
1278	}
1279	MutexLock(&jobMutex);
1280	if (!success) {
1281		jobStatus = 1;
1282	}
1283	MutexUnlock(&jobMutex);
1284
1285	CIflush(&stream.out, stdout);
1286	StringListDeinit(&stream.out.lines);
1287	StringListDeinit(&stream.out.partial);
1288
1289	CIflush(&stream.err, stderr);
1290	StringListDeinit(&stream.err.lines);
1291	StringListDeinit(&stream.err.partial);
1292}
1293
1294void _log(struct mLogger* log, int category, enum mLogLevel level, const char* format, va_list args) {
1295	UNUSED(log);
1296	if (level == mLOG_FATAL) {
1297		struct CInemaTest* test = ThreadLocalGetValue(currentTest);
1298		test->status = CI_ERROR;
1299	}
1300	if (verbosity < 0) {
1301		return;
1302	}
1303	int mask = mLOG_FATAL;
1304	if (verbosity >= 1) {
1305		mask |= mLOG_ERROR;
1306	}
1307	if (verbosity >= 2) {
1308		mask |= mLOG_WARN;
1309	}
1310	if (verbosity >= 4) {
1311		mask |= mLOG_INFO;
1312	}
1313	if (verbosity >= 5) {
1314		mask |= mLOG_ALL;
1315	}
1316	if (!(mask & level)) {
1317		return;
1318	}
1319
1320	char buffer[256];
1321	vsnprintf(buffer, sizeof(buffer), format, args);
1322	CIerr(0, "[%s] %s\n", mLogCategoryName(category), buffer);
1323}
1324
1325int main(int argc, char** argv) {
1326	ThreadLocalInitKey(&logStream);
1327	ThreadLocalSetKey(logStream, NULL);
1328
1329	int status = 0;
1330	if (!parseCInemaArgs(argc, argv)) {
1331		status = 1;
1332		goto cleanup;
1333	}
1334
1335	if (showVersion) {
1336		version(argv[0]);
1337		goto cleanup;
1338	}
1339
1340	if (showUsage) {
1341		usageCInema(argv[0]);
1342		goto cleanup;
1343	}
1344
1345	argc -= optind;
1346	argv += optind;
1347
1348	if (!base[0] && !determineBase(argc, argv)) {
1349		CIlog(0, "Could not determine CInema test base. Please specify manually.");
1350		status = 1;
1351		goto cleanup;
1352	}
1353#ifndef _WIN32
1354	char* rbase = realpath(base, NULL);
1355	if (rbase) {
1356		strlcpy(base, rbase, sizeof(base));
1357		free(rbase);
1358	}
1359#endif
1360
1361	struct CInemaTestList tests;
1362	CInemaTestListInit(&tests, 0);
1363
1364	struct mLogger logger = { .log = _log };
1365	mLogSetDefaultLogger(&logger);
1366#ifdef USE_FFMPEG
1367	if (verbosity < 2) {
1368		av_log_set_level(AV_LOG_ERROR);
1369	}
1370#endif
1371
1372	if (argc > 0) {
1373		size_t i;
1374		for (i = 0; i < (size_t) argc; ++i) {
1375			if (strchr(argv[i], '*')) {
1376				if (!globTests(&tests, argv[i], NULL)) {
1377					status = 1;
1378					break;
1379				}
1380				continue;
1381			}
1382			char path[PATH_MAX + 1] = {0};
1383			testToPath(argv[i], path);
1384
1385			if (!collectTests(&tests, path)) {
1386				status = 1;
1387				break;
1388			}
1389		}
1390	} else if (!collectTests(&tests, base)) {
1391		status = 1;
1392	}
1393
1394	if (CInemaTestListSize(&tests) == 0) {
1395		CIlog(1, "No tests found.");
1396		status = 1;
1397	} else {
1398		reduceTestList(&tests);
1399	}
1400
1401	HashTableInit(&configTree, 0, free);
1402	MutexInit(&configMutex);
1403	ThreadLocalInitKey(&currentTest);
1404	ThreadLocalSetKey(currentTest, NULL);
1405
1406	if (jobs == 1) {
1407		size_t i;
1408		for (i = 0; i < CInemaTestListSize(&tests); ++i) {
1409			bool success = CInemaTask(&tests, i);
1410			if (!success) {
1411				status = 1;
1412			}
1413		}
1414	} else {
1415		MutexInit(&jobMutex);
1416		int i;
1417		for (i = 0; i < jobs; ++i) {
1418			ThreadCreate(&jobThreads[i], CInemaJob, &tests);
1419		}
1420		for (i = 0; i < jobs; ++i) {
1421			ThreadJoin(&jobThreads[i]);
1422		}
1423		MutexDeinit(&jobMutex);
1424		status = jobStatus;
1425	}
1426
1427	MutexDeinit(&configMutex);
1428	HashTableEnumerate(&configTree, _unloadConfigTree, NULL);
1429	HashTableDeinit(&configTree);
1430	CInemaTestListDeinit(&tests);
1431
1432cleanup:
1433	return status;
1434}