| @@ -64,6 +64,10 @@ static DNNReturnType set_input_output_native(void *model, DNNInputData *input, c · 63 unmodified lines |
| 64 | 64 | DNNModel *ff_dnn_load_model_native(const char *model_filename) |
| 65 | 65 | { |
| 66 | 66 | DNNModel *model = NULL; |
| 67 | + char header_expected[] = "FFMPEGDNNNATIVE"; |
| 68 | + char *buf; |
| 69 | + size_t size; |
| 70 | + int version, header_size, major_version_expected = 0; |
| 67 | 71 | ConvolutionalNetwork *network = NULL; |
| 68 | 72 | AVIOContext *model_file_context; |
| 69 | 73 | int file_size, dnn_size, kernel_size, i; |
| @@ -84,6 +88,41 @@ DNNModel *ff_dnn_load_model_native(const char *model_filename) · 14 unmodified lines |
| 84 | 88 | } |
| 85 | 89 | file_size = avio_size(model_file_context); |
| 86 | 90 | |
| 91 | + /** |
| 92 | + * check file header with string and version |
| 93 | + */ |
| 94 | + size = sizeof(header_expected); |
| 95 | + buf = av_malloc(size); |
| 96 | + if (!buf) { |
| 97 | + avio_closep(&model_file_context); |
| 98 | + av_freep(&model); |
| 99 | + return NULL; |
| 100 | + } |
| 101 | + |
| 102 | + // size - 1 to skip the ending '\0' which is not saved in file |
| 103 | + avio_get_str(model_file_context, size - 1, buf, size); |
| 104 | + dnn_size = size - 1; |
| 105 | + if (strncmp(buf, header_expected, size) != 0) { |
| 106 | + av_freep(&buf); |
| 107 | + avio_closep(&model_file_context); |
| 108 | + av_freep(&model); |
| 109 | + return NULL; |
| 110 | + } |
| 111 | + av_freep(&buf); |
| 112 | + |
| 113 | + version = (int32_t)avio_rl32(model_file_context); |
| 114 | + dnn_size += 4; |
| 115 | + if (version != major_version_expected) { |
| 116 | + avio_closep(&model_file_context); |
| 117 | + av_freep(&model); |
| 118 | + return NULL; |
| 119 | + } |
| 120 | + |
| 121 | + // currently no need to check minor version |
| 122 | + version = (int32_t)avio_rl32(model_file_context); |
| 123 | + dnn_size += 4; |
| 124 | + header_size = dnn_size; |
| 125 | + |
| 87 | 126 | network = av_mallocz(sizeof(ConvolutionalNetwork)); |
| 88 | 127 | if (!network){ |
| 89 | 128 | avio_closep(&model_file_context); |
| @@ -95,8 +134,8 @@ DNNModel *ff_dnn_load_model_native(const char *model_filename) · 5 unmodified lines |
| 95 | 134 | avio_seek(model_file_context, file_size - 8, SEEK_SET); |
| 96 | 135 | network->layers_num = (int32_t)avio_rl32(model_file_context); |
| 97 | 136 | network->operands_num = (int32_t)avio_rl32(model_file_context); |
| 98 | | − dnn_size = 8; |
| 99 | | − avio_seek(model_file_context, 0, SEEK_SET); |
| 137 | + dnn_size += 8; |
| 138 | + avio_seek(model_file_context, header_size, SEEK_SET); |
| 100 | 139 | |
| 101 | 140 | network->layers = av_mallocz(network->layers_num * sizeof(Layer)); |
| 102 | 141 | if (!network->layers){ |