1
1
/*
2
2
This file contains the main functions for performing object detection.
3
- First, the object detection model is loaded, then the video decoder executes
4
- until every frame in the video is decoded.
3
+ First, the input video is decrypted by the AES native module, then the object
4
+ detection model is loaded, then the video decoder executes until every frame in
5
+ the video is decoded.
5
6
A callback is configured to be called whenever a frame is available, whereupon
6
- it is fed to the object detection model which outputs a prediction.
7
+ it is fed to the object detection model which outputs a prediction and
8
+ optionally saves it to disk.
7
9
8
10
AUTHORS
9
11
@@ -13,7 +15,7 @@ COPYRIGHT AND LICENSING
13
15
14
16
See the `LICENSE_MIT.markdown` file in the example's root directory for
15
17
copyright and licensing information.
16
- Based on darknet , YOLO LICENSE https://github.com/pjreddie/darknet/blob/master/LICENSE
18
+ Based on Darknet , YOLO LICENSE https://github.com/pjreddie/darknet/blob/master/LICENSE
17
19
*/
18
20
19
21
extern " C"
@@ -22,13 +24,20 @@ extern "C"
22
24
}
23
25
#include " codec_def.h"
24
26
#include " h264dec.h"
27
+ #include " mbedtls/cipher.h"
25
28
#include " utils.h"
26
29
27
30
#include < string.h>
28
31
29
32
33
+ /* Cipher's key length in bits */
34
+ unsigned int KEY_LENGTH = 128 ;
35
+
36
+ /* Cipher's block size in bits */
37
+ unsigned int BLOCK_SIZE = 128 ;
38
+
30
39
/* Keep track of the number of frames processed */
31
- int frames_processed = 0 ;
40
+ unsigned int frames_processed = 0 ;
32
41
33
42
/* Network state, to be initialized by `init_darknet_detector()` */
34
43
char **names;
@@ -138,7 +147,7 @@ void on_frame_ready(SBufferInfo *bufInfo)
138
147
139
148
im = load_image_from_raw_yuv (bufInfo);
140
149
141
- // Resize image to fit the darknet model
150
+ // Resize image to fit the Darknet model
142
151
im_sized = letterbox_image (im, net->w , net->h );
143
152
144
153
printf (" Image normalized and resized: %lf seconds\n " ,
@@ -155,12 +164,111 @@ void on_frame_ready(SBufferInfo *bufInfo)
155
164
frames_processed++;
156
165
}
157
166
167
+ int decrypt_video (char *encrypted_video_path, char *decrypted_video_path, char *key_path, char *iv_path)
168
+ {
169
+ FILE *f;
170
+ size_t n;
171
+ long input_file_size;
172
+ unsigned char key[KEY_LENGTH / 8 ];
173
+ unsigned char iv[BLOCK_SIZE / 8 ];
174
+ unsigned char *input_buffer, *output_buffer;
175
+ size_t output_len;
176
+
177
+ // Read key
178
+ f = fopen (key_path, " r" );
179
+ if (f == NULL ) {
180
+ printf (" Couldn't open %s\n " , key_path);
181
+ return 1 ;
182
+ }
183
+ n = fread (key, sizeof (key), 1 , f);
184
+ if (n != 1 ) {
185
+ printf (" Invalid key length. Should be %d bits long\n " , KEY_LENGTH);
186
+ return 1 ;
187
+ }
188
+ fclose (f);
189
+
190
+ // Read IV
191
+ f = fopen (iv_path, " r" );
192
+ if (f == NULL ) {
193
+ printf (" Couldn't open %s\n " , iv_path);
194
+ return 1 ;
195
+ }
196
+ n = fread (iv, sizeof (iv), 1 , f);
197
+ if (n != 1 ) {
198
+ printf (" Invalid IV length. Should be %d bits long\n " , BLOCK_SIZE);
199
+ return 1 ;
200
+ }
201
+ fclose (f);
202
+
203
+ // Determine input file size
204
+ f = fopen (encrypted_video_path, " r" );
205
+ if (f == NULL ) {
206
+ printf (" Couldn't open %s\n " , encrypted_video_path);
207
+ return 1 ;
208
+ }
209
+ fseek (f, 0L , SEEK_END);
210
+ input_file_size = ftell (f);
211
+ rewind (f);
212
+
213
+ // Allocate input buffer the size of the input file
214
+ input_buffer = (unsigned char *) malloc (input_file_size);
215
+
216
+ // Read input file
217
+ n = fread (input_buffer, input_file_size, 1 , f);
218
+ if (n != 1 ) {
219
+ printf (" Failure reading %s\n " , encrypted_video_path);
220
+ return 1 ;
221
+ }
222
+ fclose (f);
223
+
224
+ // Allocate output buffer the size of the input buffer (can't be longer than
225
+ // that due to padding)
226
+ output_buffer = (unsigned char *) malloc (input_file_size);
227
+
228
+ // Initialize decryption context and decrypt buffer
229
+ mbedtls_cipher_context_t ctx;
230
+ mbedtls_cipher_type_t type = MBEDTLS_CIPHER_AES_128_CTR;
231
+ mbedtls_cipher_init (&ctx);
232
+ mbedtls_cipher_setup (&ctx, mbedtls_cipher_info_from_type (type));
233
+ mbedtls_cipher_setkey (&ctx, key, KEY_LENGTH, MBEDTLS_DECRYPT);
234
+ mbedtls_cipher_crypt (&ctx, iv, BLOCK_SIZE / 8 , input_buffer, input_file_size, output_buffer, &output_len);
235
+
236
+ free (input_buffer);
237
+
238
+ // Write result to `decrypted_video_path`
239
+ f = fopen (decrypted_video_path, " w" );
240
+ if (f == NULL ) {
241
+ printf (" Couldn't open %s\n " , decrypted_video_path);
242
+ return 1 ;
243
+ }
244
+ n = fwrite (output_buffer, output_len, 1 , f);
245
+ if (n != 1 ) {
246
+ printf (" Failure writing %s\n " , decrypted_video_path);
247
+ return 1 ;
248
+ }
249
+ fclose (f);
250
+
251
+ free (output_buffer);
252
+
253
+ return 0 ;
254
+ }
255
+
158
256
/* Run the object detection model on each decoded frame */
159
257
int main (int argc, char **argv)
160
258
{
161
259
double time;
162
- char *input_file = " video_input/in.h264" ;
260
+ char *encrypted_video_path = " input/in_enc.h264" ;
261
+ char *decrypted_video_path = " internal/in.h264" ;
262
+ char *key_path = " input/key" ;
263
+ char *iv_path = " input/iv" ;
163
264
265
+ // Decrypt input video
266
+ if (decrypt_video (encrypted_video_path, decrypted_video_path, key_path, iv_path) != 0 ) {
267
+ printf (" Couldn't decrypt %s\n " , encrypted_video_path);
268
+ return 1 ;
269
+ }
270
+
271
+ // Initialize Darknet
164
272
printf (" Initializing detector...\n " );
165
273
time = what_time_is_it_now ();
166
274
// XXX: Box annotation is temporarily disabled until we find a way to
@@ -170,11 +278,12 @@ int main(int argc, char **argv)
170
278
printf (" Arguments loaded and network parsed: %lf seconds\n " ,
171
279
what_time_is_it_now () - time);
172
280
281
+ // Decode video and run object detection on each frame
173
282
printf (" Starting decoding...\n " );
174
283
time = what_time_is_it_now ();
175
- int x = h264_decode (input_file , " " , false , &on_frame_ready);
284
+ int x = h264_decode (decrypted_video_path , " " , false , &on_frame_ready);
176
285
printf (" Finished decoding: %lf seconds\n " ,
177
- what_time_is_it_now () - time);
286
+ what_time_is_it_now () - time);
178
287
179
288
return x;
180
289
}
0 commit comments