Coverage Report

Created: 2026-05-30 06:10

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