TSPlaylistFile.cs 43 KB

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