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