Coverage Report

Created: 2026-08-13 07:23

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/dav1d/src/thread_task.c
Line
Count
Source
1
/*
2
 * Copyright © 2018, VideoLAN and dav1d authors
3
 * Copyright © 2018, Two Orioles, LLC
4
 * All rights reserved.
5
 *
6
 * Redistribution and use in source and binary forms, with or without
7
 * modification, are permitted provided that the following conditions are met:
8
 *
9
 * 1. Redistributions of source code must retain the above copyright notice, this
10
 *    list of conditions and the following disclaimer.
11
 *
12
 * 2. Redistributions in binary form must reproduce the above copyright notice,
13
 *    this list of conditions and the following disclaimer in the documentation
14
 *    and/or other materials provided with the distribution.
15
 *
16
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
17
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
20
 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
 */
27
28
#include "config.h"
29
30
#include "common/frame.h"
31
32
#include "src/thread_task.h"
33
#include "src/fg_apply.h"
34
35
// This function resets the cur pointer to the first frame theoretically
36
// executable after a task completed (ie. each time we update some progress or
37
// insert some tasks in the queue).
38
// When frame_idx is set, it can be either from a completed task, or from tasks
39
// inserted in the queue, in which case we have to make sure the cur pointer
40
// isn't past this insert.
41
// The special case where frame_idx is UINT_MAX is to handle the reset after
42
// completing a task and locklessly signaling progress. In this case we don't
43
// enter a critical section, which is needed for this function, so we set an
44
// atomic for a delayed handling, happening here. Meaning we can call this
45
// function without any actual update other than what's in the atomic, hence
46
// this special case.
47
static inline int reset_task_cur(const Dav1dContext *const c,
48
                                 struct TaskThreadData *const ttd,
49
                                 unsigned frame_idx)
50
12.0M
{
51
12.0M
    const unsigned first = atomic_load(&ttd->first);
52
12.0M
    unsigned reset_frame_idx = atomic_exchange(&ttd->reset_task_cur, UINT_MAX);
53
12.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
12.0M
    if (!ttd->cur && c->fc[first].task_thread.task_cur_prev == NULL)
58
1.95M
        return 0;
59
10.1M
    if (reset_frame_idx != UINT_MAX) {
60
356k
        if (frame_idx == UINT_MAX) {
61
195k
            if (reset_frame_idx > first + ttd->cur)
62
1.71k
                return 0;
63
193k
            ttd->cur = reset_frame_idx - first;
64
193k
            goto cur_found;
65
195k
        }
66
9.77M
    } else if (frame_idx == UINT_MAX)
67
7.74M
        return 0;
68
2.19M
    if (frame_idx < first) frame_idx += c->n_fc;
69
2.19M
    const unsigned min_frame_idx = umin(reset_frame_idx, frame_idx);
70
2.19M
    const unsigned cur_frame_idx = first + ttd->cur;
71
2.19M
    if (ttd->cur < c->n_fc && cur_frame_idx < min_frame_idx)
72
366k
        return 0;
73
2.93M
    for (ttd->cur = min_frame_idx - first; ttd->cur < c->n_fc; ttd->cur++)
74
2.78M
        if (c->fc[(first + ttd->cur) % c->n_fc].task_thread.task_head)
75
1.67M
            break;
76
2.02M
cur_found:
77
9.52M
    for (unsigned i = ttd->cur; i < c->n_fc; i++)
78
7.49M
        c->fc[(first + i) % c->n_fc].task_thread.task_cur_prev = NULL;
79
2.02M
    return 1;
80
1.83M
}
81
82
static inline void reset_task_cur_async(struct TaskThreadData *const ttd,
83
                                        unsigned frame_idx, unsigned n_frames)
84
940k
{
85
940k
    const unsigned first = atomic_load(&ttd->first);
86
940k
    if (frame_idx < first) frame_idx += n_frames;
87
940k
    unsigned last_idx = frame_idx;
88
943k
    do {
89
943k
        frame_idx = last_idx;
90
943k
        last_idx = atomic_exchange(&ttd->reset_task_cur, frame_idx);
91
943k
    } while (last_idx < frame_idx);
92
940k
    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
940k
}
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.15M
{
103
2.15M
    struct TaskThreadData *const ttd = f->task_thread.ttd;
104
2.15M
    if (atomic_load(f->c->flush)) return;
105
2.15M
    assert(!a || a->next == b);
106
2.15M
    if (!a) f->task_thread.task_head = first;
107
1.47M
    else a->next = first;
108
2.15M
    if (!b) f->task_thread.task_tail = last;
109
2.15M
    last->next = b;
110
2.15M
    reset_task_cur(f->c, ttd, first->frame_idx);
111
2.15M
    if (cond_signal && !atomic_fetch_or(&ttd->cond_signaled, 1))
112
123k
        pthread_cond_signal(&ttd->cond);
113
2.15M
}
114
115
static void insert_tasks(Dav1dFrameContext *const f,
116
                         Dav1dTask *const first, Dav1dTask *const last,
117
                         const int cond_signal)
118
2.15M
{
119
    // insert task back into task queue
120
2.15M
    Dav1dTask *t_ptr, *prev_t = NULL;
121
2.15M
    for (t_ptr = f->task_thread.task_head;
122
7.59M
         t_ptr; prev_t = t_ptr, t_ptr = t_ptr->next)
123
5.79M
    {
124
        // entropy coding precedes other steps
125
5.79M
        if (t_ptr->type == DAV1D_TASK_TYPE_TILE_ENTROPY) {
126
1.35M
            if (first->type > DAV1D_TASK_TYPE_TILE_ENTROPY) continue;
127
            // both are entropy
128
237k
            if (first->sby > t_ptr->sby) continue;
129
66.9k
            if (first->sby < t_ptr->sby) {
130
1.98k
                insert_tasks_between(f, first, last, prev_t, t_ptr, cond_signal);
131
1.98k
                return;
132
1.98k
            }
133
            // same sby
134
4.44M
        } else {
135
4.44M
            if (first->type == DAV1D_TASK_TYPE_TILE_ENTROPY) {
136
181k
                insert_tasks_between(f, first, last, prev_t, t_ptr, cond_signal);
137
181k
                return;
138
181k
            }
139
4.26M
            if (first->sby > t_ptr->sby) continue;
140
1.22M
            if (first->sby < t_ptr->sby) {
141
168k
                insert_tasks_between(f, first, last, prev_t, t_ptr, cond_signal);
142
168k
                return;
143
168k
            }
144
            // same sby
145
1.05M
            if (first->type > t_ptr->type) continue;
146
66.9k
            if (first->type < t_ptr->type) {
147
5.45k
                insert_tasks_between(f, first, last, prev_t, t_ptr, cond_signal);
148
5.45k
                return;
149
5.45k
            }
150
            // same task type
151
66.9k
        }
152
153
        // sort by tile-id
154
126k
        assert(first->type == DAV1D_TASK_TYPE_TILE_RECONSTRUCTION ||
155
126k
               first->type == DAV1D_TASK_TYPE_TILE_ENTROPY);
156
126k
        assert(first->type == t_ptr->type);
157
126k
        assert(t_ptr->sby == first->sby);
158
126k
        const int p = first->type == DAV1D_TASK_TYPE_TILE_ENTROPY;
159
126k
        const int t_tile_idx = (int) (first - f->task_thread.tile_tasks[p]);
160
126k
        const int p_tile_idx = (int) (t_ptr - f->task_thread.tile_tasks[p]);
161
126k
        assert(t_tile_idx != p_tile_idx);
162
126k
        if (t_tile_idx > p_tile_idx) continue;
163
531
        insert_tasks_between(f, first, last, prev_t, t_ptr, cond_signal);
164
531
        return;
165
126k
    }
166
    // append at the end
167
1.79M
    insert_tasks_between(f, first, last, prev_t, NULL, cond_signal);
168
1.79M
}
169
170
static inline void insert_task(Dav1dFrameContext *const f,
171
                               Dav1dTask *const t, const int cond_signal)
172
2.15M
{
173
2.15M
    insert_tasks(f, t, t, cond_signal);
174
2.15M
}
175
176
398k
static inline void add_pending(Dav1dFrameContext *const f, Dav1dTask *const t) {
177
398k
    pthread_mutex_lock(&f->task_thread.pending_tasks.lock);
178
398k
    t->next = NULL;
179
398k
    if (!f->task_thread.pending_tasks.head)
180
388k
        f->task_thread.pending_tasks.head = t;
181
9.38k
    else
182
9.38k
        f->task_thread.pending_tasks.tail->next = t;
183
398k
    f->task_thread.pending_tasks.tail = t;
184
398k
    atomic_store(&f->task_thread.pending_tasks.merge, 1);
185
398k
    pthread_mutex_unlock(&f->task_thread.pending_tasks.lock);
186
398k
}
187
188
69.2M
static inline int merge_pending_frame(Dav1dFrameContext *const f) {
189
69.2M
    int const merge = atomic_load(&f->task_thread.pending_tasks.merge);
190
69.2M
    if (merge) {
191
606k
        pthread_mutex_lock(&f->task_thread.pending_tasks.lock);
192
606k
        Dav1dTask *t = f->task_thread.pending_tasks.head;
193
606k
        f->task_thread.pending_tasks.head = NULL;
194
606k
        f->task_thread.pending_tasks.tail = NULL;
195
606k
        atomic_store(&f->task_thread.pending_tasks.merge, 0);
196
606k
        pthread_mutex_unlock(&f->task_thread.pending_tasks.lock);
197
1.90M
        while (t) {
198
1.29M
            Dav1dTask *const tmp = t->next;
199
1.29M
            insert_task(f, t, 0);
200
1.29M
            t = tmp;
201
1.29M
        }
202
606k
    }
203
69.2M
    return merge;
204
69.2M
}
205
206
10.5M
static inline int merge_pending(const Dav1dContext *const c) {
207
10.5M
    int res = 0;
208
73.5M
    for (unsigned i = 0; i < c->n_fc; i++)
209
63.0M
        res |= merge_pending_frame(&c->fc[i]);
210
10.5M
    return res;
211
10.5M
}
212
213
static int create_filter_sbrow(Dav1dFrameContext *const f,
214
                               const int pass, Dav1dTask **res_t)
215
437k
{
216
437k
    const int has_deblock = f->frame_hdr->loopfilter.level_y[0] ||
217
65.1k
                            f->frame_hdr->loopfilter.level_y[1];
218
437k
    const int has_cdef = f->seq_hdr->cdef;
219
437k
    const int has_resize = f->frame_hdr->width[0] != f->frame_hdr->width[1];
220
437k
    const int has_lr = f->lf.restore_planes;
221
222
437k
    Dav1dTask *tasks = f->task_thread.tasks;
223
437k
    const int uses_2pass = f->c->n_fc > 1;
224
437k
    int num_tasks = f->sbh * (1 + uses_2pass);
225
437k
    if (num_tasks > f->task_thread.num_tasks) {
226
89.3k
        const size_t size = sizeof(Dav1dTask) * num_tasks;
227
89.3k
        tasks = dav1d_realloc(ALLOC_COMMON_CTX, f->task_thread.tasks, size);
228
89.3k
        if (!tasks) return -1;
229
89.3k
        memset(tasks, 0, size);
230
89.3k
        f->task_thread.tasks = tasks;
231
89.3k
        f->task_thread.num_tasks = num_tasks;
232
89.3k
    }
233
437k
    tasks += f->sbh * (pass & 1);
234
235
437k
    if (pass & 1) {
236
218k
        f->frame_thread.entropy_progress = 0;
237
218k
    } else {
238
218k
        const int prog_sz = ((f->sbh + 31) & ~31) >> 5;
239
218k
        if (prog_sz > f->frame_thread.prog_sz) {
240
88.8k
            atomic_uint *const prog = dav1d_realloc(ALLOC_COMMON_CTX, f->frame_thread.frame_progress,
241
88.8k
                                                    2 * prog_sz * sizeof(*prog));
242
88.8k
            if (!prog) return -1;
243
88.8k
            f->frame_thread.frame_progress = prog;
244
88.8k
            f->frame_thread.copy_lpf_progress = prog + prog_sz;
245
88.8k
        }
246
218k
        f->frame_thread.prog_sz = prog_sz;
247
218k
        memset(f->frame_thread.frame_progress, 0, prog_sz * sizeof(atomic_uint));
248
218k
        memset(f->frame_thread.copy_lpf_progress, 0, prog_sz * sizeof(atomic_uint));
249
218k
        atomic_store(&f->frame_thread.deblock_progress, 0);
250
218k
    }
251
437k
    f->frame_thread.next_tile_row[pass & 1] = 0;
252
253
437k
    Dav1dTask *t = &tasks[0];
254
437k
    t->sby = 0;
255
437k
    t->recon_progress = 1;
256
437k
    t->deblock_progress = 0;
257
437k
    t->type = pass == 1 ? DAV1D_TASK_TYPE_ENTROPY_PROGRESS :
258
437k
              has_deblock ? DAV1D_TASK_TYPE_DEBLOCK_COLS :
259
218k
              has_cdef || has_lr /* i.e. LR backup */ ? DAV1D_TASK_TYPE_DEBLOCK_ROWS :
260
26.2k
              has_resize ? DAV1D_TASK_TYPE_SUPER_RESOLUTION :
261
12.4k
              DAV1D_TASK_TYPE_RECONSTRUCTION_PROGRESS;
262
437k
    t->frame_idx = (int)(f - f->c->fc);
263
264
437k
    *res_t = t;
265
437k
    return 0;
266
437k
}
267
268
int dav1d_task_create_tile_sbrow(Dav1dFrameContext *const f, const int pass,
269
                                 const int cond_signal)
270
437k
{
271
437k
    Dav1dTask *tasks = f->task_thread.tile_tasks[0];
272
437k
    const int uses_2pass = f->c->n_fc > 1;
273
437k
    const int n_tasks_per_pass = f->frame_hdr->tiling.cols * f->frame_hdr->tiling.rows;
274
437k
    const int n_tasks = n_tasks_per_pass * (1 + uses_2pass);
275
437k
    if (pass < 2) {
276
218k
        if (n_tasks > f->task_thread.num_tile_tasks) {
277
88.9k
            const size_t size = sizeof(Dav1dTask) * n_tasks;
278
88.9k
            tasks = dav1d_realloc(ALLOC_COMMON_CTX, f->task_thread.tile_tasks[0], size);
279
88.9k
            if (!tasks) return -1;
280
88.9k
            memset(tasks, 0, size);
281
88.9k
            f->task_thread.tile_tasks[0] = tasks;
282
88.9k
            f->task_thread.num_tile_tasks = n_tasks;
283
88.9k
        }
284
218k
        f->task_thread.tile_tasks[1] = tasks + n_tasks_per_pass;
285
218k
    }
286
437k
    assert(n_tasks <= f->task_thread.num_tile_tasks);
287
288
437k
    Dav1dTask *pf_t;
289
437k
    if (create_filter_sbrow(f, pass, &pf_t))
290
0
        return -1;
291
292
437k
    Dav1dTask *const p1_tasks = f->task_thread.tile_tasks[1];
293
437k
    Dav1dTask *prev_t = NULL;
294
437k
    if (pass == 2) {
295
218k
        prev_t = &p1_tasks[n_tasks_per_pass - 1];
296
        // PF task is scheduled after the last sby=0 TILE task
297
218k
        if (f->frame_hdr->tiling.rows == 1)
298
216k
            prev_t = prev_t->next;
299
218k
    }
300
437k
    tasks += (pass & 1) * n_tasks_per_pass;
301
898k
    for (int tile_idx = 0; tile_idx < n_tasks_per_pass; tile_idx++) {
302
461k
        Dav1dTileState *const ts = &f->ts[tile_idx];
303
461k
        Dav1dTask *t = &tasks[tile_idx];
304
461k
        t->sby = ts->tiling.row_start >> f->sb_shift;
305
461k
        if (pf_t && t->sby) {
306
3.89k
            prev_t->next = pf_t;
307
3.89k
            prev_t = pf_t;
308
3.89k
            pf_t = NULL;
309
3.89k
        }
310
461k
        t->recon_progress = 0;
311
461k
        t->deblock_progress = 0;
312
461k
        t->deps_skip = 0;
313
461k
        t->type = pass != 1 ? DAV1D_TASK_TYPE_TILE_RECONSTRUCTION :
314
461k
                              DAV1D_TASK_TYPE_TILE_ENTROPY;
315
461k
        t->frame_idx = (int)(f - f->c->fc);
316
461k
        if (prev_t) prev_t->next = t;
317
461k
        prev_t = t;
318
461k
    }
319
437k
    if (pf_t) {
320
433k
        prev_t->next = pf_t;
321
433k
        prev_t = pf_t;
322
433k
    }
323
437k
    prev_t->next = NULL;
324
325
437k
    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
437k
    if (!(pass & 1)) {
331
218k
        pthread_mutex_lock(&f->task_thread.pending_tasks.lock);
332
218k
        assert(f->task_thread.pending_tasks.head == NULL);
333
218k
        f->task_thread.pending_tasks.head = f->task_thread.tile_tasks[pass == 2];
334
218k
        f->task_thread.pending_tasks.tail = prev_t;
335
218k
        atomic_store(&f->task_thread.pending_tasks.merge, 1);
336
218k
        atomic_store(&f->task_thread.init_done, 1);
337
218k
        pthread_mutex_unlock(&f->task_thread.pending_tasks.lock);
338
218k
    }
339
437k
    return 0;
340
437k
}
341
342
226k
void dav1d_task_frame_init(Dav1dFrameContext *const f) {
343
226k
    const Dav1dContext *const c = f->c;
344
345
226k
    atomic_store(&f->task_thread.init_done, 0);
346
    // schedule init task, which will schedule the remaining tasks
347
226k
    Dav1dTask *const t = &f->task_thread.init_task;
348
226k
    t->type = DAV1D_TASK_TYPE_INIT;
349
226k
    t->frame_idx = (int)(f - c->fc);
350
226k
    t->sby = 0;
351
226k
    t->recon_progress = t->deblock_progress = 0;
352
226k
    insert_task(f, t, 1);
353
226k
}
354
355
void dav1d_task_delayed_fg(Dav1dContext *const c, Dav1dPicture *const out,
356
                           const Dav1dPicture *const in)
357
2.91k
{
358
2.91k
    struct TaskThreadData *const ttd = &c->task_thread;
359
2.91k
    ttd->delayed_fg.in = in;
360
2.91k
    ttd->delayed_fg.out = out;
361
2.91k
    ttd->delayed_fg.type = DAV1D_TASK_TYPE_FG_PREP;
362
2.91k
    atomic_init(&ttd->delayed_fg.progress[0], 0);
363
2.91k
    atomic_init(&ttd->delayed_fg.progress[1], 0);
364
2.91k
    pthread_mutex_lock(&ttd->lock);
365
2.91k
    ttd->delayed_fg.exec = 1;
366
2.91k
    ttd->delayed_fg.finished = 0;
367
2.91k
    pthread_cond_signal(&ttd->cond);
368
2.91k
    do {
369
2.91k
        pthread_cond_wait(&ttd->delayed_fg.cond, &ttd->lock);
370
2.91k
    } while (!ttd->delayed_fg.finished);
371
2.91k
    pthread_mutex_unlock(&ttd->lock);
372
2.91k
}
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
356k
{
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
356k
    int p1 = atomic_load(state);
382
356k
    if (p1 < t->sby) {
383
12.1k
        t->type = type;
384
12.1k
        t->recon_progress = t->deblock_progress = 0;
385
12.1k
        *target = t->sby;
386
12.1k
        add_pending(f, t);
387
12.1k
        pthread_mutex_lock(&ttd->lock);
388
12.1k
        return 1;
389
12.1k
    }
390
343k
    return 0;
391
356k
}
392
393
static inline int check_tile(Dav1dTask *const t, Dav1dFrameContext *const f,
394
                             const int frame_mt)
395
2.95M
{
396
2.95M
    const int tp = t->type == DAV1D_TASK_TYPE_TILE_ENTROPY;
397
2.95M
    const int tile_idx = (int)(t - f->task_thread.tile_tasks[tp]);
398
2.95M
    Dav1dTileState *const ts = &f->ts[tile_idx];
399
2.95M
    const int p1 = atomic_load(&ts->progress[tp]);
400
2.95M
    if (p1 < t->sby) return 1;
401
2.61M
    int error = p1 == TILE_ERROR;
402
2.61M
    error |= atomic_fetch_or(&f->task_thread.error, error);
403
2.61M
    if (!error && frame_mt && !tp) {
404
1.49M
        const int p2 = atomic_load(&ts->progress[1]);
405
1.49M
        if (p2 <= t->sby) return 1;
406
808k
        error = p2 == TILE_ERROR;
407
808k
        error |= atomic_fetch_or(&f->task_thread.error, error);
408
808k
    }
409
1.93M
    if (!error && frame_mt && !IS_KEY_OR_INTRA(f->frame_hdr)) {
410
        // check reference state
411
1.17M
        const Dav1dThreadPicture *p = &f->sr_cur;
412
1.17M
        const int ss_ver = p->p.p.layout == DAV1D_PIXEL_LAYOUT_I420;
413
1.17M
        const unsigned p_b = (t->sby + 1) << (f->sb_shift + 2);
414
1.17M
        const int tile_sby = t->sby - (ts->tiling.row_start >> f->sb_shift);
415
1.17M
        const int (*const lowest_px)[2] = ts->lowest_pixel[tile_sby];
416
3.76M
        for (int n = t->deps_skip; n < 7; n++, t->deps_skip++) {
417
3.39M
            unsigned lowest;
418
3.39M
            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.67M
                lowest = p_b;
423
1.72M
            } else {
424
                // +8 is postfilter-induced delay
425
1.72M
                const int y = lowest_px[n][0] == INT_MIN ? INT_MIN :
426
1.72M
                              lowest_px[n][0] + 8;
427
1.72M
                const int uv = lowest_px[n][1] == INT_MIN ? INT_MIN :
428
1.72M
                               lowest_px[n][1] * (1 << ss_ver) + 8;
429
1.72M
                const int max = imax(y, uv);
430
1.72M
                if (max == INT_MIN) continue;
431
702k
                lowest = iclip(max, 1, f->refp[n].p.p.h);
432
702k
            }
433
2.37M
            const unsigned p3 = atomic_load(&f->refp[n].progress[!tp]);
434
2.37M
            if (p3 < lowest) return 1;
435
2.37M
            atomic_fetch_or(&f->task_thread.error, p3 == FRAME_ERROR);
436
1.57M
        }
437
1.17M
    }
438
1.13M
    return 0;
439
1.93M
}
440
441
static inline int get_frame_progress(const Dav1dContext *const c,
442
                                     const Dav1dFrameContext *const f)
443
525k
{
444
525k
    unsigned frame_prog = c->n_fc > 1 ? atomic_load(&f->sr_cur.progress[1]) : 0;
445
525k
    if (frame_prog >= FRAME_ERROR)
446
171k
        return f->sbh - 1;
447
354k
    int idx = frame_prog >> (f->sb_shift + 7);
448
354k
    int prog;
449
356k
    do {
450
356k
        atomic_uint *state = &f->frame_thread.frame_progress[idx];
451
356k
        const unsigned val = ~atomic_load(state);
452
356k
        prog = val ? ctz(val) : 32;
453
356k
        if (prog != 32) break;
454
1.82k
        prog = 0;
455
1.82k
    } while (++idx < f->frame_thread.prog_sz);
456
354k
    return ((idx << 5) | prog) - 1;
457
525k
}
458
459
4.22k
static inline void abort_frame(Dav1dFrameContext *const f, const int error) {
460
4.22k
    atomic_store(&f->task_thread.error, error == DAV1D_ERR(EINVAL) ? 1 : -1);
461
4.22k
    atomic_store(&f->task_thread.task_counter, 0);
462
4.22k
    atomic_store(&f->task_thread.done[0], 1);
463
4.22k
    atomic_store(&f->task_thread.done[1], 1);
464
4.22k
    atomic_store(&f->sr_cur.progress[0], FRAME_ERROR);
465
4.22k
    atomic_store(&f->sr_cur.progress[1], FRAME_ERROR);
466
4.22k
    dav1d_decode_frame_exit(f, error);
467
4.22k
    f->n_tile_data = 0;
468
4.22k
    pthread_cond_signal(&f->task_thread.cond);
469
4.22k
}
470
471
static inline void delayed_fg_task(const Dav1dContext *const c,
472
                                   struct TaskThreadData *const ttd)
473
14.6k
{
474
14.6k
    const Dav1dPicture *const in = ttd->delayed_fg.in;
475
14.6k
    Dav1dPicture *const out = ttd->delayed_fg.out;
476
14.6k
#if CONFIG_16BPC
477
14.6k
    int off;
478
14.6k
    if (out->p.bpc != 8)
479
8.52k
        off = (out->p.bpc >> 1) - 4;
480
14.6k
#endif
481
14.6k
    switch (ttd->delayed_fg.type) {
482
2.91k
    case DAV1D_TASK_TYPE_FG_PREP:
483
2.91k
        ttd->delayed_fg.exec = 0;
484
2.91k
        if (atomic_load(&ttd->cond_signaled))
485
1
            pthread_cond_signal(&ttd->cond);
486
2.91k
        pthread_mutex_unlock(&ttd->lock);
487
2.91k
        switch (out->p.bpc) {
488
0
#if CONFIG_8BPC
489
1.21k
        case 8:
490
1.21k
            dav1d_prep_grain_8bpc(&c->dsp[0].fg, out, in,
491
1.21k
                                  ttd->delayed_fg.scaling_8bpc,
492
1.21k
                                  ttd->delayed_fg.grain_lut_8bpc);
493
1.21k
            break;
494
0
#endif
495
0
#if CONFIG_16BPC
496
1.55k
        case 10:
497
1.69k
        case 12:
498
1.69k
            dav1d_prep_grain_16bpc(&c->dsp[off].fg, out, in,
499
1.69k
                                   ttd->delayed_fg.scaling_16bpc,
500
1.69k
                                   ttd->delayed_fg.grain_lut_16bpc);
501
1.69k
            break;
502
0
#endif
503
0
        default: abort();
504
2.91k
        }
505
2.91k
        ttd->delayed_fg.type = DAV1D_TASK_TYPE_FG_APPLY;
506
2.91k
        pthread_mutex_lock(&ttd->lock);
507
2.91k
        ttd->delayed_fg.exec = 1;
508
        // fall-through
509
14.6k
    case DAV1D_TASK_TYPE_FG_APPLY:;
510
14.6k
        int row = atomic_fetch_add(&ttd->delayed_fg.progress[0], 1);
511
14.6k
        pthread_mutex_unlock(&ttd->lock);
512
14.6k
        int progmax = (out->p.h + FG_BLOCK_SIZE - 1) / FG_BLOCK_SIZE;
513
47.4k
        while (row < progmax) {
514
33.2k
            if (row + 1 < progmax)
515
30.3k
                pthread_cond_signal(&ttd->cond);
516
2.91k
            else {
517
2.91k
                pthread_mutex_lock(&ttd->lock);
518
2.91k
                ttd->delayed_fg.exec = 0;
519
2.91k
                pthread_mutex_unlock(&ttd->lock);
520
2.91k
            }
521
33.2k
            switch (out->p.bpc) {
522
0
#if CONFIG_8BPC
523
14.1k
            case 8:
524
14.1k
                dav1d_apply_grain_row_8bpc(&c->dsp[0].fg, out, in,
525
14.1k
                                           ttd->delayed_fg.scaling_8bpc,
526
14.1k
                                           ttd->delayed_fg.grain_lut_8bpc, row);
527
14.1k
                break;
528
0
#endif
529
0
#if CONFIG_16BPC
530
17.3k
            case 10:
531
19.0k
            case 12:
532
19.0k
                dav1d_apply_grain_row_16bpc(&c->dsp[off].fg, out, in,
533
19.0k
                                            ttd->delayed_fg.scaling_16bpc,
534
19.0k
                                            ttd->delayed_fg.grain_lut_16bpc, row);
535
19.0k
                break;
536
0
#endif
537
0
            default: abort();
538
33.2k
            }
539
32.7k
            row = atomic_fetch_add(&ttd->delayed_fg.progress[0], 1);
540
32.7k
            atomic_fetch_add(&ttd->delayed_fg.progress[1], 1);
541
32.7k
        }
542
14.2k
        pthread_mutex_lock(&ttd->lock);
543
14.2k
        ttd->delayed_fg.exec = 0;
544
14.2k
        int done = atomic_fetch_add(&ttd->delayed_fg.progress[1], 1) + 1;
545
14.2k
        progmax = atomic_load(&ttd->delayed_fg.progress[0]);
546
        // signal for completion only once the last runner reaches this
547
14.2k
        if (done >= progmax) {
548
2.91k
            ttd->delayed_fg.finished = 1;
549
2.91k
            pthread_cond_signal(&ttd->delayed_fg.cond);
550
2.91k
        }
551
14.2k
        break;
552
0
    default: abort();
553
14.6k
    }
554
14.6k
}
555
556
1.94M
void *dav1d_worker_task(void *data) {
557
1.94M
    Dav1dTaskContext *const tc = data;
558
1.94M
    const Dav1dContext *const c = tc->c;
559
1.94M
    struct TaskThreadData *const ttd = tc->task_thread.ttd;
560
561
1.94M
    dav1d_set_thread_name("dav1d-worker");
562
563
1.94M
    pthread_mutex_lock(&ttd->lock);
564
8.35M
    for (;;) {
565
8.35M
        if (tc->task_thread.die) break;
566
6.40M
        if (atomic_load(c->flush)) goto park;
567
568
6.34M
        merge_pending(c);
569
6.34M
        if (ttd->delayed_fg.exec) { // run delayed film grain first
570
14.6k
            delayed_fg_task(c, ttd);
571
14.6k
            continue;
572
14.6k
        }
573
6.32M
        Dav1dFrameContext *f;
574
6.32M
        Dav1dTask *t, *prev_t = NULL;
575
6.32M
        if (c->n_fc > 1) { // run init tasks second
576
43.5M
            for (unsigned i = 0; i < c->n_fc; i++) {
577
37.4M
                const unsigned first = atomic_load(&ttd->first);
578
37.4M
                f = &c->fc[(first + i) % c->n_fc];
579
37.4M
                if (atomic_load(&f->task_thread.init_done)) continue;
580
23.1M
                t = f->task_thread.task_head;
581
23.1M
                if (!t) continue;
582
1.04M
                if (t->type == DAV1D_TASK_TYPE_INIT) goto found;
583
821k
                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
821k
                    const int p1 = f->in_cdf.progress ?
592
821k
                        atomic_load(f->in_cdf.progress) : 1;
593
821k
                    if (p1) {
594
44.8k
                        atomic_fetch_or(&f->task_thread.error, p1 == TILE_ERROR);
595
44.8k
                        goto found;
596
44.8k
                    }
597
821k
                }
598
821k
            }
599
6.32M
        }
600
10.3M
        while (ttd->cur < c->n_fc) { // run decoding tasks last
601
6.15M
            const unsigned first = atomic_load(&ttd->first);
602
6.15M
            f = &c->fc[(first + ttd->cur) % c->n_fc];
603
6.15M
            merge_pending_frame(f);
604
6.15M
            prev_t = f->task_thread.task_cur_prev;
605
6.15M
            t = prev_t ? prev_t->next : f->task_thread.task_head;
606
12.7M
            while (t) {
607
8.47M
                if (t->type == DAV1D_TASK_TYPE_INIT_CDF) goto next;
608
8.28M
                else if (t->type == DAV1D_TASK_TYPE_TILE_ENTROPY ||
609
7.53M
                         t->type == DAV1D_TASK_TYPE_TILE_RECONSTRUCTION)
610
2.27M
                {
611
                    // if not bottom sbrow of tile, this task will be re-added
612
                    // after it's finished
613
2.27M
                    if (!check_tile(t, f, c->n_fc > 1))
614
785k
                        goto found;
615
6.01M
                } else if (t->recon_progress) {
616
3.72M
                    const int p = t->type == DAV1D_TASK_TYPE_ENTROPY_PROGRESS;
617
3.72M
                    int error = atomic_load(&f->task_thread.error);
618
3.72M
                    assert(!atomic_load(&f->task_thread.done[p]) || error);
619
3.72M
                    const int tile_row_base = f->frame_hdr->tiling.cols *
620
3.72M
                                              f->frame_thread.next_tile_row[p];
621
3.72M
                    if (p) {
622
1.38M
                        atomic_int *const prog = &f->frame_thread.entropy_progress;
623
1.38M
                        const int p1 = atomic_load(prog);
624
1.38M
                        if (p1 < t->sby) goto next;
625
1.38M
                        atomic_fetch_or(&f->task_thread.error, p1 == TILE_ERROR);
626
1.33M
                    }
627
4.83M
                    for (int tc = 0; tc < f->frame_hdr->tiling.cols; tc++) {
628
3.77M
                        Dav1dTileState *const ts = &f->ts[tile_row_base + tc];
629
3.77M
                        const int p2 = atomic_load(&ts->progress[p]);
630
3.77M
                        if (p2 < t->recon_progress) goto next;
631
3.77M
                        atomic_fetch_or(&f->task_thread.error, p2 == TILE_ERROR);
632
1.15M
                    }
633
1.05M
                    if (t->sby + 1 < f->sbh) {
634
                        // add sby+1 to list to replace this one
635
631k
                        Dav1dTask *next_t = &t[1];
636
631k
                        *next_t = *t;
637
631k
                        next_t->sby++;
638
631k
                        const int ntr = f->frame_thread.next_tile_row[p] + 1;
639
631k
                        const int start = f->frame_hdr->tiling.row_start_sb[ntr];
640
631k
                        if (next_t->sby == start)
641
8.94k
                            f->frame_thread.next_tile_row[p] = ntr;
642
631k
                        next_t->recon_progress = next_t->sby + 1;
643
631k
                        insert_task(f, next_t, 0);
644
631k
                    }
645
1.05M
                    goto found;
646
3.68M
                } else if (t->type == DAV1D_TASK_TYPE_CDEF) {
647
41.0k
                    atomic_uint *prog = f->frame_thread.copy_lpf_progress;
648
41.0k
                    const int p1 = atomic_load(&prog[(t->sby - 1) >> 5]);
649
41.0k
                    if (p1 & (1U << ((t->sby - 1) & 31)))
650
7.09k
                        goto found;
651
2.24M
                } else {
652
2.24M
                    assert(t->deblock_progress);
653
2.24M
                    const int p1 = atomic_load(&f->frame_thread.deblock_progress);
654
2.24M
                    if (p1 >= t->deblock_progress) {
655
12.1k
                        atomic_fetch_or(&f->task_thread.error, p1 == TILE_ERROR);
656
12.1k
                        goto found;
657
12.1k
                    }
658
2.24M
                }
659
6.61M
            next:
660
6.61M
                prev_t = t;
661
6.61M
                t = t->next;
662
6.61M
                f->task_thread.task_cur_prev = prev_t;
663
6.61M
            }
664
4.29M
            ttd->cur++;
665
4.29M
        }
666
4.19M
        if (reset_task_cur(c, ttd, UINT_MAX)) continue;
667
4.17M
        if (merge_pending(c)) continue;
668
4.22M
    park:
669
4.22M
        tc->task_thread.flushed = 1;
670
4.22M
        pthread_cond_signal(&tc->task_thread.td.cond);
671
        // we want to be woken up next time progress is signaled
672
4.22M
        atomic_store(&ttd->cond_signaled, 0);
673
4.22M
        pthread_cond_wait(&ttd->cond, &ttd->lock);
674
4.22M
        tc->task_thread.flushed = 0;
675
4.22M
        reset_task_cur(c, ttd, UINT_MAX);
676
4.22M
        continue;
677
678
2.12M
    found:
679
        // remove t from list
680
2.12M
        if (prev_t) prev_t->next = t->next;
681
1.88M
        else f->task_thread.task_head = t->next;
682
2.12M
        if (!t->next) f->task_thread.task_tail = prev_t;
683
2.12M
        if (t->type > DAV1D_TASK_TYPE_INIT_CDF && !f->task_thread.task_head)
684
211k
            ttd->cur++;
685
2.12M
        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.12M
        atomic_store(&ttd->cond_signaled, 1);
690
2.12M
        pthread_cond_signal(&ttd->cond);
691
2.12M
        pthread_mutex_unlock(&ttd->lock);
692
2.65M
    found_unlocked:;
693
2.65M
        const int flush = atomic_load(c->flush);
694
2.65M
        int error = atomic_fetch_or(&f->task_thread.error, flush) | flush;
695
696
        // run it
697
2.65M
        tc->f = f;
698
2.65M
        int sby = t->sby;
699
2.65M
        switch (t->type) {
700
223k
        case DAV1D_TASK_TYPE_INIT: {
701
223k
            assert(c->n_fc > 1);
702
223k
            int res = dav1d_decode_frame_init(f);
703
223k
            int p1 = f->in_cdf.progress ? atomic_load(f->in_cdf.progress) : 1;
704
223k
            if (res || p1 == TILE_ERROR) {
705
281
                pthread_mutex_lock(&ttd->lock);
706
281
                abort_frame(f, res ? res : DAV1D_ERR(EINVAL));
707
281
                reset_task_cur(c, ttd, t->frame_idx);
708
223k
            } else {
709
223k
                t->type = DAV1D_TASK_TYPE_INIT_CDF;
710
223k
                if (p1) goto found_unlocked;
711
45.5k
                add_pending(f, t);
712
45.5k
                pthread_mutex_lock(&ttd->lock);
713
45.5k
            }
714
45.8k
            continue;
715
223k
        }
716
222k
        case DAV1D_TASK_TYPE_INIT_CDF: {
717
222k
            assert(c->n_fc > 1);
718
222k
            int res = DAV1D_ERR(EINVAL);
719
222k
            if (!atomic_load(&f->task_thread.error))
720
219k
                res = dav1d_decode_frame_init_cdf(f);
721
222k
            if (f->frame_hdr->refresh_context && !f->task_thread.update_set)
722
222k
                atomic_store(f->out_cdf.progress, res < 0 ? TILE_ERROR : 1);
723
660k
            for (int p = 1; p <= 2 && !res; p++)
724
437k
                res = dav1d_task_create_tile_sbrow(f, p, 0);
725
222k
            pthread_mutex_lock(&ttd->lock);
726
222k
            if (res) {
727
3.94k
                abort_frame(f, DAV1D_ERR(ENOMEM));
728
3.94k
                reset_task_cur(c, ttd, t->frame_idx);
729
3.94k
                atomic_store(&f->task_thread.init_done, 1);
730
3.94k
            }
731
222k
            continue;
732
222k
        }
733
571k
        case DAV1D_TASK_TYPE_TILE_ENTROPY:
734
1.13M
        case DAV1D_TASK_TYPE_TILE_RECONSTRUCTION: {
735
1.13M
            const int p = t->type == DAV1D_TASK_TYPE_TILE_ENTROPY;
736
1.13M
            const int tile_idx = (int)(t - f->task_thread.tile_tasks[p]);
737
1.13M
            Dav1dTileState *const ts = &f->ts[tile_idx];
738
739
1.13M
            tc->ts = ts;
740
1.13M
            tc->by = sby << f->sb_shift;
741
1.13M
            const int uses_2pass = c->n_fc > 1;
742
1.13M
            tc->frame_thread.pass = !uses_2pass ? 0 :
743
1.13M
                1 + (t->type == DAV1D_TASK_TYPE_TILE_RECONSTRUCTION);
744
1.13M
            if (!error) error = dav1d_decode_tile_sbrow(tc);
745
1.13M
            const int progress = error ? TILE_ERROR : 1 + sby;
746
747
            // signal progress
748
1.13M
            atomic_fetch_or(&f->task_thread.error, error);
749
1.13M
            if (((sby + 1) << f->sb_shift) < ts->tiling.row_end) {
750
682k
                t->sby++;
751
682k
                t->deps_skip = 0;
752
682k
                if (!check_tile(t, f, uses_2pass)) {
753
348k
                    atomic_store(&ts->progress[p], progress);
754
348k
                    reset_task_cur_async(ttd, t->frame_idx, c->n_fc);
755
348k
                    if (!atomic_fetch_or(&ttd->cond_signaled, 1))
756
112
                        pthread_cond_signal(&ttd->cond);
757
348k
                    goto found_unlocked;
758
348k
                }
759
682k
                atomic_store(&ts->progress[p], progress);
760
333k
                add_pending(f, t);
761
333k
                pthread_mutex_lock(&ttd->lock);
762
452k
            } else {
763
452k
                pthread_mutex_lock(&ttd->lock);
764
452k
                atomic_store(&ts->progress[p], progress);
765
452k
                reset_task_cur(c, ttd, t->frame_idx);
766
452k
                error = atomic_load(&f->task_thread.error);
767
452k
                if (f->frame_hdr->refresh_context &&
768
259k
                    tc->frame_thread.pass <= 1 && f->task_thread.update_set &&
769
130k
                    f->frame_hdr->tiling.update == tile_idx)
770
129k
                {
771
129k
                    if (!error)
772
126k
                        dav1d_cdf_thread_update(f->frame_hdr, f->out_cdf.data.cdf,
773
126k
                                                &f->ts[f->frame_hdr->tiling.update].cdf);
774
129k
                    if (c->n_fc > 1)
775
129k
                        atomic_store(f->out_cdf.progress, error ? TILE_ERROR : 1);
776
129k
                }
777
452k
                if (atomic_fetch_sub(&f->task_thread.task_counter, 1) - 1 == 0 &&
778
452k
                    atomic_load(&f->task_thread.done[0]) &&
779
1.01k
                    (!uses_2pass || atomic_load(&f->task_thread.done[1])))
780
1.01k
                {
781
1.01k
                    error = atomic_load(&f->task_thread.error);
782
1.01k
                    dav1d_decode_frame_exit(f, error == 1 ? DAV1D_ERR(EINVAL) :
783
1.01k
                                            error ? DAV1D_ERR(ENOMEM) : 0);
784
1.01k
                    f->n_tile_data = 0;
785
1.01k
                    pthread_cond_signal(&f->task_thread.cond);
786
1.01k
                }
787
452k
                assert(atomic_load(&f->task_thread.task_counter) >= 0);
788
452k
                if (!atomic_fetch_or(&ttd->cond_signaled, 1))
789
304k
                    pthread_cond_signal(&ttd->cond);
790
452k
            }
791
785k
            continue;
792
1.13M
        }
793
785k
        case DAV1D_TASK_TYPE_DEBLOCK_COLS:
794
356k
            if (!atomic_load(&f->task_thread.error))
795
272k
                f->bd_fn.filter_sbrow_deblock_cols(f, sby);
796
356k
            if (ensure_progress(ttd, f, t, DAV1D_TASK_TYPE_DEBLOCK_ROWS,
797
356k
                                &f->frame_thread.deblock_progress,
798
356k
                                &t->deblock_progress)) continue;
799
            // fall-through
800
443k
        case DAV1D_TASK_TYPE_DEBLOCK_ROWS:
801
443k
            if (!atomic_load(&f->task_thread.error))
802
308k
                f->bd_fn.filter_sbrow_deblock_rows(f, sby);
803
            // signal deblock progress
804
443k
            if (f->frame_hdr->loopfilter.level_y[0] ||
805
118k
                f->frame_hdr->loopfilter.level_y[1])
806
356k
            {
807
356k
                error = atomic_load(&f->task_thread.error);
808
356k
                atomic_store(&f->frame_thread.deblock_progress,
809
356k
                             error ? TILE_ERROR : sby + 1);
810
356k
                reset_task_cur_async(ttd, t->frame_idx, c->n_fc);
811
356k
                if (!atomic_fetch_or(&ttd->cond_signaled, 1))
812
110k
                    pthread_cond_signal(&ttd->cond);
813
356k
            } else if (f->seq_hdr->cdef || f->lf.restore_planes) {
814
87.8k
                atomic_fetch_or(&f->frame_thread.copy_lpf_progress[sby >> 5],
815
87.8k
                                1U << (sby & 31));
816
                // CDEF needs the top buffer to be saved by lr_copy_lpf of the
817
                // previous sbrow
818
87.8k
                if (sby) {
819
74.4k
                    int prog = atomic_load(&f->frame_thread.copy_lpf_progress[(sby - 1) >> 5]);
820
74.4k
                    if (~prog & (1U << ((sby - 1) & 31))) {
821
7.09k
                        t->type = DAV1D_TASK_TYPE_CDEF;
822
7.09k
                        t->recon_progress = t->deblock_progress = 0;
823
7.09k
                        add_pending(f, t);
824
7.09k
                        pthread_mutex_lock(&ttd->lock);
825
7.09k
                        continue;
826
7.09k
                    }
827
74.4k
                }
828
87.8k
            }
829
            // fall-through
830
443k
        case DAV1D_TASK_TYPE_CDEF:
831
443k
            if (f->seq_hdr->cdef) {
832
235k
                if (!atomic_load(&f->task_thread.error))
833
155k
                    f->bd_fn.filter_sbrow_cdef(tc, sby);
834
235k
                reset_task_cur_async(ttd, t->frame_idx, c->n_fc);
835
235k
                if (!atomic_fetch_or(&ttd->cond_signaled, 1))
836
69.5k
                    pthread_cond_signal(&ttd->cond);
837
235k
            }
838
            // fall-through
839
445k
        case DAV1D_TASK_TYPE_SUPER_RESOLUTION:
840
445k
            if (f->frame_hdr->width[0] != f->frame_hdr->width[1])
841
71.5k
                if (!atomic_load(&f->task_thread.error))
842
48.1k
                    f->bd_fn.filter_sbrow_resize(f, sby);
843
            // fall-through
844
445k
        case DAV1D_TASK_TYPE_LOOP_RESTORATION:
845
445k
            if (!atomic_load(&f->task_thread.error) && f->lf.restore_planes)
846
173k
                f->bd_fn.filter_sbrow_lr(f, sby);
847
            // fall-through
848
525k
        case DAV1D_TASK_TYPE_RECONSTRUCTION_PROGRESS:
849
            // dummy to cover for no post-filters
850
1.05M
        case DAV1D_TASK_TYPE_ENTROPY_PROGRESS:
851
            // dummy to convert tile progress to frame
852
1.05M
            break;
853
0
        default: abort();
854
2.65M
        }
855
        // if task completed [typically LR], signal picture progress as per below
856
1.05M
        const int uses_2pass = c->n_fc > 1;
857
1.05M
        const int sbh = f->sbh;
858
1.05M
        const int sbsz = f->sb_step * 4;
859
1.05M
        if (t->type == DAV1D_TASK_TYPE_ENTROPY_PROGRESS) {
860
530k
            error = atomic_load(&f->task_thread.error);
861
530k
            const unsigned y = sby + 1 == sbh ? UINT_MAX : (unsigned)(sby + 1) * sbsz;
862
530k
            assert(c->n_fc > 1);
863
530k
            if (f->sr_cur.p.data[0] /* upon flush, this can be free'ed already */)
864
530k
                atomic_store(&f->sr_cur.progress[0], error ? FRAME_ERROR : y);
865
530k
            atomic_store(&f->frame_thread.entropy_progress,
866
530k
                         error ? TILE_ERROR : sby + 1);
867
530k
            if (sby + 1 == sbh)
868
530k
                atomic_store(&f->task_thread.done[1], 1);
869
530k
            pthread_mutex_lock(&ttd->lock);
870
530k
            const int num_tasks = atomic_fetch_sub(&f->task_thread.task_counter, 1) - 1;
871
530k
            if (sby + 1 < sbh && num_tasks) {
872
313k
                reset_task_cur(c, ttd, t->frame_idx);
873
313k
                continue;
874
313k
            }
875
216k
            if (!num_tasks && atomic_load(&f->task_thread.done[0]) &&
876
216k
                atomic_load(&f->task_thread.done[1]))
877
28.4k
            {
878
28.4k
                error = atomic_load(&f->task_thread.error);
879
28.4k
                dav1d_decode_frame_exit(f, error == 1 ? DAV1D_ERR(EINVAL) :
880
28.4k
                                        error ? DAV1D_ERR(ENOMEM) : 0);
881
28.4k
                f->n_tile_data = 0;
882
28.4k
                pthread_cond_signal(&f->task_thread.cond);
883
28.4k
            }
884
216k
            reset_task_cur(c, ttd, t->frame_idx);
885
216k
            continue;
886
530k
        }
887
    // t->type != DAV1D_TASK_TYPE_ENTROPY_PROGRESS
888
1.05M
        atomic_fetch_or(&f->frame_thread.frame_progress[sby >> 5],
889
525k
                        1U << (sby & 31));
890
525k
        pthread_mutex_lock(&f->task_thread.lock);
891
525k
        sby = get_frame_progress(c, f);
892
525k
        error = atomic_load(&f->task_thread.error);
893
525k
        const unsigned y = sby + 1 == sbh ? UINT_MAX : (unsigned)(sby + 1) * sbsz;
894
525k
        if (c->n_fc > 1 && f->sr_cur.p.data[0] /* upon flush, this can be free'ed already */)
895
525k
            atomic_store(&f->sr_cur.progress[1], error ? FRAME_ERROR : y);
896
525k
        pthread_mutex_unlock(&f->task_thread.lock);
897
525k
        if (sby + 1 == sbh)
898
525k
            atomic_store(&f->task_thread.done[0], 1);
899
525k
        pthread_mutex_lock(&ttd->lock);
900
525k
        const int num_tasks = atomic_fetch_sub(&f->task_thread.task_counter, 1) - 1;
901
525k
        if (sby + 1 < sbh && num_tasks) {
902
147k
            reset_task_cur(c, ttd, t->frame_idx);
903
147k
            continue;
904
147k
        }
905
377k
        if (!num_tasks && atomic_load(&f->task_thread.done[0]) &&
906
180k
            (!uses_2pass || atomic_load(&f->task_thread.done[1])))
907
180k
        {
908
180k
            error = atomic_load(&f->task_thread.error);
909
180k
            dav1d_decode_frame_exit(f, error == 1 ? DAV1D_ERR(EINVAL) :
910
180k
                                    error ? DAV1D_ERR(ENOMEM) : 0);
911
180k
            f->n_tile_data = 0;
912
180k
            pthread_cond_signal(&f->task_thread.cond);
913
180k
        }
914
377k
        reset_task_cur(c, ttd, t->frame_idx);
915
377k
    }
916
1.94M
    pthread_mutex_unlock(&ttd->lock);
917
918
    return NULL;
919
1.94M
}