Exemplo n.º 1
0
            /// <summary>
            /// Adds a new video frame from a byte array.
            /// </summary>
            /// <param name="pixels">The pixels to be saved. The row-major array is of size Width * Height in 8-bit mode and 2 * Width * Height in little-endian 16-bit mode.</param>
            /// <param name="compress">True if the frame is to be compressed. Please note that compression is CPU and I/O intensive and may not work at high frame rates. Use wisely.</param>
            /// <param name="imageData">The format of the pixels - 8 bit or 16 bit.</param>
            /// <param name="timeStamp">The high accuracy timestamp for the middle of the frame. If the timestamp is not with an accuracy of 1ms then set it as zero. A lower accuracy timestamp can be specified in the SystemTime status value.</param>
            /// <param name="exposureIn10thMilliseconds">The duration of the frame in whole 0.1 ms as determined by the high accuracy timestamping. If high accuracy timestamp is not available then set this to zero. Note that the Shutter status value should be derived from the camera settings rather than from the timestamps.</param>
            /// <param name="metadata">The status metadata to be saved with the video frame.</param>
            public void AddVideoFrame(byte[] pixels, bool compress, AdvImageData imageData, AdvTimeStamp timeStamp, uint exposureIn10thMilliseconds, AdvStatusEntry metadata)
            {
                BeginVideoFrame(timeStamp, exposureIn10thMilliseconds, metadata);

                if (imageData == AdvImageData.PixelDepth16Bit)
                {
                    byte layoutIdForCurrentFramerate = compress ? CFG_ADV_LAYOUT_3_COMPRESSED : CFG_ADV_LAYOUT_1_UNCOMPRESSED;
                    AdvLib.Obsolete.AdvVer1.FrameAddImageBytes(layoutIdForCurrentFramerate, pixels, 16);
                }
                else if (imageData == AdvImageData.PixelDepth8Bit)
                {
                    byte layoutIdForCurrentFramerate = compress ? CFG_ADV_LAYOUT_5_COMPRESSED : CFG_ADV_LAYOUT_4_UNCOMPRESSED;
                    AdvLib.Obsolete.AdvVer1.FrameAddImageBytes(layoutIdForCurrentFramerate, pixels, 8);
                }

                AdvLib.Obsolete.AdvVer1.EndFrame();
            }
Exemplo n.º 2
0
        public void Initialize(Action <int, int> progressCallback)
        {
            Task.Run(() =>
            {
                progressCallback(0, m_Aav.CountFrames);

                var aavFile = AdvFile.OpenFile(m_Aav.FileName);
                aavFile.Close();

                // This is the system timestamp retrieved by the system when the frame was saved to disk.
                var SYSTEM_TIME_TAG = aavFile.StatusSection.TagDefinitions.SingleOrDefault(x => x.Name == "SystemTime");

                // This is the system timestamp retrieved by the system when the frame was received.
                var SYSTEM_TIME_FT_TAG = aavFile.StatusSection.TagDefinitions.SingleOrDefault(x => x.Name == "SystemTimeFileTime");

                // This is the OCR-ed time with the full precision (0.1 ms for IOTA-VTI) rather than only the 1 ms from the AAVv1 timestamp
                var OCR_TIME_TAG = aavFile.StatusSection.TagDefinitions.SingleOrDefault(x => x.Name == "OcrTime");

                // This is the NTP timestamp retrieved by the system when the frame was received. It has been corrected for the configured 'Calibration Correction' in OccuRec
                var NTP_TIME_TAG = aavFile.StatusSection.TagDefinitions.SingleOrDefault(x => x.Name == "NTPEndTimestamp");

                var NTP_ERROR_TAG   = aavFile.StatusSection.TagDefinitions.SingleOrDefault(x => x.Name == "NTPTimestampError");
                var OCR_DEBUG_1_TAG = aavFile.StatusSection.TagDefinitions.SingleOrDefault(x => x.Name == "StartFrameTimestamp");
                var OCR_DEBUG_2_TAG = aavFile.StatusSection.TagDefinitions.SingleOrDefault(x => x.Name == "EndFrameTimestamp");

                var CPU_USAGE_TAG   = aavFile.StatusSection.TagDefinitions.SingleOrDefault(x => x.Name == "CpuUtilisation");
                var DISK_USAGE_TAG  = aavFile.StatusSection.TagDefinitions.SingleOrDefault(x => x.Name == "DisksUtilisation");
                var FREE_MEMORY_TAG = aavFile.StatusSection.TagDefinitions.SingleOrDefault(x => x.Name == "FreeMemoryMb");

                float maxDeltaSys   = 0;
                float minDeltaSys   = 0;
                float maxDeltaSysF  = 0;
                float minDeltaSysF  = 0;
                float maxDeltaNtp   = 0;
                float minDeltaNtp   = 0;
                float maxNtpError   = 0;
                float minNtpError   = 0;
                float maxFreeMemory = 0;
                float minFreeMemory = float.MaxValue;
                float maxDiskUsage  = 0;

                using (FileStream file = new FileStream(m_Aav.FileName, FileMode.Open, FileAccess.Read, FileShare.Read))
                    using (BinaryReader reader = new BinaryReader(file))
                    {
                        int i = 0;
                        string tagVal;
                        List <TimeAnalyserEntry> entriesWithNoUtil = new List <TimeAnalyserEntry>();

                        foreach (var idx in aavFile.Index.Index)
                        {
                            file.Seek(idx.Offset, SeekOrigin.Begin);

                            uint frameDataMagic = reader.ReadUInt32();
                            Trace.Assert(frameDataMagic == 0xEE0122FF);

                            byte[] data = reader.ReadBytes((int)idx.Length);

                            // Read the timestamp and exposure
                            long frameTimeMsSince2010 =
                                (long)data[0] + (((long)data[1]) << 8) + (((long)data[2]) << 16) + (((long)data[3]) << 24) +
                                (((long)data[4]) << 32) + (((long)data[5]) << 40) + (((long)data[6]) << 48) + (((long)data[7]) << 56);
                            int exposure = data[8] + (data[9] << 8) + (data[10] << 16) + (data[11] << 24);

                            if (frameTimeMsSince2010 == 0)
                            {
                                // First or last AAV frame
                                continue;
                            }

                            var entry              = new TimeAnalyserEntry();
                            int dataOffset         = 12;
                            AdvImageData imageData = null;
                            int sectionDataLength  = data[dataOffset] + (data[dataOffset + 1] << 8) + (data[dataOffset + 2] << 16) + (data[dataOffset + 3] << 24);
                            if (sectionDataLength > 2)
                            {
                                imageData = (AdvImageData)aavFile.ImageSection.GetDataFromDataBytes(data, null, sectionDataLength, dataOffset + 4);
                            }
                            dataOffset += sectionDataLength + 4;

                            sectionDataLength           = data[dataOffset] + (data[dataOffset + 1] << 8) + (data[dataOffset + 2] << 16) + (data[dataOffset + 3] << 24);
                            AdvStatusData statusSection = (AdvStatusData)aavFile.StatusSection.GetDataFromDataBytes(data, null, sectionDataLength, dataOffset + 4);

                            entry.FrameNo    = i;
                            entry.ExposureMs = (float)(exposure / 10.0);

                            if (OCR_TIME_TAG != null && statusSection.TagValues.TryGetValue(OCR_TIME_TAG, out tagVal))
                            {
                                entry.ReferenceTime = new DateTime(long.Parse(tagVal));
                            }
                            else
                            {
                                entry.ReferenceTime = AdvFile.ADV_ZERO_DATE_REF.AddMilliseconds(frameTimeMsSince2010).AddMilliseconds(-1 * entry.ExposureMs / 2);
                            }

                            long referenceTimeTicks = entry.ReferenceTime.Ticks;

                            if (SYSTEM_TIME_TAG != null && statusSection.TagValues.TryGetValue(SYSTEM_TIME_TAG, out tagVal))
                            {
                                entry.SystemTime = AdvFile.ADV_ZERO_DATE_REF.AddMilliseconds(long.Parse(tagVal)).AddMilliseconds(m_CorrSystemTimeMs);
                                if (entry.SystemTime.Day + 1 == entry.ReferenceTime.Day)
                                {
                                    // Fixing a date-change bug in OccuRec (var 1)
                                    entry.ReferenceTime = entry.ReferenceTime.AddDays(-1);
                                    referenceTimeTicks  = entry.ReferenceTime.Ticks;
                                }
                                if (entry.SystemTime.Day == entry.ReferenceTime.Day && entry.ReferenceTime.Hour - entry.SystemTime.Hour == 23)
                                {
                                    // Fixing a date-change bug in OccuRec (var 2)
                                    entry.ReferenceTime = entry.ReferenceTime.AddDays(-1);
                                    referenceTimeTicks  = entry.ReferenceTime.Ticks;
                                }
                                entry.DeltaSystemTimeMs = (float)new TimeSpan(entry.SystemTime.Ticks - referenceTimeTicks).TotalMilliseconds;
                            }
                            if (SYSTEM_TIME_FT_TAG != null && statusSection.TagValues.TryGetValue(SYSTEM_TIME_FT_TAG, out tagVal))
                            {
                                // SystemTimeAsFileTime has a microsecond precision. However the IOTA-VTI reference time precision is only 0.1 ms
                                // so here we are rounding the SystemTimeAsFileTime value to the nearest 0.1 ms for correctness
                                entry.SystemTimeFileTime    = new DateTime(1000 * (long.Parse(tagVal) / 1000)).AddMilliseconds(m_CorrSystemTimeMs);
                                entry.DeltaSystemFileTimeMs = (float)new TimeSpan(entry.SystemTimeFileTime.Ticks - referenceTimeTicks).TotalMilliseconds;
                            }
                            if (NTP_TIME_TAG != null && statusSection.TagValues.TryGetValue(NTP_TIME_TAG, out tagVal))
                            {
                                entry.NTPTime        = AdvFile.ADV_ZERO_DATE_REF.AddMilliseconds(long.Parse(tagVal)).AddMilliseconds(m_CorrNtpTimeMs);
                                entry.DeltaNTPTimeMs = (float)new TimeSpan(entry.NTPTime.Ticks - referenceTimeTicks).TotalMilliseconds;
                            }
                            if (NTP_ERROR_TAG != null && statusSection.TagValues.TryGetValue(NTP_ERROR_TAG, out tagVal))
                            {
                                entry.NTPErrorMs = 3 * int.Parse(tagVal) / 10.0f; // Value recorded in 0.1MS, converted to MS and then taken as 3-Sigma
                            }

                            Entries.Add(entry);

                            if (imageData != null)
                            {
                                entry.DebugImage = BitmapFilter.ToVideoFields(Pixelmap.ConstructBitmapFromBitmapPixels(imageData.ImageData));
                                entry.IsOutlier  = true;

                                if (OCR_DEBUG_1_TAG != null && statusSection.TagValues.TryGetValue(OCR_DEBUG_1_TAG, out tagVal))
                                {
                                    entry.OrcField1 = tagVal;
                                }
                                if (OCR_DEBUG_2_TAG != null && statusSection.TagValues.TryGetValue(OCR_DEBUG_2_TAG, out tagVal))
                                {
                                    entry.OrcField2 = tagVal;
                                }

                                DebugFrames.Add(entry);
                            }
                            else
                            {
                                int maxOutlier = 2000;
                                int minOutlier = -2000;

                                if (entry.DeltaSystemTimeMs > maxDeltaSys)
                                {
                                    if (entry.DeltaSystemTimeMs < maxOutlier)
                                    {
                                        maxDeltaSys = entry.DeltaSystemTimeMs;
                                    }
                                    else
                                    {
                                        entry.IsOutlier = true;
                                        Trace.WriteLine("SystemTime Outlier: " + entry.DeltaSystemTimeMs);
                                    }
                                }
                                if (entry.DeltaSystemTimeMs < minDeltaSys)
                                {
                                    if (entry.DeltaSystemTimeMs > minOutlier)
                                    {
                                        minDeltaSys = entry.DeltaSystemTimeMs;
                                    }
                                    else
                                    {
                                        entry.IsOutlier = true;
                                        Trace.WriteLine("SystemTime Outlier: " + entry.DeltaSystemTimeMs);
                                    }
                                }
                                if (entry.DeltaSystemFileTimeMs > maxDeltaSysF)
                                {
                                    if (entry.DeltaSystemFileTimeMs < maxOutlier)
                                    {
                                        maxDeltaSysF = entry.DeltaSystemFileTimeMs;
                                    }
                                    else
                                    {
                                        entry.IsOutlier = true;
                                        Trace.WriteLine("SystemFileTime Outlier: " + entry.DeltaSystemFileTimeMs);
                                    }
                                }
                                if (entry.DeltaSystemFileTimeMs < minDeltaSysF)
                                {
                                    if (entry.DeltaSystemFileTimeMs > minOutlier)
                                    {
                                        minDeltaSysF = entry.DeltaSystemFileTimeMs;
                                    }
                                    else
                                    {
                                        entry.IsOutlier = true;
                                        Trace.WriteLine("SystemFileTime Outlier: " + entry.DeltaSystemFileTimeMs);
                                    }
                                }
                                if (entry.DeltaNTPTimeMs > maxDeltaNtp)
                                {
                                    if (entry.DeltaNTPTimeMs < maxOutlier)
                                    {
                                        maxDeltaNtp = entry.DeltaNTPTimeMs;
                                    }
                                    else
                                    {
                                        entry.IsOutlier = true;
                                        //Trace.WriteLine("NTPTime Outlier: " + entry.DeltaNTPTimeMs);
                                    }
                                }
                                if (entry.DeltaNTPTimeMs < minDeltaNtp)
                                {
                                    if (entry.DeltaNTPTimeMs > minOutlier)
                                    {
                                        minDeltaNtp = entry.DeltaNTPTimeMs;
                                    }
                                    else
                                    {
                                        entry.IsOutlier = true;
                                        //Trace.WriteLine("NTPTime Outlier: " + entry.DeltaNTPTimeMs);
                                    }
                                }
                                if (entry.NTPErrorMs > maxNtpError)
                                {
                                    if (entry.NTPErrorMs < maxOutlier)
                                    {
                                        maxNtpError = entry.NTPErrorMs;
                                    }
                                    else
                                    {
                                        entry.IsOutlier = true;
                                        //Trace.WriteLine("NtpError Outlier: " + entry.NTPErrorMs);
                                    }
                                }
                                if (-entry.NTPErrorMs < minNtpError)
                                {
                                    if (-entry.NTPErrorMs > minOutlier)
                                    {
                                        minNtpError = -entry.NTPErrorMs;
                                    }
                                    else
                                    {
                                        entry.IsOutlier = true;
                                        //Trace.WriteLine("NtpError Outlier: " + -entry.NTPErrorMs);
                                    }
                                }
                            }

                            if (CPU_USAGE_TAG != null && statusSection.TagValues.TryGetValue(CPU_USAGE_TAG, out tagVal))
                            {
                                float cpuUsage   = float.Parse(tagVal, CultureInfo.InvariantCulture);
                                float diskUsage  = 0;
                                float freeMemory = 0;
                                if (DISK_USAGE_TAG != null && statusSection.TagValues.TryGetValue(DISK_USAGE_TAG, out tagVal))
                                {
                                    diskUsage = float.Parse(tagVal, CultureInfo.InvariantCulture);
                                    if (maxDiskUsage < diskUsage)
                                    {
                                        maxDiskUsage = diskUsage;
                                    }
                                }

                                if (FREE_MEMORY_TAG != null && statusSection.TagValues.TryGetValue(FREE_MEMORY_TAG, out tagVal))
                                {
                                    freeMemory = float.Parse(tagVal, CultureInfo.InvariantCulture);
                                    if (maxFreeMemory < freeMemory)
                                    {
                                        maxFreeMemory = freeMemory;
                                    }
                                    if (minFreeMemory > freeMemory)
                                    {
                                        minFreeMemory = freeMemory;
                                    }
                                }
                                var currUtilisationEntry = new SystemUtilisationEntry()
                                {
                                    CpuUtilisation = cpuUsage, DiskUtilisation = diskUsage, FreeMemory = freeMemory
                                };
                                SystemUtilisation.Add(currUtilisationEntry);
                                entriesWithNoUtil.ForEach(x => x.UtilisationEntry = currUtilisationEntry);
                                entriesWithNoUtil.Clear();
                            }
                            entriesWithNoUtil.Add(entry);

                            i++;
                            if (i % 100 == 0)
                            {
                                progressCallback(i - m_Aav.FirstFrame, m_Aav.CountFrames);
                            }
                        }
                    }

                Trace.WriteLine(string.Format("MinDeltaSys: {0:0.0}, MaxDeltaSys: {1:0.0}", minDeltaSys, maxDeltaSys));
                Trace.WriteLine(string.Format("MinDeltaSysF: {0:0.0}, MaxDeltaSysF: {1:0.0}", minDeltaSysF, maxDeltaSysF));
                Trace.WriteLine(string.Format("MinDeltaNtp: {0:0.0}, MaxDeltaNtp: {1:0.0}", minDeltaNtp, maxDeltaNtp));

                MinDeltaNTPMs            = minDeltaNtp;
                MaxDeltaNTPMs            = maxDeltaNtp;
                MinDeltaNTPErrorMs       = minNtpError;
                MaxDeltaNTPErrorMs       = maxNtpError;
                MinDeltaSystemTimeMs     = minDeltaSys;
                MaxDeltaSystemTimeMs     = maxDeltaSys;
                MinDeltaSystemFileTimeMs = minDeltaSysF;
                MaxDeltaSystemFileTimeMs = maxDeltaSysF;
                MinFreeMemoryMb          = minFreeMemory;
                MaxFreeMemoryMb          = maxFreeMemory;
                MaxDiskUsage             = maxDiskUsage;

                if (Entries.Count > 0)
                {
                    FromDateTime = Entries.First().ReferenceTime;
                    ToDateTime   = Entries.Last().ReferenceTime;
                }

                var logFileName = Path.ChangeExtension(m_Aav.FileName, ".log");
                if (File.Exists(logFileName))
                {
                    ExtractNTPLogData(logFileName);
                }

                var meinbergFileName = Path.GetFullPath(Path.GetDirectoryName(m_Aav.FileName) + @"\ntsmadvlog.txt");
                if (File.Exists(meinbergFileName))
                {
                    ExtractMeinbergLogData(meinbergFileName);
                }

                progressCallback(0, 0);
            });
        }