4 * This file is part of FFmpeg.
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.
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.
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
23 * DNN native backend implementation.
28 #include "dnn_backend_native.h"
29 #include "libavutil/avassert.h"
30 #include "dnn_backend_native_layer_mathunary.h"
32 int ff_dnn_load_layer_math_unary(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num)
34 DnnLayerMathUnaryParams *params;
36 params = av_malloc(sizeof(*params));
40 params->un_op = (int32_t)avio_rl32(model_file_context);
42 layer->params = params;
43 layer->input_operand_indexes[0] = (int32_t)avio_rl32(model_file_context);
44 layer->output_operand_index = (int32_t)avio_rl32(model_file_context);
47 if (layer->input_operand_indexes[0] >= operands_num || layer->output_operand_index >= operands_num) {
55 int ff_dnn_execute_layer_math_unary(DnnOperand *operands, const int32_t *input_operand_indexes,
56 int32_t output_operand_index, const void *parameters, NativeContext *ctx)
58 const DnnOperand *input = &operands[input_operand_indexes[0]];
59 DnnOperand *output = &operands[output_operand_index];
60 const DnnLayerMathUnaryParams *params = parameters;
65 for (int i = 0; i < 4; ++i)
66 output->dims[i] = input->dims[i];
68 output->data_type = input->data_type;
69 output->length = ff_calculate_operand_data_length(output);
70 if (output->length <= 0) {
71 av_log(ctx, AV_LOG_ERROR, "The output data length overflow\n");
74 output->data = av_realloc(output->data, output->length);
76 av_log(ctx, AV_LOG_ERROR, "Failed to reallocate memory for output\n");
80 dims_count = ff_calculate_operand_dims_count(output);
84 switch (params->un_op) {
86 for (int i = 0; i < dims_count; ++i)
87 dst[i] = FFABS(src[i]);
90 for (int i = 0; i < dims_count; ++i)
94 for (int i = 0; i < dims_count; ++i)
98 for (int i = 0; i < dims_count; ++i)
102 for (int i = 0; i < dims_count; ++i)
103 dst[i] = asin(src[i]);
106 for (int i = 0; i < dims_count; ++i)
107 dst[i] = acos(src[i]);
110 for (int i = 0; i < dims_count; ++i)
111 dst[i] = atan(src[i]);
114 for (int i = 0; i < dims_count; ++i)
115 dst[i] = sinh(src[i]);
118 for (int i = 0; i < dims_count; ++i)
119 dst[i] = cosh(src[i]);
122 for (int i = 0; i < dims_count; ++i)
123 dst[i] = tanh(src[i]);
126 for (int i = 0; i < dims_count; ++i)
127 dst[i] = asinh(src[i]);
130 for (int i = 0; i < dims_count; ++i)
131 dst[i] = acosh(src[i]);
134 for (int i = 0; i < dims_count; ++i)
135 dst[i] = atanh(src[i]);
138 for (int i = 0; i < dims_count; ++i)
139 dst[i] = ceil(src[i]);
142 for (int i = 0; i < dims_count; ++i)
143 dst[i] = floor(src[i]);
146 for (int i = 0; i < dims_count; ++i)
147 dst[i] = round(src[i]);
150 for (int i = 0; i < dims_count; ++i)
151 dst[i] = exp(src[i]);
154 av_log(ctx, AV_LOG_ERROR, "Unmatch math unary operator\n");