18
18
#endif
19
19
20
20
int log_level = 3 ;
21
- int *is_event_animated;
22
21
23
- typedef struct {
24
- void *buffer;
25
- int size;
26
- int lessen_counter;
27
- } buffer_t ;
22
+ class ReusableBuffer {
23
+ public:
24
+ ReusableBuffer (): buffer(NULL ), size(-1 ), lessen_counter(0 ) {}
25
+ ~ReusableBuffer () {
26
+ free (buffer);
27
+ }
28
+ void clear () {
29
+ free (buffer);
30
+ buffer = NULL ;
31
+ size = -1 ;
32
+ lessen_counter = 0 ;
33
+ }
34
+ void *take (int new_size, bool keep_content) {
35
+ if (size >= new_size) {
36
+ if (size >= 1.3 * new_size) {
37
+ // big reduction request
38
+ lessen_counter++;
39
+ } else {
40
+ lessen_counter = 0 ;
41
+ }
42
+ if (lessen_counter < 10 ) {
43
+ // not reducing the buffer yet
44
+ return buffer;
45
+ }
46
+ }
28
47
29
- void * buffer_resize (buffer_t *buf, int new_size, int keep_content) {
30
- if (buf->size >= new_size) {
31
- if (buf->size >= 1.3 * new_size) {
32
- // big reduction request
33
- buf->lessen_counter ++;
48
+ void *newbuf;
49
+ if (keep_content) {
50
+ newbuf = realloc (buffer, new_size);
34
51
} else {
35
- buf->lessen_counter = 0 ;
36
- }
37
- if (buf->lessen_counter < 10 ) {
38
- // not reducing the buffer yet
39
- return buf->buffer ;
52
+ newbuf = malloc (new_size);
40
53
}
41
- }
54
+ if (!newbuf) return NULL ;
42
55
43
- void *newbuf ;
44
- if (keep_content) {
45
- newbuf = realloc (buf-> buffer , new_size) ;
46
- } else {
47
- newbuf = malloc (new_size) ;
56
+ if (!keep_content) free (buffer) ;
57
+ buffer = newbuf;
58
+ size = new_size;
59
+ lessen_counter = 0 ;
60
+ return buffer ;
48
61
}
49
- if (!newbuf) return NULL ;
50
62
51
- if (!keep_content) free (buf->buffer );
52
- buf->buffer = newbuf;
53
- buf->size = new_size;
54
- buf->lessen_counter = 0 ;
55
- return buf->buffer ;
56
- }
57
-
58
- void buffer_init (buffer_t *buf) {
59
- buf->buffer = NULL ;
60
- buf->size = -1 ;
61
- buf->lessen_counter = 0 ;
62
- }
63
-
64
- void buffer_free (buffer_t *buf) {
65
- free (buf->buffer );
66
- buffer_init (buf);
67
- }
63
+ private:
64
+ void *buffer;
65
+ int size;
66
+ int lessen_counter;
67
+ };
68
68
69
69
void msg_callback (int level, const char *fmt, va_list va, void *data) {
70
70
if (level > log_level) // 6 for verbose
@@ -232,7 +232,7 @@ void libassjs_find_event_stop_times(double tm, double *eventFinish, double *empt
232
232
if (finish > maxFinish) {
233
233
maxFinish = finish;
234
234
}
235
- if (!current_animated) current_animated = is_event_animated [i];
235
+ if (!current_animated) current_animated = m_is_event_animated [i];
236
236
}
237
237
} else if (start < minStart || minStart == -1 ) {
238
238
minStart = start;
@@ -300,8 +300,8 @@ class SubtitleOctopus {
300
300
resizeCanvas (frame_w, frame_h);
301
301
302
302
reloadFonts ();
303
- buffer_init (& m_blend);
304
- is_event_animated = NULL ;
303
+ m_blend. clear ( );
304
+ m_is_event_animated = NULL ;
305
305
}
306
306
307
307
/* TRACK */
@@ -313,9 +313,9 @@ class SubtitleOctopus {
313
313
exit (4 );
314
314
}
315
315
316
- free (is_event_animated );
317
- is_event_animated = (int *)malloc (sizeof (int ) * track->n_events );
318
- if (is_event_animated == NULL ) {
316
+ free (m_is_event_animated );
317
+ m_is_event_animated = (int *)malloc (sizeof (int ) * track->n_events );
318
+ if (m_is_event_animated == NULL ) {
319
319
printf (" cannot parse animated events\n " );
320
320
exit (5 );
321
321
}
@@ -336,8 +336,8 @@ class SubtitleOctopus {
336
336
ass_free_track (track);
337
337
track = NULL ;
338
338
}
339
- free (is_event_animated );
340
- is_event_animated = NULL ;
339
+ free (m_is_event_animated );
340
+ m_is_event_animated = NULL ;
341
341
}
342
342
/* TRACK */
343
343
@@ -357,9 +357,9 @@ class SubtitleOctopus {
357
357
ass_free_track (track);
358
358
ass_renderer_done (ass_renderer);
359
359
ass_library_done (ass_library);
360
- buffer_free (& m_blend);
361
- free (is_event_animated );
362
- is_event_animated = NULL ;
360
+ m_blend. clear ( );
361
+ free (m_is_event_animated );
362
+ m_is_event_animated = NULL ;
363
363
}
364
364
void reloadLibrary () {
365
365
quitLibrary ();
@@ -442,7 +442,7 @@ class SubtitleOctopus {
442
442
443
443
// make float buffer for blending
444
444
int width = max_x - min_x + 1 , height = max_y - min_y + 1 ;
445
- float * buf = (float *)buffer_resize (& m_blend, sizeof (float ) * width * height * 4 , 0 );
445
+ float * buf = (float *)m_blend. take ( sizeof (float ) * width * height * 4 , 0 );
446
446
if (buf == NULL ) {
447
447
printf (" libass: error: cannot allocate buffer for blending" );
448
448
return NULL ;
@@ -522,10 +522,13 @@ class SubtitleOctopus {
522
522
}
523
523
524
524
private:
525
- buffer_t m_blend;
525
+ ReusableBuffer m_blend;
526
526
RenderBlendResult m_blendResult;
527
+ int *m_is_event_animated;
527
528
};
528
529
529
530
int main (int argc, char *argv[]) { return 0 ; }
530
531
531
- #include " ./SubOctpInterface.cpp"
532
+ #ifdef __EMSCRIPTEN__
533
+ #include " ./SubOctpInterface.cpp"
534
+ #endif
0 commit comments