Sample Code

Principles

Sample Code

You can view the complete code in Sample Overview.

Following the API calls, add exception handling branches and specify log printing of error and information levels. The following is a code sample of key steps for your reference only.

//1. Initialize AscendCL.
aclRet = aclInit(nullptr);

//2. Allocate runtime resources including the device, context, and stream in sequence.
aclrtContext context_;
aclrtStream stream_;
aclrtSetDevice(0);
aclrtCreateContext(&context_, 0);
aclrtCreateStream(&stream_);

//3. Create a callback function.
void callback(acldvppStreamDesc *input, acldvppPicDesc *output, void *userdata)
{
    static int count = 1;
    if (output != nullptr) {
        //Obtain the VDEC output buffer, call the user-defined function WriteToFile to write output buffer data to a file, and then call acldvppFree to free the output buffer.
        void *vdecOutBufferDev = acldvppGetPicDescData(output);
        if (vdecOutBufferDev != nullptr) {
            // 0: vdec success; others, vdec failed
            //If retCode is 0, the decoding is successful. If retCode is 1, the decoding fails.
            int retCode = acldvppGetPicDescRetCode(output);
            if (retCode == 0) {
                // process task: write file
                uint32_t size = acldvppGetPicDescSize(output);
                std::string fileNameSave = "outdir/image" + std::to_string(count);
                //The VDEC output result is stored on the device. Perform the following operations in the WriteToFile method:
                if (!Utils::WriteToFile(fileNameSave.c_str(), vdecOutBufferDev, size)) {
                    ERROR_LOG("write file failed.");
                }
            } else {
                ERROR_LOG("vdec decode frame failed.");
            }

            // free output vdecOutBufferDev
            aclError ret = acldvppFree(vdecOutBufferDev);
        }
        //Destroy data of type acldvppPicDesc, which indicates the description data of the decoded output image.
        aclError ret = acldvppDestroyPicDesc(output);
    }

    // free input vdecInBufferDev and destroy stream desc
    if (input != nullptr) {
        void *vdecInBufferDev = acldvppGetStreamDescData(input);
        if (vdecInBufferDev != nullptr) {
            aclError ret = acldvppFree(vdecInBufferDev);
        }
         //Destroy data of type acldvppStreamDesc, which indicates the description data of the decoded input stream.
        aclError ret = acldvppDestroyStreamDesc(input);
    }

    INFO_LOG("success to callback %d.", count);
    count++;
}

//4. Create the description of the video stream processing channel, and set the attributes of the video processing channel description. The thread and callback function need to be created in advance.
//vdecChannelDesc_ is of type aclvdecChannelDesc.
vdecChannelDesc_ = aclvdecCreateChannelDesc();
ret = aclvdecSetChannelDescChannelId(vdecChannelDesc_, 10);
ret = aclvdecSetChannelDescThreadId(vdecChannelDesc_, threadId_);
ret = aclvdecSetChannelDescCallback(vdecChannelDesc_, callback);
//In the sample, the H265_MAIN_LEVEL video coding protocol is used.
ret = aclvdecSetChannelDescEnType(vdecChannelDesc_, static_cast<acldvppStreamFormat>(enType_));
//In the sample, PIXEL_FORMAT_YVU_SEMIPLANAR_420 is used.
ret = aclvdecSetChannelDescOutPicFormat(vdecChannelDesc_, static_cast<acldvppPixelFormat>(format_));

//5. Create a channel for processing the video stream.
ret = aclvdecCreateChannel(vdecChannelDesc_);

//6. Allocate input buffer (run mode specific).
//Call aclrtGetRunMode to obtain the run mode of the software stack. If ACL_DEVICE is returned, allocate and use the device buffer. If ACL_HOST is returned, transfer the input image data to the device by using the aclrtMemcpy call. After the data transfer is complete, the buffer needs to be freed in a timely manner.
aclrtRunMode runMode;
ret = aclrtGetRunMode(&runMode);
if (runMode == ACL_HOST) {
    //Allocate host buffer inputHostBuff.
    void* inputHostBuff= nullptr;
    //inBufferSize_ is the size of the input stream.
    inputHostBuff= malloc(inBufferSize_);
    //Load the input image into the buffer. The ReadPicFile function is defined by the user.
    ReadPicFile(picName, inputHostBuff, inBufferSize_);
    //Allocate the device buffer inBufferDev_.
    aclRet = acldvppMalloc(&inBufferDev_, inBufferSize_);
    //Transfer the input image data to the device by using the aclrtMemcpy call.
    aclRet = aclrtMemcpy(inBufferDev_, inBufferSize_, inputHostBuff, inBufferSize_, ACL_MEMCPY_HOST_TO_DEVICE);
    //Free the buffer in a timely manner after data transfer is complete.
    free(inputHostBuff);
} else {
    //Allocate device input buffer dataDev. The StreamBufferSize is the size of the input stream.
    ret = acldvppMalloc(&inBufferDev_, inBufferSize_);
    //Load the input image into the buffer. The ReadPicFile function is defined by the user.
    ReadPicFile(picName, inBufferDev_, inBufferSize_);
}

//7. Perform video decoding for 10 times cyclically and output 10 images in YUV420SP (NV12) format.
int rest_len = 10;
int32_t count = 0;
while (rest_len > 0) {
    //7.1 Create the description of the input video stream and set the stream attributes.
    streamInputDesc_ = acldvppCreateStreamDesc(); 
    //inBufferDev_ indicates the buffer for storing the input video data on the device, and inBufferSize_ indicates the buffer size.
    ret = acldvppSetStreamDescData(streamInputDesc_, inBufferDev_);
    ret = acldvppSetStreamDescSize(streamInputDesc_, inBufferSize_);

    //7.2 Allocate device buffer picOutBufferDev_ for storing the VDEC-decoded output data.
    ret = acldvppMalloc(&picOutBufferDev_, size);

    //7.3 Create the description of the output image and set the attributes of the image description.
    //picOutputDesc_ is of type acldvppPicDesc.
    picOutputDesc_ = acldvppCreatePicDesc();
    ret = acldvppSetPicDescData(picOutputDesc_, picOutBufferDev_);
    ret = acldvppSetPicDescSize(picOutputDesc_, size);
    ret = acldvppSetPicDescFormat(picOutputDesc_, static_cast<acldvppPixelFormat>(format_));

    //7.4 Decode the video stream. The callback function is called after each decoded frame to write data to the file, and destroy allocations in a timely manner.
    ret = aclvdecSendFrame(vdecChannelDesc_, streamInputDesc_, picOutputDesc_, nullptr, nullptr);
    // ......
    ++count;
    rest_len = rest_len - 1;
    // ......
}

//8. Destroy the image processing channel and image description information.
ret = aclvdecDestroyChannel(vdecChannelDesc_);
aclvdecDestroyChannelDesc(vdecChannelDesc_);

//9. Release runtime resources including the stream, context, and device in sequence.
aclrtDestroyStream(stream_);
aclrtDestroyContext(context_);
aclrtResetDevice(0);

//10. Deinitialize AscendCL.
aclRet = aclFinalize();

//...

Return Codes

Table 1 Return codes

Return Code

Description

Solution

AICPU_DVPP_KERNEL_STATE_SUCCESS = 0

Decoding success

-

AICPU_DVPP_KERNEL_STATE_FAILED = 1

Other error

For details about logs, see Log Reference.

AICPU_DVPP_KERNEL_STATE_DVPP_ERROR = 2

Failed to call APIs of other modules

For details about logs, see Log Reference.

AICPU_DVPP_KERNEL_STATE_PARAM_INVALID = 3

Argument verification failure

Check that the API arguments meet the API requirements.

AICPU_DVPP_KERNEL_STATE_OUTPUT_SIZE_INVALID = 4

Output buffer size verification failure

Check that the output buffer size meets the API requirements.

AICPU_DVPP_KERNEL_STATE_INTERNAL_ERROR = 5

System internal error

For details about logs, see Log Reference.

AICPU_DVPP_KERNEL_STATE_QUEUE_FULL = 6

Full internal queue

For details about logs, see Log Reference.

AICPU_DVPP_KERNEL_STATE_QUEUE_EMPTY = 7

Empty internal queue

For details about logs, see Log Reference.

AICPU_DVPP_KERNEL_STATE_QUEUE_NOT_EXIST = 8

Nonexistent internal queue

For details about logs, see Log Reference.

AICPU_DVPP_KERNEL_STATE_GET_CONTEX_FAILED = 9

Internal context obtaining failure

For details about logs, see Log Reference.

AICPU_DVPP_KERNEL_STATE_SUBMIT_EVENT_FAILED = 10

Internal event submission failure

For details about logs, see Log Reference.

AICPU_DVPP_KERNEL_STATE_MEMORY_FAILED = 11

Internal memory allocation failure

Check that the system has available memory.

AICPU_DVPP_KERNEL_STATE_SEND_NOTIFY_FAILED = 12

Internal notification push failure

For details about logs, see Log Reference.

AICPU_DVPP_KERNEL_STATE_VPC_OPERATE_FAILED = 13

Internal API operation failure

For details about logs, see Log Reference.

AICPU_DVPP_KERNEL_STATE_CHANNEL_ABNORMAL = 14

Abnormal channel

For details about logs, see Log Reference.

ERR_INVALID_STATE = 0x10001

Abnormal VDEC decoder state

For details about logs, see Log Reference.

ERR_HARDWARE = 0x10002

Hardware error, including decoder starts, execution, and stops

For details about logs, see Log Reference.

ERR_SCD_CUT_FAIL = 0x10003

An error occurs when the video stream is divided into multi-frame images.

Check that the input video stream is valid.

ERR_VDM_DECODE_FAIL = 0x10004

Single-frame decoding failure

Check that the input video stream is valid.

ERR_ALLOC_MEM_FAIL = 0x10005

Internal memory allocation failure

Check whether the system has available memory. If this error is ignored, no decoding result may be output even with stream input.

ERR_ALLOC_DYNAMIC_MEM_FAIL = 0x10006

Out-of-range input video resolution and dynamic memory allocation failure

Check the resolution of the input video stream and whether the system has available memory. If this error is ignored, no decoding result may be output with the continuously sent streams.

ERR_ALLOC_IN_OR_OUT_PORT_MEM_FAIL = 0x10007

Internal VDEC input and output buffer allocation failure

Check whether the system has available memory. If this error is ignored, no decoding result may be output with the continuously sent streams.

ERR_BITSTREAM = 0x10008

Invalid stream (for example, syntax error, eos retried, or eos detected in the first frame)

Check that the input video stream is valid.

ERR_VIDEO_FORMAT = 0x10009

Incorrect input video format

Check that the input video format is H.264 or H.265.

ERR_IMAGE_FORMAT = 0x1000a

Incorrect output format

Check that the output image format is NV12 or NV21.

ERR_CALLBACK = 0x1000b

Empty callback function

Check whether the callback function is empty.

ERR_INPUT_BUFFER = 0x1000c

Empty input buffer

Check whether the input buffer is empty.

ERR_INBUF_SIZE = 0x1000d

Input buffer size ≤ 0

Check whether the buffer size is less than or equal to 0.

ERR_THREAD_CREATE_FBD_FAIL = 0x1000e

Abnormal thread (thread where the callback function returns the decoding result)

Check that the resources (such as threads and memory) in the system are available.

ERR_CREATE_INSTANCE_FAIL = 0x1000f

Decoding instance creation failure

For details about logs, see Log Reference.

ERR_INIT_DECODER_FAIL = 0x10010

Decoder initialization failure. For example, the number of decoding instances exceeds the maximum of 16.

For details about logs, see Log Reference.

ERR_GET_CHANNEL_HANDLE_FAIL = 0x10011

Channel handle obtaining failure

For details about logs, see Log Reference.

ERR_COMPONENT_SET_FAIL = 0x10012

Decoding instance setting failure

Check that the input arguments are correct, for example, the input video format video_format and output frame format image_format.

ERR_COMPARE_NAME_FAIL = 0x10013

Decoding instance naming failure

Check that the input arguments are correct, for example, the input video format video_format and output frame format image_format.

ERR_OTHER = 0x10014

Other error

Contact technical support.

ERR_DECODE_NOPIC = 0x20000

Can be ignored. In the interlaced scenario where two fields are transmitted every frame, one of every two transmitted fields has no decoding output. This is expected. The decoding output of an interlaced stream is stored in the output buffers of the odd fields.

-

0x20001

Reference frame count setting failure

Check that the actual number of reference frames of the stream is the same as the configured number of reference frames. The number of reference frames defaults to 8 for the Ascend 310P AI Processor.

0x20002

Size of VDEC frame buffer setting failure

Check that the actual width and height of the input stream are the same as the configured width and height.

0xA0058001

Invalid device ID

(Reserved)

-

0xA0058002

Invalid channel ID

Check that the input channel ID is correct or whether the total number of channels reaches the upper limit.

0xA0058003

Invalid argument, such as an invalid enum value

Check the incorrect argument based on the logs.

For details about logs, see Log Reference.

0xA0058004

Existing resource

Check whether the channel has been repeatedly created.

0xA0058005

Non-existent channel resource

Check whether a non-existent channel ID or channel handle is used.

0xA0058006

Null pointer

Check the input of the API based on the logs.

For details about logs, see Log Reference.

0xA0058007

No configuration made to corresponding parameters before the system, device, or channel is enabled.

Check the input of the API based on the logs.

For details about logs, see Log Reference.

0xA0058008

Unsupported argument or function.

Check the input of the API based on the logs.

For details about logs, see Log Reference.

0xA0058009

Unallowed operation, for example, an attempt to update a static attribute.

For details about logs, see Log Reference.

0xA005800C

Memory allocation failure due to memory insufficiency or other reasons.

Check whether the system has available memory. If this error is ignored, no decoding result may be output with the continuously sent streams.

0xA005800D

Buffer allocation failure due to too large requested picture buffer or other reasons

(Reserved)

-

0xA005800E

Empty buffer

Decoding is not complete, so there is no decoding result in the buffer. Try again after the decoding result is loaded to the buffer.

0xA005800F

Full buffer

Stream feeding is too fast, which causes full input buffer. Slow down stream feeding or increase the buffer allocation when creating a channel.

0xA0058010

Uninitialized system or unloaded dependent modules

(Reserved)

-

Check that the system initialization API is called.

0xA0058011

Bad address

(Reserved)

-

For details about logs, see Log Reference.

0xA0058012

Busy system

Check whether the total number of VDEC channels reaches the upper limit.

For details about logs, see Log Reference.

0xA0058013

Not enough buffer

(Reserved)

-

0xA0058014

Hardware or software processing timeout

(Reserved)

-

0xA0058015

System error

For details about logs, see Log Reference.

0xA005803F

Maximum return. The error code value of the module must be less than this value.

-