Example #1
0
        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)SLW.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();
        }
        protected void CameraCapturer_CallBack(byte[] yuv)
        {
            if (!_isworking)
            {
                return;
            }
            try
            {
                Draw(yuv);

                if ((Environment.TickCount - _lastTick) < 1000 / _fps)
                {
                    return;
                }

                _lastTick = Environment.TickCount;

                var yv12 = _ffscale.Convert(yuv);

                if (_needForceIDRFrame)
                {
                    _needForceIDRFrame = false;
                    _x264.ForceIDRFrame();
                }

                var enc = _x264.Encode(yv12);



                var mf = new MediaFrame()
                {
                    nWidth            = _cfgVideo.width,
                    nHeight           = _cfgVideo.height,
                    nEx               = 1,
                    nIsAudio          = 0,
                    nEncoder          = MediaFrame.H264Encoder,
                    nIsKeyFrame       = (byte)(_x264.IsKeyFrame() ? 1 : 0),
                    MediaFrameVersion = 0,
                    nPPSLen           = 0,
                    nSPSLen           = 0,
                    Data              = enc,
                    nSize             = enc.Length,
                    nTimetick         = Environment.TickCount,
                };

                if (mf.nIsKeyFrame == 1)
                {
                    var sps_pps = SLW.ClientBase.Media.MediaSteamConverter.GetSPS_PPS(enc);
                    mf.nSPSLen = (short)sps_pps[0].Length;
                    mf.nPPSLen = (short)sps_pps[1].Length;
                }
                mf.MediaFrameVersion = mf.nIsKeyFrame;



                if (_needClearVideoTransportBuffer)
                {
                    _needClearVideoTransportBuffer = false;
                    var frame = CreateClearVideoTransportBufferMediaFrame(mf);
                    if (_callBack != null)
                    {
                        _callBack(frame);
                    }
                }

                if (_isFirstKeyFrame)
                {
                    if (mf.nIsKeyFrame == 1)
                    {
                        mf.nEx = 0;
                    }
                    _isFirstKeyFrame = false;
                    var frame = CreateResetCodecMediaFrame(mf);
                    if (_callBack != null)
                    {
                        _callBack(frame);
                    }
                }

                if (_callBack != null)
                {
                    _callBack(mf);
                }
            }
            catch (Exception e)
            {
                if (_isworking)
                {
                    throw;
                }
            }
        }