dav1d.c 6.41 KB
Newer Older
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
/*
 * 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"
Marvin Scholz's avatar
Marvin Scholz committed
29
#include "version.h"
30 31 32 33 34 35

#include <assert.h>
#include <errno.h>
#include <math.h>
#include <stdio.h>
#include <string.h>
36 37 38
#ifdef HAVE_UNISTD_H
# include <unistd.h>
#endif
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

#include "dav1d/data.h"

#include "input/input.h"

#include "output/output.h"

#include "dav1d_cli_parse.h"

static void print_stats(const int istty, const unsigned n,
                        const unsigned num)
{
    const char *pre_string = istty ? "\r" : "";
    const char *post_string = istty ? "" : "\n";

    if (num == 0xFFFFFFFFU) {
        fprintf(stderr, "%sDecoded %u frames%s", pre_string, n, post_string);
    } else {
        fprintf(stderr, "%sDecoded %u/%u frames (%.1lf%%)%s",
                pre_string, n, num, 100.0 * n / num, post_string);
    }
}

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;
    unsigned n_out = 0, total, fps[2];
    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);

85 86
    if ((res = input_open(&in, cli_settings.demuxer,
                          cli_settings.inputfile,
87 88 89 90 91
                          fps, &total)) < 0)
    {
        return res;
    }
    for (unsigned i = 0; i <= cli_settings.skip; i++) {
92 93
        if ((res = input_read(in, &data)) < 0) {
            input_close(in);
94
            return res;
95
        }
96 97 98 99
        if (i < cli_settings.skip) dav1d_data_unref(&data);
    }

    if (!cli_settings.quiet)
100
        fprintf(stderr, "dav1d %s - by VideoLAN\n", DAV1D_VERSION);
101

102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
    // 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);
    }

119 120 121 122 123 124 125 126 127
    //getc(stdin);
    if (cli_settings.limit != 0 && cli_settings.limit < total)
        total = cli_settings.limit;

    if ((res = dav1d_open(&c, &lib_settings)))
        return res;

    do {
        memset(&p, 0, sizeof(p));
128 129 130 131 132 133 134 135 136
        if ((res = dav1d_send_data(c, &data)) < 0) {
            if (res != -EAGAIN) {
                fprintf(stderr, "Error decoding frame: %s\n",
                        strerror(-res));
                break;
            }
        }

        if ((res = dav1d_get_picture(c, &p)) < 0) {
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
            if (res != -EAGAIN) {
                fprintf(stderr, "Error decoding frame: %s\n",
                        strerror(-res));
                break;
            }
            res = 0;
        } else {
            if (!n_out) {
                if ((res = output_open(&out, cli_settings.muxer,
                                       cli_settings.outputfile,
                                       &p.p, fps)) < 0)
                {
                    return res;
                }
            }
            if ((res = output_write(out, &p)) < 0)
                break;
            n_out++;
            if (!cli_settings.quiet)
                print_stats(istty, n_out, total);
        }

        if (cli_settings.limit && n_out == cli_settings.limit)
            break;
    } while (data.sz > 0 || !input_read(in, &data));

163 164
    if (data.sz > 0) dav1d_data_unref(&data);

165 166
    // flush
    if (res == 0) while (!cli_settings.limit || n_out < cli_settings.limit) {
167
        if ((res = dav1d_get_picture(c, &p)) < 0) {
168 169 170
            if (res != -EAGAIN) {
                fprintf(stderr, "Error decoding frame: %s\n",
                        strerror(-res));
171
            } else {
172
                res = 0;
173 174
                break;
            }
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
        } else {
            if (!n_out) {
                if ((res = output_open(&out, cli_settings.muxer,
                                       cli_settings.outputfile,
                                       &p.p, fps)) < 0)
                {
                    return res;
                }
            }
            if ((res = output_write(out, &p)) < 0)
                break;
            n_out++;
            if (!cli_settings.quiet)
                print_stats(istty, n_out, total);
        }
    }

    input_close(in);
    if (out) {
        if (!cli_settings.quiet && istty)
            fprintf(stderr, "\n");
196 197 198 199
        if (cli_settings.verify)
            res |= output_verify(out, cli_settings.verify);
        else
            output_close(out);
200 201 202 203
    } else {
        fprintf(stderr, "No data decoded\n");
        res = 1;
    }
204
    dav1d_close(&c);
205 206 207

    return res;
}