]> git.sesse.net Git - vlc/blob - modules/demux/rawvid.c
* modules/demux/rawvid.c: raw video demuxer (only does YV12 for now).
[vlc] / modules / demux / rawvid.c
1 /*****************************************************************************
2  * rawvid.c : raw video input module for vlc
3  *****************************************************************************
4  * Copyright (C) 2007 the VideoLAN team
5  * $Id: rawdv.c 18062 2006-11-26 14:20:34Z zorglub $
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc_demux.h>
31
32 /*****************************************************************************
33  * Module descriptor
34  *****************************************************************************/
35 static int  Open ( vlc_object_t * );
36 static void Close( vlc_object_t * );
37
38 #define FPS_TEXT N_("Frames per Second")
39 #define FPS_LONGTEXT N_("This is the desired frame rate when " \
40     "playing raw video streams.")
41
42 #define WIDTH_TEXT N_("Width")
43 #define WIDTH_LONGTEXT N_("This specifies the width in pixels of the raw " \
44     "video stream.")
45
46 #define HEIGHT_TEXT N_("Height")
47 #define HEIGHT_LONGTEXT N_("This specifies the height in pixels of the raw " \
48     "video stream.")
49
50 vlc_module_begin();
51     set_shortname( "Raw Video" );
52     set_description( _("Raw video demuxer") );
53     set_capability( "demux2", 2 );
54     set_category( CAT_INPUT );
55     set_subcategory( SUBCAT_INPUT_DEMUX );
56     set_callbacks( Open, Close );
57     add_shortcut( "rawvideo" );
58     add_float( "rawvid-fps", 25, 0, FPS_TEXT, FPS_LONGTEXT, VLC_FALSE );
59     add_integer( "rawvid-width", 176, 0, WIDTH_TEXT, WIDTH_LONGTEXT, 0 );
60     add_integer( "rawvid-height", 144, 0, HEIGHT_TEXT, HEIGHT_LONGTEXT, 0 );
61 vlc_module_end();
62
63 /*****************************************************************************
64  * Definitions of structures used by this plugin
65  *****************************************************************************/
66 struct demux_sys_t
67 {
68     int    frame_size;
69     float  f_fps;
70
71     es_out_id_t *p_es_video;
72     es_format_t  fmt_video;
73
74     mtime_t i_pcr;
75 };
76
77 /*****************************************************************************
78  * Local prototypes
79  *****************************************************************************/
80 static int Demux( demux_t * );
81 static int Control( demux_t *, int i_query, va_list args );
82
83 /*****************************************************************************
84  * Open: initializes raw DV demux structures
85  *****************************************************************************/
86 static int Open( vlc_object_t * p_this )
87 {
88     demux_t     *p_demux = (demux_t*)p_this;
89     demux_sys_t *p_sys;
90     int i_width, i_height;
91     vlc_value_t val;
92     char *psz_ext;
93
94     /* Check for YUV file extension */
95     psz_ext = strrchr( p_demux->psz_path, '.' );
96     if( ( !psz_ext || strcasecmp( psz_ext, ".yuv") ) &&
97         strcmp(p_demux->psz_demux, "rawvid") )
98     {
99         return VLC_EGENERIC;
100     }
101
102     /* Set p_input field */
103     p_demux->pf_demux   = Demux;
104     p_demux->pf_control = Control;
105     p_demux->p_sys      = p_sys = malloc( sizeof( demux_sys_t ) );
106     p_sys->i_pcr = 1;
107
108     var_Create( p_demux, "rawvid-fps", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT );
109     var_Get( p_demux, "rawvid-fps", &val );
110     p_sys->f_fps = val.f_float;
111     var_Create( p_demux, "rawvid-width", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
112     var_Get( p_demux, "rawvid-width", &val );
113     i_width = val.i_int;
114     var_Create( p_demux, "rawvid-height", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT);
115     var_Get( p_demux, "rawvid-height", &val );
116     i_height = val.i_int;
117
118     /* Only handle YV12 for now */
119     es_format_Init( &p_sys->fmt_video, VIDEO_ES, VLC_FOURCC('Y','V','1','2') );
120     p_sys->fmt_video.video.i_width  = i_width;
121     p_sys->fmt_video.video.i_height = i_height;
122     p_sys->frame_size = i_width * i_height * 3 / 2;
123     p_sys->p_es_video = es_out_Add( p_demux->out, &p_sys->fmt_video );
124
125     return VLC_SUCCESS;
126 }
127
128 /*****************************************************************************
129  * Close: frees unused data
130  *****************************************************************************/
131 static void Close( vlc_object_t *p_this )
132 {
133     demux_t     *p_demux = (demux_t*)p_this;
134     demux_sys_t *p_sys  = p_demux->p_sys;
135     free( p_sys );
136 }
137
138 /*****************************************************************************
139  * Demux: reads and demuxes data packets
140  *****************************************************************************
141  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
142  *****************************************************************************/
143 static int Demux( demux_t *p_demux )
144 {
145     demux_sys_t *p_sys  = p_demux->p_sys;
146     block_t     *p_block;
147
148     /* Call the pace control */
149     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
150
151     if( ( p_block = stream_Block( p_demux->s, p_sys->frame_size ) ) == NULL )
152     {
153         /* EOF */
154         return 0;
155     }
156
157     p_block->i_dts = p_block->i_pts = p_sys->i_pcr;
158     es_out_Send( p_demux->out, p_sys->p_es_video, p_block );
159
160     p_sys->i_pcr += ( I64C(1000000) / p_sys->f_fps );
161
162     return 1;
163 }
164
165 /*****************************************************************************
166  * Control:
167  *****************************************************************************/
168 static int Control( demux_t *p_demux, int i_query, va_list args )
169 {
170     demux_sys_t *p_sys  = p_demux->p_sys;
171
172     /* XXX: DEMUX_SET_TIME is precise here */
173     return demux2_vaControlHelper( p_demux->s, 0, -1,
174                                    p_sys->frame_size * p_sys->f_fps * 8,
175                                    p_sys->frame_size, i_query, args );
176 }