summaryrefslogtreecommitdiff
path: root/src/advtime.c
blob: 1b0fa3d7501824edffb27d38a37f4d9a22c066ce (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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>

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

static inline void
pswap(void **p1, void **p2)
{
	void *tmp;

	tmp = *p1;
	*p1 = *p2;
	*p2 = tmp;
}

static void
start_new_szene(int64_t prev_szene_end, int64_t start)
{
	static int prev_dts_start = 0;

	print_time(prev_dts_start);
	printf(" ");
	print_time(prev_szene_end - prev_dts_start);
	prev_dts_start = start;
}

static double
frame_diff(struct video_frame *frame_a, struct video_frame *frame_b)
{
	double average = 0;
	uint8_t *row_a = frame_a->data, *col_a = NULL;
	uint8_t *row_b = frame_b->data, *col_b = NULL;

	for (int y = 0; y < frame_a->height; ++y) {
		col_a = row_a;
		col_b = row_b;
		for (int x = 0; x < frame_a->width; ++x) {
			average += abs(*col_a - *col_b);
			col_a++;
			col_b++;
		}
		row_a += frame_a->stride;
		row_b += frame_b->stride;
	}

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

static double
frame_average(struct video_frame *frame)
{
	double 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);
}

static void
usage(void)
{
	fprintf(stderr,
		"advtime [-adfs] FILE\n"
		"\n"
		"OPTIONS\n"
		"\t-a\tcalculate average color of a frame\n"
		"\t-d\tcalculate difference betweenprev and next frame\n"
		"\t-f\tprint frame number\n"
		"\t-s\tsave frame from detected cut\n");

	exit(EXIT_FAILURE);
}

int
main(int argc, char **argv)
{
	struct video_decode *vd;
	struct video_frame *frame_a = NULL, *frame_b = NULL;
	int ret, ch;
	int diff_flag = 0, average_flag = 0, frame_flag = 0, showcut_flag = 0;
	int64_t timestamp = 0;
	double diff = 0.0, old_diff = 0.0;
	char filename[BUFSIZ];

	while ((ch = getopt(argc, argv, "adfs")) != -1) {
		switch (ch) {
		case 'a':
			average_flag = 1;
			break;
		case 'd':
			diff_flag = 1;
			break;
		case 'f':
			frame_flag = 1;
			break;
		case 's':
			showcut_flag = 1;
			break;
		default:
			usage();
			/* NOTREACHED */
		}
	}
	argc -= optind;
	argv += optind;

	if (argc < 1)
		usage();

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

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

	ret = video_decode_get_frame(vd, &frame_a);
	if (ret < 0)
		return -ret;

	for (int i = 0; video_decode_get_frame(vd, &frame_b) > 0; ++i) {

		if (frame_flag)		/* print frame number */
			printf("%d\t", i);

		if (average_flag)		/* print frame average */
			printf("%f", frame_average(frame_a));

		if (average_flag && diff_flag)
			printf("\t");

		if (diff_flag) {
			diff = frame_diff(frame_a, frame_b);
//			printf("%f", diff);
//			printf("\t%f", (diff - old_diff));
			old_diff = diff;
		}

		if (diff > 40.0) {
			start_new_szene(frame_a->dts, frame_b->dts);

			if (!frame_flag && !average_flag) {
				printf("\n");
				fflush(stdout);
			}

			if (showcut_flag == 1) {
				snprintf(filename, BUFSIZ, "img/%04i.pgm", i);
				frame_mix(frame_a, frame_b);

				if (pgm_save(frame_a->data, frame_a->stride,
					     frame_a->width, frame_a->height,
					     filename) < 0)
					exit(EXIT_FAILURE);
			}
		}

		if (frame_flag || average_flag) {
			printf("\n");
			fflush(stdout);
		}

		pswap((void **)&frame_a, (void **)&frame_b);
	}

	if (diff_flag) {
		/* acutally we just finalize the last frame here */
		start_new_szene(frame_a->dts, 0);
		printf("\n");
	}

	video_decode_free_frame(&frame_a);
	if (frame_b)
		video_decode_free_frame(&frame_b);
	video_decode_uninit(&vd);
	return EXIT_SUCCESS;
}