Update to VA API 0.26
[platform/upstream/libva.git] / src / va.h
1 /*
2  * Copyright (c) 2007 Intel Corporation. All Rights Reserved.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sub license, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  * 
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial portions
14  * of the Software.
15  * 
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
19  * IN NO EVENT SHALL PRECISION INSIGHT AND/OR ITS SUPPLIERS BE LIABLE FOR
20  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
21  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
22  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 /*
25  * Video Decode Acceleration API Specification
26  *
27  * Rev. 0.25
28  * <jonathan.bian@intel.com>
29  *
30  * Revision History:
31  * rev 0.10 (12/10/2006 Jonathan Bian) - Initial draft
32  * rev 0.11 (12/15/2006 Jonathan Bian) - Fixed some errors
33  * rev 0.12 (02/05/2007 Jonathan Bian) - Added VC-1 data structures for slice level decode
34  * rev 0.13 (02/28/2007 Jonathan Bian) - Added GetDisplay()
35  * rev 0.14 (04/13/2007 Jonathan Bian) - Fixed MPEG-2 PictureParameter structure, cleaned up a few funcs.
36  * rev 0.15 (04/20/2007 Jonathan Bian) - Overhauled buffer management
37  * rev 0.16 (05/02/2007 Jonathan Bian) - Added error codes and fixed some issues with configuration
38  * rev 0.17 (05/07/2007 Jonathan Bian) - Added H.264/AVC data structures for slice level decode.
39  * rev 0.18 (05/14/2007 Jonathan Bian) - Added data structures for MPEG-4 slice level decode 
40  *                                       and MPEG-2 motion compensation.
41  * rev 0.19 (08/06/2007 Jonathan Bian) - Removed extra type for bitplane data.
42  * rev 0.20 (08/08/2007 Jonathan Bian) - Added missing fields to VC-1 PictureParameter structure.
43  * rev 0.21 (08/20/2007 Jonathan Bian) - Added image and subpicture support.
44  * rev 0.22 (08/27/2007 Jonathan Bian) - Added support for chroma-keying and global alpha.
45  * rev 0.23 (09/11/2007 Jonathan Bian) - Fixed some issues with images and subpictures.
46  * rev 0.24 (09/18/2007 Jonathan Bian) - Added display attributes.
47  * rev 0.25 (10/18/2007 Jonathan Bian) - Changed to use IDs only for some types.
48  * rev 0.26 (11/07/2007 Waldo Bastian) - Change vaCreateBuffer semantics
49  *
50  * Acknowledgements:
51  *  Some concepts borrowed from XvMC and XvImage.
52  *  Thanks to Waldo Bastian, Matt Sottek and Austin Yuan at Intel for many valuable feedbacks.
53  */
54
55 #ifndef _VA_H_
56 #define _VA_H_
57
58 #ifdef __cplusplus
59 extern "C" {
60 #endif
61
62 /* 
63 Overview 
64
65 This API is intended to provide an interface between a video decode
66 application (client) and a hardware decode accelerator (server), to off-load 
67 video decode operations from the host to the hardware accelerator at various 
68 entry-points.
69
70 The basic operation steps are:
71
72 - Negotiate a mutually acceptable configuration with the server to lock
73   down profile, entrypoints, and other attributes that will not change on 
74   a frame-by-frame basis.
75 - Create a decode context which represents a "virtualized" hardware decode 
76   device
77 - Get and fill decode buffers with picture level, slice level and macroblock 
78   level data (depending on entrypoints)
79 - Pass the decode buffers to the server to decode the current frame
80
81 Initialization & Configuration Management 
82
83 - Find out supported profiles
84 - Find out entrypoints for a given profile
85 - Find out configuration attributes for a given profile/entrypoint pair
86 - Create a configuration for use by the decoder
87
88 */
89
90 typedef void* VADisplay;        /* window system dependent */
91
92 typedef int VAStatus;   /* Return status type from functions */
93 /* Values for the return status */
94 #define VA_STATUS_SUCCESS                       0x00000000
95 #define VA_STATUS_ERROR_OPERATION_FAILED        0x00000001
96 #define VA_STATUS_ERROR_ALLOCATION_FAILED       0x00000002
97 #define VA_STATUS_ERROR_INVALID_DISPLAY         0x00000003
98 #define VA_STATUS_ERROR_INVALID_CONFIG          0x00000004
99 #define VA_STATUS_ERROR_INVALID_CONTEXT         0x00000005
100 #define VA_STATUS_ERROR_INVALID_SURFACE         0x00000006
101 #define VA_STATUS_ERROR_INVALID_BUFFER          0x00000007
102 #define VA_STATUS_ERROR_INVALID_IMAGE           0x00000008
103 #define VA_STATUS_ERROR_INVALID_SUBPICTURE      0x00000009
104 #define VA_STATUS_ERROR_ATTR_NOT_SUPPORTED      0x0000000a
105 #define VA_STATUS_ERROR_MAX_NUM_EXCEEDED        0x0000000b
106 #define VA_STATUS_ERROR_UNSUPPORTED_PROFILE     0x0000000c
107 #define VA_STATUS_ERROR_UNSUPPORTED_ENTRYPOINT  0x0000000d
108 #define VA_STATUS_ERROR_UNSUPPORTED_RT_FORMAT   0x0000000e
109 #define VA_STATUS_ERROR_UNSUPPORTED_BUFFERTYPE  0x0000000f
110 #define VA_STATUS_ERROR_SURFACE_BUSY            0x00000010
111 #define VA_STATUS_ERROR_UNKNOWN                 0xFFFFFFFF
112
113 /*
114  * Returns a short english description of error_status
115  */
116 const char *vaErrorStr(VAStatus error_status);
117
118 /*
119  * Initialization:
120  * A display must be obtained by calling vaGetDisplay() before calling
121  * vaInitialize() and other functions. This connects the API to the 
122  * native window system.
123  * For X Windows, native_dpy would be from XOpenDisplay()
124  */
125 typedef void* NativeDisplay;    /* window system dependent */
126
127 VADisplay vaGetDisplay (
128     NativeDisplay native_dpy    /* implementation specific */
129 );
130
131 /*
132  * Initialize the library 
133  */
134 VAStatus vaInitialize (
135     VADisplay dpy,
136     int *major_version,  /* out */
137     int *minor_version   /* out */
138 );
139
140 /*
141  * After this call, all library internal resources will be cleaned up
142  */ 
143 VAStatus vaTerminate (
144     VADisplay dpy
145 );
146
147 /*
148  * vaQueryVendorString returns a pointer to a zero-terminated string
149  * describing some aspects of the VA implemenation on a specific    
150  * hardware accelerator. The format of the returned string is:      
151  * <vendorname>-<major_version>-<minor_version>-<addtional_info>    
152  * e.g. for the Intel GMA500 implementation, an example would be:   
153  * "IntelGMA500-1.0-0.2-patch3
154  */
155 const char *vaQueryVendorString (
156     VADisplay dpy
157 );
158
159 /* Currently defined profiles */
160 typedef enum
161 {
162     VAProfileMPEG2Simple                = 0,
163     VAProfileMPEG2Main                  = 1,
164     VAProfileMPEG4Simple                = 2,
165     VAProfileMPEG4AdvancedSimple        = 3,
166     VAProfileMPEG4Main                  = 4,
167     VAProfileH264Baseline               = 5,
168     VAProfileH264Main                   = 6,
169     VAProfileH264High                   = 7,
170     VAProfileVC1Simple                  = 8,
171     VAProfileVC1Main                    = 9,
172     VAProfileVC1Advanced                = 10,
173 } VAProfile;
174
175 /* 
176  *  Currently defined entrypoints 
177  */
178 typedef enum
179 {
180     VAEntrypointVLD             = 1,
181     VAEntrypointIZZ             = 2,
182     VAEntrypointIDCT            = 3,
183     VAEntrypointMoComp          = 4,
184     VAEntrypointDeblocking      = 5,
185 } VAEntrypoint;
186
187 /* Currently defined configuration attribute types */
188 typedef enum
189 {
190     VAConfigAttribRTFormat              = 0,
191     VAConfigAttribSpatialResidual       = 1,
192     VAConfigAttribSpatialClipping       = 2,
193     VAConfigAttribIntraResidual         = 3,
194     VAConfigAttribEncryption            = 4,
195 } VAConfigAttribType;
196
197 /*
198  * Configuration attributes
199  * If there is more than one value for an attribute, a default
200  * value will be assigned to the attribute if the client does not
201  * specify the attribute when creating a configuration
202  */
203 typedef struct _VAConfigAttrib {
204     VAConfigAttribType type;
205     unsigned int value; /* OR'd flags (bits) for this attribute */
206 } VAConfigAttrib;
207
208 /* attribute value for VAConfigAttribRTFormat */
209 #define VA_RT_FORMAT_YUV420     0x00000001      
210 #define VA_RT_FORMAT_YUV422     0x00000002
211 #define VA_RT_FORMAT_YUV444     0x00000004
212
213 /*
214  * if an attribute is not applicable for a given
215  * profile/entrypoint pair, then set the value to the following 
216  */
217 #define VA_ATTRIB_NOT_SUPPORTED 0x80000000
218
219 /* Get maximum number of profiles supported by the implementation */
220 int vaMaxNumProfiles (
221     VADisplay dpy
222 );
223
224 /* Get maximum number of entrypoints supported by the implementation */
225 int vaMaxNumEntrypoints (
226     VADisplay dpy
227 );
228
229 /* Get maximum number of attributs supported by the implementation */
230 int vaMaxNumConfigAttributes (
231     VADisplay dpy
232 );
233
234 /* 
235  * Query supported profiles 
236  * The caller must provide a "profile_list" array that can hold at
237  * least vaMaxNumProfile() entries. The actual number of profiles
238  * returned in "profile_list" is returned in "num_profile".
239  */
240 VAStatus vaQueryConfigProfiles (
241     VADisplay dpy,
242     VAProfile *profile_list,    /* out */
243     int *num_profiles           /* out */
244 );
245
246 /* 
247  * Query supported entrypoints for a given profile 
248  * The caller must provide an "entrypoint_list" array that can hold at
249  * least vaMaxNumEntrypoints() entries. The actual number of entrypoints 
250  * returned in "entrypoint_list" is returned in "num_entrypoints".
251  */
252 VAStatus vaQueryConfigEntrypoints (
253     VADisplay dpy,
254     VAProfile profile,
255     VAEntrypoint *entrypoint_list,      /* out */
256     int *num_entrypoints                /* out */
257 );
258
259 /* 
260  * Get attributes for a given profile/entrypoint pair 
261  * The caller must provide an "attrib_list" with all attributes to be 
262  * retrieved.  Upon return, the attributes in "attrib_list" have been 
263  * updated with their value.  Unknown attributes or attributes that are 
264  * not supported for the given profile/entrypoint pair will have their 
265  * value set to VA_ATTRIB_NOT_SUPPORTED
266  */
267 VAStatus vaGetConfigAttributes (
268     VADisplay dpy,
269     VAProfile profile,
270     VAEntrypoint entrypoint,
271     VAConfigAttrib *attrib_list, /* in/out */
272     int num_attribs
273 );
274
275 /* Generic ID type, can be re-typed for specific implementation */
276 typedef unsigned int VAGenericID;
277
278 typedef VAGenericID VAConfigID;
279
280 /* 
281  * Create a configuration for the decode pipeline 
282  * it passes in the attribute list that specifies the attributes it cares 
283  * about, with the rest taking default values.  
284  */
285 VAStatus vaCreateConfig (
286     VADisplay dpy,
287     VAProfile profile, 
288     VAEntrypoint entrypoint, 
289     VAConfigAttrib *attrib_list,
290     int num_attribs,
291     VAConfigID *config_id /* out */
292 );
293
294 /* 
295  * Free resources associdated with a given config 
296  */
297 VAStatus vaDestroyConfig (
298     VADisplay dpy,
299     VAConfigID config_id
300 );
301
302 /* 
303  * Query all attributes for a given configuration 
304  * The profile of the configuration is returned in "profile"
305  * The entrypoint of the configuration is returned in "entrypoint"
306  * The caller must provide an "attrib_list" array that can hold at least 
307  * vaMaxNumConfigAttributes() entries. The actual number of attributes 
308  * returned in "attrib_list" is returned in "num_attribs"
309  */
310 VAStatus vaQueryConfigAttributes (
311     VADisplay dpy,
312     VAConfigID config_id, 
313     VAProfile *profile,         /* out */
314     VAEntrypoint *entrypoint,   /* out */
315     VAConfigAttrib *attrib_list,/* out */
316     int *num_attribs            /* out */
317 );
318
319
320 /*
321  * Contexts and Surfaces
322  *
323  * Context represents a "virtual" video decode pipeline. Surfaces are render 
324  * targets for a given context. The data in the surfaces are not accessible  
325  * to the client and the internal data format of the surface is implementatin 
326  * specific. 
327  *
328  * Surfaces will be bound to a context when the context is created. Once
329  * a surface is bound to a given context, it can not be used to create  
330  * another context. The association is removed when the context is destroyed
331  * 
332  * Both contexts and surfaces are identified by unique IDs and its
333  * implementation specific internals are kept opaque to the clients
334  */
335
336 typedef VAGenericID VAContextID;
337
338 typedef VAGenericID VASurfaceID;
339
340 #define VA_INVALID_SURFACE      -1
341
342 /* 
343  * vaCreateSurfaces - Create an array of surfaces used for decode and display  
344  *  dpy: display
345  *  width: surface width
346  *  height: surface height
347  *  format: VA_RT_FORMAT_YUV420, VA_RT_FORMAT_YUV422 or VA_RT_FORMAT_YUV444
348  *  num_surfaces: number of surfaces to be created
349  *  surfaces: array of surfaces created upon return
350  */
351 VAStatus vaCreateSurfaces (
352     VADisplay dpy,
353     int width,
354     int height,
355     int format,
356     int num_surfaces,
357     VASurfaceID *surfaces       /* out */
358 );
359
360 /*
361  * vaDestroySurfaces - Destroy resources associated with surfaces. 
362  *  Surfaces can only be destroyed after the context associated has been 
363  *  destroyed.  
364  *  dpy: display
365  *  surfaces: array of surfaces to destroy
366  *  num_surfaces: number of surfaces in the array to be destroyed.
367  */
368 VAStatus vaDestroySurfaces (
369     VADisplay dpy,
370     VASurfaceID *surfaces,
371     int num_surfaces
372 );
373
374 #define VA_PROGRESSIVE 0x1
375 /*
376  * vaCreateContext - Create a context
377  *  dpy: display
378  *  config_id: configuration for the context
379  *  picture_width: coded picture width
380  *  picture_height: coded picture height
381  *  flag: any combination of the following:
382  *    VA_PROGRESSIVE (only progressive frame pictures in the sequence when set)
383  *  render_targets: render targets (surfaces) tied to the context
384  *  num_render_targets: number of render targets in the above array
385  *  context: created context id upon return
386  */
387 VAStatus vaCreateContext (
388     VADisplay dpy,
389     VAConfigID config_id,
390     int picture_width,
391     int picture_height,
392     int flag,
393     VASurfaceID *render_targets,
394     int num_render_targets,
395     VAContextID *context                /* out */
396 );
397
398 /*
399  * vaDestroyContext - Destroy a context 
400  *  dpy: display
401  *  context: context to be destroyed
402  */
403 VAStatus vaDestroyContext (
404     VADisplay dpy,
405     VAContextID context
406 );
407
408 /*
409  * Buffers 
410  * Buffers are used to pass various types of data from the
411  * client to the server. The server maintains a data store
412  * for each buffer created, and the client idenfies a buffer
413  * through a unique buffer id assigned by the server.
414  */
415
416 typedef VAGenericID VABufferID;
417
418 typedef enum
419 {
420     VAPictureParameterBufferType        = 0,
421     VAIQMatrixBufferType                = 1,
422     VABitPlaneBufferType                = 2,
423     VASliceGroupMapBufferType           = 3,
424     VASliceParameterBufferType          = 4,
425     VASliceDataBufferType               = 5,
426     VAMacroblockParameterBufferType     = 6,
427     VAResidualDataBufferType            = 7,
428     VADeblockingParameterBufferType     = 8,
429     VAImageBufferType                   = 9
430 } VABufferType;
431
432 /****************************
433  * MPEG-2 data structures
434  ****************************/
435  
436 /* MPEG-2 Picture Parameter Buffer */
437 /* 
438  * For each frame or field, and before any slice data, a single
439  * picture parameter buffer must be send.
440  */
441 typedef struct _VAPictureParameterBufferMPEG2
442 {
443     unsigned short horizontal_size;
444     unsigned short vertical_size;
445     VASurfaceID forward_reference_picture;
446     VASurfaceID backward_reference_picture;
447     /* meanings of the following fields are the same as in the standard */
448     int picture_coding_type;
449     int f_code; /* pack all four fcode into this */
450     union {
451         struct {
452             unsigned char intra_dc_precision            : 2; 
453             unsigned char picture_structure             : 2; 
454             unsigned char top_field_first               : 1; 
455             unsigned char frame_pred_frame_dct          : 1; 
456             unsigned char concealment_motion_vectors    : 1;
457             unsigned char q_scale_type                  : 1;
458             unsigned char intra_vlc_format              : 1;
459             unsigned char alternate_scan                : 1;
460             unsigned char repeat_first_field            : 1;
461             unsigned char progressive_frame             : 1;
462             unsigned char is_first_field                : 1; /* indicate whether the current field
463                                                               * is the first field for field picture
464                                                               */
465         };
466         unsigned int picture_coding_extension;
467     };
468 } VAPictureParameterBufferMPEG2;
469
470 /* MPEG-2 Inverse Quantization Matrix Buffer */
471 typedef struct _VAIQMatrixBufferMPEG2
472 {
473     int load_intra_quantiser_matrix;
474     int load_non_intra_quantiser_matrix;
475     int load_chroma_intra_quantiser_matrix;
476     int load_chroma_non_intra_quantiser_matrix;
477     unsigned char intra_quantiser_matrix[64];
478     unsigned char non_intra_quantiser_matrix[64];
479     unsigned char chroma_intra_quantiser_matrix[64];
480     unsigned char chroma_non_intra_quantiser_matrix[64];
481 } VAIQMatrixBufferMPEG2;
482
483 /* 
484  * There will be cases where the bitstream buffer will not have enough room to hold
485  * the data for the entire slice, and the following flags will be used in the slice
486  * parameter to signal to the server for the possible cases.
487  * If a slice parameter buffer and slice data buffer pair is sent to the server with 
488  * the slice data partially in the slice data buffer (BEGIN and MIDDLE cases below), 
489  * then a slice parameter and data buffer needs to be sent again to complete this slice. 
490  */
491 #define VA_SLICE_DATA_FLAG_ALL          0x00    /* whole slice is in the buffer */
492 #define VA_SLICE_DATA_FLAG_BEGIN        0x01    /* The beginning of the slice is in the buffer but the end if not */
493 #define VA_SLICE_DATA_FLAG_MIDDLE       0x02    /* Neither beginning nor end of the slice is in the buffer */
494 #define VA_SLICE_DATA_FLAG_END          0x04    /* end of the slice is in the buffer */
495
496 /* MPEG-2 Slice Parameter Buffer */
497 typedef struct _VASliceParameterBufferMPEG2
498 {
499     unsigned int slice_data_size;/* number of bytes in the slice data buffer for this slice */
500     unsigned int slice_data_offset;/* the offset to the first byte of slice data */
501     unsigned int slice_data_flag; /* see VA_SLICE_DATA_FLAG_XXX defintions */
502     unsigned int macroblock_offset;/* the offset to the first bit of MB from the first byte of slice data */
503     unsigned int slice_vertical_position;
504     int quantiser_scale_code;
505     int intra_slice_flag;
506 } VASliceParameterBufferMPEG2;
507
508 /* MPEG-2 Macroblock Parameter Buffer */
509 typedef struct _VAMacroblockParameterBufferMPEG2
510 {
511     unsigned short macroblock_address;
512     /* 
513      * macroblock_address (in raster scan order)
514      * top-left: 0
515      * bottom-right: picture-height-in-mb*picture-width-in-mb - 1
516      */
517     unsigned char macroblock_type;  /* see definition below */
518     union {
519         struct {
520             unsigned char frame_motion_type             : 2; 
521             unsigned char field_motion_type             : 2; 
522             unsigned char dct_type                      : 1; 
523         };
524         unsigned char macroblock_modes;
525     };
526     unsigned char motion_vertical_field_select; 
527     /* 
528      * motion_vertical_field_select:
529      * see section 6.3.17.2 in the spec
530      * only the lower 4 bits are used
531      * bit 0: first vector forward
532      * bit 1: first vector backward
533      * bit 2: second vector forward
534      * bit 3: second vector backward
535      */
536     short PMV[2][2][2]; /* see Table 7-7 in the spec */
537     unsigned short coded_block_pattern;
538     /* 
539      * The bitplanes for coded_block_pattern are described 
540      * in Figure 6.10-12 in the spec
541      */
542      
543     /* Number of skipped macroblocks after this macroblock */
544     unsigned short num_skipped_macroblocks;
545 } VAMacroblockParameterBufferMPEG2;
546
547 /* 
548  * OR'd flags for macroblock_type (section 6.3.17.1 in the spec)
549  */
550 #define VA_MB_TYPE_MOTION_FORWARD       0x02
551 #define VA_MB_TYPE_MOTION_BACKWARD      0x04
552 #define VA_MB_TYPE_MOTION_PATTERN       0x08
553 #define VA_MB_TYPE_MOTION_INTRA         0x10
554
555 /* 
556  * MPEG-2 Residual Data Buffer 
557  * For each macroblock, there wil be 64 shorts (16-bit) in the 
558  * residual data buffer
559  */
560
561 /****************************
562  * MPEG-4 Part 2 data structures
563  ****************************/
564  
565 /* MPEG-4 Picture Parameter Buffer */
566 /* 
567  * For each frame or field, and before any slice data, a single
568  * picture parameter buffer must be send.
569  */
570 typedef struct _VAPictureParameterBufferMPEG4
571 {
572     unsigned short vop_width;
573     unsigned short vop_height;
574     VASurfaceID forward_reference_picture;
575     VASurfaceID backward_reference_picture;
576     union {
577         struct {
578             unsigned char short_video_header            : 1; 
579             unsigned char chroma_format                 : 2; 
580             unsigned char interlaced                    : 1; 
581             unsigned char obmc_disable                  : 1; 
582             unsigned char sprite_enable                 : 2; 
583             unsigned char sprite_warping_accuracy       : 2; 
584             unsigned char quant_type                    : 1; 
585             unsigned char quarter_sample                : 1; 
586             unsigned char data_partitioned              : 1; 
587             unsigned char reversible_vlc                : 1; 
588         };
589         unsigned short vol_fields;
590     };
591     unsigned char no_of_sprite_warping_points;
592     short sprite_trajectory_du[3];
593     short sprite_trajectory_dv[3];
594     unsigned char quant_precision;
595     union {
596         struct {
597             unsigned char vop_coding_type               : 2; 
598             unsigned char backward_reference_vop_coding_type    : 2; 
599             unsigned char vop_rounding_type             : 1; 
600             unsigned char intra_dc_vlc_thr              : 3; 
601             unsigned char top_field_first               : 1; 
602             unsigned char alternate_vertical_scan_flag  : 1; 
603         };
604         unsigned short vop_fields;
605     };
606     unsigned char vop_fcode_forward;
607     unsigned char vop_fcode_backward;
608     /* short header related */
609     unsigned char num_gobs_in_vop;
610     unsigned char num_macroblocks_in_gob;
611     /* for direct mode prediction */
612     short TRB;
613     short TRD;
614 } VAPictureParameterBufferMPEG4;
615
616 /* MPEG-4 Inverse Quantization Matrix Buffer */
617 typedef struct _VAIQMatrixBufferMPEG4
618 {
619     int load_intra_quant_mat;
620     int load_non_intra_quant_mat;
621     unsigned char intra_quant_mat[64];
622     unsigned char non_intra_quant_mat[64];
623 } VAIQMatrixBufferMPEG4;
624
625 /* MPEG-4 Slice Parameter Buffer */
626 typedef struct _VASliceParameterBufferMPEG4
627 {
628     unsigned int slice_data_size;/* number of bytes in the slice data buffer for this slice */
629     unsigned int slice_data_offset;/* the offset to the first byte of slice data */
630     unsigned int slice_data_flag; /* see VA_SLICE_DATA_FLAG_XXX defintions */
631     unsigned int macroblock_offset;/* the offset to the first bit of MB from the first byte of slice data */
632     unsigned int macroblock_number;
633     int quant_scale;
634 } VASliceParameterBufferMPEG4;
635
636 /*
637  VC-1 data structures
638 */
639
640 typedef enum   /* see 7.1.1.32 */
641 {
642     VAMvMode1Mv                        = 0,
643     VAMvMode1MvHalfPel                 = 1,
644     VAMvMode1MvHalfPelBilinear         = 2,
645     VAMvModeMixedMv                    = 3,
646     VAMvModeIntensityCompensation      = 4 
647 } VAMvModeVC1;
648
649 /* VC-1 Picture Parameter Buffer */
650 /* 
651  * For each picture, and before any slice data, a picture parameter
652  * buffer must be send. Multiple picture parameter buffers may be
653  * sent for a single picture. In that case picture parameters will
654  * apply to all slice data that follow it until a new picture
655  * parameter buffer is sent.
656  *
657  * Notes:
658  *   pic_quantizer_type should be set to the applicable quantizer
659  *   type as defined by QUANTIZER (J.1.19) and either
660  *   PQUANTIZER (7.1.1.8) or PQINDEX (7.1.1.6)
661  */
662 typedef struct _VAPictureParameterBufferVC1
663 {
664     VASurfaceID forward_reference_picture;
665     VASurfaceID backward_reference_picture;
666     /* if out-of-loop post-processing is done on the render
667        target, then we need to keep the in-loop decoded 
668        picture as a reference picture */
669     VASurfaceID inloop_decoded_picture;
670
671     /* sequence layer for AP or meta data for SP and MP */
672     union {
673         struct {
674             unsigned char interlace     : 1; /* SEQUENCE_LAYER::INTERLACE */
675             unsigned char syncmarker    : 1;/* METADATA::SYNCMARKER */
676             unsigned char overlap       : 1;/* METADATA::OVERLAP */
677         };
678         unsigned char sequence_fields;
679     };
680
681     unsigned short coded_width;         /* ENTRY_POINT_LAYER::CODED_WIDTH */
682     unsigned short coded_height;        /* ENTRY_POINT_LAYER::CODED_HEIGHT */
683     unsigned char closed_entry;         /* ENTRY_POINT_LAYER::CLOSED_ENTRY */
684     unsigned char broken_link;          /* ENTRY_POINT_LAYER::BROKEN_LINK */
685     unsigned char loopfilter;           /* ENTRY_POINT_LAYER::LOOPFILTER */
686     unsigned char conditional_overlap_flag; /* ENTRY_POINT_LAYER::CONDOVER */
687     unsigned char fast_uvmc_flag;       /* ENTRY_POINT_LAYER::FASTUVMC */
688     union {
689         struct {
690             unsigned char range_mapping_luma_flag:      1; /* ENTRY_POINT_LAYER::RANGE_MAPY_FLAG */
691             unsigned char range_mapping_luma:           3; /* ENTRY_POINT_LAYER::RANGE_MAPY */
692             unsigned char range_mapping_chroma_flag:    1; /* ENTRY_POINT_LAYER::RANGE_MAPUV_FLAG */
693             unsigned char range_mapping_chroma:         3; /* ENTRY_POINT_LAYER::RANGE_MAPUV */
694         };
695         unsigned char range_mapping_fields;
696     };
697
698     unsigned char b_picture_fraction;   /* PICTURE_LAYER::BFRACTION */
699     unsigned char cbp_table;            /* PICTURE_LAYER::CBPTAB/ICBPTAB */
700     unsigned char mb_mode_table;        /* PICTURE_LAYER::MBMODETAB */
701     unsigned char range_reduction_frame;/* PICTURE_LAYER::RANGEREDFRM */
702     unsigned char rounding_control;     /* PICTURE_LAYER::RNDCTRL */
703     unsigned char post_processing;      /* PICTURE_LAYER::POSTPROC */
704     unsigned char picture_resolution_index;     /* PICTURE_LAYER::RESPIC */
705     unsigned char luma_scale;           /* PICTURE_LAYER::LUMSCALE */
706     unsigned char luma_shift;           /* PICTURE_LAYER::LUMSHIFT */
707     union {
708         struct {
709             unsigned char picture_type  : 2;    /* PICTURE_LAYER::PTYPE */
710             unsigned char frame_coding_mode     : 3;/* PICTURE_LAYER::FCM */
711             unsigned char top_field_first       : 1;/* PICTURE_LAYER::TFF */
712             unsigned char is_first_field        : 1; /* set to 1 if it is the first field */
713             unsigned char intensity_compensation: 1;/* PICTURE_LAYER::INTCOMP */
714         };
715         unsigned char picture_fields;
716     };
717     union {
718         struct {
719             unsigned char mv_type_mb    : 1;    /* PICTURE::MVTYPEMB */
720             unsigned char direct_mb     : 1;    /* PICTURE::DIRECTMB */
721             unsigned char skip_mb       : 1;    /* PICTURE::SKIPMB */
722             unsigned char field_tx      : 1;    /* PICTURE::FIELDTX */
723             unsigned char forward_mb    : 1;    /* PICTURE::FORWARDMB */
724             unsigned char ac_pred       : 1;    /* PICTURE::ACPRED */
725             unsigned char overflags     : 1;    /* PICTURE::OVERFLAGS */
726         };
727         unsigned char raw_coding_flag;
728     };
729     union {
730         struct {
731             unsigned char bp_mv_type_mb   : 1;    /* PICTURE::MVTYPEMB */
732             unsigned char bp_direct_mb    : 1;    /* PICTURE::DIRECTMB */
733             unsigned char bp_skip_mb      : 1;    /* PICTURE::SKIPMB */  
734             unsigned char bp_field_tx     : 1;    /* PICTURE::FIELDTX */ 
735             unsigned char bp_forward_mb   : 1;    /* PICTURE::FORWARDMB */
736             unsigned char bp_ac_pred      : 1;    /* PICTURE::ACPRED */   
737             unsigned char bp_overflags    : 1;    /* PICTURE::OVERFLAGS */
738         };
739         unsigned char bitplane_present_flag; /* signal what bitplane is being passed via the bitplane buffer */
740     };
741     union {
742         struct {
743             unsigned char reference_distance_flag : 1;/* PICTURE_LAYER::REFDIST_FLAG */
744             unsigned char reference_distance    : 1;/* PICTURE_LAYER::REFDIST */
745             unsigned char num_reference_pictures: 1;/* PICTURE_LAYER::NUMREF */
746             unsigned char reference_field_pic_indicator : 1;/* PICTURE_LAYER::REFFIELD */
747         };
748         unsigned short reference_fields;
749     };
750     union {
751         struct {
752             VAMvModeVC1 mv_mode              : 3; /* PICTURE_LAYER::MVMODE */
753             VAMvModeVC1 mv_mode2             : 3; /* PICTURE_LAYER::MVMODE2 */
754             unsigned char mv_table           : 3; /* PICTURE_LAYER::MVTAB/IMVTAB */
755             unsigned char two_mv_block_pattern_table: 2; /* PICTURE_LAYER::2MVBPTAB */
756             unsigned char four_mv_switch     : 1; /* PICTURE_LAYER::4MVSWITCH */
757             unsigned char four_mv_block_pattern_table : 2; /* PICTURE_LAYER::4MVBPTAB */
758             unsigned char extended_mv_flag   : 1; /* ENTRY_POINT_LAYER::EXTENDED_MV */
759             unsigned char extended_mv_range  : 2; /* PICTURE_LAYER::MVRANGE */
760             unsigned char extended_dmv_flag  : 1; /* ENTRY_POINT_LAYER::EXTENDED_DMV */
761             unsigned char extended_dmv_range : 2; /* PICTURE_LAYER::DMVRANGE */
762         };
763         unsigned int mv_fields;
764     };
765     union {
766         struct {
767             unsigned char dquant        : 2;    /* ENTRY_POINT_LAYER::DQUANT */
768             unsigned char half_qp       : 1;    /* PICTURE_LAYER::HALFQP */
769             unsigned char pic_quantizer_scale : 5;/* PICTURE_LAYER::PQUANT */
770             unsigned char pic_quantizer_type : 1;/* PICTURE_LAYER::PQUANTIZER */
771             unsigned char dq_frame      : 1;    /* VOPDQUANT::DQUANTFRM */
772             unsigned char dq_profile    : 2;    /* VOPDQUANT::DQPROFILE */
773             unsigned char dq_sb_edge    : 2;    /* VOPDQUANT::DQSBEDGE */
774             unsigned char dq_db_edge    : 2;    /* VOPDQUANT::DQDBEDGE */
775             unsigned char dq_binary_level : 1;  /* VOPDQUANT::DQBILEVEL */
776             unsigned char alt_pic_quantizer : 5;/* VOPDQUANT::ALTPQUANT */
777         };
778         unsigned long pic_quantizer_fields;
779     };
780     union {
781         struct {
782             unsigned char variable_sized_transform_flag : 1;/* ENTRY_POINT_LAYER::VSTRANSFORM */
783             unsigned char mb_level_transform_type_flag  : 1;/* PICTURE_LAYER::TTMBF */
784             unsigned char frame_level_transform_type    : 2;/* PICTURE_LAYER::TTFRM */
785             unsigned char transform_ac_codingset_idx1   : 2;/* PICTURE_LAYER::TRANSACFRM */
786             unsigned char transform_ac_codingset_idx2   : 2;/* PICTURE_LAYER::TRANSACFRM2 */
787             unsigned char intra_transform_dc_table      : 1;/* PICTURE_LAYER::TRANSDCTAB */
788         };
789         unsigned short transform_fields;
790     };
791 } VAPictureParameterBufferVC1;
792
793 /* VC-1 Bitplane Buffer 
794 There will be at most three bitplanes coded in any picture header. To send 
795 the bitplane data more efficiently, each byte is divided in two nibbles, with
796 each nibble carrying three bitplanes for one macroblock.  The following table
797 shows the bitplane data arrangement within each nibble based on the picture
798 type.
799
800 Picture Type    Bit3            Bit2            Bit1            Bit0
801 I or BI                         OVERFLAGS       ACPRED          FIELDTX
802 P                               MYTYPEMB        SKIPMB          DIRECTMB
803 B                               FORWARDMB       SKIPMB          DIRECTMB
804
805 Within each byte, the lower nibble is for the first MB and the upper nibble is 
806 for the second MB.  E.g. the lower nibble of the first byte in the bitplane
807 buffer is for Macroblock #1 and the upper nibble of the first byte is for 
808 Macroblock #2 in the first row.
809 */
810
811 /* VC-1 Slice Parameter Buffer */
812 typedef struct _VASliceParameterBufferVC1
813 {
814     unsigned int slice_data_size;/* number of bytes in the slice data buffer for this slice */
815     unsigned int slice_data_offset;/* the offset to the first byte of slice data */
816     unsigned int slice_data_flag; /* see VA_SLICE_DATA_FLAG_XXX defintions */
817     unsigned int macroblock_offset;/* the offset to the first bit of MB from the first byte of slice data */
818     unsigned int slice_vertical_position;
819 } VASliceParameterBufferVC1;
820
821 /* VC-1 Slice Data Buffer */
822 /* 
823 This is simplely a buffer containing raw bit-stream bytes 
824 */
825
826 /****************************
827  * H.264/AVC data structures
828  ****************************/
829
830 typedef struct _VAPictureH264
831 {
832     VASurfaceID picture_id;
833     unsigned int flags;
834     unsigned int TopFieldOrderCnt;
835     unsigned int BottomFieldOrderCnt;
836 } VAPictureH264;
837 /* flags in VAPictureH264 could be OR of the following */
838 #define VA_PICTURE_H264_INVALID                 0x00000001
839 #define VA_PICTURE_H264_TOP_FIELD               0x00000002
840 #define VA_PICTURE_H264_BOTTOM_FIELD            0x00000004
841 #define VA_PICTURE_H264_SHORT_TERM_REFERENCE    0x00000008
842 #define VA_PICTURE_H264_LONG_TERM_REFERENCE     0x00000010
843
844 /* H.264 Picture Parameter Buffer */
845 /* 
846  * For each picture, and before any slice data, a single
847  * picture parameter buffer must be send.
848  */
849 typedef struct _VAPictureParameterBufferH264
850 {
851     VAPictureH264 CurrPic;
852     VAPictureH264 ReferenceFrames[16];  /* in DPB */
853     unsigned short picture_width_in_mbs_minus1;
854     unsigned short picture_height_in_mbs_minus1;
855     unsigned char bit_depth_luma_minus8;
856     unsigned char bit_depth_chroma_minus8;
857     unsigned char num_ref_frames;
858     union {
859         struct {
860             unsigned char chroma_format_idc                     : 2; 
861             unsigned char residual_colour_transform_flag        : 1; 
862             unsigned char frame_mbs_only_flag                   : 1; 
863             unsigned char mb_adaptive_frame_field_flag          : 1; 
864             unsigned char direct_8x8_inference_flag             : 1; 
865             unsigned char MinLumaBiPredSize8x8                  : 1; /* see A.3.3.2 */
866         };
867         unsigned char seq_fields;
868     };
869     unsigned char num_slice_groups_minus1;
870     unsigned char slice_group_map_type;
871     signed char pic_init_qp_minus26;
872     signed char chroma_qp_index_offset;
873     signed char second_chroma_qp_index_offset;
874     union {
875         struct {
876             unsigned char entropy_coding_mode_flag      : 1;
877             unsigned char weighted_pred_flag            : 1;
878             unsigned char weighted_bipred_idc           : 2;
879             unsigned char transform_8x8_mode_flag       : 1;
880             unsigned char field_pic_flag                : 1;
881             unsigned char constrained_intra_pred_flag   : 1;
882         };
883         unsigned char pic_fields;
884     };
885     unsigned short frame_num;
886 } VAPictureParameterBufferH264;
887
888 /* H.264 Inverse Quantization Matrix Buffer */
889 typedef struct _VAIQMatrixBufferH264
890 {
891     unsigned char ScalingList4x4[6][16];
892     unsigned char ScalingList8x8[2][64];
893 } VAIQMatrixBufferH264;
894
895 /* 
896  * H.264 Slice Group Map Buffer 
897  * When VAPictureParameterBufferH264::num_slice_group_minus1 is not equal to 0,
898  * A slice group map buffer should be sent for each picture if required. The buffer
899  * is sent only when there is a change in the mapping values.
900  * The slice group map buffer map "map units" to slice groups as specified in 
901  * section 8.2.2 of the H.264 spec. The buffer will contain one byte for each macroblock 
902  * in raster scan order
903  */ 
904
905 /* H.264 Slice Parameter Buffer */
906 typedef struct _VASliceParameterBufferH264
907 {
908     unsigned int slice_data_size;/* number of bytes in the slice data buffer for this slice */
909     unsigned int slice_data_offset;/* the offset to first byte of slice data */
910     unsigned int slice_data_flag; /* see VA_SLICE_DATA_FLAG_XXX defintions */
911     unsigned short slice_data_bit_offset; /* bit offset in the first byte of valid data */
912     unsigned short first_mb_in_slice;
913     unsigned char slice_type;
914     unsigned char direct_spatial_mv_pred_flag;
915     unsigned char num_ref_idx_l0_active_minus1;
916     unsigned char num_ref_idx_l1_active_minus1;
917     unsigned char cabac_init_idc;
918     char slice_qp_delta;
919     unsigned char disable_deblocking_filter_idc;
920     char slice_alpha_c0_offset_div2;
921     char slice_beta_offset_div2;
922     VAPictureH264 RefPicList0[32];      /* See 8.2.4.2 */
923     VAPictureH264 RefPicList1[32];      /* See 8.2.4.2 */
924     unsigned char luma_log2_weight_denom;
925     unsigned char chroma_log2_weight_denom;
926     unsigned char luma_weight_l0_flag;
927     short luma_weight_l0[32];
928     short luma_offset_l0[32];
929     unsigned char chroma_weight_l0_flag;
930     short chroma_weight_l0[32][2];
931     short chroma_offset_l0[32][2];
932     unsigned char luma_weight_l1_flag;
933     short luma_weight_l1[32];
934     short luma_offset_l1[32];
935     unsigned char chroma_weight_l1_flag;
936     short chroma_weight_l1[32][2];
937     short chroma_offset_l1[32][2];
938 } VASliceParameterBufferH264;
939
940 /* Buffer functions */
941
942 /*
943  * Creates a buffer for "num_elements" elements of "size" bytes and 
944  * initalize with "data".
945  * if "data" is null, then the contents of the buffer data store
946  * are undefined.
947  * Basically there are two ways to get buffer data to the server side. One is 
948  * to call vaCreateBuffer() with a non-null "data", which results the data being
949  * copied to the data store on the server side.  A different method that 
950  * eliminates this copy is to pass null as "data" when calling vaCreateBuffer(),
951  * and then use vaMapBuffer() to map the data store from the server side to the
952  * client address space for access.
953  *  Note: image buffers are created by the library, not the client. Please see 
954  *        vaCreateImage on how image buffers are managed.
955  */
956 VAStatus vaCreateBuffer (
957     VADisplay dpy,
958     VAContextID context,
959     VABufferType type,  /* in */
960     unsigned int size,  /* in */
961     unsigned int num_elements, /* in */
962     void *data,         /* in */
963     VABufferID *buf_id  /* out */
964 );
965
966 /*
967  * Convey to the server how many valid elements are in the buffer. 
968  * e.g. if multiple slice parameters are being held in a single buffer,
969  * this will communicate to the server the number of slice parameters
970  * that are valid in the buffer.
971  */
972 VAStatus vaBufferSetNumElements (
973     VADisplay dpy,
974     VABufferID buf_id,  /* in */
975     unsigned int num_elements /* in */
976 );
977
978 /*
979  * Map data store of the buffer into the client's address space
980  * vaCreateBuffer() needs to be called with "data" set to NULL before
981  * calling vaMapBuffer()
982  */
983 VAStatus vaMapBuffer (
984     VADisplay dpy,
985     VABufferID buf_id,  /* in */
986     void **pbuf         /* out */
987 );
988
989 /*
990  * After client making changes to a mapped data store, it needs to
991  * "Unmap" it to let the server know that the data is ready to be
992  * consumed by the server
993  */
994 VAStatus vaUnmapBuffer (
995     VADisplay dpy,
996     VABufferID buf_id   /* in */
997 );
998
999 /*
1000  * After this call, the buffer is deleted and this buffer_id is no longer valid
1001  * Only call this if the buffer is not going to be passed to vaRenderBuffer
1002  */
1003 VAStatus vaDestroyBuffer (
1004     VADisplay dpy,
1005     VABufferID buffer_id
1006 );
1007
1008 /*
1009 Render (Decode) Pictures
1010
1011 A picture represents either a frame or a field.
1012
1013 The Begin/Render/End sequence sends the decode buffers to the server
1014 */
1015
1016 /*
1017  * Get ready to decode a picture to a target surface
1018  */
1019 VAStatus vaBeginPicture (
1020     VADisplay dpy,
1021     VAContextID context,
1022     VASurfaceID render_target
1023 );
1024
1025 /* 
1026  * Send decode buffers to the server.
1027  * Buffers are automatically destroyed afterwards
1028  */
1029 VAStatus vaRenderPicture (
1030     VADisplay dpy,
1031     VAContextID context,
1032     VABufferID *buffers,
1033     int num_buffers
1034 );
1035
1036 /* 
1037  * Make the end of rendering for a picture. 
1038  * The server should start processing all pending operations for this 
1039  * surface. This call is non-blocking. The client can start another 
1040  * Begin/Render/End sequence on a different render target.
1041  */
1042 VAStatus vaEndPicture (
1043     VADisplay dpy,
1044     VAContextID context
1045 );
1046
1047 /*
1048
1049 Synchronization 
1050
1051 */
1052
1053 /* 
1054  * This function blocks until all pending operations on the render target
1055  * have been completed.  Upon return it is safe to use the render target for a 
1056  * different picture. 
1057  */
1058 VAStatus vaSyncSurface (
1059     VADisplay dpy,
1060     VAContextID context,
1061     VASurfaceID render_target
1062 );
1063
1064 typedef enum
1065 {
1066     VASurfaceRendering  = 0, /* Rendering in progress */ 
1067     VASurfaceDisplaying = 1, /* Displaying in progress (not safe to render into it) */ 
1068                              /* this status is useful if surface is used as the source */
1069                              /* of an overlay */
1070     VASurfaceReady      = 2  /* not being rendered or displayed */
1071 } VASurfaceStatus;
1072
1073 /*
1074  * Find out any pending ops on the render target 
1075  */
1076 VAStatus vaQuerySurfaceStatus (
1077     VADisplay dpy,
1078     VASurfaceID render_target,
1079     VASurfaceStatus *status     /* out */
1080 );
1081
1082
1083 /*
1084  * Copies the surface to a buffer
1085  * The stride of the surface will be stored in *stride
1086  * Caller should free the returned buffer with free() when done. 
1087  */
1088 VAStatus vaDbgCopySurfaceToBuffer(VADisplay dpy,
1089     VASurfaceID surface,
1090     void **buffer, /* out */
1091     unsigned int *stride /* out */
1092 );
1093
1094 /*
1095  * Images and Subpictures
1096  * VAImage is used to either get the surface data to client memory, or 
1097  * to copy image data in client memory to a surface. 
1098  * Both images, subpictures and surfaces follow the same 2D coordinate system where origin 
1099  * is at the upper left corner with positive X to the right and positive Y down
1100  */
1101 #define MAKEFOURCC(ch0, ch1, ch2, ch3) \
1102     ((unsigned long)(unsigned char) (ch0) | ((unsigned long)(unsigned char) (ch1) << 8) | \
1103     ((unsigned long)(unsigned char) (ch2) << 16) | ((unsigned long)(unsigned char) (ch3) << 24 ))
1104
1105 /* a few common FourCCs */
1106 #define VA_FOURCC_NV12          0x3231564E
1107 #define VA_FOURCC_AI44          0x34344149
1108 #define VA_FOURCC_RGBA          0x41424752
1109
1110 /* byte order */
1111 #define VA_LSB_FIRST            1
1112 #define VA_MSB_FIRST            2
1113
1114 typedef struct _VAImageFormat
1115 {
1116     unsigned int        fourcc;
1117     unsigned int        byte_order; /* VA_LSB_FIRST, VA_MSB_FIRST */
1118     unsigned int        bits_per_pixel;
1119     /* for RGB formats */
1120     unsigned int        depth; /* significant bits per pixel */
1121     unsigned int        red_mask;
1122     unsigned int        green_mask;
1123     unsigned int        blue_mask;
1124     unsigned int        alpha_mask;
1125 } VAImageFormat;
1126
1127 typedef VAGenericID VAImageID;
1128
1129 typedef struct _VAImage
1130 {
1131     VAImageID           image_id; /* uniquely identify this image */
1132     VAImageFormat       format;
1133     VABufferID          buf;    /* image data buffer */
1134     /*
1135      * Image data will be stored in a buffer of type VAImageBufferType to facilitate
1136      * data store on the server side for optimal performance. The buffer will be 
1137      * created by the CreateImage function, and proper storage allocated based on the image
1138      * size and format. This buffer is managed by the library implementation, and 
1139      * accessed by the client through the buffer Map/Unmap functions.
1140      */
1141     unsigned short      width; 
1142     unsigned short      height;
1143     unsigned int        data_size;
1144     unsigned int        num_planes;     /* can not be greater than 3 */
1145     /* 
1146      * An array indicating the scanline pitch in bytes for each plane.
1147      * Each plane may have a different pitch. Maximum 3 planes for planar formats
1148      */
1149     unsigned int        pitches[3];
1150     /* 
1151      * An array indicating the byte offset from the beginning of the image data 
1152      * to the start of each plane.
1153      */
1154     unsigned int        offsets[3];
1155
1156     /* The following fields are only needed for paletted formats */
1157     int num_palette_entries;   /* set to zero for non-palette images */
1158     /* 
1159      * Each component is one byte and entry_bytes indicates the number of components in 
1160      * each entry (eg. 3 for YUV palette entries). set to zero for non-palette images   
1161      */
1162     int entry_bytes; 
1163     /*
1164      * An array of ascii characters describing the order of the components within the bytes.
1165      * Only entry_bytes characters of the string are used.
1166      */
1167     char component_order[4];
1168 } VAImage;
1169
1170 /* Get maximum number of image formats supported by the implementation */
1171 int vaMaxNumImageFormats (
1172     VADisplay dpy
1173 );
1174
1175 /* 
1176  * Query supported image formats 
1177  * The caller must provide a "format_list" array that can hold at
1178  * least vaMaxNumImageFormats() entries. The actual number of formats
1179  * returned in "format_list" is returned in "num_formats".
1180  */
1181 VAStatus vaQueryImageFormats (
1182     VADisplay dpy,
1183     VAImageFormat *format_list, /* out */
1184     int *num_formats            /* out */
1185 );
1186
1187 /* 
1188  * Create a VAImage structure
1189  * The width and height fields returned in the VAImage structure may get 
1190  * enlarged for some YUV formats. Upon return from this function, 
1191  * image->buf has been created and proper storage allocated by the library. 
1192  * The client can access the image through the Map/Unmap calls.
1193  */
1194 VAStatus vaCreateImage (
1195     VADisplay dpy,
1196     VAImageFormat *format,
1197     int width,
1198     int height,
1199     VAImage *image      /* out */
1200 );
1201
1202 /*
1203  * Should call DestroyImage before destroying the surface it is bound to
1204  */
1205 VAStatus vaDestroyImage (
1206     VADisplay dpy,
1207     VAImageID image
1208 );
1209
1210 VAStatus vaSetImagePalette (
1211     VADisplay dpy,
1212     VAImageID image,
1213     /* 
1214      * pointer to an array holding the palette data.  The size of the array is 
1215      * num_palette_entries * entry_bytes in size.  The order of the components 
1216      * in the palette is described by the component_order in VAImage struct    
1217      */
1218     unsigned char *palette 
1219 );
1220
1221 /*
1222  * Retrive surface data into a VAImage
1223  * Image must be in a format supported by the implementation
1224  */
1225 VAStatus vaGetImage (
1226     VADisplay dpy,
1227     VASurfaceID surface,
1228     int x,      /* coordinates of the upper left source pixel */
1229     int y,
1230     unsigned int width, /* width and height of the region */
1231     unsigned int height,
1232     VAImageID image
1233 );
1234
1235 /*
1236  * Copy data from a VAImage to a surface
1237  * Image must be in a format supported by the implementation
1238  * Returns a VA_STATUS_ERROR_SURFACE_BUSY if the surface
1239  * shouldn't be rendered into when this is called
1240  * The source and destionation width and height are the same and
1241  * no scaling is performed with this operation.
1242  */
1243 VAStatus vaPutImage (
1244     VADisplay dpy,
1245     VASurfaceID surface,
1246     VAImageID image,
1247     int src_x,
1248     int src_y,
1249     unsigned int width,
1250     unsigned int height,
1251     int dest_x,
1252     int dest_y
1253 );
1254
1255 /*
1256  * Subpictures 
1257  * Subpicture is a special type of image that can be blended 
1258  * with a surface during vaPutSurface(). Subpicture can be used to render
1259  * DVD sub-titles or closed captioning text etc.  
1260  */
1261
1262 typedef VAGenericID VASubpictureID;
1263
1264 /* Get maximum number of subpicture formats supported by the implementation */
1265 int vaMaxNumSubpictureFormats (
1266     VADisplay dpy
1267 );
1268
1269 /* flags for subpictures */
1270 #define VA_SUBPICTURE_CHROMA_KEYING     0x0001
1271 #define VA_SUBPICTURE_GLOBAL_ALPHA      0x0002
1272 /* 
1273  * Query supported subpicture formats 
1274  * The caller must provide a "format_list" array that can hold at
1275  * least vaMaxNumSubpictureFormats() entries. The flags arrary holds the flag 
1276  * for each format to indicate additional capabilities for that format. The actual 
1277  * number of formats returned in "format_list" is returned in "num_formats".
1278  *  flags: returned value to indicate addtional capabilities
1279  *         VA_SUBPICTURE_CHROMA_KEYING - supports chroma-keying
1280  *         VA_SUBPICTURE_GLOBAL_ALPHA - supports global alpha  
1281  */
1282 VAStatus vaQuerySubpictureFormats (
1283     VADisplay dpy,
1284     VAImageFormat *format_list, /* out */
1285     unsigned int *flags,        /* out */
1286     unsigned int *num_formats   /* out */
1287 );
1288
1289 /* 
1290  * Subpictures are created with an image associated. 
1291  */
1292 VAStatus vaCreateSubpicture (
1293     VADisplay dpy,
1294     VAImageID image,
1295     VASubpictureID *subpicture  /* out */
1296 );
1297
1298 /*
1299  * Destroy the subpicture before destroying the image it is assocated to
1300  */
1301 VAStatus vaDestroySubpicture (
1302     VADisplay dpy,
1303     VASubpictureID subpicture
1304 );
1305
1306 /* 
1307  * Bind an image to the subpicture. This image will now be associated with 
1308  * the subpicture instead of the one at creation.
1309  */
1310 VAStatus vaSetSubpictureImage (
1311     VADisplay dpy,
1312     VASubpictureID subpicture,
1313     VAImageID image
1314 );
1315
1316 VAStatus vaSetSubpicturePalette (
1317     VADisplay dpy,
1318     VASubpictureID subpicture,
1319     /* 
1320      * pointer to an array holding the palette data.  The size of the array is 
1321      * num_palette_entries * entry_bytes in size.  The order of the components
1322      * in the palette is described by the component_order in VAImage struct
1323      */
1324     unsigned char *palette 
1325 );
1326
1327 /*
1328  * If chromakey is enabled, then the area where the source value falls within
1329  * the chromakey [min, max] range is transparent
1330  * The chromakey component format is the following:
1331  *  For RGB: [0:7] Red [8:15] Blue [16:23] Green   
1332  *  For YUV: [0:7] V [8:15] U [16:23] Y
1333  * The chromakey mask can be used to mask out certain components for chromakey
1334  * comparision
1335  */
1336 VAStatus vaSetSubpictureChromakey (
1337     VADisplay dpy,
1338     VASubpictureID subpicture,
1339     unsigned int chromakey_min,
1340     unsigned int chromakey_max,
1341     unsigned int chromakey_mask
1342 );
1343
1344 /*
1345  * Global alpha value is between 0 and 1. A value of 1 means fully opaque and 
1346  * a value of 0 means fully transparent. If per-pixel alpha is also specified then
1347  * the overall alpha is per-pixel alpha multiplied by the global alpha
1348  */
1349 VAStatus vaSetSubpictureGlobalAlpha (
1350     VADisplay dpy,
1351     VASubpictureID subpicture,
1352     float global_alpha 
1353 );
1354
1355 /*
1356  * vaAssociateSubpicture associates the subpicture with target_surfaces.
1357  * It defines the region mapping between the subpicture and the target  
1358  * surfaces through source and destination rectangles (with the same width and height).
1359  * Both will be displayed at the next call to vaPutSurface.  Additional
1360  * associations before the call to vaPutSurface simply overrides the association.
1361  */
1362 VAStatus vaAssociateSubpicture (
1363     VADisplay dpy,
1364     VASubpictureID subpicture,
1365     VASurfaceID *target_surfaces,
1366     int num_surfaces,
1367     short src_x, /* upper left offset in subpicture */
1368     short src_y,
1369     short dest_x, /* upper left offset in surface */
1370     short dest_y,
1371     unsigned short width,
1372     unsigned short height,
1373     /*
1374      * whether to enable chroma-keying or global-alpha
1375      * see VA_SUBPICTURE_XXX values
1376      */
1377     unsigned int flags
1378 );
1379
1380 /*
1381  * vaDeassociateSubpicture removes the association of the subpicture with target_surfaces.
1382  */
1383 VAStatus vaDeassociateSubpicture (
1384     VADisplay dpy,
1385     VASubpictureID subpicture,
1386     VASurfaceID *target_surfaces,
1387     int num_surfaces
1388 );
1389
1390 typedef struct _VARectangle
1391 {
1392     short x;
1393     short y;
1394     unsigned short width;
1395     unsigned short height;
1396 } VARectangle;
1397
1398 /*
1399  * Display attributes
1400  * Display attributes are used to control things such as contrast, hue, saturation,
1401  * brightness etc. in the rendering process.  The application can query what
1402  * attributes are supported by the driver, and then set the appropriate attributes
1403  * before calling vaPutSurface()
1404  */
1405
1406 /* Currently defined display attribute types */
1407 typedef enum
1408 {
1409     VADisplayAttribBrightness           = 0,
1410     VADisplayAttribContrast             = 1,
1411     VADisplayAttribHue                  = 2,
1412     VADisplayAttribSaturation           = 3,
1413     /* client can specifiy a background color for the target window */
1414     VADisplayAttribBackgroundColor      = 4,
1415     /*
1416      * this is a gettable only attribute. For some implementations that use the
1417      * hardware overlay, after PutSurface is called, the surface can not be    
1418      * re-used until after the subsequent PutSurface call. If this is the case 
1419      * then the value for this attribute will be set to 1 so that the client   
1420      * will not attempt to re-use the surface right after returning from a call
1421      * to PutSurface.
1422      */
1423     VADisplayAttribDirectSurface       = 5,
1424 } VADisplayAttribType;
1425
1426 /* flags for VADisplayAttribute */
1427 #define VA_DISPLAY_ATTRIB_NOT_SUPPORTED 0x0000
1428 #define VA_DISPLAY_ATTRIB_GETTABLE      0x0001
1429 #define VA_DISPLAY_ATTRIB_SETTABLE      0x0002
1430
1431 typedef struct _VADisplayAttribute
1432 {
1433     VADisplayAttribType type;
1434     int min_value;
1435     int max_value;
1436     int value;  /* used by the set/get attribute functions */
1437 /* flags can be VA_DISPLAY_ATTRIB_GETTABLE or VA_DISPLAY_ATTRIB_SETTABLE or OR'd together */
1438     unsigned int flags;
1439 } VADisplayAttribute;
1440
1441 /* Get maximum number of display attributs supported by the implementation */
1442 int vaMaxNumDisplayAttributes (
1443     VADisplay dpy
1444 );
1445
1446 /* 
1447  * Query display attributes 
1448  * The caller must provide a "attr_list" array that can hold at
1449  * least vaMaxNumDisplayAttributes() entries. The actual number of attributes
1450  * returned in "attr_list" is returned in "num_attributes".
1451  */
1452 VAStatus vaQueryDisplayAttributes (
1453     VADisplay dpy,
1454     VADisplayAttribute *attr_list,      /* out */
1455     int *num_attributes                 /* out */
1456 );
1457
1458 /* 
1459  * Get display attributes 
1460  * This function returns the current attribute values in "attr_list".
1461  * Only attributes returned with VA_DISPLAY_ATTRIB_GETTABLE set in the "flags" field
1462  * from vaQueryDisplayAttributes() can have their values retrieved.  
1463  */
1464 VAStatus vaGetDisplayAttributes (
1465     VADisplay dpy,
1466     VADisplayAttribute *attr_list,      /* in/out */
1467     int num_attributes
1468 );
1469
1470 /* 
1471  * Set display attributes 
1472  * Only attributes returned with VA_DISPLAY_ATTRIB_SETTABLE set in the "flags" field
1473  * from vaQueryDisplayAttributes() can be set.  If the attribute is not settable or 
1474  * the value is out of range, the function returns VA_STATUS_ERROR_ATTR_NOT_SUPPORTED
1475  */
1476 VAStatus vaSetDisplayAttributes (
1477     VADisplay dpy,
1478     VADisplayAttribute *attr_list,
1479     int num_attributes
1480 );
1481
1482 #ifdef __cplusplus
1483 }
1484 #endif
1485
1486 #endif /* _VA_H_ */
1487
1488 #if 0
1489 /*****************************************************************************/ 
1490
1491 Sample Program (w/ pseudo code)
1492
1493 Mostly to demonstrate program flow with no error handling ...
1494
1495 /*****************************************************************************/
1496
1497         /* MPEG-2 VLD decode for a 720x480 frame */
1498
1499         int major_ver, minor_ver;
1500         vaInitialize(dpy, &major_ver, &minor_ver);
1501
1502         int max_num_profiles, max_num_entrypoints, max_num_attribs;
1503         max_num_profiles = vaMaxNumProfiles(dpy);
1504         max_num_entrypoints = vaMaxNumProfiles(dpy);
1505         max_num_attribs = vaMaxNumProfiles(dpy);
1506
1507         /* find out whether MPEG2 MP is supported */
1508         VAProfile *profiles = malloc(sizeof(VAProfile)*max_num_profiles);
1509         int num_profiles;
1510         vaQueryConfigProfiles(dpy, profiles, &profiles);
1511         /*
1512          * traverse "profiles" to locate the one that matches VAProfileMPEG2Main
1513          */ 
1514
1515         /* now get the available entrypoints for MPEG2 MP */
1516         VAEntrypoint *entrypoints = malloc(sizeof(VAEntrypoint)*max_num_entrypoints);
1517         int num_entrypoints;
1518         vaQueryConfigEntrypoints(dpy, VAProfileMPEG2Main, entrypoints, &num_entrypoints);
1519
1520         /* traverse "entrypoints" to see whether VLD is there */
1521
1522         /* Assuming finding VLD, find out the format for the render target */
1523         VAConfigAttrib attrib;
1524         attrib.type = VAConfigAttribRTFormat;
1525         vaGetConfigAttributes(dpy, VAProfileMPEG2Main, VAEntrypointVLD,
1526                                 &attrib, 1);
1527
1528         if (attrib.value & VA_RT_FORMAT_YUV420)
1529                 /* Found desired RT format, keep going */ 
1530
1531         VAConfigID config_id;
1532         vaCreateConfig(dpy, VAProfileMPEG2Main, VAEntrypointVLD, &attrib, 1,
1533                        &config_id);
1534
1535         /* 
1536          * create surfaces for the current target as well as reference frames
1537          * we can get by with 4 surfaces for MPEG-2
1538          */
1539         VASurfaceID surfaces[4];
1540         vaCreateSurfaces(dpy, 720, 480, VA_RT_FORMAT_YUV420, 4, surfaces);
1541
1542         /* 
1543          * Create a context for this decode pipe
1544          */
1545         VAContextID context;
1546         vaCreateContext(dpy, config_id, 720, 480, VA_PROGRESSIVE, surfaces,
1547                         4, &context);
1548
1549         /* Create a picture parameter buffer for this frame */
1550         VABufferID picture_buf;
1551         VAPictureParameterBufferMPEG2 *picture_param;
1552         vaCreateBuffer(dpy, context, VAPictureParameterBufferType, sizeof(VAPictureParameterBufferMPEG2), 1, NULL, &picture_buf);
1553         vaMapBuffer(dpy, picture_buf, &picture_param);
1554         picture_param->horizontal_size = 720;
1555         picture_param->vertical_size = 480;
1556         picture_param->picture_coding_type = 1; /* I-frame */   
1557         /* fill in picture_coding_extension fields here */
1558         vaUnmapBuffer(dpy, picture_buf);
1559
1560         /* Create an IQ matrix buffer for this frame */
1561         VABufferID iq_buf;
1562         VAIQMatrixBufferMPEG2 *iq_matrix;
1563         vaCreateBuffer(dpy, context, VAIQMatrixBufferType, sizeof(VAIQMatrixBufferMPEG2), 1, NULL, &iq_buf);
1564         vaMapBuffer(dpy, iq_buf, &iq_matrix);
1565         /* fill values for IQ_matrix here */
1566         vaUnmapBuffer(dpy, iq_buf);
1567
1568         /* send the picture and IQ matrix buffers to the server */
1569         vaBeginPicture(dpy, context, surfaces[0]);
1570
1571         vaRenderPicture(dpy, context, &picture_buf, 1);
1572         vaRenderPicture(dpy, context, &iq_buf, 1);
1573
1574         /* 
1575          * Send slices in this frame to the server.
1576          * For MPEG-2, each slice is one row of macroblocks, and
1577          * we have 30 slices for a 720x480 frame 
1578          */
1579         for (int i = 1; i <= 30; i++) {
1580
1581                 /* Create a slice parameter buffer */
1582                 VABufferID slice_param_buf;
1583                 VASliceParameterBufferMPEG2 *slice_param;
1584                 vaCreateBuffer(dpy, context, VASliceParameterBufferType, sizeof(VASliceParameterBufferMPEG2), 1, NULL, &slice_param_buf);
1585                 vaMapBuffer(dpy, slice_param_buf, &slice_param);
1586                 slice_param->slice_data_offset = 0;
1587                 /* Let's say all slices in this bit-stream has 64-bit header */
1588                 slice_param->macroblock_offset = 64; 
1589                 slice_param->vertical_position = i;
1590                 /* set up the rest based on what is in the slice header ... */
1591                 vaUnmapBuffer(dpy, slice_param_buf);
1592
1593                 /* send the slice parameter buffer */
1594                 vaRenderPicture(dpy, context, &slice_param_buf, 1);
1595
1596                 /* Create a slice data buffer */
1597                 unsigned char *slice_data;
1598                 VABufferID slice_data_buf;
1599                 vaCreateBuffer(dpy, context, VASliceDataBufferType, x /* decoder figure out how big */, 1, NULL, &slice_data_buf);
1600                 vaMapBuffer(dpy, slice_data_buf, &slice_data);
1601                 /* decoder fill in slice_data */
1602                 vaUnmapBuffer(dpy, slice_data_buf);
1603
1604                 /* send the slice data buffer */
1605                 vaRenderPicture(dpy, context, &slice_data_buf, 1);
1606         }
1607
1608         /* all slices have been sent, mark the end for this frame */
1609         vaEndPicture(dpy, context);
1610
1611         /* The following code demonstrates rendering a sub-title with the target surface */
1612         /* Find out supported Subpicture formats */
1613         VAImageFormat sub_formats[4];
1614         int num_formats;
1615         vaQuerySubpictureFormats(dpy, sub_formats, &num_formats);
1616         /* Assume that we find AI44 as a subpicture format in sub_formats[0] */
1617         VAImage sub_image;
1618         VASubpictureID subpicture;
1619         unsigned char *sub_data;
1620         /* create an image for the subtitle */
1621         vaCreateImage(dpy, sub_formats, 128, 16, &sub_image);
1622         vaMapBuffer(dpy, sub_image->buf, &sub_data);
1623         /* fill the image data */
1624         vaUnmapBuffer(dpy, sub_image->buf);
1625         vaCreateSubpicture(dpy, sub_image, &subpicture);
1626         unsigned char palette[3][16];
1627         /* fill the palette data */
1628         vaSetSubpicturePalette(dpy, subpicture, palette);
1629         vaAssociateSubpicture(dpy, subpicture, surfaces, 1, 0, 0, 296, 400, 128, 16);
1630         vaPutSurface(dpy, surfaces, win, 0, 0, 720, 480, 100, 100, 640, 480, NULL, 0, 0);
1631 #endif