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") );
93 add_integer( "v4l-caching", DEFAULT_PTS_DELAY / 1000, NULL,
94 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
95 add_string( "v4l-vdev", "/dev/video", 0, VDEV_TEXT, VDEV_LONGTEXT,
97 add_string( "v4l-adev", "/dev/dsp", 0, ADEV_TEXT, ADEV_LONGTEXT,
99 add_string( "v4l-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
102 add_shortcut( "v4l" );
103 set_capability( "access_demux", 10 );
104 set_callbacks( Open, Close );
107 /*****************************************************************************
108 * Access: local prototypes
109 *****************************************************************************/
110 static int Demux ( demux_t * );
111 static int Control( demux_t *, int, va_list );
113 static void ParseMRL ( demux_t * );
114 static int OpenVideoDev( demux_t *, char * );
115 static int OpenAudioDev( demux_t *, char * );
117 static block_t *GrabAudio( demux_t * );
118 static block_t *GrabVideo( demux_t * );
120 #define MJPEG_BUFFER_SIZE (256*1024)
122 struct quicktime_mjpeg_app1
124 uint32_t i_reserved; /* set to 0 */
125 uint32_t i_tag; /* 'mjpg' */
126 uint32_t i_field_size; /* offset following EOI */
127 uint32_t i_padded_field_size; /* offset following EOI+pad */
128 uint32_t i_next_field; /* offset to next field */
129 uint32_t i_DQT_offset;
130 uint32_t i_DHT_offset;
131 uint32_t i_SOF_offset;
132 uint32_t i_SOS_offset;
133 uint32_t i_data_offset; /* following SOS marker data */
141 } v4lchroma_to_fourcc[] =
143 { VIDEO_PALETTE_GREY, VLC_FOURCC( 'G', 'R', 'E', 'Y' ) },
144 { VIDEO_PALETTE_HI240, VLC_FOURCC( 'I', '2', '4', '0' ) },
145 { VIDEO_PALETTE_RGB565, VLC_FOURCC( 'R', 'V', '1', '6' ) },
146 { VIDEO_PALETTE_RGB555, VLC_FOURCC( 'R', 'V', '1', '5' ) },
147 { VIDEO_PALETTE_RGB24, VLC_FOURCC( 'R', 'V', '2', '4' ) },
148 { VIDEO_PALETTE_RGB32, VLC_FOURCC( 'R', 'V', '3', '2' ) },
149 { VIDEO_PALETTE_YUV422, VLC_FOURCC( 'I', '4', '2', '2' ) },
150 { VIDEO_PALETTE_YUYV, VLC_FOURCC( 'Y', 'U', 'Y', 'V' ) },
151 { VIDEO_PALETTE_UYVY, VLC_FOURCC( 'U', 'Y', 'V', 'Y' ) },
152 { VIDEO_PALETTE_YUV420, VLC_FOURCC( 'I', '4', '2', 'N' ) },
153 { VIDEO_PALETTE_YUV411, VLC_FOURCC( 'I', '4', '1', 'N' ) },
154 { VIDEO_PALETTE_RAW, VLC_FOURCC( 'G', 'R', 'A', 'W' ) },
155 { VIDEO_PALETTE_YUV422P, VLC_FOURCC( 'I', '4', '2', '2' ) },
156 { VIDEO_PALETTE_YUV420P, VLC_FOURCC( 'I', '4', '2', '0' ) },
157 { VIDEO_PALETTE_YUV411P, VLC_FOURCC( 'I', '4', '1', '1' ) },
164 char *psz_device; /* Main device from MRL, can be video or audio */
172 /* Video properties */
189 float f_fps; /* <= 0.0 mean to grab at full rate */
190 mtime_t i_video_pts; /* only used when f_fps > 0 */
196 struct video_capability vid_cap;
197 struct video_mbuf vid_mbuf;
198 struct mjpeg_requestbuffers mjpeg_buffers;
200 uint8_t *p_video_mmap;
203 struct video_mmap vid_mmap;
204 struct video_picture vid_picture;
206 int i_video_frame_size;
207 es_out_id_t *p_es_video;
209 /* Audio properties */
210 vlc_fourcc_t i_acodec_raw;
213 int i_audio_max_frame_size;
214 block_t *p_block_audio;
215 es_out_id_t *p_es_audio;
218 /*****************************************************************************
219 * Open: opens v4l device
220 *****************************************************************************
222 * url: <video device>::::
224 *****************************************************************************/
225 static int Open( vlc_object_t *p_this )
227 demux_t *p_demux = (demux_t*)p_this;
230 /* Only when selected */
231 if( *p_demux->psz_access == '\0' )
235 p_demux->pf_demux = Demux;
236 p_demux->pf_control = Control;
237 p_demux->info.i_update = 0;
238 p_demux->info.i_title = 0;
239 p_demux->info.i_seekpoint = 0;
240 p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
241 memset( p_sys, 0, sizeof( demux_sys_t ) );
244 p_sys->i_norm = VIDEO_MODE_AUTO; // auto
246 p_sys->i_frequency = -1;
249 p_sys->i_video_pts = -1;
251 p_sys->i_brightness = -1;
253 p_sys->i_colour = -1;
254 p_sys->i_contrast = -1;
256 p_sys->b_mjpeg = VLC_FALSE;
257 p_sys->i_decimation = 1;
258 p_sys->i_quality = 100;
260 p_sys->i_sample_rate = 44100;
261 p_sys->b_stereo = VLC_TRUE;
263 p_sys->psz_device = p_sys->psz_vdev = p_sys->psz_adev = NULL;
264 p_sys->fd_video = -1;
265 p_sys->fd_audio = -1;
267 p_sys->p_es_video = p_sys->p_es_audio = 0;
268 p_sys->p_block_audio = 0;
272 /* Find main device (video or audio) */
273 if( p_sys->psz_device && *p_sys->psz_device )
275 msg_Dbg( p_demux, "main device=`%s'", p_sys->psz_device );
277 /* Try to open as video device */
278 p_sys->fd_video = OpenVideoDev( p_demux, p_sys->psz_device );
280 if( p_sys->fd_video < 0 )
282 /* Try to open as audio device */
283 p_sys->fd_audio = OpenAudioDev( p_demux, p_sys->psz_device );
284 if( p_sys->fd_audio >= 0 )
286 if( p_sys->psz_adev ) free( p_sys->psz_adev );
287 p_sys->psz_adev = p_sys->psz_device;
288 p_sys->psz_device = NULL;
293 if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
294 p_sys->psz_vdev = p_sys->psz_device;
295 p_sys->psz_device = NULL;
299 /* If no device opened, only continue if the access was forced */
300 if( p_sys->fd_video < 0 && p_sys->fd_audio < 0 )
302 if( strcmp( p_demux->psz_access, "v4l" ) )
309 /* Find video device */
310 if( p_sys->fd_video < 0 )
312 if( !p_sys->psz_vdev || !*p_sys->psz_vdev )
314 if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
315 p_sys->psz_vdev = var_CreateGetString( p_demux, "v4l-vdev" );;
318 if( p_sys->psz_vdev && *p_sys->psz_vdev )
320 p_sys->fd_video = OpenVideoDev( p_demux, p_sys->psz_vdev );
324 /* Find audio device */
325 if( p_sys->fd_audio < 0 )
327 if( !p_sys->psz_adev || !*p_sys->psz_adev )
329 if( p_sys->psz_adev ) free( p_sys->psz_adev );
330 p_sys->psz_adev = var_CreateGetString( p_demux, "v4l-adev" );;
333 if( p_sys->psz_adev && *p_sys->psz_adev )
335 p_sys->fd_audio = OpenAudioDev( p_demux, p_sys->psz_adev );
339 if( p_sys->fd_video < 0 && p_sys->fd_audio < 0 )
345 msg_Dbg( p_demux, "v4l grabbing started" );
347 /* Declare elementary streams */
348 if( p_sys->fd_video >= 0 )
351 es_format_Init( &fmt, VIDEO_ES, p_sys->i_fourcc );
352 fmt.video.i_width = p_sys->i_width;
353 fmt.video.i_height = p_sys->i_height;
355 msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
356 (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
357 p_sys->p_es_video = es_out_Add( p_demux->out, &fmt );
360 if( p_sys->fd_audio >= 0 )
363 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC('a','r','a','w') );
365 fmt.audio.i_channels = p_sys->b_stereo ? 2 : 1;
366 fmt.audio.i_rate = p_sys->i_sample_rate;
367 fmt.audio.i_bitspersample = 16; // FIXME ?
368 fmt.audio.i_blockalign = fmt.audio.i_channels *
369 fmt.audio.i_bitspersample / 8;
370 fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate *
371 fmt.audio.i_bitspersample;
373 msg_Dbg( p_demux, "new audio es %d channels %dHz",
374 fmt.audio.i_channels, fmt.audio.i_rate );
376 p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt );
379 /* Update default_pts to a suitable value for access */
380 var_Create( p_demux, "v4l-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
385 /*****************************************************************************
386 * Close: close device, free resources
387 *****************************************************************************/
388 static void Close( vlc_object_t *p_this )
390 demux_t *p_demux = (demux_t *)p_this;
391 demux_sys_t *p_sys = p_demux->p_sys;
393 if( p_sys->psz_device ) free( p_sys->psz_device );
394 if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
395 if( p_sys->psz_adev ) free( p_sys->psz_adev );
396 if( p_sys->fd_video >= 0 ) close( p_sys->fd_video );
397 if( p_sys->fd_audio >= 0 ) close( p_sys->fd_audio );
398 if( p_sys->p_block_audio ) block_Release( p_sys->p_block_audio );
403 ioctl( p_sys->fd_video, MJPIOC_QBUF_CAPT, &i_noframe );
406 if( p_sys->p_video_mmap && p_sys->p_video_mmap != MAP_FAILED )
409 munmap( p_sys->p_video_mmap, p_sys->mjpeg_buffers.size *
410 p_sys->mjpeg_buffers.count );
412 munmap( p_sys->p_video_mmap, p_sys->vid_mbuf.size );
418 /*****************************************************************************
420 *****************************************************************************/
421 static int Control( demux_t *p_demux, int i_query, va_list args )
428 /* Special for access_demux */
429 case DEMUX_CAN_PAUSE:
430 case DEMUX_SET_PAUSE_STATE:
431 case DEMUX_CAN_CONTROL_PACE:
432 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
436 case DEMUX_GET_PTS_DELAY:
437 pi64 = (int64_t*)va_arg( args, int64_t * );
438 *pi64 = (int64_t)var_GetInteger( p_demux, "v4l-caching" ) * 1000;
442 pi64 = (int64_t*)va_arg( args, int64_t * );
446 /* TODO implement others */
454 /*****************************************************************************
456 *****************************************************************************/
457 static int Demux( demux_t *p_demux )
459 demux_sys_t *p_sys = p_demux->p_sys;
460 es_out_id_t *p_es = p_sys->p_es_audio;
461 block_t *p_block = NULL;
463 /* Try grabbing audio frames first */
464 if( p_sys->fd_audio < 0 || !( p_block = GrabAudio( p_demux ) ) )
466 /* Try grabbing video frame */
467 p_es = p_sys->p_es_video;
468 if( p_sys->fd_video > 0 ) p_block = GrabVideo( p_demux );
473 /* Sleep so we do not consume all the cpu, 10ms seems
474 * like a good value (100fps) */
479 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
480 es_out_Send( p_demux->out, p_es, p_block );
485 /*****************************************************************************
486 * ParseMRL: parse the options contained in the MRL
487 *****************************************************************************/
488 static void ParseMRL( demux_t *p_demux )
490 demux_sys_t *p_sys = p_demux->p_sys;
492 char *psz_dup = strdup( p_demux->psz_path );
493 char *psz_parser = psz_dup;
495 while( *psz_parser && *psz_parser != ':' )
500 if( *psz_parser == ':' )
505 *psz_parser++ = '\0';
506 if( !strncmp( psz_parser, "channel=", strlen( "channel=" ) ) )
508 p_sys->i_channel = strtol( psz_parser + strlen( "channel=" ),
511 else if( !strncmp( psz_parser, "norm=", strlen( "norm=" ) ) )
513 psz_parser += strlen( "norm=" );
514 if( !strncmp( psz_parser, "pal", strlen( "pal" ) ) )
516 p_sys->i_norm = VIDEO_MODE_PAL;
517 psz_parser += strlen( "pal" );
519 else if( !strncmp( psz_parser, "ntsc", strlen( "ntsc" ) ) )
521 p_sys->i_norm = VIDEO_MODE_NTSC;
522 psz_parser += strlen( "ntsc" );
524 else if( !strncmp( psz_parser, "secam", strlen( "secam" ) ) )
526 p_sys->i_norm = VIDEO_MODE_SECAM;
527 psz_parser += strlen( "secam" );
529 else if( !strncmp( psz_parser, "auto", strlen( "auto" ) ) )
531 p_sys->i_norm = VIDEO_MODE_AUTO;
532 psz_parser += strlen( "auto" );
536 p_sys->i_norm = strtol( psz_parser, &psz_parser, 0 );
539 else if( !strncmp( psz_parser, "frequency=",
540 strlen( "frequency=" ) ) )
543 strtol( psz_parser + strlen( "frequency=" ),
545 if( p_sys->i_frequency < 30000 )
547 msg_Warn( p_demux, "v4l syntax has changed : "
548 "'frequency' is now channel frequency in kHz");
551 else if( !strncmp( psz_parser, "audio=", strlen( "audio=" ) ) )
553 p_sys->i_audio = strtol( psz_parser + strlen( "audio=" ),
556 else if( !strncmp( psz_parser, "size=", strlen( "size=" ) ) )
558 psz_parser += strlen( "size=" );
559 if( !strncmp( psz_parser, "subqcif", strlen( "subqcif" ) ) )
561 p_sys->i_width = 128;
562 p_sys->i_height = 96;
564 else if( !strncmp( psz_parser, "qsif", strlen( "qsif" ) ) )
566 p_sys->i_width = 160;
567 p_sys->i_height = 120;
569 else if( !strncmp( psz_parser, "qcif", strlen( "qcif" ) ) )
571 p_sys->i_width = 176;
572 p_sys->i_height = 144;
574 else if( !strncmp( psz_parser, "sif", strlen( "sif" ) ) )
576 p_sys->i_width = 320;
577 p_sys->i_height = 244;
579 else if( !strncmp( psz_parser, "cif", strlen( "cif" ) ) )
581 p_sys->i_width = 352;
582 p_sys->i_height = 288;
584 else if( !strncmp( psz_parser, "vga", strlen( "vga" ) ) )
586 p_sys->i_width = 640;
587 p_sys->i_height = 480;
592 p_sys->i_width = strtol( psz_parser, &psz_parser, 0 );
593 if( *psz_parser == 'x' || *psz_parser == 'X')
595 p_sys->i_height = strtol( psz_parser + 1,
598 msg_Dbg( p_demux, "WxH %dx%d", p_sys->i_width,
602 else if( !strncmp( psz_parser, "brightness=", strlen( "brightness=" ) ) )
604 p_sys->i_brightness = strtol( psz_parser + strlen( "brightness=" ),
607 else if( !strncmp( psz_parser, "colour=", strlen( "colour=" ) ) )
609 p_sys->i_colour = strtol( psz_parser + strlen( "colour=" ),
612 else if( !strncmp( psz_parser, "hue=", strlen( "hue=" ) ) )
614 p_sys->i_hue = strtol( psz_parser + strlen( "hue=" ),
617 else if( !strncmp( psz_parser, "contrast=", strlen( "contrast=" ) ) )
619 p_sys->i_contrast = strtol( psz_parser + strlen( "contrast=" ),
622 else if( !strncmp( psz_parser, "tuner=", strlen( "tuner=" ) ) )
624 p_sys->i_tuner = strtol( psz_parser + strlen( "tuner=" ),
627 else if( !strncmp( psz_parser, "adev=", strlen( "adev=" ) ) )
631 psz_parser += strlen( "adev=" );
632 if( strchr( psz_parser, ':' ) )
634 i_len = strchr( psz_parser, ':' ) - psz_parser;
638 i_len = strlen( psz_parser );
641 p_sys->psz_adev = strndup( psz_parser, i_len );
645 else if( !strncmp( psz_parser, "samplerate=",
646 strlen( "samplerate=" ) ) )
648 p_sys->i_sample_rate =
649 strtol( psz_parser + strlen( "samplerate=" ),
652 else if( !strncmp( psz_parser, "stereo", strlen( "stereo" ) ) )
654 psz_parser += strlen( "stereo" );
656 p_sys->b_stereo = VLC_TRUE;
658 else if( !strncmp( psz_parser, "mono", strlen( "mono" ) ) )
660 psz_parser += strlen( "mono" );
662 p_sys->b_stereo = VLC_FALSE;
664 else if( !strncmp( psz_parser, "mjpeg", strlen( "mjpeg" ) ) )
666 psz_parser += strlen( "mjpeg" );
668 p_sys->b_mjpeg = VLC_TRUE;
670 else if( !strncmp( psz_parser, "decimation=",
671 strlen( "decimation=" ) ) )
673 p_sys->i_decimation =
674 strtol( psz_parser + strlen( "decimation=" ),
677 else if( !strncmp( psz_parser, "quality=",
678 strlen( "quality=" ) ) )
681 strtol( psz_parser + strlen( "quality=" ),
684 else if( !strncmp( psz_parser, "fps=", strlen( "fps=" ) ) )
686 p_sys->f_fps = strtof( psz_parser + strlen( "fps=" ),
691 msg_Warn( p_demux, "unknown option" );
694 while( *psz_parser && *psz_parser != ':' )
699 if( *psz_parser == '\0' )
708 p_sys->psz_device = strdup( psz_dup );
710 if( psz_dup ) free( psz_dup );
713 /*****************************************************************************
715 *****************************************************************************/
716 static int OpenVideoDev( demux_t *p_demux, char *psz_device )
718 demux_sys_t *p_sys = p_demux->p_sys;
721 struct video_channel vid_channel;
722 struct mjpeg_params mjpeg;
725 if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
727 msg_Err( p_demux, "cannot open device (%s)", strerror( errno ) );
731 if( ioctl( i_fd, VIDIOCGCAP, &p_sys->vid_cap ) < 0 )
733 msg_Err( p_demux, "cannot get capabilities (%s)", strerror( errno ) );
738 "V4L device %s %d channels %d audios %d < w < %d %d < h < %d",
740 p_sys->vid_cap.channels,
741 p_sys->vid_cap.audios,
742 p_sys->vid_cap.minwidth, p_sys->vid_cap.maxwidth,
743 p_sys->vid_cap.minheight, p_sys->vid_cap.maxheight );
745 if( p_sys->i_channel < 0 || p_sys->i_channel >= p_sys->vid_cap.channels )
747 msg_Dbg( p_demux, "invalid channel, falling back on channel 0" );
748 p_sys->i_channel = 0;
750 if( p_sys->i_audio >= p_sys->vid_cap.audios )
752 msg_Dbg( p_demux, "invalid audio, falling back with no audio" );
756 if( p_sys->i_width < p_sys->vid_cap.minwidth ||
757 p_sys->i_width > p_sys->vid_cap.maxwidth )
759 msg_Dbg( p_demux, "invalid width %i", p_sys->i_width );
762 if( p_sys->i_height < p_sys->vid_cap.minheight ||
763 p_sys->i_height > p_sys->vid_cap.maxheight )
765 msg_Dbg( p_demux, "invalid height %i", p_sys->i_height );
769 if( !( p_sys->vid_cap.type & VID_TYPE_CAPTURE ) )
771 msg_Err( p_demux, "cannot grab" );
775 vid_channel.channel = p_sys->i_channel;
776 if( ioctl( i_fd, VIDIOCGCHAN, &vid_channel ) < 0 )
778 msg_Err( p_demux, "cannot get channel infos (%s)",
783 "setting channel %s(%d) %d tuners flags=0x%x type=0x%x norm=0x%x",
784 vid_channel.name, vid_channel.channel, vid_channel.tuners,
785 vid_channel.flags, vid_channel.type, vid_channel.norm );
787 if( p_sys->i_tuner >= vid_channel.tuners )
789 msg_Dbg( p_demux, "invalid tuner, falling back on tuner 0" );
793 vid_channel.norm = p_sys->i_norm;
794 if( ioctl( i_fd, VIDIOCSCHAN, &vid_channel ) < 0 )
796 msg_Err( p_demux, "cannot set channel (%s)", strerror( errno ) );
800 if( vid_channel.flags & VIDEO_VC_TUNER )
805 struct video_tuner vid_tuner;
806 if( p_sys->i_tuner >= 0 )
808 vid_tuner.tuner = p_sys->i_tuner;
809 if( ioctl( i_fd, VIDIOCGTUNER, &vid_tuner ) < 0 )
811 msg_Err( p_demux, "cannot get tuner (%s)", strerror( errno ) );
814 msg_Dbg( p_demux, "tuner %s low=%d high=%d, flags=0x%x "
815 "mode=0x%x signal=0x%x",
816 vid_tuner.name, vid_tuner.rangelow, vid_tuner.rangehigh,
817 vid_tuner.flags, vid_tuner.mode, vid_tuner.signal );
819 msg_Dbg( p_demux, "setting tuner %s (%d)",
820 vid_tuner.name, vid_tuner.tuner );
822 /* FIXME FIXME to be checked FIXME FIXME */
823 //vid_tuner.mode = p_sys->i_norm;
824 if( ioctl( i_fd, VIDIOCSTUNER, &vid_tuner ) < 0 )
826 msg_Err( p_demux, "cannot set tuner (%s)", strerror( errno ) );
832 /* Show a warning if frequency is < than 30000.
833 * User is certainly usint old syntax. */
837 if( p_sys->i_frequency >= 0 )
839 int driver_frequency = p_sys->i_frequency * 16 /1000;
840 if( ioctl( i_fd, VIDIOCSFREQ, &driver_frequency ) < 0 )
842 msg_Err( p_demux, "cannot set frequency (%s)",
846 msg_Dbg( p_demux, "frequency %d (%d)", p_sys->i_frequency,
852 if( vid_channel.flags & VIDEO_VC_AUDIO )
854 struct video_audio vid_audio;
856 /* XXX TODO volume, balance, ... */
857 if( p_sys->i_audio >= 0 )
859 vid_audio.audio = p_sys->i_audio;
860 if( ioctl( i_fd, VIDIOCGAUDIO, &vid_audio ) < 0 )
862 msg_Err( p_demux, "cannot get audio (%s)", strerror( errno ) );
867 vid_audio.flags &= ~VIDEO_AUDIO_MUTE;
869 if( ioctl( i_fd, VIDIOCSAUDIO, &vid_audio ) < 0 )
871 msg_Err( p_demux, "cannot set audio (%s)", strerror( errno ) );
878 /* establish basic params with input and norm before feeling width
882 struct quicktime_mjpeg_app1 *p_app1;
885 if( ioctl( i_fd, MJPIOC_G_PARAMS, &mjpeg ) < 0 )
887 msg_Err( p_demux, "cannot get mjpeg params (%s)",
891 mjpeg.input = p_sys->i_channel;
892 mjpeg.norm = p_sys->i_norm;
893 mjpeg.decimation = p_sys->i_decimation;
896 mjpeg.img_width = p_sys->i_width / p_sys->i_decimation;
897 if( p_sys->i_height )
898 mjpeg.img_height = p_sys->i_height / p_sys->i_decimation;
900 /* establish Quicktime APP1 marker while we are here */
905 p_app1 = (struct quicktime_mjpeg_app1 *)mjpeg.APP_data;
906 p_app1->i_reserved = 0;
907 p_app1->i_tag = VLC_FOURCC( 'm','j','p','g' );
908 p_app1->i_field_size = 0;
909 p_app1->i_padded_field_size = 0;
910 p_app1->i_next_field = 0;
911 /* XXX WARNING XXX */
912 /* these's nothing magic about these values. We are dangerously
913 * assuming the encoder card is encoding mjpeg-a and is not throwing
914 * in marker tags we aren't expecting. It's bad enough we have to
915 * search through the jpeg output for every frame we grab just to
916 * find the first field's end marker, so we take this risk to boost
918 * This is really something the driver could do for us because this
919 * does conform to standards outside of Apple Quicktime.
922 p_app1->i_DQT_offset = hton32( i_offset );
924 p_app1->i_DHT_offset = hton32( i_offset );
926 p_app1->i_SOF_offset = hton32( i_offset );
928 p_app1->i_SOS_offset = hton32( i_offset );
930 p_app1->i_data_offset = hton32( i_offset );
932 /* SOF and SOS aren't specified by the mjpeg API because they aren't
933 * optional. They will be present in the output. */
934 mjpeg.jpeg_markers = JPEG_MARKER_DHT | JPEG_MARKER_DQT;
936 if( ioctl( i_fd, MJPIOC_S_PARAMS, &mjpeg ) < 0 )
938 msg_Err( p_demux, "cannot set mjpeg params (%s)",
943 p_sys->i_width = mjpeg.img_width * mjpeg.HorDcm;
944 p_sys->i_height = mjpeg.img_height * mjpeg.VerDcm *
945 mjpeg.field_per_buff;
948 /* fix width/height */
949 if( !p_sys->b_mjpeg && ( p_sys->i_width == 0 || p_sys->i_height == 0 ) )
951 struct video_window vid_win;
953 if( ioctl( i_fd, VIDIOCGWIN, &vid_win ) < 0 )
955 msg_Err( p_demux, "cannot get win (%s)", strerror( errno ) );
958 p_sys->i_width = vid_win.width;
959 p_sys->i_height = vid_win.height;
961 msg_Dbg( p_demux, "will use %dx%d", p_sys->i_width, p_sys->i_height );
964 if( !p_sys->b_mjpeg )
966 /* set hue/color/.. */
967 if( ioctl( i_fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 )
969 struct video_picture vid_picture = p_sys->vid_picture;
971 if( p_sys->i_brightness >= 0 && p_sys->i_brightness < 65536 )
973 vid_picture.brightness = p_sys->i_brightness;
975 if( p_sys->i_colour >= 0 && p_sys->i_colour < 65536 )
977 vid_picture.colour = p_sys->i_colour;
979 if( p_sys->i_hue >= 0 && p_sys->i_hue < 65536 )
981 vid_picture.hue = p_sys->i_hue;
983 if( p_sys->i_contrast >= 0 && p_sys->i_contrast < 65536 )
985 vid_picture.contrast = p_sys->i_contrast;
987 if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 )
989 msg_Dbg( p_demux, "v4l device uses brightness: %d",
990 vid_picture.brightness );
991 msg_Dbg( p_demux, "v4l device uses colour: %d",
992 vid_picture.colour );
993 msg_Dbg( p_demux, "v4l device uses hue: %d", vid_picture.hue );
994 msg_Dbg( p_demux, "v4l device uses contrast: %d",
995 vid_picture.contrast );
996 p_sys->vid_picture = vid_picture;
1000 /* Find out video format used by device */
1001 if( ioctl( i_fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 )
1003 struct video_picture vid_picture = p_sys->vid_picture;
1007 vid_picture.palette = 0;
1008 p_sys->i_fourcc = 0;
1010 psz = var_CreateGetString( p_demux, "v4l-chroma" );
1011 if( strlen( psz ) >= 4 )
1013 int i_chroma = VLC_FOURCC( psz[0], psz[1], psz[2], psz[3] );
1015 /* Find out v4l chroma code */
1016 for( i = 0; v4lchroma_to_fourcc[i].i_v4l != 0; i++ )
1018 if( v4lchroma_to_fourcc[i].i_fourcc == i_chroma )
1020 vid_picture.palette = v4lchroma_to_fourcc[i].i_v4l;
1027 if( vid_picture.palette &&
1028 !ioctl( i_fd, VIDIOCSPICT, &vid_picture ) )
1030 p_sys->vid_picture = vid_picture;
1034 /* Try to set the format to something easy to encode */
1035 vid_picture.palette = VIDEO_PALETTE_YUV420P;
1036 if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 )
1038 p_sys->vid_picture = vid_picture;
1042 vid_picture.palette = VIDEO_PALETTE_YUV422P;
1043 if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 )
1045 p_sys->vid_picture = vid_picture;
1050 /* Find out final format */
1051 for( i = 0; v4lchroma_to_fourcc[i].i_v4l != 0; i++ )
1053 if( v4lchroma_to_fourcc[i].i_v4l == p_sys->vid_picture.palette)
1055 p_sys->i_fourcc = v4lchroma_to_fourcc[i].i_fourcc;
1063 msg_Err( p_demux, "ioctl VIDIOCGPICT failed" );
1068 if( p_sys->b_mjpeg )
1072 p_sys->mjpeg_buffers.count = 8;
1073 p_sys->mjpeg_buffers.size = MJPEG_BUFFER_SIZE;
1075 if( ioctl( i_fd, MJPIOC_REQBUFS, &p_sys->mjpeg_buffers ) < 0 )
1077 msg_Err( p_demux, "mmap unsupported" );
1081 p_sys->p_video_mmap = mmap( 0,
1082 p_sys->mjpeg_buffers.size * p_sys->mjpeg_buffers.count,
1083 PROT_READ | PROT_WRITE, MAP_SHARED, i_fd, 0 );
1084 if( p_sys->p_video_mmap == MAP_FAILED )
1086 msg_Err( p_demux, "mmap failed" );
1090 p_sys->i_fourcc = VLC_FOURCC( 'm','j','p','g' );
1091 p_sys->i_frame_pos = -1;
1093 /* queue up all the frames */
1094 for( i = 0; i < (int)p_sys->mjpeg_buffers.count; i++ )
1096 if( ioctl( i_fd, MJPIOC_QBUF_CAPT, &i ) < 0 )
1098 msg_Err( p_demux, "unable to queue frame" );
1105 /* Fill in picture_t fields */
1106 vout_InitPicture( VLC_OBJECT(p_demux), &p_sys->pic, p_sys->i_fourcc,
1107 p_sys->i_width, p_sys->i_height, p_sys->i_width *
1108 VOUT_ASPECT_FACTOR / p_sys->i_height );
1109 if( !p_sys->pic.i_planes )
1111 msg_Err( p_demux, "unsupported chroma" );
1114 p_sys->i_video_frame_size = 0;
1115 for( i = 0; i < p_sys->pic.i_planes; i++ )
1117 p_sys->i_video_frame_size += p_sys->pic.p[i].i_visible_lines *
1118 p_sys->pic.p[i].i_visible_pitch;
1121 msg_Dbg( p_demux, "v4l device uses frame size: %i",
1122 p_sys->i_video_frame_size );
1123 msg_Dbg( p_demux, "v4l device uses chroma: %4.4s",
1124 (char*)&p_sys->i_fourcc );
1126 /* Allocate mmap buffer */
1127 if( ioctl( i_fd, VIDIOCGMBUF, &p_sys->vid_mbuf ) < 0 )
1129 msg_Err( p_demux, "mmap unsupported" );
1133 p_sys->p_video_mmap = mmap( 0, p_sys->vid_mbuf.size,
1134 PROT_READ|PROT_WRITE, MAP_SHARED,
1136 if( p_sys->p_video_mmap == MAP_FAILED )
1138 /* FIXME -> normal read */
1139 msg_Err( p_demux, "mmap failed" );
1144 p_sys->vid_mmap.frame = 0;
1145 p_sys->vid_mmap.width = p_sys->i_width;
1146 p_sys->vid_mmap.height = p_sys->i_height;
1147 p_sys->vid_mmap.format = p_sys->vid_picture.palette;
1148 if( ioctl( i_fd, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 )
1150 msg_Warn( p_demux, "%4.4s refused", (char*)&p_sys->i_fourcc );
1151 msg_Err( p_demux, "chroma selection failed" );
1159 if( i_fd >= 0 ) close( i_fd );
1163 /*****************************************************************************
1165 *****************************************************************************/
1166 static int OpenAudioDev( demux_t *p_demux, char *psz_device )
1168 demux_sys_t *p_sys = p_demux->p_sys;
1171 if( (i_fd = open( psz_device, O_RDONLY | O_NONBLOCK )) < 0 )
1173 msg_Err( p_demux, "cannot open audio device (%s)", strerror( errno ) );
1177 i_format = AFMT_S16_LE;
1178 if( ioctl( i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
1179 || i_format != AFMT_S16_LE )
1181 msg_Err( p_demux, "cannot set audio format (16b little endian) "
1182 "(%s)", strerror( errno ) );
1186 if( ioctl( i_fd, SNDCTL_DSP_STEREO,
1187 &p_sys->b_stereo ) < 0 )
1189 msg_Err( p_demux, "cannot set audio channels count (%s)",
1190 strerror( errno ) );
1194 if( ioctl( i_fd, SNDCTL_DSP_SPEED,
1195 &p_sys->i_sample_rate ) < 0 )
1197 msg_Err( p_demux, "cannot set audio sample rate (%s)",
1198 strerror( errno ) );
1202 msg_Dbg( p_demux, "openened adev=`%s' %s %dHz",
1203 psz_device, p_sys->b_stereo ? "stereo" : "mono",
1204 p_sys->i_sample_rate );
1206 p_sys->i_audio_max_frame_size = 6 * 1024;
1212 if( i_fd >= 0 ) close( i_fd );
1216 /*****************************************************************************
1217 * GrabAudio: grab audio
1218 *****************************************************************************/
1219 static block_t *GrabAudio( demux_t *p_demux )
1221 demux_sys_t *p_sys = p_demux->p_sys;
1222 struct audio_buf_info buf_info;
1223 int i_read, i_correct;
1226 if( p_sys->p_block_audio ) p_block = p_sys->p_block_audio;
1227 else p_block = block_New( p_demux, p_sys->i_audio_max_frame_size );
1231 msg_Warn( p_demux, "cannot get block" );
1235 p_sys->p_block_audio = p_block;
1237 i_read = read( p_sys->fd_audio, p_block->p_buffer,
1238 p_sys->i_audio_max_frame_size );
1240 if( i_read <= 0 ) return 0;
1242 p_block->i_buffer = i_read;
1243 p_sys->p_block_audio = 0;
1245 /* Correct the date because of kernel buffering */
1247 if( ioctl( p_sys->fd_audio, SNDCTL_DSP_GETISPACE, &buf_info ) == 0 )
1249 i_correct += buf_info.bytes;
1252 p_block->i_pts = p_block->i_dts =
1253 mdate() - I64C(1000000) * (mtime_t)i_correct /
1254 2 / ( p_sys->b_stereo ? 2 : 1) / p_sys->i_sample_rate;
1259 /*****************************************************************************
1261 *****************************************************************************/
1262 static uint8_t *GrabCapture( demux_t *p_demux )
1264 demux_sys_t *p_sys = p_demux->p_sys;
1265 int i_captured_frame = p_sys->i_frame_pos;
1267 p_sys->vid_mmap.frame = (p_sys->i_frame_pos + 1) % p_sys->vid_mbuf.frames;
1269 while( ioctl( p_sys->fd_video, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 )
1271 if( errno != EAGAIN )
1273 msg_Err( p_demux, "failed capturing new frame" );
1277 if( p_demux->b_die )
1282 msg_Dbg( p_demux, "grab failed, trying again" );
1285 while( ioctl(p_sys->fd_video, VIDIOCSYNC, &p_sys->i_frame_pos) < 0 )
1287 if( errno != EAGAIN && errno != EINTR )
1289 msg_Err( p_demux, "failed syncing new frame" );
1294 p_sys->i_frame_pos = p_sys->vid_mmap.frame;
1295 /* leave i_video_frame_size alone */
1296 return p_sys->p_video_mmap + p_sys->vid_mbuf.offsets[i_captured_frame];
1299 static uint8_t *GrabMJPEG( demux_t *p_demux )
1301 demux_sys_t *p_sys = p_demux->p_sys;
1302 struct mjpeg_sync sync;
1303 uint8_t *p_frame, *p_field, *p;
1306 struct quicktime_mjpeg_app1 *p_app1 = NULL;
1308 /* re-queue the last frame we sync'd */
1309 if( p_sys->i_frame_pos != -1 )
1311 while( ioctl( p_sys->fd_video, MJPIOC_QBUF_CAPT,
1312 &p_sys->i_frame_pos ) < 0 )
1314 if( errno != EAGAIN && errno != EINTR )
1316 msg_Err( p_demux, "failed capturing new frame" );
1322 /* sync on the next frame */
1323 while( ioctl( p_sys->fd_video, MJPIOC_SYNC, &sync ) < 0 )
1325 if( errno != EAGAIN && errno != EINTR )
1327 msg_Err( p_demux, "failed syncing new frame" );
1332 p_sys->i_frame_pos = sync.frame;
1333 p_frame = p_sys->p_video_mmap + p_sys->mjpeg_buffers.size * sync.frame;
1335 /* p_frame now points to the data. fix up the Quicktime APP1 marker */
1337 tag = hton16( tag );
1341 p = memmem( p_field, sync.length, &tag, 2 );
1345 p += 2; /* data immediately following EOI */
1347 p_app1 = (struct quicktime_mjpeg_app1 *)(p_field + 6);
1349 i_size = ((uint32_t)(p - p_field));
1350 i_size = hton32( i_size );
1351 memcpy( &p_app1->i_field_size, &i_size, 4 );
1353 while( *p == 0xff && *(p+1) == 0xff )
1356 i_size = ((uint32_t)(p - p_field));
1357 i_size = hton32( i_size );
1358 memcpy( &p_app1->i_padded_field_size, &i_size, 4 );
1362 tag = hton16( tag );
1363 p_field = memmem( p, sync.length - (size_t)(p - p_frame), &tag, 2 );
1367 i_size = (uint32_t)(p_field - p_frame);
1368 i_size = hton32( i_size );
1369 memcpy( &p_app1->i_next_field, &i_size, 4 );
1372 p_app1 = (struct quicktime_mjpeg_app1 *)(p_field + 6);
1374 tag = hton16( tag );
1375 p = memmem( p_field, sync.length - (size_t)(p_field - p_frame),
1380 /* sometimes the second field doesn't have the EOI. just put it
1383 p = p_frame + sync.length;
1384 memcpy( p, &tag, 2 );
1389 i_size = (uint32_t)(p - p_field);
1390 i_size = hton32( i_size );
1391 memcpy( &p_app1->i_field_size, &i_size, 4 );
1392 i_size = (uint32_t)(sync.length - (uint32_t)(p_field - p_frame));
1393 i_size = hton32( i_size );
1394 memcpy( &p_app1->i_padded_field_size, &i_size, 4 );
1397 p_sys->i_video_frame_size = sync.length;
1401 static block_t *GrabVideo( demux_t *p_demux )
1403 demux_sys_t *p_sys = p_demux->p_sys;
1407 if( p_sys->f_fps >= 0.1 && p_sys->i_video_pts > 0 )
1409 mtime_t i_dur = (mtime_t)((double)1000000 / (double)p_sys->f_fps);
1411 /* Did we wait long enough ? (frame rate reduction) */
1412 if( p_sys->i_video_pts + i_dur > mdate() ) return 0;
1415 if( p_sys->b_mjpeg ) p_frame = GrabMJPEG( p_demux );
1416 else p_frame = GrabCapture( p_demux );
1418 if( !p_frame ) return 0;
1420 if( !( p_block = block_New( p_demux, p_sys->i_video_frame_size ) ) )
1422 msg_Warn( p_demux, "cannot get block" );
1426 memcpy( p_block->p_buffer, p_frame, p_sys->i_video_frame_size );
1427 p_sys->i_video_pts = p_block->i_pts = p_block->i_dts = mdate();