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/mem.h"
28 #include "avcodec.h"
29 #if FF_API_DESTRUCT_PACKET
30 
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 */
41 {
42  av_assert0(0);
43 }
44 #endif
45 
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
57  pkt->destruct = NULL;
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 
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
95 #endif
96 
97  return 0;
98 }
99 
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
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 
142 {
143  if (size >= INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
144  return AVERROR(EINVAL);
145 
148  if (!pkt->buf)
149  return AVERROR(ENOMEM);
150 
151  pkt->data = data;
152  pkt->size = size;
153 #if FF_API_DESTRUCT_PACKET
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 
190 {
191  AVPacket tmp_pkt;
192 
194  if (!pkt->buf && pkt->data
196  && !pkt->destruct
197 #endif
198  ) {
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
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 
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 
243 {
244  if (pkt) {
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
254  pkt->data = NULL;
255  pkt->size = 0;
256 
258  }
259 }
260 
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 
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 
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 
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 
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;
326  dst->flags = src->flags;
327  dst->stream_index = src->stream_index;
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) {
337  return AVERROR(ENOMEM);
338  }
339  memcpy(dst_data, src_data, size);
340  }
341 
342  return 0;
343 }
344 
346 {
348  av_buffer_unref(&pkt->buf);
349  av_init_packet(pkt);
350  pkt->data = NULL;
351  pkt->size = 0;
352 }
353 
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:
375  return ret;
376 }
377 
379 {
380  *dst = *src;
381  av_init_packet(src);
382 }
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:169
AVPacketSideDataType
Definition: avcodec.h:885
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:105
int size
void av_free_packet(AVPacket *pkt)
Free a packet.
Definition: avpacket.c:242
memory handling functions
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:1002
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:100
#define ALLOC_MALLOC(data, size)
Definition: avpacket.c:162
int size
Definition: avcodec.h:974
#define ALLOC_BUF(data, size)
Definition: avpacket.c:163
int av_dup_packet(AVPacket *pkt)
Definition: avpacket.c:189
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:198
#define av_assert0(cond)
assert() equivalent, that is always enabled.
Definition: avassert.h:37
uint8_t
attribute_deprecated void(* destruct)(struct AVPacket *)
Definition: avcodec.h:998
static void dummy_destruct_packet(AVPacket *pkt)
Definition: avpacket.c:40
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:141
const char data[16]
Definition: mxf.c:66
uint8_t * data
Definition: avcodec.h:973
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
Definition: avpacket.c:378
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:995
void av_buffer_default_free(void *opaque, uint8_t *data)
Default free callback, which calls av_free() on the buffer data.
Definition: buffer.c:60
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:80
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
#define AVERROR(e)
Definition: error.h:43
void av_packet_free_side_data(AVPacket *pkt)
Convenience function to free all the side data stored.
Definition: avpacket.c:233
int64_t convergence_duration
Time difference in AVStream->time_base units from the pts of this packet to the point at which the ou...
Definition: avcodec.h:1021
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
Definition: avcodec.h:956
simple assert() macros that are a bit more flexible than ISO C assert().
int side_data_elems
Definition: avcodec.h:989
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:27
int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Shrink the already allocated side data buffer.
Definition: avpacket.c:301
int av_buffer_realloc(AVBufferRef **pbuf, int size)
Reallocate a given buffer.
Definition: buffer.c:146
int flags
A combination of AV_PKT_FLAG values.
Definition: avcodec.h:979
common internal API header
#define FF_INPUT_BUFFER_PADDING_SIZE
Required number of additionally allocated bytes at the end of the input bitstream for decoding...
Definition: avcodec.h:509
int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
Copy only "properties" fields from src to dst.
Definition: avpacket.c:317
#define FFMIN(a, b)
Definition: common.h:57
static int packet_alloc(AVBufferRef **buf, int size)
Definition: avpacket.c:65
#define FF_API_DESTRUCT_PACKET
Definition: version.h:58
void av_destruct_packet(AVPacket *pkt)
Default packet destructor.
Definition: avpacket.c:31
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:65
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
Definition: avpacket.c:345
uint8_t * data
The data buffer.
Definition: buffer.h:89
A reference to a data buffer.
Definition: buffer.h:81
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:75
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:91
int av_grow_packet(AVPacket *pkt, int grow_by)
Increase packet size, correctly zeroing padding.
Definition: avpacket.c:108
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:46
int av_packet_ref(AVPacket *dst, AVPacket *src)
Setup a new reference to the data described by a given packet.
Definition: avpacket.c:354
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:76
struct AVPacket::@11 * side_data
Additional packet data that can be provided by the container.
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed...
Definition: avcodec.h:972
uint8_t * av_packet_get_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int *size)
Get side information from packet.
Definition: avpacket.c:286
enum AVPacketSideDataType type
Definition: avcodec.h:987
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
Definition: avpacket.c:261
int stream_index
Definition: avcodec.h:975
This structure stores compressed data.
Definition: avcodec.h:950
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
Definition: avcodec.h:966
#define AV_NOPTS_VALUE
Undefined timestamp value.
Definition: avutil.h:228