瀏覽代碼

申请内存缓存局部数组

DevYK 3 年之前
父節點
當前提交
9d504321cd
共有 3 個文件被更改,包括 36 次插入17 次删除
  1. 1 1
      src/ffmpeg/av_decode.h
  2. 11 1
      src/opencv/image_fingerprint.cpp
  3. 24 15
      src/utils/video_similarity.cpp

+ 1 - 1
src/ffmpeg/av_decode.h

@@ -26,7 +26,7 @@ typedef struct ImageHashModel {
     /*图片指纹个数*/
     int img_len;
     /*帧对应的指纹*/
-    const char *image_hash;
+    const char image_hash[64];
 } ImageHashModel;
 
 typedef struct VideoSimilarityModel {

+ 11 - 1
src/opencv/image_fingerprint.cpp

@@ -51,7 +51,16 @@ const char *fingerprintFromFFAVFrame(FILE *file, AVFrame *frame, int *len) {
 //        return buf;
 //    }
 
-    auto img = avframeToCvmat(frame);
+//    auto img = avframeToCvmat(frame);
+    int width = frame->width;
+    int height = frame->height;
+    cv::Mat img(height, width, CV_8UC3);
+    int cvLinesizes[1];
+    cvLinesizes[0] = img.step1();
+    SwsContext *conversion = sws_getContext(width, height, (AVPixelFormat) frame->format, width, height,
+                                            AVPixelFormat::AV_PIX_FMT_BGR24, SWS_FAST_BILINEAR, NULL, NULL, NULL);
+    sws_scale(conversion, frame->data, frame->linesize, 0, height, &img.data, cvLinesizes);
+    sws_freeContext(conversion);
     if (img.empty()) {
         printf("image is empty.");
         return NULL;
@@ -73,6 +82,7 @@ const char *fingerprintFromFFAVFrame(FILE *file, AVFrame *frame, int *len) {
     static char buf[64];
 //    char *buf = (char *) malloc(sizeof(char) * scale_width * scale_height);
     if (file) fprintf(file, "fingerprintFromFFAVFrame 7\n");
+    if (gray.cols * gray.rows < scale_height * scale_width)return NULL;
 //    //得到图像指纹值
     for (int i = 0; i < 8; i++) {
         for (int j = 0; j < 8; j++) {

+ 24 - 15
src/utils/video_similarity.cpp

@@ -141,18 +141,18 @@ int video_similarity_detection_start(long id, int force_keyframe,
         model->img_len = len;
 
         if (len > 0) {
-            model->image_hash = static_cast<const char *>(malloc(sizeof(char) * len+1));
-            memset((void*)model->image_hash,'\0',sizeof(char) * len+1);
-            string append_array;
-            for (int i = 0; i < len; ++i) {
-                char c = image_hash[i];
-                if (c == 0) {
-                    append_array.append("0");
-                } else if (c == 1) {
-                    append_array.append("1");
-                }
-            }
-            strcpy((char *)model->image_hash,append_array.c_str());
+//            model->image_hash = static_cast<const char *>(malloc(sizeof(char) * len+1));
+//            memset((void*)model->image_hash,'\0',sizeof(char) * len+1);
+//            string append_array;
+//            for (int i = 0; i < len; ++i) {
+//                char c = image_hash[i];
+//                if (c == 0) {
+//                    append_array.append("0");
+//                } else if (c == 1) {
+//                    append_array.append("1");
+//                }
+//            }
+            strcpy((char *)model->image_hash,image_hash);
         }
         if (video_frame->pts >= endTimeMs && (*lists).size() > 1 && index + 1 < (*lists).size()) {
             index++;
@@ -238,9 +238,18 @@ const char *videoSimilarity2json(VideoSimilarity *videoSimilarity) {
 //                LOGE("size=%d %d\n", item->hashs.size(), item2->img_len);
             }
             if (item2->image_hash && item2->img_len > 0) {
-                va["imageHash"] = item2->image_hash;
-                free((void *) item2->image_hash);
-                item2->image_hash = NULL;
+                string append_array;
+                for (int i = 0; i <  item2->img_len; ++i) {
+                    char c = item2->image_hash[i];
+                    if (c == 0) {
+                        append_array.append("0");
+                    } else if (c == 1) {
+                        append_array.append("1");
+                    }
+                }
+                va["imageHash"] = append_array.c_str();
+//                free((void *) item2->image_hash);
+//                item2->image_hash = NULL;
             }
             va["hashSize"] = item2->img_len;
             va["pts"] = item2->pts;