You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

200 lines
5.0 KiB

//#define LOG_NDEBUG 0
#define LOG_TAG "JpegEncoderTest"
#include <utils/Log.h>
#include <getopt.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include "MpiJpegEncoder.h"
/*
* Dumps usage on stderr.
*/
void usage() {
fprintf(stderr,
"Usage: jpege_test [options]\n"
"\n"
"used for MpiJpegEncoder test.\n"
"\n"
"Options:\n"
"-i\n"
" input file.\n"
"-o\n"
" output file.\n"
"-w\n"
" input width.\n"
"-h\n"
" input height.\n"
"-f\n"
" input format.\n"
""
" jpege_test -i test.yuv -o test.jpg -w 1920 -h 1080 -f yuv420sp"
"\n"
);
}
bool storeFileData(const char *fileName, char **data, size_t *size) {
FILE *file = NULL;
size_t fileLen = 0;
file = fopen(fileName, "rb");
if (file == NULL) {
ALOGE("failed to open file %s - %s", fileName, strerror(errno));
return false;
}
fseek(file, 0L, SEEK_END);
fileLen = ftell(file);
rewind(file);
*data = (char *)malloc(fileLen);
if (*data == NULL) {
ALOGE("failed to malloc buffer");
fclose(file);
return false;
}
fread(*data, 1, fileLen, file);
*size = fileLen;
fflush(file);
fclose(file);
return true;
}
bool dumpDataToFile(void *data, size_t size, const char *fileName) {
FILE *file = NULL;
file = fopen(fileName, "w+b");
if (file == NULL) {
ALOGE("failed to open file %s - %s", fileName, strerror(errno));
return false;
}
fwrite(data, 1, size, file);
fflush(file);
fclose(file);
return true;
}
int main(int argc, char **argv) {
const char *inputFile = NULL;
const char *outputFile = NULL;
int width = 0, height = 0;
MpiJpegEncoder::InputFormat inputFormat = MpiJpegEncoder::INPUT_FMT_YUV420SP;
int res;
while ((res = getopt(argc, argv, "i:o:w:h:f:")) >= 0) {
switch (res) {
case 'i': {
inputFile = optarg;
break;
}
case 'o': {
outputFile = optarg;
break;
}
case 'w': {
char *end;
width = strtol(optarg, &end, 10);
break;
}
case 'h': {
char *end;
height = strtol(optarg, &end, 10);
break;
}
case 'f': {
if (strcmp(optarg, "yuv420sp") == 0) {
inputFormat = MpiJpegEncoder::INPUT_FMT_YUV420SP;
} else if (strcmp(optarg, "yuv420p") == 0) {
inputFormat = MpiJpegEncoder::INPUT_FMT_YUV420P;
} else if (strcmp(optarg, "rgba") == 0) {
inputFormat = MpiJpegEncoder::INPUT_FMT_RGBA8888;
} else if (strcmp(optarg, "argb") == 0) {
inputFormat = MpiJpegEncoder::INPUT_FMT_ARGB8888;
} else if (strcmp(optarg, "abgr") == 0) {
inputFormat = MpiJpegEncoder::INPUT_FMT_ABGR8888;
} else if (strcmp(optarg, "yuv422sp") == 0) {
inputFormat = MpiJpegEncoder::INPUT_FMT_YUV422SP_VU;
} else {
fprintf(stderr, "Unknown format '%s'\n", optarg);
return 2;
}
break;
}
default: {
usage();
return 0;
}
}
}
if (!inputFile || !outputFile || !width || !height) {
printf("Must specify width/height/inputFile/outputFile (see -h).\n");
return 2;
}
bool err = false;
char *data = NULL;
size_t size = 0;
MpiJpegEncoder encoder;
MpiJpegEncoder::OutputPacket_t pktOut;
memset(&pktOut, 0, sizeof(pktOut));
err = encoder.prepareEncoder();
if (!err) {
printf("failed to prepare encoder.\n");
goto cleanUp;
}
err = encoder.updateEncodeCfg(width, height, inputFormat);
if (!err) {
printf("failed to update encode config.\n");
goto cleanUp;
}
err = storeFileData(inputFile, &data, &size);
if (!err) {
printf("failed to open input file.\n");
goto cleanUp;
}
ALOGD("start with size %zu cfg %dx%d Fmt %d", size, width, height, inputFormat);
err = encoder.encodeFrame(data, &pktOut);
if (!err) {
printf("failed to encode frame.\n");
goto cleanUp;
}
// write output packet to destination.
err = dumpDataToFile(pktOut.data, pktOut.size, outputFile);
if (!err) {
printf("failed to open output file.\n");
} else {
printf("get output file %s - size %d.\n", outputFile, pktOut.size);
}
/* output buffer count within limits, so release frame buffer if one
frame has been display successful. */
encoder.deinitOutputPacket(&pktOut);
encoder.flushBuffer();
cleanUp:
if (data) {
free(data);
data = NULL;
}
return 0;
}