OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
| 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 |
| 7 #include "remoting/base/decompressor.h" |
| 8 #include "remoting/base/decompressor_zlib.h" |
| 9 #include "remoting/base/decompressor_verbatim.h" |
| 10 #include "remoting/base/protocol_util.h" |
| 11 |
| 12 namespace remoting { |
| 13 |
| 14 DecoderRowBased* DecoderRowBased::CreateZlibDecoder() { |
| 15 return new DecoderRowBased(new DecompressorZlib(), EncodingZlib); |
| 16 } |
| 17 |
| 18 DecoderRowBased* DecoderRowBased::CreateVerbatimDecoder() { |
| 19 return new DecoderRowBased(new DecompressorVerbatim(), EncodingNone); |
| 20 } |
| 21 |
| 22 DecoderRowBased::DecoderRowBased(Decompressor* decompressor, |
| 23 UpdateStreamEncoding encoding) |
| 24 : state_(kUninitialized), |
| 25 decompressor_(decompressor), |
| 26 encoding_(encoding), |
| 27 bytes_per_src_pixel_(0), |
| 28 row_pos_(0), |
| 29 row_y_(0), |
| 30 // TODO(hclam): We should use the information from the update stream |
| 31 // to determine whether we should reverse the rows or not. |
| 32 // But for simplicity we set to be always true. |
| 33 reverse_rows_(true) { |
| 34 } |
| 35 |
| 36 DecoderRowBased::~DecoderRowBased() { |
| 37 } |
| 38 |
| 39 void DecoderRowBased::Reset() { |
| 40 frame_ = NULL; |
| 41 decompressor_->Reset(); |
| 42 state_ = kUninitialized; |
| 43 } |
| 44 |
| 45 bool DecoderRowBased::IsReadyForData() { |
| 46 return state_ == kReady; |
| 47 } |
| 48 |
| 49 void DecoderRowBased::Initialize(scoped_refptr<media::VideoFrame> frame, |
| 50 const gfx::Rect& clip, |
| 51 int bytes_per_src_pixel) { |
| 52 // Make sure we are not currently initialized. |
| 53 CHECK_EQ(kUninitialized, state_); |
| 54 |
| 55 if (static_cast<PixelFormat>(frame->format()) != PixelFormatRgb32) { |
| 56 LOG(WARNING) << "DecoderRowBased only supports RGB32."; |
| 57 state_ = kError; |
| 58 return; |
| 59 } |
| 60 |
| 61 frame_ = frame; |
| 62 |
| 63 // Reset the buffer location status variables. |
| 64 clip_ = clip; |
| 65 row_pos_ = 0; |
| 66 row_y_ = 0; |
| 67 bytes_per_src_pixel_ = bytes_per_src_pixel; |
| 68 |
| 69 state_ = kReady; |
| 70 } |
| 71 |
| 72 void DecoderRowBased::DecodeBytes(const std::string& encoded_bytes) { |
| 73 DCHECK_EQ(kReady, state_); |
| 74 |
| 75 const uint8* in = reinterpret_cast<const uint8*>(encoded_bytes.data()); |
| 76 const int in_size = encoded_bytes.size(); |
| 77 const int row_size = clip_.width() * bytes_per_src_pixel_; |
| 78 int stride = frame_->stride(media::VideoFrame::kRGBPlane); |
| 79 uint8* rect_begin = frame_->data(media::VideoFrame::kRGBPlane); |
| 80 |
| 81 if (reverse_rows_) { |
| 82 // Advance the pointer to the last row. |
| 83 rect_begin += (frame_->height() - 1) * stride; |
| 84 |
| 85 // And then make the stride negative. |
| 86 stride = -stride; |
| 87 } |
| 88 |
| 89 // TODO(ajwong): This should be bytes_per_dst_pixel shouldn't this.? |
| 90 uint8* out = rect_begin + |
| 91 stride * (clip_.y() + row_y_) + |
| 92 bytes_per_src_pixel_ * clip_.x(); |
| 93 |
| 94 // Consume all the data in the message. |
| 95 bool decompress_again = true; |
| 96 int used = 0; |
| 97 while (decompress_again && used < in_size) { |
| 98 int written = 0; |
| 99 int consumed = 0; |
| 100 // TODO(ajwong): This assume source and dest stride are the same, which is |
| 101 // incorrect. |
| 102 decompress_again = decompressor_->Process( |
| 103 in + used, in_size - used, out + row_pos_, row_size - row_pos_, |
| 104 &consumed, &written); |
| 105 used += consumed; |
| 106 row_pos_ += written; |
| 107 |
| 108 // If this row is completely filled then move onto the next row. |
| 109 if (row_pos_ == row_size) { |
| 110 ++row_y_; |
| 111 row_pos_ = 0; |
| 112 out += stride; |
| 113 } |
| 114 } |
| 115 } |
| 116 |
| 117 } // namespace remoting |
OLD | NEW |