dc1394.c 31.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10
/*****************************************************************************
 * dc1394.c: firewire input module
 *****************************************************************************
 * Copyright (C) 2006 the VideoLAN team
 *
 * Authors: Xant Majere <xant@xant.net>
 *
 *****************************************************************************
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
11
 * License as published by the Free Software Foundation;
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 * version 2 of the License.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/

29 30 31 32
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

33
#include <vlc/vlc.h>
34
#include <vlc_plugin.h>
35 36 37
#include <vlc_input.h>
#include <vlc_vout.h>
#include <vlc_demux.h>
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66


#ifdef HAVE_FCNTL_H
#   include <fcntl.h>
#endif
#ifdef HAVE_UNISTD_H
#   include <unistd.h>
#elif defined( WIN32 ) && !defined( UNDER_CE )
#   include <io.h>
#endif

#include <sys/ioctl.h>
#include <sys/soundcard.h>

#include <libraw1394/raw1394.h>
#include <libdc1394/dc1394_control.h>

#define MAX_IEEE1394_HOSTS 32
#define MAX_CAMERA_NODES 32

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );
static void OpenAudioDev( demux_t *p_demux );
static inline void CloseAudioDev( demux_t *p_demux );

vlc_module_begin();
67
    set_description( N_("dc1394 input") );
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
    set_capability( "access_demux", 10 );
    add_shortcut( "dc1394" );
    set_callbacks( Open, Close );
vlc_module_end();

typedef struct __dc_camera
{
    int port;
    nodeid_t node;
    u_int64_t uid;
} dc_camera;

typedef struct demux_sys_t
{
    dc1394_cameracapture camera;
    picture_t            pic;
    int                  dma_capture;
#define DMA_OFF 0
#define DMA_ON 1
    int                 num_ports;
    int                 num_cameras;
    int                 selected_camera;
    u_int64_t           selected_uid;

    dc_camera           *camera_nodes;
    dc1394_camerainfo   camera_info;
    dc1394_miscinfo     misc_info;
    raw1394handle_t     fd_video;
    quadlet_t           supported_framerates;

    int                 width;
    int                 height;
    int                 frame_size;
    int                 frame_rate;
    unsigned int        brightness;
    unsigned int        focus;
    char                *dma_device;
    es_out_id_t         *p_es_video;

    /* audio stuff */
    int                 i_sample_rate;
    int                 channels;
    int                 i_audio_max_frame_size;
    int                 fd_audio;
    char                *audio_device;
#define NO_ROTATION 0
#define ROTATION_LEFT 1
#define ROTATION_RIGHT 2
    es_out_id_t         *p_es_audio;
} dc1394_sys;

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
static int Demux( demux_t *p_demux );
static int Control( demux_t *, int, va_list );
static block_t *GrabVideo( demux_t *p_demux );
static block_t *GrabAudio( demux_t *p_demux );
static int process_options( demux_t *p_demux);

/*****************************************************************************
 * ScanCameras
 *****************************************************************************/
static void ScanCameras( dc1394_sys *sys, demux_t *p_demux )
{
    struct raw1394_portinfo portinfo[MAX_IEEE1394_HOSTS];
    raw1394handle_t tempFd;
    dc1394_camerainfo  info;
    dc_camera *node_list = NULL;
    nodeid_t  *nodes = NULL;
    int num_ports = 0;
    int num_cameras = 0;
    int nodecount;
    int i, n;

    memset( &portinfo, 0, sizeof(portinfo) );

    msg_Dbg( p_demux, "Scanning for ieee1394 ports ..." );

    tempFd = raw1394_new_handle();
    if( !tempFd )
        return VLC_EGENERIC;
    raw1394_get_port_info( tempFd, portinfo, MAX_IEEE1394_HOSTS);
    raw1394_destroy_handle( tempFd );

    for( i=0; i < MAX_IEEE1394_HOSTS; i++ )
    {
        /* check if port exists and has at least one node*/
        if( !portinfo[i].nodes )
            continue;
        nodes = NULL;
        nodecount = 0;
        tempFd = dc1394_create_handle( i );

        /* skip this port if we can't obtain a valid handle */
        if( !tempFd )
            continue;
        msg_Dbg( p_demux, "Found ieee1394 port %d (%s) ... "
                          "checking for camera nodes",
                          i, portinfo[i].name );
        num_ports++;

        nodes = dc1394_get_camera_nodes( tempFd, &nodecount, 0 );
        if( nodecount )
        {
            msg_Dbg( p_demux, "Found %d dc1394 cameras on port %d (%s)",
                     nodecount, i, portinfo[i].name );

            if( node_list )
                node_list = realloc( node_list, sizeof(dc_camera) * (num_cameras+nodecount) );
            else
                node_list = malloc( sizeof(dc_camera) * nodecount);

            for( n = 0; n < nodecount; n++ )
            {
                int result = 0;

                result = dc1394_get_camera_info( tempFd, nodes[n], &info );
                if( result == DC1394_SUCCESS )
                {
                    node_list[num_cameras+n].uid = info.euid_64;
                }
                node_list[num_cameras+n].node = nodes[n];
                node_list[num_cameras+n].port = i;
            }
            num_cameras += nodecount;
        }
        else
            msg_Dbg( p_demux, "no cameras found  on port %d (%s)",
                     i, portinfo[i].name );

        if( tempFd )
            dc1394_destroy_handle( tempFd );
    }
    sys->num_ports = num_ports;
    sys->num_cameras = num_cameras;
    sys->camera_nodes = node_list;
}

/*****************************************************************************
 * Open:
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys;
    es_format_t fmt;
    int i;
    int i_width;
    int i_height;
    int i_aspect;
    int result = 0;

    /* Set up p_demux */
    p_demux->pf_demux = Demux;
    p_demux->pf_control = Control;
    p_demux->info.i_update = 0;
    p_demux->info.i_title = 0;
    p_demux->info.i_seekpoint = 0;

    p_demux->p_sys = p_sys = malloc( sizeof( demux_sys_t ) );
    if( !p_sys )
    {
        msg_Err( p_demux, "not enough memory available" );
        return VLC_ENOMEM;
    }
    memset( p_sys, 0, sizeof( demux_sys_t ) );
    memset( &fmt, 0, sizeof( es_format_t ) );

    /* DEFAULTS */
    p_sys->frame_size = MODE_640x480_YUV422;
    p_sys->width      = 640;
    p_sys->height     = 480;
    p_sys->frame_rate = FRAMERATE_30;
    p_sys->brightness = 200;
    p_sys->focus      = 0;
    p_sys->dma_capture = DMA_ON; /* defaults to VIDEO1394 capture mode */
    p_sys->fd_audio   = -1;
    p_sys->fd_video   = NULL;
    p_sys->camera_nodes = NULL;
    p_sys->selected_camera = 0;
    p_sys->dma_device = NULL;
    p_sys->selected_uid = 0;

    /* PROCESS INPUT OPTIONS */
    if( process_options(p_demux) != VLC_SUCCESS )
    {
        msg_Err( p_demux, "Bad MRL, please check the option line "
                          "(MRL was: %s)",
                          p_demux->psz_path );
        free( p_sys );
        p_demux->p_sys = NULL;
        return VLC_EGENERIC;
    }

    msg_Dbg( p_demux, "Selected camera %d", p_sys->selected_camera );
    msg_Dbg( p_demux, "Selected uid 0x%llx", p_sys->selected_uid );

    ScanCameras( p_sys, p_demux );
    if( !p_sys->camera_nodes )
    {
        msg_Err( p_demux, "No camera found !!" );
        free( p_sys );
        p_demux->p_sys = NULL;
        return VLC_EGENERIC;
    }

    if( p_sys->selected_uid )
    {
        int found = 0;
        for( i=0; i < p_sys->num_cameras; i++ )
        {
            if( p_sys->camera_nodes[i].uid == p_sys->selected_uid )
            {
                p_sys->selected_camera = i;
                found++;
                break;
            }
        }
        if( !found )
        {
            msg_Err( p_demux, "Can't find camera with uid : 0x%llx.",
                     p_sys->selected_uid );
            Close( p_this );
            return VLC_EGENERIC;
        }
    }
    else if( p_sys->selected_camera >= p_sys->num_cameras )
    {
        msg_Err( p_demux, "there are not this many cameras. (%d/%d)",
                          p_sys->selected_camera, p_sys->num_cameras );
        Close( p_this );
        return VLC_EGENERIC;
    }

    p_sys->fd_video = dc1394_create_handle(
                        p_sys->camera_nodes[p_sys->selected_camera].port );
    if( (int)p_sys->fd_video < 0 )
    {
        msg_Err( p_demux, "Can't init dc1394 handle" );
        Close( p_this );
        return VLC_EGENERIC;
    }

    /* get camera info */
    result = dc1394_get_camera_info( p_sys->fd_video,
                        p_sys->camera_nodes[p_sys->selected_camera].node,
                        &p_sys->camera_info );
    if( result != DC1394_SUCCESS )
    {
        msg_Err( p_demux ,"unable to get camera info" );
        Close( p_this );
        return VLC_EGENERIC;
    }

    dc1394_print_camera_info( &p_sys->camera_info );
    result = dc1394_get_camera_misc_info( p_sys->fd_video,
                        p_sys->camera_nodes[p_sys->selected_camera].node,
                        &p_sys->misc_info );
    if( result != DC1394_SUCCESS )
    {
        msg_Err( p_demux, "unable to get camera misc info" );
        Close( p_this );
        return VLC_EGENERIC;
    }

    /* init camera and set some video options  */
    result = dc1394_init_camera( p_sys->camera_info.handle,
                                 p_sys->camera_info.id );
    if( result != DC1394_SUCCESS )
    {
        msg_Err( p_demux, "unable to get init dc1394 camera" );
        Close( p_this );
        return VLC_EGENERIC;
    }

    if( p_sys->focus )
    {
        result = dc1394_set_focus( p_sys->camera_info.handle,
                        p_sys->camera_nodes[p_sys->selected_camera].node,
                        p_sys->focus );
        if( result != DC1394_SUCCESS )
        {
            msg_Err( p_demux, "unable to set initial focus to %u",
                     p_sys->focus );
        }
        msg_Dbg( p_demux, "Initial focus set to %u", p_sys->focus );
    }

    result = dc1394_set_brightness( p_sys->camera_info.handle,
                        p_sys->camera_nodes[p_sys->selected_camera].node,
                        p_sys->brightness );
    if( result != DC1394_SUCCESS )
    {
        msg_Err( p_demux, "unable to set init brightness to %d",
                 p_sys->brightness);
        Close( p_this );
        return VLC_EGENERIC;
    }

    result = dc1394_set_video_framerate( p_sys->camera_info.handle,
                        p_sys->camera_nodes[p_sys->selected_camera].node,
                        p_sys->frame_rate );
    if( result != DC1394_SUCCESS )
    {
        msg_Err( p_demux, "unable to set framerate to %d",
                 p_sys->frame_rate );
        Close( p_this );
        return VLC_EGENERIC;
    }
    p_sys->misc_info.framerate = p_sys->frame_rate;

    result = dc1394_set_video_format( p_sys->camera_info.handle,
                        p_sys->camera_nodes[p_sys->selected_camera].node,
                        FORMAT_VGA_NONCOMPRESSED );
    if( result != DC1394_SUCCESS )
    {
        msg_Err( p_demux, "unable to set video format to VGA_NONCOMPRESSED" );
        Close( p_this );
        return VLC_EGENERIC;
    }
    p_sys->misc_info.format = FORMAT_VGA_NONCOMPRESSED;

    result = dc1394_set_video_mode( p_sys->camera_info.handle,
                        p_sys->camera_nodes[p_sys->selected_camera].node,
                        p_sys->frame_size );
    if( result != DC1394_SUCCESS )
    {
        msg_Err( p_demux, "unable to set video mode" );
        Close( p_this );
        return VLC_EGENERIC;
    }
    p_sys->misc_info.mode = p_sys->frame_size;

    /* reprobe everything */
    result = dc1394_get_camera_info( p_sys->camera_info.handle,
                                     p_sys->camera_info.id,
                                     &p_sys->camera_info );
    if( result != DC1394_SUCCESS )
    {
        msg_Err( p_demux, "Could not get camera basic information!" );
        Close( p_this );
        return VLC_EGENERIC;
    }

    result = dc1394_get_camera_misc_info( p_sys->camera_info.handle,
                                          p_sys->camera_info.id,
                                          &p_sys->misc_info );
    if( result != DC1394_SUCCESS )
    {
        msg_Err( p_demux, "Could not get camera misc information!" );
        Close( p_this );
        return VLC_EGENERIC;
    }

    /* set iso_channel */
    result = dc1394_set_iso_channel_and_speed( p_sys->camera_info.handle,
                                               p_sys->camera_info.id,
                                               p_sys->selected_camera,
                                               SPEED_400 );
    if( result != DC1394_SUCCESS )
    {
        msg_Err( p_demux, "Could not set iso channel!" );
        Close( p_this );
        return VLC_EGENERIC;
    }
    msg_Dbg( p_demux, "Using ISO channel %d", p_sys->misc_info.iso_channel );
    p_sys->misc_info.iso_channel = p_sys->selected_camera;

    /* and setup capture */
    if( p_sys->dma_capture )
    {
        result = dc1394_dma_setup_capture( p_sys->camera_info.handle,
                        p_sys->camera_info.id,
                        p_sys->misc_info.iso_channel,
                        p_sys->misc_info.format,
                        p_sys->misc_info.mode,
                        SPEED_400,
                        p_sys->misc_info.framerate,
                        10, 0,
                        p_sys->dma_device,
                        &p_sys->camera );
        if( result != DC1394_SUCCESS )
        {
            msg_Err( p_demux ,"unable to setup camera" );
            Close( p_this );
            return VLC_EGENERIC;
        }
    }
    else
    {
        result = dc1394_setup_capture( p_sys->camera_info.handle,
                    p_sys->camera_info.id,
                    p_sys->misc_info.iso_channel,
                    p_sys->misc_info.format,
                    p_sys->misc_info.mode,
                    SPEED_400,
                    p_sys->misc_info.framerate,
                    &p_sys->camera );
        if( result != DC1394_SUCCESS)
        {
            msg_Err( p_demux ,"unable to setup camera" );
            Close( p_this );
            return VLC_EGENERIC;
        }
    }

    /* TODO - UYV444 chroma converter is missing, when it will be available
     * fourcc will become variable (and not just a fixed value for UYVY)
     */
    i_width = p_sys->camera.frame_width;
    i_height = p_sys->camera.frame_height;

    i_aspect = vout_InitPicture( VLC_OBJECT(p_demux), &p_sys->pic,
                    VLC_FOURCC('U', 'Y', 'V', 'Y'),
                    i_width, i_height,
                    i_width * VOUT_ASPECT_FACTOR / i_height );

    es_format_Init( &fmt, VIDEO_ES, VLC_FOURCC('U', 'Y', 'V', 'Y') );

    fmt.video.i_width = i_width;
    fmt.video.i_height = i_height;

    msg_Dbg( p_demux, "added new video es %4.4s %dx%d",
             (char*)&fmt.i_codec, fmt.video.i_width, fmt.video.i_height );

    p_sys->p_es_video = es_out_Add( p_demux->out, &fmt );

    if( p_sys->audio_device )
    {
        OpenAudioDev( p_demux );
        if( p_sys->fd_audio >= 0 )
        {
            es_format_t fmt;
            es_format_Init( &fmt, AUDIO_ES, VLC_FOURCC('a','r','a','w') );

            fmt.audio.i_channels = p_sys->channels ? p_sys->channels : 1;
            fmt.audio.i_rate = p_sys->i_sample_rate;
            fmt.audio.i_bitspersample = 16; /* FIXME: hmm, ?? */
            fmt.audio.i_blockalign = fmt.audio.i_channels *
                                     fmt.audio.i_bitspersample / 8;
            fmt.i_bitrate = fmt.audio.i_channels * fmt.audio.i_rate *
                            fmt.audio.i_bitspersample;

            msg_Dbg( p_demux, "new audio es %d channels %dHz",
            fmt.audio.i_channels, fmt.audio.i_rate );

            p_sys->p_es_audio = es_out_Add( p_demux->out, &fmt );
        }
    }

    /* have the camera start sending us data */
    result = dc1394_start_iso_transmission( p_sys->camera_info.handle,
                                            p_sys->camera_info.id );
    if( result != DC1394_SUCCESS )
    {
        msg_Err( p_demux, "unable to start camera iso transmission" );
        if( p_sys->dma_capture )
        {
            dc1394_dma_release_camera( p_sys->fd_video, &p_sys->camera );
        }
        else
        {
            dc1394_release_camera( p_sys->fd_video, &p_sys->camera );
        }
        Close( p_this );
        return VLC_EGENERIC;
    }
    p_sys->misc_info.is_iso_on = DC1394_TRUE;
    return VLC_SUCCESS;
}

static void OpenAudioDev( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    char *psz_device = p_sys->audio_device;
    int i_format = AFMT_S16_LE;
    int result;

    p_sys->fd_audio = open( psz_device, O_RDONLY | O_NONBLOCK );
    if( p_sys->fd_audio  < 0 )
    {
        msg_Err( p_demux, "cannot open audio device (%s)", psz_device );
        CloseAudioDev( p_demux );
    }

    if( !p_sys->i_sample_rate )
        p_sys->i_sample_rate = 44100;

    result = ioctl( p_sys->fd_audio, SNDCTL_DSP_SETFMT, &i_format );
    if( (result  < 0) || (i_format != AFMT_S16_LE) )
    {
        msg_Err( p_demux, "cannot set audio format (16b little endian) "
                          "(%d)", i_format );
        CloseAudioDev( p_demux );
    }

    result = ioctl( p_sys->fd_audio, SNDCTL_DSP_CHANNELS, &p_sys->channels );
    if( result < 0 )
    {
        msg_Err( p_demux, "cannot set audio channels count (%d)",
                 p_sys->channels );
        CloseAudioDev( p_demux );
    }

    result = ioctl( p_sys->fd_audio, SNDCTL_DSP_SPEED, &p_sys->i_sample_rate );
    if( result < 0 )
    {
        msg_Err( p_demux, "cannot set audio sample rate (%s)", p_sys->i_sample_rate );
        CloseAudioDev( p_demux );
    }

580
    msg_Dbg( p_demux, "opened adev=`%s' %s %dHz",
581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
             psz_device,
             (p_sys->channels > 1) ? "stereo" : "mono",
             p_sys->i_sample_rate );

    p_sys->i_audio_max_frame_size = 32 * 1024;
}

static inline void CloseAudioDev( demux_t *p_demux )
{
    demux_sys_t *p_sys = NULL;

    if( p_demux )
    {
        p_sys = p_demux->p_sys;
        if( p_sys->fd_audio >= 0 )
            close( p_sys->fd_audio );
    }
}

/*****************************************************************************
 * Close:
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
    demux_t     *p_demux = (demux_t*)p_this;
    demux_sys_t *p_sys = p_demux->p_sys;
    int result = 0;

    /* Stop data transmission */
    result = dc1394_stop_iso_transmission( p_sys->fd_video,
                                           p_sys->camera.node );
    if( result != DC1394_SUCCESS )
    {
        msg_Err( p_demux, "couldn't stop the camera" );
    }

    /* Close camera */
    if( p_sys->dma_capture )
    {
        dc1394_dma_unlisten( p_sys->fd_video, &p_sys->camera );
        dc1394_dma_release_camera( p_sys->fd_video, &p_sys->camera );
    }
    else
    {
        dc1394_release_camera( p_sys->fd_video, &p_sys->camera );
    }

    if( p_sys->fd_video )
        dc1394_destroy_handle( p_sys->fd_video );
    CloseAudioDev( p_demux );

632 633
    free( p_sys->camera_nodes );
    free( p_sys->audio_device );
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753

    free( p_sys );
}

static void MovePixelUYVY( void *src, int spos, void *dst, int dpos )
{
    char u,v,y;
    u_char  *sc;
    u_char  *dc;

    sc = (u_char *)src + (spos*2);
    if( spos % 2 )
    {
        v = sc[0];
        y = sc[1];
        u = *(sc -2);
    }
    else
    {
        u = sc[0];
        y = sc[1];
        v = sc[2];
    }
    dc = (u_char *)dst+(dpos*2);
    if( dpos % 2 )
    {
        dc[0] = v;
        dc[1] = y;
    }
    else
    {
        dc[0] = u;
        dc[1] = y;
    }
}

/*****************************************************************************
 * Demux:
 *****************************************************************************/
static block_t *GrabVideo( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    block_t     *p_block = NULL;
    int result = 0;

    if( p_sys->dma_capture )
    {
        result = dc1394_dma_single_capture( &p_sys->camera );
        if( result != DC1394_SUCCESS )
        {
            msg_Err( p_demux, "unable to capture a frame" );
            return NULL;
        }
    }
    else
    {
        result = dc1394_single_capture( p_sys->camera_info.handle,
                                        &p_sys->camera );
        if( result != DC1394_SUCCESS )
        {
            msg_Err( p_demux, "unable to capture a frame" );
            return NULL;
        }
    }

    p_block = block_New( p_demux, p_sys->camera.frame_width *
                                  p_sys->camera.frame_height * 2 );
    if( !p_block )
    {
        msg_Err( p_demux, "cannot get block" );
        return NULL;
    }

    if( !p_sys->camera.capture_buffer )
    {
        msg_Err (p_demux, "caputer buffer empty");
        block_Release( p_block );
        return NULL;
    }

    memcpy( p_block->p_buffer, (const char *)p_sys->camera.capture_buffer,
            p_sys->camera.frame_width * p_sys->camera.frame_height * 2 );

    p_block->i_pts = p_block->i_dts = mdate();
    if( p_sys->dma_capture )
        dc1394_dma_done_with_buffer( &p_sys->camera );
    return p_block;
}

static block_t *GrabAudio( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    struct audio_buf_info buf_info;
    block_t *p_block = NULL;
    int i_read = 0;
    int i_correct = 0;
    int result = 0;

    p_block = block_New( p_demux, p_sys->i_audio_max_frame_size );
    if( !p_block )
    {
        msg_Warn( p_demux, "cannot get buffer" );
        return NULL;
    }

    i_read = read( p_sys->fd_audio, p_block->p_buffer,
                   p_sys->i_audio_max_frame_size );

    if( i_read <= 0 )
        return NULL;

    p_block->i_buffer = i_read;

    /* Correct the date because of kernel buffering */
    i_correct = i_read;
    result = ioctl( p_sys->fd_audio, SNDCTL_DSP_GETISPACE, &buf_info );
    if( result == 0 )
        i_correct += buf_info.bytes;

    p_block->i_pts = p_block->i_dts =
754
                        mdate() - INT64_C(1000000) * (mtime_t)i_correct /
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800
                        2 / p_sys->channels / p_sys->i_sample_rate;
    return p_block;
}

static int Demux( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    block_t *p_blocka = NULL;
    block_t *p_blockv = NULL;

    /* Try grabbing audio frames first */
    if( p_sys->fd_audio > 0 )
        p_blocka = GrabAudio( p_demux );

    /* Try grabbing video frame */
    if( (int)p_sys->fd_video > 0 )
        p_blockv = GrabVideo( p_demux );

    if( !p_blocka && !p_blockv )
    {
        /* Sleep so we do not consume all the cpu, 10ms seems
         * like a good value (100fps)
         */
        msleep( 10000 );
        return 1;
    }

    if( p_blocka )
    {
        es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_blocka->i_pts );
        es_out_Send( p_demux->out, p_sys->p_es_audio, p_blocka );
    }

    if( p_blockv )
    {
        es_out_Control( p_demux->out, ES_OUT_SET_PCR, p_blockv->i_pts );
        es_out_Send( p_demux->out, p_sys->p_es_video, p_blockv );
    }
    return 1;
}

/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( demux_t *p_demux, int i_query, va_list args )
{
801
    bool *pb;
802 803 804 805 806 807
    int64_t    *pi64;

    switch( i_query )
    {
        /* Special for access_demux */
        case DEMUX_CAN_PAUSE:
808
        case DEMUX_CAN_SEEK:
809 810
        case DEMUX_SET_PAUSE_STATE:
        case DEMUX_CAN_CONTROL_PACE:
811 812
            pb = (bool*)va_arg( args, bool * );
            *pb = false;
813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834
            return VLC_SUCCESS;

        case DEMUX_GET_PTS_DELAY:
            pi64 = (int64_t*)va_arg( args, int64_t * );
            *pi64 = (int64_t)DEFAULT_PTS_DELAY;
            return VLC_SUCCESS;

        case DEMUX_GET_TIME:
            pi64 = (int64_t*)va_arg( args, int64_t * );
            *pi64 = mdate();
            return VLC_SUCCESS;

        /* TODO implement others */
        default:
            return VLC_EGENERIC;
    }
    return VLC_EGENERIC;
}

static int process_options( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
835
    char *psz_dup;
836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
    char *psz_parser;
    char *token = NULL;
    char *state = NULL;
    float rate_f;

    if( strncmp(p_demux->psz_access, "dc1394", 6) != 0 )
        return VLC_EGENERIC;

    psz_dup = strdup( p_demux->psz_path );
    psz_parser = psz_dup;
    for( token = strtok_r( psz_parser,":",&state); token;
         token = strtok_r( NULL, ":", &state ) )
    {
        if( strncmp( token, "size=", strlen("size=") ) == 0 )
        {
            token += strlen("size=");
            if( strncmp( token, "160x120", 7 ) == 0 )
            {
                /* TODO - UYV444 chroma converter is needed ...
                    * video size of 160x120 is temporarily disabled
                    */
                msg_Err( p_demux,
                    "video size of 160x120 is actually disabled for lack of chroma "
                    "support. It will relased ASAP, until then try an higher size "
                    "(320x240 and 640x480 are fully supported)" );
861
                free( psz_dup );
862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887
                return VLC_EGENERIC;
#if 0
                p_sys->frame_size = MODE_160x120_YUV444;
                p_sys->width = 160;
                p_sys->height = 120;
#endif
            }
            else if( strncmp( token, "320x240", 7 ) == 0 )
            {
                p_sys->frame_size = MODE_320x240_YUV422;
                p_sys->width = 320;
                p_sys->height = 240;
            }
            else if( strncmp( token, "640x480", 7 ) == 0 )
            {
                p_sys->frame_size = MODE_640x480_YUV422;
                p_sys->width = 640;
                p_sys->height = 480;
            }
            else
            {
                msg_Err( p_demux,
                    "This program currently suppots frame sizes of"
                    " 160x120, 320x240, and 640x480. "
                    "Please specify one of them. You have specified %s.",
                    token );
888
                free( psz_dup );
889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915
                return VLC_EGENERIC;
            }
            msg_Dbg( p_demux, "Requested video size : %s",token );
        }
        else if( strncmp( token, "fps=", strlen( "fps=" ) ) == 0 )
        {
            token += strlen("fps=");
            sscanf( token, "%g", &rate_f );
            if( rate_f == 1.875 )
                p_sys->frame_rate = FRAMERATE_1_875;
            else if( rate_f == 3.75 )
                p_sys->frame_rate = FRAMERATE_3_75;
            else if( rate_f == 7.5 )
                p_sys->frame_rate = FRAMERATE_7_5;
            else if( rate_f == 15 )
                p_sys->frame_rate = FRAMERATE_15;
            else if( rate_f == 30 )
                p_sys->frame_rate = FRAMERATE_30;
            else if( rate_f == 60 )
                p_sys->frame_rate = FRAMERATE_60;
            else
            {
                msg_Err( p_demux ,
                    "This program supports framerates of"
                    " 1.875, 3.75, 7.5, 15, 30, 60. "
                    "Please specify one of them. You have specified %s.",
                    token);
916
                free( psz_dup );
917 918 919 920 921 922 923 924 925 926 927 928 929 930
                return VLC_EGENERIC;
            }
            msg_Dbg( p_demux, "Requested frame rate : %s",token );
        }
        else if( strncmp( token, "brightness=", strlen( "brightness=" ) ) == 0 )
        {
            int nr = 0;
            token += strlen("brightness=");
            nr = sscanf( token, "%u", &p_sys->brightness);
            if( nr != 1 )
            {
                msg_Err( p_demux, "Bad brightness value '%s', "
                                  "must be an unsigned integer.",
                                  token );
931
                free( psz_dup );
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959
                return VLC_EGENERIC;
            }
        }
#if 0
        else if( strncmp( token, "controller=", strlen( "controller=" ) ) == 0 )
        {
            int nr = 0;
            token += strlen("controller=");
            nr = sscanf( token, "%u", &p_sys->controller );
            if( nr != 1)
            {
                msg_Err(p_demux, "Bad controller value '%s', "
                                 "must be an unsigned integer.",
                                 token );
                return VLC_EGENERIC;
            }
        }
#endif
        else if( strncmp( token, "camera=", strlen( "camera=" ) ) == 0 )
        {
            int nr = 0;
            token += strlen("camera=");
            nr = sscanf(token,"%u",&p_sys->selected_camera);
            if( nr != 1)
            {
                msg_Err( p_demux, "Bad camera number '%s', "
                                  "must be an unsigned integer.",
                                  token );
960
                free( psz_dup );
961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981
                return VLC_EGENERIC;
            }
        }
        else if( strncmp( token, "capture=", strlen( "capture=" ) ) == 0)
        {
            token += strlen("capture=");
            if( strncmp(token, "raw1394",7) == 0 )
            {
                msg_Dbg( p_demux, "DMA capture disabled!" );
                p_sys->dma_capture = DMA_OFF;
            }
            else if( strncmp(token,"video1394",9) == 0 )
            {
                msg_Dbg( p_demux, "DMA capture enabled!" );
                p_sys->dma_capture = DMA_ON;
            }
            else
            {
                msg_Err(p_demux, "Bad capture method value '%s', "
                                 "it can be 'raw1394' or 'video1394'.",
                                token );
982
                free( psz_dup );
983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012
                return VLC_EGENERIC;
            }
        }
        else if( strncmp( token, "adev=", strlen( "adev=" ) ) == 0 )
        {
            token += strlen("adev=");
            p_sys->audio_device = strdup(token);
            msg_Dbg( p_demux, "Using audio device '%s'.", token );
        }
        else if( strncmp( token, "samplerate=", strlen( "samplerate=" ) ) == 0 )
        {
            token += strlen("samplerate=");
            sscanf( token, "%d", &p_sys->i_sample_rate );
        }
        else if( strncmp( token, "channels=", strlen("channels=" ) ) == 0 )
        {
            token += strlen("channels=");
            sscanf( token, "%d", &p_sys->channels );
        }
        else if( strncmp( token, "focus=", strlen("focus=" ) ) == 0)
        {
            token += strlen("focus=");
            sscanf( token, "%u", &p_sys->focus );
        }
        else if( strncmp( token, "uid=", strlen("uid=") ) == 0)
        {
            token += strlen("uid=");
            sscanf( token, "0x%llx", &p_sys->selected_uid );
        }
    }
1013
    free( psz_dup );
1014 1015 1016
    return VLC_SUCCESS;
}