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 *);
72  struct vlc_video_context *vctx;
74 
75 typedef struct picture_buffer_t
76 {
77  int fd;
78  void *base;
79  size_t size;
80  off_t offset;
82 
85 
87 {
88  void (*destroy)(void *priv);
89 };
90 
91 /** Decoder device type */
93 {
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* */
103 };
104 
106  enum vlc_video_context_type private_type,
107  size_t private_size,
108  const struct vlc_video_context_operations *);
110 
114 
115 /**
116  * Get the decoder device used by the device context.
117  *
118  * This will increment the refcount of the decoder device.
119  */
121 
122 
123 /**
124  * Video picture
125  */
126 struct picture_t
127 {
128  /**
129  * The properties of the picture
130  */
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 */
139  /**@{*/
140  vlc_tick_t date; /**< display date */
141  bool b_force;
142  bool b_still;
143  /**@}*/
144 
145  /** \name Picture dynamic properties
146  * Those properties can be changed by the decoder
147  * @{
148  */
149  bool b_progressive; /**< is it a progressive frame? */
150  bool b_top_field_first; /**< which field is first */
151  unsigned int i_nb_fields; /**< number of displayed fields */
152  picture_context_t *context; /**< video format-specific data pointer */
153  /**@}*/
154 
155  /** Private data - the video output plugin might want to put stuff here to
156  * keep track of the picture */
157  void *p_sys;
159  /** Next picture in a FIFO a pictures */
160  struct picture_t *p_next;
162  atomic_uintptr_t refs;
163 };
164 
166 {
167  return pic->context ? pic->context->vctx : NULL;
168 }
169 
170 /**
171  * This function will create a new picture.
172  * The picture created will implement a default release management compatible
173  * with picture_Hold and picture_Release. This default management will release
174  * p_sys, gc.p_sys fields if non NULL.
175  */
176 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;
177 
178 /**
179  * This function will create a new picture using the given format.
180  *
181  * When possible, it is preferred to use this function over picture_New
182  * as more information about the format is kept.
183  */
185 
186 /**
187  * Resource for a picture.
188  */
189 typedef struct
190 {
191  void *p_sys;
192  void (*pf_destroy)(picture_t *);
194  /* Plane resources
195  * XXX all fields MUST be set to the right value.
196  */
197  struct
198  {
199  uint8_t *p_pixels; /**< Start of the plane's data */
200  int i_lines; /**< Number of lines, including margins */
201  int i_pitch; /**< Number of bytes in a line, including margins */
203 
205 
206 /**
207  * This function will create a new picture using the provided resource.
208  *
209  * If the resource is NULL then a plain picture_NewFromFormat is returned.
210  */
212 
213 /**
214  * Destroys a picture without references.
215  *
216  * This function destroys a picture with zero references left.
217  * Never call this function directly. Use picture_Release() instead.
218  */
219 VLC_API void picture_Destroy(picture_t *picture);
220 
221 /**
222  * Increments the picture reference count.
223  *
224  * \return picture
225  */
226 static inline picture_t *picture_Hold(picture_t *picture)
227 {
228  atomic_fetch_add_explicit(&picture->refs, (uintptr_t)1,
229  memory_order_relaxed);
230  return picture;
231 }
232 
233 /**
234  * Decrements the picture reference count.
235  *
236  * If the reference count reaches zero, the picture is destroyed. If it was
237  * allocated from a pool, the underlying picture buffer will be returned to the
238  * pool. Otherwise, the picture buffer will be freed.
239  */
240 static inline void picture_Release(picture_t *picture)
241 {
242  uintptr_t refs = atomic_fetch_sub_explicit(&picture->refs, (uintptr_t)1,
243  memory_order_release);
244  vlc_assert(refs > 0);
245  if (refs == 1)
246  picture_Destroy(picture);
247 }
248 
249 /**
250  * This function will copy all picture dynamic properties.
251  */
252 VLC_API void picture_CopyProperties( picture_t *p_dst, const picture_t *p_src );
253 
254 /**
255  * This function will reset a picture information (properties and quantizers).
256  * It is sometimes useful for reusing pictures (like from a pool).
257  */
259 
260 /**
261  * This function will copy the picture pixels.
262  * You can safely copy between pictures that do not have the same size,
263  * only the compatible(smaller) part will be copied.
264  */
265 VLC_API void picture_CopyPixels( picture_t *p_dst, const picture_t *p_src );
266 VLC_API void plane_CopyPixels( plane_t *p_dst, const plane_t *p_src );
267 
268 /**
269  * This function will copy both picture dynamic properties and pixels.
270  * You have to notice that sometime a simple picture_Hold may do what
271  * you want without the copy overhead.
272  * Provided for convenience.
273  *
274  * \param p_dst pointer to the destination picture.
275  * \param p_src pointer to the source picture.
276  */
277 VLC_API void picture_Copy( picture_t *p_dst, const picture_t *p_src );
278 
279 /**
280  * Perform a shallow picture copy
281  *
282  * This function makes a shallow copy of an existing picture. The same planes
283  * and resources will be used, and the cloned picture reference count will be
284  * incremented.
285  *
286  * \return A clone picture on success, NULL on error.
287  */
289 
290 /**
291  * This function will export a picture to an encoded bitstream.
292  *
293  * pp_image will contain the encoded bitstream in psz_format format.
294  *
295  * p_fmt can be NULL otherwise it will be set with the format used for the
296  * picture before encoding.
297  *
298  * i_override_width/height allow to override the width and/or the height of the
299  * picture to be encoded:
300  * - if strictly lower than 0, the original dimension will be used.
301  * - if equal to 0, it will be deduced from the other dimension which must be
302  * different to 0.
303  * - if strictly higher than 0, it will either override the dimension if b_crop
304  * is false, or crop the picture to the provided size if b_crop is true.
305  * If at most one of them is > 0 then the picture aspect ratio will be kept.
306  */
307 VLC_API int picture_Export( vlc_object_t *p_obj, block_t **pp_image, video_format_t *p_fmt,
308  picture_t *p_picture, vlc_fourcc_t i_format, int i_override_width,
309  int i_override_height, bool b_crop );
310 
311 /**
312  * This function will setup all fields of a picture_t without allocating any
313  * memory.
314  * XXX The memory must already be initialized.
315  * It does not need to be released.
316  *
317  * It will return VLC_EGENERIC if the core does not understand the requested
318  * format.
319  *
320  * It can be useful to get the properties of planes.
321  */
323 
324 
325 /*****************************************************************************
326  * Shortcuts to access image components
327  *****************************************************************************/
328 
329 /* Plane indices */
330 enum
331 {
332  Y_PLANE = 0,
333  U_PLANE = 1,
334  V_PLANE = 2,
335  A_PLANE = 3,
336 };
337 
338 /* Shortcuts */
339 #define Y_PIXELS p[Y_PLANE].p_pixels
340 #define Y_PITCH p[Y_PLANE].i_pitch
341 #define U_PIXELS p[U_PLANE].p_pixels
342 #define U_PITCH p[U_PLANE].i_pitch
343 #define V_PIXELS p[V_PLANE].p_pixels
344 #define V_PITCH p[V_PLANE].i_pitch
345 #define A_PIXELS p[A_PLANE].p_pixels
346 #define A_PITCH p[A_PLANE].i_pitch
348 /**
349  * Swap UV planes of a Tri Planars picture.
350  *
351  * It just swap the planes information without doing any copy.
352  */
353 static inline void picture_SwapUV(picture_t *picture)
354 {
355  vlc_assert(picture->i_planes == 3);
356 
357  plane_t tmp_plane = picture->p[U_PLANE];
358  picture->p[U_PLANE] = picture->p[V_PLANE];
359  picture->p[V_PLANE] = tmp_plane;
360 }
361 
362 /**@}*/
363 
364 #endif /* VLC_PICTURE_H */
Definition: vlc_picture.h:96
private: d3d11_video_context_t*
Definition: vlc_picture.h:99
void plane_CopyPixels(plane_t *p_dst, const plane_t *p_src)
Definition: picture.c:355
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:391
atomic_uintptr_t refs
Definition: vlc_picture.h:163
Video picture.
Definition: vlc_picture.h:127
struct vlc_video_context * vctx
Definition: vlc_picture.h:73
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:461
Definition: vlc_picture.h:334
vlc_video_context_type
Decoder device type.
Definition: vlc_picture.h:93
Resource for a picture.
Definition: vlc_picture.h:190
int i_visible_pitch
How many visible pixels are there?
Definition: vlc_picture.h:60
static vlc_video_context * picture_GetVideoContext(picture_t *pic)
Definition: vlc_picture.h:166
Definition: vlc_picture.h:101
int i_planes
number of allocated planes
Definition: vlc_picture.h:135
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:87
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:227
picture_context_t * context
video format-specific data pointer
Definition: vlc_picture.h:153
Definition: vlc_picture.h:76
Definition: vlc_picture.h:103
struct picture_buffer_t picture_buffer_t
Definition: decoder_helpers.c:232
Definition: vlc_picture.h:95
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:158
private: android_video_context_t*
Definition: vlc_picture.h:100
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:241
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:448
#define vlc_assert(pred)
Run-time assertion.
Definition: vlc_common.h:267
Definition: vlc_picture.h:336
private: d3d9_video_context_t*
Definition: vlc_picture.h:98
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:413
struct picture_t * p_next
Next picture in a FIFO a pictures.
Definition: vlc_picture.h:161
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:323
Decoder context struct.
Definition: vlc_codec.h:577
static void picture_SwapUV(picture_t *picture)
Swap UV planes of a Tri Planars picture.
Definition: vlc_picture.h:354
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:402
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:226
#define VLC_API
Definition: fourcc_gen.c:31
Definition: vlc_picture.h:333
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:242
void vlc_video_context_Release(vlc_video_context *)
Definition: decoder_helpers.c:279
Definition: vlc_picture.h:335
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:291
Definition: vlc_picture.h:102
Definition: vlc_picture.h:97
vlc_video_context * vlc_video_context_Hold(vlc_video_context *)
Definition: decoder_helpers.c:273
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:261
void picture_Destroy(picture_t *picture)
Destroys a picture without references.
Definition: picture.c:338
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:268
plane_t p[(5)]
description of the planes
Definition: vlc_picture.h:134
#define VLC_USED
Definition: fourcc_gen.c:32
Definition: vlc_picture.h:69
void picture_Reset(picture_t *)
This function will reset a picture information (properties and quantizers).
Definition: picture.c:91