From 1b4d2718a4864190a47d8b7863f36a9ae0b8055c Mon Sep 17 00:00:00 2001
From: houxiao <houxiao@454eff88-639b-444f-9e54-f578c98de674>
Date: 星期五, 28 四月 2017 17:38:09 +0800
Subject: [PATCH] optimize

---
 VisitFace/RtspNativeCodec/app/src/main/cpp/FaceCache.cpp |  100 ++++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 97 insertions(+), 3 deletions(-)

diff --git a/VisitFace/RtspNativeCodec/app/src/main/cpp/FaceCache.cpp b/VisitFace/RtspNativeCodec/app/src/main/cpp/FaceCache.cpp
index 4be9089..aec1267 100644
--- a/VisitFace/RtspNativeCodec/app/src/main/cpp/FaceCache.cpp
+++ b/VisitFace/RtspNativeCodec/app/src/main/cpp/FaceCache.cpp
@@ -11,13 +11,16 @@
 
 #include <vector>
 
-#include "PbFaceList.pb.h"
+#include <PbFaceList.pb.h>
 
 #include <libyuv.h>
+#include <MediaHelper.h>
+
 #define SUBSAMPLE(v, a) ((((v) + (a) - 1)) / (a))
 
 //#define YUV420_TO_RGB888 1
-#define YUV420_TO_RGB565 1
+//#define YUV420_TO_RGB565 1
+#define YUV420_TO_ABGR8888 1
 
 struct FcPmBreackerContext
 {
@@ -92,6 +95,34 @@
                             frameRGB, 2 * src_width,
                             src_width, src_height);
         frameRGBSize = src_height * src_width * 2;
+        return true;
+    }
+
+    bool convertYUV420ToABGR8888()
+    {
+        int src_height = height;
+        int src_width = width;
+        const uint8* src_y = (const uint8*)(frameYUV);
+        const uint8* src_u = (const uint8*)(src_y + (src_height * src_width));
+        const uint8* src_v = (const uint8*)(src_u + (src_height * src_width / 4));
+
+        libyuv::I420ToABGR(src_y, src_width, // android ARGB_8888 is ABGR
+                             src_u, SUBSAMPLE(src_width, 2),
+                             src_v, SUBSAMPLE(src_width, 2),
+                             frameRGB, 4 * src_width,
+                             src_width, src_height);
+        frameRGBSize = src_height * src_width * 4;
+
+        //{
+        //    static size_t f = 0;
+        //    char fname[50];
+        //    sprintf(fname, "/sdcard/face-%u.argb", ++f);
+        //    FILE *pFile = fopen(fname, "wb");
+        //    fwrite(frameRGB, 1, frameRGBSize, pFile);
+        //    fclose(pFile);
+        //    if (f > 10)exit(0);
+        //}
+
         return true;
     }
 };
@@ -194,7 +225,7 @@
 		PbFaceList_FaceListImage& pbFaceListImage(*(pbFaceList.add_images()));
 		pbFaceListImage.set_idx(i);
 		pbFaceListImage.set_size(s);
-		pbFaceListImage.set_type(PbFaceList_FaceListImage_ImageType_IT_Y);
+		pbFaceListImage.set_type(PbFaceList_FaceListImage_ImageType_MBFT_Y8);
 		pbFaceListImage.set_width(roiMat.cols);
 		pbFaceListImage.set_height(roiMat.rows);
 		pbFaceListImage.set_top_left_x(faceRect.leftTop.x);
@@ -391,4 +422,67 @@
     return true;
 }
 
+#elif YUV420_TO_ABGR8888
+
+bool FaceCache::getFaceListImage(std::vector<NativeImgIdx>& imgIdxes, uint8_t* buffImg, size_t& buffImgMaxSize)
+{
+    FcPmBreackerContext& ctx(*(FcPmBreackerContext*)_ctx);
+    if (!ctx.dataAvailable)
+        return false;
+
+    if (ctx.frameRGBSize == 0)
+    {
+        //#todo should optimize not convert the whole image
+        if (! ctx.convertYUV420ToABGR8888())
+            return false;
+    }
+
+    imgIdxes.clear();
+    size_t offset = 0;
+
+    cv::Mat rgbMat(cv::Size(ctx.width, ctx.height), CV_8UC4, ctx.frameRGB);
+    for (st_ff_vect_t::const_iterator ffiter = ctx.faceFeatures.begin(); ffiter != ctx.faceFeatures.end(); ++ffiter)
+    {
+        const FaceRect& faceRect(ffiter->rect);
+
+        cv::Mat roiMat1(rgbMat, cv::Rect(faceRect.leftTop.x, faceRect.leftTop.y, faceRect.rightBottom.x - faceRect.leftTop.x, faceRect.rightBottom.y - faceRect.leftTop.y));
+        cv::Mat roiMat(roiMat1.clone()); // #todo copy data should be avoid!!!!
+
+        NativeImgIdx imgidx;
+        imgidx.offset = offset;
+        imgidx.size = roiMat.total() * roiMat.elemSize();
+        imgidx.type = MB_Frame::MBFT_ABGR8888;
+        imgidx.width = roiMat.cols;
+        imgidx.height = roiMat.rows;
+
+        const size_t newSize = imgidx.offset + imgidx.size;
+
+        //isContinuous
+        if (newSize > buffImgMaxSize)
+        {
+            LOG_ERROR << "FaceCache::getFaceListImage buffImgMaxSize truncated" << LOG_ENDL;
+            return false;
+        }
+
+        uint8_t* pbuf = buffImg + imgidx.offset;
+        memcpy(pbuf, roiMat.ptr(), imgidx.size);
+
+        //{
+        //    static size_t f = 0;
+        //    char fname[50];
+        //    sprintf(fname, "/sdcard/face-%u-w%d-h%d.argb", ++f, imgidx.width, imgidx.height);
+        //    FILE *pFile = fopen(fname, "wb");
+        //    fwrite(pbuf, 1, imgidx.size, pFile);
+        //    fclose(pFile);
+        //    if (f > 20)exit(0);
+        //}
+
+        imgIdxes.push_back(imgidx);
+        offset = newSize;
+    }
+
+    buffImgMaxSize = offset;
+    return true;
+}
+
 #endif

--
Gitblit v1.8.0