FFmpeg
dnn_backend_native_layer_depth2space.c
Go to the documentation of this file.
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"
29 
30 int dnn_load_layer_depth2space(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num)
31 {
33  int dnn_size = 0;
34  params = av_malloc(sizeof(*params));
35  if (!params)
36  return 0;
37 
38  params->block_size = (int32_t)avio_rl32(model_file_context);
39  dnn_size += 4;
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 int dnn_execute_layer_depth2space(DnnOperand *operands, const int32_t *input_operand_indexes,
53  int32_t output_operand_index, const void *parameters, NativeContext *ctx)
54 {
55  float *output;
56  const DepthToSpaceParams *params = (const DepthToSpaceParams *)parameters;
57  int block_size = params->block_size;
58  int32_t input_operand_index = input_operand_indexes[0];
59  int number = operands[input_operand_index].dims[0];
60  int height = operands[input_operand_index].dims[1];
61  int width = operands[input_operand_index].dims[2];
62  int channels = operands[input_operand_index].dims[3];
63  const float *input = operands[input_operand_index].data;
64 
65  int y, x, by, bx, ch;
66  int new_channels = channels / (block_size * block_size);
67  int output_linesize = width * channels;
68  int by_linesize = output_linesize / block_size;
69  int x_linesize = new_channels * block_size;
70 
71  DnnOperand *output_operand = &operands[output_operand_index];
72  output_operand->dims[0] = number;
73  output_operand->dims[1] = height * block_size;
74  output_operand->dims[2] = width * block_size;
75  output_operand->dims[3] = new_channels;
76  output_operand->data_type = operands[input_operand_index].data_type;
77  output_operand->length = calculate_operand_data_length(output_operand);
78  if (output_operand->length <= 0) {
79  av_log(ctx, AV_LOG_ERROR, "The output data length overflow\n");
80  return DNN_ERROR;
81  }
82  output_operand->data = av_realloc(output_operand->data, output_operand->length);
83  if (!output_operand->data) {
84  av_log(ctx, AV_LOG_ERROR, "Failed to reallocate memory for output\n");
85  return DNN_ERROR;
86  }
87  output = output_operand->data;
88 
89  for (y = 0; y < height; ++y){
90  for (x = 0; x < width; ++x){
91  for (by = 0; by < block_size; ++by){
92  for (bx = 0; bx < block_size; ++bx){
93  for (ch = 0; ch < new_channels; ++ch){
94  output[by * by_linesize + x * x_linesize + bx * new_channels + ch] = input[ch];
95  }
96  input += new_channels;
97  }
98  }
99  }
100  output += output_linesize;
101  }
102  return 0;
103 }
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
DNN inference functions interface for native backend.
int32_t input_operand_indexes[4]
a layer can have multiple inputs and one output.
#define av_malloc(s)
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...
channels
Definition: aptx.h:33
#define av_log(a,...)
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:194
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:754
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_depth2space(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num)
#define width
int32_t
AVFormatContext * ctx
Definition: movenc.c:48
DNN inference functions interface 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
int32_t calculate_operand_data_length(const DnnOperand *oprd)
void * params
int dnn_execute_layer_depth2space(DnnOperand *operands, const int32_t *input_operand_indexes, int32_t output_operand_index, const void *parameters, NativeContext *ctx)
int32_t output_operand_index