]> git.sesse.net Git - vlc/blobdiff - modules/demux/ogg.c
httplive: Adjust conditions for playlist reloading
[vlc] / modules / demux / ogg.c
index 4088773ec5311a8b80dfb9ab3c763a60f65eacf8..9c858c861123532cfd9a62e3210f5f2ba25e7452 100644 (file)
@@ -137,6 +137,7 @@ static void Ogg_ReadKateHeader( logical_stream_t *, ogg_packet * );
 static void Ogg_ReadFlacHeader( demux_t *, logical_stream_t *, ogg_packet * );
 static void Ogg_ReadAnnodexHeader( demux_t *, logical_stream_t *, ogg_packet * );
 static bool Ogg_ReadDiracHeader( logical_stream_t *, ogg_packet * );
+static bool Ogg_ReadVP8Header( demux_t *, logical_stream_t *, ogg_packet * );
 static void Ogg_ReadSkeletonHeader( demux_t *, logical_stream_t *, ogg_packet * );
 
 /* Skeleton */
@@ -291,16 +292,10 @@ static int Demux( demux_t * p_demux )
         }
     }
 
-    if ( p_sys->b_preparsing_done )
+    if ( p_sys->b_preparsing_done && !p_sys->b_es_created )
     {
-        for ( int i=0; i < p_sys->i_streams; i++ )
-        {
-            if ( !p_sys->pp_stream[i]->p_es )
-            {
-                Ogg_CreateES( p_demux );
-                break;
-            }
-        }
+        Ogg_CreateES( p_demux );
+        p_sys->b_es_created = true;
     }
 
     /*
@@ -496,6 +491,7 @@ static int Demux( demux_t * p_demux )
                 if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS ||
                     p_stream->fmt.i_codec == VLC_CODEC_SPEEX ||
                     p_stream->fmt.i_codec == VLC_CODEC_OPUS ||
+                    p_stream->fmt.i_codec == VLC_CODEC_VP8 ||
                     p_stream->fmt.i_codec == VLC_CODEC_FLAC )
                 {
                     if( ogg_stream_packetout( &p_stream->os, &oggpacket ) > 0 )
@@ -602,6 +598,7 @@ static logical_stream_t * Ogg_GetSelectedStream( demux_t *p_demux )
     for( int i=0; i<p_sys->i_streams; i++ )
     {
         logical_stream_t *p_candidate = p_sys->pp_stream[i];
+        if ( !p_candidate->p_es ) continue;
 
         bool b_selected = false;
         es_out_Control( p_demux->out, ES_OUT_GET_ES_STATE,
@@ -867,6 +864,7 @@ static void Ogg_UpdatePCR( demux_t *p_demux, logical_stream_t *p_stream,
     {
         if( p_stream->fmt.i_codec == VLC_CODEC_THEORA ||
             p_stream->fmt.i_codec == VLC_CODEC_KATE ||
+            p_stream->fmt.i_codec == VLC_CODEC_VP8 ||
             p_stream->fmt.i_codec == VLC_CODEC_DIRAC )
         {
             p_stream->i_pcr = Oggseek_GranuleToAbsTimestamp( p_stream,
@@ -978,6 +976,13 @@ static void Ogg_DecodePacket( demux_t *p_demux,
         Ogg_ReadSkeletonIndex( p_demux, p_oggpacket );
         return;
     }
+    else if( p_stream->fmt.i_codec == VLC_CODEC_VP8 &&
+             p_oggpacket->bytes >= 7 &&
+             !memcmp( p_oggpacket->packet, "OVP80\x02\x20", 7 ) )
+    {
+        Ogg_ReadVP8Header( p_demux, p_stream, p_oggpacket );
+        return;
+    }
 
     if( p_stream->fmt.i_codec == VLC_CODEC_SUBT && p_oggpacket->bytes > 0 &&
         p_oggpacket->packet[0] & PACKET_TYPE_BITS ) return;
@@ -1102,6 +1107,7 @@ static void Ogg_DecodePacket( demux_t *p_demux,
     if( p_stream->fmt.i_codec == VLC_CODEC_VORBIS ||
         p_stream->fmt.i_codec == VLC_CODEC_SPEEX ||
         p_stream->fmt.i_codec == VLC_CODEC_OPUS ||
+        p_stream->fmt.i_codec == VLC_CODEC_VP8 ||
         p_stream->fmt.i_codec == VLC_CODEC_FLAC )
     {
         if( p_stream->i_pcr > VLC_TS_INVALID )
@@ -1119,6 +1125,7 @@ static void Ogg_DecodePacket( demux_t *p_demux,
     if( p_stream->fmt.i_codec != VLC_CODEC_VORBIS &&
         p_stream->fmt.i_codec != VLC_CODEC_SPEEX &&
         p_stream->fmt.i_codec != VLC_CODEC_OPUS &&
+        p_stream->fmt.i_codec != VLC_CODEC_VP8 &&
         p_stream->fmt.i_codec != VLC_CODEC_FLAC &&
         p_stream->i_pcr >= 0 )
     {
@@ -1211,6 +1218,15 @@ static void Ogg_DecodePacket( demux_t *p_demux,
         if( -1 != p_oggpacket->granulepos )
             p_block->i_pts = u_pnum * CLOCK_FREQ / p_stream->f_rate / 2;
     }
+    else if( p_stream->fmt.i_codec == VLC_CODEC_VP8 )
+    {
+        p_block->i_pts = p_stream->i_interpolated_pcr;
+        p_block->i_dts = p_block->i_pts;
+        if( p_oggpacket->granulepos > 0 && Ogg_IsKeyFrame( p_stream, p_oggpacket ) )
+        {
+            p_block->i_flags |= BLOCK_FLAG_TYPE_I;
+        }
+    }
     else
     {
         p_block->i_dts = i_pts;
@@ -1220,6 +1236,7 @@ static void Ogg_DecodePacket( demux_t *p_demux,
     if( p_stream->fmt.i_codec != VLC_CODEC_VORBIS &&
         p_stream->fmt.i_codec != VLC_CODEC_SPEEX &&
         p_stream->fmt.i_codec != VLC_CODEC_OPUS &&
+        p_stream->fmt.i_codec != VLC_CODEC_VP8 &&
         p_stream->fmt.i_codec != VLC_CODEC_FLAC &&
         p_stream->fmt.i_codec != VLC_CODEC_TARKIN &&
         p_stream->fmt.i_codec != VLC_CODEC_THEORA &&
@@ -1507,6 +1524,19 @@ static int Ogg_FindLogicalStreams( demux_t *p_demux )
                              "found tarkin header, bitrate: %i, rate: %f",
                              p_stream->fmt.i_bitrate, p_stream->f_rate );
                 }
+                /* Check for VP8 header */
+                else if( oggpacket.bytes >= 26 &&
+                         ! memcmp( oggpacket.packet, "OVP80", 5 ) )
+                {
+                    if ( Ogg_ReadVP8Header( p_demux, p_stream, &oggpacket ) )
+                        msg_Dbg( p_demux, "found VP8 header "
+                             "fps: %f, width:%i; height:%i",
+                             p_stream->f_rate,
+                             p_stream->fmt.video.i_width,
+                             p_stream->fmt.video.i_height );
+                    else
+                        p_stream->fmt.i_cat = UNKNOWN_ES;
+                }
                 /* Check for Annodex header */
                 else if( oggpacket.bytes >= 7 &&
                          ! memcmp( oggpacket.packet, "Annodex", 7 ) )
@@ -1827,7 +1857,7 @@ static void Ogg_CreateES( demux_t *p_demux )
     {
         logical_stream_t *p_stream = p_ogg->pp_stream[i_stream];
 
-        if ( p_stream->p_es == NULL )
+        if ( p_stream->p_es == NULL && !p_stream->b_finished )
         {
             /* Better be safe than sorry when possible with ogm */
             if( p_stream->fmt.i_codec == VLC_CODEC_MPGA ||
@@ -1845,12 +1875,16 @@ static void Ogg_CreateES( demux_t *p_demux )
                 p_stream->b_finished = false;
                 p_stream->b_reinit = false;
                 p_stream->b_initializing = false;
+                bool b_resetdecoder = Ogg_LogicalStreamResetEsFormat( p_demux, p_stream );
                 es_format_Copy( &p_stream->fmt_old, &p_old_stream->fmt );
 
                 p_old_stream->p_es = NULL;
                 p_old_stream = NULL;
-                es_out_Control( p_demux->out, ES_OUT_SET_ES_FMT,
-                                p_stream->p_es, &p_stream->fmt );
+                if ( b_resetdecoder )
+                {
+                    es_out_Control( p_demux->out, ES_OUT_SET_ES_FMT,
+                                    p_stream->p_es, &p_stream->fmt );
+                }
             }
             else
             {
@@ -1940,6 +1974,7 @@ static void Ogg_EndOfStream( demux_t *p_demux )
     p_ogg->skeleton.major = 0;
     p_ogg->skeleton.minor = 0;
     p_ogg->b_preparsing_done = false;
+    p_ogg->b_es_created = false;
     p_ogg->i_pcr_offset = p_ogg->i_pcr;
 
     /* */
@@ -2119,56 +2154,46 @@ static bool Ogg_LogicalStreamResetEsFormat( demux_t *p_demux, logical_stream_t *
 
     return !b_compatible;
 }
-static void Ogg_ExtractXiphMeta( demux_t *p_demux, es_format_t *p_fmt,
-                                 const void *p_headers, unsigned i_headers, unsigned i_skip )
+
+static void Ogg_ExtractComments( demux_t *p_demux, es_format_t *p_fmt,
+                                 const void *p_headers, unsigned i_headers )
 {
     demux_sys_t *p_ogg = p_demux->p_sys;
+    int i_cover_score = 0;
+    int i_cover_idx = 0;
+    float pf_replay_gain[AUDIO_REPLAY_GAIN_MAX];
+    float pf_replay_peak[AUDIO_REPLAY_GAIN_MAX];
+    for(int i=0; i< AUDIO_REPLAY_GAIN_MAX; i++ )
+    {
+        pf_replay_gain[i] = 0;
+        pf_replay_peak[i] = 0;
+    }
+    vorbis_ParseComment( &p_ogg->p_meta, p_headers, i_headers,
+                         &p_ogg->i_attachments, &p_ogg->attachments,
+                         &i_cover_score, &i_cover_idx,
+                         &p_ogg->i_seekpoints, &p_ogg->pp_seekpoints,
+                         &pf_replay_gain, &pf_replay_peak );
+    if( p_ogg->p_meta != NULL && i_cover_idx < p_ogg->i_attachments )
+    {
+        char psz_url[128];
+        snprintf( psz_url, sizeof(psz_url), "attachment://%s",
+                  p_ogg->attachments[i_cover_idx]->psz_name );
+        vlc_meta_Set( p_ogg->p_meta, vlc_meta_ArtworkURL, psz_url );
+    }
 
-    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, i_headers, p_headers ) )
-        return;
-
-    /* TODO how to handle multiple comments properly ? */
-    if( i_count >= 2 && pi_size[1] > i_skip )
+    for ( int i=0; i<AUDIO_REPLAY_GAIN_MAX;i++ )
     {
-        int i_cover_score = 0;
-        int i_cover_idx = 0;
-        float pf_replay_gain[AUDIO_REPLAY_GAIN_MAX];
-        float pf_replay_peak[AUDIO_REPLAY_GAIN_MAX];
-        for(int i=0; i< AUDIO_REPLAY_GAIN_MAX; i++ )
+        if ( pf_replay_gain[i] != 0 )
         {
-            pf_replay_gain[i] = 0;
-            pf_replay_peak[i] = 0;
+            p_fmt->audio_replay_gain.pb_gain[i] = true;
+            p_fmt->audio_replay_gain.pf_gain[i] = pf_replay_gain[i];
+            msg_Dbg( p_demux, "setting replay gain %d to %f", i, pf_replay_gain[i] );
         }
-        vorbis_ParseComment( &p_ogg->p_meta, (uint8_t*)pp_data[1] + i_skip, pi_size[1] - i_skip,
-                             &p_ogg->i_attachments, &p_ogg->attachments,
-                             &i_cover_score, &i_cover_idx,
-                             &p_ogg->i_seekpoints, &p_ogg->pp_seekpoints,
-                             &pf_replay_gain, &pf_replay_peak );
-        if( p_ogg->p_meta != NULL && i_cover_idx < p_ogg->i_attachments )
+        if ( pf_replay_peak[i] != 0 )
         {
-            char psz_url[128];
-            snprintf( psz_url, sizeof(psz_url), "attachment://%s",
-                p_ogg->attachments[i_cover_idx]->psz_name );
-            vlc_meta_Set( p_ogg->p_meta, vlc_meta_ArtworkURL, psz_url );
-        }
-
-        for ( int i=0; i<AUDIO_REPLAY_GAIN_MAX;i++ )
-        {
-            if ( pf_replay_gain[i] != 0 )
-            {
-                p_fmt->audio_replay_gain.pb_gain[i] = true;
-                p_fmt->audio_replay_gain.pf_gain[i] = pf_replay_gain[i];
-                msg_Dbg( p_demux, "setting replay gain %d to %f", i, pf_replay_gain[i] );
-            }
-            if ( pf_replay_peak[i] != 0 )
-            {
-                p_fmt->audio_replay_gain.pb_peak[i] = true;
-                p_fmt->audio_replay_gain.pf_peak[i] = pf_replay_peak[i];
-                msg_Dbg( p_demux, "setting replay peak %d to %f", i, pf_replay_gain[i] );
-            }
+            p_fmt->audio_replay_gain.pb_peak[i] = true;
+            p_fmt->audio_replay_gain.pf_peak[i] = pf_replay_peak[i];
+            msg_Dbg( p_demux, "setting replay peak %d to %f", i, pf_replay_gain[i] );
         }
     }
 
@@ -2177,6 +2202,23 @@ static void Ogg_ExtractXiphMeta( demux_t *p_demux, es_format_t *p_fmt,
         p_demux->info.i_update |= INPUT_UPDATE_TITLE_LIST;
     }
 }
+
+static void Ogg_ExtractXiphMeta( demux_t *p_demux, es_format_t *p_fmt,
+                                 const void *p_headers, unsigned i_headers, unsigned i_skip )
+{
+    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, i_headers, p_headers ) )
+        return;
+    /* TODO how to handle multiple comments properly ? */
+    if( i_count >= 2 && pi_size[1] > i_skip )
+    {
+        Ogg_ExtractComments( p_demux, p_fmt, (uint8_t*)pp_data[1] + i_skip, pi_size[1] - i_skip );
+    }
+}
+
 static void Ogg_ExtractMeta( demux_t *p_demux, es_format_t *p_fmt, const uint8_t *p_headers, int i_headers )
 {
     demux_sys_t *p_ogg = p_demux->p_sys;
@@ -2194,7 +2236,9 @@ static void Ogg_ExtractMeta( demux_t *p_demux, es_format_t *p_fmt, const uint8_t
     case VLC_CODEC_SPEEX:
         Ogg_ExtractXiphMeta( p_demux, p_fmt, p_headers, i_headers, 0 );
         break;
-
+    case VLC_CODEC_VP8:
+        Ogg_ExtractComments( p_demux, p_fmt, p_headers, i_headers );
+        break;
     /* N headers with the 2° one being the comments */
     case VLC_CODEC_KATE:
         /* 1 byte for header type, 7 bytes for magic, 1 reserved zero byte */
@@ -2306,7 +2350,8 @@ static void Ogg_ReadVorbisHeader( logical_stream_t *p_stream,
     p_stream->f_rate = p_stream->fmt.audio.i_rate =
         oggpack_read( &opb, 32 );
     oggpack_adv( &opb, 32 );
-    p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 );
+    p_stream->fmt.i_bitrate = oggpack_read( &opb, 32 ); /* is signed 32 */
+    if( p_stream->fmt.i_bitrate > INT32_MAX ) p_stream->fmt.i_bitrate = 0;
 }
 
 static void Ogg_ReadSpeexHeader( logical_stream_t *p_stream,
@@ -2462,6 +2507,37 @@ static void Ogg_ReadKateHeader( logical_stream_t *p_stream,
     }
 }
 
+static bool Ogg_ReadVP8Header( demux_t *p_demux, logical_stream_t *p_stream,
+                               ogg_packet *p_oggpacket )
+{
+    switch( p_oggpacket->packet[5] )
+    {
+    /* STREAMINFO */
+    case 0x01:
+        /* Mapping version */
+        if ( p_oggpacket->packet[6] != 0x01 || p_oggpacket->packet[7] != 0x00 )
+            return false;
+        p_stream->fmt.i_cat = VIDEO_ES;
+        p_stream->fmt.i_codec = VLC_CODEC_VP8;
+        p_stream->i_granule_shift = 32;
+        p_stream->fmt.video.i_width = GetWBE( &p_oggpacket->packet[8] );
+        p_stream->fmt.video.i_height = GetWBE( &p_oggpacket->packet[10] );
+        p_stream->fmt.video.i_sar_num = GetDWBE( &p_oggpacket->packet[12 - 1] ) & 0x0FFF;
+        p_stream->fmt.video.i_sar_den = GetDWBE( &p_oggpacket->packet[15 - 1] ) & 0x0FFF;
+        p_stream->fmt.video.i_frame_rate = GetDWBE( &p_oggpacket->packet[18] );
+        p_stream->fmt.video.i_frame_rate_base = GetDWBE( &p_oggpacket->packet[22] );
+        p_stream->f_rate = (double) p_stream->fmt.video.i_frame_rate / p_stream->fmt.video.i_frame_rate_base;
+        return true;
+    /* METADATA */
+    case 0x02:
+        Ogg_ExtractMeta( p_demux, & p_stream->fmt,
+                         p_oggpacket->packet + 7, p_oggpacket->bytes - 7 );
+        return true;
+    default:
+        return false;
+    }
+}
+
 static void Ogg_ApplyContentType( logical_stream_t *p_stream, const char* psz_value,
                                   bool *b_force_backup, bool *b_packet_out )
 {
@@ -2530,6 +2606,11 @@ static void Ogg_ApplyContentType( logical_stream_t *p_stream, const char* psz_va
         free( p_stream->fmt.psz_description );
         p_stream->fmt.psz_description = strdup("OGG Kate Overlay (Unsupported)");
     }
+    else if( !strncmp(psz_value, "video/x-vp8", 11) )
+    {
+        p_stream->fmt.i_cat = VIDEO_ES;
+        p_stream->fmt.i_codec = VLC_CODEC_VP8;
+    }
 }
 
 static void Ogg_ReadAnnodexHeader( demux_t *p_demux,