xref: /rockchip-linux_mpp/mpp/codec/enc/h264/h264e_pps.c (revision 437bfbeb9567cca9cd9080e3f6954aa9d6a94f18)
1 /*
2  * Copyright 2015 Rockchip Electronics Co. LTD
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 #define MODULE_TAG "h264e_sps"
18 
19 #include "mpp_common.h"
20 
21 #include "mpp_bitwrite.h"
22 #include "h264e_debug.h"
23 #include "h264e_pps.h"
24 
25 static const uint8_t zigzag[64] = {
26     0,  8,  1,  2,  9, 16, 24, 17, 10,  3,  4, 11, 18, 25, 32, 40,
27     33, 26, 19, 12,  5,  6, 13, 20, 27, 34, 41, 48, 56, 49, 42, 35,
28     28, 21, 14,  7, 15, 22, 29, 36, 43, 50, 57, 58, 51, 44, 37, 30,
29     23, 31, 38, 45, 52, 59, 60, 53, 46, 39, 47, 54, 61, 62, 55, 63
30 };
31 
32 static const uint8_t intra_scl[64] = {
33     10, 11, 14, 16, 17, 19, 21, 23,
34     11, 12, 16, 17, 19, 21, 23, 25,
35     14, 16, 17, 19, 21, 23, 25, 27,
36     16, 17, 19, 21, 23, 25, 27, 28,
37     17, 19, 21, 23, 25, 27, 28, 29,
38     19, 21, 23, 25, 27, 28, 29, 30,
39     21, 23, 25, 27, 28, 29, 30, 31,
40     23, 25, 27, 28, 29, 30, 31, 32,
41 };
42 
43 static const uint8_t inter_scl[64] = {
44     12, 13, 15, 16, 17, 19, 20, 21,
45     13, 14, 16, 17, 19, 20, 21, 22,
46     15, 16, 17, 19, 20, 21, 22, 23,
47     16, 17, 19, 20, 21, 22, 23, 25,
48     17, 19, 20, 21, 22, 23, 25, 27,
49     19, 20, 21, 22, 23, 25, 27, 28,
50     20, 21, 22, 23, 25, 27, 28, 29,
51     21, 22, 23, 25, 27, 28, 29, 30,
52 };
53 
h264e_pps_update(H264ePps * pps,MppEncCfgSet * cfg)54 MPP_RET h264e_pps_update(H264ePps *pps, MppEncCfgSet *cfg)
55 {
56     MppEncH264Cfg *codec = &cfg->h264;
57 
58     pps->pps_id = 0;
59     pps->sps_id = 0;
60 
61     pps->entropy_coding_mode = codec->entropy_coding_mode;
62     pps->bottom_field_pic_order_in_frame_present = 0;
63     pps->num_slice_groups = 1;
64 
65     pps->num_ref_idx_l0_default_active = 1;
66     pps->num_ref_idx_l1_default_active = 1;
67 
68     pps->weighted_pred = 0;
69     pps->weighted_bipred_idc = 0;
70 
71     pps->pic_init_qp = 26;
72     pps->pic_init_qs = pps->pic_init_qp;
73 
74     pps->chroma_qp_index_offset = codec->chroma_cb_qp_offset;
75     pps->second_chroma_qp_index_offset = codec->chroma_cb_qp_offset;
76     pps->deblocking_filter_control = 1;
77     pps->constrained_intra_pred = codec->constrained_intra_pred_mode;
78     pps->redundant_pic_cnt = 0;
79 
80     // if (more_rbsp_data())
81     pps->transform_8x8_mode = codec->transform8x8_mode;
82     mpp_assert(codec->scaling_list_mode == 0 || codec->scaling_list_mode == 1);
83     pps->pic_scaling_matrix_present = codec->scaling_list_mode;
84     if (codec->scaling_list_mode) {
85         /* NOTE: H.264 current encoder do NOT split detail matrix case */
86         pps->use_default_scaling_matrix[H264_INTRA_4x4_Y] = 1;
87         pps->use_default_scaling_matrix[H264_INTRA_4x4_U] = 1;
88         pps->use_default_scaling_matrix[H264_INTRA_4x4_V] = 1;
89         pps->use_default_scaling_matrix[H264_INTER_4x4_Y] = 1;
90         pps->use_default_scaling_matrix[H264_INTER_4x4_U] = 1;
91         pps->use_default_scaling_matrix[H264_INTER_4x4_V] = 1;
92         pps->use_default_scaling_matrix[H264_INTRA_8x8_Y] = 1;
93         pps->use_default_scaling_matrix[H264_INTER_8x8_Y] = 1;
94     }
95 
96     if (codec->profile < H264_PROFILE_HIGH) {
97         pps->second_chroma_qp_index_offset_present = 0;
98         if (pps->transform_8x8_mode) {
99             pps->transform_8x8_mode = 0;
100             mpp_log_f("warning: for profile %d transform_8x8_mode should be 0\n",
101                       codec->profile);
102         }
103         if (pps->pic_scaling_matrix_present) {
104             pps->pic_scaling_matrix_present = 0;
105             mpp_log_f("warning: for profile %d pic_scaling_matrix_present should be 0\n",
106                       codec->profile);
107         }
108     } else {
109         pps->second_chroma_qp_index_offset_present = 1;
110         pps->second_chroma_qp_index_offset = codec->chroma_cr_qp_offset;
111     }
112 
113     if (codec->profile == H264_PROFILE_BASELINE && pps->entropy_coding_mode) {
114         mpp_log_f("warning: for baseline profile entropy_coding_mode should be 0\n");
115         pps->entropy_coding_mode = 0;
116     }
117 
118     return MPP_OK;
119 }
120 
h264e_pps_to_packet(H264ePps * pps,MppPacket packet,RK_S32 * offset,RK_S32 * len)121 RK_S32 h264e_pps_to_packet(H264ePps *pps, MppPacket packet, RK_S32 *offset, RK_S32 *len)
122 {
123     void *pos = mpp_packet_get_pos(packet);
124     void *data = mpp_packet_get_data(packet);
125     size_t size = mpp_packet_get_size(packet);
126     size_t length = mpp_packet_get_length(packet);
127     void *p = pos + length;
128     RK_S32 buf_size = (data + size) - (pos + length);
129     MppWriteCtx bit_ctx;
130     MppWriteCtx *bit = &bit_ctx;
131     RK_S32 pps_size = 0;
132 
133     mpp_writer_init(bit, p, buf_size);
134 
135     /* start_code_prefix 00 00 00 01 */
136     mpp_writer_put_raw_bits(bit, 0, 24);
137     mpp_writer_put_raw_bits(bit, 1, 8);
138     /* forbidden_zero_bit */
139     mpp_writer_put_raw_bits(bit, 0, 1);
140     /* nal_ref_idc */
141     mpp_writer_put_raw_bits(bit, H264_NALU_PRIORITY_HIGHEST, 2);
142     /* nal_unit_type */
143     mpp_writer_put_raw_bits(bit, H264_NALU_TYPE_PPS, 5);
144 
145     /* pic_parameter_set_id */
146     mpp_writer_put_ue(bit, pps->pps_id);
147     /* seq_parameter_set_id */
148     mpp_writer_put_ue(bit, pps->sps_id);
149     /* entropy_coding_mode_flag */
150     mpp_writer_put_bits(bit, pps->entropy_coding_mode, 1);
151     /* bottom_field_pic_order_in_frame_present */
152     mpp_writer_put_bits(bit, pps->bottom_field_pic_order_in_frame_present, 1);
153     /* num_slice_groups_minus1 */
154     mpp_writer_put_ue(bit, pps->num_slice_groups - 1);
155     /* num_ref_idx_l0_active_minus1 */
156     mpp_writer_put_ue(bit, pps->num_ref_idx_l0_default_active - 1);
157     /* num_ref_idx_l1_active_minus1 */
158     mpp_writer_put_ue(bit, pps->num_ref_idx_l1_default_active - 1);
159     /* weighted_pred_flag */
160     mpp_writer_put_bits(bit, pps->weighted_pred, 1);
161     /* weighted_bipred_idc */
162     mpp_writer_put_bits(bit, pps->weighted_bipred_idc, 2);
163     /* pic_init_qp_minus26 */
164     mpp_writer_put_se(bit, pps->pic_init_qp - 26);
165     /* pic_init_qs_minus26 */
166     mpp_writer_put_se(bit, pps->pic_init_qs - 26);
167     /* chroma_qp_index_offset */
168     mpp_writer_put_se(bit, pps->chroma_qp_index_offset);
169     /* deblocking_filter_control_present_flag */
170     mpp_writer_put_bits(bit, pps->deblocking_filter_control, 1);
171     /* constrained_intra_pred_flag */
172     mpp_writer_put_bits(bit, pps->constrained_intra_pred, 1);
173     /* redundant_pic_cnt_present_flag */
174     mpp_writer_put_bits(bit, pps->redundant_pic_cnt, 1);
175 
176     if (pps->transform_8x8_mode ||
177         pps->second_chroma_qp_index_offset_present ||
178         pps->pic_scaling_matrix_present) {
179         /* transform_8x8_mode_flag */
180         mpp_writer_put_bits(bit, pps->transform_8x8_mode, 1);
181 
182         /* TODO: scaling_list_mode */
183         mpp_writer_put_bits(bit, pps->pic_scaling_matrix_present != 0, 1);
184         if (pps->pic_scaling_matrix_present)
185             mpp_writer_put_bits(bit, 0, 6);
186 
187         if (1 == pps->pic_scaling_matrix_present)
188             mpp_writer_put_bits(bit, 0, 2); /* default scaling list */
189         else if (2 == pps->pic_scaling_matrix_present) {
190             /* user defined scaling list */
191             if (pps->transform_8x8_mode) {
192                 RK_S32 run = 0;
193                 RK_S32 len2 = 64;
194                 RK_S32 j = 0;
195 
196                 mpp_writer_put_bits(bit, 1, 1);
197                 for (run = len2; run > 1; run --)
198                     if (intra_scl[zigzag[run - 1]] != intra_scl[zigzag[run - 2]])
199                         break;
200                 for (j = 0; j < run; j ++)
201                     mpp_writer_put_se(bit, (int8_t)(intra_scl[zigzag[j]] - (j > 0 ? intra_scl[zigzag[j - 1]] : 8)));
202                 if (run < len2)
203                     mpp_writer_put_se(bit, (int8_t) - intra_scl[zigzag[run]]);
204 
205                 mpp_writer_put_bits(bit, 1, 1);
206                 for (run = len2; run > 1; run --)
207                     if (inter_scl[zigzag[run - 1]] != inter_scl[zigzag[run - 2]])
208                         break;
209                 for (j = 0; j < run; j ++)
210                     mpp_writer_put_se(bit, (int8_t)(inter_scl[zigzag[j]] - (j > 0 ? inter_scl[zigzag[j - 1]] : 8)));
211                 if (run < len2)
212                     mpp_writer_put_se(bit, (int8_t) - inter_scl[zigzag[run]]);
213             } else
214                 mpp_writer_put_bits(bit, 0, 2);
215         }
216 
217         /* second_chroma_qp_index_offset */
218         mpp_writer_put_se(bit, pps->second_chroma_qp_index_offset);
219     }
220 
221     mpp_writer_trailing(bit);
222 
223     pps_size = mpp_writer_bytes(bit);
224     if (len)
225         *len = pps_size;
226     if (offset)
227         *offset = length;
228 
229     mpp_packet_set_length(packet, length + pps_size);
230 
231     return pps_size;
232 }
233 
h264e_pps_dump(H264ePps * pps)234 MPP_RET h264e_pps_dump(H264ePps *pps)
235 {
236     (void) pps;
237     return MPP_OK;
238 }
239