• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2015 Rockchip Electronics Co. LTD
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef __MPI_ENC_UTILS_H__
18 #define __MPI_ENC_UTILS_H__
19 
20 #include <stdio.h>
21 
22 #include "rk_venc_cmd.h"
23 #include "iniparser.h"
24 #include "utils_mpp.h"
25 #include "mpp_enc_roi_utils.h"
26 #include "camera_source.h"
27 
28 typedef struct MpiEncTestArgs_t {
29     char                *file_input;
30     char                *file_output;
31     char                *file_cfg;
32     dictionary          *cfg_ini;
33 
34     MppCodingType       type;
35     MppCodingType       type_src;       /* for file source input */
36     MppFrameFormat      format;
37     RK_S32              frame_num;
38     RK_S32              loop_cnt;
39     RK_S32              nthreads;
40 
41     RK_S32              width;
42     RK_S32              height;
43     RK_S32              hor_stride;
44     RK_S32              ver_stride;
45 
46     /* -rc */
47     RK_S32              rc_mode;
48 
49     /* -bps */
50     RK_S32              bps_target;
51     RK_S32              bps_max;
52     RK_S32              bps_min;
53 
54     /* -fps */
55     RK_S32              fps_in_flex;
56     RK_S32              fps_in_num;
57     RK_S32              fps_in_den;
58     RK_S32              fps_out_flex;
59     RK_S32              fps_out_num;
60     RK_S32              fps_out_den;
61 
62     /* -qc */
63     RK_S32              qp_init;
64     RK_S32              qp_min;
65     RK_S32              qp_max;
66     RK_S32              qp_min_i;
67     RK_S32              qp_max_i;
68 
69     /* -g gop mode */
70     RK_S32              gop_mode;
71     RK_S32              gop_len;
72     RK_S32              vi_len;
73 
74     /* -v q runtime log disable flag */
75     RK_U32              quiet;
76     /* -v f runtime fps log flag */
77     RK_U32              trace_fps;
78     FpsCalc             fps;
79     RK_U32              psnr_en;
80     RK_U32              ssim_en;
81     char                *file_slt;
82     unsigned char       *buf;
83     size_t              *buf_size;
84 } MpiEncTestArgs;
85 
86 #ifdef __cplusplus
87 extern "C" {
88 #endif
89 
90 RK_S32 mpi_enc_width_default_stride(RK_S32 width, MppFrameFormat fmt);
91 
92 /*
93  * gop_mode
94  * 0     - default IPPPP gop
95  * 1 ~ 3 - tsvc2 ~ tsvc4
96  * >=  4 - smart gop mode
97  */
98 MPP_RET mpi_enc_gen_ref_cfg(MppEncRefCfg ref, RK_S32 gop_mode);
99 MPP_RET mpi_enc_gen_smart_gop_ref_cfg(MppEncRefCfg ref, RK_S32 gop_len, RK_S32 vi_len);
100 MPP_RET mpi_enc_gen_osd_data(MppEncOSDData *osd_data, MppBufferGroup group,
101                              RK_U32 width, RK_U32 height, RK_U32 frame_cnt);
102 MPP_RET mpi_enc_gen_osd_plt(MppEncOSDPlt *osd_plt, RK_U32 frame_cnt);
103 
104 MpiEncTestArgs *mpi_enc_test_cmd_get(void);
105 MPP_RET mpi_enc_test_cmd_update_by_args(MpiEncTestArgs* cmd, int argc, char **argv);
106 MPP_RET mpi_enc_test_cmd_put(MpiEncTestArgs* cmd);
107 
108 MPP_RET mpi_enc_test_cmd_show_opt(MpiEncTestArgs* cmd);
109 
110 //int RKYuvToH264(unsigned char *buf, size_t *buf_size, int width,
111 //    int height, MppFrameFormat format, MppCodingType type);
112 int hal_mpp_encode(void *ctxs, int dma_fd, unsigned char *buf, size_t *buf_size);
113 void *hal_mpp_ctx_create(MpiEncTestArgs *args);
114 void hal_mpp_ctx_delete(void *ctx);
115 
116 typedef struct {
117     // base flow context
118     MppCtx ctx;
119     MppApi *mpi;
120     RK_S32 chn;
121 
122     // global flow control flag
123     RK_U32 frm_eos;
124     RK_U32 pkt_eos;
125     RK_U32 frm_pkt_cnt;
126     RK_S32 frame_num;
127     RK_S32 frame_count;
128     RK_U64 stream_size;
129     /* end of encoding flag when set quit the loop */
130     volatile RK_U32 loop_end;
131 
132     // src and dst
133     FILE *fp_input;
134     FILE *fp_output;
135     FILE *fp_verify;
136 
137     /* encoder config set */
138     MppEncCfg       cfg;
139     MppEncPrepCfg   prep_cfg;
140     MppEncRcCfg     rc_cfg;
141     MppEncCodecCfg  codec_cfg;
142     MppEncSliceSplit split_cfg;
143     MppEncOSDPltCfg osd_plt_cfg;
144     MppEncOSDPlt    osd_plt;
145     MppEncOSDData   osd_data;
146     RoiRegionCfg    roi_region;
147     MppEncROICfg    roi_cfg;
148 
149     // input / output
150     MppBufferGroup buf_grp;
151     MppBuffer frm_buf;
152     MppBuffer pkt_buf;
153     MppBuffer md_info;
154     MppEncSeiMode sei_mode;
155     MppEncHeaderMode header_mode;
156 
157     // paramter for resource malloc
158     RK_U32 width;
159     RK_U32 height;
160     RK_U32 hor_stride;
161     RK_U32 ver_stride;
162     MppFrameFormat fmt;
163     MppCodingType type;
164     RK_S32 loop_times;
165     CamSource *cam_ctx;
166     MppEncRoiCtx roi_ctx;
167 
168     // resources
169     size_t header_size;
170     size_t frame_size;
171     size_t mdinfo_size;
172     /* NOTE: packet buffer may overflow */
173     size_t packet_size;
174 
175     RK_U32 osd_enable;
176     RK_U32 osd_mode;
177     RK_U32 split_mode;
178     RK_U32 split_arg;
179     RK_U32 split_out;
180 
181     RK_U32 user_data_enable;
182     RK_U32 roi_enable;
183 
184     // rate control runtime parameter
185     RK_S32 fps_in_flex;
186     RK_S32 fps_in_den;
187     RK_S32 fps_in_num;
188     RK_S32 fps_out_flex;
189     RK_S32 fps_out_den;
190     RK_S32 fps_out_num;
191     RK_S32 bps;
192     RK_S32 bps_max;
193     RK_S32 bps_min;
194     RK_S32 rc_mode;
195     RK_S32 gop_mode;
196     RK_S32 gop_len;
197     RK_S32 vi_len;
198 
199     RK_S64 first_frm;
200     RK_S64 first_pkt;
201 } MpiEncTestData;
202 
203 /* For each instance thread return value */
204 typedef struct {
205     float           frame_rate;
206     RK_U64          bit_rate;
207     RK_S64          elapsed_time;
208     RK_S32          frame_count;
209     RK_S64          stream_size;
210     RK_S64          delay;
211 } MpiEncMultiCtxRet;
212 
213 typedef struct {
214     MpiEncTestArgs      *cmd;       // pointer to global command line info
215     const char          *name;
216     RK_S32              chn;
217 
218     pthread_t           thd;        // thread for for each instance
219     MpiEncTestData      ctx;        // context of encoder
220     MpiEncMultiCtxRet   ret;        // return of encoder
221 } MpiEncMultiCtxInfo;
222 
223 #ifdef __cplusplus
224 }
225 #endif
226 
227 #endif /*__MPI_ENC_UTILS_H__*/
228