在directshow上创建了一个UYVY图像,它已经损坏,我不知道为什么

时间:2011-09-08 20:16:50

标签: c# directshow directshow.net

我有来自相机的UYVY数据缓冲区,我使用GSSF Directshow.net过滤器通过图形推送缓冲区。

目前的图表是

GSSF -> YUV Transform -> AVI Splitter -> Video Renderer

图表正确计算颜色并正确显示,但图像中的条形图应该不在那里,我不知道它们来自哪里。盯着图像会伤到我的眼睛。

enter image description here

此函数获取UYVY缓冲区(mainbyte)并将其复制到整数数组

        unsafe public void ImageFromPixels__()
    {
        byte[] x = mainbyte;
        long fff = 720 * 1280;
        mainptr = new IntPtr(fff);
        for (int p = 0; p < 720 * 1280; p++)
        {
            U = (x[p * 4 + 0]);

            Y = (x[p * 4 + 1]);
            V = (x[p * 4 + 2]);
            Y2 = (x[p * 4 + 3]);

           // int one = V << 16 | Y << 8 | U;
           // int two = V << 16 | Y2 << 8 | U;
            int one = Y2 << 24 | V << 16 | Y << 8 | U;
           // mainint[p * 2 + 0] = one;
           // mainint[p * 2 + 1] = two;
            mainint[p] = one;

        }

        m_FPS = UNIT / 20;
        m_b = 211;
        m_g = 197;
    }

此函数获取相同的整数数组并将其打包到GSSF流指针

        override unsafe public int GetImage(int iFrameNumber, IntPtr ip, int iSize, out int iRead)
    {
        int hr = 0;

        if (iFrameNumber>-1)
        {
            if (iFrameNumber < MAXFRAMES)
            {
                ImageFromPixels_(20, mainbyte);

                m_g += 3;
                m_b += 7;
                int* bp = (int*)ip.ToPointer();
                Random k = new Random();

                StreamReader s = new StreamReader("jpegFile.txt");
                for (int f = 0; f < HEIGHT; f++)
                {
                    for (int x = 0; x < (WIDTH); x += 1)
                    {
                        *(bp + (f * WIDTH) + x) = mainint[f * 1280 + x];
                    }
                }
            }
            else
            {
                hr = 1; // End of stream
            }
        }

        iRead = iSize;

        return hr;
    }

这为GSSF的输出引脚设置位图压缩 我想我可能在这里做错了但看起来不错。

        override public void SetMediaType(IGenericSampleConfig psc)
    {
        BitmapInfoHeader bmi = new BitmapInfoHeader();

        // Build a BitmapInfo struct using the parms from the file
        bmi.Size = Marshal.SizeOf(typeof(BitmapInfoHeader));
        bmi.Width = WIDTH;
        bmi.Height = HEIGHT * -1;
        bmi.Planes = 1;
        bmi.BitCount = BPP;
        bmi.Compression = 0x59565955; //UYVY
        bmi.ImageSize = (bmi.BitCount / 8) * bmi.Width * bmi.Height;
        bmi.XPelsPerMeter = 0;
        bmi.YPelsPerMeter = 0;
        bmi.ClrUsed = 0;
        bmi.ClrImportant = 0;

        int hr = psc.SetMediaTypeFromBitmap(bmi, m_FPS);
        DsError.ThrowExceptionForHR(hr);
    }

更新 改变它升技

        override unsafe public int GetImage(int iFrameNumber, IntPtr ip, int iSize, out int iRead)
    {
        int hr = 0;

        if (iFrameNumber>-1)
        {
            if (iFrameNumber < MAXFRAMES)
            {
                ImageFromPixels_(20, mainbyte);

                m_g += 3;
                m_b += 7;
                int* bp = (int*)ip.ToPointer();
                Random k = new Random();
                StreamReader s = new StreamReader("jpegFile.txt");
                for (int f = 0; f < 720; f++)
                {
                    for (int x = 0; x < (1280); x += 1)
                    {
                        *(bp + (f * 1280) + x) = mainint[f * 1280 + x];
                    }
                }
            }
            else
            {
                hr = 1; // End of stream
            }
        }

//             override public void SetMediaType(IGenericSampleConfig psc)         {             BitmapInfoHeader bmi = new BitmapInfoHeader();

        // Build a BitmapInfo struct using the parms from the file
        bmi.Size = Marshal.SizeOf(typeof(BitmapInfoHeader));
        bmi.Width = WIDTH;
        bmi.Height = HEIGHT * -1;
        bmi.Planes = 1;
        bmi.BitCount = BPP;
        bmi.Compression = 0x59565955;
        bmi.ImageSize = (bmi.BitCount / 8) * bmi.Width * bmi.Height;
        bmi.XPelsPerMeter = 0;
        bmi.YPelsPerMeter = 0;
        bmi.ClrUsed = 0;
        bmi.ClrImportant = 0;

        int hr = psc.SetMediaTypeFromBitmap(bmi, m_FPS);
        DsError.ThrowExceptionForHR(hr);
    }

//

        unsafe public void ImageFromPixels_(long FPS, byte[] x)
    {

        long fff = 720 * 1280 * 3;
        mainptr = new IntPtr(fff);
        for (int p = 0; p < 720 * 640; p++)
        {
            U = (x[ p * 4 + 0]);

            Y = (x[p * 4 + 1]);
            V = (x[p * 4 + 2]);
            Y2 = (x[p * 4 + 3]);

            int one = Y2 << 24 | V << 16 | Y << 8 | U;
            //int one = V << 16 | Y << 8 | U;
            //int two = V << 16 | Y2 << 8 | U;
            //mainint[p * 2 + 0] = one;
            //mainint[p * 2 + 1] = two;
            mainint[p] = one;

        }

        m_FPS = UNIT / FPS;
        m_b = 211;
        m_g = 197;
    }

enter image description here

如果我更改GetImage中的其他数字以改变视频的高度,或者如果我在ImagePixel中更改它,那么我只是得到黑屏:|

2 个答案:

答案 0 :(得分:1)

指针数学很糟糕。从

更改了我的代码
for (int f = 0; f < 720; f++)
{
    for (int x = 0; x < (1280); x += 1)
    {
        *(bp + (f * 1280) + x) = mainbyte[f * 1280 + x];
    }
}

Marshal.Copy(mainbyte, 0, ip, 1280*720);

并将BPP从32改为16,现在它可以毫无障碍地工作。

&LT; _&LT;

答案 1 :(得分:0)

你的循环如何达到(720 * 1280),这是图片中每个像素的一次迭代,但在循环中你正在做Y和Y2。看起来不对。