2 * Copyright (c) 2007-2011 Intel Corporation. All Rights Reserved.
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:
12 * The above copyright notice and this permission notice (including the
13 * next paragraph) shall be included in all copies or substantial portions
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 INTEL 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.
27 * \brief The H.264 encoding API
29 * This file contains the \ref api_enc_h264 "H.264 encoding API".
40 * \defgroup api_enc_h264 H.264 encoding API
48 * Those flags flags are meant to signal when a picture marks the end
49 * of a sequence, a stream, or even both at once.
54 * \brief Marks the last picture in the sequence.
56 * i.e. the driver appends \c end_of_seq() NAL unit to the encoded frame.
58 #define H264_LAST_PICTURE_EOSEQ 0x01
60 * \brief Marks the last picture in the stream.
62 * i.e. the driver appends \c end_of_stream() NAL unit to the encoded frame.
64 #define H264_LAST_PICTURE_EOSTREAM 0x02
68 * \brief Packed header types specific to H.264 encoding.
70 * Types of packed headers generally used for H.264 encoding. Each
71 * associated packed header data buffer shall contain the start code
72 * prefix 0x000001 followed by the complete NAL unit, thus also
73 * including the \c nal_unit_type.
75 * Note: the start code prefix can contain an arbitrary number of leading
76 * zeros. The driver will skip them for emulation prevention bytes insertion,
81 * \brief Packed Sequence Parameter Set (SPS).
83 * The corresponding packed header data buffer shall contain the
84 * complete seq_parameter_set_rbsp() syntax element.
86 * Note: packed \c nal_unit_type shall be equal to 7.
88 VAEncPackedHeaderH264_SPS = VAEncPackedHeaderSequence,
90 * \brief Packed Picture Parameter Set (PPS).
92 * The corresponding packed header data buffer shall contain the
93 * complete pic_parameter_set_rbsp() syntax element.
95 * Note: packed \c nal_unit_type shall be equal to 8.
97 VAEncPackedHeaderH264_PPS = VAEncPackedHeaderPicture,
99 * \brief Packed slice header.
101 * The corresponding packed header data buffer shall contain the
102 * \c slice_header() syntax element only, along with any start
103 * code prefix and NAL unit type preceeding it. i.e. this means
104 * that the buffer does not contain any of the \c slice_data() or
105 * the \c rbsp_slice_trailing_bits().
107 * Note: packed \c nal_unit_type shall be equal to 1 (non-IDR
108 * picture), or 5 (IDR picture).
110 VAEncPackedHeaderH264_Slice = VAEncPackedHeaderSlice,
112 * \brief Packed Supplemental Enhancement Information (SEI).
114 * The corresponding packed header data buffer shall contain the
115 * complete sei_rbsp() syntax element, thus including several
116 * sei_message() elements if necessary.
118 * Note: packed \c nal_unit_type shall be equal to 6.
121 * This is a deprecated packed header flag, All applications can use
122 * \c VA_ENC_PACKED_HEADER_RAW_DATA to pass the corresponding packed
123 * SEI header data buffer to the driver
125 VAEncPackedHeaderH264_SEI va_deprecated_enum = (0x80000000 | 1),
126 } VAEncPackedHeaderTypeH264;
129 * \brief Sequence parameter for H.264 encoding in baseline, main & high
132 * This structure holds information for \c seq_parameter_set_data() as
133 * defined by the H.264 specification.
135 * If packed sequence headers mode is used, i.e. if the encoding
136 * pipeline was configured with the #VA_ENC_PACKED_HEADER_SEQUENCE
137 * flag, then the driver expects two more buffers to be provided to
138 * the same \c vaRenderPicture() as this buffer:
139 * - a #VAEncPackedHeaderParameterBuffer with type set to
140 * VAEncPackedHeaderType::VAEncPackedHeaderSequence ;
141 * - a #VAEncPackedHeaderDataBuffer which holds the actual packed
144 * If \c seq_scaling_matrix_present_flag is set to \c 1, then a
145 * #VAIQMatrixBufferH264 buffer shall also be provided within the same
146 * \c vaRenderPicture() call as this sequence parameter buffer.
148 typedef struct _VAEncSequenceParameterBufferH264 {
149 /** \brief Same as the H.264 bitstream syntax element. */
150 uint8_t seq_parameter_set_id;
151 /** \brief Same as the H.264 bitstream syntax element. */
153 /** \brief Period between I frames. */
154 uint32_t intra_period;
155 /** \brief Period between IDR frames. */
156 uint32_t intra_idr_period;
157 /** \brief Period between I/P frames. */
160 * \brief Initial bitrate set for this sequence in CBR or VBR modes.
162 * This field represents the initial bitrate value for this
163 * sequence if CBR or VBR mode is used, i.e. if the encoder
164 * pipeline was created with a #VAConfigAttribRateControl
165 * attribute set to either \ref VA_RC_CBR or \ref VA_RC_VBR.
167 * The bitrate can be modified later on through
168 * #VAEncMiscParameterRateControl buffers.
170 uint32_t bits_per_second;
171 /** \brief Same as the H.264 bitstream syntax element. */
172 uint32_t max_num_ref_frames;
173 /** \brief Picture width in macroblocks. */
174 uint16_t picture_width_in_mbs;
175 /** \brief Picture height in macroblocks. */
176 uint16_t picture_height_in_mbs;
180 /** \brief Same as the H.264 bitstream syntax element. */
181 uint32_t chroma_format_idc : 2;
182 /** \brief Same as the H.264 bitstream syntax element. */
183 uint32_t frame_mbs_only_flag : 1;
184 /** \brief Same as the H.264 bitstream syntax element. */
185 uint32_t mb_adaptive_frame_field_flag : 1;
186 /** \brief Same as the H.264 bitstream syntax element. */
187 uint32_t seq_scaling_matrix_present_flag : 1;
188 /** \brief Same as the H.264 bitstream syntax element. */
189 uint32_t direct_8x8_inference_flag : 1;
190 /** \brief Same as the H.264 bitstream syntax element. */
191 uint32_t log2_max_frame_num_minus4 : 4;
192 /** \brief Same as the H.264 bitstream syntax element. */
193 uint32_t pic_order_cnt_type : 2;
194 /** \brief Same as the H.264 bitstream syntax element. */
195 uint32_t log2_max_pic_order_cnt_lsb_minus4 : 4;
196 /** \brief Same as the H.264 bitstream syntax element. */
197 uint32_t delta_pic_order_always_zero_flag : 1;
202 /** \brief Same as the H.264 bitstream syntax element. */
203 uint8_t bit_depth_luma_minus8;
204 /** \brief Same as the H.264 bitstream syntax element. */
205 uint8_t bit_depth_chroma_minus8;
207 /** if pic_order_cnt_type == 1 */
209 /** \brief Same as the H.264 bitstream syntax element. */
210 uint8_t num_ref_frames_in_pic_order_cnt_cycle;
211 /** \brief Same as the H.264 bitstream syntax element. */
212 int32_t offset_for_non_ref_pic;
213 /** \brief Same as the H.264 bitstream syntax element. */
214 int32_t offset_for_top_to_bottom_field;
215 /** \brief Same as the H.264 bitstream syntax element. */
216 int32_t offset_for_ref_frame[256];
219 /** @name Cropping (optional) */
221 /** \brief Same as the H.264 bitstream syntax element. */
222 uint8_t frame_cropping_flag;
223 /** \brief Same as the H.264 bitstream syntax element. */
224 uint32_t frame_crop_left_offset;
225 /** \brief Same as the H.264 bitstream syntax element. */
226 uint32_t frame_crop_right_offset;
227 /** \brief Same as the H.264 bitstream syntax element. */
228 uint32_t frame_crop_top_offset;
229 /** \brief Same as the H.264 bitstream syntax element. */
230 uint32_t frame_crop_bottom_offset;
233 /** @name VUI parameters (optional) */
235 /** \brief Same as the H.264 bitstream syntax element. */
236 uint8_t vui_parameters_present_flag;
239 /** \brief Same as the H.264 bitstream syntax element. */
240 uint32_t aspect_ratio_info_present_flag : 1;
241 /** \brief Same as the H.264 bitstream syntax element. */
242 uint32_t timing_info_present_flag : 1;
243 /** \brief Same as the H.264 bitstream syntax element. */
244 uint32_t bitstream_restriction_flag : 1;
245 /** \brief Range: 0 to 16, inclusive. */
246 uint32_t log2_max_mv_length_horizontal : 5;
247 /** \brief Range: 0 to 16, inclusive. */
248 uint32_t log2_max_mv_length_vertical : 5;
249 /** \brief Same as the H.264 bitstream syntax element. */
250 uint32_t fixed_frame_rate_flag : 1;
251 /** \brief Same as the H.264 bitstream syntax element. */
252 uint32_t low_delay_hrd_flag : 1;
253 /** \brief Same as the H.264 bitstream syntax element. */
254 uint32_t motion_vectors_over_pic_boundaries_flag: 1;
255 /** \brief Reserved for future use, must be zero */
256 uint32_t reserved : 16;
260 /** \brief Same as the H.264 bitstream syntax element. */
261 uint8_t aspect_ratio_idc;
262 /** \brief Same as the H.264 bitstream syntax element. */
264 /** \brief Same as the H.264 bitstream syntax element. */
266 /** \brief Same as the H.264 bitstream syntax element. */
267 uint32_t num_units_in_tick;
268 /** \brief Same as the H.264 bitstream syntax element. */
271 /** \brief Reserved bytes for future use, must be zero */
272 uint32_t va_reserved[VA_PADDING_LOW];
274 } VAEncSequenceParameterBufferH264;
277 * \brief Picture parameter for H.264 encoding in baseline, main & high
280 * This structure holds information for \c pic_parameter_set_rbsp() as
281 * defined by the H.264 specification.
283 * If packed picture headers mode is used, i.e. if the encoding
284 * pipeline was configured with the #VA_ENC_PACKED_HEADER_PICTURE
285 * flag, then the driver expects two more buffers to be provided to
286 * the same \c vaRenderPicture() as this buffer:
287 * - a #VAEncPackedHeaderParameterBuffer with type set to
288 * VAEncPackedHeaderType::VAEncPackedHeaderPicture ;
289 * - a #VAEncPackedHeaderDataBuffer which holds the actual packed
292 * If \c pic_scaling_matrix_present_flag is set to \c 1, then a
293 * #VAIQMatrixBufferH264 buffer shall also be provided within the same
294 * \c vaRenderPicture() call as this picture parameter buffer.
296 typedef struct _VAEncPictureParameterBufferH264 {
298 * \brief Information about the picture to be encoded.
300 * See #VAPictureH264 for further description of each field.
301 * Note that CurrPic.picture_id represents the reconstructed
302 * (decoded) picture. User provides a scratch VA surface ID here.
304 VAPictureH264 CurrPic;
306 * \brief Decoded Picture Buffer (DPB).
308 * This array represents the list of reconstructed (decoded)
309 * frames used as reference. It is important to keep track of
310 * reconstructed frames so that they can be used later on as
311 * reference for P or B-frames encoding.
313 VAPictureH264 ReferenceFrames[16];
315 * \brief Output encoded bitstream.
317 * \ref coded_buf has type #VAEncCodedBufferType. It should be
318 * large enough to hold the compressed NAL slice and possibly SPS
321 VABufferID coded_buf;
323 /** \brief The picture parameter set referred to in the slice header. */
324 uint8_t pic_parameter_set_id;
325 /** \brief The active sequence parameter set. Range: 0 to 31, inclusive. */
326 uint8_t seq_parameter_set_id;
329 * \brief OR'd flags describing whether the picture is the last one or not.
331 * This fields holds 0 if the picture to be encoded is not the last
332 * one in the stream or sequence. Otherwise, it is a combination of
333 * \ref H264_LAST_PICTURE_EOSEQ or \ref H264_LAST_PICTURE_EOSTREAM.
335 uint8_t last_picture;
337 /** \brief The picture identifier.
338 * Range: 0 to \f$2^{log2\_max\_frame\_num\_minus4 + 4} - 1\f$, inclusive.
342 /** \brief \c pic_init_qp_minus26 + 26. */
344 /** \brief Maximum reference index for reference picture list 0.
345 * Range: 0 to 31, inclusive.
347 uint8_t num_ref_idx_l0_active_minus1;
348 /** \brief Maximum reference index for reference picture list 1.
349 * Range: 0 to 31, inclusive.
351 uint8_t num_ref_idx_l1_active_minus1;
353 /** \brief Range: -12 to 12, inclusive. */
354 int8_t chroma_qp_index_offset;
355 /** \brief Range: -12 to 12, inclusive. */
356 int8_t second_chroma_qp_index_offset;
360 /** \brief Is picture an IDR picture? */
361 uint32_t idr_pic_flag : 1;
362 /** \brief Is picture a reference picture? */
363 uint32_t reference_pic_flag : 2;
364 /** \brief Selects CAVLC (0) or CABAC (1) entropy coding mode. */
365 uint32_t entropy_coding_mode_flag : 1;
366 /** \brief Is weighted prediction applied to P slices? */
367 uint32_t weighted_pred_flag : 1;
368 /** \brief Range: 0 to 2, inclusive. */
369 uint32_t weighted_bipred_idc : 2;
370 /** \brief Same as the H.264 bitstream syntax element. */
371 uint32_t constrained_intra_pred_flag : 1;
372 /** \brief Same as the H.264 bitstream syntax element. */
373 uint32_t transform_8x8_mode_flag : 1;
374 /** \brief Same as the H.264 bitstream syntax element. */
375 uint32_t deblocking_filter_control_present_flag : 1;
376 /** \brief Same as the H.264 bitstream syntax element. */
377 uint32_t redundant_pic_cnt_present_flag : 1;
378 /** \brief Same as the H.264 bitstream syntax element. */
379 uint32_t pic_order_present_flag : 1;
380 /** \brief Same as the H.264 bitstream syntax element. */
381 uint32_t pic_scaling_matrix_present_flag : 1;
386 /** \brief Reserved bytes for future use, must be zero */
387 uint32_t va_reserved[VA_PADDING_LOW];
388 } VAEncPictureParameterBufferH264;
390 typedef struct _VAEncQPBufferH264 {
392 * \brief This structure holds QP per 16x16 macroblock. Buffer size shall be
393 * sufficient to fit the slice or frame to be encoded depending on if it is a
394 * slice level or frame level encoding.
400 * \brief Slice parameter for H.264 encoding in baseline, main & high profiles.
402 * This structure holds information for \c
403 * slice_layer_without_partitioning_rbsp() as defined by the H.264
406 * If packed slice headers mode is used, i.e. if the encoding
407 * pipeline was configured with the #VA_ENC_PACKED_HEADER_SLICE
408 * flag, then the driver expects two more buffers to be provided to
409 * the same \c vaRenderPicture() as this buffer:
410 * - a #VAEncPackedHeaderParameterBuffer with type set to
411 * VAEncPackedHeaderType::VAEncPackedHeaderSlice ;
412 * - a #VAEncPackedHeaderDataBuffer which holds the actual packed
415 * If per-macroblock encoder configuration is needed, \c macroblock_info
416 * references a buffer of type #VAEncMacroblockParameterBufferH264. This
417 * buffer is not passed to vaRenderPicture() and it can be re-used
418 * without re-allocating the whole buffer.
420 typedef struct _VAEncSliceParameterBufferH264 {
421 /** \brief Starting MB address for this slice. */
422 uint32_t macroblock_address;
423 /** \brief Number of macroblocks in this slice. */
424 uint32_t num_macroblocks;
426 * \brief Per-MB encoder configuration buffer, or \c VA_INVALID_ID.
428 * If per-MB encoder configuration is needed, then \ref macroblock_info
429 * references a buffer of type #VAEncMacroblockParameterBufferH264
430 * (\c VAEncMacroblockParameterBufferType). Otherwise, buffer id
431 * is set to \c VA_INVALID_ID and per-MB configuration is derived
432 * from this slice parameter.
434 * The \c macroblock_info buffer must hold \ref num_macroblocks
437 VABufferID macroblock_info;
438 /** \brief Slice type.
439 * Range: 0..2, 5..7, i.e. no switching slices.
442 /** \brief Same as the H.264 bitstream syntax element. */
443 uint8_t pic_parameter_set_id;
444 /** \brief Same as the H.264 bitstream syntax element. */
447 /** @name If pic_order_cnt_type == 0 */
449 /** \brief The picture order count modulo MaxPicOrderCntLsb. */
450 uint16_t pic_order_cnt_lsb;
451 /** \brief Valid if \c pic_order_present_flag and this is a bottom field. */
452 int32_t delta_pic_order_cnt_bottom;
454 /** @name If pic_order_cnt_type == 1 && !delta_pic_order_always_zero_flag */
456 /** \brief [0]: top, [1]: bottom. */
457 int32_t delta_pic_order_cnt[2];
460 /** @name If slice_type == B */
462 uint8_t direct_spatial_mv_pred_flag;
465 /** @name If slice_type == P */
467 /** \brief Specifies if
468 * \ref _VAEncPictureParameterBufferH264::num_ref_idx_l0_active_minus1 or
469 * \ref _VAEncPictureParameterBufferH264::num_ref_idx_l1_active_minus1 are
470 * overriden by the values for this slice.
472 uint8_t num_ref_idx_active_override_flag;
473 /** \brief Maximum reference index for reference picture list 0.
474 * Range: 0 to 31, inclusive.
476 uint8_t num_ref_idx_l0_active_minus1;
477 /** \brief Maximum reference index for reference picture list 1.
478 * Range: 0 to 31, inclusive.
480 uint8_t num_ref_idx_l1_active_minus1;
481 /** \brief Reference picture list 0 (for P slices). */
482 VAPictureH264 RefPicList0[32];
483 /** \brief Reference picture list 1 (for B slices). */
484 VAPictureH264 RefPicList1[32];
487 /** @name pred_weight_table() */
489 /** \brief Same as the H.264 bitstream syntax element. */
490 uint8_t luma_log2_weight_denom;
491 /** \brief Same as the H.264 bitstream syntax element. */
492 uint8_t chroma_log2_weight_denom;
493 /** \brief Same as the H.264 bitstream syntax element. */
494 uint8_t luma_weight_l0_flag;
495 /** \brief Same as the H.264 bitstream syntax element. */
496 signed short luma_weight_l0[32];
497 /** \brief Same as the H.264 bitstream syntax element. */
498 signed short luma_offset_l0[32];
499 /** \brief Same as the H.264 bitstream syntax element. */
500 uint8_t chroma_weight_l0_flag;
501 /** \brief Same as the H.264 bitstream syntax element. */
502 signed short chroma_weight_l0[32][2];
503 /** \brief Same as the H.264 bitstream syntax element. */
504 signed short chroma_offset_l0[32][2];
505 /** \brief Same as the H.264 bitstream syntax element. */
506 uint8_t luma_weight_l1_flag;
507 /** \brief Same as the H.264 bitstream syntax element. */
508 signed short luma_weight_l1[32];
509 /** \brief Same as the H.264 bitstream syntax element. */
510 signed short luma_offset_l1[32];
511 /** \brief Same as the H.264 bitstream syntax element. */
512 uint8_t chroma_weight_l1_flag;
513 /** \brief Same as the H.264 bitstream syntax element. */
514 signed short chroma_weight_l1[32][2];
515 /** \brief Same as the H.264 bitstream syntax element. */
516 signed short chroma_offset_l1[32][2];
519 /** \brief Range: 0 to 2, inclusive. */
520 uint8_t cabac_init_idc;
521 /** \brief Same as the H.264 bitstream syntax element. */
522 int8_t slice_qp_delta;
523 /** @name If deblocking_filter_control_present_flag */
525 /** \brief Range: 0 to 2, inclusive. */
526 uint8_t disable_deblocking_filter_idc;
527 /** \brief Same as the H.264 bitstream syntax element. */
528 int8_t slice_alpha_c0_offset_div2;
529 /** \brief Same as the H.264 bitstream syntax element. */
530 int8_t slice_beta_offset_div2;
532 /** \brief Reserved bytes for future use, must be zero */
533 uint32_t va_reserved[VA_PADDING_LOW];
535 } VAEncSliceParameterBufferH264;
538 * @name Macroblock neighbour availability bits
540 * \anchor api_enc_h264_mb_pred_avail_bits
541 * Definitions for macroblock neighbour availability bits used in
542 * intra prediction mode (non MBAFF only).
546 /** \brief References macroblock in the top-left corner. */
547 #define VA_MB_PRED_AVAIL_TOP_LEFT (1 << 2)
548 /** \brief References macroblock above the current macroblock. */
549 #define VA_MB_PRED_AVAIL_TOP (1 << 4)
550 /** \brief References macroblock in the top-right corner. */
551 #define VA_MB_PRED_AVAIL_TOP_RIGHT (1 << 3)
552 /** \brief References macroblock on the left of the current macroblock. */
553 #define VA_MB_PRED_AVAIL_LEFT (1 << 6)
557 * \brief Macroblock parameter for H.264 encoding in baseline, main & high
560 * This structure holds per-macroblock information. The buffer must be
561 * allocated with as many elements (macroblocks) as necessary to fit
562 * the slice to be encoded. Besides, the per-macroblock records must
563 * be written in a strict raster order and with no gap. i.e. every
564 * macroblock, regardless of its type, shall have an entry.
566 typedef struct _VAEncMacroblockParameterBufferH264 {
568 * \brief Quantization parameter.
570 * Requested quantization parameter. Range: 0 to 51, inclusive.
571 * If \ref qp is set to 0xff, then the actual value is derived
572 * from the slice-level value: \c pic_init_qp + \c slice_qp_delta.
577 /** @name Data for intra macroblock */
582 * \brief Flag specified to override MB neighbour
583 * availability bits from VME stage.
585 * This flag specifies that macroblock neighbour
586 * availability bits from the VME stage are overriden
587 * by the \ref pred_avail_flags hereunder.
589 uint32_t pred_avail_override_flag : 1;
591 * \brief Bitwise representation of which macroblocks
592 * are available for intra prediction.
594 * If the slice is intra-coded, this field represents
595 * the macroblocks available for intra prediction.
596 * See \ref api_enc_h264_mb_pred_avail_bits
597 * "macroblock neighbour availability" bit definitions.
599 uint32_t pred_avail_flags : 8;
605 /** @name Data for inter macroblock */
616 /** \brief Reserved bytes for future use, must be zero */
617 uint32_t va_reserved[VA_PADDING_LOW];
618 } VAEncMacroblockParameterBufferH264;
626 #endif /* VA_ENC_H264_H */