packet.h 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870
  1. /*
  2. * AVPacket public API
  3. *
  4. * This file is part of FFmpeg.
  5. *
  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.
  10. *
  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.
  15. *
  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
  19. */
  20. #ifndef AVCODEC_PACKET_H
  21. #define AVCODEC_PACKET_H
  22. #include <stddef.h>
  23. #include <stdint.h>
  24. #include "libavutil/attributes.h"
  25. #include "libavutil/buffer.h"
  26. #include "libavutil/dict.h"
  27. #include "libavutil/rational.h"
  28. #include "libavutil/version.h"
  29. #include "libavcodec/version_major.h"
  30. /**
  31. * @defgroup lavc_packet_side_data AVPacketSideData
  32. *
  33. * Types and functions for working with AVPacketSideData.
  34. * @{
  35. */
  36. enum AVPacketSideDataType {
  37. /**
  38. * An AV_PKT_DATA_PALETTE side data packet contains exactly AVPALETTE_SIZE
  39. * bytes worth of palette. This side data signals that a new palette is
  40. * present.
  41. */
  42. AV_PKT_DATA_PALETTE,
  43. /**
  44. * The AV_PKT_DATA_NEW_EXTRADATA is used to notify the codec or the format
  45. * that the extradata buffer was changed and the receiving side should
  46. * act upon it appropriately. The new extradata is embedded in the side
  47. * data buffer and should be immediately used for processing the current
  48. * frame or packet.
  49. */
  50. AV_PKT_DATA_NEW_EXTRADATA,
  51. /**
  52. * An AV_PKT_DATA_PARAM_CHANGE side data packet is laid out as follows:
  53. * @code
  54. * u32le param_flags
  55. * if (param_flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_COUNT)
  56. * s32le channel_count
  57. * if (param_flags & AV_SIDE_DATA_PARAM_CHANGE_CHANNEL_LAYOUT)
  58. * u64le channel_layout
  59. * if (param_flags & AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE)
  60. * s32le sample_rate
  61. * if (param_flags & AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS)
  62. * s32le width
  63. * s32le height
  64. * @endcode
  65. */
  66. AV_PKT_DATA_PARAM_CHANGE,
  67. /**
  68. * An AV_PKT_DATA_H263_MB_INFO side data packet contains a number of
  69. * structures with info about macroblocks relevant to splitting the
  70. * packet into smaller packets on macroblock edges (e.g. as for RFC 2190).
  71. * That is, it does not necessarily contain info about all macroblocks,
  72. * as long as the distance between macroblocks in the info is smaller
  73. * than the target payload size.
  74. * Each MB info structure is 12 bytes, and is laid out as follows:
  75. * @code
  76. * u32le bit offset from the start of the packet
  77. * u8 current quantizer at the start of the macroblock
  78. * u8 GOB number
  79. * u16le macroblock address within the GOB
  80. * u8 horizontal MV predictor
  81. * u8 vertical MV predictor
  82. * u8 horizontal MV predictor for block number 3
  83. * u8 vertical MV predictor for block number 3
  84. * @endcode
  85. */
  86. AV_PKT_DATA_H263_MB_INFO,
  87. /**
  88. * This side data should be associated with an audio stream and contains
  89. * ReplayGain information in form of the AVReplayGain struct.
  90. */
  91. AV_PKT_DATA_REPLAYGAIN,
  92. /**
  93. * This side data contains a 3x3 transformation matrix describing an affine
  94. * transformation that needs to be applied to the decoded video frames for
  95. * correct presentation.
  96. *
  97. * See libavutil/display.h for a detailed description of the data.
  98. */
  99. AV_PKT_DATA_DISPLAYMATRIX,
  100. /**
  101. * This side data should be associated with a video stream and contains
  102. * Stereoscopic 3D information in form of the AVStereo3D struct.
  103. */
  104. AV_PKT_DATA_STEREO3D,
  105. /**
  106. * This side data should be associated with an audio stream and corresponds
  107. * to enum AVAudioServiceType.
  108. */
  109. AV_PKT_DATA_AUDIO_SERVICE_TYPE,
  110. /**
  111. * This side data contains quality related information from the encoder.
  112. * @code
  113. * u32le quality factor of the compressed frame. Allowed range is between 1 (good) and FF_LAMBDA_MAX (bad).
  114. * u8 picture type
  115. * u8 error count
  116. * u16 reserved
  117. * u64le[error count] sum of squared differences between encoder in and output
  118. * @endcode
  119. */
  120. AV_PKT_DATA_QUALITY_STATS,
  121. /**
  122. * This side data contains an integer value representing the stream index
  123. * of a "fallback" track. A fallback track indicates an alternate
  124. * track to use when the current track can not be decoded for some reason.
  125. * e.g. no decoder available for codec.
  126. */
  127. AV_PKT_DATA_FALLBACK_TRACK,
  128. /**
  129. * This side data corresponds to the AVCPBProperties struct.
  130. */
  131. AV_PKT_DATA_CPB_PROPERTIES,
  132. /**
  133. * Recommmends skipping the specified number of samples
  134. * @code
  135. * u32le number of samples to skip from start of this packet
  136. * u32le number of samples to skip from end of this packet
  137. * u8 reason for start skip
  138. * u8 reason for end skip (0=padding silence, 1=convergence)
  139. * @endcode
  140. */
  141. AV_PKT_DATA_SKIP_SAMPLES,
  142. /**
  143. * An AV_PKT_DATA_JP_DUALMONO side data packet indicates that
  144. * the packet may contain "dual mono" audio specific to Japanese DTV
  145. * and if it is true, recommends only the selected channel to be used.
  146. * @code
  147. * u8 selected channels (0=main/left, 1=sub/right, 2=both)
  148. * @endcode
  149. */
  150. AV_PKT_DATA_JP_DUALMONO,
  151. /**
  152. * A list of zero terminated key/value strings. There is no end marker for
  153. * the list, so it is required to rely on the side data size to stop.
  154. */
  155. AV_PKT_DATA_STRINGS_METADATA,
  156. /**
  157. * Subtitle event position
  158. * @code
  159. * u32le x1
  160. * u32le y1
  161. * u32le x2
  162. * u32le y2
  163. * @endcode
  164. */
  165. AV_PKT_DATA_SUBTITLE_POSITION,
  166. /**
  167. * Data found in BlockAdditional element of matroska container. There is
  168. * no end marker for the data, so it is required to rely on the side data
  169. * size to recognize the end. 8 byte id (as found in BlockAddId) followed
  170. * by data.
  171. */
  172. AV_PKT_DATA_MATROSKA_BLOCKADDITIONAL,
  173. /**
  174. * The optional first identifier line of a WebVTT cue.
  175. */
  176. AV_PKT_DATA_WEBVTT_IDENTIFIER,
  177. /**
  178. * The optional settings (rendering instructions) that immediately
  179. * follow the timestamp specifier of a WebVTT cue.
  180. */
  181. AV_PKT_DATA_WEBVTT_SETTINGS,
  182. /**
  183. * A list of zero terminated key/value strings. There is no end marker for
  184. * the list, so it is required to rely on the side data size to stop. This
  185. * side data includes updated metadata which appeared in the stream.
  186. */
  187. AV_PKT_DATA_METADATA_UPDATE,
  188. /**
  189. * MPEGTS stream ID as uint8_t, this is required to pass the stream ID
  190. * information from the demuxer to the corresponding muxer.
  191. */
  192. AV_PKT_DATA_MPEGTS_STREAM_ID,
  193. /**
  194. * Mastering display metadata (based on SMPTE-2086:2014). This metadata
  195. * should be associated with a video stream and contains data in the form
  196. * of the AVMasteringDisplayMetadata struct.
  197. */
  198. AV_PKT_DATA_MASTERING_DISPLAY_METADATA,
  199. /**
  200. * This side data should be associated with a video stream and corresponds
  201. * to the AVSphericalMapping structure.
  202. */
  203. AV_PKT_DATA_SPHERICAL,
  204. /**
  205. * Content light level (based on CTA-861.3). This metadata should be
  206. * associated with a video stream and contains data in the form of the
  207. * AVContentLightMetadata struct.
  208. */
  209. AV_PKT_DATA_CONTENT_LIGHT_LEVEL,
  210. /**
  211. * ATSC A53 Part 4 Closed Captions. This metadata should be associated with
  212. * a video stream. A53 CC bitstream is stored as uint8_t in AVPacketSideData.data.
  213. * The number of bytes of CC data is AVPacketSideData.size.
  214. */
  215. AV_PKT_DATA_A53_CC,
  216. /**
  217. * This side data is encryption initialization data.
  218. * The format is not part of ABI, use av_encryption_init_info_* methods to
  219. * access.
  220. */
  221. AV_PKT_DATA_ENCRYPTION_INIT_INFO,
  222. /**
  223. * This side data contains encryption info for how to decrypt the packet.
  224. * The format is not part of ABI, use av_encryption_info_* methods to access.
  225. */
  226. AV_PKT_DATA_ENCRYPTION_INFO,
  227. /**
  228. * Active Format Description data consisting of a single byte as specified
  229. * in ETSI TS 101 154 using AVActiveFormatDescription enum.
  230. */
  231. AV_PKT_DATA_AFD,
  232. /**
  233. * Producer Reference Time data corresponding to the AVProducerReferenceTime struct,
  234. * usually exported by some encoders (on demand through the prft flag set in the
  235. * AVCodecContext export_side_data field).
  236. */
  237. AV_PKT_DATA_PRFT,
  238. /**
  239. * ICC profile data consisting of an opaque octet buffer following the
  240. * format described by ISO 15076-1.
  241. */
  242. AV_PKT_DATA_ICC_PROFILE,
  243. /**
  244. * DOVI configuration
  245. * ref:
  246. * dolby-vision-bitstreams-within-the-iso-base-media-file-format-v2.1.2, section 2.2
  247. * dolby-vision-bitstreams-in-mpeg-2-transport-stream-multiplex-v1.2, section 3.3
  248. * Tags are stored in struct AVDOVIDecoderConfigurationRecord.
  249. */
  250. AV_PKT_DATA_DOVI_CONF,
  251. /**
  252. * Timecode which conforms to SMPTE ST 12-1:2014. The data is an array of 4 uint32_t
  253. * where the first uint32_t describes how many (1-3) of the other timecodes are used.
  254. * The timecode format is described in the documentation of av_timecode_get_smpte_from_framenum()
  255. * function in libavutil/timecode.h.
  256. */
  257. AV_PKT_DATA_S12M_TIMECODE,
  258. /**
  259. * HDR10+ dynamic metadata associated with a video frame. The metadata is in
  260. * the form of the AVDynamicHDRPlus struct and contains
  261. * information for color volume transform - application 4 of
  262. * SMPTE 2094-40:2016 standard.
  263. */
  264. AV_PKT_DATA_DYNAMIC_HDR10_PLUS,
  265. /**
  266. * IAMF Mix Gain Parameter Data associated with the audio frame. This metadata
  267. * is in the form of the AVIAMFParamDefinition struct and contains information
  268. * defined in sections 3.6.1 and 3.8.1 of the Immersive Audio Model and
  269. * Formats standard.
  270. */
  271. AV_PKT_DATA_IAMF_MIX_GAIN_PARAM,
  272. /**
  273. * IAMF Demixing Info Parameter Data associated with the audio frame. This
  274. * metadata is in the form of the AVIAMFParamDefinition struct and contains
  275. * information defined in sections 3.6.1 and 3.8.2 of the Immersive Audio Model
  276. * and Formats standard.
  277. */
  278. AV_PKT_DATA_IAMF_DEMIXING_INFO_PARAM,
  279. /**
  280. * IAMF Recon Gain Info Parameter Data associated with the audio frame. This
  281. * metadata is in the form of the AVIAMFParamDefinition struct and contains
  282. * information defined in sections 3.6.1 and 3.8.3 of the Immersive Audio Model
  283. * and Formats standard.
  284. */
  285. AV_PKT_DATA_IAMF_RECON_GAIN_INFO_PARAM,
  286. /**
  287. * Ambient viewing environment metadata, as defined by H.274. This metadata
  288. * should be associated with a video stream and contains data in the form
  289. * of the AVAmbientViewingEnvironment struct.
  290. */
  291. AV_PKT_DATA_AMBIENT_VIEWING_ENVIRONMENT,
  292. /**
  293. * The number of side data types.
  294. * This is not part of the public API/ABI in the sense that it may
  295. * change when new side data types are added.
  296. * This must stay the last enum value.
  297. * If its value becomes huge, some code using it
  298. * needs to be updated as it assumes it to be smaller than other limits.
  299. */
  300. AV_PKT_DATA_NB
  301. };
  302. #define AV_PKT_DATA_QUALITY_FACTOR AV_PKT_DATA_QUALITY_STATS //DEPRECATED
  303. /**
  304. * This structure stores auxiliary information for decoding, presenting, or
  305. * otherwise processing the coded stream. It is typically exported by demuxers
  306. * and encoders and can be fed to decoders and muxers either in a per packet
  307. * basis, or as global side data (applying to the entire coded stream).
  308. *
  309. * Global side data is handled as follows:
  310. * - During demuxing, it may be exported through
  311. * @ref AVStream.codecpar.side_data "AVStream's codec parameters", which can
  312. * then be passed as input to decoders through the
  313. * @ref AVCodecContext.coded_side_data "decoder context's side data", for
  314. * initialization.
  315. * - For muxing, it can be fed through @ref AVStream.codecpar.side_data
  316. * "AVStream's codec parameters", typically the output of encoders through
  317. * the @ref AVCodecContext.coded_side_data "encoder context's side data", for
  318. * initialization.
  319. *
  320. * Packet specific side data is handled as follows:
  321. * - During demuxing, it may be exported through @ref AVPacket.side_data
  322. * "AVPacket's side data", which can then be passed as input to decoders.
  323. * - For muxing, it can be fed through @ref AVPacket.side_data "AVPacket's
  324. * side data", typically the output of encoders.
  325. *
  326. * Different modules may accept or export different types of side data
  327. * depending on media type and codec. Refer to @ref AVPacketSideDataType for a
  328. * list of defined types and where they may be found or used.
  329. */
  330. typedef struct AVPacketSideData {
  331. uint8_t *data;
  332. size_t size;
  333. enum AVPacketSideDataType type;
  334. } AVPacketSideData;
  335. /**
  336. * Allocate a new packet side data.
  337. *
  338. * @param sd pointer to an array of side data to which the side data should
  339. * be added. *sd may be NULL, in which case the array will be
  340. * initialized.
  341. * @param nb_sd pointer to an integer containing the number of entries in
  342. * the array. The integer value will be increased by 1 on success.
  343. * @param type side data type
  344. * @param size desired side data size
  345. * @param flags currently unused. Must be zero
  346. *
  347. * @return pointer to freshly allocated side data on success, or NULL otherwise.
  348. */
  349. AVPacketSideData *av_packet_side_data_new(AVPacketSideData **psd, int *pnb_sd,
  350. enum AVPacketSideDataType type,
  351. size_t size, int flags);
  352. /**
  353. * Wrap existing data as packet side data.
  354. *
  355. * @param sd pointer to an array of side data to which the side data should
  356. * be added. *sd may be NULL, in which case the array will be
  357. * initialized
  358. * @param nb_sd pointer to an integer containing the number of entries in
  359. * the array. The integer value will be increased by 1 on success.
  360. * @param type side data type
  361. * @param data a data array. It must be allocated with the av_malloc() family
  362. * of functions. The ownership of the data is transferred to the
  363. * side data array on success
  364. * @param size size of the data array
  365. * @param flags currently unused. Must be zero
  366. *
  367. * @return pointer to freshly allocated side data on success, or NULL otherwise
  368. * On failure, the side data array is unchanged and the data remains
  369. * owned by the caller.
  370. */
  371. AVPacketSideData *av_packet_side_data_add(AVPacketSideData **sd, int *nb_sd,
  372. enum AVPacketSideDataType type,
  373. void *data, size_t size, int flags);
  374. /**
  375. * Get side information from a side data array.
  376. *
  377. * @param sd the array from which the side data should be fetched
  378. * @param nb_sd value containing the number of entries in the array.
  379. * @param type desired side information type
  380. *
  381. * @return pointer to side data if present or NULL otherwise
  382. */
  383. const AVPacketSideData *av_packet_side_data_get(const AVPacketSideData *sd,
  384. int nb_sd,
  385. enum AVPacketSideDataType type);
  386. /**
  387. * Remove side data of the given type from a side data array.
  388. *
  389. * @param sd the array from which the side data should be removed
  390. * @param nb_sd pointer to an integer containing the number of entries in
  391. * the array. Will be reduced by the amount of entries removed
  392. * upon return
  393. * @param type side information type
  394. */
  395. void av_packet_side_data_remove(AVPacketSideData *sd, int *nb_sd,
  396. enum AVPacketSideDataType type);
  397. /**
  398. * Convenience function to free all the side data stored in an array, and
  399. * the array itself.
  400. *
  401. * @param sd pointer to array of side data to free. Will be set to NULL
  402. * upon return.
  403. * @param nb_sd pointer to an integer containing the number of entries in
  404. * the array. Will be set to 0 upon return.
  405. */
  406. void av_packet_side_data_free(AVPacketSideData **sd, int *nb_sd);
  407. const char *av_packet_side_data_name(enum AVPacketSideDataType type);
  408. /**
  409. * @}
  410. */
  411. /**
  412. * @defgroup lavc_packet AVPacket
  413. *
  414. * Types and functions for working with AVPacket.
  415. * @{
  416. */
  417. /**
  418. * This structure stores compressed data. It is typically exported by demuxers
  419. * and then passed as input to decoders, or received as output from encoders and
  420. * then passed to muxers.
  421. *
  422. * For video, it should typically contain one compressed frame. For audio it may
  423. * contain several compressed frames. Encoders are allowed to output empty
  424. * packets, with no compressed data, containing only side data
  425. * (e.g. to update some stream parameters at the end of encoding).
  426. *
  427. * The semantics of data ownership depends on the buf field.
  428. * If it is set, the packet data is dynamically allocated and is
  429. * valid indefinitely until a call to av_packet_unref() reduces the
  430. * reference count to 0.
  431. *
  432. * If the buf field is not set av_packet_ref() would make a copy instead
  433. * of increasing the reference count.
  434. *
  435. * The side data is always allocated with av_malloc(), copied by
  436. * av_packet_ref() and freed by av_packet_unref().
  437. *
  438. * sizeof(AVPacket) being a part of the public ABI is deprecated. once
  439. * av_init_packet() is removed, new packets will only be able to be allocated
  440. * with av_packet_alloc(), and new fields may be added to the end of the struct
  441. * with a minor bump.
  442. *
  443. * @see av_packet_alloc
  444. * @see av_packet_ref
  445. * @see av_packet_unref
  446. */
  447. typedef struct AVPacket {
  448. /**
  449. * A reference to the reference-counted buffer where the packet data is
  450. * stored.
  451. * May be NULL, then the packet data is not reference-counted.
  452. */
  453. AVBufferRef *buf;
  454. /**
  455. * Presentation timestamp in AVStream->time_base units; the time at which
  456. * the decompressed packet will be presented to the user.
  457. * Can be AV_NOPTS_VALUE if it is not stored in the file.
  458. * pts MUST be larger or equal to dts as presentation cannot happen before
  459. * decompression, unless one wants to view hex dumps. Some formats misuse
  460. * the terms dts and pts/cts to mean something different. Such timestamps
  461. * must be converted to true pts/dts before they are stored in AVPacket.
  462. */
  463. int64_t pts;
  464. /**
  465. * Decompression timestamp in AVStream->time_base units; the time at which
  466. * the packet is decompressed.
  467. * Can be AV_NOPTS_VALUE if it is not stored in the file.
  468. */
  469. int64_t dts;
  470. uint8_t *data;
  471. int size;
  472. int stream_index;
  473. /**
  474. * A combination of AV_PKT_FLAG values
  475. */
  476. int flags;
  477. /**
  478. * Additional packet data that can be provided by the container.
  479. * Packet can contain several types of side information.
  480. */
  481. AVPacketSideData *side_data;
  482. int side_data_elems;
  483. /**
  484. * Duration of this packet in AVStream->time_base units, 0 if unknown.
  485. * Equals next_pts - this_pts in presentation order.
  486. */
  487. int64_t duration;
  488. int64_t pos; ///< byte position in stream, -1 if unknown
  489. /**
  490. * for some private data of the user
  491. */
  492. void *opaque;
  493. /**
  494. * AVBufferRef for free use by the API user. FFmpeg will never check the
  495. * contents of the buffer ref. FFmpeg calls av_buffer_unref() on it when
  496. * the packet is unreferenced. av_packet_copy_props() calls create a new
  497. * reference with av_buffer_ref() for the target packet's opaque_ref field.
  498. *
  499. * This is unrelated to the opaque field, although it serves a similar
  500. * purpose.
  501. */
  502. AVBufferRef *opaque_ref;
  503. /**
  504. * Time base of the packet's timestamps.
  505. * In the future, this field may be set on packets output by encoders or
  506. * demuxers, but its value will be by default ignored on input to decoders
  507. * or muxers.
  508. */
  509. AVRational time_base;
  510. } AVPacket;
  511. #if FF_API_INIT_PACKET
  512. attribute_deprecated
  513. typedef struct AVPacketList {
  514. AVPacket pkt;
  515. struct AVPacketList *next;
  516. } AVPacketList;
  517. #endif
  518. #define AV_PKT_FLAG_KEY 0x0001 ///< The packet contains a keyframe
  519. #define AV_PKT_FLAG_CORRUPT 0x0002 ///< The packet content is corrupted
  520. /**
  521. * Flag is used to discard packets which are required to maintain valid
  522. * decoder state but are not required for output and should be dropped
  523. * after decoding.
  524. **/
  525. #define AV_PKT_FLAG_DISCARD 0x0004
  526. /**
  527. * The packet comes from a trusted source.
  528. *
  529. * Otherwise-unsafe constructs such as arbitrary pointers to data
  530. * outside the packet may be followed.
  531. */
  532. #define AV_PKT_FLAG_TRUSTED 0x0008
  533. /**
  534. * Flag is used to indicate packets that contain frames that can
  535. * be discarded by the decoder. I.e. Non-reference frames.
  536. */
  537. #define AV_PKT_FLAG_DISPOSABLE 0x0010
  538. enum AVSideDataParamChangeFlags {
  539. AV_SIDE_DATA_PARAM_CHANGE_SAMPLE_RATE = 0x0004,
  540. AV_SIDE_DATA_PARAM_CHANGE_DIMENSIONS = 0x0008,
  541. };
  542. /**
  543. * Allocate an AVPacket and set its fields to default values. The resulting
  544. * struct must be freed using av_packet_free().
  545. *
  546. * @return An AVPacket filled with default values or NULL on failure.
  547. *
  548. * @note this only allocates the AVPacket itself, not the data buffers. Those
  549. * must be allocated through other means such as av_new_packet.
  550. *
  551. * @see av_new_packet
  552. */
  553. AVPacket *av_packet_alloc(void);
  554. /**
  555. * Create a new packet that references the same data as src.
  556. *
  557. * This is a shortcut for av_packet_alloc()+av_packet_ref().
  558. *
  559. * @return newly created AVPacket on success, NULL on error.
  560. *
  561. * @see av_packet_alloc
  562. * @see av_packet_ref
  563. */
  564. AVPacket *av_packet_clone(const AVPacket *src);
  565. /**
  566. * Free the packet, if the packet is reference counted, it will be
  567. * unreferenced first.
  568. *
  569. * @param pkt packet to be freed. The pointer will be set to NULL.
  570. * @note passing NULL is a no-op.
  571. */
  572. void av_packet_free(AVPacket **pkt);
  573. #if FF_API_INIT_PACKET
  574. /**
  575. * Initialize optional fields of a packet with default values.
  576. *
  577. * Note, this does not touch the data and size members, which have to be
  578. * initialized separately.
  579. *
  580. * @param pkt packet
  581. *
  582. * @see av_packet_alloc
  583. * @see av_packet_unref
  584. *
  585. * @deprecated This function is deprecated. Once it's removed,
  586. sizeof(AVPacket) will not be a part of the ABI anymore.
  587. */
  588. attribute_deprecated
  589. void av_init_packet(AVPacket *pkt);
  590. #endif
  591. /**
  592. * Allocate the payload of a packet and initialize its fields with
  593. * default values.
  594. *
  595. * @param pkt packet
  596. * @param size wanted payload size
  597. * @return 0 if OK, AVERROR_xxx otherwise
  598. */
  599. int av_new_packet(AVPacket *pkt, int size);
  600. /**
  601. * Reduce packet size, correctly zeroing padding
  602. *
  603. * @param pkt packet
  604. * @param size new size
  605. */
  606. void av_shrink_packet(AVPacket *pkt, int size);
  607. /**
  608. * Increase packet size, correctly zeroing padding
  609. *
  610. * @param pkt packet
  611. * @param grow_by number of bytes by which to increase the size of the packet
  612. */
  613. int av_grow_packet(AVPacket *pkt, int grow_by);
  614. /**
  615. * Initialize a reference-counted packet from av_malloc()ed data.
  616. *
  617. * @param pkt packet to be initialized. This function will set the data, size,
  618. * and buf fields, all others are left untouched.
  619. * @param data Data allocated by av_malloc() to be used as packet data. If this
  620. * function returns successfully, the data is owned by the underlying AVBuffer.
  621. * The caller may not access the data through other means.
  622. * @param size size of data in bytes, without the padding. I.e. the full buffer
  623. * size is assumed to be size + AV_INPUT_BUFFER_PADDING_SIZE.
  624. *
  625. * @return 0 on success, a negative AVERROR on error
  626. */
  627. int av_packet_from_data(AVPacket *pkt, uint8_t *data, int size);
  628. /**
  629. * Allocate new information of a packet.
  630. *
  631. * @param pkt packet
  632. * @param type side information type
  633. * @param size side information size
  634. * @return pointer to fresh allocated data or NULL otherwise
  635. */
  636. uint8_t* av_packet_new_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
  637. size_t size);
  638. /**
  639. * Wrap an existing array as a packet side data.
  640. *
  641. * @param pkt packet
  642. * @param type side information type
  643. * @param data the side data array. It must be allocated with the av_malloc()
  644. * family of functions. The ownership of the data is transferred to
  645. * pkt.
  646. * @param size side information size
  647. * @return a non-negative number on success, a negative AVERROR code on
  648. * failure. On failure, the packet is unchanged and the data remains
  649. * owned by the caller.
  650. */
  651. int av_packet_add_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
  652. uint8_t *data, size_t size);
  653. /**
  654. * Shrink the already allocated side data buffer
  655. *
  656. * @param pkt packet
  657. * @param type side information type
  658. * @param size new side information size
  659. * @return 0 on success, < 0 on failure
  660. */
  661. int av_packet_shrink_side_data(AVPacket *pkt, enum AVPacketSideDataType type,
  662. size_t size);
  663. /**
  664. * Get side information from packet.
  665. *
  666. * @param pkt packet
  667. * @param type desired side information type
  668. * @param size If supplied, *size will be set to the size of the side data
  669. * or to zero if the desired side data is not present.
  670. * @return pointer to data if present or NULL otherwise
  671. */
  672. uint8_t* av_packet_get_side_data(const AVPacket *pkt, enum AVPacketSideDataType type,
  673. size_t *size);
  674. /**
  675. * Pack a dictionary for use in side_data.
  676. *
  677. * @param dict The dictionary to pack.
  678. * @param size pointer to store the size of the returned data
  679. * @return pointer to data if successful, NULL otherwise
  680. */
  681. uint8_t *av_packet_pack_dictionary(AVDictionary *dict, size_t *size);
  682. /**
  683. * Unpack a dictionary from side_data.
  684. *
  685. * @param data data from side_data
  686. * @param size size of the data
  687. * @param dict the metadata storage dictionary
  688. * @return 0 on success, < 0 on failure
  689. */
  690. int av_packet_unpack_dictionary(const uint8_t *data, size_t size,
  691. AVDictionary **dict);
  692. /**
  693. * Convenience function to free all the side data stored.
  694. * All the other fields stay untouched.
  695. *
  696. * @param pkt packet
  697. */
  698. void av_packet_free_side_data(AVPacket *pkt);
  699. /**
  700. * Setup a new reference to the data described by a given packet
  701. *
  702. * If src is reference-counted, setup dst as a new reference to the
  703. * buffer in src. Otherwise allocate a new buffer in dst and copy the
  704. * data from src into it.
  705. *
  706. * All the other fields are copied from src.
  707. *
  708. * @see av_packet_unref
  709. *
  710. * @param dst Destination packet. Will be completely overwritten.
  711. * @param src Source packet
  712. *
  713. * @return 0 on success, a negative AVERROR on error. On error, dst
  714. * will be blank (as if returned by av_packet_alloc()).
  715. */
  716. int av_packet_ref(AVPacket *dst, const AVPacket *src);
  717. /**
  718. * Wipe the packet.
  719. *
  720. * Unreference the buffer referenced by the packet and reset the
  721. * remaining packet fields to their default values.
  722. *
  723. * @param pkt The packet to be unreferenced.
  724. */
  725. void av_packet_unref(AVPacket *pkt);
  726. /**
  727. * Move every field in src to dst and reset src.
  728. *
  729. * @see av_packet_unref
  730. *
  731. * @param src Source packet, will be reset
  732. * @param dst Destination packet
  733. */
  734. void av_packet_move_ref(AVPacket *dst, AVPacket *src);
  735. /**
  736. * Copy only "properties" fields from src to dst.
  737. *
  738. * Properties for the purpose of this function are all the fields
  739. * beside those related to the packet data (buf, data, size)
  740. *
  741. * @param dst Destination packet
  742. * @param src Source packet
  743. *
  744. * @return 0 on success AVERROR on failure.
  745. */
  746. int av_packet_copy_props(AVPacket *dst, const AVPacket *src);
  747. /**
  748. * Ensure the data described by a given packet is reference counted.
  749. *
  750. * @note This function does not ensure that the reference will be writable.
  751. * Use av_packet_make_writable instead for that purpose.
  752. *
  753. * @see av_packet_ref
  754. * @see av_packet_make_writable
  755. *
  756. * @param pkt packet whose data should be made reference counted.
  757. *
  758. * @return 0 on success, a negative AVERROR on error. On failure, the
  759. * packet is unchanged.
  760. */
  761. int av_packet_make_refcounted(AVPacket *pkt);
  762. /**
  763. * Create a writable reference for the data described by a given packet,
  764. * avoiding data copy if possible.
  765. *
  766. * @param pkt Packet whose data should be made writable.
  767. *
  768. * @return 0 on success, a negative AVERROR on failure. On failure, the
  769. * packet is unchanged.
  770. */
  771. int av_packet_make_writable(AVPacket *pkt);
  772. /**
  773. * Convert valid timing fields (timestamps / durations) in a packet from one
  774. * timebase to another. Timestamps with unknown values (AV_NOPTS_VALUE) will be
  775. * ignored.
  776. *
  777. * @param pkt packet on which the conversion will be performed
  778. * @param tb_src source timebase, in which the timing fields in pkt are
  779. * expressed
  780. * @param tb_dst destination timebase, to which the timing fields will be
  781. * converted
  782. */
  783. void av_packet_rescale_ts(AVPacket *pkt, AVRational tb_src, AVRational tb_dst);
  784. /**
  785. * @}
  786. */
  787. #endif // AVCODEC_PACKET_H