1 /*****************************************************************************
2 * v4l.c : Video4Linux input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002-2004 VideoLAN
7 * Author: Laurent Aimar <fenrir@via.ecp.fr>
8 * Paul Forgey <paulf at aphrodite dot com>
9 * Gildas Bazin <gbazin@videolan.org>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
24 *****************************************************************************/
26 /*****************************************************************************
28 *****************************************************************************/
34 #include <vlc/input.h>
38 #include <sys/types.h>
40 #include <sys/ioctl.h>
46 /* From GStreamer's v4l plugin:
47 * Because of some really cool feature in video4linux1, also known as
48 * 'not including sys/types.h and sys/time.h', we had to include it
49 * ourselves. In all their intelligence, these people decided to fix
50 * this in the next version (video4linux2) in such a cool way that it
51 * breaks all compilations of old stuff...
52 * The real problem is actually that linux/time.h doesn't use proper
53 * macro checks before defining types like struct timeval. The proper
54 * fix here is to either fuck the kernel header (which is what we do
55 * by defining _LINUX_TIME_H, an innocent little hack) or by fixing it
56 * upstream, which I'll consider doing later on. If you get compiler
57 * errors here, check your linux/time.h && sys/time.h header setup.
61 #include <linux/videodev.h>
62 #include "videodev_mjpeg.h"
64 #include <sys/soundcard.h>
66 /*****************************************************************************
68 *****************************************************************************/
69 static int Open ( vlc_object_t * );
70 static void Close( vlc_object_t * );
72 #define CACHING_TEXT N_("Caching value in ms")
73 #define CACHING_LONGTEXT N_( \
74 "Allows you to modify the default caching value for v4l streams. This " \
75 "value should be set in millisecond units." )
76 #define VDEV_TEXT N_("Video device name")
77 #define VDEV_LONGTEXT N_( \
78 "Specify the name of the video device that will be used. " \
79 "If you don't specify anything, no video device will be used.")
80 #define ADEV_TEXT N_("Audio device name")
81 #define ADEV_LONGTEXT N_( \
82 "Specify the name of the audio device that will be used. " \
83 "If you don't specify anything, no audio device will be used.")
84 #define CHROMA_TEXT N_("Video input chroma format")
85 #define CHROMA_LONGTEXT N_( \
86 "Force the Video4Linux video device to use a specific chroma format " \
87 "(eg. I420 (default), RV24, etc.)")
90 set_shortname( _("Video4Linux") );
91 set_description( _("Video4Linux input") );
92 set_category( CAT_INPUT );
93 set_subcategory( SUBCAT_INPUT_ACCESS );
95 add_integer( "v4l-caching", DEFAULT_PTS_DELAY / 1000, NULL,
96 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
97 add_string( "v4l-vdev", "/dev/video", 0, VDEV_TEXT, VDEV_LONGTEXT,
99 add_string( "v4l-adev", "/dev/dsp", 0, ADEV_TEXT, ADEV_LONGTEXT,
101 add_string( "v4l-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
104 add_shortcut( "v4l" );
105 set_capability( "access_demux", 10 );
106 set_callbacks( Open, Close );
109 /*****************************************************************************
110 * Access: local prototypes
111 *****************************************************************************/
112 static int Demux ( demux_t * );
113 static int Control( demux_t *, int, va_list );
115 static void ParseMRL ( demux_t * );
116 static int OpenVideoDev( demux_t *, char * );
117 static int OpenAudioDev( demux_t *, char * );
119 static block_t *GrabAudio( demux_t * );
120 static block_t *GrabVideo( demux_t * );
122 #define MJPEG_BUFFER_SIZE (256*1024)
124 struct quicktime_mjpeg_app1
126 uint32_t i_reserved; /* set to 0 */
127 uint32_t i_tag; /* 'mjpg' */
128 uint32_t i_field_size; /* offset following EOI */
129 uint32_t i_padded_field_size; /* offset following EOI+pad */
130 uint32_t i_next_field; /* offset to next field */
131 uint32_t i_DQT_offset;
132 uint32_t i_DHT_offset;
133 uint32_t i_SOF_offset;
134 uint32_t i_SOS_offset;
135 uint32_t i_data_offset; /* following SOS marker data */
143 } v4lchroma_to_fourcc[] =
145 { VIDEO_PALETTE_GREY, VLC_FOURCC( 'G', 'R', 'E', 'Y' ) },
146 { VIDEO_PALETTE_HI240, VLC_FOURCC( 'I', '2', '4', '0' ) },
147 { VIDEO_PALETTE_RGB565, VLC_FOURCC( 'R', 'V', '1', '6' ) },
148 { VIDEO_PALETTE_RGB555, VLC_FOURCC( 'R', 'V', '1', '5' ) },
149 { VIDEO_PALETTE_RGB24, VLC_FOURCC( 'R', 'V', '2', '4' ) },
150 { VIDEO_PALETTE_RGB32, VLC_FOURCC( 'R', 'V', '3', '2' ) },
151 { VIDEO_PALETTE_YUV422, VLC_FOURCC( 'I', '4', '2', '2' ) },
152 { VIDEO_PALETTE_YUYV, VLC_FOURCC( 'Y', 'U', 'Y', 'V' ) },
153 { VIDEO_PALETTE_UYVY, VLC_FOURCC( 'U', 'Y', 'V', 'Y' ) },
154 { VIDEO_PALETTE_YUV420, VLC_FOURCC( 'I', '4', '2', 'N' ) },
155 { VIDEO_PALETTE_YUV411, VLC_FOURCC( 'I', '4', '1', 'N' ) },
156 { VIDEO_PALETTE_RAW, VLC_FOURCC( 'G', 'R', 'A', 'W' ) },
157 { VIDEO_PALETTE_YUV422P, VLC_FOURCC( 'I', '4', '2', '2' ) },
158 { VIDEO_PALETTE_YUV420P, VLC_FOURCC( 'I', '4', '2', '0' ) },
159 { VIDEO_PALETTE_YUV411P, VLC_FOURCC( 'I', '4', '1', '1' ) },
166 char *psz_device; /* Main device from MRL, can be video or audio */
174 /* Video properties */
191 float f_fps; /* <= 0.0 mean to grab at full rate */
192 mtime_t i_video_pts; /* only used when f_fps > 0 */
198 struct video_capability vid_cap;
199 struct video_mbuf vid_mbuf;
200 struct mjpeg_requestbuffers mjpeg_buffers;
202 uint8_t *p_video_mmap;
205 struct video_mmap vid_mmap;
206 struct video_picture vid_picture;
208 int i_video_frame_size;
209 es_out_id_t *p_es_video;
211 /* Audio properties */
212 vlc_fourcc_t i_acodec_raw;
215 int i_audio_max_frame_size;
216 block_t *p_block_audio;
217 es_out_id_t *p_es_audio;
220 /*****************************************************************************
221 * Open: opens v4l device
222 *****************************************************************************
224 * url: <video device>::::
226 *****************************************************************************/
227 static int Open( vlc_object_t *p_this )
229 demux_t *p_demux = (demux_t*)p_this;
232 /* Only when selected */
233 if( *p_demux->psz_access == '\0' )
237 p_demux->pf_demux = Demux;
238 p_demux->pf_control = Control;
239 p_demux->info.i_update = 0;
240 p_demux->info.i_title = 0;
241 p_demux->info.i_seekpoint = 0;
242 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
243 memset( p_sys, 0, sizeof( demux_sys_t ) );
246 p_sys->i_norm = VIDEO_MODE_AUTO; // auto
248 p_sys->i_frequency = -1;
251 p_sys->i_video_pts = -1;
253 p_sys->i_brightness = -1;
255 p_sys->i_colour = -1;
256 p_sys->i_contrast = -1;
258 p_sys->b_mjpeg = VLC_FALSE;
259 p_sys->i_decimation = 1;
260 p_sys->i_quality = 100;
262 p_sys->i_sample_rate = 44100;
263 p_sys->b_stereo = VLC_TRUE;
265 p_sys->psz_device = p_sys->psz_vdev = p_sys->psz_adev = NULL;
266 p_sys->fd_video = -1;
267 p_sys->fd_audio = -1;
269 p_sys->p_es_video = p_sys->p_es_audio = 0;
270 p_sys->p_block_audio = 0;
274 /* Find main device (video or audio) */
275 if( p_sys->psz_device && *p_sys->psz_device )
277 msg_Dbg( p_demux, "main device=`%s'", p_sys->psz_device );
279 /* Try to open as video device */
280 p_sys->fd_video = OpenVideoDev( p_demux, p_sys->psz_device );
282 if( p_sys->fd_video < 0 )
284 /* Try to open as audio device */
285 p_sys->fd_audio = OpenAudioDev( p_demux, p_sys->psz_device );
286 if( p_sys->fd_audio >= 0 )
288 if( p_sys->psz_adev ) free( p_sys->psz_adev );
289 p_sys->psz_adev = p_sys->psz_device;
290 p_sys->psz_device = NULL;
295 if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
296 p_sys->psz_vdev = p_sys->psz_device;
297 p_sys->psz_device = NULL;
301 /* If no device opened, only continue if the access was forced */
302 if( p_sys->fd_video < 0 && p_sys->fd_audio < 0 )
304 if( strcmp( p_demux->psz_access, "v4l" ) )
311 /* Find video device */
312 if( p_sys->fd_video < 0 )
314 if( !p_sys->psz_vdev || !*p_sys->psz_vdev )
316 if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
317 p_sys->psz_vdev = var_CreateGetString( p_demux, "v4l-vdev" );;
320 if( p_sys->psz_vdev && *p_sys->psz_vdev )
322 p_sys->fd_video = OpenVideoDev( p_demux, p_sys->psz_vdev );
326 /* Find audio device */
327 if( p_sys->fd_audio < 0 )
329 if( !p_sys->psz_adev || !*p_sys->psz_adev )
331 if( p_sys->psz_adev ) free( p_sys->psz_adev );
332 p_sys->psz_adev = var_CreateGetString( p_demux, "v4l-adev" );;
335 if( p_sys->psz_adev && *p_sys->psz_adev )
337 p_sys->fd_audio = OpenAudioDev( p_demux, p_sys->psz_adev );
341 if( p_sys->fd_video < 0 && p_sys->fd_audio < 0 )
347 msg_Dbg( p_demux, "v4l grabbing started" );
349 /* Declare elementary streams */
350 if( p_sys->fd_video >= 0 )
353 es_format_Init( &fmt, VIDEO_ES, p_sys->i_fourcc );
354 fmt.video.i_width = p_sys->i_width;
355 fmt.video.i_height = p_sys->i_height;
356 fmt.video.i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
358 /* Setup rgb mask for RGB formats */
359 if( p_sys->i_fourcc == VLC_FOURCC('R','V','2','4') )
361 /* This is in BGR format */
362 fmt.video.i_bmask = 0x00ff0000;
363 fmt.video.i_gmask = 0x0000ff00;
364 fmt.video.i_rmask = 0x000000ff;
367 msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
368 (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
369 p_sys->p_es_video = es_out_Add( p_demux->out, &fmt );
372 if( p_sys->fd_audio >= 0 )
375 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC('a','r','a','w') );
377 fmt.audio.i_channels = p_sys->b_stereo ? 2 : 1;
378 fmt.audio.i_rate = p_sys->i_sample_rate;
379 fmt.audio.i_bitspersample = 16; // FIXME ?
380 fmt.audio.i_blockalign = fmt.audio.i_channels *
381 fmt.audio.i_bitspersample / 8;
382 fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate *
383 fmt.audio.i_bitspersample;
385 msg_Dbg( p_demux, "new audio es %d channels %dHz",
386 fmt.audio.i_channels, fmt.audio.i_rate );
388 p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt );
391 /* Update default_pts to a suitable value for access */
392 var_Create( p_demux, "v4l-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
397 /*****************************************************************************
398 * Close: close device, free resources
399 *****************************************************************************/
400 static void Close( vlc_object_t *p_this )
402 demux_t *p_demux = (demux_t *)p_this;
403 demux_sys_t *p_sys = p_demux->p_sys;
405 if( p_sys->psz_device ) free( p_sys->psz_device );
406 if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
407 if( p_sys->psz_adev ) free( p_sys->psz_adev );
408 if( p_sys->fd_video >= 0 ) close( p_sys->fd_video );
409 if( p_sys->fd_audio >= 0 ) close( p_sys->fd_audio );
410 if( p_sys->p_block_audio ) block_Release( p_sys->p_block_audio );
415 ioctl( p_sys->fd_video, MJPIOC_QBUF_CAPT, &i_noframe );
418 if( p_sys->p_video_mmap && p_sys->p_video_mmap != MAP_FAILED )
421 munmap( p_sys->p_video_mmap, p_sys->mjpeg_buffers.size *
422 p_sys->mjpeg_buffers.count );
424 munmap( p_sys->p_video_mmap, p_sys->vid_mbuf.size );
430 /*****************************************************************************
432 *****************************************************************************/
433 static int Control( demux_t *p_demux, int i_query, va_list args )
440 /* Special for access_demux */
441 case DEMUX_CAN_PAUSE:
442 case DEMUX_SET_PAUSE_STATE:
443 case DEMUX_CAN_CONTROL_PACE:
444 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
448 case DEMUX_GET_PTS_DELAY:
449 pi64 = (int64_t*)va_arg( args, int64_t * );
450 *pi64 = (int64_t)var_GetInteger( p_demux, "v4l-caching" ) * 1000;
454 pi64 = (int64_t*)va_arg( args, int64_t * );
458 /* TODO implement others */
466 /*****************************************************************************
468 *****************************************************************************/
469 static int Demux( demux_t *p_demux )
471 demux_sys_t *p_sys = p_demux->p_sys;
472 es_out_id_t *p_es = p_sys->p_es_audio;
473 block_t *p_block = NULL;
475 /* Try grabbing audio frames first */
476 if( p_sys->fd_audio < 0 || !( p_block = GrabAudio( p_demux ) ) )
478 /* Try grabbing video frame */
479 p_es = p_sys->p_es_video;
480 if( p_sys->fd_video > 0 ) p_block = GrabVideo( p_demux );
485 /* Sleep so we do not consume all the cpu, 10ms seems
486 * like a good value (100fps) */
491 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
492 es_out_Send( p_demux->out, p_es, p_block );
497 /*****************************************************************************
498 * ParseMRL: parse the options contained in the MRL
499 *****************************************************************************/
500 static void ParseMRL( demux_t *p_demux )
502 demux_sys_t *p_sys = p_demux->p_sys;
504 char *psz_dup = strdup( p_demux->psz_path );
505 char *psz_parser = psz_dup;
507 while( *psz_parser && *psz_parser != ':' )
512 if( *psz_parser == ':' )
517 *psz_parser++ = '\0';
518 if( !strncmp( psz_parser, "channel=", strlen( "channel=" ) ) )
520 p_sys->i_channel = strtol( psz_parser + strlen( "channel=" ),
523 else if( !strncmp( psz_parser, "norm=", strlen( "norm=" ) ) )
525 psz_parser += strlen( "norm=" );
526 if( !strncmp( psz_parser, "pal", strlen( "pal" ) ) )
528 p_sys->i_norm = VIDEO_MODE_PAL;
529 psz_parser += strlen( "pal" );
531 else if( !strncmp( psz_parser, "ntsc", strlen( "ntsc" ) ) )
533 p_sys->i_norm = VIDEO_MODE_NTSC;
534 psz_parser += strlen( "ntsc" );
536 else if( !strncmp( psz_parser, "secam", strlen( "secam" ) ) )
538 p_sys->i_norm = VIDEO_MODE_SECAM;
539 psz_parser += strlen( "secam" );
541 else if( !strncmp( psz_parser, "auto", strlen( "auto" ) ) )
543 p_sys->i_norm = VIDEO_MODE_AUTO;
544 psz_parser += strlen( "auto" );
548 p_sys->i_norm = strtol( psz_parser, &psz_parser, 0 );
551 else if( !strncmp( psz_parser, "frequency=",
552 strlen( "frequency=" ) ) )
555 strtol( psz_parser + strlen( "frequency=" ),
557 if( p_sys->i_frequency < 30000 )
559 msg_Warn( p_demux, "v4l syntax has changed : "
560 "'frequency' is now channel frequency in kHz");
563 else if( !strncmp( psz_parser, "audio=", strlen( "audio=" ) ) )
565 p_sys->i_audio = strtol( psz_parser + strlen( "audio=" ),
568 else if( !strncmp( psz_parser, "size=", strlen( "size=" ) ) )
570 psz_parser += strlen( "size=" );
571 if( !strncmp( psz_parser, "subqcif", strlen( "subqcif" ) ) )
573 p_sys->i_width = 128;
574 p_sys->i_height = 96;
576 else if( !strncmp( psz_parser, "qsif", strlen( "qsif" ) ) )
578 p_sys->i_width = 160;
579 p_sys->i_height = 120;
581 else if( !strncmp( psz_parser, "qcif", strlen( "qcif" ) ) )
583 p_sys->i_width = 176;
584 p_sys->i_height = 144;
586 else if( !strncmp( psz_parser, "sif", strlen( "sif" ) ) )
588 p_sys->i_width = 320;
589 p_sys->i_height = 244;
591 else if( !strncmp( psz_parser, "cif", strlen( "cif" ) ) )
593 p_sys->i_width = 352;
594 p_sys->i_height = 288;
596 else if( !strncmp( psz_parser, "vga", strlen( "vga" ) ) )
598 p_sys->i_width = 640;
599 p_sys->i_height = 480;
604 p_sys->i_width = strtol( psz_parser, &psz_parser, 0 );
605 if( *psz_parser == 'x' || *psz_parser == 'X')
607 p_sys->i_height = strtol( psz_parser + 1,
610 msg_Dbg( p_demux, "WxH %dx%d", p_sys->i_width,
614 else if( !strncmp( psz_parser, "brightness=", strlen( "brightness=" ) ) )
616 p_sys->i_brightness = strtol( psz_parser + strlen( "brightness=" ),
619 else if( !strncmp( psz_parser, "colour=", strlen( "colour=" ) ) )
621 p_sys->i_colour = strtol( psz_parser + strlen( "colour=" ),
624 else if( !strncmp( psz_parser, "hue=", strlen( "hue=" ) ) )
626 p_sys->i_hue = strtol( psz_parser + strlen( "hue=" ),
629 else if( !strncmp( psz_parser, "contrast=", strlen( "contrast=" ) ) )
631 p_sys->i_contrast = strtol( psz_parser + strlen( "contrast=" ),
634 else if( !strncmp( psz_parser, "tuner=", strlen( "tuner=" ) ) )
636 p_sys->i_tuner = strtol( psz_parser + strlen( "tuner=" ),
639 else if( !strncmp( psz_parser, "adev=", strlen( "adev=" ) ) )
643 psz_parser += strlen( "adev=" );
644 if( strchr( psz_parser, ':' ) )
646 i_len = strchr( psz_parser, ':' ) - psz_parser;
650 i_len = strlen( psz_parser );
653 p_sys->psz_adev = strndup( psz_parser, i_len );
657 else if( !strncmp( psz_parser, "samplerate=",
658 strlen( "samplerate=" ) ) )
660 p_sys->i_sample_rate =
661 strtol( psz_parser + strlen( "samplerate=" ),
664 else if( !strncmp( psz_parser, "stereo", strlen( "stereo" ) ) )
666 psz_parser += strlen( "stereo" );
668 p_sys->b_stereo = VLC_TRUE;
670 else if( !strncmp( psz_parser, "mono", strlen( "mono" ) ) )
672 psz_parser += strlen( "mono" );
674 p_sys->b_stereo = VLC_FALSE;
676 else if( !strncmp( psz_parser, "mjpeg", strlen( "mjpeg" ) ) )
678 psz_parser += strlen( "mjpeg" );
680 p_sys->b_mjpeg = VLC_TRUE;
682 else if( !strncmp( psz_parser, "decimation=",
683 strlen( "decimation=" ) ) )
685 p_sys->i_decimation =
686 strtol( psz_parser + strlen( "decimation=" ),
689 else if( !strncmp( psz_parser, "quality=",
690 strlen( "quality=" ) ) )
693 strtol( psz_parser + strlen( "quality=" ),
696 else if( !strncmp( psz_parser, "fps=", strlen( "fps=" ) ) )
698 p_sys->f_fps = strtof( psz_parser + strlen( "fps=" ),
703 msg_Warn( p_demux, "unknown option" );
706 while( *psz_parser && *psz_parser != ':' )
711 if( *psz_parser == '\0' )
720 p_sys->psz_device = strdup( psz_dup );
722 if( psz_dup ) free( psz_dup );
725 /*****************************************************************************
727 *****************************************************************************/
728 static int OpenVideoDev( demux_t *p_demux, char *psz_device )
730 demux_sys_t *p_sys = p_demux->p_sys;
733 struct video_channel vid_channel;
734 struct mjpeg_params mjpeg;
737 if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
739 msg_Err( p_demux, "cannot open device (%s)", strerror( errno ) );
743 if( ioctl( i_fd, VIDIOCGCAP, &p_sys->vid_cap ) < 0 )
745 msg_Err( p_demux, "cannot get capabilities (%s)", strerror( errno ) );
750 "V4L device %s %d channels %d audios %d < w < %d %d < h < %d",
752 p_sys->vid_cap.channels,
753 p_sys->vid_cap.audios,
754 p_sys->vid_cap.minwidth, p_sys->vid_cap.maxwidth,
755 p_sys->vid_cap.minheight, p_sys->vid_cap.maxheight );
757 if( p_sys->i_channel < 0 || p_sys->i_channel >= p_sys->vid_cap.channels )
759 msg_Dbg( p_demux, "invalid channel, falling back on channel 0" );
760 p_sys->i_channel = 0;
762 if( p_sys->i_audio >= p_sys->vid_cap.audios )
764 msg_Dbg( p_demux, "invalid audio, falling back with no audio" );
768 if( p_sys->i_width < p_sys->vid_cap.minwidth ||
769 p_sys->i_width > p_sys->vid_cap.maxwidth )
771 msg_Dbg( p_demux, "invalid width %i", p_sys->i_width );
774 if( p_sys->i_height < p_sys->vid_cap.minheight ||
775 p_sys->i_height > p_sys->vid_cap.maxheight )
777 msg_Dbg( p_demux, "invalid height %i", p_sys->i_height );
781 if( !( p_sys->vid_cap.type & VID_TYPE_CAPTURE ) )
783 msg_Err( p_demux, "cannot grab" );
787 vid_channel.channel = p_sys->i_channel;
788 if( ioctl( i_fd, VIDIOCGCHAN, &vid_channel ) < 0 )
790 msg_Err( p_demux, "cannot get channel infos (%s)",
795 "setting channel %s(%d) %d tuners flags=0x%x type=0x%x norm=0x%x",
796 vid_channel.name, vid_channel.channel, vid_channel.tuners,
797 vid_channel.flags, vid_channel.type, vid_channel.norm );
799 if( p_sys->i_tuner >= vid_channel.tuners )
801 msg_Dbg( p_demux, "invalid tuner, falling back on tuner 0" );
805 vid_channel.norm = p_sys->i_norm;
806 if( ioctl( i_fd, VIDIOCSCHAN, &vid_channel ) < 0 )
808 msg_Err( p_demux, "cannot set channel (%s)", strerror( errno ) );
812 if( vid_channel.flags & VIDEO_VC_TUNER )
817 struct video_tuner vid_tuner;
818 if( p_sys->i_tuner >= 0 )
820 vid_tuner.tuner = p_sys->i_tuner;
821 if( ioctl( i_fd, VIDIOCGTUNER, &vid_tuner ) < 0 )
823 msg_Err( p_demux, "cannot get tuner (%s)", strerror( errno ) );
826 msg_Dbg( p_demux, "tuner %s low=%d high=%d, flags=0x%x "
827 "mode=0x%x signal=0x%x",
828 vid_tuner.name, vid_tuner.rangelow, vid_tuner.rangehigh,
829 vid_tuner.flags, vid_tuner.mode, vid_tuner.signal );
831 msg_Dbg( p_demux, "setting tuner %s (%d)",
832 vid_tuner.name, vid_tuner.tuner );
834 /* FIXME FIXME to be checked FIXME FIXME */
835 //vid_tuner.mode = p_sys->i_norm;
836 if( ioctl( i_fd, VIDIOCSTUNER, &vid_tuner ) < 0 )
838 msg_Err( p_demux, "cannot set tuner (%s)", strerror( errno ) );
844 /* Show a warning if frequency is < than 30000.
845 * User is certainly usint old syntax. */
849 if( p_sys->i_frequency >= 0 )
851 int driver_frequency = p_sys->i_frequency * 16 /1000;
852 if( ioctl( i_fd, VIDIOCSFREQ, &driver_frequency ) < 0 )
854 msg_Err( p_demux, "cannot set frequency (%s)",
858 msg_Dbg( p_demux, "frequency %d (%d)", p_sys->i_frequency,
864 if( vid_channel.flags & VIDEO_VC_AUDIO )
866 struct video_audio vid_audio;
868 /* XXX TODO volume, balance, ... */
869 if( p_sys->i_audio >= 0 )
871 vid_audio.audio = p_sys->i_audio;
872 if( ioctl( i_fd, VIDIOCGAUDIO, &vid_audio ) < 0 )
874 msg_Err( p_demux, "cannot get audio (%s)", strerror( errno ) );
879 vid_audio.flags &= ~VIDEO_AUDIO_MUTE;
881 if( ioctl( i_fd, VIDIOCSAUDIO, &vid_audio ) < 0 )
883 msg_Err( p_demux, "cannot set audio (%s)", strerror( errno ) );
890 /* establish basic params with input and norm before feeling width
894 struct quicktime_mjpeg_app1 *p_app1;
897 if( ioctl( i_fd, MJPIOC_G_PARAMS, &mjpeg ) < 0 )
899 msg_Err( p_demux, "cannot get mjpeg params (%s)",
903 mjpeg.input = p_sys->i_channel;
904 mjpeg.norm = p_sys->i_norm;
905 mjpeg.decimation = p_sys->i_decimation;
908 mjpeg.img_width = p_sys->i_width / p_sys->i_decimation;
909 if( p_sys->i_height )
910 mjpeg.img_height = p_sys->i_height / p_sys->i_decimation;
912 /* establish Quicktime APP1 marker while we are here */
917 p_app1 = (struct quicktime_mjpeg_app1 *)mjpeg.APP_data;
918 p_app1->i_reserved = 0;
919 p_app1->i_tag = VLC_FOURCC( 'm','j','p','g' );
920 p_app1->i_field_size = 0;
921 p_app1->i_padded_field_size = 0;
922 p_app1->i_next_field = 0;
923 /* XXX WARNING XXX */
924 /* these's nothing magic about these values. We are dangerously
925 * assuming the encoder card is encoding mjpeg-a and is not throwing
926 * in marker tags we aren't expecting. It's bad enough we have to
927 * search through the jpeg output for every frame we grab just to
928 * find the first field's end marker, so we take this risk to boost
930 * This is really something the driver could do for us because this
931 * does conform to standards outside of Apple Quicktime.
934 p_app1->i_DQT_offset = hton32( i_offset );
936 p_app1->i_DHT_offset = hton32( i_offset );
938 p_app1->i_SOF_offset = hton32( i_offset );
940 p_app1->i_SOS_offset = hton32( i_offset );
942 p_app1->i_data_offset = hton32( i_offset );
944 /* SOF and SOS aren't specified by the mjpeg API because they aren't
945 * optional. They will be present in the output. */
946 mjpeg.jpeg_markers = JPEG_MARKER_DHT | JPEG_MARKER_DQT;
948 if( ioctl( i_fd, MJPIOC_S_PARAMS, &mjpeg ) < 0 )
950 msg_Err( p_demux, "cannot set mjpeg params (%s)",
955 p_sys->i_width = mjpeg.img_width * mjpeg.HorDcm;
956 p_sys->i_height = mjpeg.img_height * mjpeg.VerDcm *
957 mjpeg.field_per_buff;
960 /* fix width/height */
961 if( !p_sys->b_mjpeg && ( p_sys->i_width == 0 || p_sys->i_height == 0 ) )
963 struct video_window vid_win;
965 if( ioctl( i_fd, VIDIOCGWIN, &vid_win ) < 0 )
967 msg_Err( p_demux, "cannot get win (%s)", strerror( errno ) );
970 p_sys->i_width = vid_win.width;
971 p_sys->i_height = vid_win.height;
973 msg_Dbg( p_demux, "will use %dx%d", p_sys->i_width, p_sys->i_height );
976 if( !p_sys->b_mjpeg )
978 /* set hue/color/.. */
979 if( ioctl( i_fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 )
981 struct video_picture vid_picture = p_sys->vid_picture;
983 if( p_sys->i_brightness >= 0 && p_sys->i_brightness < 65536 )
985 vid_picture.brightness = p_sys->i_brightness;
987 if( p_sys->i_colour >= 0 && p_sys->i_colour < 65536 )
989 vid_picture.colour = p_sys->i_colour;
991 if( p_sys->i_hue >= 0 && p_sys->i_hue < 65536 )
993 vid_picture.hue = p_sys->i_hue;
995 if( p_sys->i_contrast >= 0 && p_sys->i_contrast < 65536 )
997 vid_picture.contrast = p_sys->i_contrast;
999 if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 )
1001 msg_Dbg( p_demux, "v4l device uses brightness: %d",
1002 vid_picture.brightness );
1003 msg_Dbg( p_demux, "v4l device uses colour: %d",
1004 vid_picture.colour );
1005 msg_Dbg( p_demux, "v4l device uses hue: %d", vid_picture.hue );
1006 msg_Dbg( p_demux, "v4l device uses contrast: %d",
1007 vid_picture.contrast );
1008 p_sys->vid_picture = vid_picture;
1012 /* Find out video format used by device */
1013 if( ioctl( i_fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 )
1015 struct video_picture vid_picture = p_sys->vid_picture;
1019 vid_picture.palette = 0;
1020 p_sys->i_fourcc = 0;
1022 psz = var_CreateGetString( p_demux, "v4l-chroma" );
1023 if( strlen( psz ) >= 4 )
1025 int i_chroma = VLC_FOURCC( psz[0], psz[1], psz[2], psz[3] );
1027 /* Find out v4l chroma code */
1028 for( i = 0; v4lchroma_to_fourcc[i].i_v4l != 0; i++ )
1030 if( v4lchroma_to_fourcc[i].i_fourcc == i_chroma )
1032 vid_picture.palette = v4lchroma_to_fourcc[i].i_v4l;
1039 if( vid_picture.palette &&
1040 !ioctl( i_fd, VIDIOCSPICT, &vid_picture ) )
1042 p_sys->vid_picture = vid_picture;
1046 /* Try to set the format to something easy to encode */
1047 vid_picture.palette = VIDEO_PALETTE_YUV420P;
1048 if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 )
1050 p_sys->vid_picture = vid_picture;
1054 vid_picture.palette = VIDEO_PALETTE_YUV422P;
1055 if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 )
1057 p_sys->vid_picture = vid_picture;
1062 /* Find out final format */
1063 for( i = 0; v4lchroma_to_fourcc[i].i_v4l != 0; i++ )
1065 if( v4lchroma_to_fourcc[i].i_v4l == p_sys->vid_picture.palette)
1067 p_sys->i_fourcc = v4lchroma_to_fourcc[i].i_fourcc;
1074 msg_Err( p_demux, "ioctl VIDIOCGPICT failed" );
1079 if( p_sys->b_mjpeg )
1083 p_sys->mjpeg_buffers.count = 8;
1084 p_sys->mjpeg_buffers.size = MJPEG_BUFFER_SIZE;
1086 if( ioctl( i_fd, MJPIOC_REQBUFS, &p_sys->mjpeg_buffers ) < 0 )
1088 msg_Err( p_demux, "mmap unsupported" );
1092 p_sys->p_video_mmap = mmap( 0,
1093 p_sys->mjpeg_buffers.size * p_sys->mjpeg_buffers.count,
1094 PROT_READ | PROT_WRITE, MAP_SHARED, i_fd, 0 );
1095 if( p_sys->p_video_mmap == MAP_FAILED )
1097 msg_Err( p_demux, "mmap failed" );
1101 p_sys->i_fourcc = VLC_FOURCC( 'm','j','p','g' );
1102 p_sys->i_frame_pos = -1;
1104 /* queue up all the frames */
1105 for( i = 0; i < (int)p_sys->mjpeg_buffers.count; i++ )
1107 if( ioctl( i_fd, MJPIOC_QBUF_CAPT, &i ) < 0 )
1109 msg_Err( p_demux, "unable to queue frame" );
1116 /* Fill in picture_t fields */
1117 vout_InitPicture( VLC_OBJECT(p_demux), &p_sys->pic, p_sys->i_fourcc,
1118 p_sys->i_width, p_sys->i_height, p_sys->i_width *
1119 VOUT_ASPECT_FACTOR / p_sys->i_height );
1120 if( !p_sys->pic.i_planes )
1122 msg_Err( p_demux, "unsupported chroma" );
1125 p_sys->i_video_frame_size = 0;
1126 for( i = 0; i < p_sys->pic.i_planes; i++ )
1128 p_sys->i_video_frame_size += p_sys->pic.p[i].i_visible_lines *
1129 p_sys->pic.p[i].i_visible_pitch;
1132 msg_Dbg( p_demux, "v4l device uses frame size: %i",
1133 p_sys->i_video_frame_size );
1134 msg_Dbg( p_demux, "v4l device uses chroma: %4.4s",
1135 (char*)&p_sys->i_fourcc );
1137 /* Allocate mmap buffer */
1138 if( ioctl( i_fd, VIDIOCGMBUF, &p_sys->vid_mbuf ) < 0 )
1140 msg_Err( p_demux, "mmap unsupported" );
1144 p_sys->p_video_mmap = mmap( 0, p_sys->vid_mbuf.size,
1145 PROT_READ|PROT_WRITE, MAP_SHARED,
1147 if( p_sys->p_video_mmap == MAP_FAILED )
1149 /* FIXME -> normal read */
1150 msg_Err( p_demux, "mmap failed" );
1155 p_sys->vid_mmap.frame = 0;
1156 p_sys->vid_mmap.width = p_sys->i_width;
1157 p_sys->vid_mmap.height = p_sys->i_height;
1158 p_sys->vid_mmap.format = p_sys->vid_picture.palette;
1159 if( ioctl( i_fd, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 )
1161 msg_Warn( p_demux, "%4.4s refused", (char*)&p_sys->i_fourcc );
1162 msg_Err( p_demux, "chroma selection failed" );
1170 if( i_fd >= 0 ) close( i_fd );
1174 /*****************************************************************************
1176 *****************************************************************************/
1177 static int OpenAudioDev( demux_t *p_demux, char *psz_device )
1179 demux_sys_t *p_sys = p_demux->p_sys;
1182 if( (i_fd = open( psz_device, O_RDONLY | O_NONBLOCK )) < 0 )
1184 msg_Err( p_demux, "cannot open audio device (%s)", strerror( errno ) );
1188 i_format = AFMT_S16_LE;
1189 if( ioctl( i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
1190 || i_format != AFMT_S16_LE )
1192 msg_Err( p_demux, "cannot set audio format (16b little endian) "
1193 "(%s)", strerror( errno ) );
1197 if( ioctl( i_fd, SNDCTL_DSP_STEREO,
1198 &p_sys->b_stereo ) < 0 )
1200 msg_Err( p_demux, "cannot set audio channels count (%s)",
1201 strerror( errno ) );
1205 if( ioctl( i_fd, SNDCTL_DSP_SPEED,
1206 &p_sys->i_sample_rate ) < 0 )
1208 msg_Err( p_demux, "cannot set audio sample rate (%s)",
1209 strerror( errno ) );
1213 msg_Dbg( p_demux, "openened adev=`%s' %s %dHz",
1214 psz_device, p_sys->b_stereo ? "stereo" : "mono",
1215 p_sys->i_sample_rate );
1217 p_sys->i_audio_max_frame_size = 6 * 1024;
1223 if( i_fd >= 0 ) close( i_fd );
1227 /*****************************************************************************
1228 * GrabAudio: grab audio
1229 *****************************************************************************/
1230 static block_t *GrabAudio( demux_t *p_demux )
1232 demux_sys_t *p_sys = p_demux->p_sys;
1233 struct audio_buf_info buf_info;
1234 int i_read, i_correct;
1237 if( p_sys->p_block_audio ) p_block = p_sys->p_block_audio;
1238 else p_block = block_New( p_demux, p_sys->i_audio_max_frame_size );
1242 msg_Warn( p_demux, "cannot get block" );
1246 p_sys->p_block_audio = p_block;
1248 i_read = read( p_sys->fd_audio, p_block->p_buffer,
1249 p_sys->i_audio_max_frame_size );
1251 if( i_read <= 0 ) return 0;
1253 p_block->i_buffer = i_read;
1254 p_sys->p_block_audio = 0;
1256 /* Correct the date because of kernel buffering */
1258 if( ioctl( p_sys->fd_audio, SNDCTL_DSP_GETISPACE, &buf_info ) == 0 )
1260 i_correct += buf_info.bytes;
1263 p_block->i_pts = p_block->i_dts =
1264 mdate() - I64C(1000000) * (mtime_t)i_correct /
1265 2 / ( p_sys->b_stereo ? 2 : 1) / p_sys->i_sample_rate;
1270 /*****************************************************************************
1272 *****************************************************************************/
1273 static uint8_t *GrabCapture( demux_t *p_demux )
1275 demux_sys_t *p_sys = p_demux->p_sys;
1276 int i_captured_frame = p_sys->i_frame_pos;
1278 p_sys->vid_mmap.frame = (p_sys->i_frame_pos + 1) % p_sys->vid_mbuf.frames;
1280 while( ioctl( p_sys->fd_video, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 )
1282 if( errno != EAGAIN )
1284 msg_Err( p_demux, "failed capturing new frame" );
1288 if( p_demux->b_die )
1293 msg_Dbg( p_demux, "grab failed, trying again" );
1296 while( ioctl(p_sys->fd_video, VIDIOCSYNC, &p_sys->i_frame_pos) < 0 )
1298 if( errno != EAGAIN && errno != EINTR )
1300 msg_Err( p_demux, "failed syncing new frame" );
1305 p_sys->i_frame_pos = p_sys->vid_mmap.frame;
1306 /* leave i_video_frame_size alone */
1307 return p_sys->p_video_mmap + p_sys->vid_mbuf.offsets[i_captured_frame];
1310 static uint8_t *GrabMJPEG( demux_t *p_demux )
1312 demux_sys_t *p_sys = p_demux->p_sys;
1313 struct mjpeg_sync sync;
1314 uint8_t *p_frame, *p_field, *p;
1317 struct quicktime_mjpeg_app1 *p_app1 = NULL;
1319 /* re-queue the last frame we sync'd */
1320 if( p_sys->i_frame_pos != -1 )
1322 while( ioctl( p_sys->fd_video, MJPIOC_QBUF_CAPT,
1323 &p_sys->i_frame_pos ) < 0 )
1325 if( errno != EAGAIN && errno != EINTR )
1327 msg_Err( p_demux, "failed capturing new frame" );
1333 /* sync on the next frame */
1334 while( ioctl( p_sys->fd_video, MJPIOC_SYNC, &sync ) < 0 )
1336 if( errno != EAGAIN && errno != EINTR )
1338 msg_Err( p_demux, "failed syncing new frame" );
1343 p_sys->i_frame_pos = sync.frame;
1344 p_frame = p_sys->p_video_mmap + p_sys->mjpeg_buffers.size * sync.frame;
1346 /* p_frame now points to the data. fix up the Quicktime APP1 marker */
1348 tag = hton16( tag );
1352 p = memmem( p_field, sync.length, &tag, 2 );
1356 p += 2; /* data immediately following EOI */
1358 p_app1 = (struct quicktime_mjpeg_app1 *)(p_field + 6);
1360 i_size = ((uint32_t)(p - p_field));
1361 i_size = hton32( i_size );
1362 memcpy( &p_app1->i_field_size, &i_size, 4 );
1364 while( *p == 0xff && *(p+1) == 0xff )
1367 i_size = ((uint32_t)(p - p_field));
1368 i_size = hton32( i_size );
1369 memcpy( &p_app1->i_padded_field_size, &i_size, 4 );
1373 tag = hton16( tag );
1374 p_field = memmem( p, sync.length - (size_t)(p - p_frame), &tag, 2 );
1378 i_size = (uint32_t)(p_field - p_frame);
1379 i_size = hton32( i_size );
1380 memcpy( &p_app1->i_next_field, &i_size, 4 );
1383 p_app1 = (struct quicktime_mjpeg_app1 *)(p_field + 6);
1385 tag = hton16( tag );
1386 p = memmem( p_field, sync.length - (size_t)(p_field - p_frame),
1391 /* sometimes the second field doesn't have the EOI. just put it
1394 p = p_frame + sync.length;
1395 memcpy( p, &tag, 2 );
1400 i_size = (uint32_t)(p - p_field);
1401 i_size = hton32( i_size );
1402 memcpy( &p_app1->i_field_size, &i_size, 4 );
1403 i_size = (uint32_t)(sync.length - (uint32_t)(p_field - p_frame));
1404 i_size = hton32( i_size );
1405 memcpy( &p_app1->i_padded_field_size, &i_size, 4 );
1408 p_sys->i_video_frame_size = sync.length;
1412 static block_t *GrabVideo( demux_t *p_demux )
1414 demux_sys_t *p_sys = p_demux->p_sys;
1418 if( p_sys->f_fps >= 0.1 && p_sys->i_video_pts > 0 )
1420 mtime_t i_dur = (mtime_t)((double)1000000 / (double)p_sys->f_fps);
1422 /* Did we wait long enough ? (frame rate reduction) */
1423 if( p_sys->i_video_pts + i_dur > mdate() ) return 0;
1426 if( p_sys->b_mjpeg ) p_frame = GrabMJPEG( p_demux );
1427 else p_frame = GrabCapture( p_demux );
1429 if( !p_frame ) return 0;
1431 if( !( p_block = block_New( p_demux, p_sys->i_video_frame_size ) ) )
1433 msg_Warn( p_demux, "cannot get block" );
1437 memcpy( p_block->p_buffer, p_frame, p_sys->i_video_frame_size );
1438 p_sys->i_video_pts = p_block->i_pts = p_block->i_dts = mdate();