1 /*****************************************************************************
3 *****************************************************************************
4 * Copyright (C) 2001, 2002 the VideoLAN team
7 * Authors: Eric Petit <titer@videolan.org>
8 * Paul Corke <paulc@datatote.co.uk>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_access.h>
37 #include <sys/types.h>
40 #include <linux/types.h>
41 #include <sys/ioctl.h>
43 #ifdef HAVE_NEW_LINUX_VIDEODEV2_H
44 # ifdef VIDEODEV2_H_FILE
45 # include VIDEODEV2_H_FILE
47 # include <linux/videodev2.h>
50 #include "videodev2.h"
53 /*****************************************************************************
55 *****************************************************************************/
56 static int Open ( vlc_object_t * );
57 static void Close( vlc_object_t * );
59 #define CACHING_TEXT N_("Caching value in ms")
60 #define CACHING_LONGTEXT N_( \
61 "Default caching value for PVR streams. This " \
62 "value should be set in milliseconds." )
64 #define DEVICE_TEXT N_( "Device" )
65 #define DEVICE_LONGTEXT N_( "PVR video device" )
67 #define RADIO_DEVICE_TEXT N_( "Radio device" )
68 #define RADIO_DEVICE_LONGTEXT N_( "PVR radio device" )
70 #define NORM_TEXT N_( "Norm" )
71 #define NORM_LONGTEXT N_( "Norm of the stream " \
72 "(Automatic, SECAM, PAL, or NTSC)." )
74 #define WIDTH_TEXT N_( "Width" )
75 #define WIDTH_LONGTEXT N_( "Width of the stream to capture " \
76 "(-1 for autodetection)." )
78 #define HEIGHT_TEXT N_( "Height" )
79 #define HEIGHT_LONGTEXT N_( "Height of the stream to capture " \
80 "(-1 for autodetection)." )
82 #define FREQUENCY_TEXT N_( "Frequency" )
83 #define FREQUENCY_LONGTEXT N_( "Frequency to capture (in kHz), if applicable." )
85 #define FRAMERATE_TEXT N_( "Framerate" )
86 #define FRAMERATE_LONGTEXT N_( "Framerate to capture, if applicable " \
87 "(-1 for autodetect)." )
89 #define KEYINT_TEXT N_( "Key interval" )
90 #define KEYINT_LONGTEXT N_( "Interval between keyframes (-1 for autodetect)." )
92 #define BFRAMES_TEXT N_( "B Frames" )
93 #define BFRAMES_LONGTEXT N_("If this option is set, B-Frames will be used. " \
94 "Use this option to set the number of B-Frames.")
96 #define BITRATE_TEXT N_( "Bitrate" )
97 #define BITRATE_LONGTEXT N_( "Bitrate to use (-1 for default)." )
99 #define BITRATE_PEAK_TEXT N_( "Bitrate peak" )
100 #define BITRATE_PEAK_LONGTEXT N_( "Peak bitrate in VBR mode." )
102 #define BITRATE_MODE_TEXT N_( "Bitrate mode" )
103 #define BITRATE_MODE_LONGTEXT N_( "Bitrate mode to use (VBR or CBR)." )
105 #define BITMASK_TEXT N_( "Audio bitmask" )
106 #define BITMASK_LONGTEXT N_("Bitmask that will "\
107 "get used by the audio part of the card." )
109 #define VOLUME_TEXT N_( "Volume" )
110 #define VOLUME_LONGTEXT N_("Audio volume (0-65535)." )
112 #define CHAN_TEXT N_( "Channel" )
113 #define CHAN_LONGTEXT N_( "Channel of the card to use (Usually, 0 = tuner, " \
114 "1 = composite, 2 = svideo)" )
116 static const int i_norm_list[] =
117 { V4L2_STD_UNKNOWN, V4L2_STD_SECAM, V4L2_STD_PAL, V4L2_STD_NTSC };
118 static const char *const psz_norm_list_text[] =
119 { N_("Automatic"), N_("SECAM"), N_("PAL"), N_("NTSC") };
121 static const int i_bitrates[] = { 0, 1 };
122 static const char *const psz_bitrates_list_text[] = { N_("vbr"), N_("cbr") };
124 static const int pi_radio_range[2] = { 65000, 108000 };
127 set_shortname( N_("PVR") )
128 set_description( N_("IVTV MPEG Encoding cards input") )
129 set_category( CAT_INPUT )
130 set_subcategory( SUBCAT_INPUT_ACCESS )
131 set_capability( "access", 0 )
132 add_shortcut( "pvr" )
134 add_integer( "pvr-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
135 CACHING_LONGTEXT, true )
136 add_string( "pvr-device", "/dev/video0", NULL, DEVICE_TEXT,
137 DEVICE_LONGTEXT, false )
138 add_string( "pvr-radio-device", "/dev/radio0", NULL, RADIO_DEVICE_TEXT,
139 RADIO_DEVICE_LONGTEXT, false )
140 add_integer( "pvr-norm", V4L2_STD_UNKNOWN , NULL, NORM_TEXT,
141 NORM_LONGTEXT, false )
142 change_integer_list( i_norm_list, psz_norm_list_text, NULL )
143 add_integer( "pvr-width", -1, NULL, WIDTH_TEXT, WIDTH_LONGTEXT, true )
144 add_integer( "pvr-height", -1, NULL, HEIGHT_TEXT, HEIGHT_LONGTEXT,
146 add_integer( "pvr-frequency", -1, NULL, FREQUENCY_TEXT, FREQUENCY_LONGTEXT,
148 add_integer( "pvr-framerate", -1, NULL, FRAMERATE_TEXT, FRAMERATE_LONGTEXT,
150 add_integer( "pvr-keyint", -1, NULL, KEYINT_TEXT, KEYINT_LONGTEXT,
152 add_integer( "pvr-bframes", -1, NULL, FRAMERATE_TEXT, FRAMERATE_LONGTEXT,
154 add_integer( "pvr-bitrate", -1, NULL, BITRATE_TEXT, BITRATE_LONGTEXT,
156 add_integer( "pvr-bitrate-peak", -1, NULL, BITRATE_PEAK_TEXT,
157 BITRATE_PEAK_LONGTEXT, true )
158 add_integer( "pvr-bitrate-mode", -1, NULL, BITRATE_MODE_TEXT,
159 BITRATE_MODE_LONGTEXT, true )
160 change_integer_list( i_bitrates, psz_bitrates_list_text, NULL )
161 add_integer( "pvr-audio-bitmask", -1, NULL, BITMASK_TEXT,
162 BITMASK_LONGTEXT, true )
163 add_integer( "pvr-audio-volume", -1, NULL, VOLUME_TEXT,
164 VOLUME_LONGTEXT, true )
165 add_integer( "pvr-channel", -1, NULL, CHAN_TEXT, CHAN_LONGTEXT, true )
167 set_callbacks( Open, Close )
170 /*****************************************************************************
172 *****************************************************************************/
173 static ssize_t Read ( access_t *, uint8_t *, size_t );
174 static int Control( access_t *, int, va_list );
176 /* ivtv specific ioctls */
177 #define IVTV_IOC_G_CODEC 0xFFEE7703
178 #define IVTV_IOC_S_CODEC 0xFFEE7704
180 /* for use with IVTV_IOC_G_CODEC and IVTV_IOC_S_CODEC */
182 struct ivtv_ioctl_codec {
184 uint32_t audio_bitmask;
186 uint32_t bitrate_mode;
188 uint32_t bitrate_peak;
190 uint32_t dnr_spatial;
191 uint32_t dnr_temporal;
194 uint32_t framespergop;
195 uint32_t gop_closure;
197 uint32_t stream_type;
202 /* file descriptor */
228 /*****************************************************************************
229 * ConfigureIVTV: set up codec parameters using the old ivtv api
230 *****************************************************************************/
231 static int ConfigureIVTV( access_t * p_access )
233 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
234 struct ivtv_ioctl_codec codec;
237 memset( &codec, 0, sizeof(struct ivtv_ioctl_codec) );
239 result = ioctl( p_sys->i_fd, IVTV_IOC_G_CODEC, &codec );
242 msg_Err( p_access, "Failed to read current capture card settings." );
246 if( p_sys->i_framerate != -1 )
248 switch( p_sys->i_framerate )
259 msg_Warn( p_access, "Invalid framerate, reverting to 25." );
265 if( p_sys->i_bitrate != -1 )
267 codec.bitrate = p_sys->i_bitrate;
270 if( p_sys->i_bitrate_peak != -1 )
272 codec.bitrate_peak = p_sys->i_bitrate_peak;
275 if( p_sys->i_bitrate_mode != -1 )
277 codec.bitrate_mode = p_sys->i_bitrate_mode;
280 if( p_sys->i_audio_bitmask != -1 )
282 codec.audio_bitmask = p_sys->i_audio_bitmask;
285 if( p_sys->i_keyint != -1 )
287 codec.framespergop = p_sys->i_keyint;
290 if( p_sys->i_bframes != -1 )
292 codec.bframes = p_sys->i_bframes;
295 result = ioctl( p_sys->i_fd, IVTV_IOC_S_CODEC, &codec );
298 msg_Err( p_access, "Failed to write new capture card settings." );
302 msg_Dbg( p_access, "Setting codec parameters to: framerate: "
303 "%d, bitrate: %d/%d/%d",
304 codec.framerate, codec.bitrate,
305 codec.bitrate_peak, codec.bitrate_mode );
309 #ifdef HAVE_NEW_LINUX_VIDEODEV2_H
311 #define MAX_V4L2_CTRLS (6)
312 /*****************************************************************************
313 * AddV4L2Ctrl: adds a control to the v4l2 controls list
314 *****************************************************************************/
315 static void AddV4L2Ctrl( access_t * p_access,
316 struct v4l2_ext_controls * p_controls,
317 uint32_t i_id, uint32_t i_value )
319 if( p_controls->count >= MAX_V4L2_CTRLS )
321 msg_Err( p_access, "Tried to set too many v4l2 controls at once." );
325 p_controls->controls[p_controls->count].id = i_id;
326 p_controls->controls[p_controls->count].value = i_value;
330 /*****************************************************************************
331 * V4L2SampleRate: calculate v4l2 sample rate from pvr-audio-bitmask
332 *****************************************************************************/
333 static uint32_t V4L2SampleRate( uint32_t i_bitmask )
335 switch( i_bitmask & 0x0003 )
337 case 0x0001: return V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000;
338 case 0x0002: return V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000;
340 return V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100;
343 /*****************************************************************************
344 * V4L2AudioEncoding: calculate v4l2 audio encoding level from pvr-audio-bitmask
345 *****************************************************************************/
346 static uint32_t V4L2AudioEncoding( uint32_t i_bitmask )
348 switch( i_bitmask & 0x000c )
350 case 0x0004: return V4L2_MPEG_AUDIO_ENCODING_LAYER_1;
351 case 0x0008: return V4L2_MPEG_AUDIO_ENCODING_LAYER_2;
356 /*****************************************************************************
357 * V4L2AudioL1Bitrate: calculate v4l2 audio bitrate for layer-1 audio from pvr-audio-bitmask
358 *****************************************************************************/
359 static uint32_t V4L2AudioL1Bitrate( uint32_t i_bitmask )
361 switch( i_bitmask & 0x00f0 )
363 case 0x0010: return V4L2_MPEG_AUDIO_L1_BITRATE_32K;
364 case 0x0020: return V4L2_MPEG_AUDIO_L1_BITRATE_64K;
365 case 0x0030: return V4L2_MPEG_AUDIO_L1_BITRATE_96K;
366 case 0x0040: return V4L2_MPEG_AUDIO_L1_BITRATE_128K;
367 case 0x0050: return V4L2_MPEG_AUDIO_L1_BITRATE_160K;
368 case 0x0060: return V4L2_MPEG_AUDIO_L1_BITRATE_192K;
369 case 0x0070: return V4L2_MPEG_AUDIO_L1_BITRATE_224K;
370 case 0x0080: return V4L2_MPEG_AUDIO_L1_BITRATE_256K;
371 case 0x0090: return V4L2_MPEG_AUDIO_L1_BITRATE_288K;
372 case 0x00a0: return V4L2_MPEG_AUDIO_L1_BITRATE_320K;
373 case 0x00b0: return V4L2_MPEG_AUDIO_L1_BITRATE_352K;
374 case 0x00c0: return V4L2_MPEG_AUDIO_L1_BITRATE_384K;
375 case 0x00d0: return V4L2_MPEG_AUDIO_L1_BITRATE_416K;
376 case 0x00e0: return V4L2_MPEG_AUDIO_L1_BITRATE_448K;
378 return V4L2_MPEG_AUDIO_L1_BITRATE_320K;
381 /*****************************************************************************
382 * V4L2AudioL2Bitrate: calculate v4l2 audio bitrate for layer-1 audio from pvr-audio-bitmask
383 *****************************************************************************/
384 static uint32_t V4L2AudioL2Bitrate( uint32_t i_bitmask )
386 switch( i_bitmask & 0x00f0 )
388 case 0x0010: return V4L2_MPEG_AUDIO_L2_BITRATE_32K;
389 case 0x0020: return V4L2_MPEG_AUDIO_L2_BITRATE_48K;
390 case 0x0030: return V4L2_MPEG_AUDIO_L2_BITRATE_56K;
391 case 0x0040: return V4L2_MPEG_AUDIO_L2_BITRATE_64K;
392 case 0x0050: return V4L2_MPEG_AUDIO_L2_BITRATE_80K;
393 case 0x0060: return V4L2_MPEG_AUDIO_L2_BITRATE_96K;
394 case 0x0070: return V4L2_MPEG_AUDIO_L2_BITRATE_112K;
395 case 0x0080: return V4L2_MPEG_AUDIO_L2_BITRATE_128K;
396 case 0x0090: return V4L2_MPEG_AUDIO_L2_BITRATE_160K;
397 case 0x00a0: return V4L2_MPEG_AUDIO_L2_BITRATE_192K;
398 case 0x00b0: return V4L2_MPEG_AUDIO_L2_BITRATE_224K;
399 case 0x00c0: return V4L2_MPEG_AUDIO_L2_BITRATE_256K;
400 case 0x00d0: return V4L2_MPEG_AUDIO_L2_BITRATE_320K;
401 case 0x00e0: return V4L2_MPEG_AUDIO_L2_BITRATE_384K;
403 return V4L2_MPEG_AUDIO_L2_BITRATE_192K;
406 /*****************************************************************************
407 * V4L2AudioMode: calculate v4l2 audio mode from pvr-audio-bitmask
408 *****************************************************************************/
409 static uint32_t V4L2AudioMode( uint32_t i_bitmask )
411 switch( i_bitmask & 0x0300 )
413 case 0x0100: return V4L2_MPEG_AUDIO_MODE_JOINT_STEREO;
414 case 0x0200: return V4L2_MPEG_AUDIO_MODE_DUAL;
415 case 0x0300: return V4L2_MPEG_AUDIO_MODE_MONO;
417 return V4L2_MPEG_AUDIO_MODE_STEREO;
420 /*****************************************************************************
421 * ConfigureV4L2: set up codec parameters using the new v4l2 api
422 *****************************************************************************/
423 static int ConfigureV4L2( access_t * p_access )
425 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
426 struct v4l2_ext_controls controls;
429 memset( &controls, 0, sizeof(struct v4l2_ext_controls) );
430 controls.ctrl_class = V4L2_CTRL_CLASS_MPEG;
431 controls.error_idx = 0;
432 controls.reserved[0] = 0;
433 controls.reserved[1] = 0;
435 controls.controls = calloc( MAX_V4L2_CTRLS,
436 sizeof( struct v4l2_ext_control ) );
438 if( controls.controls == NULL )
441 /* Note: Ignore frame rate. Doesn't look like it can be changed. */
442 if( p_sys->i_bitrate != -1 )
444 AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_BITRATE,
446 msg_Dbg( p_access, "Setting [%u] bitrate = %u",
447 controls.count - 1, p_sys->i_bitrate );
450 if( p_sys->i_bitrate_peak != -1 )
452 AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_BITRATE_PEAK,
453 p_sys->i_bitrate_peak );
454 msg_Dbg( p_access, "Setting [%u] bitrate_peak = %u",
455 controls.count - 1, p_sys->i_bitrate_peak );
458 if( p_sys->i_bitrate_mode != -1 )
460 AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_BITRATE_MODE,
461 p_sys->i_bitrate_mode );
462 msg_Dbg( p_access, "Setting [%u] bitrate_mode = %u",
463 controls.count - 1, p_sys->i_bitrate_mode );
466 if( p_sys->i_audio_bitmask != -1 )
469 AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
470 V4L2SampleRate( p_sys->i_audio_bitmask ) );
472 /* Encoding layer and bitrate */
473 switch( V4L2AudioEncoding( p_sys->i_audio_bitmask ) )
475 case V4L2_MPEG_AUDIO_ENCODING_LAYER_1:
476 AddV4L2Ctrl( p_access, &controls,
477 V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
478 V4L2_MPEG_AUDIO_ENCODING_LAYER_1 );
479 AddV4L2Ctrl( p_access, &controls,
480 V4L2_CID_MPEG_AUDIO_L1_BITRATE,
481 V4L2AudioL1Bitrate( p_sys->i_audio_bitmask ) );
484 case V4L2_MPEG_AUDIO_ENCODING_LAYER_2:
485 AddV4L2Ctrl( p_access, &controls,
486 V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ,
487 V4L2_MPEG_AUDIO_ENCODING_LAYER_2 );
488 AddV4L2Ctrl( p_access, &controls,
489 V4L2_CID_MPEG_AUDIO_L2_BITRATE,
490 V4L2AudioL2Bitrate( p_sys->i_audio_bitmask ) );
494 /* Audio mode - stereo or mono */
495 AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_AUDIO_MODE,
496 V4L2AudioMode( p_sys->i_audio_bitmask ) );
498 /* See if the user wants any other audio feature */
499 if( ( p_sys->i_audio_bitmask & 0x1ff00 ) != 0 )
501 /* It would be possible to support the bits that represent:
502 * V4L2_CID_MPEG_AUDIO_MODE_EXTENSION
503 * V4L2_CID_MPEG_AUDIO_EMPHASIS
504 * V4L2_CID_MPEG_AUDIO_CRC
505 * but they are not currently used. Tell the user.
507 msg_Err( p_access, "There were bits in pvr-audio-bitmask that were not used.");
509 msg_Dbg( p_access, "Setting audio controls");
512 if( p_sys->i_keyint != -1 )
514 AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_GOP_SIZE,
516 msg_Dbg( p_access, "Setting [%u] keyint = %u",
517 controls.count - 1, p_sys->i_keyint );
520 if( p_sys->i_bframes != -1 )
522 AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_B_FRAMES,
524 msg_Dbg( p_access, "Setting [%u] bframes = %u",
525 controls.count - 1, p_sys->i_bframes );
528 result = ioctl( p_sys->i_fd, VIDIOC_S_EXT_CTRLS, &controls );
531 msg_Err( p_access, "Failed to write %u new capture card settings.",
532 controls.error_idx );
534 free( controls.controls );
538 #endif /* HAVE_NEW_LINUX_VIDEODEV2_H */
540 /*****************************************************************************
541 * Open: open the device
542 *****************************************************************************/
543 static int Open( vlc_object_t * p_this )
545 access_t *p_access = (access_t*) p_this;
546 access_sys_t * p_sys;
549 struct v4l2_capability device_capability;
552 memset( &device_capability, 0, sizeof(struct v4l2_capability) );
554 access_InitFields( p_access );
555 ACCESS_SET_CALLBACKS( Read, NULL, Control, NULL );
556 p_sys = p_access->p_sys = calloc( 1, sizeof( access_sys_t ));
557 if( !p_sys ) return VLC_ENOMEM;
559 /* defaults values */
560 var_Create( p_access, "pvr-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
562 p_sys->psz_videodev = var_CreateGetString( p_access, "pvr-device" );
563 p_sys->psz_radiodev = var_CreateGetString( p_access, "pvr-radio-device" );
564 p_sys->i_standard = var_CreateGetInteger( p_access, "pvr-norm" );
565 p_sys->i_width = var_CreateGetInteger( p_access, "pvr-width" );
566 p_sys->i_height = var_CreateGetInteger( p_access, "pvr-height" );
567 p_sys->i_frequency = var_CreateGetInteger( p_access, "pvr-frequency" );
568 p_sys->i_framerate = var_CreateGetInteger( p_access, "pvr-framerate" );
569 p_sys->i_keyint = var_CreateGetInteger( p_access, "pvr-keyint" );
570 p_sys->i_bframes = var_CreateGetInteger( p_access, "pvr-bframes" );
571 p_sys->i_bitrate = var_CreateGetInteger( p_access, "pvr-bitrate" );
572 p_sys->i_bitrate_peak = var_CreateGetInteger( p_access, "pvr-bitrate-peak" );
573 p_sys->i_bitrate_mode = var_CreateGetInteger( p_access, "pvr-bitrate-mode" );
574 p_sys->i_audio_bitmask = var_CreateGetInteger( p_access, "pvr-audio-bitmask" );
575 p_sys->i_volume = var_CreateGetInteger( p_access, "pvr-audio-volume" );
576 p_sys->i_input = var_CreateGetInteger( p_access, "pvr-channel" );
578 /* parse command line options */
579 psz_tofree = strdup( p_access->psz_path );
582 free( p_sys->psz_radiodev );
583 free( p_sys->psz_videodev );
588 psz_parser = psz_tofree;
591 /* Leading slash -> device path */
592 if( *psz_parser == '/' )
594 free( p_sys->psz_videodev );
595 p_sys->psz_videodev = strdup( psz_parser );
599 /* Extract option name */
600 const char *optname = psz_parser;
601 psz_parser = strchr( psz_parser, '=' );
602 if( psz_parser == NULL )
604 *psz_parser++ = '\0';
606 /* Extract option value */
607 char *optval = psz_parser;
608 while( memchr( ":,", *psz_parser, 3 /* includes \0 */ ) == NULL )
610 if( *psz_parser ) /* more options to come */
611 *psz_parser++ = '\0'; /* skip , or : */
613 if ( !strcmp( optname, "norm" ) )
615 if ( !strcmp( optval, "secam" ) )
616 p_sys->i_standard = V4L2_STD_SECAM;
617 else if ( !strcmp( optval, "pal" ) )
618 p_sys->i_standard = V4L2_STD_PAL;
619 else if ( !strcmp( optval, "ntsc" ) )
620 p_sys->i_standard = V4L2_STD_NTSC;
622 p_sys->i_standard = atoi( optval );
624 else if( !strcmp( optname, "channel" ) )
625 p_sys->i_input = atoi( optval );
626 else if( !strcmp( optname, "device" ) )
628 free( p_sys->psz_videodev );
629 if( asprintf( &p_sys->psz_videodev, "/dev/video%s", optval ) == -1)
630 p_sys->psz_videodev = NULL;
632 else if( !strcmp( optname, "frequency" ) )
633 p_sys->i_frequency = atoi( optval );
634 else if( !strcmp( optname, "framerate" ) )
635 p_sys->i_framerate = atoi( optval );
636 else if( !strcmp( optname, "keyint" ) )
637 p_sys->i_keyint = atoi( optval );
638 else if( !strcmp( optname, "bframes" ) )
639 p_sys->i_bframes = atoi( optval );
640 else if( !strcmp( optname, "width" ) )
641 p_sys->i_width = atoi( optval );
642 else if( !strcmp( optname, "height" ) )
643 p_sys->i_height = atoi( optval );
644 else if( !strcmp( optname, "audio" ) )
645 p_sys->i_audio_bitmask = atoi( optval );
646 else if( !strcmp( optname, "bitrate" ) )
647 p_sys->i_bitrate = atoi( optval );
648 else if( !strcmp( optname, "maxbitrate" ) )
649 p_sys->i_bitrate_peak = atoi( optval );
650 else if( !strcmp( optname, "bitratemode" ) )
652 if( !strcmp( optval, "vbr" ) )
653 p_sys->i_bitrate_mode = 0;
654 else if( !strcmp( optval, "cbr" ) )
655 p_sys->i_bitrate_mode = 1;
657 else if( !strcmp( optname, "size" ) )
659 p_sys->i_width = strtol( optval, &optval, 0 );
660 p_sys->i_height = atoi( optval );
665 /* open the device */
666 p_sys->i_fd = vlc_open( p_sys->psz_videodev, O_RDWR );
667 if( p_sys->i_fd < 0 )
669 msg_Err( p_access, "Cannot open device %s (%m).",
670 p_sys->psz_videodev );
671 Close( VLC_OBJECT(p_access) );
674 msg_Dbg( p_access, "Using video device: %s.", p_sys->psz_videodev);
676 /* See what version of ivtvdriver is running */
677 result = ioctl( p_sys->i_fd, VIDIOC_QUERYCAP, &device_capability );
680 msg_Err( p_access, "unknown ivtv/pvr driver version in use" );
681 Close( VLC_OBJECT(p_access) );
685 msg_Dbg( p_access, "%s driver (%s on %s) version %02x.%02x.%02x",
686 device_capability.driver,
687 device_capability.card,
688 device_capability.bus_info,
689 ( device_capability.version >> 16 ) & 0xff,
690 ( device_capability.version >> 8 ) & 0xff,
691 ( device_capability.version ) & 0xff);
693 if ( strncmp( (char *) device_capability.driver, "ivtv", 4 )
694 || device_capability.version >= 0x000800 )
696 /* Drivers > 0.8.0 use v4l2 API instead of IVTV ioctls */
697 msg_Dbg( p_access, "this driver uses the v4l2 API" );
698 p_sys->b_v4l2_api = true;
702 p_sys->b_v4l2_api = false;
706 if ( p_sys->i_input != -1 )
708 result = ioctl( p_sys->i_fd, VIDIOC_S_INPUT, &p_sys->i_input );
710 msg_Warn( p_access, "Failed to select the requested input pin." );
712 msg_Dbg( p_access, "input set to: %d", p_sys->i_input );
715 /* set the video standard */
716 if ( p_sys->i_standard != V4L2_STD_UNKNOWN )
718 result = ioctl( p_sys->i_fd, VIDIOC_S_STD, &p_sys->i_standard );
720 msg_Warn( p_access, "Failed to set the requested video standard." );
722 msg_Dbg( p_access, "video standard set to: %x",
726 /* set the picture size */
727 if ( (p_sys->i_width != -1) || (p_sys->i_height != -1) )
729 struct v4l2_format vfmt;
731 memset( &vfmt, 0, sizeof(struct v4l2_format) );
732 vfmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE;
734 result = ioctl( p_sys->i_fd, VIDIOC_G_FMT, &vfmt );
737 msg_Warn( p_access, "Failed to read current picture size." );
741 if ( p_sys->i_width != -1 )
743 vfmt.fmt.pix.width = p_sys->i_width;
746 if ( p_sys->i_height != -1 )
748 vfmt.fmt.pix.height = p_sys->i_height;
751 result = ioctl( p_sys->i_fd, VIDIOC_S_FMT, &vfmt );
754 msg_Warn( p_access, "Failed to set requested picture size." );
758 msg_Dbg( p_access, "picture size set to: %dx%d",
759 vfmt.fmt.pix.width, vfmt.fmt.pix.height );
764 /* set the frequency */
765 if ( p_sys->i_frequency != -1 )
768 struct v4l2_tuner vt;
770 /* TODO: let the user choose the tuner */
771 memset( &vt, 0, sizeof(struct v4l2_tuner) );
773 if ( (p_sys->i_frequency >= pi_radio_range[0])
774 && (p_sys->i_frequency <= pi_radio_range[1]) )
776 p_sys->i_radio_fd = vlc_open( p_sys->psz_radiodev, O_RDWR );
777 if( p_sys->i_radio_fd < 0 )
779 msg_Err( p_access, "Cannot open radio device (%m)." );
780 Close( VLC_OBJECT(p_access) );
783 msg_Dbg( p_access, "using radio device: %s",
784 p_sys->psz_radiodev );
785 i_fd = p_sys->i_radio_fd;
790 p_sys->i_radio_fd = -1;
793 result = ioctl( i_fd, VIDIOC_G_TUNER, &vt );
796 msg_Warn( p_access, "Failed to read tuner information (%m)." );
800 struct v4l2_frequency vf;
802 memset( &vf, 0, sizeof(struct v4l2_frequency) );
805 result = ioctl( i_fd, VIDIOC_G_FREQUENCY, &vf );
808 msg_Warn( p_access, "Failed to read tuner frequency (%m)." );
812 if( vt.capability & V4L2_TUNER_CAP_LOW )
813 vf.frequency = p_sys->i_frequency * 16;
815 vf.frequency = (p_sys->i_frequency * 16 + 500) / 1000;
817 result = ioctl( i_fd, VIDIOC_S_FREQUENCY, &vf );
820 msg_Warn( p_access, "Failed to set tuner frequency (%m)." );
824 msg_Dbg( p_access, "tuner frequency set to: %d",
825 p_sys->i_frequency );
831 /* control parameters */
832 if ( p_sys->i_volume != -1 )
834 struct v4l2_control ctrl;
836 memset( &ctrl, 0, sizeof(struct v4l2_control) );
837 ctrl.id = V4L2_CID_AUDIO_VOLUME;
838 ctrl.value = p_sys->i_volume;
840 result = ioctl( p_sys->i_fd, VIDIOC_S_CTRL, &ctrl );
843 msg_Warn( p_access, "Failed to set the volume." );
847 /* codec parameters */
848 if ( (p_sys->i_framerate != -1)
849 || (p_sys->i_bitrate_mode != -1)
850 || (p_sys->i_bitrate_peak != -1)
851 || (p_sys->i_keyint != -1)
852 || (p_sys->i_bframes != -1)
853 || (p_sys->i_bitrate != -1)
854 || (p_sys->i_audio_bitmask != -1) )
856 if( p_sys->b_v4l2_api )
858 #ifdef HAVE_NEW_LINUX_VIDEODEV2_H
859 result = ConfigureV4L2( p_access );
860 if( result != VLC_SUCCESS )
862 Close( VLC_OBJECT(p_access) );
866 msg_Warn( p_access, "You have new ivtvdrivers, "
867 "but this vlc was built against an old v4l2 version." );
872 result = ConfigureIVTV( p_access );
873 if( result != VLC_SUCCESS )
875 Close( VLC_OBJECT(p_access) );
884 /*****************************************************************************
885 * Close: close the device
886 *****************************************************************************/
887 static void Close( vlc_object_t * p_this )
889 access_t *p_access = (access_t*) p_this;
890 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
892 if ( p_sys->i_fd != -1 )
893 close( p_sys->i_fd );
894 if ( p_sys->i_radio_fd != -1 )
895 close( p_sys->i_radio_fd );
896 free( p_sys->psz_videodev );
897 free( p_sys->psz_radiodev );
901 /*****************************************************************************
903 *****************************************************************************/
904 static ssize_t Read( access_t * p_access, uint8_t * p_buffer, size_t i_len )
906 access_sys_t *p_sys = (access_sys_t *) p_access->p_sys;
910 ufd.fd = p_sys->i_fd;
913 if( p_access->info.b_eof )
918 if( !vlc_object_alive (p_access) )
923 while( ( i_ret = poll( &ufd, 1, 500 ) ) == 0 );
927 msg_Err( p_access, "Polling error (%m)." );
931 i_ret = read( p_sys->i_fd, p_buffer, i_len );
934 p_access->info.b_eof = true;
938 p_access->info.i_pos += i_ret;
944 /*****************************************************************************
946 *****************************************************************************/
947 static int Control( access_t *p_access, int i_query, va_list args )
955 case ACCESS_CAN_SEEK:
956 case ACCESS_CAN_FASTSEEK:
957 pb_bool = (bool*)va_arg( args, bool* );
960 case ACCESS_CAN_PAUSE:
961 pb_bool = (bool*)va_arg( args, bool* );
964 case ACCESS_CAN_CONTROL_PACE:
965 pb_bool = (bool*)va_arg( args, bool* );
970 case ACCESS_GET_PTS_DELAY:
971 pi_64 = (int64_t*)va_arg( args, int64_t * );
972 *pi_64 = (int64_t)var_GetInteger( p_access, "pvr-caching" ) * 1000;
976 case ACCESS_SET_PAUSE_STATE:
980 case ACCESS_GET_TITLE_INFO:
981 case ACCESS_SET_TITLE:
982 case ACCESS_SET_SEEKPOINT:
983 case ACCESS_SET_PRIVATE_ID_STATE:
984 case ACCESS_GET_CONTENT_TYPE:
988 msg_Warn( p_access, "Unimplemented query in control." );