OSDN Git Service

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