xref: /rockchip-linux_mpp/utils/mpi_dec_utils.h (revision 437bfbeb9567cca9cd9080e3f6954aa9d6a94f18)
1 /*
2  * Copyright 2020 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_DEC_UTILS_H__
18 #define __MPI_DEC_UTILS_H__
19 
20 #include <stdio.h>
21 #include "utils.h"
22 
23 #define MAX_FILE_NAME_LENGTH        256
24 #define MPI_DEC_STREAM_SIZE         (SZ_4K)
25 #define MPI_DEC_LOOP_COUNT          4
26 
27 /*
28  * NOTE: We can choose decoder's buffer mode here.
29  * There are three mode that decoder can support:
30  *
31  * Mode 1: Pure internal mode
32  * In the mode user will NOT call MPP_DEC_SET_EXT_BUF_GROUP
33  * control to decoder. Only call MPP_DEC_SET_INFO_CHANGE_READY
34  * to let decoder go on. Then decoder will use create buffer
35  * internally and user need to release each frame they get.
36  *
37  * Advantage:
38  * Easy to use and get a demo quickly
39  * Disadvantage:
40  * 1. The buffer from decoder may not be return before
41  * decoder is close. So memroy leak or crash may happen.
42  * 2. The decoder memory usage can not be control. Decoder
43  * is on a free-to-run status and consume all memory it can
44  * get.
45  * 3. Difficult to implement zero-copy display path.
46  *
47  * Mode 2: Half internal mode
48  * This is the mode current test code using. User need to
49  * create MppBufferGroup according to the returned info
50  * change MppFrame. User can use mpp_buffer_group_limit_config
51  * function to limit decoder memory usage.
52  *
53  * Advantage:
54  * 1. Easy to use
55  * 2. User can release MppBufferGroup after decoder is closed.
56  *    So memory can stay longer safely.
57  * 3. Can limit the memory usage by mpp_buffer_group_limit_config
58  * Disadvantage:
59  * 1. The buffer limitation is still not accurate. Memory usage
60  * is 100% fixed.
61  * 2. Also difficult to implement zero-copy display path.
62  *
63  * Mode 3: Pure external mode
64  * In this mode use need to create empty MppBufferGroup and
65  * import memory from external allocator by file handle.
66  * On Android surfaceflinger will create buffer. Then
67  * mediaserver get the file handle from surfaceflinger and
68  * commit to decoder's MppBufferGroup.
69  *
70  * Advantage:
71  * 1. Most efficient way for zero-copy display
72  * Disadvantage:
73  * 1. Difficult to learn and use.
74  * 2. Player work flow may limit this usage.
75  * 3. May need a external parser to get the correct buffer
76  * size for the external allocator.
77  *
78  * The required buffer size caculation:
79  * hor_stride * ver_stride * 3 / 2 for pixel data
80  * hor_stride * ver_stride / 2 for extra info
81  * Total hor_stride * ver_stride * 2 will be enough.
82  *
83  * For H.264/H.265 20+ buffers will be enough.
84  * For other codec 10 buffers will be enough.
85  */
86 typedef enum MppDecBufMode_e {
87     MPP_DEC_BUF_HALF_INT,
88     MPP_DEC_BUF_INTERNAL,
89     MPP_DEC_BUF_EXTERNAL,
90     MPP_DEC_BUF_MODE_BUTT,
91 } MppDecBufMode;
92 
93 typedef void* FileReader;
94 typedef void* DecBufMgr;
95 
96 typedef struct FileBufSlot_t {
97     RK_S32          index;
98     MppBuffer       buf;
99     size_t          size;
100     RK_U32          eos;
101     char            *data;
102 } FileBufSlot;
103 
104 /* For overall configure setup */
105 typedef struct MpiDecTestCmd_t {
106     char            file_input[MAX_FILE_NAME_LENGTH];
107     char            file_output[MAX_FILE_NAME_LENGTH];
108 
109     MppCodingType   type;
110     MppFrameFormat  format;
111     RK_U32          width;
112     RK_U32          height;
113 
114     RK_U32          have_input;
115     RK_U32          have_output;
116 
117     RK_U32          simple;
118     RK_S32          timeout;
119     RK_S32          frame_num;
120     size_t          pkt_size;
121     MppDecBufMode   buf_mode;
122 
123     /* use for mpi_dec_multi_test */
124     RK_S32          nthreads;
125     // report information
126     size_t          max_usage;
127 
128     /* data for share */
129     FileReader      reader;
130     FpsCalc         fps;
131 
132     /* runtime log flag */
133     RK_U32          quiet;
134     RK_U32          trace_fps;
135     char            *file_slt;
136 } MpiDecTestCmd;
137 
138 RK_S32  mpi_dec_test_cmd_init(MpiDecTestCmd* cmd, int argc, char **argv);
139 RK_S32  mpi_dec_test_cmd_deinit(MpiDecTestCmd* cmd);
140 void    mpi_dec_test_cmd_options(MpiDecTestCmd* cmd);
141 
142 void    reader_init(FileReader* reader, char* file_in, MppCodingType type);
143 void    reader_deinit(FileReader reader);
144 
145 void    reader_start(FileReader reader);
146 void    reader_sync(FileReader reader);
147 void    reader_stop(FileReader reader);
148 
149 size_t  reader_size(FileReader reader);
150 MPP_RET reader_read(FileReader reader, FileBufSlot **buf);
151 MPP_RET reader_index_read(FileReader reader, RK_S32 index, FileBufSlot **buf);
152 void    reader_rewind(FileReader reader);
153 
154 MPP_RET dec_buf_mgr_init(DecBufMgr *mgr);
155 void dec_buf_mgr_deinit(DecBufMgr mgr);
156 MppBufferGroup dec_buf_mgr_setup(DecBufMgr mgr, RK_U32 size, RK_U32 count, MppDecBufMode mode);
157 
158 void show_dec_fps(RK_S64 total_time, RK_S64 total_count, RK_S64 last_time, RK_S64 last_count);
159 
160 #endif