Get rid of most system headers.
[gemrb.git] / gemrb / plugins / BIKPlayer / BIKPlayer.cpp
blob4eadd78e05d36f8f67e0b86b706e934735b0c8a3
1 /* GemRB - Infinity Engine Emulator
2 * Copyright (C) 2009 The GemRB Project
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 * code derived from Bink Audio decoder
23 * Copyright (c) 2007-2009 Peter Ross (pross@xvid.org)
24 * Copyright (c) 2009 Daniel Verkamp (daniel@drv.nu)
26 * code derived from Bink video decoder
27 * Copyright (c) 2009 Konstantin Shishkov
30 #include "BIKPlayer.h"
32 #include "rational.h"
33 #include "binkdata.h"
35 #include "ie_types.h"
37 #include "Audio.h"
38 #include "Variables.h"
39 #include "Video.h"
41 #include <cassert>
42 #include <cstdio>
44 static int g_truecolor;
45 static ieDword *cbAtFrame = NULL;
46 static ieDword *strRef = NULL;
48 static const int ff_wma_critical_freqs[25] = {
49 100, 200, 300, 400, 510, 630, 770, 920,
50 1080, 1270, 1480, 1720, 2000, 2320, 2700, 3150,
51 3700, 4400, 5300, 6400, 7700, 9500, 12000, 15500,
52 24500,
55 static uint8_t ff_cropTbl[256 + 2 * MAX_NEG_CROP] = {0, };
57 BIKPlayer::BIKPlayer(void)
59 int i;
61 video = core->GetVideoDriver();
62 inbuff = NULL;
63 maxRow = 0;
64 rowCount = 0;
65 frameCount = 0;
66 //force initialisation of static tables
67 memset(bink_trees, 0, sizeof(bink_trees));
68 memset(table, 0, sizeof(table));
70 for(i=0;i<256;i++) ff_cropTbl[i + MAX_NEG_CROP] = i;
71 for(i=0;i<MAX_NEG_CROP;i++) {
72 ff_cropTbl[i] = 0;
73 ff_cropTbl[i + MAX_NEG_CROP + 256] = 255;
77 BIKPlayer::~BIKPlayer(void)
79 av_freep((void **) &inbuff);
82 void BIKPlayer::av_set_pts_info(AVRational &time_base, unsigned int pts_num, unsigned int pts_den)
84 //pts_wrap_bits, if needed, is always 64
85 if(av_reduce(time_base.num, time_base.den, pts_num, pts_den, INT_MAX)) {
86 //bla bla, something didn't work
89 if(!time_base.num || !time_base.den)
90 time_base.num = time_base.den = 0;
93 int BIKPlayer::ReadHeader()
95 str->Seek(0,GEM_STREAM_START);
96 str->Read( header.signature, BIK_SIGNATURE_LEN );
97 str->ReadDword(&header.filesize);
98 header.filesize += 8;
99 str->ReadDword(&header.framecount);
101 if (header.framecount > 1000000) {
102 return -1;
105 str->ReadDword(&header.maxframesize);
106 if (header.maxframesize > header.filesize) {
107 return -1;
110 str->Seek(4,GEM_CURRENT_POS);
112 str->ReadDword(&header.width);
113 str->ReadDword(&header.height);
115 ieDword fps_num, fps_den;
117 str->ReadDword(&fps_num);
118 str->ReadDword(&fps_den);
120 if (fps_num == 0 || fps_den == 0) {
121 //av_log(s, AV_LOG_ERROR, "invalid header: invalid fps (%d/%d)\n", fps_num, fps_den);
122 return -1;
124 //also sets pts_wrap_bits to 64
125 av_set_pts_info(v_timebase, fps_den, fps_num);
127 str->Seek(4,GEM_CURRENT_POS);
128 str->ReadDword(&header.tracks);
130 //we handle only single tracks, is this a problem with multi language iwd2?
131 if (header.tracks > 1) {
132 return -1;
135 if (header.tracks) {
136 str->Seek(4 * header.tracks,GEM_CURRENT_POS);
137 //make sure we use one track, if more needed, rewrite this part
138 assert(header.tracks==1);
140 str->ReadWord(&header.samplerate);
141 //also sets pts_wrap_bits to 64
142 //av_set_pts_info(s_timebase, 1, header.samplerate); //unused, we simply use header.samplerate
143 str->ReadWord(&header.audioflag);
145 str->Seek(4 * header.tracks,GEM_CURRENT_POS);
148 /* build frame index table */
149 ieDword pos, next_pos;
150 int keyframe;
152 str->ReadDword(&pos);
153 keyframe = pos & 1;
154 pos &= ~1;
156 frames.reserve(header.framecount);
157 for (unsigned int i = 0; i < header.framecount; i++) {
158 if (i == header.framecount - 1) {
159 next_pos = header.filesize;
160 } else {
161 str->ReadDword(&next_pos);
163 if (next_pos <= pos) {
164 // av_log(s, AV_LOG_ERROR, "invalid frame index table\n");
165 return -1;
167 //offset, size, keyframe
168 binkframe frame;
170 //the order of instructions is important here!
171 frame.pos=pos;
172 frame.keyframe=keyframe;
173 pos = next_pos&~1;
174 keyframe = next_pos&1;
175 frame.size=pos-frame.pos;
176 //sanity hack, we might as well just go belly up and refuse playing
177 if (frame.size>header.maxframesize) {
178 frame.size = header.maxframesize;
181 frames.push_back(frame);
184 inbuff = (ieByte *) av_malloc(header.maxframesize);
185 if (!inbuff) {
186 return -2;
189 str->Seek(4, GEM_CURRENT_POS);
190 return 0;
193 bool BIKPlayer::Open(DataStream* stream)
195 validVideo = false;
196 str = stream;
198 str->Read( &header.signature, BIK_SIGNATURE_LEN );
199 if (memcmp( header.signature, BIK_SIGNATURE_DATA, 4 ) == 0) {
200 validVideo = ReadHeader()==0;
201 return validVideo;
203 return false;
206 void BIKPlayer::CallBackAtFrames(ieDword cnt, ieDword *arg, ieDword *arg2 )
208 maxRow = cnt;
209 frameCount = 0;
210 rowCount = 0;
211 cbAtFrame = arg;
212 strRef = arg2;
215 int BIKPlayer::Play()
217 if (!validVideo) {
218 return 0;
220 //Start Movie Playback
221 frameCount = 0;
222 int ret = doPlay( );
224 EndAudio();
225 EndVideo();
226 av_freep((void **) &inbuff);
227 return ret;
230 //this code could be in the movieplayer parent class
231 void get_current_time(long &sec, long &usec) {
232 #ifdef _WIN32
233 DWORD time;
234 time = GetTickCount();
236 sec = time / 1000;
237 usec = (time % 1000) * 1000;
238 #else
239 struct timeval tv;
240 gettimeofday(&tv, NULL);
242 sec = tv.tv_sec;
243 usec = tv.tv_usec;
244 #endif
247 void BIKPlayer::timer_start()
249 get_current_time(timer_last_sec, timer_last_usec);
252 void BIKPlayer::timer_wait()
254 long sec, usec;
255 get_current_time(sec, usec);
257 while (sec > timer_last_sec) {
258 usec += 1000000;
259 timer_last_sec++;
262 //quick hack, we should rather use the rational time base as ffmpeg
263 frame_wait = v_timebase.num*1000000/v_timebase.den;
264 while (usec - timer_last_usec > (long)frame_wait) {
265 usec -= frame_wait;
266 video_frameskip++;
269 long to_sleep = frame_wait - (usec - timer_last_usec);
270 #ifdef _WIN32
271 Sleep(to_sleep / 1000);
272 #else
273 usleep(to_sleep);
274 #endif
276 timer_start();
279 bool BIKPlayer::next_frame()
281 if (timer_last_sec) {
282 timer_wait();
284 if(frameCount>=header.framecount) {
285 return false;
287 binkframe frame = frames[frameCount++];
288 //frame.size = fileRead( frame.pos, inbuff, frame.size);
289 //ieDword audframesize = *(ieDword *) inbuff;
290 str->Seek(frame.pos, GEM_STREAM_START);
291 ieDword audframesize;
292 str->ReadDword(&audframesize);
293 frame.size = str->Read( inbuff, frame.size - 4 );
294 if (DecodeAudioFrame(inbuff, audframesize)) {
295 //buggy frame, we stop immediately
296 //return false;
298 if (DecodeVideoFrame(inbuff+audframesize, frame.size-audframesize)) {
299 //buggy frame, we stop immediately
300 return false;
302 if (!timer_last_sec) {
303 timer_start();
305 return true;
308 int BIKPlayer::doPlay()
310 int done = 0;
312 //bink is always truecolor
313 g_truecolor = 1;
315 frame_wait = 0;
316 timer_last_sec = 0;
317 video_frameskip = 0;
319 if (sound_init( core->GetAudioDrv()->CanPlay())) {
320 //sound couldn't be initialized
321 return 1;
324 //last parameter is to enable YUV overlay
325 outputwidth = (int) header.width;
326 outputheight= (int) header.height;
327 video->InitMovieScreen(outputwidth,outputheight, true);
329 if (video_init(outputwidth,outputheight)) {
330 return 2;
333 while (!done && next_frame()) {
334 done = video->PollMovieEvents();
337 return 0;
340 unsigned int BIKPlayer::fileRead(unsigned int pos, void* buf, unsigned int count)
342 str->Seek(pos, GEM_STREAM_START);
343 return str->Read( buf, count );
346 void BIKPlayer::showFrame(unsigned char** buf, unsigned int *strides, unsigned int bufw,
347 unsigned int bufh, unsigned int w, unsigned int h, unsigned int dstx, unsigned int dsty)
349 ieDword titleref = 0;
351 if (cbAtFrame && strRef) {
352 frameCount ++;
353 if ((rowCount<maxRow) && (frameCount >= cbAtFrame[rowCount]) ) {
354 rowCount++;
356 //draw subtitle here
357 if (rowCount) {
358 titleref = strRef[rowCount-1];
361 video->showYUVFrame(buf,strides,bufw,bufh,w,h,dstx,dsty, titleref);
364 int BIKPlayer::setAudioStream()
366 ieDword volume;
367 core->GetDictionary()->Lookup( "Volume Movie", volume) ;
368 int source = core->GetAudioDrv()->SetupNewStream(0, 0, 0, volume, false, false);
369 return source;
372 void BIKPlayer::freeAudioStream(int stream)
374 if (stream > -1)
375 core->GetAudioDrv()->ReleaseStream(stream, true);
378 void BIKPlayer::queueBuffer(int stream, unsigned short bits, int channels, short* memory, int size, int samplerate)
380 if (stream > -1)
381 core->GetAudioDrv()->QueueBuffer(stream, bits, channels, memory, size, samplerate);
386 * @file libavcodec/binkaudio.c
388 * Technical details here:
389 * http://wiki.multimedia.cx/index.php?title=Bink_Audio
392 int BIKPlayer::sound_init(bool need_init)
394 int sample_rate = header.samplerate;
395 int sample_rate_half;
396 unsigned int i;
397 int frame_len_bits;
398 int ret;
400 if(need_init) {
401 s_stream = setAudioStream();
402 } else {
403 s_stream = -1;
404 return 0;
407 if(s_stream<0) {
408 return 0;
411 if(header.audioflag&BINK_AUD_STEREO) {
412 header.channels=2;
415 /* determine frame length */
416 if (sample_rate < 22050) {
417 frame_len_bits = 9;
418 } else if (sample_rate < 44100) {
419 frame_len_bits = 10;
420 } else {
421 frame_len_bits = 11;
423 //audio frame length
424 s_frame_len = 1 << frame_len_bits;
426 if (header.channels > MAX_CHANNELS) {
427 //av_log(s->avctx, AV_LOG_ERROR, "too many channels: %d\n", s->channels);
428 return -1;
431 if (header.audioflag&BINK_AUD_USEDCT) {
432 s_channels = header.channels;
433 } else {
434 // audio is already interleaved for the RDFT format variant
435 sample_rate *= header.channels;
436 s_frame_len *= header.channels;
437 s_channels = 1;
438 if (header.channels == 2)
439 frame_len_bits++;
442 s_overlap_len = s_frame_len / 16;
443 s_block_size = (s_frame_len - s_overlap_len) * s_channels;
444 sample_rate_half = (sample_rate + 1) / 2;
445 s_root = (float) (2.0 / sqrt((float) s_frame_len));
447 /* calculate number of bands */
448 for (s_num_bands = 1; s_num_bands < 25; s_num_bands++) {
449 if (sample_rate_half <= ff_wma_critical_freqs[s_num_bands - 1]) {
450 break;
454 s_bands = (unsigned int *) av_malloc((s_num_bands + 1) * sizeof(*s_bands));
455 if (!s_bands) {
456 return -2;
459 /* populate bands data */
460 s_bands[0] = 1;
461 for (i = 1; i < s_num_bands; i++)
462 s_bands[i] = ff_wma_critical_freqs[i - 1] * (s_frame_len / 2) / sample_rate_half;
463 s_bands[s_num_bands] = s_frame_len / 2;
465 s_first = 1;
467 for (i = 0; i < s_channels; i++)
468 s_coeffs_ptr[i] = s_coeffs + i * s_frame_len;
470 if (header.audioflag&BINK_AUD_USEDCT)
471 ret = ff_dct_init(&s_trans.dct, frame_len_bits, 0);
472 else
473 ret = ff_rdft_init(&s_trans.rdft, frame_len_bits, IRIDFT);
475 return ret;
478 void BIKPlayer::ff_init_scantable(uint8_t *permutation, ScanTable *st, const uint8_t *src_scantable){
479 int i,j;
480 int end;
482 st->scantable= src_scantable;
484 for(i=0; i<64; i++){
485 j = src_scantable[i];
486 st->permutated[i] = permutation[j];
489 end=-1;
490 for(i=0; i<64; i++){
491 j = st->permutated[i];
492 if(j>end) end=j;
493 st->raster_end[i]= end;
497 int BIKPlayer::video_init(int w, int h)
499 int bw, bh, blocks;
500 int i;
502 if (!bink_trees[15].table) {
503 for (i = 0; i < 16; i++) {
504 const int maxbits = bink_tree_lens[i][15];
505 bink_trees[i].table = table + i*128;
506 bink_trees[i].table_allocated = 1 << maxbits;
507 bink_trees[i].init_vlc(maxbits, 16, bink_tree_lens[i], 1, 1,
508 bink_tree_bits[i], 1, 1, INIT_VLC_LE);
512 memset(&c_pic,0, sizeof(AVFrame));
513 memset(&c_last,0, sizeof(AVFrame));
515 if (w<(signed) header.width || h<(signed) header.height) {
516 //movie dimensions are higher than available screen
517 return 1;
520 //pixel format is PIX_FMT_YUV420P
521 //idct permutation is used in various optimisations,
522 //we go with the simplest (no permutation)
523 for(i=0;i<64;i++) {
524 c_idct_permutation[i]=i;
527 ff_init_scantable(c_idct_permutation, &c_scantable, bink_scan);
529 bw = (header.width + 7) >> 3;
530 bh = (header.height + 7) >> 3;
531 blocks = bw * bh;
533 for (i = 0; i < BINK_NB_SRC; i++) {
534 c_bundle[i].data = (uint8_t *) av_malloc(blocks * 64);
535 //not enough memory
536 if(!c_bundle[i].data) {
537 return 2;
539 c_bundle[i].data_end = (uint8_t *) c_bundle[i].data + blocks * 64;
542 return 0;
545 int BIKPlayer::EndAudio()
547 freeAudioStream(s_stream);
548 av_freep((void **) &s_bands);
549 if (header.audioflag&BINK_AUD_USEDCT)
550 ff_dct_end(&s_trans.dct);
551 else
552 ff_rdft_end(&s_trans.rdft);
553 return 0;
556 static inline void release_buffer(AVFrame *p)
558 int i;
560 for(i=0;i<3;i++) {
561 av_freep((void **) &p->data[i]);
565 static inline void ff_fill_linesize(AVFrame *picture, int width)
567 memset(picture->linesize, 0, sizeof(picture->linesize));
568 int w2 = (width + (1 << 1) - 1) >> 1;
569 picture->linesize[0] = width;
570 picture->linesize[1] = w2;
571 picture->linesize[2] = w2;
574 static inline void get_buffer(AVFrame *p, int width, int height)
576 ff_fill_linesize(p, width);
577 for(int plane=0;plane<3;plane++) {
578 p->data[plane] = (uint8_t *) av_malloc(p->linesize[plane]*height);
582 int BIKPlayer::EndVideo()
584 int i;
586 release_buffer(&c_pic);
587 release_buffer(&c_last);
588 for (i = 0; i < BINK_NB_SRC; i++) {
589 av_freep((void **) &c_bundle[i].data);
591 return 0;
593 static const uint8_t rle_length_tab[16] = {
594 2, 3, 4, 5, 6, 8, 9, 10, 11, 12, 13, 14, 15, 16, 32, 64
597 const uint8_t ff_log2_tab[256]={
598 0,0,1,1,2,2,2,2,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,4,
599 5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,5,
600 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
601 6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,6,
602 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
603 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
604 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
605 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7
608 static inline int float_to_int16_one(const float *src){
609 float f = *src;
610 // clamp the values to the range of an int16.
611 if (f > 32767.0)
612 return 32767;
613 else if (f < -32768.0)
614 return -32768;
615 return (int32_t) (f);
618 static void ff_float_to_int16_interleave_c(int16_t *dst, const float **src, long len, int channels){
619 int i;
620 if(channels==2) {
621 for(i=0; i<len; i++) {
622 dst[2*i] = float_to_int16_one(src[0]+i);
623 dst[2*i+1] = float_to_int16_one(src[1]+i);
625 return;
627 //one channel
628 for(i=0; i<len; i++) {
629 dst[i] = float_to_int16_one(src[0]+i);
634 * Decode Bink Audio block
635 * @param[out] out Output buffer (must contain s->block_size elements)
637 void BIKPlayer::DecodeBlock(short *out)
639 unsigned int ch, i, j, k;
640 float q, quant[25];
641 int width, coeff;
643 if (header.audioflag&BINK_AUD_USEDCT) {
644 s_gb.skip_bits(2);
647 for (ch = 0; ch < s_channels; ch++) {
648 FFTSample *coeffs = s_coeffs_ptr[ch];
649 q = 0.0;
650 coeffs[0] = s_gb.get_float();
651 coeffs[1] = s_gb.get_float();
653 for (i = 0; i < s_num_bands; i++) {
654 int value = s_gb.get_bits(8);
655 quant[i] = (float) pow(10.0, FFMIN(value, 95) * 0.066399999);
658 // find band (k)
659 for (k = 0; s_bands[k] * 2 < 2; k++) {
660 q = quant[k];
663 // parse coefficients
664 i = 2;
665 while (i < s_frame_len) {
666 if (s_gb.get_bits(1)) {
667 j = i + rle_length_tab[s_gb.get_bits(4)] * 8;
668 } else {
669 j = i + 8;
672 if (j > s_frame_len)
673 j = s_frame_len;
675 width = s_gb.get_bits(4);
676 if (width == 0) {
677 memset(coeffs + i, 0, (j - i) * sizeof(*coeffs));
678 i = j;
679 while (s_bands[k] * 2 < i)
680 q = quant[k++];
681 } else {
682 while (i < j) {
683 if (s_bands[k] * 2 == i)
684 q = quant[k++];
685 coeff = s_gb.get_bits(width);
686 if (coeff) {
687 if (s_gb.get_bits(1))
688 coeffs[i] = -q * coeff;
689 else
690 coeffs[i] = q * coeff;
691 } else {
692 coeffs[i] = 0.0;
694 i++;
699 if (header.audioflag&BINK_AUD_USEDCT)
700 ff_dct_calc (&s_trans.dct, coeffs);
701 else
702 ff_rdft_calc(&s_trans.rdft, coeffs);
704 for (i = 0; i < s_frame_len; i++)
705 coeffs[i] *= s_root;
708 ff_float_to_int16_interleave_c(out, (const float **)s_coeffs_ptr, s_frame_len, s_channels);
710 if (!s_first) {
711 unsigned int count = s_overlap_len * s_channels;
712 int shift = av_log2(count);
713 for (i = 0; i < count; i++) {
714 out[i] = (s_previous[i] * (count - i) + out[i] * i) >> shift;
718 memcpy(s_previous, out + s_block_size, s_overlap_len * s_channels * sizeof(*out));
720 s_first = 0;
723 //audio samples
724 int BIKPlayer::DecodeAudioFrame(void *data, int data_size)
726 int bits = data_size*8;
727 s_gb.init_get_bits((uint8_t *) data, bits);
729 unsigned int reported_size = s_gb.get_bits_long(32);
730 ieWordSigned *samples = (ieWordSigned *) calloc(reported_size+s_block_size,1);
731 if (!samples) {
732 return -1;
735 ieWordSigned *outbuf = samples;
736 ieWordSigned *samples_end = samples+reported_size/sizeof(ieWordSigned);
738 //s_block_size is in sample units
739 while (s_gb.get_bits_count() < bits && outbuf + s_block_size <= samples_end) {
740 DecodeBlock(outbuf);
741 outbuf += s_block_size;
742 s_gb.get_bits_align32();
745 unsigned int ret = (unsigned int) ((uint8_t*)outbuf - (uint8_t*)samples);
747 //sample format is signed 16 bit integers
748 //ret is a better value here as it provides almost perfect sound.
749 //Original ffmpeg code produces worse results with reported_size.
750 //Ideally ret == reported_size
751 queueBuffer(s_stream, 16, s_channels, samples, ret, header.samplerate);
753 free(samples);
754 return reported_size!=ret;
758 * Reads 8x8 block of DCT coefficients.
760 * @param gb context for reading bits
761 * @param block place for storing coefficients
762 * @param scan scan order table
763 * @return 0 for success, negative value in other cases
765 int BIKPlayer::read_dct_coeffs(DCTELEM block[64], const uint8_t *scan)
767 int mode_list[128];
768 int i, t, mask, bits, ccoef, mode;
769 int list_start = 64, list_end = 64, list_pos;
771 mode_list[list_end++] = ( 4 << 2) | 0;
772 mode_list[list_end++] = (24 << 2) | 0;
773 mode_list[list_end++] = (44 << 2) | 0;
774 mode_list[list_end++] = ( 1 << 2) | 3;
775 mode_list[list_end++] = ( 2 << 2) | 3;
776 mode_list[list_end++] = ( 3 << 2) | 3;
778 bits = v_gb.get_bits(4) - 1;
779 for (mask = 1 << bits; bits >= 0; mask >>= 1, bits--) {
780 list_pos = list_start;
781 while (list_pos < list_end) {
782 if (!mode_list[list_pos] || !v_gb.get_bits(1)) {
783 list_pos++;
784 continue;
786 ccoef = mode_list[list_pos] >> 2;
787 mode = mode_list[list_pos] & 3;
788 switch (mode) {
789 case 0:
790 case 2:
791 if (!mode) {
792 mode_list[list_pos] = ((ccoef + 4) << 2) | 1;
793 } else {
794 mode_list[list_pos++] = 0;
796 for (i = 0; i < 4; i++, ccoef++) {
797 if (v_gb.get_bits(1)) {
798 mode_list[--list_start] = (ccoef << 2) | 3;
799 } else {
800 int t;
801 if (!bits) {
802 t = v_gb.get_bits(1) ? -1 : 1;
803 } else {
804 t = v_gb.get_bits(bits) | mask;
805 if (v_gb.get_bits(1)) {
806 t = -t;
809 block[scan[ccoef]] = t;
812 break;
813 case 1:
814 mode_list[list_pos] = (ccoef << 2) | 2;
815 for (i = 0; i < 3; i++) {
816 ccoef += 4;
817 mode_list[list_end++] = (ccoef << 2) | 2;
819 break;
820 case 3:
821 if (!bits) {
822 t = v_gb.get_bits(1) ? -1 : 1;
823 } else {
824 t = v_gb.get_bits(bits) | mask;
825 if (v_gb.get_bits(1))
826 t = -t;
828 block[scan[ccoef]] = t;
829 mode_list[list_pos++] = 0;
830 break;
835 return 0;
839 * Reads 8x8 block with residue after motion compensation.
841 * @param gb context for reading bits
842 * @param block place to store read data
843 * @param masks_count number of masks to decode
844 * @return 0 on success, negative value in other cases
846 int BIKPlayer::read_residue(DCTELEM block[64], int masks_count)
848 int mode_list[128];
849 int i, mask, ccoef, mode;
850 int list_start = 64, list_end = 64, list_pos;
851 int nz_coeff[64];
852 int nz_coeff_count = 0;
854 mode_list[list_end++] = ( 4 << 2) | 0;
855 mode_list[list_end++] = (24 << 2) | 0;
856 mode_list[list_end++] = (44 << 2) | 0;
857 mode_list[list_end++] = ( 0 << 2) | 2;
859 for (mask = 1 << v_gb.get_bits(3); mask; mask >>= 1) {
860 for (i = 0; i < nz_coeff_count; i++) {
861 if (!v_gb.get_bits(1))
862 continue;
863 if (block[nz_coeff[i]] < 0)
864 block[nz_coeff[i]] -= mask;
865 else
866 block[nz_coeff[i]] += mask;
867 masks_count--;
868 if (masks_count < 0)
869 return 0;
871 list_pos = list_start;
872 while (list_pos < list_end) {
873 if (!mode_list[list_pos] || !v_gb.get_bits(1)) {
874 list_pos++;
875 continue;
877 ccoef = mode_list[list_pos] >> 2;
878 mode = mode_list[list_pos] & 3;
879 switch (mode) {
880 case 0:
881 case 2:
882 if (!mode) {
883 mode_list[list_pos] = ((ccoef + 4) << 2) | 1;
884 } else {
885 mode_list[list_pos++] = 0;
887 for (i = 0; i < 4; i++, ccoef++) {
888 if (v_gb.get_bits(1)) {
889 mode_list[--list_start] = (ccoef << 2) | 3;
890 } else {
891 nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
892 block[bink_scan[ccoef]] = v_gb.get_bits(1) ? -mask : mask;
893 masks_count--;
894 if (masks_count < 0) {
895 return 0;
899 break;
900 case 1:
901 mode_list[list_pos] = (ccoef << 2) | 2;
902 for (i = 0; i < 3; i++) {
903 ccoef += 4;
904 mode_list[list_end++] = (ccoef << 2) | 2;
906 break;
907 case 3:
908 nz_coeff[nz_coeff_count++] = bink_scan[ccoef];
909 block[bink_scan[ccoef]] = v_gb.get_bits(1) ? -mask : mask;
910 mode_list[list_pos++] = 0;
911 masks_count--;
912 if (masks_count < 0)
913 return 0;
914 break;
919 return 0;
923 * Prepares bundle for decoding data.
925 * @param gb context for reading bits
926 * @param c decoder context
927 * @param bundle_num number of the bundle to initialize
929 void BIKPlayer::read_bundle(int bundle_num)
931 int i;
933 if (bundle_num == BINK_SRC_COLORS) {
934 for (i = 0; i < 16; i++)
935 v_gb.read_tree(&c_col_high[i]);
936 c_col_lastval = 0;
938 if (bundle_num != BINK_SRC_INTRA_DC && bundle_num != BINK_SRC_INTER_DC)
939 v_gb.read_tree(&c_bundle[bundle_num].tree);
940 c_bundle[bundle_num].cur_dec =
941 c_bundle[bundle_num].cur_ptr = c_bundle[bundle_num].data;
945 * Initializes length length in all bundles.
947 * @param c decoder context
948 * @param width plane width
949 * @param bw plane width in 8x8 blocks
951 void BIKPlayer::init_lengths(int width, int bw)
953 c_bundle[BINK_SRC_BLOCK_TYPES].len = av_log2((width >> 3) + 511) + 1;
955 c_bundle[BINK_SRC_SUB_BLOCK_TYPES].len = av_log2((width >> 4) + 511) + 1;
957 c_bundle[BINK_SRC_COLORS].len = av_log2((width >> 3)*64 + 511) + 1;
959 c_bundle[BINK_SRC_INTRA_DC].len =
960 c_bundle[BINK_SRC_INTER_DC].len =
961 c_bundle[BINK_SRC_X_OFF].len =
962 c_bundle[BINK_SRC_Y_OFF].len = av_log2((width >> 3) + 511) + 1;
964 c_bundle[BINK_SRC_PATTERN].len = av_log2((bw << 3) + 511) + 1;
966 c_bundle[BINK_SRC_RUN].len = av_log2((width >> 3)*48 + 511) + 1;
969 #define CHECK_READ_VAL(gb, b, t) \
970 if (!b->cur_dec || (b->cur_dec > b->cur_ptr)) \
971 return 0; \
972 t = gb.get_bits(b->len); \
973 if (!t) { \
974 b->cur_dec = NULL; \
975 return 0; \
978 int BIKPlayer::get_vlc2(int16_t (*table)[2], int bits, int max_depth)
980 int code;
981 int n, index, nb_bits;
983 index= v_gb.peek_bits(bits);
984 code = table[index][0];
985 n = table[index][1];
987 if(max_depth > 1 && n < 0){
988 v_gb.skip_bits(bits);
990 nb_bits = -n;
992 index= v_gb.peek_bits(nb_bits) + code;
993 code = table[index][0];
994 n = table[index][1];
995 if(max_depth > 2 && n < 0){
996 v_gb.skip_bits(nb_bits);
998 nb_bits = -n;
1000 index= v_gb.get_bits(nb_bits) + code;
1001 code = table[index][0];
1002 n = table[index][1];
1005 v_gb.skip_bits(n);
1006 return code;
1009 #define GET_HUFF(tree) \
1010 (tree).syms[get_vlc2(bink_trees[(tree).vlc_num].table, \
1011 bink_trees[(tree).vlc_num].bits, 1)]
1013 int BIKPlayer::read_runs(Bundle *b)
1015 int i, t, v;
1017 CHECK_READ_VAL(v_gb, b, t);
1018 if (v_gb.get_bits(1)) {
1019 v = v_gb.get_bits(4);
1020 if (b->cur_dec + t > b->data_end) {
1021 return -1;
1023 memset(b->cur_dec, v, t);
1024 b->cur_dec += t;
1025 } else {
1026 for (i = 0; i < t; i++) {
1027 v = GET_HUFF(b->tree);
1028 *b->cur_dec++ = v;
1031 return 0;
1034 int BIKPlayer::read_motion_values(Bundle *b)
1036 int i, t, v;
1038 CHECK_READ_VAL(v_gb, b, t);
1039 if (v_gb.get_bits(1)) {
1040 v = v_gb.get_bits(4);
1041 if (v && v_gb.get_bits(1)) {
1042 v = -v;
1044 if (b->cur_dec + t > b->data_end) {
1045 return -1;
1047 memset(b->cur_dec, v, t);
1048 b->cur_dec += t;
1049 } else {
1050 for (i = 0; i < t; i++) {
1051 v = GET_HUFF(b->tree);
1052 if (v && v_gb.get_bits(1)) {
1053 v = -v;
1055 *b->cur_dec++ = v;
1058 return 0;
1061 const uint8_t bink_rlelens[4] = { 4, 8, 12, 32 };
1063 int BIKPlayer::read_block_types(Bundle *b)
1065 int i, t, v;
1066 int last = 0;
1068 CHECK_READ_VAL(v_gb, b, t);
1069 if (v_gb.get_bits(1)) {
1070 v = v_gb.get_bits(4);
1071 memset(b->cur_dec, v, t);
1072 b->cur_dec += t;
1073 } else {
1074 for (i = 0; i < t; i++) {
1075 v = GET_HUFF(b->tree);
1076 if (v < 12) {
1077 last = v;
1078 *b->cur_dec++ = v;
1079 } else {
1080 int run = bink_rlelens[v - 12];
1082 memset(b->cur_dec, last, run);
1083 b->cur_dec += run;
1084 i += run - 1;
1088 return 0;
1091 int BIKPlayer::read_patterns(Bundle *b)
1093 int i, t, v;
1095 CHECK_READ_VAL(v_gb, b, t);
1096 for (i = 0; i < t; i++) {
1097 v = GET_HUFF(b->tree);
1098 v |= GET_HUFF(b->tree) << 4;
1099 *b->cur_dec++ = v;
1102 return 0;
1105 int BIKPlayer::read_colors(Bundle *b)
1107 int i, t, v, v2;
1109 CHECK_READ_VAL(v_gb, b, t);
1110 if (v_gb.get_bits(1)) {
1111 v2 = GET_HUFF(c_col_high[c_col_lastval]);
1112 c_col_lastval = v2;
1113 v = GET_HUFF(b->tree);
1114 v = (v2 << 4) | v;
1115 memset(b->cur_dec, v, t);
1116 b->cur_dec += t;
1117 } else {
1118 for (i = 0; i < t; i++) {
1119 v2 = GET_HUFF(c_col_high[c_col_lastval]);
1120 c_col_lastval = v2;
1121 v = GET_HUFF(b->tree);
1122 v = (v2 << 4) | v;
1123 *b->cur_dec++ = v;
1126 return 0;
1129 /** number of bits used to store first DC value in bundle */
1130 #define DC_START_BITS 11
1132 int BIKPlayer::read_dcs(Bundle *b, int start_bits, int has_sign)
1134 int i, j, len, len2, bsize, v, v2;
1135 int16_t *dst = (int16_t*)b->cur_dec;
1137 CHECK_READ_VAL(v_gb, b, len);
1138 if (has_sign) {
1139 v = v_gb.get_bits(start_bits - 1);
1140 if (v && v_gb.get_bits(1))
1141 v = -v;
1142 } else {
1143 v = v_gb.get_bits(start_bits);
1145 *dst++ = v;
1146 len--;
1147 for (i = 0; i < len; i += 8) {
1148 len2 = FFMIN(len - i, 8);
1149 bsize = v_gb.get_bits(4);
1150 if (bsize) {
1151 for (j = 0; j < len2; j++) {
1152 v2 = v_gb.get_bits(bsize);
1153 if (v2 && v_gb.get_bits(1)) {
1154 v2 = -v2;
1156 v += v2;
1157 *dst++ = v;
1158 if (v < -32768 || v > 32767) {
1159 return -1;
1162 } else {
1163 for (j = 0; j < len2; j++) {
1164 *dst++ = v;
1169 b->cur_dec = (uint8_t*)dst;
1170 return 0;
1173 inline int BIKPlayer::get_value(int bundle)
1175 int16_t ret;
1177 if (bundle < BINK_SRC_X_OFF || bundle == BINK_SRC_RUN) {
1178 return *c_bundle[bundle].cur_ptr++;
1180 if (bundle == BINK_SRC_X_OFF || bundle == BINK_SRC_Y_OFF) {
1181 return (int8_t)*c_bundle[bundle].cur_ptr++;
1183 ret = *(int16_t*)c_bundle[bundle].cur_ptr;
1184 c_bundle[bundle].cur_ptr += 2;
1185 return ret;
1188 #define PUT2x2(dst, stride, x, y, pix) \
1189 dst[(x)*2 + (y)*2 * stride] = \
1190 dst[(x)*2 + 1 + (y)*2 * stride] = \
1191 dst[(x)*2 + ((y)*2 + 1) * stride] = \
1192 dst[(x)*2 + 1 + ((y)*2 + 1) * stride] = pix;
1194 static void get_pixels(DCTELEM *block, const uint8_t *pixels, int line_size)
1196 int i;
1198 /* read the pixels */
1199 for(i=0;i<8;i++) {
1200 block[0] = pixels[0];
1201 block[1] = pixels[1];
1202 block[2] = pixels[2];
1203 block[3] = pixels[3];
1204 block[4] = pixels[4];
1205 block[5] = pixels[5];
1206 block[6] = pixels[6];
1207 block[7] = pixels[7];
1208 pixels += line_size;
1209 block += 8;
1213 static void put_pixels_clamped(const DCTELEM *block, uint8_t *pixels, int line_size)
1215 int i;
1216 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1218 /* read the pixels */
1219 for(i=0;i<8;i++) {
1220 pixels[0] = cm[block[0]];
1221 pixels[1] = cm[block[1]];
1222 pixels[2] = cm[block[2]];
1223 pixels[3] = cm[block[3]];
1224 pixels[4] = cm[block[4]];
1225 pixels[5] = cm[block[5]];
1226 pixels[6] = cm[block[6]];
1227 pixels[7] = cm[block[7]];
1228 pixels += line_size;
1229 block += 8;
1233 static void add_pixels_clamped(const DCTELEM *block, uint8_t *pixels, int line_size)
1235 int i;
1236 uint8_t *cm = ff_cropTbl + MAX_NEG_CROP;
1238 /* read the pixels */
1239 for(i=0;i<8;i++) {
1240 pixels[0] = cm[pixels[0] + block[0]];
1241 pixels[1] = cm[pixels[1] + block[1]];
1242 pixels[2] = cm[pixels[2] + block[2]];
1243 pixels[3] = cm[pixels[3] + block[3]];
1244 pixels[4] = cm[pixels[4] + block[4]];
1245 pixels[5] = cm[pixels[5] + block[5]];
1246 pixels[6] = cm[pixels[6] + block[6]];
1247 pixels[7] = cm[pixels[7] + block[7]];
1248 pixels += line_size;
1249 block += 8;
1253 static inline void copy_block(DCTELEM block[64], const uint8_t *src, uint8_t *dst, int stride)
1255 get_pixels(block, src, stride);
1256 put_pixels_clamped(block, dst, stride);
1259 #define clear_block(block) memset( (block), 0, sizeof(DCTELEM)*64);
1261 //This replaces the j_rev_dct module
1262 void bink_idct(DCTELEM *block)
1264 int i, t0, t1, t2, t3, t4, t5, t6, t7, t8, t9, tA, tB, tC;
1265 int tblock[64];
1267 for (i = 0; i < 8; i++) {
1268 t0 = block[i+ 0] + block[i+32];
1269 t1 = block[i+ 0] - block[i+32];
1270 t2 = block[i+16] + block[i+48];
1271 t3 = block[i+16] - block[i+48];
1272 t3 = ((t3 * 0xB50) >> 11) - t2;
1274 t4 = t0 - t2;
1275 t5 = t0 + t2;
1276 t6 = t1 + t3;
1277 t7 = t1 - t3;
1279 t0 = block[i+40] + block[i+24];
1280 t1 = block[i+40] - block[i+24];
1281 t2 = block[i+ 8] + block[i+56];
1282 t3 = block[i+ 8] - block[i+56];
1284 t8 = t2 + t0;
1285 t9 = t3 + t1;
1286 t9 = (0xEC8 * t9) >> 11;
1287 tA = ((-0x14E8 * t1) >> 11) + t9 - t8;
1288 tB = t2 - t0;
1289 tB = ((0xB50 * tB) >> 11) - tA;
1290 tC = ((0x8A9 * t3) >> 11) + tB - t9;
1292 tblock[i+ 0] = t5 + t8;
1293 tblock[i+56] = t5 - t8;
1294 tblock[i+ 8] = t6 + tA;
1295 tblock[i+48] = t6 - tA;
1296 tblock[i+16] = t7 + tB;
1297 tblock[i+40] = t7 - tB;
1298 tblock[i+32] = t4 + tC;
1299 tblock[i+24] = t4 - tC;
1302 for (i = 0; i < 64; i += 8) {
1303 t0 = tblock[i+0] + tblock[i+4];
1304 t1 = tblock[i+0] - tblock[i+4];
1305 t2 = tblock[i+2] + tblock[i+6];
1306 t3 = tblock[i+2] - tblock[i+6];
1307 t3 = ((t3 * 0xB50) >> 11) - t2;
1309 t4 = t0 - t2;
1310 t5 = t0 + t2;
1311 t6 = t1 + t3;
1312 t7 = t1 - t3;
1314 t0 = tblock[i+5] + tblock[i+3];
1315 t1 = tblock[i+5] - tblock[i+3];
1316 t2 = tblock[i+1] + tblock[i+7];
1317 t3 = tblock[i+1] - tblock[i+7];
1319 t8 = t2 + t0;
1320 t9 = t3 + t1;
1321 t9 = (0xEC8 * t9) >> 11;
1322 tA = ((-0x14E8 * t1) >> 11) + t9 - t8;
1323 tB = t2 - t0;
1324 tB = ((0xB50 * tB) >> 11) - tA;
1325 tC = ((0x8A9 * t3) >> 11) + tB - t9;
1327 block[i+0] = (t5 + t8 + 0x7F) >> 8;
1328 block[i+7] = (t5 - t8 + 0x7F) >> 8;
1329 block[i+1] = (t6 + tA + 0x7F) >> 8;
1330 block[i+6] = (t6 - tA + 0x7F) >> 8;
1331 block[i+2] = (t7 + tB + 0x7F) >> 8;
1332 block[i+5] = (t7 - tB + 0x7F) >> 8;
1333 block[i+4] = (t4 + tC + 0x7F) >> 8;
1334 block[i+3] = (t4 - tC + 0x7F) >> 8;
1338 static void idct_put(uint8_t *dest, int line_size, DCTELEM *block)
1340 bink_idct(block);
1341 put_pixels_clamped(block, dest, line_size);
1343 static void idct_add(uint8_t *dest, int line_size, DCTELEM *block)
1345 bink_idct(block);
1346 add_pixels_clamped(block, dest, line_size);
1349 int BIKPlayer::DecodeVideoFrame(void *data, int data_size)
1351 int blk, bw, bh;
1352 int i, j, plane, bx, by;
1353 uint8_t *dst, *prev;
1354 int v, c1, c2;
1355 const uint8_t *scan;
1356 const uint32_t *quant;
1357 int xoff, yoff;
1358 #pragma pack(push,16)
1359 DCTELEM block[64];
1360 #pragma pack(pop)
1362 int bits = data_size*8;
1363 v_gb.init_get_bits((uint8_t *) data, bits);
1364 //this is compatible only with the BIKi version
1365 v_gb.skip_bits(32);
1367 get_buffer(&c_pic, header.width, header.height);
1368 //plane order is YUV
1369 for (plane = 0; plane < 3; plane++) {
1370 const int stride = c_pic.linesize[plane];
1372 bw = plane ? (header.width + 15) >> 4 : (header.width + 7) >> 3;
1373 bh = plane ? (header.height + 15) >> 4 : (header.height + 7) >> 3;
1374 if(plane) {
1375 init_lengths(header.width >> 1, bw);
1376 } else {
1377 init_lengths(header.width, bw);
1380 for (i = 0; i < BINK_NB_SRC; i++) {
1381 read_bundle(i);
1384 for (by = 0; by < bh; by++) {
1385 if (read_block_types(&c_bundle[BINK_SRC_BLOCK_TYPES]) < 0)
1386 return -1;
1387 if (read_block_types(&c_bundle[BINK_SRC_SUB_BLOCK_TYPES]) < 0)
1388 return -1;
1389 if (read_colors(&c_bundle[BINK_SRC_COLORS]) < 0)
1390 return -1;
1391 if (read_patterns(&c_bundle[BINK_SRC_PATTERN]) < 0)
1392 return -1;
1393 if (read_motion_values(&c_bundle[BINK_SRC_X_OFF]) < 0)
1394 return -1;
1395 if (read_motion_values(&c_bundle[BINK_SRC_Y_OFF]) < 0)
1396 return -1;
1397 if (read_dcs(&c_bundle[BINK_SRC_INTRA_DC], DC_START_BITS, 0) < 0)
1398 return -1;
1399 if (read_dcs(&c_bundle[BINK_SRC_INTER_DC], DC_START_BITS, 1) < 0)
1400 return -1;
1401 if (read_runs(&c_bundle[BINK_SRC_RUN]) < 0)
1402 return -1;
1404 //why is this here?
1405 if (by == bh)
1406 break;
1408 dst = c_pic.data[plane] + 8*by*stride;
1409 prev = c_last.data[plane] + 8*by*stride;
1410 for (bx = 0; bx < bw; bx++, dst += 8, prev += 8) {
1411 blk = get_value(BINK_SRC_BLOCK_TYPES);
1412 if ((by & 1) && blk == 1) {
1413 bx++;
1414 dst += 8;
1415 prev += 8;
1416 continue;
1418 switch (blk) {
1419 case SKIP_BLOCK:
1420 copy_block(block, prev, dst, stride);
1421 break;
1422 case SCALED_BLOCK:
1423 blk = get_value(BINK_SRC_SUB_BLOCK_TYPES);
1424 switch (blk) {
1425 case RUN_BLOCK:
1426 scan = bink_patterns[v_gb.get_bits(4)];
1427 i = 0;
1428 do {
1429 int run = get_value(BINK_SRC_RUN) + 1;
1430 if (v_gb.get_bits(1)) {
1431 v = get_value(BINK_SRC_COLORS);
1432 for (j = 0; j < run; j++) {
1433 int pos = *scan++;
1434 PUT2x2(dst, stride, pos & 7, pos >> 3, v);
1436 } else {
1437 for (j = 0; j < run; j++) {
1438 int pos = *scan++;
1439 PUT2x2(dst, stride, pos & 7, pos >> 3, get_value(BINK_SRC_COLORS));
1442 i += run;
1443 } while (i < 63);
1444 if (i == 63) {
1445 int pos = *scan++;
1446 PUT2x2(dst, stride, pos & 7, pos >> 3, get_value(BINK_SRC_COLORS));
1448 break;
1449 case INTRA_BLOCK:
1450 clear_block(block);
1451 block[0] = get_value(BINK_SRC_INTRA_DC);
1452 read_dct_coeffs(block, c_scantable.permutated);
1453 quant = bink_intra_quant[v_gb.get_bits(4)];
1454 for (i = 0; i < 64; i++) {
1455 block[i] = (block[i] * quant[i]) >> 11;
1457 bink_idct(block);
1458 for (j = 0; j < 8; j++) {
1459 for (i = 0; i < 8; i++) {
1460 PUT2x2(dst, stride, i, j, block[i + j*8]);
1463 break;
1464 case FILL_BLOCK:
1465 v = get_value(BINK_SRC_COLORS);
1466 for (j = 0; j < 16; j++) {
1467 memset(dst + j*stride, v, 16);
1469 break;
1470 case PATTERN_BLOCK:
1471 c1 = get_value(BINK_SRC_COLORS);
1472 c2 = get_value(BINK_SRC_COLORS);
1473 for (i = 0; i < 8; i++) {
1474 v = get_value(BINK_SRC_PATTERN);
1475 for (j = 0; j < 8; j++, v >>= 1) {
1476 PUT2x2(dst, stride, i, j, (v & 1) ? c2 : c1);
1479 break;
1480 case RAW_BLOCK:
1481 for (j = 0; j < 8; j++) {
1482 for (i = 0; i < 8; i++) {
1483 PUT2x2(dst, stride, i, j, get_value(BINK_SRC_COLORS));
1486 break;
1487 default:
1488 printf("Incorrect 16x16 block type!\n");
1489 return -1;
1491 bx++;
1492 dst += 8;
1493 prev += 8;
1494 break;
1495 case MOTION_BLOCK:
1496 xoff = get_value(BINK_SRC_X_OFF);
1497 yoff = get_value(BINK_SRC_Y_OFF);
1498 copy_block(block, prev + xoff + yoff*stride, dst, stride);
1499 break;
1500 case RUN_BLOCK:
1501 scan = bink_patterns[v_gb.get_bits(4)];
1502 i = 0;
1503 do {
1504 int run = get_value(BINK_SRC_RUN) + 1;
1505 if (v_gb.get_bits(1)) {
1506 v = get_value(BINK_SRC_COLORS);
1507 for (j = 0; j < run; j++) {
1508 int pos = *scan++;
1509 dst[(pos & 7) + (pos >> 3) * stride] = v;
1511 } else {
1512 for (j = 0; j < run; j++) {
1513 int pos = *scan++;
1514 dst[(pos & 7) + (pos >> 3) * stride] = get_value(BINK_SRC_COLORS);
1517 i += run;
1518 } while (i < 63);
1519 if (i == 63) {
1520 int pos = *scan++;
1521 dst[(pos & 7) + (pos >> 3)*stride] = get_value(BINK_SRC_COLORS);
1523 break;
1524 case RESIDUE_BLOCK:
1525 xoff = get_value(BINK_SRC_X_OFF);
1526 yoff = get_value(BINK_SRC_Y_OFF);
1527 copy_block(block, prev + xoff + yoff*stride, dst, stride);
1528 clear_block(block);
1529 v = v_gb.get_bits(7);
1530 read_residue(block, v);
1531 add_pixels_clamped(block, dst, stride);
1532 break;
1533 case INTRA_BLOCK:
1534 clear_block(block);
1535 block[0] = get_value(BINK_SRC_INTRA_DC);
1536 read_dct_coeffs(block, c_scantable.permutated);
1537 quant = bink_intra_quant[v_gb.get_bits(4)];
1538 for (i = 0; i < 64; i++) {
1539 block[i] = (block[i] * quant[i]) >> 11;
1541 idct_put(dst, stride, block);
1542 break;
1543 case FILL_BLOCK:
1544 v = get_value(BINK_SRC_COLORS);
1545 for (i = 0; i < 8; i++) {
1546 memset(dst + i*stride, v, 8);
1548 break;
1549 case INTER_BLOCK:
1550 xoff = get_value(BINK_SRC_X_OFF);
1551 yoff = get_value(BINK_SRC_Y_OFF);
1552 copy_block(block, prev + xoff + yoff*stride, dst, stride);
1553 clear_block(block);
1554 block[0] = get_value(BINK_SRC_INTER_DC);
1555 read_dct_coeffs(block, c_scantable.permutated);
1556 quant = bink_inter_quant[v_gb.get_bits(4)];
1557 for (i = 0; i < 64; i++) {
1558 block[i] = (block[i] * quant[i]) >> 11;
1560 idct_add(dst, stride, block);
1561 break;
1562 case PATTERN_BLOCK:
1563 c1 = get_value(BINK_SRC_COLORS);
1564 c2 = get_value(BINK_SRC_COLORS);
1565 for (i = 0; i < 8; i++) {
1566 v = get_value(BINK_SRC_PATTERN);
1567 for (j = 0; j < 8; j++, v >>= 1) {
1568 dst[i + j*stride] = (v & 1) ? c2 : c1;
1571 break;
1572 case RAW_BLOCK:
1573 for (i = 0; i < 8; i++) {
1574 memcpy(dst + i*stride, c_bundle[BINK_SRC_COLORS].cur_ptr + i*8, 8);
1576 c_bundle[BINK_SRC_COLORS].cur_ptr += 64;
1577 break;
1578 default:
1579 printf("Unknown block type!\n");
1580 return -1;
1584 v_gb.get_bits_align32();
1587 if (video_frameskip) {
1588 video_frameskip--;
1589 video_skippedframes++;
1590 } else {
1591 unsigned int dest_x = (outputwidth - header.width) >> 1;
1592 unsigned int dest_y = (outputheight - header.height) >> 1;
1593 showFrame((ieByte **) c_pic.data, (unsigned int *) c_pic.linesize, header.width, header.height, header.width, header.height, dest_x, dest_y);
1596 //release the old frame even when frame is skipped
1597 release_buffer(&c_last);
1598 memcpy(&c_last, &c_pic, sizeof(AVFrame));
1599 memset(&c_pic, 0, sizeof(AVFrame));
1600 return 0;
1603 #include "plugindef.h"
1605 GEMRB_PLUGIN(0x316E2EDE, "BIK Video Player")
1606 PLUGIN_RESOURCE(BIKPlayer, ".mve")
1607 END_PLUGIN()