]> git.sesse.net Git - ffmpeg/blob - libavcodec/avpacket.c
lavc: Make AVPacket.duration int64, and deprecate convergence_duration
[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
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_free_packet(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 void av_free_packet(AVPacket *pkt)
201 {
202     if (pkt) {
203         if (pkt->buf)
204             av_buffer_unref(&pkt->buf);
205         pkt->data            = NULL;
206         pkt->size            = 0;
207
208         av_packet_free_side_data(pkt);
209     }
210 }
211
212 uint8_t *av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
213                                  int size)
214 {
215     int elems = pkt->side_data_elems;
216
217     if ((unsigned)elems + 1 > INT_MAX / sizeof(*pkt->side_data))
218         return NULL;
219     if ((unsigned)size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
220         return NULL;
221
222     pkt->side_data = av_realloc(pkt->side_data,
223                                 (elems + 1) * sizeof(*pkt->side_data));
224     if (!pkt->side_data)
225         return NULL;
226
227     pkt->side_data[elems].data = av_malloc(size + AV_INPUT_BUFFER_PADDING_SIZE);
228     if (!pkt->side_data[elems].data)
229         return NULL;
230     pkt->side_data[elems].size = size;
231     pkt->side_data[elems].type = type;
232     pkt->side_data_elems++;
233
234     return pkt->side_data[elems].data;
235 }
236
237 uint8_t *av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
238                                  int *size)
239 {
240     int i;
241
242     for (i = 0; i < pkt->side_data_elems; i++) {
243         if (pkt->side_data[i].type == type) {
244             if (size)
245                 *size = pkt->side_data[i].size;
246             return pkt->side_data[i].data;
247         }
248     }
249     return NULL;
250 }
251
252 int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
253                                int size)
254 {
255     int i;
256
257     for (i = 0; i < pkt->side_data_elems; i++) {
258         if (pkt->side_data[i].type == type) {
259             if (size > pkt->side_data[i].size)
260                 return AVERROR(ENOMEM);
261             pkt->side_data[i].size = size;
262             return 0;
263         }
264     }
265     return AVERROR(ENOENT);
266 }
267
268 int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
269 {
270     int i;
271
272     dst->pts                  = src->pts;
273     dst->dts                  = src->dts;
274     dst->pos                  = src->pos;
275     dst->duration             = src->duration;
276 #if FF_API_CONVERGENCE_DURATION
277 FF_DISABLE_DEPRECATION_WARNINGS
278     dst->convergence_duration = src->convergence_duration;
279 FF_ENABLE_DEPRECATION_WARNINGS
280 #endif
281     dst->flags                = src->flags;
282     dst->stream_index         = src->stream_index;
283
284     for (i = 0; i < src->side_data_elems; i++) {
285          enum AVPacketSideDataType type = src->side_data[i].type;
286          int size          = src->side_data[i].size;
287          uint8_t *src_data = src->side_data[i].data;
288          uint8_t *dst_data = av_packet_new_side_data(dst, type, size);
289
290         if (!dst_data) {
291             av_packet_free_side_data(dst);
292             return AVERROR(ENOMEM);
293         }
294         memcpy(dst_data, src_data, size);
295     }
296
297     return 0;
298 }
299
300 void av_packet_unref(AVPacket *pkt)
301 {
302     av_packet_free_side_data(pkt);
303     av_buffer_unref(&pkt->buf);
304     av_init_packet(pkt);
305     pkt->data = NULL;
306     pkt->size = 0;
307 }
308
309 int av_packet_ref(AVPacket *dst, AVPacket *src)
310 {
311     int ret;
312
313     ret = av_packet_copy_props(dst, src);
314     if (ret < 0)
315         return ret;
316
317     if (!src->buf) {
318         ret = packet_alloc(&dst->buf, src->size);
319         if (ret < 0)
320             goto fail;
321         memcpy(dst->buf->data, src->data, src->size);
322     } else {
323         dst->buf = av_buffer_ref(src->buf);
324         if (!dst->buf) {
325             ret = AVERROR(ENOMEM);
326             goto fail;
327         }
328     }
329
330     dst->size = src->size;
331     dst->data = dst->buf->data;
332     return 0;
333 fail:
334     av_packet_free_side_data(dst);
335     return ret;
336 }
337
338 void av_packet_move_ref(AVPacket *dst, AVPacket *src)
339 {
340     *dst = *src;
341     av_init_packet(src);
342 }
343
344 void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
345 {
346     if (pkt->pts != AV_NOPTS_VALUE)
347         pkt->pts = av_rescale_q(pkt->pts, src_tb, dst_tb);
348     if (pkt->dts != AV_NOPTS_VALUE)
349         pkt->dts = av_rescale_q(pkt->dts, src_tb, dst_tb);
350     if (pkt->duration > 0)
351         pkt->duration = av_rescale_q(pkt->duration, src_tb, dst_tb);
352 #if FF_API_CONVERGENCE_DURATION
353 FF_DISABLE_DEPRECATION_WARNINGS
354     if (pkt->convergence_duration > 0)
355         pkt->convergence_duration = av_rescale_q(pkt->convergence_duration, src_tb, dst_tb);
356 FF_ENABLE_DEPRECATION_WARNINGS
357 #endif
358 }