1 /*****************************************************************************
2 * dc1394.c: IIDC (DCAM) 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 *****************************************************************************
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU Lesser General Public License as published by
14 * the Free Software Foundation; either version 2.1 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public License
23 * along with this program; if not, write to the Free Software Foundation,
24 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
35 #include <vlc_common.h>
36 #include <vlc_plugin.h>
37 #include <vlc_demux.h>
39 #include <dc1394/dc1394.h>
41 #define MAX_IEEE1394_HOSTS 32
42 #define MAX_CAMERA_NODES 32
44 /*****************************************************************************
46 *****************************************************************************/
47 static int Open ( vlc_object_t
* );
48 static void Close( vlc_object_t
* );
51 set_shortname( N_("DC1394") )
52 set_description( N_("IIDC Digital Camera (FireWire) input") )
53 set_capability( "access_demux", 0 )
54 set_callbacks( Open
, Close
)
60 dc1394_t
*p_dccontext
;
62 dc1394camera_t
*camera
;
64 uint64_t selected_uid
;
66 dc1394featureset_t features
;
71 dc1394video_mode_t video_mode
;
76 unsigned int brightness
;
78 es_out_id_t
*p_es_video
;
79 dc1394video_frame_t
*frame
;
82 /*****************************************************************************
84 *****************************************************************************/
85 static int Demux( demux_t
*p_demux
);
86 static int Control( demux_t
*, int, va_list );
87 static block_t
*GrabVideo( demux_t
*p_demux
);
88 static int process_options( demux_t
*p_demux
);
90 /*****************************************************************************
92 *****************************************************************************/
93 static int FindCamera( demux_sys_t
*sys
, demux_t
*p_demux
)
95 dc1394camera_list_t
*list
;
96 int i_ret
= VLC_EGENERIC
;
98 msg_Dbg( p_demux
, "Scanning for ieee1394 ports ..." );
100 if( dc1394_camera_enumerate (sys
->p_dccontext
, &list
) != DC1394_SUCCESS
)
102 msg_Err(p_demux
, "Can not ennumerate cameras");
108 msg_Err(p_demux
, "Can not find cameras");
112 sys
->num_cameras
= list
->num
;
113 msg_Dbg( p_demux
, "Found %d dc1394 cameras.", list
->num
);
115 if( sys
->selected_uid
)
118 for( unsigned i
= 0; i
< sys
->num_cameras
; i
++ )
120 if( list
->ids
[i
].guid
== sys
->selected_uid
)
122 sys
->camera
= dc1394_camera_new(sys
->p_dccontext
,
130 msg_Err( p_demux
, "Can't find camera with uid : 0x%"PRIx64
".",
135 else if( sys
->selected_camera
>= (int)list
->num
)
137 msg_Err( p_demux
, "There are not this many cameras. (%d/%d)",
138 sys
->selected_camera
, sys
->num_cameras
);
141 else if( sys
->selected_camera
>= 0 )
143 sys
->camera
= dc1394_camera_new(sys
->p_dccontext
,
144 list
->ids
[sys
->selected_camera
].guid
);
148 sys
->camera
= dc1394_camera_new(sys
->p_dccontext
,
155 dc1394_camera_free_list (list
);
159 /*****************************************************************************
161 *****************************************************************************/
162 static int Open( vlc_object_t
*p_this
)
164 demux_t
*p_demux
= (demux_t
*)p_this
;
170 p_demux
->pf_demux
= Demux
;
171 p_demux
->pf_control
= Control
;
173 p_demux
->p_sys
= p_sys
= vlc_obj_calloc( p_this
, 1, sizeof( demux_sys_t
) );
177 memset( &fmt
, 0, sizeof( es_format_t
) );
180 p_sys
->video_mode
= DC1394_VIDEO_MODE_640x480_YUV422
;
183 p_sys
->frame_rate
= DC1394_FRAMERATE_15
;
184 p_sys
->brightness
= 200;
186 p_sys
->p_dccontext
= NULL
;
187 p_sys
->camera
= NULL
;
188 p_sys
->selected_camera
= -1;
189 p_sys
->dma_buffers
= 1;
190 p_sys
->reset_bus
= 0;
192 /* PROCESS INPUT OPTIONS */
193 if( process_options(p_demux
) != VLC_SUCCESS
)
195 msg_Err( p_demux
, "Bad MRL, please check the option line "
197 p_demux
->psz_location
);
201 p_sys
->p_dccontext
= dc1394_new();
202 if( !p_sys
->p_dccontext
)
204 msg_Err( p_demux
, "Failed to initialise libdc1394");
208 if( FindCamera( p_sys
, p_demux
) != VLC_SUCCESS
)
210 dc1394_free( p_sys
->p_dccontext
);
216 msg_Err( p_demux
, "No camera found !!" );
217 dc1394_free( p_sys
->p_dccontext
);
221 if( p_sys
->reset_bus
)
223 if( dc1394_reset_bus( p_sys
->camera
) != DC1394_SUCCESS
)
225 msg_Err( p_demux
, "Unable to reset IEEE 1394 bus");
229 else msg_Dbg( p_demux
, "Successfully reset IEEE 1394 bus");
232 if( dc1394_camera_reset( p_sys
->camera
) != DC1394_SUCCESS
)
234 msg_Err( p_demux
, "Unable to reset camera");
239 if( dc1394_camera_print_info( p_sys
->camera
,
240 stderr
) != DC1394_SUCCESS
)
242 msg_Err( p_demux
, "Unable to print camera info");
247 if( dc1394_feature_get_all( p_sys
->camera
,
248 &p_sys
->features
) != DC1394_SUCCESS
)
250 msg_Err( p_demux
, "Unable to get feature set");
254 // TODO: only print features if verbosity increased
255 dc1394_feature_print_all(&p_sys
->features
, stderr
);
257 #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/
258 if( p_sys
->video_device
)
260 if( dc1394_capture_set_device_filename( p_sys
->camera
,
261 p_sys
->video_device
) != DC1394_SUCCESS
)
263 msg_Err( p_demux
, "Unable to set video device");
272 if( dc1394_feature_set_value( p_sys
->camera
,
273 DC1394_FEATURE_FOCUS
,
274 p_sys
->focus
) != DC1394_SUCCESS
)
276 msg_Err( p_demux
, "Unable to set initial focus to %u",
280 msg_Dbg( p_demux
, "Initial focus set to %u", p_sys
->focus
);
283 if( dc1394_feature_set_value( p_sys
->camera
,
284 DC1394_FEATURE_FOCUS
,
285 p_sys
->brightness
) != DC1394_SUCCESS
)
287 msg_Err( p_demux
, "Unable to set initial brightness to %u",
291 msg_Dbg( p_demux
, "Initial brightness set to %u", p_sys
->brightness
);
293 if( dc1394_video_set_framerate( p_sys
->camera
,
294 p_sys
->frame_rate
) != DC1394_SUCCESS
)
296 msg_Err( p_demux
, "Unable to set framerate");
301 if( dc1394_video_set_mode( p_sys
->camera
,
302 p_sys
->video_mode
) != DC1394_SUCCESS
)
304 msg_Err( p_demux
, "Unable to set video mode");
309 if( dc1394_video_set_iso_speed( p_sys
->camera
,
310 DC1394_ISO_SPEED_400
) != DC1394_SUCCESS
)
312 msg_Err( p_demux
, "Unable to set iso speed");
317 /* and setup capture */
318 res
= dc1394_capture_setup( p_sys
->camera
,
320 DC1394_CAPTURE_FLAGS_DEFAULT
);
321 if( res
!= DC1394_SUCCESS
)
323 if( res
== DC1394_NO_BANDWIDTH
)
325 msg_Err( p_demux
,"No bandwidth: try adding the "
326 "\"resetbus\" option" );
330 msg_Err( p_demux
,"Unable to setup capture" );
336 /* TODO - UYV444 chroma converter is missing, when it will be available
337 * fourcc will become variable (and not just a fixed value for UYVY)
339 es_format_Init( &fmt
, VIDEO_ES
, VLC_CODEC_UYVY
);
341 fmt
.video
.i_width
= p_sys
->width
;
342 fmt
.video
.i_height
= p_sys
->height
;
344 msg_Dbg( p_demux
, "Added new video es %4.4s %dx%d",
345 (char*)&fmt
.i_codec
, fmt
.video
.i_width
, fmt
.video
.i_height
);
347 p_sys
->p_es_video
= es_out_Add( p_demux
->out
, &fmt
);
349 /* have the camera start sending us data */
350 if( dc1394_video_set_transmission( p_sys
->camera
,
351 DC1394_ON
) != DC1394_SUCCESS
)
353 msg_Err( p_demux
, "Unable to start camera iso transmission" );
354 dc1394_capture_stop( p_sys
->camera
);
358 msg_Dbg( p_demux
, "Set iso transmission" );
359 // TODO: reread camera
363 /*****************************************************************************
365 *****************************************************************************/
366 static void Close( vlc_object_t
*p_this
)
368 demux_t
*p_demux
= (demux_t
*)p_this
;
369 demux_sys_t
*p_sys
= p_demux
->p_sys
;
371 /* Stop data transmission */
372 if( dc1394_video_set_transmission( p_sys
->camera
,
373 DC1394_OFF
) != DC1394_SUCCESS
)
374 msg_Err( p_demux
, "Unable to stop camera iso transmission" );
377 dc1394_capture_stop( p_sys
->camera
);
379 dc1394_camera_free(p_sys
->camera
);
380 dc1394_free(p_sys
->p_dccontext
);
382 free( p_sys
->video_device
);
386 static void MovePixelUYVY( void *src
, int spos
, void *dst
, int dpos
)
392 sc
= (u_char
*)src
+ (spos
*2);
405 dc
= (u_char
*)dst
+(dpos
*2);
419 /*****************************************************************************
421 *****************************************************************************/
422 static block_t
*GrabVideo( demux_t
*p_demux
)
424 demux_sys_t
*p_sys
= p_demux
->p_sys
;
425 block_t
*p_block
= NULL
;
427 if( dc1394_capture_dequeue( p_sys
->camera
,
428 DC1394_CAPTURE_POLICY_WAIT
,
429 &p_sys
->frame
) != DC1394_SUCCESS
)
431 msg_Err( p_demux
, "Unable to capture a frame" );
435 p_block
= block_Alloc( p_sys
->frame
->size
[0] * p_sys
->frame
->size
[1] * 2 );
438 msg_Err( p_demux
, "Can not get block" );
442 if( !p_sys
->frame
->image
)
444 msg_Err (p_demux
, "Capture buffer empty");
445 block_Release( p_block
);
449 memcpy( p_block
->p_buffer
, (const char *)p_sys
->frame
->image
,
450 p_sys
->width
* p_sys
->height
* 2 );
452 p_block
->i_pts
= p_block
->i_dts
= mdate();
453 dc1394_capture_enqueue( p_sys
->camera
, p_sys
->frame
);
457 static int Demux( demux_t
*p_demux
)
459 demux_sys_t
*p_sys
= p_demux
->p_sys
;
460 block_t
*p_blockv
= NULL
;
462 /* Try grabbing video frame */
463 p_blockv
= GrabVideo( p_demux
);
467 /* Sleep so we do not consume all the cpu, 10ms seems
468 * like a good value (100fps)
476 es_out_SetPCR( p_demux
->out
, p_blockv
->i_pts
);
477 es_out_Send( p_demux
->out
, p_sys
->p_es_video
, p_blockv
);
482 /*****************************************************************************
484 *****************************************************************************/
485 static int Control( demux_t
*p_demux
, int i_query
, va_list args
)
487 VLC_UNUSED( p_demux
);
490 /* Special for access_demux */
491 case DEMUX_CAN_PAUSE
:
493 case DEMUX_CAN_CONTROL_PACE
:
494 *va_arg( args
, bool * ) = false;
497 case DEMUX_GET_PTS_DELAY
:
498 *va_arg( args
, int64_t * ) = (int64_t)DEFAULT_PTS_DELAY
;
502 *va_arg( args
, int64_t * ) = mdate();
505 /* TODO implement others */
512 static int process_options( demux_t
*p_demux
)
514 demux_sys_t
*p_sys
= p_demux
->p_sys
;
519 const char *in_size
= NULL
;
520 const char *in_fmt
= NULL
;
523 psz_dup
= strdup( p_demux
->psz_location
);
524 psz_parser
= psz_dup
;
525 for( token
= strtok_r( psz_parser
,":",&state
); token
;
526 token
= strtok_r( NULL
, ":", &state
) )
528 if( strncmp( token
, "size=", strlen("size=") ) == 0 )
530 token
+= strlen("size=");
531 if( strncmp( token
, "160x120", 7 ) == 0 )
533 /* TODO - UYV444 chroma converter is needed ...
534 * video size of 160x120 is temporarily disabled
537 "video size of 160x120 is actually disabled for lack of"
538 "chroma support. It will be released ASAP, until then try "
539 "an higher size (320x240 and 640x480 are fully supported)" );
548 else if( strncmp( token
, "320x240", 7 ) == 0 )
554 else if( strncmp( token
, "640x480", 7 ) == 0 )
563 "This program currently suppots frame sizes of"
564 " 160x120, 320x240, and 640x480. "
565 "Please specify one of them. You have specified %s.",
570 msg_Dbg( p_demux
, "Requested video size : %s",token
);
572 if( strncmp( token
, "format=", strlen("format=") ) == 0 )
574 token
+= strlen("format=");
575 if( strncmp( token
, "YUV411", 6 ) == 0 )
579 else if( strncmp( token
, "YUV422", 6 ) == 0 )
583 else if( strncmp( token
, "YUV444", 6 ) == 0 )
587 else if( strncmp( token
, "RGB8", 4 ) == 0 )
591 else if( strncmp( token
, "MONO8", 5 ) == 0 )
595 else if( strncmp( token
, "MONO16", 6 ) == 0 )
601 msg_Err( p_demux
, "Invalid format %s.", token
);
605 msg_Dbg( p_demux
, "Requested video format : %s", token
);
607 else if( strncmp( token
, "fps=", strlen( "fps=" ) ) == 0 )
609 token
+= strlen("fps=");
610 sscanf( token
, "%g", &rate_f
);
611 if( rate_f
== 1.875 )
612 p_sys
->frame_rate
= DC1394_FRAMERATE_1_875
;
613 else if( rate_f
== 3.75 )
614 p_sys
->frame_rate
= DC1394_FRAMERATE_3_75
;
615 else if( rate_f
== 7.5 )
616 p_sys
->frame_rate
= DC1394_FRAMERATE_7_5
;
617 else if( rate_f
== 15 )
618 p_sys
->frame_rate
= DC1394_FRAMERATE_15
;
619 else if( rate_f
== 30 )
620 p_sys
->frame_rate
= DC1394_FRAMERATE_30
;
621 else if( rate_f
== 60 )
622 p_sys
->frame_rate
= DC1394_FRAMERATE_60
;
626 "This program supports framerates of"
627 " 1.875, 3.75, 7.5, 15, 30, 60. "
628 "Please specify one of them. You have specified %s.",
633 msg_Dbg( p_demux
, "Requested frame rate : %s",token
);
635 else if( strncmp( token
, "resetbus", strlen( "resetbus" ) ) == 0 )
637 token
+= strlen("resetbus");
638 p_sys
->reset_bus
= 1;
640 else if( strncmp( token
, "brightness=", strlen( "brightness=" ) ) == 0 )
643 token
+= strlen("brightness=");
644 nr
= sscanf( token
, "%u", &p_sys
->brightness
);
647 msg_Err( p_demux
, "Bad brightness value '%s', "
648 "must be an unsigned integer.",
654 else if( strncmp( token
, "buffers=", strlen( "buffers=" ) ) == 0 )
658 token
+= strlen("buffers=");
659 nr
= sscanf( token
, "%d", &in_buf
);
660 if( nr
!= 1 || in_buf
< 1 )
662 msg_Err( p_demux
, "DMA buffers must be 1 or greater." );
666 else p_sys
->dma_buffers
= in_buf
;
669 // NOTE: If controller support is added back, more logic will needed to be added
670 // after the cameras are scanned.
671 else if( strncmp( token
, "controller=", strlen( "controller=" ) ) == 0 )
674 token
+= strlen("controller=");
675 nr
= sscanf( token
, "%u", &p_sys
->controller
);
678 msg_Err(p_demux
, "Bad controller value '%s', "
679 "must be an unsigned integer.",
685 else if( strncmp( token
, "camera=", strlen( "camera=" ) ) == 0 )
688 token
+= strlen("camera=");
689 nr
= sscanf(token
,"%u",&p_sys
->selected_camera
);
692 msg_Err( p_demux
, "Bad camera number '%s', "
693 "must be an unsigned integer.",
699 else if( strncmp( token
, "vdev=", strlen( "vdev=" ) ) == 0)
701 token
+= strlen("vdev=");
702 p_sys
->video_device
= strdup(token
);
703 msg_Dbg( p_demux
, "Using video device '%s'.", token
);
705 else if( strncmp( token
, "focus=", strlen("focus=" ) ) == 0)
708 token
+= strlen("focus=");
709 nr
= sscanf( token
, "%u", &p_sys
->focus
);
712 msg_Err( p_demux
, "Bad focus value '%s', "
713 "must be an unsigned integer.",
719 else if( strncmp( token
, "uid=", strlen("uid=") ) == 0)
721 token
+= strlen("uid=");
722 sscanf( token
, "0x%"SCNx64
, &p_sys
->selected_uid
);
726 // The mode is a combination of size and format and not every format
727 // is supported by every size.
730 if( strcmp( in_size
, "160x120") == 0)
732 if( in_fmt
&& (strcmp( in_fmt
, "YUV444") != 0) )
733 msg_Err(p_demux
, "160x120 only supports YUV444 - forcing");
734 p_sys
->video_mode
= DC1394_VIDEO_MODE_160x120_YUV444
;
736 else if( strcmp( in_size
, "320x240") == 0)
738 if( in_fmt
&& (strcmp( in_fmt
, "YUV422") != 0) )
739 msg_Err(p_demux
, "320x240 only supports YUV422 - forcing");
740 p_sys
->video_mode
= DC1394_VIDEO_MODE_320x240_YUV422
;
747 if( strcmp( in_fmt
, "RGB8") == 0)
748 p_sys
->video_mode
= DC1394_VIDEO_MODE_640x480_RGB8
;
749 else if( strcmp( in_fmt
, "MONO8") == 0)
750 p_sys
->video_mode
= DC1394_VIDEO_MODE_640x480_MONO8
;
751 else if( strcmp( in_fmt
, "MONO16") == 0)
752 p_sys
->video_mode
= DC1394_VIDEO_MODE_640x480_MONO16
;
753 else if( strcmp( in_fmt
, "YUV411") == 0)
754 p_sys
->video_mode
= DC1394_VIDEO_MODE_640x480_YUV411
;
755 else // YUV422 default
756 p_sys
->video_mode
= DC1394_VIDEO_MODE_640x480_YUV422
;
758 else // YUV422 default
759 p_sys
->video_mode
= DC1394_VIDEO_MODE_640x480_YUV422
;