]> git.sesse.net Git - vlc/blobdiff - modules/codec/theora.c
Modified the way xiph codecs headers are transported in VLC.
[vlc] / modules / codec / theora.c
index d9345003ceb38d2441b7c0f55092ec450456db06..57d90d5e6abb7e0406c6cc6a707f0df4821c8f17 100644 (file)
@@ -33,6 +33,8 @@
 #include <vlc_codec.h>
 #include <vlc_sout.h>
 #include <vlc_input.h>
+#include "../demux/xiph.h"
+
 #include <ogg/ogg.h>
 
 #include <theora/theora.h>
@@ -48,7 +50,7 @@ struct decoder_sys_t
     /*
      * Input properties
      */
-    int i_headers;
+    bool b_has_headers;
 
     /*
      * Theora properties
@@ -143,7 +145,7 @@ static int OpenDecoder( vlc_object_t *p_this )
     if( ( p_dec->p_sys = p_sys = malloc(sizeof(*p_sys)) ) == NULL )
         return VLC_ENOMEM;
     p_dec->p_sys->b_packetizer = false;
-
+    p_sys->b_has_headers = false;
     p_sys->i_pts = VLC_TS_INVALID;
     p_sys->b_decoded_first_keyframe = false;
 
@@ -161,8 +163,6 @@ static int OpenDecoder( vlc_object_t *p_this )
     theora_comment_init( &p_sys->tc );
     theora_info_init( &p_sys->ti );
 
-    p_sys->i_headers = 0;
-
     return VLC_SUCCESS;
 }
 
@@ -205,40 +205,14 @@ static void *DecodeBlock( decoder_t *p_dec, block_t **pp_block )
     oggpacket.packetno = 0;
 
     /* Check for headers */
-    if( p_sys->i_headers == 0 && p_dec->fmt_in.i_extra )
-    {
-        /* Headers already available as extra data */
-        p_sys->i_headers = 3;
-    }
-    else if( oggpacket.bytes && p_sys->i_headers < 3 )
+    if( !p_sys->b_has_headers )
     {
-        /* Backup headers as extra data */
-        uint8_t *p_extra;
-
-        p_dec->fmt_in.p_extra = xrealloc( p_dec->fmt_in.p_extra,
-                                p_dec->fmt_in.i_extra + oggpacket.bytes + 2 );
-        p_extra = ((uint8_t *)p_dec->fmt_in.p_extra) + p_dec->fmt_in.i_extra;
-        *(p_extra++) = oggpacket.bytes >> 8;
-        *(p_extra++) = oggpacket.bytes & 0xFF;
-
-        memcpy( p_extra, oggpacket.packet, oggpacket.bytes );
-        p_dec->fmt_in.i_extra += oggpacket.bytes + 2;
-
-        block_Release( *pp_block );
-        p_sys->i_headers++;
-        return NULL;
-    }
-
-    if( p_sys->i_headers == 3 )
-    {
-        if( ProcessHeaders( p_dec ) != VLC_SUCCESS )
+        if( ProcessHeaders( p_dec ) )
         {
-            p_sys->i_headers = 0;
-            p_dec->fmt_in.i_extra = 0;
             block_Release( *pp_block );
             return NULL;
         }
-        else p_sys->i_headers++;
+        p_sys->b_has_headers = true;
     }
 
     return ProcessPacket( p_dec, &oggpacket, pp_block );
@@ -251,34 +225,28 @@ static int ProcessHeaders( decoder_t *p_dec )
 {
     decoder_sys_t *p_sys = p_dec->p_sys;
     ogg_packet oggpacket;
-    uint8_t *p_extra;
-    int i_extra;
 
-    if( !p_dec->fmt_in.i_extra ) return VLC_EGENERIC;
+    unsigned pi_size[XIPH_MAX_HEADER_COUNT];
+    void     *pp_data[XIPH_MAX_HEADER_COUNT];
+    unsigned i_count;
+    if( xiph_SplitHeaders( pi_size, pp_data, &i_count,
+                           p_dec->fmt_in.i_extra, p_dec->fmt_in.p_extra) )
+        return VLC_EGENERIC;
+    if( i_count < 3 )
+        goto error;
 
     oggpacket.granulepos = -1;
-    oggpacket.b_o_s = 1; /* yes this actually is a b_o_s packet :) */
     oggpacket.e_o_s = 0;
     oggpacket.packetno = 0;
-    p_extra = p_dec->fmt_in.p_extra;
-    i_extra = p_dec->fmt_in.i_extra;
 
     /* Take care of the initial Vorbis header */
-    oggpacket.bytes = *(p_extra++) << 8;
-    oggpacket.bytes |= (*(p_extra++) & 0xFF);
-    oggpacket.packet = p_extra;
-    p_extra += oggpacket.bytes;
-    i_extra -= (oggpacket.bytes + 2);
-    if( i_extra < 0 )
-    {
-        msg_Err( p_dec, "header data corrupted");
-        return VLC_EGENERIC;
-    }
-
+    oggpacket.b_o_s  = 1; /* yes this actually is a b_o_s packet :) */
+    oggpacket.bytes  = pi_size[0];
+    oggpacket.packet = pp_data[0];
     if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
     {
         msg_Err( p_dec, "this bitstream does not contain Theora video data" );
-        return VLC_EGENERIC;
+        goto error;
     }
 
     /* Set output properties */
@@ -351,23 +319,13 @@ static int ProcessHeaders( decoder_t *p_dec )
     }
 
     /* The next packet in order is the comments header */
-    oggpacket.b_o_s = 0;
-    oggpacket.bytes = *(p_extra++) << 8;
-    oggpacket.bytes |= (*(p_extra++) & 0xFF);
-    oggpacket.packet = p_extra;
-    p_extra += oggpacket.bytes;
-    i_extra -= (oggpacket.bytes + 2);
-    if( i_extra < 0 )
-    {
-        msg_Err( p_dec, "header data corrupted");
-        return VLC_EGENERIC;
-    }
-
-    /* The next packet in order is the comments header */
+    oggpacket.b_o_s  = 0;
+    oggpacket.bytes  = pi_size[1];
+    oggpacket.packet = pp_data[1];
     if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
     {
         msg_Err( p_dec, "2nd Theora header is corrupted" );
-        return VLC_EGENERIC;
+        goto error;
     }
 
     ParseTheoraComments( p_dec );
@@ -375,23 +333,13 @@ static int ProcessHeaders( decoder_t *p_dec )
     /* The next packet in order is the codebooks header
      * We need to watch out that this packet is not missing as a
      * missing or corrupted header is fatal. */
-    oggpacket.bytes = *(p_extra++) << 8;
-    oggpacket.bytes |= (*(p_extra++) & 0xFF);
-    oggpacket.packet = p_extra;
-    i_extra -= (oggpacket.bytes + 2);
-    if( i_extra < 0 )
-    {
-        msg_Err( p_dec, "header data corrupted");
-        return VLC_EGENERIC;
-    }
-
-    /* The next packet in order is the codebooks header
-     * We need to watch out that this packet is not missing as a
-     * missing or corrupted header is fatal */
+    oggpacket.b_o_s  = 0;
+    oggpacket.bytes  = pi_size[2];
+    oggpacket.packet = pp_data[2];
     if( theora_decode_header( &p_sys->ti, &p_sys->tc, &oggpacket ) < 0 )
     {
         msg_Err( p_dec, "3rd Theora header is corrupted" );
-        return VLC_EGENERIC;
+        goto error;
     }
 
     if( !p_sys->b_packetizer )
@@ -408,7 +356,14 @@ static int ProcessHeaders( decoder_t *p_dec )
                 p_dec->fmt_in.p_extra, p_dec->fmt_out.i_extra );
     }
 
+    for( unsigned i = 0; i < i_count; i++ )
+        free( pp_data[i] );
     return VLC_SUCCESS;
+
+error:
+    for( unsigned i = 0; i < i_count; i++ )
+        free( pp_data[i] );
+    return VLC_EGENERIC;
 }
 
 /*****************************************************************************
@@ -442,21 +397,15 @@ static void *ProcessPacket( decoder_t *p_dec, ogg_packet *p_oggpacket,
         /* Date management */
         p_block->i_dts = p_block->i_pts = p_sys->i_pts;
 
-        if( p_sys->i_headers >= 3 )
-            p_block->i_length = p_sys->i_pts - p_block->i_pts;
-        else
-            p_block->i_length = 0;
+        p_block->i_length = p_sys->i_pts - p_block->i_pts;
 
         p_buf = p_block;
     }
     else
     {
-        if( p_sys->i_headers >= 3 )
-            p_buf = DecodePacket( p_dec, p_oggpacket );
-        else
-            p_buf = NULL;
-
-        if( p_block ) block_Release( p_block );
+        p_buf = DecodePacket( p_dec, p_oggpacket );
+        if( p_block )
+            block_Release( p_block );
     }
 
     /* Date management */