From 87d2a9dc6feba9e20cf40f6fd9cd6bac79838504 Mon Sep 17 00:00:00 2001
From: xuxiuxi <xuxiuxi@454eff88-639b-444f-9e54-f578c98de674>
Date: 星期二, 18 四月 2017 18:12:27 +0800
Subject: [PATCH]
---
VisitFace/RtspNativeCodec/app/src/main/cpp/FaceCache.cpp | 235 +++++++++++++++++++++++++++++++++++++++++++++++++++-------
1 files changed, 206 insertions(+), 29 deletions(-)
diff --git a/VisitFace/RtspNativeCodec/app/src/main/cpp/FaceCache.cpp b/VisitFace/RtspNativeCodec/app/src/main/cpp/FaceCache.cpp
index 160ed07..0437b6e 100644
--- a/VisitFace/RtspNativeCodec/app/src/main/cpp/FaceCache.cpp
+++ b/VisitFace/RtspNativeCodec/app/src/main/cpp/FaceCache.cpp
@@ -13,6 +13,12 @@
#include "PbFaceList.pb.h"
+#include <libyuv.h>
+#define SUBSAMPLE(v, a) ((((v) + (a) - 1)) / (a))
+
+//#define YUV420_TO_RGB888 1
+#define YUV420_TO_RGB565 1
+
struct FcPmBreackerContext
{
uint8_t frameYUV[1920*1080*4];
@@ -44,9 +50,50 @@
faceFeatures.clear();
}
- bool convertYUV420ToRGB()
+ bool convertYUV420ToRGB888()
{
+ 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::I420ToRGB24(src_y, src_width,
+ src_u, SUBSAMPLE(src_width, 2),
+ src_v, SUBSAMPLE(src_width, 2),
+ frameRGB, 3 * src_width,
+ src_width, src_height);
+ frameRGBSize = src_height * src_width * 3;
+
+ //{
+ // static size_t f = 0;
+ // char fname[50];
+ // sprintf(fname, "/sdcard/face-%u.rgb", ++f);
+ // FILE *pFile = fopen(fname, "wb");
+ // fwrite(frameRGB, 1, frameRGBSize, pFile);
+ // fclose(pFile);
+ // if (f > 20)exit(0);
+ //}
+
+ return true;
}
+
+ bool convertYUV420ToRGB565()
+ {
+ 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::I420ToRGB565(src_y, src_width,
+ src_u, SUBSAMPLE(src_width, 2),
+ src_v, SUBSAMPLE(src_width, 2),
+ frameRGB, 2 * src_width,
+ src_width, src_height);
+ frameRGBSize = src_height * src_width * 2;
+ return true;
+ }
};
bool fc_pm_breaker_ptr(const PipeMaterial* pm, void* args)
@@ -58,7 +105,7 @@
ctx->faceFeatures = faceFeatures;
ctx->dataAvailable &= true;
- return false;
+ return true;
}
bool fc_pm_breaker_frame(const PipeMaterial* pm, void* args)
@@ -84,7 +131,14 @@
memcpy(ctx->frameYUV, lastFrame->buffer, ctx->frameYUVSize);
ctx->dataAvailable &= true;
- return false;
+ return true;
+}
+
+NativeImgIdx::operator std::string() const
+{
+ char buf[256];
+ sprintf(buf, "offset=%d, size=%d, type=%d, width=%d, height=%d", offset, size, type, width, height);
+ return std::string(buf);
}
#ifdef USE_ST_SDK
@@ -119,9 +173,12 @@
bool FaceCache::getFaceListPb(uint8_t* buffer, size_t& buffMaxSize)
{
FcPmBreackerContext& ctx(*(FcPmBreackerContext*)_ctx);
+ if (!ctx.dataAvailable)
+ return false;
PbFaceList pbFaceList;
- pbFaceList.set_image_count(ctx.faceFeatures.size());
+ pbFaceList.set_magic(pbFaceList.magic());
+ pbFaceList.set_image_count(ctx.faceFeatures.size());
pbFaceList.set_src_width(ctx.width);
pbFaceList.set_src_height(ctx.height);
@@ -130,57 +187,177 @@
for (int i = 0; i < ctx.faceFeatures.size(); i++)
{
const FaceRect& faceRect(ctx.faceFeatures[i].rect);
- cv::Mat roiMat(yMat, cv::Rect(faceRect.leftTop.x, faceRect.leftTop.y, faceRect.rightBottom.x - faceRect.leftTop.x, faceRect.rightBottom.y - faceRect.leftTop.y));
-
+ cv::Mat roiMat1(yMat, 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!!!!
+ size_t s = roiMat.total() * roiMat.elemSize();
+
PbFaceList_FaceListImage pbFaceListImage;
pbFaceListImage.set_idx(i);
- pbFaceListImage.set_size(roiMat.elemSize());
+ pbFaceListImage.set_size(s);
pbFaceListImage.set_type(PbFaceList_FaceListImage_ImageType_IT_Y);
pbFaceListImage.set_width(roiMat.cols);
pbFaceListImage.set_height(roiMat.rows);
pbFaceListImage.set_top_left_x(faceRect.leftTop.x);
pbFaceListImage.set_top_left_y(faceRect.leftTop.y);
- pbFaceListImage.set_img(0, roiMat.ptr(), roiMat.elemSize());
+ pbFaceListImage.add_img(roiMat.ptr(), s);
+
+ //{
+ // static size_t f = 0;
+ // char fname[50];
+ // sprintf(fname, "/sdcard/face-%u.yuv", ++f);
+ // FILE *pFile = fopen(fname, "wb");
+ // fwrite(roiMat.ptr(), 1, s, pFile);
+ // fclose(pFile);
+ // if (f > 20)exit(0);
+ //}
}
+ size_t s = pbFaceList.ByteSize();
+ buffMaxSize = std::min(s, buffMaxSize);
+ pbFaceList.SerializeToArray(buffer, buffMaxSize);
- return false;
+ return buffMaxSize > 0;
}
-bool FaceCache::getFaceListImage(int* buffIdx, size_t& count, uint8_t* buffImg, size_t& buffImgMaxSize)
+#ifdef YUV420_TO_RGB888
+
+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)
{
- if (! ctx.convertYUV420ToRGB())
+ //#todo should optimize not convert the whole image
+ if (! ctx.convertYUV420ToRGB888())
return false;
}
-
- count = 0;
- uint8_t* pBuff = buffImg;
- size_t totalSize = 0;
+
+ imgIdxes.clear();
+ size_t offset = 0;
cv::Mat rgbMat(cv::Size(ctx.width, ctx.height), CV_8UC3, 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 roiMat(rgbMat, cv::Rect(faceRect.leftTop.x, faceRect.leftTop.y, faceRect.rightBottom.x - faceRect.leftTop.x, faceRect.rightBottom.y - faceRect.leftTop.y));
-
+ 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_RGB888;
+ imgidx.width = roiMat.cols;
+ imgidx.height = roiMat.rows;
+
+ const size_t newSize = imgidx.offset + imgidx.size;
+
//isContinuous
- size_t s = roiMat.elemSize();
- if (totalSize + s <= buffImgMaxSize)
- {
- memcpy(pBuff, roiMat.ptr(), s);
- *buffIdx = int(totalSize);
-
- pBuff += s;
- totalSize += s;
- count++;
- buffIdx++;
- }
+ 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);
+
+ //uint8_t* pbuf = buffImg + imgidx.offset;
+ //cv::Mat roiMat1(roiMat.cols, roiMat.rows, CV_8UC3, pbuf);
+ //pbuf[0]=123;
+ //roiMat.copyTo(roiMat1);
+ //memcpy(buffImg + imgidx.offset, roiMat.ptr(), imgidx.size);
+
+ //uint8_t* pbuf = buffImg + imgidx.offset;
+ //for(size_t r = 0; r < roiMat.rows; r++)
+ //{
+ // const uint8_t* pcol = roiMat.ptr<uint8_t>(r);
+ // memcpy(pbuf, pcol, roiMat.cols);
+ // pbuf += roiMat.cols;
+ //}
+
+ //{
+ // static size_t f = 0;
+ // char fname[50];
+ // sprintf(fname, "/sdcard/face-%u.rgb", ++f);
+ // FILE *pFile = fopen(fname, "wb");
+ // fwrite(pbuf, 1, imgidx.size, pFile);
+ // fclose(pFile);
+ // if (f > 20)exit(0);
+ //}
+
+ imgIdxes.push_back(imgidx);
+ offset = newSize;
}
- return (count != 0);
+ buffImgMaxSize = offset;
+ return true;
}
+
+#elif YUV420_TO_RGB565
+
+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.convertYUV420ToRGB565())
+ return false;
+ }
+
+ imgIdxes.clear();
+ size_t offset = 0;
+
+ cv::Mat rgbMat(cv::Size(ctx.width, ctx.height), CV_16UC1, 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_RGB565;
+ 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.rgb565", ++f);
+ // 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