GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavfilter/dnn/dnn_backend_native_layer_mathbinary.c Lines: 69 108 63.9 %
Date: 2021-01-27 23:23:17 Branches: 30 45 66.7 %

Line Branch Exec Source
1
/*
2
 * Copyright (c) 2020
3
 *
4
 * This file is part of FFmpeg.
5
 *
6
 * FFmpeg is free software; you can redistribute it and/or
7
 * modify it under the terms of the GNU Lesser General Public
8
 * License as published by the Free Software Foundation; either
9
 * version 2.1 of the License, or (at your option) any later version.
10
 *
11
 * FFmpeg is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14
 * Lesser General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU Lesser General Public
17
 * License along with FFmpeg; if not, write to the Free Software
18
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19
 */
20
21
/**
22
 * @file
23
 * DNN native backend implementation.
24
 */
25
26
#include "dnn_backend_native.h"
27
#include "libavutil/avassert.h"
28
#include "dnn_backend_native_layer_mathbinary.h"
29
30
typedef float (*FunType)(float src0, float src1);
31
32
18
static float sub(float src0, float src1)
33
{
34
18
    return src0 - src1;
35
}
36
18
static float add(float src0, float src1)
37
{
38
18
    return src0 + src1;
39
}
40
18
static float mul(float src0, float src1)
41
{
42
18
    return src0 * src1;
43
}
44
18
static float realdiv(float src0, float src1)
45
{
46
18
    return src0 / src1;
47
}
48
18
static float minimum(float src0, float src1)
49
{
50
18
    return FFMIN(src0, src1);
51
}
52
18
static float floormod(float src0, float src1)
53
{
54
18
    return (float)((int)(src0) % (int)(src1));
55
}
56
57
9
static void math_binary_commutative(FunType pfun, const DnnLayerMathBinaryParams *params, const DnnOperand *input, DnnOperand *output, DnnOperand *operands, const int32_t *input_operand_indexes)
58
{
59
    int dims_count;
60
    const float *src;
61
    float *dst;
62
9
    dims_count = ff_calculate_operand_dims_count(output);
63
9
    src = input->data;
64
9
    dst = output->data;
65

9
    if (params->input0_broadcast || params->input1_broadcast) {
66
42
        for (int i = 0; i < dims_count; ++i) {
67
36
            dst[i] = pfun(params->v, src[i]);
68
        }
69
    } else {
70
3
        const DnnOperand *input1 = &operands[input_operand_indexes[1]];
71
3
        const float *src1 = input1->data;
72
21
        for (int i = 0; i < dims_count; ++i) {
73
18
            dst[i] = pfun(src[i], src1[i]);
74
        }
75
    }
76
9
}
77
9
static void math_binary_not_commutative(FunType pfun, const DnnLayerMathBinaryParams *params, const DnnOperand *input, DnnOperand *output, DnnOperand *operands, const int32_t *input_operand_indexes)
78
{
79
    int dims_count;
80
    const float *src;
81
    float *dst;
82
9
    dims_count = ff_calculate_operand_dims_count(output);
83
9
    src = input->data;
84
9
    dst = output->data;
85
9
    if (params->input0_broadcast) {
86
21
        for (int i = 0; i < dims_count; ++i) {
87
18
            dst[i] = pfun(params->v, src[i]);
88
        }
89
6
    } else if (params->input1_broadcast) {
90
21
        for (int i = 0; i < dims_count; ++i) {
91
18
            dst[i] = pfun(src[i], params->v);
92
        }
93
    } else {
94
3
        const DnnOperand *input1 = &operands[input_operand_indexes[1]];
95
3
        const float *src1 = input1->data;
96
21
        for (int i = 0; i < dims_count; ++i) {
97
18
            dst[i] = pfun(src[i], src1[i]);
98
        }
99
    }
100
9
}
101
int ff_dnn_load_layer_math_binary(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num)
102
{
103
    DnnLayerMathBinaryParams *params;
104
    int dnn_size = 0;
105
    int input_index = 0;
106
    params = av_malloc(sizeof(*params));
107
    if (!params)
108
        return 0;
109
110
    params->bin_op = (int32_t)avio_rl32(model_file_context);
111
    dnn_size += 4;
112
113
    params->input0_broadcast = (int32_t)avio_rl32(model_file_context);
114
    dnn_size += 4;
115
    if (params->input0_broadcast) {
116
        params->v = av_int2float(avio_rl32(model_file_context));
117
    } else {
118
        layer->input_operand_indexes[input_index] = (int32_t)avio_rl32(model_file_context);
119
        if (layer->input_operand_indexes[input_index] >= operands_num) {
120
            return 0;
121
        }
122
        input_index++;
123
    }
124
    dnn_size += 4;
125
126
    params->input1_broadcast = (int32_t)avio_rl32(model_file_context);
127
    dnn_size += 4;
128
    if (params->input1_broadcast) {
129
        params->v = av_int2float(avio_rl32(model_file_context));
130
    } else {
131
        layer->input_operand_indexes[input_index] = (int32_t)avio_rl32(model_file_context);
132
        if (layer->input_operand_indexes[input_index] >= operands_num) {
133
            return 0;
134
        }
135
        input_index++;
136
    }
137
    dnn_size += 4;
138
139
    layer->output_operand_index = (int32_t)avio_rl32(model_file_context);
140
    dnn_size += 4;
141
    layer->params = params;
142
143
    if (layer->output_operand_index >= operands_num) {
144
        return 0;
145
    }
146
147
    return dnn_size;
148
}
149
150
18
int ff_dnn_execute_layer_math_binary(DnnOperand *operands, const int32_t *input_operand_indexes,
151
                                     int32_t output_operand_index, const void *parameters, NativeContext *ctx)
152
{
153
18
    const DnnOperand *input = &operands[input_operand_indexes[0]];
154
18
    DnnOperand *output = &operands[output_operand_index];
155
18
    const DnnLayerMathBinaryParams *params = (const DnnLayerMathBinaryParams *)parameters;
156
157
90
    for (int i = 0; i < 4; ++i)
158
72
        output->dims[i] = input->dims[i];
159
160
18
    output->data_type = input->data_type;
161
18
    output->length = ff_calculate_operand_data_length(output);
162
18
    if (output->length <= 0) {
163
        av_log(ctx, AV_LOG_ERROR, "The output data length overflow\n");
164
        return DNN_ERROR;
165
    }
166
18
    output->data = av_realloc(output->data, output->length);
167
18
    if (!output->data) {
168
        av_log(ctx, AV_LOG_ERROR, "Failed to reallocate memory for output\n");
169
        return DNN_ERROR;
170
    }
171
172

18
    switch (params->bin_op) {
173
3
    case DMBO_SUB:
174
3
        math_binary_not_commutative(sub, params, input, output, operands, input_operand_indexes);
175
3
        return 0;
176
3
    case DMBO_ADD:
177
3
        math_binary_commutative(add, params, input, output, operands, input_operand_indexes);
178
3
        return 0;
179
3
    case DMBO_MUL:
180
3
        math_binary_commutative(mul, params, input, output, operands, input_operand_indexes);
181
3
        return 0;
182
3
    case DMBO_REALDIV:
183
3
        math_binary_not_commutative(realdiv, params, input, output, operands, input_operand_indexes);
184
3
        return 0;
185
3
    case DMBO_MINIMUM:
186
3
        math_binary_commutative(minimum, params, input, output, operands, input_operand_indexes);
187
3
        return 0;
188
3
    case DMBO_FLOORMOD:
189
3
        math_binary_not_commutative(floormod, params, input, output, operands, input_operand_indexes);
190
3
        return 0;
191
    default:
192
        av_log(ctx, AV_LOG_ERROR, "Unmatch math binary operator\n");
193
        return DNN_ERROR;
194
    }
195
}