vcd.c 15.1 KB
Newer Older
1 2 3
/*****************************************************************************
 * vcd.c : VCD input module for vlc
 *****************************************************************************
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
4
 * Copyright © 2000-2011 VLC authors and VideoLAN
5
 * $Id$
6 7 8
 *
 * Author: Johan Bilien <jobi@via.ecp.fr>
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
9 10 11
 * 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
12
 * (at your option) any later version.
13
 *
14 15
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
16 17
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
18
 *
Jean-Baptiste Kempf's avatar
Jean-Baptiste Kempf committed
19 20 21
 * 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.
22 23 24 25 26 27
 *****************************************************************************/

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

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

32
#include <vlc_common.h>
33
#include <vlc_plugin.h>
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 48 49 50 51 52
vlc_module_begin ()
    set_shortname( N_("VCD"))
    set_description( N_("VCD input") )
    set_capability( "access", 60 )
    set_callbacks( Open, Close )
    set_category( CAT_INPUT )
    set_subcategory( SUBCAT_INPUT_ACCESS )
53

54
    add_usage_hint( N_("[vcd:][device][#[title][,[chapter]]]") )
55
    add_shortcut( "vcd", "svcd" )
56
vlc_module_end ()
57

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

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

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

    /* Title infos */
    int           i_titles;
73 74 75 76 77
    struct
    {
        uint64_t *seekpoints;
        size_t    count;
    } titles[99];                        /* No more that 99 track in a vcd ? */
78
    int         i_current_title;
79
    unsigned    i_current_seekpoint;
80

Gildas Bazin's avatar
Gildas Bazin committed
81
    int         i_sector;                                  /* Current Sector */
82
    int         *p_sectors;                                 /* Track sectors */
83
};
Gildas Bazin's avatar
Gildas Bazin committed
84

Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
85 86 87 88
static block_t *Block( stream_t *, bool * );
static int      Seek( stream_t *, uint64_t );
static int      Control( stream_t *, int, va_list );
static int      EntryPoints( stream_t * );
89 90 91 92

/*****************************************************************************
 * VCDOpen: open vcd
 *****************************************************************************/
93
static int Open( vlc_object_t *p_this )
94
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
95
    stream_t     *p_access = (stream_t *)p_this;
96
    access_sys_t *p_sys;
97 98 99
    if( p_access->psz_filepath == NULL )
        return VLC_EGENERIC;

100
    char *psz_dup = ToLocaleDup( p_access->psz_filepath );
101
    char *psz;
102 103 104
    int i_title = 0;
    int i_chapter = 0;
    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_name, "vcd" ) &&
            strcmp( p_access->psz_name, "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
#if defined( _WIN32 ) || defined( __OS2__ )
134 135 136 137
    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
    vcddev = ioctl_Open( p_this, psz_dup );
140
    free( psz_dup );
141 142
    if( !vcddev )
        return VLC_EGENERIC;
143

144
    /* Set up p_access */
145
    p_access->p_sys = p_sys = calloc( 1, sizeof( access_sys_t ) );
146
    if( unlikely(!p_sys ))
147
        goto error;
148
    p_sys->vcddev = vcddev;
149
    p_sys->offset = 0;
150

151 152 153
    for( size_t i = 0; i < ARRAY_SIZE(p_sys->titles); i++ )
        p_sys->titles[i].seekpoints = NULL;

154 155 156 157
    /* 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 )
158
    {
159 160
        msg_Err( p_access, "unable to count tracks" );
        goto error;
161
    }
162 163 164 165 166
    else if( p_sys->i_titles <= 1 )
    {
        msg_Err( p_access, "no movie tracks found" );
        goto error;
    }
167

168 169
    /* The first title isn't usable */
    p_sys->i_titles--;
170

171
    for( int i = 0; i < p_sys->i_titles; i++ )
172
    {
173 174
        msg_Dbg( p_access, "title[%d] start=%d", i, p_sys->p_sectors[1+i] );
        msg_Dbg( p_access, "title[%d] end=%d", i, p_sys->p_sectors[i+2] );
175
    }
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
    /* Starting title/chapter and sector */
    if( i_title >= p_sys->i_titles )
        i_title = 0;
186
    if( (unsigned)i_chapter >= p_sys->titles[i_title].count )
187
        i_chapter = 0;
188

189 190
    p_sys->i_sector = p_sys->p_sectors[1+i_title];
    if( i_chapter > 0 )
191 192
        p_sys->i_sector += p_sys->titles[i_title].seekpoints[i_chapter]
                           / VCD_DATA_SIZE;
193 194 195 196 197 198 199

    /* p_access */
    p_access->pf_read    = NULL;
    p_access->pf_block   = Block;
    p_access->pf_control = Control;
    p_access->pf_seek    = Seek;

200 201
    p_sys->i_current_title = i_title;
    p_sys->i_current_seekpoint = i_chapter;
202 203
    p_sys->offset = (uint64_t)(p_sys->i_sector - p_sys->p_sectors[1+i_title]) *
                               VCD_DATA_SIZE;
204

205
    return VLC_SUCCESS;
206

207
error:
208
    ioctl_Close( VLC_OBJECT(p_access), vcddev );
209 210 211
    free( p_sys );
    return VLC_EGENERIC;
}
212

213 214 215 216 217
/*****************************************************************************
 * Close: closes vcd
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
{
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
218
    stream_t     *p_access = (stream_t *)p_this;
219 220
    access_sys_t *p_sys = p_access->p_sys;

221 222 223
    for( size_t i = 0; i < ARRAY_SIZE(p_sys->titles); i++ )
        free( p_sys->titles[i].seekpoints );

224 225 226
    ioctl_Close( p_this, p_sys->vcddev );
    free( p_sys );
}
227

228 229 230
/*****************************************************************************
 * Control:
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
231
static int Control( stream_t *p_access, int i_query, va_list args )
232 233 234 235 236
{
    access_sys_t *p_sys = p_access->p_sys;
    input_title_t ***ppp_title;

    switch( i_query )
237
    {
238
        /* */
239 240 241 242
        case STREAM_CAN_SEEK:
        case STREAM_CAN_FASTSEEK:
        case STREAM_CAN_PAUSE:
        case STREAM_CAN_CONTROL_PACE:
243
            *va_arg( args, bool* ) = true;
244 245
            break;

246
        case STREAM_GET_SIZE:
247 248 249
        {
            int i = p_sys->i_current_title;

250
            *va_arg( args, uint64_t * ) =
251 252
                (p_sys->p_sectors[i + 2] - p_sys->p_sectors[i + 1])
                               * (uint64_t)VCD_DATA_SIZE;
253
            break;
254
        }
255

256
        /* */
257
        case STREAM_GET_PTS_DELAY:
258 259
            *va_arg( args, int64_t * ) = INT64_C(1000)
                * var_InheritInteger(p_access, "disc-caching");
260 261 262
            break;

        /* */
263
        case STREAM_SET_PAUSE_STATE:
264 265
            break;

266
        case STREAM_GET_TITLE_INFO:
267
            ppp_title = va_arg( args, input_title_t*** );
268
            /* Duplicate title infos */
269 270 271 272
            *ppp_title = vlc_alloc( p_sys->i_titles, sizeof(input_title_t *) );
            if (!*ppp_title)
                return VLC_ENOMEM;
            *va_arg( args, int* ) = p_sys->i_titles;
273
            for( int i = 0; i < p_sys->i_titles; i++ )
274
                (*ppp_title)[i] = vlc_input_title_New();
275 276
            break;

277
        case STREAM_GET_TITLE:
278 279 280
            *va_arg( args, unsigned * ) = p_sys->i_current_title;
            break;

281
        case STREAM_GET_SEEKPOINT:
282
            *va_arg( args, unsigned * ) = p_sys->i_current_seekpoint;
283
            break;
284

285
        case STREAM_GET_CONTENT_TYPE:
286
            *va_arg( args, char ** ) = strdup("video/MP2P");
287 288
            break;

289
        case STREAM_SET_TITLE:
290 291 292
        {
            int i = va_arg( args, int );
            if( i != p_sys->i_current_title )
293 294
            {
                /* Update info */
295
                p_sys->offset = 0;
296 297
                p_sys->i_current_title = i;
                p_sys->i_current_seekpoint = 0;
298 299 300 301 302

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

305
        case STREAM_SET_SEEKPOINT:
306
        {
307 308 309
            int i = va_arg( args, int );
            unsigned i_title = p_sys->i_current_title;

310
            if( p_sys->titles[i_title].count > 0 )
311
            {
312
                p_sys->i_current_seekpoint = i;
313

314
                p_sys->i_sector = p_sys->p_sectors[1 + i_title] +
315
                    p_sys->titles[i_title].seekpoints[i] / VCD_DATA_SIZE;
316

317 318
                p_sys->offset = (uint64_t)(p_sys->i_sector -
                    p_sys->p_sectors[1 + i_title]) * VCD_DATA_SIZE;
319
            }
320
            break;
321 322 323 324
        }

        default:
            return VLC_EGENERIC;
325
    }
326 327 328 329 330 331
    return VLC_SUCCESS;
}

/*****************************************************************************
 * Block:
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
332
static block_t *Block( stream_t *p_access, bool *restrict eof )
333 334
{
    access_sys_t *p_sys = p_access->p_sys;
335 336
    int i_blocks = VCD_BLOCKS_ONCE;
    block_t *p_block;
337

338
    /* Check end of title */
339
    while( p_sys->i_sector >= p_sys->p_sectors[p_sys->i_current_title + 2] )
340
    {
341
        if( p_sys->i_current_title + 2 >= p_sys->i_titles )
342
        {
343
            *eof = true;
344 345
            return NULL;
        }
346

347 348
        p_sys->i_current_title++;
        p_sys->i_current_seekpoint = 0;
349
        p_sys->offset = 0;
350 351 352 353
    }

    /* Don't read after the end of a title */
    if( p_sys->i_sector + i_blocks >=
354
        p_sys->p_sectors[p_sys->i_current_title + 2] )
355
    {
356
        i_blocks = p_sys->p_sectors[p_sys->i_current_title + 2 ] - p_sys->i_sector;
357 358
    }

359
    /* Do the actual reading */
360
    if( i_blocks < 0 || !( p_block = block_Alloc( i_blocks * VCD_DATA_SIZE ) ) )
361 362
    {
        msg_Err( p_access, "cannot get a new block of size: %i",
363
                 i_blocks * VCD_DATA_SIZE );
364 365
        return NULL;
    }
366

367 368
    if( ioctl_ReadSectors( VLC_OBJECT(p_access), p_sys->vcddev,
            p_sys->i_sector, p_block->p_buffer, i_blocks, VCD_TYPE ) < 0 )
369
    {
370
        msg_Err( p_access, "cannot read sector %i", p_sys->i_sector );
371
        block_Release( p_block );
372 373

        /* Try to skip one sector (in case of bad sectors) */
374
        p_sys->offset += VCD_DATA_SIZE;
375
        p_sys->i_sector++;
376
        return NULL;
377 378
    }

379
    /* Update seekpoints */
380
    for( int i_read = 0; i_read < i_blocks; i_read++ )
381
    {
382
        int i_title = p_sys->i_current_title;
383

384 385
        if( p_sys->titles[i_title].count > 0 &&
            p_sys->i_current_seekpoint + 1 < p_sys->titles[i_title].count &&
386
                (p_sys->offset + i_read * VCD_DATA_SIZE) >=
387
            p_sys->titles[i_title].seekpoints[p_sys->i_current_seekpoint + 1] )
388
        {
389
            msg_Dbg( p_access, "seekpoint change" );
390
            p_sys->i_current_seekpoint++;
391 392
        }
    }
Gildas Bazin's avatar
Gildas Bazin committed
393

394
    /* Update a few values */
395
    p_sys->offset += p_block->i_buffer;
396
    p_sys->i_sector += i_blocks;
397 398

    return p_block;
399 400 401
}

/*****************************************************************************
402
 * Seek:
403
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
404
static int Seek( stream_t *p_access, uint64_t i_pos )
405
{
406
    access_sys_t *p_sys = p_access->p_sys;
407 408
    int i_title = p_sys->i_current_title;
    unsigned i_seekpoint;
409 410

    /* Next sector to read */
411
    p_sys->offset = i_pos;
412
    p_sys->i_sector = i_pos / VCD_DATA_SIZE + p_sys->p_sectors[i_title + 1];
413 414

    /* Update current seekpoint */
415
    for( i_seekpoint = 0; i_seekpoint < p_sys->titles[i_title].count; i_seekpoint++ )
416
    {
417 418 419
        if( i_seekpoint + 1 >= p_sys->titles[i_title].count ) break;
        if( 0 < p_sys->titles[i_title].seekpoints[i_seekpoint + 1] &&
            i_pos < p_sys->titles[i_title].seekpoints[i_seekpoint + 1] ) break;
420 421
    }

422
    if( i_seekpoint != p_sys->i_current_seekpoint )
423 424
    {
        msg_Dbg( p_access, "seekpoint change" );
425
        p_sys->i_current_seekpoint = i_seekpoint;
426 427 428
    }

    return VLC_SUCCESS;
429
}
430

431
/*****************************************************************************
432
 * EntryPoints: Reads the information about the entry points on the disc.
433
 *****************************************************************************/
Rémi Denis-Courmont's avatar
Rémi Denis-Courmont committed
434
static int EntryPoints( stream_t *p_access )
435 436 437 438 439
{
    access_sys_t *p_sys = p_access->p_sys;
    uint8_t      sector[VCD_DATA_SIZE];

    entries_sect_t entries;
440
    int i_nb;
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464

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

465
    for( int i = 0; i < i_nb; i++ )
466 467 468 469 470 471
    {
        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  ) ));
472 473
        if( i_title < 0 ) continue;   /* Should not occur */
        if( i_title >= p_sys->i_titles ) continue;
474

475
        msg_Dbg( p_access, "Entry[%d] title=%d sector=%d",
476 477
                 i, i_title, i_sector );

478 479 480 481 482
        p_sys->titles[i_title].seekpoints = xrealloc(
            p_sys->titles[i_title].seekpoints,
            sizeof( uint64_t ) * (p_sys->titles[i_title].count + 1) );
        p_sys->titles[i_title].seekpoints[p_sys->titles[i_title].count++] =
            (i_sector - p_sys->p_sectors[i_title+1]) * VCD_DATA_SIZE;
483 484
    }

485
    return VLC_SUCCESS;
486
}
487