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.

548 lines
15KB

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