Coverage Report

Created: 2026-07-16 06:31

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
65.4k
int dav1d_default_picture_alloc(Dav1dPicture *const p, void *const cookie) {
47
65.4k
    const int hbd = p->p.bpc > 8;
48
65.4k
    const int aligned_w = (p->p.w + 127) & ~127;
49
65.4k
    const int aligned_h = (p->p.h + 127) & ~127;
50
65.4k
    const int has_chroma = p->p.layout != DAV1D_PIXEL_LAYOUT_I400;
51
65.4k
    const int ss_ver = p->p.layout == DAV1D_PIXEL_LAYOUT_I420;
52
65.4k
    const int ss_hor = p->p.layout != DAV1D_PIXEL_LAYOUT_I444;
53
65.4k
    ptrdiff_t y_stride = aligned_w << hbd;
54
65.4k
    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
65.4k
    if (!(y_stride & 1023))
61
1.98k
        y_stride += DAV1D_PICTURE_ALIGNMENT;
62
65.4k
    if (!(uv_stride & 1023) && has_chroma)
63
1.54k
        uv_stride += DAV1D_PICTURE_ALIGNMENT;
64
65.4k
    p->stride[0] = y_stride;
65
65.4k
    p->stride[1] = uv_stride;
66
65.4k
    const size_t y_sz = y_stride * aligned_h;
67
65.4k
    const size_t uv_sz = uv_stride * (aligned_h >> ss_ver);
68
65.4k
    const size_t pic_size = y_sz + 2 * uv_sz;
69
70
65.4k
    uint8_t *const buf = dav1d_mem_pool_pop(cookie, pic_size + DAV1D_PICTURE_ALIGNMENT);
71
65.4k
    if (!buf) return DAV1D_ERR(ENOMEM);
72
65.4k
    p->allocator_data = buf;
73
65.4k
    p->data[0] = buf;
74
65.4k
    p->data[1] = has_chroma ? buf + y_sz : NULL;
75
65.4k
    p->data[2] = has_chroma ? buf + y_sz + uv_sz : NULL;
76
77
65.4k
    return 0;
78
65.4k
}
79
80
65.4k
void dav1d_default_picture_release(Dav1dPicture *const p, void *const cookie) {
81
65.4k
    dav1d_mem_pool_push(cookie, p->allocator_data);
82
65.4k
}
83
84
struct pic_ctx_context {
85
    Dav1dPicAllocator allocator;
86
    Dav1dPicture pic;
87
    Dav1dRef ref;
88
    void *extra_data[];
89
};
90
91
65.4k
static void free_buffer(const uint8_t *const data, void *const user_data) {
92
65.4k
    struct pic_ctx_context *pic_ctx = (struct pic_ctx_context*)data;
93
94
65.4k
    pic_ctx->allocator.release_picture_callback(&pic_ctx->pic,
95
65.4k
                                                pic_ctx->allocator.cookie);
96
65.4k
    dav1d_mem_pool_push(user_data, pic_ctx);
97
65.4k
}
98
99
6
void dav1d_picture_free_itut_t35(const uint8_t *const data, void *const user_data) {
100
6
    struct itut_t35_ctx_context *itut_t35_ctx = user_data;
101
102
12
    for (size_t i = 0; i < itut_t35_ctx->n_itut_t35; i++)
103
6
        dav1d_free(itut_t35_ctx->itut_t35[i].payload);
104
6
    dav1d_free(itut_t35_ctx->itut_t35);
105
6
    dav1d_free(itut_t35_ctx);
106
6
}
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
65.4k
{
118
65.4k
    if (p->data[0]) {
119
0
        dav1d_log(c, "Picture already allocated!\n");
120
0
        return -1;
121
0
    }
122
65.4k
    assert(bpc > 0 && bpc <= 16);
123
124
65.4k
    size_t extra = c->n_fc > 1 ? sizeof(atomic_int) * 2 : 0;
125
65.4k
    struct pic_ctx_context *pic_ctx = dav1d_mem_pool_pop(c->pic_ctx_pool, extra +
126
65.4k
                                                         sizeof(struct pic_ctx_context));
127
65.4k
    if (!pic_ctx)
128
0
        return DAV1D_ERR(ENOMEM);
129
130
65.4k
    p->p.w = w;
131
65.4k
    p->p.h = h;
132
65.4k
    p->seq_hdr = seq_hdr;
133
65.4k
    p->frame_hdr = frame_hdr;
134
65.4k
    p->p.layout = seq_hdr->layout;
135
65.4k
    p->p.bpc = bpc;
136
65.4k
    dav1d_data_props_set_defaults(&p->m);
137
65.4k
    const int res = p_allocator->alloc_picture_callback(p, p_allocator->cookie);
138
65.4k
    if (res < 0) {
139
0
        dav1d_mem_pool_push(c->pic_ctx_pool, pic_ctx);
140
0
        return res;
141
0
    }
142
143
65.4k
    pic_ctx->allocator = *p_allocator;
144
65.4k
    pic_ctx->pic = *p;
145
65.4k
    p->ref = dav1d_ref_init(&pic_ctx->ref, pic_ctx, free_buffer, c->pic_ctx_pool, 0);
146
147
65.4k
    p->seq_hdr_ref = seq_hdr_ref;
148
65.4k
    if (seq_hdr_ref) dav1d_ref_inc(seq_hdr_ref);
149
150
65.4k
    p->frame_hdr_ref = frame_hdr_ref;
151
65.4k
    if (frame_hdr_ref) dav1d_ref_inc(frame_hdr_ref);
152
153
65.4k
    if (extra && extra_ptr)
154
53.4k
        *extra_ptr = &pic_ctx->extra_data;
155
156
65.4k
    return 0;
157
65.4k
}
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
61.5k
{
165
61.5k
    dav1d_data_props_copy(&p->m, props);
166
167
61.5k
    dav1d_ref_dec(&p->content_light_ref);
168
61.5k
    p->content_light_ref = content_light_ref;
169
61.5k
    p->content_light = content_light;
170
61.5k
    if (content_light_ref) dav1d_ref_inc(content_light_ref);
171
172
61.5k
    dav1d_ref_dec(&p->mastering_display_ref);
173
61.5k
    p->mastering_display_ref = mastering_display_ref;
174
61.5k
    p->mastering_display = mastering_display;
175
61.5k
    if (mastering_display_ref) dav1d_ref_inc(mastering_display_ref);
176
177
61.5k
    dav1d_ref_dec(&p->itut_t35_ref);
178
61.5k
    p->itut_t35_ref = itut_t35_ref;
179
61.5k
    p->itut_t35 = itut_t35;
180
61.5k
    p->n_itut_t35 = n_itut_t35;
181
61.5k
    if (itut_t35_ref) dav1d_ref_inc(itut_t35_ref);
182
61.5k
}
183
184
int dav1d_thread_picture_alloc(Dav1dContext *const c, Dav1dFrameContext *const f,
185
                               const int bpc)
186
53.4k
{
187
53.4k
    Dav1dThreadPicture *const p = &f->sr_cur;
188
189
53.4k
    const int res = picture_alloc(c, &p->p, f->frame_hdr->width[1], f->frame_hdr->height,
190
53.4k
                                  f->seq_hdr, f->seq_hdr_ref,
191
53.4k
                                  f->frame_hdr, f->frame_hdr_ref,
192
53.4k
                                  bpc, &f->tile[0].data.m, &c->allocator,
193
53.4k
                                  (void **) &p->progress);
194
53.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
53.4k
    const int flags_mask = ((f->frame_hdr->show_frame || c->output_invisible_frames) &&
199
49.3k
                            c->max_spatial_id == f->frame_hdr->spatial_id)
200
53.4k
                           ? 0 : (PICTURE_FLAG_NEW_SEQUENCE | PICTURE_FLAG_NEW_OP_PARAMS_INFO);
201
53.4k
    p->flags = c->frame_flags;
202
53.4k
    c->frame_flags &= flags_mask;
203
204
53.4k
    p->visible = f->frame_hdr->show_frame;
205
53.4k
    p->showable = f->frame_hdr->showable_frame;
206
207
53.4k
    if (p->visible) {
208
        // Only add HDR10+ and T35 metadata when show frame flag is enabled
209
49.3k
        dav1d_picture_copy_props(&p->p, c->content_light, c->content_light_ref,
210
49.3k
                                 c->mastering_display, c->mastering_display_ref,
211
49.3k
                                 c->itut_t35, c->itut_t35_ref, c->n_itut_t35,
212
49.3k
                                 &f->tile[0].data.m);
213
214
        // Must be removed from the context after being attached to the frame
215
49.3k
        dav1d_ref_dec(&c->itut_t35_ref);
216
49.3k
        c->itut_t35 = NULL;
217
49.3k
        c->n_itut_t35 = 0;
218
49.3k
    } else {
219
4.07k
        dav1d_data_props_copy(&p->p.m, &f->tile[0].data.m);
220
4.07k
    }
221
222
53.4k
    if (c->n_fc > 1) {
223
53.4k
        atomic_init(&p->progress[0], 0);
224
53.4k
        atomic_init(&p->progress[1], 0);
225
53.4k
    }
226
53.4k
    return res;
227
53.4k
}
228
229
int dav1d_picture_alloc_copy(Dav1dContext *const c, Dav1dPicture *const dst, const int w,
230
                             const Dav1dPicture *const src)
231
12.0k
{
232
12.0k
    struct pic_ctx_context *const pic_ctx = (struct pic_ctx_context*)src->ref->const_data;
233
12.0k
    const int res = picture_alloc(c, dst, w, src->p.h,
234
12.0k
                                  src->seq_hdr, src->seq_hdr_ref,
235
12.0k
                                  src->frame_hdr, src->frame_hdr_ref,
236
12.0k
                                  src->p.bpc, &src->m, &pic_ctx->allocator,
237
12.0k
                                  NULL);
238
12.0k
    if (res) return res;
239
240
12.0k
    dav1d_picture_copy_props(dst, src->content_light, src->content_light_ref,
241
12.0k
                             src->mastering_display, src->mastering_display_ref,
242
12.0k
                             src->itut_t35, src->itut_t35_ref, src->n_itut_t35,
243
12.0k
                             &src->m);
244
245
12.0k
    return 0;
246
12.0k
}
247
248
602k
void dav1d_picture_ref(Dav1dPicture *const dst, const Dav1dPicture *const src) {
249
602k
    assert(dst != NULL);
250
602k
    assert(dst->data[0] == NULL);
251
602k
    assert(src != NULL);
252
253
602k
    if (src->ref) {
254
602k
        assert(src->data[0] != NULL);
255
602k
        dav1d_ref_inc(src->ref);
256
602k
    }
257
602k
    if (src->frame_hdr_ref) dav1d_ref_inc(src->frame_hdr_ref);
258
602k
    if (src->seq_hdr_ref) dav1d_ref_inc(src->seq_hdr_ref);
259
602k
    if (src->m.user_data.ref) dav1d_ref_inc(src->m.user_data.ref);
260
602k
    if (src->content_light_ref) dav1d_ref_inc(src->content_light_ref);
261
602k
    if (src->mastering_display_ref) dav1d_ref_inc(src->mastering_display_ref);
262
602k
    if (src->itut_t35_ref) dav1d_ref_inc(src->itut_t35_ref);
263
602k
    *dst = *src;
264
602k
}
265
266
32.9k
void dav1d_picture_move_ref(Dav1dPicture *const dst, Dav1dPicture *const src) {
267
32.9k
    assert(dst != NULL);
268
32.9k
    assert(dst->data[0] == NULL);
269
32.9k
    assert(src != NULL);
270
271
32.9k
    if (src->ref)
272
32.9k
        assert(src->data[0] != NULL);
273
274
32.9k
    *dst = *src;
275
32.9k
    memset(src, 0, sizeof(*src));
276
32.9k
}
277
278
void dav1d_thread_picture_ref(Dav1dThreadPicture *const dst,
279
                              const Dav1dThreadPicture *const src)
280
558k
{
281
558k
    dav1d_picture_ref(&dst->p, &src->p);
282
558k
    dst->visible = src->visible;
283
558k
    dst->showable = src->showable;
284
558k
    dst->progress = src->progress;
285
558k
    dst->flags = src->flags;
286
558k
}
287
288
void dav1d_thread_picture_move_ref(Dav1dThreadPicture *const dst,
289
                                   Dav1dThreadPicture *const src)
290
11.7k
{
291
11.7k
    dav1d_picture_move_ref(&dst->p, &src->p);
292
11.7k
    dst->visible = src->visible;
293
11.7k
    dst->showable = src->showable;
294
11.7k
    dst->progress = src->progress;
295
11.7k
    dst->flags = src->flags;
296
11.7k
    memset(src, 0, sizeof(*src));
297
11.7k
}
298
299
1.13M
void dav1d_picture_unref_internal(Dav1dPicture *const p) {
300
1.13M
    validate_input(p != NULL);
301
302
1.13M
    if (p->ref) {
303
668k
        validate_input(p->data[0] != NULL);
304
668k
        dav1d_ref_dec(&p->ref);
305
668k
    }
306
1.13M
    dav1d_ref_dec(&p->seq_hdr_ref);
307
1.13M
    dav1d_ref_dec(&p->frame_hdr_ref);
308
1.13M
    dav1d_ref_dec(&p->m.user_data.ref);
309
1.13M
    dav1d_ref_dec(&p->content_light_ref);
310
1.13M
    dav1d_ref_dec(&p->mastering_display_ref);
311
1.13M
    dav1d_ref_dec(&p->itut_t35_ref);
312
1.13M
    memset(p, 0, sizeof(*p));
313
1.13M
    dav1d_data_props_set_defaults(&p->m);
314
1.13M
}
315
316
835k
void dav1d_thread_picture_unref(Dav1dThreadPicture *const p) {
317
835k
    dav1d_picture_unref_internal(&p->p);
318
319
835k
    p->progress = NULL;
320
835k
}
321
322
31.6k
enum Dav1dEventFlags dav1d_picture_get_event_flags(const Dav1dThreadPicture *const p) {
323
31.6k
    if (!p->flags)
324
6
        return 0;
325
326
31.6k
    enum Dav1dEventFlags flags = 0;
327
31.6k
    if (p->flags & PICTURE_FLAG_NEW_SEQUENCE)
328
31.6k
       flags |= DAV1D_EVENT_FLAG_NEW_SEQUENCE;
329
31.6k
    if (p->flags & PICTURE_FLAG_NEW_OP_PARAMS_INFO)
330
0
       flags |= DAV1D_EVENT_FLAG_NEW_OP_PARAMS_INFO;
331
332
31.6k
    return flags;
333
31.6k
}