[TestCase(License.Companyname, License.Licensekey, "DanielVideoEncoder_CUDA", ExpectedResult = true)] //should correctly create object public bool CreateEncoder(string companyName, string licenseKey, string instanceTypeName) { try { AssignCinecoderLicense(companyName, licenseKey); _encoder = (ICC_VideoEncoder)_factory.CreateInstanceByName(instanceTypeName); Assert.IsNotNull(_encoder, "Returned instance is null"); } catch (Exception ex) { Console.WriteLine($"Exception creating Cinecoder Encoder: {ex.Message}"); return(false); } return(true); }
static ICC_VideoDecoder CreateDecoder(CC_ELEMENTARY_STREAM_TYPE type) { switch (type) { case CC_ELEMENTARY_STREAM_TYPE.CC_ES_TYPE_VIDEO_MPEG1: case CC_ELEMENTARY_STREAM_TYPE.CC_ES_TYPE_VIDEO_MPEG2: return(Factory.CreateInstanceByName("MpegVideoDecoder") as ICC_VideoDecoder); case CC_ELEMENTARY_STREAM_TYPE.CC_ES_TYPE_VIDEO_H264: case CC_ELEMENTARY_STREAM_TYPE.CC_ES_TYPE_VIDEO_AVC1: return(Factory.CreateInstanceByName("H264VideoDecoder") as ICC_VideoDecoder); case CC_ELEMENTARY_STREAM_TYPE.CC_ES_TYPE_VIDEO_AVC_INTRA: return(Factory.CreateInstanceByName("AVCIntraDecoder2") as ICC_VideoDecoder); case CC_ELEMENTARY_STREAM_TYPE.CC_ES_TYPE_VIDEO_HEVC: case CC_ELEMENTARY_STREAM_TYPE.CC_ES_TYPE_VIDEO_HVC1: return(Factory.CreateInstanceByName("HEVCVideoDecoder") as ICC_VideoDecoder); case CC_ELEMENTARY_STREAM_TYPE.CC_ES_TYPE_VIDEO_J2K: return(Factory.CreateInstanceByName("J2K_VideoDecoder") as ICC_VideoDecoder); case CC_ELEMENTARY_STREAM_TYPE.CC_ES_TYPE_VIDEO_DANIEL: return(Factory.CreateInstanceByName("DanielVideoDecoder") as ICC_VideoDecoder); } throw new Exception($"Cannot create a decoder instance for {type}"); }
static unsafe int Main(string[] args) { var buildVersion = Assembly.GetEntryAssembly().GetName().Version.ToString(); Console.WriteLine($"Simple Audio Decoder Test App v{buildVersion}. Copyright (c) 2018 Cinegy LLC\n"); if (args.Length < 2) { Console.WriteLine("This sample application decodes an encoded audio elementary file (.mp3, .aac or whatever) into PCM-file\n"); Console.WriteLine("Usage: SimpleAudioDecoder.exe {MPEG|AAC|LATM|AES3} <input_file> [output_file]"); return(1); } Console.WriteLine($"Cinecoder version: {Cinecoder_.Version.VersionHi}.{Cinecoder_.Version.VersionLo}.{Cinecoder_.Version.EditionNo}.{Cinecoder_.Version.RevisionNo}\n"); string decClassName = null; switch (args[0].ToUpper()) { case "MPEG": decClassName = "MpegAudioDecoder"; break; case "AAC": decClassName = "AAC_AudioDecoder"; break; case "LATM": decClassName = "LATM_AAC_AudioDecoder"; break; case "AES3": decClassName = "Aes3AudioDecoder"; break; default: Console.Error.WriteLine($"Wrong audio decoder type specified: {args[0]}"); return(-1); } try { Cinecoder_.ErrorHandler = new ErrorHandler(); ICC_ClassFactory Factory = Cinecoder_.CreateClassFactory(); Factory.AssignLicense(License.Companyname, License.Licensekey); var audioDecoder = Factory.CreateInstanceByName(decClassName) as ICC_AudioDecoder; BinaryReader inputFile = new BinaryReader(new FileStream(args[1], FileMode.Open)); audioDecoder.OutputCallback = new AudioWriterCallback(args.Length > 2 ? args[2] : args[1] + ".pcm"); var buffer = new byte[8 * 1024]; for (;;) { int bytes_read = inputFile.Read(buffer, 0, buffer.Length); if (bytes_read == 0) break; fixed(byte *p = buffer) audioDecoder.ProcessData((IntPtr)p, (uint)bytes_read); } audioDecoder.Done(true); } catch (Exception e) { if (e.Message != null) { Console.Error.WriteLine($"Error: {e.Message}"); } return(e.HResult); } return(0); }
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); }
static unsafe int Main(string[] args) { var buildVersion = Assembly.GetEntryAssembly().GetName().Version.ToString(); Console.WriteLine($"Simple Audio Encoder Test App v{buildVersion}. Copyright (c) 2018 Cinegy LLC\n"); if (args.Length < 3) { Console.WriteLine("This sample application encodes a PCM audio file into elementary coded audio file (.mp3, .aac or whatever)\n"); Console.WriteLine("Usage: SimpleAudioEncoder.exe {MPEG|AAC} <input_file> <output_file> <switches>"); Console.WriteLine("Where switches are:"); Console.WriteLine(" /br=# - bitrate (Kbps)"); Console.WriteLine(" /nch=# - number of channels"); Console.WriteLine(" /freq=# - sample rate"); return(1); } Console.WriteLine($"Cinecoder version: {Cinecoder_.Version.VersionHi}.{Cinecoder_.Version.VersionLo}.{Cinecoder_.Version.EditionNo}.{Cinecoder_.Version.RevisionNo}\n"); try { Cinecoder_.ErrorHandler = new ErrorHandler(); ICC_ClassFactory Factory = Cinecoder_.CreateClassFactory(); Factory.AssignLicense(License.Companyname, License.Licensekey); int nch = 2; int freq = 48000; int br = 192; for (int i = 3; i < args.Length; i++) { if (args[i].ToLower().StartsWith("/br=")) { br = Int32.Parse(args[i].Substring(4)); } else if (args[i].ToLower().StartsWith("/nch=")) { nch = Int32.Parse(args[i].Substring(5)); } else if (args[i].ToLower().StartsWith("/freq=")) { freq = Int32.Parse(args[i].Substring(6)); } else { throw new Exception($"Unknow argument: {args[i]}"); } } ICC_AudioEncoder audioEncoder = null; if (args[0] == "MPEG") { if (nch != 1 && nch != 2) { throw new Exception("MPEG Audio Encoder: only 1 or 2 audio channels are supported"); } var encPar = Factory.CreateInstanceByName("MpegAudioEncoderSettings") as ICC_MpegAudioEncoderSettings; encPar.BitRate = br * 1000; encPar.ChannelMode = nch == 1 ? CC_MPG_AUDIO_CHANNEL_MODE.CC_MPG_ACH_MONO : CC_MPG_AUDIO_CHANNEL_MODE.CC_MPG_ACH_STEREO; encPar.SampleRate = (uint)freq; audioEncoder = Factory.CreateInstanceByName("MpegAudioEncoder") as ICC_AudioEncoder; audioEncoder.Init(encPar); } else if (args[0] == "AAC") { var encPar = Factory.CreateInstanceByName("AAC_AudioEncoderSettings") as ICC_AAC_AudioEncoderSettings; encPar.BitRate = br * 1000; encPar.NumChannels = (uint)nch; encPar.SampleRate = (uint)freq; audioEncoder = Factory.CreateInstanceByName("AAC_AudioEncoder") as ICC_AudioEncoder; audioEncoder.Init(encPar); } else { throw new Exception($"Unknown audio encoder type: {args[0]}"); } BinaryReader inputFile = new BinaryReader(new FileStream(args[1], FileMode.Open)); var outputFile = Factory.CreateInstanceByName("OutputFile") as ICC_OutputFile; outputFile.Create(args[2]); audioEncoder.OutputCallback = outputFile; var buffer = new byte[8 * 1024]; for (;;) { int bytes_read = inputFile.Read(buffer, 0, buffer.Length); if (bytes_read == 0) break; fixed(byte *p = buffer) audioEncoder.ProcessAudio(CC_AUDIO_FMT.CAF_PCM16, (IntPtr)p, (uint)bytes_read); } audioEncoder.Done(true); outputFile.Close(); } catch (Exception e) { if (e.Message != null) { Console.Error.WriteLine($"Error: {e.Message}"); } return(e.HResult); } return(0); }