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.

516 lines
14KB

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