linsys_hdsdi.c 35.1 KB
Newer Older
1 2 3 4 5 6 7
/*****************************************************************************
 * linsys_hdsdi.c: HDSDI capture for Linear Systems/Computer Modules cards
 *****************************************************************************
 * Copyright (C) 2010-2011 VideoLAN
 *
 * Authors: Christophe Massiot <massiot@via.ecp.fr>
 *
8 9 10
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation; either version 2.1 of the License, or
11 12 13 14
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
17
 *
18 19 20
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 22 23 24 25 26 27 28 29 30 31 32 33 34
 *****************************************************************************/

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <poll.h>
#include <sys/mman.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
35
#include <sys/eventfd.h>
36 37 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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
#include <fcntl.h>
#include <errno.h>

#include <vlc_common.h>
#include <vlc_plugin.h>

#include <vlc_input.h>
#include <vlc_access.h>
#include <vlc_demux.h>

#include <vlc_fs.h>

#include "linsys_sdivideo.h"
#include "linsys_sdiaudio.h"

#undef HAVE_MMAP_SDIVIDEO
#undef HAVE_MMAP_SDIAUDIO

#define SDIVIDEO_DEVICE         "/dev/sdivideorx%u"
#define SDIVIDEO_BUFFERS_FILE   "/sys/class/sdivideo/sdivideorx%u/buffers"
#define SDIVIDEO_BUFSIZE_FILE   "/sys/class/sdivideo/sdivideorx%u/bufsize"
#define SDIVIDEO_MODE_FILE      "/sys/class/sdivideo/sdivideorx%u/mode"
#define SDIAUDIO_DEVICE         "/dev/sdiaudiorx%u"
#define SDIAUDIO_BUFFERS_FILE   "/sys/class/sdiaudio/sdiaudiorx%u/buffers"
#define SDIAUDIO_BUFSIZE_FILE   "/sys/class/sdiaudio/sdiaudiorx%u/bufsize"
#define SDIAUDIO_SAMPLESIZE_FILE "/sys/class/sdiaudio/sdiaudiorx%u/sample_size"
#define SDIAUDIO_CHANNELS_FILE  "/sys/class/sdiaudio/sdiaudiorx%u/channels"
#define NB_VBUFFERS             2
#define CLOCK_GAP               INT64_C(500000)
#define START_DATE              INT64_C(4294967296)

#define MAX_AUDIOS              4

/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
#define LINK_TEXT N_("Link #")
#define LINK_LONGTEXT N_( \
    "Allows you to set the desired link of the board for the capture (starting at 0)." )
#define VIDEO_TEXT N_("Video ID")
#define VIDEO_LONGTEXT N_( \
    "Allows you to set the ES ID of the video." )
#define VIDEO_ASPECT_TEXT N_("Aspect ratio")
#define VIDEO_ASPECT_LONGTEXT N_( \
    "Allows you to force the aspect ratio of the video." )
#define AUDIO_TEXT N_("Audio configuration")
#define AUDIO_LONGTEXT N_( \
    "Allows you to set audio configuration (id=group,pair:id=group,pair...)." )

static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );

vlc_module_begin()
89
    set_description( N_("HD-SDI Input") )
90
    set_shortname( N_("HD-SDI") )
91 92 93 94 95 96 97 98 99 100 101 102 103
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_ACCESS )

    add_integer( "linsys-hdsdi-link", 0,
        LINK_TEXT, LINK_LONGTEXT, true )

    add_integer( "linsys-hdsdi-id-video", 0,
        VIDEO_TEXT, VIDEO_LONGTEXT, true )
    add_string( "linsys-hdsdi-aspect-ratio", "",
        VIDEO_ASPECT_TEXT, VIDEO_ASPECT_LONGTEXT, true )
    add_string( "linsys-hdsdi-audio", "0=1,1",
        AUDIO_TEXT, AUDIO_LONGTEXT, true )

104
    set_capability( "access", 0 )
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
    add_shortcut( "linsys-hdsdi" )
    set_callbacks( Open, Close )
vlc_module_end()

/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
typedef struct hdsdi_audio_t
{
    int         i_channel; /* i_group * 2 + i_pair */

    /* HDSDI parser */
    int32_t     i_delay;

    /* ES stuff */
    int         i_id;
    es_out_id_t *p_es;
} hdsdi_audio_t;

124
typedef struct
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
{
    /* video device reader */
    int          i_vfd;
    unsigned int i_link;
    unsigned int i_standard;
#ifdef HAVE_MMAP_SDIVIDEO
    uint8_t      **pp_vbuffers;
    unsigned int i_vbuffers, i_current_vbuffer;
#endif
    unsigned int i_vbuffer_size;

    /* audio device reader */
    int          i_afd;
    int          i_max_channel;
    unsigned int i_sample_rate;
#ifdef HAVE_MMAP_SDIAUDIO
    uint8_t      **pp_abuffers;
    unsigned int i_abuffers, i_current_abuffer;
#endif
    unsigned int i_abuffer_size;

    /* picture decoding */
    unsigned int i_frame_rate, i_frame_rate_base;
    unsigned int i_width, i_height, i_aspect, i_forced_aspect;
    unsigned int i_vblock_size, i_ablock_size;
    mtime_t      i_next_vdate, i_next_adate;
    int          i_incr, i_aincr;

    /* ES stuff */
    int          i_id_video;
    es_out_id_t  *p_es_video;
    hdsdi_audio_t p_audios[MAX_AUDIOS];
157 158 159

    pthread_t thread;
    int evfd;
160
} demux_sys_t;
161 162

static int Control( demux_t *, int, va_list );
163
static void *Demux( void * );
164 165 166 167 168 169 170 171 172 173 174 175 176 177

static int InitCapture( demux_t *p_demux );
static void CloseCapture( demux_t *p_demux );
static int Capture( demux_t *p_demux );

/*****************************************************************************
 * DemuxOpen:
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    demux_t     *p_demux = (demux_t *)p_this;
    demux_sys_t *p_sys;
    char        *psz_parser;

178 179 180
    if (p_demux->out == NULL)
        return VLC_EGENERIC;

181 182 183 184 185 186
    /* Fill p_demux field */
    p_demux->p_sys = p_sys = calloc( 1, sizeof( demux_sys_t ) );
    if( unlikely(!p_sys) )
        return VLC_ENOMEM;

    /* HDSDI AR */
187 188 189 190 191 192 193
    unsigned int i_num, i_den;
    if ( !var_InheritURational( p_demux, &i_num, &i_den,
                               "linsys-hdsdi-aspect-ratio" ) && i_den != 0 )
        p_sys->i_forced_aspect = p_sys->i_aspect =
                i_num * VOUT_ASPECT_FACTOR / i_den;
    else
        p_sys->i_forced_aspect = 0;
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

    /* */
    p_sys->i_id_video = var_InheritInteger( p_demux, "linsys-hdsdi-id-video" );

    /* Audio ES */
    char *psz_string = psz_parser = var_InheritString( p_demux,
                                                       "linsys-hdsdi-audio" );
    int i = 0;
    p_sys->i_max_channel = -1;

    while ( psz_parser != NULL && *psz_parser )
    {
        int i_id, i_group, i_pair;
        char *psz_next = strchr( psz_parser, '=' );
        if ( psz_next != NULL )
        {
            *psz_next = '\0';
            i_id = strtol( psz_parser, NULL, 0 );
            psz_parser = psz_next + 1;
        }
        else
            i_id = 0;

        psz_next = strchr( psz_parser, ':' );
        if ( psz_next != NULL )
        {
            *psz_next = '\0';
            psz_next++;
        }

        if ( sscanf( psz_parser, "%d,%d", &i_group, &i_pair ) == 2 )
        {
            p_sys->p_audios[i].i_channel = (i_group - 1) * 2 + (i_pair - 1);
            if ( p_sys->p_audios[i].i_channel > p_sys->i_max_channel )
                p_sys->i_max_channel = p_sys->p_audios[i].i_channel;
            p_sys->p_audios[i].i_id = i_id;
            i++;
        }
        else
            msg_Warn( p_demux, "malformed audio configuration (%s)",
                      psz_parser );

        psz_parser = psz_next;
    }
    free( psz_string );
    for ( ; i < MAX_AUDIOS; i++ )
        p_sys->p_audios[i].i_channel = -1;


    p_sys->i_link = var_InheritInteger( p_demux, "linsys-hdsdi-link" );

245 246 247 248 249
    p_sys->evfd = eventfd( 0, EFD_CLOEXEC );
    if( p_sys->evfd == -1 )
        goto error;

    if( pthread_create( &p_sys->thread, NULL, Demux, p_demux ) )
250
    {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
251
        vlc_close( p_sys->evfd );
252
        goto error;
253 254
    }

255 256
    p_demux->pf_demux = NULL;
    p_demux->pf_control = Control;
257
    return VLC_SUCCESS;
258 259 260
error:
    free( p_sys );
    return VLC_EGENERIC;
261 262 263 264 265 266 267 268 269 270
}

/*****************************************************************************
 * DemuxClose:
 *****************************************************************************/
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;

271 272
    write( p_sys->evfd, &(uint64_t){ 1 }, sizeof (uint64_t));
    pthread_join( p_sys->thread, NULL );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
273
    vlc_close( p_sys->evfd );
274 275 276 277 278 279
    free( p_sys );
}

/*****************************************************************************
 * DemuxDemux:
 *****************************************************************************/
280
static void *Demux( void *opaque )
281
{
282 283 284 285 286 287 288 289 290
    demux_t *p_demux = opaque;

    if( InitCapture( p_demux ) != VLC_SUCCESS )
        return NULL;

    while( Capture( p_demux ) == VLC_SUCCESS );

    CloseCapture( p_demux );
    return NULL;
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
}

/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( demux_t *p_demux, int i_query, va_list args )
{
    bool *pb;
    int64_t *pi64;

    switch( i_query )
    {
        /* Special for access_demux */
        case DEMUX_CAN_PAUSE:
        case DEMUX_CAN_CONTROL_PACE:
            /* TODO */
307
            pb = va_arg( args, bool * );
308 309 310 311
            *pb = false;
            return VLC_SUCCESS;

        case DEMUX_GET_PTS_DELAY:
312
            pi64 = va_arg( args, int64_t * );
313 314
            *pi64 = INT64_C(1000)
                  * var_InheritInteger( p_demux, "live-caching" );
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
            return VLC_SUCCESS;

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

/*****************************************************************************
 * HDSDI syntax parsing stuff
 *****************************************************************************/
#define U   (uint16_t)(p_line[0])
#define Y1  (uint16_t)(p_line[1])
#define V   (uint16_t)(p_line[2])
#define Y2  (uint16_t)(p_line[3])

/* For lines 0 [4] or 1 [4] */
static void Unpack01( const uint8_t *p_line, unsigned int i_size,
                      uint8_t *p_y, uint8_t *p_u, uint8_t *p_v )
{
    const uint8_t *p_end = p_line + i_size;

    while ( p_line < p_end )
    {
        *p_u++ = U;
        *p_y++ = Y1;
        *p_v++ = V;
        *p_y++ = Y2;
        p_line += 4;
    }
}

/* For lines 2 [4] */
static void Unpack2( const uint8_t *p_line, unsigned int i_size,
                     uint8_t *p_y, uint8_t *p_u, uint8_t *p_v )
{
    const uint8_t *p_end = p_line + i_size;

    while ( p_line < p_end )
    {
        uint16_t tmp;
        tmp = 3 * *p_u;
        tmp += U;
        *p_u++ = tmp / 4;
        *p_y++ = Y1;
        tmp = 3 * *p_v;
        tmp += V;
        *p_v++ = tmp / 4;
        *p_y++ = Y2;
        p_line += 4;
    }
}

/* For lines 3 [4] */
static void Unpack3( const uint8_t *p_line, unsigned int i_size,
                     uint8_t *p_y, uint8_t *p_u, uint8_t *p_v )
{
    const uint8_t *p_end = p_line + i_size;

    while ( p_line < p_end )
    {
        uint16_t tmp;
        tmp = *p_u;
        tmp += 3 * U;
        *p_u++ = tmp / 4;
        *p_y++ = Y1;
        tmp = *p_v;
        tmp += 3 * V;
        *p_v++ = tmp / 4;
        *p_y++ = Y2;
        p_line += 4;
    }
}

#undef U
#undef Y1
#undef V
#undef Y2

static void SparseCopy( int16_t *p_dest, const int16_t *p_src,
                        size_t i_nb_samples, size_t i_offset, size_t i_stride )
{
    for ( size_t i = 0; i < i_nb_samples; i++ )
    {
        p_dest[2 * i] = p_src[i_offset];
        p_dest[2 * i + 1] = p_src[i_offset + 1];
        i_offset += 2 * i_stride;
    }
}

/*****************************************************************************
 * Video & audio decoding
 *****************************************************************************/
struct block_extension_t
{
    bool            b_progressive;          /**< is it a progressive frame ? */
    bool            b_top_field_first;             /**< which field is first */
    unsigned int    i_nb_fields;                  /**< # of displayed fields */
    unsigned int    i_aspect;                     /**< aspect ratio of frame */
};

static void StopDecode( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;

    es_out_Del( p_demux->out, p_sys->p_es_video );

    for ( int i = 0; i < MAX_AUDIOS; i++ )
    {
        hdsdi_audio_t *p_audio = &p_sys->p_audios[i];
        if ( p_audio->i_channel != -1 && p_audio->p_es != NULL )
        {
            es_out_Del( p_demux->out, p_audio->p_es );
            p_audio->p_es = NULL;
        }
    }
}

static int InitVideo( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    es_format_t fmt;

    msg_Dbg( p_demux, "found standard %d", p_sys->i_standard );
    switch ( p_sys->i_standard )
    {
    case SDIVIDEO_CTL_BT_601_576I_50HZ:
        /* PAL */
        p_sys->i_frame_rate      = 25;
        p_sys->i_frame_rate_base = 1;
        p_sys->i_width           = 720;
        p_sys->i_height          = 576;
        p_sys->i_aspect          = 4 * VOUT_ASPECT_FACTOR / 3;
        break;

    case SDIVIDEO_CTL_SMPTE_296M_720P_50HZ:
        p_sys->i_frame_rate      = 50;
        p_sys->i_frame_rate_base = 1;
        p_sys->i_width           = 1280;
        p_sys->i_height          = 720;
        p_sys->i_aspect          = 16 * VOUT_ASPECT_FACTOR / 9;
        break;

    case SDIVIDEO_CTL_SMPTE_296M_720P_60HZ:
        p_sys->i_frame_rate      = 60;
        p_sys->i_frame_rate_base = 1;
        p_sys->i_width           = 1280;
        p_sys->i_height          = 720;
        p_sys->i_aspect          = 16 * VOUT_ASPECT_FACTOR / 9;
        break;

    case SDIVIDEO_CTL_SMPTE_295M_1080I_50HZ:
    case SDIVIDEO_CTL_SMPTE_274M_1080I_50HZ:
    case SDIVIDEO_CTL_SMPTE_274M_1080PSF_25HZ:
        /* 1080i50 or 1080p25 */
        p_sys->i_frame_rate      = 25;
        p_sys->i_frame_rate_base = 1;
        p_sys->i_width           = 1920;
        p_sys->i_height          = 1080;
        p_sys->i_aspect          = 16 * VOUT_ASPECT_FACTOR / 9;
        break;

    case SDIVIDEO_CTL_SMPTE_274M_1080I_59_94HZ:
        p_sys->i_frame_rate      = 30000;
        p_sys->i_frame_rate_base = 1001;
        p_sys->i_width           = 1920;
        p_sys->i_height          = 1080;
        p_sys->i_aspect          = 16 * VOUT_ASPECT_FACTOR / 9;
        break;

    case SDIVIDEO_CTL_SMPTE_274M_1080I_60HZ:
        p_sys->i_frame_rate      = 30;
        p_sys->i_frame_rate_base = 1;
        p_sys->i_width           = 1920;
        p_sys->i_height          = 1080;
        p_sys->i_aspect          = 16 * VOUT_ASPECT_FACTOR / 9;
        break;

    default:
        msg_Err( p_demux, "unsupported standard %d", p_sys->i_standard );
        return VLC_EGENERIC;
    }

    p_sys->i_next_vdate = START_DATE;
    p_sys->i_incr = 1000000 * p_sys->i_frame_rate_base / p_sys->i_frame_rate;
    p_sys->i_vblock_size = p_sys->i_width * p_sys->i_height * 3 / 2
                            + sizeof(struct block_extension_t);

    /* Video ES */
504
    es_format_Init( &fmt, VIDEO_ES, VLC_CODEC_I420 );
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
    fmt.i_id                    = p_sys->i_id_video;
    fmt.video.i_frame_rate      = p_sys->i_frame_rate;
    fmt.video.i_frame_rate_base = p_sys->i_frame_rate_base;
    fmt.video.i_width           = fmt.video.i_visible_width = p_sys->i_width;
    fmt.video.i_height          = fmt.video.i_visible_height = p_sys->i_height;
    fmt.video.i_sar_num         = p_sys->i_aspect * fmt.video.i_height
                                  / fmt.video.i_width;
    fmt.video.i_sar_den         = VOUT_ASPECT_FACTOR;
    p_sys->p_es_video           = es_out_Add( p_demux->out, &fmt );

    return VLC_SUCCESS;
}

static int InitAudio( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
    es_format_t fmt;

    for ( int i = 0; i < MAX_AUDIOS; i++ )
    {
        hdsdi_audio_t *p_audio = &p_sys->p_audios[i];

        if ( p_audio->i_channel == -1 ) continue;

        msg_Dbg( p_demux, "starting audio %u/%u rate:%u delay:%d",
                 1 + p_audio->i_channel / 2, 1 + (p_audio->i_channel % 2),
                 p_sys->i_sample_rate, p_audio->i_delay );

533
        es_format_Init( &fmt, AUDIO_ES, VLC_CODEC_S16L );
534 535
        fmt.i_id = p_audio->i_id;
        fmt.audio.i_channels          = 2;
536
        fmt.audio.i_physical_channels = AOUT_CHANS_STEREO;
537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
        fmt.audio.i_rate              = p_sys->i_sample_rate;
        fmt.audio.i_bitspersample     = 16;
        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;
        p_audio->p_es = es_out_Add( p_demux->out, &fmt );
    }

    p_sys->i_next_adate = START_DATE;
    p_sys->i_ablock_size = p_sys->i_sample_rate * 4 * p_sys->i_frame_rate_base / p_sys->i_frame_rate;
    p_sys->i_aincr = 1000000. * p_sys->i_ablock_size / p_sys->i_sample_rate / 4;

    return VLC_SUCCESS;
}

static int HandleVideo( demux_t *p_demux, const uint8_t *p_buffer )
{
    demux_sys_t *p_sys = p_demux->p_sys;
556
    block_t *p_current_picture = block_Alloc( p_sys->i_vblock_size );
557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 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
    if( unlikely( !p_current_picture ) )
        return VLC_ENOMEM;
    uint8_t *p_y = p_current_picture->p_buffer;
    uint8_t *p_u = p_y + p_sys->i_width * p_sys->i_height;
    uint8_t *p_v = p_u + p_sys->i_width * p_sys->i_height / 4;
    unsigned int i_total_size = p_sys->i_width * 2;
    unsigned int i_current_line;
    struct block_extension_t ext;

    for ( i_current_line = 0; i_current_line < p_sys->i_height;
          i_current_line++ )
    {
        bool b_field = (i_current_line >= p_sys->i_height / 2);
        unsigned int i_field_line = b_field ?
            i_current_line - (p_sys->i_height + 1) / 2 :
            i_current_line;
        unsigned int i_real_line = b_field + i_field_line * 2;
        const uint8_t *p_line = p_buffer + i_current_line * p_sys->i_width * 2;

        if ( !(i_field_line % 2) && !b_field )
            Unpack01( p_line, i_total_size,
                      p_y + p_sys->i_width * i_real_line,
                      p_u + (p_sys->i_width / 2) * (i_real_line / 2),
                      p_v + (p_sys->i_width / 2) * (i_real_line / 2) );
        else if ( !(i_field_line % 2) )
            Unpack01( p_line, i_total_size,
                      p_y + p_sys->i_width * i_real_line,
                      p_u + (p_sys->i_width / 2) * (i_real_line / 2 + 1),
                      p_v + (p_sys->i_width / 2) * (i_real_line / 2 + 1) );
       else if ( !b_field )
            Unpack2( p_line, i_total_size,
                     p_y + p_sys->i_width * i_real_line,
                     p_u + (p_sys->i_width / 2) * (i_real_line / 2 - 1),
                     p_v + (p_sys->i_width / 2) * (i_real_line / 2 - 1) );
       else
            Unpack3( p_line, i_total_size,
                     p_y + p_sys->i_width * i_real_line,
                     p_u + (p_sys->i_width / 2) * (i_real_line / 2),
                     p_v + (p_sys->i_width / 2) * (i_real_line / 2) );
    }

    /* FIXME: progressive formats ? */
    ext.b_progressive = false;
    ext.i_nb_fields = 2;
    ext.b_top_field_first = true;
    ext.i_aspect = p_sys->i_forced_aspect ? p_sys->i_forced_aspect :
                   p_sys->i_aspect;

    memcpy( &p_current_picture->p_buffer[p_sys->i_vblock_size
                                          - sizeof(struct block_extension_t)],
            &ext, sizeof(struct block_extension_t) );

    p_current_picture->i_dts = p_current_picture->i_pts = p_sys->i_next_vdate;
    es_out_Send( p_demux->out, p_sys->p_es_video, p_current_picture );

612
    es_out_SetPCR( p_demux->out, p_sys->i_next_vdate );
613 614 615 616 617 618 619 620 621 622 623 624 625
    p_sys->i_next_vdate += p_sys->i_incr;
    return VLC_SUCCESS;
}

static int HandleAudio( demux_t *p_demux, const uint8_t *p_buffer )
{
    demux_sys_t *p_sys = p_demux->p_sys;

    for ( int i = 0; i < MAX_AUDIOS; i++ )
    {
        hdsdi_audio_t *p_audio = &p_sys->p_audios[i];
        if ( p_audio->i_channel != -1 && p_audio->p_es != NULL )
        {
626
            block_t *p_block = block_Alloc( p_sys->i_ablock_size );
627 628 629 630 631 632 633 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
            if( unlikely( !p_block ) )
                return VLC_ENOMEM;
            SparseCopy( (int16_t *)p_block->p_buffer, (const int16_t *)p_buffer,
                        p_sys->i_ablock_size / 4,
                        p_audio->i_channel * 2, p_sys->i_max_channel + 1 );

            p_block->i_dts = p_block->i_pts
                = p_sys->i_next_adate + (mtime_t)p_audio->i_delay
                   * INT64_C(1000000) / p_sys->i_sample_rate;
            p_block->i_length = p_sys->i_aincr;
            es_out_Send( p_demux->out, p_audio->p_es, p_block );
        }
    }
    p_sys->i_next_adate += p_sys->i_aincr;
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Low-level device stuff
 *****************************************************************************/
#define MAXLEN 256

static ssize_t WriteULSysfs( const char *psz_fmt, unsigned int i_link,
                             unsigned int i_buf )
{
    char psz_file[MAXLEN], psz_data[MAXLEN];
    int i_fd;
    ssize_t i_ret;

    snprintf( psz_file, sizeof(psz_file) -1, psz_fmt, i_link );

    snprintf( psz_data, sizeof(psz_data) -1, "%u\n", i_buf );

    if ( (i_fd = vlc_open( psz_file, O_WRONLY )) < 0 )
        return i_fd;

    i_ret = write( i_fd, psz_data, strlen(psz_data) + 1 );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
664
    vlc_close( i_fd );
665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684
    return i_ret;
}

static int InitCapture( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
#ifdef HAVE_MMAP_SDIVIDEO
    const int i_page_size = getpagesize();
    unsigned int i_bufmemsize;
#endif
    char psz_vdev[MAXLEN];

    snprintf( psz_vdev, sizeof(psz_vdev), SDIVIDEO_DEVICE, p_sys->i_link );
    if ( (p_sys->i_vfd = vlc_open( psz_vdev, O_RDONLY ) ) < 0 )
    {
        msg_Err( p_demux, "couldn't open device %s", psz_vdev );
        return VLC_EGENERIC;
    }

    /* Wait for standard to settle down */
685
    struct pollfd pfd[2];
686

687 688 689 690
    pfd[0].fd = p_sys->i_vfd;
    pfd[0].events = POLLPRI;
    pfd[1].fd = p_sys->evfd;
    pfd[1].events = POLLIN;
691

692 693 694 695
    for( ;; )
    {
        if( poll( pfd, 2, -1 ) < 0 )
            continue;
696 697 698 699 700 701

        if ( pfd[0].revents & POLLPRI )
        {
            unsigned int i_val;

            if ( ioctl( p_sys->i_vfd, SDIVIDEO_IOC_RXGETEVENTS, &i_val ) < 0 )
702 703
                msg_Warn( p_demux, "couldn't SDIVIDEO_IOC_RXGETEVENTS: %s",
                          vlc_strerror_c(errno) );
704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720
            else
            {
                if ( i_val & SDIVIDEO_EVENT_RX_BUFFER )
                    msg_Warn( p_demux, "driver receive buffer queue overrun" );
                if ( i_val & SDIVIDEO_EVENT_RX_FIFO )
                    msg_Warn( p_demux, "onboard receive FIFO overrun");
                if ( i_val & SDIVIDEO_EVENT_RX_CARRIER )
                    msg_Warn( p_demux, "carrier status change");
                if ( i_val & SDIVIDEO_EVENT_RX_DATA )
                    msg_Warn( p_demux, "data status change");
                if ( i_val & SDIVIDEO_EVENT_RX_STD )
                {
                    msg_Warn( p_demux, "standard status change");
                    break;
                }
            }
        }
721 722 723

        if( pfd[1].revents )
        {
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
724
            vlc_close( p_sys->i_vfd );
725 726
            return VLC_EGENERIC;
        }
727 728 729 730 731
    }

    if ( ioctl( p_sys->i_vfd, SDIVIDEO_IOC_RXGETVIDSTATUS, &p_sys->i_standard )
          < 0 )
    {
732 733
        msg_Warn( p_demux, "couldn't SDIVIDEO_IOC_RXGETVIDSTATUS: %s",
                  vlc_strerror_c(errno) );
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
734
        vlc_close( p_sys->i_vfd );
735 736
        return VLC_EGENERIC;
    }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
737
    vlc_close( p_sys->i_vfd );
738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757

    if ( InitVideo( p_demux ) != VLC_SUCCESS )
        return VLC_EGENERIC;
    p_sys->i_vbuffer_size = p_sys->i_height * p_sys->i_width * 2;

    /* First open the audio for synchronization reasons */
    if ( p_sys->i_max_channel != -1 )
    {
        unsigned int i_rate;
        char psz_adev[MAXLEN];

        snprintf( psz_adev, sizeof(psz_adev), SDIAUDIO_DEVICE, p_sys->i_link );
        if ( (p_sys->i_afd = vlc_open( psz_adev, O_RDONLY ) ) < 0 )
        {
            msg_Err( p_demux, "couldn't open device %s", psz_adev );
            return VLC_EGENERIC;
        }

        if ( ioctl( p_sys->i_afd, SDIAUDIO_IOC_RXGETAUDRATE, &i_rate ) < 0 )
        {
758 759
            msg_Warn( p_demux, "couldn't SDIAUDIO_IOC_RXGETAUDRATE: %s",
                      vlc_strerror_c(errno) );
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
            return VLC_EGENERIC;
        }
        switch ( i_rate )
        {
        case SDIAUDIO_CTL_ASYNC_48_KHZ:
        case SDIAUDIO_CTL_SYNC_48_KHZ:
            p_sys->i_sample_rate = 48000;
            break;
        case SDIAUDIO_CTL_ASYNC_44_1_KHZ:
        case SDIAUDIO_CTL_SYNC_44_1_KHZ:
            p_sys->i_sample_rate = 44100;
            break;
        case SDIAUDIO_CTL_ASYNC_32_KHZ:
        case SDIAUDIO_CTL_SYNC_32_KHZ:
            p_sys->i_sample_rate = 32000;
            break;
        case SDIAUDIO_CTL_ASYNC_96_KHZ:
        case SDIAUDIO_CTL_SYNC_96_KHZ:
            p_sys->i_sample_rate = 96000;
            break;
        case SDIAUDIO_CTL_ASYNC_FREE_RUNNING:
        case SDIAUDIO_CTL_SYNC_FREE_RUNNING:
        default:
            msg_Err( p_demux, "unknown sample rate %u", i_rate );
            return VLC_EGENERIC;
        }
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
786
        vlc_close( p_sys->i_afd );
787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837

        if ( InitAudio( p_demux ) != VLC_SUCCESS )
            return VLC_EGENERIC;
        p_sys->i_abuffer_size = p_sys->i_ablock_size
                                 * (1 + p_sys->i_max_channel);

        /* Use 16-bit audio */
        if ( WriteULSysfs( SDIAUDIO_SAMPLESIZE_FILE, p_sys->i_link,
                           SDIAUDIO_CTL_AUDSAMP_SZ_16 ) < 0 )
        {
            msg_Err( p_demux, "couldn't write file " SDIAUDIO_SAMPLESIZE_FILE,
                     p_sys->i_link );
            return VLC_EGENERIC;
        }

        if ( WriteULSysfs( SDIAUDIO_CHANNELS_FILE, p_sys->i_link,
                           (p_sys->i_max_channel + 1) * 2 ) < 0 )
        {
            msg_Err( p_demux, "couldn't write file " SDIAUDIO_CHANNELS_FILE,
                     p_sys->i_link );
            return VLC_EGENERIC;
        }

#ifdef HAVE_MMAP_SDIAUDIO
        if ( (p_sys->i_abuffers = ReadULSysfs( SDIAUDIO_BUFFERS_FILE,
                                               p_sys->i_link )) < 0 )
        {
            msg_Err( p_demux, "couldn't read file " SDIAUDIO_BUFFERS_FILE,
                     p_sys->i_link );
            return VLC_EGENERIC;
        }
        p_sys->i_current_abuffer = 0;
#endif

        if ( WriteULSysfs( SDIAUDIO_BUFSIZE_FILE, p_sys->i_link,
                           p_sys->i_abuffer_size ) < 0 )
        {
            msg_Err( p_demux, "couldn't write file " SDIAUDIO_BUFSIZE_FILE,
                     p_sys->i_link );
            return VLC_EGENERIC;
        }

        if ( (p_sys->i_afd = open( psz_adev, O_RDONLY ) ) < 0 )
        {
            msg_Err( p_demux, "couldn't open device %s", psz_adev );
            return VLC_EGENERIC;
        }

#ifdef HAVE_MMAP_SDIAUDIO
        i_bufmemsize = ((p_sys->i_abuffer_size + i_page_size - 1) / i_page_size)
                         * i_page_size;
Thomas Guillem's avatar
Thomas Guillem committed
838
        p_sys->pp_abuffers = vlc_alloc( p_sys->i_abuffers, sizeof(uint8_t *) );
839 840 841 842 843 844 845 846
        if( unlikely( !p_sys->pp_abuffers ) )
            return VLC_ENOMEM;
        for ( unsigned int i = 0; i < p_sys->i_abuffers; i++ )
        {
            if ( (p_sys->pp_abuffers[i] = mmap( NULL, p_sys->i_abuffer_size,
                                                PROT_READ, MAP_SHARED, p_sys->i_afd,
                                                i * i_bufmemsize )) == MAP_FAILED )
            {
847 848
                msg_Err( p_demux, "couldn't mmap(%d): %s", i,
                         vlc_strerror_c(errno) );
849 850 851 852 853 854 855 856 857 858 859 860 861 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 888 889 890 891 892
                return VLC_EGENERIC;
            }
        }
#endif
    }

    /* Use 8-bit video */
    if ( WriteULSysfs( SDIVIDEO_MODE_FILE, p_sys->i_link,
                       SDIVIDEO_CTL_MODE_UYVY ) < 0 )
    {
        msg_Err( p_demux, "couldn't write file " SDIVIDEO_MODE_FILE,
                 p_sys->i_link );
        return VLC_EGENERIC;
    }

    if ( WriteULSysfs( SDIVIDEO_BUFFERS_FILE, p_sys->i_link,
                       NB_VBUFFERS ) < 0 )
    {
        msg_Err( p_demux, "couldn't write file " SDIVIDEO_BUFFERS_FILE,
                 p_sys->i_link );
        return VLC_EGENERIC;
    }
#ifdef HAVE_MMAP_SDIVIDEO
    p_sys->i_vbuffers = NB_VBUFFERS;
#endif

    if ( WriteULSysfs( SDIVIDEO_BUFSIZE_FILE, p_sys->i_link,
                       p_sys->i_vbuffer_size ) < 0 )
    {
        msg_Err( p_demux, "couldn't write file " SDIVIDEO_BUFSIZE_FILE,
                 p_sys->i_link );
        return VLC_EGENERIC;
    }

    if ( (p_sys->i_vfd = open( psz_vdev, O_RDONLY ) ) < 0 )
    {
        msg_Err( p_demux, "couldn't open device %s", psz_vdev );
        return VLC_EGENERIC;
    }

#ifdef HAVE_MMAP_SDIVIDEO
    p_sys->i_current_vbuffer = 0;
    i_bufmemsize = ((p_sys->i_vbuffer_size + i_page_size - 1) / i_page_size)
                     * i_page_size;
Thomas Guillem's avatar
Thomas Guillem committed
893
    p_sys->pp_vbuffers = vlc_alloc( p_sys->i_vbuffers, sizeof(uint8_t *) );
894 895 896 897 898 899 900 901
    if( unlikely( !p_sys->pp_vbuffers ) )
        return VLC_ENOMEM;
    for ( unsigned int i = 0; i < p_sys->i_vbuffers; i++ )
    {
        if ( (p_sys->pp_vbuffers[i] = mmap( NULL, p_sys->i_vbuffer_size,
                                            PROT_READ, MAP_SHARED, p_sys->i_vfd,
                                            i * i_bufmemsize )) == MAP_FAILED )
        {
902 903
            msg_Err( p_demux, "couldn't mmap(%d): %s", i,
                     vlc_strerror_c(errno) );
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921
            return VLC_EGENERIC;
        }
    }
#endif

    return VLC_SUCCESS;
}

static void CloseCapture( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;

    StopDecode( p_demux );
#ifdef HAVE_MMAP_SDIVIDEO
    for ( unsigned int i = 0; i < p_sys->i_vbuffers; i++ )
        munmap( p_sys->pp_vbuffers[i], p_sys->i_vbuffer_size );
    free( p_sys->pp_vbuffers );
#endif
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
922
    vlc_close( p_sys->i_vfd );
923 924 925 926 927 928 929
    if ( p_sys->i_max_channel != -1 )
    {
#ifdef HAVE_MMAP_SDIAUDIO
        for ( unsigned int i = 0; i < p_sys->i_abuffers; i++ )
            munmap( p_sys->pp_abuffers[i], p_sys->i_abuffer_size );
        free( p_sys->pp_abuffers );
#endif
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
930
        vlc_close( p_sys->i_afd );
931 932 933 934 935 936
    }
}

static int Capture( demux_t *p_demux )
{
    demux_sys_t *p_sys = p_demux->p_sys;
937
    struct pollfd pfd[3];
938

939 940 941 942
    pfd[0].fd = p_sys->evfd;
    pfd[0].events = POLLIN;
    pfd[1].fd = p_sys->i_vfd;
    pfd[1].events = POLLIN | POLLPRI;
943 944
    if ( p_sys->i_max_channel != -1 )
    {
945 946
        pfd[2].fd = p_sys->i_afd;
        pfd[2].events = POLLIN | POLLPRI;
947 948
    }

949
    if( poll( pfd, 2 + (p_sys->i_max_channel != -1), -1 ) < 0 )
950
        return VLC_SUCCESS;
951

952 953 954 955
    if( pfd[0].revents )
        return VLC_EGENERIC; /* Stop! */

    if( pfd[1].revents & POLLPRI )
956 957 958 959
    {
        unsigned int i_val;

        if ( ioctl( p_sys->i_vfd, SDIVIDEO_IOC_RXGETEVENTS, &i_val ) < 0 )
960 961
            msg_Warn( p_demux, "couldn't SDIVIDEO_IOC_RXGETEVENTS: %s",
                      vlc_strerror_c(errno) );
962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979
        else
        {
            if ( i_val & SDIVIDEO_EVENT_RX_BUFFER )
                msg_Warn( p_demux, "driver receive buffer queue overrun" );
            if ( i_val & SDIVIDEO_EVENT_RX_FIFO )
                msg_Warn( p_demux, "onboard receive FIFO overrun");
            if ( i_val & SDIVIDEO_EVENT_RX_CARRIER )
                msg_Warn( p_demux, "carrier status change");
            if ( i_val & SDIVIDEO_EVENT_RX_DATA )
                msg_Warn( p_demux, "data status change");
            if ( i_val & SDIVIDEO_EVENT_RX_STD )
                msg_Warn( p_demux, "standard status change");
        }

        p_sys->i_next_adate += CLOCK_GAP;
        p_sys->i_next_vdate += CLOCK_GAP;
    }

980
    if( p_sys->i_max_channel != -1 && (pfd[2].revents & POLLPRI) )
981 982 983 984
    {
        unsigned int i_val;

        if ( ioctl( p_sys->i_afd, SDIAUDIO_IOC_RXGETEVENTS, &i_val ) < 0 )
985 986
            msg_Warn( p_demux, "couldn't SDIAUDIO_IOC_RXGETEVENTS: %s",
                      vlc_strerror_c(errno) );
987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002
        else
        {
            if ( i_val & SDIAUDIO_EVENT_RX_BUFFER )
                msg_Warn( p_demux, "driver receive buffer queue overrun" );
            if ( i_val & SDIAUDIO_EVENT_RX_FIFO )
                msg_Warn( p_demux, "onboard receive FIFO overrun");
            if ( i_val & SDIAUDIO_EVENT_RX_CARRIER )
                msg_Warn( p_demux, "carrier status change");
            if ( i_val & SDIAUDIO_EVENT_RX_DATA )
                msg_Warn( p_demux, "data status change");
        }

        p_sys->i_next_adate += CLOCK_GAP;
        p_sys->i_next_vdate += CLOCK_GAP;
    }

1003
    if( pfd[1].revents & POLLIN )
1004 1005 1006 1007 1008
    {
#ifdef HAVE_MMAP_SDIVIDEO
        if ( ioctl( p_sys->i_vfd, SDIVIDEO_IOC_DQBUF, p_sys->i_current_vbuffer )
              < 0 )
        {
1009 1010
            msg_Warn( p_demux, "couldn't SDIVIDEO_IOC_DQBUF: %s",
                      vlc_strerror_c(errno) );
1011 1012 1013 1014 1015 1016 1017 1018 1019
            return VLC_EGENERIC;
        }

        if( HandleVideo( p_demux, p_sys->pp_vbuffers[p_sys->i_current_vbuffer] ) != VLC_SUCCESS )
            return VLC_ENOMEM;

        if ( ioctl( p_sys->i_vfd, SDIVIDEO_IOC_QBUF, p_sys->i_current_vbuffer )
              < 0 )
        {
1020 1021
            msg_Warn( p_demux, "couldn't SDIVIDEO_IOC_QBUF: %s",
                      vlc_strerror_c(errno) );
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
            return VLC_EGENERIC;
        }

        p_sys->i_current_vbuffer++;
        p_sys->i_current_vbuffer %= p_sys->i_vbuffers;
#else
        uint8_t *p_buffer = malloc( p_sys->i_vbuffer_size );
        if( unlikely( !p_buffer ) )
            return VLC_ENOMEM;

        if ( read( p_sys->i_vfd, p_buffer, p_sys->i_vbuffer_size ) < 0 )
        {
1034
            msg_Warn( p_demux, "couldn't read: %s", vlc_strerror_c(errno) );
Rémi Duraffort's avatar
Rémi Duraffort committed
1035
            free( p_buffer );
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
            return VLC_EGENERIC;
        }

        if( HandleVideo( p_demux, p_buffer ) != VLC_SUCCESS )
        {
            free( p_buffer );
            return VLC_ENOMEM;
        }
        free( p_buffer );
#endif
    }

1048
    if( p_sys->i_max_channel != -1 && (pfd[2].revents & POLLIN) )
1049 1050 1051 1052 1053
    {
#ifdef HAVE_MMAP_SDIAUDIO
        if ( ioctl( p_sys->i_afd, SDIAUDIO_IOC_DQBUF, p_sys->i_current_abuffer )
              < 0 )
        {
1054 1055
            msg_Warn( p_demux, "couldn't SDIAUDIO_IOC_DQBUF: %s",
                      vlc_strerror_c(errno) );
1056 1057 1058 1059 1060 1061 1062 1063 1064
            return VLC_EGENERIC;
        }

        if( HandleAudio( p_demux, p_sys->pp_abuffers[p_sys->i_current_abuffer] ) != VLC_SUCCESS )
            return VLC_ENOMEM;

        if ( ioctl( p_sys->i_afd, SDIAUDIO_IOC_QBUF, p_sys->i_current_abuffer )
              < 0 )
        {
1065 1066
            msg_Warn( p_demux, "couldn't SDIAUDIO_IOC_QBUF: %s",
                      vlc_strerror_c(errno) );
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078
            return VLC_EGENERIC;
        }

        p_sys->i_current_abuffer++;
        p_sys->i_current_abuffer %= p_sys->i_abuffers;
#else
        uint8_t *p_buffer = malloc( p_sys->i_abuffer_size );
        if( unlikely( !p_buffer ) )
            return VLC_ENOMEM;

        if ( read( p_sys->i_afd, p_buffer, p_sys->i_abuffer_size ) < 0 )
        {
1079
            msg_Warn( p_demux, "couldn't read: %s", vlc_strerror_c(errno) );
Rémi Duraffort's avatar
Rémi Duraffort committed
1080
            free( p_buffer );
1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
            return VLC_EGENERIC;
        }

        if( HandleAudio( p_demux, p_buffer ) != VLC_SUCCESS )
        {
            free( p_buffer );
            return VLC_ENOMEM;
        }
        free( p_buffer );
#endif
    }

    return VLC_SUCCESS;
}