/work/dav1d/src/thread_task.c
Line | Count | Source |
1 | | /* |
2 | | * Copyright © 2018, VideoLAN and dav1d authors |
3 | | * Copyright © 2018, Two Orioles, LLC |
4 | | * All rights reserved. |
5 | | * |
6 | | * Redistribution and use in source and binary forms, with or without |
7 | | * modification, are permitted provided that the following conditions are met: |
8 | | * |
9 | | * 1. Redistributions of source code must retain the above copyright notice, this |
10 | | * list of conditions and the following disclaimer. |
11 | | * |
12 | | * 2. Redistributions in binary form must reproduce the above copyright notice, |
13 | | * this list of conditions and the following disclaimer in the documentation |
14 | | * and/or other materials provided with the distribution. |
15 | | * |
16 | | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
17 | | * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
18 | | * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
19 | | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
20 | | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
21 | | * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
22 | | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND |
23 | | * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
25 | | * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | | */ |
27 | | |
28 | | #include "config.h" |
29 | | |
30 | | #include "common/frame.h" |
31 | | |
32 | | #include "src/thread_task.h" |
33 | | #include "src/fg_apply.h" |
34 | | |
35 | | // This function resets the cur pointer to the first frame theoretically |
36 | | // executable after a task completed (ie. each time we update some progress or |
37 | | // insert some tasks in the queue). |
38 | | // When frame_idx is set, it can be either from a completed task, or from tasks |
39 | | // inserted in the queue, in which case we have to make sure the cur pointer |
40 | | // isn't past this insert. |
41 | | // The special case where frame_idx is UINT_MAX is to handle the reset after |
42 | | // completing a task and locklessly signaling progress. In this case we don't |
43 | | // enter a critical section, which is needed for this function, so we set an |
44 | | // atomic for a delayed handling, happening here. Meaning we can call this |
45 | | // function without any actual update other than what's in the atomic, hence |
46 | | // this special case. |
47 | | static inline int reset_task_cur(const Dav1dContext *const c, |
48 | | struct TaskThreadData *const ttd, |
49 | | unsigned frame_idx) |
50 | 0 | { |
51 | 0 | const unsigned first = atomic_load(&ttd->first); |
52 | 0 | unsigned reset_frame_idx = atomic_exchange(&ttd->reset_task_cur, UINT_MAX); |
53 | 0 | if (reset_frame_idx < first) { |
54 | 0 | if (frame_idx == UINT_MAX) return 0; |
55 | 0 | reset_frame_idx = UINT_MAX; |
56 | 0 | } |
57 | 0 | if (!ttd->cur && c->fc[first].task_thread.task_cur_prev == NULL) |
58 | 0 | return 0; |
59 | 0 | if (reset_frame_idx != UINT_MAX) { |
60 | 0 | if (frame_idx == UINT_MAX) { |
61 | 0 | if (reset_frame_idx > first + ttd->cur) |
62 | 0 | return 0; |
63 | 0 | ttd->cur = reset_frame_idx - first; |
64 | 0 | goto cur_found; |
65 | 0 | } |
66 | 0 | } else if (frame_idx == UINT_MAX) |
67 | 0 | return 0; |
68 | 0 | if (frame_idx < first) frame_idx += c->n_fc; |
69 | 0 | const unsigned min_frame_idx = umin(reset_frame_idx, frame_idx); |
70 | 0 | const unsigned cur_frame_idx = first + ttd->cur; |
71 | 0 | if (ttd->cur < c->n_fc && cur_frame_idx < min_frame_idx) |
72 | 0 | return 0; |
73 | 0 | for (ttd->cur = min_frame_idx - first; ttd->cur < c->n_fc; ttd->cur++) |
74 | 0 | if (c->fc[(first + ttd->cur) % c->n_fc].task_thread.task_head) |
75 | 0 | break; |
76 | 0 | cur_found: |
77 | 0 | for (unsigned i = ttd->cur; i < c->n_fc; i++) |
78 | 0 | c->fc[(first + i) % c->n_fc].task_thread.task_cur_prev = NULL; |
79 | 0 | return 1; |
80 | 0 | } |
81 | | |
82 | | static inline void reset_task_cur_async(struct TaskThreadData *const ttd, |
83 | | unsigned frame_idx, unsigned n_frames) |
84 | 0 | { |
85 | 0 | const unsigned first = atomic_load(&ttd->first); |
86 | 0 | if (frame_idx < first) frame_idx += n_frames; |
87 | 0 | unsigned last_idx = frame_idx; |
88 | 0 | do { |
89 | 0 | frame_idx = last_idx; |
90 | 0 | last_idx = atomic_exchange(&ttd->reset_task_cur, frame_idx); |
91 | 0 | } while (last_idx < frame_idx); |
92 | 0 | 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 | 0 | } |
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 | 0 | { |
103 | 0 | struct TaskThreadData *const ttd = f->task_thread.ttd; |
104 | 0 | if (atomic_load(f->c->flush)) return; |
105 | 0 | assert(!a || a->next == b); |
106 | 0 | if (!a) f->task_thread.task_head = first; |
107 | 0 | else a->next = first; |
108 | 0 | if (!b) f->task_thread.task_tail = last; |
109 | 0 | last->next = b; |
110 | 0 | reset_task_cur(f->c, ttd, first->frame_idx); |
111 | 0 | if (cond_signal && !atomic_fetch_or(&ttd->cond_signaled, 1)) |
112 | 0 | pthread_cond_signal(&ttd->cond); |
113 | 0 | } |
114 | | |
115 | | static void insert_tasks(Dav1dFrameContext *const f, |
116 | | Dav1dTask *const first, Dav1dTask *const last, |
117 | | const int cond_signal) |
118 | 0 | { |
119 | | // insert task back into task queue |
120 | 0 | Dav1dTask *t_ptr, *prev_t = NULL; |
121 | 0 | for (t_ptr = f->task_thread.task_head; |
122 | 0 | t_ptr; prev_t = t_ptr, t_ptr = t_ptr->next) |
123 | 0 | { |
124 | | // entropy coding precedes other steps |
125 | 0 | if (t_ptr->type == DAV1D_TASK_TYPE_TILE_ENTROPY) { |
126 | 0 | if (first->type > DAV1D_TASK_TYPE_TILE_ENTROPY) continue; |
127 | | // both are entropy |
128 | 0 | if (first->sby > t_ptr->sby) continue; |
129 | 0 | if (first->sby < t_ptr->sby) { |
130 | 0 | insert_tasks_between(f, first, last, prev_t, t_ptr, cond_signal); |
131 | 0 | return; |
132 | 0 | } |
133 | | // same sby |
134 | 0 | } else { |
135 | 0 | if (first->type == DAV1D_TASK_TYPE_TILE_ENTROPY) { |
136 | 0 | insert_tasks_between(f, first, last, prev_t, t_ptr, cond_signal); |
137 | 0 | return; |
138 | 0 | } |
139 | 0 | if (first->sby > t_ptr->sby) continue; |
140 | 0 | if (first->sby < t_ptr->sby) { |
141 | 0 | insert_tasks_between(f, first, last, prev_t, t_ptr, cond_signal); |
142 | 0 | return; |
143 | 0 | } |
144 | | // same sby |
145 | 0 | if (first->type > t_ptr->type) continue; |
146 | 0 | if (first->type < t_ptr->type) { |
147 | 0 | insert_tasks_between(f, first, last, prev_t, t_ptr, cond_signal); |
148 | 0 | return; |
149 | 0 | } |
150 | | // same task type |
151 | 0 | } |
152 | | |
153 | | // sort by tile-id |
154 | 0 | assert(first->type == DAV1D_TASK_TYPE_TILE_RECONSTRUCTION || |
155 | 0 | first->type == DAV1D_TASK_TYPE_TILE_ENTROPY); |
156 | 0 | assert(first->type == t_ptr->type); |
157 | 0 | assert(t_ptr->sby == first->sby); |
158 | 0 | const int p = first->type == DAV1D_TASK_TYPE_TILE_ENTROPY; |
159 | 0 | const int t_tile_idx = (int) (first - f->task_thread.tile_tasks[p]); |
160 | 0 | const int p_tile_idx = (int) (t_ptr - f->task_thread.tile_tasks[p]); |
161 | 0 | assert(t_tile_idx != p_tile_idx); |
162 | 0 | if (t_tile_idx > p_tile_idx) continue; |
163 | 0 | insert_tasks_between(f, first, last, prev_t, t_ptr, cond_signal); |
164 | 0 | return; |
165 | 0 | } |
166 | | // append at the end |
167 | 0 | insert_tasks_between(f, first, last, prev_t, NULL, cond_signal); |
168 | 0 | } |
169 | | |
170 | | static inline void insert_task(Dav1dFrameContext *const f, |
171 | | Dav1dTask *const t, const int cond_signal) |
172 | 0 | { |
173 | 0 | insert_tasks(f, t, t, cond_signal); |
174 | 0 | } |
175 | | |
176 | 0 | static inline void add_pending(Dav1dFrameContext *const f, Dav1dTask *const t) { |
177 | 0 | pthread_mutex_lock(&f->task_thread.pending_tasks.lock); |
178 | 0 | t->next = NULL; |
179 | 0 | if (!f->task_thread.pending_tasks.head) |
180 | 0 | f->task_thread.pending_tasks.head = t; |
181 | 0 | else |
182 | 0 | f->task_thread.pending_tasks.tail->next = t; |
183 | 0 | f->task_thread.pending_tasks.tail = t; |
184 | 0 | atomic_store(&f->task_thread.pending_tasks.merge, 1); |
185 | 0 | pthread_mutex_unlock(&f->task_thread.pending_tasks.lock); |
186 | 0 | } |
187 | | |
188 | 0 | static inline int merge_pending_frame(Dav1dFrameContext *const f) { |
189 | 0 | int const merge = atomic_load(&f->task_thread.pending_tasks.merge); |
190 | 0 | if (merge) { |
191 | 0 | pthread_mutex_lock(&f->task_thread.pending_tasks.lock); |
192 | 0 | Dav1dTask *t = f->task_thread.pending_tasks.head; |
193 | 0 | f->task_thread.pending_tasks.head = NULL; |
194 | 0 | f->task_thread.pending_tasks.tail = NULL; |
195 | 0 | atomic_store(&f->task_thread.pending_tasks.merge, 0); |
196 | 0 | pthread_mutex_unlock(&f->task_thread.pending_tasks.lock); |
197 | 0 | while (t) { |
198 | 0 | Dav1dTask *const tmp = t->next; |
199 | 0 | insert_task(f, t, 0); |
200 | 0 | t = tmp; |
201 | 0 | } |
202 | 0 | } |
203 | 0 | return merge; |
204 | 0 | } |
205 | | |
206 | 0 | static inline int merge_pending(const Dav1dContext *const c) { |
207 | 0 | int res = 0; |
208 | 0 | for (unsigned i = 0; i < c->n_fc; i++) |
209 | 0 | res |= merge_pending_frame(&c->fc[i]); |
210 | 0 | return res; |
211 | 0 | } |
212 | | |
213 | | static int create_filter_sbrow(Dav1dFrameContext *const f, |
214 | | const int pass, Dav1dTask **res_t) |
215 | 0 | { |
216 | 0 | const int has_deblock = f->frame_hdr->loopfilter.level_y[0] || |
217 | 0 | f->frame_hdr->loopfilter.level_y[1]; |
218 | 0 | const int has_cdef = f->seq_hdr->cdef; |
219 | 0 | const int has_resize = f->frame_hdr->width[0] != f->frame_hdr->width[1]; |
220 | 0 | const int has_lr = f->lf.restore_planes; |
221 | |
|
222 | 0 | Dav1dTask *tasks = f->task_thread.tasks; |
223 | 0 | const int uses_2pass = f->c->n_fc > 1; |
224 | 0 | int num_tasks = f->sbh * (1 + uses_2pass); |
225 | 0 | if (num_tasks > f->task_thread.num_tasks) { |
226 | 0 | const size_t size = sizeof(Dav1dTask) * num_tasks; |
227 | 0 | tasks = dav1d_realloc(ALLOC_COMMON_CTX, f->task_thread.tasks, size); |
228 | 0 | if (!tasks) return -1; |
229 | 0 | memset(tasks, 0, size); |
230 | 0 | f->task_thread.tasks = tasks; |
231 | 0 | f->task_thread.num_tasks = num_tasks; |
232 | 0 | } |
233 | 0 | tasks += f->sbh * (pass & 1); |
234 | |
|
235 | 0 | if (pass & 1) { |
236 | 0 | f->frame_thread.entropy_progress = 0; |
237 | 0 | } else { |
238 | 0 | const int prog_sz = ((f->sbh + 31) & ~31) >> 5; |
239 | 0 | if (prog_sz > f->frame_thread.prog_sz) { |
240 | 0 | atomic_uint *const prog = dav1d_realloc(ALLOC_COMMON_CTX, f->frame_thread.frame_progress, |
241 | 0 | 2 * prog_sz * sizeof(*prog)); |
242 | 0 | if (!prog) return -1; |
243 | 0 | f->frame_thread.frame_progress = prog; |
244 | 0 | f->frame_thread.copy_lpf_progress = prog + prog_sz; |
245 | 0 | } |
246 | 0 | f->frame_thread.prog_sz = prog_sz; |
247 | 0 | memset(f->frame_thread.frame_progress, 0, prog_sz * sizeof(atomic_uint)); |
248 | 0 | memset(f->frame_thread.copy_lpf_progress, 0, prog_sz * sizeof(atomic_uint)); |
249 | 0 | atomic_store(&f->frame_thread.deblock_progress, 0); |
250 | 0 | } |
251 | 0 | f->frame_thread.next_tile_row[pass & 1] = 0; |
252 | |
|
253 | 0 | Dav1dTask *t = &tasks[0]; |
254 | 0 | t->sby = 0; |
255 | 0 | t->recon_progress = 1; |
256 | 0 | t->deblock_progress = 0; |
257 | 0 | t->type = pass == 1 ? DAV1D_TASK_TYPE_ENTROPY_PROGRESS : |
258 | 0 | has_deblock ? DAV1D_TASK_TYPE_DEBLOCK_COLS : |
259 | 0 | has_cdef || has_lr /* i.e. LR backup */ ? DAV1D_TASK_TYPE_DEBLOCK_ROWS : |
260 | 0 | has_resize ? DAV1D_TASK_TYPE_SUPER_RESOLUTION : |
261 | 0 | DAV1D_TASK_TYPE_RECONSTRUCTION_PROGRESS; |
262 | 0 | t->frame_idx = (int)(f - f->c->fc); |
263 | |
|
264 | 0 | *res_t = t; |
265 | 0 | return 0; |
266 | 0 | } |
267 | | |
268 | | int dav1d_task_create_tile_sbrow(Dav1dFrameContext *const f, const int pass, |
269 | | const int cond_signal) |
270 | 0 | { |
271 | 0 | Dav1dTask *tasks = f->task_thread.tile_tasks[0]; |
272 | 0 | const int uses_2pass = f->c->n_fc > 1; |
273 | 0 | const int n_tasks_per_pass = f->frame_hdr->tiling.cols * f->frame_hdr->tiling.rows; |
274 | 0 | const int n_tasks = n_tasks_per_pass * (1 + uses_2pass); |
275 | 0 | if (pass < 2) { |
276 | 0 | if (n_tasks > f->task_thread.num_tile_tasks) { |
277 | 0 | const size_t size = sizeof(Dav1dTask) * n_tasks; |
278 | 0 | tasks = dav1d_realloc(ALLOC_COMMON_CTX, f->task_thread.tile_tasks[0], size); |
279 | 0 | if (!tasks) return -1; |
280 | 0 | memset(tasks, 0, size); |
281 | 0 | f->task_thread.tile_tasks[0] = tasks; |
282 | 0 | f->task_thread.num_tile_tasks = n_tasks; |
283 | 0 | } |
284 | 0 | f->task_thread.tile_tasks[1] = tasks + n_tasks_per_pass; |
285 | 0 | } |
286 | 0 | assert(n_tasks <= f->task_thread.num_tile_tasks); |
287 | | |
288 | 0 | Dav1dTask *pf_t; |
289 | 0 | if (create_filter_sbrow(f, pass, &pf_t)) |
290 | 0 | return -1; |
291 | | |
292 | 0 | Dav1dTask *const p1_tasks = f->task_thread.tile_tasks[1]; |
293 | 0 | Dav1dTask *prev_t = NULL; |
294 | 0 | if (pass == 2) { |
295 | 0 | prev_t = &p1_tasks[n_tasks_per_pass - 1]; |
296 | | // PF task is scheduled after the last sby=0 TILE task |
297 | 0 | if (f->frame_hdr->tiling.rows == 1) |
298 | 0 | prev_t = prev_t->next; |
299 | 0 | } |
300 | 0 | tasks += (pass & 1) * n_tasks_per_pass; |
301 | 0 | for (int tile_idx = 0; tile_idx < n_tasks_per_pass; tile_idx++) { |
302 | 0 | Dav1dTileState *const ts = &f->ts[tile_idx]; |
303 | 0 | Dav1dTask *t = &tasks[tile_idx]; |
304 | 0 | t->sby = ts->tiling.row_start >> f->sb_shift; |
305 | 0 | if (pf_t && t->sby) { |
306 | 0 | prev_t->next = pf_t; |
307 | 0 | prev_t = pf_t; |
308 | 0 | pf_t = NULL; |
309 | 0 | } |
310 | 0 | t->recon_progress = 0; |
311 | 0 | t->deblock_progress = 0; |
312 | 0 | t->deps_skip = 0; |
313 | 0 | t->type = pass != 1 ? DAV1D_TASK_TYPE_TILE_RECONSTRUCTION : |
314 | 0 | DAV1D_TASK_TYPE_TILE_ENTROPY; |
315 | 0 | t->frame_idx = (int)(f - f->c->fc); |
316 | 0 | if (prev_t) prev_t->next = t; |
317 | 0 | prev_t = t; |
318 | 0 | } |
319 | 0 | if (pf_t) { |
320 | 0 | prev_t->next = pf_t; |
321 | 0 | prev_t = pf_t; |
322 | 0 | } |
323 | 0 | prev_t->next = NULL; |
324 | |
|
325 | 0 | 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 | 0 | if (!(pass & 1)) { |
331 | 0 | pthread_mutex_lock(&f->task_thread.pending_tasks.lock); |
332 | 0 | assert(f->task_thread.pending_tasks.head == NULL); |
333 | 0 | f->task_thread.pending_tasks.head = f->task_thread.tile_tasks[pass == 2]; |
334 | 0 | f->task_thread.pending_tasks.tail = prev_t; |
335 | 0 | atomic_store(&f->task_thread.pending_tasks.merge, 1); |
336 | 0 | atomic_store(&f->task_thread.init_done, 1); |
337 | 0 | pthread_mutex_unlock(&f->task_thread.pending_tasks.lock); |
338 | 0 | } |
339 | 0 | return 0; |
340 | 0 | } |
341 | | |
342 | 0 | void dav1d_task_frame_init(Dav1dFrameContext *const f) { |
343 | 0 | const Dav1dContext *const c = f->c; |
344 | |
|
345 | 0 | atomic_store(&f->task_thread.init_done, 0); |
346 | | // schedule init task, which will schedule the remaining tasks |
347 | 0 | Dav1dTask *const t = &f->task_thread.init_task; |
348 | 0 | t->type = DAV1D_TASK_TYPE_INIT; |
349 | 0 | t->frame_idx = (int)(f - c->fc); |
350 | 0 | t->sby = 0; |
351 | 0 | t->recon_progress = t->deblock_progress = 0; |
352 | 0 | insert_task(f, t, 1); |
353 | 0 | } |
354 | | |
355 | | void dav1d_task_delayed_fg(Dav1dContext *const c, Dav1dPicture *const out, |
356 | | const Dav1dPicture *const in) |
357 | 0 | { |
358 | 0 | struct TaskThreadData *const ttd = &c->task_thread; |
359 | 0 | ttd->delayed_fg.in = in; |
360 | 0 | ttd->delayed_fg.out = out; |
361 | 0 | ttd->delayed_fg.type = DAV1D_TASK_TYPE_FG_PREP; |
362 | 0 | atomic_init(&ttd->delayed_fg.progress[0], 0); |
363 | 0 | atomic_init(&ttd->delayed_fg.progress[1], 0); |
364 | 0 | pthread_mutex_lock(&ttd->lock); |
365 | 0 | ttd->delayed_fg.exec = 1; |
366 | 0 | ttd->delayed_fg.finished = 0; |
367 | 0 | pthread_cond_signal(&ttd->cond); |
368 | 0 | do { |
369 | 0 | pthread_cond_wait(&ttd->delayed_fg.cond, &ttd->lock); |
370 | 0 | } while (!ttd->delayed_fg.finished); |
371 | 0 | pthread_mutex_unlock(&ttd->lock); |
372 | 0 | } |
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 | 0 | { |
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 | 0 | int p1 = atomic_load(state); |
382 | 0 | if (p1 < t->sby) { |
383 | 0 | t->type = type; |
384 | 0 | t->recon_progress = t->deblock_progress = 0; |
385 | 0 | *target = t->sby; |
386 | 0 | add_pending(f, t); |
387 | 0 | pthread_mutex_lock(&ttd->lock); |
388 | 0 | return 1; |
389 | 0 | } |
390 | 0 | return 0; |
391 | 0 | } |
392 | | |
393 | | static inline int check_tile(Dav1dTask *const t, Dav1dFrameContext *const f, |
394 | | const int frame_mt) |
395 | 0 | { |
396 | 0 | const int tp = t->type == DAV1D_TASK_TYPE_TILE_ENTROPY; |
397 | 0 | const int tile_idx = (int)(t - f->task_thread.tile_tasks[tp]); |
398 | 0 | Dav1dTileState *const ts = &f->ts[tile_idx]; |
399 | 0 | const int p1 = atomic_load(&ts->progress[tp]); |
400 | 0 | if (p1 < t->sby) return 1; |
401 | 0 | int error = p1 == TILE_ERROR; |
402 | 0 | error |= atomic_fetch_or(&f->task_thread.error, error); |
403 | 0 | if (!error && frame_mt && !tp) { |
404 | 0 | const int p2 = atomic_load(&ts->progress[1]); |
405 | 0 | if (p2 <= t->sby) return 1; |
406 | 0 | error = p2 == TILE_ERROR; |
407 | 0 | error |= atomic_fetch_or(&f->task_thread.error, error); |
408 | 0 | } |
409 | 0 | if (!error && frame_mt && !IS_KEY_OR_INTRA(f->frame_hdr)) { |
410 | | // check reference state |
411 | 0 | const Dav1dThreadPicture *p = &f->sr_cur; |
412 | 0 | const int ss_ver = p->p.p.layout == DAV1D_PIXEL_LAYOUT_I420; |
413 | 0 | const unsigned p_b = (t->sby + 1) << (f->sb_shift + 2); |
414 | 0 | const int tile_sby = t->sby - (ts->tiling.row_start >> f->sb_shift); |
415 | 0 | const int (*const lowest_px)[2] = ts->lowest_pixel[tile_sby]; |
416 | 0 | for (int n = t->deps_skip; n < 7; n++, t->deps_skip++) { |
417 | 0 | unsigned lowest; |
418 | 0 | 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 | 0 | lowest = p_b; |
423 | 0 | } else { |
424 | | // +8 is postfilter-induced delay |
425 | 0 | const int y = lowest_px[n][0] == INT_MIN ? INT_MIN : |
426 | 0 | lowest_px[n][0] + 8; |
427 | 0 | const int uv = lowest_px[n][1] == INT_MIN ? INT_MIN : |
428 | 0 | lowest_px[n][1] * (1 << ss_ver) + 8; |
429 | 0 | const int max = imax(y, uv); |
430 | 0 | if (max == INT_MIN) continue; |
431 | 0 | lowest = iclip(max, 1, f->refp[n].p.p.h); |
432 | 0 | } |
433 | 0 | const unsigned p3 = atomic_load(&f->refp[n].progress[!tp]); |
434 | 0 | if (p3 < lowest) return 1; |
435 | 0 | atomic_fetch_or(&f->task_thread.error, p3 == FRAME_ERROR); |
436 | 0 | } |
437 | 0 | } |
438 | 0 | return 0; |
439 | 0 | } |
440 | | |
441 | | static inline int get_frame_progress(const Dav1dContext *const c, |
442 | | const Dav1dFrameContext *const f) |
443 | 0 | { |
444 | 0 | unsigned frame_prog = c->n_fc > 1 ? atomic_load(&f->sr_cur.progress[1]) : 0; |
445 | 0 | if (frame_prog >= FRAME_ERROR) |
446 | 0 | return f->sbh - 1; |
447 | 0 | int idx = frame_prog >> (f->sb_shift + 7); |
448 | 0 | int prog; |
449 | 0 | do { |
450 | 0 | atomic_uint *state = &f->frame_thread.frame_progress[idx]; |
451 | 0 | const unsigned val = ~atomic_load(state); |
452 | 0 | prog = val ? ctz(val) : 32; |
453 | 0 | if (prog != 32) break; |
454 | 0 | prog = 0; |
455 | 0 | } while (++idx < f->frame_thread.prog_sz); |
456 | 0 | return ((idx << 5) | prog) - 1; |
457 | 0 | } |
458 | | |
459 | 0 | static inline void abort_frame(Dav1dFrameContext *const f, const int error) { |
460 | 0 | atomic_store(&f->task_thread.error, error == DAV1D_ERR(EINVAL) ? 1 : -1); |
461 | 0 | atomic_store(&f->task_thread.task_counter, 0); |
462 | 0 | atomic_store(&f->task_thread.done[0], 1); |
463 | 0 | atomic_store(&f->task_thread.done[1], 1); |
464 | 0 | atomic_store(&f->sr_cur.progress[0], FRAME_ERROR); |
465 | 0 | atomic_store(&f->sr_cur.progress[1], FRAME_ERROR); |
466 | 0 | dav1d_decode_frame_exit(f, error); |
467 | 0 | f->n_tile_data = 0; |
468 | 0 | pthread_cond_signal(&f->task_thread.cond); |
469 | 0 | } |
470 | | |
471 | | static inline void delayed_fg_task(const Dav1dContext *const c, |
472 | | struct TaskThreadData *const ttd) |
473 | 0 | { |
474 | 0 | const Dav1dPicture *const in = ttd->delayed_fg.in; |
475 | 0 | Dav1dPicture *const out = ttd->delayed_fg.out; |
476 | 0 | #if CONFIG_16BPC |
477 | 0 | int off; |
478 | 0 | if (out->p.bpc != 8) |
479 | 0 | off = (out->p.bpc >> 1) - 4; |
480 | 0 | #endif |
481 | 0 | switch (ttd->delayed_fg.type) { |
482 | 0 | case DAV1D_TASK_TYPE_FG_PREP: |
483 | 0 | ttd->delayed_fg.exec = 0; |
484 | 0 | if (atomic_load(&ttd->cond_signaled)) |
485 | 0 | pthread_cond_signal(&ttd->cond); |
486 | 0 | pthread_mutex_unlock(&ttd->lock); |
487 | 0 | switch (out->p.bpc) { |
488 | 0 | #if CONFIG_8BPC |
489 | 0 | case 8: |
490 | 0 | dav1d_prep_grain_8bpc(&c->dsp[0].fg, out, in, |
491 | 0 | ttd->delayed_fg.scaling_8bpc, |
492 | 0 | ttd->delayed_fg.grain_lut_8bpc); |
493 | 0 | break; |
494 | 0 | #endif |
495 | 0 | #if CONFIG_16BPC |
496 | 0 | case 10: |
497 | 0 | case 12: |
498 | 0 | dav1d_prep_grain_16bpc(&c->dsp[off].fg, out, in, |
499 | 0 | ttd->delayed_fg.scaling_16bpc, |
500 | 0 | ttd->delayed_fg.grain_lut_16bpc); |
501 | 0 | break; |
502 | 0 | #endif |
503 | 0 | default: abort(); |
504 | 0 | } |
505 | 0 | ttd->delayed_fg.type = DAV1D_TASK_TYPE_FG_APPLY; |
506 | 0 | pthread_mutex_lock(&ttd->lock); |
507 | 0 | ttd->delayed_fg.exec = 1; |
508 | | // fall-through |
509 | 0 | case DAV1D_TASK_TYPE_FG_APPLY:; |
510 | 0 | int row = atomic_fetch_add(&ttd->delayed_fg.progress[0], 1); |
511 | 0 | pthread_mutex_unlock(&ttd->lock); |
512 | 0 | int progmax = (out->p.h + FG_BLOCK_SIZE - 1) / FG_BLOCK_SIZE; |
513 | 0 | while (row < progmax) { |
514 | 0 | if (row + 1 < progmax) |
515 | 0 | pthread_cond_signal(&ttd->cond); |
516 | 0 | else { |
517 | 0 | pthread_mutex_lock(&ttd->lock); |
518 | 0 | ttd->delayed_fg.exec = 0; |
519 | 0 | pthread_mutex_unlock(&ttd->lock); |
520 | 0 | } |
521 | 0 | switch (out->p.bpc) { |
522 | 0 | #if CONFIG_8BPC |
523 | 0 | case 8: |
524 | 0 | dav1d_apply_grain_row_8bpc(&c->dsp[0].fg, out, in, |
525 | 0 | ttd->delayed_fg.scaling_8bpc, |
526 | 0 | ttd->delayed_fg.grain_lut_8bpc, row); |
527 | 0 | break; |
528 | 0 | #endif |
529 | 0 | #if CONFIG_16BPC |
530 | 0 | case 10: |
531 | 0 | case 12: |
532 | 0 | dav1d_apply_grain_row_16bpc(&c->dsp[off].fg, out, in, |
533 | 0 | ttd->delayed_fg.scaling_16bpc, |
534 | 0 | ttd->delayed_fg.grain_lut_16bpc, row); |
535 | 0 | break; |
536 | 0 | #endif |
537 | 0 | default: abort(); |
538 | 0 | } |
539 | 0 | row = atomic_fetch_add(&ttd->delayed_fg.progress[0], 1); |
540 | 0 | atomic_fetch_add(&ttd->delayed_fg.progress[1], 1); |
541 | 0 | } |
542 | 0 | pthread_mutex_lock(&ttd->lock); |
543 | 0 | ttd->delayed_fg.exec = 0; |
544 | 0 | int done = atomic_fetch_add(&ttd->delayed_fg.progress[1], 1) + 1; |
545 | 0 | progmax = atomic_load(&ttd->delayed_fg.progress[0]); |
546 | | // signal for completion only once the last runner reaches this |
547 | 0 | if (done >= progmax) { |
548 | 0 | ttd->delayed_fg.finished = 1; |
549 | 0 | pthread_cond_signal(&ttd->delayed_fg.cond); |
550 | 0 | } |
551 | 0 | break; |
552 | 0 | default: abort(); |
553 | 0 | } |
554 | 0 | } |
555 | | |
556 | 0 | void *dav1d_worker_task(void *data) { |
557 | 0 | Dav1dTaskContext *const tc = data; |
558 | 0 | const Dav1dContext *const c = tc->c; |
559 | 0 | struct TaskThreadData *const ttd = tc->task_thread.ttd; |
560 | |
|
561 | 0 | dav1d_set_thread_name("dav1d-worker"); |
562 | |
|
563 | 0 | pthread_mutex_lock(&ttd->lock); |
564 | 0 | for (;;) { |
565 | 0 | if (tc->task_thread.die) break; |
566 | 0 | if (atomic_load(c->flush)) goto park; |
567 | | |
568 | 0 | merge_pending(c); |
569 | 0 | if (ttd->delayed_fg.exec) { // run delayed film grain first |
570 | 0 | delayed_fg_task(c, ttd); |
571 | 0 | continue; |
572 | 0 | } |
573 | 0 | Dav1dFrameContext *f; |
574 | 0 | Dav1dTask *t, *prev_t = NULL; |
575 | 0 | if (c->n_fc > 1) { // run init tasks second |
576 | 0 | for (unsigned i = 0; i < c->n_fc; i++) { |
577 | 0 | const unsigned first = atomic_load(&ttd->first); |
578 | 0 | f = &c->fc[(first + i) % c->n_fc]; |
579 | 0 | if (atomic_load(&f->task_thread.init_done)) continue; |
580 | 0 | t = f->task_thread.task_head; |
581 | 0 | if (!t) continue; |
582 | 0 | if (t->type == DAV1D_TASK_TYPE_INIT) goto found; |
583 | 0 | 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 | 0 | const int p1 = f->in_cdf.progress ? |
592 | 0 | atomic_load(f->in_cdf.progress) : 1; |
593 | 0 | if (p1) { |
594 | 0 | atomic_fetch_or(&f->task_thread.error, p1 == TILE_ERROR); |
595 | 0 | goto found; |
596 | 0 | } |
597 | 0 | } |
598 | 0 | } |
599 | 0 | } |
600 | 0 | while (ttd->cur < c->n_fc) { // run decoding tasks last |
601 | 0 | const unsigned first = atomic_load(&ttd->first); |
602 | 0 | f = &c->fc[(first + ttd->cur) % c->n_fc]; |
603 | 0 | merge_pending_frame(f); |
604 | 0 | prev_t = f->task_thread.task_cur_prev; |
605 | 0 | t = prev_t ? prev_t->next : f->task_thread.task_head; |
606 | 0 | while (t) { |
607 | 0 | if (t->type == DAV1D_TASK_TYPE_INIT_CDF) goto next; |
608 | 0 | else if (t->type == DAV1D_TASK_TYPE_TILE_ENTROPY || |
609 | 0 | t->type == DAV1D_TASK_TYPE_TILE_RECONSTRUCTION) |
610 | 0 | { |
611 | | // if not bottom sbrow of tile, this task will be re-added |
612 | | // after it's finished |
613 | 0 | if (!check_tile(t, f, c->n_fc > 1)) |
614 | 0 | goto found; |
615 | 0 | } else if (t->recon_progress) { |
616 | 0 | const int p = t->type == DAV1D_TASK_TYPE_ENTROPY_PROGRESS; |
617 | 0 | int error = atomic_load(&f->task_thread.error); |
618 | 0 | assert(!atomic_load(&f->task_thread.done[p]) || error); |
619 | 0 | const int tile_row_base = f->frame_hdr->tiling.cols * |
620 | 0 | f->frame_thread.next_tile_row[p]; |
621 | 0 | if (p) { |
622 | 0 | atomic_int *const prog = &f->frame_thread.entropy_progress; |
623 | 0 | const int p1 = atomic_load(prog); |
624 | 0 | if (p1 < t->sby) goto next; |
625 | 0 | atomic_fetch_or(&f->task_thread.error, p1 == TILE_ERROR); |
626 | 0 | } |
627 | 0 | for (int tc = 0; tc < f->frame_hdr->tiling.cols; tc++) { |
628 | 0 | Dav1dTileState *const ts = &f->ts[tile_row_base + tc]; |
629 | 0 | const int p2 = atomic_load(&ts->progress[p]); |
630 | 0 | if (p2 < t->recon_progress) goto next; |
631 | 0 | atomic_fetch_or(&f->task_thread.error, p2 == TILE_ERROR); |
632 | 0 | } |
633 | 0 | if (t->sby + 1 < f->sbh) { |
634 | | // add sby+1 to list to replace this one |
635 | 0 | Dav1dTask *next_t = &t[1]; |
636 | 0 | *next_t = *t; |
637 | 0 | next_t->sby++; |
638 | 0 | const int ntr = f->frame_thread.next_tile_row[p] + 1; |
639 | 0 | const int start = f->frame_hdr->tiling.row_start_sb[ntr]; |
640 | 0 | if (next_t->sby == start) |
641 | 0 | f->frame_thread.next_tile_row[p] = ntr; |
642 | 0 | next_t->recon_progress = next_t->sby + 1; |
643 | 0 | insert_task(f, next_t, 0); |
644 | 0 | } |
645 | 0 | goto found; |
646 | 0 | } else if (t->type == DAV1D_TASK_TYPE_CDEF) { |
647 | 0 | atomic_uint *prog = f->frame_thread.copy_lpf_progress; |
648 | 0 | const int p1 = atomic_load(&prog[(t->sby - 1) >> 5]); |
649 | 0 | if (p1 & (1U << ((t->sby - 1) & 31))) |
650 | 0 | goto found; |
651 | 0 | } else { |
652 | 0 | assert(t->deblock_progress); |
653 | 0 | const int p1 = atomic_load(&f->frame_thread.deblock_progress); |
654 | 0 | if (p1 >= t->deblock_progress) { |
655 | 0 | atomic_fetch_or(&f->task_thread.error, p1 == TILE_ERROR); |
656 | 0 | goto found; |
657 | 0 | } |
658 | 0 | } |
659 | 0 | next: |
660 | 0 | prev_t = t; |
661 | 0 | t = t->next; |
662 | 0 | f->task_thread.task_cur_prev = prev_t; |
663 | 0 | } |
664 | 0 | ttd->cur++; |
665 | 0 | } |
666 | 0 | if (reset_task_cur(c, ttd, UINT_MAX)) continue; |
667 | 0 | if (merge_pending(c)) continue; |
668 | 0 | park: |
669 | 0 | tc->task_thread.flushed = 1; |
670 | 0 | pthread_cond_signal(&tc->task_thread.td.cond); |
671 | | // we want to be woken up next time progress is signaled |
672 | 0 | atomic_store(&ttd->cond_signaled, 0); |
673 | 0 | pthread_cond_wait(&ttd->cond, &ttd->lock); |
674 | 0 | tc->task_thread.flushed = 0; |
675 | 0 | reset_task_cur(c, ttd, UINT_MAX); |
676 | 0 | continue; |
677 | | |
678 | 0 | found: |
679 | | // remove t from list |
680 | 0 | if (prev_t) prev_t->next = t->next; |
681 | 0 | else f->task_thread.task_head = t->next; |
682 | 0 | if (!t->next) f->task_thread.task_tail = prev_t; |
683 | 0 | if (t->type > DAV1D_TASK_TYPE_INIT_CDF && !f->task_thread.task_head) |
684 | 0 | ttd->cur++; |
685 | 0 | 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 | 0 | atomic_store(&ttd->cond_signaled, 1); |
690 | 0 | pthread_cond_signal(&ttd->cond); |
691 | 0 | pthread_mutex_unlock(&ttd->lock); |
692 | 0 | found_unlocked:; |
693 | 0 | const int flush = atomic_load(c->flush); |
694 | 0 | int error = atomic_fetch_or(&f->task_thread.error, flush) | flush; |
695 | | |
696 | | // run it |
697 | 0 | tc->f = f; |
698 | 0 | int sby = t->sby; |
699 | 0 | switch (t->type) { |
700 | 0 | case DAV1D_TASK_TYPE_INIT: { |
701 | 0 | assert(c->n_fc > 1); |
702 | 0 | int res = dav1d_decode_frame_init(f); |
703 | 0 | int p1 = f->in_cdf.progress ? atomic_load(f->in_cdf.progress) : 1; |
704 | 0 | if (res || p1 == TILE_ERROR) { |
705 | 0 | pthread_mutex_lock(&ttd->lock); |
706 | 0 | abort_frame(f, res ? res : DAV1D_ERR(EINVAL)); |
707 | 0 | reset_task_cur(c, ttd, t->frame_idx); |
708 | 0 | } else { |
709 | 0 | t->type = DAV1D_TASK_TYPE_INIT_CDF; |
710 | 0 | if (p1) goto found_unlocked; |
711 | 0 | add_pending(f, t); |
712 | 0 | pthread_mutex_lock(&ttd->lock); |
713 | 0 | } |
714 | 0 | continue; |
715 | 0 | } |
716 | 0 | case DAV1D_TASK_TYPE_INIT_CDF: { |
717 | 0 | assert(c->n_fc > 1); |
718 | 0 | int res = DAV1D_ERR(EINVAL); |
719 | 0 | if (!atomic_load(&f->task_thread.error)) |
720 | 0 | res = dav1d_decode_frame_init_cdf(f); |
721 | 0 | if (f->frame_hdr->refresh_context && !f->task_thread.update_set) |
722 | 0 | atomic_store(f->out_cdf.progress, res < 0 ? TILE_ERROR : 1); |
723 | 0 | for (int p = 1; p <= 2 && !res; p++) |
724 | 0 | res = dav1d_task_create_tile_sbrow(f, p, 0); |
725 | 0 | pthread_mutex_lock(&ttd->lock); |
726 | 0 | if (res) { |
727 | 0 | abort_frame(f, DAV1D_ERR(ENOMEM)); |
728 | 0 | reset_task_cur(c, ttd, t->frame_idx); |
729 | 0 | atomic_store(&f->task_thread.init_done, 1); |
730 | 0 | } |
731 | 0 | continue; |
732 | 0 | } |
733 | 0 | case DAV1D_TASK_TYPE_TILE_ENTROPY: |
734 | 0 | case DAV1D_TASK_TYPE_TILE_RECONSTRUCTION: { |
735 | 0 | const int p = t->type == DAV1D_TASK_TYPE_TILE_ENTROPY; |
736 | 0 | const int tile_idx = (int)(t - f->task_thread.tile_tasks[p]); |
737 | 0 | Dav1dTileState *const ts = &f->ts[tile_idx]; |
738 | |
|
739 | 0 | tc->ts = ts; |
740 | 0 | tc->by = sby << f->sb_shift; |
741 | 0 | const int uses_2pass = c->n_fc > 1; |
742 | 0 | tc->frame_thread.pass = !uses_2pass ? 0 : |
743 | 0 | 1 + (t->type == DAV1D_TASK_TYPE_TILE_RECONSTRUCTION); |
744 | 0 | if (!error) error = dav1d_decode_tile_sbrow(tc); |
745 | 0 | const int progress = error ? TILE_ERROR : 1 + sby; |
746 | | |
747 | | // signal progress |
748 | 0 | atomic_fetch_or(&f->task_thread.error, error); |
749 | 0 | if (((sby + 1) << f->sb_shift) < ts->tiling.row_end) { |
750 | 0 | t->sby++; |
751 | 0 | t->deps_skip = 0; |
752 | 0 | if (!check_tile(t, f, uses_2pass)) { |
753 | 0 | atomic_store(&ts->progress[p], progress); |
754 | 0 | reset_task_cur_async(ttd, t->frame_idx, c->n_fc); |
755 | 0 | if (!atomic_fetch_or(&ttd->cond_signaled, 1)) |
756 | 0 | pthread_cond_signal(&ttd->cond); |
757 | 0 | goto found_unlocked; |
758 | 0 | } |
759 | 0 | atomic_store(&ts->progress[p], progress); |
760 | 0 | add_pending(f, t); |
761 | 0 | pthread_mutex_lock(&ttd->lock); |
762 | 0 | } else { |
763 | 0 | pthread_mutex_lock(&ttd->lock); |
764 | 0 | atomic_store(&ts->progress[p], progress); |
765 | 0 | reset_task_cur(c, ttd, t->frame_idx); |
766 | 0 | error = atomic_load(&f->task_thread.error); |
767 | 0 | if (f->frame_hdr->refresh_context && |
768 | 0 | tc->frame_thread.pass <= 1 && f->task_thread.update_set && |
769 | 0 | f->frame_hdr->tiling.update == tile_idx) |
770 | 0 | { |
771 | 0 | if (!error) |
772 | 0 | dav1d_cdf_thread_update(f->frame_hdr, f->out_cdf.data.cdf, |
773 | 0 | &f->ts[f->frame_hdr->tiling.update].cdf); |
774 | 0 | if (c->n_fc > 1) |
775 | 0 | atomic_store(f->out_cdf.progress, error ? TILE_ERROR : 1); |
776 | 0 | } |
777 | 0 | if (atomic_fetch_sub(&f->task_thread.task_counter, 1) - 1 == 0 && |
778 | 0 | atomic_load(&f->task_thread.done[0]) && |
779 | 0 | (!uses_2pass || atomic_load(&f->task_thread.done[1]))) |
780 | 0 | { |
781 | 0 | error = atomic_load(&f->task_thread.error); |
782 | 0 | dav1d_decode_frame_exit(f, error == 1 ? DAV1D_ERR(EINVAL) : |
783 | 0 | error ? DAV1D_ERR(ENOMEM) : 0); |
784 | 0 | f->n_tile_data = 0; |
785 | 0 | pthread_cond_signal(&f->task_thread.cond); |
786 | 0 | } |
787 | 0 | assert(atomic_load(&f->task_thread.task_counter) >= 0); |
788 | 0 | if (!atomic_fetch_or(&ttd->cond_signaled, 1)) |
789 | 0 | pthread_cond_signal(&ttd->cond); |
790 | 0 | } |
791 | 0 | continue; |
792 | 0 | } |
793 | 0 | case DAV1D_TASK_TYPE_DEBLOCK_COLS: |
794 | 0 | if (!atomic_load(&f->task_thread.error)) |
795 | 0 | f->bd_fn.filter_sbrow_deblock_cols(f, sby); |
796 | 0 | if (ensure_progress(ttd, f, t, DAV1D_TASK_TYPE_DEBLOCK_ROWS, |
797 | 0 | &f->frame_thread.deblock_progress, |
798 | 0 | &t->deblock_progress)) continue; |
799 | | // fall-through |
800 | 0 | case DAV1D_TASK_TYPE_DEBLOCK_ROWS: |
801 | 0 | if (!atomic_load(&f->task_thread.error)) |
802 | 0 | f->bd_fn.filter_sbrow_deblock_rows(f, sby); |
803 | | // signal deblock progress |
804 | 0 | if (f->frame_hdr->loopfilter.level_y[0] || |
805 | 0 | f->frame_hdr->loopfilter.level_y[1]) |
806 | 0 | { |
807 | 0 | error = atomic_load(&f->task_thread.error); |
808 | 0 | atomic_store(&f->frame_thread.deblock_progress, |
809 | 0 | error ? TILE_ERROR : sby + 1); |
810 | 0 | reset_task_cur_async(ttd, t->frame_idx, c->n_fc); |
811 | 0 | if (!atomic_fetch_or(&ttd->cond_signaled, 1)) |
812 | 0 | pthread_cond_signal(&ttd->cond); |
813 | 0 | } else if (f->seq_hdr->cdef || f->lf.restore_planes) { |
814 | 0 | atomic_fetch_or(&f->frame_thread.copy_lpf_progress[sby >> 5], |
815 | 0 | 1U << (sby & 31)); |
816 | | // CDEF needs the top buffer to be saved by lr_copy_lpf of the |
817 | | // previous sbrow |
818 | 0 | if (sby) { |
819 | 0 | int prog = atomic_load(&f->frame_thread.copy_lpf_progress[(sby - 1) >> 5]); |
820 | 0 | if (~prog & (1U << ((sby - 1) & 31))) { |
821 | 0 | t->type = DAV1D_TASK_TYPE_CDEF; |
822 | 0 | t->recon_progress = t->deblock_progress = 0; |
823 | 0 | add_pending(f, t); |
824 | 0 | pthread_mutex_lock(&ttd->lock); |
825 | 0 | continue; |
826 | 0 | } |
827 | 0 | } |
828 | 0 | } |
829 | | // fall-through |
830 | 0 | case DAV1D_TASK_TYPE_CDEF: |
831 | 0 | if (f->seq_hdr->cdef) { |
832 | 0 | if (!atomic_load(&f->task_thread.error)) |
833 | 0 | f->bd_fn.filter_sbrow_cdef(tc, sby); |
834 | 0 | reset_task_cur_async(ttd, t->frame_idx, c->n_fc); |
835 | 0 | if (!atomic_fetch_or(&ttd->cond_signaled, 1)) |
836 | 0 | pthread_cond_signal(&ttd->cond); |
837 | 0 | } |
838 | | // fall-through |
839 | 0 | case DAV1D_TASK_TYPE_SUPER_RESOLUTION: |
840 | 0 | if (f->frame_hdr->width[0] != f->frame_hdr->width[1]) |
841 | 0 | if (!atomic_load(&f->task_thread.error)) |
842 | 0 | f->bd_fn.filter_sbrow_resize(f, sby); |
843 | | // fall-through |
844 | 0 | case DAV1D_TASK_TYPE_LOOP_RESTORATION: |
845 | 0 | if (!atomic_load(&f->task_thread.error) && f->lf.restore_planes) |
846 | 0 | f->bd_fn.filter_sbrow_lr(f, sby); |
847 | | // fall-through |
848 | 0 | case DAV1D_TASK_TYPE_RECONSTRUCTION_PROGRESS: |
849 | | // dummy to cover for no post-filters |
850 | 0 | case DAV1D_TASK_TYPE_ENTROPY_PROGRESS: |
851 | | // dummy to convert tile progress to frame |
852 | 0 | break; |
853 | 0 | default: abort(); |
854 | 0 | } |
855 | | // if task completed [typically LR], signal picture progress as per below |
856 | 0 | const int uses_2pass = c->n_fc > 1; |
857 | 0 | const int sbh = f->sbh; |
858 | 0 | const int sbsz = f->sb_step * 4; |
859 | 0 | if (t->type == DAV1D_TASK_TYPE_ENTROPY_PROGRESS) { |
860 | 0 | error = atomic_load(&f->task_thread.error); |
861 | 0 | const unsigned y = sby + 1 == sbh ? UINT_MAX : (unsigned)(sby + 1) * sbsz; |
862 | 0 | assert(c->n_fc > 1); |
863 | 0 | if (f->sr_cur.p.data[0] /* upon flush, this can be free'ed already */) |
864 | 0 | atomic_store(&f->sr_cur.progress[0], error ? FRAME_ERROR : y); |
865 | 0 | atomic_store(&f->frame_thread.entropy_progress, |
866 | 0 | error ? TILE_ERROR : sby + 1); |
867 | 0 | if (sby + 1 == sbh) |
868 | 0 | atomic_store(&f->task_thread.done[1], 1); |
869 | 0 | pthread_mutex_lock(&ttd->lock); |
870 | 0 | const int num_tasks = atomic_fetch_sub(&f->task_thread.task_counter, 1) - 1; |
871 | 0 | if (sby + 1 < sbh && num_tasks) { |
872 | 0 | reset_task_cur(c, ttd, t->frame_idx); |
873 | 0 | continue; |
874 | 0 | } |
875 | 0 | if (!num_tasks && atomic_load(&f->task_thread.done[0]) && |
876 | 0 | atomic_load(&f->task_thread.done[1])) |
877 | 0 | { |
878 | 0 | error = atomic_load(&f->task_thread.error); |
879 | 0 | dav1d_decode_frame_exit(f, error == 1 ? DAV1D_ERR(EINVAL) : |
880 | 0 | error ? DAV1D_ERR(ENOMEM) : 0); |
881 | 0 | f->n_tile_data = 0; |
882 | 0 | pthread_cond_signal(&f->task_thread.cond); |
883 | 0 | } |
884 | 0 | reset_task_cur(c, ttd, t->frame_idx); |
885 | 0 | continue; |
886 | 0 | } |
887 | | // t->type != DAV1D_TASK_TYPE_ENTROPY_PROGRESS |
888 | 0 | atomic_fetch_or(&f->frame_thread.frame_progress[sby >> 5], |
889 | 0 | 1U << (sby & 31)); |
890 | 0 | pthread_mutex_lock(&f->task_thread.lock); |
891 | 0 | sby = get_frame_progress(c, f); |
892 | 0 | error = atomic_load(&f->task_thread.error); |
893 | 0 | const unsigned y = sby + 1 == sbh ? UINT_MAX : (unsigned)(sby + 1) * sbsz; |
894 | 0 | if (c->n_fc > 1 && f->sr_cur.p.data[0] /* upon flush, this can be free'ed already */) |
895 | 0 | atomic_store(&f->sr_cur.progress[1], error ? FRAME_ERROR : y); |
896 | 0 | pthread_mutex_unlock(&f->task_thread.lock); |
897 | 0 | if (sby + 1 == sbh) |
898 | 0 | atomic_store(&f->task_thread.done[0], 1); |
899 | 0 | pthread_mutex_lock(&ttd->lock); |
900 | 0 | const int num_tasks = atomic_fetch_sub(&f->task_thread.task_counter, 1) - 1; |
901 | 0 | if (sby + 1 < sbh && num_tasks) { |
902 | 0 | reset_task_cur(c, ttd, t->frame_idx); |
903 | 0 | continue; |
904 | 0 | } |
905 | 0 | if (!num_tasks && atomic_load(&f->task_thread.done[0]) && |
906 | 0 | (!uses_2pass || atomic_load(&f->task_thread.done[1]))) |
907 | 0 | { |
908 | 0 | error = atomic_load(&f->task_thread.error); |
909 | 0 | dav1d_decode_frame_exit(f, error == 1 ? DAV1D_ERR(EINVAL) : |
910 | 0 | error ? DAV1D_ERR(ENOMEM) : 0); |
911 | 0 | f->n_tile_data = 0; |
912 | 0 | pthread_cond_signal(&f->task_thread.cond); |
913 | 0 | } |
914 | 0 | reset_task_cur(c, ttd, t->frame_idx); |
915 | 0 | } |
916 | 0 | pthread_mutex_unlock(&ttd->lock); |
917 | |
|
918 | | return NULL; |
919 | 0 | } |