Coverage Report

Created: 2026-05-16 06:28

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