1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 the VideoLAN team
7 * Authors: Eric Petit <titer@videolan.org>
8 * Paul Corke <paulc@datatote.co.uk>
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.
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.
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 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_access.h>
37 #include <vlc_network.h>
39 #include <sys/types.h>
42 #include <linux/types.h>
43 #include <sys/ioctl.h>
44 #if defined(HAVE_LINUX_VIDEODEV2_H)
45 # include <linux/videodev2.h>
46 #elif defined(HAVE_SYS_VIDEOIO_H)
47 # include <sys/videoio.h>
49 # error "No Video4Linux2 headers found."
52 /*****************************************************************************
54 *****************************************************************************/
55 static int Open ( vlc_object_t * );
56 static void Close( vlc_object_t * );
58 #define CACHING_TEXT N_("Caching value in ms")
59 #define CACHING_LONGTEXT N_( \
60 "Default caching value for PVR streams. This " \
61 "value should be set in milliseconds." )
63 #define DEVICE_TEXT N_( "Device" )
64 #define DEVICE_LONGTEXT N_( "PVR video device" )
66 #define RADIO_DEVICE_TEXT N_( "Radio device" )
67 #define RADIO_DEVICE_LONGTEXT N_( "PVR radio device" )
69 #define NORM_TEXT N_( "Norm" )
70 #define NORM_LONGTEXT N_( "Norm of the stream " \
71 "(Automatic, SECAM, PAL, or NTSC)." )
73 #define WIDTH_TEXT N_( "Width" )
74 #define WIDTH_LONGTEXT N_( "Width of the stream to capture " \
75 "(-1 for autodetection)." )
77 #define HEIGHT_TEXT N_( "Height" )
78 #define HEIGHT_LONGTEXT N_( "Height of the stream to capture " \
79 "(-1 for autodetection)." )
81 #define FREQUENCY_TEXT N_( "Frequency" )
82 #define FREQUENCY_LONGTEXT N_( "Frequency to capture (in kHz), if applicable." )
84 #define FRAMERATE_TEXT N_( "Framerate" )
85 #define FRAMERATE_LONGTEXT N_( "Framerate to capture, if applicable " \
86 "(-1 for autodetect)." )
88 #define KEYINT_TEXT N_( "Key interval" )
89 #define KEYINT_LONGTEXT N_( "Interval between keyframes (-1 for autodetect)." )
91 #define BFRAMES_TEXT N_( "B Frames" )
92 #define BFRAMES_LONGTEXT N_("If this option is set, B-Frames will be used. " \
93 "Use this option to set the number of B-Frames.")
95 #define BITRATE_TEXT N_( "Bitrate" )
96 #define BITRATE_LONGTEXT N_( "Bitrate to use (-1 for default)." )
98 #define BITRATE_PEAK_TEXT N_( "Bitrate peak" )
99 #define BITRATE_PEAK_LONGTEXT N_( "Peak bitrate in VBR mode." )
101 #define BITRATE_MODE_TEXT N_( "Bitrate mode" )
102 #define BITRATE_MODE_LONGTEXT N_( "Bitrate mode to use (VBR or CBR)." )
104 #define BITMASK_TEXT N_( "Audio bitmask" )
105 #define BITMASK_LONGTEXT N_("Bitmask that will "\
106 "get used by the audio part of the card." )
108 #define VOLUME_TEXT N_( "Volume" )
109 #define VOLUME_LONGTEXT N_("Audio volume (0-65535)." )
111 #define CHAN_TEXT N_( "Channel" )
112 #define CHAN_LONGTEXT N_( "Channel of the card to use (Usually, 0 = tuner, " \
113 "1 = composite, 2 = svideo)" )
115 static const int i_norm_list[] =
116 { V4L2_STD_UNKNOWN, V4L2_STD_SECAM, V4L2_STD_PAL, V4L2_STD_NTSC };
117 static const char *const psz_norm_list_text[] =
118 { N_("Automatic"), N_("SECAM"), N_("PAL"), N_("NTSC") };
120 static const int i_bitrates[] = { 0, 1 };
121 static const char *const psz_bitrates_list_text[] = { N_("vbr"), N_("cbr") };
123 static const int pi_radio_range[2] = { 65000, 108000 };
126 set_shortname( N_("PVR") )
127 set_description( N_("IVTV MPEG Encoding cards input") )
128 set_category( CAT_INPUT )
129 set_subcategory( SUBCAT_INPUT_ACCESS )
130 set_capability( "access", 0 )
131 add_shortcut( "pvr" )
133 add_integer( "pvr-caching", DEFAULT_PTS_DELAY / 1000, CACHING_TEXT,
134 CACHING_LONGTEXT, true )
135 add_string( "pvr-device", "/dev/video0", DEVICE_TEXT,
136 DEVICE_LONGTEXT, false )
137 add_string( "pvr-radio-device", "/dev/radio0", RADIO_DEVICE_TEXT,
138 RADIO_DEVICE_LONGTEXT, false )
139 add_integer( "pvr-norm", V4L2_STD_UNKNOWN , NORM_TEXT,
140 NORM_LONGTEXT, false )
141 change_integer_list( i_norm_list, psz_norm_list_text )
142 add_integer( "pvr-width", -1, WIDTH_TEXT, WIDTH_LONGTEXT, true )
143 add_integer( "pvr-height", -1, HEIGHT_TEXT, HEIGHT_LONGTEXT,
145 add_integer( "pvr-frequency", -1, FREQUENCY_TEXT, FREQUENCY_LONGTEXT,
147 add_integer( "pvr-framerate", -1, FRAMERATE_TEXT, FRAMERATE_LONGTEXT,
149 add_integer( "pvr-keyint", -1, KEYINT_TEXT, KEYINT_LONGTEXT,
151 add_integer( "pvr-bframes", -1, FRAMERATE_TEXT, FRAMERATE_LONGTEXT,
153 add_integer( "pvr-bitrate", -1, BITRATE_TEXT, BITRATE_LONGTEXT,
155 add_integer( "pvr-bitrate-peak", -1, BITRATE_PEAK_TEXT,
156 BITRATE_PEAK_LONGTEXT, true )
157 add_integer( "pvr-bitrate-mode", -1, BITRATE_MODE_TEXT,
158 BITRATE_MODE_LONGTEXT, true )
159 change_integer_list( i_bitrates, psz_bitrates_list_text )
160 add_integer( "pvr-audio-bitmask", -1, BITMASK_TEXT,
161 BITMASK_LONGTEXT, true )
162 add_integer( "pvr-audio-volume", -1, VOLUME_TEXT,
163 VOLUME_LONGTEXT, true )
164 add_integer( "pvr-channel", -1, CHAN_TEXT, CHAN_LONGTEXT, true )
166 set_callbacks( Open, Close )
169 /*****************************************************************************
171 *****************************************************************************/
172 static ssize_t Read ( access_t *, uint8_t *, size_t );
173 static int Control( access_t *, int, va_list );
175 /* ivtv specific ioctls */
176 #define IVTV_IOC_G_CODEC 0xFFEE7703
177 #define IVTV_IOC_S_CODEC 0xFFEE7704
179 /* for use with IVTV_IOC_G_CODEC and IVTV_IOC_S_CODEC */
181 struct ivtv_ioctl_codec {
183 uint32_t audio_bitmask;
185 uint32_t bitrate_mode;
187 uint32_t bitrate_peak;
189 uint32_t dnr_spatial;
190 uint32_t dnr_temporal;
193 uint32_t framespergop;
194 uint32_t gop_closure;
196 uint32_t stream_type;
201 /* file descriptor */
227 /*****************************************************************************
228 * ConfigureIVTV: set up codec parameters using the old ivtv api
229 *****************************************************************************/
230 static int ConfigureIVTV( access_t * p_access )
232 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
233 struct ivtv_ioctl_codec codec;
236 memset( &codec, 0, sizeof(struct ivtv_ioctl_codec) );
238 result = ioctl( p_sys->i_fd, IVTV_IOC_G_CODEC, &codec );
241 msg_Err( p_access, "Failed to read current capture card settings." );
245 if( p_sys->i_framerate != -1 )
247 switch( p_sys->i_framerate )
258 msg_Warn( p_access, "Invalid framerate, reverting to 25." );
264 if( p_sys->i_bitrate != -1 )
266 codec.bitrate = p_sys->i_bitrate;
269 if( p_sys->i_bitrate_peak != -1 )
271 codec.bitrate_peak = p_sys->i_bitrate_peak;
274 if( p_sys->i_bitrate_mode != -1 )
276 codec.bitrate_mode = p_sys->i_bitrate_mode;
279 if( p_sys->i_audio_bitmask != -1 )
281 codec.audio_bitmask = p_sys->i_audio_bitmask;
284 if( p_sys->i_keyint != -1 )
286 codec.framespergop = p_sys->i_keyint;
289 if( p_sys->i_bframes != -1 )
291 codec.bframes = p_sys->i_bframes;
294 result = ioctl( p_sys->i_fd, IVTV_IOC_S_CODEC, &codec );
297 msg_Err( p_access, "Failed to write new capture card settings." );
301 msg_Dbg( p_access, "Setting codec parameters to: framerate: "
302 "%d, bitrate: %d/%d/%d",
303 codec.framerate, codec.bitrate,
304 codec.bitrate_peak, codec.bitrate_mode );
308 #define MAX_V4L2_CTRLS (6)
309 /*****************************************************************************
310 * AddV4L2Ctrl: adds a control to the v4l2 controls list
311 *****************************************************************************/
312 static void AddV4L2Ctrl( access_t * p_access,
313 struct v4l2_ext_controls * p_controls,
314 uint32_t i_id, uint32_t i_value )
316 if( p_controls->count >= MAX_V4L2_CTRLS )
318 msg_Err( p_access, "Tried to set too many v4l2 controls at once." );
322 p_controls->controls[p_controls->count].id = i_id;
323 p_controls->controls[p_controls->count].value = i_value;
327 /*****************************************************************************
328 * V4L2SampleRate: calculate v4l2 sample rate from pvr-audio-bitmask
329 *****************************************************************************/
330 static uint32_t V4L2SampleRate( uint32_t i_bitmask )
332 switch( i_bitmask & 0x0003 )
334 case 0x0001: return V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000;
335 case 0x0002: return V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000;
337 return V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100;
340 /*****************************************************************************
341 * V4L2AudioEncoding: calculate v4l2 audio encoding level from pvr-audio-bitmask
342 *****************************************************************************/
343 static uint32_t V4L2AudioEncoding( uint32_t i_bitmask )
345 switch( i_bitmask & 0x000c )
347 case 0x0004: return V4L2_MPEG_AUDIO_ENCODING_LAYER_1;
348 case 0x0008: return V4L2_MPEG_AUDIO_ENCODING_LAYER_2;
353 /*****************************************************************************
354 * V4L2AudioL1Bitrate: calculate v4l2 audio bitrate for layer-1 audio from pvr-audio-bitmask
355 *****************************************************************************/
356 static uint32_t V4L2AudioL1Bitrate( uint32_t i_bitmask )
358 switch( i_bitmask & 0x00f0 )
360 case 0x0010: return V4L2_MPEG_AUDIO_L1_BITRATE_32K;
361 case 0x0020: return V4L2_MPEG_AUDIO_L1_BITRATE_64K;
362 case 0x0030: return V4L2_MPEG_AUDIO_L1_BITRATE_96K;
363 case 0x0040: return V4L2_MPEG_AUDIO_L1_BITRATE_128K;
364 case 0x0050: return V4L2_MPEG_AUDIO_L1_BITRATE_160K;
365 case 0x0060: return V4L2_MPEG_AUDIO_L1_BITRATE_192K;
366 case 0x0070: return V4L2_MPEG_AUDIO_L1_BITRATE_224K;
367 case 0x0080: return V4L2_MPEG_AUDIO_L1_BITRATE_256K;
368 case 0x0090: return V4L2_MPEG_AUDIO_L1_BITRATE_288K;
369 case 0x00a0: return V4L2_MPEG_AUDIO_L1_BITRATE_320K;
370 case 0x00b0: return V4L2_MPEG_AUDIO_L1_BITRATE_352K;
371 case 0x00c0: return V4L2_MPEG_AUDIO_L1_BITRATE_384K;
372 case 0x00d0: return V4L2_MPEG_AUDIO_L1_BITRATE_416K;
373 case 0x00e0: return V4L2_MPEG_AUDIO_L1_BITRATE_448K;
375 return V4L2_MPEG_AUDIO_L1_BITRATE_320K;
378 /*****************************************************************************
379 * V4L2AudioL2Bitrate: calculate v4l2 audio bitrate for layer-1 audio from pvr-audio-bitmask
380 *****************************************************************************/
381 static uint32_t V4L2AudioL2Bitrate( uint32_t i_bitmask )
383 switch( i_bitmask & 0x00f0 )
385 case 0x0010: return V4L2_MPEG_AUDIO_L2_BITRATE_32K;
386 case 0x0020: return V4L2_MPEG_AUDIO_L2_BITRATE_48K;
387 case 0x0030: return V4L2_MPEG_AUDIO_L2_BITRATE_56K;
388 case 0x0040: return V4L2_MPEG_AUDIO_L2_BITRATE_64K;
389 case 0x0050: return V4L2_MPEG_AUDIO_L2_BITRATE_80K;
390 case 0x0060: return V4L2_MPEG_AUDIO_L2_BITRATE_96K;
391 case 0x0070: return V4L2_MPEG_AUDIO_L2_BITRATE_112K;
392 case 0x0080: return V4L2_MPEG_AUDIO_L2_BITRATE_128K;
393 case 0x0090: return V4L2_MPEG_AUDIO_L2_BITRATE_160K;
394 case 0x00a0: return V4L2_MPEG_AUDIO_L2_BITRATE_192K;
395 case 0x00b0: return V4L2_MPEG_AUDIO_L2_BITRATE_224K;
396 case 0x00c0: return V4L2_MPEG_AUDIO_L2_BITRATE_256K;
397 case 0x00d0: return V4L2_MPEG_AUDIO_L2_BITRATE_320K;
398 case 0x00e0: return V4L2_MPEG_AUDIO_L2_BITRATE_384K;
400 return V4L2_MPEG_AUDIO_L2_BITRATE_192K;
403 /*****************************************************************************
404 * V4L2AudioMode: calculate v4l2 audio mode from pvr-audio-bitmask
405 *****************************************************************************/
406 static uint32_t V4L2AudioMode( uint32_t i_bitmask )
408 switch( i_bitmask & 0x0300 )
410 case 0x0100: return V4L2_MPEG_AUDIO_MODE_JOINT_STEREO;
411 case 0x0200: return V4L2_MPEG_AUDIO_MODE_DUAL;
412 case 0x0300: return V4L2_MPEG_AUDIO_MODE_MONO;
414 return V4L2_MPEG_AUDIO_MODE_STEREO;
417 /*****************************************************************************
418 * ConfigureV4L2: set up codec parameters using the new v4l2 api
419 *****************************************************************************/
420 static int ConfigureV4L2( access_t * p_access )
422 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
423 struct v4l2_ext_controls controls;
426 memset( &controls, 0, sizeof(struct v4l2_ext_controls) );
427 controls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
428 controls.error_idx = 0;
429 controls.reserved[0] = 0;
430 controls.reserved[1] = 0;
432 controls.controls = calloc( MAX_V4L2_CTRLS,
433 sizeof( struct v4l2_ext_control ) );
435 if( controls.controls == NULL )
438 /* Note: Ignore frame rate. Doesn't look like it can be changed. */
439 if( p_sys->i_bitrate != -1 )
441 AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_BITRATE,
443 msg_Dbg( p_access, "Setting [%u] bitrate = %u",
444 controls.count - 1, p_sys->i_bitrate );
447 if( p_sys->i_bitrate_peak != -1 )
449 AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
450 p_sys->i_bitrate_peak );
451 msg_Dbg( p_access, "Setting [%u] bitrate_peak = %u",
452 controls.count - 1, p_sys->i_bitrate_peak );
455 if( p_sys->i_bitrate_mode != -1 )
457 AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
458 p_sys->i_bitrate_mode );
459 msg_Dbg( p_access, "Setting [%u] bitrate_mode = %u",
460 controls.count - 1, p_sys->i_bitrate_mode );
463 if( p_sys->i_audio_bitmask != -1 )
466 AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
467 V4L2SampleRate( p_sys->i_audio_bitmask ) );
469 /* Encoding layer and bitrate */
470 switch( V4L2AudioEncoding( p_sys->i_audio_bitmask ) )
472 case V4L2_MPEG_AUDIO_ENCODING_LAYER_1:
473 AddV4L2Ctrl( p_access, &controls,
474 V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
475 V4L2_MPEG_AUDIO_ENCODING_LAYER_1 );
476 AddV4L2Ctrl( p_access, &controls,
477 V4L2_CID_MPEG_AUDIO_L1_BITRATE,
478 V4L2AudioL1Bitrate( p_sys->i_audio_bitmask ) );
481 case V4L2_MPEG_AUDIO_ENCODING_LAYER_2:
482 AddV4L2Ctrl( p_access, &controls,
483 V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
484 V4L2_MPEG_AUDIO_ENCODING_LAYER_2 );
485 AddV4L2Ctrl( p_access, &controls,
486 V4L2_CID_MPEG_AUDIO_L2_BITRATE,
487 V4L2AudioL2Bitrate( p_sys->i_audio_bitmask ) );
491 /* Audio mode - stereo or mono */
492 AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_AUDIO_MODE,
493 V4L2AudioMode( p_sys->i_audio_bitmask ) );
495 /* See if the user wants any other audio feature */
496 if( ( p_sys->i_audio_bitmask & 0x1ff00 ) != 0 )
498 /* It would be possible to support the bits that represent:
499 * V4L2_CID_MPEG_AUDIO_MODE_EXTENSION
500 * V4L2_CID_MPEG_AUDIO_EMPHASIS
501 * V4L2_CID_MPEG_AUDIO_CRC
502 * but they are not currently used. Tell the user.
504 msg_Err( p_access, "There were bits in pvr-audio-bitmask that were not used.");
506 msg_Dbg( p_access, "Setting audio controls");
509 if( p_sys->i_keyint != -1 )
511 AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_GOP_SIZE,
513 msg_Dbg( p_access, "Setting [%u] keyint = %u",
514 controls.count - 1, p_sys->i_keyint );
517 if( p_sys->i_bframes != -1 )
519 AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_B_FRAMES,
521 msg_Dbg( p_access, "Setting [%u] bframes = %u",
522 controls.count - 1, p_sys->i_bframes );
525 result = ioctl( p_sys->i_fd, VIDIOC_S_EXT_CTRLS, &controls );
528 msg_Err( p_access, "Failed to write %u new capture card settings.",
529 controls.error_idx );
531 free( controls.controls );
535 /*****************************************************************************
536 * Open: open the device
537 *****************************************************************************/
538 static int Open( vlc_object_t * p_this )
540 access_t *p_access = (access_t*) p_this;
541 access_sys_t * p_sys;
544 struct v4l2_capability device_capability;
547 memset( &device_capability, 0, sizeof(struct v4l2_capability) );
549 access_InitFields( p_access );
550 ACCESS_SET_CALLBACKS( Read, NULL, Control, NULL );
551 p_sys = p_access->p_sys = calloc( 1, sizeof( access_sys_t ));
552 if( !p_sys ) return VLC_ENOMEM;
554 /* defaults values */
555 var_Create( p_access, "pvr-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
557 p_sys->psz_videodev = var_InheritString( p_access, "pvr-device" );
558 p_sys->psz_radiodev = var_InheritString( p_access, "pvr-radio-device" );
559 p_sys->i_standard = var_InheritInteger( p_access, "pvr-norm" );
560 p_sys->i_width = var_InheritInteger( p_access, "pvr-width" );
561 p_sys->i_height = var_InheritInteger( p_access, "pvr-height" );
562 p_sys->i_frequency = var_InheritInteger( p_access, "pvr-frequency" );
563 p_sys->i_framerate = var_InheritInteger( p_access, "pvr-framerate" );
564 p_sys->i_keyint = var_InheritInteger( p_access, "pvr-keyint" );
565 p_sys->i_bframes = var_InheritInteger( p_access, "pvr-bframes" );
566 p_sys->i_bitrate = var_InheritInteger( p_access, "pvr-bitrate" );
567 p_sys->i_bitrate_peak = var_InheritInteger( p_access, "pvr-bitrate-peak" );
568 p_sys->i_bitrate_mode = var_InheritInteger( p_access, "pvr-bitrate-mode" );
569 p_sys->i_audio_bitmask = var_InheritInteger( p_access, "pvr-audio-bitmask" );
570 p_sys->i_volume = var_InheritInteger( p_access, "pvr-audio-volume" );
571 p_sys->i_input = var_InheritInteger( p_access, "pvr-channel" );
573 /* parse command line options */
574 psz_tofree = strdup( p_access->psz_location );
577 free( p_sys->psz_radiodev );
578 free( p_sys->psz_videodev );
583 psz_parser = psz_tofree;
586 /* Leading slash -> device path */
587 if( *psz_parser == '/' )
589 free( p_sys->psz_videodev );
590 p_sys->psz_videodev = decode_URI_duplicate( psz_parser );
594 /* Extract option name */
595 const char *optname = psz_parser;
596 psz_parser = strchr( psz_parser, '=' );
597 if( psz_parser == NULL )
599 *psz_parser++ = '\0';
601 /* Extract option value */
602 char *optval = psz_parser;
603 while( memchr( ":,", *psz_parser, 3 /* includes \0 */ ) == NULL )
605 if( *psz_parser ) /* more options to come */
606 *psz_parser++ = '\0'; /* skip , or : */
608 if ( !strcmp( optname, "norm" ) )
610 if ( !strcmp( optval, "secam" ) )
611 p_sys->i_standard = V4L2_STD_SECAM;
612 else if ( !strcmp( optval, "pal" ) )
613 p_sys->i_standard = V4L2_STD_PAL;
614 else if ( !strcmp( optval, "ntsc" ) )
615 p_sys->i_standard = V4L2_STD_NTSC;
617 p_sys->i_standard = atoi( optval );
619 else if( !strcmp( optname, "channel" ) )
620 p_sys->i_input = atoi( optval );
621 else if( !strcmp( optname, "device" ) )
623 free( p_sys->psz_videodev );
624 if( asprintf( &p_sys->psz_videodev, "/dev/video%s", optval ) == -1)
625 p_sys->psz_videodev = NULL;
627 else if( !strcmp( optname, "frequency" ) )
628 p_sys->i_frequency = atoi( optval );
629 else if( !strcmp( optname, "framerate" ) )
630 p_sys->i_framerate = atoi( optval );
631 else if( !strcmp( optname, "keyint" ) )
632 p_sys->i_keyint = atoi( optval );
633 else if( !strcmp( optname, "bframes" ) )
634 p_sys->i_bframes = atoi( optval );
635 else if( !strcmp( optname, "width" ) )
636 p_sys->i_width = atoi( optval );
637 else if( !strcmp( optname, "height" ) )
638 p_sys->i_height = atoi( optval );
639 else if( !strcmp( optname, "audio" ) )
640 p_sys->i_audio_bitmask = atoi( optval );
641 else if( !strcmp( optname, "bitrate" ) )
642 p_sys->i_bitrate = atoi( optval );
643 else if( !strcmp( optname, "maxbitrate" ) )
644 p_sys->i_bitrate_peak = atoi( optval );
645 else if( !strcmp( optname, "bitratemode" ) )
647 if( !strcmp( optval, "vbr" ) )
648 p_sys->i_bitrate_mode = 0;
649 else if( !strcmp( optval, "cbr" ) )
650 p_sys->i_bitrate_mode = 1;
652 else if( !strcmp( optname, "size" ) )
654 p_sys->i_width = strtol( optval, &optval, 0 );
655 p_sys->i_height = atoi( optval );
660 /* open the device */
661 p_sys->i_fd = vlc_open( p_sys->psz_videodev, O_RDWR );
662 if( p_sys->i_fd < 0 )
664 msg_Err( p_access, "Cannot open device %s (%m).",
665 p_sys->psz_videodev );
666 Close( VLC_OBJECT(p_access) );
669 msg_Dbg( p_access, "Using video device: %s.", p_sys->psz_videodev);
671 /* See what version of ivtvdriver is running */
672 result = ioctl( p_sys->i_fd, VIDIOC_QUERYCAP, &device_capability );
675 msg_Err( p_access, "unknown ivtv/pvr driver version in use" );
676 Close( VLC_OBJECT(p_access) );
680 msg_Dbg( p_access, "%s driver (%s on %s) version %02x.%02x.%02x",
681 device_capability.driver,
682 device_capability.card,
683 device_capability.bus_info,
684 ( device_capability.version >> 16 ) & 0xff,
685 ( device_capability.version >> 8 ) & 0xff,
686 ( device_capability.version ) & 0xff);
688 if ( strncmp( (char *) device_capability.driver, "ivtv", 4 )
689 || device_capability.version >= 0x000800 )
691 /* Drivers > 0.8.0 use v4l2 API instead of IVTV ioctls */
692 msg_Dbg( p_access, "this driver uses the v4l2 API" );
693 p_sys->b_v4l2_api = true;
697 p_sys->b_v4l2_api = false;
701 if ( p_sys->i_input != -1 )
703 result = ioctl( p_sys->i_fd, VIDIOC_S_INPUT, &p_sys->i_input );
705 msg_Warn( p_access, "Failed to select the requested input pin." );
707 msg_Dbg( p_access, "input set to: %d", p_sys->i_input );
710 /* set the video standard */
711 if ( p_sys->i_standard != V4L2_STD_UNKNOWN )
713 result = ioctl( p_sys->i_fd, VIDIOC_S_STD, &p_sys->i_standard );
715 msg_Warn( p_access, "Failed to set the requested video standard." );
717 msg_Dbg( p_access, "video standard set to: %x",
721 /* set the picture size */
722 if ( (p_sys->i_width != -1) || (p_sys->i_height != -1) )
724 struct v4l2_format vfmt;
726 memset( &vfmt, 0, sizeof(struct v4l2_format) );
727 vfmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
729 result = ioctl( p_sys->i_fd, VIDIOC_G_FMT, &vfmt );
732 msg_Warn( p_access, "Failed to read current picture size." );
736 if ( p_sys->i_width != -1 )
738 vfmt.fmt.pix.width = p_sys->i_width;
741 if ( p_sys->i_height != -1 )
743 vfmt.fmt.pix.height = p_sys->i_height;
746 result = ioctl( p_sys->i_fd, VIDIOC_S_FMT, &vfmt );
749 msg_Warn( p_access, "Failed to set requested picture size." );
753 msg_Dbg( p_access, "picture size set to: %dx%d",
754 vfmt.fmt.pix.width, vfmt.fmt.pix.height );
759 /* set the frequency */
760 if ( p_sys->i_frequency != -1 )
763 struct v4l2_tuner vt;
765 /* TODO: let the user choose the tuner */
766 memset( &vt, 0, sizeof(struct v4l2_tuner) );
768 if ( (p_sys->i_frequency >= pi_radio_range[0])
769 && (p_sys->i_frequency <= pi_radio_range[1]) )
771 p_sys->i_radio_fd = vlc_open( p_sys->psz_radiodev, O_RDWR );
772 if( p_sys->i_radio_fd < 0 )
774 msg_Err( p_access, "Cannot open radio device (%m)." );
775 Close( VLC_OBJECT(p_access) );
778 msg_Dbg( p_access, "using radio device: %s",
779 p_sys->psz_radiodev );
780 i_fd = p_sys->i_radio_fd;
785 p_sys->i_radio_fd = -1;
788 result = ioctl( i_fd, VIDIOC_G_TUNER, &vt );
791 msg_Warn( p_access, "Failed to read tuner information (%m)." );
795 struct v4l2_frequency vf;
797 memset( &vf, 0, sizeof(struct v4l2_frequency) );
800 result = ioctl( i_fd, VIDIOC_G_FREQUENCY, &vf );
803 msg_Warn( p_access, "Failed to read tuner frequency (%m)." );
807 if( vt.capability & V4L2_TUNER_CAP_LOW )
808 vf.frequency = p_sys->i_frequency * 16;
810 vf.frequency = (p_sys->i_frequency * 16 + 500) / 1000;
812 result = ioctl( i_fd, VIDIOC_S_FREQUENCY, &vf );
815 msg_Warn( p_access, "Failed to set tuner frequency (%m)." );
819 msg_Dbg( p_access, "tuner frequency set to: %d",
820 p_sys->i_frequency );
826 /* control parameters */
827 if ( p_sys->i_volume != -1 )
829 struct v4l2_control ctrl;
831 memset( &ctrl, 0, sizeof(struct v4l2_control) );
832 ctrl.id = V4L2_CID_AUDIO_VOLUME;
833 ctrl.value = p_sys->i_volume;
835 result = ioctl( p_sys->i_fd, VIDIOC_S_CTRL, &ctrl );
838 msg_Warn( p_access, "Failed to set the volume." );
842 /* codec parameters */
843 if ( (p_sys->i_framerate != -1)
844 || (p_sys->i_bitrate_mode != -1)
845 || (p_sys->i_bitrate_peak != -1)
846 || (p_sys->i_keyint != -1)
847 || (p_sys->i_bframes != -1)
848 || (p_sys->i_bitrate != -1)
849 || (p_sys->i_audio_bitmask != -1) )
851 if( p_sys->b_v4l2_api )
853 result = ConfigureV4L2( p_access );
854 if( result != VLC_SUCCESS )
856 Close( VLC_OBJECT(p_access) );
862 result = ConfigureIVTV( p_access );
863 if( result != VLC_SUCCESS )
865 Close( VLC_OBJECT(p_access) );
874 /*****************************************************************************
875 * Close: close the device
876 *****************************************************************************/
877 static void Close( vlc_object_t * p_this )
879 access_t *p_access = (access_t*) p_this;
880 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
882 if ( p_sys->i_fd != -1 )
883 close( p_sys->i_fd );
884 if ( p_sys->i_radio_fd != -1 )
885 close( p_sys->i_radio_fd );
886 free( p_sys->psz_videodev );
887 free( p_sys->psz_radiodev );
891 /*****************************************************************************
893 *****************************************************************************/
894 static ssize_t Read( access_t * p_access, uint8_t * p_buffer, size_t i_len )
896 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
899 if( p_access->info.b_eof )
902 i_ret = net_Read( p_access, p_sys->i_fd, NULL, p_buffer, i_len, false );
905 p_access->info.b_eof = true;
909 p_access->info.i_pos += i_ret;
915 /*****************************************************************************
917 *****************************************************************************/
918 static int Control( access_t *p_access, int i_query, va_list args )
926 case ACCESS_CAN_SEEK:
927 case ACCESS_CAN_FASTSEEK:
928 pb_bool = (bool*)va_arg( args, bool* );
931 case ACCESS_CAN_PAUSE:
932 pb_bool = (bool*)va_arg( args, bool* );
935 case ACCESS_CAN_CONTROL_PACE:
936 pb_bool = (bool*)va_arg( args, bool* );
941 case ACCESS_GET_PTS_DELAY:
942 pi_64 = (int64_t*)va_arg( args, int64_t * );
943 *pi_64 = var_GetInteger( p_access, "pvr-caching" ) * 1000;
947 case ACCESS_SET_PAUSE_STATE:
951 case ACCESS_GET_TITLE_INFO:
952 case ACCESS_SET_TITLE:
953 case ACCESS_SET_SEEKPOINT:
954 case ACCESS_SET_PRIVATE_ID_STATE:
955 case ACCESS_GET_CONTENT_TYPE:
959 msg_Warn( p_access, "Unimplemented query in control." );