1 /*****************************************************************************
2 * dc1394.c: firewire input module
3 *****************************************************************************
4 * Copyright (C) 2006-2009 VideoLAN
6 * Authors: Xant Majere <xant@xant.net>
7 * Rob Shortt <rob@tvcentric.com> - libdc1394 V2 API updates
8 * Frederic Benoist <fridorik@gmail.com> - updates from Rob's work
10 *****************************************************************************
11 * This library is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation;
14 * version 2 of the License.
16 * This library 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 GNU
19 * Lesser General Public License for more details.
21 * You should have received a copy of the GNU Lesser General Public
22 * License along with this library; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_input.h>
38 #include <vlc_demux.h>
39 #include <vlc_charset.h>
40 #include <vlc_picture.h>
47 #elif defined( WIN32 ) && !defined( UNDER_CE )
51 #include <sys/ioctl.h>
52 #include <sys/soundcard.h>
54 #include <libraw1394/raw1394.h>
55 #include <dc1394/dc1394.h>
57 #define MAX_IEEE1394_HOSTS 32
58 #define MAX_CAMERA_NODES 32
60 /*****************************************************************************
62 *****************************************************************************/
63 static int Open ( vlc_object_t * );
64 static void Close( vlc_object_t * );
65 static int OpenAudioDev( demux_t *p_demux );
66 static inline void CloseAudioDev( demux_t *p_demux );
69 set_description( N_("dc1394 input") )
70 set_capability( "access_demux", 10 )
71 add_shortcut( "dc1394" )
72 set_callbacks( Open, Close )
78 dc1394_t *p_dccontext;
80 dc1394camera_t *camera;
82 uint64_t selected_uid;
85 dc1394featureset_t features;
86 quadlet_t supported_framerates;
91 dc1394video_mode_t video_mode;
96 unsigned int brightness;
98 es_out_id_t *p_es_video;
99 dc1394video_frame_t *frame;
104 int i_audio_max_frame_size;
107 es_out_id_t *p_es_audio;
110 /*****************************************************************************
112 *****************************************************************************/
113 static int Demux( demux_t *p_demux );
114 static int Control( demux_t *, int, va_list );
115 static block_t *GrabVideo( demux_t *p_demux );
116 static block_t *GrabAudio( demux_t *p_demux );
117 static int process_options( demux_t *p_demux);
119 /*****************************************************************************
121 *****************************************************************************/
122 static int FindCamera( demux_sys_t *sys, demux_t *p_demux )
124 dc1394camera_list_t *list;
125 int i_ret = VLC_EGENERIC;
127 msg_Dbg( p_demux, "Scanning for ieee1394 ports ..." );
129 if( dc1394_camera_enumerate (sys->p_dccontext, &list) != DC1394_SUCCESS )
131 msg_Err(p_demux, "Can not ennumerate cameras");
137 msg_Err(p_demux, "Can not find cameras");
141 sys->num_cameras = list->num;
142 msg_Dbg( p_demux, "Found %d dc1394 cameras.", list->num);
144 if( sys->selected_uid )
147 for( unsigned i = 0; i < sys->num_cameras; i++ )
149 if( list->ids[i].guid == sys->selected_uid )
151 sys->camera = dc1394_camera_new(sys->p_dccontext,
159 msg_Err( p_demux, "Can't find camera with uid : 0x%llx.",
164 else if( sys->selected_camera >= (int)list->num )
166 msg_Err( p_demux, "There are not this many cameras. (%d/%d)",
167 sys->selected_camera, sys->num_cameras );
170 else if( sys->selected_camera >= 0 )
172 sys->camera = dc1394_camera_new(sys->p_dccontext,
173 list->ids[sys->selected_camera].guid);
177 sys->camera = dc1394_camera_new(sys->p_dccontext,
184 dc1394_camera_free_list (list);
188 /*****************************************************************************
190 *****************************************************************************/
191 static int Open( vlc_object_t *p_this )
193 demux_t *p_demux = (demux_t*)p_this;
200 if( strncmp(p_demux->psz_access, "dc1394", 6) != 0 )
204 p_demux->pf_demux = Demux;
205 p_demux->pf_control = Control;
206 p_demux->info.i_update = 0;
207 p_demux->info.i_title = 0;
208 p_demux->info.i_seekpoint = 0;
210 p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
214 memset( &fmt, 0, sizeof( es_format_t ) );
217 p_sys->video_mode = DC1394_VIDEO_MODE_640x480_YUV422;
220 p_sys->frame_rate = DC1394_FRAMERATE_15;
221 p_sys->brightness = 200;
223 p_sys->fd_audio = -1;
224 p_sys->p_dccontext = NULL;
225 p_sys->camera = NULL;
226 p_sys->selected_camera = -1;
227 p_sys->dma_buffers = 1;
228 p_sys->reset_bus = 0;
230 /* PROCESS INPUT OPTIONS */
231 if( process_options(p_demux) != VLC_SUCCESS )
233 msg_Err( p_demux, "Bad MRL, please check the option line "
240 p_sys->p_dccontext = dc1394_new();
241 if( !p_sys->p_dccontext )
243 msg_Err( p_demux, "Failed to initialise libdc1394");
248 if( FindCamera( p_sys, p_demux ) != VLC_SUCCESS )
250 dc1394_free( p_sys->p_dccontext );
257 msg_Err( p_demux, "No camera found !!" );
258 dc1394_free( p_sys->p_dccontext );
263 if( p_sys->reset_bus )
265 if( dc1394_reset_bus( p_sys->camera ) != DC1394_SUCCESS )
267 msg_Err( p_demux, "Unable to reset IEEE 1394 bus");
271 else msg_Dbg( p_demux, "Successfully reset IEEE 1394 bus");
274 if( dc1394_camera_reset( p_sys->camera ) != DC1394_SUCCESS )
276 msg_Err( p_demux, "Unable to reset camera");
281 if( dc1394_camera_print_info( p_sys->camera,
282 stderr ) != DC1394_SUCCESS )
284 msg_Err( p_demux, "Unable to print camera info");
289 if( dc1394_feature_get_all( p_sys->camera,
290 &p_sys->features ) != DC1394_SUCCESS )
292 msg_Err( p_demux, "Unable to get feature set");
296 // TODO: only print features if verbosity increased
297 dc1394_feature_print_all(&p_sys->features, stderr);
299 #if 0 //"Note that you need to execute this function only if you use exotic video1394 device names. /dev/video1394, /dev/video1394/* and /dev/video1394-* are automatically recognized." http://damien.douxchamps.net/ieee1394/libdc1394/v2.x/api/capture/
300 if( p_sys->video_device )
302 if( dc1394_capture_set_device_filename( p_sys->camera,
303 p_sys->video_device ) != DC1394_SUCCESS )
305 msg_Err( p_demux, "Unable to set video device");
314 if( dc1394_feature_set_value( p_sys->camera,
315 DC1394_FEATURE_FOCUS,
316 p_sys->focus ) != DC1394_SUCCESS )
318 msg_Err( p_demux, "Unable to set initial focus to %u",
322 msg_Dbg( p_demux, "Initial focus set to %u", p_sys->focus );
325 if( dc1394_feature_set_value( p_sys->camera,
326 DC1394_FEATURE_FOCUS,
327 p_sys->brightness ) != DC1394_SUCCESS )
329 msg_Err( p_demux, "Unable to set initial brightness to %u",
333 msg_Dbg( p_demux, "Initial brightness set to %u", p_sys->brightness );
335 if( dc1394_video_set_framerate( p_sys->camera,
336 p_sys->frame_rate ) != DC1394_SUCCESS )
338 msg_Err( p_demux, "Unable to set framerate");
343 if( dc1394_video_set_mode( p_sys->camera,
344 p_sys->video_mode ) != DC1394_SUCCESS )
346 msg_Err( p_demux, "Unable to set video mode");
351 if( dc1394_video_set_iso_speed( p_sys->camera,
352 DC1394_ISO_SPEED_400 ) != DC1394_SUCCESS )
354 msg_Err( p_demux, "Unable to set iso speed");
359 /* and setup capture */
360 res = dc1394_capture_setup( p_sys->camera,
362 DC1394_CAPTURE_FLAGS_DEFAULT);
363 if( res != DC1394_SUCCESS )
365 if( res == DC1394_NO_BANDWIDTH )
367 msg_Err( p_demux ,"No bandwidth: try adding the "
368 "\"resetbus\" option" );
372 msg_Err( p_demux ,"Unable to setup capture" );
378 /* TODO - UYV444 chroma converter is missing, when it will be available
379 * fourcc will become variable (and not just a fixed value for UYVY)
381 i_width = p_sys->width;
382 i_height = p_sys->height;
384 if( picture_Setup( &p_sys->pic, VLC_CODEC_UYVY,
386 i_width * VOUT_ASPECT_FACTOR / i_height ) )
388 msg_Err( p_demux ,"unknown chroma" );
393 es_format_Init( &fmt, VIDEO_ES, VLC_CODEC_UYVY );
395 fmt.video.i_width = i_width;
396 fmt.video.i_height = i_height;
398 msg_Dbg( p_demux, "Added new video es %4.4s %dx%d",
399 (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
401 p_sys->p_es_video = es_out_Add( p_demux->out, &fmt );
403 if( p_sys->audio_device )
405 if( OpenAudioDev( p_demux ) == VLC_SUCCESS )
408 es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_S16L ); /* FIXME: hmm, ?? */
410 fmt.audio.i_channels = p_sys->channels ? p_sys->channels : 1;
411 fmt.audio.i_rate = p_sys->i_sample_rate;
412 fmt.audio.i_bitspersample = 16;
413 fmt.audio.i_blockalign = fmt.audio.i_channels *
414 fmt.audio.i_bitspersample / 8;
415 fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate *
416 fmt.audio.i_bitspersample;
418 msg_Dbg( p_demux, "New audio es %d channels %dHz",
419 fmt.audio.i_channels, fmt.audio.i_rate );
421 p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt );
425 /* have the camera start sending us data */
426 if( dc1394_video_set_transmission( p_sys->camera,
427 DC1394_ON ) != DC1394_SUCCESS )
429 msg_Err( p_demux, "Unable to start camera iso transmission" );
430 dc1394_capture_stop( p_sys->camera );
434 msg_Dbg( p_demux, "Set iso transmission" );
435 // TODO: reread camera
439 static int OpenAudioDev( demux_t *p_demux )
441 demux_sys_t *p_sys = p_demux->p_sys;
442 char *psz_device = p_sys->audio_device;
443 int i_format = AFMT_S16_LE;
446 p_sys->fd_audio = utf8_open( psz_device, O_RDONLY | O_NONBLOCK );
447 if( p_sys->fd_audio < 0 )
449 msg_Err( p_demux, "Cannot open audio device (%s)", psz_device );
453 if( !p_sys->i_sample_rate )
454 p_sys->i_sample_rate = 44100;
456 result = ioctl( p_sys->fd_audio, SNDCTL_DSP_SETFMT, &i_format );
457 if( (result < 0) || (i_format != AFMT_S16_LE) )
459 msg_Err( p_demux, "Cannot set audio format (16b little endian) "
464 result = ioctl( p_sys->fd_audio, SNDCTL_DSP_CHANNELS, &p_sys->channels );
467 msg_Err( p_demux, "Cannot set audio channels count (%d)",
472 result = ioctl( p_sys->fd_audio, SNDCTL_DSP_SPEED, &p_sys->i_sample_rate );
475 msg_Err( p_demux, "Cannot set audio sample rate (%d)",
476 p_sys->i_sample_rate );
480 msg_Dbg( p_demux, "Opened adev=`%s' %s %dHz",
482 (p_sys->channels > 1) ? "stereo" : "mono",
483 p_sys->i_sample_rate );
485 p_sys->i_audio_max_frame_size = 32 * 1024;
490 CloseAudioDev( p_demux );
491 p_sys->fd_audio = -1;
495 static inline void CloseAudioDev( demux_t *p_demux )
497 demux_sys_t *p_sys = p_demux->p_sys;
499 if( p_sys->fd_audio >= 0 )
500 close( p_sys->fd_audio );
503 /*****************************************************************************
505 *****************************************************************************/
506 static void Close( vlc_object_t *p_this )
508 demux_t *p_demux = (demux_t*)p_this;
509 demux_sys_t *p_sys = p_demux->p_sys;
511 /* Stop data transmission */
512 if( dc1394_video_set_transmission( p_sys->camera,
513 DC1394_OFF ) != DC1394_SUCCESS )
514 msg_Err( p_demux, "Unable to stop camera iso transmission" );
517 dc1394_capture_stop( p_sys->camera );
519 CloseAudioDev( p_demux );
521 dc1394_camera_free(p_sys->camera);
522 dc1394_free(p_sys->p_dccontext);
524 free( p_sys->video_device );
525 free( p_sys->audio_device );
530 static void MovePixelUYVY( void *src, int spos, void *dst, int dpos )
536 sc = (u_char *)src + (spos*2);
549 dc = (u_char *)dst+(dpos*2);
563 /*****************************************************************************
565 *****************************************************************************/
566 static block_t *GrabVideo( demux_t *p_demux )
568 demux_sys_t *p_sys = p_demux->p_sys;
569 block_t *p_block = NULL;
571 if( dc1394_capture_dequeue( p_sys->camera,
572 DC1394_CAPTURE_POLICY_WAIT,
573 &p_sys->frame ) != DC1394_SUCCESS )
575 msg_Err( p_demux, "Unable to capture a frame" );
579 p_block = block_New( p_demux, p_sys->frame->size[0] *
580 p_sys->frame->size[1] * 2 );
583 msg_Err( p_demux, "Can not get block" );
587 if( !p_sys->frame->image )
589 msg_Err (p_demux, "Capture buffer empty");
590 block_Release( p_block );
594 memcpy( p_block->p_buffer, (const char *)p_sys->frame->image,
595 p_sys->width * p_sys->height * 2 );
597 p_block->i_pts = p_block->i_dts = mdate();
598 dc1394_capture_enqueue( p_sys->camera, p_sys->frame );
602 static block_t *GrabAudio( demux_t *p_demux )
604 demux_sys_t *p_sys = p_demux->p_sys;
605 struct audio_buf_info buf_info;
606 block_t *p_block = NULL;
611 p_block = block_New( p_demux, p_sys->i_audio_max_frame_size );
614 msg_Warn( p_demux, "Cannot get buffer" );
618 i_read = read( p_sys->fd_audio, p_block->p_buffer,
619 p_sys->i_audio_max_frame_size );
623 block_Release( p_block );
627 p_block->i_buffer = i_read;
629 /* Correct the date because of kernel buffering */
631 result = ioctl( p_sys->fd_audio, SNDCTL_DSP_GETISPACE, &buf_info );
633 i_correct += buf_info.bytes;
635 p_block->i_pts = p_block->i_dts =
636 mdate() - INT64_C(1000000) * (mtime_t)i_correct /
637 2 / p_sys->channels / p_sys->i_sample_rate;
641 static int Demux( demux_t *p_demux )
643 demux_sys_t *p_sys = p_demux->p_sys;
644 block_t *p_blocka = NULL;
645 block_t *p_blockv = NULL;
647 /* Try grabbing audio frames first */
648 if( p_sys->fd_audio >= 0 )
649 p_blocka = GrabAudio( p_demux );
651 /* Try grabbing video frame */
652 p_blockv = GrabVideo( p_demux );
654 if( !p_blocka && !p_blockv )
656 /* Sleep so we do not consume all the cpu, 10ms seems
657 * like a good value (100fps)
665 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_blocka->i_pts );
666 es_out_Send( p_demux->out, p_sys->p_es_audio, p_blocka );
671 es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_blockv->i_pts );
672 es_out_Send( p_demux->out, p_sys->p_es_video, p_blockv );
677 /*****************************************************************************
679 *****************************************************************************/
680 static int Control( demux_t *p_demux, int i_query, va_list args )
682 VLC_UNUSED( p_demux );
685 /* Special for access_demux */
686 case DEMUX_CAN_PAUSE:
688 case DEMUX_SET_PAUSE_STATE:
689 case DEMUX_CAN_CONTROL_PACE:
690 *va_arg( args, bool * ) = false;
693 case DEMUX_GET_PTS_DELAY:
694 *va_arg( args, int64_t * ) = (int64_t)DEFAULT_PTS_DELAY;
698 *va_arg( args, int64_t * ) = mdate();
701 /* TODO implement others */
708 static int process_options( demux_t *p_demux )
710 demux_sys_t *p_sys = p_demux->p_sys;
715 const char *in_size = NULL;
716 const char *in_fmt = NULL;
719 psz_dup = strdup( p_demux->psz_path );
720 psz_parser = psz_dup;
721 for( token = strtok_r( psz_parser,":",&state); token;
722 token = strtok_r( NULL, ":", &state ) )
724 if( strncmp( token, "size=", strlen("size=") ) == 0 )
726 token += strlen("size=");
727 if( strncmp( token, "160x120", 7 ) == 0 )
729 /* TODO - UYV444 chroma converter is needed ...
730 * video size of 160x120 is temporarily disabled
733 "video size of 160x120 is actually disabled for lack of"
734 "chroma support. It will relased ASAP, until then try "
735 "an higher size (320x240 and 640x480 are fully supported)" );
744 else if( strncmp( token, "320x240", 7 ) == 0 )
750 else if( strncmp( token, "640x480", 7 ) == 0 )
759 "This program currently suppots frame sizes of"
760 " 160x120, 320x240, and 640x480. "
761 "Please specify one of them. You have specified %s.",
766 msg_Dbg( p_demux, "Requested video size : %s",token );
768 if( strncmp( token, "format=", strlen("format=") ) == 0 )
770 token += strlen("format=");
771 if( strncmp( token, "YUV411", 6 ) == 0 )
775 else if( strncmp( token, "YUV422", 6 ) == 0 )
779 else if( strncmp( token, "YUV444", 6 ) == 0 )
783 else if( strncmp( token, "RGB8", 4 ) == 0 )
787 else if( strncmp( token, "MONO8", 5 ) == 0 )
791 else if( strncmp( token, "MONO16", 6 ) == 0 )
797 msg_Err( p_demux, "Invalid format %s.", token );
801 msg_Dbg( p_demux, "Requested video format : %s", token );
803 else if( strncmp( token, "fps=", strlen( "fps=" ) ) == 0 )
805 token += strlen("fps=");
806 sscanf( token, "%g", &rate_f );
807 if( rate_f == 1.875 )
808 p_sys->frame_rate = DC1394_FRAMERATE_1_875;
809 else if( rate_f == 3.75 )
810 p_sys->frame_rate = DC1394_FRAMERATE_3_75;
811 else if( rate_f == 7.5 )
812 p_sys->frame_rate = DC1394_FRAMERATE_7_5;
813 else if( rate_f == 15 )
814 p_sys->frame_rate = DC1394_FRAMERATE_15;
815 else if( rate_f == 30 )
816 p_sys->frame_rate = DC1394_FRAMERATE_30;
817 else if( rate_f == 60 )
818 p_sys->frame_rate = DC1394_FRAMERATE_60;
822 "This program supports framerates of"
823 " 1.875, 3.75, 7.5, 15, 30, 60. "
824 "Please specify one of them. You have specified %s.",
829 msg_Dbg( p_demux, "Requested frame rate : %s",token );
831 else if( strncmp( token, "resetbus", strlen( "resetbus" ) ) == 0 )
833 token += strlen("resetbus");
834 p_sys->reset_bus = 1;
836 else if( strncmp( token, "brightness=", strlen( "brightness=" ) ) == 0 )
839 token += strlen("brightness=");
840 nr = sscanf( token, "%u", &p_sys->brightness);
843 msg_Err( p_demux, "Bad brightness value '%s', "
844 "must be an unsigned integer.",
850 else if( strncmp( token, "buffers=", strlen( "buffers=" ) ) == 0 )
854 token += strlen("buffers=");
855 nr = sscanf( token, "%d", &in_buf);
856 if( nr != 1 || in_buf < 1 )
858 msg_Err( p_demux, "DMA buffers must be 1 or greater." );
862 else p_sys->dma_buffers = in_buf;
865 // NOTE: If controller support is added back, more logic will needed to be added
866 // after the cameras are scanned.
867 else if( strncmp( token, "controller=", strlen( "controller=" ) ) == 0 )
870 token += strlen("controller=");
871 nr = sscanf( token, "%u", &p_sys->controller );
874 msg_Err(p_demux, "Bad controller value '%s', "
875 "must be an unsigned integer.",
881 else if( strncmp( token, "camera=", strlen( "camera=" ) ) == 0 )
884 token += strlen("camera=");
885 nr = sscanf(token,"%u",&p_sys->selected_camera);
888 msg_Err( p_demux, "Bad camera number '%s', "
889 "must be an unsigned integer.",
895 else if( strncmp( token, "vdev=", strlen( "vdev=" ) ) == 0)
897 token += strlen("vdev=");
898 p_sys->video_device = strdup(token);
899 msg_Dbg( p_demux, "Using video device '%s'.", token );
901 else if( strncmp( token, "adev=", strlen( "adev=" ) ) == 0 )
903 token += strlen("adev=");
904 p_sys->audio_device = strdup(token);
905 msg_Dbg( p_demux, "Using audio device '%s'.", token );
907 else if( strncmp( token, "samplerate=", strlen( "samplerate=" ) ) == 0 )
909 token += strlen("samplerate=");
910 sscanf( token, "%d", &p_sys->i_sample_rate );
912 else if( strncmp( token, "channels=", strlen("channels=" ) ) == 0 )
914 token += strlen("channels=");
915 sscanf( token, "%d", &p_sys->channels );
917 else if( strncmp( token, "focus=", strlen("focus=" ) ) == 0)
920 token += strlen("focus=");
921 nr = sscanf( token, "%u", &p_sys->focus );
924 msg_Err( p_demux, "Bad focus value '%s', "
925 "must be an unsigned integer.",
931 else if( strncmp( token, "uid=", strlen("uid=") ) == 0)
933 token += strlen("uid=");
934 sscanf( token, "0x%llx", &p_sys->selected_uid );
938 // The mode is a combination of size and format and not every format
939 // is supported by every size.
942 if( strcmp( in_size, "160x120") == 0)
944 if( in_fmt && (strcmp( in_fmt, "YUV444") != 0) )
945 msg_Err(p_demux, "160x120 only supports YUV444 - forcing");
946 p_sys->video_mode = DC1394_VIDEO_MODE_160x120_YUV444;
948 else if( strcmp( in_size, "320x240") == 0)
950 if( in_fmt && (strcmp( in_fmt, "YUV422") != 0) )
951 msg_Err(p_demux, "320x240 only supports YUV422 - forcing");
952 p_sys->video_mode = DC1394_VIDEO_MODE_320x240_YUV422;
959 if( strcmp( in_fmt, "RGB8") == 0)
960 p_sys->video_mode = DC1394_VIDEO_MODE_640x480_RGB8;
961 else if( strcmp( in_fmt, "MONO8") == 0)
962 p_sys->video_mode = DC1394_VIDEO_MODE_640x480_MONO8;
963 else if( strcmp( in_fmt, "MONO16") == 0)
964 p_sys->video_mode = DC1394_VIDEO_MODE_640x480_MONO16;
965 else if( strcmp( in_fmt, "YUV411") == 0)
966 p_sys->video_mode = DC1394_VIDEO_MODE_640x480_YUV411;
967 else // YUV422 default
968 p_sys->video_mode = DC1394_VIDEO_MODE_640x480_YUV422;
970 else // YUV422 default
971 p_sys->video_mode = DC1394_VIDEO_MODE_640x480_YUV422;