OSDN Git Service

Merge the 5.1 branch into the trunk.
[handbrake-jp/handbrake-jp-git.git] / libhb / dvd.c
1 /* $Id: dvd.c,v 1.12 2005/11/25 15:05:25 titer Exp $
2
3    This file is part of the HandBrake source code.
4    Homepage: <http://handbrake.m0k.org/>.
5    It may be used under the terms of the GNU General Public License. */
6
7 #include "hb.h"
8 #include "lang.h"
9
10 #include "dvdread/ifo_read.h"
11 #include "dvdread/nav_read.h"
12
13 struct hb_dvd_s
14 {
15     char         * path;
16
17     dvd_reader_t * reader;
18     ifo_handle_t * vmg;
19
20     int            vts;
21     int            ttn;
22     ifo_handle_t * ifo;
23     dvd_file_t   * file;
24
25     pgc_t        * pgc;
26     int            cell_start;
27     int            cell_end;
28     int            title_start;
29     int            title_end;
30     int            title_block_count;
31     int            cell_cur;
32     int            cell_next;
33     int            block;
34     int            pack_len;
35     int            next_vobu;
36 };
37
38 /***********************************************************************
39  * Local prototypes
40  **********************************************************************/
41 static void FindNextCell( hb_dvd_t * );
42 static int  dvdtime2msec( dvd_time_t * );
43
44 char * hb_dvd_name( char * path )
45 {
46     static char name[1024];
47     unsigned char unused[1024];
48     dvd_reader_t * reader;
49
50     reader = DVDOpen( path );
51     if( !reader )
52     {
53         return NULL;
54     }
55
56     if( DVDUDFVolumeInfo( reader, name, sizeof( name ),
57                           unused, sizeof( unused ) ) )
58     {
59         DVDClose( reader );
60         return NULL;
61     }
62
63     DVDClose( reader );
64     return name;
65 }
66
67 /***********************************************************************
68  * hb_dvd_init
69  ***********************************************************************
70  *
71  **********************************************************************/
72 hb_dvd_t * hb_dvd_init( char * path )
73 {
74     hb_dvd_t * d;
75
76     d = calloc( sizeof( hb_dvd_t ), 1 );
77
78     /* Open device */
79     if( !( d->reader = DVDOpen( path ) ) )
80     {
81         hb_log( "dvd: DVDOpen failed (%s)", path );
82         goto fail;
83     }
84
85     /* Open main IFO */
86     if( !( d->vmg = ifoOpen( d->reader, 0 ) ) )
87     {
88         hb_log( "dvd: ifoOpen failed" );
89         goto fail;
90     }
91
92     d->path = strdup( path );
93
94     return d;
95
96 fail:
97     if( d->vmg )    ifoClose( d->vmg );
98     if( d->reader ) DVDClose( d->reader );
99     free( d );
100     return NULL;
101 }
102
103 /***********************************************************************
104  * hb_dvd_title_count
105  **********************************************************************/
106 int hb_dvd_title_count( hb_dvd_t * d )
107 {
108     return d->vmg->tt_srpt->nr_of_srpts;
109 }
110
111 /***********************************************************************
112  * hb_dvd_title_scan
113  **********************************************************************/
114 hb_title_t * hb_dvd_title_scan( hb_dvd_t * d, int t )
115 {
116
117     hb_title_t   * title;
118     ifo_handle_t * vts = NULL;
119     int            pgc_id, pgn, i;
120     hb_chapter_t * chapter, * chapter_old;
121     int            c;
122     uint64_t       duration;
123     float          duration_correction;
124     unsigned char  unused[1024];
125
126     hb_log( "scan: scanning title %d", t );
127
128     title = hb_title_init( d->path, t );
129     if( DVDUDFVolumeInfo( d->reader, title->name, sizeof( title->name ),
130                           unused, sizeof( unused ) ) )
131     {
132         char * p_cur, * p_last = d->path;
133         for( p_cur = d->path; *p_cur; p_cur++ )
134         {
135             if( p_cur[0] == '/' && p_cur[1] )
136             {
137                 p_last = &p_cur[1];
138             }
139         }
140         snprintf( title->name, sizeof( title->name ), "%s", p_last );
141     }
142
143     /* VTS which our title is in */
144     title->vts = d->vmg->tt_srpt->title[t-1].title_set_nr;
145
146     hb_log( "scan: opening IFO for VTS %d", title->vts );
147     if( !( vts = ifoOpen( d->reader, title->vts ) ) )
148     {
149         hb_log( "scan: ifoOpen failed" );
150         goto fail;
151     }
152
153     /* Position of the title in the VTS */
154     title->ttn = d->vmg->tt_srpt->title[t-1].vts_ttn;
155
156     /* Get pgc */
157     pgc_id = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgcn;
158     pgn    = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgn;
159     d->pgc = vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
160
161     /* Start cell */
162     title->cell_start  = d->pgc->program_map[pgn-1] - 1;
163     title->block_start = d->pgc->cell_playback[title->cell_start].first_sector;
164
165     /* End cell */
166     title->cell_end  = d->pgc->nr_of_cells - 1;
167     title->block_end = d->pgc->cell_playback[title->cell_end].last_sector;
168
169     /* Block count */
170     title->block_count = 0;
171     d->cell_cur = title->cell_start;
172     while( d->cell_cur <= title->cell_end )
173     {
174 #define cp d->pgc->cell_playback[d->cell_cur]
175         title->block_count += cp.last_sector + 1 - cp.first_sector;
176 #undef cp
177         FindNextCell( d );
178         d->cell_cur = d->cell_next;
179     }
180
181     hb_log( "scan: vts=%d, ttn=%d, cells=%d->%d, blocks=%d->%d, "
182             "%d blocks", title->vts, title->ttn, title->cell_start,
183             title->cell_end, title->block_start, title->block_end,
184             title->block_count );
185
186     if( title->block_count < 2048  )
187     {
188         hb_log( "scan: title too short (%d blocks), ignoring",
189                 title->block_count );
190         goto fail;
191     }
192
193
194     /* Get duration */
195     title->duration = 90LL * dvdtime2msec( &d->pgc->playback_time );
196     title->hours    = title->duration / 90000 / 3600;
197     title->minutes  = ( ( title->duration / 90000 ) % 3600 ) / 60;
198     title->seconds  = ( title->duration / 90000 ) % 60;
199     hb_log( "scan: duration is %02d:%02d:%02d (%lld ms)",
200             title->hours, title->minutes, title->seconds,
201             title->duration / 90 );
202
203     /* Discard titles under 10 seconds */
204     if( !( title->hours | title->minutes ) && title->seconds < 10 )
205     {
206         hb_log( "scan: ignoring title (too short)" );
207         goto fail;
208     }
209
210     /* Detect languages */
211     for( i = 0; i < vts->vtsi_mat->nr_of_vts_audio_streams; i++ )
212     {
213         hb_audio_t * audio, * audio_tmp;
214         int          audio_format, lang_code, audio_control,
215                      position, j;
216
217         hb_log( "scan: checking audio %d", i + 1 );
218
219         audio = calloc( sizeof( hb_audio_t ), 1 );
220
221         audio_format  = vts->vtsi_mat->vts_audio_attr[i].audio_format;
222         lang_code     = vts->vtsi_mat->vts_audio_attr[i].lang_code;
223         audio_control =
224             vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->audio_control[i];
225
226         if( !( audio_control & 0x8000 ) )
227         {
228             hb_log( "scan: audio channel is not active" );
229             free( audio );
230             continue;
231         }
232
233         position = ( audio_control & 0x7F00 ) >> 8;
234
235         switch( audio_format )
236         {
237             case 0x00:
238                 audio->id    = ( ( 0x80 + position ) << 8 ) | 0xbd;
239                 audio->codec = HB_ACODEC_AC3;
240                 break;
241
242             case 0x02:
243             case 0x03:
244                 audio->id    = 0xc0 + position;
245                 audio->codec = HB_ACODEC_MPGA;
246                 break;
247
248             case 0x04:
249                 audio->id    = ( ( 0xa0 + position ) << 8 ) | 0xbd;
250                 audio->codec = HB_ACODEC_LPCM;
251                 break;
252
253             default:
254                 audio->id    = 0;
255                 audio->codec = 0;
256                 hb_log( "scan: unknown audio codec (%x)",
257                         audio_format );
258                 break;
259         }
260         if( !audio->id )
261         {
262             continue;
263         }
264
265         /* Check for duplicate tracks */
266         audio_tmp = NULL;
267         for( j = 0; j < hb_list_count( title->list_audio ); j++ )
268         {
269             audio_tmp = hb_list_item( title->list_audio, j );
270             if( audio->id == audio_tmp->id )
271             {
272                 break;
273             }
274             audio_tmp = NULL;
275         }
276         if( audio_tmp )
277         {
278             hb_log( "scan: duplicate audio track" );
279             free( audio );
280             continue;
281         }
282
283         snprintf( audio->lang, sizeof( audio->lang ), "%s (%s)",
284             lang_for_code( vts->vtsi_mat->vts_audio_attr[i].lang_code ),
285             audio->codec == HB_ACODEC_AC3 ? "AC3" : ( audio->codec ==
286                 HB_ACODEC_MPGA ? "MPEG" : "LPCM" ) );
287         snprintf( audio->lang_simple, sizeof( audio->lang_simple ), "%s",
288                   lang_for_code( vts->vtsi_mat->vts_audio_attr[i].lang_code ) );
289
290         hb_log( "scan: id=%x, lang=%s", audio->id,
291                 audio->lang );
292
293         hb_list_add( title->list_audio, audio );
294     }
295
296     if( !hb_list_count( title->list_audio ) )
297     {
298         hb_log( "scan: ignoring title (no audio track)" );
299         goto fail;
300     }
301
302     memcpy( title->palette,
303             vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->palette,
304             16 * sizeof( uint32_t ) );
305
306     /* Check for subtitles */
307     for( i = 0; i < vts->vtsi_mat->nr_of_vts_subp_streams; i++ )
308     {
309         hb_subtitle_t * subtitle;
310         int spu_control;
311         int position;
312
313         hb_log( "scan: checking subtitle %d", i + 1 );
314
315         spu_control =
316             vts->vts_pgcit->pgci_srp[pgc_id-1].pgc->subp_control[i];
317
318         if( !( spu_control & 0x80000000 ) )
319         {
320             hb_log( "scan: subtitle channel is not active" );
321             continue;
322         }
323
324         if( vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
325         {
326             switch( vts->vtsi_mat->vts_video_attr.permitted_df )
327             {
328                 case 1:
329                     position = spu_control & 0xFF;
330                     break;
331                 case 2:
332                     position = ( spu_control >> 8 ) & 0xFF;
333                     break;
334                 default:
335                     position = ( spu_control >> 16 ) & 0xFF;
336             }
337         }
338         else
339         {
340             position = ( spu_control >> 24 ) & 0x7F;
341         }
342
343         subtitle = calloc( sizeof( hb_subtitle_t ), 1 );
344         subtitle->id = ( ( 0x20 + position ) << 8 ) | 0xbd;
345         snprintf( subtitle->lang, sizeof( subtitle->lang ), "%s",
346             lang_for_code( vts->vtsi_mat->vts_subp_attr[i].lang_code ) );
347
348         hb_log( "scan: id=%x, lang=%s", subtitle->id,
349                 subtitle->lang );
350
351         hb_list_add( title->list_subtitle, subtitle );
352     }
353
354     /* Chapters */
355     hb_log( "scan: title %d has %d chapters", t,
356             vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts );
357     for( i = 0, c = 1;
358          i < vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts; i++ )
359     {
360         int pgc_id_next, pgn_next;
361         pgc_t * pgc_next;
362
363         chapter = calloc( sizeof( hb_chapter_t ), 1 );
364         chapter->index = c;
365
366         pgc_id = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgcn;
367         pgn    = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i].pgn;
368         d->pgc = vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
369
370         /* Start cell */
371         chapter->cell_start  = d->pgc->program_map[pgn-1] - 1;
372         chapter->block_start =
373             d->pgc->cell_playback[chapter->cell_start].first_sector;
374
375         /* End cell */
376         if( i != vts->vts_ptt_srpt->title[title->ttn-1].nr_of_ptts - 1 )
377         {
378             /* The cell before the starting cell of the next chapter,
379                or... */
380             pgc_id_next = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i+1].pgcn;
381             pgn_next    = vts->vts_ptt_srpt->title[title->ttn-1].ptt[i+1].pgn;
382             pgc_next    = vts->vts_pgcit->pgci_srp[pgc_id_next-1].pgc;
383             chapter->cell_end = pgc_next->program_map[pgn_next-1] - 2;
384             if( chapter->cell_end < 0 )
385             {
386                 /* Huh? */
387                 free( chapter );
388                 continue;
389             }
390         }
391         else
392         {
393             /* ... the last cell of the title */
394             chapter->cell_end = title->cell_end;
395         }
396         chapter->block_end =
397             d->pgc->cell_playback[chapter->cell_end].last_sector;
398
399         /* Block count, duration */
400         pgc_id = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgcn;
401         pgn    = vts->vts_ptt_srpt->title[title->ttn-1].ptt[0].pgn;
402         d->pgc = vts->vts_pgcit->pgci_srp[pgc_id-1].pgc;
403         chapter->block_count = 0;
404         chapter->duration = 0;
405
406         d->cell_cur = chapter->cell_start;
407         while( d->cell_cur <= chapter->cell_end )
408         {
409 #define cp d->pgc->cell_playback[d->cell_cur]
410             chapter->block_count += cp.last_sector + 1 - cp.first_sector;
411             chapter->duration += 90LL * dvdtime2msec( &cp.playback_time );
412 #undef cp
413             FindNextCell( d );
414             d->cell_cur = d->cell_next;
415         }
416
417         if( chapter->block_count < 2048 && chapter->index > 1 )
418         {
419             hb_log( "scan: chapter %d too short (%d blocks, "
420                     "cells=%d->%d), merging", chapter->index,
421                     chapter->block_count, chapter->cell_start,
422                     chapter->cell_end );
423             chapter_old = hb_list_item( title->list_chapter,
424                                         chapter->index - 2 );
425             chapter_old->cell_end    = chapter->cell_end;
426             chapter_old->block_end   = chapter->block_end;
427             chapter_old->block_count += chapter->block_count;
428             free( chapter );
429             chapter = chapter_old;
430         }
431         else
432         {
433             hb_list_add( title->list_chapter, chapter );
434             c++;
435         }
436     }
437
438     /* The durations we get for chapters aren't precise. Scale them so
439        the total matches the title duration */
440     duration = 0;
441     for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
442     {
443         chapter = hb_list_item( title->list_chapter, i );
444         duration += chapter->duration;
445     }
446     duration_correction = (float) title->duration / (float) duration;
447     for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
448     {
449         int seconds;
450         chapter            = hb_list_item( title->list_chapter, i );
451         chapter->duration  = duration_correction * chapter->duration;
452         seconds            = ( chapter->duration + 45000 ) / 90000;
453         chapter->hours     = seconds / 3600;
454         chapter->minutes   = ( seconds % 3600 ) / 60;
455         chapter->seconds   = seconds % 60;
456
457         hb_log( "scan: chap %d c=%d->%d, b=%d->%d (%d), %lld ms",
458                 chapter->index, chapter->cell_start, chapter->cell_end,
459                 chapter->block_start, chapter->block_end,
460                 chapter->block_count, chapter->duration / 90 );
461     }
462
463     /* Get aspect. We don't get width/height/rate infos here as
464        they tend to be wrong */
465     switch( vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
466     {
467         case 0:
468             title->aspect = HB_ASPECT_BASE * 4 / 3;
469             break;
470         case 3:
471             title->aspect = HB_ASPECT_BASE * 16 / 9;
472             break;
473         default:
474             hb_log( "scan: unknown aspect" );
475             goto fail;
476     }
477
478     hb_log( "scan: aspect = %d", title->aspect );
479
480     /* This title is ok so far */
481     goto cleanup;
482
483 fail:
484     hb_list_close( &title->list_audio );
485     free( title );
486     title = NULL;
487
488 cleanup:
489     if( vts ) ifoClose( vts );
490
491     return title;
492 }
493
494 /***********************************************************************
495  * hb_dvd_start
496  ***********************************************************************
497  * Title and chapter start at 1
498  **********************************************************************/
499 int hb_dvd_start( hb_dvd_t * d, int title, int chapter )
500 {
501     int pgc_id, pgn;
502     int i;
503
504     /* Open the IFO and the VOBs for this title */
505     d->vts = d->vmg->tt_srpt->title[title-1].title_set_nr;
506     d->ttn = d->vmg->tt_srpt->title[title-1].vts_ttn;
507     if( !( d->ifo = ifoOpen( d->reader, d->vts ) ) )
508     {
509         hb_log( "dvd: ifoOpen failed for VTS %d", d->vts );
510         return 0;
511     }
512     if( !( d->file = DVDOpenFile( d->reader, d->vts,
513                                   DVD_READ_TITLE_VOBS ) ) )
514     {
515         hb_log( "dvd: DVDOpenFile failed for VTS %d", d->vts );
516         return 0;
517     }
518
519     /* Get title first and last blocks */
520     pgc_id         = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[0].pgcn;
521     pgn            = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[0].pgn;
522     d->pgc         = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
523     d->cell_start  = d->pgc->program_map[pgn - 1] - 1;
524     d->cell_end    = d->pgc->nr_of_cells - 1;
525     d->title_start = d->pgc->cell_playback[d->cell_start].first_sector;
526     d->title_end   = d->pgc->cell_playback[d->cell_end].last_sector;
527
528     /* Block count */
529     d->title_block_count = 0;
530     for( i = d->cell_start; i <= d->cell_end; i++ )
531     {
532         d->title_block_count += d->pgc->cell_playback[i].last_sector + 1 -
533             d->pgc->cell_playback[i].first_sector;
534     }
535
536     /* Get pgc for the current chapter */
537     pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[chapter-1].pgcn;
538     pgn    = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[chapter-1].pgn;
539     d->pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
540
541     /* Get the two first cells */
542     d->cell_cur = d->pgc->program_map[pgn-1] - 1;
543     FindNextCell( d );
544
545     d->block     = d->pgc->cell_playback[d->cell_cur].first_sector;
546     d->next_vobu = d->block;
547     d->pack_len  = 0;
548
549     return 1;
550 }
551
552 /***********************************************************************
553  * hb_dvd_stop
554  ***********************************************************************
555  *
556  **********************************************************************/
557 void hb_dvd_stop( hb_dvd_t * d )
558 {
559     if( d->ifo )
560     {
561         ifoClose( d->ifo );
562         d->ifo = NULL;
563     }
564     if( d->file )
565     {
566         DVDCloseFile( d->file );
567         d->file = NULL;
568     }
569 }
570
571 /***********************************************************************
572  * hb_dvd_seek
573  ***********************************************************************
574  *
575  **********************************************************************/
576 int hb_dvd_seek( hb_dvd_t * d, float f )
577 {
578     int count, sizeCell;
579     int i;
580
581     count = f * d->title_block_count;
582
583     for( i = d->cell_start; i <= d->cell_end; i++ )
584     {
585         sizeCell = d->pgc->cell_playback[i].last_sector + 1 -
586             d->pgc->cell_playback[i].first_sector;
587
588         if( count < sizeCell )
589         {
590             d->cell_cur = i;
591             FindNextCell( d );
592
593             /* Now let hb_dvd_read find the next VOBU */
594             d->next_vobu = d->pgc->cell_playback[i].first_sector + count;
595             d->pack_len  = 0;
596             break;
597         }
598
599         count -= sizeCell;
600     }
601
602     if( i > d->cell_end )
603     {
604         return 0;
605     }
606
607     return 1;
608 }
609
610
611 /***********************************************************************
612  * is_nav_pack
613  ***********************************************************************
614  * Pretty much directly lifted from libdvdread's play_title function.
615  **********************************************************************/
616 int is_nav_pack( unsigned char *buf )
617 {
618     return ( buf[41] == 0xbf && buf[1027] == 0xbf );
619 }
620
621
622 /***********************************************************************
623  * hb_dvd_read
624  ***********************************************************************
625  *
626  **********************************************************************/
627 int hb_dvd_read( hb_dvd_t * d, hb_buffer_t * b )
628 {
629     if( !d->pack_len )
630     {
631         /* New pack */
632         dsi_t dsi_pack;
633         int   error;
634
635         error = 0;
636         
637         for( ;; )
638         {
639             int block, pack_len, next_vobu;
640
641             if( DVDReadBlocks( d->file, d->next_vobu, 1, b->data ) != 1 )
642             {
643                 hb_log( "dvd: DVDReadBlocks failed (%d)", d->next_vobu );
644                 return 0;
645             }
646
647             if ( !is_nav_pack( b->data ) ) { 
648                 (d->next_vobu)++;
649                 continue;
650             }
651
652             navRead_DSI( &dsi_pack, &b->data[DSI_START_BYTE] );
653             
654             block     = dsi_pack.dsi_gi.nv_pck_lbn;
655             pack_len  = dsi_pack.dsi_gi.vobu_ea;
656             next_vobu = block + ( dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
657
658             if( pack_len >  0    &&
659                 pack_len <  1024 &&
660                 block    >= d->next_vobu &&
661                 ( block <= d->title_start + d->title_block_count ||
662                   block <= d->title_end ) )
663             {
664                 /* XXX
665                    This looks like a valid VOBU, but actually we are
666                    just hoping */
667                 if( error )
668                 {
669 #if 0
670                     hb_log( "dvd: found VOBU at %d (b %d, l %d, n %d)",
671                             d->next_vobu, block, pack_len, next_vobu );
672 #endif
673                 }
674                 d->block     = block;
675                 d->pack_len  = pack_len;
676                 d->next_vobu = next_vobu;
677                 break;
678             }
679
680             /* Wasn't a valid VOBU, try next block */
681             if( !error )
682             {
683 #if 0
684                 hb_log( "dvd: looking for a VOBU (%d)", d->next_vobu );
685 #endif
686             }
687
688             if( ++error > 1024 )
689             {
690                 hb_log( "dvd: couldn't find a VOBU after 1024 blocks" );
691                 return 0;
692             }
693
694             (d->next_vobu)++;
695         }
696
697         if( dsi_pack.vobu_sri.next_vobu == SRI_END_OF_CELL )
698         {
699             d->cell_cur  = d->cell_next;
700             d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
701             FindNextCell( d );
702         }
703     }
704     else
705     {
706         if( DVDReadBlocks( d->file, d->block, 1, b->data ) != 1 )
707         {
708             hb_log( "reader: DVDReadBlocks failed (%d)", d->block );
709             return 0;
710         }
711         d->pack_len--;
712     }
713
714     d->block++;
715
716     return 1;
717 }
718
719 /***********************************************************************
720  * hb_dvd_chapter
721  ***********************************************************************
722  * Returns in which chapter the next block to be read is.
723  * Chapter numbers start at 1.
724  **********************************************************************/
725 int hb_dvd_chapter( hb_dvd_t * d )
726 {
727     int     i;
728     int     pgc_id, pgn;
729     pgc_t * pgc;
730
731     for( i = 0;
732          i < d->ifo->vts_ptt_srpt->title[d->ttn-1].nr_of_ptts;
733          i++ )
734     {
735         /* Get pgc for chapter (i+1) */
736         pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgcn;
737         pgn    = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgn;
738         pgc    = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
739
740         if( d->cell_cur >= pgc->program_map[pgn-1] - 1 &&
741             d->cell_cur <= pgc->nr_of_cells - 1 )
742         {
743             /* We are in this chapter */
744             return i + 1;
745         }
746     }
747
748     /* End of title */
749     return -1;
750 }
751
752 /***********************************************************************
753  * hb_dvd_close
754  ***********************************************************************
755  * Closes and frees everything
756  **********************************************************************/
757 void hb_dvd_close( hb_dvd_t ** _d )
758 {
759     hb_dvd_t * d = *_d;
760
761     if( d->vmg )
762     {
763         ifoClose( d->vmg );
764     }
765     if( d->reader )
766     {
767         DVDClose( d->reader );
768     }
769
770     free( d );
771     *_d = NULL;
772 }
773
774 /***********************************************************************
775  * FindNextCell
776  ***********************************************************************
777  * Assumes pgc and cell_cur are correctly set, and sets cell_next to the
778  * cell to be read when we will be done with cell_cur.
779  **********************************************************************/
780 static void FindNextCell( hb_dvd_t * d )
781 {
782     int i = 0;
783
784     if( d->pgc->cell_playback[d->cell_cur].block_type ==
785             BLOCK_TYPE_ANGLE_BLOCK )
786     {
787
788         while( d->pgc->cell_playback[d->cell_cur+i].block_mode !=
789                    BLOCK_MODE_LAST_CELL )
790         {
791              i++;
792         }
793         d->cell_next = d->cell_cur + i + 1;
794     }
795     else
796     {
797         d->cell_next = d->cell_cur + 1;
798     }
799 }
800
801 /***********************************************************************
802  * dvdtime2msec
803  ***********************************************************************
804  * From lsdvd
805  **********************************************************************/
806 static int dvdtime2msec(dvd_time_t * dt)
807 {
808     double frames_per_s[4] = {-1.0, 25.00, -1.0, 29.97};
809     double fps = frames_per_s[(dt->frame_u & 0xc0) >> 6];
810     long   ms;
811     ms  = (((dt->hour &   0xf0) >> 3) * 5 + (dt->hour   & 0x0f)) * 3600000;
812     ms += (((dt->minute & 0xf0) >> 3) * 5 + (dt->minute & 0x0f)) * 60000;
813     ms += (((dt->second & 0xf0) >> 3) * 5 + (dt->second & 0x0f)) * 1000;
814
815     if( fps > 0 )
816     {
817         ms += ((dt->frame_u & 0x30) >> 3) * 5 +
818               (dt->frame_u & 0x0f) * 1000.0 / fps;
819     }
820
821     return ms;
822 }