首页 > yuv420格式的数据播放时黑白的,我怎么把它转为yv12的?

yuv420格式的数据播放时黑白的,我怎么把它转为yv12的?

我借鉴网上的代码播放yuv数据,原代码是播放yv12格式的数据,但是我播放的不同,所以界面是黑白的,怎么修改代码

  case FrameFormat.YV12:
                    this.yStride = videoWidth;
                    this.yHeight = videoHeight;
                    this.ySize = videoWidth * videoHeight;
                    this.uvStride = this.yStride >> 1;
                    this.uvHeight = this.yHeight >> 1;
                    this.uvSize = this.ySize >>2;
                    break;

                case FrameFormat.NV12:
                    this.yStride = videoWidth;
                    this.yHeight = videoHeight;
                    this.ySize = videoWidth * videoHeight;
                    this.uvStride = this.yStride;
                    this.uvHeight = this.yHeight >> 1;
                    this.uvSize = this.ySize >> 1;
                    break;

                case FrameFormat.YUY2:
                case FrameFormat.UYVY:
                case FrameFormat.RGB15: // rgb555
                case FrameFormat.RGB16: // rgb565
                    this.yStride = videoWidth << 1;
                    this.yHeight = videoHeight;
                    this.ySize = this.yStride * this.yHeight;
                    this.uvStride = this.uvHeight = this.uvSize = 0;
                    break;

                case FrameFormat.RGB32:
                case FrameFormat.ARGB32:
                    this.yStride = videoWidth << 2;
                    this.yHeight = videoHeight;
                    this.ySize = this.yStride * this.yHeight;
                    this.uvStride = this.uvHeight = this.uvSize = 0;
                    break;

                default:
                    return false;
  private void FillBuffer(IntPtr bufferPtr)
        {
            if (this.inputSurface == null)
            {
                return;
            }

            DataRectangle rect = this.inputSurface.LockRectangle(LockFlags.None);
            IntPtr surfaceBufferPtr = rect.Data.DataPointer;
            switch (this.frameFormat)
            {
                case FrameFormat.YV12://修改为yuv420
                    #region 填充YV12数据
                    if (rect.Pitch == this.yStride)
                    {
                        Interop.Memcpy(surfaceBufferPtr, bufferPtr, this.ySize + this.uvSize + this.uvSize);
                    }
                    else
                    {
                        IntPtr srcPtr = bufferPtr; // Y
                        int yPitch = rect.Pitch;
                        for (int i = 0; i < this.yHeight; i++)
                        {
                            Interop.Memcpy(surfaceBufferPtr, srcPtr, this.yStride);
                            surfaceBufferPtr += yPitch;
                            srcPtr += this.yStride;
                        }

                        int uvPitch = yPitch >> 1;
                        for (int i = 0; i < yHeight; i++) // UV一起copy, uHeight + vHeight = yHeight
                        {
                            Interop.Memcpy(surfaceBufferPtr, srcPtr, this.uvStride);
                            surfaceBufferPtr += uvPitch;
                            srcPtr += this.uvStride;
                        }
                    }
                    #endregion
                    break;

                case FrameFormat.NV12:
                    #region 填充NV12. uBuffer指向UV打包数据。vBuffer为空

                    if (rect.Pitch == this.yStride)
                    {
                        Interop.Memcpy(surfaceBufferPtr, bufferPtr, this.ySize + this.uvSize);
                    }
                    else
                    {
                        // uv打包保存,uvWidth与yWidth相同, 因此可以合并在一个循环
                        IntPtr srcPtr = bufferPtr; 
                        for (int i = 0; i < this.yHeight + this.uvHeight; i++)
                        {
                            Interop.Memcpy(surfaceBufferPtr, srcPtr, this.yStride);
                            surfaceBufferPtr += rect.Pitch;
                            srcPtr += this.yStride;
                        }
                    }
                    #endregion
                    break;

                // 打包格式
                case FrameFormat.YUY2:
                case FrameFormat.UYVY:
                case FrameFormat.RGB15:
                case FrameFormat.RGB16:
                case FrameFormat.RGB24:
                case FrameFormat.RGB32:
                case FrameFormat.ARGB32:
                default:
                    #region 填充buffer。此时,所有数据都在yBuffer里,其他两个buffer无效
                    if (rect.Pitch == this.yStride)
                    {
                        Interop.Memcpy(surfaceBufferPtr, bufferPtr, this.ySize); // ySize此时等于整个dataSize
                    }
                    else
                    {
                        IntPtr srcPtr = bufferPtr;
                        for (int i = 0; i < this.yHeight; i++)
                        {
                            Interop.Memcpy(surfaceBufferPtr, srcPtr, this.yStride);
                            surfaceBufferPtr += rect.Pitch;
                            srcPtr += this.yStride;
                        }
                    }
                    #endregion
                    break;
            }

            this.inputSurface.UnlockRectangle();
        }
【热门文章】
【热门文章】