1 /*****************************************************************************
2 * v4l.c : Video4Linux input module for vlc
3 *****************************************************************************
4 * Copyright (C) 2002 VideoLAN
5 * $Id: v4l.c,v 1.26 2003/10/25 00:49:13 sam Exp $
7 * Author: Laurent Aimar <fenrir@via.ecp.fr>
8 * Paul Forgey <paulf at aphrodite dot com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
23 *****************************************************************************/
25 /*****************************************************************************
27 *****************************************************************************/
33 #include <vlc/input.h>
37 #include <sys/types.h>
39 #include <sys/ioctl.h>
45 #include <linux/videodev.h>
46 #include "videodev_mjpeg.h"
48 #include <sys/soundcard.h>
50 /*****************************************************************************
52 *****************************************************************************/
53 static int AccessOpen ( vlc_object_t * );
54 static void AccessClose ( vlc_object_t * );
55 static int Read ( input_thread_t *, byte_t *, size_t );
57 static int DemuxOpen ( vlc_object_t * );
58 static void DemuxClose ( vlc_object_t * );
59 static int Demux ( input_thread_t * );
62 /*****************************************************************************
64 *****************************************************************************/
65 #define CACHING_TEXT N_("Caching value in ms")
66 #define CACHING_LONGTEXT N_( \
67 "Allows you to modify the default caching value for v4l streams. This " \
68 "value should be set in miliseconds units." )
71 set_description( _("Video4Linux input") );
72 add_category_hint( N_("v4l"), NULL, VLC_TRUE );
73 add_integer( "v4l-caching", DEFAULT_PTS_DELAY / 1000, NULL,
74 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
75 add_shortcut( "v4l" );
76 set_capability( "access", 10 );
77 set_callbacks( AccessOpen, AccessClose );
80 set_description( _("Video4Linux demuxer") );
81 add_shortcut( "v4l" );
82 set_capability( "demux", 200 );
83 set_callbacks( DemuxOpen, DemuxClose );
87 /****************************************************************************
89 ****************************************************************************/
90 #define MJPEG_BUFFER_SIZE (256*1024)
92 struct quicktime_mjpeg_app1
94 uint32_t i_reserved; /* set to 0 */
95 uint32_t i_tag; /* 'mjpg' */
96 uint32_t i_field_size; /* offset following EOI */
97 uint32_t i_padded_field_size; /* offset following EOI+pad */
98 uint32_t i_next_field; /* offset to next field */
99 uint32_t i_DQT_offset;
100 uint32_t i_DHT_offset;
101 uint32_t i_SOF_offset;
102 uint32_t i_SOS_offset;
103 uint32_t i_data_offset; /* following SOS marker data */
108 char *psz_video_device;
126 struct video_capability vid_cap;
127 struct video_mbuf vid_mbuf;
128 struct mjpeg_requestbuffers mjpeg_buffers;
130 uint8_t *p_video_mmap;
133 struct video_mmap vid_mmap;
134 struct video_picture vid_picture;
136 uint8_t *p_video_frame;
137 int i_video_frame_size;
138 int i_video_frame_size_allocated;
142 vlc_fourcc_t i_acodec_raw;
146 uint8_t *p_audio_frame;
147 int i_audio_frame_size;
148 int i_audio_frame_size_allocated;
153 uint8_t *p_header; // at lest 8 bytes allocated
158 uint8_t *p_data; // never allocated
166 * fcc "auds"|"vids" 0
183 static void SetDWBE( uint8_t *p, uint32_t dw )
185 p[0] = (dw >> 24)&0xff;
186 p[1] = (dw >> 16)&0xff;
187 p[2] = (dw >> 8)&0xff;
191 static void SetQWBE( uint8_t *p, uint64_t qw )
193 SetDWBE( p, (qw >> 32)&0xffffffff );
194 SetDWBE( &p[4], qw&0xffffffff);
197 /*****************************************************************************
199 *****************************************************************************
201 * url: <video device>::::
203 *****************************************************************************/
204 static int AccessOpen( vlc_object_t *p_this )
206 input_thread_t *p_input = (input_thread_t *)p_this;
208 char *psz_dup, *psz_parser;
209 struct mjpeg_params mjpeg;
212 struct video_channel vid_channel;
214 /* create access private data */
215 p_sys = malloc( sizeof( access_sys_t ) );
216 memset( p_sys, 0, sizeof( access_sys_t ) );
218 p_sys->psz_video_device = NULL;
220 p_sys->i_channel = -1;
222 p_sys->i_norm = VIDEO_MODE_AUTO; // auto
224 p_sys->i_frequency = -1;
228 p_sys->b_mjpeg = VLC_FALSE;
229 p_sys->i_decimation = 1;
230 p_sys->i_quality = 100;
232 p_sys->i_frame_pos = 0;
234 p_sys->i_video_frame_size_allocated = 0;
235 p_sys->psz_adev = NULL;
236 p_sys->fd_audio = -1;
237 p_sys->i_sample_rate = 44100;
238 p_sys->b_stereo = VLC_TRUE;
240 p_sys->i_data_size = 0;
241 p_sys->i_data_pos = 0;
242 p_sys->p_data = NULL;
244 /* parse url and open device(s) */
245 psz_dup = strdup( p_input->psz_name );
246 psz_parser = psz_dup;
248 while( *psz_parser && *psz_parser != ':' )
253 if( *psz_parser == ':' )
258 *psz_parser++ = '\0';
259 if( !strncmp( psz_parser, "channel=", strlen( "channel=" ) ) )
261 p_sys->i_channel = strtol( psz_parser + strlen( "channel=" ),
264 else if( !strncmp( psz_parser, "norm=", strlen( "norm=" ) ) )
266 psz_parser += strlen( "norm=" );
267 if( !strncmp( psz_parser, "pal", strlen( "pal" ) ) )
269 p_sys->i_norm = VIDEO_MODE_PAL;
270 psz_parser += strlen( "pal" );
272 else if( !strncmp( psz_parser, "ntsc", strlen( "ntsc" ) ) )
274 p_sys->i_norm = VIDEO_MODE_NTSC;
275 psz_parser += strlen( "ntsc" );
277 else if( !strncmp( psz_parser, "secam", strlen( "secam" ) ) )
279 p_sys->i_norm = VIDEO_MODE_SECAM;
280 psz_parser += strlen( "secam" );
282 else if( !strncmp( psz_parser, "auto", strlen( "auto" ) ) )
284 p_sys->i_norm = VIDEO_MODE_AUTO;
285 psz_parser += strlen( "auto" );
289 p_sys->i_norm = strtol( psz_parser, &psz_parser, 0 );
292 else if( !strncmp( psz_parser, "frequency=",
293 strlen( "frequency=" ) ) )
296 strtol( psz_parser + strlen( "frequency=" ),
298 if( p_sys->i_frequency < 30000 )
300 msg_Warn( p_input, "v4l syntax has changed : 'frequency' is now channel frequency in kHz");
303 else if( !strncmp( psz_parser, "audio=", strlen( "audio=" ) ) )
305 p_sys->i_audio = strtol( psz_parser + strlen( "audio=" ),
308 else if( !strncmp( psz_parser, "size=", strlen( "size=" ) ) )
310 psz_parser += strlen( "size=" );
311 if( !strncmp( psz_parser, "subqcif", strlen( "subqcif" ) ) )
313 p_sys->i_width = 128;
314 p_sys->i_height = 96;
316 else if( !strncmp( psz_parser, "qsif", strlen( "qsif" ) ) )
318 p_sys->i_width = 160;
319 p_sys->i_height = 120;
321 else if( !strncmp( psz_parser, "qcif", strlen( "qcif" ) ) )
323 p_sys->i_width = 176;
324 p_sys->i_height = 144;
326 else if( !strncmp( psz_parser, "sif", strlen( "sif" ) ) )
328 p_sys->i_width = 320;
329 p_sys->i_height = 244;
331 else if( !strncmp( psz_parser, "cif", strlen( "cif" ) ) )
333 p_sys->i_width = 352;
334 p_sys->i_height = 288;
336 else if( !strncmp( psz_parser, "vga", strlen( "vga" ) ) )
338 p_sys->i_width = 640;
339 p_sys->i_height = 480;
344 p_sys->i_width = strtol( psz_parser, &psz_parser, 0 );
345 if( *psz_parser == 'x' || *psz_parser == 'X')
347 p_sys->i_height = strtol( psz_parser + 1,
350 msg_Dbg( p_input, "WxH %dx%d", p_sys->i_width,
354 else if( !strncmp( psz_parser, "tuner=", strlen( "tuner=" ) ) )
356 p_sys->i_tuner = strtol( psz_parser + strlen( "tuner=" ),
359 else if( !strncmp( psz_parser, "adev=", strlen( "adev=" ) ) )
363 psz_parser += strlen( "adev=" );
364 if( strchr( psz_parser, ':' ) )
366 i_len = strchr( psz_parser, ':' ) - psz_parser;
370 i_len = strlen( psz_parser );
373 p_sys->psz_adev = strndup( psz_parser, i_len );
377 else if( !strncmp( psz_parser, "samplerate=",
378 strlen( "samplerate=" ) ) )
380 p_sys->i_sample_rate =
381 strtol( psz_parser + strlen( "samplerate=" ),
384 else if( !strncmp( psz_parser, "stereo", strlen( "stereo" ) ) )
386 psz_parser += strlen( "stereo" );
388 p_sys->b_stereo = VLC_TRUE;
390 else if( !strncmp( psz_parser, "mono", strlen( "mono" ) ) )
392 psz_parser += strlen( "mono" );
394 p_sys->b_stereo = VLC_FALSE;
396 else if( !strncmp( psz_parser, "mjpeg", strlen( "mjpeg" ) ) )
398 psz_parser += strlen( "mjpeg" );
400 p_sys->b_mjpeg = VLC_TRUE;
402 else if( !strncmp( psz_parser, "decimation=",
403 strlen( "decimation=" ) ) )
405 p_sys->i_decimation =
406 strtol( psz_parser + strlen( "decimation=" ),
409 else if( !strncmp( psz_parser, "quality=",
410 strlen( "quality=" ) ) )
413 strtol( psz_parser + strlen( "quality=" ),
418 msg_Warn( p_input, "unknown option" );
421 while( *psz_parser && *psz_parser != ':' )
426 if( *psz_parser == '\0' )
435 p_sys->psz_video_device = strdup( psz_dup );
439 p_sys->psz_video_device = strdup( "/dev/video" );
441 msg_Dbg( p_input, "video device=`%s'", p_sys->psz_video_device );
443 if( p_sys->psz_adev && *p_sys->psz_adev == '\0' )
445 p_sys->psz_adev = strdup( "/dev/dsp" );
447 msg_Dbg( p_input, "audio device=`%s'", p_sys->psz_adev );
451 if( ( p_sys->fd = open( p_sys->psz_video_device, O_RDWR ) ) < 0 )
453 msg_Err( p_input, "cannot open device (%s)", strerror( errno ) );
457 if( ioctl( p_sys->fd, VIDIOCGCAP, &p_sys->vid_cap ) < 0 )
459 msg_Err( p_input, "cannot get capabilities (%s)", strerror( errno ) );
464 "V4L device %s %d channels %d audios %d < w < %d %d < h < %d",
466 p_sys->vid_cap.channels,
467 p_sys->vid_cap.audios,
468 p_sys->vid_cap.minwidth, p_sys->vid_cap.maxwidth,
469 p_sys->vid_cap.minheight, p_sys->vid_cap.maxheight );
471 if( p_sys->i_channel < 0 || p_sys->i_channel >= p_sys->vid_cap.channels )
473 msg_Dbg( p_input, "invalid channel, falling back on channel 0" );
474 p_sys->i_channel = 0;
476 if( p_sys->i_audio >= p_sys->vid_cap.audios )
478 msg_Dbg( p_input, "invalid audio, falling back with no audio" );
482 if( p_sys->i_width < p_sys->vid_cap.minwidth ||
483 p_sys->i_width > p_sys->vid_cap.maxwidth )
485 msg_Dbg( p_input, "invalid width %i", p_sys->i_width );
488 if( p_sys->i_height < p_sys->vid_cap.minheight ||
489 p_sys->i_height > p_sys->vid_cap.maxheight )
491 msg_Dbg( p_input, "invalid height %i", p_sys->i_height );
495 if( !( p_sys->vid_cap.type & VID_TYPE_CAPTURE ) )
497 msg_Err( p_input, "cannot grab" );
501 vid_channel.channel = p_sys->i_channel;
502 if( ioctl( p_sys->fd, VIDIOCGCHAN, &vid_channel ) < 0 )
504 msg_Err( p_input, "cannot get channel infos (%s)",
509 "setting channel %s(%d) %d tuners flags=0x%x type=0x%x norm=0x%x",
517 if( p_sys->i_tuner >= vid_channel.tuners )
519 msg_Dbg( p_input, "invalid tuner, falling back on tuner 0" );
523 vid_channel.norm = p_sys->i_norm;
524 if( ioctl( p_sys->fd, VIDIOCSCHAN, &vid_channel ) < 0 )
526 msg_Err( p_input, "cannot set channel (%s)", strerror( errno ) );
530 if( vid_channel.flags & VIDEO_VC_TUNER )
535 struct video_tuner vid_tuner;
536 if( p_sys->i_tuner >= 0 )
538 vid_tuner.tuner = p_sys->i_tuner;
539 if( ioctl( p_sys->fd, VIDIOCGTUNER, &vid_tuner ) < 0 )
541 msg_Err( p_input, "cannot get tuner (%s)", strerror( errno ) );
544 msg_Dbg( p_input, "tuner %s low=%d high=%d, flags=0x%x "
545 "mode=0x%x signal=0x%x",
546 vid_tuner.name, vid_tuner.rangelow, vid_tuner.rangehigh,
547 vid_tuner.flags, vid_tuner.mode, vid_tuner.signal );
549 msg_Dbg( p_input, "setting tuner %s (%d)",
550 vid_tuner.name, vid_tuner.tuner );
552 //vid_tuner.mode = p_sys->i_norm; /* FIXME FIXME to be checked FIXME FIXME */
553 if( ioctl( p_sys->fd, VIDIOCSTUNER, &vid_tuner ) < 0 )
555 msg_Err( p_input, "cannot set tuner (%s)", strerror( errno ) );
561 // show a warning if frequency is < than 30000. User is certainly usint old syntax.
565 if( p_sys->i_frequency >= 0 )
567 int driver_frequency = p_sys->i_frequency * 16 /1000;
568 if( ioctl( p_sys->fd, VIDIOCSFREQ, &driver_frequency ) < 0 )
570 msg_Err( p_input, "cannot set frequency (%s)",
574 msg_Dbg( p_input, "frequency %d (%d)", p_sys->i_frequency,
580 if( vid_channel.flags & VIDEO_VC_AUDIO )
582 struct video_audio vid_audio;
584 /* XXX TODO volume, balance, ... */
585 if( p_sys->i_audio >= 0 )
587 vid_audio.audio = p_sys->i_audio;
588 if( ioctl( p_sys->fd, VIDIOCGAUDIO, &vid_audio ) < 0 )
590 msg_Err( p_input, "cannot get audio (%s)", strerror( errno ) );
595 vid_audio.flags &= ~VIDEO_AUDIO_MUTE;
597 if( ioctl( p_sys->fd, VIDIOCSAUDIO, &vid_audio ) < 0 )
599 msg_Err( p_input, "cannot set audio (%s)", strerror( errno ) );
606 if( p_sys->psz_adev )
609 if( ( p_sys->fd_audio = open( p_sys->psz_adev, O_RDONLY|O_NONBLOCK ) ) < 0 )
611 msg_Err( p_input, "cannot open audio device (%s)",
616 i_format = AFMT_S16_LE;
617 if( ioctl( p_sys->fd_audio, SNDCTL_DSP_SETFMT, &i_format ) < 0
618 || i_format != AFMT_S16_LE )
620 msg_Err( p_input, "cannot set audio format (16b little endian) "
621 "(%s)", strerror( errno ) );
625 if( ioctl( p_sys->fd_audio, SNDCTL_DSP_STEREO,
626 &p_sys->b_stereo ) < 0 )
628 msg_Err( p_input, "cannot set audio channels count (%s)",
633 if( ioctl( p_sys->fd_audio, SNDCTL_DSP_SPEED,
634 &p_sys->i_sample_rate ) < 0 )
636 msg_Err( p_input, "cannot set audio sample rate (%s)",
644 p_sys->b_stereo ? "stereo" : "mono",
645 p_sys->i_sample_rate );
647 p_sys->i_audio_frame_size = 0;
648 p_sys->i_audio_frame_size_allocated = 6*1024;
649 p_sys->p_audio_frame =
650 malloc( p_sys->i_audio_frame_size_allocated );
653 /* establish basic params with input and norm before feeling width
657 struct quicktime_mjpeg_app1 *p_app1;
660 if( ioctl( p_sys->fd, MJPIOC_G_PARAMS, &mjpeg ) < 0 )
662 msg_Err( p_input, "cannot get mjpeg params (%s)",
666 mjpeg.input = p_sys->i_channel;
667 mjpeg.norm = p_sys->i_norm;
668 mjpeg.decimation = p_sys->i_decimation;
671 mjpeg.img_width = p_sys->i_width / p_sys->i_decimation;
672 if( p_sys->i_height )
673 mjpeg.img_height = p_sys->i_height / p_sys->i_decimation;
675 /* establish Quicktime APP1 marker while we are here */
680 p_app1 = (struct quicktime_mjpeg_app1 *)mjpeg.APP_data;
681 p_app1->i_reserved = 0;
682 p_app1->i_tag = VLC_FOURCC( 'm','j','p','g' );
683 p_app1->i_field_size = 0;
684 p_app1->i_padded_field_size = 0;
685 p_app1->i_next_field = 0;
686 /* XXX WARNING XXX */
687 /* these's nothing magic about these values. We are dangerously
688 * assuming the encoder card is encoding mjpeg-a and is not throwing
689 * in marker tags we aren't expecting. It's bad enough we have to
690 * search through the jpeg output for every frame we grab just to
691 * find the first field's end marker, so we take this risk to boost
693 * This is really something the driver could do for us because this
694 * does conform to standards outside of Apple Quicktime.
697 p_app1->i_DQT_offset = hton32( i_offset );
699 p_app1->i_DHT_offset = hton32( i_offset );
701 p_app1->i_SOF_offset = hton32( i_offset );
703 p_app1->i_SOS_offset = hton32( i_offset );
705 p_app1->i_data_offset = hton32( i_offset );
707 /* SOF and SOS aren't specified by the mjpeg API because they aren't
708 * optional. They will be present in the output. */
709 mjpeg.jpeg_markers = JPEG_MARKER_DHT | JPEG_MARKER_DQT;
711 if( ioctl( p_sys->fd, MJPIOC_S_PARAMS, &mjpeg ) < 0 )
713 msg_Err( p_input, "cannot set mjpeg params (%s)",
718 p_sys->i_width = mjpeg.img_width * mjpeg.HorDcm;
719 p_sys->i_height = mjpeg.img_height * mjpeg.VerDcm *
720 mjpeg.field_per_buff;
723 /* fix width/height */
724 if( !p_sys->b_mjpeg && ( p_sys->i_width == 0 || p_sys->i_height == 0 ) )
726 struct video_window vid_win;
728 if( ioctl( p_sys->fd, VIDIOCGWIN, &vid_win ) < 0 )
730 msg_Err( p_input, "cannot get win (%s)", strerror( errno ) );
733 p_sys->i_width = vid_win.width;
734 p_sys->i_height = vid_win.height;
736 msg_Dbg( p_input, "will use %dx%d", p_sys->i_width, p_sys->i_height );
739 p_sys->p_video_frame = NULL;
741 if( !p_sys->b_mjpeg )
743 /* Find out video format used by device */
744 if( ioctl( p_sys->fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 )
746 int i_chroma = VLC_FOURCC( ' ', ' ', ' ', ' ' );
747 struct video_picture vid_picture = p_sys->vid_picture;
749 /* Try to set the format to something easy to encode */
750 vid_picture.palette = VIDEO_PALETTE_YUV420P;
751 if( ioctl( p_sys->fd, VIDIOCSPICT, &vid_picture ) == 0 )
753 p_sys->vid_picture = vid_picture;
757 vid_picture.palette = VIDEO_PALETTE_YUV422P;
758 if( ioctl( p_sys->fd, VIDIOCSPICT, &vid_picture ) == 0 )
760 p_sys->vid_picture = vid_picture;
764 /* Find out final format */
765 switch( p_sys->vid_picture.palette )
767 case VIDEO_PALETTE_GREY:
768 i_chroma = VLC_FOURCC( 'G', 'R', 'E', 'Y' );
770 case VIDEO_PALETTE_HI240:
771 i_chroma = VLC_FOURCC( 'I', '2', '4', '0' );
773 case VIDEO_PALETTE_RGB565:
774 i_chroma = VLC_FOURCC( 'R', 'V', '1', '6' );
776 case VIDEO_PALETTE_RGB555:
777 i_chroma = VLC_FOURCC( 'R', 'V', '1', '5' );
779 case VIDEO_PALETTE_RGB24:
780 i_chroma = VLC_FOURCC( 'R', 'V', '2', '4' );
782 case VIDEO_PALETTE_RGB32:
783 i_chroma = VLC_FOURCC( 'R', 'V', '3', '2' );
785 case VIDEO_PALETTE_YUV422:
786 i_chroma = VLC_FOURCC( 'I', '4', '2', '2' );
788 case VIDEO_PALETTE_YUYV:
789 i_chroma = VLC_FOURCC( 'Y', 'U', 'Y', 'V' );
791 case VIDEO_PALETTE_UYVY:
792 i_chroma = VLC_FOURCC( 'U', 'Y', 'V', 'Y' );
794 case VIDEO_PALETTE_YUV420:
795 i_chroma = VLC_FOURCC( 'I', '4', '2', 'N' );
797 case VIDEO_PALETTE_YUV411:
798 i_chroma = VLC_FOURCC( 'I', '4', '1', 'N' );
800 case VIDEO_PALETTE_RAW:
801 i_chroma = VLC_FOURCC( 'G', 'R', 'A', 'W' );
803 case VIDEO_PALETTE_YUV422P:
804 i_chroma = VLC_FOURCC( 'I', '4', '2', '2' );
806 case VIDEO_PALETTE_YUV420P:
807 i_chroma = VLC_FOURCC( 'I', '4', '2', '0' );
809 case VIDEO_PALETTE_YUV411P:
810 i_chroma = VLC_FOURCC( 'I', '4', '1', '1' );
813 p_sys->i_fourcc = i_chroma;
817 msg_Err( p_input, "ioctl VIDIOCGPICT failed" );
827 p_sys->mjpeg_buffers.count = 8;
828 p_sys->mjpeg_buffers.size = MJPEG_BUFFER_SIZE;
830 if( ioctl( p_sys->fd, MJPIOC_REQBUFS, &p_sys->mjpeg_buffers ) < 0 )
832 msg_Err( p_input, "mmap unsupported" );
836 p_sys->p_video_mmap = mmap( 0,
837 p_sys->mjpeg_buffers.size * p_sys->mjpeg_buffers.count,
838 PROT_READ | PROT_WRITE, MAP_SHARED, p_sys->fd, 0 );
839 if( p_sys->p_video_mmap == MAP_FAILED )
841 msg_Err( p_input, "mmap failed" );
845 p_sys->i_fourcc = VLC_FOURCC( 'm','j','p','g' );
846 p_sys->i_frame_pos = -1;
848 /* queue up all the frames */
849 for( i = 0; i < (int)p_sys->mjpeg_buffers.count; i++ )
851 if( ioctl( p_sys->fd, MJPIOC_QBUF_CAPT, &i ) < 0 )
853 msg_Err( p_input, "unable to queue frame" );
860 /* Fill in picture_t fields */
861 vout_InitPicture( VLC_OBJECT(p_input), &p_sys->pic, p_sys->i_fourcc,
862 p_sys->i_width, p_sys->i_height, p_sys->i_width *
863 VOUT_ASPECT_FACTOR / p_sys->i_height );
864 if( !p_sys->pic.i_planes )
866 msg_Err( p_input, "unsupported chroma" );
869 p_sys->i_video_frame_size = 0;
870 for( i = 0; i < p_sys->pic.i_planes; i++ )
872 p_sys->i_video_frame_size += p_sys->pic.p[i].i_lines *
873 p_sys->pic.p[i].i_visible_pitch;
876 msg_Dbg( p_input, "v4l device uses frame size: %i",
877 p_sys->i_video_frame_size );
878 msg_Dbg( p_input, "v4l device uses chroma: %4.4s",
879 (char*)&p_sys->i_fourcc );
881 /* Allocate mmap buffer */
882 if( ioctl( p_sys->fd, VIDIOCGMBUF, &p_sys->vid_mbuf ) < 0 )
884 msg_Err( p_input, "mmap unsupported" );
888 p_sys->p_video_mmap = mmap( 0, p_sys->vid_mbuf.size,
889 PROT_READ|PROT_WRITE, MAP_SHARED,
891 if( p_sys->p_video_mmap == MAP_FAILED )
893 /* FIXME -> normal read */
894 msg_Err( p_input, "mmap failed" );
899 p_sys->vid_mmap.frame = 0;
900 p_sys->vid_mmap.width = p_sys->i_width;
901 p_sys->vid_mmap.height = p_sys->i_height;
902 p_sys->vid_mmap.format = p_sys->vid_picture.palette;
903 if( ioctl( p_sys->fd, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 )
905 msg_Warn( p_input, "%4.4s refused", (char*)&p_sys->i_fourcc );
906 msg_Err( p_input, "chroma selection failed" );
911 p_input->pf_read = Read;
912 p_input->pf_seek = NULL;
913 p_input->pf_set_area = NULL;
914 p_input->pf_set_program = NULL;
916 p_input->p_access_data = p_sys;
918 vlc_mutex_lock( &p_input->stream.stream_lock );
919 p_input->stream.b_pace_control = 0;
920 p_input->stream.b_seekable = 0;
921 p_input->stream.p_selected_area->i_size = 0;
922 p_input->stream.p_selected_area->i_tell = 0;
923 p_input->stream.i_method = INPUT_METHOD_FILE;
924 vlc_mutex_unlock( &p_input->stream.stream_lock );
926 /* Update default_pts to a suitable value for access */
927 p_input->i_pts_delay = config_GetInt( p_input, "v4l-caching" ) * 1000;
929 msg_Info( p_input, "v4l grabbing started" );
932 p_sys->i_header_size = 8 + 20;
933 p_sys->i_header_pos = 0;
934 p_sys->p_header = malloc( p_sys->i_header_size );
936 memcpy( &p_sys->p_header[0], ".v4l", 4 );
937 SetDWBE( &p_sys->p_header[4], 1 );
939 memcpy( &p_sys->p_header[ 8], "vids", 4 );
940 memcpy( &p_sys->p_header[12], &p_sys->i_fourcc, 4 );
941 SetDWBE( &p_sys->p_header[16], p_sys->i_width );
942 SetDWBE( &p_sys->p_header[20], p_sys->i_height );
943 SetDWBE( &p_sys->p_header[24], 0 );
945 if( p_sys->fd_audio > 0 )
947 p_sys->i_header_size += 20;
948 p_sys->p_header = realloc( p_sys->p_header, p_sys->i_header_size );
950 SetDWBE( &p_sys->p_header[4], 2 );
952 memcpy( &p_sys->p_header[28], "auds", 4 );
953 memcpy( &p_sys->p_header[32], "araw", 4 );
954 SetDWBE( &p_sys->p_header[36], p_sys->b_stereo ? 2 : 1 );
955 SetDWBE( &p_sys->p_header[40], p_sys->i_sample_rate );
956 SetDWBE( &p_sys->p_header[44], 16 );
961 free( p_sys->psz_video_device );
970 /*****************************************************************************
971 * V4lClose: close device
972 *****************************************************************************/
973 static void AccessClose( vlc_object_t *p_this )
975 input_thread_t *p_input = (input_thread_t *)p_this;
976 access_sys_t *p_sys = p_input->p_access_data;
978 msg_Info( p_input, "v4l grabbing stoped" );
983 ioctl( p_sys->fd, MJPIOC_QBUF_CAPT, &i_noframe );
986 free( p_sys->psz_video_device );
988 if( p_sys->p_video_mmap && p_sys->p_video_mmap != MAP_FAILED )
991 munmap( p_sys->p_video_mmap, p_sys->mjpeg_buffers.size *
992 p_sys->mjpeg_buffers.count );
994 munmap( p_sys->p_video_mmap, p_sys->vid_mbuf.size );
996 if( p_sys->fd_audio >= 0 )
998 close( p_sys->fd_audio );
1004 static int GrabAudio( input_thread_t * p_input,
1009 access_sys_t *p_sys = p_input->p_access_data;
1010 struct audio_buf_info buf_info;
1014 i_read = read( p_sys->fd_audio, p_sys->p_audio_frame,
1015 p_sys->i_audio_frame_size_allocated );
1019 return VLC_EGENERIC;
1022 p_sys->i_audio_frame_size = i_read;
1024 /* from vls : correct the date because of kernel buffering */
1026 if( ioctl( p_sys->fd_audio, SNDCTL_DSP_GETISPACE, &buf_info ) == 0 )
1028 i_correct += buf_info.bytes;
1032 *pp_data = p_sys->p_audio_frame;
1033 *pi_data = p_sys->i_audio_frame_size;
1034 *pi_pts = mdate() - (mtime_t)1000000 * (mtime_t)i_correct /
1035 2 / ( p_sys->b_stereo ? 2 : 1) / p_sys->i_sample_rate;
1039 static uint8_t *GrabCapture( input_thread_t *p_input )
1041 access_sys_t *p_sys = p_input->p_access_data;
1042 p_sys->vid_mmap.frame = ( p_sys->i_frame_pos + 1 ) %
1043 p_sys->vid_mbuf.frames;
1046 if( ioctl( p_sys->fd, VIDIOCMCAPTURE, &p_sys->vid_mmap ) >= 0 )
1051 if( errno != EAGAIN )
1053 msg_Err( p_input, "failed while grabbing new frame" );
1056 msg_Dbg( p_input, "another try ?" );
1059 //msg_Warn( p_input, "grab a new frame" );
1061 while( ioctl(p_sys->fd, VIDIOCSYNC, &p_sys->i_frame_pos) < 0 &&
1062 ( errno == EAGAIN || errno == EINTR ) );
1064 p_sys->i_frame_pos = p_sys->vid_mmap.frame;
1065 /* leave i_video_frame_size alone */
1066 return p_sys->p_video_mmap + p_sys->vid_mbuf.offsets[p_sys->i_frame_pos];
1069 static uint8_t *GrabMJPEG( input_thread_t *p_input )
1071 access_sys_t *p_sys = p_input->p_access_data;
1072 struct mjpeg_sync sync;
1073 uint8_t *p_frame, *p_field, *p;
1076 struct quicktime_mjpeg_app1 *p_app1 = NULL;
1078 /* re-queue the last frame we sync'd */
1079 if( p_sys->i_frame_pos != -1 )
1080 while( ioctl( p_sys->fd, MJPIOC_QBUF_CAPT, &p_sys->i_frame_pos ) < 0 &&
1081 ( errno == EAGAIN || errno == EINTR ) );
1083 /* sync on the next frame */
1084 while( ioctl( p_sys->fd, MJPIOC_SYNC, &sync ) < 0 &&
1085 ( errno == EAGAIN || errno == EINTR ) );
1087 p_sys->i_frame_pos = sync.frame;
1088 p_frame = p_sys->p_video_mmap + p_sys->mjpeg_buffers.size * sync.frame;
1090 /* p_frame now points to the data. fix up the Quicktime APP1 marker */
1092 tag = hton16( tag );
1096 p = memmem( p_field, sync.length, &tag, 2 );
1100 p += 2; /* data immediately following EOI */
1102 p_app1 = (struct quicktime_mjpeg_app1 *)(p_field + 6);
1104 i_size = ((uint32_t)(p - p_field));
1105 i_size = hton32( i_size );
1106 memcpy( &p_app1->i_field_size, &i_size, 4 );
1108 while( *p == 0xff && *(p+1) == 0xff )
1111 i_size = ((uint32_t)(p - p_field));
1112 i_size = hton32( i_size );
1113 memcpy( &p_app1->i_padded_field_size, &i_size, 4 );
1117 tag = hton16( tag );
1118 p_field = memmem( p, sync.length - (size_t)(p - p_frame), &tag, 2 );
1122 i_size = (uint32_t)(p_field - p_frame);
1123 i_size = hton32( i_size );
1124 memcpy( &p_app1->i_next_field, &i_size, 4 );
1127 p_app1 = (struct quicktime_mjpeg_app1 *)(p_field + 6);
1129 tag = hton16( tag );
1130 p = memmem( p_field, sync.length - (size_t)(p_field - p_frame),
1135 /* sometimes the second field doesn't have the EOI. just put it
1138 p = p_frame + sync.length;
1139 memcpy( p, &tag, 2 );
1144 i_size = (uint32_t)(p - p_field);
1145 i_size = hton32( i_size );
1146 memcpy( &p_app1->i_field_size, &i_size, 4 );
1147 i_size = (uint32_t)(sync.length - (uint32_t)(p_field - p_frame));
1148 i_size = hton32( i_size );
1149 memcpy( &p_app1->i_padded_field_size, &i_size, 4 );
1152 p_sys->i_video_frame_size = sync.length;
1156 static int GrabVideo( input_thread_t * p_input,
1161 access_sys_t *p_sys = p_input->p_access_data;
1164 if( p_sys->b_mjpeg )
1165 p_frame = GrabMJPEG( p_input );
1167 p_frame = GrabCapture( p_input );
1172 p_sys->p_video_frame = p_frame;
1174 *pp_data = p_sys->p_video_frame;
1175 *pi_data = p_sys->i_video_frame_size;
1180 /*****************************************************************************
1181 * Read: reads from the device into PES packets.
1182 *****************************************************************************
1183 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
1185 *****************************************************************************/
1186 static int Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
1188 access_sys_t *p_sys = p_input->p_access_data;
1193 //msg_Info( p_input, "access read data_size %i, data_pos %i",
1194 //p_sys->i_data_size, p_sys->i_data_pos );
1197 /* First copy header if any */
1198 if( i_len > 0 && p_sys->i_header_pos < p_sys->i_header_size )
1202 i_copy = __MIN( p_sys->i_header_size - p_sys->i_header_pos,
1204 memcpy( p_buffer, &p_sys->p_header[p_sys->i_header_pos], i_copy );
1205 p_sys->i_header_pos += i_copy;
1213 if( i_len > 0 && p_sys->i_data_pos < p_sys->i_data_size )
1217 i_copy = __MIN( p_sys->i_data_size - p_sys->i_data_pos,
1220 memcpy( p_buffer, &p_sys->p_data[p_sys->i_data_pos], i_copy );
1221 p_sys->i_data_pos += i_copy;
1228 /* The caller got what he wanted */
1234 /* Read no more than one frame at a time.
1235 * That kills latency, especially for encoded v4l streams */
1236 if( p_sys->i_data_size && p_sys->i_data_pos == p_sys->i_data_size )
1238 p_sys->i_data_pos = 0; p_sys->i_data_size = 0;
1242 /* re fill data by grabbing audio/video */
1243 p_sys->i_data_pos = 0;
1245 /* try grabbing audio frames */
1247 if( p_sys->fd_audio < 0 ||
1248 GrabAudio( p_input, &p_sys->p_data, &p_sys->i_data_size, &i_pts ) )
1250 /* and then get video frame if no audio */
1252 if( GrabVideo( p_input, &p_sys->p_data, &p_sys->i_data_size, &i_pts ) )
1258 /* create pseudo header */
1259 p_sys->i_header_size = 16;
1260 p_sys->i_header_pos = 0;
1261 SetDWBE( &p_sys->p_header[0], i_stream );
1262 SetDWBE( &p_sys->p_header[4], p_sys->i_data_size );
1263 SetQWBE( &p_sys->p_header[8], i_pts );
1269 /****************************************************************************
1271 ****************************************************************************/
1272 #define MAX_PACKETS_IN_FIFO 3
1274 static int DemuxOpen( vlc_object_t *p_this )
1276 input_thread_t *p_input = (input_thread_t *)p_this;
1277 //demux_sys_t *p_sys;
1283 data_packet_t *p_pk;
1285 /* Initialize access plug-in structures. */
1286 if( p_input->i_mtu == 0 )
1288 /* Improve speed. */
1289 p_input->i_bufsize = INPUT_DEFAULT_BUFSIZE ;
1292 /* a little test to see if it's a v4l stream */
1293 if( input_Peek( p_input, &p_peek, 8 ) < 8 )
1295 msg_Warn( p_input, "v4l plugin discarded (cannot peek)" );
1296 return( VLC_EGENERIC );
1299 if( strncmp( p_peek, ".v4l", 4 ) || GetDWBE( &p_peek[4] ) <= 0 )
1301 msg_Warn( p_input, "v4l plugin discarded (not a valid stream)" );
1302 return VLC_EGENERIC;
1305 /* create one program */
1306 vlc_mutex_lock( &p_input->stream.stream_lock );
1307 if( input_InitStream( p_input, 0 ) == -1)
1309 vlc_mutex_unlock( &p_input->stream.stream_lock );
1310 msg_Err( p_input, "cannot init stream" );
1311 return( VLC_EGENERIC );
1313 if( input_AddProgram( p_input, 0, 0) == NULL )
1315 vlc_mutex_unlock( &p_input->stream.stream_lock );
1316 msg_Err( p_input, "cannot add program" );
1317 return( VLC_EGENERIC );
1320 p_input->stream.p_selected_program = p_input->stream.pp_programs[0];
1321 p_input->stream.i_mux_rate = 0;
1323 i_streams = GetDWBE( &p_peek[4] );
1324 if( input_Peek( p_input, &p_peek, 8 + 20 * i_streams )
1325 < 8 + 20 * i_streams )
1327 msg_Err( p_input, "v4l plugin discarded (cannot peek)" );
1328 return( VLC_EGENERIC );
1332 for( i = 0; i < i_streams; i++ )
1334 es_descriptor_t *p_es;
1336 if( !strncmp( p_peek, "auds", 4 ) )
1338 #define wf ((WAVEFORMATEX*)p_es->p_waveformatex)
1339 p_es = input_AddES( p_input, p_input->stream.pp_programs[0],
1340 i + 1, AUDIO_ES, NULL, 0 );
1341 p_es->i_stream_id = i + 1;
1343 VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] );
1345 p_es->p_waveformatex= malloc( sizeof( WAVEFORMATEX ) );
1347 wf->wFormatTag = WAVE_FORMAT_UNKNOWN;
1348 wf->nChannels = GetDWBE( &p_peek[8] );
1349 wf->nSamplesPerSec = GetDWBE( &p_peek[12] );
1350 wf->wBitsPerSample = GetDWBE( &p_peek[16] );
1351 wf->nBlockAlign = wf->wBitsPerSample * wf->nChannels / 8;
1352 wf->nAvgBytesPerSec = wf->nBlockAlign * wf->nSamplesPerSec;
1355 msg_Dbg( p_input, "added new audio es %d channels %dHz",
1356 wf->nChannels, wf->nSamplesPerSec );
1358 input_SelectES( p_input, p_es );
1361 else if( !strncmp( p_peek, "vids", 4 ) )
1363 #define bih ((BITMAPINFOHEADER*)p_es->p_bitmapinfoheader)
1364 p_es = input_AddES( p_input, p_input->stream.pp_programs[0],
1365 i + 1, VIDEO_ES, NULL, 0 );
1366 p_es->i_stream_id = i + 1;
1368 VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] );
1370 p_es->p_bitmapinfoheader = malloc( sizeof( BITMAPINFOHEADER ) );
1372 bih->biSize = sizeof( BITMAPINFOHEADER );
1373 bih->biWidth = GetDWBE( &p_peek[8] );
1374 bih->biHeight = GetDWBE( &p_peek[12] );
1376 bih->biBitCount = 0;
1377 bih->biCompression = 0;
1378 bih->biSizeImage= 0;
1379 bih->biXPelsPerMeter = 0;
1380 bih->biYPelsPerMeter = 0;
1382 bih->biClrImportant = 0;
1384 msg_Dbg( p_input, "added new video es %4.4s %dx%d",
1385 (char*)&p_es->i_fourcc, bih->biWidth, bih->biHeight );
1387 input_SelectES( p_input, p_es );
1394 p_input->stream.p_selected_program->b_is_ok = 1;
1395 vlc_mutex_unlock( &p_input->stream.stream_lock );
1397 if( input_SplitBuffer( p_input, &p_pk, 8 + i_streams * 20 ) > 0 )
1399 input_DeletePacket( p_input->p_method_data, p_pk );
1402 p_input->pf_demux = Demux;
1403 p_input->pf_demux_control = demux_vaControlDefault;
1407 static void DemuxClose( vlc_object_t *p_this )
1412 static int Demux( input_thread_t *p_input )
1414 es_descriptor_t *p_es;
1415 pes_packet_t *p_pes;
1422 if( input_Peek( p_input, &p_peek, 16 ) < 16 )
1424 msg_Warn( p_input, "cannot peek (EOF ?)" );
1428 i_stream = GetDWBE( &p_peek[0] );
1429 i_size = GetDWBE( &p_peek[4] );
1430 i_pts = GetQWBE( &p_peek[8] );
1432 //msg_Dbg( p_input, "stream=%d size=%d", i_stream, i_size );
1433 // p_es = input_FindES( p_input, i_stream );
1434 p_es = p_input->stream.p_selected_program->pp_es[i_stream];
1437 msg_Err( p_input, "cannot find ES" );
1440 p_pes = input_NewPES( p_input->p_method_data );
1443 msg_Warn( p_input, "cannot allocate PES" );
1450 data_packet_t *p_data;
1453 if( (i_read = input_SplitBuffer( p_input, &p_data,
1454 __MIN( i_size, 10000 ) ) ) <= 0 )
1456 input_DeletePES( p_input->p_method_data, p_pes );
1459 if( !p_pes->p_first )
1461 p_pes->p_first = p_data;
1462 p_pes->i_nb_data = 1;
1463 p_pes->i_pes_size = i_read;
1467 p_pes->p_last->p_next = p_data;
1469 p_pes->i_pes_size += i_read;
1471 p_pes->p_last = p_data;
1474 // input_SplitBuffer( p_input, &p_pk, i_size + 8 );
1475 p_pes->p_first->p_payload_start += 16;
1476 p_pes->i_pes_size -= 16;
1477 if( p_es && p_es->p_decoder_fifo )
1479 vlc_mutex_lock( &p_es->p_decoder_fifo->data_lock );
1480 if( p_es->p_decoder_fifo->i_depth >= MAX_PACKETS_IN_FIFO )
1482 /* Wait for the decoder. */
1483 vlc_cond_wait( &p_es->p_decoder_fifo->data_wait,
1484 &p_es->p_decoder_fifo->data_lock );
1486 vlc_mutex_unlock( &p_es->p_decoder_fifo->data_lock );
1487 //p_pes->i_pts = mdate() + p_input->i_pts_delay;
1488 p_pes->i_pts = p_pes->i_dts = i_pts + p_input->i_pts_delay;
1489 input_DecodePES( p_es->p_decoder_fifo, p_pes );
1493 input_DeletePES( p_input->p_method_data, p_pes );