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