-
Notifications
You must be signed in to change notification settings - Fork 8
/
dvd-vr.c
1675 lines (1496 loc) · 58.2 KB
/
dvd-vr.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
//vim:fileencoding=utf8
/*
dvd-vr.c Identify and optionally copy the individual programs
from a DVD-VR format disc
Copyright © 2007-2010 Pádraig Brady <[email protected]>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
*/
/*
Notes:
Individual recordings (programs) are extracted,
honouring any splits and/or deletes.
Merged programs are not handled yet though as
I would need to fully parse the higher level program set info.
Note the VOBs output from this program can be trivially
concatenated with the unix cat command for example
(note there will be timestamp jumps which may be problematic).
While extracting the DVD data, this program instructs the system
to not cache the data so that existing cached data is not affected.
We output the data from this program rather than just outputting offsets
for use with dd for example, because we may support disjoint VOBUs
(merged programs) in future. Also in future we may transform the NAV info
slightly in the VOBs? Anyway it gives us greater control over the system cache
as described above.
It might be useful to provide a FUSE module using this logic,
to present the logical structure of a DVD-VR, maybe even present as DVD-Video?
Doesn't parse play list index
Doesn't parse still image info
Doesn't parse chapters
Doesn't fixup MPEG time data
Requirements:
gcc >= 2.95
glibc >= 2.3.3 on linux
Tested on linux, CYGWIN and Mac OS X
*/
#define _GNU_SOURCE /* for posix_fadvise(), futimes() */
#define _FILE_OFFSET_BITS 64 /* for implicit large file support */
#include <inttypes.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include <unistd.h>
#include <fcntl.h>
#include <time.h>
#include <locale.h>
#include <getopt.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <sys/time.h>
#include <errno.h>
#include <limits.h>
#if !defined(MB_LEN_MAX) || MB_LEN_MAX<16
/* 1 char could be converted to 2 multibyte chars
* (for example combining accents), with each taking up to
* 6 bytes in UTF-8 for example */
# undef MB_LEN_MAX
# define MB_LEN_MAX 16
#endif
#define TYPE_SIGNED(t) (! ((t) 0 < (t) -1))
#define TYPE_WIDTH(t) (sizeof (t) * CHAR_BIT)
#define TYPE_MAX(t) \
((t) (! TYPE_SIGNED (t) \
? (t) -1 \
: ((((t) 1 << (TYPE_WIDTH (t) - 2)) - 1) * 2 + 1)))
#define OFF_T_MAX TYPE_MAX(off_t)
#define STREQ(x,y) (strcmp(x,y)==0)
#ifndef MAX
# define MAX(a, b) ((a) > (b) ? (a) : (b))
#endif
#ifndef MIN
# define MIN(a,b) (((a) < (b)) ? (a) : (b))
#endif
/* For a discussion of this macro see:
* http://www.pixelbeat.org/programming/gcc/static_assert.html */
#define ASSERT_CONCAT_(a, b) a##b
#define ASSERT_CONCAT(a, b) ASSERT_CONCAT_(a, b)
#define STATIC_ASSERT(e,m) enum { ASSERT_CONCAT(assert_line_, __LINE__) = 1/(!!(e)) }
#if defined(__CYGWIN__) || defined(_WIN32) /* windos doesn't like : in filenames */
#define TIMESTAMP_FMT "%F_%H-%M-%S"
#else
#define TIMESTAMP_FMT "%F_%T" /* keep : in filenames for backward compat */
#endif
const char* base_name = TIMESTAMP_FMT;
FILE* stdinfo; /* Where we write disc info */
#include <langinfo.h>
#ifdef HAVE_ICONV
#include <iconv.h>
#endif
const char* disc_charset;
const char* sys_charset;
/*********************************************************************************
* support routines
*********************************************************************************/
static size_t my_strnlen(const char* s, size_t n)
{
size_t len = 0;
while (n-- && *s++) len++;
return len;
}
/* Mac OS X doesn't provide strndup :( */
static char* my_strndup(const char *s, size_t n)
{
size_t len = my_strnlen(s, n);
char* ret = malloc(len+1);
if (ret) {
memcpy(ret, s, len);
ret[len] = '\0';
}
return ret;
}
#ifndef NDEBUG
void hexdump(const void* data, int len)
{
int i;
const unsigned char* bytes=data;
for (i=0; i<len; i++) {
printf("%02X ",bytes[i]);
if ((i+1)%16 == 0) printf("\n");
}
if (len%16) putchar('\n');
}
#endif//NDEBUG
typedef enum {
PERCENT_START,
PERCENT_UPDATE,
PERCENT_END
} percent_control_t;
/* Only use display_char!=0 to set non default progress chars like errors etc. */
static
void percent_display(percent_control_t percent_control, unsigned int percent, int display_char)
{
static int point;
#define POINTS 20
#define DEFAULT_PROGRESS_CHAR '.'
static char chars[POINTS+1];
switch (percent_control) {
case PERCENT_START: {
point=0;
fprintf(stderr, "[%*s]\r",POINTS,"");
memset(chars, ' ', POINTS);
*(chars+POINTS)='\0';
break;
}
case PERCENT_UPDATE: {
int newpoint=percent/(100/POINTS);
int i;
if (display_char && (display_char != DEFAULT_PROGRESS_CHAR))
for (i=point; i<=newpoint && i<POINTS; i++)
chars[i]=display_char;
for (i=0; i<newpoint; i++)
if (chars[i] == ' ')
chars[i] = DEFAULT_PROGRESS_CHAR;
fprintf(stderr, "\r[%s]",chars);
point=newpoint;
break;
}
case PERCENT_END: {
fprintf(stderr, "\r %*s \r",POINTS,"");
break;
}
}
fflush(stderr);
}
/* Set access and modfied times of filename
to the specified broken down time */
static int touch(const char* filename, struct tm* tm)
{
time_t ut = mktime(tm);
struct timeval tv[2]={ {.tv_sec=ut, .tv_usec=0}, {.tv_sec=ut, .tv_usec=0} };
return utimes(filename, tv);
}
typedef void (*process_func_t)(uint8_t* buf, unsigned int bs, void* context);
/*
Copy data between file descriptors while not
putting more than blocks*block_size in the system cache.
Therefore you will probably want to call this function repeatedly.
I tested 3 methods for streaming large amounts of data to/from disk.
All 3 took the same time as the bottleneck is the reading and writing to disk.
On x86 at least there is no significant difference between the AUTO and ALLOC_ALIGN
methods, the latter of which allocates the userspace buffer aligned on a page.
There was a noticeable reduction in CPU usage when MMAP_WRITE was used,
but the CPU usage is insignificant anyway due to the disc speeds we will
generally be dealing with. I also noticed that the MMAP method was more stable
giving consistent timings in all benchmark runs. However to ease portability worries
I use the AUTO method below, which will also allow us to modify the MPEG frames if
required. For reference the timings for extracting a 338 MiB VOB
from a VRO on the same hard disk were:
MMAP_WRITE
real 0m30.650s
user 0m0.007s
sys 0m1.130s
AUTO/ALLOC_ALIGN
real 0m31.776s
user 0m0.075s
sys 0m1.803s
*/
static int stream_data(int src_fd, int dst_fd, uint32_t blocks, uint16_t block_size,
process_func_t process_func, void* process_context)
{
#define AUTO
#define BLOCKS_PER_OP 1
#if defined AUTO
uint8_t buf[block_size*BLOCKS_PER_OP]; /* Not page aligned by default */
#elif defined ALLOC_ALIGN
/* There are portability issue with this.
* One may need to use MAP_ANONYMOUS rather than MAP_ANON.
* Also one may need to use MAP_FILE and operate on /dev/zero instead.
*
* Also see posix_memalign().
* Also see pagealign_alloc in gnulib.
*/
static int8_t* buf;
if (!buf) {
buf = mmap(NULL,block_size*BLOCKS_PER_OP,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_ANON,-1,0);
}
if (buf == MAP_FAILED) {
fprintf(stderr, "Error: Failed allocating mmap aligned buf [%s]\n", strerror(errno));
exit(EXIT_FAILURE);
}
if ((size_t)buf & (sysconf(_SC_PAGE_SIZE)-1)) {
fprintf(stderr, "Warning: mmap buffer not aligned\n");
}
#endif
unsigned int block;
for (block=0; block<blocks; block+=BLOCKS_PER_OP) {
int trans_blocks = MIN(blocks-block, BLOCKS_PER_OP);
int trans_size = trans_blocks * block_size;
int bytes_read = read(src_fd, buf, trans_size);
if (bytes_read != trans_size) {
#ifndef NDEBUG
if (bytes_read<0) /* otherwise file truncated */
fprintf(stderr, "Error reading from SRC [%s]\n", strerror(errno));
#endif //NDEBUG
return -1;
}
if (process_func) {
int pblock;
for (pblock=0; pblock<trans_blocks; pblock++) {
process_func(buf+(pblock*block_size), block_size, process_context);
}
}
if (write(dst_fd, buf, trans_size) != trans_size) {
fprintf(stderr, "Error writing to DST [%s]\n", strerror(errno));
return -2;
}
}
#ifdef POSIX_FADV_DONTNEED
/* Don't fill cache with SRC.
Note be careful to invalidate only what we've written
so that we don't dump any readahead cache. */
uint32_t bytes = blocks * block_size;
off_t offset = lseek(src_fd, 0, SEEK_CUR);
/* Note src is already guaranteed seekable, but offset may
* be 0 for example if /dev/zero is specified for testing. */
if (offset >= bytes) {
int ret = posix_fadvise(src_fd, offset-bytes, bytes, POSIX_FADV_DONTNEED);
if (ret) {
fprintf(stderr, "Warning: posix_fadvise failed [%s]\n", strerror(ret));
}
}
/* Don't fill cache with DST.
Note this slows the operation down by 20% when both source
and dest are on the same hard disk at least. I guess
this is due to implicit syncing in posix_fadvise()? */
offset = lseek(dst_fd, 0, SEEK_CUR);
if (offset != (off_t)-1) { /* seekable */
int ret = posix_fadvise(dst_fd, 0, 0, POSIX_FADV_DONTNEED);
if (ret) {
fprintf(stderr, "Warning: posix_fadvise failed [%s]\n", strerror(ret));
}
}
#endif //POSIX_FADV_DONTNEED
return 0;
}
#ifdef MMAP_WRITE
static int stream_data(int src_fd, int dst_fd, uint32_t blocks, uint16_t block_size)
{
int8_t* buf;
off_t offset = lseek(src_fd, 0, SEEK_CUR);
off_t pa_offset = offset & ~(sysconf(_SC_PAGE_SIZE) - 1); /* 4097 -> 4096 */
off_t offset_align = offset - pa_offset;;
buf = mmap(NULL, block_size*blocks+offset_align,
PROT_READ, MAP_PRIVATE, src_fd, pa_offset);
if (buf == MAP_FAILED) {
fprintf(stderr, "Error mmaping file [%s]\n", strerror(errno));
exit(EXIT_FAILURE);
}
#ifdef MADV_SEQUENTIAL
if (madvise(buf, block_size*blocks+offset_align, MADV_SEQUENTIAL)) {
fprintf(stderr, "Warning: madvise failed [%s]\n", strerror(errno));
}
#endif
if (write(dst_fd,buf+offset_align,blocks*block_size) != blocks*block_size) {
fprintf(stderr, "Error writing to DST [%s]\n", strerror(errno));
return -2;
}
offset = lseek(src_fd, blocks*block_size, SEEK_CUR); /* This won't seek head I presume */
if (offset == (off_t)-1) {
fprintf(stderr, "Error seeking in src [%s]\n", strerror(errno));
exit(EXIT_FAILURE);
}
#ifdef MADV_DONTNEED
if (madvise(buf, blocks*block_size, MADV_DONTNEED)) {
fprintf(stderr, "Warning: madvise failed [%s]\n", strerror(errno));
}
#endif
return 0;
}
#endif //MMAP_WRITE
static const char* get_charset(void)
{
const char* codeset = nl_langinfo(CODESET);
#ifdef __CYGWIN__
/* Cygwin 1.5 does not support locales and nl_langinfo (CODESET)
always returns "US-ASCII". This is fixed in v1.7 I think? */
if (codeset && STREQ(codeset, "US-ASCII")) {
/* parse LANG=ja_JP.SJIS -> SJIS */
const char* locale = getenv ("LANG");
if (locale && *locale) {
const char *dot = strchr (locale, '.');
if (dot) {
const char *modifier;
dot++;
if (!(modifier = strchr (dot, '@'))) {
return dot;
} else {
static char buf[32];
size_t len = modifier - dot;
if (len < sizeof (buf)) {
memcpy (buf, dot, len);
*(buf+len) = '\0';
return buf;
}
}
}
}
return "UTF-8";
}
#endif
return codeset;
}
static bool text_convert(const char *src, size_t srclen, char *dst, size_t dstlen)
{
bool ret=false;
#ifdef HAVE_ICONV
iconv_t cd = iconv_open (sys_charset, disc_charset);
if (cd != (iconv_t)-1) {
if (iconv (cd, (ICONV_CONST char**)&src, &srclen, &dst, &dstlen) != (size_t)-1) {
if (iconv (cd, NULL, NULL, &dst, &dstlen) != (size_t)-1) { /* terminate string */
ret=true;
}
} else {
fprintf(stderr, "Error converting text from %s to %s\n",
disc_charset, sys_charset);
}
iconv_close (cd);
} else {
fprintf(stderr, "Error converting text from %s to %s. Not supported\n",
disc_charset, sys_charset);
}
#else
/* avoid warnings (__attribute__ ((unused)) is too verbose/non standard) */
(void)src; (void)dst; (void)srclen; (void)dstlen;
fprintf(stderr, "Error converting text. libiconv missing\n");
#endif
return ret;
}
/*********************************************************************************
* Internal structures
*********************************************************************************/
typedef struct {
int aspect;
int width;
int height;
} p_video_attr_t;
p_video_attr_t* ifo_video_attrs;
typedef enum {
SCRAMBLED_UNSET=-1,
UNSCRAMBLED=0,
SCRAMBLED=1,
PARTIALLY_SCRAMBLED=2
} scrambled_t;
typedef struct {
int video_attr;
scrambled_t scrambled;
} p_program_attr_t;
p_program_attr_t* ifo_program_attrs;
/*********************************************************************************
* The DVD-VR structures
*********************************************************************************/
#undef PACKED
#if defined(__GNUC__)
# if __GNUC__ > 2 || (__GNUC__ == 2 && __GNUC_MINOR__ >= 95)
# define PACKED __attribute__ ((packed))
# endif
#endif
#if !defined(PACKED)
# error "Your compiler doesn't support __attribute__ ((packed))"
#endif
/* DVD structures are in network byte order (big endian) */
#include <netinet/in.h>
#undef NTOHS
#undef NTOHL
#define NTOHS(x) x=ntohs(x) /* 16 bit */
#define NTOHL(x) x=ntohl(x) /* 32 bit */
#define DVD_SECTOR_SIZE 2048
typedef struct {
struct {
/* Suffix numbers are decimal offsets */
/* 0 */
char id[12];
uint32_t vmg_ea; /* end address */
uint8_t zero_16[12];
uint32_t vmgi_ea; /* includes playlist info after this structure */
uint16_t version; /* specification version */
/* 34 */ /* Different from DVD-Video from here */
uint8_t zero_34[30];
uint8_t data_64[3];
uint8_t txt_encoding; /* as per VideoTextDataUsage.pdf */
uint8_t data_68[30];
/* 98 */
char disc_info1[64]; /* format name, or copy of disc_info2. */
char disc_info2[64]; /* format name, time or user label.. */
uint8_t zero_226[30];
/* 256 */
uint32_t pgit_sa; /* program info table start address */
uint32_t info_260_sa; /* ? start address */
uint8_t zero_264[3];
struct {
uint8_t supported; /* Encrypted Title Key Status */
uint8_t title_key[8];/* This needs to be decrypted using media key */
} cprm;
uint8_t zero_276[28];
/* 304 */
uint32_t def_psi_sa; /* default program set info start address */
uint32_t info_308_sa; /* ? start address */
uint32_t info_312_sa; /* user defined program set info start address? */
uint32_t info_316_sa; /* ? start address */
uint8_t zero_320[32];
uint32_t txt_attr_sa; /* extra attributes for programs (chan id etc.) */
uint32_t info_356_sa; /* ? start address */
uint8_t zero_360[152];
} PACKED mat;
} PACKED rtav_vmgi_t; /*Real Time AV (from DVD_RTAV dir)*/
STATIC_ASSERT(sizeof(rtav_vmgi_t) == 512,""); /* catch any miscounting above */
typedef struct {
uint8_t audio_attr[3];
} PACKED audio_attr_t;
typedef struct {
uint8_t pgtm[5];
} PACKED pgtm_t;
typedef struct {
uint32_t ptm;
uint16_t ptm_extra; /* extra to DSI pkts */
} PACKED ptm_t;
typedef struct {
uint16_t vob_attr;
pgtm_t vob_timestamp;
uint8_t data1;
uint8_t vob_format_id;
ptm_t vob_v_s_ptm;
ptm_t vob_v_e_ptm;
} PACKED vvob_t; /* Virtual VOB */
typedef struct {
uint8_t data[12];
} PACKED adj_vob_t;
typedef struct {
uint16_t nr_of_time_info;
uint16_t nr_of_vobu_info;
uint16_t time_offset;
uint32_t vob_offset;
} PACKED vobu_map_t;
typedef struct {
uint8_t data[7];
} PACKED time_info_t;
typedef struct {
uint8_t data1;
/* only 14 bits are used for size,
* but use full 16 for easy access. */
uint16_t vobu_size;
} PACKED vobu_info_t;
typedef struct {
uint16_t zero1;
uint8_t nr_of_pgi;
uint8_t nr_of_vob_formats;
uint32_t pgit_ea;
} PACKED pgiti_t; /* info for ProGram Info Table */
typedef struct {
uint16_t video_attr;
uint8_t nr_of_audio_streams;
uint8_t data1;
audio_attr_t audio_attr0;
audio_attr_t audio_attr1;
uint8_t data2[50];
} PACKED vob_format_t;
typedef struct {
uint16_t nr_of_programs;
} PACKED pgi_gi_t; /* global info for ProGram Info */
typedef struct {
uint8_t data1;
uint8_t nr_of_psi;
uint16_t nr_of_programs; /* Num programs on disc */
} PACKED psi_gi_t; /* global info for Program Set Info */
typedef struct {
uint8_t data1;
uint8_t data2;
uint16_t nr_of_programs; /* Num programs in program set */
char label[64]; /* ASCII. Might not be NUL terminated */
char title[64]; /* Could be same as label, NUL, or another charset */
uint16_t prog_set_id; /* On LG V1.1 discs this is program set ID */
uint16_t first_prog_id; /* ID of first program in this program set */
char data3[6];
} PACKED psi_t;
static const char* parse_txt_encoding(uint8_t txt_encoding)
{
/* from the VideoTextDataUsage.pdf available at dvdforum.org we have:
01h : ISO 646
10h : JIS Roman[14]*and JIS Kanji1990[168]*
11h : ISO 8859-1
12h : JIS Roman[14]*and JIS Katakana[13]*including Shift JIS Kanji
Also Nero generates discs with 00h, so I'll assume this is ASCII.
*/
const char* charset="Unknown";
switch (txt_encoding) {
case 0x00: charset="ASCII"; break;
case 0x01: charset="ISO646-JP"; break; /* ?? */
case 0x10: charset="JIS_C6220-1969-RO"; break; /* ?? */
case 0x11: charset="ISO_8859-1"; break;
case 0x12: charset="SHIFT_JIS"; break;
}
if (STREQ("Unknown", charset)) {
fprintf(stdinfo, "text encoding: %s", charset);
fprintf(stdinfo, ". (%02X). Please report this number and actual text encoding.\n", txt_encoding );
charset="ISO_8859-15"; /* Shouldn't give an error at least */
}
return charset;
}
static bool parse_audio_attr(audio_attr_t audio_attr0)
{
int coding = (audio_attr0.audio_attr[0] & 0xE0)>>5;
int channels = (audio_attr0.audio_attr[1] & 0x0F);
/* audio_attr0.audio_attr[2] = 7 for my camcorder. Is this 192Kbit? */
/* audio_attr0.audio_attr[2] = 9 for Masato Nunokawa's disc? */
if (channels < 8) {
fprintf(stdinfo, "audio_channs: %d\n",channels+1);
} else if (channels == 9) {
/* According to Masato Nunokawa's disc */
fprintf(stdinfo, "audio_channs: 2 (mono)\n");
} else {
return false;
}
const char* coding_name="Unknown";
switch (coding) {
case 0: coding_name="Dolby AC-3"; break;
case 2: coding_name="MPEG-1"; break;
case 3: coding_name="MPEG-2ext"; break;
case 4: coding_name="Linear PCM"; break;
}
fprintf(stdinfo, "audio_coding: %s",coding_name);
if (STREQ("Unknown", coding_name)) {
fprintf(stdinfo, ". (%d). Please report this number and actual audio encoding.\n", coding );
} else {
putc('\n', stdinfo);
}
return true;
}
static bool parse_video_attr(uint16_t video_attr, p_video_attr_t* p_video_attr)
{
int resolution = (video_attr & 0x0038) >> 3;
int aspect = (video_attr & 0x0C00) >> 10;
int tv_sys = (video_attr & 0x3000) >> 12;
int compression = (video_attr & 0xC000) >> 14;
p_video_attr->aspect = p_video_attr->width = p_video_attr->height = -1;
int vert_resolution = 0;
int horiz_resolution = 0;
const char* tv_system = "Unknown";
switch (tv_sys) {
case 0:
tv_system = "NTSC";
vert_resolution=480;
break;
case 1:
tv_system = "PAL";
vert_resolution=576;
break;
}
fprintf(stdinfo, "tv_system : %s", tv_system);
if (STREQ("Unknown", tv_system)) {
fprintf(stdinfo, ". (%d). Please report this number and actual TV system.\n", tv_sys );
} else {
putc('\n', stdinfo);
}
switch (resolution) {
case 0: horiz_resolution=720; break;
case 1: horiz_resolution=704; break;
case 2: horiz_resolution=352; break;
case 3: horiz_resolution=352; vert_resolution/=2; break;
case 4: horiz_resolution=544; break; /* this is a google inspired guess. */
case 5: horiz_resolution=480; break; /* from Aaron Binns' disc */
}
if (horiz_resolution && vert_resolution) {
fprintf(stdinfo, "resolution : %dx%d\n", horiz_resolution, vert_resolution);
p_video_attr->width = horiz_resolution;
p_video_attr->height = vert_resolution;
} else if (!horiz_resolution) {
fprintf(stdinfo, "resolution : Unknown (%d). Please report this number and actual resolution.\n", resolution );
}
const char* aspect_ratio = "Unknown";
switch (aspect) {
case 0: aspect_ratio="4:3"; break;
case 1: aspect_ratio="16:9"; break;
}
fprintf(stdinfo, "aspect_ratio: %s", aspect_ratio );
if (STREQ("Unknown", aspect_ratio)) {
fprintf(stdinfo, ". (%d). Please report this number and actual aspect ratio.\n", aspect );
} else {
putc('\n', stdinfo);
p_video_attr->aspect = aspect + 2; /* DVD-Video aspect encoding */
}
const char* mode = "Unknown";
switch (compression) {
case 0: mode="MPEG1"; break;
case 1: mode="MPEG2"; break;
}
fprintf(stdinfo, "video_format: %s", mode );
if (STREQ("Unknown", mode)) {
p_video_attr->aspect = -1; /* Don't adjust aspect later for unknown formats */
fprintf(stdinfo, ". (%d). Please report this number and actual compression format.\n", compression );
} else {
putc('\n', stdinfo);
}
return true;
}
static bool parse_pgtm(pgtm_t pgtm, struct tm* tm)
{
bool ret=false;
uint16_t year = ((pgtm.pgtm[0] ) <<8 | (pgtm.pgtm[1] )) >> 2;
uint8_t month = (pgtm.pgtm[1] & 0x03) <<2 | (pgtm.pgtm[2] >> 6);
uint8_t day = (pgtm.pgtm[2] & 0x3E) >>1;
uint8_t hour = (pgtm.pgtm[2] & 0x01) <<4 | (pgtm.pgtm[3] >> 4);
uint8_t min = (pgtm.pgtm[3] & 0x0F) <<2 | (pgtm.pgtm[4] >> 6);
uint8_t sec = (pgtm.pgtm[4] & 0x3F);
if (year) {
tm->tm_year=year-1900;
tm->tm_mon=month-1;
tm->tm_mday=day;
tm->tm_hour=hour;
tm->tm_min=min;
tm->tm_sec=sec;
tm->tm_isdst=-1; /*Auto calc DST offset.*/
char date_str[32];
strftime(date_str,sizeof(date_str),"%F %T",tm); //locale = %x %X
fprintf(stdinfo, "date : %s\n",date_str);
ret=true;
} else {
fprintf(stdinfo, "date : not set\n");
}
return ret;
}
#ifndef NDEBUG
/* This is basically a simplification of find_program_text_info() */
static void print_psi(psi_gi_t* psi_gi)
{
putc('\n', stdinfo);
int ps;
uint16_t program_count = 0;
for (ps=0; ps<psi_gi->nr_of_psi; ps++) {
psi_t *psi = (psi_t*)(((char*)(psi_gi+1)) + (ps * sizeof(psi_t)));
uint16_t first_prog_num = ntohs(psi->first_prog_id); /* assuming this is first to play? */
uint16_t start_prog_num = program_count+1;
uint16_t num_progs_in_set = ntohs(psi->nr_of_programs);
program_count += num_progs_in_set;
fprintf(stdinfo, "Programs in Program set %d:", ps+1);
int program_id;
for (program_id = start_prog_num;
program_id < start_prog_num+num_progs_in_set;
program_id++) {
const char* fmt = (program_id==first_prog_num ? " (%d)" : " %d");
fprintf(stdinfo, fmt, program_id);
}
putc('\n', stdinfo);
}
putc('\n', stdinfo);
}
#endif//NDEBUG
/*
* FIXME: This assumes the programs occur linearly within
* the default program sets. This has been accurate for all
* discs I've seen so far at least. Note I've noticed a
* couple of "SONY_MOBILE" discs with no labels at all.
*/
static psi_t* find_program_text_info(psi_gi_t* psi_gi, int program)
{
int ps;
uint16_t program_count = 0;
for (ps=0; ps<psi_gi->nr_of_psi; ps++) {
psi_t *psi = (psi_t*)(((char*)(psi_gi+1)) + (ps * sizeof(psi_t)));
uint16_t start_prog_num;
/*
start_prog_num = ntohs(psi->first_prog_id);
We need to maintain program count as first_prog_id is often not stored,
as is the case for LG and "CIRRUS LOGIC" V1.1 discs for example (it's 0 or 0xFFFF).
Also I noticed a Sony disc that had programs sets with 2 programs in them, which
sometimes set the first_prog_id to the second program in the set. Perhaps
this field identifies the prog to start playing, as the first program in those
sets was a single VOBU that was generated due to a split.
Perhaps I should name the programs label.ps_id(001) when > 1 ps. */
start_prog_num = program_count+1;
uint16_t num_progs_in_set = ntohs(psi->nr_of_programs);
/* TODO: Perhaps have an option to merge all programs
in a program set to a vob using this info. That would assume
though that the programs were adjacent. */
program_count += num_progs_in_set;
uint16_t end_prog_num = start_prog_num + num_progs_in_set - 1;
if ((program >= start_prog_num) && (program <= end_prog_num)) {
return psi;
}
}
return (psi_t*)NULL;
}
/*
* This function controls the storage used by the actual
* encoding conversion routines. Note a len must be passed
* since the text fields are sometimes not NUL terminated.
*
* A string in the local encoding is returned which must be free()
*/
static char* text_field_convert(const char* field, unsigned int len)
{
unsigned int conv_max_len=len*MB_LEN_MAX+1/*NUL*/;
char* field_local=malloc(conv_max_len);
if (!field_local) {
fprintf(stderr, "Error allocating space for text conversion\n");
return NULL;
}
if (*field) {
char field_copy[len+1]; /* Copy as may not be NUL terminated */
field_copy[len] = '\0';
(void) strncpy(field_copy, field, len);
size_t srclen = strlen(field_copy) + 1; /* convert NUL also */
if (!text_convert(field_copy, srclen, field_local, conv_max_len)) {
free(field_local);
field_local=NULL;
}
} else {
*field_local='\0';
}
return field_local;
}
/* Filter redundant info */
static bool disc_info_redundant(const char* info)
{
const char* info_exclude_list[] = {
"DVD VR",
"DVD-VR",
" ",
"" /* must be last */
};
const char** info_to_exclude = info_exclude_list;
while (**info_to_exclude) {
if (STREQ(info, *info_to_exclude)) {
return true;
}
info_to_exclude++;
}
return false;
}
static void print_disc_info(rtav_vmgi_t* rtav_vmgi_ptr)
{
char* txt_local;
txt_local = text_field_convert(rtav_vmgi_ptr->mat.disc_info2,
sizeof(rtav_vmgi_ptr->mat.disc_info2));
if (txt_local && *txt_local && !disc_info_redundant(txt_local)) {
fprintf(stdinfo, "info : %s\n", txt_local);
}
free(txt_local);
if (strncmp(rtav_vmgi_ptr->mat.disc_info1,
rtav_vmgi_ptr->mat.disc_info2,
sizeof(rtav_vmgi_ptr->mat.disc_info1))) {
/* If there is a unique disc_info1 here, then there is
* no disc_info2 above on the discs I've seen so far */
txt_local = text_field_convert(rtav_vmgi_ptr->mat.disc_info1,
sizeof(rtav_vmgi_ptr->mat.disc_info1));
if (txt_local && *txt_local && !disc_info_redundant(txt_local)) {
fprintf(stdinfo, "info : %s\n", txt_local);
}
free(txt_local);
}
}
static char* mb_clean_name(const char* src)
{
size_t src_size = strlen (src) + 1;
wchar_t *str_wc = NULL;
size_t src_chars = mbstowcs (NULL, src, 0);
if (src_chars == (size_t) -1)
return NULL;
src_chars += 1; /* make space for NUL */
str_wc = malloc (src_chars * sizeof (wchar_t));
if (str_wc == NULL)
return NULL;
if (mbstowcs (str_wc, src, src_chars) <= 0) {
free(str_wc);
return NULL;
}
str_wc[src_chars - 1] = L'\0';
wchar_t* wc = str_wc;
while (*wc) {
size_t good = wcscspn(wc, L" /:?\\");
if (good)
wc+=good;
else
*wc++=L'-';
}
char* newstr = malloc (src_size);
if (newstr == NULL) {
free(str_wc);
return NULL;
}
(void) wcstombs(newstr, str_wc, src_size);
free(str_wc);
return newstr;
}
/* Must pass a string on the heap which may be modified inplace,
* or may be reallocated. */
static char* clean_name(char* src, bool mb_src)
{
if (mb_src && (MB_CUR_MAX > 1)) {
char* cleaned = mb_clean_name(src);
free(src);
return cleaned;
}
char* c = src;
while (*c) {
size_t good = strcspn(c, " /:?\\");
if (good)
c+=good;
else
*c++='-';
}
return src;
}
static char* get_label_base(const psi_t* psi)
{
char* title_local = text_field_convert(psi->title, sizeof(psi->title));
if (title_local && *title_local &&
strncmp(title_local, psi->label, sizeof(psi->label))) { /* if title != label */
title_local = clean_name(title_local, true);
if (!title_local) {
fprintf(stderr, "Error generating file name from title\n");
return NULL;
} else {
return title_local;
}
}
free(title_local);
const char* label=psi->label; /* ASCII */
if (*label && !STREQ(label, " ")) {
char* label_local = my_strndup(label, sizeof(psi->label));
label_local = clean_name(label_local, false);
return label_local;
}
return NULL;
}
static void print_label(const psi_t* psi)
{
const char* label=psi->label; /* ASCII */
char* title_local = text_field_convert(psi->title, sizeof(psi->title));
if (title_local && *title_local &&
strncmp(title_local, label, sizeof(psi->label))) { /* if title != label */
fprintf(stdinfo, "title: %s\n", title_local);
}
free(title_local);
if (*label && !STREQ(label, " ")) {
fprintf(stdinfo, "label: %.*s\n", (int)sizeof(psi->label), label);