TSPlaylistFile.cs 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284
  1. //============================================================================
  2. // BDInfo - Blu-ray Video and Audio Analysis Tool
  3. // Copyright © 2010 Cinema Squid
  4. //
  5. // This library is free software; you can redistribute it and/or
  6. // modify it under the terms of the GNU Lesser General Public
  7. // License as published by the Free Software Foundation; either
  8. // version 2.1 of the License, or (at your option) any later version.
  9. //
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. // Lesser General Public License for more details.
  14. //
  15. // You should have received a copy of the GNU Lesser General Public
  16. // License along with this library; if not, write to the Free Software
  17. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. //=============================================================================
  19. #undef DEBUG
  20. using System;
  21. using System.Collections.Generic;
  22. using System.IO;
  23. using MediaBrowser.Model.IO;
  24. namespace BDInfo
  25. {
  26. public class TSPlaylistFile
  27. {
  28. private readonly IFileSystem _fileSystem;
  29. private FileSystemMetadata FileInfo = null;
  30. public string FileType = null;
  31. public bool IsInitialized = false;
  32. public string Name = null;
  33. public BDROM BDROM = null;
  34. public bool HasHiddenTracks = false;
  35. public bool HasLoops = false;
  36. public bool IsCustom = false;
  37. public List<double> Chapters = new List<double>();
  38. public Dictionary<ushort, TSStream> Streams =
  39. new Dictionary<ushort, TSStream>();
  40. public Dictionary<ushort, TSStream> PlaylistStreams =
  41. new Dictionary<ushort, TSStream>();
  42. public List<TSStreamClip> StreamClips =
  43. new List<TSStreamClip>();
  44. public List<Dictionary<ushort, TSStream>> AngleStreams =
  45. new List<Dictionary<ushort, TSStream>>();
  46. public List<Dictionary<double, TSStreamClip>> AngleClips =
  47. new List<Dictionary<double, TSStreamClip>>();
  48. public int AngleCount = 0;
  49. public List<TSStream> SortedStreams =
  50. new List<TSStream>();
  51. public List<TSVideoStream> VideoStreams =
  52. new List<TSVideoStream>();
  53. public List<TSAudioStream> AudioStreams =
  54. new List<TSAudioStream>();
  55. public List<TSTextStream> TextStreams =
  56. new List<TSTextStream>();
  57. public List<TSGraphicsStream> GraphicsStreams =
  58. new List<TSGraphicsStream>();
  59. public TSPlaylistFile(BDROM bdrom,
  60. FileSystemMetadata fileInfo, IFileSystem fileSystem)
  61. {
  62. BDROM = bdrom;
  63. FileInfo = fileInfo;
  64. _fileSystem = fileSystem;
  65. Name = fileInfo.Name.ToUpper();
  66. }
  67. public TSPlaylistFile(BDROM bdrom,
  68. string name,
  69. List<TSStreamClip> clips, IFileSystem fileSystem)
  70. {
  71. BDROM = bdrom;
  72. Name = name;
  73. _fileSystem = fileSystem;
  74. IsCustom = true;
  75. foreach (var clip in clips)
  76. {
  77. var newClip = new TSStreamClip(
  78. clip.StreamFile, clip.StreamClipFile);
  79. newClip.Name = clip.Name;
  80. newClip.TimeIn = clip.TimeIn;
  81. newClip.TimeOut = clip.TimeOut;
  82. newClip.Length = newClip.TimeOut - newClip.TimeIn;
  83. newClip.RelativeTimeIn = TotalLength;
  84. newClip.RelativeTimeOut = newClip.RelativeTimeIn + newClip.Length;
  85. newClip.AngleIndex = clip.AngleIndex;
  86. newClip.Chapters.Add(clip.TimeIn);
  87. StreamClips.Add(newClip);
  88. if (newClip.AngleIndex > AngleCount)
  89. {
  90. AngleCount = newClip.AngleIndex;
  91. }
  92. if (newClip.AngleIndex == 0)
  93. {
  94. Chapters.Add(newClip.RelativeTimeIn);
  95. }
  96. }
  97. LoadStreamClips();
  98. IsInitialized = true;
  99. }
  100. public override string ToString()
  101. {
  102. return Name;
  103. }
  104. public ulong InterleavedFileSize
  105. {
  106. get
  107. {
  108. ulong size = 0;
  109. foreach (var clip in StreamClips)
  110. {
  111. size += clip.InterleavedFileSize;
  112. }
  113. return size;
  114. }
  115. }
  116. public ulong FileSize
  117. {
  118. get
  119. {
  120. ulong size = 0;
  121. foreach (var clip in StreamClips)
  122. {
  123. size += clip.FileSize;
  124. }
  125. return size;
  126. }
  127. }
  128. public double TotalLength
  129. {
  130. get
  131. {
  132. double length = 0;
  133. foreach (var clip in StreamClips)
  134. {
  135. if (clip.AngleIndex == 0)
  136. {
  137. length += clip.Length;
  138. }
  139. }
  140. return length;
  141. }
  142. }
  143. public double TotalAngleLength
  144. {
  145. get
  146. {
  147. double length = 0;
  148. foreach (var clip in StreamClips)
  149. {
  150. length += clip.Length;
  151. }
  152. return length;
  153. }
  154. }
  155. public ulong TotalSize
  156. {
  157. get
  158. {
  159. ulong size = 0;
  160. foreach (var clip in StreamClips)
  161. {
  162. if (clip.AngleIndex == 0)
  163. {
  164. size += clip.PacketSize;
  165. }
  166. }
  167. return size;
  168. }
  169. }
  170. public ulong TotalAngleSize
  171. {
  172. get
  173. {
  174. ulong size = 0;
  175. foreach (var clip in StreamClips)
  176. {
  177. size += clip.PacketSize;
  178. }
  179. return size;
  180. }
  181. }
  182. public ulong TotalBitRate
  183. {
  184. get
  185. {
  186. if (TotalLength > 0)
  187. {
  188. return (ulong)Math.Round(((TotalSize * 8.0) / TotalLength));
  189. }
  190. return 0;
  191. }
  192. }
  193. public ulong TotalAngleBitRate
  194. {
  195. get
  196. {
  197. if (TotalAngleLength > 0)
  198. {
  199. return (ulong)Math.Round(((TotalAngleSize * 8.0) / TotalAngleLength));
  200. }
  201. return 0;
  202. }
  203. }
  204. public void Scan(
  205. Dictionary<string, TSStreamFile> streamFiles,
  206. Dictionary<string, TSStreamClipFile> streamClipFiles)
  207. {
  208. Stream fileStream = null;
  209. BinaryReader fileReader = null;
  210. try
  211. {
  212. Streams.Clear();
  213. StreamClips.Clear();
  214. fileStream = _fileSystem.OpenRead(FileInfo.FullName);
  215. fileReader = new BinaryReader(fileStream);
  216. byte[] data = new byte[fileStream.Length];
  217. int dataLength = fileReader.Read(data, 0, data.Length);
  218. int pos = 0;
  219. FileType = ReadString(data, 8, ref pos);
  220. if (FileType != "MPLS0100" && FileType != "MPLS0200")
  221. {
  222. throw new Exception(string.Format(
  223. "Playlist {0} has an unknown file type {1}.",
  224. FileInfo.Name, FileType));
  225. }
  226. int playlistOffset = ReadInt32(data, ref pos);
  227. int chaptersOffset = ReadInt32(data, ref pos);
  228. int extensionsOffset = ReadInt32(data, ref pos);
  229. pos = playlistOffset;
  230. int playlistLength = ReadInt32(data, ref pos);
  231. int playlistReserved = ReadInt16(data, ref pos);
  232. int itemCount = ReadInt16(data, ref pos);
  233. int subitemCount = ReadInt16(data, ref pos);
  234. var chapterClips = new List<TSStreamClip>();
  235. for (int itemIndex = 0; itemIndex < itemCount; itemIndex++)
  236. {
  237. int itemStart = pos;
  238. int itemLength = ReadInt16(data, ref pos);
  239. string itemName = ReadString(data, 5, ref pos);
  240. string itemType = ReadString(data, 4, ref pos);
  241. TSStreamFile streamFile = null;
  242. string streamFileName = string.Format(
  243. "{0}.M2TS", itemName);
  244. if (streamFiles.ContainsKey(streamFileName))
  245. {
  246. streamFile = streamFiles[streamFileName];
  247. }
  248. if (streamFile == null)
  249. {
  250. // Error condition
  251. }
  252. TSStreamClipFile streamClipFile = null;
  253. string streamClipFileName = string.Format(
  254. "{0}.CLPI", itemName);
  255. if (streamClipFiles.ContainsKey(streamClipFileName))
  256. {
  257. streamClipFile = streamClipFiles[streamClipFileName];
  258. }
  259. if (streamClipFile == null)
  260. {
  261. throw new Exception(string.Format(
  262. "Playlist {0} referenced missing file {1}.",
  263. FileInfo.Name, streamFileName));
  264. }
  265. pos += 1;
  266. int multiangle = (data[pos] >> 4) & 0x01;
  267. int condition = data[pos] & 0x0F;
  268. pos += 2;
  269. int inTime = ReadInt32(data, ref pos);
  270. if (inTime < 0) inTime &= 0x7FFFFFFF;
  271. double timeIn = (double)inTime / 45000;
  272. int outTime = ReadInt32(data, ref pos);
  273. if (outTime < 0) outTime &= 0x7FFFFFFF;
  274. double timeOut = (double)outTime / 45000;
  275. var streamClip = new TSStreamClip(
  276. streamFile, streamClipFile);
  277. streamClip.Name = streamFileName; //TODO
  278. streamClip.TimeIn = timeIn;
  279. streamClip.TimeOut = timeOut;
  280. streamClip.Length = streamClip.TimeOut - streamClip.TimeIn;
  281. streamClip.RelativeTimeIn = TotalLength;
  282. streamClip.RelativeTimeOut = streamClip.RelativeTimeIn + streamClip.Length;
  283. StreamClips.Add(streamClip);
  284. chapterClips.Add(streamClip);
  285. pos += 12;
  286. if (multiangle > 0)
  287. {
  288. int angles = data[pos];
  289. pos += 2;
  290. for (int angle = 0; angle < angles - 1; angle++)
  291. {
  292. string angleName = ReadString(data, 5, ref pos);
  293. string angleType = ReadString(data, 4, ref pos);
  294. pos += 1;
  295. TSStreamFile angleFile = null;
  296. string angleFileName = string.Format(
  297. "{0}.M2TS", angleName);
  298. if (streamFiles.ContainsKey(angleFileName))
  299. {
  300. angleFile = streamFiles[angleFileName];
  301. }
  302. if (angleFile == null)
  303. {
  304. throw new Exception(string.Format(
  305. "Playlist {0} referenced missing angle file {1}.",
  306. FileInfo.Name, angleFileName));
  307. }
  308. TSStreamClipFile angleClipFile = null;
  309. string angleClipFileName = string.Format(
  310. "{0}.CLPI", angleName);
  311. if (streamClipFiles.ContainsKey(angleClipFileName))
  312. {
  313. angleClipFile = streamClipFiles[angleClipFileName];
  314. }
  315. if (angleClipFile == null)
  316. {
  317. throw new Exception(string.Format(
  318. "Playlist {0} referenced missing angle file {1}.",
  319. FileInfo.Name, angleClipFileName));
  320. }
  321. var angleClip =
  322. new TSStreamClip(angleFile, angleClipFile);
  323. angleClip.AngleIndex = angle + 1;
  324. angleClip.TimeIn = streamClip.TimeIn;
  325. angleClip.TimeOut = streamClip.TimeOut;
  326. angleClip.RelativeTimeIn = streamClip.RelativeTimeIn;
  327. angleClip.RelativeTimeOut = streamClip.RelativeTimeOut;
  328. angleClip.Length = streamClip.Length;
  329. StreamClips.Add(angleClip);
  330. }
  331. if (angles - 1 > AngleCount) AngleCount = angles - 1;
  332. }
  333. int streamInfoLength = ReadInt16(data, ref pos);
  334. pos += 2;
  335. int streamCountVideo = data[pos++];
  336. int streamCountAudio = data[pos++];
  337. int streamCountPG = data[pos++];
  338. int streamCountIG = data[pos++];
  339. int streamCountSecondaryAudio = data[pos++];
  340. int streamCountSecondaryVideo = data[pos++];
  341. int streamCountPIP = data[pos++];
  342. pos += 5;
  343. #if DEBUG
  344. Debug.WriteLine(string.Format(
  345. "{0} : {1} -> V:{2} A:{3} PG:{4} IG:{5} 2A:{6} 2V:{7} PIP:{8}",
  346. Name, streamFileName, streamCountVideo, streamCountAudio, streamCountPG, streamCountIG,
  347. streamCountSecondaryAudio, streamCountSecondaryVideo, streamCountPIP));
  348. #endif
  349. for (int i = 0; i < streamCountVideo; i++)
  350. {
  351. var stream = CreatePlaylistStream(data, ref pos);
  352. if (stream != null) PlaylistStreams[stream.PID] = stream;
  353. }
  354. for (int i = 0; i < streamCountAudio; i++)
  355. {
  356. var stream = CreatePlaylistStream(data, ref pos);
  357. if (stream != null) PlaylistStreams[stream.PID] = stream;
  358. }
  359. for (int i = 0; i < streamCountPG; i++)
  360. {
  361. var stream = CreatePlaylistStream(data, ref pos);
  362. if (stream != null) PlaylistStreams[stream.PID] = stream;
  363. }
  364. for (int i = 0; i < streamCountIG; i++)
  365. {
  366. var stream = CreatePlaylistStream(data, ref pos);
  367. if (stream != null) PlaylistStreams[stream.PID] = stream;
  368. }
  369. for (int i = 0; i < streamCountSecondaryAudio; i++)
  370. {
  371. var stream = CreatePlaylistStream(data, ref pos);
  372. if (stream != null) PlaylistStreams[stream.PID] = stream;
  373. pos += 2;
  374. }
  375. for (int i = 0; i < streamCountSecondaryVideo; i++)
  376. {
  377. var stream = CreatePlaylistStream(data, ref pos);
  378. if (stream != null) PlaylistStreams[stream.PID] = stream;
  379. pos += 6;
  380. }
  381. /*
  382. * TODO
  383. *
  384. for (int i = 0; i < streamCountPIP; i++)
  385. {
  386. TSStream stream = CreatePlaylistStream(data, ref pos);
  387. if (stream != null) PlaylistStreams[stream.PID] = stream;
  388. }
  389. */
  390. pos += itemLength - (pos - itemStart) + 2;
  391. }
  392. pos = chaptersOffset + 4;
  393. int chapterCount = ReadInt16(data, ref pos);
  394. for (int chapterIndex = 0;
  395. chapterIndex < chapterCount;
  396. chapterIndex++)
  397. {
  398. int chapterType = data[pos + 1];
  399. if (chapterType == 1)
  400. {
  401. int streamFileIndex =
  402. ((int)data[pos + 2] << 8) + data[pos + 3];
  403. long chapterTime =
  404. ((long)data[pos + 4] << 24) +
  405. ((long)data[pos + 5] << 16) +
  406. ((long)data[pos + 6] << 8) +
  407. ((long)data[pos + 7]);
  408. var streamClip = chapterClips[streamFileIndex];
  409. double chapterSeconds = (double)chapterTime / 45000;
  410. double relativeSeconds =
  411. chapterSeconds -
  412. streamClip.TimeIn +
  413. streamClip.RelativeTimeIn;
  414. // TODO: Ignore short last chapter?
  415. if (TotalLength - relativeSeconds > 1.0)
  416. {
  417. streamClip.Chapters.Add(chapterSeconds);
  418. this.Chapters.Add(relativeSeconds);
  419. }
  420. }
  421. else
  422. {
  423. // TODO: Handle other chapter types?
  424. }
  425. pos += 14;
  426. }
  427. }
  428. finally
  429. {
  430. if (fileReader != null)
  431. {
  432. fileReader.Dispose();
  433. }
  434. if (fileStream != null)
  435. {
  436. fileStream.Dispose();
  437. }
  438. }
  439. }
  440. public void Initialize()
  441. {
  442. LoadStreamClips();
  443. var clipTimes = new Dictionary<string, List<double>>();
  444. foreach (var clip in StreamClips)
  445. {
  446. if (clip.AngleIndex == 0)
  447. {
  448. if (clipTimes.ContainsKey(clip.Name))
  449. {
  450. if (clipTimes[clip.Name].Contains(clip.TimeIn))
  451. {
  452. HasLoops = true;
  453. break;
  454. }
  455. else
  456. {
  457. clipTimes[clip.Name].Add(clip.TimeIn);
  458. }
  459. }
  460. else
  461. {
  462. clipTimes[clip.Name] = new List<double> { clip.TimeIn };
  463. }
  464. }
  465. }
  466. ClearBitrates();
  467. IsInitialized = true;
  468. }
  469. protected TSStream CreatePlaylistStream(byte[] data, ref int pos)
  470. {
  471. TSStream stream = null;
  472. int start = pos;
  473. int headerLength = data[pos++];
  474. int headerPos = pos;
  475. int headerType = data[pos++];
  476. int pid = 0;
  477. int subpathid = 0;
  478. int subclipid = 0;
  479. switch (headerType)
  480. {
  481. case 1:
  482. pid = ReadInt16(data, ref pos);
  483. break;
  484. case 2:
  485. subpathid = data[pos++];
  486. subclipid = data[pos++];
  487. pid = ReadInt16(data, ref pos);
  488. break;
  489. case 3:
  490. subpathid = data[pos++];
  491. pid = ReadInt16(data, ref pos);
  492. break;
  493. case 4:
  494. subpathid = data[pos++];
  495. subclipid = data[pos++];
  496. pid = ReadInt16(data, ref pos);
  497. break;
  498. default:
  499. break;
  500. }
  501. pos = headerPos + headerLength;
  502. int streamLength = data[pos++];
  503. int streamPos = pos;
  504. var streamType = (TSStreamType)data[pos++];
  505. switch (streamType)
  506. {
  507. case TSStreamType.MVC_VIDEO:
  508. // TODO
  509. break;
  510. case TSStreamType.AVC_VIDEO:
  511. case TSStreamType.MPEG1_VIDEO:
  512. case TSStreamType.MPEG2_VIDEO:
  513. case TSStreamType.VC1_VIDEO:
  514. var videoFormat = (TSVideoFormat)
  515. (data[pos] >> 4);
  516. var frameRate = (TSFrameRate)
  517. (data[pos] & 0xF);
  518. var aspectRatio = (TSAspectRatio)
  519. (data[pos + 1] >> 4);
  520. stream = new TSVideoStream();
  521. ((TSVideoStream)stream).VideoFormat = videoFormat;
  522. ((TSVideoStream)stream).AspectRatio = aspectRatio;
  523. ((TSVideoStream)stream).FrameRate = frameRate;
  524. #if DEBUG
  525. Debug.WriteLine(string.Format(
  526. "\t{0} {1} {2} {3} {4}",
  527. pid,
  528. streamType,
  529. videoFormat,
  530. frameRate,
  531. aspectRatio));
  532. #endif
  533. break;
  534. case TSStreamType.AC3_AUDIO:
  535. case TSStreamType.AC3_PLUS_AUDIO:
  536. case TSStreamType.AC3_PLUS_SECONDARY_AUDIO:
  537. case TSStreamType.AC3_TRUE_HD_AUDIO:
  538. case TSStreamType.DTS_AUDIO:
  539. case TSStreamType.DTS_HD_AUDIO:
  540. case TSStreamType.DTS_HD_MASTER_AUDIO:
  541. case TSStreamType.DTS_HD_SECONDARY_AUDIO:
  542. case TSStreamType.LPCM_AUDIO:
  543. case TSStreamType.MPEG1_AUDIO:
  544. case TSStreamType.MPEG2_AUDIO:
  545. int audioFormat = ReadByte(data, ref pos);
  546. var channelLayout = (TSChannelLayout)
  547. (audioFormat >> 4);
  548. var sampleRate = (TSSampleRate)
  549. (audioFormat & 0xF);
  550. string audioLanguage = ReadString(data, 3, ref pos);
  551. stream = new TSAudioStream();
  552. ((TSAudioStream)stream).ChannelLayout = channelLayout;
  553. ((TSAudioStream)stream).SampleRate = TSAudioStream.ConvertSampleRate(sampleRate);
  554. ((TSAudioStream)stream).LanguageCode = audioLanguage;
  555. #if DEBUG
  556. Debug.WriteLine(string.Format(
  557. "\t{0} {1} {2} {3} {4}",
  558. pid,
  559. streamType,
  560. audioLanguage,
  561. channelLayout,
  562. sampleRate));
  563. #endif
  564. break;
  565. case TSStreamType.INTERACTIVE_GRAPHICS:
  566. case TSStreamType.PRESENTATION_GRAPHICS:
  567. string graphicsLanguage = ReadString(data, 3, ref pos);
  568. stream = new TSGraphicsStream();
  569. ((TSGraphicsStream)stream).LanguageCode = graphicsLanguage;
  570. if (data[pos] != 0)
  571. {
  572. }
  573. #if DEBUG
  574. Debug.WriteLine(string.Format(
  575. "\t{0} {1} {2}",
  576. pid,
  577. streamType,
  578. graphicsLanguage));
  579. #endif
  580. break;
  581. case TSStreamType.SUBTITLE:
  582. int code = ReadByte(data, ref pos); // TODO
  583. string textLanguage = ReadString(data, 3, ref pos);
  584. stream = new TSTextStream();
  585. ((TSTextStream)stream).LanguageCode = textLanguage;
  586. #if DEBUG
  587. Debug.WriteLine(string.Format(
  588. "\t{0} {1} {2}",
  589. pid,
  590. streamType,
  591. textLanguage));
  592. #endif
  593. break;
  594. default:
  595. break;
  596. }
  597. pos = streamPos + streamLength;
  598. if (stream != null)
  599. {
  600. stream.PID = (ushort)pid;
  601. stream.StreamType = streamType;
  602. }
  603. return stream;
  604. }
  605. private void LoadStreamClips()
  606. {
  607. AngleClips.Clear();
  608. if (AngleCount > 0)
  609. {
  610. for (int angleIndex = 0; angleIndex < AngleCount; angleIndex++)
  611. {
  612. AngleClips.Add(new Dictionary<double, TSStreamClip>());
  613. }
  614. }
  615. TSStreamClip referenceClip = null;
  616. if (StreamClips.Count > 0)
  617. {
  618. referenceClip = StreamClips[0];
  619. }
  620. foreach (var clip in StreamClips)
  621. {
  622. if (clip.StreamClipFile.Streams.Count > referenceClip.StreamClipFile.Streams.Count)
  623. {
  624. referenceClip = clip;
  625. }
  626. else if (clip.Length > referenceClip.Length)
  627. {
  628. referenceClip = clip;
  629. }
  630. if (AngleCount > 0)
  631. {
  632. if (clip.AngleIndex == 0)
  633. {
  634. for (int angleIndex = 0; angleIndex < AngleCount; angleIndex++)
  635. {
  636. AngleClips[angleIndex][clip.RelativeTimeIn] = clip;
  637. }
  638. }
  639. else
  640. {
  641. AngleClips[clip.AngleIndex - 1][clip.RelativeTimeIn] = clip;
  642. }
  643. }
  644. }
  645. foreach (var clipStream
  646. in referenceClip.StreamClipFile.Streams.Values)
  647. {
  648. if (!Streams.ContainsKey(clipStream.PID))
  649. {
  650. var stream = clipStream.Clone();
  651. Streams[clipStream.PID] = stream;
  652. if (!IsCustom && !PlaylistStreams.ContainsKey(stream.PID))
  653. {
  654. stream.IsHidden = true;
  655. HasHiddenTracks = true;
  656. }
  657. if (stream.IsVideoStream)
  658. {
  659. VideoStreams.Add((TSVideoStream)stream);
  660. }
  661. else if (stream.IsAudioStream)
  662. {
  663. AudioStreams.Add((TSAudioStream)stream);
  664. }
  665. else if (stream.IsGraphicsStream)
  666. {
  667. GraphicsStreams.Add((TSGraphicsStream)stream);
  668. }
  669. else if (stream.IsTextStream)
  670. {
  671. TextStreams.Add((TSTextStream)stream);
  672. }
  673. }
  674. }
  675. if (referenceClip.StreamFile != null)
  676. {
  677. // TODO: Better way to add this in?
  678. if (BDInfoSettings.EnableSSIF &&
  679. referenceClip.StreamFile.InterleavedFile != null &&
  680. referenceClip.StreamFile.Streams.ContainsKey(4114) &&
  681. !Streams.ContainsKey(4114))
  682. {
  683. var stream = referenceClip.StreamFile.Streams[4114].Clone();
  684. Streams[4114] = stream;
  685. if (stream.IsVideoStream)
  686. {
  687. VideoStreams.Add((TSVideoStream)stream);
  688. }
  689. }
  690. foreach (var clipStream
  691. in referenceClip.StreamFile.Streams.Values)
  692. {
  693. if (Streams.ContainsKey(clipStream.PID))
  694. {
  695. var stream = Streams[clipStream.PID];
  696. if (stream.StreamType != clipStream.StreamType) continue;
  697. if (clipStream.BitRate > stream.BitRate)
  698. {
  699. stream.BitRate = clipStream.BitRate;
  700. }
  701. stream.IsVBR = clipStream.IsVBR;
  702. if (stream.IsVideoStream &&
  703. clipStream.IsVideoStream)
  704. {
  705. ((TSVideoStream)stream).EncodingProfile =
  706. ((TSVideoStream)clipStream).EncodingProfile;
  707. }
  708. else if (stream.IsAudioStream &&
  709. clipStream.IsAudioStream)
  710. {
  711. var audioStream = (TSAudioStream)stream;
  712. var clipAudioStream = (TSAudioStream)clipStream;
  713. if (clipAudioStream.ChannelCount > audioStream.ChannelCount)
  714. {
  715. audioStream.ChannelCount = clipAudioStream.ChannelCount;
  716. }
  717. if (clipAudioStream.LFE > audioStream.LFE)
  718. {
  719. audioStream.LFE = clipAudioStream.LFE;
  720. }
  721. if (clipAudioStream.SampleRate > audioStream.SampleRate)
  722. {
  723. audioStream.SampleRate = clipAudioStream.SampleRate;
  724. }
  725. if (clipAudioStream.BitDepth > audioStream.BitDepth)
  726. {
  727. audioStream.BitDepth = clipAudioStream.BitDepth;
  728. }
  729. if (clipAudioStream.DialNorm < audioStream.DialNorm)
  730. {
  731. audioStream.DialNorm = clipAudioStream.DialNorm;
  732. }
  733. if (clipAudioStream.AudioMode != TSAudioMode.Unknown)
  734. {
  735. audioStream.AudioMode = clipAudioStream.AudioMode;
  736. }
  737. if (clipAudioStream.CoreStream != null &&
  738. audioStream.CoreStream == null)
  739. {
  740. audioStream.CoreStream = (TSAudioStream)
  741. clipAudioStream.CoreStream.Clone();
  742. }
  743. }
  744. }
  745. }
  746. }
  747. for (int i = 0; i < AngleCount; i++)
  748. {
  749. AngleStreams.Add(new Dictionary<ushort, TSStream>());
  750. }
  751. if (!BDInfoSettings.KeepStreamOrder)
  752. {
  753. VideoStreams.Sort(CompareVideoStreams);
  754. }
  755. foreach (TSStream stream in VideoStreams)
  756. {
  757. SortedStreams.Add(stream);
  758. for (int i = 0; i < AngleCount; i++)
  759. {
  760. var angleStream = stream.Clone();
  761. angleStream.AngleIndex = i + 1;
  762. AngleStreams[i][angleStream.PID] = angleStream;
  763. SortedStreams.Add(angleStream);
  764. }
  765. }
  766. if (!BDInfoSettings.KeepStreamOrder)
  767. {
  768. AudioStreams.Sort(CompareAudioStreams);
  769. }
  770. foreach (TSStream stream in AudioStreams)
  771. {
  772. SortedStreams.Add(stream);
  773. }
  774. if (!BDInfoSettings.KeepStreamOrder)
  775. {
  776. GraphicsStreams.Sort(CompareGraphicsStreams);
  777. }
  778. foreach (TSStream stream in GraphicsStreams)
  779. {
  780. SortedStreams.Add(stream);
  781. }
  782. if (!BDInfoSettings.KeepStreamOrder)
  783. {
  784. TextStreams.Sort(CompareTextStreams);
  785. }
  786. foreach (TSStream stream in TextStreams)
  787. {
  788. SortedStreams.Add(stream);
  789. }
  790. }
  791. public void ClearBitrates()
  792. {
  793. foreach (var clip in StreamClips)
  794. {
  795. clip.PayloadBytes = 0;
  796. clip.PacketCount = 0;
  797. clip.PacketSeconds = 0;
  798. if (clip.StreamFile != null)
  799. {
  800. foreach (var stream in clip.StreamFile.Streams.Values)
  801. {
  802. stream.PayloadBytes = 0;
  803. stream.PacketCount = 0;
  804. stream.PacketSeconds = 0;
  805. }
  806. if (clip.StreamFile != null &&
  807. clip.StreamFile.StreamDiagnostics != null)
  808. {
  809. clip.StreamFile.StreamDiagnostics.Clear();
  810. }
  811. }
  812. }
  813. foreach (var stream in SortedStreams)
  814. {
  815. stream.PayloadBytes = 0;
  816. stream.PacketCount = 0;
  817. stream.PacketSeconds = 0;
  818. }
  819. }
  820. public bool IsValid
  821. {
  822. get
  823. {
  824. if (!IsInitialized) return false;
  825. if (BDInfoSettings.FilterShortPlaylists &&
  826. TotalLength < BDInfoSettings.FilterShortPlaylistsValue)
  827. {
  828. return false;
  829. }
  830. if (HasLoops &&
  831. BDInfoSettings.FilterLoopingPlaylists)
  832. {
  833. return false;
  834. }
  835. return true;
  836. }
  837. }
  838. public int CompareVideoStreams(
  839. TSVideoStream x,
  840. TSVideoStream y)
  841. {
  842. if (x == null && y == null)
  843. {
  844. return 0;
  845. }
  846. else if (x == null && y != null)
  847. {
  848. return 1;
  849. }
  850. else if (x != null && y == null)
  851. {
  852. return -1;
  853. }
  854. else
  855. {
  856. if (x.Height > y.Height)
  857. {
  858. return -1;
  859. }
  860. else if (y.Height > x.Height)
  861. {
  862. return 1;
  863. }
  864. else if (x.PID > y.PID)
  865. {
  866. return 1;
  867. }
  868. else if (y.PID > x.PID)
  869. {
  870. return -1;
  871. }
  872. else
  873. {
  874. return 0;
  875. }
  876. }
  877. }
  878. public int CompareAudioStreams(
  879. TSAudioStream x,
  880. TSAudioStream y)
  881. {
  882. if (x == y)
  883. {
  884. return 0;
  885. }
  886. else if (x == null && y == null)
  887. {
  888. return 0;
  889. }
  890. else if (x == null && y != null)
  891. {
  892. return -1;
  893. }
  894. else if (x != null && y == null)
  895. {
  896. return 1;
  897. }
  898. else
  899. {
  900. if (x.ChannelCount > y.ChannelCount)
  901. {
  902. return -1;
  903. }
  904. else if (y.ChannelCount > x.ChannelCount)
  905. {
  906. return 1;
  907. }
  908. else
  909. {
  910. int sortX = GetStreamTypeSortIndex(x.StreamType);
  911. int sortY = GetStreamTypeSortIndex(y.StreamType);
  912. if (sortX > sortY)
  913. {
  914. return -1;
  915. }
  916. else if (sortY > sortX)
  917. {
  918. return 1;
  919. }
  920. else
  921. {
  922. if (x.LanguageCode == "eng")
  923. {
  924. return -1;
  925. }
  926. else if (y.LanguageCode == "eng")
  927. {
  928. return 1;
  929. }
  930. else if (x.LanguageCode != y.LanguageCode)
  931. {
  932. return string.Compare(
  933. x.LanguageName, y.LanguageName);
  934. }
  935. else if (x.PID < y.PID)
  936. {
  937. return -1;
  938. }
  939. else if (y.PID < x.PID)
  940. {
  941. return 1;
  942. }
  943. return 0;
  944. }
  945. }
  946. }
  947. }
  948. public int CompareTextStreams(
  949. TSTextStream x,
  950. TSTextStream y)
  951. {
  952. if (x == y)
  953. {
  954. return 0;
  955. }
  956. else if (x == null && y == null)
  957. {
  958. return 0;
  959. }
  960. else if (x == null && y != null)
  961. {
  962. return -1;
  963. }
  964. else if (x != null && y == null)
  965. {
  966. return 1;
  967. }
  968. else
  969. {
  970. if (x.LanguageCode == "eng")
  971. {
  972. return -1;
  973. }
  974. else if (y.LanguageCode == "eng")
  975. {
  976. return 1;
  977. }
  978. else
  979. {
  980. if (x.LanguageCode == y.LanguageCode)
  981. {
  982. if (x.PID > y.PID)
  983. {
  984. return 1;
  985. }
  986. else if (y.PID > x.PID)
  987. {
  988. return -1;
  989. }
  990. else
  991. {
  992. return 0;
  993. }
  994. }
  995. else
  996. {
  997. return string.Compare(
  998. x.LanguageName, y.LanguageName);
  999. }
  1000. }
  1001. }
  1002. }
  1003. private int CompareGraphicsStreams(
  1004. TSGraphicsStream x,
  1005. TSGraphicsStream y)
  1006. {
  1007. if (x == y)
  1008. {
  1009. return 0;
  1010. }
  1011. else if (x == null && y == null)
  1012. {
  1013. return 0;
  1014. }
  1015. else if (x == null && y != null)
  1016. {
  1017. return -1;
  1018. }
  1019. else if (x != null && y == null)
  1020. {
  1021. return 1;
  1022. }
  1023. else
  1024. {
  1025. int sortX = GetStreamTypeSortIndex(x.StreamType);
  1026. int sortY = GetStreamTypeSortIndex(y.StreamType);
  1027. if (sortX > sortY)
  1028. {
  1029. return -1;
  1030. }
  1031. else if (sortY > sortX)
  1032. {
  1033. return 1;
  1034. }
  1035. else if (x.LanguageCode == "eng")
  1036. {
  1037. return -1;
  1038. }
  1039. else if (y.LanguageCode == "eng")
  1040. {
  1041. return 1;
  1042. }
  1043. else
  1044. {
  1045. if (x.LanguageCode == y.LanguageCode)
  1046. {
  1047. if (x.PID > y.PID)
  1048. {
  1049. return 1;
  1050. }
  1051. else if (y.PID > x.PID)
  1052. {
  1053. return -1;
  1054. }
  1055. else
  1056. {
  1057. return 0;
  1058. }
  1059. }
  1060. else
  1061. {
  1062. return string.Compare(x.LanguageName, y.LanguageName);
  1063. }
  1064. }
  1065. }
  1066. }
  1067. private int GetStreamTypeSortIndex(TSStreamType streamType)
  1068. {
  1069. switch (streamType)
  1070. {
  1071. case TSStreamType.Unknown:
  1072. return 0;
  1073. case TSStreamType.MPEG1_VIDEO:
  1074. return 1;
  1075. case TSStreamType.MPEG2_VIDEO:
  1076. return 2;
  1077. case TSStreamType.AVC_VIDEO:
  1078. return 3;
  1079. case TSStreamType.VC1_VIDEO:
  1080. return 4;
  1081. case TSStreamType.MVC_VIDEO:
  1082. return 5;
  1083. case TSStreamType.MPEG1_AUDIO:
  1084. return 1;
  1085. case TSStreamType.MPEG2_AUDIO:
  1086. return 2;
  1087. case TSStreamType.AC3_PLUS_SECONDARY_AUDIO:
  1088. return 3;
  1089. case TSStreamType.DTS_HD_SECONDARY_AUDIO:
  1090. return 4;
  1091. case TSStreamType.AC3_AUDIO:
  1092. return 5;
  1093. case TSStreamType.DTS_AUDIO:
  1094. return 6;
  1095. case TSStreamType.AC3_PLUS_AUDIO:
  1096. return 7;
  1097. case TSStreamType.DTS_HD_AUDIO:
  1098. return 8;
  1099. case TSStreamType.AC3_TRUE_HD_AUDIO:
  1100. return 9;
  1101. case TSStreamType.DTS_HD_MASTER_AUDIO:
  1102. return 10;
  1103. case TSStreamType.LPCM_AUDIO:
  1104. return 11;
  1105. case TSStreamType.SUBTITLE:
  1106. return 1;
  1107. case TSStreamType.INTERACTIVE_GRAPHICS:
  1108. return 2;
  1109. case TSStreamType.PRESENTATION_GRAPHICS:
  1110. return 3;
  1111. default:
  1112. return 0;
  1113. }
  1114. }
  1115. protected string ReadString(
  1116. byte[] data,
  1117. int count,
  1118. ref int pos)
  1119. {
  1120. string val = Encoding.ASCII.GetString(data, pos, count);
  1121. pos += count;
  1122. return val;
  1123. }
  1124. protected int ReadInt32(
  1125. byte[] data,
  1126. ref int pos)
  1127. {
  1128. int val =
  1129. ((int)data[pos] << 24) +
  1130. ((int)data[pos + 1] << 16) +
  1131. ((int)data[pos + 2] << 8) +
  1132. ((int)data[pos + 3]);
  1133. pos += 4;
  1134. return val;
  1135. }
  1136. protected int ReadInt16(
  1137. byte[] data,
  1138. ref int pos)
  1139. {
  1140. int val =
  1141. ((int)data[pos] << 8) +
  1142. ((int)data[pos + 1]);
  1143. pos += 2;
  1144. return val;
  1145. }
  1146. protected byte ReadByte(
  1147. byte[] data,
  1148. ref int pos)
  1149. {
  1150. return data[pos++];
  1151. }
  1152. }
  1153. }