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"
30 
31 int ff_dnn_load_layer_math_unary(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num)
32 {
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 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;
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 
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 }
dnn_backend_native_layer_mathunary.h
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
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:54
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:30
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:503
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:180
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)
Load the Unary Math Layer.
Definition: dnn_backend_native_layer_mathunary.c:31
floor
static __device__ float floor(float a)
Definition: cuda_runtime.h:173
ctx
AVFormatContext * ctx
Definition: movenc.c:48
FFABS
#define FFABS(a)
Absolute value, Note, INT_MIN / INT64_MIN result in undefined behavior as they are not representable ...
Definition: common.h:64
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)
Execute the Unary Math Layer.
Definition: dnn_backend_native_layer_mathunary.c:54
Layer::params
void * params
Definition: dnn_backend_native.h:66
av_realloc
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:153
DMUO_EXP
@ DMUO_EXP
Definition: dnn_backend_native_layer_mathunary.h:49
exp
int8_t exp
Definition: eval.c:72
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:761
AVIOContext
Bytestream IO Context.
Definition: avio.h:166
Layer::output_operand_index
int32_t output_operand_index
Definition: dnn_backend_native.h:65
NativeContext
Definition: dnn_backend_native.h:118
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:57
Layer::input_operand_indexes
int32_t input_operand_indexes[4]
a layer can have multiple inputs and one output.
Definition: dnn_backend_native.h:64
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:53
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
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
DMUO_ASIN
@ DMUO_ASIN
Definition: dnn_backend_native_layer_mathunary.h:37
DnnOperand
Definition: dnn_backend_native.h:69
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:494
DMUO_COS
@ DMUO_COS
Definition: dnn_backend_native_layer_mathunary.h:35
src
INIT_CLIP pixel * src
Definition: h264pred_template.c:418
int32_t
int32_t
Definition: audioconvert.c:56
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27