2 * Copyright (c) 2019 Guo Yejun
4 * This file is part of FFmpeg.
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.
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.
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
22 #include "libavutil/avassert.h"
23 #include "dnn_backend_native_layer_pad.h"
25 int dnn_load_layer_pad(Layer *layer, AVIOContext *model_file_context, int file_size)
27 LayerPadParams *params;
29 params = av_malloc(sizeof(*params));
33 params->mode = (int32_t)avio_rl32(model_file_context);
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);
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);
43 layer->params = params;
48 static int before_get_buddy(int given, int paddings, LayerPadModeParam mode)
50 if (mode == LPMP_SYMMETRIC) {
51 return (2 * paddings - 1 - given);
52 } else if (mode == LPMP_REFLECT) {
53 return (2 * paddings - given);
55 av_assert0(!"should not reach here");
60 static int after_get_buddy(int given, int border, LayerPadModeParam mode)
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);
69 av_assert0(!"should not reach here");
74 int dnn_execute_layer_pad(DnnOperand *operands, const int32_t *input_operand_indexes,
75 int32_t output_operand_index, const void *parameters)
77 int32_t before_paddings;
78 int32_t after_paddings;
80 const LayerPadParams *params = (const LayerPadParams *)parameters;
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;
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];
95 int c_stride = channel;
96 int wc_stride = c_stride * width;
97 int hwc_stride = wc_stride * height;
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;
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)
113 output = output_operand->data;
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));
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;
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));
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;
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));
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;
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));
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;
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));
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;
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));
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;
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));
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;
237 int buddy = before_get_buddy(c, before_paddings, params->mode);
238 float *src = start + buddy;
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;
248 int buddy = after_get_buddy(given, channel + before_paddings, params->mode);
249 float *src = start + buddy;