Ejemplo n.º 1
0
        protected Format(AVCodecID eCodecID, IntPtr pAVCC, byte nThreads, AVFieldOrder eFieldsOrder)
            : this()
        {
            helper.Initialize();
            _pCodec     = NULL;
            nBufferSize = 0;
            int nResult = 0;

            _bEncode        = false;
            pAVCodecContext = pAVCC;
            _bAVCodecContextAllocationInternal = false;
            AVMediaType eAVMediaType = AVMediaType.AVMEDIA_TYPE_UNKNOWN;

            if (NULL != pAVCodecContext)
            {
                stAVCodecContext = (AVCodecContext)Marshal.PtrToStructure(pAVCodecContext, typeof(AVCodecContext));
                eAVMediaType     = stAVCodecContext.codec_type;
            }

            if (AVMediaType.AVMEDIA_TYPE_UNKNOWN == eAVMediaType)
            {
                if (CodecIDRawGet() != eCodecID)
                {
                    //if (AVCodecID.CODEC_ID_H264_MOBILE == eCodecID)
                    //	eCodecID = AVCodecID.CODEC_ID_H264;
                    if (NULL == (_pCodec = Functions.avcodec_find_encoder(eCodecID)))
                    {
                        throw new Exception("can't find codec " + eCodecID.ToString());
                    }
                }
                if (NULL == pAVCodecContext)
                {
                    //lock (helper._oSyncRootGlobal)
                    {
                        pAVCodecContext = Functions.avcodec_alloc_context3(_pCodec);
                        _bAVCodecContextAllocationInternal = true;
                    }
                }
                else
                {
                    //lock (helper._oSyncRootGlobal)
                    nResult = Functions.avcodec_get_context_defaults3(pAVCodecContext, _pCodec);
                }
                stAVCodecContext          = (AVCodecContext)Marshal.PtrToStructure(pAVCodecContext, typeof(AVCodecContext));
                stAVCodecContext.codec_id = eCodecID;
                _bEncode = true;
            }
            if (1 > nThreads)
            {
                nThreads = (byte)Environment.ProcessorCount;
            }
            stAVCodecContext.thread_count = nThreads;
            stAVCodecContext.field_order  = eFieldsOrder;
            Marshal.StructureToPtr(stAVCodecContext, pAVCodecContext, true);
        }
Ejemplo n.º 2
0
 public Video(ushort nWidth, ushort nHeight, AVCodecID eCodecID, PixelFormat ePixelFormat, IntPtr pAVCC, byte nThreads, AVFieldOrder eFieldsOrder)
     : this(nWidth, nHeight, eCodecID, ePixelFormat, pAVCC, nThreads, 800000, eFieldsOrder)
 {
 }
Ejemplo n.º 3
0
            public Video(ushort nWidth, ushort nHeight, AVCodecID eCodecID, PixelFormat ePixelFormat, IntPtr pAVCC, byte nThreads, uint nBitRate, AVFieldOrder eFieldsOrder)
                : base(eCodecID, pAVCC, nThreads, eFieldsOrder)
            {
                _oDisposeLock = new object();
                int nResult = 0;

                _ahTransformContexts = new Dictionary <Video, TransformContext>();
                nBufferSize          = Functions.avpicture_get_size(ePixelFormat, nWidth, nHeight);
                _nBitsPerPixel       = (ushort)(nBufferSize * 8 / (nWidth * nHeight));
                if (_bEncode)
                {
                    stAVCodecContext.codec_type    = AVMediaType.AVMEDIA_TYPE_VIDEO;
                    stAVCodecContext.field_order   = eFieldsOrder;                    // чо-то толку нет от полей (((
                    stAVCodecContext.width         = nWidth;
                    stAVCodecContext.height        = nHeight;
                    stAVCodecContext.pix_fmt       = ePixelFormat;
                    stAVCodecContext.time_base.num = 1;
                    stAVCodecContext.time_base.den = 25;                                                       //FPS
                    stAVCodecContext.bit_rate      = (int)(int.MaxValue < nBitRate ? int.MaxValue : nBitRate); //чем больше, тем лучше качество
                    if (NULL != _pCodec)
                    {
                        switch (eCodecID)
                        {
                        case AVCodecID.CODEC_ID_H264:
                            stAVCodecContext.gop_size     = 250;                                 //кол-во неключевых кадров между ключевыми. чем больше, тем легче для CPU
                            stAVCodecContext.max_b_frames = 2;
                            stAVCodecContext.keyint_min   = 25;                                  //минимальное кол-во неключевых кадров между ключевыми

                            //stAVCodecContext.flags |= (int)(CodecFlags.CODEC_FLAG_LOOP_FILTER | CodecFlags.CODEC_FLAG_4MV | CodecFlags.CODEC_FLAG_GLOBAL_HEADER);
                            //stAVCodecContext.flags2 |= (int)CodecFlags.CODEC_FLAG2_MIXED_REFS;
                            //stAVCodecContext.me_cmp |= (int)MotionCompare.FF_CMP_CHROMA;
                            //stAVCodecContext.max_qdiff = 4;
                            //stAVCodecContext.i_quant_factor = 0.71F;
                            //stAVCodecContext.qcompress = 0.6F;
                            //stAVCodecContext.gop_size = 250; //кол-во неключевых кадров между ключевыми. чем больше, тем легче для CPU
                            //stAVCodecContext.max_b_frames = 2;
                            //stAVCodecContext.keyint_min = 25; //минимальное кол-во неключевых кадров между ключевыми
                            if (0 > (nResult = Functions.av_opt_set(stAVCodecContext.priv_data, new StringBuilder("profile"), new StringBuilder("baseline"), 1)))
                            {
                                throw new Exception(helper.ErrorDescriptionGet(nResult));
                            }
                            if (0 > (nResult = Functions.av_opt_set(stAVCodecContext.priv_data, new StringBuilder("preset"), new StringBuilder("slow"), 0)))
                            {
                                throw new Exception(helper.ErrorDescriptionGet(nResult));
                            }
                            if (0 > (nResult = Functions.av_opt_set(stAVCodecContext.priv_data, new StringBuilder("tune"), new StringBuilder("zerolatency"), 0)))                                     //film,zerolatency,etc.
                            {
                                throw new Exception(helper.ErrorDescriptionGet(nResult));
                            }

                            break;

                        case AVCodecID.CODEC_ID_MPEG2VIDEO:
                            stAVCodecContext.max_b_frames = 2;
                            break;

                        case AVCodecID.CODEC_ID_MPEG1VIDEO:
                            stAVCodecContext.mb_decision = 2;
                            break;

                        default:
                            break;
                        }
                        Marshal.StructureToPtr(stAVCodecContext, pAVCodecContext, true);
                        ////lock (helper._oSyncRootGlobal)
                        {
                            if (0 > (nResult = Functions.avcodec_open2(pAVCodecContext, _pCodec, NULL)))
                            {
                                base.Dispose();
                                throw new Exception("can't open video codec:" + eCodecID.ToString() + "[" + helper.ErrorDescriptionGet(nResult) + "]");
                            }
                        }
                    }
                    else
                    {
                        Marshal.StructureToPtr(stAVCodecContext, pAVCodecContext, true);
                    }
                }
                else
                {
                    ////lock (helper._oSyncRootGlobal)
                    {
                        if (CodecIDRawGet() != eCodecID && 0 > Functions.avcodec_open2(pAVCodecContext, Functions.avcodec_find_decoder(eCodecID), NULL))
                        {
                            if (CodecIDRawGet() != eCodecID && 0 > Functions.avcodec_open2(pAVCodecContext, Functions.avcodec_find_decoder(eCodecID), NULL))
                            {
                                base.Dispose();
                                throw new Exception("can't open video codec:" + eCodecID.ToString());
                            }
                        }
                    }
                }
            }
Ejemplo n.º 4
0
 public Video(Video cFormat, IntPtr pAVCC, AVFieldOrder eFieldsOrder)
     : this(cFormat.nWidth, cFormat.nHeight, cFormat.eCodecID, cFormat.ePixelFormat, pAVCC, eFieldsOrder)
 {
 }
Ejemplo n.º 5
0
 public Video(Video cFormat, AVFieldOrder eFieldsOrder)
     : this(cFormat, NULL, eFieldsOrder)
 {
 }
Ejemplo n.º 6
0
 public Video(ushort nWidth, ushort nHeight, AVCodecID eCodecID, PixelFormat ePixelFormat, byte nThreads, AVFieldOrder eFieldsOrder)
     : this(nWidth, nHeight, eCodecID, ePixelFormat, NULL, nThreads, eFieldsOrder)
 {
 }
Ejemplo n.º 7
0
 public Video(ushort nWidth, ushort nHeight, PixelFormat ePixelFormat, byte nThreads, AVFieldOrder eFieldsOrder)
     : this(nWidth, nHeight, AVCodecID.CODEC_ID_RAWVIDEO, ePixelFormat, nThreads, eFieldsOrder)
 {
 }
Ejemplo n.º 8
0
 public Video(ushort nWidth, ushort nHeight, PixelFormat ePixelFormat, AVFieldOrder eFieldsOrder)
     : this(nWidth, nHeight, ePixelFormat, 0, eFieldsOrder)
 {
 }