Libav
avpacket.c
Go to the documentation of this file.
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 
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
39  pkt->convergence_duration = 0;
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 
50 {
51  AVPacket *pkt = av_mallocz(sizeof(AVPacket));
52  if (!pkt)
53  return pkt;
54 
55  av_packet_unref(pkt);
56 
57  return pkt;
58 }
59 
61 {
62  if (!pkt || !*pkt)
63  return;
64 
65  av_packet_unref(*pkt);
66  av_freep(pkt);
67 }
68 
69 static int packet_alloc(AVBufferRef **buf, int size)
70 {
71  int ret;
72  if (size < 0 || size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
73  return AVERROR(EINVAL);
74 
76  if (ret < 0)
77  return ret;
78 
79  memset((*buf)->data + size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
80 
81  return 0;
82 }
83 
84 int av_new_packet(AVPacket *pkt, int size)
85 {
86  AVBufferRef *buf = NULL;
87  int ret = packet_alloc(&buf, size);
88  if (ret < 0)
89  return ret;
90 
91  av_init_packet(pkt);
92  pkt->buf = buf;
93  pkt->data = buf->data;
94  pkt->size = size;
95 
96  return 0;
97 }
98 
100 {
101  if (pkt->size <= size)
102  return;
103  pkt->size = size;
104  memset(pkt->data + size, 0, AV_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 - AV_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 + AV_INPUT_BUFFER_PADDING_SIZE))
115  return -1;
116 
117  new_size = pkt->size + grow_by + AV_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  }
128  pkt->data = pkt->buf->data;
129  pkt->size += grow_by;
130  memset(pkt->data + pkt->size, 0, AV_INPUT_BUFFER_PADDING_SIZE);
131 
132  return 0;
133 }
134 
136 {
137  if (size >= INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
138  return AVERROR(EINVAL);
139 
142  if (!pkt->buf)
143  return AVERROR(ENOMEM);
144 
145  pkt->data = data;
146  pkt->size = size;
147 
148  return 0;
149 }
150 
151 #if FF_API_AVPACKET_OLD_API
153 #define ALLOC_MALLOC(data, size) data = av_malloc(size)
154 #define ALLOC_BUF(data, size) \
155 do { \
156  av_buffer_realloc(&pkt->buf, size); \
157  data = pkt->buf ? pkt->buf->data : NULL; \
158 } while (0)
159 
160 #define DUP_DATA(dst, src, size, padding, ALLOC) \
161  do { \
162  void *data; \
163  if (padding) { \
164  if ((unsigned)(size) > \
165  (unsigned)(size) + AV_INPUT_BUFFER_PADDING_SIZE) \
166  goto failed_alloc; \
167  ALLOC(data, size + AV_INPUT_BUFFER_PADDING_SIZE); \
168  } else { \
169  ALLOC(data, size); \
170  } \
171  if (!data) \
172  goto failed_alloc; \
173  memcpy(data, src, size); \
174  if (padding) \
175  memset((uint8_t *)data + size, 0, \
176  AV_INPUT_BUFFER_PADDING_SIZE); \
177  dst = data; \
178  } while (0)
179 
181 {
182  AVPacket tmp_pkt;
183 
184  if (!pkt->buf && pkt->data) {
185  tmp_pkt = *pkt;
186 
187  pkt->data = NULL;
188  pkt->side_data = NULL;
189  DUP_DATA(pkt->data, tmp_pkt.data, pkt->size, 1, ALLOC_BUF);
190 
191  if (pkt->side_data_elems) {
192  int i;
193 
194  DUP_DATA(pkt->side_data, tmp_pkt.side_data,
195  pkt->side_data_elems * sizeof(*pkt->side_data), 0, ALLOC_MALLOC);
196  memset(pkt->side_data, 0,
197  pkt->side_data_elems * sizeof(*pkt->side_data));
198  for (i = 0; i < pkt->side_data_elems; i++) {
199  DUP_DATA(pkt->side_data[i].data, tmp_pkt.side_data[i].data,
200  tmp_pkt.side_data[i].size, 1, ALLOC_MALLOC);
201  pkt->side_data[i].size = tmp_pkt.side_data[i].size;
202  pkt->side_data[i].type = tmp_pkt.side_data[i].type;
203  }
204  }
205  }
206  return 0;
207 
208 failed_alloc:
209  av_packet_unref(pkt);
210  return AVERROR(ENOMEM);
211 }
213 #endif
214 
216 {
217  int i;
218  for (i = 0; i < pkt->side_data_elems; i++)
219  av_free(pkt->side_data[i].data);
220  av_freep(&pkt->side_data);
221  pkt->side_data_elems = 0;
222 }
223 
224 #if FF_API_AVPACKET_OLD_API
227 {
228  if (pkt) {
229  if (pkt->buf)
230  av_buffer_unref(&pkt->buf);
231  pkt->data = NULL;
232  pkt->size = 0;
233 
235  }
236 }
238 #endif
239 
241  uint8_t *data, size_t size)
242 {
244  int elems = pkt->side_data_elems;
245 
246  if ((unsigned)elems + 1 > INT_MAX / sizeof(*pkt->side_data))
247  return AVERROR(ERANGE);
248 
249  tmp = av_realloc(pkt->side_data, (elems + 1) * sizeof(*tmp));
250  if (!tmp)
251  return AVERROR(ENOMEM);
252 
253  pkt->side_data = tmp;
254  pkt->side_data[elems].data = data;
255  pkt->side_data[elems].size = size;
256  pkt->side_data[elems].type = type;
257  pkt->side_data_elems++;
258 
259  return 0;
260 }
261 
262 
264  int size)
265 {
266  int ret;
267  uint8_t *data;
268 
269  if (!size || (unsigned)size > INT_MAX - AV_INPUT_BUFFER_PADDING_SIZE)
270  return NULL;
272  if (!data)
273  return NULL;
274 
275  ret = av_packet_add_side_data(pkt, type, data, size);
276  if (ret < 0) {
277  av_freep(&data);
278  return NULL;
279  }
280 
281  return data;
282 }
283 
285  int *size)
286 {
287  int i;
288 
289  for (i = 0; i < pkt->side_data_elems; i++) {
290  if (pkt->side_data[i].type == type) {
291  if (size)
292  *size = pkt->side_data[i].size;
293  return pkt->side_data[i].data;
294  }
295  }
296  return NULL;
297 }
298 
300  int size)
301 {
302  int i;
303 
304  for (i = 0; i < pkt->side_data_elems; i++) {
305  if (pkt->side_data[i].type == type) {
306  if (size > pkt->side_data[i].size)
307  return AVERROR(ENOMEM);
308  pkt->side_data[i].size = size;
309  return 0;
310  }
311  }
312  return AVERROR(ENOENT);
313 }
314 
316 {
317  int i;
318 
319  dst->pts = src->pts;
320  dst->dts = src->dts;
321  dst->pos = src->pos;
322  dst->duration = src->duration;
323 #if FF_API_CONVERGENCE_DURATION
327 #endif
328  dst->flags = src->flags;
329  dst->stream_index = src->stream_index;
330 
331  for (i = 0; i < src->side_data_elems; i++) {
332  enum AVPacketSideDataType type = src->side_data[i].type;
333  int size = src->side_data[i].size;
334  uint8_t *src_data = src->side_data[i].data;
335  uint8_t *dst_data = av_packet_new_side_data(dst, type, size);
336 
337  if (!dst_data) {
339  return AVERROR(ENOMEM);
340  }
341  memcpy(dst_data, src_data, size);
342  }
343 
344  return 0;
345 }
346 
348 {
350  av_buffer_unref(&pkt->buf);
351  av_init_packet(pkt);
352  pkt->data = NULL;
353  pkt->size = 0;
354 }
355 
357 {
358  int ret;
359 
360  ret = av_packet_copy_props(dst, src);
361  if (ret < 0)
362  return ret;
363 
364  if (!src->buf) {
365  ret = packet_alloc(&dst->buf, src->size);
366  if (ret < 0)
367  goto fail;
368  memcpy(dst->buf->data, src->data, src->size);
369 
370  dst->data = dst->buf->data;
371  } else {
372  dst->buf = av_buffer_ref(src->buf);
373  if (!dst->buf) {
374  ret = AVERROR(ENOMEM);
375  goto fail;
376  }
377  dst->data = src->data;
378  }
379 
380  dst->size = src->size;
381 
382  return 0;
383 fail:
385  return ret;
386 }
387 
389 {
390  AVPacket *ret = av_packet_alloc();
391 
392  if (!ret)
393  return ret;
394 
395  if (av_packet_ref(ret, src))
396  av_packet_free(&ret);
397 
398  return ret;
399 }
400 
402 {
403  *dst = *src;
404  av_init_packet(src);
405  src->data = NULL;
406  src->size = 0;
407 }
408 
410 {
411  if (pkt->pts != AV_NOPTS_VALUE)
412  pkt->pts = av_rescale_q(pkt->pts, src_tb, dst_tb);
413  if (pkt->dts != AV_NOPTS_VALUE)
414  pkt->dts = av_rescale_q(pkt->dts, src_tb, dst_tb);
415  if (pkt->duration > 0)
416  pkt->duration = av_rescale_q(pkt->duration, src_tb, dst_tb);
417 #if FF_API_CONVERGENCE_DURATION
419  if (pkt->convergence_duration > 0)
420  pkt->convergence_duration = av_rescale_q(pkt->convergence_duration, src_tb, dst_tb);
422 #endif
423 }
void * av_malloc(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:62
#define DUP_DATA(dst, src, size, padding, ALLOC)
Definition: avpacket.c:160
AVPacketSideDataType
Definition: avcodec.h:1191
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it...
Definition: buffer.c:106
int size
memory handling functions
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1366
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:99
#define ALLOC_MALLOC(data, size)
Definition: avpacket.c:153
int size
Definition: avcodec.h:1347
#define ALLOC_BUF(data, size)
Definition: avpacket.c:154
void av_freep(void *arg)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc() and set the pointer ...
Definition: mem.c:202
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
Definition: avpacket.c:60
uint8_t
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:1364
int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
Initialize a reference-counted packet from av_malloc()ed data.
Definition: avpacket.c:135
const char data[16]
Definition: mxf.c:70
uint8_t * data
Definition: avcodec.h:1346
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition: avpacket.c:401
uint8_t * data
Definition: avcodec.h:1290
void av_buffer_default_free(void *opaque, uint8_t *data)
Default free callback, which calls av_free() on the buffer data.
Definition: buffer.c:61
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:99
#define src
Definition: vp8dsp.c:254
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:84
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:190
void av_packet_rescale_ts(AVPacket *pkt, AVRational src_tb, AVRational dst_tb)
Convert valid timing fields (timestamps / durations) in a packet from one timebase to another...
Definition: avpacket.c:409
#define AVERROR(e)
Definition: error.h:43
FF_ENABLE_DEPRECATION_WARNINGS void av_packet_free_side_data(AVPacket *pkt)
Convenience function to free all the side data stored.
Definition: avpacket.c:215
int av_dup_packet(AVPacket *pkt)
Definition: avpacket.c:180
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:1329
simple assert() macros that are a bit more flexible than ISO C assert().
enum AVPacketSideDataType type
Definition: avcodec.h:1292
int side_data_elems
Definition: avcodec.h:1358
AVBufferRef * av_buffer_create(uint8_t *data, int size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
Definition: buffer.c:28
int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Shrink the already allocated side data buffer.
Definition: avpacket.c:299
int av_buffer_realloc(AVBufferRef **pbuf, int size)
Reallocate a given buffer.
Definition: buffer.c:147
#define fail()
Definition: checkasm.h:80
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:1352
common internal API header
int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
Copy only "properties" fields from src to dst.
Definition: avpacket.c:315
#define FFMIN(a, b)
Definition: common.h:66
static int packet_alloc(AVBufferRef **buf, int size)
Definition: avpacket.c:69
AVPacket * av_packet_clone(AVPacket *src)
Create a new packet that references the same data as src.
Definition: avpacket.c:388
NULL
Definition: eval.c:55
Libavcodec external API header.
AVBufferRef * av_buffer_alloc(int size)
Allocate an AVBuffer of the given size using av_malloc().
Definition: buffer.c:66
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:347
uint8_t * data
The data buffer.
Definition: buffer.h:89
rational number numerator/denominator
Definition: rational.h:43
FF_ENABLE_DEPRECATION_WARNINGS int av_packet_add_side_data(AVPacket *pkt, enum AVPacketSideDataType type, uint8_t *data, size_t size)
Wrap an existing array as a packet side data.
Definition: avpacket.c:240
A reference to a data buffer.
Definition: buffer.h:81
AVPacketSideData * side_data
Additional packet data that can be provided by the container.
Definition: avcodec.h:1357
attribute_deprecated int64_t convergence_duration
Definition: avcodec.h:1375
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:77
common internal and external API header
void * av_realloc(void *ptr, size_t size)
Allocate or reallocate a block of memory.
Definition: mem.c:117
AVBufferRef * av_buffer_ref(AVBufferRef *buf)
Create a new reference to an AVBuffer.
Definition: buffer.c:92
int av_grow_packet(AVPacket *pkt, int grow_by)
Increase packet size, correctly zeroing padding.
Definition: avpacket.c:107
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:31
#define AV_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:638
int av_packet_ref(AVPacket *dst, AVPacket *src)
Setup a new reference to the data described by a given packet.
Definition: avpacket.c:356
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:78
static uint8_t tmp[8]
Definition: des.c:38
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
Definition: avpacket.c:49
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:1345
FF_DISABLE_DEPRECATION_WARNINGS void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:226
uint8_t * av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:284
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
Definition: avpacket.c:263
int stream_index
Definition: avcodec.h:1348
This structure stores compressed data.
Definition: avcodec.h:1323
void * av_mallocz(size_t size)
Allocate a block of size bytes with alignment suitable for all memory accesses (including vectors if ...
Definition: mem.c:211
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:1339
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:235