forked from NVIDIA/nvvl
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extract_frames_c.c
67 lines (54 loc) · 1.94 KB
/
extract_frames_c.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <cuda_runtime.h>
#include "VideoLoader.h"
void write_frames(PictureSequenceHandle sequence) {
struct NVVL_PicLayer pixels = nvvl_get_layer(sequence, PDT_BYTE, "pixels");
printf("Got a sequence of size: %d\n", pixels.desc.count);
for (int i = 0; i < pixels.desc.count; i++) {
uint8_t* p = pixels.data + i*pixels.desc.stride.n;
uint8_t tmp[100];
if (cudaMemcpy(tmp, p, 100, cudaMemcpyDeviceToHost) != cudaSuccess) {
fprintf(stderr, "Couldn't copy frame data to cpu\n");
exit(1);
}
uint32_t sum = 0;
for (int i = 0; i < 100; i++) {
sum += tmp[i];
}
printf(" Frame %d sum (first 100): %d\n", i, sum);
}
}
int main(int argc, char** argv) {
if (argc < 2) {
printf("usage: %s <video file>\n", argv[0]);
return -1;
}
const uint16_t sequence_width = 400;
const uint16_t sequence_height = 400;
const uint16_t sequence_count = 10;
VideoLoaderHandle loader = nvvl_create_video_loader(0);
PictureSequenceHandle sequence = nvvl_create_sequence(sequence_count);
struct NVVL_PicLayer b;
memset(&b, 0, sizeof(b));
b.desc.count = sequence_count;
b.desc.channels = 3;
b.desc.width = sequence_width;
b.desc.height = sequence_height;
b.type = PDT_BYTE;
if(cudaMallocPitch(&b.data, &b.desc.stride.y, b.desc.width, b.desc.height * sequence_count * 3) != cudaSuccess) {
fprintf(stderr, "Unable to allocate buffer in device memory\n");
exit(1);
}
b.desc.stride.x = 1;
b.desc.stride.c = b.desc.stride.y*b.desc.height;
b.desc.stride.n = b.desc.stride.c*3;
nvvl_set_layer(sequence, &b, "pixels");
nvvl_read_sequence(loader, argv[1], 0, sequence_count);
nvvl_receive_frames_sync(loader, sequence);
write_frames(sequence);
nvvl_destroy_video_loader(loader);
return 0;
}