Plugin: Move deallocation into core.
[gemrb.git] / gemrb / plugins / BIKPlayer / BIKPlayer.h
blobf703f39f37efe30c67d15ea26a597538a3e6c68b
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.
21 #ifndef BIKPLAYER_H
22 #define BIKPLAYER_H
24 #include "win32def.h"
25 #include "globals.h"
26 #include "MoviePlayer.h"
27 #include "Interface.h"
28 #include "common.h"
29 #include "rational.h"
30 #include "GetBitContext.h"
31 #include "dsputil.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)
39 enum BinkAudFlags {
40 BINK_AUD_16BITS = 0x4000, ///< prefer 16-bit output
41 BINK_AUD_STEREO = 0x2000,
42 BINK_AUD_USEDCT = 0x1000
45 /**
46 * IDs for different data types used in Bink video codec
48 enum Sources {
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
59 BINK_NB_SRC
62 /**
63 * Bink video block types
65 enum BlockTypes {
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 {
79 /**
80 * pointer to the picture planes.
81 * This might be different from the first allocated byte
82 * - encoding:
83 * - decoding:
85 uint8_t *data[3];
86 int linesize[3];
87 } AVFrame;
89 typedef struct {
90 char signature[BIK_SIGNATURE_LEN];
91 ieDword filesize;
92 ieDword framecount;
93 ieDword maxframesize;
94 //ieDword framecount2; //unused by player
95 ieDword width;
96 ieDword height;
97 ieDword fps;
98 ieDword divider;
99 ieDword videoflag;
100 ieDword tracks; //BinkAudFlags
101 //optional if tracks == 1 (original had multiple tracks)
102 ieWord unknown2;
103 ieWord channels;
104 ieWord samplerate;
105 ieWord audioflag;
106 ieDword unknown4;
107 } binkheader;
109 typedef struct {
110 int keyframe;
111 ieDword pos;
112 ieDword size;
113 } binkframe;
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
122 } Bundle;
124 class BIKPlayer : public MoviePlayer {
126 private:
127 bool validVideo;
128 binkheader header;
129 std::vector<binkframe> frames;
130 ieByte *inbuff;
132 //subtitle and frame counting
133 ieDword maxRow;
134 ieDword rowCount;
135 ieDword frameCount;
137 //audio context (consider packing it in a struct)
138 unsigned int s_frame_len;
139 unsigned int s_channels;
140 int s_overlap_len;
141 int s_block_size;
142 unsigned int *s_bands;
143 float s_root;
144 unsigned int s_num_bands;
145 int s_first;
146 bool s_audio;
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
152 #pragma pack(pop)
154 float *s_coeffs_ptr[MAX_CHANNELS]; ///< pointers to the coeffs arrays for float_to_int16_interleave
156 union {
157 RDFTContext rdft;
158 DCTContext dct;
159 } s_trans;
160 GetBitContext s_gb;
162 //video context (consider packing it in a struct)
163 AVRational v_timebase;
164 long timer_last_sec;
165 long timer_last_usec;
166 unsigned int frame_wait;
167 bool video_rendered_frame;
168 unsigned int video_frameskip;
169 bool done;
170 int outputwidth, outputheight;
171 unsigned int video_skippedframes;
172 //bink specific
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
180 VLC bink_trees[16];
181 int16_t table[16 * 128][2];
182 GetBitContext v_gb;
183 AVFrame c_pic, c_last;
185 private:
186 void timer_start();
187 void timer_wait();
188 void segment_video_play();
189 bool next_frame();
190 int doPlay();
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,
194 unsigned int dsty);
195 int pollEvents();
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);
204 int ReadHeader();
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);
220 int EndAudio();
221 int EndVideo();
222 public:
223 BIKPlayer(void);
224 ~BIKPlayer(void);
225 bool Open(DataStream* stream);
226 void CallBackAtFrames(ieDword cnt, ieDword *arg, ieDword *arg2);
227 int Play();
230 #endif