forked from gnachman/iTerm2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DVRDecoder.m
executable file
·240 lines (213 loc) · 5.4 KB
/
DVRDecoder.m
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
// -*- mode:objc -*-
/*
** DVRDecoder.m
**
** Copyright 20101
**
** Author: George Nachman
**
** Project: iTerm2
**
** Description: Decodes the key+diff frame scheme implemented in
** DVREncoder. Used by the instant replay feature to load screen
** images out of a circular DVRBuffer owned by a DVR.
**
** 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 2 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, write to the Free Software
** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#import "DVRDecoder.h"
#import "DVRIndexEntry.h"
#import "LineBuffer.h"
@implementation DVRDecoder
- (id)initWithBuffer:(DVRBuffer*)buffer
{
if ([super init] == nil) {
return nil;
}
buffer_ = buffer;
frame_ = 0;
length_ = 0;
key_ = -1;
return self;
}
- (void)dealloc
{
if (frame_) {
free(frame_);
}
[super dealloc];
}
- (BOOL)seek:(long long)timestamp
{
// TODO(georgen): Do a binary search
long long lastKey = [buffer_ lastKey];
for (long long key = [buffer_ firstKey]; key <= lastKey; ++key) {
DVRIndexEntry* entry = [buffer_ entryForKey:key];
if (entry->info.timestamp >= timestamp) {
[self _seekToEntryWithKey:key];
return YES;
}
}
return NO;
}
- (char*)decodedFrame
{
return frame_;
}
- (int)length
{
return length_;
}
- (BOOL)next
{
long long newKey;
if (key_ == -1) {
newKey = [buffer_ firstKey];
} else {
newKey = key_ + 1;
if (newKey < [buffer_ firstKey]) {
newKey = [buffer_ firstKey];
} else if (newKey > [buffer_ lastKey]) {
return NO;
}
}
[self _seekToEntryWithKey:newKey];
return YES;
}
- (BOOL)prev
{
if (key_ <= [buffer_ firstKey]) {
return NO;
}
[self _seekToEntryWithKey:key_ - 1];
return YES;
}
- (long long)timestamp
{
return info_.timestamp;
}
- (void)invalidateIndex:(long long)i
{
if (i == key_) {
key_ = - 1;
}
}
- (DVRFrameInfo)info
{
return info_;
}
@end
@implementation DVRDecoder (Private)
- (void)debug:(NSString*)prefix buffer:(char*)buffer length:(int)length
{
char d[30000];
int i;
for (i = 0; i * sizeof(screen_char_t) < length; i++) {
screen_char_t s = ((screen_char_t*)buffer)[i];
if (s.code && s.complexChar) {
d[i] = s.code;
} else {
d[i] = ' ';
}
}
d[i] = 0;
NSLog(@"%@: \"%s\"", prefix, d);
}
- (void)_seekToEntryWithKey:(long long)key
{
#ifdef DVRDEBUG
NSLog(@"Begin seek to %lld", key);
#endif
// Make sure key is valid.
if (key < [buffer_ firstKey] || key > [buffer_ lastKey]) {
#ifdef DVRDEBUG
NSLog(@"Frame %lld doesn't exist so skipping to %lld", key, [buffer_ firstKey]);
#endif
key = [buffer_ firstKey];
}
// Find the key frame before 'key'.
long long j = key;
while ([buffer_ entryForKey:j]->info.frameType != DVRFrameTypeKeyFrame) {
assert(j != [buffer_ firstKey]);
--j;
}
[self _loadKeyFrameWithKey:j];
#ifdef DVRDEBUG
[self debug:@"Key frame:" buffer:frame_ length:length_];
#endif
// Apply all the diff frames up to key.
while (j != key) {
++j;
[self _loadDiffFrameWithKey:j];
#ifdef DVRDEBUG
[self debug:[NSString stringWithFormat:@"After applying diff of %d:", j] buffer:frame_ length:length_];
#endif
}
key_ = j;
#ifdef DVRDEBUG
NSLog(@"end seek to %d", i);
#endif
}
- (void)_loadKeyFrameWithKey:(long long)key
{
DVRIndexEntry* entry = [buffer_ entryForKey:key];
if (length_ != entry->frameLength && frame_) {
free(frame_);
frame_ = 0;
}
length_ = entry->frameLength;
if (!frame_) {
frame_ = malloc(length_);
}
char* data = [buffer_ blockForKey:key];
info_ = entry->info;
memcpy(frame_, data, length_);
}
- (void)_loadDiffFrameWithKey:(long long)key
{
#ifdef DVRDEBUG
NSLog(@"Load diff frame at index %d", theIndex);
#endif
DVRIndexEntry* entry = [buffer_ entryForKey:key];
info_ = entry->info;
char* diff = [buffer_ blockForKey:key];
int o = 0;
for (int i = 0; i < entry->frameLength; ) {
int n;
switch (diff[i++]) {
case kSameSequence:
memcpy(&n, diff + i, sizeof(n));
i += sizeof(n);
#ifdef DVRDEBUG
[self debug:@"same seq" buffer:frame_ + o length:n];
#endif
o += n;
break;
case kDiffSequence:
memcpy(&n, diff + i, sizeof(n));
i += sizeof(n);
memcpy(frame_ + o, diff + i, n);
#ifdef DVRDEBUG
[self debug:@"diff seq" buffer:frame_ + o length:n];
#endif
o += n;
i += n;
break;
default:
NSLog(@"Unexpected block type %d", (int)diff[i]);
assert(0);
}
}
}
@end