Coverage Report

Created: 2026-05-30 06:08

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
1
static COLD void init_internal(void) {
54
1
    dav1d_init_cpu();
55
1
    dav1d_init_ii_wedge_masks();
56
1
    dav1d_init_intra_edge_tree();
57
1
    dav1d_init_qm_tables();
58
1
    dav1d_init_thread();
59
1
}
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
39.4k
COLD void dav1d_default_settings(Dav1dSettings *const s) {
72
39.4k
    s->n_threads = 0;
73
39.4k
    s->max_frame_delay = 0;
74
39.4k
    s->apply_grain = 1;
75
39.4k
    s->allocator.cookie = NULL;
76
39.4k
    s->allocator.alloc_picture_callback = dav1d_default_picture_alloc;
77
39.4k
    s->allocator.release_picture_callback = dav1d_default_picture_release;
78
39.4k
    s->logger.cookie = NULL;
79
39.4k
    s->logger.callback = dav1d_log_default_callback;
80
39.4k
    s->operating_point = 0;
81
39.4k
    s->all_layers = 1; // just until the tests are adjusted
82
39.4k
    s->frame_size_limit = 0;
83
39.4k
    s->strict_std_compliance = 0;
84
39.4k
    s->output_invisible_frames = 0;
85
39.4k
    s->inloop_filters = DAV1D_INLOOPFILTER_ALL;
86
39.4k
    s->decode_frame_type = DAV1D_DECODEFRAMETYPE_ALL;
87
39.4k
}
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
39.4k
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
39.4k
    size_t (*const get_minstack)(const pthread_attr_t*) =
100
39.4k
        dlsym(RTLD_DEFAULT, "__pthread_get_minstack");
101
39.4k
    if (get_minstack)
102
39.4k
        return get_minstack(thread_attr) - PTHREAD_STACK_MIN;
103
0
    return 0;
104
39.4k
}
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
39.4k
{
112
    /* ceil(sqrt(n)) */
113
39.4k
    static const uint8_t fc_lut[49] = {
114
39.4k
        1,                                     /*     1 */
115
39.4k
        2, 2, 2,                               /*  2- 4 */
116
39.4k
        3, 3, 3, 3, 3,                         /*  5- 9 */
117
39.4k
        4, 4, 4, 4, 4, 4, 4,                   /* 10-16 */
118
39.4k
        5, 5, 5, 5, 5, 5, 5, 5, 5,             /* 17-25 */
119
39.4k
        6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6,       /* 26-36 */
120
39.4k
        7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, /* 37-49 */
121
39.4k
    };
122
39.4k
    *n_tc = s->n_threads ? s->n_threads :
123
39.4k
        iclip(dav1d_num_logical_processors(c), 1, DAV1D_MAX_THREADS);
124
39.4k
    *n_fc = s->max_frame_delay ? umin(s->max_frame_delay, *n_tc) :
125
39.4k
            *n_tc < 50 ? fc_lut[*n_tc - 1] : 8; // min(8, ceil(sqrt(n)))
126
39.4k
}
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
39.4k
COLD int dav1d_open(Dav1dContext **const c_out, const Dav1dSettings *const s) {
141
39.4k
    static pthread_once_t initted = PTHREAD_ONCE_INIT;
142
39.4k
    pthread_once(&initted, init_internal);
143
144
39.4k
    validate_input_or_ret(c_out != NULL, DAV1D_ERR(EINVAL));
145
39.4k
    validate_input_or_ret(s != NULL, DAV1D_ERR(EINVAL));
146
39.4k
    validate_input_or_ret(s->n_threads >= 0 &&
147
39.4k
                          s->n_threads <= DAV1D_MAX_THREADS, DAV1D_ERR(EINVAL));
148
39.4k
    validate_input_or_ret(s->max_frame_delay >= 0 &&
149
39.4k
                          s->max_frame_delay <= DAV1D_MAX_FRAME_DELAY, DAV1D_ERR(EINVAL));
150
39.4k
    validate_input_or_ret(s->allocator.alloc_picture_callback != NULL,
151
39.4k
                          DAV1D_ERR(EINVAL));
152
39.4k
    validate_input_or_ret(s->allocator.release_picture_callback != NULL,
153
39.4k
                          DAV1D_ERR(EINVAL));
154
39.4k
    validate_input_or_ret(s->operating_point >= 0 &&
155
39.4k
                          s->operating_point <= 31, DAV1D_ERR(EINVAL));
156
39.4k
    validate_input_or_ret(s->decode_frame_type >= DAV1D_DECODEFRAMETYPE_ALL &&
157
39.4k
                          s->decode_frame_type <= DAV1D_DECODEFRAMETYPE_KEY, DAV1D_ERR(EINVAL));
158
159
39.4k
    pthread_attr_t thread_attr;
160
39.4k
    if (pthread_attr_init(&thread_attr)) return DAV1D_ERR(ENOMEM);
161
39.4k
    size_t stack_size = 1024 * 1024 + get_stack_size_internal(&thread_attr);
162
163
39.4k
    pthread_attr_setstacksize(&thread_attr, stack_size);
164
165
39.4k
    Dav1dContext *const c = *c_out = dav1d_alloc_aligned(ALLOC_COMMON_CTX, sizeof(*c), 64);
166
39.4k
    if (!c) goto error;
167
39.4k
    memset(c, 0, sizeof(*c));
168
169
39.4k
    c->allocator = s->allocator;
170
39.4k
    c->logger = s->logger;
171
39.4k
    c->apply_grain = s->apply_grain;
172
39.4k
    c->operating_point = s->operating_point;
173
39.4k
    c->all_layers = s->all_layers;
174
39.4k
    c->frame_size_limit = s->frame_size_limit;
175
39.4k
    c->strict_std_compliance = s->strict_std_compliance;
176
39.4k
    c->output_invisible_frames = s->output_invisible_frames;
177
39.4k
    c->inloop_filters = s->inloop_filters;
178
39.4k
    c->decode_frame_type = s->decode_frame_type;
179
180
39.4k
    dav1d_data_props_set_defaults(&c->cached_error_props);
181
182
39.4k
    if (dav1d_mem_pool_init(ALLOC_OBU_HDR, &c->seq_hdr_pool) ||
183
39.4k
        dav1d_mem_pool_init(ALLOC_OBU_HDR, &c->frame_hdr_pool) ||
184
39.4k
        dav1d_mem_pool_init(ALLOC_SEGMAP, &c->segmap_pool) ||
185
39.4k
        dav1d_mem_pool_init(ALLOC_REFMVS, &c->refmvs_pool) ||
186
39.4k
        dav1d_mem_pool_init(ALLOC_PIC_CTX, &c->pic_ctx_pool) ||
187
39.4k
        dav1d_mem_pool_init(ALLOC_CDF, &c->cdf_pool))
188
0
    {
189
0
        goto error;
190
0
    }
191
192
39.4k
    if (c->allocator.alloc_picture_callback   == dav1d_default_picture_alloc &&
193
39.4k
        c->allocator.release_picture_callback == dav1d_default_picture_release)
194
39.4k
    {
195
39.4k
        if (c->allocator.cookie) goto error;
196
39.4k
        if (dav1d_mem_pool_init(ALLOC_PIC, &c->picture_pool)) goto error;
197
39.4k
        c->allocator.cookie = c->picture_pool;
198
39.4k
    } 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
39.4k
    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
39.4k
    c->flush = &c->flush_mem;
216
39.4k
    atomic_init(c->flush, 0);
217
218
39.4k
    get_num_threads(c, s, &c->n_tc, &c->n_fc);
219
220
39.4k
    c->fc = dav1d_alloc_aligned(ALLOC_THREAD_CTX, sizeof(*c->fc) * c->n_fc, 32);
221
39.4k
    if (!c->fc) goto error;
222
39.4k
    memset(c->fc, 0, sizeof(*c->fc) * c->n_fc);
223
224
39.4k
    c->tc = dav1d_alloc_aligned(ALLOC_THREAD_CTX, sizeof(*c->tc) * c->n_tc, 64);
225
39.4k
    if (!c->tc) goto error;
226
39.4k
    memset(c->tc, 0, sizeof(*c->tc) * c->n_tc);
227
39.4k
    if (c->n_tc > 1) {
228
39.4k
        if (pthread_mutex_init(&c->task_thread.lock, NULL)) goto error;
229
39.4k
        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
39.4k
        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
39.4k
        c->task_thread.cur = c->n_fc;
239
39.4k
        atomic_init(&c->task_thread.reset_task_cur, UINT_MAX);
240
39.4k
        atomic_init(&c->task_thread.cond_signaled, 0);
241
39.4k
        c->task_thread.inited = 1;
242
39.4k
    }
243
244
39.4k
    if (c->n_fc > 1) {
245
39.4k
        const size_t out_delayed_sz = sizeof(*c->frame_thread.out_delayed) * c->n_fc;
246
39.4k
        c->frame_thread.out_delayed =
247
39.4k
            dav1d_malloc(ALLOC_THREAD_CTX, out_delayed_sz);
248
39.4k
        if (!c->frame_thread.out_delayed) goto error;
249
39.4k
        memset(c->frame_thread.out_delayed, 0, out_delayed_sz);
250
39.4k
    }
251
276k
    for (unsigned n = 0; n < c->n_fc; n++) {
252
236k
        Dav1dFrameContext *const f = &c->fc[n];
253
236k
        if (c->n_tc > 1) {
254
236k
            if (pthread_mutex_init(&f->task_thread.lock, NULL)) goto error;
255
236k
            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
236k
            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
236k
        }
265
236k
        f->c = c;
266
236k
        f->task_thread.ttd = &c->task_thread;
267
236k
        f->lf.last_sharpness = -1;
268
236k
    }
269
270
1.30M
    for (unsigned m = 0; m < c->n_tc; m++) {
271
1.26M
        Dav1dTaskContext *const t = &c->tc[m];
272
1.26M
        t->f = &c->fc[0];
273
1.26M
        t->task_thread.ttd = &c->task_thread;
274
1.26M
        t->c = c;
275
1.26M
        memset(t->cf_16bpc, 0, sizeof(t->cf_16bpc));
276
1.26M
        if (c->n_tc > 1) {
277
1.26M
            if (pthread_mutex_init(&t->task_thread.td.lock, NULL)) goto error;
278
1.26M
            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
1.26M
            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
1.26M
            t->task_thread.td.inited = 1;
288
1.26M
        }
289
1.26M
    }
290
39.4k
    dav1d_pal_dsp_init(&c->pal_dsp);
291
39.4k
    dav1d_refmvs_dsp_init(&c->refmvs_dsp);
292
293
39.4k
    pthread_attr_destroy(&thread_attr);
294
295
39.4k
    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
39.4k
}
302
303
static int has_grain(const Dav1dPicture *const pic)
304
26.4k
{
305
26.4k
    const Dav1dFilmGrainData *fgdata = &pic->frame_hdr->film_grain.data;
306
26.4k
    return fgdata->num_y_points || fgdata->num_uv_points[0] ||
307
22.4k
           fgdata->num_uv_points[1] || (fgdata->clip_to_restricted_range &&
308
915
                                        fgdata->chroma_scaling_from_luma);
309
26.4k
}
310
311
static int output_image(Dav1dContext *const c, Dav1dPicture *const out)
312
23.7k
{
313
23.7k
    int res = 0;
314
315
23.7k
    Dav1dThreadPicture *const in = (c->all_layers || !c->max_spatial_id)
316
23.7k
                                   ? &c->out : &c->cache;
317
23.7k
    if (!c->apply_grain || !has_grain(&in->p)) {
318
20.9k
        dav1d_picture_move_ref(out, &in->p);
319
20.9k
        dav1d_thread_picture_unref(in);
320
20.9k
        goto end;
321
20.9k
    }
322
323
2.73k
    res = dav1d_apply_grain(c, out, &in->p);
324
2.73k
    dav1d_thread_picture_unref(in);
325
23.7k
end:
326
23.7k
    if (!c->all_layers && c->max_spatial_id && c->out.p.data[0]) {
327
99
        dav1d_thread_picture_move_ref(in, &c->out);
328
99
    }
329
23.7k
    return res;
330
2.73k
}
331
332
422k
static int output_picture_ready(Dav1dContext *const c, const int drain) {
333
422k
    if (c->cached_error) return 1;
334
422k
    if (!c->all_layers && c->max_spatial_id) {
335
103k
        if (c->out.p.data[0] && c->cache.p.data[0]) {
336
3.40k
            if (c->max_spatial_id == c->cache.p.frame_hdr->spatial_id ||
337
3.40k
                c->out.flags & PICTURE_FLAG_NEW_TEMPORAL_UNIT)
338
99
                return 1;
339
3.30k
            dav1d_thread_picture_unref(&c->cache);
340
3.30k
            dav1d_thread_picture_move_ref(&c->cache, &c->out);
341
3.30k
            return 0;
342
99.7k
        } else if (c->cache.p.data[0] && drain) {
343
3.42k
            return 1;
344
96.3k
        } else if (c->out.p.data[0]) {
345
6.30k
            dav1d_thread_picture_move_ref(&c->cache, &c->out);
346
6.30k
            return 0;
347
6.30k
        }
348
103k
    }
349
350
409k
    return !!c->out.p.data[0];
351
422k
}
352
353
52.2k
static int drain_picture(Dav1dContext *const c, Dav1dPicture *const out) {
354
52.2k
    unsigned drain_count = 0;
355
52.2k
    int drained = 0;
356
305k
    do {
357
305k
        const unsigned next = c->frame_thread.next;
358
305k
        Dav1dFrameContext *const f = &c->fc[next];
359
305k
        pthread_mutex_lock(&c->task_thread.lock);
360
346k
        while (f->n_tile_data > 0)
361
41.7k
            pthread_cond_wait(&f->task_thread.cond,
362
41.7k
                              &f->task_thread.ttd->lock);
363
305k
        Dav1dThreadPicture *const out_delayed =
364
305k
            &c->frame_thread.out_delayed[next];
365
305k
        if (out_delayed->p.data[0] || atomic_load(&f->task_thread.error)) {
366
44.6k
            unsigned first = atomic_load(&c->task_thread.first);
367
44.6k
            if (first + 1U < c->n_fc)
368
44.6k
                atomic_fetch_add(&c->task_thread.first, 1U);
369
63
            else
370
44.6k
                atomic_store(&c->task_thread.first, 0);
371
44.6k
            atomic_compare_exchange_strong(&c->task_thread.reset_task_cur,
372
44.6k
                                           &first, UINT_MAX);
373
44.6k
            if (c->task_thread.cur && c->task_thread.cur < c->n_fc)
374
2.31k
                c->task_thread.cur--;
375
44.6k
            drained = 1;
376
260k
        } else if (drained) {
377
0
            pthread_mutex_unlock(&c->task_thread.lock);
378
0
            break;
379
0
        }
380
305k
        if (++c->frame_thread.next == c->n_fc)
381
52.2k
            c->frame_thread.next = 0;
382
305k
        pthread_mutex_unlock(&c->task_thread.lock);
383
305k
        const int error = f->task_thread.retval;
384
305k
        if (error) {
385
13.0k
            f->task_thread.retval = 0;
386
13.0k
            dav1d_data_props_copy(&c->cached_error_props, &out_delayed->p.m);
387
13.0k
            dav1d_thread_picture_unref(out_delayed);
388
13.0k
            return error;
389
13.0k
        }
390
291k
        if (out_delayed->p.data[0]) {
391
31.6k
            const unsigned progress =
392
31.6k
                atomic_load_explicit(&out_delayed->progress[1],
393
31.6k
                                     memory_order_relaxed);
394
31.6k
            if ((out_delayed->visible || c->output_invisible_frames) &&
395
29.9k
                progress != FRAME_ERROR)
396
29.9k
            {
397
29.9k
                dav1d_thread_picture_ref(&c->out, out_delayed);
398
29.9k
                c->event_flags |= dav1d_picture_get_event_flags(out_delayed);
399
29.9k
            }
400
31.6k
            dav1d_thread_picture_unref(out_delayed);
401
31.6k
            if (output_picture_ready(c, 0))
402
20.2k
                return output_image(c, out);
403
31.6k
        }
404
291k
    } while (++drain_count < c->n_fc);
405
406
18.8k
    if (output_picture_ready(c, 1))
407
3.42k
        return output_image(c, out);
408
409
15.4k
    return DAV1D_ERR(EAGAIN);
410
18.8k
}
411
412
static int gen_picture(Dav1dContext *const c)
413
165k
{
414
165k
    Dav1dData *const in = &c->in;
415
416
165k
    if (output_picture_ready(c, 0))
417
0
        return 0;
418
419
280k
    while (in->sz > 0) {
420
117k
        const ptrdiff_t res = dav1d_parse_obus(c, in);
421
117k
        if (res < 0) {
422
2.33k
            dav1d_data_unref_internal(in);
423
114k
        } else {
424
114k
            assert((size_t)res <= in->sz);
425
114k
            in->sz -= res;
426
114k
            in->data += res;
427
114k
            if (!in->sz) dav1d_data_unref_internal(in);
428
114k
        }
429
117k
        if (output_picture_ready(c, 0))
430
0
            break;
431
117k
        if (res < 0)
432
2.33k
            return (int)res;
433
117k
    }
434
435
163k
    return 0;
436
165k
}
437
438
int dav1d_send_data(Dav1dContext *const c, Dav1dData *const in)
439
76.5k
{
440
76.5k
    validate_input_or_ret(c != NULL, DAV1D_ERR(EINVAL));
441
76.5k
    validate_input_or_ret(in != NULL, DAV1D_ERR(EINVAL));
442
443
76.5k
    if (in->data) {
444
39.4k
        validate_input_or_ret(in->sz > 0 && in->sz <= SIZE_MAX / 2, DAV1D_ERR(EINVAL));
445
39.4k
        c->drain = 0;
446
39.4k
    }
447
76.5k
    if (c->in.data)
448
0
        return DAV1D_ERR(EAGAIN);
449
76.5k
    dav1d_data_ref(&c->in, in);
450
451
76.5k
    int res = gen_picture(c);
452
76.5k
    if (!res)
453
74.1k
        dav1d_data_unref_internal(in);
454
455
76.5k
    return res;
456
76.5k
}
457
458
int dav1d_get_picture(Dav1dContext *const c, Dav1dPicture *const out)
459
89.3k
{
460
89.3k
    validate_input_or_ret(c != NULL, DAV1D_ERR(EINVAL));
461
89.3k
    validate_input_or_ret(out != NULL, DAV1D_ERR(EINVAL));
462
463
89.3k
    const int drain = c->drain;
464
89.3k
    c->drain = 1;
465
466
89.3k
    int res = gen_picture(c);
467
89.3k
    if (res < 0)
468
0
        return res;
469
470
89.3k
    if (c->cached_error) {
471
0
        const int res = c->cached_error;
472
0
        c->cached_error = 0;
473
0
        return res;
474
0
    }
475
476
89.3k
    if (output_picture_ready(c, c->n_fc == 1))
477
0
        return output_image(c, out);
478
479
89.3k
    if (c->n_fc > 1 && drain)
480
52.2k
        return drain_picture(c, out);
481
482
37.0k
    return DAV1D_ERR(EAGAIN);
483
89.3k
}
484
485
int dav1d_apply_grain(Dav1dContext *const c, Dav1dPicture *const out,
486
                      const Dav1dPicture *const in)
487
2.73k
{
488
2.73k
    validate_input_or_ret(c != NULL, DAV1D_ERR(EINVAL));
489
2.73k
    validate_input_or_ret(out != NULL, DAV1D_ERR(EINVAL));
490
2.73k
    validate_input_or_ret(in != NULL, DAV1D_ERR(EINVAL));
491
492
2.73k
    if (!has_grain(in)) {
493
0
        dav1d_picture_ref(out, in);
494
0
        return 0;
495
0
    }
496
497
2.73k
    int res = dav1d_picture_alloc_copy(c, out, in->p.w, in);
498
2.73k
    if (res < 0) goto error;
499
500
2.73k
    if (c->n_tc > 1) {
501
2.73k
        dav1d_task_delayed_fg(c, out, in);
502
2.73k
    } 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
2.73k
    return 0;
520
521
0
error:
522
0
    dav1d_picture_unref_internal(out);
523
0
    return res;
524
2.73k
}
525
526
39.4k
void dav1d_flush(Dav1dContext *const c) {
527
39.4k
    dav1d_data_unref_internal(&c->in);
528
39.4k
    if (c->out.p.frame_hdr)
529
0
        dav1d_thread_picture_unref(&c->out);
530
39.4k
    if (c->cache.p.frame_hdr)
531
2.88k
        dav1d_thread_picture_unref(&c->cache);
532
533
39.4k
    c->drain = 0;
534
39.4k
    c->cached_error = 0;
535
536
354k
    for (int i = 0; i < 8; i++) {
537
315k
        if (c->refs[i].p.p.frame_hdr)
538
304k
            dav1d_thread_picture_unref(&c->refs[i].p);
539
315k
        dav1d_ref_dec(&c->refs[i].segmap);
540
315k
        dav1d_ref_dec(&c->refs[i].refmvs);
541
315k
        dav1d_cdf_thread_unref(&c->cdf[i]);
542
315k
    }
543
39.4k
    c->frame_hdr = NULL;
544
39.4k
    c->seq_hdr = NULL;
545
39.4k
    dav1d_ref_dec(&c->seq_hdr_ref);
546
547
39.4k
    c->mastering_display = NULL;
548
39.4k
    c->content_light = NULL;
549
39.4k
    c->itut_t35 = NULL;
550
39.4k
    c->n_itut_t35 = 0;
551
39.4k
    dav1d_ref_dec(&c->mastering_display_ref);
552
39.4k
    dav1d_ref_dec(&c->content_light_ref);
553
39.4k
    dav1d_ref_dec(&c->itut_t35_ref);
554
555
39.4k
    dav1d_data_props_unref_internal(&c->cached_error_props);
556
557
39.4k
    if (c->n_fc == 1 && c->n_tc == 1) return;
558
39.4k
    atomic_store(c->flush, 1);
559
560
39.4k
    if (c->n_tc > 1) {
561
39.4k
        pthread_mutex_lock(&c->task_thread.lock);
562
        // stop running tasks in worker threads
563
1.30M
        for (unsigned i = 0; i < c->n_tc; i++) {
564
1.26M
            Dav1dTaskContext *const tc = &c->tc[i];
565
1.26M
            while (!tc->task_thread.flushed) {
566
6.81k
                pthread_cond_wait(&tc->task_thread.td.cond, &c->task_thread.lock);
567
6.81k
            }
568
1.26M
        }
569
276k
        for (unsigned i = 0; i < c->n_fc; i++) {
570
236k
            c->fc[i].task_thread.task_head = NULL;
571
236k
            c->fc[i].task_thread.task_tail = NULL;
572
236k
            c->fc[i].task_thread.task_cur_prev = NULL;
573
236k
            c->fc[i].task_thread.pending_tasks.head = NULL;
574
236k
            c->fc[i].task_thread.pending_tasks.tail = NULL;
575
236k
            atomic_init(&c->fc[i].task_thread.pending_tasks.merge, 0);
576
236k
        }
577
39.4k
        atomic_init(&c->task_thread.first, 0);
578
39.4k
        c->task_thread.cur = c->n_fc;
579
39.4k
        atomic_store(&c->task_thread.reset_task_cur, UINT_MAX);
580
39.4k
        atomic_store(&c->task_thread.cond_signaled, 0);
581
39.4k
        pthread_mutex_unlock(&c->task_thread.lock);
582
39.4k
    }
583
584
39.4k
    if (c->n_fc > 1) {
585
276k
        for (unsigned n = 0, next = c->frame_thread.next; n < c->n_fc; n++, next++) {
586
236k
            if (next == c->n_fc) next = 0;
587
236k
            Dav1dFrameContext *const f = &c->fc[next];
588
236k
            dav1d_decode_frame_exit(f, -1);
589
236k
            f->n_tile_data = 0;
590
236k
            f->task_thread.retval = 0;
591
236k
            f->task_thread.error = 0;
592
236k
            Dav1dThreadPicture *out_delayed = &c->frame_thread.out_delayed[next];
593
236k
            if (out_delayed->p.frame_hdr) {
594
11.7k
                dav1d_thread_picture_unref(out_delayed);
595
11.7k
            }
596
236k
        }
597
39.4k
        c->frame_thread.next = 0;
598
39.4k
    }
599
39.4k
    atomic_store(c->flush, 0);
600
39.4k
}
601
602
39.4k
COLD void dav1d_close(Dav1dContext **const c_out) {
603
39.4k
    validate_input(c_out != NULL);
604
#if TRACK_HEAP_ALLOCATIONS
605
    dav1d_log_alloc_stats(*c_out);
606
#endif
607
39.4k
    close_internal(c_out, 1);
608
39.4k
}
609
610
39.4k
static COLD void close_internal(Dav1dContext **const c_out, int flush) {
611
39.4k
    Dav1dContext *const c = *c_out;
612
39.4k
    if (!c) return;
613
614
39.4k
    if (flush) dav1d_flush(c);
615
616
39.4k
    if (c->tc) {
617
39.4k
        struct TaskThreadData *ttd = &c->task_thread;
618
39.4k
        if (ttd->inited) {
619
39.4k
            pthread_mutex_lock(&ttd->lock);
620
1.30M
            for (unsigned n = 0; n < c->n_tc && c->tc[n].task_thread.td.inited; n++)
621
1.26M
                c->tc[n].task_thread.die = 1;
622
39.4k
            pthread_cond_broadcast(&ttd->cond);
623
39.4k
            pthread_mutex_unlock(&ttd->lock);
624
1.30M
            for (unsigned n = 0; n < c->n_tc; n++) {
625
1.26M
                Dav1dTaskContext *const pf = &c->tc[n];
626
1.26M
                if (!pf->task_thread.td.inited) break;
627
1.26M
                pthread_join(pf->task_thread.td.thread, NULL);
628
1.26M
                pthread_cond_destroy(&pf->task_thread.td.cond);
629
1.26M
                pthread_mutex_destroy(&pf->task_thread.td.lock);
630
1.26M
            }
631
39.4k
            pthread_cond_destroy(&ttd->delayed_fg.cond);
632
39.4k
            pthread_cond_destroy(&ttd->cond);
633
39.4k
            pthread_mutex_destroy(&ttd->lock);
634
39.4k
        }
635
39.4k
        dav1d_free_aligned(c->tc);
636
39.4k
    }
637
638
276k
    for (unsigned n = 0; c->fc && n < c->n_fc; n++) {
639
236k
        Dav1dFrameContext *const f = &c->fc[n];
640
641
        // clean-up threading stuff
642
236k
        if (c->n_fc > 1) {
643
236k
            dav1d_free(f->tile_thread.lowest_pixel_mem);
644
236k
            dav1d_free(f->frame_thread.b);
645
236k
            dav1d_free_aligned(f->frame_thread.cbi);
646
236k
            dav1d_free_aligned(f->frame_thread.pal_idx);
647
236k
            dav1d_free_aligned(f->frame_thread.cf);
648
236k
            dav1d_free(f->frame_thread.tile_start_off);
649
236k
            dav1d_free_aligned(f->frame_thread.pal);
650
236k
        }
651
236k
        if (c->n_tc > 1) {
652
236k
            pthread_mutex_destroy(&f->task_thread.pending_tasks.lock);
653
236k
            pthread_cond_destroy(&f->task_thread.cond);
654
236k
            pthread_mutex_destroy(&f->task_thread.lock);
655
236k
        }
656
236k
        dav1d_free(f->frame_thread.frame_progress);
657
236k
        dav1d_free(f->task_thread.tasks);
658
236k
        dav1d_free(f->task_thread.tile_tasks[0]);
659
236k
        dav1d_free_aligned(f->ts);
660
236k
        dav1d_free_aligned(f->ipred_edge[0]);
661
236k
        dav1d_free(f->a);
662
236k
        dav1d_free(f->tile);
663
236k
        dav1d_free(f->lf.mask);
664
236k
        dav1d_free(f->lf.level);
665
236k
        dav1d_free(f->lf.lr_mask);
666
236k
        dav1d_free(f->lf.tx_lpf_right_edge[0]);
667
236k
        dav1d_free(f->lf.start_of_tile_row);
668
236k
        dav1d_free_aligned(f->rf.r);
669
236k
        dav1d_free_aligned(f->lf.cdef_line_buf);
670
236k
        dav1d_free_aligned(f->lf.lr_line_buf);
671
236k
    }
672
39.4k
    dav1d_free_aligned(c->fc);
673
39.4k
    if (c->n_fc > 1 && c->frame_thread.out_delayed) {
674
276k
        for (unsigned n = 0; n < c->n_fc; n++)
675
236k
            if (c->frame_thread.out_delayed[n].p.frame_hdr)
676
0
                dav1d_thread_picture_unref(&c->frame_thread.out_delayed[n]);
677
39.4k
        dav1d_free(c->frame_thread.out_delayed);
678
39.4k
    }
679
39.5k
    for (int n = 0; n < c->n_tile_data; n++)
680
96
        dav1d_data_unref_internal(&c->tile[n].data);
681
39.4k
    dav1d_free(c->tile);
682
354k
    for (int n = 0; n < 8; n++) {
683
315k
        dav1d_cdf_thread_unref(&c->cdf[n]);
684
315k
        if (c->refs[n].p.p.frame_hdr)
685
0
            dav1d_thread_picture_unref(&c->refs[n].p);
686
315k
        dav1d_ref_dec(&c->refs[n].refmvs);
687
315k
        dav1d_ref_dec(&c->refs[n].segmap);
688
315k
    }
689
39.4k
    dav1d_ref_dec(&c->seq_hdr_ref);
690
39.4k
    dav1d_ref_dec(&c->frame_hdr_ref);
691
692
39.4k
    dav1d_ref_dec(&c->mastering_display_ref);
693
39.4k
    dav1d_ref_dec(&c->content_light_ref);
694
39.4k
    dav1d_ref_dec(&c->itut_t35_ref);
695
696
39.4k
    dav1d_mem_pool_end(c->seq_hdr_pool);
697
39.4k
    dav1d_mem_pool_end(c->frame_hdr_pool);
698
39.4k
    dav1d_mem_pool_end(c->segmap_pool);
699
39.4k
    dav1d_mem_pool_end(c->refmvs_pool);
700
39.4k
    dav1d_mem_pool_end(c->cdf_pool);
701
39.4k
    dav1d_mem_pool_end(c->picture_pool);
702
39.4k
    dav1d_mem_pool_end(c->pic_ctx_pool);
703
704
39.4k
    dav1d_freep_aligned(c_out);
705
39.4k
}
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
23.7k
void dav1d_picture_unref(Dav1dPicture *const p) {
728
23.7k
    dav1d_picture_unref_internal(p);
729
23.7k
}
730
731
39.4k
uint8_t *dav1d_data_create(Dav1dData *const buf, const size_t sz) {
732
39.4k
    return dav1d_data_create_internal(buf, sz);
733
39.4k
}
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
2.33k
void dav1d_data_unref(Dav1dData *const buf) {
757
2.33k
    dav1d_data_unref_internal(buf);
758
2.33k
}
759
760
0
void dav1d_data_props_unref(Dav1dDataProps *const props) {
761
0
    dav1d_data_props_unref_internal(props);
762
0
}