You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

679 lines
21KB

  1. /*
  2. *
  3. * This file is part of FFmpeg.
  4. *
  5. * FFmpeg is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU Lesser General Public
  7. * License as published by the Free Software Foundation; either
  8. * version 2.1 of the License, or (at your option) any later version.
  9. *
  10. * FFmpeg is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * Lesser General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Lesser General Public
  16. * License along with FFmpeg; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. #ifndef AVUTIL_FRAME_H
  20. #define AVUTIL_FRAME_H
  21. #include <stdint.h>
  22. #include "libavcodec/version.h"
  23. #include "avutil.h"
  24. #include "buffer.h"
  25. #include "dict.h"
  26. #include "rational.h"
  27. #include "samplefmt.h"
  28. enum AVColorSpace{
  29. AVCOL_SPC_RGB = 0,
  30. AVCOL_SPC_BT709 = 1, ///< also ITU-R BT1361 / IEC 61966-2-4 xvYCC709 / SMPTE RP177 Annex B
  31. AVCOL_SPC_UNSPECIFIED = 2,
  32. AVCOL_SPC_FCC = 4,
  33. AVCOL_SPC_BT470BG = 5, ///< also ITU-R BT601-6 625 / ITU-R BT1358 625 / ITU-R BT1700 625 PAL & SECAM / IEC 61966-2-4 xvYCC601
  34. AVCOL_SPC_SMPTE170M = 6, ///< also ITU-R BT601-6 525 / ITU-R BT1358 525 / ITU-R BT1700 NTSC / functionally identical to above
  35. AVCOL_SPC_SMPTE240M = 7,
  36. AVCOL_SPC_YCOCG = 8, ///< Used by Dirac / VC-2 and H.264 FRext, see ITU-T SG16
  37. AVCOL_SPC_BT2020_NCL = 9, ///< ITU-R BT2020 non-constant luminance system
  38. AVCOL_SPC_BT2020_CL = 10, ///< ITU-R BT2020 constant luminance system
  39. AVCOL_SPC_NB , ///< Not part of ABI
  40. };
  41. #define AVCOL_SPC_YCGCO AVCOL_SPC_YCOCG
  42. enum AVColorRange{
  43. AVCOL_RANGE_UNSPECIFIED = 0,
  44. AVCOL_RANGE_MPEG = 1, ///< the normal 219*2^(n-8) "MPEG" YUV ranges
  45. AVCOL_RANGE_JPEG = 2, ///< the normal 2^n-1 "JPEG" YUV ranges
  46. AVCOL_RANGE_NB , ///< Not part of ABI
  47. };
  48. enum AVFrameSideDataType {
  49. /**
  50. * The data is the AVPanScan struct defined in libavcodec.
  51. */
  52. AV_FRAME_DATA_PANSCAN,
  53. /**
  54. * ATSC A53 Part 4 Closed Captions.
  55. * A53 CC bitstream is stored as uint8_t in AVFrameSideData.data.
  56. * The number of bytes of CC data is AVFrameSideData.size.
  57. */
  58. AV_FRAME_DATA_A53_CC,
  59. };
  60. typedef struct AVFrameSideData {
  61. enum AVFrameSideDataType type;
  62. uint8_t *data;
  63. int size;
  64. AVDictionary *metadata;
  65. } AVFrameSideData;
  66. /**
  67. * This structure describes decoded (raw) audio or video data.
  68. *
  69. * AVFrame must be allocated using av_frame_alloc(). Note that this only
  70. * allocates the AVFrame itself, the buffers for the data must be managed
  71. * through other means (see below).
  72. * AVFrame must be freed with av_frame_free().
  73. *
  74. * AVFrame is typically allocated once and then reused multiple times to hold
  75. * different data (e.g. a single AVFrame to hold frames received from a
  76. * decoder). In such a case, av_frame_unref() will free any references held by
  77. * the frame and reset it to its original clean state before it
  78. * is reused again.
  79. *
  80. * The data described by an AVFrame is usually reference counted through the
  81. * AVBuffer API. The underlying buffer references are stored in AVFrame.buf /
  82. * AVFrame.extended_buf. An AVFrame is considered to be reference counted if at
  83. * least one reference is set, i.e. if AVFrame.buf[0] != NULL. In such a case,
  84. * every single data plane must be contained in one of the buffers in
  85. * AVFrame.buf or AVFrame.extended_buf.
  86. * There may be a single buffer for all the data, or one separate buffer for
  87. * each plane, or anything in between.
  88. *
  89. * sizeof(AVFrame) is not a part of the public ABI, so new fields may be added
  90. * to the end with a minor bump.
  91. * Similarly fields that are marked as to be only accessed by
  92. * av_opt_ptr() can be reordered. This allows 2 forks to add fields
  93. * without breaking compatibility with each other.
  94. */
  95. typedef struct AVFrame {
  96. #define AV_NUM_DATA_POINTERS 8
  97. /**
  98. * pointer to the picture/channel planes.
  99. * This might be different from the first allocated byte
  100. *
  101. * Some decoders access areas outside 0,0 - width,height, please
  102. * see avcodec_align_dimensions2(). Some filters and swscale can read
  103. * up to 16 bytes beyond the planes, if these filters are to be used,
  104. * then 16 extra bytes must be allocated.
  105. */
  106. uint8_t *data[AV_NUM_DATA_POINTERS];
  107. /**
  108. * For video, size in bytes of each picture line.
  109. * For audio, size in bytes of each plane.
  110. *
  111. * For audio, only linesize[0] may be set. For planar audio, each channel
  112. * plane must be the same size.
  113. *
  114. * For video the linesizes should be multiplies of the CPUs alignment
  115. * preference, this is 16 or 32 for modern desktop CPUs.
  116. * Some code requires such alignment other code can be slower without
  117. * correct alignment, for yet other it makes no difference.
  118. *
  119. * @note The linesize may be larger than the size of usable data -- there
  120. * may be extra padding present for performance reasons.
  121. */
  122. int linesize[AV_NUM_DATA_POINTERS];
  123. /**
  124. * pointers to the data planes/channels.
  125. *
  126. * For video, this should simply point to data[].
  127. *
  128. * For planar audio, each channel has a separate data pointer, and
  129. * linesize[0] contains the size of each channel buffer.
  130. * For packed audio, there is just one data pointer, and linesize[0]
  131. * contains the total size of the buffer for all channels.
  132. *
  133. * Note: Both data and extended_data should always be set in a valid frame,
  134. * but for planar audio with more channels that can fit in data,
  135. * extended_data must be used in order to access all channels.
  136. */
  137. uint8_t **extended_data;
  138. /**
  139. * width and height of the video frame
  140. */
  141. int width, height;
  142. /**
  143. * number of audio samples (per channel) described by this frame
  144. */
  145. int nb_samples;
  146. /**
  147. * format of the frame, -1 if unknown or unset
  148. * Values correspond to enum AVPixelFormat for video frames,
  149. * enum AVSampleFormat for audio)
  150. */
  151. int format;
  152. /**
  153. * 1 -> keyframe, 0-> not
  154. */
  155. int key_frame;
  156. /**
  157. * Picture type of the frame.
  158. */
  159. enum AVPictureType pict_type;
  160. #if FF_API_AVFRAME_LAVC
  161. attribute_deprecated
  162. uint8_t *base[AV_NUM_DATA_POINTERS];
  163. #endif
  164. /**
  165. * Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
  166. */
  167. AVRational sample_aspect_ratio;
  168. /**
  169. * Presentation timestamp in time_base units (time when frame should be shown to user).
  170. */
  171. int64_t pts;
  172. /**
  173. * PTS copied from the AVPacket that was decoded to produce this frame.
  174. */
  175. int64_t pkt_pts;
  176. /**
  177. * DTS copied from the AVPacket that triggered returning this frame. (if frame threading isnt used)
  178. * This is also the Presentation time of this AVFrame calculated from
  179. * only AVPacket.dts values without pts values.
  180. */
  181. int64_t pkt_dts;
  182. /**
  183. * picture number in bitstream order
  184. */
  185. int coded_picture_number;
  186. /**
  187. * picture number in display order
  188. */
  189. int display_picture_number;
  190. /**
  191. * quality (between 1 (good) and FF_LAMBDA_MAX (bad))
  192. */
  193. int quality;
  194. #if FF_API_AVFRAME_LAVC
  195. attribute_deprecated
  196. int reference;
  197. /**
  198. * QP table
  199. */
  200. attribute_deprecated
  201. int8_t *qscale_table;
  202. /**
  203. * QP store stride
  204. */
  205. attribute_deprecated
  206. int qstride;
  207. attribute_deprecated
  208. int qscale_type;
  209. /**
  210. * mbskip_table[mb]>=1 if MB didn't change
  211. * stride= mb_width = (width+15)>>4
  212. */
  213. attribute_deprecated
  214. uint8_t *mbskip_table;
  215. /**
  216. * motion vector table
  217. * @code
  218. * example:
  219. * int mv_sample_log2= 4 - motion_subsample_log2;
  220. * int mb_width= (width+15)>>4;
  221. * int mv_stride= (mb_width << mv_sample_log2) + 1;
  222. * motion_val[direction][x + y*mv_stride][0->mv_x, 1->mv_y];
  223. * @endcode
  224. */
  225. attribute_deprecated
  226. int16_t (*motion_val[2])[2];
  227. /**
  228. * macroblock type table
  229. * mb_type_base + mb_width + 2
  230. */
  231. attribute_deprecated
  232. uint32_t *mb_type;
  233. /**
  234. * DCT coefficients
  235. */
  236. attribute_deprecated
  237. short *dct_coeff;
  238. /**
  239. * motion reference frame index
  240. * the order in which these are stored can depend on the codec.
  241. */
  242. attribute_deprecated
  243. int8_t *ref_index[2];
  244. #endif
  245. /**
  246. * for some private data of the user
  247. */
  248. void *opaque;
  249. /**
  250. * error
  251. */
  252. uint64_t error[AV_NUM_DATA_POINTERS];
  253. #if FF_API_AVFRAME_LAVC
  254. attribute_deprecated
  255. int type;
  256. #endif
  257. /**
  258. * When decoding, this signals how much the picture must be delayed.
  259. * extra_delay = repeat_pict / (2*fps)
  260. */
  261. int repeat_pict;
  262. /**
  263. * The content of the picture is interlaced.
  264. */
  265. int interlaced_frame;
  266. /**
  267. * If the content is interlaced, is top field displayed first.
  268. */
  269. int top_field_first;
  270. /**
  271. * Tell user application that palette has changed from previous frame.
  272. */
  273. int palette_has_changed;
  274. #if FF_API_AVFRAME_LAVC
  275. attribute_deprecated
  276. int buffer_hints;
  277. /**
  278. * Pan scan.
  279. */
  280. attribute_deprecated
  281. struct AVPanScan *pan_scan;
  282. #endif
  283. /**
  284. * reordered opaque 64bit (generally an integer or a double precision float
  285. * PTS but can be anything).
  286. * The user sets AVCodecContext.reordered_opaque to represent the input at
  287. * that time,
  288. * the decoder reorders values as needed and sets AVFrame.reordered_opaque
  289. * to exactly one of the values provided by the user through AVCodecContext.reordered_opaque
  290. * @deprecated in favor of pkt_pts
  291. */
  292. int64_t reordered_opaque;
  293. #if FF_API_AVFRAME_LAVC
  294. /**
  295. * @deprecated this field is unused
  296. */
  297. attribute_deprecated void *hwaccel_picture_private;
  298. attribute_deprecated
  299. struct AVCodecContext *owner;
  300. attribute_deprecated
  301. void *thread_opaque;
  302. /**
  303. * log2 of the size of the block which a single vector in motion_val represents:
  304. * (4->16x16, 3->8x8, 2-> 4x4, 1-> 2x2)
  305. */
  306. attribute_deprecated
  307. uint8_t motion_subsample_log2;
  308. #endif
  309. /**
  310. * Sample rate of the audio data.
  311. */
  312. int sample_rate;
  313. /**
  314. * Channel layout of the audio data.
  315. */
  316. uint64_t channel_layout;
  317. /**
  318. * AVBuffer references backing the data for this frame. If all elements of
  319. * this array are NULL, then this frame is not reference counted.
  320. *
  321. * There may be at most one AVBuffer per data plane, so for video this array
  322. * always contains all the references. For planar audio with more than
  323. * AV_NUM_DATA_POINTERS channels, there may be more buffers than can fit in
  324. * this array. Then the extra AVBufferRef pointers are stored in the
  325. * extended_buf array.
  326. */
  327. AVBufferRef *buf[AV_NUM_DATA_POINTERS];
  328. /**
  329. * For planar audio which requires more than AV_NUM_DATA_POINTERS
  330. * AVBufferRef pointers, this array will hold all the references which
  331. * cannot fit into AVFrame.buf.
  332. *
  333. * Note that this is different from AVFrame.extended_data, which always
  334. * contains all the pointers. This array only contains the extra pointers,
  335. * which cannot fit into AVFrame.buf.
  336. *
  337. * This array is always allocated using av_malloc() by whoever constructs
  338. * the frame. It is freed in av_frame_unref().
  339. */
  340. AVBufferRef **extended_buf;
  341. /**
  342. * Number of elements in extended_buf.
  343. */
  344. int nb_extended_buf;
  345. AVFrameSideData **side_data;
  346. int nb_side_data;
  347. /**
  348. * The frame data may be corrupted, e.g. due to decoding errors.
  349. */
  350. #define AV_FRAME_FLAG_CORRUPT (1 << 0)
  351. /**
  352. * Frame flags, a combination of AV_FRAME_FLAG_*
  353. */
  354. int flags;
  355. /**
  356. * frame timestamp estimated using various heuristics, in stream time base
  357. * Code outside libavcodec should access this field using:
  358. * av_frame_get_best_effort_timestamp(frame)
  359. * - encoding: unused
  360. * - decoding: set by libavcodec, read by user.
  361. */
  362. int64_t best_effort_timestamp;
  363. /**
  364. * reordered pos from the last AVPacket that has been input into the decoder
  365. * Code outside libavcodec should access this field using:
  366. * av_frame_get_pkt_pos(frame)
  367. * - encoding: unused
  368. * - decoding: Read by user.
  369. */
  370. int64_t pkt_pos;
  371. /**
  372. * duration of the corresponding packet, expressed in
  373. * AVStream->time_base units, 0 if unknown.
  374. * Code outside libavcodec should access this field using:
  375. * av_frame_get_pkt_duration(frame)
  376. * - encoding: unused
  377. * - decoding: Read by user.
  378. */
  379. int64_t pkt_duration;
  380. /**
  381. * metadata.
  382. * Code outside libavcodec should access this field using:
  383. * av_frame_get_metadata(frame)
  384. * - encoding: Set by user.
  385. * - decoding: Set by libavcodec.
  386. */
  387. AVDictionary *metadata;
  388. /**
  389. * decode error flags of the frame, set to a combination of
  390. * FF_DECODE_ERROR_xxx flags if the decoder produced a frame, but there
  391. * were errors during the decoding.
  392. * Code outside libavcodec should access this field using:
  393. * av_frame_get_decode_error_flags(frame)
  394. * - encoding: unused
  395. * - decoding: set by libavcodec, read by user.
  396. */
  397. int decode_error_flags;
  398. #define FF_DECODE_ERROR_INVALID_BITSTREAM 1
  399. #define FF_DECODE_ERROR_MISSING_REFERENCE 2
  400. /**
  401. * number of audio channels, only used for audio.
  402. * Code outside libavcodec should access this field using:
  403. * av_frame_get_channels(frame)
  404. * - encoding: unused
  405. * - decoding: Read by user.
  406. */
  407. int channels;
  408. /**
  409. * size of the corresponding packet containing the compressed
  410. * frame. It must be accessed using av_frame_get_pkt_size() and
  411. * av_frame_set_pkt_size().
  412. * It is set to a negative value if unknown.
  413. * - encoding: unused
  414. * - decoding: set by libavcodec, read by user.
  415. */
  416. int pkt_size;
  417. /**
  418. * YUV colorspace type.
  419. * It must be accessed using av_frame_get_colorspace() and
  420. * av_frame_set_colorspace().
  421. * - encoding: Set by user
  422. * - decoding: Set by libavcodec
  423. */
  424. enum AVColorSpace colorspace;
  425. /**
  426. * MPEG vs JPEG YUV range.
  427. * It must be accessed using av_frame_get_color_range() and
  428. * av_frame_set_color_range().
  429. * - encoding: Set by user
  430. * - decoding: Set by libavcodec
  431. */
  432. enum AVColorRange color_range;
  433. /**
  434. * Not to be accessed directly from outside libavutil
  435. */
  436. AVBufferRef *qp_table_buf;
  437. } AVFrame;
  438. /**
  439. * Accessors for some AVFrame fields.
  440. * The position of these field in the structure is not part of the ABI,
  441. * they should not be accessed directly outside libavcodec.
  442. */
  443. int64_t av_frame_get_best_effort_timestamp(const AVFrame *frame);
  444. void av_frame_set_best_effort_timestamp(AVFrame *frame, int64_t val);
  445. int64_t av_frame_get_pkt_duration (const AVFrame *frame);
  446. void av_frame_set_pkt_duration (AVFrame *frame, int64_t val);
  447. int64_t av_frame_get_pkt_pos (const AVFrame *frame);
  448. void av_frame_set_pkt_pos (AVFrame *frame, int64_t val);
  449. int64_t av_frame_get_channel_layout (const AVFrame *frame);
  450. void av_frame_set_channel_layout (AVFrame *frame, int64_t val);
  451. int av_frame_get_channels (const AVFrame *frame);
  452. void av_frame_set_channels (AVFrame *frame, int val);
  453. int av_frame_get_sample_rate (const AVFrame *frame);
  454. void av_frame_set_sample_rate (AVFrame *frame, int val);
  455. AVDictionary *av_frame_get_metadata (const AVFrame *frame);
  456. void av_frame_set_metadata (AVFrame *frame, AVDictionary *val);
  457. int av_frame_get_decode_error_flags (const AVFrame *frame);
  458. void av_frame_set_decode_error_flags (AVFrame *frame, int val);
  459. int av_frame_get_pkt_size(const AVFrame *frame);
  460. void av_frame_set_pkt_size(AVFrame *frame, int val);
  461. AVDictionary **avpriv_frame_get_metadatap(AVFrame *frame);
  462. int8_t *av_frame_get_qp_table(AVFrame *f, int *stride, int *type);
  463. int av_frame_set_qp_table(AVFrame *f, AVBufferRef *buf, int stride, int type);
  464. enum AVColorSpace av_frame_get_colorspace(const AVFrame *frame);
  465. void av_frame_set_colorspace(AVFrame *frame, enum AVColorSpace val);
  466. enum AVColorRange av_frame_get_color_range(const AVFrame *frame);
  467. void av_frame_set_color_range(AVFrame *frame, enum AVColorRange val);
  468. /**
  469. * Get the name of a colorspace.
  470. * @return a static string identifying the colorspace; can be NULL.
  471. */
  472. const char *av_get_colorspace_name(enum AVColorSpace val);
  473. /**
  474. * Allocate an AVFrame and set its fields to default values. The resulting
  475. * struct must be freed using av_frame_free().
  476. *
  477. * @return An AVFrame filled with default values or NULL on failure.
  478. *
  479. * @note this only allocates the AVFrame itself, not the data buffers. Those
  480. * must be allocated through other means, e.g. with av_frame_get_buffer() or
  481. * manually.
  482. */
  483. AVFrame *av_frame_alloc(void);
  484. /**
  485. * Free the frame and any dynamically allocated objects in it,
  486. * e.g. extended_data. If the frame is reference counted, it will be
  487. * unreferenced first.
  488. *
  489. * @param frame frame to be freed. The pointer will be set to NULL.
  490. */
  491. void av_frame_free(AVFrame **frame);
  492. /**
  493. * Setup a new reference to the data described by a given frame.
  494. *
  495. * Copy frame properties from src to dst and create a new reference for each
  496. * AVBufferRef from src.
  497. *
  498. * If src is not reference counted, new buffers are allocated and the data is
  499. * copied.
  500. *
  501. * @return 0 on success, a negative AVERROR on error
  502. */
  503. int av_frame_ref(AVFrame *dst, const AVFrame *src);
  504. /**
  505. * Create a new frame that references the same data as src.
  506. *
  507. * This is a shortcut for av_frame_alloc()+av_frame_ref().
  508. *
  509. * @return newly created AVFrame on success, NULL on error.
  510. */
  511. AVFrame *av_frame_clone(const AVFrame *src);
  512. /**
  513. * Unreference all the buffers referenced by frame and reset the frame fields.
  514. */
  515. void av_frame_unref(AVFrame *frame);
  516. /**
  517. * Move everythnig contained in src to dst and reset src.
  518. */
  519. void av_frame_move_ref(AVFrame *dst, AVFrame *src);
  520. /**
  521. * Allocate new buffer(s) for audio or video data.
  522. *
  523. * The following fields must be set on frame before calling this function:
  524. * - format (pixel format for video, sample format for audio)
  525. * - width and height for video
  526. * - nb_samples and channel_layout for audio
  527. *
  528. * This function will fill AVFrame.data and AVFrame.buf arrays and, if
  529. * necessary, allocate and fill AVFrame.extended_data and AVFrame.extended_buf.
  530. * For planar formats, one buffer will be allocated for each plane.
  531. *
  532. * @param frame frame in which to store the new buffers.
  533. * @param align required buffer size alignment
  534. *
  535. * @return 0 on success, a negative AVERROR on error.
  536. */
  537. int av_frame_get_buffer(AVFrame *frame, int align);
  538. /**
  539. * Check if the frame data is writable.
  540. *
  541. * @return A positive value if the frame data is writable (which is true if and
  542. * only if each of the underlying buffers has only one reference, namely the one
  543. * stored in this frame). Return 0 otherwise.
  544. *
  545. * If 1 is returned the answer is valid until av_buffer_ref() is called on any
  546. * of the underlying AVBufferRefs (e.g. through av_frame_ref() or directly).
  547. *
  548. * @see av_frame_make_writable(), av_buffer_is_writable()
  549. */
  550. int av_frame_is_writable(AVFrame *frame);
  551. /**
  552. * Ensure that the frame data is writable, avoiding data copy if possible.
  553. *
  554. * Do nothing if the frame is writable, allocate new buffers and copy the data
  555. * if it is not.
  556. *
  557. * @return 0 on success, a negative AVERROR on error.
  558. *
  559. * @see av_frame_is_writable(), av_buffer_is_writable(),
  560. * av_buffer_make_writable()
  561. */
  562. int av_frame_make_writable(AVFrame *frame);
  563. /**
  564. * Copy only "metadata" fields from src to dst.
  565. *
  566. * Metadata for the purpose of this function are those fields that do not affect
  567. * the data layout in the buffers. E.g. pts, sample rate (for audio) or sample
  568. * aspect ratio (for video), but not width/height or channel layout.
  569. * Side data is also copied.
  570. */
  571. int av_frame_copy_props(AVFrame *dst, const AVFrame *src);
  572. /**
  573. * Get the buffer reference a given data plane is stored in.
  574. *
  575. * @param plane index of the data plane of interest in frame->extended_data.
  576. *
  577. * @return the buffer reference that contains the plane or NULL if the input
  578. * frame is not valid.
  579. */
  580. AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane);
  581. /**
  582. * Add a new side data to a frame.
  583. *
  584. * @param frame a frame to which the side data should be added
  585. * @param type type of the added side data
  586. * @param size size of the side data
  587. *
  588. * @return newly added side data on success, NULL on error
  589. */
  590. AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
  591. enum AVFrameSideDataType type,
  592. int size);
  593. /**
  594. * @return a pointer to the side data of a given type on success, NULL if there
  595. * is no side data with such type in this frame.
  596. */
  597. AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
  598. enum AVFrameSideDataType type);
  599. #endif /* AVUTIL_FRAME_H */