#include "dnn_backend_native_layer_conv2d.h"
#include "dnn_backend_native_layers.h"
+static const AVClass dnn_native_class = {
+ .class_name = "dnn_native",
+ .item_name = av_default_item_name,
+ .option = NULL,
+ .version = LIBAVUTIL_VERSION_INT,
+ .category = AV_CLASS_CATEGORY_FILTER,
+};
+
static DNNReturnType get_input_native(void *model, DNNData *input, const char *input_name)
{
NativeModel *native_model = (NativeModel *)model;
+ NativeContext *ctx = &native_model->ctx;
for (int i = 0; i < native_model->operands_num; ++i) {
DnnOperand *oprd = &native_model->operands[i];
if (strcmp(oprd->name, input_name) == 0) {
- if (oprd->type != DOT_INPUT)
+ if (oprd->type != DOT_INPUT) {
+ av_log(ctx, AV_LOG_ERROR, "Found \"%s\" in model, but it is not input node\n", input_name);
return DNN_ERROR;
+ }
input->dt = oprd->data_type;
av_assert0(oprd->dims[0] == 1);
input->height = oprd->dims[1];
}
// do not find the input operand
+ av_log(ctx, AV_LOG_ERROR, "Could not find \"%s\" in model\n", input_name);
return DNN_ERROR;
}
static DNNReturnType set_input_native(void *model, DNNData *input, const char *input_name)
{
NativeModel *native_model = (NativeModel *)model;
+ NativeContext *ctx = &native_model->ctx;
DnnOperand *oprd = NULL;
- if (native_model->layers_num <= 0 || native_model->operands_num <= 0)
+ if (native_model->layers_num <= 0 || native_model->operands_num <= 0) {
+ av_log(ctx, AV_LOG_ERROR, "No operands or layers in model\n");
return DNN_ERROR;
+ }
/* inputs */
for (int i = 0; i < native_model->operands_num; ++i) {
oprd = &native_model->operands[i];
if (strcmp(oprd->name, input_name) == 0) {
- if (oprd->type != DOT_INPUT)
+ if (oprd->type != DOT_INPUT) {
+ av_log(ctx, AV_LOG_ERROR, "Found \"%s\" in model, but it is not input node\n", input_name);
return DNN_ERROR;
+ }
break;
}
oprd = NULL;
}
-
- if (!oprd)
+ if (!oprd) {
+ av_log(ctx, AV_LOG_ERROR, "Could not find \"%s\" in model\n", input_name);
return DNN_ERROR;
+ }
oprd->dims[0] = 1;
oprd->dims[1] = input->height;
av_freep(&oprd->data);
oprd->length = calculate_operand_data_length(oprd);
- if (oprd->length <= 0)
+ if (oprd->length <= 0) {
+ av_log(ctx, AV_LOG_ERROR, "The input data length overflow\n");
return DNN_ERROR;
+ }
oprd->data = av_malloc(oprd->length);
- if (!oprd->data)
+ if (!oprd->data) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to malloc memory for input data\n");
return DNN_ERROR;
+ }
input->data = oprd->data;
if (!native_model){
goto fail;
}
+
+ native_model->ctx.class = &dnn_native_class;
model->model = (void *)native_model;
avio_seek(model_file_context, file_size - 8, SEEK_SET);
DNNReturnType ff_dnn_execute_model_native(const DNNModel *model, DNNData *outputs, const char **output_names, uint32_t nb_output)
{
NativeModel *native_model = (NativeModel *)model->model;
+ NativeContext *ctx = &native_model->ctx;
int32_t layer;
- if (native_model->layers_num <= 0 || native_model->operands_num <= 0)
+ if (native_model->layers_num <= 0 || native_model->operands_num <= 0) {
+ av_log(ctx, AV_LOG_ERROR, "No operands or layers in model\n");
return DNN_ERROR;
- if (!native_model->operands[0].data)
+ }
+ if (!native_model->operands[0].data) {
+ av_log(ctx, AV_LOG_ERROR, "Empty model input data\n");
return DNN_ERROR;
+ }
for (layer = 0; layer < native_model->layers_num; ++layer){
DNNLayerType layer_type = native_model->layers[layer].type;
if (layer_funcs[layer_type].pf_exec(native_model->operands,
native_model->layers[layer].input_operand_indexes,
native_model->layers[layer].output_operand_index,
- native_model->layers[layer].params) == DNN_ERROR) {
+ native_model->layers[layer].params,
+ &native_model->ctx) == DNN_ERROR) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to execuet model\n");
return DNN_ERROR;
}
}
}
}
- if (oprd == NULL)
+ if (oprd == NULL) {
+ av_log(ctx, AV_LOG_ERROR, "Could not find output in model\n");
return DNN_ERROR;
+ }
outputs[i].data = oprd->data;
outputs[i].height = oprd->dims[1];
int height, width, channels;
} InputParams;
+typedef struct NativeContext {
+ const AVClass *class;
+} NativeContext;
+
// Represents simple feed-forward convolutional network.
typedef struct NativeModel{
+ NativeContext ctx;
Layer *layers;
int32_t layers_num;
DnnOperand *operands;
}
int dnn_execute_layer_avg_pool(DnnOperand *operands, const int32_t *input_operand_indexes,
- int32_t output_operand_index, const void *parameters)
+ int32_t output_operand_index, const void *parameters, NativeContext *ctx)
{
float *output;
int height_end, width_end, height_radius, width_radius, output_height, output_width, kernel_area;
output_operand->dims[3] = channel;
output_operand->data_type = operands[input_operand_index].data_type;
output_operand->length = calculate_operand_data_length(output_operand);
+ if (output_operand->length <= 0) {
+ av_log(ctx, AV_LOG_ERROR, "The output data length overflow\n");
+ return DNN_ERROR;
+ }
output_operand->data = av_realloc(output_operand->data, output_operand->length);
- if (!output_operand->data)
+ if (!output_operand->data) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to reallocate memory for output\n");
return DNN_ERROR;
+ }
output = output_operand->data;
for (int y = 0; y < height_end; y += kernel_strides) {
int dnn_load_layer_avg_pool(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num);
int dnn_execute_layer_avg_pool(DnnOperand *operands, const int32_t *input_operand_indexes,
- int32_t output_operand_index, const void *parameters);
+ int32_t output_operand_index, const void *parameters, NativeContext *ctx);
#endif
}
int dnn_execute_layer_conv2d(DnnOperand *operands, const int32_t *input_operand_indexes,
- int32_t output_operand_index, const void *parameters)
+ int32_t output_operand_index, const void *parameters, NativeContext *ctx)
{
float *output;
int32_t input_operand_index = input_operand_indexes[0];
output_operand->dims[3] = conv_params->output_num;
output_operand->data_type = operands[input_operand_index].data_type;
output_operand->length = calculate_operand_data_length(output_operand);
- if (output_operand->length <= 0)
+ if (output_operand->length <= 0) {
+ av_log(ctx, AV_LOG_ERROR, "The output data length overflow\n");
return DNN_ERROR;
+ }
output_operand->data = av_realloc(output_operand->data, output_operand->length);
- if (!output_operand->data)
+ if (!output_operand->data) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to reallocate memory for output\n");
return DNN_ERROR;
+ }
output = output_operand->data;
av_assert0(channel == conv_params->input_num);
int dnn_load_layer_conv2d(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num);
int dnn_execute_layer_conv2d(DnnOperand *operands, const int32_t *input_operand_indexes,
- int32_t output_operand_index, const void *parameters);
+ int32_t output_operand_index, const void *parameters, NativeContext *ctx);
#endif
}
int dnn_execute_layer_depth2space(DnnOperand *operands, const int32_t *input_operand_indexes,
- int32_t output_operand_index, const void *parameters)
+ int32_t output_operand_index, const void *parameters, NativeContext *ctx)
{
float *output;
const DepthToSpaceParams *params = (const DepthToSpaceParams *)parameters;
output_operand->dims[3] = new_channels;
output_operand->data_type = operands[input_operand_index].data_type;
output_operand->length = calculate_operand_data_length(output_operand);
- if (output_operand->length <= 0)
+ if (output_operand->length <= 0) {
+ av_log(ctx, AV_LOG_ERROR, "The output data length overflow\n");
return DNN_ERROR;
+ }
output_operand->data = av_realloc(output_operand->data, output_operand->length);
- if (!output_operand->data)
+ if (!output_operand->data) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to reallocate memory for output\n");
return DNN_ERROR;
+ }
output = output_operand->data;
for (y = 0; y < height; ++y){
int dnn_load_layer_depth2space(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num);
int dnn_execute_layer_depth2space(DnnOperand *operands, const int32_t *input_operand_indexes,
- int32_t output_operand_index, const void *parameters);
+ int32_t output_operand_index, const void *parameters, NativeContext *ctx);
#endif
}
int dnn_execute_layer_math_binary(DnnOperand *operands, const int32_t *input_operand_indexes,
- int32_t output_operand_index, const void *parameters)
+ int32_t output_operand_index, const void *parameters, NativeContext *ctx)
{
const DnnOperand *input = &operands[input_operand_indexes[0]];
DnnOperand *output = &operands[output_operand_index];
output->data_type = input->data_type;
output->length = calculate_operand_data_length(output);
- if (output->length <= 0)
+ if (output->length <= 0) {
+ av_log(ctx, AV_LOG_ERROR, "The output data length overflow\n");
return DNN_ERROR;
+ }
output->data = av_realloc(output->data, output->length);
- if (!output->data)
+ if (!output->data) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to reallocate memory for output\n");
return DNN_ERROR;
+ }
switch (params->bin_op) {
case DMBO_SUB:
math_binary_not_commutative(floormod, params, input, output, operands, input_operand_indexes);
return 0;
default:
+ av_log(ctx, AV_LOG_ERROR, "Unmatch math binary operator\n");
return DNN_ERROR;
}
}
int dnn_load_layer_math_binary(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num);
int dnn_execute_layer_math_binary(DnnOperand *operands, const int32_t *input_operand_indexes,
- int32_t output_operand_index, const void *parameters);
+ int32_t output_operand_index, const void *parameters, NativeContext *ctx);
#endif
}
int dnn_execute_layer_math_unary(DnnOperand *operands, const int32_t *input_operand_indexes,
- int32_t output_operand_index, const void *parameters)
+ int32_t output_operand_index, const void *parameters, NativeContext *ctx)
{
const DnnOperand *input = &operands[input_operand_indexes[0]];
DnnOperand *output = &operands[output_operand_index];
output->data_type = input->data_type;
output->length = calculate_operand_data_length(output);
- if (output->length <= 0)
+ if (output->length <= 0) {
+ av_log(ctx, AV_LOG_ERROR, "The output data length overflow\n");
return DNN_ERROR;
+ }
output->data = av_realloc(output->data, output->length);
- if (!output->data)
+ if (!output->data) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to reallocate memory for output\n");
return DNN_ERROR;
+ }
dims_count = calculate_operand_dims_count(output);
src = input->data;
dst[i] = round(src[i]);
return 0;
default:
+ av_log(ctx, AV_LOG_ERROR, "Unmatch math unary operator\n");
return DNN_ERROR;
}
}
int dnn_load_layer_math_unary(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num);
int dnn_execute_layer_math_unary(DnnOperand *operands, const int32_t *input_operand_indexes,
- int32_t output_operand_index, const void *parameters);
+ int32_t output_operand_index, const void *parameters, NativeContext *ctx);
#endif
}
int dnn_execute_layer_maximum(DnnOperand *operands, const int32_t *input_operand_indexes,
- int32_t output_operand_index, const void *parameters)
+ int32_t output_operand_index, const void *parameters, NativeContext *ctx)
{
const DnnOperand *input = &operands[input_operand_indexes[0]];
DnnOperand *output = &operands[output_operand_index];
output->data_type = input->data_type;
output->length = calculate_operand_data_length(output);
- if (output->length <= 0)
+ if (output->length <= 0) {
+ av_log(ctx, AV_LOG_ERROR, "The output data length overflow\n");
return DNN_ERROR;
+ }
output->data = av_realloc(output->data, output->length);
- if (!output->data)
+ if (!output->data) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to reallocate memory for output\n");
return DNN_ERROR;
+ }
dims_count = calculate_operand_dims_count(output);
src = input->data;
int dnn_load_layer_maximum(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num);
int dnn_execute_layer_maximum(DnnOperand *operands, const int32_t *input_operand_indexes,
- int32_t output_operand_index, const void *parameters);
+ int32_t output_operand_index, const void *parameters, NativeContext *ctx);
#endif
}
int dnn_execute_layer_pad(DnnOperand *operands, const int32_t *input_operand_indexes,
- int32_t output_operand_index, const void *parameters)
+ int32_t output_operand_index, const void *parameters, NativeContext *ctx)
{
int32_t before_paddings;
int32_t after_paddings;
output_operand->dims[3] = new_channel;
output_operand->data_type = operands[input_operand_index].data_type;
output_operand->length = calculate_operand_data_length(output_operand);
- if (output_operand->length <= 0)
+ if (output_operand->length <= 0) {
+ av_log(ctx, AV_LOG_ERROR, "The output data length overflow\n");
return DNN_ERROR;
+ }
output_operand->data = av_realloc(output_operand->data, output_operand->length);
- if (!output_operand->data)
+ if (!output_operand->data) {
+ av_log(ctx, AV_LOG_ERROR, "Failed to reallocate memory for output\n");
return DNN_ERROR;
+ }
output = output_operand->data;
// copy the original data
int dnn_load_layer_pad(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num);
int dnn_execute_layer_pad(DnnOperand *operands, const int32_t *input_operand_indexes,
- int32_t output_operand_index, const void *parameters);
+ int32_t output_operand_index, const void *parameters, NativeContext *ctx);
#endif
#include "dnn_backend_native.h"
typedef int (*LAYER_EXEC_FUNC)(DnnOperand *operands, const int32_t *input_operand_indexes,
- int32_t output_operand_index, const void *parameters);
+ int32_t output_operand_index, const void *parameters, NativeContext *ctx);
typedef int (*LAYER_LOAD_FUNC)(Layer *layer, AVIOContext *model_file_context, int file_size, int operands_num);
typedef struct LayerFunc {
operands[1].data = NULL;
input_indexes[0] = 0;
- dnn_execute_layer_avg_pool(operands, input_indexes, 1, ¶ms);
+ dnn_execute_layer_avg_pool(operands, input_indexes, 1, ¶ms, NULL);
output = operands[1].data;
for (int i = 0; i < sizeof(expected_output) / sizeof(float); ++i) {
operands[1].data = NULL;
input_indexes[0] = 0;
- dnn_execute_layer_avg_pool(operands, input_indexes, 1, ¶ms);
+ dnn_execute_layer_avg_pool(operands, input_indexes, 1, ¶ms, NULL);
output = operands[1].data;
for (int i = 0; i < sizeof(expected_output) / sizeof(float); ++i) {
operands[1].data = NULL;
input_indexes[0] = 0;
- dnn_execute_layer_conv2d(operands, input_indexes, 1, ¶ms);
+ dnn_execute_layer_conv2d(operands, input_indexes, 1, ¶ms, NULL);
output = operands[1].data;
for (int i = 0; i < sizeof(expected_output) / sizeof(float); i++) {
operands[1].data = NULL;
input_indexes[0] = 0;
- dnn_execute_layer_conv2d(operands, input_indexes, 1, ¶ms);
+ dnn_execute_layer_conv2d(operands, input_indexes, 1, ¶ms, NULL);
output = operands[1].data;
for (int i = 0; i < sizeof(expected_output) / sizeof(float); i++) {
input_indexes[0] = 0;
params.block_size = 2;
- dnn_execute_layer_depth2space(operands, input_indexes, 1, ¶ms);
+ dnn_execute_layer_depth2space(operands, input_indexes, 1, ¶ms, NULL);
output = operands[1].data;
for (int i = 0; i < sizeof(expected_output) / sizeof(float); i++) {
operands[1].data = NULL;
input_indexes[0] = 0;
- dnn_execute_layer_math_binary(operands, input_indexes, 1, ¶ms);
+ dnn_execute_layer_math_binary(operands, input_indexes, 1, ¶ms, NULL);
output = operands[1].data;
for (int i = 0; i < sizeof(input) / sizeof(float); i++) {
operands[1].data = NULL;
input_indexes[0] = 0;
- dnn_execute_layer_math_binary(operands, input_indexes, 1, ¶ms);
+ dnn_execute_layer_math_binary(operands, input_indexes, 1, ¶ms, NULL);
output = operands[1].data;
for (int i = 0; i < sizeof(input) / sizeof(float); i++) {
input_indexes[0] = 0;
input_indexes[1] = 1;
- dnn_execute_layer_math_binary(operands, input_indexes, 2, ¶ms);
+ dnn_execute_layer_math_binary(operands, input_indexes, 2, ¶ms, NULL);
output = operands[2].data;
for (int i = 0; i < sizeof(input0) / sizeof(float); i++) {
operands[1].data = NULL;
input_indexes[0] = 0;
- dnn_execute_layer_math_unary(operands, input_indexes, 1, ¶ms);
+ dnn_execute_layer_math_unary(operands, input_indexes, 1, ¶ms, NULL);
output = operands[1].data;
for (int i = 0; i < sizeof(input) / sizeof(float); ++i) {
operands[1].data = NULL;
input_indexes[0] = 0;
- dnn_execute_layer_maximum(operands, input_indexes, 1, ¶ms);
+ dnn_execute_layer_maximum(operands, input_indexes, 1, ¶ms, NULL);
output = operands[1].data;
for (int i = 0; i < sizeof(input) / sizeof(float); i++) {
operands[1].data = NULL;
input_indexes[0] = 0;
- dnn_execute_layer_pad(operands, input_indexes, 1, ¶ms);
+ dnn_execute_layer_pad(operands, input_indexes, 1, ¶ms, NULL);
output = operands[1].data;
for (int i = 0; i < sizeof(expected_output) / sizeof(float); i++) {
operands[1].data = NULL;
input_indexes[0] = 0;
- dnn_execute_layer_pad(operands, input_indexes, 1, ¶ms);
+ dnn_execute_layer_pad(operands, input_indexes, 1, ¶ms, NULL);
output = operands[1].data;
for (int i = 0; i < sizeof(expected_output) / sizeof(float); i++) {
operands[1].data = NULL;
input_indexes[0] = 0;
- dnn_execute_layer_pad(operands, input_indexes, 1, ¶ms);
+ dnn_execute_layer_pad(operands, input_indexes, 1, ¶ms, NULL);
output = operands[1].data;
for (int i = 0; i < sizeof(expected_output) / sizeof(float); i++) {