]> git.sesse.net Git - vlc/blobdiff - modules/demux/rawvid.c
demux: ts: fix program cross PCR regression
[vlc] / modules / demux / rawvid.c
index 8a127275a0c7a90d0e404e58458ed5a47e6f22a1..a69b0b3572c7d2ba047d46eba9d95bec4f10e6c4 100644 (file)
@@ -1,25 +1,25 @@
 /*****************************************************************************
  * rawvid.c : raw video input module for vlc
  *****************************************************************************
- * Copyright (C) 2007 the VideoLAN team
+ * Copyright (C) 2007 VLC authors and VideoLAN
  * $Id$
  *
  * Authors: Gildas Bazin <gbazin@videolan.org>
  *          Antoine Cellerier <dionoea at videolan d.t org>
  *
- * 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.
  *****************************************************************************/
 
 /*****************************************************************************
@@ -33,7 +33,6 @@
 #include <vlc_common.h>
 #include <vlc_plugin.h>
 #include <vlc_demux.h>
-#include <vlc_vout.h>                                     /* vout_InitFormat */
 #include <assert.h>
 
 /*****************************************************************************
@@ -44,7 +43,7 @@ static void Close( vlc_object_t * );
 
 #define FPS_TEXT N_("Frames per Second")
 #define FPS_LONGTEXT N_("This is the desired frame rate when " \
-    "playing raw video streams.  In the form 30000/1001 or 29.97")
+    "playing raw video streams. In the form 30000/1001 or 29.97")
 
 #define WIDTH_TEXT N_("Width")
 #define WIDTH_LONGTEXT N_("This specifies the width in pixels of the raw " \
@@ -69,12 +68,12 @@ vlc_module_begin ()
     set_subcategory( SUBCAT_INPUT_DEMUX )
     set_callbacks( Open, Close )
     add_shortcut( "rawvideo" )
-    add_string( "rawvid-fps", NULL, NULL, FPS_TEXT, FPS_LONGTEXT, false )
-    add_integer( "rawvid-width", 0, 0, WIDTH_TEXT, WIDTH_LONGTEXT, 0 )
-    add_integer( "rawvid-height", 0, 0, HEIGHT_TEXT, HEIGHT_LONGTEXT, 0 )
-    add_string( "rawvid-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
+    add_string( "rawvid-fps", NULL, FPS_TEXT, FPS_LONGTEXT, false )
+    add_integer( "rawvid-width", 0, WIDTH_TEXT, WIDTH_LONGTEXT, 0 )
+    add_integer( "rawvid-height", 0, HEIGHT_TEXT, HEIGHT_LONGTEXT, 0 )
+    add_string( "rawvid-chroma", NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
                 true )
-    add_string( "rawvid-aspect-ratio", NULL, NULL,
+    add_string( "rawvid-aspect-ratio", NULL,
                 ASPECT_RATIO_TEXT, ASPECT_RATIO_LONGTEXT, true )
 vlc_module_end ()
 
@@ -130,13 +129,12 @@ static int Open( vlc_object_t * p_this )
     demux_t     *p_demux = (demux_t*)p_this;
     demux_sys_t *p_sys;
     int i_width=-1, i_height=-1;
-    unsigned u_fps_num=0, u_fps_den=1;
-    char *psz_ext;
-    vlc_fourcc_t i_chroma;
-    unsigned int i_aspect = 0;
+    unsigned u_fps_num, u_fps_den;
+    vlc_fourcc_t i_chroma = 0;
+    unsigned int i_sar_num;
+    unsigned int i_sar_den;
     const struct preset_t *p_preset = NULL;
     const uint8_t *p_peek;
-    bool b_valid = false;
     bool b_y4m = false;
 
     if( stream_Peek( p_demux->s, &p_peek, 9 ) == 9 )
@@ -144,32 +142,33 @@ static int Open( vlc_object_t * p_this )
         /* http://wiki.multimedia.cx/index.php?title=YUV4MPEG2 */
         if( !strncmp( (char *)p_peek, "YUV4MPEG2", 9 ) )
         {
-            b_valid = true;
             b_y4m = true;
+            goto valid;
         }
     }
 
-    /* guess preset based on file extension */
-    psz_ext = strrchr( p_demux->psz_path, '.' );
-    if( psz_ext )
+    if( !p_demux->b_force )
     {
+        /* guess preset based on file extension */
+        if( !p_demux->psz_file )
+            return VLC_EGENERIC;
+
+        const char *psz_ext = strrchr( p_demux->psz_file, '.' );
+        if( !psz_ext )
+            return VLC_EGENERIC;
         psz_ext++;
-        for( int i = 0; p_presets[i].psz_ext ; i++ )
+
+        for( unsigned i = 0; p_presets[i].psz_ext ; i++ )
         {
             if( !strcasecmp( psz_ext, p_presets[i].psz_ext ) )
             {
                 p_preset = &p_presets[i];
-                b_valid = true;
-                break;
+                goto valid;
             }
         }
-    }
-    if( !b_valid && !p_demux->b_force )
         return VLC_EGENERIC;
-
-    /* Set p_input field */
-    p_demux->pf_demux   = Demux;
-    p_demux->pf_control = Control;
+    }
+valid:
     p_demux->p_sys      = p_sys = malloc( sizeof( demux_sys_t ) );
     if( !p_sys )
         return VLC_ENOMEM;
@@ -183,20 +182,22 @@ static int Open( vlc_object_t * p_this )
         i_height = p_preset->i_height;
         u_fps_num = p_preset->u_fps_num;
         u_fps_den = p_preset->u_fps_den;
-        i_aspect = VOUT_ASPECT_FACTOR * p_preset->u_ar_num / p_preset->u_ar_den;
+        i_sar_num = p_preset->u_ar_num * p_preset->i_height;
+        i_sar_den = p_preset->u_ar_den * p_preset->i_width;
         i_chroma = p_preset->i_chroma;
     }
 
     /* override presets if yuv4mpeg2 */
     if( b_y4m )
     {
+        /* The string should start with "YUV4MPEG2" */
         char *psz = stream_ReadLine( p_demux->s );
         char *psz_buf;
         int a = 1;
         int b = 1;
 
-        /* The string will start with "YUV4MPEG2" */
-        assert( strlen(psz) >= 9 );
+        if( unlikely(psz == NULL) )
+            goto error;
 
         /* NB, it is not possible to handle interlaced here, since the
          * interlaced picture flags are in picture_t not block_t */
@@ -227,11 +228,11 @@ static int Open( vlc_object_t * p_this )
         READ_FRAC( " F", u_fps_num, u_fps_den );
         READ_FRAC( " A", a, b );
 #undef READ_FRAC
-        /* Try to calculate aspect ratio here, rather than store ratio
-         * in u_ar_{num,den}, since width may be overridden by then.
-         * Plus, a:b is sar. */
         if( b != 0 )
-            i_aspect = VOUT_ASPECT_FACTOR * a * i_width / (b * i_height);
+        {
+            i_sar_num = a;
+            i_sar_den = b;
+        }
 
         psz_buf = strstr( psz+9, " C" );
         if( psz_buf )
@@ -294,47 +295,15 @@ static int Open( vlc_object_t * p_this )
         free( psz_tmp );
     }
 
-    psz_tmp = var_CreateGetNonEmptyString( p_demux, "rawvid-fps" );
-    if( psz_tmp )
+    if( var_InheritURational( p_demux, &u_fps_num, &u_fps_den, "rawvid-fps" ) )
     {
-        char *p_ptr;
-        /* fps can either be n/d or q.f
-         * for accuracy, avoid representation in float */
-        u_fps_num = strtol( psz_tmp, &p_ptr, 10 );
-        if( *p_ptr == '/' )
-        {
-            p_ptr++;
-            u_fps_den = strtol( p_ptr, NULL, 10 );
-        }
-        else if( *p_ptr == '.' )
-        {
-            char *p_end;
-            p_ptr++;
-            int i_frac =  strtol( p_ptr, &p_end, 10 );
-            u_fps_den = (p_end - p_ptr) * 10;
-            if( !u_fps_den )
-                u_fps_den = 1;
-            u_fps_num = u_fps_num * u_fps_den + i_frac;
-        }
-        else if( *p_ptr == '\0')
-        {
-            u_fps_den = 1;
-        }
-        free( psz_tmp );
+        u_fps_num = 0;
+        u_fps_den = 1;
     }
 
-    psz_tmp = var_CreateGetNonEmptyString( p_demux, "rawvid-aspect-ratio" );
-    if( psz_tmp )
-    {
-        char *psz_denominator = strchr( psz_tmp, ':' );
-        if( psz_denominator )
-        {
-            *psz_denominator++ = '\0';
-            i_aspect = atoi( psz_tmp ) * VOUT_ASPECT_FACTOR
-                     / atoi( psz_denominator );
-        }
-        free( psz_tmp );
-    }
+    if( var_InheritURational( p_demux, &i_sar_num, &i_sar_den,
+                              "rawvid-aspect-ratio" ) )
+        i_sar_num = i_sar_den = 1;
 
     /* moan about anything wrong */
     if( i_width <= 0 || i_height <= 0 )
@@ -349,23 +318,31 @@ static int Open( vlc_object_t * p_this )
         goto error;
     }
 
+    if( i_chroma == 0 )
+    {
+        msg_Err( p_demux, "invalid or no chroma specified." );
+        goto error;
+    }
+
     /* fixup anything missing with sensible assumptions */
-    if( !i_aspect )
+    if( i_sar_num <= 0 || i_sar_den <= 0 )
     {
         /* assume 1:1 sar */
-        i_aspect = i_width * VOUT_ASPECT_FACTOR / i_height;
+        i_sar_num = 1;
+        i_sar_den = 1;
     }
 
     es_format_Init( &p_sys->fmt_video, VIDEO_ES, i_chroma );
-    vout_InitFormat( &p_sys->fmt_video.video, i_chroma, i_width, i_height,
-                     i_aspect );
+    video_format_Setup( &p_sys->fmt_video.video, i_chroma,
+                        i_width, i_height, i_width, i_height,
+                        i_sar_num, i_sar_den );
 
     vlc_ureduce( &p_sys->fmt_video.video.i_frame_rate,
                  &p_sys->fmt_video.video.i_frame_rate_base,
                  u_fps_num, u_fps_den, 0);
     date_Init( &p_sys->pcr, p_sys->fmt_video.video.i_frame_rate,
                p_sys->fmt_video.video.i_frame_rate_base );
-    date_Set( &p_sys->pcr, 1 );
+    date_Set( &p_sys->pcr, 0 );
 
     if( !p_sys->fmt_video.video.i_bits_per_pixel )
     {
@@ -377,9 +354,12 @@ static int Open( vlc_object_t * p_this )
                         * p_sys->fmt_video.video.i_bits_per_pixel / 8;
     p_sys->p_es_video = es_out_Add( p_demux->out, &p_sys->fmt_video );
 
+    p_demux->pf_demux   = Demux;
+    p_demux->pf_control = Control;
     return VLC_SUCCESS;
 
 error:
+    stream_Seek( p_demux->s, 0 ); // Workaround, but y4m uses stream_ReadLines
     free( p_sys );
     return VLC_EGENERIC;
 }
@@ -406,7 +386,7 @@ static int Demux( demux_t *p_demux )
     mtime_t i_pcr = date_Get( &p_sys->pcr );
 
     /* Call the pace control */
-    es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_pcr );
+    es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + i_pcr );
 
     if( p_sys->b_y4m )
     {
@@ -431,7 +411,7 @@ static int Demux( demux_t *p_demux )
         return 0;
     }
 
-    p_block->i_dts = p_block->i_pts = i_pcr;
+    p_block->i_dts = p_block->i_pts = VLC_TS_0 + i_pcr;
     es_out_Send( p_demux->out, p_sys->p_es_video, p_block );
 
     date_Increment( &p_sys->pcr, 1 );