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