summaryrefslogtreecommitdiff
path: root/media-libs/openh264/files/openh264-2.4.0-unordered-frame-glitches.patch
blob: 373fac95f940cd42e643c151fca1f6431bb94f46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
From 56d3ec4c7bfdc545a840512a8f2c72545889c538 Mon Sep 17 00:00:00 2001
From: Takashi Yano <takashi.yano@nifty.ne.jp>
Date: Tue, 5 Dec 2023 21:48:40 +0900
Subject: [PATCH 1/3] Add missing iLastWrittenPOC setting in unbuffered
 reordering.

In CWelsDecoder::ReorderPicturesInDisplay(), iLastWrittenPOC was
not set in unbuffered-reordering case. Due to this problem, it
sometimes reordered the frames incorrectly. This patch fixes the
issue.
---
 codec/decoder/plus/src/welsDecoderExt.cpp | 1 +
 1 file changed, 1 insertion(+)

diff --git a/codec/decoder/plus/src/welsDecoderExt.cpp b/codec/decoder/plus/src/welsDecoderExt.cpp
index 457effb68..a1b26c2cb 100644
--- a/codec/decoder/plus/src/welsDecoderExt.cpp
+++ b/codec/decoder/plus/src/welsDecoderExt.cpp
@@ -1206,6 +1206,7 @@ DECODING_STATE CWelsDecoder::ReorderPicturesInDisplay(PWelsDecoderContext pDecCo
       if (pDstInfo->iBufferStatus == 1) {
         if (m_sReoderingStatus.iLastGOPRemainPicts == 0 && pDecContext->pSliceHeader->eSliceType == B_SLICE && 
             pDecContext->pSliceHeader->iPicOrderCntLsb <= m_sReoderingStatus.iLastWrittenPOC + 2) {
+          m_sReoderingStatus.iLastWrittenPOC = pDecContext->pSliceHeader->iPicOrderCntLsb;
           //issue #3478, use b-slice type to determine correct picture order as the first priority as POC order is not as reliable as based on b-slice 
           ppDst[0] = pDstInfo->pDst[0];
           ppDst[1] = pDstInfo->pDst[1];

From 10d0998a966dc98ba1a93122f214c2aef5bcd33f Mon Sep 17 00:00:00 2001
From: Takashi Yano <takashi.yano@nifty.ne.jp>
Date: Wed, 6 Dec 2023 02:08:19 +0900
Subject: [PATCH 2/3] Prevent frame buffer from overwrite at GOP change.

When bNewSeqBegin got true, iRefCount was forcibly reset even if the
buffer was still used for reordering. Due to this problem, the buffer
in use was sometimes overwritten with newly decoded frame. This commit
is for fixing that problem.
---
 codec/decoder/core/src/manage_dec_ref.cpp | 37 ++++++++++++-----------
 codec/decoder/plus/src/welsDecoderExt.cpp |  4 +++
 2 files changed, 24 insertions(+), 17 deletions(-)

diff --git a/codec/decoder/core/src/manage_dec_ref.cpp b/codec/decoder/core/src/manage_dec_ref.cpp
index 88099b60e..c3c983129 100644
--- a/codec/decoder/core/src/manage_dec_ref.cpp
+++ b/codec/decoder/core/src/manage_dec_ref.cpp
@@ -66,8 +66,10 @@ int32_t GetLTRFrameIndex (PRefPic pRefPic, int32_t iAncLTRFrameNum);
 #endif
 static int32_t RemainOneBufferInDpbForEC (PWelsDecoderContext pCtx, PRefPic pRefPic);
 
-static void SetUnRef (PPicture pRef) {
-  if (NULL != pRef) {
+static void SetUnRef (PPicture pRef, bool bNewSeqBegin) {
+  if (pRef == NULL) return;
+
+  if (pRef->iRefCount <= 0 && (!pRef->bUsedAsRef || bNewSeqBegin)) {
     pRef->bUsedAsRef = false;
     pRef->bIsLongRef = false;
     pRef->iFrameNum = -1;
@@ -81,17 +83,18 @@ static void SetUnRef (PPicture pRef) {
     pRef->iSpsId = -1;
     pRef->bIsComplete = false;
     pRef->iRefCount = 0;
+  }
 
-    if (pRef->eSliceType == I_SLICE) {
-      return;
-    }
-    int32_t lists = pRef->eSliceType == P_SLICE ? 1 : 2;
-    for (int32_t i = 0; i < MAX_DPB_COUNT; ++i) {
-      for (int32_t list = 0; list < lists; ++list) {
-        if (pRef->pRefPic[list][i] != NULL) {
-          pRef->pRefPic[list][i]->iRefCount = 0;
-          pRef->pRefPic[list][i] = NULL;
-        }
+  if (pRef->eSliceType == I_SLICE) {
+    return;
+  }
+  int32_t lists = pRef->eSliceType == P_SLICE ? 1 : 2;
+  for (int32_t i = 0; i < MAX_DPB_COUNT; ++i) {
+    for (int32_t list = 0; list < lists; ++list) {
+      if (pRef->pRefPic[list][i] != NULL) {
+        if (pRef->pRefPic[list][i]->iRefCount > 0) continue;
+        pRef->pRefPic[list][i]->iRefCount = 0;
+        pRef->pRefPic[list][i] = NULL;
       }
     }
   }
@@ -111,7 +114,7 @@ void WelsResetRefPic (PWelsDecoderContext pCtx) {
 
   for (i = 0; i < MAX_DPB_COUNT; i++) {
     if (pRefPic->pShortRefList[LIST_0][i] != NULL) {
-      SetUnRef (pRefPic->pShortRefList[LIST_0][i]);
+      SetUnRef (pRefPic->pShortRefList[LIST_0][i], pCtx->bNewSeqBegin);
       pRefPic->pShortRefList[LIST_0][i] = NULL;
     }
   }
@@ -119,7 +122,7 @@ void WelsResetRefPic (PWelsDecoderContext pCtx) {
 
   for (i = 0; i < MAX_DPB_COUNT; i++) {
     if (pRefPic->pLongRefList[LIST_0][i] != NULL) {
-      SetUnRef (pRefPic->pLongRefList[LIST_0][i]);
+      SetUnRef (pRefPic->pLongRefList[LIST_0][i], pCtx->bNewSeqBegin);
       pRefPic->pLongRefList[LIST_0][i] = NULL;
     }
   }
@@ -767,7 +770,7 @@ static int32_t SlidingWindow (PWelsDecoderContext pCtx, PRefPic pRefPic) {
     for (i = pRefPic->uiShortRefCount[LIST_0] - 1; i >= 0; i--) {
       pPic = WelsDelShortFromList (pRefPic, pRefPic->pShortRefList[LIST_0][i]->iFrameNum);
       if (pPic) {
-        SetUnRef (pPic);
+        SetUnRef (pPic, pCtx->bNewSeqBegin);
         break;
       } else {
         return ERR_INFO_INVALID_MMCO_REF_NUM_OVERFLOW;
@@ -803,7 +806,7 @@ static PPicture WelsDelShortFromList (PRefPic pRefPic, int32_t iFrameNum) {
 static PPicture WelsDelShortFromListSetUnref (PRefPic pRefPic, int32_t iFrameNum) {
   PPicture pPic = WelsDelShortFromList (pRefPic, iFrameNum);
   if (pPic) {
-    SetUnRef (pPic);
+    SetUnRef (pPic, false);
   }
   return pPic;
 }
@@ -832,7 +835,7 @@ static PPicture WelsDelLongFromList (PRefPic pRefPic, uint32_t uiLongTermFrameId
 static PPicture WelsDelLongFromListSetUnref (PRefPic pRefPic, uint32_t uiLongTermFrameIdx) {
   PPicture pPic = WelsDelLongFromList (pRefPic, uiLongTermFrameIdx);
   if (pPic) {
-    SetUnRef (pPic);
+    SetUnRef (pPic, false);
   }
   return pPic;
 }
diff --git a/codec/decoder/plus/src/welsDecoderExt.cpp b/codec/decoder/plus/src/welsDecoderExt.cpp
index a1b26c2cb..14257afff 100644
--- a/codec/decoder/plus/src/welsDecoderExt.cpp
+++ b/codec/decoder/plus/src/welsDecoderExt.cpp
@@ -1084,6 +1084,8 @@ void CWelsDecoder::ReleaseBufferedReadyPictureReorder (PWelsDecoderContext pCtx,
     if (pPicBuff != NULL) {
       PPicture pPic = pPicBuff->ppPic[m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].iPicBuffIdx];
       --pPic->iRefCount;
+      if (m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].bLastGOP)
+        pPic->bUsedAsRef = false;
     }
     m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].bLastGOP = false;
     m_sReoderingStatus.iMinPOC = IMinInt32;
@@ -1187,6 +1189,8 @@ void CWelsDecoder::ReleaseBufferedReadyPictureNoReorder(PWelsDecoderContext pCtx
       PPicBuff pPicBuff = pCtx ? pCtx->pPicBuff : m_pPicBuff;
       PPicture pPic = pPicBuff->ppPic[m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].iPicBuffIdx];
       --pPic->iRefCount;
+      if (m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].bLastGOP)
+        pPic->bUsedAsRef = false;
     }
     if (m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].bLastGOP) {
       --m_sReoderingStatus.iLastGOPRemainPicts;

From f82abc09a3e7d723e21a06ec7e5f38afc218357f Mon Sep 17 00:00:00 2001
From: Takashi Yano <takashi.yano@nifty.ne.jp>
Date: Tue, 12 Dec 2023 19:23:32 +0900
Subject: [PATCH 3/3] Fix test failure due to the issue of the previous commit.

---
 codec/decoder/core/src/manage_dec_ref.cpp | 14 +++++++-------
 codec/decoder/plus/src/welsDecoderExt.cpp |  9 +++++++--
 2 files changed, 14 insertions(+), 9 deletions(-)

diff --git a/codec/decoder/core/src/manage_dec_ref.cpp b/codec/decoder/core/src/manage_dec_ref.cpp
index c3c983129..410b9c47f 100644
--- a/codec/decoder/core/src/manage_dec_ref.cpp
+++ b/codec/decoder/core/src/manage_dec_ref.cpp
@@ -66,10 +66,10 @@ int32_t GetLTRFrameIndex (PRefPic pRefPic, int32_t iAncLTRFrameNum);
 #endif
 static int32_t RemainOneBufferInDpbForEC (PWelsDecoderContext pCtx, PRefPic pRefPic);
 
-static void SetUnRef (PPicture pRef, bool bNewSeqBegin) {
+static void SetUnRef (PPicture pRef) {
   if (pRef == NULL) return;
 
-  if (pRef->iRefCount <= 0 && (!pRef->bUsedAsRef || bNewSeqBegin)) {
+  if (pRef->iRefCount <= 0) {
     pRef->bUsedAsRef = false;
     pRef->bIsLongRef = false;
     pRef->iFrameNum = -1;
@@ -114,7 +114,7 @@ void WelsResetRefPic (PWelsDecoderContext pCtx) {
 
   for (i = 0; i < MAX_DPB_COUNT; i++) {
     if (pRefPic->pShortRefList[LIST_0][i] != NULL) {
-      SetUnRef (pRefPic->pShortRefList[LIST_0][i], pCtx->bNewSeqBegin);
+      SetUnRef (pRefPic->pShortRefList[LIST_0][i]);
       pRefPic->pShortRefList[LIST_0][i] = NULL;
     }
   }
@@ -122,7 +122,7 @@ void WelsResetRefPic (PWelsDecoderContext pCtx) {
 
   for (i = 0; i < MAX_DPB_COUNT; i++) {
     if (pRefPic->pLongRefList[LIST_0][i] != NULL) {
-      SetUnRef (pRefPic->pLongRefList[LIST_0][i], pCtx->bNewSeqBegin);
+      SetUnRef (pRefPic->pLongRefList[LIST_0][i]);
       pRefPic->pLongRefList[LIST_0][i] = NULL;
     }
   }
@@ -770,7 +770,7 @@ static int32_t SlidingWindow (PWelsDecoderContext pCtx, PRefPic pRefPic) {
     for (i = pRefPic->uiShortRefCount[LIST_0] - 1; i >= 0; i--) {
       pPic = WelsDelShortFromList (pRefPic, pRefPic->pShortRefList[LIST_0][i]->iFrameNum);
       if (pPic) {
-        SetUnRef (pPic, pCtx->bNewSeqBegin);
+        SetUnRef (pPic);
         break;
       } else {
         return ERR_INFO_INVALID_MMCO_REF_NUM_OVERFLOW;
@@ -806,7 +806,7 @@ static PPicture WelsDelShortFromList (PRefPic pRefPic, int32_t iFrameNum) {
 static PPicture WelsDelShortFromListSetUnref (PRefPic pRefPic, int32_t iFrameNum) {
   PPicture pPic = WelsDelShortFromList (pRefPic, iFrameNum);
   if (pPic) {
-    SetUnRef (pPic, false);
+    SetUnRef (pPic);
   }
   return pPic;
 }
@@ -835,7 +835,7 @@ static PPicture WelsDelLongFromList (PRefPic pRefPic, uint32_t uiLongTermFrameId
 static PPicture WelsDelLongFromListSetUnref (PRefPic pRefPic, uint32_t uiLongTermFrameIdx) {
   PPicture pPic = WelsDelLongFromList (pRefPic, uiLongTermFrameIdx);
   if (pPic) {
-    SetUnRef (pPic, false);
+    SetUnRef (pPic);
   }
   return pPic;
 }
diff --git a/codec/decoder/plus/src/welsDecoderExt.cpp b/codec/decoder/plus/src/welsDecoderExt.cpp
index 14257afff..b0936a7d8 100644
--- a/codec/decoder/plus/src/welsDecoderExt.cpp
+++ b/codec/decoder/plus/src/welsDecoderExt.cpp
@@ -1083,8 +1083,9 @@ void CWelsDecoder::ReleaseBufferedReadyPictureReorder (PWelsDecoderContext pCtx,
     m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].iPOC = IMinInt32;
     if (pPicBuff != NULL) {
       PPicture pPic = pPicBuff->ppPic[m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].iPicBuffIdx];
+      bool bLastGOP = m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].bLastGOP;
       --pPic->iRefCount;
-      if (m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].bLastGOP)
+      if (pPic->iRefCount <= 0 && bLastGOP)
         pPic->bUsedAsRef = false;
     }
     m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].bLastGOP = false;
@@ -1141,7 +1142,10 @@ void CWelsDecoder::ReleaseBufferedReadyPictureReorder (PWelsDecoderContext pCtx,
         if (iPicBuffIdx >= 0 && iPicBuffIdx < pPicBuff->iCapacity)
         {
             PPicture pPic = pPicBuff->ppPic[iPicBuffIdx];
+            bool bLastGOP = m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].bLastGOP;
             --pPic->iRefCount;
+            if (pPic->iRefCount <= 0 && bLastGOP)
+              pPic->bUsedAsRef = false;
         }
       }
       m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].bLastGOP = false;
@@ -1188,8 +1192,9 @@ void CWelsDecoder::ReleaseBufferedReadyPictureNoReorder(PWelsDecoderContext pCtx
     if (pCtx || m_pPicBuff) {
       PPicBuff pPicBuff = pCtx ? pCtx->pPicBuff : m_pPicBuff;
       PPicture pPic = pPicBuff->ppPic[m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].iPicBuffIdx];
+      bool bLastGOP = m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].bLastGOP;
       --pPic->iRefCount;
-      if (m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].bLastGOP)
+      if (pPic->iRefCount <= 0 && bLastGOP)
         pPic->bUsedAsRef = false;
     }
     if (m_sPictInfoList[m_sReoderingStatus.iPictInfoIndex].bLastGOP) {