Debian Codegraph @

src/engine/smk_decoder.cpp

raw symbols SCIP
1 /***************************************************************************
2 * fheroes2: https://github.com/ihhub/fheroes2 *
3 * Copyright (C) 2020 - 2025 *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21 #include "smk_decoder.h"
22
23 #include <algorithm>
24 #include <array>
25 #include <cassert>
26 #include <cstdint>
27 #include <cstdio>
28 #include <cstring>
29 #include <ostream>
30
31 #include "exception.h"
32 #include "image.h"
33 #include "logging.h"
34 #include "serialize.h"
35
36 namespace
37 {
38 const size_t audioHeaderSize = 44;
39
40 void verifyVideoFile( const std::string & filePath )
41 {
42 if ( filePath.empty() ) {
43 // Why are you trying to even play a file from an empty path?
44 assert( 0 );
45 return;
46 }
47
48 // Verify that the file is valid. We use C-code on purpose since libsmacker library does the same.
49 const std::unique_ptr<std::FILE, int ( * )( std::FILE * )> file( std::fopen( filePath.c_str(), "rb" ), []( std::FILE * f ) { return std::fclose( f ); } );
50 if ( !file ) {
51 return;
52 }
53
54 std::fseek( file.get(), 0, SEEK_END );
55 const size_t fileSize = std::ftell( file.get() );
56
57 // According to https://wiki.multimedia.cx/index.php/Smacker the minimum size of a file must be 56 bytes.
58 if ( fileSize < 56 ) {
59 throw fheroes2::InvalidDataResources( "Video file " + filePath + " is being corrupted. Make sure that you own an official version of the game." );
60 }
61 }
62 }
63
64 SMKVideoSequence::SMKVideoSequence( const std::string & filePath )
65 {
66 verifyVideoFile( filePath );
67
68 _videoFile.reset( smk_open_file( filePath.c_str(), SMK_MODE_MEMORY ) );
69 if ( !_videoFile ) {
70 return;
71 }
72
73 const uint8_t audioChannelCount = 7;
74
75 uint8_t trackMask = 0;
76 uint8_t channelsPerTrack[audioChannelCount] = { 0 };
77 uint8_t audioBitDepth[audioChannelCount] = { 0 };
78 unsigned long audioRate[audioChannelCount] = { 0 };
79 std::array<std::vector<uint8_t>, audioChannelCount> soundBuffer;
80
81 unsigned long width = 0;
82 unsigned long height = 0;
83 unsigned char scaledYMode = 1;
84
85 if ( const signed char returnValue = smk_info_all( _videoFile.get(), nullptr, &_frameCount, &_microsecondsPerFrame ); returnValue < 0 ) {
86 ERROR_LOG( "smk_info_all() failed with error code: " << static_cast<int>( returnValue ) )
87 }
88
89 if ( const signed char returnValue = smk_info_video( _videoFile.get(), &width, &height, &scaledYMode ); returnValue < 0 ) {
90 ERROR_LOG( "smk_info_video() failed with error code: " << static_cast<int>( returnValue ) )
91 }
92
93 _heightScaleFactor = scaledYMode;
94
95 if ( _heightScaleFactor < 1 ) {
96 // This is some corrupted video file. Let's still proceed with it.
97 _heightScaleFactor = 1;
98 }
99 else if ( _heightScaleFactor > 2 ) {
100 // None of formats supports scaling more than 2.
101 _heightScaleFactor = 2;
102 }
103
104 _width = static_cast<int32_t>( width );
105 _height = static_cast<int32_t>( height ) * _heightScaleFactor;
106
107 if ( const signed char returnValue = smk_info_audio( _videoFile.get(), &trackMask, channelsPerTrack, audioBitDepth, audioRate ); returnValue < 0 ) {
108 ERROR_LOG( "smk_info_audio() failed with error code: " << static_cast<int>( returnValue ) )
109 }
110
111 if ( _microsecondsPerFrame < 1 ) {
112 // Since the value is not set let's set a default value for 15 FPS.
113 _microsecondsPerFrame = 1000000.0 / 15.0;
114 }
115
116 for ( uint8_t i = 0; i < audioChannelCount; ++i ) {
117 if ( trackMask & ( 1 << i ) ) {
118 if ( const signed char returnValue = smk_enable_audio( _videoFile.get(), i, 1 ); returnValue < 0 ) {
119 ERROR_LOG( "smk_enable_audio() failed with error code: " << static_cast<int>( returnValue ) )
120 }
121 }
122 }
123
124 // Disable video reading.
125 if ( const signed char returnValue = smk_enable_video( _videoFile.get(), 0 ); returnValue < 0 ) {
126 ERROR_LOG( "smk_enable_video() failed with error code: " << static_cast<int>( returnValue ) )
127 }
128
129 if ( const signed char returnValue = smk_first( _videoFile.get() ); returnValue < 0 ) {
130 ERROR_LOG( "smk_first() failed with error code: " << static_cast<int>( returnValue ) )
131 }
132
133 unsigned long currentFrame = 0;
134
135 if ( const signed char returnValue = smk_info_all( _videoFile.get(), &currentFrame, nullptr, nullptr ); returnValue < 0 ) {
136 ERROR_LOG( "smk_info_all() failed with error code: " << static_cast<int>( returnValue ) )
137 }
138
139 for ( uint8_t i = 0; i < audioChannelCount; ++i ) {
140 if ( trackMask & ( 1 << i ) ) {
141 const unsigned long length = smk_get_audio_size( _videoFile.get(), i );
142 if ( length == 0 ) {
143 continue;
144 }
145
146 if ( soundBuffer[i].empty() ) {
147 soundBuffer[i].resize( audioHeaderSize );
148 }
149
150 const uint8_t * data = smk_get_audio( _videoFile.get(), i );
151 soundBuffer[i].insert( soundBuffer[i].end(), data, data + length );
152 }
153 }
154
155 for ( currentFrame = 1; currentFrame < _frameCount; ++currentFrame ) {
156 if ( const signed char returnValue = smk_next( _videoFile.get() ); returnValue < 0 ) {
157 ERROR_LOG( "smk_next() failed with error code: " << static_cast<int>( returnValue ) )
158 }
159
160 for ( uint8_t i = 0; i < audioChannelCount; ++i ) {
161 if ( trackMask & ( 1 << i ) ) {
162 const unsigned long length = smk_get_audio_size( _videoFile.get(), i );
163 if ( length == 0 ) {
164 continue;
165 }
166
167 if ( soundBuffer[i].empty() ) {
168 soundBuffer[i].resize( audioHeaderSize );
169 }
170
171 const uint8_t * data = smk_get_audio( _videoFile.get(), i );
172 soundBuffer[i].insert( soundBuffer[i].end(), data, data + length );
173 }
174 }
175 }
176
177 size_t channelCount = 0;
178 for ( size_t i = 0; i < soundBuffer.size(); ++i ) {
179 if ( !soundBuffer[i].empty() ) {
180 ++channelCount;
181 }
182 }
183 _audioChannel.resize( channelCount );
184
185 channelCount = 0;
186
187 // Compose the soundtrack
188 for ( size_t i = 0; i < soundBuffer.size(); ++i ) {
189 if ( !soundBuffer[i].empty() ) {
190 std::vector<uint8_t> & wavData = _audioChannel[channelCount];
191 std::swap( wavData, soundBuffer[i] );
192
193 const uint32_t originalSize = static_cast<uint32_t>( wavData.size() - audioHeaderSize );
194
195 ++channelCount;
196
197 RWStreamBuf wavHeader( audioHeaderSize );
198 wavHeader.putLE32( 0x46464952 ); // RIFF marker ("RIFF")
199 wavHeader.putLE32( originalSize + 0x24 ); // Total size minus the size of this and previous fields
200 wavHeader.putLE32( 0x45564157 ); // File type header ("WAVE")
201 wavHeader.putLE32( 0x20746D66 ); // Format sub-chunk marker ("fmt ")
202 wavHeader.putLE32( 0x10 ); // Size of the format sub-chunk
203 wavHeader.putLE16( 0x01 ); // Audio format (1 for PCM)
204 wavHeader.putLE16( channelsPerTrack[i] ); // Number of channels
205 wavHeader.putLE32( audioRate[i] ); // Sample rate
206 wavHeader.putLE32( audioRate[i] * audioBitDepth[i] * channelsPerTrack[i] / 8 ); // Byte rate
207 wavHeader.putLE16( audioBitDepth[i] * channelsPerTrack[i] / 8 ); // Block align
208 wavHeader.putLE16( audioBitDepth[i] ); // Bits per sample
209 wavHeader.putLE32( 0x61746164 ); // Data sub-chunk marker ("data")
210 wavHeader.putLE32( originalSize ); // Size of the data sub-chunk
211
212 memcpy( wavData.data(), wavHeader.data(), audioHeaderSize );
213 }
214 }
215
216 // Enable video reading.
217 if ( const signed char returnValue = smk_enable_video( _videoFile.get(), 1 ); returnValue < 0 ) {
218 ERROR_LOG( "smk_enable_video() failed with error code: " << static_cast<int>( returnValue ) )
219 }
220
221 if ( const signed char returnValue = smk_first( _videoFile.get() ); returnValue < 0 ) {
222 ERROR_LOG( "smk_first() failed with error code: " << static_cast<int>( returnValue ) )
223 }
224 }
225
226 void SMKVideoSequence::resetFrame()
227 {
228 if ( !_videoFile ) {
229 return;
230 }
231
232 if ( const signed char returnValue = smk_first( _videoFile.get() ); returnValue < 0 ) {
233 ERROR_LOG( "smk_first() failed with error code: " << static_cast<int>( returnValue ) )
234 }
235
236 _currentFrameId = 0;
237 }
238
239 void SMKVideoSequence::getCurrentFrame( fheroes2::Image & image, const int32_t x, const int32_t y, int32_t & width, int32_t & height,
240 std::vector<uint8_t> & palette ) const
241 {
242 if ( !_videoFile || image.empty() || x < 0 || y < 0 || x >= image.width() || y >= image.height() || !image.singleLayer() ) {
243 width = 0;
244 height = 0;
245 return;
246 }
247
248 const uint8_t * data = smk_get_video( _videoFile.get() );
249 const uint8_t * paletteData = smk_get_palette( _videoFile.get() );
250
251 width = _width;
252 height = _height;
253
254 assert( _heightScaleFactor == 1 || _heightScaleFactor == 2 );
255 assert( ( _height % _heightScaleFactor ) == 0 );
256
257 if ( image.width() == _width && image.height() == _height && x == 0 && y == 0 ) {
258 const size_t size = static_cast<size_t>( _width ) * _height;
259
260 if ( _heightScaleFactor == 2 ) {
261 assert( ( height % _heightScaleFactor ) == 0 );
262
263 const uint8_t * inY = data;
264 uint8_t * outY = image.image();
265 const uint8_t * outYEnd = outY + height * _width;
266
267 for ( ; outY != outYEnd; outY += _heightScaleFactor * _width, inY += _width ) {
268 std::copy( inY, inY + width, outY );
269 }
270 }
271 else {
272 std::copy( data, data + size, image.image() );
273 }
274 }
275 else {
276 if ( x + width > image.width() ) {
277 width = image.width() - x;
278 }
279 if ( y + height > image.height() ) {
280 height = image.height() - y;
281 }
282
283 const uint8_t * inY = data;
284
285 const int32_t imageWidth = image.width();
286 uint8_t * outY = image.image() + x + y * imageWidth;
287 const uint8_t * outYEnd = outY + ( height / _heightScaleFactor ) * _heightScaleFactor * imageWidth;
288
289 if ( _heightScaleFactor == 2 ) {
290 assert( ( height % _heightScaleFactor ) == 0 );
291
292 for ( ; outY != outYEnd; outY += _heightScaleFactor * imageWidth, inY += _width ) {
293 std::copy( inY, inY + width, outY );
294 }
295 }
296 else {
297 for ( ; outY != outYEnd; outY += imageWidth, inY += _width ) {
298 std::copy( inY, inY + width, outY );
299 }
300 }
301 }
302
303 palette.resize( 256 * 3 );
304 memcpy( palette.data(), paletteData, 256 * 3 );
305 }
306
307 void SMKVideoSequence::skipFrame()
308 {
309 ++_currentFrameId;
310 if ( _currentFrameId < _frameCount ) {
311 if ( const signed char returnValue = smk_next( _videoFile.get() ); returnValue < 0 ) {
312 ERROR_LOG( "smk_next() failed with error code: " << static_cast<int>( returnValue ) )
313 }
314 }
315 }
316
317 std::vector<uint8_t> SMKVideoSequence::getCurrentPalette() const
318 {
319 assert( _videoFile );
320
321 const uint8_t * paletteData = smk_get_palette( _videoFile.get() );
322 assert( paletteData != nullptr );
323
324 return std::vector<uint8_t>( paletteData, paletteData + 256 * 3 );
325 }