GCC Code Coverage Report
Directory: ../../../ffmpeg/ Exec Total Coverage
File: src/libavfilter/dnn/dnn_backend_native_layer_mathunary.c Lines: 79 97 81.4 %
Date: 2020-08-14 10:39:37 Branches: 52 61 85.2 %

Line Branch Exec Source
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 <math.h>
27
28
#include "dnn_backend_native.h"
29
#include "libavutil/avassert.h"
30
#include "dnn_backend_native_layer_mathunary.h"
31
32
int dnn_load_layer_math_unary(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num)
33
{
34
    DnnLayerMathUnaryParams *params;
35
    int dnn_size = 0;
36
    params = av_malloc(sizeof(*params));
37
    if(!params)
38
        return 0;
39
40
    params->un_op = (int32_t)avio_rl32(model_file_context);
41
    dnn_size += 4;
42
    layer->params = params;
43
    layer->input_operand_indexes[0] = (int32_t)avio_rl32(model_file_context);
44
    layer->output_operand_index = (int32_t)avio_rl32(model_file_context);
45
    dnn_size += 8;
46
47
    if (layer->input_operand_indexes[0] >= operands_num || layer->output_operand_index >= operands_num) {
48
        return 0;
49
    }
50
51
    return dnn_size;
52
53
}
54
55
16
int dnn_execute_layer_math_unary(DnnOperand *operands, const int32_t *input_operand_indexes,
56
                                int32_t output_operand_index, const void *parameters)
57
{
58
16
    const DnnOperand *input = &operands[input_operand_indexes[0]];
59
16
    DnnOperand *output = &operands[output_operand_index];
60
16
    const DnnLayerMathUnaryParams *params = (const DnnLayerMathUnaryParams *)parameters;
61
    int dims_count;
62
    const float *src;
63
    float *dst;
64
65
80
    for (int i = 0; i < 4; ++i)
66
64
        output->dims[i] = input->dims[i];
67
68
16
    output->data_type = input->data_type;
69
16
    output->length = calculate_operand_data_length(output);
70
16
    if (output->length <= 0)
71
        return DNN_ERROR;
72
16
    output->data = av_realloc(output->data, output->length);
73
16
    if (!output->data)
74
        return DNN_ERROR;
75
76
16
    dims_count = calculate_operand_dims_count(output);
77
16
    src = input->data;
78
16
    dst = output->data;
79
80




16
    switch (params->un_op) {
81
1
    case DMUO_ABS:
82
10
        for (int i = 0; i < dims_count; ++i)
83
9
            dst[i] = FFABS(src[i]);
84
1
        return 0;
85
1
    case DMUO_SIN:
86
10
        for (int i = 0; i < dims_count; ++i)
87
9
            dst[i] = sin(src[i]);
88
1
        return 0;
89
1
    case DMUO_COS:
90
10
        for (int i = 0; i < dims_count; ++i)
91
9
            dst[i] = cos(src[i]);
92
1
        return 0;
93
1
    case DMUO_TAN:
94
10
        for (int i = 0; i < dims_count; ++i)
95
9
            dst[i] = tan(src[i]);
96
1
        return 0;
97
1
    case DMUO_ASIN:
98
10
        for (int i = 0; i < dims_count; ++i)
99
9
            dst[i] = asin(src[i]);
100
1
        return 0;
101
1
    case DMUO_ACOS:
102
10
        for (int i = 0; i < dims_count; ++i)
103
9
            dst[i] = acos(src[i]);
104
1
        return 0;
105
1
    case DMUO_ATAN:
106
10
        for (int i = 0; i < dims_count; ++i)
107
9
            dst[i] = atan(src[i]);
108
1
        return 0;
109
1
    case DMUO_SINH:
110
10
        for (int i = 0; i < dims_count; ++i)
111
9
            dst[i] = sinh(src[i]);
112
1
        return 0;
113
1
    case DMUO_COSH:
114
10
        for (int i = 0; i < dims_count; ++i)
115
9
            dst[i] = cosh(src[i]);
116
1
        return 0;
117
1
    case DMUO_TANH:
118
10
        for (int i = 0; i < dims_count; ++i)
119
9
            dst[i] = tanh(src[i]);
120
1
        return 0;
121
1
    case DMUO_ASINH:
122
10
        for (int i = 0; i < dims_count; ++i)
123
9
            dst[i] = asinh(src[i]);
124
1
        return 0;
125
1
    case DMUO_ACOSH:
126
10
        for (int i = 0; i < dims_count; ++i)
127
9
            dst[i] = acosh(src[i]);
128
1
        return 0;
129
1
    case DMUO_ATANH:
130
10
        for (int i = 0; i < dims_count; ++i)
131
9
            dst[i] = atanh(src[i]);
132
1
        return 0;
133
1
    case DMUO_CEIL:
134
10
        for (int i = 0; i < dims_count; ++i)
135
9
            dst[i] = ceil(src[i]);
136
1
        return 0;
137
1
    case DMUO_FLOOR:
138
10
        for (int i = 0; i < dims_count; ++i)
139
9
            dst[i] = floor(src[i]);
140
1
        return 0;
141
1
    case DMUO_ROUND:
142
10
        for (int i = 0; i < dims_count; ++i)
143
9
            dst[i] = round(src[i]);
144
1
        return 0;
145
    default:
146
        return -1;
147
    }
148
}