Skip to content
Snippets Groups Projects
TestCFHD.cpp 35.8 KiB
Newer Older
dnewman-gpsw's avatar
dnewman-gpsw committed
				sprintf(name, "%s%04d.cfhd", BASENAME_OUT, frameNumber);
				FILE *fp = fopen(name, "wb");
				if (fp) fwrite(sampleBuffer, 1, sampleSize, fp);
				fclose(fp);
			}
#endif
			// Print the SDK version flagged in the bit-stream
			if (frameNumber == 1)
			{
				static int once = 0;
				if(once == 0)
					SDKVerion(decoderRef, sampleBuffer, (int)sampleSize), once++;
				printf("Resolution:   %dx%d\n", FRAME_WIDTH, FRAME_HEIGHT);
dnewman-gpsw's avatar
dnewman-gpsw committed
#ifdef DECODE_FORMAT
dnewman-gpsw's avatar
dnewman-gpsw committed
				if (pixelFormat != DECODE_FORMAT)
					printf("Pixel format: %c%c%c%c <--> %c%c%c%c\n", (pixelFormat >> 24) & 0xff, (pixelFormat >> 16) & 0xff, (pixelFormat >> 8) & 0xff, (pixelFormat >> 0) & 0xff, (DECODE_FORMAT >> 24) & 0xff, (DECODE_FORMAT >> 16) & 0xff, (DECODE_FORMAT >> 8) & 0xff, (DECODE_FORMAT >> 0) & 0xff);
				else
dnewman-gpsw's avatar
dnewman-gpsw committed
#endif
dnewman-gpsw's avatar
dnewman-gpsw committed
					printf("Pixel format: %c%c%c%c\n", (pixelFormat >> 24) & 0xff, (pixelFormat >> 16) & 0xff, (pixelFormat >> 8) & 0xff, (pixelFormat >> 0) & 0xff);
				printf("Encode:       %d\n", encodedFormat == CFHD_ENCODED_FORMAT_YUV_422 ? 422 : encodedFormat == CFHD_ENCODED_FORMAT_RGB_444 ? 444 : encodedFormat == CFHD_ENCODED_FORMAT_RGBA_4444 ? 4444 : 0);
				printf("Decode:       %s\n", decode_res == 1 ? "Full res" : decode_res == 2 ? "Half res" : decode_res == 3 ? "Quarter res" : "none");
			}

#if DO_DECODE
			error = DecodeFrame(&frameDecBuffer, decoderRef, metadataDecRef, sampleBuffer, (int)sampleSize, encodedFormat, DECODE_FORMAT, decode_res, NULL, &dec_us);
			if (error) goto cleanup;

#if DO_PSNR // export frames that have more that xdB of PSNR error.
			if (1)
			{
				float psnr;
				int sourcesize = framePitch*frameHeight;

				psnr = PSNR(frameBuffer, frameDecBuffer, frameWidth, frameHeight, pixelFormat, scale);
				printf("%d: source %d compressed to %d in %1.1fms - %1.1fms (%1.1f:1 PSNR %1.1fdB)\n", frameNumber, sourcesize, (int)sampleSize, (float)enc_us / 1000.0f, (float)dec_us / 1000.0f, (float)sourcesize / (float)sampleSize, psnr);

				if (psnr < PPM_EXPORT_BELOW)
				{
					char metadata[64];
#ifdef _WINDOWS 
					sprintf_s(metadata, sizeof(metadata), "PSNR = %f", psnr);
#else
					sprintf(metadata, "PSNR = %f", psnr);
#endif
					//ExportPPM(inputname, metadata, frameBuffer, frameWidth, frameHeight, framePitch, pixelFormat);
					ExportPPM(outputname, metadata, frameDecBuffer, frameWidth / scale, frameHeight / scale, framePitch / scale, pixelFormat);
				}
			}
#else
			printf("%d: source %d compressed to %d in %1.1fms - %1.1fms (%1.1f:1)\n", frameNumber, framePitch*frameHeight, sampleSize, (float)enc_us / 1000.0f, (float)dec_us / 1000.0f, (float)(framePitch*frameHeight) / (float)sampleSize);
#endif

#endif // DO_DECODE

			frameNumber++;
		}

		// Free the encoder
		CFHD_CloseEncoder(encoderRef);
		encoderRef = NULL;

		frmt++;
		if (TestPixelFormat[frmt * 3] == 0 && TestResolution[resmode + 1] != CFHD_DECODED_RESOLUTION_UNKNOWN)
		{
			resmode++;
			frmt = 0;
		}
		printf("\n");

	} while (TestPixelFormat[frmt * 3] && TestResolution[resmode] != CFHD_DECODED_RESOLUTION_UNKNOWN);

cleanup:
	if (error)
	{

#ifdef __APPLE__
		if (frameBuffer) free(frameBuffer), frameBuffer = NULL;
		if (frameDecBuffer) free(frameDecBuffer), frameDecBuffer = NULL;
#else
		if (frameBuffer) _mm_free(frameBuffer), frameBuffer = NULL;
		if (frameDecBuffer) _mm_free(frameDecBuffer), frameDecBuffer = NULL;
#endif
	}


	if (decoderRef) CFHD_CloseDecoder(decoderRef);
	if (encoderRef) CFHD_CloseEncoder(encoderRef);

	return error;
}




int main(int argc, char **argv)
{
dnewman-gpsw's avatar
dnewman-gpsw committed
	int showusage = 0;
dnewman-gpsw's avatar
dnewman-gpsw committed
	CFHD_Error error = CFHD_ERROR_OKAY;

dnewman-gpsw's avatar
dnewman-gpsw committed
	if (argc != 2)
dnewman-gpsw's avatar
dnewman-gpsw committed
	{
dnewman-gpsw's avatar
dnewman-gpsw committed
		showusage = 1;
dnewman-gpsw's avatar
dnewman-gpsw committed
	}
dnewman-gpsw's avatar
dnewman-gpsw committed
	else if (argv[1][0] == '-')
	{
		if (argv[1][1] == 'd' || argv[1][1] == 'D')
			error = EncodeDecodeQualityTest();
		else if (argv[1][1] == 'e' || argv[1][1] == 'E')
			error = EncodeSpeedTest();
		else
			showusage = 1;
	}
	else 
dnewman-gpsw's avatar
dnewman-gpsw committed
	{
		char ext[4] = "";
		int len = strlen(argv[1]);
		if (len > 4)
		{
			ext[0] = argv[1][len - 3];
			ext[1] = argv[1][len - 2];
			ext[2] = argv[1][len - 1];
			ext[3] = 0;
		}

		error = DecodeMOVIE(argv[1], ext);
	}

dnewman-gpsw's avatar
dnewman-gpsw committed
	if (showusage)
	{
		printf("usage: %s [switches] or <filname.MOV|MP4|AVI>\n", argv[0]);
		printf("          -D = decoder tester\n");
		printf("          -E = encoder tester\n");
	}

dnewman-gpsw's avatar
dnewman-gpsw committed
	if (error) printf("error code: %d\n", error);
	return error;
}