houxiao
2017-08-09 353d2469941ee78798a68e60824aafd09c498e4a
VisitFace/RtspNativeCodec/app/src/main/cpp/FaceCache.cpp
@@ -1,7 +1,7 @@
#include "FaceCache.h"
#include "PipeLine.h"
#include "MaterialBuffer.h"
#include "PL_SensetimeFaceTrack.h"
#include "PL_SensetimeFaceTrackMultiTrd.h"
#include <logger.h>
#include <Logger/src/logger.hpp>
@@ -16,16 +16,16 @@
#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_ABGR8888 1
#define YUV420_TO_RGB565 1
//#define YUV420_TO_ABGR8888 1
//#define NV12_TO_ABGR8888 1
struct FcPmBreackerContext
{
   uint8_t frameYUV[1920*1080*4];
   size_t frameYUVSize;
    MB_Frame::MBFType frameYUVType;
   
   uint8_t frameRGB[1920*1088*4];
   size_t frameRGBSize;
@@ -38,7 +38,7 @@
   bool dataAvailable;
   
   FcPmBreackerContext() :
            frameYUV(), frameYUVSize(0), frameRGB(), frameRGBSize(0),
            frameYUV(), frameYUVSize(0), frameYUVType(MB_Frame::MBFT__FIRST), frameRGB(), frameRGBSize(0),
            faceFeatures(), width(0), height(0), dataAvailable(false)
   {
   }
@@ -62,8 +62,8 @@
        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),
                           src_u, MH_SUBSAMPLE1(src_width, 2),
                           src_v, MH_SUBSAMPLE1(src_width, 2),
                           frameRGB, 3 * src_width,
                           src_width, src_height);
        frameRGBSize = src_height * src_width * 3;
@@ -89,11 +89,31 @@
        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);
        if (frameYUVType == MB_Frame::MBFT_YUV420)
        {
            libyuv::I420ToRGB565(src_y, src_width,
                                 src_u, MH_SUBSAMPLE1(src_width, 2),
                                 src_v, MH_SUBSAMPLE1(src_width, 2),
                                 frameRGB, 2 * src_width,
                                 src_width, src_height);
        }
        else if (frameYUVType == MB_Frame::MBFT_NV12)
        {
            libyuv::NV12ToRGB565(src_y, src_width,
                                 src_u, src_width,
                                 frameRGB, 2 * src_width,
                                 src_width, src_height);
            //{
            //    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);
            //}
        }
        frameRGBSize = src_height * src_width * 2;
        return true;
    }
@@ -107,8 +127,8 @@
        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),
                             src_u, MH_SUBSAMPLE1(src_width, 2),
                             src_v, MH_SUBSAMPLE1(src_width, 2),
                             frameRGB, 4 * src_width,
                             src_width, src_height);
        frameRGBSize = src_height * src_width * 4;
@@ -139,12 +159,21 @@
   return true;
}
bool fc_pm_breaker_ptr_count(const PipeMaterial* pm, void* args)
{
    size_t* count = (size_t*)args;
    const st_ff_vect_t& faceFeatures(*(const st_ff_vect_t*)(pm->buffer));
    *count = faceFeatures.size();
    return false;
}
bool fc_pm_breaker_frame(const PipeMaterial* pm, void* args)
{
   FcPmBreackerContext* ctx = (FcPmBreackerContext*)args;
   
   const MB_Frame* lastFrame((const MB_Frame*)(pm->buffer));
   if (lastFrame->type != MB_Frame::MBFT_YUV420)
   if (lastFrame->type != MB_Frame::MBFT_YUV420 && lastFrame->type != MB_Frame::MBFT_NV12)
   {
      ctx->dataAvailable &= false;
      return false;
@@ -157,6 +186,7 @@
   }
    ctx->frameYUVSize = lastFrame->buffSize;
    ctx->frameYUVType = lastFrame->type;
    ctx->width = lastFrame->width;
    ctx->height = lastFrame->height;
   memcpy(ctx->frameYUV, lastFrame->buffer, ctx->frameYUVSize);
@@ -201,6 +231,15 @@
      return 0;
}
// returns count of face
size_t FaceCache::getFaceCount(const PipeMaterial& pm) const
{
    size_t count = 0;
    pm.breake(PipeMaterial::PMT_PTR, MB_Frame::MBFT__FIRST, fc_pm_breaker_ptr_count, &count);
    return count;
}
bool FaceCache::getFaceListPb(uint8_t* buffer, size_t& buffMaxSize)
{
    FcPmBreackerContext& ctx(*(FcPmBreackerContext*)_ctx);
@@ -217,8 +256,11 @@
    for (int i = 0; i < ctx.faceFeatures.size(); i++)
    {
        const FaceRect& faceRect(ctx.faceFeatures[i].rect);
        cv::Mat roiMat1(yMat, cv::Rect(faceRect.leftTop.x, faceRect.leftTop.y, faceRect.rightBottom.x - faceRect.leftTop.x, faceRect.rightBottom.y - faceRect.leftTop.y));
        if (! ctx.faceFeatures[i].test_face_in_cone(35.0f, 35.0f, 35.0f))
            continue;
        const PLGH_Rect& faceRect(ctx.faceFeatures[i].rect);
        cv::Mat roiMat1(yMat, cv::Rect(faceRect.leftTop.X, faceRect.leftTop.Y, faceRect.width(), faceRect.height()));
      cv::Mat roiMat(roiMat1.clone()); // #todo copy data should be avoid!!!!
      size_t s = roiMat.total() * roiMat.elemSize();
@@ -228,8 +270,8 @@
      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);
      pbFaceListImage.set_top_left_y(faceRect.leftTop.y);
      pbFaceListImage.set_top_left_x(faceRect.leftTop.X);
      pbFaceListImage.set_top_left_y(faceRect.leftTop.Y);
      
      pbFaceListImage.add_img(roiMat.ptr(), s);
@@ -294,7 +336,7 @@
    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 roiMat1(rgbMat, cv::Rect(faceRect.leftTop.x, faceRect.leftTop.y, faceRect.width(), faceRect.height()));
        cv::Mat roiMat(roiMat1.clone()); // #todo copy data should be avoid!!!!
        NativeImgIdx imgidx;
@@ -370,8 +412,11 @@
    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));
        if (! ffiter->test_face_in_cone(35.0f, 35.0f, 35.0f))
            continue;
        const PLGH_Rect& faceRect(ffiter->rect);
        cv::Mat roiMat1(rgbMat, cv::Rect(faceRect.leftTop.X, faceRect.leftTop.Y, faceRect.width(), faceRect.height()));
        cv::Mat roiMat(roiMat1.clone()); // #todo copy data should be avoid!!!!
        NativeImgIdx imgidx;