]> git.sesse.net Git - vlc/blob - modules/access/pvr.c
live555 : don't call ES_OUT_SET_ES_STATE with NULL pointer
[vlc] / modules / access / pvr.c
1 /*****************************************************************************
2  * pvr.c
3  *****************************************************************************
4  * Copyright (C) 2001, 2002 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Eric Petit <titer@videolan.org>
8  *          Paul Corke <paulc@datatote.co.uk>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /*****************************************************************************
26  * Preamble
27  *****************************************************************************/
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_access.h>
35 #include <vlc_fs.h>
36 #include <vlc_url.h>
37 #include <vlc_network.h>
38
39 #include <sys/types.h>
40 #include <fcntl.h>
41 #include <unistd.h>
42 #include <linux/types.h>
43 #include <sys/ioctl.h>
44 #if defined(HAVE_LINUX_VIDEODEV2_H)
45 #   include <linux/videodev2.h>
46 #elif defined(HAVE_SYS_VIDEOIO_H)
47 #   include <sys/videoio.h>
48 #else
49 #   error "No Video4Linux2 headers found."
50 #endif
51
52 /*****************************************************************************
53  * Module descriptor
54  *****************************************************************************/
55 static int  Open ( vlc_object_t * );
56 static void Close( vlc_object_t * );
57
58 #define DEVICE_TEXT N_( "Device" )
59 #define DEVICE_LONGTEXT N_( "PVR video device" )
60
61 #define RADIO_DEVICE_TEXT N_( "Radio device" )
62 #define RADIO_DEVICE_LONGTEXT N_( "PVR radio device" )
63
64 #define NORM_TEXT N_( "Norm" )
65 #define NORM_LONGTEXT N_( "Norm of the stream " \
66     "(Automatic, SECAM, PAL, or NTSC)." )
67
68 #define WIDTH_TEXT N_( "Width" )
69 #define WIDTH_LONGTEXT N_( "Width of the stream to capture " \
70     "(-1 for autodetection)." )
71
72 #define HEIGHT_TEXT N_( "Height" )
73 #define HEIGHT_LONGTEXT N_( "Height of the stream to capture " \
74     "(-1 for autodetection)." )
75
76 #define FREQUENCY_TEXT N_( "Frequency" )
77 #define FREQUENCY_LONGTEXT N_( "Frequency to capture (in kHz), if applicable." )
78
79 #define FRAMERATE_TEXT N_( "Framerate" )
80 #define FRAMERATE_LONGTEXT N_( "Framerate to capture, if applicable " \
81     "(-1 for autodetect)." )
82
83 #define KEYINT_TEXT N_( "Key interval" )
84 #define KEYINT_LONGTEXT N_( "Interval between keyframes (-1 for autodetect)." )
85
86 #define BFRAMES_TEXT N_( "B Frames" )
87 #define BFRAMES_LONGTEXT N_("If this option is set, B-Frames will be used. " \
88     "Use this option to set the number of B-Frames.")
89
90 #define BITRATE_TEXT N_( "Bitrate" )
91 #define BITRATE_LONGTEXT N_( "Bitrate to use (-1 for default)." )
92
93 #define BITRATE_PEAK_TEXT N_( "Bitrate peak" )
94 #define BITRATE_PEAK_LONGTEXT N_( "Peak bitrate in VBR mode." )
95
96 #define BITRATE_MODE_TEXT N_( "Bitrate mode" )
97 #define BITRATE_MODE_LONGTEXT N_( "Bitrate mode to use (VBR or CBR)." )
98
99 #define BITMASK_TEXT N_( "Audio bitmask" )
100 #define BITMASK_LONGTEXT N_("Bitmask that will "\
101     "get used by the audio part of the card." )
102
103 #define VOLUME_TEXT N_( "Volume" )
104 #define VOLUME_LONGTEXT N_("Audio volume (0-65535)." )
105
106 #define CHAN_TEXT N_( "Channel" )
107 #define CHAN_LONGTEXT N_( "Channel of the card to use (Usually, 0 = tuner, " \
108     "1 = composite, 2 = svideo)" )
109
110 static const int i_norm_list[] =
111     { V4L2_STD_UNKNOWN, V4L2_STD_SECAM, V4L2_STD_PAL, V4L2_STD_NTSC };
112 static const char *const psz_norm_list_text[] =
113     { N_("Automatic"), N_("SECAM"), N_("PAL"),  N_("NTSC") };
114
115 static const int i_bitrates[] = { 0, 1 };
116 static const char *const psz_bitrates_list_text[] = { N_("vbr"), N_("cbr") };
117
118 static const int pi_radio_range[2] = { 65000, 108000 };
119
120 vlc_module_begin ()
121     set_shortname( N_("PVR") )
122     set_description( N_("IVTV MPEG Encoding cards input") )
123     set_category( CAT_INPUT )
124     set_subcategory( SUBCAT_INPUT_ACCESS )
125     set_capability( "access", 0 )
126     add_shortcut( "pvr" )
127
128     add_string( "pvr-device", "/dev/video0", DEVICE_TEXT,
129                  DEVICE_LONGTEXT, false )
130     add_string( "pvr-radio-device", "/dev/radio0", RADIO_DEVICE_TEXT,
131                  RADIO_DEVICE_LONGTEXT, false )
132     add_integer( "pvr-norm", V4L2_STD_UNKNOWN , NORM_TEXT,
133                  NORM_LONGTEXT, false )
134        change_integer_list( i_norm_list, psz_norm_list_text )
135     add_integer( "pvr-width", -1, WIDTH_TEXT, WIDTH_LONGTEXT, true )
136     add_integer( "pvr-height", -1, HEIGHT_TEXT, HEIGHT_LONGTEXT,
137                  true )
138     add_integer( "pvr-frequency", -1, FREQUENCY_TEXT, FREQUENCY_LONGTEXT,
139                  false )
140     add_integer( "pvr-framerate", -1, FRAMERATE_TEXT, FRAMERATE_LONGTEXT,
141                  true )
142     add_integer( "pvr-keyint", -1, KEYINT_TEXT, KEYINT_LONGTEXT,
143                  true )
144     add_integer( "pvr-bframes", -1, FRAMERATE_TEXT, FRAMERATE_LONGTEXT,
145                  true )
146     add_integer( "pvr-bitrate", -1, BITRATE_TEXT, BITRATE_LONGTEXT,
147                  false )
148     add_integer( "pvr-bitrate-peak", -1, BITRATE_PEAK_TEXT,
149                  BITRATE_PEAK_LONGTEXT, true )
150     add_integer( "pvr-bitrate-mode", -1, BITRATE_MODE_TEXT,
151                  BITRATE_MODE_LONGTEXT, true )
152         change_integer_list( i_bitrates, psz_bitrates_list_text )
153     add_integer( "pvr-audio-bitmask", -1, BITMASK_TEXT,
154                  BITMASK_LONGTEXT, true )
155     add_integer( "pvr-audio-volume", -1, VOLUME_TEXT,
156                  VOLUME_LONGTEXT, true )
157     add_integer( "pvr-channel", -1, CHAN_TEXT, CHAN_LONGTEXT, true )
158
159     set_callbacks( Open, Close )
160 vlc_module_end ()
161
162 /*****************************************************************************
163  * Prototypes
164  *****************************************************************************/
165 static ssize_t Read   ( access_t *, uint8_t *, size_t );
166 static int Control( access_t *, int, va_list );
167
168 struct access_sys_t
169 {
170     /* file descriptor */
171     int i_fd;
172     int i_radio_fd;
173
174     char *psz_videodev;
175     char *psz_radiodev;
176
177     /* options */
178     int i_standard;
179     int i_width;
180     int i_height;
181     int i_frequency;
182     int i_framerate;
183     int i_keyint;
184     int i_bframes;
185     int i_bitrate;
186     int i_bitrate_peak;
187     int i_bitrate_mode;
188     int i_audio_bitmask;
189     int i_input;
190     int i_volume;
191 };
192
193
194 #define MAX_V4L2_CTRLS (6)
195 /*****************************************************************************
196  * AddV4L2Ctrl: adds a control to the v4l2 controls list
197  *****************************************************************************/
198 static void AddV4L2Ctrl( access_t * p_access,
199                          struct v4l2_ext_controls * p_controls,
200                          uint32_t i_id, uint32_t i_value )
201 {
202     if( p_controls->count >= MAX_V4L2_CTRLS )
203     {
204         msg_Err( p_access, "Tried to set too many v4l2 controls at once." );
205         return;
206     }
207
208     p_controls->controls[p_controls->count].id    = i_id;
209     p_controls->controls[p_controls->count].value = i_value;
210     p_controls->count++;
211 }
212
213 /*****************************************************************************
214  * V4L2SampleRate: calculate v4l2 sample rate from pvr-audio-bitmask
215  *****************************************************************************/
216 static uint32_t V4L2SampleRate( uint32_t i_bitmask )
217 {
218     switch( i_bitmask & 0x0003 )
219     {
220         case 0x0001: return V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000;
221         case 0x0002: return V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000;
222     }
223     return V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100;
224 }
225
226 /*****************************************************************************
227  * V4L2AudioEncoding: calculate v4l2 audio encoding level from pvr-audio-bitmask
228  *****************************************************************************/
229 static uint32_t V4L2AudioEncoding( uint32_t i_bitmask )
230 {
231     switch( i_bitmask & 0x000c )
232     {
233         case 0x0004: return V4L2_MPEG_AUDIO_ENCODING_LAYER_1;
234         case 0x0008: return V4L2_MPEG_AUDIO_ENCODING_LAYER_2;
235     }
236     return 0xffffffff;
237 }
238
239 /*****************************************************************************
240  * V4L2AudioL1Bitrate: calculate v4l2 audio bitrate for layer-1 audio from pvr-audio-bitmask
241  *****************************************************************************/
242 static uint32_t V4L2AudioL1Bitrate( uint32_t i_bitmask )
243 {
244     switch( i_bitmask & 0x00f0 )
245     {
246         case 0x0010: return V4L2_MPEG_AUDIO_L1_BITRATE_32K;
247         case 0x0020: return V4L2_MPEG_AUDIO_L1_BITRATE_64K;
248         case 0x0030: return V4L2_MPEG_AUDIO_L1_BITRATE_96K;
249         case 0x0040: return V4L2_MPEG_AUDIO_L1_BITRATE_128K;
250         case 0x0050: return V4L2_MPEG_AUDIO_L1_BITRATE_160K;
251         case 0x0060: return V4L2_MPEG_AUDIO_L1_BITRATE_192K;
252         case 0x0070: return V4L2_MPEG_AUDIO_L1_BITRATE_224K;
253         case 0x0080: return V4L2_MPEG_AUDIO_L1_BITRATE_256K;
254         case 0x0090: return V4L2_MPEG_AUDIO_L1_BITRATE_288K;
255         case 0x00a0: return V4L2_MPEG_AUDIO_L1_BITRATE_320K;
256         case 0x00b0: return V4L2_MPEG_AUDIO_L1_BITRATE_352K;
257         case 0x00c0: return V4L2_MPEG_AUDIO_L1_BITRATE_384K;
258         case 0x00d0: return V4L2_MPEG_AUDIO_L1_BITRATE_416K;
259         case 0x00e0: return V4L2_MPEG_AUDIO_L1_BITRATE_448K;
260     }
261     return V4L2_MPEG_AUDIO_L1_BITRATE_320K;
262 }
263
264 /*****************************************************************************
265  * V4L2AudioL2Bitrate: calculate v4l2 audio bitrate for layer-1 audio from pvr-audio-bitmask
266  *****************************************************************************/
267 static uint32_t V4L2AudioL2Bitrate( uint32_t i_bitmask )
268 {
269     switch( i_bitmask & 0x00f0 )
270     {
271         case 0x0010: return V4L2_MPEG_AUDIO_L2_BITRATE_32K;
272         case 0x0020: return V4L2_MPEG_AUDIO_L2_BITRATE_48K;
273         case 0x0030: return V4L2_MPEG_AUDIO_L2_BITRATE_56K;
274         case 0x0040: return V4L2_MPEG_AUDIO_L2_BITRATE_64K;
275         case 0x0050: return V4L2_MPEG_AUDIO_L2_BITRATE_80K;
276         case 0x0060: return V4L2_MPEG_AUDIO_L2_BITRATE_96K;
277         case 0x0070: return V4L2_MPEG_AUDIO_L2_BITRATE_112K;
278         case 0x0080: return V4L2_MPEG_AUDIO_L2_BITRATE_128K;
279         case 0x0090: return V4L2_MPEG_AUDIO_L2_BITRATE_160K;
280         case 0x00a0: return V4L2_MPEG_AUDIO_L2_BITRATE_192K;
281         case 0x00b0: return V4L2_MPEG_AUDIO_L2_BITRATE_224K;
282         case 0x00c0: return V4L2_MPEG_AUDIO_L2_BITRATE_256K;
283         case 0x00d0: return V4L2_MPEG_AUDIO_L2_BITRATE_320K;
284         case 0x00e0: return V4L2_MPEG_AUDIO_L2_BITRATE_384K;
285     }
286     return V4L2_MPEG_AUDIO_L2_BITRATE_192K;
287 }
288
289 /*****************************************************************************
290  * V4L2AudioMode: calculate v4l2 audio mode from pvr-audio-bitmask
291  *****************************************************************************/
292 static uint32_t V4L2AudioMode( uint32_t i_bitmask )
293 {
294     switch( i_bitmask & 0x0300 )
295     {
296         case 0x0100: return V4L2_MPEG_AUDIO_MODE_JOINT_STEREO;
297         case 0x0200: return V4L2_MPEG_AUDIO_MODE_DUAL;
298         case 0x0300: return V4L2_MPEG_AUDIO_MODE_MONO;
299     }
300     return V4L2_MPEG_AUDIO_MODE_STEREO;
301 }
302
303 /*****************************************************************************
304  * ConfigureV4L2: set up codec parameters using the new v4l2 api
305  *****************************************************************************/
306 static int ConfigureV4L2( access_t * p_access )
307 {
308     access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
309     struct v4l2_ext_controls controls;
310     int result;
311
312     memset( &controls, 0, sizeof(struct v4l2_ext_controls) );
313     controls.ctrl_class  = V4L2_CTRL_CLASS_MPEG;
314     controls.error_idx   = 0;
315     controls.reserved[0] = 0;
316     controls.reserved[1] = 0;
317     controls.count       = 0;
318     controls.controls    = calloc( MAX_V4L2_CTRLS,
319                                    sizeof( struct v4l2_ext_control ) );
320
321     if( controls.controls == NULL )
322         return VLC_ENOMEM;
323
324     /* Note: Ignore frame rate.  Doesn't look like it can be changed. */
325     if( p_sys->i_bitrate != -1 )
326     {
327         AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_BITRATE,
328                      p_sys->i_bitrate );
329         msg_Dbg( p_access, "Setting [%u] bitrate = %u",
330                  controls.count - 1, p_sys->i_bitrate );
331     }
332
333     if( p_sys->i_bitrate_peak != -1 )
334     {
335         AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
336                      p_sys->i_bitrate_peak );
337         msg_Dbg( p_access, "Setting [%u] bitrate_peak = %u",
338                  controls.count - 1, p_sys->i_bitrate_peak );
339     }
340
341     if( p_sys->i_bitrate_mode != -1 )
342     {
343         AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
344                      p_sys->i_bitrate_mode );
345         msg_Dbg( p_access, "Setting [%u] bitrate_mode = %u",
346                  controls.count - 1, p_sys->i_bitrate_mode );
347     }
348
349     if( p_sys->i_audio_bitmask != -1 )
350     {
351         /* Sample rate */
352         AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
353                     V4L2SampleRate( p_sys->i_audio_bitmask ) );
354
355         /* Encoding layer and bitrate */
356         switch( V4L2AudioEncoding( p_sys->i_audio_bitmask ) )
357         {
358             case V4L2_MPEG_AUDIO_ENCODING_LAYER_1:
359                  AddV4L2Ctrl( p_access, &controls,
360                               V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
361                               V4L2_MPEG_AUDIO_ENCODING_LAYER_1 );
362                  AddV4L2Ctrl( p_access, &controls,
363                               V4L2_CID_MPEG_AUDIO_L1_BITRATE,
364                               V4L2AudioL1Bitrate( p_sys->i_audio_bitmask ) );
365                  break;
366
367             case V4L2_MPEG_AUDIO_ENCODING_LAYER_2:
368                  AddV4L2Ctrl( p_access, &controls,
369                               V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
370                               V4L2_MPEG_AUDIO_ENCODING_LAYER_2 );
371                  AddV4L2Ctrl( p_access, &controls,
372                               V4L2_CID_MPEG_AUDIO_L2_BITRATE,
373                               V4L2AudioL2Bitrate( p_sys->i_audio_bitmask ) );
374                  break;
375         }
376
377         /* Audio mode - stereo or mono */
378         AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_AUDIO_MODE,
379                      V4L2AudioMode( p_sys->i_audio_bitmask ) );
380
381         /* See if the user wants any other audio feature */
382         if( ( p_sys->i_audio_bitmask & 0x1ff00 ) != 0 )
383         {
384             /* It would be possible to support the bits that represent:
385              *   V4L2_CID_MPEG_AUDIO_MODE_EXTENSION
386              *   V4L2_CID_MPEG_AUDIO_EMPHASIS
387              *   V4L2_CID_MPEG_AUDIO_CRC
388              * but they are not currently used.  Tell the user.
389              */
390             msg_Err( p_access, "There were bits in pvr-audio-bitmask that were not used.");
391         }
392         msg_Dbg( p_access, "Setting audio controls");
393     }
394
395     if( p_sys->i_keyint != -1 )
396     {
397         AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_GOP_SIZE,
398                      p_sys->i_keyint );
399         msg_Dbg( p_access, "Setting [%u] keyint = %u",
400                  controls.count - 1, p_sys->i_keyint );
401     }
402
403     if( p_sys->i_bframes != -1 )
404     {
405         AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_B_FRAMES,
406                      p_sys->i_bframes );
407         msg_Dbg( p_access, "Setting [%u] bframes = %u",
408                  controls.count - 1, p_sys->i_bframes );
409     }
410
411     result = ioctl( p_sys->i_fd, VIDIOC_S_EXT_CTRLS, &controls );
412     if( result < 0 )
413     {
414         msg_Err( p_access, "Failed to write %u new capture card settings.",
415                             controls.error_idx );
416     }
417     free( controls.controls );
418     return VLC_SUCCESS;
419 }
420
421 /*****************************************************************************
422  * Open: open the device
423  *****************************************************************************/
424 static int Open( vlc_object_t * p_this )
425 {
426     access_t *p_access = (access_t*) p_this;
427     access_sys_t * p_sys;
428     char * psz_tofree;
429     char * psz_parser;
430     struct v4l2_capability device_capability;
431     int result;
432
433     memset( &device_capability, 0, sizeof(struct v4l2_capability) );
434
435     access_InitFields( p_access );
436     ACCESS_SET_CALLBACKS( Read, NULL, Control, NULL );
437     p_sys = p_access->p_sys = calloc( 1, sizeof( access_sys_t ));
438     if( !p_sys ) return VLC_ENOMEM;
439
440     /* defaults values */
441     p_sys->psz_videodev = var_InheritString( p_access, "pvr-device" );
442     p_sys->psz_radiodev = var_InheritString( p_access, "pvr-radio-device" );
443     p_sys->i_standard   = var_InheritInteger( p_access, "pvr-norm" );
444     p_sys->i_width      = var_InheritInteger( p_access, "pvr-width" );
445     p_sys->i_height     = var_InheritInteger( p_access, "pvr-height" );
446     p_sys->i_frequency  = var_InheritInteger( p_access, "pvr-frequency" );
447     p_sys->i_framerate  = var_InheritInteger( p_access, "pvr-framerate" );
448     p_sys->i_keyint     = var_InheritInteger( p_access, "pvr-keyint" );
449     p_sys->i_bframes    = var_InheritInteger( p_access, "pvr-bframes" );
450     p_sys->i_bitrate    = var_InheritInteger( p_access, "pvr-bitrate" );
451     p_sys->i_bitrate_peak  = var_InheritInteger( p_access, "pvr-bitrate-peak" );
452     p_sys->i_bitrate_mode  = var_InheritInteger( p_access, "pvr-bitrate-mode" );
453     p_sys->i_audio_bitmask = var_InheritInteger( p_access, "pvr-audio-bitmask" );
454     p_sys->i_volume     = var_InheritInteger( p_access, "pvr-audio-volume" );
455     p_sys->i_input      = var_InheritInteger( p_access, "pvr-channel" );
456
457     /* parse command line options */
458     psz_tofree = strdup( p_access->psz_location );
459     if( !psz_tofree )
460     {
461         free( p_sys->psz_radiodev );
462         free( p_sys->psz_videodev );
463         free( p_sys );
464         return VLC_ENOMEM;
465     }
466
467     psz_parser = psz_tofree;
468     while( *psz_parser )
469     {
470         /* Leading slash -> device path */
471         if( *psz_parser == '/' )
472         {
473             free( p_sys->psz_videodev );
474             p_sys->psz_videodev = decode_URI_duplicate( psz_parser );
475             break;
476         }
477
478         /* Extract option name */
479         const char *optname = psz_parser;
480         psz_parser = strchr( psz_parser, '=' );
481         if( psz_parser == NULL )
482             break;
483         *psz_parser++ = '\0';
484
485         /* Extract option value */
486         char *optval = psz_parser;
487         while( memchr( ":,", *psz_parser, 3 /* includes \0 */ ) == NULL )
488             psz_parser++;
489         if( *psz_parser ) /* more options to come */
490             *psz_parser++ = '\0'; /* skip , or : */
491
492         if ( !strcmp( optname, "norm" ) )
493         {
494             if ( !strcmp( optval, "secam" ) )
495                 p_sys->i_standard = V4L2_STD_SECAM;
496             else if ( !strcmp( optval, "pal" ) )
497                 p_sys->i_standard = V4L2_STD_PAL;
498             else if ( !strcmp( optval, "ntsc" ) )
499                 p_sys->i_standard = V4L2_STD_NTSC;
500             else
501                 p_sys->i_standard = atoi( optval );
502         }
503         else if( !strcmp( optname, "channel" ) )
504             p_sys->i_input = atoi( optval );
505         else if( !strcmp( optname, "device" ) )
506         {
507             free( p_sys->psz_videodev );
508             if( asprintf( &p_sys->psz_videodev, "/dev/video%s", optval ) == -1)
509                 p_sys->psz_videodev = NULL;
510         }
511         else if( !strcmp( optname, "frequency" ) )
512             p_sys->i_frequency = atoi( optval );
513         else if( !strcmp( optname, "framerate" ) )
514             p_sys->i_framerate = atoi( optval );
515         else if( !strcmp( optname, "keyint" ) )
516             p_sys->i_keyint = atoi( optval );
517         else if( !strcmp( optname, "bframes" ) )
518             p_sys->i_bframes = atoi( optval );
519         else if( !strcmp( optname, "width" ) )
520             p_sys->i_width = atoi( optval );
521         else if( !strcmp( optname, "height" ) )
522             p_sys->i_height = atoi( optval );
523         else if( !strcmp( optname, "audio" ) )
524             p_sys->i_audio_bitmask = atoi( optval );
525         else if( !strcmp( optname, "bitrate" ) )
526             p_sys->i_bitrate = atoi( optval );
527         else if( !strcmp( optname, "maxbitrate" ) )
528             p_sys->i_bitrate_peak = atoi( optval );
529         else if( !strcmp( optname, "bitratemode" ) )
530         {
531             if( !strcmp( optval, "vbr" ) )
532                 p_sys->i_bitrate_mode = 0;
533             else if( !strcmp( optval, "cbr" ) )
534                 p_sys->i_bitrate_mode = 1;
535         }
536         else if( !strcmp( optname, "size" ) )
537         {
538             p_sys->i_width = strtol( optval, &optval, 0 );
539             p_sys->i_height = atoi( optval );
540         }
541     }
542     free( psz_tofree );
543
544     /* open the device */
545     p_sys->i_fd = vlc_open( p_sys->psz_videodev, O_RDWR );
546     if( p_sys->i_fd < 0 )
547     {
548         msg_Err( p_access, "Cannot open device %s (%m).",
549                  p_sys->psz_videodev );
550         Close( VLC_OBJECT(p_access) );
551         return VLC_EGENERIC;
552     }
553     msg_Dbg( p_access, "Using video device: %s.", p_sys->psz_videodev);
554
555     /* See what version of ivtvdriver is running */
556     result = ioctl( p_sys->i_fd, VIDIOC_QUERYCAP, &device_capability );
557     if( result < 0 )
558     {
559         msg_Err( p_access, "unknown ivtv/pvr driver version in use" );
560         Close( VLC_OBJECT(p_access) );
561         return VLC_EGENERIC;
562     }
563
564     msg_Dbg( p_access, "%s driver (%s on %s) version %02x.%02x.%02x",
565               device_capability.driver,
566               device_capability.card,
567               device_capability.bus_info,
568             ( device_capability.version >> 16 ) & 0xff,
569             ( device_capability.version >>  8 ) & 0xff,
570             ( device_capability.version       ) & 0xff);
571
572     /* set the input */
573     if ( p_sys->i_input != -1 )
574     {
575         result = ioctl( p_sys->i_fd, VIDIOC_S_INPUT, &p_sys->i_input );
576         if ( result < 0 )
577             msg_Warn( p_access, "Failed to select the requested input pin." );
578         else
579             msg_Dbg( p_access, "input set to: %d", p_sys->i_input );
580     }
581
582     /* set the video standard */
583     if ( p_sys->i_standard != V4L2_STD_UNKNOWN )
584     {
585         result = ioctl( p_sys->i_fd, VIDIOC_S_STD, &p_sys->i_standard );
586         if ( result  < 0 )
587             msg_Warn( p_access, "Failed to set the requested video standard." );
588         else
589             msg_Dbg( p_access, "video standard set to: %x",
590                      p_sys->i_standard);
591     }
592
593     /* set the picture size */
594     if ( (p_sys->i_width != -1) || (p_sys->i_height != -1) )
595     {
596         struct v4l2_format vfmt;
597
598         memset( &vfmt, 0, sizeof(struct v4l2_format) );
599         vfmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
600
601         result = ioctl( p_sys->i_fd, VIDIOC_G_FMT, &vfmt );
602         if ( result < 0 )
603         {
604             msg_Warn( p_access, "Failed to read current picture size." );
605         }
606         else
607         {
608             if ( p_sys->i_width != -1 )
609             {
610                 vfmt.fmt.pix.width = p_sys->i_width;
611             }
612
613             if ( p_sys->i_height != -1 )
614             {
615                 vfmt.fmt.pix.height = p_sys->i_height;
616             }
617
618             result = ioctl( p_sys->i_fd, VIDIOC_S_FMT, &vfmt );
619             if ( result < 0 )
620             {
621                 msg_Warn( p_access, "Failed to set requested picture size." );
622             }
623             else
624             {
625                 msg_Dbg( p_access, "picture size set to: %dx%d",
626                          vfmt.fmt.pix.width, vfmt.fmt.pix.height );
627             }
628         }
629     }
630
631     /* set the frequency */
632     if ( p_sys->i_frequency != -1 )
633     {
634         int i_fd;
635         struct v4l2_tuner vt;
636
637          /* TODO: let the user choose the tuner */
638         memset( &vt, 0, sizeof(struct v4l2_tuner) );
639
640         if ( (p_sys->i_frequency >= pi_radio_range[0])
641               && (p_sys->i_frequency <= pi_radio_range[1]) )
642         {
643             p_sys->i_radio_fd = vlc_open( p_sys->psz_radiodev, O_RDWR );
644             if( p_sys->i_radio_fd < 0 )
645             {
646                 msg_Err( p_access, "Cannot open radio device (%m)." );
647                 Close( VLC_OBJECT(p_access) );
648                 return VLC_EGENERIC;
649             }
650             msg_Dbg( p_access, "using radio device: %s",
651                      p_sys->psz_radiodev );
652             i_fd = p_sys->i_radio_fd;
653         }
654         else
655         {
656             i_fd = p_sys->i_fd;
657             p_sys->i_radio_fd = -1;
658         }
659
660         result = ioctl( i_fd, VIDIOC_G_TUNER, &vt );
661         if ( result < 0 )
662         {
663             msg_Warn( p_access, "Failed to read tuner information (%m)." );
664         }
665         else
666         {
667             struct v4l2_frequency vf;
668
669             memset( &vf, 0, sizeof(struct v4l2_frequency) );
670             vf.tuner = vt.index;
671
672             result = ioctl( i_fd, VIDIOC_G_FREQUENCY, &vf );
673             if ( result < 0 )
674             {
675                 msg_Warn( p_access, "Failed to read tuner frequency (%m)." );
676             }
677             else
678             {
679                 if( vt.capability & V4L2_TUNER_CAP_LOW )
680                     vf.frequency = p_sys->i_frequency * 16;
681                 else
682                     vf.frequency = (p_sys->i_frequency * 16 + 500) / 1000;
683
684                 result = ioctl( i_fd, VIDIOC_S_FREQUENCY, &vf );
685                 if( result < 0 )
686                 {
687                     msg_Warn( p_access, "Failed to set tuner frequency (%m)." );
688                 }
689                 else
690                 {
691                     msg_Dbg( p_access, "tuner frequency set to: %d",
692                              p_sys->i_frequency );
693                 }
694             }
695         }
696     }
697
698     /* control parameters */
699     if ( p_sys->i_volume != -1 )
700     {
701         struct v4l2_control ctrl;
702
703         memset( &ctrl, 0, sizeof(struct v4l2_control) );
704         ctrl.id = V4L2_CID_AUDIO_VOLUME;
705         ctrl.value = p_sys->i_volume;
706
707         result = ioctl( p_sys->i_fd, VIDIOC_S_CTRL, &ctrl );
708         if ( result < 0 )
709         {
710             msg_Warn( p_access, "Failed to set the volume." );
711         }
712     }
713
714     /* codec parameters */
715     if ( (p_sys->i_framerate != -1)
716             || (p_sys->i_bitrate_mode != -1)
717             || (p_sys->i_bitrate_peak != -1)
718             || (p_sys->i_keyint != -1)
719             || (p_sys->i_bframes != -1)
720             || (p_sys->i_bitrate != -1)
721             || (p_sys->i_audio_bitmask != -1) )
722     {
723         result = ConfigureV4L2( p_access );
724         if( result != VLC_SUCCESS )
725         {
726             Close( VLC_OBJECT(p_access) );
727             return result;
728         }
729     }
730
731     return VLC_SUCCESS;
732 }
733
734 /*****************************************************************************
735  * Close: close the device
736  *****************************************************************************/
737 static void Close( vlc_object_t * p_this )
738 {
739     access_t *p_access = (access_t*) p_this;
740     access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
741
742     if ( p_sys->i_fd != -1 )
743         close( p_sys->i_fd );
744     if ( p_sys->i_radio_fd != -1 )
745         close( p_sys->i_radio_fd );
746     free( p_sys->psz_videodev );
747     free( p_sys->psz_radiodev );
748     free( p_sys );
749 }
750
751 /*****************************************************************************
752  * Read
753  *****************************************************************************/
754 static ssize_t Read( access_t * p_access, uint8_t * p_buffer, size_t i_len )
755 {
756     access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
757     ssize_t i_ret;
758
759     if( p_access->info.b_eof )
760         return 0;
761
762     i_ret = net_Read( p_access, p_sys->i_fd, NULL, p_buffer, i_len, false );
763     if( i_ret == 0 )
764     {
765         p_access->info.b_eof = true;
766     }
767     else if( i_ret > 0 )
768     {
769         p_access->info.i_pos += i_ret;
770     }
771
772     return i_ret;
773 }
774
775 /*****************************************************************************
776  * Control
777  *****************************************************************************/
778 static int Control( access_t *p_access, int i_query, va_list args )
779 {
780     bool   *pb_bool;
781     int64_t      *pi_64;
782
783     switch( i_query )
784     {
785         /* */
786         case ACCESS_CAN_SEEK:
787         case ACCESS_CAN_FASTSEEK:
788             pb_bool = (bool*)va_arg( args, bool* );
789             *pb_bool = false;
790             break;
791         case ACCESS_CAN_PAUSE:
792             pb_bool = (bool*)va_arg( args, bool* );
793             *pb_bool = false;
794             break;
795         case ACCESS_CAN_CONTROL_PACE:
796             pb_bool = (bool*)va_arg( args, bool* );
797             *pb_bool = false;
798             break;
799
800         /* */
801         case ACCESS_GET_PTS_DELAY:
802             pi_64 = (int64_t*)va_arg( args, int64_t * );
803             *pi_64 = INT64_C(1000)
804                    * var_InheritInteger( p_access, "live-caching" );
805             break;
806
807         /* */
808         case ACCESS_SET_PAUSE_STATE:
809             /* Nothing to do */
810             break;
811
812         case ACCESS_GET_TITLE_INFO:
813         case ACCESS_SET_TITLE:
814         case ACCESS_SET_SEEKPOINT:
815         case ACCESS_SET_PRIVATE_ID_STATE:
816         case ACCESS_GET_CONTENT_TYPE:
817             return VLC_EGENERIC;
818
819         default:
820             msg_Warn( p_access, "Unimplemented query in control." );
821             return VLC_EGENERIC;
822
823     }
824     return VLC_SUCCESS;
825 }