Skip to content

Commit 276cf94

Browse files
committed
Implement internal buffer holding structure, change codestyle
1 parent d6b4615 commit 276cf94

File tree

1 file changed

+54
-23
lines changed

1 file changed

+54
-23
lines changed

src/SubtitleOctopus.cpp

Lines changed: 54 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,49 @@
1616

1717
int log_level = 3;
1818

19-
void msg_callback(int level, const char *fmt, va_list va, void *data)
20-
{
19+
typedef struct {
20+
void *buffer;
21+
int size;
22+
int lessen_counter;
23+
} buffer_t;
24+
25+
buffer_t blend, blend_result;
26+
27+
int buffer_resize(buffer_t *buf, int new_size, int keep_content) {
28+
if (buf->size >= new_size) {
29+
if (buf->size >= 1.3 * new_size) {
30+
// big reduction request
31+
buf->lessen_counter++;
32+
} else {
33+
buf->lessen_counter = 0;
34+
}
35+
if (buf->lessen_counter < 30) {
36+
// not reducing the buffer yet
37+
return 1;
38+
}
39+
}
40+
41+
void *newbuf;
42+
if (keep_content) {
43+
newbuf = realloc(buf->buffer, new_size);
44+
} else {
45+
newbuf = malloc(new_size);
46+
}
47+
if (!newbuf) return 0;
48+
49+
buf->buffer = newbuf;
50+
buf->size = new_size;
51+
buf->lessen_counter = 0;
52+
return 1;
53+
}
54+
55+
void buffer_init(buffer_t *buf) {
56+
buf->buffer = NULL;
57+
buf->size = -1;
58+
buf->lessen_counter = 0;
59+
}
60+
61+
void msg_callback(int level, const char *fmt, va_list va, void *data) {
2162
if (level > log_level) // 6 for verbose
2263
return;
2364
printf("libass: ");
@@ -172,13 +213,11 @@ const float MAX_UINT8_CAST = 256.0 / 255;
172213
#define CLAMP_UINT8(value) ((value > MIN_UINT8_CAST) ? ((value < MAX_UINT8_CAST) ? (int)(value * 255) : 255) : 0)
173214

174215
void* libassjs_render_blend(double tm, int force, int *changed, double *blend_time,
175-
int *dest_x, int *dest_y, int *dest_width, int *dest_height)
176-
{
216+
int *dest_x, int *dest_y, int *dest_width, int *dest_height) {
177217
*blend_time = 0.0;
178218

179219
ASS_Image *img = ass_render_frame(ass_renderer, track, (int)(tm * 1000), changed);
180-
if (img == NULL || (*changed == 0 && !force))
181-
{
220+
if (img == NULL || (*changed == 0 && !force)) {
182221
return NULL;
183222
}
184223

@@ -188,8 +227,7 @@ void* libassjs_render_blend(double tm, int force, int *changed, double *blend_ti
188227
int min_x = img->dst_x, min_y = img->dst_y;
189228
int max_x = img->dst_x + img->w - 1, max_y = img->dst_y + img->h - 1;
190229
ASS_Image *cur;
191-
for (cur = img->next; cur != NULL; cur = cur->next)
192-
{
230+
for (cur = img->next; cur != NULL; cur = cur->next) {
193231
if (cur->dst_x < min_x) min_x = cur->dst_x;
194232
if (cur->dst_y < min_y) min_y = cur->dst_y;
195233
int right = cur->dst_x + cur->w - 1;
@@ -201,14 +239,12 @@ void* libassjs_render_blend(double tm, int force, int *changed, double *blend_ti
201239
// make float buffer for blending
202240
int width = max_x - min_x + 1, height = max_y - min_y + 1;
203241
float* buf = (float*)malloc(sizeof(float) * width * height * 4);
204-
if (buf == NULL)
205-
{
242+
if (buf == NULL) {
206243
printf("libass: error: cannot allocate buffer for blending");
207244
return NULL;
208245
}
209246
unsigned char *result = (unsigned char*)malloc(sizeof(unsigned char) * width * height * 4);
210-
if (result == NULL)
211-
{
247+
if (result == NULL) {
212248
printf("libass: error: cannot allocate result for blending");
213249
free(buf);
214250
return NULL;
@@ -217,8 +253,7 @@ void* libassjs_render_blend(double tm, int force, int *changed, double *blend_ti
217253
memset(result, 0, sizeof(unsigned char) * width * height * 4);
218254

219255
// blend things in
220-
for (cur = img; cur != NULL; cur = cur->next)
221-
{
256+
for (cur = img; cur != NULL; cur = cur->next) {
222257
int curw = cur->w, curh = cur->h;
223258
if (curw == 0 || curh == 0) continue; // skip empty images
224259
int a = (255 - (cur->color & 0xFF));
@@ -257,16 +292,12 @@ void* libassjs_render_blend(double tm, int force, int *changed, double *blend_ti
257292
}
258293

259294
// now build the result
260-
for (int y = 0, buf_line_coord = 0; y < height; y++, buf_line_coord += width)
261-
{
262-
for (int x = 0; x < width; x++)
263-
{
295+
for (int y = 0, buf_line_coord = 0; y < height; y++, buf_line_coord += width) {
296+
for (int x = 0; x < width; x++) {
264297
int buf_coord = (buf_line_coord + x) << 2;
265298
float alpha = buf[buf_coord + 3];
266-
if (alpha > MIN_UINT8_CAST)
267-
{
268-
for (int offset = 0; offset < 3; offset++)
269-
{
299+
if (alpha > MIN_UINT8_CAST) {
300+
for (int offset = 0; offset < 3; offset++) {
270301
// need to un-multiply the result
271302
float value = buf[buf_coord + offset] / alpha;
272303
result[buf_coord + offset] = CLAMP_UINT8(value);
@@ -286,6 +317,6 @@ void* libassjs_render_blend(double tm, int force, int *changed, double *blend_ti
286317
return result;
287318
}
288319

289-
int main(int argc, char *argv[]) {}
320+
int main(int argc, char *argv[]) { return 0; }
290321

291322
#include "./SubOctpInterface.cpp"

0 commit comments

Comments
 (0)