aboutsummaryrefslogtreecommitdiffstats
path: root/src/stream.c
blob: cb487ad7dca76608f842ca23e877e2c4c480da67 (plain) (blame)
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
/* See LICENSE file for copyright and license details. */
#include "stream.h"
#include "util.h"

#include <sys/stat.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

void
eninit_stream(int status, struct stream *stream)
{
	ssize_t r;
	size_t n;
	char *p = NULL, *w, *h, *f, *end;

	for (stream->ptr = 0; p;) {
		r = read(stream->fd, stream->buf + stream->ptr, sizeof(stream->buf) - stream->ptr);
		if (r < 0)
			enprintf(status, "read %s:", stream->file);
		if (r == 0)
			goto bad_format;
		stream->ptr += (size_t)r;
		p = memchr(stream->buf, '\n', stream->ptr);
	}

	*p = '\0';
	w = strchr(stream->buf, ' ');
	if (!w)
		goto bad_format;
	h = strchr(w + 1, ' ');
	if (!h)
		goto bad_format;
	f = strchr(h + 1, ' ');
	if (!f)
		goto bad_format;
	*w++ = *h++ = *f++ = '\0';

	if (strlen(f) >= sizeof(stream->pixfmt))
		goto bad_format;
	strcpy(stream->pixfmt, f);
	errno = 0;
	stream->frames = strtoul(stream->buf, &end, 10);
	if (errno == ERANGE && *stream->buf != '-')
		eprintf("%s: too long\n", stream->file);
	if (errno || *end)
		goto bad_format;
	errno = 0;
	stream->width = strtoul(w, &end, 10);
	if (errno == ERANGE && *stream->buf != '-')
		eprintf("%s: too wide\n", stream->file);
	if (errno || *end)
		goto bad_format;
	errno = 0;
	stream->height = strtoul(h, &end, 10);
	if (errno == ERANGE && *stream->buf != '-')
		eprintf("%s: too tall\n", stream->file);
	if (errno || *end)
		goto bad_format;

	if (!stream->width)
		eprintf("%s: width is zero\n", stream->file);
	if (!stream->height)
		eprintf("%s: height is zero\n", stream->file);

	n = (size_t)(p - stream->buf) + 1;
	memmove(stream->buf, stream->buf + n, stream->ptr -= n);
	while (stream->ptr < 5) {
		r = read(stream->fd, stream->buf + stream->ptr, sizeof(stream->buf) - stream->ptr);
		if (r < 0)
			enprintf(status, "read %s:", stream->file);
		if (r == 0)
			goto bad_format;
		stream->ptr += (size_t)r;
	}
	if (stream->buf[0] != '\0' ||
	    stream->buf[1] != 'u' || stream->buf[2] != 'i' ||
	    stream->buf[3] != 'v' || stream->buf[4] != 'f')
		goto bad_format;
	memmove(stream->buf, stream->buf + 5, stream->ptr -= 5);

	enset_pixel_size(status, stream);

	return;
bad_format:
	enprintf(status, "%s: file format not supported%s\n", stream->file);
}


int
set_pixel_size(struct stream *stream)
{
	if (!strcmp(stream->pixfmt, "xyza"))
		stream->pixel_size = 4 * sizeof(double);
	else
		return -1;
	return 0;
}

void
enset_pixel_size(int status, struct stream *stream)
{
	if (set_pixel_size(stream))
		enprintf(status, "file %s uses unsupported pixel format: %s\n",
			 stream->file, stream->pixfmt);
}


void
fprint_stream_head(FILE *fp, struct stream *stream)
{
	fprintf(fp, "%zu %zu %zu %s\n%cuivf",
		stream->frames, stream->width, stream->height, stream->pixfmt, 0);
}


size_t
enread_stream(int status, struct stream *stream, size_t n)
{
	ssize_t r = read(stream->fd, stream->buf + stream->ptr,
			 sizeof(stream->buf) - stream->ptr < n ?
			 sizeof(stream->buf) - stream->ptr : n);
	if (r < 0)
		enprintf(status, "read %s:", stream->file);
	stream->ptr += (size_t)r;
	return (size_t)r;
}


void
eninf_check_fd(int status, int fd, const char *file)
{
	struct stat st;
	if (fstat(fd, &st))
		eprintf("fstat %s:", file);
	if (S_ISREG(st.st_mode))
		eprintf("%s is a regular file, refusing infinite write\n");
}