From 3f23278d7911847d825b61e0cd2ad87425d91deb Mon Sep 17 00:00:00 2001
From: pans <pans@454eff88-639b-444f-9e54-f578c98de674>
Date: 星期五, 30 十二月 2016 13:43:16 +0800
Subject: [PATCH] 

---
 RtspFace/PL_H264Decoder.cpp |   62 ++++++++++++++++++++++++++----
 1 files changed, 53 insertions(+), 9 deletions(-)

diff --git a/RtspFace/PL_H264Decoder.cpp b/RtspFace/PL_H264Decoder.cpp
index 879fa6f..3f61cce 100644
--- a/RtspFace/PL_H264Decoder.cpp
+++ b/RtspFace/PL_H264Decoder.cpp
@@ -1,4 +1,6 @@
 #include "PL_H264Decoder.h"
+#include "MaterialBuffer.h"
+#include "logger.h"
 
 #include <H264VideoRTPSource.hh> // for SPropRecord
 #include <libbase64.h>
@@ -20,12 +22,14 @@
 
 	AVCodecContext* pAVCodecContext;
 	AVFrame* pAVFrame;//#todo delete
+	MB_Frame lastFrame;
 	
 	H264Decoder_Internal() : 
 		//buffSize(0), buffSizeMax(sizeof(buffer)), 
 		fmtp_set_to_context(false), 
 		payError(true), 
-		pAVCodecContext(nullptr), pAVFrame(nullptr)
+		pAVCodecContext(nullptr), pAVFrame(nullptr), 
+		lastFrame()
 	{
 	}
 	
@@ -38,6 +42,9 @@
 		//buffSize = 0;
 		fmtp_set_to_context = false;
 		payError = true;
+		
+		MB_Frame _lastFrame;
+		lastFrame = _lastFrame;
 	}
 };
 
@@ -115,7 +122,7 @@
 
 	if (!avCodec)   
 	{  
-		printf("codec not found!\n");  
+		LOG(WARN) << "codec not found!";  
 		return false;  
 	}  
 
@@ -156,16 +163,18 @@
 	return true;
 }
 
-bool decodeH264(H264Decoder_Internal* in, uint8_t* buffer, size_t buffSize)  
+bool decodeH264(H264Decoder_Internal* in, uint8_t* buffer, size_t buffSize, timeval pts)  
 {
 	AVPacket packet = {0};
 	int gotPicture = buffSize; // frameFinished
 
 	if (av_packet_from_data(&packet, buffer, buffSize) != 0)
 	{  
-		printf("av_packet_from_data error\n");
+		LOG(WARN) << "av_packet_from_data error";
 		return false;
-	}  
+	}
+	
+	packet.pts = packet.dts = (pts.tv_sec * 1000 * 1000 + pts.tv_usec) / 90000;
 	
 	// decode
 	avcodec_decode_video2(in->pAVCodecContext, in->pAVFrame, &gotPicture, &packet);
@@ -176,7 +185,7 @@
 	}
 	else
 	{
-		printf("incomplete frame\n");
+		LOG(WARN) << "incomplete frame";
 		return false;
 	}
 }
@@ -206,12 +215,46 @@
 
 		bool ret = initH264DecoderEnv(in, sps.sPropBytes, sps.sPropLength, pps.sPropBytes, pps.sPropLength);
 		if (!ret)
+		{
+			LOG(ERROR) << "PL_H264Decoder::pay initH264DecoderEnv error";
 			return false; // #todo log
+		}
 		else
 			in->fmtp_set_to_context = true;
 	}
 	
-	bool ret = decodeH264(in, pm.buffer, pm.buffSize);
+	if (pm.buffer == nullptr)
+		return false;
+	
+	bool ret = false;
+	if (pm.type == PipeMaterial::PMT_BYTES)
+	{
+		if (pm.buffSize <= 0)
+			return false;
+
+		timeval pts = {0};
+		ret = decodeH264(in, (uint8_t*)pm.buffer, pm.buffSize, pts);
+	}
+	else if (pm.type == PipeMaterial::PMT_FRAME)
+	{
+		MB_Frame* frame = (MB_Frame*)pm.buffer;
+		
+		if (frame->buffSize <= 0)
+			return false;
+		
+		ret = decodeH264(in, (uint8_t*)frame->buffer, frame->buffSize, frame->pts);
+		if (ret)
+		{
+			in->lastFrame.type = MB_Frame::MBFT_PTR_AVFRAME;
+			in->lastFrame.buffer = (uint8_t*)(in->pAVFrame);
+			in->lastFrame.buffSize = sizeof(in->pAVFrame);
+			in->lastFrame.width = in->pAVFrame->width;
+			in->lastFrame.height = in->pAVFrame->height;
+			//in->lastFrame.pts = frame->pts;//#todo
+			gettimeofday(&(in->lastFrame.pts),NULL);
+		}
+	}
+	
 	in->payError = !ret;
 	return ret;
 }
@@ -222,8 +265,9 @@
 
 	if (!in->payError)
 	{
-		pm.buffer = (uint8_t*)in->pAVFrame;//in->buffer;
-		pm.buffSize = sizeof(uint8_t*);//in->buffSize;
+		pm.type = PipeMaterial::PMT_FRAME;
+		pm.buffer = &(in->lastFrame);
+		pm.buffSize = 0;
 	}
 	pm.former = this;
 	return !in->payError;

--
Gitblit v1.8.0