Coverage Report

Created: 2026-09-14 06:39

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
6.89M
{
51
6.89M
    const unsigned first = atomic_load(&ttd->first);
52
6.89M
    unsigned reset_frame_idx = atomic_exchange(&ttd->reset_task_cur, UINT_MAX);
53
6.89M
    if (reset_frame_idx < first) {
54
0
        if (frame_idx == UINT_MAX) return 0;
55
0
        reset_frame_idx = UINT_MAX;
56
0
    }
57
6.89M
    if (!ttd->cur && c->fc[first].task_thread.task_cur_prev == NULL)
58
1.01M
        return 0;
59
5.87M
    if (reset_frame_idx != UINT_MAX) {
60
253k
        if (frame_idx == UINT_MAX) {
61
134k
            if (reset_frame_idx > first + ttd->cur)
62
1.97k
                return 0;
63
132k
            ttd->cur = reset_frame_idx - first;
64
132k
            goto cur_found;
65
134k
        }
66
5.62M
    } else if (frame_idx == UINT_MAX)
67
4.25M
        return 0;
68
1.48M
    if (frame_idx < first) frame_idx += c->n_fc;
69
1.48M
    const unsigned min_frame_idx = umin(reset_frame_idx, frame_idx);
70
1.48M
    const unsigned cur_frame_idx = first + ttd->cur;
71
1.48M
    if (ttd->cur < c->n_fc && cur_frame_idx < min_frame_idx)
72
213k
        return 0;
73
1.87M
    for (ttd->cur = min_frame_idx - first; ttd->cur < c->n_fc; ttd->cur++)
74
1.79M
        if (c->fc[(first + ttd->cur) % c->n_fc].task_thread.task_head)
75
1.19M
            break;
76
1.40M
cur_found:
77
6.33M
    for (unsigned i = ttd->cur; i < c->n_fc; i++)
78
4.92M
        c->fc[(first + i) % c->n_fc].task_thread.task_cur_prev = NULL;
79
1.40M
    return 1;
80
1.27M
}
81
82
static inline void reset_task_cur_async(struct TaskThreadData *const ttd,
83
                                        unsigned frame_idx, unsigned n_frames)
84
564k
{
85
564k
    const unsigned first = atomic_load(&ttd->first);
86
564k
    if (frame_idx < first) frame_idx += n_frames;
87
564k
    unsigned last_idx = frame_idx;
88
568k
    do {
89
568k
        frame_idx = last_idx;
90
568k
        last_idx = atomic_exchange(&ttd->reset_task_cur, frame_idx);
91
568k
    } while (last_idx < frame_idx);
92
564k
    if (frame_idx == first && atomic_load(&ttd->first) != first) {
93
0
        unsigned expected = frame_idx;
94
0
        atomic_compare_exchange_strong(&ttd->reset_task_cur, &expected, UINT_MAX);
95
0
    }
96
564k
}
97
98
static void insert_tasks_between(Dav1dFrameContext *const f,
99
                                 Dav1dTask *const first, Dav1dTask *const last,
100
                                 Dav1dTask *const a, Dav1dTask *const b,
101
                                 const int cond_signal)
102
1.31M
{
103
1.31M
    struct TaskThreadData *const ttd = f->task_thread.ttd;
104
1.31M
    if (atomic_load(f->c->flush)) return;
105
1.31M
    assert(!a || a->next == b);
106
1.31M
    if (!a) f->task_thread.task_head = first;
107
906k
    else a->next = first;
108
1.31M
    if (!b) f->task_thread.task_tail = last;
109
1.31M
    last->next = b;
110
1.31M
    reset_task_cur(f->c, ttd, first->frame_idx);
111
1.31M
    if (cond_signal && !atomic_fetch_or(&ttd->cond_signaled, 1))
112
72.1k
        pthread_cond_signal(&ttd->cond);
113
1.31M
}
114
115
static void insert_tasks(Dav1dFrameContext *const f,
116
                         Dav1dTask *const first, Dav1dTask *const last,
117
                         const int cond_signal)
118
1.31M
{
119
    // insert task back into task queue
120
1.31M
    Dav1dTask *t_ptr, *prev_t = NULL;
121
1.31M
    for (t_ptr = f->task_thread.task_head;
122
3.32M
         t_ptr; prev_t = t_ptr, t_ptr = t_ptr->next)
123
2.28M
    {
124
        // entropy coding precedes other steps
125
2.28M
        if (t_ptr->type == DAV1D_TASK_TYPE_TILE_ENTROPY) {
126
535k
            if (first->type > DAV1D_TASK_TYPE_TILE_ENTROPY) continue;
127
            // both are entropy
128
44.9k
            if (first->sby > t_ptr->sby) continue;
129
17.4k
            if (first->sby < t_ptr->sby) {
130
447
                insert_tasks_between(f, first, last, prev_t, t_ptr, cond_signal);
131
447
                return;
132
447
            }
133
            // same sby
134
1.74M
        } else {
135
1.74M
            if (first->type == DAV1D_TASK_TYPE_TILE_ENTROPY) {
136
138k
                insert_tasks_between(f, first, last, prev_t, t_ptr, cond_signal);
137
138k
                return;
138
138k
            }
139
1.60M
            if (first->sby > t_ptr->sby) continue;
140
721k
            if (first->sby < t_ptr->sby) {
141
124k
                insert_tasks_between(f, first, last, prev_t, t_ptr, cond_signal);
142
124k
                return;
143
124k
            }
144
            // same sby
145
597k
            if (first->type > t_ptr->type) continue;
146
20.4k
            if (first->type < t_ptr->type) {
147
3.51k
                insert_tasks_between(f, first, last, prev_t, t_ptr, cond_signal);
148
3.51k
                return;
149
3.51k
            }
150
            // same task type
151
20.4k
        }
152
153
        // sort by tile-id
154
33.9k
        assert(first->type == DAV1D_TASK_TYPE_TILE_RECONSTRUCTION ||
155
33.9k
               first->type == DAV1D_TASK_TYPE_TILE_ENTROPY);
156
33.9k
        assert(first->type == t_ptr->type);
157
33.9k
        assert(t_ptr->sby == first->sby);
158
33.9k
        const int p = first->type == DAV1D_TASK_TYPE_TILE_ENTROPY;
159
33.9k
        const int t_tile_idx = (int) (first - f->task_thread.tile_tasks[p]);
160
33.9k
        const int p_tile_idx = (int) (t_ptr - f->task_thread.tile_tasks[p]);
161
33.9k
        assert(t_tile_idx != p_tile_idx);
162
33.9k
        if (t_tile_idx > p_tile_idx) continue;
163
23
        insert_tasks_between(f, first, last, prev_t, t_ptr, cond_signal);
164
23
        return;
165
33.9k
    }
166
    // append at the end
167
1.04M
    insert_tasks_between(f, first, last, prev_t, NULL, cond_signal);
168
1.04M
}
169
170
static inline void insert_task(Dav1dFrameContext *const f,
171
                               Dav1dTask *const t, const int cond_signal)
172
1.31M
{
173
1.31M
    insert_tasks(f, t, t, cond_signal);
174
1.31M
}
175
176
289k
static inline void add_pending(Dav1dFrameContext *const f, Dav1dTask *const t) {
177
289k
    pthread_mutex_lock(&f->task_thread.pending_tasks.lock);
178
289k
    t->next = NULL;
179
289k
    if (!f->task_thread.pending_tasks.head)
180
283k
        f->task_thread.pending_tasks.head = t;
181
5.83k
    else
182
5.83k
        f->task_thread.pending_tasks.tail->next = t;
183
289k
    f->task_thread.pending_tasks.tail = t;
184
289k
    atomic_store(&f->task_thread.pending_tasks.merge, 1);
185
289k
    pthread_mutex_unlock(&f->task_thread.pending_tasks.lock);
186
289k
}
187
188
39.9M
static inline int merge_pending_frame(Dav1dFrameContext *const f) {
189
39.9M
    int const merge = atomic_load(&f->task_thread.pending_tasks.merge);
190
39.9M
    if (merge) {
191
402k
        pthread_mutex_lock(&f->task_thread.pending_tasks.lock);
192
402k
        Dav1dTask *t = f->task_thread.pending_tasks.head;
193
402k
        f->task_thread.pending_tasks.head = NULL;
194
402k
        f->task_thread.pending_tasks.tail = NULL;
195
402k
        atomic_store(&f->task_thread.pending_tasks.merge, 0);
196
402k
        pthread_mutex_unlock(&f->task_thread.pending_tasks.lock);
197
1.17M
        while (t) {
198
775k
            Dav1dTask *const tmp = t->next;
199
775k
            insert_task(f, t, 0);
200
775k
            t = tmp;
201
775k
        }
202
402k
    }
203
39.9M
    return merge;
204
39.9M
}
205
206
5.97M
static inline int merge_pending(const Dav1dContext *const c) {
207
5.97M
    int res = 0;
208
41.8M
    for (unsigned i = 0; i < c->n_fc; i++)
209
35.8M
        res |= merge_pending_frame(&c->fc[i]);
210
5.97M
    return res;
211
5.97M
}
212
213
static int create_filter_sbrow(Dav1dFrameContext *const f,
214
                               const int pass, Dav1dTask **res_t)
215
238k
{
216
238k
    const int has_deblock = f->frame_hdr->loopfilter.level_y[0] ||
217
31.0k
                            f->frame_hdr->loopfilter.level_y[1];
218
238k
    const int has_cdef = f->seq_hdr->cdef;
219
238k
    const int has_resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
220
238k
    const int has_lr = f->lf.restore_planes;
221
222
238k
    Dav1dTask *tasks = f->task_thread.tasks;
223
238k
    const int uses_2pass = f->c->n_fc > 1;
224
238k
    int num_tasks = f->sbh * (1 + uses_2pass);
225
238k
    if (num_tasks > f->task_thread.num_tasks) {
226
44.3k
        const size_t size = sizeof(Dav1dTask) * num_tasks;
227
44.3k
        tasks = dav1d_realloc(ALLOC_COMMON_CTX, f->task_thread.tasks, size);
228
44.3k
        if (!tasks) return -1;
229
44.3k
        memset(tasks, 0, size);
230
44.3k
        f->task_thread.tasks = tasks;
231
44.3k
        f->task_thread.num_tasks = num_tasks;
232
44.3k
    }
233
238k
    tasks += f->sbh * (pass & 1);
234
235
238k
    if (pass & 1) {
236
119k
        f->frame_thread.entropy_progress = 0;
237
119k
    } else {
238
119k
        const int prog_sz = ((f->sbh + 31) & ~31) >> 5;
239
119k
        if (prog_sz > f->frame_thread.prog_sz) {
240
44.0k
            atomic_uint *const prog = dav1d_realloc(ALLOC_COMMON_CTX, f->frame_thread.frame_progress,
241
44.0k
                                                    2 * prog_sz * sizeof(*prog));
242
44.0k
            if (!prog) return -1;
243
44.0k
            f->frame_thread.frame_progress = prog;
244
44.0k
            f->frame_thread.copy_lpf_progress = prog + prog_sz;
245
44.0k
        }
246
119k
        f->frame_thread.prog_sz = prog_sz;
247
119k
        memset(f->frame_thread.frame_progress, 0, prog_sz * sizeof(atomic_uint));
248
119k
        memset(f->frame_thread.copy_lpf_progress, 0, prog_sz * sizeof(atomic_uint));
249
119k
        atomic_store(&f->frame_thread.deblock_progress, 0);
250
119k
    }
251
238k
    f->frame_thread.next_tile_row[pass & 1] = 0;
252
253
238k
    Dav1dTask *t = &tasks[0];
254
238k
    t->sby = 0;
255
238k
    t->recon_progress = 1;
256
238k
    t->deblock_progress = 0;
257
238k
    t->type = pass == 1 ? DAV1D_TASK_TYPE_ENTROPY_PROGRESS :
258
238k
              has_deblock ? DAV1D_TASK_TYPE_DEBLOCK_COLS :
259
119k
              has_cdef || has_lr /* i.e. LR backup */ ? DAV1D_TASK_TYPE_DEBLOCK_ROWS :
260
12.5k
              has_resize ? DAV1D_TASK_TYPE_SUPER_RESOLUTION :
261
7.12k
              DAV1D_TASK_TYPE_RECONSTRUCTION_PROGRESS;
262
238k
    t->frame_idx = (int)(f - f->c->fc);
263
264
238k
    *res_t = t;
265
238k
    return 0;
266
238k
}
267
268
int dav1d_task_create_tile_sbrow(Dav1dFrameContext *const f, const int pass,
269
                                 const int cond_signal)
270
238k
{
271
238k
    Dav1dTask *tasks = f->task_thread.tile_tasks[0];
272
238k
    const int uses_2pass = f->c->n_fc > 1;
273
238k
    const int n_tasks_per_pass = f->frame_hdr->tiling.cols * f->frame_hdr->tiling.rows;
274
238k
    const int n_tasks = n_tasks_per_pass * (1 + uses_2pass);
275
238k
    if (pass < 2) {
276
119k
        if (n_tasks > f->task_thread.num_tile_tasks) {
277
44.0k
            const size_t size = sizeof(Dav1dTask) * n_tasks;
278
44.0k
            tasks = dav1d_realloc(ALLOC_COMMON_CTX, f->task_thread.tile_tasks[0], size);
279
44.0k
            if (!tasks) return -1;
280
44.0k
            memset(tasks, 0, size);
281
44.0k
            f->task_thread.tile_tasks[0] = tasks;
282
44.0k
            f->task_thread.num_tile_tasks = n_tasks;
283
44.0k
        }
284
119k
        f->task_thread.tile_tasks[1] = tasks + n_tasks_per_pass;
285
119k
    }
286
238k
    assert(n_tasks <= f->task_thread.num_tile_tasks);
287
288
238k
    Dav1dTask *pf_t;
289
238k
    if (create_filter_sbrow(f, pass, &pf_t))
290
0
        return -1;
291
292
238k
    Dav1dTask *const p1_tasks = f->task_thread.tile_tasks[1];
293
238k
    Dav1dTask *prev_t = NULL;
294
238k
    if (pass == 2) {
295
119k
        prev_t = &p1_tasks[n_tasks_per_pass - 1];
296
        // PF task is scheduled after the last sby=0 TILE task
297
119k
        if (f->frame_hdr->tiling.rows == 1)
298
118k
            prev_t = prev_t->next;
299
119k
    }
300
238k
    tasks += (pass & 1) * n_tasks_per_pass;
301
487k
    for (int tile_idx = 0; tile_idx < n_tasks_per_pass; tile_idx++) {
302
248k
        Dav1dTileState *const ts = &f->ts[tile_idx];
303
248k
        Dav1dTask *t = &tasks[tile_idx];
304
248k
        t->sby = ts->tiling.row_start >> f->sb_shift;
305
248k
        if (pf_t && t->sby) {
306
1.82k
            prev_t->next = pf_t;
307
1.82k
            prev_t = pf_t;
308
1.82k
            pf_t = NULL;
309
1.82k
        }
310
248k
        t->recon_progress = 0;
311
248k
        t->deblock_progress = 0;
312
248k
        t->deps_skip = 0;
313
248k
        t->type = pass != 1 ? DAV1D_TASK_TYPE_TILE_RECONSTRUCTION :
314
248k
                              DAV1D_TASK_TYPE_TILE_ENTROPY;
315
248k
        t->frame_idx = (int)(f - f->c->fc);
316
248k
        if (prev_t) prev_t->next = t;
317
248k
        prev_t = t;
318
248k
    }
319
238k
    if (pf_t) {
320
236k
        prev_t->next = pf_t;
321
236k
        prev_t = pf_t;
322
236k
    }
323
238k
    prev_t->next = NULL;
324
325
238k
    atomic_store(&f->task_thread.done[pass & 1], 0);
326
327
    // XXX in theory this could be done locklessly, at this point they are no
328
    // tasks in the frameQ, so no other runner should be using this lock, but
329
    // we must add both passes at once
330
238k
    if (!(pass & 1)) {
331
119k
        pthread_mutex_lock(&f->task_thread.pending_tasks.lock);
332
119k
        assert(f->task_thread.pending_tasks.head == NULL);
333
119k
        f->task_thread.pending_tasks.head = f->task_thread.tile_tasks[pass == 2];
334
119k
        f->task_thread.pending_tasks.tail = prev_t;
335
119k
        atomic_store(&f->task_thread.pending_tasks.merge, 1);
336
119k
        atomic_store(&f->task_thread.init_done, 1);
337
119k
        pthread_mutex_unlock(&f->task_thread.pending_tasks.lock);
338
119k
    }
339
238k
    return 0;
340
238k
}
341
342
122k
void dav1d_task_frame_init(Dav1dFrameContext *const f) {
343
122k
    const Dav1dContext *const c = f->c;
344
345
122k
    atomic_store(&f->task_thread.init_done, 0);
346
    // schedule init task, which will schedule the remaining tasks
347
122k
    Dav1dTask *const t = &f->task_thread.init_task;
348
122k
    t->type = DAV1D_TASK_TYPE_INIT;
349
122k
    t->frame_idx = (int)(f - c->fc);
350
122k
    t->sby = 0;
351
122k
    t->recon_progress = t->deblock_progress = 0;
352
122k
    insert_task(f, t, 1);
353
122k
}
354
355
void dav1d_task_delayed_fg(Dav1dContext *const c, Dav1dPicture *const out,
356
                           const Dav1dPicture *const in)
357
1.07k
{
358
1.07k
    struct TaskThreadData *const ttd = &c->task_thread;
359
1.07k
    ttd->delayed_fg.in = in;
360
1.07k
    ttd->delayed_fg.out = out;
361
1.07k
    ttd->delayed_fg.type = DAV1D_TASK_TYPE_FG_PREP;
362
1.07k
    atomic_init(&ttd->delayed_fg.progress[0], 0);
363
1.07k
    atomic_init(&ttd->delayed_fg.progress[1], 0);
364
1.07k
    pthread_mutex_lock(&ttd->lock);
365
1.07k
    ttd->delayed_fg.exec = 1;
366
1.07k
    ttd->delayed_fg.finished = 0;
367
1.07k
    pthread_cond_signal(&ttd->cond);
368
1.07k
    do {
369
1.07k
        pthread_cond_wait(&ttd->delayed_fg.cond, &ttd->lock);
370
1.07k
    } while (!ttd->delayed_fg.finished);
371
1.07k
    pthread_mutex_unlock(&ttd->lock);
372
1.07k
}
373
374
static inline int ensure_progress(struct TaskThreadData *const ttd,
375
                                  Dav1dFrameContext *const f,
376
                                  Dav1dTask *const t, const enum TaskType type,
377
                                  atomic_int *const state, int *const target)
378
228k
{
379
    // deblock_rows (non-LR portion) depends on deblock of previous sbrow,
380
    // so ensure that completed. if not, re-add to task-queue; else, fall-through
381
228k
    int p1 = atomic_load(state);
382
228k
    if (p1 < t->sby) {
383
2.08k
        t->type = type;
384
2.08k
        t->recon_progress = t->deblock_progress = 0;
385
2.08k
        *target = t->sby;
386
2.08k
        add_pending(f, t);
387
2.08k
        pthread_mutex_lock(&ttd->lock);
388
2.08k
        return 1;
389
2.08k
    }
390
226k
    return 0;
391
228k
}
392
393
static inline int check_tile(Dav1dTask *const t, Dav1dFrameContext *const f,
394
                             const int frame_mt)
395
2.09M
{
396
2.09M
    const int tp = t->type == DAV1D_TASK_TYPE_TILE_ENTROPY;
397
2.09M
    const int tile_idx = (int)(t - f->task_thread.tile_tasks[tp]);
398
2.09M
    Dav1dTileState *const ts = &f->ts[tile_idx];
399
2.09M
    const int p1 = atomic_load(&ts->progress[tp]);
400
2.09M
    if (p1 < t->sby) return 1;
401
1.83M
    int error = p1 == TILE_ERROR;
402
1.83M
    error |= atomic_fetch_or(&f->task_thread.error, error);
403
1.83M
    if (!error && frame_mt && !tp) {
404
1.17M
        const int p2 = atomic_load(&ts->progress[1]);
405
1.17M
        if (p2 <= t->sby) return 1;
406
812k
        error = p2 == TILE_ERROR;
407
812k
        error |= atomic_fetch_or(&f->task_thread.error, error);
408
812k
    }
409
1.47M
    if (!error && frame_mt && !IS_KEY_OR_INTRA(f->frame_hdr)) {
410
        // check reference state
411
1.10M
        const Dav1dThreadPicture *p = &f->sr_cur;
412
1.10M
        const int ss_ver = p->p.p.layout == DAV1D_PIXEL_LAYOUT_I420;
413
1.10M
        const unsigned p_b = (t->sby + 1) << (f->sb_shift + 2);
414
1.10M
        const int tile_sby = t->sby - (ts->tiling.row_start >> f->sb_shift);
415
1.10M
        const int (*const lowest_px)[2] = ts->lowest_pixel[tile_sby];
416
3.14M
        for (int n = t->deps_skip; n < 7; n++, t->deps_skip++) {
417
2.85M
            unsigned lowest;
418
2.85M
            if (tp) {
419
                // if temporal mv refs are disabled, we only need this
420
                // for the primary ref; if segmentation is disabled, we
421
                // don't even need that
422
1.27M
                lowest = p_b;
423
1.58M
            } else {
424
                // +8 is postfilter-induced delay
425
1.58M
                const int y = lowest_px[n][0] == INT_MIN ? INT_MIN :
426
1.58M
                              lowest_px[n][0] + 8;
427
1.58M
                const int uv = lowest_px[n][1] == INT_MIN ? INT_MIN :
428
1.58M
                               lowest_px[n][1] * (1 << ss_ver) + 8;
429
1.58M
                const int max = imax(y, uv);
430
1.58M
                if (max == INT_MIN) continue;
431
783k
                lowest = iclip(max, 1, f->refp[n].p.p.h);
432
783k
            }
433
2.05M
            const unsigned p3 = atomic_load(&f->refp[n].progress[!tp]);
434
2.05M
            if (p3 < lowest) return 1;
435
2.05M
            atomic_fetch_or(&f->task_thread.error, p3 == FRAME_ERROR);
436
1.24M
        }
437
1.10M
    }
438
663k
    return 0;
439
1.47M
}
440
441
static inline int get_frame_progress(const Dav1dContext *const c,
442
                                     const Dav1dFrameContext *const f)
443
321k
{
444
321k
    unsigned frame_prog = c->n_fc > 1 ? atomic_load(&f->sr_cur.progress[1]) : 0;
445
321k
    if (frame_prog >= FRAME_ERROR)
446
87.3k
        return f->sbh - 1;
447
234k
    int idx = frame_prog >> (f->sb_shift + 7);
448
234k
    int prog;
449
235k
    do {
450
235k
        atomic_uint *state = &f->frame_thread.frame_progress[idx];
451
235k
        const unsigned val = ~atomic_load(state);
452
235k
        prog = val ? ctz(val) : 32;
453
235k
        if (prog != 32) break;
454
1.12k
        prog = 0;
455
1.12k
    } while (++idx < f->frame_thread.prog_sz);
456
234k
    return ((idx << 5) | prog) - 1;
457
321k
}
458
459
2.67k
static inline void abort_frame(Dav1dFrameContext *const f, const int error) {
460
2.67k
    atomic_store(&f->task_thread.error, error == DAV1D_ERR(EINVAL) ? 1 : -1);
461
2.67k
    atomic_store(&f->task_thread.task_counter, 0);
462
2.67k
    atomic_store(&f->task_thread.done[0], 1);
463
2.67k
    atomic_store(&f->task_thread.done[1], 1);
464
2.67k
    atomic_store(&f->sr_cur.progress[0], FRAME_ERROR);
465
2.67k
    atomic_store(&f->sr_cur.progress[1], FRAME_ERROR);
466
2.67k
    dav1d_decode_frame_exit(f, error);
467
2.67k
    f->n_tile_data = 0;
468
2.67k
    pthread_cond_signal(&f->task_thread.cond);
469
2.67k
}
470
471
static inline void delayed_fg_task(const Dav1dContext *const c,
472
                                   struct TaskThreadData *const ttd)
473
5.09k
{
474
5.09k
    const Dav1dPicture *const in = ttd->delayed_fg.in;
475
5.09k
    Dav1dPicture *const out = ttd->delayed_fg.out;
476
5.09k
#if CONFIG_16BPC
477
5.09k
    int off;
478
5.09k
    if (out->p.bpc != 8)
479
2.84k
        off = (out->p.bpc >> 1) - 4;
480
5.09k
#endif
481
5.09k
    switch (ttd->delayed_fg.type) {
482
1.07k
    case DAV1D_TASK_TYPE_FG_PREP:
483
1.07k
        ttd->delayed_fg.exec = 0;
484
1.07k
        if (atomic_load(&ttd->cond_signaled))
485
7
            pthread_cond_signal(&ttd->cond);
486
1.07k
        pthread_mutex_unlock(&ttd->lock);
487
1.07k
        switch (out->p.bpc) {
488
0
#if CONFIG_8BPC
489
518
        case 8:
490
518
            dav1d_prep_grain_8bpc(&c->dsp[0].fg, out, in,
491
518
                                  ttd->delayed_fg.scaling_8bpc,
492
518
                                  ttd->delayed_fg.grain_lut_8bpc);
493
518
            break;
494
0
#endif
495
0
#if CONFIG_16BPC
496
471
        case 10:
497
559
        case 12:
498
559
            dav1d_prep_grain_16bpc(&c->dsp[off].fg, out, in,
499
559
                                   ttd->delayed_fg.scaling_16bpc,
500
559
                                   ttd->delayed_fg.grain_lut_16bpc);
501
559
            break;
502
0
#endif
503
0
        default: abort();
504
1.07k
        }
505
1.07k
        ttd->delayed_fg.type = DAV1D_TASK_TYPE_FG_APPLY;
506
1.07k
        pthread_mutex_lock(&ttd->lock);
507
1.07k
        ttd->delayed_fg.exec = 1;
508
        // fall-through
509
5.09k
    case DAV1D_TASK_TYPE_FG_APPLY:;
510
5.09k
        int row = atomic_fetch_add(&ttd->delayed_fg.progress[0], 1);
511
5.09k
        pthread_mutex_unlock(&ttd->lock);
512
5.09k
        int progmax = (out->p.h + FG_BLOCK_SIZE - 1) / FG_BLOCK_SIZE;
513
12.1k
        while (row < progmax) {
514
8.77k
            if (row + 1 < progmax)
515
7.70k
                pthread_cond_signal(&ttd->cond);
516
1.07k
            else {
517
1.07k
                pthread_mutex_lock(&ttd->lock);
518
1.07k
                ttd->delayed_fg.exec = 0;
519
1.07k
                pthread_mutex_unlock(&ttd->lock);
520
1.07k
            }
521
8.77k
            switch (out->p.bpc) {
522
0
#if CONFIG_8BPC
523
4.15k
            case 8:
524
4.15k
                dav1d_apply_grain_row_8bpc(&c->dsp[0].fg, out, in,
525
4.15k
                                           ttd->delayed_fg.scaling_8bpc,
526
4.15k
                                           ttd->delayed_fg.grain_lut_8bpc, row);
527
4.15k
                break;
528
0
#endif
529
0
#if CONFIG_16BPC
530
3.74k
            case 10:
531
4.64k
            case 12:
532
4.64k
                dav1d_apply_grain_row_16bpc(&c->dsp[off].fg, out, in,
533
4.64k
                                            ttd->delayed_fg.scaling_16bpc,
534
4.64k
                                            ttd->delayed_fg.grain_lut_16bpc, row);
535
4.64k
                break;
536
0
#endif
537
0
            default: abort();
538
8.77k
            }
539
7.06k
            row = atomic_fetch_add(&ttd->delayed_fg.progress[0], 1);
540
7.06k
            atomic_fetch_add(&ttd->delayed_fg.progress[1], 1);
541
7.06k
        }
542
3.38k
        pthread_mutex_lock(&ttd->lock);
543
3.38k
        ttd->delayed_fg.exec = 0;
544
3.38k
        int done = atomic_fetch_add(&ttd->delayed_fg.progress[1], 1) + 1;
545
3.38k
        progmax = atomic_load(&ttd->delayed_fg.progress[0]);
546
        // signal for completion only once the last runner reaches this
547
3.38k
        if (done >= progmax) {
548
1.07k
            ttd->delayed_fg.finished = 1;
549
1.07k
            pthread_cond_signal(&ttd->delayed_fg.cond);
550
1.07k
        }
551
3.38k
        break;
552
0
    default: abort();
553
5.09k
    }
554
5.09k
}
555
556
939k
void *dav1d_worker_task(void *data) {
557
939k
    Dav1dTaskContext *const tc = data;
558
939k
    const Dav1dContext *const c = tc->c;
559
939k
    struct TaskThreadData *const ttd = tc->task_thread.ttd;
560
561
939k
    dav1d_set_thread_name("dav1d-worker");
562
563
939k
    pthread_mutex_lock(&ttd->lock);
564
4.61M
    for (;;) {
565
4.61M
        if (tc->task_thread.die) break;
566
3.67M
        if (atomic_load(c->flush)) goto park;
567
568
3.65M
        merge_pending(c);
569
3.65M
        if (ttd->delayed_fg.exec) { // run delayed film grain first
570
5.09k
            delayed_fg_task(c, ttd);
571
5.09k
            continue;
572
5.09k
        }
573
3.64M
        Dav1dFrameContext *f;
574
3.64M
        Dav1dTask *t, *prev_t = NULL;
575
3.64M
        if (c->n_fc > 1) { // run init tasks second
576
25.1M
            for (unsigned i = 0; i < c->n_fc; i++) {
577
21.6M
                const unsigned first = atomic_load(&ttd->first);
578
21.6M
                f = &c->fc[(first + i) % c->n_fc];
579
21.6M
                if (atomic_load(&f->task_thread.init_done)) continue;
580
11.7M
                t = f->task_thread.task_head;
581
11.7M
                if (!t) continue;
582
498k
                if (t->type == DAV1D_TASK_TYPE_INIT) goto found;
583
375k
                if (t->type == DAV1D_TASK_TYPE_INIT_CDF) {
584
                    // XXX This can be a simple else, if adding tasks of both
585
                    // passes at once (in dav1d_task_create_tile_sbrow).
586
                    // Adding the tasks to the pending Q can result in a
587
                    // thread merging them before setting init_done.
588
                    // We will need to set init_done before adding to the
589
                    // pending Q, so maybe return the tasks, set init_done,
590
                    // and add to pending Q only then.
591
375k
                    const int p1 = f->in_cdf.progress ?
592
375k
                        atomic_load(f->in_cdf.progress) : 1;
593
375k
                    if (p1) {
594
19.7k
                        atomic_fetch_or(&f->task_thread.error, p1 == TILE_ERROR);
595
19.7k
                        goto found;
596
19.7k
                    }
597
375k
                }
598
375k
            }
599
3.64M
        }
600
6.38M
        while (ttd->cur < c->n_fc) { // run decoding tasks last
601
4.04M
            const unsigned first = atomic_load(&ttd->first);
602
4.04M
            f = &c->fc[(first + ttd->cur) % c->n_fc];
603
4.04M
            merge_pending_frame(f);
604
4.04M
            prev_t = f->task_thread.task_cur_prev;
605
4.04M
            t = prev_t ? prev_t->next : f->task_thread.task_head;
606
7.36M
            while (t) {
607
4.48M
                if (t->type == DAV1D_TASK_TYPE_INIT_CDF) goto next;
608
4.39M
                else if (t->type == DAV1D_TASK_TYPE_TILE_ENTROPY ||
609
3.90M
                         t->type == DAV1D_TASK_TYPE_TILE_RECONSTRUCTION)
610
1.67M
                {
611
                    // if not bottom sbrow of tile, this task will be re-added
612
                    // after it's finished
613
1.67M
                    if (!check_tile(t, f, c->n_fc > 1))
614
504k
                        goto found;
615
2.71M
                } else if (t->recon_progress) {
616
2.65M
                    const int p = t->type == DAV1D_TASK_TYPE_ENTROPY_PROGRESS;
617
2.65M
                    int error = atomic_load(&f->task_thread.error);
618
2.65M
                    assert(!atomic_load(&f->task_thread.done[p]) || error);
619
2.65M
                    const int tile_row_base = f->frame_hdr->tiling.cols *
620
2.65M
                                              f->frame_thread.next_tile_row[p];
621
2.65M
                    if (p) {
622
896k
                        atomic_int *const prog = &f->frame_thread.entropy_progress;
623
896k
                        const int p1 = atomic_load(prog);
624
896k
                        if (p1 < t->sby) goto next;
625
896k
                        atomic_fetch_or(&f->task_thread.error, p1 == TILE_ERROR);
626
829k
                    }
627
3.25M
                    for (int tc = 0; tc < f->frame_hdr->tiling.cols; tc++) {
628
2.60M
                        Dav1dTileState *const ts = &f->ts[tile_row_base + tc];
629
2.60M
                        const int p2 = atomic_load(&ts->progress[p]);
630
2.60M
                        if (p2 < t->recon_progress) goto next;
631
2.60M
                        atomic_fetch_or(&f->task_thread.error, p2 == TILE_ERROR);
632
666k
                    }
633
646k
                    if (t->sby + 1 < f->sbh) {
634
                        // add sby+1 to list to replace this one
635
413k
                        Dav1dTask *next_t = &t[1];
636
413k
                        *next_t = *t;
637
413k
                        next_t->sby++;
638
413k
                        const int ntr = f->frame_thread.next_tile_row[p] + 1;
639
413k
                        const int start = f->frame_hdr->tiling.row_start_sb[ntr];
640
413k
                        if (next_t->sby == start)
641
4.66k
                            f->frame_thread.next_tile_row[p] = ntr;
642
413k
                        next_t->recon_progress = next_t->sby + 1;
643
413k
                        insert_task(f, next_t, 0);
644
413k
                    }
645
646k
                    goto found;
646
2.58M
                } else if (t->type == DAV1D_TASK_TYPE_CDEF) {
647
37.7k
                    atomic_uint *prog = f->frame_thread.copy_lpf_progress;
648
37.7k
                    const int p1 = atomic_load(&prog[(t->sby - 1) >> 5]);
649
37.7k
                    if (p1 & (1U << ((t->sby - 1) & 31)))
650
6.75k
                        goto found;
651
37.7k
                } else {
652
28.6k
                    assert(t->deblock_progress);
653
28.6k
                    const int p1 = atomic_load(&f->frame_thread.deblock_progress);
654
28.6k
                    if (p1 >= t->deblock_progress) {
655
2.09k
                        atomic_fetch_or(&f->task_thread.error, p1 == TILE_ERROR);
656
2.09k
                        goto found;
657
2.09k
                    }
658
28.6k
                }
659
3.32M
            next:
660
3.32M
                prev_t = t;
661
3.32M
                t = t->next;
662
3.32M
                f->task_thread.task_cur_prev = prev_t;
663
3.32M
            }
664
2.88M
            ttd->cur++;
665
2.88M
        }
666
2.34M
        if (reset_task_cur(c, ttd, UINT_MAX)) continue;
667
2.32M
        if (merge_pending(c)) continue;
668
2.34M
    park:
669
2.34M
        tc->task_thread.flushed = 1;
670
2.34M
        pthread_cond_signal(&tc->task_thread.td.cond);
671
        // we want to be woken up next time progress is signaled
672
2.34M
        atomic_store(&ttd->cond_signaled, 0);
673
2.34M
        pthread_cond_wait(&ttd->cond, &ttd->lock);
674
2.34M
        tc->task_thread.flushed = 0;
675
2.34M
        reset_task_cur(c, ttd, UINT_MAX);
676
2.34M
        continue;
677
678
1.30M
    found:
679
        // remove t from list
680
1.30M
        if (prev_t) prev_t->next = t->next;
681
1.11M
        else f->task_thread.task_head = t->next;
682
1.30M
        if (!t->next) f->task_thread.task_tail = prev_t;
683
1.30M
        if (t->type > DAV1D_TASK_TYPE_INIT_CDF && !f->task_thread.task_head)
684
115k
            ttd->cur++;
685
1.30M
        t->next = NULL;
686
        // we don't need to check cond_signaled here, since we found a task
687
        // after the last signal so we want to re-signal the next waiting thread
688
        // and again won't need to signal after that
689
1.30M
        atomic_store(&ttd->cond_signaled, 1);
690
1.30M
        pthread_cond_signal(&ttd->cond);
691
1.30M
        pthread_mutex_unlock(&ttd->lock);
692
1.56M
    found_unlocked:;
693
1.56M
        const int flush = atomic_load(c->flush);
694
1.56M
        int error = atomic_fetch_or(&f->task_thread.error, flush) | flush;
695
696
        // run it
697
1.56M
        tc->f = f;
698
1.56M
        int sby = t->sby;
699
1.56M
        switch (t->type) {
700
122k
        case DAV1D_TASK_TYPE_INIT: {
701
122k
            assert(c->n_fc > 1);
702
122k
            int res = dav1d_decode_frame_init(f);
703
122k
            int p1 = f->in_cdf.progress ? atomic_load(f->in_cdf.progress) : 1;
704
122k
            if (res || p1 == TILE_ERROR) {
705
193
                pthread_mutex_lock(&ttd->lock);
706
193
                abort_frame(f, res ? res : DAV1D_ERR(EINVAL));
707
193
                reset_task_cur(c, ttd, t->frame_idx);
708
122k
            } else {
709
122k
                t->type = DAV1D_TASK_TYPE_INIT_CDF;
710
122k
                if (p1) goto found_unlocked;
711
20.2k
                add_pending(f, t);
712
20.2k
                pthread_mutex_lock(&ttd->lock);
713
20.2k
            }
714
20.4k
            continue;
715
122k
        }
716
121k
        case DAV1D_TASK_TYPE_INIT_CDF: {
717
121k
            assert(c->n_fc > 1);
718
121k
            int res = DAV1D_ERR(EINVAL);
719
121k
            if (!atomic_load(&f->task_thread.error))
720
119k
                res = dav1d_decode_frame_init_cdf(f);
721
121k
            if (f->frame_hdr->refresh_context && !f->task_thread.update_set)
722
121k
                atomic_store(f->out_cdf.progress, res < 0 ? TILE_ERROR : 1);
723
360k
            for (int p = 1; p <= 2 && !res; p++)
724
238k
                res = dav1d_task_create_tile_sbrow(f, p, 0);
725
121k
            pthread_mutex_lock(&ttd->lock);
726
121k
            if (res) {
727
2.48k
                abort_frame(f, DAV1D_ERR(ENOMEM));
728
2.48k
                reset_task_cur(c, ttd, t->frame_idx);
729
2.48k
                atomic_store(&f->task_thread.init_done, 1);
730
2.48k
            }
731
121k
            continue;
732
121k
        }
733
335k
        case DAV1D_TASK_TYPE_TILE_ENTROPY:
734
666k
        case DAV1D_TASK_TYPE_TILE_RECONSTRUCTION: {
735
666k
            const int p = t->type == DAV1D_TASK_TYPE_TILE_ENTROPY;
736
666k
            const int tile_idx = (int)(t - f->task_thread.tile_tasks[p]);
737
666k
            Dav1dTileState *const ts = &f->ts[tile_idx];
738
739
666k
            tc->ts = ts;
740
666k
            tc->by = sby << f->sb_shift;
741
666k
            const int uses_2pass = c->n_fc > 1;
742
666k
            tc->frame_thread.pass = !uses_2pass ? 0 :
743
666k
                1 + (t->type == DAV1D_TASK_TYPE_TILE_RECONSTRUCTION);
744
666k
            if (!error) error = dav1d_decode_tile_sbrow(tc);
745
666k
            const int progress = error ? TILE_ERROR : 1 + sby;
746
747
            // signal progress
748
666k
            atomic_fetch_or(&f->task_thread.error, error);
749
666k
            if (((sby + 1) << f->sb_shift) < ts->tiling.row_end) {
750
420k
                t->sby++;
751
420k
                t->deps_skip = 0;
752
420k
                if (!check_tile(t, f, uses_2pass)) {
753
161k
                    atomic_store(&ts->progress[p], progress);
754
161k
                    reset_task_cur_async(ttd, t->frame_idx, c->n_fc);
755
161k
                    if (!atomic_fetch_or(&ttd->cond_signaled, 1))
756
95
                        pthread_cond_signal(&ttd->cond);
757
161k
                    goto found_unlocked;
758
161k
                }
759
420k
                atomic_store(&ts->progress[p], progress);
760
259k
                add_pending(f, t);
761
259k
                pthread_mutex_lock(&ttd->lock);
762
259k
            } else {
763
245k
                pthread_mutex_lock(&ttd->lock);
764
245k
                atomic_store(&ts->progress[p], progress);
765
245k
                reset_task_cur(c, ttd, t->frame_idx);
766
245k
                error = atomic_load(&f->task_thread.error);
767
245k
                if (f->frame_hdr->refresh_context &&
768
167k
                    tc->frame_thread.pass <= 1 && f->task_thread.update_set &&
769
84.7k
                    f->frame_hdr->tiling.update == tile_idx)
770
84.1k
                {
771
84.1k
                    if (!error)
772
82.2k
                        dav1d_cdf_thread_update(f->frame_hdr, f->out_cdf.data.cdf,
773
82.2k
                                                &f->ts[f->frame_hdr->tiling.update].cdf);
774
84.1k
                    if (c->n_fc > 1)
775
84.1k
                        atomic_store(f->out_cdf.progress, error ? TILE_ERROR : 1);
776
84.1k
                }
777
245k
                if (atomic_fetch_sub(&f->task_thread.task_counter, 1) - 1 == 0 &&
778
245k
                    atomic_load(&f->task_thread.done[0]) &&
779
702
                    (!uses_2pass || atomic_load(&f->task_thread.done[1])))
780
702
                {
781
702
                    error = atomic_load(&f->task_thread.error);
782
702
                    dav1d_decode_frame_exit(f, error == 1 ? DAV1D_ERR(EINVAL) :
783
702
                                            error ? DAV1D_ERR(ENOMEM) : 0);
784
702
                    f->n_tile_data = 0;
785
702
                    pthread_cond_signal(&f->task_thread.cond);
786
702
                }
787
245k
                assert(atomic_load(&f->task_thread.task_counter) >= 0);
788
245k
                if (!atomic_fetch_or(&ttd->cond_signaled, 1))
789
165k
                    pthread_cond_signal(&ttd->cond);
790
245k
            }
791
504k
            continue;
792
666k
        }
793
504k
        case DAV1D_TASK_TYPE_DEBLOCK_COLS:
794
228k
            if (!atomic_load(&f->task_thread.error))
795
185k
                f->bd_fn.filter_sbrow_deblock_cols(f, sby);
796
228k
            if (ensure_progress(ttd, f, t, DAV1D_TASK_TYPE_DEBLOCK_ROWS,
797
228k
                                &f->frame_thread.deblock_progress,
798
228k
                                &t->deblock_progress)) continue;
799
            // fall-through
800
280k
        case DAV1D_TASK_TYPE_DEBLOCK_ROWS:
801
280k
            if (!atomic_load(&f->task_thread.error))
802
205k
                f->bd_fn.filter_sbrow_deblock_rows(f, sby);
803
            // signal deblock progress
804
280k
            if (f->frame_hdr->loopfilter.level_y[0] ||
805
65.8k
                f->frame_hdr->loopfilter.level_y[1])
806
228k
            {
807
228k
                error = atomic_load(&f->task_thread.error);
808
228k
                atomic_store(&f->frame_thread.deblock_progress,
809
228k
                             error ? TILE_ERROR : sby + 1);
810
228k
                reset_task_cur_async(ttd, t->frame_idx, c->n_fc);
811
228k
                if (!atomic_fetch_or(&ttd->cond_signaled, 1))
812
68.9k
                    pthread_cond_signal(&ttd->cond);
813
228k
            } else if (f->seq_hdr->cdef || f->lf.restore_planes) {
814
51.4k
                atomic_fetch_or(&f->frame_thread.copy_lpf_progress[sby >> 5],
815
51.4k
                                1U << (sby & 31));
816
                // CDEF needs the top buffer to be saved by lr_copy_lpf of the
817
                // previous sbrow
818
51.4k
                if (sby) {
819
46.1k
                    int prog = atomic_load(&f->frame_thread.copy_lpf_progress[(sby - 1) >> 5]);
820
46.1k
                    if (~prog & (1U << ((sby - 1) & 31))) {
821
6.75k
                        t->type = DAV1D_TASK_TYPE_CDEF;
822
6.75k
                        t->recon_progress = t->deblock_progress = 0;
823
6.75k
                        add_pending(f, t);
824
6.75k
                        pthread_mutex_lock(&ttd->lock);
825
6.75k
                        continue;
826
6.75k
                    }
827
46.1k
                }
828
51.4k
            }
829
            // fall-through
830
280k
        case DAV1D_TASK_TYPE_CDEF:
831
280k
            if (f->seq_hdr->cdef) {
832
175k
                if (!atomic_load(&f->task_thread.error))
833
129k
                    f->bd_fn.filter_sbrow_cdef(tc, sby);
834
175k
                reset_task_cur_async(ttd, t->frame_idx, c->n_fc);
835
175k
                if (!atomic_fetch_or(&ttd->cond_signaled, 1))
836
53.0k
                    pthread_cond_signal(&ttd->cond);
837
175k
            }
838
            // fall-through
839
281k
        case DAV1D_TASK_TYPE_SUPER_RESOLUTION:
840
281k
            if (f->frame_hdr->width[0] != f->frame_hdr->width[1])
841
35.3k
                if (!atomic_load(&f->task_thread.error))
842
29.9k
                    f->bd_fn.filter_sbrow_resize(f, sby);
843
            // fall-through
844
281k
        case DAV1D_TASK_TYPE_LOOP_RESTORATION:
845
281k
            if (!atomic_load(&f->task_thread.error) && f->lf.restore_planes)
846
133k
                f->bd_fn.filter_sbrow_lr(f, sby);
847
            // fall-through
848
321k
        case DAV1D_TASK_TYPE_RECONSTRUCTION_PROGRESS:
849
            // dummy to cover for no post-filters
850
645k
        case DAV1D_TASK_TYPE_ENTROPY_PROGRESS:
851
            // dummy to convert tile progress to frame
852
645k
            break;
853
0
        default: abort();
854
1.56M
        }
855
        // if task completed [typically LR], signal picture progress as per below
856
644k
        const int uses_2pass = c->n_fc > 1;
857
644k
        const int sbh = f->sbh;
858
644k
        const int sbsz = f->sb_step * 4;
859
644k
        if (t->type == DAV1D_TASK_TYPE_ENTROPY_PROGRESS) {
860
324k
            error = atomic_load(&f->task_thread.error);
861
324k
            const unsigned y = sby + 1 == sbh ? UINT_MAX : (unsigned)(sby + 1) * sbsz;
862
324k
            assert(c->n_fc > 1);
863
324k
            if (f->sr_cur.p.data[0] /* upon flush, this can be free'ed already */)
864
324k
                atomic_store(&f->sr_cur.progress[0], error ? FRAME_ERROR : y);
865
324k
            atomic_store(&f->frame_thread.entropy_progress,
866
324k
                         error ? TILE_ERROR : sby + 1);
867
324k
            if (sby + 1 == sbh)
868
324k
                atomic_store(&f->task_thread.done[1], 1);
869
324k
            pthread_mutex_lock(&ttd->lock);
870
324k
            const int num_tasks = atomic_fetch_sub(&f->task_thread.task_counter, 1) - 1;
871
324k
            if (sby + 1 < sbh && num_tasks) {
872
205k
                reset_task_cur(c, ttd, t->frame_idx);
873
205k
                continue;
874
205k
            }
875
118k
            if (!num_tasks && atomic_load(&f->task_thread.done[0]) &&
876
118k
                atomic_load(&f->task_thread.done[1]))
877
15.1k
            {
878
15.1k
                error = atomic_load(&f->task_thread.error);
879
15.1k
                dav1d_decode_frame_exit(f, error == 1 ? DAV1D_ERR(EINVAL) :
880
15.1k
                                        error ? DAV1D_ERR(ENOMEM) : 0);
881
15.1k
                f->n_tile_data = 0;
882
15.1k
                pthread_cond_signal(&f->task_thread.cond);
883
15.1k
            }
884
118k
            reset_task_cur(c, ttd, t->frame_idx);
885
118k
            continue;
886
324k
        }
887
    // t->type != DAV1D_TASK_TYPE_ENTROPY_PROGRESS
888
644k
        atomic_fetch_or(&f->frame_thread.frame_progress[sby >> 5],
889
320k
                        1U << (sby & 31));
890
320k
        pthread_mutex_lock(&f->task_thread.lock);
891
320k
        sby = get_frame_progress(c, f);
892
320k
        error = atomic_load(&f->task_thread.error);
893
320k
        const unsigned y = sby + 1 == sbh ? UINT_MAX : (unsigned)(sby + 1) * sbsz;
894
321k
        if (c->n_fc > 1 && f->sr_cur.p.data[0] /* upon flush, this can be free'ed already */)
895
321k
            atomic_store(&f->sr_cur.progress[1], error ? FRAME_ERROR : y);
896
320k
        pthread_mutex_unlock(&f->task_thread.lock);
897
320k
        if (sby + 1 == sbh)
898
320k
            atomic_store(&f->task_thread.done[0], 1);
899
320k
        pthread_mutex_lock(&ttd->lock);
900
320k
        const int num_tasks = atomic_fetch_sub(&f->task_thread.task_counter, 1) - 1;
901
320k
        if (sby + 1 < sbh && num_tasks) {
902
118k
            reset_task_cur(c, ttd, t->frame_idx);
903
118k
            continue;
904
118k
        }
905
202k
        if (!num_tasks && atomic_load(&f->task_thread.done[0]) &&
906
99.4k
            (!uses_2pass || atomic_load(&f->task_thread.done[1])))
907
99.4k
        {
908
99.4k
            error = atomic_load(&f->task_thread.error);
909
99.4k
            dav1d_decode_frame_exit(f, error == 1 ? DAV1D_ERR(EINVAL) :
910
99.4k
                                    error ? DAV1D_ERR(ENOMEM) : 0);
911
99.4k
            f->n_tile_data = 0;
912
99.4k
            pthread_cond_signal(&f->task_thread.cond);
913
99.4k
        }
914
202k
        reset_task_cur(c, ttd, t->frame_idx);
915
202k
    }
916
938k
    pthread_mutex_unlock(&ttd->lock);
917
918
    return NULL;
919
939k
}