[xiph-commits] r9843 - trunk/ffmpeg2theora

j at svn.xiph.org j at svn.xiph.org
Wed Aug 24 13:41:52 PDT 2005


Author: j
Date: 2005-08-24 13:41:49 -0700 (Wed, 24 Aug 2005)
New Revision: 9843

Modified:
   trunk/ffmpeg2theora/README
   trunk/ffmpeg2theora/theorautils.c
   trunk/ffmpeg2theora/theorautils.h
Log:
new oggmuxer in theoraframes_flush,  thanks MikeS for fixing this.



Modified: trunk/ffmpeg2theora/README
===================================================================
--- trunk/ffmpeg2theora/README	2005-08-24 20:37:58 UTC (rev 9842)
+++ trunk/ffmpeg2theora/README	2005-08-24 20:41:49 UTC (rev 9843)
@@ -9,7 +9,7 @@
 
 usage right now:
  ./ffmpeg2theora clip.avi
- will produce clip.avi.ogg
+ will produce clip.ogg
  for more options run ffmpeg2theora -h
 
  there is a export plugin for kino (http://kino.schirmacher.de/)

Modified: trunk/ffmpeg2theora/theorautils.c
===================================================================
--- trunk/ffmpeg2theora/theorautils.c	2005-08-24 20:37:58 UTC (rev 9842)
+++ trunk/ffmpeg2theora/theorautils.c	2005-08-24 20:41:49 UTC (rev 9843)
@@ -44,9 +44,13 @@
 void init_info(theoraframes_info *info) {
     info->videotime =  0;
     info->audiotime = 0;
-    info->videoflag = 0;
-    info->audioflag = 0;
     info->debug = 0;
+    info->videopage_valid = 0;
+    info->audiopage_valid = 0;
+    info->audiopage_buffer_length = 0;
+    info->videopage_buffer_length = 0;
+    info->audiopage = NULL;
+    info->videopage = NULL;
 }
 
 
@@ -195,7 +199,6 @@
     while(theora_encode_packetout (&info->td, e_o_s, &info->op)) {
       ogg_stream_packetin (&info->to, &info->op);
     }
-    info->videoflag=1;
     return 0;
 }
     
@@ -236,13 +239,6 @@
         }
 
     }
-    info->audioflag=1;
-    /*
-    if (ogg_stream_eos (&info->vo)){
-        info->audioflag = 0;
-        return 0;
-    }
-    */
     return 0;
 }
 
@@ -268,64 +264,94 @@
 
 }
 
+static int write_audio_page(theoraframes_info *info)
+{
+  /* TODO: Check return values */
+  info->audio_bytesout +=
+    fwrite(info->audiopage, 1, info->audiopage_len, info->outfile);
+  info->audiopage_valid = 0;
 
-void theoraframes_flush (theoraframes_info *info, int e_o_s){
+  info->akbps = rint (info->audio_bytesout * 8. / info->audiotime * .001);
+  print_stats(info, info->audiotime);
+}
+
+static int write_video_page(theoraframes_info *info)
+{
+  /* TODO: Check return values */
+  info->video_bytesout +=
+    fwrite(info->videopage, 1, info->videopage_len, info->outfile);
+  info->videopage_valid = 0;
+
+  info->vkbps = rint (info->video_bytesout * 8. / info->videotime * .001);
+  print_stats(info, info->videotime);
+}
+
+void theoraframes_flush (theoraframes_info *info, int e_o_s)
+{
+    int len;
+
     /* flush out the ogg pages to info->outfile */
-    
-    int flushloop=1;
-    ogg_page videopage;
-    ogg_page audiopage;
+    while(1) {
+      /* Get pages for both streams, if not already present, and if available.*/
+      if(!info->audio_only && !info->videopage_valid) {
+        if(ogg_stream_pageout(&info->to, &info->og) > 0) {
+          len = info->og.header_len + info->og.body_len;
+          if(info->videopage_buffer_length < len) {
+            info->videopage = realloc(info->videopage, len);
+            info->videopage_buffer_length = len;
+          }
+          info->videopage_len = len;
+          memcpy(info->videopage, info->og.header, info->og.header_len);
+          memcpy(info->videopage+info->og.header_len , info->og.body, info->og.body_len);
 
-    while(flushloop){
-        int video = -1;
-        flushloop=0;
-        while(!info->audio_only && (e_o_s || 
-            ((info->videotime <= info->audiotime || info->video_only) && info->videoflag == 1))){
-                
-            info->videoflag = 0;
-            while(ogg_stream_pageout (&info->to, &videopage) > 0){
-                info->videotime =
-                    theora_granule_time (&info->td,ogg_page_granulepos(&videopage));
-                
-                /* flush a video page */
-                info->video_bytesout +=
-                    fwrite (videopage.header, 1,videopage.header_len, info->outfile);
-                info->video_bytesout +=
-                    fwrite (videopage.body, 1,videopage.body_len, info->outfile);
-                
-                info->vkbps = rint (info->video_bytesout * 8. / info->videotime * .001);
+          info->videopage_valid = 1;
+          info->videotime = theora_granule_time (&info->td,
+                  ogg_page_granulepos(&info->og));
+        }
+      }
+      if(!info->video_only && !info->audiopage_valid) {
+        if(ogg_stream_pageout(&info->vo, &info->og) > 0) {
+          len = info->og.header_len + info->og.body_len;
+          if(info->audiopage_buffer_length < len) {
+            info->audiopage = realloc(info->audiopage, len);
+            info->audiopage_buffer_length = len;
+          }
+          info->audiopage_len = len;
+          memcpy(info->audiopage, info->og.header, info->og.header_len);
+          memcpy(info->audiopage+info->og.header_len , info->og.body, info->og.body_len);
 
-                print_stats(info, info->videotime);
-                video=1;
-                info->videoflag = 1;
-                flushloop=1;
-            }
-            if(e_o_s)
-                break;
+          info->audiopage_valid = 1;
+          info->audiotime= vorbis_granule_time (&info->vd, 
+                  ogg_page_granulepos(&info->og));
         }
+      }
 
-        while (!info->video_only && (e_o_s || 
-            ((info->audiotime < info->videotime || info->audio_only) && info->audioflag==1))){
-            
-            info->audioflag = 0;
-            while(ogg_stream_pageout (&info->vo, &audiopage) > 0){    
-                /* flush an audio page */
-                info->audiotime=
-                    vorbis_granule_time (&info->vd,ogg_page_granulepos(&audiopage));
-                info->audio_bytesout +=
-                    fwrite (audiopage.header, 1,audiopage.header_len, info->outfile);
-                info->audio_bytesout +=
-                    fwrite (audiopage.body, 1,audiopage.body_len, info->outfile);
-
-                info->akbps = rint (info->audio_bytesout * 8. / info->audiotime * .001);
-                print_stats(info, info->audiotime);
-                video=0;
-                info->audioflag = 1;
-                flushloop=1;
-            }
-            if(e_o_s)
-                break;
-        }
+      if(info->video_only && info->videopage_valid) {
+        write_video_page(info);
+      }
+      else if(info->audio_only && info->audiopage_valid) {
+        write_audio_page(info);
+      }
+      /* We're using both. We can output only:
+       *  a) If we have valid pages for both
+       *  b) At EOS, for the remaining stream.
+       */
+      else if(info->videopage_valid && info->audiopage_valid) {
+        /* Make sure they're in the right order. */
+        if(info->videotime <= info->audiotime)
+          write_video_page(info);
+        else
+          write_audio_page(info);
+      } 
+      else if(e_o_s && info->videopage_valid) {
+          write_video_page(info);
+      }
+      else if(e_o_s && info->audiopage_valid) {
+          write_audio_page(info);
+      }
+      else {
+        break; /* Nothing more writable at the moment */
+      }
     }
 }
 
@@ -341,4 +367,9 @@
 
     if (info->outfile && info->outfile != stdout)
         fclose (info->outfile);
+
+    if(info->videopage)
+      free(info->videopage);
+    if(info->audiopage)
+      free(info->audiopage);
 }

Modified: trunk/ffmpeg2theora/theorautils.h
===================================================================
--- trunk/ffmpeg2theora/theorautils.h	2005-08-24 20:37:58 UTC (rev 9842)
+++ trunk/ffmpeg2theora/theorautils.h	2005-08-24 20:41:49 UTC (rev 9843)
@@ -21,6 +21,7 @@
 #include "theora/theora.h"
 #include "vorbis/codec.h"
 #include "vorbis/vorbisenc.h"
+#include "ogg/ogg.h"
 
 #define V2V_PRESET_PRO 1
 #define V2V_PRESET_PREVIEW 2
@@ -93,8 +94,6 @@
     double fps;
     double start;
     double end;
-    int audioflag;
-    int videoflag;
     double audiotime;
     double videotime;
     int vkbps;
@@ -121,6 +120,15 @@
     vorbis_dsp_state vd; /* central working state for the packet->PCM decoder */
     vorbis_block vb;     /* local working space for packet->PCM decode */
     FILE *outfile;
+
+    int audiopage_valid;
+    int videopage_valid;
+    unsigned char *audiopage;
+    unsigned char *videopage;
+    int videopage_len;
+    int audiopage_len;
+    int videopage_buffer_length;
+    int audiopage_buffer_length;
 }
 theoraframes_info;
 



More information about the commits mailing list