了解MPP的基本功能后,接下来具体分析编码的代码。首先把编码的代码提取出来,方便以后的使用。
完整的编码代码如下,相比较给出的示例代码,做了一些改动,输入的指令全部去除,将函数入口改为利用OpenCV打开USB摄像头,调用编码函数编码,编码后的数据保存在本地。


1 #if defined(_WIN32) 2 #include "vld.h" 3 #endif 4 5 #define MODULE_TAG "mpi_enc_test" 6 7 #include <string.h> 8 #include <sys/time.h> 9 10 #include "utils.h" 11 #include "rk_mpi.h" 12 #include "mpp_env.h" 13 #include "mpp_mem.h" 14 #include "mpp_log.h" 15 #include "mpp_time.h" 16 #include "mpp_common.h" 17 18 #include <opencv2/core/core.hpp> 19 #include <opencv2/highgui/highgui.hpp> 20 #include <opencv2/opencv.hpp> 21 22 using namespace std; 23 using namespace cv; 24 25 #define MAX_FILE_NAME_LENGTH 256 26 #define calTimeCost(begin,end)(end.tv_sec*1000.-begin.tv_sec*1000.+end.tv_usec/1000.-begin.tv_usec/1000.) 27 28 typedef struct 29 { 30 MppCodingType type; 31 RK_U32 width; 32 RK_U32 height; 33 MppFrameFormat format; 34 35 RK_U32 num_frames; 36 } MpiEncTestCmd; 37 38 typedef struct 39 { 40 //global flow control flag 41 RK_U32 frm_eos; 42 RK_U32 pkt_eos; 43 RK_U32 frame_count; 44 RK_U64 stream_size; 45 46 //input ang output file 47 FILE *fp_input; 48 FILE *fp_output; 49 50 //input and output 51 MppBuffer frm_buf; 52 MppEncSeiMode sei_mode; 53 54 //base flow context 55 MppCtx ctx; 56 MppApi *mpi; 57 MppEncPrepCfg prep_cfg; 58 MppEncRcCfg rc_cfg; 59 MppEncCodecCfg codec_cfg; 60 61 //paramter for resource malloc 62 RK_U32 width; 63 RK_U32 height; 64 RK_U32 hor_stride; //horizontal stride 65 RK_U32 ver_stride; //vertical stride 66 MppFrameFormat fmt; 67 MppCodingType type; 68 RK_U32 num_frames; 69 70 //resources 71 size_t frame_size; 72 //NOTE: packet buffer may overflow 73 size_t packet_size; 74 75 //rate control runtime parameter 76 RK_S32 gop; 77 RK_S32 fps; 78 RK_S32 bps; 79 } MpiEncTestData; 80 81 MpiEncTestData data; 82 MpiEncTestCmd cmd_ctx; 83 MpiEncTestData *ptr1; 84 85 MPP_RET ret = MPP_OK; 86 87 FILE *fp_out = fopen("result.h264", "w+b"); 88 89 MPP_RET test_ctx_init(MpiEncTestData **data, MpiEncTestCmd *cmd) 90 { 91 MpiEncTestData *p = NULL; 92 MPP_RET ret = MPP_OK; 93 94 if (!data || !cmd) 95 { 96 mpp_err_f("invalid input data %p cmd %p\n", data, cmd); 97 return MPP_ERR_NULL_PTR; 98 } 99 100 p = mpp_calloc(MpiEncTestData, 1); 101 if (!p) 102 { 103 mpp_err_f("create MpiEncTestData failed\n");