1 /*****************************************************************************
2 * v4l.c : Video4Linux input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: v4l.c,v 1.14 2003/05/08 19:21:39 fenrir Exp $
7 * Author: Samuel Hocevar <sam@zoy.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
32 #include <vlc/input.h>
37 #include <sys/types.h>
39 #include <sys/ioctl.h>
45 #include <linux/videodev.h>
47 #include <sys/soundcard.h>
49 /*****************************************************************************
51 *****************************************************************************/
52 static int AccessOpen ( vlc_object_t * );
53 static void AccessClose ( vlc_object_t * );
54 static int Read ( input_thread_t *, byte_t *, size_t );
56 static int DemuxOpen ( vlc_object_t * );
57 static void DemuxClose ( vlc_object_t * );
58 static int Demux ( input_thread_t * );
61 /*****************************************************************************
63 *****************************************************************************/
64 #define CACHING_TEXT N_("caching value in ms")
65 #define CACHING_LONGTEXT N_( \
66 "Allows you to modify the default caching value for v4l streams. This " \
67 "value should be set in miliseconds units." )
70 set_description( _("Video4Linux input") );
71 add_category_hint( N_("v4l"), NULL, VLC_TRUE );
72 add_integer( "v4l-caching", DEFAULT_PTS_DELAY / 1000, NULL,
73 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
74 add_shortcut( "v4l" );
75 set_capability( "access", 10 );
76 set_callbacks( AccessOpen, AccessClose );
79 set_description( _("Video4Linux demuxer") );
80 add_shortcut( "v4l" );
81 set_capability( "demux", 200 );
82 set_callbacks( DemuxOpen, DemuxClose );
86 /****************************************************************************
88 ****************************************************************************/
91 char *psz_video_device;
94 vlc_fourcc_t i_codec; // if i_codec != i_chroma then we need a compressor
95 video_encoder_t *p_encoder;
107 struct video_capability vid_cap;
108 struct video_mbuf vid_mbuf;
110 uint8_t *p_video_mmap;
112 struct video_mmap vid_mmap;
113 struct video_picture vid_picture;
115 uint8_t *p_video_frame;
116 int i_video_frame_size;
117 int i_video_frame_size_allocated;
121 vlc_fourcc_t i_acodec_raw;
125 uint8_t *p_audio_frame;
126 int i_audio_frame_size;
127 int i_audio_frame_size_allocated;
132 uint8_t *p_header; // at lest 8 bytes allocated
137 uint8_t *p_data; // never allocated
145 * fcc "auds"|"vids" 0
162 static void SetDWBE( uint8_t *p, uint32_t dw )
164 p[0] = (dw >> 24)&0xff;
165 p[1] = (dw >> 16)&0xff;
166 p[2] = (dw >> 8)&0xff;
170 static void SetQWBE( uint8_t *p, uint64_t qw )
172 SetDWBE( p, (qw >> 32)&0xffffffff );
173 SetDWBE( &p[4], qw&0xffffffff);
176 static uint32_t GetDWBE( uint8_t *p_buff )
178 return( ( p_buff[0] << 24 ) + ( p_buff[1] << 16 ) +
179 ( p_buff[2] << 8 ) + p_buff[3] );
181 static uint64_t GetQWBE( uint8_t *p )
183 return( ( (uint64_t)GetDWBE( p ) << 32) | (uint64_t)GetDWBE( &p[4] ) );
186 /*****************************************************************************
188 *****************************************************************************
190 * url: <video device>::::
192 *****************************************************************************/
193 static int AccessOpen( vlc_object_t *p_this )
195 input_thread_t *p_input = (input_thread_t *)p_this;
197 char *psz_dup, *psz_parser;
199 struct video_channel vid_channel;
201 /* create access private data */
202 p_sys = malloc( sizeof( access_sys_t ) );
203 memset( p_sys, 0, sizeof( access_sys_t ) );
205 p_sys->psz_video_device = NULL;
207 p_sys->i_channel = -1;
209 p_sys->i_norm = VIDEO_MODE_AUTO; // auto
211 p_sys->i_frequency = -1;
215 p_sys->i_frame_pos = 0;
217 p_sys->i_codec = VLC_FOURCC( 0, 0, 0, 0 );
218 p_sys->i_video_frame_size_allocated = 0;
219 p_sys->psz_adev = NULL;
220 p_sys->fd_audio = -1;
221 p_sys->i_sample_rate = 44100;
222 p_sys->b_stereo = VLC_TRUE;
224 p_sys->i_data_size = 0;
225 p_sys->i_data_pos = 0;
226 p_sys->p_data = NULL;
228 /* parse url and open device(s) */
229 psz_dup = strdup( p_input->psz_name );
230 psz_parser = psz_dup;
232 while( *psz_parser && *psz_parser != ':' )
237 if( *psz_parser == ':' )
242 *psz_parser++ = '\0';
243 if( !strncmp( psz_parser, "channel=", strlen( "channel=" ) ) )
245 p_sys->i_channel = strtol( psz_parser + strlen( "channel=" ),
248 else if( !strncmp( psz_parser, "norm=", strlen( "norm=" ) ) )
250 psz_parser += strlen( "norm=" );
251 if( !strncmp( psz_parser, "pal", strlen( "pal" ) ) )
253 p_sys->i_norm = VIDEO_MODE_PAL;
254 psz_parser += strlen( "pal" );
256 else if( !strncmp( psz_parser, "ntsc", strlen( "ntsc" ) ) )
258 p_sys->i_norm = VIDEO_MODE_NTSC;
259 psz_parser += strlen( "ntsc" );
261 else if( !strncmp( psz_parser, "secam", strlen( "secam" ) ) )
263 p_sys->i_norm = VIDEO_MODE_SECAM;
264 psz_parser += strlen( "secam" );
266 else if( !strncmp( psz_parser, "auto", strlen( "auto" ) ) )
268 p_sys->i_norm = VIDEO_MODE_AUTO;
269 psz_parser += strlen( "auto" );
273 p_sys->i_norm = strtol( psz_parser, &psz_parser, 0 );
276 else if( !strncmp( psz_parser, "frequency=",
277 strlen( "frequency=" ) ) )
280 strtol( psz_parser + strlen( "frequency=" ),
283 else if( !strncmp( psz_parser, "audio=", strlen( "audio=" ) ) )
285 p_sys->i_audio = strtol( psz_parser + strlen( "audio=" ),
288 else if( !strncmp( psz_parser, "size=", strlen( "size=" ) ) )
290 psz_parser += strlen( "size=" );
291 if( !strncmp( psz_parser, "subqcif", strlen( "subqcif" ) ) )
293 p_sys->i_width = 128;
294 p_sys->i_height = 96;
296 else if( !strncmp( psz_parser, "qsif", strlen( "qsif" ) ) )
298 p_sys->i_width = 160;
299 p_sys->i_height = 120;
301 else if( !strncmp( psz_parser, "qcif", strlen( "qcif" ) ) )
303 p_sys->i_width = 176;
304 p_sys->i_height = 144;
306 else if( !strncmp( psz_parser, "sif", strlen( "sif" ) ) )
308 p_sys->i_width = 320;
309 p_sys->i_height = 244;
311 else if( !strncmp( psz_parser, "cif", strlen( "cif" ) ) )
313 p_sys->i_width = 352;
314 p_sys->i_height = 288;
316 else if( !strncmp( psz_parser, "vga", strlen( "vga" ) ) )
318 p_sys->i_width = 640;
319 p_sys->i_height = 480;
324 p_sys->i_width = strtol( psz_parser, &psz_parser, 0 );
325 if( *psz_parser == 'x' || *psz_parser == 'X')
327 p_sys->i_height = strtol( psz_parser + 1,
330 msg_Dbg( p_input, "WxH %dx%d", p_sys->i_width,
334 else if( !strncmp( psz_parser, "tuner=", strlen( "tuner=" ) ) )
336 p_sys->i_tuner = strtol( psz_parser + strlen( "tuner=" ),
339 else if( !strncmp( psz_parser, "codec=", strlen( "codec=" ) ) )
341 psz_parser += strlen( "codec=" );
342 if( !strncmp( psz_parser, "mpeg4", strlen( "mpeg4" ) ) )
344 p_sys->i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
346 else if( !strncmp( psz_parser, "mpeg1", strlen( "mpeg1" ) ) )
348 p_sys->i_codec = VLC_FOURCC( 'm', 'p', '1', 'v' );
352 msg_Warn( p_input, "unknow codec" );
355 else if( !strncmp( psz_parser, "adev=", strlen( "adev=" ) ) )
359 psz_parser += strlen( "adev=" );
360 if( strchr( psz_parser, ':' ) )
362 i_len = strchr( psz_parser, ':' ) - psz_parser;
366 i_len = strlen( psz_parser );
369 p_sys->psz_adev = strndup( psz_parser, i_len );
373 else if( !strncmp( psz_parser, "samplerate=",
374 strlen( "samplerate=" ) ) )
376 p_sys->i_sample_rate =
377 strtol( psz_parser + strlen( "samplerate=" ),
380 else if( !strncmp( psz_parser, "stereo", strlen( "stereo" ) ) )
382 psz_parser += strlen( "stereo" );
384 p_sys->b_stereo = VLC_TRUE;
386 else if( !strncmp( psz_parser, "mono", strlen( "mono" ) ) )
388 psz_parser += strlen( "mono" );
390 p_sys->b_stereo = VLC_FALSE;
394 msg_Warn( p_input, "unknow option" );
397 while( *psz_parser && *psz_parser != ':' )
402 if( *psz_parser == '\0' )
411 p_sys->psz_video_device = strdup( psz_dup );
415 p_sys->psz_video_device = strdup( "/dev/video" );
417 msg_Dbg( p_input, "video device=`%s'", p_sys->psz_video_device );
419 if( p_sys->psz_adev && *p_sys->psz_adev == '\0' )
421 p_sys->psz_adev = strdup( "/dev/dsp" );
423 msg_Dbg( p_input, "audio device=`%s'", p_sys->psz_adev );
427 if( ( p_sys->fd = open( p_sys->psz_video_device, O_RDWR ) ) < 0 )
429 msg_Err( p_input, "cannot open device" );
433 if( ioctl( p_sys->fd, VIDIOCGCAP, &p_sys->vid_cap ) < 0 )
435 msg_Err( p_input, "cannot get capabilities" );
440 "V4L device %s %d channels %d audios %d < w < %d %d < h < %d",
442 p_sys->vid_cap.channels,
443 p_sys->vid_cap.audios,
444 p_sys->vid_cap.minwidth, p_sys->vid_cap.maxwidth,
445 p_sys->vid_cap.minheight, p_sys->vid_cap.maxheight );
447 if( p_sys->i_channel < 0 || p_sys->i_channel >= p_sys->vid_cap.channels )
449 msg_Dbg( p_input, "invalid channel, falling back on channel 0" );
450 p_sys->i_channel = 0;
452 if( p_sys->i_audio >= p_sys->vid_cap.audios )
454 msg_Dbg( p_input, "invalid audio, falling back with no audio" );
458 if( p_sys->i_width < p_sys->vid_cap.minwidth ||
459 p_sys->i_width > p_sys->vid_cap.maxwidth )
461 msg_Dbg( p_input, "invalid width" );
464 if( p_sys->i_height < p_sys->vid_cap.minheight ||
465 p_sys->i_height > p_sys->vid_cap.maxheight )
467 msg_Dbg( p_input, "invalid height" );
471 if( !( p_sys->vid_cap.type&VID_TYPE_CAPTURE ) )
473 msg_Err( p_input, "cannot grab" );
477 vid_channel.channel = p_sys->i_channel;
478 if( ioctl( p_sys->fd, VIDIOCGCHAN, &vid_channel ) < 0 )
480 msg_Err( p_input, "cannot get channel infos" );
484 "setting channel %s(%d) %d tuners flags=0x%x type=0x%x norm=0x%x",
492 if( p_sys->i_tuner >= vid_channel.tuners )
494 msg_Dbg( p_input, "invalid tuner, falling back on tuner 0" );
498 vid_channel.norm = p_sys->i_norm;
499 if( ioctl( p_sys->fd, VIDIOCSCHAN, &vid_channel ) < 0 )
501 msg_Err( p_input, "cannot set channel" );
505 if( vid_channel.flags & VIDEO_VC_TUNER )
510 struct video_tuner vid_tuner;
511 if( p_sys->i_tuner >= 0 )
513 vid_tuner.tuner = p_sys->i_tuner;
514 if( ioctl( p_sys->fd, VIDIOCGTUNER, &vid_tuner ) < 0 )
516 msg_Err( p_input, "cannot get tuner" );
519 msg_Dbg( p_input, "tuner %s low=%d high=%d, flags=0x%x "
520 "mode=0x%x signal=0x%x",
521 vid_tuner.name, vid_tuner.rangelow, vid_tuner.rangehigh,
522 vid_tuner.flags, vid_tuner.mode, vid_tuner.signal );
524 msg_Dbg( p_input, "setting tuner %s (%d)",
525 vid_tuner.name, vid_tuner.tuner );
527 //vid_tuner.mode = p_sys->i_norm; /* FIXME FIXME to be checked FIXME FIXME */
528 if( ioctl( p_sys->fd, VIDIOCSTUNER, &vid_tuner ) < 0 )
530 msg_Err( p_input, "cannot set tuner" );
536 if( p_sys->i_frequency >= 0 )
538 if( ioctl( p_sys->fd, VIDIOCSFREQ, &p_sys->i_frequency ) < 0 )
540 msg_Err( p_input, "cannot set frequency" );
543 msg_Dbg( p_input, "frequency %d", p_sys->i_frequency );
548 if( vid_channel.flags & VIDEO_VC_AUDIO )
550 struct video_audio vid_audio;
552 /* XXX TODO volume, balance, ... */
553 if( p_sys->i_audio >= 0 )
555 vid_audio.audio = p_sys->i_audio;
556 if( ioctl( p_sys->fd, VIDIOCGAUDIO, &vid_audio ) < 0 )
558 msg_Err( p_input, "cannot get audio" );
563 vid_audio.flags &= ~VIDEO_AUDIO_MUTE;
565 if( ioctl( p_sys->fd, VIDIOCSAUDIO, &vid_audio ) < 0 )
567 msg_Err( p_input, "cannot set audio" );
571 if( p_sys->psz_adev )
574 if( ( p_sys->fd_audio = open( p_sys->psz_adev, O_RDONLY|O_NONBLOCK ) ) < 0 )
576 msg_Err( p_input, "cannot open audio device" );
580 i_format = AFMT_S16_LE;
581 if( ioctl( p_sys->fd_audio, SNDCTL_DSP_SETFMT, &i_format ) < 0
582 || i_format != AFMT_S16_LE )
585 "cannot set audio format (16b little endian)" );
589 if( ioctl( p_sys->fd_audio, SNDCTL_DSP_STEREO,
590 &p_sys->b_stereo ) < 0 )
592 msg_Err( p_input, "cannot set audio channels count" );
596 if( ioctl( p_sys->fd_audio, SNDCTL_DSP_SPEED,
597 &p_sys->i_sample_rate ) < 0 )
599 msg_Err( p_input, "cannot set audio sample rate" );
606 p_sys->b_stereo ? "stereo" : "mono",
607 p_sys->i_sample_rate );
609 p_sys->i_audio_frame_size = 0;
610 p_sys->i_audio_frame_size_allocated = 6*1024;
611 p_sys->p_audio_frame =
612 malloc( p_sys->i_audio_frame_size_allocated );
617 /* fix width/heigh */
618 if( p_sys->i_width == 0 || p_sys->i_height == 0 )
620 struct video_window vid_win;
622 if( ioctl( p_sys->fd, VIDIOCGWIN, &vid_win ) < 0 )
624 msg_Err( p_input, "cannot get win" );
627 p_sys->i_width = vid_win.width;
628 p_sys->i_height = vid_win.height;
630 msg_Dbg( p_input, "will use %dx%d", p_sys->i_width, p_sys->i_height );
633 p_sys->p_video_frame = NULL;
635 /* Find out video format used by device */
636 if( ioctl( p_sys->fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 )
639 struct video_picture vid_picture = p_sys->vid_picture;
641 /* Try to set the format to something easy to encode */
642 vid_picture.palette = VIDEO_PALETTE_YUV420P;
643 if( ioctl( p_sys->fd, VIDIOCSPICT, &vid_picture ) == 0 )
645 p_sys->vid_picture = vid_picture;
649 vid_picture.palette = VIDEO_PALETTE_YUV422P;
650 if( ioctl( p_sys->fd, VIDIOCSPICT, &vid_picture ) == 0 )
652 p_sys->vid_picture = vid_picture;
656 /* Find out final format */
657 switch( p_sys->vid_picture.palette )
659 case VIDEO_PALETTE_GREY:
660 i_chroma = VLC_FOURCC( 'G', 'R', 'E', 'Y' );
662 case VIDEO_PALETTE_HI240:
663 i_chroma = VLC_FOURCC( 'I', '2', '4', '0' );
665 case VIDEO_PALETTE_RGB565:
666 i_chroma = VLC_FOURCC( 'R', 'V', '1', '6' );
668 case VIDEO_PALETTE_RGB555:
669 i_chroma = VLC_FOURCC( 'R', 'V', '1', '5' );
671 case VIDEO_PALETTE_RGB24:
672 i_chroma = VLC_FOURCC( 'R', 'V', '2', '4' );
674 case VIDEO_PALETTE_RGB32:
675 i_chroma = VLC_FOURCC( 'R', 'V', '3', '2' );
677 case VIDEO_PALETTE_YUV422:
678 i_chroma = VLC_FOURCC( 'I', '4', '2', '2' );
680 case VIDEO_PALETTE_YUYV:
681 i_chroma = VLC_FOURCC( 'Y', 'U', 'Y', 'V' );
683 case VIDEO_PALETTE_UYVY:
684 i_chroma = VLC_FOURCC( 'U', 'Y', 'V', 'Y' );
686 case VIDEO_PALETTE_YUV420:
687 i_chroma = VLC_FOURCC( 'I', '4', '2', 'N' );
689 case VIDEO_PALETTE_YUV411:
690 i_chroma = VLC_FOURCC( 'I', '4', '1', 'N' );
692 case VIDEO_PALETTE_RAW:
693 i_chroma = VLC_FOURCC( 'G', 'R', 'A', 'W' );
695 case VIDEO_PALETTE_YUV422P:
696 i_chroma = VLC_FOURCC( 'I', '4', '2', '2' );
698 case VIDEO_PALETTE_YUV420P:
699 i_chroma = VLC_FOURCC( 'I', '4', '2', '0' );
701 case VIDEO_PALETTE_YUV411P:
702 i_chroma = VLC_FOURCC( 'I', '4', '1', '1' );
706 p_sys->i_chroma = i_chroma;
708 /* Fill in picture_t fields */
709 vout_InitPicture( VLC_OBJECT(p_input), &p_sys->pic,
710 p_sys->i_width, p_sys->i_height, p_sys->i_chroma );
711 if( !p_sys->pic.i_planes )
713 msg_Err( p_input, "unsupported chroma" );
716 p_sys->i_video_frame_size = 0;
717 for( i = 0; i < p_sys->pic.i_planes; i++ )
719 p_sys->i_video_frame_size += p_sys->pic.p[i].i_lines *
720 p_sys->pic.p[i].i_visible_pitch;
723 msg_Dbg( p_input, "v4l device uses frame size: %i",
724 p_sys->i_video_frame_size );
725 msg_Dbg( p_input, "v4l device uses chroma: %4.4s", (char*)&i_chroma );
729 msg_Err( p_input, "ioctl VIDIOCGPICT failed" );
733 /* Allocate mmap buffer */
734 if( ioctl( p_sys->fd, VIDIOCGMBUF, &p_sys->vid_mbuf ) < 0 )
736 msg_Err( p_input, "mmap unsupported" );
740 p_sys->p_video_mmap = mmap( 0, p_sys->vid_mbuf.size,
741 PROT_READ|PROT_WRITE, MAP_SHARED,
743 if( p_sys->p_video_mmap == MAP_FAILED )
745 /* FIXME -> normal read */
746 msg_Err( p_input, "mmap failed" );
751 p_sys->vid_mmap.frame = 0;
752 p_sys->vid_mmap.width = p_sys->i_width;
753 p_sys->vid_mmap.height = p_sys->i_height;
754 p_sys->vid_mmap.format = p_sys->vid_picture.palette;
755 if( ioctl( p_sys->fd, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 )
757 msg_Warn( p_input, "%4.4s refused", (char*)&p_sys->i_chroma );
758 msg_Err( p_input, "chroma selection failed" );
763 if( p_sys->i_codec != VLC_FOURCC( 0, 0, 0, 0 ) )
766 "need a rencoder from %4.4s to %4.4s",
767 (char*)&p_sys->i_chroma,
768 (char*)&p_sys->i_codec );
769 #define p_enc p_sys->p_encoder
770 p_enc = vlc_object_create( p_input, sizeof( video_encoder_t ) );
771 p_enc->i_codec = p_sys->i_codec;
772 p_enc->i_chroma= p_sys->i_chroma;
773 p_enc->i_width = p_sys->i_width;
774 p_enc->i_height= p_sys->i_height;
778 p_enc->p_module = module_Need( p_enc, "video encoder",
780 if( !p_enc->p_module )
782 msg_Warn( p_input, "no suitable encoder to %4.4s",
783 (char*)&p_enc->i_codec );
784 vlc_object_destroy( p_enc );
788 /* *** init the codec *** */
789 if( p_enc->pf_init( p_enc ) )
791 msg_Err( p_input, "failed to initialize video encoder plugin" );
792 vlc_object_destroy( p_enc );
796 /* *** alloacted buffer *** */
797 if( p_enc->i_buffer_size <= 0 )
799 p_enc->i_buffer_size = 1024 * 1024;// * p_enc->i_width * p_enc->i_height;
801 p_sys->i_video_frame_size = p_enc->i_buffer_size;
802 p_sys->i_video_frame_size_allocated = p_enc->i_buffer_size;
803 if( !( p_sys->p_video_frame = malloc( p_enc->i_buffer_size ) ) )
805 msg_Err( p_input, "out of memory" );
812 p_sys->i_codec = p_sys->i_chroma;
813 p_sys->p_encoder = NULL;
817 p_input->pf_read = Read;
818 p_input->pf_seek = NULL;
819 p_input->pf_set_area = NULL;
820 p_input->pf_set_program = NULL;
822 p_input->p_access_data = p_sys;
824 vlc_mutex_lock( &p_input->stream.stream_lock );
825 p_input->stream.b_pace_control = 0;
826 p_input->stream.b_seekable = 0;
827 p_input->stream.p_selected_area->i_size = 0;
828 p_input->stream.p_selected_area->i_tell = 0;
829 p_input->stream.i_method = INPUT_METHOD_FILE;
830 vlc_mutex_unlock( &p_input->stream.stream_lock );
832 /* Update default_pts to a suitable value for access */
833 p_input->i_pts_delay = config_GetInt( p_input, "v4l-caching" ) * 1000;
835 msg_Info( p_input, "v4l grabbing started" );
838 p_sys->i_header_size = 8 + 20;
839 p_sys->i_header_pos = 0;
840 p_sys->p_header = malloc( p_sys->i_header_size );
842 memcpy( &p_sys->p_header[0], ".v4l", 4 );
843 SetDWBE( &p_sys->p_header[4], 1 );
845 memcpy( &p_sys->p_header[ 8], "vids", 4 );
846 memcpy( &p_sys->p_header[12], &p_sys->i_codec, 4 );
847 SetDWBE( &p_sys->p_header[16], p_sys->i_width );
848 SetDWBE( &p_sys->p_header[20], p_sys->i_height );
849 SetDWBE( &p_sys->p_header[24], 0 );
851 if( p_sys->fd_audio > 0 )
853 p_sys->i_header_size += 20;
854 p_sys->p_header = realloc( p_sys->p_header, p_sys->i_header_size );
856 SetDWBE( &p_sys->p_header[4], 2 );
858 memcpy( &p_sys->p_header[28], "auds", 4 );
859 memcpy( &p_sys->p_header[32], "araw", 4 );
860 SetDWBE( &p_sys->p_header[36], p_sys->b_stereo ? 2 : 1 );
861 SetDWBE( &p_sys->p_header[40], p_sys->i_sample_rate );
862 SetDWBE( &p_sys->p_header[44], 16 );
867 free( p_sys->psz_video_device );
876 /*****************************************************************************
877 * V4lClose: close device
878 *****************************************************************************/
879 static void AccessClose( vlc_object_t *p_this )
881 input_thread_t *p_input = (input_thread_t *)p_this;
882 access_sys_t *p_sys = p_input->p_access_data;
884 msg_Info( p_input, "v4l grabbing stoped" );
886 free( p_sys->psz_video_device );
888 if( p_sys->p_video_mmap )
890 munmap( p_sys->p_video_mmap, p_sys->vid_mbuf.size );
892 if( p_sys->fd_audio >= 0 )
894 close( p_sys->fd_audio );
897 if( p_sys->p_encoder )
899 p_sys->p_encoder->pf_end( p_sys->p_encoder );
901 module_Unneed( p_sys->p_encoder,
902 p_sys->p_encoder->p_module );
903 vlc_object_destroy( p_sys->p_encoder );
905 free( p_sys->p_video_frame );
910 static int GrabAudio( input_thread_t * p_input,
915 access_sys_t *p_sys = p_input->p_access_data;
916 struct audio_buf_info buf_info;
920 i_read = read( p_sys->fd_audio, p_sys->p_audio_frame,
921 p_sys->i_audio_frame_size_allocated );
928 p_sys->i_audio_frame_size = i_read;
930 /* from vls : correct the date because of kernel buffering */
932 if( ioctl( p_sys->fd_audio, SNDCTL_DSP_GETISPACE, &buf_info ) == 0 )
934 i_correct += buf_info.bytes;
938 *pp_data = p_sys->p_audio_frame;
939 *pi_data = p_sys->i_audio_frame_size;
940 *pi_pts = mdate() - (mtime_t)1000000 * (mtime_t)i_correct /
941 2 / ( p_sys->b_stereo ? 2 : 1) / p_sys->i_sample_rate;
945 static int GrabVideo( input_thread_t * p_input,
950 access_sys_t *p_sys = p_input->p_access_data;
952 p_sys->vid_mmap.frame = ( p_sys->i_frame_pos + 1 ) %
953 p_sys->vid_mbuf.frames;
956 if( ioctl( p_sys->fd, VIDIOCMCAPTURE, &p_sys->vid_mmap ) >= 0 )
961 if( errno != EAGAIN )
963 msg_Err( p_input, "failed while grabbing new frame" );
966 msg_Dbg( p_input, "another try ?" );
969 //msg_Warn( p_input, "grab a new frame" );
971 while( ioctl(p_sys->fd, VIDIOCSYNC, &p_sys->i_frame_pos) < 0 &&
972 ( errno == EAGAIN || errno == EINTR ) );
975 p_sys->i_frame_pos = p_sys->vid_mmap.frame;
977 if( p_sys->p_encoder )
981 p_sys->pic.p[0].p_pixels = p_sys->p_video_mmap +
982 p_sys->vid_mbuf.offsets[p_sys->i_frame_pos];
984 for( i = 1; i < p_sys->pic.i_planes; i++ )
986 p_sys->pic.p[i].p_pixels = p_sys->pic.p[i-1].p_pixels +
987 p_sys->pic.p[i-1].i_pitch * p_sys->pic.p[i-1].i_lines;
990 p_sys->i_video_frame_size = p_sys->i_video_frame_size_allocated;
991 p_sys->p_encoder->pf_encode( p_sys->p_encoder, &p_sys->pic,
992 p_sys->p_video_frame,
993 &p_sys->i_video_frame_size );
997 p_sys->p_video_frame = p_sys->p_video_mmap +
998 p_sys->vid_mbuf.offsets[p_sys->i_frame_pos];
1001 *pp_data = p_sys->p_video_frame;
1002 *pi_data = p_sys->i_video_frame_size;
1007 /*****************************************************************************
1008 * Read: reads from the device into PES packets.
1009 *****************************************************************************
1010 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
1012 *****************************************************************************/
1013 static int Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
1015 access_sys_t *p_sys = p_input->p_access_data;
1020 //msg_Info( p_input, "access read data_size %i, data_pos %i",
1021 //p_sys->i_data_size, p_sys->i_data_pos );
1024 /* First copy header if any */
1025 if( i_len > 0 && p_sys->i_header_pos < p_sys->i_header_size )
1029 i_copy = __MIN( p_sys->i_header_size - p_sys->i_header_pos,
1031 memcpy( p_buffer, &p_sys->p_header[p_sys->i_header_pos], i_copy );
1032 p_sys->i_header_pos += i_copy;
1040 if( i_len > 0 && p_sys->i_data_pos < p_sys->i_data_size )
1044 i_copy = __MIN( p_sys->i_data_size - p_sys->i_data_pos,
1047 memcpy( p_buffer, &p_sys->p_data[p_sys->i_data_pos], i_copy );
1048 p_sys->i_data_pos += i_copy;
1055 /* The caller got what he wanted */
1061 /* Read no more than one frame at a time.
1062 * That kills latency, especially for encoded v4l streams */
1063 if( p_sys->i_data_size && p_sys->i_data_pos == p_sys->i_data_size )
1065 p_sys->i_data_pos = 0; p_sys->i_data_size = 0;
1069 /* re fill data by grabbing audio/video */
1070 p_sys->i_data_pos = 0;
1072 /* try grabbing audio frames */
1074 if( p_sys->fd_audio < 0 ||
1075 GrabAudio( p_input, &p_sys->p_data, &p_sys->i_data_size, &i_pts ) )
1077 /* and then get video frame if no audio */
1079 if( GrabVideo( p_input, &p_sys->p_data, &p_sys->i_data_size, &i_pts ) )
1085 /* create pseudo header */
1086 p_sys->i_header_size = 16;
1087 p_sys->i_header_pos = 0;
1088 SetDWBE( &p_sys->p_header[0], i_stream );
1089 SetDWBE( &p_sys->p_header[4], p_sys->i_data_size );
1090 SetQWBE( &p_sys->p_header[8], i_pts );
1096 /****************************************************************************
1098 ****************************************************************************/
1099 #define MAX_PACKETS_IN_FIFO 3
1101 static int DemuxOpen( vlc_object_t *p_this )
1103 input_thread_t *p_input = (input_thread_t *)p_this;
1104 //demux_sys_t *p_sys;
1110 data_packet_t *p_pk;
1112 /* Initialize access plug-in structures. */
1113 if( p_input->i_mtu == 0 )
1115 /* Improve speed. */
1116 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE ;
1119 /* a little test to see if it's a v4l stream */
1120 if( input_Peek( p_input, &p_peek, 8 ) < 8 )
1122 msg_Warn( p_input, "v4l plugin discarded (cannot peek)" );
1123 return( VLC_EGENERIC );
1126 if( strncmp( p_peek, ".v4l", 4 ) || GetDWBE( &p_peek[4] ) <= 0 )
1128 msg_Warn( p_input, "v4l plugin discarded (not a valid stream)" );
1129 return VLC_EGENERIC;
1132 /* create one program */
1133 vlc_mutex_lock( &p_input->stream.stream_lock );
1134 if( input_InitStream( p_input, 0 ) == -1)
1136 vlc_mutex_unlock( &p_input->stream.stream_lock );
1137 msg_Err( p_input, "cannot init stream" );
1138 return( VLC_EGENERIC );
1140 if( input_AddProgram( p_input, 0, 0) == NULL )
1142 vlc_mutex_unlock( &p_input->stream.stream_lock );
1143 msg_Err( p_input, "cannot add program" );
1144 return( VLC_EGENERIC );
1147 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
1148 p_input->stream.i_mux_rate = 0;
1150 i_streams = GetDWBE( &p_peek[4] );
1151 if( input_Peek( p_input, &p_peek, 8 + 20 * i_streams )
1152 < 8 + 20 * i_streams )
1154 msg_Err( p_input, "v4l plugin discarded (cannot peek)" );
1155 return( VLC_EGENERIC );
1159 for( i = 0; i < i_streams; i++ )
1161 es_descriptor_t *p_es;
1163 if( !strncmp( p_peek, "auds", 4 ) )
1165 #define wf ((WAVEFORMATEX*)p_es->p_waveformatex)
1166 p_es = input_AddES( p_input, p_input->stream.pp_programs[0],
1167 i + 1, AUDIO_ES, NULL, 0 );
1168 p_es->i_stream_id = i + 1;
1170 VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] );
1172 p_es->p_waveformatex= malloc( sizeof( WAVEFORMATEX ) );
1174 wf->wFormatTag = WAVE_FORMAT_UNKNOWN;
1175 wf->nChannels = GetDWBE( &p_peek[8] );
1176 wf->nSamplesPerSec = GetDWBE( &p_peek[12] );
1177 wf->wBitsPerSample = GetDWBE( &p_peek[16] );
1178 wf->nBlockAlign = wf->wBitsPerSample * wf->nChannels / 8;
1179 wf->nAvgBytesPerSec = wf->nBlockAlign * wf->nSamplesPerSec;
1182 msg_Dbg( p_input, "added new audio es %d channels %dHz",
1183 wf->nChannels, wf->nSamplesPerSec );
1185 input_SelectES( p_input, p_es );
1188 else if( !strncmp( p_peek, "vids", 4 ) )
1190 #define bih ((BITMAPINFOHEADER*)p_es->p_bitmapinfoheader)
1191 p_es = input_AddES( p_input, p_input->stream.pp_programs[0],
1192 i + 1, VIDEO_ES, NULL, 0 );
1193 p_es->i_stream_id = i + 1;
1195 VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] );
1197 p_es->p_bitmapinfoheader = malloc( sizeof( BITMAPINFOHEADER ) );
1199 bih->biSize = sizeof( BITMAPINFOHEADER );
1200 bih->biWidth = GetDWBE( &p_peek[8] );
1201 bih->biHeight = GetDWBE( &p_peek[12] );
1203 bih->biBitCount = 0;
1204 bih->biCompression = 0;
1205 bih->biSizeImage= 0;
1206 bih->biXPelsPerMeter = 0;
1207 bih->biYPelsPerMeter = 0;
1209 bih->biClrImportant = 0;
1211 msg_Dbg( p_input, "added new video es %4.4s %dx%d",
1212 (char*)&p_es->i_fourcc, bih->biWidth, bih->biHeight );
1214 input_SelectES( p_input, p_es );
1221 p_input->stream.p_selected_program->b_is_ok = 1;
1222 vlc_mutex_unlock( &p_input->stream.stream_lock );
1224 if( input_SplitBuffer( p_input, &p_pk, 8 + i_streams * 20 ) > 0 )
1226 input_DeletePacket( p_input->p_method_data, p_pk );
1229 p_input->pf_demux = Demux;
1233 static void DemuxClose( vlc_object_t *p_this )
1238 static int Demux( input_thread_t *p_input )
1240 es_descriptor_t *p_es;
1241 pes_packet_t *p_pes;
1248 if( input_Peek( p_input, &p_peek, 16 ) < 16 )
1250 msg_Warn( p_input, "cannot peek (EOF ?)" );
1254 i_stream = GetDWBE( &p_peek[0] );
1255 i_size = GetDWBE( &p_peek[4] );
1256 i_pts = GetQWBE( &p_peek[8] );
1258 //msg_Dbg( p_input, "stream=%d size=%d", i_stream, i_size );
1259 // p_es = input_FindES( p_input, i_stream );
1260 p_es = p_input->stream.p_selected_program->pp_es[i_stream];
1263 msg_Err( p_input, "cannot find ES" );
1266 if( p_es == NULL || p_es->p_decoder_fifo == NULL )
1268 msg_Err( p_input, "cannot find decoder" );
1272 p_pes = input_NewPES( p_input->p_method_data );
1275 msg_Warn( p_input, "cannot allocate PES" );
1282 data_packet_t *p_data;
1285 if( (i_read = input_SplitBuffer( p_input, &p_data,
1286 __MIN( i_size, 10000 ) ) ) <= 0 )
1288 input_DeletePES( p_input->p_method_data, p_pes );
1291 if( !p_pes->p_first )
1293 p_pes->p_first = p_data;
1294 p_pes->i_nb_data = 1;
1295 p_pes->i_pes_size = i_read;
1299 p_pes->p_last->p_next = p_data;
1301 p_pes->i_pes_size += i_read;
1303 p_pes->p_last = p_data;
1306 // input_SplitBuffer( p_input, &p_pk, i_size + 8 );
1307 p_pes->p_first->p_payload_start += 16;
1308 p_pes->i_pes_size -= 16;
1309 if( p_es && p_es->p_decoder_fifo )
1311 vlc_mutex_lock( &p_es->p_decoder_fifo->data_lock );
1312 if( p_es->p_decoder_fifo->i_depth >= MAX_PACKETS_IN_FIFO )
1314 /* Wait for the decoder. */
1315 vlc_cond_wait( &p_es->p_decoder_fifo->data_wait,
1316 &p_es->p_decoder_fifo->data_lock );
1318 vlc_mutex_unlock( &p_es->p_decoder_fifo->data_lock );
1319 //p_pes->i_pts = mdate() + p_input->i_pts_delay;
1320 p_pes->i_pts = p_pes->i_dts = i_pts + p_input->i_pts_delay;
1321 input_DecodePES( p_es->p_decoder_fifo, p_pes );
1325 input_DeletePES( p_input->p_method_data, p_pes );