Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7fc34cf

Browse files
committedMar 23, 2020
Event time findings are now proper members (but not exported yet)
1 parent 6d3a5c7 commit 7fc34cf

File tree

1 file changed

+82
-75
lines changed

1 file changed

+82
-75
lines changed
 

‎src/SubtitleOctopus.cpp

+82-75
Original file line numberDiff line numberDiff line change
@@ -80,29 +80,16 @@ const float MAX_UINT8_CAST = 255.9 / 255;
8080
#define CLAMP_UINT8(value) ((value > MIN_UINT8_CAST) ? ((value < MAX_UINT8_CAST) ? (int)(value * 255) : 255) : 0)
8181

8282
typedef struct {
83-
public:
8483
int changed;
8584
double blend_time;
8685
int dest_x, dest_y, dest_width, dest_height;
8786
unsigned char* image;
8887
} RenderBlendResult;
8988

90-
double libassjs_find_next_event_start(double tm) {
91-
if (!track || track->n_events == 0) return -1;
92-
93-
ASS_Event *cur = track->events;
94-
long long now = (long long)(tm * 1000);
95-
long long closest = -1;
96-
97-
for (int i = 0; i < track->n_events; i++, cur++) {
98-
long long start = cur->Start;
99-
if (start >= now && (start < closest || closest == -1)) {
100-
closest = start;
101-
}
102-
}
103-
104-
return closest / 1000.0;
105-
}
89+
typedef struct {
90+
double eventFinish, emptyFinish;
91+
int is_animated;
92+
} EventStopTimesResult;
10693

10794
static int _is_move_tag_animated(char *begin, char *end) {
10895
int params[6];
@@ -201,63 +188,6 @@ static int _is_event_animated(ASS_Event *event) {
201188
return 0;
202189
}
203190

204-
static void detect_animated_events() {
205-
ASS_Event *cur = track->events;
206-
int *animated = is_animated_events;
207-
for (int i = 0; i < track->n_events; i++, cur++, animated++) {
208-
*animated = _is_event_animated(cur);
209-
}
210-
}
211-
212-
void libassjs_find_event_stop_times(double tm, double *eventFinish, double *emptyFinish, int *is_animated) {
213-
if (!track || track->n_events == 0) {
214-
*eventFinish = *emptyFinish = -1;
215-
return;
216-
}
217-
218-
ASS_Event *cur = track->events;
219-
long long now = (long long)(tm * 1000);
220-
221-
long long minFinish = -1, maxFinish = -1, minStart = -1;
222-
int current_animated = 0;
223-
224-
for (int i = 0; i < track->n_events; i++, cur++) {
225-
long long start = cur->Start;
226-
long long finish = start + cur->Duration;
227-
if (start <= now) {
228-
if (finish > now) {
229-
if (finish < minFinish || minFinish == -1) {
230-
minFinish = finish;
231-
}
232-
if (finish > maxFinish) {
233-
maxFinish = finish;
234-
}
235-
if (!current_animated) current_animated = m_is_event_animated[i];
236-
}
237-
} else if (start < minStart || minStart == -1) {
238-
minStart = start;
239-
}
240-
}
241-
*is_animated = current_animated;
242-
243-
if (minFinish != -1) {
244-
// some event is going on, so we need to re-draw either when it stops
245-
// or when some other event starts
246-
*eventFinish = ((minFinish < minStart) ? minFinish : minStart) / 1000.0;
247-
} else {
248-
// there's no current event, so no need to draw anything
249-
*eventFinish = -1;
250-
}
251-
252-
if (minFinish == maxFinish && (minStart == -1 || minStart > maxFinish)) {
253-
// there's empty space after this event ends
254-
*emptyFinish = minStart / 1000.0;
255-
} else {
256-
// there's no empty space after eventFinish happens
257-
*emptyFinish = *eventFinish;
258-
}
259-
}
260-
261191
class SubtitleOctopus {
262192
public:
263193
ASS_Library* ass_library;
@@ -319,7 +249,7 @@ class SubtitleOctopus {
319249
printf("cannot parse animated events\n");
320250
exit(5);
321251
}
322-
detect_animated_events();
252+
detectAnimatedEvents();
323253
}
324254

325255
void createTrackMem(char *buf, unsigned long bufsize) {
@@ -521,7 +451,84 @@ class SubtitleOctopus {
521451
return &m_blendResult;
522452
}
523453

454+
double findNextEventStart(double tm) {
455+
if (!track || track->n_events == 0) return -1;
456+
457+
ASS_Event *cur = track->events;
458+
long long now = (long long)(tm * 1000);
459+
long long closest = -1;
460+
461+
for (int i = 0; i < track->n_events; i++, cur++) {
462+
long long start = cur->Start;
463+
if (start >= now && (start < closest || closest == -1)) {
464+
closest = start;
465+
}
466+
}
467+
468+
return closest / 1000.0;
469+
}
470+
471+
EventStopTimesResult findEventStopTimes(double tm) {
472+
EventStopTimesResult result;
473+
if (!track || track->n_events == 0) {
474+
result.eventFinish = result.emptyFinish = -1;
475+
return result;
476+
}
477+
478+
ASS_Event *cur = track->events;
479+
long long now = (long long)(tm * 1000);
480+
481+
long long minFinish = -1, maxFinish = -1, minStart = -1;
482+
int current_animated = 0;
483+
484+
for (int i = 0; i < track->n_events; i++, cur++) {
485+
long long start = cur->Start;
486+
long long finish = start + cur->Duration;
487+
if (start <= now) {
488+
if (finish > now) {
489+
if (finish < minFinish || minFinish == -1) {
490+
minFinish = finish;
491+
}
492+
if (finish > maxFinish) {
493+
maxFinish = finish;
494+
}
495+
if (!current_animated) current_animated = m_is_event_animated[i];
496+
}
497+
} else if (start < minStart || minStart == -1) {
498+
minStart = start;
499+
}
500+
}
501+
result.is_animated = current_animated;
502+
503+
if (minFinish != -1) {
504+
// some event is going on, so we need to re-draw either when it stops
505+
// or when some other event starts
506+
result.eventFinish = ((minFinish < minStart) ? minFinish : minStart) / 1000.0;
507+
} else {
508+
// there's no current event, so no need to draw anything
509+
result.eventFinish = -1;
510+
}
511+
512+
if (minFinish == maxFinish && (minStart == -1 || minStart > maxFinish)) {
513+
// there's empty space after this event ends
514+
result.emptyFinish = minStart / 1000.0;
515+
} else {
516+
// there's no empty space after eventFinish happens
517+
result.emptyFinish = result.eventFinish;
518+
}
519+
520+
return result;
521+
}
522+
524523
private:
524+
void detectAnimatedEvents() {
525+
ASS_Event *cur = track->events;
526+
int *animated = m_is_event_animated;
527+
for (int i = 0; i < track->n_events; i++, cur++, animated++) {
528+
*animated = _is_event_animated(cur);
529+
}
530+
}
531+
525532
ReusableBuffer m_blend;
526533
RenderBlendResult m_blendResult;
527534
int *m_is_event_animated;

0 commit comments

Comments
 (0)
Please sign in to comment.