blind

suckless command-line video editing utility
git clone git://git.suckless.org/blind
Log | Files | Refs | README | LICENSE

commit 0f03cc378e6ce48f17a20e409f93bfc11345a6ed
parent d3cd95e055f50df97979bcca7bb26a3edd4b82ee
Author: Mattias Andrée <maandree@kth.se>
Date:   Tue, 16 May 2017 20:14:22 +0200

Fix errors and warnings and make the code cleaner

Signed-off-by: Mattias Andrée <maandree@kth.se>

Diffstat:
MMakefile | 8++++++--
Mconfig.mk | 2+-
Msrc/blind-arithm.c | 34+++++++++++++++-------------------
Msrc/blind-colour-ciexyz.c | 2+-
Msrc/blind-colour-srgb.c | 8++++----
Msrc/blind-compress.c | 3+--
Msrc/blind-concat.c | 25+++++++++----------------
Msrc/blind-convert.c | 14+++++---------
Msrc/blind-crop.c | 5+----
Msrc/blind-cut.c | 5+----
Msrc/blind-decompress.c | 5+----
Msrc/blind-dissolve.c | 7++-----
Msrc/blind-extend.c | 5+----
Msrc/blind-flip.c | 3+--
Msrc/blind-flop.c | 3+--
Msrc/blind-from-image.c | 23+++++++++--------------
Msrc/blind-from-portable.c | 22+++++++++-------------
Msrc/blind-from-text.c | 5+----
Msrc/blind-from-video.c | 60++++++++++++++++++++++++------------------------------------
Msrc/blind-gauss-blur.c | 36+++++++++++++++---------------------
Msrc/blind-invert-luma.c | 5+----
Msrc/blind-kernel.c | 19++++++++++---------
Msrc/blind-make-kernel.c | 8++------
Msrc/blind-next-frame.c | 5+----
Msrc/blind-read-head.c | 6+-----
Msrc/blind-repeat.c | 9+++------
Msrc/blind-reverse.c | 13++++++-------
Msrc/blind-rewrite-head.c | 11+++--------
Msrc/blind-set-alpha.c | 5+----
Msrc/blind-set-luma.c | 5+----
Msrc/blind-set-saturation.c | 5+----
Msrc/blind-single-colour.c | 5+----
Msrc/blind-skip-pattern.c | 5+----
Msrc/blind-split.c | 6+-----
Msrc/blind-stack.c | 7++-----
Msrc/blind-temporal-mean.c | 52++++++++++++++++------------------------------------
Msrc/blind-time-blur.c | 6+-----
Msrc/blind-to-image.c | 29+++++++++++++----------------
Msrc/blind-to-portable.c | 38++++++++++++++++++++------------------
Msrc/blind-to-text.c | 3+--
Msrc/blind-to-video.c | 40+++++++++++++++++-----------------------
Msrc/blind-translate.c | 6+-----
Msrc/blind-transpose.c | 3+--
Msrc/blind-write-head.c | 2+-
Asrc/common.h | 37+++++++++++++++++++++++++++++++++++++
Msrc/stream.c | 11+----------
Msrc/util.c | 23+++++------------------
Msrc/util.h | 9++++++++-
Msrc/util/colour.h | 316++++++++++++++++++++++++++++---------------------------------------------------
Msrc/util/io.h | 4++--
Asrc/video-math.h | 90+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
51 files changed, 469 insertions(+), 589 deletions(-)

diff --git a/Makefile b/Makefile @@ -41,7 +41,9 @@ BIN =\ blind-to-video\ blind-translate\ blind-transpose\ - blind-write-head + blind-write-head\ + blind-kernel\ + blind-temporal-mean # TODO Not tested yet (and doesn't have any manpages): # blind-kernel @@ -59,6 +61,7 @@ SRC = $(BIN:=.c) util.c stream.c HDR =\ arg.h\ + common.h\ stream.h\ util.h\ util/to.h\ @@ -71,7 +74,8 @@ HDR =\ util/efflush.h\ util/efunc.h\ util/eprintf.h\ - util/fshut.h + util/fshut.h\ + video-math.h MISCFILES = Makefile config.mk LICENSE README TODO diff --git a/config.mk b/config.mk @@ -6,6 +6,6 @@ PREFIX = /usr/local MANPREFIX = $(PREFIX)/share/man # You may want to remove -DHAVE_PRCTL and -DHAVE_EPOLL from CPPFLAGS if you are not using Linux. -CFLAGS = -std=c99 -Wall -pedantic -O2 +CFLAGS = -std=c11 -Wall -pedantic -O2 CPPFLAGS = -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -D_FILE_OFFSET_BITS=64 -DHAVE_PRCTL -DHAVE_EPOLL LDFLAGS = -lm -s diff --git a/src/blind-arithm.c b/src/blind-arithm.c @@ -1,9 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" - -#include <math.h> -#include <string.h> +#include "common.h" USAGE("[-axyz] operation right-hand-stream") @@ -15,16 +11,16 @@ static int skip_z = 0; /* Because the syntax for a function returning a function pointer is disgusting. */ typedef void (*process_func)(struct stream *left, struct stream *right, size_t n); -#define LIST_OPERATORS(PIXFMT, TYPE, SUFFIX)\ - X(add, *lh += rh, PIXFMT, TYPE)\ - X(sub, *lh -= rh, PIXFMT, TYPE)\ - X(mul, *lh *= rh, PIXFMT, TYPE)\ - X(div, *lh /= rh, PIXFMT, TYPE)\ - X(exp, *lh = pow##SUFFIX(*lh, rh), PIXFMT, TYPE)\ - X(log, *lh = log##SUFFIX(*lh) / log##SUFFIX(rh), PIXFMT, TYPE)\ - X(min, *lh = MIN(*lh, rh), PIXFMT, TYPE)\ - X(max, *lh = MAX(*lh, rh), PIXFMT, TYPE)\ - X(abs, *lh = fabs##SUFFIX(*lh - rh) + rh, PIXFMT, TYPE) +#define LIST_OPERATORS(PIXFMT, TYPE)\ + X(add, *lh += rh, PIXFMT, TYPE)\ + X(sub, *lh -= rh, PIXFMT, TYPE)\ + X(mul, *lh *= rh, PIXFMT, TYPE)\ + X(div, *lh /= rh, PIXFMT, TYPE)\ + X(exp, *lh = pow(*lh, rh), PIXFMT, TYPE)\ + X(log, *lh = log2(*lh) / log2(rh), PIXFMT, TYPE)\ + X(min, *lh = MIN(*lh, rh), PIXFMT, TYPE)\ + X(max, *lh = MAX(*lh, rh), PIXFMT, TYPE)\ + X(abs, *lh = abs(*lh - rh) + rh, PIXFMT, TYPE) #define C(CH, CHI, ALGO, TYPE)\ (!skip_##CH ? ((lh = ((TYPE *)(left->buf + i)) + (CHI),\ @@ -49,8 +45,8 @@ typedef void (*process_func)(struct stream *left, struct stream *right, size_t n C(a, 3, ALGO, TYPE);\ }\ } -LIST_OPERATORS(xyza, double,) -LIST_OPERATORS(xyzaf, float, f) +LIST_OPERATORS(xyza, double) +LIST_OPERATORS(xyzaf, float) #undef X #if defined(__GNUC__) && !defined(__clang__) @@ -62,7 +58,7 @@ get_process_xyza(const char *operation) { #define X(NAME, ALGO, PIXFMT, TYPE)\ if (!strcmp(operation, #NAME)) return process_##PIXFMT##_##NAME; - LIST_OPERATORS(xyza, double,) + LIST_OPERATORS(xyza, double) #undef X eprintf("algorithm not recognised: %s\n", operation); return NULL; @@ -73,7 +69,7 @@ get_process_xyzaf(const char *operation) { #define X(NAME, ALGO, PIXFMT, TYPE)\ if (!strcmp(operation, #NAME)) return process_##PIXFMT##_##NAME; - LIST_OPERATORS(xyzaf, float, f) + LIST_OPERATORS(xyzaf, float) #undef X eprintf("algorithm not recognised: %s\n", operation); return NULL; diff --git a/src/blind-colour-ciexyz.c b/src/blind-colour-ciexyz.c @@ -1,5 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "util.h" +#include "common.h" USAGE("(X Y Z | Y)") diff --git a/src/blind-colour-srgb.c b/src/blind-colour-srgb.c @@ -1,5 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "util.h" +#include "common.h" USAGE("[-d depth] [-l] red green blue") @@ -26,9 +26,9 @@ main(int argc, char *argv[]) max = 1ULL << (depth - 1); max |= max - 1; - red = etolf_arg("the red value", argv[0]) / max; - green = etolf_arg("the green value", argv[1]) / max; - blue = etolf_arg("the blue value", argv[2]) / max; + red = etolf_arg("the red value", argv[0]) / (double)max; + green = etolf_arg("the green value", argv[1]) / (double)max; + blue = etolf_arg("the blue value", argv[2]) / (double)max; if (!linear) { red = srgb_decode(red); green = srgb_decode(green); diff --git a/src/blind-compress.c b/src/blind-compress.c @@ -1,6 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" +#include "common.h" USAGE("") diff --git a/src/blind-concat.c b/src/blind-concat.c @@ -1,12 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" - -#if defined(HAVE_EPOLL) -# include <sys/epoll.h> -#endif -#include <sys/mman.h> -#include <string.h> +#include "common.h" USAGE("[-o output-file [-j jobs]] first-stream ... last-stream") @@ -97,9 +90,9 @@ concat_to_file_parallel(int argc, char *argv[], char *output_file, size_t jobs) #else struct epoll_event *events; struct stream *streams; - size_t *ptrs; + off_t *ptrs, ptr; char head[STREAM_HEAD_MAX]; - size_t ptr, frames = 0, next = 0, j; + size_t frames = 0, next = 0, j; ssize_t headlen; int fd, i, n, pollfd; @@ -123,14 +116,14 @@ concat_to_file_parallel(int argc, char *argv[], char *output_file, size_t jobs) SPRINTF_HEAD_ZN(head, frames, streams->width, streams->height, streams->pixfmt, &headlen); echeck_dimensions(streams, WIDTH | HEIGHT, NULL); - ptr = (size_t)headlen; + ptr = (off_t)headlen; for (i = 0; i < argc; i++) { ptrs[i] = ptr; - ptr += streams->frames * streams->frame_size; + ptr += (off_t)streams->frames * (off_t)streams->frame_size; } if (ftruncate(fd, (off_t)ptr)) eprintf("ftruncate %s:", output_file); - fadvise_random(fd, (size_t)headlen, 0); + fadvise_random(fd, (off_t)headlen, 0); pollfd = epoll_create1(0); if (pollfd == -1) @@ -139,7 +132,7 @@ concat_to_file_parallel(int argc, char *argv[], char *output_file, size_t jobs) epwriteall(fd, head, (size_t)headlen, 0, output_file); for (i = 0; i < argc; i++) { epwriteall(fd, streams[i].buf, streams[i].ptr, ptrs[i], output_file); - ptrs[i] += streams[i].ptr; + ptrs[i] += (off_t)(streams[i].ptr); streams[i].ptr = 0; } @@ -155,14 +148,14 @@ concat_to_file_parallel(int argc, char *argv[], char *output_file, size_t jobs) jobs = j; while (jobs) { - n = epoll_wait(pollfd, events, jobs, -1); + n = epoll_wait(pollfd, events, (int)jobs, -1); if (n < 0) eprintf("epoll_wait:"); for (i = 0; i < n; i++) { j = events[i].data.u64; if (streams[j].ptr || eread_stream(streams + j, SIZE_MAX)) { epwriteall(fd, streams[j].buf, streams[j].ptr, ptrs[j], output_file); - ptrs[j] += streams[j].ptr; + ptrs[j] += (off_t)(streams[j].ptr); streams[j].ptr = 0; continue; } diff --git a/src/blind-convert.c b/src/blind-convert.c @@ -1,9 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" - -#include <alloca.h> -#include <string.h> +#include "common.h" USAGE("pixel-format ...") @@ -20,10 +16,10 @@ static void (*outconv)(double *xyzas, size_t n); interm = buf;\ n = stream->ptr / stream->pixel_size;\ for (m = n; m--; in += 4, interm += 4) { \ - interm[0] = (TYPE)(in[0]);\ - interm[1] = (TYPE)(in[1]);\ - interm[2] = (TYPE)(in[2]);\ - interm[3] = (TYPE)(in[3]);\ + interm[0] = (double)(in[0]);\ + interm[1] = (double)(in[1]);\ + interm[2] = (double)(in[2]);\ + interm[3] = (double)(in[3]);\ }\ outconv(buf, n);\ n *= stream->pixel_size;\ diff --git a/src/blind-crop.c b/src/blind-crop.c @@ -1,8 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" - -#include <string.h> +#include "common.h" USAGE("[-s | -S | -t] width height left top") diff --git a/src/blind-cut.c b/src/blind-cut.c @@ -1,8 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" - -#include <string.h> +#include "common.h" USAGE("start-point (end-point | 'end') file") diff --git a/src/blind-decompress.c b/src/blind-decompress.c @@ -1,8 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" - -#include <string.h> +#include "common.h" USAGE("") diff --git a/src/blind-dissolve.c b/src/blind-dissolve.c @@ -1,8 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" - -#include <string.h> +#include "common.h" USAGE("[-r]") @@ -55,7 +52,7 @@ main(int argc, char *argv[]) fprint_stream_head(stdout, &stream); efflush(stdout, "<stdout>"); - fm_double = fm = stream.frames - 1; + fm_double = (double)(fm = stream.frames - 1); fm_float = (float)fm_double; process_each_frame_segmented(&stream, STDOUT_FILENO, "<stdout>", process); return 0; diff --git a/src/blind-extend.c b/src/blind-extend.c @@ -1,8 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" - -#include <string.h> +#include "common.h" USAGE("[-l left] [-r right] [-a above] [-b below] [-t]") diff --git a/src/blind-flip.c b/src/blind-flip.c @@ -1,6 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" +#include "common.h" USAGE("") diff --git a/src/blind-flop.c b/src/blind-flop.c @@ -1,6 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" +#include "common.h" USAGE("") diff --git a/src/blind-from-image.c b/src/blind-from-image.c @@ -1,10 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" - -#include <arpa/inet.h> -#include <inttypes.h> -#include <string.h> +#include "common.h" USAGE("[-h] [-f | -p]") @@ -24,7 +19,7 @@ get_value_u8(char** bufp) { uint8_t value = *(uint8_t *)(*bufp); *bufp += 1; - return value / value_max; + return (double)value / (double)value_max; } static double @@ -32,7 +27,7 @@ get_value_u16(char** bufp) { uint16_t value = ntohs(*(uint16_t *)(*bufp)); *bufp += 2; - return value / value_max; + return (double)value / (double)value_max; } static double @@ -40,7 +35,7 @@ get_value_u32(char** bufp) { uint32_t value = ntohl(*(uint32_t *)(*bufp)); *bufp += 4; - return value / value_max; + return (double)value / (double)value_max; } static double @@ -56,7 +51,7 @@ get_value_u64(char** bufp) value |= (uint64_t)(buf[6]) << 8; value |= (uint64_t)(buf[7]); *bufp += 8; - return value / value_max; + return (double)value / (double)value_max; } static void @@ -96,13 +91,13 @@ static size_t pam_head(int fd, const char *fname) { size_t ptr; - ssize_t r; + size_t r; char *p; unsigned long long int maxval = UINT8_MAX; for (ptr = 0;;) { if (!(r = eread(fd, buf + ptr, sizeof(buf) - 1, fname))) eprintf("%s\n", conv_fail_msg); - ptr += (size_t)r; + ptr += r; for (;;) { p = memchr(buf, '\n', ptr); if (!p) { @@ -163,8 +158,8 @@ header_done: pixel_size = sizeof(uint64_t); get_value = get_value_u64; } - value_max = maxval; - pixel_size *= (with_colour ? 3 : 1) + with_alpha; + value_max = (double)maxval; + pixel_size *= (size_t)((with_colour ? 3 : 1) + with_alpha); convert = from_srgb; return ptr; } diff --git a/src/blind-from-portable.c b/src/blind-from-portable.c @@ -1,17 +1,12 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" - -#include <alloca.h> -#include <math.h> -#include <string.h> +#include "common.h" USAGE("[-s]") #define USING_BINARY32 0 #define USING_BINARY64 0 -#define CONV(ITYPE, SITYPE, OTYPE, EXPONENT, HA2EXPONENT, FRACTION, SUFFIX)\ +#define CONV(ITYPE, SITYPE, OTYPE, EXPONENT, HA2EXPONENT, FRACTION)\ do {\ static int cache_i = 0;\ static ITYPE cache_in[] = {0, 0, 0, 0};\ @@ -24,6 +19,7 @@ USAGE("[-s]") cache_i &= 3;\ return ret;\ }\ + cache_in[cache_i] = portable;\ signb = portable >> (EXPONENT + FRACTION);\ exponent = (portable >> FRACTION) ^ (signb << EXPONENT);\ fraction = portable & (((ITYPE)1 << FRACTION) - 1);\ @@ -33,8 +29,8 @@ USAGE("[-s]") } else {\ sexponent = 1 - HA2EXPONENT - FRACTION;\ dexponent = (OTYPE)sexponent;\ - ret = (ITYPE)fraction;\ - ret *= pow##SUFFIX((OTYPE)2.0, dexponent);\ + ret = (OTYPE)fraction;\ + ret *= pow((OTYPE)2.0, dexponent);\ }\ } else if (exponent + 1 == (ITYPE)1 << EXPONENT) {\ ret = (OTYPE)(fraction ? NAN : INFINITY);\ @@ -43,8 +39,8 @@ USAGE("[-s]") sexponent = (SITYPE)exponent;\ sexponent -= HA2EXPONENT + FRACTION;\ dexponent = (OTYPE)sexponent;\ - ret = (ITYPE)fraction;\ - ret *= pow##SUFFIX((OTYPE)2.0, dexponent);\ + ret = (OTYPE)fraction;\ + ret *= pow((OTYPE)2.0, dexponent);\ }\ ret = signb ? -ret : ret;\ cache_out[cache_i++] = ret;\ @@ -78,8 +74,8 @@ USAGE("[-s]") eprintf("%s: incomplete frame\n", stream->file);\ } while (0) -static double conv_double(uint64_t portable) {CONV(uint64_t, int64_t, double, 11, 1023, 52,);} -static float conv_float (uint32_t portable) {CONV(uint32_t, int32_t, float, 8, 127, 23, f);} +static double conv_double(uint64_t portable) {CONV(uint64_t, int64_t, double, 11, 1023, 52);} +static float conv_float (uint32_t portable) {CONV(uint32_t, int32_t, float, 8, 127, 23);} static void process_xyza (struct stream *stream, int strict) {PROCESS(uint64_t, double, 64);} static void process_xyzaf(struct stream *stream, int strict) {PROCESS(uint32_t, float, 32);} diff --git a/src/blind-from-text.c b/src/blind-from-text.c @@ -1,8 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" - -#include <string.h> +#include "common.h" USAGE("") diff --git a/src/blind-from-video.c b/src/blind-from-video.c @@ -1,10 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" - -#include <sys/mman.h> -#include <sys/stat.h> -#include <string.h> +#include "common.h" USAGE("[-F pixel-format] [-r frame-rate] [-w width -h height] [-dL] input-file output-file") @@ -76,21 +71,23 @@ get_metadata(char *file, size_t *width, size_t *height) exit(1); } -#define CONVERT_SEGMENT(TYPE, SUFFIX)\ +#define CONVERT_SEGMENT(TYPE)\ do {\ typedef TYPE pixel_t[4];\ size_t i, ptr;\ TYPE y, u, v, max = (TYPE)UINT16_MAX;\ TYPE r, g, b;\ pixel_t pixels[1024];\ + uint16_t *pix;\ if (draft) {\ for (ptr = i = 0; ptr < n; ptr += 8) {\ + pix = (uint16_t *)(buf + ptr);\ pixels[i][3] = 1;\ - y = (long int)(le16toh(((uint16_t *)(buf + ptr))[1])) - 16L * 256L;\ - u = (long int)(le16toh(((uint16_t *)(buf + ptr))[2])) - 128L * 256L;\ - v = (long int)(le16toh(((uint16_t *)(buf + ptr))[3])) - 128L * 256L;\ - scaled_yuv_to_ciexyz##SUFFIX(y, u, v, pixels[i] + 0,\ - pixels[i] + 1, pixels[i] + 2);\ + y = (TYPE)((long int)(le16toh(pix[1])) - 16L * 256L);\ + u = (TYPE)((long int)(le16toh(pix[2])) - 128L * 256L);\ + v = (TYPE)((long int)(le16toh(pix[3])) - 128L * 256L);\ + scaled_yuv_to_ciexyz(y, u, v, pixels[i] + 0,\ + pixels[i] + 1, pixels[i] + 2);\ if (++i == 1024) {\ i = 0;\ ewriteall(fd, pixels, sizeof(pixels), file);\ @@ -98,15 +95,16 @@ get_metadata(char *file, size_t *width, size_t *height) }\ } else {\ for (ptr = i = 0; ptr < n; ptr += 8) {\ - pixels[i][3] = le16toh(((uint16_t *)(buf + ptr))[0]) / max;\ - y = ((long int)le16toh(((uint16_t *)(buf + ptr))[1]) - 16L * 256L) / max;\ - u = ((long int)le16toh(((uint16_t *)(buf + ptr))[2]) - 128L * 256L) / max;\ - v = ((long int)le16toh(((uint16_t *)(buf + ptr))[3]) - 128L * 256L) / max;\ - yuv_to_srgb##SUFFIX(y, u, v, &r, &g, &b);\ - r = srgb_decode##SUFFIX(r);\ - g = srgb_decode##SUFFIX(g);\ - b = srgb_decode##SUFFIX(b);\ - srgb_to_ciexyz##SUFFIX(r, g, b, pixels[i] + 0, pixels[i] + 1, pixels[i] + 2);\ + pix = (uint16_t *)(buf + ptr);\ + pixels[i][3] = le16toh(pix[0]) / max;\ + y = (TYPE)((long int)le16toh(pix[1]) - 16L * 256L) / max;\ + u = (TYPE)((long int)le16toh(pix[2]) - 128L * 256L) / max;\ + v = (TYPE)((long int)le16toh(pix[3]) - 128L * 256L) / max;\ + yuv_to_srgb(y, u, v, &r, &g, &b);\ + r = srgb_decode(r);\ + g = srgb_decode(g);\ + b = srgb_decode(b);\ + srgb_to_ciexyz(r, g, b, pixels[i] + 0, pixels[i] + 1, pixels[i] + 2);\ if (++i == 1024) {\ i = 0;\ ewriteall(fd, pixels, sizeof(pixels), file);\ @@ -117,17 +115,8 @@ get_metadata(char *file, size_t *width, size_t *height) ewriteall(fd, pixels, i * sizeof(*pixels), file);\ } while (0) -static void -convert_segment_xyza(char *buf, size_t n, int fd, const char *file) -{ - CONVERT_SEGMENT(double,); -} - -static void -convert_segment_xyzaf(char *buf, size_t n, int fd, const char *file) -{ - CONVERT_SEGMENT(float, _f); -} +static void convert_segment_xyza (char *buf, size_t n, int fd, const char *file) {CONVERT_SEGMENT(double);} +static void convert_segment_xyzaf(char *buf, size_t n, int fd, const char *file) {CONVERT_SEGMENT(float);} static void convert(const char *infile, int outfd, const char *outfile, size_t width, size_t height, const char *frame_rate) @@ -136,7 +125,6 @@ convert(const char *infile, int outfd, const char *outfile, size_t width, size_t const char *cmd[13]; int status, pipe_rw[2]; size_t i = 0, n, ptr; - ssize_t r; pid_t pid; cmd[i++] = "ffmpeg"; @@ -166,9 +154,9 @@ convert(const char *infile, int outfd, const char *outfile, size_t width, size_t close(pipe_rw[1]); for (ptr = 0;;) { - if (!(r = eread(pipe_rw[0], buf + ptr, sizeof(buf) - ptr, "<subprocess>"))) + if (!(n = eread(pipe_rw[0], buf + ptr, sizeof(buf) - ptr, "<subprocess>"))) break; - ptr += (size_t)r; + ptr += n; n = ptr - (ptr % 8); convert_segment(buf, n, outfd, outfile); memmove(buf, buf + n, ptr -= n); @@ -253,7 +241,7 @@ main(int argc, char *argv[]) } if (skip_length) { - SPRINTF_HEAD_ZN(head, frames, width, height, pixfmt, &headlen); + SPRINTF_HEAD_ZN(head, 0, width, height, pixfmt, &headlen); ewriteall(outfd, head, (size_t)headlen, outfile); } diff --git a/src/blind-gauss-blur.c b/src/blind-gauss-blur.c @@ -1,9 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" - -#include <math.h> -#include <string.h> +#include "common.h" USAGE("[-j jobs] [-s spread | -s 'auto'] [-achvy] sd-stream") @@ -62,13 +58,13 @@ static size_t spread = 0; if (auto_spread && spread < 1)\ spread = 1; -#define BLUR_PIXEL(START, LOOP, DISTANCE, SUFFIX)\ +#define BLUR_PIXEL(TYPE, START, LOOP, DISTANCE)\ if (k[0] == k[1] && k[1] == k[2]) {\ START;\ for (LOOP) {\ - d = (DISTANCE);\ + d = (TYPE)(DISTANCE);\ d *= d;\ - m = c[0] * exp##SUFFIX(d * k[0]);\ + m = c[0] * exp(d * k[0]);\ img[i2][0] += clr[i1][0] * m;\ img[i2][1] += clr[i1][1] * m;\ img[i2][2] += clr[i1][2] * m;\ @@ -87,11 +83,11 @@ static size_t spread = 0; continue;\ START;\ for (LOOP) {\ - d = (DISTANCE);\ + d = (TYPE)(DISTANCE);\ d *= d;\ - m = c[i] * exp##SUFFIX(d * k[i]);\ + m = c[i] * exp(d * k[i]);\ img[i2][i] += clr[i1][i] * m;\ - img[i2][3] += clr[i1][3] * m / blurred;\ + img[i2][3] += clr[i1][3] * m / (TYPE)blurred;\ }\ }\ } @@ -104,7 +100,7 @@ static size_t spread = 0; img[i1][2] = clr[i1][2];\ img[i1][3] = clr[i1][3]; -#define BLUR(TYPE, DIR, SETSTART, SETEND, START, LOOP, DISTANCE, SUFFIX)\ +#define BLUR(TYPE, DIR, SETSTART, SETEND, START, LOOP, DISTANCE)\ do {\ memset(img, 0, colour->frame_size);\ start = 0, end = colour->height;\ @@ -117,14 +113,14 @@ static size_t spread = 0; SETSTART;\ SETEND;\ }\ - BLUR_PIXEL(START, LOOP, DISTANCE, SUFFIX);\ + BLUR_PIXEL(TYPE, START, LOOP, DISTANCE);\ BLUR_PIXEL_EPILOGUE(DIR);\ }\ }\ ejoin_jobs(is_master, children);\ } while (0) -#define PROCESS(TYPE, SUFFIX)\ +#define PROCESS(TYPE)\ do {\ typedef TYPE pixel_t[4];\ \ @@ -226,8 +222,7 @@ static size_t spread = 0; x2end = spread + 1 > colour->width - x1 ? colour->width : x1 + spread + 1,\ i2 = y1 * colour->width + x2start,\ x2 = x2start; x2 < x2end; (x2++, i2++),\ - (ssize_t)x1 - (ssize_t)x2,\ - SUFFIX);\ + (ssize_t)x1 - (ssize_t)x2);\ if (horizontal && vertical)\ memcpy(clr, img, colour->frame_size);\ if (vertical)\ @@ -236,8 +231,7 @@ static size_t spread = 0; y2end = spread + 1 > colour->height - y1 ? colour->height : y1 + spread + 1,\ i2 = y2start * colour->width + x1,\ y2 = y2start; y2 < y2end; (y2++, i2 += colour->width),\ - (ssize_t)y1 - (ssize_t)y2,\ - SUFFIX);\ + (ssize_t)y1 - (ssize_t)y2);\ \ start = 0, end = colour->height;\ is_master = efork_jobs(&start, &end, jobs, &children);\ @@ -257,7 +251,7 @@ static size_t spread = 0; i1 = start * colour->width;\ for (y1 = start; y1 < end; y1++) {\ for (x1 = 0; x1 < colour->width; x1++, i1++) {\ - if (!img[i1][3])\ + if (img[i1][3] != 0)\ continue;\ img[i1][0] /= img[i1][3];\ img[i1][1] /= img[i1][3];\ @@ -282,14 +276,14 @@ static void process_xyza(char *restrict output, char *restrict cbuf, char *restrict sbuf, struct stream *colour, struct stream *sigma) { - PROCESS(double,); + PROCESS(double); } static void process_xyzaf(char *restrict output, char *restrict cbuf, char *restrict sbuf, struct stream *colour, struct stream *sigma) { - PROCESS(float, f); + PROCESS(float); } int diff --git a/src/blind-invert-luma.c b/src/blind-invert-luma.c @@ -1,8 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" - -#include <string.h> +#include "common.h" USAGE("[-iw] mask-stream") diff --git a/src/blind-kernel.c b/src/blind-kernel.c @@ -1,13 +1,14 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" +#include "common.h" -#include <math.h> -#include <string.h> +#if defined(__GNUC__) && !defined(__clang__) +# pragma GCC diagnostic push +# pragma GCC diagnostic ignored "-Wunsafe-loop-optimizations" +#endif USAGE("[-xyza] kernel [parameter] ...") -#define SUBUSAGE(FORMAT) "Usage: %s [-xyza] " FORMAT, argv0 +#define SUBUSAGE(FORMAT) "usage: %s [-xyza] " FORMAT, argv0 #define STRCASEEQ3(A, B1, B2, B3) (!strcasecmp(A, B1) || !strcasecmp(A, B2) || !strcasecmp(A, B3)) #define LIST_KERNELS\ @@ -110,6 +111,7 @@ kernel_sharpen(int argc, char *argv[], size_t *rows, size_t *cols, double **free *rows = *cols = 3; #define argv0 arg + (void) arg; argc++, argv--; ARGBEGIN { case 'i': @@ -126,7 +128,6 @@ kernel_sharpen(int argc, char *argv[], size_t *rows, size_t *cols, double **free usage: eprintf(SUBUSAGE("'sharpen' [-i]")); return NULL; - (void) arg; } static const double * @@ -156,7 +157,7 @@ kernel_gaussian(int argc, char *argv[], size_t *rows, size_t *cols, double **fre if (argc != 1) goto usage; - sigma = etof_arg("standard-deviation", argv[0]); + sigma = etolf_arg("standard-deviation", argv[0]); if (!spread) spread = (size_t)(sigma * 3.0 + 0.5); @@ -171,7 +172,7 @@ kernel_gaussian(int argc, char *argv[], size_t *rows, size_t *cols, double **fre k = 1.0 / -k; for (x = 0; x <= spread; x++) { - value = spread - x; + value = (double)(spread - x); value *= value * k; value = exp(value) * c; for (y = 0; y < *rows; y++) { @@ -181,7 +182,7 @@ kernel_gaussian(int argc, char *argv[], size_t *rows, size_t *cols, double **fre } for (y = 0; y <= spread; y++) { - value = spread - y; + value = (double)(spread - y); value *= value * k; value = exp(value) * c; for (x = 0; x < *cols; x++) { diff --git a/src/blind-make-kernel.c b/src/blind-make-kernel.c @@ -1,9 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" - -#include <ctype.h> -#include <string.h> +#include "common.h" USAGE("[-d denominator] ... [-nxyza] [-- value ...] ...") @@ -58,7 +54,7 @@ static double * read_matrix_stdin(size_t *rows, size_t *cols) { char *line = NULL, *p, *q; - size_t size = 0, col; + size_t size = 0, col = 0; double *kernel = NULL; ssize_t len; *rows = *cols = 0; diff --git a/src/blind-next-frame.c b/src/blind-next-frame.c @@ -1,8 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" - -#include <string.h> +#include "common.h" USAGE("[-f frames] width height pixel-format ...") diff --git a/src/blind-read-head.c b/src/blind-read-head.c @@ -1,8 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" - -#include <ctype.h> +#include "common.h" USAGE("") @@ -50,5 +47,4 @@ main(int argc, char *argv[]) return 0; bad_format: eprintf("<stdin>: file format not supported\n"); - return 0; } diff --git a/src/blind-repeat.c b/src/blind-repeat.c @@ -1,8 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" - -#include <string.h> +#include "common.h" USAGE("(count | 'inf') file") @@ -14,8 +11,8 @@ static int repeat_regular_file(void) { while (inf || count--) { - fadvise_sequential(stream.fd, stream.headlen, 0); - elseek(stream.fd, stream.headlen, SEEK_SET, stream.file); + fadvise_sequential(stream.fd, (off_t)(stream.headlen), 0); + elseek(stream.fd, (off_t)(stream.headlen), SEEK_SET, stream.file); if (esend_stream(&stream, STDOUT_FILENO, NULL)) return -1; } diff --git a/src/blind-reverse.c b/src/blind-reverse.c @@ -1,6 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" +#include "common.h" USAGE("[-i] file") @@ -9,15 +8,15 @@ to_stdout(struct stream *stream) { size_t ptr, end, n; char buf[BUFSIZ]; - ssize_t r; while (stream->frames--) { ptr = stream->frames * stream->frame_size + stream->headlen; end = ptr + stream->frame_size; while (ptr < end) { - if (!(r = epread(stream->fd, buf, MIN(sizeof(buf), end - ptr), ptr, stream->file))) + if (!(n = epread(stream->fd, buf, MIN(sizeof(buf), end - ptr), + (off_t)ptr, stream->file))) eprintf("%s: file is shorter than expected\n", stream->file); - ptr += n = (size_t)r; + ptr += n; ewriteall(STDOUT_FILENO, buf, n, "<stdout>"); } } @@ -57,8 +56,8 @@ in_place(struct stream *stream) bufb = emalloc(stream->frame_size); for (f = 0; f < stream->frames >> 1; f++) { - pa = f * stream->frame_size + stream->headlen; - pb = (fm - f) * stream->frame_size + stream->headlen; + pa = (off_t)f * (off_t)(stream->frame_size) + (off_t)(stream->headlen); + pb = (off_t)(fm - f) * (off_t)(stream->frame_size) + (off_t)(stream->headlen); epread_frame(stream, bufa, pa); epread_frame(stream, bufb, pb); diff --git a/src/blind-rewrite-head.c b/src/blind-rewrite-head.c @@ -1,10 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" - -#include <sys/mman.h> -#include <sys/stat.h> -#include <string.h> +#include "common.h" USAGE("[-h] file [(frames | 'auto') [(width | 'same') (height | 'same') [format | 'same']]]") @@ -40,7 +35,7 @@ rewrite(struct stream *stream, int frames_auto) eprintf("%s: video is too long\n", stream->file); if ((size_t)headlen > stream->headlen) - if (ftruncate(stream->fd, length + headlen)) + if (ftruncate(stream->fd, (off_t)length + (off_t)headlen)) eprintf("ftruncate %s:", stream->file); data = mmap(0, length + (size_t)headlen, PROT_READ | PROT_WRITE, MAP_SHARED, stream->fd, 0); @@ -52,7 +47,7 @@ rewrite(struct stream *stream, int frames_auto) munmap(data, length + (size_t)headlen); if ((size_t)headlen < stream->headlen) - if (ftruncate(stream->fd, length + headlen)) + if (ftruncate(stream->fd, (off_t)length + (off_t)headlen)) eprintf("ftruncate %s:", stream->file); } diff --git a/src/blind-set-alpha.c b/src/blind-set-alpha.c @@ -1,8 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" - -#include <string.h> +#include "common.h" USAGE("[-i] alpha-stream") diff --git a/src/blind-set-luma.c b/src/blind-set-luma.c @@ -1,8 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" - -#include <string.h> +#include "common.h" USAGE("luma-stream") diff --git a/src/blind-set-saturation.c b/src/blind-set-saturation.c @@ -1,8 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" - -#include <string.h> +#include "common.h" USAGE("[-w] saturation-stream") diff --git a/src/blind-single-colour.c b/src/blind-single-colour.c @@ -1,8 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" - -#include <string.h> +#include "common.h" USAGE("[-f frames | -f 'inf'] [-F pixel-format] -w width -h height (X Y Z | Y) [alpha]") diff --git a/src/blind-skip-pattern.c b/src/blind-skip-pattern.c @@ -1,8 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" - -#include <string.h> +#include "common.h" USAGE("(skipped-frames | +included-frames) ...") diff --git a/src/blind-split.c b/src/blind-split.c @@ -1,9 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" - -#include <alloca.h> -#include <string.h> +#include "common.h" USAGE("[-L] (file (end-point | 'end')) ...") diff --git a/src/blind-stack.c b/src/blind-stack.c @@ -1,8 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" - -#include <string.h> +#include "common.h" USAGE("[-b] bottom-stream ... top-stream") @@ -22,7 +19,7 @@ USAGE("[-b] bottom-stream ... top-stream") z2 = ((TYPE *)(streams[j].buf + i))[2];\ a2 = ((TYPE *)(streams[j].buf + i))[3];\ if (BLEND)\ - a2 /= j + 1;\ + a2 /= (TYPE)(j + 1);\ x1 = x1 * a1 * (1 - a2) + x2 * a2;\ y1 = y1 * a1 * (1 - a2) + y2 * a2;\ z1 = z1 * a1 * (1 - a2) + z2 * a2;\ diff --git a/src/blind-temporal-mean.c b/src/blind-temporal-mean.c @@ -1,9 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" - -#include <math.h> -#include <string.h> +#include "common.h" USAGE("[-g | -h | -l power | -p power]") /* TODO add -w weight-stream */ @@ -21,23 +17,23 @@ typedef void (*process_func)(struct stream *stream, void *buffer, void *image, s * X-parameter 7: pre-finalise assignments * X-parameter 8: subcell finalisation */ -#define LIST_MEANS(TYPE, SUFFIX)\ +#define LIST_MEANS(TYPE)\ /* [default] arithmetic mean */\ X(ARITHMETIC, arithmetic, 1, COPY_FRAME,, *img1 += *buf,\ a = (TYPE)1.0 / (TYPE)frame, *img1 *= a)\ /* geometric mean */\ X(GEOMETRIC, geometric, 1, COPY_FRAME,, *img1 *= *buf,\ - a = (TYPE)1.0 / (TYPE)frame, *img1 = nnpow##SUFFIX(*img1, a))\ + a = (TYPE)1.0 / (TYPE)frame, *img1 = nnpow(*img1, a))\ /* harmonic mean */\ X(HARMONIC, harmonic, 1, ZERO_AND_PROCESS_FRAME,, *img1 += (TYPE)1 / *buf,\ a = (TYPE)frame, *img1 = a / *img1)\ /* lehmer mean */\ X(LEHMER, lehmer, 2, ZERO_AND_PROCESS_FRAME, (a = (TYPE)power, b = a - (TYPE)1),\ - (*img1 += nnpow##SUFFIX(*buf, a), *img2 += nnpow##SUFFIX(*buf, b)),, *img1 /= *img2)\ + (*img1 += nnpow(*buf, a), *img2 += nnpow(*buf, b)),, *img1 /= *img2)\ /* power mean (Hölder mean) (m = 2 for root square mean; m = 3 for cubic mean) */\ X(POWER, power, 1, ZERO_AND_PROCESS_FRAME, a = (TYPE)power,\ - *img1 += nnpow##SUFFIX(*buf, a), (a = (TYPE)1 / (TYPE)frame, b = (TYPE)(1.0 / power)),\ - *img1 = a * nnpow##SUFFIX(*img1, b)) + *img1 += nnpow(*buf, a), (a = (TYPE)1 / (TYPE)frame, b = (TYPE)(1.0 / power)),\ + *img1 = a * nnpow(*img1, b)) enum first_frame_action { COPY_FRAME, @@ -46,28 +42,12 @@ enum first_frame_action { }; #define X(V, ...) V, -enum method { LIST_MEANS(,) }; +enum method { LIST_MEANS() }; #undef X static double power; -static inline double -nnpow(double a, double b) -{ - int neg = a < 0; - a = pow(neg ? -a : a, b); - return neg ? -a : a; -} - -static inline float -nnpowf(float a, float b) -{ - int neg = a < 0; - a = powf(neg ? -a : a, b); - return neg ? -a : a; -} - -#define MAKE_PROCESS(PIXFMT, TYPE, SUFFIX,\ +#define MAKE_PROCESS(PIXFMT, TYPE,\ _1, NAME, _3, _4, PRE_PROCESS, PROCESS_SUBCELL, PRE_FINALISE, FINALISE_SUBCELL)\ static void\ process_##PIXFMT##_##NAME(struct stream *stream, void *buffer, void *image, size_t frame)\ @@ -86,22 +66,22 @@ nnpowf(float a, float b) for (x = 0; x < stream->width; x++, img1++, img2++, buf++)\ PROCESS_SUBCELL;\ }\ - (void) img2, (void) a, (void) b;\ + (void) img2, (void) a, (void) b, (void) frame;\ } -#define X(...) MAKE_PROCESS(xyza, double,, __VA_ARGS__) -LIST_MEANS(double,) +#define X(...) MAKE_PROCESS(xyza, double, __VA_ARGS__) +LIST_MEANS(double) #undef X -#define X(...) MAKE_PROCESS(xyzaf, float, f, __VA_ARGS__) -LIST_MEANS(float, f) +#define X(...) MAKE_PROCESS(xyzaf, float, __VA_ARGS__) +LIST_MEANS(float) #undef X #undef MAKE_PROCESS #define X(ID, NAME, ...) [ID] = process_xyza_##NAME, -static const process_func process_functions_xyza[] = { LIST_MEANS(,) }; +static const process_func process_functions_xyza[] = { LIST_MEANS() }; #undef X #define X(ID, NAME, ...) [ID] = process_xyzaf_##NAME, -static const process_func process_functions_xyzaf[] = { LIST_MEANS(,) }; +static const process_func process_functions_xyzaf[] = { LIST_MEANS() }; #undef X int @@ -142,7 +122,7 @@ main(int argc, char *argv[]) first_frame_action = FIRST_FRAME_ACTION;\ break; switch (method) { - LIST_MEANS(,) + LIST_MEANS() default: abort(); } diff --git a/src/blind-time-blur.c b/src/blind-time-blur.c @@ -1,9 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" - -#include <string.h> -#include <unistd.h> +#include "common.h" USAGE("alpha-stream") diff --git a/src/blind-to-image.c b/src/blind-to-image.c @@ -1,9 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" - -#include <arpa/inet.h> -#include <string.h> +#include "common.h" USAGE("[-d depth | -f]") @@ -13,7 +9,7 @@ static int alpha_warning_triggered = 0; static unsigned long long int max; static int bytes; -#define WRITE_PIXEL(TYPE, SUFFIX)\ +#define WRITE_PIXEL(TYPE)\ do {\ unsigned long long int colours[4];\ unsigned char buf[4 * 8];\ @@ -38,10 +34,10 @@ static int bytes; A = A < 0 ? 0 : 1;\ }\ \ - colours[0] = srgb_encode##SUFFIX(R) * max + (TYPE)0.5;\ - colours[1] = srgb_encode##SUFFIX(G) * max + (TYPE)0.5;\ - colours[2] = srgb_encode##SUFFIX(B) * max + (TYPE)0.5;\ - colours[3] = A * max + (TYPE)0.5;\ + colours[0] = (unsigned long long int)(srgb_encode(R) * (TYPE)max + (TYPE)0.5);\ + colours[1] = (unsigned long long int)(srgb_encode(G) * (TYPE)max + (TYPE)0.5);\ + colours[2] = (unsigned long long int)(srgb_encode(B) * (TYPE)max + (TYPE)0.5);\ + colours[3] = (unsigned long long int)(A * (TYPE)max + (TYPE)0.5);\ \ for (i = k = 0; i < 4; i++, k += bytes) {\ for (j = 0; j < bytes; j++) {\ @@ -50,7 +46,7 @@ static int bytes; }\ }\ \ - ewriteall(STDOUT_FILENO, buf, k, "<stdout>");\ + ewriteall(STDOUT_FILENO, buf, (size_t)k, "<stdout>");\ } while (0) #define PROCESS(TYPE, SUFFIX)\ @@ -71,15 +67,15 @@ static int bytes; }\ }\ \ - ciexyz_to_srgb##SUFFIX(X, Y, Z, &R, &G, &B);\ + ciexyz_to_srgb(X, Y, Z, &R, &G, &B);\ write_pixel##SUFFIX(R, G, B, A);\ }\ } while (0) -static void write_pixel(double R, double G, double B, double A) {WRITE_PIXEL(double,);} -static void write_pixel_f(float R, float G, float B, float A) {WRITE_PIXEL(float, _f);} +static void write_pixel_d(double R, double G, double B, double A) {WRITE_PIXEL(double);} +static void write_pixel_f(float R, float G, float B, float A) {WRITE_PIXEL(float);} -static void process_xyza (struct stream *stream, size_t n) {PROCESS(double,);} +static void process_xyza (struct stream *stream, size_t n) {PROCESS(double, _d);} static void process_xyzaf(struct stream *stream, size_t n) {PROCESS(float, _f);} int @@ -117,7 +113,8 @@ main(int argc, char *argv[]) eprintf("pixel format %s is not supported, try xyza\n", stream.pixfmt); if (farbfeld) { - uint32_t width = stream.width, height = stream.height; + uint32_t width = (uint32_t)(stream.width); + uint32_t height = (uint32_t)(stream.height); if (stream.width > UINT32_MAX) eprintf("%s: frame is too wide\n", stream.file); if (stream.height > UINT32_MAX) diff --git a/src/blind-to-portable.c b/src/blind-to-portable.c @@ -1,17 +1,18 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" +#include "common.h" -#include <alloca.h> -#include <math.h> -#include <string.h> +/* Disable warnings in <math.h> */ +#if defined(__clang__) +# pragma clang diagnostic ignored "-Wdouble-promotion" +# pragma clang diagnostic ignored "-Wconversion" +#endif USAGE("[-s]") #define USING_BINARY32 0 #define USING_BINARY64 0 -#define CONV(ITYPE, OTYPE, SOTYPE, EXPONENT, HA2EXPONENT, FRACTION, SUFFIX)\ +#define CONV(ITYPE, OTYPE, SOTYPE, EXPONENT, HA2EXPONENT, FRACTION)\ do {\ static int cache_i = 0;\ static ITYPE cache_in[] = {0, 0, 0, 0};\ @@ -24,22 +25,23 @@ USAGE("[-s]") cache_i &= 3;\ return ret;\ }\ - signb = signbit(host);\ + cache_in[cache_i] = host;\ + signb = (OTYPE)signbit(host);\ u = signb ? -host : host;\ - if (isnan(host) || !isfinite(host)) {\ - ret = ((((OTYPE)1 << EXPONENT) - 1) << FRACTION) | !isinf(host);\ + if (g_isnan(host) || g_isinf(host)) {\ + ret = ((((OTYPE)1 << EXPONENT) - (OTYPE)1) << FRACTION) | (OTYPE)!g_isinf(host);\ } else if (u == (ITYPE)0.0) {\ ret = 0;\ } else {\ - dexponent = log2##SUFFIX(u);\ + dexponent = log2(u);\ exponent = (SOTYPE)dexponent;\ - if (u == pow##SUFFIX(2.0, (ITYPE)exponent)) {\ + if (u == pow((ITYPE)2.0, (ITYPE)exponent)) {\ exponent += HA2EXPONENT;\ fraction = 0;\ } else {\ /* TODO subnormals are a bit rounded off */\ - u *= pow##SUFFIX(2.0, (ITYPE)(FRACTION + 1 - exponent));\ - fraction = u;\ + u *= pow((ITYPE)2.0, (ITYPE)(FRACTION + 1 - exponent));\ + fraction = (OTYPE)u;\ while (fraction >= (OTYPE)2 << FRACTION) {\ fraction >>= 1;\ exponent += 1;\ @@ -51,11 +53,11 @@ USAGE("[-s]") /* TODO subnormal result */\ exponent = 0;\ fraction = 0;\ - } else if (exponent >= ((SOTYPE)1 << EXPONENT) - (SOTYPE)1) { \ - exponent = ((SOTYPE)1 << EXPONENT) - (SOTYPE)1;\ + } else if (exponent >= ((SOTYPE)1 << EXPONENT) - 1) { \ + exponent = ((SOTYPE)1 << EXPONENT) - 1;\ fraction = 0;\ }\ - ret = (exponent << FRACTION) + fraction;\ + ret = ((OTYPE)exponent << FRACTION) + fraction;\ }\ ret |= signb << (FRACTION + EXPONENT);\ cache_out[cache_i++] = ret;\ @@ -89,8 +91,8 @@ USAGE("[-s]") eprintf("%s: incomplete frame\n", stream->file);\ } while (0) -static uint64_t conv_double(double host) {CONV(double, uint64_t, int64_t, 11, 1023, 52,);} -static uint32_t conv_float (float host) {CONV(float, uint32_t, int32_t, 8, 127, 23, f);} +static uint64_t conv_double(double host) {CONV(double, uint64_t, int64_t, 11, 1023, 52);} +static uint32_t conv_float (float host) {CONV(float, uint32_t, int32_t, 8, 127, 23);} static void process_xyza (struct stream *stream, int strict) {PROCESS(double, uint64_t, 64);} static void process_xyzaf(struct stream *stream, int strict) {PROCESS(float, uint32_t, 32);} diff --git a/src/blind-to-text.c b/src/blind-to-text.c @@ -1,6 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" +#include "common.h" #include <string.h> diff --git a/src/blind-to-video.c b/src/blind-to-video.c @@ -1,18 +1,12 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" - -#include <signal.h> -#include <stdio.h> -#include <stdint.h> -#include <string.h> +#include "common.h" USAGE("[-d] frame-rate ffmpeg-arguments ...") static int draft = 0; static int fd; -#define PROCESS(TYPE, SUFFIX)\ +#define PROCESS(TYPE)\ do {\ char *buf = stream->buf;\ TYPE *pixel, r, g, b;\ @@ -25,14 +19,14 @@ static int fd; if (draft) {\ for (ptr = 0; ptr < n; ptr += 4 * sizeof(TYPE)) {\ pixel = (TYPE *)(buf + ptr);\ - ciexyz_to_scaled_yuv##SUFFIX(pixel[0], pixel[1], pixel[2], &r, &g, &b);\ + ciexyz_to_scaled_yuv(pixel[0], pixel[1], pixel[2], &r, &g, &b);\ y = (long int)r + 16L * 256L;\ u = (long int)g + 128L * 256L;\ v = (long int)b + 128L * 256L;\ *pixels++ = 0xFFFFU;\ - *pixels++ = htole16((uint16_t)CLIP(0, y, 0xFFFFL));\ - *pixels++ = htole16((uint16_t)CLIP(0, u, 0xFFFFL));\ - *pixels++ = htole16((uint16_t)CLIP(0, v, 0xFFFFL));\ + *pixels++ = htole((uint16_t)CLIP(0, y, 0xFFFFL));\ + *pixels++ = htole((uint16_t)CLIP(0, u, 0xFFFFL));\ + *pixels++ = htole((uint16_t)CLIP(0, v, 0xFFFFL));\ if (pixels == end)\ ewriteall(fd, pixels = pixbuf, sizeof(pixbuf), "<subprocess>");\ }\ @@ -40,18 +34,18 @@ static int fd; for (ptr = 0; ptr < n; ptr += 4 * sizeof(TYPE)) {\ pixel = (TYPE *)(buf + ptr);\ a = (long int)(pixel[3] * 0xFFFFL);\ - ciexyz_to_srgb##SUFFIX(pixel[0], pixel[1], pixel[2], &r, &g, &b);\ - r = srgb_encode##SUFFIX(r);\ - g = srgb_encode##SUFFIX(g);\ - b = srgb_encode##SUFFIX(b);\ - srgb_to_yuv##SUFFIX(r, g, b, pixel + 0, pixel + 1, pixel + 2);\ + ciexyz_to_srgb(pixel[0], pixel[1], pixel[2], &r, &g, &b);\ + r = srgb_encode(r);\ + g = srgb_encode(g);\ + b = srgb_encode(b);\ + srgb_to_yuv(r, g, b, pixel + 0, pixel + 1, pixel + 2);\ y = (long int)(pixel[0] * 0xFFFFL) + 16L * 256L;\ u = (long int)(pixel[1] * 0xFFFFL) + 128L * 256L;\ v = (long int)(pixel[2] * 0xFFFFL) + 128L * 256L;\ - *pixels++ = htole16((uint16_t)CLIP(0, a, 0xFFFFL));\ - *pixels++ = htole16((uint16_t)CLIP(0, y, 0xFFFFL));\ - *pixels++ = htole16((uint16_t)CLIP(0, u, 0xFFFFL));\ - *pixels++ = htole16((uint16_t)CLIP(0, v, 0xFFFFL));\ + *pixels++ = htole((uint16_t)CLIP(0, a, 0xFFFFL));\ + *pixels++ = htole((uint16_t)CLIP(0, y, 0xFFFFL));\ + *pixels++ = htole((uint16_t)CLIP(0, u, 0xFFFFL));\ + *pixels++ = htole((uint16_t)CLIP(0, v, 0xFFFFL));\ if (pixels == end)\ ewriteall(fd, pixels = pixbuf, sizeof(pixbuf), "<subprocess>");\ }\ @@ -59,8 +53,8 @@ static int fd; ewriteall(fd, pixbuf, (size_t)(pixels - pixbuf) * sizeof(*pixels), "<subprocess>");\ } while (0) -static void process_xyza (struct stream *stream, size_t n) {PROCESS(double,);} -static void process_xyzaf(struct stream *stream, size_t n) {PROCESS(float, _f);} +static void process_xyza (struct stream *stream, size_t n) {PROCESS(double);} +static void process_xyzaf(struct stream *stream, size_t n) {PROCESS(float);} int main(int argc, char *argv[]) diff --git a/src/blind-translate.c b/src/blind-translate.c @@ -1,8 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" - -#include <string.h> +#include "common.h" USAGE("[-wp] translation-stream") @@ -59,7 +56,6 @@ process_frame(struct stream *stream, char *buf, size_t above, size_t below, size return 1; eof: eprintf("%s: file is shorter than expected\n", stream->file); - return 0; } static void diff --git a/src/blind-transpose.c b/src/blind-transpose.c @@ -1,6 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" +#include "common.h" USAGE("") diff --git a/src/blind-write-head.c b/src/blind-write-head.c @@ -1,5 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "util.h" +#include "common.h" USAGE("parameters ...") diff --git a/src/common.h b/src/common.h @@ -0,0 +1,37 @@ +/* See LICENSE file for copyright and license details. */ +#if defined(__clang__) +# pragma clang diagnostic ignored "-Wdisabled-macro-expansion" +# pragma clang diagnostic ignored "-Wcomma" +# pragma clang diagnostic ignored "-Wcast-align" +# pragma clang diagnostic ignored "-Wassign-enum" +# pragma clang diagnostic ignored "-Wfloat-equal" +# pragma clang diagnostic ignored "-Wformat-nonliteral" +# pragma clang diagnostic ignored "-Wcovered-switch-default" +#elif defined(__GNUC__) +# pragma GCC diagnostic ignored "-Wfloat-equal" +#endif + +#include "stream.h" +#include "util.h" +#include "video-math.h" + +#include <arpa/inet.h> +#if defined(HAVE_EPOLL) +# include <sys/epoll.h> +#endif +#include <sys/mman.h> +#include <sys/stat.h> +#include <sys/wait.h> +#include <alloca.h> +#include <ctype.h> +#include <errno.h> +#include <fcntl.h> +#include <inttypes.h> +#include <limits.h> +#include <signal.h> +#include <stdarg.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> +#include <strings.h> +#include <unistd.h> diff --git a/src/stream.c b/src/stream.c @@ -1,14 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "stream.h" -#include "util.h" - -#include <sys/mman.h> -#include <sys/stat.h> -#include <errno.h> -#include <inttypes.h> -#include <stdlib.h> -#include <string.h> -#include <unistd.h> +#include "common.h" static inline int get_dimension(int status, size_t *out, const char *s, const char *fname, const char *dim) diff --git a/src/util.c b/src/util.c @@ -1,18 +1,5 @@ /* See LICENSE file for copyright and license details. */ -#include "util.h" - -#include <sys/wait.h> -#include <ctype.h> -#include <errno.h> -#include <fcntl.h> -#include <limits.h> -#include <signal.h> -#include <stdarg.h> -#include <stdio.h> -#include <stdlib.h> -#include <string.h> -#include <strings.h> -#include <unistd.h> +#include "common.h" char *argv0; @@ -139,17 +126,17 @@ readall(int fd, void *buf, size_t n) } int -pwriteall(int fd, void *buf, size_t n, size_t ptr) +pwriteall(int fd, void *buf, size_t n, off_t ptr) { char *buffer = buf; ssize_t r; while (n) { - r = pwrite(fd, buffer, n, ptr); + r = pwrite(fd, buffer, n, (off_t)ptr); if (r < 0) return -1; buffer += (size_t)r; n -= (size_t)r; - ptr += (size_t)r; + ptr += (off_t)r; } return 0; } @@ -171,7 +158,7 @@ getfile(int fd, void *buffer, size_t *restrict ptr, size_t *restrict size) { char *restrict *restrict buf = buffer; void *new; - size_t r; + ssize_t r; for (;;) { if (*ptr == *size) { diff --git a/src/util.h b/src/util.h @@ -1,6 +1,12 @@ /* See LICENSE file for copyright and license details. */ #include "arg.h" +#if defined(__GNUC__) || defined(__clang__) +# define ATTRIBUTE_NORETURN __attribute__((noreturn)) +#else +# define ATTRIBUTE_NORETURN +#endif + #define ELEMENTSOF(ARRAY) (sizeof(ARRAY) / sizeof(*(ARRAY))) #define MIN(A, B) ((A) < (B) ? (A) : (B)) #define MAX(A, B) ((A) > (B) ? (A) : (B)) @@ -9,8 +15,9 @@ #define INTSTRLEN(TYPE) ((sizeof(TYPE) == 1 ? 3 : (5 * sizeof(TYPE) / 2)) + ((TYPE)-1 < 1)) #define USAGE(SYNOPSIS)\ + ATTRIBUTE_NORETURN\ static void usage(void)\ - { eprintf("usage: %s%s%s\n", argv0, SYNOPSIS ? " " : "", SYNOPSIS); } + { eprintf("usage: %s%s%s\n", argv0, *SYNOPSIS ? " " : "", SYNOPSIS); } #include "util/eprintf.h" #include "util/efflush.h" diff --git a/src/util/colour.h b/src/util/colour.h @@ -7,210 +7,118 @@ #define D65_XYZ_X (D65_XYY_X / D65_XYY_Y) #define D65_XYZ_Z (1 / D65_XYY_Y - 1 - D65_XYZ_X) -static inline double -srgb_encode(double t) -{ - double sign = 1; - if (t < 0) { - t = -t; - sign = -1; +#define SRGB_ENCODE(TYPE, NAME_SUFFIX, MATH_SUFFIX)\ + static inline TYPE\ + srgb_encode##NAME_SUFFIX(TYPE t)\ + {\ + TYPE sign = 1;\ + if (t < 0) {\ + t = -t;\ + sign = -1;\ + }\ + t = t <= (TYPE)0.0031306684425217108\ + ? (TYPE)12.92 * t\ + : (TYPE)1.055 * pow##MATH_SUFFIX(t, (TYPE)1.0 / (TYPE)2.4) - (TYPE)0.055;\ + return t * sign;\ } - t = t <= 0.0031306684425217108 - ? 12.92 * t - : 1.055 * pow(t, 1 / 2.4) - 0.055; - return t * sign; -} - -static inline float -srgb_encode_f(float t) -{ - float sign = 1; - if (t < 0) { - t = -t; - sign = -1; - } - t = t <= (float)0.0031306684425217108 - ? (float)12.92 * t - : (float)1.055 * powf(t, 1 / (float)2.4) - (float)0.055; - return t * sign; -} - -static inline double -srgb_decode(double t) -{ - double sign = 1; - if (t < 0) { - t = -t; - sign = -1; +SRGB_ENCODE(double, _d,) +SRGB_ENCODE(float, _f, f) +#undef SRGB_ENCODE + +#define SRGB_DECODE(TYPE, NAME_SUFFIX, MATH_SUFFIX)\ + static inline TYPE\ + srgb_decode##NAME_SUFFIX(TYPE t)\ + {\ + TYPE sign = 1;\ + if (t < 0) {\ + t = -t;\ + sign = -1;\ + }\ + t = t <= (TYPE)0.0031306684425217108 * (TYPE)12.92\ + ? t / (TYPE)12.92\ + : pow##MATH_SUFFIX((t + (TYPE)0.055) / (TYPE)1.055, (TYPE)2.4);\ + return t * sign;\ } - t = t <= 0.0031306684425217108 * 12.92 - ? t / 12.92 - : pow((t + 0.055) / 1.055, 2.4); - return t * sign; -} - -static inline float -srgb_decode_f(float t) -{ - float sign = 1; - if (t < 0) { - t = -t; - sign = -1; +SRGB_DECODE(double, _d,) +SRGB_DECODE(float, _f, f) +#undef SRGB_DECODE + + +#define MATRIX_MULTIPLY_FUNCTION(FUNCTION, TYPE, R1C1, R1C2, R1C3, R2C1, R2C2, R2C3, R3C1, R3C2, R3C3)\ + static inline void\ + FUNCTION(TYPE ia, TYPE ib, TYPE ic, TYPE *oa, TYPE *ob, TYPE *oc)\ + {\ + *oa = (TYPE)(R1C1) * ia + (TYPE)(R1C2) * ib + (TYPE)(R1C3) * ic;\ + *ob = (TYPE)(R2C1) * ia + (TYPE)(R2C2) * ib + (TYPE)(R2C3) * ic;\ + *oc = (TYPE)(R3C1) * ia + (TYPE)(R3C2) * ib + (TYPE)(R3C3) * ic;\ } - t = t <= (float)0.0031306684425217108 * (float)12.92 - ? t / (float)12.92 - : powf((t + (float)0.055) / (float)1.055, (float)2.4); - return t * sign; -} - -static inline void -yuv_to_srgb(double y, double u, double v, double *r, double *g, double *b) -{ -#define MULTIPLY(CY, CU, CV) ((CY) * y + (CU) * u + (CV) * v) - *r = MULTIPLY(1, 0.00028328010485821202317155420580263580632163211703, 1.14070449590558520291949662350816652178764343261719); - *g = MULTIPLY(1, -0.39630886669497211727275498560629785060882568359375, -0.58107364288228224857846271333983168005943298339844); - *b = MULTIPLY(1, 2.03990003507541306504435851820744574069976806640625, 0.00017179031692307700847528739718228507626918144524); -#undef MULTIPLY -} - -static inline void -yuv_to_srgb_f(float y, float u, float v, float *r, float *g, float *b) -{ -#define MULTIPLY(CY, CU, CV) ((float)(CY) * y + (float)(CU) * u + (float)(CV) * v) - *r = MULTIPLY(1, 0.00028328010485821202317155420580263580632163211703, 1.14070449590558520291949662350816652178764343261719); - *g = MULTIPLY(1, -0.39630886669497211727275498560629785060882568359375, -0.58107364288228224857846271333983168005943298339844); - *b = MULTIPLY(1, 2.03990003507541306504435851820744574069976806640625, 0.00017179031692307700847528739718228507626918144524); -#undef MULTIPLY -} - -static inline void -srgb_to_yuv(double r, double g, double b, double *y, double *u, double *v) -{ -#define MULTIPLY(CR, CG, CB) ((CR) * r + (CG) * g + (CB) * b) - *y = MULTIPLY(0.299, 0.587, 0.114); - *u = MULTIPLY(-0.14662756598240470062854967636667424812912940979004, - -0.28771586836102963635752871596196200698614120483398, - 0.43434343434343436474165400795754976570606231689453); - *v = MULTIPLY( 0.61456892577224520035628074765554629266262054443359, - -0.51452282157676354490405401520547457039356231689453, - -0.10004610419548178035231700278018251992762088775635); -#undef MULTIPLY -} - -static inline void -srgb_to_yuv_f(float r, float g, float b, float *y, float *u, float *v) -{ -#define MULTIPLY(CR, CG, CB) ((float)(CR) * r + (float)(CG) * g + (float)(CB) * b) - *y = MULTIPLY(0.299, 0.587, 0.114); - *u = MULTIPLY(-0.14662756598240470062854967636667424812912940979004, - -0.28771586836102963635752871596196200698614120483398, - 0.43434343434343436474165400795754976570606231689453); - *v = MULTIPLY( 0.61456892577224520035628074765554629266262054443359, - -0.51452282157676354490405401520547457039356231689453, - -0.10004610419548178035231700278018251992762088775635); -#undef MULTIPLY -} - -static inline void -ciexyz_to_srgb(double x, double y, double z, double *r, double *g, double *b) -{ -#define MULTIPLY(CX, CY, CZ) ((CX) * x + (CY) * y + (CZ) * z) - *r = MULTIPLY(3.240446254647737500675930277794, -1.537134761820080575134284117667, -0.498530193022728718155178739835); - *g = MULTIPLY(-0.969266606244679751469561779231, 1.876011959788370209167851498933, 0.041556042214430065351304932619); - *b = MULTIPLY(0.055643503564352832235773149705, -0.204026179735960239147729566866, 1.057226567722703292062647051353); -#undef MULTIPLY -} - -static inline void -ciexyz_to_srgb_f(float x, float y, float z, float *r, float *g, float *b) -{ -#define MULTIPLY(CX, CY, CZ) ((float)(CX) * x + (float)(CY) * y + (float)(CZ) * z) - *r = MULTIPLY(3.240446254647737500675930277794, -1.537134761820080575134284117667, -0.498530193022728718155178739835); - *g = MULTIPLY(-0.969266606244679751469561779231, 1.876011959788370209167851498933, 0.041556042214430065351304932619); - *b = MULTIPLY(0.055643503564352832235773149705, -0.204026179735960239147729566866, 1.057226567722703292062647051353); -#undef MULTIPLY -} - -static inline void -srgb_to_ciexyz(double r, double g, double b, double *x, double *y, double *z) -{ -#define MULTIPLY(CR, CG, CB) ((CR) * r + (CG) * g + (CB) * b) - *x = MULTIPLY(0.412457445582367576708548995157, 0.357575865245515878143578447634, 0.180437247826399665973085006954); - *y = MULTIPLY(0.212673370378408277403536885686, 0.715151730491031756287156895269, 0.072174899130559869164791564344); - *z = MULTIPLY(0.019333942761673460208893260415, 0.119191955081838593666354597644, 0.950302838552371742508739771438); -#undef MULTIPLY -} - -static inline void -srgb_to_ciexyz_f(float r, float g, float b, float *x, float *y, float *z) -{ -#define MULTIPLY(CR, CG, CB) ((float)(CR) * r + (float)(CG) * g + (float)(CB) * b) - *x = MULTIPLY(0.412457445582367576708548995157, 0.357575865245515878143578447634, 0.180437247826399665973085006954); - *y = MULTIPLY(0.212673370378408277403536885686, 0.715151730491031756287156895269, 0.072174899130559869164791564344); - *z = MULTIPLY(0.019333942761673460208893260415, 0.119191955081838593666354597644, 0.950302838552371742508739771438); -#undef MULTIPLY -} - -static inline void -scaled_yuv_to_ciexyz(double y, double u, double v, double *xp, double *yp, double *zp) -{ -#define MULTIPLY(CY, CU, CV) ((CY) * y + (CU) * u + (CV) * v) - *xp = MULTIPLY( 0.00001450325106667098632156481796684488472237717360, - 0.00000345586790639342739093228633329157872822179343, - 0.00000400923398630552893485111398685916128670214675); - *yp = MULTIPLY( 0.00001525902189669641837040624243737596543724066578, - -0.00000207722814409390653614547427030512238843584782, - -0.00000263898607692305410302407824019166326934282552); - *zp = MULTIPLY( 0.00001661446153041708825425643025752719950105529279, - 0.00002885925752619118069149627137104374696718878113, - -0.00000071781086875769179526501342566979779746816348); -#undef MULTIPLY -} - -static inline void -scaled_yuv_to_ciexyz_f(float y, float u, float v, float *xp, float *yp, float *zp) -{ -#define MULTIPLY(CY, CU, CV) ((float)(CY) * y + (float)(CU) * u + (float)(CV) * v) - *xp = MULTIPLY( 0.00001450325106667098632156481796684488472237717360, - 0.00000345586790639342739093228633329157872822179343, - 0.00000400923398630552893485111398685916128670214675); - *yp = MULTIPLY( 0.00001525902189669641837040624243737596543724066578, - -0.00000207722814409390653614547427030512238843584782, - -0.00000263898607692305410302407824019166326934282552); - *zp = MULTIPLY( 0.00001661446153041708825425643025752719950105529279, - 0.00002885925752619118069149627137104374696718878113, - -0.00000071781086875769179526501342566979779746816348); -#undef MULTIPLY -} - -static inline void -ciexyz_to_scaled_yuv(double x, double y, double z, double *yp, double *up, double *vp) -{ -#define MULTIPLY(CX, CY, CZ) ((CX) * x + (CY) * y + (CZ) * z) - *yp = MULTIPLY( 26625.38231027395886485464870929718017578125, - 40524.0090949436053051613271236419677734375, - -271.5313105642117079696618020534515380859375); - *up = MULTIPLY( -11278.3751445417292416095733642578125, - -26409.91773157499847002327442169189453125, - 34100.5706543184860493056476116180419921875); - *vp = MULTIPLY( 162829.60100012840121053159236907958984375, - -123829.313212639070115983486175537109375, - -28411.65702312920984695665538311004638671875); -#undef MULTIPLY -} - -static inline void -ciexyz_to_scaled_yuv_f(float x, float y, float z, float *yp, float *up, float *vp) -{ -#define MULTIPLY(CX, CY, CZ) ((float)(CX) * x + (float)(CY) * y + (float)(CZ) * z) - *yp = MULTIPLY( 26625.38231027395886485464870929718017578125, - 40524.0090949436053051613271236419677734375, - -271.5313105642117079696618020534515380859375); - *up = MULTIPLY( -11278.3751445417292416095733642578125, - -26409.91773157499847002327442169189453125, - 34100.5706543184860493056476116180419921875); - *vp = MULTIPLY( 162829.60100012840121053159236907958984375, - -123829.313212639070115983486175537109375, - -28411.65702312920984695665538311004638671875); -#undef MULTIPLY -} +#define MATRIX_MULTIPLY_FUNCTIONS(FUNCTION_BASE, ...)\ + MATRIX_MULTIPLY_FUNCTION(FUNCTION_BASE##_d, double, __VA_ARGS__)\ + MATRIX_MULTIPLY_FUNCTION(FUNCTION_BASE##_f, float, __VA_ARGS__) + +MATRIX_MULTIPLY_FUNCTIONS(yuv_to_srgb, + 1, + 0.00028328010485821202317155420580263580632163211703, + 1.14070449590558520291949662350816652178764343261719, + 1, + -0.39630886669497211727275498560629785060882568359375, + -0.58107364288228224857846271333983168005943298339844, + 1, + 2.03990003507541306504435851820744574069976806640625, + 0.00017179031692307700847528739718228507626918144524) + +MATRIX_MULTIPLY_FUNCTIONS(srgb_to_yuv, + 0.299, 0.587, 0.114, + -0.14662756598240470062854967636667424812912940979004, + -0.28771586836102963635752871596196200698614120483398, + 0.43434343434343436474165400795754976570606231689453, + 0.61456892577224520035628074765554629266262054443359, + -0.51452282157676354490405401520547457039356231689453, + -0.10004610419548178035231700278018251992762088775635) + +MATRIX_MULTIPLY_FUNCTIONS(ciexyz_to_srgb, + 3.240446254647737500675930277794, + -1.537134761820080575134284117667, + -0.498530193022728718155178739835, + -0.969266606244679751469561779231, + 1.876011959788370209167851498933, + 0.041556042214430065351304932619, + 0.055643503564352832235773149705, + -0.204026179735960239147729566866, + 1.057226567722703292062647051353) + +MATRIX_MULTIPLY_FUNCTIONS(srgb_to_ciexyz, + 0.412457445582367576708548995157, + 0.357575865245515878143578447634, + 0.180437247826399665973085006954, + 0.212673370378408277403536885686, + 0.715151730491031756287156895269, + 0.072174899130559869164791564344, + 0.019333942761673460208893260415, + 0.119191955081838593666354597644, + 0.950302838552371742508739771438) + +MATRIX_MULTIPLY_FUNCTIONS(scaled_yuv_to_ciexyz, + 0.00001450325106667098632156481796684488472237717360, + 0.00000345586790639342739093228633329157872822179343, + 0.00000400923398630552893485111398685916128670214675, + 0.00001525902189669641837040624243737596543724066578, + -0.00000207722814409390653614547427030512238843584782, + -0.00000263898607692305410302407824019166326934282552, + 0.00001661446153041708825425643025752719950105529279, + 0.00002885925752619118069149627137104374696718878113, + -0.00000071781086875769179526501342566979779746816348) + +MATRIX_MULTIPLY_FUNCTIONS(ciexyz_to_scaled_yuv, + 26625.38231027395886485464870929718017578125, + 40524.0090949436053051613271236419677734375, + -271.5313105642117079696618020534515380859375, + -11278.3751445417292416095733642578125, + -26409.91773157499847002327442169189453125, + 34100.5706543184860493056476116180419921875, + 162829.60100012840121053159236907958984375, + -123829.313212639070115983486175537109375, + -28411.65702312920984695665538311004638671875) + +#undef MATRIX_MULTIPLY_FUNCTIONS +#undef MATRIX_MULTIPLY_FUNCTION diff --git a/src/util/io.h b/src/util/io.h @@ -40,10 +40,10 @@ enreadall(int status, int fd, void *buf, size_t n, const char *fname) return (size_t)r; } -int pwriteall(int fd, void *buf, size_t n, size_t ptr); +int pwriteall(int fd, void *buf, size_t n, off_t ptr); static inline void -enpwriteall(int status, int fd, void *buf, size_t n, size_t ptr, const char *fname) +enpwriteall(int status, int fd, void *buf, size_t n, off_t ptr, const char *fname) { if (pwriteall(fd, buf, n, ptr)) enprintf(status, "pwrite %s:", fname); diff --git a/src/video-math.h b/src/video-math.h @@ -0,0 +1,90 @@ +/* See LICENSE file for copyright and license details. */ +#include <math.h> + +static inline double +nnpow(double a, double b) +{ + int neg = a < 0; + a = pow(neg ? -a : a, b); + return neg ? -a : a; +} + +static inline float +nnpowf(float a, float b) +{ + int neg = a < 0; + a = powf(neg ? -a : a, b); + return neg ? -a : a; +} + +#define GENERIC(TYPE, FUNC, ...)\ + TYPE: FUNC(__VA_ARGS__),\ + TYPE *: FUNC(__VA_ARGS__),\ + TYPE **: FUNC(__VA_ARGS__),\ + TYPE ***: FUNC(__VA_ARGS__),\ + const TYPE: FUNC(__VA_ARGS__),\ + const TYPE *: FUNC(__VA_ARGS__),\ + const TYPE **: FUNC(__VA_ARGS__),\ + const TYPE ***: FUNC(__VA_ARGS__) + +#define MATH_GENERIC_1(FUNC, A) (_Generic((A),\ + GENERIC(double, FUNC, A),\ + GENERIC(float, FUNC##f, A))) + +#define MATH_GENERIC_N(FUNC, A, ...) (_Generic((A),\ + GENERIC(double, FUNC, A, __VA_ARGS__),\ + GENERIC(float, FUNC##f, A, __VA_ARGS__))) + +#define BLIND_GENERIC_1(FUNC, A) (_Generic((A),\ + GENERIC(double, FUNC##_d, A),\ + GENERIC(float, FUNC##_f, A))) + +#define BLIND_GENERIC_N(FUNC, A, ...) (_Generic((A),\ + GENERIC(double, FUNC##_d, A, __VA_ARGS__), \ + GENERIC(float, FUNC##_f, A, __VA_ARGS__))) + +#define pow(...) MATH_GENERIC_N(pow, __VA_ARGS__) +#define log2(...) MATH_GENERIC_1(log2, __VA_ARGS__) +#define abs(...) MATH_GENERIC_1(fabs, __VA_ARGS__) +#define sqrt(...) MATH_GENERIC_1(sqrt, __VA_ARGS__) +#define exp(...) MATH_GENERIC_1(exp, __VA_ARGS__) +#define g_isnan(...) MATH_GENERIC_1(isnan, __VA_ARGS__) +#define g_isinf(...) MATH_GENERIC_1(isinf, __VA_ARGS__) +#define g_isfinite(...) MATH_GENERIC_1(isfinite, __VA_ARGS__) +#define nnpow(...) MATH_GENERIC_N(nnpow, __VA_ARGS__) + +#define srgb_encode(...) BLIND_GENERIC_1(srgb_encode, __VA_ARGS__) +#define srgb_decode(...) BLIND_GENERIC_1(srgb_decode, __VA_ARGS__) + +#define yuv_to_srgb(a, b, c, d, e, f)\ + BLIND_GENERIC_N(yuv_to_srgb, (a), (b), (c), (void *)(d), (void *)(e), (void *)(f)) +#define srgb_to_yuv(a, b, c, d, e, f)\ + BLIND_GENERIC_N(srgb_to_yuv, (a), (b), (c), (void *)(d), (void *)(e), (void *)(f)) +#define ciexyz_to_srgb(a, b, c, d, e, f)\ + BLIND_GENERIC_N(ciexyz_to_srgb, (a), (b), (c), (void *)(d), (void *)(e), (void *)(f)) +#define srgb_to_ciexyz(a, b, c, d, e, f)\ + BLIND_GENERIC_N(srgb_to_ciexyz, (a), (b), (c), (void *)(d), (void *)(e), (void *)(f)) +#define scaled_yuv_to_ciexyz(a, b, c, d, e, f)\ + BLIND_GENERIC_N(scaled_yuv_to_ciexyz, (a), (b), (c), (void *)(d), (void *)(e), (void *)(f)) +#define ciexyz_to_scaled_yuv(a, b, c, d, e, f)\ + BLIND_GENERIC_N(ciexyz_to_scaled_yuv, (a), (b), (c), (void *)(d), (void *)(e), (void *)(f)) + +#define htole(A) (_Generic((A),\ + uint8_t: (A),\ + int8_t: (uint8_t)(A),\ + uint16_t: htole16(A),\ + int16_t: (uint16_t)htole16((uint16_t)(A)),\ + uint32_t: htole32(A),\ + int32_t: (uint32_t)htole32((uint32_t)(A)),\ + uint64_t: htole64(A),\ + int64_t: (uint64_t)htole64((uint64_t)(A)))) + +#define letoh(A) (_Generic((A),\ + uint8_t: (A),\ + int8_t: (uint8_t)(A),\ + uint16_t: le16toh(A),\ + int16_t: (uint16_t)le16toh((uint16_t)(A)),\ + uint32_t: le32toh(A),\ + int32_t: (uint32_t)le32toh((uint32_t)(A)),\ + uint64_t: le64toh(A),\ + int64_t: (uint64_t)le64toh((uint64_t)(A))))