summaryrefslogtreecommitdiffstats
path: root/libavfilter/dnn/dnn_backend_native_layer_pad.c
blob: 8fa35de196a5408f64487d33c613edb4b73dd86b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
 * Copyright (c) 2019 Guo Yejun
 *
 * This file is part of FFmpeg.
 *
 * FFmpeg is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * FFmpeg is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with FFmpeg; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
 */

#include <string.h>
#include "libavutil/avassert.h"
#include "dnn_backend_native_layer_pad.h"

int dnn_load_layer_pad(Layer *layer, AVIOContext *model_file_context, int file_size)
{
    LayerPadParams *params;
    int dnn_size = 0;
    params = av_malloc(sizeof(*params));
    if (!params)
        return 0;

    params->mode = (int32_t)avio_rl32(model_file_context);
    dnn_size += 4;
    for (int i = 0; i < 4; ++i) {
        params->paddings[i][0] = avio_rl32(model_file_context);
        params->paddings[i][1] = avio_rl32(model_file_context);
        dnn_size += 8;
    }
    layer->input_operand_indexes[0] = (int32_t)avio_rl32(model_file_context);
    layer->output_operand_index = (int32_t)avio_rl32(model_file_context);
    dnn_size += 8;
    layer->params = params;

    return dnn_size;
}

static int before_get_buddy(int given, int paddings, LayerPadModeParam mode)
{
    if (mode == LPMP_SYMMETRIC) {
        return (2 * paddings - 1 - given);
    } else if (mode == LPMP_REFLECT) {
        return (2 * paddings - given);
    } else {
        av_assert0(!"should not reach here");
        return 0;
    }
}

static int after_get_buddy(int given, int border, LayerPadModeParam mode)
{
    if (mode == LPMP_SYMMETRIC) {
        int offset = given - border;
        return (border - 1 - offset);
    } else if (mode == LPMP_REFLECT) {
        int offset = given - border;
        return (border - 2 - offset);
    } else {
        av_assert0(!"should not reach here");
        return 0;
    }
}

int dnn_execute_layer_pad(DnnOperand *operands, const int32_t *input_operand_indexes,
                          int32_t output_operand_index, const void *parameters)
{
    int32_t before_paddings;
    int32_t after_paddings;
    float* output;
    const LayerPadParams *params = (const LayerPadParams *)parameters;

    // suppose format is <N, H, W, C>
    int32_t input_operand_index = input_operand_indexes[0];
    int number = operands[input_operand_index].dims[0];
    int height = operands[input_operand_index].dims[1];
    int width = operands[input_operand_index].dims[2];
    int channel = operands[input_operand_index].dims[3];
    const float *input = operands[input_operand_index].data;

    int new_number = number + params->paddings[0][0] + params->paddings[0][1];
    int new_height = height + params->paddings[1][0] + params->paddings[1][1];
    int new_width = width + params->paddings[2][0] + params->paddings[2][1];
    int new_channel = channel + params->paddings[3][0] + params->paddings[3][1];

    int c_stride = channel;
    int wc_stride = c_stride * width;
    int hwc_stride = wc_stride * height;

    int new_c_stride = new_channel;
    int new_wc_stride = new_c_stride * new_width;
    int new_hwc_stride = new_wc_stride * new_height;

    DnnOperand *output_operand = &operands[output_operand_index];
    output_operand->dims[0] = new_number;
    output_operand->dims[1] = new_height;
    output_operand->dims[2] = new_width;
    output_operand->dims[3] = new_channel;
    output_operand->length = calculate_operand_data_length(output_operand);
    output_operand->data = av_realloc(output_operand->data, output_operand->length);
    if (!output_operand->data)
        return -1;
    output = output_operand->data;

    // copy the original data
    for (int n = 0; n < number; n++) {
        for (int h = 0; h < height; h++) {
            for (int w = 0; w < width; w++) {
                const float *src = input + n * hwc_stride + h * wc_stride + w * c_stride;
                float *dst = output + (n + params->paddings[0][0]) * new_hwc_stride
                                    + (h + params->paddings[1][0]) * new_wc_stride
                                    + (w + params->paddings[2][0]) * new_c_stride
                                    + params->paddings[3][0];
                memcpy(dst, src, channel * sizeof(float));
            }
        }
    }

    // handle the first dimension
    before_paddings = params->paddings[0][0];
    after_paddings = params->paddings[0][1];
    for (int n = 0; n < before_paddings; n++) {
        float *dst = output + n * new_hwc_stride;
        if (params->mode == LPMP_CONSTANT) {
            for (int i = 0; i < new_hwc_stride; i++) {
                dst[i] = params->constant_values;
            }
        }
        else {
            int buddy = before_get_buddy(n, before_paddings, params->mode);
            float *src = output + buddy * new_hwc_stride;
            memcpy(dst, src, new_hwc_stride * sizeof(float));
        }
    }
    for (int n = 0; n < after_paddings; n++) {
        int given = number + before_paddings + n;
        float *dst = output + given * new_hwc_stride;
        if (params->mode == LPMP_CONSTANT) {
            for (int i = 0; i < new_hwc_stride; i++) {
                dst[i] = params->constant_values;
            }
        } else {
            int buddy = after_get_buddy(given, number + before_paddings, params->mode);
            float *src = output + buddy * new_hwc_stride;
            memcpy(dst, src, new_hwc_stride * sizeof(float));
        }
    }

    // handle the second dimension
    before_paddings = params->paddings[1][0];
    after_paddings = params->paddings[1][1];
    for (int n = 0; n < new_number; n++) {
        float *start = output + n * new_hwc_stride;
        for (int h = 0; h < before_paddings; h++) {
            float *dst = start + h * new_wc_stride;
            if (params->mode == LPMP_CONSTANT) {
                for (int i = 0; i < new_wc_stride; i++) {
                    dst[i] = params->constant_values;
                }
            } else {
                int buddy = before_get_buddy(h, before_paddings, params->mode);
                float *src = start + buddy * new_wc_stride;
                memcpy(dst, src, new_wc_stride * sizeof(float));
            }
        }
        for (int h = 0; h < after_paddings; h++) {
            int given = height + before_paddings + h;
            float *dst = start + given * new_wc_stride;
            if (params->mode == LPMP_CONSTANT) {
                for (int i = 0; i < new_wc_stride; i++) {
                    dst[i] = params->constant_values;
                }
            } else {
                int buddy = after_get_buddy(given, height + before_paddings, params->mode);
                float *src = start + buddy * new_wc_stride;
                memcpy(dst, src, new_wc_stride * sizeof(float));
            }
        }
    }

    // handle the third dimension
    before_paddings = params->paddings[2][0];
    after_paddings = params->paddings[2][1];
    for (int n = 0; n < new_number; n++) {
        for (int h = 0; h < new_height; h++) {
            float *start = output + n * new_hwc_stride + h * new_wc_stride;
            for (int w = 0; w < before_paddings; w++) {
                float *dst = start + w * new_c_stride;
                if (params->mode == LPMP_CONSTANT) {
                    for (int i = 0; i < new_c_stride; i++) {
                        dst[i] = params->constant_values;
                    }
                } else {
                    int buddy = before_get_buddy(w, before_paddings, params->mode);
                    float *src = start + buddy * new_c_stride;
                    memcpy(dst, src, new_c_stride * sizeof(float));
                }
            }
            for (int w = 0; w < after_paddings; w++) {
                int given = width + before_paddings + w;
                float *dst = start + given * new_c_stride;
                if (params->mode == LPMP_CONSTANT) {
                    for (int i = 0; i < new_c_stride; i++) {
                        dst[i] = params->constant_values;
                    }
                } else {
                    int buddy = after_get_buddy(given, width + before_paddings, params->mode);
                    float *src = start + buddy * new_c_stride;
                    memcpy(dst, src, new_c_stride * sizeof(float));
                }
            }
        }
    }

    // handle the fourth dimension
    before_paddings = params->paddings[3][0];
    after_paddings = params->paddings[3][1];
    for (int n = 0; n < new_number; n++) {
        for (int h = 0; h < new_height; h++) {
            for (int w = 0; w < new_width; w++) {
                float *start = output + n * new_hwc_stride + h * new_wc_stride + w * new_c_stride;
                for (int c = 0; c < before_paddings; c++) {
                    float *dst = start + c;
                    if (params->mode == LPMP_CONSTANT) {
                        *dst = params->constant_values;
                    } else {
                        int buddy = before_get_buddy(c, before_paddings, params->mode);
                        float *src = start + buddy;
                        *dst = *src;
                    }
                }
                for (int c = 0; c < after_paddings; c++) {
                    int given = channel + before_paddings + c;
                    float *dst = start + given;
                    if (params->mode == LPMP_CONSTANT) {
                        *dst = params->constant_values;
                    } else {
                        int buddy = after_get_buddy(given, channel + before_paddings, params->mode);
                        float *src = start + buddy;
                        *dst = *src;
                    }
                }
            }
        }
    }

    return 0;
}
OpenPOWER on IntegriCloud