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