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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
|
/*
* (C) 2006-2012 see Authors.txt
*
* This file is part of MPC-HC.
*
* MPC-HC is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* MPC-HC is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include "stdafx.h"
#include "DXVADecoderMpeg2.h"
#include "MPCVideoDecFilter.h"
#include "FfmpegContext.h"
#if 0
#define TRACE_MPEG2 TRACE
#else
#define TRACE_MPEG2 __noop
#endif
CDXVADecoderMpeg2::CDXVADecoderMpeg2(CMPCVideoDecFilter* pFilter, IAMVideoAccelerator* pAMVideoAccelerator, DXVAMode nMode, int nPicEntryNumber)
: CDXVADecoder(pFilter, pAMVideoAccelerator, nMode, nPicEntryNumber)
{
Init();
}
CDXVADecoderMpeg2::CDXVADecoderMpeg2(CMPCVideoDecFilter* pFilter, IDirectXVideoDecoder* pDirectXVideoDec, DXVAMode nMode, int nPicEntryNumber, DXVA2_ConfigPictureDecode* pDXVA2Config)
: CDXVADecoder(pFilter, pDirectXVideoDec, nMode, nPicEntryNumber, pDXVA2Config)
{
Init();
}
CDXVADecoderMpeg2::~CDXVADecoderMpeg2()
{
Flush();
}
void CDXVADecoderMpeg2::Init()
{
memset(&m_PictureParams, 0, sizeof(m_PictureParams));
memset(&m_SliceInfo, 0, sizeof(m_SliceInfo));
memset(&m_QMatrixData, 0, sizeof(m_QMatrixData));
m_nMaxWaiting = 5;
m_wRefPictureIndex[0] = NO_REF_FRAME;
m_wRefPictureIndex[1] = NO_REF_FRAME;
m_nSliceCount = 0;
switch (GetMode()) {
case MPEG2_VLD:
AllocExecuteParams(4);
break;
default:
ASSERT(FALSE);
}
}
// === Public functions
HRESULT CDXVADecoderMpeg2::DecodeFrame(BYTE* pDataIn, UINT nSize, REFERENCE_TIME rtStart, REFERENCE_TIME rtStop)
{
HRESULT hr;
int nFieldType;
int nSliceType;
FFMpeg2DecodeFrame(&m_PictureParams, &m_QMatrixData, m_SliceInfo, &m_nSliceCount, m_pFilter->GetAVCtx(),
m_pFilter->GetFrame(), &m_nNextCodecIndex, &nFieldType, &nSliceType, pDataIn, nSize);
if (m_PictureParams.bSecondField && !m_bSecondField) {
m_bSecondField = true;
}
// Wait I frame after a flush
if (m_bFlushed && (!m_PictureParams.bPicIntra || (m_bSecondField && m_PictureParams.bSecondField))) {
TRACE_MPEG2("CDXVADecoderMpeg2::DecodeFrame() : Flush - wait I frame\n");
return S_FALSE;
}
if (m_bSecondField) {
if (!m_PictureParams.bSecondField) {
m_rtStart = rtStart;
m_rtStop = rtStop;
m_pSampleToDeliver = NULL;
hr = GetFreeSurfaceIndex(m_nSurfaceIndex, &m_pSampleToDeliver, rtStart, rtStop);
if (FAILED(hr)) {
ASSERT(hr == VFW_E_NOT_COMMITTED); // Normal when stop playing
return hr;
}
}
} else {
m_rtStart = rtStart;
m_rtStop = rtStop;
m_pSampleToDeliver = NULL;
hr = GetFreeSurfaceIndex(m_nSurfaceIndex, &m_pSampleToDeliver, rtStart, rtStop);
if (FAILED(hr)) {
ASSERT(hr == VFW_E_NOT_COMMITTED); // Normal when stop playing
return hr;
}
}
if (m_pSampleToDeliver == NULL) {
return S_FALSE;
}
CHECK_HR_TRACE(BeginFrame(m_nSurfaceIndex, m_pSampleToDeliver));
if (m_bSecondField) {
if (!m_PictureParams.bSecondField) {
UpdatePictureParams(m_nSurfaceIndex);
}
} else {
UpdatePictureParams(m_nSurfaceIndex);
}
TRACE_MPEG2("CDXVADecoderMpeg2::DecodeFrame() : Surf = %d, PictureType = %d, SecondField = %d, m_nNextCodecIndex = %d, rtStart = [%I64d]\n",
m_nSurfaceIndex, nSliceType, m_PictureParams.bSecondField, m_nNextCodecIndex, rtStart);
CHECK_HR_TRACE(AddExecuteBuffer(DXVA2_PictureParametersBufferType, sizeof(m_PictureParams), &m_PictureParams));
CHECK_HR_TRACE(AddExecuteBuffer(DXVA2_InverseQuantizationMatrixBufferType, sizeof(m_QMatrixData), &m_QMatrixData));
// Send bitstream to accelerator
CHECK_HR_TRACE(AddExecuteBuffer(DXVA2_SliceControlBufferType, sizeof(DXVA_SliceInfo)*m_nSliceCount, &m_SliceInfo));
CHECK_HR_TRACE(AddExecuteBuffer(DXVA2_BitStreamDateBufferType, nSize, pDataIn, &nSize));
// Decode frame
CHECK_HR_TRACE(Execute());
CHECK_HR_TRACE(EndFrame(m_nSurfaceIndex));
if (m_bSecondField) {
if (m_PictureParams.bSecondField) {
AddToStore(m_nSurfaceIndex, m_pSampleToDeliver, (m_PictureParams.bPicBackwardPrediction != 1), m_rtStart, m_rtStop,
false, (FF_FIELD_TYPE)nFieldType, (FF_SLICE_TYPE)nSliceType, FFGetCodedPicture(m_pFilter->GetAVCtx()));
hr = DisplayNextFrame();
}
} else {
AddToStore(m_nSurfaceIndex, m_pSampleToDeliver, (m_PictureParams.bPicBackwardPrediction != 1), m_rtStart, m_rtStop,
false, (FF_FIELD_TYPE)nFieldType, (FF_SLICE_TYPE)nSliceType, FFGetCodedPicture(m_pFilter->GetAVCtx()));
hr = DisplayNextFrame();
}
m_bFlushed = false;
return hr;
}
void CDXVADecoderMpeg2::UpdatePictureParams(int nSurfaceIndex)
{
DXVA2_ConfigPictureDecode* cpd = GetDXVA2Config(); // Ok for DXVA1 too (parameters have been copied)
m_PictureParams.wDecodedPictureIndex = nSurfaceIndex;
// Manage reference picture list
if (!m_PictureParams.bPicBackwardPrediction) {
if (m_wRefPictureIndex[0] != NO_REF_FRAME) {
RemoveRefFrame(m_wRefPictureIndex[0]);
}
m_wRefPictureIndex[0] = m_wRefPictureIndex[1];
m_wRefPictureIndex[1] = nSurfaceIndex;
}
m_PictureParams.wForwardRefPictureIndex = (m_PictureParams.bPicIntra == 0) ? m_wRefPictureIndex[0] : NO_REF_FRAME;
m_PictureParams.wBackwardRefPictureIndex = (m_PictureParams.bPicBackwardPrediction == 1) ? m_wRefPictureIndex[1] : NO_REF_FRAME;
// Shall be 0 if bConfigResidDiffHost is 0 or if BPP > 8
if (cpd->ConfigResidDiffHost == 0 || m_PictureParams.bBPPminus1 > 7) {
m_PictureParams.bPicSpatialResid8 = 0;
} else {
if (m_PictureParams.bBPPminus1 == 7 && m_PictureParams.bPicIntra && cpd->ConfigResidDiffHost)
// Shall be 1 if BPP is 8 and bPicIntra is 1 and bConfigResidDiffHost is 1
{
m_PictureParams.bPicSpatialResid8 = 1;
} else
// Shall be 1 if bConfigSpatialResid8 is 1
{
m_PictureParams.bPicSpatialResid8 = cpd->ConfigSpatialResid8;
}
}
// Shall be 0 if bConfigResidDiffHost is 0 or if bConfigSpatialResid8 is 0 or if BPP > 8
if (cpd->ConfigResidDiffHost == 0 || cpd->ConfigSpatialResid8 == 0 || m_PictureParams.bBPPminus1 > 7) {
m_PictureParams.bPicOverflowBlocks = 0;
}
// Shall be 1 if bConfigHostInverseScan is 1 or if bConfigResidDiffAccelerator is 0.
if (cpd->ConfigHostInverseScan == 1 || cpd->ConfigResidDiffAccelerator == 0) {
m_PictureParams.bPicScanFixed = 1;
if (cpd->ConfigHostInverseScan != 0) {
m_PictureParams.bPicScanMethod = 3; // 11 = Arbitrary scan with absolute coefficient address.
} else if (FFGetAlternateScan(m_pFilter->GetAVCtx())) {
m_PictureParams.bPicScanMethod = 1; // 00 = Zig-zag scan (MPEG-2 Figure 7-2)
} else {
m_PictureParams.bPicScanMethod = 0; // 01 = Alternate-vertical (MPEG-2 Figure 7-3),
}
}
}
void CDXVADecoderMpeg2::CopyBitstream(BYTE* pDXVABuffer, BYTE* pBuffer, UINT& nSize)
{
while (*((DWORD*)pBuffer) != 0x01010000) {
pBuffer++;
nSize--;
if (nSize <= 0) {
return;
}
}
memcpy(pDXVABuffer, pBuffer, nSize);
}
void CDXVADecoderMpeg2::Flush()
{
m_nNextCodecIndex = INT_MIN;
if (m_wRefPictureIndex[0] != NO_REF_FRAME) {
RemoveRefFrame(m_wRefPictureIndex[0]);
}
if (m_wRefPictureIndex[1] != NO_REF_FRAME) {
RemoveRefFrame(m_wRefPictureIndex[1]);
}
m_wRefPictureIndex[0] = NO_REF_FRAME;
m_wRefPictureIndex[1] = NO_REF_FRAME;
m_nSurfaceIndex = 0;
m_pSampleToDeliver = NULL;
m_bSecondField = false;
m_rtStart = _I64_MIN;
m_rtStop = _I64_MIN;
m_rtLastStart = 0;
__super::Flush();
}
int CDXVADecoderMpeg2::FindOldestFrame()
{
int nPos = -1;
for (int i = 0; i < m_nPicEntryNumber; i++) {
if (!m_pPictureStore[i].bDisplayed &&
m_pPictureStore[i].bInUse &&
(m_pPictureStore[i].nCodecSpecific == m_nNextCodecIndex)) {
m_nNextCodecIndex = INT_MIN;
nPos = i;
}
}
if (nPos != -1) {
UpdateFrameTime(m_pPictureStore[nPos].rtStart, m_pPictureStore[nPos].rtStop);
}
return nPos;
}
void CDXVADecoderMpeg2::UpdateFrameTime(REFERENCE_TIME& rtStart, REFERENCE_TIME& rtStop)
{
TRACE(_T("\n\nrtStart = [%10I64d - %10I64d] // "), rtStart, rtStop);
if (m_rtLastStart && (rtStart == _I64_MIN || (rtStart < m_rtLastStart))) {
rtStart = m_rtLastStart;
}
rtStop = rtStart + (REFERENCE_TIME)(m_pFilter->GetAvrTimePerFrame() / m_pFilter->GetRate());
m_rtLastStart = rtStop;
TRACE(_T("rtStart = [%10I64d - %10I64d]\n"), rtStart, rtStop);
}
|