]> git.sesse.net Git - vlc/blob - modules/access/pvr.c
Configure: fix typo
[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 /* ivtv specific ioctls */
169 #define IVTV_IOC_G_CODEC    0xFFEE7703
170 #define IVTV_IOC_S_CODEC    0xFFEE7704
171
172 /* for use with IVTV_IOC_G_CODEC and IVTV_IOC_S_CODEC */
173
174 struct ivtv_ioctl_codec {
175     uint32_t aspect;
176     uint32_t audio_bitmask;
177     uint32_t bframes;
178     uint32_t bitrate_mode;
179     uint32_t bitrate;
180     uint32_t bitrate_peak;
181     uint32_t dnr_mode;
182     uint32_t dnr_spatial;
183     uint32_t dnr_temporal;
184     uint32_t dnr_type;
185     uint32_t framerate;
186     uint32_t framespergop;
187     uint32_t gop_closure;
188     uint32_t pulldown;
189     uint32_t stream_type;
190 };
191
192 struct access_sys_t
193 {
194     /* file descriptor */
195     int i_fd;
196     int i_radio_fd;
197
198     char *psz_videodev;
199     char *psz_radiodev;
200
201     /* options */
202     int i_standard;
203     int i_width;
204     int i_height;
205     int i_frequency;
206     int i_framerate;
207     int i_keyint;
208     int i_bframes;
209     int i_bitrate;
210     int i_bitrate_peak;
211     int i_bitrate_mode;
212     int i_audio_bitmask;
213     int i_input;
214     int i_volume;
215
216     /* driver version */
217     bool b_v4l2_api;
218 };
219
220 /*****************************************************************************
221  * ConfigureIVTV: set up codec parameters using the old ivtv api
222  *****************************************************************************/
223 static int ConfigureIVTV( access_t * p_access )
224 {
225     access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
226     struct ivtv_ioctl_codec codec;
227     int result;
228
229     memset( &codec, 0, sizeof(struct ivtv_ioctl_codec) );
230
231     result = ioctl( p_sys->i_fd, IVTV_IOC_G_CODEC, &codec );
232     if( result < 0 )
233     {
234         msg_Err( p_access, "Failed to read current capture card settings." );
235         return VLC_EGENERIC;
236     }
237
238     if( p_sys->i_framerate != -1 )
239     {
240         switch( p_sys->i_framerate )
241         {
242             case 30:
243                 codec.framerate = 0;
244                 break;
245
246             case 25:
247                 codec.framerate = 1;
248                 break;
249
250             default:
251                 msg_Warn( p_access, "Invalid framerate, reverting to 25." );
252                 codec.framerate = 1;
253                 break;
254         }
255     }
256
257     if( p_sys->i_bitrate != -1 )
258     {
259         codec.bitrate = p_sys->i_bitrate;
260     }
261
262     if( p_sys->i_bitrate_peak != -1 )
263     {
264         codec.bitrate_peak = p_sys->i_bitrate_peak;
265     }
266
267     if( p_sys->i_bitrate_mode != -1 )
268     {
269         codec.bitrate_mode = p_sys->i_bitrate_mode;
270     }
271
272     if( p_sys->i_audio_bitmask != -1 )
273     {
274         codec.audio_bitmask = p_sys->i_audio_bitmask;
275     }
276
277     if( p_sys->i_keyint != -1 )
278     {
279         codec.framespergop = p_sys->i_keyint;
280     }
281
282     if( p_sys->i_bframes != -1 )
283     {
284         codec.bframes = p_sys->i_bframes;
285     }
286
287     result = ioctl( p_sys->i_fd, IVTV_IOC_S_CODEC, &codec );
288     if( result  < 0 )
289     {
290         msg_Err( p_access, "Failed to write new capture card settings." );
291         return VLC_EGENERIC;
292     }
293
294     msg_Dbg( p_access, "Setting codec parameters to:  framerate: "
295                         "%d, bitrate: %d/%d/%d",
296                         codec.framerate, codec.bitrate,
297                         codec.bitrate_peak, codec.bitrate_mode );
298     return VLC_SUCCESS;
299 }
300
301 #define MAX_V4L2_CTRLS (6)
302 /*****************************************************************************
303  * AddV4L2Ctrl: adds a control to the v4l2 controls list
304  *****************************************************************************/
305 static void AddV4L2Ctrl( access_t * p_access,
306                          struct v4l2_ext_controls * p_controls,
307                          uint32_t i_id, uint32_t i_value )
308 {
309     if( p_controls->count >= MAX_V4L2_CTRLS )
310     {
311         msg_Err( p_access, "Tried to set too many v4l2 controls at once." );
312         return;
313     }
314
315     p_controls->controls[p_controls->count].id    = i_id;
316     p_controls->controls[p_controls->count].value = i_value;
317     p_controls->count++;
318 }
319
320 /*****************************************************************************
321  * V4L2SampleRate: calculate v4l2 sample rate from pvr-audio-bitmask
322  *****************************************************************************/
323 static uint32_t V4L2SampleRate( uint32_t i_bitmask )
324 {
325     switch( i_bitmask & 0x0003 )
326     {
327         case 0x0001: return V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000;
328         case 0x0002: return V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000;
329     }
330     return V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100;
331 }
332
333 /*****************************************************************************
334  * V4L2AudioEncoding: calculate v4l2 audio encoding level from pvr-audio-bitmask
335  *****************************************************************************/
336 static uint32_t V4L2AudioEncoding( uint32_t i_bitmask )
337 {
338     switch( i_bitmask & 0x000c )
339     {
340         case 0x0004: return V4L2_MPEG_AUDIO_ENCODING_LAYER_1;
341         case 0x0008: return V4L2_MPEG_AUDIO_ENCODING_LAYER_2;
342     }
343     return 0xffffffff;
344 }
345
346 /*****************************************************************************
347  * V4L2AudioL1Bitrate: calculate v4l2 audio bitrate for layer-1 audio from pvr-audio-bitmask
348  *****************************************************************************/
349 static uint32_t V4L2AudioL1Bitrate( uint32_t i_bitmask )
350 {
351     switch( i_bitmask & 0x00f0 )
352     {
353         case 0x0010: return V4L2_MPEG_AUDIO_L1_BITRATE_32K;
354         case 0x0020: return V4L2_MPEG_AUDIO_L1_BITRATE_64K;
355         case 0x0030: return V4L2_MPEG_AUDIO_L1_BITRATE_96K;
356         case 0x0040: return V4L2_MPEG_AUDIO_L1_BITRATE_128K;
357         case 0x0050: return V4L2_MPEG_AUDIO_L1_BITRATE_160K;
358         case 0x0060: return V4L2_MPEG_AUDIO_L1_BITRATE_192K;
359         case 0x0070: return V4L2_MPEG_AUDIO_L1_BITRATE_224K;
360         case 0x0080: return V4L2_MPEG_AUDIO_L1_BITRATE_256K;
361         case 0x0090: return V4L2_MPEG_AUDIO_L1_BITRATE_288K;
362         case 0x00a0: return V4L2_MPEG_AUDIO_L1_BITRATE_320K;
363         case 0x00b0: return V4L2_MPEG_AUDIO_L1_BITRATE_352K;
364         case 0x00c0: return V4L2_MPEG_AUDIO_L1_BITRATE_384K;
365         case 0x00d0: return V4L2_MPEG_AUDIO_L1_BITRATE_416K;
366         case 0x00e0: return V4L2_MPEG_AUDIO_L1_BITRATE_448K;
367     }
368     return V4L2_MPEG_AUDIO_L1_BITRATE_320K;
369 }
370
371 /*****************************************************************************
372  * V4L2AudioL2Bitrate: calculate v4l2 audio bitrate for layer-1 audio from pvr-audio-bitmask
373  *****************************************************************************/
374 static uint32_t V4L2AudioL2Bitrate( uint32_t i_bitmask )
375 {
376     switch( i_bitmask & 0x00f0 )
377     {
378         case 0x0010: return V4L2_MPEG_AUDIO_L2_BITRATE_32K;
379         case 0x0020: return V4L2_MPEG_AUDIO_L2_BITRATE_48K;
380         case 0x0030: return V4L2_MPEG_AUDIO_L2_BITRATE_56K;
381         case 0x0040: return V4L2_MPEG_AUDIO_L2_BITRATE_64K;
382         case 0x0050: return V4L2_MPEG_AUDIO_L2_BITRATE_80K;
383         case 0x0060: return V4L2_MPEG_AUDIO_L2_BITRATE_96K;
384         case 0x0070: return V4L2_MPEG_AUDIO_L2_BITRATE_112K;
385         case 0x0080: return V4L2_MPEG_AUDIO_L2_BITRATE_128K;
386         case 0x0090: return V4L2_MPEG_AUDIO_L2_BITRATE_160K;
387         case 0x00a0: return V4L2_MPEG_AUDIO_L2_BITRATE_192K;
388         case 0x00b0: return V4L2_MPEG_AUDIO_L2_BITRATE_224K;
389         case 0x00c0: return V4L2_MPEG_AUDIO_L2_BITRATE_256K;
390         case 0x00d0: return V4L2_MPEG_AUDIO_L2_BITRATE_320K;
391         case 0x00e0: return V4L2_MPEG_AUDIO_L2_BITRATE_384K;
392     }
393     return V4L2_MPEG_AUDIO_L2_BITRATE_192K;
394 }
395
396 /*****************************************************************************
397  * V4L2AudioMode: calculate v4l2 audio mode from pvr-audio-bitmask
398  *****************************************************************************/
399 static uint32_t V4L2AudioMode( uint32_t i_bitmask )
400 {
401     switch( i_bitmask & 0x0300 )
402     {
403         case 0x0100: return V4L2_MPEG_AUDIO_MODE_JOINT_STEREO;
404         case 0x0200: return V4L2_MPEG_AUDIO_MODE_DUAL;
405         case 0x0300: return V4L2_MPEG_AUDIO_MODE_MONO;
406     }
407     return V4L2_MPEG_AUDIO_MODE_STEREO;
408 }
409
410 /*****************************************************************************
411  * ConfigureV4L2: set up codec parameters using the new v4l2 api
412  *****************************************************************************/
413 static int ConfigureV4L2( access_t * p_access )
414 {
415     access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
416     struct v4l2_ext_controls controls;
417     int result;
418
419     memset( &controls, 0, sizeof(struct v4l2_ext_controls) );
420     controls.ctrl_class  = V4L2_CTRL_CLASS_MPEG;
421     controls.error_idx   = 0;
422     controls.reserved[0] = 0;
423     controls.reserved[1] = 0;
424     controls.count       = 0;
425     controls.controls    = calloc( MAX_V4L2_CTRLS,
426                                    sizeof( struct v4l2_ext_control ) );
427
428     if( controls.controls == NULL )
429         return VLC_ENOMEM;
430
431     /* Note: Ignore frame rate.  Doesn't look like it can be changed. */
432     if( p_sys->i_bitrate != -1 )
433     {
434         AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_BITRATE,
435                      p_sys->i_bitrate );
436         msg_Dbg( p_access, "Setting [%u] bitrate = %u",
437                  controls.count - 1, p_sys->i_bitrate );
438     }
439
440     if( p_sys->i_bitrate_peak != -1 )
441     {
442         AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
443                      p_sys->i_bitrate_peak );
444         msg_Dbg( p_access, "Setting [%u] bitrate_peak = %u",
445                  controls.count - 1, p_sys->i_bitrate_peak );
446     }
447
448     if( p_sys->i_bitrate_mode != -1 )
449     {
450         AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
451                      p_sys->i_bitrate_mode );
452         msg_Dbg( p_access, "Setting [%u] bitrate_mode = %u",
453                  controls.count - 1, p_sys->i_bitrate_mode );
454     }
455
456     if( p_sys->i_audio_bitmask != -1 )
457     {
458         /* Sample rate */
459         AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
460                     V4L2SampleRate( p_sys->i_audio_bitmask ) );
461
462         /* Encoding layer and bitrate */
463         switch( V4L2AudioEncoding( p_sys->i_audio_bitmask ) )
464         {
465             case V4L2_MPEG_AUDIO_ENCODING_LAYER_1:
466                  AddV4L2Ctrl( p_access, &controls,
467                               V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
468                               V4L2_MPEG_AUDIO_ENCODING_LAYER_1 );
469                  AddV4L2Ctrl( p_access, &controls,
470                               V4L2_CID_MPEG_AUDIO_L1_BITRATE,
471                               V4L2AudioL1Bitrate( p_sys->i_audio_bitmask ) );
472                  break;
473
474             case V4L2_MPEG_AUDIO_ENCODING_LAYER_2:
475                  AddV4L2Ctrl( p_access, &controls,
476                               V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
477                               V4L2_MPEG_AUDIO_ENCODING_LAYER_2 );
478                  AddV4L2Ctrl( p_access, &controls,
479                               V4L2_CID_MPEG_AUDIO_L2_BITRATE,
480                               V4L2AudioL2Bitrate( p_sys->i_audio_bitmask ) );
481                  break;
482         }
483
484         /* Audio mode - stereo or mono */
485         AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_AUDIO_MODE,
486                      V4L2AudioMode( p_sys->i_audio_bitmask ) );
487
488         /* See if the user wants any other audio feature */
489         if( ( p_sys->i_audio_bitmask & 0x1ff00 ) != 0 )
490         {
491             /* It would be possible to support the bits that represent:
492              *   V4L2_CID_MPEG_AUDIO_MODE_EXTENSION
493              *   V4L2_CID_MPEG_AUDIO_EMPHASIS
494              *   V4L2_CID_MPEG_AUDIO_CRC
495              * but they are not currently used.  Tell the user.
496              */
497             msg_Err( p_access, "There were bits in pvr-audio-bitmask that were not used.");
498         }
499         msg_Dbg( p_access, "Setting audio controls");
500     }
501
502     if( p_sys->i_keyint != -1 )
503     {
504         AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_GOP_SIZE,
505                      p_sys->i_keyint );
506         msg_Dbg( p_access, "Setting [%u] keyint = %u",
507                  controls.count - 1, p_sys->i_keyint );
508     }
509
510     if( p_sys->i_bframes != -1 )
511     {
512         AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_B_FRAMES,
513                      p_sys->i_bframes );
514         msg_Dbg( p_access, "Setting [%u] bframes = %u",
515                  controls.count - 1, p_sys->i_bframes );
516     }
517
518     result = ioctl( p_sys->i_fd, VIDIOC_S_EXT_CTRLS, &controls );
519     if( result < 0 )
520     {
521         msg_Err( p_access, "Failed to write %u new capture card settings.",
522                             controls.error_idx );
523     }
524     free( controls.controls );
525     return VLC_SUCCESS;
526 }
527
528 /*****************************************************************************
529  * Open: open the device
530  *****************************************************************************/
531 static int Open( vlc_object_t * p_this )
532 {
533     access_t *p_access = (access_t*) p_this;
534     access_sys_t * p_sys;
535     char * psz_tofree;
536     char * psz_parser;
537     struct v4l2_capability device_capability;
538     int result;
539
540     memset( &device_capability, 0, sizeof(struct v4l2_capability) );
541
542     access_InitFields( p_access );
543     ACCESS_SET_CALLBACKS( Read, NULL, Control, NULL );
544     p_sys = p_access->p_sys = calloc( 1, sizeof( access_sys_t ));
545     if( !p_sys ) return VLC_ENOMEM;
546
547     /* defaults values */
548     p_sys->psz_videodev = var_InheritString( p_access, "pvr-device" );
549     p_sys->psz_radiodev = var_InheritString( p_access, "pvr-radio-device" );
550     p_sys->i_standard   = var_InheritInteger( p_access, "pvr-norm" );
551     p_sys->i_width      = var_InheritInteger( p_access, "pvr-width" );
552     p_sys->i_height     = var_InheritInteger( p_access, "pvr-height" );
553     p_sys->i_frequency  = var_InheritInteger( p_access, "pvr-frequency" );
554     p_sys->i_framerate  = var_InheritInteger( p_access, "pvr-framerate" );
555     p_sys->i_keyint     = var_InheritInteger( p_access, "pvr-keyint" );
556     p_sys->i_bframes    = var_InheritInteger( p_access, "pvr-bframes" );
557     p_sys->i_bitrate    = var_InheritInteger( p_access, "pvr-bitrate" );
558     p_sys->i_bitrate_peak  = var_InheritInteger( p_access, "pvr-bitrate-peak" );
559     p_sys->i_bitrate_mode  = var_InheritInteger( p_access, "pvr-bitrate-mode" );
560     p_sys->i_audio_bitmask = var_InheritInteger( p_access, "pvr-audio-bitmask" );
561     p_sys->i_volume     = var_InheritInteger( p_access, "pvr-audio-volume" );
562     p_sys->i_input      = var_InheritInteger( p_access, "pvr-channel" );
563
564     /* parse command line options */
565     psz_tofree = strdup( p_access->psz_location );
566     if( !psz_tofree )
567     {
568         free( p_sys->psz_radiodev );
569         free( p_sys->psz_videodev );
570         free( p_sys );
571         return VLC_ENOMEM;
572     }
573
574     psz_parser = psz_tofree;
575     while( *psz_parser )
576     {
577         /* Leading slash -> device path */
578         if( *psz_parser == '/' )
579         {
580             free( p_sys->psz_videodev );
581             p_sys->psz_videodev = decode_URI_duplicate( psz_parser );
582             break;
583         }
584
585         /* Extract option name */
586         const char *optname = psz_parser;
587         psz_parser = strchr( psz_parser, '=' );
588         if( psz_parser == NULL )
589             break;
590         *psz_parser++ = '\0';
591
592         /* Extract option value */
593         char *optval = psz_parser;
594         while( memchr( ":,", *psz_parser, 3 /* includes \0 */ ) == NULL )
595             psz_parser++;
596         if( *psz_parser ) /* more options to come */
597             *psz_parser++ = '\0'; /* skip , or : */
598
599         if ( !strcmp( optname, "norm" ) )
600         {
601             if ( !strcmp( optval, "secam" ) )
602                 p_sys->i_standard = V4L2_STD_SECAM;
603             else if ( !strcmp( optval, "pal" ) )
604                 p_sys->i_standard = V4L2_STD_PAL;
605             else if ( !strcmp( optval, "ntsc" ) )
606                 p_sys->i_standard = V4L2_STD_NTSC;
607             else
608                 p_sys->i_standard = atoi( optval );
609         }
610         else if( !strcmp( optname, "channel" ) )
611             p_sys->i_input = atoi( optval );
612         else if( !strcmp( optname, "device" ) )
613         {
614             free( p_sys->psz_videodev );
615             if( asprintf( &p_sys->psz_videodev, "/dev/video%s", optval ) == -1)
616                 p_sys->psz_videodev = NULL;
617         }
618         else if( !strcmp( optname, "frequency" ) )
619             p_sys->i_frequency = atoi( optval );
620         else if( !strcmp( optname, "framerate" ) )
621             p_sys->i_framerate = atoi( optval );
622         else if( !strcmp( optname, "keyint" ) )
623             p_sys->i_keyint = atoi( optval );
624         else if( !strcmp( optname, "bframes" ) )
625             p_sys->i_bframes = atoi( optval );
626         else if( !strcmp( optname, "width" ) )
627             p_sys->i_width = atoi( optval );
628         else if( !strcmp( optname, "height" ) )
629             p_sys->i_height = atoi( optval );
630         else if( !strcmp( optname, "audio" ) )
631             p_sys->i_audio_bitmask = atoi( optval );
632         else if( !strcmp( optname, "bitrate" ) )
633             p_sys->i_bitrate = atoi( optval );
634         else if( !strcmp( optname, "maxbitrate" ) )
635             p_sys->i_bitrate_peak = atoi( optval );
636         else if( !strcmp( optname, "bitratemode" ) )
637         {
638             if( !strcmp( optval, "vbr" ) )
639                 p_sys->i_bitrate_mode = 0;
640             else if( !strcmp( optval, "cbr" ) )
641                 p_sys->i_bitrate_mode = 1;
642         }
643         else if( !strcmp( optname, "size" ) )
644         {
645             p_sys->i_width = strtol( optval, &optval, 0 );
646             p_sys->i_height = atoi( optval );
647         }
648     }
649     free( psz_tofree );
650
651     /* open the device */
652     p_sys->i_fd = vlc_open( p_sys->psz_videodev, O_RDWR );
653     if( p_sys->i_fd < 0 )
654     {
655         msg_Err( p_access, "Cannot open device %s (%m).",
656                  p_sys->psz_videodev );
657         Close( VLC_OBJECT(p_access) );
658         return VLC_EGENERIC;
659     }
660     msg_Dbg( p_access, "Using video device: %s.", p_sys->psz_videodev);
661
662     /* See what version of ivtvdriver is running */
663     result = ioctl( p_sys->i_fd, VIDIOC_QUERYCAP, &device_capability );
664     if( result < 0 )
665     {
666         msg_Err( p_access, "unknown ivtv/pvr driver version in use" );
667         Close( VLC_OBJECT(p_access) );
668         return VLC_EGENERIC;
669     }
670
671     msg_Dbg( p_access, "%s driver (%s on %s) version %02x.%02x.%02x",
672               device_capability.driver,
673               device_capability.card,
674               device_capability.bus_info,
675             ( device_capability.version >> 16 ) & 0xff,
676             ( device_capability.version >>  8 ) & 0xff,
677             ( device_capability.version       ) & 0xff);
678
679     if ( strncmp( (char *) device_capability.driver, "ivtv", 4 )
680            || device_capability.version >= 0x000800 )
681     {
682         /* Drivers > 0.8.0 use v4l2 API instead of IVTV ioctls */
683         msg_Dbg( p_access, "this driver uses the v4l2 API" );
684         p_sys->b_v4l2_api = true;
685     }
686     else
687     {
688         p_sys->b_v4l2_api = false;
689     }
690
691     /* set the input */
692     if ( p_sys->i_input != -1 )
693     {
694         result = ioctl( p_sys->i_fd, VIDIOC_S_INPUT, &p_sys->i_input );
695         if ( result < 0 )
696             msg_Warn( p_access, "Failed to select the requested input pin." );
697         else
698             msg_Dbg( p_access, "input set to: %d", p_sys->i_input );
699     }
700
701     /* set the video standard */
702     if ( p_sys->i_standard != V4L2_STD_UNKNOWN )
703     {
704         result = ioctl( p_sys->i_fd, VIDIOC_S_STD, &p_sys->i_standard );
705         if ( result  < 0 )
706             msg_Warn( p_access, "Failed to set the requested video standard." );
707         else
708             msg_Dbg( p_access, "video standard set to: %x",
709                      p_sys->i_standard);
710     }
711
712     /* set the picture size */
713     if ( (p_sys->i_width != -1) || (p_sys->i_height != -1) )
714     {
715         struct v4l2_format vfmt;
716
717         memset( &vfmt, 0, sizeof(struct v4l2_format) );
718         vfmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
719
720         result = ioctl( p_sys->i_fd, VIDIOC_G_FMT, &vfmt );
721         if ( result < 0 )
722         {
723             msg_Warn( p_access, "Failed to read current picture size." );
724         }
725         else
726         {
727             if ( p_sys->i_width != -1 )
728             {
729                 vfmt.fmt.pix.width = p_sys->i_width;
730             }
731
732             if ( p_sys->i_height != -1 )
733             {
734                 vfmt.fmt.pix.height = p_sys->i_height;
735             }
736
737             result = ioctl( p_sys->i_fd, VIDIOC_S_FMT, &vfmt );
738             if ( result < 0 )
739             {
740                 msg_Warn( p_access, "Failed to set requested picture size." );
741             }
742             else
743             {
744                 msg_Dbg( p_access, "picture size set to: %dx%d",
745                          vfmt.fmt.pix.width, vfmt.fmt.pix.height );
746             }
747         }
748     }
749
750     /* set the frequency */
751     if ( p_sys->i_frequency != -1 )
752     {
753         int i_fd;
754         struct v4l2_tuner vt;
755
756          /* TODO: let the user choose the tuner */
757         memset( &vt, 0, sizeof(struct v4l2_tuner) );
758
759         if ( (p_sys->i_frequency >= pi_radio_range[0])
760               && (p_sys->i_frequency <= pi_radio_range[1]) )
761         {
762             p_sys->i_radio_fd = vlc_open( p_sys->psz_radiodev, O_RDWR );
763             if( p_sys->i_radio_fd < 0 )
764             {
765                 msg_Err( p_access, "Cannot open radio device (%m)." );
766                 Close( VLC_OBJECT(p_access) );
767                 return VLC_EGENERIC;
768             }
769             msg_Dbg( p_access, "using radio device: %s",
770                      p_sys->psz_radiodev );
771             i_fd = p_sys->i_radio_fd;
772         }
773         else
774         {
775             i_fd = p_sys->i_fd;
776             p_sys->i_radio_fd = -1;
777         }
778
779         result = ioctl( i_fd, VIDIOC_G_TUNER, &vt );
780         if ( result < 0 )
781         {
782             msg_Warn( p_access, "Failed to read tuner information (%m)." );
783         }
784         else
785         {
786             struct v4l2_frequency vf;
787
788             memset( &vf, 0, sizeof(struct v4l2_frequency) );
789             vf.tuner = vt.index;
790
791             result = ioctl( i_fd, VIDIOC_G_FREQUENCY, &vf );
792             if ( result < 0 )
793             {
794                 msg_Warn( p_access, "Failed to read tuner frequency (%m)." );
795             }
796             else
797             {
798                 if( vt.capability & V4L2_TUNER_CAP_LOW )
799                     vf.frequency = p_sys->i_frequency * 16;
800                 else
801                     vf.frequency = (p_sys->i_frequency * 16 + 500) / 1000;
802
803                 result = ioctl( i_fd, VIDIOC_S_FREQUENCY, &vf );
804                 if( result < 0 )
805                 {
806                     msg_Warn( p_access, "Failed to set tuner frequency (%m)." );
807                 }
808                 else
809                 {
810                     msg_Dbg( p_access, "tuner frequency set to: %d",
811                              p_sys->i_frequency );
812                 }
813             }
814         }
815     }
816
817     /* control parameters */
818     if ( p_sys->i_volume != -1 )
819     {
820         struct v4l2_control ctrl;
821
822         memset( &ctrl, 0, sizeof(struct v4l2_control) );
823         ctrl.id = V4L2_CID_AUDIO_VOLUME;
824         ctrl.value = p_sys->i_volume;
825
826         result = ioctl( p_sys->i_fd, VIDIOC_S_CTRL, &ctrl );
827         if ( result < 0 )
828         {
829             msg_Warn( p_access, "Failed to set the volume." );
830         }
831     }
832
833     /* codec parameters */
834     if ( (p_sys->i_framerate != -1)
835             || (p_sys->i_bitrate_mode != -1)
836             || (p_sys->i_bitrate_peak != -1)
837             || (p_sys->i_keyint != -1)
838             || (p_sys->i_bframes != -1)
839             || (p_sys->i_bitrate != -1)
840             || (p_sys->i_audio_bitmask != -1) )
841     {
842         if( p_sys->b_v4l2_api )
843         {
844             result = ConfigureV4L2( p_access );
845             if( result != VLC_SUCCESS )
846             {
847                 Close( VLC_OBJECT(p_access) );
848                 return result;
849             }
850         }
851         else
852         {
853             result = ConfigureIVTV( p_access );
854             if( result != VLC_SUCCESS )
855             {
856                 Close( VLC_OBJECT(p_access) );
857                 return result;
858             }
859         }
860     }
861
862     return VLC_SUCCESS;
863 }
864
865 /*****************************************************************************
866  * Close: close the device
867  *****************************************************************************/
868 static void Close( vlc_object_t * p_this )
869 {
870     access_t *p_access = (access_t*) p_this;
871     access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
872
873     if ( p_sys->i_fd != -1 )
874         close( p_sys->i_fd );
875     if ( p_sys->i_radio_fd != -1 )
876         close( p_sys->i_radio_fd );
877     free( p_sys->psz_videodev );
878     free( p_sys->psz_radiodev );
879     free( p_sys );
880 }
881
882 /*****************************************************************************
883  * Read
884  *****************************************************************************/
885 static ssize_t Read( access_t * p_access, uint8_t * p_buffer, size_t i_len )
886 {
887     access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
888     ssize_t i_ret;
889
890     if( p_access->info.b_eof )
891         return 0;
892
893     i_ret = net_Read( p_access, p_sys->i_fd, NULL, p_buffer, i_len, false );
894     if( i_ret == 0 )
895     {
896         p_access->info.b_eof = true;
897     }
898     else if( i_ret > 0 )
899     {
900         p_access->info.i_pos += i_ret;
901     }
902
903     return i_ret;
904 }
905
906 /*****************************************************************************
907  * Control
908  *****************************************************************************/
909 static int Control( access_t *p_access, int i_query, va_list args )
910 {
911     bool   *pb_bool;
912     int64_t      *pi_64;
913
914     switch( i_query )
915     {
916         /* */
917         case ACCESS_CAN_SEEK:
918         case ACCESS_CAN_FASTSEEK:
919             pb_bool = (bool*)va_arg( args, bool* );
920             *pb_bool = false;
921             break;
922         case ACCESS_CAN_PAUSE:
923             pb_bool = (bool*)va_arg( args, bool* );
924             *pb_bool = false;
925             break;
926         case ACCESS_CAN_CONTROL_PACE:
927             pb_bool = (bool*)va_arg( args, bool* );
928             *pb_bool = false;
929             break;
930
931         /* */
932         case ACCESS_GET_PTS_DELAY:
933             pi_64 = (int64_t*)va_arg( args, int64_t * );
934             *pi_64 = INT64_C(1000)
935                    * var_InheritInteger( p_access, "live-caching" );
936             break;
937
938         /* */
939         case ACCESS_SET_PAUSE_STATE:
940             /* Nothing to do */
941             break;
942
943         case ACCESS_GET_TITLE_INFO:
944         case ACCESS_SET_TITLE:
945         case ACCESS_SET_SEEKPOINT:
946         case ACCESS_SET_PRIVATE_ID_STATE:
947         case ACCESS_GET_CONTENT_TYPE:
948             return VLC_EGENERIC;
949
950         default:
951             msg_Warn( p_access, "Unimplemented query in control." );
952             return VLC_EGENERIC;
953
954     }
955     return VLC_SUCCESS;
956 }