1 /*
2  * Copyright (C) 2014 Intel Corporation. All rights reserved.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 
18 #ifndef VBP_LOADER_H
19 #define VBP_LOADER_H
20 
21 #include <va/va.h>
22 
23 #ifdef USE_HW_VP8
24 #include <va/va_dec_vp8.h>
25 #endif
26 
27 #ifndef TRUE
28 #define TRUE 1
29 #endif
30 
31 #ifndef FALSE
32 #define FALSE 0
33 #endif
34 
35 
36 #ifndef uint8
37 typedef unsigned char uint8;
38 #endif
39 #ifndef uint16
40 typedef unsigned short uint16;
41 #endif
42 #ifndef uint32
43 typedef unsigned int uint32;
44 #endif
45 #ifndef int16
46 typedef short int16;
47 #endif
48 
49 typedef void *Handle;
50 
51 /*
52  * MPEG-4 Part 2 data structure
53  */
54 
55 typedef struct _vbp_codec_data_mp42
56 {
57     uint8  profile_and_level_indication;
58     uint32 video_object_layer_width;
59     uint32 video_object_layer_height;
60 
61     // 0 for unspecified, PAL/NTSC/SECAM
62     uint8  video_format;
63 
64     // 0 short range, 1 full range
65     uint8  video_range;
66 
67     // default 2 (unspecified), 1 for BT709.
68     uint8  matrix_coefficients;
69 
70     uint8  short_video_header;
71 
72     // always exist for mpeg-4,
73     uint8   aspect_ratio_info;
74     uint8   par_width;
75     uint8   par_height;
76 
77     // bit rate
78     int bit_rate;
79 
80     // indicate if vol is received
81     uint8 got_vol;
82     // indicate if vop is received
83     uint8 got_vop;
84 } vbp_codec_data_mp42;
85 
86 typedef struct _vbp_slice_data_mp42
87 {
88     uint8* buffer_addr;
89     uint32 slice_offset;
90     uint32 slice_size;
91     VASliceParameterBufferMPEG4 slice_param;
92     uint8* cur_frame_addr;
93     uint8* forward_ref_addr;
94     uint8* backward_ref_addr;
95     uint32_t pic_stride;
96     uint32_t pic_height;
97 } vbp_slice_data_mp42;
98 
99 typedef struct _vbp_picture_data_mp42 vbp_picture_data_mp42;
100 
101 struct _vbp_picture_data_mp42
102 {
103     uint8 vop_coded;
104     uint16 vop_time_increment;
105     /* indicates if current buffer contains parameter for the first slice of the picture */
106     uint8 new_picture_flag;
107     VAPictureParameterBufferMPEG4 picture_param;
108     vbp_slice_data_mp42 slice_data;
109 
110     vbp_picture_data_mp42* next_picture_data;
111 };
112 
113 typedef struct _vbp_data_mp42
114 {
115     vbp_codec_data_mp42 codec_data;
116     VAIQMatrixBufferMPEG4 iq_matrix_buffer;
117 
118     uint32 number_picture_data;
119     uint32 number_pictures;
120 
121     vbp_picture_data_mp42 *picture_data;
122 
123 } vbp_data_mp42;
124 
125 /*
126  * MPEG2 data structure
127  */
128 
129 typedef struct _vbp_codec_data_mpeg2
130 {
131     uint8 profile_and_level_indication;
132 
133     // picture_coding_type
134     uint8 frame_type;
135 
136     // picture_structure
137     uint8 interlaced;
138 
139     // horizontal_size is a 14-bit unsigned integer
140     uint32 frame_width;
141     // vertical_size is a 14-bit unsigned integer
142     uint32 frame_height;
143 
144     // a 3-bit integer, 0 for unspecified, PAL/NTSC/SECAM
145     uint8 video_format;
146 
147     // 0 short range, 1 full range
148     uint8 video_range;
149 
150     // a 4-bit integer
151     uint8  aspect_ratio;
152     uint32 par_width;
153     uint32 par_height;
154 
155     // a 8-bit integer
156     uint8  matrix_coefficients;
157 
158     uint8  load_intra_quantiser_matrix;
159     uint8  load_non_intra_quantiser_matrix;
160 
161     // a 30-bit integer
162     int bit_rate;
163 
164     // a 4-bit integer
165     int frame_rate;
166 } vbp_codec_data_mpeg2;
167 
168 typedef struct _vbp_slice_data_mpeg2
169 {
170     uint8* buffer_addr;
171     uint32 slice_offset;
172     uint32 slice_size;
173     VASliceParameterBufferMPEG2 slice_param;
174 } vbp_slice_data_mpeg2;
175 
176 typedef struct _vbp_picture_data_mpeg2
177 {
178     VAPictureParameterBufferMPEG2* pic_parms;
179     vbp_slice_data_mpeg2* slice_data;
180 
181     uint32 num_slices;
182 } vbp_picture_data_mpeg2;
183 
184 typedef struct _vbp_data_mpeg2
185 {
186     /* rolling counter of buffers sent by vbp_parse */
187     uint32 buf_number;
188 
189     vbp_codec_data_mpeg2* codec_data;
190     VAIQMatrixBufferMPEG2* iq_matrix_buffer;
191 
192     uint32 num_pictures;
193 
194     vbp_picture_data_mpeg2 *pic_data;
195 } vbp_data_mpeg2;
196 
197 
198 /*
199  * H.264 data structure
200  */
201 
202 typedef struct _vbp_codec_data_h264
203 {
204     uint8 pic_parameter_set_id;
205     uint8 seq_parameter_set_id;
206 
207     uint8 profile_idc;
208     uint8 level_idc;
209     /*constraint flag sets (h.264 Spec v2009)*/
210     uint8 constraint_set0_flag;
211     uint8 constraint_set1_flag;
212     uint8 constraint_set2_flag;
213     uint8 constraint_set3_flag;
214     uint8 constraint_set4_flag;
215     uint8 constraint_set5_flag;
216 
217     uint8 num_ref_frames;
218     uint8 gaps_in_frame_num_value_allowed_flag;
219 
220     uint8 frame_mbs_only_flag;
221     uint8 mb_adaptive_frame_field_flag;
222 
223     int frame_width;
224     int frame_height;
225 
226     uint8 vui_parameters_present_flag;
227 
228     /* aspect ratio */
229     uint8 aspect_ratio_idc;
230     uint16 sar_width;
231     uint16 sar_height;
232 
233     /* cropping information */
234     int crop_top;
235     int crop_bottom;
236     int crop_left;
237     int crop_right;
238 
239     /* video fromat */
240 
241     // default 5 unspecified
242     uint8 video_format;
243     uint8 video_full_range_flag;
244 
245     // default 2 unspecified
246     uint8 matrix_coefficients;
247 
248     uint8 pic_order_cnt_type;
249     int log2_max_pic_order_cnt_lsb_minus4;
250 
251     int bit_rate;
252 
253     int has_slice;
254 } vbp_codec_data_h264;
255 
256 typedef struct _vbp_slice_data_h264
257 {
258     uint8* buffer_addr;
259 
260     uint32 slice_offset; /* slice data offset */
261 
262     uint32 slice_size; /* slice data size */
263 
264     uint8 nal_unit_type;
265 
266     VASliceParameterBufferH264 slc_parms;
267 
268 } vbp_slice_data_h264;
269 
270 
271 typedef struct _vbp_picture_data_h264
272 {
273     VAPictureParameterBufferH264* pic_parms;
274 
275     uint32 num_slices;
276 
277     vbp_slice_data_h264* slc_data;
278 
279 } vbp_picture_data_h264;
280 
281 
282 typedef struct _vbp_data_h264
283 {
284     /* rolling counter of buffers sent by vbp_parse */
285     uint32 buf_number;
286 
287     uint32 num_pictures;
288 
289     /* if SPS has been received */
290     uint8  has_sps;
291 
292     /* if PPS has been received */
293     uint8  has_pps;
294 
295     uint8  new_sps;
296 
297     uint8  new_pps;
298 
299     vbp_picture_data_h264* pic_data;
300 
301     /**
302         * do we need to send matrix to VA for each picture? If not, we need
303         * a flag indicating whether it is updated.
304         */
305     VAIQMatrixBufferH264* IQ_matrix_buf;
306 
307     vbp_codec_data_h264* codec_data;
308 
309 #ifdef USE_SLICE_HEADER_PARSING
310     VAParsePictureParameterBuffer* pic_parse_buffer;
311 #endif
312 
313 } vbp_data_h264;
314 
315 /*
316  * vc1 data structure
317  */
318 typedef struct _vbp_codec_data_vc1
319 {
320     /* Sequence layer. */
321     uint8  PROFILE;
322     uint8  LEVEL;
323     uint8  POSTPROCFLAG;
324     uint8  PULLDOWN;
325     uint8  INTERLACE;
326     uint8  TFCNTRFLAG;
327     uint8  FINTERPFLAG;
328     uint8  PSF;
329 
330     // default 2: unspecified
331     uint8  MATRIX_COEF;
332 
333     /* Entry point layer. */
334     uint8  BROKEN_LINK;
335     uint8  CLOSED_ENTRY;
336     uint8  PANSCAN_FLAG;
337     uint8  REFDIST_FLAG;
338     uint8  LOOPFILTER;
339     uint8  FASTUVMC;
340     uint8  EXTENDED_MV;
341     uint8  DQUANT;
342     uint8  VSTRANSFORM;
343     uint8  OVERLAP;
344     uint8  QUANTIZER;
345     uint16 CODED_WIDTH;
346     uint16 CODED_HEIGHT;
347     uint8  EXTENDED_DMV;
348     uint8  RANGE_MAPY_FLAG;
349     uint8  RANGE_MAPY;
350     uint8  RANGE_MAPUV_FLAG;
351     uint8  RANGE_MAPUV;
352 
353     /* Others. */
354     uint8  RANGERED;
355     uint8  MAXBFRAMES;
356     uint8  MULTIRES;
357     uint8  SYNCMARKER;
358     uint8  RNDCTRL;
359     uint8  REFDIST;
360     uint16 widthMB;
361     uint16 heightMB;
362 
363     uint8  INTCOMPFIELD;
364     uint8  LUMSCALE2;
365     uint8  LUMSHIFT2;
366 
367     // aspect ratio
368 
369     // default unspecified
370     uint8 ASPECT_RATIO;
371 
372     uint8 ASPECT_HORIZ_SIZE;
373     uint8 ASPECT_VERT_SIZE;
374     // bit rate
375     int bit_rate;
376 } vbp_codec_data_vc1;
377 
378 typedef struct _vbp_slice_data_vc1
379 {
380     uint8 *buffer_addr;
381     uint32 slice_offset;
382     uint32 slice_size;
383     VASliceParameterBufferVC1 slc_parms;     /* pointer to slice parms */
384 } vbp_slice_data_vc1;
385 
386 
387 typedef struct _vbp_picture_data_vc1
388 {
389     uint32 picture_is_skipped;                /* VC1_PTYPE_SKIPPED is PTYPE is skipped. */
390     VAPictureParameterBufferVC1 *pic_parms;   /* current parsed picture header */
391     uint32 size_bitplanes;                    /* based on number of MBs */
392     uint8 *packed_bitplanes;                  /* contains up to three bitplanes packed for libVA */
393     uint32 num_slices;                        /* number of slices.  always at least one */
394     vbp_slice_data_vc1 *slc_data;             /* pointer to array of slice data */
395 } vbp_picture_data_vc1;
396 
397 typedef struct _vbp_data_vc1
398 {
399     uint32 buf_number;                        /* rolling counter of buffers sent by vbp_parse */
400     vbp_codec_data_vc1 *se_data;              /* parsed SH/EPs */
401 
402     uint32 num_pictures;
403 
404     vbp_picture_data_vc1* pic_data;
405 } vbp_data_vc1;
406 
407 #ifdef USE_HW_VP8
408 typedef struct _vbp_codec_data_vp8
409 {
410     uint8 frame_type;
411     uint8 version_num;
412     int show_frame;
413 
414     /* color space type specification */
415     int clr_type;
416 
417     uint32 frame_width;
418     uint32 frame_height;
419 
420     int refresh_alt_frame;
421     int refresh_golden_frame;
422     int refresh_last_frame;
423 
424     /* cropping information */
425     int crop_top;
426     int crop_bottom;
427     int crop_left;
428     int crop_right;
429 
430     int golden_copied;
431     int altref_copied;
432 } vbp_codec_data_vp8;
433 
434 typedef struct _vbp_slice_data_vp8
435 {
436     uint8 *buffer_addr;
437     uint32 slice_offset;
438     uint32 slice_size;
439     VASliceParameterBufferVP8 slc_parms;     /* pointer to slice parms */
440 } vbp_slice_data_vp8;
441 
442 typedef struct _vbp_picture_data_vp8
443 {
444     VAPictureParameterBufferVP8* pic_parms;   /* current parsed picture header */
445 
446     uint32 num_slices;                        /* number of slices.  always one for VP8 */
447     vbp_slice_data_vp8 *slc_data;             /* pointer to array of slice data */
448 } vbp_picture_data_vp8;
449 
450 typedef struct _vbp_data_vp8
451 {
452     uint32 buf_number;                        /* rolling counter of buffers sent by vbp_parse */
453     vbp_codec_data_vp8 *codec_data;
454 
455     uint32 num_pictures;
456 
457     vbp_picture_data_vp8* pic_data;
458 
459     VAProbabilityDataBufferVP8* prob_data;
460     VAIQMatrixBufferVP8* IQ_matrix_buf;
461 } vbp_data_vp8;
462 #endif
463 
464 enum _picture_type
465 {
466     VC1_PTYPE_I,
467     VC1_PTYPE_P,
468     VC1_PTYPE_B,
469     VC1_PTYPE_BI,
470     VC1_PTYPE_SKIPPED
471 };
472 
473 enum _vbp_parser_error
474 {
475     VBP_OK,
476     VBP_TYPE,
477     VBP_LOAD,
478     VBP_INIT,
479     VBP_DATA,
480     VBP_DONE,
481     VBP_MEM,
482     VBP_PARM,
483     VBP_PARTIAL,
484     VBP_MULTI,
485     VBP_ERROR
486 };
487 
488 enum _vbp_parser_type
489 {
490     VBP_VC1,
491     VBP_MPEG2,
492     VBP_MPEG4,
493     VBP_H264,
494 #ifdef USE_HW_VP8
495     VBP_VP8,
496 #endif
497 #if (defined USE_AVC_SHORT_FORMAT || defined USE_SLICE_HEADER_PARSING)
498     VBP_H264SECURE,
499 #endif
500 };
501 
502 
503 /*
504  * open video bitstream parser to parse a specific media type.
505  * @param  parser_type: one of the types defined in #vbp_parser_type
506  * @param  hcontext: pointer to hold returned VBP context handle.
507  * @return VBP_OK on success, anything else on failure.
508  *
509  */
510 uint32 vbp_open(uint32 parser_type, Handle *hcontext);
511 
512 /*
513  * close video bitstream parser.
514  * @param hcontext: VBP context handle.
515  * @returns VBP_OK on success, anything else on failure.
516  *
517  */
518 uint32 vbp_close(Handle hcontext);
519 
520 /*
521  * parse bitstream.
522  * @param hcontext: handle to VBP context.
523  * @param data: pointer to bitstream buffer.
524  * @param size: size of bitstream buffer.
525  * @param init_flag: 1 if buffer contains bitstream configuration data, 0 otherwise.
526  * @return VBP_OK on success, anything else on failure.
527  *
528  */
529 uint32 vbp_parse(Handle hcontext, uint8 *data, uint32 size, uint8 init_data_flag);
530 
531 /*
532  * query parsing result.
533  * @param hcontext: handle to VBP context.
534  * @param data: pointer to hold a data blob that contains parsing result.
535  * Structure of data blob is determined by the media type.
536  * @return VBP_OK on success, anything else on failure.
537  *
538  */
539 uint32 vbp_query(Handle hcontext, void **data);
540 
541 
542 /*
543  * flush any un-parsed bitstream.
544  * @param hcontext: handle to VBP context.
545  * @returns VBP_OK on success, anything else on failure.
546  *
547  */
548 uint32 vbp_flush(Handle hcontent);
549 
550 #if (defined USE_AVC_SHORT_FORMAT || defined USE_SLICE_HEADER_PARSING)
551 /*
552  * update the the vbp context using the new data
553  * @param hcontext: handle to VBP context.
554  * @param data: pointer to the new data buffer.
555  * @param size: size of new data buffer.
556  * @param data: pointer to hold a data blob that contains parsing result.
557  * @returns VBP_OK on success, anything else on failure.
558  *
559 */
560 uint32 vbp_update(Handle hcontext, void *newdata, uint32 size, void **data);
561 #endif
562 
563 uint32 vbp_decode(Handle hcontext, void *picdata);
564 
565 #endif /* VBP_LOADER_H */
566