Skip to content

Commit 992a113

Browse files
committed
Reduce heap usage more by using same array both for float-blending and as an int8 RGBA result
1 parent 5985057 commit 992a113

File tree

1 file changed

+22
-20
lines changed

1 file changed

+22
-20
lines changed

src/SubtitleOctopus.cpp

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ typedef struct {
2222
int lessen_counter;
2323
} buffer_t;
2424

25-
buffer_t blend, blend_result;
25+
buffer_t blend;
2626

2727
void* buffer_resize(buffer_t *buf, int new_size, int keep_content) {
2828
if (buf->size >= new_size) {
@@ -59,6 +59,10 @@ void buffer_init(buffer_t *buf) {
5959
buf->lessen_counter = 0;
6060
}
6161

62+
void buffer_free(buffer_t *buf) {
63+
free(buf->buffer);
64+
}
65+
6266
void msg_callback(int level, const char *fmt, va_list va, void *data) {
6367
if (level > log_level) // 6 for verbose
6468
return;
@@ -110,7 +114,6 @@ class SubtitleOctopus {
110114

111115
reloadFonts();
112116
buffer_init(&blend);
113-
buffer_init(&blend_result);
114117
}
115118

116119
/* TRACK */
@@ -156,8 +159,7 @@ class SubtitleOctopus {
156159
ass_free_track(track);
157160
ass_renderer_done(ass_renderer);
158161
ass_library_done(ass_library);
159-
free(blend.buffer);
160-
free(blend_result.buffer);
162+
buffer_free(&blend);
161163
}
162164
void reloadLibrary() {
163165
quitLibrary();
@@ -211,7 +213,7 @@ class SubtitleOctopus {
211213
};
212214

213215
const float MIN_UINT8_CAST = 0.9 / 255;
214-
const float MAX_UINT8_CAST = 256.0 / 255;
216+
const float MAX_UINT8_CAST = 255.9 / 255;
215217

216218
#define CLAMP_UINT8(value) ((value > MIN_UINT8_CAST) ? ((value < MAX_UINT8_CAST) ? (int)(value * 255) : 255) : 0)
217219

@@ -246,14 +248,7 @@ void* libassjs_render_blend(double tm, int force, int *changed, double *blend_ti
246248
printf("libass: error: cannot allocate buffer for blending");
247249
return NULL;
248250
}
249-
unsigned char *result = (unsigned char*)buffer_resize(&blend_result,
250-
sizeof(unsigned char) * width * height * 4, 0);
251-
if (result == NULL) {
252-
printf("libass: error: cannot allocate result for blending");
253-
return NULL;
254-
}
255251
memset(buf, 0, sizeof(float) * width * height * 4);
256-
memset(result, 0, sizeof(unsigned char) * width * height * 4);
257252

258253
// blend things in
259254
for (cur = img; cur != NULL; cur = cur->next) {
@@ -293,20 +288,27 @@ void* libassjs_render_blend(double tm, int force, int *changed, double *blend_ti
293288
}
294289
}
295290
}
296-
297-
// now build the result
291+
292+
// now build the result;
293+
// NOTE: we use a "view" over [float,float,float,float] array of pixels,
294+
// so we _must_ go left-right top-bottom to not mangle the result
295+
unsigned int *result = (unsigned int*)buf;
298296
for (int y = 0, buf_line_coord = 0; y < height; y++, buf_line_coord += width) {
299297
for (int x = 0; x < width; x++) {
298+
unsigned int pixel = 0;
300299
int buf_coord = (buf_line_coord + x) << 2;
301300
float alpha = buf[buf_coord + 3];
302301
if (alpha > MIN_UINT8_CAST) {
303-
for (int offset = 0; offset < 3; offset++) {
304-
// need to un-multiply the result
305-
float value = buf[buf_coord + offset] / alpha;
306-
result[buf_coord + offset] = CLAMP_UINT8(value);
307-
}
308-
result[buf_coord + 3] = CLAMP_UINT8(alpha);
302+
// need to un-multiply the result
303+
float value = buf[buf_coord] / alpha;
304+
pixel |= CLAMP_UINT8(value); // R
305+
value = buf[buf_coord + 1] / alpha;
306+
pixel |= CLAMP_UINT8(value) << 8; // G
307+
value = buf[buf_coord + 2] / alpha;
308+
pixel |= CLAMP_UINT8(value) << 16; // B
309+
pixel |= CLAMP_UINT8(alpha) << 24; // A
309310
}
311+
result[buf_line_coord + x] = pixel;
310312
}
311313
}
312314

0 commit comments

Comments
 (0)