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(sz);
48     if (!buf->ref) return NULL;
49     buf->data = buf->ref->const_data;
50     buf->sz = buf->m.size = sz;
51     dav1d_data_props_set_defaults(&buf->m);
52 
53     return buf->ref->data;
54 }
55 
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)56 int dav1d_data_wrap_internal(Dav1dData *const buf, const uint8_t *const ptr,
57                              const size_t sz,
58                              void (*const free_callback)(const uint8_t *data,
59                                                          void *cookie),
60                              void *const cookie)
61 {
62     validate_input_or_ret(buf != NULL, DAV1D_ERR(EINVAL));
63     validate_input_or_ret(ptr != NULL, DAV1D_ERR(EINVAL));
64     validate_input_or_ret(free_callback != NULL, DAV1D_ERR(EINVAL));
65 
66     buf->ref = dav1d_ref_wrap(ptr, free_callback, cookie);
67     if (!buf->ref) return DAV1D_ERR(ENOMEM);
68     buf->data = ptr;
69     buf->sz = buf->m.size = sz;
70     dav1d_data_props_set_defaults(&buf->m);
71 
72     return 0;
73 }
74 
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)75 int dav1d_data_wrap_user_data_internal(Dav1dData *const buf,
76                                        const uint8_t *const user_data,
77                                        void (*const free_callback)(const uint8_t *user_data,
78                                                                    void *cookie),
79                                        void *const cookie)
80 {
81     validate_input_or_ret(buf != NULL, DAV1D_ERR(EINVAL));
82     validate_input_or_ret(free_callback != NULL, DAV1D_ERR(EINVAL));
83 
84     buf->m.user_data.ref = dav1d_ref_wrap(user_data, free_callback, cookie);
85     if (!buf->m.user_data.ref) return DAV1D_ERR(ENOMEM);
86     buf->m.user_data.data = user_data;
87 
88     return 0;
89 }
90 
91 
dav1d_data_ref(Dav1dData * const dst,const Dav1dData * const src)92 void dav1d_data_ref(Dav1dData *const dst, const Dav1dData *const src) {
93     validate_input(dst != NULL);
94     validate_input(dst->data == NULL);
95     validate_input(src != NULL);
96 
97     if (src->ref) {
98         validate_input(src->data != NULL);
99         dav1d_ref_inc(src->ref);
100     }
101     if (src->m.user_data.ref) dav1d_ref_inc(src->m.user_data.ref);
102     *dst = *src;
103 }
104 
dav1d_data_props_copy(Dav1dDataProps * const dst,const Dav1dDataProps * const src)105 void dav1d_data_props_copy(Dav1dDataProps *const dst,
106                            const Dav1dDataProps *const src)
107 {
108     assert(dst != NULL);
109     assert(src != NULL);
110 
111     dav1d_ref_dec(&dst->user_data.ref);
112     *dst = *src;
113     if (dst->user_data.ref) dav1d_ref_inc(dst->user_data.ref);
114 }
115 
dav1d_data_props_set_defaults(Dav1dDataProps * const props)116 void dav1d_data_props_set_defaults(Dav1dDataProps *const props) {
117     assert(props != NULL);
118 
119     props->timestamp = INT64_MIN;
120     props->duration = 0;
121     props->offset = -1;
122     props->user_data.data = NULL;
123     props->user_data.ref = NULL;
124 }
125 
dav1d_data_unref_internal(Dav1dData * const buf)126 void dav1d_data_unref_internal(Dav1dData *const buf) {
127     validate_input(buf != NULL);
128 
129     struct Dav1dRef *user_data_ref = buf->m.user_data.ref;
130     if (buf->ref) {
131         validate_input(buf->data != NULL);
132         dav1d_ref_dec(&buf->ref);
133     }
134     memset(buf, 0, sizeof(*buf));
135     dav1d_ref_dec(&user_data_ref);
136 }
137