Coverage Report

Created: 2026-01-10 06:33

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