前面文章有人问我解码出来的264如何播放,一般用
OPENGLES
去渲染
- 这里只给关键部分代码, 从最近做的项目里面摘出来的。
这部分内容推荐看一本书《音视频进阶开发指南Android和iOS》
c++
这里我定义了一个snode结构体来存储YUV数据,这个自己也可以写;
还有一个经常被忽略的问题:记住处理
内存泄露
的问题,如果常年不写c++
的话可能会忽视掉这个问题。 用PS
检测内存泄露
情况,跑上个十来八个小时的看下内存变换情况; 但是如果你常年写java
,php
,python
且操作系统
课本不扎实的话就得回头好好补补了,代码风骚的背后需要理论功底去突破瓶颈,不需要背名词
,但要记原理
。
if (open_ret1 >= 0 ) {
avcodec_decode_video2(rtspVideoCodecContext, v_frame, &got_picture, read_packet);
printf("1\n");
if (got_picture) {
// Init And Define YUV INFO
/*
snode->luma = new unsigned char[width*height];
snode->chromaB = new unsigned char[width*height/4];
snode->chromaR = new unsigned char[width*height/4];
*/
printf("alloc ok\n");
unsigned int height = rtspVideoCodecContext->height;
unsigned int width = rtspVideoCodecContext->width;
printf("decode video ok:%d,%d\n",width,height);
if (snode->luma != NULL) delete []snode->luma;
if (snode->chromaB != NULL) delete []snode->chromaB;
if (snode->chromaR != NULL) delete []snode->chromaR;
snode->luma = NULL;
snode->chromaB = NULL;
snode->chromaR = NULL;
snode->luma = new char[width*height];
snode->chromaB = new char[width*height/4];
snode->chromaR = new char[width*height/4];
int i;
for (i = 0; i<height; i++) {
memcpy(snode->luma + width*i, v_frame->data[0] + i * v_frame->linesize[0], width);
//memcpy(snode->luma + a, v_frame->data[0] + i * v_frame->linesize[0], width);
}
for (i = 0; i<height / 2; i++) {
memcpy(snode->chromaB + width/2*i, v_frame->data[1] + i * v_frame->linesize[1], width / 2);
//memcpy(snode->chromaB + a, v_frame->data[1] + i * v_frame->linesize[1], width / 2);
}
for (i = 0; i<height / 2; i++) {
memcpy(snode->chromaR + width/2*i, v_frame->data[2] + i * v_frame->linesize[2], width / 2);
//memcpy(snode->chromaR + a, v_frame->data[2] + i * v_frame->linesize[2], width / 2);
}
snode->luma_length = width*height;
snode->chromaB_length = (width*height)/4;
snode->chromaR_length = (width*height)/4;
snode->pts = read_packet->pts;
snode->width = width;
snode->height = height;
printf("decode video ok:%d,%d\n%d,%d,%d\n",width,height,snode->luma_length,snode->chromaB_length,snode->chromaR_length);
printf("set data ok\n");
printf("===>%d\n",snode->pts);
//exit(0);
} // if got_pic
}