UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
M4BitstreamParser.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2#pragma once
3
4#include "vdecmpeg4.h"
5#include "M4Global.h"
6#include "M4Bitstream.h"
7#include "M4VlcDecoder.h"
9
10namespace vdecmpeg4
11{
12
13#define M4_CLIP(X,A) (X > A) ? (A) : (X)
14#define M4_ABS(X) (((X)>0)?(X):-(X))
15
16
17class M4Decoder;
18
28
29
30/*
31 ******************************************************************************
32 * This class handles the entire parsing process of the input stream.
33 * Currently, we're reading an MPEG4 compatible bitstream.
34 *
35 * Note: This class is not designed for inheritance.
36 ******************************************************************************
37 */
39{
40public:
43
46
49
52
54 void initFrame(int16 width, int16 height);
55
58
61
63 {
64 uint32 index;
65 while((index = mBitstream->show(9)) == 1)
66 {
67 mBitstream->skip(9);
68 }
69 index >>= 3;
70 mBitstream->skip(VLC_LEN(mVLCDecoder.mTabCbpCIntra[index]));
71 return VLC_CODE(mVLCDecoder.mTabCbpCIntra[index]);
72 }
73
75 {
76 uint32 index;
77 while((index = M4_CLIP(mBitstream->show(9), 256)) == 1)
78 {
79 mBitstream->skip(9);
80 }
81 mBitstream->skip(VLC_LEN(mVLCDecoder.mTabCbpCInter[index]));
82 return VLC_CODE(mVLCDecoder.mTabCbpCInter[index]);
83 }
84
86 {
87 uint8 type;
88 for(type = 0; type <= 3; type++)
89 {
90 if (mBitstream->getBit())
91 {
92 break;
93 }
94 }
95 M4CHECK(type <= 3);
96 return type;
97 }
98
99 /*
100 ******************************************************************************
101 * get CBPY from stream
102 *
103 * This variable length code represents a pattern of non-transparent
104 * blocks with at least one non intra DC transform
105 * coefficient, in a macroblock.
106 ******************************************************************************
107 */
109 {
110 uint32 index = mBitstream->show(6);
111 mBitstream->skip(VLC_LEN(mVLCDecoder.mTabCbpY[index]));
112 uint32 cbpy = VLC_CODE(mVLCDecoder.mTabCbpY[index]);
113 return intra ? cbpy : 15 - cbpy;
114 }
115
116
118 {
119 if (quant > 0 && quant < 5)
120 {
121 return 8;
122 }
123 if (quant < 25 && !luminance)
124 {
125 return (quant + 13) >> 1;
126 }
127 if (quant < 9)
128 {
129 return (uint16)(quant << 1);
130 }
131 if (quant < 25)
132 {
133 return quant + 8;
134 }
135 return luminance ? (uint16)(quant << 1) - 16 : quant - 6;
136 }
137
139 {
140 uint32 code, i;
141
142 if (luminance)
143 {
144 code = mBitstream->show(11);
145 for (i = 11; i > 3; i--)
146 {
147 if (code == 1)
148 {
149 mBitstream->skip(i);
150 return (int32)(i + 1);
151 }
152 code >>= 1;
153 }
154 mBitstream->skip(VLC_LEN(mVLCDecoder.mDCLumTab[code]));
155 return VLC_CODE(mVLCDecoder.mDCLumTab[code]);
156 }
157 else
158 {
159 code = mBitstream->show(12);
160 for(i = 12; i > 2; i--)
161 {
162 if (code == 1)
163 {
164 mBitstream->skip(i);
165 return (int32)i;
166 }
167 code >>= 1;
168 }
169 return 3 - (int32)mBitstream->getBits(2);
170 }
171 }
172
174 {
175 M4CHECK(dc_size < 32U);
176 int32 code = (int32)mBitstream->getBits(dc_size);
177 int32 msb = code >> (dc_size - 1);
178 return msb == 0 ? -1 * (code^((1<<dc_size) - 1)) : code;
179 }
180
182 {
183 getInterBlockNoAsm(block);
184 }
185
190
192 {
193 if ( !mBitstream->getBit() ) // '0'
194 {
195 return 0;
196 }
197 else if (!mBitstream->getBit() ) // '10'
198 {
199 return -2;
200 }
201 else
202 {
203 return 2; // '11'
204 }
205 }
206
207 /*
208 ******************************************************************************
209 * Get a coded INTRA block from bitstream
210 *
211 * Note: We assume that the input block is set to 0.
212 *
213 * @param block
214 * ptr to int16 to receive coefficients from from bitstream
215 * @param direction
216 * 0: zigZag scan
217 * 1: horizontal scan
218 * 2: vertical scan
219 * @param startCoeff
220 * where to start in block
221 ******************************************************************************
222 **/
224 {
225 const uint8* scan = mScanTable[direction];
226 int32 last, run;
227
228 do
229 {
230 int32 level = mVLCDecoder.getCoeffIntraNoAsm(run, last, *mBitstream);
231 M4CHECK((level >= -2047) && (level <= 2047) && "getIntraBlock: intra_overflow!!");
232 M4CHECK(run != -1 && "getIntraBlock: invalid run");
233 M4CHECK(run >= 0 && "getIntraBlock: invalid run"); // JPCHANGE
234 startCoeff += (uint32)run; // number of '0's to skip
236 block[ scan[startCoeff] ] = (int16)level; // set to current level
237 startCoeff++;
238 }
239 while(!last); // last == 0: "there are more nonzero coefficients in this block"
240 }
241
243 {
244 const uint8* scan = mScanTable[0];
245 int32 run, last;
246 int32 p = 0;
247
248 do
249 {
250 int32 level = mVLCDecoder.getCoeffInterNoAsm(run, last, *mBitstream);
251 M4CHECK((level >= -2047) && (level <= 2047) && "getInterBlock: level overflow!!");
252 M4CHECK(run != -1 && "getInterBlock: invalid run");
253 p += run;
254 M4CHECK(p<64);
255 block[ scan[p] ] = (int16)level;
256 p++;
257 }
258 while(!last); // last == 0: "there are more nonzero coefficients in this block"
259 }
260
261 int32 getMv(const uint16 code)
262 {
263 int32 res;
264 int32 mv;
265 int32 scale_fac = 1 << (code - 1);
266
267 int32 codeMagnitude = getMvVlc();
268
269 if (scale_fac == 1 || codeMagnitude == 0)
270 {
271 return codeMagnitude;
272 }
273
274 // read motion vector residual
275 M4CHECK(code <= 32U);
276 res = (int32)mBitstream->getBits(code - 1);
277 mv = ((M4_ABS(codeMagnitude) - 1) * scale_fac) + res + 1;
278
279 return codeMagnitude < 0 ? -mv : mv;
280 }
281
283 {
284 return mVOLInfo.mWidth;
285 }
286
288 {
289 return mVOLInfo.mHeight;
290 }
291
292 double GetLastVopTime() const
293 {
294 return mVopTime;
295 }
296
298 {
299 return mVOLInfo;
300 }
301
303
304private:
306 void readGMCSprite();
307
309 void decodeGMCSprite();
310
312 void setMatrix(uint8* matrixOut, const uint8* matrixIn)
313 {
314 for(uint32 i=0; i<64; ++i)
315 {
316 matrixOut[i] = matrixIn[i];
317 }
318 }
319
321 uint32 log2bin(uint32 value)
322 {
323 uint32 n = 0;
324 while(value)
325 {
326 value >>= 1;
327 ++n;
328 }
329 return n;
330 }
331
333 void getMatrix(uint8* matrix)
334 {
335 uint32 last;
336 uint32 i = 0;
337 uint32 value = 0;
338 do
339 {
340 last = value;
341 value = mBitstream->getBits(8);
342 matrix[mScanTable[0][i++]] = (uint8)value;
343 }
344 while(value != 0 && i < 64);
345
346 // 'fill-up' end of matrix using last
347 i--;
348 while(i < 64)
349 {
350 matrix[mScanTable[0][i++] ] = (uint8)last;
351 }
352 }
353
354 int32 getTrajaPoint();
355 int32 getMvVlc()
356 {
357 uint32 index;
358
359 if (mBitstream->getBit())
360 {
361 return 0; // Vector difference == 0
362 }
363
364 index = mBitstream->show(12);
365
366 if (index >= 512)
367 {
368 index = (index >> 8) - 2;
369 mBitstream->skip(VLC_LEN(mVLCDecoder.mTabTMNMV0[index]));
370 return (int16)(VLC_CODE(mVLCDecoder.mTabTMNMV0[index]));
371 }
372
373 if (index >= 128)
374 {
375 index = (index >> 2) - 32;
376 mBitstream->skip(VLC_LEN(mVLCDecoder.mTabTMNMV1[index]));
377 return (int16)VLC_CODE(mVLCDecoder.mTabTMNMV1[index]);
378 }
379
380 index -= 4;
381 mBitstream->skip(VLC_LEN(mVLCDecoder.mTabTMNMV2[index]));
382 return (int16)VLC_CODE(mVLCDecoder.mTabTMNMV2[index]);
383 }
384
387
389 const M4BitstreamParser& operator=(const M4BitstreamParser& pObj);
390
391public:
394
398
401
406
408 double mVopTime;
409
412
415
418
420
422
425
428
432
433private:
434 M4Bitstream* mBitstream;
435
436 uint32 mShape;
437 uint32 mQuantBits;
438 uint32 mQuarterpel;
439 uint32 mNewPredEnable;
440
441 uint8* mScanTable[3];
442
443 bool mVOLfound;
444 uint64 mVOLCounter;
445
446 bool mLoadIntraQuant;
447 bool mLoadInterQuant;
448
449 uint8 mIntraMatrix[64];
450 uint8 mInterMatrix[64];
451
452 M4_VECTOR mSprite[4];
453
454 M4VlcDecoder mVLCDecoder;
455
456 M4_VECTOR mRefVop[4];
457
458 static uint8 mScanTableInput[3][64];
459
460 static const uint32 mIntraDCThresholdTable[8];
461 static const uint8 mDefaultIntraMatrix[64];
462 static const uint8 mDefaultInterMatrix[64];
463
464 M4Decoder* mDecoder;
465};
466
467
468
469
470
471
472
473
474
476{
477public:
480
482 void Exit(M4MemHandler& memSys);
483
485 {
486 FMemory::Memzero(mpCacheEntry->mDctFromBitstream, sizeof(mpCacheEntry->mDctFromBitstream));
487 return *mpCacheEntry;
488 }
489
490private:
491 M4BitstreamCacheEntry* mpCacheEntry;
492};
493
494
495}
496
497
GLenum GLuint GLint level
Definition AndroidOpenGLFunctions.h:46
FPlatformTypes::int16 int16
A 16-bit signed integer.
Definition Platform.h:1123
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
FPlatformTypes::uint64 uint64
A 64-bit unsigned integer.
Definition Platform.h:1117
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
void Init()
Definition LockFreeList.h:4
#define M4_ABS(X)
Definition M4BitstreamParser.h:14
#define M4_CLIP(X, A)
Definition M4BitstreamParser.h:13
#define VLC_LEN(a)
Definition M4VlcDecoder.h:16
#define VLC_CODE(a)
Definition M4VlcDecoder.h:13
uint8_t uint8
Definition binka_ue_file_header.h:8
uint16_t uint16
Definition binka_ue_file_header.h:7
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition M4BitstreamParser.h:476
M4BitstreamCacheEntry & Alloc()
Definition M4BitstreamParser.h:484
~M4BitstreamCache()
Definition M4BitstreamParser.cpp:1424
M4BitstreamCache()
Definition M4BitstreamParser.cpp:1419
Definition M4BitstreamParser.h:39
~M4BitstreamParser()
Destructor.
Definition M4BitstreamParser.cpp:133
uint16 mVopTimeIncrementBits
Definition M4BitstreamParser.h:402
uint16 getDCScaler(uint8 quant, bool luminance)
Definition M4BitstreamParser.h:117
int32 getQuantiserChange()
Definition M4BitstreamParser.h:191
VIDError init(M4Decoder *decoder, M4Bitstream *bitstream)
Perform initialization of data structures.
Definition M4BitstreamParser.cpp:148
uint8 getMBType()
Definition M4BitstreamParser.h:85
uint64 mTime
Definition M4BitstreamParser.h:413
uint16 mFcodeForward
Definition M4BitstreamParser.h:396
M4_VECTOR mSpriteDelta[2]
Definition M4BitstreamParser.h:430
uint32 mIntraDCThreshold
Definition M4BitstreamParser.h:399
uint32 mVopTimeFixedIncrement
Definition M4BitstreamParser.h:404
int16 _free
Definition M4BitstreamParser.h:427
uint64 mTimePP
Definition M4BitstreamParser.h:416
int16 GetWidth() const
Definition M4BitstreamParser.h:282
uint64 mLastNonBTime
Definition M4BitstreamParser.h:414
VIDStreamEvents::VOLInfo mVOLInfo
Definition M4BitstreamParser.h:393
uint32 mResyncMacroblockNumber
Definition M4BitstreamParser.h:419
int32 getDCDiff(uint32 dc_size)
Definition M4BitstreamParser.h:173
uint32 getCbpy(bool intra)
Definition M4BitstreamParser.h:108
const VIDStreamEvents::VOLInfo & GetVOLInfo() const
Definition M4BitstreamParser.h:297
void decodeInterBlock(int16 *block)
Definition M4BitstreamParser.h:181
int16 mSpriteWarpingAccuracy
Definition M4BitstreamParser.h:426
M4BitstreamHeaderInfo mHeaderInfo
Definition M4BitstreamParser.h:392
bool mbVopTimeFixedRate
Definition M4BitstreamParser.h:405
uint32 mLastTimeBase
Definition M4BitstreamParser.h:410
VIDError parseMPEG4ES(M4PictureType &pictureType)
Parsing of full MPEG4-ES.
Definition M4BitstreamParser.cpp:436
int32 getMv(const uint16 code)
Definition M4BitstreamParser.h:261
uint16 mFcodeBackward
Definition M4BitstreamParser.h:397
uint16 mVopTimeIncrementResolution
Definition M4BitstreamParser.h:403
void getIntraBlockNoAsm(int16 *block, uint32 direction, uint32 startCoeff)
Definition M4BitstreamParser.h:223
int32 getDCSize(bool luminance)
Definition M4BitstreamParser.h:138
uint32 mSpriteUsage
Definition M4BitstreamParser.h:421
M4_VECTOR mSpriteShift
Definition M4BitstreamParser.h:431
double mTicksPerSecond
Definition M4BitstreamParser.h:407
uint64 mTimeBP
Definition M4BitstreamParser.h:417
VIDError reset()
Attach parser to new stream.
Definition M4BitstreamParser.cpp:181
uint16 mSpriteWarpingPoints
Definition M4BitstreamParser.h:423
M4BitstreamParser()
Default constructor.
Definition M4BitstreamParser.cpp:127
M4_VECTOR mSpriteOffset[2]
Definition M4BitstreamParser.h:429
void decodeIntraBlock(int16 *block, uint32 direction, uint32 startCoeff)
Definition M4BitstreamParser.h:186
int32 getCbpCIntra()
Definition M4BitstreamParser.h:62
uint32 mScalability
Definition M4BitstreamParser.h:400
void getInterBlockNoAsm(int16 *block)
Definition M4BitstreamParser.h:242
uint32 mTimeBase
Definition M4BitstreamParser.h:411
uint16 mSpriteWarpingPointsUsed
Definition M4BitstreamParser.h:424
double mVopTime
Definition M4BitstreamParser.h:408
VIDError findNextStartCode(uint32 &absolutePos)
Scan stream for next valid startcode.
VIDError videoPacketHeader()
Definition M4BitstreamParser.cpp:1116
void initFrame(int16 width, int16 height)
Handle change/update of frame parameters.
Definition M4BitstreamParser.cpp:236
double GetLastVopTime() const
Definition M4BitstreamParser.h:292
int16 GetHeight() const
Definition M4BitstreamParser.h:287
int32 getCbpCInter()
Definition M4BitstreamParser.h:74
uint16 mLowDelay
set to '1' indicates the VOL contains NO B-VOPs!
Definition M4BitstreamParser.h:395
Definition M4Bitstream.h:22
uint32 show(const uint32 bits)
Show indicated bit from stream without moving file position.
Definition M4Bitstream.h:279
uint32 getBits(const uint32 bits)
Return indicated # of bits from stream.
Definition M4Bitstream.h:451
uint32 getBit()
Return 1 bit from stream.
Definition M4Bitstream.h:463
void skip(const uint32 bits)
Skip indicated bits in stream.
Definition M4Bitstream.h:302
Definition M4Decoder.h:30
Definition M4Memory.h:34
Definition M4VlcDecoder.h:36
int32 getCoeffInterNoAsm(int32 &run, int32 &last, M4Bitstream &bs)
Definition M4VlcDecoder.h:119
int32 getCoeffIntraNoAsm(int32 &run, int32 &last, M4Bitstream &bs)
Definition M4VlcDecoder.h:50
Definition M4Bitstream.h:9
M4PictureType
Picture type in input stream to decode.
Definition M4BitstreamParser.h:21
@ M4PIC_VOL
Definition M4BitstreamParser.h:26
@ M4PIC_B_VOP
Definition M4BitstreamParser.h:24
@ M4PIC_I_VOP
Definition M4BitstreamParser.h:22
@ M4PIC_P_VOP
Definition M4BitstreamParser.h:23
@ M4PIC_S_VOP
Definition M4BitstreamParser.h:25
int32 VIDError
Generic error type.
Definition vdecmpeg4_ErrorCodes.h:10
static UE_FORCEINLINE_HINT void * Memzero(void *Dest, SIZE_T Count)
Definition UnrealMemory.h:131
Information per macroblock filled-in by the bitstream parser.
Definition M4Global.h:54
int16 mDctFromBitstream[6 *64]
parsed idct coefficients from the bitstream (6 blocks (4:1:1) by 8x8 values)
Definition M4Global.h:57
Definition M4BitstreamHeaderInfo.h:11
Motion vector.
Definition M4Global.h:71
VideoObjectLayer.
Definition vdecmpeg4_Stream.h:63
int16 mWidth
Definition vdecmpeg4_Stream.h:64
int16 mHeight
Definition vdecmpeg4_Stream.h:65
#define M4CHECK
Definition vdecmpeg4_Types.h:8