[email protected] | bcad268 | 2011-09-30 20:35:26 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 8ea7a167 | 2010-10-04 19:48:42 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "remoting/base/decoder_row_based.h" |
| 6 | |
[email protected] | 1e1cb3b | 2011-11-10 02:07:41 | [diff] [blame^] | 7 | #include "base/logging.h" |
[email protected] | 8ea7a167 | 2010-10-04 19:48:42 | [diff] [blame] | 8 | #include "remoting/base/decompressor.h" |
| 9 | #include "remoting/base/decompressor_zlib.h" |
| 10 | #include "remoting/base/decompressor_verbatim.h" |
[email protected] | c3af26f33 | 2010-10-06 22:46:00 | [diff] [blame] | 11 | #include "remoting/base/util.h" |
[email protected] | 8ea7a167 | 2010-10-04 19:48:42 | [diff] [blame] | 12 | |
| 13 | namespace remoting { |
| 14 | |
[email protected] | 3adf1b2 | 2010-11-09 23:22:20 | [diff] [blame] | 15 | namespace { |
| 16 | // Both input and output data are assumed to be RGBA32. |
| 17 | const int kBytesPerPixel = 4; |
| 18 | } |
| 19 | |
[email protected] | 8ea7a167 | 2010-10-04 19:48:42 | [diff] [blame] | 20 | DecoderRowBased* DecoderRowBased::CreateZlibDecoder() { |
[email protected] | 04b3614 | 2010-11-02 01:08:19 | [diff] [blame] | 21 | return new DecoderRowBased(new DecompressorZlib(), |
| 22 | VideoPacketFormat::ENCODING_ZLIB); |
[email protected] | 8ea7a167 | 2010-10-04 19:48:42 | [diff] [blame] | 23 | } |
| 24 | |
| 25 | DecoderRowBased* DecoderRowBased::CreateVerbatimDecoder() { |
[email protected] | 04b3614 | 2010-11-02 01:08:19 | [diff] [blame] | 26 | return new DecoderRowBased(new DecompressorVerbatim(), |
| 27 | VideoPacketFormat::ENCODING_VERBATIM); |
[email protected] | 8ea7a167 | 2010-10-04 19:48:42 | [diff] [blame] | 28 | } |
| 29 | |
| 30 | DecoderRowBased::DecoderRowBased(Decompressor* decompressor, |
[email protected] | 04b3614 | 2010-11-02 01:08:19 | [diff] [blame] | 31 | VideoPacketFormat::Encoding encoding) |
[email protected] | 8ea7a167 | 2010-10-04 19:48:42 | [diff] [blame] | 32 | : state_(kUninitialized), |
| 33 | decompressor_(decompressor), |
| 34 | encoding_(encoding), |
[email protected] | 8ea7a167 | 2010-10-04 19:48:42 | [diff] [blame] | 35 | row_pos_(0), |
[email protected] | d3a4b09d | 2011-02-22 18:16:53 | [diff] [blame] | 36 | row_y_(0) { |
[email protected] | 8ea7a167 | 2010-10-04 19:48:42 | [diff] [blame] | 37 | } |
| 38 | |
| 39 | DecoderRowBased::~DecoderRowBased() { |
| 40 | } |
| 41 | |
| 42 | void DecoderRowBased::Reset() { |
| 43 | frame_ = NULL; |
| 44 | decompressor_->Reset(); |
| 45 | state_ = kUninitialized; |
[email protected] | 5bc7183 | 2010-12-09 01:34:08 | [diff] [blame] | 46 | updated_rects_.clear(); |
[email protected] | 8ea7a167 | 2010-10-04 19:48:42 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | bool DecoderRowBased::IsReadyForData() { |
[email protected] | 5bc7183 | 2010-12-09 01:34:08 | [diff] [blame] | 50 | switch (state_) { |
| 51 | case kUninitialized: |
| 52 | case kError: |
| 53 | return false; |
| 54 | case kReady: |
| 55 | case kProcessing: |
| 56 | case kPartitionDone: |
| 57 | case kDone: |
| 58 | return true; |
| 59 | } |
| 60 | NOTREACHED(); |
| 61 | return false; |
[email protected] | 8ea7a167 | 2010-10-04 19:48:42 | [diff] [blame] | 62 | } |
| 63 | |
[email protected] | 3adf1b2 | 2010-11-09 23:22:20 | [diff] [blame] | 64 | void DecoderRowBased::Initialize(scoped_refptr<media::VideoFrame> frame) { |
[email protected] | 8ea7a167 | 2010-10-04 19:48:42 | [diff] [blame] | 65 | // Make sure we are not currently initialized. |
| 66 | CHECK_EQ(kUninitialized, state_); |
| 67 | |
[email protected] | 3adf1b2 | 2010-11-09 23:22:20 | [diff] [blame] | 68 | if (frame->format() != media::VideoFrame::RGB32) { |
[email protected] | 8ea7a167 | 2010-10-04 19:48:42 | [diff] [blame] | 69 | LOG(WARNING) << "DecoderRowBased only supports RGB32."; |
| 70 | state_ = kError; |
| 71 | return; |
| 72 | } |
| 73 | |
| 74 | frame_ = frame; |
[email protected] | 8ea7a167 | 2010-10-04 19:48:42 | [diff] [blame] | 75 | state_ = kReady; |
| 76 | } |
| 77 | |
[email protected] | 2c22968 | 2010-12-02 20:23:35 | [diff] [blame] | 78 | Decoder::DecodeResult DecoderRowBased::DecodePacket(const VideoPacket* packet) { |
[email protected] | 3adf1b2 | 2010-11-09 23:22:20 | [diff] [blame] | 79 | UpdateStateForPacket(packet); |
[email protected] | 8ea7a167 | 2010-10-04 19:48:42 | [diff] [blame] | 80 | |
[email protected] | 3adf1b2 | 2010-11-09 23:22:20 | [diff] [blame] | 81 | if (state_ == kError) { |
| 82 | return DECODE_ERROR; |
| 83 | } |
| 84 | |
| 85 | const uint8* in = reinterpret_cast<const uint8*>(packet->data().data()); |
| 86 | const int in_size = packet->data().size(); |
| 87 | |
| 88 | const int row_size = clip_.width() * kBytesPerPixel; |
[email protected] | 8ea7a167 | 2010-10-04 19:48:42 | [diff] [blame] | 89 | int stride = frame_->stride(media::VideoFrame::kRGBPlane); |
| 90 | uint8* rect_begin = frame_->data(media::VideoFrame::kRGBPlane); |
| 91 | |
[email protected] | bcad268 | 2011-09-30 20:35:26 | [diff] [blame] | 92 | uint8* out = rect_begin + stride * (clip_.fTop + row_y_) + |
| 93 | kBytesPerPixel * clip_.fLeft; |
[email protected] | 8ea7a167 | 2010-10-04 19:48:42 | [diff] [blame] | 94 | |
| 95 | // Consume all the data in the message. |
| 96 | bool decompress_again = true; |
| 97 | int used = 0; |
| 98 | while (decompress_again && used < in_size) { |
[email protected] | 3adf1b2 | 2010-11-09 23:22:20 | [diff] [blame] | 99 | if (row_y_ >= clip_.height()) { |
| 100 | state_ = kError; |
| 101 | LOG(WARNING) << "Too much data is received for the given rectangle."; |
| 102 | return DECODE_ERROR; |
| 103 | } |
| 104 | |
[email protected] | 8ea7a167 | 2010-10-04 19:48:42 | [diff] [blame] | 105 | int written = 0; |
| 106 | int consumed = 0; |
| 107 | // TODO(ajwong): This assume source and dest stride are the same, which is |
| 108 | // incorrect. |
| 109 | decompress_again = decompressor_->Process( |
| 110 | in + used, in_size - used, out + row_pos_, row_size - row_pos_, |
| 111 | &consumed, &written); |
| 112 | used += consumed; |
| 113 | row_pos_ += written; |
| 114 | |
| 115 | // If this row is completely filled then move onto the next row. |
| 116 | if (row_pos_ == row_size) { |
| 117 | ++row_y_; |
| 118 | row_pos_ = 0; |
| 119 | out += stride; |
| 120 | } |
| 121 | } |
[email protected] | 3adf1b2 | 2010-11-09 23:22:20 | [diff] [blame] | 122 | |
[email protected] | 5bc7183 | 2010-12-09 01:34:08 | [diff] [blame] | 123 | if (state_ == kPartitionDone || state_ == kDone) { |
[email protected] | 9b2b3ae8 | 2010-11-16 00:36:08 | [diff] [blame] | 124 | if (row_y_ < clip_.height()) { |
| 125 | state_ = kError; |
| 126 | LOG(WARNING) << "Received LAST_PACKET, but didn't get enough data."; |
| 127 | return DECODE_ERROR; |
| 128 | } |
[email protected] | 3adf1b2 | 2010-11-09 23:22:20 | [diff] [blame] | 129 | |
[email protected] | 5bc7183 | 2010-12-09 01:34:08 | [diff] [blame] | 130 | updated_rects_.push_back(clip_); |
[email protected] | 9b2b3ae8 | 2010-11-16 00:36:08 | [diff] [blame] | 131 | decompressor_->Reset(); |
[email protected] | 5bc7183 | 2010-12-09 01:34:08 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | if (state_ == kDone) { |
[email protected] | 9b2b3ae8 | 2010-11-16 00:36:08 | [diff] [blame] | 135 | return DECODE_DONE; |
| 136 | } else { |
| 137 | return DECODE_IN_PROGRESS; |
| 138 | } |
[email protected] | 3adf1b2 | 2010-11-09 23:22:20 | [diff] [blame] | 139 | } |
| 140 | |
| 141 | void DecoderRowBased::UpdateStateForPacket(const VideoPacket* packet) { |
| 142 | if (state_ == kError) { |
| 143 | return; |
| 144 | } |
| 145 | |
| 146 | if (packet->flags() & VideoPacket::FIRST_PACKET) { |
[email protected] | 5bc7183 | 2010-12-09 01:34:08 | [diff] [blame] | 147 | if (state_ != kReady && state_ != kDone && state_ != kPartitionDone) { |
[email protected] | 3adf1b2 | 2010-11-09 23:22:20 | [diff] [blame] | 148 | state_ = kError; |
| 149 | LOG(WARNING) << "Received unexpected FIRST_PACKET."; |
| 150 | return; |
| 151 | } |
| 152 | state_ = kProcessing; |
| 153 | |
| 154 | // Reset the buffer location status variables on the first packet. |
[email protected] | bcad268 | 2011-09-30 20:35:26 | [diff] [blame] | 155 | clip_.setXYWH(packet->format().x(), packet->format().y(), |
[email protected] | 3adf1b2 | 2010-11-09 23:22:20 | [diff] [blame] | 156 | packet->format().width(), packet->format().height()); |
| 157 | row_pos_ = 0; |
| 158 | row_y_ = 0; |
| 159 | } |
| 160 | |
| 161 | if (state_ != kProcessing) { |
| 162 | state_ = kError; |
| 163 | LOG(WARNING) << "Received unexpected packet."; |
| 164 | return; |
| 165 | } |
| 166 | |
| 167 | if (packet->flags() & VideoPacket::LAST_PACKET) { |
| 168 | if (state_ != kProcessing) { |
| 169 | state_ = kError; |
| 170 | LOG(WARNING) << "Received unexpected LAST_PACKET."; |
| 171 | return; |
| 172 | } |
[email protected] | 5bc7183 | 2010-12-09 01:34:08 | [diff] [blame] | 173 | state_ = kPartitionDone; |
| 174 | } |
| 175 | |
| 176 | if (packet->flags() & VideoPacket::LAST_PARTITION) { |
| 177 | if (state_ != kPartitionDone) { |
| 178 | state_ = kError; |
| 179 | LOG(WARNING) << "Received unexpected LAST_PARTITION."; |
| 180 | return; |
| 181 | } |
[email protected] | 3adf1b2 | 2010-11-09 23:22:20 | [diff] [blame] | 182 | state_ = kDone; |
| 183 | } |
| 184 | |
| 185 | return; |
| 186 | } |
| 187 | |
[email protected] | bcad268 | 2011-09-30 20:35:26 | [diff] [blame] | 188 | void DecoderRowBased::GetUpdatedRects(RectVector* rects) { |
[email protected] | 5bc7183 | 2010-12-09 01:34:08 | [diff] [blame] | 189 | rects->swap(updated_rects_); |
| 190 | updated_rects_.clear(); |
[email protected] | 3adf1b2 | 2010-11-09 23:22:20 | [diff] [blame] | 191 | } |
| 192 | |
| 193 | VideoPacketFormat::Encoding DecoderRowBased::Encoding() { |
| 194 | return encoding_; |
[email protected] | 8ea7a167 | 2010-10-04 19:48:42 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | } // namespace remoting |