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
ff_dnn_load_layer_math_unary(Layer * layer,AVIOContext * model_file_context,int file_size,int operands_num)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
ff_dnn_execute_layer_math_unary(DnnOperand * operands,const int32_t * input_operand_indexes,int32_t output_operand_index,const void * parameters,NativeContext * ctx)54 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 const DnnOperand *input = &operands[input_operand_indexes[0]];
58 DnnOperand *output = &operands[output_operand_index];
59 const DnnLayerMathUnaryParams *params = parameters;
60 int dims_count;
61 const float *src;
62 float *dst;
63
64 for (int i = 0; i < 4; ++i)
65 output->dims[i] = input->dims[i];
66
67 output->data_type = input->data_type;
68 output->length = ff_calculate_operand_data_length(output);
69 if (output->length <= 0) {
70 av_log(ctx, AV_LOG_ERROR, "The output data length overflow\n");
71 return AVERROR(EINVAL);
72 }
73 output->data = av_realloc(output->data, output->length);
74 if (!output->data) {
75 av_log(ctx, AV_LOG_ERROR, "Failed to reallocate memory for output\n");
76 return AVERROR(ENOMEM);
77 }
78
79 dims_count = ff_calculate_operand_dims_count(output);
80 src = input->data;
81 dst = output->data;
82
83 switch (params->un_op) {
84 case DMUO_ABS:
85 for (int i = 0; i < dims_count; ++i)
86 dst[i] = FFABS(src[i]);
87 return 0;
88 case DMUO_SIN:
89 for (int i = 0; i < dims_count; ++i)
90 dst[i] = sin(src[i]);
91 return 0;
92 case DMUO_COS:
93 for (int i = 0; i < dims_count; ++i)
94 dst[i] = cos(src[i]);
95 return 0;
96 case DMUO_TAN:
97 for (int i = 0; i < dims_count; ++i)
98 dst[i] = tan(src[i]);
99 return 0;
100 case DMUO_ASIN:
101 for (int i = 0; i < dims_count; ++i)
102 dst[i] = asin(src[i]);
103 return 0;
104 case DMUO_ACOS:
105 for (int i = 0; i < dims_count; ++i)
106 dst[i] = acos(src[i]);
107 return 0;
108 case DMUO_ATAN:
109 for (int i = 0; i < dims_count; ++i)
110 dst[i] = atan(src[i]);
111 return 0;
112 case DMUO_SINH:
113 for (int i = 0; i < dims_count; ++i)
114 dst[i] = sinh(src[i]);
115 return 0;
116 case DMUO_COSH:
117 for (int i = 0; i < dims_count; ++i)
118 dst[i] = cosh(src[i]);
119 return 0;
120 case DMUO_TANH:
121 for (int i = 0; i < dims_count; ++i)
122 dst[i] = tanh(src[i]);
123 return 0;
124 case DMUO_ASINH:
125 for (int i = 0; i < dims_count; ++i)
126 dst[i] = asinh(src[i]);
127 return 0;
128 case DMUO_ACOSH:
129 for (int i = 0; i < dims_count; ++i)
130 dst[i] = acosh(src[i]);
131 return 0;
132 case DMUO_ATANH:
133 for (int i = 0; i < dims_count; ++i)
134 dst[i] = atanh(src[i]);
135 return 0;
136 case DMUO_CEIL:
137 for (int i = 0; i < dims_count; ++i)
138 dst[i] = ceil(src[i]);
139 return 0;
140 case DMUO_FLOOR:
141 for (int i = 0; i < dims_count; ++i)
142 dst[i] = floor(src[i]);
143 return 0;
144 case DMUO_ROUND:
145 for (int i = 0; i < dims_count; ++i)
146 dst[i] = round(src[i]);
147 return 0;
148 case DMUO_EXP:
149 for (int i = 0; i < dims_count; ++i)
150 dst[i] = exp(src[i]);
151 return 0;
152 default:
153 av_log(ctx, AV_LOG_ERROR, "Unmatch math unary operator\n");
154 return AVERROR(EINVAL);
155 }
156 }
157