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.

651 lines
20KB

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