Ticket #8607: vqa_player_cleanup.diff

File vqa_player_cleanup.diff, 7.0 KB (added by cyxx, 17 years ago)

Re-use existing bitmap decoding code

  • kyra/screen.h

     
    225225
    226226        // decoding functions
    227227        static void decodeFrame3(const uint8 *src, uint8 *dst, uint32 size);
    228         static void decodeFrame4(const uint8 *src, uint8 *dst, uint32 dstSize);
     228        static uint32 decodeFrame4(const uint8 *src, uint8 *dst, uint32 dstSize);
    229229        static void decodeFrameDelta(uint8 *dst, const uint8 *src);
    230230        static void decodeFrameDeltaPage(uint8 *dst, const uint8 *src, const int pitch, bool noXor);
    231231
  • kyra/vqa.cpp

     
    9696        return tag;
    9797}
    9898
    99 // Chunk types ending in a 'Z' are decoded using this function.
    100 
    101 int VQAMovie::decodeFormat80(byte *inbuf, byte *outbuf) {
    102         byte *src = inbuf;
    103         byte *dst = outbuf;
    104 
    105         while (1) {
    106                 int relPos, pos;
    107                 int count;
    108                 byte color;
    109                 int i;
    110 
    111                 byte command = *src++;
    112 
    113                 switch (command) {
    114                 case 0x80:
    115                         return dst - outbuf;
    116 
    117                 case 0xFF:
    118                         /* 11111111 <count> <pos> */
    119                         count = src[0] | (src[1] << 8);
    120                         pos = src[2] | (src[3] << 8);
    121                         src += 4;
    122                         for (i = 0; i < count; i++)
    123                                 dst[i] = outbuf[i + pos];
    124                         break;
    125 
    126                 case 0xFE:
    127                         /* 11111110 <count> <color> */
    128                         count = src[0] | (src[1] << 8);
    129                         color = src[2];
    130                         src += 3;
    131                         memset(dst, color, count);
    132                         break;
    133 
    134                 default:
    135                         if (command >= 0xC0) {
    136                                 /* 11 <count - 3> <pos> */
    137                                 count = (command & 0x3F) + 3;
    138                                 pos = src[0] | (src[1] << 8);
    139                                 src += 2;
    140                                 for (i = 0; i < count; i++)
    141                                         dst[i] = outbuf[pos + i];
    142                         } else if (command >= 0x80) {
    143                                 /* 10 <count> */
    144                                 count = command & 0x3F;
    145                                 memcpy(dst, src, count);
    146                                 src += count;
    147                         } else {
    148                                 /* 0 <count - 3> <relative pos> */
    149                                 count = ((command & 0x70) >> 4) + 3;
    150                                 relPos = ((command & 0x0F) << 8) | src[0];
    151                                 src++;
    152                                 for (i = 0; i < count; i++)
    153                                         dst[i] = dst[i - relPos];
    154                         }
    155                         break;
    156                 }
    157 
    158                 dst += count;
    159         }
    160 }
    161 
    162 inline int16 clip8BitSample(int16 sample) {
    163         if (sample > 255)
    164                 return 255;
    165         if (sample < 0)
    166                 return 0;
    167         return sample;
    168 }
    169 
    17099void VQAMovie::decodeSND1(byte *inbuf, uint32 insize, byte *outbuf, uint32 outsize) {
    171100        const int8 WSTable2Bit[] = { -2, -1, 0, 1 };
    172101        const int8 WSTable4Bit[] = {
     
    206135                                code = *inbuf++;
    207136
    208137                                curSample += WSTable4Bit[code & 0x0f];
    209                                 curSample = clip8BitSample(curSample);
     138                                curSample = CLIP<int16>(curSample, 0, 255);
    210139                                *outbuf++ = curSample;
    211140
    212141                                curSample += WSTable4Bit[code >> 4];
    213                                 curSample = clip8BitSample(curSample);
     142                                curSample = CLIP<int16>(curSample, 0, 255);
    214143                                *outbuf++ = curSample;
    215144
    216145                                outsize -= 2;
     
    221150                                code = *inbuf++;
    222151
    223152                                curSample += WSTable2Bit[code & 0x03];
    224                                 curSample = clip8BitSample(curSample);
     153                                curSample = CLIP<int16>(curSample, 0, 255);
    225154                                *outbuf++ = curSample;
    226155
    227156                                curSample += WSTable2Bit[(code >> 2) & 0x03];
    228                                 curSample = clip8BitSample(curSample);
     157                                curSample = CLIP<int16>(curSample, 0, 255);
    229158                                *outbuf++ = curSample;
    230159
    231160                                curSample += WSTable2Bit[(code >> 4) & 0x03];
    232                                 curSample = clip8BitSample(curSample);
     161                                curSample = CLIP<int16>(curSample, 0, 255);
    233162                                *outbuf++ = curSample;
    234163
    235164                                curSample += WSTable2Bit[(code >> 6) & 0x03];
    236                                 curSample = clip8BitSample(curSample);
     165                                curSample = CLIP<int16>(curSample, 0, 255);
    237166                                *outbuf++ = curSample;
    238167
    239168                                outsize -= 4;
     
    325254                        _frameInfo = new uint32[_header.numFrames];
    326255                        _frame = new byte[_header.width * _header.height];
    327256
    328                         size = 0xf00 * _header.blockW * _header.blockH;
    329                         _codeBook = new byte[size];
    330                         _partialCodeBook = new byte[size];
    331                         memset(_codeBook, 0, size);
    332                         memset(_partialCodeBook, 0, size);
     257                        _codeBookSize = 0xf00 * _header.blockW * _header.blockH;
     258                        _codeBook = new byte[_codeBookSize];
     259                        _partialCodeBook = new byte[_codeBookSize];
     260                        memset(_codeBook, 0, _codeBookSize);
     261                        memset(_partialCodeBook, 0, _codeBookSize);
    333262
    334263                        _numVectorPointers = (_header.width / _header.blockW) * (_header.height * _header.blockH);
    335264                        _vectorPointers = new uint16[_numVectorPointers];
     
    427356
    428357                _frameInfo = NULL;
    429358                _frame = NULL;
     359                _codeBookSize = 0;
    430360                _codeBook = NULL;
    431361                _partialCodeBook = NULL;
    432362                _vectorPointers = NULL;
     
    513443                                case MKID_BE('CBFZ'):   // Full codebook
    514444                                        inbuf = (byte *)allocBuffer(0, size);
    515445                                        _file.read(inbuf, size);
    516                                         decodeFormat80(inbuf, _codeBook);
     446                                        Screen::decodeFrame4(inbuf, _codeBook, _codeBookSize);
    517447                                        break;
    518448
    519449                                case MKID_BE('CBP0'):   // Partial codebook
     
    538468                                case MKID_BE('CPLZ'):   // Palette
    539469                                        inbuf = (byte *)allocBuffer(0, size);
    540470                                        _file.read(inbuf, size);
    541                                         size = decodeFormat80(inbuf, _vm->screen()->_currentPalette);
     471                                        Screen::decodeFrame4(inbuf, _vm->screen()->_currentPalette, 768);
    542472                                        break;
    543473
    544474                                case MKID_BE('VPT0'):   // Frame data
     
    553483                                        outbuf = (byte *)allocBuffer(1, 2 * _numVectorPointers);
    554484
    555485                                        _file.read(inbuf, size);
    556                                         size = decodeFormat80(inbuf, outbuf);
     486                                        size = Screen::decodeFrame4(inbuf, outbuf, 2 * _numVectorPointers);
    557487
    558488                                        assert(size / 2 <= _numVectorPointers);
    559489
     
    614544
    615545        if (_numPartialCodeBooks == _header.cbParts) {
    616546                if (_compressedCodeBook) {
    617                         decodeFormat80(_partialCodeBook, _codeBook);
     547                        Screen::decodeFrame4(_partialCodeBook, _codeBook, _codeBookSize);
    618548                } else {
    619549                        memcpy(_codeBook, _partialCodeBook, _partialCodeBookSize);
    620550                }
     
    633563
    634564        startTick = _system->getMillis();
    635565
    636         // First, handle any sound chunk that apears before the first frame.
     566        // First, handle any sound chunk that appears before the first frame.
    637567        // At least in some versions, it will contain half a second of audio,
    638568        // presumably to lessen the risk of audio underflow.
    639569        //
  • kyra/screen.cpp

     
    15471547        }
    15481548}
    15491549
    1550 void Screen::decodeFrame4(const uint8 *src, uint8 *dst, uint32 dstSize) {
     1550uint32 Screen::decodeFrame4(const uint8 *src, uint8 *dst, uint32 dstSize) {
    15511551        debugC(9, kDebugLevelScreen, "Screen::decodeFrame4(%p, %p, %d)", (const void *)src, (const void *)dst, dstSize);
    15521552        uint8 *dstOrig = dst;
    15531553        uint8 *dstEnd = dst + dstSize;
     
    15951595                        break;
    15961596                }
    15971597        }
     1598        return dst - dstOrig;
    15981599}
    15991600
    16001601void Screen::decodeFrameDelta(uint8 *dst, const uint8 *src) {
  • kyra/vqa.h

     
    9191        void *allocBuffer(int num, uint32 size);
    9292        void freeBuffers();
    9393
    94         int decodeFormat80(byte *inbuf, byte *outbuf);
    9594        void decodeSND1(byte *inbuf, uint32 insize, byte *outbuf, uint32 outsize);
    9695
    9796        void displayFrame(uint frameNum);
     
    10099
    101100        VQAHeader _header;
    102101        uint32 *_frameInfo;
     102        uint32 _codeBookSize;
    103103        byte *_codeBook;
    104104        byte *_partialCodeBook;
    105105        bool _compressedCodeBook;