From 64fed650d429fdbdfa4011956a2c057dd0348914 Mon Sep 17 00:00:00 2001
From: zhangmeng <775834166@qq.com>
Date: 星期三, 13 十一月 2019 14:21:59 +0800
Subject: [PATCH] bug fix

---
 csrc/wrapper.cpp                |   21 ++++++++--
 csrc/wrapper.hpp                |    3 +
 cffmpeg.h                       |    2 
 csrc/ffmpeg/bridge/cvbridge.cpp |   12 +++--
 goenc.go                        |   21 ++++++++++
 goconv.go                       |    2 
 libcffmpeg.c                    |    6 +-
 csrc/cffmpeg.cpp                |    4 +-
 libcffmpeg.h                    |    4 +-
 9 files changed, 54 insertions(+), 21 deletions(-)

diff --git a/cffmpeg.h b/cffmpeg.h
index 0a7ebd1..258416d 100644
--- a/cffmpeg.h
+++ b/cffmpeg.h
@@ -32,7 +32,7 @@
 void* c_ffmpeg_decode(const char *file, const int gb, int *wid, int *hei);
 
 // pic encoder
-void *c_ffmpeg_create_encoder(const int w, const int h, const int fps, const int br, const int scale_flag, const int gi);
+void *c_ffmpeg_create_encoder(const int w, const int h, const int fps, const int br, const int scale_flag, const int gi, const int fmt, const char *file);
 void c_ffmpeg_destroy_encoder(void *h);
 int c_ffmpeg_encode(void *hdl, uint8_t *in, const int w, const int h, uint8_t **out, int *size, int *key);
 
diff --git a/csrc/cffmpeg.cpp b/csrc/cffmpeg.cpp
index 4cf92da..176e1fb 100644
--- a/csrc/cffmpeg.cpp
+++ b/csrc/cffmpeg.cpp
@@ -115,8 +115,8 @@
 }
 
 // pic encoder
-void *c_ffmpeg_create_encoder(const int w, const int h, const int fps, const int br, const int scale_flag, const int gi){
-    return CreateEncoder(w, h, fps, br, scale_flag, gi);
+void *c_ffmpeg_create_encoder(const int w, const int h, const int fps, const int br, const int scale_flag, const int gi, const int fmt, const char *file){
+    return CreateEncoder(w, h, fps, br, scale_flag, gi, fmt, file);
 }
 
 void c_ffmpeg_destroy_encoder(void *h){
diff --git a/csrc/ffmpeg/bridge/cvbridge.cpp b/csrc/ffmpeg/bridge/cvbridge.cpp
index 21d09a1..0749f12 100644
--- a/csrc/ffmpeg/bridge/cvbridge.cpp
+++ b/csrc/ffmpeg/bridge/cvbridge.cpp
@@ -62,11 +62,13 @@
 			return NULL;
 		}
 
-		if(!scale_->scaleFrame(in, pic_->getAVFrame())){
-			return NULL;
-		}
-
-        return av_frame_clone(pic_->getAVFrame());
+		uint8_t *out = convert2Data(in);
+        AVFrame *frm = NULL;
+        if (out){
+            frm = fillFrame(out, scale_->dstW_, scale_->dstH_, scale_->dstFmt_);
+        }
+        free(out);
+        return frm;
     }
 
 /////////////////////////////////////////////////////////////////
diff --git a/csrc/wrapper.cpp b/csrc/wrapper.cpp
index 86662a1..6e067d9 100644
--- a/csrc/wrapper.cpp
+++ b/csrc/wrapper.cpp
@@ -370,10 +370,14 @@
         int br;
         int gi;
         int flag;
+        int fmt;
         cvbridge *bridge;
     } PicEncoder;
 
-    void *CreateEncoder(const int w, const int h, const int fps, const int br, const int scale_flag, const int gi){
+    void *CreateEncoder(const int w, const int h, const int fps, const int br, 
+                        const int scale_flag, const int gi, const int fmt, const char *file){
+
+        if (fmt < 0) return NULL;
 
         PicEncoder *e = (PicEncoder*)malloc(sizeof(PicEncoder));
         e->enc = NULL;
@@ -383,6 +387,7 @@
         e->br = br;
         e->gi = gi;
         e->flag = scale_flag;
+        e->fmt = fmt;
         e->bridge = NULL;
 
         VideoProp prop_;
@@ -392,7 +397,10 @@
         prop_.bit_rate_ = br;
         gi < 0 ? prop_.gpu_acc_ = false : prop_.gpu_acc_ = true;
 
-		FormatOut *enc = new FormatOut(prop_, "./88.mp4");
+        std::string filename("./88.mp4");
+        if (file) filename = file;
+
+		FormatOut *enc = new FormatOut(prop_, filename.c_str());
         e->enc = enc;
 
         return e;
@@ -415,14 +423,17 @@
         PicEncoder *e = (PicEncoder*)hdl;
         auto ctx = e->enc->getCodecContext();
 
-        AVPixelFormat pix_fmt = AV_PIX_FMT_BGR24;
+        // AVPixelFormat pix_fmt = AV_PIX_FMT_BGR24;
         if (e->bridge == NULL){
             e->bridge = new cvbridge(
-                    w, h, AV_PIX_FMT_BGR24,
+                    w, h, e->fmt,
                     e->w, e->h, ctx->pix_fmt, e->flag);
         }
 
-        AVFrame *frame = cvbridge::fillFrame(in, w, h, pix_fmt);
+        AVFrame *bgr_frame = cvbridge::fillFrame(in, w, h, e->fmt);
+        AVFrame *frame = e->bridge->convert2Frame(bgr_frame);
+        av_frame_free(&bgr_frame);
+
         AVPacket *pkt = av_packet_alloc();
 	
         auto flag = e->enc->encode(pkt, frame);
diff --git a/csrc/wrapper.hpp b/csrc/wrapper.hpp
index 73da823..99039b6 100644
--- a/csrc/wrapper.hpp
+++ b/csrc/wrapper.hpp
@@ -81,7 +81,8 @@
 
     uint8_t* Decode(const char *file, const int gb, int *w, int *h);
 
-    void *CreateEncoder(const int w, const int h, const int fps, const int br, const int scale_flag, const int gi);
+    void *CreateEncoder(const int w, const int h, const int fps, const int br, 
+                        const int scale_flag, const int gi, const int fmt, const char *file);
     void DestroyEncoder(void *h);
     int Encode(void *hdl, uint8_t *in, const int w, const int h, uint8_t **out, int *size, int *key);
 
diff --git a/goconv.go b/goconv.go
index eb4aac0..311bdd4 100644
--- a/goconv.go
+++ b/goconv.go
@@ -34,7 +34,7 @@
 	ScaleSpline = 0x400
 )
 
-// SrcFormat format
+// SrcFormat format NV
 const SrcFormat = 23
 
 // DstFormat format
diff --git a/goenc.go b/goenc.go
index d7a6b43..15a24ef 100644
--- a/goenc.go
+++ b/goenc.go
@@ -21,7 +21,7 @@
 	}
 
 	return &GoEncoder{
-		enc: C.wrap_fn_create_encoder(unsafe.Pointer(libcffmpeg), C.int(w), C.int(h), C.int(fps), C.int(br), C.int(sFlag), C.int(gi)),
+		enc: C.wrap_fn_create_encoder(unsafe.Pointer(libcffmpeg), C.int(w), C.int(h), C.int(fps), C.int(br), C.int(sFlag), C.int(gi), C.int(DstFormat), nil),
 	}
 }
 
@@ -53,3 +53,22 @@
 	}
 	return nil, 0, false
 }
+
+// NewJpegEncoder encoder
+func NewJpegEncoder(w, h, fps, br, sFlag, gi, fmt int, file string) *GoEncoder {
+	if w <= 0 || h <= 0 {
+		return nil
+	}
+
+	cfile := C.CString(file)
+	defer C.free(unsafe.Pointer(cfile))
+	return &GoEncoder{
+		enc: C.wrap_fn_create_encoder(unsafe.Pointer(libcffmpeg), C.int(w), C.int(h), C.int(fps), C.int(br), C.int(sFlag), C.int(gi), C.int(fmt), cfile),
+	}
+}
+
+// EncodeJpeg bgr->jpg
+func (e *GoEncoder) EncodeJpeg(bgr []byte, w, h int) ([]byte, error) {
+
+	return nil, nil
+}
diff --git a/libcffmpeg.c b/libcffmpeg.c
index 7c30caf..7faf09a 100644
--- a/libcffmpeg.c
+++ b/libcffmpeg.c
@@ -153,12 +153,12 @@
 }
 
 // for encoder
-cencoder wrap_fn_create_encoder(void *lib, const int w, const int h, const int fps, const int br, const int scale_flag, const int gi){
+cencoder wrap_fn_create_encoder(void *lib, const int w, const int h, const int fps, const int br, const int scale_flag, const int gi, const int fmt, const char *file){
     if (!fn_create_encoder){
         fn_create_encoder = (lib_cffmpeg_create_encoder)dlsym(lib, "c_ffmpeg_create_encoder");
         release_if_err(fn_create_encoder, lib);
     }
-    return fn_create_encoder(w, h, fps, br, scale_flag, gi);
+    return fn_create_encoder(w, h, fps, br, scale_flag, gi, fmt, file);
 }
 
 void wrap_fn_destroy_encoder(void *lib, const cencoder h){
@@ -177,7 +177,7 @@
     
     uint8_t *out = NULL;
     const int flag = fn_encode(hdl, (uint8_t*)in, w, h, &out, out_size, key);
-    if (flag > 0 && out != NULL) {
+    if (flag == 0 && out != NULL) {
         return out;
     }
     *out_size = 0;
diff --git a/libcffmpeg.h b/libcffmpeg.h
index 7bc4998..acf5666 100644
--- a/libcffmpeg.h
+++ b/libcffmpeg.h
@@ -63,7 +63,7 @@
 void* wrap_fn_decode(void *lib, const char* file, const int gb, int* wid, int* hei);
 // for encoder
 typedef void* cencoder;
-typedef cencoder (*lib_cffmpeg_create_encoder)(const int w, const int h, const int fps, const int br, const int scale_flag, const int gi);
+typedef cencoder (*lib_cffmpeg_create_encoder)(const int w, const int h, const int fps, const int br, const int scale_flag, const int gi, const int fmt, const char *file);
 typedef void (*lib_cffmpeg_destroy_encoder)(cencoder h);
 typedef int (*lib_cffmpeg_encode)(cencoder hdl, uint8_t *in, const int w, const int h, uint8_t **out, int *size, int *key);
 
@@ -71,7 +71,7 @@
 static lib_cffmpeg_destroy_encoder fn_destroy_encoder = NULL;
 static lib_cffmpeg_encode fn_encode = NULL;
 
-cencoder wrap_fn_create_encoder(void *lib, const int w, const int h, const int fps, const int br, const int scale_flag, const int gi);
+cencoder wrap_fn_create_encoder(void *lib, const int w, const int h, const int fps, const int br, const int scale_flag, const int gi, const int fmt, const char *file);
 void wrap_fn_destroy_encoder(void *lib, const cencoder h);
 void* wrap_fn_encode(void *lib, cencoder hdl, void *in, const int w, const int h, int *out_size, int *key);
 

--
Gitblit v1.8.0