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>
35 #include <vlc_charset.h>
37 #include <sys/types.h>
42 #include <linux/types.h>
43 #include <sys/ioctl.h>
45 #ifdef HAVE_NEW_LINUX_VIDEODEV2_H
46 # ifdef VIDEODEV2_H_FILE
47 # include VIDEODEV2_H_FILE
49 # include <linux/videodev2.h>
52 #include "videodev2.h"
55 /*****************************************************************************
57 *****************************************************************************/
58 static int Open ( vlc_object_t * );
59 static void Close( vlc_object_t * );
61 #define CACHING_TEXT N_("Caching value in ms")
62 #define CACHING_LONGTEXT N_( \
63 "Default caching value for PVR streams. This " \
64 "value should be set in milliseconds." )
66 #define DEVICE_TEXT N_( "Device" )
67 #define DEVICE_LONGTEXT N_( "PVR video device" )
69 #define RADIO_DEVICE_TEXT N_( "Radio device" )
70 #define RADIO_DEVICE_LONGTEXT N_( "PVR radio device" )
72 #define NORM_TEXT N_( "Norm" )
73 #define NORM_LONGTEXT N_( "Norm of the stream " \
74 "(Automatic, SECAM, PAL, or NTSC)." )
76 #define WIDTH_TEXT N_( "Width" )
77 #define WIDTH_LONGTEXT N_( "Width of the stream to capture " \
78 "(-1 for autodetection)." )
80 #define HEIGHT_TEXT N_( "Height" )
81 #define HEIGHT_LONGTEXT N_( "Height of the stream to capture " \
82 "(-1 for autodetection)." )
84 #define FREQUENCY_TEXT N_( "Frequency" )
85 #define FREQUENCY_LONGTEXT N_( "Frequency to capture (in kHz), if applicable." )
87 #define FRAMERATE_TEXT N_( "Framerate" )
88 #define FRAMERATE_LONGTEXT N_( "Framerate to capture, if applicable " \
89 "(-1 for autodetect)." )
91 #define KEYINT_TEXT N_( "Key interval" )
92 #define KEYINT_LONGTEXT N_( "Interval between keyframes (-1 for autodetect)." )
94 #define BFRAMES_TEXT N_( "B Frames" )
95 #define BFRAMES_LONGTEXT N_("If this option is set, B-Frames will be used. " \
96 "Use this option to set the number of B-Frames.")
98 #define BITRATE_TEXT N_( "Bitrate" )
99 #define BITRATE_LONGTEXT N_( "Bitrate to use (-1 for default)." )
101 #define BITRATE_PEAK_TEXT N_( "Bitrate peak" )
102 #define BITRATE_PEAK_LONGTEXT N_( "Peak bitrate in VBR mode." )
104 #define BITRATE_MODE_TEXT N_( "Bitrate mode" )
105 #define BITRATE_MODE_LONGTEXT N_( "Bitrate mode to use (VBR or CBR)." )
107 #define BITMASK_TEXT N_( "Audio bitmask" )
108 #define BITMASK_LONGTEXT N_("Bitmask that will "\
109 "get used by the audio part of the card." )
111 #define VOLUME_TEXT N_( "Volume" )
112 #define VOLUME_LONGTEXT N_("Audio volume (0-65535)." )
114 #define CHAN_TEXT N_( "Channel" )
115 #define CHAN_LONGTEXT N_( "Channel of the card to use (Usually, 0 = tuner, " \
116 "1 = composite, 2 = svideo)" )
118 static const int i_norm_list[] =
119 { V4L2_STD_UNKNOWN, V4L2_STD_SECAM, V4L2_STD_PAL, V4L2_STD_NTSC };
120 static const char *const psz_norm_list_text[] =
121 { N_("Automatic"), N_("SECAM"), N_("PAL"), N_("NTSC") };
123 static const int i_bitrates[] = { 0, 1 };
124 static const char *const psz_bitrates_list_text[] = { N_("vbr"), N_("cbr") };
126 static const int pi_radio_range[2] = { 65000, 108000 };
129 set_shortname( N_("PVR") )
130 set_description( N_("IVTV MPEG Encoding cards input") )
131 set_category( CAT_INPUT )
132 set_subcategory( SUBCAT_INPUT_ACCESS )
133 set_capability( "access", 0 )
134 add_shortcut( "pvr" )
136 add_integer( "pvr-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
137 CACHING_LONGTEXT, true )
138 add_string( "pvr-device", "/dev/video0", NULL, DEVICE_TEXT,
139 DEVICE_LONGTEXT, false )
140 add_string( "pvr-radio-device", "/dev/radio0", NULL, RADIO_DEVICE_TEXT,
141 RADIO_DEVICE_LONGTEXT, false )
142 add_integer( "pvr-norm", V4L2_STD_UNKNOWN , NULL, NORM_TEXT,
143 NORM_LONGTEXT, false )
144 change_integer_list( i_norm_list, psz_norm_list_text, NULL )
145 add_integer( "pvr-width", -1, NULL, WIDTH_TEXT, WIDTH_LONGTEXT, true )
146 add_integer( "pvr-height", -1, NULL, HEIGHT_TEXT, HEIGHT_LONGTEXT,
148 add_integer( "pvr-frequency", -1, NULL, FREQUENCY_TEXT, FREQUENCY_LONGTEXT,
150 add_integer( "pvr-framerate", -1, NULL, FRAMERATE_TEXT, FRAMERATE_LONGTEXT,
152 add_integer( "pvr-keyint", -1, NULL, KEYINT_TEXT, KEYINT_LONGTEXT,
154 add_integer( "pvr-bframes", -1, NULL, FRAMERATE_TEXT, FRAMERATE_LONGTEXT,
156 add_integer( "pvr-bitrate", -1, NULL, BITRATE_TEXT, BITRATE_LONGTEXT,
158 add_integer( "pvr-bitrate-peak", -1, NULL, BITRATE_PEAK_TEXT,
159 BITRATE_PEAK_LONGTEXT, true )
160 add_integer( "pvr-bitrate-mode", -1, NULL, BITRATE_MODE_TEXT,
161 BITRATE_MODE_LONGTEXT, true )
162 change_integer_list( i_bitrates, psz_bitrates_list_text, NULL )
163 add_integer( "pvr-audio-bitmask", -1, NULL, BITMASK_TEXT,
164 BITMASK_LONGTEXT, true )
165 add_integer( "pvr-audio-volume", -1, NULL, VOLUME_TEXT,
166 VOLUME_LONGTEXT, true )
167 add_integer( "pvr-channel", -1, NULL, CHAN_TEXT, CHAN_LONGTEXT, true )
169 set_callbacks( Open, Close )
172 /*****************************************************************************
174 *****************************************************************************/
175 static ssize_t Read ( access_t *, uint8_t *, size_t );
176 static int Control( access_t *, int, va_list );
178 /* ivtv specific ioctls */
179 #define IVTV_IOC_G_CODEC 0xFFEE7703
180 #define IVTV_IOC_S_CODEC 0xFFEE7704
182 /* for use with IVTV_IOC_G_CODEC and IVTV_IOC_S_CODEC */
184 struct ivtv_ioctl_codec {
186 uint32_t audio_bitmask;
188 uint32_t bitrate_mode;
190 uint32_t bitrate_peak;
192 uint32_t dnr_spatial;
193 uint32_t dnr_temporal;
196 uint32_t framespergop;
197 uint32_t gop_closure;
199 uint32_t stream_type;
204 /* file descriptor */
230 /*****************************************************************************
231 * ConfigureIVTV: set up codec parameters using the old ivtv api
232 *****************************************************************************/
233 static int ConfigureIVTV( access_t * p_access )
235 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
236 struct ivtv_ioctl_codec codec;
239 memset( &codec, 0, sizeof(struct ivtv_ioctl_codec) );
241 result = ioctl( p_sys->i_fd, IVTV_IOC_G_CODEC, &codec );
244 msg_Err( p_access, "Failed to read current capture card settings." );
248 if( p_sys->i_framerate != -1 )
250 switch( p_sys->i_framerate )
261 msg_Warn( p_access, "Invalid framerate, reverting to 25." );
267 if( p_sys->i_bitrate != -1 )
269 codec.bitrate = p_sys->i_bitrate;
272 if( p_sys->i_bitrate_peak != -1 )
274 codec.bitrate_peak = p_sys->i_bitrate_peak;
277 if( p_sys->i_bitrate_mode != -1 )
279 codec.bitrate_mode = p_sys->i_bitrate_mode;
282 if( p_sys->i_audio_bitmask != -1 )
284 codec.audio_bitmask = p_sys->i_audio_bitmask;
287 if( p_sys->i_keyint != -1 )
289 codec.framespergop = p_sys->i_keyint;
292 if( p_sys->i_bframes != -1 )
294 codec.bframes = p_sys->i_bframes;
297 result = ioctl( p_sys->i_fd, IVTV_IOC_S_CODEC, &codec );
300 msg_Err( p_access, "Failed to write new capture card settings." );
304 msg_Dbg( p_access, "Setting codec parameters to: framerate: "
305 "%d, bitrate: %d/%d/%d",
306 codec.framerate, codec.bitrate,
307 codec.bitrate_peak, codec.bitrate_mode );
311 #ifdef HAVE_NEW_LINUX_VIDEODEV2_H
313 #define MAX_V4L2_CTRLS (6)
314 /*****************************************************************************
315 * AddV4L2Ctrl: adds a control to the v4l2 controls list
316 *****************************************************************************/
317 static void AddV4L2Ctrl( access_t * p_access,
318 struct v4l2_ext_controls * p_controls,
319 uint32_t i_id, uint32_t i_value )
321 if( p_controls->count >= MAX_V4L2_CTRLS )
323 msg_Err( p_access, "Tried to set too many v4l2 controls at once." );
327 p_controls->controls[p_controls->count].id = i_id;
328 p_controls->controls[p_controls->count].value = i_value;
332 /*****************************************************************************
333 * V4L2SampleRate: calculate v4l2 sample rate from pvr-audio-bitmask
334 *****************************************************************************/
335 static uint32_t V4L2SampleRate( uint32_t i_bitmask )
337 switch( i_bitmask & 0x0003 )
339 case 0x0001: return V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000;
340 case 0x0002: return V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000;
342 return V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100;
345 /*****************************************************************************
346 * V4L2AudioEncoding: calculate v4l2 audio encoding level from pvr-audio-bitmask
347 *****************************************************************************/
348 static uint32_t V4L2AudioEncoding( uint32_t i_bitmask )
350 switch( i_bitmask & 0x000c )
352 case 0x0004: return V4L2_MPEG_AUDIO_ENCODING_LAYER_1;
353 case 0x0008: return V4L2_MPEG_AUDIO_ENCODING_LAYER_2;
358 /*****************************************************************************
359 * V4L2AudioL1Bitrate: calculate v4l2 audio bitrate for layer-1 audio from pvr-audio-bitmask
360 *****************************************************************************/
361 static uint32_t V4L2AudioL1Bitrate( uint32_t i_bitmask )
363 switch( i_bitmask & 0x00f0 )
365 case 0x0010: return V4L2_MPEG_AUDIO_L1_BITRATE_32K;
366 case 0x0020: return V4L2_MPEG_AUDIO_L1_BITRATE_64K;
367 case 0x0030: return V4L2_MPEG_AUDIO_L1_BITRATE_96K;
368 case 0x0040: return V4L2_MPEG_AUDIO_L1_BITRATE_128K;
369 case 0x0050: return V4L2_MPEG_AUDIO_L1_BITRATE_160K;
370 case 0x0060: return V4L2_MPEG_AUDIO_L1_BITRATE_192K;
371 case 0x0070: return V4L2_MPEG_AUDIO_L1_BITRATE_224K;
372 case 0x0080: return V4L2_MPEG_AUDIO_L1_BITRATE_256K;
373 case 0x0090: return V4L2_MPEG_AUDIO_L1_BITRATE_288K;
374 case 0x00a0: return V4L2_MPEG_AUDIO_L1_BITRATE_320K;
375 case 0x00b0: return V4L2_MPEG_AUDIO_L1_BITRATE_352K;
376 case 0x00c0: return V4L2_MPEG_AUDIO_L1_BITRATE_384K;
377 case 0x00d0: return V4L2_MPEG_AUDIO_L1_BITRATE_416K;
378 case 0x00e0: return V4L2_MPEG_AUDIO_L1_BITRATE_448K;
380 return V4L2_MPEG_AUDIO_L1_BITRATE_320K;
383 /*****************************************************************************
384 * V4L2AudioL2Bitrate: calculate v4l2 audio bitrate for layer-1 audio from pvr-audio-bitmask
385 *****************************************************************************/
386 static uint32_t V4L2AudioL2Bitrate( uint32_t i_bitmask )
388 switch( i_bitmask & 0x00f0 )
390 case 0x0010: return V4L2_MPEG_AUDIO_L2_BITRATE_32K;
391 case 0x0020: return V4L2_MPEG_AUDIO_L2_BITRATE_48K;
392 case 0x0030: return V4L2_MPEG_AUDIO_L2_BITRATE_56K;
393 case 0x0040: return V4L2_MPEG_AUDIO_L2_BITRATE_64K;
394 case 0x0050: return V4L2_MPEG_AUDIO_L2_BITRATE_80K;
395 case 0x0060: return V4L2_MPEG_AUDIO_L2_BITRATE_96K;
396 case 0x0070: return V4L2_MPEG_AUDIO_L2_BITRATE_112K;
397 case 0x0080: return V4L2_MPEG_AUDIO_L2_BITRATE_128K;
398 case 0x0090: return V4L2_MPEG_AUDIO_L2_BITRATE_160K;
399 case 0x00a0: return V4L2_MPEG_AUDIO_L2_BITRATE_192K;
400 case 0x00b0: return V4L2_MPEG_AUDIO_L2_BITRATE_224K;
401 case 0x00c0: return V4L2_MPEG_AUDIO_L2_BITRATE_256K;
402 case 0x00d0: return V4L2_MPEG_AUDIO_L2_BITRATE_320K;
403 case 0x00e0: return V4L2_MPEG_AUDIO_L2_BITRATE_384K;
405 return V4L2_MPEG_AUDIO_L2_BITRATE_192K;
408 /*****************************************************************************
409 * V4L2AudioMode: calculate v4l2 audio mode from pvr-audio-bitmask
410 *****************************************************************************/
411 static uint32_t V4L2AudioMode( uint32_t i_bitmask )
413 switch( i_bitmask & 0x0300 )
415 case 0x0100: return V4L2_MPEG_AUDIO_MODE_JOINT_STEREO;
416 case 0x0200: return V4L2_MPEG_AUDIO_MODE_DUAL;
417 case 0x0300: return V4L2_MPEG_AUDIO_MODE_MONO;
419 return V4L2_MPEG_AUDIO_MODE_STEREO;
422 /*****************************************************************************
423 * ConfigureV4L2: set up codec parameters using the new v4l2 api
424 *****************************************************************************/
425 static int ConfigureV4L2( access_t * p_access )
427 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
428 struct v4l2_ext_controls controls;
431 memset( &controls, 0, sizeof(struct v4l2_ext_controls) );
432 controls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
433 controls.error_idx = 0;
434 controls.reserved[0] = 0;
435 controls.reserved[1] = 0;
437 controls.controls = calloc( MAX_V4L2_CTRLS,
438 sizeof( struct v4l2_ext_control ) );
440 if( controls.controls == NULL )
443 /* Note: Ignore frame rate. Doesn't look like it can be changed. */
444 if( p_sys->i_bitrate != -1 )
446 AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_BITRATE,
448 msg_Dbg( p_access, "Setting [%u] bitrate = %u",
449 controls.count - 1, p_sys->i_bitrate );
452 if( p_sys->i_bitrate_peak != -1 )
454 AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
455 p_sys->i_bitrate_peak );
456 msg_Dbg( p_access, "Setting [%u] bitrate_peak = %u",
457 controls.count - 1, p_sys->i_bitrate_peak );
460 if( p_sys->i_bitrate_mode != -1 )
462 AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
463 p_sys->i_bitrate_mode );
464 msg_Dbg( p_access, "Setting [%u] bitrate_mode = %u",
465 controls.count - 1, p_sys->i_bitrate_mode );
468 if( p_sys->i_audio_bitmask != -1 )
471 AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
472 V4L2SampleRate( p_sys->i_audio_bitmask ) );
474 /* Encoding layer and bitrate */
475 switch( V4L2AudioEncoding( p_sys->i_audio_bitmask ) )
477 case V4L2_MPEG_AUDIO_ENCODING_LAYER_1:
478 AddV4L2Ctrl( p_access, &controls,
479 V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
480 V4L2_MPEG_AUDIO_ENCODING_LAYER_1 );
481 AddV4L2Ctrl( p_access, &controls,
482 V4L2_CID_MPEG_AUDIO_L1_BITRATE,
483 V4L2AudioL1Bitrate( p_sys->i_audio_bitmask ) );
486 case V4L2_MPEG_AUDIO_ENCODING_LAYER_2:
487 AddV4L2Ctrl( p_access, &controls,
488 V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
489 V4L2_MPEG_AUDIO_ENCODING_LAYER_2 );
490 AddV4L2Ctrl( p_access, &controls,
491 V4L2_CID_MPEG_AUDIO_L2_BITRATE,
492 V4L2AudioL2Bitrate( p_sys->i_audio_bitmask ) );
496 /* Audio mode - stereo or mono */
497 AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_AUDIO_MODE,
498 V4L2AudioMode( p_sys->i_audio_bitmask ) );
500 /* See if the user wants any other audio feature */
501 if( ( p_sys->i_audio_bitmask & 0x1ff00 ) != 0 )
503 /* It would be possible to support the bits that represent:
504 * V4L2_CID_MPEG_AUDIO_MODE_EXTENSION
505 * V4L2_CID_MPEG_AUDIO_EMPHASIS
506 * V4L2_CID_MPEG_AUDIO_CRC
507 * but they are not currently used. Tell the user.
509 msg_Err( p_access, "There were bits in pvr-audio-bitmask that were not used.");
511 msg_Dbg( p_access, "Setting audio controls");
514 if( p_sys->i_keyint != -1 )
516 AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_GOP_SIZE,
518 msg_Dbg( p_access, "Setting [%u] keyint = %u",
519 controls.count - 1, p_sys->i_keyint );
522 if( p_sys->i_bframes != -1 )
524 AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_B_FRAMES,
526 msg_Dbg( p_access, "Setting [%u] bframes = %u",
527 controls.count - 1, p_sys->i_bframes );
530 result = ioctl( p_sys->i_fd, VIDIOC_S_EXT_CTRLS, &controls );
533 msg_Err( p_access, "Failed to write %u new capture card settings.",
534 controls.error_idx );
536 free( controls.controls );
540 #endif /* HAVE_NEW_LINUX_VIDEODEV2_H */
542 /*****************************************************************************
543 * Open: open the device
544 *****************************************************************************/
545 static int Open( vlc_object_t * p_this )
547 access_t *p_access = (access_t*) p_this;
548 access_sys_t * p_sys;
551 struct v4l2_capability device_capability;
554 memset( &device_capability, 0, sizeof(struct v4l2_capability) );
556 access_InitFields( p_access );
557 ACCESS_SET_CALLBACKS( Read, NULL, Control, NULL );
558 p_sys = p_access->p_sys = calloc( 1, sizeof( access_sys_t ));
559 if( !p_sys ) return VLC_ENOMEM;
561 /* defaults values */
562 var_Create( p_access, "pvr-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
564 p_sys->psz_videodev = var_CreateGetString( p_access, "pvr-device" );
565 p_sys->psz_radiodev = var_CreateGetString( p_access, "pvr-radio-device" );
566 p_sys->i_standard = var_CreateGetInteger( p_access, "pvr-norm" );
567 p_sys->i_width = var_CreateGetInteger( p_access, "pvr-width" );
568 p_sys->i_height = var_CreateGetInteger( p_access, "pvr-height" );
569 p_sys->i_frequency = var_CreateGetInteger( p_access, "pvr-frequency" );
570 p_sys->i_framerate = var_CreateGetInteger( p_access, "pvr-framerate" );
571 p_sys->i_keyint = var_CreateGetInteger( p_access, "pvr-keyint" );
572 p_sys->i_bframes = var_CreateGetInteger( p_access, "pvr-bframes" );
573 p_sys->i_bitrate = var_CreateGetInteger( p_access, "pvr-bitrate" );
574 p_sys->i_bitrate_peak = var_CreateGetInteger( p_access, "pvr-bitrate-peak" );
575 p_sys->i_bitrate_mode = var_CreateGetInteger( p_access, "pvr-bitrate-mode" );
576 p_sys->i_audio_bitmask = var_CreateGetInteger( p_access, "pvr-audio-bitmask" );
577 p_sys->i_volume = var_CreateGetInteger( p_access, "pvr-audio-volume" );
578 p_sys->i_input = var_CreateGetInteger( p_access, "pvr-channel" );
580 /* parse command line options */
581 psz_tofree = strdup( p_access->psz_path );
584 free( p_sys->psz_radiodev );
585 free( p_sys->psz_videodev );
590 psz_parser = psz_tofree;
593 /* Leading slash -> device path */
594 if( *psz_parser == '/' )
596 free( p_sys->psz_videodev );
597 p_sys->psz_videodev = strdup( psz_parser );
601 /* Extract option name */
602 const char *optname = psz_parser;
603 psz_parser = strchr( psz_parser, '=' );
604 if( psz_parser == NULL )
606 *psz_parser++ = '\0';
608 /* Extract option value */
609 char *optval = psz_parser;
610 while( memchr( ":,", *psz_parser, 3 /* includes \0 */ ) == NULL )
612 if( *psz_parser ) /* more options to come */
613 *psz_parser++ = '\0'; /* skip , or : */
615 if ( !strcmp( optname, "norm" ) )
617 if ( !strcmp( optval, "secam" ) )
618 p_sys->i_standard = V4L2_STD_SECAM;
619 else if ( !strcmp( optval, "pal" ) )
620 p_sys->i_standard = V4L2_STD_PAL;
621 else if ( !strcmp( optval, "ntsc" ) )
622 p_sys->i_standard = V4L2_STD_NTSC;
624 p_sys->i_standard = atoi( optval );
626 else if( !strcmp( optname, "channel" ) )
627 p_sys->i_input = atoi( optval );
628 else if( !strcmp( optname, "device" ) )
630 free( p_sys->psz_videodev );
631 if( asprintf( &p_sys->psz_videodev, "/dev/video%s", optval ) == -1)
632 p_sys->psz_videodev = NULL;
634 else if( !strcmp( optname, "frequency" ) )
635 p_sys->i_frequency = atoi( optval );
636 else if( !strcmp( optname, "framerate" ) )
637 p_sys->i_framerate = atoi( optval );
638 else if( !strcmp( optname, "keyint" ) )
639 p_sys->i_keyint = atoi( optval );
640 else if( !strcmp( optname, "bframes" ) )
641 p_sys->i_bframes = atoi( optval );
642 else if( !strcmp( optname, "width" ) )
643 p_sys->i_width = atoi( optval );
644 else if( !strcmp( optname, "height" ) )
645 p_sys->i_height = atoi( optval );
646 else if( !strcmp( optname, "audio" ) )
647 p_sys->i_audio_bitmask = atoi( optval );
648 else if( !strcmp( optname, "bitrate" ) )
649 p_sys->i_bitrate = atoi( optval );
650 else if( !strcmp( optname, "maxbitrate" ) )
651 p_sys->i_bitrate_peak = atoi( optval );
652 else if( !strcmp( optname, "bitratemode" ) )
654 if( !strcmp( optval, "vbr" ) )
655 p_sys->i_bitrate_mode = 0;
656 else if( !strcmp( optval, "cbr" ) )
657 p_sys->i_bitrate_mode = 1;
659 else if( !strcmp( optname, "size" ) )
661 p_sys->i_width = strtol( optval, &optval, 0 );
662 p_sys->i_height = atoi( optval );
667 /* open the device */
668 p_sys->i_fd = utf8_open( p_sys->psz_videodev, O_RDWR );
669 if( p_sys->i_fd < 0 )
671 msg_Err( p_access, "Cannot open device %s (%m).",
672 p_sys->psz_videodev );
673 Close( VLC_OBJECT(p_access) );
676 msg_Dbg( p_access, "Using video device: %s.", p_sys->psz_videodev);
678 /* See what version of ivtvdriver is running */
679 result = ioctl( p_sys->i_fd, VIDIOC_QUERYCAP, &device_capability );
682 msg_Err( p_access, "unknown ivtv/pvr driver version in use" );
683 Close( VLC_OBJECT(p_access) );
687 msg_Dbg( p_access, "%s driver (%s on %s) version %02x.%02x.%02x",
688 device_capability.driver,
689 device_capability.card,
690 device_capability.bus_info,
691 ( device_capability.version >> 16 ) & 0xff,
692 ( device_capability.version >> 8 ) & 0xff,
693 ( device_capability.version ) & 0xff);
695 if ( strncmp( (char *) device_capability.driver, "ivtv", 4 )
696 || device_capability.version >= 0x000800 )
698 /* Drivers > 0.8.0 use v4l2 API instead of IVTV ioctls */
699 msg_Dbg( p_access, "this driver uses the v4l2 API" );
700 p_sys->b_v4l2_api = true;
704 p_sys->b_v4l2_api = false;
708 if ( p_sys->i_input != -1 )
710 result = ioctl( p_sys->i_fd, VIDIOC_S_INPUT, &p_sys->i_input );
712 msg_Warn( p_access, "Failed to select the requested input pin." );
714 msg_Dbg( p_access, "input set to: %d", p_sys->i_input );
717 /* set the video standard */
718 if ( p_sys->i_standard != V4L2_STD_UNKNOWN )
720 result = ioctl( p_sys->i_fd, VIDIOC_S_STD, &p_sys->i_standard );
722 msg_Warn( p_access, "Failed to set the requested video standard." );
724 msg_Dbg( p_access, "video standard set to: %x",
728 /* set the picture size */
729 if ( (p_sys->i_width != -1) || (p_sys->i_height != -1) )
731 struct v4l2_format vfmt;
733 memset( &vfmt, 0, sizeof(struct v4l2_format) );
734 vfmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
736 result = ioctl( p_sys->i_fd, VIDIOC_G_FMT, &vfmt );
739 msg_Warn( p_access, "Failed to read current picture size." );
743 if ( p_sys->i_width != -1 )
745 vfmt.fmt.pix.width = p_sys->i_width;
748 if ( p_sys->i_height != -1 )
750 vfmt.fmt.pix.height = p_sys->i_height;
753 result = ioctl( p_sys->i_fd, VIDIOC_S_FMT, &vfmt );
756 msg_Warn( p_access, "Failed to set requested picture size." );
760 msg_Dbg( p_access, "picture size set to: %dx%d",
761 vfmt.fmt.pix.width, vfmt.fmt.pix.height );
766 /* set the frequency */
767 if ( p_sys->i_frequency != -1 )
770 struct v4l2_tuner vt;
772 /* TODO: let the user choose the tuner */
773 memset( &vt, 0, sizeof(struct v4l2_tuner) );
775 if ( (p_sys->i_frequency >= pi_radio_range[0])
776 && (p_sys->i_frequency <= pi_radio_range[1]) )
778 p_sys->i_radio_fd = utf8_open( p_sys->psz_radiodev, O_RDWR );
779 if( p_sys->i_radio_fd < 0 )
781 msg_Err( p_access, "Cannot open radio device (%m)." );
782 Close( VLC_OBJECT(p_access) );
785 msg_Dbg( p_access, "using radio device: %s",
786 p_sys->psz_radiodev );
787 i_fd = p_sys->i_radio_fd;
792 p_sys->i_radio_fd = -1;
795 result = ioctl( i_fd, VIDIOC_G_TUNER, &vt );
798 msg_Warn( p_access, "Failed to read tuner information (%m)." );
802 struct v4l2_frequency vf;
804 memset( &vf, 0, sizeof(struct v4l2_frequency) );
807 result = ioctl( i_fd, VIDIOC_G_FREQUENCY, &vf );
810 msg_Warn( p_access, "Failed to read tuner frequency (%m)." );
814 if( vt.capability & V4L2_TUNER_CAP_LOW )
815 vf.frequency = p_sys->i_frequency * 16;
817 vf.frequency = (p_sys->i_frequency * 16 + 500) / 1000;
819 result = ioctl( i_fd, VIDIOC_S_FREQUENCY, &vf );
822 msg_Warn( p_access, "Failed to set tuner frequency (%m)." );
826 msg_Dbg( p_access, "tuner frequency set to: %d",
827 p_sys->i_frequency );
833 /* control parameters */
834 if ( p_sys->i_volume != -1 )
836 struct v4l2_control ctrl;
838 memset( &ctrl, 0, sizeof(struct v4l2_control) );
839 ctrl.id = V4L2_CID_AUDIO_VOLUME;
840 ctrl.value = p_sys->i_volume;
842 result = ioctl( p_sys->i_fd, VIDIOC_S_CTRL, &ctrl );
845 msg_Warn( p_access, "Failed to set the volume." );
849 /* codec parameters */
850 if ( (p_sys->i_framerate != -1)
851 || (p_sys->i_bitrate_mode != -1)
852 || (p_sys->i_bitrate_peak != -1)
853 || (p_sys->i_keyint != -1)
854 || (p_sys->i_bframes != -1)
855 || (p_sys->i_bitrate != -1)
856 || (p_sys->i_audio_bitmask != -1) )
858 if( p_sys->b_v4l2_api )
860 #ifdef HAVE_NEW_LINUX_VIDEODEV2_H
861 result = ConfigureV4L2( p_access );
862 if( result != VLC_SUCCESS )
864 Close( VLC_OBJECT(p_access) );
868 msg_Warn( p_access, "You have new ivtvdrivers, "
869 "but this vlc was built against an old v4l2 version." );
874 result = ConfigureIVTV( p_access );
875 if( result != VLC_SUCCESS )
877 Close( VLC_OBJECT(p_access) );
886 /*****************************************************************************
887 * Close: close the device
888 *****************************************************************************/
889 static void Close( vlc_object_t * p_this )
891 access_t *p_access = (access_t*) p_this;
892 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
894 if ( p_sys->i_fd != -1 )
895 close( p_sys->i_fd );
896 if ( p_sys->i_radio_fd != -1 )
897 close( p_sys->i_radio_fd );
898 free( p_sys->psz_videodev );
899 free( p_sys->psz_radiodev );
903 /*****************************************************************************
905 *****************************************************************************/
906 static ssize_t Read( access_t * p_access, uint8_t * p_buffer, size_t i_len )
908 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
912 ufd.fd = p_sys->i_fd;
915 if( p_access->info.b_eof )
920 if( !vlc_object_alive (p_access) )
925 while( ( i_ret = poll( &ufd, 1, 500 ) ) == 0 );
929 msg_Err( p_access, "Polling error (%m)." );
933 i_ret = read( p_sys->i_fd, p_buffer, i_len );
936 p_access->info.b_eof = true;
940 p_access->info.i_pos += i_ret;
946 /*****************************************************************************
948 *****************************************************************************/
949 static int Control( access_t *p_access, int i_query, va_list args )
957 case ACCESS_CAN_SEEK:
958 case ACCESS_CAN_FASTSEEK:
959 pb_bool = (bool*)va_arg( args, bool* );
962 case ACCESS_CAN_PAUSE:
963 pb_bool = (bool*)va_arg( args, bool* );
966 case ACCESS_CAN_CONTROL_PACE:
967 pb_bool = (bool*)va_arg( args, bool* );
972 case ACCESS_GET_PTS_DELAY:
973 pi_64 = (int64_t*)va_arg( args, int64_t * );
974 *pi_64 = (int64_t)var_GetInteger( p_access, "pvr-caching" ) * 1000;
978 case ACCESS_SET_PAUSE_STATE:
982 case ACCESS_GET_TITLE_INFO:
983 case ACCESS_SET_TITLE:
984 case ACCESS_SET_SEEKPOINT:
985 case ACCESS_SET_PRIVATE_ID_STATE:
986 case ACCESS_GET_CONTENT_TYPE:
990 msg_Warn( p_access, "Unimplemented query in control." );