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 "dnn_backend_native.h"
27 #include "libavutil/avassert.h"
29 
30 int dnn_load_layer_math_unary(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num)
31 {
33  int dnn_size = 0;
34  params = av_malloc(sizeof(*params));
35  if(!params)
36  return 0;
37 
38  params->un_op = (int32_t)avio_rl32(model_file_context);
39  dnn_size += 4;
40  layer->params = params;
41  layer->input_operand_indexes[0] = (int32_t)avio_rl32(model_file_context);
42  layer->output_operand_index = (int32_t)avio_rl32(model_file_context);
43  dnn_size += 8;
44 
45  if (layer->input_operand_indexes[0] >= operands_num || layer->output_operand_index >= operands_num) {
46  return 0;
47  }
48 
49  return dnn_size;
50 
51 }
52 
53 int dnn_execute_layer_math_unary(DnnOperand *operands, const int32_t *input_operand_indexes,
54  int32_t output_operand_index, const void *parameters)
55 {
56  const DnnOperand *input = &operands[input_operand_indexes[0]];
57  DnnOperand *output = &operands[output_operand_index];
58  const DnnLayerMathUnaryParams *params = (const DnnLayerMathUnaryParams *)parameters;
59  int dims_count;
60  const float *src;
61  float *dst;
62 
63  for (int i = 0; i < 4; ++i)
64  output->dims[i] = input->dims[i];
65 
66  output->data_type = input->data_type;
68  if (output->length <= 0)
69  return DNN_ERROR;
70  output->data = av_realloc(output->data, output->length);
71  if (!output->data)
72  return DNN_ERROR;
73 
75  src = input->data;
76  dst = output->data;
77 
78  switch (params->un_op) {
79  case DMUO_ABS:
80  for (int i = 0; i < dims_count; ++i)
81  dst[i] = FFABS(src[i]);
82  return 0;
83  default:
84  return -1;
85  }
86 }
calculate_operand_data_length
int32_t calculate_operand_data_length(const DnnOperand *oprd)
Definition: dnn_backend_native.c:297
dnn_backend_native_layer_mathunary.h
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
DnnLayerMathUnaryParams::un_op
DNNMathUnaryOperation un_op
Definition: dnn_backend_native_layer_mathunary.h:38
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
avassert.h
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
Layer::params
void * params
Definition: dnn_backend_native.h:60
src
#define src
Definition: vp8dsp.c:254
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:747
AVIOContext
Bytestream IO Context.
Definition: avio.h:161
Layer::output_operand_index
int32_t output_operand_index
Definition: dnn_backend_native.h:59
Layer
Definition: dnn_backend_native.h:51
Layer::input_operand_indexes
int32_t input_operand_indexes[4]
a layer can have multiple inputs and one output.
Definition: dnn_backend_native.h:58
calculate_operand_dims_count
int32_t calculate_operand_dims_count(const DnnOperand *oprd)
Definition: dnn_backend_native.c:288
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:37
av_realloc
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:134
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
DNN_ERROR
@ DNN_ERROR
Definition: dnn_interface.h:31
dnn_load_layer_math_unary
int dnn_load_layer_math_unary(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num)
Definition: dnn_backend_native_layer_mathunary.c:30
DnnOperand
Definition: dnn_backend_native.h:63
DMUO_ABS
@ DMUO_ABS
Definition: dnn_backend_native_layer_mathunary.h:33
dnn_execute_layer_math_unary
int dnn_execute_layer_math_unary(DnnOperand *operands, const int32_t *input_operand_indexes, int32_t output_operand_index, const void *parameters)
Definition: dnn_backend_native_layer_mathunary.c:53