shithub: opus

Download patch

ref: 94d4ea930543f83483e158111b53389704b583b7
parent: a2fd116d16b6fbb8b9d21ac4f969edc8f6b6087e
author: Jean-Marc Valin <[email protected]>
date: Sun Jan 27 12:34:35 EST 2008

Bit of cleaning up in the byte dumping part. Making use of any remaining bit(s)
to do error detection in the decoder.

--- a/libcelt/bands.c
+++ b/libcelt/bands.c
@@ -240,6 +240,7 @@
    
    for (i=0;i<m->nbEBands;i++)
       offsets[i] = 0;
+   /* Use a single-bit margin to guard against overrunning (make sure it's enough) */
    bits = total_bits - ec_enc_tell(enc, 0) - 1;
    compute_allocation(alloc, offsets, bits, pulses);
    
@@ -291,6 +292,7 @@
    
    for (i=0;i<m->nbEBands;i++)
       offsets[i] = 0;
+   /* Use a single-bit margin to guard against overrunning (make sure it's enough) */
    bits = total_bits - ec_dec_tell(dec, 0) - 1;
    compute_allocation(alloc, offsets, bits, pulses);
 
--- a/libcelt/celt.c
+++ b/libcelt/celt.c
@@ -379,16 +379,26 @@
       }
    }
    
-   /* Not sure why, but filling the rest with zeros tends to help */
-   while (ec_enc_tell(&st->enc, 0) < nbCompressedBytes*8)
-      ec_enc_uint(&st->enc, 0, 2);
+   //printf ("%d\n", ec_enc_tell(&st->enc, 0)-8*nbCompressedBytes);
+   /* Finishing the stream with a 0101... pattern so that the decoder can check is everything's right */
+   {
+      int val = 0;
+      while (ec_enc_tell(&st->enc, 0) < nbCompressedBytes*8)
+      {
+         ec_enc_uint(&st->enc, val, 2);
+         val = 1-val;
+      }
+   }
    ec_enc_done(&st->enc);
    {
       unsigned char *data;
       int nbBytes = ec_byte_bytes(&st->buf);
-      if (nbBytes > nbCompressedBytes)
+      if (nbBytes != nbCompressedBytes)
       {
-         celt_warning("got too many bytes");
+         if (nbBytes > nbCompressedBytes)
+            celt_warning("got too many bytes");
+         else
+            celt_warning("not enough bytes");
          return CELT_INTERNAL_ERROR;
       }
       //printf ("%d\n", *nbBytes);
@@ -395,12 +405,6 @@
       data = ec_byte_get_buffer(&st->buf);
       for (i=0;i<nbBytes;i++)
          compressed[i] = data[i];
-      
-      /* Fill the last byte with the right pattern so the decoder doesn't get confused
-         if the encoder didn't return enough bytes */
-      /* FIXME: This isn't quite what the decoder expects, but it's the best we can do for now */
-      for (;i<nbCompressedBytes;i++)
-         compressed[i] = 0x00;
    }
    /* Reset the packing for the next encoding */
    ec_byte_reset(&st->buf);
@@ -409,22 +413,7 @@
    return nbCompressedBytes;
 }
 
-char *celt_encoder_get_bytes(CELTEncoder *st, int *nbBytes)
-{
-   char *data;
-   ec_enc_done(&st->enc);
-   *nbBytes = ec_byte_bytes(&st->buf);
-   data = ec_byte_get_buffer(&st->buf);
-   //printf ("%d\n", *nbBytes);
-   
-   /* Reset the packing for the next encoding */
-   ec_byte_reset(&st->buf);
-   ec_enc_init(&st->enc,&st->buf);
 
-   return data;
-}
-
-
 /****************************************************************************/
 /*                                                                          */
 /*                                DECODER                                   */
@@ -637,6 +626,19 @@
             if (tmp < -32767) tmp = -32767;
             pcm[C*i*N+C*j+c] = (short)floor(.5+tmp);
          }
+      }
+   }
+
+   {
+      int val = 0;
+      while (ec_dec_tell(&dec, 0) < len*8)
+      {
+         if (ec_dec_uint(&dec, 2) != val)
+         {
+            celt_warning("decode error");
+            return CELT_CORRUPTED_DATA;
+         }
+         val = 1-val;
       }
    }
 
--- a/libcelt/celt.h
+++ b/libcelt/celt.h
@@ -36,10 +36,11 @@
 extern "C" {
 #endif
 
-#define CELT_BAD_ARG -1
-#define CELT_INVALID_MODE -2
-#define CELT_INTERNAL_ERROR -3
-#define CELT_CORRUPTED_DATA -4
+#define CELT_OK                0
+#define CELT_BAD_ARG          -1
+#define CELT_INVALID_MODE     -2
+#define CELT_INTERNAL_ERROR   -3
+#define CELT_CORRUPTED_DATA   -4
 
 
 typedef struct CELTEncoder CELTEncoder;