demux: ts: only seek on pcr for current program
[vlc.git] / modules / access / dc1394.c
blobc958254d0e61f76bd4d0de57fdbf5e9e53acf6cb
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 /*****************************************************************************
28 * Preamble
29 *****************************************************************************/
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
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 /*****************************************************************************
45 * Module descriptor
46 *****************************************************************************/
47 static int Open ( vlc_object_t * );
48 static void Close( vlc_object_t * );
50 vlc_module_begin()
51 set_shortname( N_("DC1394") )
52 set_description( N_("IIDC Digital Camera (FireWire) input") )
53 set_capability( "access_demux", 10 )
54 set_callbacks( Open, Close )
55 vlc_module_end()
57 struct demux_sys_t
59 /* camera info */
60 dc1394_t *p_dccontext;
61 uint32_t num_cameras;
62 dc1394camera_t *camera;
63 int selected_camera;
64 uint64_t selected_uid;
65 uint32_t dma_buffers;
66 dc1394featureset_t features;
67 bool reset_bus;
69 /* video info */
70 char *video_device;
71 dc1394video_mode_t video_mode;
72 int width;
73 int height;
74 int frame_size;
75 int frame_rate;
76 unsigned int brightness;
77 unsigned int focus;
78 es_out_id_t *p_es_video;
79 dc1394video_frame_t *frame;
82 /*****************************************************************************
83 * Local prototypes
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 /*****************************************************************************
91 * FindCameras
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");
103 goto end;
106 if( list->num == 0 )
108 msg_Err(p_demux, "Can not find cameras");
109 goto end;
112 sys->num_cameras = list->num;
113 msg_Dbg( p_demux, "Found %d dc1394 cameras.", list->num);
115 if( sys->selected_uid )
117 int found = 0;
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,
123 list->ids[i].guid);
124 found++;
125 break;
128 if( !found )
130 msg_Err( p_demux, "Can't find camera with uid : 0x%"PRIx64".",
131 sys->selected_uid );
132 goto end;
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 );
139 goto end;
141 else if( sys->selected_camera >= 0 )
143 sys->camera = dc1394_camera_new(sys->p_dccontext,
144 list->ids[sys->selected_camera].guid);
146 else
148 sys->camera = dc1394_camera_new(sys->p_dccontext,
149 list->ids[0].guid);
152 i_ret = VLC_SUCCESS;
154 end:
155 dc1394_camera_free_list (list);
156 return i_ret;
159 /*****************************************************************************
160 * Open:
161 *****************************************************************************/
162 static int Open( vlc_object_t *p_this )
164 demux_t *p_demux = (demux_t*)p_this;
165 demux_sys_t *p_sys;
166 es_format_t fmt;
167 dc1394error_t res;
169 if( strncmp(p_demux->psz_access, "dc1394", 6) != 0 )
170 return VLC_EGENERIC;
172 /* Set up p_demux */
173 p_demux->pf_demux = Demux;
174 p_demux->pf_control = Control;
175 p_demux->info.i_update = 0;
176 p_demux->info.i_title = 0;
177 p_demux->info.i_seekpoint = 0;
179 p_demux->p_sys = p_sys = vlc_obj_calloc( p_this, 1, sizeof( demux_sys_t ) );
180 if( !p_sys )
181 return VLC_ENOMEM;
183 memset( &fmt, 0, sizeof( es_format_t ) );
185 /* DEFAULTS */
186 p_sys->video_mode = DC1394_VIDEO_MODE_640x480_YUV422;
187 p_sys->width = 640;
188 p_sys->height = 480;
189 p_sys->frame_rate = DC1394_FRAMERATE_15;
190 p_sys->brightness = 200;
191 p_sys->focus = 0;
192 p_sys->p_dccontext = NULL;
193 p_sys->camera = NULL;
194 p_sys->selected_camera = -1;
195 p_sys->dma_buffers = 1;
196 p_sys->reset_bus = 0;
198 /* PROCESS INPUT OPTIONS */
199 if( process_options(p_demux) != VLC_SUCCESS )
201 msg_Err( p_demux, "Bad MRL, please check the option line "
202 "(MRL was: %s)",
203 p_demux->psz_location );
204 return VLC_EGENERIC;
207 p_sys->p_dccontext = dc1394_new();
208 if( !p_sys->p_dccontext )
210 msg_Err( p_demux, "Failed to initialise libdc1394");
211 return VLC_EGENERIC;
214 if( FindCamera( p_sys, p_demux ) != VLC_SUCCESS )
216 dc1394_free( p_sys->p_dccontext );
217 return VLC_EGENERIC;
220 if( !p_sys->camera )
222 msg_Err( p_demux, "No camera found !!" );
223 dc1394_free( p_sys->p_dccontext );
224 return VLC_EGENERIC;
227 if( p_sys->reset_bus )
229 if( dc1394_reset_bus( p_sys->camera ) != DC1394_SUCCESS )
231 msg_Err( p_demux, "Unable to reset IEEE 1394 bus");
232 Close( p_this );
233 return VLC_EGENERIC;
235 else msg_Dbg( p_demux, "Successfully reset IEEE 1394 bus");
238 if( dc1394_camera_reset( p_sys->camera ) != DC1394_SUCCESS )
240 msg_Err( p_demux, "Unable to reset camera");
241 Close( p_this );
242 return VLC_EGENERIC;
245 if( dc1394_camera_print_info( p_sys->camera,
246 stderr ) != DC1394_SUCCESS )
248 msg_Err( p_demux, "Unable to print camera info");
249 Close( p_this );
250 return VLC_EGENERIC;
253 if( dc1394_feature_get_all( p_sys->camera,
254 &p_sys->features ) != DC1394_SUCCESS )
256 msg_Err( p_demux, "Unable to get feature set");
257 Close( p_this );
258 return VLC_EGENERIC;
260 // TODO: only print features if verbosity increased
261 dc1394_feature_print_all(&p_sys->features, stderr);
263 #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/
264 if( p_sys->video_device )
266 if( dc1394_capture_set_device_filename( p_sys->camera,
267 p_sys->video_device ) != DC1394_SUCCESS )
269 msg_Err( p_demux, "Unable to set video device");
270 Close( p_this );
271 return VLC_EGENERIC;
274 #endif
276 if( p_sys->focus )
278 if( dc1394_feature_set_value( p_sys->camera,
279 DC1394_FEATURE_FOCUS,
280 p_sys->focus ) != DC1394_SUCCESS )
282 msg_Err( p_demux, "Unable to set initial focus to %u",
283 p_sys->focus );
285 else
286 msg_Dbg( p_demux, "Initial focus set to %u", p_sys->focus );
289 if( dc1394_feature_set_value( p_sys->camera,
290 DC1394_FEATURE_FOCUS,
291 p_sys->brightness ) != DC1394_SUCCESS )
293 msg_Err( p_demux, "Unable to set initial brightness to %u",
294 p_sys->brightness );
296 else
297 msg_Dbg( p_demux, "Initial brightness set to %u", p_sys->brightness );
299 if( dc1394_video_set_framerate( p_sys->camera,
300 p_sys->frame_rate ) != DC1394_SUCCESS )
302 msg_Err( p_demux, "Unable to set framerate");
303 Close( p_this );
304 return VLC_EGENERIC;
307 if( dc1394_video_set_mode( p_sys->camera,
308 p_sys->video_mode ) != DC1394_SUCCESS )
310 msg_Err( p_demux, "Unable to set video mode");
311 Close( p_this );
312 return VLC_EGENERIC;
315 if( dc1394_video_set_iso_speed( p_sys->camera,
316 DC1394_ISO_SPEED_400 ) != DC1394_SUCCESS )
318 msg_Err( p_demux, "Unable to set iso speed");
319 Close( p_this );
320 return VLC_EGENERIC;
323 /* and setup capture */
324 res = dc1394_capture_setup( p_sys->camera,
325 p_sys->dma_buffers,
326 DC1394_CAPTURE_FLAGS_DEFAULT);
327 if( res != DC1394_SUCCESS )
329 if( res == DC1394_NO_BANDWIDTH )
331 msg_Err( p_demux ,"No bandwidth: try adding the "
332 "\"resetbus\" option" );
334 else
336 msg_Err( p_demux ,"Unable to setup capture" );
338 Close( p_this );
339 return VLC_EGENERIC;
342 /* TODO - UYV444 chroma converter is missing, when it will be available
343 * fourcc will become variable (and not just a fixed value for UYVY)
345 es_format_Init( &fmt, VIDEO_ES, VLC_CODEC_UYVY );
347 fmt.video.i_width = p_sys->width;
348 fmt.video.i_height = p_sys->height;
350 msg_Dbg( p_demux, "Added new video es %4.4s %dx%d",
351 (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );
353 p_sys->p_es_video = es_out_Add( p_demux->out, &fmt );
355 /* have the camera start sending us data */
356 if( dc1394_video_set_transmission( p_sys->camera,
357 DC1394_ON ) != DC1394_SUCCESS )
359 msg_Err( p_demux, "Unable to start camera iso transmission" );
360 dc1394_capture_stop( p_sys->camera );
361 Close( p_this );
362 return VLC_EGENERIC;
364 msg_Dbg( p_demux, "Set iso transmission" );
365 // TODO: reread camera
366 return VLC_SUCCESS;
369 /*****************************************************************************
370 * Close:
371 *****************************************************************************/
372 static void Close( vlc_object_t *p_this )
374 demux_t *p_demux = (demux_t*)p_this;
375 demux_sys_t *p_sys = p_demux->p_sys;
377 /* Stop data transmission */
378 if( dc1394_video_set_transmission( p_sys->camera,
379 DC1394_OFF ) != DC1394_SUCCESS )
380 msg_Err( p_demux, "Unable to stop camera iso transmission" );
382 /* Close camera */
383 dc1394_capture_stop( p_sys->camera );
385 dc1394_camera_free(p_sys->camera);
386 dc1394_free(p_sys->p_dccontext);
388 free( p_sys->video_device );
391 #if 0
392 static void MovePixelUYVY( void *src, int spos, void *dst, int dpos )
394 char u,v,y;
395 u_char *sc;
396 u_char *dc;
398 sc = (u_char *)src + (spos*2);
399 if( spos % 2 )
401 v = sc[0];
402 y = sc[1];
403 u = *(sc -2);
405 else
407 u = sc[0];
408 y = sc[1];
409 v = sc[2];
411 dc = (u_char *)dst+(dpos*2);
412 if( dpos % 2 )
414 dc[0] = v;
415 dc[1] = y;
417 else
419 dc[0] = u;
420 dc[1] = y;
423 #endif
425 /*****************************************************************************
426 * Demux:
427 *****************************************************************************/
428 static block_t *GrabVideo( demux_t *p_demux )
430 demux_sys_t *p_sys = p_demux->p_sys;
431 block_t *p_block = NULL;
433 if( dc1394_capture_dequeue( p_sys->camera,
434 DC1394_CAPTURE_POLICY_WAIT,
435 &p_sys->frame ) != DC1394_SUCCESS )
437 msg_Err( p_demux, "Unable to capture a frame" );
438 return NULL;
441 p_block = block_Alloc( p_sys->frame->size[0] * p_sys->frame->size[1] * 2 );
442 if( !p_block )
444 msg_Err( p_demux, "Can not get block" );
445 return NULL;
448 if( !p_sys->frame->image )
450 msg_Err (p_demux, "Capture buffer empty");
451 block_Release( p_block );
452 return NULL;
455 memcpy( p_block->p_buffer, (const char *)p_sys->frame->image,
456 p_sys->width * p_sys->height * 2 );
458 p_block->i_pts = p_block->i_dts = mdate();
459 dc1394_capture_enqueue( p_sys->camera, p_sys->frame );
460 return p_block;
463 static int Demux( demux_t *p_demux )
465 demux_sys_t *p_sys = p_demux->p_sys;
466 block_t *p_blockv = NULL;
468 /* Try grabbing video frame */
469 p_blockv = GrabVideo( p_demux );
471 if( !p_blockv )
473 /* Sleep so we do not consume all the cpu, 10ms seems
474 * like a good value (100fps)
476 msleep( 10000 );
477 return 1;
480 if( p_blockv )
482 es_out_SetPCR( p_demux->out, p_blockv->i_pts );
483 es_out_Send( p_demux->out, p_sys->p_es_video, p_blockv );
485 return 1;
488 /*****************************************************************************
489 * Control:
490 *****************************************************************************/
491 static int Control( demux_t *p_demux, int i_query, va_list args )
493 VLC_UNUSED( p_demux );
494 switch( i_query )
496 /* Special for access_demux */
497 case DEMUX_CAN_PAUSE:
498 case DEMUX_CAN_SEEK:
499 case DEMUX_CAN_CONTROL_PACE:
500 *va_arg( args, bool * ) = false;
501 return VLC_SUCCESS;
503 case DEMUX_GET_PTS_DELAY:
504 *va_arg( args, int64_t * ) = (int64_t)DEFAULT_PTS_DELAY;
505 return VLC_SUCCESS;
507 case DEMUX_GET_TIME:
508 *va_arg( args, int64_t * ) = mdate();
509 return VLC_SUCCESS;
511 /* TODO implement others */
512 default:
513 return VLC_EGENERIC;
515 return VLC_EGENERIC;
518 static int process_options( demux_t *p_demux )
520 demux_sys_t *p_sys = p_demux->p_sys;
521 char *psz_dup;
522 char *psz_parser;
523 char *token = NULL;
524 char *state = NULL;
525 const char *in_size = NULL;
526 const char *in_fmt = NULL;
527 float rate_f;
529 psz_dup = strdup( p_demux->psz_location );
530 psz_parser = psz_dup;
531 for( token = strtok_r( psz_parser,":",&state); token;
532 token = strtok_r( NULL, ":", &state ) )
534 if( strncmp( token, "size=", strlen("size=") ) == 0 )
536 token += strlen("size=");
537 if( strncmp( token, "160x120", 7 ) == 0 )
539 /* TODO - UYV444 chroma converter is needed ...
540 * video size of 160x120 is temporarily disabled
542 msg_Err( p_demux,
543 "video size of 160x120 is actually disabled for lack of"
544 "chroma support. It will be released ASAP, until then try "
545 "an higher size (320x240 and 640x480 are fully supported)" );
546 free(psz_dup);
547 return VLC_EGENERIC;
548 #if 0
549 in_size = "160x120";
550 p_sys->width = 160;
551 p_sys->height = 120;
552 #endif
554 else if( strncmp( token, "320x240", 7 ) == 0 )
556 in_size = "320x240";
557 p_sys->width = 320;
558 p_sys->height = 240;
560 else if( strncmp( token, "640x480", 7 ) == 0 )
562 in_size = "640x480";
563 p_sys->width = 640;
564 p_sys->height = 480;
566 else
568 msg_Err( p_demux,
569 "This program currently suppots frame sizes of"
570 " 160x120, 320x240, and 640x480. "
571 "Please specify one of them. You have specified %s.",
572 token );
573 free(psz_dup);
574 return VLC_EGENERIC;
576 msg_Dbg( p_demux, "Requested video size : %s",token );
578 if( strncmp( token, "format=", strlen("format=") ) == 0 )
580 token += strlen("format=");
581 if( strncmp( token, "YUV411", 6 ) == 0 )
583 in_fmt = "YUV411";
585 else if( strncmp( token, "YUV422", 6 ) == 0 )
587 in_fmt = "YUV422";
589 else if( strncmp( token, "YUV444", 6 ) == 0 )
591 in_fmt = "YUV444";
593 else if( strncmp( token, "RGB8", 4 ) == 0 )
595 in_fmt = "RGB8";
597 else if( strncmp( token, "MONO8", 5 ) == 0 )
599 in_fmt = "MONO8";
601 else if( strncmp( token, "MONO16", 6 ) == 0 )
603 in_fmt = "MONO16";
605 else
607 msg_Err( p_demux, "Invalid format %s.", token );
608 free(psz_dup);
609 return VLC_EGENERIC;
611 msg_Dbg( p_demux, "Requested video format : %s", token );
613 else if( strncmp( token, "fps=", strlen( "fps=" ) ) == 0 )
615 token += strlen("fps=");
616 sscanf( token, "%g", &rate_f );
617 if( rate_f == 1.875 )
618 p_sys->frame_rate = DC1394_FRAMERATE_1_875;
619 else if( rate_f == 3.75 )
620 p_sys->frame_rate = DC1394_FRAMERATE_3_75;
621 else if( rate_f == 7.5 )
622 p_sys->frame_rate = DC1394_FRAMERATE_7_5;
623 else if( rate_f == 15 )
624 p_sys->frame_rate = DC1394_FRAMERATE_15;
625 else if( rate_f == 30 )
626 p_sys->frame_rate = DC1394_FRAMERATE_30;
627 else if( rate_f == 60 )
628 p_sys->frame_rate = DC1394_FRAMERATE_60;
629 else
631 msg_Err( p_demux ,
632 "This program supports framerates of"
633 " 1.875, 3.75, 7.5, 15, 30, 60. "
634 "Please specify one of them. You have specified %s.",
635 token);
636 free(psz_dup);
637 return VLC_EGENERIC;
639 msg_Dbg( p_demux, "Requested frame rate : %s",token );
641 else if( strncmp( token, "resetbus", strlen( "resetbus" ) ) == 0 )
643 token += strlen("resetbus");
644 p_sys->reset_bus = 1;
646 else if( strncmp( token, "brightness=", strlen( "brightness=" ) ) == 0 )
648 int nr = 0;
649 token += strlen("brightness=");
650 nr = sscanf( token, "%u", &p_sys->brightness);
651 if( nr != 1 )
653 msg_Err( p_demux, "Bad brightness value '%s', "
654 "must be an unsigned integer.",
655 token );
656 free(psz_dup);
657 return VLC_EGENERIC;
660 else if( strncmp( token, "buffers=", strlen( "buffers=" ) ) == 0 )
662 int nr = 0;
663 int in_buf = 0;
664 token += strlen("buffers=");
665 nr = sscanf( token, "%d", &in_buf);
666 if( nr != 1 || in_buf < 1 )
668 msg_Err( p_demux, "DMA buffers must be 1 or greater." );
669 free(psz_dup);
670 return VLC_EGENERIC;
672 else p_sys->dma_buffers = in_buf;
674 #if 0
675 // NOTE: If controller support is added back, more logic will needed to be added
676 // after the cameras are scanned.
677 else if( strncmp( token, "controller=", strlen( "controller=" ) ) == 0 )
679 int nr = 0;
680 token += strlen("controller=");
681 nr = sscanf( token, "%u", &p_sys->controller );
682 if( nr != 1)
684 msg_Err(p_demux, "Bad controller value '%s', "
685 "must be an unsigned integer.",
686 token );
687 return VLC_EGENERIC;
690 #endif
691 else if( strncmp( token, "camera=", strlen( "camera=" ) ) == 0 )
693 int nr = 0;
694 token += strlen("camera=");
695 nr = sscanf(token,"%u",&p_sys->selected_camera);
696 if( nr != 1)
698 msg_Err( p_demux, "Bad camera number '%s', "
699 "must be an unsigned integer.",
700 token );
701 free(psz_dup);
702 return VLC_EGENERIC;
705 else if( strncmp( token, "vdev=", strlen( "vdev=" ) ) == 0)
707 token += strlen("vdev=");
708 p_sys->video_device = strdup(token);
709 msg_Dbg( p_demux, "Using video device '%s'.", token );
711 else if( strncmp( token, "focus=", strlen("focus=" ) ) == 0)
713 int nr = 0;
714 token += strlen("focus=");
715 nr = sscanf( token, "%u", &p_sys->focus );
716 if( nr != 1 )
718 msg_Err( p_demux, "Bad focus value '%s', "
719 "must be an unsigned integer.",
720 token );
721 free(psz_dup);
722 return VLC_EGENERIC;
725 else if( strncmp( token, "uid=", strlen("uid=") ) == 0)
727 token += strlen("uid=");
728 sscanf( token, "0x%"SCNx64, &p_sys->selected_uid );
732 // The mode is a combination of size and format and not every format
733 // is supported by every size.
734 if( in_size)
736 if( strcmp( in_size, "160x120") == 0)
738 if( in_fmt && (strcmp( in_fmt, "YUV444") != 0) )
739 msg_Err(p_demux, "160x120 only supports YUV444 - forcing");
740 p_sys->video_mode = DC1394_VIDEO_MODE_160x120_YUV444;
742 else if( strcmp( in_size, "320x240") == 0)
744 if( in_fmt && (strcmp( in_fmt, "YUV422") != 0) )
745 msg_Err(p_demux, "320x240 only supports YUV422 - forcing");
746 p_sys->video_mode = DC1394_VIDEO_MODE_320x240_YUV422;
749 else
750 { // 640x480 default
751 if( in_fmt )
753 if( strcmp( in_fmt, "RGB8") == 0)
754 p_sys->video_mode = DC1394_VIDEO_MODE_640x480_RGB8;
755 else if( strcmp( in_fmt, "MONO8") == 0)
756 p_sys->video_mode = DC1394_VIDEO_MODE_640x480_MONO8;
757 else if( strcmp( in_fmt, "MONO16") == 0)
758 p_sys->video_mode = DC1394_VIDEO_MODE_640x480_MONO16;
759 else if( strcmp( in_fmt, "YUV411") == 0)
760 p_sys->video_mode = DC1394_VIDEO_MODE_640x480_YUV411;
761 else // YUV422 default
762 p_sys->video_mode = DC1394_VIDEO_MODE_640x480_YUV422;
764 else // YUV422 default
765 p_sys->video_mode = DC1394_VIDEO_MODE_640x480_YUV422;
768 free( psz_dup );
769 return VLC_SUCCESS;