]> git.sesse.net Git - vlc/blobdiff - modules/demux/nuv.c
fix teletext framing code in DVB PES packets ignored
[vlc] / modules / demux / nuv.c
index 99cad8397b69466a98a7938d6183bfb6f7902576..be4008dc1e89fc55b7a87c0dfa4c56253ea06858 100644 (file)
@@ -1,25 +1,25 @@
 /*****************************************************************************
  * nuv.c:
  *****************************************************************************
- * Copyright (C) 2005 the VideoLAN team
+ * Copyright (C) 2005 VLC authors and VideoLAN
  * $Id$
  *
  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
  *          Gertjan Van Droogenbroeck <gertjanvd _PLUS_ vlc _AT_ gmail _DOT_ 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
+ * This program is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as published by
+ * the Free Software Foundation; either version 2.1 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.
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * GNU Lesser 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
  *****************************************************************************/
 
 /*****************************************************************************
@@ -208,7 +208,6 @@ static int Open( vlc_object_t * p_this )
     demux_sys_t *p_sys;
     const uint8_t *p_peek;
     frame_header_t fh;
-    bool  b_extended;
 
     /* Check id */
     if( stream_Peek( p_demux->s, &p_peek, 12 ) != 12 ||
@@ -250,7 +249,7 @@ static int Open( vlc_object_t * p_this )
     {
         if( fh.i_compression == 'F' || fh.i_compression == 'R' )
         {
-            /* ffmpeg extra data */
+            /* libavcodec extra data */
             p_sys->i_extra_f = fh.i_length;
             p_sys->p_extra_f = malloc( fh.i_length );
             if( p_sys->p_extra_f == NULL || stream_Read( p_demux->s,
@@ -270,8 +269,6 @@ static int Open( vlc_object_t * p_this )
         goto error;
     if( p_peek[0] == 'X' )
     {
-        b_extended = true;
-
         if( FrameHeaderLoad( p_demux, &fh ) )
             goto error;
         if( fh.i_length != 512 )
@@ -283,16 +280,16 @@ static int Open( vlc_object_t * p_this )
         if( !p_sys->b_seekable )
             msg_Warn( p_demux, "stream is not seekable, skipping seektable" );
         else if( SeekTableLoad( p_demux, p_sys ) )
-            goto error;
-
+        {
+            p_sys->b_index = false;
+            msg_Warn( p_demux, "Seektable is broken, seek won't be accurate" );
+        }
     }
     else
     {
-        b_extended = false;
-
         /* XXX: for now only file with extended chunk are supported
          * why: because else we need to have support for rtjpeg+stupid nuv shit */
-        msg_Err( p_demux, "incomplete NUV support (upload samples)" );
+        msg_Err( p_demux, "VLC doesn't support NUV without extended chunks (please upload samples)" );
         goto error;
     }
 
@@ -306,7 +303,8 @@ static int Open( vlc_object_t * p_this )
         fmt.video.i_height = p_sys->hdr.i_height;
         fmt.i_extra = p_sys->i_extra_f;
         fmt.p_extra = p_sys->p_extra_f;
-        fmt.video.i_aspect = VOUT_ASPECT_FACTOR * p_sys->hdr.d_aspect;
+        fmt.video.i_sar_num = p_sys->hdr.d_aspect * fmt.video.i_height;
+        fmt.video.i_sar_den = fmt.video.i_width;
 
         p_sys->p_es_video = es_out_Add( p_demux->out, &fmt );
     }
@@ -366,9 +364,6 @@ static int Demux( demux_t *p_demux )
 
     for( ;; )
     {
-        if( !vlc_object_alive (p_demux) )
-            return -1;
-
         if( FrameHeaderLoad( p_demux, &fh ) )
             return 0;
 
@@ -388,18 +383,20 @@ static int Demux( demux_t *p_demux )
     if( ( p_data = stream_Block( p_demux->s, fh.i_length ) ) == NULL )
         return 0;
 
-    p_data->i_dts = (int64_t)fh.i_timecode * 1000;
-    p_data->i_pts = (fh.i_type == 'V') ? 0 : p_data->i_dts;
+    p_data->i_dts = VLC_TS_0 + (int64_t)fh.i_timecode * 1000;
+    p_data->i_pts = (fh.i_type == 'V') ? VLC_TS_INVALID : p_data->i_dts;
 
     /* only add keyframes to index */
     if( !fh.i_keyframe && !p_sys->b_index )
-        demux_IndexAppend( &p_sys->idx, p_data->i_dts, stream_Tell(p_demux->s) - NUV_FH_SIZE );
+        demux_IndexAppend( &p_sys->idx,
+                           p_data->i_dts - VLC_TS_0,
+                           stream_Tell(p_demux->s) - NUV_FH_SIZE );
 
     /* */
-    if( p_data->i_dts > p_sys->i_pcr )
+    if( p_sys->i_pcr < 0 || p_sys->i_pcr < p_data->i_dts - VLC_TS_0 )
     {
-        p_sys->i_pcr = p_data->i_dts;
-        es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
+        p_sys->i_pcr = p_data->i_dts - VLC_TS_0;
+        es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + p_sys->i_pcr );
     }
 
     if( fh.i_type == 'A' && p_sys->p_es_audio )
@@ -418,6 +415,8 @@ static int Demux( demux_t *p_demux )
         {
             /* for rtjpeg data, the header is also needed */
             p_data = block_Realloc( p_data, NUV_FH_SIZE, fh.i_length );
+            if( unlikely(!p_data) )
+                abort();
             memcpy( p_data->p_buffer, p_sys->fh_buffer, NUV_FH_SIZE );
         }
         /* 0,1,2,3 -> rtjpeg, >=4 mpeg4 */
@@ -461,9 +460,14 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
             {
                 i64 = stream_Size( p_demux->s );
                 if( i64 > 0 )
-                    *pf = (double)stream_Tell( p_demux->s ) / (double)i64;
+                {
+                    const double f_current = stream_Tell( p_demux->s );
+                    *pf = f_current / (double)i64;
+                }
                 else
+                {
                     *pf = 0.0;
+                }
             }
             return VLC_SUCCESS;
 
@@ -566,7 +570,7 @@ static int ControlSetPosition( demux_t *p_demux, int64_t i_pos, bool b_guess )
         }
     }
 
-    while( vlc_object_alive (p_demux) )
+    for( ;; )
     {
         frame_header_t fh;
         int64_t i_tell;
@@ -723,9 +727,6 @@ static int SeekTableLoad( demux_t *p_demux, demux_sys_t *p_sys )
 {
     frame_header_t fh;
     int64_t i_original_pos;
-    uint8_t* p_seek_table;
-    uint8_t* p_kfa_table;
-    int32_t i_seek_elements = 0, i_kfa_elements = 0, j;
     int64_t i_time, i_offset;
     int keyframe, last_keyframe = 0, frame = 0, kfa_entry_id = 0;
 
@@ -745,29 +746,29 @@ static int SeekTableLoad( demux_t *p_demux, demux_sys_t *p_sys )
     if( FrameHeaderLoad( p_demux, &fh ) )
         return VLC_EGENERIC;
 
-    if( fh.i_type == 'Q' )
-    {
-        p_seek_table = malloc( fh.i_length );
-        if( p_seek_table == NULL )
-            return VLC_ENOMEM;
-
-        if( stream_Read( p_demux->s, p_seek_table, fh.i_length ) != fh.i_length )
-        {
-            free( p_seek_table );
-            return VLC_EGENERIC;
-        }
-
-        i_seek_elements = fh.i_length / 12;
-    }
-    else
+    if( fh.i_type != 'Q' )
     {
         msg_Warn( p_demux, "invalid seektable, frame type=%c", fh.i_type );
         stream_Seek( p_demux->s, i_original_pos );
         return VLC_EGENERIC;
     }
 
+    /* */
+    uint8_t *p_seek_table = malloc( fh.i_length );
+    if( p_seek_table == NULL )
+        return VLC_ENOMEM;
+
+    if( stream_Read( p_demux->s, p_seek_table, fh.i_length ) != fh.i_length )
+    {
+        free( p_seek_table );
+        return VLC_EGENERIC;
+    }
+    const int32_t i_seek_elements = fh.i_length / 12;
 
     /* Get keyframe adjust offsets */
+    int32_t i_kfa_elements = 0;
+    uint8_t *p_kfa_table = NULL;
+
     if( p_sys->exh.i_keyframe_adjust_offset > 0 )
     {
         msg_Dbg( p_demux, "seeking in stream to %"PRIi64, p_sys->exh.i_keyframe_adjust_offset );
@@ -804,11 +805,10 @@ static int SeekTableLoad( demux_t *p_demux, demux_sys_t *p_sys )
         }
     }
 
-
     if( i_kfa_elements > 0 )
         msg_Warn( p_demux, "untested keyframe adjust support, upload samples" );
 
-    for(j=0; j < i_seek_elements; j++)
+    for( int32_t j = 0; j < i_seek_elements; j++)
     {
 #if 0
         uint8_t* p = p_seek_table + j * 12;
@@ -917,8 +917,8 @@ static void demux_IndexAppend( demux_index_t *p_idx,
         else
         {
             p_idx->i_idx_max += 1000;
-            p_idx->idx = realloc( p_idx->idx,
-                                  p_idx->i_idx_max*sizeof(demux_index_entry_t));
+            p_idx->idx = xrealloc( p_idx->idx,
+                                p_idx->i_idx_max*sizeof(demux_index_entry_t));
         }
     }