public static void Test() { int width = 320, height = 240; X264Native x264 = new X264Native(new X264Params(width, height, 10, 320)); //x264.SetIKeyIntMax(10); x264.Init(); var ls = MediaServer.Media.ReadFile.GetBuffByFile1(@"D:\video_monitor\v2v_super_exchange\branch\branch_V3.16.0.0(dongtaifuyong)\bin\Debug\1237.yuv"); AVCodecCfg cf = AVCodecCfg.CreateVideo(width, height, (int)SS.Media.AVCode.CODEC_ID_H264, 100000); FFImp ffimp = new FFImp(cf, true); //FFScale ffscale = new FFScale(width, height, 26, 12, width, height, 12, 12); FFScale ffscale = new FFScale(width, height, 0, 12, width, height, 3, 24); foreach (var item1 in ls) { var item = ffscale.FormatS(item1); var in_buf = FunctionEx.BytesToIntPtr(item); var out_buf = Marshal.AllocHGlobal(item.Length); //var bKeyFrame = false; //var nOutLen = 0; var nInLen = item.Length; // var size = X264Encode(x264.obj, in_buf, ref nInLen, out_buf, ref nOutLen, ref bKeyFrame); // var buf = FunctionEx.IntPtrToBytes(out_buf, 0, size); var buf = x264.Encode(item); Console.WriteLine(buf.To16Strs(0, 16)); var size = buf.Length; if (w == null) //OK { w = new BinaryWriter(new FileStream("4567.es", FileMode.Create)); } w.Write(buf); ////Media.MediaFrame mf = new Media.MediaFrame(); ////mf.nIsKeyFrame = (byte)(x264.IsKeyFrame() ? 1 : 0); ////mf.nWidth = width; ////mf.nHeight = height; ////mf.nEncoder = Media.MediaFrame.H264Encoder; ////mf.nTimetick = 0; ////mf.nSize = size; ////mf.Data = buf; ////buf = mf.GetBytes(); ////fs.Write(BitConverter.GetBytes(buf.Length), 0, 4); //fs.Write(buf, 0, buf.Length); //fs.Flush(); //IntPtr intt = IntPtr.Zero; //var sssss = ffimp.VideoDec(buf, ref intt); ////Console.WriteLine(buf.Take(32).ToArray().To16Strs()); //// var size = Encode1(ii, in_buf, ref nInLen, out_buf); } //fs.Close(); }
public static AVCodecCfg CreateVideo(int width, int height, int codec_id = (int)AVCode.CODEC_ID_H264, int bit_rate = 98000) { var r = new AVCodecCfg(); r.codec_id = codec_id; r.width = width; r.height = height; r.codec_type = (int)AVCode.CODEC_TYPE_VIDEO; r.bit_rate = bit_rate; r.time_base_den = 15; r.time_base_num = 2; r.gop_size = 15; r.pix_fmt = (int)AVCode.PIX_FMT_YUV420P; return(r); }
public static AVCodecCfg CreateAudio(int channels, int sample_rate, int codec_id = (int)AVCode.CODEC_ID_H264, int bit_rate = 64000) { var r = new AVCodecCfg(); r.codec_id = codec_id; r.codec_type = (int)AVCode.CODEC_TYPE_AUDIO; r.bit_rate = bit_rate; r.time_base_den = 0; r.time_base_num = 0; r.gop_size = 0; r.pix_fmt = 0; r.max_b_frames = 0; r.sample_rate = sample_rate; r.channels = channels; return(r); }
public FFImp(AVCodecCfg cfg, bool isDec, bool isvideo = true) { this.isDec = isDec; this.cfg = cfg; var pcfg = FunctionEx.StructToIntPtr(cfg); pAVObj = FunctionEx.StructToIntPtr(new AVModel()); lock (_lock) { if (!ffimp_init()) { throw new Exception("ffimp init error"); } } if (isvideo) { int pOutBufSize = cfg.width * cfg.height * 3; if (pOutBufSize == 0) { pOutBufSize = 1920 * 1080 * 4; } pOutBuf = Marshal.AllocHGlobal(pOutBufSize); int init_r = 0; lock (_lock) { if (this.isDec) { init_r = ffimp_video_decode_init(ref pAVObj, pcfg, pOutBuf, pOutBufSize); } else { init_r = ffimp_video_encode_init(ref pAVObj, pcfg, pOutBuf, pOutBufSize); } _ffscale = new FFScale(cfg.width, cfg.height, 0, 12, cfg.width, cfg.height, 0, 12); } } else { int pOutBufSize = cfg.width * cfg.height * 3; if (pOutBufSize == 0) { pOutBufSize = 2048 * 2; } pOutBuf = Marshal.AllocHGlobal(pOutBufSize); int init_r = 0; lock (_lock) { if (this.isDec) { init_r = ffimp_audio_decode_init(ref pAVObj, pcfg, pOutBuf, pOutBufSize); } else { init_r = ffimp_audio_decode_init(ref pAVObj, pcfg, pOutBuf, pOutBufSize); } // _ffscale = new FFScale(cfg.width, cfg.height, 0, 12, cfg.width, cfg.height, 0, 12); } } Marshal.FreeHGlobal(pcfg); }