FFmpeg: libavcodec/packet.c Source File
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 FFmpeg.
6 *
7 * FFmpeg 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 * FFmpeg 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 FFmpeg; 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
30
34
35 #if FF_API_INIT_PACKET
37 {
50 }
51 #endif
52
54 {
55 memset(
pkt, 0,
sizeof(*
pkt));
56
61 }
62
64 {
68
70
72 }
73
75 {
77 return;
78
81 }
82
84 {
88
92
94
95 return 0;
96 }
97
99 {
104
109
110 return 0;
111 }
112
114 {
116 return;
119 }
120
122 {
123 int new_size;
125 if ((unsigned)grow_by >
128
131 size_t data_offset;
134 data_offset = 0;
136 } else {
138 if (data_offset > INT_MAX - new_size)
140 }
141
142 if (new_size + data_offset >
pkt->
buf->
size ||
145
146 // allocate slightly more than requested to avoid excessive
147 // reallocations
148 if (new_size + data_offset < INT_MAX - new_size/16)
149 new_size += new_size/16;
150
155 }
157 }
158 } else {
165 }
168
169 return 0;
170 }
171
173 {
176
181
184
185 return 0;
186 }
187
189 {
195 }
196
199 {
202
203 for (
i = 0;
i < elems;
i++) {
205
210 return 0;
211 }
212 }
213
216
220
226
227 return 0;
228 }
229
230
233 {
236
242
247 }
248
250 }
251
254 {
256
262 }
263 }
267 }
268
270 {
310 }
312 }
313
315 {
318
319 if (!dict)
321
322 for (int pass = 0; pass < 2; pass++) {
324 size_t total_length = 0;
325
327 for (
int i = 0;
i < 2;
i++) {
329 const size_t len = strlen(str) + 1;
330
331 if (pass)
332 memcpy(
data + total_length, str,
len);
333 else if (
len > SIZE_MAX - total_length)
336 }
337 }
338 if (pass)
339 break;
343 *
size = total_length;
344 }
345
347 }
348
351 {
352 const uint8_t *end;
354
356 return 0;
363
366
371 }
372
373 return 0;
374 }
375
378 {
380
386 return 0;
387 }
388 }
390 }
391
393 {
395
399 dst->duration =
src->duration;
401 dst->stream_index =
src->stream_index;
402 dst->opaque =
src->opaque;
403 dst->time_base =
src->time_base;
406 dst->side_data_elems = 0;
407
411
412 for (
i = 0;
i <
src->side_data_elems;
i++) {
414 size_t size =
src->side_data[
i].size;
415 uint8_t *src_data =
src->side_data[
i].data;
417
418 if (!dst_data) {
422 }
423 memcpy(dst_data, src_data,
size);
424 }
425
426 return 0;
427 }
428
430 {
435 }
436
438 {
440
442
446
453 memcpy(
dst->buf->data,
src->data,
src->size);
454
455 dst->data =
dst->buf->data;
456 } else {
461 }
463 }
464
466
467 return 0;
471 }
472
474 {
476
479
482
484 }
485
487 {
490 }
491
493 {
495
497 return 0;
498
505
507
508 return 0;
509 }
510
512 {
515
517 return 0;
518
525
529
530 return 0;
531 }
532
534 {
541 }
542
547 {
550
551 if (!pktl)
553
560 }
561 } else {
566 }
568 }
569
571
572 if (packet_buffer->
head)
574 else
575 packet_buffer->
head = pktl;
576
577 /* Add the packet in the buffered packet list. */
578 packet_buffer->
tail = pktl;
579 return 0;
580 }
581
584 {
586 if (!pktl)
590 if (!pkt_buffer->
head)
593 return 0;
594 }
595
597 {
599
605 }
607 }
608
610 {
611 uint8_t *side_data;
612 size_t side_data_size;
614
616 if (!side_data) {
617 side_data_size = 4+4+8*error_count;
619 side_data_size);
620 }
621
622 if (!side_data || side_data_size < 4+4+8*error_count)
624
626 side_data[4] = pict_type;
627 side_data[5] = error_count;
628 for (
i = 0;
i<error_count;
i++)
630
631 return 0;
632 }
633
635 {
637 uint8_t *side_data;
638 size_t side_data_size;
639
641 if (!side_data) {
644 }
645
648
652
653 return 0;
654 }
655
658 {
659 for (
int i = 0;
i < nb_sd;
i++)
662
664 }
665
669 {
671 int nb_sd = *pnb_sd;
672
673 for (
int i = 0;
i < nb_sd;
i++) {
675 continue;
676
681 }
682
683 if (nb_sd == INT_MAX)
685
689
694 *pnb_sd = nb_sd + 1;
695
696 return &sd[nb_sd];
697 }
698
702 {
704 }
705
709 {
712
715
720
722 if (!sd)
724
725 return sd;
726 }
727
730 {
731 int nb_sd = *pnb_sd;
732
733 for (
int i = nb_sd - 1;
i >= 0;
i--) {
735 continue;
738 break;
739 }
740
741 *pnb_sd = nb_sd;
742 }
743
745 {
747 int nb_sd = *pnb_sd;
748
749 for (
int i = 0;
i < nb_sd;
i++)
751
753 *pnb_sd = 0;
754 }
static void error(const char *err)
@ AV_PKT_DATA_DISPLAYMATRIX
This side data contains a 3x3 transformation matrix describing an affine transformation that needs to...
void av_packet_unref(AVPacket *pkt)
Wipe the packet.
@ AV_PKT_DATA_AMBIENT_VIEWING_ENVIRONMENT
Ambient viewing environment metadata, as defined by H.274.
Filter the word "frame" indicates either a video frame or a group of audio as stored in an AVFrame structure Format for each input and each output the list of supported formats For video that means pixel format For audio that means channel sample they are references to shared objects When the negotiation mechanism computes the intersection of the formats supported at each end of a all references to both lists are replaced with a reference to the intersection And when a single format is eventually chosen for a link amongst the remaining all references to the list are updated That means that if a filter requires that its input and output have the same format amongst a supported all it has to do is use a reference to the same list of formats query_formats can leave some formats unset and return AVERROR(EAGAIN) to cause the negotiation mechanism toagain later. That can be used by filters with complex requirements to use the format negotiated on one link to set the formats supported on another. Frame references ownership and permissions
@ AV_PKT_DATA_QUALITY_STATS
This side data contains quality related information from the encoder.
@ AV_PKT_DATA_FRAME_CROPPING
The number of pixels to discard from the top/bottom/left/right border of the decoded frame to obtain ...
@ AV_PKT_DATA_NEW_EXTRADATA
The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format that the extradata buffer was...
uint8_t * data
The data buffer.
@ AV_PKT_DATA_MASTERING_DISPLAY_METADATA
Mastering display metadata (based on SMPTE-2086:2014).
int av_grow_packet(AVPacket *pkt, int grow_by)
Increase packet size, correctly zeroing padding.
This structure stores auxiliary information for decoding, presenting, or otherwise processing the cod...
int64_t wallclock
A UTC timestamp, in microseconds, since Unix epoch (e.g, av_gettime()).
@ AV_PKT_DATA_ENCRYPTION_INIT_INFO
This side data is encryption initialization data.
int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Shrink the already allocated side data buffer.
@ AV_PKT_DATA_FALLBACK_TRACK
This side data contains an integer value representing the stream index of a "fallback" track.
void av_packet_side_data_remove(AVPacketSideData *sd, int *pnb_sd, enum AVPacketSideDataType type)
Remove side data of the given type from a side data array.
@ AV_PKT_DATA_S12M_TIMECODE
Timecode which conforms to SMPTE ST 12-1:2014.
int64_t duration
Duration of this packet in AVStream->time_base units, 0 if unknown.
AVBufferRef * av_buffer_ref(const AVBufferRef *buf)
Create a new reference to an AVBuffer.
void av_packet_free_side_data(AVPacket *pkt)
Convenience function to free all the side data stored.
void av_packet_free(AVPacket **pkt)
Free the packet, if the packet is reference counted, it will be unreferenced first.
trying all byte sequences megabyte in length and selecting the best looking sequence will yield cases to try But a word about quality
@ AV_PKT_DATA_DOVI_CONF
DOVI configuration ref: dolby-vision-bitstreams-within-the-iso-base-media-file-format-v2....
@ AV_PKT_DATA_REPLAYGAIN
This side data should be associated with an audio stream and contains ReplayGain information in form ...
@ AV_PKT_DATA_IAMF_RECON_GAIN_INFO_PARAM
IAMF Recon Gain Info Parameter Data associated with the audio frame.
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.
@ AV_PKT_DATA_PALETTE
An AV_PKT_DATA_PALETTE side data packet contains exactly AVPALETTE_SIZE bytes worth of palette.
AVBufferRef * opaque_ref
AVBufferRef for free use by the API user.
void av_shrink_packet(AVPacket *pkt, int size)
Reduce packet size, correctly zeroing padding.
int avpriv_packet_list_get(PacketList *pkt_buffer, AVPacket *pkt)
Remove the oldest AVPacket in the list and return it.
static double val(void *priv, double ch)
it s the only field you need to keep assuming you have a context There is some magic you don t need to care about around this just let it vf type
@ AV_PKT_DATA_WEBVTT_SETTINGS
The optional settings (rendering instructions) that immediately follow the timestamp specifier of a W...
@ AV_PKT_DATA_PARAM_CHANGE
An AV_PKT_DATA_PARAM_CHANGE side data packet is laid out as follows:
const char * av_packet_side_data_name(enum AVPacketSideDataType type)
int av_new_packet(AVPacket *pkt, int size)
Allocate the payload of a packet and initialize its fields with default values.
void * av_realloc_array(void *ptr, size_t nmemb, size_t size)
void av_buffer_default_free(void *opaque, uint8_t *data)
Default free callback, which calls av_free() on the buffer data.
#define av_assert0(cond)
assert() equivalent, that is always enabled.
@ AV_PKT_DATA_STEREO3D
This side data should be associated with a video stream and contains Stereoscopic 3D information in f...
int64_t av_rescale_q(int64_t a, AVRational bq, AVRational cq)
Rescale a 64-bit integer by 2 rational numbers.
@ AV_PKT_DATA_SUBTITLE_POSITION
Subtitle event position.
@ AV_PKT_DATA_LCEVC
Raw LCEVC payload data, as a uint8_t array, with NAL emulation bytes intact.
static AVPacketSideData * packet_side_data_add(AVPacketSideData **psd, int *pnb_sd, enum AVPacketSideDataType type, void *data, size_t size)
void * opaque
for some private data of the user
void av_packet_side_data_free(AVPacketSideData **psd, int *pnb_sd)
Convenience function to free all the side data stored in an array, and the array itself.
void avpriv_packet_list_free(PacketList *pkt_buf)
Wipe the list and unref all the packets in it.
AVBufferRef * buf
A reference to the reference-counted buffer where the packet data is stored.
void av_buffer_unref(AVBufferRef **buf)
Free a given reference and automatically free the buffer if there are no more references to it.
int avpriv_packet_list_put(PacketList *packet_buffer, AVPacket *pkt, int(*copy)(AVPacket *dst, const AVPacket *src), int flags)
Append an AVPacket to the list.
Rational number (pair of numerator and denominator).
enum AVPacketSideDataType type
This structure supplies correlation between a packet timestamp and a wall clock production time.
int av_packet_ref(AVPacket *dst, const AVPacket *src)
Setup a new reference to the data described by a given packet.
void av_packet_move_ref(AVPacket *dst, AVPacket *src)
Move every field in src to dst and reset src.
struct PacketListEntry * next
@ AV_PKT_DATA_CONTENT_LIGHT_LEVEL
Content light level (based on CTA-861.3).
static int packet_alloc(AVBufferRef **buf, int size)
AVBufferRef * av_buffer_create(uint8_t *data, size_t size, void(*free)(void *opaque, uint8_t *data), void *opaque, int flags)
Create an AVBuffer from an existing array.
@ AV_PKT_DATA_NB
The number of side data types.
const AVPacketSideData * av_packet_side_data_get(const AVPacketSideData *sd, int nb_sd, enum AVPacketSideDataType type)
Get side information from a side data array.
@ AV_PKT_DATA_SPHERICAL
This side data should be associated with a video stream and corresponds to the AVSphericalMapping str...
int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size)
Initialize a reference-counted packet from av_malloc()ed data.
static void copy(const float *p1, float *p2, const int length)
uint8_t ptrdiff_t const uint8_t ptrdiff_t int intptr_t intptr_t int int16_t * dst
@ AV_PKT_DATA_DYNAMIC_HDR10_PLUS
HDR10+ dynamic metadata associated with a video frame.
static AVRational av_make_q(int num, int den)
Create an AVRational.
@ AV_PKT_DATA_METADATA_UPDATE
A list of zero terminated key/value strings.
#define AV_NOPTS_VALUE
Undefined timestamp value.
int av_packet_unpack_dictionary(const uint8_t *data, size_t size, AVDictionary **dict)
Unpack a dictionary from side_data.
int64_t dts
Decompression timestamp in AVStream->time_base units; the time at which the packet is decompressed.
AVPacketSideData * av_packet_side_data_add(AVPacketSideData **psd, int *pnb_sd, enum AVPacketSideDataType type, void *data, size_t size, int flags)
Wrap existing data as packet side data.
@ AV_PKT_DATA_PRFT
Producer Reference Time data corresponding to the AVProducerReferenceTime struct, usually exported by...
uint8_t * av_packet_pack_dictionary(AVDictionary *dict, size_t *size)
Pack a dictionary for use in side_data.
int av_packet_make_refcounted(AVPacket *pkt)
Ensure the data described by a given packet is reference counted.
int flags
A combination of AV_PKT_FLAG values.
AVPacket * av_packet_alloc(void)
Allocate an AVPacket and set its fields to default values.
AVBufferRef * av_buffer_alloc(size_t size)
Allocate an AVBuffer of the given size using av_malloc().
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.
size_t size
Size of data in bytes.
@ AV_PKT_DATA_STRINGS_METADATA
A list of zero terminated key/value strings.
int av_packet_copy_props(AVPacket *dst, const AVPacket *src)
Copy only "properties" fields from src to dst.
@ AV_PKT_DATA_CPB_PROPERTIES
This side data corresponds to the AVCPBProperties struct.
@ AV_PKT_DATA_H263_MB_INFO
An AV_PKT_DATA_H263_MB_INFO side data packet contains a number of structures with info about macroblo...
#define i(width, name, range_min, range_max)
int64_t pts
Presentation timestamp in AVStream->time_base units; the time at which the decompressed packet will b...
static void get_packet_defaults(AVPacket *pkt)
uint8_t * av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type, size_t *size)
Get side information from packet.
@ AV_PKT_DATA_ICC_PROFILE
ICC profile data consisting of an opaque octet buffer following the format described by ISO 15076-1.
#define av_assert1(cond)
assert() equivalent, that does not lie in speed critical code.
void * av_mallocz(size_t size)
Allocate a memory block with alignment suitable for all memory accesses (including vectors if availab...
@ AV_PKT_DATA_MPEGTS_STREAM_ID
MPEGTS stream ID as uint8_t, this is required to pass the stream ID information from the demuxer to t...
int av_buffer_replace(AVBufferRef **pdst, const AVBufferRef *src)
Ensure dst refers to the same data as src.
int av_buffer_is_writable(const AVBufferRef *buf)
@ AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL
Data found in BlockAdditional element of matroska container.
@ AV_PKT_DATA_JP_DUALMONO
An AV_PKT_DATA_JP_DUALMONO side data packet indicates that the packet may contain "dual mono" audio s...
AVPacketSideData * av_packet_side_data_new(AVPacketSideData **psd, int *pnb_sd, enum AVPacketSideDataType type, size_t size, int flags)
Allocate a new packet side data.
AVPacketSideData * side_data
Additional packet data that can be provided by the container.
#define AV_INPUT_BUFFER_PADDING_SIZE
@ AV_PKT_DATA_WEBVTT_IDENTIFIER
The optional first identifier line of a WebVTT cue.
uint8_t * av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type, size_t size)
Allocate new information of a packet.
int av_packet_make_writable(AVPacket *pkt)
Create a writable reference for the data described by a given packet, avoiding data copy if possible.
@ AV_PKT_DATA_AFD
Active Format Description data consisting of a single byte as specified in ETSI TS 101 154 using AVAc...
@ AV_PKT_DATA_SKIP_SAMPLES
Recommmends skipping the specified number of samples.
int ff_side_data_set_prft(AVPacket *pkt, int64_t timestamp)
int av_buffer_realloc(AVBufferRef **pbuf, size_t size)
Reallocate a given buffer.
@ AV_PKT_DATA_ENCRYPTION_INFO
This side data contains encryption info for how to decrypt the packet.
@ AV_PKT_DATA_AUDIO_SERVICE_TYPE
This side data should be associated with an audio stream and corresponds to enum AVAudioServiceType.
@ AV_PKT_DATA_A53_CC
ATSC A53 Part 4 Closed Captions.
A reference to a data buffer.
This structure stores compressed data.
int av_dict_set(AVDictionary **pm, const char *key, const char *value, int flags)
Set the given entry in *pm, overwriting an existing entry.
int64_t pos
byte position in stream, -1 if unknown
#define flags(name, subs,...)
#define AVERROR_INVALIDDATA
Invalid data found when processing input.
int ff_side_data_set_encoder_stats(AVPacket *pkt, int quality, int64_t *error, int error_count, int pict_type)
@ AV_PKT_DATA_IAMF_MIX_GAIN_PARAM
IAMF Mix Gain Parameter Data associated with the audio frame.
const AVDictionaryEntry * av_dict_iterate(const AVDictionary *m, const AVDictionaryEntry *prev)
Iterate over a dictionary.
AVRational time_base
Time base of the packet's timestamps.
void * av_realloc(void *ptr, size_t size)
Allocate, reallocate, or free a block of memory.
AVPacket * av_packet_clone(const AVPacket *src)
Create a new packet that references the same data as src.
@ AV_PKT_DATA_IAMF_DEMIXING_INFO_PARAM
IAMF Demixing Info Parameter Data associated with the audio frame.
Generated on Fri Aug 22 2025 13:58:45 for FFmpeg by
doxygen
1.8.17