2018-11-24 04:53:30 +03:00
|
|
|
/*
|
|
|
|
* Copyright © 2018, VideoLAN and dav1d authors
|
|
|
|
* Copyright © 2018, Two Orioles, LLC
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* 1. Redistributions of source code must retain the above copyright notice, this
|
|
|
|
* list of conditions and the following disclaimer.
|
|
|
|
*
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
* this list of conditions and the following disclaimer in the documentation
|
|
|
|
* and/or other materials provided with the distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
|
|
|
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
|
|
|
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
|
|
|
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
|
|
|
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
|
|
|
|
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
|
|
|
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "config.h"
|
2019-03-19 20:35:09 +03:00
|
|
|
#include "vcs_version.h"
|
2019-07-24 20:03:48 +03:00
|
|
|
#include "cli_config.h"
|
2018-11-24 04:53:30 +03:00
|
|
|
|
|
|
|
#include <errno.h>
|
2019-06-18 23:52:59 +03:00
|
|
|
#include <inttypes.h>
|
2018-11-24 04:53:30 +03:00
|
|
|
#include <math.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
2019-06-18 23:52:59 +03:00
|
|
|
#include <time.h>
|
2018-11-24 04:53:30 +03:00
|
|
|
#ifdef HAVE_UNISTD_H
|
|
|
|
# include <unistd.h>
|
|
|
|
#endif
|
2019-03-19 20:35:09 +03:00
|
|
|
#ifdef HAVE_IO_H
|
|
|
|
# include <io.h>
|
|
|
|
#endif
|
2019-06-18 23:52:59 +03:00
|
|
|
#ifdef _WIN32
|
|
|
|
# include <windows.h>
|
|
|
|
#endif
|
2019-07-24 20:03:48 +03:00
|
|
|
#if defined(HAVE_MACH_ABSOLUTE_TIME)
|
|
|
|
#include <mach/mach_time.h>
|
|
|
|
#endif
|
2018-11-24 04:53:30 +03:00
|
|
|
|
2019-03-19 20:35:09 +03:00
|
|
|
#include "dav1d/dav1d.h"
|
2018-11-24 04:53:30 +03:00
|
|
|
|
|
|
|
#include "input/input.h"
|
|
|
|
|
|
|
|
#include "output/output.h"
|
|
|
|
|
|
|
|
#include "dav1d_cli_parse.h"
|
|
|
|
|
2019-06-18 23:52:59 +03:00
|
|
|
static uint64_t get_time_nanos(void) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
LARGE_INTEGER frequency;
|
|
|
|
QueryPerformanceFrequency(&frequency);
|
|
|
|
LARGE_INTEGER t;
|
|
|
|
QueryPerformanceCounter(&t);
|
|
|
|
return 1000000000 * t.QuadPart / frequency.QuadPart;
|
2019-07-24 20:03:48 +03:00
|
|
|
#elif defined(HAVE_CLOCK_GETTIME)
|
2019-06-18 23:52:59 +03:00
|
|
|
struct timespec ts;
|
|
|
|
clock_gettime(CLOCK_MONOTONIC, &ts);
|
|
|
|
return 1000000000ULL * ts.tv_sec + ts.tv_nsec;
|
2019-07-24 20:03:48 +03:00
|
|
|
#elif defined(HAVE_MACH_ABSOLUTE_TIME)
|
|
|
|
mach_timebase_info_data_t info;
|
|
|
|
mach_timebase_info(&info);
|
|
|
|
return mach_absolute_time() * info.numer / info.denom;
|
2019-06-18 23:52:59 +03:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void sleep_nanos(uint64_t d) {
|
|
|
|
#ifdef _WIN32
|
|
|
|
Sleep((unsigned)(d / 1000000));
|
|
|
|
#else
|
|
|
|
const struct timespec ts = {
|
|
|
|
.tv_sec = (time_t)(d / 1000000000),
|
|
|
|
.tv_nsec = d % 1000000000,
|
|
|
|
};
|
|
|
|
nanosleep(&ts, NULL);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
static void 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)
|
2018-11-24 04:53:30 +03:00
|
|
|
{
|
2019-06-18 23:52:59 +03:00
|
|
|
const uint64_t tcurr = get_time_nanos();
|
|
|
|
const uint64_t last = *elapsed;
|
|
|
|
*elapsed = tcurr - tfirst;
|
|
|
|
if (realtime) {
|
|
|
|
const uint64_t deadline = nspf * n_out;
|
|
|
|
if (*elapsed < deadline) {
|
|
|
|
const uint64_t remaining = deadline - *elapsed;
|
|
|
|
if (remaining > nspf * cache) sleep_nanos(remaining - nspf * cache);
|
|
|
|
*elapsed = deadline;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (frametimes) {
|
|
|
|
const uint64_t frametime = *elapsed - last;
|
|
|
|
fprintf(frametimes, "%" PRIu64 "\n", frametime);
|
|
|
|
fflush(frametimes);
|
|
|
|
}
|
|
|
|
}
|
2018-11-24 04:53:30 +03:00
|
|
|
|
2019-06-18 23:52:59 +03:00
|
|
|
static void print_stats(const int istty, const unsigned n, const unsigned num,
|
|
|
|
const uint64_t elapsed, const double i_fps)
|
|
|
|
{
|
|
|
|
if (istty) fputs("\r", stderr);
|
|
|
|
const double d_fps = 1e9 * n / elapsed;
|
|
|
|
const double speed = d_fps / i_fps;
|
|
|
|
if (num == 0xFFFFFFFF) {
|
|
|
|
fprintf(stderr, "Decoded %u frames", n);
|
2018-11-24 04:53:30 +03:00
|
|
|
} else {
|
2019-06-18 23:52:59 +03:00
|
|
|
fprintf(stderr, "Decoded %u/%u frames (%.1lf%%)", n, num,
|
|
|
|
100.0 * n / num);
|
2018-11-24 04:53:30 +03:00
|
|
|
}
|
2019-06-18 23:52:59 +03:00
|
|
|
if (i_fps)
|
|
|
|
fprintf(stderr, " - %.2lf/%.2lf fps (%.2lfx)", d_fps, i_fps, speed);
|
|
|
|
if (!istty) fputs("\n", stderr);
|
2018-11-24 04:53:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(const int argc, char *const *const argv) {
|
|
|
|
const int istty = isatty(fileno(stderr));
|
|
|
|
int res = 0;
|
|
|
|
CLISettings cli_settings;
|
|
|
|
Dav1dSettings lib_settings;
|
|
|
|
DemuxerContext *in;
|
|
|
|
MuxerContext *out = NULL;
|
|
|
|
Dav1dPicture p;
|
|
|
|
Dav1dContext *c;
|
|
|
|
Dav1dData data;
|
2019-09-23 21:02:33 +03:00
|
|
|
unsigned n_out = 0, total, fps[2], timebase[2];
|
2019-06-18 23:52:59 +03:00
|
|
|
uint64_t nspf, tfirst, elapsed;
|
|
|
|
double i_fps;
|
|
|
|
FILE *frametimes = NULL;
|
2018-11-24 04:53:30 +03:00
|
|
|
const char *version = dav1d_version();
|
|
|
|
|
|
|
|
if (strcmp(version, DAV1D_VERSION)) {
|
|
|
|
fprintf(stderr, "Version mismatch (library: %s, executable: %s)\n",
|
|
|
|
version, DAV1D_VERSION);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
init_demuxers();
|
|
|
|
init_muxers();
|
|
|
|
parse(argc, argv, &cli_settings, &lib_settings);
|
|
|
|
|
|
|
|
if ((res = input_open(&in, cli_settings.demuxer,
|
|
|
|
cli_settings.inputfile,
|
2019-09-23 21:02:33 +03:00
|
|
|
fps, &total, timebase)) < 0)
|
2018-11-24 04:53:30 +03:00
|
|
|
{
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
for (unsigned i = 0; i <= cli_settings.skip; i++) {
|
|
|
|
if ((res = input_read(in, &data)) < 0) {
|
|
|
|
input_close(in);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
if (i < cli_settings.skip) dav1d_data_unref(&data);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!cli_settings.quiet)
|
2019-03-19 20:35:09 +03:00
|
|
|
fprintf(stderr, "dav1d %s - by VideoLAN\n", dav1d_version());
|
2018-11-24 04:53:30 +03:00
|
|
|
|
2018-12-02 00:59:40 +03:00
|
|
|
// skip frames until a sequence header is found
|
|
|
|
if (cli_settings.skip) {
|
|
|
|
Dav1dSequenceHeader seq;
|
|
|
|
unsigned seq_skip = 0;
|
|
|
|
while (dav1d_parse_sequence_header(&seq, data.data, data.sz)) {
|
|
|
|
if ((res = input_read(in, &data)) < 0) {
|
|
|
|
input_close(in);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
seq_skip++;
|
|
|
|
}
|
|
|
|
if (seq_skip && !cli_settings.quiet)
|
|
|
|
fprintf(stderr,
|
|
|
|
"skipped %u packets due to missing sequence header\n",
|
|
|
|
seq_skip);
|
|
|
|
}
|
|
|
|
|
2018-11-24 04:53:30 +03:00
|
|
|
//getc(stdin);
|
|
|
|
if (cli_settings.limit != 0 && cli_settings.limit < total)
|
|
|
|
total = cli_settings.limit;
|
|
|
|
|
|
|
|
if ((res = dav1d_open(&c, &lib_settings)))
|
|
|
|
return res;
|
|
|
|
|
2019-06-18 23:52:59 +03:00
|
|
|
if (cli_settings.frametimes)
|
|
|
|
frametimes = fopen(cli_settings.frametimes, "w");
|
|
|
|
|
|
|
|
if (cli_settings.realtime != REALTIME_CUSTOM) {
|
|
|
|
if (fps[1] == 0) {
|
|
|
|
i_fps = 0;
|
|
|
|
nspf = 0;
|
|
|
|
} else {
|
|
|
|
i_fps = (double)fps[0] / fps[1];
|
|
|
|
nspf = 1000000000ULL * fps[1] / fps[0];
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
i_fps = cli_settings.realtime_fps;
|
|
|
|
nspf = (uint64_t)(1000000000.0 / cli_settings.realtime_fps);
|
|
|
|
}
|
|
|
|
tfirst = get_time_nanos();
|
|
|
|
|
2018-11-24 04:53:30 +03:00
|
|
|
do {
|
|
|
|
memset(&p, 0, sizeof(p));
|
2018-11-27 17:04:37 +03:00
|
|
|
if ((res = dav1d_send_data(c, &data)) < 0) {
|
2019-05-10 08:20:18 +03:00
|
|
|
if (res != DAV1D_ERR(EAGAIN)) {
|
2018-11-27 17:04:37 +03:00
|
|
|
fprintf(stderr, "Error decoding frame: %s\n",
|
2019-07-24 20:03:48 +03:00
|
|
|
strerror(DAV1D_ERR(res)));
|
2018-11-27 17:04:37 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((res = dav1d_get_picture(c, &p)) < 0) {
|
2019-05-10 08:20:18 +03:00
|
|
|
if (res != DAV1D_ERR(EAGAIN)) {
|
2018-11-24 04:53:30 +03:00
|
|
|
fprintf(stderr, "Error decoding frame: %s\n",
|
2019-07-24 20:03:48 +03:00
|
|
|
strerror(DAV1D_ERR(res)));
|
2018-11-24 04:53:30 +03:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
res = 0;
|
|
|
|
} else {
|
|
|
|
if (!n_out) {
|
|
|
|
if ((res = output_open(&out, cli_settings.muxer,
|
|
|
|
cli_settings.outputfile,
|
|
|
|
&p.p, fps)) < 0)
|
|
|
|
{
|
2019-06-18 23:52:59 +03:00
|
|
|
if (frametimes) fclose(frametimes);
|
2018-11-24 04:53:30 +03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((res = output_write(out, &p)) < 0)
|
|
|
|
break;
|
|
|
|
n_out++;
|
2019-06-18 23:52:59 +03:00
|
|
|
if (nspf) {
|
|
|
|
synchronize(cli_settings.realtime, cli_settings.realtime_cache,
|
|
|
|
n_out, nspf, tfirst, &elapsed, frametimes);
|
|
|
|
}
|
2018-11-24 04:53:30 +03:00
|
|
|
if (!cli_settings.quiet)
|
2019-06-18 23:52:59 +03:00
|
|
|
print_stats(istty, n_out, total, elapsed, i_fps);
|
2018-11-24 04:53:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (cli_settings.limit && n_out == cli_settings.limit)
|
|
|
|
break;
|
|
|
|
} while (data.sz > 0 || !input_read(in, &data));
|
|
|
|
|
|
|
|
if (data.sz > 0) dav1d_data_unref(&data);
|
|
|
|
|
|
|
|
// flush
|
|
|
|
if (res == 0) while (!cli_settings.limit || n_out < cli_settings.limit) {
|
2018-11-27 17:04:37 +03:00
|
|
|
if ((res = dav1d_get_picture(c, &p)) < 0) {
|
2019-05-10 08:20:18 +03:00
|
|
|
if (res != DAV1D_ERR(EAGAIN)) {
|
2018-11-24 04:53:30 +03:00
|
|
|
fprintf(stderr, "Error decoding frame: %s\n",
|
2019-07-24 20:03:48 +03:00
|
|
|
strerror(DAV1D_ERR(res)));
|
2018-11-27 17:04:37 +03:00
|
|
|
} else {
|
2018-11-24 04:53:30 +03:00
|
|
|
res = 0;
|
2018-11-27 17:04:37 +03:00
|
|
|
break;
|
|
|
|
}
|
2018-11-24 04:53:30 +03:00
|
|
|
} else {
|
|
|
|
if (!n_out) {
|
|
|
|
if ((res = output_open(&out, cli_settings.muxer,
|
|
|
|
cli_settings.outputfile,
|
|
|
|
&p.p, fps)) < 0)
|
|
|
|
{
|
2019-06-18 23:52:59 +03:00
|
|
|
if (frametimes) fclose(frametimes);
|
2018-11-24 04:53:30 +03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ((res = output_write(out, &p)) < 0)
|
|
|
|
break;
|
|
|
|
n_out++;
|
2019-06-18 23:52:59 +03:00
|
|
|
if (nspf) {
|
|
|
|
synchronize(cli_settings.realtime, cli_settings.realtime_cache,
|
|
|
|
n_out, nspf, tfirst, &elapsed, frametimes);
|
|
|
|
}
|
2018-11-24 04:53:30 +03:00
|
|
|
if (!cli_settings.quiet)
|
2019-06-18 23:52:59 +03:00
|
|
|
print_stats(istty, n_out, total, elapsed, i_fps);
|
2018-11-24 04:53:30 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-06-18 23:52:59 +03:00
|
|
|
if (frametimes) fclose(frametimes);
|
|
|
|
|
2018-11-24 04:53:30 +03:00
|
|
|
input_close(in);
|
|
|
|
if (out) {
|
|
|
|
if (!cli_settings.quiet && istty)
|
|
|
|
fprintf(stderr, "\n");
|
2018-11-27 17:04:37 +03:00
|
|
|
if (cli_settings.verify)
|
|
|
|
res |= output_verify(out, cli_settings.verify);
|
|
|
|
else
|
|
|
|
output_close(out);
|
2018-11-24 04:53:30 +03:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "No data decoded\n");
|
|
|
|
res = 1;
|
|
|
|
}
|
|
|
|
dav1d_close(&c);
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|