ScummVM API documentation
indeo.h
1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program. If not, see <http://www.gnu.org/licenses/>.
19  *
20  */
21 
22 #include "common/scummsys.h"
23 #include "graphics/surface.h"
24 #include "image/codecs/codec.h"
25 
26 /* Common structures, macros, and base class shared by both Indeo4 and
27  * Indeo5 decoders, derived from ffmpeg. We don't currently support Indeo5
28  * decoding, but just in case we eventually need it, this is kept as a separate
29  * file like it is in ffmpeg.
30  *
31  * Original copyright note: * Intel Indeo 4 (IV41, IV42, etc.) video decoder for ffmpeg
32  * written, produced, and directed by Alan Smithee
33  */
34 
35 #ifndef IMAGE_CODECS_INDEO_INDEO_H
36 #define IMAGE_CODECS_INDEO_INDEO_H
37 
38 #include "image/codecs/indeo/get_bits.h"
39 #include "image/codecs/indeo/vlc.h"
40 
41 namespace Image {
42 namespace Indeo {
43 
47 enum {
48  IVI4_FRAMETYPE_INTRA = 0,
49  IVI4_FRAMETYPE_INTRA1 = 1,
50  IVI4_FRAMETYPE_INTER = 2,
51  IVI4_FRAMETYPE_BIDIR = 3,
52  IVI4_FRAMETYPE_INTER_NOREF = 4,
53  IVI4_FRAMETYPE_NULL_FIRST = 5,
54  IVI4_FRAMETYPE_NULL_LAST = 6
55 };
56 
57 enum {
58  IVI_MB_HUFF = 0,
59  IVI_BLK_HUFF = 1
60 };
61 
65 typedef void (InvTransformPtr)(const int32 *in, int16 *out, uint32 pitch, const uint8 *flags);
66 typedef void (DCTransformPtr)(const int32 *in, int16 *out, uint32 pitch, int blkSize);
67 
68 typedef void (*IviMCFunc)(int16 *buf, const int16 *refBuf, uint32 pitch, int mcType);
69 typedef void (*IviMCAvgFunc)(int16 *buf, const int16 *refBuf1, const int16 *refBuf2,
70  uint32 pitch, int mcType, int mcType2);
71 
73 #define IVI_VLC_BITS 13
74 #define IVI5_IS_PROTECTED 0x20
75 
79 #define IVI_TOSIGNED(val) (-(((val) >> 1) ^ -((val) & 1)))
80 
84 #define IVI_MBs_PER_TILE(tileWidth, tileHeight, mbSize) \
85  ((((tileWidth) + (mbSize) - 1) / (mbSize)) * (((tileHeight) + (mbSize) - 1) / (mbSize)))
86 
90 struct IVIHuffDesc {
91  int32 _numRows;
92  uint8 _xBits[16];
93 
102  int createHuffFromDesc(VLC *vlc, bool flag) const;
103 
110  bool huffDescCompare(const IVIHuffDesc *desc2) const;
111 
117  void huffDescCopy(const IVIHuffDesc *src);
118 };
119 
120 struct IVI45DecContext;
121 
125 struct IVIHuffTab {
126 public:
127  int32 _tabSel;
128  VLC * _tab;
130 
134 
138  IVIHuffTab();
139 
149  int decodeHuffDesc(IVI45DecContext *ctx, int descCoded, int whichTab);
150 };
151 
155 struct RVMapDesc {
156  uint8 _eobSym;
157  uint8 _escSym;
158  uint8 _runtab[256];
159  int8 _valtab[256];
160 };
161 
165 struct IVIMbInfo {
166  int16 _xPos;
167  int16 _yPos;
168  uint32 _bufOffs;
169  uint8 _type;
170  uint8 _cbp;
171  int8 _qDelta;
172  int8 _mvX;
173  int8 _mvY;
174  int8 _bMvX;
175  int8 _bMvY;
176 
177  IVIMbInfo();
178 };
179 
183 struct IVITile {
184  int _xPos;
185  int _yPos;
186  int _width;
187  int _height;
188  int _mbSize;
189  bool _isEmpty;
190  int _dataSize;
191  int _numMBs;
194 
195  IVITile();
196 };
197 
201 struct IVIBandDesc {
202  int _plane;
203  int _bandNum;
204  int _width;
205  int _height;
206  int _aHeight;
207  const uint8 * _dataPtr;
208  int _dataSize;
209  int16 * _buf;
210  int16 * _refBuf;
211  int16 * _bRefBuf;
212  int16 * _bufs[4];
213  int _pitch;
214  bool _isEmpty;
215  int _mbSize;
216  int _blkSize;
217  uint8 _isHalfpel;
218  bool _inheritMv;
221  int _quantMat;
223  const uint8 * _scan;
224  int _scanSize;
225 
227 
228  int _numCorr;
229  uint8 _corr[61 * 2];
230  int _rvmapSel;
232  int _numTiles;
234  InvTransformPtr *_invTransform;
235  int _transformSize;
236  DCTransformPtr *_dcTransform;
237  bool _is2dTrans;
238  int32 _checksum;
239  int _checksumPresent;
240  int _bufSize;
241  const uint16 * _intraBase;
242  const uint16 * _interBase;
243  const uint8 * _intraScale;
244  const uint8 * _interScale;
245 
246  IVIBandDesc();
247 
248  int initTiles(IVITile *refTile, int p, int b, int tHeight, int tWidth);
249 };
250 
251 struct IVIPicConfig {
252  uint16 _picWidth;
253  uint16 _picHeight;
254  uint16 _chromaWidth;
255  uint16 _chromaHeight;
256  uint16 _tileWidth;
257  uint16 _tileHeight;
258  uint8 _lumaBands;
259  uint8 _chromaBands;
260 
261  IVIPicConfig();
262 
266  bool ivi_pic_config_cmp(const IVIPicConfig &cfg2);
267 };
268 
272 struct IVIPlaneDesc {
273  uint16 _width;
274  uint16 _height;
275  uint8 _numBands;
277 
278  IVIPlaneDesc();
279 
280  static int initPlanes(IVIPlaneDesc *planes, const IVIPicConfig *cfg, bool isIndeo4);
281 
282  static int initTiles(IVIPlaneDesc *planes, int tileWidth, int tileHeight);
283 
289  static void freeBuffers(IVIPlaneDesc *planes);
290 
300  static int checkImageSize(unsigned int w, unsigned int h, int logOffset);
301 };
302 
303 struct AVFrame {
307  int _width, _height;
308 
309 #define AV_NUM_DATA_POINTERS 3
310 
322  uint8 *_data[AV_NUM_DATA_POINTERS];
323 
339  int _linesize[AV_NUM_DATA_POINTERS];
340 
344  AVFrame();
345 
349  ~AVFrame() { freeFrame(); }
350 
354  int setDimensions(uint16 width, uint16 height);
355 
359  int getBuffer(int flags);
360 
364  void freeFrame();
365 };
366 
368  friend struct IVIHuffTab;
369 private:
370  VLC_TYPE _tableData[8192 * 16][2];
371  VLC _iviMbVlcTabs[8];
372  VLC _iviBlkVlcTabs[8];
373 public:
374  GetBits * _gb;
375  RVMapDesc _rvmapTabs[9];
376 
377  uint32 _frameNum;
378  int _frameType;
380  uint32 _dataSize;
381  int _isScalable;
382  const uint8 * _frameData;
384  uint32 _frameSize;
385  uint32 _picHdrSize;
386  uint8 _frameFlags;
387  uint16 _checksum;
388 
389  IVIPicConfig _picConf;
390  IVIPlaneDesc _planes[3];
391 
393  int _dstBuf;
394  int _refBuf;
395  int _ref2Buf;
396  int _bRefBuf;
397 
401 
402  uint8 _rvmapSel;
403  bool _inImf;
404  bool _inQ;
405  uint8 _picGlobQuant;
406  uint8 _unknown1;
407 
408  uint16 _gopHdrSize;
409  uint8 _gopFlags;
410  uint32 _lockWord;
411 
412  bool _hasBFrames;
413  bool _hasTransp;
414  bool _usesTiling;
415  bool _usesHaar;
416  bool _usesFullpel;
417 
418  bool _gopInvalid;
419  int _bufInvalid[4];
420 
421  bool _isIndeo4;
422  uint32 _transKeyColor;
423 
424  AVFrame * _pFrame;
425  bool _gotPFrame;
426 
427  IVI45DecContext();
428 private:
432  static const RVMapDesc _ff_ivi_rvmap_tabs[9];
433 };
434 
435 class IndeoDecoderBase : public Codec {
436 private:
443  int decode_band(IVIBandDesc *band);
444 
452  void recomposeHaar(const IVIPlaneDesc *plane, uint8 *dst, const int dstPitch);
453 
461  void recompose53(const IVIPlaneDesc *plane, uint8 *dst, const int dstPitch);
462 
463  /*
464  * Convert and output the current plane.
465  * This conversion is done by adding back the bias value of 128
466  * (subtracted in the encoder) and clipping the result.
467  *
468  * @param[in] plane Pointer to the descriptor of the plane being processed
469  * @param[out] dst Pointer to the buffer receiving converted pixels
470  * @param[in] dstPitch Pitch for moving to the next y line
471  */
472  void outputPlane(IVIPlaneDesc *plane, uint8 *dst, int dstPitch);
473 
482  int processEmptyTile(IVIBandDesc *band, IVITile *tile, int32 mvScale);
483 
484  /*
485  * Decode size of the tile data.
486  * The size is stored as a variable-length field having the following format:
487  * if (tile_data_size < 255) than this field is only one byte long
488  * if (tile_data_size >= 255) than this field four is byte long: 0xFF X1 X2 X3
489  * where X1-X3 is size of the tile data
490  *
491  * @param[in,out] gb the GetBit context
492  * @returns Size of the tile data in bytes
493  */
494  int decodeTileDataSize(GetBits *gb);
495 
496  /*
497  * Decode block data:
498  * extract huffman-coded transform coefficients from the bitstream,
499  * dequantize them, apply inverse transform and motion compensation
500  * in order to reconstruct the picture.
501  *
502  * @param[in,out] gb The GetBit context
503  * @param[in] band Pointer to the band descriptor
504  * @param[in] tile Pointer to the tile descriptor
505  * @returns Result code: 0 - OK, -1 = error (corrupted blocks data)
506  */
507  int decodeBlocks(GetBits *gb, IVIBandDesc *band, IVITile *tile);
508 
509  int iviMc(IVIBandDesc *band, IviMCFunc mc, IviMCAvgFunc mcAvg,
510  int offs, int mvX, int mvY, int mvX2, int mvY2, int mcType, int mcType2);
511 
512  int decodeCodedBlocks(GetBits *gb, IVIBandDesc *band,
513  IviMCFunc mc, IviMCAvgFunc mcAvg, int mvX, int mvY,
514  int mvX2, int mvY2, int32 *prevDc, int isIntra,
515  int mcType, int mcType2, uint32 quant, int offs);
516 
517  int iviDcTransform(IVIBandDesc *band, int32 *prevDc, int bufOffs,
518  int blkSize);
519 protected:
520  IVI45DecContext _ctx;
521  uint16 _width;
522  uint16 _height;
523  uint _bitsPerPixel;
524  Graphics::PixelFormat _pixelFormat;
525  Graphics::Surface *_surface;
526 
530  static const uint8 _ffIviVerticalScan8x8[64];
531  static const uint8 _ffIviHorizontalScan8x8[64];
532  static const uint8 _ffIviDirectScan4x4[16];
533 protected:
537  Graphics::PixelFormat getPixelFormat() const override { return _pixelFormat; }
538 
543  bool setOutputPixelFormat(const Graphics::PixelFormat &format) override { _pixelFormat = format; return true; }
544 
549  virtual int decodePictureHeader() = 0;
550 
554  virtual void switchBuffers() = 0;
555 
556  virtual bool isNonNullFrame() const = 0;
557 
564  virtual int decodeBandHeader(IVIBandDesc *band) = 0;
565 
574  virtual int decodeMbInfo(IVIBandDesc *band, IVITile *tile) = 0;
575 
579  virtual int decodeTransparency() { return -1; }
580 
585  int decodeIndeoFrame();
586 
590  int scaleMV(int mv, int mvScale);
591 public:
592  IndeoDecoderBase(uint16 width, uint16 height, uint bitsPerPixel);
593  ~IndeoDecoderBase() override;
594 };
595 
596 } // End of namespace Indeo
597 } // End of namespace Image
598 
599 #endif
IVIHuffTab _blkVlc
current block table descriptor
Definition: indeo.h:399
int8 _bMvX
second motion vector (x component)
Definition: indeo.h:174
int _dataSize
size of the band data
Definition: indeo.h:208
bool _inheritMv
tells if motion vector is inherited from reference macroblock
Definition: indeo.h:218
int _pitch
_pitch associated with the buffers above
Definition: indeo.h:213
Definition: indeo.h:251
const uint8 * _scan
ptr to the scan pattern
Definition: indeo.h:223
const uint8 * _frameData
input frame data pointer
Definition: indeo.h:382
int _globQuant
quant base for this band
Definition: indeo.h:222
uint8 _escSym
escape symbol
Definition: indeo.h:157
int _ref2Buf
temporal storage for switching buffers
Definition: indeo.h:395
int16 * _buf
pointer to the output buffer for this band
Definition: indeo.h:209
uint32 _bufOffs
address in the output buffer for this mb
Definition: indeo.h:168
Definition: surface.h:67
bool huffDescCompare(const IVIHuffDesc *desc2) const
Definition: vlc.h:56
int8 _bMvY
second motion vector (y component)
Definition: indeo.h:175
uint16 _checksum
frame _checksum
Definition: indeo.h:387
IVIHuffDesc _custDesc
pointer to the table associated with tab_sel
Definition: indeo.h:132
Definition: pixelformat.h:138
IVIMbInfo * _mbs
array of macroblock descriptors
Definition: indeo.h:192
int _prevFrameType
frame type of the previous frame
Definition: indeo.h:379
uint32 _dataSize
size of the frame data in bytes from picture header
Definition: indeo.h:380
Definition: indeo.h:272
const uint16 * _intraBase
quantization matrix for intra blocks
Definition: indeo.h:241
bool _inQ
flag for explicitly stored quantiser delta
Definition: indeo.h:404
RVMapDesc * _rvMap
ptr to the RLE table for this band
Definition: indeo.h:231
uint8 _type
macroblock type: 0 - INTRA, 1 - INTER
Definition: indeo.h:169
int _numMBs
number of macroblocks in this tile
Definition: indeo.h:191
int _bufSwitch
used to switch between three buffers
Definition: indeo.h:392
Definition: indeo.h:303
int16 * _bRefBuf
pointer to the second reference frame buffer (for motion compensation)
Definition: indeo.h:211
bool _inheritQDelta
tells if quantiser delta is inherited from reference macroblock
Definition: indeo.h:219
IVIHuffTab _transVlc
current transparency table descriptor
Definition: indeo.h:400
virtual int decodeTransparency()
Definition: indeo.h:579
IVIMbInfo * _refMbs
ptr to the macroblock descriptors of the reference tile
Definition: indeo.h:193
uint32 _picHdrSize
picture header size in bytes
Definition: indeo.h:385
const uint8 * _dataPtr
ptr to the first byte of the band data
Definition: indeo.h:207
Definition: codec.h:55
int16 * _refBuf
pointer to the reference frame buffer (for motion compensation)
Definition: indeo.h:210
int _aHeight
aligned band height
Definition: indeo.h:206
int _rvmapSel
rvmap table selector
Definition: indeo.h:230
int _numTiles
number of tiles in this band
Definition: indeo.h:232
int _bRefBuf
second reference frame buffer index
Definition: indeo.h:396
int _bandNum
band number
Definition: indeo.h:203
uint8 _eobSym
end of block symbol
Definition: indeo.h:156
int _bufSize
band buffer size in bytes
Definition: indeo.h:240
VLC _custTab
custom Huffman codebook descriptor
Definition: indeo.h:133
int8 _qDelta
quant delta
Definition: indeo.h:171
Definition: indeo.h:165
int createHuffFromDesc(VLC *vlc, bool flag) const
int32 _checksum
for debug purposes
Definition: indeo.h:238
Definition: indeo.h:367
uint8 _cbp
coded block pattern
Definition: indeo.h:170
const uint8 * _interScale
quantization coefficient for inter blocks
Definition: indeo.h:244
int _mbSize
macroblock size
Definition: indeo.h:215
Definition: indeo.h:201
IVIHuffTab _mbVlc
current macroblock table descriptor
Definition: indeo.h:398
int _quantMat
dequant matrix index
Definition: indeo.h:221
const uint8 * _intraScale
quantization coefficient for intra blocks
Definition: indeo.h:243
int _blkSize
block size
Definition: indeo.h:216
int _numCorr
number of correction entries
Definition: indeo.h:228
int _dataSize
size of the data in bytes
Definition: indeo.h:190
int8 _mvX
motion vector (x component)
Definition: indeo.h:172
bool setOutputPixelFormat(const Graphics::PixelFormat &format) override
Definition: indeo.h:543
Definition: indeo.h:435
~AVFrame()
Definition: indeo.h:349
Graphics::PixelFormat getPixelFormat() const override
Definition: indeo.h:537
bool _hasTransp
transparency mode enabled
Definition: indeo.h:413
int8 _mvY
motion vector (y component)
Definition: indeo.h:173
IVITile * _tiles
array of tile descriptors
Definition: indeo.h:233
int _width
Definition: indeo.h:307
uint8 _numBands
number of bands this plane subdivided into
Definition: indeo.h:275
IVIBandDesc * _bands
array of band descriptors
Definition: indeo.h:276
IVIHuffTab _blkVlc
vlc table for decoding block data
Definition: indeo.h:226
int _interScal
signals a sequence of scalable inter frames
Definition: indeo.h:383
const uint16 * _interBase
quantization matrix for inter blocks
Definition: indeo.h:242
int _dstBuf
buffer index for the currently decoded frame
Definition: indeo.h:393
Definition: get_bits.h:33
Definition: movie_decoder.h:32
void huffDescCopy(const IVIHuffDesc *src)
uint32 _frameSize
frame size in bytes
Definition: indeo.h:384
Definition: indeo.h:183
Definition: indeo.h:155
int _scanSize
size of the scantable
Definition: indeo.h:224
uint8 _isHalfpel
precision of the motion compensation: 0 - fullpel, 1 - halfpel
Definition: indeo.h:217
int _plane
plane number this band belongs to
Definition: indeo.h:202
bool _qdeltaPresent
tells if Qdelta signal is present in the bitstream (Indeo5 only)
Definition: indeo.h:220
int _refBuf
inter frame reference buffer index
Definition: indeo.h:394
Definition: indeo.h:90
Definition: indeo.h:125