-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathqtvr_decoder.cpp
More file actions
2040 lines (1628 loc) · 62.8 KB
/
qtvr_decoder.cpp
File metadata and controls
2040 lines (1628 loc) · 62.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
//
// Partially based on ffmpeg code.
//
// Copyright (c) 2001 Fabrice Bellard.
// First version by Francois Revol revol@free.fr
// Seek function by Gael Chardon gael.dev@4now.net
//
#include "video/qt_decoder.h"
#include "video/qt_data.h"
#include "audio/audiostream.h"
#include "common/archive.h"
#include "common/config-manager.h"
#include "common/debug.h"
#include "common/events.h"
#include "common/file.h"
#include "common/keyboard.h"
#include "common/memstream.h"
#include "common/system.h"
#include "common/textconsole.h"
#include "common/timer.h"
#include "common/util.h"
#include "common/compression/unzip.h"
#include "graphics/cursorman.h"
#include "image/icocur.h"
#include "image/png.h"
// Video codecs
#include "image/codecs/codec.h"
namespace Video {
static const char * const MACGUI_DATA_BUNDLE = "macgui.dat";
////////////////////////////////////////////
// QuickTimeDecoder methods related to QTVR
////////////////////////////////////////////
static float readAppleFloatField(Common::SeekableReadStream *stream) {
int16 a = stream->readSint16BE();
uint16 b = stream->readUint16BE();
float value = (float)a + (float)b / 65536.0f;
return value;
}
QuickTimeDecoder::PanoSampleDesc::PanoSampleDesc(Common::QuickTimeParser::Track *parentTrack, uint32 codecTag) : Common::QuickTimeParser::SampleDesc(parentTrack, codecTag) {
}
QuickTimeDecoder::PanoSampleDesc::~PanoSampleDesc() {
}
//
// Panorama Track Sample Description
//
// Source: https://developer.apple.com/library/archive/technotes/tn/tn1035.html
Common::QuickTimeParser::SampleDesc *QuickTimeDecoder::readPanoSampleDesc(Common::QuickTimeParser::Track *track, uint32 format, uint32 descSize) {
PanoSampleDesc *entry = new PanoSampleDesc(track, format);
entry->_majorVersion = _fd->readSint16BE(); // must be zero, also observed to be 1
entry->_minorVersion = _fd->readSint16BE();
entry->_sceneTrackID = _fd->readSint32BE();
entry->_loResSceneTrackID = _fd->readSint32BE();
_fd->read(entry->_reserved3, 4 * 6);
entry->_hotSpotTrackID = _fd->readSint32BE();
_fd->read(entry->_reserved4, 4 * 9);
entry->_hPanStart = readAppleFloatField(_fd);
entry->_hPanEnd = readAppleFloatField(_fd);
entry->_vPanTop = readAppleFloatField(_fd);
entry->_vPanBottom = readAppleFloatField(_fd);
entry->_minimumZoom = readAppleFloatField(_fd);
entry->_maximumZoom = readAppleFloatField(_fd);
// info for the highest res version of scene track
entry->_sceneSizeX = _fd->readUint32BE();
entry->_sceneSizeY = _fd->readUint32BE();
entry->_numFrames = _fd->readUint32BE();
entry->_reserved5 = _fd->readSint16BE();
entry->_sceneNumFramesX = _fd->readSint16BE();
entry->_sceneNumFramesY = _fd->readSint16BE();
entry->_sceneColorDepth = _fd->readSint16BE();
// info for the highest rest version of hotSpot track
entry->_hotSpotSizeX = _fd->readSint32BE(); // pixel width of the hot spot panorama
entry->_hotSpotSizeY = _fd->readSint32BE(); // pixel height of the hot spot panorama
entry->_reserved6 = _fd->readSint16BE();
entry->_hotSpotNumFramesX = _fd->readSint16BE(); // diced frames wide
entry->_hotSpotNumFramesY = _fd->readSint16BE(); // dices frame high
entry->_hotSpotColorDepth = _fd->readSint16BE(); // must be 8
if (entry->_minimumZoom == 0.0)
entry->_minimumZoom = 2.0;
if (entry->_maximumZoom == 0.0)
entry->_maximumZoom = abs(entry->_vPanTop - entry->_vPanBottom);
debugC(2, kDebugLevelGVideo, " version: %d.%d sceneTrackID: %d loResSceneTrackID: %d hotSpotTrackID: %d",
entry->_majorVersion, entry->_minorVersion, entry->_sceneTrackID, entry->_loResSceneTrackID, entry->_hotSpotTrackID);
debugC(2, kDebugLevelGVideo, " hpan: [%f - %f] vpan: [%f - %f] zoom: [%f - %f]",
entry->_hPanStart, entry->_hPanEnd, entry->_vPanTop, entry->_vPanBottom, entry->_minimumZoom, entry->_maximumZoom);
debugC(2, kDebugLevelGVideo, " sceneDims: [%d x %d] frames: %d sceneFrm: [%d x %d] bpp: %d",
entry->_sceneSizeX, entry->_sceneSizeY, entry->_numFrames, entry->_sceneNumFramesX,
entry->_sceneNumFramesY, entry->_sceneColorDepth);
debugC(2, kDebugLevelGVideo, " hotspotDims: [%d x %d] hotspotFrm: [%d x %d] bpp: %d",
entry->_hotSpotSizeX, entry->_hotSpotSizeY, entry->_hotSpotNumFramesX, entry->_hotSpotNumFramesY,
entry->_hotSpotColorDepth);
return entry;
}
void QuickTimeDecoder::closeQTVR() {
delete _dataBundle;
_dataBundle = nullptr;
cleanupCursors();
}
void QuickTimeDecoder::renderHotspots(bool mode) {
_renderHotspots = mode;
((PanoTrackHandler *)getTrack(_panoTrack->targetTrack))->setDirty();
}
void QuickTimeDecoder::setQuality(float quality) {
_quality = CLIP<float>(quality, 0.0f, 4.0f);
// 4.0 Highest quality rendering. The rendered image is fully anti-aliased.
//
// 2.0 The rendered image is partially anti-aliased.
//
// 1.0 The rendered image is not anti-aliased.
//
// 0.0 Images are rendered at quality 1.0 when the user is interactively
// panning or zooming, and are automatically updated at quality
// 4.0 during idle time when the user stops panning or zooming.
// All other updates are rendered at quality 4.0.
PanoTrackHandler *track = ((PanoTrackHandler *)getTrack(_panoTrack->targetTrack));
track->setDirty();
}
void QuickTimeDecoder::setWarpMode(int warpMode) {
// Curretnly the warp mode 1 is not implemented correctly
// So, forcing the warp mode 1 to warp mode 2
if (warpMode == 1) warpMode = 2;
_warpMode = CLIP(warpMode, 0, 2);
// 2 Two-dimensional warping. This produces perspectively correct
// images from a panoramic source picture.
//
// 1 One-dimensional warping.
//
// 0 No warping. This reproduces the source panorama directly,
// without warping it at all.
PanoTrackHandler *track = ((PanoTrackHandler *)getTrack(_panoTrack->targetTrack));
track->setDirty();
}
void QuickTimeDecoder::setTransitionMode(Common::String mode) {
if (mode.equalsIgnoreCase("swing")) {
_transitionMode = kTransitionModeSwing;
} else {
_transitionMode = kTransitionModeNormal;
}
// normal The new view is imaged and displayed. No transition effect is
// used. The user sees a "cut" from the current view to the new
// view.
//
// swing If the new view is in the current node, the view point moves
// smoothly from the current view to the new view, taking the
// shortest path if the panorama wraps around. The speed of the
// swing is controlled by the transitionSpeed property. If the new
// view is in a different node, the new view is imaged and
// displayed with a "normal" transition.
((PanoTrackHandler *)getTrack(_panoTrack->targetTrack))->setDirty();
}
void QuickTimeDecoder::setTransitionSpeed(float speed) {
_transitionSpeed = speed;
// The TransitionSpeed is a floating point quantity that provides the slowest swing transition
// at 1.0 and faster transitions at higher values. On mid-range computers, a rate of 4.0
// performs well off CD-ROM.
//
// If the TransitionSpeed property value is set to a negative number, swing updates will act
// the same as normal updates
((PanoTrackHandler *)getTrack(_panoTrack->targetTrack))->setDirty();
}
Common::String QuickTimeDecoder::getUpdateMode() const {
switch (_updateMode) {
case kUpdateModeUpdateBoth:
return "updateBoth";
case kUpdateModeOffscreenOnly:
return "offscreenOnly";
case kUpdateModeFromOffscreen:
return "fromOffscreen";
case kUpdateModeDirectToScreen:
return "directToScreen";
case kUpdateModeNormal:
default:
return "normal";
}
}
void QuickTimeDecoder::setUpdateMode(Common::String mode) {
if (mode.equalsIgnoreCase("updateBoth"))
_updateMode = kUpdateModeUpdateBoth;
else if (mode.equalsIgnoreCase("offscreenOnly"))
_updateMode = kUpdateModeOffscreenOnly;
else if (mode.equalsIgnoreCase("fromOffscreen"))
_updateMode = kUpdateModeFromOffscreen;
else if (mode.equalsIgnoreCase("directToScreen"))
_updateMode = kUpdateModeDirectToScreen;
else
_updateMode = kUpdateModeNormal;
warning("STUB: Update mode set to '%s'", getUpdateMode().c_str());
// normal Images are computed and displayed directly onto the screen
// while interactively panning and zooming. Provides the fastest
// overall frame rate, but individual frame draw times may be
// relatively slow for high-quality images on lower performance
// systems. Programmatic updates are displayed into the offscreen
// buffer, and then onto the screen.
//
// update Both Images are computed and displayed into the offscreen buffer,and
// then onto the screen. The use of the back buffer reduces the
// overall frame rate but provides the fastest imaging time for each
// frame.
//
// offscreenOnly Images are computed and displayed into the offscreen buffer
// only, and are not copied to the screen. Useful for preparing for a
// screen refresh that will have to happen quickly.
//
// fromOffscreen The offscreen buffer is copied directly to the screen. The offscreen
// buffer is refreshed only if it is out of date. The offscreen buffer is
// automatically updated if necessary to keep it in sync with the
// screen image.
//
// directToScreen Images are computed and displayed directly to the screen,
// without changing the offscreen buffer. Provides the fastest
// overall frame rate, but individual frame draw times may be
// relatively slow for high-quality images on lower performance
// systems.
((PanoTrackHandler *)getTrack(_panoTrack->targetTrack))->setDirty();
}
void QuickTimeDecoder::setTargetSize(uint16 w, uint16 h) {
if (!isVR())
warning("QuickTimeDecoder::setTargetSize() called on non-VR movie");
if (_qtvrType == QTVRType::PANORAMA) {
_width = w;
_height = h;
setFOV(_fov);
} else if (_qtvrType == QTVRType::OBJECT) {
if (_width != w)
_scaleFactorX *= Common::Rational(_width, w);
if (_height != h)
_scaleFactorY *= Common::Rational(_height, h);
_width = w;
_height = h;
}
// Set up the _hfov properly for the very first frame of the pano
// After our setFOV will handle the _hfov
_hfov = _fov * (float)_width / (float)_height;
}
void QuickTimeDecoder::setPanAngle(float angle) {
PanoSampleDesc *desc = (PanoSampleDesc *)_panoTrack->sampleDescs[0];
float panRange = abs(desc->_hPanEnd - desc->_hPanStart);
angle = fmod(angle, panRange);
if (desc->_hPanStart != desc->_hPanEnd && (desc->_hPanStart != 0.0 || desc->_hPanEnd != 360.0)) {
if (angle < desc->_hPanStart + _hfov) {
angle = desc->_hPanStart + _hfov;
} else if (angle > desc->_hPanEnd - _hfov) {
angle = desc->_hPanEnd - _hfov;
}
}
if (_panAngle != angle) {
PanoTrackHandler *track = (PanoTrackHandler *)getTrack(_panoTrack->targetTrack);
track->_currentPanAngle = _panAngle;
_panAngle = angle;
track->setDirty();
}
}
void QuickTimeDecoder::setTiltAngle(float angle) {
PanoSampleDesc *desc = (PanoSampleDesc *)_panoTrack->sampleDescs[0];
if (angle < desc->_vPanBottom + _fov / 2) {
angle = desc->_vPanBottom + _fov / 2;
} else if (angle > desc->_vPanTop - _fov / 2) {
angle = desc->_vPanTop - _fov / 2;
}
if (_tiltAngle != angle) {
PanoTrackHandler *track = (PanoTrackHandler *)getTrack(_panoTrack->targetTrack);
track->_currentTiltAngle = _tiltAngle;
_tiltAngle = angle;
track->setDirty();
}
}
bool QuickTimeDecoder::setFOV(float fov) {
PanoSampleDesc *desc = (PanoSampleDesc *)_panoTrack->sampleDescs[0];
bool success = true;
if (fov == 0.0f && _zoomState == kZoomNone) // This is reference to default FOV
fov = _panoTrack->panoInfo.defZoom;
if (fov <= desc->_minimumZoom) {
fov = desc->_minimumZoom;
success = false;
} else if (fov >= desc->_maximumZoom) {
fov = desc->_maximumZoom;
success = false;
}
if (_fov != fov) {
PanoTrackHandler *track = (PanoTrackHandler *)getTrack(_panoTrack->targetTrack);
debugC(3, kDebugLevelGVideo, "QuickTimeDecoder::setFOV: fov: %f (was %f)", fov, _fov);
track->_currentFOV = _fov;
_fov = fov;
track->_currentHFOV = _hfov;
_hfov = _fov * (float)_width / (float)_height;
// We need to recalculate the pan angle and tilt angle to see if it has went
// out of bound for the current value of FOV
// This solves the distortion that we got sometimes when we zoom out at Tilt Angle != 0
setPanAngle(_panAngle);
setTiltAngle(_tiltAngle);
track->setDirty();
}
return success;
}
Common::String QuickTimeDecoder::getCurrentNodeName() {
if (_currentSample == -1)
return Common::String();
PanoTrackSample *sample = &_panoTrack->panoSamples[_currentSample];
return sample->strTable.getString(sample->hdr.nameStrOffset);
}
void QuickTimeDecoder::updateAngles() {
if (_qtvrType == QTVRType::OBJECT) {
_panAngle = (float)getCurrentColumn() / (float)_nav.columns * 360.0;
_tiltAngle = ((_nav.rows - 1) / 2.0 - (float)getCurrentRow()) / (float)(_nav.rows - 1) * 180.0;
debugC(1, kDebugLevelGVideo, "QTVR: row: %d col: %d (%d x %d) pan: %f tilt: %f", getCurrentRow(), getCurrentColumn(), _nav.rows, _nav.columns, getPanAngle(), getTiltAngle());
}
}
void QuickTimeDecoder::setCurrentRow(int row) {
VideoTrackHandler *track = (VideoTrackHandler *)_nextVideoTrack;
int currentColumn = track->getCurFrame() % _nav.columns;
int newFrame = row * _nav.columns + currentColumn;
if (newFrame >= 0 && newFrame < track->getFrameCount()) {
track->setCurFrame(newFrame);
}
}
void QuickTimeDecoder::setCurrentColumn(int column) {
VideoTrackHandler *track = (VideoTrackHandler *)_nextVideoTrack;
int currentRow = track->getCurFrame() / _nav.columns;
int newFrame = currentRow * _nav.columns + column;
if (newFrame >= 0 && newFrame < track->getFrameCount()) {
track->setCurFrame(newFrame);
}
}
void QuickTimeDecoder::nudge(const Common::String &direction) {
VideoTrackHandler *track = (VideoTrackHandler *)_nextVideoTrack;
int curFrame = track->getCurFrame();
int currentRow = curFrame / _nav.columns;
int currentRowStart = currentRow * _nav.columns;
int newFrame = curFrame;
if (direction.equalsIgnoreCase("left")) {
newFrame = (curFrame - 1 - currentRowStart) % _nav.columns + currentRowStart;
} else if (direction.equalsIgnoreCase("right")) {
newFrame = (curFrame + 1 - currentRowStart) % _nav.columns + currentRowStart;
} else if (direction.equalsIgnoreCase("up")) {
newFrame = curFrame - _nav.columns;
if (newFrame < 0)
return;
} else if (direction.equalsIgnoreCase("down")) {
newFrame = curFrame + _nav.columns;
if (newFrame >= track->getFrameCount())
return;
} else {
error("QuickTimeDecoder::nudge(): Invald direction: ('%s')!", direction.c_str());
}
track->setCurFrame(newFrame);
}
QuickTimeDecoder::NodeData QuickTimeDecoder::getNodeData(uint32 nodeID) {
for (const auto &sample : _panoTrack->panoSamples) {
if (sample.hdr.nodeID == nodeID) {
return {
nodeID,
sample.hdr.defHPan,
sample.hdr.defVPan,
sample.hdr.defZoom,
sample.hdr.minHPan,
sample.hdr.minVPan,
sample.hdr.maxHPan,
sample.hdr.maxVPan,
sample.hdr.minZoom,
sample.strTable.getString(sample.hdr.nameStrOffset)};
}
}
error("QuickTimeDecoder::getNodeData(): Node with nodeID %d not found!", nodeID);
return {};
}
void QuickTimeDecoder::goToNode(uint32 nodeID) {
int idx = -1;
for (uint i = 0; i < _panoTrack->panoSamples.size(); i++) {
if (_panoTrack->panoSamples[i].hdr.nodeID == nodeID) {
idx = i;
break;
}
}
if (idx == -1) {
warning("QuickTimeDecoder::goToNode(): Incorrect nodeID: %d (numNodes: %d)", nodeID, _panoTrack->panoSamples.size());
idx = 0;
}
_currentSample = idx;
debugC(3, kDebugLevelGVideo, "QuickTimeDecoder::goToNode(): Moving to nodeID: %d (index: %d)", nodeID, idx);
setPanAngle(_panoTrack->panoSamples[_currentSample].hdr.defHPan);
setTiltAngle(_panoTrack->panoSamples[_currentSample].hdr.defVPan);
setFOV(_panoTrack->panoSamples[_currentSample].hdr.defZoom);
((PanoTrackHandler *)getTrack(_panoTrack->targetTrack))->constructPanorama();
}
/////////////////////////
// PANO Track
////////////////////////
QuickTimeDecoder::PanoTrackHandler::PanoTrackHandler(QuickTimeDecoder *decoder, Common::QuickTimeParser::Track *parent) : _decoder(decoder), _parent(parent) {
if (decoder->_qtvrType != QTVRType::PANORAMA)
error("QuickTimeDecoder::PanoTrackHandler: Incorrect track passed");
_isPanoConstructed = false;
_constructedPano = nullptr;
_constructedHotspots = nullptr;
_upscaledConstructedPano = nullptr;
_projectedPano = nullptr;
_planarProjection = nullptr;
_dirty = true;
_decoder->updateQTVRCursor(0, 0); // Initialize all things for cursor
}
QuickTimeDecoder::PanoTrackHandler::~PanoTrackHandler() {
if (_isPanoConstructed) {
_constructedPano->free();
delete _constructedPano;
_upscaledConstructedPano->free();
delete _upscaledConstructedPano;
if (_constructedHotspots) {
_constructedHotspots->free();
delete _constructedHotspots;
}
}
if (_projectedPano) {
_projectedPano->free();
delete _projectedPano;
_planarProjection->free();
delete _planarProjection;
}
}
uint16 QuickTimeDecoder::PanoTrackHandler::getWidth() const {
return getScaledWidth().toInt();
}
uint16 QuickTimeDecoder::PanoTrackHandler::getHeight() const {
return getScaledHeight().toInt();
}
Graphics::PixelFormat QuickTimeDecoder::PanoTrackHandler::getPixelFormat() const {
PanoSampleDesc *desc = (PanoSampleDesc *)_parent->sampleDescs[0];
VideoTrackHandler *track = (VideoTrackHandler *)(_decoder->getTrack(_decoder->Common::QuickTimeParser::_tracks[desc->_sceneTrackID - 1]->targetTrack));
return track->getPixelFormat();
}
Common::Rational QuickTimeDecoder::PanoTrackHandler::getScaledWidth() const {
return Common::Rational(_parent->width) / _parent->scaleFactorX;
}
Common::Rational QuickTimeDecoder::PanoTrackHandler::getScaledHeight() const {
return Common::Rational(_parent->height) / _parent->scaleFactorY;
}
void QuickTimeDecoder::PanoTrackHandler::swingTransitionHandler() {
// The number of steps (the speed) of the transition is decided by the _transitionSpeed
const int NUM_STEPS = 300 / _decoder->_transitionSpeed;
// Calculate the step
float stepFOV = (_decoder->_fov - _currentFOV) / NUM_STEPS;
float stepHFOV = (_decoder->_hfov - _currentHFOV) / NUM_STEPS;
float stepTiltAngle = (_decoder->_tiltAngle - _currentTiltAngle) / NUM_STEPS;
float targetPanAngle = _decoder->_panAngle;
float difference = ABS(_currentPanAngle - targetPanAngle);
float stepPanAngle;
// All of this is just because the panorama is supposed to take the smallest path
if (targetPanAngle > _currentPanAngle) {
if (difference <= 180) {
stepPanAngle = difference / NUM_STEPS;
} else {
stepPanAngle = (- (360 - difference)) / NUM_STEPS;
}
} else {
if (difference <= 180) {
stepPanAngle = (- difference) / NUM_STEPS;
} else {
stepPanAngle = (360 - difference) / NUM_STEPS;
}
}
int16 rectLeft = _decoder->_origin.x;
int16 rectRight = _decoder->_origin.y;
Common::Point mouse;
int mw = _decoder->_width, mh = _decoder->_height;
for (int i = 0; i < NUM_STEPS; i++) {
projectPanorama(1,
_currentFOV + i * stepFOV,
_currentHFOV + i * stepHFOV,
_currentTiltAngle + i * stepTiltAngle,
_currentPanAngle + i * stepPanAngle);
g_system->copyRectToScreen(_projectedPano->getPixels(),
_projectedPano->pitch,
rectLeft,
rectRight,
_projectedPano->w,
_projectedPano->h);
Common::Event event;
while (g_system->getEventManager()->pollEvent(event)) {
if (event.type == Common::EVENT_QUIT) {
debugC(1, kDebugLevelGVideo, "EVENT_QUIT passed during the Swing Transition.");
g_system->quit();
return;
}
if (Common::isMouseEvent(event)) {
mouse = event.mouse;
}
// Referenced from video.cpp in testbed engine
if (mouse.x >= _decoder->_prevMouse.x &&
mouse.x < _decoder->_prevMouse.x + mw &&
mouse.y >= _decoder->_prevMouse.y &&
mouse.y < _decoder->_prevMouse.y + mh) {
switch (event.type) {
case Common::EVENT_MOUSEMOVE:
_decoder->handleMouseMove(event.mouse.x - _decoder->_prevMouse.x, event.mouse.y - _decoder->_prevMouse.y);
break;
default:
break;
}
}
}
g_system->updateScreen();
g_system->delayMillis(10);
}
// This is so if we call swingTransitionHandler() twice
// The second time there will be no transition
_currentFOV = _decoder->_fov;
_currentPanAngle = _decoder->_panAngle;
_currentHFOV = _decoder->_hfov;
_currentTiltAngle = _decoder->_tiltAngle;
// Due to floating point errors, we may end a few degrees here and there
// Make sure we reach the destination at the end of this loop
// Also we have to go back to our original quality
projectPanorama(3, _decoder->_fov, _decoder->_hfov, _decoder->_tiltAngle, _decoder->_panAngle);
}
const Graphics::Surface *QuickTimeDecoder::PanoTrackHandler::decodeNextFrame() {
if (!_isPanoConstructed)
return nullptr;
// inject fake key/mouse events if button is held down
if (_decoder->_isKeyDown)
_decoder->handleKey(_decoder->_lastKey, true, true);
if (_decoder->_isMouseButtonDown)
_decoder->handleMouseButton(true, _decoder->_prevMouse.x, _decoder->_prevMouse.y, true);
if (_dirty && _decoder->_transitionMode == kTransitionModeNormal) {
float quality = _decoder->getQuality();
float fov = _decoder->_fov;
float hfov = _decoder->_hfov;
float tiltAngle = _decoder->_tiltAngle;
float panAngle = _decoder->_panAngle;
switch ((int)quality) {
case 1:
projectPanorama(1, fov, hfov, tiltAngle, panAngle);
break;
case 2:
projectPanorama(2, fov, hfov, tiltAngle, panAngle);
break;
case 4:
projectPanorama(3, fov, hfov, tiltAngle, panAngle);
break;
case 0:
if (_decoder->_isMouseButtonDown || _decoder->_isKeyDown) {
projectPanorama(1, fov, hfov, tiltAngle, panAngle);
} else {
projectPanorama(3, fov, hfov, tiltAngle, panAngle);
}
break;
default:
projectPanorama(3, fov, hfov, tiltAngle, panAngle);
break;
}
} else if (_decoder->_transitionMode == kTransitionModeSwing) {
swingTransitionHandler();
}
if (_decoder->_cursorDirty) {
_decoder->_cursorDirty = false;
_decoder->updateQTVRCursor(_decoder->_cursorPos.x, _decoder->_cursorPos.y);
}
return _projectedPano;
}
const Graphics::Surface *QuickTimeDecoder::PanoTrackHandler::bufferNextFrame() {
return nullptr;
}
Graphics::Surface *QuickTimeDecoder::PanoTrackHandler::constructMosaic(VideoTrackHandler *track, uint w, uint h, Common::String fname) {
int16 framew = track->getWidth();
int16 frameh = track->getHeight();
Graphics::Surface *target = new Graphics::Surface();
target->create(w * framew, h * frameh, track->getPixelFormat());
debugC(1, kDebugLevelGVideo, "Pixel format: %s", track->getPixelFormat().toString().c_str());
Common::Rect srcRect(0, 0, framew, frameh);
for (uint y = 0; y < h; y++) {
for (uint x = 0; x < w; x++) {
const Graphics::Surface *frame = track->bufferNextFrame();
if (!frame) {
warning("QuickTimeDecoder::PanoTrackHandler::constructMosaic(): Out of frames at: %d, %d", x, y);
break;
}
target->copyRectToSurface(*frame, x * framew, y * frameh, srcRect);
}
}
if (ConfMan.getBool("dump_scripts")) {
Common::Path path = Common::Path(fname);
Common::DumpFile bitmapFile;
if (!bitmapFile.open(path, true)) {
warning("Cannot dump panorama into file '%s'", path.toString().c_str());
target->free();
delete target;
return nullptr;
}
Image::writePNG(bitmapFile, *target, track->getPalette());
bitmapFile.close();
debug(0, "Dumped panorama %s of %d x %d", path.toString().c_str(), target->w, target->h);
}
return target;
}
void QuickTimeDecoder::PanoTrackHandler::initPanorama() {
if (_decoder->_panoTrack->panoInfo.nodes.size() == 0) {
// This is a single node panorama
// We add one node to the list, so the rest of our code
// is happy
PanoTrackSample *sample = &_parent->panoSamples[0];
_decoder->_panoTrack->panoInfo.nodes.resize(1);
_decoder->_panoTrack->panoInfo.nodes[0].nodeID = sample->hdr.nodeID;
_decoder->_panoTrack->panoInfo.nodes[0].timestamp = 0;
_decoder->_panoTrack->panoInfo.defNodeID = sample->hdr.nodeID;
}
_decoder->goToNode(_decoder->_panoTrack->panoInfo.defNodeID);
}
Graphics::Surface *QuickTimeDecoder::PanoTrackHandler::upscalePanorama(Graphics::Surface *sourceSurface, int8 level) {
// This algorithm (bilinear upscaling) takes quite some time to complete
//
// upscaling method
//
// for level 1 upscaling
// a | b a | avg(a, b) | b
// ----- => -----------------
// c | d c | avg(c, d) | d
//
// for level 2 upscaling
// a | avg(a, b) | b
// a | b -----------------------------------------
// ----- => avg(a, c) [e] | avg(e, f) | avg(b, d) [f]
// c | d -----------------------------------------
// c | avg(c, d) | d
uint w = sourceSurface->w;
uint h = sourceSurface->h;
Graphics::Surface *target = new Graphics::Surface();
if (level == 2) {
target->create(w * 2, h * 2, sourceSurface->format);
for (uint y = 0; y < h; ++y) {
for (uint x = 0; x < w; ++x) {
// For the row x==w and column y==h, wrap around the panorama
// and average these pixels with row x==0 and column y==0 respectively
uint x1 = (x + 1) % w;
uint y1 = (y + 1) % h;
// Couldn't find a better way to do this
// Should I write a function in the Surface or PixelFormat class?
int32 p00 = sourceSurface->getPixel(x, y);
int32 p01 = sourceSurface->getPixel(x, y1);
int32 p10 = sourceSurface->getPixel(x1, y);
int32 p11 = sourceSurface->getPixel(x1, y1);
uint8 a00, r00, g00, b00;
uint8 a01, r01, g01, b01;
uint8 a10, r10, g10, b10;
uint8 a11, r11, g11, b11;
sourceSurface->format.colorToARGB(p00, a00, r00, g00, b00);
sourceSurface->format.colorToARGB(p01, a01, r01, g01, b01);
sourceSurface->format.colorToARGB(p10, a10, r10, g10, b10);
sourceSurface->format.colorToARGB(p11, a11, r11, g11, b11);
int32 avgPixel00 = p00;
int32 avgPixel01 = sourceSurface->format.ARGBToColor((a00 + a01) >> 1, (r00 + r01) >> 1, (g00 + g01) >> 1, (b00 + b01) >> 1);
int32 avgPixel10 = sourceSurface->format.ARGBToColor((a00 + a10) >> 1, (r00 + r10) >> 1, (g00 + g10) >> 1, (b00 + b10) >> 1);
int32 avgPixel11 = sourceSurface->format.ARGBToColor(
(a00 + a01 + a10 + a11) >> 2,
(r00 + r01 + r10 + r11) >> 2,
(g00 + g01 + g10 + g11) >> 2,
(b00 + b01 + b10 + b11) >> 2);
target->setPixel(x * 2, y * 2, avgPixel00);
target->setPixel(x * 2, y * 2 + 1, avgPixel01);
target->setPixel(x * 2 + 1, y * 2, avgPixel10);
target->setPixel(x * 2 + 1, y * 2 + 1,avgPixel11);
}
}
} else {
target->create(w * 2, h, sourceSurface->format);
for (uint y = 0; y < h; ++y) {
for (uint x = 0; x < w; ++x) {
uint x1 = (x + 1) % w;
int32 p00 = sourceSurface->getPixel(x, y);
int32 p01 = sourceSurface->getPixel(x1, y);
uint8 a00, r00, g00, b00;
uint8 a01, r01, g01, b01;
sourceSurface->format.colorToARGB(p00, a00, r00, g00, b00);
sourceSurface->format.colorToARGB(p01, a01, r01, g01, b01);
int32 avgPixel00 = p00;
int32 avgPixel01 = sourceSurface->format.ARGBToColor((a00 + a01) >> 1, (r00 + r01) >> 1, (g00 + g01) >> 1, (b00 + b01) >> 1);
target->setPixel(x * 2, y, avgPixel00);
target->setPixel(x * 2 + 1, y, avgPixel01);
}
}
}
return target;
}
void QuickTimeDecoder::PanoTrackHandler::boxAverage(Graphics::Surface *sourceSurface, uint8 scaleFactor) {
uint16 h = sourceSurface->h;
uint16 w = sourceSurface->w;
// Apply box average if quality is higher than 1
// Otherwise it'll be quicker to just copy the _planarProjection onto _projectedPano
if (scaleFactor == 1) {
_projectedPano->copyFrom(*sourceSurface);
return;
}
uint8 scaleSquare = scaleFactor * scaleFactor;
// Average out the pixels from the larger image
for (uint16 y = 0; y < h / scaleFactor; y++) {
for (uint16 x = 0; x < w / scaleFactor; x++) {
uint16 avgA = 0, avgR = 0, avgG = 0, avgB = 0;
for (uint8 row = 0; row < scaleFactor; row++) {
for (uint8 column = 0; column < scaleFactor; column++) {
uint8 a00, r00, g00, b00;
uint32 pixel00 = sourceSurface->getPixel(MIN((x * scaleFactor + row), w - 1), MIN((y * scaleFactor + column), h - 1));
_projectedPano->format.colorToARGB(pixel00, a00, r00, g00, b00);
avgA += a00;
avgR += r00;
avgG += g00;
avgB += b00;
}
}
avgA /= scaleSquare;
avgR /= scaleSquare;
avgG /= scaleSquare;
avgB /= scaleSquare;
uint32 avgPixel = _projectedPano->format.ARGBToColor(avgA, avgR, avgG, avgB);
_projectedPano->setPixel(x, y, avgPixel);
}
}
}
void QuickTimeDecoder::PanoTrackHandler::constructPanorama() {
PanoSampleDesc *desc = (PanoSampleDesc *)_parent->sampleDescs[0];
PanoTrackSample *sample = &_parent->panoSamples[_decoder->_currentSample];
if (_constructedPano) {
_constructedPano->free();
delete _constructedPano;
if (_constructedHotspots) {
_constructedHotspots->free();
delete _constructedHotspots;
}
}
debugC(1, kDebugLevelGVideo, "scene: %d (%d x %d) hotspots: %d (%d x %d)", desc->_sceneTrackID, desc->_sceneSizeX, desc->_sceneSizeY,
desc->_hotSpotTrackID, desc->_hotSpotSizeX, desc->_hotSpotSizeY);
debugC(1, kDebugLevelGVideo, "sceneNumFrames: %d x %d sceneColorDepth: %d", desc->_sceneNumFramesX, desc->_sceneNumFramesY, desc->_sceneColorDepth);
debugC(1, kDebugLevelGVideo, "Node idx: %d", sample->hdr.nodeID);
int nodeidx = -1;
for (int i = 0; i < (int)_parent->panoInfo.nodes.size(); i++)
if (_parent->panoInfo.nodes[i].nodeID == sample->hdr.nodeID) {
nodeidx = i;
break;
}
if (nodeidx == -1) {
warning("constructPanorama(): Missing node %d in panoInfo", sample->hdr.nodeID);
nodeidx = 0;
}
uint32 timestamp = _parent->panoInfo.nodes[nodeidx].timestamp;
debugC(1, kDebugLevelGVideo, "Timestamp: %d", timestamp);
VideoTrackHandler *track = (VideoTrackHandler *)(_decoder->getTrack(_decoder->Common::QuickTimeParser::_tracks[desc->_sceneTrackID - 1]->targetTrack));
track->seek(Audio::Timestamp(0, timestamp, _decoder->_timeScale));
_constructedPano = constructMosaic(track, desc->_sceneNumFramesX, desc->_sceneNumFramesY, "dumps/pano-full.png");
// _upscaleLevel = 0 means _contructedPano has just been constructed, hasn't been upscaled yet
// or that the upscaledConstructedPanorama has upscaled a different panorama, not the current constructedPano
_upscaleLevel = 0;
if (desc->_hotSpotTrackID) {
track = (VideoTrackHandler *)(_decoder->getTrack(_decoder->Common::QuickTimeParser::_tracks[desc->_hotSpotTrackID - 1]->targetTrack));
track->seek(Audio::Timestamp(0, timestamp, _decoder->_timeScale));
_constructedHotspots = constructMosaic(track, desc->_hotSpotNumFramesX, desc->_hotSpotNumFramesY, "dumps/pano-hotspot.png");
}
_isPanoConstructed = true;
}
Common::Point QuickTimeDecoder::PanoTrackHandler::projectPoint(int16 mx, int16 my) {
if (!_isPanoConstructed || !_constructedHotspots)
return Common::Point(-1, -1);
uint16 w = _decoder->getWidth(), h = _decoder->getHeight();
uint16 hotWidth = _constructedHotspots->w, hotHeight = _constructedHotspots->h;
PanoSampleDesc *desc = (PanoSampleDesc *)_parent->sampleDescs[0];
float cornerVectors[3][3];
float *topRightVector = cornerVectors[0];
float *bottomRightVector = cornerVectors[1];
float *mousePixelVector = cornerVectors[2];
bottomRightVector[1] = tan(_decoder->_fov * M_PI / 360.0);
bottomRightVector[0] = bottomRightVector[1] * (float)w / (float)h;
bottomRightVector[2] = 1.0f;
topRightVector[0] = bottomRightVector[0];
topRightVector[1] = -bottomRightVector[1];
topRightVector[2] = bottomRightVector[2];
// Apply pitch (tilt) rotation
float cosTilt = cos(-_decoder->_tiltAngle * M_PI / 180.0);
float sinTilt = sin(-_decoder->_tiltAngle * M_PI / 180.0);
for (int v = 0; v < 2; v++) {
float y = cornerVectors[v][1];
float z = cornerVectors[v][2];
float newZ = z * cosTilt - y * sinTilt;
float newY = y * cosTilt + z * sinTilt;
cornerVectors[v][1] = newY;