shithub: opus

Download patch

ref: 761811d91bc6fc18b28c364252ea2b232dc4f75a
parent: 8679a80ee2a4211b53600a84a7e2d275d3b9a79b
author: Jean-Marc Valin <[email protected]>
date: Sat Oct 18 05:11:05 EDT 2008

re-enable support for resizable buffers in the range coder

--- a/TODO
+++ b/TODO
@@ -11,3 +11,10 @@
 
 - Encode band shape (or just tilt)?
 - Make energy encoding more robust to losses?
+
+
+Misc:
+Detect uint decoding and flag them in the decoder directly
+If we attempt to write too many bits on the encoder side, set a flag instead of
+aborting
+Save "raw bytes" at the end of the stream
--- a/libcelt/entcode.h
+++ b/libcelt/entcode.h
@@ -26,6 +26,7 @@
   unsigned char *buf;
   unsigned char *ptr;
   long           storage;
+  int            resizable;
 };
 
 /*Encoding functions.*/
--- a/libcelt/entenc.c
+++ b/libcelt/entenc.c
@@ -12,11 +12,13 @@
 void ec_byte_writeinit_buffer(ec_byte_buffer *_b, unsigned char *_buf, long _size){
   _b->ptr=_b->buf=_buf;
   _b->storage=_size;
+  _b->resizable = 0;
 }
 
 void ec_byte_writeinit(ec_byte_buffer *_b){
   _b->ptr=_b->buf=celt_alloc(EC_BUFFER_INCREMENT*sizeof(char));
   _b->storage=EC_BUFFER_INCREMENT;
+  _b->resizable = 1;
 }
 
 void ec_byte_writetrunc(ec_byte_buffer *_b,long _bytes){
@@ -27,7 +29,13 @@
   ptrdiff_t endbyte;
   endbyte=_b->ptr-_b->buf;
   if(endbyte>=_b->storage){
-    celt_fatal("range encoder overflow\n");
+    if (_b->resizable){
+      _b->buf=celt_realloc(_b->buf,(_b->storage+EC_BUFFER_INCREMENT)*sizeof(char));
+      _b->storage+=EC_BUFFER_INCREMENT;
+      _b->ptr=_b->buf+endbyte;
+    } else {
+      celt_fatal("range encoder overflow\n");
+    }
   }
   *(_b->ptr++)=(unsigned char)_value;
 }
@@ -36,7 +44,13 @@
   ptrdiff_t endbyte;
   endbyte=_b->ptr-_b->buf;
   if(endbyte+4>_b->storage){
-    celt_fatal("range encoder overflow\n");
+    if (_b->resizable){
+      _b->buf=celt_realloc(_b->buf,(_b->storage+EC_BUFFER_INCREMENT)*sizeof(char));
+      _b->storage+=EC_BUFFER_INCREMENT;
+      _b->ptr=_b->buf+endbyte;
+    } else {
+      celt_fatal("range encoder overflow\n");
+    }
   }
   *(_b->ptr++)=(unsigned char)_value;
   _value>>=8;
@@ -51,7 +65,13 @@
   ptrdiff_t endbyte;
   endbyte=_b->ptr-_b->buf;
   if(endbyte+_bytes>_b->storage){
-    celt_fatal("range encoder overflow\n");
+    if (_b->resizable){
+      _b->storage=endbyte+_bytes+EC_BUFFER_INCREMENT;
+      _b->buf=celt_realloc(_b->buf,_b->storage*sizeof(char));
+      _b->ptr=_b->buf+endbyte;
+    } else {
+      celt_fatal("range encoder overflow\n");
+    }
   }
   memmove(_b->ptr,_source,_bytes);
   _b->ptr+=_bytes;