aboutsummaryrefslogtreecommitdiffstats
path: root/libavfilter/bbox.c
blob: 3ed9045b02f96ebddf33cd97ce9800864fa55f1d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * Copyright (c) 2005 Robert Edele <yartrebo@earthlink.net>
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include "bbox.h"

#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;                                                        \
}

BBOX(uint8_t, 8)
BBOX(uint16_t, 16)

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);
}