1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: pvr.c,v 1.5 2003/07/22 20:25:07 bigben Exp $
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 */
46 struct ivtv_ioctl_codec {
51 uint32_t bitrate_peak;
54 uint32_t dnr_temporal;
57 uint32_t framespergop;
63 /*****************************************************************************
65 *****************************************************************************/
66 static int Open ( vlc_object_t * );
67 static void Close ( vlc_object_t * );
69 static ssize_t Read ( input_thread_t *, byte_t *, size_t );
71 /*****************************************************************************
73 *****************************************************************************/
75 set_description( _("Hauppauge PVR cards input") );
76 set_capability( "access", 0 );
77 add_shortcut( "pvr" );
78 set_callbacks( Open, Close );
81 /*****************************************************************************
83 *****************************************************************************/
99 /*****************************************************************************
100 * Open: open the device
101 *****************************************************************************/
102 static int Open( vlc_object_t * p_this )
104 input_thread_t * p_input = (input_thread_t*) p_this;
105 access_sys_t * p_sys;
106 char * psz_tofree, * psz_parser, * psz_device;
108 struct v4l2_format vfmt;
109 struct v4l2_frequency vf;
110 struct ivtv_ioctl_codec codec;
112 //psz_device = calloc( strlen( "/dev/videox" ) + 1, 1 );
114 p_input->pf_read = Read;
115 p_input->stream.b_pace_control = 0;
116 p_input->stream.b_seekable = 0;
117 p_input->i_pts_delay = 1000000;
119 /* create private access data */
120 p_sys = calloc( sizeof( access_sys_t ), 1 );
121 p_input->p_access_data = p_sys;
123 /* defaults values */
126 p_sys->i_standard = V4L2_STD_SECAM;
127 p_sys->i_width = 720;
128 p_sys->i_height = 576;
129 p_sys->i_frequency = 567250; /* M6 ;) */
130 p_sys->i_framerate = 25;
131 p_sys->i_bitrate = 3000000;
132 p_sys->i_bitrate_peak = 4000000;
134 /* parse command line options */
135 psz_tofree = strdup( p_input->psz_name );
136 psz_parser = psz_tofree;
142 if ( !strncmp( psz_parser, "norm=", strlen( "norm=" ) ) )
144 char *psz_parser_init;
145 psz_parser += strlen( "norm=" );
146 psz_parser_init = psz_parser;
147 while ( *psz_parser != ':' && *psz_parser != ',' )
152 if (!strncmp( psz_parser_init, "secam" ,
153 psz_parser - psz_parser_init ) )
155 p_sys->i_standard = V4L2_STD_SECAM;
157 else if (!strncmp( psz_parser_init, "pal" ,
158 psz_parser - psz_parser_init ) )
160 p_sys->i_standard = V4L2_STD_PAL;
162 else if (!strncmp( psz_parser_init, "ntsc" ,
163 psz_parser - psz_parser_init ) )
165 p_sys->i_standard = V4L2_STD_NTSC;
168 {p_sys->i_standard = strtol( psz_parser_init ,
172 else if( !strncmp( psz_parser, "device=", strlen( "device=" ) ) )
174 psz_device = calloc( strlen( "/dev/videox" ) + 1, 1 );
175 sprintf( psz_device, "/dev/video%ld",
176 strtol( psz_parser + strlen( "device=" ),
179 else if( !strncmp( psz_parser, "frequency=",
180 strlen( "frequency=" ) ) )
183 1000 * strtol( psz_parser + strlen( "frequency=" ),
184 &psz_parser, 0 ) / 16;
186 else if( !strncmp( psz_parser, "framerate=",
187 strlen( "framerate=" ) ) )
190 strtol( psz_parser + strlen( "framerate=" ),
193 else if( !strncmp( psz_parser, "width=",
194 strlen( "width=" ) ) )
197 strtol( psz_parser + strlen( "width=" ),
200 else if( !strncmp( psz_parser, "height=",
201 strlen( "height=" ) ) )
204 strtol( psz_parser + strlen( "height=" ),
207 else if( !strncmp( psz_parser, "bitrate=",
208 strlen( "bitrate=" ) ) )
211 strtol( psz_parser + strlen( "bitrate=" ),
214 else if( !strncmp( psz_parser, "maxbitrate=",
215 strlen( "maxbitrate=" ) ) )
217 p_sys->i_bitrate_peak =
218 strtol( psz_parser + strlen( "maxbitrate=" ),
221 else if( !strncmp( psz_parser, "size=",
222 strlen( "size=" ) ) )
225 strtol( psz_parser + strlen( "size=" ),
228 strtol( psz_parser + 1 ,
234 char *psz_parser_init;
235 psz_parser_init = psz_parser;
236 while ( *psz_parser != ':' && *psz_parser != ',' )
240 psz_device = calloc( psz_parser - psz_parser_init + 1, 1 );
241 strncpy( psz_device, psz_parser_init,
242 psz_parser - psz_parser_init );
251 //give a default value to psz_device if none has bee specified
255 psz_device = calloc( strlen( "/dev/videox" ) + 1, 1 );
256 strcpy( psz_device, "/dev/video0" );
261 msg_Dbg( p_input, "device: %s, standard: %x, size: %dx%d, "
262 "frequency: %d, framerate: %d, bitrate: %d/%d",
263 psz_device, p_sys->i_standard, p_sys->i_width,
264 p_sys->i_height, p_sys->i_frequency, p_sys->i_framerate,
265 p_sys->i_bitrate, p_sys->i_bitrate_peak );
267 /* open the device */
268 if( ( p_sys->i_fd = open( psz_device, O_RDWR ) ) < 0 )
270 msg_Err( p_input, "cannot open device (%s)", strerror( errno ) );
275 /* set the video standard */
276 if( ioctl( p_sys->i_fd, VIDIOC_S_STD, &p_sys->i_standard ) < 0 )
278 msg_Warn( p_input, "VIDIOC_S_STD failed" );
281 /* set the picture size */
282 if( ioctl( p_sys->i_fd, VIDIOC_G_FMT, &vfmt ) < 0 )
284 msg_Warn( p_input, "VIDIOC_G_FMT failed" );
288 vfmt.fmt.pix.width = p_sys->i_width;
289 vfmt.fmt.pix.height = p_sys->i_height;
290 if( ioctl( p_sys->i_fd, VIDIOC_S_FMT, &vfmt ) < 0 )
292 msg_Warn( p_input, "VIDIOC_S_FMT failed" );
296 /* set the frequency */
297 vf.tuner = 0; /* TODO: let the user choose the tuner */
298 if( ioctl( p_sys->i_fd, VIDIOC_G_FREQUENCY, &vf ) < 0 )
300 msg_Warn( p_input, "VIDIOC_G_FREQUENCY failed (%s)",
305 vf.frequency = p_sys->i_frequency * 16 / 1000;
306 if( ioctl( p_sys->i_fd, VIDIOC_S_FREQUENCY, &vf ) < 0 )
308 msg_Warn( p_input, "VIDIOC_S_FREQUENCY failed (%s)",
313 /* codec parameters */
314 if( ioctl( p_sys->i_fd, IVTV_IOC_G_CODEC, &codec ) < 0 )
316 msg_Warn( p_input, "IVTV_IOC_G_CODEC failed" );
320 switch( p_sys->i_framerate )
331 msg_Warn( p_input, "invalid framerate, reverting to 25" );
335 codec.bitrate = p_sys->i_bitrate;
336 codec.bitrate_peak = p_sys->i_bitrate_peak;
337 if( ioctl( p_sys->i_fd, IVTV_IOC_S_CODEC, &codec ) < 0 )
339 msg_Warn( p_input, "IVTV_IOC_S_CODEC failed" );
346 /*****************************************************************************
347 * Close: close the device
348 *****************************************************************************/
349 static void Close( vlc_object_t * p_this )
351 input_thread_t * p_input = (input_thread_t*) p_this;
352 access_sys_t * p_sys = p_input->p_access_data;
354 close( p_sys->i_fd );
358 /*****************************************************************************
360 *****************************************************************************/
361 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer,
364 access_sys_t * p_sys = p_input->p_access_data;
368 struct timeval timeout;
372 FD_SET( p_sys->i_fd, &fds );
374 timeout.tv_usec = 500000;
376 while( !( i_ret = select( p_sys->i_fd + 1, &fds,
377 NULL, NULL, &timeout ) ) )
380 FD_SET( p_sys->i_fd, &fds );
382 timeout.tv_usec = 500000;
384 if( p_input->b_die || p_input->b_error )
390 msg_Err( p_input, "select error (%s)", strerror( errno ) );
394 i_ret = read( p_sys->i_fd, p_buffer, i_len );