Credit Andrew - fix vorbis audio which was scratchy and ensure aging plugin does...
[goodguy/cinelerra.git] / cinelerra-5.1 / cinelerra / fileogg.C
index 7302bcd1bec7dba755df952aa3eb07781f26b17f..bac03b1eafa3688093fc5bd6920db0d596e5d1a9 100644 (file)
@@ -1,6 +1,7 @@
 /*
  * CINELERRA
  * Copyright (C) 2008 Adam Williams <broadcast at earthling dot net>
+ * Copyright (C) 2003-2016 Cinelerra CV contributors
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -17,6 +18,7 @@
  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  *
  */
+#ifdef HAVE_OGG
 
 #include "asset.h"
 #include "bcsignals.h"
@@ -1027,6 +1029,7 @@ int FileOGG::ogg_get_page_of_sample(ogg_page *og, int64_t sample)
 int FileOGG::ogg_seek_to_sample(int64_t ogg_sample)
 {
        ogg_page og;
+       ogg_packet op;
        if( !ogg_get_page_of_sample(&og, ogg_sample) ) {
                eprintf(_("Seeking to sample's page failed\n"));
                return 0;
@@ -1038,13 +1041,13 @@ int FileOGG::ogg_seek_to_sample(int64_t ogg_sample)
                while( (ret=audiosync->ogg_get_prev_page(to.serialno, &og)) &&
                        (ogg_page_packets(&og) == 0 && ogg_page_continued(&og)) );
        }
-       audio_eos = 0;
-       ogg_stream_reset(&vo);
-       ogg_stream_pagein(&vo, &og);
-       vorbis_synthesis_restart(&vd);
-       ogg_packet op;
-       while( (ret=ogg_get_audio_packet(&op)) != 0 &&
-               op.granulepos < 0 );
+       if( ret ) {
+               audio_eos = 0;
+               ogg_stream_reset(&vo);
+               ogg_stream_pagein(&vo, &og);
+               vorbis_synthesis_restart(&vd);
+               ret = ogg_get_audio_packet(&op);
+       }
        if( ret && !vorbis_synthesis(&vb, &op) ) {
                vorbis_synthesis_blockin(&vd, &vb);
                if( vorbis_synthesis_pcmout(&vd, 0) )
@@ -1055,7 +1058,7 @@ int FileOGG::ogg_seek_to_sample(int64_t ogg_sample)
                return 0;
        }
 
-       while( ogg_sample >= next_pos ) {
+       while( ogg_sample > next_pos ) {
                if( !(ret=ogg_get_audio_packet(&op)) ) break;
                if( vorbis_synthesis(&vb, &op) ) continue;
                vorbis_synthesis_blockin(&vd, &vb);
@@ -1270,13 +1273,9 @@ int FileOGG::read_frame(VFrame *frame)
                }
                expect_keyframe = 1;
        }
-       int frames_remaining = asset->video_length - (video_pos - start_frame);
-       if( decode_frames > frames_remaining ) decode_frames = frames_remaining;
        int ret = 0;
        ogg_packet op;
-       while( decode_frames > 0 && !video_eos ) {
-               if( video_pos-start_frame >= asset->video_length )
-                       return 0;
+       while( decode_frames > 0 ) {
                if( !ogg_get_video_packet(&op) ) break;
                if( expect_keyframe ) {
                        expect_keyframe = 0;
@@ -1343,7 +1342,6 @@ int FileOGG::ogg_decode_more_samples()
                }
        }
        ogg_sample_position = -11;
-       if( audio_eos ) return 0;
        eprintf(_("Cannot find next page while trying to decode more samples\n"));
        return 0;
 }
@@ -1446,22 +1444,28 @@ int FileOGG::read_samples(double *buffer, int64_t len)
                        int64_t sample_demand = samples_to_read - samples_read;
                        int64_t sample_count = MIN(samples_avail, sample_demand);
                        if( sample_count > 0 ) {
-                               for( int i=0; i<asset->channels; ++i ) {
-                                       float *input = vorbis_buffer[i];
-                                       float *output = pcm_history[i] + hole_start;
-                                       int sz = sample_count*sizeof(*output);
-                                       if( samples_waiting )
+                               int sz = sample_count*sizeof(float);
+                               if( samples_waiting ) {
+                                       for( int i=0; i<asset->channels; ++i ) {
+                                               float *input = vorbis_buffer[i];
+                                               float *output = pcm_history[i] + hole_start;
                                                memcpy(output, input, sz);
-                                       else
+                                       }
+                                       vorbis_synthesis_read(&vd, sample_count);
+                               }
+                               else {
+                                       for( int i=0; i<asset->channels; ++i ) {
+                                               float *output = pcm_history[i] + hole_start;
                                                memset(output, 0, sz);
+                                       }
                                }
-                               vorbis_synthesis_read(&vd, sample_count);
-                               samples_read += sample_count;
                                ogg_sample_position += sample_count;
                                hole_start += sample_count;
+                               samples_read += sample_count;
+                               if( samples_read >= hole_len ) break;
                        }
-
-                       if( !ogg_decode_more_samples() ) break;
+                       if( samples_read < hole_len && !ogg_decode_more_samples() )
+                               break;
                }
        }
 
@@ -1631,6 +1635,8 @@ OGGConfigAudio::OGGConfigAudio(BC_WindowBase *parent_window, Asset *asset)
 {
        this->parent_window = parent_window;
        this->asset = asset;
+// *** CONTEXT_HELP ***
+       context_help_set_keyword("Single File Rendering");
 }
 
 OGGConfigAudio::~OGGConfigAudio()
@@ -1759,6 +1765,8 @@ OGGConfigVideo::OGGConfigVideo(BC_WindowBase *parent_window, Asset *asset)
 {
        this->parent_window = parent_window;
        this->asset = asset;
+// *** CONTEXT_HELP ***
+       context_help_set_keyword("Single File Rendering");
 }
 
 OGGConfigVideo::~OGGConfigVideo()
@@ -1908,3 +1916,4 @@ int OGGTheoraSharpness::handle_event()
 }
 
 
+#endif