shithub: opus-tools

Download patch

ref: 1d778017e09bfbe3ad89f5d05a76e5b240caf61f
parent: ae7ded8f49d6c5be760d4e79c56b8e45f06eff0d
author: Jean-Marc Valin <jean-marc.valin@usherbrooke.ca>
date: Tue Aug 2 07:54:57 EDT 2011

Adds a version field and fixes pregap decoding

--- a/src/opus_header.c
+++ b/src/opus_header.c
@@ -1,5 +1,6 @@
 #include "opus_header.h"
 #include <string.h>
+#include <stdio.h>
 
 /* Header contents:
   - "OpusHead" (64 bits)
@@ -35,7 +36,7 @@
 
 static int write_uint16(Packet *p, opus_uint16 val)
 {
-   if (p->pos>p->maxlen-4)
+   if (p->pos>p->maxlen-2)
       return 0;
    p->data[p->pos  ] = (val>> 8) & 0xFF;
    p->data[p->pos+1] = (val    ) & 0xFF;
@@ -67,11 +68,11 @@
 
 static int read_uint16(ROPacket *p, opus_uint16 *val)
 {
-   if (p->pos>p->maxlen-4)
+   if (p->pos>p->maxlen-2)
       return 0;
    *val =  (opus_uint16)p->data[p->pos  ]<<8;
    *val |= (opus_uint16)p->data[p->pos+1];
-   p->pos += 4;
+   p->pos += 2;
    return 1;
 }
 
@@ -99,6 +100,9 @@
    read_chars(&p, (unsigned char*)str, 8);
    if (strcmp(str, "OpusHead")!=0)
       return 0;
+   if (!read_chars(&p, &ch, 1))
+      return 0;
+   h->version = ch;
    if (!read_uint32(&p, &h->sample_rate))
       return 0;
    if (!read_chars(&p, &ch, 1))
@@ -110,7 +114,6 @@
    if (!read_uint16(&p, &shortval))
       return 0;
    h->pregap = shortval;
-   
    return 1;
 }
 
@@ -123,6 +126,10 @@
    p.maxlen = len;
    p.pos = 0;
    if (!write_chars(&p, (const unsigned char*)"OpusHead", 8))
+      return 0;
+   /* Version is 0 */
+   ch = 0;
+   if (!write_chars(&p, &ch, 1))
       return 0;
    if (!write_uint32(&p, h->sample_rate))
       return 0;
--- a/src/opus_header.h
+++ b/src/opus_header.h
@@ -4,6 +4,7 @@
 #include "opus_types.h"
 
 typedef struct {
+   int version;
    opus_uint32 sample_rate;
    int multi_stream;
    int channels;
--- a/src/opusdec.c
+++ b/src/opusdec.c
@@ -290,8 +290,12 @@
    OpusDecoder *st;
    OpusHeader header;
 
-   opus_header_parse(op->packet, op->bytes, &header);
-
+   if (opus_header_parse(op->packet, op->bytes, &header)==0)
+   {
+      fprintf(stderr, "Cannot parse header\n");
+      return NULL;
+   }
+   
    if (header.channels>2 || header.channels<1)
    {
       fprintf (stderr, "Unsupported number of channels: %d\n", header.channels);
@@ -303,7 +307,6 @@
    if (!*rate)
       *rate = header.sample_rate;
    *pregap = header.pregap;
-
    st = opus_decoder_create(48000, header.channels);
    if (!st)
    {
@@ -595,12 +598,11 @@
                         out[i]=output[i];
                   }
                   {
-                     int frame_offset = 0;
-                     int new_frame_size = frame_size;
+                     int frame_offset, new_frame_size;
                      /*printf ("packet %d %d\n", packet_no, skip_samples);*/
                      /*fprintf (stderr, "packet %d %d %d\n", packet_no, skip_samples, lookahead);*/
 
-                     new_frame_size -= pregap;
+                     new_frame_size = frame_size - pregap;
                      frame_offset = pregap;
                      if (new_frame_size>0)
                      {