frame.h 37 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. /**
  19. * @file
  20. * @ingroup lavu_frame
  21. * reference-counted frame API
  22. */
  23. #ifndef AVUTIL_FRAME_H
  24. #define AVUTIL_FRAME_H
  25. #include <stddef.h>
  26. #include <stdint.h>
  27. #include "avutil.h"
  28. #include "buffer.h"
  29. #include "channel_layout.h"
  30. #include "dict.h"
  31. #include "rational.h"
  32. #include "samplefmt.h"
  33. #include "pixfmt.h"
  34. #include "version.h"
  35. /**
  36. * @defgroup lavu_frame AVFrame
  37. * @ingroup lavu_data
  38. *
  39. * @{
  40. * AVFrame is an abstraction for reference-counted raw multimedia data.
  41. */
  42. enum AVFrameSideDataType {
  43. /**
  44. * The data is the AVPanScan struct defined in libavcodec.
  45. */
  46. AV_FRAME_DATA_PANSCAN,
  47. /**
  48. * ATSC A53 Part 4 Closed Captions.
  49. * A53 CC bitstream is stored as uint8_t in AVFrameSideData.data.
  50. * The number of bytes of CC data is AVFrameSideData.size.
  51. */
  52. AV_FRAME_DATA_A53_CC,
  53. /**
  54. * Stereoscopic 3d metadata.
  55. * The data is the AVStereo3D struct defined in libavutil/stereo3d.h.
  56. */
  57. AV_FRAME_DATA_STEREO3D,
  58. /**
  59. * The data is the AVMatrixEncoding enum defined in libavutil/channel_layout.h.
  60. */
  61. AV_FRAME_DATA_MATRIXENCODING,
  62. /**
  63. * Metadata relevant to a downmix procedure.
  64. * The data is the AVDownmixInfo struct defined in libavutil/downmix_info.h.
  65. */
  66. AV_FRAME_DATA_DOWNMIX_INFO,
  67. /**
  68. * ReplayGain information in the form of the AVReplayGain struct.
  69. */
  70. AV_FRAME_DATA_REPLAYGAIN,
  71. /**
  72. * This side data contains a 3x3 transformation matrix describing an affine
  73. * transformation that needs to be applied to the frame for correct
  74. * presentation.
  75. *
  76. * See libavutil/display.h for a detailed description of the data.
  77. */
  78. AV_FRAME_DATA_DISPLAYMATRIX,
  79. /**
  80. * Active Format Description data consisting of a single byte as specified
  81. * in ETSI TS 101 154 using AVActiveFormatDescription enum.
  82. */
  83. AV_FRAME_DATA_AFD,
  84. /**
  85. * Motion vectors exported by some codecs (on demand through the export_mvs
  86. * flag set in the libavcodec AVCodecContext flags2 option).
  87. * The data is the AVMotionVector struct defined in
  88. * libavutil/motion_vector.h.
  89. */
  90. AV_FRAME_DATA_MOTION_VECTORS,
  91. /**
  92. * Recommmends skipping the specified number of samples. This is exported
  93. * only if the "skip_manual" AVOption is set in libavcodec.
  94. * This has the same format as AV_PKT_DATA_SKIP_SAMPLES.
  95. * @code
  96. * u32le number of samples to skip from start of this packet
  97. * u32le number of samples to skip from end of this packet
  98. * u8 reason for start skip
  99. * u8 reason for end skip (0=padding silence, 1=convergence)
  100. * @endcode
  101. */
  102. AV_FRAME_DATA_SKIP_SAMPLES,
  103. /**
  104. * This side data must be associated with an audio frame and corresponds to
  105. * enum AVAudioServiceType defined in avcodec.h.
  106. */
  107. AV_FRAME_DATA_AUDIO_SERVICE_TYPE,
  108. /**
  109. * Mastering display metadata associated with a video frame. The payload is
  110. * an AVMasteringDisplayMetadata type and contains information about the
  111. * mastering display color volume.
  112. */
  113. AV_FRAME_DATA_MASTERING_DISPLAY_METADATA,
  114. /**
  115. * The GOP timecode in 25 bit timecode format. Data format is 64-bit integer.
  116. * This is set on the first frame of a GOP that has a temporal reference of 0.
  117. */
  118. AV_FRAME_DATA_GOP_TIMECODE,
  119. /**
  120. * The data represents the AVSphericalMapping structure defined in
  121. * libavutil/spherical.h.
  122. */
  123. AV_FRAME_DATA_SPHERICAL,
  124. /**
  125. * Content light level (based on CTA-861.3). This payload contains data in
  126. * the form of the AVContentLightMetadata struct.
  127. */
  128. AV_FRAME_DATA_CONTENT_LIGHT_LEVEL,
  129. /**
  130. * The data contains an ICC profile as an opaque octet buffer following the
  131. * format described by ISO 15076-1 with an optional name defined in the
  132. * metadata key entry "name".
  133. */
  134. AV_FRAME_DATA_ICC_PROFILE,
  135. /**
  136. * Timecode which conforms to SMPTE ST 12-1. The data is an array of 4 uint32_t
  137. * where the first uint32_t describes how many (1-3) of the other timecodes are used.
  138. * The timecode format is described in the documentation of av_timecode_get_smpte_from_framenum()
  139. * function in libavutil/timecode.h.
  140. */
  141. AV_FRAME_DATA_S12M_TIMECODE,
  142. /**
  143. * HDR dynamic metadata associated with a video frame. The payload is
  144. * an AVDynamicHDRPlus type and contains information for color
  145. * volume transform - application 4 of SMPTE 2094-40:2016 standard.
  146. */
  147. AV_FRAME_DATA_DYNAMIC_HDR_PLUS,
  148. /**
  149. * Regions Of Interest, the data is an array of AVRegionOfInterest type, the number of
  150. * array element is implied by AVFrameSideData.size / AVRegionOfInterest.self_size.
  151. */
  152. AV_FRAME_DATA_REGIONS_OF_INTEREST,
  153. /**
  154. * Encoding parameters for a video frame, as described by AVVideoEncParams.
  155. */
  156. AV_FRAME_DATA_VIDEO_ENC_PARAMS,
  157. /**
  158. * User data unregistered metadata associated with a video frame.
  159. * This is the H.26[45] UDU SEI message, and shouldn't be used for any other purpose
  160. * The data is stored as uint8_t in AVFrameSideData.data which is 16 bytes of
  161. * uuid_iso_iec_11578 followed by AVFrameSideData.size - 16 bytes of user_data_payload_byte.
  162. */
  163. AV_FRAME_DATA_SEI_UNREGISTERED,
  164. /**
  165. * Film grain parameters for a frame, described by AVFilmGrainParams.
  166. * Must be present for every frame which should have film grain applied.
  167. *
  168. * May be present multiple times, for example when there are multiple
  169. * alternative parameter sets for different video signal characteristics.
  170. * The user should select the most appropriate set for the application.
  171. */
  172. AV_FRAME_DATA_FILM_GRAIN_PARAMS,
  173. /**
  174. * Bounding boxes for object detection and classification,
  175. * as described by AVDetectionBBoxHeader.
  176. */
  177. AV_FRAME_DATA_DETECTION_BBOXES,
  178. /**
  179. * Dolby Vision RPU raw data, suitable for passing to x265
  180. * or other libraries. Array of uint8_t, with NAL emulation
  181. * bytes intact.
  182. */
  183. AV_FRAME_DATA_DOVI_RPU_BUFFER,
  184. /**
  185. * Parsed Dolby Vision metadata, suitable for passing to a software
  186. * implementation. The payload is the AVDOVIMetadata struct defined in
  187. * libavutil/dovi_meta.h.
  188. */
  189. AV_FRAME_DATA_DOVI_METADATA,
  190. /**
  191. * HDR Vivid dynamic metadata associated with a video frame. The payload is
  192. * an AVDynamicHDRVivid type and contains information for color
  193. * volume transform - CUVA 005.1-2021.
  194. */
  195. AV_FRAME_DATA_DYNAMIC_HDR_VIVID,
  196. /**
  197. * Ambient viewing environment metadata, as defined by H.274.
  198. */
  199. AV_FRAME_DATA_AMBIENT_VIEWING_ENVIRONMENT,
  200. /**
  201. * Provide encoder-specific hinting information about changed/unchanged
  202. * portions of a frame. It can be used to pass information about which
  203. * macroblocks can be skipped because they didn't change from the
  204. * corresponding ones in the previous frame. This could be useful for
  205. * applications which know this information in advance to speed up
  206. * encoding.
  207. */
  208. AV_FRAME_DATA_VIDEO_HINT,
  209. };
  210. enum AVActiveFormatDescription {
  211. AV_AFD_SAME = 8,
  212. AV_AFD_4_3 = 9,
  213. AV_AFD_16_9 = 10,
  214. AV_AFD_14_9 = 11,
  215. AV_AFD_4_3_SP_14_9 = 13,
  216. AV_AFD_16_9_SP_14_9 = 14,
  217. AV_AFD_SP_4_3 = 15,
  218. };
  219. /**
  220. * Structure to hold side data for an AVFrame.
  221. *
  222. * sizeof(AVFrameSideData) is not a part of the public ABI, so new fields may be added
  223. * to the end with a minor bump.
  224. */
  225. typedef struct AVFrameSideData {
  226. enum AVFrameSideDataType type;
  227. uint8_t *data;
  228. size_t size;
  229. AVDictionary *metadata;
  230. AVBufferRef *buf;
  231. } AVFrameSideData;
  232. /**
  233. * Structure describing a single Region Of Interest.
  234. *
  235. * When multiple regions are defined in a single side-data block, they
  236. * should be ordered from most to least important - some encoders are only
  237. * capable of supporting a limited number of distinct regions, so will have
  238. * to truncate the list.
  239. *
  240. * When overlapping regions are defined, the first region containing a given
  241. * area of the frame applies.
  242. */
  243. typedef struct AVRegionOfInterest {
  244. /**
  245. * Must be set to the size of this data structure (that is,
  246. * sizeof(AVRegionOfInterest)).
  247. */
  248. uint32_t self_size;
  249. /**
  250. * Distance in pixels from the top edge of the frame to the top and
  251. * bottom edges and from the left edge of the frame to the left and
  252. * right edges of the rectangle defining this region of interest.
  253. *
  254. * The constraints on a region are encoder dependent, so the region
  255. * actually affected may be slightly larger for alignment or other
  256. * reasons.
  257. */
  258. int top;
  259. int bottom;
  260. int left;
  261. int right;
  262. /**
  263. * Quantisation offset.
  264. *
  265. * Must be in the range -1 to +1. A value of zero indicates no quality
  266. * change. A negative value asks for better quality (less quantisation),
  267. * while a positive value asks for worse quality (greater quantisation).
  268. *
  269. * The range is calibrated so that the extreme values indicate the
  270. * largest possible offset - if the rest of the frame is encoded with the
  271. * worst possible quality, an offset of -1 indicates that this region
  272. * should be encoded with the best possible quality anyway. Intermediate
  273. * values are then interpolated in some codec-dependent way.
  274. *
  275. * For example, in 10-bit H.264 the quantisation parameter varies between
  276. * -12 and 51. A typical qoffset value of -1/10 therefore indicates that
  277. * this region should be encoded with a QP around one-tenth of the full
  278. * range better than the rest of the frame. So, if most of the frame
  279. * were to be encoded with a QP of around 30, this region would get a QP
  280. * of around 24 (an offset of approximately -1/10 * (51 - -12) = -6.3).
  281. * An extreme value of -1 would indicate that this region should be
  282. * encoded with the best possible quality regardless of the treatment of
  283. * the rest of the frame - that is, should be encoded at a QP of -12.
  284. */
  285. AVRational qoffset;
  286. } AVRegionOfInterest;
  287. /**
  288. * This structure describes decoded (raw) audio or video data.
  289. *
  290. * AVFrame must be allocated using av_frame_alloc(). Note that this only
  291. * allocates the AVFrame itself, the buffers for the data must be managed
  292. * through other means (see below).
  293. * AVFrame must be freed with av_frame_free().
  294. *
  295. * AVFrame is typically allocated once and then reused multiple times to hold
  296. * different data (e.g. a single AVFrame to hold frames received from a
  297. * decoder). In such a case, av_frame_unref() will free any references held by
  298. * the frame and reset it to its original clean state before it
  299. * is reused again.
  300. *
  301. * The data described by an AVFrame is usually reference counted through the
  302. * AVBuffer API. The underlying buffer references are stored in AVFrame.buf /
  303. * AVFrame.extended_buf. An AVFrame is considered to be reference counted if at
  304. * least one reference is set, i.e. if AVFrame.buf[0] != NULL. In such a case,
  305. * every single data plane must be contained in one of the buffers in
  306. * AVFrame.buf or AVFrame.extended_buf.
  307. * There may be a single buffer for all the data, or one separate buffer for
  308. * each plane, or anything in between.
  309. *
  310. * sizeof(AVFrame) is not a part of the public ABI, so new fields may be added
  311. * to the end with a minor bump.
  312. *
  313. * Fields can be accessed through AVOptions, the name string used, matches the
  314. * C structure field name for fields accessible through AVOptions. The AVClass
  315. * for AVFrame can be obtained from avcodec_get_frame_class()
  316. */
  317. typedef struct AVFrame {
  318. #define AV_NUM_DATA_POINTERS 8
  319. /**
  320. * pointer to the picture/channel planes.
  321. * This might be different from the first allocated byte. For video,
  322. * it could even point to the end of the image data.
  323. *
  324. * All pointers in data and extended_data must point into one of the
  325. * AVBufferRef in buf or extended_buf.
  326. *
  327. * Some decoders access areas outside 0,0 - width,height, please
  328. * see avcodec_align_dimensions2(). Some filters and swscale can read
  329. * up to 16 bytes beyond the planes, if these filters are to be used,
  330. * then 16 extra bytes must be allocated.
  331. *
  332. * NOTE: Pointers not needed by the format MUST be set to NULL.
  333. *
  334. * @attention In case of video, the data[] pointers can point to the
  335. * end of image data in order to reverse line order, when used in
  336. * combination with negative values in the linesize[] array.
  337. */
  338. uint8_t *data[AV_NUM_DATA_POINTERS];
  339. /**
  340. * For video, a positive or negative value, which is typically indicating
  341. * the size in bytes of each picture line, but it can also be:
  342. * - the negative byte size of lines for vertical flipping
  343. * (with data[n] pointing to the end of the data
  344. * - a positive or negative multiple of the byte size as for accessing
  345. * even and odd fields of a frame (possibly flipped)
  346. *
  347. * For audio, only linesize[0] may be set. For planar audio, each channel
  348. * plane must be the same size.
  349. *
  350. * For video the linesizes should be multiples of the CPUs alignment
  351. * preference, this is 16 or 32 for modern desktop CPUs.
  352. * Some code requires such alignment other code can be slower without
  353. * correct alignment, for yet other it makes no difference.
  354. *
  355. * @note The linesize may be larger than the size of usable data -- there
  356. * may be extra padding present for performance reasons.
  357. *
  358. * @attention In case of video, line size values can be negative to achieve
  359. * a vertically inverted iteration over image lines.
  360. */
  361. int linesize[AV_NUM_DATA_POINTERS];
  362. /**
  363. * pointers to the data planes/channels.
  364. *
  365. * For video, this should simply point to data[].
  366. *
  367. * For planar audio, each channel has a separate data pointer, and
  368. * linesize[0] contains the size of each channel buffer.
  369. * For packed audio, there is just one data pointer, and linesize[0]
  370. * contains the total size of the buffer for all channels.
  371. *
  372. * Note: Both data and extended_data should always be set in a valid frame,
  373. * but for planar audio with more channels that can fit in data,
  374. * extended_data must be used in order to access all channels.
  375. */
  376. uint8_t **extended_data;
  377. /**
  378. * @name Video dimensions
  379. * Video frames only. The coded dimensions (in pixels) of the video frame,
  380. * i.e. the size of the rectangle that contains some well-defined values.
  381. *
  382. * @note The part of the frame intended for display/presentation is further
  383. * restricted by the @ref cropping "Cropping rectangle".
  384. * @{
  385. */
  386. int width, height;
  387. /**
  388. * @}
  389. */
  390. /**
  391. * number of audio samples (per channel) described by this frame
  392. */
  393. int nb_samples;
  394. /**
  395. * format of the frame, -1 if unknown or unset
  396. * Values correspond to enum AVPixelFormat for video frames,
  397. * enum AVSampleFormat for audio)
  398. */
  399. int format;
  400. #if FF_API_FRAME_KEY
  401. /**
  402. * 1 -> keyframe, 0-> not
  403. *
  404. * @deprecated Use AV_FRAME_FLAG_KEY instead
  405. */
  406. attribute_deprecated
  407. int key_frame;
  408. #endif
  409. /**
  410. * Picture type of the frame.
  411. */
  412. enum AVPictureType pict_type;
  413. /**
  414. * Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
  415. */
  416. AVRational sample_aspect_ratio;
  417. /**
  418. * Presentation timestamp in time_base units (time when frame should be shown to user).
  419. */
  420. int64_t pts;
  421. /**
  422. * DTS copied from the AVPacket that triggered returning this frame. (if frame threading isn't used)
  423. * This is also the Presentation time of this AVFrame calculated from
  424. * only AVPacket.dts values without pts values.
  425. */
  426. int64_t pkt_dts;
  427. /**
  428. * Time base for the timestamps in this frame.
  429. * In the future, this field may be set on frames output by decoders or
  430. * filters, but its value will be by default ignored on input to encoders
  431. * or filters.
  432. */
  433. AVRational time_base;
  434. /**
  435. * quality (between 1 (good) and FF_LAMBDA_MAX (bad))
  436. */
  437. int quality;
  438. /**
  439. * Frame owner's private data.
  440. *
  441. * This field may be set by the code that allocates/owns the frame data.
  442. * It is then not touched by any library functions, except:
  443. * - it is copied to other references by av_frame_copy_props() (and hence by
  444. * av_frame_ref());
  445. * - it is set to NULL when the frame is cleared by av_frame_unref()
  446. * - on the caller's explicit request. E.g. libavcodec encoders/decoders
  447. * will copy this field to/from @ref AVPacket "AVPackets" if the caller sets
  448. * @ref AV_CODEC_FLAG_COPY_OPAQUE.
  449. *
  450. * @see opaque_ref the reference-counted analogue
  451. */
  452. void *opaque;
  453. /**
  454. * Number of fields in this frame which should be repeated, i.e. the total
  455. * duration of this frame should be repeat_pict + 2 normal field durations.
  456. *
  457. * For interlaced frames this field may be set to 1, which signals that this
  458. * frame should be presented as 3 fields: beginning with the first field (as
  459. * determined by AV_FRAME_FLAG_TOP_FIELD_FIRST being set or not), followed
  460. * by the second field, and then the first field again.
  461. *
  462. * For progressive frames this field may be set to a multiple of 2, which
  463. * signals that this frame's duration should be (repeat_pict + 2) / 2
  464. * normal frame durations.
  465. *
  466. * @note This field is computed from MPEG2 repeat_first_field flag and its
  467. * associated flags, H.264 pic_struct from picture timing SEI, and
  468. * their analogues in other codecs. Typically it should only be used when
  469. * higher-layer timing information is not available.
  470. */
  471. int repeat_pict;
  472. #if FF_API_INTERLACED_FRAME
  473. /**
  474. * The content of the picture is interlaced.
  475. *
  476. * @deprecated Use AV_FRAME_FLAG_INTERLACED instead
  477. */
  478. attribute_deprecated
  479. int interlaced_frame;
  480. /**
  481. * If the content is interlaced, is top field displayed first.
  482. *
  483. * @deprecated Use AV_FRAME_FLAG_TOP_FIELD_FIRST instead
  484. */
  485. attribute_deprecated
  486. int top_field_first;
  487. #endif
  488. #if FF_API_PALETTE_HAS_CHANGED
  489. /**
  490. * Tell user application that palette has changed from previous frame.
  491. */
  492. attribute_deprecated
  493. int palette_has_changed;
  494. #endif
  495. /**
  496. * Sample rate of the audio data.
  497. */
  498. int sample_rate;
  499. /**
  500. * AVBuffer references backing the data for this frame. All the pointers in
  501. * data and extended_data must point inside one of the buffers in buf or
  502. * extended_buf. This array must be filled contiguously -- if buf[i] is
  503. * non-NULL then buf[j] must also be non-NULL for all j < i.
  504. *
  505. * There may be at most one AVBuffer per data plane, so for video this array
  506. * always contains all the references. For planar audio with more than
  507. * AV_NUM_DATA_POINTERS channels, there may be more buffers than can fit in
  508. * this array. Then the extra AVBufferRef pointers are stored in the
  509. * extended_buf array.
  510. */
  511. AVBufferRef *buf[AV_NUM_DATA_POINTERS];
  512. /**
  513. * For planar audio which requires more than AV_NUM_DATA_POINTERS
  514. * AVBufferRef pointers, this array will hold all the references which
  515. * cannot fit into AVFrame.buf.
  516. *
  517. * Note that this is different from AVFrame.extended_data, which always
  518. * contains all the pointers. This array only contains the extra pointers,
  519. * which cannot fit into AVFrame.buf.
  520. *
  521. * This array is always allocated using av_malloc() by whoever constructs
  522. * the frame. It is freed in av_frame_unref().
  523. */
  524. AVBufferRef **extended_buf;
  525. /**
  526. * Number of elements in extended_buf.
  527. */
  528. int nb_extended_buf;
  529. AVFrameSideData **side_data;
  530. int nb_side_data;
  531. /**
  532. * @defgroup lavu_frame_flags AV_FRAME_FLAGS
  533. * @ingroup lavu_frame
  534. * Flags describing additional frame properties.
  535. *
  536. * @{
  537. */
  538. /**
  539. * The frame data may be corrupted, e.g. due to decoding errors.
  540. */
  541. #define AV_FRAME_FLAG_CORRUPT (1 << 0)
  542. /**
  543. * A flag to mark frames that are keyframes.
  544. */
  545. #define AV_FRAME_FLAG_KEY (1 << 1)
  546. /**
  547. * A flag to mark the frames which need to be decoded, but shouldn't be output.
  548. */
  549. #define AV_FRAME_FLAG_DISCARD (1 << 2)
  550. /**
  551. * A flag to mark frames whose content is interlaced.
  552. */
  553. #define AV_FRAME_FLAG_INTERLACED (1 << 3)
  554. /**
  555. * A flag to mark frames where the top field is displayed first if the content
  556. * is interlaced.
  557. */
  558. #define AV_FRAME_FLAG_TOP_FIELD_FIRST (1 << 4)
  559. /**
  560. * @}
  561. */
  562. /**
  563. * Frame flags, a combination of @ref lavu_frame_flags
  564. */
  565. int flags;
  566. /**
  567. * MPEG vs JPEG YUV range.
  568. * - encoding: Set by user
  569. * - decoding: Set by libavcodec
  570. */
  571. enum AVColorRange color_range;
  572. enum AVColorPrimaries color_primaries;
  573. enum AVColorTransferCharacteristic color_trc;
  574. /**
  575. * YUV colorspace type.
  576. * - encoding: Set by user
  577. * - decoding: Set by libavcodec
  578. */
  579. enum AVColorSpace colorspace;
  580. enum AVChromaLocation chroma_location;
  581. /**
  582. * frame timestamp estimated using various heuristics, in stream time base
  583. * - encoding: unused
  584. * - decoding: set by libavcodec, read by user.
  585. */
  586. int64_t best_effort_timestamp;
  587. #if FF_API_FRAME_PKT
  588. /**
  589. * reordered pos from the last AVPacket that has been input into the decoder
  590. * - encoding: unused
  591. * - decoding: Read by user.
  592. * @deprecated use AV_CODEC_FLAG_COPY_OPAQUE to pass through arbitrary user
  593. * data from packets to frames
  594. */
  595. attribute_deprecated
  596. int64_t pkt_pos;
  597. #endif
  598. /**
  599. * metadata.
  600. * - encoding: Set by user.
  601. * - decoding: Set by libavcodec.
  602. */
  603. AVDictionary *metadata;
  604. /**
  605. * decode error flags of the frame, set to a combination of
  606. * FF_DECODE_ERROR_xxx flags if the decoder produced a frame, but there
  607. * were errors during the decoding.
  608. * - encoding: unused
  609. * - decoding: set by libavcodec, read by user.
  610. */
  611. int decode_error_flags;
  612. #define FF_DECODE_ERROR_INVALID_BITSTREAM 1
  613. #define FF_DECODE_ERROR_MISSING_REFERENCE 2
  614. #define FF_DECODE_ERROR_CONCEALMENT_ACTIVE 4
  615. #define FF_DECODE_ERROR_DECODE_SLICES 8
  616. #if FF_API_FRAME_PKT
  617. /**
  618. * size of the corresponding packet containing the compressed
  619. * frame.
  620. * It is set to a negative value if unknown.
  621. * - encoding: unused
  622. * - decoding: set by libavcodec, read by user.
  623. * @deprecated use AV_CODEC_FLAG_COPY_OPAQUE to pass through arbitrary user
  624. * data from packets to frames
  625. */
  626. attribute_deprecated
  627. int pkt_size;
  628. #endif
  629. /**
  630. * For hwaccel-format frames, this should be a reference to the
  631. * AVHWFramesContext describing the frame.
  632. */
  633. AVBufferRef *hw_frames_ctx;
  634. /**
  635. * Frame owner's private data.
  636. *
  637. * This field may be set by the code that allocates/owns the frame data.
  638. * It is then not touched by any library functions, except:
  639. * - a new reference to the underlying buffer is propagated by
  640. * av_frame_copy_props() (and hence by av_frame_ref());
  641. * - it is unreferenced in av_frame_unref();
  642. * - on the caller's explicit request. E.g. libavcodec encoders/decoders
  643. * will propagate a new reference to/from @ref AVPacket "AVPackets" if the
  644. * caller sets @ref AV_CODEC_FLAG_COPY_OPAQUE.
  645. *
  646. * @see opaque the plain pointer analogue
  647. */
  648. AVBufferRef *opaque_ref;
  649. /**
  650. * @anchor cropping
  651. * @name Cropping
  652. * Video frames only. The number of pixels to discard from the the
  653. * top/bottom/left/right border of the frame to obtain the sub-rectangle of
  654. * the frame intended for presentation.
  655. * @{
  656. */
  657. size_t crop_top;
  658. size_t crop_bottom;
  659. size_t crop_left;
  660. size_t crop_right;
  661. /**
  662. * @}
  663. */
  664. /**
  665. * AVBufferRef for internal use by a single libav* library.
  666. * Must not be used to transfer data between libraries.
  667. * Has to be NULL when ownership of the frame leaves the respective library.
  668. *
  669. * Code outside the FFmpeg libs should never check or change the contents of the buffer ref.
  670. *
  671. * FFmpeg calls av_buffer_unref() on it when the frame is unreferenced.
  672. * av_frame_copy_props() calls create a new reference with av_buffer_ref()
  673. * for the target frame's private_ref field.
  674. */
  675. AVBufferRef *private_ref;
  676. /**
  677. * Channel layout of the audio data.
  678. */
  679. AVChannelLayout ch_layout;
  680. /**
  681. * Duration of the frame, in the same units as pts. 0 if unknown.
  682. */
  683. int64_t duration;
  684. } AVFrame;
  685. /**
  686. * Allocate an AVFrame and set its fields to default values. The resulting
  687. * struct must be freed using av_frame_free().
  688. *
  689. * @return An AVFrame filled with default values or NULL on failure.
  690. *
  691. * @note this only allocates the AVFrame itself, not the data buffers. Those
  692. * must be allocated through other means, e.g. with av_frame_get_buffer() or
  693. * manually.
  694. */
  695. AVFrame *av_frame_alloc(void);
  696. /**
  697. * Free the frame and any dynamically allocated objects in it,
  698. * e.g. extended_data. If the frame is reference counted, it will be
  699. * unreferenced first.
  700. *
  701. * @param frame frame to be freed. The pointer will be set to NULL.
  702. */
  703. void av_frame_free(AVFrame **frame);
  704. /**
  705. * Set up a new reference to the data described by the source frame.
  706. *
  707. * Copy frame properties from src to dst and create a new reference for each
  708. * AVBufferRef from src.
  709. *
  710. * If src is not reference counted, new buffers are allocated and the data is
  711. * copied.
  712. *
  713. * @warning: dst MUST have been either unreferenced with av_frame_unref(dst),
  714. * or newly allocated with av_frame_alloc() before calling this
  715. * function, or undefined behavior will occur.
  716. *
  717. * @return 0 on success, a negative AVERROR on error
  718. */
  719. int av_frame_ref(AVFrame *dst, const AVFrame *src);
  720. /**
  721. * Ensure the destination frame refers to the same data described by the source
  722. * frame, either by creating a new reference for each AVBufferRef from src if
  723. * they differ from those in dst, by allocating new buffers and copying data if
  724. * src is not reference counted, or by unrefencing it if src is empty.
  725. *
  726. * Frame properties on dst will be replaced by those from src.
  727. *
  728. * @return 0 on success, a negative AVERROR on error. On error, dst is
  729. * unreferenced.
  730. */
  731. int av_frame_replace(AVFrame *dst, const AVFrame *src);
  732. /**
  733. * Create a new frame that references the same data as src.
  734. *
  735. * This is a shortcut for av_frame_alloc()+av_frame_ref().
  736. *
  737. * @return newly created AVFrame on success, NULL on error.
  738. */
  739. AVFrame *av_frame_clone(const AVFrame *src);
  740. /**
  741. * Unreference all the buffers referenced by frame and reset the frame fields.
  742. */
  743. void av_frame_unref(AVFrame *frame);
  744. /**
  745. * Move everything contained in src to dst and reset src.
  746. *
  747. * @warning: dst is not unreferenced, but directly overwritten without reading
  748. * or deallocating its contents. Call av_frame_unref(dst) manually
  749. * before calling this function to ensure that no memory is leaked.
  750. */
  751. void av_frame_move_ref(AVFrame *dst, AVFrame *src);
  752. /**
  753. * Allocate new buffer(s) for audio or video data.
  754. *
  755. * The following fields must be set on frame before calling this function:
  756. * - format (pixel format for video, sample format for audio)
  757. * - width and height for video
  758. * - nb_samples and ch_layout for audio
  759. *
  760. * This function will fill AVFrame.data and AVFrame.buf arrays and, if
  761. * necessary, allocate and fill AVFrame.extended_data and AVFrame.extended_buf.
  762. * For planar formats, one buffer will be allocated for each plane.
  763. *
  764. * @warning: if frame already has been allocated, calling this function will
  765. * leak memory. In addition, undefined behavior can occur in certain
  766. * cases.
  767. *
  768. * @param frame frame in which to store the new buffers.
  769. * @param align Required buffer size alignment. If equal to 0, alignment will be
  770. * chosen automatically for the current CPU. It is highly
  771. * recommended to pass 0 here unless you know what you are doing.
  772. *
  773. * @return 0 on success, a negative AVERROR on error.
  774. */
  775. int av_frame_get_buffer(AVFrame *frame, int align);
  776. /**
  777. * Check if the frame data is writable.
  778. *
  779. * @return A positive value if the frame data is writable (which is true if and
  780. * only if each of the underlying buffers has only one reference, namely the one
  781. * stored in this frame). Return 0 otherwise.
  782. *
  783. * If 1 is returned the answer is valid until av_buffer_ref() is called on any
  784. * of the underlying AVBufferRefs (e.g. through av_frame_ref() or directly).
  785. *
  786. * @see av_frame_make_writable(), av_buffer_is_writable()
  787. */
  788. int av_frame_is_writable(AVFrame *frame);
  789. /**
  790. * Ensure that the frame data is writable, avoiding data copy if possible.
  791. *
  792. * Do nothing if the frame is writable, allocate new buffers and copy the data
  793. * if it is not. Non-refcounted frames behave as non-writable, i.e. a copy
  794. * is always made.
  795. *
  796. * @return 0 on success, a negative AVERROR on error.
  797. *
  798. * @see av_frame_is_writable(), av_buffer_is_writable(),
  799. * av_buffer_make_writable()
  800. */
  801. int av_frame_make_writable(AVFrame *frame);
  802. /**
  803. * Copy the frame data from src to dst.
  804. *
  805. * This function does not allocate anything, dst must be already initialized and
  806. * allocated with the same parameters as src.
  807. *
  808. * This function only copies the frame data (i.e. the contents of the data /
  809. * extended data arrays), not any other properties.
  810. *
  811. * @return >= 0 on success, a negative AVERROR on error.
  812. */
  813. int av_frame_copy(AVFrame *dst, const AVFrame *src);
  814. /**
  815. * Copy only "metadata" fields from src to dst.
  816. *
  817. * Metadata for the purpose of this function are those fields that do not affect
  818. * the data layout in the buffers. E.g. pts, sample rate (for audio) or sample
  819. * aspect ratio (for video), but not width/height or channel layout.
  820. * Side data is also copied.
  821. */
  822. int av_frame_copy_props(AVFrame *dst, const AVFrame *src);
  823. /**
  824. * Get the buffer reference a given data plane is stored in.
  825. *
  826. * @param frame the frame to get the plane's buffer from
  827. * @param plane index of the data plane of interest in frame->extended_data.
  828. *
  829. * @return the buffer reference that contains the plane or NULL if the input
  830. * frame is not valid.
  831. */
  832. AVBufferRef *av_frame_get_plane_buffer(const AVFrame *frame, int plane);
  833. /**
  834. * Add a new side data to a frame.
  835. *
  836. * @param frame a frame to which the side data should be added
  837. * @param type type of the added side data
  838. * @param size size of the side data
  839. *
  840. * @return newly added side data on success, NULL on error
  841. */
  842. AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
  843. enum AVFrameSideDataType type,
  844. size_t size);
  845. /**
  846. * Add a new side data to a frame from an existing AVBufferRef
  847. *
  848. * @param frame a frame to which the side data should be added
  849. * @param type the type of the added side data
  850. * @param buf an AVBufferRef to add as side data. The ownership of
  851. * the reference is transferred to the frame.
  852. *
  853. * @return newly added side data on success, NULL on error. On failure
  854. * the frame is unchanged and the AVBufferRef remains owned by
  855. * the caller.
  856. */
  857. AVFrameSideData *av_frame_new_side_data_from_buf(AVFrame *frame,
  858. enum AVFrameSideDataType type,
  859. AVBufferRef *buf);
  860. /**
  861. * @return a pointer to the side data of a given type on success, NULL if there
  862. * is no side data with such type in this frame.
  863. */
  864. AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
  865. enum AVFrameSideDataType type);
  866. /**
  867. * Remove and free all side data instances of the given type.
  868. */
  869. void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type);
  870. /**
  871. * Flags for frame cropping.
  872. */
  873. enum {
  874. /**
  875. * Apply the maximum possible cropping, even if it requires setting the
  876. * AVFrame.data[] entries to unaligned pointers. Passing unaligned data
  877. * to FFmpeg API is generally not allowed, and causes undefined behavior
  878. * (such as crashes). You can pass unaligned data only to FFmpeg APIs that
  879. * are explicitly documented to accept it. Use this flag only if you
  880. * absolutely know what you are doing.
  881. */
  882. AV_FRAME_CROP_UNALIGNED = 1 << 0,
  883. };
  884. /**
  885. * Crop the given video AVFrame according to its crop_left/crop_top/crop_right/
  886. * crop_bottom fields. If cropping is successful, the function will adjust the
  887. * data pointers and the width/height fields, and set the crop fields to 0.
  888. *
  889. * In all cases, the cropping boundaries will be rounded to the inherent
  890. * alignment of the pixel format. In some cases, such as for opaque hwaccel
  891. * formats, the left/top cropping is ignored. The crop fields are set to 0 even
  892. * if the cropping was rounded or ignored.
  893. *
  894. * @param frame the frame which should be cropped
  895. * @param flags Some combination of AV_FRAME_CROP_* flags, or 0.
  896. *
  897. * @return >= 0 on success, a negative AVERROR on error. If the cropping fields
  898. * were invalid, AVERROR(ERANGE) is returned, and nothing is changed.
  899. */
  900. int av_frame_apply_cropping(AVFrame *frame, int flags);
  901. /**
  902. * @return a string identifying the side data type
  903. */
  904. const char *av_frame_side_data_name(enum AVFrameSideDataType type);
  905. /**
  906. * Free all side data entries and their contents, then zeroes out the
  907. * values which the pointers are pointing to.
  908. *
  909. * @param sd pointer to array of side data to free. Will be set to NULL
  910. * upon return.
  911. * @param nb_sd pointer to an integer containing the number of entries in
  912. * the array. Will be set to 0 upon return.
  913. */
  914. void av_frame_side_data_free(AVFrameSideData ***sd, int *nb_sd);
  915. #define AV_FRAME_SIDE_DATA_FLAG_UNIQUE (1 << 0)
  916. /**
  917. * Add new side data entry to an array.
  918. *
  919. * @param sd pointer to array of side data to which to add another entry,
  920. * or to NULL in order to start a new array.
  921. * @param nb_sd pointer to an integer containing the number of entries in
  922. * the array.
  923. * @param type type of the added side data
  924. * @param size size of the side data
  925. * @param flags Some combination of AV_FRAME_SIDE_DATA_FLAG_* flags, or 0.
  926. *
  927. * @return newly added side data on success, NULL on error. In case of
  928. * AV_FRAME_SIDE_DATA_FLAG_UNIQUE being set, entries of matching
  929. * AVFrameSideDataType will be removed before the addition is
  930. * attempted.
  931. */
  932. AVFrameSideData *av_frame_side_data_new(AVFrameSideData ***sd, int *nb_sd,
  933. enum AVFrameSideDataType type,
  934. size_t size, unsigned int flags);
  935. /**
  936. * Add a new side data entry to an array based on existing side data, taking
  937. * a reference towards the contained AVBufferRef.
  938. *
  939. * @param sd pointer to array of side data to which to add another entry,
  940. * or to NULL in order to start a new array.
  941. * @param nb_sd pointer to an integer containing the number of entries in
  942. * the array.
  943. * @param src side data to be cloned, with a new reference utilized
  944. * for the buffer.
  945. * @param flags Some combination of AV_FRAME_SIDE_DATA_FLAG_* flags, or 0.
  946. *
  947. * @return negative error code on failure, >=0 on success. In case of
  948. * AV_FRAME_SIDE_DATA_FLAG_UNIQUE being set, entries of matching
  949. * AVFrameSideDataType will be removed before the addition is
  950. * attempted.
  951. */
  952. int av_frame_side_data_clone(AVFrameSideData ***sd, int *nb_sd,
  953. const AVFrameSideData *src, unsigned int flags);
  954. /**
  955. * Get a side data entry of a specific type from an array.
  956. *
  957. * @param sd array of side data.
  958. * @param nb_sd integer containing the number of entries in the array.
  959. * @param type type of side data to be queried
  960. *
  961. * @return a pointer to the side data of a given type on success, NULL if there
  962. * is no side data with such type in this set.
  963. */
  964. const AVFrameSideData *av_frame_side_data_get_c(const AVFrameSideData * const *sd,
  965. const int nb_sd,
  966. enum AVFrameSideDataType type);
  967. /**
  968. * Wrapper around av_frame_side_data_get_c() to workaround the limitation
  969. * that for any type T the conversion from T * const * to const T * const *
  970. * is not performed automatically in C.
  971. * @see av_frame_side_data_get_c()
  972. */
  973. static inline
  974. const AVFrameSideData *av_frame_side_data_get(AVFrameSideData * const *sd,
  975. const int nb_sd,
  976. enum AVFrameSideDataType type)
  977. {
  978. return av_frame_side_data_get_c((const AVFrameSideData * const *)sd,
  979. nb_sd, type);
  980. }
  981. /**
  982. * @}
  983. */
  984. #endif /* AVUTIL_FRAME_H */