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.96M
{
51
2.96M
    const unsigned first = atomic_load_explicit(&ttd->first, memory_order_relaxed);
52
2.96M
    unsigned reset_frame_idx = atomic_exchange_explicit(&ttd->reset_task_cur, UINT_MAX,
53
2.96M
                                                        memory_order_acquire);
54
2.96M
    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.96M
    if (!ttd->cur && c->fc[first].task_thread.task_cur_prev == NULL)
59
925k
        return 0;
60
2.03M
    if (reset_frame_idx != UINT_MAX) {
61
63.9k
        if (frame_idx == UINT_MAX) {
62
40.3k
            if (reset_frame_idx > first + ttd->cur)
63
20
                return 0;
64
40.2k
            ttd->cur = reset_frame_idx - first;
65
40.2k
            goto cur_found;
66
40.3k
        }
67
1.97M
    } else if (frame_idx == UINT_MAX)
68
1.57M
        return 0;
69
418k
    if (frame_idx < first) frame_idx += c->n_fc;
70
418k
    const unsigned min_frame_idx = umin(reset_frame_idx, frame_idx);
71
418k
    const unsigned cur_frame_idx = first + ttd->cur;
72
418k
    if (ttd->cur < c->n_fc && cur_frame_idx < min_frame_idx)
73
5.72k
        return 0;
74
599k
    for (ttd->cur = min_frame_idx - first; ttd->cur < c->n_fc; ttd->cur++)
75
569k
        if (c->fc[(first + ttd->cur) % c->n_fc].task_thread.task_head)
76
382k
            break;
77
452k
cur_found:
78
2.88M
    for (unsigned i = ttd->cur; i < c->n_fc; i++)
79
2.43M
        c->fc[(first + i) % c->n_fc].task_thread.task_cur_prev = NULL;
80
452k
    return 1;
81
412k
}
82
83
static inline void reset_task_cur_async(struct TaskThreadData *const ttd,
84
                                        unsigned frame_idx, unsigned n_frames)
85
266k
{
86
266k
    const unsigned first = atomic_load_explicit(&ttd->first, memory_order_relaxed);
87
266k
    if (frame_idx < first) frame_idx += n_frames;
88
266k
    unsigned last_idx = frame_idx;
89
266k
    do {
90
266k
        frame_idx = last_idx;
91
266k
        last_idx = atomic_exchange_explicit(&ttd->reset_task_cur, frame_idx, memory_order_release);
92
266k
    } while (last_idx < frame_idx);
93
266k
    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
266k
}
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
674k
{
105
674k
    struct TaskThreadData *const ttd = f->task_thread.ttd;
106
674k
    if (atomic_load_explicit(f->c->flush, memory_order_relaxed)) return;
107
674k
    assert(!a || a->next == b);
108
674k
    if (!a) f->task_thread.task_head = first;
109
571k
    else a->next = first;
110
674k
    if (!b) f->task_thread.task_tail = last;
111
674k
    last->next = b;
112
674k
    reset_task_cur(f->c, ttd, first->frame_idx);
113
674k
    if (cond_signal && !atomic_fetch_or_explicit(&ttd->cond_signaled, 1, memory_order_relaxed))
114
20.9k
        pthread_cond_signal(&ttd->cond);
115
674k
}
116
117
static void insert_tasks(Dav1dFrameContext *const f,
118
                         Dav1dTask *const first, Dav1dTask *const last,
119
                         const int cond_signal)
120
674k
{
121
    // insert task back into task queue
122
674k
    Dav1dTask *t_ptr, *prev_t = NULL;
123
674k
    for (t_ptr = f->task_thread.task_head;
124
3.82M
         t_ptr; prev_t = t_ptr, t_ptr = t_ptr->next)
125
3.27M
    {
126
        // entropy coding precedes other steps
127
3.27M
        if (t_ptr->type == DAV1D_TASK_TYPE_TILE_ENTROPY) {
128
288k
            if (first->type > DAV1D_TASK_TYPE_TILE_ENTROPY) continue;
129
            // both are entropy
130
62.5k
            if (first->sby > t_ptr->sby) continue;
131
15.6k
            if (first->sby < t_ptr->sby) {
132
5
                insert_tasks_between(f, first, last, prev_t, t_ptr, cond_signal);
133
5
                return;
134
5
            }
135
            // same sby
136
2.98M
        } else {
137
2.98M
            if (first->type == DAV1D_TASK_TYPE_TILE_ENTROPY) {
138
2.48k
                insert_tasks_between(f, first, last, prev_t, t_ptr, cond_signal);
139
2.48k
                return;
140
2.48k
            }
141
2.97M
            if (first->sby > t_ptr->sby) continue;
142
456k
            if (first->sby < t_ptr->sby) {
143
114k
                insert_tasks_between(f, first, last, prev_t, t_ptr, cond_signal);
144
114k
                return;
145
114k
            }
146
            // same sby
147
342k
            if (first->type > t_ptr->type) continue;
148
20.6k
            if (first->type < t_ptr->type) {
149
4.65k
                insert_tasks_between(f, first, last, prev_t, t_ptr, cond_signal);
150
4.65k
                return;
151
4.65k
            }
152
            // same task type
153
20.6k
        }
154
155
        // sort by tile-id
156
31.6k
        assert(first->type == DAV1D_TASK_TYPE_TILE_RECONSTRUCTION ||
157
31.6k
               first->type == DAV1D_TASK_TYPE_TILE_ENTROPY);
158
31.6k
        assert(first->type == t_ptr->type);
159
31.6k
        assert(t_ptr->sby == first->sby);
160
31.6k
        const int p = first->type == DAV1D_TASK_TYPE_TILE_ENTROPY;
161
31.6k
        const int t_tile_idx = (int) (first - f->task_thread.tile_tasks[p]);
162
31.6k
        const int p_tile_idx = (int) (t_ptr - f->task_thread.tile_tasks[p]);
163
31.6k
        assert(t_tile_idx != p_tile_idx);
164
31.6k
        if (t_tile_idx > p_tile_idx) continue;
165
1
        insert_tasks_between(f, first, last, prev_t, t_ptr, cond_signal);
166
1
        return;
167
31.6k
    }
168
    // append at the end
169
552k
    insert_tasks_between(f, first, last, prev_t, NULL, cond_signal);
170
552k
}
171
172
static inline void insert_task(Dav1dFrameContext *const f,
173
                               Dav1dTask *const t, const int cond_signal)
174
674k
{
175
674k
    insert_tasks(f, t, t, cond_signal);
176
674k
}
177
178
static inline void add_pending(Dav1dFrameContext *const f,
179
                               Dav1dTask *const first, Dav1dTask *const last)
180
214k
{
181
214k
    uintptr_t head = atomic_load_explicit(&f->task_thread.pending_tasks,
182
214k
                                          memory_order_relaxed);
183
214k
    do {
184
214k
        last->next = (Dav1dTask *) head;
185
214k
    } while (!atomic_compare_exchange_weak_explicit(&f->task_thread.pending_tasks,
186
214k
                                                    &head, (uintptr_t) first,
187
214k
                                                    memory_order_release,
188
214k
                                                    memory_order_relaxed));
189
214k
}
190
191
16.8M
static inline int merge_pending_frame(Dav1dFrameContext *const f) {
192
16.8M
    if (!atomic_load_explicit(&f->task_thread.pending_tasks, memory_order_relaxed)) return 0;
193
214k
    Dav1dTask *t = (Dav1dTask *) atomic_exchange_explicit(&f->task_thread.pending_tasks, 0,
194
214k
                                                          memory_order_acquire);
195
527k
    while (t) {
196
313k
        Dav1dTask *const tmp = t->next;
197
313k
        insert_task(f, t, 0);
198
313k
        t = tmp;
199
313k
    }
200
214k
    return 1;
201
16.8M
}
202
203
2.49M
static inline int merge_pending(const Dav1dContext *const c) {
204
2.49M
    int res = 0;
205
17.4M
    for (unsigned i = 0; i < c->n_fc; i++)
206
14.9M
        res |= merge_pending_frame(&c->fc[i]);
207
2.49M
    return res;
208
2.49M
}
209
210
static int create_filter_sbrow(Dav1dFrameContext *const f,
211
                               const int pass, Dav1dTask **res_t)
212
59.7k
{
213
59.7k
    const int has_deblock = f->frame_hdr->loopfilter.level_y[0] ||
214
25.5k
                            f->frame_hdr->loopfilter.level_y[1];
215
59.7k
    const int has_cdef = f->lf.cdef_planes;
216
59.7k
    const int has_resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
217
59.7k
    const int has_lr = f->lf.restore_planes;
218
219
59.7k
    Dav1dTask *tasks = f->task_thread.tasks;
220
59.7k
    const int uses_2pass = f->c->n_fc > 1;
221
59.7k
    int num_tasks = f->sbh * (1 + uses_2pass);
222
59.7k
    if (num_tasks > f->task_thread.num_tasks) {
223
29.4k
        const size_t size = sizeof(Dav1dTask) * num_tasks;
224
29.4k
        tasks = dav1d_realloc(ALLOC_COMMON_CTX, f->task_thread.tasks, size);
225
29.4k
        if (!tasks) return DAV1D_ERR(ENOMEM);
226
29.4k
        memset(tasks, 0, size);
227
29.4k
        f->task_thread.tasks = tasks;
228
29.4k
        f->task_thread.num_tasks = num_tasks;
229
29.4k
    }
230
59.7k
    tasks += f->sbh * (pass & 1);
231
232
59.7k
    if (pass & 1) {
233
29.8k
        atomic_store_explicit(&f->frame_thread.entropy_progress, 0, memory_order_relaxed);
234
29.8k
    } else {
235
29.8k
        const int prog_sz = ((f->sbh + 31) & ~31) >> 5;
236
29.8k
        if (prog_sz > f->frame_thread.prog_sz) {
237
29.4k
            atomic_uint *const prog = dav1d_realloc(ALLOC_COMMON_CTX, f->frame_thread.frame_progress,
238
29.4k
                                                    2 * prog_sz * sizeof(*prog));
239
29.4k
            if (!prog) return DAV1D_ERR(ENOMEM);
240
29.4k
            f->frame_thread.frame_progress = prog;
241
29.4k
            f->frame_thread.copy_lpf_progress = prog + prog_sz;
242
29.4k
        }
243
29.8k
        f->frame_thread.prog_sz = prog_sz;
244
29.8k
        memset(f->frame_thread.frame_progress, 0, prog_sz * sizeof(atomic_uint));
245
29.8k
        memset(f->frame_thread.copy_lpf_progress, 0, prog_sz * sizeof(atomic_uint));
246
29.8k
        atomic_store_explicit(&f->frame_thread.deblock_progress, 0, memory_order_relaxed);
247
29.8k
    }
248
59.7k
    f->frame_thread.next_tile_row[pass & 1] = 0;
249
250
59.7k
    Dav1dTask *t = &tasks[0];
251
59.7k
    t->sby = 0;
252
59.7k
    t->recon_progress = 1;
253
59.7k
    t->deblock_progress = 0;
254
59.7k
    t->type = pass == 1 ? DAV1D_TASK_TYPE_ENTROPY_PROGRESS :
255
59.7k
              has_deblock ? DAV1D_TASK_TYPE_DEBLOCK_COLS :
256
29.8k
              has_cdef || has_lr /* i.e. LR backup */ ? DAV1D_TASK_TYPE_DEBLOCK_ROWS :
257
10.6k
              has_resize ? DAV1D_TASK_TYPE_SUPER_RESOLUTION :
258
8.37k
              DAV1D_TASK_TYPE_RECONSTRUCTION_PROGRESS;
259
59.7k
    t->frame_idx = (int)(f - f->c->fc);
260
261
59.7k
    *res_t = t;
262
59.7k
    return 0;
263
59.7k
}
264
265
int dav1d_task_create_tile_sbrow(Dav1dFrameContext *const f, const int pass,
266
                                 const int cond_signal)
267
59.7k
{
268
59.7k
    Dav1dTask *tasks = f->task_thread.tile_tasks[0];
269
59.7k
    const int uses_2pass = f->c->n_fc > 1;
270
59.7k
    const int n_tasks_per_pass = f->frame_hdr->tiling.cols * f->frame_hdr->tiling.rows;
271
59.7k
    const int n_tasks = n_tasks_per_pass * (1 + uses_2pass);
272
59.7k
    if (pass < 2) {
273
29.8k
        if (n_tasks > f->task_thread.num_tile_tasks) {
274
29.4k
            const size_t size = sizeof(Dav1dTask) * n_tasks;
275
29.4k
            tasks = dav1d_realloc(ALLOC_COMMON_CTX, f->task_thread.tile_tasks[0], size);
276
29.4k
            if (!tasks) return DAV1D_ERR(ENOMEM);
277
29.4k
            memset(tasks, 0, size);
278
29.4k
            f->task_thread.tile_tasks[0] = tasks;
279
29.4k
            f->task_thread.num_tile_tasks = n_tasks;
280
29.4k
        }
281
29.8k
        f->task_thread.tile_tasks[1] = tasks + n_tasks_per_pass;
282
29.8k
    }
283
59.7k
    assert(n_tasks <= f->task_thread.num_tile_tasks);
284
285
59.7k
    Dav1dTask *pf_t;
286
59.7k
    const int res = create_filter_sbrow(f, pass, &pf_t);
287
59.7k
    if (res) return res;
288
289
59.7k
    Dav1dTask *const p1_tasks = f->task_thread.tile_tasks[1];
290
59.7k
    Dav1dTask *prev_t = NULL;
291
59.7k
    if (pass == 2) {
292
29.8k
        prev_t = &p1_tasks[n_tasks_per_pass - 1];
293
        // PF task is scheduled after the last sby=0 TILE task
294
29.8k
        if (f->frame_hdr->tiling.rows == 1)
295
29.3k
            prev_t = prev_t->next;
296
29.8k
    }
297
59.7k
    tasks += (pass & 1) * n_tasks_per_pass;
298
128k
    for (int tile_idx = 0; tile_idx < n_tasks_per_pass; tile_idx++) {
299
68.4k
        Dav1dTileState *const ts = &f->ts[tile_idx];
300
68.4k
        Dav1dTask *t = &tasks[tile_idx];
301
68.4k
        t->sby = ts->tiling.row_start >> f->sb_shift;
302
68.4k
        if (pf_t && t->sby) {
303
982
            prev_t->next = pf_t;
304
982
            prev_t = pf_t;
305
982
            pf_t = NULL;
306
982
        }
307
68.4k
        t->recon_progress = 0;
308
68.4k
        t->deblock_progress = 0;
309
68.4k
        t->deps_skip = 0;
310
68.4k
        t->type = pass != 1 ? DAV1D_TASK_TYPE_TILE_RECONSTRUCTION :
311
68.4k
                              DAV1D_TASK_TYPE_TILE_ENTROPY;
312
68.4k
        t->frame_idx = (int)(f - f->c->fc);
313
68.4k
        if (prev_t) prev_t->next = t;
314
68.4k
        prev_t = t;
315
68.4k
    }
316
59.7k
    if (pf_t) {
317
58.7k
        prev_t->next = pf_t;
318
58.7k
        prev_t = pf_t;
319
58.7k
    }
320
59.7k
    prev_t->next = NULL;
321
322
59.7k
    f->task_thread.done[pass & 1] = 0;
323
324
59.7k
    if (!(pass & 1)) {
325
29.8k
        atomic_store_explicit(&f->task_thread.init_done, 1, memory_order_relaxed);
326
29.8k
        add_pending(f, f->task_thread.tile_tasks[pass == 2], prev_t);
327
29.8k
    }
328
59.7k
    return 0;
329
59.7k
}
330
331
31.7k
void dav1d_task_frame_init(Dav1dFrameContext *const f) {
332
31.7k
    const Dav1dContext *const c = f->c;
333
334
31.7k
    atomic_store_explicit(&f->task_thread.init_done, 0, memory_order_relaxed);
335
31.7k
    f->task_thread.task_cur_prev = NULL;
336
    // schedule init task, which will schedule the remaining tasks
337
31.7k
    Dav1dTask *const t = &f->task_thread.init_task;
338
31.7k
    t->type = DAV1D_TASK_TYPE_INIT;
339
31.7k
    t->frame_idx = (int)(f - c->fc);
340
31.7k
    t->sby = 0;
341
31.7k
    t->recon_progress = t->deblock_progress = 0;
342
31.7k
    insert_task(f, t, 1);
343
31.7k
}
344
345
void dav1d_task_delayed_fg(Dav1dContext *const c, Dav1dPicture *const out,
346
                           const Dav1dPicture *const in)
347
910
{
348
910
    struct TaskThreadData *const ttd = &c->task_thread;
349
910
    ttd->delayed_fg.in = in;
350
910
    ttd->delayed_fg.out = out;
351
910
    ttd->delayed_fg.type = DAV1D_TASK_TYPE_FG_PREP;
352
910
    atomic_init(&ttd->delayed_fg.progress[0], 0);
353
910
    atomic_init(&ttd->delayed_fg.progress[1], 0);
354
910
    pthread_mutex_lock(&ttd->lock);
355
910
    ttd->delayed_fg.exec = 1;
356
910
    ttd->delayed_fg.finished = 0;
357
910
    pthread_cond_signal(&ttd->cond);
358
910
    do {
359
910
        pthread_cond_wait(&ttd->delayed_fg.cond, &ttd->lock);
360
910
    } while (!ttd->delayed_fg.finished);
361
910
    pthread_mutex_unlock(&ttd->lock);
362
910
}
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
99.4k
{
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
99.4k
    int p1 = atomic_load_explicit(state, memory_order_acquire);
372
99.4k
    if (p1 < t->sby) {
373
16.5k
        t->type = type;
374
16.5k
        t->recon_progress = t->deblock_progress = 0;
375
16.5k
        *target = t->sby;
376
16.5k
        add_pending(f, t, t);
377
16.5k
        pthread_mutex_lock(&ttd->lock);
378
16.5k
        return 1;
379
16.5k
    }
380
82.9k
    return 0;
381
99.4k
}
382
383
static inline int check_tile(Dav1dTask *const t, Dav1dFrameContext *const f,
384
                             const int frame_mt)
385
534k
{
386
534k
    const int tp = t->type == DAV1D_TASK_TYPE_TILE_ENTROPY;
387
534k
    const int tile_idx = (int)(t - f->task_thread.tile_tasks[tp]);
388
534k
    Dav1dTileState *const ts = &f->ts[tile_idx];
389
534k
    const int p1 = atomic_load_explicit(&ts->progress[tp], memory_order_relaxed);
390
534k
    if (p1 < t->sby) return 1;
391
534k
    if (p1 == TILE_ERROR)
392
534k
        atomic_fetch_or_explicit(&f->task_thread.error, 1, memory_order_relaxed);
393
534k
    int error = atomic_load_explicit(&f->task_thread.error, memory_order_relaxed);
394
534k
    if (!error && frame_mt && !tp) {
395
236k
        const int p2 = atomic_load_explicit(&ts->progress[1], memory_order_acquire);
396
236k
        if (p2 <= t->sby) return 1;
397
188k
        if (p2 == TILE_ERROR)
398
188k
            atomic_fetch_or_explicit(&f->task_thread.error, 1, memory_order_relaxed);
399
188k
        error = atomic_load_explicit(&f->task_thread.error, memory_order_relaxed);
400
188k
    }
401
486k
    if (!error && frame_mt && !IS_KEY_OR_INTRA(f->frame_hdr)) {
402
        // check reference state
403
60.1k
        const Dav1dThreadPicture *p = &f->sr_cur;
404
60.1k
        const int ss_ver = p->p.p.layout == DAV1D_PIXEL_LAYOUT_I420;
405
60.1k
        const unsigned p_b = (t->sby + 1) << (f->sb_shift + 2);
406
60.1k
        const int tile_sby = t->sby - (ts->tiling.row_start >> f->sb_shift);
407
60.1k
        const int (*const lowest_px)[2] = ts->lowest_pixel[tile_sby];
408
235k
        for (int n = t->deps_skip; n < 7; n++, t->deps_skip++) {
409
204k
            unsigned lowest;
410
204k
            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
109k
                lowest = p_b;
415
109k
            } else {
416
                // +8 is postfilter-induced delay
417
95.0k
                const int y = lowest_px[n][0] == INT_MIN ? INT_MIN :
418
95.0k
                              lowest_px[n][0] + 8;
419
95.0k
                const int uv = lowest_px[n][1] == INT_MIN ? INT_MIN :
420
95.0k
                               lowest_px[n][1] * (1 << ss_ver) + 8;
421
95.0k
                const int max = imax(y, uv);
422
95.0k
                if (max == INT_MIN) continue;
423
31.3k
                lowest = iclip(max, 1, f->refp[n].p.p.h);
424
31.3k
            }
425
140k
            const unsigned p3 = atomic_load_explicit(&f->refp[n].progress[!tp],
426
140k
                                                     memory_order_acquire);
427
140k
            if (p3 < lowest) return 1;
428
112k
            if (p3 == FRAME_ERROR)
429
112k
                atomic_fetch_or_explicit(&f->task_thread.error, 1, memory_order_relaxed);
430
112k
        }
431
60.1k
    }
432
458k
    return 0;
433
486k
}
434
435
static inline int get_frame_progress(const Dav1dContext *const c,
436
                                     const Dav1dFrameContext *const f)
437
194k
{
438
194k
    unsigned frame_prog = c->n_fc > 1 ?
439
194k
        atomic_load_explicit(&f->sr_cur.progress[1], memory_order_relaxed) : 0;
440
194k
    if (frame_prog >= FRAME_ERROR)
441
72.6k
        return f->sbh - 1;
442
121k
    int idx = frame_prog >> (f->sb_shift + 7);
443
121k
    int prog;
444
123k
    do {
445
123k
        atomic_uint *state = &f->frame_thread.frame_progress[idx];
446
123k
        const unsigned val = ~atomic_load_explicit(state, memory_order_acquire);
447
123k
        prog = val ? ctz(val) : 32;
448
123k
        if (prog != 32) break;
449
2.17k
        prog = 0;
450
2.17k
    } while (++idx < f->frame_thread.prog_sz);
451
121k
    return ((idx << 5) | prog) - 1;
452
194k
}
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.69M
{
459
1.69M
    const unsigned first = atomic_load_explicit(&ttd->first, memory_order_relaxed);
460
11.6M
    for (unsigned i = 0; i < c->n_fc; i++) {
461
10.0M
        Dav1dFrameContext *const f = &c->fc[(first + i) % c->n_fc];
462
10.0M
        if (atomic_load_explicit(&f->task_thread.init_done, memory_order_relaxed)) continue;
463
8.66M
        Dav1dTask *const t = f->task_thread.task_head;
464
8.66M
        if (!t) continue;
465
81.1k
        if (t->type == DAV1D_TASK_TYPE_INIT) {
466
37.0k
            *res_f = f;
467
37.0k
            return t;
468
37.0k
        }
469
44.0k
        assert(t->type == DAV1D_TASK_TYPE_INIT_CDF);
470
44.0k
        const int p1 = f->in_cdf.progress ?
471
44.0k
            atomic_load_explicit(f->in_cdf.progress, memory_order_acquire) : 1;
472
44.0k
        if (p1) {
473
4.32k
            if (p1 == TILE_ERROR)
474
4.32k
                atomic_fetch_or_explicit(&f->task_thread.error, 1, memory_order_relaxed);
475
4.32k
            *res_f = f;
476
4.32k
            return t;
477
4.32k
        }
478
44.0k
    }
479
1.65M
    return NULL;
480
1.69M
}
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
2.85M
{
486
2.85M
    if (t->type == DAV1D_TASK_TYPE_INIT_CDF) return 0;
487
2.84M
    if (t->type == DAV1D_TASK_TYPE_TILE_ENTROPY ||
488
2.79M
        t->type == DAV1D_TASK_TYPE_TILE_RECONSTRUCTION)
489
367k
    {
490
        // if not bottom sbrow of tile, this task will be re-added
491
        // after it's finished
492
367k
        return !check_tile(t, f, c->n_fc > 1);
493
367k
    }
494
2.47M
    if (t->recon_progress) {
495
803k
        const int p = t->type == DAV1D_TASK_TYPE_ENTROPY_PROGRESS;
496
803k
        int error = atomic_load_explicit(&f->task_thread.error, memory_order_relaxed);
497
803k
        assert(!f->task_thread.done[p] || error);
498
803k
        const int tile_row_base = f->frame_hdr->tiling.cols *
499
803k
                                  f->frame_thread.next_tile_row[p];
500
803k
        if (p) {
501
323k
            atomic_int *const prog = &f->frame_thread.entropy_progress;
502
323k
            const int p1 = atomic_load_explicit(prog, memory_order_acquire);
503
323k
            if (p1 < t->sby) return 0;
504
287k
            if (p1 == TILE_ERROR)
505
287k
                atomic_fetch_or_explicit(&f->task_thread.error, 1, memory_order_relaxed);
506
287k
        }
507
1.19M
        for (int tc = 0; tc < f->frame_hdr->tiling.cols; tc++) {
508
779k
            Dav1dTileState *const ts = &f->ts[tile_row_base + tc];
509
779k
            const int p2 = atomic_load_explicit(&ts->progress[p], memory_order_acquire);
510
779k
            if (p2 < t->recon_progress) return 0;
511
423k
            if (p2 == TILE_ERROR)
512
423k
                atomic_fetch_or_explicit(&f->task_thread.error, 1, memory_order_relaxed);
513
423k
        }
514
412k
        return 1;
515
767k
    }
516
1.67M
    if (t->type == DAV1D_TASK_TYPE_CDEF) {
517
79.5k
        atomic_uint *prog = f->frame_thread.copy_lpf_progress;
518
79.5k
        const int p1 = atomic_load_explicit(&prog[(t->sby - 1) >> 5], memory_order_acquire);
519
79.5k
        return !!(p1 & (1U << ((t->sby - 1) & 31)));
520
79.5k
    }
521
1.59M
    assert(t->deblock_progress);
522
1.59M
    const int p1 = atomic_load_explicit(&f->frame_thread.deblock_progress, memory_order_acquire);
523
1.59M
    if (p1 >= t->deblock_progress) {
524
19.9k
        if (p1 == TILE_ERROR)
525
19.9k
            atomic_fetch_or_explicit(&f->task_thread.error, 1, memory_order_relaxed);
526
19.9k
        return 1;
527
19.9k
    }
528
1.57M
    return 0;
529
1.59M
}
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.65M
{
538
1.65M
    const unsigned first = atomic_load_explicit(&ttd->first, memory_order_relaxed);
539
2.81M
    while (ttd->cur < c->n_fc) {
540
1.88M
        Dav1dFrameContext *const f = &c->fc[(first + ttd->cur) % c->n_fc];
541
1.88M
        merge_pending_frame(f);
542
1.88M
        Dav1dTask *prev_t = f->task_thread.task_cur_prev;
543
1.88M
        for (Dav1dTask *t = prev_t ? prev_t->next : f->task_thread.task_head;
544
4.01M
             t; prev_t = t, t = t->next)
545
2.85M
        {
546
2.85M
            if (task_ready(c, f, t)) {
547
725k
                *res_f = f;
548
725k
                *res_prev_t = prev_t;
549
725k
                return t;
550
725k
            }
551
2.13M
            f->task_thread.task_cur_prev = t;
552
2.13M
        }
553
1.16M
        ttd->cur++;
554
1.16M
    }
555
930k
    return NULL;
556
1.65M
}
557
558
static int has_ready_task(const Dav1dContext *const c,
559
                          struct TaskThreadData *const ttd)
560
223k
{
561
223k
    if (ttd->delayed_fg.exec) return 1;
562
223k
    Dav1dFrameContext *f;
563
223k
    Dav1dTask *prev_t;
564
223k
    if (c->n_fc > 1 && find_init_task(c, ttd, &f)) return 1;
565
217k
    merge_pending(c);
566
217k
    reset_task_cur(c, ttd, UINT_MAX);
567
217k
    return !!find_task(c, ttd, &f, &prev_t);
568
223k
}
569
570
static void unlock_and_signal(const Dav1dContext *const c,
571
                              struct TaskThreadData *const ttd)
572
707k
{
573
707k
    const int signal = !atomic_load_explicit(&ttd->cond_signaled, memory_order_relaxed) &&
574
223k
                       has_ready_task(c, ttd);
575
707k
    if (signal) atomic_store_explicit(&ttd->cond_signaled, 1, memory_order_relaxed);
576
707k
    pthread_mutex_unlock(&ttd->lock);
577
707k
    if (signal) pthread_cond_signal(&ttd->cond);
578
707k
}
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
266k
{
586
266k
    if (atomic_load_explicit(&ttd->cond_signaled, memory_order_relaxed)) return;
587
44.7k
    if (!pthread_mutex_trylock(&ttd->lock))
588
34.4k
        unlock_and_signal(c, ttd);
589
10.3k
    else if (!atomic_fetch_or_explicit(&ttd->cond_signaled, 1, memory_order_relaxed))
590
9.92k
        pthread_cond_signal(&ttd->cond);
591
44.7k
}
592
593
30.4k
static inline void finish_frame(Dav1dFrameContext *const f, const int res) {
594
30.4k
    dav1d_decode_frame_exit(f, res);
595
30.4k
    f->n_tile_data = 0;
596
30.4k
    pthread_cond_signal(&f->task_thread.cond);
597
30.4k
}
598
599
456k
static inline void complete_task(Dav1dFrameContext *const f) {
600
456k
    if (--f->task_thread.task_counter || !f->task_thread.done[0] ||
601
29.5k
        (f->c->n_fc > 1 && !f->task_thread.done[1]))
602
426k
    {
603
426k
        return;
604
426k
    }
605
29.5k
    const int error = atomic_load_explicit(&f->task_thread.error, memory_order_relaxed);
606
29.5k
    finish_frame(f, error == 1 ? DAV1D_ERR(EINVAL) : error ? DAV1D_ERR(ENOMEM) : 0);
607
29.5k
}
608
609
914
static inline void abort_frame(Dav1dFrameContext *const f, const int error) {
610
914
    atomic_store_explicit(&f->task_thread.error, error == DAV1D_ERR(EINVAL) ? 1 : -1,
611
914
                          memory_order_relaxed);
612
914
    f->task_thread.task_counter = 0;
613
914
    f->task_thread.done[0] = 1;
614
914
    f->task_thread.done[1] = 1;
615
914
    atomic_store_explicit(&f->sr_cur.progress[0], FRAME_ERROR, memory_order_relaxed);
616
914
    atomic_store_explicit(&f->sr_cur.progress[1], FRAME_ERROR, memory_order_relaxed);
617
914
    finish_frame(f, error);
618
914
}
619
620
static inline void delayed_fg_task(const Dav1dContext *const c,
621
                                   struct TaskThreadData *const ttd)
622
3.74k
{
623
3.74k
    const Dav1dPicture *const in = ttd->delayed_fg.in;
624
3.74k
    Dav1dPicture *const out = ttd->delayed_fg.out;
625
3.74k
#if CONFIG_16BPC
626
3.74k
    int off;
627
3.74k
    if (out->p.bpc != 8)
628
2.04k
        off = (out->p.bpc >> 1) - 4;
629
3.74k
#endif
630
3.74k
    switch (ttd->delayed_fg.type) {
631
910
    case DAV1D_TASK_TYPE_FG_PREP:
632
910
        ttd->delayed_fg.exec = 0;
633
910
        unlock_and_signal(c, ttd);
634
910
        switch (out->p.bpc) {
635
0
#if CONFIG_8BPC
636
427
        case 8:
637
427
            dav1d_prep_grain_8bpc(&c->dsp[0].fg, out, in,
638
427
                                  ttd->delayed_fg.scaling_8bpc,
639
427
                                  ttd->delayed_fg.grain_lut_8bpc);
640
427
            break;
641
0
#endif
642
0
#if CONFIG_16BPC
643
390
        case 10:
644
483
        case 12:
645
483
            dav1d_prep_grain_16bpc(&c->dsp[off].fg, out, in,
646
483
                                   ttd->delayed_fg.scaling_16bpc,
647
483
                                   ttd->delayed_fg.grain_lut_16bpc);
648
483
            break;
649
0
#endif
650
0
        default: abort();
651
910
        }
652
910
        ttd->delayed_fg.type = DAV1D_TASK_TYPE_FG_APPLY;
653
910
        pthread_mutex_lock(&ttd->lock);
654
910
        ttd->delayed_fg.exec = 1;
655
        // fall-through
656
3.74k
    case DAV1D_TASK_TYPE_FG_APPLY:;
657
3.74k
        int row = atomic_fetch_add_explicit(&ttd->delayed_fg.progress[0], 1, memory_order_relaxed);
658
3.74k
        pthread_mutex_unlock(&ttd->lock);
659
3.74k
        int progmax = (out->p.h + FG_BLOCK_SIZE - 1) / FG_BLOCK_SIZE;
660
10.7k
        while (row < progmax) {
661
7.09k
            if (row + 1 < progmax)
662
6.18k
                pthread_cond_signal(&ttd->cond);
663
911
            else {
664
911
                pthread_mutex_lock(&ttd->lock);
665
911
                ttd->delayed_fg.exec = 0;
666
911
                pthread_mutex_unlock(&ttd->lock);
667
911
            }
668
7.09k
            switch (out->p.bpc) {
669
0
#if CONFIG_8BPC
670
3.46k
            case 8:
671
3.46k
                dav1d_apply_grain_row_8bpc(&c->dsp[0].fg, out, in,
672
3.46k
                                           ttd->delayed_fg.scaling_8bpc,
673
3.46k
                                           ttd->delayed_fg.grain_lut_8bpc, row);
674
3.46k
                break;
675
0
#endif
676
0
#if CONFIG_16BPC
677
3.21k
            case 10:
678
3.63k
            case 12:
679
3.63k
                dav1d_apply_grain_row_16bpc(&c->dsp[off].fg, out, in,
680
3.63k
                                            ttd->delayed_fg.scaling_16bpc,
681
3.63k
                                            ttd->delayed_fg.grain_lut_16bpc, row);
682
3.63k
                break;
683
0
#endif
684
0
            default: abort();
685
7.09k
            }
686
7.00k
            row = atomic_fetch_add_explicit(&ttd->delayed_fg.progress[0], 1, memory_order_relaxed);
687
7.00k
            atomic_fetch_add_explicit(&ttd->delayed_fg.progress[1], 1, memory_order_release);
688
7.00k
        }
689
3.65k
        pthread_mutex_lock(&ttd->lock);
690
3.65k
        ttd->delayed_fg.exec = 0;
691
3.65k
        int done = atomic_fetch_add_explicit(&ttd->delayed_fg.progress[1], 1,
692
3.65k
                                             memory_order_acquire) + 1;
693
3.65k
        progmax = atomic_load_explicit(&ttd->delayed_fg.progress[0], memory_order_relaxed);
694
        // signal for completion only once the last runner reaches this
695
3.65k
        if (done >= progmax) {
696
910
            ttd->delayed_fg.finished = 1;
697
910
            pthread_cond_signal(&ttd->delayed_fg.cond);
698
910
        }
699
3.65k
        break;
700
0
    default: abort();
701
3.74k
    }
702
3.74k
}
703
704
683k
void *dav1d_worker_task(void *data) {
705
683k
    Dav1dTaskContext *const tc = data;
706
683k
    const Dav1dContext *const c = tc->c;
707
683k
    struct TaskThreadData *const ttd = tc->task_thread.ttd;
708
709
683k
    dav1d_set_thread_name("dav1d-worker");
710
711
683k
    pthread_mutex_lock(&ttd->lock);
712
2.17M
    for (;;) {
713
2.17M
        if (tc->task_thread.die) break;
714
1.49M
        if (atomic_load_explicit(c->flush, memory_order_relaxed)) goto park;
715
716
1.47M
        merge_pending(c);
717
1.47M
        if (ttd->delayed_fg.exec) { // run delayed film grain first
718
3.74k
            delayed_fg_task(c, ttd);
719
3.74k
            continue;
720
3.74k
        }
721
1.47M
        Dav1dFrameContext *f;
722
1.47M
        Dav1dTask *t, *prev_t = NULL;
723
1.47M
        if (c->n_fc > 1) { // run init tasks second
724
1.47M
            t = find_init_task(c, ttd, &f);
725
1.47M
            if (t) goto found;
726
1.47M
        }
727
1.43M
        t = find_task(c, ttd, &f, &prev_t); // run decoding tasks last
728
1.43M
        if (t) {
729
637k
            if (t->type > DAV1D_TASK_TYPE_INIT_CDF && t->recon_progress &&
730
388k
                t->type != DAV1D_TASK_TYPE_TILE_ENTROPY &&
731
388k
                t->type != DAV1D_TASK_TYPE_TILE_RECONSTRUCTION &&
732
388k
                t->sby + 1 < f->sbh)
733
329k
            {
734
                // add sby+1 to list to replace this one
735
329k
                const int p = t->type == DAV1D_TASK_TYPE_ENTROPY_PROGRESS;
736
329k
                Dav1dTask *next_t = &t[1];
737
329k
                *next_t = *t;
738
329k
                next_t->sby++;
739
329k
                const int ntr = f->frame_thread.next_tile_row[p] + 1;
740
329k
                const int start = f->frame_hdr->tiling.row_start_sb[ntr];
741
329k
                if (next_t->sby == start)
742
3.92k
                    f->frame_thread.next_tile_row[p] = ntr;
743
329k
                next_t->recon_progress = next_t->sby + 1;
744
329k
                insert_task(f, next_t, 0);
745
329k
            }
746
637k
            goto found;
747
637k
        }
748
800k
        if (reset_task_cur(c, ttd, UINT_MAX)) continue;
749
799k
        if (merge_pending(c)) continue;
750
812k
    park:
751
812k
        tc->task_thread.flushed = 1;
752
812k
        pthread_cond_signal(&tc->task_thread.td.cond);
753
        // we want to be woken up next time progress is signaled
754
812k
        atomic_store_explicit(&ttd->cond_signaled, 0, memory_order_relaxed);
755
812k
        pthread_cond_wait(&ttd->cond, &ttd->lock);
756
812k
        atomic_store_explicit(&ttd->cond_signaled, 0, memory_order_relaxed);
757
812k
        tc->task_thread.flushed = 0;
758
812k
        reset_task_cur(c, ttd, UINT_MAX);
759
812k
        continue;
760
761
672k
    found:
762
        // remove t from list
763
672k
        if (prev_t) prev_t->next = t->next;
764
516k
        else f->task_thread.task_head = t->next;
765
672k
        if (!t->next) f->task_thread.task_tail = prev_t;
766
672k
        if (t->type > DAV1D_TASK_TYPE_INIT_CDF && !f->task_thread.task_head)
767
41.6k
            ttd->cur++;
768
672k
        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
672k
        unlock_and_signal(c, ttd);
776
865k
    found_unlocked:;
777
865k
        if (atomic_load_explicit(c->flush, memory_order_relaxed))
778
865k
            atomic_fetch_or_explicit(&f->task_thread.error, 1, memory_order_relaxed);
779
865k
        int error = atomic_load_explicit(&f->task_thread.error, memory_order_relaxed);
780
781
        // run it
782
865k
        tc->f = f;
783
865k
        int sby = t->sby;
784
865k
        switch (t->type) {
785
30.8k
        case DAV1D_TASK_TYPE_INIT: {
786
30.8k
            assert(c->n_fc > 1);
787
30.8k
            int res = dav1d_decode_frame_init(f);
788
30.8k
            int p1 = f->in_cdf.progress ?
789
30.8k
                atomic_load_explicit(f->in_cdf.progress, memory_order_acquire) : 1;
790
30.8k
            if (res || p1 == TILE_ERROR) {
791
21
                pthread_mutex_lock(&ttd->lock);
792
21
                abort_frame(f, res ? res : DAV1D_ERR(EINVAL));
793
21
                reset_task_cur(c, ttd, t->frame_idx);
794
30.8k
            } else {
795
30.8k
                t->type = DAV1D_TASK_TYPE_INIT_CDF;
796
30.8k
                if (p1) goto found_unlocked;
797
3.91k
                add_pending(f, t, t);
798
3.91k
                pthread_mutex_lock(&ttd->lock);
799
3.91k
            }
800
3.93k
            continue;
801
30.8k
        }
802
30.7k
        case DAV1D_TASK_TYPE_INIT_CDF: {
803
30.7k
            assert(c->n_fc > 1);
804
30.7k
            int res = DAV1D_ERR(EINVAL);
805
30.7k
            if (!atomic_load_explicit(&f->task_thread.error, memory_order_relaxed))
806
30.1k
                res = dav1d_decode_frame_init_cdf(f);
807
90.4k
            for (int p = 1; p <= 2 && !res; p++)
808
59.7k
                res = dav1d_task_create_tile_sbrow(f, p, 0);
809
30.7k
            pthread_mutex_lock(&ttd->lock);
810
30.7k
            if (res) {
811
893
                abort_frame(f, res);
812
893
                reset_task_cur(c, ttd, t->frame_idx);
813
893
                atomic_store_explicit(&f->task_thread.init_done, 1, memory_order_relaxed);
814
893
            }
815
30.7k
            continue;
816
30.7k
        }
817
201k
        case DAV1D_TASK_TYPE_TILE_ENTROPY:
818
397k
        case DAV1D_TASK_TYPE_TILE_RECONSTRUCTION: {
819
397k
            const int p = t->type == DAV1D_TASK_TYPE_TILE_ENTROPY;
820
397k
            const int tile_idx = (int)(t - f->task_thread.tile_tasks[p]);
821
397k
            Dav1dTileState *const ts = &f->ts[tile_idx];
822
823
397k
            tc->ts = ts;
824
397k
            tc->by = sby << f->sb_shift;
825
397k
            const int uses_2pass = c->n_fc > 1;
826
397k
            tc->frame_thread.pass = !uses_2pass ? 0 :
827
397k
                1 + (t->type == DAV1D_TASK_TYPE_TILE_RECONSTRUCTION);
828
397k
            if (!error) error = dav1d_decode_tile_sbrow(tc);
829
397k
            const int progress = error ? TILE_ERROR : 1 + sby;
830
831
            // signal progress
832
397k
            if (error)
833
397k
                atomic_fetch_or_explicit(&f->task_thread.error, error, memory_order_relaxed);
834
397k
            if (((sby + 1) << f->sb_shift) < ts->tiling.row_end) {
835
329k
                t->sby++;
836
329k
                t->deps_skip = 0;
837
329k
                atomic_store_explicit(&ts->progress[p], progress, memory_order_release);
838
329k
                if (p && !check_tile(t, f, uses_2pass)) {
839
166k
                    reset_task_cur_async(ttd, t->frame_idx, c->n_fc);
840
166k
                    signal_if_ready(c, ttd);
841
166k
                    goto found_unlocked;
842
166k
                }
843
162k
                add_pending(f, t, t);
844
162k
                pthread_mutex_lock(&ttd->lock);
845
162k
            } else {
846
68.3k
                if (f->frame_hdr->refresh_context &&
847
19.1k
                    tc->frame_thread.pass <= 1 &&
848
9.63k
                    f->frame_hdr->tiling.update == tile_idx)
849
8.81k
                {
850
8.81k
                    error = atomic_load_explicit(&f->task_thread.error, memory_order_relaxed);
851
8.81k
                    if (!error)
852
7.00k
                        dav1d_cdf_thread_update(f->frame_hdr, f->out_cdf.data.cdf,
853
7.00k
                                                &f->ts[f->frame_hdr->tiling.update].cdf);
854
8.81k
                    if (c->n_fc > 1)
855
8.81k
                        atomic_store_explicit(f->out_cdf.progress, error ? TILE_ERROR : 1,
856
8.81k
                                              memory_order_release);
857
8.81k
                }
858
68.3k
                pthread_mutex_lock(&ttd->lock);
859
68.3k
                atomic_store_explicit(&ts->progress[p], progress, memory_order_relaxed);
860
68.3k
                reset_task_cur(c, ttd, t->frame_idx);
861
68.3k
                complete_task(f);
862
68.3k
                assert(f->task_thread.task_counter >= 0);
863
68.3k
            }
864
230k
            continue;
865
397k
        }
866
230k
        case DAV1D_TASK_TYPE_DEBLOCK_COLS:
867
99.4k
            if (!atomic_load_explicit(&f->task_thread.error, memory_order_relaxed))
868
61.5k
                f->bd_fn.filter_sbrow_deblock_cols(f, sby);
869
99.4k
            if (ensure_progress(ttd, f, t, DAV1D_TASK_TYPE_DEBLOCK_ROWS,
870
99.4k
                                &f->frame_thread.deblock_progress,
871
99.4k
                                &t->deblock_progress)) continue;
872
            // fall-through
873
123k
        case DAV1D_TASK_TYPE_DEBLOCK_ROWS:
874
123k
            if (!atomic_load_explicit(&f->task_thread.error, memory_order_relaxed))
875
76.7k
                f->bd_fn.filter_sbrow_deblock_rows(f, sby);
876
            // signal deblock progress
877
123k
            if (f->frame_hdr->loopfilter.level_y[0] ||
878
32.2k
                f->frame_hdr->loopfilter.level_y[1])
879
99.4k
            {
880
99.4k
                error = atomic_load_explicit(&f->task_thread.error, memory_order_relaxed);
881
99.4k
                atomic_store_explicit(&f->frame_thread.deblock_progress,
882
99.4k
                                      error ? TILE_ERROR : sby + 1, memory_order_release);
883
99.4k
                reset_task_cur_async(ttd, t->frame_idx, c->n_fc);
884
99.4k
                signal_if_ready(c, ttd);
885
99.4k
            } else if (f->lf.cdef_planes || f->lf.restore_planes) {
886
23.9k
                atomic_fetch_or_explicit(&f->frame_thread.copy_lpf_progress[sby >> 5],
887
23.9k
                                         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
23.9k
                if (sby) {
891
21.7k
                    int prog =
892
21.7k
                        atomic_load_explicit(&f->frame_thread.copy_lpf_progress[(sby - 1) >> 5],
893
21.7k
                                             memory_order_acquire);
894
21.7k
                    if (~prog & (1U << ((sby - 1) & 31))) {
895
1.68k
                        t->type = DAV1D_TASK_TYPE_CDEF;
896
1.68k
                        t->recon_progress = t->deblock_progress = 0;
897
1.68k
                        add_pending(f, t, t);
898
1.68k
                        pthread_mutex_lock(&ttd->lock);
899
1.68k
                        continue;
900
1.68k
                    }
901
21.7k
                }
902
23.9k
            }
903
            // fall-through
904
123k
        case DAV1D_TASK_TYPE_CDEF:
905
123k
            if (f->lf.cdef_planes &&
906
62.7k
                !atomic_load_explicit(&f->task_thread.error, memory_order_relaxed))
907
41.0k
            {
908
41.0k
                f->bd_fn.filter_sbrow_cdef(tc, sby);
909
41.0k
            }
910
            // fall-through
911
124k
        case DAV1D_TASK_TYPE_SUPER_RESOLUTION:
912
124k
            if (f->frame_hdr->width[0] != f->frame_hdr->width[1])
913
9.98k
                if (!atomic_load_explicit(&f->task_thread.error, memory_order_relaxed))
914
8.77k
                    f->bd_fn.filter_sbrow_resize(f, sby);
915
            // fall-through
916
124k
        case DAV1D_TASK_TYPE_LOOP_RESTORATION:
917
124k
            if (!atomic_load_explicit(&f->task_thread.error, memory_order_relaxed) &&
918
77.3k
                f->lf.restore_planes)
919
26.9k
            {
920
26.9k
                f->bd_fn.filter_sbrow_lr(f, sby);
921
26.9k
            }
922
            // fall-through
923
194k
        case DAV1D_TASK_TYPE_RECONSTRUCTION_PROGRESS:
924
            // dummy to cover for no post-filters
925
388k
        case DAV1D_TASK_TYPE_ENTROPY_PROGRESS:
926
            // dummy to convert tile progress to frame
927
388k
            break;
928
0
        default: abort();
929
865k
        }
930
        // if task completed [typically LR], signal picture progress as per below
931
387k
        const int sbh = f->sbh;
932
387k
        const int sbsz = f->sb_step * 4;
933
387k
        if (t->type == DAV1D_TASK_TYPE_ENTROPY_PROGRESS) {
934
194k
            error = atomic_load_explicit(&f->task_thread.error, memory_order_relaxed);
935
194k
            const unsigned y = sby + 1 == sbh ? UINT_MAX : (unsigned)(sby + 1) * sbsz;
936
194k
            assert(c->n_fc > 1);
937
194k
            atomic_store_explicit(&f->sr_cur.progress[0], error ? FRAME_ERROR : y,
938
194k
                                  memory_order_release);
939
194k
            atomic_store_explicit(&f->frame_thread.entropy_progress,
940
194k
                                  error ? TILE_ERROR : sby + 1, memory_order_release);
941
194k
            pthread_mutex_lock(&ttd->lock);
942
194k
            if (sby + 1 == sbh)
943
29.6k
                f->task_thread.done[1] = 1;
944
194k
            complete_task(f);
945
194k
            reset_task_cur(c, ttd, t->frame_idx);
946
194k
            continue;
947
194k
        }
948
    // t->type != DAV1D_TASK_TYPE_ENTROPY_PROGRESS
949
387k
        atomic_fetch_or_explicit(&f->frame_thread.frame_progress[sby >> 5],
950
193k
                                 1U << (sby & 31), memory_order_release);
951
193k
        pthread_mutex_lock(&ttd->lock);
952
193k
        sby = get_frame_progress(c, f);
953
193k
        error = atomic_load_explicit(&f->task_thread.error, memory_order_relaxed);
954
193k
        const unsigned y = sby + 1 == sbh ? UINT_MAX : (unsigned)(sby + 1) * sbsz;
955
193k
        if (c->n_fc > 1)
956
194k
            atomic_store_explicit(&f->sr_cur.progress[1], error ? FRAME_ERROR : y,
957
194k
                                  memory_order_relaxed);
958
193k
        if (sby + 1 == sbh)
959
100k
            f->task_thread.done[0] = 1;
960
193k
        complete_task(f);
961
193k
        reset_task_cur(c, ttd, t->frame_idx);
962
193k
    }
963
682k
    pthread_mutex_unlock(&ttd->lock);
964
965
    return NULL;
966
683k
}