Skip to content

Commit a0ba9c7

Browse files
committed
Make blend_render re-use buffer - thread-unsafe but more optimized
1 parent 276cf94 commit a0ba9c7

File tree

2 files changed

+11
-11
lines changed

2 files changed

+11
-11
lines changed

src/SubtitleOctopus.cpp

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ typedef struct {
2424

2525
buffer_t blend, blend_result;
2626

27-
int buffer_resize(buffer_t *buf, int new_size, int keep_content) {
27+
void* buffer_resize(buffer_t *buf, int new_size, int keep_content) {
2828
if (buf->size >= new_size) {
2929
if (buf->size >= 1.3 * new_size) {
3030
// big reduction request
@@ -34,7 +34,7 @@ int buffer_resize(buffer_t *buf, int new_size, int keep_content) {
3434
}
3535
if (buf->lessen_counter < 30) {
3636
// not reducing the buffer yet
37-
return 1;
37+
return buf->buffer;
3838
}
3939
}
4040

@@ -44,12 +44,12 @@ int buffer_resize(buffer_t *buf, int new_size, int keep_content) {
4444
} else {
4545
newbuf = malloc(new_size);
4646
}
47-
if (!newbuf) return 0;
47+
if (!newbuf) return NULL;
4848

4949
buf->buffer = newbuf;
5050
buf->size = new_size;
5151
buf->lessen_counter = 0;
52-
return 1;
52+
return buf->buffer;
5353
}
5454

5555
void buffer_init(buffer_t *buf) {
@@ -108,6 +108,8 @@ class SubtitleOctopus {
108108
resizeCanvas(frame_w, frame_h);
109109

110110
reloadFonts();
111+
buffer_init(&blend);
112+
buffer_init(&blend_result);
111113
}
112114

113115
/* TRACK */
@@ -143,7 +145,6 @@ class SubtitleOctopus {
143145
canvas_h = frame_h;
144146
canvas_w = frame_w;
145147
}
146-
147148
ASS_Image* renderImage(double time, int* changed) {
148149
ASS_Image *img = ass_render_frame(ass_renderer, track, (int) (time * 1000), changed);
149150
return img;
@@ -154,8 +155,9 @@ class SubtitleOctopus {
154155
ass_free_track(track);
155156
ass_renderer_done(ass_renderer);
156157
ass_library_done(ass_library);
158+
free(blend.buffer);
159+
free(blend_result.buffer);
157160
}
158-
159161
void reloadLibrary() {
160162
quitLibrary();
161163

@@ -238,15 +240,15 @@ void* libassjs_render_blend(double tm, int force, int *changed, double *blend_ti
238240

239241
// make float buffer for blending
240242
int width = max_x - min_x + 1, height = max_y - min_y + 1;
241-
float* buf = (float*)malloc(sizeof(float) * width * height * 4);
243+
float* buf = (float*)buffer_resize(&blend, sizeof(float) * width * height * 4, 0);
242244
if (buf == NULL) {
243245
printf("libass: error: cannot allocate buffer for blending");
244246
return NULL;
245247
}
246-
unsigned char *result = (unsigned char*)malloc(sizeof(unsigned char) * width * height * 4);
248+
unsigned char *result = (unsigned char*)buffer_resize(&blend_result,
249+
sizeof(unsigned char) * width * height * 4, 0);
247250
if (result == NULL) {
248251
printf("libass: error: cannot allocate result for blending");
249-
free(buf);
250252
return NULL;
251253
}
252254
memset(buf, 0, sizeof(float) * width * height * 4);
@@ -308,7 +310,6 @@ void* libassjs_render_blend(double tm, int force, int *changed, double *blend_ti
308310
}
309311

310312
// return the thing
311-
free(buf);
312313
*dest_x = min_x;
313314
*dest_y = min_y;
314315
*dest_width = width;

src/post-worker.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,6 @@ self.blendRender = function (force) {
209209
var blendH = Module.getValue(self.blendH, 'i32');
210210
// make a copy, as we should free the memory so subsequent calls can utilize it
211211
var result = new Uint8Array(HEAPU8.subarray(renderResult, renderResult + blendW * blendH * 4));
212-
Module._free(renderResult);
213212

214213
canvases = [{w: blendW, h: blendH, x: blendX, y: blendY, buffer: result.buffer}];
215214
buffers = [result.buffer];

0 commit comments

Comments
 (0)