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