Chromium Code Reviews| Index: content/common/gpu/media/vaapi_wrapper.cc |
| diff --git a/content/common/gpu/media/vaapi_wrapper.cc b/content/common/gpu/media/vaapi_wrapper.cc |
| index 5e93b994db02dcf52301327e0c285b82a148e001..e5a2e2080641782cc6f24a67284194222029846a 100644 |
| --- a/content/common/gpu/media/vaapi_wrapper.cc |
| +++ b/content/common/gpu/media/vaapi_wrapper.cc |
| @@ -57,27 +57,46 @@ static const VAConfigAttrib kEncodeVAConfigAttribs[] = { |
| VA_ENC_PACKED_HEADER_SEQUENCE | VA_ENC_PACKED_HEADER_PICTURE}, |
| }; |
| +struct ProfileMap { |
| + media::VideoCodecProfile profile; |
| + VAProfile va_profile; |
| +}; |
| + |
| +// A map between VideoCodecProfile and VAProfile. |
| +static const ProfileMap kProfileMap[] = { |
| + {media::H264PROFILE_BASELINE, VAProfileH264Baseline}, |
| + {media::H264PROFILE_MAIN, VAProfileH264Main}, |
| + // TODO(posciak): See if we can/want support other variants of |
| + // media::H264PROFILE_HIGH*. |
| + {media::H264PROFILE_HIGH, VAProfileH264High}}; |
|
kcwu
2014/09/26 10:06:14
how about }; in next line
|
| + |
| +static std::vector<VAConfigAttrib> GetRequiredAttribs( |
| + VaapiWrapper::CodecMode mode) { |
| + std::vector<VAConfigAttrib> required_attribs; |
| + required_attribs.insert( |
| + required_attribs.end(), |
| + kCommonVAConfigAttribs, |
| + kCommonVAConfigAttribs + arraysize(kCommonVAConfigAttribs)); |
| + if (mode == VaapiWrapper::kEncode) { |
| + required_attribs.insert( |
| + required_attribs.end(), |
| + kEncodeVAConfigAttribs, |
| + kEncodeVAConfigAttribs + arraysize(kEncodeVAConfigAttribs)); |
| + } |
| + return required_attribs; |
| +} |
| + |
| // Maps Profile enum values to VaProfile values. |
| static VAProfile ProfileToVAProfile( |
| media::VideoCodecProfile profile, |
| const std::vector<VAProfile>& supported_profiles) { |
| VAProfile va_profile = VAProfileNone; |
| - |
| - switch (profile) { |
| - case media::H264PROFILE_BASELINE: |
| - va_profile = VAProfileH264Baseline; |
| - break; |
| - case media::H264PROFILE_MAIN: |
| - va_profile = VAProfileH264Main; |
| - break; |
| - // TODO(posciak): See if we can/want support other variants |
| - // of media::H264PROFILE_HIGH*. |
| - case media::H264PROFILE_HIGH: |
| - va_profile = VAProfileH264High; |
| - break; |
| - default: |
| + for (size_t i = 0; i < arraysize(kProfileMap); i++) { |
| + if (kProfileMap[i].profile == profile) { |
| + va_profile = kProfileMap[i].va_profile; |
| break; |
| + } |
| } |
| bool supported = std::find(supported_profiles.begin(), |
| @@ -139,6 +158,34 @@ scoped_ptr<VaapiWrapper> VaapiWrapper::Create( |
| return vaapi_wrapper.Pass(); |
| } |
| +std::vector<media::VideoCodecProfile> VaapiWrapper::GetSupportedEncodeProfiles( |
| + Display* x_display, |
| + const base::Closure& report_error_to_uma_cb) { |
| + std::vector<media::VideoCodecProfile> supported_profiles; |
| + |
| + scoped_ptr<VaapiWrapper> wrapper(new VaapiWrapper()); |
| + if (!wrapper->VaInitialize(x_display, report_error_to_uma_cb)) { |
| + return supported_profiles; |
| + } |
| + |
| + std::vector<VAProfile> va_profiles; |
| + if (!wrapper->GetSupportedVaProfiles(&va_profiles)) |
| + return supported_profiles; |
| + |
| + std::vector<VAConfigAttrib> required_attribs = GetRequiredAttribs(kEncode); |
| + for (size_t i = 0; i < arraysize(kProfileMap); i++) { |
| + VAProfile va_profile = VAProfileNone; |
| + if (wrapper->IsProfileSupported(kProfileMap[i].profile, |
| + va_profiles, |
| + VAEntrypointEncSlice, |
| + required_attribs, |
| + &va_profile)) { |
| + supported_profiles.push_back(kProfileMap[i].profile); |
| + } |
| + } |
| + return supported_profiles; |
| +} |
| + |
| void VaapiWrapper::TryToSetVADisplayAttributeToLocalGPU() { |
| VADisplayAttribute item = {VADisplayAttribRenderMode, |
| 1, // At least support '_LOCAL_OVERLAY'. |
| @@ -151,10 +198,8 @@ void VaapiWrapper::TryToSetVADisplayAttributeToLocalGPU() { |
| DVLOG(2) << "vaSetDisplayAttributes unsupported, ignoring by default."; |
| } |
| -bool VaapiWrapper::Initialize(CodecMode mode, |
| - media::VideoCodecProfile profile, |
| - Display* x_display, |
| - const base::Closure& report_error_to_uma_cb) { |
| +bool VaapiWrapper::VaInitialize(Display* x_display, |
| + const base::Closure& report_error_to_uma_cb) { |
| static bool vaapi_functions_initialized = PostSandboxInitialization(); |
| if (!vaapi_functions_initialized) { |
| DVLOG(1) << "Failed to initialize VAAPI libs"; |
| @@ -179,14 +224,18 @@ bool VaapiWrapper::Initialize(CodecMode mode, |
| DVLOG(1) << "VAAPI version < 0.34 is not supported."; |
| return false; |
| } |
| + return true; |
| +} |
| +bool VaapiWrapper::GetSupportedVaProfiles(std::vector<VAProfile>* profiles) { |
| + base::AutoLock auto_lock(va_lock_); |
| // Query the driver for supported profiles. |
| int max_profiles = vaMaxNumProfiles(va_display_); |
| std::vector<VAProfile> supported_profiles( |
| base::checked_cast<size_t>(max_profiles)); |
| int num_supported_profiles; |
| - va_res = vaQueryConfigProfiles( |
| + VAStatus va_res = vaQueryConfigProfiles( |
| va_display_, &supported_profiles[0], &num_supported_profiles); |
| VA_SUCCESS_OR_RETURN(va_res, "vaQueryConfigProfiles failed", false); |
| if (num_supported_profiles < 0 || num_supported_profiles > max_profiles) { |
| @@ -195,23 +244,33 @@ bool VaapiWrapper::Initialize(CodecMode mode, |
| } |
| supported_profiles.resize(base::checked_cast<size_t>(num_supported_profiles)); |
| + *profiles = supported_profiles; |
| + return true; |
| +} |
| - VAProfile va_profile = ProfileToVAProfile(profile, supported_profiles); |
| - if (va_profile == VAProfileNone) { |
| +bool VaapiWrapper::IsProfileSupported( |
| + media::VideoCodecProfile profile, |
| + const std::vector<VAProfile>& supported_profiles, |
| + VAEntrypoint entrypoint, |
| + const std::vector<VAConfigAttrib>& required_attribs, |
| + VAProfile* va_profile) { |
| + *va_profile = ProfileToVAProfile(profile, supported_profiles); |
| + if (*va_profile == VAProfileNone) { |
| DVLOG(1) << "Unsupported profile"; |
| return false; |
| } |
| + base::AutoLock auto_lock(va_lock_); |
| // Query the driver for supported entrypoints. |
| int max_entrypoints = vaMaxNumEntrypoints(va_display_); |
| std::vector<VAEntrypoint> supported_entrypoints( |
| base::checked_cast<size_t>(max_entrypoints)); |
| int num_supported_entrypoints; |
| - va_res = vaQueryConfigEntrypoints(va_display_, |
| - va_profile, |
| - &supported_entrypoints[0], |
| - &num_supported_entrypoints); |
| + VAStatus va_res = vaQueryConfigEntrypoints(va_display_, |
| + *va_profile, |
| + &supported_entrypoints[0], |
| + &num_supported_entrypoints); |
| VA_SUCCESS_OR_RETURN(va_res, "vaQueryConfigEntrypoints failed", false); |
| if (num_supported_entrypoints < 0 || |
| num_supported_entrypoints > max_entrypoints) { |
| @@ -220,9 +279,6 @@ bool VaapiWrapper::Initialize(CodecMode mode, |
| return false; |
| } |
| - VAEntrypoint entrypoint = |
| - (mode == kEncode ? VAEntrypointEncSlice : VAEntrypointVLD); |
| - |
| if (std::find(supported_entrypoints.begin(), |
| supported_entrypoints.end(), |
| entrypoint) == supported_entrypoints.end()) { |
| @@ -231,24 +287,12 @@ bool VaapiWrapper::Initialize(CodecMode mode, |
| } |
| // Query the driver for required attributes. |
| - std::vector<VAConfigAttrib> required_attribs; |
| - required_attribs.insert( |
| - required_attribs.end(), |
| - kCommonVAConfigAttribs, |
| - kCommonVAConfigAttribs + arraysize(kCommonVAConfigAttribs)); |
| - if (mode == kEncode) { |
| - required_attribs.insert( |
| - required_attribs.end(), |
| - kEncodeVAConfigAttribs, |
| - kEncodeVAConfigAttribs + arraysize(kEncodeVAConfigAttribs)); |
| - } |
| - |
| std::vector<VAConfigAttrib> attribs = required_attribs; |
| for (size_t i = 0; i < required_attribs.size(); ++i) |
| attribs[i].value = 0; |
| va_res = vaGetConfigAttributes( |
| - va_display_, va_profile, entrypoint, &attribs[0], attribs.size()); |
| + va_display_, *va_profile, entrypoint, &attribs[0], attribs.size()); |
| VA_SUCCESS_OR_RETURN(va_res, "vaGetConfigAttributes failed", false); |
| for (size_t i = 0; i < required_attribs.size(); ++i) { |
| @@ -260,15 +304,39 @@ bool VaapiWrapper::Initialize(CodecMode mode, |
| return false; |
| } |
| } |
| + return true; |
| +} |
| + |
| +bool VaapiWrapper::Initialize(CodecMode mode, |
| + media::VideoCodecProfile profile, |
| + Display* x_display, |
| + const base::Closure& report_error_to_uma_cb) { |
| + if (!VaInitialize(x_display, report_error_to_uma_cb)) |
| + return false; |
| + std::vector<VAProfile> supported_va_profiles; |
| + if (!GetSupportedVaProfiles(&supported_va_profiles)) |
| + return false; |
| + VAEntrypoint entrypoint = |
| + (mode == kEncode ? VAEntrypointEncSlice : VAEntrypointVLD); |
| + std::vector<VAConfigAttrib> required_attribs = GetRequiredAttribs(mode); |
| + VAProfile va_profile = VAProfileNone; |
| + if (!IsProfileSupported(profile, |
| + supported_va_profiles, |
| + entrypoint, |
| + required_attribs, |
| + &va_profile)) { |
| + return false; |
| + } |
| + base::AutoLock auto_lock(va_lock_); |
| TryToSetVADisplayAttributeToLocalGPU(); |
| - va_res = vaCreateConfig(va_display_, |
| - va_profile, |
| - entrypoint, |
| - &required_attribs[0], |
| - required_attribs.size(), |
| - &va_config_id_); |
| + VAStatus va_res = vaCreateConfig(va_display_, |
| + va_profile, |
| + entrypoint, |
| + &required_attribs[0], |
| + required_attribs.size(), |
| + &va_config_id_); |
| VA_SUCCESS_OR_RETURN(va_res, "vaCreateConfig failed", false); |
| return true; |