summaryrefslogtreecommitdiff
path: root/src/advtime.c
blob: 07be68cf06014eb99637db0b1a3a528d2f7d95c3 (plain)
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
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#include "video_decode.h"

static void
pgm_save(unsigned char *buf, int wrap, int xsize, int ysize, char *filename)
{
	FILE *f;
	int i;

	f = fopen(filename, "w");
	fprintf(f, "P5\n%d %d\n%d\n", xsize, ysize, 255);
	for(i=0; i < ysize; i++)
		fwrite(buf + i * wrap, 1, xsize, f);
	fclose(f);
}

int64_t
frame_average(struct video_frame *frame)
{
	int64_t average = 0;
	uint8_t *row = frame->data;
	uint8_t *col = NULL;

	for (int y = 0; y < frame->height; ++y) {
		col = row;
		for (int x = 0; x < frame->width; ++x) {
			average += *col;
			col++;
		}
		row += frame->stride;
	}

	return average / (frame->width * frame->height);
}

int
main(int argc, char **argv)
{
	struct video_decode *vd;
	struct video_frame *frame;
	int ret;
	int64_t timestamp = 0;

	if (argc < 2)
		return 1;

	if (argc == 3 && strtol(argv[2], NULL, 10) > 0)
		timestamp = strtol(argv[2], NULL, 10);

	ret = video_decode_init(&vd, argv[1], timestamp);
	if (ret < 0)
		return -ret;

	while (video_decode_get_frame(vd, &frame) == 0) {
		printf("%d\n", frame_average(frame));
		video_decode_free_frame(&frame);
	}

//	pgm_save(frame->data, frame->stride, frame->width, frame->height,
//		 argv[2]);

	video_decode_uninit(&vd);
}