]> git.sesse.net Git - ffmpeg/commitdiff
avfilter/bbox: add support for >8 depth
authorPaul B Mahol <onemda@gmail.com>
Sun, 17 Jan 2021 11:06:01 +0000 (12:06 +0100)
committerPaul B Mahol <onemda@gmail.com>
Sun, 17 Jan 2021 11:06:01 +0000 (12:06 +0100)
libavfilter/bbox.c
libavfilter/bbox.h
libavfilter/vf_bbox.c
libavfilter/vf_removelogo.c

index be9b2e6b7352ebe668d93744b5b7948cd27e02b1..3ed9045b02f96ebddf33cd97ce9800864fa55f1d 100644 (file)
 
 #include "bbox.h"
 
-int ff_calculate_bounding_box(FFBoundingBox *bbox,
-                              const uint8_t *data, int linesize, int w, int h,
-                              int min_val)
-{
-    int x, y;
-    int start_x;
-    int start_y;
-    int end_x;
-    int end_y;
-    const uint8_t *line;
-
-    /* left bound */
-    for (start_x = 0; start_x < w; start_x++)
-        for (y = 0; y < h; y++)
-            if ((data[y * linesize + start_x] > min_val))
-                goto outl;
-outl:
-    if (start_x == w) /* no points found */
-        return 0;
-
-    /* right bound */
-    for (end_x = w - 1; end_x >= start_x; end_x--)
-        for (y = 0; y < h; y++)
-            if ((data[y * linesize + end_x] > min_val))
-                goto outr;
-outr:
-
-    /* top bound */
-    line = data;
-    for (start_y = 0; start_y < h; start_y++) {
-        for (x = 0; x < w; x++)
-            if (line[x] > min_val)
-                goto outt;
-        line += linesize;
-    }
-outt:
+#define BBOX(type, name)                                             \
+static int bbox_##name(FFBoundingBox *bbox,                          \
+                       const type *data, int linesize, int w, int h, \
+                       int min_val)                                  \
+{                                                                    \
+    int x, y;                                                        \
+    int start_x;                                                     \
+    int start_y;                                                     \
+    int end_x;                                                       \
+    int end_y;                                                       \
+    const type *line;                                                \
+                                                                     \
+    /* left bound */                                                 \
+    for (start_x = 0; start_x < w; start_x++)                        \
+        for (y = 0; y < h; y++)                                      \
+            if ((data[y * linesize + start_x] > min_val))            \
+                goto outl;                                           \
+outl:                                                                \
+    if (start_x == w) /* no points found */                          \
+        return 0;                                                    \
+                                                                     \
+    /* right bound */                                                \
+    for (end_x = w - 1; end_x >= start_x; end_x--)                   \
+        for (y = 0; y < h; y++)                                      \
+            if ((data[y * linesize + end_x] > min_val))              \
+                goto outr;                                           \
+outr:                                                                \
+                                                                     \
+    /* top bound */                                                  \
+    line = data;                                                     \
+    for (start_y = 0; start_y < h; start_y++) {                      \
+        for (x = 0; x < w; x++)                                      \
+            if (line[x] > min_val)                                   \
+                goto outt;                                           \
+        line += linesize;                                            \
+    }                                                                \
+outt:                                                                \
+                                                                     \
+    /* bottom bound */                                               \
+    line = data + (h-1)*linesize;                                    \
+    for (end_y = h - 1; end_y >= start_y; end_y--) {                 \
+        for (x = 0; x < w; x++)                                      \
+            if (line[x] > min_val)                                   \
+                goto outb;                                           \
+        line -= linesize;                                            \
+    }                                                                \
+outb:                                                                \
+                                                                     \
+    bbox->x1 = start_x;                                              \
+    bbox->y1 = start_y;                                              \
+    bbox->x2 = end_x;                                                \
+    bbox->y2 = end_y;                                                \
+    return 1;                                                        \
+}
 
-    /* bottom bound */
-    line = data + (h-1)*linesize;
-    for (end_y = h - 1; end_y >= start_y; end_y--) {
-        for (x = 0; x < w; x++)
-            if (line[x] > min_val)
-                goto outb;
-        line -= linesize;
-    }
-outb:
+BBOX(uint8_t, 8)
+BBOX(uint16_t, 16)
 
-    bbox->x1 = start_x;
-    bbox->y1 = start_y;
-    bbox->x2 = end_x;
-    bbox->y2 = end_y;
-    return 1;
+int ff_calculate_bounding_box(FFBoundingBox *bbox,
+                              const uint8_t *data, int linesize,
+                              int w, int h,
+                              int min_val, int depth)
+{
+    if (depth <= 8)
+        return bbox_8(bbox, data, linesize, w, h, min_val);
+    else
+        return bbox_16(bbox, (const uint16_t *)data, linesize / 2, w, h, min_val);
 }
index cf026b81f9c7d4cf2de00b8be23ec681fd0b93e2..116158d59ed99aa2f3eb60945adc0c0a5c1ac592 100644 (file)
@@ -39,6 +39,6 @@ typedef struct FFBoundingBox {
  */
 int ff_calculate_bounding_box(FFBoundingBox *bbox,
                               const uint8_t *data, int linesize,
-                              int w, int h, int min_val);
+                              int w, int h, int min_val, int depth);
 
 #endif /* AVFILTER_BBOX_H */
index 4ac2980a6c4af4aa606fa5d899c8c892a023c1e4..79fa03d7f55e7e6e55635badecfbbc30405191bb 100644 (file)
@@ -75,7 +75,7 @@ static int filter_frame(AVFilterLink *inlink, AVFrame *frame)
     has_bbox =
         ff_calculate_bounding_box(&box,
                                   frame->data[0], frame->linesize[0],
-                                  inlink->w, inlink->h, bbox->min_val);
+                                  inlink->w, inlink->h, bbox->min_val, 8);
     w = box.x2 - box.x1 + 1;
     h = box.y2 - box.y1 + 1;
 
index aff2d12a17bf34588f21fe1e114da2c159bc219c..4026bdd09abf5760b9c60f1ec42c2aba00deaac3 100644 (file)
@@ -342,8 +342,8 @@ static av_cold int init(AVFilterContext *ctx)
 
     /* Calculate our bounding rectangles, which determine in what
      * region the logo resides for faster processing. */
-    ff_calculate_bounding_box(&s->full_mask_bbox, s->full_mask_data, w, w, h, 0);
-    ff_calculate_bounding_box(&s->half_mask_bbox, s->half_mask_data, w/2, w/2, h/2, 0);
+    ff_calculate_bounding_box(&s->full_mask_bbox, s->full_mask_data, w, w, h, 0, 8);
+    ff_calculate_bounding_box(&s->half_mask_bbox, s->half_mask_data, w/2, w/2, h/2, 0, 8);
 
 #define SHOW_LOGO_INFO(mask_type)                                       \
     av_log(ctx, AV_LOG_VERBOSE, #mask_type " x1:%d x2:%d y1:%d y2:%d max_mask_size:%d\n", \