vcd.c 18.3 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
vlc_module_begin();
    set_description( _("VCD input") );
    set_capability( "access", 80 );
    set_callbacks( Open, Close );
44

45 46 47
    add_usage_hint( N_("[vcd:][device][@[title][,[chapter]]]") );
    add_shortcut( "svcd" );
vlc_module_end();
48

49 50 51
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
52 53 54 55 56

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

57
struct access_sys_t
Gildas Bazin's avatar
 
Gildas Bazin committed
58 59
{
    vcddev_t    *vcddev;                            /* vcd device descriptor */
60
    int         i_nb_tracks;                        /* Nb of tracks (titles) */
Gildas Bazin's avatar
 
Gildas Bazin committed
61 62 63
    int         i_track;                                    /* Current track */
    int         i_sector;                                  /* Current Sector */
    int *       p_sectors;                                  /* Track sectors */
64 65 66
    int         i_entries_nb;                      /* Number of entry points */
    int *       p_entries;                                   /* Entry points */
    vlc_bool_t  b_valid_ep;                       /* Valid entry points flag */
Gildas Bazin's avatar
 
Gildas Bazin committed
67
    vlc_bool_t  b_end_of_track;           /* If the end of track was reached */
68
};
Gildas Bazin's avatar
 
Gildas Bazin committed
69

70 71 72 73
static int  VCDRead         ( input_thread_t *, byte_t *, size_t );
static void VCDSeek         ( input_thread_t *, off_t );
static int  VCDSetArea      ( input_thread_t *, input_area_t * );
static int  VCDSetProgram   ( input_thread_t *, pgrm_descriptor_t * );
74
static int  VCDEntryPoints  ( input_thread_t * );
75 76 77 78

/*****************************************************************************
 * VCDOpen: open vcd
 *****************************************************************************/
79
static int Open( vlc_object_t *p_this )
80
{
81 82 83 84 85
    input_thread_t *p_input = (input_thread_t *)p_this;
    access_sys_t   *p_sys;
    char *psz_dup = strdup( p_input->psz_name );
    char *psz;

86 87
    int                     i;
    input_area_t *          p_area;
88 89
    int                     i_title = 0;
    int                     i_chapter = 0;
Gildas Bazin's avatar
 
Gildas Bazin committed
90
    vcddev_t                *vcddev;
91

92 93
    /* Command line: vcd://[dev_path][@title[,chapter]] */
    if( ( psz = strchr( psz_dup, '@' ) ) )
94
    {
95
        *psz++ = '\0';
96

97 98
        i_title = strtol( psz, &psz, 0 );
        if( *psz )
99
        {
100
            i_chapter = strtol( psz, &psz, 0 );
101 102 103
        }
    }

104
    if( *psz_dup == '\0' )
105
    {
106 107 108 109 110 111 112 113
        free( psz_dup );

        /* Only when selected */
        if( *p_input->psz_access == '\0' )
            return VLC_EGENERIC;

        psz_dup = var_CreateGetString( p_input, "vcd" );
        if( *psz_dup == '\0' )
114
        {
115 116
            free( psz_dup );
            return VLC_EGENERIC;
117 118 119
        }
    }

Gildas Bazin's avatar
 
Gildas Bazin committed
120
    /* Open VCD */
121
    if( !(vcddev = ioctl_Open( p_this, psz_dup )) )
Gildas Bazin's avatar
 
Gildas Bazin committed
122
    {
123 124 125
        msg_Warn( p_input, "could not open %s", psz_dup );
        free( psz_dup );
        return VLC_EGENERIC;
Gildas Bazin's avatar
 
Gildas Bazin committed
126
    }
127

128 129
    p_sys = malloc( sizeof(access_sys_t) );
    if( p_sys == NULL )
130 131
    {
        msg_Err( p_input, "out of memory" );
132 133
        free( psz_dup );
        return VLC_EGENERIC;
134
    }
135
    free( psz_dup );
136

137 138
    p_sys->vcddev = vcddev;
    p_input->p_access_data = (void *)p_sys;
139

140
    p_input->i_mtu = VCD_DATA_ONCE;
141

142 143 144 145 146 147 148 149
    vlc_mutex_lock( &p_input->stream.stream_lock );
    p_input->stream.b_pace_control = 1;
    p_input->stream.b_seekable = 1;
    p_input->stream.p_selected_area->i_size = 0;
    p_input->stream.p_selected_area->i_tell = 0;
    vlc_mutex_unlock( &p_input->stream.stream_lock );

    /* We read the Table Of Content information */
150 151 152
    p_sys->i_nb_tracks = ioctl_GetTracksMap( VLC_OBJECT(p_input),
                                           p_sys->vcddev, &p_sys->p_sectors );
    if( p_sys->i_nb_tracks < 0 )
153
        msg_Err( p_input, "unable to count tracks" );
154
    else if( p_sys->i_nb_tracks <= 1 )
155
        msg_Err( p_input, "no movie tracks found" );
156
    if( p_sys->i_nb_tracks <= 1)
157
    {
158 159
        ioctl_Close( p_this, p_sys->vcddev );
        free( p_sys );
160 161 162
        return -1;
    }

163
    /* Allocate the entry points table */
164
    p_sys->p_entries = malloc( p_sys->i_nb_tracks * sizeof( int ) );
165

166
    if( p_sys->p_entries == NULL )
167 168
    {
        msg_Err( p_input, "not enough memory" );
169 170
        ioctl_Close( p_this, p_sys->vcddev );
        free( p_sys );
171 172
    }

173 174 175 176
    /* Set stream and area data */
    vlc_mutex_lock( &p_input->stream.stream_lock );

    /* Initialize ES structures */
177
    input_InitStream( p_input, 0 );
178 179 180 181

    /* disc input method */
    p_input->stream.i_method = INPUT_METHOD_VCD;

182 183
    p_input->stream.i_area_nb = 1;

184
#define area p_input->stream.pp_areas
185
    for( i = 1 ; i < p_sys->i_nb_tracks; i++ )
186 187
    {
        /* Titles are Program Chains */
Gildas Bazin's avatar
 
Gildas Bazin committed
188
        input_AddArea( p_input, i, 1 );
189 190

        /* Absolute start offset and size */
191 192
        area[i]->i_start = (off_t)p_sys->p_sectors[i] * (off_t)VCD_DATA_SIZE;
        area[i]->i_size = (off_t)(p_sys->p_sectors[i+1] - p_sys->p_sectors[i])
193 194
                           * (off_t)VCD_DATA_SIZE;

Gildas Bazin's avatar
 
Gildas Bazin committed
195
        /* Default Chapter */
196 197
        area[i]->i_part = 1;

198 199 200
        /* i_plugin_data is used to store which entry point is the first
         * of the track (area) */
        area[i]->i_plugin_data = 0;
201 202 203
    }
#undef area

204
    p_area = p_input->stream.pp_areas[__MIN(i_title,p_sys->i_nb_tracks -1)];
205

206
    p_sys->b_valid_ep = 1;
207 208
    if( VCDEntryPoints( p_input ) < 0 )
    {
209
        msg_Warn( p_input, "could not read entry points, will not use them" );
210
        p_sys->b_valid_ep = 0;
211 212
    }

213 214 215 216
    VCDSetArea( p_input, p_area );

    vlc_mutex_unlock( &p_input->stream.stream_lock );

217 218 219 220
    if( !p_input->psz_demux || !*p_input->psz_demux )
    {
        p_input->psz_demux = "ps";
    }
221

Gildas Bazin's avatar
 
Gildas Bazin committed
222 223 224 225 226
    p_input->pf_read = VCDRead;
    p_input->pf_seek = VCDSeek;
    p_input->pf_set_area = VCDSetArea;
    p_input->pf_set_program = VCDSetProgram;

227 228 229 230 231 232
    return 0;
}

/*****************************************************************************
 * VCDClose: closes vcd
 *****************************************************************************/
233
static void Close( vlc_object_t *p_this )
234 235
{
    input_thread_t *   p_input = (input_thread_t *)p_this;
236
    access_sys_t *p_sys = p_input->p_access_data;
237

238 239
    ioctl_Close( p_this, p_sys->vcddev );
    free( p_sys );
240 241 242 243 244 245 246 247
}

/*****************************************************************************
 * VCDRead: reads from the VCD into PES packets.
 *****************************************************************************
 * Returns -1 in case of error, 0 in case of EOF, otherwise the number of
 * bytes.
 *****************************************************************************/
248
static int VCDRead( input_thread_t * p_input, byte_t * p_buffer,
249 250
                     size_t i_len )
{
251
    access_sys_t *p_sys;
252 253 254 255 256
    int                     i_blocks;
    int                     i_index;
    int                     i_read;
    byte_t                  p_last_sector[ VCD_DATA_SIZE ];

257
    p_sys = p_input->p_access_data;
258 259 260 261 262 263 264

    i_read = 0;

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

    i_blocks = i_len / VCD_DATA_SIZE;

265
    for ( i_index = 0 ; i_index < i_blocks ; i_index++ )
266
    {
267 268
        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
269
             VCD_TYPE ) < 0 )
270
        {
271
            msg_Err( p_input, "could not read sector %d", p_sys->i_sector );
272 273 274
            return -1;
        }

275 276
        p_sys->i_sector ++;
        if ( p_sys->i_sector == p_sys->p_sectors[p_sys->i_track + 1] )
277 278
        {
            input_area_t *p_area;
279

280
            if ( p_sys->i_track >= p_sys->i_nb_tracks - 1 )
281
                return 0; /* EOF */
282 283

            vlc_mutex_lock( &p_input->stream.stream_lock );
284 285
            p_area = p_input->stream.pp_areas[
                    p_input->stream.p_selected_area->i_id + 1 ];
286

287
            msg_Dbg( p_input, "new title" );
288

289 290
            p_area->i_part = 1;
            VCDSetArea( p_input, p_area );
291
            vlc_mutex_unlock( &p_input->stream.stream_lock );
292
        }
293 294

        /* Update chapter */
295
        else if( p_sys->b_valid_ep &&
296 297 298 299 300 301 302 303 304 305 306
                /* 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;
307 308
            if( i_entry + 1 < p_sys->i_entries_nb &&
                    p_sys->i_sector >= p_sys->p_entries[i_entry + 1] )
309
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
310 311
                vlc_value_t val;

312 313
                msg_Dbg( p_input, "new chapter" );
                p_input->stream.p_selected_area->i_part ++;
Gildas Bazin's avatar
 
Gildas Bazin committed
314 315 316 317 318

                /* 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 );
319 320 321 322
            }
            vlc_mutex_unlock( &p_input->stream.stream_lock );
        }

323 324
        i_read += VCD_DATA_SIZE;
    }
325

326
    if ( i_len % VCD_DATA_SIZE ) /* this should not happen */
327
    {
328 329
        if ( ioctl_ReadSectors( VLC_OBJECT(p_input), p_sys->vcddev,
             p_sys->i_sector, p_last_sector, 1, VCD_TYPE ) < 0 )
330
        {
331
            msg_Err( p_input, "could not read sector %d", p_sys->i_sector );
332 333
            return -1;
        }
334

335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357
        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 )
{
358
    access_sys_t *p_sys = p_input->p_access_data;
Gildas Bazin's avatar
 
Gildas Bazin committed
359
    vlc_value_t val;
360 361 362 363 364 365

    /* 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
366 367
        unsigned int i;

368 369 370 371 372 373 374 375 376
        /* 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  */
377 378
        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
379 380 381

        /* Update the navigation variables without triggering a callback */
        val.i_int = p_area->i_id;
Gildas Bazin's avatar
 
Gildas Bazin committed
382 383
        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
384 385 386
        for( i = 1; i <= p_area->i_part_nb; i++ )
        {
            val.i_int = i;
Gildas Bazin's avatar
 
Gildas Bazin committed
387
            var_Change( p_input, "chapter", VLC_VAR_ADDCHOICE, &val, NULL );
Gildas Bazin's avatar
 
Gildas Bazin committed
388
        }
389 390
    }

391
    if( p_sys->b_valid_ep )
392 393 394 395
    {
        int i_entry = p_area->i_plugin_data /* 1st entry point of
                                               the track (area)*/
                            + p_area->i_part - 1;
396
        p_sys->i_sector = p_sys->p_entries[i_entry];
397 398
    }
    else
399
        p_sys->i_sector = p_sys->p_sectors[p_sys->i_track];
400 401

    p_input->stream.p_selected_area->i_tell =
402
        (off_t)p_sys->i_sector * (off_t)VCD_DATA_SIZE
403 404
         - p_input->stream.p_selected_area->i_start;

405 406 407 408
    /* 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
409 410
    /* Update the navigation variables without triggering a callback */
    val.i_int = p_area->i_part;
Gildas Bazin's avatar
 
Gildas Bazin committed
411
    var_Change( p_input, "chapter", VLC_VAR_SETVALUE, &val, NULL );
Gildas Bazin's avatar
 
Gildas Bazin committed
412

413 414 415 416 417 418 419 420
    return 0;
}

/****************************************************************************
 * VCDSeek
 ****************************************************************************/
static void VCDSeek( input_thread_t * p_input, off_t i_off )
{
421
    access_sys_t * p_sys = p_input->p_access_data;
422
    unsigned int i_index;
423

424
    p_sys->i_sector = p_sys->p_sectors[p_sys->i_track]
425 426
                       + i_off / (off_t)VCD_DATA_SIZE;

427 428 429
    vlc_mutex_lock( &p_input->stream.stream_lock );
#define p_area p_input->stream.p_selected_area
    /* Find chapter */
430
    if( p_sys->b_valid_ep )
431
    {
Gildas Bazin's avatar
 
Gildas Bazin committed
432
        for( i_index = 2 ; i_index <= p_area->i_part_nb; i_index ++ )
433
        {
434
            if( p_sys->i_sector < p_sys->p_entries[p_area->i_plugin_data
Gildas Bazin's avatar
 
Gildas Bazin committed
435
                + i_index - 1] )
436
            {
Gildas Bazin's avatar
 
Gildas Bazin committed
437 438 439 440 441 442 443 444
                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 );
445 446 447 448 449 450 451
                break;
            }
        }
    }
#undef p_area

    p_input->stream.p_selected_area->i_tell =
452
        (off_t)p_sys->i_sector * (off_t)VCD_DATA_SIZE
453
         - p_input->stream.p_selected_area->i_start;
454 455 456 457 458 459 460 461
    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 )
{
462
    access_sys_t * p_sys = p_input->p_access_data;
463 464 465 466 467 468 469 470 471 472 473 474 475
    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;
    }

476
    if( ioctl_ReadSectors( VLC_OBJECT(p_input), p_sys->vcddev,
Gildas Bazin's avatar
 
Gildas Bazin committed
477
        VCD_ENTRIES_SECTOR, p_sector, 1, VCD_TYPE ) < 0 )
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492
    {
        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 );
    }

493 494
    p_sys->p_entries = malloc( sizeof( int ) * i_nb );
    if( p_sys->p_entries == NULL )
495 496 497 498 499 500 501 502 503
    {
        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" );
504
        free( p_sys->p_entries );
505 506 507
        return -1;
    }

508
    p_sys->i_entries_nb = 0;
509

510
#define i_track BCD_TO_BIN(entries.entry[i].i_track)
Gildas Bazin's avatar
 
Gildas Bazin committed
511 512 513 514 515 516 517 518 519
    /* 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;
        }
    }

520 521 522 523
    for( i = 0 ; i < i_nb ; i++ )
    {
        if( i_track <= p_input->stream.i_area_nb )
        {
524
            p_sys->p_entries[i_entry_index] =
525 526 527 528 529 530 531 532 533 534 535 536
                (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
537
            msg_Dbg( p_input, "entry point %i begins at LBA: %i",
538
                     i_entry_index, p_sys->p_entries[i_entry_index] );
Gildas Bazin's avatar
 
Gildas Bazin committed
539

540
            i_entry_index ++;
541
            p_sys->i_entries_nb ++;
542 543 544 545 546 547
        }
        else
            msg_Warn( p_input, "wrong track number found in entry points" );
    }
#undef i_track
    return 0;
548
}