With the C APIs provided by the VideoProcessing module, you can enhance the image detail within video streams and scale them. The processed data is suitable for use in display, playback, and recording.
Common usage scenarios are as follows: After video decoding, the video undergoes detail enhancement before being displayed by the XComponent.
To ensure real-time response, you are advised to create only one instance at a time.
Only a video that meets the following conditions can be processed:
Four levels of algorithms are provided, each offering better processing outcomes at the expense of increased performance degradation.
| Algorithm Level | Input Resolution (Unit: Pixel) | Output Resolution (Unit: Pixel) | Description |
|---|---|---|---|
| NONE | Width: (32, 2000] Height: (32, 2000] | Width: (32, 2000] Height: (32, 2000] | Applies only to scaling scenarios. No detail enhancement is provided. |
| LOW | Width: (32, 2000] Height: (32, 2000] | Width: (32, 2000] Height: (32, 2000] | Applies only to scaling scenarios. No detail enhancement is provided during proportional scaling. During scaling, a basic level of detail enhancement is provided at a high processing speed. This level is the default setting. |
| MEDIUM | Width: (32, 2000] Height: (32, 2000] | Width: (32, 2000] Height: (32, 2000] | Applies only to scaling scenarios. No detail enhancement is provided during proportional scaling. During scaling, a moderate level of detail enhancement is provided at a balanced processing speed. |
| HIGH | Width: [512, 2000] Height: [512, 2000] | Width: [512, 2000] Height: [512, 2000] | Applies to scaling and detail enhancement scenarios. Detail enhancement is provided during proportional scaling. During scaling, a high level of detail enhancement is provided at a relatively low processing speed. |
Do not directly call video processing APIs or perform other time-consuming operations in the video processing callback function. Instead, call them in the thread of the application.
target_link_libraries(sample PUBLIC libvideo_processing.so)
Add the header files.
#include <ace/xcomponent/native_interface_xcomponent.h> #include <multimedia/player_framework/native_avformat.h> #include <multimedia/video_processing_engine/video_processing.h> #include <multimedia/video_processing_engine/video_processing_types.h> #include <native_window/external_window.h> #include <native_buffer/native_buffer.h>
(Optional) Create a decoding instance.
The input of the detail enhancement module can be a video stream decoded by the system or video data filled into the window by the application (for example, the application directly fills data into the window after internal software decoding). If the system decoder is selected to process the video file or video streaming media, you can create a decoding instance as an input of the detail enhancement module.
// Create a demuxer to parse audio and video information.
OH_AVSource* source_ = OH_AVSource_CreateWithFD(inputFd, inputFileOffset, inputFileSize);
OH_AVDemuxer* demuxer_ = OH_AVDemuxer_CreateWithSource(source_);
auto sourceFormat = std::shared_ptr<OH_AVFormat>(OH_AVSource_GetSourceFormat(source_), OH_AVFormat_Destroy);
// Create a video decoder.
OH_AVCodec * decoder_ = OH_VideoDecoder_CreateByMime(videoCodecMime.c_str());
// Configure video information.
OH_AVFormat *format = OH_AVFormat_Create();
OH_AVFormat_SetIntValue(format, OH_MD_KEY_WIDTH, videoWidth);
OH_AVFormat_SetIntValue(format, OH_MD_KEY_HEIGHT, videoHeight);
OH_AVFormat_SetDoubleValue(format, OH_MD_KEY_FRAME_RATE, frameRate);
OH_AVFormat_SetIntValue(format, OH_MD_KEY_PIXEL_FORMAT, pixelFormat);
OH_AVFormat_SetIntValue(format, OH_MD_KEY_ROTATION, rotation);
int ret = OH_VideoDecoder_Configure(decoder_, format);
OH_AVFormat_Destroy(format);
// Configure the callback function to maintain the buffer queue of the video decoder.
OH_VideoDecoder_RegisterCallback(decoder_,
{SampleCallback::OnCodecError, SampleCallback::OnCodecFormatChange,
SampleCallback::OnNeedInputBuffer, SampleCallback::OnNewOutputBuffer}, videoDecContext_);
// Prepare the video decoder.
int ret = OH_VideoDecoder_Prepare(decoder_);
// Create a decoding context.
videoDecContext_ = new CodecUserData; (Optional) Initialize the environment.
This is usually executed on the initial use within a process to preemptively handle certain time-consuming operations.
VideoProcessing_ErrorCode ret = OH_VideoProcessing_InitializeEnvironment();
Create a detail enhancement module.
The application may create a detail enhancement module by using a video processing engine type. In the code snippet below, the following variables are used:
// Create a detail enhancement module instance by specifying the video processing engine type. VideoProcessing_ErrorCode ret = OH_VideoProcessing_Create(&videoProcessor, VIDEO_PROCESSING_TYPE_DETAIL_ENHANCER);
Configure the asynchronous callback function.
// Create a callback instance. ret = OH_VideoProcessingCallback_Create(&callback); // Declare the callback function (userData is the user data passed during callback registration, for example, this pointer.) void OnError(OH_VideoProcessing* videoProcessor, VideoProcessing_ErrorCode error, void* userData); void OnState(OH_VideoProcessing* videoProcessor, VideoProcessing_State state, void* userData); void OnNewOutputBuffer(OH_VideoProcessing* videoProcessor, uint32_t index, void* userData); // Bind the callback function. OH_VideoProcessingCallback_BindOnError(callback, OnError); OH_VideoProcessingCallback_BindOnState(callback, OnState); OH_VideoProcessingCallback_BindOnNewOutputBuffer(callback, OnNewOutputBuffer); // Register the callback function. ret = OH_VideoProcessing_RegisterCallback(videoProcessor, callback, this);
(Optional) Configure the detail enhancement level, which can be HIGH, MEDIUM, LOW, or NONE. If it is not configured, the LOW level is used by default.
// Create a format instance. OH_AVFormat* parameter = OH_AVFormat_Create(); // Specify the level. OH_AVFormat_SetIntValue(parameter, VIDEO_DETAIL_ENHANCER_PARAMETER_KEY_QUALITY_LEVEL, VIDEO_DETAIL_ENHANCER_QUALITY_LEVEL_HIGH); // Configure parameters. OH_VideoProcessing_SetParameter(videoProcessor, parameter);
Obtain a surface.
// Configure the input for the algorithm. ret = OH_VideoProcessing_GetSurface(videoProcessor, inputWindow); // Bind the output of the decoder to the input of the algorithm, with the decoder's output window resolution being the algorithm's input resolution. OH_VideoDecoder_SetSurface(decoder_, inputWindow_);
Set the surface (for display).
// Configure the output of the algorithm, with the configured output window resolution being the algorithm's output resolution. ret = OH_VideoProcessing_SetSurface(videoProcessor, outWindow);
Create the input and output threads of the decoder.
std::unique_ptr<std::thread> videoDecInputThread_ = std::make_unique<std::thread>(&Player::VideoDecInputThread, this); std::unique_ptr<std::thread> videoDecOutputThread_ = std::make_unique<std::thread>(&Player::VideoDecOutputThread, this);
Start detail enhancement.
// Start decoding. int ret = OH_VideoDecoder_Start(decoder_); // Start detail enhancement. ret = OH_VideoProcessing_Start(videoProcessor);
Call OH_VideoProcessing_Stop() to disable detail enhancement.
VideoProcessing_ErrorCode ret = OH_VideoProcessing_Stop(videoProcessor);
Release the video processing instance.
VideoProcessing_ErrorCode ret = OH_VideoProcessing_Destroy(videoProcessor); videoProcessor = nullptr; VideoProcessing_ErrorCode ret = OH_VideoProcessingCallback_Destroy(callback); callback = nullptr;
Release the video processing resources.
VideoProcessing_ErrorCode ret = OH_VideoProcessing_DeinitializeEnvironment();