ios - YUV-NV12 video buffer的y平面和uv平面的延续
<p><p>我有一个 <code>(CMSampleBufferRef)imageBuffer</code>,它是 yuv_nv12(4:2:0) 的类型。</p>
<p>现在我运行下面的代码,发现结果很困惑。</p>
<pre><code>UInt8 *baseSRC = (UInt8 *)CVPixelBufferGetBaseAddress(imageBuffer);
UInt8 *yBaseSRC = (UInt8 *)CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 0);
UInt8 *uvBaseSRC = (UInt8 *)CVPixelBufferGetBaseAddressOfPlane(imageBuffer, 1);
int width = (int)CVPixelBufferGetWidthOfPlane(imageBuffer, 0); //width = 480;
int height = (int)CVPixelBufferGetHeightOfPlane(imageBuffer, 0);//height = 360;
int y_base = yBaseSRC - baseSRC; //y_base = 64;
int uv_y = uvBaseSRC-yBaseSRC; //uv_y = 176640;
int delta = uv_y - width*height; //delta = 3840;
</code></pre>
<p>我对这个结果有几个问题。</p>
<p>1:为什么 <code>baseSRC</code> 不等于 <code>yBaseSRC</code>?</p>
<p>2:为什么<code>yBaseSRC+width*height</code>不等于<code>uvBaseSRC</code>?理论上,y平面数据后面跟着uv平面数据,没有任何中断,对吧?现在它被大小为 3840 字节的东西打断了,我不明白。</p>
<p>3:我尝试使用以下代码将此示例像素转换为 cvmat,在大多数 iOS 设备上,这可以正常工作,但不适用于 iPhone 4s。在 iPhone 4s 上,
转换后,像素缓冲区的旁边有一些绿线。</p>
<pre><code>Mat nv12Mat(height*1.5,width,CV_8UC1,(unsigned char *)yBaseSRC);
Mat rgbMat;
cvtColor(nv12Mat, rgbMat, CV_YUV2RGB_NV12);
</code></pre>
<p>现在 rgbMat 看起来像这样:</p>
<p> <a href="/image/wFmcs.png" rel="noreferrer noopener nofollow"><img src="/image/wFmcs.png" alt="enter image description here"/></a> </p></p>
<br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
<p><p>终于找到了解决办法,基本上解决办法就是分配一 block 新的内存,把y平面数据和uv平面数据拼接起来,然后转成cvmat就好了。</p>
<p>这里是代码片段:</p>
<pre><code>UInt8 *newBase = (UInt8 *)malloc(landscapeWidth*landscapeHeight*1.5);
memcpy(newBase, yBaseSRC, landscapeWidth*landscapeHeight);
memcpy(newBase+landscapeWidth*landscapeHeight, uvBaseSRC, landscapeWidth*landscapeHeight*0.5);
Mat nv12Mat(landscapeHeight*1.5,landscapeWidth,CV_8UC1,(unsigned char *)newBase);
</code></pre></p>
<p style="font-size: 20px;">关于ios - YUV-NV12 video buffer的y平面和uv平面的延续,我们在Stack Overflow上找到一个类似的问题:
<a href="https://stackoverflow.com/questions/36145599/" rel="noreferrer noopener nofollow" style="color: red;">
https://stackoverflow.com/questions/36145599/
</a>
</p>
页:
[1]