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: 2020-10-23 17:01:47 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
FunType pfun;
32
33
18
static float sub(float src0, float src1)
34
{
35
18
    return src0 - src1;
36
}
37
18
static float add(float src0, float src1)
38
{
39
18
    return src0 + src1;
40
}
41
18
static float mul(float src0, float src1)
42
{
43
18
    return src0 * src1;
44
}
45
18
static float realdiv(float src0, float src1)
46
{
47
18
    return src0 / src1;
48
}
49
18
static float minimum(float src0, float src1)
50
{
51
18
    return FFMIN(src0, src1);
52
}
53
18
static float floormod(float src0, float src1)
54
{
55
18
    return (float)((int)(src0) % (int)(src1));
56
}
57
58
9
static void math_binary_commutative(FunType pfun, const DnnLayerMathBinaryParams *params, const DnnOperand *input, DnnOperand *output, DnnOperand *operands, const int32_t *input_operand_indexes)
59
{
60
    int dims_count;
61
    const float *src;
62
    float *dst;
63
9
    dims_count = calculate_operand_dims_count(output);
64
9
    src = input->data;
65
9
    dst = output->data;
66

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

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