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_shortname( _("PVR") );
78 set_description( _("MPEG Encoding cards input (with ivtv drivers)") );
79 set_capability( "access", 0 );
80 add_shortcut( "pvr" );
81 set_callbacks( Open, Close );
84 /*****************************************************************************
86 *****************************************************************************/
105 /*****************************************************************************
106 * Open: open the device
107 *****************************************************************************/
108 static int Open( vlc_object_t * p_this )
110 input_thread_t * p_input = (input_thread_t*) p_this;
111 access_sys_t * p_sys;
112 char * psz_tofree, * psz_parser, * psz_device;
115 struct v4l2_format vfmt;
116 struct v4l2_frequency vf;
117 struct ivtv_ioctl_codec codec;
119 //psz_device = calloc( strlen( "/dev/videox" ) + 1, 1 );
121 p_input->pf_read = Read;
122 p_input->stream.b_pace_control = 0;
123 p_input->stream.b_seekable = 0;
124 p_input->i_pts_delay = 1000000;
126 /* create private access data */
127 p_sys = calloc( sizeof( access_sys_t ), 1 );
128 p_input->p_access_data = p_sys;
130 /* defaults values */
133 p_sys->i_standard = V4L2_STD_UNKNOWN;
135 p_sys->i_height = -1;
136 p_sys->i_frequency = -1;
137 p_sys->i_framerate = -1;
138 p_sys->i_bitrate = -1;
139 p_sys->i_bitrate_peak = -1;
140 p_sys->i_bitrate_mode = -1;
141 p_sys->i_audio_bitmask = -1;
144 /* parse command line options */
145 psz_tofree = strdup( p_input->psz_name );
146 psz_parser = psz_tofree;
152 if ( !strncmp( psz_parser, "norm=", strlen( "norm=" ) ) )
154 char *psz_parser_init;
155 psz_parser += strlen( "norm=" );
156 psz_parser_init = psz_parser;
157 while ( *psz_parser != ':' && *psz_parser != ','
158 && *psz_parser != '\0' )
163 if ( !strncmp( psz_parser_init, "secam" ,
164 psz_parser - psz_parser_init ) )
166 p_sys->i_standard = V4L2_STD_SECAM;
168 else if ( !strncmp( psz_parser_init, "pal" ,
169 psz_parser - psz_parser_init ) )
171 p_sys->i_standard = V4L2_STD_PAL;
173 else if ( !strncmp( psz_parser_init, "ntsc" ,
174 psz_parser - psz_parser_init ) )
176 p_sys->i_standard = V4L2_STD_NTSC;
180 p_sys->i_standard = strtol( psz_parser_init ,
184 else if( !strncmp( psz_parser, "channel=",
185 strlen( "channel=" ) ) )
188 strtol( psz_parser + strlen( "channel=" ),
191 else if( !strncmp( psz_parser, "device=", strlen( "device=" ) ) )
193 psz_device = calloc( strlen( "/dev/videox" ) + 1, 1 );
194 sprintf( psz_device, "/dev/video%ld",
195 strtol( psz_parser + strlen( "device=" ),
198 else if( !strncmp( psz_parser, "frequency=",
199 strlen( "frequency=" ) ) )
202 strtol( psz_parser + strlen( "frequency=" ),
205 else if( !strncmp( psz_parser, "framerate=",
206 strlen( "framerate=" ) ) )
209 strtol( psz_parser + strlen( "framerate=" ),
212 else if( !strncmp( psz_parser, "width=",
213 strlen( "width=" ) ) )
216 strtol( psz_parser + strlen( "width=" ),
219 else if( !strncmp( psz_parser, "height=",
220 strlen( "height=" ) ) )
223 strtol( psz_parser + strlen( "height=" ),
226 else if( !strncmp( psz_parser, "audio=",
227 strlen( "audio=" ) ) )
229 p_sys->i_audio_bitmask =
230 strtol( psz_parser + strlen( "audio=" ),
233 else if( !strncmp( psz_parser, "bitrate=",
234 strlen( "bitrate=" ) ) )
237 strtol( psz_parser + strlen( "bitrate=" ),
240 else if( !strncmp( psz_parser, "maxbitrate=",
241 strlen( "maxbitrate=" ) ) )
243 p_sys->i_bitrate_peak =
244 strtol( psz_parser + strlen( "maxbitrate=" ),
247 else if( !strncmp( psz_parser, "bitratemode=",
248 strlen( "bitratemode=" ) ) )
250 char *psz_parser_init;
251 psz_parser += strlen( "bitratemode=" );
252 psz_parser_init = psz_parser;
253 while ( *psz_parser != ':' && *psz_parser != ','
254 && *psz_parser != '\0' )
259 if ( !strncmp( psz_parser_init, "vbr" ,
260 psz_parser - psz_parser_init ) )
262 p_sys->i_bitrate_mode = 0;
264 else if ( !strncmp( psz_parser_init, "cbr" ,
265 psz_parser - psz_parser_init ) )
267 p_sys->i_bitrate_mode = 1;
270 else if( !strncmp( psz_parser, "size=",
271 strlen( "size=" ) ) )
274 strtol( psz_parser + strlen( "size=" ),
277 strtol( psz_parser + 1 ,
282 char *psz_parser_init;
283 psz_parser_init = psz_parser;
284 while ( *psz_parser != ':' && *psz_parser != ',' && *psz_parser != '\0' )
288 psz_device = calloc( psz_parser - psz_parser_init + 1, 1 );
289 strncpy( psz_device, psz_parser_init,
290 psz_parser - psz_parser_init );
299 //give a default value to psz_device if none has bee specified
303 psz_device = calloc( strlen( "/dev/videox" ) + 1, 1 );
304 strcpy( psz_device, "/dev/video0" );
309 /* open the device */
310 if( ( p_sys->i_fd = open( psz_device, O_RDWR ) ) < 0 )
312 msg_Err( p_input, "cannot open device (%s)", strerror( errno ) );
317 msg_Dbg( p_input, "using video device: %s",psz_device);
323 if ( p_sys->i_input != -1 )
325 if ( ioctl( p_sys->i_fd, VIDIOC_S_INPUT, &p_sys->i_input ) < 0 )
327 msg_Warn( p_input, "VIDIOC_S_INPUT failed" );
331 msg_Dbg( p_input, "input set to:%d", p_sys->i_input);
335 /* set the video standard */
336 if ( p_sys->i_standard != V4L2_STD_UNKNOWN )
338 if ( ioctl( p_sys->i_fd, VIDIOC_S_STD, &p_sys->i_standard ) < 0 )
340 msg_Warn( p_input, "VIDIOC_S_STD failed" );
344 msg_Dbg( p_input, "video standard set to:%x", p_sys->i_standard);
348 /* set the picture size */
349 if ( p_sys->i_width != -1 || p_sys->i_height != -1 )
351 if ( ioctl( p_sys->i_fd, VIDIOC_G_FMT, &vfmt ) < 0 )
353 msg_Warn( p_input, "VIDIOC_G_FMT failed" );
357 if ( p_sys->i_width != -1 )
359 vfmt.fmt.pix.width = p_sys->i_width;
362 if ( p_sys->i_height != -1 )
364 vfmt.fmt.pix.height = p_sys->i_height;
367 if ( ioctl( p_sys->i_fd, VIDIOC_S_FMT, &vfmt ) < 0 )
369 msg_Warn( p_input, "VIDIOC_S_FMT failed" );
373 msg_Dbg( p_input, "picture size set to:%dx%d",
374 vfmt.fmt.pix.width, vfmt.fmt.pix.height );
379 /* set the frequency */
380 if ( p_sys->i_frequency != -1 )
382 vf.tuner = 0; /* TODO: let the user choose the tuner */
383 if ( ioctl( p_sys->i_fd, VIDIOC_G_FREQUENCY, &vf ) < 0 )
385 msg_Warn( p_input, "VIDIOC_G_FREQUENCY failed (%s)",
390 vf.frequency = p_sys->i_frequency * 16 / 1000;
391 if( ioctl( p_sys->i_fd, VIDIOC_S_FREQUENCY, &vf ) < 0 )
393 msg_Warn( p_input, "VIDIOC_S_FREQUENCY failed (%s)",
398 msg_Dbg( p_input, "Tuner frequency set to:%d",
404 /* codec parameters */
405 if ( p_sys->i_framerate != -1
406 || p_sys->i_bitrate_mode != -1
407 || p_sys->i_bitrate_peak != -1
408 || p_sys->i_bitrate != -1
409 || p_sys->i_audio_bitmask != -1 )
411 if ( ioctl( p_sys->i_fd, IVTV_IOC_G_CODEC, &codec ) < 0 )
413 msg_Warn( p_input, "IVTV_IOC_G_CODEC failed" );
417 if ( p_sys->i_framerate != -1 )
419 switch ( p_sys->i_framerate )
430 msg_Warn( p_input, "invalid framerate, reverting to 25" );
436 if ( p_sys->i_bitrate != -1 )
438 codec.bitrate = p_sys->i_bitrate;
441 if ( p_sys->i_bitrate_peak != -1 )
443 codec.bitrate_peak = p_sys->i_bitrate_peak;
446 if ( p_sys->i_bitrate_mode != -1 )
448 codec.bitrate_mode = p_sys->i_bitrate_mode;
451 if ( p_sys->i_audio_bitmask != -1 )
453 codec.audio_bitmask = p_sys->i_audio_bitmask;
456 if( ioctl( p_sys->i_fd, IVTV_IOC_S_CODEC, &codec ) < 0 )
458 msg_Warn( p_input, "IVTV_IOC_S_CODEC failed" );
462 msg_Dbg( p_input, "Setting codec parameters to: framerate: %d, bitrate: %d/%d/%d",
463 codec.framerate, codec.bitrate, codec.bitrate_peak, codec.bitrate_mode );
468 /* do a quick read */
472 if ( read( p_sys->i_fd, psz_tmp, 1 ) )
474 msg_Dbg(p_input, "Could read byte from device");
478 msg_Warn(p_input, "Could not read byte from device");
485 /*****************************************************************************
486 * Close: close the device
487 *****************************************************************************/
488 static void Close( vlc_object_t * p_this )
490 input_thread_t * p_input = (input_thread_t*) p_this;
491 access_sys_t * p_sys = p_input->p_access_data;
493 close( p_sys->i_fd );
497 /*****************************************************************************
499 *****************************************************************************/
500 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer,
503 access_sys_t * p_sys = p_input->p_access_data;
507 struct timeval timeout;
511 FD_SET( p_sys->i_fd, &fds );
513 timeout.tv_usec = 500000;
515 while( !( i_ret = select( p_sys->i_fd + 1, &fds,
516 NULL, NULL, &timeout ) ) )
519 FD_SET( p_sys->i_fd, &fds );
521 timeout.tv_usec = 500000;
523 if( p_input->b_die || p_input->b_error )
529 msg_Err( p_input, "select error (%s)", strerror( errno ) );
533 i_ret = read( p_sys->i_fd, p_buffer, i_len );