Chromium Code Reviews| 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_encoder_row_based.h" | 5 #include "remoting/codec/video_encoder_verbatim.h" |
| 6 | 6 |
| 7 #include "base/logging.h" | 7 #include "base/logging.h" |
| 8 #include "remoting/base/capture_data.h" | 8 #include "remoting/base/capture_data.h" |
| 9 #include "remoting/base/compressor_verbatim.h" | |
| 10 #include "remoting/base/compressor_zlib.h" | |
| 11 #include "remoting/base/util.h" | 9 #include "remoting/base/util.h" |
| 12 #include "remoting/proto/video.pb.h" | 10 #include "remoting/proto/video.pb.h" |
| 13 | 11 |
| 14 namespace remoting { | 12 namespace remoting { |
| 15 | 13 |
| 16 static const int kPacketSize = 1024 * 1024; | 14 static const int kPacketSize = 1024 * 1024; |
| 17 | 15 |
| 18 VideoEncoderRowBased* VideoEncoderRowBased::CreateZlibEncoder() { | 16 VideoEncoderVerbatim::VideoEncoderVerbatim() |
| 19 return new VideoEncoderRowBased(new CompressorZlib(), | 17 : screen_size_(SkISize::Make(0,0)), |
| 20 VideoPacketFormat::ENCODING_ZLIB); | |
| 21 } | |
| 22 | |
| 23 VideoEncoderRowBased* VideoEncoderRowBased::CreateZlibEncoder(int packet_size) { | |
| 24 return new VideoEncoderRowBased(new CompressorZlib(), | |
| 25 VideoPacketFormat::ENCODING_ZLIB, | |
| 26 packet_size); | |
| 27 } | |
| 28 | |
| 29 VideoEncoderRowBased* VideoEncoderRowBased::CreateVerbatimEncoder() { | |
| 30 return new VideoEncoderRowBased(new CompressorVerbatim(), | |
| 31 VideoPacketFormat::ENCODING_VERBATIM); | |
| 32 } | |
| 33 | |
| 34 VideoEncoderRowBased* VideoEncoderRowBased::CreateVerbatimEncoder( | |
| 35 int packet_size) { | |
| 36 return new VideoEncoderRowBased(new CompressorVerbatim(), | |
| 37 VideoPacketFormat::ENCODING_VERBATIM, | |
| 38 packet_size); | |
| 39 } | |
| 40 | |
| 41 VideoEncoderRowBased::VideoEncoderRowBased(Compressor* compressor, | |
| 42 VideoPacketFormat::Encoding encoding) | |
| 43 : encoding_(encoding), | |
| 44 compressor_(compressor), | |
| 45 screen_size_(SkISize::Make(0,0)), | |
| 46 packet_size_(kPacketSize) { | 18 packet_size_(kPacketSize) { |
| 47 } | 19 } |
| 48 | 20 |
| 49 VideoEncoderRowBased::VideoEncoderRowBased(Compressor* compressor, | 21 void VideoEncoderVerbatim::SetMaxPacketSize(int size) { |
| 50 VideoPacketFormat::Encoding encoding, | 22 packet_size_ = size; |
| 51 int packet_size) | |
| 52 : encoding_(encoding), | |
| 53 compressor_(compressor), | |
| 54 screen_size_(SkISize::Make(0,0)), | |
| 55 packet_size_(packet_size) { | |
| 56 } | 23 } |
| 57 | 24 |
| 58 VideoEncoderRowBased::~VideoEncoderRowBased() {} | 25 VideoEncoderVerbatim::~VideoEncoderVerbatim() { |
| 26 } | |
| 59 | 27 |
| 60 void VideoEncoderRowBased::Encode( | 28 void VideoEncoderVerbatim::Encode( |
| 61 scoped_refptr<CaptureData> capture_data, | 29 scoped_refptr<CaptureData> capture_data, |
| 62 bool key_frame, | 30 bool key_frame, |
| 63 const DataAvailableCallback& data_available_callback) { | 31 const DataAvailableCallback& data_available_callback) { |
| 64 CHECK(capture_data->pixel_format() == media::VideoFrame::RGB32) | 32 CHECK(capture_data->pixel_format() == media::VideoFrame::RGB32) |
| 65 << "RowBased VideoEncoder only works with RGB32. Got " | 33 << "RowBased VideoEncoder only works with RGB32. Got " |
| 66 << capture_data->pixel_format(); | 34 << capture_data->pixel_format(); |
| 67 capture_data_ = capture_data; | 35 capture_data_ = capture_data; |
| 68 callback_ = data_available_callback; | 36 callback_ = data_available_callback; |
| 69 | 37 |
| 70 const SkRegion& region = capture_data->dirty_region(); | 38 const SkRegion& region = capture_data->dirty_region(); |
| 71 SkRegion::Iterator iter(region); | 39 SkRegion::Iterator iter(region); |
| 72 while (!iter.done()) { | 40 while (!iter.done()) { |
| 73 SkIRect rect = iter.rect(); | 41 SkIRect rect = iter.rect(); |
| 74 iter.next(); | 42 iter.next(); |
| 75 EncodeRect(rect, iter.done()); | 43 EncodeRect(rect, iter.done()); |
| 76 } | 44 } |
| 77 | 45 |
| 78 capture_data_ = NULL; | 46 capture_data_ = NULL; |
| 79 callback_.Reset(); | 47 callback_.Reset(); |
| 80 } | 48 } |
| 81 | 49 |
| 82 void VideoEncoderRowBased::EncodeRect(const SkIRect& rect, bool last) { | 50 void VideoEncoderVerbatim::EncodeRect(const SkIRect& rect, bool last) { |
| 83 CHECK(capture_data_->data_planes().data[0]); | 51 CHECK(capture_data_->data_planes().data[0]); |
| 84 CHECK_EQ(capture_data_->pixel_format(), media::VideoFrame::RGB32); | 52 CHECK_EQ(capture_data_->pixel_format(), media::VideoFrame::RGB32); |
| 85 const int strides = capture_data_->data_planes().strides[0]; | 53 const int strides = capture_data_->data_planes().strides[0]; |
| 86 const int bytes_per_pixel = 4; | 54 const int bytes_per_pixel = 4; |
| 87 const int row_size = bytes_per_pixel * rect.width(); | 55 const int row_size = bytes_per_pixel * rect.width(); |
| 88 | 56 |
| 89 compressor_->Reset(); | |
| 90 | |
| 91 scoped_ptr<VideoPacket> packet(new VideoPacket()); | 57 scoped_ptr<VideoPacket> packet(new VideoPacket()); |
| 92 PrepareUpdateStart(rect, packet.get()); | 58 PrepareUpdateStart(rect, packet.get()); |
| 93 const uint8* in = capture_data_->data_planes().data[0] + | 59 const uint8* in = capture_data_->data_planes().data[0] + |
| 94 rect.fTop * strides + rect.fLeft * bytes_per_pixel; | 60 rect.fTop * strides + rect.fLeft * bytes_per_pixel; |
| 95 // TODO(hclam): Fill in the sequence number. | 61 // TODO(hclam): Fill in the sequence number. |
| 96 uint8* out = GetOutputBuffer(packet.get(), packet_size_); | 62 uint8* out = GetOutputBuffer(packet.get(), packet_size_); |
| 97 int filled = 0; | 63 int filled = 0; |
| 98 int row_pos = 0; // Position in the current row in bytes. | 64 int row_pos = 0; // Position in the current row in bytes. |
| 99 int row_y = 0; // Current row. | 65 int row_y = 0; // Current row. |
| 100 bool compress_again = true; | 66 while (row_y < rect.height()) { |
| 101 while (compress_again) { | |
| 102 // Prepare a message for sending out. | 67 // Prepare a message for sending out. |
| 103 if (!packet.get()) { | 68 if (!packet.get()) { |
| 104 packet.reset(new VideoPacket()); | 69 packet.reset(new VideoPacket()); |
| 105 out = GetOutputBuffer(packet.get(), packet_size_); | 70 out = GetOutputBuffer(packet.get(), packet_size_); |
| 106 filled = 0; | 71 filled = 0; |
| 107 } | 72 } |
| 108 | 73 |
| 109 Compressor::CompressorFlush flush = Compressor::CompressorNoFlush; | 74 if (row_y < rect.height()) { |
| 110 if (row_y == rect.height() - 1) { | 75 int bytes_to_copy = std::min(row_size - row_pos, packet_size_ - filled); |
| 111 flush = Compressor::CompressorFinish; | 76 memcpy(out + filled, in + row_pos, bytes_to_copy); |
| 77 row_pos += bytes_to_copy; | |
| 78 filled += bytes_to_copy; | |
| 79 | |
| 80 // Just to the next row when we've reached the end of the current row. | |
|
Wez
2012/10/17 22:25:16
nit: Just -> Move
Sergey Ulanov
2012/10/18 01:09:52
Changed to "Jump"
| |
| 81 if (row_pos == row_size) { | |
| 82 row_pos = 0; | |
| 83 in += strides; | |
| 84 ++row_y; | |
| 85 } | |
| 112 } | 86 } |
| 113 | 87 |
| 114 int consumed = 0; | 88 if (row_y == rect.height()) { |
| 115 int written = 0; | 89 DCHECK_EQ(row_pos, 0); |
| 116 compress_again = compressor_->Process(in + row_pos, row_size - row_pos, | |
| 117 out + filled, packet_size_ - filled, | |
| 118 flush, &consumed, &written); | |
| 119 row_pos += consumed; | |
| 120 filled += written; | |
| 121 | 90 |
| 122 // We have reached the end of stream. | 91 packet->mutable_data()->resize(filled); |
| 123 if (!compress_again) { | |
| 124 packet->set_flags(packet->flags() | VideoPacket::LAST_PACKET); | 92 packet->set_flags(packet->flags() | VideoPacket::LAST_PACKET); |
| 125 packet->set_capture_time_ms(capture_data_->capture_time_ms()); | 93 packet->set_capture_time_ms(capture_data_->capture_time_ms()); |
| 126 packet->set_client_sequence_number( | 94 packet->set_client_sequence_number( |
| 127 capture_data_->client_sequence_number()); | 95 capture_data_->client_sequence_number()); |
| 128 SkIPoint dpi(capture_data_->dpi()); | 96 SkIPoint dpi(capture_data_->dpi()); |
| 129 if (dpi.x()) | 97 if (dpi.x()) |
| 130 packet->mutable_format()->set_x_dpi(dpi.x()); | 98 packet->mutable_format()->set_x_dpi(dpi.x()); |
| 131 if (dpi.y()) | 99 if (dpi.y()) |
| 132 packet->mutable_format()->set_y_dpi(dpi.y()); | 100 packet->mutable_format()->set_y_dpi(dpi.y()); |
| 133 if (last) | 101 if (last) |
| 134 packet->set_flags(packet->flags() | VideoPacket::LAST_PARTITION); | 102 packet->set_flags(packet->flags() | VideoPacket::LAST_PARTITION); |
| 135 DCHECK(row_pos == row_size); | |
| 136 DCHECK(row_y == rect.height() - 1); | |
| 137 } | 103 } |
| 138 | 104 |
| 139 // If we have filled the message or we have reached the end of stream. | 105 // If we have filled the current packet, then send it. |
| 140 if (filled == packet_size_ || !compress_again) { | 106 if (filled == packet_size_ || row_y == rect.height()) { |
| 141 packet->mutable_data()->resize(filled); | 107 packet->mutable_data()->resize(filled); |
| 142 callback_.Run(packet.Pass()); | 108 callback_.Run(packet.Pass()); |
| 143 } | 109 } |
| 144 | |
| 145 // Reached the end of input row and we're not at the last row. | |
| 146 if (row_pos == row_size && row_y < rect.height() - 1) { | |
| 147 row_pos = 0; | |
| 148 in += strides; | |
| 149 ++row_y; | |
| 150 } | |
| 151 } | 110 } |
| 152 } | 111 } |
| 153 | 112 |
| 154 void VideoEncoderRowBased::PrepareUpdateStart(const SkIRect& rect, | 113 void VideoEncoderVerbatim::PrepareUpdateStart(const SkIRect& rect, |
| 155 VideoPacket* packet) { | 114 VideoPacket* packet) { |
| 156 packet->set_flags(packet->flags() | VideoPacket::FIRST_PACKET); | 115 packet->set_flags(packet->flags() | VideoPacket::FIRST_PACKET); |
| 157 | 116 |
| 158 VideoPacketFormat* format = packet->mutable_format(); | 117 VideoPacketFormat* format = packet->mutable_format(); |
| 159 format->set_x(rect.fLeft); | 118 format->set_x(rect.fLeft); |
| 160 format->set_y(rect.fTop); | 119 format->set_y(rect.fTop); |
| 161 format->set_width(rect.width()); | 120 format->set_width(rect.width()); |
| 162 format->set_height(rect.height()); | 121 format->set_height(rect.height()); |
| 163 format->set_encoding(encoding_); | 122 format->set_encoding(VideoPacketFormat::ENCODING_VERBATIM); |
| 164 if (capture_data_->size() != screen_size_) { | 123 if (capture_data_->size() != screen_size_) { |
| 165 screen_size_ = capture_data_->size(); | 124 screen_size_ = capture_data_->size(); |
| 166 format->set_screen_width(screen_size_.width()); | 125 format->set_screen_width(screen_size_.width()); |
| 167 format->set_screen_height(screen_size_.height()); | 126 format->set_screen_height(screen_size_.height()); |
| 168 } | 127 } |
| 169 } | 128 } |
| 170 | 129 |
| 171 uint8* VideoEncoderRowBased::GetOutputBuffer(VideoPacket* packet, size_t size) { | 130 uint8* VideoEncoderVerbatim::GetOutputBuffer(VideoPacket* packet, size_t size) { |
| 172 packet->mutable_data()->resize(size); | 131 packet->mutable_data()->resize(size); |
| 173 // TODO(ajwong): Is there a better way to do this at all??? | 132 // TODO(ajwong): Is there a better way to do this at all??? |
| 174 return const_cast<uint8*>(reinterpret_cast<const uint8*>( | 133 return const_cast<uint8*>(reinterpret_cast<const uint8*>( |
| 175 packet->mutable_data()->data())); | 134 packet->mutable_data()->data())); |
| 176 } | 135 } |
| 177 | 136 |
| 178 } // namespace remoting | 137 } // namespace remoting |
| OLD | NEW |