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