Skip to content

Buffer refactor and buffer bug fixes #133

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Apr 2, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
100 changes: 58 additions & 42 deletions src/SubtitleOctopus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <cstdint>
#include "../lib/libass/libass/ass.h"

#include "libass.cpp"
Expand All @@ -19,50 +20,66 @@

int log_level = 3;

typedef struct {
class ReusableBuffer2D {
private:
void *buffer;
int size;
size_t size;
int lessen_counter;
} buffer_t;

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++;
} else {
buf->lessen_counter = 0;
}
if (buf->lessen_counter < 10) {
// not reducing the buffer yet
return buf->buffer;
}
}

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

~ReusableBuffer2D() {
free(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 clear() {
free(buffer);
buffer = NULL;
size = 0;
lessen_counter = 0;
}

void buffer_init(buffer_t *buf) {
buf->buffer = NULL;
buf->size = -1;
buf->lessen_counter = 0;
}
/*
* Request a raw pointer to a buffer being able to hold at least
* x times y values of size member_size.
* If zero is set to true, the requested region will be zero-initialised.
* On failure NULL is returned.
* The pointer is valid during the lifetime of the ReusableBuffer
* object until the next call to get_rawbuf or clear.
*/
void *get_rawbuf(size_t x, size_t y, size_t member_size, bool zero) {
if (x > SIZE_MAX / member_size / y)
return NULL;

size_t new_size = x * y * member_size;
if (!new_size) new_size = 1;
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
if (zero)
memset(buffer, 0, new_size);
return buffer;
}
}

void buffer_free(buffer_t *buf) {
free(buf->buffer);
}
free(buffer);
buffer = malloc(new_size);
if (buffer) {
size = new_size;
memset(buffer, 0, size);
} else
size = 0;
lessen_counter = 0;
return buffer;
}
};

void msg_callback(int level, const char *fmt, va_list va, void *data) {
if (level > log_level) // 6 for verbose
Expand Down Expand Up @@ -131,7 +148,7 @@ class SubtitleOctopus {
resizeCanvas(frame_w, frame_h);

reloadFonts();
buffer_init(&m_blend);
m_blend.clear();
}

/* TRACK */
Expand Down Expand Up @@ -177,7 +194,7 @@ class SubtitleOctopus {
ass_free_track(track);
ass_renderer_done(ass_renderer);
ass_library_done(ass_library);
buffer_free(&m_blend);
m_blend.clear();
}
void reloadLibrary() {
quitLibrary();
Expand Down Expand Up @@ -268,12 +285,11 @@ class SubtitleOctopus {
}

// make float buffer for blending
float* buf = (float*)buffer_resize(&m_blend, sizeof(float) * width * height * 4, 0);
float* buf = (float*)m_blend.get_rawbuf(width, height, sizeof(float) * 4, true);
if (buf == NULL) {
fprintf(stderr, "jso: cannot allocate buffer for blending\n");
return &m_blendResult;
}
memset(buf, 0, sizeof(float) * width * height * 4);

// blend things in
for (cur = img; cur != NULL; cur = cur->next) {
Expand Down Expand Up @@ -348,7 +364,7 @@ class SubtitleOctopus {
}

private:
buffer_t m_blend;
ReusableBuffer2D m_blend;
RenderBlendResult m_blendResult;
};

Expand Down