Index: modules/access/pvr.c =================================================================== --- modules/access/pvr.c (revision 19653) +++ modules/access/pvr.c (working copy) @@ -5,6 +5,7 @@ * $Id$ * * Authors: Eric Petit + * Paul Corke * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by @@ -36,7 +37,11 @@ #include #include #include +#if HAVE_NEW_LINUX_VIDEODEV2_H +#include VIDEODEV2_H_FILE +#else #include "videodev2.h" +#endif /***************************************************************************** * Module descriptor @@ -204,9 +209,288 @@ int i_audio_bitmask; int i_input; int i_volume; + + /* driver version */ + vlc_bool_t b_v4l2_api; }; /***************************************************************************** + * ConfigureIVTV: set up codec parameters using the old ivtv api + *****************************************************************************/ +static void ConfigureIVTV( access_t * p_access, access_sys_t * p_sys ) +{ + struct ivtv_ioctl_codec codec; + + if( ioctl( p_sys->i_fd, IVTV_IOC_G_CODEC, &codec ) < 0 ) + { + msg_Warn( p_access, "Failed to read current capture card settings." ); + } + else + { + if( p_sys->i_framerate != -1 ) + { + switch( p_sys->i_framerate ) + { + case 30: + codec.framerate = 0; + break; + + case 25: + codec.framerate = 1; + break; + + default: + msg_Warn( p_access, "Invalid framerate, reverting to 25." ); + codec.framerate = 1; + break; + } + } + + if( p_sys->i_bitrate != -1 ) + { + codec.bitrate = p_sys->i_bitrate; + } + + if( p_sys->i_bitrate_peak != -1 ) + { + codec.bitrate_peak = p_sys->i_bitrate_peak; + } + + if( p_sys->i_bitrate_mode != -1 ) + { + codec.bitrate_mode = p_sys->i_bitrate_mode; + } + + if( p_sys->i_audio_bitmask != -1 ) + { + codec.audio_bitmask = p_sys->i_audio_bitmask; + } + + if( p_sys->i_keyint != -1 ) + { + codec.framespergop = p_sys->i_keyint; + } + + if( p_sys->i_bframes != -1 ) + { + codec.bframes = p_sys->i_bframes; + } + + if( ioctl( p_sys->i_fd, IVTV_IOC_S_CODEC, &codec ) < 0 ) + { + msg_Warn( p_access, "Failed to write new capture card settings." ); + } + else + { + msg_Dbg( p_access, "Setting codec parameters to: framerate: %d, bitrate: %d/%d/%d", + codec.framerate, codec.bitrate, codec.bitrate_peak, codec.bitrate_mode ); + } + } +} + +#if HAVE_NEW_LINUX_VIDEODEV2_H + +#define MAX_V4L2_CTRLS (6) +/***************************************************************************** + * AddV4L2Ctrl: adds a control to the v4l2 controls list + *****************************************************************************/ +static void AddV4L2Ctrl( access_t * p_access, struct v4l2_ext_controls * p_controls, uint32_t i_id, uint32_t i_value ) +{ + if( p_controls->count < MAX_V4L2_CTRLS ) + { + p_controls->controls[p_controls->count].id = i_id; + p_controls->controls[p_controls->count].value = i_value; + ++p_controls->count; + } + else + { + msg_Err( p_access, "Tried to set too many v4l2 controls at once." ); + } +} + +/***************************************************************************** + * V4L2SampleRate: calculate v4l2 sample rate from pvr-audio-bitmask + *****************************************************************************/ +static uint32_t V4L2SampleRate( uint32_t i_bitmask ) +{ + switch( i_bitmask & 0x0003 ) + { + case 0x0001: return V4L2_MPEG_AUDIO_SAMPLING_FREQ_48000; + case 0x0002: return V4L2_MPEG_AUDIO_SAMPLING_FREQ_32000; + } + return V4L2_MPEG_AUDIO_SAMPLING_FREQ_44100; +} + +/***************************************************************************** + * V4L2AudioEncoding: calculate v4l2 audio encoding level from pvr-audio-bitmask + *****************************************************************************/ +static uint32_t V4L2AudioEncoding( uint32_t i_bitmask ) +{ + switch( i_bitmask & 0x000c ) + { + case 0x0004: return V4L2_MPEG_AUDIO_ENCODING_LAYER_1; + case 0x0008: return V4L2_MPEG_AUDIO_ENCODING_LAYER_2; + } + return 0xffffffff; +} + +/***************************************************************************** + * V4L2AudioL1Bitrate: calculate v4l2 audio bitrate for layer-1 audio from pvr-audio-bitmask + *****************************************************************************/ +static uint32_t V4L2AudioL1Bitrate( uint32_t i_bitmask ) +{ + switch( i_bitmask & 0x00f0 ) + { + case 0x0010: return V4L2_MPEG_AUDIO_L1_BITRATE_32K; + case 0x0020: return V4L2_MPEG_AUDIO_L1_BITRATE_64K; + case 0x0030: return V4L2_MPEG_AUDIO_L1_BITRATE_96K; + case 0x0040: return V4L2_MPEG_AUDIO_L1_BITRATE_128K; + case 0x0050: return V4L2_MPEG_AUDIO_L1_BITRATE_160K; + case 0x0060: return V4L2_MPEG_AUDIO_L1_BITRATE_192K; + case 0x0070: return V4L2_MPEG_AUDIO_L1_BITRATE_224K; + case 0x0080: return V4L2_MPEG_AUDIO_L1_BITRATE_256K; + case 0x0090: return V4L2_MPEG_AUDIO_L1_BITRATE_288K; + case 0x00a0: return V4L2_MPEG_AUDIO_L1_BITRATE_320K; + case 0x00b0: return V4L2_MPEG_AUDIO_L1_BITRATE_352K; + case 0x00c0: return V4L2_MPEG_AUDIO_L1_BITRATE_384K; + case 0x00d0: return V4L2_MPEG_AUDIO_L1_BITRATE_416K; + case 0x00e0: return V4L2_MPEG_AUDIO_L1_BITRATE_448K; + } + return V4L2_MPEG_AUDIO_L1_BITRATE_320K; +} + +/***************************************************************************** + * V4L2AudioL2Bitrate: calculate v4l2 audio bitrate for layer-1 audio from pvr-audio-bitmask + *****************************************************************************/ +static uint32_t V4L2AudioL2Bitrate( uint32_t i_bitmask ) +{ + switch( i_bitmask & 0x00f0 ) + { + case 0x0010: return V4L2_MPEG_AUDIO_L2_BITRATE_32K; + case 0x0020: return V4L2_MPEG_AUDIO_L2_BITRATE_48K; + case 0x0030: return V4L2_MPEG_AUDIO_L2_BITRATE_56K; + case 0x0040: return V4L2_MPEG_AUDIO_L2_BITRATE_64K; + case 0x0050: return V4L2_MPEG_AUDIO_L2_BITRATE_80K; + case 0x0060: return V4L2_MPEG_AUDIO_L2_BITRATE_96K; + case 0x0070: return V4L2_MPEG_AUDIO_L2_BITRATE_112K; + case 0x0080: return V4L2_MPEG_AUDIO_L2_BITRATE_128K; + case 0x0090: return V4L2_MPEG_AUDIO_L2_BITRATE_160K; + case 0x00a0: return V4L2_MPEG_AUDIO_L2_BITRATE_192K; + case 0x00b0: return V4L2_MPEG_AUDIO_L2_BITRATE_224K; + case 0x00c0: return V4L2_MPEG_AUDIO_L2_BITRATE_256K; + case 0x00d0: return V4L2_MPEG_AUDIO_L2_BITRATE_320K; + case 0x00e0: return V4L2_MPEG_AUDIO_L2_BITRATE_384K; + } + return V4L2_MPEG_AUDIO_L2_BITRATE_192K; +} + +/***************************************************************************** + * V4L2AudioMode: calculate v4l2 audio mode from pvr-audio-bitmask + *****************************************************************************/ +static uint32_t V4L2AudioMode( uint32_t i_bitmask ) +{ + switch( i_bitmask & 0x0300 ) + { + case 0x0100: return V4L2_MPEG_AUDIO_MODE_JOINT_STEREO; + case 0x0200: return V4L2_MPEG_AUDIO_MODE_DUAL; + case 0x0300: return V4L2_MPEG_AUDIO_MODE_MONO; + } + return V4L2_MPEG_AUDIO_MODE_STEREO; +} + +/***************************************************************************** + * ConfigureV4L2: set up codec parameters using the new v4l2 api + *****************************************************************************/ +static void ConfigureV4L2( access_t * p_access, access_sys_t * p_sys ) +{ + struct v4l2_ext_controls controls; + + controls.ctrl_class = V4L2_CTRL_CLASS_MPEG; + controls.error_idx = 0; + controls.reserved[0] = 0; + controls.reserved[1] = 0; + controls.count = 0; + controls.controls = calloc( sizeof( struct v4l2_ext_control ), MAX_V4L2_CTRLS ); + + /* Note: Ignore frame rate. Doesn't look like it can be changed. */ + + if( p_sys->i_bitrate != -1 ) + { + AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_BITRATE, p_sys->i_bitrate ); + msg_Dbg( p_access, "Setting [%u] bitrate = %u", controls.count - 1, p_sys->i_bitrate ); + } + + if( p_sys->i_bitrate_peak != -1 ) + { + AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_BITRATE_PEAK, p_sys->i_bitrate_peak ); + msg_Dbg( p_access, "Setting [%u] bitrate_peak = %u", controls.count - 1, p_sys->i_bitrate_peak ); + } + + if( p_sys->i_bitrate_mode != -1 ) + { + AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_BITRATE_MODE, p_sys->i_bitrate_mode ); + msg_Dbg( p_access, "Setting [%u] bitrate_mode = %u", controls.count - 1, p_sys->i_bitrate_mode ); + } + + if( p_sys->i_audio_bitmask != -1 ) + { + /* Sample rate */ + AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ, V4L2SampleRate( p_sys->i_audio_bitmask ) ); + + /* Encoding layer and bitrate */ + switch( V4L2AudioEncoding( p_sys->i_audio_bitmask ) ) + { + case V4L2_MPEG_AUDIO_ENCODING_LAYER_1: + AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ, V4L2_MPEG_AUDIO_ENCODING_LAYER_1 ); + AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_AUDIO_L1_BITRATE, V4L2AudioL1Bitrate( p_sys->i_audio_bitmask ) ); + break; + + case V4L2_MPEG_AUDIO_ENCODING_LAYER_2: + AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_AUDIO_SAMPLING_FREQ, V4L2_MPEG_AUDIO_ENCODING_LAYER_2 ); + AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_AUDIO_L2_BITRATE, V4L2AudioL2Bitrate( p_sys->i_audio_bitmask ) ); + break; + } + + /* Audio mode - stereo or mono */ + AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_AUDIO_MODE, V4L2AudioMode( p_sys->i_audio_bitmask ) ); + + /* See if the user wants any other audio feature */ + if( ( p_sys->i_audio_bitmask & 0x1ff00 ) != 0 ) + { + /* It would be possible to support the bits that represent: + * V4L2_CID_MPEG_AUDIO_MODE_EXTENSION + * V4L2_CID_MPEG_AUDIO_EMPHASIS + * V4L2_CID_MPEG_AUDIO_CRC + * but they are not currently used. Tell the user. + */ + msg_Err( p_access, "There were bits in pvr-audio-bitmask that were not used."); + } + + msg_Dbg( p_access, "Setting audio controls"); + } + + if( p_sys->i_keyint != -1 ) + { + AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_GOP_SIZE, p_sys->i_keyint ); + msg_Dbg( p_access, "Setting [%u] keyint = %u", controls.count - 1, p_sys->i_keyint ); + } + + if( p_sys->i_bframes != -1 ) + { + AddV4L2Ctrl( p_access, &controls, V4L2_CID_MPEG_VIDEO_B_FRAMES, p_sys->i_bframes ); + msg_Dbg( p_access, "Setting [%u] bframes = %u", controls.count - 1, p_sys->i_bframes ); + } + + if( ioctl( p_sys->i_fd, VIDIOC_S_EXT_CTRLS, &controls ) < 0 ) + { + msg_Dbg( p_access, "VIDIOC_S_EXT_CTRLS failed on control number [%u]", controls.error_idx ); + msg_Warn( p_access, "Failed to write new capture card settings."); + } +} + +#endif /* HAVE_NEW_LINUX_VIDEODEV2_H */ + +/***************************************************************************** * Open: open the device *****************************************************************************/ static int Open( vlc_object_t * p_this ) @@ -215,6 +499,7 @@ access_sys_t * p_sys; char * psz_tofree, * psz_parser, * psz_device, * psz_radio_device; vlc_value_t val; + struct v4l2_capability device_capability; //psz_device = calloc( strlen( "/dev/videox" ) + 1, 1 ); @@ -485,23 +770,48 @@ /* open the device */ if( ( p_sys->i_fd = open( psz_device, O_RDWR ) ) < 0 ) { - msg_Err( p_access, "cannot open device (%s)", strerror( errno ) ); + msg_Err( p_access, "Cannot open device (%s).", strerror( errno ) ); free( p_sys ); return VLC_EGENERIC; } else { - msg_Dbg( p_access, "using video device: %s",psz_device); + msg_Dbg( p_access, "Using video device: %s.",psz_device); } free( psz_device ); + /* See what version of ivtvdriver is running */ + if( ioctl( p_sys->i_fd, VIDIOC_QUERYCAP, &device_capability ) < 0 ) + { + msg_Err( p_access, "Failed to work out which ivtv driver is in use." ); + free( p_sys ); + return VLC_EGENERIC; + } + else + { + msg_Dbg( p_access, "ivtv driver version %02x.%02x.%02x", + ( device_capability.version >> 16 ) & 0xff, + ( device_capability.version >> 8 ) & 0xff, + ( device_capability.version ) & 0xff); + if ( device_capability.version >= 0x000800 ) + { + /* Drivers > 0.8.0 use v4l2 API instead of IVTV ioctls */ + msg_Dbg( p_access, "this driver uses the v4l2 API" ); + p_sys->b_v4l2_api = VLC_TRUE; + } + else + { + p_sys->b_v4l2_api = VLC_FALSE; + } + } + /* set the input */ if ( p_sys->i_input != -1 ) { if ( ioctl( p_sys->i_fd, VIDIOC_S_INPUT, &p_sys->i_input ) < 0 ) { - msg_Warn( p_access, "VIDIOC_S_INPUT failed" ); + msg_Warn( p_access, "Failed to select the requested input pin." ); } else { @@ -514,7 +824,7 @@ { if ( ioctl( p_sys->i_fd, VIDIOC_S_STD, &p_sys->i_standard ) < 0 ) { - msg_Warn( p_access, "VIDIOC_S_STD failed" ); + msg_Warn( p_access, "Failed to set the requested video standard." ); } else { @@ -530,7 +840,7 @@ vfmt.type = V4L2_BUF_TYPE_VIDEO_CAPTURE; if ( ioctl( p_sys->i_fd, VIDIOC_G_FMT, &vfmt ) < 0 ) { - msg_Warn( p_access, "VIDIOC_G_FMT failed" ); + msg_Warn( p_access, "Failed to read the current picture size." ); } else { @@ -546,7 +856,7 @@ if ( ioctl( p_sys->i_fd, VIDIOC_S_FMT, &vfmt ) < 0 ) { - msg_Warn( p_access, "VIDIOC_S_FMT failed" ); + msg_Warn( p_access, "Failed to set the requested picture size." ); } else { @@ -569,7 +879,7 @@ { if( ( p_sys->i_radio_fd = open( psz_radio_device, O_RDWR ) ) < 0 ) { - msg_Err( p_access, "cannot open radio device (%s)", + msg_Err( p_access, "Cannot open radio device (%s).", strerror( errno ) ); close( p_sys->i_fd ); free( p_sys ); @@ -589,7 +899,7 @@ if ( ioctl( i_fd, VIDIOC_G_TUNER, &vt ) < 0 ) { - msg_Warn( p_access, "VIDIOC_G_TUNER failed (%s)", + msg_Warn( p_access, "Failed to read tuner information (%s).", strerror( errno ) ); } else @@ -599,7 +909,7 @@ if ( ioctl( i_fd, VIDIOC_G_FREQUENCY, &vf ) < 0 ) { - msg_Warn( p_access, "VIDIOC_G_FREQUENCY failed (%s)", + msg_Warn( p_access, "Failed to read tuner frequency (%s).", strerror( errno ) ); } else @@ -611,7 +921,7 @@ if( ioctl( i_fd, VIDIOC_S_FREQUENCY, &vf ) < 0 ) { - msg_Warn( p_access, "VIDIOC_S_FREQUENCY failed (%s)", + msg_Warn( p_access, "Failed to set tuner frequency (%s).", strerror( errno ) ); } else @@ -633,7 +943,7 @@ if ( ioctl( p_sys->i_fd, VIDIOC_S_CTRL, &ctrl ) < 0 ) { - msg_Warn( p_access, "VIDIOC_S_CTRL failed" ); + msg_Warn( p_access, "Failed to set the volume." ); } } @@ -646,70 +956,17 @@ || p_sys->i_bitrate != -1 || p_sys->i_audio_bitmask != -1 ) { - struct ivtv_ioctl_codec codec; - - if ( ioctl( p_sys->i_fd, IVTV_IOC_G_CODEC, &codec ) < 0 ) + if( p_sys->b_v4l2_api ) { - msg_Warn( p_access, "IVTV_IOC_G_CODEC failed" ); +#if HAVE_NEW_LINUX_VIDEODEV2_H + ConfigureV4L2( p_access, p_sys ); +#else + msg_Warn( p_access, "You have new ivtvdrivers, but this vlc was built against an old v4l2 header file."); +#endif } else { - if ( p_sys->i_framerate != -1 ) - { - switch ( p_sys->i_framerate ) - { - case 30: - codec.framerate = 0; - break; - - case 25: - codec.framerate = 1; - break; - - default: - msg_Warn( p_access, "invalid framerate, reverting to 25" ); - codec.framerate = 1; - break; - } - } - - if ( p_sys->i_bitrate != -1 ) - { - codec.bitrate = p_sys->i_bitrate; - } - - if ( p_sys->i_bitrate_peak != -1 ) - { - codec.bitrate_peak = p_sys->i_bitrate_peak; - } - - if ( p_sys->i_bitrate_mode != -1 ) - { - codec.bitrate_mode = p_sys->i_bitrate_mode; - } - - if ( p_sys->i_audio_bitmask != -1 ) - { - codec.audio_bitmask = p_sys->i_audio_bitmask; - } - if ( p_sys->i_keyint != -1 ) - { - codec.framespergop = p_sys->i_keyint; - } - - if ( p_sys->i_bframes != -1 ) - { - codec.bframes = p_sys->i_bframes; - } - if( ioctl( p_sys->i_fd, IVTV_IOC_S_CODEC, &codec ) < 0 ) - { - msg_Warn( p_access, "IVTV_IOC_S_CODEC failed" ); - } - else - { - msg_Dbg( p_access, "Setting codec parameters to: framerate: %d, bitrate: %d/%d/%d", - codec.framerate, codec.bitrate, codec.bitrate_peak, codec.bitrate_mode ); - } + ConfigureIVTV( p_access, p_sys ); } } @@ -723,7 +980,7 @@ } else { - msg_Warn(p_input, "Could not read byte from device"); + msg_Warn(p_input, "Could not read byte from device."); } } #endif @@ -772,7 +1029,7 @@ if( i_ret < 0 ) { - msg_Err( p_access, "select error (%s)", strerror( errno ) ); + msg_Err( p_access, "Select error (%s).", strerror( errno ) ); return -1; } @@ -838,7 +1095,7 @@ return VLC_EGENERIC; default: - msg_Warn( p_access, "unimplemented query in control" ); + msg_Warn( p_access, "Unimplemented query in control." ); return VLC_EGENERIC; }