-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathIFOParse.cs
633 lines (573 loc) · 18.2 KB
/
IFOParse.cs
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
using System;
using System.Text;
using System.IO;
using System.Collections;
using System.Collections.Generic;
namespace Utilities.DVDImport
{
/// <summary>
/// Summary description for IFOParse.
/// </summary>
public class IFOParse
{
public class Cell
{
private int m_duration = 0;
private int m_partialFrames = 0;
private int m_frameRate = 0;
private int m_firstSector = 0;
private int m_lastSector = 0;
private int m_vobID = 0;
private int m_cellID = 0;
public int Duration { get { return m_duration; } set { m_duration = value; } }
public int PartialFrames { get { return m_partialFrames; } set { m_partialFrames = value; } }
public int FrameRate { get { return m_frameRate; } set { m_frameRate = value; } }
public int FirstSector { get { return m_firstSector; } set { m_firstSector = value; } }
public int LastSector { get { return m_lastSector; } set { m_lastSector = value; } }
public int VobID { get { return m_vobID; } set { m_vobID = value; } }
public int CellID { get { return m_cellID; } set { m_cellID = value; } }
}
public class ProgramChain
{
private int m_duration = 0;
private int m_partialFrames = 0;
private int m_frameRate = 0;
private int m_angles = 0;
private List<Cell> m_cells = new List<Cell>();
private IFOParse m_parent = null;
public ProgramChain(IFOParse parent)
{
m_parent = parent;
}
public int Duration { get { return m_duration; } set { m_duration = value; } }
public int PartialFrames { get { return m_partialFrames; } set { m_partialFrames = value; } }
public int FrameRate { get { return m_frameRate; } set { m_frameRate = value; } }
public int Angles { get { return m_angles; } set { m_angles = value; } }
public List<Cell> Cells { get { return m_cells; } set { m_cells = value; } }
public IFOParse Title { get { return m_parent; } }
}
public class VOB
{
private FileInfo m_file = null;
private long m_sectors = 0;
private long m_startSector = 0;
public VOB(string filename)
{
m_file = new FileInfo(filename);
m_sectors = m_file.Length / 2048;
}
public long Sectors { get { return m_sectors; } }
public long FirstSector { get { return m_startSector; } set { m_startSector = value; } }
public long LastSector
{
get
{
return m_startSector + m_sectors;
}
}
public string Filename { get { return m_file.FullName; } }
public FileInfo FileInfo { get { return m_file; } }
}
public class AudioFormatDetails
{
private string m_encoding = "AC3";
private string m_language = "en 2ch";
private int m_streamID = 0x80;
public AudioFormatDetails(string encoding, string language, int streamID)
{
m_encoding = encoding;
m_language = language;
m_streamID = streamID;
}
public string Encoding
{
get { return (m_encoding); }
}
public string Language
{
get { return (m_language); }
}
public int StreamID
{
get { return (m_streamID); }
}
public override string ToString()
{
return String.Format("{0} {1} audio", m_encoding, m_language);
}
}
private UInt16 ReadWord(byte[] p, int offset)
{
UInt16 x;
x = p[offset]; x <<= 8;
x |= p[offset + 1];
return (x);
}
private FileInfo m_file = null;
private int m_numAudio = 0;
private List<AudioFormatDetails> m_audioFormat = new List<AudioFormatDetails>();
private string m_aspectRatio = "";
private string m_resolution = "";
private string m_videoMode = "";
private List<ProgramChain> m_pgcs = new List<ProgramChain>();
private List<VOB> m_vobs = new List<VOB>();
public IFOParse(string filename)
{
m_file = new FileInfo(filename);
FileStream fs = File.OpenRead(filename);
try
{
#region Header
// verify valid video IFO header
byte[] id = new byte[12];
if(fs.Read(id, 0, 12) != 12)
throw new IOException("Error reading IFO file");
string idStr = Encoding.ASCII.GetString(id, 0, 12);
if(idStr != "DVDVIDEO-VTS")
throw new Exception("Invalid IFO Header");
#endregion
#region Version
// verify IFO version (1.0 or 1.1)
if(fs.Seek(0x21, SeekOrigin.Begin) != 0x21)
throw new IOException("Error reading IFO file");
byte[] version = new byte[1];
if(fs.Read(version, 0, 1) != 1)
throw new IOException("Error reading IFO file");
if(version[0] != 0x10 && version[0] != 0x11)
throw new Exception("Invalid IFO Header Version");
#endregion
#region PGC Info
// find offset to pgc tables
if(fs.Seek(0xcc, SeekOrigin.Begin) != 0xcc)
throw new IOException("Error reading IFO file");
byte[] pgcOffsetData = new byte[4];
if(fs.Read(pgcOffsetData, 0, 4) != 4)
throw new IOException("Error reading IFO file");
int pgcOffset = (pgcOffsetData[0] << 24) + (pgcOffsetData[1] << 16) + (pgcOffsetData[2] << 8) + pgcOffsetData[3];
// get count of pgcs
if(fs.Seek(pgcOffset * 2048, SeekOrigin.Begin) != pgcOffset * 2048)
throw new IOException("Error reading IFO file");
byte[] pgciData = new byte[2];
if(fs.Read(pgciData, 0, 2) != 2)
throw new IOException("Error reading IFO file");
int pgcCount = (pgciData[0] << 8) + pgciData[1];
for (int i = 1; i <= pgcCount; i++)
{
ProgramChain pgc = new ProgramChain(this);
// get pgc's info offset within pgc (length is 4 bytes into pgc info)
if(fs.Seek((pgcOffset * 2048) + (i * 8) + 4, SeekOrigin.Begin) != (pgcOffset * 2048) + (i * 8) + 4)
throw new IOException("Error reading IFO file");
byte[] pgcStartData = new byte[4];
if(fs.Read(pgcStartData, 0, 4) != 4)
throw new IOException("Error reading IFO file");
int pgcStart = (pgcStartData[0] << 24) + (pgcStartData[1] << 16) + (pgcStartData[2] << 8) + pgcStartData[3];
// get pgc's playback time
if(fs.Seek((pgcOffset * 2048) + pgcStart + 4, SeekOrigin.Begin) != (pgcOffset * 2048) + pgcStart + 4)
throw new IOException("Error reading IFO file");
byte[] playbackTimeData = new byte[4];
if(fs.Read(playbackTimeData, 0, 4) != 4)
throw new IOException("Error reading IFO file");
// frame rate
int frameRate = (playbackTimeData[3] & 0xC0) >> 6;
if(frameRate == 3)
pgc.FrameRate = 30;
else if(frameRate == 1)
pgc.FrameRate = 25;
// duration
int playbackTime = (playbackTimeData[0] << 24) + (playbackTimeData[1] << 16) + (playbackTimeData[2] << 8) + playbackTimeData[3];
int hour = ((playbackTime >> 28) & 0x0f) * 10 + ((playbackTime >> 24) & 0x0f);
int min = ((playbackTime >> 20) & 0x0f) * 10 + ((playbackTime >> 16) & 0x0f);
int sec = ((playbackTime >> 12) & 0x0f) * 10 + ((playbackTime >> 8) & 0x0f);
int frame = (((playbackTime >> 4) & 0x0f) * 10 + (playbackTime & 0x0f)) - 120;
pgc.Duration = hour * 3600 + min * 60 + sec;
pgc.PartialFrames = frame;
// get pgc's cell playback info table
if (fs.Seek((pgcOffset * 2048) + pgcStart + 0xE8, SeekOrigin.Begin) != (pgcOffset * 2048) + pgcStart + 0xE8)
throw new IOException("Error reading IFO file");
byte[] C_PBKT_buf = new byte[2];
if (fs.Read(C_PBKT_buf, 0, 2) != 2)
throw new IOException("Error reading IFO file");
int C_PBKT = (C_PBKT_buf[0] << 8) + C_PBKT_buf[1];
if (C_PBKT != 0)
C_PBKT += (pgcOffset * 2048) + pgcStart;
// get pgc's cell position info table
if (fs.Seek((pgcOffset * 2048) + pgcStart + 0xEA, SeekOrigin.Begin) != (pgcOffset * 2048) + pgcStart + 0xEA)
throw new IOException("Error reading IFO file");
byte[] C_POST_buf = new byte[2];
if (fs.Read(C_POST_buf, 0, 2) != 2)
throw new IOException("Error reading IFO file");
int C_POST = (C_POST_buf[0] << 8) + C_POST_buf[1];
if (C_POST != 0)
C_POST += (pgcOffset * 2048) + pgcStart;
// get pgc's number of cells
if (fs.Seek((pgcOffset * 2048) + pgcStart + 3, SeekOrigin.Begin) != (pgcOffset * 2048) + pgcStart + 3)
throw new IOException("Error reading IFO file");
byte[] nCells_buf = new byte[1];
if (fs.Read(nCells_buf, 0, 1) != 1)
throw new IOException("Error reading IFO file");
int nCells = nCells_buf[0];
int nAngles = 1;
if (C_POST != 0 && C_PBKT != 0)
{
for (int nCell = 0; nCell < nCells; nCell++)
{
Cell c = new Cell();
// get info for cell
/*if (fs.Seek(C_PBKT + 24 * nCell, SeekOrigin.Begin) != C_PBKT + 24 * nCell)
throw new IOException("Error reading IFO file");
byte[] iCat_buf = new byte[1];
if (fs.Read(iCat_buf, 0, 1) != 1)
throw new IOException("Error reading IFO file");
// ignore multi-angle
int iCat = iCat_buf[0] & 0xF10;
// 0101=First; 1001=Middle ; 1101=Last
if (iCat == 0x50)
nAngles = 1;
else if (iCat == 0x90)
nAngles++;
else if (iCat == 0xD0)
{
nAngles++;
break;
}*/
#region read duration
if (fs.Seek(C_PBKT + 24 * nCell + 4, SeekOrigin.Begin) != C_PBKT + 24 * nCell + 4)
throw new IOException("Error reading IFO file");
playbackTimeData = new byte[4];
if (fs.Read(playbackTimeData, 0, 4) != 4)
throw new IOException("Error reading IFO file");
playbackTime = (playbackTimeData[0] << 24) + (playbackTimeData[1] << 16) + (playbackTimeData[2] << 8) + playbackTimeData[3];
hour = ((playbackTime >> 28) & 0x0f) * 10 + ((playbackTime >> 24) & 0x0f);
min = ((playbackTime >> 20) & 0x0f) * 10 + ((playbackTime >> 16) & 0x0f);
sec = ((playbackTime >> 12) & 0x0f) * 10 + ((playbackTime >> 8) & 0x0f);
frame = (((playbackTime >> 4) & 0x0f) * 10 + (playbackTime & 0x0f)) - 120;
c.Duration = hour * 3600 + min * 60 + sec;
c.PartialFrames = frame;
#endregion
#region entry point
if (fs.Seek(C_PBKT + 24 * nCell + 8, SeekOrigin.Begin) != C_PBKT + 24 * nCell + 8)
throw new IOException("Error reading IFO file");
byte[] entryPointBuf = new byte[4];
if (fs.Read(entryPointBuf, 0, 4) != 4)
throw new IOException("Error reading IFO file");
c.FirstSector = (entryPointBuf[0] << 24) + (entryPointBuf[1] << 16) + (entryPointBuf[2] << 8) + entryPointBuf[3];
#endregion
#region last sector
if (fs.Seek(C_PBKT + 24 * nCell + 20, SeekOrigin.Begin) != C_PBKT + 24 * nCell + 20)
throw new IOException("Error reading IFO file");
byte[] lastSectorBuf = new byte[4];
if (fs.Read(lastSectorBuf, 0, 4) != 4)
throw new IOException("Error reading IFO file");
c.LastSector = (lastSectorBuf[0] << 24) + (lastSectorBuf[1] << 16) + (lastSectorBuf[2] << 8) + lastSectorBuf[3];
#endregion
if (fs.Seek(C_POST + 4 * nCell, SeekOrigin.Begin) != C_POST + 4 * nCell)
throw new IOException("Error reading IFO file");
byte[] vobIDBuf = new byte[2];
if (fs.Read(vobIDBuf, 0, 2) != 2)
throw new IOException("Error reading IFO file");
c.VobID = (vobIDBuf[0] << 8) + vobIDBuf[1];
if (fs.Seek(C_POST + 4 * nCell + 3, SeekOrigin.Begin) != C_POST + 4 * nCell + 3)
throw new IOException("Error reading IFO file");
byte[] cellIDBuf = new byte[1];
if (fs.Read(cellIDBuf, 0, 1) != 1)
throw new IOException("Error reading IFO file");
c.CellID = cellIDBuf[0];
pgc.Cells.Add(c);
}
}
pgc.Angles = nAngles;
m_pgcs.Add(pgc);
}
#endregion
#region Video
if(fs.Seek(0x200, SeekOrigin.Begin) != 0x200)
throw new IOException("Error reading IFO file");
byte[] videoAttributes = new byte[2];
if(fs.Read(videoAttributes, 0, 2) != 2)
throw new IOException("Error reading IFO file");
byte va1 = videoAttributes[0];
byte va2 = videoAttributes[1];
// video mode
if(((va1 & 0x30) >> 4) == 0)
m_videoMode = "NTSC";
else if(((va1 & 0x30) >> 4) == 1)
m_videoMode = "PAL";
// aspect
if(((va1 & 0xC) >> 2) == 0)
m_aspectRatio = "4:3";
else if(((va1 & 0xC) >> 2) == 3)
m_aspectRatio = "16:9";
// resolution
int resolution = ((va2 & 0x38) >> 3);
switch(resolution)
{
case 0:
m_resolution = "720x480";
break;
case 1:
m_resolution = "704x480";
break;
case 2:
m_resolution = "352x480";
break;
case 3:
m_resolution = "352x240";
break;
}
#endregion
#region Audio
// http://dvd.sourceforge.net/dvdinfo/ifo.html
// find count of audio streams
if (fs.Seek(0x203, SeekOrigin.Begin) != 0x203)
throw new IOException("Error reading IFO file");
byte[] numAudio = new byte[1];
if(fs.Read(numAudio, 0, 1) != 1)
throw new IOException("Error reading IFO file");
m_numAudio = numAudio[0];
for (int i = 0; i < m_numAudio; i++)
{
byte[] audioFormatData = new byte[8];
if (fs.Read(audioFormatData, 0, 8) != 8)
throw new IOException("Error reading IFO file");
// check if the language is 'specified'
string lang = "";
if (((audioFormatData[0] & 0xC) >> 2) > 0)
{
// extract the two character ASCII code for the language
char langID1 = (char)audioFormatData[2];
char langID2 = (char)audioFormatData[3];
lang = String.Format("{0}{1}", (char)audioFormatData[2], (char)audioFormatData[3]);
// translate some language codes
//http://www.dvd-replica.com/DVD/vtslanguage.php
if (lang == "en")
lang = "English";
else if (lang == "es")
lang = "Spanish";
else if (lang == "fr")
lang = "French";
else if (lang == "ja")
lang = "Japanese";
else if (lang == "ar")
lang = "Arabic";
else if (lang == "no")
lang = "Norwegian";
else if (lang == "pl")
lang = "Polish";
else if (lang == "km")
lang = "Cambodian";
else if (lang == "pt")
lang = "Portuguese";
else if (lang == "zh")
lang = "Chinese";
else if (lang == "ro")
lang = "Romanian";
else if (lang == "ru")
lang = "Russian";
else if (lang == "cs")
lang = "Czech";
else if (lang == "da")
lang = "Danish";
else if (lang == "nl")
lang = "Dutch";
else if (lang == "sa")
lang = "Sanskrit";
else if (lang == "eo")
lang = "Esperanto";
else if (lang == "fi")
lang = "Finnish";
else if (lang == "de")
lang = "German";
else if (lang == "el")
lang = "Greek";
else if (lang == "sw")
lang = "Swahili";
else if (lang == "sv")
lang = "Swedish";
else if (lang == "iw")
lang = "Hebrew";
else if (lang == "hi")
lang = "Hindi";
else if (lang == "th")
lang = "Thai";
else if (lang == "ga")
lang = "Irish";
else if (lang == "it")
lang = "Italian";
else if (lang == "vi")
lang = "Vietnamese";
else if (lang == "ko")
lang = "Korean";
else if (lang == "ji")
lang = "Yiddish";
else if (lang == "la")
lang = "Latin";
else if (audioFormatData[2] == 0)
lang = "";
int audioType = audioFormatData[5];
switch (audioType)
{
case 0:
case 1:
case 2: //for visually impaired
break;
case 3: //director's comments
case 4: //alt. director's comments
lang += "/c";
break;
}
}
// display the number of channels in the audio stream
int channels = (audioFormatData[1] & 0x7) + 1;
if (lang != "")
lang += " ";
lang += String.Format("{0}ch", channels);
// find the format for the stream and add it to the format list if it is supported
int audioFormat = (audioFormatData[0] & 0xe0) >> 5;
switch (audioFormat)
{
case 0:
m_audioFormat.Add(new AudioFormatDetails("AC3", lang, 0x80 + i));
break;
case 2:
m_audioFormat.Add(new AudioFormatDetails("MPEG1", lang, 0xC0 + i));
break;
case 4:
m_audioFormat.Add(new AudioFormatDetails("LPCM", lang, 0xA0 + i));
break;
case 6: // DTS
case 3: // MPEG 2
default:
// ignore other stream types (don't add to the list)
//throw new IOException("This DVD contains an unsupported audio type");
//m_audioFormat.Add(new AudioFormatDetails("DTS", lang, 0x88 + i));
break;
}
}
#endregion
#region VOB File info
// VTS_##_0.IFO -> VTS_##_1.VOB
string vobFile = filename.Replace("0.IFO", "1.VOB");
int idx = vobFile.LastIndexOf("_");
if (idx == -1)
{
// got a straight vob file like "TRAILER.VOB"
VOB v = new VOB(filename.ToUpper().Replace("IFO", "VOB"));
m_vobs.Add(v);
}
else
{
string vobBase = "";
int vobNum = 0;
long nextSectors = 0;
// extract the VOB index # into vobNum
// vobBase will contain the title's index
vobBase = vobFile.Substring(0, idx); // up to last _
vobNum = Convert.ToInt32(vobFile.Substring(idx + 1, 1));
if (vobNum == 0)
vobNum++;
while (true)
{
vobFile = vobBase + "_" + vobNum.ToString("0") + ".VOB";
vobNum++;
if (File.Exists(vobFile) == false)
break;
VOB v = new VOB(vobFile);
v.FirstSector = nextSectors;
nextSectors += v.Sectors;
m_vobs.Add(v);
}
}
#endregion
}
finally
{
fs.Close();
}
}
public int AudioStreamCount
{
get
{
return(m_numAudio);
}
}
public List<ProgramChain> ProgramChains
{
get
{
return (m_pgcs);
}
}
public List<VOB> VOBs
{
get
{
return (m_vobs);
}
}
public int ProgramChainCount
{
get
{
return(m_pgcs.Count);
}
}
public string AspectRatio
{
get
{
return(m_aspectRatio);
}
}
public string Resolution
{
get
{
return(m_resolution);
}
}
public string VideoMode
{
get
{
return(m_videoMode);
}
}
public List<AudioFormatDetails> AudioFormat
{
get
{
return (m_audioFormat);
}
}
public string Filename
{
get
{
return (m_file.FullName);
}
}
public FileInfo FileInfo
{
get
{
return (m_file);
}
}
public int Length
{
get
{
int duration = 0;
int partialFrames = 0;
foreach (ProgramChain pgc in m_pgcs)
{
duration += pgc.Duration;
partialFrames += pgc.PartialFrames;
}
duration += partialFrames % m_pgcs[0].FrameRate;
return (duration);
}
}
}
}