public Dec2EncAdapter(CC_COLOR_FMT exchg_fmt, ICC_VideoEncoder encoder, bool useCudaMemory) { _exFormat = exchg_fmt; _encoder = encoder; _useCudaMemory = useCudaMemory; }
static unsafe int Main(string[] args) { var buildVersion = Assembly.GetEntryAssembly().GetName().Version.ToString(); Console.WriteLine($"Daniel2 MXF Transcoder App v{buildVersion}. Copyright (c) 2018 Cinegy LLC\n"); if (args.Length < 2) { print_help(); return(1); } Console.WriteLine($"Cinecoder version: {Cinecoder_.Version.VersionHi}.{Cinecoder_.Version.VersionLo}.{Cinecoder_.Version.EditionNo}.{Cinecoder_.Version.RevisionNo}\n"); try { Cinecoder_.ErrorHandler = new ErrorHandler(); Factory = Cinecoder_.CreateClassFactory(); Factory.AssignLicense(License.Companyname, License.Licensekey); Factory.LoadPlugin("Cinecoder.Plugin.Multiplexers.dll"); VideoFile = new VideoFileReader(); var openResult = VideoFile.Open(args[0]); if (openResult != VideoFileReader.OpenResult.OK) { Console.Error.WriteLine($"Can't open source file '{args[0]}': {openResult}"); return(-1); } var streamInfo = GetStreamInfo(); Console.WriteLine($"Source file : {args[0]}"); Console.WriteLine($"Stream type : {VideoFile.StreamType}"); Console.WriteLine($"Frame size : {streamInfo.FrameSize.cx} x {streamInfo.FrameSize.cy}"); Console.WriteLine($"Frame rate : {streamInfo.FrameRate.num * 1000 / streamInfo.FrameRate.denom / 1000.0}"); Console.WriteLine($"Aspect ratio : {streamInfo.AspectRatio.num}:{streamInfo.AspectRatio.denom}"); Console.WriteLine("Bit depth : {0}{1}", streamInfo.Assigned("BitDepth") ? "" : "<unknown>, assuming ", streamInfo.BitDepth); Console.WriteLine("Chroma format: {0}{1}", streamInfo.Assigned("ChromaFormat") ? "" : "<unknown>, assuming ", streamInfo.ChromaFormat); bool use_cuda = false; bool enc_looped = false; var encParams = ParseArgs(args, streamInfo, ref use_cuda, ref enc_looped); CC_COLOR_FMT exch_fmt = encParams.ChromaFormat == CC_CHROMA_FORMAT.CC_CHROMA_RGB || encParams.ChromaFormat == CC_CHROMA_FORMAT.CC_CHROMA_RGBA ? (encParams.BitDepth == 8 ? CC_COLOR_FMT.CCF_RGBA : CC_COLOR_FMT.CCF_RGBA64) : (encParams.BitDepth == 8 ? CC_COLOR_FMT.CCF_YUY2 : CC_COLOR_FMT.CCF_Y216); if (use_cuda) { encParams.InputColorFormat = exch_fmt; } Console.WriteLine(); Console.WriteLine($"Target file : {args[1]}"); Console.WriteLine($"Stream type : {(encParams as ICC_ElementaryStreamInfo).StreamType}"); Console.WriteLine($"Frame size : {encParams.FrameSize.cx} x {encParams.FrameSize.cy}"); Console.WriteLine($"Frame rate : {encParams.FrameRate.num * 1000 / encParams.FrameRate.denom / 1000.0}"); Console.WriteLine($"Aspect ratio : {encParams.AspectRatio.num}:{encParams.AspectRatio.denom}"); Console.WriteLine($"Bit depth : {encParams.BitDepth}"); Console.WriteLine($"Chroma format: {encParams.ChromaFormat}"); if (encParams.RateMode == CC_BITRATE_MODE.CC_CQ) { Console.WriteLine($"QuantScale : {encParams.QuantScale}"); } else { Console.WriteLine($"Bitrate : {encParams.BitRate / 1E6:F2} Mbps"); } Console.WriteLine($"Coding method: {encParams.CodingMethod}"); var decoder = CreateDecoder(VideoFile.StreamType); var encoder = Factory.CreateInstanceByName(use_cuda ? "DanielVideoEncoder_CUDA" : "DanielVideoEncoder") as ICC_VideoEncoder; var muxer = Factory.CreateInstanceByName("MXF_OP1A_Multiplexer") as ICC_Multiplexer; var pinDescr = Factory.CreateInstanceByName("MXF_MultiplexerPinSettings") as ICC_ElementaryStreamSettings; var fileWriter = Factory.CreateInstanceByName("OutputFile") as ICC_OutputFile; muxer.Init(); muxer.OutputCallback = fileWriter; pinDescr.StreamType = CC_ELEMENTARY_STREAM_TYPE.CC_ES_TYPE_VIDEO_DANIEL; pinDescr.BitRate = encParams.BitRate; pinDescr.FrameRate = encParams.FrameRate; encoder.Init(encParams); encoder.OutputCallback = muxer.CreatePin(pinDescr); decoder.Init(); decoder.OutputCallback = new Dec2EncAdapter(exch_fmt, encoder, use_cuda); if (decoder is ICC_ProcessDataPolicyProp) { (decoder as ICC_ProcessDataPolicyProp).ProcessDataPolicy = CC_PROCESS_DATA_POLICY.CC_PDP_PARSED_DATA; } fileWriter.Create(args[1]); // audio ------------------------------- var audioReader = Factory.CreateInstanceByName("MediaReader") as ICC_MediaReader; audioReader.Open(args[0]); int numAudioTracks = audioReader.NumberOfAudioTracks; Console.WriteLine($"\nAudio tracks : {numAudioTracks}"); int audioBufferSize = 96000 * 16 * 4; // max possible buffer IntPtr audioBuffer = Marshal.AllocHGlobal(audioBufferSize); var audioPins = new ICC_ByteStreamConsumer[numAudioTracks]; var audioFmts = new CC_AUDIO_FMT[numAudioTracks]; for (int i = 0; i < numAudioTracks; i++) { audioReader.CurrentAudioTrackNumber = i; var audio_info = audioReader.CurrentAudioTrackInfo; Console.WriteLine($"{i}: Freq = {audio_info.SampleRate}Hz, NumCh = {audio_info.NumChannels}, BitDepth = {audio_info.BitsPerSample}"); var pin_descr = Factory.CreateInstanceByName("MXF_MultiplexerPinSettings") as ICC_MXF_MultiplexerPinSettings; pin_descr.StreamType = CC_ELEMENTARY_STREAM_TYPE.CC_ES_TYPE_AUDIO_LPCM; pin_descr.FrameRate = encParams.FrameRate; pin_descr.BitsPerSample = audio_info.BitsPerSample; pin_descr.NumChannels = audio_info.NumChannels; pin_descr.SampleRate = audio_info.SampleRate; audioPins[i] = muxer.CreatePin(pin_descr); audioFmts[i] = GetAudioFormat(audio_info.NumChannels, audio_info.BitsPerSample); } // audio ------------------------------- Console.WriteLine($"\nTotal frames: {VideoFile.Length}" + (enc_looped ? ", encoding looped." : "")); Console.WriteLine("Press ESC if you want to stop encoding."); long totalFrames = VideoFile.Length; long codedFrames = 0; DateTime t00 = DateTime.Now, t0 = t00; double fps = 0; long f = 0, f0 = 0; for (long i = 0; i < totalFrames; i++, f++) { Console.Write($"\rframe {f} ({f*100.0/totalFrames:F2}%), {fps:F2} fps \b"); var frameData = VideoFile.ReadFrame(i); fixed(byte *p = frameData) decoder.ProcessData((IntPtr)p, (uint)frameData.Length); audioReader.CurrentFrameNumber = (int)i; for (int track = 0; track < numAudioTracks; track++) { audioReader.CurrentAudioTrackNumber = track; var ret_sz = audioReader.GetCurrentAudioFrame(audioFmts[track], audioBuffer, (uint)audioBufferSize); audioPins[track].ProcessData(audioBuffer, ret_sz); } codedFrames++; if (Console.KeyAvailable && Console.ReadKey().Key == ConsoleKey.Escape) { Console.WriteLine("\nStopped by ESC."); break; } DateTime t1 = DateTime.Now; if ((t1 - t0).TotalMilliseconds > 500) { fps = (f - f0) / (t1 - t0).TotalSeconds; f0 = f; t0 = t1; } if (enc_looped && i + 1 == totalFrames) { i = -1; } } decoder.Done(true); encoder.Done(true); muxer.Done(true); Marshal.FreeHGlobal(audioBuffer); Console.WriteLine($"\nTotal frame(s) processed: {codedFrames}, average fps: {codedFrames/(DateTime.Now-t00).TotalSeconds:F2}, average bitrate: {fileWriter.Length*8/1E6/codedFrames*encParams.FrameRate.num/encParams.FrameRate.denom:F2} Mbps"); fileWriter.Close(); } catch (Exception e) { if (e.Message != null) { Console.Error.WriteLine($"Error: {e.Message}"); } return(e.HResult); } return(0); }