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
|
/* Copyright (C) 2021 Vladimir Sukhonosov <xornet@xornet.org>
* Copyright (C) 2021 Martins Mozeiko <martins.mozeiko@gmail.com>
* All Rights Reserved.
*
* This 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 software 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 software; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
* USA.
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#define MAX_H264_INSTANCES 64
#include <deque>
#include <rdr/MemInStream.h>
#include <rdr/InStream.h>
#include <rdr/OutStream.h>
#include <rfb/LogWriter.h>
#include <rfb/H264Decoder.h>
#include <rfb/H264DecoderContext.h>
using namespace rfb;
static LogWriter vlog("H264Decoder");
enum rectFlags {
resetContext = 0x1,
resetAllContexts = 0x2,
};
H264Decoder::H264Decoder() : Decoder(DecoderOrdered)
{
}
H264Decoder::~H264Decoder()
{
resetContexts();
}
void H264Decoder::resetContexts()
{
os::AutoMutex lock(&mutex);
for (H264DecoderContext* context : contexts)
delete context;
contexts.clear();
}
H264DecoderContext* H264Decoder::findContext(const Rect& r)
{
os::AutoMutex m(&mutex);
for (H264DecoderContext* context : contexts)
if (context->isEqualRect(r))
return context;
return nullptr;
}
bool H264Decoder::readRect(const Rect& /*r*/,
rdr::InStream* is,
const ServerParams& /*server*/,
rdr::OutStream* os)
{
uint32_t len;
if (!is->hasData(8))
return false;
is->setRestorePoint();
len = is->readU32();
os->writeU32(len);
uint32_t reset = is->readU32();
os->writeU32(reset);
if (!is->hasDataOrRestore(len))
return false;
is->clearRestorePoint();
os->copyBytes(is, len);
return true;
}
void H264Decoder::decodeRect(const Rect& r, const uint8_t* buffer,
size_t buflen,
const ServerParams& /*server*/,
ModifiablePixelBuffer* pb)
{
rdr::MemInStream is(buffer, buflen);
uint32_t len = is.readU32();
uint32_t reset = is.readU32();
H264DecoderContext* ctx = nullptr;
if (reset & resetAllContexts)
{
resetContexts();
if (!len)
return;
reset &= ~(resetContext | resetAllContexts);
} else {
ctx = findContext(r);
}
if (!ctx)
{
os::AutoMutex lock(&mutex);
if (contexts.size() >= MAX_H264_INSTANCES)
{
H264DecoderContext* excess_ctx = contexts.front();
delete excess_ctx;
contexts.pop_front();
}
ctx = H264DecoderContext::createContext(r);
if (!ctx)
throw std::runtime_error("H264Decoder: Context not be created");
contexts.push_back(ctx);
}
if (!ctx->isReady())
throw std::runtime_error("H264Decoder: Context is not ready");
if (reset & resetContext)
ctx->reset();
if (!len)
return;
ctx->decode(is.getptr(len), len, pb);
}
|