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 );
133 add_string( "pvr-device", "/dev/video0", NULL, DEVICE_TEXT,
134 DEVICE_LONGTEXT, VLC_FALSE );
136 add_string( "pvr-radio-device", "/dev/radio0", NULL, RADIO_DEVICE_TEXT,
137 RADIO_DEVICE_LONGTEXT, VLC_FALSE );
139 add_integer( "pvr-norm", V4L2_STD_UNKNOWN , NULL, NORM_TEXT,
140 NORM_LONGTEXT, VLC_FALSE );
142 change_integer_list( i_norm_list, psz_norm_list_text, 0 );
143 add_integer( "pvr-width", -1, NULL, WIDTH_TEXT, WIDTH_LONGTEXT, VLC_TRUE );
145 add_integer( "pvr-height", -1, NULL, HEIGHT_TEXT, HEIGHT_LONGTEXT,
148 add_integer( "pvr-frequency", -1, NULL, FREQUENCY_TEXT, FREQUENCY_LONGTEXT,
151 add_integer( "pvr-framerate", -1, NULL, FRAMERATE_TEXT, FRAMERATE_LONGTEXT,
154 add_integer( "pvr-keyint", -1, NULL, KEYINT_TEXT, KEYINT_LONGTEXT,
157 add_integer( "pvr-bframes", -1, NULL, FRAMERATE_TEXT, FRAMERATE_LONGTEXT,
160 add_integer( "pvr-bitrate", -1, NULL, BITRATE_TEXT, BITRATE_LONGTEXT,
163 add_integer( "pvr-bitrate-peak", -1, NULL, BITRATE_PEAK_TEXT,
164 BITRATE_PEAK_LONGTEXT, VLC_TRUE );
166 add_integer( "pvr-bitrate-mode", -1, NULL, BITRATE_MODE_TEXT,
167 BITRATE_MODE_LONGTEXT, VLC_TRUE );
169 change_integer_list( i_bitrates, psz_bitrates_list_text, 0 );
170 add_integer( "pvr-audio-bitmask", -1, NULL, BITMASK_TEXT,
171 BITMASK_LONGTEXT, VLC_TRUE );
173 add_integer( "pvr-audio-volume", -1, NULL, VOLUME_TEXT,
174 VOLUME_LONGTEXT, VLC_TRUE );
176 add_integer( "pvr-channel", -1, NULL, CHAN_TEXT, CHAN_LONGTEXT, VLC_TRUE );
179 set_callbacks( Open, Close );
182 /*****************************************************************************
184 *****************************************************************************/
185 static ssize_t Read ( access_t *, uint8_t *, size_t );
186 static int Control( access_t *, int, va_list );
188 /* ivtv specific ioctls */
189 #define IVTV_IOC_G_CODEC 0xFFEE7703
190 #define IVTV_IOC_S_CODEC 0xFFEE7704
192 /* for use with IVTV_IOC_G_CODEC and IVTV_IOC_S_CODEC */
194 struct ivtv_ioctl_codec {
196 uint32_t audio_bitmask;
198 uint32_t bitrate_mode;
200 uint32_t bitrate_peak;
202 uint32_t dnr_spatial;
203 uint32_t dnr_temporal;
206 uint32_t framespergop;
207 uint32_t gop_closure;
209 uint32_t stream_type;
214 /* file descriptor */
237 vlc_bool_t b_v4l2_api;
240 /*****************************************************************************
241 * ConfigureIVTV: set up codec parameters using the old ivtv api
242 *****************************************************************************/
243 static int ConfigureIVTV( access_t * p_access )
245 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
246 struct ivtv_ioctl_codec codec;
249 memset( &codec, 0, sizeof(struct ivtv_ioctl_codec) );
251 result = ioctl( p_sys->i_fd, IVTV_IOC_G_CODEC, &codec );
254 msg_Err( p_access, "Failed to read current capture card settings." );
258 if( p_sys->i_framerate != -1 )
260 switch( p_sys->i_framerate )
271 msg_Warn( p_access, "Invalid framerate, reverting to 25." );
277 if( p_sys->i_bitrate != -1 )
279 codec.bitrate = p_sys->i_bitrate;
282 if( p_sys->i_bitrate_peak != -1 )
284 codec.bitrate_peak = p_sys->i_bitrate_peak;
287 if( p_sys->i_bitrate_mode != -1 )
289 codec.bitrate_mode = p_sys->i_bitrate_mode;
292 if( p_sys->i_audio_bitmask != -1 )
294 codec.audio_bitmask = p_sys->i_audio_bitmask;
297 if( p_sys->i_keyint != -1 )
299 codec.framespergop = p_sys->i_keyint;
302 if( p_sys->i_bframes != -1 )
304 codec.bframes = p_sys->i_bframes;
307 result = ioctl( p_sys->i_fd, IVTV_IOC_S_CODEC, &codec );
310 msg_Err( p_access, "Failed to write new capture card settings." );
314 msg_Dbg( p_access, "Setting codec parameters to: framerate: "
315 "%d, bitrate: %d/%d/%d",
316 codec.framerate, codec.bitrate,
317 codec.bitrate_peak, codec.bitrate_mode );
321 #ifdef HAVE_NEW_LINUX_VIDEODEV2_H
323 #define MAX_V4L2_CTRLS (6)
324 /*****************************************************************************
325 * AddV4L2Ctrl: adds a control to the v4l2 controls list
326 *****************************************************************************/
327 static void AddV4L2Ctrl( access_t * p_access,
328 struct v4l2_ext_controls * p_controls,
329 uint32_t i_id, uint32_t i_value )
331 if( p_controls->count >= MAX_V4L2_CTRLS )
333 msg_Err( p_access, "Tried to set too many v4l2 controls at once." );
337 p_controls->controls[p_controls->count].id = i_id;
338 p_controls->controls[p_controls->count].value = i_value;
342 /*****************************************************************************
343 * V4L2SampleRate: calculate v4l2 sample rate from pvr-audio-bitmask
344 *****************************************************************************/
345 static uint32_t V4L2SampleRate( uint32_t i_bitmask )
347 switch( i_bitmask & 0x0003 )
349 case 0x0001: return V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000;
350 case 0x0002: return V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000;
352 return V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100;
355 /*****************************************************************************
356 * V4L2AudioEncoding: calculate v4l2 audio encoding level from pvr-audio-bitmask
357 *****************************************************************************/
358 static uint32_t V4L2AudioEncoding( uint32_t i_bitmask )
360 switch( i_bitmask & 0x000c )
362 case 0x0004: return V4L2_MPEG_AUDIO_ENCODING_LAYER_1;
363 case 0x0008: return V4L2_MPEG_AUDIO_ENCODING_LAYER_2;
368 /*****************************************************************************
369 * V4L2AudioL1Bitrate: calculate v4l2 audio bitrate for layer-1 audio from pvr-audio-bitmask
370 *****************************************************************************/
371 static uint32_t V4L2AudioL1Bitrate( uint32_t i_bitmask )
373 switch( i_bitmask & 0x00f0 )
375 case 0x0010: return V4L2_MPEG_AUDIO_L1_BITRATE_32K;
376 case 0x0020: return V4L2_MPEG_AUDIO_L1_BITRATE_64K;
377 case 0x0030: return V4L2_MPEG_AUDIO_L1_BITRATE_96K;
378 case 0x0040: return V4L2_MPEG_AUDIO_L1_BITRATE_128K;
379 case 0x0050: return V4L2_MPEG_AUDIO_L1_BITRATE_160K;
380 case 0x0060: return V4L2_MPEG_AUDIO_L1_BITRATE_192K;
381 case 0x0070: return V4L2_MPEG_AUDIO_L1_BITRATE_224K;
382 case 0x0080: return V4L2_MPEG_AUDIO_L1_BITRATE_256K;
383 case 0x0090: return V4L2_MPEG_AUDIO_L1_BITRATE_288K;
384 case 0x00a0: return V4L2_MPEG_AUDIO_L1_BITRATE_320K;
385 case 0x00b0: return V4L2_MPEG_AUDIO_L1_BITRATE_352K;
386 case 0x00c0: return V4L2_MPEG_AUDIO_L1_BITRATE_384K;
387 case 0x00d0: return V4L2_MPEG_AUDIO_L1_BITRATE_416K;
388 case 0x00e0: return V4L2_MPEG_AUDIO_L1_BITRATE_448K;
390 return V4L2_MPEG_AUDIO_L1_BITRATE_320K;
393 /*****************************************************************************
394 * V4L2AudioL2Bitrate: calculate v4l2 audio bitrate for layer-1 audio from pvr-audio-bitmask
395 *****************************************************************************/
396 static uint32_t V4L2AudioL2Bitrate( uint32_t i_bitmask )
398 switch( i_bitmask & 0x00f0 )
400 case 0x0010: return V4L2_MPEG_AUDIO_L2_BITRATE_32K;
401 case 0x0020: return V4L2_MPEG_AUDIO_L2_BITRATE_48K;
402 case 0x0030: return V4L2_MPEG_AUDIO_L2_BITRATE_56K;
403 case 0x0040: return V4L2_MPEG_AUDIO_L2_BITRATE_64K;
404 case 0x0050: return V4L2_MPEG_AUDIO_L2_BITRATE_80K;
405 case 0x0060: return V4L2_MPEG_AUDIO_L2_BITRATE_96K;
406 case 0x0070: return V4L2_MPEG_AUDIO_L2_BITRATE_112K;
407 case 0x0080: return V4L2_MPEG_AUDIO_L2_BITRATE_128K;
408 case 0x0090: return V4L2_MPEG_AUDIO_L2_BITRATE_160K;
409 case 0x00a0: return V4L2_MPEG_AUDIO_L2_BITRATE_192K;
410 case 0x00b0: return V4L2_MPEG_AUDIO_L2_BITRATE_224K;
411 case 0x00c0: return V4L2_MPEG_AUDIO_L2_BITRATE_256K;
412 case 0x00d0: return V4L2_MPEG_AUDIO_L2_BITRATE_320K;
413 case 0x00e0: return V4L2_MPEG_AUDIO_L2_BITRATE_384K;
415 return V4L2_MPEG_AUDIO_L2_BITRATE_192K;
418 /*****************************************************************************
419 * V4L2AudioMode: calculate v4l2 audio mode from pvr-audio-bitmask
420 *****************************************************************************/
421 static uint32_t V4L2AudioMode( uint32_t i_bitmask )
423 switch( i_bitmask & 0x0300 )
425 case 0x0100: return V4L2_MPEG_AUDIO_MODE_JOINT_STEREO;
426 case 0x0200: return V4L2_MPEG_AUDIO_MODE_DUAL;
427 case 0x0300: return V4L2_MPEG_AUDIO_MODE_MONO;
429 return V4L2_MPEG_AUDIO_MODE_STEREO;
432 /*****************************************************************************
433 * ConfigureV4L2: set up codec parameters using the new v4l2 api
434 *****************************************************************************/
435 static int ConfigureV4L2( access_t * p_access )
437 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
438 struct v4l2_ext_controls controls;
441 memset( &controls, 0, sizeof(struct v4l2_ext_controls) );
442 controls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
443 controls.error_idx = 0;
444 controls.reserved[0] = 0;
445 controls.reserved[1] = 0;
447 controls.controls = calloc( sizeof( struct v4l2_ext_control ),
450 if( controls.controls == NULL )
453 /* Note: Ignore frame rate. Doesn't look like it can be changed. */
454 if( p_sys->i_bitrate != -1 )
456 AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_BITRATE,
458 msg_Dbg( p_access, "Setting [%u] bitrate = %u",
459 controls.count - 1, p_sys->i_bitrate );
462 if( p_sys->i_bitrate_peak != -1 )
464 AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
465 p_sys->i_bitrate_peak );
466 msg_Dbg( p_access, "Setting [%u] bitrate_peak = %u",
467 controls.count - 1, p_sys->i_bitrate_peak );
470 if( p_sys->i_bitrate_mode != -1 )
472 AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
473 p_sys->i_bitrate_mode );
474 msg_Dbg( p_access, "Setting [%u] bitrate_mode = %u",
475 controls.count - 1, p_sys->i_bitrate_mode );
478 if( p_sys->i_audio_bitmask != -1 )
481 AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
482 V4L2SampleRate( p_sys->i_audio_bitmask ) );
484 /* Encoding layer and bitrate */
485 switch( V4L2AudioEncoding( p_sys->i_audio_bitmask ) )
487 case V4L2_MPEG_AUDIO_ENCODING_LAYER_1:
488 AddV4L2Ctrl( p_access, &controls,
489 V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
490 V4L2_MPEG_AUDIO_ENCODING_LAYER_1 );
491 AddV4L2Ctrl( p_access, &controls,
492 V4L2_CID_MPEG_AUDIO_L1_BITRATE,
493 V4L2AudioL1Bitrate( p_sys->i_audio_bitmask ) );
496 case V4L2_MPEG_AUDIO_ENCODING_LAYER_2:
497 AddV4L2Ctrl( p_access, &controls,
498 V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
499 V4L2_MPEG_AUDIO_ENCODING_LAYER_2 );
500 AddV4L2Ctrl( p_access, &controls,
501 V4L2_CID_MPEG_AUDIO_L2_BITRATE,
502 V4L2AudioL2Bitrate( p_sys->i_audio_bitmask ) );
506 /* Audio mode - stereo or mono */
507 AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_AUDIO_MODE,
508 V4L2AudioMode( p_sys->i_audio_bitmask ) );
510 /* See if the user wants any other audio feature */
511 if( ( p_sys->i_audio_bitmask & 0x1ff00 ) != 0 )
513 /* It would be possible to support the bits that represent:
514 * V4L2_CID_MPEG_AUDIO_MODE_EXTENSION
515 * V4L2_CID_MPEG_AUDIO_EMPHASIS
516 * V4L2_CID_MPEG_AUDIO_CRC
517 * but they are not currently used. Tell the user.
519 msg_Err( p_access, "There were bits in pvr-audio-bitmask that were not used.");
521 msg_Dbg( p_access, "Setting audio controls");
524 if( p_sys->i_keyint != -1 )
526 AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_GOP_SIZE,
528 msg_Dbg( p_access, "Setting [%u] keyint = %u",
529 controls.count - 1, p_sys->i_keyint );
532 if( p_sys->i_bframes != -1 )
534 AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_B_FRAMES,
536 msg_Dbg( p_access, "Setting [%u] bframes = %u",
537 controls.count - 1, p_sys->i_bframes );
540 result = ioctl( p_sys->i_fd, VIDIOC_S_EXT_CTRLS, &controls );
543 msg_Err( p_access, "Failed to write %u new capture card settings.",
544 controls.error_idx );
546 free( controls.controls );
550 #endif /* HAVE_NEW_LINUX_VIDEODEV2_H */
552 /*****************************************************************************
553 * Open: open the device
554 *****************************************************************************/
555 static int Open( vlc_object_t * p_this )
557 access_t *p_access = (access_t*) p_this;
558 access_sys_t * p_sys;
561 char * psz_device = NULL;
563 struct v4l2_capability device_capability;
566 memset( &device_capability, 0, sizeof(struct v4l2_capability) );
568 p_access->pf_read = Read;
569 p_access->pf_block = NULL;
570 p_access->pf_seek = NULL;
571 p_access->pf_control = Control;
572 p_access->info.i_update = 0;
573 p_access->info.i_size = 0;
574 p_access->info.i_pos = 0;
575 p_access->info.b_eof = VLC_FALSE;
576 p_access->info.i_title = 0;
577 p_access->info.i_seekpoint = 0;
579 /* create private access data */
580 p_sys = calloc( sizeof( access_sys_t ), 1 );
584 p_access->p_sys = p_sys;
586 /* defaults values */
587 var_Create( p_access, "pvr-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
589 var_Create( p_access, "pvr-device", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
590 var_Get( p_access, "pvr-device" , &val);
591 p_sys->psz_videodev = val.psz_string;
593 var_Create( p_access, "pvr-radio-device", VLC_VAR_STRING |
595 var_Get( p_access, "pvr-radio-device" , &val);
596 p_sys->psz_radiodev = val.psz_string;
598 var_Create( p_access, "pvr-norm", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
599 var_Get( p_access, "pvr-norm" , &val);
600 p_sys->i_standard = val.i_int;
602 var_Create( p_access, "pvr-width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
603 var_Get( p_access, "pvr-width" , &val);
604 p_sys->i_width = val.i_int;
606 var_Create( p_access, "pvr-height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
607 var_Get( p_access, "pvr-height" , &val);
608 p_sys->i_height = val.i_int;
610 var_Create( p_access, "pvr-frequency", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
611 var_Get( p_access, "pvr-frequency" , &val);
612 p_sys->i_frequency = val.i_int;
614 var_Create( p_access, "pvr-framerate", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
615 var_Get( p_access, "pvr-framerate" , &val);
616 p_sys->i_framerate = val.i_int;
618 var_Create( p_access, "pvr-keyint", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
619 var_Get( p_access, "pvr-keyint" , &val);
620 p_sys->i_keyint = val.i_int;
622 var_Create( p_access, "pvr-bframes", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
623 var_Get( p_access, "pvr-bframes" , &val);
624 p_sys->i_bframes = val.b_bool;
626 var_Create( p_access, "pvr-bitrate", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
627 var_Get( p_access, "pvr-bitrate" , &val);
628 p_sys->i_bitrate = val.i_int;
630 var_Create( p_access, "pvr-bitrate-peak", VLC_VAR_INTEGER |
632 var_Get( p_access, "pvr-bitrate-peak" , &val);
633 p_sys->i_bitrate_peak = val.i_int;
635 var_Create( p_access, "pvr-bitrate-mode", VLC_VAR_INTEGER |
637 var_Get( p_access, "pvr-bitrate-mode" , &val);
638 p_sys->i_bitrate_mode = val.i_int;
640 var_Create( p_access, "pvr-audio-bitmask", VLC_VAR_INTEGER |
642 var_Get( p_access, "pvr-audio-bitmask" , &val);
643 p_sys->i_audio_bitmask = val.i_int;
645 var_Create( p_access, "pvr-audio-volume", VLC_VAR_INTEGER |
647 var_Get( p_access, "pvr-audio-volume" , &val);
648 p_sys->i_volume = val.i_int;
650 var_Create( p_access, "pvr-channel", VLC_VAR_INTEGER |
652 var_Get( p_access, "pvr-channel" , &val);
653 p_sys->i_input = val.i_int;
655 /* parse command line options */
656 psz_tofree = strdup( p_access->psz_path );
660 psz_parser = psz_tofree;
665 if ( !strncmp( psz_parser, "norm=", strlen( "norm=" ) ) )
667 char *psz_parser_init;
668 psz_parser += strlen( "norm=" );
669 psz_parser_init = psz_parser;
670 while ( (*psz_parser != ':')
671 && (*psz_parser != ',')
672 && (*psz_parser != '\0') )
677 if ( !strncmp( psz_parser_init, "secam" ,
678 psz_parser - psz_parser_init ) )
680 p_sys->i_standard = V4L2_STD_SECAM;
682 else if ( !strncmp( psz_parser_init, "pal" ,
683 psz_parser - psz_parser_init ) )
685 p_sys->i_standard = V4L2_STD_PAL;
687 else if ( !strncmp( psz_parser_init, "ntsc" ,
688 psz_parser - psz_parser_init ) )
690 p_sys->i_standard = V4L2_STD_NTSC;
694 p_sys->i_standard = strtol( psz_parser_init ,
698 else if( !strncmp( psz_parser, "channel=",
699 strlen( "channel=" ) ) )
702 strtol( psz_parser + strlen( "channel=" ),
705 else if( !strncmp( psz_parser, "device=", strlen( "device=" ) ) )
707 int i_len = strlen( "/dev/videox" );
708 psz_device = calloc( i_len + 1, 1 );
712 snprintf( psz_device, i_len, "/dev/video%ld",
713 strtol( psz_parser + strlen( "device=" ),
716 else if( !strncmp( psz_parser, "frequency=",
717 strlen( "frequency=" ) ) )
720 strtol( psz_parser + strlen( "frequency=" ),
723 else if( !strncmp( psz_parser, "framerate=",
724 strlen( "framerate=" ) ) )
727 strtol( psz_parser + strlen( "framerate=" ),
730 else if( !strncmp( psz_parser, "keyint=",
731 strlen( "keyint=" ) ) )
734 strtol( psz_parser + strlen( "keyint=" ),
737 else if( !strncmp( psz_parser, "bframes=",
738 strlen( "bframes=" ) ) )
741 strtol( psz_parser + strlen( "bframes=" ),
745 else if( !strncmp( psz_parser, "width=",
746 strlen( "width=" ) ) )
749 strtol( psz_parser + strlen( "width=" ),
752 else if( !strncmp( psz_parser, "height=",
753 strlen( "height=" ) ) )
756 strtol( psz_parser + strlen( "height=" ),
759 else if( !strncmp( psz_parser, "audio=",
760 strlen( "audio=" ) ) )
762 p_sys->i_audio_bitmask =
763 strtol( psz_parser + strlen( "audio=" ),
766 else if( !strncmp( psz_parser, "bitrate=",
767 strlen( "bitrate=" ) ) )
770 strtol( psz_parser + strlen( "bitrate=" ),
773 else if( !strncmp( psz_parser, "maxbitrate=",
774 strlen( "maxbitrate=" ) ) )
776 p_sys->i_bitrate_peak =
777 strtol( psz_parser + strlen( "maxbitrate=" ),
780 else if( !strncmp( psz_parser, "bitratemode=",
781 strlen( "bitratemode=" ) ) )
783 char *psz_parser_init;
784 psz_parser += strlen( "bitratemode=" );
785 psz_parser_init = psz_parser;
786 while ( (*psz_parser != ':')
787 && (*psz_parser != ',')
788 && (*psz_parser != '\0') )
793 if ( !strncmp( psz_parser_init, "vbr" ,
794 psz_parser - psz_parser_init ) )
796 p_sys->i_bitrate_mode = 0;
798 else if ( !strncmp( psz_parser_init, "cbr" ,
799 psz_parser - psz_parser_init ) )
801 p_sys->i_bitrate_mode = 1;
804 else if( !strncmp( psz_parser, "size=",
805 strlen( "size=" ) ) )
808 strtol( psz_parser + strlen( "size=" ),
811 strtol( psz_parser + 1 ,
816 char *psz_parser_init;
817 psz_parser_init = psz_parser;
818 while ( (*psz_parser != ':') &&
819 (*psz_parser != ',') &&
820 (*psz_parser != '\0') )
824 psz_device = calloc( psz_parser - psz_parser_init + 1, 1 );
828 strncpy( psz_device, psz_parser_init,
829 psz_parser - psz_parser_init );
841 if( p_sys->psz_videodev )
842 free( p_sys->psz_videodev );
843 p_sys->psz_videodev = psz_device;
846 /* open the device */
847 p_sys->i_fd = open( p_sys->psz_videodev, O_RDWR );
848 if( p_sys->i_fd < 0 )
850 msg_Err( p_access, "Cannot open device (%m)." );
851 Close( VLC_OBJECT(p_access) );
854 msg_Dbg( p_access, "Using video device: %s.", p_sys->psz_videodev);
856 /* See what version of ivtvdriver is running */
857 result = ioctl( p_sys->i_fd, VIDIOC_QUERYCAP, &device_capability );
860 msg_Err( p_access, "unknown ivtv/pvr driver version in use" );
861 Close( VLC_OBJECT(p_access) );
865 msg_Dbg( p_access, "%s driver (%s on %s) version %02x.%02x.%02x",
866 device_capability.driver,
867 device_capability.card,
868 device_capability.bus_info,
869 ( device_capability.version >> 16 ) & 0xff,
870 ( device_capability.version >> 8 ) & 0xff,
871 ( device_capability.version ) & 0xff);
873 if ( strncmp( (char *) device_capability.driver, "ivtv", 4 )
874 || device_capability.version >= 0x000800 )
876 /* Drivers > 0.8.0 use v4l2 API instead of IVTV ioctls */
877 msg_Dbg( p_access, "this driver uses the v4l2 API" );
878 p_sys->b_v4l2_api = VLC_TRUE;
882 p_sys->b_v4l2_api = VLC_FALSE;
886 if ( p_sys->i_input != -1 )
888 result = ioctl( p_sys->i_fd, VIDIOC_S_INPUT, &p_sys->i_input );
890 msg_Warn( p_access, "Failed to select the requested input pin." );
892 msg_Dbg( p_access, "input set to: %d", p_sys->i_input );
895 /* set the video standard */
896 if ( p_sys->i_standard != V4L2_STD_UNKNOWN )
898 result = ioctl( p_sys->i_fd, VIDIOC_S_STD, &p_sys->i_standard );
900 msg_Warn( p_access, "Failed to set the requested video standard." );
902 msg_Dbg( p_access, "video standard set to: %x",
906 /* set the picture size */
907 if ( (p_sys->i_width != -1) || (p_sys->i_height != -1) )
909 struct v4l2_format vfmt;
911 memset( &vfmt, 0, sizeof(struct v4l2_format) );
912 vfmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
914 result = ioctl( p_sys->i_fd, VIDIOC_G_FMT, &vfmt );
917 msg_Warn( p_access, "Failed to read current picture size." );
921 if ( p_sys->i_width != -1 )
923 vfmt.fmt.pix.width = p_sys->i_width;
926 if ( p_sys->i_height != -1 )
928 vfmt.fmt.pix.height = p_sys->i_height;
931 result = ioctl( p_sys->i_fd, VIDIOC_S_FMT, &vfmt );
934 msg_Warn( p_access, "Failed to set requested picture size." );
938 msg_Dbg( p_access, "picture size set to: %dx%d",
939 vfmt.fmt.pix.width, vfmt.fmt.pix.height );
944 /* set the frequency */
945 if ( p_sys->i_frequency != -1 )
948 struct v4l2_tuner vt;
950 /* TODO: let the user choose the tuner */
951 memset( &vt, 0, sizeof(struct v4l2_tuner) );
953 if ( (p_sys->i_frequency >= pi_radio_range[0])
954 && (p_sys->i_frequency <= pi_radio_range[1]) )
956 p_sys->i_radio_fd = open( p_sys->psz_radiodev, O_RDWR );
957 if( p_sys->i_radio_fd < 0 )
959 msg_Err( p_access, "Cannot open radio device (%m)." );
960 Close( VLC_OBJECT(p_access) );
963 msg_Dbg( p_access, "using radio device: %s",
964 p_sys->psz_radiodev );
965 i_fd = p_sys->i_radio_fd;
970 p_sys->i_radio_fd = -1;
973 result = ioctl( i_fd, VIDIOC_G_TUNER, &vt );
976 msg_Warn( p_access, "Failed to read tuner information (%m)." );
980 struct v4l2_frequency vf;
982 memset( &vf, 0, sizeof(struct v4l2_frequency) );
985 result = ioctl( i_fd, VIDIOC_G_FREQUENCY, &vf );
988 msg_Warn( p_access, "Failed to read tuner frequency (%m)." );
992 if( vt.capability & V4L2_TUNER_CAP_LOW )
993 vf.frequency = p_sys->i_frequency * 16;
995 vf.frequency = (p_sys->i_frequency * 16 + 500) / 1000;
997 result = ioctl( i_fd, VIDIOC_S_FREQUENCY, &vf );
1000 msg_Warn( p_access, "Failed to set tuner frequency (%m)." );
1004 msg_Dbg( p_access, "tuner frequency set to: %d",
1005 p_sys->i_frequency );
1011 /* control parameters */
1012 if ( p_sys->i_volume != -1 )
1014 struct v4l2_control ctrl;
1016 memset( &ctrl, 0, sizeof(struct v4l2_control) );
1017 ctrl.id = V4L2_CID_AUDIO_VOLUME;
1018 ctrl.value = p_sys->i_volume;
1020 result = ioctl( p_sys->i_fd, VIDIOC_S_CTRL, &ctrl );
1023 msg_Warn( p_access, "Failed to set the volume." );
1027 /* codec parameters */
1028 if ( (p_sys->i_framerate != -1)
1029 || (p_sys->i_bitrate_mode != -1)
1030 || (p_sys->i_bitrate_peak != -1)
1031 || (p_sys->i_keyint != -1)
1032 || (p_sys->i_bframes != -1)
1033 || (p_sys->i_bitrate != -1)
1034 || (p_sys->i_audio_bitmask != -1) )
1036 if( p_sys->b_v4l2_api )
1038 #ifdef HAVE_NEW_LINUX_VIDEODEV2_H
1039 result = ConfigureV4L2( p_access );
1040 if( result != VLC_SUCCESS )
1042 Close( VLC_OBJECT(p_access) );
1046 msg_Warn( p_access, "You have new ivtvdrivers, "
1047 "but this vlc was built against an old v4l2 version." );
1052 result = ConfigureIVTV( p_access );
1053 if( result != VLC_SUCCESS )
1055 Close( VLC_OBJECT(p_access) );
1064 /*****************************************************************************
1065 * Close: close the device
1066 *****************************************************************************/
1067 static void Close( vlc_object_t * p_this )
1069 access_t *p_access = (access_t*) p_this;
1070 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
1072 if ( p_sys->i_fd != -1 )
1073 close( p_sys->i_fd );
1074 if ( p_sys->i_radio_fd != -1 )
1075 close( p_sys->i_radio_fd );
1076 if ( p_sys->psz_videodev )
1077 free( p_sys->psz_videodev );
1078 if ( p_sys->psz_radiodev )
1079 free( p_sys->psz_radiodev );
1083 /*****************************************************************************
1085 *****************************************************************************/
1086 static ssize_t Read( access_t * p_access, uint8_t * p_buffer, size_t i_len )
1088 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
1092 ufd.fd = p_sys->i_fd;
1093 ufd.events = POLLIN;
1095 if( p_access->info.b_eof )
1100 if( p_access->b_die )
1105 while( ( i_ret = poll( &ufd, 1, 500 ) ) == 0 );
1109 msg_Err( p_access, "Polling error (%m)." );
1113 i_ret = read( p_sys->i_fd, p_buffer, i_len );
1116 p_access->info.b_eof = VLC_TRUE;
1118 else if( i_ret > 0 )
1120 p_access->info.i_pos += i_ret;
1126 /*****************************************************************************
1128 *****************************************************************************/
1129 static int Control( access_t *p_access, int i_query, va_list args )
1131 vlc_bool_t *pb_bool;
1138 case ACCESS_CAN_SEEK:
1139 case ACCESS_CAN_FASTSEEK:
1140 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
1141 *pb_bool = VLC_FALSE;
1143 case ACCESS_CAN_PAUSE:
1144 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
1145 *pb_bool = VLC_FALSE;
1147 case ACCESS_CAN_CONTROL_PACE:
1148 pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
1149 *pb_bool = VLC_FALSE;
1153 case ACCESS_GET_MTU:
1154 pi_int = (int*)va_arg( args, int * );
1158 case ACCESS_GET_PTS_DELAY:
1159 pi_64 = (int64_t*)va_arg( args, int64_t * );
1160 *pi_64 = (int64_t)var_GetInteger( p_access, "pvr-caching" ) * 1000;
1164 case ACCESS_SET_PAUSE_STATE:
1168 case ACCESS_GET_TITLE_INFO:
1169 case ACCESS_SET_TITLE:
1170 case ACCESS_SET_SEEKPOINT:
1171 case ACCESS_SET_PRIVATE_ID_STATE:
1172 case ACCESS_GET_CONTENT_TYPE:
1173 return VLC_EGENERIC;
1176 msg_Warn( p_access, "Unimplemented query in control." );
1177 return VLC_EGENERIC;