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;
354 fmt.video.i_aspect = 4 * VOUT_ASPECT_FACTOR / 3;
356 /* Setup rgb mask for RGB formats */
357 if( p_sys->i_fourcc == VLC_FOURCC('R','V','2','4') )
359 /* This is in BGR format */
360 fmt.video.i_bmask = 0x00ff0000;
361 fmt.video.i_gmask = 0x0000ff00;
362 fmt.video.i_rmask = 0x000000ff;
365 msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
366 (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
367 p_sys->p_es_video = es_out_Add( p_demux->out, &fmt );
370 if( p_sys->fd_audio >= 0 )
373 es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC('a','r','a','w') );
375 fmt.audio.i_channels = p_sys->b_stereo ? 2 : 1;
376 fmt.audio.i_rate = p_sys->i_sample_rate;
377 fmt.audio.i_bitspersample = 16; // FIXME ?
378 fmt.audio.i_blockalign = fmt.audio.i_channels *
379 fmt.audio.i_bitspersample / 8;
380 fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate *
381 fmt.audio.i_bitspersample;
383 msg_Dbg( p_demux, "new audio es %d channels %dHz",
384 fmt.audio.i_channels, fmt.audio.i_rate );
386 p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt );
389 /* Update default_pts to a suitable value for access */
390 var_Create( p_demux, "v4l-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
395 /*****************************************************************************
396 * Close: close device, free resources
397 *****************************************************************************/
398 static void Close( vlc_object_t *p_this )
400 demux_t *p_demux = (demux_t *)p_this;
401 demux_sys_t *p_sys = p_demux->p_sys;
403 if( p_sys->psz_device ) free( p_sys->psz_device );
404 if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
405 if( p_sys->psz_adev ) free( p_sys->psz_adev );
406 if( p_sys->fd_video >= 0 ) close( p_sys->fd_video );
407 if( p_sys->fd_audio >= 0 ) close( p_sys->fd_audio );
408 if( p_sys->p_block_audio ) block_Release( p_sys->p_block_audio );
413 ioctl( p_sys->fd_video, MJPIOC_QBUF_CAPT, &i_noframe );
416 if( p_sys->p_video_mmap && p_sys->p_video_mmap != MAP_FAILED )
419 munmap( p_sys->p_video_mmap, p_sys->mjpeg_buffers.size *
420 p_sys->mjpeg_buffers.count );
422 munmap( p_sys->p_video_mmap, p_sys->vid_mbuf.size );
428 /*****************************************************************************
430 *****************************************************************************/
431 static int Control( demux_t *p_demux, int i_query, va_list args )
438 /* Special for access_demux */
439 case DEMUX_CAN_PAUSE:
440 case DEMUX_SET_PAUSE_STATE:
441 case DEMUX_CAN_CONTROL_PACE:
442 pb = (vlc_bool_t*)va_arg( args, vlc_bool_t * );
446 case DEMUX_GET_PTS_DELAY:
447 pi64 = (int64_t*)va_arg( args, int64_t * );
448 *pi64 = (int64_t)var_GetInteger( p_demux, "v4l-caching" ) * 1000;
452 pi64 = (int64_t*)va_arg( args, int64_t * );
456 /* TODO implement others */
464 /*****************************************************************************
466 *****************************************************************************/
467 static int Demux( demux_t *p_demux )
469 demux_sys_t *p_sys = p_demux->p_sys;
470 es_out_id_t *p_es = p_sys->p_es_audio;
471 block_t *p_block = NULL;
473 /* Try grabbing audio frames first */
474 if( p_sys->fd_audio < 0 || !( p_block = GrabAudio( p_demux ) ) )
476 /* Try grabbing video frame */
477 p_es = p_sys->p_es_video;
478 if( p_sys->fd_video > 0 ) p_block = GrabVideo( p_demux );
483 /* Sleep so we do not consume all the cpu, 10ms seems
484 * like a good value (100fps) */
489 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_block->i_pts );
490 es_out_Send( p_demux->out, p_es, p_block );
495 /*****************************************************************************
496 * ParseMRL: parse the options contained in the MRL
497 *****************************************************************************/
498 static void ParseMRL( demux_t *p_demux )
500 demux_sys_t *p_sys = p_demux->p_sys;
502 char *psz_dup = strdup( p_demux->psz_path );
503 char *psz_parser = psz_dup;
505 while( *psz_parser && *psz_parser != ':' )
510 if( *psz_parser == ':' )
515 *psz_parser++ = '\0';
516 if( !strncmp( psz_parser, "channel=", strlen( "channel=" ) ) )
518 p_sys->i_channel = strtol( psz_parser + strlen( "channel=" ),
521 else if( !strncmp( psz_parser, "norm=", strlen( "norm=" ) ) )
523 psz_parser += strlen( "norm=" );
524 if( !strncmp( psz_parser, "pal", strlen( "pal" ) ) )
526 p_sys->i_norm = VIDEO_MODE_PAL;
527 psz_parser += strlen( "pal" );
529 else if( !strncmp( psz_parser, "ntsc", strlen( "ntsc" ) ) )
531 p_sys->i_norm = VIDEO_MODE_NTSC;
532 psz_parser += strlen( "ntsc" );
534 else if( !strncmp( psz_parser, "secam", strlen( "secam" ) ) )
536 p_sys->i_norm = VIDEO_MODE_SECAM;
537 psz_parser += strlen( "secam" );
539 else if( !strncmp( psz_parser, "auto", strlen( "auto" ) ) )
541 p_sys->i_norm = VIDEO_MODE_AUTO;
542 psz_parser += strlen( "auto" );
546 p_sys->i_norm = strtol( psz_parser, &psz_parser, 0 );
549 else if( !strncmp( psz_parser, "frequency=",
550 strlen( "frequency=" ) ) )
553 strtol( psz_parser + strlen( "frequency=" ),
555 if( p_sys->i_frequency < 30000 )
557 msg_Warn( p_demux, "v4l syntax has changed : "
558 "'frequency' is now channel frequency in kHz");
561 else if( !strncmp( psz_parser, "audio=", strlen( "audio=" ) ) )
563 p_sys->i_audio = strtol( psz_parser + strlen( "audio=" ),
566 else if( !strncmp( psz_parser, "size=", strlen( "size=" ) ) )
568 psz_parser += strlen( "size=" );
569 if( !strncmp( psz_parser, "subqcif", strlen( "subqcif" ) ) )
571 p_sys->i_width = 128;
572 p_sys->i_height = 96;
574 else if( !strncmp( psz_parser, "qsif", strlen( "qsif" ) ) )
576 p_sys->i_width = 160;
577 p_sys->i_height = 120;
579 else if( !strncmp( psz_parser, "qcif", strlen( "qcif" ) ) )
581 p_sys->i_width = 176;
582 p_sys->i_height = 144;
584 else if( !strncmp( psz_parser, "sif", strlen( "sif" ) ) )
586 p_sys->i_width = 320;
587 p_sys->i_height = 244;
589 else if( !strncmp( psz_parser, "cif", strlen( "cif" ) ) )
591 p_sys->i_width = 352;
592 p_sys->i_height = 288;
594 else if( !strncmp( psz_parser, "vga", strlen( "vga" ) ) )
596 p_sys->i_width = 640;
597 p_sys->i_height = 480;
602 p_sys->i_width = strtol( psz_parser, &psz_parser, 0 );
603 if( *psz_parser == 'x' || *psz_parser == 'X')
605 p_sys->i_height = strtol( psz_parser + 1,
608 msg_Dbg( p_demux, "WxH %dx%d", p_sys->i_width,
612 else if( !strncmp( psz_parser, "brightness=", strlen( "brightness=" ) ) )
614 p_sys->i_brightness = strtol( psz_parser + strlen( "brightness=" ),
617 else if( !strncmp( psz_parser, "colour=", strlen( "colour=" ) ) )
619 p_sys->i_colour = strtol( psz_parser + strlen( "colour=" ),
622 else if( !strncmp( psz_parser, "hue=", strlen( "hue=" ) ) )
624 p_sys->i_hue = strtol( psz_parser + strlen( "hue=" ),
627 else if( !strncmp( psz_parser, "contrast=", strlen( "contrast=" ) ) )
629 p_sys->i_contrast = strtol( psz_parser + strlen( "contrast=" ),
632 else if( !strncmp( psz_parser, "tuner=", strlen( "tuner=" ) ) )
634 p_sys->i_tuner = strtol( psz_parser + strlen( "tuner=" ),
637 else if( !strncmp( psz_parser, "adev=", strlen( "adev=" ) ) )
641 psz_parser += strlen( "adev=" );
642 if( strchr( psz_parser, ':' ) )
644 i_len = strchr( psz_parser, ':' ) - psz_parser;
648 i_len = strlen( psz_parser );
651 p_sys->psz_adev = strndup( psz_parser, i_len );
655 else if( !strncmp( psz_parser, "samplerate=",
656 strlen( "samplerate=" ) ) )
658 p_sys->i_sample_rate =
659 strtol( psz_parser + strlen( "samplerate=" ),
662 else if( !strncmp( psz_parser, "stereo", strlen( "stereo" ) ) )
664 psz_parser += strlen( "stereo" );
666 p_sys->b_stereo = VLC_TRUE;
668 else if( !strncmp( psz_parser, "mono", strlen( "mono" ) ) )
670 psz_parser += strlen( "mono" );
672 p_sys->b_stereo = VLC_FALSE;
674 else if( !strncmp( psz_parser, "mjpeg", strlen( "mjpeg" ) ) )
676 psz_parser += strlen( "mjpeg" );
678 p_sys->b_mjpeg = VLC_TRUE;
680 else if( !strncmp( psz_parser, "decimation=",
681 strlen( "decimation=" ) ) )
683 p_sys->i_decimation =
684 strtol( psz_parser + strlen( "decimation=" ),
687 else if( !strncmp( psz_parser, "quality=",
688 strlen( "quality=" ) ) )
691 strtol( psz_parser + strlen( "quality=" ),
694 else if( !strncmp( psz_parser, "fps=", strlen( "fps=" ) ) )
696 p_sys->f_fps = strtof( psz_parser + strlen( "fps=" ),
701 msg_Warn( p_demux, "unknown option" );
704 while( *psz_parser && *psz_parser != ':' )
709 if( *psz_parser == '\0' )
718 p_sys->psz_device = strdup( psz_dup );
720 if( psz_dup ) free( psz_dup );
723 /*****************************************************************************
725 *****************************************************************************/
726 static int OpenVideoDev( demux_t *p_demux, char *psz_device )
728 demux_sys_t *p_sys = p_demux->p_sys;
731 struct video_channel vid_channel;
732 struct mjpeg_params mjpeg;
735 if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
737 msg_Err( p_demux, "cannot open device (%s)", strerror( errno ) );
741 if( ioctl( i_fd, VIDIOCGCAP, &p_sys->vid_cap ) < 0 )
743 msg_Err( p_demux, "cannot get capabilities (%s)", strerror( errno ) );
748 "V4L device %s %d channels %d audios %d < w < %d %d < h < %d",
750 p_sys->vid_cap.channels,
751 p_sys->vid_cap.audios,
752 p_sys->vid_cap.minwidth, p_sys->vid_cap.maxwidth,
753 p_sys->vid_cap.minheight, p_sys->vid_cap.maxheight );
755 if( p_sys->i_channel < 0 || p_sys->i_channel >= p_sys->vid_cap.channels )
757 msg_Dbg( p_demux, "invalid channel, falling back on channel 0" );
758 p_sys->i_channel = 0;
760 if( p_sys->i_audio >= p_sys->vid_cap.audios )
762 msg_Dbg( p_demux, "invalid audio, falling back with no audio" );
766 if( p_sys->i_width < p_sys->vid_cap.minwidth ||
767 p_sys->i_width > p_sys->vid_cap.maxwidth )
769 msg_Dbg( p_demux, "invalid width %i", p_sys->i_width );
772 if( p_sys->i_height < p_sys->vid_cap.minheight ||
773 p_sys->i_height > p_sys->vid_cap.maxheight )
775 msg_Dbg( p_demux, "invalid height %i", p_sys->i_height );
779 if( !( p_sys->vid_cap.type & VID_TYPE_CAPTURE ) )
781 msg_Err( p_demux, "cannot grab" );
785 vid_channel.channel = p_sys->i_channel;
786 if( ioctl( i_fd, VIDIOCGCHAN, &vid_channel ) < 0 )
788 msg_Err( p_demux, "cannot get channel infos (%s)",
793 "setting channel %s(%d) %d tuners flags=0x%x type=0x%x norm=0x%x",
794 vid_channel.name, vid_channel.channel, vid_channel.tuners,
795 vid_channel.flags, vid_channel.type, vid_channel.norm );
797 if( p_sys->i_tuner >= vid_channel.tuners )
799 msg_Dbg( p_demux, "invalid tuner, falling back on tuner 0" );
803 vid_channel.norm = p_sys->i_norm;
804 if( ioctl( i_fd, VIDIOCSCHAN, &vid_channel ) < 0 )
806 msg_Err( p_demux, "cannot set channel (%s)", strerror( errno ) );
810 if( vid_channel.flags & VIDEO_VC_TUNER )
815 struct video_tuner vid_tuner;
816 if( p_sys->i_tuner >= 0 )
818 vid_tuner.tuner = p_sys->i_tuner;
819 if( ioctl( i_fd, VIDIOCGTUNER, &vid_tuner ) < 0 )
821 msg_Err( p_demux, "cannot get tuner (%s)", strerror( errno ) );
824 msg_Dbg( p_demux, "tuner %s low=%d high=%d, flags=0x%x "
825 "mode=0x%x signal=0x%x",
826 vid_tuner.name, vid_tuner.rangelow, vid_tuner.rangehigh,
827 vid_tuner.flags, vid_tuner.mode, vid_tuner.signal );
829 msg_Dbg( p_demux, "setting tuner %s (%d)",
830 vid_tuner.name, vid_tuner.tuner );
832 /* FIXME FIXME to be checked FIXME FIXME */
833 //vid_tuner.mode = p_sys->i_norm;
834 if( ioctl( i_fd, VIDIOCSTUNER, &vid_tuner ) < 0 )
836 msg_Err( p_demux, "cannot set tuner (%s)", strerror( errno ) );
842 /* Show a warning if frequency is < than 30000.
843 * User is certainly usint old syntax. */
847 if( p_sys->i_frequency >= 0 )
849 int driver_frequency = p_sys->i_frequency * 16 /1000;
850 if( ioctl( i_fd, VIDIOCSFREQ, &driver_frequency ) < 0 )
852 msg_Err( p_demux, "cannot set frequency (%s)",
856 msg_Dbg( p_demux, "frequency %d (%d)", p_sys->i_frequency,
862 if( vid_channel.flags & VIDEO_VC_AUDIO )
864 struct video_audio vid_audio;
866 /* XXX TODO volume, balance, ... */
867 if( p_sys->i_audio >= 0 )
869 vid_audio.audio = p_sys->i_audio;
870 if( ioctl( i_fd, VIDIOCGAUDIO, &vid_audio ) < 0 )
872 msg_Err( p_demux, "cannot get audio (%s)", strerror( errno ) );
877 vid_audio.flags &= ~VIDEO_AUDIO_MUTE;
879 if( ioctl( i_fd, VIDIOCSAUDIO, &vid_audio ) < 0 )
881 msg_Err( p_demux, "cannot set audio (%s)", strerror( errno ) );
888 /* establish basic params with input and norm before feeling width
892 struct quicktime_mjpeg_app1 *p_app1;
895 if( ioctl( i_fd, MJPIOC_G_PARAMS, &mjpeg ) < 0 )
897 msg_Err( p_demux, "cannot get mjpeg params (%s)",
901 mjpeg.input = p_sys->i_channel;
902 mjpeg.norm = p_sys->i_norm;
903 mjpeg.decimation = p_sys->i_decimation;
906 mjpeg.img_width = p_sys->i_width / p_sys->i_decimation;
907 if( p_sys->i_height )
908 mjpeg.img_height = p_sys->i_height / p_sys->i_decimation;
910 /* establish Quicktime APP1 marker while we are here */
915 p_app1 = (struct quicktime_mjpeg_app1 *)mjpeg.APP_data;
916 p_app1->i_reserved = 0;
917 p_app1->i_tag = VLC_FOURCC( 'm','j','p','g' );
918 p_app1->i_field_size = 0;
919 p_app1->i_padded_field_size = 0;
920 p_app1->i_next_field = 0;
921 /* XXX WARNING XXX */
922 /* these's nothing magic about these values. We are dangerously
923 * assuming the encoder card is encoding mjpeg-a and is not throwing
924 * in marker tags we aren't expecting. It's bad enough we have to
925 * search through the jpeg output for every frame we grab just to
926 * find the first field's end marker, so we take this risk to boost
928 * This is really something the driver could do for us because this
929 * does conform to standards outside of Apple Quicktime.
932 p_app1->i_DQT_offset = hton32( i_offset );
934 p_app1->i_DHT_offset = hton32( i_offset );
936 p_app1->i_SOF_offset = hton32( i_offset );
938 p_app1->i_SOS_offset = hton32( i_offset );
940 p_app1->i_data_offset = hton32( i_offset );
942 /* SOF and SOS aren't specified by the mjpeg API because they aren't
943 * optional. They will be present in the output. */
944 mjpeg.jpeg_markers = JPEG_MARKER_DHT | JPEG_MARKER_DQT;
946 if( ioctl( i_fd, MJPIOC_S_PARAMS, &mjpeg ) < 0 )
948 msg_Err( p_demux, "cannot set mjpeg params (%s)",
953 p_sys->i_width = mjpeg.img_width * mjpeg.HorDcm;
954 p_sys->i_height = mjpeg.img_height * mjpeg.VerDcm *
955 mjpeg.field_per_buff;
958 /* fix width/height */
959 if( !p_sys->b_mjpeg && ( p_sys->i_width == 0 || p_sys->i_height == 0 ) )
961 struct video_window vid_win;
963 if( ioctl( i_fd, VIDIOCGWIN, &vid_win ) < 0 )
965 msg_Err( p_demux, "cannot get win (%s)", strerror( errno ) );
968 p_sys->i_width = vid_win.width;
969 p_sys->i_height = vid_win.height;
971 msg_Dbg( p_demux, "will use %dx%d", p_sys->i_width, p_sys->i_height );
974 if( !p_sys->b_mjpeg )
976 /* set hue/color/.. */
977 if( ioctl( i_fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 )
979 struct video_picture vid_picture = p_sys->vid_picture;
981 if( p_sys->i_brightness >= 0 && p_sys->i_brightness < 65536 )
983 vid_picture.brightness = p_sys->i_brightness;
985 if( p_sys->i_colour >= 0 && p_sys->i_colour < 65536 )
987 vid_picture.colour = p_sys->i_colour;
989 if( p_sys->i_hue >= 0 && p_sys->i_hue < 65536 )
991 vid_picture.hue = p_sys->i_hue;
993 if( p_sys->i_contrast >= 0 && p_sys->i_contrast < 65536 )
995 vid_picture.contrast = p_sys->i_contrast;
997 if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 )
999 msg_Dbg( p_demux, "v4l device uses brightness: %d",
1000 vid_picture.brightness );
1001 msg_Dbg( p_demux, "v4l device uses colour: %d",
1002 vid_picture.colour );
1003 msg_Dbg( p_demux, "v4l device uses hue: %d", vid_picture.hue );
1004 msg_Dbg( p_demux, "v4l device uses contrast: %d",
1005 vid_picture.contrast );
1006 p_sys->vid_picture = vid_picture;
1010 /* Find out video format used by device */
1011 if( ioctl( i_fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 )
1013 struct video_picture vid_picture = p_sys->vid_picture;
1017 vid_picture.palette = 0;
1018 p_sys->i_fourcc = 0;
1020 psz = var_CreateGetString( p_demux, "v4l-chroma" );
1021 if( strlen( psz ) >= 4 )
1023 int i_chroma = VLC_FOURCC( psz[0], psz[1], psz[2], psz[3] );
1025 /* Find out v4l chroma code */
1026 for( i = 0; v4lchroma_to_fourcc[i].i_v4l != 0; i++ )
1028 if( v4lchroma_to_fourcc[i].i_fourcc == i_chroma )
1030 vid_picture.palette = v4lchroma_to_fourcc[i].i_v4l;
1037 if( vid_picture.palette &&
1038 !ioctl( i_fd, VIDIOCSPICT, &vid_picture ) )
1040 p_sys->vid_picture = vid_picture;
1044 /* Try to set the format to something easy to encode */
1045 vid_picture.palette = VIDEO_PALETTE_YUV420P;
1046 if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 )
1048 p_sys->vid_picture = vid_picture;
1052 vid_picture.palette = VIDEO_PALETTE_YUV422P;
1053 if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 )
1055 p_sys->vid_picture = vid_picture;
1060 /* Find out final format */
1061 for( i = 0; v4lchroma_to_fourcc[i].i_v4l != 0; i++ )
1063 if( v4lchroma_to_fourcc[i].i_v4l == p_sys->vid_picture.palette)
1065 p_sys->i_fourcc = v4lchroma_to_fourcc[i].i_fourcc;
1072 msg_Err( p_demux, "ioctl VIDIOCGPICT failed" );
1077 if( p_sys->b_mjpeg )
1081 p_sys->mjpeg_buffers.count = 8;
1082 p_sys->mjpeg_buffers.size = MJPEG_BUFFER_SIZE;
1084 if( ioctl( i_fd, MJPIOC_REQBUFS, &p_sys->mjpeg_buffers ) < 0 )
1086 msg_Err( p_demux, "mmap unsupported" );
1090 p_sys->p_video_mmap = mmap( 0,
1091 p_sys->mjpeg_buffers.size * p_sys->mjpeg_buffers.count,
1092 PROT_READ | PROT_WRITE, MAP_SHARED, i_fd, 0 );
1093 if( p_sys->p_video_mmap == MAP_FAILED )
1095 msg_Err( p_demux, "mmap failed" );
1099 p_sys->i_fourcc = VLC_FOURCC( 'm','j','p','g' );
1100 p_sys->i_frame_pos = -1;
1102 /* queue up all the frames */
1103 for( i = 0; i < (int)p_sys->mjpeg_buffers.count; i++ )
1105 if( ioctl( i_fd, MJPIOC_QBUF_CAPT, &i ) < 0 )
1107 msg_Err( p_demux, "unable to queue frame" );
1114 /* Fill in picture_t fields */
1115 vout_InitPicture( VLC_OBJECT(p_demux), &p_sys->pic, p_sys->i_fourcc,
1116 p_sys->i_width, p_sys->i_height, p_sys->i_width *
1117 VOUT_ASPECT_FACTOR / p_sys->i_height );
1118 if( !p_sys->pic.i_planes )
1120 msg_Err( p_demux, "unsupported chroma" );
1123 p_sys->i_video_frame_size = 0;
1124 for( i = 0; i < p_sys->pic.i_planes; i++ )
1126 p_sys->i_video_frame_size += p_sys->pic.p[i].i_visible_lines *
1127 p_sys->pic.p[i].i_visible_pitch;
1130 msg_Dbg( p_demux, "v4l device uses frame size: %i",
1131 p_sys->i_video_frame_size );
1132 msg_Dbg( p_demux, "v4l device uses chroma: %4.4s",
1133 (char*)&p_sys->i_fourcc );
1135 /* Allocate mmap buffer */
1136 if( ioctl( i_fd, VIDIOCGMBUF, &p_sys->vid_mbuf ) < 0 )
1138 msg_Err( p_demux, "mmap unsupported" );
1142 p_sys->p_video_mmap = mmap( 0, p_sys->vid_mbuf.size,
1143 PROT_READ|PROT_WRITE, MAP_SHARED,
1145 if( p_sys->p_video_mmap == MAP_FAILED )
1147 /* FIXME -> normal read */
1148 msg_Err( p_demux, "mmap failed" );
1153 p_sys->vid_mmap.frame = 0;
1154 p_sys->vid_mmap.width = p_sys->i_width;
1155 p_sys->vid_mmap.height = p_sys->i_height;
1156 p_sys->vid_mmap.format = p_sys->vid_picture.palette;
1157 if( ioctl( i_fd, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 )
1159 msg_Warn( p_demux, "%4.4s refused", (char*)&p_sys->i_fourcc );
1160 msg_Err( p_demux, "chroma selection failed" );
1168 if( i_fd >= 0 ) close( i_fd );
1172 /*****************************************************************************
1174 *****************************************************************************/
1175 static int OpenAudioDev( demux_t *p_demux, char *psz_device )
1177 demux_sys_t *p_sys = p_demux->p_sys;
1180 if( (i_fd = open( psz_device, O_RDONLY | O_NONBLOCK )) < 0 )
1182 msg_Err( p_demux, "cannot open audio device (%s)", strerror( errno ) );
1186 i_format = AFMT_S16_LE;
1187 if( ioctl( i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
1188 || i_format != AFMT_S16_LE )
1190 msg_Err( p_demux, "cannot set audio format (16b little endian) "
1191 "(%s)", strerror( errno ) );
1195 if( ioctl( i_fd, SNDCTL_DSP_STEREO,
1196 &p_sys->b_stereo ) < 0 )
1198 msg_Err( p_demux, "cannot set audio channels count (%s)",
1199 strerror( errno ) );
1203 if( ioctl( i_fd, SNDCTL_DSP_SPEED,
1204 &p_sys->i_sample_rate ) < 0 )
1206 msg_Err( p_demux, "cannot set audio sample rate (%s)",
1207 strerror( errno ) );
1211 msg_Dbg( p_demux, "openened adev=`%s' %s %dHz",
1212 psz_device, p_sys->b_stereo ? "stereo" : "mono",
1213 p_sys->i_sample_rate );
1215 p_sys->i_audio_max_frame_size = 6 * 1024;
1221 if( i_fd >= 0 ) close( i_fd );
1225 /*****************************************************************************
1226 * GrabAudio: grab audio
1227 *****************************************************************************/
1228 static block_t *GrabAudio( demux_t *p_demux )
1230 demux_sys_t *p_sys = p_demux->p_sys;
1231 struct audio_buf_info buf_info;
1232 int i_read, i_correct;
1235 if( p_sys->p_block_audio ) p_block = p_sys->p_block_audio;
1236 else p_block = block_New( p_demux, p_sys->i_audio_max_frame_size );
1240 msg_Warn( p_demux, "cannot get block" );
1244 p_sys->p_block_audio = p_block;
1246 i_read = read( p_sys->fd_audio, p_block->p_buffer,
1247 p_sys->i_audio_max_frame_size );
1249 if( i_read <= 0 ) return 0;
1251 p_block->i_buffer = i_read;
1252 p_sys->p_block_audio = 0;
1254 /* Correct the date because of kernel buffering */
1256 if( ioctl( p_sys->fd_audio, SNDCTL_DSP_GETISPACE, &buf_info ) == 0 )
1258 i_correct += buf_info.bytes;
1261 p_block->i_pts = p_block->i_dts =
1262 mdate() - I64C(1000000) * (mtime_t)i_correct /
1263 2 / ( p_sys->b_stereo ? 2 : 1) / p_sys->i_sample_rate;
1268 /*****************************************************************************
1270 *****************************************************************************/
1271 static uint8_t *GrabCapture( demux_t *p_demux )
1273 demux_sys_t *p_sys = p_demux->p_sys;
1274 int i_captured_frame = p_sys->i_frame_pos;
1276 p_sys->vid_mmap.frame = (p_sys->i_frame_pos + 1) % p_sys->vid_mbuf.frames;
1278 while( ioctl( p_sys->fd_video, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 )
1280 if( errno != EAGAIN )
1282 msg_Err( p_demux, "failed capturing new frame" );
1286 if( p_demux->b_die )
1291 msg_Dbg( p_demux, "grab failed, trying again" );
1294 while( ioctl(p_sys->fd_video, VIDIOCSYNC, &p_sys->i_frame_pos) < 0 )
1296 if( errno != EAGAIN && errno != EINTR )
1298 msg_Err( p_demux, "failed syncing new frame" );
1303 p_sys->i_frame_pos = p_sys->vid_mmap.frame;
1304 /* leave i_video_frame_size alone */
1305 return p_sys->p_video_mmap + p_sys->vid_mbuf.offsets[i_captured_frame];
1308 static uint8_t *GrabMJPEG( demux_t *p_demux )
1310 demux_sys_t *p_sys = p_demux->p_sys;
1311 struct mjpeg_sync sync;
1312 uint8_t *p_frame, *p_field, *p;
1315 struct quicktime_mjpeg_app1 *p_app1 = NULL;
1317 /* re-queue the last frame we sync'd */
1318 if( p_sys->i_frame_pos != -1 )
1320 while( ioctl( p_sys->fd_video, MJPIOC_QBUF_CAPT,
1321 &p_sys->i_frame_pos ) < 0 )
1323 if( errno != EAGAIN && errno != EINTR )
1325 msg_Err( p_demux, "failed capturing new frame" );
1331 /* sync on the next frame */
1332 while( ioctl( p_sys->fd_video, MJPIOC_SYNC, &sync ) < 0 )
1334 if( errno != EAGAIN && errno != EINTR )
1336 msg_Err( p_demux, "failed syncing new frame" );
1341 p_sys->i_frame_pos = sync.frame;
1342 p_frame = p_sys->p_video_mmap + p_sys->mjpeg_buffers.size * sync.frame;
1344 /* p_frame now points to the data. fix up the Quicktime APP1 marker */
1346 tag = hton16( tag );
1350 p = memmem( p_field, sync.length, &tag, 2 );
1354 p += 2; /* data immediately following EOI */
1356 p_app1 = (struct quicktime_mjpeg_app1 *)(p_field + 6);
1358 i_size = ((uint32_t)(p - p_field));
1359 i_size = hton32( i_size );
1360 memcpy( &p_app1->i_field_size, &i_size, 4 );
1362 while( *p == 0xff && *(p+1) == 0xff )
1365 i_size = ((uint32_t)(p - p_field));
1366 i_size = hton32( i_size );
1367 memcpy( &p_app1->i_padded_field_size, &i_size, 4 );
1371 tag = hton16( tag );
1372 p_field = memmem( p, sync.length - (size_t)(p - p_frame), &tag, 2 );
1376 i_size = (uint32_t)(p_field - p_frame);
1377 i_size = hton32( i_size );
1378 memcpy( &p_app1->i_next_field, &i_size, 4 );
1381 p_app1 = (struct quicktime_mjpeg_app1 *)(p_field + 6);
1383 tag = hton16( tag );
1384 p = memmem( p_field, sync.length - (size_t)(p_field - p_frame),
1389 /* sometimes the second field doesn't have the EOI. just put it
1392 p = p_frame + sync.length;
1393 memcpy( p, &tag, 2 );
1398 i_size = (uint32_t)(p - p_field);
1399 i_size = hton32( i_size );
1400 memcpy( &p_app1->i_field_size, &i_size, 4 );
1401 i_size = (uint32_t)(sync.length - (uint32_t)(p_field - p_frame));
1402 i_size = hton32( i_size );
1403 memcpy( &p_app1->i_padded_field_size, &i_size, 4 );
1406 p_sys->i_video_frame_size = sync.length;
1410 static block_t *GrabVideo( demux_t *p_demux )
1412 demux_sys_t *p_sys = p_demux->p_sys;
1416 if( p_sys->f_fps >= 0.1 && p_sys->i_video_pts > 0 )
1418 mtime_t i_dur = (mtime_t)((double)1000000 / (double)p_sys->f_fps);
1420 /* Did we wait long enough ? (frame rate reduction) */
1421 if( p_sys->i_video_pts + i_dur > mdate() ) return 0;
1424 if( p_sys->b_mjpeg ) p_frame = GrabMJPEG( p_demux );
1425 else p_frame = GrabCapture( p_demux );
1427 if( !p_frame ) return 0;
1429 if( !( p_block = block_New( p_demux, p_sys->i_video_frame_size ) ) )
1431 msg_Warn( p_demux, "cannot get block" );
1435 memcpy( p_block->p_buffer, p_frame, p_sys->i_video_frame_size );
1436 p_sys->i_video_pts = p_block->i_pts = p_block->i_dts = mdate();