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