FFmpeg  4.3
dnn_backend_native_layer_mathbinary.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_binary(Layer *layer, AVIOContext *model_file_context, int file_size)
31 {
33  int dnn_size = 0;
34  int input_index = 0;
35  params = av_malloc(sizeof(*params));
36  if (!params)
37  return 0;
38 
39  params->bin_op = (int32_t)avio_rl32(model_file_context);
40  dnn_size += 4;
41 
42  params->input0_broadcast = (int32_t)avio_rl32(model_file_context);
43  dnn_size += 4;
44  if (params->input0_broadcast) {
45  params->v = av_int2float(avio_rl32(model_file_context));
46  } else {
47  layer->input_operand_indexes[input_index] = (int32_t)avio_rl32(model_file_context);
48  input_index++;
49  }
50  dnn_size += 4;
51 
52  params->input1_broadcast = (int32_t)avio_rl32(model_file_context);
53  dnn_size += 4;
54  if (params->input1_broadcast) {
55  params->v = av_int2float(avio_rl32(model_file_context));
56  } else {
57  layer->input_operand_indexes[input_index] = (int32_t)avio_rl32(model_file_context);
58  input_index++;
59  }
60  dnn_size += 4;
61 
62  layer->output_operand_index = (int32_t)avio_rl32(model_file_context);
63  dnn_size += 4;
64  layer->params = params;
65 
66  return dnn_size;
67 }
68 
69 int dnn_execute_layer_math_binary(DnnOperand *operands, const int32_t *input_operand_indexes,
70  int32_t output_operand_index, const void *parameters)
71 {
72  const DnnOperand *input = &operands[input_operand_indexes[0]];
73  DnnOperand *output = &operands[output_operand_index];
74  const DnnLayerMathBinaryParams *params = (const DnnLayerMathBinaryParams *)parameters;
75  int dims_count;
76  const float *src;
77  float *dst;
78 
79  for (int i = 0; i < 4; ++i)
80  output->dims[i] = input->dims[i];
81 
82  output->data_type = input->data_type;
84  output->data = av_realloc(output->data, output->length);
85  if (!output->data)
86  return DNN_ERROR;
87 
89  src = input->data;
90  dst = output->data;
91 
92  switch (params->bin_op) {
93  case DMBO_SUB:
94  if (params->input0_broadcast) {
95  for (int i = 0; i < dims_count; ++i) {
96  dst[i] = params->v - src[i];
97  }
98  } else if (params->input1_broadcast) {
99  for (int i = 0; i < dims_count; ++i) {
100  dst[i] = src[i] - params->v;
101  }
102  } else {
103  const DnnOperand *input1 = &operands[input_operand_indexes[1]];
104  const float *src1 = input1->data;
105  for (int i = 0; i < dims_count; ++i) {
106  dst[i] = src[i] - src1[i];
107  }
108  }
109  return 0;
110  case DMBO_ADD:
111  if (params->input0_broadcast || params->input1_broadcast) {
112  for (int i = 0; i < dims_count; ++i) {
113  dst[i] = params->v + src[i];
114  }
115  } else {
116  const DnnOperand *input1 = &operands[input_operand_indexes[1]];
117  const float *src1 = input1->data;
118  for (int i = 0; i < dims_count; ++i) {
119  dst[i] = src[i] + src1[i];
120  }
121  }
122  return 0;
123  case DMBO_MUL:
124  if (params->input0_broadcast || params->input1_broadcast) {
125  for (int i = 0; i < dims_count; ++i) {
126  dst[i] = params->v * src[i];
127  }
128  } else {
129  const DnnOperand *input1 = &operands[input_operand_indexes[1]];
130  const float *src1 = input1->data;
131  for (int i = 0; i < dims_count; ++i) {
132  dst[i] = src[i] * src1[i];
133  }
134  }
135  return 0;
136  case DMBO_REALDIV:
137  if (params->input0_broadcast) {
138  for (int i = 0; i < dims_count; ++i) {
139  dst[i] = params->v / src[i];
140  }
141  } else if (params->input1_broadcast) {
142  for (int i = 0; i < dims_count; ++i) {
143  dst[i] = src[i] / params->v;
144  }
145  } else {
146  const DnnOperand *input1 = &operands[input_operand_indexes[1]];
147  const float *src1 = input1->data;
148  for (int i = 0; i < dims_count; ++i) {
149  dst[i] = src[i] / src1[i];
150  }
151  }
152  return 0;
153  case DMBO_MINIMUM:
154  if (params->input0_broadcast || params->input1_broadcast) {
155  for (int i = 0; i < dims_count; ++i) {
156  dst[i] = FFMIN(params->v, src[i]);
157  }
158  } else {
159  const DnnOperand *input1 = &operands[input_operand_indexes[1]];
160  const float *src1 = input1->data;
161  for (int i = 0; i < dims_count; ++i) {
162  dst[i] = FFMIN(src[i], src1[i]);
163  }
164  }
165  return 0;
166  default:
167  return -1;
168  }
169 }
calculate_operand_data_length
int32_t calculate_operand_data_length(const DnnOperand *oprd)
Definition: dnn_backend_native.c:303
DnnLayerMathBinaryParams::input0_broadcast
int input0_broadcast
Definition: dnn_backend_native_layer_mathbinary.h:44
av_realloc
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:135
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
DnnLayerMathBinaryParams
Definition: dnn_backend_native_layer_mathbinary.h:42
DnnLayerMathBinaryParams::v
float v
Definition: dnn_backend_native_layer_mathbinary.h:46
dnn_load_layer_math_binary
int dnn_load_layer_math_binary(Layer *layer, AVIOContext *model_file_context, int file_size)
Definition: dnn_backend_native_layer_mathbinary.c:30
DnnLayerMathBinaryParams::input1_broadcast
int input1_broadcast
Definition: dnn_backend_native_layer_mathbinary.h:45
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
dnn_backend_native_layer_mathbinary.h
av_int2float
static av_always_inline float av_int2float(uint32_t i)
Reinterpret a 32-bit integer as a float.
Definition: intfloat.h:40
avassert.h
dnn_execute_layer_math_binary
int dnn_execute_layer_math_binary(DnnOperand *operands, const int32_t *input_operand_indexes, int32_t output_operand_index, const void *parameters)
Definition: dnn_backend_native_layer_mathbinary.c:69
DnnOperand::data
void * data
data pointer with data length in bytes.
Definition: dnn_backend_native.h:98
int32_t
int32_t
Definition: audio_convert.c:194
Layer::params
void * params
Definition: dnn_backend_native.h:60
src
#define src
Definition: vp8dsp.c:254
DMBO_MUL
@ DMBO_MUL
Definition: dnn_backend_native_layer_mathbinary.h:36
DMBO_MINIMUM
@ DMBO_MINIMUM
Definition: dnn_backend_native_layer_mathbinary.h:38
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
DMBO_ADD
@ DMBO_ADD
Definition: dnn_backend_native_layer_mathbinary.h:35
calculate_operand_dims_count
int32_t calculate_operand_dims_count(const DnnOperand *oprd)
Definition: dnn_backend_native.c:294
FFMIN
#define FFMIN(a, b)
Definition: common.h:96
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
src1
#define src1
Definition: h264pred.c:139
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
DNN_ERROR
@ DNN_ERROR
Definition: dnn_interface.h:31
DMBO_SUB
@ DMBO_SUB
Definition: dnn_backend_native_layer_mathbinary.h:34
DnnOperand
Definition: dnn_backend_native.h:63
DMBO_REALDIV
@ DMBO_REALDIV
Definition: dnn_backend_native_layer_mathbinary.h:37
DnnLayerMathBinaryParams::bin_op
DNNMathBinaryOperation bin_op
Definition: dnn_backend_native_layer_mathbinary.h:43