]> git.sesse.net Git - vlc/commitdiff
* modules/codec/theora.c, configure.ac.in, modules/codec/Modules.am: new
authorGildas Bazin <gbazin@videolan.org>
Wed, 20 Nov 2002 14:09:57 +0000 (14:09 +0000)
committerGildas Bazin <gbazin@videolan.org>
Wed, 20 Nov 2002 14:09:57 +0000 (14:09 +0000)
   theora codec plugin.
   This plugin is based on the experimental theora codec from the Xiph.Org
   foundation (http://www.theora.org/). Because the theora bitstream
   specifications are likely to change in the near future, this plugin won't
   be compiled by default. If you want to test/develop theora with vlc, you'll
   need to configure it with --enable-theora and also make sure you've got a
   fresh libogg from cvs.

* modules/demux/ogg.c: modified to handle theora and tarkin bitstreams.

* modules/codec/tarkin.c: fixed a few typos.

configure.ac.in
modules/codec/Modules.am
modules/codec/tarkin.c
modules/codec/theora.c [new file with mode: 0644]
modules/demux/ogg.c

index 0310b8465efcd284a391efa148de8e138c596f97..fa2d75fa43cc426ac3d84310e9a31501b89c27df 100644 (file)
@@ -1109,8 +1109,11 @@ AC_ARG_ENABLE(ogg,
 if test "x${enable_ogg}" != "xno"
 then
   AC_CHECK_HEADERS(ogg/ogg.h, [
-    PLUGINS="${PLUGINS} ogg"
-    LDFLAGS_ogg="${LDFLAGS_ogg} -logg"
+    AC_CHECK_LIB( ogg, oggpack_read, [
+      PLUGINS="${PLUGINS} ogg"
+      LDFLAGS_ogg="${LDFLAGS_ogg} -logg"
+      AC_CHECK_LIB( ogg, oggpackB_read, [
+        CPPFLAGS_ogg="${CPPFLAGS_ogg} -DHAVE_OGGPACKB"])])
    ],[])
 fi
 
@@ -1497,6 +1500,7 @@ then
       BUILTINS="${BUILTINS} tarkin"
       CPPFLAGS_tarkin="${CPPFLAGS_tarkin} -I${real_tarkin_tree}"
       LDFLAGS_tarkin="${LDFLAGS_tarkin} ${real_tarkin_tree}/mem.o ${real_tarkin_tree}/pnm.o ${real_tarkin_tree}/wavelet.o ${real_tarkin_tree}/wavelet_xform.o ${real_tarkin_tree}/wavelet_coeff.o ${real_tarkin_tree}/yuv.o ${real_tarkin_tree}/tarkin.o ${real_tarkin_tree}/info.o -logg"
+      AC_MSG_RESULT(yes)
     else
       dnl  The given tarkin tree wasn't built
       AC_MSG_RESULT(no)
@@ -1506,6 +1510,23 @@ then
   fi
 fi
 
+dnl
+dnl  theora decoder plugin
+dnl
+AC_ARG_ENABLE(theora,
+[  --enable-theora         experimental theora codec (default disabled)])
+if test "x${enable_theora}" = "xyes"
+then
+  AC_CHECK_HEADERS(theora/theora.h, [
+    AC_CHECK_LIB(theora, theora_granule_time, [
+      BUILTINS="${BUILTINS} theora"
+      LDFLAGS_theora="${LDFLAGS_theora} -logg -ltheora" ],[
+      AC_MSG_ERROR([libtheora doesn't appear to be installed on you system.
+You also need to check that you have a libogg posterior to the 1.0 release.])],
+      [-logg])
+  ])
+fi
+
 dnl
 dnl  Video plugins
 dnl
index 084aa311bccdf5d150b73dd60f0dc7df52c892fc..e07e946e4e05f7e28e4c4046c64cb556c747341f 100644 (file)
@@ -3,5 +3,6 @@ SOURCES_lpcm = modules/codec/lpcm.c
 SOURCES_araw = modules/codec/araw.c
 SOURCES_vorbis = modules/codec/vorbis.c
 SOURCES_tarkin = modules/codec/tarkin.c
+SOURCES_theora = modules/codec/theora.c
 SOURCES_dv = modules/codec/dv.c
 SOURCES_xvid = modules/codec/xvid.c
index 373439989a8aa7f5550dfa6fe31cdd81884fe4f0..3ddcdf1afb842c7b090ea04f767cb3583dd6a8ee 100644 (file)
@@ -2,7 +2,7 @@
  * tarkin.c: tarkin decoder module making use of libtarkin.
  *****************************************************************************
  * Copyright (C) 1999-2001 VideoLAN
- * $Id: tarkin.c,v 1.1 2002/11/18 13:28:09 gbazin Exp $
+ * $Id: tarkin.c,v 1.2 2002/11/20 14:09:57 gbazin Exp $
  *
  * Authors: Gildas Bazin <gbazin@netcourrier.com>
  *
@@ -45,7 +45,7 @@
 #include <tarkin.h>
 
 /*****************************************************************************
- * dec_thread_t : vorbis decoder thread descriptor
+ * dec_thread_t : tarkin decoder thread descriptor
  *****************************************************************************/
 typedef struct dec_thread_t
 {
@@ -59,10 +59,8 @@ typedef struct dec_thread_t
      */
     TarkinStream *tarkin_stream;
 
-    TarkinInfo       ti; /* struct that stores all the static tarkin bitstream
-                            settings */
-    TarkinComment    tc; /* struct that stores all the bitstream user
-                          * comments */
+    TarkinInfo       ti;                        /* tarkin bitstream settings */
+    TarkinComment    tc;                   /* tarkin bitstream user comments */
     TarkinTime           tarkdate;
 
     /*
@@ -97,6 +95,7 @@ vlc_module_begin();
     set_description( _("Tarkin decoder module") );
     set_capability( "decoder", 100 );
     set_callbacks( OpenDecoder, NULL );
+    add_shortcut( "tarkin" );
 vlc_module_end();
 
 /*****************************************************************************
@@ -149,7 +148,7 @@ static int RunDecoder( decoder_fifo_t *p_fifo )
     if( tarkin_synthesis_headerin( &p_dec->ti, &p_dec->tc, &oggpacket ) < 0 )
     {
         msg_Err( p_dec->p_fifo, "This bitstream does not contain Tarkin "
-                 "vido data");
+                 "video data");
         goto error;
     }
 
@@ -177,13 +176,13 @@ static int RunDecoder( decoder_fifo_t *p_fifo )
     /* Initialize the tarkin decoder */
     tarkin_synthesis_init( p_dec->tarkin_stream, &p_dec->ti );
 
-    /* vorbis decoder thread's main loop */
+    /* tarkin decoder thread's main loop */
     while( (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )
     {
         DecodePacket( p_dec );
     }
 
-    /* If b_error is set, the vorbis decoder thread enters the error loop */
+    /* If b_error is set, the tarkin decoder thread enters the error loop */
     if( p_dec->p_fifo->b_error )
     {
         DecoderError( p_dec->p_fifo );
@@ -229,29 +228,29 @@ static void DecodePacket( dec_thread_t *p_dec )
     tarkin_synthesis_packetin( p_dec->tarkin_stream, &oggpacket );
 
     while( tarkin_synthesis_frameout( p_dec->tarkin_stream,
-                                     &rgb, 0, &p_dec->tarkdate ) == 0 )
+                                      &rgb, 0, &p_dec->tarkdate ) == 0 )
     {
 
-       i_width = p_dec->tarkin_stream->layer->desc.width;
-       i_height = p_dec->tarkin_stream->layer->desc.height;
-       switch( p_dec->tarkin_stream->layer->desc.format )
-       {
-       case TARKIN_RGB24:
+        i_width = p_dec->tarkin_stream->layer->desc.width;
+        i_height = p_dec->tarkin_stream->layer->desc.height;
+        switch( p_dec->tarkin_stream->layer->desc.format )
+        {
+        case TARKIN_RGB24:
             i_chroma = VLC_FOURCC('R','V','2','4');
-           break;
-       case TARKIN_RGB32:
+            break;
+        case TARKIN_RGB32:
             i_chroma = VLC_FOURCC('R','V','3','2');
-           break;
-       case TARKIN_RGBA:
+            break;
+        case TARKIN_RGBA:
             i_chroma = VLC_FOURCC('R','G','B','A');
-           break;
-       default:
+            break;
+        default:
             i_chroma = VLC_FOURCC('Y','V','1','2');
-           break;
-       }
+            break;
+        }
         i_aspect = VOUT_ASPECT_FACTOR * i_width / i_height;
         p_dec->p_vout = tarkin_SpawnVout( p_dec, i_width, i_height,
-                                         i_aspect, i_chroma );
+                                          i_aspect, i_chroma );
 
         /* Get a new picture */
         while( !(p_pic = vout_CreatePicture( p_dec->p_vout, 0, 0, 0 ) ) )
@@ -314,13 +313,13 @@ static void CloseDecoder( dec_thread_t * p_dec )
             input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_dec->p_pes );
 
         if( p_dec->p_vout )
-       {
+        {
             vlc_object_detach( p_dec->p_vout );
             vout_DestroyThread( p_dec->p_vout );
-       }
+        }
 
-       if( p_dec->tarkin_stream )
-           tarkin_stream_destroy( p_dec->tarkin_stream );
+        if( p_dec->tarkin_stream )
+            tarkin_stream_destroy( p_dec->tarkin_stream );
 
         free( p_dec );
     }
@@ -392,22 +391,22 @@ static vout_thread_t *tarkin_SpawnVout( dec_thread_t *p_dec,
  *                     picture_t structure.
  *****************************************************************************/
 static void tarkin_CopyPicture( dec_thread_t *p_dec, picture_t *p_pic,
-                               uint8_t *p_src )
+                                uint8_t *p_src )
 {
     int i_plane, i_line, i_width, i_dst_stride;
     u8  *p_dst;
 
     for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
     {
-       p_dst = p_pic->p[i_plane].p_pixels;
-       i_width = p_pic->p[i_plane].i_visible_pitch;
-       i_dst_stride = p_pic->p[i_plane].i_pitch;
-
-       for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
-       {
-           p_dec->p_fifo->p_vlc->pf_memcpy( p_dst, p_src, i_width );
-           p_src += i_width;
-           p_dst += i_dst_stride;
-       }
+        p_dst = p_pic->p[i_plane].p_pixels;
+        i_width = p_pic->p[i_plane].i_visible_pitch;
+        i_dst_stride = p_pic->p[i_plane].i_pitch;
+
+        for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
+        {
+            p_dec->p_fifo->p_vlc->pf_memcpy( p_dst, p_src, i_width );
+            p_src += i_width;
+            p_dst += i_dst_stride;
+        }
     }
 }
diff --git a/modules/codec/theora.c b/modules/codec/theora.c
new file mode 100644 (file)
index 0000000..e8f3172
--- /dev/null
@@ -0,0 +1,362 @@
+/*****************************************************************************
+ * theora.c: theora decoder module making use of libtheora.
+ *****************************************************************************
+ * Copyright (C) 1999-2001 VideoLAN
+ * $Id: theora.c,v 1.1 2002/11/20 14:09:57 gbazin Exp $
+ *
+ * Authors: Gildas Bazin <gbazin@netcourrier.com>
+ *
+ * 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
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ * 
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
+ *****************************************************************************/
+
+/*****************************************************************************
+ * Preamble
+ *****************************************************************************/
+#include <vlc/vlc.h>
+#include <vlc/vout.h>
+#include <vlc/input.h>
+#include <vlc/decoder.h>
+
+#include <stdlib.h>                                      /* malloc(), free() */
+#include <string.h>                                    /* memcpy(), memset() */
+
+#include <ogg/ogg.h>
+
+#include <theora/theora.h>
+
+/*****************************************************************************
+ * dec_thread_t : theora decoder thread descriptor
+ *****************************************************************************/
+typedef struct dec_thread_t
+{
+    /*
+     * Thread properties
+     */
+    vlc_thread_t        thread_id;                /* id for thread functions */
+
+    /*
+     * Theora properties
+     */
+    theora_info      ti;                        /* theora bitstream settings */
+    theora_state     td;                   /* theora bitstream user comments */
+
+    /*
+     * Input properties
+     */
+    decoder_fifo_t         *p_fifo;            /* stores the PES stream data */
+    pes_packet_t           *p_pes;            /* current PES we are decoding */
+
+    /*
+     * Output properties
+     */
+    vout_thread_t *p_vout;
+
+} dec_thread_t;
+
+/*****************************************************************************
+ * Local prototypes
+ *****************************************************************************/
+static int  OpenDecoder  ( vlc_object_t * );
+static int  RunDecoder   ( decoder_fifo_t * );
+static void CloseDecoder ( dec_thread_t * );
+
+static void DecodePacket ( dec_thread_t * );
+static int  GetOggPacket ( dec_thread_t *, ogg_packet *, mtime_t * );
+
+static void theora_CopyPicture( dec_thread_t *, picture_t *, yuv_buffer * );
+static vout_thread_t *theora_SpawnVout( dec_thread_t *, int, int, int, int );
+/*****************************************************************************
+ * Module descriptor
+ *****************************************************************************/
+vlc_module_begin();
+    set_description( _("Theora decoder module") );
+    set_capability( "decoder", 100 );
+    set_callbacks( OpenDecoder, NULL );
+    add_shortcut( "theora" );
+vlc_module_end();
+
+/*****************************************************************************
+ * OpenDecoder: probe the decoder and return score
+ *****************************************************************************/
+static int OpenDecoder( vlc_object_t *p_this )
+{
+    decoder_fifo_t *p_fifo = (decoder_fifo_t*) p_this;
+
+    if( p_fifo->i_fourcc != VLC_FOURCC('t','h','e','o') )
+    {
+        return VLC_EGENERIC;
+    }
+
+    p_fifo->pf_run = RunDecoder;
+    return VLC_SUCCESS;
+}
+/*****************************************************************************
+ * RunDecoder: the theora decoder
+ *****************************************************************************/
+static int RunDecoder( decoder_fifo_t *p_fifo )
+{
+    dec_thread_t *p_dec;
+    ogg_packet oggpacket;
+    int i_chroma, i_aspect;
+    mtime_t i_pts;
+
+    /* Allocate the memory needed to store the thread's structure */
+    if( (p_dec = (dec_thread_t *)malloc (sizeof(dec_thread_t)) )
+            == NULL)
+    {
+        msg_Err( p_fifo, "out of memory" );
+        goto error;
+    }
+
+    /* Initialize the thread properties */
+    memset( p_dec, 0, sizeof(dec_thread_t) );
+    p_dec->p_fifo = p_fifo;
+    p_dec->p_pes  = NULL;
+
+    /* Take care of the initial Theora header */
+    if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
+        goto error;
+
+    oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
+    if( theora_decode_header( &p_dec->ti, &oggpacket ) < 0 )
+    {
+        msg_Err( p_dec->p_fifo, "This bitstream does not contain Theora "
+                 "video data");
+        goto error;
+    }
+
+    /* Initialize decoder */
+    theora_decode_init( &p_dec->td, &p_dec->ti );
+    msg_Dbg( p_dec->p_fifo, "%dx%d %.02f fps video",
+             p_dec->ti.width, p_dec->ti.height,
+             (double)p_dec->ti.fps_numerator/p_dec->ti.fps_denominator);
+
+    /* Initialize video output */
+    if( p_dec->ti.aspect_denominator )
+        i_aspect = VOUT_ASPECT_FACTOR * p_dec->ti.aspect_numerator /
+                    p_dec->ti.aspect_denominator;
+    else
+        i_aspect = VOUT_ASPECT_FACTOR * p_dec->ti.width / p_dec->ti.height;
+
+    i_chroma = VLC_FOURCC('Y','V','1','2');
+
+    p_dec->p_vout = theora_SpawnVout( p_dec, p_dec->ti.width, p_dec->ti.height,
+                                      i_aspect, i_chroma );
+
+    /* theora decoder thread's main loop */
+    while( (!p_dec->p_fifo->b_die) && (!p_dec->p_fifo->b_error) )
+    {
+        DecodePacket( p_dec );
+    }
+
+    /* If b_error is set, the theora decoder thread enters the error loop */
+    if( p_dec->p_fifo->b_error )
+    {
+        DecoderError( p_dec->p_fifo );
+    }
+
+    /* End of the theora decoder thread */
+    CloseDecoder( p_dec );
+
+    return 0;
+
+ error:
+    DecoderError( p_fifo );
+    if( p_dec )
+    {
+        if( p_dec->p_fifo )
+            p_dec->p_fifo->b_error = 1;
+
+        /* End of the theora decoder thread */
+        CloseDecoder( p_dec );
+    }
+
+    return -1;
+}
+
+/*****************************************************************************
+ * DecodePacket: decodes a Theora packet.
+ *****************************************************************************/
+static void DecodePacket( dec_thread_t *p_dec )
+{
+    ogg_packet oggpacket;
+    picture_t *p_pic;
+    mtime_t i_pts;
+    yuv_buffer yuv;
+
+    if( GetOggPacket( p_dec, &oggpacket, &i_pts ) != VLC_SUCCESS )
+    {
+        /* This should mean an eos */
+        return;
+    }
+
+    theora_decode_packetin( &p_dec->td, &oggpacket );
+
+    /* Decode */
+    theora_decode_YUVout( &p_dec->td, &yuv );
+
+    /* Get a new picture */
+    while( !(p_pic = vout_CreatePicture( p_dec->p_vout, 0, 0, 0 ) ) )
+    {
+        if( p_dec->p_fifo->b_die || p_dec->p_fifo->b_error )
+        {
+            return;
+        }
+        msleep( VOUT_OUTMEM_SLEEP );
+    }
+    if( !p_pic )
+        return;
+
+    theora_CopyPicture( p_dec, p_pic, &yuv );
+
+    vout_DatePicture( p_dec->p_vout, p_pic, i_pts );
+    vout_DisplayPicture( p_dec->p_vout, p_pic );
+}
+
+/*****************************************************************************
+ * GetOggPacket: get the following theora packet from the stream and send back
+ *               the result in an ogg packet (for easy decoding by libtheora).
+ *****************************************************************************
+ * Returns VLC_EGENERIC in case of eof.
+ *****************************************************************************/
+static int GetOggPacket( dec_thread_t *p_dec, ogg_packet *p_oggpacket,
+                         mtime_t *p_pts )
+{
+    if( p_dec->p_pes ) input_DeletePES( p_dec->p_fifo->p_packets_mgt,
+                                        p_dec->p_pes );
+
+    input_ExtractPES( p_dec->p_fifo, &p_dec->p_pes );
+    if( !p_dec->p_pes ) return VLC_EGENERIC;
+
+    p_oggpacket->packet = p_dec->p_pes->p_first->p_payload_start;
+    p_oggpacket->bytes = p_dec->p_pes->i_pes_size;
+    p_oggpacket->granulepos = p_dec->p_pes->i_dts;
+    p_oggpacket->b_o_s = 0;
+    p_oggpacket->e_o_s = 0;
+    p_oggpacket->packetno = 0;
+
+    *p_pts = p_dec->p_pes->i_pts;
+
+    return VLC_SUCCESS;
+}
+
+/*****************************************************************************
+ * CloseDecoder: theora decoder destruction
+ *****************************************************************************/
+static void CloseDecoder( dec_thread_t * p_dec )
+{
+
+    if( p_dec )
+    {
+        if( p_dec->p_pes )
+            input_DeletePES( p_dec->p_fifo->p_packets_mgt, p_dec->p_pes );
+
+        if( p_dec->p_vout )
+        {
+            vlc_object_detach( p_dec->p_vout );
+            vout_DestroyThread( p_dec->p_vout );
+        }
+
+        free( p_dec );
+    }
+}
+
+/*****************************************************************************
+ * theora_SpawnVout: creates a new video output
+ *****************************************************************************/
+static vout_thread_t *theora_SpawnVout( dec_thread_t *p_dec,
+                                        int i_width,
+                                        int i_height,
+                                        int i_aspect,
+                                        int i_chroma )
+{
+    vout_thread_t *p_vout;
+
+    if( !i_width || !i_height )
+        return NULL;
+
+    if( !i_chroma )
+        return NULL;
+
+    /* Spawn a video output if there is none. First we look for our children,
+     * then we look for any other vout that might be available. */
+    p_vout = vlc_object_find( p_dec->p_fifo, VLC_OBJECT_VOUT,
+                                              FIND_CHILD );
+    if( !p_vout )
+    {
+        p_vout = vlc_object_find( p_dec->p_fifo, VLC_OBJECT_VOUT,
+                                                  FIND_ANYWHERE );
+    }
+
+    if( p_vout )
+    {
+        if( p_vout->render.i_width != i_width
+            || p_vout->render.i_height != i_height
+            || p_vout->render.i_chroma != i_chroma
+            || p_vout->render.i_aspect != i_aspect )
+        {
+            /* We are not interested in this format, close this vout */
+            vlc_object_detach( p_vout );
+            vlc_object_release( p_vout );
+            vout_DestroyThread( p_vout );
+            p_vout = NULL;
+        }
+        else
+        {
+            /* This video output is cool! Hijack it. */
+            vlc_object_detach( p_vout );
+            vlc_object_attach( p_vout, p_dec->p_fifo );
+            vlc_object_release( p_vout );
+        }
+    }
+
+    if( p_vout == NULL )
+    {
+        msg_Dbg( p_dec->p_fifo, "no vout present, spawning one" );
+
+        p_vout = vout_CreateThread( p_dec->p_fifo,
+                                    i_width, i_height,
+                                    i_chroma, i_aspect );
+    }
+
+    return( p_vout );
+}
+
+/*****************************************************************************
+ * theora_CopyPicture: copy a picture from theora internal buffers to a
+ *                     picture_t structure.
+ *****************************************************************************/
+static void theora_CopyPicture( dec_thread_t *p_dec, picture_t *p_pic,
+                                yuv_buffer *yuv )
+{
+    int i_plane, i_line, i_width, i_dst_stride, i_src_stride;
+    u8  *p_dst, *p_src;
+
+    for( i_plane = 0; i_plane < p_pic->i_planes; i_plane++ )
+    {
+        p_dst = p_pic->p[i_plane].p_pixels;
+        p_src = i_plane ? (i_plane - 1 ? yuv->v : yuv->u ) : yuv->y;
+        i_width = p_pic->p[i_plane].i_visible_pitch;
+        i_dst_stride = p_pic->p[i_plane].i_pitch;
+        i_src_stride = i_plane ? yuv->uv_stride : yuv->y_stride;
+
+        for( i_line = 0; i_line < p_pic->p[i_plane].i_lines; i_line++ )
+        {
+            p_dec->p_fifo->p_vlc->pf_memcpy( p_dst, p_src, i_width );
+            p_src += i_src_stride;
+            p_dst += i_dst_stride;
+        }
+    }
+}
index 6fe549636e8a8db274947f8f4c12cb6fd57547ef..2d64e3c889b980858dd2ad8b842eea47d5b83d67 100644 (file)
@@ -2,7 +2,7 @@
  * ogg.c : ogg stream input module for vlc
  *****************************************************************************
  * Copyright (C) 2001 VideoLAN
- * $Id: ogg.c,v 1.9 2002/11/18 19:31:20 fenrir Exp $
+ * $Id: ogg.c,v 1.10 2002/11/20 14:09:57 gbazin Exp $
  *
  * Authors: Gildas Bazin <gbazin@netcourrier.com>
  * 
@@ -52,7 +52,7 @@ typedef struct logical_stream_s
     vlc_fourcc_t     i_fourcc;
     vlc_fourcc_t     i_codec;
 
-    es_descriptor_t  *p_es;   
+    es_descriptor_t  *p_es;
     int              b_selected;                           /* newly selected */
 
     /* the header of some logical streams (eg vorbis) contain essential
@@ -64,16 +64,18 @@ typedef struct logical_stream_s
 
     /* program clock reference (in units of 90kHz) derived from the previous
      * granulepos */
-    mtime_t i_pcr;
-    long    l_previous_granulepos;
+    mtime_t          i_pcr;
+    mtime_t          i_last_received_pcr;
 
     /* info from logical streams */
-    double i_rate;
+    double f_rate;
     int i_bitrate;
     int b_reinit;
 
+    /* codec specific stuff */
     BITMAPINFOHEADER *p_bih;
     WAVEFORMATEX *p_wf;
+    int i_theora_keyframe_granule_shift;
 
 } logical_stream_t;
 
@@ -315,7 +317,7 @@ static void Ogg_DecodePacket( input_thread_t *p_input,
 
     if( p_stream->b_force_backup )
     {
-        /* Backup the ogg packet (likely an header) */
+        /* Backup the ogg packet (likely an header packet) */
         ogg_packet *p_packet_backup;
         p_stream->i_packets_backup++;
         p_stream->p_packets_backup =
@@ -349,11 +351,45 @@ static void Ogg_DecodePacket( input_thread_t *p_input,
         /* This stream isn't currently selected so we don't need to decode it,
          * but we do need to store its pcr as it might be selected later on. */
 
-        /* Convert the next granule into a pcr */
-        p_stream->i_pcr = ( p_oggpacket->granulepos < 0 ) ?
-            p_stream->i_pcr + ( p_oggpacket->bytes * 90000
-                / p_stream->i_bitrate / 8 ):
-            p_oggpacket->granulepos * 90000 / p_stream->i_rate;
+        /* Convert the next granulepos into a pcr */
+        if( p_oggpacket->granulepos >= 0 )
+        {
+            if( p_stream->i_fourcc != VLC_FOURCC( 't','h','e','o' ) )
+            {
+                p_stream->i_pcr = p_oggpacket->granulepos * 90000
+                                  / p_stream->f_rate;
+            }
+            else
+            {
+                ogg_int64_t iframe = p_oggpacket->granulepos >>
+                    p_stream->i_theora_keyframe_granule_shift;
+                ogg_int64_t pframe = p_oggpacket->granulepos -
+                    ( iframe << p_stream->i_theora_keyframe_granule_shift );
+
+                p_stream->i_pcr = ( iframe + pframe ) * 90000
+                                  / p_stream->f_rate;
+            }
+
+            p_stream->i_last_received_pcr = p_stream->i_pcr;
+        }
+        else
+        {
+            /* no granulepos available, try to interpolate */
+            if( p_stream->i_bitrate )
+                p_stream->i_pcr += ( p_oggpacket->bytes * 90000
+                                     / p_stream->i_bitrate / 8 );
+            else
+                p_stream->i_pcr = -1;
+        }
+
+        /* Update the main pcr */
+        if( p_stream == p_ogg->p_stream_timeref )
+        {
+            if( p_ogg->p_stream_timeref->i_pcr >= 0 )
+            {
+                p_ogg->i_pcr = p_ogg->p_stream_timeref->i_pcr;
+            }
+        }
 
         return;
     }
@@ -368,8 +404,7 @@ static void Ogg_DecodePacket( input_thread_t *p_input,
         input_DeletePES( p_input->p_method_data, p_pes );
         return;
     }
-    
-    p_pes->i_dts = p_oggpacket->granulepos;
+
     /* Convert the pcr into a pts */
     if( p_stream->i_cat != SPU_ES )
     {
@@ -383,22 +418,37 @@ static void Ogg_DecodePacket( input_thread_t *p_input,
         p_pes->i_pts = ( p_oggpacket->granulepos < 0 ) ? 0 :
             input_ClockGetTS( p_input, p_input->stream.p_selected_program,
                               p_oggpacket->granulepos * 90000 /
-                              p_stream->i_rate );
-        p_pes->i_dts = 0;
+                              p_stream->f_rate );
     }
 
-    /* Convert the next granule into a pcr */
-    if( p_oggpacket->granulepos < 0 )
+    /* Convert the next granulepos into a pcr */
+    if( p_oggpacket->granulepos >= 0 )
     {
-        /* FIXME: ffmpeg doesn't like null pts */
-        if( p_stream->i_cat == VIDEO_ES )
-            p_stream->i_pcr += (90000 / p_stream->i_rate);
+        if( p_stream->i_fourcc != VLC_FOURCC( 't','h','e','o' ) )
+        {
+            p_stream->i_pcr = p_oggpacket->granulepos * 90000
+                              / p_stream->f_rate;
+        }
         else
-            p_stream->i_pcr = -1;
+        {
+            ogg_int64_t iframe = p_oggpacket->granulepos >>
+                p_stream->i_theora_keyframe_granule_shift;
+            ogg_int64_t pframe = p_oggpacket->granulepos -
+                ( iframe << p_stream->i_theora_keyframe_granule_shift );
+
+            p_stream->i_pcr = ( iframe + pframe ) * 90000 / p_stream->f_rate;
+        }
+
+        p_stream->i_last_received_pcr = p_stream->i_pcr;
     }
     else
     {
-        p_stream->i_pcr = p_oggpacket->granulepos * 90000 / p_stream->i_rate;
+        /* FIXME: ffmpeg doesn't like null pts */
+        if( p_stream->i_cat == VIDEO_ES )
+            /* 1 frame per packet */
+            p_stream->i_pcr += (90000 / p_stream->f_rate);
+        else
+            p_stream->i_pcr = -1;
     }
 
     /* Update the main pcr */
@@ -410,16 +460,21 @@ static void Ogg_DecodePacket( input_thread_t *p_input,
         }
         else
         {
-            p_ogg->i_pcr += ( p_oggpacket->bytes * 90000
-                              / p_stream->i_bitrate / 8 );
+            /* no granulepos available, try to interpolate */
+            if( p_stream->i_bitrate )
+                p_ogg->i_pcr += ( p_oggpacket->bytes * 90000
+                                     / p_stream->i_bitrate / 8 );
         }
     }
 
     p_pes->i_nb_data = 1;
+    p_pes->i_dts = p_oggpacket->granulepos;
     p_pes->p_first = p_pes->p_last = p_data;
     p_pes->i_pes_size = p_oggpacket->bytes;
 
-    if( p_stream->i_fourcc != VLC_FOURCC( 'v','o','r','b' ) )
+    if( p_stream->i_fourcc != VLC_FOURCC( 'v','o','r','b' ) &&
+        p_stream->i_fourcc != VLC_FOURCC( 't','a','r','k' ) &&
+        p_stream->i_fourcc != VLC_FOURCC( 't','h','e','o' ) )
     {
         /* Remove the header from the packet */
         i_header_len = (*p_oggpacket->packet & PACKET_LEN_BITS01) >> 6;
@@ -427,6 +482,15 @@ static void Ogg_DecodePacket( input_thread_t *p_input,
         i_header_len++;
 
         p_pes->i_pes_size -= i_header_len;
+        p_pes->i_dts = 0;
+    }
+
+    if( p_stream->i_fourcc == VLC_FOURCC( 't','a','r','k' ) )
+    {
+        /* FIXME: the biggest hack I've ever done */
+        msg_Warn( p_input, "tark pts: %lli, granule: %i",
+                  p_pes->i_pts, p_pes->i_dts );
+        msleep(10000);
     }
 
     memcpy( p_data->p_payload_start,
@@ -513,10 +577,85 @@ static int Ogg_FindLogicalStreams( input_thread_t *p_input, demux_sys_t *p_ogg)
                     /* Cheat and get additionnal info ;) */
                     oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
                     oggpack_adv( &opb, 96 );
-                    p_stream->i_rate = oggpack_read( &opb, 32 );
+                    p_stream->f_rate = oggpack_read( &opb, 32 );
                     oggpack_adv( &opb, 32 );
                     p_stream->i_bitrate = oggpack_read( &opb, 32 );
                 }
+                /* Check for Theora header */
+                else if( oggpacket.bytes >= 7 &&
+                         ! strncmp( &oggpacket.packet[1], "theora", 6 ) )
+                {
+#ifdef HAVE_OGGPACKB
+                    oggpack_buffer opb;
+                    int i_fps_numerator;
+                    int i_fps_denominator;
+                    int i_keyframe_frequency_force;
+#endif
+
+                    msg_Dbg( p_input, "found theora header" );
+#ifdef HAVE_OGGPACKB
+                    p_stream->i_cat = VIDEO_ES;
+                    p_stream->i_fourcc = VLC_FOURCC( 't','h','e','o' );
+
+                    /* Cheat and get additionnal info ;) */
+                    oggpackB_readinit(&opb, oggpacket.packet, oggpacket.bytes);
+                    oggpackB_adv( &opb, 56 );
+                    oggpackB_read( &opb, 8 ); /* major version num */
+                    oggpackB_read( &opb, 8 ); /* minor version num */
+                    oggpackB_read( &opb, 8 ); /* subminor version num */
+                    oggpackB_read( &opb, 16 ) /*<< 4*/; /* width */
+                    oggpackB_read( &opb, 16 ) /*<< 4*/; /* height */
+                    i_fps_numerator = oggpackB_read( &opb, 32 );
+                    i_fps_denominator = oggpackB_read( &opb, 32 );
+                    oggpackB_read( &opb, 24 ); /* aspect_numerator */
+                    oggpackB_read( &opb, 24 ); /* aspect_denominator */
+                    i_keyframe_frequency_force = 1 << oggpackB_read( &opb, 5 );
+                    p_stream->i_bitrate = oggpackB_read( &opb, 24 );
+                    oggpackB_read(&opb,6); /* quality */
+
+                    /* granule_shift = i_log( frequency_force -1 ) */
+                    p_stream->i_theora_keyframe_granule_shift = 0;
+                    i_keyframe_frequency_force--;
+                    while( i_keyframe_frequency_force )
+                    {
+                        p_stream->i_theora_keyframe_granule_shift++;
+                        i_keyframe_frequency_force >>= 1;
+                    }
+
+                    p_stream->f_rate = (float)i_fps_numerator /
+                                                i_fps_denominator;
+                    msg_Dbg( p_input,
+                             "found theora header, bitrate: %i, rate: %f",
+                             p_stream->i_bitrate, p_stream->f_rate );
+#else /* HAVE_OGGPACKB */
+                    msg_Dbg( p_input, "the ogg demuxer has been compiled "
+                             "without support for the oggpackB extension."
+                             "The theora stream won't be decoded." );
+                    free( p_stream );
+                    p_ogg->i_streams--;
+                    continue;
+#endif /* HAVE_OGGPACKB */
+                }
+                /* Check for Tarkin header */
+                else if( oggpacket.bytes >= 7 &&
+                         ! strncmp( &oggpacket.packet[1], "tarkin", 6 ) )
+                {
+                    oggpack_buffer opb;
+
+                    msg_Dbg( p_input, "found tarkin header" );
+                    p_stream->i_cat = VIDEO_ES;
+                    p_stream->i_fourcc = VLC_FOURCC( 't','a','r','k' );
+
+                    /* Cheat and get additionnal info ;) */
+                    oggpack_readinit( &opb, oggpacket.packet, oggpacket.bytes);
+                    oggpack_adv( &opb, 88 );
+                    oggpack_adv( &opb, 104 );
+                    p_stream->i_bitrate = oggpack_read( &opb, 32 );
+                    p_stream->f_rate = 2; /* FIXME */
+                    msg_Dbg( p_input,
+                             "found tarkin header, bitrate: %i, rate: %f",
+                             p_stream->i_bitrate, p_stream->f_rate );
+                }
                 else if( (*oggpacket.packet & PACKET_TYPE_BITS )
                          == PACKET_TYPE_HEADER && 
                          oggpacket.bytes >= (int)sizeof(stream_header)+1 )
@@ -549,7 +688,7 @@ static int Ogg_FindLogicalStreams( input_thread_t *p_input, demux_sys_t *p_ogg)
                         msg_Dbg( p_input, "found video header of type: %.4s",
                                  (char *)&p_stream->i_fourcc );
 
-                        p_stream->i_rate = 10000000.0 /
+                        p_stream->f_rate = 10000000.0 /
                             GetQWLE((uint8_t *)&st->time_unit);
                         p_stream->p_bih->biBitCount =
                             GetWLE((uint8_t *)&st->bits_per_sample);
@@ -565,11 +704,11 @@ static int Ogg_FindLogicalStreams( input_thread_t *p_input, demux_sys_t *p_ogg)
 
                         msg_Dbg( p_input,
                              "fps: %f, width:%i; height:%i, bitcount:%i",
-                            p_stream->i_rate, p_stream->p_bih->biWidth,
+                            p_stream->f_rate, p_stream->p_bih->biWidth,
                             p_stream->p_bih->biHeight,
                             p_stream->p_bih->biBitCount);
 
-                        p_stream->i_bitrate = 1; /* FIXME */
+                        p_stream->i_bitrate = 0;
                     }
                     /* Check for audio header (new format) */
                     else if( !strncmp( st->streamtype, "audio", 5 ) )
@@ -596,7 +735,7 @@ static int Ogg_FindLogicalStreams( input_thread_t *p_input, demux_sys_t *p_ogg)
                         p_stream->p_wf->wFormatTag = strtol(p_buffer,NULL,16);
                         p_stream->p_wf->nChannels =
                             GetWLE((uint8_t *)&st->sh.audio.channels);
-                        p_stream->i_rate = p_stream->p_wf->nSamplesPerSec =
+                        p_stream->f_rate = p_stream->p_wf->nSamplesPerSec =
                             GetQWLE((uint8_t *)&st->samples_per_unit);
                         p_stream->i_bitrate = p_stream->p_wf->nAvgBytesPerSec =
                             GetDWLE((uint8_t *)&st->sh.audio.avgbytespersec);
@@ -656,7 +795,7 @@ static int Ogg_FindLogicalStreams( input_thread_t *p_input, demux_sys_t *p_ogg)
                         p_stream->i_cat = SPU_ES;
                         p_stream->i_fourcc =
                             VLC_FOURCC( 's', 'u', 'b', 't' );
-                        p_stream->i_rate = 1000; /* granulepos is in milisec */
+                        p_stream->f_rate = 1000; /* granulepos is in milisec */
                     }
                     else
                     {
@@ -981,7 +1120,9 @@ static int Demux( input_thread_t * p_input )
     }
 
 
-    /* Demux ogg pages from the stream */
+    /*
+     * Demux ogg pages from the stream
+     */
     for( i = 0; (i < PAGES_READ_ONCE) || p_ogg->p_stream_timeref->b_reinit;
          i++ )
     {
@@ -1009,7 +1150,7 @@ static int Demux( input_thread_t * p_input )
                     {
                         p_stream->b_reinit = 0;
                         p_stream->i_pcr = oggpacket.granulepos
-                            * 90000 / p_stream->i_rate;
+                            * 90000 / p_stream->f_rate;
 
                         if( !p_ogg->i_pcr ||
                             (p_stream == p_ogg->p_stream_timeref) )