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 *****************************************************************************/
107 /*****************************************************************************
108 * Open: open the device
109 *****************************************************************************/
110 static int Open( vlc_object_t * p_this )
112 input_thread_t * p_input = (input_thread_t*) p_this;
113 access_sys_t * p_sys;
114 char * psz_tofree, * psz_parser, * psz_device;
117 struct v4l2_format vfmt;
118 struct v4l2_frequency vf;
119 struct ivtv_ioctl_codec codec;
121 //psz_device = calloc( strlen( "/dev/videox" ) + 1, 1 );
123 p_input->pf_read = Read;
124 p_input->stream.b_pace_control = 0;
125 p_input->stream.b_seekable = 0;
126 p_input->i_pts_delay = 1000000;
128 /* create private access data */
129 p_sys = calloc( sizeof( access_sys_t ), 1 );
130 p_input->p_access_data = p_sys;
132 /* defaults values */
135 p_sys->i_standard = V4L2_STD_UNKNOWN;
137 p_sys->i_height = -1;
138 p_sys->i_frequency = -1;
139 p_sys->i_framerate = -1;
140 p_sys->i_keyint = -1;
141 p_sys->i_bframes = -1;
142 p_sys->i_bitrate = -1;
143 p_sys->i_bitrate_peak = -1;
144 p_sys->i_bitrate_mode = -1;
145 p_sys->i_audio_bitmask = -1;
148 /* parse command line options */
149 psz_tofree = strdup( p_input->psz_name );
150 psz_parser = psz_tofree;
156 if ( !strncmp( psz_parser, "norm=", strlen( "norm=" ) ) )
158 char *psz_parser_init;
159 psz_parser += strlen( "norm=" );
160 psz_parser_init = psz_parser;
161 while ( *psz_parser != ':' && *psz_parser != ','
162 && *psz_parser != '\0' )
167 if ( !strncmp( psz_parser_init, "secam" ,
168 psz_parser - psz_parser_init ) )
170 p_sys->i_standard = V4L2_STD_SECAM;
172 else if ( !strncmp( psz_parser_init, "pal" ,
173 psz_parser - psz_parser_init ) )
175 p_sys->i_standard = V4L2_STD_PAL;
177 else if ( !strncmp( psz_parser_init, "ntsc" ,
178 psz_parser - psz_parser_init ) )
180 p_sys->i_standard = V4L2_STD_NTSC;
184 p_sys->i_standard = strtol( psz_parser_init ,
188 else if( !strncmp( psz_parser, "channel=",
189 strlen( "channel=" ) ) )
192 strtol( psz_parser + strlen( "channel=" ),
195 else if( !strncmp( psz_parser, "device=", strlen( "device=" ) ) )
197 psz_device = calloc( strlen( "/dev/videox" ) + 1, 1 );
198 sprintf( psz_device, "/dev/video%ld",
199 strtol( psz_parser + strlen( "device=" ),
202 else if( !strncmp( psz_parser, "frequency=",
203 strlen( "frequency=" ) ) )
206 strtol( psz_parser + strlen( "frequency=" ),
209 else if( !strncmp( psz_parser, "framerate=",
210 strlen( "framerate=" ) ) )
213 strtol( psz_parser + strlen( "framerate=" ),
216 else if( !strncmp( psz_parser, "keyint=",
217 strlen( "keyint=" ) ) )
220 strtol( psz_parser + strlen( "keyint=" ),
223 else if( !strncmp( psz_parser, "bframes=",
224 strlen( "bframes=" ) ) )
227 strtol( psz_parser + strlen( "bframes=" ),
230 else if( !strncmp( psz_parser, "width=",
231 strlen( "width=" ) ) )
234 strtol( psz_parser + strlen( "width=" ),
237 else if( !strncmp( psz_parser, "height=",
238 strlen( "height=" ) ) )
241 strtol( psz_parser + strlen( "height=" ),
244 else if( !strncmp( psz_parser, "audio=",
245 strlen( "audio=" ) ) )
247 p_sys->i_audio_bitmask =
248 strtol( psz_parser + strlen( "audio=" ),
251 else if( !strncmp( psz_parser, "bitrate=",
252 strlen( "bitrate=" ) ) )
255 strtol( psz_parser + strlen( "bitrate=" ),
258 else if( !strncmp( psz_parser, "maxbitrate=",
259 strlen( "maxbitrate=" ) ) )
261 p_sys->i_bitrate_peak =
262 strtol( psz_parser + strlen( "maxbitrate=" ),
265 else if( !strncmp( psz_parser, "bitratemode=",
266 strlen( "bitratemode=" ) ) )
268 char *psz_parser_init;
269 psz_parser += strlen( "bitratemode=" );
270 psz_parser_init = psz_parser;
271 while ( *psz_parser != ':' && *psz_parser != ','
272 && *psz_parser != '\0' )
277 if ( !strncmp( psz_parser_init, "vbr" ,
278 psz_parser - psz_parser_init ) )
280 p_sys->i_bitrate_mode = 0;
282 else if ( !strncmp( psz_parser_init, "cbr" ,
283 psz_parser - psz_parser_init ) )
285 p_sys->i_bitrate_mode = 1;
288 else if( !strncmp( psz_parser, "size=",
289 strlen( "size=" ) ) )
292 strtol( psz_parser + strlen( "size=" ),
295 strtol( psz_parser + 1 ,
300 char *psz_parser_init;
301 psz_parser_init = psz_parser;
302 while ( *psz_parser != ':' && *psz_parser != ',' && *psz_parser != '\0' )
306 psz_device = calloc( psz_parser - psz_parser_init + 1, 1 );
307 strncpy( psz_device, psz_parser_init,
308 psz_parser - psz_parser_init );
317 //give a default value to psz_device if none has bee specified
321 psz_device = calloc( strlen( "/dev/videox" ) + 1, 1 );
322 strcpy( psz_device, "/dev/video0" );
327 /* open the device */
328 if( ( p_sys->i_fd = open( psz_device, O_RDWR ) ) < 0 )
330 msg_Err( p_input, "cannot open device (%s)", strerror( errno ) );
335 msg_Dbg( p_input, "using video device: %s",psz_device);
341 if ( p_sys->i_input != -1 )
343 if ( ioctl( p_sys->i_fd, VIDIOC_S_INPUT, &p_sys->i_input ) < 0 )
345 msg_Warn( p_input, "VIDIOC_S_INPUT failed" );
349 msg_Dbg( p_input, "input set to:%d", p_sys->i_input);
353 /* set the video standard */
354 if ( p_sys->i_standard != V4L2_STD_UNKNOWN )
356 if ( ioctl( p_sys->i_fd, VIDIOC_S_STD, &p_sys->i_standard ) < 0 )
358 msg_Warn( p_input, "VIDIOC_S_STD failed" );
362 msg_Dbg( p_input, "video standard set to:%x", p_sys->i_standard);
366 /* set the picture size */
367 if ( p_sys->i_width != -1 || p_sys->i_height != -1 )
369 if ( ioctl( p_sys->i_fd, VIDIOC_G_FMT, &vfmt ) < 0 )
371 msg_Warn( p_input, "VIDIOC_G_FMT failed" );
375 if ( p_sys->i_width != -1 )
377 vfmt.fmt.pix.width = p_sys->i_width;
380 if ( p_sys->i_height != -1 )
382 vfmt.fmt.pix.height = p_sys->i_height;
385 if ( ioctl( p_sys->i_fd, VIDIOC_S_FMT, &vfmt ) < 0 )
387 msg_Warn( p_input, "VIDIOC_S_FMT failed" );
391 msg_Dbg( p_input, "picture size set to:%dx%d",
392 vfmt.fmt.pix.width, vfmt.fmt.pix.height );
397 /* set the frequency */
398 if ( p_sys->i_frequency != -1 )
400 vf.tuner = 0; /* TODO: let the user choose the tuner */
401 if ( ioctl( p_sys->i_fd, VIDIOC_G_FREQUENCY, &vf ) < 0 )
403 msg_Warn( p_input, "VIDIOC_G_FREQUENCY failed (%s)",
408 vf.frequency = p_sys->i_frequency * 16 / 1000;
409 if( ioctl( p_sys->i_fd, VIDIOC_S_FREQUENCY, &vf ) < 0 )
411 msg_Warn( p_input, "VIDIOC_S_FREQUENCY failed (%s)",
416 msg_Dbg( p_input, "Tuner frequency set to:%d",
422 /* codec parameters */
423 if ( p_sys->i_framerate != -1
424 || p_sys->i_keyint != -1
425 || p_sys->i_bframes != -1
426 || p_sys->i_bitrate_mode != -1
427 || p_sys->i_bitrate_peak != -1
428 || p_sys->i_bitrate != -1
429 || p_sys->i_audio_bitmask != -1 )
431 if ( ioctl( p_sys->i_fd, IVTV_IOC_G_CODEC, &codec ) < 0 )
433 msg_Warn( p_input, "IVTV_IOC_G_CODEC failed" );
437 if ( p_sys->i_framerate != -1 )
439 switch ( p_sys->i_framerate )
450 msg_Warn( p_input, "invalid framerate, reverting to 25" );
456 if ( p_sys->i_keyint != -1 )
458 codec.framespergop = p_sys->i_keyint;
461 if ( p_sys->i_bframes != -1 )
463 codec.bframes = p_sys->i_bframes;
466 if ( p_sys->i_bitrate != -1 )
468 codec.bitrate = p_sys->i_bitrate;
471 if ( p_sys->i_bitrate_peak != -1 )
473 codec.bitrate_peak = p_sys->i_bitrate_peak;
476 if ( p_sys->i_bitrate_mode != -1 )
478 codec.bitrate_mode = p_sys->i_bitrate_mode;
481 if ( p_sys->i_audio_bitmask != -1 )
483 codec.audio_bitmask = p_sys->i_audio_bitmask;
486 if( ioctl( p_sys->i_fd, IVTV_IOC_S_CODEC, &codec ) < 0 )
488 msg_Warn( p_input, "IVTV_IOC_S_CODEC failed" );
492 msg_Dbg( p_input, "Setting codec parameters to: framerate: %d, bitrate: %d/%d/%d",
493 codec.framerate, codec.bitrate, codec.bitrate_peak, codec.bitrate_mode );
498 /* do a quick read */
502 if ( read( p_sys->i_fd, psz_tmp, 1 ) )
504 msg_Dbg(p_input, "Could read byte from device");
508 msg_Warn(p_input, "Could not read byte from device");
515 /*****************************************************************************
516 * Close: close the device
517 *****************************************************************************/
518 static void Close( vlc_object_t * p_this )
520 input_thread_t * p_input = (input_thread_t*) p_this;
521 access_sys_t * p_sys = p_input->p_access_data;
523 close( p_sys->i_fd );
527 /*****************************************************************************
529 *****************************************************************************/
530 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer,
533 access_sys_t * p_sys = p_input->p_access_data;
537 struct timeval timeout;
541 FD_SET( p_sys->i_fd, &fds );
543 timeout.tv_usec = 500000;
545 while( !( i_ret = select( p_sys->i_fd + 1, &fds,
546 NULL, NULL, &timeout ) ) )
549 FD_SET( p_sys->i_fd, &fds );
551 timeout.tv_usec = 500000;
553 if( p_input->b_die || p_input->b_error )
559 msg_Err( p_input, "select error (%s)", strerror( errno ) );
563 i_ret = read( p_sys->i_fd, p_buffer, i_len );