1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 VideoLAN
5 * $Id: pvr.c,v 1.10 2003/11/23 18:31:54 alexis 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 */
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( _("input for encoding cards supported by the ivtv drivers") );
78 set_capability( "access", 0 );
79 add_shortcut( "pvr" );
80 set_callbacks( Open, Close );
83 /*****************************************************************************
85 *****************************************************************************/
103 /*****************************************************************************
104 * Open: open the device
105 *****************************************************************************/
106 static int Open( vlc_object_t * p_this )
108 input_thread_t * p_input = (input_thread_t*) p_this;
109 access_sys_t * p_sys;
110 char * psz_tofree, * psz_parser, * psz_device;
112 struct v4l2_format vfmt;
113 struct v4l2_frequency vf;
114 struct ivtv_ioctl_codec codec;
116 //psz_device = calloc( strlen( "/dev/videox" ) + 1, 1 );
118 p_input->pf_read = Read;
119 p_input->stream.b_pace_control = 0;
120 p_input->stream.b_seekable = 0;
121 p_input->i_pts_delay = 1000000;
123 /* create private access data */
124 p_sys = calloc( sizeof( access_sys_t ), 1 );
125 p_input->p_access_data = p_sys;
127 /* defaults values */
130 p_sys->i_standard = V4L2_STD_SECAM;
131 p_sys->i_width = 720;
132 p_sys->i_height = 576;
133 p_sys->i_frequency = 567250; /* M6 ;) */
134 p_sys->i_framerate = 25;
135 p_sys->i_bitrate = 3000000;
136 p_sys->i_bitrate_peak = 4000000;
139 /* parse command line options */
140 psz_tofree = strdup( p_input->psz_name );
141 psz_parser = psz_tofree;
147 if ( !strncmp( psz_parser, "norm=", strlen( "norm=" ) ) )
149 char *psz_parser_init;
150 psz_parser += strlen( "norm=" );
151 psz_parser_init = psz_parser;
152 while ( *psz_parser != ':' && *psz_parser != ','
153 && *psz_parser != '\0' )
158 if (!strncmp( psz_parser_init, "secam" ,
159 psz_parser - psz_parser_init ) )
161 p_sys->i_standard = V4L2_STD_SECAM;
163 else if (!strncmp( psz_parser_init, "pal" ,
164 psz_parser - psz_parser_init ) )
166 p_sys->i_standard = V4L2_STD_PAL;
168 else if (!strncmp( psz_parser_init, "ntsc" ,
169 psz_parser - psz_parser_init ) )
171 p_sys->i_standard = V4L2_STD_NTSC;
174 {p_sys->i_standard = strtol( psz_parser_init ,
178 else if( !strncmp( psz_parser, "channel=",
179 strlen( "channel=" ) ) )
182 strtol( psz_parser + strlen( "channel=" ),
185 else if( !strncmp( psz_parser, "device=", strlen( "device=" ) ) )
187 psz_device = calloc( strlen( "/dev/videox" ) + 1, 1 );
188 sprintf( psz_device, "/dev/video%ld",
189 strtol( psz_parser + strlen( "device=" ),
192 else if( !strncmp( psz_parser, "frequency=",
193 strlen( "frequency=" ) ) )
196 strtol( psz_parser + strlen( "frequency=" ),
199 else if( !strncmp( psz_parser, "framerate=",
200 strlen( "framerate=" ) ) )
203 strtol( psz_parser + strlen( "framerate=" ),
206 else if( !strncmp( psz_parser, "width=",
207 strlen( "width=" ) ) )
210 strtol( psz_parser + strlen( "width=" ),
213 else if( !strncmp( psz_parser, "height=",
214 strlen( "height=" ) ) )
217 strtol( psz_parser + strlen( "height=" ),
220 else if( !strncmp( psz_parser, "bitrate=",
221 strlen( "bitrate=" ) ) )
224 strtol( psz_parser + strlen( "bitrate=" ),
227 else if( !strncmp( psz_parser, "maxbitrate=",
228 strlen( "maxbitrate=" ) ) )
230 p_sys->i_bitrate_peak =
231 strtol( psz_parser + strlen( "maxbitrate=" ),
234 else if( !strncmp( psz_parser, "bitratemode=",
235 strlen( "bitratemode=" ) ) )
237 char *psz_parser_init;
238 psz_parser += strlen( "bitratemode=" );
239 psz_parser_init = psz_parser;
240 while ( *psz_parser != ':' && *psz_parser != ','
241 && *psz_parser != '\0' )
246 if (!strncmp( psz_parser_init, "vbr" ,
247 psz_parser - psz_parser_init ) )
249 p_sys->i_bitrate_mode = 0;
251 else if (!strncmp( psz_parser_init, "cbr" ,
252 psz_parser - psz_parser_init ) )
254 p_sys->i_bitrate_mode = 1;
258 else if( !strncmp( psz_parser, "size=",
259 strlen( "size=" ) ) )
262 strtol( psz_parser + strlen( "size=" ),
265 strtol( psz_parser + 1 ,
271 char *psz_parser_init;
272 psz_parser_init = psz_parser;
273 while ( *psz_parser != ':' && *psz_parser != ','
274 && *psz_parser != '\0' )
278 psz_device = calloc( psz_parser - psz_parser_init + 1, 1 );
279 strncpy( psz_device, psz_parser_init,
280 psz_parser - psz_parser_init );
289 //give a default value to psz_device if none has bee specified
293 psz_device = calloc( strlen( "/dev/videox" ) + 1, 1 );
294 strcpy( psz_device, "/dev/video0" );
299 msg_Dbg( p_input, "device: %s, input: %d, standard: %x, size: %dx%d, "
300 "frequency: %d, framerate: %d, bitrate: %d/%d/%d",
301 psz_device, p_sys->i_input, p_sys->i_standard, p_sys->i_width,
302 p_sys->i_height, p_sys->i_frequency, p_sys->i_framerate,
303 p_sys->i_bitrate, p_sys->i_bitrate_peak,p_sys->i_bitrate_mode );
305 /* open the device */
306 if( ( p_sys->i_fd = open( psz_device, O_RDWR ) ) < 0 )
308 msg_Err( p_input, "cannot open device (%s)", strerror( errno ) );
314 if( ioctl( p_sys->i_fd, VIDIOC_S_INPUT, &p_sys->i_input ) < 0 )
316 msg_Warn( p_input, "VIDIOC_S_INPUT failed" );
320 /* set the video standard */
321 if( ioctl( p_sys->i_fd, VIDIOC_S_STD, &p_sys->i_standard ) < 0 )
323 msg_Warn( p_input, "VIDIOC_S_STD failed" );
326 /* set the picture size */
327 if( ioctl( p_sys->i_fd, VIDIOC_G_FMT, &vfmt ) < 0 )
329 msg_Warn( p_input, "VIDIOC_G_FMT failed" );
333 vfmt.fmt.pix.width = p_sys->i_width;
334 vfmt.fmt.pix.height = p_sys->i_height;
335 if( ioctl( p_sys->i_fd, VIDIOC_S_FMT, &vfmt ) < 0 )
337 msg_Warn( p_input, "VIDIOC_S_FMT failed" );
341 /* set the frequency */
342 vf.tuner = 0; /* TODO: let the user choose the tuner */
343 if( ioctl( p_sys->i_fd, VIDIOC_G_FREQUENCY, &vf ) < 0 )
345 msg_Warn( p_input, "VIDIOC_G_FREQUENCY failed (%s)",
350 vf.frequency = p_sys->i_frequency * 16 / 1000;
351 if( ioctl( p_sys->i_fd, VIDIOC_S_FREQUENCY, &vf ) < 0 )
353 msg_Warn( p_input, "VIDIOC_S_FREQUENCY failed (%s)",
358 /* codec parameters */
359 if( ioctl( p_sys->i_fd, IVTV_IOC_G_CODEC, &codec ) < 0 )
361 msg_Warn( p_input, "IVTV_IOC_G_CODEC failed" );
365 switch( p_sys->i_framerate )
376 msg_Warn( p_input, "invalid framerate, reverting to 25" );
380 codec.bitrate = p_sys->i_bitrate;
381 codec.bitrate_peak = p_sys->i_bitrate_peak;
382 codec.bitrate_mode = p_sys->i_bitrate_mode;
383 if( ioctl( p_sys->i_fd, IVTV_IOC_S_CODEC, &codec ) < 0 )
385 msg_Warn( p_input, "IVTV_IOC_S_CODEC failed" );
392 /*****************************************************************************
393 * Close: close the device
394 *****************************************************************************/
395 static void Close( vlc_object_t * p_this )
397 input_thread_t * p_input = (input_thread_t*) p_this;
398 access_sys_t * p_sys = p_input->p_access_data;
400 close( p_sys->i_fd );
404 /*****************************************************************************
406 *****************************************************************************/
407 static ssize_t Read( input_thread_t * p_input, byte_t * p_buffer,
410 access_sys_t * p_sys = p_input->p_access_data;
414 struct timeval timeout;
418 FD_SET( p_sys->i_fd, &fds );
420 timeout.tv_usec = 500000;
422 while( !( i_ret = select( p_sys->i_fd + 1, &fds,
423 NULL, NULL, &timeout ) ) )
426 FD_SET( p_sys->i_fd, &fds );
428 timeout.tv_usec = 500000;
430 if( p_input->b_die || p_input->b_error )
436 msg_Err( p_input, "select error (%s)", strerror( errno ) );
440 i_ret = read( p_sys->i_fd, p_buffer, i_len );