summaryrefslogtreecommitdiff
path: root/chromium/third_party/dav1d/libdav1d/tools/output/output.c
blob: cf66d2ec5ab5ca4b224c752838cbf14de10472c7 (plain)
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
/*
 * 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"
#include "cli_config.h"

#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "common/attributes.h"
#include "common/intops.h"

#include "output/output.h"
#include "output/muxer.h"

struct MuxerContext {
    MuxerPriv *data;
    const Muxer *impl;
    int one_file_per_frame;
    unsigned fps[2];
    const char *filename;
    int framenum;
};

extern const Muxer null_muxer;
extern const Muxer md5_muxer;
extern const Muxer xxh3_muxer;
extern const Muxer yuv_muxer;
extern const Muxer y4m2_muxer;
static const Muxer *muxers[] = {
    &null_muxer,
    &md5_muxer,
#if HAVE_XXHASH_H
    &xxh3_muxer,
#endif
    &yuv_muxer,
    &y4m2_muxer,
    NULL
};

static const char *find_extension(const char *const f) {
    const size_t l = strlen(f);

    if (l == 0) return NULL;

    const char *const end = &f[l - 1], *step = end;
    while ((*step >= 'a' && *step <= 'z') ||
           (*step >= 'A' && *step <= 'Z') ||
           (*step >= '0' && *step <= '9'))
    {
        step--;
    }

    return (step < end && step > f && *step == '.' && step[-1] != '/') ?
           &step[1] : NULL;
}

int output_open(MuxerContext **const c_out,
                const char *const name, const char *const filename,
                const Dav1dPictureParameters *const p, const unsigned fps[2])
{
    const Muxer *impl;
    MuxerContext *c;
    unsigned i;
    int res;
    int name_offset = 0;

    if (name) {
        name_offset = 5 * !strncmp(name, "frame", 5);
        for (i = 0; muxers[i]; i++) {
            if (!strcmp(muxers[i]->name, &name[name_offset])) {
                impl = muxers[i];
                break;
            }
        }
        if (!muxers[i]) {
            fprintf(stderr, "Failed to find muxer named \"%s\"\n", name);
            return DAV1D_ERR(ENOPROTOOPT);
        }
    } else if (!strcmp(filename, "/dev/null")) {
        impl = muxers[0];
    } else {
        const char *const ext = find_extension(filename);
        if (!ext) {
            fprintf(stderr, "No extension found for file %s\n", filename);
            return -1;
        }
        for (i = 0; muxers[i]; i++) {
            if (!strcmp(muxers[i]->extension, ext)) {
                impl = muxers[i];
                break;
            }
        }
        if (!muxers[i]) {
            fprintf(stderr, "Failed to find muxer for extension \"%s\"\n", ext);
            return DAV1D_ERR(ENOPROTOOPT);
        }
    }

    if (!(c = malloc(sizeof(MuxerContext) + impl->priv_data_size))) {
        fprintf(stderr, "Failed to allocate memory\n");
        return DAV1D_ERR(ENOMEM);
    }
    c->impl = impl;
    c->data = (MuxerPriv *) &c[1];
    int have_num_pattern = 0;
    for (const char *ptr = filename ? strchr(filename, '%') : NULL;
         !have_num_pattern && ptr; ptr = strchr(ptr, '%'))
    {
        ptr++; // skip '%'
        while (*ptr >= '0' && *ptr <= '9')
            ptr++; // skip length indicators
        have_num_pattern = *ptr == 'n';
    }
    c->one_file_per_frame = name_offset || (!name && have_num_pattern);

    if (c->one_file_per_frame) {
        c->fps[0] = fps[0];
        c->fps[1] = fps[1];
        c->filename = filename;
        c->framenum = 0;
    } else if (impl->write_header &&
               (res = impl->write_header(c->data, filename, p, fps)) < 0)
    {
        free(c);
        return res;
    }
    *c_out = c;

    return 0;
}

static void safe_strncat(char *const dst, const int dst_len,
                         const char *const src, const int src_len)
{
    if (!src_len) return;
    const int dst_fill = (int) strlen(dst);
    assert(dst_fill < dst_len);
    const int to_copy = imin(src_len, dst_len - dst_fill - 1);
    if (!to_copy) return;
    memcpy(dst + dst_fill, src, to_copy);
    dst[dst_fill + to_copy] = 0;
}

static void assemble_field(char *const dst, const int dst_len,
                           const char *const fmt, const int fmt_len,
                           const int field)
{
    char fmt_copy[32];

    assert(fmt[0] == '%');
    fmt_copy[0] = '%';
    if (fmt[1] >= '1' && fmt[1] <= '9') {
        fmt_copy[1] = '0'; // pad with zeroes, not spaces
        fmt_copy[2] = 0;
    } else {
        fmt_copy[1] = 0;
    }
    safe_strncat(fmt_copy, sizeof(fmt_copy), &fmt[1], fmt_len - 1);
    safe_strncat(fmt_copy, sizeof(fmt_copy), "d", 1);

    char tmp[32];
    snprintf(tmp, sizeof(tmp), fmt_copy, field);

    safe_strncat(dst, dst_len, tmp, (int) strlen(tmp));
}

static void assemble_filename(MuxerContext *const ctx, char *const filename,
                              const int filename_size,
                              const Dav1dPictureParameters *const p)
{
    filename[0] = 0;
    const int framenum = ctx->framenum++;
    assert(ctx->filename);
    const char *ptr = ctx->filename, *iptr;
    while ((iptr = strchr(ptr, '%'))) {
        safe_strncat(filename, filename_size, ptr, (int) (iptr - ptr));
        ptr = iptr;

        const char *iiptr = &iptr[1]; // skip '%'
        while (*iiptr >= '0' && *iiptr <= '9')
            iiptr++; // skip length indicators

        switch (*iiptr) {
        case 'w':
            assemble_field(filename, filename_size, ptr, (int) (iiptr - ptr), p->w);
            break;
        case 'h':
            assemble_field(filename, filename_size, ptr, (int) (iiptr - ptr), p->h);
            break;
        case 'n':
            assemble_field(filename, filename_size, ptr, (int) (iiptr - ptr), framenum);
            break;
        default:
            safe_strncat(filename, filename_size, "%", 1);
            ptr = &iptr[1];
            continue;
        }

        ptr = &iiptr[1];
    }
    safe_strncat(filename, filename_size, ptr, (int) strlen(ptr));
}

int output_write(MuxerContext *const ctx, Dav1dPicture *const p) {
    int res;

    if (ctx->one_file_per_frame && ctx->impl->write_header) {
        char filename[1024];
        assemble_filename(ctx, filename, sizeof(filename), &p->p);
        res = ctx->impl->write_header(ctx->data, filename, &p->p, ctx->fps);
        if (res < 0)
            return res;
    }
    if ((res = ctx->impl->write_picture(ctx->data, p)) < 0)
        return res;
    if (ctx->one_file_per_frame && ctx->impl->write_trailer)
        ctx->impl->write_trailer(ctx->data);

    return 0;
}

void output_close(MuxerContext *const ctx) {
    if (!ctx->one_file_per_frame && ctx->impl->write_trailer)
        ctx->impl->write_trailer(ctx->data);
    free(ctx);
}

int output_verify(MuxerContext *const ctx, const char *const md5_str) {
    const int res = ctx->impl->verify ?
        ctx->impl->verify(ctx->data, md5_str) : 0;
    free(ctx);
    return res;
}