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