summaryrefslogtreecommitdiff
path: root/src/overlap.c
blob: a9d4bf7ab5acaa3fd8223c4872d6593e1ac7a398 (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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <inttypes.h>
#include <unistd.h>

#include "video_decode.h"
#include "util.h"

static void
usage(void)
{
	fprintf(stderr, "overlap [-dh] [-t DURATION] FIRST_VIDEO SECOND_VIDEO\n"
			"\n"
			"OPTIONS\n"
			"\t-d\tprint diff for every frame\n"
			"\t-h\tprint this help message\n"
			"\t-t\tmax duration to find overlap in milliseconds\n"
			"\t\t(default 60000 = 1 min)\n");

	exit(EXIT_FAILURE);
}

int
main(int argc, char **argv)
{
	struct video_decode *first_vd;
	struct video_decode *second_vd;
	struct video_frame *cur_frame = NULL, *last_frame = NULL;
	int ret, ch;
	int diff_flag = 0;
	int64_t timestamp = 0;
	int64_t duration = 60 * 1000;	/* duration for searching min_diff    */
					/* default 1 min = 60 sec * 1000 msec */
	int64_t min_diff_time = 0;	/* dts at overlaping frame */
	double min_diff, diff = 0.0;

	while ((ch = getopt(argc, argv, "dth")) != -1) {
		switch (ch) {
		case 'd':
			diff_flag = 1;	/* print diff of every frame */
			break;
		case 't':
			duration = strtol(optarg, NULL, 10);
			break;
		case 'h':
		default:
			usage();
			/* NOTREACHED */
		}
	}
	argc -= optind;
	argv += optind;

	if (argc < 2)
		usage();

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

	/* just get last frame of move */
	video_decode_seek(first_vd, first_vd->duration);
	for (int i = 0; video_decode_get_frame(first_vd, &last_frame) > 0; ++i);

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

	for (int i = 0; video_decode_get_frame(second_vd, &cur_frame) > 0; ++i){
		diff = frame_diff(last_frame, cur_frame);

		if (i == 0 || diff <= min_diff) {
			min_diff = diff;
			min_diff_time = cur_frame->dts;
		}

		if (diff_flag)
			printf("%" PRId64 " %f\n", cur_frame->dts, diff);

		if (cur_frame->dts > duration)
			break;
	}

	if (!diff_flag) {
		print_time(min_diff_time);
		printf("\n");
	}

	video_decode_free_frame(&cur_frame);
	video_decode_free_frame(&last_frame);
	video_decode_uninit(&first_vd);
	video_decode_uninit(&second_vd);
	return EXIT_SUCCESS;
}