// Create a box by reading the file and add it to "parent". public static Mpeg4Box Create(Mpeg4File file, long position, Mpeg4Box parent) { // Read the box header. Mpeg4BoxHeader header = new Mpeg4BoxHeader(file, position); // If we're not even valid, quit. if (!header.IsValid) { return(null); } // IF we're in a SampleDescriptionBox and haven'type loaded all the // entries, try loading an appropriate entry. if (parent.BoxType == "stsd" && parent.Children.Count < ((Mpeg4IsoSampleDescriptionBox)parent).EntryCount) { Mpeg4IsoHandlerBox handler = parent.Handler; if (handler != null && handler.HandlerType == "soun") { return(new Mpeg4IsoAudioSampleEntry(header, parent)); } else { return(new Mpeg4IsoSampleEntry(header, parent)); } } // // A bunch of standard items. // if (header.BoxType == "moov") { return(new Mpeg4IsoMovieBox(header, parent)); } if (header.BoxType == "mvhd") { return(new Mpeg4IsoMovieHeaderBox(header, parent)); } if (header.BoxType == "mdia") { return(new Mpeg4IsoMediaBox(header, parent)); } if (header.BoxType == "minf") { return(new Mpeg4IsoMediaInformationBox(header, parent)); } if (header.BoxType == "stbl") { return(new Mpeg4IsoSampleTableBox(header, parent)); } if (header.BoxType == "stsd") { return(new Mpeg4IsoSampleDescriptionBox(header, parent)); } if (header.BoxType == "stco") { return(new Mpeg4IsoChunkOffsetBox(header, parent)); } if (header.BoxType == "co64") { return(new Mpeg4IsoChunkLargeOffsetBox(header, parent)); } if (header.BoxType == "trak") { return(new Mpeg4IsoTrackBox(header, parent)); } if (header.BoxType == "hdlr") { return(new Mpeg4IsoHandlerBox(header, parent)); } if (header.BoxType == "udta") { return(new Mpeg4IsoUserDataBox(header, parent)); } if (header.BoxType == "meta") { return(new Mpeg4IsoMetaBox(header, parent)); } if (header.BoxType == "ilst") { return(new Mpeg4AppleItemListBox(header, parent)); } if (header.BoxType == "data") { return(new Mpeg4AppleDataBox(header, parent)); } if (header.BoxType == "esds") { return(new Mpeg4AppleElementaryStreamDescriptor(header, parent)); } if (header.BoxType == "free" || header.BoxType == "skip") { return(new Mpeg4IsoFreeSpaceBox(header, parent)); } if (header.BoxType == "mean" || header.BoxType == "name") { return(new Mpeg4AppleAdditionalInfoBox(header, parent)); } // If we still don'type have a tag, and we're inside an ItemLisBox, load // lthe box as an AnnotationBox (Apple tag item). if (parent.GetType() == typeof(Mpeg4AppleItemListBox)) { return(new Mpeg4AppleAnnotationBox(header, parent)); } // Nothing good. Go generic. return(new Mpeg4UnknownBox(header, parent)); }
// Read the file. private void Read(ReadStyle propertiesStyle) { // Create a dummie outer box, as perscribed by the specs. Mpeg4FileBox file_box = new Mpeg4FileBox(this); // Find the movie box and item text. If the movie box doen'type exist, an // exception will be thrown on the next call, but if there is no movie // box, the file can'type possibly be valid. Mpeg4IsoMovieBox moov_box = (Mpeg4IsoMovieBox)file_box.FindChildDeep("moov"); Mpeg4AppleItemListBox ilst_box = (Mpeg4AppleItemListBox)moov_box.FindChildDeep("ilst"); // If we have a ItemListBox, deparent it. if (ilst_box != null) { ilst_box.RemoveFromParent(); } // Create the tag. tag = new Mpeg4AppleTag(ilst_box, this); // If we're not reading properties, we're done. if (propertiesStyle == ReadStyle.None) { return; } // Get the movie header box. Mpeg4IsoMovieHeaderBox mvhd_box = (Mpeg4IsoMovieHeaderBox)moov_box.FindChildDeep("mvhd"); Mpeg4IsoAudioSampleEntry sample_entry = null; // Find a TrackBox with a sound Handler. foreach (Mpeg4Box box in moov_box.Children) { if (box.BoxType == "trak") { // If the handler isn'type sound, it could be metadata or video or // any number of other things. Mpeg4IsoHandlerBox hdlr_box = (Mpeg4IsoHandlerBox)box.FindChildDeep("hdlr"); if (hdlr_box == null || hdlr_box.HandlerType != "soun") { continue; } // This track SHOULD contain at least one sample entry. sample_entry = (Mpeg4IsoAudioSampleEntry)box.FindChildDeep(typeof(Mpeg4IsoAudioSampleEntry)); break; } } // If we have a MovieHeaderBox, deparent it. if (mvhd_box != null) { mvhd_box.RemoveFromParent(); } // If we have a SampleEntry, deparent it. if (sample_entry != null) { sample_entry.RemoveFromParent(); } // Read the properties. properties = new Mpeg4Properties(mvhd_box, sample_entry, propertiesStyle); }