1 /* GemRB - Infinity Engine Emulator
2 * Copyright (C) 2003 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.
26 #include "MoviePlayer.h"
27 #include "Interface.h"
30 #include "GetBitContext.h"
33 #define BIK_SIGNATURE_LEN 4
34 #define BIK_SIGNATURE_DATA "BIKi"
36 #define MAX_CHANNELS 2
37 #define BINK_BLOCK_MAX_SIZE (MAX_CHANNELS << 11)
40 BINK_AUD_16BITS
= 0x4000, ///< prefer 16-bit output
41 BINK_AUD_STEREO
= 0x2000,
42 BINK_AUD_USEDCT
= 0x1000
46 * IDs for different data types used in Bink video codec
49 BINK_SRC_BLOCK_TYPES
= 0, ///< 8x8 block types
50 BINK_SRC_SUB_BLOCK_TYPES
, ///< 16x16 block types (a subset of 8x8 block types)
51 BINK_SRC_COLORS
, ///< pixel values used for different block types
52 BINK_SRC_PATTERN
, ///< 8-bit values for 2-colour pattern fill
53 BINK_SRC_X_OFF
, ///< X components of motion value
54 BINK_SRC_Y_OFF
, ///< Y components of motion value
55 BINK_SRC_INTRA_DC
, ///< DC values for intrablocks with DCT
56 BINK_SRC_INTER_DC
, ///< DC values for intrablocks with DCT
57 BINK_SRC_RUN
, ///< run lengths for special fill block
63 * Bink video block types
66 SKIP_BLOCK
= 0, ///< skipped block
67 SCALED_BLOCK
, ///< block has size 16x16
68 MOTION_BLOCK
, ///< block is copied from previous frame with some offset
69 RUN_BLOCK
, ///< block is composed from runs of colours with custom scan order
70 RESIDUE_BLOCK
, ///< motion block with some difference added
71 INTRA_BLOCK
, ///< intra DCT block
72 FILL_BLOCK
, ///< block is filled with single colour
73 INTER_BLOCK
, ///< motion block with DCT applied to the difference
74 PATTERN_BLOCK
, ///< block is filled with two colours following custom pattern
75 RAW_BLOCK
///< uncoded 8x8 block
78 typedef struct AVFrame
{
80 * pointer to the picture planes.
81 * This might be different from the first allocated byte
90 char signature
[BIK_SIGNATURE_LEN
];
94 //ieDword framecount2; //unused by player
100 ieDword tracks
; //BinkAudFlags
101 //optional if tracks == 1 (original had multiple tracks)
115 typedef struct Bundle
{
116 int len
; ///< length of number of entries to decode (in bits)
117 Tree tree
; ///< Huffman tree-related data
118 uint8_t *data
; ///< buffer for decoded symbols
119 uint8_t *data_end
; ///< buffer end
120 uint8_t *cur_dec
; ///< pointer to the not yet decoded part of the buffer
121 uint8_t *cur_ptr
; ///< pointer to the data that is not read from buffer yet
124 class BIKPlayer
: public MoviePlayer
{
129 std::vector
<binkframe
> frames
;
132 //subtitle and frame counting
137 //audio context (consider packing it in a struct)
138 unsigned int s_frame_len
;
139 unsigned int s_channels
;
142 unsigned int *s_bands
;
144 unsigned int s_num_bands
;
147 int s_stream
; //openal stream handle
149 #pragma pack(push,16)
150 FFTSample s_coeffs
[BINK_BLOCK_MAX_SIZE
];
151 short s_previous
[BINK_BLOCK_MAX_SIZE
/ 16]; ///< coeffs from previous audio block
154 float *s_coeffs_ptr
[MAX_CHANNELS
]; ///< pointers to the coeffs arrays for float_to_int16_interleave
162 //video context (consider packing it in a struct)
163 AVRational v_timebase
;
165 long timer_last_usec
;
166 unsigned int frame_wait
;
167 bool video_rendered_frame
;
168 unsigned int video_frameskip
;
170 int outputwidth
, outputheight
;
171 unsigned int video_skippedframes
;
173 uint8_t c_idct_permutation
[64];
174 ScanTable c_scantable
;
175 Bundle c_bundle
[BINK_NB_SRC
]; ///< bundles for decoding all data types
176 Tree c_col_high
[16]; ///< trees for decoding high nibble in "colours" data type
177 int c_col_lastval
; ///< value of last decoded high nibble in "colours" data type
179 //huffman trees for video decoding
181 int16_t table
[16 * 128][2];
183 AVFrame c_pic
, c_last
;
188 void segment_video_play();
191 unsigned int fileRead(unsigned int pos
, void* buf
, unsigned int count
);
192 void showFrame(unsigned char** buf
, unsigned int *strides
, unsigned int bufw
,
193 unsigned int bufh
, unsigned int w
, unsigned int h
, unsigned int dstx
,
196 int setAudioStream();
197 void freeAudioStream(int stream
);
198 void queueBuffer(int stream
, unsigned short bits
,
199 int channels
, short* memory
, int size
, int samplerate
);
200 int sound_init(bool need_init
);
201 void ff_init_scantable(uint8_t *permutation
, ScanTable
*st
, const uint8_t *src_scantable
);
202 int video_init(int w
, int h
);
203 void av_set_pts_info(AVRational
&time_base
, unsigned int pts_num
, unsigned int pts_den
);
205 void DecodeBlock(short *out
);
206 int DecodeAudioFrame(void *data
, int data_size
);
207 inline int get_value(int bundle
);
208 int read_dct_coeffs(DCTELEM block
[64], const uint8_t *scan
);
209 int read_residue(DCTELEM block
[64], int masks_count
);
210 int read_runs(Bundle
*b
);
211 int read_motion_values(Bundle
*b
);
212 int read_block_types(Bundle
*b
);
213 int read_patterns(Bundle
*b
);
214 int read_colors(Bundle
*b
);
215 int read_dcs(Bundle
*b
, int start_bits
, int has_sign
);
216 int get_vlc2(int16_t (*table
)[2], int bits
, int max_depth
);
217 void read_bundle(int bundle_num
);
218 void init_lengths(int width
, int bw
);
219 int DecodeVideoFrame(void *data
, int data_size
);
225 bool Open(DataStream
* stream
);
226 void CallBackAtFrames(ieDword cnt
, ieDword
*arg
, ieDword
*arg2
);