shithub: libopusenc

Download patch

ref: 05467e5cadf38168e9ddc402755597cf91e6c9b4
parent: f089e3f4207814731ed257fa31341524a896b2d3
author: Jean-Marc Valin <jmvalin@jmvalin.ca>
date: Thu Apr 27 11:34:41 EDT 2017

More comments code

--- a/examples/opusenc_example.c
+++ b/examples/opusenc_example.c
@@ -19,6 +19,8 @@
     printf("cannout open output file: %s\n", argv[2]);
     return 1;
   }
+  ope_add_comment(enc, "ARTIST", "Someone");
+  ope_add_comment(enc, "TITLE", "Some track");
   ope_close_and_free(enc);
   return 0;
 }
--- a/include/opusenc.h
+++ b/include/opusenc.h
@@ -38,13 +38,17 @@
 #define OPE_CANNOT_OPEN -10
 #define OPE_UNIMPLEMENTED -11
 #define OPE_BAD_ARG -12
+#define OPE_INTERNAL_ERROR -13
 
 typedef int (*ope_write_func)(void *user_data, const unsigned char *ptr, int len);
 
 typedef int (*ope_close_func)(void *user_data);
 
+/** Callback functions for accessing the stream. */
 typedef struct {
+  /** Callback for writing to the stream. */
   ope_write_func write;
+  /** Callback for closing the stream. */
   ope_close_func close;
 } OpusEncCallbacks;
 
--- a/src/opus_header.c
+++ b/src/opus_header.c
@@ -200,7 +200,7 @@
   *comments=p;
 }
 
-void comment_add(char **comments, int* length, char *tag, char *val)
+int comment_add(char **comments, int* length, char *tag, char *val)
 {
   char* p=*comments;
   int vendor_length=readint(p, 8);
@@ -210,7 +210,7 @@
   int len=(*length)+4+tag_len+val_len;
 
   p=(char*)realloc(p, len);
-  if (p == NULL) return;
+  if (p == NULL) return 1;
 
   writeint(p, *length, tag_len+val_len);      /* length of comment */
   if(tag){
@@ -221,6 +221,7 @@
   writeint(p, 8+4+vendor_length, user_comment_list_length+1);
   *comments=p;
   *length=len;
+  return 0;
 }
 
 void comment_pad(char **comments, int* length, int amount)
--- a/src/opus_header.h
+++ b/src/opus_header.h
@@ -50,7 +50,7 @@
 
 void comment_init(char **comments, int* length, const char *vendor_string);
 
-void comment_add(char **comments, int* length, char *tag, char *val);
+int comment_add(char **comments, int* length, char *tag, char *val);
 
 void comment_pad(char **comments, int* length, int amount);
 
--- a/src/opusenc.c
+++ b/src/opusenc.c
@@ -40,8 +40,13 @@
 #include "opusenc.h"
 #include "opus_header.h"
 
-#define BUFFER_SAMPLES 96000
+/* Allow up to 2 seconds for delayed decision. */
+#define MAX_LOOKAHEAD 96000
+/* We can't have a circular buffer (because of delayed decision), so let's not copy too often. */
+#define BUFFER_EXTRA 24000
 
+#define BUFFER_SAMPLES (MAX_LOOKAHEAD + BUFFER_EXTRA)
+
 static int oe_write_page(ogg_page *page, OpusEncCallbacks *cb, void *user_data)
 {
    int err;
@@ -59,6 +64,8 @@
 struct OggOpusEnc {
   OpusMSEncoder *st;
   float *buffer;
+  int buffer_start;
+  int buffer_end;
   OpusEncCallbacks callbacks;
   void *user_data;
   int os_allocated;
@@ -155,7 +162,6 @@
     char encoder_string[1024];
     snprintf(encoder_string, sizeof(encoder_string), "%s version %s", PACKAGE_NAME, PACKAGE_VERSION);
     comment_add(&enc->comment, &enc->comment_length, "ENCODER", encoder_string);
-    comment_pad(&enc->comment, &enc->comment_length, 512);
   }
   if (enc->comment == NULL) goto fail;
   if ( (enc->buffer = malloc(sizeof(*enc->buffer)*BUFFER_SAMPLES*channels)) == NULL) goto fail;
@@ -188,6 +194,7 @@
   }
   /* FIXME: Compute preskip. */
   enc->header.preskip = 0;
+  comment_pad(&enc->comment, &enc->comment_length, 512);
 
   /*Write header*/
   {
@@ -270,9 +277,7 @@
 
 /* Add a comment to the file (can only be called before encoding samples). */
 int ope_add_comment(OggOpusEnc *enc, char *tag, char *val) {
-  (void)enc;
-  (void)tag;
-  (void)val;
+  if (comment_add(&enc->comment, &enc->comment_length, tag, val)) return OPE_INTERNAL_ERROR;
   return OPE_OK;
 }
 
@@ -280,7 +285,7 @@
 int ope_set_vendor_string(OggOpusEnc *enc, char *vendor) {
   (void)enc;
   (void)vendor;
-  return OPE_OK;
+  return OPE_UNIMPLEMENTED;
 }
 
 /* Goes straight to the libopus ctl() functions. */