rawvid.c 13.7 KB
Newer Older
1 2 3
/*****************************************************************************
 * rawvid.c : raw video input module for vlc
 *****************************************************************************
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
4
 * Copyright (C) 2007 VLC authors and VideoLAN
Christophe Mutricy's avatar
Christophe Mutricy committed
5
 * $Id$
6 7
 *
 * Authors: Gildas Bazin <gbazin@videolan.org>
8
 *          Antoine Cellerier <dionoea at videolan d.t org>
9
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
10 11 12
 * 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
13 14 15 16
 * (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
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
17 18
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
19
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
20 21 22
 * 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.
23 24 25 26 27 28
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

29 30 31 32
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

33
#include <vlc_common.h>
34
#include <vlc_plugin.h>
35 36 37 38 39 40 41 42 43 44
#include <vlc_demux.h>

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );

#define FPS_TEXT N_("Frames per Second")
#define FPS_LONGTEXT N_("This is the desired frame rate when " \
45
    "playing raw video streams. In the form 30000/1001 or 29.97")
46 47 48 49 50 51 52 53 54

#define WIDTH_TEXT N_("Width")
#define WIDTH_LONGTEXT N_("This specifies the width in pixels of the raw " \
    "video stream.")

#define HEIGHT_TEXT N_("Height")
#define HEIGHT_LONGTEXT N_("This specifies the height in pixels of the raw " \
    "video stream.")

55 56 57 58 59
#define CHROMA_TEXT N_("Force chroma (Use carefully)")
#define CHROMA_LONGTEXT N_("Force chroma. This is a four character string.")

#define ASPECT_RATIO_TEXT N_("Aspect ratio")
#define ASPECT_RATIO_LONGTEXT N_( \
60
    "Aspect ratio (4:3, 16:9). Default assumes square pixels." )
61

62 63 64 65 66 67 68 69
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" )
70
    add_string( "rawvid-fps", NULL, FPS_TEXT, FPS_LONGTEXT, false )
71 72
    add_integer( "rawvid-width", 0, WIDTH_TEXT, WIDTH_LONGTEXT, 0 )
    add_integer( "rawvid-height", 0, HEIGHT_TEXT, HEIGHT_LONGTEXT, 0 )
73
    add_string( "rawvid-chroma", NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
74
                true )
75
    add_string( "rawvid-aspect-ratio", NULL,
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
76
                ASPECT_RATIO_TEXT, ASPECT_RATIO_LONGTEXT, true )
77
vlc_module_end ()
78 79 80 81 82 83 84 85 86 87 88

/*****************************************************************************
 * Definitions of structures used by this plugin
 *****************************************************************************/
struct demux_sys_t
{
    int    frame_size;

    es_out_id_t *p_es_video;
    es_format_t  fmt_video;

89
    date_t pcr;
90

91
    bool b_y4m;
92 93 94 95 96 97 98 99
};

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int Demux( demux_t * );
static int Control( demux_t *, int i_query, va_list args );

100 101 102 103 104
struct preset_t
{
    const char *psz_ext;
    int i_width;
    int i_height;
105 106 107 108
    unsigned u_fps_num;
    unsigned u_fps_den;
    unsigned u_ar_num;
    unsigned u_ar_den;
109
    vlc_fourcc_t i_chroma;
110 111
};

112
static const struct preset_t p_presets[] =
113
{
114 115 116 117 118 119
    { "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 },
120
    { NULL, 0, 0, 0, 0, 0,0, 0 }
121 122
};

123 124 125 126 127 128 129
/*****************************************************************************
 * Open: initializes raw DV demux structures
 *****************************************************************************/
static int Open( vlc_object_t * p_this )
{
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys;
130
    int i_width=-1, i_height=-1;
131
    unsigned u_fps_num, u_fps_den;
132
    vlc_fourcc_t i_chroma = 0;
133 134
    unsigned int i_sar_num;
    unsigned int i_sar_den;
135
    const struct preset_t *p_preset = NULL;
136
    const uint8_t *p_peek;
137
    bool b_y4m = false;
138

139
    if( vlc_stream_Peek( p_demux->s, &p_peek, 9 ) == 9 )
140 141 142 143
    {
        /* http://wiki.multimedia.cx/index.php?title=YUV4MPEG2 */
        if( !strncmp( (char *)p_peek, "YUV4MPEG2", 9 ) )
        {
144
            b_y4m = true;
145
            goto valid;
146 147
        }
    }
148

149
    if( !p_demux->obj.force )
150
    {
151
        /* guess preset based on file extension */
152
        if( !p_demux->psz_filepath )
153 154
            return VLC_EGENERIC;

155
        const char *psz_ext = strrchr( p_demux->psz_filepath, '.' );
156 157
        if( !psz_ext )
            return VLC_EGENERIC;
158
        psz_ext++;
159 160

        for( unsigned i = 0; p_presets[i].psz_ext ; i++ )
161 162
        {
            if( !strcasecmp( psz_ext, p_presets[i].psz_ext ) )
163
            {
164
                p_preset = &p_presets[i];
165
                goto valid;
166
            }
167
        }
168
        return VLC_EGENERIC;
169 170
    }
valid:
171
    p_demux->p_sys      = p_sys = malloc( sizeof( demux_sys_t ) );
172
    if( !p_sys )
173
        return VLC_ENOMEM;
174

175
    p_sys->b_y4m = b_y4m;
176

177 178 179 180 181 182 183
    /* 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;
184 185
        i_sar_num = p_preset->u_ar_num * p_preset->i_height;
        i_sar_den = p_preset->u_ar_den * p_preset->i_width;
186 187 188 189
        i_chroma = p_preset->i_chroma;
    }

    /* override presets if yuv4mpeg2 */
190 191
    if( b_y4m )
    {
192
        /* The string should start with "YUV4MPEG2" */
193
        char *psz = vlc_stream_ReadLine( p_demux->s );
194
        char *psz_buf;
195 196
        int a = 1;
        int b = 1;
197

198 199
        if( unlikely(psz == NULL) )
            goto error;
200

201 202
        /* NB, it is not possible to handle interlaced here, since the
         * interlaced picture flags are in picture_t not block_t */
203

204 205 206
#define READ_FRAC( key, num, den ) do { \
        psz_buf = strstr( psz+9, key );\
        if( psz_buf )\
207
        {\
208
            char *end = strchr( psz_buf+1, ' ' );\
209 210
            char *sep;\
            if( end ) *end = '\0';\
211
            sep = strchr( psz_buf+1, ':' );\
212 213 214 215 216 217 218 219 220
            if( sep )\
            {\
                *sep = '\0';\
                den = atoi( sep+1 );\
            }\
            else\
            {\
                den = 1;\
            }\
221
            num = atoi( psz_buf+2 );\
222 223
            if( sep ) *sep = ':';\
            if( end ) *end = ' ';\
224 225 226 227 228 229
        } } 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
230
        if( b != 0 )
231 232 233 234
        {
            i_sar_num = a;
            i_sar_den = b;
        }
235

236 237
        psz_buf = strstr( psz+9, " C" );
        if( psz_buf )
238
        {
239 240
            static const struct { const char *psz_name; vlc_fourcc_t i_fcc; } formats[] =
            {
241 242 243 244 245 246
                { "420jpeg",    VLC_CODEC_I420 },
                { "420paldv",   VLC_CODEC_I420 },
                { "420",        VLC_CODEC_I420 },
                { "422",        VLC_CODEC_I422 },
                { "444",        VLC_CODEC_I444 },
                { "mono",       VLC_CODEC_GREY },
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262
                { 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++ )
            {
                if( !strncmp( psz_buf, formats[i].psz_name, strlen(formats[i].psz_name) ) )
                {
                    i_chroma = formats[i].i_fcc;
                    b_found = true;
                    break;
                }
263
            }
264 265 266 267
            if( !b_found )
                msg_Warn( p_demux, "Unknown YUV4MPEG2 chroma type \"%s\"", psz_buf );
            if( psz_end )
                *psz_end = ' ';
268 269 270 271 272
        }

        free( psz );
    }

273 274 275 276 277 278 279 280 281
    /* 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;
282
    psz_tmp = var_CreateGetNonEmptyString( p_demux, "rawvid-chroma" );
283
    if( psz_tmp )
284
    {
285 286 287 288 289 290 291 292 293 294 295 296
        if( strlen( psz_tmp ) != 4 )
        {
            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 );
    }

297
    if( var_InheritURational( p_demux, &u_fps_num, &u_fps_den, "rawvid-fps" ) )
298
    {
299 300
        u_fps_num = 0;
        u_fps_den = 1;
301 302
    }

303 304 305
    if( var_InheritURational( p_demux, &i_sar_num, &i_sar_den,
                              "rawvid-aspect-ratio" ) )
        i_sar_num = i_sar_den = 1;
306

307 308
    /* moan about anything wrong */
    if( i_width <= 0 || i_height <= 0 )
309
    {
310 311
        msg_Err( p_demux, "width and height must be strictly positive." );
        goto error;
312 313
    }

314
    if( !u_fps_num || !u_fps_den )
315
    {
316 317
        msg_Err( p_demux, "invalid or no framerate specified." );
        goto error;
318
    }
319

320 321 322 323 324 325
    if( i_chroma == 0 )
    {
        msg_Err( p_demux, "invalid or no chroma specified." );
        goto error;
    }

326
    /* fixup anything missing with sensible assumptions */
327
    if( i_sar_num <= 0 || i_sar_den <= 0 )
328
    {
329
        /* assume 1:1 sar */
330 331
        i_sar_num = 1;
        i_sar_den = 1;
332 333 334
    }

    es_format_Init( &p_sys->fmt_video, VIDEO_ES, i_chroma );
335 336
    video_format_Setup( &p_sys->fmt_video.video, i_chroma,
                        i_width, i_height, i_width, i_height,
337
                        i_sar_num, i_sar_den );
338 339 340 341 342 343

    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 );
344
    date_Set( &p_sys->pcr, 0 );
345

346 347 348 349
    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 );
350
        goto error;
351 352 353
    }
    p_sys->frame_size = i_width * i_height
                        * p_sys->fmt_video.video.i_bits_per_pixel / 8;
354 355
    p_sys->p_es_video = es_out_Add( p_demux->out, &p_sys->fmt_video );

356 357
    p_demux->pf_demux   = Demux;
    p_demux->pf_control = Control;
358
    return VLC_SUCCESS;
359 360

error:
Rémi Duraffort's avatar
Rémi Duraffort committed
361
    free( p_sys );
362
    return VLC_EGENERIC;
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383
}

/*****************************************************************************
 * Close: frees unused data
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys  = p_demux->p_sys;
    free( p_sys );
}

/*****************************************************************************
 * Demux: reads and demuxes data packets
 *****************************************************************************
 * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
 *****************************************************************************/
static int Demux( demux_t *p_demux )
{
    demux_sys_t *p_sys  = p_demux->p_sys;
    block_t     *p_block;
384
    mtime_t i_pcr = date_Get( &p_sys->pcr );
385 386

    /* Call the pace control */
387
    es_out_SetPCR( p_demux->out, VLC_TS_0 + i_pcr );
388

389 390 391
    if( p_sys->b_y4m )
    {
        /* Skip the frame header */
392
        /* Skip "FRAME" */
393
        if( vlc_stream_Read( p_demux->s, NULL, 5 ) < 5 )
394 395 396
            return 0;
        /* Find \n */
        for( ;; )
397
        {
398
            uint8_t b;
399
            if( vlc_stream_Read( p_demux->s, &b, 1 ) < 1 )
400
                return 0;
401 402
            if( b == 0x0a )
                break;
403 404 405
        }
    }

406 407
    p_block = vlc_stream_Block( p_demux->s, p_sys->frame_size );
    if( p_block == NULL )
408 409 410 411 412
    {
        /* EOF */
        return 0;
    }

413
    p_block->i_dts = p_block->i_pts = VLC_TS_0 + i_pcr;
414 415
    es_out_Send( p_demux->out, p_sys->p_es_video, p_block );

416
    date_Increment( &p_sys->pcr, 1 );
417 418 419 420 421 422 423 424 425 426 427

    return 1;
}

/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( demux_t *p_demux, int i_query, va_list args )
{
    demux_sys_t *p_sys  = p_demux->p_sys;

428 429 430
     /* (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;
431

432
    /* XXX: DEMUX_SET_TIME is precise here */
433
    return demux_vaControlHelper( p_demux->s, 0, -1, i_bps,
434 435
                                   p_sys->frame_size, i_query, args );
}
436