input_dvd.c 32 KB
Newer Older
1
/*****************************************************************************
2 3 4 5 6 7 8 9 10
 * input_dvd.c: DVD raw reading plugin.
 * ---
 * This plugins should handle all the known specificities of the DVD format,
 * especially the 2048 bytes logical block size.
 * It depends on:
 *  -input_netlist used to read packets
 *  -dvd_ifo for ifo parsing and analyse
 *  -dvd_css for unscrambling
 *  -dvd_udf to find files
11
 *****************************************************************************
Sam Hocevar's avatar
 
Sam Hocevar committed
12
 * Copyright (C) 1998-2001 VideoLAN
Sam Hocevar's avatar
 
Sam Hocevar committed
13
 * $Id: input_dvd.c,v 1.26 2001/03/03 11:01:07 sam Exp $
14
 *
Sam Hocevar's avatar
 
Sam Hocevar committed
15
 * Author: Stphane Borel <stef@via.ecp.fr>
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
 *
 * 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.
 * 
 * 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 "defs.h"

Sam Hocevar's avatar
 
Sam Hocevar committed
37 38 39 40 41 42 43
#ifdef HAVE_CSS
#define MODULE_NAME dvd-css
#else /* HAVE_CSS */
#define MODULE_NAME dvd-nocss
#endif /* HAVE_CSS */
#include "modules_inner.h"

Sam Hocevar's avatar
 
Sam Hocevar committed
44
#include <stdio.h>
45
#include <stdlib.h>
Sam Hocevar's avatar
 
Sam Hocevar committed
46 47 48 49 50
#include <unistd.h>
#include <netinet/in.h>

#include <fcntl.h>
#include <sys/types.h>
51
#include <sys/uio.h>
Sam Hocevar's avatar
 
Sam Hocevar committed
52

53 54
#include <string.h>
#include <errno.h>
Sam Hocevar's avatar
 
Sam Hocevar committed
55 56
#include <malloc.h>

57 58 59 60
#include "config.h"
#include "common.h"
#include "threads.h"
#include "mtime.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
61
#include "tests.h"
62 63 64

#include "intf_msg.h"

65 66
#include "main.h"

67 68 69 70 71
#include "stream_control.h"
#include "input_ext-intf.h"
#include "input_ext-dec.h"

#include "input.h"
72

73
#include "dvd_netlist.h"
Sam Hocevar's avatar
 
Sam Hocevar committed
74 75 76
#include "dvd_ifo.h"
#include "dvd_css.h"
#include "input_dvd.h"
77 78 79 80
#include "mpeg_system.h"

#include "debug.h"

Sam Hocevar's avatar
 
Sam Hocevar committed
81 82
#include "modules.h"

83 84 85 86 87
/*****************************************************************************
 * Local tables
 *****************************************************************************/
static struct
{
Sam Hocevar's avatar
 
Sam Hocevar committed
88
    char    p_code[3];
89
    char    p_lang_long[20];
Sam Hocevar's avatar
 
Sam Hocevar committed
90
}
91

Sam Hocevar's avatar
 
Sam Hocevar committed
92 93
lang_tbl[] =
{
94 95 96
    /* The ISO 639 language codes.
     * Language names with * prefix are not spelled in their own language 
     */
Sam Hocevar's avatar
 
Sam Hocevar committed
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 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
    { "  ", "Not Specified" },
    { "aa", "*Afar" },
    { "ab", "*Abkhazian" },
    { "af", "*Afrikaans" },
    { "am", "*Amharic" },
    { "ar", "*Arabic" },
    { "as", "*Assamese" },
    { "ay", "*Aymara" },
    { "az", "*Azerbaijani" },
    { "ba", "*Bashkir" },
    { "be", "*Byelorussian" },
    { "bg", "*Bulgarian" },
    { "bh", "*Bihari" },
    { "bi", "*Bislama" },
    { "bn", "*Bengali; Bangla" },
    { "bo", "*Tibetan" },
    { "br", "*Breton" },
    { "ca", "*Catalan" },
    { "co", "*Corsican" },
    { "cs", "*Czech(Ceske)" },
    { "cy", "*Welsh" },
    { "da", "Dansk" },
    { "de", "Deutsch" },
    { "dz", "*Bhutani" },
    { "el", "*Greek" },
    { "en", "English" },
    { "eo", "*Esperanto" },
    { "es", "Espanol" },
    { "et", "*Estonian" },
    { "eu", "*Basque" },
    { "fa", "*Persian" },
    { "fi", "Suomi" },
    { "fj", "*Fiji" },
    { "fo", "*Faroese" },
    { "fr", "Francais" },
    { "fy", "*Frisian" },
    { "ga", "*Irish" },
    { "gd", "*Scots Gaelic" },
    { "gl", "*Galician" },
    { "gn", "*Guarani" },
    { "gu", "*Gujarati" },
    { "ha", "*Hausa" },
    { "he", "*Hebrew" },                                      /* formerly iw */
    { "hi", "*Hindi" },
    { "hr", "Hrvatski" },                                        /* Croatian */
    { "hu", "Magyar" },
    { "hy", "*Armenian" },
    { "ia", "*Interlingua" },
    { "id", "*Indonesian" },                                  /* formerly in */
    { "ie", "*Interlingue" },
    { "ik", "*Inupiak" },
    { "in", "*Indonesian" },                               /* replaced by id */
    { "is", "Islenska" },
    { "it", "Italiano" },
    { "iu", "*Inuktitut" },
    { "iw", "*Hebrew" },                                   /* replaced by he */
    { "ja", "*Japanese" },
    { "ji", "*Yiddish" },                                  /* replaced by yi */
    { "jw", "*Javanese" },
    { "ka", "*Georgian" },
    { "kk", "*Kazakh" },
    { "kl", "*Greenlandic" },
    { "km", "*Cambodian" },
    { "kn", "*Kannada" },
    { "ko", "*Korean" },
    { "ks", "*Kashmiri" },
    { "ku", "*Kurdish" },
    { "ky", "*Kirghiz" },
    { "la", "*Latin" },
    { "ln", "*Lingala" },
    { "lo", "*Laothian" },
    { "lt", "*Lithuanian" },
    { "lv", "*Latvian, Lettish" },
    { "mg", "*Malagasy" },
    { "mi", "*Maori" },
    { "mk", "*Macedonian" },
    { "ml", "*Malayalam" },
    { "mn", "*Mongolian" },
    { "mo", "*Moldavian" },
    { "mr", "*Marathi" },
    { "ms", "*Malay" },
    { "mt", "*Maltese" },
    { "my", "*Burmese" },
    { "na", "*Nauru" },
    { "ne", "*Nepali" },
    { "nl", "Nederlands" },
    { "no", "Norsk" },
    { "oc", "*Occitan" },
    { "om", "*(Afan) Oromo" },
    { "or", "*Oriya" },
    { "pa", "*Punjabi" },
    { "pl", "*Polish" },
    { "ps", "*Pashto, Pushto" },
    { "pt", "Portugues" },
    { "qu", "*Quechua" },
    { "rm", "*Rhaeto-Romance" },
    { "rn", "*Kirundi" },
    { "ro", "*Romanian"  },
    { "ru", "*Russian" },
    { "rw", "*Kinyarwanda" },
    { "sa", "*Sanskrit" },
    { "sd", "*Sindhi" },
    { "sg", "*Sangho" },
    { "sh", "*Serbo-Croatian" },
    { "si", "*Sinhalese" },
    { "sk", "*Slovak" },
    { "sl", "*Slovenian" },
    { "sm", "*Samoan" },
    { "sn", "*Shona"  },
    { "so", "*Somali" },
    { "sq", "*Albanian" },
    { "sr", "*Serbian" },
    { "ss", "*Siswati" },
    { "st", "*Sesotho" },
    { "su", "*Sundanese" },
    { "sv", "Svenska" },
    { "sw", "*Swahili" },
    { "ta", "*Tamil" },
    { "te", "*Telugu" },
    { "tg", "*Tajik" },
    { "th", "*Thai" },
    { "ti", "*Tigrinya" },
    { "tk", "*Turkmen" },
    { "tl", "*Tagalog" },
    { "tn", "*Setswana" },
    { "to", "*Tonga" },
    { "tr", "*Turkish" },
    { "ts", "*Tsonga" },
    { "tt", "*Tatar" },
    { "tw", "*Twi" },
    { "ug", "*Uighur" },
    { "uk", "*Ukrainian" },
    { "ur", "*Urdu" },
    { "uz", "*Uzbek" },
    { "vi", "*Vietnamese" },
    { "vo", "*Volapuk" },
    { "wo", "*Wolof" },
    { "xh", "*Xhosa" },
    { "yi", "*Yiddish" },                                     /* formerly ji */
    { "yo", "*Yoruba" },
    { "za", "*Zhuang" },
    { "zh", "*Chinese" },
    { "zu", "*Zulu" },
    { "\0", "" }
};
242

243 244 245
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
246 247
static int  DVDProbe    ( probedata_t *p_data );
static int  DVDCheckCSS ( struct input_thread_s * );
248
static int  DVDRead     ( struct input_thread_s *, data_packet_t ** );
Sam Hocevar's avatar
 
Sam Hocevar committed
249 250
static void DVDInit     ( struct input_thread_s * );
static void DVDEnd      ( struct input_thread_s * );
251
static void DVDSeek     ( struct input_thread_s *, off_t );
Stéphane Borel's avatar
 
Stéphane Borel committed
252
static int  DVDSetArea  ( struct input_thread_s *, int, int, int, int );
Sam Hocevar's avatar
 
Sam Hocevar committed
253
static int  DVDRewind   ( struct input_thread_s * );
254

Sam Hocevar's avatar
 
Sam Hocevar committed
255 256 257 258
/*****************************************************************************
 * Functions exported as capabilities. They are declared as static so that
 * we don't pollute the namespace too much.
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
259
void _M( input_getfunctions )( function_list_t * p_function_list )
Sam Hocevar's avatar
 
Sam Hocevar committed
260 261 262 263
{
#define input p_function_list->functions.input
    p_function_list->pf_probe = DVDProbe;
    input.pf_init             = DVDInit;
264 265
    input.pf_open             = input_FileOpen;
    input.pf_close            = input_FileClose;
Sam Hocevar's avatar
 
Sam Hocevar committed
266 267
    input.pf_end              = DVDEnd;
    input.pf_read             = DVDRead;
Stéphane Borel's avatar
 
Stéphane Borel committed
268
    input.pf_set_area         = DVDSetArea;
Sam Hocevar's avatar
 
Sam Hocevar committed
269
    input.pf_demux            = input_DemuxPS;
270 271 272 273
    input.pf_new_packet       = DVDNewPacket;
    input.pf_new_pes          = DVDNewPES;
    input.pf_delete_packet    = DVDDeletePacket;
    input.pf_delete_pes       = DVDDeletePES;
274 275
    input.pf_rewind           = DVDRewind;
    input.pf_seek             = DVDSeek;
Sam Hocevar's avatar
 
Sam Hocevar committed
276 277 278
#undef input
}

279 280 281 282 283 284 285
/*
 * Local tools to decode some data in ifo
 */

/*****************************************************************************
 * Language: gives the long language name from the two-letters ISO-639 code
 *****************************************************************************/
286
static char * Language( u16 i_code )
287 288 289
{
    int     i = 0;

290 291
    while( memcmp( lang_tbl[i].p_code, &i_code, 2 ) &&
           lang_tbl[i].p_lang_long[0] )
292 293 294 295 296 297 298
    {
        i++;
    }

    return lang_tbl[i].p_lang_long;
}

299 300 301 302 303
/*
 * Data reading functions
 */

/*****************************************************************************
304
 * DVDProbe: verifies that the stream is a PS stream
305
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
306
static int DVDProbe( probedata_t *p_data )
307
{
Sam Hocevar's avatar
 
Sam Hocevar committed
308 309 310 311 312 313
    input_thread_t * p_input = (input_thread_t *)p_data;

    char * psz_name = p_input->p_source;
    int i_handle;
    int i_score = 5;

Sam Hocevar's avatar
 
Sam Hocevar committed
314 315
    if( TestMethod( INPUT_METHOD_VAR, "dvd" ) )
    {
Sam Hocevar's avatar
 
Sam Hocevar committed
316
#ifdef HAVE_CSS
Sam Hocevar's avatar
 
Sam Hocevar committed
317
        return( 999 );
Sam Hocevar's avatar
 
Sam Hocevar committed
318 319 320
#else /* HAVE_CSS */
        return( 998 );
#endif /* HAVE_CSS */
Sam Hocevar's avatar
 
Sam Hocevar committed
321 322
    }

Sam Hocevar's avatar
 
Sam Hocevar committed
323 324 325
    if( ( strlen(psz_name) > 4 ) && !strncasecmp( psz_name, "dvd:", 4 ) )
    {
        /* If the user specified "dvd:" then it's probably a DVD */
Sam Hocevar's avatar
 
Sam Hocevar committed
326
#ifdef HAVE_CSS
Sam Hocevar's avatar
 
Sam Hocevar committed
327
        i_score = 100;
Sam Hocevar's avatar
 
Sam Hocevar committed
328 329 330
#else /* HAVE_CSS */
        i_score = 90;
#endif /* HAVE_CSS */
Sam Hocevar's avatar
 
Sam Hocevar committed
331 332 333 334 335 336 337 338 339 340 341
        psz_name += 4;
    }

    i_handle = open( psz_name, 0 );
    if( i_handle == -1 )
    {
        return( 0 );
    }
    close( i_handle );

    return( i_score );
Sam Hocevar's avatar
 
Sam Hocevar committed
342 343 344 345 346 347 348
}

/*****************************************************************************
 * DVDCheckCSS: check the stream
 *****************************************************************************/
static int DVDCheckCSS( input_thread_t * p_input )
{
349
    return CSSTest( p_input->i_handle );
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
/*****************************************************************************
 * DVDCellToOff: synchronize navigation maps with current offset
 *****************************************************************************/
static int DVDCellToOff( thread_dvd_data_t * p_dvd )
{


    return 0;
}

/*****************************************************************************
 * DVDFindCell: from cell index in adress map from index in information table.
 *****************************************************************************/
static int DVDFindCell( thread_dvd_data_t * p_dvd, int i_index )
{
    pgc_t *              p_pgc;
    int                  i_cell;

    p_pgc = &p_dvd->ifo.vts.pgci_ti.p_srp[p_dvd->i_vts_title-1].pgc;
    i_cell = 0;

    while( ( p_pgc->p_cell_pos_inf[i_index].i_vob_id >
             p_dvd->ifo.vts.c_adt.p_cell_inf[i_cell].i_vob_id ) &&
             p_dvd->ifo.vts.c_adt.i_cell_nb > i_cell )
    {
        //if( p_dvd->ifo.vts.c_adt.p_cell_inf[i_cell+1].i_esector*DVD_LB_SIZE >
        //    p_dvd->i_start ) 
        {
            i_cell++;
        }
    }
    while( ( p_pgc->p_cell_pos_inf[i_index].i_cell_id >
             p_dvd->ifo.vts.c_adt.p_cell_inf[i_cell].i_cell_id ) &&
             p_dvd->ifo.vts.c_adt.i_cell_nb > i_cell )
    {
        //if( p_dvd->ifo.vts.c_adt.p_cell_inf[i_cell+1].i_esector*DVD_LB_SIZE >
        //    p_dvd->i_start ) 
        {
            i_cell++;
        }
    }

    return i_cell;
}

397
/*****************************************************************************
398
 * DVDChapterSelect: find the cell corresponding to requested chapter
399
 * When called to find chapter 1, also sets title size and end.
400
 *****************************************************************************/
401
static int DVDChapterSelect( thread_dvd_data_t * p_dvd, int i_chapter )
402
{
403 404 405 406
    pgc_t *              p_pgc;
    int                  i_start_cell;
    int                  i_end_cell;
    int                  i_index;
407
    int                  i_last_chapter;
Sam Hocevar's avatar
 
Sam Hocevar committed
408

409
    p_pgc = &p_dvd->ifo.vts.pgci_ti.p_srp[p_dvd->i_vts_title-1].pgc;
Sam Hocevar's avatar
 
Sam Hocevar committed
410

411 412
    /* Find cell index in Program chain for current chapter */
    i_index = p_pgc->prg_map.pi_entry_cell[i_chapter-1] - 1;
413

414
    /* Search for cell_index in cell adress_table */
415
    i_start_cell = DVDFindCell( p_dvd, i_index );
416

417
/*    intf_WarnMsg( 3, "DVD: Cell: %d vob id: %d cell id: %d", i_start_cell, p_dvd->ifo.vts.c_adt.p_cell_inf[i_start_cell].i_vob_id, p_dvd->ifo.vts.c_adt.p_cell_inf[i_start_cell].i_cell_id );*/
418

419
    /* start is : beginning of vts + offset to vobs + offset to vob x */
420 421 422
    p_dvd->i_start = p_dvd->ifo.vts.i_pos + DVD_LB_SIZE *
            (off_t)( p_dvd->ifo.vts.mat.i_tt_vobs_ssector +
                     p_dvd->ifo.vts.c_adt.p_cell_inf[i_start_cell].i_ssector );
Stéphane Borel's avatar
 
Stéphane Borel committed
423

424
    /* compute size of the stream */
425 426
    if( i_chapter == 1 )
    {
427 428 429 430 431 432 433 434 435 436
        i_last_chapter =
                p_dvd->ifo.vmg.ptt_srpt.p_tts[p_dvd->i_title-1].i_ptt_nb;
        i_index = p_pgc->prg_map.pi_entry_cell[i_last_chapter - 1] - 1;
                  
/*        intf_WarnMsg( 3, "last: %d index: %d", i_last_chapter, i_index );*/

        i_end_cell = DVDFindCell( p_dvd, i_index );

/*        intf_WarnMsg( 3, "DVD: Cell: %d vob id: %d cell id: %d", i_end_cell, p_dvd->ifo.vts.c_adt.p_cell_inf[i_end_cell].i_vob_id, p_dvd->ifo.vts.c_adt.p_cell_inf[i_end_cell].i_cell_id );*/

437 438 439 440 441 442
        p_dvd->i_size = (off_t)DVD_LB_SIZE *
            ( p_dvd->ifo.vts.c_adt.p_cell_inf[i_end_cell].i_esector -
              p_dvd->ifo.vts.c_adt.p_cell_inf[i_start_cell].i_ssector + 1 );
        intf_WarnMsg( 3, "DVD: Start cell: %d End Cell: %d",
                                            i_start_cell, i_end_cell );
    }
443

444 445
    return 0;
}
Stéphane Borel's avatar
 
Stéphane Borel committed
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
/*****************************************************************************
 * DVDSetArea: initialize input data for title x, chapter y.
 * It should be called for each user navigation request, and to change
 * audio or sub-picture streams.
 * ---
 * Take care that i_title starts from 0 (vmg) and i_chapter start from 1.
 * i_audio, i_spu start from 1 ; 0 means off.
 * A negative value for an argument means it does not change
 *****************************************************************************/
static int DVDSetArea( input_thread_t * p_input,
                       int i_title, int i_chapter,
                       int i_audio, int i_spu )
{
    thread_dvd_data_t *  p_method;
    es_descriptor_t *    p_es;
    int                  i_index;
    int                  i_nb;
    u16                  i_id;
    u8                   i_ac3;
    u8                   i_mpeg;
    u8                   i_sub_pic;
    u8                   i;
    boolean_t            b_last;
    
    p_method = (thread_dvd_data_t*)p_input->p_plugin_data;
Stéphane Borel's avatar
 
Stéphane Borel committed
472 473 474

    vlc_mutex_lock( &p_input->stream.stream_lock );

475
    if( i_title >= 0 )
Stéphane Borel's avatar
 
Stéphane Borel committed
476
    {
477

478 479 480 481 482 483 484
        /*
         *  We have to load all title information
         */

        /* Change the default area */
        p_input->stream.p_selected_area = p_input->stream.pp_areas[i_title];

485 486 487 488 489 490 491 492 493 494 495
        p_method->i_title = i_title;

        /* title position inside the selected vts */
        p_method->i_vts_title =
                p_method->ifo.vmg.ptt_srpt.p_tts[i_title-1].i_vts_ttn;

        /* vts number */
        p_method->ifo.i_title =
                p_method->ifo.vmg.ptt_srpt.p_tts[i_title-1].i_tts_nb;

        /* ifo vts */
496
        IfoReadVTS( &(p_method->ifo) );
497
        intf_WarnMsg( 2, "ifo info: vts initialized" );
498
    
499
        /* css title key for current vts */
500
        if( p_method->b_encrypted )
Stéphane Borel's avatar
 
Stéphane Borel committed
501
        {
502 503 504
            p_method->p_css->i_title =
                    p_method->ifo.vmg.ptt_srpt.p_tts[i_title-1].i_tts_nb;
            p_method->p_css->i_title_pos =
505 506
                    p_method->ifo.vts.i_pos +
                    p_method->ifo.vts.mat.i_tt_vobs_ssector * DVD_LB_SIZE;
507 508
            CSSGetKey( p_method->p_css );
            intf_WarnMsg( 2, "css info: vts key initialized" );
Stéphane Borel's avatar
 
Stéphane Borel committed
509
        }
510 511 512 513 514 515 516 517 518
    
        /*
         * Set selected title start and size
         */
        DVDChapterSelect( p_method, 1 );
    
        p_method->i_start =
                    lseek( p_input->i_handle, p_method->i_start, SEEK_SET );

519 520 521 522 523
        intf_WarnMsg( 2, "dvd info: title: %d", i_title );
        intf_WarnMsg( 2, "dvd info: vobstart at: %lld", p_method->i_start );
        intf_WarnMsg( 2, "dvd info: stream size: %lld", p_method->i_size );
        intf_WarnMsg( 2, "dvd info: number of chapters: %d",
                            p_input->stream.p_selected_area->i_part_nb );
524 525 526 527 528 529 530 531 532 533

        /* Area definition */
        p_input->stream.p_selected_area->i_start = p_method->i_start;
        p_input->stream.p_selected_area->i_size = p_method->i_size;

        /*
         * Destroy obsolete ES by reinitializing program 0
         * and find all ES in title with ifo data
         */
        if( p_input->stream.pp_programs != NULL )
Stéphane Borel's avatar
 
Stéphane Borel committed
534
        {
535
            input_DelProgram( p_input, p_input->stream.pp_programs[0] );
Stéphane Borel's avatar
 
Stéphane Borel committed
536 537
        }

538
        input_AddProgram( p_input, 0, sizeof( stream_ps_data_t ) );
Stéphane Borel's avatar
 
Stéphane Borel committed
539

540
        p_es = NULL;
541

542 543 544 545 546
        /* ES 0 -> video MPEG2 */
        p_es = input_AddES( p_input, p_input->stream.pp_programs[0], 0xe0, 0 );
        p_es->i_stream_id = 0xe0;
        p_es->i_type = MPEG2_VIDEO_ES;
        input_SelectES( p_input, p_es );
547
        intf_WarnMsg( 1, "dvd info: video MPEG2 stream" );
548 549 550 551 552 553
    
        /* Audio ES, in the order they appear in .ifo */
        i_nb = p_method->ifo.vts.mat.i_audio_nb;
    
        i_ac3 = 0x80;
        i_mpeg = 0xc0;
Stéphane Borel's avatar
 
Stéphane Borel committed
554

555 556
        for( i = 1 ; i <= i_nb ; i++ )
        {
557

558
#if 0
559 560
    fprintf( stderr, "Audio %d: %x %x %x %x %x %x %x\n", i,
            p_method->ifo.vts.mat.p_audio_atrt[i].i_num_channels,
561 562 563 564 565 566 567
            p_method->ifo.vts.mat.p_audio_atrt[i].i_coding_mode,
            p_method->ifo.vts.mat.p_audio_atrt[i].i_multichannel_extension,
            p_method->ifo.vts.mat.p_audio_atrt[i].i_type,
            p_method->ifo.vts.mat.p_audio_atrt[i].i_appl_mode,
            p_method->ifo.vts.mat.p_audio_atrt[i].i_foo,
            p_method->ifo.vts.mat.p_audio_atrt[i].i_bar );
#endif
568

569 570 571 572 573 574 575 576 577 578 579
            switch( p_method->ifo.vts.mat.p_audio_atrt[i].i_coding_mode )
            {
            case 0x00:              /* AC3 */
                i_id = ( ( 0x7f + i ) << 8 ) | 0xbd;
                p_es = input_AddES( p_input,
                                    p_input->stream.pp_programs[0], i_id, 0 );
                p_es->i_stream_id = 0xbd;
                p_es->i_type = AC3_AUDIO_ES;
                p_es->b_audio = 1;
                strcpy( p_es->psz_desc, Language( hton16(
                    p_method->ifo.vts.mat.p_audio_atrt[i-1].i_lang_code ) ) ); 
580

581 582 583 584 585 586 587 588 589 590 591
                break;
            case 0x02:
            case 0x03:              /* MPEG audio */
                i_id = 0xbf + i;
                p_es = input_AddES( p_input,
                                    p_input->stream.pp_programs[0], i_id, 0 );
                p_es->i_stream_id = i_id;
                p_es->i_type = MPEG2_AUDIO_ES;
                p_es->b_audio = 1;
                strcpy( p_es->psz_desc, Language( hton16(
                    p_method->ifo.vts.mat.p_audio_atrt[i-1].i_lang_code ) ) ); 
592

593
                break;
594 595
            case 0x04:              /* LPCM */
                i_id = 0;
596
                intf_ErrMsg( "dvd error: LPCM audio not handled yet" );
597 598 599
                break;
            case 0x06:              /* DTS */
                i_id = 0;
600
                intf_ErrMsg( "dvd error: DTS audio not handled yet" );
601
                break;
602 603
            default:
                i_id = 0;
604
                intf_ErrMsg( "dvd error: unkown audio" );
605
            }
606
        
607
            intf_WarnMsg( 1, "dvd info: audio stream %d %s\t(0x%x)",
608 609 610 611 612 613 614 615 616 617 618
                          i, p_es->psz_desc, i_id );
        }
    
        /* Sub Picture ES */
        i_nb = p_method->ifo.vts.mat.i_subpic_nb;
    
        b_last = 0;
        i_sub_pic = 0x20;
        for( i = 1 ; i <= i_nb ; i++ )
        {
            if( !b_last )
619
            {
620 621 622 623 624 625 626
                i_id = ( i_sub_pic++ << 8 ) | 0xbd;
                p_es = input_AddES( p_input,
                                    p_input->stream.pp_programs[0], i_id, 0 );
                p_es->i_stream_id = 0xbd;
                p_es->i_type = DVD_SPU_ES;
                strcpy( p_es->psz_desc, Language( hton16(
                    p_method->ifo.vts.mat.p_subpic_atrt[i-1].i_lang_code ) ) ); 
627
                intf_WarnMsg( 1, "dvd info: spu stream %d %s\t(0x%x)",
628 629 630 631 632
                              i, p_es->psz_desc, i_id );
    
                /* The before the last spu has a 0x0 prefix */
                b_last =
                    ( p_method->ifo.vts.mat.p_subpic_atrt[i].i_prefix == 0 ); 
633
            }
634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651
        }

    } // i_title >= 0

    /*
     * Select requested ES
     */
    if( ( i_audio >= 0 ) || ( i_title >= 0 ) )
    {

        /* Audio: we check it is in the range and
         * default it to the first if not */
        if( i_audio > p_method->ifo.vts.mat.i_audio_nb )
        {
            i_audio = 1;
        }

        p_es = p_input->stream.pp_programs[0]->pp_es[i_audio];
652

653 654 655 656 657 658
        /* We can only have one audio channel */
        /* Look for a preselected one */
        i_index = -1;
        for( i = 0 ; i < p_input->stream.i_selected_es_number ; i++ )
        {
            if( p_input->stream.pp_selected_es[i]->b_audio )
659
            {
660
                i_index = i;
661 662
                break;
            }
663
        }
664

665
        if( i_index != -1 )
666
        {
667 668 669
            
            if( p_input->stream.pp_selected_es[i_index] != p_es )
            {
670
                input_UnselectES( p_input,
671 672
                                  p_input->stream.pp_selected_es[i_index] );
                input_SelectES( p_input, p_es );
673
                intf_WarnMsg( 1, "dvd info: audio %d selected -> %s (0x%x)",
674 675
                              i_audio, p_es->psz_desc, p_es->i_id );
            }
676 677 678
        }
        else
        {
679
            input_SelectES( p_input, p_es );
680
            intf_WarnMsg( 1, "dvd info: audio %d selected -> %s (0x%x)",
681
                          i_audio, p_es->psz_desc, p_es->i_id );
682
        }
683
    }
684

685 686 687 688 689 690 691 692
    if( ( i_spu >= 0 ) || ( i_title >= 0 ) )
    {
        /* For spu: no one if none or a not existed one requested */
        if( ( i_spu <= p_method->ifo.vts.mat.i_subpic_nb ) && ( i_spu > 0 ) )
        {
            input_SelectES( p_input, ( p_es = p_input->stream.pp_programs[0]->
                pp_es[ i_spu + p_method->ifo.vts.mat.i_audio_nb ] ) );
    
693
            intf_WarnMsg( 1, "dvd info: spu   %d selected -> %s (0x%x)",
694 695
                          i_spu, p_es->psz_desc, p_es->i_id );
        }
696
    }
697 698 699 700 701 702 703

    /*
     * Chapter selection
     */

    if( ( i_chapter > 0 ) &&
        ( i_chapter <= p_input->stream.p_selected_area->i_part_nb ) )
704
    {
705
        DVDChapterSelect( p_method, i_chapter );
Sam Hocevar's avatar
 
Sam Hocevar committed
706

707
        p_input->stream.p_selected_area->i_part = i_chapter; 
Sam Hocevar's avatar
 
Sam Hocevar committed
708

709 710 711
        DVDSeek( p_input, p_method->i_start -
                          p_input->stream.p_selected_area->i_start );

712
        intf_WarnMsg( 2, "dvd info: chapter %d start at: %lld", i_chapter,
713
                                    p_input->stream.p_selected_area->i_tell );
Sam Hocevar's avatar
 
Sam Hocevar committed
714
    }
715 716 717 718 719

    /* No PSM to read in DVD mode, we already have all information */
    p_input->stream.pp_programs[0]->b_is_ok = 1;

    vlc_mutex_unlock( &p_input->stream.stream_lock );
720 721 722 723 724 725 726 727 728 729

    return 0;
}

/*****************************************************************************
 * DVDInit: initializes DVD structures
 *****************************************************************************/
static void DVDInit( input_thread_t * p_input )
{
    thread_dvd_data_t *  p_method;
730 731 732 733
    int                  i_title;
    int                  i_chapter;
    int                  i_audio;
    int                  i_spu;
Stéphane Borel's avatar
 
Stéphane Borel committed
734
    int                  i;
735 736 737 738 739 740 741 742 743 744 745 746

    if( (p_method = malloc( sizeof(thread_dvd_data_t) )) == NULL )
    {
        intf_ErrMsg( "Out of memory" );
        p_input->b_error = 1;
        return;
    }

    p_input->p_plugin_data = (void *)p_method;
    p_input->p_method_data = NULL;

    p_method->i_fd = p_input->i_handle;
747 748 749 750

    p_method->i_block_once = 32;
    p_input->i_read_once = 128;

751 752 753 754 755
    p_method->b_encrypted = DVDCheckCSS( p_input );

    lseek( p_input->i_handle, 0, SEEK_SET );

    /* Reading structures initialisation */
756 757 758
    p_input->p_method_data =
    DVDNetlistInit( 4096, 16384, 4096, DVD_LB_SIZE, p_method->i_block_once ); 
    intf_WarnMsg( 2, "dvd info: netlist initialized" );
759 760 761

    /* Ifo initialisation */
    p_method->ifo = IfoInit( p_input->i_handle );
762
    intf_WarnMsg( 2, "ifo info: vmg initialized" );
763 764 765 766

    /* CSS initialisation */
    if( p_method->b_encrypted )
    {
767
        p_method->p_css = CSSInit( p_input->i_handle );
768

769
        if( p_method->p_css == NULL )
770
        {
Sam Hocevar's avatar
 
Sam Hocevar committed
771
            intf_ErrMsg( "css error: fatal failure" );
772
            p_input->b_error = 1;
773 774
            return;
        }
775

Sam Hocevar's avatar
 
Sam Hocevar committed
776
        intf_WarnMsg( 2, "css info: initialized" );
777 778 779 780 781
    }

    /* Initialize ES structures */
    input_InitStream( p_input, sizeof( stream_ps_data_t ) );

Stéphane Borel's avatar
 
Stéphane Borel committed
782
    /* Set stream and area data */
783
    vlc_mutex_lock( &p_input->stream.stream_lock );
Stéphane Borel's avatar
 
Stéphane Borel committed
784

785 786
#define srpt p_method->ifo.vmg.ptt_srpt
    intf_WarnMsg( 2, "dvd info: number of titles: %d", srpt.i_ttu_nb );
787

Stéphane Borel's avatar
 
Stéphane Borel committed
788 789
#define area p_input->stream.pp_areas
    /* We start from 1 here since area 0 is reserved for video_ts.vob */
790
    for( i = 1 ; i <= srpt.i_ttu_nb ; i++ )
Stéphane Borel's avatar
 
Stéphane Borel committed
791 792 793 794 795 796 797 798 799 800 801 802 803 804
    {
        input_AddArea( p_input );

        /* Should not be so simple eventually :
         * are title Program Chains, or still something else ? */
        area[i]->i_id = i;

        /* Absolute start offset and size 
         * We can only set that with vts ifo, so we do it during the
         * first call to DVDSetArea */
        area[i]->i_start = 0;
        area[i]->i_size = 0;

        /* Number of chapter */
805
        area[i]->i_part_nb = srpt.p_tts[i-1].i_ptt_nb;
Stéphane Borel's avatar
 
Stéphane Borel committed
806
        area[i]->i_part = 1;
807

Stéphane Borel's avatar
 
Stéphane Borel committed
808 809
        /* Offset to vts_i_0.ifo */
        area[i]->i_plugin_data = p_method->ifo.i_off +
810
                               ( srpt.p_tts[i-1].i_ssector * DVD_LB_SIZE );
Stéphane Borel's avatar
 
Stéphane Borel committed
811 812 813
    }   
#undef area

814 815
    vlc_mutex_unlock( &p_input->stream.stream_lock );

816
    /* Get requested title - if none try to find one where is the movie */
817 818
    i_title = main_GetIntVariable( INPUT_TITLE_VAR, 1 );
    if( i_title <= 0 || i_title > srpt.i_ttu_nb )
819
    {
820
        i_title = 1;
821
    }
822
#undef srpt
823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841

    /* Get requested chapter - if none defaults to first one */
    i_chapter = main_GetIntVariable( INPUT_CHAPTER_VAR, 1 );
    if( i_chapter <= 0 )
    {
        i_chapter = 1;
    }

    /* For audio: first one if none or a not existing one specified */
    i_audio = main_GetIntVariable( INPUT_CHANNEL_VAR, 1 );
    if( i_audio <= 0 )
    {
        main_PutIntVariable( INPUT_CHANNEL_VAR, 1 );
        i_audio = 1;
    }

    i_spu = main_GetIntVariable( INPUT_SUBTITLE_VAR, 0 );

    DVDSetArea( p_input, i_title, i_chapter, i_audio, i_spu );
Sam Hocevar's avatar
 
Sam Hocevar committed
842

843
    return;
Sam Hocevar's avatar
 
Sam Hocevar committed
844 845 846 847 848 849
}

/*****************************************************************************
 * DVDEnd: frees unused data
 *****************************************************************************/
static void DVDEnd( input_thread_t * p_input )
850
{
851 852 853 854 855 856 857 858 859 860
    thread_dvd_data_t *     p_method;
    dvd_netlist_t *         p_netlist;

    p_method = (thread_dvd_data_t*)p_input->p_plugin_data;
    p_netlist = (dvd_netlist_t *)p_input->p_method_data;

    CSSEnd( p_method->p_css );
//    IfoEnd( p_method->p_ifo ) );
    free( p_method );
    DVDNetlistEnd( p_netlist );
861 862 863
}

/*****************************************************************************
864
 * DVDRead: reads data packets into the netlist.
865 866 867 868
 *****************************************************************************
 * Returns -1 in case of error, 0 if everything went well, and 1 in case of
 * EOF.
 *****************************************************************************/
Sam Hocevar's avatar
 
Sam Hocevar committed
869
static int DVDRead( input_thread_t * p_input,
870
                    data_packet_t ** pp_packets )
871
{
872
    thread_dvd_data_t *     p_method;
873
    dvd_netlist_t *         p_netlist;
874
    struct iovec *          p_vec;
875
    struct data_packet_s *  pp_data[p_input->i_read_once];
876 877
    u8 *                    pi_cur;
    int                     i_packet_size;
878
    int                     i_iovec;
879 880 881
    int                     i_packet;
    int                     i_pos;

882 883
    p_method = (thread_dvd_data_t *)p_input->p_plugin_data;
    p_netlist = (dvd_netlist_t *)p_input->p_method_data;
884 885

    /* Get an iovec pointer */
886
    if( ( p_vec = DVDGetiovec( p_netlist ) ) == NULL )
887 888 889 890
    {
        intf_ErrMsg( "DVD: read error" );
        return -1;
    }
891

892
    /* Reads from DVD */
893
    readv( p_input->i_handle, p_vec, p_method->i_block_once );
894

895 896 897 898 899 900
    /* Update netlist indexes */
    DVDMviovec( p_netlist, p_method->i_block_once, pp_data );
    i_packet = 0;

    /* Read headers to compute payload length */
    for( i_iovec = 0 ; i_iovec < p_method->i_block_once ; i_iovec++ )
901
    {
902
        if( p_method->b_encrypted )
903
        {
904
            CSSDescrambleSector( p_method->p_css->pi_title_key, 
905 906
                                 p_vec[i_iovec].iov_base );
            ((u8*)(p_vec[i_iovec].iov_base))[0x14] &= 0x8F;
907
        }
908

909
        i_pos = 0;
910

911 912
        while( i_pos < p_netlist->i_buffer_size )
        {
913 914
            pi_cur = (u8*)(p_vec[i_iovec].iov_base + i_pos);

915 916
            /*default header */
            if( U32_AT( pi_cur ) != 0x1BA )
917
            {
918 919
                /* That's the case for all packets, except pack header. */
                i_packet_size = U16_AT( pi_cur + 4 );
920
                pp_packets[i_packet] = DVDNewPtr( p_netlist );
921
            }
922
            else
923
            {
924 925 926 927 928 929 930
                /* Pack header. */
                if( ( pi_cur[4] & 0xC0 ) == 0x40 )
                {
                    /* MPEG-2 */
                    i_packet_size = 8;
                }
                else if( ( pi_cur[4] & 0xF0 ) == 0x20 )
931
                {
932 933
                    /* MPEG-1 */
                    i_packet_size = 6;
934 935 936
                }
                else
                {
937 938
                    intf_ErrMsg( "Unable to determine stream type" );
                    return( -1 );
939
                }
940 941 942

                pp_packets[i_packet] = pp_data[i_iovec];

943
            }
944

945 946 947 948 949 950 951 952 953 954 955 956
            (*pp_data[i_iovec]->pi_refcount)++;

            pp_packets[i_packet]->pi_refcount = pp_data[i_iovec]->pi_refcount;

            pp_packets[i_packet]->p_buffer = pp_data[i_iovec]->p_buffer;

            pp_packets[i_packet]->p_payload_start =
                    pp_packets[i_packet]->p_buffer + i_pos;

            pp_packets[i_packet]->p_payload_end =
                    pp_packets[i_packet]->p_payload_start + i_packet_size + 6;

957 958
            i_packet++;
            i_pos += i_packet_size + 6;
959 960
        }
    }
961

962 963 964 965
    pp_packets[i_packet] = NULL;

    vlc_mutex_lock( &p_input->stream.stream_lock );
    p_input->stream.p_selected_area->i_tell +=
966
                                        p_method->i_block_once *DVD_LB_SIZE;
967
    vlc_mutex_unlock( &p_input->stream.stream_lock );
968

969
    return( 0 );
970 971 972
}


Sam Hocevar's avatar
 
Sam Hocevar committed
973 974 975 976 977 978 979 980 981 982 983 984 985
/*****************************************************************************
 * DVDRewind : reads a stream backward
 *****************************************************************************/
static int DVDRewind( input_thread_t * p_input )
{
    return( -1 );
}

/*****************************************************************************
 * DVDSeek : Goes to a given position on the stream ; this one is used by the 
 * input and translate chronological position from input to logical postion
 * on the device
 *****************************************************************************/
986
static void DVDSeek( input_thread_t * p_input, off_t i_off )
Sam Hocevar's avatar
 
Sam Hocevar committed
987
{
988 989 990 991 992 993
    thread_dvd_data_t *     p_method;
    off_t                   i_pos;
    
    p_method = ( thread_dvd_data_t * )p_input->p_plugin_data;

    /* We have to take care of offset of beginning of title */
994
    i_pos = i_off + p_input->stream.p_selected_area->i_start;
995 996 997 998 999 1000

    /* With DVD, we have to be on a sector boundary */
    i_pos = i_pos & (~0x7ff);

    i_pos = lseek( p_input->i_handle, i_pos, SEEK_SET );

1001 1002
    p_input->stream.p_selected_area->i_tell = i_pos -
                                    p_input->stream.p_selected_area->i_start;
1003

1004
    return;
Sam Hocevar's avatar
 
Sam Hocevar committed
1005
}
1006 1007 1008