FFmpeg  4.3
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)
26 {
27  LayerPadParams *params;
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  return dnn_size;
46 }
47 
48 static int before_get_buddy(int given, int paddings, LayerPadModeParam mode)
49 {
50  if (mode == LPMP_SYMMETRIC) {
51  return (2 * paddings - 1 - given);
52  } else if (mode == LPMP_REFLECT) {
53  return (2 * paddings - given);
54  } else {
55  av_assert0(!"should not reach here");
56  return 0;
57  }
58 }
59 
60 static int after_get_buddy(int given, int border, LayerPadModeParam mode)
61 {
62  if (mode == LPMP_SYMMETRIC) {
63  int offset = given - border;
64  return (border - 1 - offset);
65  } else if (mode == LPMP_REFLECT) {
66  int offset = given - border;
67  return (border - 2 - offset);
68  } else {
69  av_assert0(!"should not reach here");
70  return 0;
71  }
72 }
73 
74 int dnn_execute_layer_pad(DnnOperand *operands, const int32_t *input_operand_indexes,
75  int32_t output_operand_index, const void *parameters)
76 {
77  int32_t before_paddings;
78  int32_t after_paddings;
79  float* output;
80  const LayerPadParams *params = (const LayerPadParams *)parameters;
81 
82  // suppose format is <N, H, W, C>
83  int32_t input_operand_index = input_operand_indexes[0];
84  int number = operands[input_operand_index].dims[0];
85  int height = operands[input_operand_index].dims[1];
86  int width = operands[input_operand_index].dims[2];
87  int channel = operands[input_operand_index].dims[3];
88  const float *input = operands[input_operand_index].data;
89 
90  int new_number = number + params->paddings[0][0] + params->paddings[0][1];
91  int new_height = height + params->paddings[1][0] + params->paddings[1][1];
92  int new_width = width + params->paddings[2][0] + params->paddings[2][1];
93  int new_channel = channel + params->paddings[3][0] + params->paddings[3][1];
94 
95  int c_stride = channel;
96  int wc_stride = c_stride * width;
97  int hwc_stride = wc_stride * height;
98 
99  int new_c_stride = new_channel;
100  int new_wc_stride = new_c_stride * new_width;
101  int new_hwc_stride = new_wc_stride * new_height;
102 
103  DnnOperand *output_operand = &operands[output_operand_index];
104  output_operand->dims[0] = new_number;
105  output_operand->dims[1] = new_height;
106  output_operand->dims[2] = new_width;
107  output_operand->dims[3] = new_channel;
108  output_operand->data_type = operands[input_operand_index].data_type;
109  output_operand->length = calculate_operand_data_length(output_operand);
110  output_operand->data = av_realloc(output_operand->data, output_operand->length);
111  if (!output_operand->data)
112  return -1;
113  output = output_operand->data;
114 
115  // copy the original data
116  for (int n = 0; n < number; n++) {
117  for (int h = 0; h < height; h++) {
118  for (int w = 0; w < width; w++) {
119  const float *src = input + n * hwc_stride + h * wc_stride + w * c_stride;
120  float *dst = output + (n + params->paddings[0][0]) * new_hwc_stride
121  + (h + params->paddings[1][0]) * new_wc_stride
122  + (w + params->paddings[2][0]) * new_c_stride
123  + params->paddings[3][0];
124  memcpy(dst, src, channel * sizeof(float));
125  }
126  }
127  }
128 
129  // handle the first dimension
130  before_paddings = params->paddings[0][0];
131  after_paddings = params->paddings[0][1];
132  for (int n = 0; n < before_paddings; n++) {
133  float *dst = output + n * new_hwc_stride;
134  if (params->mode == LPMP_CONSTANT) {
135  for (int i = 0; i < new_hwc_stride; i++) {
136  dst[i] = params->constant_values;
137  }
138  }
139  else {
140  int buddy = before_get_buddy(n, before_paddings, params->mode);
141  float *src = output + buddy * new_hwc_stride;
142  memcpy(dst, src, new_hwc_stride * sizeof(float));
143  }
144  }
145  for (int n = 0; n < after_paddings; n++) {
146  int given = number + before_paddings + n;
147  float *dst = output + given * new_hwc_stride;
148  if (params->mode == LPMP_CONSTANT) {
149  for (int i = 0; i < new_hwc_stride; i++) {
150  dst[i] = params->constant_values;
151  }
152  } else {
153  int buddy = after_get_buddy(given, number + before_paddings, params->mode);
154  float *src = output + buddy * new_hwc_stride;
155  memcpy(dst, src, new_hwc_stride * sizeof(float));
156  }
157  }
158 
159  // handle the second dimension
160  before_paddings = params->paddings[1][0];
161  after_paddings = params->paddings[1][1];
162  for (int n = 0; n < new_number; n++) {
163  float *start = output + n * new_hwc_stride;
164  for (int h = 0; h < before_paddings; h++) {
165  float *dst = start + h * new_wc_stride;
166  if (params->mode == LPMP_CONSTANT) {
167  for (int i = 0; i < new_wc_stride; i++) {
168  dst[i] = params->constant_values;
169  }
170  } else {
171  int buddy = before_get_buddy(h, before_paddings, params->mode);
172  float *src = start + buddy * new_wc_stride;
173  memcpy(dst, src, new_wc_stride * sizeof(float));
174  }
175  }
176  for (int h = 0; h < after_paddings; h++) {
177  int given = height + before_paddings + h;
178  float *dst = start + given * new_wc_stride;
179  if (params->mode == LPMP_CONSTANT) {
180  for (int i = 0; i < new_wc_stride; i++) {
181  dst[i] = params->constant_values;
182  }
183  } else {
184  int buddy = after_get_buddy(given, height + before_paddings, params->mode);
185  float *src = start + buddy * new_wc_stride;
186  memcpy(dst, src, new_wc_stride * sizeof(float));
187  }
188  }
189  }
190 
191  // handle the third dimension
192  before_paddings = params->paddings[2][0];
193  after_paddings = params->paddings[2][1];
194  for (int n = 0; n < new_number; n++) {
195  for (int h = 0; h < new_height; h++) {
196  float *start = output + n * new_hwc_stride + h * new_wc_stride;
197  for (int w = 0; w < before_paddings; w++) {
198  float *dst = start + w * new_c_stride;
199  if (params->mode == LPMP_CONSTANT) {
200  for (int i = 0; i < new_c_stride; i++) {
201  dst[i] = params->constant_values;
202  }
203  } else {
204  int buddy = before_get_buddy(w, before_paddings, params->mode);
205  float *src = start + buddy * new_c_stride;
206  memcpy(dst, src, new_c_stride * sizeof(float));
207  }
208  }
209  for (int w = 0; w < after_paddings; w++) {
210  int given = width + before_paddings + w;
211  float *dst = start + given * new_c_stride;
212  if (params->mode == LPMP_CONSTANT) {
213  for (int i = 0; i < new_c_stride; i++) {
214  dst[i] = params->constant_values;
215  }
216  } else {
217  int buddy = after_get_buddy(given, width + before_paddings, params->mode);
218  float *src = start + buddy * new_c_stride;
219  memcpy(dst, src, new_c_stride * sizeof(float));
220  }
221  }
222  }
223  }
224 
225  // handle the fourth dimension
226  before_paddings = params->paddings[3][0];
227  after_paddings = params->paddings[3][1];
228  for (int n = 0; n < new_number; n++) {
229  for (int h = 0; h < new_height; h++) {
230  for (int w = 0; w < new_width; w++) {
231  float *start = output + n * new_hwc_stride + h * new_wc_stride + w * new_c_stride;
232  for (int c = 0; c < before_paddings; c++) {
233  float *dst = start + c;
234  if (params->mode == LPMP_CONSTANT) {
235  *dst = params->constant_values;
236  } else {
237  int buddy = before_get_buddy(c, before_paddings, params->mode);
238  float *src = start + buddy;
239  *dst = *src;
240  }
241  }
242  for (int c = 0; c < after_paddings; c++) {
243  int given = channel + before_paddings + c;
244  float *dst = start + given;
245  if (params->mode == LPMP_CONSTANT) {
246  *dst = params->constant_values;
247  } else {
248  int buddy = after_get_buddy(given, channel + before_paddings, params->mode);
249  float *src = start + buddy;
250  *dst = *src;
251  }
252  }
253  }
254  }
255  }
256 
257  return 0;
258 }
calculate_operand_data_length
int32_t calculate_operand_data_length(const DnnOperand *oprd)
Definition: dnn_backend_native.c:303
before_get_buddy
static int before_get_buddy(int given, int paddings, LayerPadModeParam mode)
Definition: dnn_backend_native_layer_pad.c:48
av_realloc
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:134
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
LayerPadModeParam
LayerPadModeParam
Definition: dnn_backend_native_layer_pad.h:31
LPMP_REFLECT
@ LPMP_REFLECT
Definition: dnn_backend_native_layer_pad.h:31
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:31
dnn_backend_native_layer_pad.h
avassert.h
LayerPadParams::mode
LayerPadModeParam mode
Definition: dnn_backend_native_layer_pad.h:35
width
#define width
DnnOperand::data
void * data
data pointer with data length in bytes.
Definition: dnn_backend_native.h:98
av_assert0
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
DnnOperand::data_type
DNNDataType data_type
support different kinds of data type such as float, half float, int8 etc, first support float now.
Definition: dnn_backend_native.h:79
LayerPadParams
Definition: dnn_backend_native_layer_pad.h:33
int32_t
int32_t
Definition: audio_convert.c:194
dnn_execute_layer_pad
int dnn_execute_layer_pad(DnnOperand *operands, const int32_t *input_operand_indexes, int32_t output_operand_index, const void *parameters)
Definition: dnn_backend_native_layer_pad.c:74
Layer::params
void * params
Definition: dnn_backend_native.h:60
DnnOperand::dims
int32_t dims[4]
there are two memory layouts, NHWC or NCHW, so we use dims, dims[0] is Number.
Definition: dnn_backend_native.h:68
src
#define src
Definition: vp8dsp.c:254
c
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
LPMP_SYMMETRIC
@ LPMP_SYMMETRIC
Definition: dnn_backend_native_layer_pad.h:31
DnnOperand::length
int32_t length
Definition: dnn_backend_native.h:99
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
LPMP_CONSTANT
@ LPMP_CONSTANT
Definition: dnn_backend_native_layer_pad.h:31
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
height
#define height
offset
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
Definition: writing_filters.txt:86
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
i
#define i(width, name, range_min, range_max)
Definition: cbs_h2645.c:269
LayerPadParams::paddings
int32_t paddings[4][2]
Definition: dnn_backend_native_layer_pad.h:34
w
FFmpeg Automated Testing Environment ************************************Introduction Using FATE from your FFmpeg source directory Submitting the results to the FFmpeg result aggregation server Uploading new samples to the fate suite FATE makefile targets and variables Makefile targets Makefile variables Examples Introduction **************FATE is an extended regression suite on the client side and a means for results aggregation and presentation on the server side The first part of this document explains how you can use FATE from your FFmpeg source directory to test your ffmpeg binary The second part describes how you can run FATE to submit the results to FFmpeg’s FATE server In any way you can have a look at the publicly viewable FATE results by visiting this as it can be seen if some test on some platform broke with their recent contribution This usually happens on the platforms the developers could not test on The second part of this document describes how you can run FATE to submit your results to FFmpeg’s FATE server If you want to submit your results be sure to check that your combination of OS and compiler is not already listed on the above mentioned website In the third part you can find a comprehensive listing of FATE makefile targets and variables Using FATE from your FFmpeg source directory **********************************************If you want to run FATE on your machine you need to have the samples in place You can get the samples via the build target fate rsync Use this command from the top level source this will cause FATE to fail NOTE To use a custom wrapper to run the pass ‘ target exec’ to ‘configure’ or set the TARGET_EXEC Make variable Submitting the results to the FFmpeg result aggregation server ****************************************************************To submit your results to the server you should run fate through the shell script ‘tests fate sh’ from the FFmpeg sources This script needs to be invoked with a configuration file as its first argument tests fate sh path to fate_config A configuration file template with comments describing the individual configuration variables can be found at ‘doc fate_config sh template’ Create a configuration that suits your based on the configuration template The ‘slot’ configuration variable can be any string that is not yet but it is suggested that you name it adhering to the following pattern ‘ARCH OS COMPILER COMPILER VERSION’ The configuration file itself will be sourced in a shell therefore all shell features may be used This enables you to setup the environment as you need it for your build For your first test runs the ‘fate_recv’ variable should be empty or commented out This will run everything as normal except that it will omit the submission of the results to the server The following files should be present in $workdir as specified in the configuration it may help to try out the ‘ssh’ command with one or more ‘ v’ options You should get detailed output concerning your SSH configuration and the authentication process The only thing left is to automate the execution of the fate sh script and the synchronisation of the samples directory Uploading new samples to the fate suite *****************************************If you need a sample uploaded send a mail to samples request This is for developers who have an account on the fate suite server If you upload new please make sure they are as small as space on each network bandwidth and so on benefit from smaller test cases Also keep in mind older checkouts use existing sample that means in practice generally do not remove or overwrite files as it likely would break older checkouts or releases Also all needed samples for a commit should be ideally before the push If you need an account for frequently uploading samples or you wish to help others by doing that send a mail to ffmpeg devel rsync vauL Duo ug o o w
Definition: fate.txt:150
DnnOperand
Definition: dnn_backend_native.h:63
mode
mode
Definition: ebur128.h:83
after_get_buddy
static int after_get_buddy(int given, int border, LayerPadModeParam mode)
Definition: dnn_backend_native_layer_pad.c:60
LayerPadParams::constant_values
float constant_values
Definition: dnn_backend_native_layer_pad.h:36
h
h
Definition: vp9dsp_template.c:2038
dnn_load_layer_pad
int dnn_load_layer_pad(Layer *layer, AVIOContext *model_file_context, int file_size)
Definition: dnn_backend_native_layer_pad.c:25
channel
channel
Definition: ebur128.h:39