video_encoder.c
来自「TI workshop 培训资料。 是关于如何创建DAVINCI平台下h.264」· C语言 代码 · 共 260 行
C
260 行
/* * video_encoder.c *//* Standard Linux headers */#include <stdio.h> // always include stdio.h#include <stdlib.h> // always include stdlib.h#include <string.h> // defines memset and memcpy methods/* Codec Engine headers */#include <xdc/std.h> // xdc base definitions. Must come 1st#include <ti/sdo/ce/Engine.h> // required for any CE application#include <ti/sdo/ce/video/videnc.h> // defines video encoder methods/* Application header files */#include "debug.h" // DBG and ERR macros#include "video_encoder.h" // video encoder functions, must be included // after codec engine headers!/* Macro for clearing structures */#define CLEAR(x) memset (&(x), 0, sizeof (x))/****************************************************************************** * video_encoder_setup ******************************************************************************//* input parameters: *//* Engine_Handle engineHandle -- handle to the open codec engine *//* containing the video encoder *//* char *encoderName -- name of the video encoder to create *//* VIDENC_Handle *encoderHandleByRef -- a video encoder handle passed by *//* reference. Used to return the handle *//* of the newly created video encoder *//* int frameWidth -- width of video frames to encode *//* int frameHeight -- height of video frames to encode *//* int frameRate -- frame rate of video being encoded *//* specified in ms, i.e. 30000 for 30fps *//* int bitRate -- bit rate target for the encoder *//* specified in bps, i.e. 4000000 for *//* 4Mbps *//* int *minOutputSizeByRef -- used to return the smallest encoded *//* buffer size the encoder will support *//* *//* *//* return value: *//* int -- VENC_SUCCESS or VENC_FAILURE as defined in video_encoder.h *//* *//******************************************************************************/int video_encoder_setup(Engine_Handle engineHandle, char *encoderName, VIDENC_Handle *encoderHandleByRef, int frameWidth, int frameHeight, int frameRate, int bitRate, int *minOutputSizeByRef){ VIDENC_Handle encoderHandle; // handle to the video encoder instance VIDENC_Params params; // static parameters for encoder VIDENC_Status encStatus; // provides buffer information VIDENC_DynamicParams dynamicParams; // required for VIDENC_control call XDAS_Int32 status; // return value of control call /* Fill in parameters for encoder */ params.size = sizeof(VIDENC_Params); params.encodingPreset = XDM_DEFAULT; params.rateControlPreset = 1; params.dataEndianness = XDM_BYTE; params.maxInterFrameInterval = 0; params.inputChromaFormat = XDM_YUV_422ILE; params.inputContentType = IVIDEO_PROGRESSIVE; params.maxHeight = frameHeight; params.maxWidth = frameWidth; params.maxFrameRate = frameRate; params.maxBitRate = bitRate; /* Create video encoder instance */ encoderHandle = VIDENC_create(engineHandle, encoderName, ¶ms); if (encoderHandle == NULL) { ERR("Can't open encode algorithm: %s\n", encoderName); return VENC_FAILURE; } DBG("Created video encoder instance of %s\n", encoderName); DBG("\tWith handle %p\n", encoderHandle); DBG("\tUsing default static and dynamic parameters\n"); /* set up dynamicParams and decStatus for call */ CLEAR(encStatus); encStatus.size = sizeof(VIDENC_Status); /* Initialize dynamic parameters */ CLEAR(dynamicParams); dynamicParams.size = sizeof(VIDENC_DynamicParams); dynamicParams.inputHeight = frameHeight; dynamicParams.inputWidth = frameWidth; dynamicParams.refFrameRate = frameRate; dynamicParams.targetFrameRate = frameRate; dynamicParams.targetBitRate = bitRate; dynamicParams.intraFrameInterval = 30; dynamicParams.generateHeader = XDM_ENCODE_AU; dynamicParams.captureWidth = 0; dynamicParams.forceIFrame = 0; status = VIDENC_control(encoderHandle, XDM_SETPARAMS, &dynamicParams, &encStatus); if (status != VIDENC_EOK) { ERR("XDM_SETPARAMS failed, extended error=%#x\n", (unsigned int) encStatus.extendedError); VIDENC_delete(encoderHandle); *encoderHandleByRef = NULL; return VENC_FAILURE; } /* Get buffer information from video decoder */ status = VIDENC_control(encoderHandle, XDM_GETBUFINFO, &dynamicParams, &encStatus); if (status != VIDENC_EOK) { ERR("XDM_GETBUFINFO failed, extended error=%#x\n", (unsigned int) encStatus.extendedError); VIDENC_delete(encoderHandle); *encoderHandleByRef = NULL; return VENC_FAILURE; } /* return the decoder instance handle and max output buffer size */ *encoderHandleByRef = encoderHandle; *minOutputSizeByRef = encStatus.bufInfo.minOutBufSize; /* indicate successful completion of function with return value */ return VENC_SUCCESS;}/****************************************************************************** * encode_video ******************************************************************************//* input parameters: *//* VIDENC_Handle encoderHandle -- handle to the video encoder as *//* returned by video_encoder_setup *//* char *inputBuffer -- pointer to buffer of input data *//* int inputSize -- size of inputBuffer in bytes *//* char *outputBuffer -- pointer to output buffer *//* int outputSizeByRef -- used as both an input and output *//* on input: size of outputBuffer *//* on output: size of encoded data *//* *//* *//* return value: *//* int -- VENC_SUCCESS or VENC_FAILURE as defined in video_encoder.h *//* *//******************************************************************************/int encode_video(VIDENC_Handle encoderHandle, char *inputBuffer, int inputSize, char *outputBuffer, int *outputSizeByRef){/* Declare arguments and return value for VIDDEC_process */ VIDENC_InArgs inArgs; // input args for VIDENC_process VIDENC_OutArgs outArgs; // output (return) from VIDENC_process XDM_BufDesc inBufDesc; // buffer descriptor for input buffer XDM_BufDesc outBufDesc; // buffer descriptor for output buffer XDAS_Int32 status; // success or failure for VIDENC_process /* Declare buffer descriptor elements */ XDAS_Int32 inBufSizeArray[1]; // pointed to by inBufDesc.bufSizes XDAS_Int32 outBufSizeArray[1]; // pointed to by outBufDesc.bufSizes/* Fill in input buffer descriptor */ inBufSizeArray[0] = inputSize; inBufDesc.numBufs = 1; // one input buffer inBufDesc.bufSizes = inBufSizeArray; // of this size inBufDesc.bufs = (XDAS_Int8 **) &inputBuffer; // located here/* Fill in output buffer descriptor */ outBufSizeArray[0] = *outputSizeByRef; outBufDesc.numBufs = 1; // one output buffer outBufDesc.bufSizes = outBufSizeArray; // of this size outBufDesc.bufs = (XDAS_Int8 **) &outputBuffer; // located here/* Fill in input arguments structure */ inArgs.size = sizeof(VIDENC_InArgs);/* Specify size of output arguments (return) structure */ outArgs.size = sizeof(VIDENC_OutArgs); /* Encode video buffer */ status = VIDENC_process(encoderHandle, &inBufDesc, &outBufDesc, &inArgs, &outArgs); if (status != VIDENC_EOK) { ERR("VIDENC_process() failed with a fatal error (%ld ext: %#lx\n", status, outArgs.extendedError); return VENC_FAILURE; } /* Return size of encoded buffer through the provided pointer */ *outputSizeByRef = outArgs.bytesGenerated; /* return VENC_SUCCESS if video encoder executes correctly */ return VENC_SUCCESS;}/****************************************************************************** * video_encoder_cleanup ******************************************************************************//* input parameters: *//* VIDENC_Handle encoderHandle -- handle to the video encoder as *//* returned by video_encoder_setup *//* *//* *//* return value: *//* int -- always returns VENC_SUCCESS as defined in video_encoder.h *//* *//******************************************************************************/int video_encoder_cleanup(VIDENC_Handle encoderHandle){ VIDENC_Status status; VIDENC_DynamicParams dynParams; /* Zero out the status and dynParams structures */ CLEAR(status); CLEAR(dynParams); /* Set size field of status and dynParams */ status.size = sizeof(VIDENC_Status); dynParams.size = sizeof(VIDENC_DynamicParams); /* Query encoder for status */ VIDENC_control(encoderHandle, XDM_GETSTATUS, &dynParams, &status); /* Delete encoder instance */ VIDENC_delete(encoderHandle); /* Report any errors */ if(status.extendedError){ ERR("Deleted video encoder instance with handle %#0lx\n", (unsigned long) encoderHandle); ERR("\tEncoder exited with error mask %#0x\n", (unsigned int) status.extendedError ); } else{ DBG("Deleted video encoder instance with handle %#0lx\n", (unsigned long) encoderHandle); DBG("\tEncoder exited with no errors\n"); } /* Return success */ return VENC_SUCCESS;}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?