FFmpeg
dnn_backend_native_layer_mathunary.c
Go to the documentation of this file.
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 "libavutil/avassert.h"
31 
32 int ff_dnn_load_layer_math_unary(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num)
33 {
35  int dnn_size = 0;
36  params = av_malloc(sizeof(*params));
37  if(!params)
38  return 0;
39 
40  params->un_op = (int32_t)avio_rl32(model_file_context);
41  dnn_size += 4;
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);
45  dnn_size += 8;
46 
47  if (layer->input_operand_indexes[0] >= operands_num || layer->output_operand_index >= operands_num) {
48  return 0;
49  }
50 
51  return dnn_size;
52 
53 }
54 
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)
57 {
58  const DnnOperand *input = &operands[input_operand_indexes[0]];
59  DnnOperand *output = &operands[output_operand_index];
60  const DnnLayerMathUnaryParams *params = parameters;
61  int dims_count;
62  const float *src;
63  float *dst;
64 
65  for (int i = 0; i < 4; ++i)
66  output->dims[i] = input->dims[i];
67 
68  output->data_type = input->data_type;
70  if (output->length <= 0) {
71  av_log(ctx, AV_LOG_ERROR, "The output data length overflow\n");
72  return DNN_ERROR;
73  }
74  output->data = av_realloc(output->data, output->length);
75  if (!output->data) {
76  av_log(ctx, AV_LOG_ERROR, "Failed to reallocate memory for output\n");
77  return DNN_ERROR;
78  }
79 
81  src = input->data;
82  dst = output->data;
83 
84  switch (params->un_op) {
85  case DMUO_ABS:
86  for (int i = 0; i < dims_count; ++i)
87  dst[i] = FFABS(src[i]);
88  return 0;
89  case DMUO_SIN:
90  for (int i = 0; i < dims_count; ++i)
91  dst[i] = sin(src[i]);
92  return 0;
93  case DMUO_COS:
94  for (int i = 0; i < dims_count; ++i)
95  dst[i] = cos(src[i]);
96  return 0;
97  case DMUO_TAN:
98  for (int i = 0; i < dims_count; ++i)
99  dst[i] = tan(src[i]);
100  return 0;
101  case DMUO_ASIN:
102  for (int i = 0; i < dims_count; ++i)
103  dst[i] = asin(src[i]);
104  return 0;
105  case DMUO_ACOS:
106  for (int i = 0; i < dims_count; ++i)
107  dst[i] = acos(src[i]);
108  return 0;
109  case DMUO_ATAN:
110  for (int i = 0; i < dims_count; ++i)
111  dst[i] = atan(src[i]);
112  return 0;
113  case DMUO_SINH:
114  for (int i = 0; i < dims_count; ++i)
115  dst[i] = sinh(src[i]);
116  return 0;
117  case DMUO_COSH:
118  for (int i = 0; i < dims_count; ++i)
119  dst[i] = cosh(src[i]);
120  return 0;
121  case DMUO_TANH:
122  for (int i = 0; i < dims_count; ++i)
123  dst[i] = tanh(src[i]);
124  return 0;
125  case DMUO_ASINH:
126  for (int i = 0; i < dims_count; ++i)
127  dst[i] = asinh(src[i]);
128  return 0;
129  case DMUO_ACOSH:
130  for (int i = 0; i < dims_count; ++i)
131  dst[i] = acosh(src[i]);
132  return 0;
133  case DMUO_ATANH:
134  for (int i = 0; i < dims_count; ++i)
135  dst[i] = atanh(src[i]);
136  return 0;
137  case DMUO_CEIL:
138  for (int i = 0; i < dims_count; ++i)
139  dst[i] = ceil(src[i]);
140  return 0;
141  case DMUO_FLOOR:
142  for (int i = 0; i < dims_count; ++i)
143  dst[i] = floor(src[i]);
144  return 0;
145  case DMUO_ROUND:
146  for (int i = 0; i < dims_count; ++i)
147  dst[i] = round(src[i]);
148  return 0;
149  default:
150  av_log(ctx, AV_LOG_ERROR, "Unmatch math unary operator\n");
151  return DNN_ERROR;
152  }
153 }
dnn_backend_native_layer_mathunary.h
DMUO_TANH
@ DMUO_TANH
Definition: dnn_backend_native_layer_mathunary.h:42
DMUO_ACOS
@ DMUO_ACOS
Definition: dnn_backend_native_layer_mathunary.h:38
output
filter_frame For filters that do not use the this method is called when a frame is pushed to the filter s input It can be called at any time except in a reentrant way If the input frame is enough to produce output
Definition: filter_design.txt:225
DMUO_ACOSH
@ DMUO_ACOSH
Definition: dnn_backend_native_layer_mathunary.h:44
DnnLayerMathUnaryParams::un_op
DNNMathUnaryOperation un_op
Definition: dnn_backend_native_layer_mathunary.h:53
DMUO_ASINH
@ DMUO_ASINH
Definition: dnn_backend_native_layer_mathunary.h:43
DMUO_CEIL
@ DMUO_CEIL
Definition: dnn_backend_native_layer_mathunary.h:46
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
DMUO_COSH
@ DMUO_COSH
Definition: dnn_backend_native_layer_mathunary.h:41
ff_calculate_operand_data_length
int32_t ff_calculate_operand_data_length(const DnnOperand *oprd)
Definition: dnn_backend_native.c:403
avassert.h
ceil
static __device__ float ceil(float a)
Definition: cuda_runtime.h:176
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
DMUO_SIN
@ DMUO_SIN
Definition: dnn_backend_native_layer_mathunary.h:34
DMUO_TAN
@ DMUO_TAN
Definition: dnn_backend_native_layer_mathunary.h:36
DMUO_ROUND
@ DMUO_ROUND
Definition: dnn_backend_native_layer_mathunary.h:48
ff_dnn_load_layer_math_unary
int ff_dnn_load_layer_math_unary(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num)
Definition: dnn_backend_native_layer_mathunary.c:32
floor
static __device__ float floor(float a)
Definition: cuda_runtime.h:173
ctx
AVFormatContext * ctx
Definition: movenc.c:48
int32_t
int32_t
Definition: audio_convert.c:194
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:72
DMUO_SINH
@ DMUO_SINH
Definition: dnn_backend_native_layer_mathunary.h:40
ff_dnn_execute_layer_math_unary
int ff_dnn_execute_layer_math_unary(DnnOperand *operands, const int32_t *input_operand_indexes, int32_t output_operand_index, const void *parameters, NativeContext *ctx)
Definition: dnn_backend_native_layer_mathunary.c:55
Layer::params
void * params
Definition: dnn_backend_native.h:65
src
#define src
Definition: vp8dsp.c:255
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:750
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
Layer::output_operand_index
int32_t output_operand_index
Definition: dnn_backend_native.h:64
NativeContext
Definition: dnn_backend_native.h:116
DMUO_ATANH
@ DMUO_ATANH
Definition: dnn_backend_native_layer_mathunary.h:45
DMUO_FLOOR
@ DMUO_FLOOR
Definition: dnn_backend_native_layer_mathunary.h:47
Layer
Definition: dnn_backend_native.h:56
Layer::input_operand_indexes
int32_t input_operand_indexes[4]
a layer can have multiple inputs and one output.
Definition: dnn_backend_native.h:63
dnn_backend_native.h
input
and forward the test the status of outputs and forward it to the corresponding return FFERROR_NOT_READY If the filters stores internally one or a few frame for some input
Definition: filter_design.txt:172
DnnLayerMathUnaryParams
Definition: dnn_backend_native_layer_mathunary.h:52
av_realloc
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:134
i
int i
Definition: input.c:407
round
static av_always_inline av_const double round(double x)
Definition: libm.h:444
DMUO_ATAN
@ DMUO_ATAN
Definition: dnn_backend_native_layer_mathunary.h:39
DNN_ERROR
@ DNN_ERROR
Definition: dnn_interface.h:33
DMUO_ASIN
@ DMUO_ASIN
Definition: dnn_backend_native_layer_mathunary.h:37
DnnOperand
Definition: dnn_backend_native.h:68
DMUO_ABS
@ DMUO_ABS
Definition: dnn_backend_native_layer_mathunary.h:33
ff_calculate_operand_dims_count
int32_t ff_calculate_operand_dims_count(const DnnOperand *oprd)
Definition: dnn_backend_native.c:394
DMUO_COS
@ DMUO_COS
Definition: dnn_backend_native_layer_mathunary.h:35
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28