shithub: treason

Download patch

ref: dbd994ee852e77583186f0ebc18f0bcf3aae090f
parent: be66f01228b3bc33958f8082aa038b92140461ab
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Thu Feb 11 07:32:25 EST 2021

h264: remove more of treason-specific code, reuse what is already in openh264

--- a/decoder_h264.c
+++ b/decoder_h264.c
@@ -11,6 +11,9 @@
 typedef struct Aux Aux;
 
 struct Aux {
+	SPictReoderingStatus;
+	SPictInfo pics[16];
+
 	SWelsDecoderContext ctx;
 	SLogContext logctx;
 	SBufferInfo info;
@@ -18,15 +21,6 @@
 	SVlcTable vlctbl;
 	SDecoderStatistics stat;
 	uint8_t *data[3];
-
-	/* reordering */
-	SPictInfo pics[16];
-	int npics;
-	int lastgopremain;
-	int lastwritten;
-	int lbufpicind;
-	int minpoc;
-	int picind;
 };
 
 static void
@@ -46,17 +40,17 @@
 {
 	int i, firstvalid;
 
-	if(a->npics > 0){
+	if(a->iNumOfPicts > 0){
 		if(a->ctx.pLastDecPicInfo->pPreviousDecodedPictureInDpb && a->ctx.pLastDecPicInfo->pPreviousDecodedPictureInDpb->bNewSeqBegin){
-			a->lastgopremain = a->npics;
+			a->iLastGOPRemainPicts = a->iNumOfPicts;
 
-			for(i = 0; i <= a->lbufpicind; i++)
+			for(i = 0; i <= a->iLargestBufferedPicIndex; i++)
 				a->pics[i].bLastGOP = a->pics[i].iPOC > IMinInt32;
 		}else{
-			for(i = 0; i <= a->lbufpicind; i++){
+			for(i = 0; i <= a->iLargestBufferedPicIndex; i++){
 				if(a->pics[i].iPOC == a->ctx.pSliceHeader->iPicOrderCntLsb){
-					a->lastgopremain = a->npics;
-					for(i = 0; i <= a->lbufpicind; i++)
+					a->iLastGOPRemainPicts = a->iNumOfPicts;
+					for(i = 0; i <= a->iLargestBufferedPicIndex; i++)
 						a->pics[i].bLastGOP = a->pics[i].iPOC > IMinInt32;
 					break;
 				}
@@ -73,100 +67,84 @@
 			a->ctx.pLastDecPicInfo->pPreviousDecodedPictureInDpb->iRefCount++;
 			a->pics[i].bLastGOP = false;
 			a->info.iBufferStatus = 0;
-			a->npics++;
-			if(i > a->lbufpicind)
-				a->lbufpicind = i;
+			a->iNumOfPicts++;
+			if(a->iLargestBufferedPicIndex < i)
+				a->iLargestBufferedPicIndex = i;
 			break;
 		}
 	}
 
 	PPicBuff picbuf = a->ctx.pPicBuff;
-	if(a->lastgopremain > 0){
-		a->minpoc = IMinInt32;
+	if(a->iLastGOPRemainPicts > 0){
+		a->iMinPOC = IMinInt32;
 		firstvalid = -1;
-		for(i = 0; i <= a->lbufpicind; i++){
-			if(a->minpoc == IMinInt32 && a->pics[i].iPOC > IMinInt32 && a->pics[i].bLastGOP){
-				a->minpoc = a->pics[i].iPOC;
-				a->picind = i;
+		for(i = 0; i <= a->iLargestBufferedPicIndex; i++){
+			if(a->iMinPOC == IMinInt32 && a->pics[i].iPOC > IMinInt32 && a->pics[i].bLastGOP){
+				a->iMinPOC = a->pics[i].iPOC;
+				a->iPictInfoIndex = i;
 				firstvalid = i;
 				break;
 			}
 		}
-		for(i = 0; i <= a->lbufpicind; i++){
+		for(i = 0; i <= a->iLargestBufferedPicIndex; i++){
 			if(i == firstvalid)
 				continue;
-			if(a->pics[i].iPOC > IMinInt32 && a->pics[i].iPOC < a->minpoc && a->pics[i].bLastGOP){
-				a->minpoc = a->pics[i].iPOC;
-				a->picind = i;
+			if(a->pics[i].iPOC > IMinInt32 && a->pics[i].iPOC < a->iMinPOC && a->pics[i].bLastGOP){
+				a->iMinPOC = a->pics[i].iPOC;
+				a->iPictInfoIndex = i;
 			}
 		}
-		a->lastwritten = a->minpoc;
-		memmove(&a->info, &a->pics[a->picind].sBufferInfo, sizeof(a->info));
+		a->iLastWrittenPOC = a->iMinPOC;
+		memmove(&a->info, &a->pics[a->iPictInfoIndex].sBufferInfo, sizeof(a->info));
 		a->data[0] = a->info.pDst[0];
 		a->data[1] = a->info.pDst[1];
 		a->data[2] = a->info.pDst[2];
-		a->pics[a->picind].iPOC = IMinInt32;
-		picbuf->ppPic[a->pics[a->picind].iPicBuffIdx]->iRefCount--;
-		a->pics[a->picind].bLastGOP = false;
-		a->minpoc = IMinInt32;
-		a->npics--;
-		a->lastgopremain--;
-		if(a->lastgopremain == 0)
-			a->lastwritten = IMinInt32;
+		a->pics[a->iPictInfoIndex].iPOC = IMinInt32;
+		picbuf->ppPic[a->pics[a->iPictInfoIndex].iPicBuffIdx]->iRefCount--;
+		a->pics[a->iPictInfoIndex].bLastGOP = false;
+		a->iMinPOC = IMinInt32;
+		a->iNumOfPicts--;
+		a->iLastGOPRemainPicts--;
+		if(a->iLastGOPRemainPicts == 0)
+			a->iLastWrittenPOC = IMinInt32;
 		return;
 	}
 
-	if(a->npics > 0){
-		a->minpoc = IMinInt32;
+	if(a->iNumOfPicts > 0){
+		a->iMinPOC = IMinInt32;
 		firstvalid = -1;
-		for(i = 0; i <= a->lbufpicind; i++){
-			if(a->minpoc == IMinInt32 && a->pics[i].iPOC > IMinInt32){
-				a->minpoc = a->pics[i].iPOC;
-				a->picind = i;
+		for(i = 0; i <= a->iLargestBufferedPicIndex; i++){
+			if(a->iMinPOC == IMinInt32 && a->pics[i].iPOC > IMinInt32){
+				a->iMinPOC = a->pics[i].iPOC;
+				a->iPictInfoIndex = i;
 				firstvalid = i;
 				break;
 			}
 		}
-		for(i = 0; i <= a->lbufpicind; i++){
+		for(i = 0; i <= a->iLargestBufferedPicIndex; i++){
 			if(i == firstvalid)
 				continue;
-			if(a->pics[i].iPOC > IMinInt32 && a->pics[i].iPOC < a->minpoc){
-				a->minpoc = a->pics[i].iPOC;
-				a->picind = i;
+			if(a->pics[i].iPOC > IMinInt32 && a->pics[i].iPOC < a->iMinPOC){
+				a->iMinPOC = a->pics[i].iPOC;
+				a->iPictInfoIndex = i;
 			}
 		}
 	}
-	if(a->minpoc > IMinInt32 && ((a->lastwritten > IMinInt32 && a->minpoc - a->lastwritten <= 1) || a->minpoc < a->ctx.pSliceHeader->iPicOrderCntLsb)){
-		a->lastwritten = a->minpoc;
-		memmove(&a->info, &a->pics[a->picind].sBufferInfo, sizeof(a->info));
+
+	if(a->iMinPOC > IMinInt32 && ((a->iLastWrittenPOC > IMinInt32 && a->iMinPOC - a->iLastWrittenPOC <= 1) || a->iMinPOC < a->ctx.pSliceHeader->iPicOrderCntLsb)){
+		a->iLastWrittenPOC = a->iMinPOC;
+		memmove(&a->info, &a->pics[a->iPictInfoIndex].sBufferInfo, sizeof(a->info));
 		a->data[0] = a->info.pDst[0];
 		a->data[1] = a->info.pDst[1];
 		a->data[2] = a->info.pDst[2];
-		a->pics[a->picind].iPOC = IMinInt32;
-		picbuf->ppPic[a->pics[a->picind].iPicBuffIdx]->iRefCount--;
-		a->pics[a->picind].bLastGOP = false;
-		a->minpoc = IMinInt32;
-		a->npics--;
+		a->pics[a->iPictInfoIndex].iPOC = IMinInt32;
+		picbuf->ppPic[a->pics[a->iPictInfoIndex].iPicBuffIdx]->iRefCount--;
+		a->pics[a->iPictInfoIndex].bLastGOP = false;
+		a->iMinPOC = IMinInt32;
+		a->iNumOfPicts--;
 	}
 }
 
-static void
-resetreorder(Aux *a, int full)
-{
-	int i;
-
-	a->npics = 0;
-	a->lastgopremain = 0;
-	a->lastwritten = IMinInt32;
-	a->minpoc = IMinInt32;
-	a->picind = 0;
-	for(i = 0; i < (full ? nelem(a->pics) : (a->lbufpicind+1)); i++){
-		a->pics[i].bLastGOP = false;
-		a->pics[i].iPOC = IMinInt32;
-	}
-	a->lbufpicind = 0;
-}
-
 static char *
 err2s(int err)
 {
@@ -283,7 +261,7 @@
 		fprint(2, "h264: frame %llud: %r\n", framenum);
 
 	/* FIXME the frames are finished but there might still be left-overs */
-	resetreorder(a, 0);
+	ResetReorderingPictureBuffers(a, a->pics, false);
 	WelsResetRefPic(&a->ctx);
 
 done:
@@ -307,6 +285,7 @@
 	a->ctx.pVlcTable = &a->vlctbl;
 	a->ctx.pDecoderStatistics = &a->stat;
 	a->ctx.pMemAlign = &cMemoryAlign;
+	a->ctx.pPictReoderingStatus = a;
 	a->logctx.pfLog = logfun;
 	a->ctx.sLogCtx = a->logctx;
 
@@ -313,7 +292,7 @@
 	a->ctx.pParam = calloc(1, sizeof(SDecodingParam));
 	a->ctx.pParam->sVideoProperty.eVideoBsType = VIDEO_BITSTREAM_DEFAULT;
 
-	resetreorder(a, 1);
+	ResetReorderingPictureBuffers(a, a->pics, true);
 
 	if((res = WelsInitDecoder(&a->ctx, &a->logctx)) != 0){
 		werrstr("WelsInitDecoder: %s", err2s(res));