Exemplo n.º 1
0
        private static Mat BgraToMat(MediaFrame frame)
        {
            Mat mat    = new Mat(frame.AVFrame.height, frame.AVFrame.width, MatType.CV_8UC4);
            int stride = (int)(uint)mat.Step();

            for (int i = 0; i < frame.AVFrame.height; i++)
            {
                FFmpegHelper.CopyMemory(mat.Data + i * stride, frame.Data[0] + i * frame.AVFrame.linesize[0], (uint)stride);
            }
            return(mat);
        }
Exemplo n.º 2
0
        private static Mat BgraToMat(MediaFrame frame)
        {
            Mat mat    = new Mat(frame.AVFrame.height, frame.AVFrame.width, DepthType.Cv8U, 4);
            int stride = mat.Step;

            for (int i = 0; i < frame.AVFrame.height; i++)
            {
                FFmpegHelper.CopyMemory(mat.DataPointer + i * stride, frame.Data[0] + i * frame.AVFrame.linesize[0], (uint)stride);
            }
            return(mat);
        }
Exemplo n.º 3
0
        private static Mat AudioFrameToMat(AudioFrame frame)
        {
            int planar      = ffmpeg.av_sample_fmt_is_planar((AVSampleFormat)frame.AVFrame.format);
            int planes      = planar != 0 ? frame.AVFrame.channels : 1;
            int block_align = ffmpeg.av_get_bytes_per_sample((AVSampleFormat)frame.AVFrame.format) * (planar != 0 ? 1 : frame.AVFrame.channels);
            int stride      = frame.AVFrame.nb_samples * block_align;
            int channels    = planar != 0 ? 1 : frame.AVFrame.channels;

            MatType dstType;

            switch ((AVSampleFormat)frame.AVFrame.format)
            {
            case AVSampleFormat.AV_SAMPLE_FMT_U8:
            case AVSampleFormat.AV_SAMPLE_FMT_U8P:
                dstType = MatType.CV_8UC(channels);
                break;

            case AVSampleFormat.AV_SAMPLE_FMT_S16:
            case AVSampleFormat.AV_SAMPLE_FMT_S16P:
                dstType = MatType.CV_16SC(channels);
                break;

            case AVSampleFormat.AV_SAMPLE_FMT_S32:
            case AVSampleFormat.AV_SAMPLE_FMT_S32P:
                dstType = MatType.CV_32SC(channels);
                break;

            case AVSampleFormat.AV_SAMPLE_FMT_FLT:
            case AVSampleFormat.AV_SAMPLE_FMT_FLTP:
                dstType = MatType.CV_32FC(channels);
                break;

            case AVSampleFormat.AV_SAMPLE_FMT_DBL:
            case AVSampleFormat.AV_SAMPLE_FMT_DBLP:
            // opencv not have 64S, use 64F
            case AVSampleFormat.AV_SAMPLE_FMT_S64:
            case AVSampleFormat.AV_SAMPLE_FMT_S64P:
                dstType = MatType.CV_64FC(channels);
                break;

            default:
                throw new FFmpegException(FFmpegException.NotSupportFormat);
            }

            Mat mat = new Mat(planes, frame.AVFrame.nb_samples, dstType);

            for (int i = 0; i < planes; i++)
            {
                FFmpegHelper.CopyMemory(mat.Data + i * stride, frame.Data[i], (uint)stride);
            }
            return(mat);
        }
Exemplo n.º 4
0
        private static AudioFrame MatToAudioFrame(Mat mat, AVSampleFormat srctFormat, int sampleRate)
        {
            int        channels = mat.Channels() > 1 ? mat.Channels() : mat.Height;
            AudioFrame frame    = new AudioFrame(srctFormat, channels, mat.Width, sampleRate);
            bool       isPlanar = ffmpeg.av_sample_fmt_is_planar(srctFormat) > 0;
            int        stride   = (int)mat.Step();

            for (int i = 0; i < (isPlanar ? channels : 1); i++)
            {
                FFmpegHelper.CopyMemory(frame.Data[i], mat.Data + i * stride, (uint)stride);
            }
            return(frame);
        }
Exemplo n.º 5
0
 private static VideoFrame MatToVideoFrame(Mat mat)
 {
     using (Image <Bgr, byte> image = mat.ToImage <Bgr, byte>())
     {
         VideoFrame frame  = new VideoFrame(AVPixelFormat.AV_PIX_FMT_BGR24, image.Width, image.Height);
         int        stride = image.Width * image.NumberOfChannels;
         for (int i = 0; i < frame.AVFrame.height; i++)
         {
             FFmpegHelper.CopyMemory(frame.Data[0] + i * frame.AVFrame.linesize[0], image.Mat.DataPointer + i * stride, (uint)stride);
         }
         return(frame);
     }
 }
Exemplo n.º 6
0
        private static VideoFrame MatToVideoFrame(Mat mat)
        {
            if (mat.Type() != MatType.CV_8UC3)
            {
                throw new FFmpegException(FFmpegException.NotSupportFormat);
            }
            VideoFrame frame  = new VideoFrame(AVPixelFormat.AV_PIX_FMT_BGR24, mat.Width, mat.Height);
            int        stride = (int)mat.Step();

            for (int i = 0; i < frame.AVFrame.height; i++)
            {
                FFmpegHelper.CopyMemory(frame.Data[0] + i * frame.AVFrame.linesize[0], mat.Data + i * stride, (uint)stride);
            }
            return(frame);
        }
Exemplo n.º 7
0
        public VideoFrame ConvertTo(Bitmap bitmap)
        {
            if (bitmap.PixelFormat != PixelFormat.Format24bppRgb)
            {
                throw new Exception("only support Format24bppRgb format");
            }
            int        width      = bitmap.Width;
            int        height     = bitmap.Height;
            VideoFrame frame      = new VideoFrame(AVPixelFormat.AV_PIX_FMT_BGR24, width, height);
            BitmapData bitmapData = bitmap.LockBits(new Rectangle(0, 0, width, height), ImageLockMode.ReadWrite, bitmap.PixelFormat);
            // do not use frame.Data[0] = bitmapData.Scan0
            // frame.Linesize[0] may not be equal bitmapData.Stride and both of them may not be equal to width * 3,
            // because of memory alignment
            int stride = Math.Min(bitmapData.Stride, frame.Linesize[0]);

            for (int i = 0; i < height; i++)
            {
                FFmpegHelper.CopyMemory(bitmapData.Scan0 + i * bitmapData.Stride, frame.Data[0] + i * frame.Linesize[0], (uint)stride);
            }
            bitmap.UnlockBits(bitmapData);
            return(frame);
        }