FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/dnn/dnn_backend_native_layer_mathbinary.c
Date: 2022-12-06 04:36:11
Exec Total Coverage
Lines: 69 108 63.9%
Functions: 9 10 90.0%
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 "dnn_backend_native_layer_mathbinary.h"
28
29 typedef float (*FunType)(float src0, float src1);
30
31 18 static float sub(float src0, float src1)
32 {
33 18 return src0 - src1;
34 }
35 18 static float add(float src0, float src1)
36 {
37 18 return src0 + src1;
38 }
39 18 static float mul(float src0, float src1)
40 {
41 18 return src0 * src1;
42 }
43 18 static float realdiv(float src0, float src1)
44 {
45 18 return src0 / src1;
46 }
47 18 static float minimum(float src0, float src1)
48 {
49
2/2
✓ Branch 0 taken 11 times.
✓ Branch 1 taken 7 times.
18 return FFMIN(src0, src1);
50 }
51 18 static float floormod(float src0, float src1)
52 {
53 18 return (float)((int)(src0) % (int)(src1));
54 }
55
56 9 static void math_binary_commutative(FunType pfun, const DnnLayerMathBinaryParams *params, const DnnOperand *input, DnnOperand *output, DnnOperand *operands, const int32_t *input_operand_indexes)
57 {
58 int dims_count;
59 const float *src;
60 float *dst;
61 9 dims_count = ff_calculate_operand_dims_count(output);
62 9 src = input->data;
63 9 dst = output->data;
64
4/4
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 3 times.
9 if (params->input0_broadcast || params->input1_broadcast) {
65
2/2
✓ Branch 0 taken 36 times.
✓ Branch 1 taken 6 times.
42 for (int i = 0; i < dims_count; ++i) {
66 36 dst[i] = pfun(params->v, src[i]);
67 }
68 } else {
69 3 const DnnOperand *input1 = &operands[input_operand_indexes[1]];
70 3 const float *src1 = input1->data;
71
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 3 times.
21 for (int i = 0; i < dims_count; ++i) {
72 18 dst[i] = pfun(src[i], src1[i]);
73 }
74 }
75 9 }
76 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)
77 {
78 int dims_count;
79 const float *src;
80 float *dst;
81 9 dims_count = ff_calculate_operand_dims_count(output);
82 9 src = input->data;
83 9 dst = output->data;
84
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 6 times.
9 if (params->input0_broadcast) {
85
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 3 times.
21 for (int i = 0; i < dims_count; ++i) {
86 18 dst[i] = pfun(params->v, src[i]);
87 }
88
2/2
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
6 } else if (params->input1_broadcast) {
89
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 3 times.
21 for (int i = 0; i < dims_count; ++i) {
90 18 dst[i] = pfun(src[i], params->v);
91 }
92 } else {
93 3 const DnnOperand *input1 = &operands[input_operand_indexes[1]];
94 3 const float *src1 = input1->data;
95
2/2
✓ Branch 0 taken 18 times.
✓ Branch 1 taken 3 times.
21 for (int i = 0; i < dims_count; ++i) {
96 18 dst[i] = pfun(src[i], src1[i]);
97 }
98 }
99 9 }
100 int ff_dnn_load_layer_math_binary(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num)
101 {
102 DnnLayerMathBinaryParams params = { 0 };
103 int dnn_size = 0;
104 int input_index = 0;
105
106 params.bin_op = (int32_t)avio_rl32(model_file_context);
107 dnn_size += 4;
108
109 params.input0_broadcast = (int32_t)avio_rl32(model_file_context);
110 dnn_size += 4;
111 if (params.input0_broadcast) {
112 params.v = av_int2float(avio_rl32(model_file_context));
113 } else {
114 layer->input_operand_indexes[input_index] = (int32_t)avio_rl32(model_file_context);
115 if (layer->input_operand_indexes[input_index] >= operands_num) {
116 return 0;
117 }
118 input_index++;
119 }
120 dnn_size += 4;
121
122 params.input1_broadcast = (int32_t)avio_rl32(model_file_context);
123 dnn_size += 4;
124 if (params.input1_broadcast) {
125 params.v = av_int2float(avio_rl32(model_file_context));
126 } else {
127 layer->input_operand_indexes[input_index] = (int32_t)avio_rl32(model_file_context);
128 if (layer->input_operand_indexes[input_index] >= operands_num) {
129 return 0;
130 }
131 input_index++;
132 }
133 dnn_size += 4;
134
135 layer->output_operand_index = (int32_t)avio_rl32(model_file_context);
136 dnn_size += 4;
137
138 if (layer->output_operand_index >= operands_num) {
139 return 0;
140 }
141 layer->params = av_memdup(&params, sizeof(params));
142 if (!layer->params)
143 return 0;
144
145 return dnn_size;
146 }
147
148 18 int ff_dnn_execute_layer_math_binary(DnnOperand *operands, const int32_t *input_operand_indexes,
149 int32_t output_operand_index, const void *parameters, NativeContext *ctx)
150 {
151 18 const DnnOperand *input = &operands[input_operand_indexes[0]];
152 18 DnnOperand *output = &operands[output_operand_index];
153 18 const DnnLayerMathBinaryParams *params = parameters;
154
155
2/2
✓ Branch 0 taken 72 times.
✓ Branch 1 taken 18 times.
90 for (int i = 0; i < 4; ++i)
156 72 output->dims[i] = input->dims[i];
157
158 18 output->data_type = input->data_type;
159 18 output->length = ff_calculate_operand_data_length(output);
160
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (output->length <= 0) {
161 av_log(ctx, AV_LOG_ERROR, "The output data length overflow\n");
162 return AVERROR(EINVAL);
163 }
164 18 output->data = av_realloc(output->data, output->length);
165
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
18 if (!output->data) {
166 av_log(ctx, AV_LOG_ERROR, "Failed to reallocate memory for output\n");
167 return AVERROR(ENOMEM);
168 }
169
170
6/7
✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 3 times.
✓ Branch 5 taken 3 times.
✗ Branch 6 not taken.
18 switch (params->bin_op) {
171 3 case DMBO_SUB:
172 3 math_binary_not_commutative(sub, params, input, output, operands, input_operand_indexes);
173 3 return 0;
174 3 case DMBO_ADD:
175 3 math_binary_commutative(add, params, input, output, operands, input_operand_indexes);
176 3 return 0;
177 3 case DMBO_MUL:
178 3 math_binary_commutative(mul, params, input, output, operands, input_operand_indexes);
179 3 return 0;
180 3 case DMBO_REALDIV:
181 3 math_binary_not_commutative(realdiv, params, input, output, operands, input_operand_indexes);
182 3 return 0;
183 3 case DMBO_MINIMUM:
184 3 math_binary_commutative(minimum, params, input, output, operands, input_operand_indexes);
185 3 return 0;
186 3 case DMBO_FLOORMOD:
187 3 math_binary_not_commutative(floormod, params, input, output, operands, input_operand_indexes);
188 3 return 0;
189 default:
190 av_log(ctx, AV_LOG_ERROR, "Unmatch math binary operator\n");
191 return AVERROR(EINVAL);
192 }
193 }
194