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

Side by Side Diff: remoting/base/encoder_vp8.cc

Issue 4136010: Cleanups in the video encoding decoding code. Reenable VP8. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Fixed arm build Created 10 years, 1 month 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
« no previous file with comments | « remoting/base/encoder_verbatim.cc ('k') | remoting/base/encoder_zlib.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2010 The Chromium Authors. All rights reserved. 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 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 "base/logging.h" 5 #include "base/logging.h"
6 #include "media/base/callback.h" 6 #include "media/base/callback.h"
7 #include "media/base/data_buffer.h" 7 #include "media/base/data_buffer.h"
8 #include "media/base/media.h" 8 #include "media/base/media.h"
9 #include "remoting/base/capture_data.h" 9 #include "remoting/base/capture_data.h"
10 #include "remoting/base/encoder_vp8.h" 10 #include "remoting/base/encoder_vp8.h"
(...skipping 30 matching lines...) Expand all
41 41
42 // libvpx seems to require both to be assigned. 42 // libvpx seems to require both to be assigned.
43 image_->d_w = width; 43 image_->d_w = width;
44 image_->w = width; 44 image_->w = width;
45 image_->d_h = height; 45 image_->d_h = height;
46 image_->h = height; 46 image_->h = height;
47 47
48 vpx_codec_enc_cfg_t config; 48 vpx_codec_enc_cfg_t config;
49 const vpx_codec_iface_t* algo = 49 const vpx_codec_iface_t* algo =
50 (const vpx_codec_iface_t*)media::GetVp8CxAlgoAddress(); 50 (const vpx_codec_iface_t*)media::GetVp8CxAlgoAddress();
51 CHECK(algo);
51 vpx_codec_err_t ret = vpx_codec_enc_config_default(algo, &config, 0); 52 vpx_codec_err_t ret = vpx_codec_enc_config_default(algo, &config, 0);
52 if (ret != VPX_CODEC_OK) 53 if (ret != VPX_CODEC_OK)
53 return false; 54 return false;
54 55
55 // TODO(hclam): Tune the parameters to better suit the application. 56 // TODO(hclam): Tune the parameters to better suit the application.
56 config.rc_target_bitrate = width * height * config.rc_target_bitrate 57 config.rc_target_bitrate = width * height * config.rc_target_bitrate
57 / config.g_w / config.g_h; 58 / config.g_w / config.g_h;
58 config.g_w = width; 59 config.g_w = width;
59 config.g_h = height; 60 config.g_h = height;
60 config.g_pass = VPX_RC_ONE_PASS; 61 config.g_pass = VPX_RC_ONE_PASS;
61 config.g_profile = 1; 62 config.g_profile = 1;
62 config.g_threads = 2; 63 config.g_threads = 2;
63 config.rc_min_quantizer = 0; 64 config.rc_min_quantizer = 0;
64 config.rc_max_quantizer = 15; 65 config.rc_max_quantizer = 15;
65 config.g_timebase.num = 1; 66 config.g_timebase.num = 1;
66 config.g_timebase.den = 30; 67 config.g_timebase.den = 30;
67 68
68 if (vpx_codec_enc_init(codec_.get(), algo, &config, 0)) 69 if (vpx_codec_enc_init(codec_.get(), algo, &config, 0))
69 return false; 70 return false;
70 return true; 71 return true;
71 } 72 }
72 73
74 static int clip_byte(int x) {
75 if (x > 255)
76 return 255;
77 else if (x < 0)
78 return 0;
79 else
80 return x;
81 }
82
73 bool EncoderVp8::PrepareImage(scoped_refptr<CaptureData> capture_data) { 83 bool EncoderVp8::PrepareImage(scoped_refptr<CaptureData> capture_data) {
84 const int plane_size = capture_data->width() * capture_data->height();
85
74 if (!yuv_image_.get()) { 86 if (!yuv_image_.get()) {
75 const int plane_size = capture_data->width() * capture_data->height();
76 87
77 // YUV image size is 1.5 times of a plane. Multiplication is performed first 88 // YUV image size is 1.5 times of a plane. Multiplication is performed first
78 // to avoid rounding error. 89 // to avoid rounding error.
79 const int size = plane_size * 3 / 2; 90 const int size = plane_size * 3 / 2;
80 91
81 yuv_image_.reset(new uint8[size]); 92 yuv_image_.reset(new uint8[size]);
82 93
83 // Reset image value to 128 so we just need to fill in the y plane. 94 // Reset image value to 128 so we just need to fill in the y plane.
84 memset(yuv_image_.get(), 128, size); 95 memset(yuv_image_.get(), 128, size);
85 96
86 // Fill in the information for |image_|. 97 // Fill in the information for |image_|.
87 unsigned char* image = reinterpret_cast<unsigned char*>(yuv_image_.get()); 98 unsigned char* image = reinterpret_cast<unsigned char*>(yuv_image_.get());
88 image_->planes[0] = image; 99 image_->planes[0] = image;
89 image_->planes[1] = image + plane_size; 100 image_->planes[1] = image + plane_size;
90 101
91 // The V plane starts from 1.25 of the plane size. 102 // The V plane starts from 1.25 of the plane size.
92 image_->planes[2] = image + plane_size + plane_size / 4; 103 image_->planes[2] = image + plane_size + plane_size / 4;
93 104
94 // In YV12 Y plane has full width, UV plane has half width because of 105 // In YV12 Y plane has full width, UV plane has half width because of
95 // subsampling. 106 // subsampling.
96 image_->stride[0] = image_->w; 107 image_->stride[0] = image_->w;
97 image_->stride[1] = image_->w / 2; 108 image_->stride[1] = image_->w / 2;
98 image_->stride[2] = image_->w / 2; 109 image_->stride[2] = image_->w / 2;
99 } 110 }
100 111
101 // And then do RGB->YUV conversion. 112 // And then do RGB->YUV conversion.
102 // Currently we just produce the Y channel as the average of RGB. This will 113 // Currently we just produce the Y channel as the average of RGB. This will
103 // give a gray scale image after conversion. 114 // giv ae gray scale image after conversion.
104 // TODO(hclam): Implement the actual color space conversion. 115 // TODO(sergeyu): Move this code to a separate routine.
105 DCHECK(capture_data->pixel_format() == PixelFormatRgb32) 116 // TODO(sergeyu): Optimize this code.
117 DCHECK(capture_data->pixel_format() == PIXEL_FORMAT_RGB32)
106 << "Only RGB32 is supported"; 118 << "Only RGB32 is supported";
107 uint8* in = capture_data->data_planes().data[0]; 119 uint8* in = capture_data->data_planes().data[0];
108 const int in_stride = capture_data->data_planes().strides[0]; 120 const int in_stride = capture_data->data_planes().strides[0];
109 uint8* out = yuv_image_.get(); 121 uint8* y_out = yuv_image_.get();
122 uint8* u_out = yuv_image_.get() + plane_size;
123 uint8* v_out = yuv_image_.get() + plane_size + plane_size / 4;
110 const int out_stride = image_->stride[0]; 124 const int out_stride = image_->stride[0];
111 for (int i = 0; i < capture_data->height(); ++i) { 125 for (int i = 0; i < capture_data->height(); ++i) {
112 for (int j = 0; j < capture_data->width(); ++j) { 126 for (int j = 0; j < capture_data->width(); ++j) {
113 // Since the input pixel format is RGB32, there are 4 bytes per pixel. 127 // Since the input pixel format is RGB32, there are 4 bytes per pixel.
114 uint8* pixel = in + 4 * j; 128 uint8* pixel = in + 4 * j;
115 out[j] = (pixel[0] + pixel[1] + pixel[2]) / 3; 129 y_out[j] = clip_byte(((pixel[0] * 66 + pixel[1] * 129 +
130 pixel[2] * 25 + 128) >> 8) + 16);
131 if (i % 2 == 0 && j % 2 == 0) {
132 u_out[j / 2] = clip_byte(((pixel[0] * -38 + pixel[1] * -74 +
133 pixel[2] * 112 + 128) >> 8) + 128);
134 v_out[j / 2] = clip_byte(((pixel[0] * 112 + pixel[1] * -94 +
135 pixel[2] * -18 + 128) >> 8) + 128);
136 }
116 } 137 }
117 in += in_stride; 138 in += in_stride;
118 out += out_stride; 139 y_out += out_stride;
140 if (i % 2 == 0) {
141 u_out += out_stride / 2;
142 v_out += out_stride / 2;
143 }
119 } 144 }
120 return true; 145 return true;
121 } 146 }
122 147
123 void EncoderVp8::Encode(scoped_refptr<CaptureData> capture_data, 148 void EncoderVp8::Encode(scoped_refptr<CaptureData> capture_data,
124 bool key_frame, 149 bool key_frame,
125 DataAvailableCallback* data_available_callback) { 150 DataAvailableCallback* data_available_callback) {
126 if (!initialized_) { 151 if (!initialized_) {
127 bool ret = Init(capture_data->width(), capture_data->height()); 152 bool ret = Init(capture_data->width(), capture_data->height());
128 // TODO(hclam): Handle error better. 153 // TODO(hclam): Handle error better.
129 DCHECK(ret) << "Initialization of encoder failed"; 154 DCHECK(ret) << "Initialization of encoder failed";
130 initialized_ = ret; 155 initialized_ = ret;
131 } 156 }
132 157
133 if (!PrepareImage(capture_data)) { 158 if (!PrepareImage(capture_data)) {
134 NOTREACHED() << "Can't image data for encoding"; 159 NOTREACHED() << "Can't image data for encoding";
135 } 160 }
136 161
137 // Do the actual encoding. 162 // Do the actual encoding.
138 vpx_codec_err_t ret = vpx_codec_encode(codec_.get(), image_.get(), 163 vpx_codec_err_t ret = vpx_codec_encode(codec_.get(), image_.get(),
139 last_timestamp_, 164 last_timestamp_,
140 1, 0, VPX_DL_REALTIME); 165 1, 0, VPX_DL_REALTIME);
141 DCHECK(ret == VPX_CODEC_OK) << "Encoding error: " 166 DCHECK_EQ(ret, VPX_CODEC_OK)
142 << vpx_codec_err_to_string(ret) 167 << "Encoding error: " << vpx_codec_err_to_string(ret) << "\n"
143 << "\n" 168 << "Details: " << vpx_codec_error(codec_.get()) << "\n"
144 << "Details: " 169 << vpx_codec_error_detail(codec_.get());
145 << vpx_codec_error(codec_.get())
146 << "\n"
147 << vpx_codec_error_detail(codec_.get());
148 170
149 // TODO(hclam): fix this. 171 // TODO(hclam): fix this.
150 last_timestamp_ += 100; 172 last_timestamp_ += 100;
151 173
152 // Read the encoded data. 174 // Read the encoded data.
153 vpx_codec_iter_t iter = NULL; 175 vpx_codec_iter_t iter = NULL;
154 bool got_data = false; 176 bool got_data = false;
155 177
156 // TODO(hclam): Make sure we get exactly one frame from the packet. 178 // TODO(hclam): Make sure we get exactly one frame from the packet.
157 // TODO(hclam): We should provide the output buffer to avoid one copy. 179 // TODO(hclam): We should provide the output buffer to avoid one copy.
158 ChromotingHostMessage* message = new ChromotingHostMessage(); 180 VideoPacket* message = new VideoPacket();
159 UpdateStreamPacketMessage* packet = message->mutable_update_stream_packet();
160
161 // Prepare the begin rect.
162 packet->mutable_begin_rect()->set_x(0);
163 packet->mutable_begin_rect()->set_y(0);
164 packet->mutable_begin_rect()->set_width(capture_data->width());
165 packet->mutable_begin_rect()->set_height(capture_data->height());
166 packet->mutable_begin_rect()->set_encoding(EncodingVp8);
167 packet->mutable_begin_rect()->set_pixel_format(PixelFormatYv12);
168 181
169 while (!got_data) { 182 while (!got_data) {
170 const vpx_codec_cx_pkt_t* packet = vpx_codec_get_cx_data(codec_.get(), 183 const vpx_codec_cx_pkt_t* packet = vpx_codec_get_cx_data(codec_.get(),
171 &iter); 184 &iter);
172 if (!packet) 185 if (!packet)
173 continue; 186 continue;
174 187
175 switch (packet->kind) { 188 switch (packet->kind) {
176 case VPX_CODEC_CX_FRAME_PKT: 189 case VPX_CODEC_CX_FRAME_PKT:
177 got_data = true; 190 got_data = true;
178 message->mutable_update_stream_packet()->mutable_rect_data()->set_data( 191 message->set_data(
179 packet->data.frame.buf, packet->data.frame.sz); 192 packet->data.frame.buf, packet->data.frame.sz);
180 break; 193 break;
181 default: 194 default:
182 break; 195 break;
183 } 196 }
184 } 197 }
185 198
186 // Enter the end rect. 199 message->mutable_format()->set_encoding(VideoPacketFormat::ENCODING_VP8);
187 message->mutable_update_stream_packet()->mutable_end_rect(); 200 message->set_flags(VideoPacket::FIRST_PACKET | VideoPacket::LAST_PACKET);
188 data_available_callback->Run( 201 message->mutable_format()->set_pixel_format(PIXEL_FORMAT_RGB32);
189 message, 202 message->mutable_format()->set_x(0);
190 EncodingStarting | EncodingInProgress | EncodingEnded); 203 message->mutable_format()->set_y(0);
204 message->mutable_format()->set_width(capture_data->width());
205 message->mutable_format()->set_height(capture_data->height());
206
207 data_available_callback->Run(message);
191 delete data_available_callback; 208 delete data_available_callback;
192 } 209 }
193 210
194 } // namespace remoting 211 } // namespace remoting
OLDNEW
« no previous file with comments | « remoting/base/encoder_verbatim.cc ('k') | remoting/base/encoder_zlib.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698