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 | | #include "vcs_version.h" |
30 | | |
31 | | #include <errno.h> |
32 | | #include <string.h> |
33 | | |
34 | | #if defined(__linux__) && HAVE_DLSYM |
35 | | #include <dlfcn.h> |
36 | | #endif |
37 | | |
38 | | #include "dav1d/dav1d.h" |
39 | | #include "dav1d/data.h" |
40 | | |
41 | | #include "common/validate.h" |
42 | | |
43 | | #include "src/cpu.h" |
44 | | #include "src/fg_apply.h" |
45 | | #include "src/internal.h" |
46 | | #include "src/log.h" |
47 | | #include "src/obu.h" |
48 | | #include "src/qm.h" |
49 | | #include "src/ref.h" |
50 | | #include "src/thread_task.h" |
51 | | #include "src/wedge.h" |
52 | | |
53 | 1 | static COLD void init_internal(void) { |
54 | 1 | dav1d_init_cpu(); |
55 | 1 | dav1d_init_ii_wedge_masks(); |
56 | 1 | dav1d_init_intra_edge_tree(); |
57 | 1 | dav1d_init_qm_tables(); |
58 | 1 | dav1d_init_thread(); |
59 | 1 | } |
60 | | |
61 | 0 | COLD const char *dav1d_version(void) { |
62 | 0 | return DAV1D_VERSION; |
63 | 0 | } |
64 | | |
65 | 0 | COLD unsigned dav1d_version_api(void) { |
66 | 0 | return (DAV1D_API_VERSION_MAJOR << 16) | |
67 | 0 | (DAV1D_API_VERSION_MINOR << 8) | |
68 | 0 | (DAV1D_API_VERSION_PATCH << 0); |
69 | 0 | } |
70 | | |
71 | 9.48k | COLD void dav1d_default_settings(Dav1dSettings *const s) { |
72 | 9.48k | s->n_threads = 0; |
73 | 9.48k | s->max_frame_delay = 0; |
74 | 9.48k | s->apply_grain = 1; |
75 | 9.48k | s->allocator.cookie = NULL; |
76 | 9.48k | s->allocator.alloc_picture_callback = dav1d_default_picture_alloc; |
77 | 9.48k | s->allocator.release_picture_callback = dav1d_default_picture_release; |
78 | 9.48k | s->logger.cookie = NULL; |
79 | 9.48k | s->logger.callback = dav1d_log_default_callback; |
80 | 9.48k | s->operating_point = 0; |
81 | 9.48k | s->all_layers = 1; // just until the tests are adjusted |
82 | 9.48k | s->frame_size_limit = 0; |
83 | 9.48k | s->strict_std_compliance = 0; |
84 | 9.48k | s->output_invisible_frames = 0; |
85 | 9.48k | s->inloop_filters = DAV1D_INLOOPFILTER_ALL; |
86 | 9.48k | s->decode_frame_type = DAV1D_DECODEFRAMETYPE_ALL; |
87 | 9.48k | } |
88 | | |
89 | | static void close_internal(Dav1dContext **const c_out, int flush); |
90 | | |
91 | | #if defined(__linux__) && HAVE_DLSYM && defined(__GLIBC__) |
92 | | NO_SANITIZE("cfi-icall") // CFI is broken with dlsym() |
93 | 9.48k | static COLD size_t get_stack_size_internal(const pthread_attr_t *const thread_attr) { |
94 | | /* glibc has an issue where the size of the TLS is subtracted from the stack |
95 | | * size instead of allocated separately. As a result the specified stack |
96 | | * size may be insufficient when used in an application with large amounts |
97 | | * of TLS data. The following is a workaround to compensate for that. |
98 | | * See https://sourceware.org/bugzilla/show_bug.cgi?id=11787 */ |
99 | 9.48k | size_t (*const get_minstack)(const pthread_attr_t*) = |
100 | 9.48k | dlsym(RTLD_DEFAULT, "__pthread_get_minstack"); |
101 | 9.48k | if (get_minstack) |
102 | 9.48k | return get_minstack(thread_attr) - PTHREAD_STACK_MIN; |
103 | 0 | return 0; |
104 | 9.48k | } |
105 | | #else |
106 | | #define get_stack_size_internal(attr) (0) |
107 | | #endif |
108 | | |
109 | | static COLD void get_num_threads(Dav1dContext *const c, const Dav1dSettings *const s, |
110 | | unsigned *n_tc, unsigned *n_fc) |
111 | 9.48k | { |
112 | | /* ceil(sqrt(n)) */ |
113 | 9.48k | static const uint8_t fc_lut[49] = { |
114 | 9.48k | 1, /* 1 */ |
115 | 9.48k | 2, 2, 2, /* 2- 4 */ |
116 | 9.48k | 3, 3, 3, 3, 3, /* 5- 9 */ |
117 | 9.48k | 4, 4, 4, 4, 4, 4, 4, /* 10-16 */ |
118 | 9.48k | 5, 5, 5, 5, 5, 5, 5, 5, 5, /* 17-25 */ |
119 | 9.48k | 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, /* 26-36 */ |
120 | 9.48k | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, /* 37-49 */ |
121 | 9.48k | }; |
122 | 9.48k | *n_tc = s->n_threads ? s->n_threads : |
123 | 9.48k | iclip(dav1d_num_logical_processors(c), 1, DAV1D_MAX_THREADS); |
124 | 9.48k | *n_fc = s->max_frame_delay ? umin(s->max_frame_delay, *n_tc) : |
125 | 9.48k | *n_tc < 50 ? fc_lut[*n_tc - 1] : 8; // min(8, ceil(sqrt(n))) |
126 | 9.48k | } |
127 | | |
128 | 0 | COLD int dav1d_get_frame_delay(const Dav1dSettings *const s) { |
129 | 0 | unsigned n_tc, n_fc; |
130 | 0 | validate_input_or_ret(s != NULL, DAV1D_ERR(EINVAL)); |
131 | 0 | validate_input_or_ret(s->n_threads >= 0 && |
132 | 0 | s->n_threads <= DAV1D_MAX_THREADS, DAV1D_ERR(EINVAL)); |
133 | 0 | validate_input_or_ret(s->max_frame_delay >= 0 && |
134 | 0 | s->max_frame_delay <= DAV1D_MAX_FRAME_DELAY, DAV1D_ERR(EINVAL)); |
135 | |
|
136 | 0 | get_num_threads(NULL, s, &n_tc, &n_fc); |
137 | 0 | return n_fc; |
138 | 0 | } |
139 | | |
140 | 9.48k | COLD int dav1d_open(Dav1dContext **const c_out, const Dav1dSettings *const s) { |
141 | 9.48k | static pthread_once_t initted = PTHREAD_ONCE_INIT; |
142 | 9.48k | pthread_once(&initted, init_internal); |
143 | | |
144 | 9.48k | validate_input_or_ret(c_out != NULL, DAV1D_ERR(EINVAL)); |
145 | 9.48k | validate_input_or_ret(s != NULL, DAV1D_ERR(EINVAL)); |
146 | 9.48k | validate_input_or_ret(s->n_threads >= 0 && |
147 | 9.48k | s->n_threads <= DAV1D_MAX_THREADS, DAV1D_ERR(EINVAL)); |
148 | 9.48k | validate_input_or_ret(s->max_frame_delay >= 0 && |
149 | 9.48k | s->max_frame_delay <= DAV1D_MAX_FRAME_DELAY, DAV1D_ERR(EINVAL)); |
150 | 9.48k | validate_input_or_ret(s->allocator.alloc_picture_callback != NULL, |
151 | 9.48k | DAV1D_ERR(EINVAL)); |
152 | 9.48k | validate_input_or_ret(s->allocator.release_picture_callback != NULL, |
153 | 9.48k | DAV1D_ERR(EINVAL)); |
154 | 9.48k | validate_input_or_ret(s->operating_point >= 0 && |
155 | 9.48k | s->operating_point <= 31, DAV1D_ERR(EINVAL)); |
156 | 9.48k | validate_input_or_ret(s->decode_frame_type >= DAV1D_DECODEFRAMETYPE_ALL && |
157 | 9.48k | s->decode_frame_type <= DAV1D_DECODEFRAMETYPE_KEY, DAV1D_ERR(EINVAL)); |
158 | | |
159 | 9.48k | pthread_attr_t thread_attr; |
160 | 9.48k | if (pthread_attr_init(&thread_attr)) return DAV1D_ERR(ENOMEM); |
161 | 9.48k | size_t stack_size = 1024 * 1024 + get_stack_size_internal(&thread_attr); |
162 | | |
163 | 9.48k | pthread_attr_setstacksize(&thread_attr, stack_size); |
164 | | |
165 | 9.48k | Dav1dContext *const c = *c_out = dav1d_alloc_aligned(ALLOC_COMMON_CTX, sizeof(*c), 64); |
166 | 9.48k | if (!c) goto error; |
167 | 9.48k | memset(c, 0, sizeof(*c)); |
168 | | |
169 | 9.48k | c->allocator = s->allocator; |
170 | 9.48k | c->logger = s->logger; |
171 | 9.48k | c->apply_grain = s->apply_grain; |
172 | 9.48k | c->operating_point = s->operating_point; |
173 | 9.48k | c->all_layers = s->all_layers; |
174 | 9.48k | c->frame_size_limit = s->frame_size_limit; |
175 | 9.48k | c->strict_std_compliance = s->strict_std_compliance; |
176 | 9.48k | c->output_invisible_frames = s->output_invisible_frames; |
177 | 9.48k | c->inloop_filters = s->inloop_filters; |
178 | 9.48k | c->decode_frame_type = s->decode_frame_type; |
179 | | |
180 | 9.48k | dav1d_data_props_set_defaults(&c->cached_error_props); |
181 | | |
182 | 9.48k | if (dav1d_mem_pool_init(ALLOC_OBU_HDR, &c->seq_hdr_pool) || |
183 | 9.48k | dav1d_mem_pool_init(ALLOC_OBU_HDR, &c->frame_hdr_pool) || |
184 | 9.48k | dav1d_mem_pool_init(ALLOC_SEGMAP, &c->segmap_pool) || |
185 | 9.48k | dav1d_mem_pool_init(ALLOC_REFMVS, &c->refmvs_pool) || |
186 | 9.48k | dav1d_mem_pool_init(ALLOC_PIC_CTX, &c->pic_ctx_pool) || |
187 | 9.48k | dav1d_mem_pool_init(ALLOC_CDF, &c->cdf_pool)) |
188 | 0 | { |
189 | 0 | goto error; |
190 | 0 | } |
191 | | |
192 | 9.48k | if (c->allocator.alloc_picture_callback == dav1d_default_picture_alloc && |
193 | 9.48k | c->allocator.release_picture_callback == dav1d_default_picture_release) |
194 | 9.48k | { |
195 | 9.48k | if (c->allocator.cookie) goto error; |
196 | 9.48k | if (dav1d_mem_pool_init(ALLOC_PIC, &c->picture_pool)) goto error; |
197 | 9.48k | c->allocator.cookie = c->picture_pool; |
198 | 9.48k | } else if (c->allocator.alloc_picture_callback == dav1d_default_picture_alloc || |
199 | 0 | c->allocator.release_picture_callback == dav1d_default_picture_release) |
200 | 0 | { |
201 | 0 | goto error; |
202 | 0 | } |
203 | | |
204 | | /* On 32-bit systems extremely large frame sizes can cause overflows in |
205 | | * dav1d_decode_frame() malloc size calculations. Prevent that from occuring |
206 | | * by enforcing a maximum frame size limit, chosen to roughly correspond to |
207 | | * the largest size possible to decode without exhausting virtual memory. */ |
208 | 9.48k | if (sizeof(size_t) < 8 && s->frame_size_limit - 1 >= 8192 * 8192) { |
209 | 0 | c->frame_size_limit = 8192 * 8192; |
210 | 0 | if (s->frame_size_limit) |
211 | 0 | dav1d_log(c, "Frame size limit reduced from %u to %u.\n", |
212 | 0 | s->frame_size_limit, c->frame_size_limit); |
213 | 0 | } |
214 | | |
215 | 9.48k | c->flush = &c->flush_mem; |
216 | 9.48k | atomic_init(c->flush, 0); |
217 | | |
218 | 9.48k | get_num_threads(c, s, &c->n_tc, &c->n_fc); |
219 | | |
220 | 9.48k | c->fc = dav1d_alloc_aligned(ALLOC_THREAD_CTX, sizeof(*c->fc) * c->n_fc, 32); |
221 | 9.48k | if (!c->fc) goto error; |
222 | 9.48k | memset(c->fc, 0, sizeof(*c->fc) * c->n_fc); |
223 | | |
224 | 9.48k | c->tc = dav1d_alloc_aligned(ALLOC_THREAD_CTX, sizeof(*c->tc) * c->n_tc, 64); |
225 | 9.48k | if (!c->tc) goto error; |
226 | 9.48k | memset(c->tc, 0, sizeof(*c->tc) * c->n_tc); |
227 | 9.48k | if (c->n_tc > 1) { |
228 | 9.48k | if (pthread_mutex_init(&c->task_thread.lock, NULL)) goto error; |
229 | 9.48k | if (pthread_cond_init(&c->task_thread.cond, NULL)) { |
230 | 0 | pthread_mutex_destroy(&c->task_thread.lock); |
231 | 0 | goto error; |
232 | 0 | } |
233 | 9.48k | if (pthread_cond_init(&c->task_thread.delayed_fg.cond, NULL)) { |
234 | 0 | pthread_cond_destroy(&c->task_thread.cond); |
235 | 0 | pthread_mutex_destroy(&c->task_thread.lock); |
236 | 0 | goto error; |
237 | 0 | } |
238 | 9.48k | c->task_thread.cur = c->n_fc; |
239 | 9.48k | atomic_init(&c->task_thread.reset_task_cur, UINT_MAX); |
240 | 9.48k | atomic_init(&c->task_thread.cond_signaled, 0); |
241 | 9.48k | c->task_thread.inited = 1; |
242 | 9.48k | } |
243 | | |
244 | 9.48k | if (c->n_fc > 1) { |
245 | 9.48k | const size_t out_delayed_sz = sizeof(*c->frame_thread.out_delayed) * c->n_fc; |
246 | 9.48k | c->frame_thread.out_delayed = |
247 | 9.48k | dav1d_malloc(ALLOC_THREAD_CTX, out_delayed_sz); |
248 | 9.48k | if (!c->frame_thread.out_delayed) goto error; |
249 | 9.48k | memset(c->frame_thread.out_delayed, 0, out_delayed_sz); |
250 | 9.48k | } |
251 | 66.3k | for (unsigned n = 0; n < c->n_fc; n++) { |
252 | 56.9k | Dav1dFrameContext *const f = &c->fc[n]; |
253 | 56.9k | if (c->n_tc > 1) { |
254 | 56.9k | if (pthread_cond_init(&f->task_thread.cond, NULL)) goto error; |
255 | 56.9k | } |
256 | 56.9k | f->c = c; |
257 | 56.9k | f->task_thread.ttd = &c->task_thread; |
258 | 56.9k | f->lf.last_sharpness = -1; |
259 | 56.9k | } |
260 | | |
261 | 313k | for (unsigned m = 0; m < c->n_tc; m++) { |
262 | 303k | Dav1dTaskContext *const t = &c->tc[m]; |
263 | 303k | t->f = &c->fc[0]; |
264 | 303k | t->task_thread.ttd = &c->task_thread; |
265 | 303k | t->c = c; |
266 | 303k | memset(t->cf_16bpc, 0, sizeof(t->cf_16bpc)); |
267 | 303k | if (c->n_tc > 1) { |
268 | 303k | if (pthread_cond_init(&t->task_thread.td.cond, NULL)) goto error; |
269 | 303k | if (pthread_create(&t->task_thread.td.thread, &thread_attr, dav1d_worker_task, t)) { |
270 | 0 | pthread_cond_destroy(&t->task_thread.td.cond); |
271 | 0 | goto error; |
272 | 0 | } |
273 | 303k | t->task_thread.td.inited = 1; |
274 | 303k | } |
275 | 303k | } |
276 | 9.48k | dav1d_lf_mask_dsp_init(&c->lf_mask_dsp); |
277 | 9.48k | dav1d_pal_dsp_init(&c->pal_dsp); |
278 | 9.48k | dav1d_refmvs_dsp_init(&c->refmvs_dsp); |
279 | | |
280 | 9.48k | pthread_attr_destroy(&thread_attr); |
281 | | |
282 | 9.48k | return 0; |
283 | | |
284 | 0 | error: |
285 | 0 | if (c) close_internal(c_out, 0); |
286 | 0 | pthread_attr_destroy(&thread_attr); |
287 | 0 | return DAV1D_ERR(ENOMEM); |
288 | 9.48k | } |
289 | | |
290 | | static int has_grain(const Dav1dPicture *const pic) |
291 | 37.1k | { |
292 | 37.1k | const Dav1dFilmGrainData *fgdata = &pic->frame_hdr->film_grain.data; |
293 | 37.1k | return fgdata->num_y_points || fgdata->num_uv_points[0] || |
294 | 32.3k | fgdata->num_uv_points[1] || (fgdata->clip_to_restricted_range && |
295 | 1.70k | fgdata->chroma_scaling_from_luma); |
296 | 37.1k | } |
297 | | |
298 | | static int output_image(Dav1dContext *const c, Dav1dPicture *const out) |
299 | 34.0k | { |
300 | 34.0k | int res = 0; |
301 | | |
302 | 34.0k | Dav1dThreadPicture *const in = (c->all_layers || !c->max_spatial_id) |
303 | 34.0k | ? &c->out : &c->cache; |
304 | 34.0k | if (!c->apply_grain || !has_grain(&in->p)) { |
305 | 30.9k | dav1d_picture_move_ref(out, &in->p); |
306 | 30.9k | dav1d_thread_picture_unref(in); |
307 | 30.9k | goto end; |
308 | 30.9k | } |
309 | | |
310 | 3.08k | res = dav1d_apply_grain(c, out, &in->p); |
311 | 3.08k | dav1d_thread_picture_unref(in); |
312 | 34.0k | end: |
313 | 34.0k | if (!c->all_layers && c->max_spatial_id && c->out.p.data[0]) { |
314 | 1.78k | dav1d_thread_picture_move_ref(in, &c->out); |
315 | 1.78k | } |
316 | 34.0k | return res; |
317 | 3.08k | } |
318 | | |
319 | 476k | static int output_picture_ready(Dav1dContext *const c, const int drain) { |
320 | 476k | if (c->cached_error) return 1; |
321 | 476k | if (!c->all_layers && c->max_spatial_id) { |
322 | 89.6k | if (c->out.p.data[0] && c->cache.p.data[0]) { |
323 | 7.28k | if (c->max_spatial_id == c->cache.p.frame_hdr->spatial_id || |
324 | 5.91k | c->out.flags & PICTURE_FLAG_NEW_TEMPORAL_UNIT) |
325 | 2.55k | return 1; |
326 | 4.73k | dav1d_thread_picture_unref(&c->cache); |
327 | 4.73k | dav1d_thread_picture_move_ref(&c->cache, &c->out); |
328 | 4.73k | return 0; |
329 | 82.3k | } else if (c->cache.p.data[0] && drain) { |
330 | 880 | return 1; |
331 | 81.4k | } else if (c->out.p.data[0]) { |
332 | 1.77k | dav1d_thread_picture_move_ref(&c->cache, &c->out); |
333 | 1.77k | return 0; |
334 | 1.77k | } |
335 | 89.6k | } |
336 | | |
337 | 466k | return !!c->out.p.data[0]; |
338 | 476k | } |
339 | | |
340 | 36.2k | static int drain_picture(Dav1dContext *const c, Dav1dPicture *const out) { |
341 | 36.2k | unsigned drain_count = 0; |
342 | 36.2k | int drained = 0; |
343 | 89.9k | do { |
344 | 89.9k | const unsigned next = c->frame_thread.next; |
345 | 89.9k | Dav1dFrameContext *const f = &c->fc[next]; |
346 | 89.9k | pthread_mutex_lock(&c->task_thread.lock); |
347 | 107k | while (f->n_tile_data > 0) |
348 | 17.4k | pthread_cond_wait(&f->task_thread.cond, |
349 | 17.4k | &f->task_thread.ttd->lock); |
350 | 89.9k | Dav1dThreadPicture *const out_delayed = |
351 | 89.9k | &c->frame_thread.out_delayed[next]; |
352 | 89.9k | if (out_delayed->p.data[0] || |
353 | 89.9k | atomic_load_explicit(&f->task_thread.error, memory_order_relaxed)) |
354 | 35.0k | { |
355 | 35.0k | unsigned first = atomic_load_explicit(&c->task_thread.first, memory_order_relaxed); |
356 | 35.0k | if (first + 1U < c->n_fc) |
357 | 35.0k | atomic_fetch_add_explicit(&c->task_thread.first, 1U, memory_order_relaxed); |
358 | 4.51k | else |
359 | 35.0k | atomic_store_explicit(&c->task_thread.first, 0, memory_order_relaxed); |
360 | 35.0k | atomic_compare_exchange_strong_explicit(&c->task_thread.reset_task_cur, |
361 | 35.0k | &first, UINT_MAX, |
362 | 35.0k | memory_order_relaxed, |
363 | 35.0k | memory_order_relaxed); |
364 | 35.0k | if (c->task_thread.cur && c->task_thread.cur < c->n_fc) |
365 | 4.67k | c->task_thread.cur--; |
366 | 35.0k | drained = 1; |
367 | 54.8k | } else if (drained) { |
368 | 182 | pthread_mutex_unlock(&c->task_thread.lock); |
369 | 182 | break; |
370 | 182 | } |
371 | 89.7k | if (++c->frame_thread.next == c->n_fc) |
372 | 15.0k | c->frame_thread.next = 0; |
373 | 89.7k | pthread_mutex_unlock(&c->task_thread.lock); |
374 | 89.7k | const int error = f->task_thread.retval; |
375 | 89.7k | if (error) { |
376 | 1.92k | f->task_thread.retval = 0; |
377 | 1.92k | dav1d_data_props_copy(&c->cached_error_props, &out_delayed->p.m); |
378 | 1.92k | dav1d_thread_picture_unref(out_delayed); |
379 | 1.92k | return error; |
380 | 1.92k | } |
381 | 87.8k | if (out_delayed->p.data[0]) { |
382 | 33.0k | const unsigned progress = |
383 | 33.0k | atomic_load_explicit(&out_delayed->progress[1], |
384 | 33.0k | memory_order_relaxed); |
385 | 33.0k | if ((out_delayed->visible || c->output_invisible_frames) && |
386 | 31.0k | progress != FRAME_ERROR) |
387 | 31.0k | { |
388 | 31.0k | dav1d_thread_picture_ref(&c->out, out_delayed); |
389 | 31.0k | c->event_flags |= dav1d_picture_get_event_flags(out_delayed); |
390 | 31.0k | } |
391 | 33.0k | dav1d_thread_picture_unref(out_delayed); |
392 | 33.0k | if (output_picture_ready(c, 0)) |
393 | 28.6k | return output_image(c, out); |
394 | 33.0k | } |
395 | 87.8k | } while (++drain_count < c->n_fc); |
396 | | |
397 | 5.72k | if (output_picture_ready(c, 1)) |
398 | 880 | return output_image(c, out); |
399 | | |
400 | 4.84k | return DAV1D_ERR(EAGAIN); |
401 | 5.72k | } |
402 | | |
403 | | static int gen_picture(Dav1dContext *const c) |
404 | 209k | { |
405 | 209k | Dav1dData *const in = &c->in; |
406 | | |
407 | 209k | if (output_picture_ready(c, 0)) |
408 | 4.54k | return 0; |
409 | | |
410 | 308k | while (in->sz > 0) { |
411 | 108k | const ptrdiff_t res = dav1d_parse_obus(c, in); |
412 | 108k | if (res < 0) { |
413 | 383 | dav1d_data_unref_internal(in); |
414 | 108k | } else { |
415 | 108k | assert((size_t)res <= in->sz); |
416 | 108k | in->sz -= res; |
417 | 108k | in->data += res; |
418 | 108k | if (!in->sz) dav1d_data_unref_internal(in); |
419 | 108k | } |
420 | 108k | if (output_picture_ready(c, 0)) |
421 | 4.55k | break; |
422 | 104k | if (res < 0) |
423 | 379 | return (int)res; |
424 | 104k | } |
425 | | |
426 | 204k | return 0; |
427 | 204k | } |
428 | | |
429 | | int dav1d_send_data(Dav1dContext *const c, Dav1dData *const in) |
430 | 89.5k | { |
431 | 89.5k | validate_input_or_ret(c != NULL, DAV1D_ERR(EINVAL)); |
432 | 89.5k | validate_input_or_ret(in != NULL, DAV1D_ERR(EINVAL)); |
433 | | |
434 | 89.5k | if (in->data) { |
435 | 83.5k | validate_input_or_ret(in->sz > 0 && in->sz <= SIZE_MAX / 2, DAV1D_ERR(EINVAL)); |
436 | 83.5k | c->drain = 0; |
437 | 83.5k | } |
438 | 89.5k | if (c->in.data) |
439 | 0 | return DAV1D_ERR(EAGAIN); |
440 | 89.5k | dav1d_data_ref(&c->in, in); |
441 | | |
442 | 89.5k | int res = gen_picture(c); |
443 | 89.5k | if (!res) |
444 | 89.2k | dav1d_data_unref_internal(in); |
445 | | |
446 | 89.5k | return res; |
447 | 89.5k | } |
448 | | |
449 | | int dav1d_get_picture(Dav1dContext *const c, Dav1dPicture *const out) |
450 | 119k | { |
451 | 119k | validate_input_or_ret(c != NULL, DAV1D_ERR(EINVAL)); |
452 | 119k | validate_input_or_ret(out != NULL, DAV1D_ERR(EINVAL)); |
453 | | |
454 | 119k | const int drain = c->drain; |
455 | 119k | c->drain = 1; |
456 | | |
457 | 119k | int res = gen_picture(c); |
458 | 119k | if (res < 0) |
459 | 1 | return res; |
460 | | |
461 | 119k | if (c->cached_error) { |
462 | 9 | const int res = c->cached_error; |
463 | 9 | c->cached_error = 0; |
464 | 9 | return res; |
465 | 9 | } |
466 | | |
467 | 119k | if (output_picture_ready(c, c->n_fc == 1)) |
468 | 4.54k | return output_image(c, out); |
469 | | |
470 | 114k | if (c->n_fc > 1 && drain) |
471 | 36.2k | return drain_picture(c, out); |
472 | | |
473 | 78.6k | return DAV1D_ERR(EAGAIN); |
474 | 114k | } |
475 | | |
476 | | int dav1d_apply_grain(Dav1dContext *const c, Dav1dPicture *const out, |
477 | | const Dav1dPicture *const in) |
478 | 3.08k | { |
479 | 3.08k | validate_input_or_ret(c != NULL, DAV1D_ERR(EINVAL)); |
480 | 3.08k | validate_input_or_ret(out != NULL, DAV1D_ERR(EINVAL)); |
481 | 3.08k | validate_input_or_ret(in != NULL, DAV1D_ERR(EINVAL)); |
482 | | |
483 | 3.08k | if (!has_grain(in)) { |
484 | 0 | dav1d_picture_ref(out, in); |
485 | 0 | return 0; |
486 | 0 | } |
487 | | |
488 | 3.08k | int res = dav1d_picture_alloc_copy(c, out, in->p.w, in); |
489 | 3.08k | if (res < 0) goto error; |
490 | | |
491 | 3.08k | if (c->n_tc > 1) { |
492 | 3.08k | dav1d_task_delayed_fg(c, out, in); |
493 | 3.08k | } else { |
494 | 0 | switch (out->p.bpc) { |
495 | 0 | #if CONFIG_8BPC |
496 | 0 | case 8: |
497 | 0 | dav1d_apply_grain_8bpc(&c->dsp[0].fg, out, in); |
498 | 0 | break; |
499 | 0 | #endif |
500 | 0 | #if CONFIG_16BPC |
501 | 0 | case 10: |
502 | 0 | case 12: |
503 | 0 | dav1d_apply_grain_16bpc(&c->dsp[(out->p.bpc >> 1) - 4].fg, out, in); |
504 | 0 | break; |
505 | 0 | #endif |
506 | 0 | default: abort(); |
507 | 0 | } |
508 | 0 | } |
509 | | |
510 | 3.08k | return 0; |
511 | | |
512 | 0 | error: |
513 | 0 | dav1d_picture_unref_internal(out); |
514 | 0 | return res; |
515 | 3.08k | } |
516 | | |
517 | 9.48k | void dav1d_flush(Dav1dContext *const c) { |
518 | 9.48k | dav1d_data_unref_internal(&c->in); |
519 | 9.48k | if (c->out.p.frame_hdr) |
520 | 0 | dav1d_thread_picture_unref(&c->out); |
521 | 9.48k | if (c->cache.p.frame_hdr) |
522 | 895 | dav1d_thread_picture_unref(&c->cache); |
523 | | |
524 | 9.48k | c->drain = 0; |
525 | 9.48k | c->cached_error = 0; |
526 | | |
527 | 85.3k | for (int i = 0; i < 8; i++) { |
528 | 75.8k | if (c->refs[i].p.p.frame_hdr) |
529 | 66.1k | dav1d_thread_picture_unref(&c->refs[i].p); |
530 | 75.8k | dav1d_ref_dec(&c->refs[i].segmap); |
531 | 75.8k | dav1d_ref_dec(&c->refs[i].refmvs); |
532 | 75.8k | dav1d_cdf_thread_unref(&c->cdf[i]); |
533 | 75.8k | } |
534 | 9.48k | c->frame_hdr = NULL; |
535 | 9.48k | c->seq_hdr = NULL; |
536 | 9.48k | dav1d_ref_dec(&c->seq_hdr_ref); |
537 | | |
538 | 9.48k | c->mastering_display = NULL; |
539 | 9.48k | c->content_light = NULL; |
540 | 9.48k | c->itut_t35 = NULL; |
541 | 9.48k | c->n_itut_t35 = 0; |
542 | 9.48k | dav1d_ref_dec(&c->mastering_display_ref); |
543 | 9.48k | dav1d_ref_dec(&c->content_light_ref); |
544 | 9.48k | dav1d_ref_dec(&c->itut_t35_ref); |
545 | | |
546 | 9.48k | dav1d_data_props_unref_internal(&c->cached_error_props); |
547 | | |
548 | 9.48k | if (c->n_fc == 1 && c->n_tc == 1) return; |
549 | 9.48k | atomic_store_explicit(c->flush, 1, memory_order_relaxed); |
550 | | |
551 | 9.48k | if (c->n_tc > 1) { |
552 | 9.48k | pthread_mutex_lock(&c->task_thread.lock); |
553 | | // stop running tasks in worker threads |
554 | 313k | for (unsigned i = 0; i < c->n_tc; i++) { |
555 | 303k | Dav1dTaskContext *const tc = &c->tc[i]; |
556 | 305k | while (!tc->task_thread.flushed) { |
557 | 2.20k | pthread_cond_wait(&tc->task_thread.td.cond, &c->task_thread.lock); |
558 | 2.20k | } |
559 | 303k | } |
560 | 66.3k | for (unsigned i = 0; i < c->n_fc; i++) { |
561 | 56.9k | c->fc[i].task_thread.task_head = NULL; |
562 | 56.9k | c->fc[i].task_thread.task_tail = NULL; |
563 | 56.9k | c->fc[i].task_thread.task_cur_prev = NULL; |
564 | 56.9k | atomic_init(&c->fc[i].task_thread.pending_tasks, 0); |
565 | 56.9k | } |
566 | 9.48k | atomic_init(&c->task_thread.first, 0); |
567 | 9.48k | c->task_thread.cur = c->n_fc; |
568 | 9.48k | atomic_store_explicit(&c->task_thread.reset_task_cur, UINT_MAX, memory_order_relaxed); |
569 | 9.48k | atomic_store_explicit(&c->task_thread.cond_signaled, 0, memory_order_relaxed); |
570 | 9.48k | pthread_mutex_unlock(&c->task_thread.lock); |
571 | 9.48k | } |
572 | | |
573 | 9.48k | if (c->n_fc > 1) { |
574 | 66.3k | for (unsigned n = 0, next = c->frame_thread.next; n < c->n_fc; n++, next++) { |
575 | 56.9k | if (next == c->n_fc) next = 0; |
576 | 56.9k | Dav1dFrameContext *const f = &c->fc[next]; |
577 | 56.9k | dav1d_decode_frame_exit(f, -1); |
578 | 56.9k | f->n_tile_data = 0; |
579 | 56.9k | f->task_thread.retval = 0; |
580 | 56.9k | atomic_store_explicit(&f->task_thread.error, 0, memory_order_relaxed); |
581 | 56.9k | Dav1dThreadPicture *out_delayed = &c->frame_thread.out_delayed[next]; |
582 | 56.9k | if (out_delayed->p.frame_hdr) { |
583 | 11.4k | dav1d_thread_picture_unref(out_delayed); |
584 | 11.4k | } |
585 | 56.9k | } |
586 | 9.48k | c->frame_thread.next = 0; |
587 | 9.48k | } |
588 | 9.48k | atomic_store_explicit(c->flush, 0, memory_order_relaxed); |
589 | 9.48k | } |
590 | | |
591 | 9.48k | COLD void dav1d_close(Dav1dContext **const c_out) { |
592 | 9.48k | validate_input(c_out != NULL); |
593 | | #if TRACK_HEAP_ALLOCATIONS |
594 | | dav1d_log_alloc_stats(*c_out); |
595 | | #endif |
596 | 9.48k | close_internal(c_out, 1); |
597 | 9.48k | } |
598 | | |
599 | 9.48k | static COLD void close_internal(Dav1dContext **const c_out, int flush) { |
600 | 9.48k | Dav1dContext *const c = *c_out; |
601 | 9.48k | if (!c) return; |
602 | | |
603 | 9.48k | if (flush) dav1d_flush(c); |
604 | | |
605 | 9.48k | if (c->tc) { |
606 | 9.48k | struct TaskThreadData *ttd = &c->task_thread; |
607 | 9.48k | if (ttd->inited) { |
608 | 9.48k | pthread_mutex_lock(&ttd->lock); |
609 | 313k | for (unsigned n = 0; n < c->n_tc && c->tc[n].task_thread.td.inited; n++) |
610 | 303k | c->tc[n].task_thread.die = 1; |
611 | 9.48k | pthread_cond_broadcast(&ttd->cond); |
612 | 9.48k | pthread_mutex_unlock(&ttd->lock); |
613 | 313k | for (unsigned n = 0; n < c->n_tc; n++) { |
614 | 303k | Dav1dTaskContext *const pf = &c->tc[n]; |
615 | 303k | if (!pf->task_thread.td.inited) break; |
616 | 303k | pthread_join(pf->task_thread.td.thread, NULL); |
617 | 303k | pthread_cond_destroy(&pf->task_thread.td.cond); |
618 | 303k | } |
619 | 9.48k | pthread_cond_destroy(&ttd->delayed_fg.cond); |
620 | 9.48k | pthread_cond_destroy(&ttd->cond); |
621 | 9.48k | pthread_mutex_destroy(&ttd->lock); |
622 | 9.48k | } |
623 | 9.48k | dav1d_free_aligned(c->tc); |
624 | 9.48k | } |
625 | | |
626 | 66.3k | for (unsigned n = 0; c->fc && n < c->n_fc; n++) { |
627 | 56.9k | Dav1dFrameContext *const f = &c->fc[n]; |
628 | | |
629 | | // clean-up threading stuff |
630 | 56.9k | if (c->n_fc > 1) { |
631 | 56.9k | dav1d_free(f->tile_thread.lowest_pixel_mem); |
632 | 56.9k | dav1d_free(f->frame_thread.b); |
633 | 56.9k | dav1d_free_aligned(f->frame_thread.cbi); |
634 | 56.9k | dav1d_free_aligned(f->frame_thread.pal_idx); |
635 | 56.9k | dav1d_free_aligned(f->frame_thread.cf); |
636 | 56.9k | dav1d_free(f->frame_thread.tile_start_off); |
637 | 56.9k | dav1d_free_aligned(f->frame_thread.pal); |
638 | 56.9k | } |
639 | 56.9k | if (c->n_tc > 1) { |
640 | 56.9k | pthread_cond_destroy(&f->task_thread.cond); |
641 | 56.9k | } |
642 | 56.9k | dav1d_free(f->frame_thread.frame_progress); |
643 | 56.9k | dav1d_free(f->task_thread.tasks); |
644 | 56.9k | dav1d_free(f->task_thread.tile_tasks[0]); |
645 | 56.9k | dav1d_free_aligned(f->ts); |
646 | 56.9k | dav1d_free_aligned(f->ipred_edge[0]); |
647 | 56.9k | dav1d_free_aligned(f->a); |
648 | 56.9k | dav1d_free(f->tile); |
649 | 56.9k | dav1d_free(f->lf.mask); |
650 | 56.9k | dav1d_free_aligned(f->lf.level[0]); |
651 | 56.9k | dav1d_free(f->lf.lr_mask); |
652 | 56.9k | dav1d_free(f->lf.tx_lpf_right_edge[0]); |
653 | 56.9k | dav1d_free(f->lf.start_of_tile_row); |
654 | 56.9k | dav1d_free_aligned(f->rf.r); |
655 | 56.9k | dav1d_free_aligned(f->lf.cdef_line_buf); |
656 | 56.9k | dav1d_free_aligned(f->lf.lr_line_buf); |
657 | 56.9k | } |
658 | 9.48k | dav1d_free_aligned(c->fc); |
659 | 9.48k | if (c->n_fc > 1 && c->frame_thread.out_delayed) { |
660 | 66.3k | for (unsigned n = 0; n < c->n_fc; n++) |
661 | 56.9k | if (c->frame_thread.out_delayed[n].p.frame_hdr) |
662 | 0 | dav1d_thread_picture_unref(&c->frame_thread.out_delayed[n]); |
663 | 9.48k | dav1d_free(c->frame_thread.out_delayed); |
664 | 9.48k | } |
665 | 9.54k | for (int n = 0; n < c->n_tile_data; n++) |
666 | 64 | dav1d_data_unref_internal(&c->tile[n].data); |
667 | 9.48k | dav1d_free(c->tile); |
668 | 85.3k | for (int n = 0; n < 8; n++) { |
669 | 75.8k | dav1d_cdf_thread_unref(&c->cdf[n]); |
670 | 75.8k | if (c->refs[n].p.p.frame_hdr) |
671 | 0 | dav1d_thread_picture_unref(&c->refs[n].p); |
672 | 75.8k | dav1d_ref_dec(&c->refs[n].refmvs); |
673 | 75.8k | dav1d_ref_dec(&c->refs[n].segmap); |
674 | 75.8k | } |
675 | 9.48k | dav1d_ref_dec(&c->seq_hdr_ref); |
676 | 9.48k | dav1d_ref_dec(&c->frame_hdr_ref); |
677 | | |
678 | 9.48k | dav1d_ref_dec(&c->mastering_display_ref); |
679 | 9.48k | dav1d_ref_dec(&c->content_light_ref); |
680 | 9.48k | dav1d_ref_dec(&c->itut_t35_ref); |
681 | | |
682 | 9.48k | dav1d_mem_pool_end(c->seq_hdr_pool); |
683 | 9.48k | dav1d_mem_pool_end(c->frame_hdr_pool); |
684 | 9.48k | dav1d_mem_pool_end(c->segmap_pool); |
685 | 9.48k | dav1d_mem_pool_end(c->refmvs_pool); |
686 | 9.48k | dav1d_mem_pool_end(c->cdf_pool); |
687 | 9.48k | dav1d_mem_pool_end(c->picture_pool); |
688 | 9.48k | dav1d_mem_pool_end(c->pic_ctx_pool); |
689 | | |
690 | 9.48k | dav1d_freep_aligned(c_out); |
691 | 9.48k | } |
692 | | |
693 | 0 | int dav1d_get_event_flags(Dav1dContext *const c, enum Dav1dEventFlags *const flags) { |
694 | 0 | validate_input_or_ret(c != NULL, DAV1D_ERR(EINVAL)); |
695 | 0 | validate_input_or_ret(flags != NULL, DAV1D_ERR(EINVAL)); |
696 | |
|
697 | 0 | *flags = c->event_flags; |
698 | 0 | c->event_flags = 0; |
699 | 0 | return 0; |
700 | 0 | } |
701 | | |
702 | 0 | int dav1d_get_decode_error_data_props(Dav1dContext *const c, Dav1dDataProps *const out) { |
703 | 0 | validate_input_or_ret(c != NULL, DAV1D_ERR(EINVAL)); |
704 | 0 | validate_input_or_ret(out != NULL, DAV1D_ERR(EINVAL)); |
705 | |
|
706 | 0 | dav1d_data_props_unref_internal(out); |
707 | 0 | *out = c->cached_error_props; |
708 | 0 | dav1d_data_props_set_defaults(&c->cached_error_props); |
709 | |
|
710 | 0 | return 0; |
711 | 0 | } |
712 | | |
713 | 34.0k | void dav1d_picture_unref(Dav1dPicture *const p) { |
714 | 34.0k | dav1d_picture_unref_internal(p); |
715 | 34.0k | } |
716 | | |
717 | 83.5k | uint8_t *dav1d_data_create(Dav1dData *const buf, const size_t sz) { |
718 | 83.5k | return dav1d_data_create_internal(buf, sz); |
719 | 83.5k | } |
720 | | |
721 | | int dav1d_data_wrap(Dav1dData *const buf, const uint8_t *const ptr, |
722 | | const size_t sz, |
723 | | void (*const free_callback)(const uint8_t *data, |
724 | | void *user_data), |
725 | | void *const user_data) |
726 | 0 | { |
727 | 0 | return dav1d_data_wrap_internal(buf, ptr, sz, free_callback, user_data); |
728 | 0 | } |
729 | | |
730 | | int dav1d_data_wrap_user_data(Dav1dData *const buf, |
731 | | const uint8_t *const user_data, |
732 | | void (*const free_callback)(const uint8_t *user_data, |
733 | | void *cookie), |
734 | | void *const cookie) |
735 | 0 | { |
736 | 0 | return dav1d_data_wrap_user_data_internal(buf, |
737 | 0 | user_data, |
738 | 0 | free_callback, |
739 | 0 | cookie); |
740 | 0 | } |
741 | | |
742 | 378 | void dav1d_data_unref(Dav1dData *const buf) { |
743 | 378 | dav1d_data_unref_internal(buf); |
744 | 378 | } |
745 | | |
746 | 0 | void dav1d_data_props_unref(Dav1dDataProps *const props) { |
747 | 0 | dav1d_data_props_unref_internal(props); |
748 | 0 | } |