]> git.sesse.net Git - vlc/blob - modules/demux/rawvid.c
Add aspect ratio and chroma option to the rawvid demux.
[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$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *          Antoine Cellerier <dionoea at videolan d.t org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #include <stdlib.h>                                      /* malloc(), free() */
29
30 #include <vlc/vlc.h>
31 #include <vlc_demux.h>
32 #include <vlc_vout.h>                                     /* vout_InitFormat */
33
34 /*****************************************************************************
35  * Module descriptor
36  *****************************************************************************/
37 static int  Open ( vlc_object_t * );
38 static void Close( vlc_object_t * );
39
40 #define FPS_TEXT N_("Frames per Second")
41 #define FPS_LONGTEXT N_("This is the desired frame rate when " \
42     "playing raw video streams.")
43
44 #define WIDTH_TEXT N_("Width")
45 #define WIDTH_LONGTEXT N_("This specifies the width in pixels of the raw " \
46     "video stream.")
47
48 #define HEIGHT_TEXT N_("Height")
49 #define HEIGHT_LONGTEXT N_("This specifies the height in pixels of the raw " \
50     "video stream.")
51
52 #define CHROMA_TEXT N_("Force chroma (Use carefully)")
53 #define CHROMA_LONGTEXT N_("Force chroma. This is a four character string.")
54
55 #define ASPECT_RATIO_TEXT N_("Aspect ratio")
56 #define ASPECT_RATIO_LONGTEXT N_( \
57     "Aspect ratio (4:3, 16:9). Default is square pixels." )
58
59 vlc_module_begin();
60     set_shortname( "Raw Video" );
61     set_description( _("Raw video demuxer") );
62     set_capability( "demux2", 2 );
63     set_category( CAT_INPUT );
64     set_subcategory( SUBCAT_INPUT_DEMUX );
65     set_callbacks( Open, Close );
66     add_shortcut( "rawvideo" );
67     add_float( "rawvid-fps", 25, 0, FPS_TEXT, FPS_LONGTEXT, VLC_FALSE );
68     add_integer( "rawvid-width", 176, 0, WIDTH_TEXT, WIDTH_LONGTEXT, 0 );
69     add_integer( "rawvid-height", 144, 0, HEIGHT_TEXT, HEIGHT_LONGTEXT, 0 );
70     add_string( "rawvid-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
71                 VLC_TRUE );
72     add_string( "rawvid-aspect-ratio", NULL, NULL,
73                 ASPECT_RATIO_TEXT, ASPECT_RATIO_LONGTEXT, VLC_TRUE );
74 vlc_module_end();
75
76 /*****************************************************************************
77  * Definitions of structures used by this plugin
78  *****************************************************************************/
79 struct demux_sys_t
80 {
81     int    frame_size;
82     float  f_fps;
83
84     es_out_id_t *p_es_video;
85     es_format_t  fmt_video;
86
87     mtime_t i_pcr;
88 };
89
90 /*****************************************************************************
91  * Local prototypes
92  *****************************************************************************/
93 static int Demux( demux_t * );
94 static int Control( demux_t *, int i_query, va_list args );
95
96 /*****************************************************************************
97  * Open: initializes raw DV demux structures
98  *****************************************************************************/
99 static int Open( vlc_object_t * p_this )
100 {
101     demux_t     *p_demux = (demux_t*)p_this;
102     demux_sys_t *p_sys;
103     int i_width, i_height;
104     char *psz_ext;
105     char *psz_chroma;
106     uint32_t i_chroma;
107     char *psz_aspect_ratio;
108     unsigned int i_aspect;
109
110     /* Check for YUV file extension */
111     psz_ext = strrchr( p_demux->psz_path, '.' );
112     if( ( !psz_ext || strcasecmp( psz_ext, ".yuv") ) &&
113         strcmp(p_demux->psz_demux, "rawvid") )
114     {
115         return VLC_EGENERIC;
116     }
117
118     /* Set p_input field */
119     p_demux->pf_demux   = Demux;
120     p_demux->pf_control = Control;
121     p_demux->p_sys      = p_sys = malloc( sizeof( demux_sys_t ) );
122     p_sys->i_pcr = 1;
123
124     p_sys->f_fps = var_CreateGetFloat( p_demux, "rawvid-fps" );
125
126     i_width = var_CreateGetInteger( p_demux, "rawvid-width" );
127     i_height = var_CreateGetInteger( p_demux, "rawvid-height" );
128     if( i_width <= 0 || i_height <= 0 )
129     {
130         msg_Err( p_demux, "width and height must be strictly positive." );
131         free( p_sys );
132         return VLC_EGENERIC;
133     }
134
135     psz_chroma = var_CreateGetString( p_demux, "rawvid-chroma" );
136     psz_aspect_ratio = var_CreateGetString( p_demux, "rawvid-aspect-ratio" );
137
138     if( psz_aspect_ratio && *psz_aspect_ratio )
139     {
140         char *psz_parser = strchr( psz_aspect_ratio, ':' );
141         if( psz_parser )
142         {
143             *psz_parser++ = '\0';
144             i_aspect = atoi( psz_aspect_ratio ) * VOUT_ASPECT_FACTOR
145                        / atoi( psz_parser );
146         }
147         else
148         {
149             i_aspect = atof( psz_aspect_ratio ) * VOUT_ASPECT_FACTOR;
150         }
151     }
152     else
153     {
154         i_aspect = i_width * VOUT_ASPECT_FACTOR / i_height;
155     }
156     free( psz_aspect_ratio );
157
158     if( psz_chroma && strlen( psz_chroma ) >= 4 )
159     {
160         memcpy( &i_chroma, psz_chroma, 4 );
161         msg_Dbg( p_demux, "Forcing chroma to 0x%.8x (%4.4s)", i_chroma,
162                  (char*)&i_chroma );
163     }
164     else
165     {
166         i_chroma = VLC_FOURCC('Y','V','1','2');
167         msg_Dbg( p_demux, "Using default chroma 0x%.8x (%4.4s)", i_chroma,
168                  (char*)&i_chroma );
169     }
170     free( psz_chroma );
171
172     es_format_Init( &p_sys->fmt_video, VIDEO_ES, i_chroma );
173     vout_InitFormat( &p_sys->fmt_video.video, i_chroma, i_width, i_height,
174                      i_aspect );
175     if( !p_sys->fmt_video.video.i_bits_per_pixel )
176     {
177         msg_Err( p_demux, "Unsupported chroma 0x%.8x (%4.4s)", i_chroma,
178                  (char*)&i_chroma );
179         free( p_sys );
180         return VLC_EGENERIC;
181     }
182     p_sys->frame_size = i_width * i_height
183                         * p_sys->fmt_video.video.i_bits_per_pixel / 8;
184     p_sys->p_es_video = es_out_Add( p_demux->out, &p_sys->fmt_video );
185
186     return VLC_SUCCESS;
187 }
188
189 /*****************************************************************************
190  * Close: frees unused data
191  *****************************************************************************/
192 static void Close( vlc_object_t *p_this )
193 {
194     demux_t     *p_demux = (demux_t*)p_this;
195     demux_sys_t *p_sys  = p_demux->p_sys;
196     free( p_sys );
197 }
198
199 /*****************************************************************************
200  * Demux: reads and demuxes data packets
201  *****************************************************************************
202  * Returns -1 in case of error, 0 in case of EOF, 1 otherwise
203  *****************************************************************************/
204 static int Demux( demux_t *p_demux )
205 {
206     demux_sys_t *p_sys  = p_demux->p_sys;
207     block_t     *p_block;
208
209     /* Call the pace control */
210     es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_sys->i_pcr );
211
212     if( ( p_block = stream_Block( p_demux->s, p_sys->frame_size ) ) == NULL )
213     {
214         /* EOF */
215         return 0;
216     }
217
218     p_block->i_dts = p_block->i_pts = p_sys->i_pcr;
219     es_out_Send( p_demux->out, p_sys->p_es_video, p_block );
220
221     p_sys->i_pcr += ( I64C(1000000) / p_sys->f_fps );
222
223     return 1;
224 }
225
226 /*****************************************************************************
227  * Control:
228  *****************************************************************************/
229 static int Control( demux_t *p_demux, int i_query, va_list args )
230 {
231     demux_sys_t *p_sys  = p_demux->p_sys;
232
233     /* XXX: DEMUX_SET_TIME is precise here */
234     return demux2_vaControlHelper( p_demux->s, 0, -1,
235                                    p_sys->frame_size * p_sys->f_fps * 8,
236                                    p_sys->frame_size, i_query, args );
237 }