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"
38 #include "Variables.h"
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,
55 static uint8_t ff_cropTbl
[256 + 2 * MAX_NEG_CROP
] = {0, };
57 BIKPlayer::BIKPlayer(void)
61 video
= core
->GetVideoDriver();
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
++) {
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
);
99 str
->ReadDword(&header
.framecount
);
101 if (header
.framecount
> 1000000) {
105 str
->ReadDword(&header
.maxframesize
);
106 if (header
.maxframesize
> header
.filesize
) {
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);
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) {
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
;
152 str
->ReadDword(&pos
);
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
;
161 str
->ReadDword(&next_pos
);
163 if (next_pos
<= pos
) {
164 // av_log(s, AV_LOG_ERROR, "invalid frame index table\n");
167 //offset, size, keyframe
170 //the order of instructions is important here!
172 frame
.keyframe
=keyframe
;
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
);
189 str
->Seek(4, GEM_CURRENT_POS
);
193 bool BIKPlayer::Open(DataStream
* stream
)
198 str
->Read( &header
.signature
, BIK_SIGNATURE_LEN
);
199 if (memcmp( header
.signature
, BIK_SIGNATURE_DATA
, 4 ) == 0) {
200 validVideo
= ReadHeader()==0;
206 void BIKPlayer::CallBackAtFrames(ieDword cnt
, ieDword
*arg
, ieDword
*arg2
)
215 int BIKPlayer::Play()
220 //Start Movie Playback
226 av_freep((void **) &inbuff
);
230 //this code could be in the movieplayer parent class
231 void get_current_time(long &sec
, long &usec
) {
234 time
= GetTickCount();
237 usec
= (time
% 1000) * 1000;
240 gettimeofday(&tv
, NULL
);
247 void BIKPlayer::timer_start()
249 get_current_time(timer_last_sec
, timer_last_usec
);
252 void BIKPlayer::timer_wait()
255 get_current_time(sec
, usec
);
257 while (sec
> 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
) {
269 long to_sleep
= frame_wait
- (usec
- timer_last_usec
);
271 Sleep(to_sleep
/ 1000);
279 bool BIKPlayer::next_frame()
281 if (timer_last_sec
) {
284 if(frameCount
>=header
.framecount
) {
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
298 if (DecodeVideoFrame(inbuff
+audframesize
, frame
.size
-audframesize
)) {
299 //buggy frame, we stop immediately
302 if (!timer_last_sec
) {
308 int BIKPlayer::doPlay()
312 //bink is always truecolor
319 if (sound_init( core
->GetAudioDrv()->CanPlay())) {
320 //sound couldn't be initialized
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
)) {
333 while (!done
&& next_frame()) {
334 done
= video
->PollMovieEvents();
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
) {
353 if ((rowCount
<maxRow
) && (frameCount
>= cbAtFrame
[rowCount
]) ) {
358 titleref
= strRef
[rowCount
-1];
361 video
->showYUVFrame(buf
,strides
,bufw
,bufh
,w
,h
,dstx
,dsty
, titleref
);
364 int BIKPlayer::setAudioStream()
367 core
->GetDictionary()->Lookup( "Volume Movie", volume
) ;
368 int source
= core
->GetAudioDrv()->SetupNewStream(0, 0, 0, volume
, false, false);
372 void BIKPlayer::freeAudioStream(int stream
)
375 core
->GetAudioDrv()->ReleaseStream(stream
, true);
378 void BIKPlayer::queueBuffer(int stream
, unsigned short bits
, int channels
, short* memory
, int size
, int samplerate
)
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
;
401 s_stream
= setAudioStream();
411 if(header
.audioflag
&BINK_AUD_STEREO
) {
415 /* determine frame length */
416 if (sample_rate
< 22050) {
418 } else if (sample_rate
< 44100) {
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);
431 if (header
.audioflag
&BINK_AUD_USEDCT
) {
432 s_channels
= header
.channels
;
434 // audio is already interleaved for the RDFT format variant
435 sample_rate
*= header
.channels
;
436 s_frame_len
*= header
.channels
;
438 if (header
.channels
== 2)
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]) {
454 s_bands
= (unsigned int *) av_malloc((s_num_bands
+ 1) * sizeof(*s_bands
));
459 /* populate bands data */
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;
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);
473 ret
= ff_rdft_init(&s_trans
.rdft
, frame_len_bits
, IRIDFT
);
478 void BIKPlayer::ff_init_scantable(uint8_t *permutation
, ScanTable
*st
, const uint8_t *src_scantable
){
482 st
->scantable
= src_scantable
;
485 j
= src_scantable
[i
];
486 st
->permutated
[i
] = permutation
[j
];
491 j
= st
->permutated
[i
];
493 st
->raster_end
[i
]= end
;
497 int BIKPlayer::video_init(int w
, int h
)
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
520 //pixel format is PIX_FMT_YUV420P
521 //idct permutation is used in various optimisations,
522 //we go with the simplest (no permutation)
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;
533 for (i
= 0; i
< BINK_NB_SRC
; i
++) {
534 c_bundle
[i
].data
= (uint8_t *) av_malloc(blocks
* 64);
536 if(!c_bundle
[i
].data
) {
539 c_bundle
[i
].data_end
= (uint8_t *) c_bundle
[i
].data
+ blocks
* 64;
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
);
552 ff_rdft_end(&s_trans
.rdft
);
556 static inline void release_buffer(AVFrame
*p
)
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()
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
);
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
){
610 // clamp the values to the range of an int16.
613 else if (f
< -32768.0)
615 return (int32_t) (f
);
618 static void ff_float_to_int16_interleave_c(int16_t *dst
, const float **src
, long len
, int channels
){
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
);
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
;
643 if (header
.audioflag
&BINK_AUD_USEDCT
) {
647 for (ch
= 0; ch
< s_channels
; ch
++) {
648 FFTSample
*coeffs
= s_coeffs_ptr
[ch
];
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);
659 for (k
= 0; s_bands
[k
] * 2 < 2; k
++) {
663 // parse coefficients
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;
675 width
= s_gb
.get_bits(4);
677 memset(coeffs
+ i
, 0, (j
- i
) * sizeof(*coeffs
));
679 while (s_bands
[k
] * 2 < i
)
683 if (s_bands
[k
] * 2 == i
)
685 coeff
= s_gb
.get_bits(width
);
687 if (s_gb
.get_bits(1))
688 coeffs
[i
] = -q
* coeff
;
690 coeffs
[i
] = q
* coeff
;
699 if (header
.audioflag
&BINK_AUD_USEDCT
)
700 ff_dct_calc (&s_trans
.dct
, coeffs
);
702 ff_rdft_calc(&s_trans
.rdft
, coeffs
);
704 for (i
= 0; i
< s_frame_len
; i
++)
708 ff_float_to_int16_interleave_c(out
, (const float **)s_coeffs_ptr
, s_frame_len
, s_channels
);
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
));
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);
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
) {
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
);
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
)
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)) {
786 ccoef
= mode_list
[list_pos
] >> 2;
787 mode
= mode_list
[list_pos
] & 3;
792 mode_list
[list_pos
] = ((ccoef
+ 4) << 2) | 1;
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;
802 t
= v_gb
.get_bits(1) ? -1 : 1;
804 t
= v_gb
.get_bits(bits
) | mask
;
805 if (v_gb
.get_bits(1)) {
809 block
[scan
[ccoef
]] = t
;
814 mode_list
[list_pos
] = (ccoef
<< 2) | 2;
815 for (i
= 0; i
< 3; i
++) {
817 mode_list
[list_end
++] = (ccoef
<< 2) | 2;
822 t
= v_gb
.get_bits(1) ? -1 : 1;
824 t
= v_gb
.get_bits(bits
) | mask
;
825 if (v_gb
.get_bits(1))
828 block
[scan
[ccoef
]] = t
;
829 mode_list
[list_pos
++] = 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
)
849 int i
, mask
, ccoef
, mode
;
850 int list_start
= 64, list_end
= 64, list_pos
;
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))
863 if (block
[nz_coeff
[i
]] < 0)
864 block
[nz_coeff
[i
]] -= mask
;
866 block
[nz_coeff
[i
]] += mask
;
871 list_pos
= list_start
;
872 while (list_pos
< list_end
) {
873 if (!mode_list
[list_pos
] || !v_gb
.get_bits(1)) {
877 ccoef
= mode_list
[list_pos
] >> 2;
878 mode
= mode_list
[list_pos
] & 3;
883 mode_list
[list_pos
] = ((ccoef
+ 4) << 2) | 1;
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;
891 nz_coeff
[nz_coeff_count
++] = bink_scan
[ccoef
];
892 block
[bink_scan
[ccoef
]] = v_gb
.get_bits(1) ? -mask
: mask
;
894 if (masks_count
< 0) {
901 mode_list
[list_pos
] = (ccoef
<< 2) | 2;
902 for (i
= 0; i
< 3; i
++) {
904 mode_list
[list_end
++] = (ccoef
<< 2) | 2;
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;
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
)
933 if (bundle_num
== BINK_SRC_COLORS
) {
934 for (i
= 0; i
< 16; i
++)
935 v_gb
.read_tree(&c_col_high
[i
]);
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)) \
972 t = gb.get_bits(b->len); \
978 int BIKPlayer::get_vlc2(int16_t (*table
)[2], int bits
, int max_depth
)
981 int n
, index
, nb_bits
;
983 index
= v_gb
.peek_bits(bits
);
984 code
= table
[index
][0];
987 if(max_depth
> 1 && n
< 0){
988 v_gb
.skip_bits(bits
);
992 index
= v_gb
.peek_bits(nb_bits
) + code
;
993 code
= table
[index
][0];
995 if(max_depth
> 2 && n
< 0){
996 v_gb
.skip_bits(nb_bits
);
1000 index
= v_gb
.get_bits(nb_bits
) + code
;
1001 code
= table
[index
][0];
1002 n
= table
[index
][1];
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
)
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
) {
1023 memset(b
->cur_dec
, v
, t
);
1026 for (i
= 0; i
< t
; i
++) {
1027 v
= GET_HUFF(b
->tree
);
1034 int BIKPlayer::read_motion_values(Bundle
*b
)
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)) {
1044 if (b
->cur_dec
+ t
> b
->data_end
) {
1047 memset(b
->cur_dec
, v
, t
);
1050 for (i
= 0; i
< t
; i
++) {
1051 v
= GET_HUFF(b
->tree
);
1052 if (v
&& v_gb
.get_bits(1)) {
1061 const uint8_t bink_rlelens
[4] = { 4, 8, 12, 32 };
1063 int BIKPlayer::read_block_types(Bundle
*b
)
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
);
1074 for (i
= 0; i
< t
; i
++) {
1075 v
= GET_HUFF(b
->tree
);
1080 int run
= bink_rlelens
[v
- 12];
1082 memset(b
->cur_dec
, last
, run
);
1091 int BIKPlayer::read_patterns(Bundle
*b
)
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;
1105 int BIKPlayer::read_colors(Bundle
*b
)
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
]);
1113 v
= GET_HUFF(b
->tree
);
1115 memset(b
->cur_dec
, v
, t
);
1118 for (i
= 0; i
< t
; i
++) {
1119 v2
= GET_HUFF(c_col_high
[c_col_lastval
]);
1121 v
= GET_HUFF(b
->tree
);
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
);
1139 v
= v_gb
.get_bits(start_bits
- 1);
1140 if (v
&& v_gb
.get_bits(1))
1143 v
= v_gb
.get_bits(start_bits
);
1147 for (i
= 0; i
< len
; i
+= 8) {
1148 len2
= FFMIN(len
- i
, 8);
1149 bsize
= v_gb
.get_bits(4);
1151 for (j
= 0; j
< len2
; j
++) {
1152 v2
= v_gb
.get_bits(bsize
);
1153 if (v2
&& v_gb
.get_bits(1)) {
1158 if (v
< -32768 || v
> 32767) {
1163 for (j
= 0; j
< len2
; j
++) {
1169 b
->cur_dec
= (uint8_t*)dst
;
1173 inline int BIKPlayer::get_value(int bundle
)
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;
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
)
1198 /* read the pixels */
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
;
1213 static void put_pixels_clamped(const DCTELEM
*block
, uint8_t *pixels
, int line_size
)
1216 uint8_t *cm
= ff_cropTbl
+ MAX_NEG_CROP
;
1218 /* read the pixels */
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
;
1233 static void add_pixels_clamped(const DCTELEM
*block
, uint8_t *pixels
, int line_size
)
1236 uint8_t *cm
= ff_cropTbl
+ MAX_NEG_CROP
;
1238 /* read the pixels */
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
;
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
;
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
;
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];
1286 t9
= (0xEC8 * t9
) >> 11;
1287 tA
= ((-0x14E8 * t1
) >> 11) + t9
- t8
;
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
;
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];
1321 t9
= (0xEC8 * t9
) >> 11;
1322 tA
= ((-0x14E8 * t1
) >> 11) + t9
- t8
;
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
)
1341 put_pixels_clamped(block
, dest
, line_size
);
1343 static void idct_add(uint8_t *dest
, int line_size
, DCTELEM
*block
)
1346 add_pixels_clamped(block
, dest
, line_size
);
1349 int BIKPlayer::DecodeVideoFrame(void *data
, int data_size
)
1352 int i
, j
, plane
, bx
, by
;
1353 uint8_t *dst
, *prev
;
1355 const uint8_t *scan
;
1356 const uint32_t *quant
;
1358 #pragma pack(push,16)
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
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;
1375 init_lengths(header
.width
>> 1, bw
);
1377 init_lengths(header
.width
, bw
);
1380 for (i
= 0; i
< BINK_NB_SRC
; i
++) {
1384 for (by
= 0; by
< bh
; by
++) {
1385 if (read_block_types(&c_bundle
[BINK_SRC_BLOCK_TYPES
]) < 0)
1387 if (read_block_types(&c_bundle
[BINK_SRC_SUB_BLOCK_TYPES
]) < 0)
1389 if (read_colors(&c_bundle
[BINK_SRC_COLORS
]) < 0)
1391 if (read_patterns(&c_bundle
[BINK_SRC_PATTERN
]) < 0)
1393 if (read_motion_values(&c_bundle
[BINK_SRC_X_OFF
]) < 0)
1395 if (read_motion_values(&c_bundle
[BINK_SRC_Y_OFF
]) < 0)
1397 if (read_dcs(&c_bundle
[BINK_SRC_INTRA_DC
], DC_START_BITS
, 0) < 0)
1399 if (read_dcs(&c_bundle
[BINK_SRC_INTER_DC
], DC_START_BITS
, 1) < 0)
1401 if (read_runs(&c_bundle
[BINK_SRC_RUN
]) < 0)
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) {
1420 copy_block(block
, prev
, dst
, stride
);
1423 blk
= get_value(BINK_SRC_SUB_BLOCK_TYPES
);
1426 scan
= bink_patterns
[v_gb
.get_bits(4)];
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
++) {
1434 PUT2x2(dst
, stride
, pos
& 7, pos
>> 3, v
);
1437 for (j
= 0; j
< run
; j
++) {
1439 PUT2x2(dst
, stride
, pos
& 7, pos
>> 3, get_value(BINK_SRC_COLORS
));
1446 PUT2x2(dst
, stride
, pos
& 7, pos
>> 3, get_value(BINK_SRC_COLORS
));
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;
1458 for (j
= 0; j
< 8; j
++) {
1459 for (i
= 0; i
< 8; i
++) {
1460 PUT2x2(dst
, stride
, i
, j
, block
[i
+ j
*8]);
1465 v
= get_value(BINK_SRC_COLORS
);
1466 for (j
= 0; j
< 16; j
++) {
1467 memset(dst
+ j
*stride
, v
, 16);
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
);
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
));
1488 printf("Incorrect 16x16 block type!\n");
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
);
1501 scan
= bink_patterns
[v_gb
.get_bits(4)];
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
++) {
1509 dst
[(pos
& 7) + (pos
>> 3) * stride
] = v
;
1512 for (j
= 0; j
< run
; j
++) {
1514 dst
[(pos
& 7) + (pos
>> 3) * stride
] = get_value(BINK_SRC_COLORS
);
1521 dst
[(pos
& 7) + (pos
>> 3)*stride
] = get_value(BINK_SRC_COLORS
);
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
);
1529 v
= v_gb
.get_bits(7);
1530 read_residue(block
, v
);
1531 add_pixels_clamped(block
, dst
, stride
);
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
);
1544 v
= get_value(BINK_SRC_COLORS
);
1545 for (i
= 0; i
< 8; i
++) {
1546 memset(dst
+ i
*stride
, v
, 8);
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
);
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
);
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
;
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;
1579 printf("Unknown block type!\n");
1584 v_gb
.get_bits_align32();
1587 if (video_frameskip
) {
1589 video_skippedframes
++;
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
));
1603 #include "plugindef.h"
1605 GEMRB_PLUGIN(0x316E2EDE, "BIK Video Player")
1606 PLUGIN_RESOURCE(BIKPlayer
, ".mve")