Skip to content

Commit

Permalink
Add --splitmargin argument to specify minimum VxH spacing to do a split.
Browse files Browse the repository at this point in the history
  • Loading branch information
cubicibo committed Sep 6, 2023
1 parent 5195e91 commit e99269b
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 9 deletions.
7 changes: 6 additions & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ The following optional arguments are available:
| ``-q`` | Sets and enable image quantization with N colors. |
| ``--quantize`` | Choices: value in [0; 255]. |
| | Default: ``0`` (Disabled, PNGs are 32-bit RGBA) |
| | Note: Do not use if the BDNXML is generated for SUPer. |
| | Notes: Do not use if the BDNXML is generated for SUPer.|
| | This must be enabled if target software is Scenarist BD|
+--------------------+--------------------------------------------------------+
| ``-a`` | Sets an additional font directory for custom fonts not |
Expand All @@ -55,6 +55,11 @@ The following optional arguments are available:
| | Default: ``0`` (Disabled) |
| | Note: Do not use if the BDNXML is generated for SUPer. |
+--------------------+--------------------------------------------------------+
| ``-m`` | Sets the vertical and opt. horizontal margins to split |
| ``--splitmargin`` | Format: ``VxH`` (V=y difference, H=x difference). |
| | Default: 0x0. Split search is done on 8x8 grid anyway. |
| | Note: If only V is given, 'x' separator must be omitted|
+--------------------+--------------------------------------------------------+
| ``-p`` | Sets the ASS pixel aspect ratio. Required for |
| ``--par`` | anamorphic content. Defaults to libass default value. |
+--------------------+--------------------------------------------------------+
Expand Down
35 changes: 32 additions & 3 deletions ass2bdnxml.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ static void die_usage(const char *name)
exit(1);
}

void tc_to_tcarray(char *buf, uint8_t *vals)
static void tc_to_tcarray(char *buf, uint8_t *vals)
{
uint8_t hits = 0;

Expand All @@ -80,7 +80,7 @@ void tc_to_tcarray(char *buf, uint8_t *vals)
}
}

uint64_t tcarray_to_frame(uint8_t *vals, frate_t *fps)
static uint64_t tcarray_to_frame(uint8_t *vals, frate_t *fps)
{
if (vals[3] >= fps->rate) {
printf("Frame in TC is above framerate: %d >= %d\n", vals[3], fps->rate);
Expand All @@ -93,6 +93,26 @@ uint64_t tcarray_to_frame(uint8_t *vals, frate_t *fps)
return offset;
}

static void parse_margins(char *buf, uint16_t *margins)
{
uint8_t k = 0;
uint8_t idx = 0;
for (k = 0; k < 9 && buf[k]; k++) {
if (buf[k] == 'x' && idx == 0) {
idx = 1;
} else if (buf[k] >= '0' && buf[k] <= '9') {
margins[idx] = margins[idx]*10 + (uint16_t)(buf[k] - '0');
} else /*if ((buf[k] == 'x' && idx == 1) || (idx == 0 && k > 4))*/ {
printf("Invalid margins format, expected 'VxH' (akin to 1080x1920), got %s\n", buf);
exit(1);
}
}
if (k >= 5 && idx == 0) {
printf("Invalid margins format.\n");
exit(1);
}
}

static void frame_to_tc(uint64_t frames, frate_t *fps, char *buf)
{
frames--;
Expand Down Expand Up @@ -200,6 +220,7 @@ int main(int argc, char *argv[])
{"negative", no_argument, 0, 'z'},
{"rleopt", no_argument, 0, 'r'},
{"split", required_argument, 0, 's'},
{"splitmargin", required_argument, 0, 'm'},
{"trackname", required_argument, 0, 't'},
{"language", required_argument, 0, 'l'},
{"video-format", required_argument, 0, 'v'},
Expand All @@ -217,7 +238,7 @@ int main(int argc, char *argv[])

while (1) {
int opt_index = 0;
int c = getopt_long(argc, argv, "zdgrt:l:v:f:w:h:x:y:p:a:o:q:s:", longopts, &opt_index);
int c = getopt_long(argc, argv, "zdgrt:l:v:f:w:h:x:y:p:a:o:q:s:m:", longopts, &opt_index);

if (c == -1)
break;
Expand Down Expand Up @@ -263,6 +284,9 @@ int main(int argc, char *argv[])
exit(1);
}
break;
case 'm':
parse_margins(optarg, args.splitmargin);
break;
case 'o':
tc_to_tcarray(optarg, offset_vals);
break;
Expand Down Expand Up @@ -349,6 +373,11 @@ int main(int argc, char *argv[])
args.frame_h = vfmt->h;
args.frame_w = vfmt->w;

if ((args.splitmargin[1] > (args.frame_h*3)/4) || (args.splitmargin[0] > (args.frame_w*3)/4)) {
printf("Excessive split margin(s), should be less than 3/4 of video height or width.\n");
exit(1);
}

// Some mapping
if (args.render_w == 0)
args.render_w = args.frame_w;
Expand Down
1 change: 1 addition & 0 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ typedef struct opts_s {
int storage_w;
int storage_h;
uint16_t quantize;
uint16_t splitmargin[2];
uint8_t dvd_mode;
uint8_t hinting;
uint8_t split;
Expand Down
10 changes: 5 additions & 5 deletions render.c
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ static void find_bbox_xsplit(image_t *frame, int x_start, int x_stop, const int
}
}

static int find_split(image_t *frame, uint8_t split_mode)
static int find_split(image_t *frame, opts_t *args)
{
const int margin = 8;
uint32_t best_score = (uint32_t)(-1);
Expand Down Expand Up @@ -528,12 +528,12 @@ static int find_split(image_t *frame, uint8_t split_mode)
find_bbox_ysplit(frame, yk, frame->suby2, margin, &eval[1]);

surface = BOX_AREA(eval[0]) + BOX_AREA(eval[1]);
if (surface < best_score) {
if (surface < best_score && abs(eval[0].y2 - eval[1].y1) >= args->splitmargin[0]) {
best_score = surface;
memcpy(frame->crops, eval, sizeof(eval));
}
}
if (split_mode == 3 || (split_mode == 2 && (frame->suby2 - frame->suby1) > frame->height/2.5)) {
if (args->split == 3 || (args->split == 2 && (frame->suby2 - frame->suby1) > frame->height/2.5)) {
//Search for a vertical split
for (xk = frame->subx1 + margin; xk < frame->subx2 - margin; xk+=margin) {
pixelExist = 0;
Expand All @@ -550,7 +550,7 @@ static int find_split(image_t *frame, uint8_t split_mode)
find_bbox_xsplit(frame, xk, frame->subx2, margin, &eval[1]);

surface = BOX_AREA(eval[0]) + BOX_AREA(eval[1]);
if (surface < best_score) {
if (surface < best_score && abs(eval[0].x2 - eval[1].x1) >= args->splitmargin[1]) {
best_score = surface;
memcpy(frame->crops, eval, sizeof(eval));
}
Expand Down Expand Up @@ -647,7 +647,7 @@ eventlist_t *render_subs(char *subfile, frate_t *frate, opts_t *args)
printf("Quantization failed for " FILENAME_FMT FILENAME_EXT ".\n", count);
exit(1);
}
if (args->split && find_split(frame, args->split)) {
if (args->split && find_split(frame, args)) {
if (args->quantize) {
write_png_palette(count, frame, &img, &res, args, 1);
} else {
Expand Down

0 comments on commit e99269b

Please sign in to comment.