xref: /OK3568_Linux_fs/external/mpp/mpp/codec/enc/h265/h265e_slice.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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 "h265e_slice"
18 
19 #include <string.h>
20 
21 #include "mpp_debug.h"
22 #include "h265e_codec.h"
23 #include "h265e_slice.h"
24 
get_ref_pic(H265eDpbFrm * frame_list,RK_S32 poc)25 H265eDpbFrm* get_ref_pic(H265eDpbFrm *frame_list, RK_S32 poc)
26 {
27     RK_S32 index = 0;
28     H265eDpbFrm *frame = NULL;
29 
30     h265e_dbg_func("enter\n");
31     for (index = 0; index < MAX_REFS; index++) {
32         frame = &frame_list[index];
33         if (frame->inited && frame->poc == poc) {
34             break;
35         }
36     }
37     h265e_dbg_func("leave\n");
38     return frame;
39 }
40 
get_lt_ref_pic(H265eDpbFrm * frame_list,H265eSlice * slice,RK_S32 poc,RK_U32 pocHasMsb)41 H265eDpbFrm* get_lt_ref_pic(H265eDpbFrm *frame_list, H265eSlice *slice, RK_S32 poc, RK_U32 pocHasMsb)
42 {
43     RK_S32 index = 0;
44     H265eDpbFrm *frame = NULL;
45     H265eDpbFrm* stPic = &frame_list[MAX_REFS - 1];
46     RK_S32 pocCycle = 1 << slice->m_sps->m_bitsForPOC;
47 
48     h265e_dbg_func("enter\n");
49     if (!pocHasMsb) {
50         poc = poc % pocCycle;
51     }
52 
53     for (index = MAX_REFS - 1 ; index >= 0; index--) {
54         frame = &frame_list[index];
55         if (frame->on_used && frame->poc != slice->poc && frame->slice->is_referenced) {
56             RK_S32 picPoc = frame->poc;
57             if (!pocHasMsb) {
58                 picPoc = picPoc % pocCycle;
59             }
60 
61             if (poc == picPoc) {
62                 if (frame->is_long_term) {
63                     return frame;
64                 } else {
65                     stPic = frame;
66                 }
67             }
68         }
69 
70     }
71     h265e_dbg_func("leave\n");
72     return stPic;
73 }
74 
h265e_slice_set_ref_list(H265eDpbFrm * frame_list,H265eSlice * slice)75 void h265e_slice_set_ref_list(H265eDpbFrm *frame_list, H265eSlice *slice)
76 {
77     H265eReferencePictureSet *rps = slice->m_rps;
78     H265eDpbFrm* refPic = NULL;
79     H265eDpbFrm* refPicSetStCurr0[MAX_REFS];
80     H265eDpbFrm* refPicSetStCurr1[MAX_REFS];
81     H265eDpbFrm* refPicSetLtCurr[MAX_REFS];
82     RK_S32 numPocStCurr0 = 0;
83     RK_S32 numPocStCurr1 = 0;
84     RK_S32 numPocLtCurr = 0;
85     RK_S32 i, cIdx = 0, rIdx = 0;
86 
87     h265e_dbg_func("enter\n");
88     if (slice->m_sliceType == I_SLICE) {
89         memset(slice->m_refPicList, 0, sizeof(slice->m_refPicList));
90         memset(slice->m_numRefIdx,  0, sizeof(slice->m_numRefIdx));
91         return;
92     }
93     for (i = 0; i < rps->num_negative_pic; i++) {
94         if (rps->m_used[i]) {
95             refPic = get_ref_pic(frame_list, slice->poc + rps->delta_poc[i]);
96             refPic->is_long_term = 0;
97             refPicSetStCurr0[numPocStCurr0] = refPic;
98             numPocStCurr0++;
99             refPic->check_lt_msb = 0;
100         }
101     }
102 
103     for (; i < rps->num_negative_pic + rps->num_positive_pic; i++) {
104         if (rps->m_used[i]) {
105             refPic = get_ref_pic(frame_list,  slice->poc + rps->delta_poc[i]);
106             refPic->is_long_term = 0;
107             refPicSetStCurr1[numPocStCurr1] = refPic;
108             numPocStCurr1++;
109             refPic->check_lt_msb = 0;
110         }
111     }
112 
113     for (i = rps->num_negative_pic + rps->num_positive_pic + rps->num_long_term_pic - 1;
114          i > rps->num_negative_pic + rps->num_positive_pic - 1; i--) {
115         if (rps->m_used[i]) {
116             refPic = get_lt_ref_pic(frame_list, slice, rps->m_RealPoc[i], rps->check_lt_msb[i]);
117             refPic->is_long_term = 1;
118             /*due to only one ref if num_negative_pic > 0 set lt not used*/
119             /* if (rps->num_negative_pic > 0) {
120                  rps->m_used[i] = 0;
121              }*/
122             refPicSetLtCurr[numPocLtCurr] = refPic;
123             numPocLtCurr++;
124         }
125         if (refPic == NULL) {
126             refPic = get_lt_ref_pic(frame_list, slice, rps->m_RealPoc[i], rps->check_lt_msb[i]);
127         }
128         refPic->check_lt_msb = rps->check_lt_msb[i];
129     }
130 
131     // ref_pic_list_init
132     H265eDpbFrm* rpsCurrList0[MAX_REFS + 1];
133     H265eDpbFrm* rpsCurrList1[MAX_REFS + 1];
134     RK_S32 numPocTotalCurr = numPocStCurr0 + numPocStCurr1 + numPocLtCurr;
135 
136     for (i = 0; i < numPocStCurr0; i++, cIdx++) {
137         rpsCurrList0[cIdx] = refPicSetStCurr0[i];
138     }
139 
140     for (i = 0; i < numPocStCurr1; i++, cIdx++) {
141         rpsCurrList0[cIdx] = refPicSetStCurr1[i];
142     }
143 
144     for (i = 0; i < numPocLtCurr; i++, cIdx++) {
145         rpsCurrList0[cIdx] = refPicSetLtCurr[i];
146     }
147 
148     mpp_assert(cIdx == numPocTotalCurr);
149 
150     if (slice->m_sliceType == B_SLICE) {
151         cIdx = 0;
152         for (i = 0; i < numPocStCurr1; i++, cIdx++) {
153             rpsCurrList1[cIdx] = refPicSetStCurr1[i];
154         }
155 
156         for (i = 0; i < numPocStCurr0; i++, cIdx++) {
157             rpsCurrList1[cIdx] = refPicSetStCurr0[i];
158         }
159 
160         for (i = 0; i < numPocLtCurr; i++, cIdx++) {
161             rpsCurrList1[cIdx] = refPicSetLtCurr[i];
162         }
163 
164         mpp_assert(cIdx == numPocTotalCurr);
165     }
166 
167     memset(slice->m_bIsUsedAsLongTerm, 0, sizeof(slice->m_bIsUsedAsLongTerm));
168 
169     for ( rIdx = 0; rIdx < slice->m_numRefIdx[0]; rIdx++) {
170         cIdx = slice->m_RefPicListModification.m_refPicListModificationFlagL0 ? slice->m_RefPicListModification.m_RefPicSetIdxL0[rIdx] : (RK_U32)rIdx % numPocTotalCurr;
171         mpp_assert(cIdx >= 0 && cIdx < numPocTotalCurr);
172         slice->m_refPicList[0][rIdx] = rpsCurrList0[cIdx];
173         slice->m_bIsUsedAsLongTerm[0][rIdx] = (cIdx >= numPocStCurr0 + numPocStCurr1);
174     }
175 
176     if (slice->m_sliceType != B_SLICE) {
177         slice->m_numRefIdx[1] = 0;
178         memset(slice->m_refPicList[1], 0, sizeof(slice->m_refPicList[1]));
179     } else {
180         for (rIdx = 0; rIdx < slice->m_numRefIdx[1]; rIdx++) {
181             cIdx = slice->m_RefPicListModification.m_refPicListModificationFlagL1 ? slice->m_RefPicListModification.m_RefPicSetIdxL1[rIdx] : (RK_U32)rIdx % numPocTotalCurr;
182             mpp_assert(cIdx >= 0 && cIdx < numPocTotalCurr);
183             slice->m_refPicList[1][rIdx] = rpsCurrList1[cIdx];
184             slice->m_bIsUsedAsLongTerm[1][rIdx] = (cIdx >= numPocStCurr0 + numPocStCurr1);
185         }
186     }
187 
188     h265e_dbg_func("leave\n");
189 }
190 
h265e_slice_set_ref_poc_list(H265eSlice * slice)191 void h265e_slice_set_ref_poc_list(H265eSlice *slice)
192 {
193     RK_S32 dir, numRefIdx;
194 
195     h265e_dbg_func("enter\n");
196 
197     for (dir = 0; dir < 2; dir++) {
198         for (numRefIdx = 0; numRefIdx < slice->m_numRefIdx[dir]; numRefIdx++) {
199             slice->m_refPOCList[dir][numRefIdx] = slice->m_refPicList[dir][numRefIdx]->poc;
200         }
201     }
202 
203     h265e_dbg_func("leave\n");
204 }
205 
h265e_slice_init(void * ctx,EncFrmStatus curr)206 void h265e_slice_init(void *ctx, EncFrmStatus curr)
207 {
208     H265eCtx *p = (H265eCtx *)ctx;
209     H265eVps *vps = &p->vps;
210     H265eSps *sps = &p->sps;
211     H265ePps *pps = &p->pps;
212     MppEncCfgSet *cfg = p->cfg;
213     MppEncH265Cfg *codec = &cfg->codec.h265;
214     H265eSlice *slice = p->dpb->curr->slice;
215     p->slice = p->dpb->curr->slice;
216     h265e_dbg_func("enter\n");
217     memset(slice, 0, sizeof(H265eSlice));
218     slice->m_sps = sps;
219     slice->m_vps = vps;
220     slice->m_pps = pps;
221     slice->m_numRefIdx[0] = 0;
222     slice->m_numRefIdx[1] = 0;
223     slice->m_colFromL0Flag = 1;
224     slice->m_colRefIdx = 0;
225     slice->m_bCheckLDC = 0;
226     slice->m_sliceQpDeltaCb = 0;
227     slice->m_sliceQpDeltaCr = 0;
228     slice->m_maxNumMergeCand = 5;
229     slice->m_bFinalized = 0;
230 
231     slice->m_cabacInitFlag = 0;
232     slice->m_numEntryPointOffsets = 0;
233     slice->m_enableTMVPFlag = sps->m_TMVPFlagsPresent;
234     slice->m_picOutputFlag = 1;
235     p->dpb->curr->is_key_frame = 0;
236     if (curr.is_idr) {
237         slice->m_sliceType = I_SLICE;
238         p->dpb->curr->is_key_frame = 1;
239         p->dpb->curr->status.is_intra = 1;
240         p->dpb->gop_idx = 0;
241     } else {
242         slice->m_sliceType = P_SLICE;
243         p->dpb->curr->status.is_intra = 0;
244     }
245 
246     p->dpb->curr->status.val = curr.val;
247 
248     if (slice->m_sliceType  != B_SLICE && !curr.non_recn)
249         slice->is_referenced = 1;
250 
251     if (slice->m_pps->m_deblockingFilterOverrideEnabledFlag) {
252         h265e_dbg_slice("to do in this case");
253     } else {
254         slice->m_deblockingFilterDisable = pps->m_picDisableDeblockingFilterFlag;
255         slice->m_deblockingFilterBetaOffsetDiv2 = pps->m_deblockingFilterBetaOffsetDiv2;
256         slice->m_deblockingFilterTcOffsetDiv2 = pps->m_deblockingFilterTcOffsetDiv2;
257     }
258     slice->m_saoEnabledFlag = !codec->sao_cfg.slice_sao_luma_disable;
259     slice->m_saoEnabledFlagChroma = !codec->sao_cfg.slice_sao_chroma_disable;
260     slice->m_maxNumMergeCand = codec->merge_cfg.max_mrg_cnd;
261     slice->m_cabacInitFlag = codec->entropy_cfg.cabac_init_flag;
262     slice->m_picOutputFlag = 1;
263     slice->m_ppsId = pps->m_PPSId;
264     if (slice->m_pps->m_bSliceChromaQpFlag) {
265         slice->m_sliceQpDeltaCb = codec->trans_cfg.cb_qp_offset;
266         slice->m_sliceQpDeltaCr = codec->trans_cfg.cr_qp_offset;
267     }
268 
269     slice->poc = p->dpb->curr->seq_idx;
270     slice->gop_idx = p->dpb->gop_idx;
271     p->dpb->curr->gop_idx =  p->dpb->gop_idx++;
272     p->dpb->curr->poc = slice->poc;
273     if (curr.is_lt_ref)
274         p->dpb->curr->is_long_term = 1;
275 
276     h265e_dbg_slice("slice->m_sliceType = %d slice->is_referenced = %d \n",
277                     slice->m_sliceType, slice->is_referenced);
278     h265e_dbg_func("leave\n");
279 }
280 
code_st_refpic_set(MppWriteCtx * bitIf,H265eReferencePictureSet * rps,RK_S32 idx)281 void code_st_refpic_set(MppWriteCtx *bitIf, H265eReferencePictureSet* rps, RK_S32 idx)
282 {
283     if (idx > 0) {
284         mpp_writer_put_bits(bitIf, rps->m_interRPSPrediction, 1); // inter_RPS_prediction_flag
285     }
286     if (rps->m_interRPSPrediction) {
287         RK_S32 deltaRPS = rps->m_deltaRPS;
288         RK_S32 j;
289 
290         mpp_writer_put_ue(bitIf, rps->m_deltaRIdxMinus1); // delta index of the Reference Picture Set used for prediction minus 1
291 
292         mpp_writer_put_bits(bitIf, (deltaRPS >= 0 ? 0 : 1), 1); //delta_rps_sign
293         mpp_writer_put_ue(bitIf, abs(deltaRPS) - 1); // absolute delta RPS minus 1
294 
295         for (j = 0; j < rps->m_numRefIdc; j++) {
296             RK_S32 refIdc = rps->m_refIdc[j];
297             mpp_writer_put_bits(bitIf, (refIdc == 1 ? 1 : 0), 1); //first bit is "1" if Idc is 1
298             if (refIdc != 1) {
299                 mpp_writer_put_bits(bitIf, refIdc >> 1, 1); //second bit is "1" if Idc is 2, "0" otherwise.
300             }
301         }
302     } else {
303         mpp_writer_put_ue(bitIf, rps->num_negative_pic);
304         mpp_writer_put_ue(bitIf, rps->num_positive_pic);
305         RK_S32 prev = 0;
306         RK_S32 j;
307 
308         for (j = 0; j < rps->num_negative_pic; j++) {
309             mpp_writer_put_ue(bitIf, prev - rps->delta_poc[j] - 1);
310             prev = rps->delta_poc[j];
311             mpp_writer_put_bits(bitIf, rps->m_used[j], 1);
312         }
313 
314         prev = 0;
315         for (j = rps->num_negative_pic; j < rps->num_negative_pic + rps->num_positive_pic; j++) {
316             mpp_writer_put_ue(bitIf, rps->delta_poc[j] - prev - 1);
317             prev = rps->delta_poc[j];
318             mpp_writer_put_bits(bitIf, rps->m_used[j], 1);
319         }
320     }
321 }
322 
find_matching_ltrp(H265eSlice * slice,RK_U32 * ltrpsIndex,RK_S32 ltrpPOC,RK_U32 usedFlag)323 RK_U8 find_matching_ltrp(H265eSlice* slice, RK_U32 *ltrpsIndex, RK_S32 ltrpPOC, RK_U32 usedFlag)
324 {
325     RK_U32 lsb = ltrpPOC % (1 << slice->m_sps->m_bitsForPOC);
326     RK_U32 k;
327     for (k = 0; k < slice->m_sps->m_numLongTermRefPicSPS; k++) {
328         if ((lsb == slice->m_sps->m_ltRefPicPocLsbSps[k]) && (usedFlag == slice->m_sps->m_usedByCurrPicLtSPSFlag[k])) {
329             *ltrpsIndex = k;
330             return 1;
331         }
332     }
333 
334     return 0;
335 }
336 
get_num_rps_cur_templist(H265eReferencePictureSet * rps)337 RK_S32 get_num_rps_cur_templist(H265eReferencePictureSet* rps)
338 {
339     RK_S32 numRpsCurrTempList = 0;
340     RK_S32 i;
341     for ( i = 0; i < rps->num_negative_pic + rps->num_positive_pic + rps->num_long_term_pic; i++) {
342         if (rps->m_used[i]) {
343             numRpsCurrTempList++;
344         }
345     }
346 
347     return numRpsCurrTempList;
348 }
349 
350 
h265e_code_slice_header(H265eSlice * slice,MppWriteCtx * bitIf)351 void h265e_code_slice_header(H265eSlice *slice, MppWriteCtx *bitIf)
352 {
353     RK_U32 i = 0;
354     mpp_writer_put_bits(bitIf, 1, 1); //first_slice_segment_in_pic_flag
355     mpp_writer_put_ue(bitIf, slice->m_ppsId);
356     H265eReferencePictureSet* rps = slice->m_rps;
357     slice->m_enableTMVPFlag = 0;
358     if (!slice->m_dependentSliceSegmentFlag) {
359         for (i = 0; i < (RK_U32)slice->m_pps->m_numExtraSliceHeaderBits; i++) {
360             mpp_writer_put_bits(bitIf, (slice->slice_reserved_flag >> i) & 0x1, 1);
361         }
362 
363         mpp_writer_put_ue(bitIf, slice->m_sliceType);
364 
365         if (slice->m_pps->m_outputFlagPresentFlag) {
366             mpp_writer_put_bits(bitIf, slice->m_picOutputFlag ? 1 : 0, 1);
367         }
368 
369         if (slice->m_sliceType != I_SLICE) { // skip frame can't iDR
370             RK_S32 picOrderCntLSB = (slice->poc - slice->last_idr + (1 << slice->m_sps->m_bitsForPOC)) % (1 << slice->m_sps->m_bitsForPOC);
371             mpp_writer_put_bits(bitIf, picOrderCntLSB, slice->m_sps->m_bitsForPOC);
372             if (slice->m_bdIdx < 0) {
373                 mpp_writer_put_bits(bitIf, 0, 1);
374                 code_st_refpic_set(bitIf, rps, slice->m_sps->m_RPSList.m_numberOfReferencePictureSets);
375             } else {
376                 mpp_writer_put_bits(bitIf, 1, 1);
377                 RK_S32 numBits = 0;
378                 while ((1 << numBits) < slice->m_sps->m_RPSList.m_numberOfReferencePictureSets) {
379                     numBits++;
380                 }
381 
382                 if (numBits > 0) {
383                     mpp_writer_put_bits(bitIf, slice->m_bdIdx, numBits);
384                 }
385             }
386             if (slice->m_sps->m_bLongTermRefsPresent) {
387 
388                 RK_S32 numLtrpInSH = rps->m_numberOfPictures;
389                 RK_S32 ltrpInSPS[MAX_REFS];
390                 RK_S32 numLtrpInSPS = 0;
391                 RK_U32 ltrpIndex;
392                 RK_S32 counter = 0;
393                 RK_S32 k;
394                 for (k = rps->m_numberOfPictures - 1; k > rps->m_numberOfPictures - rps->num_long_term_pic - 1; k--) {
395                     if (find_matching_ltrp(slice, &ltrpIndex, rps->poc[k], rps->m_used[k])) {
396                         ltrpInSPS[numLtrpInSPS] = ltrpIndex;
397                         numLtrpInSPS++;
398                     } else {
399                         counter++;
400                     }
401                 }
402 
403                 numLtrpInSH -= numLtrpInSPS;
404 
405                 RK_S32 bitsForLtrpInSPS = 0;
406                 while (slice->m_sps->m_numLongTermRefPicSPS > (RK_U32)(1 << bitsForLtrpInSPS)) {
407                     bitsForLtrpInSPS++;
408                 }
409 
410                 if (slice->m_sps->m_numLongTermRefPicSPS > 0) {
411                     mpp_writer_put_ue(bitIf, numLtrpInSPS);
412                 }
413                 mpp_writer_put_ue(bitIf, numLtrpInSH);
414                 // Note that the LSBs of the LT ref. pic. POCs must be sorted before.
415                 // Not sorted here because LT ref indices will be used in setRefPicList()
416                 RK_S32 prevDeltaMSB = 0;
417                 RK_S32 offset = rps->num_negative_pic + rps->num_positive_pic;
418                 for ( k = rps->m_numberOfPictures - 1; k > offset - 1; k--) {
419                     if (counter < numLtrpInSPS) {
420                         if (bitsForLtrpInSPS > 0) {
421                             mpp_writer_put_bits(bitIf, ltrpInSPS[counter], bitsForLtrpInSPS);
422                         }
423                     } else {
424                         mpp_writer_put_bits(bitIf, rps->m_pocLSBLT[k], slice->m_sps->m_bitsForPOC);
425                         mpp_writer_put_bits(bitIf, rps->m_used[k], 1);
426                     }
427                     mpp_writer_put_bits(bitIf, rps->m_deltaPocMSBPresentFlag[k], 1);
428 
429                     if (rps->m_deltaPocMSBPresentFlag[k]) {
430                         RK_U32 deltaFlag = 0;
431                         if ((k == rps->m_numberOfPictures - 1) || (k == rps->m_numberOfPictures - 1 - numLtrpInSPS)) {
432                             deltaFlag = 1;
433                         }
434                         if (deltaFlag) {
435                             mpp_writer_put_ue(bitIf, rps->m_deltaPOCMSBCycleLT[k]);
436                         } else {
437                             RK_S32 differenceInDeltaMSB = rps->m_deltaPOCMSBCycleLT[k] - prevDeltaMSB;
438                             mpp_writer_put_ue(bitIf, differenceInDeltaMSB);
439                         }
440                         prevDeltaMSB = rps->m_deltaPOCMSBCycleLT[k];
441                     }
442                 }
443             }
444             if (slice->m_sps->m_TMVPFlagsPresent) {
445                 mpp_writer_put_bits(bitIf, slice->m_enableTMVPFlag ? 1 : 0, 1);
446             }
447         }
448 
449         if (slice->m_sps->m_bUseSAO) { //skip frame close sao
450             mpp_writer_put_bits(bitIf, 0, 1);
451             mpp_writer_put_bits(bitIf, 0, 1);
452 
453         }
454         //check if numrefidxes match the defaults. If not, override
455 
456         if (slice->m_sliceType != I_SLICE) {
457             RK_U32 overrideFlag = (slice->m_numRefIdx[0] != (RK_S32)slice->m_pps->m_numRefIdxL0DefaultActive);
458             mpp_writer_put_bits(bitIf, overrideFlag ? 1 : 0, 1);
459             if (overrideFlag) {
460                 mpp_writer_put_ue(bitIf, slice->m_numRefIdx[0] - 1);
461                 slice->m_numRefIdx[1] = 0;
462             }
463         }
464 
465         if (slice->m_pps->m_listsModificationPresentFlag && get_num_rps_cur_templist(rps) > 1) {
466             H265eRefPicListModification* refPicListModification = &slice->m_RefPicListModification;
467             mpp_writer_put_bits(bitIf, refPicListModification->m_refPicListModificationFlagL0 ? 1 : 0, 1);
468             if (refPicListModification->m_refPicListModificationFlagL0) {
469                 RK_S32 numRpsCurrTempList0 = get_num_rps_cur_templist(rps);
470                 if (numRpsCurrTempList0 > 1) {
471                     RK_S32 length = 1;
472                     numRpsCurrTempList0--;
473                     while (numRpsCurrTempList0 >>= 1) {
474                         length++;
475                     }
476                     for (i = 0; i < (RK_U32)slice->m_numRefIdx[0]; i++) {
477                         mpp_writer_put_bits(bitIf, refPicListModification->m_RefPicSetIdxL0[i], length);
478                     }
479                 }
480             }
481         }
482 
483         if (slice->m_pps->m_cabacInitPresentFlag) {
484             mpp_writer_put_bits(bitIf, slice->m_cabacInitFlag, 1);
485         }
486 
487         if (slice->m_enableTMVPFlag) {
488 
489             if (slice->m_sliceType != I_SLICE &&
490                 ((slice->m_colFromL0Flag == 1 && slice->m_numRefIdx[0] > 1) ||
491                  (slice->m_colFromL0Flag == 0 && slice->m_numRefIdx[1] > 1))) {
492                 mpp_writer_put_ue(bitIf, slice->m_colRefIdx);
493             }
494         }
495 
496         if (slice->m_sliceType != I_SLICE) {
497             RK_S32 flag = MRG_MAX_NUM_CANDS - slice->m_maxNumMergeCand;
498             flag = flag == 5 ? 4 : flag;
499             mpp_writer_put_ue(bitIf, flag);
500         }
501         RK_S32 code = slice->m_sliceQp - (slice->m_pps->m_picInitQPMinus26 + 26);
502         mpp_writer_put_se(bitIf, code);
503         if (slice->m_pps->m_bSliceChromaQpFlag) {
504             code = slice->m_sliceQpDeltaCb;
505             mpp_writer_put_se(bitIf, code);
506             code = slice->m_sliceQpDeltaCr;
507             mpp_writer_put_se(bitIf, code);
508         }
509         if (slice->m_pps->m_deblockingFilterControlPresentFlag) {
510             if (slice->m_pps->m_deblockingFilterOverrideEnabledFlag) {
511                 mpp_writer_put_bits(bitIf, slice->m_deblockingFilterOverrideFlag, 1);
512             }
513             if (slice->m_deblockingFilterOverrideFlag) {
514                 mpp_writer_put_bits(bitIf, slice->m_deblockingFilterDisable, 1);
515                 if (!slice->m_deblockingFilterDisable) {
516                     mpp_writer_put_se(bitIf, slice->m_deblockingFilterBetaOffsetDiv2);
517                     mpp_writer_put_se(bitIf, slice->m_deblockingFilterTcOffsetDiv2);
518                 }
519             }
520         }
521     }
522     if (slice->m_pps->m_sliceHeaderExtensionPresentFlag) {
523         mpp_writer_put_ue(bitIf, slice->slice_header_extension_length);
524         for (i = 0; i < slice->slice_header_extension_length; i++) {
525             mpp_writer_put_bits(bitIf, 0, 8);
526         }
527     }
528     h265e_dbg_func("leave\n");
529 }
530 
code_skip_flag(H265eSlice * slice,RK_U32 abs_part_idx,DataCu * cu)531 void code_skip_flag(H265eSlice *slice, RK_U32 abs_part_idx, DataCu *cu)
532 {
533     // get context function is here
534     H265eCabacCtx *cabac_ctx = &slice->m_cabac;
535     H265eSps *sps = slice->m_sps;
536     RK_U32  ctxSkip;
537     RK_U32 tpelx = cu->pixelX + sps->raster2pelx[sps->zscan2raster[abs_part_idx]];
538     RK_U32 tpely = cu->pixelY + sps->raster2pely[sps->zscan2raster[abs_part_idx]];
539     //RK_U32 ctxSkip = cu->getCtxSkipFlag(abs_part_idx);
540 
541     h265e_dbg_skip("tpelx = %d", tpelx);
542     if (cu->cur_addr == 0 ) {
543         ctxSkip = 0;
544     } else if ((tpely == 0) || (tpelx == 0)) {
545         ctxSkip = 1;
546     } else {
547         ctxSkip = 2;
548     }
549     h265e_dbg_skip("ctxSkip = %d", ctxSkip);
550     h265e_cabac_encodeBin(cabac_ctx, &slice->m_contextModels[OFF_SKIP_FLAG_CTX + ctxSkip], 1);
551 }
552 
code_merge_index(H265eSlice * slice)553 static void code_merge_index(H265eSlice *slice)
554 {
555     H265eCabacCtx *cabac_ctx = &slice->m_cabac;
556 
557     h265e_cabac_encodeBin(cabac_ctx, &slice->m_contextModels[OFF_MERGE_IDX_EXT_CTX], 0);
558 }
559 
code_split_flag(H265eSlice * slice,RK_U32 abs_part_idx,RK_U32 depth,DataCu * cu)560 static void code_split_flag(H265eSlice *slice, RK_U32 abs_part_idx, RK_U32 depth, DataCu *cu)
561 {
562     H265eSps *sps = slice->m_sps;
563 
564     if (depth == slice->m_sps->m_maxCUDepth - slice->m_sps->m_addCUDepth)
565         return;
566 
567     h265e_dbg_skip("depth %d cu->m_cuDepth %d", depth, cu->m_cuDepth[sps->zscan2raster[abs_part_idx]]);
568 
569     H265eCabacCtx *cabac_ctx = &slice->m_cabac;
570     RK_U32 currSplitFlag = (cu->m_cuDepth[sps->zscan2raster[abs_part_idx]] > depth) ? 1 : 0;
571 
572     h265e_cabac_encodeBin(cabac_ctx, &slice->m_contextModels[OFF_SPLIT_FLAG_CTX], currSplitFlag);
573 }
574 
encode_cu(H265eSlice * slice,RK_U32 abs_part_idx,RK_U32 depth,DataCu * cu)575 static void encode_cu(H265eSlice *slice, RK_U32 abs_part_idx, RK_U32 depth, DataCu *cu)
576 {
577     H265eSps *sps = slice->m_sps;
578     RK_U32 bBoundary = 0;
579     RK_U32 lpelx = cu->pixelX + sps->raster2pelx[sps->zscan2raster[abs_part_idx]];
580     RK_U32 rpelx = lpelx + (sps->m_maxCUSize >> depth) - 1;
581     RK_U32 tpely = cu->pixelY + sps->raster2pely[sps->zscan2raster[abs_part_idx]];
582     RK_U32 bpely = tpely + (sps->m_maxCUSize >> depth) - 1;
583 
584     h265e_dbg_skip("EncodeCU depth %d, abs_part_idx %d", depth, abs_part_idx);
585 
586     if ((rpelx < sps->m_picWidthInLumaSamples) && (bpely < sps->m_picHeightInLumaSamples)) {
587 
588         h265e_dbg_skip("code_split_flag in depth %d", depth);
589         code_split_flag(slice, abs_part_idx, depth, cu);
590     } else {
591         h265e_dbg_skip("boundary flag found");
592         bBoundary = 1;
593     }
594 
595     h265e_dbg_skip("m_cuDepth[%d] = %d maxCUDepth %d, m_addCUDepth %d", abs_part_idx, cu->m_cuDepth[sps->zscan2raster[abs_part_idx]], sps->m_maxCUDepth, sps->m_addCUDepth);
596 
597     if ((depth < cu->m_cuDepth[sps->zscan2raster[abs_part_idx]] && (depth < (sps->m_maxCUDepth - sps->m_addCUDepth))) || bBoundary) {
598         RK_U32 qNumParts = (256 >> (depth << 1)) >> 2;
599         RK_U32 partUnitIdx = 0;
600 
601         for (partUnitIdx = 0; partUnitIdx < 4; partUnitIdx++, abs_part_idx += qNumParts) {
602             h265e_dbg_skip("depth %d partUnitIdx = %d, qNumParts %d, abs_part_idx %d", depth, partUnitIdx, qNumParts, abs_part_idx);
603             lpelx = cu->pixelX + sps->raster2pelx[sps->zscan2raster[abs_part_idx]];
604             tpely = cu->pixelY + sps->raster2pely[sps->zscan2raster[abs_part_idx]];
605             if ((lpelx < sps->m_picWidthInLumaSamples) && (tpely < sps->m_picHeightInLumaSamples)) {
606                 encode_cu(slice, abs_part_idx, depth + 1, cu);
607             }
608         }
609         return;
610     }
611 
612     h265e_dbg_skip("code_skip_flag in depth %d", depth);
613     code_skip_flag(slice, abs_part_idx, cu);
614     h265e_dbg_skip("code_merge_index in depth %d", depth);
615     code_merge_index(slice);
616     return;
617 }
618 
proc_cu8(DataCu * cu,RK_U32 pos_x,RK_U32 pos_y)619 static void proc_cu8(DataCu *cu, RK_U32 pos_x, RK_U32 pos_y)
620 {
621     RK_S32 nSize = 8;
622     RK_S32 nSubPart = nSize * nSize / 4 / 4;
623     RK_S32 puIdx = pos_x / 8 + pos_y / 8 * 8;
624 
625     h265e_dbg_skip("8 ctu puIdx %d no need split", puIdx);
626 
627     memset(cu->m_cuDepth + puIdx * nSubPart, 3, nSubPart);
628 }
629 
proc_cu16(H265eSlice * slice,DataCu * cu,RK_U32 pos_x,RK_U32 pos_y)630 static void proc_cu16(H265eSlice *slice, DataCu *cu, RK_U32 pos_x, RK_U32 pos_y)
631 {
632     RK_U32 m;
633     H265eSps *sps = slice->m_sps;
634     RK_S32 nSize = 16;
635     RK_S32 nSubPart = nSize * nSize / 4 / 4;
636     RK_S32 puIdx = pos_x / 16 + pos_y / 16 * 4;
637     RK_U32 cu_x_1, cu_y_1;
638 
639     h265e_dbg_skip("cu 16 pos_x %d pos_y %d", pos_x, pos_y);
640 
641     if ((cu->pixelX + pos_x + 15 < sps->m_picWidthInLumaSamples) &&
642         (cu->pixelY + pos_y + 15 < sps->m_picHeightInLumaSamples)) {
643         h265e_dbg_skip("16 ctu puIdx %d no need split", puIdx);
644         memset(cu->m_cuDepth + puIdx * nSubPart, 2, nSubPart);
645         return;
646     } else if ((cu->pixelX + pos_x >=  sps->m_picWidthInLumaSamples) ||
647                (cu->pixelY + pos_y  >= sps->m_picHeightInLumaSamples)) {
648         h265e_dbg_skip("16 ctu puIdx %d out of pic", puIdx);
649         memset(cu->m_cuDepth + puIdx * nSubPart, 2, nSubPart);
650         return;
651     }
652 
653     for (m = 0; m < 4; m ++) {
654         cu_x_1 = pos_x + (m & 1) * (nSize >> 1);
655         cu_y_1 = pos_y + (m >> 1) * (nSize >> 1);
656 
657         proc_cu8(cu, cu_x_1, cu_y_1);
658     }
659 }
660 
661 
proc_cu32(H265eSlice * slice,DataCu * cu,RK_U32 pos_x,RK_U32 pos_y)662 static void proc_cu32(H265eSlice *slice, DataCu *cu, RK_U32 pos_x, RK_U32 pos_y)
663 {
664     RK_U32 m;
665     H265eSps *sps = slice->m_sps;
666     RK_S32 nSize = 32;
667     RK_S32 nSubPart = nSize * nSize / 4 / 4;
668     RK_S32 puIdx = pos_x / 32 + pos_y / 32 * 2;
669     RK_U32 cu_x_1, cu_y_1;
670 
671     h265e_dbg_skip("cu 32 pos_x %d pos_y %d", pos_x, pos_y);
672 
673     if ((cu->pixelX + pos_x + 31 < sps->m_picWidthInLumaSamples) &&
674         (cu->pixelY + pos_y + 31 < sps->m_picHeightInLumaSamples)) {
675         h265e_dbg_skip("32 ctu puIdx %d no need split", puIdx);
676         memset(cu->m_cuDepth + puIdx * nSubPart, 1, nSubPart);
677         return;
678     } else if ((cu->pixelX + pos_x >=  sps->m_picWidthInLumaSamples) ||
679                (cu->pixelY + pos_y  >= sps->m_picHeightInLumaSamples)) {
680         h265e_dbg_skip("32 ctu puIdx %d out of pic", puIdx);
681         memset(cu->m_cuDepth + puIdx * nSubPart, 1, nSubPart);
682         return;
683     }
684 
685     for (m = 0; m < 4; m ++) {
686         cu_x_1 = pos_x + (m & 1) * (nSize >> 1);
687         cu_y_1 = pos_y + (m >> 1) * (nSize >> 1);
688 
689         proc_cu16(slice, cu, cu_x_1, cu_y_1);
690     }
691 }
692 
proc_ctu(H265eSlice * slice,DataCu * cu)693 static void proc_ctu(H265eSlice *slice, DataCu *cu)
694 {
695     H265eSps *sps = slice->m_sps;
696     RK_U32 k, m;
697     RK_U32 cu_x_1, cu_y_1, m_nCtuSize = 64;
698     RK_U32 lpelx = cu->pixelX;
699     RK_U32 rpelx = lpelx + 63;
700     RK_U32 tpely = cu->pixelY;
701     RK_U32 bpely = tpely + 63;
702 
703     for (k = 0; k < 256; k++) {
704         cu->m_cuDepth[k] = 0;
705         cu->m_cuSize[k] = 64;
706     }
707     if ((rpelx < sps->m_picWidthInLumaSamples) && (bpely < sps->m_picHeightInLumaSamples))
708         return;
709 
710     for (m = 0; m < 4; m ++) {
711         cu_x_1 = (m & 1) * (m_nCtuSize >> 1);
712         cu_y_1 = (m >> 1) * (m_nCtuSize >> 1);
713         proc_cu32(slice, cu, cu_x_1, cu_y_1);
714     }
715 
716     for (k = 0; k < 256; k++) {
717         switch (cu->m_cuDepth[k]) {
718         case 0: cu->m_cuSize[k] = 64; break;
719         case 1: cu->m_cuSize[k] = 32; break;
720         case 2: cu->m_cuSize[k] = 16; break;
721         case 3: cu->m_cuSize[k] = 8;  break;
722         }
723     }
724 }
725 
h265e_write_nal(MppWriteCtx * bitIf)726 static void h265e_write_nal(MppWriteCtx *bitIf)
727 {
728     h265e_dbg_func("enter\n");
729 
730     mpp_writer_put_raw_bits(bitIf, 0x0, 24);
731     mpp_writer_put_raw_bits(bitIf, 0x01, 8);
732     mpp_writer_put_bits(bitIf, 0, 1);   // forbidden_zero_bit
733     mpp_writer_put_bits(bitIf, 1, 6);   // nal_unit_type
734     mpp_writer_put_bits(bitIf, 0, 6);   // nuh_reserved_zero_6bits
735     mpp_writer_put_bits(bitIf, 1, 3);   // nuh_temporal_id_plus1
736 
737     h265e_dbg_func("leave\n");
738 }
739 
h265e_write_algin(MppWriteCtx * bitIf)740 static void h265e_write_algin(MppWriteCtx *bitIf)
741 {
742     h265e_dbg_func("enter\n");
743     mpp_writer_put_bits(bitIf, 1, 1);
744     mpp_writer_align_zero(bitIf);
745     h265e_dbg_func("leave\n");
746 }
747 
748 
h265e_code_slice_skip_frame(void * ctx,H265eSlice * slice,RK_U8 * buf,RK_S32 len)749 RK_S32 h265e_code_slice_skip_frame(void *ctx, H265eSlice *slice, RK_U8 *buf, RK_S32 len)
750 {
751 
752     MppWriteCtx bitIf;
753     H265eCtx *p = (H265eCtx *)ctx;
754     H265eSps *sps = &p->sps;
755     H265eCabacCtx *cabac_ctx = &slice->m_cabac;
756     h265e_dbg_func("enter\n");
757     RK_U32 mb_wd = ((sps->m_picWidthInLumaSamples + 63) >> 6);
758     RK_U32 mb_h = ((sps->m_picHeightInLumaSamples + 63) >> 6);
759     RK_U32 i = 0, j = 0, cu_cnt = 0;
760     if (!buf || !len) {
761         mpp_err("buf or size no set");
762         return MPP_NOK;
763     }
764     mpp_writer_init(&bitIf, buf, len);
765     h265e_write_nal(&bitIf);
766     h265e_code_slice_header(slice, &bitIf);
767     h265e_write_algin(&bitIf);
768     h265e_reset_enctropy((void*)slice);
769     h265e_cabac_init(cabac_ctx, &bitIf);
770     DataCu cu;
771     cu.mb_w = mb_wd;
772     cu.mb_h = mb_h;
773     slice->is_referenced = 0;
774     for (i = 0; i < mb_h; i++) {
775         for ( j = 0; j < mb_wd; j++) {
776             cu.pixelX = j * 64;
777             cu.pixelY = i * 64;
778             cu.cur_addr = cu_cnt;
779             proc_ctu(slice, &cu);
780             encode_cu(slice, 0, 0, &cu);
781             h265e_cabac_encodeBinTrm(cabac_ctx, 0);
782             cu_cnt++;
783         }
784     }
785 
786     h265e_cabac_finish(cabac_ctx);
787     h265e_write_algin(&bitIf);
788     h265e_dbg_func("leave\n");
789     return mpp_writer_bytes(&bitIf);
790 }
791 
792 
793