OSDN Git Service

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