]> git.sesse.net Git - ffmpeg/blob - libavfilter/dnn/dnn_backend_native_layer_depth2space.c
libavfilter/dnn: separate depth_to_space layer from dnn_backend_native.c to a new...
[ffmpeg] / libavfilter / dnn / dnn_backend_native_layer_depth2space.c
1 /*
2  * Copyright (c) 2018 Sergey Lavrushkin
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"
28 #include "dnn_backend_native_layer_depth2space.h"
29
30 int depth_to_space(DnnOperand *operands, const int32_t *input_operand_indexes, int32_t output_operand_index, int block_size)
31 {
32     float *output;
33     int32_t input_operand_index = input_operand_indexes[0];
34     int number = operands[input_operand_index].dims[0];
35     int height = operands[input_operand_index].dims[1];
36     int width = operands[input_operand_index].dims[2];
37     int channels = operands[input_operand_index].dims[3];
38     const float *input = operands[input_operand_index].data;
39
40     int y, x, by, bx, ch;
41     int new_channels = channels / (block_size * block_size);
42     int output_linesize = width * channels;
43     int by_linesize = output_linesize / block_size;
44     int x_linesize = new_channels * block_size;
45
46     DnnOperand *output_operand = &operands[output_operand_index];
47     output_operand->dims[0] = number;
48     output_operand->dims[1] = height * block_size;
49     output_operand->dims[2] = width * block_size;
50     output_operand->dims[3] = new_channels;
51     output_operand->length = calculate_operand_data_length(output_operand);
52     output_operand->data = av_realloc(output_operand->data, output_operand->length);
53     if (!output_operand->data)
54         return -1;
55     output = output_operand->data;
56
57     for (y = 0; y < height; ++y){
58         for (x = 0; x < width; ++x){
59             for (by = 0; by < block_size; ++by){
60                 for (bx = 0; bx < block_size; ++bx){
61                     for (ch = 0; ch < new_channels; ++ch){
62                         output[by * by_linesize + x * x_linesize + bx * new_channels + ch] = input[ch];
63                     }
64                     input += new_channels;
65                 }
66             }
67         }
68         output += output_linesize;
69     }
70     return 0;
71 }