File: info.c

package info (click to toggle)
vcdimager 0.7.24+dfsg-0.2
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, stretch
  • size: 3,956 kB
  • ctags: 2,102
  • sloc: ansic: 18,154; sh: 10,560; xml: 317; makefile: 298; cpp: 58
file content (2201 lines) | stat: -rw-r--r-- 59,145 bytes parent folder | download | duplicates (2)
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
/*
    $Id: info.c,v 1.38 2005/08/03 08:15:37 rocky Exp $

    Copyright (C) 2002, 2003, 2004, 2005 Rocky Bernstein <rocky@panix.com>

    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 Foundation
    Software, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/
/* 
   Things here refer to higher-level structures usually accessed via
   vcdinfo_t. For lower-level access which generally use 
   structures other than vcdinfo_t, see inf.c
*/


/* Private headers */
#include "info_private.h"
#include "vcd_assert.h"
#include "pbc.h"
#include "util.h"
#include "vcd_read.h"

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_STAT_H
#include <sys/stat.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <cdio/cdio.h>
#include <cdio/bytesex.h>
#include <cdio/cd_types.h>
#include <cdio/util.h>

/* Eventually move above libvcd includes but having vcdinfo including. */
#include <libvcd/info.h>

#include <stdio.h>
#include <stddef.h>
#include <errno.h>

static const char _rcsid[] = "$Id: info.c,v 1.38 2005/08/03 08:15:37 rocky Exp $";

#define BUF_COUNT 16
#define BUF_SIZE 80

/* Return a pointer to a internal free buffer */
static char *
_getbuf (void)
{
  static char _buf[BUF_COUNT][BUF_SIZE];
  static int _num = -1;
  
  _num++;
  _num %= BUF_COUNT;

  memset (_buf[_num], 0, BUF_SIZE);

  return _buf[_num];
}

/* 
   Initialize/allocate segment portion of vcdinfo_obj_t. 

   Getting exact segments sizes is done in a rather complicated way. 
   A simple approach would be to use the fixed size allocated on disk, 
   but players have trouble with the internal fragmentation padding. 
   More accurate results are obtained by consulting with ISO 9660 
   information for the corresponding file entry.

   Another approach to get segment sizes is to read/scan the
   MPEGs. That would be rather slow.
*/
static void
_init_segments (vcdinfo_obj_t *p_obj)
{
  InfoVcd_t *info = vcdinfo_get_infoVcd(p_obj);
  segnum_t num_segments = vcdinfo_get_num_segments(p_obj);
  CdioListNode_t *entnode;
  CdioList_t *entlist;
  int i;
  lsn_t last_lsn=0;
  
  p_obj->first_segment_lsn = cdio_msf_to_lsn(&info->first_seg_addr);
  p_obj->seg_sizes         = calloc(1, num_segments * sizeof(uint32_t *));

  if (NULL == p_obj->seg_sizes || 0 == num_segments) return;

  entlist = iso9660_fs_readdir(p_obj->img, "SEGMENT", true);

  i=0;
  _CDIO_LIST_FOREACH (entnode, entlist) {
    iso9660_stat_t *statbuf = _cdio_list_node_data (entnode);

    if (statbuf->type == _STAT_DIR) continue;

    while(info->spi_contents[i].item_cont) {
      p_obj->seg_sizes[i] = VCDINFO_SEGMENT_SECTOR_SIZE;
      i++;
    }
    
    /* Should have an entry in the ISO 9660 filesystem. Get and save 
       in statbuf.secsize this size.
    */
    p_obj->seg_sizes[i] = statbuf->secsize;

    if (last_lsn >= statbuf->lsn) 
      vcd_warn ("Segments if ISO 9660 directory out of order lsn %ul >= %ul", 
                (unsigned int) last_lsn, (unsigned int) statbuf->lsn);
    last_lsn = statbuf->lsn;

    i++;
  }

  while(i < num_segments && info->spi_contents[i].item_cont) {
    p_obj->seg_sizes[i] = VCDINFO_SEGMENT_SECTOR_SIZE;
    i++;
  }

  if (i != num_segments) 
    vcd_warn ("Number of segments found %d is not number of segments %d", 
              i, num_segments);

  _cdio_list_free (entlist, true);

  
#if 0
  /* Figure all of the segment sector sizes */
  for (i=0; i < num_segments; i++) {
    
    p_obj->seg_sizes[i] = VCDINFO_SEGMENT_SECTOR_SIZE;

    if ( !info->spi_contents[i].item_cont ) {
      /* Should have an entry in the ISO 9660 filesystem. Get and save 
         in statbuf.secsize this size.
       */
      lsn_t lsn = vcdinfo_get_seg_lsn(p_obj, i);
      iso9660_stat_t *statbuf =iso9660_find_fs_lsn(p_obj->img, lsn);
      if (NULL != statbuf) {
        p_obj->seg_sizes[i] = statbuf->secsize;
        free(statbuf);
      } else {
        vcd_warn ("Trouble finding ISO 9660 size for segment %d.", i);
      }
    }
  }
#endif

}

/*!
   Return the number of audio channels implied by "audio_type".
   0 is returned on error.
*/
unsigned int
vcdinfo_audio_type_num_channels(const vcdinfo_obj_t *p_obj, 
                                 unsigned int audio_type) 
{
  const int audio_types[2][5] =
    {
      { /* VCD 2.0 */
        0,   /* no audio*/
        1,   /* single channel */
        1,   /* stereo */
        2,   /* dual channel */
        0},  /* error */

      { /* SVCD, HQVCD */
        0,   /* no stream */ 
        1,   /*  1 stream */ 
        2,   /*  2 streams */
        1,   /*  1 multi-channel stream (surround sound) */
        0}   /*  error */
    };

  /* We should also check that the second index is in range too. */
  if (audio_type > 4) {
    return 0;
  }
  
  /* Get first index entry into above audio_type array from vcd_type */
  switch (p_obj->vcd_type) {

  case VCD_TYPE_VCD:
  case VCD_TYPE_VCD11:
    return 1;

  case VCD_TYPE_VCD2:
    return 3;
    break;

  case VCD_TYPE_HQVCD:
  case VCD_TYPE_SVCD:
    return audio_types[1][audio_type];
    break;

  case VCD_TYPE_INVALID:
  default:
    /* We have an invalid entry. Set to handle below. */
    return 0;
  }
}

/*!
  Return a string describing an audio type.
*/
const char * 
vcdinfo_audio_type2str(const vcdinfo_obj_t *p_obj, unsigned int audio_type)
{
  const char *audio_types[3][5] =
    {
      /* INVALID, VCD 1.0, or VCD 1.1 */
      { "unknown", "invalid", "", "", "" },  
      
      /*VCD 2.0 */
      { "no audio", "single channel", "stereo", "dual channel", "error" }, 
      
      /* SVCD, HQVCD */
      { "no stream", "1 stream", "2 streams", 
        "1 multi-channel stream (surround sound)", "error"}, 
    };

  unsigned int first_index = 0; 

  /* Get first index entry into above audio_type array from vcd_type */
  switch (p_obj->vcd_type) {

  case VCD_TYPE_VCD:
  case VCD_TYPE_VCD11:
  case VCD_TYPE_VCD2:
    first_index=1;
    break;

  case VCD_TYPE_HQVCD:
  case VCD_TYPE_SVCD:
    first_index=2;
    break;

  case VCD_TYPE_INVALID:
  default:
    /* We have an invalid entry. Set to handle below. */
    audio_type=4;
  }
  
  /* We should also check that the second index is in range too. */
  if (audio_type > 3) {
    first_index=0;
    audio_type=1;
  }
  
  return audio_types[first_index][audio_type];
}

/*!
  Note first i_seg is 0!
*/
const char * 
vcdinfo_ogt2str(const vcdinfo_obj_t *p_obj, segnum_t i_seg)
{
  const InfoVcd_t *info = &(p_obj->info);
  const char *ogt_str[] =
    {
      "None",
      "1 available",
      "0 & 1 available",
      "all 4 available"
    };
  
  return ogt_str[info->spi_contents[i_seg].ogt];
}


const char * 
vcdinfo_video_type2str(const vcdinfo_obj_t *p_obj, segnum_t i_seg)
{
  const char *video_types[] =
    {
      "no stream",
      "NTSC still",
      "NTSC still (lo+hires)",
      "NTSC motion",
      "reserved (0x4)",
      "PAL still",
      "PAL still (lo+hires)",
      "PAL motion"
      "INVALID ENTRY"
    };
  
  return video_types[vcdinfo_get_video_type(p_obj, i_seg)];
}

/*!
  \brief Classify i_itemid into the kind of item it is: track #, entry #, 
  segment #. 
  \param itemid is set to contain this classification an the converted 
  entry number. 
*/
void
vcdinfo_classify_itemid (uint16_t i_itemid, 
                         /*out*/ vcdinfo_itemid_t *itemid)
{

  itemid->num = i_itemid;
  if (i_itemid < 2) 
    itemid->type = VCDINFO_ITEM_TYPE_NOTFOUND;
  else if (i_itemid < MIN_ENCODED_TRACK_NUM) {
    itemid->type = VCDINFO_ITEM_TYPE_TRACK;
    itemid->num--;
  } else if (i_itemid < 600) {
    itemid->type = VCDINFO_ITEM_TYPE_ENTRY;
    itemid->num -= MIN_ENCODED_TRACK_NUM;
  } else if (i_itemid < MIN_ENCODED_SEGMENT_NUM)
    itemid->type = VCDINFO_ITEM_TYPE_LID;
  else if (i_itemid <= MAX_ENCODED_SEGMENT_NUM) {
    itemid->type = VCDINFO_ITEM_TYPE_SEGMENT;
    itemid->num -= (MIN_ENCODED_SEGMENT_NUM);
  } else 
    itemid->type = VCDINFO_ITEM_TYPE_SPAREID2;
}

const char *
vcdinfo_pin2str (uint16_t i_itemid)
{
  char *buf = _getbuf ();
  vcdinfo_itemid_t itemid;
  
  vcdinfo_classify_itemid(i_itemid, &itemid);
  strcpy (buf, "??");

  switch(itemid.type) {
  case VCDINFO_ITEM_TYPE_NOTFOUND:
    snprintf (buf, BUF_SIZE, "play nothing (0x%4.4x)", itemid.num);
    break;
  case VCDINFO_ITEM_TYPE_TRACK:
    snprintf (buf, BUF_SIZE, "SEQUENCE[%d] (0x%4.4x)", itemid.num-1, 
              i_itemid);
    break;
  case VCDINFO_ITEM_TYPE_ENTRY:
    snprintf (buf, BUF_SIZE, "ENTRY[%d] (0x%4.4x)", itemid.num, i_itemid);
    break;
  case VCDINFO_ITEM_TYPE_SEGMENT:
    snprintf (buf, BUF_SIZE, "SEGMENT[%d] (0x%4.4x)", itemid.num, i_itemid);
    break;
  case VCDINFO_ITEM_TYPE_LID:
    snprintf (buf, BUF_SIZE, "spare id (0x%4.4x)", itemid.num);
    break;
  case VCDINFO_ITEM_TYPE_SPAREID2:
    snprintf (buf, BUF_SIZE, "spare id2 (0x%4.4x)", itemid.num);
    break;
  }

  return buf;
}

/*!
   Return a string containing the VCD album id, or NULL if there is 
   some problem in getting this. 
*/
const char *
vcdinfo_get_album_id(const vcdinfo_obj_t *p_obj)
{
  if ( !p_obj ) return NULL;
  return vcdinf_get_album_id( &(p_obj->info) );
}

/*!
  Return the VCD ID.
  NULL is returned if there is some problem in getting this. 
*/
char *
vcdinfo_get_application_id(vcdinfo_obj_t *p_obj)
{
  if ( !p_obj ) return NULL;
  return iso9660_get_application_id( &(p_obj->pvd) );
}

/*!
  Return a pointer to the cdio structure for the CD image opened or
  NULL if error.
*/
CdIo_t *
vcdinfo_get_cd_image (const vcdinfo_obj_t *p_vcd_obj) 
{
  if ( !p_vcd_obj ) return NULL;
  return p_vcd_obj->img;
}


/*!
  \fn vcdinfo_selection_get_lid(const vcdinfo_obj_t *p_obj, lid_t lid,
                               unsigned int selection);

  \brief Get offset of a selection for a given lid. 

  Return the LID offset associated with a the selection number of the
  passed-in LID parameter. 

  \return VCDINFO_INVALID_LID is returned if obj on error or obj
  is NULL. Otherwise the LID offset is returned.
*/
lid_t vcdinfo_selection_get_lid(const vcdinfo_obj_t *p_obj, lid_t lid,
                                unsigned int selection) 
{
  unsigned int offset;

  if (!p_obj) return VCDINFO_INVALID_LID;

  offset = vcdinfo_selection_get_offset(p_obj, lid, selection);
  switch (offset) {
  case VCDINFO_INVALID_OFFSET:
  case PSD_OFS_MULTI_DEF:
  case PSD_OFS_MULTI_DEF_NO_NUM:
    return VCDINFO_INVALID_LID;
  default: 
    {
      vcdinfo_offset_t *ofs = vcdinfo_get_offset_t(p_obj, offset);
      return ofs->lid;
    }
  }
}

/*!
  \fn vcdinfo_selection_get_offset(const vcdinfo_obj_t *p_obj, lid_t lid,
  unsigned int selection);

  \brief Get offset of a selection for a given lid. 

  Return the LID offset associated with a the selection number of the
  passed-in LID parameter. 

  \return VCDINFO_INVALID_OFFSET is returned if error, obj is NULL or
  the lid is not some type of selection list. Otherwise the LID offset
  is returned.
*/
uint16_t vcdinfo_selection_get_offset(const vcdinfo_obj_t *p_obj, lid_t lid,
                                      unsigned int selection) 
{
  unsigned int bsn;

  PsdListDescriptor_t pxd;
  vcdinfo_lid_get_pxd(p_obj, &pxd, lid);
  if (pxd.descriptor_type != PSD_TYPE_SELECTION_LIST &&
      pxd.descriptor_type != PSD_TYPE_EXT_SELECTION_LIST) {
    vcd_warn( "Requesting selection of LID %i which not a selection list -"
              " type is 0x%x", 
              lid, pxd.descriptor_type );
    return VCDINFO_INVALID_OFFSET;
  }
  
  bsn=vcdinf_get_bsn(pxd.psd);

  if ( (selection - bsn + 1) > 0) {
    return vcdinfo_lid_get_offset(p_obj, lid, selection-bsn+1);
  } else {
    vcd_warn( "Selection number %u too small. bsn %u", selection, bsn );
    return VCDINFO_INVALID_OFFSET;
  }
}

/**
 \fn vcdinfo_get_default_offset(const vcdinfo_obj_t *p_obj, unsinged int lid);
 \brief Get return offset for a given PLD selector descriptor. 
 \return  VCDINFO_INVALID_OFFSET is returned on error or if pld has no 
 "return" entry or pld is NULL. Otherwise the LID offset is returned.
 */
uint16_t
vcdinfo_get_default_offset(const vcdinfo_obj_t *p_obj, lid_t lid)
{
  if (p_obj) {
    
    PsdListDescriptor_t pxd;

    vcdinfo_lid_get_pxd(p_obj, &pxd, lid);
    
    switch (pxd.descriptor_type) {
    case PSD_TYPE_EXT_SELECTION_LIST:
    case PSD_TYPE_SELECTION_LIST:
      return vcdinf_psd_get_default_offset(pxd.psd);
      break;
    case PSD_TYPE_PLAY_LIST:
    case PSD_TYPE_END_LIST:
    case PSD_TYPE_COMMAND_LIST:
      break;
    }
  }
  return VCDINFO_INVALID_OFFSET;
}

/*!
  \brief Get default or multi-default LID. 
  
  Return the LID offset associated with a the "default" entry of the
  passed-in LID parameter. Note "default" entries are associated
  with PSDs that are (extended) selection lists. If the "default"
  offset is a multi-default, we use entry_num to find the proper
  "default" LID. Otherwise this routine is exactly like
  vcdinfo_get_default_lid with the exception of requiring an
  additional "entry_num" parameter.
  
  \return VCDINFO_INVALID_LID is returned on error, or if the LID
  is not a selection list or no "default" entry. Otherwise the LID
  offset is returned.
*/
lid_t
vcdinfo_get_multi_default_lid(const vcdinfo_obj_t *p_obj, lid_t lid, 
                              lsn_t lsn)
{
  unsigned int offset;
  unsigned int entry_num;

  entry_num = vcdinfo_lsn_get_entry(p_obj, lsn);
  offset    = vcdinfo_get_multi_default_offset(p_obj, lid, entry_num);

  switch (offset) {
  case VCDINFO_INVALID_OFFSET:
  case PSD_OFS_MULTI_DEF:
  case PSD_OFS_MULTI_DEF_NO_NUM:
    return VCDINFO_INVALID_LID;
  default: 
    {
      vcdinfo_offset_t *ofs = vcdinfo_get_offset_t(p_obj, offset);
      return ofs->lid;
    }
  }
}

/*!
  \brief Get default or multi-default LID offset. 
  
  Return the LID offset associated with a the "default" entry of the
  passed-in LID parameter. Note "default" entries are associated
  with PSDs that are (extended) selection lists. If the "default"
  offset is a multi-default, we use entry_num to find the proper
  "default" offset. Otherwise this routine is exactly like
  vcdinfo_get_default_offset with the exception of requiring an
  additional "entry_num" parameter.
  
  \return VCDINFO_INVALID_OFFSET is returned on error, or if the LID
  is not a selection list or no "default" entry. Otherwise the LID
  offset is returned.
*/
uint16_t
vcdinfo_get_multi_default_offset(const vcdinfo_obj_t *p_obj, lid_t lid, 
                                 unsigned int entry_num)
{
  uint16_t offset=vcdinfo_get_default_offset(p_obj, lid);

  switch (offset) {
  case PSD_OFS_MULTI_DEF:
  case PSD_OFS_MULTI_DEF_NO_NUM: 
    {
      /* Have some work todo... Figure the selection number. */
      PsdListDescriptor_t pxd;
      
      vcdinfo_lid_get_pxd(p_obj, &pxd, lid);

      switch (pxd.descriptor_type) {
        
      case PSD_TYPE_SELECTION_LIST:
      case PSD_TYPE_EXT_SELECTION_LIST: {
        vcdinfo_itemid_t selection_itemid;
        uint16_t selection_itemid_num;
        unsigned int start_entry_num;

        if (pxd.psd == NULL) return VCDINFO_INVALID_OFFSET;
        selection_itemid_num  = vcdinf_psd_get_itemid(pxd.psd);
        vcdinfo_classify_itemid(selection_itemid_num, &selection_itemid);
        if (selection_itemid.type != VCDINFO_ITEM_TYPE_TRACK) {
          return VCDINFO_INVALID_OFFSET;
        }

        start_entry_num = vcdinfo_track_get_entry(p_obj, 
                                                  selection_itemid.num);
        return vcdinfo_selection_get_offset(p_obj, lid, 
                                            entry_num-start_entry_num);
      }
      default: ;
      }
    }
  default:
    return VCDINFO_INVALID_OFFSET;
  }
}

/*!
  Return a string containing the default VCD device if none is specified.
  Return NULL we can't get this information.
*/
char *
vcdinfo_get_default_device (const vcdinfo_obj_t *p_vcd_obj)
{

  /* If device not already open, then we'll open it temporarily and
     let CdIo select a driver, get the default for that and then
     close/destroy the temporary we created.
   */
  CdIo_t *p_cdio=NULL;
  if (p_vcd_obj && p_vcd_obj->img)
    p_cdio = p_vcd_obj->img;

  return cdio_get_default_device(p_cdio);
}

/*!
  Return number of sector units in of an entry. 0 is returned if entry_num
  is out of range.
  The first entry number is 0.
*/
uint32_t
vcdinfo_get_entry_sect_count (const vcdinfo_obj_t *p_obj, 
                              unsigned int entry_num)
{
  const EntriesVcd_t *entries = &(p_obj->entries);
  const unsigned int entry_count = vcdinf_get_num_entries(entries);
  if (entry_num > entry_count) 
    return 0;
  else {
    const lsn_t this_lsn = vcdinfo_get_entry_lsn(p_obj, entry_num);
    lsn_t next_lsn;
    if (entry_num < entry_count-1) {
      track_t track=vcdinfo_get_track(p_obj, entry_num);
      track_t next_track=vcdinfo_get_track(p_obj, entry_num+1);
      next_lsn = vcdinfo_get_entry_lsn(p_obj, entry_num+1);
      /* If we've changed tracks, don't include pregap sector between 
         tracks.
       */
      if (track != next_track) next_lsn -= CDIO_PREGAP_SECTORS;
    } else {
      /* entry_num == entry_count -1. Or the last entry.
         This is really really ugly. There's probably a better
         way to do it. 
         Below we get the track of the current entry and then the LBA of the
         beginning of the following (leadout?) track.

         Wait! It's uglier than that! Since VCD's can be created
         *without* a pregap to the leadout track, we try not to use
         that if we can get the entry from the ISO 9660 filesystem.
      */
      track_t track = vcdinfo_get_track(p_obj, entry_num);
      if (track != VCDINFO_INVALID_TRACK) {
        iso9660_stat_t *statbuf;
        const lsn_t lsn = vcdinfo_get_track_lsn(p_obj, track);

        /* Try to get the sector count from the ISO 9660 filesystem */
        statbuf = iso9660_find_fs_lsn(p_obj->img, lsn);
        
        if (NULL != statbuf) {
          next_lsn = lsn + statbuf->secsize;
          free(statbuf);
        } else {
          /* Failed on ISO 9660 filesystem. Use next track or
             LEADOUT track.  */
          next_lsn = vcdinfo_get_track_lsn(p_obj, track+1);
        }
        if (next_lsn == VCDINFO_NULL_LSN)
          return 0;
      } else {
        /* Something went wrong. Set up size to zero. */
        return 0;
      }
    }
    return (next_lsn - this_lsn);
  }
}

/*!  Return the starting MSF (minutes/secs/frames) for sequence
  entry_num in obj.  NULL is returned if there is no entry.
  The first entry number is 0.
*/
const msf_t *
vcdinfo_get_entry_msf(const vcdinfo_obj_t *p_obj, unsigned int entry_num)
{
  const EntriesVcd_t *entries = &(p_obj->entries);
  return vcdinf_get_entry_msf(entries, entry_num);
}

/*!  Return the starting LBA (logical block address) for sequence
  entry_num in obj.  VCDINFO_NULL_LBA is returned if there is no entry.
*/
lba_t
vcdinfo_get_entry_lba(const vcdinfo_obj_t *p_obj, unsigned int entry_num)
{
  if ( !p_obj ) return VCDINFO_NULL_LBA;
  else {
    const msf_t *msf = vcdinfo_get_entry_msf(p_obj, entry_num);
    msf = vcdinfo_get_entry_msf(p_obj, entry_num);
    return (msf != NULL) ? cdio_msf_to_lba(msf) : VCDINFO_NULL_LBA;
  }
}

/*!  Return the starting LBA (logical block address) for sequence
  entry_num in obj.  VCDINFO_NULL_LBA is returned if there is no entry.
*/
lsn_t
vcdinfo_get_entry_lsn(const vcdinfo_obj_t *p_obj, unsigned int entry_num)
{
  if ( !p_obj ) return VCDINFO_NULL_LBA;
  else {
    const msf_t *msf = vcdinfo_get_entry_msf(p_obj, entry_num);
    return (msf != NULL) ? cdio_msf_to_lsn(msf) : VCDINFO_NULL_LSN;
  }
}

EntriesVcd_t * 
vcdinfo_get_entriesVcd (vcdinfo_obj_t *p_obj) 
{
  if (!p_obj) return NULL;
  return &(p_obj->entries);
}
  
/*!
   Get the VCD format (VCD 1.0 VCD 1.1, SVCD, ... for this object.
   The type is also set inside obj.
*/
vcd_type_t
vcdinfo_get_format_version (const vcdinfo_obj_t *p_obj)
{
  return p_obj->vcd_type;
}

/*!
   Return a string giving VCD format (VCD 1.0 VCD 1.1, SVCD, ... 
   for this object.
*/
const char *
vcdinfo_get_format_version_str (const vcdinfo_obj_t *p_obj) 
{
  if (!p_obj) return "*Uninitialized*";
  return vcdinf_get_format_version_str(p_obj->vcd_type);
}

InfoVcd_t * 
vcdinfo_get_infoVcd (vcdinfo_obj_t *p_obj) 
{
  if (!p_obj) return NULL;
  return &(p_obj->info);
}
  
/*!  Return the entry number closest and before the given LSN.
 */
unsigned int 
vcdinfo_lsn_get_entry(const vcdinfo_obj_t *p_obj, lsn_t lsn) 
{

  /* Do a binary search to find the entry. */
  unsigned int i = 0;
  unsigned int j = vcdinfo_get_num_entries(p_obj);
  unsigned int mid;
  unsigned int mid_lsn;
  do {
    mid = (i+j)/2;
    mid_lsn = vcdinfo_get_entry_lsn(p_obj, mid);
    if ( lsn <=  mid_lsn ) j = mid-1;
    if ( lsn >=  mid_lsn ) i = mid+1;
  } while (i <= j);

  /* We want the entry closest but before. */
  return (lsn == mid_lsn) ? mid : mid-1;
}

  
/*!
   \brief Get ISO 9660 Primary Volume Descriptor (PVD) for a VCDinfo
   object.
*/
iso9660_pvd_t * 
vcdinfo_get_pvd (vcdinfo_obj_t *p_obj) 
{
  if (!p_obj) return NULL;
  return &(p_obj->pvd);
}
  
void * 
vcdinfo_get_scandata (vcdinfo_obj_t *p_obj) 
{
  if (!p_obj) return NULL;
  return p_obj->scandata_buf;
}
  
void * 
vcdinfo_get_searchDat (vcdinfo_obj_t *p_obj) 
{
  if (!p_obj) return NULL;
  return p_obj->search_buf;
}
  
void * 
vcdinfo_get_tracksSVD (vcdinfo_obj_t *p_obj) 
{
  if (!p_obj) return NULL;
  return p_obj->tracks_buf;
}
  
/*!
  Get the itemid for a given list ID. 
  VCDINFO_REJECTED_MASK is returned on error or if obj is NULL. 
*/
uint16_t
vcdinfo_lid_get_itemid(const vcdinfo_obj_t *p_obj, lid_t lid)
{
  PsdListDescriptor_t pxd;

  if (!p_obj) return VCDINFO_REJECTED_MASK;
  vcdinfo_lid_get_pxd(p_obj, &pxd, lid);
  switch (pxd.descriptor_type) {
  case PSD_TYPE_SELECTION_LIST:
  case PSD_TYPE_EXT_SELECTION_LIST:
    if (pxd.psd == NULL) return VCDINFO_REJECTED_MASK;
    return vcdinf_psd_get_itemid(pxd.psd);
    break;
  case PSD_TYPE_PLAY_LIST:
    /* FIXME: There is an array of items */
  case PSD_TYPE_END_LIST:
  case PSD_TYPE_COMMAND_LIST:
    return VCDINFO_REJECTED_MASK;
  }

  return VCDINFO_REJECTED_MASK;

}

/*!
  Get the LOT pointer. 
*/
LotVcd_t *
vcdinfo_get_lot(const vcdinfo_obj_t *p_obj)
{
  if (!p_obj) return NULL;
  return p_obj->lot;
}

/*!
  Get the extended LOT pointer. 
*/
LotVcd_t *
vcdinfo_get_lot_x(const vcdinfo_obj_t *p_obj)
{
  if (!p_obj) return NULL;
  return p_obj->lot_x;
}
  
/*!
  Return number of LIDs. 
*/
lid_t
vcdinfo_get_num_LIDs (const vcdinfo_obj_t *p_obj)
{
  /* Should probably use _vcd_pbc_max_lid instead? */
  if (!p_obj) return 0;
  return vcdinf_get_num_LIDs( (&p_obj->info) );
}

/*!
  Return the number of entries in the VCD.
*/
unsigned int
vcdinfo_get_num_entries(const vcdinfo_obj_t *p_obj)
{
  const EntriesVcd_t *entries = &(p_obj->entries);
  return vcdinf_get_num_entries(entries);
}

/*!
  Return the number of segments in the VCD. Return 0 if there is some
  problem.
*/
segnum_t
vcdinfo_get_num_segments(const vcdinfo_obj_t *p_obj)
{
  if (!p_obj) return 0;
  return vcdinf_get_num_segments( &(p_obj->info) );
}

/*!
  \fn vcdinfo_get_offset_lid(const vcdinfo_obj_t *p_obj, 
                             unsigned int entry_num);
  \brief Get offset entry_num for a given LID. 
  \return VCDINFO_INVALID_OFFSET is returned if obj on error or obj
  is NULL. Otherwise the LID offset is returned.
*/
uint16_t
vcdinfo_lid_get_offset(const vcdinfo_obj_t *p_obj, lid_t lid,
                       unsigned int entry_num) 
{
  PsdListDescriptor_t pxd;

  if (!p_obj) return VCDINFO_INVALID_OFFSET;
  vcdinfo_lid_get_pxd(p_obj, &pxd, lid);

  switch (pxd.descriptor_type) {
  case PSD_TYPE_SELECTION_LIST:
  case PSD_TYPE_EXT_SELECTION_LIST:
    if (pxd.psd == NULL) return VCDINFO_INVALID_OFFSET;
    return vcdinf_psd_get_offset(pxd.psd, entry_num-1);
    break;
  case PSD_TYPE_PLAY_LIST:
    /* FIXME: There is an array of items */
  case PSD_TYPE_END_LIST:
  case PSD_TYPE_COMMAND_LIST:
    return VCDINFO_INVALID_OFFSET;
  }
  return VCDINFO_INVALID_OFFSET;

}

/*! 
  NULL is returned on error. 
*/
static vcdinfo_offset_t *
_vcdinfo_get_offset_t (const vcdinfo_obj_t *p_obj, unsigned int offset, 
                       bool ext)
{
  CdioListNode_t *node;
  CdioList_t *offset_list = ext ? p_obj->offset_x_list : p_obj->offset_list;

  switch (offset) {
  case PSD_OFS_DISABLED:
  case PSD_OFS_MULTI_DEF:
  case PSD_OFS_MULTI_DEF_NO_NUM:
    return NULL;
  default: ;
  }
  
  _CDIO_LIST_FOREACH (node, offset_list)
    {
      vcdinfo_offset_t *p_ofs = _cdio_list_node_data (node);
      if (offset == p_ofs->offset)
        return p_ofs;
    }
  return NULL;
}

/*!
  Get the VCD info list.
*/
CdioList_t *
vcdinfo_get_offset_list(const vcdinfo_obj_t *p_obj)
{
  if (!p_obj) return NULL;
  return p_obj->offset_list;
}


/*!
  Get the VCD info extended offset list.
*/
CdioList_t *
vcdinfo_get_offset_x_list(const vcdinfo_obj_t *p_obj)
{
  if (!p_obj) return NULL;
  return p_obj->offset_x_list;
}

/*!
  Get the VCD info offset multiplier.
*/
unsigned int vcdinfo_get_offset_mult(const vcdinfo_obj_t *p_obj) 
{
  if (!p_obj) return 0xFFFF;
  return p_obj->info.offset_mult;
}

/*! 
  Get entry in offset list for the item that has offset. This entry 
  has for example the LID. NULL is returned on error. 
*/
vcdinfo_offset_t *
vcdinfo_get_offset_t (const vcdinfo_obj_t *p_obj, unsigned int offset)
{
  vcdinfo_offset_t *off_p= _vcdinfo_get_offset_t (p_obj, offset, true);
  if (NULL != off_p) 
    return off_p;
  return _vcdinfo_get_offset_t (p_obj, offset, false);
}

/*!
   Return a string containing the VCD publisher id with trailing
   blanks removed, or NULL if there is some problem in getting this.
*/
char *
vcdinfo_get_preparer_id(const vcdinfo_obj_t *p_obj)
{
  if ( !p_obj ) return (NULL);
  return iso9660_get_preparer_id(&(p_obj->pvd));
}

/*!
  Get the PSD.
*/
uint8_t *
vcdinfo_get_psd(const vcdinfo_obj_t *p_obj) 
{
  if ( !p_obj ) return NULL;
  return p_obj->psd;
}

/*!
  Get the extended PSD.
*/
uint8_t *
vcdinfo_get_psd_x(const vcdinfo_obj_t *p_obj)
{
  if ( !p_obj ) return NULL;
  return p_obj->psd_x;
}

/*!
  Return number of bytes in PSD. Return 0 if there's an error.
*/
uint32_t
vcdinfo_get_psd_size (const vcdinfo_obj_t *p_obj)
{
  if ( !p_obj ) return 0;
  return vcdinf_get_psd_size(&(p_obj->info));
}

/*!
  Return number of bytes in the extended PSD. Return 0 if there's an error.
*/
uint32_t
vcdinfo_get_psd_x_size (const vcdinfo_obj_t *p_obj)
{
  if ( !p_obj ) return 0;
  return p_obj->psd_x_size;
}

/*!
   Return a string containing the VCD publisher id with trailing
   blanks removed, or NULL if there is some problem in getting this.
*/
char *
vcdinfo_get_publisher_id(const vcdinfo_obj_t *p_obj)
{
  if ( !p_obj ) return (NULL);
  return iso9660_get_publisher_id(&(p_obj->pvd));
}

/*!
  Get the PSD Selection List Descriptor for a given lid.
  NULL is returned if error or not found.
*/
static bool
_vcdinfo_lid_get_pxd(const vcdinfo_obj_t *p_obj, PsdListDescriptor_t *pxd,
                     uint16_t lid, bool ext) 
{
  CdioListNode_t *node;
  unsigned mult = p_obj->info.offset_mult;
  const uint8_t *psd = ext ? p_obj->psd_x : p_obj->psd;
  CdioList_t *offset_list = ext ? p_obj->offset_x_list : p_obj->offset_list;

  if (offset_list == NULL) return false;
  
  _CDIO_LIST_FOREACH (node, offset_list)
    {
      vcdinfo_offset_t *ofs = _cdio_list_node_data (node);
      unsigned _rofs = ofs->offset * mult;

      pxd->descriptor_type = psd[_rofs];

      switch (pxd->descriptor_type)
        {
        case PSD_TYPE_PLAY_LIST:
          {
            pxd->pld = (PsdPlayListDescriptor_t *) (psd + _rofs);
            if (vcdinf_pld_get_lid(pxd->pld) == lid) {
              return true;
            }
            break;
          }

        case PSD_TYPE_EXT_SELECTION_LIST:
        case PSD_TYPE_SELECTION_LIST: 
          {
            pxd->psd = (PsdSelectionListDescriptor_t *) (psd + _rofs);
            if (vcdinf_psd_get_lid(pxd->psd) == lid) {
              return true;
            }
            break;
          }
        default: ;
        }
    }
  return false;
}

/*!
  Get the PSD Selection List Descriptor for a given lid.
  False is returned if not found.
*/
bool
vcdinfo_lid_get_pxd(const vcdinfo_obj_t *p_obj, PsdListDescriptor_t *pxd,
                    uint16_t lid)
{
  if (_vcdinfo_lid_get_pxd(p_obj, pxd, lid, true))
    return true;
  return _vcdinfo_lid_get_pxd(p_obj, pxd, lid, false);
}

/**
 \fn vcdinfo_get_return_offset(const vcdinfo_obj_t *p_obj);
 \brief Get return offset for a given LID. 
 \return  VCDINFO_INVALID_OFFSET is returned on error or if LID has no 
 "return" entry. Otherwise the LID offset is returned.
 */
uint16_t
vcdinfo_get_return_offset(const vcdinfo_obj_t *p_obj, lid_t lid)
{
  if (NULL != p_obj) {

    PsdListDescriptor_t pxd;

    vcdinfo_lid_get_pxd(p_obj, &pxd, lid);
    
    switch (pxd.descriptor_type) {
    case PSD_TYPE_PLAY_LIST:
      return vcdinf_pld_get_return_offset(pxd.pld);
    case PSD_TYPE_SELECTION_LIST:
    case PSD_TYPE_EXT_SELECTION_LIST:
      return vcdinf_psd_get_return_offset(pxd.psd);
      break;
    case PSD_TYPE_END_LIST:
    case PSD_TYPE_COMMAND_LIST:
      break;
    }
  }
  
  return VCDINFO_INVALID_OFFSET;
}

/*!
   Return the audio type for a given segment. 
   VCDINFO_INVALID_AUDIO_TYPE is returned on error.
*/
unsigned int
vcdinfo_get_seg_audio_type(const vcdinfo_obj_t *p_obj, segnum_t i_seg)
{
  if ( !p_obj || NULL == &(p_obj->info)
       || i_seg >= vcdinfo_get_num_segments(p_obj) )
    return VCDINFO_INVALID_AUDIO_TYPE;
  return(p_obj->info.spi_contents[i_seg].audio_type);
}

/*!
   Return true if this segment is supposed to continue to the next one,
   (is part of an "item" or listing in the ISO 9660 filesystem).
*/
bool
vcdinfo_get_seg_continue(const vcdinfo_obj_t *p_obj, segnum_t i_seg)
{
  if ( !p_obj || !&(p_obj->info)
       || i_seg >= vcdinfo_get_num_segments(p_obj) )
    return false;
  return(p_obj->info.spi_contents[i_seg].item_cont);
}

/*!  Return the starting LBA (logical block address) for segment
  entry_num in obj.  VCDINFO_LBA_NULL is returned if there is no entry.

  Note first i_seg is 0.
*/
lba_t
vcdinfo_get_seg_lba(const vcdinfo_obj_t *p_obj, segnum_t i_seg)
{ 
  if (!p_obj) return VCDINFO_NULL_LBA;
  return cdio_lsn_to_lba(vcdinfo_get_seg_lsn(p_obj, i_seg));
}

/*!  Return the starting LBA (logical block address) for segment
  entry_num in obj.  VCDINFO_LSN_NULL is returned if there is no entry.

  Note first i_seg is 0.
*/
lsn_t
vcdinfo_get_seg_lsn(const vcdinfo_obj_t *p_obj, segnum_t i_seg)
{ 
  if (!p_obj || i_seg >= vcdinfo_get_num_segments(p_obj))
    return VCDINFO_NULL_LSN;
  return p_obj->first_segment_lsn + (VCDINFO_SEGMENT_SECTOR_SIZE * i_seg);
}

/*!  Return the starting MSF (minutes/secs/frames) for segment
  entry_num in obj.  NULL is returned if there is no entry.

  Note first i_seg is 0!
*/
const msf_t *
vcdinfo_get_seg_msf(const vcdinfo_obj_t *p_obj, segnum_t i_seg)
{ 
  if (!p_obj || i_seg >= vcdinfo_get_num_segments(p_obj))
    return NULL;
  else {
    lsn_t lsn = vcdinfo_get_seg_lsn(p_obj, i_seg);
    static msf_t msf;
    cdio_lsn_to_msf(lsn, &msf);
    return &msf;
  }
}

/*! Return the x-y resolution for a given segment.
  Note first i_seg is 0.
*/
void
vcdinfo_get_seg_resolution(const vcdinfo_obj_t *p_vcdinfo, segnum_t i_seg,
                           /*out*/ uint16_t *max_x, /*out*/ uint16_t *max_y)
{
  vcdinfo_video_segment_type_t segtype 
    = vcdinfo_get_video_type(p_vcdinfo, i_seg);
  segnum_t i_segs = vcdinfo_get_num_segments(p_vcdinfo);
  
  if (i_seg >= i_segs) return;
      
  switch (segtype) {
  case VCDINFO_FILES_VIDEO_NTSC_STILL:
    *max_x = 704;
    *max_y = 480;
    break;
  case VCDINFO_FILES_VIDEO_NTSC_STILL2:
    *max_x = 352;
    *max_y = 240;
    break;
  case VCDINFO_FILES_VIDEO_PAL_STILL:
    *max_x = 704;
    *max_y = 576;
    break;
  case VCDINFO_FILES_VIDEO_PAL_STILL2:
    *max_x = 352;
    *max_y = 288;
    break;
  default:
    /* */
    switch (vcdinfo_get_format_version(p_vcdinfo)) {
    case VCD_TYPE_VCD:
      *max_x = 352;
      *max_y = 240;
      break;
    case VCD_TYPE_VCD11:
    case VCD_TYPE_VCD2:
      *max_x = 352;
      switch(segtype) {
      case VCDINFO_FILES_VIDEO_NTSC_MOTION:
        *max_y = 240;
        break;
      case VCDINFO_FILES_VIDEO_PAL_MOTION:
        *max_y = 288;
      default:
        *max_y = 289;
      }
      break;
    default: ;
    }
  }
}


  
/*!  
  Return the number of sectors for segment
  entry_num in obj.  0 is returned if there is no entry.

  Use this routine to figure out the actual number of bytes a physical
  region of a disk or CD takes up for a segment.

  If an item has been broken up into a number of "continued" segments,
  we will report the item size for the first segment and 0 for the
  remaining ones. We may revisit this decision later. 
*/
uint32_t
vcdinfo_get_seg_sector_count(const vcdinfo_obj_t *p_obj, segnum_t i_seg)
{ 
  if (!p_obj || i_seg >= vcdinfo_get_num_segments(p_obj))
    return 0;
  return p_obj->seg_sizes[i_seg];
}

/*!
   Return a string containing the VCD system id with trailing
   blanks removed, or NULL if there is some problem in getting this.
*/
char *
vcdinfo_get_system_id(const vcdinfo_obj_t *p_obj)
{
  if ( !p_obj || !&(p_obj->pvd) ) return NULL;
  return(iso9660_get_system_id(&p_obj->pvd));
}

/*!
  Return the track number for entry n in obj. 
  
  In contrast to libcdio we start numbering at 0 which is the
  ISO9660 and metadata information for the Video CD. Thus track 
  1 is the first track the first complete MPEG track generally.
*/
track_t
vcdinfo_get_track(const vcdinfo_obj_t *p_obj, const unsigned int entry_num)
{
  const EntriesVcd_t *entries = &p_obj->entries;
  const unsigned int entry_count = vcdinf_get_num_entries(entries);
  /* Note entry_num is 0 origin. */
  return entry_num < entry_count ?
    vcdinf_get_track(entries, entry_num)-1: VCDINFO_INVALID_TRACK;
}

/*!
   Return the audio type for a given track. 
   VCDINFO_INVALID_AUDIO_TYPE is returned on error.

   Note: track 1 is usually the first track.
*/
unsigned int
vcdinfo_get_track_audio_type(const vcdinfo_obj_t *p_obj, track_t i_track)
{
  TracksSVD_t *tracks;
  TracksSVD2_t *tracks2;
  if ( !p_obj || !&(p_obj->info) ) return VCDINFO_INVALID_AUDIO_TYPE;
  tracks = p_obj->tracks_buf;

  if ( NULL == tracks ) return 0;
  tracks2 = (TracksSVD2_t *) &(tracks->playing_time[tracks->tracks]);
  return(tracks2->contents[i_track-1].audio);
}

/*!  
  Return the highest track number in the current medium. 
  
  Because we track start numbering at 0 (which is the ISO 9660 track
  containing Video CD naviagion and disk information), this is one
  less than the number of tracks.
  
    If there are no tracks, we return -1.
*/
unsigned int
vcdinfo_get_num_tracks(const vcdinfo_obj_t *p_obj)
{
  if (!p_obj || !p_obj->img) return 0;

  return cdio_get_num_tracks(p_obj->img)-1;
}


/*!  
  Return the starting LBA (logical block address) for track number
  i_track in obj.  

  The IS0-9660 filesystem track has number 0. Tracks associated
  with playable entries numbers start at 1.

  The "leadout" track is specified either by
  using i_track LEADOUT_TRACK or the total tracks+1.
  VCDINFO_NULL_LBA is returned on failure.
*/
lba_t 
vcdinfo_get_track_lba(const vcdinfo_obj_t *p_obj, track_t i_track)
{
  if (!p_obj || !p_obj->img)
    return VCDINFO_NULL_LBA;
  

  /* CdIo tracks start at 1 rather than 0. */
  return cdio_get_track_lba(p_obj->img, i_track+1);
}

/*!  
  Return the starting LSN (logical sector number) for track number
  i_track in obj.  

  The IS0-9660 filesystem track has number 0. Tracks associated
  with playable entries numbers start at 1.

  The "leadout" track is specified either by
  using i_track LEADOUT_TRACK or the total tracks+1.
  VCDINFO_NULL_LBA is returned on failure.
*/
lsn_t 
vcdinfo_get_track_lsn(const vcdinfo_obj_t *p_obj, track_t i_track)
{
  if (!p_obj || !p_obj->img)
    return VCDINFO_NULL_LSN;

  /* CdIo tracks start at 1 rather than 0. */
  return cdio_get_track_lsn(p_obj->img, i_track+1);
}

/*!  
  Return the ending LSN for track number
  i_track in cdio.  VCDINFO_NULL_LSN is returned on error.
*/
lsn_t vcdinfo_get_track_last_lsn(const vcdinfo_obj_t *p_obj, track_t i_track)
{
  if (!p_obj || !p_obj->img)
    return VCDINFO_NULL_LSN;

  /* CdIo tracks start at 1 rather than 0. */
  return cdio_get_track_last_lsn(p_obj->img, i_track+1);
}

/*!  
  Return the starting MSF (minutes/secs/frames) for track number
  i_track in obj.  

  The IS0-9660 filesystem track has number 0. Tracks associated
  with playable entries numbers start at 1.

  The "leadout" track is specified either by
  using i_track LEADOUT_TRACK or the total tracks+1.
  VCDINFO_NULL_LBA is returned on failure.
*/
int
vcdinfo_get_track_msf(const vcdinfo_obj_t *p_obj, track_t i_track,
                      uint8_t *min, uint8_t *sec, uint8_t *frame)
{
  msf_t msf;

  if (!p_obj || !p_obj->img)
    return 1;
  
  /* CdIo tracks start at 1 rather than 0. */
  if (cdio_get_track_msf(p_obj->img, i_track+1, &msf)) {
    *min   = cdio_from_bcd8(msf.m);
    *sec   = cdio_from_bcd8(msf.s);
    *frame = cdio_from_bcd8(msf.f);
    return 0;
  }
  
  return 1;
}

/*!
  Return the size in sectors for track n. 

  The IS0-9660 filesystem track has number 0. Tracks associated
  with playable entries numbers start at 1.

  FIXME: Whether we count the track pregap sectors is a bit haphazard.
  We should add a parameter to indicate whether this is wanted or not.

*/
unsigned int
vcdinfo_get_track_sect_count(const vcdinfo_obj_t *p_obj, 
                             const track_t i_track)
{
  if (!p_obj || VCDINFO_INVALID_TRACK == i_track) 
    return 0;
  
  {
    iso9660_stat_t *p_statbuf;
    const lsn_t lsn = vcdinfo_get_track_lsn(p_obj, i_track);
    
    /* Try to get the sector count from the ISO 9660 filesystem */
    if (p_obj->has_xa && (p_statbuf = iso9660_find_fs_lsn(p_obj->img, lsn))) {
      unsigned int secsize = p_statbuf->secsize;
      free(p_statbuf);
      return secsize;
    } else {
      const lsn_t next_lsn=vcdinfo_get_track_lsn(p_obj, i_track+1);
      /* Failed on ISO 9660 filesystem. Use track information.  */
      return next_lsn > lsn ? next_lsn - lsn : 0;
    }
  }
  return 0;
}

/*!
  Return size in bytes for track number for entry n in obj.

  The IS0-9660 filesystem track has number 1. Tracks associated
  with playable entries numbers start at 2.

  FIXME: Whether we count the track pregap sectors is a bit haphazard.
  We should add a parameter to indicate whether this is wanted or not.
*/
unsigned int
vcdinfo_get_track_size(const vcdinfo_obj_t *p_obj, track_t i_track)
{
  if (NULL == p_obj || VCDINFO_INVALID_TRACK == i_track) 
    return 0;
  
  {
    const lsn_t lsn = cdio_lba_to_lsn(vcdinfo_get_track_lba(p_obj, 
                                                            i_track));
    
    /* Try to get the sector count from the ISO 9660 filesystem */
    if (p_obj->has_xa) {
      iso9660_stat_t *p_statbuf;
      p_statbuf = iso9660_find_fs_lsn(p_obj->img, lsn);
      return p_statbuf->size;
    } 
#if 0
    else {
      /* Failed on ISO 9660 filesystem. Use track information.  */
      if (p_obj->img != NULL) 
        return cdio_get_track_size(p_obj->img);
    }
#endif 
  }
  return 0;
}

/*!
  \brief Get the kind of video stream segment of segment i_seg in p_obj.
  \return VCDINFO_FILES_VIDEO_INVALID is returned if  on error or p_obj is
  null. Otherwise the enumeration type.

  Note first i_seg is 0!
*/
vcdinfo_video_segment_type_t
vcdinfo_get_video_type(const vcdinfo_obj_t *p_obj, segnum_t i_seg)
{
  const InfoVcd_t *info;
  if (p_obj == NULL)  return VCDINFO_FILES_VIDEO_INVALID;
  info = &p_obj->info;
  if (info == NULL) return VCDINFO_FILES_VIDEO_INVALID;
  return info->spi_contents[i_seg].video_type;
}

/*!
  \brief Get the kind of VCD that p_obj refers to.
*/
vcd_type_t
vcdinfo_get_VCD_type(const vcdinfo_obj_t *p_obj) 
{
  if (NULL == p_obj) return VCD_TYPE_INVALID;
  return p_obj->vcd_type;
}

  
/*!
  Return the VCD volume count - the number of CD's in the collection.
  O is returned if there is some problem in getting this. 
*/
unsigned int
vcdinfo_get_volume_count(const vcdinfo_obj_t *p_obj)
{
  if ( NULL == p_obj ) return 0;
  return vcdinf_get_volume_count(&p_obj->info);
}

/*!
  Return the VCD ID.
  NULL is returned if there is some problem in getting this. 
*/
const char *
vcdinfo_get_volume_id(const vcdinfo_obj_t *p_obj)
{
  static char psz_vol_id[ISO_MAX_VOLUME_ID+1] = {'\0'};
  char *psz_vol_id2;
  if ( NULL == p_obj || NULL == &p_obj->pvd ) return (NULL);
  psz_vol_id2 = iso9660_get_volume_id(&p_obj->pvd);
  strncpy(psz_vol_id, psz_vol_id2, ISO_MAX_VOLUME_ID);
  free(psz_vol_id2);
  return psz_vol_id;
}

/*!
  Return the VCD volumeset ID.
  NULL is returned if there is some problem in getting this. 
*/
const char *
vcdinfo_get_volumeset_id(const vcdinfo_obj_t *p_obj)
{
  static char volume_set_id[ISO_MAX_VOLUMESET_ID+1] = {'\0'};
  if ( NULL == p_obj || NULL == &p_obj->pvd ) return (NULL);
  strncpy(volume_set_id, p_obj->pvd.volume_set_id, ISO_MAX_VOLUMESET_ID);
  return vcdinfo_strip_trail(volume_set_id, ISO_MAX_VOLUMESET_ID);
}

/*!
  Return the VCD volume num - the number of the CD in the collection.
  This is a number between 1 and the volume count.
  O is returned if there is some problem in getting this. 
*/
unsigned int
vcdinfo_get_volume_num(const vcdinfo_obj_t *p_obj)
{
  if ( NULL == p_obj ) return 0;
  return(uint16_from_be( p_obj->info.vol_id));
}

int
vcdinfo_get_wait_time (uint16_t wtime)
{
  /* Note: this doesn't agree exactly with _wtime */
  if (wtime < 61)
    return wtime;
  else if (wtime < 255)
    return (wtime - 60) * 10 + 60;
  else
    return -1;
}

/*!
  Return true is there is playback control. 
*/
bool
vcdinfo_has_pbc (const vcdinfo_obj_t *p_obj) 
{
  return (p_obj && p_obj->info.psd_size!=0);
}
  
/*! 
  Return true if VCD has "extended attributes" (XA). Extended attributes
  add meta-data attributes to a entries of file describing the file.
  See also cdio_get_xa_attr_str() which returns a string similar to
  a string you might get on a Unix filesystem listing ("ls").
*/
bool
vcdinfo_has_xa(const vcdinfo_obj_t *p_obj) 
{
  return p_obj->has_xa;
}

/*!
  Add one to the MSF.
*/
void
vcdinfo_inc_msf (uint8_t *p_min, uint8_t *p_sec, int8_t *p_frame)
{
  (*p_frame)++;
  if (*p_frame>=CDIO_CD_FRAMES_PER_SEC) {
    *p_frame = 0;
    (*p_sec)++;
    if (*p_sec>=CDIO_CD_SECS_PER_MIN) {
      *p_sec = 0;
      (*p_min)++;
    }
  }
}

/*!
  Convert minutes, seconds and frame (MSF components) into a
  logical block address (or LBA). 
  See also cdio_msf_to_lba which uses msf_t as its single parameter.
*/
#undef vcdinfo_msf2lba
lba_t
vcdinfo_msf2lba (uint8_t min, uint8_t sec, int8_t frame)
{
  return CDIO_CD_FRAMES_PER_SEC*(CDIO_CD_SECS_PER_MIN*min + sec) + frame;
}

/*!
  Convert minutes, seconds and frame (MSF components) into a
  logical block address (or LBA). 
  See also cdio_msf_to_lba which uses msf_t as its single parameter.
*/
void 
vcdinfo_lba2msf (lba_t lba, uint8_t *p_min, uint8_t *p_sec, uint8_t *p_frame) 
{
  *p_min = lba / (60*75);
  lba %= (60*75);
  *p_sec = lba / 75; 
  *p_frame = lba % 75; 
}

/*!
  Convert minutes, seconds and frame (MSF components) into a
  logical sector number (or LSN). 
*/
lsn_t
vcdinfo_msf2lsn (uint8_t min, uint8_t sec, int8_t frame)
{
  lba_t lba=75*(60*min + sec) + frame;
  if (lba < CDIO_PREGAP_SECTORS) {
    vcd_error ("lba (%u) less than pregap sector (%u)", 
               (unsigned int) lba, CDIO_PREGAP_SECTORS);
    return lba;
  }
  return lba - CDIO_PREGAP_SECTORS;
}

const char *
vcdinfo_ofs2str (const vcdinfo_obj_t *p_obj, unsigned int offset, bool ext)
{
  vcdinfo_offset_t *ofs;
  char *buf;

  switch (offset) {
  case PSD_OFS_DISABLED:
    return "disabled";
  case PSD_OFS_MULTI_DEF:
    return "multi-default";
  case PSD_OFS_MULTI_DEF_NO_NUM:
    return "multi_def_no_num";
  default: ;
  }

  buf = _getbuf ();
  ofs = _vcdinfo_get_offset_t(p_obj, offset, ext);
  if (ofs != NULL) {
    if (ofs->lid)
      snprintf (buf, BUF_SIZE, "LID[%d] @0x%4.4x", 
                ofs->lid, ofs->offset);
    else 
      snprintf (buf, BUF_SIZE, "PSD[?] @0x%4.4x", 
                ofs->offset);
  } else {
    snprintf (buf, BUF_SIZE, "? @0x%4.4x", offset);
  }
  return buf;
}

bool
vcdinfo_read_psd (vcdinfo_obj_t *p_obj)
{
  unsigned psd_size = vcdinfo_get_psd_size (p_obj);

  if (psd_size)
    {
      if (psd_size > 256*1024)
        {
          vcd_error ("weird psd size (%u) -- aborting", psd_size);
          return false;
        }

      free(p_obj->lot);
      p_obj->lot = calloc(1, ISO_BLOCKSIZE * LOT_VCD_SIZE);
      free(p_obj->psd);
      p_obj->psd = calloc(1, ISO_BLOCKSIZE * _vcd_len2blocks (psd_size, 
                                                               ISO_BLOCKSIZE));
      
      if (cdio_read_mode2_sectors (p_obj->img, (void *) p_obj->lot, LOT_VCD_SECTOR,
                                   false, LOT_VCD_SIZE))
        return false;
          
      if (cdio_read_mode2_sectors (p_obj->img, (void *) p_obj->psd, PSD_VCD_SECTOR,
                                   false, _vcd_len2blocks (psd_size, 
                                                           ISO_BLOCKSIZE)))
        return false;
  
    } else {
      return false;
    }
  return true;
}

/*!  Return the entry number for the given track.  */
unsigned int 
vcdinfo_track_get_entry(const vcdinfo_obj_t *p_obj, track_t i_track) 
{
  /* FIXME: Add structure to directly map track to first entry number. 
     Until then...
   */
  lsn_t lsn= vcdinfo_get_track_lsn(p_obj, i_track);
  return vcdinfo_lsn_get_entry(p_obj, lsn);
}
  
/*!
   Calls recursive routine to populate obj->offset_list or obj->offset_x_list
   by going through LOT.

   Returns false if there was some error.
*/
bool
vcdinfo_visit_lot (vcdinfo_obj_t *p_obj, bool extended)
{
  struct _vcdinf_pbc_ctx pbc_ctx;
  bool ret;

  pbc_ctx.psd_size      = vcdinfo_get_psd_size (p_obj);
  pbc_ctx.psd_x_size    = p_obj->psd_x_size;
  pbc_ctx.offset_mult   = 8;
  pbc_ctx.maximum_lid   = vcdinfo_get_num_LIDs(p_obj);
  pbc_ctx.offset_x_list = NULL;
  pbc_ctx.offset_list   = NULL;
  pbc_ctx.psd           = p_obj->psd;
  pbc_ctx.psd_x         = p_obj->psd_x;
  pbc_ctx.lot           = p_obj->lot;
  pbc_ctx.lot_x         = p_obj->lot_x;
  pbc_ctx.extended      = extended;

  ret = vcdinf_visit_lot(&pbc_ctx);
  if (NULL != p_obj->offset_x_list) 
    _cdio_list_free(p_obj->offset_x_list, true);
  p_obj->offset_x_list = pbc_ctx.offset_x_list;
  if (NULL != p_obj->offset_list) 
    _cdio_list_free(p_obj->offset_list, true);
  p_obj->offset_list = pbc_ctx.offset_list;
  return ret;
}

/*!
   Change trailing blanks in str to nulls.  Str has a maximum size of
   n characters.
*/
const char *
vcdinfo_strip_trail (const char str[], size_t n)
{
  static char buf[1024];
  int j;

  vcd_assert (n < 1024);

  strncpy (buf, str, n);
  buf[n] = '\0';

  for (j = strlen (buf) - 1; j >= 0; j--)
    {
      if (buf[j] != ' ')
        break;

      buf[j] = '\0';
    }

  return buf;
}

/*!
 Return true if offset is "rejected". That is shouldn't be displayed
 in a list of entries.
*/
bool
vcdinfo_is_rejected(uint16_t offset)
{
  return (offset & VCDINFO_REJECTED_MASK) != 0;
}

/*!
   Nulls/zeros vcdinfo_obj_t structures; The caller should have 
   ensured that p_obj != NULL.
   routines using p_obj are called.
*/
static void
_vcdinfo_zero(vcdinfo_obj_t *p_obj)
{
  memset(p_obj, 0, sizeof(vcdinfo_obj_t));
  p_obj->vcd_type = VCD_TYPE_INVALID;
  p_obj->img = NULL;
  p_obj->lot = NULL;
  p_obj->source_name = NULL;
  p_obj->seg_sizes = NULL;
}

/*!
   Initialize the vcdinfo structure "obj". Should be done before other
   routines using p_obj are called.
*/
bool 
vcdinfo_init(vcdinfo_obj_t *p_obj)
{
  if (NULL == p_obj) return false;
  _vcdinfo_zero(p_obj);
  return cdio_init();
}

/*!
   Set up vcdinfo structure "p_obj" for reading from a particular
   medium. This should be done before after initialization but before
   any routines that need to retrieve data.
 
   source_name is the device or file to use for inspection, and
   source_type indicates what driver to use or class of drivers in the
   case of DRIVER_DEVICE.
   access_mode gives the CD access method for reading should the driver
   allow for more than one kind of access method (e.g. MMC versus ioctl
   on GNU/Linux)
    
   If source_name is NULL we'll fill in the appropriate default device
   name for the given source_type. However if in addtion source_type is
   DRIVER_UNKNOWN, then we'll scan for a drive containing a VCD.
    
   VCDINFO_OPEN_VCD is returned if everything went okay; 
   VCDINFO_OPEN_ERROR if there was an error and VCDINFO_OPEN_OTHER if the
   medium is something other than a VCD.

   Only if VCDINFO_OPEN_VCD is returned, the caller needs free the
   vcdinfo_obj_t. 
 */
vcdinfo_open_return_t
vcdinfo_open(vcdinfo_obj_t **pp_obj, char *source_name[], 
             driver_id_t source_type, const char access_mode[])
{
  CdIo_t *p_cdio;
  vcdinfo_obj_t *p_obj = calloc(1, sizeof(vcdinfo_obj_t));
  iso9660_stat_t *statbuf;


  /* If we don't specify a driver_id or a source_name, scan the
     system for a CD that contains a VCD.
   */
  if (NULL == *source_name && source_type == DRIVER_UNKNOWN) {
    char **cd_drives=NULL;
    cd_drives = cdio_get_devices_with_cap_ret(NULL, 
                (CDIO_FS_ANAL_SVCD|CDIO_FS_ANAL_CVD|CDIO_FS_ANAL_VIDEOCD
                |CDIO_FS_UNKNOWN),
                                              true, &source_type);
    if ( NULL == cd_drives || NULL == cd_drives[0] ) {
      goto err_return;
    }
    *source_name = strdup(cd_drives[0]);
    cdio_free_device_list(cd_drives);
  }

  p_cdio = cdio_open(*source_name, source_type);
  if (NULL == p_cdio) {
    goto err_return;
  }

  *pp_obj = p_obj;
  
  if (access_mode != NULL) 
    cdio_set_arg (p_cdio, "access-mode", access_mode);

  if (NULL == *source_name) {
    *source_name = cdio_get_default_device(p_cdio);
    if (NULL == *source_name) {
      goto err_return;
    }
  }
    
  memset (p_obj, 0, sizeof (vcdinfo_obj_t));
  p_obj->img = p_cdio;  /* Note we do this after the above wipeout! */

  if (!iso9660_fs_read_pvd(p_obj->img, &(p_obj->pvd))) {
    goto err_return;
  }
  
  /* Determine if VCD has XA attributes. */
  {
    
    iso9660_pvd_t const *pvd = &p_obj->pvd;
    
    p_obj->has_xa = !strncmp ((char *) pvd + ISO_XA_MARKER_OFFSET, 
                            ISO_XA_MARKER_STRING, 
                           strlen (ISO_XA_MARKER_STRING));
  }
    
  if (!read_info(p_obj->img, &(p_obj->info), &(p_obj->vcd_type)))
    goto other_return;

  if (vcdinfo_get_format_version (p_obj) == VCD_TYPE_INVALID)
    goto other_return;

  if (!read_entries(p_obj->img, &(p_obj->entries)))
    goto other_return;
  
  {
    size_t len = strlen(*source_name)+1;
    p_obj->source_name = (char *) malloc(len * sizeof(char));
    strncpy(p_obj->source_name, *source_name, len);
  }

  if (p_obj->vcd_type == VCD_TYPE_SVCD || p_obj->vcd_type == VCD_TYPE_HQVCD) {
    statbuf = iso9660_fs_stat (p_obj->img, "MPEGAV");
    
    if (NULL != statbuf) {
      vcd_warn ("non compliant /MPEGAV folder detected!");
      free(statbuf);
    }
    

    statbuf = iso9660_fs_stat (p_obj->img, "SVCD/TRACKS.SVD;1");
    if (NULL != statbuf) {
      lsn_t lsn = statbuf->lsn;
      if (statbuf->size != ISO_BLOCKSIZE)
        vcd_warn ("TRACKS.SVD filesize != %d!", ISO_BLOCKSIZE);
      
      p_obj->tracks_buf = calloc(1, ISO_BLOCKSIZE);

      free(statbuf);
      if (cdio_read_mode2_sector (p_obj->img, p_obj->tracks_buf, lsn, false))
        goto err_return;
    }
  }
      
  _init_segments (p_obj);

  switch (p_obj->vcd_type) {
  case VCD_TYPE_VCD2: {
    /* FIXME: Can reduce CD reads by using 
       iso9660_fs_readdir(img, "EXT", true) and then scanning for
       the files listed below.
    */
    statbuf = iso9660_fs_stat (p_cdio, "EXT/PSD_X.VCD;1");
    if (NULL != statbuf) {
      lsn_t lsn        = statbuf->lsn;
      uint32_t secsize = statbuf->secsize;

      p_obj->psd_x       = calloc(1, ISO_BLOCKSIZE * secsize);
      p_obj->psd_x_size  = statbuf->size;
      
      vcd_debug ("found /EXT/PSD_X.VCD at sector %lu", 
                 (long unsigned int) lsn);

      free(statbuf);
      if (cdio_read_mode2_sectors (p_cdio, p_obj->psd_x, lsn, false, secsize))
        goto err_return;
    }

    statbuf = iso9660_fs_stat (p_cdio, "EXT/LOT_X.VCD;1");
    if (NULL != statbuf) {
      lsn_t lsn        = statbuf->lsn;
      uint32_t secsize = statbuf->secsize;
      p_obj->lot_x       = calloc(1, ISO_BLOCKSIZE * secsize);
      
      vcd_debug ("found /EXT/LOT_X.VCD at sector %lu", 
                 (unsigned long int) lsn);
        
      if (statbuf->size != LOT_VCD_SIZE * ISO_BLOCKSIZE)
        vcd_warn ("LOT_X.VCD size != 65535");

      free(statbuf);
      if (cdio_read_mode2_sectors (p_cdio, p_obj->lot_x, lsn, false, secsize))
        goto err_return;
      
    }
    break;
  }
  case VCD_TYPE_SVCD: 
  case VCD_TYPE_HQVCD: {
    /* FIXME: Can reduce CD reads by using 
       iso9660_fs_readdir(p_cdio, "SVCD", true) and then scanning for
       the files listed below.
    */
    statbuf = iso9660_fs_stat (p_cdio, "MPEGAV");
    if (NULL != statbuf) {
      vcd_warn ("non compliant /MPEGAV folder detected!");
      free(statbuf);
    }
    
    statbuf = iso9660_fs_stat (p_cdio, "SVCD/TRACKS.SVD;1");
    if (NULL == statbuf)
      vcd_warn ("mandatory /SVCD/TRACKS.SVD not found!");
    else {
      vcd_debug ("found TRACKS.SVD signature at sector %lu", 
                 (unsigned long int) statbuf->lsn);
      free(statbuf);
    }
    
    statbuf = iso9660_fs_stat (p_cdio, "SVCD/SEARCH.DAT;1");
    if (NULL == statbuf)
      vcd_warn ("mandatory /SVCD/SEARCH.DAT not found!");
    else {
      lsn_t    lsn       = statbuf->lsn;
      uint32_t secsize   = statbuf->secsize;
      uint32_t stat_size = statbuf->size;
      uint32_t size;

      vcd_debug ("found SEARCH.DAT at sector %lu", (unsigned long int) lsn);
      
      p_obj->search_buf = calloc(1, ISO_BLOCKSIZE * secsize);
      
      if (cdio_read_mode2_sectors (p_cdio, p_obj->search_buf, lsn, false, secsize))
        goto err_return;
      
      size = (3 * uint16_from_be (((SearchDat_t *)p_obj->search_buf)->scan_points))
        + sizeof (SearchDat_t);

      free(statbuf);
      if (size > stat_size) {
        vcd_warn ("number of scanpoints leads to bigger size than "
                  "file size of SEARCH.DAT! -- rereading");
        
        free (p_obj->search_buf);
        p_obj->search_buf = calloc(1, ISO_BLOCKSIZE 
                                       * _vcd_len2blocks(size, ISO_BLOCKSIZE));
        
        if (cdio_read_mode2_sectors (p_cdio, p_obj->search_buf, lsn, false, 
                                     secsize))
          goto err_return;
      }
    }
    break;
    }
  default:
    ;
  }

  statbuf = iso9660_fs_stat (p_cdio, "EXT/SCANDATA.DAT;1");
  if (statbuf != NULL) {
    lsn_t    lsn       = statbuf->lsn;
    uint32_t secsize   = statbuf->secsize;

    vcd_debug ("found /EXT/SCANDATA.DAT at sector %u", (unsigned int) lsn);
    
    p_obj->scandata_buf = calloc(1, ISO_BLOCKSIZE * secsize);

    free(statbuf);
    if (cdio_read_mode2_sectors (p_cdio, p_obj->scandata_buf, lsn, false, 
                                 secsize))
      return VCDINFO_OPEN_ERROR;
  }

  return VCDINFO_OPEN_VCD;

 other_return:
  free(p_obj);
  return VCDINFO_OPEN_OTHER;

 err_return:
  free(p_obj);
  return VCDINFO_OPEN_ERROR;

  
}

/*!
 Dispose of any resources associated with vcdinfo structure "p_obj".
 Call this when "p_obj" it isn't needed anymore. 

 True is returned is everything went okay, and false if not.
*/
bool 
vcdinfo_close(vcdinfo_obj_t *p_obj)
{
  if (p_obj != NULL) {
    if (p_obj->offset_list != NULL) 
      _cdio_list_free(p_obj->offset_list, true);
    if (p_obj->offset_x_list != NULL) 
      _cdio_list_free(p_obj->offset_x_list, true);
    free(p_obj->seg_sizes);
    free(p_obj->lot);
    free(p_obj->lot_x);
    free(p_obj->psd_x);
    free(p_obj->psd);
    free(p_obj->scandata_buf);
    free(p_obj->tracks_buf);
    free(p_obj->search_buf);
    free(p_obj->source_name);

    if (p_obj->img != NULL) cdio_destroy (p_obj->img);
    _vcdinfo_zero(p_obj);
  }
  
  free(p_obj);
  return(true);
}

/*! Return the selection number of the area that a point is enclosed in.
   In short we return < 0 on an error of some kind.
   If the VCD contains no extended selection list return -1.
   If we are not in an extended selection list LID, return -2.
   If there no area encloses the point return -3

   max_x, max_y are the  maximum values that x and y can take on. 
   They would be the largest coordinate in the screen coordinate space.
   For example they might be 352, 240 (for VCD) or 704, 480 for SVCD NTSC, 
   or 704, 576. 
 */
int 
vcdinfo_get_area_selection(const vcdinfo_obj_t *p_vcdinfo, 
                           lid_t lid, int16_t x, int16_t y,
                           uint16_t max_x, uint16_t max_y)
{
  /* dbg_print(INPUT_DBG_CALL, "Called\n"); */
  PsdListDescriptor_t pxd;
  if (! vcdinfo_lid_get_pxd(p_vcdinfo, &pxd, lid) )
    return -1;
  if  (pxd.descriptor_type != PSD_TYPE_EXT_SELECTION_LIST &&
       !pxd.psd->flags.SelectionAreaFlag) 
    return -2;
  {
    const PsdSelectionListDescriptorExtended_t *d2 = 
                  (const void *) &(pxd.psd->ofs[pxd.psd->nos]);
    const int32_t scaled_x = x * 255 / max_x;
    const int32_t scaled_y = y * 255 / max_y;
    const int n = vcdinf_get_num_selections(pxd.psd);
    int i;
    vcd_debug("max x %d, max y %d, scaled x: %d, scaled y %d", 
              max_x, max_y, (int) scaled_x, (int) scaled_y);
    for (i = 0; i < n; i++) {
      vcd_debug("x1: %d, y1 %d, x2: %d, y2 %d",
             d2->area[i].x1, d2->area[i].y1, d2->area[i].y2, d2->area[i].y2 );
      if (d2->area[i].x1 <= scaled_x && 
          d2->area[i].y1 <= scaled_y &&
          d2->area[i].x2 >= scaled_x &&
          d2->area[i].y2 >= scaled_y ) {
        return i + vcdinf_get_bsn(pxd.psd);
      }
    }
  }
  return -3;
  
}


/* 
 * Local variables:
 *  c-file-style: "gnu"
 *  tab-width: 8
 *  indent-tabs-mode: nil
 * End:
 */