]> git.sesse.net Git - ffmpeg/blob - libavcodec/avpacket.c
Use av_frame_copy() to simplify code where appropriate.
[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     int ret;
68     if ((unsigned)size >= (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE)
69         return AVERROR(EINVAL);
70
71     ret = av_buffer_realloc(buf, size + FF_INPUT_BUFFER_PADDING_SIZE);
72     if (ret < 0)
73         return ret;
74
75     memset((*buf)->data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
76
77     return 0;
78 }
79
80 int av_new_packet(AVPacket *pkt, int size)
81 {
82     AVBufferRef *buf = NULL;
83     int ret = packet_alloc(&buf, size);
84     if (ret < 0)
85         return ret;
86
87     av_init_packet(pkt);
88     pkt->buf      = buf;
89     pkt->data     = buf->data;
90     pkt->size     = size;
91 #if FF_API_DESTRUCT_PACKET
92 FF_DISABLE_DEPRECATION_WARNINGS
93     pkt->destruct = dummy_destruct_packet;
94 FF_ENABLE_DEPRECATION_WARNINGS
95 #endif
96
97     return 0;
98 }
99
100 void av_shrink_packet(AVPacket *pkt, int size)
101 {
102     if (pkt->size <= size)
103         return;
104     pkt->size = size;
105     memset(pkt->data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
106 }
107
108 int av_grow_packet(AVPacket *pkt, int grow_by)
109 {
110     int new_size;
111     av_assert0((unsigned)pkt->size <= INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
112     if (!pkt->size)
113         return av_new_packet(pkt, grow_by);
114     if ((unsigned)grow_by >
115         INT_MAX - (pkt->size + FF_INPUT_BUFFER_PADDING_SIZE))
116         return -1;
117
118     new_size = pkt->size + grow_by + FF_INPUT_BUFFER_PADDING_SIZE;
119     if (pkt->buf) {
120         int ret = av_buffer_realloc(&pkt->buf, new_size);
121         if (ret < 0)
122             return ret;
123     } else {
124         pkt->buf = av_buffer_alloc(new_size);
125         if (!pkt->buf)
126             return AVERROR(ENOMEM);
127         memcpy(pkt->buf->data, pkt->data, FFMIN(pkt->size, pkt->size + grow_by));
128 #if FF_API_DESTRUCT_PACKET
129 FF_DISABLE_DEPRECATION_WARNINGS
130         pkt->destruct = dummy_destruct_packet;
131 FF_ENABLE_DEPRECATION_WARNINGS
132 #endif
133     }
134     pkt->data  = pkt->buf->data;
135     pkt->size += grow_by;
136     memset(pkt->data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
137
138     return 0;
139 }
140
141 int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
142 {
143     if (size >= INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
144         return AVERROR(EINVAL);
145
146     pkt->buf = av_buffer_create(data, size + FF_INPUT_BUFFER_PADDING_SIZE,
147                                 av_buffer_default_free, NULL, 0);
148     if (!pkt->buf)
149         return AVERROR(ENOMEM);
150
151     pkt->data = data;
152     pkt->size = size;
153 #if FF_API_DESTRUCT_PACKET
154 FF_DISABLE_DEPRECATION_WARNINGS
155     pkt->destruct = dummy_destruct_packet;
156 FF_ENABLE_DEPRECATION_WARNINGS
157 #endif
158
159     return 0;
160 }
161
162 #define ALLOC_MALLOC(data, size) data = av_malloc(size)
163 #define ALLOC_BUF(data, size)                \
164 do {                                         \
165     av_buffer_realloc(&pkt->buf, size);      \
166     data = pkt->buf ? pkt->buf->data : NULL; \
167 } while (0)
168
169 #define DUP_DATA(dst, src, size, padding, ALLOC)                        \
170     do {                                                                \
171         void *data;                                                     \
172         if (padding) {                                                  \
173             if ((unsigned)(size) >                                      \
174                 (unsigned)(size) + FF_INPUT_BUFFER_PADDING_SIZE)        \
175                 goto failed_alloc;                                      \
176             ALLOC(data, size + FF_INPUT_BUFFER_PADDING_SIZE);           \
177         } else {                                                        \
178             ALLOC(data, size);                                          \
179         }                                                               \
180         if (!data)                                                      \
181             goto failed_alloc;                                          \
182         memcpy(data, src, size);                                        \
183         if (padding)                                                    \
184             memset((uint8_t *)data + size, 0,                           \
185                    FF_INPUT_BUFFER_PADDING_SIZE);                       \
186         dst = data;                                                     \
187     } while (0)
188
189 int av_dup_packet(AVPacket *pkt)
190 {
191     AVPacket tmp_pkt;
192
193 FF_DISABLE_DEPRECATION_WARNINGS
194     if (!pkt->buf && pkt->data
195 #if FF_API_DESTRUCT_PACKET
196         && !pkt->destruct
197 #endif
198         ) {
199 FF_ENABLE_DEPRECATION_WARNINGS
200         tmp_pkt = *pkt;
201
202         pkt->data      = NULL;
203         pkt->side_data = NULL;
204         DUP_DATA(pkt->data, tmp_pkt.data, pkt->size, 1, ALLOC_BUF);
205 #if FF_API_DESTRUCT_PACKET
206 FF_DISABLE_DEPRECATION_WARNINGS
207         pkt->destruct = dummy_destruct_packet;
208 FF_ENABLE_DEPRECATION_WARNINGS
209 #endif
210
211         if (pkt->side_data_elems) {
212             int i;
213
214             DUP_DATA(pkt->side_data, tmp_pkt.side_data,
215                      pkt->side_data_elems * sizeof(*pkt->side_data), 0, ALLOC_MALLOC);
216             memset(pkt->side_data, 0,
217                    pkt->side_data_elems * sizeof(*pkt->side_data));
218             for (i = 0; i < pkt->side_data_elems; i++) {
219                 DUP_DATA(pkt->side_data[i].data, tmp_pkt.side_data[i].data,
220                          tmp_pkt.side_data[i].size, 1, ALLOC_MALLOC);
221                 pkt->side_data[i].size = tmp_pkt.side_data[i].size;
222                 pkt->side_data[i].type = tmp_pkt.side_data[i].type;
223             }
224         }
225     }
226     return 0;
227
228 failed_alloc:
229     av_free_packet(pkt);
230     return AVERROR(ENOMEM);
231 }
232
233 void av_packet_free_side_data(AVPacket *pkt)
234 {
235     int i;
236     for (i = 0; i < pkt->side_data_elems; i++)
237         av_free(pkt->side_data[i].data);
238     av_freep(&pkt->side_data);
239     pkt->side_data_elems = 0;
240 }
241
242 void av_free_packet(AVPacket *pkt)
243 {
244     if (pkt) {
245 FF_DISABLE_DEPRECATION_WARNINGS
246         if (pkt->buf)
247             av_buffer_unref(&pkt->buf);
248 #if FF_API_DESTRUCT_PACKET
249         else if (pkt->destruct)
250             pkt->destruct(pkt);
251         pkt->destruct = NULL;
252 #endif
253 FF_ENABLE_DEPRECATION_WARNINGS
254         pkt->data            = NULL;
255         pkt->size            = 0;
256
257         av_packet_free_side_data(pkt);
258     }
259 }
260
261 uint8_t *av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
262                                  int size)
263 {
264     int elems = pkt->side_data_elems;
265
266     if ((unsigned)elems + 1 > INT_MAX / sizeof(*pkt->side_data))
267         return NULL;
268     if ((unsigned)size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
269         return NULL;
270
271     pkt->side_data = av_realloc(pkt->side_data,
272                                 (elems + 1) * sizeof(*pkt->side_data));
273     if (!pkt->side_data)
274         return NULL;
275
276     pkt->side_data[elems].data = av_malloc(size + FF_INPUT_BUFFER_PADDING_SIZE);
277     if (!pkt->side_data[elems].data)
278         return NULL;
279     pkt->side_data[elems].size = size;
280     pkt->side_data[elems].type = type;
281     pkt->side_data_elems++;
282
283     return pkt->side_data[elems].data;
284 }
285
286 uint8_t *av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
287                                  int *size)
288 {
289     int i;
290
291     for (i = 0; i < pkt->side_data_elems; i++) {
292         if (pkt->side_data[i].type == type) {
293             if (size)
294                 *size = pkt->side_data[i].size;
295             return pkt->side_data[i].data;
296         }
297     }
298     return NULL;
299 }
300
301 int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
302                                int size)
303 {
304     int i;
305
306     for (i = 0; i < pkt->side_data_elems; i++) {
307         if (pkt->side_data[i].type == type) {
308             if (size > pkt->side_data[i].size)
309                 return AVERROR(ENOMEM);
310             pkt->side_data[i].size = size;
311             return 0;
312         }
313     }
314     return AVERROR(ENOENT);
315 }
316
317 int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
318 {
319     int i;
320
321     dst->pts                  = src->pts;
322     dst->dts                  = src->dts;
323     dst->pos                  = src->pos;
324     dst->duration             = src->duration;
325     dst->convergence_duration = src->convergence_duration;
326     dst->flags                = src->flags;
327     dst->stream_index         = src->stream_index;
328     dst->side_data_elems      = src->side_data_elems;
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 }