Ogre 播放 YUV 视频文件

参考同事等的代码

这里的YUV文件的每一帧是Y,U,V分别单独存储的。

这里逻辑是对的,但代码不太完整


pBuf = new unsigned char[bytesPerFrame];
pBuf2 = new unsigned char[bytesPerFrame];
//fread(pBuf, 1, bytesPerFrame, f);
//yuv420_to_nv12(pBuf, pBuf2, nWidth, nHeight);

tex0 = TextureManager::getSingleton().createManual( "tex0",
            ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
TEX_TYPE_2D, nWidth, nHeight, 0,
Ogre::PF_L8,
            Ogre::TU_DYNAMIC_WRITE_ONLY );


tex1 = TextureManager::getSingleton().createManual( "tex1",
            ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,
TEX_TYPE_2D, nWidth >> 1, nHeight >> 1, 0,
Ogre::PF_BYTE_LA,
            Ogre::TU_DYNAMIC_WRITE_ONLY );


File *f = fopen("E:\\ogre-1.10\\Samples\\Car\\akiyo_qcif.yuv","rb");

bytesPerFrame = ( nWidth * nHeight * 3 ) >> 1;
fseek(f, 0, SEEK_END);
frame_count = (int) ((int)ftell(f)/bytesPerFrame);  
 fseek(f, 0, SEEK_SET);
unsigned char *pBuf = new unsigned char[bytesPerFrame];

Ogre::MaterialPtr material = Ogre::MaterialManager::getSingleton().getByName("YUV_test");
Pass *pass = material->getTechnique(0)->getPass(0);
pass->setDepthCheckEnabled(true);  
pass->setDepthWriteEnabled(true);  
pass->setLightingEnabled(false);  
TextureUnitState* state = pass->createTextureUnitState("tex0"); //会累计,0,纹理坐标集 texture coordinate set
TextureUnitState* state1 = pass->createTextureUnitState("tex1");//会累计,1,纹理坐标集 texture coordinate set
GpuProgramParametersSharedPtr ptrFP = pass->getFragmentProgramParameters();


ptrFP->setNamedConstant("texTyp",1);
ptrFP->setNamedConstant("texture",0);//设置纹理坐标集
ptrFP->setNamedConstant("texture2",1);//设置纹理坐标集
ptrFP->setNamedConstant("gamVal",Ogre::Real(1.0));


ent->setMaterial(material);


每一帧更换纹理,这里pBuff2是用来做实验的,来验证另一种调整UV位置的方法

//设置当前帧的纹理数据,yoga test
void set_cur_frame_tex(int i)
{
if( i == frame_count )
return;
fseek(f, i * bytesPerFrame, SEEK_SET);
fread(pBuf2, 1, bytesPerFrame, f);


yuv420_to_nv12(pBuf, pBuf2, nWidth, nHeight);


// tex0
Ogre::HardwarePixelBufferSharedPtr pixelBuffer = tex0->getBuffer();
 
pixelBuffer->lock( Ogre::HardwareBuffer::HBL_DISCARD );
{
const Ogre::PixelBox &pixelBox = pixelBuffer->getCurrentLock();
 
Ogre::uint8 *dest = static_cast<Ogre::uint8*>( pixelBox.data );
PixelFormat  format = pixelBox.format;




for( size_t j = 0; j < nWidth * nHeight; j++ )
{
dest[j] = pBuf[j];
}
}
pixelBuffer->unlock();


// tex1
//pixelBuffer = tex1->getBuffer();
Ogre::HardwarePixelBufferSharedPtr pixelBuffer1 = tex1->getBuffer();
 
pixelBuffer1->lock( Ogre::HardwareBuffer::HBL_DISCARD );
{
const Ogre::PixelBox &pixelBox = pixelBuffer1->getCurrentLock();
 
Ogre::uint8 *dest = static_cast<Ogre::uint8*>( pixelBox.data );
PixelFormat  format = pixelBox.format;



/* for( size_t j = 0; j < ((nWidth * nHeight)>>1); j++ )
{
dest[j] = pBuf[ nWidth * nHeight + j];
}



for( size_t j = nWidth * nHeight, k = 0; j < nWidth * nHeight + ((nWidth * nHeight)>>2); j++, k++ )//nWidth * nHeight + (bytesPerFrame >> 1); j+=2, k+=2 )
{
dest[(k<<1)] = pBuf2[j];
//dest[k+1] = pBuf[k+1];
//break;//test
}


for( size_t j = nWidth * nHeight + ((nWidth * nHeight)>>2), k = 0; j < nWidth * nHeight + ((nWidth * nHeight)>>1); j++, k++ )//nWidth * nHeight + (bytesPerFrame >> 1); j+=2, k+=2 )
{
dest[(k<<1)+1] = pBuf2[j];
//dest[k+1] = pBuf[k+1];
//break;//test
}*/


//size_t sz0 = nWidth * nHeight;
//size_t sz1 = bytesPerFrame;


size_t pixelsPerFrame = nWidth * nHeight;


for( size_t j = pixelsPerFrame, k = 0; j < pixelsPerFrame + ((pixelsPerFrame)>>2); j++, k++ )//nWidth * nHeight + (bytesPerFrame >> 1); j+=2, k+=2 )
{
dest[(k<<1)] = pBuf2[j];
//dest[k+1] = pBuf[k+1];
//break;//test
}


for( size_t j = pixelsPerFrame + ((pixelsPerFrame)>>2), k = 0; j < pixelsPerFrame + ((pixelsPerFrame)>>1); j++, k++ )//nWidth * nHeight + (bytesPerFrame >> 1); j+=2, k+=2 )
{
dest[(k<<1)+1] = pBuf2[j];
//dest[k+1] = pBuf[k+1];
//break;//test
}


}
pixelBuffer1->unlock();


}


// Ogre glsles 要求一定要有 vert 和 frag shader

frag shader 如下,是把由420 YUV 的单帧文件数据记录到两个 纹理中,来进行计算,生产rgb颜色


precision mediump float;


uniform sampler2D texture;
uniform sampler2D texture2;
uniform float gamVal;
uniform int texTyp;
varying   vec2 g_vVSTexCoord;

void main()
{
if( 0 == texTyp)
{
gl_FragColor = texture2D(texture,g_vVSTexCoord);
}
else if(1 == texTyp)
{
vec4 tmpColor;
vec4 yuv = vec4(1.0);
    vec4 rgb = vec4(1.0);

mat4 conversion = mat4(1.0, 0.0, 1.596, -0.798,
                           1.0, -0.392, -0.813, 0.603,
                           1.0, 2.017, 0.0, -1.009,
                           0, 0, 0, 0);
                           
tmpColor = texture2D(texture,g_vVSTexCoord);
yuv.r = tmpColor.r;
yuv.r = pow(yuv.r, gamVal);
    tmpColor = texture2D(texture2,g_vVSTexCoord);
    yuv.yz = tmpColor.ra;
    rgb = yuv*conversion;
    rgb.a = 1.0;
    gl_FragColor = rgb;
    }
}


OGRE(O-O Graphics Rendering Engine) 使用指南________________________________________________________________1 作者_________________________________________________________________________ 6 序____________________________________________________________________________7 教程的目的_________________________________________________________________ 7 读者要求___________________________________________________________________ 7 教程的由来_________________________________________________________________ 7 OGRE简介___________________________________________________________________ 8 OGRE特点_________________________________________________________________ 8 效率特性__________________________________________________________________8 平台和3D API 支持________________________________________________________ 8 网格Meshes_______________________________________________________________ 8 场景特性__________________________________________________________________9 特效______________________________________________________________________9 其它特性__________________________________________________________________9 OGRE中的模块_____________________________________________________________ 9 OgreMain 模块____________________________________________________________ 10 SDL 平台管理模块________________________________________________________ 11 Win32 平台管理模块_______________________________________________________11 BSP 场景管理_____________________________________________________________11 文件系统插件_____________________________________________________________11 GuiElement 插件__________________________________________________________ 11 OctreeSceneManager 插件___________________________________________________ 11 ParticleFX 插件___________________________________________________________ 12 Direct3D7 渲染系统插件____________________________________________________12 Direct3D8 渲染系统插件____________________________________________________12 SDL 渲染系统插件________________________________________________________ 12 3ds2oof 工具______________________________________________________________12 3Dstudio Max 导出器_______________________________________________________12 位图字体创建工具_________________________________________________________12 Milkshape3D 导出器_______________________________________________________ 12 Python 接口______________________________________________________________ 13 XML 转换器______________________________________________________________13
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值