summaryrefslogtreecommitdiff
path: root/src/encoder.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/encoder.c')
-rw-r--r--src/encoder.c73
1 files changed, 72 insertions, 1 deletions
diff --git a/src/encoder.c b/src/encoder.c
index b45e3b9..a670a11 100644
--- a/src/encoder.c
+++ b/src/encoder.c
@@ -8,15 +8,86 @@
#include "sstv.h"
#include "libsstv.h"
+/*
+ * Encoder context
+ */
+typedef struct {
+ /* common encoder stuff */
+ sstv_mode_t mode;
+} sstv_encoder_context_t;
+
+/* default encoder contexts, for when no allocation/deallocation routines are provided */
+static sstv_encoder_context_t default_encoder_context[SSTV_DEFAULT_ENCODER_CONTEXT_COUNT];
+static uint64_t default_encoder_context_usage = 0x0;
+
+
sstv_error_t
sstv_create_encoder(sstv_mode_t mode, uint8_t *image, size_t sample_rate, void **out_ctx)
{
- /* Check input */
+ sstv_encoder_context_t *ctx = NULL;
+
+ /* check input */
+ if (!out_ctx) {
+ return SSTV_BAD_PARAMETER;
+ }
+
+ /* create context */
+ if (sstv_malloc_user) {
+ /* user allocator */
+ ctx = (sstv_encoder_context_t *) sstv_malloc_user(sizeof(sstv_encoder_context_t));
+ if (!ctx) {
+ return SSTV_BAD_USER_ALLOC;
+ }
+ } else {
+ size_t i;
+ /* use default contexts */
+ for (i = 0; i < SSTV_DEFAULT_ENCODER_CONTEXT_COUNT; i++) {
+ if ((default_encoder_context_usage & (0x1 << i)) == 0) {
+ default_encoder_context_usage |= (0x1 << i);
+ ctx = &default_encoder_context[i];
+ break;
+ }
+ }
+ }
+
+ if (!ctx) {
+ return SSTV_INTERNAL_ERROR;
+ }
+
+ /* initialize context */
+ ctx->mode = mode;
+
+ /* set output */
+ *out_ctx = ctx;
+
+ /* all ok */
+ return SSTV_OK;
}
sstv_error_t
sstv_delete_encoder(void *ctx)
{
+ size_t i;
+
+ /* check default contexts */
+ for (i = 0; i < SSTV_DEFAULT_ENCODER_CONTEXT_COUNT; i++) {
+ if (ctx == default_encoder_context+i) {
+ default_encoder_context_usage &= ~(0x1 << i);
+ ctx = NULL;
+ break;
+ }
+ }
+
+ /* deallocate context */
+ if (ctx) {
+ if (!sstv_free_user) {
+ return SSTV_BAD_USER_DEALLOC;
+ }
+ sstv_free_user(ctx);
+ }
+
+ /* all ok */
+ return SSTV_OK;
}
sstv_error_t