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 | 43.3k | COLD void dav1d_default_settings(Dav1dSettings *const s) { |
72 | 43.3k | s->n_threads = 0; |
73 | 43.3k | s->max_frame_delay = 0; |
74 | 43.3k | s->apply_grain = 1; |
75 | 43.3k | s->allocator.cookie = NULL; |
76 | 43.3k | s->allocator.alloc_picture_callback = dav1d_default_picture_alloc; |
77 | 43.3k | s->allocator.release_picture_callback = dav1d_default_picture_release; |
78 | 43.3k | s->logger.cookie = NULL; |
79 | 43.3k | s->logger.callback = dav1d_log_default_callback; |
80 | 43.3k | s->operating_point = 0; |
81 | 43.3k | s->all_layers = 1; // just until the tests are adjusted |
82 | 43.3k | s->frame_size_limit = 0; |
83 | 43.3k | s->strict_std_compliance = 0; |
84 | 43.3k | s->output_invisible_frames = 0; |
85 | 43.3k | s->inloop_filters = DAV1D_INLOOPFILTER_ALL; |
86 | 43.3k | s->decode_frame_type = DAV1D_DECODEFRAMETYPE_ALL; |
87 | 43.3k | } |
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 | 43.3k | 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 | 43.3k | size_t (*const get_minstack)(const pthread_attr_t*) = |
100 | 43.3k | dlsym(RTLD_DEFAULT, "__pthread_get_minstack"); |
101 | 43.3k | if (get_minstack) |
102 | 43.3k | return get_minstack(thread_attr) - PTHREAD_STACK_MIN; |
103 | 0 | return 0; |
104 | 43.3k | } |
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 | 43.3k | { |
112 | | /* ceil(sqrt(n)) */ |
113 | 43.3k | static const uint8_t fc_lut[49] = { |
114 | 43.3k | 1, /* 1 */ |
115 | 43.3k | 2, 2, 2, /* 2- 4 */ |
116 | 43.3k | 3, 3, 3, 3, 3, /* 5- 9 */ |
117 | 43.3k | 4, 4, 4, 4, 4, 4, 4, /* 10-16 */ |
118 | 43.3k | 5, 5, 5, 5, 5, 5, 5, 5, 5, /* 17-25 */ |
119 | 43.3k | 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, /* 26-36 */ |
120 | 43.3k | 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, 7, /* 37-49 */ |
121 | 43.3k | }; |
122 | 43.3k | *n_tc = s->n_threads ? s->n_threads : |
123 | 43.3k | iclip(dav1d_num_logical_processors(c), 1, DAV1D_MAX_THREADS); |
124 | 43.3k | *n_fc = s->max_frame_delay ? umin(s->max_frame_delay, *n_tc) : |
125 | 43.3k | *n_tc < 50 ? fc_lut[*n_tc - 1] : 8; // min(8, ceil(sqrt(n))) |
126 | 43.3k | } |
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 | 43.3k | COLD int dav1d_open(Dav1dContext **const c_out, const Dav1dSettings *const s) { |
141 | 43.3k | static pthread_once_t initted = PTHREAD_ONCE_INIT; |
142 | 43.3k | pthread_once(&initted, init_internal); |
143 | | |
144 | 43.3k | validate_input_or_ret(c_out != NULL, DAV1D_ERR(EINVAL)); |
145 | 43.3k | validate_input_or_ret(s != NULL, DAV1D_ERR(EINVAL)); |
146 | 43.3k | validate_input_or_ret(s->n_threads >= 0 && |
147 | 43.3k | s->n_threads <= DAV1D_MAX_THREADS, DAV1D_ERR(EINVAL)); |
148 | 43.3k | validate_input_or_ret(s->max_frame_delay >= 0 && |
149 | 43.3k | s->max_frame_delay <= DAV1D_MAX_FRAME_DELAY, DAV1D_ERR(EINVAL)); |
150 | 43.3k | validate_input_or_ret(s->allocator.alloc_picture_callback != NULL, |
151 | 43.3k | DAV1D_ERR(EINVAL)); |
152 | 43.3k | validate_input_or_ret(s->allocator.release_picture_callback != NULL, |
153 | 43.3k | DAV1D_ERR(EINVAL)); |
154 | 43.3k | validate_input_or_ret(s->operating_point >= 0 && |
155 | 43.3k | s->operating_point <= 31, DAV1D_ERR(EINVAL)); |
156 | 43.3k | validate_input_or_ret(s->decode_frame_type >= DAV1D_DECODEFRAMETYPE_ALL && |
157 | 43.3k | s->decode_frame_type <= DAV1D_DECODEFRAMETYPE_KEY, DAV1D_ERR(EINVAL)); |
158 | | |
159 | 43.3k | pthread_attr_t thread_attr; |
160 | 43.3k | if (pthread_attr_init(&thread_attr)) return DAV1D_ERR(ENOMEM); |
161 | 43.3k | size_t stack_size = 1024 * 1024 + get_stack_size_internal(&thread_attr); |
162 | | |
163 | 43.3k | pthread_attr_setstacksize(&thread_attr, stack_size); |
164 | | |
165 | 43.3k | Dav1dContext *const c = *c_out = dav1d_alloc_aligned(ALLOC_COMMON_CTX, sizeof(*c), 64); |
166 | 43.3k | if (!c) goto error; |
167 | 43.3k | memset(c, 0, sizeof(*c)); |
168 | | |
169 | 43.3k | c->allocator = s->allocator; |
170 | 43.3k | c->logger = s->logger; |
171 | 43.3k | c->apply_grain = s->apply_grain; |
172 | 43.3k | c->operating_point = s->operating_point; |
173 | 43.3k | c->all_layers = s->all_layers; |
174 | 43.3k | c->frame_size_limit = s->frame_size_limit; |
175 | 43.3k | c->strict_std_compliance = s->strict_std_compliance; |
176 | 43.3k | c->output_invisible_frames = s->output_invisible_frames; |
177 | 43.3k | c->inloop_filters = s->inloop_filters; |
178 | 43.3k | c->decode_frame_type = s->decode_frame_type; |
179 | | |
180 | 43.3k | dav1d_data_props_set_defaults(&c->cached_error_props); |
181 | | |
182 | 43.3k | if (dav1d_mem_pool_init(ALLOC_OBU_HDR, &c->seq_hdr_pool) || |
183 | 43.3k | dav1d_mem_pool_init(ALLOC_OBU_HDR, &c->frame_hdr_pool) || |
184 | 43.3k | dav1d_mem_pool_init(ALLOC_SEGMAP, &c->segmap_pool) || |
185 | 43.3k | dav1d_mem_pool_init(ALLOC_REFMVS, &c->refmvs_pool) || |
186 | 43.3k | dav1d_mem_pool_init(ALLOC_PIC_CTX, &c->pic_ctx_pool) || |
187 | 43.3k | dav1d_mem_pool_init(ALLOC_CDF, &c->cdf_pool)) |
188 | 0 | { |
189 | 0 | goto error; |
190 | 0 | } |
191 | | |
192 | 43.3k | if (c->allocator.alloc_picture_callback == dav1d_default_picture_alloc && |
193 | 43.3k | c->allocator.release_picture_callback == dav1d_default_picture_release) |
194 | 43.3k | { |
195 | 43.3k | if (c->allocator.cookie) goto error; |
196 | 43.3k | if (dav1d_mem_pool_init(ALLOC_PIC, &c->picture_pool)) goto error; |
197 | 43.3k | c->allocator.cookie = c->picture_pool; |
198 | 43.3k | } 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 | 43.3k | 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 | 43.3k | c->flush = &c->flush_mem; |
216 | 43.3k | atomic_init(c->flush, 0); |
217 | | |
218 | 43.3k | get_num_threads(c, s, &c->n_tc, &c->n_fc); |
219 | | |
220 | 43.3k | c->fc = dav1d_alloc_aligned(ALLOC_THREAD_CTX, sizeof(*c->fc) * c->n_fc, 32); |
221 | 43.3k | if (!c->fc) goto error; |
222 | 43.3k | memset(c->fc, 0, sizeof(*c->fc) * c->n_fc); |
223 | | |
224 | 43.3k | c->tc = dav1d_alloc_aligned(ALLOC_THREAD_CTX, sizeof(*c->tc) * c->n_tc, 64); |
225 | 43.3k | if (!c->tc) goto error; |
226 | 43.3k | memset(c->tc, 0, sizeof(*c->tc) * c->n_tc); |
227 | 43.3k | if (c->n_tc > 1) { |
228 | 43.3k | if (pthread_mutex_init(&c->task_thread.lock, NULL)) goto error; |
229 | 43.3k | 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 | 43.3k | 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 | 43.3k | c->task_thread.cur = c->n_fc; |
239 | 43.3k | atomic_init(&c->task_thread.reset_task_cur, UINT_MAX); |
240 | 43.3k | atomic_init(&c->task_thread.cond_signaled, 0); |
241 | 43.3k | c->task_thread.inited = 1; |
242 | 43.3k | } |
243 | | |
244 | 43.3k | if (c->n_fc > 1) { |
245 | 43.3k | const size_t out_delayed_sz = sizeof(*c->frame_thread.out_delayed) * c->n_fc; |
246 | 43.3k | c->frame_thread.out_delayed = |
247 | 43.3k | dav1d_malloc(ALLOC_THREAD_CTX, out_delayed_sz); |
248 | 43.3k | if (!c->frame_thread.out_delayed) goto error; |
249 | 43.3k | memset(c->frame_thread.out_delayed, 0, out_delayed_sz); |
250 | 43.3k | } |
251 | 303k | for (unsigned n = 0; n < c->n_fc; n++) { |
252 | 260k | Dav1dFrameContext *const f = &c->fc[n]; |
253 | 260k | if (c->n_tc > 1) { |
254 | 260k | if (pthread_cond_init(&f->task_thread.cond, NULL)) goto error; |
255 | 260k | } |
256 | 260k | f->c = c; |
257 | 260k | f->task_thread.ttd = &c->task_thread; |
258 | 260k | f->lf.last_sharpness = -1; |
259 | 260k | } |
260 | | |
261 | 1.43M | for (unsigned m = 0; m < c->n_tc; m++) { |
262 | 1.38M | Dav1dTaskContext *const t = &c->tc[m]; |
263 | 1.38M | t->f = &c->fc[0]; |
264 | 1.38M | t->task_thread.ttd = &c->task_thread; |
265 | 1.38M | t->c = c; |
266 | 1.38M | memset(t->cf_16bpc, 0, sizeof(t->cf_16bpc)); |
267 | 1.38M | if (c->n_tc > 1) { |
268 | 1.38M | if (pthread_cond_init(&t->task_thread.td.cond, NULL)) goto error; |
269 | 1.38M | 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 | 1.38M | t->task_thread.td.inited = 1; |
274 | 1.38M | } |
275 | 1.38M | } |
276 | 43.3k | dav1d_lf_mask_dsp_init(&c->lf_mask_dsp); |
277 | 43.3k | dav1d_pal_dsp_init(&c->pal_dsp); |
278 | 43.3k | dav1d_refmvs_dsp_init(&c->refmvs_dsp); |
279 | | |
280 | 43.3k | pthread_attr_destroy(&thread_attr); |
281 | | |
282 | 43.3k | 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 | 43.3k | } |
289 | | |
290 | | static int has_grain(const Dav1dPicture *const pic) |
291 | 28.0k | { |
292 | 28.0k | const Dav1dFilmGrainData *fgdata = &pic->frame_hdr->film_grain.data; |
293 | 28.0k | return fgdata->num_y_points || fgdata->num_uv_points[0] || |
294 | 24.1k | fgdata->num_uv_points[1] || (fgdata->clip_to_restricted_range && |
295 | 241 | fgdata->chroma_scaling_from_luma); |
296 | 28.0k | } |
297 | | |
298 | | static int output_image(Dav1dContext *const c, Dav1dPicture *const out) |
299 | 25.8k | { |
300 | 25.8k | int res = 0; |
301 | | |
302 | 25.8k | Dav1dThreadPicture *const in = (c->all_layers || !c->max_spatial_id) |
303 | 25.8k | ? &c->out : &c->cache; |
304 | 25.8k | if (!c->apply_grain || !has_grain(&in->p)) { |
305 | 23.6k | dav1d_picture_move_ref(out, &in->p); |
306 | 23.6k | dav1d_thread_picture_unref(in); |
307 | 23.6k | goto end; |
308 | 23.6k | } |
309 | | |
310 | 2.17k | res = dav1d_apply_grain(c, out, &in->p); |
311 | 2.17k | dav1d_thread_picture_unref(in); |
312 | 25.8k | end: |
313 | 25.8k | if (!c->all_layers && c->max_spatial_id && c->out.p.data[0]) { |
314 | 82 | dav1d_thread_picture_move_ref(in, &c->out); |
315 | 82 | } |
316 | 25.8k | return res; |
317 | 2.17k | } |
318 | | |
319 | 535k | static int output_picture_ready(Dav1dContext *const c, const int drain) { |
320 | 535k | if (c->cached_error) return 1; |
321 | 535k | if (!c->all_layers && c->max_spatial_id) { |
322 | 161k | if (c->out.p.data[0] && c->cache.p.data[0]) { |
323 | 9.76k | if (c->max_spatial_id == c->cache.p.frame_hdr->spatial_id || |
324 | 9.72k | c->out.flags & PICTURE_FLAG_NEW_TEMPORAL_UNIT) |
325 | 82 | return 1; |
326 | 9.68k | dav1d_thread_picture_unref(&c->cache); |
327 | 9.68k | dav1d_thread_picture_move_ref(&c->cache, &c->out); |
328 | 9.68k | return 0; |
329 | 151k | } else if (c->cache.p.data[0] && drain) { |
330 | 6.54k | return 1; |
331 | 144k | } else if (c->out.p.data[0]) { |
332 | 10.7k | dav1d_thread_picture_move_ref(&c->cache, &c->out); |
333 | 10.7k | return 0; |
334 | 10.7k | } |
335 | 161k | } |
336 | | |
337 | 508k | return !!c->out.p.data[0]; |
338 | 535k | } |
339 | | |
340 | 75.8k | static int drain_picture(Dav1dContext *const c, Dav1dPicture *const out) { |
341 | 75.8k | unsigned drain_count = 0; |
342 | 75.8k | int drained = 0; |
343 | 451k | do { |
344 | 451k | const unsigned next = c->frame_thread.next; |
345 | 451k | Dav1dFrameContext *const f = &c->fc[next]; |
346 | 451k | pthread_mutex_lock(&c->task_thread.lock); |
347 | 501k | while (f->n_tile_data > 0) |
348 | 50.7k | pthread_cond_wait(&f->task_thread.cond, |
349 | 50.7k | &f->task_thread.ttd->lock); |
350 | 451k | Dav1dThreadPicture *const out_delayed = |
351 | 451k | &c->frame_thread.out_delayed[next]; |
352 | 451k | if (out_delayed->p.data[0] || |
353 | 451k | atomic_load_explicit(&f->task_thread.error, memory_order_relaxed)) |
354 | 55.3k | { |
355 | 55.3k | unsigned first = atomic_load_explicit(&c->task_thread.first, memory_order_relaxed); |
356 | 55.3k | if (first + 1U < c->n_fc) |
357 | 55.3k | atomic_fetch_add_explicit(&c->task_thread.first, 1U, memory_order_relaxed); |
358 | 335 | else |
359 | 55.3k | atomic_store_explicit(&c->task_thread.first, 0, memory_order_relaxed); |
360 | 55.3k | atomic_compare_exchange_strong_explicit(&c->task_thread.reset_task_cur, |
361 | 55.3k | &first, UINT_MAX, |
362 | 55.3k | memory_order_relaxed, |
363 | 55.3k | memory_order_relaxed); |
364 | 55.3k | if (c->task_thread.cur && c->task_thread.cur < c->n_fc) |
365 | 4.28k | c->task_thread.cur--; |
366 | 55.3k | drained = 1; |
367 | 395k | } else if (drained) { |
368 | 0 | pthread_mutex_unlock(&c->task_thread.lock); |
369 | 0 | break; |
370 | 0 | } |
371 | 451k | if (++c->frame_thread.next == c->n_fc) |
372 | 75.7k | c->frame_thread.next = 0; |
373 | 451k | pthread_mutex_unlock(&c->task_thread.lock); |
374 | 451k | const int error = f->task_thread.retval; |
375 | 451k | if (error) { |
376 | 14.5k | f->task_thread.retval = 0; |
377 | 14.5k | dav1d_data_props_copy(&c->cached_error_props, &out_delayed->p.m); |
378 | 14.5k | dav1d_thread_picture_unref(out_delayed); |
379 | 14.5k | return error; |
380 | 14.5k | } |
381 | 436k | if (out_delayed->p.data[0]) { |
382 | 40.8k | const unsigned progress = |
383 | 40.8k | atomic_load_explicit(&out_delayed->progress[1], |
384 | 40.8k | memory_order_relaxed); |
385 | 40.8k | if ((out_delayed->visible || c->output_invisible_frames) && |
386 | 39.4k | progress != FRAME_ERROR) |
387 | 39.4k | { |
388 | 39.4k | dav1d_thread_picture_ref(&c->out, out_delayed); |
389 | 39.4k | c->event_flags |= dav1d_picture_get_event_flags(out_delayed); |
390 | 39.4k | } |
391 | 40.8k | dav1d_thread_picture_unref(out_delayed); |
392 | 40.8k | if (output_picture_ready(c, 0)) |
393 | 19.3k | return output_image(c, out); |
394 | 40.8k | } |
395 | 436k | } while (++drain_count < c->n_fc); |
396 | | |
397 | 41.9k | if (output_picture_ready(c, 1)) |
398 | 6.54k | return output_image(c, out); |
399 | | |
400 | 35.4k | return DAV1D_ERR(EAGAIN); |
401 | 41.9k | } |
402 | | |
403 | | static int gen_picture(Dav1dContext *const c) |
404 | 201k | { |
405 | 201k | Dav1dData *const in = &c->in; |
406 | | |
407 | 201k | if (output_picture_ready(c, 0)) |
408 | 0 | return 0; |
409 | | |
410 | 333k | while (in->sz > 0) { |
411 | 134k | const ptrdiff_t res = dav1d_parse_obus(c, in); |
412 | 134k | if (res < 0) { |
413 | 2.21k | dav1d_data_unref_internal(in); |
414 | 132k | } else { |
415 | 132k | assert((size_t)res <= in->sz); |
416 | 132k | in->sz -= res; |
417 | 132k | in->data += res; |
418 | 132k | if (!in->sz) dav1d_data_unref_internal(in); |
419 | 132k | } |
420 | 134k | if (output_picture_ready(c, 0)) |
421 | 0 | break; |
422 | 134k | if (res < 0) |
423 | 2.21k | return (int)res; |
424 | 134k | } |
425 | | |
426 | 199k | return 0; |
427 | 201k | } |
428 | | |
429 | | int dav1d_send_data(Dav1dContext *const c, Dav1dData *const in) |
430 | 84.4k | { |
431 | 84.4k | validate_input_or_ret(c != NULL, DAV1D_ERR(EINVAL)); |
432 | 84.4k | validate_input_or_ret(in != NULL, DAV1D_ERR(EINVAL)); |
433 | | |
434 | 84.4k | if (in->data) { |
435 | 43.3k | validate_input_or_ret(in->sz > 0 && in->sz <= SIZE_MAX / 2, DAV1D_ERR(EINVAL)); |
436 | 43.3k | c->drain = 0; |
437 | 43.3k | } |
438 | 84.4k | if (c->in.data) |
439 | 0 | return DAV1D_ERR(EAGAIN); |
440 | 84.4k | dav1d_data_ref(&c->in, in); |
441 | | |
442 | 84.4k | int res = gen_picture(c); |
443 | 84.4k | if (!res) |
444 | 82.2k | dav1d_data_unref_internal(in); |
445 | | |
446 | 84.4k | return res; |
447 | 84.4k | } |
448 | | |
449 | | int dav1d_get_picture(Dav1dContext *const c, Dav1dPicture *const out) |
450 | 116k | { |
451 | 116k | validate_input_or_ret(c != NULL, DAV1D_ERR(EINVAL)); |
452 | 116k | validate_input_or_ret(out != NULL, DAV1D_ERR(EINVAL)); |
453 | | |
454 | 116k | const int drain = c->drain; |
455 | 116k | c->drain = 1; |
456 | | |
457 | 116k | int res = gen_picture(c); |
458 | 116k | if (res < 0) |
459 | 0 | return res; |
460 | | |
461 | 116k | if (c->cached_error) { |
462 | 0 | const int res = c->cached_error; |
463 | 0 | c->cached_error = 0; |
464 | 0 | return res; |
465 | 0 | } |
466 | | |
467 | 116k | if (output_picture_ready(c, c->n_fc == 1)) |
468 | 0 | return output_image(c, out); |
469 | | |
470 | 116k | if (c->n_fc > 1 && drain) |
471 | 75.8k | return drain_picture(c, out); |
472 | | |
473 | 41.1k | return DAV1D_ERR(EAGAIN); |
474 | 116k | } |
475 | | |
476 | | int dav1d_apply_grain(Dav1dContext *const c, Dav1dPicture *const out, |
477 | | const Dav1dPicture *const in) |
478 | 2.17k | { |
479 | 2.17k | validate_input_or_ret(c != NULL, DAV1D_ERR(EINVAL)); |
480 | 2.17k | validate_input_or_ret(out != NULL, DAV1D_ERR(EINVAL)); |
481 | 2.17k | validate_input_or_ret(in != NULL, DAV1D_ERR(EINVAL)); |
482 | | |
483 | 2.17k | if (!has_grain(in)) { |
484 | 0 | dav1d_picture_ref(out, in); |
485 | 0 | return 0; |
486 | 0 | } |
487 | | |
488 | 2.17k | int res = dav1d_picture_alloc_copy(c, out, in->p.w, in); |
489 | 2.17k | if (res < 0) goto error; |
490 | | |
491 | 2.17k | if (c->n_tc > 1) { |
492 | 2.17k | dav1d_task_delayed_fg(c, out, in); |
493 | 2.17k | } 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 | 2.17k | return 0; |
511 | | |
512 | 0 | error: |
513 | 0 | dav1d_picture_unref_internal(out); |
514 | 0 | return res; |
515 | 2.17k | } |
516 | | |
517 | 43.3k | void dav1d_flush(Dav1dContext *const c) { |
518 | 43.3k | dav1d_data_unref_internal(&c->in); |
519 | 43.3k | if (c->out.p.frame_hdr) |
520 | 0 | dav1d_thread_picture_unref(&c->out); |
521 | 43.3k | if (c->cache.p.frame_hdr) |
522 | 4.18k | dav1d_thread_picture_unref(&c->cache); |
523 | | |
524 | 43.3k | c->drain = 0; |
525 | 43.3k | c->cached_error = 0; |
526 | | |
527 | 390k | for (int i = 0; i < 8; i++) { |
528 | 346k | if (c->refs[i].p.p.frame_hdr) |
529 | 329k | dav1d_thread_picture_unref(&c->refs[i].p); |
530 | 346k | dav1d_ref_dec(&c->refs[i].segmap); |
531 | 346k | dav1d_ref_dec(&c->refs[i].refmvs); |
532 | 346k | dav1d_cdf_thread_unref(&c->cdf[i]); |
533 | 346k | } |
534 | 43.3k | c->frame_hdr = NULL; |
535 | 43.3k | c->seq_hdr = NULL; |
536 | 43.3k | dav1d_ref_dec(&c->seq_hdr_ref); |
537 | | |
538 | 43.3k | c->mastering_display = NULL; |
539 | 43.3k | c->content_light = NULL; |
540 | 43.3k | c->itut_t35 = NULL; |
541 | 43.3k | c->n_itut_t35 = 0; |
542 | 43.3k | dav1d_ref_dec(&c->mastering_display_ref); |
543 | 43.3k | dav1d_ref_dec(&c->content_light_ref); |
544 | 43.3k | dav1d_ref_dec(&c->itut_t35_ref); |
545 | | |
546 | 43.3k | dav1d_data_props_unref_internal(&c->cached_error_props); |
547 | | |
548 | 43.3k | if (c->n_fc == 1 && c->n_tc == 1) return; |
549 | 43.3k | atomic_store_explicit(c->flush, 1, memory_order_relaxed); |
550 | | |
551 | 43.3k | if (c->n_tc > 1) { |
552 | 43.3k | pthread_mutex_lock(&c->task_thread.lock); |
553 | | // stop running tasks in worker threads |
554 | 1.43M | for (unsigned i = 0; i < c->n_tc; i++) { |
555 | 1.38M | Dav1dTaskContext *const tc = &c->tc[i]; |
556 | 1.39M | while (!tc->task_thread.flushed) { |
557 | 6.37k | pthread_cond_wait(&tc->task_thread.td.cond, &c->task_thread.lock); |
558 | 6.37k | } |
559 | 1.38M | } |
560 | 303k | for (unsigned i = 0; i < c->n_fc; i++) { |
561 | 260k | c->fc[i].task_thread.task_head = NULL; |
562 | 260k | c->fc[i].task_thread.task_tail = NULL; |
563 | 260k | c->fc[i].task_thread.task_cur_prev = NULL; |
564 | 260k | atomic_init(&c->fc[i].task_thread.pending_tasks, 0); |
565 | 260k | } |
566 | 43.3k | atomic_init(&c->task_thread.first, 0); |
567 | 43.3k | c->task_thread.cur = c->n_fc; |
568 | 43.3k | atomic_store_explicit(&c->task_thread.reset_task_cur, UINT_MAX, memory_order_relaxed); |
569 | 43.3k | atomic_store_explicit(&c->task_thread.cond_signaled, 0, memory_order_relaxed); |
570 | 43.3k | pthread_mutex_unlock(&c->task_thread.lock); |
571 | 43.3k | } |
572 | | |
573 | 43.3k | if (c->n_fc > 1) { |
574 | 303k | for (unsigned n = 0, next = c->frame_thread.next; n < c->n_fc; n++, next++) { |
575 | 260k | if (next == c->n_fc) next = 0; |
576 | 260k | Dav1dFrameContext *const f = &c->fc[next]; |
577 | 260k | dav1d_decode_frame_exit(f, -1); |
578 | 260k | f->n_tile_data = 0; |
579 | 260k | f->task_thread.retval = 0; |
580 | 260k | atomic_store_explicit(&f->task_thread.error, 0, memory_order_relaxed); |
581 | 260k | Dav1dThreadPicture *out_delayed = &c->frame_thread.out_delayed[next]; |
582 | 260k | if (out_delayed->p.frame_hdr) { |
583 | 6.19k | dav1d_thread_picture_unref(out_delayed); |
584 | 6.19k | } |
585 | 260k | } |
586 | 43.3k | c->frame_thread.next = 0; |
587 | 43.3k | } |
588 | 43.3k | atomic_store_explicit(c->flush, 0, memory_order_relaxed); |
589 | 43.3k | } |
590 | | |
591 | 43.3k | COLD void dav1d_close(Dav1dContext **const c_out) { |
592 | 43.3k | validate_input(c_out != NULL); |
593 | | #if TRACK_HEAP_ALLOCATIONS |
594 | | dav1d_log_alloc_stats(*c_out); |
595 | | #endif |
596 | 43.3k | close_internal(c_out, 1); |
597 | 43.3k | } |
598 | | |
599 | 43.3k | static COLD void close_internal(Dav1dContext **const c_out, int flush) { |
600 | 43.3k | Dav1dContext *const c = *c_out; |
601 | 43.3k | if (!c) return; |
602 | | |
603 | 43.3k | if (flush) dav1d_flush(c); |
604 | | |
605 | 43.3k | if (c->tc) { |
606 | 43.3k | struct TaskThreadData *ttd = &c->task_thread; |
607 | 43.3k | if (ttd->inited) { |
608 | 43.3k | pthread_mutex_lock(&ttd->lock); |
609 | 1.43M | for (unsigned n = 0; n < c->n_tc && c->tc[n].task_thread.td.inited; n++) |
610 | 1.38M | c->tc[n].task_thread.die = 1; |
611 | 43.3k | pthread_cond_broadcast(&ttd->cond); |
612 | 43.3k | pthread_mutex_unlock(&ttd->lock); |
613 | 1.43M | for (unsigned n = 0; n < c->n_tc; n++) { |
614 | 1.38M | Dav1dTaskContext *const pf = &c->tc[n]; |
615 | 1.38M | if (!pf->task_thread.td.inited) break; |
616 | 1.38M | pthread_join(pf->task_thread.td.thread, NULL); |
617 | 1.38M | pthread_cond_destroy(&pf->task_thread.td.cond); |
618 | 1.38M | } |
619 | 43.3k | pthread_cond_destroy(&ttd->delayed_fg.cond); |
620 | 43.3k | pthread_cond_destroy(&ttd->cond); |
621 | 43.3k | pthread_mutex_destroy(&ttd->lock); |
622 | 43.3k | } |
623 | 43.3k | dav1d_free_aligned(c->tc); |
624 | 43.3k | } |
625 | | |
626 | 303k | for (unsigned n = 0; c->fc && n < c->n_fc; n++) { |
627 | 260k | Dav1dFrameContext *const f = &c->fc[n]; |
628 | | |
629 | | // clean-up threading stuff |
630 | 260k | if (c->n_fc > 1) { |
631 | 260k | dav1d_free(f->tile_thread.lowest_pixel_mem); |
632 | 260k | dav1d_free(f->frame_thread.b); |
633 | 260k | dav1d_free_aligned(f->frame_thread.cbi); |
634 | 260k | dav1d_free_aligned(f->frame_thread.pal_idx); |
635 | 260k | dav1d_free_aligned(f->frame_thread.cf); |
636 | 260k | dav1d_free(f->frame_thread.tile_start_off); |
637 | 260k | dav1d_free_aligned(f->frame_thread.pal); |
638 | 260k | } |
639 | 260k | if (c->n_tc > 1) { |
640 | 260k | pthread_cond_destroy(&f->task_thread.cond); |
641 | 260k | } |
642 | 260k | dav1d_free(f->frame_thread.frame_progress); |
643 | 260k | dav1d_free(f->task_thread.tasks); |
644 | 260k | dav1d_free(f->task_thread.tile_tasks[0]); |
645 | 260k | dav1d_free_aligned(f->ts); |
646 | 260k | dav1d_free_aligned(f->ipred_edge[0]); |
647 | 260k | dav1d_free_aligned(f->a); |
648 | 260k | dav1d_free(f->tile); |
649 | 260k | dav1d_free(f->lf.mask); |
650 | 260k | dav1d_free_aligned(f->lf.level[0]); |
651 | 260k | dav1d_free(f->lf.lr_mask); |
652 | 260k | dav1d_free(f->lf.tx_lpf_right_edge[0]); |
653 | 260k | dav1d_free(f->lf.start_of_tile_row); |
654 | 260k | dav1d_free_aligned(f->rf.r); |
655 | 260k | dav1d_free_aligned(f->lf.cdef_line_buf); |
656 | 260k | dav1d_free_aligned(f->lf.lr_line_buf); |
657 | 260k | } |
658 | 43.3k | dav1d_free_aligned(c->fc); |
659 | 43.3k | if (c->n_fc > 1 && c->frame_thread.out_delayed) { |
660 | 303k | for (unsigned n = 0; n < c->n_fc; n++) |
661 | 260k | if (c->frame_thread.out_delayed[n].p.frame_hdr) |
662 | 0 | dav1d_thread_picture_unref(&c->frame_thread.out_delayed[n]); |
663 | 43.3k | dav1d_free(c->frame_thread.out_delayed); |
664 | 43.3k | } |
665 | 43.5k | for (int n = 0; n < c->n_tile_data; n++) |
666 | 163 | dav1d_data_unref_internal(&c->tile[n].data); |
667 | 43.3k | dav1d_free(c->tile); |
668 | 390k | for (int n = 0; n < 8; n++) { |
669 | 346k | dav1d_cdf_thread_unref(&c->cdf[n]); |
670 | 346k | if (c->refs[n].p.p.frame_hdr) |
671 | 0 | dav1d_thread_picture_unref(&c->refs[n].p); |
672 | 346k | dav1d_ref_dec(&c->refs[n].refmvs); |
673 | 346k | dav1d_ref_dec(&c->refs[n].segmap); |
674 | 346k | } |
675 | 43.3k | dav1d_ref_dec(&c->seq_hdr_ref); |
676 | 43.3k | dav1d_ref_dec(&c->frame_hdr_ref); |
677 | | |
678 | 43.3k | dav1d_ref_dec(&c->mastering_display_ref); |
679 | 43.3k | dav1d_ref_dec(&c->content_light_ref); |
680 | 43.3k | dav1d_ref_dec(&c->itut_t35_ref); |
681 | | |
682 | 43.3k | dav1d_mem_pool_end(c->seq_hdr_pool); |
683 | 43.3k | dav1d_mem_pool_end(c->frame_hdr_pool); |
684 | 43.3k | dav1d_mem_pool_end(c->segmap_pool); |
685 | 43.3k | dav1d_mem_pool_end(c->refmvs_pool); |
686 | 43.3k | dav1d_mem_pool_end(c->cdf_pool); |
687 | 43.3k | dav1d_mem_pool_end(c->picture_pool); |
688 | 43.3k | dav1d_mem_pool_end(c->pic_ctx_pool); |
689 | | |
690 | 43.3k | dav1d_freep_aligned(c_out); |
691 | 43.3k | } |
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 | 25.8k | void dav1d_picture_unref(Dav1dPicture *const p) { |
714 | 25.8k | dav1d_picture_unref_internal(p); |
715 | 25.8k | } |
716 | | |
717 | 43.3k | uint8_t *dav1d_data_create(Dav1dData *const buf, const size_t sz) { |
718 | 43.3k | return dav1d_data_create_internal(buf, sz); |
719 | 43.3k | } |
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 | 2.21k | void dav1d_data_unref(Dav1dData *const buf) { |
743 | 2.21k | dav1d_data_unref_internal(buf); |
744 | 2.21k | } |
745 | | |
746 | 0 | void dav1d_data_props_unref(Dav1dDataProps *const props) { |
747 | 0 | dav1d_data_props_unref_internal(props); |
748 | 0 | } |