summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJan Klemkow <j.klemkow@wemelug.de>2012-11-14 17:16:16 +0100
committerJan Klemkow <j.klemkow@wemelug.de>2012-11-14 17:16:16 +0100
commitbfcb903f8ff64c55cf3b4d0789369be495e5dac8 (patch)
tree63992459c6bdb527fac0129ba0ce5288ead0fd12 /src
parent871f4383c4295b2796f46c7faee1a5056b5c95c3 (diff)
downloadadvtime-bfcb903f8ff64c55cf3b4d0789369be495e5dac8.tar.gz
advtime-bfcb903f8ff64c55cf3b4d0789369be495e5dac8.tar.bz2
advtime-bfcb903f8ff64c55cf3b4d0789369be495e5dac8.zip
Add frame diff calculation and getopt.
Diffstat (limited to 'src')
-rw-r--r--src/advtime.c86
1 files changed, 71 insertions, 15 deletions
diff --git a/src/advtime.c b/src/advtime.c
index 5dbe6d7..6144cc3 100644
--- a/src/advtime.c
+++ b/src/advtime.c
@@ -1,13 +1,37 @@
+#define _XOPEN_SOURCE 500
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
+#include <unistd.h>
#include "video_decode.h"
-static int64_t
+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)
{
- int64_t average = 0;
+ double average = 0;
uint8_t *row = frame->data;
uint8_t *col = NULL;
@@ -23,31 +47,63 @@ frame_average(struct video_frame *frame)
return average / (frame->width * frame->height);
}
+static void
+usage(void)
+{
+ fprintf(stderr, "advtime [-a] [-d] FILE\n");
+ exit(EXIT_FAILURE);
+}
+
int
main(int argc, char **argv)
{
struct video_decode *vd;
- struct video_frame *frame;
- int ret;
+ struct video_frame *frame_a;
+ struct video_frame *frame_b;
+ int ret, ch;
+ int diff_flag = 0, average_flag = 0;
int64_t timestamp = 0;
- if (argc < 2)
- return 1;
+ while ((ch = getopt(argc, argv, "ad")) != -1) {
+ switch (ch) {
+ case 'a':
+ average_flag = 1;
+ break;
+ case 'd':
+ diff_flag = 1;
+ break;
+ default:
+ usage();
+ /* NOTREACHED */
+ }
+ }
+ argc -= optind;
+ argv += optind;
+
+ if (argc < 1)
+ usage();
- if (argc == 3 && strtol(argv[2], NULL, 10) > 0)
- timestamp = strtol(argv[2], NULL, 10);
+ if (argc == 2 && strtol(argv[1], NULL, 10) > 0)
+ timestamp = strtol(argv[1], NULL, 10);
- ret = video_decode_init(&vd, argv[1], timestamp);
+ ret = video_decode_init(&vd, argv[0], timestamp);
if (ret < 0)
return -ret;
- while (video_decode_get_frame(vd, &frame) == 0) {
- printf("%ld\n", frame_average(frame));
- video_decode_free_frame(&frame);
- }
+ ret = video_decode_get_frame(vd, &frame_a);
+ if (ret < 0)
+ return -ret;
-// pgm_save(frame->data, frame->stride, frame->width, frame->height,
-// argv[2]);
+ while (video_decode_get_frame(vd, &frame_b) == 0) {
+ if (average_flag == 1)
+ printf("%f\n", frame_average(frame_a));
+ if (diff_flag == 1)
+ printf("%f\n", frame_diff(frame_a, frame_b));
+ video_decode_free_frame(&frame_a);
+ frame_a = frame_b;
+ }
+ video_decode_free_frame(&frame_b);
video_decode_uninit(&vd);
+ return EXIT_SUCCESS;
}