Skip to content

Commit

Permalink
Detect and fix degenerate events to avoid bad coordinates.
Browse files Browse the repository at this point in the history
  • Loading branch information
cubicibo committed May 6, 2024
1 parent 1257871 commit 4940225
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 18 deletions.
11 changes: 11 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,17 @@ Moreover, the last table has debugging parameters. These should not have any pra
| ``--hinting`` | Flag to enable soft hinting in libass. |
+--------------------+--------------------------------------------------------+

Basic Scenarist BD example
--------------------------

:: ass2bdnxml -f 29.97 -v 1080i -s 2 -q 255 -r --liq-quality 98 subtitle.ass

- 1080i29.97 (``-v 1080i -f 29.97``)
- quantize with a maximum of 255 colours (``-q 255``)
- optimise palette layout (``-r``)
- Set quality to 98%, to enhance stream compression and palette allocation (``--liq-quality 98``)
- Set image split mode 2 (strong) ``-s 2``

Notes
-----

Expand Down
52 changes: 34 additions & 18 deletions render.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,12 @@ void eventlist_set(eventlist_t *list, image_t *ev, int index)

if (list->size <= index) {
list->size = index + 200;
list->events = realloc(list->events, sizeof(image_t*) * list->size);
image_t **new_list = realloc(list->events, sizeof(image_t*) * list->size);
if (!new_list) {
printf("Can't allocate memory.\n");
exit(1);
}
list->events = new_list;
}

list->events[index] = newev;
Expand Down Expand Up @@ -633,41 +638,52 @@ static uint8_t diff_frames(image_t *current, image_t *prev)
static int get_frame(ASS_Renderer *renderer, ASS_Track *track, image_t *prev_frame,
image_t *frame, uint64_t frame_cnt, frate_t *frate, opts_t *args)
{
//libass can return blank ASS_Images, we must remember whenever that happen as the changed
//flag returned by libass becomes meaningless, and we would corrupt the event.
static int prev_invalid = 0;
int changed;

uint64_t ms = frame_to_realtime_ms(frame_cnt, frate, SAMPLE_TC_PTSIN);
ASS_Image *img = ass_render_frame(renderer, track, ms, &changed);

if (changed && img) {
blend(frame, img, args->dimf, args->dim_flag);
//frame differ from the previous?
if (NULL == prev_frame) {
frame->in = frame_cnt;
} else if (diff_frames(frame, prev_frame)) {
frame->in = frame_cnt;
memcpy(prev_frame, frame, offsetof(image_t, out));
memcpy(prev_frame->buffer, frame->buffer, frame->stride*frame->height);
} else {
// img exists and is identical to prev.
++frame->out;
return 1;
}
frame->out = frame_cnt + 1;

//Sometime sampling time is on an active event but the blended image is transparent
// because the composition coefficients are weak -> discard
if (frame->subx1 == -1 || frame->suby1 == -1) {
if (frame->subx1 > -1 && frame->suby1 > -1) {
//frame differ from the previous?
if (NULL == prev_frame) {
frame->in = frame_cnt;
} else if (diff_frames(frame, prev_frame)) {
frame->in = frame_cnt;
memcpy(prev_frame, frame, offsetof(image_t, out));
memcpy(prev_frame->buffer, frame->buffer, frame->stride*frame->height);
} else {
// img exists and is identical to prev.
++frame->out;
return 1;
}
frame->out = frame_cnt + 1;
} else {
//Sometime sampling time is on an active event but the blended image is transparent
// because the composition coefficients are weak -> discard
prev_invalid = 1;
if (prev_frame)
prev_frame->in = (uint64_t)(-1);
return 2;
}
prev_invalid = 0;

return 3;
} else if (!changed && img) {
if (prev_invalid)
return 2;
++frame->out;
return 1;
} else {
//No event, set prev_frame as invalid
//No event, change prev_frame content
if (prev_frame)
prev_frame->in = (uint64_t)(-1);
prev_invalid = 0;
return 0;
}
}
Expand Down

0 comments on commit 4940225

Please sign in to comment.