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"
28 
29 int ff_dnn_load_layer_depth2space(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num)
30 {
31  DepthToSpaceParams *params;
32  int dnn_size = 0;
33  params = av_malloc(sizeof(*params));
34  if (!params)
35  return 0;
36 
37  params->block_size = (int32_t)avio_rl32(model_file_context);
38  dnn_size += 4;
39  layer->input_operand_indexes[0] = (int32_t)avio_rl32(model_file_context);
40  layer->output_operand_index = (int32_t)avio_rl32(model_file_context);
41  dnn_size += 8;
42  layer->params = params;
43 
44  if (layer->input_operand_indexes[0] >= operands_num || layer->output_operand_index >= operands_num) {
45  return 0;
46  }
47 
48  return dnn_size;
49 }
50 
51 int ff_dnn_execute_layer_depth2space(DnnOperand *operands, const int32_t *input_operand_indexes,
52  int32_t output_operand_index, const void *parameters, NativeContext *ctx)
53 {
54  float *output;
55  const DepthToSpaceParams *params = parameters;
56  int block_size = params->block_size;
57  int32_t input_operand_index = input_operand_indexes[0];
58  int number = operands[input_operand_index].dims[0];
59  int height = operands[input_operand_index].dims[1];
60  int width = operands[input_operand_index].dims[2];
61  int channels = operands[input_operand_index].dims[3];
62  const float *input = operands[input_operand_index].data;
63 
64  int y, x, by, bx, ch;
65  int new_channels = channels / (block_size * block_size);
66  int output_linesize = width * channels;
67  int by_linesize = output_linesize / block_size;
68  int x_linesize = new_channels * block_size;
69 
70  DnnOperand *output_operand = &operands[output_operand_index];
71  output_operand->dims[0] = number;
72  output_operand->dims[1] = height * block_size;
73  output_operand->dims[2] = width * block_size;
74  output_operand->dims[3] = new_channels;
75  output_operand->data_type = operands[input_operand_index].data_type;
76  output_operand->length = ff_calculate_operand_data_length(output_operand);
77  if (output_operand->length <= 0) {
78  av_log(ctx, AV_LOG_ERROR, "The output data length overflow\n");
79  return AVERROR(EINVAL);
80  }
81  output_operand->data = av_realloc(output_operand->data, output_operand->length);
82  if (!output_operand->data) {
83  av_log(ctx, AV_LOG_ERROR, "Failed to reallocate memory for output\n");
84  return AVERROR(ENOMEM);
85  }
86  output = output_operand->data;
87 
88  for (y = 0; y < height; ++y){
89  for (x = 0; x < width; ++x){
90  for (by = 0; by < block_size; ++by){
91  for (bx = 0; bx < block_size; ++bx){
92  for (ch = 0; ch < new_channels; ++ch){
93  output[by * by_linesize + x * x_linesize + bx * new_channels + ch] = input[ch];
94  }
95  input += new_channels;
96  }
97  }
98  }
99  output += output_linesize;
100  }
101  return 0;
102 }
AVERROR
Filter the word “frame” indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
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
av_malloc
#define av_malloc(s)
Definition: tableprint_vlc.h:30
ff_calculate_operand_data_length
int32_t ff_calculate_operand_data_length(const DnnOperand *oprd)
Definition: dnn_backend_native.c:503
AV_LOG_ERROR
#define AV_LOG_ERROR
Something went wrong and cannot losslessly be recovered.
Definition: log.h:180
width
#define width
DnnOperand::data
void * data
data pointer with data length in bytes.
Definition: dnn_backend_native.h:104
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:85
ctx
AVFormatContext * ctx
Definition: movenc.c:48
channels
channels
Definition: aptx.h:31
dnn_backend_native_layer_depth2space.h
Layer::params
void * params
Definition: dnn_backend_native.h:66
ff_dnn_load_layer_depth2space
int ff_dnn_load_layer_depth2space(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num)
Load the Depth to Space Layer.
Definition: dnn_backend_native_layer_depth2space.c:29
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:74
DnnOperand::length
int32_t length
Definition: dnn_backend_native.h:105
avio_rl32
unsigned int avio_rl32(AVIOContext *s)
Definition: aviobuf.c:751
AVIOContext
Bytestream IO Context.
Definition: avio.h:166
Layer::output_operand_index
int32_t output_operand_index
Definition: dnn_backend_native.h:65
NativeContext
Definition: dnn_backend_native.h:118
Layer
Definition: dnn_backend_native.h:57
Layer::input_operand_indexes
int32_t input_operand_indexes[4]
a layer can have multiple inputs and one output.
Definition: dnn_backend_native.h:64
height
#define height
DepthToSpaceParams
Definition: dnn_backend_native_layer_depth2space.h:33
dnn_backend_native.h
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
DepthToSpaceParams::block_size
int block_size
Definition: dnn_backend_native_layer_depth2space.h:34
DnnOperand
Definition: dnn_backend_native.h:69
ff_dnn_execute_layer_depth2space
int ff_dnn_execute_layer_depth2space(DnnOperand *operands, const int32_t *input_operand_indexes, int32_t output_operand_index, const void *parameters, NativeContext *ctx)
Execute the Depth to Space Layer.
Definition: dnn_backend_native_layer_depth2space.c:51
int32_t
int32_t
Definition: audioconvert.c:56
av_log
#define av_log(a,...)
Definition: tableprint_vlc.h:27
av_realloc
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
Definition: mem.c:153