1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
7 * Authors: Eric Petit <titer@videolan.org>
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
28 #include <vlc/input.h>
31 #include <sys/types.h>
36 #include <linux/types.h>
37 #include <sys/ioctl.h>
38 #include "videodev2.h"
40 /* ivtv specific ioctls */
41 #define IVTV_IOC_G_CODEC 0xFFEE7703
42 #define IVTV_IOC_S_CODEC 0xFFEE7704
45 /* for use with IVTV_IOC_G_CODEC and IVTV_IOC_S_CODEC */
47 struct ivtv_ioctl_codec {
49 uint32_t audio_bitmask;
51 uint32_t bitrate_mode;
53 uint32_t bitrate_peak;
56 uint32_t dnr_temporal;
59 uint32_t framespergop;
65 /*****************************************************************************
67 *****************************************************************************/
68 static int Open ( vlc_object_t * );
69 static void Close ( vlc_object_t * );
71 static ssize_t Read ( input_thread_t *, byte_t *, size_t );
73 /*****************************************************************************
75 *****************************************************************************/
77 set_description( _("MPEG Encoding cards input (with ivtv drivers)") );
78 set_capability( "access", 0 );
79 add_shortcut( "pvr" );
80 set_callbacks( Open, Close );
83 /*****************************************************************************
85 *****************************************************************************/
104 /*****************************************************************************
105 * Open: open the device
106 *****************************************************************************/
107 static int Open( vlc_object_t * p_this )
109 input_thread_t * p_input = (input_thread_t*) p_this;
110 access_sys_t * p_sys;
111 char * psz_tofree, * psz_parser, * psz_device;
114 struct v4l2_format vfmt;
115 struct v4l2_frequency vf;
116 struct ivtv_ioctl_codec codec;
118 //psz_device = calloc( strlen( "/dev/videox" ) + 1, 1 );
120 p_input->pf_read = Read;
121 p_input->stream.b_pace_control = 0;
122 p_input->stream.b_seekable = 0;
123 p_input->i_pts_delay = 1000000;
125 /* create private access data */
126 p_sys = calloc( sizeof( access_sys_t ), 1 );
127 p_input->p_access_data = p_sys;
129 /* defaults values */
132 p_sys->i_standard = V4L2_STD_UNKNOWN;
134 p_sys->i_height = -1;
135 p_sys->i_frequency = -1;
136 p_sys->i_framerate = -1;
137 p_sys->i_bitrate = -1;
138 p_sys->i_bitrate_peak = -1;
139 p_sys->i_bitrate_mode = -1;
140 p_sys->i_audio_bitmask = -1;
143 /* parse command line options */
144 psz_tofree = strdup( p_input->psz_name );
145 psz_parser = psz_tofree;
151 if ( !strncmp( psz_parser, "norm=", strlen( "norm=" ) ) )
153 char *psz_parser_init;
154 psz_parser += strlen( "norm=" );
155 psz_parser_init = psz_parser;
156 while ( *psz_parser != ':' && *psz_parser != ','
157 && *psz_parser != '\0' )
162 if ( !strncmp( psz_parser_init, "secam" ,
163 psz_parser - psz_parser_init ) )
165 p_sys->i_standard = V4L2_STD_SECAM;
167 else if ( !strncmp( psz_parser_init, "pal" ,
168 psz_parser - psz_parser_init ) )
170 p_sys->i_standard = V4L2_STD_PAL;
172 else if ( !strncmp( psz_parser_init, "ntsc" ,
173 psz_parser - psz_parser_init ) )
175 p_sys->i_standard = V4L2_STD_NTSC;
179 p_sys->i_standard = strtol( psz_parser_init ,
183 else if( !strncmp( psz_parser, "channel=",
184 strlen( "channel=" ) ) )
187 strtol( psz_parser + strlen( "channel=" ),
190 else if( !strncmp( psz_parser, "device=", strlen( "device=" ) ) )
192 psz_device = calloc( strlen( "/dev/videox" ) + 1, 1 );
193 sprintf( psz_device, "/dev/video%ld",
194 strtol( psz_parser + strlen( "device=" ),
197 else if( !strncmp( psz_parser, "frequency=",
198 strlen( "frequency=" ) ) )
201 strtol( psz_parser + strlen( "frequency=" ),
204 else if( !strncmp( psz_parser, "framerate=",
205 strlen( "framerate=" ) ) )
208 strtol( psz_parser + strlen( "framerate=" ),
211 else if( !strncmp( psz_parser, "width=",
212 strlen( "width=" ) ) )
215 strtol( psz_parser + strlen( "width=" ),
218 else if( !strncmp( psz_parser, "height=",
219 strlen( "height=" ) ) )
222 strtol( psz_parser + strlen( "height=" ),
225 else if( !strncmp( psz_parser, "audio=",
226 strlen( "audio=" ) ) )
228 p_sys->i_audio_bitmask =
229 strtol( psz_parser + strlen( "audio=" ),
232 else if( !strncmp( psz_parser, "bitrate=",
233 strlen( "bitrate=" ) ) )
236 strtol( psz_parser + strlen( "bitrate=" ),
239 else if( !strncmp( psz_parser, "maxbitrate=",
240 strlen( "maxbitrate=" ) ) )
242 p_sys->i_bitrate_peak =
243 strtol( psz_parser + strlen( "maxbitrate=" ),
246 else if( !strncmp( psz_parser, "bitratemode=",
247 strlen( "bitratemode=" ) ) )
249 char *psz_parser_init;
250 psz_parser += strlen( "bitratemode=" );
251 psz_parser_init = psz_parser;
252 while ( *psz_parser != ':' && *psz_parser != ','
253 && *psz_parser != '\0' )
258 if ( !strncmp( psz_parser_init, "vbr" ,
259 psz_parser - psz_parser_init ) )
261 p_sys->i_bitrate_mode = 0;
263 else if ( !strncmp( psz_parser_init, "cbr" ,
264 psz_parser - psz_parser_init ) )
266 p_sys->i_bitrate_mode = 1;
269 else if( !strncmp( psz_parser, "size=",
270 strlen( "size=" ) ) )
273 strtol( psz_parser + strlen( "size=" ),
276 strtol( psz_parser + 1 ,
281 char *psz_parser_init;
282 psz_parser_init = psz_parser;
283 while ( *psz_parser != ':' && *psz_parser != ',' && *psz_parser != '\0' )
287 psz_device = calloc( psz_parser - psz_parser_init + 1, 1 );
288 strncpy( psz_device, psz_parser_init,
289 psz_parser - psz_parser_init );
298 //give a default value to psz_device if none has bee specified
302 psz_device = calloc( strlen( "/dev/videox" ) + 1, 1 );
303 strcpy( psz_device, "/dev/video0" );
308 /* open the device */
309 if( ( p_sys->i_fd = open( psz_device, O_RDWR ) ) < 0 )
311 msg_Err( p_input, "cannot open device (%s)", strerror( errno ) );
316 msg_Dbg( p_input, "using video device: %s",psz_device);
322 if ( p_sys->i_input != -1 )
324 if ( ioctl( p_sys->i_fd, VIDIOC_S_INPUT, &p_sys->i_input ) < 0 )
326 msg_Warn( p_input, "VIDIOC_S_INPUT failed" );
330 msg_Dbg( p_input, "input set to:%d", p_sys->i_input);
334 /* set the video standard */
335 if ( p_sys->i_standard != V4L2_STD_UNKNOWN )
337 if ( ioctl( p_sys->i_fd, VIDIOC_S_STD, &p_sys->i_standard ) < 0 )
339 msg_Warn( p_input, "VIDIOC_S_STD failed" );
343 msg_Dbg( p_input, "video standard set to:%x", p_sys->i_standard);
347 /* set the picture size */
348 if ( p_sys->i_width != -1 || p_sys->i_height != -1 )
350 if ( ioctl( p_sys->i_fd, VIDIOC_G_FMT, &vfmt ) < 0 )
352 msg_Warn( p_input, "VIDIOC_G_FMT failed" );
356 if ( p_sys->i_width != -1 )
358 vfmt.fmt.pix.width = p_sys->i_width;
361 if ( p_sys->i_height != -1 )
363 vfmt.fmt.pix.height = p_sys->i_height;
366 if ( ioctl( p_sys->i_fd, VIDIOC_S_FMT, &vfmt ) < 0 )
368 msg_Warn( p_input, "VIDIOC_S_FMT failed" );
372 msg_Dbg( p_input, "picture size set to:%dx%d",
373 vfmt.fmt.pix.width, vfmt.fmt.pix.height );
378 /* set the frequency */
379 if ( p_sys->i_frequency != -1 )
381 vf.tuner = 0; /* TODO: let the user choose the tuner */
382 if ( ioctl( p_sys->i_fd, VIDIOC_G_FREQUENCY, &vf ) < 0 )
384 msg_Warn( p_input, "VIDIOC_G_FREQUENCY failed (%s)",
389 vf.frequency = p_sys->i_frequency * 16 / 1000;
390 if( ioctl( p_sys->i_fd, VIDIOC_S_FREQUENCY, &vf ) < 0 )
392 msg_Warn( p_input, "VIDIOC_S_FREQUENCY failed (%s)",
397 msg_Dbg( p_input, "Tuner frequency set to:%d",
403 /* codec parameters */
404 if ( p_sys->i_framerate != -1
405 || p_sys->i_bitrate_mode != -1
406 || p_sys->i_bitrate_peak != -1
407 || p_sys->i_bitrate != -1
408 || p_sys->i_audio_bitmask != -1 )
410 if ( ioctl( p_sys->i_fd, IVTV_IOC_G_CODEC, &codec ) < 0 )
412 msg_Warn( p_input, "IVTV_IOC_G_CODEC failed" );
416 if ( p_sys->i_framerate != -1 )
418 switch ( p_sys->i_framerate )
429 msg_Warn( p_input, "invalid framerate, reverting to 25" );
435 if ( p_sys->i_bitrate != -1 )
437 codec.bitrate = p_sys->i_bitrate;
440 if ( p_sys->i_bitrate_peak != -1 )
442 codec.bitrate_peak = p_sys->i_bitrate_peak;
445 if ( p_sys->i_bitrate_mode != -1 )
447 codec.bitrate_mode = p_sys->i_bitrate_mode;
450 if ( p_sys->i_audio_bitmask != -1 )
452 codec.audio_bitmask = p_sys->i_audio_bitmask;
455 if( ioctl( p_sys->i_fd, IVTV_IOC_S_CODEC, &codec ) < 0 )
457 msg_Warn( p_input, "IVTV_IOC_S_CODEC failed" );
461 msg_Dbg( p_input, "Setting codec parameters to: framerate: %d, bitrate: %d/%d/%d",
462 codec.framerate, codec.bitrate, codec.bitrate_peak, codec.bitrate_mode );
467 /* do a quick read */
471 if ( read( p_sys->i_fd, psz_tmp, 1 ) )
473 msg_Dbg(p_input, "Could read byte from device");
477 msg_Warn(p_input, "Could not read byte from device");
484 /*****************************************************************************
485 * Close: close the device
486 *****************************************************************************/
487 static void Close( vlc_object_t * p_this )
489 input_thread_t * p_input = (input_thread_t*) p_this;
490 access_sys_t * p_sys = p_input->p_access_data;
492 close( p_sys->i_fd );
496 /*****************************************************************************
498 *****************************************************************************/
499 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer,
502 access_sys_t * p_sys = p_input->p_access_data;
506 struct timeval timeout;
510 FD_SET( p_sys->i_fd, &fds );
512 timeout.tv_usec = 500000;
514 while( !( i_ret = select( p_sys->i_fd + 1, &fds,
515 NULL, NULL, &timeout ) ) )
518 FD_SET( p_sys->i_fd, &fds );
520 timeout.tv_usec = 500000;
522 if( p_input->b_die || p_input->b_error )
528 msg_Err( p_input, "select error (%s)", strerror( errno ) );
532 i_ret = read( p_sys->i_fd, p_buffer, i_len );