OSDN Git Service

Improved debug messages for Cell start/stop with Cell number and block.
[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             free( chapter );
454             chapter = chapter_old;
455         }
456         else
457         {
458             hb_list_add( title->list_chapter, chapter );
459             c++;
460         }
461     }
462
463     /* The durations we get for chapters aren't precise. Scale them so
464        the total matches the title duration */
465     duration = 0;
466     for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
467     {
468         chapter = hb_list_item( title->list_chapter, i );
469         duration += chapter->duration;
470     }
471     duration_correction = (float) title->duration / (float) duration;
472     for( i = 0; i < hb_list_count( title->list_chapter ); i++ )
473     {
474         int seconds;
475         chapter            = hb_list_item( title->list_chapter, i );
476         chapter->duration  = duration_correction * chapter->duration;
477         seconds            = ( chapter->duration + 45000 ) / 90000;
478         chapter->hours     = seconds / 3600;
479         chapter->minutes   = ( seconds % 3600 ) / 60;
480         chapter->seconds   = seconds % 60;
481
482         hb_log( "scan: chap %d c=%d->%d, b=%d->%d (%d), %lld ms",
483                 chapter->index, chapter->cell_start, chapter->cell_end,
484                 chapter->block_start, chapter->block_end,
485                 chapter->block_count, chapter->duration / 90 );
486     }
487
488     /* Get aspect. We don't get width/height/rate infos here as
489        they tend to be wrong */
490     switch( vts->vtsi_mat->vts_video_attr.display_aspect_ratio )
491     {
492         case 0:
493             title->aspect = HB_ASPECT_BASE * 4 / 3;
494             break;
495         case 3:
496             title->aspect = HB_ASPECT_BASE * 16 / 9;
497             break;
498         default:
499             hb_log( "scan: unknown aspect" );
500             goto fail;
501     }
502
503     hb_log( "scan: aspect = %d", title->aspect );
504
505     /* This title is ok so far */
506     goto cleanup;
507
508 fail:
509     hb_list_close( &title->list_audio );
510     free( title );
511     title = NULL;
512
513 cleanup:
514     if( vts ) ifoClose( vts );
515
516     return title;
517 }
518
519 /***********************************************************************
520  * hb_dvd_start
521  ***********************************************************************
522  * Title and chapter start at 1
523  **********************************************************************/
524 int hb_dvd_start( hb_dvd_t * d, int title, int chapter )
525 {
526     int pgc_id, pgn;
527     int i;
528
529     /* Open the IFO and the VOBs for this title */
530     d->vts = d->vmg->tt_srpt->title[title-1].title_set_nr;
531     d->ttn = d->vmg->tt_srpt->title[title-1].vts_ttn;
532     if( !( d->ifo = ifoOpen( d->reader, d->vts ) ) )
533     {
534         hb_error( "dvd: ifoOpen failed for VTS %d", d->vts );
535         return 0;
536     }
537     if( !( d->file = DVDOpenFile( d->reader, d->vts,
538                                   DVD_READ_TITLE_VOBS ) ) )
539     {
540         hb_error( "dvd: DVDOpenFile failed for VTS %d", d->vts );
541         return 0;
542     }
543
544     /* Get title first and last blocks */
545     pgc_id         = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[0].pgcn;
546     pgn            = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[0].pgn;
547     d->pgc         = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
548     d->cell_start  = d->pgc->program_map[pgn - 1] - 1;
549     d->cell_end    = d->pgc->nr_of_cells - 1;
550     d->title_start = d->pgc->cell_playback[d->cell_start].first_sector;
551     d->title_end   = d->pgc->cell_playback[d->cell_end].last_sector;
552
553     /* Block count */
554     d->title_block_count = 0;
555     for( i = d->cell_start; i <= d->cell_end; i++ )
556     {
557         d->title_block_count += d->pgc->cell_playback[i].last_sector + 1 -
558             d->pgc->cell_playback[i].first_sector;
559     }
560
561     /* Get pgc for the current chapter */
562     pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[chapter-1].pgcn;
563     pgn    = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[chapter-1].pgn;
564     d->pgc = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
565
566     /* Get the two first cells */
567     d->cell_cur = d->pgc->program_map[pgn-1] - 1;
568     FindNextCell( d );
569
570     d->block     = d->pgc->cell_playback[d->cell_cur].first_sector;
571     d->next_vobu = d->block;
572     d->pack_len  = 0;
573     d->cell_overlap = 0;
574     
575     return 1;
576 }
577
578 /***********************************************************************
579  * hb_dvd_stop
580  ***********************************************************************
581  *
582  **********************************************************************/
583 void hb_dvd_stop( hb_dvd_t * d )
584 {
585     if( d->ifo )
586     {
587         ifoClose( d->ifo );
588         d->ifo = NULL;
589     }
590     if( d->file )
591     {
592         DVDCloseFile( d->file );
593         d->file = NULL;
594     }
595 }
596
597 /***********************************************************************
598  * hb_dvd_seek
599  ***********************************************************************
600  *
601  **********************************************************************/
602 int hb_dvd_seek( hb_dvd_t * d, float f )
603 {
604     int count, sizeCell;
605     int i;
606
607     count = f * d->title_block_count;
608
609     for( i = d->cell_start; i <= d->cell_end; i++ )
610     {
611         sizeCell = d->pgc->cell_playback[i].last_sector + 1 -
612             d->pgc->cell_playback[i].first_sector;
613
614         if( count < sizeCell )
615         {
616             d->cell_cur = i;
617             FindNextCell( d );
618
619             /* Now let hb_dvd_read find the next VOBU */
620             d->next_vobu = d->pgc->cell_playback[i].first_sector + count;
621             d->pack_len  = 0;
622             break;
623         }
624
625         count -= sizeCell;
626     }
627
628     if( i > d->cell_end )
629     {
630         return 0;
631     }
632
633     return 1;
634 }
635
636
637 /***********************************************************************
638  * is_nav_pack
639  ***********************************************************************
640  * Pretty much directly lifted from libdvdread's play_title function.
641  **********************************************************************/
642 int is_nav_pack( unsigned char *buf )
643 {
644     return ( buf[41] == 0xbf && buf[1027] == 0xbf );
645 }
646
647
648 /***********************************************************************
649  * hb_dvd_read
650  ***********************************************************************
651  *
652  **********************************************************************/
653 int hb_dvd_read( hb_dvd_t * d, hb_buffer_t * b )
654 {
655     if( !d->pack_len )
656     {
657         /* New pack */
658         dsi_t dsi_pack;
659         int   error;
660
661         error = 0;
662         
663         for( ;; )
664         {
665             int block, pack_len, next_vobu, read_retry;
666
667             for( read_retry = 0; read_retry < 3; read_retry++ )
668             {
669                 if( DVDReadBlocks( d->file, d->next_vobu, 1, b->data ) == 1 )
670                 {
671                     /*
672                      * Successful read.
673                      */
674                     break;
675                 } else {
676                     hb_log( "dvd: Read Error on blk %d, attempt %d",
677                             d->next_vobu, read_retry );
678                 }
679             }
680
681             if( read_retry == 3 )
682             {
683                 hb_log( "dvd: Unrecoverable Read Error from DVD, potential HD or DVD Failure (blk: %d)", d->next_vobu );
684                 return 0;
685             }
686
687             if ( !is_nav_pack( b->data ) ) { 
688                 (d->next_vobu)++;
689                 continue;
690             }
691
692             navRead_DSI( &dsi_pack, &b->data[DSI_START_BYTE] );
693             
694             block     = dsi_pack.dsi_gi.nv_pck_lbn;
695             pack_len  = dsi_pack.dsi_gi.vobu_ea;
696             next_vobu = block + ( dsi_pack.vobu_sri.next_vobu & 0x7fffffff );
697
698             if( pack_len >  0    &&
699                 pack_len <  1024 &&
700                 block    >= d->next_vobu &&
701                 ( block <= d->title_start + d->title_block_count ||
702                   block <= d->title_end ) )
703             {
704                 /* XXX
705                    This looks like a valid VOBU, but actually we are
706                    just hoping */
707                 if( error )
708                 {
709 #if 0
710                     hb_log( "dvd: found VOBU at %d (b %d, l %d, n %d)",
711                             d->next_vobu, block, pack_len, next_vobu );
712 #endif
713                 }
714                 d->block     = block;
715                 d->pack_len  = pack_len;
716                 d->next_vobu = next_vobu;
717                 break;
718             }
719
720             /* Wasn't a valid VOBU, try next block */
721             if( !error )
722             {
723 #if 0
724                 hb_log( "dvd: looking for a VOBU (%d)", d->next_vobu );
725 #endif
726             }
727
728             if( ++error > 1024 )
729             {
730                 hb_log( "dvd: couldn't find a VOBU after 1024 blocks" );
731                 return 0;
732             }
733
734             (d->next_vobu)++;
735         }
736
737         if( dsi_pack.vobu_sri.next_vobu == SRI_END_OF_CELL )
738         {
739             hb_log( "DVD: End of Cell (%d) at block %d", d->cell_cur, 
740                     d->block );
741             d->cell_cur  = d->cell_next;
742             d->next_vobu = d->pgc->cell_playback[d->cell_cur].first_sector;
743             FindNextCell( d );
744             d->cell_overlap = 1;
745         }
746         
747         // Revert the cell overlap, and check for a chapter break
748         if( dsi_pack.vobu_sri.prev_vobu == SRI_END_OF_CELL )
749         {
750             hb_log( "DVD: Beginning of Cell (%d) at block %d", d->cell_cur, 
751                    d->block );
752             if( d->cell_overlap )
753             {
754                 b->new_chap = hb_dvd_is_break( d );
755                 d->cell_overlap = 0;
756             }
757         }
758     }
759     else
760     {
761         if( DVDReadBlocks( d->file, d->block, 1, b->data ) != 1 )
762         {
763             hb_error( "reader: DVDReadBlocks failed (%d)", d->block );
764             return 0;
765         }
766         d->pack_len--;
767     }
768
769     d->block++;
770
771     return 1;
772 }
773
774 /***********************************************************************
775  * hb_dvd_chapter
776  ***********************************************************************
777  * Returns in which chapter the next block to be read is.
778  * Chapter numbers start at 1.
779  **********************************************************************/
780 int hb_dvd_chapter( hb_dvd_t * d )
781 {
782     int     i;
783     int     pgc_id, pgn;
784         int     nr_of_ptts = d->ifo->vts_ptt_srpt->title[d->ttn-1].nr_of_ptts;
785     pgc_t * pgc;
786
787     for( i = nr_of_ptts - 1;
788          i >= 0;
789          i-- )
790     {
791         /* Get pgc for chapter (i+1) */
792         pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgcn;
793         pgn    = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgn;
794         pgc    = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
795
796         if( d->cell_cur - d->cell_overlap >= pgc->program_map[pgn-1] - 1 &&
797             d->cell_cur - d->cell_overlap <= pgc->nr_of_cells - 1 )
798         {
799             /* We are in this chapter */
800             return i + 1;
801         }
802     }
803
804     /* End of title */
805     return -1;
806 }
807
808 /***********************************************************************
809  * hb_dvd_is_break
810  ***********************************************************************
811  * Returns 1 if the current block is a new chapter start
812  **********************************************************************/
813 int hb_dvd_is_break( hb_dvd_t * d )
814 {
815     int     i, j;
816     int     pgc_id, pgn;
817         int     nr_of_ptts = d->ifo->vts_ptt_srpt->title[d->ttn-1].nr_of_ptts;
818     pgc_t * pgc;
819     int     cell, chapter_length, cell_end;
820     
821     for( i = nr_of_ptts - 1;
822          i > 0;
823          i-- )
824     {
825         /* Get pgc for chapter (i+1) */
826         pgc_id = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgcn;
827         pgn    = d->ifo->vts_ptt_srpt->title[d->ttn-1].ptt[i].pgn;
828         pgc    = d->ifo->vts_pgcit->pgci_srp[pgc_id-1].pgc;
829         cell   = pgc->program_map[pgn-1] - 1;
830
831         if( cell <= d->cell_start )
832             break;
833
834         // This must not match against the start cell.
835         if( pgc->cell_playback[cell].first_sector == d->block && cell != d->cell_start )
836         {
837             /* Check to see if we merged this chapter into the previous one... */
838             /* As a note, merging chapters is probably bad practice for this very reason */
839             chapter_length = 0;
840             
841             if( i == nr_of_ptts - 1 )
842             {
843                 cell_end = d->pgc->nr_of_cells;
844             }
845             else
846             {
847                 cell_end = pgc->program_map[pgn] - 1;
848             }
849             
850             for( j = cell; j < cell_end; j++ )
851             {
852                 chapter_length += pgc->cell_playback[j].last_sector + 1 - 
853                                   pgc->cell_playback[j].first_sector;
854             }
855             
856             if( chapter_length >= 2048 )
857             {
858                 hb_log("DVD: Chapter Break Cell Found");
859                 /* We have a chapter break */
860                 return 1;
861             }
862             else
863             {
864                 hb_log("DVD: Cell Found (%d)", chapter_length);
865             }
866         }
867     }
868     
869     return 0;
870 }
871
872 /***********************************************************************
873  * hb_dvd_close
874  ***********************************************************************
875  * Closes and frees everything
876  **********************************************************************/
877 void hb_dvd_close( hb_dvd_t ** _d )
878 {
879     hb_dvd_t * d = *_d;
880
881     if( d->vmg )
882     {
883         ifoClose( d->vmg );
884     }
885     if( d->reader )
886     {
887         DVDClose( d->reader );
888     }
889
890     free( d );
891     *_d = NULL;
892 }
893
894 /***********************************************************************
895  * FindNextCell
896  ***********************************************************************
897  * Assumes pgc and cell_cur are correctly set, and sets cell_next to the
898  * cell to be read when we will be done with cell_cur.
899  **********************************************************************/
900 static void FindNextCell( hb_dvd_t * d )
901 {
902     int i = 0;
903
904     if( d->pgc->cell_playback[d->cell_cur].block_type ==
905             BLOCK_TYPE_ANGLE_BLOCK )
906     {
907
908         while( d->pgc->cell_playback[d->cell_cur+i].block_mode !=
909                    BLOCK_MODE_LAST_CELL )
910         {
911              i++;
912         }
913         d->cell_next = d->cell_cur + i + 1;
914     }
915     else
916     {
917         d->cell_next = d->cell_cur + 1;
918     }
919 }
920
921 /***********************************************************************
922  * dvdtime2msec
923  ***********************************************************************
924  * From lsdvd
925  **********************************************************************/
926 static int dvdtime2msec(dvd_time_t * dt)
927 {
928     double frames_per_s[4] = {-1.0, 25.00, -1.0, 29.97};
929     double fps = frames_per_s[(dt->frame_u & 0xc0) >> 6];
930     long   ms;
931     ms  = (((dt->hour &   0xf0) >> 3) * 5 + (dt->hour   & 0x0f)) * 3600000;
932     ms += (((dt->minute & 0xf0) >> 3) * 5 + (dt->minute & 0x0f)) * 60000;
933     ms += (((dt->second & 0xf0) >> 3) * 5 + (dt->second & 0x0f)) * 1000;
934
935     if( fps > 0 )
936     {
937         ms += ((dt->frame_u & 0x30) >> 3) * 5 +
938               (dt->frame_u & 0x0f) * 1000.0 / fps;
939     }
940
941     return ms;
942 }