/// <summary> /// Constructor. Header of the given RIFF stream will be read and processed here. /// </summary> /// <param name="stream">Stream.</param> public RiffParser(AtomicBinaryReader reader) { this.reader = reader; nextElementOffset = 0; // do the most basic file type check long p = 0; streamRiff = reader.ReadUInt32 (ref p); if (streamRiff != RIFF4CC && streamRiff != RIFX4CC) { throw new RiffParserException ("Error. Not a valid RIFF stream"); } }
/// <summary> /// Constructor. Header of the given RIFF stream will be read and processed here. /// </summary> /// <param name="stream">Stream.</param> public RiffParser(AtomicBinaryReader reader) { this.reader = reader; nextElementOffset = 0; // do the most basic file type check long p = 0; streamRiff = reader.ReadUInt32(ref p); if (streamRiff != RIFF4CC && streamRiff != RIFX4CC) { throw new RiffParserException("Error. Not a valid RIFF stream"); } }
public override void Init(Stream sourceStream, LoadOptions loadOptions = null) { // skip the video if asked not to load it if (loadOptions != null && loadOptions.skipVideo) return; // check the arguments if (sourceStream == null) { throw new System.ArgumentException ("sourceStream is required"); } // measure load time var watch = new System.Diagnostics.Stopwatch (); watch.Start (); reader = new AtomicBinaryReader (sourceStream); // for detecting the buffer size int maxRawJpgSize = 0; // the stream can't be seeked unless there is an index. create it frameStartIndex.Clear (); frameSize.Clear (); long markerCount = 0; long startIndex = -1; bool markerStart = false; int bytesRead = -1; long i = 0; var buffer = new byte[FILE_READ_BUFFER_SIZE]; // read the file in chunks (more than 30x faster than reading by byte) long p = 0; do { bytesRead = reader.Read (ref p, buffer, 0, FILE_READ_BUFFER_SIZE); for (int j = 0; j < bytesRead; j++) { byte b = buffer [j]; // wait for marker start if (b == 0xFF) { markerStart = true; } else if (markerStart) { // read the other marker byte and decide what to do switch (b) { case 0xD8: // Start of image startIndex = i + j - 1; break; case 0xD9: // End of image frameStartIndex.Add (startIndex); int size = (int)(i + j - startIndex + 1); if (size > maxRawJpgSize) maxRawJpgSize = size; frameSize.Add (size); //Debug.Log("Found frame OFFS: " + startIndex + " SIZE: " + size); break; } markerStart = false; markerCount++; } } i += bytesRead; } while(bytesRead >= FILE_READ_BUFFER_SIZE); // create a buffer for holding raw jpg data when decoding a frame rawJpgBuffer = new byte[maxRawJpgSize]; watch.Stop (); #if MP_DEBUG Debug.Log("Recreated index for raw MJPEG stream in " + (watch.Elapsed.TotalMilliseconds * 0.001f) + " seconds." + "Frames: " + frameStartIndex.Count + ". Max jpg size: " + maxRawJpgSize + ". Markers: " + markerCount); #endif // set all the info about the video stream we know if (loadOptions != null && loadOptions.videoStreamInfo != null) { videoStreamInfo = loadOptions.videoStreamInfo; } else { videoStreamInfo = new VideoStreamInfo (); videoStreamInfo.codecFourCC = VideoDecoderMJPEG.FOURCC_MJPG; } videoStreamInfo.frameCount = frameSize.Count; videoStreamInfo.lengthBytes = reader.StreamLength; }