From 65eb475b323b3b9c28761b4e3009ab3d269963e1 Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期四, 22 十月 2020 11:13:50 +0800
Subject: [PATCH] h264 cuvid av bsf and rec

---
 csrc/ffmpeg/format/FormatIn.cpp  |   56 ------------------
 csrc/ffmpeg/format/FormatOut.hpp |    3 +
 csrc/ffmpeg/format/FormatOut.cpp |   63 ++++++++++++++++++++
 csrc/ffmpeg/format/FormatIn.hpp  |    4 -
 4 files changed, 68 insertions(+), 58 deletions(-)

diff --git a/csrc/ffmpeg/format/FormatIn.cpp b/csrc/ffmpeg/format/FormatIn.cpp
index e69a1bc..89a28fa 100644
--- a/csrc/ffmpeg/format/FormatIn.cpp
+++ b/csrc/ffmpeg/format/FormatIn.cpp
@@ -13,7 +13,6 @@
 #include <libavutil/avassert.h>
 #include <libavutil/imgutils.h>
 #include <libswscale/swscale.h>
-#include <libavutil/intreadwrite.h>
 }
 
 #include "../log/log.hpp"
@@ -38,8 +37,6 @@
 	,read_io_buff_size_(32768)
 	,handle_gb28181(NULL)
 	,fps_(25.0)
-    ,bsf_h264(NULL)
-    ,bsf_hevc(NULL)
 	{}
 
 	FormatIn::FormatIn(const VideoProp &prop, bool hw/*=true*/)
@@ -54,8 +51,6 @@
 	,read_io_buff_size_(32768)
 	,handle_gb28181(NULL)
 	,fps_(25.0)
-    ,bsf_h264(NULL)
-    ,bsf_hevc(NULL)
 	{
 		prop_ = new VideoProp;
 		*prop_ = prop;
@@ -87,8 +82,6 @@
 			io_ctx_ = NULL;
 		}
 
-        if (bsf_h264) av_bsf_free(&bsf_h264);
-        if (bsf_hevc) av_bsf_free(&bsf_hevc);
 	}
 
 ////////////////////////////////////////////////////////////////////////
@@ -190,18 +183,7 @@
             	}
 				logIt("in stream video fps %f, time_base: %d : %d, size: %dx%d", fps_, in->time_base.num, in->time_base.den, in->codecpar->width, in->codecpar->height);
 
-				if (IsHEVC()){
 
-				}else if (isAVC1()) {
-					const AVBitStreamFilter *f = av_bsf_get_by_name("h264_mp4toannexb");
-                    if (f){
-                        if (av_bsf_alloc(f, &bsf_h264) >= 0){
-                            if (avcodec_parameters_copy(bsf_h264->par_in, in->codecpar) >= 0){
-                                if (av_bsf_init(bsf_h264) < 0) bsf_h264 = NULL;
-                            }
-                        }
-                    }
-                }
 			}
 			if (type == AVMEDIA_TYPE_AUDIO){
 				auto in = ctx_->streams[i];
@@ -220,7 +202,7 @@
 		return ctx_->streams[vs_idx_]->codecpar->codec_id == AV_CODEC_ID_HEVC;
 	}
 
-	const bool FormatIn::isAVC1()const{
+	const bool FormatIn::IsAVC1()const{
 		if (IsHEVC()) return false;
 
 		char p[100] = {0};
@@ -377,40 +359,6 @@
 	int FormatIn::readPacket(AVPacket *pkt_out){
 
 		auto flag = av_read_frame(ctx_, pkt_out);
-		if (flag < 0) return flag;
-
-		AVBSFContext *bsf = NULL;
-
-		if (IsHEVC()){
-			if (pkt_out->size >= 5 &&
-				AV_RB32(pkt_out->data) != 0x0000001 &&
-                AV_RB24(pkt_out->data) != 0x000001 &&
-                !bsf_hevc){
-
-				const AVBitStreamFilter *f = av_bsf_get_by_name("hevc_mp4toannexb");
-                if (f){
-                	if (av_bsf_alloc(f, &bsf_hevc) >= 0){
-                	    if (avcodec_parameters_copy(bsf_hevc->par_in, ctx_->streams[vs_idx_]->codecpar) >= 0){
-                	        if (av_bsf_init(bsf_hevc) < 0) bsf_hevc = NULL;
-                	    }
-                	}
-                }
-            }
-
-            bsf = bsf_hevc;
-
-		}else {
-			bsf = bsf_h264;
-		}
-		
-		if (bsf){
-			if (av_bsf_send_packet(bsf, pkt_out) < 0){
-                logIt("bsf_%s send packet failed", IsHEVC() ? "hevc" : "h264");
-                return -1;
-            }
-            return av_bsf_receive_packet(bsf, pkt_out);
-		}
-
 		return flag;
 	}
 
@@ -421,7 +369,7 @@
 		av_packet_rescale_ts(pkt, in->time_base, in->codec->time_base);
         int ret = avcodec_send_packet(dec_ctx_, pkt);
         if(ret < 0){
-        	logIt("avcodec_send_packet error : %s", getAVErrorDesc(ret).c_str());
+        	logIt("pkt size %d avcodec_send_packet error : %s", pkt->size, getAVErrorDesc(ret).c_str());
         	return -1;
         }
         while (ret >= 0) {
diff --git a/csrc/ffmpeg/format/FormatIn.hpp b/csrc/ffmpeg/format/FormatIn.hpp
index b441c0b..aebdbf8 100644
--- a/csrc/ffmpeg/format/FormatIn.hpp
+++ b/csrc/ffmpeg/format/FormatIn.hpp
@@ -13,7 +13,6 @@
 struct AVFrame;
 struct AVCodec;
 struct AVIOContext;
-struct AVBSFContext;
 
 typedef int(* read_packet)(void *opaque,uint8_t *buf, int buf_size);
 
@@ -52,14 +51,13 @@
 		AVFormatContext *getFromatContext(){return ctx_;}
 		const double getFPS()const{return fps_;}
 		const bool IsHEVC()const;
-		const bool isAVC1()const;
+		const bool IsAVC1()const;
 	private:
 	 	AVFormatContext 	*ctx_;
 	 	AVCodecContext 		*dec_ctx_;
 	 	int 				vs_idx_;
 		int 				as_idx_;
 
-        AVBSFContext   		*bsf_h264, *bsf_hevc;
 
 		VideoProp 			*prop_;
 	 	bool 				hw_accl_;
diff --git a/csrc/ffmpeg/format/FormatOut.cpp b/csrc/ffmpeg/format/FormatOut.cpp
index 4d6ce60..6c91db6 100644
--- a/csrc/ffmpeg/format/FormatOut.cpp
+++ b/csrc/ffmpeg/format/FormatOut.cpp
@@ -10,6 +10,7 @@
 #include <libavformat/avformat.h>
 #include <libavcodec/avcodec.h>
 #include <libavutil/opt.h>
+#include <libavutil/intreadwrite.h>
 }
 
 #include "../log/log.hpp"
@@ -33,12 +34,16 @@
     ,format_name_("mp4")
     ,in_v_stream_(NULL)
     ,in_a_stream_(NULL)
+    ,bsf_h264(NULL)
+    ,bsf_hevc(NULL)
 	{}
 
 	FormatOut::~FormatOut()
 	{
 		clear();
 
+        if (bsf_h264) av_bsf_free(&bsf_h264);
+        if (bsf_hevc) av_bsf_free(&bsf_hevc);
 	}
 
     void FormatOut::clear(){
@@ -341,7 +346,26 @@
         }
 
         flag = openResource(filename, 2);
-        
+
+// h264 mp4toannexb
+        if (v && v->codecpar->codec_id == AV_CODEC_ID_H264) {
+            if (!bsf_h264){
+                char p[100] = {0};
+                char *sub = av_fourcc_make_string(p, v->codecpar->codec_tag);
+                if (strcmp(sub, "avc1") == 0){
+                    const AVBitStreamFilter *f = av_bsf_get_by_name("h264_mp4toannexb");
+                    if (f){
+                        if (av_bsf_alloc(f, &bsf_h264) >= 0){
+                            if (avcodec_parameters_copy(bsf_h264->par_in, v->codecpar) >= 0){
+                                if (av_bsf_init(bsf_h264) < 0) bsf_h264 = NULL;
+                            }
+                        }
+                    }
+                }
+            }
+        }
+// h264 mp4toannexb
+
         if(flag){
             AVDictionary *avdic = NULL;
             char option_key[]="movflags";
@@ -456,6 +480,7 @@
     }
 
     static bool write_frame(AVFormatContext *ctx, AVPacket *pkt, bool interleaved){
+
         int ret = 0;
         if(interleaved){
             ret = av_interleaved_write_frame(ctx, pkt);
@@ -473,6 +498,42 @@
 
     bool FormatOut::writeFrameInternal(AVPacket *pkt, bool interleaved){
 
+        AVBSFContext *bsf = NULL;
+
+        if (in_v_stream_ && in_v_stream_->codecpar->codec_id == AV_CODEC_ID_HEVC) {
+            if (pkt->size >= 5 &&
+                AV_RB32(pkt->data) != 0x0000001 &&
+                AV_RB24(pkt->data) != 0x000001 &&
+                !bsf_hevc){
+
+                const AVBitStreamFilter *f = av_bsf_get_by_name("hevc_mp4toannexb");
+                if (f){
+                    if (av_bsf_alloc(f, &bsf_hevc) >= 0){
+                        if (avcodec_parameters_copy(bsf_hevc->par_in, in_v_stream_->codecpar) >= 0){
+                            if (av_bsf_init(bsf_hevc) < 0) bsf_hevc = NULL;
+                        }
+                    }
+                }
+            }
+
+            bsf = bsf_hevc;
+            logIt("use hevc AVBitStreamFilter");
+        }else {
+            bsf = bsf_h264;
+            logIt("use h264 AVBitStreamFilter");
+        }
+
+        if (bsf){
+            if (av_bsf_send_packet(bsf, pkt) < 0){
+                logIt("bsf send packet failed");
+                return true;
+            }
+            if (av_bsf_receive_packet(bsf, pkt) < 0){
+                logIt("bsf recv packet failed");
+                return true;
+            }
+        }
+
         return write_frame(ctx_, pkt, interleaved);
     }
 
diff --git a/csrc/ffmpeg/format/FormatOut.hpp b/csrc/ffmpeg/format/FormatOut.hpp
index 0d746fc..2a7e280 100644
--- a/csrc/ffmpeg/format/FormatOut.hpp
+++ b/csrc/ffmpeg/format/FormatOut.hpp
@@ -11,6 +11,7 @@
 struct AVFrame;
 struct AVPacket;
 struct AVDictionary;
+struct AVBSFContext;
 
 namespace ffwrapper{
 	class VideoProp;
@@ -65,6 +66,8 @@
 		int 					a_idx_;
 		AVCodecContext 			*enc_ctx_;
 
+        AVBSFContext   			*bsf_h264, *bsf_hevc;
+
 		int64_t				 	sync_opts_;
 
 		bool 					record_;

--
Gitblit v1.8.0