Coverage Report

Created: 2026-09-01 06:57

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