libbpg-0.9.5

This commit is contained in:
King_DuckZ 2015-01-16 13:48:11 +01:00
parent 6e56352f86
commit 357f186837
35 changed files with 3022 additions and 2134 deletions

View file

@ -9,7 +9,15 @@
#include "bpgenc.h"
#define ARGV_MAX 128
struct HEVCEncoderContext {
HEVCEncodeParams params;
char infilename[1024];
char outfilename[1024];
FILE *yuv_file;
int frame_count;
};
#define ARGV_MAX 256
static void add_opt(int *pargc, char **argv,
const char *str)
@ -22,31 +30,59 @@ static void add_opt(int *pargc, char **argv,
*pargc = argc;
}
/* return the encoded data in *pbuf and the size. Return < 0 if error */
int jctvc_encode_picture(uint8_t **pbuf, Image *img,
const HEVCEncodeParams *params)
static HEVCEncoderContext *jctvc_open(const HEVCEncodeParams *params)
{
TAppEncTop cTAppEncTop;
HEVCEncoderContext *s;
char buf[1024];
static int tmp_idx = 1;
s = (HEVCEncoderContext *)malloc(sizeof(HEVCEncoderContext));
memset(s, 0, sizeof(*s));
s->params = *params;
#ifdef WIN32
if (GetTempPath(sizeof(buf), buf) > sizeof(buf) - 1) {
fprintf(stderr, "Temporary path too long\n");
free(s);
return NULL;
}
#else
strcpy(buf, "/tmp/");
#endif
snprintf(s->infilename, sizeof(s->infilename), "%sout%d-%d.yuv", buf, getpid(), tmp_idx);
snprintf(s->outfilename, sizeof(s->outfilename), "%sout%d-%d.bin", buf, getpid(), tmp_idx);
tmp_idx++;
s->yuv_file = fopen(s->infilename, "wb");
if (!s->yuv_file) {
fprintf(stderr, "Could not open '%s'\n", s->infilename);
free(s);
return NULL;
}
return s;
}
static int jctvc_encode(HEVCEncoderContext *s, Image *img)
{
save_yuv1(img, s->yuv_file);
s->frame_count++;
return 0;
}
/* return the encoded data in *pbuf and the size. Return < 0 if error */
static int jctvc_close(HEVCEncoderContext *s, uint8_t **pbuf)
{
TAppEncTop cTAppEncTop;
int argc;
char *argv[ARGV_MAX + 1];
char buf[1024], infilename[1024], outfilename[1024];
char buf[1024];
const char *str;
FILE *f;
uint8_t *out_buf;
int out_buf_len, i;
#ifdef WIN32
if (GetTempPath(sizeof(buf), buf) > sizeof(buf) - 1) {
fprintf(stderr, "Temporary path too long\n");
return -1;
}
#else
strcpy(buf, "/tmp/");
#endif
snprintf(infilename, sizeof(infilename), "%sout%d.yuv", buf, getpid());
snprintf(outfilename, sizeof(outfilename), "%sout%d.bin", buf, getpid());
save_yuv(img, infilename);
fclose(s->yuv_file);
s->yuv_file = NULL;
m_gcAnalyzeAll.clear();
m_gcAnalyzeI.clear();
@ -59,22 +95,19 @@ int jctvc_encode_picture(uint8_t **pbuf, Image *img,
argc = 0;
add_opt(&argc, argv, "jctvc"); /* dummy executable name */
snprintf(buf, sizeof(buf),"--InputFile=%s", infilename);
snprintf(buf, sizeof(buf),"--InputFile=%s", s->infilename);
add_opt(&argc, argv, buf);
snprintf(buf, sizeof(buf),"--BitstreamFile=%s", outfilename);
snprintf(buf, sizeof(buf),"--BitstreamFile=%s", s->outfilename);
add_opt(&argc, argv, buf);
snprintf(buf, sizeof(buf),"--SourceWidth=%d", img->w);
snprintf(buf, sizeof(buf),"--SourceWidth=%d", s->params.width);
add_opt(&argc, argv, buf);
snprintf(buf, sizeof(buf),"--SourceHeight=%d", s->params.height);
add_opt(&argc, argv, buf);
snprintf(buf, sizeof(buf),"--InputBitDepth=%d", s->params.bit_depth);
add_opt(&argc, argv, buf);
snprintf(buf, sizeof(buf),"--SourceWidth=%d", img->w);
add_opt(&argc, argv, buf);
snprintf(buf, sizeof(buf),"--SourceHeight=%d", img->h);
add_opt(&argc, argv, buf);
snprintf(buf, sizeof(buf),"--InputBitDepth=%d", img->bit_depth);
add_opt(&argc, argv, buf);
switch(img->format) {
switch(s->params.chroma_format) {
case BPG_FORMAT_GRAY:
str = "400";
break;
@ -93,18 +126,19 @@ int jctvc_encode_picture(uint8_t **pbuf, Image *img,
snprintf(buf, sizeof(buf),"--InputChromaFormat=%s", str);
add_opt(&argc, argv, buf);
snprintf(buf, sizeof(buf),"--QP=%d", params->qp);
snprintf(buf, sizeof(buf),"--QP=%d", s->params.qp);
add_opt(&argc, argv, buf);
snprintf(buf, sizeof(buf),"--SEIDecodedPictureHash=%d",
params->sei_decoded_picture_hash);
s->params.sei_decoded_picture_hash);
add_opt(&argc, argv, buf);
if (!params->verbose)
if (!s->params.verbose)
add_opt(&argc, argv, "--Verbose=0");
/* single frame */
add_opt(&argc, argv, "--FramesToBeEncoded=1");
snprintf(buf, sizeof(buf),"--FramesToBeEncoded=%d", s->frame_count);
add_opt(&argc, argv, buf);
/* no padding necessary (it is done before) */
add_opt(&argc, argv, "--ConformanceWindowMode=0");
@ -113,22 +147,43 @@ int jctvc_encode_picture(uint8_t **pbuf, Image *img,
add_opt(&argc, argv, "--FrameRate=25");
/* general config */
add_opt(&argc, argv, "--Profile=main_444_16_intra");
add_opt(&argc, argv, "--QuadtreeTULog2MaxSize=5");
add_opt(&argc, argv, "--QuadtreeTUMaxDepthIntra=3");
if (s->params.compress_level == 9) {
add_opt(&argc, argv, "--QuadtreeTUMaxDepthIntra=4");
add_opt(&argc, argv, "--QuadtreeTUMaxDepthInter=4");
} else {
add_opt(&argc, argv, "--QuadtreeTUMaxDepthIntra=3");
add_opt(&argc, argv, "--QuadtreeTUMaxDepthInter=3");
}
add_opt(&argc, argv, "--IntraPeriod=1");
add_opt(&argc, argv, "--GOPSize=1");
if (s->params.intra_only) {
add_opt(&argc, argv, "--Profile=main_444_16_intra");
add_opt(&argc, argv, "--IntraPeriod=1");
add_opt(&argc, argv, "--GOPSize=1");
} else {
int gop_size;
add_opt(&argc, argv, "--Profile=main_444_16");
add_opt(&argc, argv, "--IntraPeriod=250");
gop_size = 1;
snprintf(buf, sizeof(buf), "--GOPSize=%d", gop_size);
add_opt(&argc, argv, buf);
for(i = 0; i < gop_size; i++) {
snprintf(buf, sizeof(buf), "--Frame%d=P 1 3 0.4624 0 0 0 1 1 -1 0", i + 1);
add_opt(&argc, argv, buf);
}
}
add_opt(&argc, argv, "--TransformSkip=1");
add_opt(&argc, argv, "--TransformSkipFast=1");
/* Note: Format Range extension */
if (img->format == BPG_FORMAT_444) {
if (s->params.chroma_format == BPG_FORMAT_444) {
add_opt(&argc, argv, "--CrossComponentPrediction=1");
}
if (params->lossless) {
if (s->params.lossless) {
add_opt(&argc, argv, "--CostMode=lossless");
add_opt(&argc, argv, "--SAO=0");
add_opt(&argc, argv, "--LoopFilterDisable");
@ -148,7 +203,7 @@ int jctvc_encode_picture(uint8_t **pbuf, Image *img,
/* trailing NULL */
argv[argc] = NULL;
if (params->verbose >= 2) {
if (s->params.verbose >= 2) {
int i;
printf("Encode options:");
for(i = 0; i < argc; i++) {
@ -169,12 +224,12 @@ int jctvc_encode_picture(uint8_t **pbuf, Image *img,
for(i = 0; i < argc; i++)
free(argv[i]);
unlink(infilename);
unlink(s->infilename);
/* read output bitstream */
f = fopen(outfilename, "rb");
f = fopen(s->outfilename, "rb");
if (!f) {
fprintf(stderr, "Could not open '%s'\n", outfilename);
fprintf(stderr, "Could not open '%s'\n", s->outfilename);
return -1;
}
@ -189,7 +244,14 @@ int jctvc_encode_picture(uint8_t **pbuf, Image *img,
return -1;
}
fclose(f);
unlink(outfilename);
unlink(s->outfilename);
*pbuf = out_buf;
free(s);
return out_buf_len;
}
HEVCEncoder jctvc_encoder = {
.open = jctvc_open,
.encode = jctvc_encode,
.close = jctvc_close,
};