/// <summary> /// Load the entire partition from disk (when not yet loaded) /// </summary> public void Load() { if (!this.IsLoaded) { MXFKLVFactory klvFactory = new MXFKLVFactory(); using (MXFReader reader = new MXFReader(this.File.Filename)) { // Seek just after this partition reader.Seek(this.DataOffset + this.Length); while (!reader.EOF) { MXFKLV klv = klvFactory.CreateObject(reader, this); if (klv.Key.Type == KeyType.Partition || klv.Key.Type == KeyType.RIP || klv.Key.Type == KeyType.PrimerPack) { break; // Next partition or other segment, quit reading } if (!this.Children.Any(a => a.Offset == klv.Offset)) { // Normal, just add the new child this.AddChild(klv); } // Next KLV please reader.Seek(klv.DataOffset + klv.Length); } } this.IsLoaded = true; } }
/// <summary> /// Try to locate the RIP /// </summary> private bool ReadRIP(MXFKLVFactory klvFactory) { if (this.RIP == null) { // Read the last 4 bytes of the file m_reader.Seek(this.Filesize - 4); uint ripSize = m_reader.ReadD(); if (ripSize < this.Filesize && ripSize >= 4) // At least 4 bytes { m_reader.Seek(this.Filesize - ripSize); MXFKLV klv = klvFactory.CreateObject(m_reader, null); if (klv.Key.Type == KeyType.RIP) { // Yes, RIP found this.AddChild(klv); this.RIP = klv as MXFRIP; return(true); } } } return(false); }
/// <summary> /// Fully Parse an MXF file /// </summary> protected void ParseFull(BackgroundWorker worker) { Stopwatch sw = Stopwatch.StartNew(); MXFKLVFactory klvFactory = new MXFKLVFactory(); MXFPartition currentPartition = null; int previousPercentage = 0; Dictionary <UInt16, MXFEntryPrimer> allPrimerKeys = null; int[] counters = new int[Enum.GetNames(typeof(KeyType)).Length]; using (m_reader = new MXFReader(this.Filename)) { this.Filesize = m_reader.Size; MXFObject partitions = new MXFNamedObject("Partitions", 0); this.AddChild(partitions); int partitionNumber = 0; // For easy partition identification while (!m_reader.EOF) { MXFKLV klv = klvFactory.CreateObject(m_reader, currentPartition); // Update overall counters if (klv.Key.Type == KeyType.None) { counters[(int)klv.Key.Type]++; } // Process the new KLV ProcessKLVObject(klv, partitions, ref currentPartition, ref partitionNumber, ref allPrimerKeys); // Next KLV please m_reader.Seek(klv.DataOffset + klv.Length); // Only report progress when the percentage has changed int currentPercentage = (int)((m_reader.Position * 90) / m_reader.Size); if (currentPercentage != previousPercentage) { worker.ReportProgress(currentPercentage, "Parsing MXF file"); previousPercentage = currentPercentage; } } } // Progress should now be 90% // Update all type descriptions klvFactory.UpdateAllTypeDescriptions(allPrimerKeys); worker.ReportProgress(91, "Creating key list"); Debug.WriteLine("Finished parsing file '{0}' in {1} ms", this.Filename, sw.ElapsedMilliseconds); sw.Restart(); // Create a list with all UID keys Dictionary <string, MXFObject> allKeys = new Dictionary <string, MXFObject>(); CreateKeyList(allKeys, this); worker.ReportProgress(92, "Creating resolving references"); // Resolve the references ResolveReferences(allKeys, this); Debug.WriteLine("Finished resolving references in {0} ms", sw.ElapsedMilliseconds); sw.Restart(); worker.ReportProgress(93, "Resolving flatlist"); this.FlatList = new List <MXFObject>(); this.AddToList(this.FlatList); Debug.WriteLine("Flatlist created in {0} ms", sw.ElapsedMilliseconds); sw.Restart(); worker.ReportProgress(94, "Creating Logical tree"); // Create the logical tree CreateLogicalTree(); Debug.WriteLine("Logical tree created in {0} ms", sw.ElapsedMilliseconds); sw.Restart(); // And Execute ALL test this.ExecuteValidationTest(worker, true); // Finished worker.ReportProgress(100, "Finished"); }
/// <summary> /// Partially Parse an MXF file, skip all data /// </summary> protected void ParsePartial(BackgroundWorker worker) { Stopwatch sw = Stopwatch.StartNew(); MXFKLVFactory klvFactory = new MXFKLVFactory(); MXFPartition currentPartition = null; int previousPercentage = 0; Dictionary <UInt16, MXFEntryPrimer> allPrimerKeys = null; int[] counters = new int[Enum.GetNames(typeof(KeyType)).Length]; PartialSeekMode seekMode = PartialSeekMode.Unknown; using (m_reader = new MXFReader(this.Filename)) { this.Filesize = m_reader.Size; MXFObject partitions = new MXFNamedObject("Partitions", 0); this.AddChild(partitions); // Start with trying to find the RIP bool ripFound = ReadRIP(klvFactory); if (ripFound) { seekMode = PartialSeekMode.UsingRIP; } m_reader.Seek(0); // Start at the beginning // Start by reading the first partition int partitionNumber = 0; // For easy partition identification while (!m_reader.EOF && seekMode != PartialSeekMode.Backwards) // Eof and NOT searching backwards { MXFKLV klv = klvFactory.CreateObject(m_reader, currentPartition); // Update overall counters if (klv.Key.Type == KeyType.None) { counters[(int)klv.Key.Type]++; } if (klv.Key.Type == KeyType.Partition && seekMode == PartialSeekMode.Backwards) { if (this.Partitions.Exists(a => a.Offset == klv.Offset)) { // A new partition has been found that we already found, quit the main loop break; } } // Process the new KLV ProcessKLVObject(klv, partitions, ref currentPartition, ref partitionNumber, ref allPrimerKeys); // If we found the second partition long nextSeekPosition = klv.DataOffset + klv.Length; if (partitionNumber >= 2) // Header fully read, now busy with the second partition { switch (seekMode) { case PartialSeekMode.UsingRIP: // And we already found the RIP if (currentPartition.FirstSystemItem != null) // And we found the first system item { MXFEntryRIP ripEntry = this.RIP.GetPartition(partitionNumber); if (ripEntry != null) { // Mark the current partition as not-completely read currentPartition.IsLoaded = false; // Start at the next partition nextSeekPosition = (long)ripEntry.PartitionOffset; } } break; case PartialSeekMode.Backwards: // NO RIP, searching backwards // Backwards, jump to the PREVIOUS partition if (currentPartition.FirstSystemItem != null) // And we found the first system item { // Jump to the previous partition if (currentPartition.PreviousPartition != 0) { // And we haven't found this partition yet if (!this.Partitions.Exists(a => a.ThisPartition == currentPartition.PreviousPartition)) { nextSeekPosition = (long)currentPartition.PreviousPartition; // Jump to previous } } } break; case PartialSeekMode.Unknown: // No RIP.... // Hmmm, RIP is not found, check if we have a footer partition somewhere MXFPartition part = this.Partitions.Where(a => a.FooterPartition != 0).FirstOrDefault(); if (part != null) { // If we are already at the footer, don't bother to seek if (currentPartition.Offset != (long)part.FooterPartition) { nextSeekPosition = (long)part.FooterPartition; // Start at the footer seekMode = PartialSeekMode.Backwards; } } break; } } // Next KLV please m_reader.Seek(nextSeekPosition); // Only report progress when the percentage has changed int currentPercentage = (int)((m_reader.Position * 90) / m_reader.Size); if (currentPercentage != previousPercentage) { worker.ReportProgress(currentPercentage, "Partial Parsing MXF file"); previousPercentage = currentPercentage; } } } // Progress should now be 90% // Update all type descriptions klvFactory.UpdateAllTypeDescriptions(allPrimerKeys); Debug.WriteLine("Finished parsing file '{0}' in {1} ms", this.Filename, sw.ElapsedMilliseconds); sw.Restart(); // Create a list with all UID keys worker.ReportProgress(91, "Creating key list"); Dictionary <string, MXFObject> allKeys = new Dictionary <string, MXFObject>(); CreateKeyList(allKeys, this); // Resolve the references worker.ReportProgress(92, "Resolving references"); ResolveReferences(allKeys, this); Debug.WriteLine("Finished resolving references in {0} ms", sw.ElapsedMilliseconds); sw.Restart(); this.FlatList = new List <MXFObject>(); worker.ReportProgress(93, "Creating flat list"); this.AddToList(this.FlatList); Debug.WriteLine("Flatlist created in {0} ms", sw.ElapsedMilliseconds); sw.Restart(); // Create the logical tree worker.ReportProgress(94, "Creating logical tree"); CreateLogicalTree(); Debug.WriteLine("Logical tree created in {0} ms", sw.ElapsedMilliseconds); sw.Restart(); // And Execute FAST tests this.ExecuteValidationTest(worker, false); // Finished worker.ReportProgress(100, "Finished"); }
/// <summary> /// Fully Parse an MXF file /// </summary> protected void ParseFull(BackgroundWorker worker) { Stopwatch sw = Stopwatch.StartNew(); MXFKLVFactory klvFactory = new MXFKLVFactory(); MXFPartition currentPartition = null; int previousPercentage = 0; Dictionary <UInt16, MXFEntryPrimer> allPrimerKeys = null; //int[] counters = new int[Enum.GetNames(typeof(KeyType)).Length]; using (m_reader = new MXFReader(this.Filename)) { this.Filesize = m_reader.Size; MXFObject partitions = new MXFNamedObject("Partitions", 0); this.AddChild(partitions); int partitionNumber = 0; // For easy partition identification while (!m_reader.EOF) { try { MXFKLV klv = klvFactory.CreateObject(m_reader, currentPartition); //// Update overall counters //if (klv.Key.Type == KeyType.None) // counters[(int)klv.Key.Type]++; // Process the new KLV ProcessKLVObject(klv, partitions, ref currentPartition, ref partitionNumber, ref allPrimerKeys); // Next KLV please m_reader.Seek(klv.DataOffset + klv.Length); } catch (Exception e) { m_reader.SeekForNextPotentialKey(); } // Only report progress when the percentage has changed int currentPercentage = (int)((m_reader.Position * 90) / m_reader.Size); if (currentPercentage != previousPercentage) { worker.ReportProgress(currentPercentage, "Parsing MXF file"); previousPercentage = currentPercentage; } } } Debug.WriteLine("Finished parsing file '{0}' in {1} ms", this.Filename, sw.ElapsedMilliseconds); // Progress should now be 90% DoPostWork(worker, sw, allPrimerKeys); // And Execute ALL test sw.Restart(); this.ExecuteValidationTest(worker, true); Debug.WriteLine("Tests executed in {0} ms", sw.ElapsedMilliseconds); // Finished worker.ReportProgress(100, "Finished"); }