Coverage Report

Created: 2026-05-30 06:06

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/work/dav1d/src/picture.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 <errno.h>
31
#include <stdint.h>
32
#include <stdio.h>
33
#include <stdlib.h>
34
#include <string.h>
35
36
#include "common/intops.h"
37
#include "common/validate.h"
38
39
#include "src/internal.h"
40
#include "src/log.h"
41
#include "src/picture.h"
42
#include "src/ref.h"
43
#include "src/thread.h"
44
#include "src/thread_task.h"
45
46
25.1k
int dav1d_default_picture_alloc(Dav1dPicture *const p, void *const cookie) {
47
25.1k
    const int hbd = p->p.bpc > 8;
48
25.1k
    const int aligned_w = (p->p.w + 127) & ~127;
49
25.1k
    const int aligned_h = (p->p.h + 127) & ~127;
50
25.1k
    const int has_chroma = p->p.layout != DAV1D_PIXEL_LAYOUT_I400;
51
25.1k
    const int ss_ver = p->p.layout == DAV1D_PIXEL_LAYOUT_I420;
52
25.1k
    const int ss_hor = p->p.layout != DAV1D_PIXEL_LAYOUT_I444;
53
25.1k
    ptrdiff_t y_stride = aligned_w << hbd;
54
25.1k
    ptrdiff_t uv_stride = has_chroma ? y_stride >> ss_hor : 0;
55
    /* Due to how mapping of addresses to sets works in most L1 and L2 cache
56
     * implementations, strides of multiples of certain power-of-two numbers
57
     * may cause multiple rows of the same superblock to map to the same set,
58
     * causing evictions of previous rows resulting in a reduction in cache
59
     * hit rate. Avoid that by slightly padding the stride when necessary. */
60
25.1k
    if (!(y_stride & 1023))
61
1.12k
        y_stride += DAV1D_PICTURE_ALIGNMENT;
62
25.1k
    if (!(uv_stride & 1023) && has_chroma)
63
735
        uv_stride += DAV1D_PICTURE_ALIGNMENT;
64
25.1k
    p->stride[0] = y_stride;
65
25.1k
    p->stride[1] = uv_stride;
66
25.1k
    const size_t y_sz = y_stride * aligned_h;
67
25.1k
    const size_t uv_sz = uv_stride * (aligned_h >> ss_ver);
68
25.1k
    const size_t pic_size = y_sz + 2 * uv_sz;
69
70
25.1k
    uint8_t *const buf = dav1d_mem_pool_pop(cookie, pic_size + DAV1D_PICTURE_ALIGNMENT);
71
25.1k
    if (!buf) return DAV1D_ERR(ENOMEM);
72
25.1k
    p->allocator_data = buf;
73
25.1k
    p->data[0] = buf;
74
25.1k
    p->data[1] = has_chroma ? buf + y_sz : NULL;
75
25.1k
    p->data[2] = has_chroma ? buf + y_sz + uv_sz : NULL;
76
77
25.1k
    return 0;
78
25.1k
}
79
80
25.1k
void dav1d_default_picture_release(Dav1dPicture *const p, void *const cookie) {
81
25.1k
    dav1d_mem_pool_push(cookie, p->allocator_data);
82
25.1k
}
83
84
struct pic_ctx_context {
85
    Dav1dPicAllocator allocator;
86
    Dav1dPicture pic;
87
    Dav1dRef ref;
88
    void *extra_data[];
89
};
90
91
25.1k
static void free_buffer(const uint8_t *const data, void *const user_data) {
92
25.1k
    struct pic_ctx_context *pic_ctx = (struct pic_ctx_context*)data;
93
94
25.1k
    pic_ctx->allocator.release_picture_callback(&pic_ctx->pic,
95
25.1k
                                                pic_ctx->allocator.cookie);
96
25.1k
    dav1d_mem_pool_push(user_data, pic_ctx);
97
25.1k
}
98
99
4
void dav1d_picture_free_itut_t35(const uint8_t *const data, void *const user_data) {
100
4
    struct itut_t35_ctx_context *itut_t35_ctx = user_data;
101
102
8
    for (size_t i = 0; i < itut_t35_ctx->n_itut_t35; i++)
103
4
        dav1d_free(itut_t35_ctx->itut_t35[i].payload);
104
4
    dav1d_free(itut_t35_ctx->itut_t35);
105
4
    dav1d_free(itut_t35_ctx);
106
4
}
107
108
static int picture_alloc(Dav1dContext *const c,
109
                         Dav1dPicture *const p,
110
                         const int w, const int h,
111
                         Dav1dSequenceHeader *const seq_hdr, Dav1dRef *const seq_hdr_ref,
112
                         Dav1dFrameHeader *const frame_hdr, Dav1dRef *const frame_hdr_ref,
113
                         const int bpc,
114
                         const Dav1dDataProps *const props,
115
                         Dav1dPicAllocator *const p_allocator,
116
                         void **const extra_ptr)
117
25.1k
{
118
25.1k
    if (p->data[0]) {
119
0
        dav1d_log(c, "Picture already allocated!\n");
120
0
        return -1;
121
0
    }
122
25.1k
    assert(bpc > 0 && bpc <= 16);
123
124
25.1k
    size_t extra = c->n_fc > 1 ? sizeof(atomic_int) * 2 : 0;
125
25.1k
    struct pic_ctx_context *pic_ctx = dav1d_mem_pool_pop(c->pic_ctx_pool, extra +
126
25.1k
                                                         sizeof(struct pic_ctx_context));
127
25.1k
    if (!pic_ctx)
128
0
        return DAV1D_ERR(ENOMEM);
129
130
25.1k
    p->p.w = w;
131
25.1k
    p->p.h = h;
132
25.1k
    p->seq_hdr = seq_hdr;
133
25.1k
    p->frame_hdr = frame_hdr;
134
25.1k
    p->p.layout = seq_hdr->layout;
135
25.1k
    p->p.bpc = bpc;
136
25.1k
    dav1d_data_props_set_defaults(&p->m);
137
25.1k
    const int res = p_allocator->alloc_picture_callback(p, p_allocator->cookie);
138
25.1k
    if (res < 0) {
139
0
        dav1d_mem_pool_push(c->pic_ctx_pool, pic_ctx);
140
0
        return res;
141
0
    }
142
143
25.1k
    pic_ctx->allocator = *p_allocator;
144
25.1k
    pic_ctx->pic = *p;
145
25.1k
    p->ref = dav1d_ref_init(&pic_ctx->ref, pic_ctx, free_buffer, c->pic_ctx_pool, 0);
146
147
25.1k
    p->seq_hdr_ref = seq_hdr_ref;
148
25.1k
    if (seq_hdr_ref) dav1d_ref_inc(seq_hdr_ref);
149
150
25.1k
    p->frame_hdr_ref = frame_hdr_ref;
151
25.1k
    if (frame_hdr_ref) dav1d_ref_inc(frame_hdr_ref);
152
153
25.1k
    if (extra && extra_ptr)
154
20.4k
        *extra_ptr = &pic_ctx->extra_data;
155
156
25.1k
    return 0;
157
25.1k
}
158
159
void dav1d_picture_copy_props(Dav1dPicture *const p,
160
                              Dav1dContentLightLevel *const content_light, Dav1dRef *const content_light_ref,
161
                              Dav1dMasteringDisplay *const mastering_display, Dav1dRef *const mastering_display_ref,
162
                              Dav1dITUTT35 *const itut_t35, Dav1dRef *itut_t35_ref, size_t n_itut_t35,
163
                              const Dav1dDataProps *const props)
164
20.5k
{
165
20.5k
    dav1d_data_props_copy(&p->m, props);
166
167
20.5k
    dav1d_ref_dec(&p->content_light_ref);
168
20.5k
    p->content_light_ref = content_light_ref;
169
20.5k
    p->content_light = content_light;
170
20.5k
    if (content_light_ref) dav1d_ref_inc(content_light_ref);
171
172
20.5k
    dav1d_ref_dec(&p->mastering_display_ref);
173
20.5k
    p->mastering_display_ref = mastering_display_ref;
174
20.5k
    p->mastering_display = mastering_display;
175
20.5k
    if (mastering_display_ref) dav1d_ref_inc(mastering_display_ref);
176
177
20.5k
    dav1d_ref_dec(&p->itut_t35_ref);
178
20.5k
    p->itut_t35_ref = itut_t35_ref;
179
20.5k
    p->itut_t35 = itut_t35;
180
20.5k
    p->n_itut_t35 = n_itut_t35;
181
20.5k
    if (itut_t35_ref) dav1d_ref_inc(itut_t35_ref);
182
20.5k
}
183
184
int dav1d_thread_picture_alloc(Dav1dContext *const c, Dav1dFrameContext *const f,
185
                               const int bpc)
186
20.4k
{
187
20.4k
    Dav1dThreadPicture *const p = &f->sr_cur;
188
189
20.4k
    const int res = picture_alloc(c, &p->p, f->frame_hdr->width[1], f->frame_hdr->height,
190
20.4k
                                  f->seq_hdr, f->seq_hdr_ref,
191
20.4k
                                  f->frame_hdr, f->frame_hdr_ref,
192
20.4k
                                  bpc, &f->tile[0].data.m, &c->allocator,
193
20.4k
                                  (void **) &p->progress);
194
20.4k
    if (res) return res;
195
196
    // Don't clear these flags from c->frame_flags if the frame is not going to be output.
197
    // This way they will be added to the next visible frame too.
198
20.4k
    const int flags_mask = ((f->frame_hdr->show_frame || c->output_invisible_frames) &&
199
15.7k
                            c->max_spatial_id == f->frame_hdr->spatial_id)
200
20.4k
                           ? 0 : (PICTURE_FLAG_NEW_SEQUENCE | PICTURE_FLAG_NEW_OP_PARAMS_INFO);
201
20.4k
    p->flags = c->frame_flags;
202
20.4k
    c->frame_flags &= flags_mask;
203
204
20.4k
    p->visible = f->frame_hdr->show_frame;
205
20.4k
    p->showable = f->frame_hdr->showable_frame;
206
207
20.4k
    if (p->visible) {
208
        // Only add HDR10+ and T35 metadata when show frame flag is enabled
209
15.7k
        dav1d_picture_copy_props(&p->p, c->content_light, c->content_light_ref,
210
15.7k
                                 c->mastering_display, c->mastering_display_ref,
211
15.7k
                                 c->itut_t35, c->itut_t35_ref, c->n_itut_t35,
212
15.7k
                                 &f->tile[0].data.m);
213
214
        // Must be removed from the context after being attached to the frame
215
15.7k
        dav1d_ref_dec(&c->itut_t35_ref);
216
15.7k
        c->itut_t35 = NULL;
217
15.7k
        c->n_itut_t35 = 0;
218
15.7k
    } else {
219
4.68k
        dav1d_data_props_copy(&p->p.m, &f->tile[0].data.m);
220
4.68k
    }
221
222
20.4k
    if (c->n_fc > 1) {
223
20.4k
        atomic_init(&p->progress[0], 0);
224
20.4k
        atomic_init(&p->progress[1], 0);
225
20.4k
    }
226
20.4k
    return res;
227
20.4k
}
228
229
int dav1d_picture_alloc_copy(Dav1dContext *const c, Dav1dPicture *const dst, const int w,
230
                             const Dav1dPicture *const src)
231
4.76k
{
232
4.76k
    struct pic_ctx_context *const pic_ctx = (struct pic_ctx_context*)src->ref->const_data;
233
4.76k
    const int res = picture_alloc(c, dst, w, src->p.h,
234
4.76k
                                  src->seq_hdr, src->seq_hdr_ref,
235
4.76k
                                  src->frame_hdr, src->frame_hdr_ref,
236
4.76k
                                  src->p.bpc, &src->m, &pic_ctx->allocator,
237
4.76k
                                  NULL);
238
4.76k
    if (res) return res;
239
240
4.76k
    dav1d_picture_copy_props(dst, src->content_light, src->content_light_ref,
241
4.76k
                             src->mastering_display, src->mastering_display_ref,
242
4.76k
                             src->itut_t35, src->itut_t35_ref, src->n_itut_t35,
243
4.76k
                             &src->m);
244
245
4.76k
    return 0;
246
4.76k
}
247
248
205k
void dav1d_picture_ref(Dav1dPicture *const dst, const Dav1dPicture *const src) {
249
205k
    assert(dst != NULL);
250
205k
    assert(dst->data[0] == NULL);
251
205k
    assert(src != NULL);
252
253
205k
    if (src->ref) {
254
205k
        assert(src->data[0] != NULL);
255
205k
        dav1d_ref_inc(src->ref);
256
205k
    }
257
205k
    if (src->frame_hdr_ref) dav1d_ref_inc(src->frame_hdr_ref);
258
205k
    if (src->seq_hdr_ref) dav1d_ref_inc(src->seq_hdr_ref);
259
205k
    if (src->m.user_data.ref) dav1d_ref_inc(src->m.user_data.ref);
260
205k
    if (src->content_light_ref) dav1d_ref_inc(src->content_light_ref);
261
205k
    if (src->mastering_display_ref) dav1d_ref_inc(src->mastering_display_ref);
262
205k
    if (src->itut_t35_ref) dav1d_ref_inc(src->itut_t35_ref);
263
205k
    *dst = *src;
264
205k
}
265
266
11.4k
void dav1d_picture_move_ref(Dav1dPicture *const dst, Dav1dPicture *const src) {
267
11.4k
    assert(dst != NULL);
268
11.4k
    assert(dst->data[0] == NULL);
269
11.4k
    assert(src != NULL);
270
271
11.4k
    if (src->ref)
272
11.4k
        assert(src->data[0] != NULL);
273
274
11.4k
    *dst = *src;
275
11.4k
    memset(src, 0, sizeof(*src));
276
11.4k
}
277
278
void dav1d_thread_picture_ref(Dav1dThreadPicture *const dst,
279
                              const Dav1dThreadPicture *const src)
280
188k
{
281
188k
    dav1d_picture_ref(&dst->p, &src->p);
282
188k
    dst->visible = src->visible;
283
188k
    dst->showable = src->showable;
284
188k
    dst->progress = src->progress;
285
188k
    dst->flags = src->flags;
286
188k
}
287
288
void dav1d_thread_picture_move_ref(Dav1dThreadPicture *const dst,
289
                                   Dav1dThreadPicture *const src)
290
3.94k
{
291
3.94k
    dav1d_picture_move_ref(&dst->p, &src->p);
292
3.94k
    dst->visible = src->visible;
293
3.94k
    dst->showable = src->showable;
294
3.94k
    dst->progress = src->progress;
295
3.94k
    dst->flags = src->flags;
296
3.94k
    memset(src, 0, sizeof(*src));
297
3.94k
}
298
299
464k
void dav1d_picture_unref_internal(Dav1dPicture *const p) {
300
464k
    validate_input(p != NULL);
301
302
464k
    if (p->ref) {
303
230k
        validate_input(p->data[0] != NULL);
304
230k
        dav1d_ref_dec(&p->ref);
305
230k
    }
306
464k
    dav1d_ref_dec(&p->seq_hdr_ref);
307
464k
    dav1d_ref_dec(&p->frame_hdr_ref);
308
464k
    dav1d_ref_dec(&p->m.user_data.ref);
309
464k
    dav1d_ref_dec(&p->content_light_ref);
310
464k
    dav1d_ref_dec(&p->mastering_display_ref);
311
464k
    dav1d_ref_dec(&p->itut_t35_ref);
312
464k
    memset(p, 0, sizeof(*p));
313
464k
    dav1d_data_props_set_defaults(&p->m);
314
464k
}
315
316
322k
void dav1d_thread_picture_unref(Dav1dThreadPicture *const p) {
317
322k
    dav1d_picture_unref_internal(&p->p);
318
319
322k
    p->progress = NULL;
320
322k
}
321
322
10.3k
enum Dav1dEventFlags dav1d_picture_get_event_flags(const Dav1dThreadPicture *const p) {
323
10.3k
    if (!p->flags)
324
1
        return 0;
325
326
10.3k
    enum Dav1dEventFlags flags = 0;
327
10.3k
    if (p->flags & PICTURE_FLAG_NEW_SEQUENCE)
328
10.3k
       flags |= DAV1D_EVENT_FLAG_NEW_SEQUENCE;
329
10.3k
    if (p->flags & PICTURE_FLAG_NEW_OP_PARAMS_INFO)
330
0
       flags |= DAV1D_EVENT_FLAG_NEW_OP_PARAMS_INFO;
331
332
10.3k
    return flags;
333
10.3k
}