1 /*****************************************************************************
2 * v4l.c : Video4Linux input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: v4l.c,v 1.5 2003/03/31 03:46:11 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 /* enable audio grabbing */
51 #include <sys/soundcard.h>
54 /*****************************************************************************
56 *****************************************************************************/
57 static int AccessOpen ( vlc_object_t * );
58 static void AccessClose ( vlc_object_t * );
59 static int Read ( input_thread_t *, byte_t *, size_t );
61 static int DemuxOpen ( vlc_object_t * );
62 static void DemuxClose ( vlc_object_t * );
63 static int Demux ( input_thread_t * );
66 /*****************************************************************************
68 *****************************************************************************/
69 #define CACHING_TEXT N_("caching value in ms")
70 #define CACHING_LONGTEXT N_( \
71 "Allows you to modify the default caching value for v4l streams. This " \
72 "value should be set in miliseconds units." )
75 set_description( _("Video4Linux input") );
76 add_category_hint( N_("v4l"), NULL, VLC_TRUE );
77 add_integer( "v4l-caching", DEFAULT_PTS_DELAY / 1000, NULL,
78 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
80 add_shortcut( "v4l" );
81 set_capability( "access", 0 );
82 set_callbacks( AccessOpen, AccessClose );
84 add_shortcut( "v4l" );
85 set_capability( "demux", 200 );
86 set_callbacks( DemuxOpen, DemuxClose );
90 /****************************************************************************
92 ****************************************************************************/
95 char *psz_video_device;
98 vlc_fourcc_t i_codec; // if i_codec != i_chroma then we need a compressor
99 video_encoder_t *p_encoder;
111 struct video_capability vid_cap;
112 struct video_mbuf vid_mbuf;
114 uint8_t *p_video_mmap;
116 struct video_mmap vid_mmap;
118 uint8_t *p_video_frame;
119 int i_video_frame_size;
120 int i_video_frame_size_allocated;
125 vlc_fourcc_t i_acodec_raw;
129 uint8_t *p_audio_frame;
130 int i_audio_frame_size;
131 int i_audio_frame_size_allocated;
138 uint8_t *p_header; // at lest 8 bytes allocated
143 uint8_t *p_data; // never allocated
151 * fcc "auds"|"vids" 0
168 static void SetDWBE( uint8_t *p, uint32_t dw )
170 p[0] = (dw >> 24)&0xff;
171 p[1] = (dw >> 16)&0xff;
172 p[2] = (dw >> 8)&0xff;
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] );
182 /*****************************************************************************
184 *****************************************************************************
186 * url: <video device>::::
188 *****************************************************************************/
189 static int AccessOpen( vlc_object_t *p_this )
191 input_thread_t *p_input = (input_thread_t *)p_this;
193 char *psz_dup, *psz_parser;
195 struct video_channel vid_channel;
197 /* create access private data */
198 p_sys = malloc( sizeof( access_sys_t ) );
199 memset( p_sys, 0, sizeof( access_sys_t ) );
201 p_sys->psz_video_device = NULL;
203 p_sys->i_channel = -1;
205 p_sys->i_norm = VIDEO_MODE_AUTO; // auto
207 p_sys->i_frequency = -1;
211 p_sys->i_frame_pos = 0;
213 p_sys->i_codec = VLC_FOURCC( 0, 0, 0, 0 );
214 p_sys->i_video_frame_size_allocated = 0;
216 p_sys->psz_adev = NULL;
217 p_sys->fd_audio = -1;
218 p_sys->i_sample_rate = 44100;
219 p_sys->b_stereo = VLC_TRUE;
222 p_sys->i_data_size = 0;
223 p_sys->i_data_pos = 0;
224 p_sys->p_data = NULL;
226 /* parse url and open device(s) */
227 psz_dup = strdup( p_input->psz_name );
228 psz_parser = psz_dup;
230 while( *psz_parser && *psz_parser != ':' )
235 if( *psz_parser == ':' )
240 *psz_parser++ = '\0';
241 if( !strncmp( psz_parser, "channel=", strlen( "channel=" ) ) )
244 strtol( psz_parser + strlen( "channel=" ), &psz_parser, 0 );
246 else if( !strncmp( psz_parser, "norm=", strlen( "norm=" ) ) )
248 psz_parser += strlen( "norm=" );
249 if( !strncmp( psz_parser, "pal", strlen( "pal" ) ) )
251 p_sys->i_norm = VIDEO_MODE_PAL;
252 psz_parser += strlen( "pal" );
254 else if( !strncmp( psz_parser, "ntsc", strlen( "ntsc" ) ) )
256 p_sys->i_norm = VIDEO_MODE_NTSC;
257 psz_parser += strlen( "ntsc" );
259 else if( !strncmp( psz_parser, "secam", strlen( "secam" ) ) )
261 p_sys->i_norm = VIDEO_MODE_SECAM;
262 psz_parser += strlen( "secam" );
264 else if( !strncmp( psz_parser, "auto", strlen( "auto" ) ) )
266 p_sys->i_norm = VIDEO_MODE_AUTO;
267 psz_parser += strlen( "auto" );
271 p_sys->i_norm = strtol( psz_parser, &psz_parser, 0 );
274 else if( !strncmp( psz_parser, "frequency=", strlen( "frequency=" ) ) )
277 strtol( psz_parser + strlen( "frequency=" ), &psz_parser, 0 );
279 else if( !strncmp( psz_parser, "audio=", strlen( "audio=" ) ) )
281 p_sys->i_audio = strtol( psz_parser + strlen( "audio=" ), &psz_parser, 0 );
283 else if( !strncmp( psz_parser, "size=", strlen( "size=" ) ) )
285 psz_parser += strlen( "size=" );
286 if( !strncmp( psz_parser, "subqcif", strlen( "subqcif" ) ) )
288 p_sys->i_width = 128;
289 p_sys->i_height = 96;
291 else if( !strncmp( psz_parser, "qsif", strlen( "qsif" ) ) )
293 p_sys->i_width = 160;
294 p_sys->i_height = 120;
296 else if( !strncmp( psz_parser, "qcif", strlen( "qcif" ) ) )
298 p_sys->i_width = 176;
299 p_sys->i_height = 144;
301 else if( !strncmp( psz_parser, "sif", strlen( "sif" ) ) )
303 p_sys->i_width = 320;
304 p_sys->i_height = 244;
306 else if( !strncmp( psz_parser, "cif", strlen( "cif" ) ) )
308 p_sys->i_width = 352;
309 p_sys->i_height = 288;
311 else if( !strncmp( psz_parser, "vga", strlen( "vga" ) ) )
313 p_sys->i_width = 640;
314 p_sys->i_height = 480;
319 p_sys->i_width = strtol( psz_parser, &psz_parser, 0 );
320 if( *psz_parser == 'x' || *psz_parser == 'X')
322 p_sys->i_height = strtol( psz_parser + 1, &psz_parser, 0 );
324 msg_Dbg( p_input, "WxH %dx%d", p_sys->i_width, p_sys->i_height );
327 else if( !strncmp( psz_parser, "tuner=", strlen( "tuner=" ) ) )
329 p_sys->i_tuner = strtol( psz_parser + strlen( "tuner=" ), &psz_parser, 0 );
331 else if( !strncmp( psz_parser, "codec=", strlen( "codec=" ) ) )
333 psz_parser += strlen( "codec=" );
334 if( !strncmp( psz_parser, "mpeg4", strlen( "mpeg4" ) ) )
336 p_sys->i_codec = VLC_FOURCC( 'm', 'p', '4', 'v' );
338 else if( !strncmp( psz_parser, "mpeg1", strlen( "mpeg1" ) ) )
340 p_sys->i_codec = VLC_FOURCC( 'm', 'p', '1', 'v' );
344 msg_Warn( p_input, "unknow codec" );
348 else if( !strncmp( psz_parser, "adev=", strlen( "adev=" ) ) )
352 psz_parser += strlen( "adev=" );
353 if( strchr( psz_parser, ':' ) )
355 i_len = strchr( psz_parser, ':' ) - psz_parser;
359 i_len = strlen( psz_parser );
362 p_sys->psz_adev = strndup( psz_parser, i_len );
366 else if( !strncmp( psz_parser, "samplerate=", strlen( "samplerate=" ) ) )
368 p_sys->i_sample_rate = strtol( psz_parser + strlen( "samplerate=" ), &psz_parser, 0 );
370 else if( !strncmp( psz_parser, "stereo", strlen( "stereo" ) ) )
372 psz_parser += strlen( "stereo" );
374 p_sys->b_stereo = VLC_TRUE;
376 else if( !strncmp( psz_parser, "mono", strlen( "mono" ) ) )
378 psz_parser += strlen( "mono" );
380 p_sys->b_stereo = VLC_FALSE;
385 msg_Warn( p_input, "unknow option" );
388 while( *psz_parser && *psz_parser != ':' )
393 if( *psz_parser == '\0' )
402 p_sys->psz_video_device = strdup( psz_dup );
406 p_sys->psz_video_device = strdup( "/dev/video" );
408 msg_Dbg( p_input, "video device=`%s'", p_sys->psz_video_device );
411 if( p_sys->psz_adev && *p_sys->psz_adev == '\0' )
413 p_sys->psz_adev = strdup( "/dev/dsp" );
415 msg_Dbg( p_input, "audio device=`%s'", p_sys->psz_adev );
420 if( ( p_sys->fd = open( p_sys->psz_video_device, O_RDWR ) ) < 0 )
422 msg_Err( p_input, "cannot open device" );
426 if( ioctl( p_sys->fd, VIDIOCGCAP, &p_sys->vid_cap ) < 0 )
428 msg_Err( p_input, "cannot get capabilities" );
433 "V4L device %s %d channels %d audios %d < w < %d %d < h < %d",
435 p_sys->vid_cap.channels,
436 p_sys->vid_cap.audios,
437 p_sys->vid_cap.minwidth, p_sys->vid_cap.maxwidth,
438 p_sys->vid_cap.minheight, p_sys->vid_cap.maxheight );
440 if( p_sys->i_channel < 0 || p_sys->i_channel >= p_sys->vid_cap.channels )
442 msg_Dbg( p_input, "invalid channel, falling back on channel 0" );
443 p_sys->i_channel = 0;
445 if( p_sys->i_audio >= p_sys->vid_cap.audios )
447 msg_Dbg( p_input, "invalid audio, falling back with no audio" );
451 if( p_sys->i_width < p_sys->vid_cap.minwidth ||
452 p_sys->i_width > p_sys->vid_cap.maxwidth )
454 msg_Dbg( p_input, "invalid width" );
457 if( p_sys->i_height < p_sys->vid_cap.minheight ||
458 p_sys->i_height > p_sys->vid_cap.maxheight )
460 msg_Dbg( p_input, "invalid height" );
464 if( !( p_sys->vid_cap.type&VID_TYPE_CAPTURE ) )
466 msg_Err( p_input, "cannot grab" );
470 vid_channel.channel = p_sys->i_channel;
471 if( ioctl( p_sys->fd, VIDIOCGCHAN, &vid_channel ) < 0 )
473 msg_Err( p_input, "cannot get channel infos" );
477 "setting channel %s(%d) %d tuners flags=0x%x type=0x%x norm=0x%x",
485 if( p_sys->i_tuner >= vid_channel.tuners )
487 msg_Dbg( p_input, "invalid tuner, falling back on tuner 0" );
491 vid_channel.norm = p_sys->i_norm;
492 if( ioctl( p_sys->fd, VIDIOCSCHAN, &vid_channel ) < 0 )
494 msg_Err( p_input, "cannot set channel" );
498 if( vid_channel.flags & VIDEO_VC_TUNER )
503 struct video_tuner vid_tuner;
504 if( p_sys->i_tuner >= 0 )
506 vid_tuner.tuner = p_sys->i_tuner;
507 if( ioctl( p_sys->fd, VIDIOCGTUNER, &vid_tuner ) < 0 )
509 msg_Err( p_input, "cannot get tuner" );
513 "tuner %s low=%d high=%d, flags=0x%x mode=0x%x signal=0x%x",
515 vid_tuner.rangelow, vid_tuner.rangehigh,
521 "setting tuner %s (%d)",
522 vid_tuner.name, vid_tuner.tuner );
524 //vid_tuner.mode = p_sys->i_norm; /* FIXME FIXME to be checked FIXME FIXME */
525 if( ioctl( p_sys->fd, VIDIOCSTUNER, &vid_tuner ) < 0 )
527 msg_Err( p_input, "cannot set tuner" );
533 if( p_sys->i_frequency >= 0 )
535 if( ioctl( p_sys->fd, VIDIOCSFREQ, &p_sys->i_frequency ) < 0 )
537 msg_Err( p_input, "cannot set frequency" );
540 msg_Dbg( p_input, "frequency %d", p_sys->i_frequency );
545 if( vid_channel.flags & VIDEO_VC_AUDIO )
547 struct video_audio vid_audio;
549 /* XXX TODO volume, balance, ... */
550 if( p_sys->i_audio >= 0 )
552 vid_audio.audio = p_sys->i_audio;
553 if( ioctl( p_sys->fd, VIDIOCGAUDIO, &vid_audio ) < 0 )
555 msg_Err( p_input, "cannot get audio" );
560 vid_audio.flags &= ~VIDEO_AUDIO_MUTE;
562 if( ioctl( p_sys->fd, VIDIOCSAUDIO, &vid_audio ) < 0 )
564 msg_Err( p_input, "cannot set audio" );
569 if( p_sys->psz_adev )
572 if( ( p_sys->fd_audio = open( p_sys->psz_adev, O_RDONLY ) ) < 0 )
574 msg_Err( p_input, "cannot open audio device" );
578 i_format = AFMT_S16_LE;
579 if( ioctl( p_sys->fd_audio, SNDCTL_DSP_SETFMT, &i_format ) < 0 ||
580 i_format != AFMT_S16_LE )
582 msg_Err( p_input, "cannot set audio format (16b little endian)" );
586 if( ioctl( p_sys->fd_audio, SNDCTL_DSP_STEREO, &p_sys->b_stereo ) < 0 )
588 msg_Err( p_input, "cannot set audio channels count" );
592 if( ioctl( p_sys->fd_audio, SNDCTL_DSP_SPEED, &p_sys->i_sample_rate ) < 0 )
594 msg_Err( p_input, "cannot set audio sample rate" );
601 p_sys->b_stereo ? "stereo" : "mono",
602 p_sys->i_sample_rate );
604 p_sys->i_audio_frame_size = 0;
605 p_sys->i_audio_frame_size_allocated = 10*1024;
606 p_sys->p_audio_frame = malloc( p_sys->i_audio_frame_size_allocated );
612 /* fix width/heigh */
613 if( p_sys->i_width == 0 || p_sys->i_height == 0 )
615 struct video_window vid_win;
617 if( ioctl( p_sys->fd, VIDIOCGWIN, &vid_win ) < 0 )
619 msg_Err( p_input, "cannot get win" );
622 p_sys->i_width = vid_win.width;
623 p_sys->i_height = vid_win.height;
625 msg_Dbg( p_input, "will use %dx%d", p_sys->i_width, p_sys->i_height );
628 if( ioctl( p_sys->fd, VIDIOCGMBUF, &p_sys->vid_mbuf ) < 0 )
630 msg_Err( p_input, "mmap unsupported" );
634 p_sys->p_video_mmap = mmap( 0, p_sys->vid_mbuf.size,
635 PROT_READ|PROT_WRITE,
639 if( p_sys->p_video_mmap == MAP_FAILED )
641 /* FIXME -> normal read */
642 msg_Err( p_input, "mmap failed" );
646 p_sys->p_video_frame = NULL;
649 p_sys->vid_mmap.frame = 0;
650 p_sys->vid_mmap.width = p_sys->i_width;
651 p_sys->vid_mmap.height = p_sys->i_height;
652 p_sys->vid_mmap.format = VIDEO_PALETTE_YUV420P;
653 p_sys->i_chroma = VLC_FOURCC( 'I', '4', '2', '0' );
654 if( ioctl( p_sys->fd, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 )
656 msg_Warn( p_input, "%4.4s refused", (char*)&p_sys->i_chroma );
658 p_sys->vid_mmap.format = VIDEO_PALETTE_YUV422P;
659 p_sys->i_chroma = VLC_FOURCC( 'I', '4', '2', '2' );
661 if( ioctl( p_sys->fd, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 )
663 msg_Warn( p_input, "%4.4s refused", (char*)&p_sys->i_chroma );
665 msg_Err( p_input, "chroma selection failed" );
670 p_sys->i_video_frame_size = p_sys->i_width * p_sys->i_height * 2;
675 p_sys->i_video_frame_size = p_sys->i_width * p_sys->i_height * 3 / 2;
678 if( p_sys->i_codec != VLC_FOURCC( 0, 0, 0, 0 ) )
681 "need a rencoder from %4.4s to %4.4s",
682 (char*)&p_sys->i_chroma,
683 (char*)&p_sys->i_codec );
684 #define p_enc p_sys->p_encoder
685 p_enc = vlc_object_create( p_input, sizeof( video_encoder_t ) );
686 p_enc->i_codec = p_sys->i_codec;
687 p_enc->i_chroma= p_sys->i_chroma;
688 p_enc->i_width = p_sys->i_width;
689 p_enc->i_height= p_sys->i_height;
693 p_enc->p_module = module_Need( p_enc,
696 if( !p_enc->p_module )
699 "no suitable encoder to %4.4s",
700 (char*)&p_enc->i_codec );
701 vlc_object_destroy( p_enc );
705 /* *** init the codec *** */
706 if( p_enc->pf_init( p_enc ) )
708 msg_Err( p_input, "failed to initialize video encoder plugin" );
709 vlc_object_destroy( p_enc );
713 /* *** alloacted buffer *** */
714 if( p_enc->i_buffer_size <= 0 )
716 p_enc->i_buffer_size = 10 * p_enc->i_width * p_enc->i_height;
718 p_sys->i_video_frame_size = p_enc->i_buffer_size;
719 p_sys->i_video_frame_size_allocated = p_enc->i_buffer_size;
720 if( !( p_sys->p_video_frame = malloc( p_enc->i_buffer_size ) ) )
722 msg_Err( p_input, "out of memory" );
726 switch( p_sys->i_chroma )
728 case VLC_FOURCC( 'I', '4', '2', '0' ):
729 p_sys->pic.i_planes = 3;
730 p_sys->pic.p[0].i_pitch = p_sys->i_width;
731 p_sys->pic.p[0].i_lines = p_sys->i_height;
732 p_sys->pic.p[0].i_pixel_pitch = 1;
733 p_sys->pic.p[0].i_visible_pitch = p_sys->i_width;
735 p_sys->pic.p[1].i_pitch = p_sys->i_width / 2;
736 p_sys->pic.p[1].i_lines = p_sys->i_height / 2;
737 p_sys->pic.p[1].i_pixel_pitch = 1;
738 p_sys->pic.p[1].i_visible_pitch = p_sys->i_width / 2;
740 p_sys->pic.p[2].i_pitch = p_sys->i_width / 2;
741 p_sys->pic.p[2].i_lines = p_sys->i_height / 2;
742 p_sys->pic.p[2].i_pixel_pitch = 1;
743 p_sys->pic.p[2].i_visible_pitch = p_sys->i_width / 2;
746 msg_Err( p_input, "unsuported chroma" );
747 vlc_object_destroy( p_enc );
754 p_sys->i_codec = p_sys->i_chroma;
755 p_sys->p_encoder = NULL;
759 p_input->pf_read = Read;
760 p_input->pf_seek = NULL;
761 p_input->pf_set_area = NULL;
762 p_input->pf_set_program = NULL;
764 p_input->p_access_data = p_sys;
766 vlc_mutex_lock( &p_input->stream.stream_lock );
767 p_input->stream.b_pace_control = 0;
768 p_input->stream.b_seekable = 0;
769 p_input->stream.p_selected_area->i_size = 0;
770 p_input->stream.p_selected_area->i_tell = 0;
771 p_input->stream.i_method = INPUT_METHOD_FILE;
772 vlc_mutex_unlock( &p_input->stream.stream_lock );
774 /* Update default_pts to a suitable value for access */
775 p_input->i_pts_delay = config_GetInt( p_input, "v4l-caching" ) * 1000;
777 msg_Info( p_input, "v4l grabbing started" );
780 p_sys->i_header_size = 8 + 20;
781 p_sys->i_header_pos = 0;
782 p_sys->p_header = malloc( p_sys->i_header_size );
784 memcpy( &p_sys->p_header[0], ".v4l", 4 );
785 SetDWBE( &p_sys->p_header[4], 1 );
787 memcpy( &p_sys->p_header[ 8], "vids", 4 );
788 memcpy( &p_sys->p_header[12], &p_sys->i_codec, 4 );
789 SetDWBE( &p_sys->p_header[16], p_sys->i_width );
790 SetDWBE( &p_sys->p_header[20], p_sys->i_height );
791 SetDWBE( &p_sys->p_header[24], 0 );
794 if( p_sys->fd_audio > 0 )
796 p_sys->i_header_size += 20;
797 p_sys->p_header = realloc( p_sys->p_header, p_sys->i_header_size );
799 SetDWBE( &p_sys->p_header[4], 2 );
801 memcpy( &p_sys->p_header[28], "auds", 4 );
802 memcpy( &p_sys->p_header[32], "araw", 4 );
803 SetDWBE( &p_sys->p_header[36], p_sys->b_stereo ? 2 : 1 );
804 SetDWBE( &p_sys->p_header[40], p_sys->i_sample_rate );
805 SetDWBE( &p_sys->p_header[44], 16 );
811 free( p_sys->psz_video_device );
820 /*****************************************************************************
821 * V4lClose: close device
822 *****************************************************************************/
823 static void AccessClose( vlc_object_t *p_this )
825 input_thread_t *p_input = (input_thread_t *)p_this;
826 access_sys_t *p_sys = p_input->p_access_data;
828 msg_Info( p_input, "v4l grabbing stoped" );
830 free( p_sys->psz_video_device );
833 if( p_sys->p_encoder )
835 p_sys->p_encoder->pf_end( p_sys->p_encoder );
837 module_Unneed( p_sys->p_encoder,
838 p_sys->p_encoder->p_module );
839 vlc_object_destroy( p_sys->p_encoder );
841 free( p_sys->p_video_frame );
847 static int GrabAudio( input_thread_t * p_input,
851 access_sys_t *p_sys = p_input->p_access_data;
853 struct timeval timeout;
857 /* we first try to get an audio frame */
859 FD_SET( p_sys->fd_audio, &fds );
864 i_ret = select( p_sys->fd_audio + 1, &fds, NULL, NULL, &timeout );
866 if( i_ret < 0 && errno != EINTR )
868 msg_Warn( p_input, "audio select failed" );
872 if( !FD_ISSET( p_sys->fd_audio , &fds) )
877 i_read = read( p_sys->fd_audio, p_sys->p_audio_frame, p_sys->i_audio_frame_size_allocated );
884 p_sys->i_audio_frame_size = i_read;
887 *pp_data = p_sys->p_audio_frame;
888 *pi_data = p_sys->i_audio_frame_size;
893 static int GrabVideo( input_thread_t * p_input,
897 access_sys_t *p_sys = p_input->p_access_data;
899 p_sys->vid_mmap.frame = ( p_sys->i_frame_pos + 1 ) % p_sys->vid_mbuf.frames;
902 if( ioctl( p_sys->fd, VIDIOCMCAPTURE, &p_sys->vid_mmap ) >= 0 )
907 if( errno != EAGAIN )
909 msg_Err( p_input, "failed while grabbing new frame" );
912 msg_Dbg( p_input, "another try ?" );
915 //msg_Warn( p_input, "grab a new frame" );
917 while( ioctl(p_sys->fd, VIDIOCSYNC, &p_sys->i_frame_pos) < 0 &&
918 ( errno == EAGAIN || errno == EINTR ) );
921 p_sys->i_frame_pos = p_sys->vid_mmap.frame;
923 if( p_sys->p_encoder )
925 p_sys->pic.p[0].p_pixels = p_sys->p_video_mmap + p_sys->vid_mbuf.offsets[p_sys->i_frame_pos];
926 p_sys->pic.p[1].p_pixels = p_sys->pic.p[0].p_pixels +
927 p_sys->pic.p[0].i_pitch * p_sys->pic.p[0].i_lines;
928 p_sys->pic.p[2].p_pixels = p_sys->pic.p[1].p_pixels +
929 p_sys->pic.p[1].i_pitch * p_sys->pic.p[1].i_lines;
931 p_sys->i_video_frame_size = p_sys->i_video_frame_size_allocated;
932 p_sys->p_encoder->pf_encode( p_sys->p_encoder,
934 p_sys->p_video_frame,
935 &p_sys->i_video_frame_size );
939 p_sys->p_video_frame = p_sys->p_video_mmap + p_sys->vid_mbuf.offsets[p_sys->i_frame_pos];
942 *pp_data = p_sys->p_video_frame;
943 *pi_data = p_sys->i_video_frame_size;
947 /*****************************************************************************
948 * Read: reads from the device into PES packets.
949 *****************************************************************************
950 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
952 *****************************************************************************/
953 static int Read( input_thread_t * p_input, byte_t * p_buffer,
956 access_sys_t *p_sys = p_input->p_access_data;
960 msg_Info( p_input, "access read" );
964 /* First copy header if any */
965 if( i_len > 0 && p_sys->i_header_pos < p_sys->i_header_size )
969 i_copy = __MIN( p_sys->i_header_size - p_sys->i_header_pos,
972 &p_sys->p_header[p_sys->i_header_pos],
974 p_sys->i_header_pos += i_copy;
982 if( i_len > 0 && p_sys->i_data_pos < p_sys->i_data_size )
986 i_copy = __MIN( p_sys->i_data_size - p_sys->i_data_pos,
990 &p_sys->p_data[p_sys->i_data_pos],
992 p_sys->i_data_pos += i_copy;
1004 /* re fill data by grabbing audio/video */
1005 p_sys->i_data_pos = 0;
1007 /* try grabbing audio frames */
1010 if( p_sys->fd_audio < 0 || GrabAudio( p_input, &p_sys->p_data, &p_sys->i_data_size ) )
1012 /* and then get video frame if no audio */
1014 if( GrabVideo( p_input, &p_sys->p_data, &p_sys->i_data_size ) )
1020 /* and then get video frame if no audio */
1022 if( GrabVideo( p_input, &p_sys->p_data, &p_sys->i_data_size ) )
1028 /* create pseudo header */
1029 p_sys->i_header_size = 8;
1030 p_sys->i_header_pos = 0;
1031 SetDWBE( &p_sys->p_header[0], i_stream );
1032 SetDWBE( &p_sys->p_header[4], p_sys->i_data_size );
1038 /****************************************************************************
1040 ****************************************************************************/
1041 #define MAX_PACKETS_IN_FIFO 3
1043 static int DemuxOpen ( vlc_object_t *p_this )
1045 input_thread_t *p_input = (input_thread_t *)p_this;
1046 //demux_sys_t *p_sys;
1052 data_packet_t *p_pk;
1054 /* Initialize access plug-in structures. */
1055 if( p_input->i_mtu == 0 )
1057 /* Improve speed. */
1058 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE ;
1061 /* a little test to see if it's a v4l stream */
1062 if( input_Peek( p_input, &p_peek, 8 ) < 8 )
1064 msg_Warn( p_input, "v4l plugin discarded (cannot peek)" );
1065 return( VLC_EGENERIC );
1068 if( strncmp( p_peek, ".v4l", 4 ) || GetDWBE( &p_peek[4] ) <= 0 )
1070 msg_Warn( p_input, "v4l plugin discarded (not a valid stream)" );
1071 return VLC_EGENERIC;
1074 /* create one program */
1075 vlc_mutex_lock( &p_input->stream.stream_lock );
1076 if( input_InitStream( p_input, 0 ) == -1)
1078 vlc_mutex_unlock( &p_input->stream.stream_lock );
1079 msg_Err( p_input, "cannot init stream" );
1080 return( VLC_EGENERIC );
1082 if( input_AddProgram( p_input, 0, 0) == NULL )
1084 vlc_mutex_unlock( &p_input->stream.stream_lock );
1085 msg_Err( p_input, "cannot add program" );
1086 return( VLC_EGENERIC );
1089 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
1090 p_input->stream.i_mux_rate = 0;
1092 i_streams = GetDWBE( &p_peek[4] );
1093 if( input_Peek( p_input, &p_peek, 8 + 20 * i_streams ) < 8 + 20 * i_streams )
1095 msg_Err( p_input, "v4l plugin discarded (cannot peek)" );
1096 return( VLC_EGENERIC );
1100 for( i = 0; i < i_streams; i++ )
1102 es_descriptor_t *p_es;
1105 p_es = input_AddES( p_input, p_input->stream.pp_programs[0], i + 1, 0 );
1106 p_es->i_stream_id = i + 1;
1107 if( !strncmp( p_peek, "auds", 4 ) )
1109 #define wf ((WAVEFORMATEX*)p_es->p_waveformatex)
1110 p_es->i_cat = AUDIO_ES;
1111 p_es->i_fourcc = VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] );
1112 p_es->p_waveformatex= malloc( sizeof( WAVEFORMATEX ) );
1114 wf->wFormatTag = WAVE_FORMAT_UNKNOWN;
1115 wf->nChannels = GetDWBE( &p_peek[8] );
1116 wf->nSamplesPerSec = GetDWBE( &p_peek[12] );
1117 wf->wBitsPerSample = GetDWBE( &p_peek[16] );
1118 wf->nBlockAlign = wf->wBitsPerSample * wf->nChannels / 8;
1119 wf->nAvgBytesPerSec = wf->nBlockAlign * wf->nSamplesPerSec;
1122 msg_Dbg( p_input, "added new audio es %d channels %dHz",
1124 wf->nSamplesPerSec );
1126 input_SelectES( p_input, p_es );
1129 else if( !strncmp( p_peek, "vids", 4 ) )
1131 #define bih ((BITMAPINFOHEADER*)p_es->p_bitmapinfoheader)
1132 p_es->i_cat = VIDEO_ES;
1133 p_es->i_fourcc = VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] );
1135 p_es->p_bitmapinfoheader = malloc( sizeof( BITMAPINFOHEADER ) );
1137 bih->biSize = sizeof( BITMAPINFOHEADER );
1138 bih->biWidth = GetDWBE( &p_peek[8] );
1139 bih->biHeight = GetDWBE( &p_peek[12] );
1141 bih->biBitCount = 0;
1142 bih->biCompression = 0;
1143 bih->biSizeImage= 0;
1144 bih->biXPelsPerMeter = 0;
1145 bih->biYPelsPerMeter = 0;
1147 bih->biClrImportant = 0;
1149 msg_Dbg( p_input, "added new video es %4.4s %dx%d",
1150 (char*)&p_es->i_fourcc,
1154 input_SelectES( p_input, p_es );
1161 p_input->stream.p_selected_program->b_is_ok = 1;
1162 vlc_mutex_unlock( &p_input->stream.stream_lock );
1164 if( input_SplitBuffer( p_input, &p_pk, 8 + i_streams * 20 ) > 0 )
1166 input_DeletePacket( p_input->p_method_data, p_pk );
1169 p_input->pf_demux = Demux;
1173 static void DemuxClose ( vlc_object_t *p_this )
1179 static int Demux ( input_thread_t *p_input )
1181 es_descriptor_t *p_es;
1182 pes_packet_t *p_pes;
1188 if( input_Peek( p_input, &p_peek, 8 ) < 8 )
1190 msg_Warn( p_input, "cannot peek (EOF ?)" );
1194 i_stream = GetDWBE( &p_peek[0] );
1195 i_size = GetDWBE( &p_peek[4] );
1197 msg_Dbg( p_input, "stream=%d size=%d", i_stream, i_size );
1198 // p_es = input_FindES( p_input, i_stream );
1199 p_es = p_input->stream.p_selected_program->pp_es[i_stream];
1201 if( p_es == NULL || p_es->p_decoder_fifo == NULL )
1203 msg_Err( p_input, "cannot find decoder" );
1207 p_pes = input_NewPES( p_input->p_method_data );
1210 msg_Warn( p_input, "cannot allocate PES" );
1217 data_packet_t *p_data;
1220 if( (i_read = input_SplitBuffer( p_input,
1222 __MIN( i_size, 10000 ) ) ) <= 0 )
1224 input_DeletePES( p_input->p_method_data, p_pes );
1227 if( !p_pes->p_first )
1229 p_pes->p_first = p_data;
1230 p_pes->i_nb_data = 1;
1231 p_pes->i_pes_size = i_read;
1235 p_pes->p_last->p_next = p_data;
1237 p_pes->i_pes_size += i_read;
1239 p_pes->p_last = p_data;
1242 // input_SplitBuffer( p_input, &p_pk, i_size + 8 );
1243 p_pes->p_first->p_payload_start += 8;
1245 vlc_mutex_lock( &p_es->p_decoder_fifo->data_lock );
1246 if( p_es->p_decoder_fifo->i_depth >= MAX_PACKETS_IN_FIFO )
1248 /* Wait for the decoder. */
1249 vlc_cond_wait( &p_es->p_decoder_fifo->data_wait, &p_es->p_decoder_fifo->data_lock );
1251 vlc_mutex_unlock( &p_es->p_decoder_fifo->data_lock );
1253 p_pes->i_pts = mdate() + p_input->i_pts_delay;
1255 input_DecodePES( p_es->p_decoder_fifo, p_pes );