shithub: opus-tools

Download patch

ref: ae7ded8f49d6c5be760d4e79c56b8e45f06eff0d
parent: 3161489199534e1a3a3bacc699f47d766a736c84
author: Jean-Marc Valin <jean-marc.valin@usherbrooke.ca>
date: Tue Aug 2 07:18:01 EDT 2011

Pregap handling in opusenc, proper ending of audio read in opusenc

--- a/src/opusdec.c
+++ b/src/opusdec.c
@@ -285,7 +285,7 @@
    printf ("Copyright (C) 2008-2011 Jean-Marc Valin\n");
 }
 
-static OpusDecoder *process_header(ogg_packet *op, opus_int32 *rate, int *channels, int quiet)
+static OpusDecoder *process_header(ogg_packet *op, opus_int32 *rate, int *channels, int *pregap, int quiet)
 {
    OpusDecoder *st;
    OpusHeader header;
@@ -302,6 +302,7 @@
 
    if (!*rate)
       *rate = header.sample_rate;
+   *pregap = header.pregap;
 
    st = opus_decoder_create(48000, header.channels);
    if (!st)
@@ -375,7 +376,6 @@
    ogg_packet     op;
    ogg_stream_state os;
    int enh_enabled;
-   int nframes=2;
    int print_bitrate=0;
    int close_in=0;
    int eos=0;
@@ -384,9 +384,8 @@
    int channels=-1;
    int rate=0;
    int wav_format=0;
-   int lookahead=0;
+   int pregap=0;
    int opus_serialno = -1;
-   int firstpacket = 1;
    SpeexResamplerState *resampler=NULL;
 
    enh_enabled = 1;
@@ -537,11 +536,9 @@
             /*If first packet, process as OPUS header*/
             if (packet_count==0)
             {
-               st = process_header(&op, &rate, &channels, quiet);
+               st = process_header(&op, &rate, &channels, &pregap, quiet);
                if (!st)
                   exit(1);
-               if (!nframes)
-                  nframes=1;
                if (rate != 48000)
                {
                   int err;
@@ -602,17 +599,16 @@
                      int new_frame_size = frame_size;
                      /*printf ("packet %d %d\n", packet_no, skip_samples);*/
                      /*fprintf (stderr, "packet %d %d %d\n", packet_no, skip_samples, lookahead);*/
-                     if (firstpacket == 1)
-                     {
-                        /*printf ("chopping first packet\n");*/
-                        new_frame_size -= lookahead;
-                        frame_offset = lookahead;
-                        firstpacket = 0;
-                     }
+
+                     new_frame_size -= pregap;
+                     frame_offset = pregap;
                      if (new_frame_size>0)
                      {
                         audio_write(out+frame_offset*channels, channels, new_frame_size, fout, resampler);
                         audio_size+=sizeof(short)*new_frame_size*channels;
+                        pregap = 0;
+                     } else {
+                        pregap -= frame_size;
                      }
                   }
                }
--- a/src/opusenc.c
+++ b/src/opusenc.c
@@ -159,12 +159,16 @@
          reading = 1024-channels*inbuf;
          ret = read_samples_pcm(fin, reading, bits, channels, lsb, pcmbuf+inbuf*channels, buff, size);
          /* FIXME: We should drain the buffer before stopping */
-         if (ret==0)
-            return 0;
          inbuf += ret;
          in_len = inbuf;
          out_len = frame_size-out_samples;
          speex_resampler_process_interleaved_int(resampler, pcmbuf, &in_len, input+out_samples*channels, &out_len);
+         if (ret==0 && in_len==0)
+         {
+            for (i=out_samples*channels;i<frame_size*channels;i++)
+               input[i] = 0;
+            return out_samples;
+         }
          out_samples += out_len;
          for (i=0;i<channels*(inbuf-in_len);i++)
             pcmbuf[i] = pcmbuf[i+channels*in_len];