Skip to content

Commit

Permalink
Merge remote-tracking branch 'remotes/origin/unify-roi-features'
Browse files Browse the repository at this point in the history
  • Loading branch information
Jovasa committed Mar 12, 2022
2 parents 96b00ff + 917d26f commit 763ad3f
Show file tree
Hide file tree
Showing 16 changed files with 311 additions and 203 deletions.
19 changes: 14 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -156,11 +156,20 @@ Video structure:
- frametile: Constrain within the tile.
- frametilemargin: Constrain even more.
--roi <filename> : Use a delta QP map for region of interest.
Reads an array of delta QP values from a text
file. The file format is: width and height of
the QP delta map followed by width*height delta
QP values in raster order. The map can be of any
size and will be scaled to the video size.
Reads an array of delta QP values from a file.
Text and binary files are supported and detected
from the file extension (.txt/.bin). If a known
extension is not found, the file is treated as
a text file. The file can include one or many
ROI frames each in the following format:
width and height of the QP delta map followed
by width * height delta QP values in raster
order. In binary format, width and height are
32-bit integers whereas the delta QP values are
signed 8-bit values. The map can be of any size
and will be scaled to the video size. The file
reading will loop if end of the file is reached.
See roi.txt in the examples folder.
--set-qp-in-cu : Set QP at CU level keeping pic_init_qp_minus26.
in PPS and slice_qp_delta in slize header zero.
--(no-)erp-aqp : Use adaptive QP for 360 degree video with
Expand Down
4 changes: 2 additions & 2 deletions configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ AC_CONFIG_SRCDIR([src/encmain.c])
# - Increment when making new releases and major or minor was not changed since last release.
#
# Here is a somewhat sane guide to lib versioning: http://apr.apache.org/versioning.html
ver_major=6
ver_minor=6
ver_major=7
ver_minor=0
ver_release=0

# Prevents configure from adding a lot of defines to the CFLAGS
Expand Down
21 changes: 15 additions & 6 deletions doc/kvazaar.1
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
.TH KVAZAAR "1" "October 2021" "kvazaar v2.1.0" "User Commands"
.TH KVAZAAR "1" "February 2022" "kvazaar v2.1.0" "User Commands"
.SH NAME
kvazaar \- open source HEVC encoder
.SH SYNOPSIS
Expand Down Expand Up @@ -180,11 +180,20 @@ Constrain movement vectors. [none]
.TP
\fB\-\-roi <filename>
Use a delta QP map for region of interest.
Reads an array of delta QP values from a text
file. The file format is: width and height of
the QP delta map followed by width*height delta
QP values in raster order. The map can be of any
size and will be scaled to the video size.
Reads an array of delta QP values from a file.
Text and binary files are supported and detected
from the file extension (.txt/.bin). If a known
extension is not found, the file is treated as
a text file. The file can include one or many
ROI frames each in the following format:
width and height of the QP delta map followed
by width * height delta QP values in raster
order. In binary format, width and height are
32\-bit integers whereas the delta QP values are
signed 8\-bit values. The map can be of any size
and will be scaled to the video size. The file
reading will loop if end of the file is reached.
See roi.txt in the examples folder.
.TP
\fB\-\-set\-qp\-in\-cu
Set QP at CU level keeping pic_init_qp_minus26.
Expand Down
73 changes: 21 additions & 52 deletions src/cfg.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,9 +139,9 @@ int kvz_config_init(kvz_config *cfg)
cfg->gop_lp_definition.t = 1;
cfg->open_gop = true;

cfg->roi.width = 0;
cfg->roi.height = 0;
cfg->roi.dqps = NULL;
cfg->roi.file_path = NULL;
cfg->roi.format = KVZ_ROI_TXT;

cfg->set_qp_in_cu = false;

cfg->erp_aqp = false;
Expand Down Expand Up @@ -190,11 +190,11 @@ int kvz_config_destroy(kvz_config *cfg)
{
if (cfg) {
FREE_POINTER(cfg->cqmfile);
FREE_POINTER(cfg->roi.file_path);
FREE_POINTER(cfg->fast_coeff_table_fn);
FREE_POINTER(cfg->tiles_width_split);
FREE_POINTER(cfg->tiles_height_split);
FREE_POINTER(cfg->slice_addresses_in_ts);
FREE_POINTER(cfg->roi.dqps);
FREE_POINTER(cfg->optional_key);
FREE_POINTER(cfg->fastrd_learning_outdir_fn);
}
Expand Down Expand Up @@ -1241,60 +1241,29 @@ int kvz_config_parse(kvz_config *cfg, const char *name, const char *value)
}
else if OPT("implicit-rdpcm")
cfg->implicit_rdpcm = (bool)atobool(value);
else if OPT("roi") {
// The ROI description is as follows:
// First number is width, second number is height,
// then follows width * height number of dqp values.
FILE* f = fopen(value, "rb");
if (!f) {
fprintf(stderr, "Could not open ROI file.\n");
return 0;
}

int width = 0;
int height = 0;
if (!fscanf(f, "%d", &width) || !fscanf(f, "%d", &height)) {
fprintf(stderr, "Failed to read ROI size.\n");
fclose(f);
return 0;
}

if (width <= 0 || height <= 0) {
fprintf(stderr, "Invalid ROI size: %dx%d.\n", width, height);
fclose(f);
return 0;
}

if (width > 10000 || height > 10000) {
fprintf(stderr, "ROI dimensions exceed arbitrary value of 10000.\n");
fclose(f);
return 0;
}
else if OPT("roi") {
static enum kvz_roi_format const formats[] = { KVZ_ROI_TXT, KVZ_ROI_BIN };
static const char * const format_names[] = { "txt", "bin", NULL };

const unsigned size = width * height;
int8_t *dqp_array = calloc((size_t)size, sizeof(cfg->roi.dqps[0]));
if (!dqp_array) {
fprintf(stderr, "Failed to allocate memory for ROI table.\n");
fclose(f);
char *roi_file = strdup(value);
if (!roi_file) {
fprintf(stderr, "Failed to allocate memory for ROI file name.\n");
return 0;
}
FREE_POINTER(cfg->roi.file_path);
cfg->roi.file_path = roi_file;

FREE_POINTER(cfg->roi.dqps);
cfg->roi.dqps = dqp_array;
cfg->roi.width = width;
cfg->roi.height = height;

for (int i = 0; i < size; ++i) {
int number; // Need a pointer to int for fscanf
if (fscanf(f, "%d", &number) != 1) {
fprintf(stderr, "Reading ROI file failed.\n");
fclose(f);
return 0;
}
dqp_array[i] = CLIP(-51, 51, number);
// Get file extension or the substring after the last dot
char *maybe_extension = strrchr(cfg->roi.file_path, '.');
if (!maybe_extension) {
cfg->roi.format = KVZ_ROI_TXT;
} else {
maybe_extension++;
int8_t format;
bool unknown_format = !parse_enum(maybe_extension, format_names, &format);
cfg->roi.format = unknown_format ? KVZ_ROI_TXT : formats[format];
}

fclose(f);
}
else if OPT("set-qp-in-cu") {
cfg->set_qp_in_cu = (bool)atobool(value);
Expand Down
20 changes: 15 additions & 5 deletions src/cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ static const struct option long_options[] = {
{ "force-level", required_argument, NULL, 0 },
{ "high-tier", no_argument, NULL, 0 },
{ "me-steps", required_argument, NULL, 0 },
{ "roi-file", required_argument, NULL, 0 },
{ "fast-residual-cost", required_argument, NULL, 0 },
{ "set-qp-in-cu", no_argument, NULL, 0 },
{ "open-gop", no_argument, NULL, 0 },
Expand Down Expand Up @@ -500,11 +501,20 @@ void print_help(void)
" - frametile: Constrain within the tile.\n"
" - frametilemargin: Constrain even more.\n"
" --roi <filename> : Use a delta QP map for region of interest.\n"
" Reads an array of delta QP values from a text\n"
" file. The file format is: width and height of\n"
" the QP delta map followed by width*height delta\n"
" QP values in raster order. The map can be of any\n"
" size and will be scaled to the video size.\n"
" Reads an array of delta QP values from a file.\n"
" Text and binary files are supported and detected\n"
" from the file extension (.txt/.bin). If a known\n"
" extension is not found, the file is treated as\n"
" a text file. The file can include one or many\n"
" ROI frames each in the following format:\n"
" width and height of the QP delta map followed\n"
" by width * height delta QP values in raster\n"
" order. In binary format, width and height are\n"
" 32-bit integers whereas the delta QP values are\n"
" signed 8-bit values. The map can be of any size\n"
" and will be scaled to the video size. The file\n"
" reading will loop if end of the file is reached.\n"
" See roi.txt in the examples folder.\n"
" --set-qp-in-cu : Set QP at CU level keeping pic_init_qp_minus26.\n"
" in PPS and slice_qp_delta in slize header zero.\n"
" --(no-)erp-aqp : Use adaptive QP for 360 degree video with\n"
Expand Down
4 changes: 3 additions & 1 deletion src/encmain.c
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ int main(int argc, char *argv[])
FILE *input = NULL; //!< input file (YUV)
FILE *output = NULL; //!< output file (HEVC NAL stream)
FILE *recout = NULL; //!< reconstructed YUV output, --debug
FILE *roifile = NULL;
clock_t start_time = clock();
clock_t encoding_start_cpu_time;
KVZ_CLOCK_T encoding_start_real_time;
Expand Down Expand Up @@ -566,7 +567,7 @@ int main(int argc, char *argv[])
// Give arguments via struct to the input thread
input_handler_args in_args = {
.available_input_slots = available_input_slots,
.filled_input_slots = filled_input_slots,
.filled_input_slots = filled_input_slots,

.input = input,
.api = api,
Expand Down Expand Up @@ -805,6 +806,7 @@ int main(int argc, char *argv[])
if (input) fclose(input);
if (output) fclose(output);
if (recout) fclose(recout);
if (roifile) fclose(roifile);

CHECKPOINTS_FINALIZE();

Expand Down
2 changes: 1 addition & 1 deletion src/encode_coding_tree.c
Original file line number Diff line number Diff line change
Expand Up @@ -758,7 +758,7 @@ void kvz_encode_coding_tree(encoder_state_t * const state,
bool border_split_y = ctrl->in.height >= abs_y + (LCU_WIDTH >> MAX_DEPTH) + half_cu;
bool border = border_x || border_y; /*!< are we in any border CU */

if (depth <= ctrl->max_qp_delta_depth) {
if (depth <= state->frame->max_qp_delta_depth) {
state->must_code_qp_delta = true;
}

Expand Down

0 comments on commit 763ad3f

Please sign in to comment.