shithub: libopusenc

Download patch

ref: d9b84e2cb8d556d75f796311e090cb523ee50777
parent: 4fca05b7dc011f7290b94f96ca3bbfa9284cb128
author: Jean-Marc Valin <jmvalin@jmvalin.ca>
date: Mon Apr 24 21:19:08 EDT 2017

stream allocation

--- a/include/opusenc.h
+++ b/include/opusenc.h
@@ -35,8 +35,9 @@
 #include "opus.h"
 
 #define OPE_OK 0
-#define OPE_ERROR_CANNOT_OPEN -10
-#define OPE_ERROR_UNIMPLEMENTED -11
+#define OPE_CANNOT_OPEN -10
+#define OPE_UNIMPLEMENTED -11
+#define OPE_BAD_ARG -12
 
 typedef int (*ope_write_func)(void *user_data, const unsigned char *ptr, int len);
 
--- a/src/opusenc.c
+++ b/src/opusenc.c
@@ -31,8 +31,11 @@
 #include "config.h"
 #endif
 
+#include <time.h>
+#include <unistd.h>
 #include <stdlib.h>
 #include <stdio.h>
+#include <assert.h>
 #include <opus_multistream.h>
 #include "opusenc.h"
 #include "opus_header.h"
@@ -48,6 +51,10 @@
   float *buffer;
   OpusEncCallbacks callbacks;
   void *user_data;
+  int os_allocated;
+  ogg_stream_state os;
+  ogg_page og;
+  ogg_packet op;
 };
 
 int stdio_write(void *user_data, const unsigned char *ptr, int len) {
@@ -79,7 +86,7 @@
   }
   obj->file = fopen(path, "wb");
   if (!obj->file) {
-    if (error) *error = OPE_ERROR_CANNOT_OPEN;
+    if (error) *error = OPE_CANNOT_OPEN;
     /* FIXME: Destroy the encoder properly. */
     free(obj);
     return NULL;
@@ -95,7 +102,7 @@
   OpusHeader header;
   int ret;
   if (family != 0 && family != 1 && family != 255) {
-    if (error) *error = OPE_ERROR_UNIMPLEMENTED;
+    if (error) *error = OPE_UNIMPLEMENTED;
     return NULL;
   }
   if (channels <= 0 || channels > 255) {
@@ -112,6 +119,7 @@
     goto fail;
   }
   if ( (enc = malloc(sizeof(*enc))) == NULL) goto fail;
+  enc->os_allocated = 0;
   if ( (enc->buffer = malloc(sizeof(*enc->buffer)*BUFFER_SAMPLES*channels)) == NULL) goto fail;
   enc->st = st;
   enc->callbacks = *callbacks;
@@ -129,6 +137,19 @@
   return NULL;
 }
 
+static void init_stream(OggOpusEnc *enc) {
+  time_t start_time;
+  int serialno;
+  start_time = time(NULL);
+  srand(((getpid()&65535)<<15)^start_time);
+
+  serialno = rand();
+  if (ogg_stream_init(&enc->os, serialno) == -1) {
+    assert(0);
+    /* FIXME: How the hell do we handle that? */
+  }
+}
+
 /* Add/encode any number of float samples to the file. */
 int ope_write_float(OggOpusEnc *enc, float *pcm, int samples_per_channel) {
   (void)enc;
@@ -154,6 +175,7 @@
   finalize_stream(enc);
   free(enc->buffer);
   opus_multistream_encoder_destroy(enc->st);
+  if (enc->os_allocated) ogg_stream_clear(&enc->os);
   return OPE_OK;
 }