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>
38 #include <sys/types.h>
41 #include <linux/types.h>
42 #include <sys/ioctl.h>
44 #ifdef HAVE_NEW_LINUX_VIDEODEV2_H
45 # ifdef VIDEODEV2_H_FILE
46 # include VIDEODEV2_H_FILE
48 # include <linux/videodev2.h>
51 #include "videodev2.h"
54 /*****************************************************************************
56 *****************************************************************************/
57 static int Open ( vlc_object_t
* );
58 static void Close( vlc_object_t
* );
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." )
65 #define DEVICE_TEXT N_( "Device" )
66 #define DEVICE_LONGTEXT N_( "PVR video device" )
68 #define RADIO_DEVICE_TEXT N_( "Radio device" )
69 #define RADIO_DEVICE_LONGTEXT N_( "PVR radio device" )
71 #define NORM_TEXT N_( "Norm" )
72 #define NORM_LONGTEXT N_( "Norm of the stream " \
73 "(Automatic, SECAM, PAL, or NTSC)." )
75 #define WIDTH_TEXT N_( "Width" )
76 #define WIDTH_LONGTEXT N_( "Width of the stream to capture " \
77 "(-1 for autodetection)." )
79 #define HEIGHT_TEXT N_( "Height" )
80 #define HEIGHT_LONGTEXT N_( "Height of the stream to capture " \
81 "(-1 for autodetection)." )
83 #define FREQUENCY_TEXT N_( "Frequency" )
84 #define FREQUENCY_LONGTEXT N_( "Frequency to capture (in kHz), if applicable." )
86 #define FRAMERATE_TEXT N_( "Framerate" )
87 #define FRAMERATE_LONGTEXT N_( "Framerate to capture, if applicable " \
88 "(-1 for autodetect)." )
90 #define KEYINT_TEXT N_( "Key interval" )
91 #define KEYINT_LONGTEXT N_( "Interval between keyframes (-1 for autodetect)." )
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.")
97 #define BITRATE_TEXT N_( "Bitrate" )
98 #define BITRATE_LONGTEXT N_( "Bitrate to use (-1 for default)." )
100 #define BITRATE_PEAK_TEXT N_( "Bitrate peak" )
101 #define BITRATE_PEAK_LONGTEXT N_( "Peak bitrate in VBR mode." )
103 #define BITRATE_MODE_TEXT N_( "Bitrate mode" )
104 #define BITRATE_MODE_LONGTEXT N_( "Bitrate mode to use (VBR or CBR)." )
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." )
110 #define VOLUME_TEXT N_( "Volume" )
111 #define VOLUME_LONGTEXT N_("Audio volume (0-65535)." )
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)" )
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") };
122 static const int i_bitrates
[] = { 0, 1 };
123 static const char *const psz_bitrates_list_text
[] = { N_("vbr"), N_("cbr") };
125 static const int pi_radio_range
[2] = { 65000, 108000 };
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" )
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
,
147 add_integer( "pvr-frequency", -1, NULL
, FREQUENCY_TEXT
, FREQUENCY_LONGTEXT
,
149 add_integer( "pvr-framerate", -1, NULL
, FRAMERATE_TEXT
, FRAMERATE_LONGTEXT
,
151 add_integer( "pvr-keyint", -1, NULL
, KEYINT_TEXT
, KEYINT_LONGTEXT
,
153 add_integer( "pvr-bframes", -1, NULL
, FRAMERATE_TEXT
, FRAMERATE_LONGTEXT
,
155 add_integer( "pvr-bitrate", -1, NULL
, BITRATE_TEXT
, BITRATE_LONGTEXT
,
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 )
168 set_callbacks( Open
, Close
)
171 /*****************************************************************************
173 *****************************************************************************/
174 static ssize_t
Read ( access_t
*, uint8_t *, size_t );
175 static int Control( access_t
*, int, va_list );
177 /* ivtv specific ioctls */
178 #define IVTV_IOC_G_CODEC 0xFFEE7703
179 #define IVTV_IOC_S_CODEC 0xFFEE7704
181 /* for use with IVTV_IOC_G_CODEC and IVTV_IOC_S_CODEC */
183 struct ivtv_ioctl_codec
{
185 uint32_t audio_bitmask
;
187 uint32_t bitrate_mode
;
189 uint32_t bitrate_peak
;
191 uint32_t dnr_spatial
;
192 uint32_t dnr_temporal
;
195 uint32_t framespergop
;
196 uint32_t gop_closure
;
198 uint32_t stream_type
;
203 /* file descriptor */
229 /*****************************************************************************
230 * ConfigureIVTV: set up codec parameters using the old ivtv api
231 *****************************************************************************/
232 static int ConfigureIVTV( access_t
* p_access
)
234 access_sys_t
*p_sys
= (access_sys_t
*) p_access
->p_sys
;
235 struct ivtv_ioctl_codec codec
;
238 memset( &codec
, 0, sizeof(struct ivtv_ioctl_codec
) );
240 result
= ioctl( p_sys
->i_fd
, IVTV_IOC_G_CODEC
, &codec
);
243 msg_Err( p_access
, "Failed to read current capture card settings." );
247 if( p_sys
->i_framerate
!= -1 )
249 switch( p_sys
->i_framerate
)
260 msg_Warn( p_access
, "Invalid framerate, reverting to 25." );
266 if( p_sys
->i_bitrate
!= -1 )
268 codec
.bitrate
= p_sys
->i_bitrate
;
271 if( p_sys
->i_bitrate_peak
!= -1 )
273 codec
.bitrate_peak
= p_sys
->i_bitrate_peak
;
276 if( p_sys
->i_bitrate_mode
!= -1 )
278 codec
.bitrate_mode
= p_sys
->i_bitrate_mode
;
281 if( p_sys
->i_audio_bitmask
!= -1 )
283 codec
.audio_bitmask
= p_sys
->i_audio_bitmask
;
286 if( p_sys
->i_keyint
!= -1 )
288 codec
.framespergop
= p_sys
->i_keyint
;
291 if( p_sys
->i_bframes
!= -1 )
293 codec
.bframes
= p_sys
->i_bframes
;
296 result
= ioctl( p_sys
->i_fd
, IVTV_IOC_S_CODEC
, &codec
);
299 msg_Err( p_access
, "Failed to write new capture card settings." );
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
);
310 #ifdef HAVE_NEW_LINUX_VIDEODEV2_H
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
)
320 if( p_controls
->count
>= MAX_V4L2_CTRLS
)
322 msg_Err( p_access
, "Tried to set too many v4l2 controls at once." );
326 p_controls
->controls
[p_controls
->count
].id
= i_id
;
327 p_controls
->controls
[p_controls
->count
].value
= i_value
;
331 /*****************************************************************************
332 * V4L2SampleRate: calculate v4l2 sample rate from pvr-audio-bitmask
333 *****************************************************************************/
334 static uint32_t V4L2SampleRate( uint32_t i_bitmask
)
336 switch( i_bitmask
& 0x0003 )
338 case 0x0001: return V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000
;
339 case 0x0002: return V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000
;
341 return V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100
;
344 /*****************************************************************************
345 * V4L2AudioEncoding: calculate v4l2 audio encoding level from pvr-audio-bitmask
346 *****************************************************************************/
347 static uint32_t V4L2AudioEncoding( uint32_t i_bitmask
)
349 switch( i_bitmask
& 0x000c )
351 case 0x0004: return V4L2_MPEG_AUDIO_ENCODING_LAYER_1
;
352 case 0x0008: return V4L2_MPEG_AUDIO_ENCODING_LAYER_2
;
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
)
362 switch( i_bitmask
& 0x00f0 )
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
;
379 return V4L2_MPEG_AUDIO_L1_BITRATE_320K
;
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
)
387 switch( i_bitmask
& 0x00f0 )
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
;
404 return V4L2_MPEG_AUDIO_L2_BITRATE_192K
;
407 /*****************************************************************************
408 * V4L2AudioMode: calculate v4l2 audio mode from pvr-audio-bitmask
409 *****************************************************************************/
410 static uint32_t V4L2AudioMode( uint32_t i_bitmask
)
412 switch( i_bitmask
& 0x0300 )
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
;
418 return V4L2_MPEG_AUDIO_MODE_STEREO
;
421 /*****************************************************************************
422 * ConfigureV4L2: set up codec parameters using the new v4l2 api
423 *****************************************************************************/
424 static int ConfigureV4L2( access_t
* p_access
)
426 access_sys_t
*p_sys
= (access_sys_t
*) p_access
->p_sys
;
427 struct v4l2_ext_controls controls
;
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;
436 controls
.controls
= calloc( MAX_V4L2_CTRLS
,
437 sizeof( struct v4l2_ext_control
) );
439 if( controls
.controls
== NULL
)
442 /* Note: Ignore frame rate. Doesn't look like it can be changed. */
443 if( p_sys
->i_bitrate
!= -1 )
445 AddV4L2Ctrl( p_access
, &controls
, V4L2_CID_MPEG_VIDEO_BITRATE
,
447 msg_Dbg( p_access
, "Setting [%u] bitrate = %u",
448 controls
.count
- 1, p_sys
->i_bitrate
);
451 if( p_sys
->i_bitrate_peak
!= -1 )
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
);
459 if( p_sys
->i_bitrate_mode
!= -1 )
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
);
467 if( p_sys
->i_audio_bitmask
!= -1 )
470 AddV4L2Ctrl( p_access
, &controls
, V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ
,
471 V4L2SampleRate( p_sys
->i_audio_bitmask
) );
473 /* Encoding layer and bitrate */
474 switch( V4L2AudioEncoding( p_sys
->i_audio_bitmask
) )
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
) );
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
) );
495 /* Audio mode - stereo or mono */
496 AddV4L2Ctrl( p_access
, &controls
, V4L2_CID_MPEG_AUDIO_MODE
,
497 V4L2AudioMode( p_sys
->i_audio_bitmask
) );
499 /* See if the user wants any other audio feature */
500 if( ( p_sys
->i_audio_bitmask
& 0x1ff00 ) != 0 )
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.
508 msg_Err( p_access
, "There were bits in pvr-audio-bitmask that were not used.");
510 msg_Dbg( p_access
, "Setting audio controls");
513 if( p_sys
->i_keyint
!= -1 )
515 AddV4L2Ctrl( p_access
, &controls
, V4L2_CID_MPEG_VIDEO_GOP_SIZE
,
517 msg_Dbg( p_access
, "Setting [%u] keyint = %u",
518 controls
.count
- 1, p_sys
->i_keyint
);
521 if( p_sys
->i_bframes
!= -1 )
523 AddV4L2Ctrl( p_access
, &controls
, V4L2_CID_MPEG_VIDEO_B_FRAMES
,
525 msg_Dbg( p_access
, "Setting [%u] bframes = %u",
526 controls
.count
- 1, p_sys
->i_bframes
);
529 result
= ioctl( p_sys
->i_fd
, VIDIOC_S_EXT_CTRLS
, &controls
);
532 msg_Err( p_access
, "Failed to write %u new capture card settings.",
533 controls
.error_idx
);
535 free( controls
.controls
);
539 #endif /* HAVE_NEW_LINUX_VIDEODEV2_H */
541 /*****************************************************************************
542 * Open: open the device
543 *****************************************************************************/
544 static int Open( vlc_object_t
* p_this
)
546 access_t
*p_access
= (access_t
*) p_this
;
547 access_sys_t
* p_sys
;
550 struct v4l2_capability device_capability
;
553 memset( &device_capability
, 0, sizeof(struct v4l2_capability
) );
555 access_InitFields( p_access
);
556 ACCESS_SET_CALLBACKS( Read
, NULL
, Control
, NULL
);
557 p_sys
= p_access
->p_sys
= calloc( 1, sizeof( access_sys_t
));
558 if( !p_sys
) return VLC_ENOMEM
;
560 /* defaults values */
561 var_Create( p_access
, "pvr-caching", VLC_VAR_INTEGER
| VLC_VAR_DOINHERIT
);
563 p_sys
->psz_videodev
= var_CreateGetString( p_access
, "pvr-device" );
564 p_sys
->psz_radiodev
= var_CreateGetString( p_access
, "pvr-radio-device" );
565 p_sys
->i_standard
= var_CreateGetInteger( p_access
, "pvr-norm" );
566 p_sys
->i_width
= var_CreateGetInteger( p_access
, "pvr-width" );
567 p_sys
->i_height
= var_CreateGetInteger( p_access
, "pvr-height" );
568 p_sys
->i_frequency
= var_CreateGetInteger( p_access
, "pvr-frequency" );
569 p_sys
->i_framerate
= var_CreateGetInteger( p_access
, "pvr-framerate" );
570 p_sys
->i_keyint
= var_CreateGetInteger( p_access
, "pvr-keyint" );
571 p_sys
->i_bframes
= var_CreateGetInteger( p_access
, "pvr-bframes" );
572 p_sys
->i_bitrate
= var_CreateGetInteger( p_access
, "pvr-bitrate" );
573 p_sys
->i_bitrate_peak
= var_CreateGetInteger( p_access
, "pvr-bitrate-peak" );
574 p_sys
->i_bitrate_mode
= var_CreateGetInteger( p_access
, "pvr-bitrate-mode" );
575 p_sys
->i_audio_bitmask
= var_CreateGetInteger( p_access
, "pvr-audio-bitmask" );
576 p_sys
->i_volume
= var_CreateGetInteger( p_access
, "pvr-audio-volume" );
577 p_sys
->i_input
= var_CreateGetInteger( p_access
, "pvr-channel" );
579 /* parse command line options */
580 psz_tofree
= strdup( p_access
->psz_location
);
583 free( p_sys
->psz_radiodev
);
584 free( p_sys
->psz_videodev
);
589 psz_parser
= psz_tofree
;
592 /* Leading slash -> device path */
593 if( *psz_parser
== '/' )
595 free( p_sys
->psz_videodev
);
596 p_sys
->psz_videodev
= decode_URI_duplicate( psz_parser
);
600 /* Extract option name */
601 const char *optname
= psz_parser
;
602 psz_parser
= strchr( psz_parser
, '=' );
603 if( psz_parser
== NULL
)
605 *psz_parser
++ = '\0';
607 /* Extract option value */
608 char *optval
= psz_parser
;
609 while( memchr( ":,", *psz_parser
, 3 /* includes \0 */ ) == NULL
)
611 if( *psz_parser
) /* more options to come */
612 *psz_parser
++ = '\0'; /* skip , or : */
614 if ( !strcmp( optname
, "norm" ) )
616 if ( !strcmp( optval
, "secam" ) )
617 p_sys
->i_standard
= V4L2_STD_SECAM
;
618 else if ( !strcmp( optval
, "pal" ) )
619 p_sys
->i_standard
= V4L2_STD_PAL
;
620 else if ( !strcmp( optval
, "ntsc" ) )
621 p_sys
->i_standard
= V4L2_STD_NTSC
;
623 p_sys
->i_standard
= atoi( optval
);
625 else if( !strcmp( optname
, "channel" ) )
626 p_sys
->i_input
= atoi( optval
);
627 else if( !strcmp( optname
, "device" ) )
629 free( p_sys
->psz_videodev
);
630 if( asprintf( &p_sys
->psz_videodev
, "/dev/video%s", optval
) == -1)
631 p_sys
->psz_videodev
= NULL
;
633 else if( !strcmp( optname
, "frequency" ) )
634 p_sys
->i_frequency
= atoi( optval
);
635 else if( !strcmp( optname
, "framerate" ) )
636 p_sys
->i_framerate
= atoi( optval
);
637 else if( !strcmp( optname
, "keyint" ) )
638 p_sys
->i_keyint
= atoi( optval
);
639 else if( !strcmp( optname
, "bframes" ) )
640 p_sys
->i_bframes
= atoi( optval
);
641 else if( !strcmp( optname
, "width" ) )
642 p_sys
->i_width
= atoi( optval
);
643 else if( !strcmp( optname
, "height" ) )
644 p_sys
->i_height
= atoi( optval
);
645 else if( !strcmp( optname
, "audio" ) )
646 p_sys
->i_audio_bitmask
= atoi( optval
);
647 else if( !strcmp( optname
, "bitrate" ) )
648 p_sys
->i_bitrate
= atoi( optval
);
649 else if( !strcmp( optname
, "maxbitrate" ) )
650 p_sys
->i_bitrate_peak
= atoi( optval
);
651 else if( !strcmp( optname
, "bitratemode" ) )
653 if( !strcmp( optval
, "vbr" ) )
654 p_sys
->i_bitrate_mode
= 0;
655 else if( !strcmp( optval
, "cbr" ) )
656 p_sys
->i_bitrate_mode
= 1;
658 else if( !strcmp( optname
, "size" ) )
660 p_sys
->i_width
= strtol( optval
, &optval
, 0 );
661 p_sys
->i_height
= atoi( optval
);
666 /* open the device */
667 p_sys
->i_fd
= vlc_open( p_sys
->psz_videodev
, O_RDWR
);
668 if( p_sys
->i_fd
< 0 )
670 msg_Err( p_access
, "Cannot open device %s (%m).",
671 p_sys
->psz_videodev
);
672 Close( VLC_OBJECT(p_access
) );
675 msg_Dbg( p_access
, "Using video device: %s.", p_sys
->psz_videodev
);
677 /* See what version of ivtvdriver is running */
678 result
= ioctl( p_sys
->i_fd
, VIDIOC_QUERYCAP
, &device_capability
);
681 msg_Err( p_access
, "unknown ivtv/pvr driver version in use" );
682 Close( VLC_OBJECT(p_access
) );
686 msg_Dbg( p_access
, "%s driver (%s on %s) version %02x.%02x.%02x",
687 device_capability
.driver
,
688 device_capability
.card
,
689 device_capability
.bus_info
,
690 ( device_capability
.version
>> 16 ) & 0xff,
691 ( device_capability
.version
>> 8 ) & 0xff,
692 ( device_capability
.version
) & 0xff);
694 if ( strncmp( (char *) device_capability
.driver
, "ivtv", 4 )
695 || device_capability
.version
>= 0x000800 )
697 /* Drivers > 0.8.0 use v4l2 API instead of IVTV ioctls */
698 msg_Dbg( p_access
, "this driver uses the v4l2 API" );
699 p_sys
->b_v4l2_api
= true;
703 p_sys
->b_v4l2_api
= false;
707 if ( p_sys
->i_input
!= -1 )
709 result
= ioctl( p_sys
->i_fd
, VIDIOC_S_INPUT
, &p_sys
->i_input
);
711 msg_Warn( p_access
, "Failed to select the requested input pin." );
713 msg_Dbg( p_access
, "input set to: %d", p_sys
->i_input
);
716 /* set the video standard */
717 if ( p_sys
->i_standard
!= V4L2_STD_UNKNOWN
)
719 result
= ioctl( p_sys
->i_fd
, VIDIOC_S_STD
, &p_sys
->i_standard
);
721 msg_Warn( p_access
, "Failed to set the requested video standard." );
723 msg_Dbg( p_access
, "video standard set to: %x",
727 /* set the picture size */
728 if ( (p_sys
->i_width
!= -1) || (p_sys
->i_height
!= -1) )
730 struct v4l2_format vfmt
;
732 memset( &vfmt
, 0, sizeof(struct v4l2_format
) );
733 vfmt
.type
= V4L2_BUF_TYPE_VIDEO_CAPTURE
;
735 result
= ioctl( p_sys
->i_fd
, VIDIOC_G_FMT
, &vfmt
);
738 msg_Warn( p_access
, "Failed to read current picture size." );
742 if ( p_sys
->i_width
!= -1 )
744 vfmt
.fmt
.pix
.width
= p_sys
->i_width
;
747 if ( p_sys
->i_height
!= -1 )
749 vfmt
.fmt
.pix
.height
= p_sys
->i_height
;
752 result
= ioctl( p_sys
->i_fd
, VIDIOC_S_FMT
, &vfmt
);
755 msg_Warn( p_access
, "Failed to set requested picture size." );
759 msg_Dbg( p_access
, "picture size set to: %dx%d",
760 vfmt
.fmt
.pix
.width
, vfmt
.fmt
.pix
.height
);
765 /* set the frequency */
766 if ( p_sys
->i_frequency
!= -1 )
769 struct v4l2_tuner vt
;
771 /* TODO: let the user choose the tuner */
772 memset( &vt
, 0, sizeof(struct v4l2_tuner
) );
774 if ( (p_sys
->i_frequency
>= pi_radio_range
[0])
775 && (p_sys
->i_frequency
<= pi_radio_range
[1]) )
777 p_sys
->i_radio_fd
= vlc_open( p_sys
->psz_radiodev
, O_RDWR
);
778 if( p_sys
->i_radio_fd
< 0 )
780 msg_Err( p_access
, "Cannot open radio device (%m)." );
781 Close( VLC_OBJECT(p_access
) );
784 msg_Dbg( p_access
, "using radio device: %s",
785 p_sys
->psz_radiodev
);
786 i_fd
= p_sys
->i_radio_fd
;
791 p_sys
->i_radio_fd
= -1;
794 result
= ioctl( i_fd
, VIDIOC_G_TUNER
, &vt
);
797 msg_Warn( p_access
, "Failed to read tuner information (%m)." );
801 struct v4l2_frequency vf
;
803 memset( &vf
, 0, sizeof(struct v4l2_frequency
) );
806 result
= ioctl( i_fd
, VIDIOC_G_FREQUENCY
, &vf
);
809 msg_Warn( p_access
, "Failed to read tuner frequency (%m)." );
813 if( vt
.capability
& V4L2_TUNER_CAP_LOW
)
814 vf
.frequency
= p_sys
->i_frequency
* 16;
816 vf
.frequency
= (p_sys
->i_frequency
* 16 + 500) / 1000;
818 result
= ioctl( i_fd
, VIDIOC_S_FREQUENCY
, &vf
);
821 msg_Warn( p_access
, "Failed to set tuner frequency (%m)." );
825 msg_Dbg( p_access
, "tuner frequency set to: %d",
826 p_sys
->i_frequency
);
832 /* control parameters */
833 if ( p_sys
->i_volume
!= -1 )
835 struct v4l2_control ctrl
;
837 memset( &ctrl
, 0, sizeof(struct v4l2_control
) );
838 ctrl
.id
= V4L2_CID_AUDIO_VOLUME
;
839 ctrl
.value
= p_sys
->i_volume
;
841 result
= ioctl( p_sys
->i_fd
, VIDIOC_S_CTRL
, &ctrl
);
844 msg_Warn( p_access
, "Failed to set the volume." );
848 /* codec parameters */
849 if ( (p_sys
->i_framerate
!= -1)
850 || (p_sys
->i_bitrate_mode
!= -1)
851 || (p_sys
->i_bitrate_peak
!= -1)
852 || (p_sys
->i_keyint
!= -1)
853 || (p_sys
->i_bframes
!= -1)
854 || (p_sys
->i_bitrate
!= -1)
855 || (p_sys
->i_audio_bitmask
!= -1) )
857 if( p_sys
->b_v4l2_api
)
859 #ifdef HAVE_NEW_LINUX_VIDEODEV2_H
860 result
= ConfigureV4L2( p_access
);
861 if( result
!= VLC_SUCCESS
)
863 Close( VLC_OBJECT(p_access
) );
867 msg_Warn( p_access
, "You have new ivtvdrivers, "
868 "but this vlc was built against an old v4l2 version." );
873 result
= ConfigureIVTV( p_access
);
874 if( result
!= VLC_SUCCESS
)
876 Close( VLC_OBJECT(p_access
) );
885 /*****************************************************************************
886 * Close: close the device
887 *****************************************************************************/
888 static void Close( vlc_object_t
* p_this
)
890 access_t
*p_access
= (access_t
*) p_this
;
891 access_sys_t
*p_sys
= (access_sys_t
*) p_access
->p_sys
;
893 if ( p_sys
->i_fd
!= -1 )
894 close( p_sys
->i_fd
);
895 if ( p_sys
->i_radio_fd
!= -1 )
896 close( p_sys
->i_radio_fd
);
897 free( p_sys
->psz_videodev
);
898 free( p_sys
->psz_radiodev
);
902 /*****************************************************************************
904 *****************************************************************************/
905 static ssize_t
Read( access_t
* p_access
, uint8_t * p_buffer
, size_t i_len
)
907 access_sys_t
*p_sys
= (access_sys_t
*) p_access
->p_sys
;
911 ufd
.fd
= p_sys
->i_fd
;
914 if( p_access
->info
.b_eof
)
919 if( !vlc_object_alive (p_access
) )
924 while( ( i_ret
= poll( &ufd
, 1, 500 ) ) == 0 );
928 msg_Err( p_access
, "Polling error (%m)." );
932 i_ret
= read( p_sys
->i_fd
, p_buffer
, i_len
);
935 p_access
->info
.b_eof
= true;
939 p_access
->info
.i_pos
+= i_ret
;
945 /*****************************************************************************
947 *****************************************************************************/
948 static int Control( access_t
*p_access
, int i_query
, va_list args
)
956 case ACCESS_CAN_SEEK
:
957 case ACCESS_CAN_FASTSEEK
:
958 pb_bool
= (bool*)va_arg( args
, bool* );
961 case ACCESS_CAN_PAUSE
:
962 pb_bool
= (bool*)va_arg( args
, bool* );
965 case ACCESS_CAN_CONTROL_PACE
:
966 pb_bool
= (bool*)va_arg( args
, bool* );
971 case ACCESS_GET_PTS_DELAY
:
972 pi_64
= (int64_t*)va_arg( args
, int64_t * );
973 *pi_64
= (int64_t)var_GetInteger( p_access
, "pvr-caching" ) * 1000;
977 case ACCESS_SET_PAUSE_STATE
:
981 case ACCESS_GET_TITLE_INFO
:
982 case ACCESS_SET_TITLE
:
983 case ACCESS_SET_SEEKPOINT
:
984 case ACCESS_SET_PRIVATE_ID_STATE
:
985 case ACCESS_GET_CONTENT_TYPE
:
989 msg_Warn( p_access
, "Unimplemented query in control." );