From be9c1d1f659b0ff31f656424c478e83a4f7c53b5 Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期五, 20 九月 2019 11:44:19 +0800
Subject: [PATCH] update ffmpeg

---
 csrc/ffmpeg/format/FormatIn.cpp |  113 +++++++++++++++++++++++++++++++++++---------------------
 1 files changed, 70 insertions(+), 43 deletions(-)

diff --git a/csrc/ffmpeg/format/FormatIn.cpp b/csrc/ffmpeg/format/FormatIn.cpp
index d61fbc7..3f87d59 100644
--- a/csrc/ffmpeg/format/FormatIn.cpp
+++ b/csrc/ffmpeg/format/FormatIn.cpp
@@ -21,7 +21,6 @@
 #include "../data/FrameData.hpp"
 
 #include "../../common/gpu/info.h"
-#include "PsToEs.hpp"
 
 using namespace logif;
 
@@ -30,10 +29,13 @@
 	:ctx_(NULL)
 	,dec_ctx_(NULL)
 	,vs_idx_(-1)
+	,as_idx_(-1)
 	,hw_accl_(hw)
 	,io_ctx_(NULL)
 	,read_io_buff_(NULL)
 	,read_io_buff_size_(32768)
+	,handle_gb28181(NULL)
+	,fps_(25.0)
 	{}
 
 	FormatIn::~FormatIn()
@@ -54,6 +56,9 @@
 				avcodec_close(dec_ctx_);
 				dec_ctx_ = NULL;
 			}
+		}
+		if (handle_gb28181){
+			delete handle_gb28181;
 		}
 	}
 
@@ -90,19 +95,23 @@
 	int FormatIn::openGb28181(const char *filename, AVDictionary **options){
 
 	    std::string fn = filename;
-		addCamera(fn);
+		//GB28181API gb28181(fn);
+		if (handle_gb28181){
+			delete handle_gb28181;
+		}
+		handle_gb28181 = new GB28181API;
+		handle_gb28181->addCamera(fn);
 
-        int ret = openWithCustomIO(NULL, readData, options);
+        int ret = openWithCustomIO(handle_gb28181, handle_gb28181->readData, options);
         if(ret < 0){
             logIt("do openWithCustomIO failed:%d",ret);
         }
 
         ret = avformat_open_input(&ctx_, "", NULL, options);
-        if(ret < 0){
-            logIt("open %s failed:%s",filename,
-                  getAVErrorDesc(ret).c_str());
-
-        }
+        // if(ret < 0){
+            // logIt("open %s failed:%s",filename,
+            //       getAVErrorDesc(ret).c_str());
+        // }
 
 		return ret;
 	}
@@ -111,11 +120,10 @@
 	int FormatIn::open(const char *filename, AVDictionary **options){
 
 		const int ret = avformat_open_input(&ctx_, filename, NULL, options);
-		if(ret < 0){
-			logIt("open %s failed:%s",filename,
-					getAVErrorDesc(ret).c_str()); 
-
-		}
+		// if(ret < 0){
+		// 	logIt("open %s failed:%s",filename,
+		// 			getAVErrorDesc(ret).c_str()); 
+		// }
 
 		return ret;
 	}
@@ -130,15 +138,25 @@
 			return false;
 		}
 
+		logIt("there are %d stream", ctx_->nb_streams);
+
 		for (int i = 0; i < ctx_->nb_streams; ++i)
 		{
-			switch(ctx_->streams[i]->codecpar->codec_type){
-				case AVMEDIA_TYPE_VIDEO:
-					vs_idx_ = i;
-					break;
+			auto type = ctx_->streams[i]->codecpar->codec_type;
+			if (type == AVMEDIA_TYPE_VIDEO){
+				vs_idx_ = i;
 
-				default:
-					break;
+				auto in = ctx_->streams[i];
+				if(in->r_frame_rate.num >=1 && in->r_frame_rate.den >= 1){
+            	    fps_ = av_q2d(in->r_frame_rate);
+            	}else if(in->avg_frame_rate.num >=1 && in->avg_frame_rate.den >= 1){
+            	    fps_ = av_q2d(in->avg_frame_rate);
+            	}
+				logIt("video stream time base %d : %d", in->time_base.num, in->time_base.den);
+			}
+			if (type == AVMEDIA_TYPE_AUDIO){
+				as_idx_ = i;
+				logIt("audio stream time base %d : %d", ctx_->streams[i]->time_base.num, ctx_->streams[i]->time_base.den);
 			}
 		}
 		return true;
@@ -244,43 +262,52 @@
 		return true;
 	}
 
-	AVStream *FormatIn::getStream(int type){
-		return ctx_->streams[vs_idx_];
+	AVStream *FormatIn::getStream(int type/*=-1*/){
+		if (type == -1){
+			return ctx_->streams[vs_idx_];
+		}
+
+		if (type == ctx_->streams[vs_idx_]->codecpar->codec_type)
+			return ctx_->streams[vs_idx_];
+		if (type == ctx_->streams[as_idx_]->codecpar->codec_type)
+			return ctx_->streams[as_idx_];
+
+		return NULL;
 	}
 
 	AVCodecContext *FormatIn::getCodecContext(int type){
 		return dec_ctx_;
 	}
 	
-	bool FormatIn::readPacket(AVPacket &pkt_out, int stream_index){
-
-		bool founded = false;
-		while (!founded){
-			const int ret = av_read_frame(ctx_, &pkt_out);
-			if(ret < 0){
-				logIt("read frame from %s failed:%s",
-						ctx_->filename,getAVErrorDesc(ret).c_str()); 
-	
-				return false;
-			}
-			if(pkt_out.stream_index == stream_index){
-				founded = true;
-			}else{
-				av_free_packet(&pkt_out);
-				av_init_packet(&pkt_out);
-				pkt_out.data = NULL;
-				pkt_out.size = 0;
-			}
+	bool FormatIn::isVideoPkt(AVPacket &pkt){
+		if (pkt.stream_index == vs_idx_){
+			return true;
 		}
-		pkt_out.stream_index = 0;
+		return false;
+	}
+
+	bool FormatIn::isAudioPkt(AVPacket &pkt){
+		if (pkt.stream_index == as_idx_){
+			return true;
+		}
+		return false;
+	}
+
+	bool FormatIn::readPacket(AVPacket &pkt_out){
+
+		const int ret = av_read_frame(ctx_, &pkt_out);
+		if(ret < 0){
+			return false;
+		}
+		
 		return true;
 
 	}
 
-	bool FormatIn::readPacket(std::shared_ptr<CodedData> &data, int stream_index){
+	bool FormatIn::readPacket(std::shared_ptr<CodedData> &data){
 
 		AVPacket &pkt(data->getAVPacket());
-		return readPacket(pkt, getStream()->index);
+		return readPacket(pkt);
 	}
 
 	int FormatIn::decode(AVFrame* &frame, AVPacket &pkt){

--
Gitblit v1.8.0