예제 #1
0
                public void Process(Frame cFrameSource, Frame cFrameTarget)
                {
                    int     nResult        = 0;
                    AVFrame cAVFrameSource = (AVFrame)Marshal.PtrToStructure(cFrameSource, typeof(AVFrame));
                    AVFrame cAVFrameTarget = (AVFrame)Marshal.PtrToStructure(cFrameTarget, typeof(AVFrame));

                    nResult = Functions.sws_scale(pContext, cAVFrameSource.data, cAVFrameSource.linesize, 0, _cFormatSource.nHeight, cAVFrameTarget.data, cAVFrameTarget.linesize);
                }
예제 #2
0
        public Frame(Format.Audio cFormat, Frame cFrame)
            : this()
        {
            if (null != cFrame._aBuffer)
            {
                throw new NotImplementedException();
            }
            AVFrame cAVFrame = (AVFrame)Marshal.PtrToStructure(cFrame._pAVFrame, typeof(AVFrame));

            if (0 < cAVFrame.width || 0 < cAVFrame.height || 1 > cAVFrame.nb_samples)
            {
                throw new NotImplementedException();
            }
            int nLineSize = cFormat.nBitsPerSample / 8 * cAVFrame.nb_samples;

            //int nReminder = nLineSize % 64;
            //if(0 < nReminder)
            //	nLineSize += 64 - nReminder;
            _nLength = cFormat.nChannelsQty * nLineSize;
            _aBuffer = new byte[_nLength];
            (new Logger()).WriteDebug4("frame format_frame: - new bytes processed! [size=" + _nLength + "]");
            bool bPlanar = (1 < cAVFrame.data.Count(o => NULL != o));

            if (!bPlanar)
            {
                nLineSize = nLength;
            }
            for (int nIndx = 0; cAVFrame.data.Length > nIndx; nIndx++)
            {
                if (NULL == cAVFrame.data[nIndx])
                {
                    break;
                }
                Marshal.Copy(cAVFrame.data[nIndx], _aBuffer, nIndx * nLineSize, nLineSize);
            }
            Init(cFormat);
        }
예제 #3
0
        public void PassSamples(Format.Audio cFormat)
        {
            if (1 > _nLength)
            {
                throw new NotImplementedException();
            }

            AVFrame cAVFrame = (AVFrame)Marshal.PtrToStructure(_pAVFrame, typeof(AVFrame));

            if (0 < cAVFrame.width || 0 < cAVFrame.height || 1 > cAVFrame.nb_samples)
            {
                throw new NotImplementedException();
            }
            int nLineSize = cFormat.nBitsPerSample / 8 * cAVFrame.nb_samples;

            _nLength = cFormat.nChannelsQty * nLineSize;
            if (null == _aBuffer)
            {
                _aBuffer = new byte[_nLength];
                (new Logger()).WriteDebug("passSamples: - new bytes processed! [size=" + _nLength + "]");
            }
            bool bPlanar = (1 < cAVFrame.data.Count(o => NULL != o));

            if (!bPlanar)
            {
                nLineSize = nLength;
            }
            for (int nIndx = 0; cAVFrame.data.Length > nIndx; nIndx++)
            {
                if (NULL == cAVFrame.data[nIndx])
                {
                    break;
                }
                Marshal.Copy(cAVFrame.data[nIndx], _aBuffer, nIndx * nLineSize, nLineSize);
            }
        }
예제 #4
0
            override public Frame[] Convert(Format cFormatTarget, Frame cFrameSource)             //в pAVFrameSource лежат байты в формате this!!!
            {
                List <Frame> aRetVal = new List <Frame>();

                if (null == cFormatTarget || !(cFormatTarget is Format.Video))
                {
                    throw new Exception("target format is null or has a wrong type");
                }
                Format.Video cFormatVideoTarget = (Format.Video)cFormatTarget;
                try
                {
                    if (ePixelFormat == cFormatVideoTarget.ePixelFormat && nHeight == cFormatVideoTarget.nHeight && nWidth == cFormatVideoTarget.nWidth)
                    {
                        return new Frame[] { new Frame(cFrameSource.aBytesCopy)
                                             {
                                                 nPTS = cFrameSource.nPTS, bKeyframe = cFrameSource.bKeyframe
                                             } }
                    }
                    ;
                    if (eCodecID == cFormatTarget.eCodecID || NULL != _pCodec)
                    {
                        throw new NotImplementedException(); //TODO доделать конверт из encoded в raw
                    }
                    cFrameSource = Transform(cFormatVideoTarget, cFrameSource);

                    int nSize;
                    if (NULL == cFrameSource)
                    {
                        (new Logger()).WriteWarning("Format.Video.Convert: IntPtr.Zero == cFrameSource.AVFrameGet()");
                    }
                    if (NULL == cFormatVideoTarget.pAVCodecContext)
                    {
                        (new Logger()).WriteWarning("Format.Video.Convert: IntPtr.Zero == cFormatVideoTarget.pAVCodecContext");
                    }
                    if (null == _cFrame)
                    {
                        _cFrame      = new Frame(cFormatVideoTarget.nBufferSize);
                        _cFrame.nPTS = 0;
                    }
                    cFrameSource.nPTS = _cFrame.nPTS;
                    nSize             = Functions.avcodec_encode_video(cFormatVideoTarget.pAVCodecContext, _cFrame.aBuffer, _cFrame.nLengthBuffer, cFrameSource.pBytes);
                    if (0 > nSize)
                    {
                        throw new Exception("video encoding failed:" + nSize);
                    }
                    if (0 < nSize)
                    {
                        aRetVal.Add(new Frame(null, _cFrame.aBuffer.Take(nSize).ToArray()));

                        AVCodecContext stAVCodecContext = (AVCodecContext)Marshal.PtrToStructure(cFormatVideoTarget.pAVCodecContext, typeof(AVCodecContext));
                        if (NULL != stAVCodecContext.coded_frame)
                        {
                            AVFrame cAVFrame = (AVFrame)Marshal.PtrToStructure(stAVCodecContext.coded_frame, typeof(AVFrame));
                            aRetVal[0].nPTS      = cAVFrame.pts;
                            aRetVal[0].bKeyframe = 0 < cAVFrame.key_frame;
                        }
                    }
                    _cFrame.nPTS++;
                }
                catch (Exception ex)
                {
                    (new Logger()).WriteError(ex);
                }
                return(aRetVal.ToArray());
            }