VLC  4.0.0-dev
vlc_picture.h
Go to the documentation of this file.
1 /*****************************************************************************
2  * vlc_picture.h: picture definitions
3  *****************************************************************************
4  * Copyright (C) 1999 - 2009 VLC authors and VideoLAN
5  *
6  * Authors: Vincent Seguin <seguin@via.ecp.fr>
7  * Samuel Hocevar <sam@via.ecp.fr>
8  * Olivier Aubert <oaubert 47 videolan d07 org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24 
25 #ifndef VLC_PICTURE_H
26 #define VLC_PICTURE_H 1
27 
28 #include <assert.h>
29 #ifndef __cplusplus
30 #include <stdatomic.h>
31 #else
32 #include <atomic>
33 using std::atomic_uintptr_t;
34 using std::memory_order_relaxed;
35 using std::memory_order_release;
36 #endif
37 
38 /**
39  * \file
40  * This file defines picture structures and functions in vlc
41  */
42 
43 #include <vlc_es.h>
44 
45 /** Description of a planar graphic field */
46 typedef struct plane_t
47 {
48  uint8_t *p_pixels; /**< Start of the plane's data */
49 
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 */
53 
54  /** Size of a macropixel, defaults to 1 */
55  int i_pixel_pitch;
56 
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? */
60 
61 } plane_t;
62 
63 /**
64  * Maximum number of plane for a picture
65  */
66 #define PICTURE_PLANE_MAX (VOUT_MAX_PLANES)
67 
68 typedef struct picture_context_t
69 {
70  void (*destroy)(struct picture_context_t *);
71  struct picture_context_t *(*copy)(struct picture_context_t *);
73 
74 typedef struct picture_buffer_t
75 {
76  int fd;
77  void *base;
78  size_t size;
79  off_t offset;
81 
84 
86 {
87  void (*destroy)(void *priv);
88 };
89 
90 /** Decoder device type */
92 {
96  VLC_VIDEO_CONTEXT_DXVA2, /**< private: d3d9_video_context_t* */
97  VLC_VIDEO_CONTEXT_D3D11VA, /**< private: d3d11_video_context_t* */
102 };
103 
105  enum vlc_video_context_type private_type,
106  size_t private_size,
107  const struct vlc_video_context_operations *);
109 
113 
114 /**
115  * Get the decoder device used by the device context.
116  *
117  * This will increment the refcount of the decoder device.
118  */
120 
121 
122 /**
123  * Video picture
124  */
125 struct picture_t
126 {
127  /**
128  * The properties of the picture
129  */
130  video_frame_format_t format;
132  plane_t p[PICTURE_PLANE_MAX]; /**< description of the planes */
133  int i_planes; /**< number of allocated planes */
135  /** \name Picture management properties
136  * These properties can be modified using the video output thread API,
137  * but should never be written directly */
138  /**@{*/
139  vlc_tick_t date; /**< display date */
140  bool b_force;
141  bool b_still;
142  /**@}*/
143 
144  /** \name Picture dynamic properties
145  * Those properties can be changed by the decoder
146  * @{
147  */
148  bool b_progressive; /**< is it a progressive frame? */
149  bool b_top_field_first; /**< which field is first */
150  unsigned int i_nb_fields; /**< number of displayed fields */
151  picture_context_t *context; /**< video format-specific data pointer */
152  /**@}*/
153 
154  /** Private data - the video output plugin might want to put stuff here to
155  * keep track of the picture */
156  void *p_sys;
158  /** Next picture in a FIFO a pictures */
159  struct picture_t *p_next;
161  atomic_uintptr_t refs;
162 };
163 
164 /**
165  * This function will create a new picture.
166  * The picture created will implement a default release management compatible
167  * with picture_Hold and picture_Release. This default management will release
168  * p_sys, gc.p_sys fields if non NULL.
169  */
170 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;
171 
172 /**
173  * This function will create a new picture using the given format.
174  *
175  * When possible, it is preferred to use this function over picture_New
176  * as more information about the format is kept.
177  */
179 
180 /**
181  * Resource for a picture.
182  */
183 typedef struct
184 {
185  void *p_sys;
186  void (*pf_destroy)(picture_t *);
188  /* Plane resources
189  * XXX all fields MUST be set to the right value.
190  */
191  struct
192  {
193  uint8_t *p_pixels; /**< Start of the plane's data */
194  int i_lines; /**< Number of lines, including margins */
195  int i_pitch; /**< Number of bytes in a line, including margins */
197 
199 
200 /**
201  * This function will create a new picture using the provided resource.
202  *
203  * If the resource is NULL then a plain picture_NewFromFormat is returned.
204  */
206 
207 /**
208  * Destroys a picture without references.
209  *
210  * This function destroys a picture with zero references left.
211  * Never call this function directly. Use picture_Release() instead.
212  */
213 VLC_API void picture_Destroy(picture_t *picture);
214 
215 /**
216  * Increments the picture reference count.
217  *
218  * \return picture
219  */
220 static inline picture_t *picture_Hold(picture_t *picture)
221 {
222  atomic_fetch_add_explicit(&picture->refs, (uintptr_t)1,
223  memory_order_relaxed);
224  return picture;
225 }
226 
227 /**
228  * Decrements the picture reference count.
229  *
230  * If the reference count reaches zero, the picture is destroyed. If it was
231  * allocated from a pool, the underlying picture buffer will be returned to the
232  * pool. Otherwise, the picture buffer will be freed.
233  */
234 static inline void picture_Release(picture_t *picture)
235 {
236  uintptr_t refs = atomic_fetch_sub_explicit(&picture->refs, (uintptr_t)1,
237  memory_order_release);
238  vlc_assert(refs > 0);
239  if (refs == 1)
240  picture_Destroy(picture);
241 }
242 
243 /**
244  * This function will copy all picture dynamic properties.
245  */
246 VLC_API void picture_CopyProperties( picture_t *p_dst, const picture_t *p_src );
247 
248 /**
249  * This function will reset a picture information (properties and quantizers).
250  * It is sometimes useful for reusing pictures (like from a pool).
251  */
253 
254 /**
255  * This function will copy the picture pixels.
256  * You can safely copy between pictures that do not have the same size,
257  * only the compatible(smaller) part will be copied.
258  */
259 VLC_API void picture_CopyPixels( picture_t *p_dst, const picture_t *p_src );
260 VLC_API void plane_CopyPixels( plane_t *p_dst, const plane_t *p_src );
261 
262 /**
263  * This function will copy both picture dynamic properties and pixels.
264  * You have to notice that sometime a simple picture_Hold may do what
265  * you want without the copy overhead.
266  * Provided for convenience.
267  *
268  * \param p_dst pointer to the destination picture.
269  * \param p_src pointer to the source picture.
270  */
271 VLC_API void picture_Copy( picture_t *p_dst, const picture_t *p_src );
272 
273 /**
274  * Perform a shallow picture copy
275  *
276  * This function makes a shallow copy of an existing picture. The same planes
277  * and resources will be used, and the cloned picture reference count will be
278  * incremented.
279  *
280  * \return A clone picture on success, NULL on error.
281  */
283 
284 /**
285  * This function will export a picture to an encoded bitstream.
286  *
287  * pp_image will contain the encoded bitstream in psz_format format.
288  *
289  * p_fmt can be NULL otherwise it will be set with the format used for the
290  * picture before encoding.
291  *
292  * i_override_width/height allow to override the width and/or the height of the
293  * picture to be encoded:
294  * - if strictly lower than 0, the original dimension will be used.
295  * - if equal to 0, it will be deduced from the other dimension which must be
296  * different to 0.
297  * - if strictly higher than 0, it will either override the dimension if b_crop
298  * is false, or crop the picture to the provided size if b_crop is true.
299  * If at most one of them is > 0 then the picture aspect ratio will be kept.
300  */
301 VLC_API int picture_Export( vlc_object_t *p_obj, block_t **pp_image, video_format_t *p_fmt,
302  picture_t *p_picture, vlc_fourcc_t i_format, int i_override_width,
303  int i_override_height, bool b_crop );
304 
305 /**
306  * This function will setup all fields of a picture_t without allocating any
307  * memory.
308  * XXX The memory must already be initialized.
309  * It does not need to be released.
310  *
311  * It will return VLC_EGENERIC if the core does not understand the requested
312  * format.
313  *
314  * It can be useful to get the properties of planes.
315  */
317 
318 
319 /*****************************************************************************
320  * Shortcuts to access image components
321  *****************************************************************************/
322 
323 /* Plane indices */
324 enum
325 {
326  Y_PLANE = 0,
327  U_PLANE = 1,
328  V_PLANE = 2,
329  A_PLANE = 3,
330 };
331 
332 /* Shortcuts */
333 #define Y_PIXELS p[Y_PLANE].p_pixels
334 #define Y_PITCH p[Y_PLANE].i_pitch
335 #define U_PIXELS p[U_PLANE].p_pixels
336 #define U_PITCH p[U_PLANE].i_pitch
337 #define V_PIXELS p[V_PLANE].p_pixels
338 #define V_PITCH p[V_PLANE].i_pitch
339 #define A_PIXELS p[A_PLANE].p_pixels
340 #define A_PITCH p[A_PLANE].i_pitch
342 /**
343  * Swap UV planes of a Tri Planars picture.
344  *
345  * It just swap the planes information without doing any copy.
346  */
347 static inline void picture_SwapUV(picture_t *picture)
348 {
349  vlc_assert(picture->i_planes == 3);
350 
351  plane_t tmp_plane = picture->p[U_PLANE];
352  picture->p[U_PLANE] = picture->p[V_PLANE];
353  picture->p[V_PLANE] = tmp_plane;
354 }
355 
356 /**@}*/
357 
358 #endif /* VLC_PICTURE_H */
Definition: vlc_picture.h:95
private: d3d11_video_context_t*
Definition: vlc_picture.h:98
void plane_CopyPixels(plane_t *p_dst, const plane_t *p_src)
Definition: picture.c:337
int i_visible_lines
How many visible lines are there?
Definition: vlc_picture.h:59
void picture_CopyProperties(picture_t *p_dst, const picture_t *p_src)
This function will copy all picture dynamic properties.
Definition: picture.c:373
atomic_uintptr_t refs
Definition: vlc_picture.h:162
Video picture.
Definition: vlc_picture.h:126
int picture_Setup(picture_t *, const video_format_t *)
This function will setup all fields of a picture_t without allocating any memory. ...
int i_pitch
Number of bytes in a line, including margins.
Definition: vlc_picture.h:53
int picture_Export(vlc_object_t *p_obj, block_t **pp_image, video_format_t *p_fmt, picture_t *p_picture, vlc_fourcc_t i_format, int i_override_width, int i_override_height, bool b_crop)
This function will export a picture to an encoded bitstream.
Definition: picture.c:436
vlc_video_context_type
Decoder device type.
Definition: vlc_picture.h:92
Resource for a picture.
Definition: vlc_picture.h:184
int i_visible_pitch
How many visible pixels are there?
Definition: vlc_picture.h:60
Definition: vlc_picture.h:100
int i_planes
number of allocated planes
Definition: vlc_picture.h:134
picture_t * picture_NewFromFormat(const video_format_t *p_fmt)
This function will create a new picture using the given format.
#define PICTURE_PLANE_MAX
Maximum number of plane for a picture.
Definition: vlc_picture.h:67
Definition: vlc_picture.h:86
uint8_t * p_pixels
Start of the plane&#39;s data.
Definition: vlc_picture.h:49
static picture_t * picture_Hold(picture_t *picture)
Increments the picture reference count.
Definition: vlc_picture.h:221
Definition: vlc_picture.h:329
Definition: vlc_picture.h:75
Definition: vlc_picture.h:328
Definition: vlc_picture.h:102
struct picture_buffer_t picture_buffer_t
Definition: decoder_helpers.c:231
Definition: vlc_picture.h:327
Definition: vlc_picture.h:94
int64_t vlc_tick_t
High precision date or time interval.
Definition: vlc_tick.h:45
void * p_sys
Private data - the video output plugin might want to put stuff here to keep track of the picture...
Definition: vlc_picture.h:157
Definition: vlc_picture.h:99
uint32_t vlc_fourcc_t
Definition: fourcc_gen.c:33
static void picture_Release(picture_t *picture)
Decrements the picture reference count.
Definition: vlc_picture.h:235
int i_pixel_pitch
Size of a macropixel, defaults to 1.
Definition: vlc_picture.h:56
picture_t * picture_Clone(picture_t *pic)
Perform a shallow picture copy.
Definition: picture.c:408
#define vlc_assert(pred)
Run-time assertion.
Definition: vlc_common.h:267
private: d3d9_video_context_t*
Definition: vlc_picture.h:97
void picture_Copy(picture_t *p_dst, const picture_t *p_src)
This function will copy both picture dynamic properties and pixels.
Definition: picture.c:395
struct picture_t * p_next
Next picture in a FIFO a pictures.
Definition: vlc_picture.h:160
video format description
Definition: vlc_es.h:349
int i_lines
Number of lines, including margins.
Definition: vlc_picture.h:52
picture_t * picture_New(vlc_fourcc_t i_chroma, int i_width, int i_height, int i_sar_num, int i_sar_den)
This function will create a new picture.
Definition: picture.c:305
Decoder context struct.
Definition: vlc_codec.h:555
static void picture_SwapUV(picture_t *picture)
Swap UV planes of a Tri Planars picture.
Definition: vlc_picture.h:348
Description of a planar graphic field.
Definition: vlc_picture.h:47
void picture_CopyPixels(picture_t *p_dst, const picture_t *p_src)
This function will copy the picture pixels.
Definition: picture.c:384
struct picture_context_t picture_context_t
picture_t * picture_NewFromResource(const video_format_t *, const picture_resource_t *)
This function will create a new picture using the provided resource.
Definition: picture.c:222
#define VLC_API
Definition: fourcc_gen.c:31
vlc_video_context * vlc_video_context_Create(vlc_decoder_device *, enum vlc_video_context_type private_type, size_t private_size, const struct vlc_video_context_operations *)
Definition: decoder_helpers.c:241
void vlc_video_context_Release(vlc_video_context *)
Definition: decoder_helpers.c:277
struct plane_t plane_t
Description of a planar graphic field.
vlc_decoder_device * vlc_video_context_HoldDevice(vlc_video_context *)
Get the decoder device used by the device context.
Definition: decoder_helpers.c:289
Definition: vlc_picture.h:101
Definition: vlc_picture.h:96
vlc_video_context * vlc_video_context_Hold(vlc_video_context *)
Definition: decoder_helpers.c:271
Definition: vlc_block.h:117
#define p(t)
void * vlc_video_context_GetPrivate(vlc_video_context *, enum vlc_video_context_type)
Definition: decoder_helpers.c:259
void picture_Destroy(picture_t *picture)
Destroys a picture without references.
Definition: picture.c:320
This file defines the elementary streams format types.
VLC object common members.
Definition: vlc_objects.h:43
enum vlc_video_context_type vlc_video_context_GetType(const vlc_video_context *)
Definition: decoder_helpers.c:266
plane_t p[(5)]
description of the planes
Definition: vlc_picture.h:133
#define VLC_USED
Definition: fourcc_gen.c:32
Definition: vlc_picture.h:330
Definition: vlc_picture.h:69
void picture_Reset(picture_t *)
This function will reset a picture information (properties and quantizers).
Definition: picture.c:88