forked from HandBrake/HandBrake
-
Notifications
You must be signed in to change notification settings - Fork 0
/
dvdnav.c
2179 lines (1920 loc) · 68.3 KB
/
dvdnav.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* dvdnav.c
Copyright (c) 2003-2019 HandBrake Team
This file is part of the HandBrake source code
Homepage: <http://handbrake.fr/>.
It may be used under the terms of the GNU General Public License v2.
For full terms see the file COPYING file or visit http://www.gnu.org/licenses/gpl-2.0.html
*/
#include "libavcodec/avcodec.h"
#include "handbrake/handbrake.h"
#include "handbrake/lang.h"
#include "handbrake/dvd.h"
#include "dvdnav/dvdnav.h"
#include "dvdread/ifo_read.h"
#include "dvdread/ifo_print.h"
#include "dvdread/nav_read.h"
#define DVD_READ_CACHE 1
static char * hb_dvdnav_name( char * path );
static hb_dvd_t * hb_dvdnav_init( hb_handle_t * h, const char * path );
static int hb_dvdnav_title_count( hb_dvd_t * d );
static hb_title_t * hb_dvdnav_title_scan( hb_dvd_t * d, int t, uint64_t min_duration );
static int hb_dvdnav_start( hb_dvd_t * d, hb_title_t *title, int chapter );
static void hb_dvdnav_stop( hb_dvd_t * d );
static int hb_dvdnav_seek( hb_dvd_t * d, float f );
static hb_buffer_t * hb_dvdnav_read( hb_dvd_t * d );
static int hb_dvdnav_chapter( hb_dvd_t * d );
static void hb_dvdnav_close( hb_dvd_t ** _d );
static int hb_dvdnav_angle_count( hb_dvd_t * d );
static void hb_dvdnav_set_angle( hb_dvd_t * d, int angle );
static int hb_dvdnav_main_feature( hb_dvd_t * d, hb_list_t * list_title );
hb_dvd_func_t hb_dvdnav_func =
{
hb_dvdnav_init,
hb_dvdnav_close,
hb_dvdnav_name,
hb_dvdnav_title_count,
hb_dvdnav_title_scan,
hb_dvdnav_start,
hb_dvdnav_stop,
hb_dvdnav_seek,
hb_dvdnav_read,
hb_dvdnav_chapter,
hb_dvdnav_angle_count,
hb_dvdnav_set_angle,
hb_dvdnav_main_feature
};
// there can be at most 999 PGCs per title. round that up to the nearest
// power of two.
#define MAX_PGCN 1024
/***********************************************************************
* Local prototypes
**********************************************************************/
static void PgcWalkInit( uint32_t pgcn_map[MAX_PGCN/32] );
static int FindChapterIndex( hb_list_t * list, int pgcn, int pgn );
static int NextPgcn( ifo_handle_t *ifo, int pgcn, uint32_t pgcn_map[MAX_PGCN/32] );
static int FindNextCell( pgc_t *pgc, int cell_cur );
static int dvdtime2msec( dvd_time_t * );
static int TitleOpenIfo(hb_dvdnav_t * d, int t);
static void TitleCloseIfo(hb_dvdnav_t * d);
hb_dvd_func_t * hb_dvdnav_methods( void )
{
return &hb_dvdnav_func;
}
static char * hb_dvdnav_name( char * path )
{
static char name[1024];
unsigned char unused[1024];
dvd_reader_t * reader;
reader = DVDOpen( path );
if( !reader )
{
return NULL;
}
if( DVDUDFVolumeInfo( reader, name, sizeof( name ),
unused, sizeof( unused ) ) )
{
DVDClose( reader );
return NULL;
}
DVDClose( reader );
return name;
}
/***********************************************************************
* hb_dvdnav_reset
***********************************************************************
* Once dvdnav has entered the 'stopped' state, it can not be revived
* dvdnav_reset doesn't work because it doesn't remember the path
* So this function re-opens dvdnav
**********************************************************************/
static int hb_dvdnav_reset( hb_dvdnav_t * d )
{
char * path_ccp = hb_utf8_to_cp( d->path );
if ( d->dvdnav )
dvdnav_close( d->dvdnav );
/* Open device */
if( dvdnav_open(&d->dvdnav, path_ccp) != DVDNAV_STATUS_OK )
{
/*
* Not an error, may be a stream - which we'll try in a moment.
*/
hb_log( "dvd: not a dvd - trying as a stream/file instead" );
goto fail;
}
if (dvdnav_set_readahead_flag(d->dvdnav, DVD_READ_CACHE) !=
DVDNAV_STATUS_OK)
{
hb_error("Error: dvdnav_set_readahead_flag: %s\n",
dvdnav_err_to_string(d->dvdnav));
goto fail;
}
/*
** set the PGC positioning flag to have position information
** relatively to the whole feature instead of just relatively to the
** current chapter
**/
if (dvdnav_set_PGC_positioning_flag(d->dvdnav, 1) != DVDNAV_STATUS_OK)
{
hb_error("Error: dvdnav_set_PGC_positioning_flag: %s\n",
dvdnav_err_to_string(d->dvdnav));
goto fail;
}
free( path_ccp );
return 1;
fail:
if( d->dvdnav ) dvdnav_close( d->dvdnav );
free( path_ccp );
return 0;
}
/***********************************************************************
* hb_dvdnav_init
***********************************************************************
*
**********************************************************************/
static hb_dvd_t * hb_dvdnav_init( hb_handle_t * h, const char * path )
{
hb_dvd_t * e;
hb_dvdnav_t * d;
int region_mask;
char * path_ccp;
e = calloc( sizeof( hb_dvd_t ), 1 );
d = &(e->dvdnav);
d->h = h;
/*
* Convert UTF-8 path to current code page on Windows
* hb_utf8_to_cp() is the same as strdup on non-Windows,
* so no #ifdef required here
*/
path_ccp = hb_utf8_to_cp( path );
/* Log DVD drive region code */
if ( hb_dvd_region( path_ccp, ®ion_mask ) == 0 )
{
hb_log( "dvd: Region mask 0x%02x", region_mask );
if ( region_mask == 0xFF )
{
hb_log( "dvd: Warning, DVD device has no region set" );
}
}
/* Open device */
if( dvdnav_open(&d->dvdnav, path_ccp) != DVDNAV_STATUS_OK )
{
/*
* Not an error, may be a stream - which we'll try in a moment.
*/
hb_log( "dvd: not a dvd - trying as a stream/file instead" );
goto fail;
}
if (dvdnav_set_readahead_flag(d->dvdnav, DVD_READ_CACHE) !=
DVDNAV_STATUS_OK)
{
hb_error("Error: dvdnav_set_readahead_flag: %s\n",
dvdnav_err_to_string(d->dvdnav));
goto fail;
}
/*
** set the PGC positioning flag to have position information
** relatively to the whole feature instead of just relatively to the
** current chapter
**/
if (dvdnav_set_PGC_positioning_flag(d->dvdnav, 1) != DVDNAV_STATUS_OK)
{
hb_error("Error: dvdnav_set_PGC_positioning_flag: %s\n",
dvdnav_err_to_string(d->dvdnav));
goto fail;
}
/* Open device */
if( !( d->reader = DVDOpen( path_ccp ) ) )
{
/*
* Not an error, may be a stream - which we'll try in a moment.
*/
hb_log( "dvd: not a dvd - trying as a stream/file instead" );
goto fail;
}
/* Open main IFO */
if( !( d->vmg = ifoOpen( d->reader, 0 ) ) )
{
hb_error( "dvd: ifoOpen failed" );
goto fail;
}
d->path = strdup( path ); /* hb_dvdnav_title_scan assumes UTF-8 path, so not path_ccp here */
free( path_ccp );
return e;
fail:
if( d->dvdnav ) dvdnav_close( d->dvdnav );
if( d->vmg ) ifoClose( d->vmg );
if( d->reader ) DVDClose( d->reader );
free( e );
free( path_ccp );
return NULL;
}
/***********************************************************************
* hb_dvdnav_title_count
**********************************************************************/
static int hb_dvdnav_title_count( hb_dvd_t * e )
{
int titles = 0;
hb_dvdnav_t * d = &(e->dvdnav);
dvdnav_get_number_of_titles(d->dvdnav, &titles);
return titles;
}
static uint64_t
PttDuration(ifo_handle_t *ifo, int ttn, int pttn, int *blocks, int *last_pgcn)
{
int pgcn, pgn;
pgc_t * pgc;
uint64_t duration = 0;
int cell_start, cell_end;
int i;
*blocks = 0;
// Initialize map of visited pgc's to prevent loops
uint32_t pgcn_map[MAX_PGCN/32];
PgcWalkInit( pgcn_map );
pgcn = ifo->vts_ptt_srpt->title[ttn-1].ptt[pttn-1].pgcn;
pgn = ifo->vts_ptt_srpt->title[ttn-1].ptt[pttn-1].pgn;
if ( pgcn < 1 || pgcn > ifo->vts_pgcit->nr_of_pgci_srp || pgcn >= MAX_PGCN)
{
hb_log( "invalid PGC ID %d, skipping", pgcn );
return 0;
}
if( pgn <= 0 || pgn > 99 )
{
hb_log( "scan: pgn %d not valid, skipping", pgn );
return 0;
}
do
{
pgc = ifo->vts_pgcit->pgci_srp[pgcn-1].pgc;
if (!pgc)
{
*blocks = 0;
duration = 0;
hb_log( "scan: pgc not valid, skipping" );
break;
}
if (pgc->cell_playback == NULL)
{
*blocks = 0;
duration = 0;
hb_log("invalid PGC cell_playback table, skipping");
break;
}
if (pgn > pgc->nr_of_programs)
{
pgn = 1;
continue;
}
duration += 90LL * dvdtime2msec( &pgc->playback_time );
cell_start = pgc->program_map[pgn-1] - 1;
cell_end = pgc->nr_of_cells - 1;
for(i = cell_start; i <= cell_end; i = FindNextCell(pgc, i))
{
*blocks += pgc->cell_playback[i].last_sector + 1 -
pgc->cell_playback[i].first_sector;
}
*last_pgcn = pgcn;
pgn = 1;
} while((pgcn = NextPgcn(ifo, pgcn, pgcn_map)) != 0);
return duration;
}
static void add_subtitle( hb_list_t * list_subtitle, int position,
iso639_lang_t * lang, int lang_extension,
uint8_t * palette, int style )
{
hb_subtitle_t * subtitle;
int ii, count;
count = hb_list_count(list_subtitle);
for (ii = 0; ii < count; ii++)
{
subtitle = hb_list_item(list_subtitle, ii);
if (((subtitle->id >> 8) & 0x1f) == position)
{
// The subtitle is already in the list
return;
}
}
subtitle = calloc(sizeof(hb_subtitle_t), 1);
subtitle->track = count;
subtitle->id = ((0x20 + position) << 8) | 0xbd;
snprintf(subtitle->lang, sizeof( subtitle->lang ), "%s",
strlen(lang->native_name) ? lang->native_name : lang->eng_name);
snprintf(subtitle->iso639_2, sizeof( subtitle->iso639_2 ), "%s",
lang->iso639_2);
subtitle->format = PICTURESUB;
subtitle->source = VOBSUB;
subtitle->config.dest = RENDERSUB;
subtitle->stream_type = 0xbd;
subtitle->substream_type = 0x20 + position;
subtitle->codec = WORK_DECVOBSUB;
subtitle->timebase.num = 1;
subtitle->timebase.den = 90000;
memcpy(subtitle->palette, palette, 16 * sizeof(uint32_t));
subtitle->palette_set = 1;
const char * name = NULL;
switch (lang_extension)
{
case 1:
subtitle->attributes = HB_SUBTITLE_ATTR_NORMAL;
break;
case 2:
subtitle->attributes = HB_SUBTITLE_ATTR_LARGE;
strcat(subtitle->lang, " Large Type");
name = "Large Type";
break;
case 3:
subtitle->attributes = HB_SUBTITLE_ATTR_CHILDREN;
strcat(subtitle->lang, " Children");
name = "Children";
break;
case 5:
subtitle->attributes = HB_SUBTITLE_ATTR_CC;
strcat(subtitle->lang, " Closed Caption");
name = "Closed Caption";
break;
case 6:
subtitle->attributes = HB_SUBTITLE_ATTR_CC | HB_SUBTITLE_ATTR_LARGE;
strcat(subtitle->lang, " Closed Caption, Large Type");
name = "Closed Caption, Large Type";
break;
case 7:
subtitle->attributes = HB_SUBTITLE_ATTR_CC |
HB_SUBTITLE_ATTR_CHILDREN;
strcat(subtitle->lang, " Closed Caption, Children");
name = "Closed Caption, Children";
break;
case 9:
subtitle->attributes = HB_SUBTITLE_ATTR_FORCED;
strcat(subtitle->lang, " Forced");
break;
case 13:
subtitle->attributes = HB_SUBTITLE_ATTR_COMMENTARY;
strcat(subtitle->lang, " Director's Commentary");
name = "Commentary";
break;
case 14:
subtitle->attributes = HB_SUBTITLE_ATTR_COMMENTARY |
HB_SUBTITLE_ATTR_LARGE;
strcat(subtitle->lang, " Director's Commentary, Large Type");
name = "Commentary, Large Type";
break;
case 15:
subtitle->attributes = HB_SUBTITLE_ATTR_COMMENTARY |
HB_SUBTITLE_ATTR_CHILDREN;
strcat(subtitle->lang, " Director's Commentary, Children");
name = "Commentary, Children";
default:
subtitle->attributes = HB_SUBTITLE_ATTR_UNKNOWN;
break;
}
if (name != NULL)
{
subtitle->name = strdup(name);
}
switch (style)
{
case HB_VOBSUB_STYLE_4_3:
subtitle->attributes |= HB_SUBTITLE_ATTR_4_3;
strcat(subtitle->lang, " (4:3)");
break;
case HB_VOBSUB_STYLE_WIDE:
subtitle->attributes |= HB_SUBTITLE_ATTR_WIDE;
strcat(subtitle->lang, " (Wide Screen)");
break;
case HB_VOBSUB_STYLE_LETTERBOX:
subtitle->attributes |= HB_SUBTITLE_ATTR_LETTERBOX;
strcat(subtitle->lang, " (Letterbox)");
break;
case HB_VOBSUB_STYLE_PANSCAN:
subtitle->attributes |= HB_SUBTITLE_ATTR_PANSCAN;
strcat(subtitle->lang, " (Pan & Scan)");
break;
}
strcat(subtitle->lang, " [");
strcat(subtitle->lang, hb_subsource_name(subtitle->source));
strcat(subtitle->lang, "]");
hb_log("scan: id=0x%x, lang=%s, 3cc=%s ext=%i", subtitle->id,
subtitle->lang, subtitle->iso639_2, lang_extension);
hb_list_add(list_subtitle, subtitle);
}
/***********************************************************************
* hb_dvdnav_title_scan
**********************************************************************/
static hb_title_t * hb_dvdnav_title_scan( hb_dvd_t * e, int t, uint64_t min_duration )
{
hb_dvdnav_t * d = &(e->dvdnav);
hb_title_t * title;
int pgcn, i;
pgc_t * pgc;
hb_chapter_t * chapter;
hb_dvd_chapter_t * dvd_chapter;
int count;
const char * title_string;
char name[1024];
unsigned char unused[1024];
const char * codec_name;
hb_log( "scan: scanning title %d", t );
title = hb_title_init( d->path, t );
title->type = HB_DVD_TYPE;
if (dvdnav_get_title_string(d->dvdnav, &title_string) == DVDNAV_STATUS_OK)
{
title->name = strdup(title_string);
}
if (title->name == NULL || title->name[0] == 0)
{
free((char*)title->name);
if (DVDUDFVolumeInfo(d->reader, name, sizeof(name),
unused, sizeof(unused)))
{
char * p_cur, * p_last = d->path;
for( p_cur = d->path; *p_cur; p_cur++ )
{
if( IS_DIR_SEP(p_cur[0]) && p_cur[1] )
{
p_last = &p_cur[1];
}
}
title->name = strdup(p_last);
char *dot_term = strrchr(title->name, '.');
if (dot_term)
*dot_term = '\0';
}
else
{
title->name = strdup(name);
}
}
if (!TitleOpenIfo(d, t))
{
goto fail;
}
/* ignore titles with bogus cell addresses so we don't abort later
** in libdvdread. */
for ( i = 0; i < d->ifo->vts_c_adt->nr_of_vobs; ++i)
{
if( (d->ifo->vts_c_adt->cell_adr_table[i].start_sector & 0xffffff ) ==
0xffffff )
{
hb_log( "scan: cell_adr_table[%d].start_sector invalid (0x%x) "
"- skipping title", i,
d->ifo->vts_c_adt->cell_adr_table[i].start_sector );
goto fail;
}
if( (d->ifo->vts_c_adt->cell_adr_table[i].last_sector & 0xffffff ) ==
0xffffff )
{
hb_log( "scan: cell_adr_table[%d].last_sector invalid (0x%x) "
"- skipping title", i,
d->ifo->vts_c_adt->cell_adr_table[i].last_sector );
goto fail;
}
}
if (global_verbosity_level == 4)
{
ifo_print( d->reader, d->vts );
}
/* Get duration */
title->duration = d->duration;
title->hours = title->duration / 90000 / 3600;
title->minutes = ((title->duration / 90000) % 3600) / 60;
title->seconds = ( title->duration / 90000) % 60;
hb_log( "scan: duration is %02d:%02d:%02d (%"PRId64" ms)",
title->hours, title->minutes, title->seconds,
title->duration / 90 );
/* ignore titles under 10 seconds because they're often stills or
* clips with no audio & our preview code doesn't currently handle
* either of these. */
if (title->duration < min_duration)
{
hb_log( "scan: ignoring title (too short)" );
goto fail;
}
/* Get pgc */
if (d->pgcn < 1 ||
d->pgcn > d->ifo->vts_pgcit->nr_of_pgci_srp ||
d->pgcn >= MAX_PGCN)
{
hb_log( "invalid PGC ID %d for title %d, skipping", d->pgcn, t );
goto fail;
}
// Check all pgc's for validity
uint32_t pgcn_map[MAX_PGCN/32];
pgcn = d->pgcn;
PgcWalkInit( pgcn_map );
do
{
pgc = d->ifo->vts_pgcit->pgci_srp[pgcn-1].pgc;
if (!pgc || !pgc->program_map)
{
hb_log( "scan: pgc not valid, skipping" );
goto fail;
}
if (pgc->cell_playback == NULL)
{
hb_log( "invalid PGC cell_playback table for title %d, skipping", t );
goto fail;
}
} while ((pgcn = NextPgcn(d->ifo, pgcn, pgcn_map)) != 0);
pgc = d->ifo->vts_pgcit->pgci_srp[d->pgcn-1].pgc;
hb_log("pgc_id: %d, pgn: %d: pgc: %p", d->pgcn, d->pgn, pgc);
if (d->pgn > pgc->nr_of_programs)
{
hb_log( "invalid PGN %d for title %d, skipping", d->pgn, t );
goto fail;
}
/* Detect languages */
for (i = 0; i < d->ifo->vtsi_mat->nr_of_vts_audio_streams; i++)
{
int audio_format, lang_code, lang_extension, audio_control, position, j;
hb_audio_t * audio, * audio_tmp;
iso639_lang_t * lang;
hb_log( "scan: checking audio %d", i + 1 );
audio = calloc( sizeof( hb_audio_t ), 1 );
audio_format = d->ifo->vtsi_mat->vts_audio_attr[i].audio_format;
lang_code = d->ifo->vtsi_mat->vts_audio_attr[i].lang_code;
lang_extension = d->ifo->vtsi_mat->vts_audio_attr[i].code_extension;
audio_control =
d->ifo->vts_pgcit->pgci_srp[d->pgcn-1].pgc->audio_control[i];
if (!(audio_control & 0x8000))
{
hb_log( "scan: audio channel is not active" );
free( audio );
continue;
}
position = ( audio_control & 0x7F00 ) >> 8;
switch( audio_format )
{
case 0x00:
audio->id = ( ( 0x80 + position ) << 8 ) | 0xbd;
audio->config.in.codec = HB_ACODEC_AC3;
audio->config.in.codec_param = AV_CODEC_ID_AC3;
codec_name = "AC3";
break;
case 0x02:
case 0x03:
audio->id = 0xc0 + position;
audio->config.in.codec = HB_ACODEC_FFMPEG;
audio->config.in.codec_param = AV_CODEC_ID_MP2;
codec_name = "MPEG";
break;
case 0x04:
audio->id = ( ( 0xa0 + position ) << 8 ) | 0xbd;
audio->config.in.codec = HB_ACODEC_LPCM;
codec_name = "LPCM";
break;
case 0x06:
audio->id = ( ( 0x88 + position ) << 8 ) | 0xbd;
audio->config.in.codec = HB_ACODEC_DCA;
audio->config.in.codec_param = AV_CODEC_ID_DTS;
codec_name = "DTS";
break;
default:
audio->id = 0;
audio->config.in.codec = 0;
codec_name = "Unknown";
hb_log( "scan: unknown audio codec (%x)",
audio_format );
break;
}
if( !audio->id )
{
continue;
}
const char * name = NULL;
switch ( lang_extension )
{
case 1:
audio->config.lang.attributes = HB_AUDIO_ATTR_NORMAL;
break;
case 2:
audio->config.lang.attributes = HB_AUDIO_ATTR_VISUALLY_IMPAIRED;
name = "Visually Impaired";
break;
case 3:
audio->config.lang.attributes = HB_AUDIO_ATTR_COMMENTARY;
name = "Commentary";
break;
case 4:
audio->config.lang.attributes = HB_AUDIO_ATTR_ALT_COMMENTARY;
name = "Commentary";
break;
default:
audio->config.lang.attributes = HB_AUDIO_ATTR_NONE;
break;
}
if (name != NULL)
{
audio->config.in.name = strdup(name);
}
/* Check for duplicate tracks */
audio_tmp = NULL;
for( j = 0; j < hb_list_count( title->list_audio ); j++ )
{
audio_tmp = hb_list_item( title->list_audio, j );
if( audio->id == audio_tmp->id )
{
break;
}
audio_tmp = NULL;
}
if( audio_tmp )
{
hb_log( "scan: duplicate audio track" );
free( audio );
continue;
}
lang = lang_for_code( lang_code );
snprintf( audio->config.lang.simple,
sizeof( audio->config.lang.simple ), "%s",
strlen( lang->native_name ) ? lang->native_name : lang->eng_name );
snprintf( audio->config.lang.iso639_2,
sizeof( audio->config.lang.iso639_2 ), "%s", lang->iso639_2 );
hb_log("scan: id=0x%x, lang=%s (%s), 3cc=%s ext=%i", audio->id,
audio->config.lang.simple, codec_name,
audio->config.lang.iso639_2, lang_extension);
audio->config.in.track = i;
audio->config.in.timebase.num = 1;
audio->config.in.timebase.den = 90000;
hb_list_add( title->list_audio, audio );
}
/* Check for subtitles */
for( i = 0; i < d->ifo->vtsi_mat->nr_of_vts_subp_streams; i++ )
{
int spu_control, pos, lang_ext = 0;
iso639_lang_t * lang;
hb_log( "scan: checking subtitle %d", i + 1 );
// spu_control
// 0x80000000 - Subtitle enabled
// 0x1f000000 - Position mask for 4:3 aspect subtitle track
// 0x001f0000 - Position mask for Wide Screen subtitle track
// 0x00001f00 - Position mask for Letterbox subtitle track
// 0x0000001f - Position mask for Pan&Scan subtitle track
spu_control =
d->ifo->vts_pgcit->pgci_srp[d->pgcn-1].pgc->subp_control[i];
if( !( spu_control & 0x80000000 ) )
{
hb_log( "scan: subtitle channel is not active" );
continue;
}
lang_ext = d->ifo->vtsi_mat->vts_subp_attr[i].code_extension;
lang = lang_for_code(d->ifo->vtsi_mat->vts_subp_attr[i].lang_code);
// display_aspect_ratio
// 0 = 4:3
// 3 = 16:9
// other = invalid
if (d->ifo->vtsi_mat->vts_video_attr.display_aspect_ratio)
{
// Add Wide Screen subtitle.
pos = (spu_control >> 16) & 0x1F;
add_subtitle(title->list_subtitle, pos, lang, lang_ext,
(uint8_t*)d->ifo->vts_pgcit->pgci_srp[d->pgcn-1].pgc->palette,
HB_VOBSUB_STYLE_WIDE);
// permitted_df
// 1 - Letterbox not permitted
// 2 - Pan&Scan not permitted
// 3 - Letterbox and Pan&Scan not permitted
if (!(d->ifo->vtsi_mat->vts_video_attr.permitted_df & 1))
{
// Letterbox permitted. Add Letterbox subtitle.
pos = (spu_control >> 8) & 0x1F;
add_subtitle(title->list_subtitle, pos, lang, lang_ext,
(uint8_t*)d->ifo->vts_pgcit->pgci_srp[d->pgcn-1].pgc->palette,
HB_VOBSUB_STYLE_LETTERBOX);
}
if (!(d->ifo->vtsi_mat->vts_video_attr.permitted_df & 2))
{
// Pan&Scan permitted. Add Pan&Scan subtitle.
pos = spu_control & 0x1F;
add_subtitle(title->list_subtitle, pos, lang, lang_ext,
(uint8_t*)d->ifo->vts_pgcit->pgci_srp[d->pgcn-1].pgc->palette,
HB_VOBSUB_STYLE_PANSCAN);
}
}
else
{
pos = (spu_control >> 24) & 0x1F;
add_subtitle(title->list_subtitle, pos, lang, lang_ext,
(uint8_t*)d->ifo->vts_pgcit->pgci_srp[d->pgcn-1].pgc->palette,
HB_VOBSUB_STYLE_4_3);
}
}
/* Chapters */
count = hb_list_count(d->list_dvd_chapter);
hb_log( "scan: title %d has %d chapters", t, count );
for (i = 0; i < count; i++)
{
char chapter_title[80];
dvd_chapter = hb_list_item(d->list_dvd_chapter, i);
chapter = calloc(sizeof( hb_chapter_t ), 1);
chapter->index = i + 1;
chapter->duration = dvd_chapter->duration;
sprintf(chapter_title, "Chapter %d", chapter->index);
hb_chapter_set_title(chapter, chapter_title);
hb_list_add( title->list_chapter, chapter );
int seconds = ( chapter->duration + 45000 ) / 90000;
chapter->hours = ( seconds / 3600 );
chapter->minutes = ( seconds % 3600 ) / 60;
chapter->seconds = ( seconds % 60 );
hb_log( "scan: chap %d, %"PRId64" ms",
chapter->index, chapter->duration / 90 );
}
/* Get aspect. We don't get width/height/rate infos here as
they tend to be wrong */
switch (d->ifo->vtsi_mat->vts_video_attr.display_aspect_ratio)
{
case 0:
title->container_dar.num = 4;
title->container_dar.den = 3;
break;
case 3:
title->container_dar.num = 16;
title->container_dar.den = 9;
break;
default:
hb_log( "scan: unknown aspect" );
goto fail;
}
switch (d->ifo->vtsi_mat->vts_video_attr.mpeg_version)
{
case 0:
title->video_codec = WORK_DECAVCODECV;
title->video_codec_param = AV_CODEC_ID_MPEG1VIDEO;
break;
case 1:
title->video_codec = WORK_DECAVCODECV;
title->video_codec_param = AV_CODEC_ID_MPEG2VIDEO;
break;
default:
hb_log("scan: unknown/reserved MPEG version %d",
d->ifo->vtsi_mat->vts_video_attr.mpeg_version);
title->video_codec = WORK_DECAVCODECV;
title->video_codec_param = AV_CODEC_ID_MPEG2VIDEO;
break;
}
hb_log("scan: aspect = %d:%d",
title->container_dar.num, title->container_dar.den);
/* This title is ok so far */
goto cleanup;
fail:
hb_title_close( &title );
cleanup:
TitleCloseIfo(d);
return title;
}
/***********************************************************************
* hb_dvdnav_title_scan
**********************************************************************/
static int find_title( hb_list_t * list_title, int title )
{
int ii;
for ( ii = 0; ii < hb_list_count( list_title ); ii++ )
{
hb_title_t * hbtitle = hb_list_item( list_title, ii );
if ( hbtitle->index == title )
return ii;
}
return -1;
}
static int skip_to_menu( dvdnav_t * dvdnav, int blocks )
{
int ii;
int result, event, len;
uint8_t buf[HB_DVD_READ_BUFFER_SIZE];
for ( ii = 0; ii < blocks; ii++ )
{
result = dvdnav_get_next_block( dvdnav, buf, &event, &len );
if ( result == DVDNAV_STATUS_ERR )
{
hb_error("dvdnav: Read Error, %s", dvdnav_err_to_string(dvdnav));
return 0;
}
switch ( event )
{
case DVDNAV_BLOCK_OK:
break;
case DVDNAV_CELL_CHANGE:
{
} break;
case DVDNAV_STILL_FRAME:
{
dvdnav_still_event_t *event;
event = (dvdnav_still_event_t*)buf;
dvdnav_still_skip( dvdnav );
if ( event->length == 255 )
{
// Infinite still. Can't be the main feature unless
// you like watching paint dry.
return 0;
}
} break;
case DVDNAV_WAIT:
dvdnav_wait_skip( dvdnav );
break;
case DVDNAV_STOP:
return 0;
case DVDNAV_HOP_CHANNEL:
break;
case DVDNAV_NAV_PACKET:
{
pci_t *pci = dvdnav_get_current_nav_pci( dvdnav );
if ( pci == NULL ) break;
int buttons = pci->hli.hl_gi.btn_ns;
int title, part;
result = dvdnav_current_title_info( dvdnav, &title, &part );
if (result != DVDNAV_STATUS_OK)
{
hb_log("dvdnav title info: %s", dvdnav_err_to_string(dvdnav));
}
else if ( title == 0 && buttons > 0 )
{
// Check button activation duration to see if this
// isn't another fake menu.
if ( pci->hli.hl_gi.btn_se_e_ptm - pci->hli.hl_gi.hli_s_ptm >
15 * 90000 )
{
// Found what appears to be a good menu.
return 1;
}
}
} break;
case DVDNAV_VTS_CHANGE:
{
dvdnav_vts_change_event_t *event;
event = (dvdnav_vts_change_event_t*)buf;
// Some discs initialize the vts with the "first play" item
// and some don't seem to. So if we see it is uninitialized,
// set it.
if ( event->new_vtsN <= 0 )
result = dvdnav_title_play( dvdnav, 1 );
} break;
case DVDNAV_HIGHLIGHT:
break;
case DVDNAV_AUDIO_STREAM_CHANGE:
break;
case DVDNAV_SPU_STREAM_CHANGE:
break;
case DVDNAV_SPU_CLUT_CHANGE:
break;
case DVDNAV_NOP:
break;
default:
break;
}
}
return 0;
}
static int try_button( dvdnav_t * dvdnav, int button, hb_list_t * list_title )
{
int result, event, len;
uint8_t buf[HB_DVD_READ_BUFFER_SIZE];
int ii, jj;
int32_t cur_title = 0, title, part;
uint64_t longest_duration = 0;
int longest = -1;
pci_t *pci = dvdnav_get_current_nav_pci( dvdnav );