Coverage Report

Created: 2026-08-13 07:23

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