commit 45ac53d25ba0e5d5255789d7665057e404dc3269
parent 664c54ec31217e4939f12ee9a848ed4ac38561b6
Author: Mattias Andrée <maandree@kth.se>
Date: Thu, 12 Jan 2017 00:13:17 +0100
cleanup
Signed-off-by: Mattias Andrée <maandree@kth.se>
Diffstat:
3 files changed, 75 insertions(+), 89 deletions(-)
diff --git a/src/stream.c b/src/stream.c
@@ -267,3 +267,43 @@ process_two_streams(struct stream *left, struct stream *right, int output_fd, co
}
}
}
+
+
+void
+process_multiple_streams(struct stream *streams, size_t n_streams, int output_fd, const char* output_fname,
+ void (*process)(struct stream *streams, size_t n_streams, size_t n))
+{
+ size_t closed, i, j, n;
+
+ for (i = 1; i < n_streams; i++)
+ echeck_compat(streams + i, streams);
+
+ while (n_streams) {
+ n = SIZE_MAX;
+ for (i = 0; i < n_streams; i++) {
+ if (streams[i].ptr < sizeof(streams->buf) && !eread_stream(streams + i, SIZE_MAX)) {
+ close(streams[i].fd);
+ streams[i].fd = -1;
+ }
+ if (streams[i].ptr && streams[i].ptr < n)
+ n = streams[i].ptr;
+ }
+ n -= n % streams->pixel_size;
+
+ process(streams, n_streams, n);
+ ewriteall(output_fd, streams->buf, n, output_fname);
+
+ closed = SIZE_MAX;
+ for (i = 0; i < n_streams; i++) {
+ memmove(streams[i].buf, streams[i].buf + n, streams[i].ptr -= n);
+ if (streams[i].ptr < streams->pixel_size && streams[i].fd < 0 && closed == SIZE_MAX)
+ closed = i;
+ }
+ if (closed != SIZE_MAX) {
+ for (i = (j = closed) + 1; i < n_streams; i++)
+ if (streams[i].ptr < streams->pixel_size && streams[i].fd < 0)
+ streams[j++] = streams[i];
+ n_streams = j;
+ }
+ }
+}
diff --git a/src/stream.h b/src/stream.h
@@ -43,3 +43,6 @@ void process_each_frame_segmented(struct stream *stream, int output_fd, const ch
void process_two_streams(struct stream *left, struct stream *right, int output_fd, const char* output_fname,
void (*process)(struct stream *left, struct stream *right, size_t n));
+
+void process_multiple_streams(struct stream *streams, size_t n_streams, int output_fd, const char* output_fname,
+ void (*process)(struct stream *streams, size_t n_streams, size_t n));
diff --git a/src/vu-stack.c b/src/vu-stack.c
@@ -9,71 +9,44 @@
USAGE("[-b] bottom-stream ... top-stream")
-static void
-process_xyza(struct stream *streams, size_t n_streams, size_t n)
-{
- double x1, y1, z1, a1;
- double x2, y2, z2, a2;
- size_t i, j;
- for (i = 0; i < n; i += streams->pixel_size) {
- x1 = ((double *)(streams[0].buf + i))[0];
- y1 = ((double *)(streams[0].buf + i))[1];
- z1 = ((double *)(streams[0].buf + i))[2];
- a1 = ((double *)(streams[0].buf + i))[3];
- for (j = 1; j < n_streams; j++) {
- x2 = ((double *)(streams[j].buf + i))[0];
- y2 = ((double *)(streams[j].buf + i))[1];
- z2 = ((double *)(streams[j].buf + i))[2];
- a2 = ((double *)(streams[j].buf + i))[3];
- x1 = x1 * a1 * (1 - a2) + x2 * a2;
- y1 = y1 * a1 * (1 - a2) + y2 * a2;
- z1 = z1 * a1 * (1 - a2) + z2 * a2;
- a1 = a1 * (1 - a2) + a2;
- }
- ((double *)(streams[0].buf + i))[0] = x1;
- ((double *)(streams[0].buf + i))[1] = y1;
- ((double *)(streams[0].buf + i))[2] = z1;
- ((double *)(streams[0].buf + i))[3] = a1;
- }
-}
+#define PROCESS_LINEAR_3CH_ALPHA(TYPE, BLEND)\
+ do {\
+ TYPE x1, y1, z1, a1;\
+ TYPE x2, y2, z2, a2;\
+ size_t i, j;\
+ for (i = 0; i < n; i += streams->pixel_size) {\
+ x1 = ((TYPE *)(streams[0].buf + i))[0];\
+ y1 = ((TYPE *)(streams[0].buf + i))[1];\
+ z1 = ((TYPE *)(streams[0].buf + i))[2];\
+ a1 = ((TYPE *)(streams[0].buf + i))[3];\
+ for (j = 1; j < n_streams; j++) {\
+ x2 = ((TYPE *)(streams[j].buf + i))[0];\
+ y2 = ((TYPE *)(streams[j].buf + i))[1];\
+ z2 = ((TYPE *)(streams[j].buf + i))[2];\
+ a2 = ((TYPE *)(streams[j].buf + i))[3];\
+ if (BLEND)\
+ a2 /= j + 1;\
+ x1 = x1 * a1 * (1 - a2) + x2 * a2;\
+ y1 = y1 * a1 * (1 - a2) + y2 * a2;\
+ z1 = z1 * a1 * (1 - a2) + z2 * a2;\
+ a1 = a1 * (1 - a2) + a2;\
+ }\
+ ((TYPE *)(streams[0].buf + i))[0] = x1;\
+ ((TYPE *)(streams[0].buf + i))[1] = y1;\
+ ((TYPE *)(streams[0].buf + i))[2] = z1;\
+ ((TYPE *)(streams[0].buf + i))[3] = a1;\
+ }\
+ } while (0)
-static void
-process_xyza_b(struct stream *streams, size_t n_streams, size_t n)
-{
- double x1, y1, z1, a1;
- double x2, y2, z2, a2;
- size_t i, j;
- for (i = 0; i < n; i += streams->pixel_size) {
- x1 = ((double *)(streams[0].buf + i))[0];
- y1 = ((double *)(streams[0].buf + i))[1];
- z1 = ((double *)(streams[0].buf + i))[2];
- a1 = ((double *)(streams[0].buf + i))[3];
- for (j = 1; j < n_streams;) {
- x2 = ((double *)(streams[j].buf + i))[0];
- y2 = ((double *)(streams[j].buf + i))[1];
- z2 = ((double *)(streams[j].buf + i))[2];
- a2 = ((double *)(streams[j].buf + i))[3];
- a2 /= ++j;
- x1 = x1 * a1 * (1 - a2) + x2 * a2;
- y1 = y1 * a1 * (1 - a2) + y2 * a2;
- z1 = z1 * a1 * (1 - a2) + z2 * a2;
- a1 = a1 * (1 - a2) + a2;
- }
- ((double *)(streams[0].buf + i))[0] = x1;
- ((double *)(streams[0].buf + i))[1] = y1;
- ((double *)(streams[0].buf + i))[2] = z1;
- ((double *)(streams[0].buf + i))[3] = a1;
- }
-}
+static void process_xyza (struct stream *streams, size_t n_streams, size_t n) { PROCESS_LINEAR_3CH_ALPHA(double, 0); }
+static void process_xyza_b(struct stream *streams, size_t n_streams, size_t n) { PROCESS_LINEAR_3CH_ALPHA(double, 1); }
int
main(int argc, char *argv[])
{
struct stream *streams;
- size_t n_streams;
+ size_t n_streams, i;
int blend = 0;
- size_t i, j, n;
- size_t closed;
void (*process)(struct stream *streams, size_t n_streams, size_t n) = NULL;
ARGBEGIN {
@@ -93,8 +66,6 @@ main(int argc, char *argv[])
for (i = 0; i < n_streams; i++) {
streams[i].file = argv[i];
streams[i].fd = eopen(streams[i].file, O_RDONLY);
- if (i)
- echeck_compat(streams + i, streams);
}
if (!strcmp(streams->pixfmt, "xyza"))
@@ -102,35 +73,7 @@ main(int argc, char *argv[])
else
eprintf("pixel format %s is not supported, try xyza\n", streams->pixfmt);
- while (n_streams) {
- n = SIZE_MAX;
- for (i = 0; i < n_streams; i++) {
- if (streams[i].ptr < sizeof(streams->buf) && !eread_stream(streams + i, SIZE_MAX)) {
- close(streams[i].fd);
- streams[i].fd = -1;
- }
- if (streams[i].ptr && streams[i].ptr < n)
- n = streams[i].ptr;
- }
- n -= n % streams->pixel_size;
-
- process(streams, n_streams, n);
-
- ewriteall(STDOUT_FILENO, streams->buf, n, "<stdout>");
-
- closed = SIZE_MAX;
- for (i = 0; i < n_streams; i++) {
- memmove(streams[i].buf, streams[i].buf + n, streams[i].ptr -= n);
- if (streams[i].ptr < streams->pixel_size && streams[i].fd < 0 && closed == SIZE_MAX)
- closed = i;
- }
- if (closed != SIZE_MAX) {
- for (i = (j = closed) + 1; i < n_streams; i++)
- if (streams[i].ptr < streams->pixel_size && streams[i].fd < 0)
- streams[j++] = streams[i];
- n_streams = j;
- }
- }
+ process_multiple_streams(streams, n_streams, STDOUT_FILENO, "<stdout>", process);
free(streams);
return 0;