WebRTC中I420BufferInterface如何取yuv数据

在WebRTC中使用VP8编码I420BufferInterface方式中DataY(),DataU(),DataV(),取出的数据正好是Y,U,V分量,可以直接在OpenGL中渲染,但是改成open264后,由于用的是ffmpeg解码,复用的AVFrame里的数据,涉及到内存对齐,需要按linesize去每行的copy。

所以解码后的数据在I420BufferInterface的通用取法为:

   int nYUVBufsize = 0;
   int nVOffset = 0;
   for (int i = 0; i < m_height; i++) {
    memcpy(m_uBuffer + nYUVBufsize,m_I420Buffer->DataY() + i * m_I420Buffer->StrideY(), m_width);
    nYUVBufsize += m_width;
   }
   for (int i = 0; i < m_height / 2; i++) {
    memcpy(m_uBuffer + nYUVBufsize, m_I420Buffer->DataU() + i * m_I420Buffer->StrideU(), m_width / 2);
    nYUVBufsize += m_width / 2;
    memcpy(m_uBuffer + m_width * m_height * 5 / 4 + nVOffset, m_I420Buffer->DataV() + i * m_I420Buffer->StrideV(), m_width / 2);
    nVOffset += m_width / 2;
    }
    
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在 Android WebRTC ,可以使用 `ByteBuffer` 类来处理 i420 数据。以下是一个将 i420 缓冲区转换为 byte 数组的示例代码: ```java public byte[] i420BufferToByteArray(I420Buffer i420Buffer) { int width = i420Buffer.getWidth(); int height = i420Buffer.getHeight(); int imageSize = width * height * 3 / 2; byte[] imageBytes = new byte[imageSize]; int i = 0; // Y plane (luminance) for (int row = 0; row < height; row++) { for (int col = 0; col < width; col++) { imageBytes[i++] = i420Buffer.getDataY().get(row * i420Buffer.getStrideY() + col); } } // U and V planes (chrominance) for (int row = 0; row < height / 2; row++) { for (int col = 0; col < width / 2; col++) { imageBytes[i++] = i420Buffer.getDataU().get(row * i420Buffer.getStrideU() + col); imageBytes[i++] = i420Buffer.getDataV().get(row * i420Buffer.getStrideV() + col); } } return imageBytes; } ``` 上述代码的 `i420Buffer` 是一个 `I420Buffer` 对象,它包含了 i420 数据的 Y、U 和 V 分量。通过 `getDataY()`、`getDataU()` 和 `getDataV()` 方法可以获到相应分量的 `ByteBuffer` 对象。`getStrideY()`、`getStrideU()` 和 `getStrideV()` 方法分别返回相应分量的跨度。 代码首先遍历 Y 分量的每个像素,并将其保存到 `imageBytes` 数组。然后遍历 U 和 V 分量的每个像素,并将它们交替保存到 `imageBytes` 数组。 请注意,上述代码假设输入的 i420Buffer 是按照标准的 i420 格式排列的。如果你的数据不符合标准格式,可能需要进行适当的调整。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值