1
/***************************************************************************
5
Audio/video compression and decompression helpers.
7
****************************************************************************
12
Redistribution and use in source and binary forms, with or without
13
modification, are permitted provided that the following conditions are
16
* Redistributions of source code must retain the above copyright
17
notice, this list of conditions and the following disclaimer.
18
* Redistributions in binary form must reproduce the above copyright
19
notice, this list of conditions and the following disclaimer in
20
the documentation and/or other materials provided with the
22
* Neither the name 'MAME' nor the names of its contributors may be
23
used to endorse or promote products derived from this software
24
without specific prior written permission.
26
THIS SOFTWARE IS PROVIDED BY AARON GILES ''AS IS'' AND ANY EXPRESS OR
27
IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
28
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
29
DISCLAIMED. IN NO EVENT SHALL AARON GILES BE LIABLE FOR ANY DIRECT,
30
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
31
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
32
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
34
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
35
IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36
POSSIBILITY OF SUCH DAMAGE.
38
***************************************************************************/
52
//**************************************************************************
54
//**************************************************************************
56
#define AVHUFF_USE_FLAC (1)
64
AVHERR_VIDEO_TOO_LARGE,
65
AVHERR_AUDIO_TOO_LARGE,
66
AVHERR_METADATA_TOO_LARGE,
68
AVHERR_COMPRESSION_ERROR,
69
AVHERR_TOO_MANY_CHANNELS,
70
AVHERR_INVALID_CONFIGURATION,
71
AVHERR_INVALID_PARAMETER,
72
AVHERR_BUFFER_TOO_SMALL
77
//**************************************************************************
79
//**************************************************************************
81
// ======================> av_codec_decompress_config
83
// decompression configuration
84
class avhuff_decompress_config
87
avhuff_decompress_config()
94
memset(audio, 0, sizeof(audio));
97
bitmap_yuy16 video; // pointer to video bitmap
98
UINT32 maxsamples; // maximum number of samples per channel
99
UINT32 * actsamples; // actual number of samples per channel
100
INT16 * audio[16]; // pointer to individual audio channels
101
UINT32 maxmetalength; // maximum length of metadata
102
UINT32 * actmetalength; // actual length of metadata
103
UINT8 * metadata; // pointer to metadata buffer
107
// ======================> avhuff_encoder
109
// core state for the codec
113
// construction/destruction
117
avhuff_error encode_data(const UINT8 *source, UINT8 *dest, UINT32 &complength);
120
static UINT32 raw_data_size(const UINT8 *data);
121
static UINT32 raw_data_size(UINT32 width, UINT32 height, UINT8 channels, UINT32 numsamples, UINT32 metadatasize = 0) { return 12 + channels * numsamples * 2 + width * height * 2; }
122
static avhuff_error assemble_data(dynamic_buffer &buffer, bitmap_yuy16 &bitmap, UINT8 channels, UINT32 numsamples, INT16 **samples, UINT8 *metadata = NULL, UINT32 metadatasize = 0);
125
// delta-RLE Huffman encoder
126
class deltarle_encoder
129
// construction/destruction
134
UINT16 *rle_and_histo_bitmap(const UINT8 *source, UINT32 items_per_row, UINT32 item_advance, UINT32 row_count);
137
void flush_rle() { m_rlecount = 0; }
138
void encode_one(bitstream_out &bitbuf, UINT16 *&rleptr);
139
huffman_error export_tree_rle(bitstream_out &bitbuf) { return m_encoder.export_tree_rle(bitbuf); }
144
huffman_encoder<256 + 16> m_encoder;
145
dynamic_array<UINT16> m_rlebuffer;
149
avhuff_error encode_audio(const UINT8 *source, int channels, int samples, UINT8 *dest, UINT8 *sizes);
150
avhuff_error encode_video(const UINT8 *source, int width, int height, UINT8 *dest, UINT32 &complength);
151
avhuff_error encode_video_lossless(const UINT8 *source, int width, int height, UINT8 *dest, UINT32 &complength);
153
// video encoding contexts
154
deltarle_encoder m_ycontext;
155
deltarle_encoder m_cbcontext;
156
deltarle_encoder m_crcontext;
158
// audio encoding contexts
159
dynamic_buffer m_audiobuffer;
161
flac_encoder m_flac_encoder;
163
huffman_8bit_encoder m_audiohi_encoder;
164
huffman_8bit_encoder m_audiolo_encoder;
169
// ======================> avhuff_decoder
171
// core state for the codec
175
// construction/destruction
179
void configure(const avhuff_decompress_config &config);
182
avhuff_error decode_data(const UINT8 *source, UINT32 complength, UINT8 *dest);
185
// delta-RLE Huffman decoder
186
class deltarle_decoder
189
// construction/destruction
191
: m_rlecount(0), m_prevdata(0) { }
194
void reset() { m_rlecount = m_prevdata = 0; }
197
void flush_rle() { m_rlecount = 0; }
198
UINT32 decode_one(bitstream_in &bitbuf);
199
huffman_error import_tree_rle(bitstream_in &bitbuf) { return m_decoder.import_tree_rle(bitbuf); }
205
huffman_decoder<256 + 16> m_decoder;
210
avhuff_error decode_audio(int channels, int samples, const UINT8 *source, UINT8 **dest, UINT32 dxor, const UINT8 *sizes);
211
avhuff_error decode_video(int width, int height, const UINT8 *source, UINT32 complength, UINT8 *dest, UINT32 dstride, UINT32 dxor);
212
avhuff_error decode_video_lossless(int width, int height, const UINT8 *source, UINT32 complength, UINT8 *dest, UINT32 dstride, UINT32 dxor);
215
avhuff_decompress_config m_config;
217
// video decoding contexts
218
deltarle_decoder m_ycontext;
219
deltarle_decoder m_cbcontext;
220
deltarle_decoder m_crcontext;
222
// audio decoding contexts
223
huffman_8bit_decoder m_audiohi_decoder;
224
huffman_8bit_decoder m_audiolo_decoder;
226
flac_decoder m_flac_decoder;