1# VPE(multimedia_video_processing_engine) 2 3## Overview 4The Video Processing Engine (VPE) is a media engine for processing video and image data. It offers a range of fundamental capabilities including enhancements to details, contrast, luminance, and dynamic ranges. It also supports essential algorithms for color space conversion, scaling and upscaling, and dynamic metadata generation for transcoding, sharing, and post-processing for display. 5 6The following figure demonstrates the VPE architecture. 7 8 9 10 11#### 各模块功能说明 12 13<table> 14<tr> 15<td bgcolor=#F5F5F5> Level </td> 16<td bgcolor=#F5F5F5> module </td> 17<td bgcolor=#F5F5F5> Description of the function </td> 18</tr> 19<tr> 20<td rowspan="7" colspan="1" > Interface </td> 21<td> Video color space CAPI </td> 22<td> Provides interfaces for color space conversion in video scenes </td> 23</tr> 24<tr> 25<td> Picture color space CAPI </td> 26<td> Provides APIs for color space conversion of image scenes </td> 27</tr> 28<tr> 29<td> Video detail enhancement CAPI </td> 30<td> Provides APIs for video superresolution algorithms and sharpening algorithms </td> 31</tr> 32<tr> 33<td> Picture detail enhancement CAPI </td> 34<td> Provides APIs related to image superresolution algorithm and sharpening algorithm </td> 35</tr> 36<tr> 37<td> Video dynamic metadata CAPI </td> 38<td> APIs are provided for invoking dynamic metadata generation algorithms for video content </td> 39</tr> 40<tr> 41<td> Image dynamic metadata CAPI </td> 42<td> APIs are provided for dynamic metadata generation algorithms for image content </td> 43</tr> 44<tr> 45<td> The TS interface is enhanced for image details </td> 46<td> Provides TS interfaces for image super-resolution algorithm and sharpening algorithm </td> 47</tr> 48<tr> 49<td rowspan="6" colspan="1" > Atomically capable encapsulation layer </td> 50<td> Video color space atomic capabilities </td> 51<td> Realize the channel scheduling and context management of the video scene color space conversion software, and realize the process control of the video stream </td> 52</tr> 53<tr> 54<td> Picture color space atomic capability </td> 55<td> Realize the color space conversion software channel scheduling of picture scenes </td> 56</tr> 57<tr> 58<td> Video details enhance atomic capabilities </td> 59<td> Realize video scene clarity enhancement and scaling algorithm software channel scheduling and context management, and realize video stream process control </td> 60</tr> 61<tr> 62<td> Picture details enhance atomic capabilities </td> 63<td> Realize the image scene clarity enhancement and scaling algorithm software channel scheduling </td> 64</tr> 65<tr> 66<td> Dynamic metadata atomic capabilities for video </td> 67<td> Realize the dynamic metadata generation software channel scheduling of video scenes </td> 68</tr> 69<tr> 70<td> Dynamic metadata atomic capabilities for images </td> 71<td> Realize the dynamic metadata generation software channel scheduling of image scenes </td> 72</tr> 73<tr> 74<td rowspan="6" colspan="1" > Algorithm plug-in layer </td> 75<td> Video color space processing algorithm plug-in </td> 76<td> Implement the function of video color space conversion algorithm, including SDR2SDR, HDR2SDR, and HDR2HDR </td> 77</tr> 78<tr> 79<td> Plug-in for image color space algorithms </td> 80<td> Realize the function of image color space conversion algorithm, including SDR2SDR, single-layer to double-layer, double-layer to single-layer </td> 81</tr> 82<tr> 83<td> Video Detail Enhancement Algorithm Plug-in </td> 84<td> Implement video scaling and image quality enhancement algorithms </td> 85</tr> 86<tr> 87<td> Plug-in for image detail enhancement algorithms </td> 88<td> Implement image scaling and image quality enhancement algorithms </td> 89</tr> 90<tr> 91<td> Video dynamic metadata algorithm plug-in </td> 92<td> Implement the video dynamic metadata production algorithm </td> 93</tr> 94<tr> 95<td> Image dynamic metadata algorithm plug-in </td> 96<td> Implement the dynamic metadata generation algorithm for images </td> 97</tr> 98<tr> 99<td rowspan="3" colspan="1"> Plugin management </td> 100<td> Plugin registration </td> 101<td> Provide the function of system developer plug-in registration </td> 102</tr> 103<tr> 104<td> Capability inquiry </td> 105<td> Application developers can use the capability query function to confirm whether a given plug-in is supported on that device or system </td> 106</tr> 107<tr> 108<td> Plug-in calls </td> 109<td> Invoke specific plug-in capabilities to complete related algorithm functions </td> 110</tr> 111<tr> 112<td rowspan="2" colspan="1"> Service management </td> 113<td> Resource management </td> 114<td> Resource scheduling and algorithm context management, such as information about the frames before and after video content </td> 115</tr> 116<tr> 117<td> Process management </td> 118<td> Complete functions such as cross-process communication </td> 119</tr> 120</table> 121 122 123| Dependency modules | Description of the function | 124| :-- | :-- | 125| graphic_graphic_surface | Provide video surface support | 126| graphic_graphic_2d | Provide image surfacebuffer support | 127| multimedia_media_foundation | Pixelmap support is available | 128| multimedia_image_framework | Format parameter setting is supported | 129| third_party_skia | Provides a scaling algorithm | 130 131## Directory Structure 132 133The structure of the repository directory is as follows: 134 135``` 136/foundation/multimedia/video_processing_engine/ 137├── framework # Framework code 138│ ├── algorithm # Algorithm framework 139│ ├── aihdr_enhancer # Image HDR enhancement algorithm framework 140│ ├── aihdr_enhancer_video # Video HDR enhancement algorithm framework 141│ ├── colorspace_converter # Image color space conversion algorithm framework 142│ ├── colorspace_converter_display # Image color space display algorithm framework 143│ ├── colorspace_converter_video # Video color space conversion algorithm framework 144│ ├── common # Common 145│ ├── contrast_enhancer # Contrast enhancement algorithm framework 146│ ├── detail_enhancer # Image detail enhancement algorithm framework 147│ ├── detail_enhancer_video # Video detail enhancement algorithm framework 148│ ├── extension_manager # Plugin management 149│ ├── metadata_generator # Image metadata generation algorithm framework 150│ ├── metadata_generator_video # Video metadata generation algorithm framework 151│ ├── video_variable_refresh_rate # Video variable frame rate algorithm framework 152│ ├── capi # CAPI layer 153│ ├── image_processing # Image CAPI 154│ ├── video_processing # Video CAPI 155│ ├── dfx # DFX code 156├── interfaces # API layer 157│ ├── inner_api # Internal APIs 158│ ├── kits # Application APIs 159├── services # Service code 160├── sertestvices # Test code 161``` 162 163## Build 164 165Run the following command to build the VPE for the 32-bit ARM system: 166``` 167./build.sh --product-name {product_name} --ccache --build-target video_processing_engine 168``` 169 170Run the following command to build the VPE for the 64-bit ARM system: 171``` 172./build.sh --product-name {product_name} --ccache --target-cpu arm64 --build-target video_processing_engine 173``` 174 175**product_name** indicates the product supported, for example, **rk3568**. 176 177## Description 178 179### How to Use 180As a component of OpenHarmony, the VPE provides video and image processing capabilities, including color space conversion, dynamic metadata generation, and detail enhancement. 181 182#### The app developer calls the image scaling sample 1831. Add a header file. 184 ```cpp 185 #include <hilog/log.h> 186 #include <multimedia/image_framework/image_pixel_map_mdk.h> 187 #include <multimedia/image_framework/image/pixelmap_native.h> 188 #include <multimedia/video_processing_engine/image_processing.h> 189 #include <multimedia/video_processing_engine/image_processing_types.h> 190 #include <multimedia/player_framework/native_avformat.h> 191 #include <napi/native_api.h> 192 ``` 1932. Optionally, initialize the environment. 194 Generally, it is called when it is used for the first time in the process, and some time-consuming operations can be completed in advance. 195 ```cpp 196 ImageProcessing_ErrorCode ret = OH_ImageProcessing_InitializeEnvironment(); 197 ``` 1983. Create a detail enhancement module. 199 Applications can create image detail enhancement modules by image processing engine module types. The variables in the example are described as follows: 200 imageProcessor:an instance of the Detail Enhancement module. 201 IMAGE_PROCESSING_TYPE_DETAIL_ENHANCER:Detail enhancement type. 202 Expected return value: IMAGE_PROCESSING_SUCCESS 203 ```cpp 204 ImageProcessing_ErrorCode ret = OH_ImageProcessing_Create(&imageProcessor, IMAGE_PROCESSING_TYPE_DETAIL_ENHANCER); 205 ``` 2064. (Optional) Configure the detail enhancement gear, there are currently three gears of high, medium and low and NONE optional, if not configured, the default gear is LOW. 207 ```cpp 208 OH_AVFormat* parameter = OH_AVFormat_Create(); 209 OH_AVFormat_SetIntValue(parameter, IMAGE_DETAIL_ENHANCER_PARAMETER_KEY_QUALITY_LEVEL, 210 IMAGE_DETAIL_ENHANCER_QUALITY_LEVEL_HIGH); 211 ImageProcessing_ErrorCode ret = OH_ImageProcessing_SetParameter(imageProcessor,parameter); 212 ``` 2135. Initiates detail enhancement processing. 214 ```cpp 215 ImageProcessing_ErrorCode ret = OH_ImageProcessing_EnhanceDetail(imageProcessor, srcImage, dstImage); 216 ``` 2176. Release the processing instance. 218 ```cpp 219 ImageProcessing_ErrorCode ret = OH_ImageProcessing_Destroy(imageProcessor); 220 ``` 2217. Free up processing resources. 222 ```cpp 223 OH_ImageProcessing_DeinitializeEnvironment(); 224 ``` 225#### The app developer calls the video zoom sample 2261. Add a header file. 227 ```cpp 228 #include <ace/xcomponent/native_interface_xcomponent.h> 229 #include <multimedia/player_framework/native_avformat.h> 230 #include <multimedia/video_processing_engine/video_processing.h> 231 #include <multimedia/video_processing_engine/video_processing_types.h> 232 #include <native_window/external_window.h> 233 #include <native_buffer/native_buffer.h> 234 ``` 2352. Optionally, create a decode instance. 236 The input of the detail enhancement module can be a video stream decoded by the system, or the application can fill the window with video data (for example, the application directly fills the data into the window after internal soft decoding). If you select a system decoder to process a video file or video stream, you can create a decoder instance as input to the Detail Enhancement module. 237 ```cpp 238 OH_AVSource* source_ = OH_AVSource_CreateWithFD(inputFd, inputFileOffset, inputFileSize); 239 OH_AVDemuxer* demuxer_ = OH_AVDemuxer_CreateWithSource(source_); 240 auto sourceFormat = std::shared_ptr<OH_AVFormat>(OH_AVSource_GetSourceFormat(source_), OH_AVFormat_Destroy); 241 OH_AVCodec * decoder_ = OH_VideoDecoder_CreateByMime(videoCodecMime.c_str()); 242 OH_AVFormat *format = OH_AVFormat_Create(); 243 OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, videoWidth); 244 OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, videoHeight); 245 OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_FRAME_RATE, frameRate); 246 OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, pixelFormat); 247 OH_AVFormat_SetIntValue(format, OH_MD_KEY_ROTATION, rotation); 248 int ret = OH_VideoDecoder_Configure(decoder_, format); 249 OH_AVFormat_Destroy(format); 250 OH_VideoDecoder_RegisterCallback(decoder_, 251 {SampleCallback::OnCodecError, SampleCallback::OnCodecFormatChange, 252 SampleCallback::OnNeedInputBuffer, SampleCallback::OnNewOutputBuffer}, videoDecContext_); 253 254 int ret = OH_VideoDecoder_Prepare(decoder_); 255 256 videoDecContext_ = new CodecUserData; 257 ``` 2583. Optionally, initialize the environment. 259 Generally, it is called when it is used for the first time in the process, and some time-consuming operations can be completed in advance. 260 ```cpp 261 VideoProcessing_ErrorCode ret = OH_VideoProcessing_InitializeEnvironment(); 262 ``` 2634. Create a detail enhancement module. 264 Applications can create detail enhancement modules by video processing engine module types. The variables in the example are described as follows: 265 videoProcessor:an instance of the Detail Enhancement module. 266 VIDEO_PROCESSING_TYPE_DETAIL_ENHANCER:Detail enhancement type. 267 Expected return value: VIDEO_PROCESSING_SUCCESS 268 ```cpp 269 VideoProcessing_ErrorCode ret = OH_VideoProcessing_Create(&videoProcessor, VIDEO_PROCESSING_TYPE_DETAIL_ENHANCER); 270 ``` 2715. Configure an asynchronous callback function. 272 ```cpp 273 ret = OH_VideoProcessingCallback_Create(&callback); 274 OH_VideoProcessingCallback_BindOnError(callback, OnError); 275 OH_VideoProcessingCallback_BindOnState(callback, OnState); 276 OH_VideoProcessingCallback_BindOnNewOutputBuffer(callback, OnNewOutputBuffer); 277 ret = OH_VideoProcessing_RegisterCallback(videoProcessor, callback, this); 278 void OnError(OH_VideoProcessing* videoProcessor, VideoProcessing_ErrorCode error, void* userData); 279 void OnState(OH_VideoProcessing* videoProcessor, VideoProcessing_State state, void* userData); 280 void OnNewOutputBuffer(OH_VideoProcessing* videoProcessor, uint32_t index, void* userData); 281 ``` 2826. (Optional) Configure the detail enhancement gear, there are currently three gears of high, medium and low and NONE optional, if not configured, the default gear is LOW. 283 ```cpp 284 OH_AVFormat* parameter = OH_AVFormat_Create(); 285 OH_AVFormat_SetIntValue(parameter, VIDEO_DETAIL_ENHANCER_PARAMETER_KEY_QUALITY_LEVEL, VIDEO_DETAIL_ENHANCER_QUALITY_LEVEL_HIGH); 286 OH_VideoProcessing_SetParameter(videoProcessor, parameter); 287 ``` 2887. Get your Surface. 289 ```cpp 290 ret = OH_VideoProcessing_GetSurface(videoProcessor, inputWindow); 291 OH_VideoDecoder_SetSurface(decoder_, inputWindow_); 292 ``` 2938. Set Surface 294 ```cpp 295 ret = OH_VideoProcessing_SetSurface(videoProcessor, outWindow); 296 ``` 2979. Create a decoder input and output thread. 298 ```cpp 299 std::unique_ptr<std::thread> videoDecInputThread_ = std::make_unique<std::thread>(&Player::VideoDecInputThread, this); 300 std::unique_ptr<std::thread> videoDecOutputThread_ = std::make_unique<std::thread>(&Player::VideoDecOutputThread, this); 301 ``` 30210. Initiates detail enhancement processing. 303 ```cpp 304 int ret = OH_VideoDecoder_Start(decoder_); 305 ret = OH_VideoProcessing_Start(videoProcessor); 306 ``` 30711. Call OH_VideoProcessing_Stop() 308 ```cpp 309 VideoProcessing_ErrorCode ret = OH_VideoProcessing_Stop(videoProcessor); 310 ``` 31112. Release the processing instance. 312 ```cpp 313 VideoProcessing_ErrorCode ret = OH_VideoProcessing_Destroy(videoProcessor); 314 VideoProcessing_ErrorCode ret = OH_VideoProcessingCallback_Destroy(callback); 315 ``` 31613. Free up processing resources. 317 ```cpp 318 VideoProcessing_ErrorCode ret = OH_VideoProcessing_DeinitializeEnvironment(); 319 ``` 320#### Custom algorithm plugin registration 3211. Implement plugin registration function 322 ```cpp 323 std::vector<MetadataGeneratorCapability> ImageMetadataGen::BuildCapabilities() 324 { 325 std::vector<MetadataGeneratorCapability> capabilities; 326 for (const auto &inColorspace : inColorspaceList) { 327 MetadataGeneratorCapability capability = { inColorspace, pixelFormatMap, RANK, IMAGEMETAGENVERSION }; 328 capabilities.emplace_back(capability); 329 } 330 return capabilities; 331 } 332 333 static std::vector<std::shared_ptr<OHOS::Media::VideoProcessingEngine::Extension::ExtensionBase>> RegisterExtensions() 334 { 335 std::vector<std::shared_ptr<OHOS::Media::VideoProcessingEngine::Extension::ExtensionBase>> extensions; 336 auto extension = std::make_shared<OHOS::Media::VideoProcessingEngine::Extension::MetadataGeneratorExtension>(); 337 extension->info = { OHOS::Media::VideoProcessingEngine::Extension::ExtensionType::METADATA_GENERATOR, 338 "ImageMetadataGen", "v1" }; 339 extension->capabilitiesBuilder = OHOS::Media::VideoProcessingEngine::ImageMetadataGen::BuildCapabilities; 340 extensions.push_back( 341 std::static_pointer_cast<OHOS::Media::VideoProcessingEngine::Extension::ExtensionBase>(extension)); 342 return extensions; 343 } 344 345 void RegisterImageMetadataGeneratorExtensions(uintptr_t extensionListAddr) 346 { 347 OHOS::Media::VideoProcessingEngine::Extension::DoRegisterExtensions(extensionListAddr, RegisterExtensions); 348 } 349 ``` 3502. Implement algorithm 351 ```cpp 352 ImageMetadataGen::Process(const sptr<SurfaceBuffer> &input) 353 ``` 3543. Add registered plugin callback function. 355 Add at staticExtensionsRegisterMap 356 ```cpp 357 const std::unordered_map<std::string, RegisterExtensionFunc> staticExtensionsRegisterMap = { 358 {"ImageMetadataGeneratorExtensions", RegisterImageMetadataGeneratorExtensions} 359 }; 360 ``` 361 362#### The system developer implements a custom algorithm plug-in registration example 363The video processing engine provides algorithm plug-in registration interfaces such as color space conversion, dynamic metadata generation, and detail enhancement, and system users can register their own algorithms to enrich algorithm plug-ins. The video processing engine defines the basic classes of various algorithms, as follows: 364| Function | Base class | Header file | 365| :-- | :-- | :-- | 366| Video color space conversion | ColorSpaceConverterBase | colorspace_converter_base.h | 367| Picture color space conversion | ColorSpaceConverterBase | colorspace_converter_base.h | 368| Video detail enhancement | DetailEnhancerBase | detail_enhancer_base.h | 369| Picture detail enhancement | DetailEnhancerBase | detail_enhancer_base.h | 370| Video dynamic metadata | MetadataGeneratorBase | metadata_generator_base.h | 371| Image dynamic metadata | MetadataGeneratorBase | metadata_generator_base.h | 372 373<br> 374Take the scaling algorithm as an example, the plug-in algorithm has been implemented in framework/algorithm/extensions/skia, and the specific development steps are described below 375 3761. Create a new folder in the framework/algorithm/extensions directory, for example, skia, along with the corresponding cpp and h files 377 3782. The scaling algorithm header file is implemented, see framework/algorithm/extensions/skia/include/skia_impl.h 379 380```cpp 381#ifndef SKIA_IMPL_H 382#define SKIA_IMPL_H 383 384#include "surface_buffer.h" 385#include "include/core/SkYUVAPixmaps.h" 386 387#include "algorithm_errors.h" 388#include "detail_enhancer_base.h" 389#include "detail_enhancer_capability.h" 390 391namespace OHOS { 392namespace Media { 393namespace VideoProcessingEngine { 394 395class Skia : public DetailEnhancerBase { 396public: 397 Skia() = default; 398 virtual ~Skia() = default; 399 Skia(const Skia&) = delete; 400 Skia& operator=(const Skia&) = delete; 401 Skia(Skia&&) = delete; 402 Skia& operator=(Skia&&) = delete; 403 static std::unique_ptr<DetailEnhancerBase> Create(); 404 static DetailEnhancerCapability BuildCapabilities(); 405 VPEAlgoErrCode Init() override; 406 VPEAlgoErrCode Deinit() override; 407 VPEAlgoErrCode SetParameter(const DetailEnhancerParameters& parameter, int type, bool flag) override; 408 VPEAlgoErrCode Process(const sptr<SurfaceBuffer>& input, const sptr<SurfaceBuffer>& output) override; 409}; 410void RegisterSkiaExtensions(uintptr_t extensionListAddr); 411} // VideoProcessingEngine 412} // Media 413} // OHOS 414#endif // SKIA_IMPL_H 415``` 4163. For details on plug-in algorithm capability registration and plug-in algorithm implementation, see framework/algorithm/extensions/skia/skia_impl.cpp 417 418```cpp 419#include "skia_impl.h" 420 421#include <array> 422#include <chrono> 423#include <dlfcn.h> 424#include "detail_enhancer_extension.h" 425#include "utils.h" 426 427#include "vpe_log.h" 428 429namespace OHOS { 430namespace Media { 431namespace VideoProcessingEngine { 432 433namespace { 434 435constexpr Extension::Rank RANK = Extension::Rank::RANK_DEFAULT; 436constexpr uint32_t VERSION = 0; 437} // namespace 438 439std::unique_ptr<DetailEnhancerBase> Skia::Create() 440{ 441 return std::make_unique<Skia>(); 442} 443 444 445DetailEnhancerCapability Skia::BuildCapabilities() 446{ 447 std::vector<uint32_t> levels = { DETAIL_ENH_LEVEL_NONE, DETAIL_ENH_LEVEL_LOW, DETAIL_ENH_LEVEL_MEDIUM, 448 DETAIL_ENH_LEVEL_HIGH_EVE, DETAIL_ENH_LEVEL_HIGH_AISR, DETAIL_ENH_LEVEL_VIDEO}; 449 DetailEnhancerCapability capability = { levels, RANK, VERSION }; 450 return capability; 451} 452 453VPEAlgoErrCode Skia::Init() 454{ 455 return VPE_ALGO_ERR_OK; 456} 457 458VPEAlgoErrCode Skia::Deinit() 459{ 460 return VPE_ALGO_ERR_OK; 461} 462 463VPEAlgoErrCode Skia::SetParameter([[maybe_unused]] const DetailEnhancerParameters& parameter, 464 [[maybe_unused]] int type, [[maybe_unused]] bool flag) 465{ 466 return VPE_ALGO_ERR_OK; 467} 468 469VPEAlgoErrCode Skia::Process(const sptr<SurfaceBuffer>& input, const sptr<SurfaceBuffer>& output) 470{ 471 return errCode; 472} 473 474static std::vector<std::shared_ptr<Extension::ExtensionBase>> RegisterExtensions() 475{ 476 std::vector<std::shared_ptr<Extension::ExtensionBase>> extensions; 477 478 auto extension = std::make_shared<Extension::DetailEnhancerExtension>(); 479 CHECK_AND_RETURN_RET_LOG(extension != nullptr, extensions, "null pointer"); 480 extension->info = { Extension::ExtensionType::DETAIL_ENHANCER, "SKIA", "0.0.1" }; 481 extension->creator = Skia::Create; 482 extension->capabilitiesBuilder = Skia::BuildCapabilities; 483 extensions.push_back(std::static_pointer_cast<Extension::ExtensionBase>(extension)); 484 485 return extensions; 486} 487 488void RegisterSkiaExtensions(uintptr_t extensionListAddr) 489{ 490 Extension::DoRegisterExtensions(extensionListAddr, RegisterExtensions); 491} 492} // VideoProcessingEngine 493} // Media 494} // OHOS 495``` 496 4974. Add a callback function for registering a plug-in, and the VPE plug-in management will iterate through all plug-in registration functions. 498 see framework/algorithm/extension_manager/include/static_extension_list.h 499```cpp 500const std::unordered_map<std::string, RegisterExtensionFunc> staticExtensionsRegisterMap = { 501 { "Skia", RegisterSkiaExtensions }, 502}; 503``` 504 505## Repositories Involved 506 507- [graphic_graphic_2d](https://gitee.com/openharmony/graphic_graphic_2d) 508- [graphic_graphic_surface](https://gitee.com/openharmony/graphic_graphic_surface) 509- [multimedia_image_framework](https://gitee.com/openharmony/multimedia_image_framework) 510- [multimedia_media_foundation](https://gitee.com/openharmony/multimedia_media_foundation) 511- [third_party_skia](https://gitee.com/openharmony/third_party_skia) 512