[FFmpeg-devel] [PATCH v2 FFmpeg 3/20] libavfilter/dnn: move contain_valid_detection_bbox to dnn_backend_common

m.kaindl0208 at gmail.com m.kaindl0208 at gmail.com
Mon Mar 10 21:51:43 EET 2025


Signed-off-by: MaximilianKaindl <m.kaindl0208 at gmail.com>
---
 libavfilter/dnn/dnn_backend_common.c   | 38 +++++++++++++++++++++++++
 libavfilter/dnn/dnn_backend_common.h   |  8 ++++++
 libavfilter/dnn/dnn_backend_openvino.c | 39 +-------------------------
 3 files changed, 47 insertions(+), 38 deletions(-)

diff --git a/libavfilter/dnn/dnn_backend_common.c b/libavfilter/dnn/dnn_backend_common.c
index e45eefd14d..3944db35b5 100644
--- a/libavfilter/dnn/dnn_backend_common.c
+++ b/libavfilter/dnn/dnn_backend_common.c
@@ -23,6 +23,7 @@

 #include "libavutil/mem.h"
 #include "dnn_backend_common.h"
+#include "libavutil/detection_bbox.h"

 #define DNN_ASYNC_SUCCESS (void *)0
 #define DNN_ASYNC_FAIL (void *)-1
@@ -178,3 +179,40 @@ int ff_dnn_fill_gettingoutput_task(TaskItem *task, DNNExecBaseParams *exec_param

     return ff_dnn_fill_task(task, exec_params, backend_model, 0, 0);
 }
+
+int ff_dnn_contain_valid_detection_bbox(AVFrame *frame)
+{
+    AVFrameSideData *sd;
+    const AVDetectionBBoxHeader *header;
+    const AVDetectionBBox *bbox;
+
+    sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DETECTION_BBOXES);
+    if (!sd) { // this frame has nothing detected
+        return 0;
+    }
+
+    if (!sd->size) {
+        return 0;
+    }
+
+    header = (const AVDetectionBBoxHeader *)sd->data;
+    if (!header->nb_bboxes) {
+        return 0;
+    }
+
+    for (uint32_t i = 0; i < header->nb_bboxes; i++) {
+        bbox = av_get_detection_bbox(header, i);
+        if (bbox->x < 0 || bbox->w < 0 || bbox->x + bbox->w >= frame->width) {
+            return 0;
+        }
+        if (bbox->y < 0 || bbox->h < 0 || bbox->y + bbox->h >= frame->height) {
+            return 0;
+        }
+
+        if (bbox->classify_count == AV_NUM_DETECTION_BBOX_CLASSIFY) {
+            return 0;
+        }
+    }
+
+    return 1;
+}
\ No newline at end of file
diff --git a/libavfilter/dnn/dnn_backend_common.h b/libavfilter/dnn/dnn_backend_common.h
index 9f5d37b3e0..b5b15bc2f6 100644
--- a/libavfilter/dnn/dnn_backend_common.h
+++ b/libavfilter/dnn/dnn_backend_common.h
@@ -157,4 +157,12 @@ DNNAsyncStatusType ff_dnn_get_result_common(Queue *task_queue, AVFrame **in, AVF
  */
 int ff_dnn_fill_gettingoutput_task(TaskItem *task, DNNExecBaseParams *exec_params, void *backend_model, int input_height, int input_width, void *ctx);

+/**
+ * Check if the given frame contains a valid detection bounding box.
+ *
+ * @param frame The frame to check for valid detection bounding box
+ *
+ * @return Non-zero if frame contains valid detection bounding box, 0 otherwise
+ */
+int ff_dnn_contain_valid_detection_bbox(AVFrame *frame);
 #endif
diff --git a/libavfilter/dnn/dnn_backend_openvino.c b/libavfilter/dnn/dnn_backend_openvino.c
index 2f6706dcd4..03183af822 100644
--- a/libavfilter/dnn/dnn_backend_openvino.c
+++ b/libavfilter/dnn/dnn_backend_openvino.c
@@ -1151,43 +1151,6 @@ static int get_input_ov(DNNModel *model, DNNData *input, const char *input_name)
 #endif
 }

-static int contain_valid_detection_bbox(AVFrame *frame)
-{
-    AVFrameSideData *sd;
-    const AVDetectionBBoxHeader *header;
-    const AVDetectionBBox *bbox;
-
-    sd = av_frame_get_side_data(frame, AV_FRAME_DATA_DETECTION_BBOXES);
-    if (!sd) { // this frame has nothing detected
-        return 0;
-    }
-
-    if (!sd->size) {
-        return 0;
-    }
-
-    header = (const AVDetectionBBoxHeader *)sd->data;
-    if (!header->nb_bboxes) {
-        return 0;
-    }
-
-    for (uint32_t i = 0; i < header->nb_bboxes; i++) {
-        bbox = av_get_detection_bbox(header, i);
-        if (bbox->x < 0 || bbox->w < 0 || bbox->x + bbox->w >= frame->width) {
-            return 0;
-        }
-        if (bbox->y < 0 || bbox->h < 0 || bbox->y + bbox->h >= frame->height) {
-            return 0;
-        }
-
-        if (bbox->classify_count == AV_NUM_DETECTION_BBOX_CLASSIFY) {
-            return 0;
-        }
-    }
-
-    return 1;
-}
-
 static int extract_lltask_from_task(DNNFunctionType func_type, TaskItem *task, Queue *lltask_queue, DNNExecBaseParams *exec_params)
 {
     switch (func_type) {
@@ -1217,7 +1180,7 @@ static int extract_lltask_from_task(DNNFunctionType func_type, TaskItem *task, Q
         task->inference_todo = 0;
         task->inference_done = 0;

-        if (!contain_valid_detection_bbox(frame)) {
+        if (!ff_dnn_contain_valid_detection_bbox(frame)) {
             return 0;
         }

--
2.34.1




More information about the ffmpeg-devel mailing list