FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavfilter/dnn/dnn_backend_native_layer_mathunary.c
Date: 2022-11-26 13:19:19
Exec Total Coverage
Lines: 83 104 79.8%
Branches: 55 64 85.9%

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 <math.h>
27
28 #include "dnn_backend_native.h"
29 #include "dnn_backend_native_layer_mathunary.h"
30
31 int ff_dnn_load_layer_math_unary(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num)
32 {
33 DnnLayerMathUnaryParams *params;
34 int dnn_size = 0;
35 params = av_malloc(sizeof(*params));
36 if(!params)
37 return 0;
38
39 params->un_op = (int32_t)avio_rl32(model_file_context);
40 dnn_size += 4;
41 layer->params = params;
42 layer->input_operand_indexes[0] = (int32_t)avio_rl32(model_file_context);
43 layer->output_operand_index = (int32_t)avio_rl32(model_file_context);
44 dnn_size += 8;
45
46 if (layer->input_operand_indexes[0] >= operands_num || layer->output_operand_index >= operands_num) {
47 return 0;
48 }
49
50 return dnn_size;
51
52 }
53
54 17 int ff_dnn_execute_layer_math_unary(DnnOperand *operands, const int32_t *input_operand_indexes,
55 int32_t output_operand_index, const void *parameters, NativeContext *ctx)
56 {
57 17 const DnnOperand *input = &operands[input_operand_indexes[0]];
58 17 DnnOperand *output = &operands[output_operand_index];
59 17 const DnnLayerMathUnaryParams *params = parameters;
60 int dims_count;
61 const float *src;
62 float *dst;
63
64
2/2
✓ Branch 0 taken 68 times.
✓ Branch 1 taken 17 times.
85 for (int i = 0; i < 4; ++i)
65 68 output->dims[i] = input->dims[i];
66
67 17 output->data_type = input->data_type;
68 17 output->length = ff_calculate_operand_data_length(output);
69
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (output->length <= 0) {
70 av_log(ctx, AV_LOG_ERROR, "The output data length overflow\n");
71 return AVERROR(EINVAL);
72 }
73 17 output->data = av_realloc(output->data, output->length);
74
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 17 times.
17 if (!output->data) {
75 av_log(ctx, AV_LOG_ERROR, "Failed to reallocate memory for output\n");
76 return AVERROR(ENOMEM);
77 }
78
79 17 dims_count = ff_calculate_operand_dims_count(output);
80 17 src = input->data;
81 17 dst = output->data;
82
83
17/18
✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
✓ Branch 2 taken 1 times.
✓ Branch 3 taken 1 times.
✓ Branch 4 taken 1 times.
✓ Branch 5 taken 1 times.
✓ Branch 6 taken 1 times.
✓ Branch 7 taken 1 times.
✓ Branch 8 taken 1 times.
✓ Branch 9 taken 1 times.
✓ Branch 10 taken 1 times.
✓ Branch 11 taken 1 times.
✓ Branch 12 taken 1 times.
✓ Branch 13 taken 1 times.
✓ Branch 14 taken 1 times.
✓ Branch 15 taken 1 times.
✓ Branch 16 taken 1 times.
✗ Branch 17 not taken.
17 switch (params->un_op) {
84 1 case DMUO_ABS:
85
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
10 for (int i = 0; i < dims_count; ++i)
86 9 dst[i] = FFABS(src[i]);
87 1 return 0;
88 1 case DMUO_SIN:
89
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
10 for (int i = 0; i < dims_count; ++i)
90 9 dst[i] = sin(src[i]);
91 1 return 0;
92 1 case DMUO_COS:
93
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
10 for (int i = 0; i < dims_count; ++i)
94 9 dst[i] = cos(src[i]);
95 1 return 0;
96 1 case DMUO_TAN:
97
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
10 for (int i = 0; i < dims_count; ++i)
98 9 dst[i] = tan(src[i]);
99 1 return 0;
100 1 case DMUO_ASIN:
101
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
10 for (int i = 0; i < dims_count; ++i)
102 9 dst[i] = asin(src[i]);
103 1 return 0;
104 1 case DMUO_ACOS:
105
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
10 for (int i = 0; i < dims_count; ++i)
106 9 dst[i] = acos(src[i]);
107 1 return 0;
108 1 case DMUO_ATAN:
109
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
10 for (int i = 0; i < dims_count; ++i)
110 9 dst[i] = atan(src[i]);
111 1 return 0;
112 1 case DMUO_SINH:
113
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
10 for (int i = 0; i < dims_count; ++i)
114 9 dst[i] = sinh(src[i]);
115 1 return 0;
116 1 case DMUO_COSH:
117
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
10 for (int i = 0; i < dims_count; ++i)
118 9 dst[i] = cosh(src[i]);
119 1 return 0;
120 1 case DMUO_TANH:
121
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
10 for (int i = 0; i < dims_count; ++i)
122 9 dst[i] = tanh(src[i]);
123 1 return 0;
124 1 case DMUO_ASINH:
125
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
10 for (int i = 0; i < dims_count; ++i)
126 9 dst[i] = asinh(src[i]);
127 1 return 0;
128 1 case DMUO_ACOSH:
129
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
10 for (int i = 0; i < dims_count; ++i)
130 9 dst[i] = acosh(src[i]);
131 1 return 0;
132 1 case DMUO_ATANH:
133
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
10 for (int i = 0; i < dims_count; ++i)
134 9 dst[i] = atanh(src[i]);
135 1 return 0;
136 1 case DMUO_CEIL:
137
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
10 for (int i = 0; i < dims_count; ++i)
138 9 dst[i] = ceil(src[i]);
139 1 return 0;
140 1 case DMUO_FLOOR:
141
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
10 for (int i = 0; i < dims_count; ++i)
142 9 dst[i] = floor(src[i]);
143 1 return 0;
144 1 case DMUO_ROUND:
145
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
10 for (int i = 0; i < dims_count; ++i)
146 9 dst[i] = round(src[i]);
147 1 return 0;
148 1 case DMUO_EXP:
149
2/2
✓ Branch 0 taken 9 times.
✓ Branch 1 taken 1 times.
10 for (int i = 0; i < dims_count; ++i)
150 9 dst[i] = exp(src[i]);
151 1 return 0;
152 default:
153 av_log(ctx, AV_LOG_ERROR, "Unmatch math unary operator\n");
154 return AVERROR(EINVAL);
155 }
156 }
157