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 <stdlib.h> |
33 | | #include <string.h> |
34 | | |
35 | | #include "dav1d/data.h" |
36 | | |
37 | | #include "common/attributes.h" |
38 | | #include "common/validate.h" |
39 | | |
40 | | #include "src/data.h" |
41 | | #include "src/ref.h" |
42 | | |
43 | 0 | uint8_t *dav1d_data_create_internal(Dav1dData *const buf, const size_t sz) { |
44 | 0 | validate_input_or_ret(buf != NULL, NULL); |
45 | |
|
46 | 0 | if (sz > SIZE_MAX / 2) return NULL; |
47 | 0 | buf->ref = dav1d_ref_create(ALLOC_DAV1DDATA, sz); |
48 | 0 | if (!buf->ref) return NULL; |
49 | 0 | buf->data = buf->ref->const_data; |
50 | 0 | buf->sz = sz; |
51 | 0 | dav1d_data_props_set_defaults(&buf->m); |
52 | 0 | buf->m.size = sz; |
53 | |
|
54 | 0 | return buf->ref->data; |
55 | 0 | } |
56 | | |
57 | | int dav1d_data_wrap_internal(Dav1dData *const buf, const uint8_t *const ptr, |
58 | | const size_t sz, |
59 | | void (*const free_callback)(const uint8_t *data, |
60 | | void *cookie), |
61 | | void *const cookie) |
62 | 0 | { |
63 | 0 | validate_input_or_ret(buf != NULL, DAV1D_ERR(EINVAL)); |
64 | 0 | validate_input_or_ret(ptr != NULL, DAV1D_ERR(EINVAL)); |
65 | 0 | validate_input_or_ret(free_callback != NULL, DAV1D_ERR(EINVAL)); |
66 | |
|
67 | 0 | if (sz > SIZE_MAX / 2) return DAV1D_ERR(EINVAL); |
68 | 0 | Dav1dRef *const ref = dav1d_malloc(ALLOC_DAV1DDATA, sizeof(Dav1dRef)); |
69 | 0 | if (!ref) return DAV1D_ERR(ENOMEM); |
70 | | |
71 | 0 | buf->ref = dav1d_ref_init(ref, ptr, free_callback, cookie, 1); |
72 | 0 | buf->data = ptr; |
73 | 0 | buf->sz = sz; |
74 | 0 | dav1d_data_props_set_defaults(&buf->m); |
75 | 0 | buf->m.size = sz; |
76 | |
|
77 | 0 | return 0; |
78 | 0 | } |
79 | | |
80 | | int dav1d_data_wrap_user_data_internal(Dav1dData *const buf, |
81 | | const uint8_t *const user_data, |
82 | | void (*const free_callback)(const uint8_t *user_data, |
83 | | void *cookie), |
84 | | void *const cookie) |
85 | 0 | { |
86 | 0 | validate_input_or_ret(buf != NULL, DAV1D_ERR(EINVAL)); |
87 | 0 | validate_input_or_ret(free_callback != NULL, DAV1D_ERR(EINVAL)); |
88 | |
|
89 | 0 | Dav1dRef *const ref = dav1d_malloc(ALLOC_DAV1DDATA, sizeof(Dav1dRef)); |
90 | 0 | if (!ref) return DAV1D_ERR(ENOMEM); |
91 | | |
92 | 0 | buf->m.user_data.ref = dav1d_ref_init(ref, user_data, free_callback, cookie, 1); |
93 | 0 | buf->m.user_data.data = user_data; |
94 | |
|
95 | 0 | return 0; |
96 | 0 | } |
97 | | |
98 | 0 | void dav1d_data_ref(Dav1dData *const dst, const Dav1dData *const src) { |
99 | 0 | assert(dst != NULL); |
100 | 0 | assert(dst->data == NULL); |
101 | 0 | assert(src != NULL); |
102 | | |
103 | 0 | if (src->ref) { |
104 | 0 | assert(src->data != NULL); |
105 | 0 | dav1d_ref_inc(src->ref); |
106 | 0 | } |
107 | 0 | if (src->m.user_data.ref) dav1d_ref_inc(src->m.user_data.ref); |
108 | 0 | *dst = *src; |
109 | 0 | } |
110 | | |
111 | | void dav1d_data_props_copy(Dav1dDataProps *const dst, |
112 | | const Dav1dDataProps *const src) |
113 | 0 | { |
114 | 0 | assert(dst != NULL); |
115 | 0 | assert(src != NULL); |
116 | | |
117 | 0 | dav1d_ref_dec(&dst->user_data.ref); |
118 | 0 | *dst = *src; |
119 | 0 | if (dst->user_data.ref) dav1d_ref_inc(dst->user_data.ref); |
120 | 0 | } |
121 | | |
122 | 0 | void dav1d_data_props_set_defaults(Dav1dDataProps *const props) { |
123 | 0 | assert(props != NULL); |
124 | | |
125 | 0 | memset(props, 0, sizeof(*props)); |
126 | 0 | props->timestamp = INT64_MIN; |
127 | 0 | props->offset = -1; |
128 | 0 | } |
129 | | |
130 | 0 | void dav1d_data_props_unref_internal(Dav1dDataProps *const props) { |
131 | 0 | validate_input(props != NULL); |
132 | |
|
133 | 0 | struct Dav1dRef *user_data_ref = props->user_data.ref; |
134 | 0 | dav1d_data_props_set_defaults(props); |
135 | 0 | dav1d_ref_dec(&user_data_ref); |
136 | 0 | } |
137 | | |
138 | 0 | void dav1d_data_unref_internal(Dav1dData *const buf) { |
139 | 0 | validate_input(buf != NULL); |
140 | |
|
141 | 0 | struct Dav1dRef *user_data_ref = buf->m.user_data.ref; |
142 | 0 | if (buf->ref) { |
143 | 0 | validate_input(buf->data != NULL); |
144 | 0 | dav1d_ref_dec(&buf->ref); |
145 | 0 | } |
146 | 0 | memset(buf, 0, sizeof(*buf)); |
147 | 0 | dav1d_data_props_set_defaults(&buf->m); |
148 | 0 | dav1d_ref_dec(&user_data_ref); |
149 | 0 | } |