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 #if FF_API_DESTRUCT_PACKET
31 
33 {
34  av_free(pkt->data);
35  pkt->data = NULL;
36  pkt->size = 0;
37 }
38 
39 /* a dummy destruct callback for the callers that assume AVPacket.destruct ==
40  * NULL => static data */
42 {
43  av_assert0(0);
44 }
45 #endif
46 
48 {
49  pkt->pts = AV_NOPTS_VALUE;
50  pkt->dts = AV_NOPTS_VALUE;
51  pkt->pos = -1;
52  pkt->duration = 0;
53  pkt->convergence_duration = 0;
54  pkt->flags = 0;
55  pkt->stream_index = 0;
56 #if FF_API_DESTRUCT_PACKET
58  pkt->destruct = NULL;
60 #endif
61  pkt->buf = NULL;
62  pkt->side_data = NULL;
63  pkt->side_data_elems = 0;
64 }
65 
66 static int packet_alloc(AVBufferRef **buf, int size)
67 {
68  int ret;
69  if ((unsigned)size >= (unsigned)size + FF_INPUT_BUFFER_PADDING_SIZE)
70  return AVERROR(EINVAL);
71 
73  if (ret < 0)
74  return ret;
75 
76  memset((*buf)->data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
77 
78  return 0;
79 }
80 
81 int av_new_packet(AVPacket *pkt, int size)
82 {
83  AVBufferRef *buf = NULL;
84  int ret = packet_alloc(&buf, size);
85  if (ret < 0)
86  return ret;
87 
88  av_init_packet(pkt);
89  pkt->buf = buf;
90  pkt->data = buf->data;
91  pkt->size = size;
92 #if FF_API_DESTRUCT_PACKET
96 #endif
97 
98  return 0;
99 }
100 
102 {
103  if (pkt->size <= size)
104  return;
105  pkt->size = size;
106  memset(pkt->data + size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
107 }
108 
109 int av_grow_packet(AVPacket *pkt, int grow_by)
110 {
111  int new_size;
112  av_assert0((unsigned)pkt->size <= INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE);
113  if (!pkt->size)
114  return av_new_packet(pkt, grow_by);
115  if ((unsigned)grow_by >
116  INT_MAX - (pkt->size + FF_INPUT_BUFFER_PADDING_SIZE))
117  return -1;
118 
119  new_size = pkt->size + grow_by + FF_INPUT_BUFFER_PADDING_SIZE;
120  if (pkt->buf) {
121  int ret = av_buffer_realloc(&pkt->buf, new_size);
122  if (ret < 0)
123  return ret;
124  } else {
125  pkt->buf = av_buffer_alloc(new_size);
126  if (!pkt->buf)
127  return AVERROR(ENOMEM);
128  memcpy(pkt->buf->data, pkt->data, FFMIN(pkt->size, pkt->size + grow_by));
129 #if FF_API_DESTRUCT_PACKET
133 #endif
134  }
135  pkt->data = pkt->buf->data;
136  pkt->size += grow_by;
137  memset(pkt->data + pkt->size, 0, FF_INPUT_BUFFER_PADDING_SIZE);
138 
139  return 0;
140 }
141 
143 {
144  if (size >= INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
145  return AVERROR(EINVAL);
146 
149  if (!pkt->buf)
150  return AVERROR(ENOMEM);
151 
152  pkt->data = data;
153  pkt->size = size;
154 #if FF_API_DESTRUCT_PACKET
158 #endif
159 
160  return 0;
161 }
162 
163 #define ALLOC_MALLOC(data, size) data = av_malloc(size)
164 #define ALLOC_BUF(data, size) \
165 do { \
166  av_buffer_realloc(&pkt->buf, size); \
167  data = pkt->buf ? pkt->buf->data : NULL; \
168 } while (0)
169 
170 #define DUP_DATA(dst, src, size, padding, ALLOC) \
171  do { \
172  void *data; \
173  if (padding) { \
174  if ((unsigned)(size) > \
175  (unsigned)(size) + FF_INPUT_BUFFER_PADDING_SIZE) \
176  goto failed_alloc; \
177  ALLOC(data, size + FF_INPUT_BUFFER_PADDING_SIZE); \
178  } else { \
179  ALLOC(data, size); \
180  } \
181  if (!data) \
182  goto failed_alloc; \
183  memcpy(data, src, size); \
184  if (padding) \
185  memset((uint8_t *)data + size, 0, \
186  FF_INPUT_BUFFER_PADDING_SIZE); \
187  dst = data; \
188  } while (0)
189 
191 {
192  AVPacket tmp_pkt;
193 
195  if (!pkt->buf && pkt->data
197  && !pkt->destruct
198 #endif
199  ) {
201  tmp_pkt = *pkt;
202 
203  pkt->data = NULL;
204  pkt->side_data = NULL;
205  DUP_DATA(pkt->data, tmp_pkt.data, pkt->size, 1, ALLOC_BUF);
206 #if FF_API_DESTRUCT_PACKET
210 #endif
211 
212  if (pkt->side_data_elems) {
213  int i;
214 
215  DUP_DATA(pkt->side_data, tmp_pkt.side_data,
216  pkt->side_data_elems * sizeof(*pkt->side_data), 0, ALLOC_MALLOC);
217  memset(pkt->side_data, 0,
218  pkt->side_data_elems * sizeof(*pkt->side_data));
219  for (i = 0; i < pkt->side_data_elems; i++) {
220  DUP_DATA(pkt->side_data[i].data, tmp_pkt.side_data[i].data,
221  tmp_pkt.side_data[i].size, 1, ALLOC_MALLOC);
222  pkt->side_data[i].size = tmp_pkt.side_data[i].size;
223  pkt->side_data[i].type = tmp_pkt.side_data[i].type;
224  }
225  }
226  }
227  return 0;
228 
229 failed_alloc:
230  av_free_packet(pkt);
231  return AVERROR(ENOMEM);
232 }
233 
235 {
236  int i;
237  for (i = 0; i < pkt->side_data_elems; i++)
238  av_free(pkt->side_data[i].data);
239  av_freep(&pkt->side_data);
240  pkt->side_data_elems = 0;
241 }
242 
244 {
245  if (pkt) {
247  if (pkt->buf)
248  av_buffer_unref(&pkt->buf);
249 #if FF_API_DESTRUCT_PACKET
250  else if (pkt->destruct)
251  pkt->destruct(pkt);
252  pkt->destruct = NULL;
253 #endif
255  pkt->data = NULL;
256  pkt->size = 0;
257 
259  }
260 }
261 
263  int size)
264 {
265  int elems = pkt->side_data_elems;
266 
267  if ((unsigned)elems + 1 > INT_MAX / sizeof(*pkt->side_data))
268  return NULL;
269  if ((unsigned)size > INT_MAX - FF_INPUT_BUFFER_PADDING_SIZE)
270  return NULL;
271 
272  pkt->side_data = av_realloc(pkt->side_data,
273  (elems + 1) * sizeof(*pkt->side_data));
274  if (!pkt->side_data)
275  return NULL;
276 
278  if (!pkt->side_data[elems].data)
279  return NULL;
280  pkt->side_data[elems].size = size;
281  pkt->side_data[elems].type = type;
282  pkt->side_data_elems++;
283 
284  return pkt->side_data[elems].data;
285 }
286 
288  int *size)
289 {
290  int i;
291 
292  for (i = 0; i < pkt->side_data_elems; i++) {
293  if (pkt->side_data[i].type == type) {
294  if (size)
295  *size = pkt->side_data[i].size;
296  return pkt->side_data[i].data;
297  }
298  }
299  return NULL;
300 }
301 
303  int size)
304 {
305  int i;
306 
307  for (i = 0; i < pkt->side_data_elems; i++) {
308  if (pkt->side_data[i].type == type) {
309  if (size > pkt->side_data[i].size)
310  return AVERROR(ENOMEM);
311  pkt->side_data[i].size = size;
312  return 0;
313  }
314  }
315  return AVERROR(ENOENT);
316 }
317 
319 {
320  int i;
321 
322  dst->pts = src->pts;
323  dst->dts = src->dts;
324  dst->pos = src->pos;
325  dst->duration = src->duration;
327  dst->flags = src->flags;
328  dst->stream_index = src->stream_index;
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) {
338  return AVERROR(ENOMEM);
339  }
340  memcpy(dst_data, src_data, size);
341  }
342 
343  return 0;
344 }
345 
347 {
349  av_buffer_unref(&pkt->buf);
350  av_init_packet(pkt);
351  pkt->data = NULL;
352  pkt->size = 0;
353 }
354 
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:
376  return ret;
377 }
378 
380 {
381  *dst = *src;
382  av_init_packet(src);
383 }
384 
386 {
387  if (pkt->pts != AV_NOPTS_VALUE)
388  pkt->pts = av_rescale_q(pkt->pts, src_tb, dst_tb);
389  if (pkt->dts != AV_NOPTS_VALUE)
390  pkt->dts = av_rescale_q(pkt->dts, src_tb, dst_tb);
391  if (pkt->duration > 0)
392  pkt->duration = av_rescale_q(pkt->duration, src_tb, dst_tb);
393  if (pkt->convergence_duration > 0)
394  pkt->convergence_duration = av_rescale_q(pkt->convergence_duration, src_tb, dst_tb);
395 }
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:170
AVPacketSideDataType
Definition: avcodec.h:858
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:243
memory handling functions
int64_t pos
byte position in stream, -1 if unknown
Definition: avcodec.h:998
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
Definition: avpacket.c:101
#define ALLOC_MALLOC(data, size)
Definition: avpacket.c:163
int size
Definition: avcodec.h:974
#define ALLOC_BUF(data, size)
Definition: avpacket.c:164
int av_dup_packet(AVPacket *pkt)
Definition: avpacket.c:190
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:994
static void dummy_destruct_packet(AVPacket *pkt)
Definition: avpacket.c:41
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:142
const char data[16]
Definition: mxf.c:70
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:379
uint8_t * data
Definition: avcodec.h:923
int duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
Definition: avcodec.h:991
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
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
Definition: mathematics.c:129
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
Definition: avpacket.c:81
void av_free(void *ptr)
Free a memory block which has been allocated with av_malloc(z)() or av_realloc(). ...
Definition: mem.c:186
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:385
#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:234
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:1017
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().
enum AVPacketSideDataType type
Definition: avcodec.h:925
int side_data_elems
Definition: avcodec.h:985
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:302
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:531
int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
Copy only "properties" fields from src to dst.
Definition: avpacket.c:318
#define FFMIN(a, b)
Definition: common.h:57
static int packet_alloc(AVBufferRef **buf, int size)
Definition: avpacket.c:66
#define FF_API_DESTRUCT_PACKET
Definition: version.h:58
void av_destruct_packet(AVPacket *pkt)
Default packet destructor.
Definition: avpacket.c:32
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:346
uint8_t * data
The data buffer.
Definition: buffer.h:89
rational number numerator/denominator
Definition: rational.h:43
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:984
#define FF_DISABLE_DEPRECATION_WARNINGS
Definition: internal.h:76
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:109
void av_init_packet(AVPacket *pkt)
Initialize optional fields of a packet with default values.
Definition: avpacket.c:47
int av_packet_ref(AVPacket *dst, AVPacket *src)
Setup a new reference to the data described by a given packet.
Definition: avpacket.c:355
#define FF_ENABLE_DEPRECATION_WARNINGS
Definition: internal.h:77
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:287
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, int size)
Allocate new information of a packet.
Definition: avpacket.c:262
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