1 /*
2 * Copyright © 2018, VideoLAN and dav1d authors
3 * Copyright © 2018, Two Orioles, LLC
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice, this
10 * list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include "config.h"
29 #include "vcs_version.h"
30 #include "cli_config.h"
31
32 #include <assert.h>
33 #include <errno.h>
34 #include <inttypes.h>
35 #include <math.h>
36 #include <signal.h>
37 #include <stdlib.h>
38 #include <stdio.h>
39 #include <string.h>
40 #include <time.h>
41 #if HAVE_UNISTD_H
42 # include <unistd.h>
43 #endif
44 #if HAVE_IO_H
45 # include <io.h>
46 #endif
47 #ifdef _WIN32
48 # include <windows.h>
49 #endif
50 #ifdef __APPLE__
51 #include <mach/mach_time.h>
52 #endif
53
54 #include "dav1d/dav1d.h"
55
56 #include "input/input.h"
57
58 #include "output/output.h"
59
60 #include "dav1d_cli_parse.h"
61
get_time_nanos(void)62 static uint64_t get_time_nanos(void) {
63 #ifdef _WIN32
64 LARGE_INTEGER frequency;
65 QueryPerformanceFrequency(&frequency);
66 LARGE_INTEGER t;
67 QueryPerformanceCounter(&t);
68 uint64_t seconds = t.QuadPart / frequency.QuadPart;
69 uint64_t fractions = t.QuadPart % frequency.QuadPart;
70 return 1000000000 * seconds + 1000000000 * fractions / frequency.QuadPart;
71 #elif HAVE_CLOCK_GETTIME
72 struct timespec ts;
73 clock_gettime(CLOCK_MONOTONIC, &ts);
74 return 1000000000ULL * ts.tv_sec + ts.tv_nsec;
75 #elif defined(__APPLE__)
76 mach_timebase_info_data_t info;
77 mach_timebase_info(&info);
78 return mach_absolute_time() * info.numer / info.denom;
79 #endif
80 }
81
sleep_nanos(uint64_t d)82 static void sleep_nanos(uint64_t d) {
83 #ifdef _WIN32
84 Sleep((unsigned)(d / 1000000));
85 #else
86 const struct timespec ts = {
87 .tv_sec = (time_t)(d / 1000000000),
88 .tv_nsec = d % 1000000000,
89 };
90 nanosleep(&ts, NULL);
91 #endif
92 }
93
synchronize(const int realtime,const unsigned cache,const unsigned n_out,const uint64_t nspf,const uint64_t tfirst,uint64_t * const elapsed,FILE * const frametimes)94 static void synchronize(const int realtime, const unsigned cache,
95 const unsigned n_out, const uint64_t nspf,
96 const uint64_t tfirst, uint64_t *const elapsed,
97 FILE *const frametimes)
98 {
99 const uint64_t tcurr = get_time_nanos();
100 const uint64_t last = *elapsed;
101 *elapsed = tcurr - tfirst;
102 if (realtime) {
103 const uint64_t deadline = nspf * n_out;
104 if (*elapsed < deadline) {
105 const uint64_t remaining = deadline - *elapsed;
106 if (remaining > nspf * cache) sleep_nanos(remaining - nspf * cache);
107 *elapsed = deadline;
108 }
109 }
110 if (frametimes) {
111 const uint64_t frametime = *elapsed - last;
112 fprintf(frametimes, "%" PRIu64 "\n", frametime);
113 fflush(frametimes);
114 }
115 }
116
print_stats(const int istty,const unsigned n,const unsigned num,const uint64_t elapsed,const double i_fps)117 static void print_stats(const int istty, const unsigned n, const unsigned num,
118 const uint64_t elapsed, const double i_fps)
119 {
120 char buf[80], *b = buf, *const end = buf + 80;
121
122 if (istty)
123 *b++ = '\r';
124 if (num == 0xFFFFFFFF)
125 b += snprintf(b, end - b, "Decoded %u frames", n);
126 else
127 b += snprintf(b, end - b, "Decoded %u/%u frames (%.1lf%%)",
128 n, num, 100.0 * n / num);
129 if (b < end) {
130 const double d_fps = 1e9 * n / elapsed;
131 if (i_fps) {
132 const double speed = d_fps / i_fps;
133 b += snprintf(b, end - b, " - %.2lf/%.2lf fps (%.2lfx)",
134 d_fps, i_fps, speed);
135 } else {
136 b += snprintf(b, end - b, " - %.2lf fps", d_fps);
137 }
138 }
139 if (!istty)
140 strcpy(b > end - 2 ? end - 2 : b, "\n");
141 fputs(buf, stderr);
142 }
143
picture_alloc(Dav1dPicture * const p,void * const _)144 static int picture_alloc(Dav1dPicture *const p, void *const _) {
145 const int hbd = p->p.bpc > 8;
146 const int aligned_w = (p->p.w + 127) & ~127;
147 const int aligned_h = (p->p.h + 127) & ~127;
148 const int has_chroma = p->p.layout != DAV1D_PIXEL_LAYOUT_I400;
149 const int ss_ver = p->p.layout == DAV1D_PIXEL_LAYOUT_I420;
150 const int ss_hor = p->p.layout != DAV1D_PIXEL_LAYOUT_I444;
151 ptrdiff_t y_stride = aligned_w << hbd;
152 ptrdiff_t uv_stride = has_chroma ? y_stride >> ss_hor : 0;
153 /* Due to how mapping of addresses to sets works in most L1 and L2 cache
154 * implementations, strides of multiples of certain power-of-two numbers
155 * may cause multiple rows of the same superblock to map to the same set,
156 * causing evictions of previous rows resulting in a reduction in cache
157 * hit rate. Avoid that by slightly padding the stride when necessary. */
158 if (!(y_stride & 1023))
159 y_stride += DAV1D_PICTURE_ALIGNMENT;
160 if (!(uv_stride & 1023) && has_chroma)
161 uv_stride += DAV1D_PICTURE_ALIGNMENT;
162 p->stride[0] = -y_stride;
163 p->stride[1] = -uv_stride;
164 const size_t y_sz = y_stride * aligned_h;
165 const size_t uv_sz = uv_stride * (aligned_h >> ss_ver);
166 const size_t pic_size = y_sz + 2 * uv_sz;
167
168 uint8_t *const buf = malloc(pic_size + DAV1D_PICTURE_ALIGNMENT * 2);
169 if (!buf) return DAV1D_ERR(ENOMEM);
170 p->allocator_data = buf;
171
172 const ptrdiff_t align_m1 = DAV1D_PICTURE_ALIGNMENT - 1;
173 uint8_t *const data = (uint8_t *)(((ptrdiff_t)buf + align_m1) & ~align_m1);
174 p->data[0] = data + y_sz - y_stride;
175 p->data[1] = has_chroma ? data + y_sz + uv_sz * 1 - uv_stride : NULL;
176 p->data[2] = has_chroma ? data + y_sz + uv_sz * 2 - uv_stride : NULL;
177
178 return 0;
179 }
180
picture_release(Dav1dPicture * const p,void * const _)181 static void picture_release(Dav1dPicture *const p, void *const _) {
182 free(p->allocator_data);
183 }
184
185 static volatile sig_atomic_t signal_terminate;
signal_handler(const int s)186 static void signal_handler(const int s) {
187 signal_terminate = 1;
188 }
189
main(const int argc,char * const * const argv)190 int main(const int argc, char *const *const argv) {
191 const int istty = isatty(fileno(stderr));
192 int res = 0;
193 CLISettings cli_settings;
194 Dav1dSettings lib_settings;
195 DemuxerContext *in;
196 MuxerContext *out = NULL;
197 Dav1dPicture p;
198 Dav1dContext *c;
199 Dav1dData data;
200 unsigned n_out = 0, total, fps[2], timebase[2];
201 uint64_t nspf, tfirst, elapsed;
202 double i_fps;
203 FILE *frametimes = NULL;
204 const unsigned version = dav1d_version_api();
205 const int major = DAV1D_API_MAJOR(version);
206 const int minor = DAV1D_API_MINOR(version);
207 const int patch = DAV1D_API_PATCH(version);
208
209 if (DAV1D_API_VERSION_MAJOR != major ||
210 DAV1D_API_VERSION_MINOR > minor) {
211 fprintf(stderr, "Version mismatch (library: %d.%d.%d, executable: %d.%d.%d)\n",
212 major, minor, patch,
213 DAV1D_API_VERSION_MAJOR,
214 DAV1D_API_VERSION_MINOR,
215 DAV1D_API_VERSION_PATCH);
216 return EXIT_FAILURE;
217 }
218
219 parse(argc, argv, &cli_settings, &lib_settings);
220 if (cli_settings.neg_stride) {
221 lib_settings.allocator.alloc_picture_callback = picture_alloc;
222 lib_settings.allocator.release_picture_callback = picture_release;
223 }
224
225 if ((res = input_open(&in, cli_settings.demuxer,
226 cli_settings.inputfile,
227 fps, &total, timebase)) < 0)
228 {
229 return EXIT_FAILURE;
230 }
231 for (unsigned i = 0; i <= cli_settings.skip; i++) {
232 if ((res = input_read(in, &data)) < 0) {
233 input_close(in);
234 return EXIT_FAILURE;
235 }
236 if (i < cli_settings.skip) dav1d_data_unref(&data);
237 }
238
239 if (!cli_settings.quiet)
240 fprintf(stderr, "dav1d %s - by VideoLAN\n", dav1d_version());
241
242 // skip frames until a sequence header is found
243 if (cli_settings.skip) {
244 Dav1dSequenceHeader seq;
245 unsigned seq_skip = 0;
246 while (dav1d_parse_sequence_header(&seq, data.data, data.sz)) {
247 if ((res = input_read(in, &data)) < 0) {
248 input_close(in);
249 return EXIT_FAILURE;
250 }
251 seq_skip++;
252 }
253 if (seq_skip && !cli_settings.quiet)
254 fprintf(stderr,
255 "skipped %u packets due to missing sequence header\n",
256 seq_skip);
257 }
258
259 if (cli_settings.limit != 0 && cli_settings.limit < total)
260 total = cli_settings.limit;
261
262 if ((res = dav1d_open(&c, &lib_settings)))
263 return EXIT_FAILURE;
264
265 if (cli_settings.frametimes)
266 frametimes = fopen(cli_settings.frametimes, "w");
267
268 if (cli_settings.realtime != REALTIME_CUSTOM) {
269 if (fps[1] == 0) {
270 i_fps = 0;
271 nspf = 0;
272 } else {
273 i_fps = (double)fps[0] / fps[1];
274 nspf = 1000000000ULL * fps[1] / fps[0];
275 }
276 } else {
277 i_fps = cli_settings.realtime_fps;
278 nspf = (uint64_t)(1000000000.0 / cli_settings.realtime_fps);
279 }
280 tfirst = get_time_nanos();
281
282 #ifdef _WIN32
283 signal(SIGINT, signal_handler);
284 signal(SIGTERM, signal_handler);
285 #else
286 static const struct sigaction sa = {
287 .sa_handler = signal_handler,
288 .sa_flags = SA_RESETHAND,
289 };
290 sigaction(SIGINT, &sa, NULL);
291 sigaction(SIGTERM, &sa, NULL);
292 #endif
293
294 do {
295 if ((res = signal_terminate)) break;
296
297 memset(&p, 0, sizeof(p));
298 if ((res = dav1d_send_data(c, &data)) < 0) {
299 if (res != DAV1D_ERR(EAGAIN)) {
300 dav1d_data_unref(&data);
301 fprintf(stderr, "Error decoding frame: %s\n",
302 strerror(DAV1D_ERR(res)));
303 if (res != DAV1D_ERR(EINVAL)) break;
304 }
305 }
306
307 if ((res = dav1d_get_picture(c, &p)) < 0) {
308 if (res != DAV1D_ERR(EAGAIN)) {
309 fprintf(stderr, "Error decoding frame: %s\n",
310 strerror(DAV1D_ERR(res)));
311 if (res != DAV1D_ERR(EINVAL)) break;
312 }
313 res = 0;
314 } else {
315 if (!n_out) {
316 if ((res = output_open(&out, cli_settings.muxer,
317 cli_settings.outputfile,
318 &p.p, fps)) < 0)
319 {
320 if (frametimes) fclose(frametimes);
321 return EXIT_FAILURE;
322 }
323 }
324 if ((res = output_write(out, &p)) < 0)
325 break;
326 n_out++;
327 if (nspf || !cli_settings.quiet) {
328 synchronize(cli_settings.realtime, cli_settings.realtime_cache,
329 n_out, nspf, tfirst, &elapsed, frametimes);
330 }
331 if (!cli_settings.quiet)
332 print_stats(istty, n_out, total, elapsed, i_fps);
333 }
334
335 if (cli_settings.limit && n_out == cli_settings.limit)
336 break;
337 } while (data.sz > 0 || !input_read(in, &data));
338
339 if (data.sz > 0) dav1d_data_unref(&data);
340
341 // flush
342 if (res == 0) while (!cli_settings.limit || n_out < cli_settings.limit) {
343 if ((res = signal_terminate)) break;
344
345 if ((res = dav1d_get_picture(c, &p)) < 0) {
346 if (res != DAV1D_ERR(EAGAIN)) {
347 fprintf(stderr, "Error decoding frame: %s\n",
348 strerror(DAV1D_ERR(res)));
349 if (res != DAV1D_ERR(EINVAL)) break;
350 } else {
351 res = 0;
352 break;
353 }
354 } else {
355 if (!n_out) {
356 if ((res = output_open(&out, cli_settings.muxer,
357 cli_settings.outputfile,
358 &p.p, fps)) < 0)
359 {
360 if (frametimes) fclose(frametimes);
361 return EXIT_FAILURE;
362 }
363 }
364 if ((res = output_write(out, &p)) < 0)
365 break;
366 n_out++;
367 if (nspf || !cli_settings.quiet) {
368 synchronize(cli_settings.realtime, cli_settings.realtime_cache,
369 n_out, nspf, tfirst, &elapsed, frametimes);
370 }
371 if (!cli_settings.quiet)
372 print_stats(istty, n_out, total, elapsed, i_fps);
373 }
374 }
375
376 if (frametimes) fclose(frametimes);
377
378 input_close(in);
379 if (out) {
380 if (!cli_settings.quiet && istty)
381 fprintf(stderr, "\n");
382 if (cli_settings.verify)
383 res |= output_verify(out, cli_settings.verify);
384 else
385 output_close(out);
386 } else {
387 fprintf(stderr, "No data decoded\n");
388 res = 1;
389 }
390 dav1d_close(&c);
391
392 return (res == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
393 }
394