Coverage Report

Created: 2026-12-01 06:47

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/dav1d/src/thread_task.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
30
#include "common/frame.h"
31
32
#include "src/thread_task.h"
33
#include "src/fg_apply.h"
34
35
// This function resets the cur pointer to the first frame theoretically
36
// executable after a task completed (ie. each time we update some progress or
37
// insert some tasks in the queue).
38
// When frame_idx is set, it can be either from a completed task, or from tasks
39
// inserted in the queue, in which case we have to make sure the cur pointer
40
// isn't past this insert.
41
// The special case where frame_idx is UINT_MAX is to handle the reset after
42
// completing a task and locklessly signaling progress. In this case we don't
43
// enter a critical section, which is needed for this function, so we set an
44
// atomic for a delayed handling, happening here. Meaning we can call this
45
// function without any actual update other than what's in the atomic, hence
46
// this special case.
47
static inline int reset_task_cur(const Dav1dContext *const c,
48
                                 struct TaskThreadData *const ttd,
49
                                 unsigned frame_idx)
50
2.48M
{
51
2.48M
    const unsigned first = atomic_load_explicit(&ttd->first, memory_order_relaxed);
52
2.48M
    unsigned reset_frame_idx = atomic_exchange_explicit(&ttd->reset_task_cur, UINT_MAX,
53
2.48M
                                                        memory_order_acquire);
54
2.48M
    if (reset_frame_idx < first) {
55
0
        if (frame_idx == UINT_MAX) return 0;
56
0
        reset_frame_idx = UINT_MAX;
57
0
    }
58
2.48M
    if (!ttd->cur && c->fc[first].task_thread.task_cur_prev == NULL)
59
829k
        return 0;
60
1.65M
    if (reset_frame_idx != UINT_MAX) {
61
119k
        if (frame_idx == UINT_MAX) {
62
71.6k
            if (reset_frame_idx > first + ttd->cur)
63
621
                return 0;
64
71.0k
            ttd->cur = reset_frame_idx - first;
65
71.0k
            goto cur_found;
66
71.6k
        }
67
1.53M
    } else if (frame_idx == UINT_MAX)
68
960k
        return 0;
69
620k
    if (frame_idx < first) frame_idx += c->n_fc;
70
620k
    const unsigned min_frame_idx = umin(reset_frame_idx, frame_idx);
71
620k
    const unsigned cur_frame_idx = first + ttd->cur;
72
620k
    if (ttd->cur < c->n_fc && cur_frame_idx < min_frame_idx)
73
42.2k
        return 0;
74
783k
    for (ttd->cur = min_frame_idx - first; ttd->cur < c->n_fc; ttd->cur++)
75
752k
        if (c->fc[(first + ttd->cur) % c->n_fc].task_thread.task_head)
76
547k
            break;
77
649k
cur_found:
78
3.59M
    for (unsigned i = ttd->cur; i < c->n_fc; i++)
79
2.94M
        c->fc[(first + i) % c->n_fc].task_thread.task_cur_prev = NULL;
80
649k
    return 1;
81
578k
}
82
83
static inline void reset_task_cur_async(struct TaskThreadData *const ttd,
84
                                        unsigned frame_idx, unsigned n_frames)
85
330k
{
86
330k
    const unsigned first = atomic_load_explicit(&ttd->first, memory_order_relaxed);
87
330k
    if (frame_idx < first) frame_idx += n_frames;
88
330k
    unsigned last_idx = frame_idx;
89
338k
    do {
90
338k
        frame_idx = last_idx;
91
338k
        last_idx = atomic_exchange_explicit(&ttd->reset_task_cur, frame_idx, memory_order_release);
92
338k
    } while (last_idx < frame_idx);
93
330k
    if (frame_idx == first && atomic_load_explicit(&ttd->first, memory_order_relaxed) != first) {
94
0
        unsigned expected = frame_idx;
95
0
        atomic_compare_exchange_strong_explicit(&ttd->reset_task_cur, &expected, UINT_MAX,
96
0
                                                memory_order_relaxed, memory_order_relaxed);
97
0
    }
98
330k
}
99
100
static void insert_tasks_between(Dav1dFrameContext *const f,
101
                                 Dav1dTask *const first, Dav1dTask *const last,
102
                                 Dav1dTask *const a, Dav1dTask *const b,
103
                                 const int cond_signal)
104
757k
{
105
757k
    struct TaskThreadData *const ttd = f->task_thread.ttd;
106
757k
    if (atomic_load_explicit(f->c->flush, memory_order_relaxed)) return;
107
757k
    assert(!a || a->next == b);
108
757k
    if (!a) f->task_thread.task_head = first;
109
606k
    else a->next = first;
110
757k
    if (!b) f->task_thread.task_tail = last;
111
757k
    last->next = b;
112
757k
    reset_task_cur(f->c, ttd, first->frame_idx);
113
757k
    if (cond_signal && !atomic_fetch_or_explicit(&ttd->cond_signaled, 1, memory_order_relaxed))
114
18.6k
        pthread_cond_signal(&ttd->cond);
115
757k
}
116
117
static void insert_tasks(Dav1dFrameContext *const f,
118
                         Dav1dTask *const first, Dav1dTask *const last,
119
                         const int cond_signal)
120
757k
{
121
    // insert task back into task queue
122
757k
    Dav1dTask *t_ptr, *prev_t = NULL;
123
757k
    for (t_ptr = f->task_thread.task_head;
124
10.4M
         t_ptr; prev_t = t_ptr, t_ptr = t_ptr->next)
125
9.79M
    {
126
        // entropy coding precedes other steps
127
9.79M
        if (t_ptr->type == DAV1D_TASK_TYPE_TILE_ENTROPY) {
128
379k
            if (first->type > DAV1D_TASK_TYPE_TILE_ENTROPY) continue;
129
            // both are entropy
130
77.0k
            if (first->sby > t_ptr->sby) continue;
131
16.5k
            if (first->sby < t_ptr->sby) {
132
6
                insert_tasks_between(f, first, last, prev_t, t_ptr, cond_signal);
133
6
                return;
134
6
            }
135
            // same sby
136
9.41M
        } else {
137
9.41M
            if (first->type == DAV1D_TASK_TYPE_TILE_ENTROPY) {
138
4.34k
                insert_tasks_between(f, first, last, prev_t, t_ptr, cond_signal);
139
4.34k
                return;
140
4.34k
            }
141
9.41M
            if (first->sby > t_ptr->sby) continue;
142
465k
            if (first->sby < t_ptr->sby) {
143
123k
                insert_tasks_between(f, first, last, prev_t, t_ptr, cond_signal);
144
123k
                return;
145
123k
            }
146
            // same sby
147
342k
            if (first->type > t_ptr->type) continue;
148
23.6k
            if (first->type < t_ptr->type) {
149
4.01k
                insert_tasks_between(f, first, last, prev_t, t_ptr, cond_signal);
150
4.01k
                return;
151
4.01k
            }
152
            // same task type
153
23.6k
        }
154
155
        // sort by tile-id
156
36.1k
        assert(first->type == DAV1D_TASK_TYPE_TILE_RECONSTRUCTION ||
157
36.1k
               first->type == DAV1D_TASK_TYPE_TILE_ENTROPY);
158
36.1k
        assert(first->type == t_ptr->type);
159
36.1k
        assert(t_ptr->sby == first->sby);
160
36.1k
        const int p = first->type == DAV1D_TASK_TYPE_TILE_ENTROPY;
161
36.1k
        const int t_tile_idx = (int) (first - f->task_thread.tile_tasks[p]);
162
36.1k
        const int p_tile_idx = (int) (t_ptr - f->task_thread.tile_tasks[p]);
163
36.1k
        assert(t_tile_idx != p_tile_idx);
164
36.1k
        if (t_tile_idx > p_tile_idx) continue;
165
152
        insert_tasks_between(f, first, last, prev_t, t_ptr, cond_signal);
166
152
        return;
167
36.1k
    }
168
    // append at the end
169
624k
    insert_tasks_between(f, first, last, prev_t, NULL, cond_signal);
170
624k
}
171
172
static inline void insert_task(Dav1dFrameContext *const f,
173
                               Dav1dTask *const t, const int cond_signal)
174
757k
{
175
757k
    insert_tasks(f, t, t, cond_signal);
176
757k
}
177
178
static inline void add_pending(Dav1dFrameContext *const f,
179
                               Dav1dTask *const first, Dav1dTask *const last)
180
244k
{
181
244k
    uintptr_t head = atomic_load_explicit(&f->task_thread.pending_tasks,
182
244k
                                          memory_order_relaxed);
183
244k
    do {
184
244k
        last->next = (Dav1dTask *) head;
185
244k
    } while (!atomic_compare_exchange_weak_explicit(&f->task_thread.pending_tasks,
186
244k
                                                    &head, (uintptr_t) first,
187
244k
                                                    memory_order_release,
188
244k
                                                    memory_order_relaxed));
189
244k
}
190
191
14.0M
static inline int merge_pending_frame(Dav1dFrameContext *const f) {
192
14.0M
    if (!atomic_load_explicit(&f->task_thread.pending_tasks, memory_order_relaxed)) return 0;
193
243k
    Dav1dTask *t = (Dav1dTask *) atomic_exchange_explicit(&f->task_thread.pending_tasks, 0,
194
243k
                                                          memory_order_acquire);
195
633k
    while (t) {
196
390k
        Dav1dTask *const tmp = t->next;
197
390k
        insert_task(f, t, 0);
198
390k
        t = tmp;
199
390k
    }
200
243k
    return 1;
201
14.0M
}
202
203
1.97M
static inline int merge_pending(const Dav1dContext *const c) {
204
1.97M
    int res = 0;
205
13.8M
    for (unsigned i = 0; i < c->n_fc; i++)
206
11.8M
        res |= merge_pending_frame(&c->fc[i]);
207
1.97M
    return res;
208
1.97M
}
209
210
static int create_filter_sbrow(Dav1dFrameContext *const f,
211
                               const int pass, Dav1dTask **res_t)
212
91.2k
{
213
91.2k
    const int has_deblock = f->frame_hdr->loopfilter.level_y[0] ||
214
9.84k
                            f->frame_hdr->loopfilter.level_y[1];
215
91.2k
    const int has_cdef = f->lf.cdef_planes;
216
91.2k
    const int has_resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
217
91.2k
    const int has_lr = f->lf.restore_planes;
218
219
91.2k
    Dav1dTask *tasks = f->task_thread.tasks;
220
91.2k
    const int uses_2pass = f->c->n_fc > 1;
221
91.2k
    int num_tasks = f->sbh * (1 + uses_2pass);
222
91.2k
    if (num_tasks > f->task_thread.num_tasks) {
223
21.1k
        const size_t size = sizeof(Dav1dTask) * num_tasks;
224
21.1k
        tasks = dav1d_realloc(ALLOC_COMMON_CTX, f->task_thread.tasks, size);
225
21.1k
        if (!tasks) return DAV1D_ERR(ENOMEM);
226
21.1k
        memset(tasks, 0, size);
227
21.1k
        f->task_thread.tasks = tasks;
228
21.1k
        f->task_thread.num_tasks = num_tasks;
229
21.1k
    }
230
91.2k
    tasks += f->sbh * (pass & 1);
231
232
91.2k
    if (pass & 1) {
233
45.6k
        atomic_store_explicit(&f->frame_thread.entropy_progress, 0, memory_order_relaxed);
234
45.6k
    } else {
235
45.5k
        const int prog_sz = ((f->sbh + 31) & ~31) >> 5;
236
45.5k
        if (prog_sz > f->frame_thread.prog_sz) {
237
21.1k
            atomic_uint *const prog = dav1d_realloc(ALLOC_COMMON_CTX, f->frame_thread.frame_progress,
238
21.1k
                                                    2 * prog_sz * sizeof(*prog));
239
21.1k
            if (!prog) return DAV1D_ERR(ENOMEM);
240
21.1k
            f->frame_thread.frame_progress = prog;
241
21.1k
            f->frame_thread.copy_lpf_progress = prog + prog_sz;
242
21.1k
        }
243
45.5k
        f->frame_thread.prog_sz = prog_sz;
244
45.5k
        memset(f->frame_thread.frame_progress, 0, prog_sz * sizeof(atomic_uint));
245
45.5k
        memset(f->frame_thread.copy_lpf_progress, 0, prog_sz * sizeof(atomic_uint));
246
45.5k
        atomic_store_explicit(&f->frame_thread.deblock_progress, 0, memory_order_relaxed);
247
45.5k
    }
248
91.2k
    f->frame_thread.next_tile_row[pass & 1] = 0;
249
250
91.2k
    Dav1dTask *t = &tasks[0];
251
91.2k
    t->sby = 0;
252
91.2k
    t->recon_progress = 1;
253
91.2k
    t->deblock_progress = 0;
254
91.2k
    t->type = pass == 1 ? DAV1D_TASK_TYPE_ENTROPY_PROGRESS :
255
91.2k
              has_deblock ? DAV1D_TASK_TYPE_DEBLOCK_COLS :
256
45.6k
              has_cdef || has_lr /* i.e. LR backup */ ? DAV1D_TASK_TYPE_DEBLOCK_ROWS :
257
4.03k
              has_resize ? DAV1D_TASK_TYPE_SUPER_RESOLUTION :
258
3.43k
              DAV1D_TASK_TYPE_RECONSTRUCTION_PROGRESS;
259
91.2k
    t->frame_idx = (int)(f - f->c->fc);
260
261
91.2k
    *res_t = t;
262
91.2k
    return 0;
263
91.2k
}
264
265
int dav1d_task_create_tile_sbrow(Dav1dFrameContext *const f, const int pass,
266
                                 const int cond_signal)
267
91.1k
{
268
91.1k
    Dav1dTask *tasks = f->task_thread.tile_tasks[0];
269
91.1k
    const int uses_2pass = f->c->n_fc > 1;
270
91.1k
    const int n_tasks_per_pass = f->frame_hdr->tiling.cols * f->frame_hdr->tiling.rows;
271
91.1k
    const int n_tasks = n_tasks_per_pass * (1 + uses_2pass);
272
91.1k
    if (pass < 2) {
273
45.6k
        if (n_tasks > f->task_thread.num_tile_tasks) {
274
21.1k
            const size_t size = sizeof(Dav1dTask) * n_tasks;
275
21.1k
            tasks = dav1d_realloc(ALLOC_COMMON_CTX, f->task_thread.tile_tasks[0], size);
276
21.1k
            if (!tasks) return DAV1D_ERR(ENOMEM);
277
21.1k
            memset(tasks, 0, size);
278
21.1k
            f->task_thread.tile_tasks[0] = tasks;
279
21.1k
            f->task_thread.num_tile_tasks = n_tasks;
280
21.1k
        }
281
45.6k
        f->task_thread.tile_tasks[1] = tasks + n_tasks_per_pass;
282
45.6k
    }
283
91.1k
    assert(n_tasks <= f->task_thread.num_tile_tasks);
284
285
91.1k
    Dav1dTask *pf_t;
286
91.1k
    const int res = create_filter_sbrow(f, pass, &pf_t);
287
91.1k
    if (res) return res;
288
289
91.1k
    Dav1dTask *const p1_tasks = f->task_thread.tile_tasks[1];
290
91.1k
    Dav1dTask *prev_t = NULL;
291
91.1k
    if (pass == 2) {
292
45.6k
        prev_t = &p1_tasks[n_tasks_per_pass - 1];
293
        // PF task is scheduled after the last sby=0 TILE task
294
45.6k
        if (f->frame_hdr->tiling.rows == 1)
295
44.3k
            prev_t = prev_t->next;
296
45.6k
    }
297
91.1k
    tasks += (pass & 1) * n_tasks_per_pass;
298
191k
    for (int tile_idx = 0; tile_idx < n_tasks_per_pass; tile_idx++) {
299
100k
        Dav1dTileState *const ts = &f->ts[tile_idx];
300
100k
        Dav1dTask *t = &tasks[tile_idx];
301
100k
        t->sby = ts->tiling.row_start >> f->sb_shift;
302
100k
        if (pf_t && t->sby) {
303
2.54k
            prev_t->next = pf_t;
304
2.54k
            prev_t = pf_t;
305
2.54k
            pf_t = NULL;
306
2.54k
        }
307
100k
        t->recon_progress = 0;
308
100k
        t->deblock_progress = 0;
309
100k
        t->deps_skip = 0;
310
100k
        t->type = pass != 1 ? DAV1D_TASK_TYPE_TILE_RECONSTRUCTION :
311
100k
                              DAV1D_TASK_TYPE_TILE_ENTROPY;
312
100k
        t->frame_idx = (int)(f - f->c->fc);
313
100k
        if (prev_t) prev_t->next = t;
314
100k
        prev_t = t;
315
100k
    }
316
91.1k
    if (pf_t) {
317
88.6k
        prev_t->next = pf_t;
318
88.6k
        prev_t = pf_t;
319
88.6k
    }
320
91.1k
    prev_t->next = NULL;
321
322
91.1k
    f->task_thread.done[pass & 1] = 0;
323
324
91.1k
    if (!(pass & 1)) {
325
45.6k
        atomic_store_explicit(&f->task_thread.init_done, 1, memory_order_relaxed);
326
45.6k
        add_pending(f, f->task_thread.tile_tasks[pass == 2], prev_t);
327
45.6k
    }
328
91.1k
    return 0;
329
91.1k
}
330
331
53.2k
void dav1d_task_frame_init(Dav1dFrameContext *const f) {
332
53.2k
    const Dav1dContext *const c = f->c;
333
334
53.2k
    atomic_store_explicit(&f->task_thread.init_done, 0, memory_order_relaxed);
335
53.2k
    f->task_thread.task_cur_prev = NULL;
336
    // schedule init task, which will schedule the remaining tasks
337
53.2k
    Dav1dTask *const t = &f->task_thread.init_task;
338
53.2k
    t->type = DAV1D_TASK_TYPE_INIT;
339
53.2k
    t->frame_idx = (int)(f - c->fc);
340
53.2k
    t->sby = 0;
341
53.2k
    t->recon_progress = t->deblock_progress = 0;
342
53.2k
    insert_task(f, t, 1);
343
53.2k
}
344
345
void dav1d_task_delayed_fg(Dav1dContext *const c, Dav1dPicture *const out,
346
                           const Dav1dPicture *const in)
347
3.08k
{
348
3.08k
    struct TaskThreadData *const ttd = &c->task_thread;
349
3.08k
    ttd->delayed_fg.in = in;
350
3.08k
    ttd->delayed_fg.out = out;
351
3.08k
    ttd->delayed_fg.type = DAV1D_TASK_TYPE_FG_PREP;
352
3.08k
    atomic_init(&ttd->delayed_fg.progress[0], 0);
353
3.08k
    atomic_init(&ttd->delayed_fg.progress[1], 0);
354
3.08k
    pthread_mutex_lock(&ttd->lock);
355
3.08k
    ttd->delayed_fg.exec = 1;
356
3.08k
    ttd->delayed_fg.finished = 0;
357
3.08k
    pthread_cond_signal(&ttd->cond);
358
3.08k
    do {
359
3.08k
        pthread_cond_wait(&ttd->delayed_fg.cond, &ttd->lock);
360
3.08k
    } while (!ttd->delayed_fg.finished);
361
3.08k
    pthread_mutex_unlock(&ttd->lock);
362
3.08k
}
363
364
static inline int ensure_progress(struct TaskThreadData *const ttd,
365
                                  Dav1dFrameContext *const f,
366
                                  Dav1dTask *const t, const enum TaskType type,
367
                                  atomic_int *const state, int *const target)
368
167k
{
369
    // deblock_rows (non-LR portion) depends on deblock of previous sbrow,
370
    // so ensure that completed. if not, re-add to task-queue; else, fall-through
371
167k
    int p1 = atomic_load_explicit(state, memory_order_acquire);
372
167k
    if (p1 < t->sby) {
373
30.7k
        t->type = type;
374
30.7k
        t->recon_progress = t->deblock_progress = 0;
375
30.7k
        *target = t->sby;
376
30.7k
        add_pending(f, t, t);
377
30.7k
        pthread_mutex_lock(&ttd->lock);
378
30.7k
        return 1;
379
30.7k
    }
380
136k
    return 0;
381
167k
}
382
383
static inline int check_tile(Dav1dTask *const t, Dav1dFrameContext *const f,
384
                             const int frame_mt)
385
819k
{
386
819k
    const int tp = t->type == DAV1D_TASK_TYPE_TILE_ENTROPY;
387
819k
    const int tile_idx = (int)(t - f->task_thread.tile_tasks[tp]);
388
819k
    Dav1dTileState *const ts = &f->ts[tile_idx];
389
819k
    const int p1 = atomic_load_explicit(&ts->progress[tp], memory_order_relaxed);
390
819k
    if (p1 < t->sby) return 1;
391
819k
    if (p1 == TILE_ERROR)
392
819k
        atomic_fetch_or_explicit(&f->task_thread.error, 1, memory_order_relaxed);
393
819k
    int error = atomic_load_explicit(&f->task_thread.error, memory_order_relaxed);
394
819k
    if (!error && frame_mt && !tp) {
395
449k
        const int p2 = atomic_load_explicit(&ts->progress[1], memory_order_acquire);
396
449k
        if (p2 <= t->sby) return 1;
397
309k
        if (p2 == TILE_ERROR)
398
309k
            atomic_fetch_or_explicit(&f->task_thread.error, 1, memory_order_relaxed);
399
309k
        error = atomic_load_explicit(&f->task_thread.error, memory_order_relaxed);
400
309k
    }
401
678k
    if (!error && frame_mt && !IS_KEY_OR_INTRA(f->frame_hdr)) {
402
        // check reference state
403
277k
        const Dav1dThreadPicture *p = &f->sr_cur;
404
277k
        const int ss_ver = p->p.p.layout == DAV1D_PIXEL_LAYOUT_I420;
405
277k
        const unsigned p_b = (t->sby + 1) << (f->sb_shift + 2);
406
277k
        const int tile_sby = t->sby - (ts->tiling.row_start >> f->sb_shift);
407
277k
        const int (*const lowest_px)[2] = ts->lowest_pixel[tile_sby];
408
763k
        for (int n = t->deps_skip; n < 7; n++, t->deps_skip++) {
409
676k
            unsigned lowest;
410
676k
            if (tp) {
411
                // if temporal mv refs are disabled, we only need this
412
                // for the primary ref; if segmentation is disabled, we
413
                // don't even need that
414
333k
                lowest = p_b;
415
343k
            } else {
416
                // +8 is postfilter-induced delay
417
343k
                const int y = lowest_px[n][0] == INT_MIN ? INT_MIN :
418
343k
                              lowest_px[n][0] + 8;
419
343k
                const int uv = lowest_px[n][1] == INT_MIN ? INT_MIN :
420
343k
                               lowest_px[n][1] * (1 << ss_ver) + 8;
421
343k
                const int max = imax(y, uv);
422
343k
                if (max == INT_MIN) continue;
423
162k
                lowest = iclip(max, 1, f->refp[n].p.p.h);
424
162k
            }
425
495k
            const unsigned p3 = atomic_load_explicit(&f->refp[n].progress[!tp],
426
495k
                                                     memory_order_acquire);
427
495k
            if (p3 < lowest) return 1;
428
305k
            if (p3 == FRAME_ERROR)
429
305k
                atomic_fetch_or_explicit(&f->task_thread.error, 1, memory_order_relaxed);
430
305k
        }
431
277k
    }
432
488k
    return 0;
433
678k
}
434
435
static inline int get_frame_progress(const Dav1dContext *const c,
436
                                     const Dav1dFrameContext *const f)
437
199k
{
438
199k
    unsigned frame_prog = c->n_fc > 1 ?
439
199k
        atomic_load_explicit(&f->sr_cur.progress[1], memory_order_relaxed) : 0;
440
199k
    if (frame_prog >= FRAME_ERROR)
441
61.6k
        return f->sbh - 1;
442
138k
    int idx = frame_prog >> (f->sb_shift + 7);
443
138k
    int prog;
444
139k
    do {
445
139k
        atomic_uint *state = &f->frame_thread.frame_progress[idx];
446
139k
        const unsigned val = ~atomic_load_explicit(state, memory_order_acquire);
447
139k
        prog = val ? ctz(val) : 32;
448
139k
        if (prog != 32) break;
449
1.91k
        prog = 0;
450
1.91k
    } while (++idx < f->frame_thread.prog_sz);
451
138k
    return ((idx << 5) | prog) - 1;
452
199k
}
453
454
// Returns the first executable init task, if any.
455
static inline Dav1dTask *find_init_task(const Dav1dContext *const c,
456
                                        struct TaskThreadData *const ttd,
457
                                        Dav1dFrameContext **const res_f)
458
1.51M
{
459
1.51M
    const unsigned first = atomic_load_explicit(&ttd->first, memory_order_relaxed);
460
10.3M
    for (unsigned i = 0; i < c->n_fc; i++) {
461
8.90M
        Dav1dFrameContext *const f = &c->fc[(first + i) % c->n_fc];
462
8.90M
        if (atomic_load_explicit(&f->task_thread.init_done, memory_order_relaxed)) continue;
463
5.56M
        Dav1dTask *const t = f->task_thread.task_head;
464
5.56M
        if (!t) continue;
465
194k
        if (t->type == DAV1D_TASK_TYPE_INIT) {
466
59.8k
            *res_f = f;
467
59.8k
            return t;
468
59.8k
        }
469
134k
        assert(t->type == DAV1D_TASK_TYPE_INIT_CDF);
470
134k
        const int p1 = f->in_cdf.progress ?
471
134k
            atomic_load_explicit(f->in_cdf.progress, memory_order_acquire) : 1;
472
134k
        if (p1) {
473
9.86k
            if (p1 == TILE_ERROR)
474
9.86k
                atomic_fetch_or_explicit(&f->task_thread.error, 1, memory_order_relaxed);
475
9.86k
            *res_f = f;
476
9.86k
            return t;
477
9.86k
        }
478
134k
    }
479
1.44M
    return NULL;
480
1.51M
}
481
482
// Checks whether the dependencies of a (non-init) task are satisfied.
483
static inline int task_ready(const Dav1dContext *const c,
484
                             Dav1dFrameContext *const f, Dav1dTask *const t)
485
10.9M
{
486
10.9M
    if (t->type == DAV1D_TASK_TYPE_INIT_CDF) return 0;
487
10.9M
    if (t->type == DAV1D_TASK_TYPE_TILE_ENTROPY ||
488
10.7M
        t->type == DAV1D_TASK_TYPE_TILE_RECONSTRUCTION)
489
655k
    {
490
        // if not bottom sbrow of tile, this task will be re-added
491
        // after it's finished
492
655k
        return !check_tile(t, f, c->n_fc > 1);
493
655k
    }
494
10.2M
    if (t->recon_progress) {
495
1.19M
        const int p = t->type == DAV1D_TASK_TYPE_ENTROPY_PROGRESS;
496
1.19M
        int error = atomic_load_explicit(&f->task_thread.error, memory_order_relaxed);
497
1.19M
        assert(!f->task_thread.done[p] || error);
498
1.19M
        const int tile_row_base = f->frame_hdr->tiling.cols *
499
1.19M
                                  f->frame_thread.next_tile_row[p];
500
1.19M
        if (p) {
501
444k
            atomic_int *const prog = &f->frame_thread.entropy_progress;
502
444k
            const int p1 = atomic_load_explicit(prog, memory_order_acquire);
503
444k
            if (p1 < t->sby) return 0;
504
405k
            if (p1 == TILE_ERROR)
505
405k
                atomic_fetch_or_explicit(&f->task_thread.error, 1, memory_order_relaxed);
506
405k
        }
507
1.60M
        for (int tc = 0; tc < f->frame_hdr->tiling.cols; tc++) {
508
1.17M
            Dav1dTileState *const ts = &f->ts[tile_row_base + tc];
509
1.17M
            const int p2 = atomic_load_explicit(&ts->progress[p], memory_order_acquire);
510
1.17M
            if (p2 < t->recon_progress) return 0;
511
451k
            if (p2 == TILE_ERROR)
512
451k
                atomic_fetch_or_explicit(&f->task_thread.error, 1, memory_order_relaxed);
513
451k
        }
514
430k
        return 1;
515
1.15M
    }
516
9.06M
    if (t->type == DAV1D_TASK_TYPE_CDEF) {
517
3.22k
        atomic_uint *prog = f->frame_thread.copy_lpf_progress;
518
3.22k
        const int p1 = atomic_load_explicit(&prog[(t->sby - 1) >> 5], memory_order_acquire);
519
3.22k
        return !!(p1 & (1U << ((t->sby - 1) & 31)));
520
3.22k
    }
521
9.05M
    assert(t->deblock_progress);
522
9.05M
    const int p1 = atomic_load_explicit(&f->frame_thread.deblock_progress, memory_order_acquire);
523
9.05M
    if (p1 >= t->deblock_progress) {
524
44.4k
        if (p1 == TILE_ERROR)
525
44.4k
            atomic_fetch_or_explicit(&f->task_thread.error, 1, memory_order_relaxed);
526
44.4k
        return 1;
527
44.4k
    }
528
9.01M
    return 0;
529
9.05M
}
530
531
// Scans the task queue, starting at the cur pointer, for the first executable
532
// decoding task. Non-executable tasks that are skipped advance the cur pointer.
533
static Dav1dTask *find_task(const Dav1dContext *const c,
534
                            struct TaskThreadData *const ttd,
535
                            Dav1dFrameContext **const res_f,
536
                            Dav1dTask **const res_prev_t)
537
1.44M
{
538
1.44M
    const unsigned first = atomic_load_explicit(&ttd->first, memory_order_relaxed);
539
2.81M
    while (ttd->cur < c->n_fc) {
540
2.16M
        Dav1dFrameContext *const f = &c->fc[(first + ttd->cur) % c->n_fc];
541
2.16M
        merge_pending_frame(f);
542
2.16M
        Dav1dTask *prev_t = f->task_thread.task_cur_prev;
543
2.16M
        for (Dav1dTask *t = prev_t ? prev_t->next : f->task_thread.task_head;
544
12.3M
             t; prev_t = t, t = t->next)
545
10.9M
        {
546
10.9M
            if (task_ready(c, f, t)) {
547
800k
                *res_f = f;
548
800k
                *res_prev_t = prev_t;
549
800k
                return t;
550
800k
            }
551
10.1M
            f->task_thread.task_cur_prev = t;
552
10.1M
        }
553
1.36M
        ttd->cur++;
554
1.36M
    }
555
643k
    return NULL;
556
1.44M
}
557
558
static int has_ready_task(const Dav1dContext *const c,
559
                          struct TaskThreadData *const ttd)
560
303k
{
561
303k
    if (ttd->delayed_fg.exec) return 1;
562
303k
    Dav1dFrameContext *f;
563
303k
    Dav1dTask *prev_t;
564
303k
    if (c->n_fc > 1 && find_init_task(c, ttd, &f)) return 1;
565
290k
    merge_pending(c);
566
290k
    reset_task_cur(c, ttd, UINT_MAX);
567
290k
    return !!find_task(c, ttd, &f, &prev_t);
568
303k
}
569
570
static void unlock_and_signal(const Dav1dContext *const c,
571
                              struct TaskThreadData *const ttd)
572
806k
{
573
806k
    const int signal = !atomic_load_explicit(&ttd->cond_signaled, memory_order_relaxed) &&
574
303k
                       has_ready_task(c, ttd);
575
806k
    if (signal) atomic_store_explicit(&ttd->cond_signaled, 1, memory_order_relaxed);
576
806k
    pthread_mutex_unlock(&ttd->lock);
577
806k
    if (signal) pthread_cond_signal(&ttd->cond);
578
806k
}
579
580
// Wakes up another thread if there's a task ready for execution and no other
581
// thread is already in the process of being woken up. If the lock is busy,
582
// signal without checking for work rather than waiting for it.
583
static void signal_if_ready(const Dav1dContext *const c,
584
                            struct TaskThreadData *const ttd)
585
330k
{
586
330k
    if (atomic_load_explicit(&ttd->cond_signaled, memory_order_relaxed)) return;
587
73.2k
    if (!pthread_mutex_trylock(&ttd->lock))
588
59.5k
        unlock_and_signal(c, ttd);
589
13.7k
    else if (!atomic_fetch_or_explicit(&ttd->cond_signaled, 1, memory_order_relaxed))
590
13.1k
        pthread_cond_signal(&ttd->cond);
591
73.2k
}
592
593
44.4k
static inline void finish_frame(Dav1dFrameContext *const f, const int res) {
594
44.4k
    dav1d_decode_frame_exit(f, res);
595
44.4k
    f->n_tile_data = 0;
596
44.4k
    pthread_cond_signal(&f->task_thread.cond);
597
44.4k
}
598
599
497k
static inline void complete_task(Dav1dFrameContext *const f) {
600
497k
    if (--f->task_thread.task_counter || !f->task_thread.done[0] ||
601
43.0k
        (f->c->n_fc > 1 && !f->task_thread.done[1]))
602
454k
    {
603
454k
        return;
604
454k
    }
605
43.0k
    const int error = atomic_load_explicit(&f->task_thread.error, memory_order_relaxed);
606
43.0k
    finish_frame(f, error == 1 ? DAV1D_ERR(EINVAL) : error ? DAV1D_ERR(ENOMEM) : 0);
607
43.0k
}
608
609
1.40k
static inline void abort_frame(Dav1dFrameContext *const f, const int error) {
610
1.40k
    atomic_store_explicit(&f->task_thread.error, error == DAV1D_ERR(EINVAL) ? 1 : -1,
611
1.40k
                          memory_order_relaxed);
612
1.40k
    f->task_thread.task_counter = 0;
613
1.40k
    f->task_thread.done[0] = 1;
614
1.40k
    f->task_thread.done[1] = 1;
615
1.40k
    atomic_store_explicit(&f->sr_cur.progress[0], FRAME_ERROR, memory_order_relaxed);
616
1.40k
    atomic_store_explicit(&f->sr_cur.progress[1], FRAME_ERROR, memory_order_relaxed);
617
1.40k
    finish_frame(f, error);
618
1.40k
}
619
620
static inline void delayed_fg_task(const Dav1dContext *const c,
621
                                   struct TaskThreadData *const ttd)
622
7.81k
{
623
7.81k
    const Dav1dPicture *const in = ttd->delayed_fg.in;
624
7.81k
    Dav1dPicture *const out = ttd->delayed_fg.out;
625
7.81k
#if CONFIG_16BPC
626
7.81k
    int off;
627
7.81k
    if (out->p.bpc != 8)
628
4.25k
        off = (out->p.bpc >> 1) - 4;
629
7.81k
#endif
630
7.81k
    switch (ttd->delayed_fg.type) {
631
3.08k
    case DAV1D_TASK_TYPE_FG_PREP:
632
3.08k
        ttd->delayed_fg.exec = 0;
633
3.08k
        unlock_and_signal(c, ttd);
634
3.08k
        switch (out->p.bpc) {
635
0
#if CONFIG_8BPC
636
1.28k
        case 8:
637
1.28k
            dav1d_prep_grain_8bpc(&c->dsp[0].fg, out, in,
638
1.28k
                                  ttd->delayed_fg.scaling_8bpc,
639
1.28k
                                  ttd->delayed_fg.grain_lut_8bpc);
640
1.28k
            break;
641
0
#endif
642
0
#if CONFIG_16BPC
643
649
        case 10:
644
1.80k
        case 12:
645
1.80k
            dav1d_prep_grain_16bpc(&c->dsp[off].fg, out, in,
646
1.80k
                                   ttd->delayed_fg.scaling_16bpc,
647
1.80k
                                   ttd->delayed_fg.grain_lut_16bpc);
648
1.80k
            break;
649
0
#endif
650
0
        default: abort();
651
3.08k
        }
652
3.08k
        ttd->delayed_fg.type = DAV1D_TASK_TYPE_FG_APPLY;
653
3.08k
        pthread_mutex_lock(&ttd->lock);
654
3.08k
        ttd->delayed_fg.exec = 1;
655
        // fall-through
656
7.81k
    case DAV1D_TASK_TYPE_FG_APPLY:;
657
7.81k
        int row = atomic_fetch_add_explicit(&ttd->delayed_fg.progress[0], 1, memory_order_relaxed);
658
7.81k
        pthread_mutex_unlock(&ttd->lock);
659
7.81k
        int progmax = (out->p.h + FG_BLOCK_SIZE - 1) / FG_BLOCK_SIZE;
660
21.3k
        while (row < progmax) {
661
13.6k
            if (row + 1 < progmax)
662
10.5k
                pthread_cond_signal(&ttd->cond);
663
3.08k
            else {
664
3.08k
                pthread_mutex_lock(&ttd->lock);
665
3.08k
                ttd->delayed_fg.exec = 0;
666
3.08k
                pthread_mutex_unlock(&ttd->lock);
667
3.08k
            }
668
13.6k
            switch (out->p.bpc) {
669
0
#if CONFIG_8BPC
670
5.68k
            case 8:
671
5.68k
                dav1d_apply_grain_row_8bpc(&c->dsp[0].fg, out, in,
672
5.68k
                                           ttd->delayed_fg.scaling_8bpc,
673
5.68k
                                           ttd->delayed_fg.grain_lut_8bpc, row);
674
5.68k
                break;
675
0
#endif
676
0
#if CONFIG_16BPC
677
3.99k
            case 10:
678
7.96k
            case 12:
679
7.96k
                dav1d_apply_grain_row_16bpc(&c->dsp[off].fg, out, in,
680
7.96k
                                            ttd->delayed_fg.scaling_16bpc,
681
7.96k
                                            ttd->delayed_fg.grain_lut_16bpc, row);
682
7.96k
                break;
683
0
#endif
684
0
            default: abort();
685
13.6k
            }
686
13.5k
            row = atomic_fetch_add_explicit(&ttd->delayed_fg.progress[0], 1, memory_order_relaxed);
687
13.5k
            atomic_fetch_add_explicit(&ttd->delayed_fg.progress[1], 1, memory_order_release);
688
13.5k
        }
689
7.73k
        pthread_mutex_lock(&ttd->lock);
690
7.73k
        ttd->delayed_fg.exec = 0;
691
7.73k
        int done = atomic_fetch_add_explicit(&ttd->delayed_fg.progress[1], 1,
692
7.73k
                                             memory_order_acquire) + 1;
693
7.73k
        progmax = atomic_load_explicit(&ttd->delayed_fg.progress[0], memory_order_relaxed);
694
        // signal for completion only once the last runner reaches this
695
7.73k
        if (done >= progmax) {
696
3.08k
            ttd->delayed_fg.finished = 1;
697
3.08k
            pthread_cond_signal(&ttd->delayed_fg.cond);
698
3.08k
        }
699
7.73k
        break;
700
0
    default: abort();
701
7.81k
    }
702
7.81k
}
703
704
303k
void *dav1d_worker_task(void *data) {
705
303k
    Dav1dTaskContext *const tc = data;
706
303k
    const Dav1dContext *const c = tc->c;
707
303k
    struct TaskThreadData *const ttd = tc->task_thread.ttd;
708
709
303k
    dav1d_set_thread_name("dav1d-worker");
710
711
303k
    pthread_mutex_lock(&ttd->lock);
712
1.52M
    for (;;) {
713
1.52M
        if (tc->task_thread.die) break;
714
1.22M
        if (atomic_load_explicit(c->flush, memory_order_relaxed)) goto park;
715
716
1.21M
        merge_pending(c);
717
1.21M
        if (ttd->delayed_fg.exec) { // run delayed film grain first
718
7.81k
            delayed_fg_task(c, ttd);
719
7.81k
            continue;
720
7.81k
        }
721
1.21M
        Dav1dFrameContext *f;
722
1.21M
        Dav1dTask *t, *prev_t = NULL;
723
1.21M
        if (c->n_fc > 1) { // run init tasks second
724
1.21M
            t = find_init_task(c, ttd, &f);
725
1.21M
            if (t) goto found;
726
1.21M
        }
727
1.15M
        t = find_task(c, ttd, &f, &prev_t); // run decoding tasks last
728
1.15M
        if (t) {
729
687k
            if (t->type > DAV1D_TASK_TYPE_INIT_CDF && t->recon_progress &&
730
400k
                t->type != DAV1D_TASK_TYPE_TILE_ENTROPY &&
731
400k
                t->type != DAV1D_TASK_TYPE_TILE_RECONSTRUCTION &&
732
400k
                t->sby + 1 < f->sbh)
733
313k
            {
734
                // add sby+1 to list to replace this one
735
313k
                const int p = t->type == DAV1D_TASK_TYPE_ENTROPY_PROGRESS;
736
313k
                Dav1dTask *next_t = &t[1];
737
313k
                *next_t = *t;
738
313k
                next_t->sby++;
739
313k
                const int ntr = f->frame_thread.next_tile_row[p] + 1;
740
313k
                const int start = f->frame_hdr->tiling.row_start_sb[ntr];
741
313k
                if (next_t->sby == start)
742
4.29k
                    f->frame_thread.next_tile_row[p] = ntr;
743
313k
                next_t->recon_progress = next_t->sby + 1;
744
313k
                insert_task(f, next_t, 0);
745
313k
            }
746
687k
            goto found;
747
687k
        }
748
466k
        if (reset_task_cur(c, ttd, UINT_MAX)) continue;
749
464k
        if (merge_pending(c)) continue;
750
469k
    park:
751
469k
        tc->task_thread.flushed = 1;
752
469k
        pthread_cond_signal(&tc->task_thread.td.cond);
753
        // we want to be woken up next time progress is signaled
754
469k
        atomic_store_explicit(&ttd->cond_signaled, 0, memory_order_relaxed);
755
469k
        pthread_cond_wait(&ttd->cond, &ttd->lock);
756
469k
        atomic_store_explicit(&ttd->cond_signaled, 0, memory_order_relaxed);
757
469k
        tc->task_thread.flushed = 0;
758
469k
        reset_task_cur(c, ttd, UINT_MAX);
759
469k
        continue;
760
761
743k
    found:
762
        // remove t from list
763
743k
        if (prev_t) prev_t->next = t->next;
764
566k
        else f->task_thread.task_head = t->next;
765
743k
        if (!t->next) f->task_thread.task_tail = prev_t;
766
743k
        if (t->type > DAV1D_TASK_TYPE_INIT_CDF && !f->task_thread.task_head)
767
57.1k
            ttd->cur++;
768
743k
        t->next = NULL;
769
        // Only wake up another thread if there's more work available and no
770
        // other thread is already in the process of being woken up (it will
771
        // in turn wake up the next one if needed). Waking up threads only for
772
        // them to go straight back to sleep is expensive and causes a lot of
773
        // lock contention. Since we always come back here after executing the
774
        // task, not waking anyone can't cause tasks to get stuck in the queue.
775
743k
        unlock_and_signal(c, ttd);
776
945k
    found_unlocked:;
777
945k
        if (atomic_load_explicit(c->flush, memory_order_relaxed))
778
945k
            atomic_fetch_or_explicit(&f->task_thread.error, 1, memory_order_relaxed);
779
945k
        int error = atomic_load_explicit(&f->task_thread.error, memory_order_relaxed);
780
781
        // run it
782
945k
        tc->f = f;
783
945k
        int sby = t->sby;
784
945k
        switch (t->type) {
785
47.3k
        case DAV1D_TASK_TYPE_INIT: {
786
47.3k
            assert(c->n_fc > 1);
787
47.3k
            int res = dav1d_decode_frame_init(f);
788
47.3k
            int p1 = f->in_cdf.progress ?
789
47.3k
                atomic_load_explicit(f->in_cdf.progress, memory_order_acquire) : 1;
790
47.3k
            if (res || p1 == TILE_ERROR) {
791
25
                pthread_mutex_lock(&ttd->lock);
792
25
                abort_frame(f, res ? res : DAV1D_ERR(EINVAL));
793
25
                reset_task_cur(c, ttd, t->frame_idx);
794
47.3k
            } else {
795
47.3k
                t->type = DAV1D_TASK_TYPE_INIT_CDF;
796
47.3k
                if (p1) goto found_unlocked;
797
9.12k
                add_pending(f, t, t);
798
9.12k
                pthread_mutex_lock(&ttd->lock);
799
9.12k
            }
800
9.14k
            continue;
801
47.3k
        }
802
47.0k
        case DAV1D_TASK_TYPE_INIT_CDF: {
803
47.0k
            assert(c->n_fc > 1);
804
47.0k
            int res = DAV1D_ERR(EINVAL);
805
47.0k
            if (!atomic_load_explicit(&f->task_thread.error, memory_order_relaxed))
806
45.7k
                res = dav1d_decode_frame_init_cdf(f);
807
138k
            for (int p = 1; p <= 2 && !res; p++)
808
91.1k
                res = dav1d_task_create_tile_sbrow(f, p, 0);
809
47.0k
            pthread_mutex_lock(&ttd->lock);
810
47.0k
            if (res) {
811
1.37k
                abort_frame(f, res);
812
1.37k
                reset_task_cur(c, ttd, t->frame_idx);
813
1.37k
                atomic_store_explicit(&f->task_thread.init_done, 1, memory_order_relaxed);
814
1.37k
            }
815
47.0k
            continue;
816
47.0k
        }
817
213k
        case DAV1D_TASK_TYPE_TILE_ENTROPY:
818
419k
        case DAV1D_TASK_TYPE_TILE_RECONSTRUCTION: {
819
419k
            const int p = t->type == DAV1D_TASK_TYPE_TILE_ENTROPY;
820
419k
            const int tile_idx = (int)(t - f->task_thread.tile_tasks[p]);
821
419k
            Dav1dTileState *const ts = &f->ts[tile_idx];
822
823
419k
            tc->ts = ts;
824
419k
            tc->by = sby << f->sb_shift;
825
419k
            const int uses_2pass = c->n_fc > 1;
826
419k
            tc->frame_thread.pass = !uses_2pass ? 0 :
827
419k
                1 + (t->type == DAV1D_TASK_TYPE_TILE_RECONSTRUCTION);
828
419k
            if (!error) error = dav1d_decode_tile_sbrow(tc);
829
419k
            const int progress = error ? TILE_ERROR : 1 + sby;
830
831
            // signal progress
832
419k
            if (error)
833
419k
                atomic_fetch_or_explicit(&f->task_thread.error, error, memory_order_relaxed);
834
419k
            if (((sby + 1) << f->sb_shift) < ts->tiling.row_end) {
835
321k
                t->sby++;
836
321k
                t->deps_skip = 0;
837
321k
                atomic_store_explicit(&ts->progress[p], progress, memory_order_release);
838
321k
                if (p && !check_tile(t, f, uses_2pass)) {
839
163k
                    reset_task_cur_async(ttd, t->frame_idx, c->n_fc);
840
163k
                    signal_if_ready(c, ttd);
841
163k
                    goto found_unlocked;
842
163k
                }
843
158k
                add_pending(f, t, t);
844
158k
                pthread_mutex_lock(&ttd->lock);
845
158k
            } else {
846
97.2k
                if (f->frame_hdr->refresh_context &&
847
45.3k
                    tc->frame_thread.pass <= 1 &&
848
22.9k
                    f->frame_hdr->tiling.update == tile_idx)
849
22.2k
                {
850
22.2k
                    error = atomic_load_explicit(&f->task_thread.error, memory_order_relaxed);
851
22.2k
                    if (!error)
852
21.6k
                        dav1d_cdf_thread_update(f->frame_hdr, f->out_cdf.data.cdf,
853
21.6k
                                                &f->ts[f->frame_hdr->tiling.update].cdf);
854
22.2k
                    if (c->n_fc > 1)
855
22.2k
                        atomic_store_explicit(f->out_cdf.progress, error ? TILE_ERROR : 1,
856
22.2k
                                              memory_order_release);
857
22.2k
                }
858
97.2k
                pthread_mutex_lock(&ttd->lock);
859
97.2k
                atomic_store_explicit(&ts->progress[p], progress, memory_order_relaxed);
860
97.2k
                reset_task_cur(c, ttd, t->frame_idx);
861
97.2k
                complete_task(f);
862
97.2k
                assert(f->task_thread.task_counter >= 0);
863
97.2k
            }
864
256k
            continue;
865
419k
        }
866
256k
        case DAV1D_TASK_TYPE_DEBLOCK_COLS:
867
167k
            if (!atomic_load_explicit(&f->task_thread.error, memory_order_relaxed))
868
119k
                f->bd_fn.filter_sbrow_deblock_cols(f, sby);
869
167k
            if (ensure_progress(ttd, f, t, DAV1D_TASK_TYPE_DEBLOCK_ROWS,
870
167k
                                &f->frame_thread.deblock_progress,
871
167k
                                &t->deblock_progress)) continue;
872
            // fall-through
873
173k
        case DAV1D_TASK_TYPE_DEBLOCK_ROWS:
874
173k
            if (!atomic_load_explicit(&f->task_thread.error, memory_order_relaxed))
875
124k
                f->bd_fn.filter_sbrow_deblock_rows(f, sby);
876
            // signal deblock progress
877
173k
            if (f->frame_hdr->loopfilter.level_y[0] ||
878
17.6k
                f->frame_hdr->loopfilter.level_y[1])
879
167k
            {
880
167k
                error = atomic_load_explicit(&f->task_thread.error, memory_order_relaxed);
881
167k
                atomic_store_explicit(&f->frame_thread.deblock_progress,
882
167k
                                      error ? TILE_ERROR : sby + 1, memory_order_release);
883
167k
                reset_task_cur_async(ttd, t->frame_idx, c->n_fc);
884
167k
                signal_if_ready(c, ttd);
885
167k
            } else if (f->lf.cdef_planes || f->lf.restore_planes) {
886
5.99k
                atomic_fetch_or_explicit(&f->frame_thread.copy_lpf_progress[sby >> 5],
887
5.99k
                                         1U << (sby & 31), memory_order_release);
888
                // CDEF needs the top buffer to be saved by lr_copy_lpf of the
889
                // previous sbrow
890
5.99k
                if (sby) {
891
5.43k
                    int prog =
892
5.43k
                        atomic_load_explicit(&f->frame_thread.copy_lpf_progress[(sby - 1) >> 5],
893
5.43k
                                             memory_order_acquire);
894
5.43k
                    if (~prog & (1U << ((sby - 1) & 31))) {
895
284
                        t->type = DAV1D_TASK_TYPE_CDEF;
896
284
                        t->recon_progress = t->deblock_progress = 0;
897
284
                        add_pending(f, t, t);
898
284
                        pthread_mutex_lock(&ttd->lock);
899
284
                        continue;
900
284
                    }
901
5.43k
                }
902
5.99k
            }
903
            // fall-through
904
173k
        case DAV1D_TASK_TYPE_CDEF:
905
173k
            if (f->lf.cdef_planes &&
906
80.1k
                !atomic_load_explicit(&f->task_thread.error, memory_order_relaxed))
907
56.2k
            {
908
56.2k
                f->bd_fn.filter_sbrow_cdef(tc, sby);
909
56.2k
            }
910
            // fall-through
911
174k
        case DAV1D_TASK_TYPE_SUPER_RESOLUTION:
912
174k
            if (f->frame_hdr->width[0] != f->frame_hdr->width[1])
913
35.8k
                if (!atomic_load_explicit(&f->task_thread.error, memory_order_relaxed))
914
29.4k
                    f->bd_fn.filter_sbrow_resize(f, sby);
915
            // fall-through
916
174k
        case DAV1D_TASK_TYPE_LOOP_RESTORATION:
917
174k
            if (!atomic_load_explicit(&f->task_thread.error, memory_order_relaxed) &&
918
124k
                f->lf.restore_planes)
919
74.5k
            {
920
74.5k
                f->bd_fn.filter_sbrow_lr(f, sby);
921
74.5k
            }
922
            // fall-through
923
199k
        case DAV1D_TASK_TYPE_RECONSTRUCTION_PROGRESS:
924
            // dummy to cover for no post-filters
925
400k
        case DAV1D_TASK_TYPE_ENTROPY_PROGRESS:
926
            // dummy to convert tile progress to frame
927
400k
            break;
928
0
        default: abort();
929
945k
        }
930
        // if task completed [typically LR], signal picture progress as per below
931
400k
        const int sbh = f->sbh;
932
400k
        const int sbsz = f->sb_step * 4;
933
400k
        if (t->type == DAV1D_TASK_TYPE_ENTROPY_PROGRESS) {
934
201k
            error = atomic_load_explicit(&f->task_thread.error, memory_order_relaxed);
935
201k
            const unsigned y = sby + 1 == sbh ? UINT_MAX : (unsigned)(sby + 1) * sbsz;
936
201k
            assert(c->n_fc > 1);
937
201k
            atomic_store_explicit(&f->sr_cur.progress[0], error ? FRAME_ERROR : y,
938
201k
                                  memory_order_release);
939
201k
            atomic_store_explicit(&f->frame_thread.entropy_progress,
940
201k
                                  error ? TILE_ERROR : sby + 1, memory_order_release);
941
201k
            pthread_mutex_lock(&ttd->lock);
942
201k
            if (sby + 1 == sbh)
943
43.9k
                f->task_thread.done[1] = 1;
944
201k
            complete_task(f);
945
201k
            reset_task_cur(c, ttd, t->frame_idx);
946
201k
            continue;
947
201k
        }
948
    // t->type != DAV1D_TASK_TYPE_ENTROPY_PROGRESS
949
400k
        atomic_fetch_or_explicit(&f->frame_thread.frame_progress[sby >> 5],
950
199k
                                 1U << (sby & 31), memory_order_release);
951
199k
        pthread_mutex_lock(&ttd->lock);
952
199k
        sby = get_frame_progress(c, f);
953
199k
        error = atomic_load_explicit(&f->task_thread.error, memory_order_relaxed);
954
199k
        const unsigned y = sby + 1 == sbh ? UINT_MAX : (unsigned)(sby + 1) * sbsz;
955
199k
        if (c->n_fc > 1)
956
199k
            atomic_store_explicit(&f->sr_cur.progress[1], error ? FRAME_ERROR : y,
957
199k
                                  memory_order_relaxed);
958
199k
        if (sby + 1 == sbh)
959
103k
            f->task_thread.done[0] = 1;
960
199k
        complete_task(f);
961
199k
        reset_task_cur(c, ttd, t->frame_idx);
962
199k
    }
963
303k
    pthread_mutex_unlock(&ttd->lock);
964
965
    return NULL;
966
303k
}