Skip to content

Commit

Permalink
Rework buffer into C++ class
Browse files Browse the repository at this point in the history
  • Loading branch information
JustAMan committed Mar 23, 2020
1 parent 6980deb commit 6d3a5c7
Showing 1 changed file with 57 additions and 54 deletions.
111 changes: 57 additions & 54 deletions src/SubtitleOctopus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,53 +18,53 @@
#endif

int log_level = 3;
int *is_event_animated;

typedef struct {
void *buffer;
int size;
int lessen_counter;
} buffer_t;
class ReusableBuffer {
public:
ReusableBuffer(): buffer(NULL), size(-1), lessen_counter(0) {}
~ReusableBuffer() {
free(buffer);
}
void clear() {
free(buffer);
buffer = NULL;
size = -1;
lessen_counter = 0;
}
void *take(int new_size, bool keep_content) {
if (size >= new_size) {
if (size >= 1.3 * new_size) {
// big reduction request
lessen_counter++;
} else {
lessen_counter = 0;
}
if (lessen_counter < 10) {
// not reducing the buffer yet
return buffer;
}
}

void* buffer_resize(buffer_t *buf, int new_size, int keep_content) {
if (buf->size >= new_size) {
if (buf->size >= 1.3 * new_size) {
// big reduction request
buf->lessen_counter++;
void *newbuf;
if (keep_content) {
newbuf = realloc(buffer, new_size);
} else {
buf->lessen_counter = 0;
}
if (buf->lessen_counter < 10) {
// not reducing the buffer yet
return buf->buffer;
newbuf = malloc(new_size);
}
}
if (!newbuf) return NULL;

void *newbuf;
if (keep_content) {
newbuf = realloc(buf->buffer, new_size);
} else {
newbuf = malloc(new_size);
if (!keep_content) free(buffer);
buffer = newbuf;
size = new_size;
lessen_counter = 0;
return buffer;
}
if (!newbuf) return NULL;

if (!keep_content) free(buf->buffer);
buf->buffer = newbuf;
buf->size = new_size;
buf->lessen_counter = 0;
return buf->buffer;
}

void buffer_init(buffer_t *buf) {
buf->buffer = NULL;
buf->size = -1;
buf->lessen_counter = 0;
}

void buffer_free(buffer_t *buf) {
free(buf->buffer);
buffer_init(buf);
}
private:
void *buffer;
int size;
int lessen_counter;
};

void msg_callback(int level, const char *fmt, va_list va, void *data) {
if (level > log_level) // 6 for verbose
Expand Down Expand Up @@ -232,7 +232,7 @@ void libassjs_find_event_stop_times(double tm, double *eventFinish, double *empt
if (finish > maxFinish) {
maxFinish = finish;
}
if (!current_animated) current_animated = is_event_animated[i];
if (!current_animated) current_animated = m_is_event_animated[i];
}
} else if (start < minStart || minStart == -1) {
minStart = start;
Expand Down Expand Up @@ -300,8 +300,8 @@ class SubtitleOctopus {
resizeCanvas(frame_w, frame_h);

reloadFonts();
buffer_init(&m_blend);
is_event_animated = NULL;
m_blend.clear();
m_is_event_animated = NULL;
}

/* TRACK */
Expand All @@ -313,9 +313,9 @@ class SubtitleOctopus {
exit(4);
}

free(is_event_animated);
is_event_animated = (int*)malloc(sizeof(int) * track->n_events);
if (is_event_animated == NULL) {
free(m_is_event_animated);
m_is_event_animated = (int*)malloc(sizeof(int) * track->n_events);
if (m_is_event_animated == NULL) {
printf("cannot parse animated events\n");
exit(5);
}
Expand All @@ -336,8 +336,8 @@ class SubtitleOctopus {
ass_free_track(track);
track = NULL;
}
free(is_event_animated);
is_event_animated = NULL;
free(m_is_event_animated);
m_is_event_animated = NULL;
}
/* TRACK */

Expand All @@ -357,9 +357,9 @@ class SubtitleOctopus {
ass_free_track(track);
ass_renderer_done(ass_renderer);
ass_library_done(ass_library);
buffer_free(&m_blend);
free(is_event_animated);
is_event_animated = NULL;
m_blend.clear();
free(m_is_event_animated);
m_is_event_animated = NULL;
}
void reloadLibrary() {
quitLibrary();
Expand Down Expand Up @@ -442,7 +442,7 @@ class SubtitleOctopus {

// make float buffer for blending
int width = max_x - min_x + 1, height = max_y - min_y + 1;
float* buf = (float*)buffer_resize(&m_blend, sizeof(float) * width * height * 4, 0);
float* buf = (float*)m_blend.take(sizeof(float) * width * height * 4, 0);
if (buf == NULL) {
printf("libass: error: cannot allocate buffer for blending");
return NULL;
Expand Down Expand Up @@ -522,10 +522,13 @@ class SubtitleOctopus {
}

private:
buffer_t m_blend;
ReusableBuffer m_blend;
RenderBlendResult m_blendResult;
int *m_is_event_animated;
};

int main(int argc, char *argv[]) { return 0; }

#include "./SubOctpInterface.cpp"
#ifdef __EMSCRIPTEN__
#include "./SubOctpInterface.cpp"
#endif

0 comments on commit 6d3a5c7

Please sign in to comment.