dfaf045036e3db092ad0c3515882034247514964
[libav.git] / libavcodec / avpacket.c
1 /*
2 * AVPacket functions for libavcodec
3 * Copyright (c) 2000, 2001, 2002 Fabrice Bellard
4 *
5 * This file is part of Libav.
6 *
7 * Libav is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * Libav is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with Libav; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20 */
21
22 #include <string.h>
23
24 #include "libavutil/avassert.h"
25 #include "libavutil/common.h"
26 #include "libavutil/internal.h"
27 #include "libavutil/mathematics.h"
28 #include "libavutil/mem.h"
29 #include "avcodec.h"
30
31 void av_init_packet(AVPacket *pkt)
32 {
33 pkt->pts = AV_NOPTS_VALUE;
34 pkt->dts = AV_NOPTS_VALUE;
35 pkt->pos = -1;
36 pkt->duration = 0;
37 pkt->convergence_duration = 0;
38 pkt->flags = 0;
39 pkt->stream_index = 0;
40 pkt->buf = NULL;
41 pkt->side_data = NULL;
42 pkt->side_data_elems = 0;
43 }
44
45 static int packet_alloc(AVBufferRef **buf, int size)
46 {
47 int ret;
48 if ((unsigned)size >= (unsigned)size + AV_INPUT_BUFFER_PADDING_SIZE)
49 return AVERROR(EINVAL);
50
51 ret = av_buffer_realloc(buf, size + AV_INPUT_BUFFER_PADDING_SIZE);
52 if (ret < 0)
53 return ret;
54
55 memset((*buf)->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
56
57 return 0;
58 }
59
60 int av_new_packet(AVPacket *pkt, int size)
61 {
62 AVBufferRef *buf = NULL;
63 int ret = packet_alloc(&buf, size);
64 if (ret < 0)
65 return ret;
66
67 av_init_packet(pkt);
68 pkt->buf = buf;
69 pkt->data = buf->data;
70 pkt->size = size;
71
72 return 0;
73 }
74
75 void av_shrink_packet(AVPacket *pkt, int size)
76 {
77 if (pkt->size <= size)
78 return;
79 pkt->size = size;
80 memset(pkt->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
81 }
82
83 int av_grow_packet(AVPacket *pkt, int grow_by)
84 {
85 int new_size;
86 av_assert0((unsigned)pkt->size <= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
87 if (!pkt->size)
88 return av_new_packet(pkt, grow_by);
89 if ((unsigned)grow_by >
90 INT_MAX - (pkt->size + AV_INPUT_BUFFER_PADDING_SIZE))
91 return -1;
92
93 new_size = pkt->size + grow_by + AV_INPUT_BUFFER_PADDING_SIZE;
94 if (pkt->buf) {
95 int ret = av_buffer_realloc(&pkt->buf, new_size);
96 if (ret < 0)
97 return ret;
98 } else {
99 pkt->buf = av_buffer_alloc(new_size);
100 if (!pkt->buf)
101 return AVERROR(ENOMEM);
102 memcpy(pkt->buf->data, pkt->data, FFMIN(pkt->size, pkt->size + grow_by));
103 }
104 pkt->data = pkt->buf->data;
105 pkt->size += grow_by;
106 memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
107
108 return 0;
109 }
110
111 int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
112 {
113 if (size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
114 return AVERROR(EINVAL);
115
116 pkt->buf = av_buffer_create(data, size + AV_INPUT_BUFFER_PADDING_SIZE,
117 av_buffer_default_free, NULL, 0);
118 if (!pkt->buf)
119 return AVERROR(ENOMEM);
120
121 pkt->data = data;
122 pkt->size = size;
123
124 return 0;
125 }
126
127 #define ALLOC_MALLOC(data, size) data = av_malloc(size)
128 #define ALLOC_BUF(data, size) \
129 do { \
130 av_buffer_realloc(&pkt->buf, size); \
131 data = pkt->buf ? pkt->buf->data : NULL; \
132 } while (0)
133
134 #define DUP_DATA(dst, src, size, padding, ALLOC) \
135 do { \
136 void *data; \
137 if (padding) { \
138 if ((unsigned)(size) > \
139 (unsigned)(size) + AV_INPUT_BUFFER_PADDING_SIZE) \
140 goto failed_alloc; \
141 ALLOC(data, size + AV_INPUT_BUFFER_PADDING_SIZE); \
142 } else { \
143 ALLOC(data, size); \
144 } \
145 if (!data) \
146 goto failed_alloc; \
147 memcpy(data, src, size); \
148 if (padding) \
149 memset((uint8_t *)data + size, 0, \
150 AV_INPUT_BUFFER_PADDING_SIZE); \
151 dst = data; \
152 } while (0)
153
154 int av_dup_packet(AVPacket *pkt)
155 {
156 AVPacket tmp_pkt;
157
158 if (!pkt->buf && pkt->data) {
159 tmp_pkt = *pkt;
160
161 pkt->data = NULL;
162 pkt->side_data = NULL;
163 DUP_DATA(pkt->data, tmp_pkt.data, pkt->size, 1, ALLOC_BUF);
164
165 if (pkt->side_data_elems) {
166 int i;
167
168 DUP_DATA(pkt->side_data, tmp_pkt.side_data,
169 pkt->side_data_elems * sizeof(*pkt->side_data), 0, ALLOC_MALLOC);
170 memset(pkt->side_data, 0,
171 pkt->side_data_elems * sizeof(*pkt->side_data));
172 for (i = 0; i < pkt->side_data_elems; i++) {
173 DUP_DATA(pkt->side_data[i].data, tmp_pkt.side_data[i].data,
174 tmp_pkt.side_data[i].size, 1, ALLOC_MALLOC);
175 pkt->side_data[i].size = tmp_pkt.side_data[i].size;
176 pkt->side_data[i].type = tmp_pkt.side_data[i].type;
177 }
178 }
179 }
180 return 0;
181
182 failed_alloc:
183 av_free_packet(pkt);
184 return AVERROR(ENOMEM);
185 }
186
187 void av_packet_free_side_data(AVPacket *pkt)
188 {
189 int i;
190 for (i = 0; i < pkt->side_data_elems; i++)
191 av_free(pkt->side_data[i].data);
192 av_freep(&pkt->side_data);
193 pkt->side_data_elems = 0;
194 }
195
196 void av_free_packet(AVPacket *pkt)
197 {
198 if (pkt) {
199 if (pkt->buf)
200 av_buffer_unref(&pkt->buf);
201 pkt->data = NULL;
202 pkt->size = 0;
203
204 av_packet_free_side_data(pkt);
205 }
206 }
207
208 uint8_t *av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
209 int size)
210 {
211 int elems = pkt->side_data_elems;
212
213 if ((unsigned)elems + 1 > INT_MAX / sizeof(*pkt->side_data))
214 return NULL;
215 if ((unsigned)size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
216 return NULL;
217
218 pkt->side_data = av_realloc(pkt->side_data,
219 (elems + 1) * sizeof(*pkt->side_data));
220 if (!pkt->side_data)
221 return NULL;
222
223 pkt->side_data[elems].data = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
224 if (!pkt->side_data[elems].data)
225 return NULL;
226 pkt->side_data[elems].size = size;
227 pkt->side_data[elems].type = type;
228 pkt->side_data_elems++;
229
230 return pkt->side_data[elems].data;
231 }
232
233 uint8_t *av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
234 int *size)
235 {
236 int i;
237
238 for (i = 0; i < pkt->side_data_elems; i++) {
239 if (pkt->side_data[i].type == type) {
240 if (size)
241 *size = pkt->side_data[i].size;
242 return pkt->side_data[i].data;
243 }
244 }
245 return NULL;
246 }
247
248 int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
249 int size)
250 {
251 int i;
252
253 for (i = 0; i < pkt->side_data_elems; i++) {
254 if (pkt->side_data[i].type == type) {
255 if (size > pkt->side_data[i].size)
256 return AVERROR(ENOMEM);
257 pkt->side_data[i].size = size;
258 return 0;
259 }
260 }
261 return AVERROR(ENOENT);
262 }
263
264 int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
265 {
266 int i;
267
268 dst->pts = src->pts;
269 dst->dts = src->dts;
270 dst->pos = src->pos;
271 dst->duration = src->duration;
272 dst->convergence_duration = src->convergence_duration;
273 dst->flags = src->flags;
274 dst->stream_index = src->stream_index;
275
276 for (i = 0; i < src->side_data_elems; i++) {
277 enum AVPacketSideDataType type = src->side_data[i].type;
278 int size = src->side_data[i].size;
279 uint8_t *src_data = src->side_data[i].data;
280 uint8_t *dst_data = av_packet_new_side_data(dst, type, size);
281
282 if (!dst_data) {
283 av_packet_free_side_data(dst);
284 return AVERROR(ENOMEM);
285 }
286 memcpy(dst_data, src_data, size);
287 }
288
289 return 0;
290 }
291
292 void av_packet_unref(AVPacket *pkt)
293 {
294 av_packet_free_side_data(pkt);
295 av_buffer_unref(&pkt->buf);
296 av_init_packet(pkt);
297 pkt->data = NULL;
298 pkt->size = 0;
299 }
300
301 int av_packet_ref(AVPacket *dst, AVPacket *src)
302 {
303 int ret;
304
305 ret = av_packet_copy_props(dst, src);
306 if (ret < 0)
307 return ret;
308
309 if (!src->buf) {
310 ret = packet_alloc(&dst->buf, src->size);
311 if (ret < 0)
312 goto fail;
313 memcpy(dst->buf->data, src->data, src->size);
314 } else {
315 dst->buf = av_buffer_ref(src->buf);
316 if (!dst->buf) {
317 ret = AVERROR(ENOMEM);
318 goto fail;
319 }
320 }
321
322 dst->size = src->size;
323 dst->data = dst->buf->data;
324 return 0;
325 fail:
326 av_packet_free_side_data(dst);
327 return ret;
328 }
329
330 void av_packet_move_ref(AVPacket *dst, AVPacket *src)
331 {
332 *dst = *src;
333 av_init_packet(src);
334 }
335
336 void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
337 {
338 if (pkt->pts != AV_NOPTS_VALUE)
339 pkt->pts = av_rescale_q(pkt->pts, src_tb, dst_tb);
340 if (pkt->dts != AV_NOPTS_VALUE)
341 pkt->dts = av_rescale_q(pkt->dts, src_tb, dst_tb);
342 if (pkt->duration > 0)
343 pkt->duration = av_rescale_q(pkt->duration, src_tb, dst_tb);
344 if (pkt->convergence_duration > 0)
345 pkt->convergence_duration = av_rescale_q(pkt->convergence_duration, src_tb, dst_tb);
346 }