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 *****************************************************************************/
29 #include <vlc_access.h>
31 #include <sys/types.h>
36 #include <linux/types.h>
37 #include <sys/ioctl.h>
39 #ifdef HAVE_NEW_LINUX_VIDEODEV2_H
40 # ifdef VIDEODEV2_H_FILE
41 # include VIDEODEV2_H_FILE
43 # include <linux/videodev2.h>
46 #include "videodev2.h"
49 /*****************************************************************************
51 *****************************************************************************/
52 static int Open ( vlc_object_t * );
53 static void Close( vlc_object_t * );
55 #define CACHING_TEXT N_("Caching value in ms")
56 #define CACHING_LONGTEXT N_( \
57 "Default caching value for PVR streams. This " \
58 "value should be set in milliseconds." )
60 #define DEVICE_TEXT N_( "Device" )
61 #define DEVICE_LONGTEXT N_( "PVR video device" )
63 #define RADIO_DEVICE_TEXT N_( "Radio device" )
64 #define RADIO_DEVICE_LONGTEXT N_( "PVR radio device" )
66 #define NORM_TEXT N_( "Norm" )
67 #define NORM_LONGTEXT N_( "Norm of the stream " \
68 "(Automatic, SECAM, PAL, or NTSC)." )
70 #define WIDTH_TEXT N_( "Width" )
71 #define WIDTH_LONGTEXT N_( "Width of the stream to capture " \
72 "(-1 for autodetection)." )
74 #define HEIGHT_TEXT N_( "Height" )
75 #define HEIGHT_LONGTEXT N_( "Height of the stream to capture " \
76 "(-1 for autodetection)." )
78 #define FREQUENCY_TEXT N_( "Frequency" )
79 #define FREQUENCY_LONGTEXT N_( "Frequency to capture (in kHz), if applicable." )
81 #define FRAMERATE_TEXT N_( "Framerate" )
82 #define FRAMERATE_LONGTEXT N_( "Framerate to capture, if applicable " \
83 "(-1 for autodetect)." )
85 #define KEYINT_TEXT N_( "Key interval" )
86 #define KEYINT_LONGTEXT N_( "Interval between keyframes (-1 for autodetect)." )
88 #define BFRAMES_TEXT N_( "B Frames" )
89 #define BFRAMES_LONGTEXT N_("If this option is set, B-Frames will be used. " \
90 "Use this option to set the number of B-Frames.")
92 #define BITRATE_TEXT N_( "Bitrate" )
93 #define BITRATE_LONGTEXT N_( "Bitrate to use (-1 for default)." )
95 #define BITRATE_PEAK_TEXT N_( "Bitrate peak" )
96 #define BITRATE_PEAK_LONGTEXT N_( "Peak bitrate in VBR mode." )
98 #define BITRATE_MODE_TEXT N_( "Bitrate mode" )
99 #define BITRATE_MODE_LONGTEXT N_( "Bitrate mode to use (VBR or CBR)." )
101 #define BITMASK_TEXT N_( "Audio bitmask" )
102 #define BITMASK_LONGTEXT N_("Bitmask that will "\
103 "get used by the audio part of the card." )
105 #define VOLUME_TEXT N_( "Volume" )
106 #define VOLUME_LONGTEXT N_("Audio volume (0-65535)." )
108 #define CHAN_TEXT N_( "Channel" )
109 #define CHAN_LONGTEXT N_( "Channel of the card to use (Usually, 0 = tuner, " \
110 "1 = composite, 2 = svideo)" )
112 static int i_norm_list[] =
113 { V4L2_STD_UNKNOWN, V4L2_STD_SECAM, V4L2_STD_PAL, V4L2_STD_NTSC };
114 static const char *psz_norm_list_text[] =
115 { N_("Automatic"), N_("SECAM"), N_("PAL"), N_("NTSC") };
117 static int i_bitrates[] = { 0, 1 };
118 static const char *psz_bitrates_list_text[] = { N_("vbr"), N_("cbr") };
120 static int pi_radio_range[2] = { 65000, 108000 };
123 set_shortname( _("PVR") );
124 set_description( _("IVTV MPEG Encoding cards input") );
125 set_category( CAT_INPUT );
126 set_subcategory( SUBCAT_INPUT_ACCESS );
127 set_capability( "access2", 0 );
128 add_shortcut( "pvr" );
130 add_integer( "pvr-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
131 CACHING_LONGTEXT, VLC_TRUE );
132 add_string( "pvr-device", "/dev/video0", NULL, DEVICE_TEXT,
133 DEVICE_LONGTEXT, VLC_FALSE );
134 add_string( "pvr-radio-device", "/dev/radio0", NULL, RADIO_DEVICE_TEXT,
135 RADIO_DEVICE_LONGTEXT, VLC_FALSE );
136 add_integer( "pvr-norm", V4L2_STD_UNKNOWN , NULL, NORM_TEXT,
137 NORM_LONGTEXT, VLC_FALSE );
138 change_integer_list( i_norm_list, psz_norm_list_text, 0 );
139 add_integer( "pvr-width", -1, NULL, WIDTH_TEXT, WIDTH_LONGTEXT, VLC_TRUE );
140 add_integer( "pvr-height", -1, NULL, HEIGHT_TEXT, HEIGHT_LONGTEXT,
142 add_integer( "pvr-frequency", -1, NULL, FREQUENCY_TEXT, FREQUENCY_LONGTEXT,
144 add_integer( "pvr-framerate", -1, NULL, FRAMERATE_TEXT, FRAMERATE_LONGTEXT,
146 add_integer( "pvr-keyint", -1, NULL, KEYINT_TEXT, KEYINT_LONGTEXT,
148 add_integer( "pvr-bframes", -1, NULL, FRAMERATE_TEXT, FRAMERATE_LONGTEXT,
150 add_integer( "pvr-bitrate", -1, NULL, BITRATE_TEXT, BITRATE_LONGTEXT,
152 add_integer( "pvr-bitrate-peak", -1, NULL, BITRATE_PEAK_TEXT,
153 BITRATE_PEAK_LONGTEXT, VLC_TRUE );
154 add_integer( "pvr-bitrate-mode", -1, NULL, BITRATE_MODE_TEXT,
155 BITRATE_MODE_LONGTEXT, VLC_TRUE );
156 change_integer_list( i_bitrates, psz_bitrates_list_text, 0 );
157 add_integer( "pvr-audio-bitmask", -1, NULL, BITMASK_TEXT,
158 BITMASK_LONGTEXT, VLC_TRUE );
159 add_integer( "pvr-audio-volume", -1, NULL, VOLUME_TEXT,
160 VOLUME_LONGTEXT, VLC_TRUE );
161 add_integer( "pvr-channel", -1, NULL, CHAN_TEXT, CHAN_LONGTEXT, VLC_TRUE );
163 set_callbacks( Open, Close );
166 /*****************************************************************************
168 *****************************************************************************/
169 static int Read ( access_t *, uint8_t *, int );
170 static int Control( access_t *, int, va_list );
172 /* ivtv specific ioctls */
173 #define IVTV_IOC_G_CODEC 0xFFEE7703
174 #define IVTV_IOC_S_CODEC 0xFFEE7704
176 /* for use with IVTV_IOC_G_CODEC and IVTV_IOC_S_CODEC */
178 struct ivtv_ioctl_codec {
180 uint32_t audio_bitmask;
182 uint32_t bitrate_mode;
184 uint32_t bitrate_peak;
186 uint32_t dnr_spatial;
187 uint32_t dnr_temporal;
190 uint32_t framespergop;
191 uint32_t gop_closure;
193 uint32_t stream_type;
198 /* file descriptor */
221 vlc_bool_t b_v4l2_api;
224 /*****************************************************************************
225 * ConfigureIVTV: set up codec parameters using the old ivtv api
226 *****************************************************************************/
227 static int ConfigureIVTV( access_t * p_access )
229 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
230 struct ivtv_ioctl_codec codec;
233 memset( &codec, 0, sizeof(struct ivtv_ioctl_codec) );
235 result = ioctl( p_sys->i_fd, IVTV_IOC_G_CODEC, &codec );
238 msg_Err( p_access, "Failed to read current capture card settings." );
242 if( p_sys->i_framerate != -1 )
244 switch( p_sys->i_framerate )
255 msg_Warn( p_access, "Invalid framerate, reverting to 25." );
261 if( p_sys->i_bitrate != -1 )
263 codec.bitrate = p_sys->i_bitrate;
266 if( p_sys->i_bitrate_peak != -1 )
268 codec.bitrate_peak = p_sys->i_bitrate_peak;
271 if( p_sys->i_bitrate_mode != -1 )
273 codec.bitrate_mode = p_sys->i_bitrate_mode;
276 if( p_sys->i_audio_bitmask != -1 )
278 codec.audio_bitmask = p_sys->i_audio_bitmask;
281 if( p_sys->i_keyint != -1 )
283 codec.framespergop = p_sys->i_keyint;
286 if( p_sys->i_bframes != -1 )
288 codec.bframes = p_sys->i_bframes;
291 result = ioctl( p_sys->i_fd, IVTV_IOC_S_CODEC, &codec );
294 msg_Err( p_access, "Failed to write new capture card settings." );
298 msg_Dbg( p_access, "Setting codec parameters to: framerate: "
299 "%d, bitrate: %d/%d/%d",
300 codec.framerate, codec.bitrate,
301 codec.bitrate_peak, codec.bitrate_mode );
305 #ifdef HAVE_NEW_LINUX_VIDEODEV2_H
307 #define MAX_V4L2_CTRLS (6)
308 /*****************************************************************************
309 * AddV4L2Ctrl: adds a control to the v4l2 controls list
310 *****************************************************************************/
311 static void AddV4L2Ctrl( access_t * p_access,
312 struct v4l2_ext_controls * p_controls,
313 uint32_t i_id, uint32_t i_value )
315 if( p_controls->count >= MAX_V4L2_CTRLS )
317 msg_Err( p_access, "Tried to set too many v4l2 controls at once." );
321 p_controls->controls[p_controls->count].id = i_id;
322 p_controls->controls[p_controls->count].value = i_value;
326 /*****************************************************************************
327 * V4L2SampleRate: calculate v4l2 sample rate from pvr-audio-bitmask
328 *****************************************************************************/
329 static uint32_t V4L2SampleRate( uint32_t i_bitmask )
331 switch( i_bitmask & 0x0003 )
333 case 0x0001: return V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000;
334 case 0x0002: return V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000;
336 return V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100;
339 /*****************************************************************************
340 * V4L2AudioEncoding: calculate v4l2 audio encoding level from pvr-audio-bitmask
341 *****************************************************************************/
342 static uint32_t V4L2AudioEncoding( uint32_t i_bitmask )
344 switch( i_bitmask & 0x000c )
346 case 0x0004: return V4L2_MPEG_AUDIO_ENCODING_LAYER_1;
347 case 0x0008: return V4L2_MPEG_AUDIO_ENCODING_LAYER_2;
352 /*****************************************************************************
353 * V4L2AudioL1Bitrate: calculate v4l2 audio bitrate for layer-1 audio from pvr-audio-bitmask
354 *****************************************************************************/
355 static uint32_t V4L2AudioL1Bitrate( uint32_t i_bitmask )
357 switch( i_bitmask & 0x00f0 )
359 case 0x0010: return V4L2_MPEG_AUDIO_L1_BITRATE_32K;
360 case 0x0020: return V4L2_MPEG_AUDIO_L1_BITRATE_64K;
361 case 0x0030: return V4L2_MPEG_AUDIO_L1_BITRATE_96K;
362 case 0x0040: return V4L2_MPEG_AUDIO_L1_BITRATE_128K;
363 case 0x0050: return V4L2_MPEG_AUDIO_L1_BITRATE_160K;
364 case 0x0060: return V4L2_MPEG_AUDIO_L1_BITRATE_192K;
365 case 0x0070: return V4L2_MPEG_AUDIO_L1_BITRATE_224K;
366 case 0x0080: return V4L2_MPEG_AUDIO_L1_BITRATE_256K;
367 case 0x0090: return V4L2_MPEG_AUDIO_L1_BITRATE_288K;
368 case 0x00a0: return V4L2_MPEG_AUDIO_L1_BITRATE_320K;
369 case 0x00b0: return V4L2_MPEG_AUDIO_L1_BITRATE_352K;
370 case 0x00c0: return V4L2_MPEG_AUDIO_L1_BITRATE_384K;
371 case 0x00d0: return V4L2_MPEG_AUDIO_L1_BITRATE_416K;
372 case 0x00e0: return V4L2_MPEG_AUDIO_L1_BITRATE_448K;
374 return V4L2_MPEG_AUDIO_L1_BITRATE_320K;
377 /*****************************************************************************
378 * V4L2AudioL2Bitrate: calculate v4l2 audio bitrate for layer-1 audio from pvr-audio-bitmask
379 *****************************************************************************/
380 static uint32_t V4L2AudioL2Bitrate( uint32_t i_bitmask )
382 switch( i_bitmask & 0x00f0 )
384 case 0x0010: return V4L2_MPEG_AUDIO_L2_BITRATE_32K;
385 case 0x0020: return V4L2_MPEG_AUDIO_L2_BITRATE_48K;
386 case 0x0030: return V4L2_MPEG_AUDIO_L2_BITRATE_56K;
387 case 0x0040: return V4L2_MPEG_AUDIO_L2_BITRATE_64K;
388 case 0x0050: return V4L2_MPEG_AUDIO_L2_BITRATE_80K;
389 case 0x0060: return V4L2_MPEG_AUDIO_L2_BITRATE_96K;
390 case 0x0070: return V4L2_MPEG_AUDIO_L2_BITRATE_112K;
391 case 0x0080: return V4L2_MPEG_AUDIO_L2_BITRATE_128K;
392 case 0x0090: return V4L2_MPEG_AUDIO_L2_BITRATE_160K;
393 case 0x00a0: return V4L2_MPEG_AUDIO_L2_BITRATE_192K;
394 case 0x00b0: return V4L2_MPEG_AUDIO_L2_BITRATE_224K;
395 case 0x00c0: return V4L2_MPEG_AUDIO_L2_BITRATE_256K;
396 case 0x00d0: return V4L2_MPEG_AUDIO_L2_BITRATE_320K;
397 case 0x00e0: return V4L2_MPEG_AUDIO_L2_BITRATE_384K;
399 return V4L2_MPEG_AUDIO_L2_BITRATE_192K;
402 /*****************************************************************************
403 * V4L2AudioMode: calculate v4l2 audio mode from pvr-audio-bitmask
404 *****************************************************************************/
405 static uint32_t V4L2AudioMode( uint32_t i_bitmask )
407 switch( i_bitmask & 0x0300 )
409 case 0x0100: return V4L2_MPEG_AUDIO_MODE_JOINT_STEREO;
410 case 0x0200: return V4L2_MPEG_AUDIO_MODE_DUAL;
411 case 0x0300: return V4L2_MPEG_AUDIO_MODE_MONO;
413 return V4L2_MPEG_AUDIO_MODE_STEREO;
416 /*****************************************************************************
417 * ConfigureV4L2: set up codec parameters using the new v4l2 api
418 *****************************************************************************/
419 static int ConfigureV4L2( access_t * p_access )
421 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
422 struct v4l2_ext_controls controls;
425 memset( &controls, 0, sizeof(struct v4l2_ext_controls) );
426 controls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
427 controls.error_idx = 0;
428 controls.reserved[0] = 0;
429 controls.reserved[1] = 0;
431 controls.controls = calloc( sizeof( struct v4l2_ext_control ),
434 if( controls.controls == NULL )
437 /* Note: Ignore frame rate. Doesn't look like it can be changed. */
438 if( p_sys->i_bitrate != -1 )
440 AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_BITRATE,
442 msg_Dbg( p_access, "Setting [%u] bitrate = %u",
443 controls.count - 1, p_sys->i_bitrate );
446 if( p_sys->i_bitrate_peak != -1 )
448 AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
449 p_sys->i_bitrate_peak );
450 msg_Dbg( p_access, "Setting [%u] bitrate_peak = %u",
451 controls.count - 1, p_sys->i_bitrate_peak );
454 if( p_sys->i_bitrate_mode != -1 )
456 AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
457 p_sys->i_bitrate_mode );
458 msg_Dbg( p_access, "Setting [%u] bitrate_mode = %u",
459 controls.count - 1, p_sys->i_bitrate_mode );
462 if( p_sys->i_audio_bitmask != -1 )
465 AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
466 V4L2SampleRate( p_sys->i_audio_bitmask ) );
468 /* Encoding layer and bitrate */
469 switch( V4L2AudioEncoding( p_sys->i_audio_bitmask ) )
471 case V4L2_MPEG_AUDIO_ENCODING_LAYER_1:
472 AddV4L2Ctrl( p_access, &controls,
473 V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
474 V4L2_MPEG_AUDIO_ENCODING_LAYER_1 );
475 AddV4L2Ctrl( p_access, &controls,
476 V4L2_CID_MPEG_AUDIO_L1_BITRATE,
477 V4L2AudioL1Bitrate( p_sys->i_audio_bitmask ) );
480 case V4L2_MPEG_AUDIO_ENCODING_LAYER_2:
481 AddV4L2Ctrl( p_access, &controls,
482 V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
483 V4L2_MPEG_AUDIO_ENCODING_LAYER_2 );
484 AddV4L2Ctrl( p_access, &controls,
485 V4L2_CID_MPEG_AUDIO_L2_BITRATE,
486 V4L2AudioL2Bitrate( p_sys->i_audio_bitmask ) );
490 /* Audio mode - stereo or mono */
491 AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_AUDIO_MODE,
492 V4L2AudioMode( p_sys->i_audio_bitmask ) );
494 /* See if the user wants any other audio feature */
495 if( ( p_sys->i_audio_bitmask & 0x1ff00 ) != 0 )
497 /* It would be possible to support the bits that represent:
498 * V4L2_CID_MPEG_AUDIO_MODE_EXTENSION
499 * V4L2_CID_MPEG_AUDIO_EMPHASIS
500 * V4L2_CID_MPEG_AUDIO_CRC
501 * but they are not currently used. Tell the user.
503 msg_Err( p_access, "There were bits in pvr-audio-bitmask that were not used.");
505 msg_Dbg( p_access, "Setting audio controls");
508 if( p_sys->i_keyint != -1 )
510 AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_GOP_SIZE,
512 msg_Dbg( p_access, "Setting [%u] keyint = %u",
513 controls.count - 1, p_sys->i_keyint );
516 if( p_sys->i_bframes != -1 )
518 AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_B_FRAMES,
520 msg_Dbg( p_access, "Setting [%u] bframes = %u",
521 controls.count - 1, p_sys->i_bframes );
524 result = ioctl( p_sys->i_fd, VIDIOC_S_EXT_CTRLS, &controls );
527 msg_Err( p_access, "Failed to write %u new capture card settings.",
528 controls.error_idx );
530 free( controls.controls );
534 #endif /* HAVE_NEW_LINUX_VIDEODEV2_H */
536 /*****************************************************************************
537 * Open: open the device
538 *****************************************************************************/
539 static int Open( vlc_object_t * p_this )
541 access_t *p_access = (access_t*) p_this;
542 access_sys_t * p_sys;
545 char * psz_device = NULL;
547 struct v4l2_capability device_capability;
550 memset( &device_capability, 0, sizeof(struct v4l2_capability) );
552 p_access->pf_read = Read;
553 p_access->pf_block = NULL;
554 p_access->pf_seek = NULL;
555 p_access->pf_control = Control;
556 p_access->info.i_update = 0;
557 p_access->info.i_size = 0;
558 p_access->info.i_pos = 0;
559 p_access->info.b_eof = VLC_FALSE;
560 p_access->info.i_title = 0;
561 p_access->info.i_seekpoint = 0;
563 /* create private access data */
564 p_sys = calloc( sizeof( access_sys_t ), 1 );
568 p_access->p_sys = p_sys;
570 /* defaults values */
571 var_Create( p_access, "pvr-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
573 var_Create( p_access, "pvr-device", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
574 var_Get( p_access, "pvr-device" , &val);
575 p_sys->psz_videodev = val.psz_string;
577 var_Create( p_access, "pvr-radio-device", VLC_VAR_STRING |
579 var_Get( p_access, "pvr-radio-device" , &val);
580 p_sys->psz_radiodev = val.psz_string;
582 var_Create( p_access, "pvr-norm", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
583 var_Get( p_access, "pvr-norm" , &val);
584 p_sys->i_standard = val.i_int;
586 var_Create( p_access, "pvr-width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
587 var_Get( p_access, "pvr-width" , &val);
588 p_sys->i_width = val.i_int;
590 var_Create( p_access, "pvr-height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
591 var_Get( p_access, "pvr-height" , &val);
592 p_sys->i_height = val.i_int;
594 var_Create( p_access, "pvr-frequency", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
595 var_Get( p_access, "pvr-frequency" , &val);
596 p_sys->i_frequency = val.i_int;
598 var_Create( p_access, "pvr-framerate", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
599 var_Get( p_access, "pvr-framerate" , &val);
600 p_sys->i_framerate = val.i_int;
602 var_Create( p_access, "pvr-keyint", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
603 var_Get( p_access, "pvr-keyint" , &val);
604 p_sys->i_keyint = val.i_int;
606 var_Create( p_access, "pvr-bframes", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
607 var_Get( p_access, "pvr-bframes" , &val);
608 p_sys->i_bframes = val.b_bool;
610 var_Create( p_access, "pvr-bitrate", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
611 var_Get( p_access, "pvr-bitrate" , &val);
612 p_sys->i_bitrate = val.i_int;
614 var_Create( p_access, "pvr-bitrate-peak", VLC_VAR_INTEGER |
616 var_Get( p_access, "pvr-bitrate-peak" , &val);
617 p_sys->i_bitrate_peak = val.i_int;
619 var_Create( p_access, "pvr-bitrate-mode", VLC_VAR_INTEGER |
621 var_Get( p_access, "pvr-bitrate-mode" , &val);
622 p_sys->i_bitrate_mode = val.i_int;
624 var_Create( p_access, "pvr-audio-bitmask", VLC_VAR_INTEGER |
626 var_Get( p_access, "pvr-audio-bitmask" , &val);
627 p_sys->i_audio_bitmask = val.i_int;
629 var_Create( p_access, "pvr-audio-volume", VLC_VAR_INTEGER |
631 var_Get( p_access, "pvr-audio-volume" , &val);
632 p_sys->i_volume = val.i_int;
634 var_Create( p_access, "pvr-channel", VLC_VAR_INTEGER |
636 var_Get( p_access, "pvr-channel" , &val);
637 p_sys->i_input = val.i_int;
639 /* parse command line options */
640 psz_tofree = strdup( p_access->psz_path );
644 psz_parser = psz_tofree;
649 if ( !strncmp( psz_parser, "norm=", strlen( "norm=" ) ) )
651 char *psz_parser_init;
652 psz_parser += strlen( "norm=" );
653 psz_parser_init = psz_parser;
654 while ( (*psz_parser != ':')
655 && (*psz_parser != ',')
656 && (*psz_parser != '\0') )
661 if ( !strncmp( psz_parser_init, "secam" ,
662 psz_parser - psz_parser_init ) )
664 p_sys->i_standard = V4L2_STD_SECAM;
666 else if ( !strncmp( psz_parser_init, "pal" ,
667 psz_parser - psz_parser_init ) )
669 p_sys->i_standard = V4L2_STD_PAL;
671 else if ( !strncmp( psz_parser_init, "ntsc" ,
672 psz_parser - psz_parser_init ) )
674 p_sys->i_standard = V4L2_STD_NTSC;
678 p_sys->i_standard = strtol( psz_parser_init ,
682 else if( !strncmp( psz_parser, "channel=",
683 strlen( "channel=" ) ) )
686 strtol( psz_parser + strlen( "channel=" ),
689 else if( !strncmp( psz_parser, "device=", strlen( "device=" ) ) )
691 int i_len = strlen( "/dev/videox" );
692 psz_device = calloc( i_len + 1, 1 );
696 snprintf( psz_device, i_len, "/dev/video%ld",
697 strtol( psz_parser + strlen( "device=" ),
700 else if( !strncmp( psz_parser, "frequency=",
701 strlen( "frequency=" ) ) )
704 strtol( psz_parser + strlen( "frequency=" ),
707 else if( !strncmp( psz_parser, "framerate=",
708 strlen( "framerate=" ) ) )
711 strtol( psz_parser + strlen( "framerate=" ),
714 else if( !strncmp( psz_parser, "keyint=",
715 strlen( "keyint=" ) ) )
718 strtol( psz_parser + strlen( "keyint=" ),
721 else if( !strncmp( psz_parser, "bframes=",
722 strlen( "bframes=" ) ) )
725 strtol( psz_parser + strlen( "bframes=" ),
729 else if( !strncmp( psz_parser, "width=",
730 strlen( "width=" ) ) )
733 strtol( psz_parser + strlen( "width=" ),
736 else if( !strncmp( psz_parser, "height=",
737 strlen( "height=" ) ) )
740 strtol( psz_parser + strlen( "height=" ),
743 else if( !strncmp( psz_parser, "audio=",
744 strlen( "audio=" ) ) )
746 p_sys->i_audio_bitmask =
747 strtol( psz_parser + strlen( "audio=" ),
750 else if( !strncmp( psz_parser, "bitrate=",
751 strlen( "bitrate=" ) ) )
754 strtol( psz_parser + strlen( "bitrate=" ),
757 else if( !strncmp( psz_parser, "maxbitrate=",
758 strlen( "maxbitrate=" ) ) )
760 p_sys->i_bitrate_peak =
761 strtol( psz_parser + strlen( "maxbitrate=" ),
764 else if( !strncmp( psz_parser, "bitratemode=",
765 strlen( "bitratemode=" ) ) )
767 char *psz_parser_init;
768 psz_parser += strlen( "bitratemode=" );
769 psz_parser_init = psz_parser;
770 while ( (*psz_parser != ':')
771 && (*psz_parser != ',')
772 && (*psz_parser != '\0') )
777 if ( !strncmp( psz_parser_init, "vbr" ,
778 psz_parser - psz_parser_init ) )
780 p_sys->i_bitrate_mode = 0;
782 else if ( !strncmp( psz_parser_init, "cbr" ,
783 psz_parser - psz_parser_init ) )
785 p_sys->i_bitrate_mode = 1;
788 else if( !strncmp( psz_parser, "size=",
789 strlen( "size=" ) ) )
792 strtol( psz_parser + strlen( "size=" ),
795 strtol( psz_parser + 1 ,
800 char *psz_parser_init;
801 psz_parser_init = psz_parser;
802 while ( (*psz_parser != ':') &&
803 (*psz_parser != ',') &&
804 (*psz_parser != '\0') )
808 psz_device = calloc( psz_parser - psz_parser_init + 1, 1 );
812 strncpy( psz_device, psz_parser_init,
813 psz_parser - psz_parser_init );
825 if( p_sys->psz_videodev )
826 free( p_sys->psz_videodev );
827 p_sys->psz_videodev = psz_device;
830 /* open the device */
831 p_sys->i_fd = open( p_sys->psz_videodev, O_RDWR );
832 if( p_sys->i_fd < 0 )
834 msg_Err( p_access, "Cannot open device (%m)." );
835 Close( VLC_OBJECT(p_access) );
838 msg_Dbg( p_access, "Using video device: %s.", p_sys->psz_videodev);
840 /* See what version of ivtvdriver is running */
841 result = ioctl( p_sys->i_fd, VIDIOC_QUERYCAP, &device_capability );
844 msg_Err( p_access, "unknown ivtv driver version in use" );
845 Close( VLC_OBJECT(p_access) );
849 msg_Dbg( p_access, "ivtv driver version %02x.%02x.%02x",
850 ( device_capability.version >> 16 ) & 0xff,
851 ( device_capability.version >> 8 ) & 0xff,
852 ( device_capability.version ) & 0xff);
854 if ( device_capability.version >= 0x000800 )
856 /* Drivers > 0.8.0 use v4l2 API instead of IVTV ioctls */
857 msg_Dbg( p_access, "this driver uses the v4l2 API" );
858 p_sys->b_v4l2_api = VLC_TRUE;
862 p_sys->b_v4l2_api = VLC_FALSE;
866 if ( p_sys->i_input != -1 )
868 result = ioctl( p_sys->i_fd, VIDIOC_S_INPUT, &p_sys->i_input );
870 msg_Warn( p_access, "Failed to select the requested input pin." );
872 msg_Dbg( p_access, "input set to: %d", p_sys->i_input );
875 /* set the video standard */
876 if ( p_sys->i_standard != V4L2_STD_UNKNOWN )
878 result = ioctl( p_sys->i_fd, VIDIOC_S_STD, &p_sys->i_standard );
880 msg_Warn( p_access, "Failed to set the requested video standard." );
882 msg_Dbg( p_access, "video standard set to: %x",
886 /* set the picture size */
887 if ( (p_sys->i_width != -1) || (p_sys->i_height != -1) )
889 struct v4l2_format vfmt;
891 memset( &vfmt, 0, sizeof(struct v4l2_format) );
892 vfmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
894 result = ioctl( p_sys->i_fd, VIDIOC_G_FMT, &vfmt );
897 msg_Warn( p_access, "Failed to read current picture size." );
901 if ( p_sys->i_width != -1 )
903 vfmt.fmt.pix.width = p_sys->i_width;
906 if ( p_sys->i_height != -1 )
908 vfmt.fmt.pix.height = p_sys->i_height;
911 result = ioctl( p_sys->i_fd, VIDIOC_S_FMT, &vfmt );
914 msg_Warn( p_access, "Failed to set requested picture size." );
918 msg_Dbg( p_access, "picture size set to: %dx%d",
919 vfmt.fmt.pix.width, vfmt.fmt.pix.height );
924 /* set the frequency */
925 if ( p_sys->i_frequency != -1 )
928 struct v4l2_tuner vt;
930 /* TODO: let the user choose the tuner */
931 memset( &vt, 0, sizeof(struct v4l2_tuner) );
933 if ( (p_sys->i_frequency >= pi_radio_range[0])
934 && (p_sys->i_frequency <= pi_radio_range[1]) )
936 p_sys->i_radio_fd = open( p_sys->psz_radiodev, O_RDWR );
937 if( p_sys->i_radio_fd < 0 )
939 msg_Err( p_access, "Cannot open radio device (%m)." );
940 Close( VLC_OBJECT(p_access) );
943 msg_Dbg( p_access, "using radio device: %s",
944 p_sys->psz_radiodev );
945 i_fd = p_sys->i_radio_fd;
950 p_sys->i_radio_fd = -1;
953 result = ioctl( i_fd, VIDIOC_G_TUNER, &vt );
956 msg_Warn( p_access, "Failed to read tuner information (%m)." );
960 struct v4l2_frequency vf;
962 memset( &vf, 0, sizeof(struct v4l2_frequency) );
965 result = ioctl( i_fd, VIDIOC_G_FREQUENCY, &vf );
968 msg_Warn( p_access, "Failed to read tuner frequency (%m)." );
972 if( vt.capability & V4L2_TUNER_CAP_LOW )
973 vf.frequency = p_sys->i_frequency * 16;
975 vf.frequency = (p_sys->i_frequency * 16 + 500) / 1000;
977 result = ioctl( i_fd, VIDIOC_S_FREQUENCY, &vf );
980 msg_Warn( p_access, "Failed to set tuner frequency (%m)." );
984 msg_Dbg( p_access, "tuner frequency set to: %d",
985 p_sys->i_frequency );
991 /* control parameters */
992 if ( p_sys->i_volume != -1 )
994 struct v4l2_control ctrl;
996 memset( &ctrl, 0, sizeof(struct v4l2_control) );
997 ctrl.id = V4L2_CID_AUDIO_VOLUME;
998 ctrl.value = p_sys->i_volume;
1000 result = ioctl( p_sys->i_fd, VIDIOC_S_CTRL, &ctrl );
1003 msg_Warn( p_access, "Failed to set the volume." );
1007 /* codec parameters */
1008 if ( (p_sys->i_framerate != -1)
1009 || (p_sys->i_bitrate_mode != -1)
1010 || (p_sys->i_bitrate_peak != -1)
1011 || (p_sys->i_keyint != -1)
1012 || (p_sys->i_bframes != -1)
1013 || (p_sys->i_bitrate != -1)
1014 || (p_sys->i_audio_bitmask != -1) )
1016 if( p_sys->b_v4l2_api )
1018 #ifdef HAVE_NEW_LINUX_VIDEODEV2_H
1019 result = ConfigureV4L2( p_access );
1020 if( result != VLC_SUCCESS )
1022 Close( VLC_OBJECT(p_access) );
1026 msg_Warn( p_access, "You have new ivtvdrivers, "
1027 "but this vlc was built against an old v4l2 version." );
1032 result = ConfigureIVTV( p_access );
1033 if( result != VLC_SUCCESS )
1035 Close( VLC_OBJECT(p_access) );
1044 /*****************************************************************************
1045 * Close: close the device
1046 *****************************************************************************/
1047 static void Close( vlc_object_t * p_this )
1049 access_t *p_access = (access_t*) p_this;
1050 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
1052 if ( p_sys->i_fd != -1 )
1053 close( p_sys->i_fd );
1054 if ( p_sys->i_radio_fd != -1 )
1055 close( p_sys->i_radio_fd );
1056 if ( p_sys->psz_videodev )
1057 free( p_sys->psz_videodev );
1058 if ( p_sys->psz_radiodev )
1059 free( p_sys->psz_radiodev );
1063 /*****************************************************************************
1065 *****************************************************************************/
1066 static int Read( access_t * p_access, uint8_t * p_buffer, int i_len )
1068 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
1072 ufd.fd = p_sys->i_fd;
1073 ufd.events = POLLIN;
1075 if( p_access->info.b_eof )
1080 if( p_access->b_die )
1085 while( ( i_ret = poll( &ufd, 1, 500 ) ) == 0 );
1089 msg_Err( p_access, "Polling error (%m)." );
1093 i_ret = read( p_sys->i_fd, p_buffer, i_len );
1096 p_access->info.b_eof = VLC_TRUE;
1098 else if( i_ret > 0 )
1100 p_access->info.i_pos += i_ret;
1106 /*****************************************************************************
1108 *****************************************************************************/
1109 static int Control( access_t *p_access, int i_query, va_list args )
1111 vlc_bool_t *pb_bool;
1118 case ACCESS_CAN_SEEK:
1119 case ACCESS_CAN_FASTSEEK:
1120 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
1121 *pb_bool = VLC_FALSE;
1123 case ACCESS_CAN_PAUSE:
1124 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
1125 *pb_bool = VLC_FALSE;
1127 case ACCESS_CAN_CONTROL_PACE:
1128 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
1129 *pb_bool = VLC_FALSE;
1133 case ACCESS_GET_MTU:
1134 pi_int = (int*)va_arg( args, int * );
1138 case ACCESS_GET_PTS_DELAY:
1139 pi_64 = (int64_t*)va_arg( args, int64_t * );
1140 *pi_64 = (int64_t)var_GetInteger( p_access, "pvr-caching" ) * 1000;
1144 case ACCESS_SET_PAUSE_STATE:
1148 case ACCESS_GET_TITLE_INFO:
1149 case ACCESS_SET_TITLE:
1150 case ACCESS_SET_SEEKPOINT:
1151 case ACCESS_SET_PRIVATE_ID_STATE:
1152 return VLC_EGENERIC;
1155 msg_Warn( p_access, "Unimplemented query in control." );
1156 return VLC_EGENERIC;