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.

576 lines
16KB

  1. /*
  2. *
  3. * This file is part of Libav.
  4. *
  5. * Libav 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. * Libav 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 Libav; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18. */
  19. /**
  20. * @file
  21. * @ingroup lavu_frame
  22. * reference-counted frame API
  23. */
  24. #ifndef AVUTIL_FRAME_H
  25. #define AVUTIL_FRAME_H
  26. #include <stdint.h>
  27. #include "avutil.h"
  28. #include "buffer.h"
  29. #include "dict.h"
  30. #include "rational.h"
  31. #include "samplefmt.h"
  32. #include "version.h"
  33. /**
  34. * @defgroup lavu_frame AVFrame
  35. * @ingroup lavu_data
  36. *
  37. * @{
  38. * AVFrame is an abstraction for reference-counted raw multimedia data.
  39. */
  40. enum AVFrameSideDataType {
  41. /**
  42. * The data is the AVPanScan struct defined in libavcodec.
  43. */
  44. AV_FRAME_DATA_PANSCAN,
  45. /**
  46. * ATSC A53 Part 4 Closed Captions.
  47. * A53 CC bitstream is stored as uint8_t in AVFrameSideData.data.
  48. * The number of bytes of CC data is AVFrameSideData.size.
  49. */
  50. AV_FRAME_DATA_A53_CC,
  51. /**
  52. * Stereoscopic 3d metadata.
  53. * The data is the AVStereo3D struct defined in libavutil/stereo3d.h.
  54. */
  55. AV_FRAME_DATA_STEREO3D,
  56. /**
  57. * The data is the AVMatrixEncoding enum defined in libavutil/channel_layout.h.
  58. */
  59. AV_FRAME_DATA_MATRIXENCODING,
  60. /**
  61. * Metadata relevant to a downmix procedure.
  62. * The data is the AVDownmixInfo struct defined in libavutil/downmix_info.h.
  63. */
  64. AV_FRAME_DATA_DOWNMIX_INFO,
  65. /**
  66. * ReplayGain information in the form of the AVReplayGain struct.
  67. */
  68. AV_FRAME_DATA_REPLAYGAIN,
  69. };
  70. typedef struct AVFrameSideData {
  71. enum AVFrameSideDataType type;
  72. uint8_t *data;
  73. int size;
  74. AVDictionary *metadata;
  75. } AVFrameSideData;
  76. /**
  77. * This structure describes decoded (raw) audio or video data.
  78. *
  79. * AVFrame must be allocated using av_frame_alloc(). Note that this only
  80. * allocates the AVFrame itself, the buffers for the data must be managed
  81. * through other means (see below).
  82. * AVFrame must be freed with av_frame_free().
  83. *
  84. * AVFrame is typically allocated once and then reused multiple times to hold
  85. * different data (e.g. a single AVFrame to hold frames received from a
  86. * decoder). In such a case, av_frame_unref() will free any references held by
  87. * the frame and reset it to its original clean state before it
  88. * is reused again.
  89. *
  90. * The data described by an AVFrame is usually reference counted through the
  91. * AVBuffer API. The underlying buffer references are stored in AVFrame.buf /
  92. * AVFrame.extended_buf. An AVFrame is considered to be reference counted if at
  93. * least one reference is set, i.e. if AVFrame.buf[0] != NULL. In such a case,
  94. * every single data plane must be contained in one of the buffers in
  95. * AVFrame.buf or AVFrame.extended_buf.
  96. * There may be a single buffer for all the data, or one separate buffer for
  97. * each plane, or anything in between.
  98. *
  99. * sizeof(AVFrame) is not a part of the public ABI, so new fields may be added
  100. * to the end with a minor bump.
  101. */
  102. typedef struct AVFrame {
  103. #define AV_NUM_DATA_POINTERS 8
  104. /**
  105. * pointer to the picture/channel planes.
  106. * This might be different from the first allocated byte
  107. */
  108. uint8_t *data[AV_NUM_DATA_POINTERS];
  109. /**
  110. * For video, size in bytes of each picture line.
  111. * For audio, size in bytes of each plane.
  112. *
  113. * For audio, only linesize[0] may be set. For planar audio, each channel
  114. * plane must be the same size.
  115. *
  116. * @note The linesize may be larger than the size of usable data -- there
  117. * may be extra padding present for performance reasons.
  118. */
  119. int linesize[AV_NUM_DATA_POINTERS];
  120. /**
  121. * pointers to the data planes/channels.
  122. *
  123. * For video, this should simply point to data[].
  124. *
  125. * For planar audio, each channel has a separate data pointer, and
  126. * linesize[0] contains the size of each channel buffer.
  127. * For packed audio, there is just one data pointer, and linesize[0]
  128. * contains the total size of the buffer for all channels.
  129. *
  130. * Note: Both data and extended_data should always be set in a valid frame,
  131. * but for planar audio with more channels that can fit in data,
  132. * extended_data must be used in order to access all channels.
  133. */
  134. uint8_t **extended_data;
  135. /**
  136. * width and height of the video frame
  137. */
  138. int width, height;
  139. /**
  140. * number of audio samples (per channel) described by this frame
  141. */
  142. int nb_samples;
  143. /**
  144. * format of the frame, -1 if unknown or unset
  145. * Values correspond to enum AVPixelFormat for video frames,
  146. * enum AVSampleFormat for audio)
  147. */
  148. int format;
  149. /**
  150. * 1 -> keyframe, 0-> not
  151. */
  152. int key_frame;
  153. /**
  154. * Picture type of the frame.
  155. */
  156. enum AVPictureType pict_type;
  157. #if FF_API_AVFRAME_LAVC
  158. attribute_deprecated
  159. uint8_t *base[AV_NUM_DATA_POINTERS];
  160. #endif
  161. /**
  162. * Sample aspect ratio for the video frame, 0/1 if unknown/unspecified.
  163. */
  164. AVRational sample_aspect_ratio;
  165. /**
  166. * Presentation timestamp in time_base units (time when frame should be shown to user).
  167. */
  168. int64_t pts;
  169. /**
  170. * PTS copied from the AVPacket that was decoded to produce this frame.
  171. */
  172. int64_t pkt_pts;
  173. /**
  174. * DTS copied from the AVPacket that triggered returning this frame.
  175. */
  176. int64_t pkt_dts;
  177. /**
  178. * picture number in bitstream order
  179. */
  180. int coded_picture_number;
  181. /**
  182. * picture number in display order
  183. */
  184. int display_picture_number;
  185. /**
  186. * quality (between 1 (good) and FF_LAMBDA_MAX (bad))
  187. */
  188. int quality;
  189. #if FF_API_AVFRAME_LAVC
  190. attribute_deprecated
  191. int reference;
  192. /**
  193. * QP table
  194. */
  195. attribute_deprecated
  196. int8_t *qscale_table;
  197. /**
  198. * QP store stride
  199. */
  200. attribute_deprecated
  201. int qstride;
  202. attribute_deprecated
  203. int qscale_type;
  204. /**
  205. * mbskip_table[mb]>=1 if MB didn't change
  206. * stride= mb_width = (width+15)>>4
  207. */
  208. attribute_deprecated
  209. uint8_t *mbskip_table;
  210. /**
  211. * motion vector table
  212. * @code
  213. * example:
  214. * int mv_sample_log2= 4 - motion_subsample_log2;
  215. * int mb_width= (width+15)>>4;
  216. * int mv_stride= (mb_width << mv_sample_log2) + 1;
  217. * motion_val[direction][x + y*mv_stride][0->mv_x, 1->mv_y];
  218. * @endcode
  219. */
  220. attribute_deprecated
  221. int16_t (*motion_val[2])[2];
  222. /**
  223. * macroblock type table
  224. * mb_type_base + mb_width + 2
  225. */
  226. attribute_deprecated
  227. uint32_t *mb_type;
  228. /**
  229. * DCT coefficients
  230. */
  231. attribute_deprecated
  232. short *dct_coeff;
  233. /**
  234. * motion reference frame index
  235. * the order in which these are stored can depend on the codec.
  236. */
  237. attribute_deprecated
  238. int8_t *ref_index[2];
  239. #endif
  240. /**
  241. * for some private data of the user
  242. */
  243. void *opaque;
  244. /**
  245. * error
  246. */
  247. uint64_t error[AV_NUM_DATA_POINTERS];
  248. #if FF_API_AVFRAME_LAVC
  249. attribute_deprecated
  250. int type;
  251. #endif
  252. /**
  253. * When decoding, this signals how much the picture must be delayed.
  254. * extra_delay = repeat_pict / (2*fps)
  255. */
  256. int repeat_pict;
  257. /**
  258. * The content of the picture is interlaced.
  259. */
  260. int interlaced_frame;
  261. /**
  262. * If the content is interlaced, is top field displayed first.
  263. */
  264. int top_field_first;
  265. /**
  266. * Tell user application that palette has changed from previous frame.
  267. */
  268. int palette_has_changed;
  269. #if FF_API_AVFRAME_LAVC
  270. attribute_deprecated
  271. int buffer_hints;
  272. /**
  273. * Pan scan.
  274. */
  275. attribute_deprecated
  276. struct AVPanScan *pan_scan;
  277. #endif
  278. /**
  279. * reordered opaque 64bit (generally an integer or a double precision float
  280. * PTS but can be anything).
  281. * The user sets AVCodecContext.reordered_opaque to represent the input at
  282. * that time,
  283. * the decoder reorders values as needed and sets AVFrame.reordered_opaque
  284. * to exactly one of the values provided by the user through AVCodecContext.reordered_opaque
  285. * @deprecated in favor of pkt_pts
  286. */
  287. int64_t reordered_opaque;
  288. #if FF_API_AVFRAME_LAVC
  289. /**
  290. * @deprecated this field is unused
  291. */
  292. attribute_deprecated void *hwaccel_picture_private;
  293. attribute_deprecated
  294. struct AVCodecContext *owner;
  295. attribute_deprecated
  296. void *thread_opaque;
  297. /**
  298. * log2 of the size of the block which a single vector in motion_val represents:
  299. * (4->16x16, 3->8x8, 2-> 4x4, 1-> 2x2)
  300. */
  301. attribute_deprecated
  302. uint8_t motion_subsample_log2;
  303. #endif
  304. /**
  305. * Sample rate of the audio data.
  306. */
  307. int sample_rate;
  308. /**
  309. * Channel layout of the audio data.
  310. */
  311. uint64_t channel_layout;
  312. /**
  313. * AVBuffer references backing the data for this frame. If all elements of
  314. * this array are NULL, then this frame is not reference counted.
  315. *
  316. * There may be at most one AVBuffer per data plane, so for video this array
  317. * always contains all the references. For planar audio with more than
  318. * AV_NUM_DATA_POINTERS channels, there may be more buffers than can fit in
  319. * this array. Then the extra AVBufferRef pointers are stored in the
  320. * extended_buf array.
  321. */
  322. AVBufferRef *buf[AV_NUM_DATA_POINTERS];
  323. /**
  324. * For planar audio which requires more than AV_NUM_DATA_POINTERS
  325. * AVBufferRef pointers, this array will hold all the references which
  326. * cannot fit into AVFrame.buf.
  327. *
  328. * Note that this is different from AVFrame.extended_data, which always
  329. * contains all the pointers. This array only contains the extra pointers,
  330. * which cannot fit into AVFrame.buf.
  331. *
  332. * This array is always allocated using av_malloc() by whoever constructs
  333. * the frame. It is freed in av_frame_unref().
  334. */
  335. AVBufferRef **extended_buf;
  336. /**
  337. * Number of elements in extended_buf.
  338. */
  339. int nb_extended_buf;
  340. AVFrameSideData **side_data;
  341. int nb_side_data;
  342. /**
  343. * @defgroup lavu_frame_flags AV_FRAME_FLAGS
  344. * Flags describing additional frame properties.
  345. *
  346. * @{
  347. */
  348. /**
  349. * The frame data may be corrupted, e.g. due to decoding errors.
  350. */
  351. #define AV_FRAME_FLAG_CORRUPT (1 << 0)
  352. /**
  353. * @}
  354. */
  355. /**
  356. * Frame flags, a combination of @ref lavu_frame_flags
  357. */
  358. int flags;
  359. } AVFrame;
  360. /**
  361. * Allocate an AVFrame and set its fields to default values. The resulting
  362. * struct must be freed using av_frame_free().
  363. *
  364. * @return An AVFrame filled with default values or NULL on failure.
  365. *
  366. * @note this only allocates the AVFrame itself, not the data buffers. Those
  367. * must be allocated through other means, e.g. with av_frame_get_buffer() or
  368. * manually.
  369. */
  370. AVFrame *av_frame_alloc(void);
  371. /**
  372. * Free the frame and any dynamically allocated objects in it,
  373. * e.g. extended_data. If the frame is reference counted, it will be
  374. * unreferenced first.
  375. *
  376. * @param frame frame to be freed. The pointer will be set to NULL.
  377. */
  378. void av_frame_free(AVFrame **frame);
  379. /**
  380. * Set up a new reference to the data described by the source frame.
  381. *
  382. * Copy frame properties from src to dst and create a new reference for each
  383. * AVBufferRef from src.
  384. *
  385. * If src is not reference counted, new buffers are allocated and the data is
  386. * copied.
  387. *
  388. * @return 0 on success, a negative AVERROR on error
  389. */
  390. int av_frame_ref(AVFrame *dst, const AVFrame *src);
  391. /**
  392. * Create a new frame that references the same data as src.
  393. *
  394. * This is a shortcut for av_frame_alloc()+av_frame_ref().
  395. *
  396. * @return newly created AVFrame on success, NULL on error.
  397. */
  398. AVFrame *av_frame_clone(const AVFrame *src);
  399. /**
  400. * Unreference all the buffers referenced by frame and reset the frame fields.
  401. */
  402. void av_frame_unref(AVFrame *frame);
  403. /**
  404. * Move everythnig contained in src to dst and reset src.
  405. */
  406. void av_frame_move_ref(AVFrame *dst, AVFrame *src);
  407. /**
  408. * Allocate new buffer(s) for audio or video data.
  409. *
  410. * The following fields must be set on frame before calling this function:
  411. * - format (pixel format for video, sample format for audio)
  412. * - width and height for video
  413. * - nb_samples and channel_layout for audio
  414. *
  415. * This function will fill AVFrame.data and AVFrame.buf arrays and, if
  416. * necessary, allocate and fill AVFrame.extended_data and AVFrame.extended_buf.
  417. * For planar formats, one buffer will be allocated for each plane.
  418. *
  419. * @param frame frame in which to store the new buffers.
  420. * @param align required buffer size alignment
  421. *
  422. * @return 0 on success, a negative AVERROR on error.
  423. */
  424. int av_frame_get_buffer(AVFrame *frame, int align);
  425. /**
  426. * Check if the frame data is writable.
  427. *
  428. * @return A positive value if the frame data is writable (which is true if and
  429. * only if each of the underlying buffers has only one reference, namely the one
  430. * stored in this frame). Return 0 otherwise.
  431. *
  432. * If 1 is returned the answer is valid until av_buffer_ref() is called on any
  433. * of the underlying AVBufferRefs (e.g. through av_frame_ref() or directly).
  434. *
  435. * @see av_frame_make_writable(), av_buffer_is_writable()
  436. */
  437. int av_frame_is_writable(AVFrame *frame);
  438. /**
  439. * Ensure that the frame data is writable, avoiding data copy if possible.
  440. *
  441. * Do nothing if the frame is writable, allocate new buffers and copy the data
  442. * if it is not.
  443. *
  444. * @return 0 on success, a negative AVERROR on error.
  445. *
  446. * @see av_frame_is_writable(), av_buffer_is_writable(),
  447. * av_buffer_make_writable()
  448. */
  449. int av_frame_make_writable(AVFrame *frame);
  450. /**
  451. * Copy the frame data from src to dst.
  452. *
  453. * This function does not allocate anything, dst must be already initialized and
  454. * allocated with the same parameters as src.
  455. *
  456. * This function only copies the frame data (i.e. the contents of the data /
  457. * extended data arrays), not any other properties.
  458. *
  459. * @return >= 0 on success, a negative AVERROR on error.
  460. */
  461. int av_frame_copy(AVFrame *dst, const AVFrame *src);
  462. /**
  463. * Copy only "metadata" fields from src to dst.
  464. *
  465. * Metadata for the purpose of this function are those fields that do not affect
  466. * the data layout in the buffers. E.g. pts, sample rate (for audio) or sample
  467. * aspect ratio (for video), but not width/height or channel layout.
  468. * Side data is also copied.
  469. */
  470. int av_frame_copy_props(AVFrame *dst, const AVFrame *src);
  471. /**
  472. * Get the buffer reference a given data plane is stored in.
  473. *
  474. * @param plane index of the data plane of interest in frame->extended_data.
  475. *
  476. * @return the buffer reference that contains the plane or NULL if the input
  477. * frame is not valid.
  478. */
  479. AVBufferRef *av_frame_get_plane_buffer(AVFrame *frame, int plane);
  480. /**
  481. * Add a new side data to a frame.
  482. *
  483. * @param frame a frame to which the side data should be added
  484. * @param type type of the added side data
  485. * @param size size of the side data
  486. *
  487. * @return newly added side data on success, NULL on error
  488. */
  489. AVFrameSideData *av_frame_new_side_data(AVFrame *frame,
  490. enum AVFrameSideDataType type,
  491. int size);
  492. /**
  493. * @return a pointer to the side data of a given type on success, NULL if there
  494. * is no side data with such type in this frame.
  495. */
  496. AVFrameSideData *av_frame_get_side_data(const AVFrame *frame,
  497. enum AVFrameSideDataType type);
  498. /**
  499. * If side data of the supplied type exists in the frame, free it and remove it
  500. * from the frame.
  501. */
  502. void av_frame_remove_side_data(AVFrame *frame, enum AVFrameSideDataType type);
  503. /**
  504. * @}
  505. */
  506. #endif /* AVUTIL_FRAME_H */