Coverage Report

Created: 2026-07-30 06:27

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