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