4 * This file is part of FFmpeg.
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifndef AVCODEC_PACKET_H
22 #define AVCODEC_PACKET_H
27 #include "libavutil/attributes.h"
28 #include "libavutil/buffer.h"
29 #include "libavutil/dict.h"
30 #include "libavutil/rational.h"
32 #include "libavcodec/version.h"
35 * @defgroup lavc_packet AVPacket
37 * Types and functions for working with AVPacket.
40 enum AVPacketSideDataType {
42 * An AV_PKT_DATA_PALETTE side data packet contains exactly AVPALETTE_SIZE
43 * bytes worth of palette. This side data signals that a new palette is
49 * The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format
50 * that the extradata buffer was changed and the receiving side should
51 * act upon it appropriately. The new extradata is embedded in the side
52 * data buffer and should be immediately used for processing the current
55 AV_PKT_DATA_NEW_EXTRADATA,
58 * An AV_PKT_DATA_PARAM_CHANGE side data packet is laid out as follows:
61 * if (param_flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT)
63 * if (param_flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT)
64 * u64le channel_layout
65 * if (param_flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE)
67 * if (param_flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS)
72 AV_PKT_DATA_PARAM_CHANGE,
75 * An AV_PKT_DATA_H263_MB_INFO side data packet contains a number of
76 * structures with info about macroblocks relevant to splitting the
77 * packet into smaller packets on macroblock edges (e.g. as for RFC 2190).
78 * That is, it does not necessarily contain info about all macroblocks,
79 * as long as the distance between macroblocks in the info is smaller
80 * than the target payload size.
81 * Each MB info structure is 12 bytes, and is laid out as follows:
83 * u32le bit offset from the start of the packet
84 * u8 current quantizer at the start of the macroblock
86 * u16le macroblock address within the GOB
87 * u8 horizontal MV predictor
88 * u8 vertical MV predictor
89 * u8 horizontal MV predictor for block number 3
90 * u8 vertical MV predictor for block number 3
93 AV_PKT_DATA_H263_MB_INFO,
96 * This side data should be associated with an audio stream and contains
97 * ReplayGain information in form of the AVReplayGain struct.
99 AV_PKT_DATA_REPLAYGAIN,
102 * This side data contains a 3x3 transformation matrix describing an affine
103 * transformation that needs to be applied to the decoded video frames for
104 * correct presentation.
106 * See libavutil/display.h for a detailed description of the data.
108 AV_PKT_DATA_DISPLAYMATRIX,
111 * This side data should be associated with a video stream and contains
112 * Stereoscopic 3D information in form of the AVStereo3D struct.
114 AV_PKT_DATA_STEREO3D,
117 * This side data should be associated with an audio stream and corresponds
118 * to enum AVAudioServiceType.
120 AV_PKT_DATA_AUDIO_SERVICE_TYPE,
123 * This side data contains quality related information from the encoder.
125 * u32le quality factor of the compressed frame. Allowed range is between 1 (good) and FF_LAMBDA_MAX (bad).
129 * u64le[error count] sum of squared differences between encoder in and output
132 AV_PKT_DATA_QUALITY_STATS,
135 * This side data contains an integer value representing the stream index
136 * of a "fallback" track. A fallback track indicates an alternate
137 * track to use when the current track can not be decoded for some reason.
138 * e.g. no decoder available for codec.
140 AV_PKT_DATA_FALLBACK_TRACK,
143 * This side data corresponds to the AVCPBProperties struct.
145 AV_PKT_DATA_CPB_PROPERTIES,
148 * Recommmends skipping the specified number of samples
150 * u32le number of samples to skip from start of this packet
151 * u32le number of samples to skip from end of this packet
152 * u8 reason for start skip
153 * u8 reason for end skip (0=padding silence, 1=convergence)
156 AV_PKT_DATA_SKIP_SAMPLES,
159 * An AV_PKT_DATA_JP_DUALMONO side data packet indicates that
160 * the packet may contain "dual mono" audio specific to Japanese DTV
161 * and if it is true, recommends only the selected channel to be used.
163 * u8 selected channels (0=mail/left, 1=sub/right, 2=both)
166 AV_PKT_DATA_JP_DUALMONO,
169 * A list of zero terminated key/value strings. There is no end marker for
170 * the list, so it is required to rely on the side data size to stop.
172 AV_PKT_DATA_STRINGS_METADATA,
175 * Subtitle event position
183 AV_PKT_DATA_SUBTITLE_POSITION,
186 * Data found in BlockAdditional element of matroska container. There is
187 * no end marker for the data, so it is required to rely on the side data
188 * size to recognize the end. 8 byte id (as found in BlockAddId) followed
191 AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL,
194 * The optional first identifier line of a WebVTT cue.
196 AV_PKT_DATA_WEBVTT_IDENTIFIER,
199 * The optional settings (rendering instructions) that immediately
200 * follow the timestamp specifier of a WebVTT cue.
202 AV_PKT_DATA_WEBVTT_SETTINGS,
205 * A list of zero terminated key/value strings. There is no end marker for
206 * the list, so it is required to rely on the side data size to stop. This
207 * side data includes updated metadata which appeared in the stream.
209 AV_PKT_DATA_METADATA_UPDATE,
212 * MPEGTS stream ID as uint8_t, this is required to pass the stream ID
213 * information from the demuxer to the corresponding muxer.
215 AV_PKT_DATA_MPEGTS_STREAM_ID,
218 * Mastering display metadata (based on SMPTE-2086:2014). This metadata
219 * should be associated with a video stream and contains data in the form
220 * of the AVMasteringDisplayMetadata struct.
222 AV_PKT_DATA_MASTERING_DISPLAY_METADATA,
225 * This side data should be associated with a video stream and corresponds
226 * to the AVSphericalMapping structure.
228 AV_PKT_DATA_SPHERICAL,
231 * Content light level (based on CTA-861.3). This metadata should be
232 * associated with a video stream and contains data in the form of the
233 * AVContentLightMetadata struct.
235 AV_PKT_DATA_CONTENT_LIGHT_LEVEL,
238 * ATSC A53 Part 4 Closed Captions. This metadata should be associated with
239 * a video stream. A53 CC bitstream is stored as uint8_t in AVPacketSideData.data.
240 * The number of bytes of CC data is AVPacketSideData.size.
245 * This side data is encryption initialization data.
246 * The format is not part of ABI, use av_encryption_init_info_* methods to
249 AV_PKT_DATA_ENCRYPTION_INIT_INFO,
252 * This side data contains encryption info for how to decrypt the packet.
253 * The format is not part of ABI, use av_encryption_info_* methods to access.
255 AV_PKT_DATA_ENCRYPTION_INFO,
258 * Active Format Description data consisting of a single byte as specified
259 * in ETSI TS 101 154 using AVActiveFormatDescription enum.
264 * Producer Reference Time data corresponding to the AVProducerReferenceTime struct,
265 * usually exported by some encoders (on demand through the prft flag set in the
266 * AVCodecContext export_side_data field).
271 * ICC profile data consisting of an opaque octet buffer following the
272 * format described by ISO 15076-1.
274 AV_PKT_DATA_ICC_PROFILE,
279 * dolby-vision-bitstreams-within-the-iso-base-media-file-format-v2.1.2, section 2.2
280 * dolby-vision-bitstreams-in-mpeg-2-transport-stream-multiplex-v1.2, section 3.3
281 * Tags are stored in struct AVDOVIDecoderConfigurationRecord.
283 AV_PKT_DATA_DOVI_CONF,
286 * Timecode which conforms to SMPTE ST 12-1:2014. The data is an array of 4 uint32_t
287 * where the first uint32_t describes how many (1-3) of the other timecodes are used.
288 * The timecode format is described in the documentation of av_timecode_get_smpte_from_framenum()
289 * function in libavutil/timecode.h.
291 AV_PKT_DATA_S12M_TIMECODE,
294 * The number of side data types.
295 * This is not part of the public API/ABI in the sense that it may
296 * change when new side data types are added.
297 * This must stay the last enum value.
298 * If its value becomes huge, some code using it
299 * needs to be updated as it assumes it to be smaller than other limits.
304 #define AV_PKT_DATA_QUALITY_FACTOR AV_PKT_DATA_QUALITY_STATS //DEPRECATED
306 typedef struct AVPacketSideData {
308 #if FF_API_BUFFER_SIZE_T
313 enum AVPacketSideDataType type;
317 * This structure stores compressed data. It is typically exported by demuxers
318 * and then passed as input to decoders, or received as output from encoders and
319 * then passed to muxers.
321 * For video, it should typically contain one compressed frame. For audio it may
322 * contain several compressed frames. Encoders are allowed to output empty
323 * packets, with no compressed data, containing only side data
324 * (e.g. to update some stream parameters at the end of encoding).
326 * The semantics of data ownership depends on the buf field.
327 * If it is set, the packet data is dynamically allocated and is
328 * valid indefinitely until a call to av_packet_unref() reduces the
329 * reference count to 0.
331 * If the buf field is not set av_packet_ref() would make a copy instead
332 * of increasing the reference count.
334 * The side data is always allocated with av_malloc(), copied by
335 * av_packet_ref() and freed by av_packet_unref().
337 * sizeof(AVPacket) being a part of the public ABI is deprecated. once
338 * av_init_packet() is removed, new packets will only be able to be allocated
339 * with av_packet_alloc(), and new fields may be added to the end of the struct
342 * @see av_packet_alloc
344 * @see av_packet_unref
346 typedef struct AVPacket {
348 * A reference to the reference-counted buffer where the packet data is
350 * May be NULL, then the packet data is not reference-counted.
354 * Presentation timestamp in AVStream->time_base units; the time at which
355 * the decompressed packet will be presented to the user.
356 * Can be AV_NOPTS_VALUE if it is not stored in the file.
357 * pts MUST be larger or equal to dts as presentation cannot happen before
358 * decompression, unless one wants to view hex dumps. Some formats misuse
359 * the terms dts and pts/cts to mean something different. Such timestamps
360 * must be converted to true pts/dts before they are stored in AVPacket.
364 * Decompression timestamp in AVStream->time_base units; the time at which
365 * the packet is decompressed.
366 * Can be AV_NOPTS_VALUE if it is not stored in the file.
373 * A combination of AV_PKT_FLAG values
377 * Additional packet data that can be provided by the container.
378 * Packet can contain several types of side information.
380 AVPacketSideData *side_data;
384 * Duration of this packet in AVStream->time_base units, 0 if unknown.
385 * Equals next_pts - this_pts in presentation order.
389 int64_t pos; ///< byte position in stream, -1 if unknown
391 #if FF_API_CONVERGENCE_DURATION
393 * @deprecated Same as the duration field, but as int64_t. This was required
394 * for Matroska subtitles, whose duration values could overflow when the
395 * duration field was still an int.
398 int64_t convergence_duration;
402 #if FF_API_INIT_PACKET
404 typedef struct AVPacketList {
406 struct AVPacketList *next;
410 #define AV_PKT_FLAG_KEY 0x0001 ///< The packet contains a keyframe
411 #define AV_PKT_FLAG_CORRUPT 0x0002 ///< The packet content is corrupted
413 * Flag is used to discard packets which are required to maintain valid
414 * decoder state but are not required for output and should be dropped
417 #define AV_PKT_FLAG_DISCARD 0x0004
419 * The packet comes from a trusted source.
421 * Otherwise-unsafe constructs such as arbitrary pointers to data
422 * outside the packet may be followed.
424 #define AV_PKT_FLAG_TRUSTED 0x0008
426 * Flag is used to indicate packets that contain frames that can
427 * be discarded by the decoder. I.e. Non-reference frames.
429 #define AV_PKT_FLAG_DISPOSABLE 0x0010
431 enum AVSideDataParamChangeFlags {
432 AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT = 0x0001,
433 AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT = 0x0002,
434 AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE = 0x0004,
435 AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS = 0x0008,
439 * Allocate an AVPacket and set its fields to default values. The resulting
440 * struct must be freed using av_packet_free().
442 * @return An AVPacket filled with default values or NULL on failure.
444 * @note this only allocates the AVPacket itself, not the data buffers. Those
445 * must be allocated through other means such as av_new_packet.
449 AVPacket *av_packet_alloc(void);
452 * Create a new packet that references the same data as src.
454 * This is a shortcut for av_packet_alloc()+av_packet_ref().
456 * @return newly created AVPacket on success, NULL on error.
458 * @see av_packet_alloc
461 AVPacket *av_packet_clone(const AVPacket *src);
464 * Free the packet, if the packet is reference counted, it will be
465 * unreferenced first.
467 * @param pkt packet to be freed. The pointer will be set to NULL.
468 * @note passing NULL is a no-op.
470 void av_packet_free(AVPacket **pkt);
472 #if FF_API_INIT_PACKET
474 * Initialize optional fields of a packet with default values.
476 * Note, this does not touch the data and size members, which have to be
477 * initialized separately.
481 * @see av_packet_alloc
482 * @see av_packet_unref
484 * @deprecated This function is deprecated. Once it's removed,
485 sizeof(AVPacket) will not be a part of the ABI anymore.
488 void av_init_packet(AVPacket *pkt);
492 * Allocate the payload of a packet and initialize its fields with
496 * @param size wanted payload size
497 * @return 0 if OK, AVERROR_xxx otherwise
499 int av_new_packet(AVPacket *pkt, int size);
502 * Reduce packet size, correctly zeroing padding
505 * @param size new size
507 void av_shrink_packet(AVPacket *pkt, int size);
510 * Increase packet size, correctly zeroing padding
513 * @param grow_by number of bytes by which to increase the size of the packet
515 int av_grow_packet(AVPacket *pkt, int grow_by);
518 * Initialize a reference-counted packet from av_malloc()ed data.
520 * @param pkt packet to be initialized. This function will set the data, size,
521 * and buf fields, all others are left untouched.
522 * @param data Data allocated by av_malloc() to be used as packet data. If this
523 * function returns successfully, the data is owned by the underlying AVBuffer.
524 * The caller may not access the data through other means.
525 * @param size size of data in bytes, without the padding. I.e. the full buffer
526 * size is assumed to be size + AV_INPUT_BUFFER_PADDING_SIZE.
528 * @return 0 on success, a negative AVERROR on error
530 int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size);
532 #if FF_API_AVPACKET_OLD_API
534 * @warning This is a hack - the packet memory allocation stuff is broken. The
535 * packet is allocated if it was not really allocated.
537 * @deprecated Use av_packet_ref or av_packet_make_refcounted
540 int av_dup_packet(AVPacket *pkt);
542 * Copy packet, including contents
544 * @return 0 on success, negative AVERROR on fail
546 * @deprecated Use av_packet_ref
549 int av_copy_packet(AVPacket *dst, const AVPacket *src);
552 * Copy packet side data
554 * @return 0 on success, negative AVERROR on fail
556 * @deprecated Use av_packet_copy_props
559 int av_copy_packet_side_data(AVPacket *dst, const AVPacket *src);
564 * @deprecated Use av_packet_unref
566 * @param pkt packet to free
569 void av_free_packet(AVPacket *pkt);
572 * Allocate new information of a packet.
575 * @param type side information type
576 * @param size side information size
577 * @return pointer to fresh allocated data or NULL otherwise
579 uint8_t* av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
580 #if FF_API_BUFFER_SIZE_T
587 * Wrap an existing array as a packet side data.
590 * @param type side information type
591 * @param data the side data array. It must be allocated with the av_malloc()
592 * family of functions. The ownership of the data is transferred to
594 * @param size side information size
595 * @return a non-negative number on success, a negative AVERROR code on
596 * failure. On failure, the packet is unchanged and the data remains
597 * owned by the caller.
599 int av_packet_add_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
600 uint8_t *data, size_t size);
603 * Shrink the already allocated side data buffer
606 * @param type side information type
607 * @param size new side information size
608 * @return 0 on success, < 0 on failure
610 int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
611 #if FF_API_BUFFER_SIZE_T
618 * Get side information from packet.
621 * @param type desired side information type
622 * @param size If supplied, *size will be set to the size of the side data
623 * or to zero if the desired side data is not present.
624 * @return pointer to data if present or NULL otherwise
626 uint8_t* av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type,
627 #if FF_API_BUFFER_SIZE_T
633 #if FF_API_MERGE_SD_API
635 int av_packet_merge_side_data(AVPacket *pkt);
638 int av_packet_split_side_data(AVPacket *pkt);
641 const char *av_packet_side_data_name(enum AVPacketSideDataType type);
644 * Pack a dictionary for use in side_data.
646 * @param dict The dictionary to pack.
647 * @param size pointer to store the size of the returned data
648 * @return pointer to data if successful, NULL otherwise
650 #if FF_API_BUFFER_SIZE_T
651 uint8_t *av_packet_pack_dictionary(AVDictionary *dict, int *size);
653 uint8_t *av_packet_pack_dictionary(AVDictionary *dict, size_t *size);
656 * Unpack a dictionary from side_data.
658 * @param data data from side_data
659 * @param size size of the data
660 * @param dict the metadata storage dictionary
661 * @return 0 on success, < 0 on failure
663 #if FF_API_BUFFER_SIZE_T
664 int av_packet_unpack_dictionary(const uint8_t *data, int size, AVDictionary **dict);
666 int av_packet_unpack_dictionary(const uint8_t *data, size_t size,
667 AVDictionary **dict);
671 * Convenience function to free all the side data stored.
672 * All the other fields stay untouched.
676 void av_packet_free_side_data(AVPacket *pkt);
679 * Setup a new reference to the data described by a given packet
681 * If src is reference-counted, setup dst as a new reference to the
682 * buffer in src. Otherwise allocate a new buffer in dst and copy the
683 * data from src into it.
685 * All the other fields are copied from src.
687 * @see av_packet_unref
689 * @param dst Destination packet. Will be completely overwritten.
690 * @param src Source packet
692 * @return 0 on success, a negative AVERROR on error. On error, dst
693 * will be blank (as if returned by av_packet_alloc()).
695 int av_packet_ref(AVPacket *dst, const AVPacket *src);
700 * Unreference the buffer referenced by the packet and reset the
701 * remaining packet fields to their default values.
703 * @param pkt The packet to be unreferenced.
705 void av_packet_unref(AVPacket *pkt);
708 * Move every field in src to dst and reset src.
710 * @see av_packet_unref
712 * @param src Source packet, will be reset
713 * @param dst Destination packet
715 void av_packet_move_ref(AVPacket *dst, AVPacket *src);
718 * Copy only "properties" fields from src to dst.
720 * Properties for the purpose of this function are all the fields
721 * beside those related to the packet data (buf, data, size)
723 * @param dst Destination packet
724 * @param src Source packet
726 * @return 0 on success AVERROR on failure.
728 int av_packet_copy_props(AVPacket *dst, const AVPacket *src);
731 * Ensure the data described by a given packet is reference counted.
733 * @note This function does not ensure that the reference will be writable.
734 * Use av_packet_make_writable instead for that purpose.
737 * @see av_packet_make_writable
739 * @param pkt packet whose data should be made reference counted.
741 * @return 0 on success, a negative AVERROR on error. On failure, the
742 * packet is unchanged.
744 int av_packet_make_refcounted(AVPacket *pkt);
747 * Create a writable reference for the data described by a given packet,
748 * avoiding data copy if possible.
750 * @param pkt Packet whose data should be made writable.
752 * @return 0 on success, a negative AVERROR on failure. On failure, the
753 * packet is unchanged.
755 int av_packet_make_writable(AVPacket *pkt);
758 * Convert valid timing fields (timestamps / durations) in a packet from one
759 * timebase to another. Timestamps with unknown values (AV_NOPTS_VALUE) will be
762 * @param pkt packet on which the conversion will be performed
763 * @param tb_src source timebase, in which the timing fields in pkt are
765 * @param tb_dst destination timebase, to which the timing fields will be
768 void av_packet_rescale_ts(AVPacket *pkt, AVRational tb_src, AVRational tb_dst);
774 #endif // AVCODEC_PACKET_H