FFmpeg
dnn_backend_native_layer_pad.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2019 Guo Yejun
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 #include <string.h>
22 #include "libavutil/avassert.h"
24 
25 int dnn_load_layer_pad(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num)
26 {
28  int dnn_size = 0;
29  params = av_malloc(sizeof(*params));
30  if (!params)
31  return 0;
32 
33  params->mode = (int32_t)avio_rl32(model_file_context);
34  dnn_size += 4;
35  for (int i = 0; i < 4; ++i) {
36  params->paddings[i][0] = avio_rl32(model_file_context);
37  params->paddings[i][1] = avio_rl32(model_file_context);
38  dnn_size += 8;
39  }
40  layer->input_operand_indexes[0] = (int32_t)avio_rl32(model_file_context);
41  layer->output_operand_index = (int32_t)avio_rl32(model_file_context);
42  dnn_size += 8;
43  layer->params = params;
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 static int before_get_buddy(int given, int paddings, LayerPadModeParam mode)
53 {
54  if (mode == LPMP_SYMMETRIC) {
55  return (2 * paddings - 1 - given);
56  } else if (mode == LPMP_REFLECT) {
57  return (2 * paddings - given);
58  } else {
59  av_assert0(!"should not reach here");
60  return 0;
61  }
62 }
63 
64 static int after_get_buddy(int given, int border, LayerPadModeParam mode)
65 {
66  if (mode == LPMP_SYMMETRIC) {
67  int offset = given - border;
68  return (border - 1 - offset);
69  } else if (mode == LPMP_REFLECT) {
70  int offset = given - border;
71  return (border - 2 - offset);
72  } else {
73  av_assert0(!"should not reach here");
74  return 0;
75  }
76 }
77 
78 int dnn_execute_layer_pad(DnnOperand *operands, const int32_t *input_operand_indexes,
79  int32_t output_operand_index, const void *parameters)
80 {
81  int32_t before_paddings;
82  int32_t after_paddings;
83  float* output;
84  const LayerPadParams *params = (const LayerPadParams *)parameters;
85 
86  // suppose format is <N, H, W, C>
87  int32_t input_operand_index = input_operand_indexes[0];
88  int number = operands[input_operand_index].dims[0];
89  int height = operands[input_operand_index].dims[1];
90  int width = operands[input_operand_index].dims[2];
91  int channel = operands[input_operand_index].dims[3];
92  const float *input = operands[input_operand_index].data;
93 
94  int new_number = number + params->paddings[0][0] + params->paddings[0][1];
95  int new_height = height + params->paddings[1][0] + params->paddings[1][1];
96  int new_width = width + params->paddings[2][0] + params->paddings[2][1];
97  int new_channel = channel + params->paddings[3][0] + params->paddings[3][1];
98 
99  int c_stride = channel;
100  int wc_stride = c_stride * width;
101  int hwc_stride = wc_stride * height;
102 
103  int new_c_stride = new_channel;
104  int new_wc_stride = new_c_stride * new_width;
105  int new_hwc_stride = new_wc_stride * new_height;
106 
107  DnnOperand *output_operand = &operands[output_operand_index];
108  output_operand->dims[0] = new_number;
109  output_operand->dims[1] = new_height;
110  output_operand->dims[2] = new_width;
111  output_operand->dims[3] = new_channel;
112  output_operand->data_type = operands[input_operand_index].data_type;
113  output_operand->length = calculate_operand_data_length(output_operand);
114  output_operand->data = av_realloc(output_operand->data, output_operand->length);
115  if (!output_operand->data)
116  return -1;
117  output = output_operand->data;
118 
119  // copy the original data
120  for (int n = 0; n < number; n++) {
121  for (int h = 0; h < height; h++) {
122  for (int w = 0; w < width; w++) {
123  const float *src = input + n * hwc_stride + h * wc_stride + w * c_stride;
124  float *dst = output + (n + params->paddings[0][0]) * new_hwc_stride
125  + (h + params->paddings[1][0]) * new_wc_stride
126  + (w + params->paddings[2][0]) * new_c_stride
127  + params->paddings[3][0];
128  memcpy(dst, src, channel * sizeof(float));
129  }
130  }
131  }
132 
133  // handle the first dimension
134  before_paddings = params->paddings[0][0];
135  after_paddings = params->paddings[0][1];
136  for (int n = 0; n < before_paddings; n++) {
137  float *dst = output + n * new_hwc_stride;
138  if (params->mode == LPMP_CONSTANT) {
139  for (int i = 0; i < new_hwc_stride; i++) {
140  dst[i] = params->constant_values;
141  }
142  }
143  else {
144  int buddy = before_get_buddy(n, before_paddings, params->mode);
145  float *src = output + buddy * new_hwc_stride;
146  memcpy(dst, src, new_hwc_stride * sizeof(float));
147  }
148  }
149  for (int n = 0; n < after_paddings; n++) {
150  int given = number + before_paddings + n;
151  float *dst = output + given * new_hwc_stride;
152  if (params->mode == LPMP_CONSTANT) {
153  for (int i = 0; i < new_hwc_stride; i++) {
154  dst[i] = params->constant_values;
155  }
156  } else {
157  int buddy = after_get_buddy(given, number + before_paddings, params->mode);
158  float *src = output + buddy * new_hwc_stride;
159  memcpy(dst, src, new_hwc_stride * sizeof(float));
160  }
161  }
162 
163  // handle the second dimension
164  before_paddings = params->paddings[1][0];
165  after_paddings = params->paddings[1][1];
166  for (int n = 0; n < new_number; n++) {
167  float *start = output + n * new_hwc_stride;
168  for (int h = 0; h < before_paddings; h++) {
169  float *dst = start + h * new_wc_stride;
170  if (params->mode == LPMP_CONSTANT) {
171  for (int i = 0; i < new_wc_stride; i++) {
172  dst[i] = params->constant_values;
173  }
174  } else {
175  int buddy = before_get_buddy(h, before_paddings, params->mode);
176  float *src = start + buddy * new_wc_stride;
177  memcpy(dst, src, new_wc_stride * sizeof(float));
178  }
179  }
180  for (int h = 0; h < after_paddings; h++) {
181  int given = height + before_paddings + h;
182  float *dst = start + given * new_wc_stride;
183  if (params->mode == LPMP_CONSTANT) {
184  for (int i = 0; i < new_wc_stride; i++) {
185  dst[i] = params->constant_values;
186  }
187  } else {
188  int buddy = after_get_buddy(given, height + before_paddings, params->mode);
189  float *src = start + buddy * new_wc_stride;
190  memcpy(dst, src, new_wc_stride * sizeof(float));
191  }
192  }
193  }
194 
195  // handle the third dimension
196  before_paddings = params->paddings[2][0];
197  after_paddings = params->paddings[2][1];
198  for (int n = 0; n < new_number; n++) {
199  for (int h = 0; h < new_height; h++) {
200  float *start = output + n * new_hwc_stride + h * new_wc_stride;
201  for (int w = 0; w < before_paddings; w++) {
202  float *dst = start + w * new_c_stride;
203  if (params->mode == LPMP_CONSTANT) {
204  for (int i = 0; i < new_c_stride; i++) {
205  dst[i] = params->constant_values;
206  }
207  } else {
208  int buddy = before_get_buddy(w, before_paddings, params->mode);
209  float *src = start + buddy * new_c_stride;
210  memcpy(dst, src, new_c_stride * sizeof(float));
211  }
212  }
213  for (int w = 0; w < after_paddings; w++) {
214  int given = width + before_paddings + w;
215  float *dst = start + given * new_c_stride;
216  if (params->mode == LPMP_CONSTANT) {
217  for (int i = 0; i < new_c_stride; i++) {
218  dst[i] = params->constant_values;
219  }
220  } else {
221  int buddy = after_get_buddy(given, width + before_paddings, params->mode);
222  float *src = start + buddy * new_c_stride;
223  memcpy(dst, src, new_c_stride * sizeof(float));
224  }
225  }
226  }
227  }
228 
229  // handle the fourth dimension
230  before_paddings = params->paddings[3][0];
231  after_paddings = params->paddings[3][1];
232  for (int n = 0; n < new_number; n++) {
233  for (int h = 0; h < new_height; h++) {
234  for (int w = 0; w < new_width; w++) {
235  float *start = output + n * new_hwc_stride + h * new_wc_stride + w * new_c_stride;
236  for (int c = 0; c < before_paddings; c++) {
237  float *dst = start + c;
238  if (params->mode == LPMP_CONSTANT) {
239  *dst = params->constant_values;
240  } else {
241  int buddy = before_get_buddy(c, before_paddings, params->mode);
242  float *src = start + buddy;
243  *dst = *src;
244  }
245  }
246  for (int c = 0; c < after_paddings; c++) {
247  int given = channel + before_paddings + c;
248  float *dst = start + given;
249  if (params->mode == LPMP_CONSTANT) {
250  *dst = params->constant_values;
251  } else {
252  int buddy = after_get_buddy(given, channel + before_paddings, params->mode);
253  float *src = start + buddy;
254  *dst = *src;
255  }
256  }
257  }
258  }
259  }
260 
261  return 0;
262 }
Bytestream IO Context.
Definition: avio.h:161
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:134
int32_t input_operand_indexes[4]
a layer can have multiple inputs and one output.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
#define av_malloc(s)
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf offset
Undefined Behavior In the C some operations are like signed integer dereferencing freed accessing outside allocated Undefined Behavior must not occur in a C it is not safe even if the output of undefined operations is unused The unsafety may seem nit picking but Optimizing compilers have in fact optimized code on the assumption that no undefined Behavior occurs Optimizing code based on wrong assumptions can and has in some cases lead to effects beyond the output of computations The signed integer overflow problem in speed critical code Code which is highly optimized and works with signed integers sometimes has the problem that often the output of the computation does not c
Definition: undefined.txt:32
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
#define height
DNNDataType data_type
support different kinds of data type such as float, half float, int8 etc, first support float now...
#define src
Definition: vp8dsp.c:254
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:759
void * data
data pointer with data length in bytes.
GLenum GLint * params
Definition: opengl_enc.c:113
simple assert() macros that are a bit more flexible than ISO C assert().
int32_t dims[4]
there are two memory layouts, NHWC or NCHW, so we use dims, dims[0] is Number.
int dnn_load_layer_pad(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num)
#define width
uint8_t w
Definition: llviddspenc.c:38
static int after_get_buddy(int given, int border, LayerPadModeParam mode)
int32_t
int dnn_execute_layer_pad(DnnOperand *operands, const int32_t *input_operand_indexes, int32_t output_operand_index, const void *parameters)
static int before_get_buddy(int given, int paddings, LayerPadModeParam mode)
layer pad (equivalent to tf.pad) for native backend.
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
channel
Use these values when setting the channel map with ebur128_set_channel().
Definition: ebur128.h:39
int32_t calculate_operand_data_length(const DnnOperand *oprd)
void * params
mode
Use these values in ebur128_init (or&#39;ed).
Definition: ebur128.h:83
int i
Definition: input.c:406
int32_t output_operand_index