Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | RSS feed

  1. /*
  2.  * Copyright © 2011 Intel Corporation
  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.  
  26. #include <stdlib.h>
  27. #include <assert.h>
  28.  
  29. #include <va/va.h>
  30. #include <va/va_enc_h264.h>
  31. #include <va/va_enc_mpeg2.h>
  32. #include <va/va_enc_vp8.h>
  33. #include <va/va_enc_hevc.h>
  34. #include <math.h>
  35. #include "gen6_mfc.h"
  36. #include "i965_encoder_utils.h"
  37.  
  38. #define BITSTREAM_ALLOCATE_STEPPING     4096
  39.  
  40. #define NAL_REF_IDC_NONE        0
  41. #define NAL_REF_IDC_LOW         1
  42. #define NAL_REF_IDC_MEDIUM      2
  43. #define NAL_REF_IDC_HIGH        3
  44.  
  45. #define NAL_NON_IDR             1
  46. #define NAL_IDR                 5
  47. #define NAL_SPS                 7
  48. #define NAL_PPS                 8
  49. #define NAL_SEI                 6
  50.  
  51. #define SLICE_TYPE_P            0
  52. #define SLICE_TYPE_B            1
  53. #define SLICE_TYPE_I            2
  54.  
  55. #define IS_I_SLICE(type) (SLICE_TYPE_I == (type) || SLICE_TYPE_I == (type - 5))
  56. #define IS_P_SLICE(type) (SLICE_TYPE_P == (type) || SLICE_TYPE_P == (type - 5))
  57. #define IS_B_SLICE(type) (SLICE_TYPE_B == (type) || SLICE_TYPE_B == (type - 5))
  58.  
  59. #define ENTROPY_MODE_CAVLC      0
  60. #define ENTROPY_MODE_CABAC      1
  61.  
  62. #define PROFILE_IDC_BASELINE    66
  63. #define PROFILE_IDC_MAIN        77
  64. #define PROFILE_IDC_HIGH        100
  65.  
  66. /*HEVC*/
  67. #define VPS_NUT         32
  68. #define SPS_NUT         33
  69. #define PPS_NUT         34
  70. #define IDR_WRADL_NUT   19
  71. #define IDR_NLP_NUT     20
  72. #define SLICE_TRAIL_N_NUT       0
  73. #define SLICE_TRAIL_R_NUT       1
  74. #define PREFIX_SEI_NUT  39
  75. #define SUFFIX_SEI_NUT  40
  76.  
  77. struct __avc_bitstream {
  78.     unsigned int *buffer;
  79.     int bit_offset;
  80.     int max_size_in_dword;
  81. };
  82.  
  83. typedef struct __avc_bitstream avc_bitstream;
  84.  
  85. static unsigned int
  86. swap32(unsigned int val)
  87. {
  88.     unsigned char *pval = (unsigned char *)&val;
  89.  
  90.     return ((pval[0] << 24)     |
  91.             (pval[1] << 16)     |
  92.             (pval[2] << 8)      |
  93.             (pval[3] << 0));
  94. }
  95.  
  96. static void
  97. avc_bitstream_start(avc_bitstream *bs)
  98. {
  99.     bs->max_size_in_dword = BITSTREAM_ALLOCATE_STEPPING;
  100.     bs->buffer = calloc(bs->max_size_in_dword * sizeof(int), 1);
  101.     bs->bit_offset = 0;
  102. }
  103.  
  104. static void
  105. avc_bitstream_end(avc_bitstream *bs)
  106. {
  107.     int pos = (bs->bit_offset >> 5);
  108.     int bit_offset = (bs->bit_offset & 0x1f);
  109.     int bit_left = 32 - bit_offset;
  110.  
  111.     if (bit_offset) {
  112.         bs->buffer[pos] = swap32((bs->buffer[pos] << bit_left));
  113.     }
  114.  
  115.     // free(bs->buffer);
  116. }
  117.  
  118. static void
  119. avc_bitstream_put_ui(avc_bitstream *bs, unsigned int val, int size_in_bits)
  120. {
  121.     int pos = (bs->bit_offset >> 5);
  122.     int bit_offset = (bs->bit_offset & 0x1f);
  123.     int bit_left = 32 - bit_offset;
  124.  
  125.     if (!size_in_bits)
  126.         return;
  127.  
  128.     if (size_in_bits < 32)
  129.         val &= (( 1 << size_in_bits) - 1);
  130.  
  131.     bs->bit_offset += size_in_bits;
  132.  
  133.     if (bit_left > size_in_bits) {
  134.         bs->buffer[pos] = (bs->buffer[pos] << size_in_bits | val);
  135.     } else {
  136.         size_in_bits -= bit_left;
  137.         bs->buffer[pos] = (bs->buffer[pos] << bit_left) | (val >> size_in_bits);
  138.         bs->buffer[pos] = swap32(bs->buffer[pos]);
  139.  
  140.         if (pos + 1 == bs->max_size_in_dword) {
  141.             bs->max_size_in_dword += BITSTREAM_ALLOCATE_STEPPING;
  142.             bs->buffer = realloc(bs->buffer, bs->max_size_in_dword * sizeof(unsigned int));
  143.            
  144.             if (!bs->buffer)
  145.                 return;
  146.         }
  147.  
  148.         bs->buffer[pos + 1] = val;
  149.     }
  150. }
  151.  
  152. static void
  153. avc_bitstream_put_ue(avc_bitstream *bs, unsigned int val)
  154. {
  155.     int size_in_bits = 0;
  156.     int tmp_val = ++val;
  157.  
  158.     while (tmp_val) {
  159.         tmp_val >>= 1;
  160.         size_in_bits++;
  161.     }
  162.  
  163.     avc_bitstream_put_ui(bs, 0, size_in_bits - 1); // leading zero
  164.     avc_bitstream_put_ui(bs, val, size_in_bits);
  165. }
  166.  
  167. static void
  168. avc_bitstream_put_se(avc_bitstream *bs, int val)
  169. {
  170.     unsigned int new_val;
  171.  
  172.     if (val <= 0)
  173.         new_val = -2 * val;
  174.     else
  175.         new_val = 2 * val - 1;
  176.  
  177.     avc_bitstream_put_ue(bs, new_val);
  178. }
  179.  
  180. static void
  181. avc_bitstream_byte_aligning(avc_bitstream *bs, int bit)
  182. {
  183.     int bit_offset = (bs->bit_offset & 0x7);
  184.     int bit_left = 8 - bit_offset;
  185.     int new_val;
  186.  
  187.     if (!bit_offset)
  188.         return;
  189.  
  190.     assert(bit == 0 || bit == 1);
  191.  
  192.     if (bit)
  193.         new_val = (1 << bit_left) - 1;
  194.     else
  195.         new_val = 0;
  196.  
  197.     avc_bitstream_put_ui(bs, new_val, bit_left);
  198. }
  199. static void avc_rbsp_trailing_bits(avc_bitstream *bs)
  200. {
  201.     avc_bitstream_put_ui(bs, 1, 1);
  202.     avc_bitstream_byte_aligning(bs, 0);
  203. }
  204. static void nal_start_code_prefix(avc_bitstream *bs)
  205. {
  206.     avc_bitstream_put_ui(bs, 0x00000001, 32);
  207. }
  208.  
  209. static void nal_header(avc_bitstream *bs, int nal_ref_idc, int nal_unit_type)
  210. {
  211.     avc_bitstream_put_ui(bs, 0, 1);                /* forbidden_zero_bit: 0 */
  212.     avc_bitstream_put_ui(bs, nal_ref_idc, 2);
  213.     avc_bitstream_put_ui(bs, nal_unit_type, 5);
  214. }
  215.  
  216. static void
  217. slice_header(avc_bitstream *bs,
  218.              VAEncSequenceParameterBufferH264 *sps_param,
  219.              VAEncPictureParameterBufferH264 *pic_param,
  220.              VAEncSliceParameterBufferH264 *slice_param)
  221. {
  222.     int first_mb_in_slice = slice_param->macroblock_address;
  223.  
  224.     avc_bitstream_put_ue(bs, first_mb_in_slice);        /* first_mb_in_slice: 0 */
  225.     avc_bitstream_put_ue(bs, slice_param->slice_type);  /* slice_type */
  226.     avc_bitstream_put_ue(bs, slice_param->pic_parameter_set_id);        /* pic_parameter_set_id: 0 */
  227.     avc_bitstream_put_ui(bs, pic_param->frame_num, sps_param->seq_fields.bits.log2_max_frame_num_minus4 + 4); /* frame_num */
  228.  
  229.     /* frame_mbs_only_flag == 1 */
  230.     if (!sps_param->seq_fields.bits.frame_mbs_only_flag) {
  231.         /* FIXME: */
  232.         assert(0);
  233.     }
  234.  
  235.     if (pic_param->pic_fields.bits.idr_pic_flag)
  236.         avc_bitstream_put_ue(bs, slice_param->idr_pic_id);              /* idr_pic_id: 0 */
  237.  
  238.     if (sps_param->seq_fields.bits.pic_order_cnt_type == 0) {
  239.         avc_bitstream_put_ui(bs, pic_param->CurrPic.TopFieldOrderCnt, sps_param->seq_fields.bits.log2_max_pic_order_cnt_lsb_minus4 + 4);
  240.         /* pic_order_present_flag == 0 */
  241.     } else {
  242.         /* FIXME: */
  243.         assert(0);
  244.     }
  245.  
  246.     /* redundant_pic_cnt_present_flag == 0 */
  247.    
  248.     /* slice type */
  249.     if (IS_P_SLICE(slice_param->slice_type)) {
  250.         avc_bitstream_put_ui(bs, slice_param->num_ref_idx_active_override_flag, 1);            /* num_ref_idx_active_override_flag: */
  251.  
  252.         if (slice_param->num_ref_idx_active_override_flag)
  253.             avc_bitstream_put_ue(bs, slice_param->num_ref_idx_l0_active_minus1);
  254.  
  255.         /* ref_pic_list_reordering */
  256.         avc_bitstream_put_ui(bs, 0, 1);            /* ref_pic_list_reordering_flag_l0: 0 */
  257.     } else if (IS_B_SLICE(slice_param->slice_type)) {
  258.         avc_bitstream_put_ui(bs, slice_param->direct_spatial_mv_pred_flag, 1);            /* direct_spatial_mv_pred: 1 */
  259.  
  260.         avc_bitstream_put_ui(bs, slice_param->num_ref_idx_active_override_flag, 1);       /* num_ref_idx_active_override_flag: */
  261.  
  262.         if (slice_param->num_ref_idx_active_override_flag) {
  263.             avc_bitstream_put_ue(bs, slice_param->num_ref_idx_l0_active_minus1);
  264.             avc_bitstream_put_ue(bs, slice_param->num_ref_idx_l1_active_minus1);
  265.         }
  266.  
  267.         /* ref_pic_list_reordering */
  268.         avc_bitstream_put_ui(bs, 0, 1);            /* ref_pic_list_reordering_flag_l0: 0 */
  269.         avc_bitstream_put_ui(bs, 0, 1);            /* ref_pic_list_reordering_flag_l1: 0 */
  270.     }
  271.  
  272.     if ((pic_param->pic_fields.bits.weighted_pred_flag &&
  273.          IS_P_SLICE(slice_param->slice_type)) ||
  274.         ((pic_param->pic_fields.bits.weighted_bipred_idc == 1) &&
  275.          IS_B_SLICE(slice_param->slice_type))) {
  276.         /* FIXME: fill weight/offset table */
  277.         assert(0);
  278.     }
  279.  
  280.     /* dec_ref_pic_marking */
  281.     if (pic_param->pic_fields.bits.reference_pic_flag) {     /* nal_ref_idc != 0 */
  282.         unsigned char no_output_of_prior_pics_flag = 0;
  283.         unsigned char long_term_reference_flag = 0;
  284.         unsigned char adaptive_ref_pic_marking_mode_flag = 0;
  285.  
  286.         if (pic_param->pic_fields.bits.idr_pic_flag) {
  287.             avc_bitstream_put_ui(bs, no_output_of_prior_pics_flag, 1);            /* no_output_of_prior_pics_flag: 0 */
  288.             avc_bitstream_put_ui(bs, long_term_reference_flag, 1);            /* long_term_reference_flag: 0 */
  289.         } else {
  290.             avc_bitstream_put_ui(bs, adaptive_ref_pic_marking_mode_flag, 1);            /* adaptive_ref_pic_marking_mode_flag: 0 */
  291.         }
  292.     }
  293.  
  294.     if (pic_param->pic_fields.bits.entropy_coding_mode_flag &&
  295.         !IS_I_SLICE(slice_param->slice_type))
  296.         avc_bitstream_put_ue(bs, slice_param->cabac_init_idc);               /* cabac_init_idc: 0 */
  297.  
  298.     avc_bitstream_put_se(bs, slice_param->slice_qp_delta);                   /* slice_qp_delta: 0 */
  299.  
  300.     /* ignore for SP/SI */
  301.  
  302.     if (pic_param->pic_fields.bits.deblocking_filter_control_present_flag) {
  303.         avc_bitstream_put_ue(bs, slice_param->disable_deblocking_filter_idc);           /* disable_deblocking_filter_idc: 0 */
  304.  
  305.         if (slice_param->disable_deblocking_filter_idc != 1) {
  306.             avc_bitstream_put_se(bs, slice_param->slice_alpha_c0_offset_div2);          /* slice_alpha_c0_offset_div2: 2 */
  307.             avc_bitstream_put_se(bs, slice_param->slice_beta_offset_div2);              /* slice_beta_offset_div2: 2 */
  308.         }
  309.     }
  310.  
  311.     if (pic_param->pic_fields.bits.entropy_coding_mode_flag) {
  312.         avc_bitstream_byte_aligning(bs, 1);
  313.     }
  314. }
  315.  
  316. int
  317. build_avc_slice_header(VAEncSequenceParameterBufferH264 *sps_param,
  318.                        VAEncPictureParameterBufferH264 *pic_param,
  319.                        VAEncSliceParameterBufferH264 *slice_param,
  320.                        unsigned char **slice_header_buffer)
  321. {
  322.     avc_bitstream bs;
  323.     int is_idr = !!pic_param->pic_fields.bits.idr_pic_flag;
  324.     int is_ref = !!pic_param->pic_fields.bits.reference_pic_flag;
  325.  
  326.     avc_bitstream_start(&bs);
  327.     nal_start_code_prefix(&bs);
  328.  
  329.     if (IS_I_SLICE(slice_param->slice_type)) {
  330.         nal_header(&bs, NAL_REF_IDC_HIGH, is_idr ? NAL_IDR : NAL_NON_IDR);
  331.     } else if (IS_P_SLICE(slice_param->slice_type)) {
  332.         assert(!is_idr);
  333.         nal_header(&bs, NAL_REF_IDC_MEDIUM, NAL_NON_IDR);
  334.     } else {
  335.         assert(IS_B_SLICE(slice_param->slice_type));
  336.         assert(!is_idr);
  337.         nal_header(&bs, is_ref ? NAL_REF_IDC_LOW : NAL_REF_IDC_NONE, NAL_NON_IDR);
  338.     }
  339.  
  340.     slice_header(&bs, sps_param, pic_param, slice_param);
  341.  
  342.     avc_bitstream_end(&bs);
  343.     *slice_header_buffer = (unsigned char *)bs.buffer;
  344.  
  345.     return bs.bit_offset;
  346. }
  347.  
  348. int
  349. build_avc_sei_buffering_period(int cpb_removal_length,
  350.                                unsigned int init_cpb_removal_delay,
  351.                                unsigned int init_cpb_removal_delay_offset,
  352.                                unsigned char **sei_buffer)
  353. {
  354.     unsigned char *byte_buf;
  355.     int byte_size, i;
  356.  
  357.     avc_bitstream nal_bs;
  358.     avc_bitstream sei_bs;
  359.  
  360.     avc_bitstream_start(&sei_bs);
  361.     avc_bitstream_put_ue(&sei_bs, 0);       /*seq_parameter_set_id*/
  362.     avc_bitstream_put_ui(&sei_bs, init_cpb_removal_delay, cpb_removal_length);
  363.     avc_bitstream_put_ui(&sei_bs, init_cpb_removal_delay_offset, cpb_removal_length);
  364.     if ( sei_bs.bit_offset & 0x7) {
  365.         avc_bitstream_put_ui(&sei_bs, 1, 1);
  366.     }
  367.     avc_bitstream_end(&sei_bs);
  368.     byte_size = (sei_bs.bit_offset + 7) / 8;
  369.    
  370.     avc_bitstream_start(&nal_bs);
  371.     nal_start_code_prefix(&nal_bs);
  372.     nal_header(&nal_bs, NAL_REF_IDC_NONE, NAL_SEI);
  373.    
  374.     avc_bitstream_put_ui(&nal_bs, 0, 8);
  375.     avc_bitstream_put_ui(&nal_bs, byte_size, 8);
  376.    
  377.     byte_buf = (unsigned char *)sei_bs.buffer;
  378.     for(i = 0; i < byte_size; i++) {
  379.         avc_bitstream_put_ui(&nal_bs, byte_buf[i], 8);
  380.     }
  381.     free(byte_buf);
  382.  
  383.     avc_rbsp_trailing_bits(&nal_bs);
  384.     avc_bitstream_end(&nal_bs);
  385.  
  386.     *sei_buffer = (unsigned char *)nal_bs.buffer;
  387.    
  388.     return nal_bs.bit_offset;
  389. }
  390.  
  391. int
  392. build_avc_sei_pic_timing(unsigned int cpb_removal_length, unsigned int cpb_removal_delay,
  393.                          unsigned int dpb_output_length, unsigned int dpb_output_delay,
  394.                          unsigned char **sei_buffer)
  395. {
  396.     unsigned char *byte_buf;
  397.     int byte_size, i;
  398.  
  399.     avc_bitstream nal_bs;
  400.     avc_bitstream sei_bs;
  401.  
  402.     avc_bitstream_start(&sei_bs);
  403.     avc_bitstream_put_ui(&sei_bs, cpb_removal_delay, cpb_removal_length);
  404.     avc_bitstream_put_ui(&sei_bs, dpb_output_delay, dpb_output_length);
  405.     if ( sei_bs.bit_offset & 0x7) {
  406.         avc_bitstream_put_ui(&sei_bs, 1, 1);
  407.     }
  408.     avc_bitstream_end(&sei_bs);
  409.     byte_size = (sei_bs.bit_offset + 7) / 8;
  410.    
  411.     avc_bitstream_start(&nal_bs);
  412.     nal_start_code_prefix(&nal_bs);
  413.     nal_header(&nal_bs, NAL_REF_IDC_NONE, NAL_SEI);
  414.    
  415.     avc_bitstream_put_ui(&nal_bs, 0x01, 8);
  416.     avc_bitstream_put_ui(&nal_bs, byte_size, 8);
  417.    
  418.     byte_buf = (unsigned char *)sei_bs.buffer;
  419.     for(i = 0; i < byte_size; i++) {
  420.         avc_bitstream_put_ui(&nal_bs, byte_buf[i], 8);
  421.     }
  422.     free(byte_buf);
  423.  
  424.     avc_rbsp_trailing_bits(&nal_bs);
  425.     avc_bitstream_end(&nal_bs);
  426.  
  427.     *sei_buffer = (unsigned char *)nal_bs.buffer;
  428.    
  429.     return nal_bs.bit_offset;
  430. }
  431.  
  432.  
  433. int
  434. build_avc_sei_buffer_timing(unsigned int init_cpb_removal_length,
  435.                                 unsigned int init_cpb_removal_delay,
  436.                                 unsigned int init_cpb_removal_delay_offset,
  437.                                 unsigned int cpb_removal_length,
  438.                                 unsigned int cpb_removal_delay,
  439.                                 unsigned int dpb_output_length,
  440.                                 unsigned int dpb_output_delay,
  441.                                 unsigned char **sei_buffer)
  442. {
  443.     unsigned char *byte_buf;
  444.     int bp_byte_size, i, pic_byte_size;
  445.  
  446.     avc_bitstream nal_bs;
  447.     avc_bitstream sei_bp_bs, sei_pic_bs;
  448.  
  449.     avc_bitstream_start(&sei_bp_bs);
  450.     avc_bitstream_put_ue(&sei_bp_bs, 0);       /*seq_parameter_set_id*/
  451.     avc_bitstream_put_ui(&sei_bp_bs, init_cpb_removal_delay, cpb_removal_length);
  452.     avc_bitstream_put_ui(&sei_bp_bs, init_cpb_removal_delay_offset, cpb_removal_length);
  453.     if ( sei_bp_bs.bit_offset & 0x7) {
  454.         avc_bitstream_put_ui(&sei_bp_bs, 1, 1);
  455.     }
  456.     avc_bitstream_end(&sei_bp_bs);
  457.     bp_byte_size = (sei_bp_bs.bit_offset + 7) / 8;
  458.    
  459.     avc_bitstream_start(&sei_pic_bs);
  460.     avc_bitstream_put_ui(&sei_pic_bs, cpb_removal_delay, cpb_removal_length);
  461.     avc_bitstream_put_ui(&sei_pic_bs, dpb_output_delay, dpb_output_length);
  462.     if ( sei_pic_bs.bit_offset & 0x7) {
  463.         avc_bitstream_put_ui(&sei_pic_bs, 1, 1);
  464.     }
  465.     avc_bitstream_end(&sei_pic_bs);
  466.     pic_byte_size = (sei_pic_bs.bit_offset + 7) / 8;
  467.    
  468.     avc_bitstream_start(&nal_bs);
  469.     nal_start_code_prefix(&nal_bs);
  470.     nal_header(&nal_bs, NAL_REF_IDC_NONE, NAL_SEI);
  471.  
  472.         /* Write the SEI buffer period data */    
  473.     avc_bitstream_put_ui(&nal_bs, 0, 8);
  474.     avc_bitstream_put_ui(&nal_bs, bp_byte_size, 8);
  475.    
  476.     byte_buf = (unsigned char *)sei_bp_bs.buffer;
  477.     for(i = 0; i < bp_byte_size; i++) {
  478.         avc_bitstream_put_ui(&nal_bs, byte_buf[i], 8);
  479.     }
  480.     free(byte_buf);
  481.         /* write the SEI timing data */
  482.     avc_bitstream_put_ui(&nal_bs, 0x01, 8);
  483.     avc_bitstream_put_ui(&nal_bs, pic_byte_size, 8);
  484.    
  485.     byte_buf = (unsigned char *)sei_pic_bs.buffer;
  486.     for(i = 0; i < pic_byte_size; i++) {
  487.         avc_bitstream_put_ui(&nal_bs, byte_buf[i], 8);
  488.     }
  489.     free(byte_buf);
  490.  
  491.     avc_rbsp_trailing_bits(&nal_bs);
  492.     avc_bitstream_end(&nal_bs);
  493.  
  494.     *sei_buffer = (unsigned char *)nal_bs.buffer;
  495.    
  496.     return nal_bs.bit_offset;
  497. }
  498.  
  499. int
  500. build_mpeg2_slice_header(VAEncSequenceParameterBufferMPEG2 *sps_param,
  501.                          VAEncPictureParameterBufferMPEG2 *pic_param,
  502.                          VAEncSliceParameterBufferMPEG2 *slice_param,
  503.                          unsigned char **slice_header_buffer)
  504. {
  505.     avc_bitstream bs;
  506.  
  507.     avc_bitstream_start(&bs);
  508.     avc_bitstream_end(&bs);
  509.     *slice_header_buffer = (unsigned char *)bs.buffer;
  510.  
  511.     return bs.bit_offset;
  512. }
  513.  
  514. static void binarize_qindex_delta(avc_bitstream *bs, int qindex_delta)
  515. {
  516.     if (qindex_delta == 0)
  517.         avc_bitstream_put_ui(bs, 0, 1);
  518.     else {
  519.        avc_bitstream_put_ui(bs, 1, 1);
  520.        avc_bitstream_put_ui(bs, abs(qindex_delta), 4);
  521.  
  522.        if (qindex_delta < 0)
  523.            avc_bitstream_put_ui(bs, 1, 1);
  524.        else
  525.            avc_bitstream_put_ui(bs, 0, 1);
  526.     }
  527. }
  528.  
  529. void binarize_vp8_frame_header(VAEncSequenceParameterBufferVP8 *seq_param,
  530.                            VAEncPictureParameterBufferVP8 *pic_param,
  531.                            VAQMatrixBufferVP8 *q_matrix,
  532.                            struct gen6_mfc_context *mfc_context,
  533.                            struct intel_encoder_context *encoder_context)
  534. {
  535.     avc_bitstream bs;
  536.     int i, j;
  537.     int is_intra_frame = !pic_param->pic_flags.bits.frame_type;
  538.     int log2num = pic_param->pic_flags.bits.num_token_partitions;
  539.  
  540.     /* modify picture paramters */
  541.     pic_param->pic_flags.bits.loop_filter_adj_enable = 1;
  542.     pic_param->pic_flags.bits.mb_no_coeff_skip = 1;
  543.     pic_param->pic_flags.bits.forced_lf_adjustment = 1;
  544.     pic_param->pic_flags.bits.refresh_entropy_probs = 1;
  545.     pic_param->pic_flags.bits.segmentation_enabled = 0;
  546.  
  547.     pic_param->pic_flags.bits.loop_filter_type = pic_param->pic_flags.bits.version / 2;
  548.     if (pic_param->pic_flags.bits.version > 1)
  549.         pic_param->loop_filter_level[0] = 0;
  550.  
  551.     avc_bitstream_start(&bs);
  552.  
  553.     if (is_intra_frame) {
  554.        avc_bitstream_put_ui(&bs, 0, 1);
  555.        avc_bitstream_put_ui(&bs, pic_param->pic_flags.bits.clamping_type ,1);
  556.     }
  557.  
  558.     avc_bitstream_put_ui(&bs, pic_param->pic_flags.bits.segmentation_enabled, 1);
  559.    
  560.     if (pic_param->pic_flags.bits.segmentation_enabled) {
  561.         avc_bitstream_put_ui(&bs, pic_param->pic_flags.bits.update_mb_segmentation_map, 1);
  562.         avc_bitstream_put_ui(&bs, pic_param->pic_flags.bits.update_segment_feature_data, 1);
  563.         if (pic_param->pic_flags.bits.update_segment_feature_data) {
  564.             /*add it later*/
  565.             assert(0);
  566.         }
  567.         if (pic_param->pic_flags.bits.update_mb_segmentation_map) {
  568.            for (i = 0; i < 3; i++) {
  569.               if (mfc_context->vp8_state.mb_segment_tree_probs[i] == 255)
  570.                   avc_bitstream_put_ui(&bs, 0, 1);
  571.               else {
  572.                   avc_bitstream_put_ui(&bs, 1, 1);
  573.                   avc_bitstream_put_ui(&bs, mfc_context->vp8_state.mb_segment_tree_probs[i], 8);
  574.               }
  575.            }
  576.         }
  577.     }
  578.  
  579.     avc_bitstream_put_ui(&bs, pic_param->pic_flags.bits.loop_filter_type, 1);
  580.     avc_bitstream_put_ui(&bs, pic_param->loop_filter_level[0], 6);
  581.     avc_bitstream_put_ui(&bs, pic_param->sharpness_level, 3);
  582.    
  583.     mfc_context->vp8_state.frame_header_lf_update_pos = bs.bit_offset;
  584.    
  585.     if (pic_param->pic_flags.bits.forced_lf_adjustment) {
  586.         avc_bitstream_put_ui(&bs, 1, 1);//mode_ref_lf_delta_enable = 1
  587.         avc_bitstream_put_ui(&bs, 1, 1);//mode_ref_lf_delta_update = 1
  588.  
  589.         for (i =0; i < 4; i++) {
  590.             avc_bitstream_put_ui(&bs, 1, 1);
  591.             if (pic_param->ref_lf_delta[i] > 0) {
  592.                 avc_bitstream_put_ui(&bs, (abs(pic_param->ref_lf_delta[i]) & 0x3F), 6);
  593.                 avc_bitstream_put_ui(&bs, 0, 1);
  594.             } else {
  595.                 avc_bitstream_put_ui(&bs, (abs(pic_param->ref_lf_delta[i]) & 0x3F), 6);
  596.                 avc_bitstream_put_ui(&bs, 1, 1);
  597.             }
  598.         }
  599.  
  600.         for (i =0; i < 4; i++) {
  601.             avc_bitstream_put_ui(&bs, 1, 1);
  602.             if (pic_param->mode_lf_delta[i] > 0) {
  603.                 avc_bitstream_put_ui(&bs, (abs(pic_param->mode_lf_delta[i]) & 0x3F), 6);
  604.                 avc_bitstream_put_ui(&bs, 0, 1);
  605.             } else {
  606.                 avc_bitstream_put_ui(&bs, (abs(pic_param->mode_lf_delta[i]) & 0x3F), 6);
  607.                 avc_bitstream_put_ui(&bs, 1, 1);
  608.             }
  609.         }
  610.  
  611.     } else {
  612.         avc_bitstream_put_ui(&bs, 0, 1);//mode_ref_lf_delta_enable = 0
  613.     }
  614.  
  615.     avc_bitstream_put_ui(&bs, log2num, 2);
  616.    
  617.     mfc_context->vp8_state.frame_header_qindex_update_pos = bs.bit_offset;
  618.  
  619.     avc_bitstream_put_ui(&bs, q_matrix->quantization_index[0], 7);
  620.    
  621.     for (i = 0; i < 5; i++)
  622.         binarize_qindex_delta(&bs, q_matrix->quantization_index_delta[i]);
  623.  
  624.     if (!is_intra_frame) {
  625.         avc_bitstream_put_ui(&bs, pic_param->pic_flags.bits.refresh_golden_frame, 1);
  626.         avc_bitstream_put_ui(&bs, pic_param->pic_flags.bits.refresh_alternate_frame, 1);
  627.  
  628.         if (!pic_param->pic_flags.bits.refresh_golden_frame)
  629.             avc_bitstream_put_ui(&bs, pic_param->pic_flags.bits.copy_buffer_to_golden, 2);
  630.  
  631.         if (!pic_param->pic_flags.bits.refresh_alternate_frame)
  632.             avc_bitstream_put_ui(&bs, pic_param->pic_flags.bits.copy_buffer_to_alternate, 2);
  633.        
  634.         avc_bitstream_put_ui(&bs, pic_param->pic_flags.bits.sign_bias_golden, 1);
  635.         avc_bitstream_put_ui(&bs, pic_param->pic_flags.bits.sign_bias_alternate, 1);
  636.     }
  637.    
  638.     avc_bitstream_put_ui(&bs, pic_param->pic_flags.bits.refresh_entropy_probs, 1);
  639.  
  640.     if (!is_intra_frame)
  641.         avc_bitstream_put_ui(&bs, pic_param->pic_flags.bits.refresh_last, 1);
  642.  
  643.     mfc_context->vp8_state.frame_header_token_update_pos = bs.bit_offset;
  644.  
  645.     for (i = 0; i < 4 * 8 * 3 * 11; i++)
  646.         avc_bitstream_put_ui(&bs, 0, 1); //don't update coeff_probs
  647.  
  648.     avc_bitstream_put_ui(&bs, pic_param->pic_flags.bits.mb_no_coeff_skip, 1);
  649.     if (pic_param->pic_flags.bits.mb_no_coeff_skip)
  650.         avc_bitstream_put_ui(&bs, mfc_context->vp8_state.prob_skip_false, 8);
  651.  
  652.     if (!is_intra_frame) {
  653.         avc_bitstream_put_ui(&bs, mfc_context->vp8_state.prob_intra, 8);
  654.         avc_bitstream_put_ui(&bs, mfc_context->vp8_state.prob_last, 8);
  655.         avc_bitstream_put_ui(&bs, mfc_context->vp8_state.prob_gf, 8);
  656.  
  657.         avc_bitstream_put_ui(&bs, 1, 1); //y_mode_update_flag = 1
  658.         for (i = 0; i < 4; i++) {
  659.             avc_bitstream_put_ui(&bs, mfc_context->vp8_state.y_mode_probs[i], 8);
  660.         }
  661.  
  662.         avc_bitstream_put_ui(&bs, 1, 1); //uv_mode_update_flag = 1
  663.         for (i = 0; i < 3; i++) {
  664.             avc_bitstream_put_ui(&bs, mfc_context->vp8_state.uv_mode_probs[i], 8);
  665.         }
  666.  
  667.         mfc_context->vp8_state.frame_header_bin_mv_upate_pos = bs.bit_offset;
  668.        
  669.         for (i = 0; i < 2 ; i++) {
  670.             for (j = 0; j < 19; j++) {
  671.                 avc_bitstream_put_ui(&bs, 0, 1);
  672.                 //avc_bitstream_put_ui(&bs, mfc_context->vp8_state.mv_probs[i][j], 7);
  673.             }
  674.         }
  675.     }
  676.  
  677.     avc_bitstream_end(&bs);
  678.  
  679.     mfc_context->vp8_state.vp8_frame_header = (unsigned char *)bs.buffer;
  680.     mfc_context->vp8_state.frame_header_bit_count = bs.bit_offset;
  681. }
  682.  
  683. /* HEVC to do for internal header generated*/
  684.  
  685. void nal_header_hevc(avc_bitstream *bs, int nal_unit_type, int temporalid)
  686. {
  687.     /* forbidden_zero_bit: 0 */
  688.     avc_bitstream_put_ui(bs, 0, 1);
  689.     /* nal unit_type */
  690.     avc_bitstream_put_ui(bs, nal_unit_type, 6);
  691.     /* layer_id. currently it is zero */
  692.     avc_bitstream_put_ui(bs, 0, 6);
  693.     /* teporalid + 1 .*/
  694.     avc_bitstream_put_ui(bs, temporalid + 1, 3);
  695. }
  696.  
  697. int build_hevc_sei_buffering_period(int init_cpb_removal_delay_length,
  698.                                 unsigned int init_cpb_removal_delay,
  699.                                 unsigned int init_cpb_removal_delay_offset,
  700.                                 unsigned char **sei_buffer)
  701. {
  702.     unsigned char *byte_buf;
  703.     int bp_byte_size, i;
  704.     //unsigned int cpb_removal_delay;
  705.  
  706.     avc_bitstream nal_bs;
  707.     avc_bitstream sei_bp_bs;
  708.  
  709.     avc_bitstream_start(&sei_bp_bs);
  710.     avc_bitstream_put_ue(&sei_bp_bs, 0);       /*seq_parameter_set_id*/
  711.     /* SEI buffer period info */
  712.     /* NALHrdBpPresentFlag == 1 */
  713.     avc_bitstream_put_ui(&sei_bp_bs, init_cpb_removal_delay,init_cpb_removal_delay_length);
  714.     avc_bitstream_put_ui(&sei_bp_bs, init_cpb_removal_delay_offset,init_cpb_removal_delay_length);
  715.     if ( sei_bp_bs.bit_offset & 0x7) {
  716.         avc_bitstream_put_ui(&sei_bp_bs, 1, 1);
  717.     }
  718.     avc_bitstream_end(&sei_bp_bs);
  719.     bp_byte_size = (sei_bp_bs.bit_offset + 7) / 8;
  720.  
  721.     avc_bitstream_start(&nal_bs);
  722.     nal_start_code_prefix(&nal_bs);
  723.     nal_header_hevc(&nal_bs, PREFIX_SEI_NUT ,0);
  724.  
  725.     /* Write the SEI buffer period data */
  726.     avc_bitstream_put_ui(&nal_bs, 0, 8);
  727.     avc_bitstream_put_ui(&nal_bs, bp_byte_size, 8);
  728.  
  729.     byte_buf = (unsigned char *)sei_bp_bs.buffer;
  730.     for(i = 0; i < bp_byte_size; i++) {
  731.         avc_bitstream_put_ui(&nal_bs, byte_buf[i], 8);
  732.     }
  733.     free(byte_buf);
  734.  
  735.     avc_rbsp_trailing_bits(&nal_bs);
  736.     avc_bitstream_end(&nal_bs);
  737.  
  738.     *sei_buffer = (unsigned char *)nal_bs.buffer;
  739.  
  740.     return nal_bs.bit_offset;
  741. }
  742.  
  743. int build_hevc_idr_sei_buffer_timing(unsigned int init_cpb_removal_delay_length,
  744.                                  unsigned int init_cpb_removal_delay,
  745.                                  unsigned int init_cpb_removal_delay_offset,
  746.                                  unsigned int cpb_removal_length,
  747.                                  unsigned int cpb_removal_delay,
  748.                                  unsigned int dpb_output_length,
  749.                                  unsigned int dpb_output_delay,
  750.                                  unsigned char **sei_buffer)
  751. {
  752.     unsigned char *byte_buf;
  753.     int bp_byte_size, i, pic_byte_size;
  754.     //unsigned int cpb_removal_delay;
  755.  
  756.     avc_bitstream nal_bs;
  757.     avc_bitstream sei_bp_bs, sei_pic_bs;
  758.  
  759.     avc_bitstream_start(&sei_bp_bs);
  760.     avc_bitstream_put_ue(&sei_bp_bs, 0);       /*seq_parameter_set_id*/
  761.     /* SEI buffer period info */
  762.     /* NALHrdBpPresentFlag == 1 */
  763.     avc_bitstream_put_ui(&sei_bp_bs, init_cpb_removal_delay,init_cpb_removal_delay_length);
  764.     avc_bitstream_put_ui(&sei_bp_bs, init_cpb_removal_delay_offset,init_cpb_removal_delay_length);
  765.     if ( sei_bp_bs.bit_offset & 0x7) {
  766.         avc_bitstream_put_ui(&sei_bp_bs, 1, 1);
  767.     }
  768.     avc_bitstream_end(&sei_bp_bs);
  769.     bp_byte_size = (sei_bp_bs.bit_offset + 7) / 8;
  770.  
  771.     /* SEI pic timing info */
  772.     avc_bitstream_start(&sei_pic_bs);
  773.     /* The info of CPB and DPB delay is controlled by CpbDpbDelaysPresentFlag,
  774.     * which is derived as 1 if one of the following conditions is true:
  775.     * nal_hrd_parameters_present_flag is present in the avc_bitstream and is equal to 1,
  776.     * vcl_hrd_parameters_present_flag is present in the avc_bitstream and is equal to 1,
  777.     */
  778.     //cpb_removal_delay = (hevc_context.current_cpb_removal - hevc_context.prev_idr_cpb_removal);
  779.     avc_bitstream_put_ui(&sei_pic_bs, cpb_removal_delay, cpb_removal_length);
  780.     avc_bitstream_put_ui(&sei_pic_bs, dpb_output_delay,dpb_output_length);
  781.     if ( sei_pic_bs.bit_offset & 0x7) {
  782.         avc_bitstream_put_ui(&sei_pic_bs, 1, 1);
  783.     }
  784.     /* The pic_structure_present_flag determines whether the pic_structure
  785.     * info is written into the SEI pic timing info.
  786.     * Currently it is set to zero.
  787.     */
  788.     avc_bitstream_end(&sei_pic_bs);
  789.     pic_byte_size = (sei_pic_bs.bit_offset + 7) / 8;
  790.  
  791.     avc_bitstream_start(&nal_bs);
  792.     nal_start_code_prefix(&nal_bs);
  793.     nal_header_hevc(&nal_bs, PREFIX_SEI_NUT ,0);
  794.  
  795.     /* Write the SEI buffer period data */
  796.     avc_bitstream_put_ui(&nal_bs, 0, 8);
  797.     avc_bitstream_put_ui(&nal_bs, bp_byte_size, 8);
  798.  
  799.     byte_buf = (unsigned char *)sei_bp_bs.buffer;
  800.     for(i = 0; i < bp_byte_size; i++) {
  801.         avc_bitstream_put_ui(&nal_bs, byte_buf[i], 8);
  802.     }
  803.     free(byte_buf);
  804.     /* write the SEI pic timing data */
  805.     avc_bitstream_put_ui(&nal_bs, 0x01, 8);
  806.     avc_bitstream_put_ui(&nal_bs, pic_byte_size, 8);
  807.  
  808.     byte_buf = (unsigned char *)sei_pic_bs.buffer;
  809.     for(i = 0; i < pic_byte_size; i++) {
  810.         avc_bitstream_put_ui(&nal_bs, byte_buf[i], 8);
  811.     }
  812.     free(byte_buf);
  813.  
  814.     avc_rbsp_trailing_bits(&nal_bs);
  815.     avc_bitstream_end(&nal_bs);
  816.  
  817.     *sei_buffer = (unsigned char *)nal_bs.buffer;
  818.  
  819.     return nal_bs.bit_offset;
  820. }
  821.  
  822. int build_hevc_sei_pic_timing(unsigned int cpb_removal_length, unsigned int cpb_removal_delay,
  823.                          unsigned int dpb_output_length, unsigned int dpb_output_delay,
  824.                          unsigned char **sei_buffer)
  825. {
  826.     unsigned char *byte_buf;
  827.     int i, pic_byte_size;
  828.     //unsigned int cpb_removal_delay;
  829.  
  830.     avc_bitstream nal_bs;
  831.     avc_bitstream sei_pic_bs;
  832.  
  833.     avc_bitstream_start(&sei_pic_bs);
  834.     /* The info of CPB and DPB delay is controlled by CpbDpbDelaysPresentFlag,
  835.     * which is derived as 1 if one of the following conditions is true:
  836.     * nal_hrd_parameters_present_flag is present in the avc_bitstream and is equal to 1,
  837.     * vcl_hrd_parameters_present_flag is present in the avc_bitstream and is equal to 1,
  838.     */
  839.     //cpb_removal_delay = (hevc_context.current_cpb_removal - hevc_context.current_idr_cpb_removal);
  840.     avc_bitstream_put_ui(&sei_pic_bs, cpb_removal_delay, cpb_removal_length);
  841.     avc_bitstream_put_ui(&sei_pic_bs, dpb_output_delay,  dpb_output_length);
  842.     if ( sei_pic_bs.bit_offset & 0x7) {
  843.         avc_bitstream_put_ui(&sei_pic_bs, 1, 1);
  844.     }
  845.  
  846.     /* The pic_structure_present_flag determines whether the pic_structure
  847.     * info is written into the SEI pic timing info.
  848.     * Currently it is set to zero.
  849.     */
  850.     avc_bitstream_end(&sei_pic_bs);
  851.     pic_byte_size = (sei_pic_bs.bit_offset + 7) / 8;
  852.  
  853.     avc_bitstream_start(&nal_bs);
  854.     nal_start_code_prefix(&nal_bs);
  855.     nal_header_hevc(&nal_bs, PREFIX_SEI_NUT ,0);
  856.  
  857.     /* write the SEI Pic timing data */
  858.     avc_bitstream_put_ui(&nal_bs, 0x01, 8);
  859.     avc_bitstream_put_ui(&nal_bs, pic_byte_size, 8);
  860.  
  861.     byte_buf = (unsigned char *)sei_pic_bs.buffer;
  862.     for(i = 0; i < pic_byte_size; i++) {
  863.         avc_bitstream_put_ui(&nal_bs, byte_buf[i], 8);
  864.     }
  865.     free(byte_buf);
  866.  
  867.     avc_rbsp_trailing_bits(&nal_bs);
  868.     avc_bitstream_end(&nal_bs);
  869.  
  870.     *sei_buffer = (unsigned char *)nal_bs.buffer;
  871.  
  872.     return nal_bs.bit_offset;
  873. }
  874.  
  875. typedef struct _RefPicSet
  876. {
  877.     unsigned char    num_negative_pics;
  878.     unsigned char    num_positive_pics;
  879.     unsigned char    delta_poc_s0_minus1[8];
  880.     unsigned char    used_by_curr_pic_s0_flag[8];
  881.     unsigned char    delta_poc_s1_minus1[8];
  882.     unsigned char    used_by_curr_pic_s1_flag[8];
  883.     unsigned int     inter_ref_pic_set_prediction_flag;
  884. }hevcRefPicSet;
  885.  
  886. void hevc_short_term_ref_pic_set(avc_bitstream *bs,VAEncSliceParameterBufferHEVC *slice_param,int curPicOrderCnt)
  887. {
  888.     hevcRefPicSet hevc_rps;
  889.     int rps_idx = 1, ref_idx = 0;
  890.     int i = 0;
  891.  
  892.     hevc_rps.inter_ref_pic_set_prediction_flag = 0;
  893.     /* s0: between I and P/B; s1 : between P and B */
  894.     hevc_rps.num_negative_pics               = (slice_param->slice_type!=HEVC_SLICE_I) ? 1 : 0;
  895.     hevc_rps.num_positive_pics               = (slice_param->slice_type==HEVC_SLICE_B) ? 1 : 0;
  896.     hevc_rps.delta_poc_s0_minus1[0]          = 0;
  897.     hevc_rps.used_by_curr_pic_s0_flag[0]     = 0;
  898.     hevc_rps.delta_poc_s1_minus1[0]          = 0;
  899.     hevc_rps.used_by_curr_pic_s1_flag[0]     = 0;
  900.     if(slice_param->num_ref_idx_l0_active_minus1==0 )
  901.     {
  902.         hevc_rps.delta_poc_s0_minus1[0]          = (slice_param->slice_type==HEVC_SLICE_I) ? 0 : ( curPicOrderCnt - slice_param->ref_pic_list0[0].pic_order_cnt-1); //0;
  903.         hevc_rps.used_by_curr_pic_s0_flag[0]     = 1;
  904.     }
  905.     if(slice_param->num_ref_idx_l1_active_minus1==0 )
  906.     {
  907.         hevc_rps.delta_poc_s1_minus1[0]          = (slice_param->slice_type==HEVC_SLICE_I) ? 0 : ( slice_param->ref_pic_list1[0].pic_order_cnt -curPicOrderCnt -1);
  908.         hevc_rps.used_by_curr_pic_s1_flag[0]     = 1;
  909.     }
  910.  
  911.     if (rps_idx)
  912.         avc_bitstream_put_ui(bs, hevc_rps.inter_ref_pic_set_prediction_flag, 1);
  913.  
  914.     if (hevc_rps.inter_ref_pic_set_prediction_flag)
  915.     {
  916.         /* not support */
  917.         /* to do */
  918.     } else {
  919.         avc_bitstream_put_ue(bs, hevc_rps.num_negative_pics);
  920.         avc_bitstream_put_ue(bs, hevc_rps.num_positive_pics);
  921.  
  922.         for (i = 0; i < hevc_rps.num_negative_pics; i++)
  923.         {
  924.             avc_bitstream_put_ue(bs, hevc_rps.delta_poc_s0_minus1[ref_idx]);
  925.             avc_bitstream_put_ui(bs, hevc_rps.used_by_curr_pic_s0_flag[ref_idx], 1);
  926.         }
  927.         for (i = 0; i < hevc_rps.num_positive_pics; i++)
  928.         {
  929.             avc_bitstream_put_ue(bs, hevc_rps.delta_poc_s1_minus1[ref_idx]);
  930.             avc_bitstream_put_ui(bs, hevc_rps.used_by_curr_pic_s1_flag[ref_idx], 1);
  931.         }
  932.     }
  933.  
  934.     return;
  935. }
  936.  
  937. static void slice_rbsp(avc_bitstream *bs,
  938.                        int slice_index,
  939.                        VAEncSequenceParameterBufferHEVC *seq_param,
  940.                        VAEncPictureParameterBufferHEVC *pic_param,
  941.                        VAEncSliceParameterBufferHEVC *slice_param)
  942. {
  943.     int log2_cu_size = seq_param->log2_min_luma_coding_block_size_minus3 + 3;
  944.     int log2_ctb_size = seq_param->log2_diff_max_min_luma_coding_block_size + log2_cu_size;
  945.     int ctb_size = 1 << log2_ctb_size;
  946.  
  947.     int picture_width_in_ctb = (seq_param->pic_width_in_luma_samples + ctb_size - 1) / ctb_size;
  948.     int picture_height_in_ctb = (seq_param->pic_height_in_luma_samples + ctb_size - 1) / ctb_size;
  949.  
  950.     /* first_slice_segment_in_pic_flag */
  951.     if (slice_index == 0)
  952.     {
  953.         avc_bitstream_put_ui(bs, 1, 1);
  954.     }
  955.     else
  956.     {
  957.         avc_bitstream_put_ui(bs, 0, 1);
  958.     }
  959.  
  960.     /* no_output_of_prior_pics_flag */
  961.     if (pic_param->pic_fields.bits.idr_pic_flag)
  962.         avc_bitstream_put_ui(bs, 1, 1);
  963.  
  964.     /* slice_pic_parameter_set_id */
  965.     avc_bitstream_put_ue(bs, 0);
  966.  
  967.     /* not the first slice */
  968.     if (slice_index)
  969.     {
  970.         /* TBD */
  971.         int bit_size;
  972.  
  973.         float num_ctus;
  974.  
  975.         num_ctus = picture_width_in_ctb * picture_height_in_ctb;
  976.         bit_size = ceilf(log2f(num_ctus));
  977.  
  978.         if (pic_param->pic_fields.bits.dependent_slice_segments_enabled_flag)
  979.         {
  980.             avc_bitstream_put_ui(bs,
  981.                 slice_param->slice_fields.bits.dependent_slice_segment_flag, 1);
  982.         }
  983.         /* slice_segment_address is based on Ceil(log2(PictureSizeinCtbs)) */
  984.         avc_bitstream_put_ui(bs, slice_param->slice_segment_address, bit_size);
  985.     }
  986.     if (!slice_param->slice_fields.bits.dependent_slice_segment_flag)
  987.     {
  988.         /* slice_reserved_flag */
  989.  
  990.         /* slice_type */
  991.         avc_bitstream_put_ue(bs, slice_param->slice_type);
  992.         /* use the inferred the value of pic_output_flag */
  993.  
  994.         /* colour_plane_id */
  995.         if (seq_param->seq_fields.bits.separate_colour_plane_flag)
  996.         {
  997.             avc_bitstream_put_ui(bs, slice_param->slice_fields.bits.colour_plane_id, 1);
  998.         }
  999.  
  1000.         if (!pic_param->pic_fields.bits.idr_pic_flag)
  1001.         {
  1002.             int Log2MaxPicOrderCntLsb = 8;
  1003.             avc_bitstream_put_ui(bs, pic_param->decoded_curr_pic.pic_order_cnt, Log2MaxPicOrderCntLsb);
  1004.  
  1005.             //if (!slice_param->short_term_ref_pic_set_sps_flag)
  1006.             {
  1007.                 /* short_term_ref_pic_set_sps_flag.
  1008.                 * Use zero and then pass the RPS from slice_header
  1009.                 */
  1010.                 avc_bitstream_put_ui(bs, 0, 1);
  1011.                 /* TBD
  1012.                 * Add the short_term reference picture set
  1013.                 */
  1014.                 hevc_short_term_ref_pic_set(bs,slice_param,pic_param->decoded_curr_pic.pic_order_cnt);
  1015.             }
  1016.             /* long term reference present flag. unpresent */
  1017.             /* TBD */
  1018.  
  1019.             /* sps temporal MVP*/
  1020.             if (seq_param->seq_fields.bits.sps_temporal_mvp_enabled_flag)
  1021.             {
  1022.                 avc_bitstream_put_ui(bs,
  1023.                     slice_param->slice_fields.bits.slice_temporal_mvp_enabled_flag, 1);
  1024.             }
  1025.         }
  1026.  
  1027.         /* long term reference present flag. unpresent */
  1028.  
  1029.         /* sample adaptive offset enabled flag */
  1030.         if (seq_param->seq_fields.bits.sample_adaptive_offset_enabled_flag)
  1031.         {
  1032.             avc_bitstream_put_ui(bs, slice_param->slice_fields.bits.slice_sao_luma_flag, 1);
  1033.             avc_bitstream_put_ui(bs, slice_param->slice_fields.bits.slice_sao_chroma_flag, 1);
  1034.         }
  1035.  
  1036.         if (slice_param->slice_type != HEVC_SLICE_I)
  1037.         {
  1038.             /* num_ref_idx_active_override_flag. 0 */
  1039.             avc_bitstream_put_ui(bs, 0, 1);
  1040.             /* lists_modification_flag is unpresent NumPocTotalCurr > 1 ,here it is 1*/
  1041.  
  1042.             /* No reference picture set modification */
  1043.  
  1044.             /* MVD_l1_zero_flag */
  1045.             if (slice_param->slice_type == HEVC_SLICE_B)
  1046.                 avc_bitstream_put_ui(bs, slice_param->slice_fields.bits.mvd_l1_zero_flag, 1);
  1047.  
  1048.             /* cabac_init_present_flag. 0 */
  1049.  
  1050.             /* slice_temporal_mvp_enabled_flag. */
  1051.             if (slice_param->slice_fields.bits.slice_temporal_mvp_enabled_flag)
  1052.             {
  1053.                 if (slice_param->slice_type == HEVC_SLICE_B)
  1054.                     avc_bitstream_put_ui(bs, slice_param->slice_fields.bits.collocated_from_l0_flag, 1);
  1055.                 /*
  1056.                 * TBD: Add the collocated_ref_idx.
  1057.                 */
  1058.             }
  1059.             if (((pic_param->pic_fields.bits.weighted_pred_flag) &&
  1060.                 (slice_param->slice_type == HEVC_SLICE_P)) ||
  1061.                 ((pic_param->pic_fields.bits.weighted_bipred_flag) &&
  1062.                 (slice_param->slice_type == HEVC_SLICE_B)))
  1063.             {
  1064.                 /* TBD:
  1065.                 * add the weighted table
  1066.                 */
  1067.             }
  1068.             avc_bitstream_put_ue(bs, 5 - slice_param->max_num_merge_cand);
  1069.         }
  1070.         /* slice_qp_delta */
  1071.         avc_bitstream_put_ue(bs, slice_param->slice_qp_delta);
  1072.  
  1073.         /* slice_cb/cr_qp_offset is controlled by pps_slice_chroma_qp_offsets_present_flag
  1074.         * The present flag is set to 1.
  1075.         */
  1076.         avc_bitstream_put_ue(bs, slice_param->slice_cb_qp_offset);
  1077.         avc_bitstream_put_ue(bs, slice_param->slice_cr_qp_offset);
  1078.  
  1079.         /*
  1080.         * deblocking_filter_override_flag is controlled by
  1081.         * deblocking_filter_override_enabled_flag.
  1082.         * The override_enabled_flag is zero.
  1083.         * deblocking_filter_override_flag is zero. then
  1084.         * slice_deblocking_filter_disabled_flag is also zero
  1085.         * (It is inferred to be equal to pps_deblocking_filter_disabled_flag.
  1086.         */
  1087.  
  1088.         /* slice_loop_filter_across_slices_enabled_flag is controlled
  1089.         * by pps_loop_filter_across_slices_enabled_flag &&
  1090.         * (slice_sao_luma_flag | | slice_sao_chroma_flag | |
  1091.         *  !slice_deblocking_filter_disabled_flag ))
  1092.         *
  1093.         */
  1094.     }
  1095.  
  1096.     if (pic_param->pic_fields.bits.tiles_enabled_flag ||
  1097.         pic_param->pic_fields.bits.entropy_coding_sync_enabled_flag)
  1098.     {
  1099.         /* TBD.
  1100.         * Add the Entry-points && tile definition.
  1101.         */
  1102.     }
  1103.  
  1104.     /* slice_segment_header_extension_present_flag. Not present */
  1105.  
  1106.     /* byte_alignment */
  1107.     avc_rbsp_trailing_bits(bs);
  1108. }
  1109.  
  1110. int get_hevc_slice_nalu_type (VAEncPictureParameterBufferHEVC *pic_param)
  1111. {
  1112.     if (pic_param->pic_fields.bits.idr_pic_flag)
  1113.       return IDR_WRADL_NUT;
  1114.     else if (pic_param->pic_fields.bits.reference_pic_flag)
  1115.       return SLICE_TRAIL_R_NUT;
  1116.     else
  1117.       return SLICE_TRAIL_N_NUT;
  1118. }
  1119.  
  1120. int build_hevc_slice_header(VAEncSequenceParameterBufferHEVC *seq_param,
  1121.                        VAEncPictureParameterBufferHEVC *pic_param,
  1122.                        VAEncSliceParameterBufferHEVC *slice_param,
  1123.                        unsigned char **header_buffer,
  1124.                        int slice_index)
  1125. {
  1126.     avc_bitstream bs;
  1127.  
  1128.     avc_bitstream_start(&bs);
  1129.     nal_start_code_prefix(&bs);
  1130.     nal_header_hevc(&bs, get_hevc_slice_nalu_type(pic_param), 0);
  1131.     slice_rbsp(&bs, slice_index, seq_param,pic_param,slice_param);
  1132.     avc_bitstream_end(&bs);
  1133.  
  1134.     *header_buffer = (unsigned char *)bs.buffer;
  1135.     return bs.bit_offset;
  1136. }
  1137.