private void LoadFileStructure(string fileName) { using (var file = new AdvFile2(fileName)) { var pixels = file.GetMainFramePixels(0); } }
public void TestPixelDeserialization(AdvSourceDataFormat dataFormat, byte dynaBits, CompressionType compression) { var fileGen = new AdvGenerator(); var cfg = new AdvGenerationConfig() { DynaBits = dynaBits, SourceFormat = dataFormat, NumberOfFrames = 1, Compression = compression, NormalPixelValue = null }; string fileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")); if (File.Exists(fileName)) { File.Delete(fileName); } AdvFile2 file = null; try { // Generate fileGen.GenerateaAdv_V2(cfg, fileName); var hasher = new Hasher(); string h1 = hasher.CalcMd5(fileName); Console.WriteLine("File hash for {0} at {1} bits: {2}", dataFormat, dynaBits, h1); // Verify using (file = new AdvFile2(fileName)) { uint[] pixels = file.GetMainFramePixels(0); var imageGenerator = new ImageGenerator(); var verified = imageGenerator.VerifyImagePattern1UInt32(pixels, cfg.DynaBits); Assert.IsTrue(verified); } } finally { try { if (file != null) { file.Close(); } if (File.Exists(fileName)) { File.Delete(fileName); } } catch (Exception ex) { Console.WriteLine(ex); Trace.WriteLine(ex); } } }
public frmAdv2Viewer(string fileName) { InitializeComponent(); m_FileName = fileName; m_AdvFile = new AdvFile2(fileName); }
public void Dispose() { if (m_AdvFile != null) { m_AdvFile.Close(); m_AdvFile = null; } }
public void TestReadingFileWithErrorMessageInTheStatusChannel() { var fileGen = new AdvGenerator(); var cfg = new AdvGenerationConfig() { DynaBits = 16, SourceFormat = AdvSourceDataFormat.Format16BitUShort, NumberOfFrames = 1, Compression = CompressionType.Uncompressed, NormalPixelValue = null, SystemErrorsCallback = delegate(int id) { return("Sample Error Message"); } }; string fileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")); if (File.Exists(fileName)) { File.Delete(fileName); } AdvFile2 file = null; try { // Generate fileGen.GenerateaAdv_V2(cfg, fileName); // Verify using (file = new AdvFile2(fileName)) { uint[] pixels = file.GetMainFramePixels(0); var imageGenerator = new ImageGenerator(); var verified = imageGenerator.VerifyImagePattern1UInt32(pixels, cfg.DynaBits); Assert.IsTrue(verified); } } finally { try { if (file != null) { file.Close(); } if (File.Exists(fileName)) { File.Delete(fileName); } } catch (Exception ex) { Console.WriteLine(ex); Trace.WriteLine(ex); } } }
/// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } if (m_AdvFile != null) { m_AdvFile.Dispose(); m_AdvFile = null; } base.Dispose(disposing); }
private void btnGenSpecExample_Click(object sender, EventArgs e) { string fileName = Path.GetFullPath(AppDomain.CurrentDomain.BaseDirectory + Path.DirectorySeparatorChar + @"SpecExample.adv.bin"); if (File.Exists(fileName)) { if (MessageBox.Show(string.Format("Output file exists:\r\n\r\n{0}\r\n\r\nOverwrite?", fileName), "Confirmation", MessageBoxButtons.YesNo) != DialogResult.Yes) { return; } File.Delete(fileName); } var advGen = new AdvGenerator(); advGen.GenerateSpecExampleFile(fileName); using (var loadedFile = new AdvFile2(fileName)) { Trace.WriteLine(loadedFile.Width); } }
public void TestMetadataTagsAreSavedAndReadCorrectly() { var fileGen = new AdvGenerator(); var cfg = BuildZeroTimestampConfig(AdvSourceDataFormat.Format16BitUShort, 16, CompressionType.Uncompressed); string fileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")); if (File.Exists(fileName)) { File.Delete(fileName); } try { // Generate cfg.MainStreamMetadata.Add("Name1", "Христо"); cfg.MainStreamMetadata.Add("Name2", "Frédéric"); cfg.CalibrationStreamMetadata.Add("Name3", "好的茶"); cfg.UserMetadata.Add("User0", "1234\rabcd\r\n"); cfg.UserMetadata.Add("UserArb", "1 قيمة"); cfg.UserMetadata.Add("UserChi", "值1"); cfg.UserMetadata.Add("UserCyr", "Значение 1"); cfg.UserMetadata.Add("UserGal", "કિંમત 1"); cfg.UserMetadata.Add("UserGre", "αξία 1"); cfg.UserMetadata.Add("UserHeb", "1 ערך"); cfg.UserMetadata.Add("UserHin", "मान 1"); cfg.UserMetadata.Add("UserJpn", "値1"); cfg.UserMetadata.Add("UserKan", "ಮೌಲ್ಯ 1"); cfg.UserMetadata.Add("UserKaz", "мән 1"); cfg.UserMetadata.Add("UserKor", "값 1"); cfg.UserMetadata.Add("UserMal", "മൂല്യം 1"); cfg.UserMetadata.Add("UserMar", "मूल्य 1"); cfg.UserMetadata.Add("UserPer", "1 ارزش"); cfg.UserMetadata.Add("UserTel", "విలువ 1"); cfg.UserMetadata.Add("UserUrd", "قیمت 1"); cfg.UserMetadata.Add("UserViet", "giá trị 1"); fileGen.GenerateaAdv_V2(cfg, fileName); // Verify using (var loadedFile = new AdvFile2(fileName)) { Assert.AreEqual(2, loadedFile.MainSteamInfo.MetadataTags.Count); Assert.IsTrue(loadedFile.MainSteamInfo.MetadataTags.ContainsKey("Name1")); Assert.AreEqual("Христо", loadedFile.MainSteamInfo.MetadataTags["Name1"]); Assert.IsTrue(loadedFile.MainSteamInfo.MetadataTags.ContainsKey("Name2")); Assert.AreEqual("Frédéric", loadedFile.MainSteamInfo.MetadataTags["Name2"]); Assert.AreEqual(1, loadedFile.CalibrationSteamInfo.MetadataTags.Count); Assert.IsTrue(loadedFile.CalibrationSteamInfo.MetadataTags.ContainsKey("Name3")); Assert.AreEqual("好的茶", loadedFile.CalibrationSteamInfo.MetadataTags["Name3"]); Assert.AreEqual(18, loadedFile.UserMetadataTags.Count); Assert.IsTrue(loadedFile.UserMetadataTags.ContainsKey("User0")); Assert.AreEqual("1234\rabcd\r\n", loadedFile.UserMetadataTags["User0"]); Assert.IsTrue(loadedFile.UserMetadataTags.ContainsKey("UserArb")); Assert.AreEqual("1 قيمة", loadedFile.UserMetadataTags["UserArb"]); Assert.IsTrue(loadedFile.UserMetadataTags.ContainsKey("UserChi")); Assert.AreEqual("值1", loadedFile.UserMetadataTags["UserChi"]); Assert.IsTrue(loadedFile.UserMetadataTags.ContainsKey("UserCyr")); Assert.AreEqual("Значение 1", loadedFile.UserMetadataTags["UserCyr"]); Assert.IsTrue(loadedFile.UserMetadataTags.ContainsKey("UserGal")); Assert.AreEqual("કિંમત 1", loadedFile.UserMetadataTags["UserGal"]); Assert.IsTrue(loadedFile.UserMetadataTags.ContainsKey("UserGre")); Assert.AreEqual("αξία 1", loadedFile.UserMetadataTags["UserGre"]); Assert.IsTrue(loadedFile.UserMetadataTags.ContainsKey("UserHeb")); Assert.AreEqual("1 ערך", loadedFile.UserMetadataTags["UserHeb"]); Assert.IsTrue(loadedFile.UserMetadataTags.ContainsKey("UserHin")); Assert.AreEqual("मान 1", loadedFile.UserMetadataTags["UserHin"]); Assert.IsTrue(loadedFile.UserMetadataTags.ContainsKey("UserJpn")); Assert.AreEqual("値1", loadedFile.UserMetadataTags["UserJpn"]); Assert.IsTrue(loadedFile.UserMetadataTags.ContainsKey("UserKan")); Assert.AreEqual("ಮೌಲ್ಯ 1", loadedFile.UserMetadataTags["UserKan"]); Assert.IsTrue(loadedFile.UserMetadataTags.ContainsKey("UserKaz")); Assert.AreEqual("мән 1", loadedFile.UserMetadataTags["UserKaz"]); Assert.IsTrue(loadedFile.UserMetadataTags.ContainsKey("UserKor")); Assert.AreEqual("값 1", loadedFile.UserMetadataTags["UserKor"]); Assert.IsTrue(loadedFile.UserMetadataTags.ContainsKey("UserMal")); Assert.AreEqual("മൂല്യം 1", loadedFile.UserMetadataTags["UserMal"]); Assert.IsTrue(loadedFile.UserMetadataTags.ContainsKey("UserMar")); Assert.AreEqual("मूल्य 1", loadedFile.UserMetadataTags["UserMar"]); Assert.IsTrue(loadedFile.UserMetadataTags.ContainsKey("UserPer")); Assert.AreEqual("1 ارزش", loadedFile.UserMetadataTags["UserPer"]); Assert.IsTrue(loadedFile.UserMetadataTags.ContainsKey("UserTel")); Assert.AreEqual("విలువ 1", loadedFile.UserMetadataTags["UserTel"]); Assert.IsTrue(loadedFile.UserMetadataTags.ContainsKey("UserUrd")); Assert.AreEqual("قیمت 1", loadedFile.UserMetadataTags["UserUrd"]); Assert.IsTrue(loadedFile.UserMetadataTags.ContainsKey("UserViet")); Assert.AreEqual("giá trị 1", loadedFile.UserMetadataTags["UserViet"]); } } finally { try { if (File.Exists(fileName)) { File.Delete(fileName); } } catch (Exception ex) { Console.WriteLine(ex); Trace.WriteLine(ex); } } }
private static void FixOccuRecEveryFrameNoLockedSavedInPeriod(string fileLocation) { string fileLocationOut = Path.GetFullPath(Path.GetDirectoryName(fileLocation) + @"\" + Path.GetFileName(fileLocation) + "-fixed.aav"); int FIRST_FRAME_IN_PERIOD = 0; int ACTUAL_INTEGRATION = 2; var aaFile = new AdvFile2(fileLocation); int FIRST_OSD_LINE = int.Parse(aaFile.UserMetadataTags["OSD-FIRST-LINE"]); int LAST_OSD_LINE = int.Parse(aaFile.UserMetadataTags["OSD-LAST-LINE"]); int FRAME_WIDTH = int.Parse(aaFile.SystemMetadataTags["WIDTH"]); int FRAME_HEIGHT = int.Parse(aaFile.SystemMetadataTags["HEIGHT"]); int FRAME_PIXELS = FRAME_WIDTH * FRAME_HEIGHT; var frame = new ushort[FRAME_PIXELS]; List <ushort[]> calFrames = new List <ushort[]>(); for (uint i = 0; i < aaFile.CalibrationSteamInfo.FrameCount; i++) { var calPix = aaFile.GetCalibrationFramePixels(i); for (int j = 0; j < FRAME_PIXELS; j++) { frame[j] = (ushort)calPix[j]; } calFrames.Add(frame.ToArray()); } for (int j = 0; j < FRAME_PIXELS; j++) { frame[j] = 0; } int totalIntegratedFrames = 0; string tmpBuffFileName = Path.GetTempFileName(); using (var tmpFile = File.Create(tmpBuffFileName)) using (var bw = new BinaryWriter(tmpFile)) { uint[] pixels = null; int originalRawFrames = 0; for (uint i = 0; i < aaFile.MainSteamInfo.FrameCount; i++) { AdvFrameInfo frameInfo; pixels = aaFile.GetMainFramePixels(i, out frameInfo); bool isFirstFrameInInterval = (i - (FIRST_FRAME_IN_PERIOD + ACTUAL_INTEGRATION)) % ACTUAL_INTEGRATION == 0; bool isLastFrameInInterval = (i - (FIRST_FRAME_IN_PERIOD + ACTUAL_INTEGRATION - 1)) % ACTUAL_INTEGRATION == 0; bool isFirstFrameOfOccuRecPresumedIntegrationInterval = Convert.ToInt64(frameInfo.Status["StartFrame"]) == Convert.ToInt64(frameInfo.Status["EndFrame"]); for (int j = 0; j < FRAME_PIXELS; j++) { int lineNo = (j / FRAME_WIDTH); if (lineNo < FIRST_OSD_LINE || lineNo > (LAST_OSD_LINE + 1)) { // Sum up all pixels which are NOT in the OSD area frame[j] += (ushort)pixels[j]; } else { // This is *the* OSD timestamp. Copy only even/odd lines if (isFirstFrameInInterval) { if (lineNo % 2 == 0) { // The first timestamp will be only correct if this is also a first frame from the 'presumed' integration period if (isFirstFrameOfOccuRecPresumedIntegrationInterval) { frame[j] = (ushort)(ACTUAL_INTEGRATION * pixels[j]); } else { frame[j] = 0; } } } else if (isLastFrameInInterval) { // The timestamp of the last field is always correct so always copy it if (lineNo % 2 == 1) { frame[j] = (ushort)pixels[j]; } } } } if (isLastFrameInInterval) { for (int j = 0; j < frame.Length; j++) { bw.Write(frame[j]); } totalIntegratedFrames++; for (int j = 0; j < FRAME_PIXELS; j++) { frame[j] = 0; } } originalRawFrames += (short)frameInfo.Status["IntegratedFrames"]; } } var recorder = new AdvRecorder(); recorder.ImageConfig.SetImageParameters( (ushort)aaFile.Width, (ushort)aaFile.Height, 16, 255 * ACTUAL_INTEGRATION); recorder.FileMetaData.RecorderSoftwareName = "Tangra"; recorder.FileMetaData.RecorderSoftwareVersion = VersionHelper.AssemblyVersion; recorder.FileMetaData.CameraModel = "Unknown"; recorder.FileMetaData.CameraSensorInfo = "Unknown"; var frameRate = 25.00; recorder.FileMetaData.NativeFrameRate = frameRate; recorder.FileMetaData.EffectiveFrameRate = 25.0 / ACTUAL_INTEGRATION; var nativeStandards = string.Empty; if (Math.Abs(frameRate - 25.0) < 0.1) { nativeStandards = "PAL"; } else if (Math.Abs(frameRate - 29.97) < 0.1) { nativeStandards = "NTSC"; } recorder.FileMetaData.AddUserTag("NATIVE-VIDEO-STANDARD", nativeStandards); recorder.FileMetaData.AddUserTag("FRAME-COMBINING", "Binning"); recorder.FileMetaData.AddUserTag("OSD-FIRST-LINE", FIRST_OSD_LINE.ToString()); recorder.FileMetaData.AddUserTag("OSD-LAST-LINE", LAST_OSD_LINE.ToString()); recorder.FileMetaData.AddUserTag("AAV-VERSION", "2"); recorder.FileMetaData.AddUserTag("AAV16-NORMVAL", (255 * ACTUAL_INTEGRATION).ToString()); recorder.FileMetaData.AddCalibrationStreamTag("TYPE", "VTI-OSD-CALIBRATION"); recorder.FileMetaData.AddUserTag("OSD-FIRST-LINE", FIRST_OSD_LINE.ToString()); recorder.FileMetaData.AddUserTag("OSD-LAST-LINE", LAST_OSD_LINE.ToString()); recorder.StatusSectionConfig.RecordSystemErrors = true; recorder.StatusSectionConfig.AddDefineTag("FRAME-TYPE", Adv2TagType.UTF8String); recorder.StatusSectionConfig.AddDefineTag("FRAMES-IN-INTERVAL", Adv2TagType.Int8); recorder.StatusSectionConfig.AddDefineTag("NOISE-SIGNATURES", Adv2TagType.UTF8String); recorder.StatusSectionConfig.AddDefineTag("ORIGINAL-FRAME-ID", Adv2TagType.Int32); recorder.StatusSectionConfig.AddDefineTag("IntegratedFrames", Adv2TagType.Int8); recorder.StartRecordingNewFile(fileLocationOut, 0, true); int calFrameId = 0; foreach (var calFrame in calFrames) { recorder.AddCalibrationFrame(calFrame, true, PreferredCompression.Lagarith16, new AdvRecorder.AdvStatusEntry() { AdditionalStatusTags = new[] { "VTI-OSD-CALIBRATION", (object)(byte)0, string.Empty, (object)calFrameId, (object)(byte)0 } }, Adv.AdvImageData.PixelDepth16Bit); calFrameId++; } using (var tmpFile = File.OpenRead(tmpBuffFileName)) using (var rdr = new BinaryReader(tmpFile)) { for (int i = 0; i < totalIntegratedFrames; i++) { for (int j = 0; j < frame.Length; j++) { frame[j] = rdr.ReadUInt16(); } recorder.AddVideoFrame(frame, true, PreferredCompression.Lagarith16, new AdvRecorder.AdvStatusEntry() { SystemErrors = "", AdditionalStatusTags = new[] { "DATA", (object)(byte)8, string.Empty, (object)(int)0, (object)(byte)8 } }, Adv.AdvImageData.PixelDepth16Bit); } } File.Delete(tmpBuffFileName); recorder.FinishRecording(); }
private static void MergeOverlappingAAVFiles(string file1, string file2) { string fileLocationOut = Path.GetFullPath(Path.GetDirectoryName(file1) + @"\" + Path.GetFileName(file1) + "-fixed.aav"); int FIRST_FILE_FRAME_NO_SEC_FILE_ZERO = 110; var aaFile1 = new AdvFile2(file1); int FIRST_OSD_LINE = int.Parse(aaFile1.SystemMetadataTags["OSD-FIRST-LINE"]); int LAST_OSD_LINE = int.Parse(aaFile1.SystemMetadataTags["OSD-LAST-LINE"]); int INTEGRATION = (int)Math.Round(double.Parse(aaFile1.SystemMetadataTags["NATIVE-FRAME-RATE"]) / double.Parse(aaFile1.SystemMetadataTags["EFFECTIVE-FRAME-RATE"])); int FRAME_WIDTH = int.Parse(aaFile1.SystemMetadataTags["WIDTH"]); int FRAME_HEIGHT = int.Parse(aaFile1.SystemMetadataTags["HEIGHT"]); int FRAME_PIXELS = FRAME_WIDTH * FRAME_HEIGHT; var frame = new ushort[FRAME_PIXELS]; List <ushort[]> calFrames = new List <ushort[]>(); // TODO: Frames of large AAV files should be saved in a temporary file to avoid running OutOfMemory List <ushort[]> mainFrames = new List <ushort[]>(); List <Tuple <object, object, object> > mainFramesInfo = new List <Tuple <object, object, object> >(); for (uint i = 0; i < aaFile1.CalibrationSteamInfo.FrameCount; i++) { var calPix = aaFile1.GetCalibrationFramePixels(i); for (int j = 0; j < FRAME_PIXELS; j++) { frame[j] = (ushort)calPix[j]; } calFrames.Add(frame.ToArray()); } for (uint i = 0; i < FIRST_FILE_FRAME_NO_SEC_FILE_ZERO; i++) { AdvFrameInfo frameInfo; var framePix = aaFile1.GetMainFramePixels(i, out frameInfo); for (int j = 0; j < FRAME_PIXELS; j++) { frame[j] = (ushort)framePix[j]; } mainFrames.Add(frame.ToArray()); mainFramesInfo.Add(Tuple.Create(frameInfo.Status["IntegratedFrames"], frameInfo.Status["StartFrame"], frameInfo.Status["EndFrame"])); } aaFile1.Close(); var aaFile2 = new AdvFile2(file2); for (uint i = 0; i < aaFile2.MainSteamInfo.FrameCount; i++) { AdvFrameInfo frameInfo; var framePix = aaFile2.GetMainFramePixels(i, out frameInfo); for (int j = 0; j < FRAME_PIXELS; j++) { frame[j] = (ushort)framePix[j]; } mainFrames.Add(frame.ToArray()); mainFramesInfo.Add(Tuple.Create(frameInfo.Status["IntegratedFrames"], frameInfo.Status["StartFrame"], frameInfo.Status["EndFrame"])); } aaFile2.Close(); var recorder = new AdvRecorder(); recorder.ImageConfig.SetImageParameters( (ushort)aaFile1.Width, (ushort)aaFile1.Height, (byte)aaFile1.DataBpp, aaFile1.MaxPixelValue); recorder.FileMetaData.RecorderSoftwareName = "Tangra"; recorder.FileMetaData.RecorderSoftwareVersion = VersionHelper.AssemblyVersion; recorder.FileMetaData.CameraModel = "Unknown"; recorder.FileMetaData.CameraSensorInfo = "Unknown"; var frameRate = 25.00; recorder.FileMetaData.NativeFrameRate = frameRate; recorder.FileMetaData.EffectiveFrameRate = 25.0 / INTEGRATION; var nativeStandards = string.Empty; if (Math.Abs(frameRate - 25.0) < 0.1) { nativeStandards = "PAL"; } else if (Math.Abs(frameRate - 29.97) < 0.1) { nativeStandards = "NTSC"; } recorder.FileMetaData.AddUserTag("NATIVE-VIDEO-STANDARD", nativeStandards); recorder.FileMetaData.AddUserTag("FRAME-COMBINING", "Binning"); recorder.FileMetaData.AddUserTag("OSD-FIRST-LINE", FIRST_OSD_LINE.ToString()); recorder.FileMetaData.AddUserTag("OSD-LAST-LINE", LAST_OSD_LINE.ToString()); recorder.FileMetaData.AddUserTag("AAV-VERSION", "2"); recorder.FileMetaData.AddUserTag("AAV16-NORMVAL", (256 * INTEGRATION).ToString()); recorder.FileMetaData.AddCalibrationStreamTag("TYPE", "VTI-OSD-CALIBRATION"); recorder.FileMetaData.AddUserTag("OSD-FIRST-LINE", FIRST_OSD_LINE.ToString()); recorder.FileMetaData.AddUserTag("OSD-LAST-LINE", LAST_OSD_LINE.ToString()); recorder.StatusSectionConfig.RecordSystemErrors = true; recorder.StatusSectionConfig.AddDefineTag("FRAME-TYPE", Adv2TagType.UTF8String); recorder.StatusSectionConfig.AddDefineTag("ORIGINAL-FRAME-ID", Adv2TagType.Int32); recorder.StatusSectionConfig.AddDefineTag("IntegratedFrames", Adv2TagType.Int16); recorder.StatusSectionConfig.AddDefineTag("StartFrame", Adv2TagType.Long64); recorder.StatusSectionConfig.AddDefineTag("EndFrame", Adv2TagType.Long64); recorder.StartRecordingNewFile(fileLocationOut, 0, true); int calFrameId = 0; foreach (var calFrame in calFrames) { recorder.AddCalibrationFrame(calFrame, true, PreferredCompression.Lagarith16, new AdvRecorder.AdvStatusEntry() { AdditionalStatusTags = new[] { "VTI-OSD-CALIBRATION", (object)calFrameId, (object)(short)0, (object)(long)0, (object)(long)0 } }, Adv.AdvImageData.PixelDepth16Bit); calFrameId++; } for (int frameNo = 0; frameNo < mainFrames.Count; frameNo++) { Tuple <object, object, object> frameInfo = mainFramesInfo[frameNo]; recorder.AddVideoFrame(mainFrames[frameNo], true, PreferredCompression.Lagarith16, new AdvRecorder.AdvStatusEntry() { SystemErrors = "", AdditionalStatusTags = new[] { "DATA", (object)(int)frameNo, frameInfo.Item1, frameInfo.Item2, frameInfo.Item3 } }, Adv.AdvImageData.PixelDepth16Bit); } recorder.FinishRecording(); }
private static void FixOccuRecEveryFrameSavedInPeriod(string fileLocation) { string fileLocationOut = Path.GetFullPath(Path.GetDirectoryName(fileLocation) + @"\" + Path.GetFileName(fileLocation) + "-fixed.aav"); int FIRST_FRAME_IN_PERIOD = 2; List <uint> BAD_FRAMES = new List <uint>(); var aaFile = new AdvFile2(fileLocation); int FIRST_OSD_LINE = int.Parse(aaFile.SystemMetadataTags["OSD-FIRST-LINE"]); int LAST_OSD_LINE = int.Parse(aaFile.SystemMetadataTags["OSD-LAST-LINE"]); int INTEGRATION = (int)Math.Round(double.Parse(aaFile.SystemMetadataTags["NATIVE-FRAME-RATE"]) / double.Parse(aaFile.SystemMetadataTags["EFFECTIVE-FRAME-RATE"])); int FRAME_WIDTH = int.Parse(aaFile.SystemMetadataTags["WIDTH"]); int FRAME_HEIGHT = int.Parse(aaFile.SystemMetadataTags["HEIGHT"]); int FRAME_PIXELS = FRAME_WIDTH * FRAME_HEIGHT; var frame = new ushort[FRAME_PIXELS]; List <ushort[]> calFrames = new List <ushort[]>(); for (uint i = 0; i < aaFile.CalibrationSteamInfo.FrameCount; i++) { var calPix = aaFile.GetCalibrationFramePixels(i); for (int j = 0; j < FRAME_PIXELS; j++) { frame[j] = (ushort)calPix[j]; } calFrames.Add(frame.ToArray()); } for (int j = 0; j < FRAME_PIXELS; j++) { frame[j] = 0; } int totalIntegratedFrames = 0; string tmpBuffFileName = Path.GetTempFileName(); using (var tmpFile = File.Create(tmpBuffFileName)) using (var bw = new BinaryWriter(tmpFile)) { uint[] pixels = null; for (uint i = 1; i < aaFile.MainSteamInfo.FrameCount; i++) { if (!BAD_FRAMES.Contains(i)) { pixels = aaFile.GetMainFramePixels(i); } for (int j = 0; j < FRAME_PIXELS; j++) { if (j < FIRST_OSD_LINE * FRAME_WIDTH) { frame[j] += (ushort)pixels[j]; } else { frame[j] = (ushort)pixels[j]; } } if ((i - (FIRST_FRAME_IN_PERIOD + INTEGRATION - 1)) % INTEGRATION == 0) { for (int j = 0; j < frame.Length; j++) { bw.Write(frame[j]); } totalIntegratedFrames++; for (int j = 0; j < FRAME_PIXELS; j++) { frame[j] = 0; } } } } var recorder = new AdvRecorder(); recorder.ImageConfig.SetImageParameters( (ushort)aaFile.Width, (ushort)aaFile.Height, 16, aaFile.MaxPixelValue); recorder.FileMetaData.RecorderSoftwareName = "Tangra"; recorder.FileMetaData.RecorderSoftwareVersion = VersionHelper.AssemblyVersion; recorder.FileMetaData.CameraModel = "Unknown"; recorder.FileMetaData.CameraSensorInfo = "Unknown"; var frameRate = 25.00; recorder.FileMetaData.NativeFrameRate = frameRate; recorder.FileMetaData.EffectiveFrameRate = 25.0 / INTEGRATION; var nativeStandards = string.Empty; if (Math.Abs(frameRate - 25.0) < 0.1) { nativeStandards = "PAL"; } else if (Math.Abs(frameRate - 29.97) < 0.1) { nativeStandards = "NTSC"; } recorder.FileMetaData.AddUserTag("NATIVE-VIDEO-STANDARD", nativeStandards); recorder.FileMetaData.AddUserTag("FRAME-COMBINING", "Binning"); recorder.FileMetaData.AddUserTag("OSD-FIRST-LINE", FIRST_OSD_LINE.ToString()); recorder.FileMetaData.AddUserTag("OSD-LAST-LINE", LAST_OSD_LINE.ToString()); recorder.FileMetaData.AddUserTag("AAV-VERSION", "2"); recorder.FileMetaData.AddUserTag("AAV16-NORMVAL", (256 * INTEGRATION).ToString()); recorder.FileMetaData.AddCalibrationStreamTag("TYPE", "VTI-OSD-CALIBRATION"); recorder.FileMetaData.AddUserTag("OSD-FIRST-LINE", FIRST_OSD_LINE.ToString()); recorder.FileMetaData.AddUserTag("OSD-LAST-LINE", LAST_OSD_LINE.ToString()); recorder.StatusSectionConfig.RecordSystemErrors = true; recorder.StatusSectionConfig.AddDefineTag("FRAME-TYPE", Adv2TagType.UTF8String); recorder.StatusSectionConfig.AddDefineTag("FRAMES-IN-INTERVAL", Adv2TagType.Int8); recorder.StatusSectionConfig.AddDefineTag("NOISE-SIGNATURES", Adv2TagType.UTF8String); recorder.StatusSectionConfig.AddDefineTag("ORIGINAL-FRAME-ID", Adv2TagType.Int32); recorder.StatusSectionConfig.AddDefineTag("IntegratedFrames", Adv2TagType.Int8); recorder.StartRecordingNewFile(fileLocationOut, 0, true); int calFrameId = 0; foreach (var calFrame in calFrames) { recorder.AddCalibrationFrame(calFrame, true, PreferredCompression.Lagarith16, new AdvRecorder.AdvStatusEntry() { AdditionalStatusTags = new[] { "VTI-OSD-CALIBRATION", (object)(byte)0, string.Empty, (object)calFrameId, (object)(byte)0 } }, Adv.AdvImageData.PixelDepth16Bit); calFrameId++; } using (var tmpFile = File.OpenRead(tmpBuffFileName)) using (var rdr = new BinaryReader(tmpFile)) { for (int i = 0; i < totalIntegratedFrames; i++) { for (int j = 0; j < frame.Length; j++) { frame[j] = rdr.ReadUInt16(); } recorder.AddVideoFrame(frame, true, PreferredCompression.Lagarith16, new AdvRecorder.AdvStatusEntry() { SystemErrors = "", AdditionalStatusTags = new[] { "DATA", (object)(byte)8, string.Empty, (object)(int)0, (object)(byte)8 } }, Adv.AdvImageData.PixelDepth16Bit); } } File.Delete(tmpBuffFileName); recorder.FinishRecording(); }
private AstroDigitalVideoStreamV2(string fileName, ref AdvFileMetadataInfo fileMetadataInfo, ref GeoLocationInfo geoLocation) { //CheckAdvFileFormat(fileName, ref fileMetadataInfo, ref geoLocation); m_FileName = fileName; m_AdvFile = new AdvFile2(fileName); m_FirstFrame = 0; m_CountFrames = m_AdvFile.MainSteamInfo.FrameCount; m_BitPix = m_AdvFile.DataBpp; m_Width = m_AdvFile.Width; m_Height = m_AdvFile.Height; m_MaxPixelValue = (uint)m_AdvFile.MaxPixelValue; m_FrameRate = 0; m_Engine = "ADV2"; fileMetadataInfo.Recorder = GetFileTag("RECORDER-SOFTWARE"); CameraModel = fileMetadataInfo.Camera = GetFileTag("CAMERA-MODEL"); fileMetadataInfo.Engine = GetFileTag("FSTF-TYPE"); fileMetadataInfo.AdvrVersion = GetFileTag("RECORDER-SOFTWARE-VERSION"); fileMetadataInfo.SensorInfo = GetFileTag("CAMERA-SENSOR-INFO"); fileMetadataInfo.ObjectName = GetFileTag("OBJNAME"); int aavVersion; if (int.TryParse(GetFileTag("AAV-VERSION"), out aavVersion)) { m_AAVVersion = aavVersion; m_VideoStandard = GetFileTag("NATIVE-VIDEO-STANDARD"); double.TryParse(GetFileTag("NATIVE-FRAME-RATE"), NumberStyles.Float, CultureInfo.InvariantCulture, out m_NativeFrameRate); int.TryParse(GetFileTag("OSD-FIRST-LINE"), out m_OsdFirstLine); int.TryParse(GetFileTag("OSD-LAST-LINE"), out m_OsdLastLine); if (m_OsdLastLine > m_Height) { m_OsdLastLine = m_Height; } if (m_OsdFirstLine < 0) { m_OsdFirstLine = 0; } m_IntegratedAAVFrames = -1; if (double.TryParse(GetFileTag("EFFECTIVE-FRAME-RATE"), NumberStyles.Float, CultureInfo.InvariantCulture, out m_EffectiveFrameRate) && m_NativeFrameRate != 0) { m_IntegratedAAVFrames = (int)Math.Round(m_NativeFrameRate / m_EffectiveFrameRate); m_FrameRate = m_EffectiveFrameRate; // This is important for OCR-ing as the frame rate is used to derive the frame exposure } int.TryParse(GetFileTag("FRAME-STACKING-RATE"), out m_StackingRate); if (m_StackingRate == 1) { m_StackingRate = 0; // Video stacked at x1 is a non-stacked video } m_Engine = string.Format("AAV{0}", aavVersion); } this.geoLocation = new GeoLocationInfo() { //TODO }; geoLocation = this.geoLocation; }
public void TestTimestampsAreSavedAndReadCorrectly() { DateTime frameTimeStamp = new DateTime(2016, 6, 24, 20, 42, 15).AddMilliseconds(1234); long tickStamp = frameTimeStamp.Ticks; float exposureMS = 16.7f; DateTime frameTimeStamp2 = frameTimeStamp.AddMilliseconds(17); long tickStamp2 = frameTimeStamp2.Ticks; var utcTimeStamps = new DateTime[] { frameTimeStamp, frameTimeStamp2 }; var tickStamps = new long[] { tickStamp, tickStamp2 }; AdvTimeStamp ts = AdvTimeStamp.FromDateTime(frameTimeStamp); var tdBackFromMS = new DateTime((long)AdvTimeStamp.ADV_EPOCH_ZERO_TICKS).AddMilliseconds(ts.MillisecondsAfterAdvZeroEpoch); Assert.AreEqual(frameTimeStamp.Ticks, tdBackFromMS.Ticks); Assert.AreEqual(ts.MillisecondsAfterAdvZeroEpoch, ts.NanosecondsAfterAdvZeroEpoch / 1000000); var tdBackFromNS = new DateTime((long)AdvTimeStamp.ADV_EPOCH_ZERO_TICKS).AddMilliseconds(ts.NanosecondsAfterAdvZeroEpoch / 1000000.0); Assert.AreEqual(frameTimeStamp.Ticks, tdBackFromNS.Ticks); var maxTimeStamp = new DateTime((long)AdvTimeStamp.ADV_EPOCH_ZERO_TICKS).AddMilliseconds(ulong.MaxValue / 1000000.0); Console.WriteLine(string.Format("Max ADV UTC Timestamp: {0}", maxTimeStamp.ToString("yyyy-MMM-dd HH:mm:ss"))); Assert.AreEqual(frameTimeStamp.Ticks, new DateTime((long)AdvTimeStamp.ADV_EPOCH_ZERO_TICKS).AddMilliseconds(204496936234000000 / 1000000.0).Ticks); var fileGen = new AdvGenerator(); int tickId = -1; var cfg = new AdvGenerationConfig() { DynaBits = 16, SourceFormat = AdvSourceDataFormat.Format16BitUShort, NumberOfFrames = 2, Compression = CompressionType.Uncompressed, NormalPixelValue = null, MainStreamCustomClock = new CustomClockConfig() { ClockFrequency = 10000000, ClockTicksCallback = () => { tickId++; return(tickStamps[tickId]); }, TicksTimingAccuracy = 1 }, CalibrationStreamCustomClock = new CustomClockConfig() { ClockFrequency = 10000000, ClockTicksCallback = () => 0, TicksTimingAccuracy = 1 }, TimeStampCallback = new GetCurrentImageTimeStampCallback((frameId) => utcTimeStamps[frameId]), ExposureCallback = id => (uint)(exposureMS * 1000000.0) }; string fileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")); if (File.Exists(fileName)) { File.Delete(fileName); } try { // Generate fileGen.GenerateaAdv_V2(cfg, fileName); // Verify using (var loadedFile = new AdvFile2(fileName)) { AdvFrameInfo frameInfo; loadedFile.GetMainFramePixels(0, out frameInfo); Assert.IsNotNull(frameInfo); Assert.IsTrue(frameInfo.HasUtcTimeStamp); Assert.AreEqual(frameTimeStamp.Ticks, frameInfo.UtcStartExposureTimeStamp.Ticks); Assert.AreEqual(exposureMS, frameInfo.UtcExposureMilliseconds, 0.00001); Assert.AreEqual(0, frameInfo.TickStampStartTicks); Assert.AreEqual(tickStamp, frameInfo.TickStampEndTicks); loadedFile.GetMainFramePixels(1, out frameInfo); Assert.IsNotNull(frameInfo); Assert.IsTrue(frameInfo.HasUtcTimeStamp); Assert.AreEqual(frameTimeStamp2.Ticks, frameInfo.UtcStartExposureTimeStamp.Ticks); Assert.AreEqual(exposureMS, frameInfo.UtcExposureMilliseconds, 0.00001); Assert.AreEqual(tickStamp, frameInfo.TickStampStartTicks); Assert.AreEqual(tickStamp2, frameInfo.TickStampEndTicks); } } finally { try { if (File.Exists(fileName)) { File.Delete(fileName); } } catch (Exception ex) { Console.WriteLine(ex); Trace.WriteLine(ex); } } }
private void btnOpenFile_Click(object sender, EventArgs e) { if (openFileDialog2.ShowDialog(this) == DialogResult.OK) { if (loadedFile != null) { loadedFile.Dispose(); loadedFile = null; } loadedFile = new AdvFile2(openFileDialog2.FileName); tbxFileName.Text = openFileDialog2.FileName; lblWidth.Text = loadedFile.Width.ToString(); lblHeight.Text = loadedFile.Height.ToString(); lblBPP.Text = string.Format("{0} bit, {1}", loadedFile.DataBpp, loadedFile.IsColourImage ? "Colour" : "Monochrome"); lblMaxPixelValue.Text = loadedFile.MaxPixelValue.ToString(); lblUtcAccuracy.Text = GetUtcAccuracy(loadedFile.UtcTimestampAccuracyInNanoseconds); lblMainFrames.Text = loadedFile.MainSteamInfo.FrameCount.ToString(); lblMainClockFreq.Text = string.Format("{0} Hz", loadedFile.MainSteamInfo.ClockFrequency); lblMainFrameTSAccu.Text = GetFrameTimeStampAccuracy(loadedFile.MainSteamInfo.TimingAccuracy * 1.0 / loadedFile.MainSteamInfo.ClockFrequency, loadedFile.MainSteamInfo.TimingAccuracy); LoadMetadata(lvMainStream, loadedFile.MainSteamInfo.MetadataTags); lblCalibFrames.Text = loadedFile.CalibrationSteamInfo.FrameCount.ToString(); lblCalibClockFreq.Text = string.Format("{0} Hz", loadedFile.CalibrationSteamInfo.ClockFrequency); lblCalibFrameTSAccu.Text = GetFrameTimeStampAccuracy(loadedFile.CalibrationSteamInfo.TimingAccuracy * 1.0 / loadedFile.CalibrationSteamInfo.ClockFrequency, loadedFile.MainSteamInfo.TimingAccuracy); LoadMetadata(lvCalibStream, loadedFile.CalibrationSteamInfo.MetadataTags); LoadMetadata(lvImageSectionTags, loadedFile.ImageSectionTags); lblCountLayouts.Text = loadedFile.ImageLayouts.Count.ToString(); lvImageLayoutTags.Tag = loadedFile.ImageLayouts; LoadImageLayoutTags(loadedFile.ImageLayouts.Count - 1); LoadStatusTags(loadedFile.StatusTagDefinitions); LoadMetadata(lvSystemMedata, loadedFile.SystemMetadataTags); LoadMetadata(lvUserMetadata, loadedFile.UserMetadataTags); if (loadedFile.MainSteamInfo.FrameCount > 0) { // Make sure we can read a frame, if any AdvFrameInfo frameInfo; loadedFile.GetMainFramePixels(0, out frameInfo); } if (loadedFile.MainIndex.Count > 0) { var sb = new StringBuilder(); sb.AppendFormat("FrameOffset\t\tBytesCount\tElapsedTicks\r\n"); foreach (var ie in loadedFile.MainIndex) { sb.AppendFormat("0x{0}\t0x{1}\t{2}\r\n", Convert.ToString(ie.FrameOffset, 16).PadLeft(16, '0'), Convert.ToString(ie.BytesCount, 16).PadLeft(8, '0'), ie.ElapsedTicks); } tbxMainIndex.Text = sb.ToString(); } if (loadedFile.CalibrationIndex.Count > 0) { var sb = new StringBuilder(); sb.AppendFormat("FrameOffset\t\tBytesCount\tElapsedTicks\r\n"); foreach (var ie in loadedFile.CalibrationIndex) { sb.AppendFormat("0x{0}\t0x{1}\t{2}\r\n", Convert.ToString(ie.FrameOffset, 16).PadLeft(16, '0'), Convert.ToString(ie.BytesCount, 16).PadLeft(8, '0'), ie.ElapsedTicks); } tbxCalibIndex.Text = sb.ToString(); } } }
private AstroDigitalVideoStreamV2(string fileName, ref AdvFileMetadataInfo fileMetadataInfo, ref GeoLocationInfo geoLocation) { //CheckAdvFileFormat(fileName, ref fileMetadataInfo, ref geoLocation); m_FileName = fileName; m_AdvFile = new AdvFile2(fileName); m_FirstFrame = 0; m_CountFrames = m_AdvFile.MainSteamInfo.FrameCount; m_BitPix = m_AdvFile.DataBpp; m_Width = m_AdvFile.Width; m_Height = m_AdvFile.Height; m_MaxPixelValue = (uint)m_AdvFile.MaxPixelValue; m_FrameRate = 0; m_Engine = "ADV2"; fileMetadataInfo.Recorder = GetFileTag("RECORDER-SOFTWARE"); CameraModel = fileMetadataInfo.Camera = GetFileTag("CAMERA-MODEL"); fileMetadataInfo.Engine = GetFileTag("FSTF-TYPE"); fileMetadataInfo.AdvrVersion = GetFileTag("RECORDER-SOFTWARE-VERSION"); fileMetadataInfo.SensorInfo = GetFileTag("CAMERA-SENSOR-INFO"); fileMetadataInfo.ObjectName = GetFileTag("OBJNAME"); int aavVersion; if (int.TryParse(GetFileTag("AAV-VERSION"), out aavVersion)) { m_AAVVersion = aavVersion; m_VideoStandard = GetFileTag("NATIVE-VIDEO-STANDARD"); double.TryParse(GetFileTag("NATIVE-FRAME-RATE"), out m_NativeFrameRate); int.TryParse(GetFileTag("OSD-FIRST-LINE"), out m_OsdFirstLine); int.TryParse(GetFileTag("OSD-LAST-LINE"), out m_OsdLastLine); if (m_OsdLastLine > m_Height) m_OsdLastLine = m_Height; if (m_OsdFirstLine < 0) m_OsdFirstLine = 0; m_IntegratedAAVFrames = -1; if (double.TryParse(GetFileTag("EFFECTIVE-FRAME-RATE"), out m_EffectiveFrameRate) && m_NativeFrameRate != 0) { m_IntegratedAAVFrames = (int)Math.Round(m_NativeFrameRate / m_EffectiveFrameRate); m_FrameRate = m_EffectiveFrameRate; // This is important for OCR-ing as the frame rate is used to derive the frame exposure } int.TryParse(GetFileTag("FRAME-STACKING-RATE"), out m_StackingRate); if (m_StackingRate == 1) m_StackingRate = 0; // Video stacked at x1 is a non-stacked video m_Engine = string.Format("AAV{0}", aavVersion); } this.geoLocation = new GeoLocationInfo() { //TODO }; geoLocation = this.geoLocation; }
public void TestStatusTagsAreSavedAndReadCorrectly() { string fileName = Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString("N")); if (File.Exists(fileName)) { File.Delete(fileName); } try { // Generate var recorder = new AdvRecorder(); recorder.ImageConfig.SetImageParameters(640, 480, 16, 0); recorder.FileMetaData.RecorderSoftwareName = "AdvLibTestRecorder"; recorder.FileMetaData.RecorderSoftwareVersion = "x.y.z"; recorder.FileMetaData.RecorderHardwareName = "a.b.c"; recorder.FileMetaData.CameraModel = "TestCamera"; recorder.FileMetaData.CameraSensorInfo = "TestSensor"; recorder.StatusSectionConfig.RecordGain = true; recorder.StatusSectionConfig.RecordGamma = true; recorder.StatusSectionConfig.RecordShutter = true; recorder.StatusSectionConfig.RecordCameraOffset = true; recorder.StatusSectionConfig.RecordSystemTime = true; recorder.StatusSectionConfig.RecordTrackedSatellites = true; recorder.StatusSectionConfig.RecordAlmanacStatus = true; recorder.StatusSectionConfig.RecordAlmanacOffset = true; recorder.StatusSectionConfig.RecordFixStatus = true; recorder.StatusSectionConfig.RecordSystemErrors = true; recorder.StatusSectionConfig.RecordVideoCameraFrameId = true; recorder.StatusSectionConfig.RecordHardwareTimerFrameId = true; recorder.StatusSectionConfig.AddDefineTag("CustomInt8", Adv2TagType.Int8); recorder.StatusSectionConfig.AddDefineTag("CustomInt16", Adv2TagType.Int16); recorder.StatusSectionConfig.AddDefineTag("CustomInt32", Adv2TagType.Int32); recorder.StatusSectionConfig.AddDefineTag("CustomLong64", Adv2TagType.Long64); recorder.StatusSectionConfig.AddDefineTag("CustomReal", Adv2TagType.Real); recorder.StatusSectionConfig.AddDefineTag("CustomString", Adv2TagType.UTF8String); recorder.StartRecordingNewFile(fileName, 0); var systemTimeStamp = DateTime.Now.AddMilliseconds(123); var status = new AdvRecorder.AdvStatusEntry() { AlmanacStatus = AlmanacStatus.Good, AlmanacOffset = 14, TrackedSatellites = 8, CameraOffset = 8.23f, FixStatus = FixStatus.PFix, Gain = 32.82f, Gamma = 0.35f, Shutter = 2.502f, SystemTime = AdvTimeStamp.FromDateTime(systemTimeStamp), VideoCameraFrameId = 19289232, HardwareTimerFrameId = 9102 }; status.AdditionalStatusTags = new object[] { (byte)12, (short)-123, (int)192847, -1 * (long)(0x6E9104B012CD110F), 91.291823f, "Значение 1" }; var imageGenerator = new ImageGenerator(); ushort[] imagePixels = imageGenerator.GetCurrentImageBytesInt16(0, 16); recorder.AddVideoFrame( imagePixels, false, null, AdvTimeStamp.FromDateTime(DateTime.Now), AdvTimeStamp.FromDateTime(DateTime.Now.AddSeconds(2.56)), status, AdvImageData.PixelDepth16Bit); recorder.FinishRecording(); // Verify using (var loadedFile = new AdvFile2(fileName)) { AdvFrameInfo frameInfo; loadedFile.GetMainFramePixels(0, out frameInfo); Assert.AreEqual(status.Gain, frameInfo.Gain, 0.000001); Assert.AreEqual(status.Gamma, frameInfo.Gamma, 0.000001); Assert.AreEqual(status.Shutter, frameInfo.Shutter, 0.000001); Assert.AreEqual(status.CameraOffset, frameInfo.Offset, 0.000001); Assert.AreEqual(status.FixStatus, (FixStatus)frameInfo.GPSFixStatus); Assert.AreEqual(status.AlmanacStatus, (AlmanacStatus)frameInfo.GPSAlmanacStatus); Assert.AreEqual(status.TrackedSatellites, frameInfo.GPSTrackedSattelites); Assert.AreEqual(status.AlmanacOffset, frameInfo.GPSAlmanacOffset); Assert.AreEqual(status.VideoCameraFrameId, frameInfo.VideoCameraFrameId); Assert.AreEqual(status.HardwareTimerFrameId, frameInfo.HardwareTimerFrameId); Assert.AreEqual(systemTimeStamp.Ticks, frameInfo.SystemTimestamp.Ticks); Assert.AreEqual(status.AdditionalStatusTags[0], frameInfo.Status["CustomInt8"]); Assert.AreEqual(status.AdditionalStatusTags[1], frameInfo.Status["CustomInt16"]); Assert.AreEqual(status.AdditionalStatusTags[2], frameInfo.Status["CustomInt32"]); Assert.AreEqual(status.AdditionalStatusTags[3], frameInfo.Status["CustomLong64"]); Assert.AreEqual(status.AdditionalStatusTags[4], frameInfo.Status["CustomReal"]); Assert.AreEqual(status.AdditionalStatusTags[5], frameInfo.Status["CustomString"]); } } finally { try { if (File.Exists(fileName)) { File.Delete(fileName); } } catch (Exception ex) { Console.WriteLine(ex); Trace.WriteLine(ex); } } }