FFmpeg
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, int operands_num)
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  if (layer->input_operand_indexes[input_index] >= operands_num) {
49  return 0;
50  }
51  input_index++;
52  }
53  dnn_size += 4;
54 
55  params->input1_broadcast = (int32_t)avio_rl32(model_file_context);
56  dnn_size += 4;
57  if (params->input1_broadcast) {
58  params->v = av_int2float(avio_rl32(model_file_context));
59  } else {
60  layer->input_operand_indexes[input_index] = (int32_t)avio_rl32(model_file_context);
61  if (layer->input_operand_indexes[input_index] >= operands_num) {
62  return 0;
63  }
64  input_index++;
65  }
66  dnn_size += 4;
67 
68  layer->output_operand_index = (int32_t)avio_rl32(model_file_context);
69  dnn_size += 4;
70  layer->params = params;
71 
72  if (layer->output_operand_index >= operands_num) {
73  return 0;
74  }
75 
76  return dnn_size;
77 }
78 
79 int dnn_execute_layer_math_binary(DnnOperand *operands, const int32_t *input_operand_indexes,
80  int32_t output_operand_index, const void *parameters)
81 {
82  const DnnOperand *input = &operands[input_operand_indexes[0]];
83  DnnOperand *output = &operands[output_operand_index];
84  const DnnLayerMathBinaryParams *params = (const DnnLayerMathBinaryParams *)parameters;
85  int dims_count;
86  const float *src;
87  float *dst;
88 
89  for (int i = 0; i < 4; ++i)
90  output->dims[i] = input->dims[i];
91 
92  output->data_type = input->data_type;
94  if (output->length <= 0)
95  return DNN_ERROR;
96  output->data = av_realloc(output->data, output->length);
97  if (!output->data)
98  return DNN_ERROR;
99 
100  dims_count = calculate_operand_dims_count(output);
101  src = input->data;
102  dst = output->data;
103 
104  switch (params->bin_op) {
105  case DMBO_SUB:
106  if (params->input0_broadcast) {
107  for (int i = 0; i < dims_count; ++i) {
108  dst[i] = params->v - src[i];
109  }
110  } else if (params->input1_broadcast) {
111  for (int i = 0; i < dims_count; ++i) {
112  dst[i] = src[i] - params->v;
113  }
114  } else {
115  const DnnOperand *input1 = &operands[input_operand_indexes[1]];
116  const float *src1 = input1->data;
117  for (int i = 0; i < dims_count; ++i) {
118  dst[i] = src[i] - src1[i];
119  }
120  }
121  return 0;
122  case DMBO_ADD:
123  if (params->input0_broadcast || params->input1_broadcast) {
124  for (int i = 0; i < dims_count; ++i) {
125  dst[i] = params->v + src[i];
126  }
127  } else {
128  const DnnOperand *input1 = &operands[input_operand_indexes[1]];
129  const float *src1 = input1->data;
130  for (int i = 0; i < dims_count; ++i) {
131  dst[i] = src[i] + src1[i];
132  }
133  }
134  return 0;
135  case DMBO_MUL:
136  if (params->input0_broadcast || params->input1_broadcast) {
137  for (int i = 0; i < dims_count; ++i) {
138  dst[i] = params->v * src[i];
139  }
140  } else {
141  const DnnOperand *input1 = &operands[input_operand_indexes[1]];
142  const float *src1 = input1->data;
143  for (int i = 0; i < dims_count; ++i) {
144  dst[i] = src[i] * src1[i];
145  }
146  }
147  return 0;
148  case DMBO_REALDIV:
149  if (params->input0_broadcast) {
150  for (int i = 0; i < dims_count; ++i) {
151  dst[i] = params->v / src[i];
152  }
153  } else if (params->input1_broadcast) {
154  for (int i = 0; i < dims_count; ++i) {
155  dst[i] = src[i] / params->v;
156  }
157  } else {
158  const DnnOperand *input1 = &operands[input_operand_indexes[1]];
159  const float *src1 = input1->data;
160  for (int i = 0; i < dims_count; ++i) {
161  dst[i] = src[i] / src1[i];
162  }
163  }
164  return 0;
165  case DMBO_MINIMUM:
166  if (params->input0_broadcast || params->input1_broadcast) {
167  for (int i = 0; i < dims_count; ++i) {
168  dst[i] = FFMIN(params->v, src[i]);
169  }
170  } else {
171  const DnnOperand *input1 = &operands[input_operand_indexes[1]];
172  const float *src1 = input1->data;
173  for (int i = 0; i < dims_count; ++i) {
174  dst[i] = FFMIN(src[i], src1[i]);
175  }
176  }
177  return 0;
178  default:
179  return -1;
180  }
181 }
calculate_operand_data_length
int32_t calculate_operand_data_length(const DnnOperand *oprd)
Definition: dnn_backend_native.c:297
DnnLayerMathBinaryParams::input0_broadcast
int input0_broadcast
Definition: dnn_backend_native_layer_mathbinary.h:44
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
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_load_layer_math_binary
int dnn_load_layer_math_binary(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num)
Definition: dnn_backend_native_layer_mathbinary.c:30
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:79
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:288
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
av_realloc
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:134
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