1 /*****************************************************************************
2 * vlc_picture.h: picture definitions
3 *****************************************************************************
4 * Copyright (C) 1999 - 2009 VLC authors and VideoLAN
6 * Authors: Vincent Seguin <seguin@via.ecp.fr>
7 * Samuel Hocevar <sam@via.ecp.fr>
8 * Olivier Aubert <oaubert 47 videolan d07 org>
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU Lesser General Public License as published by
12 * the Free Software Foundation; either version 2.1 of the License, or
13 * (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public License
21 * along with this program; if not, write to the Free Software Foundation,
22 * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23 *****************************************************************************/
26 #define VLC_PICTURE_H 1
30 #include <stdatomic.h>
33 using std::atomic_uintptr_t
;
34 using std::memory_order_relaxed
;
35 using std::memory_order_release
;
40 * This file defines picture structures and functions in vlc
45 /** Description of a planar graphic field */
46 typedef struct plane_t
48 uint8_t *p_pixels
; /**< Start of the plane's data */
50 /* Variables used for fast memcpy operations */
51 int i_lines
; /**< Number of lines, including margins */
52 int i_pitch
; /**< Number of bytes in a line, including margins */
54 /** Size of a macropixel, defaults to 1 */
57 /* Variables used for pictures with margins */
58 int i_visible_lines
; /**< How many visible lines are there? */
59 int i_visible_pitch
; /**< How many visible pixels are there? */
64 * Maximum number of plane for a picture
66 #define PICTURE_PLANE_MAX (VOUT_MAX_PLANES)
68 typedef struct picture_context_t
70 void (*destroy
)(struct picture_context_t
*);
71 struct picture_context_t
*(*copy
)(struct picture_context_t
*);
72 struct vlc_video_context
*vctx
;
75 typedef struct picture_buffer_t
83 typedef struct vlc_decoder_device vlc_decoder_device
;
84 typedef struct vlc_video_context vlc_video_context
;
86 struct vlc_video_context_operations
88 void (*destroy
)(void *priv
);
91 /** Decoder device type */
92 enum vlc_video_context_type
94 VLC_VIDEO_CONTEXT_NONE
,
95 VLC_VIDEO_CONTEXT_VAAPI
,
96 VLC_VIDEO_CONTEXT_VDPAU
,
97 VLC_VIDEO_CONTEXT_DXVA2
, /**< private: d3d9_video_context_t* */
98 VLC_VIDEO_CONTEXT_D3D11VA
, /**< private: d3d11_video_context_t* */
99 VLC_VIDEO_CONTEXT_AWINDOW
, /**< private: android_video_context_t* */
100 VLC_VIDEO_CONTEXT_NVDEC
,
101 VLC_VIDEO_CONTEXT_CVPX
,
102 VLC_VIDEO_CONTEXT_MMAL
,
105 VLC_API vlc_video_context
* vlc_video_context_Create(vlc_decoder_device
*,
106 enum vlc_video_context_type private_type
,
108 const struct vlc_video_context_operations
*);
109 VLC_API
void vlc_video_context_Release(vlc_video_context
*);
111 VLC_API
enum vlc_video_context_type
vlc_video_context_GetType(const vlc_video_context
*);
112 VLC_API
void *vlc_video_context_GetPrivate(vlc_video_context
*, enum vlc_video_context_type
);
113 VLC_API vlc_video_context
*vlc_video_context_Hold(vlc_video_context
*);
116 * Get the decoder device used by the device context.
118 * This will increment the refcount of the decoder device.
120 VLC_API vlc_decoder_device
*vlc_video_context_HoldDevice(vlc_video_context
*);
129 * The properties of the picture
131 video_frame_format_t format
;
133 plane_t p
[PICTURE_PLANE_MAX
]; /**< description of the planes */
134 int i_planes
; /**< number of allocated planes */
136 /** \name Picture management properties
137 * These properties can be modified using the video output thread API,
138 * but should never be written directly */
140 vlc_tick_t date
; /**< display date */
145 /** \name Picture dynamic properties
146 * Those properties can be changed by the decoder
149 bool b_progressive
; /**< is it a progressive frame? */
150 bool b_top_field_first
; /**< which field is first */
151 bool b_multiview_left_eye
; /**< left eye or right eye in multiview */
152 unsigned int i_nb_fields
; /**< number of displayed fields */
153 picture_context_t
*context
; /**< video format-specific data pointer */
156 /** Private data - the video output plugin might want to put stuff here to
157 * keep track of the picture */
160 /** Next picture in a FIFO a pictures */
161 struct picture_t
*p_next
;
163 atomic_uintptr_t refs
;
166 static inline vlc_video_context
* picture_GetVideoContext(picture_t
*pic
)
168 return pic
->context
? pic
->context
->vctx
: NULL
;
172 * Check whether a picture has other pictures linked
174 static inline bool picture_HasChainedPics(const picture_t
*pic
)
176 return pic
->p_next
!= NULL
;
180 * picture chaining helpers
183 typedef struct vlc_pic_chain
{
186 } vlc_picture_chain_t
;
189 * Initializes or reset a picture chain
191 * \warning do not call this if the chain still holds pictures, it will leak them.
193 static inline void vlc_picture_chain_Init(vlc_picture_chain_t
*chain
)
196 // chain->tail = NULL not needed
200 * Check whether a picture chain holds pictures or not.
202 * \return true if it is empty.
204 static inline bool vlc_picture_chain_IsEmpty(const vlc_picture_chain_t
*chain
)
206 return chain
->front
== NULL
;
210 * Check whether a picture chain has more than one picture.
212 static inline bool vlc_picture_chain_HasNext(const vlc_picture_chain_t
*chain
)
214 return !vlc_picture_chain_IsEmpty(chain
) && chain
->front
!= chain
->tail
;
218 * Pop the front of a picture chain.
220 * The next picture in the chain becomes the front of the picture chain.
222 * \return the front of the picture chain (the picture itself)
224 static inline picture_t
* vlc_picture_chain_PopFront(vlc_picture_chain_t
*chain
)
226 picture_t
*front
= chain
->front
;
229 chain
->front
= front
->p_next
;
230 // unlink the front picture from the rest of the chain
231 front
->p_next
= NULL
;
237 * Peek the front of a picture chain.
239 * The picture chain is unchanged.
241 * \return the front of the picture chain (the picture itself)
243 static inline picture_t
* vlc_picture_chain_PeekFront(vlc_picture_chain_t
*chain
)
249 * Append a picture to a picture chain.
251 * \param chain the picture chain pointer
252 * \param tail the known tail of the picture chain
253 * \param pic the picture to append to the chain
255 * \return the new tail of the picture chain
257 static inline void vlc_picture_chain_Append(vlc_picture_chain_t
*chain
,
260 if (chain
->front
== NULL
)
263 chain
->tail
->p_next
= pic
;
264 // make sure the picture doesn't have chained pics
265 vlc_assert( !picture_HasChainedPics( pic
) );
266 pic
->p_next
= NULL
; // we're appending a picture, not a chain
271 * Append a picture chain to a picture chain.
273 static inline void vlc_picture_chain_AppendChain(picture_t
*chain
, picture_t
*tail
)
275 chain
->p_next
= tail
;
279 * Copy the picture chain in another picture chain and clear the original
282 * \param in picture chain to copy and clear
283 * \param out picture chain to copy into
285 static inline void vlc_picture_chain_GetAndClear(vlc_picture_chain_t
*in
,
286 vlc_picture_chain_t
*out
)
289 vlc_picture_chain_Init(in
);
293 * Reset a picture chain.
295 * \return the picture chain that was contained in the picture
297 static inline vlc_picture_chain_t
picture_GetAndResetChain(picture_t
*pic
)
299 vlc_picture_chain_t chain
= (vlc_picture_chain_t
) { pic
->p_next
, pic
->p_next
};
300 while ( chain
.tail
&& chain
.tail
->p_next
) // find the proper tail
301 chain
.tail
= chain
.tail
->p_next
;
308 * This function will create a new picture.
309 * The picture created will implement a default release management compatible
310 * with picture_Hold and picture_Release. This default management will release
311 * p_sys, gc.p_sys fields if non NULL.
313 VLC_API picture_t
* picture_New( vlc_fourcc_t i_chroma
, int i_width
, int i_height
, int i_sar_num
, int i_sar_den
) VLC_USED
;
316 * This function will create a new picture using the given format.
318 * When possible, it is preferred to use this function over picture_New
319 * as more information about the format is kept.
321 VLC_API picture_t
* picture_NewFromFormat( const video_format_t
*p_fmt
) VLC_USED
;
324 * Resource for a picture.
329 void (*pf_destroy
)(picture_t
*);
332 * XXX all fields MUST be set to the right value.
336 uint8_t *p_pixels
; /**< Start of the plane's data */
337 int i_lines
; /**< Number of lines, including margins */
338 int i_pitch
; /**< Number of bytes in a line, including margins */
339 } p
[PICTURE_PLANE_MAX
];
341 } picture_resource_t
;
344 * This function will create a new picture using the provided resource.
346 VLC_API picture_t
* picture_NewFromResource( const video_format_t
*, const picture_resource_t
* ) VLC_USED
;
349 * Destroys a picture without references.
351 * This function destroys a picture with zero references left.
352 * Never call this function directly. Use picture_Release() instead.
354 VLC_API
void picture_Destroy(picture_t
*picture
);
357 * Increments the picture reference count.
361 static inline picture_t
*picture_Hold(picture_t
*picture
)
363 atomic_fetch_add_explicit(&picture
->refs
, (uintptr_t)1,
364 memory_order_relaxed
);
369 * Decrements the picture reference count.
371 * If the reference count reaches zero, the picture is destroyed. If it was
372 * allocated from a pool, the underlying picture buffer will be returned to the
373 * pool. Otherwise, the picture buffer will be freed.
375 static inline void picture_Release(picture_t
*picture
)
377 uintptr_t refs
= atomic_fetch_sub_explicit(&picture
->refs
, (uintptr_t)1,
378 memory_order_release
);
379 vlc_assert(refs
> 0);
381 picture_Destroy(picture
);
385 * This function will copy all picture dynamic properties.
387 VLC_API
void picture_CopyProperties( picture_t
*p_dst
, const picture_t
*p_src
);
390 * This function will reset a picture information (properties and quantizers).
391 * It is sometimes useful for reusing pictures (like from a pool).
393 VLC_API
void picture_Reset( picture_t
* );
396 * This function will copy the picture pixels.
397 * You can safely copy between pictures that do not have the same size,
398 * only the compatible(smaller) part will be copied.
400 VLC_API
void picture_CopyPixels( picture_t
*p_dst
, const picture_t
*p_src
);
401 VLC_API
void plane_CopyPixels( plane_t
*p_dst
, const plane_t
*p_src
);
404 * This function will copy both picture dynamic properties and pixels.
405 * You have to notice that sometime a simple picture_Hold may do what
406 * you want without the copy overhead.
407 * Provided for convenience.
409 * \param p_dst pointer to the destination picture.
410 * \param p_src pointer to the source picture.
412 VLC_API
void picture_Copy( picture_t
*p_dst
, const picture_t
*p_src
);
415 * Perform a shallow picture copy
417 * This function makes a shallow copy of an existing picture. The same planes
418 * and resources will be used, and the cloned picture reference count will be
421 * \return A clone picture on success, NULL on error.
423 VLC_API picture_t
*picture_Clone(picture_t
*pic
);
426 * This function will export a picture to an encoded bitstream.
428 * pp_image will contain the encoded bitstream in psz_format format.
430 * p_fmt can be NULL otherwise it will be set with the format used for the
431 * picture before encoding.
433 * i_override_width/height allow to override the width and/or the height of the
434 * picture to be encoded:
435 * - if strictly lower than 0, the original dimension will be used.
436 * - if equal to 0, it will be deduced from the other dimension which must be
438 * - if strictly higher than 0, it will either override the dimension if b_crop
439 * is false, or crop the picture to the provided size if b_crop is true.
440 * If at most one of them is > 0 then the picture aspect ratio will be kept.
442 VLC_API
int picture_Export( vlc_object_t
*p_obj
, block_t
**pp_image
, video_format_t
*p_fmt
,
443 picture_t
*p_picture
, vlc_fourcc_t i_format
, int i_override_width
,
444 int i_override_height
, bool b_crop
);
447 * This function will setup all fields of a picture_t without allocating any
449 * XXX The memory must already be initialized.
450 * It does not need to be released.
452 * It will return VLC_EGENERIC if the core does not understand the requested
455 * It can be useful to get the properties of planes.
457 VLC_API
int picture_Setup( picture_t
*, const video_format_t
* );
460 /*****************************************************************************
461 * Shortcuts to access image components
462 *****************************************************************************/
474 #define Y_PIXELS p[Y_PLANE].p_pixels
475 #define Y_PITCH p[Y_PLANE].i_pitch
476 #define U_PIXELS p[U_PLANE].p_pixels
477 #define U_PITCH p[U_PLANE].i_pitch
478 #define V_PIXELS p[V_PLANE].p_pixels
479 #define V_PITCH p[V_PLANE].i_pitch
480 #define A_PIXELS p[A_PLANE].p_pixels
481 #define A_PITCH p[A_PLANE].i_pitch
484 * Swap UV planes of a Tri Planars picture.
486 * It just swap the planes information without doing any copy.
488 static inline void picture_SwapUV(picture_t
*picture
)
490 vlc_assert(picture
->i_planes
== 3);
492 plane_t tmp_plane
= picture
->p
[U_PLANE
];
493 picture
->p
[U_PLANE
] = picture
->p
[V_PLANE
];
494 picture
->p
[V_PLANE
] = tmp_plane
;
499 #endif /* VLC_PICTURE_H */