Skip to content

Commit 4c5fb81

Browse files
committed
Implement file decryption before object detection starts
Update comments and fix typos
1 parent 1db131f commit 4c5fb81

File tree

1 file changed

+118
-9
lines changed

1 file changed

+118
-9
lines changed

src/main.cpp

Lines changed: 118 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
/*
22
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.
56
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.
79
810
AUTHORS
911
@@ -13,7 +15,7 @@ COPYRIGHT AND LICENSING
1315
1416
See the `LICENSE_MIT.markdown` file in the example's root directory for
1517
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
1719
*/
1820

1921
extern "C"
@@ -22,13 +24,20 @@ extern "C"
2224
}
2325
#include "codec_def.h"
2426
#include "h264dec.h"
27+
#include "mbedtls/cipher.h"
2528
#include "utils.h"
2629

2730
#include <string.h>
2831

2932

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+
3039
/* Keep track of the number of frames processed */
31-
int frames_processed = 0;
40+
unsigned int frames_processed = 0;
3241

3342
/* Network state, to be initialized by `init_darknet_detector()` */
3443
char **names;
@@ -138,7 +147,7 @@ void on_frame_ready(SBufferInfo *bufInfo)
138147

139148
im = load_image_from_raw_yuv(bufInfo);
140149

141-
// Resize image to fit the darknet model
150+
// Resize image to fit the Darknet model
142151
im_sized = letterbox_image(im, net->w, net->h);
143152

144153
printf("Image normalized and resized: %lf seconds\n",
@@ -155,12 +164,111 @@ void on_frame_ready(SBufferInfo *bufInfo)
155164
frames_processed++;
156165
}
157166

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+
158256
/* Run the object detection model on each decoded frame */
159257
int main(int argc, char **argv)
160258
{
161259
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";
163264

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
164272
printf("Initializing detector...\n");
165273
time = what_time_is_it_now();
166274
// XXX: Box annotation is temporarily disabled until we find a way to
@@ -170,11 +278,12 @@ int main(int argc, char **argv)
170278
printf("Arguments loaded and network parsed: %lf seconds\n",
171279
what_time_is_it_now() - time);
172280

281+
// Decode video and run object detection on each frame
173282
printf("Starting decoding...\n");
174283
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);
176285
printf("Finished decoding: %lf seconds\n",
177-
what_time_is_it_now() - time);
286+
what_time_is_it_now() - time);
178287

179288
return x;
180289
}

0 commit comments

Comments
 (0)