vcd.c 15.9 KB
Newer Older
1 2 3
/*****************************************************************************
 * vcd.c : VCD input module for vlc
 *****************************************************************************
4
 * Copyright (C) 2000-2004 the VideoLAN team
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
 * 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
Antoine Cellerier's avatar
Antoine Cellerier committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23 24 25 26 27
 *****************************************************************************/

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

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

32
#include <vlc/vlc.h>
33
#include <vlc_plugin.h>
Clément Stenac's avatar
Clément Stenac committed
34 35
#include <vlc_input.h>
#include <vlc_access.h>
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
36
#include <vlc_charset.h>
37

38 39 40 41 42 43 44
#include "cdrom.h"

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

46 47
#define CACHING_TEXT N_("Caching value in ms")
#define CACHING_LONGTEXT N_( \
48
    "Caching value for VCDs. This " \
Clément Stenac's avatar
Clément Stenac committed
49
    "value should be set in milliseconds." )
50

51
vlc_module_begin();
52 53
    set_shortname( N_("VCD"));
    set_description( N_("VCD input") );
54
    set_capability( "access", 60 );
55
    set_callbacks( Open, Close );
Clément Stenac's avatar
Clément Stenac committed
56 57
    set_category( CAT_INPUT );
    set_subcategory( SUBCAT_INPUT_ACCESS );
58

59
    add_usage_hint( N_("[vcd:][device][@[title][,[chapter]]]") );
60
    add_integer( "vcd-caching", DEFAULT_PTS_DELAY / 1000, NULL, CACHING_TEXT,
61
                 CACHING_LONGTEXT, true );
62
    add_shortcut( "vcd" );
63 64
    add_shortcut( "svcd" );
vlc_module_end();
65

66 67 68
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
69 70 71 72 73

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

74
struct access_sys_t
Gildas Bazin's avatar
 
Gildas Bazin committed
75 76
{
    vcddev_t    *vcddev;                            /* vcd device descriptor */
77 78 79 80 81 82

    /* 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
83
    int         i_sector;                                  /* Current Sector */
84
    int         *p_sectors;                                 /* Track sectors */
85
};
Gildas Bazin's avatar
 
Gildas Bazin committed
86

87 88 89 90
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 * );
91 92 93 94

/*****************************************************************************
 * VCDOpen: open vcd
 *****************************************************************************/
95
static int Open( vlc_object_t *p_this )
96
{
97 98
    access_t     *p_access = (access_t *)p_this;
    access_sys_t *p_sys;
99
    char *psz_dup = ToLocaleDup( p_access->psz_path );
100
    char *psz;
101 102 103 104
    int i_title = 0;
    int i_chapter = 0;
    int i;
    vcddev_t *vcddev;
105

106 107
    /* Command line: vcd://[dev_path][@title[,chapter]] */
    if( ( psz = strchr( psz_dup, '@' ) ) )
108
    {
109
        *psz++ = '\0';
110

111 112
        i_title = strtol( psz, &psz, 0 );
        if( *psz )
113
            i_chapter = strtol( psz+1, &psz, 0 );
114 115
    }

116
    if( *psz_dup == '\0' )
117
    {
118 119 120
        free( psz_dup );

        /* Only when selected */
121 122
        if( strcmp( p_access->psz_access, "vcd" ) &&
            strcmp( p_access->psz_access, "svcd" ) )
123 124
            return VLC_EGENERIC;

125
        psz_dup = var_CreateGetString( p_access, "vcd" );
126
        if( *psz_dup == '\0' )
127
        {
128 129
            free( psz_dup );
            return VLC_EGENERIC;
130 131 132
        }
    }

133 134 135 136 137
#ifdef WIN32
    if( psz_dup[0] && psz_dup[1] == ':' &&
        psz_dup[2] == '\\' && psz_dup[3] == '\0' ) psz_dup[2] = '\0';
#endif

Gildas Bazin's avatar
 
Gildas Bazin committed
138
    /* Open VCD */
139
    if( !(vcddev = ioctl_Open( p_this, psz_dup )) )
Gildas Bazin's avatar
 
Gildas Bazin committed
140
    {
141 142
        free( psz_dup );
        return VLC_EGENERIC;
Gildas Bazin's avatar
 
Gildas Bazin committed
143
    }
144
    free( psz_dup );
145

146 147 148 149 150 151 152 153
    /* 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;
154
    p_access->info.b_eof = false;
155 156 157 158 159 160 161 162 163 164
    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 )
165
    {
166 167
        msg_Err( p_access, "unable to count tracks" );
        goto error;
168
    }
169 170 171 172 173 174 175
    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--;
176

177 178 179 180
    /* Build title table */
    for( i = 0; i < p_sys->i_titles; i++ )
    {
        input_title_t *t = p_sys->title[i] = vlc_input_title_New();
181

Derk-Jan Hartman's avatar
Derk-Jan Hartman committed
182 183
        msg_Dbg( p_access, "title[%d] start=%d\n", i, p_sys->p_sectors[1+i] );
        msg_Dbg( p_access, "title[%d] end=%d\n", i, p_sys->p_sectors[i+2] );
184

185 186 187
        t->i_size = ( p_sys->p_sectors[i+2] - p_sys->p_sectors[i+1] ) *
                    (int64_t)VCD_DATA_SIZE;
    }
188

189 190 191 192
    /* Map entry points into chapters */
    if( EntryPoints( p_access ) )
    {
        msg_Warn( p_access, "could not read entry points, will not use them" );
193 194
    }

195 196 197 198 199
    /* 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;
200

201 202
    p_sys->i_sector = p_sys->p_sectors[1+i_title];
    if( i_chapter > 0 )
203
    {
204 205 206
        p_sys->i_sector +=
            ( p_sys->title[i_title]->seekpoint[i_chapter]->i_byte_offset /
              VCD_DATA_SIZE );
207
    }
208 209 210
    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;
211 212
    p_access->info.i_pos = ( p_sys->i_sector - p_sys->p_sectors[1+i_title] ) *
        VCD_DATA_SIZE;
213

Rafaël Carré's avatar
Rafaël Carré committed
214
    free( p_access->psz_demux );
215
    p_access->psz_demux = strdup( "ps" );
216

217
    return VLC_SUCCESS;
218

219 220 221 222 223
error:
    ioctl_Close( VLC_OBJECT(p_access), p_sys->vcddev );
    free( p_sys );
    return VLC_EGENERIC;
}
224

225 226 227 228 229 230 231 232 233 234 235
/*****************************************************************************
 * 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 );
}
236

237 238 239 240 241 242
/*****************************************************************************
 * Control:
 *****************************************************************************/
static int Control( access_t *p_access, int i_query, va_list args )
{
    access_sys_t *p_sys = p_access->p_sys;
243
    bool   *pb_bool;
244 245 246 247 248 249
    int          *pi_int;
    int64_t      *pi_64;
    input_title_t ***ppp_title;
    int i;

    switch( i_query )
250
    {
251 252 253 254 255
        /* */
        case ACCESS_CAN_SEEK:
        case ACCESS_CAN_FASTSEEK:
        case ACCESS_CAN_PAUSE:
        case ACCESS_CAN_CONTROL_PACE:
256 257
            pb_bool = (bool*)va_arg( args, bool* );
            *pb_bool = true;
258 259 260 261 262 263 264 265 266 267
            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 * );
268
            *pi_64 = var_GetInteger( p_access, "vcd-caching" ) * 1000;
269 270 271 272 273 274 275 276 277 278 279 280
            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;
281
            *ppp_title = malloc( sizeof(input_title_t **) * p_sys->i_titles );
282 283 284 285 286
            for( i = 0; i < p_sys->i_titles; i++ )
            {
                (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
            }
            break;
287

288 289 290 291 292
        case ACCESS_SET_TITLE:
            i = (int)va_arg( args, int );
            if( i != p_access->info.i_title )
            {
                /* Update info */
293 294
                p_access->info.i_update |=
                  INPUT_UPDATE_TITLE|INPUT_UPDATE_SEEKPOINT|INPUT_UPDATE_SIZE;
295 296 297 298 299 300 301 302 303
                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;
304

305 306 307 308 309 310 311 312 313 314
        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] +
315
                    t->seekpoint[i]->i_byte_offset / VCD_DATA_SIZE;
316

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

323
        case ACCESS_SET_PRIVATE_ID_STATE:
324
        case ACCESS_GET_CONTENT_TYPE:
325 326
            return VLC_EGENERIC;

327
        default:
328
            msg_Warn( p_access, "unimplemented query in control" );
329
            return VLC_EGENERIC;
330 331

    }
332 333 334 335 336 337 338 339 340
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Block:
 *****************************************************************************/
static block_t *Block( access_t *p_access )
{
    access_sys_t *p_sys = p_access->p_sys;
341 342 343
    int i_blocks = VCD_BLOCKS_ONCE;
    block_t *p_block;
    int i_read;
344

345 346
    /* Check end of file */
    if( p_access->info.b_eof ) return NULL;
347

348 349 350 351 352
    /* Check end of title */
    while( 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 )
        {
353
            p_access->info.b_eof = true;
354 355
            return NULL;
        }
356

357 358 359 360 361 362 363 364 365 366 367 368
        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;
    }

    /* Don't read after the end of a title */
    if( p_sys->i_sector + i_blocks >=
        p_sys->p_sectors[p_access->info.i_title + 2] )
369
    {
370 371
        i_blocks = p_sys->p_sectors[p_access->info.i_title + 2 ] -
                   p_sys->i_sector;
372 373
    }

374 375 376 377
    /* Do the actual reading */
    if( !( p_block = block_New( p_access, i_blocks * VCD_DATA_SIZE ) ) )
    {
        msg_Err( p_access, "cannot get a new block of size: %i",
378
                 i_blocks * VCD_DATA_SIZE );
379 380
        return NULL;
    }
381

382 383
    if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
            p_sys->i_sector, p_block->p_buffer, i_blocks, VCD_TYPE ) < 0 )
384
    {
385
        msg_Err( p_access, "cannot read sector %i", p_sys->i_sector );
386
        block_Release( p_block );
387 388 389 390

        /* Try to skip one sector (in case of bad sectors) */
        p_sys->i_sector++;
        p_access->info.i_pos += VCD_DATA_SIZE;
391
        return NULL;
392 393
    }

394
    /* Update seekpoints */
395
    for( i_read = 0; i_read < i_blocks; i_read++ )
396
    {
397 398
        input_title_t *t = p_sys->title[p_access->info.i_title];

399 400
        if( t->i_seekpoint > 0 &&
            p_access->info.i_seekpoint + 1 < t->i_seekpoint &&
401
            p_access->info.i_pos + i_read * VCD_DATA_SIZE >=
402
            t->seekpoint[p_access->info.i_seekpoint+1]->i_byte_offset )
403
        {
404
            msg_Dbg( p_access, "seekpoint change" );
405 406 407 408
            p_access->info.i_update |= INPUT_UPDATE_SEEKPOINT;
            p_access->info.i_seekpoint++;
        }
    }
Gildas Bazin's avatar
 
Gildas Bazin committed
409

410
    /* Update a few values */
411
    p_sys->i_sector += i_blocks;
412
    p_access->info.i_pos += p_block->i_buffer;
413 414

    return p_block;
415 416 417
}

/*****************************************************************************
418
 * Seek:
419
 *****************************************************************************/
420
static int Seek( access_t *p_access, int64_t i_pos )
421
{
422 423 424 425 426
    access_sys_t *p_sys = p_access->p_sys;
    input_title_t *t = p_sys->title[p_access->info.i_title];
    int i_seekpoint;

    /* Next sector to read */
427
    p_access->info.i_pos = i_pos;
428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444
    p_sys->i_sector = i_pos / VCD_DATA_SIZE +
        p_sys->p_sectors[p_access->info.i_title + 1];

    /* Update current seekpoint */
    for( i_seekpoint = 0; i_seekpoint < t->i_seekpoint; i_seekpoint++ )
    {
        if( i_seekpoint + 1 >= t->i_seekpoint ) break;
        if( i_pos < t->seekpoint[i_seekpoint + 1]->i_byte_offset ) break;
    }

    if( i_seekpoint != p_access->info.i_seekpoint )
    {
        msg_Dbg( p_access, "seekpoint change" );
        p_access->info.i_update |= INPUT_UPDATE_SEEKPOINT;
        p_access->info.i_seekpoint = i_seekpoint;
    }

445
    /* Reset eof */
446
    p_access->info.b_eof = false;
447

448
    return VLC_SUCCESS;
449
}
450

451
/*****************************************************************************
452
 * EntryPoints: Reads the information about the entry points on the disc.
453 454 455 456 457 458 459
 *****************************************************************************/
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;
460
    int i_nb, i;
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

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

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

497
        msg_Dbg( p_access, "Entry[%d] title=%d sector=%d\n",
498 499 500
                 i, i_title, i_sector );

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

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

508
    return VLC_SUCCESS;
509
}
510