Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

executable file 274 lines (249 sloc) 7.858 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
// -*- mode:objc -*-
/*
** DVREncoder.h
**
** Copyright 20101
**
** Author: George Nachman
**
** Project: iTerm2
**
** Description: Encodes screen images into a DVRBuffer. Implements
** a basic key-frame + differential encoding scheme.
**
** 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 "DVREncoder.h"
#import "DVRIndexEntry.h"
#include <sys/time.h>
#include "LineBuffer.h"
//#define DVRDEBUG

// Returns a timestamp for the current time.
static long long now()
{
    struct timeval tv;
    gettimeofday(&tv, NULL);
    long long result = tv.tv_sec;
    result *= 1000000;
    result += tv.tv_usec;
    return result;
}

@implementation DVREncoder

- (id)initWithBuffer:(DVRBuffer*)buffer
{
    if ([super init] == nil) {
        return nil;
    }

    buffer_ = [buffer retain];
    lastFrame_ = nil;
    count_ = 0;
    haveReservation_ = NO;
    return self;
}

- (void)dealloc
{
    [lastFrame_ release];
    [buffer_ release];
    [super dealloc];
}

- (void)appendFrame:(char*)buffer length:(int)length info:(DVRFrameInfo*)info
{
    BOOL eligibleForDiff;
    if (lastFrame_ &&
        length == [lastFrame_ length] &&
        info->width == lastInfo_.width &&
        info->height == lastInfo_.height &&
        bytesSinceLastKeyFrame_ < [buffer_ capacity] / 2) {
        eligibleForDiff = YES;
    } else {
        eligibleForDiff = NO;
    }

    const int kKeyFrameFrequency = 100;

    if (!eligibleForDiff || count_++ % kKeyFrameFrequency == 0) {
        [self _appendKeyFrame:buffer length:length info:info];
    } else {
        [self _appendDiffFrame:buffer length:length info:info];
    }
}

- (BOOL)reserve:(int)length
{
    haveReservation_ = YES;
    reservation_ = length;
    BOOL hadToFree = [buffer_ reserve:length];

    // Deallocate leading blocks until the first one is a key frame. If the first
    // block is a diff frame it's useless.
    while (![buffer_ isEmpty] && hadToFree) {
        DVRIndexEntry* entry = [buffer_ entryForKey:[buffer_ firstKey]];
        assert(entry);
        if (entry->info.frameType == DVRFrameTypeKeyFrame) {
            break;
        } else {
            [buffer_ deallocateBlock];
        }
    }
    return hadToFree;
}

@end

@implementation DVREncoder (Private)

- (void)debug:(NSString*)prefix buffer:(char*)buffer length:(int)length
{
#ifdef DVRDEBUG
    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(@"%@ length %d: \"%s\"", prefix, length, d);
#endif
}

- (void)_appendKeyFrame:(char*)buffer length:(int)length info:(DVRFrameInfo*)info
{
    [lastFrame_ release];
    lastFrame_ = [[NSMutableData dataWithBytes:buffer length:length] retain];
#ifdef DVRDEBUG
    char d[30000];
    int i;
    for (i = 0; i < length / sizeof(screen_char_t); 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(@"KEY: %s", d);
#endif
    char* scratch = [buffer_ scratch];
    memcpy(scratch, buffer, length);
    [self _appendFrameImpl:scratch length:length type:DVRFrameTypeKeyFrame info:info];
    bytesSinceLastKeyFrame_ = 0;
}

- (void)_appendDiffFrame:(char*)buffer length:(int)length info:(DVRFrameInfo*)info
{
    char* scratch = [buffer_ scratch];
    int diffBytes = [self _computeDiff:buffer
                                length:length
                                  dest:scratch
                               maxSize:reservation_];
    if (diffBytes < 0) {
        // Diff ended up being larger than a key frame would be.
        [self _appendKeyFrame:buffer length:length info:info];
        return;
    }

#ifdef DVRDEBUG2
    int i;
    screen_char_t* s = scratch;
    for (i = 0; i < diffBytes; ++i) {
        NSLog(@"Offset %d: %d (%c)", i, (int)scratch[i], scratch[i]);
    }
#endif
    [self _appendFrameImpl:scratch length:diffBytes type:DVRFrameTypeDiffFrame info:info];
    bytesSinceLastKeyFrame_ += diffBytes;
}

- (void)_appendFrameImpl:(char*)dest length:(int)length type:(DVRFrameType)type info:(DVRFrameInfo*)info
{
    assert(haveReservation_);
    haveReservation_ = NO;

#ifdef DVRDEBUG
    NSLog(@"Append frame of type %d starting at %x length %d at index %d", (int)type, dest, length, [buffer_ lastKey]+1);
#endif

    lastInfo_ = *info;

    long long key = [buffer_ allocateBlock:length];
    DVRIndexEntry* entry = [buffer_ entryForKey:key];
    entry->info = *info;
    entry->info.timestamp = now();
    entry->info.frameType = type;
}

- (int)_computeDiff:(char*)buffer length:(int)length dest:(char*)scratch maxSize:(int)maxBytes
{
    assert(length == [lastFrame_ length]);
    char* other = [lastFrame_ mutableBytes];
    assert(other);

    int o = 0;
    int sameCount = 0;
    int diffCount = 0;
    char* startDiff = 0;

    // TODO(georgen): Implement a better diff
    for (int i = 0; i < length; ++i) {
        if (buffer[i] == other[i]) {
            if (diffCount > 0) {
                if (o + 1 + sizeof(diffCount) + diffCount > maxBytes) {
                    // Diff is too big.
                    return -1;
                }
                scratch[o++] = kDiffSequence;
                memcpy(scratch + o, &diffCount, sizeof(diffCount));
                o += sizeof(diffCount);
                memcpy(scratch + o, startDiff, diffCount);
                o += diffCount;
                [self debug:@"diff " buffer:startDiff length:diffCount];
                diffCount = 0;
            }
            ++sameCount;
        } else {
            if (sameCount > 0) {
                if (o + 1 + sizeof(sameCount) > maxBytes) {
                    // Diff is too big.
                    return -1;
                }
                scratch[o++] = kSameSequence;
                memcpy(scratch + o, &sameCount, sizeof(sameCount));
                o += sizeof(sameCount);
#ifdef DVRDEBUG
                NSLog(@"%d the same", sameCount);
#endif
                sameCount = 0;
            }
            if (!diffCount) {
                startDiff = buffer + i;
            }
            other[i] = buffer[i];
            ++diffCount;
        }
    }
    if (diffCount > 0) {
        if (o + 1 + sizeof(diffCount) + diffCount > maxBytes) {
            // Diff is too big.
            return -1;
        }
        scratch[o++] = kDiffSequence;
        memcpy(scratch + o, &diffCount, sizeof(diffCount));
        o += sizeof(diffCount);
        memcpy(scratch + o, startDiff, diffCount);
        o += diffCount;
        [self debug:@"diff " buffer:startDiff length:diffCount];
    }
    if (sameCount > 0) {
        if (o + 1 + sizeof(sameCount) > maxBytes) {
            // Diff is too big.
            return -1;
        }
        scratch[o++] = kSameSequence;
        memcpy(scratch + o, &sameCount, sizeof(sameCount));
        o += sizeof(sameCount);
#ifdef DVRDEBUG
        NSLog(@"%d the same", sameCount);
#endif
    }
    return o;
}

@end
Something went wrong with that request. Please try again.