shithub: treason

Download patch

ref: 7fff6826ea87e9162151a6b48f4d61d0ad509c6b
parent: 14e6292060224eaab98ecd331b7ac3c309beb2e2
author: Sigrid Solveig Haflínudóttir <ftrvxmtrx@gmail.com>
date: Wed Feb 10 10:11:55 EST 2021

h264: clean up the reordering mess a bit

--- a/decoder_h264.c
+++ b/decoder_h264.c
@@ -41,8 +41,8 @@
 	}
 }
 
-static uvlong
-reorder(Aux *a, uvlong ts)
+static void
+reorder(Aux *a)
 {
 	int i, firstvalid;
 
@@ -69,7 +69,7 @@
 			memmove(&a->pics[i].sBufferInfo, &a->info, sizeof(a->info));
 			a->pics[i].iPOC = a->ctx.pSliceHeader->iPicOrderCntLsb;
 			a->pics[i].iPicBuffIdx = a->ctx.pLastDecPicInfo->pPreviousDecodedPictureInDpb->iPicBuffIdx;
-			a->pics[i].uiDecodingTimeStamp = ts;
+			a->pics[i].uiDecodingTimeStamp = a->ctx.uiDecodingTimeStamp;
 			a->ctx.pLastDecPicInfo->pPreviousDecodedPictureInDpb->iRefCount++;
 			a->pics[i].bLastGOP = false;
 			a->info.iBufferStatus = 0;
@@ -106,7 +106,6 @@
 		a->data[1] = a->info.pDst[1];
 		a->data[2] = a->info.pDst[2];
 		a->pics[a->picind].iPOC = IMinInt32;
-		ts = a->pics[a->picind].uiDecodingTimeStamp;
 		picbuf->ppPic[a->pics[a->picind].iPicBuffIdx]->iRefCount--;
 		a->pics[a->picind].bLastGOP = false;
 		a->minpoc = IMinInt32;
@@ -114,7 +113,7 @@
 		a->lastgopremain--;
 		if(a->lastgopremain == 0)
 			a->lastwritten = IMinInt32;
-		return ts;
+		return;
 	}
 
 	if(a->npics > 0){
@@ -143,7 +142,6 @@
 		a->data[0] = a->info.pDst[0];
 		a->data[1] = a->info.pDst[1];
 		a->data[2] = a->info.pDst[2];
-		ts = a->pics[a->picind].uiDecodingTimeStamp;
 		a->pics[a->picind].iPOC = IMinInt32;
 		picbuf->ppPic[a->pics[a->picind].iPicBuffIdx]->iRefCount--;
 		a->pics[a->picind].bLastGOP = false;
@@ -150,8 +148,6 @@
 		a->minpoc = IMinInt32;
 		a->npics--;
 	}
-
-	return ts;
 }
 
 static void
@@ -201,17 +197,18 @@
 {
 	int res;
 
-	if(sf->buf == nil){
+	if(sf->buf == nil || sf->sz < 1){
 		a->ctx.bEndOfStreamFlag = true;
 		a->ctx.bInstantDecFlag = true;
 	}else{
 		a->ctx.bEndOfStreamFlag = false;
 	}
+	memset(a->data, 0, sizeof(a->data));
+	memset(&a->info, 0, sizeof(a->info));
+	a->info.uiInBsTimeStamp = sf->timestamp;
+	a->ctx.uiTimeStamp = sf->timestamp;
 	a->ctx.iErrorCode = dsErrorFree;
 	a->ctx.iFeedbackVclNalInAu = FEEDBACK_UNKNOWN_NAL;
-    a->ctx.uiTimeStamp = a->info.uiInBsTimeStamp;
-	memset(&a->info, 0, sizeof(a->info));
-	memset(a->data, 0, sizeof(a->data));
 	a->ctx.bReferenceLostAtT0Flag = false;
 	a->ctx.bCurAuContainLtrMarkSeFlag = false;
 	a->ctx.iFrameNumOfAuMarkedLtr = 0;
@@ -218,17 +215,19 @@
 	a->ctx.iFrameNum = -1;
 	a->ctx.iFeedbackTidInAu = -1;
 	a->ctx.iFeedbackNalRefIdc = -1;
-	if((res = WelsDecodeBs(&a->ctx, sf->buf, sf->sz, a->data, &a->info, nil)) != 0){
-		if(a->ctx.iErrorCode & dsOutOfMemory)
+	res = WelsDecodeBs(&a->ctx, sf->buf, sf->sz, a->data, &a->info, nil);
+	a->ctx.bInstantDecFlag = false;
+	if(res != 0){
+		if(res & dsOutOfMemory)
 			return 0;
-		werrstr("%s: %.*H", err2s(res), MIN(32, sf->sz), sf->buf);
+		werrstr("%s: %.*H", err2s(a->ctx.iErrorCode), MIN(32, sf->sz), sf->buf);
 		return -1;
 	}
 
 	if(a->info.iBufferStatus != 0 && a->ctx.pSps->uiProfileIdc != 66 && a->ctx.pSps->uiProfileIdc != 83){
 		/* non-baseline needs reordering */
-		memset(a->data, 0, sizeof(a->data));
-		sf->timestamp = reorder(a, sf->timestamp);
+		reorder(a);
+		sf->timestamp = a->info.uiOutYuvTimeStamp;
 	}
 
 	return 0;
@@ -254,8 +253,14 @@
 	for(res = 0, framenum = 0; res >= 0 && (res = Sread(d->s, &sf)) == 0 && sf.sz > 0; framenum++){
 		if(one(a, &sf) != 0)
 			goto failed;
+		sf.buf = nil;
+		sf.sz = 0;
+		if(one(a, &sf) != 0){
+failed:
+			werrstr("frame %llud: %r", framenum);
+			break;
+		}
 
-tryagain:
 		if(a->data[0] != nil && sf.timestamp >= lasttimestamp){
 			d->decodetime = nanosec() - start;
 
@@ -276,17 +281,6 @@
 				}
 			}
 			start = nanosec();
-		}
-
-		if(sf.buf != nil){
-			sf.buf = nil;
-			sf.sz = 0;
-			if(one(a, &sf) != 0){
-failed:
-				werrstr("frame %llud: %r", framenum);
-				break;
-			}
-			goto tryagain;
 		}
 	}
 	if(res != 0)