From 7f63f973141f973faff7896087697f5150332864 Mon Sep 17 00:00:00 2001
From: pans <pans@454eff88-639b-444f-9e54-f578c98de674>
Date: 星期五, 30 十二月 2016 12:32:02 +0800
Subject: [PATCH] 

---
 RtspFace/PL_H264Encoder.cpp |   41 +++++++++++++++++++++++++++++++----------
 1 files changed, 31 insertions(+), 10 deletions(-)

diff --git a/RtspFace/PL_H264Encoder.cpp b/RtspFace/PL_H264Encoder.cpp
index 5547256..7068b79 100644
--- a/RtspFace/PL_H264Encoder.cpp
+++ b/RtspFace/PL_H264Encoder.cpp
@@ -5,8 +5,9 @@
 {
 	#include <libavcodec/avcodec.h>
 	#include <libavutil/frame.h>
-	#include <libavformat/avformat.h>
 	#include <libavutil/imgutils.h>
+	#include <libavutil/opt.h>
+	#include <libavformat/avformat.h>
 	
 	#include <libyuv.h>
 }
@@ -106,10 +107,11 @@
     in->pAVCodecContext->time_base.den=25;
     in->pAVCodecContext->gop_size = 25;
     in->pAVCodecContext->max_b_frames = 0;
+	//in->pAVCodecContext->profile = FF_PROFILE_H264_MAIN;
     in->pAVCodecContext->pix_fmt = AV_PIX_FMT_YUV420P;
 	
-	//av_opt_set(c->priv_data, "preset", "superfast", 0);  
-	//av_opt_set(c->priv_data, "tune", "zerolatency", 0);
+	av_opt_set(in->pAVCodecContext->priv_data, "preset", "superfast", 0);  
+	//av_opt_set(in->pAVCodecContext->priv_data, "tune", "zerolatency", 0);
 
 	if(avcodec_open2(in->pAVCodecContext, avCodec, NULL) >= 0)
 	{
@@ -261,6 +263,17 @@
 	return !(in->payError);
 }
 
+bool encodeH264(H264Encoder_Internal* in, uint8_t* buffer, timeval pts)
+{
+	AVFrame avFrame;
+	avFrame.width = 1920;//#todo
+	avFrame.height = 1080;
+	avFrame.data[0] = buffer;
+	avFrame.data[1] = buffer + 1920*1080;
+	avFrame.data[2] = buffer + 1920*1080 + 1920*1080/4;
+	return encodeH264(in, &avFrame, pts);
+}
+
 bool PL_H264Encoder::pay(const PipeMaterial& pm)
 {
 	H264Encoder_Internal* in = (H264Encoder_Internal*)internal;
@@ -289,13 +302,20 @@
 		return false;
 	
 	MB_Frame* frame = (MB_Frame*)pm.buffer;
-	if (frame->type != MB_Frame::MBFT_PTR_AVFRAME)
+	
+	bool ret;
+	
+	if (frame->type == MB_Frame::MBFT_PTR_AVFRAME)
+		ret = encodeH264(in, (AVFrame*)(frame->buffer), frame->pts);
+	else if (frame->type == MB_Frame::MBFT_YUV420)
+		ret = encodeH264(in, (uint8_t*)(frame->buffer), frame->pts);
+	else
 	{
-		printf("PL_H264Encoder::pay only support MBFT_PTR_AVFRAME\n");
+		printf("PL_H264Encoder::pay only support MBFT_PTR_AVFRAME / MBFT_YUV420\n");
+		in->payError = true;
 		return false;
 	}
-	
-	bool ret = encodeH264(in, (AVFrame*)(frame->buffer), frame->pts);
+
 	in->payError = !ret;
 	
 	if (ret)
@@ -303,6 +323,8 @@
 		in->lastFrame.type = MB_Frame::MBFT_H264_NALU;
 		in->lastFrame.buffer = in->buffer;
 		in->lastFrame.buffSize = in->buffSize;
+		in->lastFrame.width = frame->width;
+		in->lastFrame.height = frame->height;
 		in->lastFrame.pts = frame->pts;
 	}
 	
@@ -316,9 +338,8 @@
 	if (!in->payError)
 	{
 		pm.type = PipeMaterial::PMT_FRAME;
-		pm.buffer = (uint8_t*)(&(in->lastFrame));
-		pm.buffSize = sizeof(in->lastFrame);
-		pm.former = this;
+		pm.buffer = &(in->lastFrame);
+		pm.buffSize = 0;
 	}
 	pm.former = this;
 	return !in->payError;

--
Gitblit v1.8.0