Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(231)

Side by Side Diff: remoting/codec/video_encoder_row_based.cc

Issue 11195029: Remove ZLib codec support from chromoting host and client. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
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
3 // found in the LICENSE file.
4
5 #include "remoting/codec/video_encoder_row_based.h"
6
7 #include "base/logging.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"
12 #include "remoting/proto/video.pb.h"
13
14 namespace remoting {
15
16 static const int kPacketSize = 1024 * 1024;
17
18 VideoEncoderRowBased* VideoEncoderRowBased::CreateZlibEncoder() {
19 return new VideoEncoderRowBased(new CompressorZlib(),
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) {
47 }
48
49 VideoEncoderRowBased::VideoEncoderRowBased(Compressor* compressor,
50 VideoPacketFormat::Encoding encoding,
51 int packet_size)
52 : encoding_(encoding),
53 compressor_(compressor),
54 screen_size_(SkISize::Make(0,0)),
55 packet_size_(packet_size) {
56 }
57
58 VideoEncoderRowBased::~VideoEncoderRowBased() {}
59
60 void VideoEncoderRowBased::Encode(
61 scoped_refptr<CaptureData> capture_data,
62 bool key_frame,
63 const DataAvailableCallback& data_available_callback) {
64 CHECK(capture_data->pixel_format() == media::VideoFrame::RGB32)
65 << "RowBased VideoEncoder only works with RGB32. Got "
66 << capture_data->pixel_format();
67 capture_data_ = capture_data;
68 callback_ = data_available_callback;
69
70 const SkRegion& region = capture_data->dirty_region();
71 SkRegion::Iterator iter(region);
72 while (!iter.done()) {
73 SkIRect rect = iter.rect();
74 iter.next();
75 EncodeRect(rect, iter.done());
76 }
77
78 capture_data_ = NULL;
79 callback_.Reset();
80 }
81
82 void VideoEncoderRowBased::EncodeRect(const SkIRect& rect, bool last) {
83 CHECK(capture_data_->data_planes().data[0]);
84 CHECK_EQ(capture_data_->pixel_format(), media::VideoFrame::RGB32);
85 const int strides = capture_data_->data_planes().strides[0];
86 const int bytes_per_pixel = 4;
87 const int row_size = bytes_per_pixel * rect.width();
88
89 compressor_->Reset();
90
91 scoped_ptr<VideoPacket> packet(new VideoPacket());
92 PrepareUpdateStart(rect, packet.get());
93 const uint8* in = capture_data_->data_planes().data[0] +
94 rect.fTop * strides + rect.fLeft * bytes_per_pixel;
95 // TODO(hclam): Fill in the sequence number.
96 uint8* out = GetOutputBuffer(packet.get(), packet_size_);
97 int filled = 0;
98 int row_pos = 0; // Position in the current row in bytes.
99 int row_y = 0; // Current row.
100 bool compress_again = true;
101 while (compress_again) {
102 // Prepare a message for sending out.
103 if (!packet.get()) {
104 packet.reset(new VideoPacket());
105 out = GetOutputBuffer(packet.get(), packet_size_);
106 filled = 0;
107 }
108
109 Compressor::CompressorFlush flush = Compressor::CompressorNoFlush;
110 if (row_y == rect.height() - 1) {
111 flush = Compressor::CompressorFinish;
112 }
113
114 int consumed = 0;
115 int written = 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
122 // We have reached the end of stream.
123 if (!compress_again) {
124 packet->set_flags(packet->flags() | VideoPacket::LAST_PACKET);
125 packet->set_capture_time_ms(capture_data_->capture_time_ms());
126 packet->set_client_sequence_number(
127 capture_data_->client_sequence_number());
128 SkIPoint dpi(capture_data_->dpi());
129 if (dpi.x())
130 packet->mutable_format()->set_x_dpi(dpi.x());
131 if (dpi.y())
132 packet->mutable_format()->set_y_dpi(dpi.y());
133 if (last)
134 packet->set_flags(packet->flags() | VideoPacket::LAST_PARTITION);
135 DCHECK(row_pos == row_size);
136 DCHECK(row_y == rect.height() - 1);
137 }
138
139 // If we have filled the message or we have reached the end of stream.
140 if (filled == packet_size_ || !compress_again) {
141 packet->mutable_data()->resize(filled);
142 callback_.Run(packet.Pass());
143 }
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 }
152 }
153
154 void VideoEncoderRowBased::PrepareUpdateStart(const SkIRect& rect,
155 VideoPacket* packet) {
156 packet->set_flags(packet->flags() | VideoPacket::FIRST_PACKET);
157
158 VideoPacketFormat* format = packet->mutable_format();
159 format->set_x(rect.fLeft);
160 format->set_y(rect.fTop);
161 format->set_width(rect.width());
162 format->set_height(rect.height());
163 format->set_encoding(encoding_);
164 if (capture_data_->size() != screen_size_) {
165 screen_size_ = capture_data_->size();
166 format->set_screen_width(screen_size_.width());
167 format->set_screen_height(screen_size_.height());
168 }
169 }
170
171 uint8* VideoEncoderRowBased::GetOutputBuffer(VideoPacket* packet, size_t size) {
172 packet->mutable_data()->resize(size);
173 // TODO(ajwong): Is there a better way to do this at all???
174 return const_cast<uint8*>(reinterpret_cast<const uint8*>(
175 packet->mutable_data()->data()));
176 }
177
178 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/codec/video_encoder_row_based.h ('k') | remoting/codec/video_encoder_row_based_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698