xoreos  0.0.5
bink.h
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 /* Based on the Bink implementation in FFmpeg (<https://ffmpeg.org/)>,
26  * which is released under the terms of version 2 or later of the GNU
27  * Lesser General Public License.
28  *
29  * The original copyright notes in the files
30  * - libavformat/bink.c
31  * - libavcodec/bink.c
32  * - libavcodec/binkdata.h
33  * - libavcodec/binkdsp.c
34  * - libavcodec/binkdsp.h
35  * - libavcodec/binkaudio.c
36  * read as follows:
37  *
38  * Bink demuxer
39  * Copyright (c) 2008-2010 Peter Ross (pross@xvid.org)
40  * Copyright (c) 2009 Daniel Verkamp (daniel@drv.nu)
41  *
42  * Bink video decoder
43  * Copyright (c) 2009 Konstantin Shishkov
44  * Copyright (C) 2011 Peter Ross <pross@xvid.org>
45  *
46  * Bink video decoder
47  * Copyright (C) 2009 Konstantin Shishkov
48  *
49  * Bink DSP routines
50  * Copyright (c) 2009 Konstantin Shishkov
51  *
52  * Bink Audio decoder
53  * Copyright (c) 2007-2011 Peter Ross (pross@xvid.org)
54  * Copyright (c) 2009 Daniel Verkamp (daniel@drv.nu)
55  *
56  * FFmpeg is free software; you can redistribute it and/or
57  * modify it under the terms of the GNU Lesser General Public
58  * License as published by the Free Software Foundation; either
59  * version 2.1 of the License, or (at your option) any later version.
60  *
61  * FFmpeg is distributed in the hope that it will be useful,
62  * but WITHOUT ANY WARRANTY; without even the implied warranty of
63  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
64  * Lesser General Public License for more details.
65  *
66  * You should have received a copy of the GNU Lesser General Public
67  * License along with FFmpeg; if not, write to the Free Software
68  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
69  */
70 
71 #ifndef VIDEO_BINK_H
72 #define VIDEO_BINK_H
73 
74 #include <vector>
75 
76 #include "src/common/types.h"
77 #include "src/common/rational.h"
78 #include "src/common/scopedptr.h"
79 
80 #include "src/video/decoder.h"
81 
82 namespace Common {
83  class SeekableReadStream;
84  class BitStream;
85  class Huffman;
86 
87  class RDFT;
88  class DCT;
89 }
90 
91 namespace Sound {
92  class PacketizedAudioStream;
93 }
94 
95 namespace Video {
96 
98 class Bink : public VideoDecoder {
99 public:
101  ~Bink();
102 
103 protected:
104  void decodeNextTrackFrame(VideoTrack &track);
105  void checkAudioBuffer(AudioTrack &track, const Common::Timestamp &endTime);
106 
107 private:
108  static const int kAudioChannelsMax = 2;
109  static const int kAudioBlockSizeMax = (kAudioChannelsMax << 11);
110 
111  enum AudioCodec {
114  };
115 
117  struct AudioInfo {
119 
122 
125 
127 
128  bool first;
129 
132 
134 
137 
138  float root;
139 
142 
144 
147 
148  AudioInfo();
149  ~AudioInfo();
150  };
151 
153  struct VideoFrame {
154  bool keyFrame;
155 
158 
160 
161  VideoFrame();
162  ~VideoFrame();
163  };
164 
166 
167  std::vector<AudioInfo> _audioTracks;
168  std::vector<VideoFrame> _frames;
169 
171 
173  void load();
174 
175  class BinkVideoTrack : public FixedRateVideoTrack {
176  public:
177  BinkVideoTrack(uint32 width, uint32 height, uint32 frameCount, const Common::Rational &frameRate, bool swapPlanes, bool hasAlpha, uint32 id);
178 
179  uint32 getWidth() const { return _width; }
180  uint32 getHeight() const { return _height; }
181  int getCurFrame() const { return _curFrame; }
182  int getFrameCount() const { return _frameCount; }
183 
185  void decodePacket(Graphics::Surface &surface, VideoFrame &frame);
186 
187  protected:
189 
190  private:
192  struct DecodeContext {
194 
196 
199 
202 
205 
207 
208  int coordMap[64];
213  };
214 
216  enum Source {
226 
228  };
229 
231  enum BlockType {
242  };
243 
245  struct Huffman {
246  int index;
247  byte symbols[16];
248 
249  Huffman();
250  };
251 
253  struct Bundle {
254  int countLengths[2];
256 
258 
260 
264 
265  Bundle();
266  };
267 
270 
271  int _curFrame;
273 
275 
276  bool _swapPlanes;
277  bool _hasAlpha;
278 
280 
282 
284 
289 
292 
294  void initBundles();
295 
297  void initHuffman();
298 
300  void videoPacket(VideoFrame &video);
301 
303  void decodePlane(VideoFrame &video, int planeIdx, bool isChroma);
304 
306  void readBundle(VideoFrame &video, Source source);
307 
309  void readHuffman(VideoFrame &video, Huffman &huffman);
311  void mergeHuffmanSymbols(VideoFrame &video, byte *dst, const byte *src, int size);
312 
314  byte getHuffmanSymbol(VideoFrame &video, Huffman &huffman);
315 
317  int32 getBundleValue(Source source);
319  uint32 readBundleCount(VideoFrame &video, Bundle &bundle);
320 
321  // Handle the block types
322  void blockSkip (DecodeContext &ctx);
323  void blockScaledSkip (DecodeContext &ctx);
324  void blockScaledRun (DecodeContext &ctx);
325  void blockScaledIntra (DecodeContext &ctx);
326  void blockScaledFill (DecodeContext &ctx);
328  void blockScaledRaw (DecodeContext &ctx);
329  void blockScaled (DecodeContext &ctx);
330  void blockMotion (DecodeContext &ctx);
331  void blockRun (DecodeContext &ctx);
332  void blockResidue (DecodeContext &ctx);
333  void blockIntra (DecodeContext &ctx);
334  void blockFill (DecodeContext &ctx);
335  void blockInter (DecodeContext &ctx);
336  void blockPattern (DecodeContext &ctx);
337  void blockRaw (DecodeContext &ctx);
338 
339  // Read the bundles
340  void readRuns (VideoFrame &video, Bundle &bundle);
341  void readMotionValues(VideoFrame &video, Bundle &bundle);
342  void readBlockTypes (VideoFrame &video, Bundle &bundle);
343  void readPatterns (VideoFrame &video, Bundle &bundle);
344  void readColors (VideoFrame &video, Bundle &bundle);
345  void readDCS (VideoFrame &video, Bundle &bundle, int startBits, bool hasSign);
346  void readDCTCoeffs (VideoFrame &video, int16 *block, bool isIntra);
347  void readResidue (VideoFrame &video, int16 *block, int masksCount);
348 
349  // Bink video IDCT
350  void IDCT(int16 *block);
351  void IDCTPut(DecodeContext &ctx, int16 *block);
352  void IDCTAdd(DecodeContext &ctx, int16 *block);
353  };
354 
355  class BinkAudioTrack : public AudioTrack {
356  public:
357  BinkAudioTrack(size_t index, AudioInfo &audio);
358  ~BinkAudioTrack();
359 
360  bool canBufferData() const;
361 
363  void decodeAudio(Common::SeekableReadStream& bink, const std::vector<VideoFrame>& frames, const std::vector<AudioInfo>& audioTracks, const Common::Timestamp& endTime);
364 
365  protected:
367 
368  private:
369  size_t _index;
374 
375  float getFloat(Common::BitStream &bits);
376 
378  void audioBlock(Common::BitStream &bits, int16 *out);
380  void audioBlockDCT(Common::BitStream &bits);
382  void audioBlockRDFT(Common::BitStream &bits);
383 
384  void readAudioCoeffs(Common::BitStream &bits, float *coeffs);
385 
386  static void floatToInt16Interleave(int16 *dst, const float **src, uint32 length, uint8 channels);
387  };
388 
389  void initAudioTrack(AudioInfo &audio);
390 };
391 
392 } // End of namespace Video
393 
394 #endif // VIDEO_BINK_H
uint32 outSampleRate
Definition: bink.h:123
uint32 getHeight() const
Definition: bink.h:180
Huffman huffman
Huffman codebook.
Definition: bink.h:257
void readPatterns(VideoFrame &video, Bundle &bundle)
Definition: bink.cpp:998
Common::RDFT * rdft
Definition: bink.h:145
int countLength
Length of number of entries to decode (in bits) for the current plane.
Definition: bink.h:255
void readHuffman(VideoFrame &video, Huffman &huffman)
Read the symbols for a Huffman code.
Definition: bink.cpp:364
Common::BitStream * bits
Definition: bink.h:159
Definition: 2dafile.h:39
An abstract representation of an audio track.
Definition: decoder.h:302
A video frame.
Definition: bink.h:153
void readResidue(VideoFrame &video, int16 *block, int masksCount)
Reads 8x8 block with residue after motion compensation.
Definition: bink.cpp:1225
int _colLastVal
Value of the last decoded high nibble in color data types.
Definition: bink.h:288
A simple rational class that holds fractions.
Definition: rational.h:56
Pixel values used for different block types.
Definition: bink.h:219
void blockMotion(DecodeContext &ctx)
Definition: bink.cpp:791
Common::Rational _frameRate
The frame rate of the video.
Definition: bink.h:274
uint32 readBundleCount(VideoFrame &video, Bundle &bundle)
Read a count value out of a bundle.
Definition: bink.cpp:635
void decodePacket(Graphics::Surface &surface, VideoFrame &frame)
Decode a video packet.
Definition: bink.cpp:203
(Inverse) Discrete Cosine Transforms.
Definition: dct.h:66
uint8_t uint8
Definition: types.h:200
Block is composed from runs of colors with custom scan order.
Definition: bink.h:235
void blockRaw(DecodeContext &ctx)
Definition: bink.cpp:895
void readAudioCoeffs(Common::BitStream &bits, float *coeffs)
Definition: bink.cpp:1459
Common::ScopedArray< byte > data
Buffer for decoded symbols.
Definition: bink.h:259
void blockScaledPattern(DecodeContext &ctx)
Definition: bink.cpp:727
void readRuns(VideoFrame &video, Bundle &bundle)
Definition: bink.cpp:904
AudioCodec codec
Definition: bink.h:126
bool canBufferData() const
Definition: bink.cpp:1401
DC values for intrablocks with DCT.
Definition: bink.h:223
void blockScaledSkip(DecodeContext &ctx)
Definition: bink.cpp:654
int getFrameCount() const
Definition: bink.h:182
byte * curPtr
Pointer to the data that wasn&#39;t yet read.
Definition: bink.h:263
int getCurFrame() const
Definition: bink.h:181
void videoPacket(VideoFrame &video)
Decode a video packet.
int16 prevCoeffs[kAudioBlockSizeMax]
Definition: bink.h:141
void audioBlockRDFT(Common::BitStream &bits)
Decode a RDFT&#39;d audio block.
Definition: bink.cpp:1445
A generic interface for video decoders.
Definition: decoder.h:55
int16_t int16
Definition: types.h:201
A decoder for RAD Game Tools&#39; Bink videos.
Definition: bink.h:98
void blockScaledIntra(DecodeContext &ctx)
Definition: bink.cpp:698
uint32 * bands
Definition: bink.h:136
void blockRun(DecodeContext &ctx)
Definition: bink.cpp:804
A simple scoped smart pointer template.
int32 getBundleValue(Source source)
Get a direct value out of a bundle.
Definition: bink.cpp:621
uint32 _audioTrack
Audio track to use.
Definition: bink.h:170
void blockScaledFill(DecodeContext &ctx)
Definition: bink.cpp:719
Common::Timestamp _audioBuffered
Definition: bink.h:373
X components of motion value.
Definition: bink.h:221
Definition: game.h:37
void blockScaledRun(DecodeContext &ctx)
Definition: bink.cpp:662
void IDCTPut(DecodeContext &ctx, int16 *block)
Definition: bink.cpp:1598
void readBlockTypes(VideoFrame &video, Bundle &bundle)
Definition: bink.cpp:961
bool _hasAlpha
Do video frames have alpha?
Definition: bink.h:277
Source
IDs for different data types used in Bink video codec.
Definition: bink.h:216
void initBundles()
Initialize the bundles.
Definition: bink.cpp:583
uint16_t uint16
Definition: types.h:202
void readColors(VideoFrame &video, Bundle &bundle)
Definition: bink.cpp:1016
BlockType
Bink video block types.
Definition: bink.h:231
Huffman _colHighHuffman[16]
Huffman codebooks to use for decoding high nibbles in color data types.
Definition: bink.h:286
Block has size 16x16.
Definition: bink.h:233
An audio track.
Definition: bink.h:117
An AudioStream designed to work in terms of packets.
Definition: audiostream.h:271
Motion block with DCT applied to the difference.
Definition: bink.h:239
static void floatToInt16Interleave(int16 *dst, const float **src, uint32 length, uint8 channels)
Low-level type definitions to handle fixed width types portably.
std::vector< VideoFrame > _frames
All video frames.
Definition: bink.h:168
bool _swapPlanes
Are the planes ordered (A)YVU instead of (A)YUV?
Definition: bink.h:276
uint32 _id
The BIK FourCC.
Definition: bink.h:279
An abstract representation of a video track.
Definition: decoder.h:226
BinkAudioTrack(size_t index, AudioInfo &audio)
Definition: bink.cpp:1317
Common::ScopedArray< byte > _curPlanes[4]
The 4 color planes, YUVA, current frame.
Definition: bink.h:290
Common::DCT * dct
Definition: bink.h:146
float * coeffsPtr[kAudioChannelsMax]
Definition: bink.h:143
byte * curDec
Pointer to the data that wasn&#39;t yet decoded.
Definition: bink.h:262
Bundle _bundles[kSourceMAX]
Bundles for decoding all data types.
Definition: bink.h:281
void blockFill(DecodeContext &ctx)
Definition: bink.cpp:859
void blockPattern(DecodeContext &ctx)
Definition: bink.cpp:880
Common::ScopedPtr< Common::Huffman > _huffman[16]
The 16 Huffman codebooks used in Bink decoding.
Definition: bink.h:283
Sound::AudioStream * getAudioStream() const
Definition: bink.cpp:1329
16x16 block types (a subset of 8x8 block types).
Definition: bink.h:218
Generic audio input stream.
Definition: audiostream.h:70
Common::ScopedPtr< Common::SeekableReadStream > _bink
Definition: bink.h:165
void initHuffman()
Initialize the Huffman decoders.
Definition: bink.cpp:612
void readMotionValues(VideoFrame &video, Bundle &bundle)
Definition: bink.cpp:924
Block is copied from previous frame with some offset.
Definition: bink.h:234
uint32 getWidth() const
Definition: bink.h:179
int countLengths[2]
Lengths of number of entries to decode (in bits).
Definition: bink.h:254
Motion block with some difference added.
Definition: bink.h:236
Data structure used for decoding a single Bink data type.
Definition: bink.h:253
float coeffs[16 *kAudioBlockSizeMax]
Definition: bink.h:140
8-bit values for 2-color pattern fill.
Definition: bink.h:220
void readDCS(VideoFrame &video, Bundle &bundle, int startBits, bool hasSign)
Definition: bink.cpp:1060
byte * dataEnd
Pointer to the data end end.
Definition: bink.h:261
uint32_t uint32
Definition: types.h:204
void readBundle(VideoFrame &video, Source source)
Read/Initialize a bundle for decoding a plane.
Definition: bink.cpp:349
void decodeNextTrackFrame(VideoTrack &track)
Decode enough data for the next frame.
Definition: bink.cpp:163
Sound::PacketizedAudioStream * _audioStream
Definition: bink.h:371
Timestamps allow specifying points in time and measuring time intervals with a sub-millisecond granul...
Definition: timestamp.h:108
Common::Rational getFrameRate() const
Definition: bink.h:188
void blockInter(DecodeContext &ctx)
Definition: bink.cpp:867
byte symbols[16]
Huffman symbol => Bink symbol translation list.
Definition: bink.h:247
static const int kAudioBlockSizeMax
Definition: bink.h:109
int _curFrame
Current Frame.
Definition: bink.h:271
byte getHuffmanSymbol(VideoFrame &video, Huffman &huffman)
Read and translate a symbol out of a Huffman code.
Definition: bink.cpp:617
Y components of motion value.
Definition: bink.h:222
Block is filled with two colors following custom pattern.
Definition: bink.h:240
void load()
Load a Bink file.
Definition: bink.cpp:439
Rational number implementation.
DC values for interblocks with DCT.
Definition: bink.h:224
void checkAudioBuffer(AudioTrack &track, const Common::Timestamp &endTime)
Ensure that there is enough audio buffered in the given track to reach the given timestamp.
Definition: bink.cpp:199
void IDCTAdd(DecodeContext &ctx, int16 *block)
Definition: bink.cpp:1588
static const int kAudioChannelsMax
Definition: bink.h:108
void blockScaledRaw(DecodeContext &ctx)
Definition: bink.cpp:743
Data structure for decoding and translating Huffman&#39;d data.
Definition: bink.h:245
AudioCodec
Definition: bink.h:111
void audioBlock(Common::BitStream &bits, int16 *out)
Decode an audio block.
Definition: bink.cpp:1405
void IDCT(int16 *block)
Definition: bink.cpp:1577
BinkVideoTrack(uint32 width, uint32 height, uint32 frameCount, const Common::Rational &frameRate, bool swapPlanes, bool hasAlpha, uint32 id)
Definition: bink.cpp:1608
void decodeAudio(Common::SeekableReadStream &bink, const std::vector< VideoFrame > &frames, const std::vector< AudioInfo > &audioTracks, const Common::Timestamp &endTime)
Decode audio data up to endTime.
Definition: bink.cpp:1333
(Inverse) Real Discrete Fourier Transform.
Definition: rdft.h:64
Run lengths for special fill block.
Definition: bink.h:225
void blockResidue(DecodeContext &ctx)
Definition: bink.cpp:831
void decodePlane(VideoFrame &video, int planeIdx, bool isChroma)
Decode a plane.
Definition: bink.cpp:239
Common::ScopedArray< byte > _oldPlanes[4]
The 4 color planes, YUVA, last frame.
Definition: bink.h:291
float getFloat(Common::BitStream &bits)
Definition: bink.cpp:1390
Interface for a seekable & readable data stream.
Definition: readstream.h:265
void initAudioTrack(AudioInfo &audio)
Definition: bink.cpp:519
Bink(Common::SeekableReadStream *bink)
Definition: bink.cpp:154
void audioBlockDCT(Common::BitStream &bits)
Decode a DCT&#39;d audio block.
Definition: bink.cpp:1427
void blockSkip(DecodeContext &ctx)
Definition: bink.cpp:646
A bit stream.
Definition: bitstream.h:40
std::vector< AudioInfo > _audioTracks
All audio tracks.
Definition: bink.h:167
uint8 byte
Definition: types.h:209
int index
Index of the Huffman codebook to use.
Definition: bink.h:246
Block is filled with single color.
Definition: bink.h:238
void blockIntra(DecodeContext &ctx)
Definition: bink.cpp:848
Generic video decoder interface.
void blockScaled(DecodeContext &ctx)
Definition: bink.cpp:758
void readDCTCoeffs(VideoFrame &video, int16 *block, bool isIntra)
Reads 8x8 block of DCT coefficients.
Definition: bink.cpp:1126
int32_t int32
Definition: types.h:203
void mergeHuffmanSymbols(VideoFrame &video, byte *dst, const byte *src, int size)
Merge two Huffman symbol lists.
Definition: bink.cpp:418