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