blind

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

stream.h (7298B)


      1 /* See LICENSE file for copyright and license details. */
      2 #include <limits.h>
      3 #include <stddef.h>
      4 #include <stdio.h>
      5 
      6 #define STREAM_HEAD_MAX (3 * INTSTRLEN(size_t) + sizeof(((struct stream *)0)->pixfmt) + 10)
      7 
      8 #define XPRINTF_HEAD_FMT "%zu %zu %zu %s\n%cuivf"
      9 #define XPRINTF_HEAD_ARGS(FRAMES, WIDTH, HEIGHT, PIXFMT)\
     10 	(size_t)(FRAMES), (size_t)(WIDTH), (size_t)(HEIGHT), (PIXFMT), 0
     11 
     12 #define XPRINTF_HEAD_FMT_FMT(FFRAMES, FWIDTH, FHEIGHT)\
     13 	FFRAMES" "FWIDTH" "FHEIGHT" %s\n%cuivf"
     14 #define XPRINTF_HEAD_FMT_ARGS(FRAMES, WIDTH, HEIGHT, PIXFMT)\
     15 	(FRAMES), (WIDTH), (HEIGHT), (PIXFMT), 0
     16 
     17 #define SPRINTF_HEAD_ZN(BUF, FRAMES, WIDTH, HEIGHT, PIXFMT, LENP)\
     18 	sprintf(BUF, XPRINTF_HEAD_FMT"%zn", XPRINTF_HEAD_ARGS(FRAMES, WIDTH, HEIGHT, PIXFMT), LENP)
     19 
     20 #define SPRINTF_HEAD(BUF, FRAMES, WIDTH, HEIGHT, PIXFMT)\
     21 	sprintf(BUF, XPRINTF_HEAD_FMT, XPRINTF_HEAD_ARGS(FRAMES, WIDTH, HEIGHT, PIXFMT))
     22 
     23 #define FPRINTF_HEAD(FP, FRAMES, WIDTH, HEIGHT, PIXFMT)\
     24 	fprintf(FP, XPRINTF_HEAD_FMT, XPRINTF_HEAD_ARGS(FRAMES, WIDTH, HEIGHT, PIXFMT))
     25 
     26 #define DPRINTF_HEAD(FD, FRAMES, WIDTH, HEIGHT, PIXFMT)\
     27 	dprintf(FD, XPRINTF_HEAD_FMT, XPRINTF_HEAD_ARGS(FRAMES, WIDTH, HEIGHT, PIXFMT))
     28 
     29 #define SPRINTF_HEAD_FMT(BUF, FFRAMES, FRAMES, FWIDTH, WIDTH, FHEIGHT, HEIGHT, PIXFMT)\
     30 	sprintf(BUF, XPRINTF_HEAD_FMT_FMT(FFRAMES, FWIDTH, FHEIGHT), XPRINTF_HEAD_FMT_ARGS(FRAMES, WIDTH, HEIGHT, PIXFMT))
     31 
     32 #define FPRINTF_HEAD_FMT(FP, FFRAMES, FRAMES, FWIDTH, WIDTH, FHEIGHT, HEIGHT, PIXFMT)\
     33 	fprintf(FP, XPRINTF_HEAD_FMT_FMT(FFRAMES, FWIDTH, FHEIGHT), XPRINTF_HEAD_FMT_ARGS(FRAMES, WIDTH, HEIGHT, PIXFMT))
     34 
     35 #define DPRINTF_HEAD_FMT(FD, FFRAMES, FRAMES, FWIDTH, WIDTH, FHEIGHT, HEIGHT, PIXFMT)\
     36 	dprintf(FD, XPRINTF_HEAD_FMT_FMT(FFRAMES, FWIDTH, FHEIGHT), XPRINTF_HEAD_FMT_ARGS(FRAMES, WIDTH, HEIGHT, PIXFMT))
     37 
     38 #define einit_stream(...)             eninit_stream(1, __VA_ARGS__)
     39 #define eopen_stream(...)             enopen_stream(1, __VA_ARGS__)
     40 #define eset_pixel_format(...)        enset_pixel_format(1, __VA_ARGS__)
     41 #define eread_stream(...)             enread_stream(1, __VA_ARGS__)
     42 #define einf_check_fd(...)            eninf_check_fd(1, __VA_ARGS__)
     43 #define echeck_dimensions(...)        encheck_dimensions(1, __VA_ARGS__)
     44 #define echeck_dimensions_custom(...) encheck_dimensions_custom(1, __VA_ARGS__)
     45 #define echeck_compat(...)            encheck_compat(1, __VA_ARGS__)
     46 #define select_print_format(...)      nselect_print_format(1, __VA_ARGS__)
     47 #define eread_segment(...)            enread_segment(1, __VA_ARGS__)
     48 #define eread_frame(...)              enread_frame(1, __VA_ARGS__)
     49 #define eread_row(...)                enread_row(1, __VA_ARGS__)
     50 #define esend_frames(...)             ensend_frames(1, __VA_ARGS__)
     51 #define esend_rows(...)               ensend_rows(1, __VA_ARGS__)
     52 #define esend_pixels(...)             ensend_pixels(1, __VA_ARGS__)
     53 #define esend_stream(...)             ensend_stream(1, __VA_ARGS__)
     54 
     55 #define process_stream(...)                 nprocess_stream(1, __VA_ARGS__)
     56 #define process_each_frame_segmented(...)   nprocess_each_frame_segmented(1, __VA_ARGS__)
     57 #define process_two_streams(...)            nprocess_two_streams(1, __VA_ARGS__)
     58 #define process_multiple_streams(...)       nprocess_multiple_streams(1, __VA_ARGS__)
     59 #define process_each_frame_two_streams(...) nprocess_each_frame_two_streams(1, __VA_ARGS__)
     60 
     61 enum dimension {
     62 	WIDTH = 1,
     63 	HEIGHT = 2,
     64 	LENGTH = 4
     65 };
     66 
     67 enum colour_space {
     68 	CIEXYZ,
     69 	YUV_NONLINEAR,
     70 	SRGB_NONLINEAR,
     71 	SRGB
     72 };
     73 
     74 enum alpha {
     75 	NO_ALPHA,
     76 	UNPREMULTIPLIED,
     77 	PREMULTIPLIED /* not used */
     78 };
     79 
     80 enum encoding {
     81 	FLOAT,
     82 	DOUBLE,
     83 	LONG_DOUBLE, /* not used */
     84 	UINT8, /* not used */
     85 	UINT16,
     86 	UINT32, /* not used */
     87 	UINT64 /* not used */
     88 };
     89 
     90 enum endian {
     91 	HOST,
     92 	LITTLE,
     93 	BIG /* not used */
     94 };
     95 
     96 struct stream {
     97 	size_t frames;
     98 	size_t width;
     99 	size_t height;
    100 	size_t n_chan;
    101 	size_t chan_size;
    102 	size_t pixel_size;
    103 	char pixfmt[32];
    104 	enum colour_space space;
    105 	enum alpha alpha;
    106 	enum encoding encoding;
    107 	enum endian endian;
    108 	short int alpha_chan;
    109 	short int luma_chan;
    110 	int fd;
    111 	size_t ptr;
    112 	size_t xptr;
    113 	char buf[BUFSIZ];
    114 	const char *file;
    115 	size_t headlen;
    116 	size_t row_size;
    117 	size_t col_size;
    118 	size_t frame_size;
    119 };
    120 
    121 void eninit_stream(int status, struct stream *stream);
    122 void enopen_stream(int status, struct stream *stream, const char *file);
    123 int set_pixel_format(struct stream *stream, const char *pixfmt);
    124 void enset_pixel_format(int status, struct stream *stream, const char *pixfmt);
    125 void fprint_stream_head(FILE *fp, struct stream *stream);
    126 int dprint_stream_head(int fd, struct stream *stream);
    127 size_t enread_stream(int status, struct stream *stream, size_t n);
    128 void eninf_check_fd(int status, int fd, const char *file);
    129 void encheck_dimensions(int status, const struct stream *stream, enum dimension dimensions, const char *prefix);
    130 void encheck_compat(int status, const struct stream *a, const struct stream *b);
    131 const char *get_pixel_format(const char *specified, const char *current);
    132 const char *nselect_print_format(int status, const char *format, enum encoding encoding, const char *fmt);
    133 int enread_segment(int status, struct stream *stream, void *buf, size_t n);
    134 size_t ensend_frames(int status, struct stream *stream, int outfd, size_t frames, const char *outfname);
    135 size_t ensend_rows(int status, struct stream *stream, int outfd, size_t rows, const char *outfname);
    136 size_t ensend_pixels(int status, struct stream *stream, int outfd, size_t pixels, const char *outfname);
    137 int ensend_stream(int status, struct stream *stream, int outfd, const char *outfname);
    138 
    139 void nprocess_stream(int status, struct stream *stream, void (*process)(struct stream *stream, size_t n));
    140 
    141 void nprocess_each_frame_segmented(int status, struct stream *stream, int output_fd, const char* output_fname,
    142 				   void (*process)(struct stream *stream, size_t n, size_t frame));
    143 
    144 void nprocess_two_streams(int status, struct stream *left, struct stream *right, int output_fd, const char* output_fname,
    145 			  void (*process)(struct stream *left, struct stream *right, size_t n));
    146 
    147 void nprocess_multiple_streams(int status, struct stream *streams, size_t n_streams, int output_fd, const char* output_fname,
    148 			       int shortest, void (*process)(struct stream *streams, size_t n_streams, size_t n));
    149 
    150 void nprocess_each_frame_two_streams(int status, struct stream *left, struct stream *right, int output_fd, const char* output_fname,
    151 				     void (*process)(char *restrict output, char *restrict lbuf, char *restrict rbuf,
    152 						     struct stream *left, struct stream *right));
    153 
    154 static inline int
    155 enread_frame(int status, struct stream *stream, void *buf)
    156 {
    157 	return enread_segment(status, stream, buf, stream->frame_size);
    158 }
    159 
    160 static inline int
    161 enread_row(int status, struct stream *stream, void *buf)
    162 {
    163 	return enread_segment(status, stream, buf, stream->row_size);
    164 }
    165 
    166 static inline void
    167 encheck_dimensions_custom(int status, size_t width, size_t height, size_t frames,
    168 			  size_t pixel_size, const char *prefix, const char *fname)
    169 {
    170 	enum dimension dims = 0;
    171 	struct stream stream;
    172 	dims |= width  ? WIDTH  : 0;
    173 	dims |= height ? HEIGHT : 0;
    174 	dims |= frames ? LENGTH : 0;
    175 	stream.width      = width;
    176 	stream.height     = height;
    177 	stream.frames     = frames;
    178 	stream.pixel_size = pixel_size;
    179 	stream.file       = fname;
    180 	encheck_dimensions(status, &stream, dims, prefix);
    181 }