Skip to content

Commit

Permalink
Compute TC to match mpv PTS of ASS events.
Browse files Browse the repository at this point in the history
  • Loading branch information
cubicibo committed Oct 9, 2023
1 parent b8309e9 commit 0c1bd28
Showing 1 changed file with 15 additions and 11 deletions.
26 changes: 15 additions & 11 deletions render.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,13 @@
#define FILENAME_EXT ".png"

#define BOX_AREA(box) ((box.x2-box.x1)*(box.y2-box.y1))
#define DIV_ROUND_CLOSEST(n, d) (((n) + (d >> 1))/(d))

typedef enum SamplingFlag_s {
SAMPLE_TC_IN = 0,
SAMPLE_TC_OUT,
SAMPLE_TC_MID,
SAMPLE_TC_PTSIN,
SAMPLE_TC_PTSIN_INT,
SAMPLE_TC_PTSMID,
SAMPLE_TC_PTSMID_INT,
INVALID_SAMPLING
} SamplingFlag_t;

Expand Down Expand Up @@ -572,12 +574,14 @@ static int find_split(image_t *frame, opts_t *args)

static uint64_t frame_to_realtime_ms(uint64_t frame_cnt, frate_t *frate, SamplingFlag_t flag)
{
if (flag == SAMPLE_TC_OUT) {
return (uint64_t)floor((1000 * frame_cnt * frate->denom)/(double)frate->num);
} else if (flag == SAMPLE_TC_IN) {
return (uint64_t)ceil((1000*(frame_cnt - 1) * frate->denom)/(double)frate->num);
} else if (flag == SAMPLE_TC_MID) {
return (uint64_t)round(((1000*frame_cnt * frate->denom)/frate->num) - (500*frate->denom)/frate->num);
if (flag == SAMPLE_TC_PTSIN) {
return (uint64_t)round((1000*((uint64_t)frame_cnt - 1) * frate->denom)/(double)frate->num);
} else if (flag == SAMPLE_TC_PTSIN_INT) {
return (uint64_t)DIV_ROUND_CLOSEST(1000*((uint64_t)frame_cnt - 1) * frate->denom, (uint64_t)frate->num);
} else if (flag == SAMPLE_TC_PTSMID) {
return (uint64_t)round(((1000*(uint64_t)frame_cnt - 500) * frate->denom)/(double)frate->num);
} else if (flag == SAMPLE_TC_PTSMID_INT) {
return (uint64_t)DIV_ROUND_CLOSEST((1000*(uint64_t)frame_cnt - 500) * frate->denom, (uint64_t)frate->num);
}
fprintf(stderr, "Invalid sampling flag.\n");
exit(1);
Expand All @@ -588,7 +592,7 @@ static int get_frame(ASS_Renderer *renderer, ASS_Track *track,
{
int changed;

uint64_t ms = frame_to_realtime_ms(frame_cnt, frate, SAMPLE_TC_MID);
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) {
Expand Down Expand Up @@ -691,7 +695,7 @@ eventlist_t *render_subs(char *subfile, frate_t *frate, opts_t *args, liqopts_t
break;
case 0:
{
tm = (uint64_t)ass_step_sub(track, frame_to_realtime_ms(frame_cnt, frate, SAMPLE_TC_MID), 1);
tm = (uint64_t)ass_step_sub(track, frame_to_realtime_ms(frame_cnt, frate, SAMPLE_TC_PTSIN), 1);
uint64_t offset = (tm*frate->num)/(frate->denom*1000);

if (!tm && frame_cnt > 1)
Expand Down

0 comments on commit 0c1bd28

Please sign in to comment.