Coverage Report

Created: 2026-06-15 06:25

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/dav1d/src/lib.c
Line
Count
Source
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
31
#include <errno.h>
32
#include <string.h>
33
34
#if defined(__linux__) && HAVE_DLSYM
35
#include <dlfcn.h>
36
#endif
37
38
#include "dav1d/dav1d.h"
39
#include "dav1d/data.h"
40
41
#include "common/validate.h"
42
43
#include "src/cpu.h"
44
#include "src/fg_apply.h"
45
#include "src/internal.h"
46
#include "src/log.h"
47
#include "src/obu.h"
48
#include "src/qm.h"
49
#include "src/ref.h"
50
#include "src/thread_task.h"
51
#include "src/wedge.h"
52
53
3
static COLD void init_internal(void) {
54
3
    dav1d_init_cpu();
55
3
    dav1d_init_ii_wedge_masks();
56
3
    dav1d_init_intra_edge_tree();
57
3
    dav1d_init_qm_tables();
58
3
    dav1d_init_thread();
59
3
}
60
61
0
COLD const char *dav1d_version(void) {
62
0
    return DAV1D_VERSION;
63
0
}
64
65
0
COLD unsigned dav1d_version_api(void) {
66
0
    return (DAV1D_API_VERSION_MAJOR << 16) |
67
0
           (DAV1D_API_VERSION_MINOR <<  8) |
68
0
           (DAV1D_API_VERSION_PATCH <<  0);
69
0
}
70
71
76.9k
COLD void dav1d_default_settings(Dav1dSettings *const s) {
72
76.9k
    s->n_threads = 0;
73
76.9k
    s->max_frame_delay = 0;
74
76.9k
    s->apply_grain = 1;
75
76.9k
    s->allocator.cookie = NULL;
76
76.9k
    s->allocator.alloc_picture_callback = dav1d_default_picture_alloc;
77
76.9k
    s->allocator.release_picture_callback = dav1d_default_picture_release;
78
76.9k
    s->logger.cookie = NULL;
79
76.9k
    s->logger.callback = dav1d_log_default_callback;
80
76.9k
    s->operating_point = 0;
81
76.9k
    s->all_layers = 1; // just until the tests are adjusted
82
76.9k
    s->frame_size_limit = 0;
83
76.9k
    s->strict_std_compliance = 0;
84
76.9k
    s->output_invisible_frames = 0;
85
76.9k
    s->inloop_filters = DAV1D_INLOOPFILTER_ALL;
86
76.9k
    s->decode_frame_type = DAV1D_DECODEFRAMETYPE_ALL;
87
76.9k
}
88
89
static void close_internal(Dav1dContext **const c_out, int flush);
90
91
#if defined(__linux__) && HAVE_DLSYM && defined(__GLIBC__)
92
NO_SANITIZE("cfi-icall") // CFI is broken with dlsym()
93
76.9k
static COLD size_t get_stack_size_internal(const pthread_attr_t *const thread_attr) {
94
    /* glibc has an issue where the size of the TLS is subtracted from the stack
95
     * size instead of allocated separately. As a result the specified stack
96
     * size may be insufficient when used in an application with large amounts
97
     * of TLS data. The following is a workaround to compensate for that.
98
     * See https://sourceware.org/bugzilla/show_bug.cgi?id=11787 */
99
76.9k
    size_t (*const get_minstack)(const pthread_attr_t*) =
100
76.9k
        dlsym(RTLD_DEFAULT, "__pthread_get_minstack");
101
76.9k
    if (get_minstack)
102
76.9k
        return get_minstack(thread_attr) - PTHREAD_STACK_MIN;
103
0
    return 0;
104
76.9k
}
105
#else
106
#define get_stack_size_internal(attr) (0)
107
#endif
108
109
static COLD void get_num_threads(Dav1dContext *const c, const Dav1dSettings *const s,
110
                                 unsigned *n_tc, unsigned *n_fc)
111
76.9k
{
112
    /* ceil(sqrt(n)) */
113
76.9k
    static const uint8_t fc_lut[49] = {
114
76.9k
        1,                                     /*     1 */
115
76.9k
        2, 2, 2,                               /*  2- 4 */
116
76.9k
        3, 3, 3, 3, 3,                         /*  5- 9 */
117
76.9k
        4, 4, 4, 4, 4, 4, 4,                   /* 10-16 */
118
76.9k
        5, 5, 5, 5, 5, 5, 5, 5, 5,             /* 17-25 */
119
76.9k
        6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,       /* 26-36 */
120
76.9k
        7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, /* 37-49 */
121
76.9k
    };
122
76.9k
    *n_tc = s->n_threads ? s->n_threads :
123
76.9k
        iclip(dav1d_num_logical_processors(c), 1, DAV1D_MAX_THREADS);
124
76.9k
    *n_fc = s->max_frame_delay ? umin(s->max_frame_delay, *n_tc) :
125
76.9k
            *n_tc < 50 ? fc_lut[*n_tc - 1] : 8; // min(8, ceil(sqrt(n)))
126
76.9k
}
127
128
0
COLD int dav1d_get_frame_delay(const Dav1dSettings *const s) {
129
0
    unsigned n_tc, n_fc;
130
0
    validate_input_or_ret(s != NULL, DAV1D_ERR(EINVAL));
131
0
    validate_input_or_ret(s->n_threads >= 0 &&
132
0
                          s->n_threads <= DAV1D_MAX_THREADS, DAV1D_ERR(EINVAL));
133
0
    validate_input_or_ret(s->max_frame_delay >= 0 &&
134
0
                          s->max_frame_delay <= DAV1D_MAX_FRAME_DELAY, DAV1D_ERR(EINVAL));
135
136
0
    get_num_threads(NULL, s, &n_tc, &n_fc);
137
0
    return n_fc;
138
0
}
139
140
76.9k
COLD int dav1d_open(Dav1dContext **const c_out, const Dav1dSettings *const s) {
141
76.9k
    static pthread_once_t initted = PTHREAD_ONCE_INIT;
142
76.9k
    pthread_once(&initted, init_internal);
143
144
76.9k
    validate_input_or_ret(c_out != NULL, DAV1D_ERR(EINVAL));
145
76.9k
    validate_input_or_ret(s != NULL, DAV1D_ERR(EINVAL));
146
76.9k
    validate_input_or_ret(s->n_threads >= 0 &&
147
76.9k
                          s->n_threads <= DAV1D_MAX_THREADS, DAV1D_ERR(EINVAL));
148
76.9k
    validate_input_or_ret(s->max_frame_delay >= 0 &&
149
76.9k
                          s->max_frame_delay <= DAV1D_MAX_FRAME_DELAY, DAV1D_ERR(EINVAL));
150
76.9k
    validate_input_or_ret(s->allocator.alloc_picture_callback != NULL,
151
76.9k
                          DAV1D_ERR(EINVAL));
152
76.9k
    validate_input_or_ret(s->allocator.release_picture_callback != NULL,
153
76.9k
                          DAV1D_ERR(EINVAL));
154
76.9k
    validate_input_or_ret(s->operating_point >= 0 &&
155
76.9k
                          s->operating_point <= 31, DAV1D_ERR(EINVAL));
156
76.9k
    validate_input_or_ret(s->decode_frame_type >= DAV1D_DECODEFRAMETYPE_ALL &&
157
76.9k
                          s->decode_frame_type <= DAV1D_DECODEFRAMETYPE_KEY, DAV1D_ERR(EINVAL));
158
159
76.9k
    pthread_attr_t thread_attr;
160
76.9k
    if (pthread_attr_init(&thread_attr)) return DAV1D_ERR(ENOMEM);
161
76.9k
    size_t stack_size = 1024 * 1024 + get_stack_size_internal(&thread_attr);
162
163
76.9k
    pthread_attr_setstacksize(&thread_attr, stack_size);
164
165
76.9k
    Dav1dContext *const c = *c_out = dav1d_alloc_aligned(ALLOC_COMMON_CTX, sizeof(*c), 64);
166
76.9k
    if (!c) goto error;
167
76.9k
    memset(c, 0, sizeof(*c));
168
169
76.9k
    c->allocator = s->allocator;
170
76.9k
    c->logger = s->logger;
171
76.9k
    c->apply_grain = s->apply_grain;
172
76.9k
    c->operating_point = s->operating_point;
173
76.9k
    c->all_layers = s->all_layers;
174
76.9k
    c->frame_size_limit = s->frame_size_limit;
175
76.9k
    c->strict_std_compliance = s->strict_std_compliance;
176
76.9k
    c->output_invisible_frames = s->output_invisible_frames;
177
76.9k
    c->inloop_filters = s->inloop_filters;
178
76.9k
    c->decode_frame_type = s->decode_frame_type;
179
180
76.9k
    dav1d_data_props_set_defaults(&c->cached_error_props);
181
182
76.9k
    if (dav1d_mem_pool_init(ALLOC_OBU_HDR, &c->seq_hdr_pool) ||
183
76.9k
        dav1d_mem_pool_init(ALLOC_OBU_HDR, &c->frame_hdr_pool) ||
184
76.9k
        dav1d_mem_pool_init(ALLOC_SEGMAP, &c->segmap_pool) ||
185
76.9k
        dav1d_mem_pool_init(ALLOC_REFMVS, &c->refmvs_pool) ||
186
76.9k
        dav1d_mem_pool_init(ALLOC_PIC_CTX, &c->pic_ctx_pool) ||
187
76.9k
        dav1d_mem_pool_init(ALLOC_CDF, &c->cdf_pool))
188
0
    {
189
0
        goto error;
190
0
    }
191
192
76.9k
    if (c->allocator.alloc_picture_callback   == dav1d_default_picture_alloc &&
193
76.9k
        c->allocator.release_picture_callback == dav1d_default_picture_release)
194
76.9k
    {
195
76.9k
        if (c->allocator.cookie) goto error;
196
76.9k
        if (dav1d_mem_pool_init(ALLOC_PIC, &c->picture_pool)) goto error;
197
76.9k
        c->allocator.cookie = c->picture_pool;
198
76.9k
    } else if (c->allocator.alloc_picture_callback   == dav1d_default_picture_alloc ||
199
0
               c->allocator.release_picture_callback == dav1d_default_picture_release)
200
0
    {
201
0
        goto error;
202
0
    }
203
204
    /* On 32-bit systems extremely large frame sizes can cause overflows in
205
     * dav1d_decode_frame() malloc size calculations. Prevent that from occuring
206
     * by enforcing a maximum frame size limit, chosen to roughly correspond to
207
     * the largest size possible to decode without exhausting virtual memory. */
208
76.9k
    if (sizeof(size_t) < 8 && s->frame_size_limit - 1 >= 8192 * 8192) {
209
0
        c->frame_size_limit = 8192 * 8192;
210
0
        if (s->frame_size_limit)
211
0
            dav1d_log(c, "Frame size limit reduced from %u to %u.\n",
212
0
                      s->frame_size_limit, c->frame_size_limit);
213
0
    }
214
215
76.9k
    c->flush = &c->flush_mem;
216
76.9k
    atomic_init(c->flush, 0);
217
218
76.9k
    get_num_threads(c, s, &c->n_tc, &c->n_fc);
219
220
76.9k
    c->fc = dav1d_alloc_aligned(ALLOC_THREAD_CTX, sizeof(*c->fc) * c->n_fc, 32);
221
76.9k
    if (!c->fc) goto error;
222
76.9k
    memset(c->fc, 0, sizeof(*c->fc) * c->n_fc);
223
224
76.9k
    c->tc = dav1d_alloc_aligned(ALLOC_THREAD_CTX, sizeof(*c->tc) * c->n_tc, 64);
225
76.9k
    if (!c->tc) goto error;
226
76.9k
    memset(c->tc, 0, sizeof(*c->tc) * c->n_tc);
227
76.9k
    if (c->n_tc > 1) {
228
76.9k
        if (pthread_mutex_init(&c->task_thread.lock, NULL)) goto error;
229
76.9k
        if (pthread_cond_init(&c->task_thread.cond, NULL)) {
230
0
            pthread_mutex_destroy(&c->task_thread.lock);
231
0
            goto error;
232
0
        }
233
76.9k
        if (pthread_cond_init(&c->task_thread.delayed_fg.cond, NULL)) {
234
0
            pthread_cond_destroy(&c->task_thread.cond);
235
0
            pthread_mutex_destroy(&c->task_thread.lock);
236
0
            goto error;
237
0
        }
238
76.9k
        c->task_thread.cur = c->n_fc;
239
76.9k
        atomic_init(&c->task_thread.reset_task_cur, UINT_MAX);
240
76.9k
        atomic_init(&c->task_thread.cond_signaled, 0);
241
76.9k
        c->task_thread.inited = 1;
242
76.9k
    }
243
244
76.9k
    if (c->n_fc > 1) {
245
76.9k
        const size_t out_delayed_sz = sizeof(*c->frame_thread.out_delayed) * c->n_fc;
246
76.9k
        c->frame_thread.out_delayed =
247
76.9k
            dav1d_malloc(ALLOC_THREAD_CTX, out_delayed_sz);
248
76.9k
        if (!c->frame_thread.out_delayed) goto error;
249
76.9k
        memset(c->frame_thread.out_delayed, 0, out_delayed_sz);
250
76.9k
    }
251
538k
    for (unsigned n = 0; n < c->n_fc; n++) {
252
461k
        Dav1dFrameContext *const f = &c->fc[n];
253
461k
        if (c->n_tc > 1) {
254
461k
            if (pthread_mutex_init(&f->task_thread.lock, NULL)) goto error;
255
461k
            if (pthread_cond_init(&f->task_thread.cond, NULL)) {
256
0
                pthread_mutex_destroy(&f->task_thread.lock);
257
0
                goto error;
258
0
            }
259
461k
            if (pthread_mutex_init(&f->task_thread.pending_tasks.lock, NULL)) {
260
0
                pthread_cond_destroy(&f->task_thread.cond);
261
0
                pthread_mutex_destroy(&f->task_thread.lock);
262
0
                goto error;
263
0
            }
264
461k
        }
265
461k
        f->c = c;
266
461k
        f->task_thread.ttd = &c->task_thread;
267
461k
        f->lf.last_sharpness = -1;
268
461k
    }
269
270
2.53M
    for (unsigned m = 0; m < c->n_tc; m++) {
271
2.46M
        Dav1dTaskContext *const t = &c->tc[m];
272
2.46M
        t->f = &c->fc[0];
273
2.46M
        t->task_thread.ttd = &c->task_thread;
274
2.46M
        t->c = c;
275
2.46M
        memset(t->cf_16bpc, 0, sizeof(t->cf_16bpc));
276
2.46M
        if (c->n_tc > 1) {
277
2.46M
            if (pthread_mutex_init(&t->task_thread.td.lock, NULL)) goto error;
278
2.46M
            if (pthread_cond_init(&t->task_thread.td.cond, NULL)) {
279
0
                pthread_mutex_destroy(&t->task_thread.td.lock);
280
0
                goto error;
281
0
            }
282
2.46M
            if (pthread_create(&t->task_thread.td.thread, &thread_attr, dav1d_worker_task, t)) {
283
0
                pthread_cond_destroy(&t->task_thread.td.cond);
284
0
                pthread_mutex_destroy(&t->task_thread.td.lock);
285
0
                goto error;
286
0
            }
287
2.46M
            t->task_thread.td.inited = 1;
288
2.46M
        }
289
2.46M
    }
290
76.9k
    dav1d_pal_dsp_init(&c->pal_dsp);
291
76.9k
    dav1d_refmvs_dsp_init(&c->refmvs_dsp);
292
293
76.9k
    pthread_attr_destroy(&thread_attr);
294
295
76.9k
    return 0;
296
297
0
error:
298
0
    if (c) close_internal(c_out, 0);
299
0
    pthread_attr_destroy(&thread_attr);
300
0
    return DAV1D_ERR(ENOMEM);
301
76.9k
}
302
303
static int has_grain(const Dav1dPicture *const pic)
304
46.0k
{
305
46.0k
    const Dav1dFilmGrainData *fgdata = &pic->frame_hdr->film_grain.data;
306
46.0k
    return fgdata->num_y_points || fgdata->num_uv_points[0] ||
307
39.2k
           fgdata->num_uv_points[1] || (fgdata->clip_to_restricted_range &&
308
1.61k
                                        fgdata->chroma_scaling_from_luma);
309
46.0k
}
310
311
static int output_image(Dav1dContext *const c, Dav1dPicture *const out)
312
41.4k
{
313
41.4k
    int res = 0;
314
315
41.4k
    Dav1dThreadPicture *const in = (c->all_layers || !c->max_spatial_id)
316
41.4k
                                   ? &c->out : &c->cache;
317
41.4k
    if (!c->apply_grain || !has_grain(&in->p)) {
318
36.9k
        dav1d_picture_move_ref(out, &in->p);
319
36.9k
        dav1d_thread_picture_unref(in);
320
36.9k
        goto end;
321
36.9k
    }
322
323
4.57k
    res = dav1d_apply_grain(c, out, &in->p);
324
4.57k
    dav1d_thread_picture_unref(in);
325
41.4k
end:
326
41.4k
    if (!c->all_layers && c->max_spatial_id && c->out.p.data[0]) {
327
82
        dav1d_thread_picture_move_ref(in, &c->out);
328
82
    }
329
41.4k
    return res;
330
4.57k
}
331
332
1.10M
static int output_picture_ready(Dav1dContext *const c, const int drain) {
333
1.10M
    if (c->cached_error) return 1;
334
1.10M
    if (!c->all_layers && c->max_spatial_id) {
335
241k
        if (c->out.p.data[0] && c->cache.p.data[0]) {
336
50.2k
            if (c->max_spatial_id == c->cache.p.frame_hdr->spatial_id ||
337
50.2k
                c->out.flags & PICTURE_FLAG_NEW_TEMPORAL_UNIT)
338
102
                return 1;
339
50.1k
            dav1d_thread_picture_unref(&c->cache);
340
50.1k
            dav1d_thread_picture_move_ref(&c->cache, &c->out);
341
50.1k
            return 0;
342
191k
        } else if (c->cache.p.data[0] && drain) {
343
5.99k
            return 1;
344
185k
        } else if (c->out.p.data[0]) {
345
10.4k
            dav1d_thread_picture_move_ref(&c->cache, &c->out);
346
10.4k
            return 0;
347
10.4k
        }
348
241k
    }
349
350
1.04M
    return !!c->out.p.data[0];
351
1.10M
}
352
353
186k
static int drain_picture(Dav1dContext *const c, Dav1dPicture *const out) {
354
186k
    unsigned drain_count = 0;
355
186k
    int drained = 0;
356
1.10M
    do {
357
1.10M
        const unsigned next = c->frame_thread.next;
358
1.10M
        Dav1dFrameContext *const f = &c->fc[next];
359
1.10M
        pthread_mutex_lock(&c->task_thread.lock);
360
1.18M
        while (f->n_tile_data > 0)
361
75.6k
            pthread_cond_wait(&f->task_thread.cond,
362
75.6k
                              &f->task_thread.ttd->lock);
363
1.10M
        Dav1dThreadPicture *const out_delayed =
364
1.10M
            &c->frame_thread.out_delayed[next];
365
1.10M
        if (out_delayed->p.data[0] || atomic_load(&f->task_thread.error)) {
366
83.6k
            unsigned first = atomic_load(&c->task_thread.first);
367
83.6k
            if (first + 1U < c->n_fc)
368
83.6k
                atomic_fetch_add(&c->task_thread.first, 1U);
369
1.03k
            else
370
83.6k
                atomic_store(&c->task_thread.first, 0);
371
83.6k
            atomic_compare_exchange_strong(&c->task_thread.reset_task_cur,
372
83.6k
                                           &first, UINT_MAX);
373
83.6k
            if (c->task_thread.cur && c->task_thread.cur < c->n_fc)
374
4.69k
                c->task_thread.cur--;
375
83.6k
            drained = 1;
376
1.02M
        } else if (drained) {
377
0
            pthread_mutex_unlock(&c->task_thread.lock);
378
0
            break;
379
0
        }
380
1.10M
        if (++c->frame_thread.next == c->n_fc)
381
185k
            c->frame_thread.next = 0;
382
1.10M
        pthread_mutex_unlock(&c->task_thread.lock);
383
1.10M
        const int error = f->task_thread.retval;
384
1.10M
        if (error) {
385
21.5k
            f->task_thread.retval = 0;
386
21.5k
            dav1d_data_props_copy(&c->cached_error_props, &out_delayed->p.m);
387
21.5k
            dav1d_thread_picture_unref(out_delayed);
388
21.5k
            return error;
389
21.5k
        }
390
1.08M
        if (out_delayed->p.data[0]) {
391
62.1k
            const unsigned progress =
392
62.1k
                atomic_load_explicit(&out_delayed->progress[1],
393
62.1k
                                     memory_order_relaxed);
394
62.1k
            if ((out_delayed->visible || c->output_invisible_frames) &&
395
54.8k
                progress != FRAME_ERROR)
396
54.8k
            {
397
54.8k
                dav1d_thread_picture_ref(&c->out, out_delayed);
398
54.8k
                c->event_flags |= dav1d_picture_get_event_flags(out_delayed);
399
54.8k
            }
400
62.1k
            dav1d_thread_picture_unref(out_delayed);
401
62.1k
            if (output_picture_ready(c, 0))
402
35.4k
                return output_image(c, out);
403
62.1k
        }
404
1.08M
    } while (++drain_count < c->n_fc);
405
406
129k
    if (output_picture_ready(c, 1))
407
5.99k
        return output_image(c, out);
408
409
123k
    return DAV1D_ERR(EAGAIN);
410
129k
}
411
412
static int gen_picture(Dav1dContext *const c)
413
390k
{
414
390k
    Dav1dData *const in = &c->in;
415
416
390k
    if (output_picture_ready(c, 0))
417
225
        return 0;
418
419
657k
    while (in->sz > 0) {
420
274k
        const ptrdiff_t res = dav1d_parse_obus(c, in);
421
274k
        if (res < 0) {
422
7.49k
            dav1d_data_unref_internal(in);
423
267k
        } else {
424
267k
            assert((size_t)res <= in->sz);
425
267k
            in->sz -= res;
426
267k
            in->data += res;
427
267k
            if (!in->sz) dav1d_data_unref_internal(in);
428
267k
        }
429
274k
        if (output_picture_ready(c, 0))
430
217
            break;
431
274k
        if (res < 0)
432
7.49k
            return (int)res;
433
274k
    }
434
435
382k
    return 0;
436
390k
}
437
438
int dav1d_send_data(Dav1dContext *const c, Dav1dData *const in)
439
139k
{
440
139k
    validate_input_or_ret(c != NULL, DAV1D_ERR(EINVAL));
441
139k
    validate_input_or_ret(in != NULL, DAV1D_ERR(EINVAL));
442
443
139k
    if (in->data) {
444
73.2k
        validate_input_or_ret(in->sz > 0 && in->sz <= SIZE_MAX / 2, DAV1D_ERR(EINVAL));
445
73.2k
        c->drain = 0;
446
73.2k
    }
447
139k
    if (c->in.data)
448
418
        return DAV1D_ERR(EAGAIN);
449
138k
    dav1d_data_ref(&c->in, in);
450
451
138k
    int res = gen_picture(c);
452
138k
    if (!res)
453
131k
        dav1d_data_unref_internal(in);
454
455
138k
    return res;
456
139k
}
457
458
int dav1d_get_picture(Dav1dContext *const c, Dav1dPicture *const out)
459
251k
{
460
251k
    validate_input_or_ret(c != NULL, DAV1D_ERR(EINVAL));
461
251k
    validate_input_or_ret(out != NULL, DAV1D_ERR(EINVAL));
462
463
251k
    const int drain = c->drain;
464
251k
    c->drain = 1;
465
466
251k
    int res = gen_picture(c);
467
251k
    if (res < 0)
468
0
        return res;
469
470
251k
    if (c->cached_error) {
471
184
        const int res = c->cached_error;
472
184
        c->cached_error = 0;
473
184
        return res;
474
184
    }
475
476
251k
    if (output_picture_ready(c, c->n_fc == 1))
477
33
        return output_image(c, out);
478
479
251k
    if (c->n_fc > 1 && drain)
480
186k
        return drain_picture(c, out);
481
482
65.4k
    return DAV1D_ERR(EAGAIN);
483
251k
}
484
485
int dav1d_apply_grain(Dav1dContext *const c, Dav1dPicture *const out,
486
                      const Dav1dPicture *const in)
487
4.57k
{
488
4.57k
    validate_input_or_ret(c != NULL, DAV1D_ERR(EINVAL));
489
4.57k
    validate_input_or_ret(out != NULL, DAV1D_ERR(EINVAL));
490
4.57k
    validate_input_or_ret(in != NULL, DAV1D_ERR(EINVAL));
491
492
4.57k
    if (!has_grain(in)) {
493
0
        dav1d_picture_ref(out, in);
494
0
        return 0;
495
0
    }
496
497
4.57k
    int res = dav1d_picture_alloc_copy(c, out, in->p.w, in);
498
4.57k
    if (res < 0) goto error;
499
500
4.57k
    if (c->n_tc > 1) {
501
4.57k
        dav1d_task_delayed_fg(c, out, in);
502
4.57k
    } else {
503
0
        switch (out->p.bpc) {
504
0
#if CONFIG_8BPC
505
0
        case 8:
506
0
            dav1d_apply_grain_8bpc(&c->dsp[0].fg, out, in);
507
0
            break;
508
0
#endif
509
0
#if CONFIG_16BPC
510
0
        case 10:
511
0
        case 12:
512
0
            dav1d_apply_grain_16bpc(&c->dsp[(out->p.bpc >> 1) - 4].fg, out, in);
513
0
            break;
514
0
#endif
515
0
        default: abort();
516
0
        }
517
0
    }
518
519
4.57k
    return 0;
520
521
0
error:
522
0
    dav1d_picture_unref_internal(out);
523
0
    return res;
524
4.57k
}
525
526
76.9k
void dav1d_flush(Dav1dContext *const c) {
527
76.9k
    dav1d_data_unref_internal(&c->in);
528
76.9k
    if (c->out.p.frame_hdr)
529
0
        dav1d_thread_picture_unref(&c->out);
530
76.9k
    if (c->cache.p.frame_hdr)
531
4.45k
        dav1d_thread_picture_unref(&c->cache);
532
533
76.9k
    c->drain = 0;
534
76.9k
    c->cached_error = 0;
535
536
692k
    for (int i = 0; i < 8; i++) {
537
615k
        if (c->refs[i].p.p.frame_hdr)
538
538k
            dav1d_thread_picture_unref(&c->refs[i].p);
539
615k
        dav1d_ref_dec(&c->refs[i].segmap);
540
615k
        dav1d_ref_dec(&c->refs[i].refmvs);
541
615k
        dav1d_cdf_thread_unref(&c->cdf[i]);
542
615k
    }
543
76.9k
    c->frame_hdr = NULL;
544
76.9k
    c->seq_hdr = NULL;
545
76.9k
    dav1d_ref_dec(&c->seq_hdr_ref);
546
547
76.9k
    c->mastering_display = NULL;
548
76.9k
    c->content_light = NULL;
549
76.9k
    c->itut_t35 = NULL;
550
76.9k
    c->n_itut_t35 = 0;
551
76.9k
    dav1d_ref_dec(&c->mastering_display_ref);
552
76.9k
    dav1d_ref_dec(&c->content_light_ref);
553
76.9k
    dav1d_ref_dec(&c->itut_t35_ref);
554
555
76.9k
    dav1d_data_props_unref_internal(&c->cached_error_props);
556
557
76.9k
    if (c->n_fc == 1 && c->n_tc == 1) return;
558
76.9k
    atomic_store(c->flush, 1);
559
560
76.9k
    if (c->n_tc > 1) {
561
76.9k
        pthread_mutex_lock(&c->task_thread.lock);
562
        // stop running tasks in worker threads
563
2.53M
        for (unsigned i = 0; i < c->n_tc; i++) {
564
2.46M
            Dav1dTaskContext *const tc = &c->tc[i];
565
2.47M
            while (!tc->task_thread.flushed) {
566
12.6k
                pthread_cond_wait(&tc->task_thread.td.cond, &c->task_thread.lock);
567
12.6k
            }
568
2.46M
        }
569
538k
        for (unsigned i = 0; i < c->n_fc; i++) {
570
461k
            c->fc[i].task_thread.task_head = NULL;
571
461k
            c->fc[i].task_thread.task_tail = NULL;
572
461k
            c->fc[i].task_thread.task_cur_prev = NULL;
573
461k
            c->fc[i].task_thread.pending_tasks.head = NULL;
574
461k
            c->fc[i].task_thread.pending_tasks.tail = NULL;
575
461k
            atomic_init(&c->fc[i].task_thread.pending_tasks.merge, 0);
576
461k
        }
577
76.9k
        atomic_init(&c->task_thread.first, 0);
578
76.9k
        c->task_thread.cur = c->n_fc;
579
76.9k
        atomic_store(&c->task_thread.reset_task_cur, UINT_MAX);
580
76.9k
        atomic_store(&c->task_thread.cond_signaled, 0);
581
76.9k
        pthread_mutex_unlock(&c->task_thread.lock);
582
76.9k
    }
583
584
76.9k
    if (c->n_fc > 1) {
585
538k
        for (unsigned n = 0, next = c->frame_thread.next; n < c->n_fc; n++, next++) {
586
461k
            if (next == c->n_fc) next = 0;
587
461k
            Dav1dFrameContext *const f = &c->fc[next];
588
461k
            dav1d_decode_frame_exit(f, -1);
589
461k
            f->n_tile_data = 0;
590
461k
            f->task_thread.retval = 0;
591
461k
            f->task_thread.error = 0;
592
461k
            Dav1dThreadPicture *out_delayed = &c->frame_thread.out_delayed[next];
593
461k
            if (out_delayed->p.frame_hdr) {
594
23.0k
                dav1d_thread_picture_unref(out_delayed);
595
23.0k
            }
596
461k
        }
597
76.9k
        c->frame_thread.next = 0;
598
76.9k
    }
599
76.9k
    atomic_store(c->flush, 0);
600
76.9k
}
601
602
76.9k
COLD void dav1d_close(Dav1dContext **const c_out) {
603
76.9k
    validate_input(c_out != NULL);
604
#if TRACK_HEAP_ALLOCATIONS
605
    dav1d_log_alloc_stats(*c_out);
606
#endif
607
76.9k
    close_internal(c_out, 1);
608
76.9k
}
609
610
76.9k
static COLD void close_internal(Dav1dContext **const c_out, int flush) {
611
76.9k
    Dav1dContext *const c = *c_out;
612
76.9k
    if (!c) return;
613
614
76.9k
    if (flush) dav1d_flush(c);
615
616
76.9k
    if (c->tc) {
617
76.9k
        struct TaskThreadData *ttd = &c->task_thread;
618
76.9k
        if (ttd->inited) {
619
76.9k
            pthread_mutex_lock(&ttd->lock);
620
2.53M
            for (unsigned n = 0; n < c->n_tc && c->tc[n].task_thread.td.inited; n++)
621
2.46M
                c->tc[n].task_thread.die = 1;
622
76.9k
            pthread_cond_broadcast(&ttd->cond);
623
76.9k
            pthread_mutex_unlock(&ttd->lock);
624
2.53M
            for (unsigned n = 0; n < c->n_tc; n++) {
625
2.46M
                Dav1dTaskContext *const pf = &c->tc[n];
626
2.46M
                if (!pf->task_thread.td.inited) break;
627
2.46M
                pthread_join(pf->task_thread.td.thread, NULL);
628
2.46M
                pthread_cond_destroy(&pf->task_thread.td.cond);
629
2.46M
                pthread_mutex_destroy(&pf->task_thread.td.lock);
630
2.46M
            }
631
76.9k
            pthread_cond_destroy(&ttd->delayed_fg.cond);
632
76.9k
            pthread_cond_destroy(&ttd->cond);
633
76.9k
            pthread_mutex_destroy(&ttd->lock);
634
76.9k
        }
635
76.9k
        dav1d_free_aligned(c->tc);
636
76.9k
    }
637
638
538k
    for (unsigned n = 0; c->fc && n < c->n_fc; n++) {
639
461k
        Dav1dFrameContext *const f = &c->fc[n];
640
641
        // clean-up threading stuff
642
461k
        if (c->n_fc > 1) {
643
461k
            dav1d_free(f->tile_thread.lowest_pixel_mem);
644
461k
            dav1d_free(f->frame_thread.b);
645
461k
            dav1d_free_aligned(f->frame_thread.cbi);
646
461k
            dav1d_free_aligned(f->frame_thread.pal_idx);
647
461k
            dav1d_free_aligned(f->frame_thread.cf);
648
461k
            dav1d_free(f->frame_thread.tile_start_off);
649
461k
            dav1d_free_aligned(f->frame_thread.pal);
650
461k
        }
651
461k
        if (c->n_tc > 1) {
652
461k
            pthread_mutex_destroy(&f->task_thread.pending_tasks.lock);
653
461k
            pthread_cond_destroy(&f->task_thread.cond);
654
461k
            pthread_mutex_destroy(&f->task_thread.lock);
655
461k
        }
656
461k
        dav1d_free(f->frame_thread.frame_progress);
657
461k
        dav1d_free(f->task_thread.tasks);
658
461k
        dav1d_free(f->task_thread.tile_tasks[0]);
659
461k
        dav1d_free_aligned(f->ts);
660
461k
        dav1d_free_aligned(f->ipred_edge[0]);
661
461k
        dav1d_free(f->a);
662
461k
        dav1d_free(f->tile);
663
461k
        dav1d_free(f->lf.mask);
664
461k
        dav1d_free(f->lf.level);
665
461k
        dav1d_free(f->lf.lr_mask);
666
461k
        dav1d_free(f->lf.tx_lpf_right_edge[0]);
667
461k
        dav1d_free(f->lf.start_of_tile_row);
668
461k
        dav1d_free_aligned(f->rf.r);
669
461k
        dav1d_free_aligned(f->lf.cdef_line_buf);
670
461k
        dav1d_free_aligned(f->lf.lr_line_buf);
671
461k
    }
672
76.9k
    dav1d_free_aligned(c->fc);
673
76.9k
    if (c->n_fc > 1 && c->frame_thread.out_delayed) {
674
538k
        for (unsigned n = 0; n < c->n_fc; n++)
675
461k
            if (c->frame_thread.out_delayed[n].p.frame_hdr)
676
0
                dav1d_thread_picture_unref(&c->frame_thread.out_delayed[n]);
677
76.9k
        dav1d_free(c->frame_thread.out_delayed);
678
76.9k
    }
679
77.2k
    for (int n = 0; n < c->n_tile_data; n++)
680
293
        dav1d_data_unref_internal(&c->tile[n].data);
681
76.9k
    dav1d_free(c->tile);
682
692k
    for (int n = 0; n < 8; n++) {
683
615k
        dav1d_cdf_thread_unref(&c->cdf[n]);
684
615k
        if (c->refs[n].p.p.frame_hdr)
685
0
            dav1d_thread_picture_unref(&c->refs[n].p);
686
615k
        dav1d_ref_dec(&c->refs[n].refmvs);
687
615k
        dav1d_ref_dec(&c->refs[n].segmap);
688
615k
    }
689
76.9k
    dav1d_ref_dec(&c->seq_hdr_ref);
690
76.9k
    dav1d_ref_dec(&c->frame_hdr_ref);
691
692
76.9k
    dav1d_ref_dec(&c->mastering_display_ref);
693
76.9k
    dav1d_ref_dec(&c->content_light_ref);
694
76.9k
    dav1d_ref_dec(&c->itut_t35_ref);
695
696
76.9k
    dav1d_mem_pool_end(c->seq_hdr_pool);
697
76.9k
    dav1d_mem_pool_end(c->frame_hdr_pool);
698
76.9k
    dav1d_mem_pool_end(c->segmap_pool);
699
76.9k
    dav1d_mem_pool_end(c->refmvs_pool);
700
76.9k
    dav1d_mem_pool_end(c->cdf_pool);
701
76.9k
    dav1d_mem_pool_end(c->picture_pool);
702
76.9k
    dav1d_mem_pool_end(c->pic_ctx_pool);
703
704
76.9k
    dav1d_freep_aligned(c_out);
705
76.9k
}
706
707
0
int dav1d_get_event_flags(Dav1dContext *const c, enum Dav1dEventFlags *const flags) {
708
0
    validate_input_or_ret(c != NULL, DAV1D_ERR(EINVAL));
709
0
    validate_input_or_ret(flags != NULL, DAV1D_ERR(EINVAL));
710
711
0
    *flags = c->event_flags;
712
0
    c->event_flags = 0;
713
0
    return 0;
714
0
}
715
716
0
int dav1d_get_decode_error_data_props(Dav1dContext *const c, Dav1dDataProps *const out) {
717
0
    validate_input_or_ret(c != NULL, DAV1D_ERR(EINVAL));
718
0
    validate_input_or_ret(out != NULL, DAV1D_ERR(EINVAL));
719
720
0
    dav1d_data_props_unref_internal(out);
721
0
    *out = c->cached_error_props;
722
0
    dav1d_data_props_set_defaults(&c->cached_error_props);
723
724
0
    return 0;
725
0
}
726
727
41.4k
void dav1d_picture_unref(Dav1dPicture *const p) {
728
41.4k
    dav1d_picture_unref_internal(p);
729
41.4k
}
730
731
73.2k
uint8_t *dav1d_data_create(Dav1dData *const buf, const size_t sz) {
732
73.2k
    return dav1d_data_create_internal(buf, sz);
733
73.2k
}
734
735
int dav1d_data_wrap(Dav1dData *const buf, const uint8_t *const ptr,
736
                    const size_t sz,
737
                    void (*const free_callback)(const uint8_t *data,
738
                                                void *user_data),
739
                    void *const user_data)
740
0
{
741
0
    return dav1d_data_wrap_internal(buf, ptr, sz, free_callback, user_data);
742
0
}
743
744
int dav1d_data_wrap_user_data(Dav1dData *const buf,
745
                              const uint8_t *const user_data,
746
                              void (*const free_callback)(const uint8_t *user_data,
747
                                                          void *cookie),
748
                              void *const cookie)
749
0
{
750
0
    return dav1d_data_wrap_user_data_internal(buf,
751
0
                                              user_data,
752
0
                                              free_callback,
753
0
                                              cookie);
754
0
}
755
756
7.70k
void dav1d_data_unref(Dav1dData *const buf) {
757
7.70k
    dav1d_data_unref_internal(buf);
758
7.70k
}
759
760
0
void dav1d_data_props_unref(Dav1dDataProps *const props) {
761
0
    dav1d_data_props_unref_internal(props);
762
0
}