TSPlaylistFile.cs 43 KB

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