OSDN Git Service

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