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 typedef float (*FunType)(float src0, float src1);
31 
32 static float sub(float src0, float src1)
33 {
34  return src0 - src1;
35 }
36 static float add(float src0, float src1)
37 {
38  return src0 + src1;
39 }
40 static float mul(float src0, float src1)
41 {
42  return src0 * src1;
43 }
44 static float realdiv(float src0, float src1)
45 {
46  return src0 / src1;
47 }
48 static float minimum(float src0, float src1)
49 {
50  return FFMIN(src0, src1);
51 }
52 static float floormod(float src0, float src1)
53 {
54  return (float)((int)(src0) % (int)(src1));
55 }
56 
57 static void math_binary_commutative(FunType pfun, const DnnLayerMathBinaryParams *params, const DnnOperand *input, DnnOperand *output, DnnOperand *operands, const int32_t *input_operand_indexes)
58 {
59  int dims_count;
60  const float *src;
61  float *dst;
63  src = input->data;
64  dst = output->data;
65  if (params->input0_broadcast || params->input1_broadcast) {
66  for (int i = 0; i < dims_count; ++i) {
67  dst[i] = pfun(params->v, src[i]);
68  }
69  } else {
70  const DnnOperand *input1 = &operands[input_operand_indexes[1]];
71  const float *src1 = input1->data;
72  for (int i = 0; i < dims_count; ++i) {
73  dst[i] = pfun(src[i], src1[i]);
74  }
75  }
76 }
77 static void math_binary_not_commutative(FunType pfun, const DnnLayerMathBinaryParams *params, const DnnOperand *input, DnnOperand *output, DnnOperand *operands, const int32_t *input_operand_indexes)
78 {
79  int dims_count;
80  const float *src;
81  float *dst;
83  src = input->data;
84  dst = output->data;
85  if (params->input0_broadcast) {
86  for (int i = 0; i < dims_count; ++i) {
87  dst[i] = pfun(params->v, src[i]);
88  }
89  } else if (params->input1_broadcast) {
90  for (int i = 0; i < dims_count; ++i) {
91  dst[i] = pfun(src[i], params->v);
92  }
93  } else {
94  const DnnOperand *input1 = &operands[input_operand_indexes[1]];
95  const float *src1 = input1->data;
96  for (int i = 0; i < dims_count; ++i) {
97  dst[i] = pfun(src[i], src1[i]);
98  }
99  }
100 }
101 int ff_dnn_load_layer_math_binary(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num)
102 {
103  DnnLayerMathBinaryParams params = { 0 };
104  int dnn_size = 0;
105  int input_index = 0;
106 
107  params.bin_op = (int32_t)avio_rl32(model_file_context);
108  dnn_size += 4;
109 
110  params.input0_broadcast = (int32_t)avio_rl32(model_file_context);
111  dnn_size += 4;
112  if (params.input0_broadcast) {
113  params.v = av_int2float(avio_rl32(model_file_context));
114  } else {
115  layer->input_operand_indexes[input_index] = (int32_t)avio_rl32(model_file_context);
116  if (layer->input_operand_indexes[input_index] >= operands_num) {
117  return 0;
118  }
119  input_index++;
120  }
121  dnn_size += 4;
122 
123  params.input1_broadcast = (int32_t)avio_rl32(model_file_context);
124  dnn_size += 4;
125  if (params.input1_broadcast) {
126  params.v = av_int2float(avio_rl32(model_file_context));
127  } else {
128  layer->input_operand_indexes[input_index] = (int32_t)avio_rl32(model_file_context);
129  if (layer->input_operand_indexes[input_index] >= operands_num) {
130  return 0;
131  }
132  input_index++;
133  }
134  dnn_size += 4;
135 
136  layer->output_operand_index = (int32_t)avio_rl32(model_file_context);
137  dnn_size += 4;
138 
139  if (layer->output_operand_index >= operands_num) {
140  return 0;
141  }
142  layer->params = av_memdup(&params, sizeof(params));
143  if (!layer->params)
144  return 0;
145 
146  return dnn_size;
147 }
148 
149 int ff_dnn_execute_layer_math_binary(DnnOperand *operands, const int32_t *input_operand_indexes,
150  int32_t output_operand_index, const void *parameters, NativeContext *ctx)
151 {
152  const DnnOperand *input = &operands[input_operand_indexes[0]];
153  DnnOperand *output = &operands[output_operand_index];
154  const DnnLayerMathBinaryParams *params = parameters;
155 
156  for (int i = 0; i < 4; ++i)
157  output->dims[i] = input->dims[i];
158 
159  output->data_type = input->data_type;
161  if (output->length <= 0) {
162  av_log(ctx, AV_LOG_ERROR, "The output data length overflow\n");
163  return DNN_ERROR;
164  }
165  output->data = av_realloc(output->data, output->length);
166  if (!output->data) {
167  av_log(ctx, AV_LOG_ERROR, "Failed to reallocate memory for output\n");
168  return DNN_ERROR;
169  }
170 
171  switch (params->bin_op) {
172  case DMBO_SUB:
173  math_binary_not_commutative(sub, params, input, output, operands, input_operand_indexes);
174  return 0;
175  case DMBO_ADD:
176  math_binary_commutative(add, params, input, output, operands, input_operand_indexes);
177  return 0;
178  case DMBO_MUL:
179  math_binary_commutative(mul, params, input, output, operands, input_operand_indexes);
180  return 0;
181  case DMBO_REALDIV:
182  math_binary_not_commutative(realdiv, params, input, output, operands, input_operand_indexes);
183  return 0;
184  case DMBO_MINIMUM:
185  math_binary_commutative(minimum, params, input, output, operands, input_operand_indexes);
186  return 0;
187  case DMBO_FLOORMOD:
188  math_binary_not_commutative(floormod, params, input, output, operands, input_operand_indexes);
189  return 0;
190  default:
191  av_log(ctx, AV_LOG_ERROR, "Unmatch math binary operator\n");
192  return DNN_ERROR;
193  }
194 }
DnnLayerMathBinaryParams::input0_broadcast
int input0_broadcast
Definition: dnn_backend_native_layer_mathbinary.h:45
sub
static float sub(float src0, float src1)
Definition: dnn_backend_native_layer_mathbinary.c:32
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:43
DnnLayerMathBinaryParams::v
float v
Definition: dnn_backend_native_layer_mathbinary.h:47
DnnLayerMathBinaryParams::input1_broadcast
int input1_broadcast
Definition: dnn_backend_native_layer_mathbinary.h:46
floormod
static float floormod(float src0, float src1)
Definition: dnn_backend_native_layer_mathbinary.c:52
av_memdup
void * av_memdup(const void *p, size_t size)
Duplicate a buffer with av_malloc().
Definition: mem.c:285
dnn_backend_native_layer_mathbinary.h
realdiv
static float realdiv(float src0, float src1)
Definition: dnn_backend_native_layer_mathbinary.c:44
av_int2float
static av_always_inline float av_int2float(uint32_t i)
Reinterpret a 32-bit integer as a float.
Definition: intfloat.h:40
ff_calculate_operand_data_length
int32_t ff_calculate_operand_data_length(const DnnOperand *oprd)
Definition: dnn_backend_native.c:403
avassert.h
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
DnnOperand::data
void * data
data pointer with data length in bytes.
Definition: dnn_backend_native.h:103
ctx
AVFormatContext * ctx
Definition: movenc.c:48
math_binary_not_commutative
static void math_binary_not_commutative(FunType pfun, const DnnLayerMathBinaryParams *params, const DnnOperand *input, DnnOperand *output, DnnOperand *operands, const int32_t *input_operand_indexes)
Definition: dnn_backend_native_layer_mathbinary.c:77
mul
static float mul(float src0, float src1)
Definition: dnn_backend_native_layer_mathbinary.c:40
int32_t
int32_t
Definition: audio_convert.c:194
Layer::params
void * params
Definition: dnn_backend_native.h:65
src
#define src
Definition: vp8dsp.c:255
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:750
minimum
static float minimum(float src0, float src1)
Definition: dnn_backend_native_layer_mathbinary.c:48
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
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
DMBO_ADD
@ DMBO_ADD
Definition: dnn_backend_native_layer_mathbinary.h:35
DMBO_FLOORMOD
@ DMBO_FLOORMOD
Definition: dnn_backend_native_layer_mathbinary.h:39
FFMIN
#define FFMIN(a, b)
Definition: common.h:105
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
src0
#define src0
Definition: h264pred.c:139
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:140
ff_dnn_load_layer_math_binary
int ff_dnn_load_layer_math_binary(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num)
Definition: dnn_backend_native_layer_mathbinary.c:101
i
int i
Definition: input.c:407
DNN_ERROR
@ DNN_ERROR
Definition: dnn_interface.h:33
math_binary_commutative
static void math_binary_commutative(FunType pfun, const DnnLayerMathBinaryParams *params, const DnnOperand *input, DnnOperand *output, DnnOperand *operands, const int32_t *input_operand_indexes)
Definition: dnn_backend_native_layer_mathbinary.c:57
DMBO_SUB
@ DMBO_SUB
Definition: dnn_backend_native_layer_mathbinary.h:34
DnnOperand
Definition: dnn_backend_native.h:68
ff_calculate_operand_dims_count
int32_t ff_calculate_operand_dims_count(const DnnOperand *oprd)
Definition: dnn_backend_native.c:394
DMBO_REALDIV
@ DMBO_REALDIV
Definition: dnn_backend_native_layer_mathbinary.h:37
ff_dnn_execute_layer_math_binary
int ff_dnn_execute_layer_math_binary(DnnOperand *operands, const int32_t *input_operand_indexes, int32_t output_operand_index, const void *parameters, NativeContext *ctx)
Definition: dnn_backend_native_layer_mathbinary.c:149
add
static float add(float src0, float src1)
Definition: dnn_backend_native_layer_mathbinary.c:36
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:28
DnnLayerMathBinaryParams::bin_op
DNNMathBinaryOperation bin_op
Definition: dnn_backend_native_layer_mathbinary.h:44
int
int
Definition: ffmpeg_filter.c:170
FunType
float(* FunType)(float src0, float src1)
Definition: dnn_backend_native_layer_mathbinary.c:30