eaea061c5434ad5d423e5d6be61290042a9482a0
[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 #if FF_API_CONVERGENCE_DURATION
38 FF_DISABLE_DEPRECATION_WARNINGS
39 pkt->convergence_duration = 0;
40 FF_ENABLE_DEPRECATION_WARNINGS
41 #endif
42 pkt->flags = 0;
43 pkt->stream_index = 0;
44 pkt->buf = NULL;
45 pkt->side_data = NULL;
46 pkt->side_data_elems = 0;
47 }
48
49 static int packet_alloc(AVBufferRef **buf, int size)
50 {
51 int ret;
52 if ((unsigned)size >= (unsigned)size + AV_INPUT_BUFFER_PADDING_SIZE)
53 return AVERROR(EINVAL);
54
55 ret = av_buffer_realloc(buf, size + AV_INPUT_BUFFER_PADDING_SIZE);
56 if (ret < 0)
57 return ret;
58
59 memset((*buf)->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
60
61 return 0;
62 }
63
64 int av_new_packet(AVPacket *pkt, int size)
65 {
66 AVBufferRef *buf = NULL;
67 int ret = packet_alloc(&buf, size);
68 if (ret < 0)
69 return ret;
70
71 av_init_packet(pkt);
72 pkt->buf = buf;
73 pkt->data = buf->data;
74 pkt->size = size;
75
76 return 0;
77 }
78
79 void av_shrink_packet(AVPacket *pkt, int size)
80 {
81 if (pkt->size <= size)
82 return;
83 pkt->size = size;
84 memset(pkt->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
85 }
86
87 int av_grow_packet(AVPacket *pkt, int grow_by)
88 {
89 int new_size;
90 av_assert0((unsigned)pkt->size <= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE);
91 if (!pkt->size)
92 return av_new_packet(pkt, grow_by);
93 if ((unsigned)grow_by >
94 INT_MAX - (pkt->size + AV_INPUT_BUFFER_PADDING_SIZE))
95 return -1;
96
97 new_size = pkt->size + grow_by + AV_INPUT_BUFFER_PADDING_SIZE;
98 if (pkt->buf) {
99 int ret = av_buffer_realloc(&pkt->buf, new_size);
100 if (ret < 0)
101 return ret;
102 } else {
103 pkt->buf = av_buffer_alloc(new_size);
104 if (!pkt->buf)
105 return AVERROR(ENOMEM);
106 memcpy(pkt->buf->data, pkt->data, FFMIN(pkt->size, pkt->size + grow_by));
107 }
108 pkt->data = pkt->buf->data;
109 pkt->size += grow_by;
110 memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
111
112 return 0;
113 }
114
115 int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
116 {
117 if (size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
118 return AVERROR(EINVAL);
119
120 pkt->buf = av_buffer_create(data, size + AV_INPUT_BUFFER_PADDING_SIZE,
121 av_buffer_default_free, NULL, 0);
122 if (!pkt->buf)
123 return AVERROR(ENOMEM);
124
125 pkt->data = data;
126 pkt->size = size;
127
128 return 0;
129 }
130
131 #define ALLOC_MALLOC(data, size) data = av_malloc(size)
132 #define ALLOC_BUF(data, size) \
133 do { \
134 av_buffer_realloc(&pkt->buf, size); \
135 data = pkt->buf ? pkt->buf->data : NULL; \
136 } while (0)
137
138 #define DUP_DATA(dst, src, size, padding, ALLOC) \
139 do { \
140 void *data; \
141 if (padding) { \
142 if ((unsigned)(size) > \
143 (unsigned)(size) + AV_INPUT_BUFFER_PADDING_SIZE) \
144 goto failed_alloc; \
145 ALLOC(data, size + AV_INPUT_BUFFER_PADDING_SIZE); \
146 } else { \
147 ALLOC(data, size); \
148 } \
149 if (!data) \
150 goto failed_alloc; \
151 memcpy(data, src, size); \
152 if (padding) \
153 memset((uint8_t *)data + size, 0, \
154 AV_INPUT_BUFFER_PADDING_SIZE); \
155 dst = data; \
156 } while (0)
157
158 int av_dup_packet(AVPacket *pkt)
159 {
160 AVPacket tmp_pkt;
161
162 if (!pkt->buf && pkt->data) {
163 tmp_pkt = *pkt;
164
165 pkt->data = NULL;
166 pkt->side_data = NULL;
167 DUP_DATA(pkt->data, tmp_pkt.data, pkt->size, 1, ALLOC_BUF);
168
169 if (pkt->side_data_elems) {
170 int i;
171
172 DUP_DATA(pkt->side_data, tmp_pkt.side_data,
173 pkt->side_data_elems * sizeof(*pkt->side_data), 0, ALLOC_MALLOC);
174 memset(pkt->side_data, 0,
175 pkt->side_data_elems * sizeof(*pkt->side_data));
176 for (i = 0; i < pkt->side_data_elems; i++) {
177 DUP_DATA(pkt->side_data[i].data, tmp_pkt.side_data[i].data,
178 tmp_pkt.side_data[i].size, 1, ALLOC_MALLOC);
179 pkt->side_data[i].size = tmp_pkt.side_data[i].size;
180 pkt->side_data[i].type = tmp_pkt.side_data[i].type;
181 }
182 }
183 }
184 return 0;
185
186 failed_alloc:
187 av_packet_unref(pkt);
188 return AVERROR(ENOMEM);
189 }
190
191 void av_packet_free_side_data(AVPacket *pkt)
192 {
193 int i;
194 for (i = 0; i < pkt->side_data_elems; i++)
195 av_free(pkt->side_data[i].data);
196 av_freep(&pkt->side_data);
197 pkt->side_data_elems = 0;
198 }
199
200 #if FF_API_AVPACKET_OLD_API
201 FF_DISABLE_DEPRECATION_WARNINGS
202 void av_free_packet(AVPacket *pkt)
203 {
204 if (pkt) {
205 if (pkt->buf)
206 av_buffer_unref(&pkt->buf);
207 pkt->data = NULL;
208 pkt->size = 0;
209
210 av_packet_free_side_data(pkt);
211 }
212 }
213 FF_ENABLE_DEPRECATION_WARNINGS
214 #endif
215
216 uint8_t *av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
217 int size)
218 {
219 int elems = pkt->side_data_elems;
220
221 if ((unsigned)elems + 1 > INT_MAX / sizeof(*pkt->side_data))
222 return NULL;
223 if ((unsigned)size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
224 return NULL;
225
226 pkt->side_data = av_realloc(pkt->side_data,
227 (elems + 1) * sizeof(*pkt->side_data));
228 if (!pkt->side_data)
229 return NULL;
230
231 pkt->side_data[elems].data = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
232 if (!pkt->side_data[elems].data)
233 return NULL;
234 pkt->side_data[elems].size = size;
235 pkt->side_data[elems].type = type;
236 pkt->side_data_elems++;
237
238 return pkt->side_data[elems].data;
239 }
240
241 uint8_t *av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
242 int *size)
243 {
244 int i;
245
246 for (i = 0; i < pkt->side_data_elems; i++) {
247 if (pkt->side_data[i].type == type) {
248 if (size)
249 *size = pkt->side_data[i].size;
250 return pkt->side_data[i].data;
251 }
252 }
253 return NULL;
254 }
255
256 int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
257 int size)
258 {
259 int i;
260
261 for (i = 0; i < pkt->side_data_elems; i++) {
262 if (pkt->side_data[i].type == type) {
263 if (size > pkt->side_data[i].size)
264 return AVERROR(ENOMEM);
265 pkt->side_data[i].size = size;
266 return 0;
267 }
268 }
269 return AVERROR(ENOENT);
270 }
271
272 int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
273 {
274 int i;
275
276 dst->pts = src->pts;
277 dst->dts = src->dts;
278 dst->pos = src->pos;
279 dst->duration = src->duration;
280 #if FF_API_CONVERGENCE_DURATION
281 FF_DISABLE_DEPRECATION_WARNINGS
282 dst->convergence_duration = src->convergence_duration;
283 FF_ENABLE_DEPRECATION_WARNINGS
284 #endif
285 dst->flags = src->flags;
286 dst->stream_index = src->stream_index;
287
288 for (i = 0; i < src->side_data_elems; i++) {
289 enum AVPacketSideDataType type = src->side_data[i].type;
290 int size = src->side_data[i].size;
291 uint8_t *src_data = src->side_data[i].data;
292 uint8_t *dst_data = av_packet_new_side_data(dst, type, size);
293
294 if (!dst_data) {
295 av_packet_free_side_data(dst);
296 return AVERROR(ENOMEM);
297 }
298 memcpy(dst_data, src_data, size);
299 }
300
301 return 0;
302 }
303
304 void av_packet_unref(AVPacket *pkt)
305 {
306 av_packet_free_side_data(pkt);
307 av_buffer_unref(&pkt->buf);
308 av_init_packet(pkt);
309 pkt->data = NULL;
310 pkt->size = 0;
311 }
312
313 int av_packet_ref(AVPacket *dst, AVPacket *src)
314 {
315 int ret;
316
317 ret = av_packet_copy_props(dst, src);
318 if (ret < 0)
319 return ret;
320
321 if (!src->buf) {
322 ret = packet_alloc(&dst->buf, src->size);
323 if (ret < 0)
324 goto fail;
325 memcpy(dst->buf->data, src->data, src->size);
326 } else {
327 dst->buf = av_buffer_ref(src->buf);
328 if (!dst->buf) {
329 ret = AVERROR(ENOMEM);
330 goto fail;
331 }
332 }
333
334 dst->size = src->size;
335 dst->data = dst->buf->data;
336 return 0;
337 fail:
338 av_packet_free_side_data(dst);
339 return ret;
340 }
341
342 void av_packet_move_ref(AVPacket *dst, AVPacket *src)
343 {
344 *dst = *src;
345 av_init_packet(src);
346 }
347
348 void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
349 {
350 if (pkt->pts != AV_NOPTS_VALUE)
351 pkt->pts = av_rescale_q(pkt->pts, src_tb, dst_tb);
352 if (pkt->dts != AV_NOPTS_VALUE)
353 pkt->dts = av_rescale_q(pkt->dts, src_tb, dst_tb);
354 if (pkt->duration > 0)
355 pkt->duration = av_rescale_q(pkt->duration, src_tb, dst_tb);
356 #if FF_API_CONVERGENCE_DURATION
357 FF_DISABLE_DEPRECATION_WARNINGS
358 if (pkt->convergence_duration > 0)
359 pkt->convergence_duration = av_rescale_q(pkt->convergence_duration, src_tb, dst_tb);
360 FF_ENABLE_DEPRECATION_WARNINGS
361 #endif
362 }