]> git.sesse.net Git - vlc/blobdiff - modules/demux/rawvid.c
Qt sprefs, cleaning of Audio and Interface parts
[vlc] / modules / demux / rawvid.c
index 7c59dc1d9197bf806b228ab235b84835dc4d0b25..fbc1bf40864584aa71a5b6c4880940b793bd2b1d 100644 (file)
 /*****************************************************************************
  * Preamble
  *****************************************************************************/
-#include <stdlib.h>                                      /* malloc(), free() */
 
-#include <vlc/vlc.h>
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
+#include <vlc_common.h>
+#include <vlc_plugin.h>
 #include <vlc_demux.h>
-#include <vlc_vout.h>                                     /* vout_InitFormat */
+#include <assert.h>
 
 /*****************************************************************************
  * Module descriptor
@@ -39,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.")
+    "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 " \
@@ -54,24 +58,24 @@ static void Close( vlc_object_t * );
 
 #define ASPECT_RATIO_TEXT N_("Aspect ratio")
 #define ASPECT_RATIO_LONGTEXT N_( \
-    "Aspect ratio (4:3, 16:9). Default is square pixels." )
-
-vlc_module_begin();
-    set_shortname( "Raw Video" );
-    set_description( _("Raw video demuxer") );
-    set_capability( "demux2", 3 );
-    set_category( CAT_INPUT );
-    set_subcategory( SUBCAT_INPUT_DEMUX );
-    set_callbacks( Open, Close );
-    add_shortcut( "rawvideo" );
-    add_float( "rawvid-fps", 0, 0, FPS_TEXT, FPS_LONGTEXT, VLC_FALSE );
-    add_integer( "rawvid-width", 0, 0, WIDTH_TEXT, WIDTH_LONGTEXT, 0 );
-    add_integer( "rawvid-height", 0, 0, HEIGHT_TEXT, HEIGHT_LONGTEXT, 0 );
+    "Aspect ratio (4:3, 16:9). Default assumes square pixels." )
+
+vlc_module_begin ()
+    set_shortname( "Raw Video" )
+    set_description( N_("Raw video demuxer") )
+    set_capability( "demux", 10 )
+    set_category( CAT_INPUT )
+    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,
-                VLC_TRUE );
+                true )
     add_string( "rawvid-aspect-ratio", NULL, NULL,
-                ASPECT_RATIO_TEXT, ASPECT_RATIO_LONGTEXT, VLC_TRUE );
-vlc_module_end();
+                ASPECT_RATIO_TEXT, ASPECT_RATIO_LONGTEXT, true )
+vlc_module_end ()
 
 /*****************************************************************************
  * Definitions of structures used by this plugin
@@ -79,14 +83,13 @@ vlc_module_end();
 struct demux_sys_t
 {
     int    frame_size;
-    float  f_fps;
 
     es_out_id_t *p_es_video;
     es_format_t  fmt_video;
 
-    mtime_t i_pcr;
+    date_t pcr;
 
-    vlc_bool_t b_y4m;
+    bool b_y4m;
 };
 
 /*****************************************************************************
@@ -100,20 +103,22 @@ struct preset_t
     const char *psz_ext;
     int i_width;
     int i_height;
-    double f_fps;
-    const char *psz_aspect_ratio;
-    const char *psz_chroma;
+    unsigned u_fps_num;
+    unsigned u_fps_den;
+    unsigned u_ar_num;
+    unsigned u_ar_den;
+    vlc_fourcc_t i_chroma;
 };
 
-static struct preset_t p_presets[] =
+static const struct preset_t p_presets[] =
 {
-    { "sqcif", 128, 96, 29.97, "4:3", "YV12" },
-    { "qcif", 176, 144, 29.97, "4:3", "YV12" },
-    { "cif", 352, 288, 29.97, "4:3", "YV12" },
-    { "4cif", 704, 576, 29.97, "4:3", "YV12" },
-    { "16cif", 1408, 1152, 29.97, "4:3", "YV12" },
-    { "yuv", 176, 144, 25, "4:3", "YV12" },
-    { "", 0, 0, 0., "", "" }
+    { "sqcif", 128, 96, 30000, 1001, 4,3, VLC_CODEC_YV12 },
+    { "qcif", 176, 144, 30000, 1001, 4,3, VLC_CODEC_YV12 },
+    { "cif", 352, 288, 30000, 1001, 4,3, VLC_CODEC_YV12 },
+    { "4cif", 704, 576, 30000, 1001, 4,3, VLC_CODEC_YV12 },
+    { "16cif", 1408, 1152, 30000, 1001, 4,3, VLC_CODEC_YV12 },
+    { "yuv", 176, 144, 25, 1, 4,3, VLC_CODEC_YV12 },
+    { NULL, 0, 0, 0, 0, 0,0, 0 }
 };
 
 /*****************************************************************************
@@ -123,73 +128,90 @@ static int Open( vlc_object_t * p_this )
 {
     demux_t     *p_demux = (demux_t*)p_this;
     demux_sys_t *p_sys;
-    int i_width, i_height;
-    char *psz_ext;
-    char *psz_chroma;
-    uint32_t i_chroma;
-    char *psz_aspect_ratio;
-    unsigned int i_aspect = 0;
-    struct preset_t *p_preset = NULL;
+    int i_width=-1, i_height=-1;
+    unsigned u_fps_num=0, u_fps_den=1;
+    vlc_fourcc_t i_chroma;
+    unsigned int i_sar_num = 0;
+    unsigned int i_sar_den = 0;
+    const struct preset_t *p_preset = NULL;
     const uint8_t *p_peek;
-    vlc_bool_t b_valid = VLC_FALSE;
-    vlc_bool_t b_y4m = VLC_FALSE;
+    bool b_y4m = false;
 
     if( stream_Peek( p_demux->s, &p_peek, 9 ) == 9 )
     {
         /* http://wiki.multimedia.cx/index.php?title=YUV4MPEG2 */
         if( !strncmp( (char *)p_peek, "YUV4MPEG2", 9 ) )
         {
-            b_valid = VLC_TRUE;
-            b_y4m = VLC_TRUE;
+            b_y4m = true;
+            goto valid;
         }
     }
 
-    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( p_preset = p_presets; *p_preset->psz_ext; p_preset++ )
-            if( !strcasecmp( psz_ext, p_preset->psz_ext ) )
+
+        for( unsigned i = 0; p_presets[i].psz_ext ; i++ )
+        {
+            if( !strcasecmp( psz_ext, p_presets[i].psz_ext ) )
             {
-                b_valid = VLC_TRUE;
-                break;
+                p_preset = &p_presets[i];
+                goto valid;
             }
-    }
-    if( ( !b_valid ) && strcmp(p_demux->psz_demux, "rawvid") )
-    {
+        }
         return VLC_EGENERIC;
     }
-
+valid:
     /* Set p_input field */
     p_demux->pf_demux   = Demux;
     p_demux->pf_control = Control;
     p_demux->p_sys      = p_sys = malloc( sizeof( demux_sys_t ) );
-    p_sys->i_pcr = 1;
+    if( !p_sys )
+        return VLC_ENOMEM;
 
     p_sys->b_y4m = b_y4m;
-    p_sys->f_fps = var_CreateGetFloat( p_demux, "rawvid-fps" );
-    i_width = var_CreateGetInteger( p_demux, "rawvid-width" );
-    i_height = var_CreateGetInteger( p_demux, "rawvid-height" );
-    psz_chroma = var_CreateGetString( p_demux, "rawvid-chroma" );
-    psz_aspect_ratio = var_CreateGetString( p_demux, "rawvid-aspect-ratio" );
 
+    /* guess the parameters based on the preset */
+    if( p_preset )
+    {
+        i_width = p_preset->i_width;
+        i_height = p_preset->i_height;
+        u_fps_num = p_preset->u_fps_num;
+        u_fps_den = p_preset->u_fps_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 )
     {
-        char *psz;
-        char *buf;
-        int a, b;
-        psz = stream_ReadLine( p_demux->s );
+        char *psz = stream_ReadLine( p_demux->s );
+        char *psz_buf;
+        int a = 1;
+        int b = 1;
 
-        /* TODO: handle interlacing */
+        /* The string will start with "YUV4MPEG2" */
+        assert( strlen(psz) >= 9 );
 
-#define READ_FRAC( key, num, den ) \
-        buf = strchr( psz+9, key );\
-        if( buf )\
+        /* NB, it is not possible to handle interlaced here, since the
+         * interlaced picture flags are in picture_t not block_t */
+
+#define READ_FRAC( key, num, den ) do { \
+        psz_buf = strstr( psz+9, key );\
+        if( psz_buf )\
         {\
-            char *end = strchr( buf, ' ' );\
+            char *end = strchr( psz_buf+1, ' ' );\
             char *sep;\
             if( end ) *end = '\0';\
-            sep = strchr( buf, ':' );\
+            sep = strchr( psz_buf+1, ':' );\
             if( sep )\
             {\
                 *sep = '\0';\
@@ -199,132 +221,173 @@ static int Open( vlc_object_t * p_this )
             {\
                 den = 1;\
             }\
-            num = atoi( buf+1 );\
+            num = atoi( psz_buf+2 );\
             if( sep ) *sep = ':';\
             if( end ) *end = ' ';\
+        } } while(0)
+        READ_FRAC( " W", i_width, a );
+        READ_FRAC( " H", i_height, a );
+        READ_FRAC( " F", u_fps_num, u_fps_den );
+        READ_FRAC( " A", a, b );
+#undef READ_FRAC
+        if( b != 0 )
+        {
+            i_sar_num = a;
+            i_sar_den = b;
         }
-        READ_FRAC( 'W', i_width, a )
-        READ_FRAC( 'H', i_height, a )
-        READ_FRAC( 'F', a, b )
-        p_sys->f_fps = (double)a/(double)b;
-        READ_FRAC( 'A', a, b )
-        if( b != 0 ) i_aspect = a * VOUT_ASPECT_FACTOR / b;
-
-        buf = strchr( psz+9, 'C' );
-        if( buf )
+
+        psz_buf = strstr( psz+9, " C" );
+        if( psz_buf )
         {
-            char *end = strchr( buf, ' ' );
-            if( end ) *end = '\0';
-            buf++;
-            if( !strncmp( buf, "C420jpeg", 8 ) )
+            static const struct { const char *psz_name; vlc_fourcc_t i_fcc; } formats[] =
             {
-                psz_chroma = strdup( "I420" );
-            }
-            else if( !strncmp( buf, "C420paldv", 9 ) )
-            {
-                psz_chroma = strdup( "I420" );
-            }
-            else if( !strncmp( buf, "C420", 4 ) )
-            {
-                psz_chroma = strdup( "I420" );
-            }
-            else if( !strncmp( buf, "C422", 4 ) )
-            {
-                psz_chroma = strdup( "I422" );
-            }
-            else if( !strncmp( buf, "C444", 4 ) )
+                { "420jpeg",    VLC_CODEC_I420 },
+                { "420paldv",   VLC_CODEC_I420 },
+                { "420",        VLC_CODEC_I420 },
+                { "422",        VLC_CODEC_I422 },
+                { "444",        VLC_CODEC_I444 },
+                { "mono",       VLC_CODEC_GREY },
+                { NULL, 0 }
+            };
+            bool b_found = false;
+            char *psz_end = strchr( psz_buf+1, ' ' );
+            if( psz_end )
+                *psz_end = '\0';
+            psz_buf += 2;
+
+            for( int i = 0; formats[i].psz_name != NULL; i++ )
             {
-                psz_chroma = strdup( "I444" );
+                if( !strncmp( psz_buf, formats[i].psz_name, strlen(formats[i].psz_name) ) )
+                {
+                    i_chroma = formats[i].i_fcc;
+                    b_found = true;
+                    break;
+                }
             }
-            else
-            {
-                msg_Warn( p_demux, "Unknown YUV4MPEG2 chroma type \"%s\"",
-                          buf );
-            }
-            if( end ) *end = ' ';
+            if( !b_found )
+                msg_Warn( p_demux, "Unknown YUV4MPEG2 chroma type \"%s\"", psz_buf );
+            if( psz_end )
+                *psz_end = ' ';
         }
 
         free( psz );
     }
 
-    if( p_preset && *p_preset->psz_ext )
+    /* allow the user to override anything guessed from the input */
+    int i_tmp;
+    i_tmp = var_CreateGetInteger( p_demux, "rawvid-width" );
+    if( i_tmp ) i_width = i_tmp;
+
+    i_tmp = var_CreateGetInteger( p_demux, "rawvid-height" );
+    if( i_tmp ) i_height = i_tmp;
+
+    char *psz_tmp;
+    psz_tmp = var_CreateGetNonEmptyString( p_demux, "rawvid-chroma" );
+    if( psz_tmp )
     {
-        if( !i_width ) i_width = p_preset->i_width;
-        if( !i_height ) i_height = p_preset->i_height;
-        if( !p_sys->f_fps ) p_sys->f_fps = p_preset->f_fps;
-        if( !*psz_aspect_ratio )
+        if( strlen( psz_tmp ) != 4 )
         {
-            free( psz_aspect_ratio );
-            psz_aspect_ratio = strdup( psz_aspect_ratio );
-        }
-        if( !*psz_chroma )
-        {
-            free( psz_chroma );
-            psz_chroma = strdup( psz_chroma );
+            msg_Err( p_demux, "Invalid fourcc format/chroma specification %s"
+                     " expecting four characters eg, UYVY", psz_tmp );
+            free( psz_tmp );
+            goto error;
         }
+        memcpy( &i_chroma, psz_tmp, 4 );
+        msg_Dbg( p_demux, "Forcing chroma to 0x%.8x (%4.4s)", i_chroma, (char*)&i_chroma );
+        free( psz_tmp );
     }
 
-    if( i_width <= 0 || i_height <= 0 )
+    psz_tmp = var_CreateGetNonEmptyString( p_demux, "rawvid-fps" );
+    if( psz_tmp )
     {
-        msg_Err( p_demux, "width and height must be strictly positive." );
-        free( psz_aspect_ratio );
-        free( psz_chroma );
-        free( p_sys );
-        return VLC_EGENERIC;
+        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 );
     }
 
-    if( !i_aspect )
+    psz_tmp = var_CreateGetNonEmptyString( p_demux, "rawvid-aspect-ratio" );
+    if( psz_tmp )
     {
-        if( psz_aspect_ratio && *psz_aspect_ratio )
+        char *psz_denominator = strchr( psz_tmp, ':' );
+        if( psz_denominator )
         {
-            char *psz_parser = strchr( psz_aspect_ratio, ':' );
-            if( psz_parser )
-            {
-                *psz_parser++ = '\0';
-                i_aspect = atoi( psz_aspect_ratio ) * VOUT_ASPECT_FACTOR
-                           / atoi( psz_parser );
-            }
-            else
-            {
-                i_aspect = atof( psz_aspect_ratio ) * VOUT_ASPECT_FACTOR;
-            }
-        }
-        else
-        {
-            i_aspect = i_width * VOUT_ASPECT_FACTOR / i_height;
+            *psz_denominator++ = '\0';
+            i_sar_num = atoi( psz_tmp )         * i_height;
+            i_sar_den = atoi( psz_denominator ) * i_width;
         }
+        free( psz_tmp );
     }
-    free( psz_aspect_ratio );
 
-    if( psz_chroma && strlen( psz_chroma ) >= 4 )
+    /* moan about anything wrong */
+    if( i_width <= 0 || i_height <= 0 )
     {
-        memcpy( &i_chroma, psz_chroma, 4 );
-        msg_Dbg( p_demux, "Forcing chroma to 0x%.8x (%4.4s)", i_chroma,
-                 (char*)&i_chroma );
+        msg_Err( p_demux, "width and height must be strictly positive." );
+        goto error;
     }
-    else
+
+    if( !u_fps_num || !u_fps_den )
     {
-        i_chroma = VLC_FOURCC('Y','V','1','2');
-        msg_Dbg( p_demux, "Using default chroma 0x%.8x (%4.4s)", i_chroma,
-                 (char*)&i_chroma );
+        msg_Err( p_demux, "invalid or no framerate specified." );
+        goto error;
+    }
+
+    /* fixup anything missing with sensible assumptions */
+    if( i_sar_num <= 0 || i_sar_den <= 0 )
+    {
+        /* assume 1:1 sar */
+        i_sar_num = 1;
+        i_sar_den = 1;
     }
-    free( psz_chroma );
 
     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_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, 0 );
+
     if( !p_sys->fmt_video.video.i_bits_per_pixel )
     {
         msg_Err( p_demux, "Unsupported chroma 0x%.8x (%4.4s)", i_chroma,
                  (char*)&i_chroma );
-        free( p_sys );
-        return VLC_EGENERIC;
+        goto error;
     }
     p_sys->frame_size = i_width * i_height
                         * 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 );
 
     return VLC_SUCCESS;
+
+error:
+    stream_Seek( p_demux->s, 0 ); // Workaround, but y4m uses stream_ReadLines
+    free( p_sys );
+    return VLC_EGENERIC;
 }
 
 /*****************************************************************************
@@ -346,20 +409,25 @@ static int Demux( demux_t *p_demux )
 {
     demux_sys_t *p_sys  = p_demux->p_sys;
     block_t     *p_block;
+    mtime_t i_pcr = date_Get( &p_sys->pcr );
 
     /* Call the pace control */
-    es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
+    es_out_Control( p_demux->out, ES_OUT_SET_PCR, VLC_TS_0 + i_pcr );
 
     if( p_sys->b_y4m )
     {
         /* Skip the frame header */
-        unsigned char psz_buf[10];
-        psz_buf[9] = '\0';
-        stream_Read( p_demux->s, psz_buf, strlen( "FRAME" ) );
-        while( psz_buf[0] != 0x0a )
+        /* Skip "FRAME" */
+        if( stream_Read( p_demux->s, NULL, 5 ) < 5 )
+            return 0;
+        /* Find \n */
+        for( ;; )
         {
-            if( stream_Read( p_demux->s, psz_buf, 1 ) < 1 )
+            uint8_t b;
+            if( stream_Read( p_demux->s, &b, 1 ) < 1 )
                 return 0;
+            if( b == 0x0a )
+                break;
         }
     }
 
@@ -369,10 +437,10 @@ static int Demux( demux_t *p_demux )
         return 0;
     }
 
-    p_block->i_dts = p_block->i_pts = p_sys->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 );
 
-    p_sys->i_pcr += ( I64C(1000000) / p_sys->f_fps );
+    date_Increment( &p_sys->pcr, 1 );
 
     return 1;
 }
@@ -384,8 +452,12 @@ static int Control( demux_t *p_demux, int i_query, va_list args )
 {
     demux_sys_t *p_sys  = p_demux->p_sys;
 
+     /* (2**31)-1 is insufficient to store 1080p50 4:4:4. */
+    const int64_t i_bps = 8LL * p_sys->frame_size * p_sys->pcr.i_divider_num /
+                                                    p_sys->pcr.i_divider_den;
+
     /* XXX: DEMUX_SET_TIME is precise here */
-    return demux2_vaControlHelper( p_demux->s, 0, -1,
-                                   p_sys->frame_size * p_sys->f_fps * 8,
+    return demux_vaControlHelper( p_demux->s, 0, -1, i_bps,
                                    p_sys->frame_size, i_query, args );
 }
+