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 AccessOpen ( vlc_object_t * );
70 static void AccessClose( vlc_object_t * );
72 static int DemuxOpen ( vlc_object_t * );
73 static void DemuxClose ( vlc_object_t * );
75 #define CACHING_TEXT N_("Caching value in ms")
76 #define CACHING_LONGTEXT N_( \
77 "Allows you to modify the default caching value for v4l streams. This " \
78 "value should be set in millisecond units." )
79 #define VDEV_TEXT N_("Video device name")
80 #define VDEV_LONGTEXT N_( \
81 "Specify the name of the video device that will be used. " \
82 "If you don't specify anything, no video device will be used.")
83 #define ADEV_TEXT N_("Audio device name")
84 #define ADEV_LONGTEXT N_( \
85 "Specify the name of the audio device that will be used. " \
86 "If you don't specify anything, no audio device will be used.")
87 #define CHROMA_TEXT N_("Video input chroma format")
88 #define CHROMA_LONGTEXT N_( \
89 "Force the Video4Linux video device to use a specific chroma format " \
90 "(eg. I420 (default), RV24, etc.)")
93 set_description( _("Video4Linux input") );
95 add_integer( "v4l-caching", DEFAULT_PTS_DELAY / 1000, NULL,
96 CACHING_TEXT, CACHING_LONGTEXT, VLC_TRUE );
97 add_string( "v4l-vdev", "/dev/video", 0, VDEV_TEXT, VDEV_LONGTEXT,
99 add_string( "v4l-adev", "/dev/dsp", 0, ADEV_TEXT, ADEV_LONGTEXT,
101 add_string( "v4l-chroma", NULL, NULL, CHROMA_TEXT, CHROMA_LONGTEXT,
104 add_shortcut( "v4l" );
105 set_capability( "access", 10 );
106 set_callbacks( AccessOpen, AccessClose );
109 set_description( _("Video4Linux demuxer") );
110 add_shortcut( "v4l" );
111 set_capability( "demux", 200 );
112 set_callbacks( DemuxOpen, DemuxClose );
116 /*****************************************************************************
117 * Access: local prototypes
118 *****************************************************************************/
119 static int Read ( input_thread_t *, byte_t *, size_t );
121 static void ParseMRL ( input_thread_t * );
122 static int OpenVideoDev( input_thread_t *, char * );
123 static int OpenAudioDev( input_thread_t *, char * );
125 #define MJPEG_BUFFER_SIZE (256*1024)
127 struct quicktime_mjpeg_app1
129 uint32_t i_reserved; /* set to 0 */
130 uint32_t i_tag; /* 'mjpg' */
131 uint32_t i_field_size; /* offset following EOI */
132 uint32_t i_padded_field_size; /* offset following EOI+pad */
133 uint32_t i_next_field; /* offset to next field */
134 uint32_t i_DQT_offset;
135 uint32_t i_DHT_offset;
136 uint32_t i_SOF_offset;
137 uint32_t i_SOS_offset;
138 uint32_t i_data_offset; /* following SOS marker data */
146 } v4lchroma_to_fourcc[] =
148 { VIDEO_PALETTE_GREY, VLC_FOURCC( 'G', 'R', 'E', 'Y' ) },
149 { VIDEO_PALETTE_HI240, VLC_FOURCC( 'I', '2', '4', '0' ) },
150 { VIDEO_PALETTE_RGB565, VLC_FOURCC( 'R', 'V', '1', '6' ) },
151 { VIDEO_PALETTE_RGB555, VLC_FOURCC( 'R', 'V', '1', '5' ) },
152 { VIDEO_PALETTE_RGB24, VLC_FOURCC( 'R', 'V', '2', '4' ) },
153 { VIDEO_PALETTE_RGB32, VLC_FOURCC( 'R', 'V', '3', '2' ) },
154 { VIDEO_PALETTE_YUV422, VLC_FOURCC( 'I', '4', '2', '2' ) },
155 { VIDEO_PALETTE_YUYV, VLC_FOURCC( 'Y', 'U', 'Y', 'V' ) },
156 { VIDEO_PALETTE_UYVY, VLC_FOURCC( 'U', 'Y', 'V', 'Y' ) },
157 { VIDEO_PALETTE_YUV420, VLC_FOURCC( 'I', '4', '2', 'N' ) },
158 { VIDEO_PALETTE_YUV411, VLC_FOURCC( 'I', '4', '1', 'N' ) },
159 { VIDEO_PALETTE_RAW, VLC_FOURCC( 'G', 'R', 'A', 'W' ) },
160 { VIDEO_PALETTE_YUV422P, VLC_FOURCC( 'I', '4', '2', '2' ) },
161 { VIDEO_PALETTE_YUV420P, VLC_FOURCC( 'I', '4', '2', '0' ) },
162 { VIDEO_PALETTE_YUV411P, VLC_FOURCC( 'I', '4', '1', '1' ) },
169 char *psz_device; /* Main device from MRL, can be video or audio */
177 /* Video properties */
194 float f_fps; /* <= 0.0 mean to grab at full rate */
195 mtime_t i_video_pts; /* only used when f_fps > 0 */
201 struct video_capability vid_cap;
202 struct video_mbuf vid_mbuf;
203 struct mjpeg_requestbuffers mjpeg_buffers;
205 uint8_t *p_video_mmap;
208 struct video_mmap vid_mmap;
209 struct video_picture vid_picture;
211 uint8_t *p_video_frame;
212 int i_video_frame_size;
213 int i_video_frame_size_allocated;
215 /* Audio properties */
216 vlc_fourcc_t i_acodec_raw;
220 uint8_t *p_audio_frame;
221 int i_audio_frame_size;
222 int i_audio_frame_size_allocated;
228 uint8_t *p_header; // at lest 8 bytes allocated
233 uint8_t *p_data; // never allocated
241 * fcc "auds"|"vids" 0
258 static void SetDWBE( uint8_t *p, uint32_t dw )
260 p[0] = (dw >> 24)&0xff;
261 p[1] = (dw >> 16)&0xff;
262 p[2] = (dw >> 8)&0xff;
266 static void SetQWBE( uint8_t *p, uint64_t qw )
268 SetDWBE( p, (qw >> 32)&0xffffffff );
269 SetDWBE( &p[4], qw&0xffffffff);
272 /*****************************************************************************
273 * AccessOpen: opens v4l device
274 *****************************************************************************
276 * url: <video device>::::
278 *****************************************************************************/
279 static int AccessOpen( vlc_object_t *p_this )
281 input_thread_t *p_input = (input_thread_t *)p_this;
285 /* create access private data */
286 p_input->p_access_data = p_sys = malloc( sizeof( access_sys_t ) );
287 memset( p_sys, 0, sizeof( access_sys_t ) );
290 p_sys->i_norm = VIDEO_MODE_AUTO; // auto
292 p_sys->i_frequency = -1;
295 p_sys->i_video_pts = -1;
297 p_sys->i_brightness = -1;
299 p_sys->i_colour = -1;
300 p_sys->i_contrast = -1;
302 p_sys->b_mjpeg = VLC_FALSE;
303 p_sys->i_decimation = 1;
304 p_sys->i_quality = 100;
306 p_sys->i_video_frame_size_allocated = 0;
307 p_sys->i_frame_pos = 0;
309 p_sys->p_audio_frame = 0;
310 p_sys->i_sample_rate = 44100;
311 p_sys->b_stereo = VLC_TRUE;
313 p_sys->p_header = NULL;
314 p_sys->i_data_size = 0;
315 p_sys->i_data_pos = 0;
316 p_sys->p_data = NULL;
318 p_sys->psz_device = p_sys->psz_vdev = p_sys->psz_adev = NULL;
319 p_sys->fd_video = -1;
320 p_sys->fd_audio = -1;
324 /* Find main device (video or audio) */
325 if( p_sys->psz_device && *p_sys->psz_device )
327 msg_Dbg( p_input, "main device=`%s'", p_sys->psz_device );
329 /* Try to open as video device */
330 p_sys->fd_video = OpenVideoDev( p_input, p_sys->psz_device );
332 if( p_sys->fd_video < 0 )
334 /* Try to open as audio device */
335 p_sys->fd_audio = OpenAudioDev( p_input, p_sys->psz_device );
336 if( p_sys->fd_audio >= 0 )
338 if( p_sys->psz_adev ) free( p_sys->psz_adev );
339 p_sys->psz_adev = p_sys->psz_device;
340 p_sys->psz_device = NULL;
345 if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
346 p_sys->psz_vdev = p_sys->psz_device;
347 p_sys->psz_device = NULL;
351 /* If no device opened, only continue if the access was forced */
352 if( p_sys->fd_video < 0 && p_sys->fd_audio < 0 )
354 if( !p_input->psz_access ||
355 strcmp( p_input->psz_access, "v4l" ) )
357 AccessClose( p_this );
362 /* Find video device */
363 if( p_sys->fd_video < 0 )
365 if( !p_sys->psz_vdev || !*p_sys->psz_vdev )
367 var_Create( p_input, "v4l-vdev",
368 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
369 var_Get( p_input, "v4l-vdev", &val );
371 if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
372 p_sys->psz_vdev = val.psz_string;
375 if( p_sys->psz_vdev && *p_sys->psz_vdev )
377 p_sys->fd_video = OpenVideoDev( p_input, p_sys->psz_vdev );
381 /* Find audio device */
382 if( p_sys->fd_audio < 0 )
384 if( !p_sys->psz_adev || !*p_sys->psz_adev )
386 var_Create( p_input, "v4l-adev",
387 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
388 var_Get( p_input, "v4l-adev", &val );
390 if( p_sys->psz_adev ) free( p_sys->psz_adev );
391 p_sys->psz_adev = val.psz_string;
394 if( p_sys->psz_adev && *p_sys->psz_adev )
396 p_sys->fd_audio = OpenAudioDev( p_input, p_sys->psz_adev );
400 if( p_sys->fd_video < 0 && p_sys->fd_audio < 0 )
402 AccessClose( p_this );
406 p_input->pf_read = Read;
407 p_input->pf_seek = NULL;
408 p_input->pf_set_area = NULL;
409 p_input->pf_set_program = NULL;
411 vlc_mutex_lock( &p_input->stream.stream_lock );
412 p_input->stream.b_pace_control = 0;
413 p_input->stream.b_seekable = 0;
414 p_input->stream.p_selected_area->i_size = 0;
415 p_input->stream.p_selected_area->i_tell = 0;
416 p_input->stream.i_method = INPUT_METHOD_FILE;
417 vlc_mutex_unlock( &p_input->stream.stream_lock );
419 /* Update default_pts to a suitable value for access */
420 var_Create( p_input, "v4l-caching", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT);
421 var_Get( p_input, "v4l-caching", &val );
422 p_input->i_pts_delay = val.i_int * 1000;
424 msg_Info( p_input, "v4l grabbing started" );
427 p_sys->i_streams = 0;
428 p_sys->i_header_size = 8;
429 p_sys->i_header_pos = 8;
430 p_sys->p_header = malloc( p_sys->i_header_size );
432 memcpy( p_sys->p_header, ".v4l", 4 );
434 if( p_sys->fd_video >= 0 )
436 p_sys->i_header_size += 20;
437 p_sys->p_header = realloc( p_sys->p_header, p_sys->i_header_size );
439 SetDWBE( &p_sys->p_header[4], ++p_sys->i_streams );
441 memcpy( &p_sys->p_header[p_sys->i_header_pos], "vids", 4 );
442 memcpy( &p_sys->p_header[p_sys->i_header_pos+4],
443 &p_sys->i_fourcc, 4 );
444 SetDWBE( &p_sys->p_header[p_sys->i_header_pos+8], p_sys->i_width );
445 SetDWBE( &p_sys->p_header[p_sys->i_header_pos+12], p_sys->i_height );
446 SetDWBE( &p_sys->p_header[p_sys->i_header_pos+16], 0 );
448 p_sys->i_header_pos = p_sys->i_header_size;
451 if( p_sys->fd_audio >= 0 )
453 p_sys->i_header_size += 20;
454 p_sys->p_header = realloc( p_sys->p_header, p_sys->i_header_size );
456 SetDWBE( &p_sys->p_header[4], ++p_sys->i_streams );
458 memcpy( &p_sys->p_header[p_sys->i_header_pos], "auds", 4 );
459 memcpy( &p_sys->p_header[p_sys->i_header_pos+4], "araw", 4 );
460 SetDWBE( &p_sys->p_header[p_sys->i_header_pos+8],
461 p_sys->b_stereo ? 2 : 1 );
462 SetDWBE( &p_sys->p_header[p_sys->i_header_pos+12],
463 p_sys->i_sample_rate );
464 SetDWBE( &p_sys->p_header[p_sys->i_header_pos+16], 16 );
466 p_sys->i_header_pos = p_sys->i_header_size;
468 p_sys->i_header_pos = 0;
473 /*****************************************************************************
474 * ParseMRL: parse the options contained in the MRL
475 *****************************************************************************/
476 static void ParseMRL( input_thread_t *p_input )
478 access_sys_t *p_sys = p_input->p_access_data;
480 char *psz_dup = strdup( p_input->psz_name );
481 char *psz_parser = psz_dup;
483 while( *psz_parser && *psz_parser != ':' )
488 if( *psz_parser == ':' )
493 *psz_parser++ = '\0';
494 if( !strncmp( psz_parser, "channel=", strlen( "channel=" ) ) )
496 p_sys->i_channel = strtol( psz_parser + strlen( "channel=" ),
499 else if( !strncmp( psz_parser, "norm=", strlen( "norm=" ) ) )
501 psz_parser += strlen( "norm=" );
502 if( !strncmp( psz_parser, "pal", strlen( "pal" ) ) )
504 p_sys->i_norm = VIDEO_MODE_PAL;
505 psz_parser += strlen( "pal" );
507 else if( !strncmp( psz_parser, "ntsc", strlen( "ntsc" ) ) )
509 p_sys->i_norm = VIDEO_MODE_NTSC;
510 psz_parser += strlen( "ntsc" );
512 else if( !strncmp( psz_parser, "secam", strlen( "secam" ) ) )
514 p_sys->i_norm = VIDEO_MODE_SECAM;
515 psz_parser += strlen( "secam" );
517 else if( !strncmp( psz_parser, "auto", strlen( "auto" ) ) )
519 p_sys->i_norm = VIDEO_MODE_AUTO;
520 psz_parser += strlen( "auto" );
524 p_sys->i_norm = strtol( psz_parser, &psz_parser, 0 );
527 else if( !strncmp( psz_parser, "frequency=",
528 strlen( "frequency=" ) ) )
531 strtol( psz_parser + strlen( "frequency=" ),
533 if( p_sys->i_frequency < 30000 )
535 msg_Warn( p_input, "v4l syntax has changed : "
536 "'frequency' is now channel frequency in kHz");
539 else if( !strncmp( psz_parser, "audio=", strlen( "audio=" ) ) )
541 p_sys->i_audio = strtol( psz_parser + strlen( "audio=" ),
544 else if( !strncmp( psz_parser, "size=", strlen( "size=" ) ) )
546 psz_parser += strlen( "size=" );
547 if( !strncmp( psz_parser, "subqcif", strlen( "subqcif" ) ) )
549 p_sys->i_width = 128;
550 p_sys->i_height = 96;
552 else if( !strncmp( psz_parser, "qsif", strlen( "qsif" ) ) )
554 p_sys->i_width = 160;
555 p_sys->i_height = 120;
557 else if( !strncmp( psz_parser, "qcif", strlen( "qcif" ) ) )
559 p_sys->i_width = 176;
560 p_sys->i_height = 144;
562 else if( !strncmp( psz_parser, "sif", strlen( "sif" ) ) )
564 p_sys->i_width = 320;
565 p_sys->i_height = 244;
567 else if( !strncmp( psz_parser, "cif", strlen( "cif" ) ) )
569 p_sys->i_width = 352;
570 p_sys->i_height = 288;
572 else if( !strncmp( psz_parser, "vga", strlen( "vga" ) ) )
574 p_sys->i_width = 640;
575 p_sys->i_height = 480;
580 p_sys->i_width = strtol( psz_parser, &psz_parser, 0 );
581 if( *psz_parser == 'x' || *psz_parser == 'X')
583 p_sys->i_height = strtol( psz_parser + 1,
586 msg_Dbg( p_input, "WxH %dx%d", p_sys->i_width,
590 else if( !strncmp( psz_parser, "brightness=", strlen( "brightness=" ) ) )
592 p_sys->i_brightness = strtol( psz_parser + strlen( "brightness=" ),
595 else if( !strncmp( psz_parser, "colour=", strlen( "colour=" ) ) )
597 p_sys->i_colour = strtol( psz_parser + strlen( "colour=" ),
600 else if( !strncmp( psz_parser, "hue=", strlen( "hue=" ) ) )
602 p_sys->i_hue = strtol( psz_parser + strlen( "hue=" ),
605 else if( !strncmp( psz_parser, "contrast=", strlen( "contrast=" ) ) )
607 p_sys->i_contrast = strtol( psz_parser + strlen( "contrast=" ),
610 else if( !strncmp( psz_parser, "tuner=", strlen( "tuner=" ) ) )
612 p_sys->i_tuner = strtol( psz_parser + strlen( "tuner=" ),
615 else if( !strncmp( psz_parser, "adev=", strlen( "adev=" ) ) )
619 psz_parser += strlen( "adev=" );
620 if( strchr( psz_parser, ':' ) )
622 i_len = strchr( psz_parser, ':' ) - psz_parser;
626 i_len = strlen( psz_parser );
629 p_sys->psz_adev = strndup( psz_parser, i_len );
633 else if( !strncmp( psz_parser, "samplerate=",
634 strlen( "samplerate=" ) ) )
636 p_sys->i_sample_rate =
637 strtol( psz_parser + strlen( "samplerate=" ),
640 else if( !strncmp( psz_parser, "stereo", strlen( "stereo" ) ) )
642 psz_parser += strlen( "stereo" );
644 p_sys->b_stereo = VLC_TRUE;
646 else if( !strncmp( psz_parser, "mono", strlen( "mono" ) ) )
648 psz_parser += strlen( "mono" );
650 p_sys->b_stereo = VLC_FALSE;
652 else if( !strncmp( psz_parser, "mjpeg", strlen( "mjpeg" ) ) )
654 psz_parser += strlen( "mjpeg" );
656 p_sys->b_mjpeg = VLC_TRUE;
658 else if( !strncmp( psz_parser, "decimation=",
659 strlen( "decimation=" ) ) )
661 p_sys->i_decimation =
662 strtol( psz_parser + strlen( "decimation=" ),
665 else if( !strncmp( psz_parser, "quality=",
666 strlen( "quality=" ) ) )
669 strtol( psz_parser + strlen( "quality=" ),
672 else if( !strncmp( psz_parser, "fps=", strlen( "fps=" ) ) )
674 p_sys->f_fps = strtof( psz_parser + strlen( "fps=" ),
679 msg_Warn( p_input, "unknown option" );
682 while( *psz_parser && *psz_parser != ':' )
687 if( *psz_parser == '\0' )
696 p_sys->psz_device = strdup( psz_dup );
698 if( psz_dup ) free( psz_dup );
701 /*****************************************************************************
703 *****************************************************************************/
704 int OpenVideoDev( input_thread_t *p_input, char *psz_device )
706 access_sys_t *p_sys = p_input->p_access_data;
709 struct video_channel vid_channel;
710 struct mjpeg_params mjpeg;
713 if( ( i_fd = open( psz_device, O_RDWR ) ) < 0 )
715 msg_Err( p_input, "cannot open device (%s)", strerror( errno ) );
719 if( ioctl( i_fd, VIDIOCGCAP, &p_sys->vid_cap ) < 0 )
721 msg_Err( p_input, "cannot get capabilities (%s)", strerror( errno ) );
726 "V4L device %s %d channels %d audios %d < w < %d %d < h < %d",
728 p_sys->vid_cap.channels,
729 p_sys->vid_cap.audios,
730 p_sys->vid_cap.minwidth, p_sys->vid_cap.maxwidth,
731 p_sys->vid_cap.minheight, p_sys->vid_cap.maxheight );
733 if( p_sys->i_channel < 0 || p_sys->i_channel >= p_sys->vid_cap.channels )
735 msg_Dbg( p_input, "invalid channel, falling back on channel 0" );
736 p_sys->i_channel = 0;
738 if( p_sys->i_audio >= p_sys->vid_cap.audios )
740 msg_Dbg( p_input, "invalid audio, falling back with no audio" );
744 if( p_sys->i_width < p_sys->vid_cap.minwidth ||
745 p_sys->i_width > p_sys->vid_cap.maxwidth )
747 msg_Dbg( p_input, "invalid width %i", p_sys->i_width );
750 if( p_sys->i_height < p_sys->vid_cap.minheight ||
751 p_sys->i_height > p_sys->vid_cap.maxheight )
753 msg_Dbg( p_input, "invalid height %i", p_sys->i_height );
757 if( !( p_sys->vid_cap.type & VID_TYPE_CAPTURE ) )
759 msg_Err( p_input, "cannot grab" );
763 vid_channel.channel = p_sys->i_channel;
764 if( ioctl( i_fd, VIDIOCGCHAN, &vid_channel ) < 0 )
766 msg_Err( p_input, "cannot get channel infos (%s)",
771 "setting channel %s(%d) %d tuners flags=0x%x type=0x%x norm=0x%x",
779 if( p_sys->i_tuner >= vid_channel.tuners )
781 msg_Dbg( p_input, "invalid tuner, falling back on tuner 0" );
785 vid_channel.norm = p_sys->i_norm;
786 if( ioctl( i_fd, VIDIOCSCHAN, &vid_channel ) < 0 )
788 msg_Err( p_input, "cannot set channel (%s)", strerror( errno ) );
792 if( vid_channel.flags & VIDEO_VC_TUNER )
797 struct video_tuner vid_tuner;
798 if( p_sys->i_tuner >= 0 )
800 vid_tuner.tuner = p_sys->i_tuner;
801 if( ioctl( i_fd, VIDIOCGTUNER, &vid_tuner ) < 0 )
803 msg_Err( p_input, "cannot get tuner (%s)", strerror( errno ) );
806 msg_Dbg( p_input, "tuner %s low=%d high=%d, flags=0x%x "
807 "mode=0x%x signal=0x%x",
808 vid_tuner.name, vid_tuner.rangelow, vid_tuner.rangehigh,
809 vid_tuner.flags, vid_tuner.mode, vid_tuner.signal );
811 msg_Dbg( p_input, "setting tuner %s (%d)",
812 vid_tuner.name, vid_tuner.tuner );
814 /* FIXME FIXME to be checked FIXME FIXME */
815 //vid_tuner.mode = p_sys->i_norm;
816 if( ioctl( i_fd, VIDIOCSTUNER, &vid_tuner ) < 0 )
818 msg_Err( p_input, "cannot set tuner (%s)", strerror( errno ) );
824 /* Show a warning if frequency is < than 30000.
825 * User is certainly usint old syntax. */
829 if( p_sys->i_frequency >= 0 )
831 int driver_frequency = p_sys->i_frequency * 16 /1000;
832 if( ioctl( i_fd, VIDIOCSFREQ, &driver_frequency ) < 0 )
834 msg_Err( p_input, "cannot set frequency (%s)",
838 msg_Dbg( p_input, "frequency %d (%d)", p_sys->i_frequency,
844 if( vid_channel.flags & VIDEO_VC_AUDIO )
846 struct video_audio vid_audio;
848 /* XXX TODO volume, balance, ... */
849 if( p_sys->i_audio >= 0 )
851 vid_audio.audio = p_sys->i_audio;
852 if( ioctl( i_fd, VIDIOCGAUDIO, &vid_audio ) < 0 )
854 msg_Err( p_input, "cannot get audio (%s)", strerror( errno ) );
859 vid_audio.flags &= ~VIDEO_AUDIO_MUTE;
861 if( ioctl( i_fd, VIDIOCSAUDIO, &vid_audio ) < 0 )
863 msg_Err( p_input, "cannot set audio (%s)", strerror( errno ) );
870 /* establish basic params with input and norm before feeling width
874 struct quicktime_mjpeg_app1 *p_app1;
877 if( ioctl( i_fd, MJPIOC_G_PARAMS, &mjpeg ) < 0 )
879 msg_Err( p_input, "cannot get mjpeg params (%s)",
883 mjpeg.input = p_sys->i_channel;
884 mjpeg.norm = p_sys->i_norm;
885 mjpeg.decimation = p_sys->i_decimation;
888 mjpeg.img_width = p_sys->i_width / p_sys->i_decimation;
889 if( p_sys->i_height )
890 mjpeg.img_height = p_sys->i_height / p_sys->i_decimation;
892 /* establish Quicktime APP1 marker while we are here */
897 p_app1 = (struct quicktime_mjpeg_app1 *)mjpeg.APP_data;
898 p_app1->i_reserved = 0;
899 p_app1->i_tag = VLC_FOURCC( 'm','j','p','g' );
900 p_app1->i_field_size = 0;
901 p_app1->i_padded_field_size = 0;
902 p_app1->i_next_field = 0;
903 /* XXX WARNING XXX */
904 /* these's nothing magic about these values. We are dangerously
905 * assuming the encoder card is encoding mjpeg-a and is not throwing
906 * in marker tags we aren't expecting. It's bad enough we have to
907 * search through the jpeg output for every frame we grab just to
908 * find the first field's end marker, so we take this risk to boost
910 * This is really something the driver could do for us because this
911 * does conform to standards outside of Apple Quicktime.
914 p_app1->i_DQT_offset = hton32( i_offset );
916 p_app1->i_DHT_offset = hton32( i_offset );
918 p_app1->i_SOF_offset = hton32( i_offset );
920 p_app1->i_SOS_offset = hton32( i_offset );
922 p_app1->i_data_offset = hton32( i_offset );
924 /* SOF and SOS aren't specified by the mjpeg API because they aren't
925 * optional. They will be present in the output. */
926 mjpeg.jpeg_markers = JPEG_MARKER_DHT | JPEG_MARKER_DQT;
928 if( ioctl( i_fd, MJPIOC_S_PARAMS, &mjpeg ) < 0 )
930 msg_Err( p_input, "cannot set mjpeg params (%s)",
935 p_sys->i_width = mjpeg.img_width * mjpeg.HorDcm;
936 p_sys->i_height = mjpeg.img_height * mjpeg.VerDcm *
937 mjpeg.field_per_buff;
940 /* fix width/height */
941 if( !p_sys->b_mjpeg && ( p_sys->i_width == 0 || p_sys->i_height == 0 ) )
943 struct video_window vid_win;
945 if( ioctl( i_fd, VIDIOCGWIN, &vid_win ) < 0 )
947 msg_Err( p_input, "cannot get win (%s)", strerror( errno ) );
950 p_sys->i_width = vid_win.width;
951 p_sys->i_height = vid_win.height;
953 msg_Dbg( p_input, "will use %dx%d", p_sys->i_width, p_sys->i_height );
956 p_sys->p_video_frame = NULL;
958 if( !p_sys->b_mjpeg )
960 /* set hue/color/.. */
961 if( ioctl( i_fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 )
963 struct video_picture vid_picture = p_sys->vid_picture;
965 if( p_sys->i_brightness >= 0 && p_sys->i_brightness < 65536 )
967 vid_picture.brightness = p_sys->i_brightness;
969 if( p_sys->i_colour >= 0 && p_sys->i_colour < 65536 )
971 vid_picture.colour = p_sys->i_colour;
973 if( p_sys->i_hue >= 0 && p_sys->i_hue < 65536 )
975 vid_picture.hue = p_sys->i_hue;
977 if( p_sys->i_contrast >= 0 && p_sys->i_contrast < 65536 )
979 vid_picture.contrast = p_sys->i_contrast;
981 if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 )
983 msg_Dbg( p_input, "v4l device uses brightness: %d", vid_picture.brightness );
984 msg_Dbg( p_input, "v4l device uses colour: %d", vid_picture.colour );
985 msg_Dbg( p_input, "v4l device uses hue: %d", vid_picture.hue );
986 msg_Dbg( p_input, "v4l device uses contrast: %d", vid_picture.contrast );
987 p_sys->vid_picture = vid_picture;
991 /* Find out video format used by device */
992 if( ioctl( i_fd, VIDIOCGPICT, &p_sys->vid_picture ) == 0 )
994 struct video_picture vid_picture = p_sys->vid_picture;
998 vid_picture.palette = 0;
1001 var_Create( p_input, "v4l-chroma",
1002 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
1003 var_Get( p_input, "v4l-chroma", &val );
1004 if( val.psz_string && strlen( val.psz_string ) >= 4 )
1007 VLC_FOURCC( val.psz_string[0], val.psz_string[1],
1008 val.psz_string[2], val.psz_string[3] );
1010 /* Find out v4l chroma code */
1011 for( i = 0; v4lchroma_to_fourcc[i].i_v4l != 0; i++ )
1013 if( v4lchroma_to_fourcc[i].i_fourcc == i_chroma )
1015 vid_picture.palette = v4lchroma_to_fourcc[i].i_v4l;
1020 if( val.psz_string ) free( val.psz_string );
1022 if( vid_picture.palette &&
1023 !ioctl( i_fd, VIDIOCSPICT, &vid_picture ) )
1025 p_sys->vid_picture = vid_picture;
1029 /* Try to set the format to something easy to encode */
1030 vid_picture.palette = VIDEO_PALETTE_YUV420P;
1031 if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 )
1033 p_sys->vid_picture = vid_picture;
1037 vid_picture.palette = VIDEO_PALETTE_YUV422P;
1038 if( ioctl( i_fd, VIDIOCSPICT, &vid_picture ) == 0 )
1040 p_sys->vid_picture = vid_picture;
1045 /* Find out final format */
1046 for( i = 0; v4lchroma_to_fourcc[i].i_v4l != 0; i++ )
1048 if( v4lchroma_to_fourcc[i].i_v4l == p_sys->vid_picture.palette)
1050 p_sys->i_fourcc = v4lchroma_to_fourcc[i].i_fourcc;
1058 msg_Err( p_input, "ioctl VIDIOCGPICT failed" );
1063 if( p_sys->b_mjpeg )
1067 p_sys->mjpeg_buffers.count = 8;
1068 p_sys->mjpeg_buffers.size = MJPEG_BUFFER_SIZE;
1070 if( ioctl( i_fd, MJPIOC_REQBUFS, &p_sys->mjpeg_buffers ) < 0 )
1072 msg_Err( p_input, "mmap unsupported" );
1076 p_sys->p_video_mmap = mmap( 0,
1077 p_sys->mjpeg_buffers.size * p_sys->mjpeg_buffers.count,
1078 PROT_READ | PROT_WRITE, MAP_SHARED, i_fd, 0 );
1079 if( p_sys->p_video_mmap == MAP_FAILED )
1081 msg_Err( p_input, "mmap failed" );
1085 p_sys->i_fourcc = VLC_FOURCC( 'm','j','p','g' );
1086 p_sys->i_frame_pos = -1;
1088 /* queue up all the frames */
1089 for( i = 0; i < (int)p_sys->mjpeg_buffers.count; i++ )
1091 if( ioctl( i_fd, MJPIOC_QBUF_CAPT, &i ) < 0 )
1093 msg_Err( p_input, "unable to queue frame" );
1100 /* Fill in picture_t fields */
1101 vout_InitPicture( VLC_OBJECT(p_input), &p_sys->pic, p_sys->i_fourcc,
1102 p_sys->i_width, p_sys->i_height, p_sys->i_width *
1103 VOUT_ASPECT_FACTOR / p_sys->i_height );
1104 if( !p_sys->pic.i_planes )
1106 msg_Err( p_input, "unsupported chroma" );
1109 p_sys->i_video_frame_size = 0;
1110 for( i = 0; i < p_sys->pic.i_planes; i++ )
1112 p_sys->i_video_frame_size += p_sys->pic.p[i].i_lines *
1113 p_sys->pic.p[i].i_visible_pitch;
1116 msg_Dbg( p_input, "v4l device uses frame size: %i",
1117 p_sys->i_video_frame_size );
1118 msg_Dbg( p_input, "v4l device uses chroma: %4.4s",
1119 (char*)&p_sys->i_fourcc );
1121 /* Allocate mmap buffer */
1122 if( ioctl( i_fd, VIDIOCGMBUF, &p_sys->vid_mbuf ) < 0 )
1124 msg_Err( p_input, "mmap unsupported" );
1128 p_sys->p_video_mmap = mmap( 0, p_sys->vid_mbuf.size,
1129 PROT_READ|PROT_WRITE, MAP_SHARED,
1131 if( p_sys->p_video_mmap == MAP_FAILED )
1133 /* FIXME -> normal read */
1134 msg_Err( p_input, "mmap failed" );
1139 p_sys->vid_mmap.frame = 0;
1140 p_sys->vid_mmap.width = p_sys->i_width;
1141 p_sys->vid_mmap.height = p_sys->i_height;
1142 p_sys->vid_mmap.format = p_sys->vid_picture.palette;
1143 if( ioctl( i_fd, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 )
1145 msg_Warn( p_input, "%4.4s refused", (char*)&p_sys->i_fourcc );
1146 msg_Err( p_input, "chroma selection failed" );
1154 if( i_fd >= 0 ) close( i_fd );
1158 /*****************************************************************************
1160 *****************************************************************************/
1161 int OpenAudioDev( input_thread_t *p_input, char *psz_device )
1163 access_sys_t *p_sys = p_input->p_access_data;
1166 if( (i_fd = open( psz_device, O_RDONLY | O_NONBLOCK )) < 0 )
1168 msg_Err( p_input, "cannot open audio device (%s)", strerror( errno ) );
1172 i_format = AFMT_S16_LE;
1173 if( ioctl( i_fd, SNDCTL_DSP_SETFMT, &i_format ) < 0
1174 || i_format != AFMT_S16_LE )
1176 msg_Err( p_input, "cannot set audio format (16b little endian) "
1177 "(%s)", strerror( errno ) );
1181 if( ioctl( i_fd, SNDCTL_DSP_STEREO,
1182 &p_sys->b_stereo ) < 0 )
1184 msg_Err( p_input, "cannot set audio channels count (%s)",
1185 strerror( errno ) );
1189 if( ioctl( i_fd, SNDCTL_DSP_SPEED,
1190 &p_sys->i_sample_rate ) < 0 )
1192 msg_Err( p_input, "cannot set audio sample rate (%s)",
1193 strerror( errno ) );
1197 msg_Dbg( p_input, "openened adev=`%s' %s %dHz",
1198 psz_device, p_sys->b_stereo ? "stereo" : "mono",
1199 p_sys->i_sample_rate );
1201 p_sys->i_audio_frame_size = 0;
1202 p_sys->i_audio_frame_size_allocated = 6*1024;
1203 p_sys->p_audio_frame = malloc( p_sys->i_audio_frame_size_allocated );
1209 if( i_fd >= 0 ) close( i_fd );
1213 /*****************************************************************************
1214 * AccessClose: close device, free resources
1215 *****************************************************************************/
1216 static void AccessClose( vlc_object_t *p_this )
1218 input_thread_t *p_input = (input_thread_t *)p_this;
1219 access_sys_t *p_sys = p_input->p_access_data;
1221 if( p_sys->psz_device ) free( p_sys->psz_device );
1222 if( p_sys->psz_vdev ) free( p_sys->psz_vdev );
1223 if( p_sys->psz_adev ) free( p_sys->psz_adev );
1224 if( p_sys->fd_video >= 0 ) close( p_sys->fd_video );
1225 if( p_sys->fd_audio >= 0 ) close( p_sys->fd_audio );
1227 if( p_sys->p_header ) free( p_sys->p_header );
1228 if( p_sys->p_audio_frame ) free( p_sys->p_audio_frame );
1230 if( p_sys->b_mjpeg )
1233 ioctl( p_sys->fd_video, MJPIOC_QBUF_CAPT, &i_noframe );
1236 if( p_sys->p_video_mmap && p_sys->p_video_mmap != MAP_FAILED )
1238 if( p_sys->b_mjpeg )
1239 munmap( p_sys->p_video_mmap, p_sys->mjpeg_buffers.size *
1240 p_sys->mjpeg_buffers.count );
1242 munmap( p_sys->p_video_mmap, p_sys->vid_mbuf.size );
1248 /*****************************************************************************
1249 * GrabAudio: grab audio
1250 *****************************************************************************/
1251 static int GrabAudio( input_thread_t * p_input,
1256 access_sys_t *p_sys = p_input->p_access_data;
1257 struct audio_buf_info buf_info;
1261 i_read = read( p_sys->fd_audio, p_sys->p_audio_frame,
1262 p_sys->i_audio_frame_size_allocated );
1266 return VLC_EGENERIC;
1269 p_sys->i_audio_frame_size = i_read;
1271 /* from vls : correct the date because of kernel buffering */
1273 if( ioctl( p_sys->fd_audio, SNDCTL_DSP_GETISPACE, &buf_info ) == 0 )
1275 i_correct += buf_info.bytes;
1279 *pp_data = p_sys->p_audio_frame;
1280 *pi_data = p_sys->i_audio_frame_size;
1281 *pi_pts = mdate() - (mtime_t)1000000 * (mtime_t)i_correct /
1282 2 / ( p_sys->b_stereo ? 2 : 1) / p_sys->i_sample_rate;
1286 /*****************************************************************************
1288 *****************************************************************************/
1289 static uint8_t *GrabCapture( input_thread_t *p_input )
1291 access_sys_t *p_sys = p_input->p_access_data;
1292 int i_captured_frame = p_sys->i_frame_pos;
1294 p_sys->vid_mmap.frame = (p_sys->i_frame_pos + 1) % p_sys->vid_mbuf.frames;
1296 while( ioctl( p_sys->fd_video, VIDIOCMCAPTURE, &p_sys->vid_mmap ) < 0 )
1298 if( errno != EAGAIN )
1300 msg_Err( p_input, "failed capturing new frame" );
1304 if( p_input->b_die )
1309 msg_Dbg( p_input, "grab failed, trying again" );
1312 while( ioctl(p_sys->fd_video, VIDIOCSYNC, &p_sys->i_frame_pos) < 0 )
1314 if( errno != EAGAIN && errno != EINTR )
1316 msg_Err( p_input, "failed syncing new frame" );
1321 p_sys->i_frame_pos = p_sys->vid_mmap.frame;
1322 /* leave i_video_frame_size alone */
1323 return p_sys->p_video_mmap + p_sys->vid_mbuf.offsets[i_captured_frame];
1326 static uint8_t *GrabMJPEG( input_thread_t *p_input )
1328 access_sys_t *p_sys = p_input->p_access_data;
1329 struct mjpeg_sync sync;
1330 uint8_t *p_frame, *p_field, *p;
1333 struct quicktime_mjpeg_app1 *p_app1 = NULL;
1335 /* re-queue the last frame we sync'd */
1336 if( p_sys->i_frame_pos != -1 )
1338 while( ioctl( p_sys->fd_video, MJPIOC_QBUF_CAPT,
1339 &p_sys->i_frame_pos ) < 0 )
1341 if( errno != EAGAIN && errno != EINTR )
1343 msg_Err( p_input, "failed capturing new frame" );
1349 /* sync on the next frame */
1350 while( ioctl( p_sys->fd_video, MJPIOC_SYNC, &sync ) < 0 )
1352 if( errno != EAGAIN && errno != EINTR )
1354 msg_Err( p_input, "failed syncing new frame" );
1359 p_sys->i_frame_pos = sync.frame;
1360 p_frame = p_sys->p_video_mmap + p_sys->mjpeg_buffers.size * sync.frame;
1362 /* p_frame now points to the data. fix up the Quicktime APP1 marker */
1364 tag = hton16( tag );
1368 p = memmem( p_field, sync.length, &tag, 2 );
1372 p += 2; /* data immediately following EOI */
1374 p_app1 = (struct quicktime_mjpeg_app1 *)(p_field + 6);
1376 i_size = ((uint32_t)(p - p_field));
1377 i_size = hton32( i_size );
1378 memcpy( &p_app1->i_field_size, &i_size, 4 );
1380 while( *p == 0xff && *(p+1) == 0xff )
1383 i_size = ((uint32_t)(p - p_field));
1384 i_size = hton32( i_size );
1385 memcpy( &p_app1->i_padded_field_size, &i_size, 4 );
1389 tag = hton16( tag );
1390 p_field = memmem( p, sync.length - (size_t)(p - p_frame), &tag, 2 );
1394 i_size = (uint32_t)(p_field - p_frame);
1395 i_size = hton32( i_size );
1396 memcpy( &p_app1->i_next_field, &i_size, 4 );
1399 p_app1 = (struct quicktime_mjpeg_app1 *)(p_field + 6);
1401 tag = hton16( tag );
1402 p = memmem( p_field, sync.length - (size_t)(p_field - p_frame),
1407 /* sometimes the second field doesn't have the EOI. just put it
1410 p = p_frame + sync.length;
1411 memcpy( p, &tag, 2 );
1416 i_size = (uint32_t)(p - p_field);
1417 i_size = hton32( i_size );
1418 memcpy( &p_app1->i_field_size, &i_size, 4 );
1419 i_size = (uint32_t)(sync.length - (uint32_t)(p_field - p_frame));
1420 i_size = hton32( i_size );
1421 memcpy( &p_app1->i_padded_field_size, &i_size, 4 );
1424 p_sys->i_video_frame_size = sync.length;
1428 static int GrabVideo( input_thread_t * p_input,
1433 access_sys_t *p_sys = p_input->p_access_data;
1437 if( p_sys->f_fps >= 0.1 && p_sys->i_video_pts > 0 )
1439 mtime_t i_dur = (mtime_t)((double)1000000 / (double)p_sys->f_fps);
1441 /* Did we wait long enougth ? */
1442 if( p_sys->i_video_pts + i_dur > mdate() )
1444 return VLC_ETIMEOUT;
1448 if( p_sys->b_mjpeg )
1449 p_frame = GrabMJPEG( p_input );
1451 p_frame = GrabCapture( p_input );
1454 return VLC_EGENERIC;
1456 p_sys->i_video_pts = mdate();
1457 p_sys->p_video_frame = p_frame;
1459 *pp_data = p_sys->p_video_frame;
1460 *pi_data = p_sys->i_video_frame_size;
1461 *pi_pts = p_sys->i_video_pts;
1466 /*****************************************************************************
1467 * Read: reads from the device into PES packets.
1468 *****************************************************************************
1469 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
1471 *****************************************************************************/
1472 static int Read( input_thread_t * p_input, byte_t * p_buffer, size_t i_len )
1474 access_sys_t *p_sys = p_input->p_access_data;
1481 /* First copy header if any */
1482 if( p_sys->i_header_pos < p_sys->i_header_size )
1486 i_copy = __MIN( p_sys->i_header_size - p_sys->i_header_pos,
1488 memcpy( p_buffer, &p_sys->p_header[p_sys->i_header_pos], i_copy );
1489 p_sys->i_header_pos += i_copy;
1497 if( i_len > 0 && p_sys->i_data_pos < p_sys->i_data_size )
1501 i_copy = __MIN( p_sys->i_data_size - p_sys->i_data_pos,
1504 memcpy( p_buffer, &p_sys->p_data[p_sys->i_data_pos], i_copy );
1505 p_sys->i_data_pos += i_copy;
1512 /* The caller got what he wanted */
1518 /* Read no more than one frame at a time.
1519 * That kills latency, especially for encoded v4l streams */
1520 if( p_sys->i_data_size && p_sys->i_data_pos == p_sys->i_data_size )
1522 p_sys->i_data_pos = 0; p_sys->i_data_size = 0;
1526 /* Re-fill data by grabbing audio/video */
1527 p_sys->i_data_pos = p_sys->i_data_size = 0;
1529 /* Try grabbing audio frames first */
1530 i_stream = p_sys->i_streams - 1;
1531 if( p_sys->fd_audio < 0 ||
1532 GrabAudio( p_input, &p_sys->p_data,
1533 &p_sys->i_data_size, &i_pts ) != VLC_SUCCESS )
1535 int i_ret = VLC_ETIMEOUT;
1537 /* Try grabbing video frame */
1539 if( p_sys->fd_video > 0 )
1541 i_ret = GrabVideo( p_input, &p_sys->p_data,
1542 &p_sys->i_data_size, &i_pts );
1545 /* No video or timeout */
1546 if( i_ret == VLC_ETIMEOUT )
1548 /* Sleep so we do not consume all the cpu, 10ms seems
1549 * like a good value (100fps) */
1553 else if( i_ret != VLC_SUCCESS )
1555 msg_Err( p_input, "Error during capture!" );
1560 /* create pseudo header */
1561 p_sys->i_header_size = 16;
1562 p_sys->i_header_pos = 0;
1563 SetDWBE( &p_sys->p_header[0], i_stream );
1564 SetDWBE( &p_sys->p_header[4], p_sys->i_data_size );
1565 SetQWBE( &p_sys->p_header[8], i_pts );
1573 /*****************************************************************************
1574 * Demux: local prototypes
1575 *****************************************************************************/
1582 static int Demux ( input_thread_t * );
1584 /****************************************************************************
1586 ****************************************************************************/
1587 static int DemuxOpen( vlc_object_t *p_this )
1589 input_thread_t *p_input = (input_thread_t *)p_this;
1596 /* a little test to see if it's a v4l stream */
1597 if( stream_Peek( p_input->s, &p_peek, 8 ) < 8 )
1599 return VLC_EGENERIC;
1602 if( strncmp( p_peek, ".v4l", 4 ) || ( i_es = GetDWBE( &p_peek[4] ) ) <= 0 )
1604 return VLC_EGENERIC;
1607 vlc_mutex_lock( &p_input->stream.stream_lock );
1608 if( input_InitStream( p_input, 0 ) == -1)
1610 vlc_mutex_unlock( &p_input->stream.stream_lock );
1611 msg_Err( p_input, "cannot init stream" );
1612 return VLC_EGENERIC;
1614 p_input->stream.i_mux_rate = 0 / 50;
1615 vlc_mutex_unlock( &p_input->stream.stream_lock );
1617 p_input->pf_demux = Demux;
1618 p_input->pf_demux_control = demux_vaControlDefault;
1619 p_input->p_demux_data = p_sys = malloc( sizeof( demux_sys_t ) );
1623 if( stream_Peek( p_input->s, &p_peek, 8 + 20 * i_es ) < 8 + 20 * i_es )
1625 msg_Err( p_input, "v4l plugin discarded (cannot peek)" );
1626 return VLC_EGENERIC;
1630 for( i = 0; i < i_es; i++ )
1634 if( !strncmp( p_peek, "auds", 4 ) )
1636 es_format_Init( &fmt, AUDIO_ES,
1637 VLC_FOURCC( p_peek[4], p_peek[5], p_peek[6], p_peek[7] ) );
1639 fmt.audio.i_channels = GetDWBE( &p_peek[8] );
1640 fmt.audio.i_rate = GetDWBE( &p_peek[12] );
1641 fmt.audio.i_bitspersample = GetDWBE( &p_peek[16] );
1642 fmt.audio.i_blockalign = fmt.audio.i_channels *
1643 fmt.audio.i_bitspersample / 8;
1644 fmt.i_bitrate = fmt.audio.i_channels *
1646 fmt.audio.i_bitspersample;
1648 msg_Dbg( p_input, "new audio es %d channels %dHz",
1649 fmt.audio.i_channels, fmt.audio.i_rate );
1651 TAB_APPEND( p_sys->i_es, p_sys->es,
1652 es_out_Add( p_input->p_es_out, &fmt ) );
1654 else if( !strncmp( p_peek, "vids", 4 ) )
1656 es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC( p_peek[4], p_peek[5],
1657 p_peek[6], p_peek[7] ) );
1658 fmt.video.i_width = GetDWBE( &p_peek[8] );
1659 fmt.video.i_height = GetDWBE( &p_peek[12] );
1661 msg_Dbg( p_input, "added new video es %4.4s %dx%d",
1662 (char*)&fmt.i_codec,
1663 fmt.video.i_width, fmt.video.i_height );
1664 TAB_APPEND( p_sys->i_es, p_sys->es,
1665 es_out_Add( p_input->p_es_out, &fmt ) );
1672 stream_Read( p_input->s, NULL, 8 + 20 * i_es );
1677 /****************************************************************************
1679 ****************************************************************************/
1680 static void DemuxClose( vlc_object_t *p_this )
1682 input_thread_t *p_input = (input_thread_t *)p_this;
1683 demux_sys_t *p_sys = p_input->p_demux_data;
1685 if( p_sys->i_es > 0 )
1692 /****************************************************************************
1694 ****************************************************************************/
1695 static int Demux( input_thread_t *p_input )
1697 demux_sys_t *p_sys = p_input->p_demux_data;
1706 if( stream_Peek( p_input->s, &p_peek, 16 ) < 16 )
1708 msg_Warn( p_input, "cannot peek (EOF ?)" );
1712 i_es = GetDWBE( &p_peek[0] );
1713 if( i_es < 0 || i_es >= p_sys->i_es )
1715 msg_Err( p_input, "cannot find ES" );
1719 i_size = GetDWBE( &p_peek[4] );
1720 i_pts = GetQWBE( &p_peek[8] );
1722 if( ( p_block = stream_Block( p_input->s, 16 + i_size ) ) == NULL )
1724 msg_Warn( p_input, "cannot read data" );
1728 p_block->p_buffer += 16;
1729 p_block->i_buffer -= 16;
1732 p_block->i_pts = i_pts + p_input->i_pts_delay;
1734 es_out_Send( p_input->p_es_out, p_sys->es[i_es], p_block );