前面文章有人問(wèn)我解碼出來(lái)的264如何播放,一般用
OPENGLES
去渲染
- 這里只給關(guān)鍵部分代碼, 從最近做的項(xiàng)目里面摘出來(lái)的。
這部分內(nèi)容推薦看一本書(shū)《音視頻進(jìn)階開(kāi)發(fā)指南Android和iOS》
c++
這里我定義了一個(gè)snode結(jié)構(gòu)體來(lái)存儲(chǔ)YUV數(shù)據(jù)蝎宇,這個(gè)自己也可以寫(xiě);
還有一個(gè)經(jīng)常被忽略的問(wèn)題:記住處理
內(nèi)存泄露
的問(wèn)題脉执,如果常年不寫(xiě)c++
的話(huà)可能會(huì)忽視掉這個(gè)問(wèn)題踢代。 用PS
檢測(cè)內(nèi)存泄露
情況盲憎,跑上個(gè)十來(lái)八個(gè)小時(shí)的看下內(nèi)存變換情況; 但是如果你常年寫(xiě)java
,php
,python
且操作系統(tǒng)
課本不扎實(shí)的話(huà)就得回頭好好補(bǔ)補(bǔ)了,代碼風(fēng)騷的背后需要理論功底去突破瓶頸胳挎,不需要背名詞
饼疙,但要記原理
。
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
}