Example #1
0
        private AVFrame *get_avframe(int pix_fmt, int width, int height)
        {
            if (m_width != width || m_height != height)
            {
                rgb_buffer = null;

                int size = FFmpeg.avpicture_get_size((PixelFormat)pix_fmt, width, height);
                if (size > 0)
                {
                    rgb_buffer = new byte[size];
                }
                if (rgb_buffer == null)
                {
                    return(null);
                }

                if (rgb_frame == null)
                {
                    rgb_frame = FFmpeg.avcodec_alloc_frame();
                }
                AVPicture *temp = (AVPicture *)rgb_frame;
                FFmpeg.avpicture_fill(out *temp, rgb_buffer, (PixelFormat)pix_fmt, width, height);
                m_width  = (short)width;
                m_height = (short)height;
            }
            return(rgb_frame);
        }
        public void InternalRelease()
        {
            lock (ReleaseLock)
            {
                if (Picture != null)
                {
                    ffmpeg.av_free(Picture);
                }

                if (PictureBuffer != null)
                {
                    ffmpeg.av_free(PictureBuffer);
                }

                Picture             = null;
                PictureBuffer       = null;
                PictureBufferPtr    = IntPtr.Zero;
                PictureBufferLength = 0;
            }
        }
        public void InternalRelease()
        {
            lock (ReleaseLock)
            {
                if (Picture != null)
                    ffmpeg.av_free(Picture);

                if (PictureBuffer != null)
                    ffmpeg.av_free(PictureBuffer);

                Picture = null;
                PictureBuffer = null;
                PictureBufferPtr = IntPtr.Zero;
                PictureBufferLength = 0;
            }
        }
        public void Open(string FileName)
        {
            DecoderConfig.Init();

            AVFormatContext* pFormatContext = FFmpegInvoke.avformat_alloc_context();
            _pFormatContext = pFormatContext;

            if (FFmpegInvoke.avformat_open_input(&pFormatContext, FileName, null, null) != 0)
                throw new Exception("Could not open file");

            if (FFmpegInvoke.avformat_find_stream_info(pFormatContext, null) != 0)
                throw new Exception("Could not find stream info");

            for (int i = 0; i < pFormatContext->nb_streams; i++)
            {
                if (pFormatContext->streams[i]->codec->codec_type == AVMediaType.AVMEDIA_TYPE_VIDEO)
                {
                    _pStream = pFormatContext->streams[i];
                    break;
                }
            }

            if (_pStream == null)
                throw new Exception("Could not found video stream");
            AVCodecContext codecContext = *(_pStream->codec);
            codecContext.workaround_bugs = FFmpegInvoke.FF_BUG_AUTODETECT;

            _frameduration = 1 / q2d(_pStream->r_frame_rate);
            FrameCount = _pStream->nb_frames;
            Duration = (float)pFormatContext->duration / FFmpegInvoke.AV_TIME_BASE;
            Width = codecContext.width;
            Height = codecContext.height;

            AVPixelFormat sourcePixFmt = codecContext.pix_fmt;
            AVCodecID codecId = codecContext.codec_id;
            var convertToPixFmt = AVPixelFormat.AV_PIX_FMT_RGB24;
            _pConvertContext = FFmpegInvoke.sws_getContext(Width, Height, sourcePixFmt,
                                                                       Width, Height, convertToPixFmt,
                                                                       FFmpegInvoke.SWS_FAST_BILINEAR, null, null, null);

            if (_pConvertContext == null)
                throw new Exception("Could not initialize the conversion context");

            _pConvertedFrame = (AVPicture*)FFmpegInvoke.avcodec_alloc_frame();
            int convertedFrameBufferSize = FFmpegInvoke.avpicture_get_size(convertToPixFmt, Width, Height);
            _pConvertedFrameBuffer = (byte*)FFmpegInvoke.av_malloc((uint)convertedFrameBufferSize);
            FFmpegInvoke.avpicture_fill(_pConvertedFrame, _pConvertedFrameBuffer, convertToPixFmt, Width, Height);

            AVCodec* pCodec = FFmpegInvoke.avcodec_find_decoder(codecId);
            if (pCodec == null)
                throw new Exception("Unsupported codec");

            if (FFmpegInvoke.avcodec_open2(_pStream->codec, pCodec, null) < 0)
                throw new Exception("Could not open codec");

            _pDecodedFrame = FFmpegInvoke.avcodec_alloc_frame();

            _packet = new AVPacket();

            fixed (AVPacket* pPacket = &_packet)
            {
                FFmpegInvoke.av_init_packet(pPacket);
            }

            _opened = true;
        }
        public void Close()
        {
            if (!_opened)
                return;
            FFmpegInvoke.av_free(_pConvertedFrame);
            FFmpegInvoke.av_free(_pConvertedFrameBuffer);
            FFmpegInvoke.sws_freeContext(_pConvertContext);

            FFmpegInvoke.av_free(_pDecodedFrame);
            FFmpegInvoke.avcodec_close(_pStream->codec);
            fixed (AVFormatContext** pFormatContext = &_pFormatContext)
            {
                FFmpegInvoke.avformat_close_input(pFormatContext);
            }

            _videoClock = 0;
            _pFormatContext = null;
            _pStream = null;
            _pDecodedFrame = null;
            _pConvertedFrame = null;
            _pConvertedFrameBuffer = null;
            _pConvertContext = null;
            _opened = false;
        }
Example #6
0
 public static extern int av_picture_pad(AVPicture *dst, AVPicture *src, int height, int width, AVPixelFormat pix_fmt, int padtop, int padbottom, int padleft, int padright, int *color);
Example #7
0
 public static extern int av_picture_crop(AVPicture *dst, AVPicture *src, AVPixelFormat pix_fmt, int top_band, int left_band);
Example #8
0
 public static extern void av_picture_copy(AVPicture *dst, AVPicture *src, AVPixelFormat pix_fmt, int width, int height);
Example #9
0
 public static extern int avpicture_layout(AVPicture *src, AVPixelFormat pix_fmt, int width, int height, byte *dest, int dest_size);
Example #10
0
 public static extern int avpicture_fill(AVPicture *picture, byte *ptr, AVPixelFormat pix_fmt, int width, int height);
Example #11
0
 public static extern void avpicture_free(AVPicture *picture);
Example #12
0
 public static extern int avpicture_alloc(AVPicture *picture, AVPixelFormat pix_fmt, int width, int height);