| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "content/renderer/pepper/video_encoder_shim.h" | 5 #include "content/renderer/pepper/video_encoder_shim.h" |
| 6 | 6 |
| 7 #include <inttypes.h> | 7 #include <inttypes.h> |
| 8 | 8 |
| 9 #include <deque> | 9 #include <deque> |
| 10 | 10 |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 67 int32_t* min_quantizer, | 67 int32_t* min_quantizer, |
| 68 int32_t* max_quantizer, | 68 int32_t* max_quantizer, |
| 69 int32_t* cpu_used) { | 69 int32_t* cpu_used) { |
| 70 switch (codec) { | 70 switch (codec) { |
| 71 case media::VP8PROFILE_ANY: | 71 case media::VP8PROFILE_ANY: |
| 72 *vpx_codec = vpx_codec_vp8_cx(); | 72 *vpx_codec = vpx_codec_vp8_cx(); |
| 73 *min_quantizer = kVp8DefaultMinQuantizer; | 73 *min_quantizer = kVp8DefaultMinQuantizer; |
| 74 *max_quantizer = kVp8DefaultMaxQuantizer; | 74 *max_quantizer = kVp8DefaultMaxQuantizer; |
| 75 *cpu_used = kVp8DefaultCpuUsed; | 75 *cpu_used = kVp8DefaultCpuUsed; |
| 76 break; | 76 break; |
| 77 case media::VP9PROFILE_ANY: | 77 case media::VP9PROFILE_PROFILE0: |
| 78 case media::VP9PROFILE_PROFILE1: |
| 79 case media::VP9PROFILE_PROFILE2: |
| 80 case media::VP9PROFILE_PROFILE3: |
| 81 // TODO(servolk): These numbers should be adjusted for different profiles. |
| 82 // crbug.com/598107 |
| 78 *vpx_codec = vpx_codec_vp9_cx(); | 83 *vpx_codec = vpx_codec_vp9_cx(); |
| 79 *min_quantizer = kVp9DefaultMinQuantizer; | 84 *min_quantizer = kVp9DefaultMinQuantizer; |
| 80 *max_quantizer = kVp9DefaultMaxQuantizer; | 85 *max_quantizer = kVp9DefaultMaxQuantizer; |
| 81 *cpu_used = kVp9DefaultCpuUsed; | 86 *cpu_used = kVp9DefaultCpuUsed; |
| 82 break; | 87 break; |
| 83 default: | 88 default: |
| 84 *vpx_codec = nullptr; | 89 *vpx_codec = nullptr; |
| 85 *min_quantizer = 0; | 90 *min_quantizer = 0; |
| 86 *max_quantizer = 0; | 91 *max_quantizer = 0; |
| 87 *cpu_used = 0; | 92 *cpu_used = 0; |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 189 config_.rc_max_quantizer = max_quantizer; | 194 config_.rc_max_quantizer = max_quantizer; |
| 190 // Do not saturate CPU utilization just for encoding. On a lower-end system | 195 // Do not saturate CPU utilization just for encoding. On a lower-end system |
| 191 // with only 1 or 2 cores, use only one thread for encoding. On systems with | 196 // with only 1 or 2 cores, use only one thread for encoding. On systems with |
| 192 // more cores, allow half of the cores to be used for encoding. | 197 // more cores, allow half of the cores to be used for encoding. |
| 193 config_.g_threads = | 198 config_.g_threads = |
| 194 std::min(kMaxNumThreads, (base::SysInfo::NumberOfProcessors() + 1) / 2); | 199 std::min(kMaxNumThreads, (base::SysInfo::NumberOfProcessors() + 1) / 2); |
| 195 | 200 |
| 196 // Use Q/CQ mode if no target bitrate is given. Note that in the VP8/CQ case | 201 // Use Q/CQ mode if no target bitrate is given. Note that in the VP8/CQ case |
| 197 // the meaning of rc_target_bitrate changes to target maximum rate. | 202 // the meaning of rc_target_bitrate changes to target maximum rate. |
| 198 if (initial_bitrate == 0) { | 203 if (initial_bitrate == 0) { |
| 199 if (output_profile == media::VP9PROFILE_ANY) { | 204 if (output_profile == media::VP9PROFILE_PROFILE0 || |
| 205 output_profile == media::VP9PROFILE_PROFILE1 || |
| 206 output_profile == media::VP9PROFILE_PROFILE2 || |
| 207 output_profile == media::VP9PROFILE_PROFILE3) { |
| 200 config_.rc_end_usage = VPX_Q; | 208 config_.rc_end_usage = VPX_Q; |
| 201 } else if (output_profile == media::VP8PROFILE_ANY) { | 209 } else if (output_profile == media::VP8PROFILE_ANY) { |
| 202 config_.rc_end_usage = VPX_CQ; | 210 config_.rc_end_usage = VPX_CQ; |
| 203 config_.rc_target_bitrate = kVp8MaxCQBitrate; | 211 config_.rc_target_bitrate = kVp8MaxCQBitrate; |
| 204 } | 212 } |
| 205 } | 213 } |
| 206 | 214 |
| 207 vpx_codec_flags_t flags = 0; | 215 vpx_codec_flags_t flags = 0; |
| 208 if (vpx_codec_enc_init(&encoder_, vpx_codec, &config_, flags) != | 216 if (vpx_codec_enc_init(&encoder_, vpx_codec, &config_, flags) != |
| 209 VPX_CODEC_OK) { | 217 VPX_CODEC_OK) { |
| 210 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); | 218 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
| 211 return; | 219 return; |
| 212 } | 220 } |
| 213 initialized_ = true; | 221 initialized_ = true; |
| 214 | 222 |
| 215 if (vpx_codec_enc_config_set(&encoder_, &config_) != VPX_CODEC_OK) { | 223 if (vpx_codec_enc_config_set(&encoder_, &config_) != VPX_CODEC_OK) { |
| 216 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); | 224 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
| 217 return; | 225 return; |
| 218 } | 226 } |
| 219 | 227 |
| 220 if (vpx_codec_control(&encoder_, VP8E_SET_CPUUSED, cpu_used) != | 228 if (vpx_codec_control(&encoder_, VP8E_SET_CPUUSED, cpu_used) != |
| 221 VPX_CODEC_OK) { | 229 VPX_CODEC_OK) { |
| 222 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); | 230 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
| 223 return; | 231 return; |
| 224 } | 232 } |
| 225 | 233 |
| 226 if (output_profile == media::VP9PROFILE_ANY) { | 234 if (output_profile == media::VP9PROFILE_PROFILE0 || |
| 235 output_profile == media::VP9PROFILE_PROFILE1 || |
| 236 output_profile == media::VP9PROFILE_PROFILE2 || |
| 237 output_profile == media::VP9PROFILE_PROFILE3) { |
| 227 if (vpx_codec_control(&encoder_, VP9E_SET_AQ_MODE, | 238 if (vpx_codec_control(&encoder_, VP9E_SET_AQ_MODE, |
| 228 kVp9AqModeCyclicRefresh) != VPX_CODEC_OK) { | 239 kVp9AqModeCyclicRefresh) != VPX_CODEC_OK) { |
| 229 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); | 240 NotifyError(media::VideoEncodeAccelerator::kPlatformFailureError); |
| 230 return; | 241 return; |
| 231 } | 242 } |
| 232 } | 243 } |
| 233 | 244 |
| 234 renderer_task_runner_->PostTask( | 245 renderer_task_runner_->PostTask( |
| 235 FROM_HERE, | 246 FROM_HERE, |
| 236 base::Bind(&VideoEncoderShim::OnRequireBitstreamBuffers, shim_, | 247 base::Bind(&VideoEncoderShim::OnRequireBitstreamBuffers, shim_, |
| (...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 382 // Libvpx and media::VideoEncodeAccelerator are using opposite | 393 // Libvpx and media::VideoEncodeAccelerator are using opposite |
| 383 // notions of denominator/numerator. | 394 // notions of denominator/numerator. |
| 384 profile.max_framerate_numerator = config.g_timebase.den; | 395 profile.max_framerate_numerator = config.g_timebase.den; |
| 385 profile.max_framerate_denominator = config.g_timebase.num; | 396 profile.max_framerate_denominator = config.g_timebase.num; |
| 386 profiles.push_back(profile); | 397 profiles.push_back(profile); |
| 387 } | 398 } |
| 388 | 399 |
| 389 ret = vpx_codec_enc_config_default(vpx_codec_vp9_cx(), &config, 0); | 400 ret = vpx_codec_enc_config_default(vpx_codec_vp9_cx(), &config, 0); |
| 390 if (ret == VPX_CODEC_OK) { | 401 if (ret == VPX_CODEC_OK) { |
| 391 media::VideoEncodeAccelerator::SupportedProfile profile; | 402 media::VideoEncodeAccelerator::SupportedProfile profile; |
| 392 profile.profile = media::VP9PROFILE_ANY; | |
| 393 profile.max_resolution = gfx::Size(kMaxWidth, kMaxHeight); | 403 profile.max_resolution = gfx::Size(kMaxWidth, kMaxHeight); |
| 394 profile.max_framerate_numerator = config.g_timebase.den; | 404 profile.max_framerate_numerator = config.g_timebase.den; |
| 395 profile.max_framerate_denominator = config.g_timebase.num; | 405 profile.max_framerate_denominator = config.g_timebase.num; |
| 406 profile.profile = media::VP9PROFILE_PROFILE0; |
| 407 profiles.push_back(profile); |
| 408 profile.profile = media::VP9PROFILE_PROFILE1; |
| 409 profiles.push_back(profile); |
| 410 profile.profile = media::VP9PROFILE_PROFILE2; |
| 411 profiles.push_back(profile); |
| 412 profile.profile = media::VP9PROFILE_PROFILE3; |
| 396 profiles.push_back(profile); | 413 profiles.push_back(profile); |
| 397 } | 414 } |
| 398 | 415 |
| 399 return profiles; | 416 return profiles; |
| 400 } | 417 } |
| 401 | 418 |
| 402 bool VideoEncoderShim::Initialize( | 419 bool VideoEncoderShim::Initialize( |
| 403 media::VideoPixelFormat input_format, | 420 media::VideoPixelFormat input_format, |
| 404 const gfx::Size& input_visible_size, | 421 const gfx::Size& input_visible_size, |
| 405 media::VideoCodecProfile output_profile, | 422 media::VideoCodecProfile output_profile, |
| 406 uint32_t initial_bitrate, | 423 uint32_t initial_bitrate, |
| 407 media::VideoEncodeAccelerator::Client* client) { | 424 media::VideoEncodeAccelerator::Client* client) { |
| 408 DCHECK(RenderThreadImpl::current()); | 425 DCHECK(RenderThreadImpl::current()); |
| 409 DCHECK_EQ(client, host_); | 426 DCHECK_EQ(client, host_); |
| 410 | 427 |
| 411 if (input_format != media::PIXEL_FORMAT_I420) | 428 if (input_format != media::PIXEL_FORMAT_I420) |
| 412 return false; | 429 return false; |
| 413 | 430 |
| 414 if (output_profile != media::VP8PROFILE_ANY && | 431 if (output_profile != media::VP8PROFILE_ANY && |
| 415 output_profile != media::VP9PROFILE_ANY) | 432 output_profile != media::VP9PROFILE_PROFILE0 && |
| 433 output_profile != media::VP9PROFILE_PROFILE1 && |
| 434 output_profile != media::VP9PROFILE_PROFILE2 && |
| 435 output_profile != media::VP9PROFILE_PROFILE3) |
| 416 return false; | 436 return false; |
| 417 | 437 |
| 418 media_task_runner_->PostTask( | 438 media_task_runner_->PostTask( |
| 419 FROM_HERE, | 439 FROM_HERE, |
| 420 base::Bind(&VideoEncoderShim::EncoderImpl::Initialize, | 440 base::Bind(&VideoEncoderShim::EncoderImpl::Initialize, |
| 421 base::Unretained(encoder_impl_.get()), input_format, | 441 base::Unretained(encoder_impl_.get()), input_format, |
| 422 input_visible_size, output_profile, initial_bitrate)); | 442 input_visible_size, output_profile, initial_bitrate)); |
| 423 | 443 |
| 424 return true; | 444 return true; |
| 425 } | 445 } |
| (...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 483 } | 503 } |
| 484 | 504 |
| 485 void VideoEncoderShim::OnNotifyError( | 505 void VideoEncoderShim::OnNotifyError( |
| 486 media::VideoEncodeAccelerator::Error error) { | 506 media::VideoEncodeAccelerator::Error error) { |
| 487 DCHECK(RenderThreadImpl::current()); | 507 DCHECK(RenderThreadImpl::current()); |
| 488 | 508 |
| 489 host_->NotifyError(error); | 509 host_->NotifyError(error); |
| 490 } | 510 } |
| 491 | 511 |
| 492 } // namespace content | 512 } // namespace content |
| OLD | NEW |