]> git.sesse.net Git - ffmpeg/blob - libavfilter/dnn/dnn_backend_native_layer_avgpool.c
avformat/mpegtsenc: move is_dvb_subtitle/is_dvb_teletext initialization upwards
[ffmpeg] / libavfilter / dnn / dnn_backend_native_layer_avgpool.c
1 /*
2  * Copyright (c) 2020
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 "libavutil/avassert.h"
27 #include "dnn_backend_native_layer_avgpool.h"
28
29 int ff_dnn_load_layer_avg_pool(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num)
30 {
31     AvgPoolParams *avgpool_params;
32     int dnn_size = 0;
33     avgpool_params = av_malloc(sizeof(*avgpool_params));
34     if(!avgpool_params)
35         return 0;
36
37     avgpool_params->strides = (int32_t)avio_rl32(model_file_context);
38     avgpool_params->padding_method = (int32_t)avio_rl32(model_file_context);
39     avgpool_params->kernel_size = (int32_t)avio_rl32(model_file_context);
40     dnn_size += 12;
41
42     if (dnn_size > file_size || avgpool_params->kernel_size <= 0 || avgpool_params->strides <=0){
43         av_freep(&avgpool_params);
44         return 0;
45     }
46
47     layer->params = avgpool_params;
48     layer->input_operand_indexes[0] = (int32_t)avio_rl32(model_file_context);
49     layer->output_operand_index = (int32_t)avio_rl32(model_file_context);
50     dnn_size += 8;
51
52     if (layer->input_operand_indexes[0] >= operands_num || layer->output_operand_index >= operands_num) {
53         return 0;
54     }
55     return dnn_size;
56 }
57
58 int ff_dnn_execute_layer_avg_pool(DnnOperand *operands, const int32_t *input_operand_indexes,
59                                   int32_t output_operand_index, const void *parameters, NativeContext *ctx)
60 {
61     float *output;
62     int height_end, width_end, height_radius, width_radius, output_height, output_width, kernel_area;
63     int32_t input_operand_index = input_operand_indexes[0];
64     int number = operands[input_operand_index].dims[0];
65     int height = operands[input_operand_index].dims[1];
66     int width = operands[input_operand_index].dims[2];
67     int channel = operands[input_operand_index].dims[3];
68     const float *input = operands[input_operand_index].data;
69     const AvgPoolParams *avgpool_params = parameters;
70
71     int kernel_strides = avgpool_params->strides;
72     int src_linesize = width * channel;
73     DnnOperand *output_operand = &operands[output_operand_index];
74
75     /**
76      * When padding_method = SAME, the tensorflow will only padding the hald number of 0 pxiels
77      * except the remainders.
78      * Eg: assuming the input height = 1080, the strides = 11, so the remainders = 1080 % 11 = 2
79      *     and if ksize = 5: it will fill (5 - 2) >> 1 = 1 line before the first line of input image,
80      *                       and 5 - 2 - 1 = 2 lines after the last line of input image.
81      *     and if ksize = 7: it will fill (7 - 2) >> 1 = 2 lines before the first line of input image,
82      *                       and 7 - 2 - 2 = 3 lines after the last line of input image.
83      */
84     if (avgpool_params->padding_method == SAME) {
85         height_end = height;
86         width_end = width;
87         height_radius = avgpool_params->kernel_size - ((height - 1) % kernel_strides + 1);
88         width_radius = avgpool_params->kernel_size - ((width - 1) % kernel_strides + 1);
89         height_radius = height_radius < 0 ? 0 : height_radius >> 1;
90         width_radius = width_radius < 0 ? 0 : width_radius >> 1;
91         output_height = ceil(height / (kernel_strides * 1.0));
92         output_width = ceil(width / (kernel_strides * 1.0));
93     } else {
94         av_assert0(avgpool_params->padding_method == VALID);
95         height_end = height - avgpool_params->kernel_size + 1;
96         width_end = width - avgpool_params->kernel_size + 1;
97         height_radius = 0;
98         width_radius = 0;
99         output_height = ceil((height - avgpool_params->kernel_size + 1) / (kernel_strides * 1.0));
100         output_width = ceil((width - avgpool_params->kernel_size + 1) / (kernel_strides * 1.0));
101     }
102
103     output_operand->dims[0] = number;
104     output_operand->dims[1] = output_height;
105     output_operand->dims[2] = output_width;
106     // not support pooling in channel dimension now
107     output_operand->dims[3] = channel;
108     output_operand->data_type = operands[input_operand_index].data_type;
109     output_operand->length = ff_calculate_operand_data_length(output_operand);
110     if (output_operand->length <= 0) {
111         av_log(ctx, AV_LOG_ERROR, "The output data length overflow\n");
112         return DNN_ERROR;
113     }
114     output_operand->data = av_realloc(output_operand->data, output_operand->length);
115     if (!output_operand->data) {
116         av_log(ctx, AV_LOG_ERROR, "Failed to reallocate memory for output\n");
117         return DNN_ERROR;
118     }
119     output = output_operand->data;
120
121     for (int y = 0; y < height_end; y += kernel_strides) {
122         for (int x = 0; x < width_end; x += kernel_strides) {
123             for (int n_channel = 0; n_channel < channel; ++n_channel) {
124                 output[n_channel] = 0.0;
125                 kernel_area = 0;
126                 for (int kernel_y = 0; kernel_y < avgpool_params->kernel_size; ++kernel_y) {
127                     for (int kernel_x = 0; kernel_x < avgpool_params->kernel_size; ++kernel_x) {
128                         float input_pel;
129                         int y_pos = y + (kernel_y - height_radius);
130                         int x_pos = x + (kernel_x - width_radius);
131                         if (x_pos < 0 || x_pos >= width || y_pos < 0 || y_pos >= height) {
132                             input_pel = 0.0;
133                         } else {
134                             kernel_area++;
135                             input_pel = input[y_pos * src_linesize + x_pos * channel + n_channel];
136                         }
137                         output[n_channel] += input_pel;
138                     }
139                 }
140                 output[n_channel] /= kernel_area;
141             }
142             output += channel;
143         }
144     }
145
146     return 0;
147 }