Coverage Report

Created: 2026-12-01 06:48

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