vcd.c 26.9 KB
Newer Older
1 2 3
/*****************************************************************************
 * vcd.c : VCD input module for vlc
 *****************************************************************************
Gildas Bazin's avatar
 
Gildas Bazin committed
4
 * Copyright (C) 2000-2004 VideoLAN
5
 * $Id$
6 7 8 9 10 11 12
 *
 * Author: Johan Bilien <jobi@via.ecp.fr>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
13
 *
14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>

#include <vlc/vlc.h>
#include <vlc/input.h>

32 33 34 35 36 37 38
#include "cdrom.h"

/*****************************************************************************
 * Module descriptior
 *****************************************************************************/
static int  Open ( vlc_object_t * );
static void Close( vlc_object_t * );
39

40 41 42 43 44
#define CACHING_TEXT N_("Caching value in ms")
#define CACHING_LONGTEXT N_( \
    "Allows you to modify the default caching value for cdda streams. This " \
    "value should be set in milliseconds units." )

45 46
vlc_module_begin();
    set_description( _("VCD input") );
47
    set_capability( "access2", 10 );
48
    set_callbacks( Open, Close );
49

50
    add_usage_hint( N_("[vcd:][device][@[title][,[chapter]]]") );
51 52 53
    add_integer( "vcd-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
                 CACHING_LONGTEXT, VLC_TRUE );
    add_shortcut( "vcd" );
54 55
    add_shortcut( "svcd" );
vlc_module_end();
56

57 58 59
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
60 61 62 63 64

/* how many blocks VCDRead will read in each loop */
#define VCD_BLOCKS_ONCE 20
#define VCD_DATA_ONCE   (VCD_BLOCKS_ONCE * VCD_DATA_SIZE)

65
struct access_sys_t
Gildas Bazin's avatar
 
Gildas Bazin committed
66 67
{
    vcddev_t    *vcddev;                            /* vcd device descriptor */
68 69 70 71 72 73

    /* Title infos */
    int           i_titles;
    input_title_t *title[99];            /* No more that 99 track in a vcd ? */


Gildas Bazin's avatar
 
Gildas Bazin committed
74
    int         i_sector;                                  /* Current Sector */
75
    int         *p_sectors;                                 /* Track sectors */
76
};
Gildas Bazin's avatar
 
Gildas Bazin committed
77

78 79 80 81 82
static block_t *Block( access_t * );
static int      Seek( access_t *, int64_t );
static int      Control( access_t *, int, va_list );

static int      EntryPoints( access_t * );
83 84 85 86

/*****************************************************************************
 * VCDOpen: open vcd
 *****************************************************************************/
87
static int Open( vlc_object_t *p_this )
88
{
89 90 91
    access_t     *p_access = (access_t *)p_this;
    access_sys_t *p_sys;
    char *psz_dup = strdup( p_access->psz_path );
92
    char *psz;
93 94 95 96
    int i_title = 0;
    int i_chapter = 0;
    int i;
    vcddev_t *vcddev;
97

98 99
    /* Command line: vcd://[dev_path][@title[,chapter]] */
    if( ( psz = strchr( psz_dup, '@' ) ) )
100
    {
101
        *psz++ = '\0';
102

103 104
        i_title = strtol( psz, &psz, 0 );
        if( *psz )
105
            i_chapter = strtol( psz+1, &psz, 0 );
106 107
    }

108
    if( *psz_dup == '\0' )
109
    {
110 111 112
        free( psz_dup );

        /* Only when selected */
113 114
        if( strcmp( p_access->psz_access, "vcd" ) &&
            strcmp( p_access->psz_access, "svcd" ) )
115 116
            return VLC_EGENERIC;

117
        psz_dup = var_CreateGetString( p_access, "vcd" );
118
        if( *psz_dup == '\0' )
119
        {
120 121
            free( psz_dup );
            return VLC_EGENERIC;
122 123 124
        }
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
125
    /* Open VCD */
126
    if( !(vcddev = ioctl_Open( p_this, psz_dup )) )
Gildas Bazin's avatar
 
Gildas Bazin committed
127
    {
128
        msg_Warn( p_access, "could not open %s", psz_dup );
129 130
        free( psz_dup );
        return VLC_EGENERIC;
Gildas Bazin's avatar
 
Gildas Bazin committed
131
    }
132
    free( psz_dup );
133

134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    /* Set up p_access */
    p_access->pf_read = NULL;
    p_access->pf_block = Block;
    p_access->pf_control = Control;
    p_access->pf_seek = Seek;
    p_access->info.i_update = 0;
    p_access->info.i_size = 0;
    p_access->info.i_pos = 0;
    p_access->info.b_eof = VLC_FALSE;
    p_access->info.i_title = 0;
    p_access->info.i_seekpoint = 0;
    p_access->p_sys = p_sys = malloc( sizeof( access_sys_t ) );
    memset( p_sys, 0, sizeof( access_sys_t ) );
    p_sys->vcddev = vcddev;

    /* We read the Table Of Content information */
    p_sys->i_titles = ioctl_GetTracksMap( VLC_OBJECT(p_access),
                                          p_sys->vcddev, &p_sys->p_sectors );
    if( p_sys->i_titles < 0 )
153
    {
154 155
        msg_Err( p_access, "unable to count tracks" );
        goto error;
156
    }
157 158 159 160 161 162 163
    else if( p_sys->i_titles <= 1 )
    {
        msg_Err( p_access, "no movie tracks found" );
        goto error;
    }
    /* The first title isn't usable */
    p_sys->i_titles--;
164

165 166 167 168
    /* Build title table */
    for( i = 0; i < p_sys->i_titles; i++ )
    {
        input_title_t *t = p_sys->title[i] = vlc_input_title_New();
169

170 171
        fprintf( stderr, "title[%d] start=%d\n", i, p_sys->p_sectors[1+i] );
        fprintf( stderr, "title[%d] end=%d\n", i, p_sys->p_sectors[i+2] );
172

173 174 175
        t->i_size = ( p_sys->p_sectors[i+2] - p_sys->p_sectors[i+1] ) *
                    (int64_t)VCD_DATA_SIZE;
    }
176

177 178 179 180
    /* Map entry points into chapters */
    if( EntryPoints( p_access ) )
    {
        msg_Warn( p_access, "could not read entry points, will not use them" );
181 182
    }

183 184 185 186 187
    /* Starting title/chapter and sector */
    if( i_title >= p_sys->i_titles )
        i_title = 0;
    if( i_chapter >= p_sys->title[i_title]->i_seekpoint )
        i_chapter = 0;
188

189 190
    p_sys->i_sector = p_sys->p_sectors[1+i_title];
    if( i_chapter > 0 )
191
    {
192 193
        int64_t i_off = p_sys->title[i_title]->seekpoint[i_chapter]->i_byte_offset;
        p_sys->i_sector += i_off / VCD_DATA_SIZE;
194
    }
195 196 197 198
    p_access->info.i_title = i_title;
    p_access->info.i_seekpoint = i_chapter;
    p_access->info.i_size = p_sys->title[i_title]->i_size;
    p_access->info.i_pos = ( p_sys->i_sector - p_sys->p_sectors[1+i_title] ) * VCD_DATA_SIZE;
199

200
    p_access->psz_demux = strdup( "ps" );
201

202
    return VLC_SUCCESS;
203

204 205 206 207 208
error:
    ioctl_Close( VLC_OBJECT(p_access), p_sys->vcddev );
    free( p_sys );
    return VLC_EGENERIC;
}
209

210 211 212 213 214 215 216 217 218 219 220
/*****************************************************************************
 * Close: closes vcd
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
    access_t     *p_access = (access_t *)p_this;
    access_sys_t *p_sys = p_access->p_sys;

    ioctl_Close( p_this, p_sys->vcddev );
    free( p_sys );
}
221

222 223 224 225 226 227 228 229 230 231 232 233 234
/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( access_t *p_access, int i_query, va_list args )
{
    access_sys_t *p_sys = p_access->p_sys;
    vlc_bool_t   *pb_bool;
    int          *pi_int;
    int64_t      *pi_64;
    input_title_t ***ppp_title;
    int i;

    switch( i_query )
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
        /* */
        case ACCESS_CAN_SEEK:
        case ACCESS_CAN_FASTSEEK:
        case ACCESS_CAN_PAUSE:
        case ACCESS_CAN_CONTROL_PACE:
            pb_bool = (vlc_bool_t*)va_arg( args, vlc_bool_t* );
            *pb_bool = VLC_TRUE;
            break;

        /* */
        case ACCESS_GET_MTU:
            pi_int = (int*)va_arg( args, int * );
            *pi_int = VCD_DATA_ONCE;
            break;

        case ACCESS_GET_PTS_DELAY:
            pi_64 = (int64_t*)va_arg( args, int64_t * );
            *pi_64 = (int64_t)var_GetInteger( p_access, "vcd-caching" ) * I64C(1000);
            break;

        /* */
        case ACCESS_SET_PAUSE_STATE:
            break;

        case ACCESS_GET_TITLE_INFO:
            ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
            pi_int    = (int*)va_arg( args, int* );

            /* Duplicate title infos */
            *pi_int = p_sys->i_titles;
            *ppp_title = malloc( sizeof( input_title_t ** ) * p_sys->i_titles );
            for( i = 0; i < p_sys->i_titles; i++ )
            {
                (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
            }
            break;
272

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
        case ACCESS_SET_TITLE:
            i = (int)va_arg( args, int );
            if( i != p_access->info.i_title )
            {
                /* Update info */
                p_access->info.i_update |= INPUT_UPDATE_TITLE|INPUT_UPDATE_SEEKPOINT|INPUT_UPDATE_SIZE;
                p_access->info.i_title = i;
                p_access->info.i_seekpoint = 0;
                p_access->info.i_size = p_sys->title[i]->i_size;
                p_access->info.i_pos  = 0;

                /* Next sector to read */
                p_sys->i_sector = p_sys->p_sectors[1+i];
            }
            break;
288

289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
        case ACCESS_SET_SEEKPOINT:
        {
            input_title_t *t = p_sys->title[p_access->info.i_title];
            i = (int)va_arg( args, int );
            if( t->i_seekpoint > 0 )
            {
                p_access->info.i_update |= INPUT_UPDATE_SEEKPOINT;
                p_access->info.i_seekpoint = i;

                p_sys->i_sector = p_sys->p_sectors[1+p_access->info.i_title] +
                                  t->seekpoint[i]->i_byte_offset / VCD_DATA_SIZE;

                p_access->info.i_pos = (int64_t)(p_sys->i_sector - p_sys->p_sectors[1+p_access->info.i_title] ) *
                                       (int64_t)VCD_DATA_SIZE;
            }
            return VLC_SUCCESS;
        }

        default:
            msg_Err( p_access, "unimplemented query in control" );
            return VLC_EGENERIC;
310 311

    }
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Block:
 *****************************************************************************/
static block_t *Block( access_t *p_access )
{
    access_sys_t *p_sys = p_access->p_sys;
    int i_skip = p_access->info.i_pos % VCD_DATA_SIZE;
    block_t      *p_block;
    int          i_blocks;
    int          i_read;
    int          i;

    if( p_access->info.b_eof )
        return NULL;
329

330 331
    /* Read raw data */
    i_blocks = VCD_BLOCKS_ONCE;
332

333 334
    /* Don't read after a title */
    if( p_sys->i_sector + i_blocks >= p_sys->p_sectors[p_access->info.i_title + 2] )
335
    {
336 337
        i_blocks = p_sys->p_sectors[p_access->info.i_title + 2 ] - p_sys->i_sector;
        if( i_blocks <= 0 ) i_blocks = 1;   /* Should never occur */
338 339
    }

340
    p_block = block_New( p_access, i_blocks * VCD_DATA_SIZE );
341

342 343 344 345 346
    if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev, p_sys->i_sector,
                           p_block->p_buffer, i_blocks, VCD_TYPE ) < 0 )
    {
        msg_Err( p_access, "cannot read a sector" );
        block_Release( p_block );
347

348 349 350 351 352 353 354
        p_block = NULL;
        i_blocks = 1;   /* Next sector */
    }


    i_read = 0;
    for( i = 0; i < i_blocks; i++ )
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
        input_title_t *t = p_sys->title[p_access->info.i_title];

        /* A good sector read */
        i_read++;
        p_sys->i_sector++;

        /* Check end of title */
        if( p_sys->i_sector >= p_sys->p_sectors[p_access->info.i_title + 2] )
        {
            if( p_access->info.i_title + 2 >= p_sys->i_titles )
            {
                p_access->info.b_eof = VLC_TRUE;
                break;
            }
            p_access->info.i_update |= INPUT_UPDATE_TITLE|INPUT_UPDATE_SEEKPOINT|INPUT_UPDATE_SIZE;
            p_access->info.i_title++;
            p_access->info.i_seekpoint = 0;
            p_access->info.i_size = p_sys->title[p_access->info.i_title]->i_size;
            p_access->info.i_pos = 0;
        }
        else if( t->i_seekpoint > 0 &&
                 p_access->info.i_seekpoint + 1 < t->i_seekpoint &&
                 p_access->info.i_pos - i_skip + i_read * VCD_DATA_SIZE >= t->seekpoint[p_access->info.i_seekpoint+1]->i_byte_offset )
        {
            fprintf( stderr, "seekpoint change\n" );
            p_access->info.i_update |= INPUT_UPDATE_SEEKPOINT;
            p_access->info.i_seekpoint++;
        }
        /* TODO */
385
    }
386

387 388 389 390 391
    if( i_read <= 0 )
    {
        block_Release( p_block );
        return NULL;
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
392

393 394 395 396 397 398 399 400 401 402 403 404 405
    if( p_block )
    {

        /* Really read data */
        p_block->i_buffer = i_read * VCD_DATA_SIZE;
        /* */
        p_block->i_buffer -= i_skip;
        p_block->p_buffer += i_skip;

        p_access->info.i_pos += p_block->i_buffer;
    }

    return p_block;
406 407 408
}

/*****************************************************************************
409
 * Seek:
410
 *****************************************************************************/
411
static int Seek( access_t *p_access, int64_t i_pos )
412
{
413 414
    return VLC_EGENERIC;
}
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
/*****************************************************************************
 * EntryPoints:
 *****************************************************************************/
static int EntryPoints( access_t *p_access )
{
    access_sys_t *p_sys = p_access->p_sys;
    uint8_t      sector[VCD_DATA_SIZE];

    entries_sect_t entries;
    int i_nb;
    int i;


    /* Read the entry point sector */
    if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
        VCD_ENTRIES_SECTOR, sector, 1, VCD_TYPE ) < 0 )
    {
        msg_Err( p_access, "could not read entry points sector" );
        return VLC_EGENERIC;
    }
    memcpy( &entries, sector, CD_SECTOR_SIZE );

    i_nb = GetWBE( &entries.i_entries_nb );
    if( i_nb > 500 )
    {
        msg_Err( p_access, "invalid entry points number" );
        return VLC_EGENERIC;
    }

    if( strncmp( entries.psz_id, "ENTRYVCD", sizeof( entries.psz_id ) ) &&
        strncmp( entries.psz_id, "ENTRYSVD", sizeof( entries.psz_id ) ) )
    {
        msg_Err( p_access, "unrecognized entry points format" );
        return VLC_EGENERIC;
    }

    for( i = 0; i < i_nb; i++ )
    {
        const int i_title = BCD_TO_BIN(entries.entry[i].i_track) - 2;
        const int i_sector =
            (MSF_TO_LBA2( BCD_TO_BIN( entries.entry[i].msf.minute ),
                          BCD_TO_BIN( entries.entry[i].msf.second ),
                          BCD_TO_BIN( entries.entry[i].msf.frame  ) ));
        seekpoint_t *s;

        if( i_title < 0 )
            continue;   /* Should not occur */
        if( i_title >= p_sys->i_titles )
            continue;

        fprintf( stderr, "Entry[%d] title=%d sector=%d\n",
                 i, i_title, i_sector );

        s = vlc_seekpoint_New();
        s->i_byte_offset = (i_sector - p_sys->p_sectors[i_title+1]) * VCD_DATA_SIZE;

        TAB_APPEND( p_sys->title[i_title]->i_seekpoint, p_sys->title[i_title]->seekpoint, s );
    }

#if 0
#define i_track BCD_TO_BIN(entries.entry[i].i_track)
    /* Reset the i_part_nb for each track */
    for( i = 0 ; i < i_nb ; i++ )
    {
        if( i_track <= p_input->stream.i_area_nb )
        {
            p_input->stream.pp_areas[i_track-1]->i_part_nb = 0;
        }
    }

    for( i = 0 ; i < i_nb ; i++ )
    {
        if( i_track <= p_input->stream.i_area_nb )
        {
            p_sys->p_entries[i_entry_index] =
                (MSF_TO_LBA2( BCD_TO_BIN( entries.entry[i].msf.minute ),
                              BCD_TO_BIN( entries.entry[i].msf.second ),
                              BCD_TO_BIN( entries.entry[i].msf.frame  ) ));
            p_input->stream.pp_areas[i_track-1]->i_part_nb ++;
            /* if this entry belongs to a new track */
            if( i_track != i_previous_track )
            {
                /* i_plugin_data is used to store the first entry of the area*/
                p_input->stream.pp_areas[i_track-1]->i_plugin_data =
                                                            i_entry_index;
                i_previous_track = i_track;
            }
            msg_Dbg( p_input, "entry point %i begins at LBA: %i",
                     i_entry_index, p_sys->p_entries[i_entry_index] );

            i_entry_index ++;
            p_sys->i_entries_nb ++;
        }
        else
            msg_Warn( p_input, "wrong track number found in entry points" );
    }
#undef i_track
    return 0;
#endif
    return VLC_EGENERIC;
516 517
}

518
#if 0
519 520 521 522 523 524
/*****************************************************************************
 * VCDRead: reads from the VCD into PES packets.
 *****************************************************************************
 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
 * bytes.
 *****************************************************************************/
525
static int VCDRead( input_thread_t * p_input, byte_t * p_buffer,
526 527
                     size_t i_len )
{
528
    access_sys_t *p_sys;
529 530 531 532 533
    int                     i_blocks;
    int                     i_index;
    int                     i_read;
    byte_t                  p_last_sector[ VCD_DATA_SIZE ];

534
    p_sys = p_input->p_access_data;
535 536 537 538 539 540 541

    i_read = 0;

    /* Compute the number of blocks we have to read */

    i_blocks = i_len / VCD_DATA_SIZE;

542
    for ( i_index = 0 ; i_index < i_blocks ; i_index++ )
543
    {
544 545
        if ( ioctl_ReadSectors( VLC_OBJECT(p_input), p_sys->vcddev,
             p_sys->i_sector, p_buffer + i_index * VCD_DATA_SIZE, 1,
Gildas Bazin's avatar
 
Gildas Bazin committed
546
             VCD_TYPE ) < 0 )
547
        {
548
            msg_Err( p_input, "could not read sector %d", p_sys->i_sector );
549 550 551
            return -1;
        }

552 553
        p_sys->i_sector ++;
        if ( p_sys->i_sector == p_sys->p_sectors[p_sys->i_track + 1] )
554 555
        {
            input_area_t *p_area;
556

557
            if ( p_sys->i_track >= p_sys->i_nb_tracks - 1 )
558
                return 0; /* EOF */
559 560

            vlc_mutex_lock( &p_input->stream.stream_lock );
561 562
            p_area = p_input->stream.pp_areas[
                    p_input->stream.p_selected_area->i_id + 1 ];
563

564
            msg_Dbg( p_input, "new title" );
565

566 567
            p_area->i_part = 1;
            VCDSetArea( p_input, p_area );
568
            vlc_mutex_unlock( &p_input->stream.stream_lock );
569
        }
570 571

        /* Update chapter */
572
        else if( p_sys->b_valid_ep &&
573 574 575 576 577 578 579 580 581 582 583
                /* FIXME kludge so that read does not update chapter
                 * when a manual chapter change was requested and not
                 * yet accomplished */
                !p_input->stream.p_new_area )
        {
            int i_entry;

            vlc_mutex_lock( &p_input->stream.stream_lock );
            i_entry = p_input->stream.p_selected_area->i_plugin_data
                /* 1st entry point of the track (area)*/
                        + p_input->stream.p_selected_area->i_part - 1;
584 585
            if( i_entry + 1 < p_sys->i_entries_nb &&
                    p_sys->i_sector >= p_sys->p_entries[i_entry + 1] )
586
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
587 588
                vlc_value_t val;

589 590
                msg_Dbg( p_input, "new chapter" );
                p_input->stream.p_selected_area->i_part ++;
Gildas Bazin's avatar
 
Gildas Bazin committed
591 592 593 594 595

                /* Update the navigation variables without triggering
                 * a callback */
                val.i_int = p_input->stream.p_selected_area->i_part;
                var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &val, NULL );
596 597 598 599
            }
            vlc_mutex_unlock( &p_input->stream.stream_lock );
        }

600 601
        i_read += VCD_DATA_SIZE;
    }
602

603
    if ( i_len % VCD_DATA_SIZE ) /* this should not happen */
604
    {
605 606
        if ( ioctl_ReadSectors( VLC_OBJECT(p_input), p_sys->vcddev,
             p_sys->i_sector, p_last_sector, 1, VCD_TYPE ) < 0 )
607
        {
608
            msg_Err( p_input, "could not read sector %d", p_sys->i_sector );
609 610
            return -1;
        }
611

612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634
        p_input->p_vlc->pf_memcpy( p_buffer + i_blocks * VCD_DATA_SIZE,
                                   p_last_sector, i_len % VCD_DATA_SIZE );
        i_read += i_len % VCD_DATA_SIZE;
    }

    return i_read;
}

/*****************************************************************************
 * VCDSetProgram: Does nothing since a VCD is mono_program
 *****************************************************************************/
static int VCDSetProgram( input_thread_t * p_input,
                          pgrm_descriptor_t * p_program)
{
    return 0;
}

/*****************************************************************************
 * VCDSetArea: initialize input data for title x, chapter y.
 * It should be called for each user navigation request.
 ****************************************************************************/
static int VCDSetArea( input_thread_t * p_input, input_area_t * p_area )
{
635
    access_sys_t *p_sys = p_input->p_access_data;
Gildas Bazin's avatar
 
Gildas Bazin committed
636
    vlc_value_t val;
637 638 639 640 641 642

    /* we can't use the interface slider until initilization is complete */
    p_input->stream.b_seekable = 0;

    if( p_area != p_input->stream.p_selected_area )
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
643 644
        unsigned int i;

645 646 647 648 649 650 651 652 653
        /* Reset the Chapter position of the current title */
        p_input->stream.p_selected_area->i_part = 1;
        p_input->stream.p_selected_area->i_tell = 0;

        /* Change the default area */
        p_input->stream.p_selected_area = p_area;

        /* Change the current track */
        /* The first track is not a valid one  */
654 655
        p_sys->i_track = p_area->i_id;
        p_sys->i_sector = p_sys->p_sectors[p_sys->i_track];
Gildas Bazin's avatar
 
Gildas Bazin committed
656 657 658

        /* Update the navigation variables without triggering a callback */
        val.i_int = p_area->i_id;
Gildas Bazin's avatar
 
Gildas Bazin committed
659 660
        var_Change( p_input, "title", VLC_VAR_SETVALUE, &val, NULL );
        var_Change( p_input, "chapter", VLC_VAR_CLEARCHOICES, NULL, NULL );
Gildas Bazin's avatar
 
Gildas Bazin committed
661 662 663
        for( i = 1; i <= p_area->i_part_nb; i++ )
        {
            val.i_int = i;
Gildas Bazin's avatar
 
Gildas Bazin committed
664
            var_Change( p_input, "chapter", VLC_VAR_ADDCHOICE, &val, NULL );
Gildas Bazin's avatar
 
Gildas Bazin committed
665
        }
666 667
    }

668
    if( p_sys->b_valid_ep )
669 670 671 672
    {
        int i_entry = p_area->i_plugin_data /* 1st entry point of
                                               the track (area)*/
                            + p_area->i_part - 1;
673
        p_sys->i_sector = p_sys->p_entries[i_entry];
674 675
    }
    else
676
        p_sys->i_sector = p_sys->p_sectors[p_sys->i_track];
677 678

    p_input->stream.p_selected_area->i_tell =
679
        (off_t)p_sys->i_sector * (off_t)VCD_DATA_SIZE
680 681
         - p_input->stream.p_selected_area->i_start;

682 683 684 685
    /* warn interface that something has changed */
    p_input->stream.b_seekable = 1;
    p_input->stream.b_changed = 1;

Gildas Bazin's avatar
 
Gildas Bazin committed
686 687
    /* Update the navigation variables without triggering a callback */
    val.i_int = p_area->i_part;
Gildas Bazin's avatar
 
Gildas Bazin committed
688
    var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &val, NULL );
Gildas Bazin's avatar
 
Gildas Bazin committed
689

690 691 692 693 694 695 696 697
    return 0;
}

/****************************************************************************
 * VCDSeek
 ****************************************************************************/
static void VCDSeek( input_thread_t * p_input, off_t i_off )
{
698
    access_sys_t * p_sys = p_input->p_access_data;
699
    unsigned int i_index;
700

701
    p_sys->i_sector = p_sys->p_sectors[p_sys->i_track]
702 703
                       + i_off / (off_t)VCD_DATA_SIZE;

704 705 706
    vlc_mutex_lock( &p_input->stream.stream_lock );
#define p_area p_input->stream.p_selected_area
    /* Find chapter */
707
    if( p_sys->b_valid_ep )
708
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
709
        for( i_index = 2 ; i_index <= p_area->i_part_nb; i_index ++ )
710
        {
711
            if( p_sys->i_sector < p_sys->p_entries[p_area->i_plugin_data
Gildas Bazin's avatar
 
Gildas Bazin committed
712
                + i_index - 1] )
713
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
714 715 716 717 718 719 720 721
                vlc_value_t val;

                p_area->i_part = i_index - 1;

                /* Update the navigation variables without triggering
                 * a callback */
                val.i_int = p_area->i_part;
                var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &val, NULL );
722 723 724 725 726 727 728
                break;
            }
        }
    }
#undef p_area

    p_input->stream.p_selected_area->i_tell =
729
        (off_t)p_sys->i_sector * (off_t)VCD_DATA_SIZE
730
         - p_input->stream.p_selected_area->i_start;
731 732 733 734 735 736 737 738
    vlc_mutex_unlock( &p_input->stream.stream_lock );
}

/*****************************************************************************
 * VCDEntryPoints: Reads the information about the entry points on the disc.
 *****************************************************************************/
static int VCDEntryPoints( input_thread_t * p_input )
{
739
    access_sys_t * p_sys = p_input->p_access_data;
740 741 742 743 744 745 746 747 748 749 750 751 752
    byte_t *                          p_sector;
    entries_sect_t                    entries;
    uint16_t                          i_nb;
    int                               i, i_entry_index = 0;
    int                               i_previous_track = -1;

    p_sector = malloc( VCD_DATA_SIZE * sizeof( byte_t ) );
    if( p_sector == NULL )
    {
        msg_Err( p_input, "not enough memory for entry points treatment" );
        return -1;
    }

753
    if( ioctl_ReadSectors( VLC_OBJECT(p_input), p_sys->vcddev,
Gildas Bazin's avatar
 
Gildas Bazin committed
754
        VCD_ENTRIES_SECTOR, p_sector, 1, VCD_TYPE ) < 0 )
755 756 757 758 759 760 761 762 763 764 765 766 767 768 769
    {
        msg_Err( p_input, "could not read entry points sector" );
        free( p_sector );
        return( -1 );
    }

    memcpy( &entries, p_sector, CD_SECTOR_SIZE );
    free( p_sector );

    if( (i_nb = U16_AT( &entries.i_entries_nb )) > 500 )
    {
        msg_Err( p_input, "invalid entry points number" );
        return( -1 );
    }

770 771
    p_sys->p_entries = malloc( sizeof( int ) * i_nb );
    if( p_sys->p_entries == NULL )
772 773 774 775 776 777 778 779 780
    {
        msg_Err( p_input, "not enough memory for entry points treatment" );
        return -1;
    }

    if( strncmp( entries.psz_id, "ENTRYVCD", sizeof( entries.psz_id ) )
     && strncmp( entries.psz_id, "ENTRYSVD", sizeof( entries.psz_id ) ))
    {
        msg_Err( p_input, "unrecognized entry points format" );
781
        free( p_sys->p_entries );
782 783 784
        return -1;
    }

785
    p_sys->i_entries_nb = 0;
786

787
#define i_track BCD_TO_BIN(entries.entry[i].i_track)
Gildas Bazin's avatar
 
Gildas Bazin committed
788 789 790 791 792 793 794 795 796
    /* Reset the i_part_nb for each track */
    for( i = 0 ; i < i_nb ; i++ )
    {
        if( i_track <= p_input->stream.i_area_nb )
        {
            p_input->stream.pp_areas[i_track-1]->i_part_nb = 0;
        }
    }

797 798 799 800
    for( i = 0 ; i < i_nb ; i++ )
    {
        if( i_track <= p_input->stream.i_area_nb )
        {
801
            p_sys->p_entries[i_entry_index] =
802 803 804 805 806 807 808 809 810 811 812 813
                (MSF_TO_LBA2( BCD_TO_BIN( entries.entry[i].msf.minute ),
                              BCD_TO_BIN( entries.entry[i].msf.second ),
                              BCD_TO_BIN( entries.entry[i].msf.frame  ) ));
            p_input->stream.pp_areas[i_track-1]->i_part_nb ++;
            /* if this entry belongs to a new track */
            if( i_track != i_previous_track )
            {
                /* i_plugin_data is used to store the first entry of the area*/
                p_input->stream.pp_areas[i_track-1]->i_plugin_data =
                                                            i_entry_index;
                i_previous_track = i_track;
            }
Gildas Bazin's avatar
 
Gildas Bazin committed
814
            msg_Dbg( p_input, "entry point %i begins at LBA: %i",
815
                     i_entry_index, p_sys->p_entries[i_entry_index] );
Gildas Bazin's avatar
 
Gildas Bazin committed
816

817
            i_entry_index ++;
818
            p_sys->i_entries_nb ++;
819 820 821 822 823 824
        }
        else
            msg_Warn( p_input, "wrong track number found in entry points" );
    }
#undef i_track
    return 0;
825
}
826
#endif