OSDN Git Service

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