|
@@ -2,7 +2,9 @@
|
|
|
// Created by 阳坤 on 2022/3/2.
|
|
|
//
|
|
|
|
|
|
-
|
|
|
+extern "C"{
|
|
|
+#include <libavcodec/avcodec.h>
|
|
|
+}
|
|
|
#include "image_fingerprint.h"
|
|
|
#include "../utils/yuv_convert.h"
|
|
|
|
|
@@ -12,40 +14,47 @@ int avframeToCvmat(cv::Mat &image, const AVFrame *frame) {
|
|
|
int height = frame->height;
|
|
|
int cvLinesizes[1];
|
|
|
cvLinesizes[0] = image.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, &image.data, cvLinesizes);
|
|
|
-// sws_freeContext(conversion);
|
|
|
- if (frame->format != AV_PIX_FMT_YUV420P)return -1;
|
|
|
- auto *yuv420p = (uint8_t *) av_malloc(sizeof(uint8_t) * width * height * 3 / 2);
|
|
|
- int yLen = height * width;
|
|
|
- int uLen = yLen / 4;
|
|
|
- int vLen = yLen / 4;
|
|
|
- if (width == frame->linesize[0]) {
|
|
|
- memcpy(yuv420p, frame->data[0], yLen);
|
|
|
- memcpy(yuv420p + yLen, frame->data[1], uLen);
|
|
|
- memcpy(yuv420p + yLen + uLen, frame->data[2], vLen);
|
|
|
- } else {
|
|
|
- for (int i = 0; i < height; i++) {//y
|
|
|
- memcpy(yuv420p + width * i,
|
|
|
- frame->data[0] + frame->linesize[0] * i,
|
|
|
- width);
|
|
|
- }
|
|
|
- for (int j = 0; j < height / 2; j++) {//u
|
|
|
- memcpy((yuv420p + yLen) + width / 2 * j,
|
|
|
- frame->data[1] + frame->linesize[1] * j,
|
|
|
- width / 2);
|
|
|
- }
|
|
|
- for (int k = 0; k < height / 2; k++) {//v
|
|
|
- memcpy((yuv420p + yLen + uLen) + width / 2 * k,
|
|
|
- frame->data[2] + frame->linesize[2] * k,
|
|
|
- width / 2);
|
|
|
- }
|
|
|
+ int size = avpicture_get_size(AV_PIX_FMT_BGR24, width, height);
|
|
|
+ SwsContext *conversion = sws_getContext(width, height, (AVPixelFormat) frame->format, width, height,
|
|
|
+ AVPixelFormat::AV_PIX_FMT_BGR24, SWS_FAST_BILINEAR, NULL, NULL, NULL);
|
|
|
+ if (conversion)
|
|
|
+ {
|
|
|
+ sws_scale(conversion, frame->data, frame->linesize, 0, height, &image.data, cvLinesizes);
|
|
|
+ sws_freeContext(conversion);
|
|
|
+ conversion = NULL;
|
|
|
+ return 0;
|
|
|
}
|
|
|
- I420ToRGB24(yuv420p, width, height, image.data);
|
|
|
- free(yuv420p);
|
|
|
- yuv420p = NULL;
|
|
|
- return 0;
|
|
|
+
|
|
|
+// if (frame->format != AV_PIX_FMT_YUV420P)return -1;
|
|
|
+// auto *yuv420p = (uint8_t *) av_malloc(sizeof(uint8_t) * width * height * 3 / 2);
|
|
|
+// int yLen = height * width;
|
|
|
+// int uLen = yLen / 4;
|
|
|
+// int vLen = yLen / 4;
|
|
|
+// if (width == frame->linesize[0]) {
|
|
|
+// memcpy(yuv420p, frame->data[0], yLen);
|
|
|
+// memcpy(yuv420p + yLen, frame->data[1], uLen);
|
|
|
+// memcpy(yuv420p + yLen + uLen, frame->data[2], vLen);
|
|
|
+// } else {
|
|
|
+// for (int i = 0; i < height; i++) {//y
|
|
|
+// memcpy(yuv420p + width * i,
|
|
|
+// frame->data[0] + frame->linesize[0] * i,
|
|
|
+// width);
|
|
|
+// }
|
|
|
+// for (int j = 0; j < height / 2; j++) {//u
|
|
|
+// memcpy((yuv420p + yLen) + width / 2 * j,
|
|
|
+// frame->data[1] + frame->linesize[1] * j,
|
|
|
+// width / 2);
|
|
|
+// }
|
|
|
+// for (int k = 0; k < height / 2; k++) {//v
|
|
|
+// memcpy((yuv420p + yLen + uLen) + width / 2 * k,
|
|
|
+// frame->data[2] + frame->linesize[2] * k,
|
|
|
+// width / 2);
|
|
|
+// }
|
|
|
+// }
|
|
|
+// I420ToRGB24(yuv420p, width, height, image.data);
|
|
|
+// free(yuv420p);
|
|
|
+// yuv420p = NULL;
|
|
|
+ return -1;
|
|
|
}
|
|
|
|
|
|
//cv::Mat 转 AVFrame
|
|
@@ -77,24 +86,9 @@ int fingerprintFromFFAVFrame(FILE *file, AVFrame *frame, char buf[64]) {
|
|
|
printf("fingerprintFromFFAVFrame -2\n");
|
|
|
int ret = avframeToCvmat(img, frame);
|
|
|
if (ret < 0) {
|
|
|
- printf("fingerprintFromFFAVFrame format=%d not support!\n", frame->format);
|
|
|
+ printf("sws_getContext init error .");
|
|
|
return -1;
|
|
|
}
|
|
|
-// int cvLinesizes[1];
|
|
|
-// cvLinesizes[0] = img.step1();
|
|
|
-// printf("fingerprintFromFFAVFrame -3\n");
|
|
|
-// SwsContext *conversion = sws_getContext(width, height, (AVPixelFormat) frame->format, width, height,
|
|
|
-// AVPixelFormat::AV_PIX_FMT_BGR24, SWS_FAST_BILINEAR, NULL, NULL, NULL);
|
|
|
-// if (conversion) {
|
|
|
-// printf("fingerprintFromFFAVFrame -4\n");
|
|
|
-// int ret = sws_scale(conversion, frame->data, frame->linesize, 0, height, &img.data, cvLinesizes);
|
|
|
-// printf("fingerprintFromFFAVFrame -5 ret=%d\n", ret);
|
|
|
-// sws_freeContext(conversion);
|
|
|
-// conversion = NULL;
|
|
|
-// } else {
|
|
|
-// printf("sws_getContext init error .");
|
|
|
-// return NULL;
|
|
|
-// }
|
|
|
printf("fingerprintFromFFAVFrame -6\n");
|
|
|
if (img.empty()) {
|
|
|
printf("image is empty.");
|