OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "remoting/codec/video_decoder_row_based.h" | 5 #include "remoting/codec/video_decoder_verbatim.h" |
6 | 6 |
7 #include "base/logging.h" | 7 #include "base/logging.h" |
8 #include "remoting/base/decompressor.h" | |
9 #include "remoting/base/decompressor_zlib.h" | |
10 #include "remoting/base/decompressor_verbatim.h" | |
11 #include "remoting/base/util.h" | 8 #include "remoting/base/util.h" |
12 | 9 |
13 namespace remoting { | 10 namespace remoting { |
14 | 11 |
15 namespace { | 12 namespace { |
16 // Both input and output data are assumed to be RGBA32. | 13 // Both input and output data are assumed to be RGBA32. |
17 const int kBytesPerPixel = 4; | 14 const int kBytesPerPixel = 4; |
18 } | 15 } // namespace |
19 | 16 |
20 VideoDecoderRowBased* VideoDecoderRowBased::CreateZlibDecoder() { | |
21 return new VideoDecoderRowBased(new DecompressorZlib(), | |
22 VideoPacketFormat::ENCODING_ZLIB); | |
23 } | |
24 | 17 |
25 VideoDecoderRowBased* VideoDecoderRowBased::CreateVerbatimDecoder() { | 18 VideoDecoderVerbatim::VideoDecoderVerbatim() |
26 return new VideoDecoderRowBased(new DecompressorVerbatim(), | |
27 VideoPacketFormat::ENCODING_VERBATIM); | |
28 } | |
29 | |
30 VideoDecoderRowBased::VideoDecoderRowBased(Decompressor* decompressor, | |
31 VideoPacketFormat::Encoding encoding) | |
32 : state_(kUninitialized), | 19 : state_(kUninitialized), |
33 clip_(SkIRect::MakeEmpty()), | 20 clip_(SkIRect::MakeEmpty()), |
34 decompressor_(decompressor), | |
35 encoding_(encoding), | |
36 row_pos_(0), | 21 row_pos_(0), |
37 row_y_(0), | 22 row_y_(0), |
38 screen_size_(SkISize::Make(0, 0)) { | 23 screen_size_(SkISize::Make(0, 0)) { |
39 } | 24 } |
40 | 25 |
41 VideoDecoderRowBased::~VideoDecoderRowBased() { | 26 VideoDecoderVerbatim::~VideoDecoderVerbatim() { |
42 } | 27 } |
43 | 28 |
44 bool VideoDecoderRowBased::IsReadyForData() { | 29 bool VideoDecoderVerbatim::IsReadyForData() { |
45 switch (state_) { | 30 switch (state_) { |
46 case kUninitialized: | 31 case kUninitialized: |
47 case kError: | 32 case kError: |
48 return false; | 33 return false; |
49 case kReady: | 34 case kReady: |
50 case kProcessing: | 35 case kProcessing: |
51 case kPartitionDone: | 36 case kPartitionDone: |
52 case kDone: | 37 case kDone: |
53 return true; | 38 return true; |
54 } | 39 } |
55 NOTREACHED(); | 40 NOTREACHED(); |
56 return false; | 41 return false; |
57 } | 42 } |
58 | 43 |
59 void VideoDecoderRowBased::Initialize(const SkISize& screen_size) { | 44 void VideoDecoderVerbatim::Initialize(const SkISize& screen_size) { |
60 decompressor_->Reset(); | |
61 updated_region_.setEmpty(); | 45 updated_region_.setEmpty(); |
62 screen_buffer_.reset(NULL); | 46 screen_buffer_.reset(NULL); |
63 | 47 |
64 screen_size_ = screen_size; | 48 screen_size_ = screen_size; |
65 // Allocate the screen buffer, if necessary. | 49 // Allocate the screen buffer, if necessary. |
66 if (!screen_size_.isEmpty()) { | 50 if (!screen_size_.isEmpty()) { |
67 screen_buffer_.reset(new uint8[ | 51 screen_buffer_.reset(new uint8[ |
68 screen_size_.width() * screen_size_.height() * kBytesPerPixel]); | 52 screen_size_.width() * screen_size_.height() * kBytesPerPixel]); |
69 } | 53 } |
70 | 54 |
71 state_ = kReady; | 55 state_ = kReady; |
72 } | 56 } |
73 | 57 |
74 VideoDecoder::DecodeResult VideoDecoderRowBased::DecodePacket( | 58 VideoDecoder::DecodeResult VideoDecoderVerbatim::DecodePacket( |
75 const VideoPacket* packet) { | 59 const VideoPacket* packet) { |
76 UpdateStateForPacket(packet); | 60 UpdateStateForPacket(packet); |
77 | 61 |
78 if (state_ == kError) { | 62 if (state_ == kError) { |
79 return DECODE_ERROR; | 63 return DECODE_ERROR; |
80 } | 64 } |
81 | 65 |
82 const uint8* in = reinterpret_cast<const uint8*>(packet->data().data()); | 66 const uint8* in = reinterpret_cast<const uint8*>(packet->data().data()); |
83 const int in_size = packet->data().size(); | 67 const int in_size = packet->data().size(); |
84 const int row_size = clip_.width() * kBytesPerPixel; | 68 const int row_size = clip_.width() * kBytesPerPixel; |
85 | 69 |
86 int out_stride = screen_size_.width() * kBytesPerPixel; | 70 int out_stride = screen_size_.width() * kBytesPerPixel; |
87 uint8* out = screen_buffer_.get() + out_stride * (clip_.top() + row_y_) + | 71 uint8* out = screen_buffer_.get() + out_stride * (clip_.top() + row_y_) + |
88 kBytesPerPixel * clip_.left(); | 72 kBytesPerPixel * clip_.left(); |
89 | 73 |
90 // Consume all the data in the message. | 74 // Consume all the data in the message. |
91 bool decompress_again = true; | |
92 int used = 0; | 75 int used = 0; |
93 while (decompress_again && used < in_size) { | 76 while (used < in_size) { |
94 if (row_y_ >= clip_.height()) { | 77 if (row_y_ >= clip_.height()) { |
95 state_ = kError; | 78 state_ = kError; |
96 LOG(WARNING) << "Too much data is received for the given rectangle."; | 79 LOG(WARNING) << "Too much data is received for the given rectangle."; |
97 return DECODE_ERROR; | 80 return DECODE_ERROR; |
98 } | 81 } |
99 | 82 |
100 int written = 0; | 83 int bytes_to_copy = std::min(in_size - used, row_size - row_pos_); |
101 int consumed = 0; | 84 memcpy(out + row_pos_, in + used, bytes_to_copy); |
102 decompress_again = decompressor_->Process( | 85 |
103 in + used, in_size - used, out + row_pos_, row_size - row_pos_, | 86 used += bytes_to_copy; |
104 &consumed, &written); | 87 row_pos_ += bytes_to_copy; |
105 used += consumed; | |
106 row_pos_ += written; | |
107 | 88 |
108 // If this row is completely filled then move onto the next row. | 89 // If this row is completely filled then move onto the next row. |
109 if (row_pos_ == row_size) { | 90 if (row_pos_ == row_size) { |
110 ++row_y_; | 91 ++row_y_; |
111 row_pos_ = 0; | 92 row_pos_ = 0; |
112 out += out_stride; | 93 out += out_stride; |
113 } | 94 } |
114 } | 95 } |
115 | 96 |
116 if (state_ == kPartitionDone || state_ == kDone) { | 97 if (state_ == kPartitionDone || state_ == kDone) { |
117 if (row_y_ < clip_.height()) { | 98 if (row_y_ < clip_.height()) { |
118 state_ = kError; | 99 state_ = kError; |
119 LOG(WARNING) << "Received LAST_PACKET, but didn't get enough data."; | 100 LOG(WARNING) << "Received LAST_PACKET, but didn't get enough data."; |
120 return DECODE_ERROR; | 101 return DECODE_ERROR; |
121 } | 102 } |
122 | 103 |
123 updated_region_.op(clip_, SkRegion::kUnion_Op); | 104 updated_region_.op(clip_, SkRegion::kUnion_Op); |
124 decompressor_->Reset(); | |
125 } | 105 } |
126 | 106 |
127 if (state_ == kDone) { | 107 if (state_ == kDone) { |
128 return DECODE_DONE; | 108 return DECODE_DONE; |
129 } else { | 109 } else { |
130 return DECODE_IN_PROGRESS; | 110 return DECODE_IN_PROGRESS; |
131 } | 111 } |
132 } | 112 } |
133 | 113 |
134 void VideoDecoderRowBased::UpdateStateForPacket(const VideoPacket* packet) { | 114 void VideoDecoderVerbatim::UpdateStateForPacket(const VideoPacket* packet) { |
135 if (state_ == kError) { | 115 if (state_ == kError) { |
136 return; | 116 return; |
137 } | 117 } |
138 | 118 |
139 if (packet->flags() & VideoPacket::FIRST_PACKET) { | 119 if (packet->flags() & VideoPacket::FIRST_PACKET) { |
140 if (state_ != kReady && state_ != kDone && state_ != kPartitionDone) { | 120 if (state_ != kReady && state_ != kDone && state_ != kPartitionDone) { |
141 state_ = kError; | 121 state_ = kError; |
142 LOG(WARNING) << "Received unexpected FIRST_PACKET."; | 122 LOG(WARNING) << "Received unexpected FIRST_PACKET."; |
143 return; | 123 return; |
144 } | 124 } |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
177 state_ = kError; | 157 state_ = kError; |
178 LOG(WARNING) << "Received unexpected LAST_PARTITION."; | 158 LOG(WARNING) << "Received unexpected LAST_PARTITION."; |
179 return; | 159 return; |
180 } | 160 } |
181 state_ = kDone; | 161 state_ = kDone; |
182 } | 162 } |
183 | 163 |
184 return; | 164 return; |
185 } | 165 } |
186 | 166 |
187 VideoPacketFormat::Encoding VideoDecoderRowBased::Encoding() { | 167 VideoPacketFormat::Encoding VideoDecoderVerbatim::Encoding() { |
188 return encoding_; | 168 return VideoPacketFormat::ENCODING_VERBATIM; |
189 } | 169 } |
190 | 170 |
191 void VideoDecoderRowBased::Invalidate(const SkISize& view_size, | 171 void VideoDecoderVerbatim::Invalidate(const SkISize& view_size, |
192 const SkRegion& region) { | 172 const SkRegion& region) { |
193 updated_region_.op(region, SkRegion::kUnion_Op); | 173 updated_region_.op(region, SkRegion::kUnion_Op); |
194 } | 174 } |
195 | 175 |
196 void VideoDecoderRowBased::RenderFrame(const SkISize& view_size, | 176 void VideoDecoderVerbatim::RenderFrame(const SkISize& view_size, |
197 const SkIRect& clip_area, | 177 const SkIRect& clip_area, |
198 uint8* image_buffer, | 178 uint8* image_buffer, |
199 int image_stride, | 179 int image_stride, |
200 SkRegion* output_region) { | 180 SkRegion* output_region) { |
201 output_region->setEmpty(); | 181 output_region->setEmpty(); |
202 | 182 |
203 // TODO(alexeypa): scaling is not implemented. | 183 // TODO(alexeypa): scaling is not implemented. |
204 SkIRect clip_rect = SkIRect::MakeSize(screen_size_); | 184 SkIRect clip_rect = SkIRect::MakeSize(screen_size_); |
205 if (!clip_rect.intersect(clip_area)) | 185 if (!clip_rect.intersect(clip_area)) |
206 return; | 186 return; |
(...skipping 10 matching lines...) Expand all Loading... |
217 image_buffer, image_stride, | 197 image_buffer, image_stride, |
218 clip_area, | 198 clip_area, |
219 rect); | 199 rect); |
220 output_region->op(rect, SkRegion::kUnion_Op); | 200 output_region->op(rect, SkRegion::kUnion_Op); |
221 } | 201 } |
222 | 202 |
223 updated_region_.setEmpty(); | 203 updated_region_.setEmpty(); |
224 } | 204 } |
225 | 205 |
226 } // namespace remoting | 206 } // namespace remoting |
OLD | NEW |