Skip to content

[Jellyfin] Rework buffer into C++ class #120

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

Closed
wants to merge 4 commits into from
Closed
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
96 changes: 54 additions & 42 deletions src/SubtitleOctopus.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,50 +19,59 @@

int log_level = 3;

typedef struct {
class ReusableBuffer {
private:
void *buffer;
int 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;
}
size_t size;
size_t lessen_counter;

public:
ReusableBuffer(): buffer(NULL), size(0), lessen_counter(0) {}

~ReusableBuffer() {
free(buffer);
}

void *newbuf;
if (keep_content) {
newbuf = realloc(buf->buffer, new_size);
} else {
newbuf = malloc(new_size);
void clear() {
free(buffer);
buffer = NULL;
size = 0;
lessen_counter = 0;
}
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 *ensure_size(size_t 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_init(buffer_t *buf) {
buf->buffer = NULL;
buf->size = -1;
buf->lessen_counter = 0;
}
void *newbuf;
if (keep_content) {
newbuf = realloc(buffer, new_size);
} else {
newbuf = malloc(new_size);
}
if (!newbuf) return NULL;

void buffer_free(buffer_t *buf) {
free(buf->buffer);
}
if (!keep_content) free(buffer);
buffer = newbuf;
size = new_size;
lessen_counter = 0;
return buffer;
}

size_t capacity() const {
return size;
}
};

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 +140,7 @@ class SubtitleOctopus {
resizeCanvas(frame_w, frame_h);

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

/* TRACK */
Expand Down Expand Up @@ -177,7 +186,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 +277,13 @@ class SubtitleOctopus {
}

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

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

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

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

#ifdef __EMSCRIPTEN__
#include "./SubOctpInterface.cpp"
#endif
Comment on lines +367 to +369
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

First of all, unrelated to the buffer refactor.
Second: Are there some tests or some other use for compiling this without emscripten? If not, I see no use in adding the guard in the first place.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It allows you to compile the file alone, iirc.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know, but is there even any useful standalone program or tests you can build with this file? For some reason SubtitleOctopus.cpp has a main function, but all it does is to immediately exit, which is not remotely useful.
If there is no use for compiling it without emscripten, then the guard is not necessary and even misleading as it suggest there is some use in compiling it without emscripten.
If, on the other hand, there are some hidden tests which use this standalone we should integrate them into our CI.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just use g++ SubtitleOctopus.cpp to see if it compiles without having to run the full build process.

This has the same purpose:

#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#else
// make IDE happy
#define emscripten_get_now() 0.0
#endif

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

make -C src would also do, but if it’s already partly guarded anyway .. oh well. At least mention in the commit-message that this is only to quickly check for compiler warnings/errors and there’s no use to the compiled program or otheriwse.