forked from MUZLATAN/ffmpeg_rtsp_mpp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MppDecode.cpp
executable file
·255 lines (215 loc) · 7.85 KB
/
MppDecode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
//
// Created by LX on 2020/4/25.
//
#include "MppDecode.h"
int frame_null = 0;
int count = 0;
void dump_mpp_frame_to_file(MppFrame frame, FILE *fp)
{
RK_U32 width = 0;
RK_U32 height = 0;
RK_U32 h_stride = 0;
RK_U32 v_stride = 0;
MppBuffer buffer = NULL;
RK_U8 *base = NULL;
width = mpp_frame_get_width(frame);
height = mpp_frame_get_height(frame);
h_stride = mpp_frame_get_hor_stride(frame);
v_stride = mpp_frame_get_ver_stride(frame);
buffer = mpp_frame_get_buffer(frame);
base = (RK_U8 *)mpp_buffer_get_ptr(buffer);
RK_U32 buf_size = mpp_frame_get_buf_size(frame);
size_t base_length = mpp_buffer_get_size(buffer);
mpp_log("base_length = %d\n",base_length);
RK_U32 i;
RK_U8 *base_y = base;
RK_U8 *base_c = base + h_stride * v_stride;
//保存为YUV420sp格式
/*for (i = 0; i < height; i++, base_y += h_stride)
{
fwrite(base_y, 1, width, fp);
}
for (i = 0; i < height / 2; i++, base_c += h_stride)
{
fwrite(base_c, 1, width, fp);
}*/
//保存为YUV420p格式
for(i = 0; i < height; i++, base_y += h_stride)
{
fwrite(base_y, 1, width, fp);
}
for(i = 0; i < height * width / 2; i+=2)
{
fwrite((base_c + i), 1, 1, fp);
}
for(i = 1; i < height * width / 2; i+=2)
{
fwrite((base_c + i), 1, 1, fp);
}
}
size_t mpp_buffer_group_usage(MppBufferGroup group)
{
if (NULL == group)
{
mpp_err_f("input invalid group %p\n", group);
return MPP_BUFFER_MODE_BUTT;
}
MppBufferGroupImpl *p = (MppBufferGroupImpl *)group;
return p->usage;
}
int decode_simple(MpiDecLoopData *data, AVPacket *av_packet )
{
RK_U32 pkt_done = 0;
RK_U32 pkt_eos = 0;
RK_U32 err_info = 0;
MPP_RET ret = MPP_OK;
MppCtx ctx = data->ctx;
MppApi *mpi = data->mpi;
// char *buf = data->buf;
MppPacket packet = NULL;
MppFrame frame = NULL;
size_t read_size = 0;
size_t packet_size = data->packet_size;
ret = mpp_packet_init(&packet, av_packet->data, av_packet->size);
mpp_packet_set_pts(packet, av_packet->pts);
do {
RK_S32 times = 5;
// send the packet first if packet is not done
if (!pkt_done) {
ret = mpi->decode_put_packet(ctx, packet);
if (MPP_OK == ret)
pkt_done = 1;
}
// then get all available frame and release
do {
RK_S32 get_frm = 0;
RK_U32 frm_eos = 0;
try_again:
ret = mpi->decode_get_frame(ctx, &frame);
if (MPP_ERR_TIMEOUT == ret) {
if (times > 0) {
times--;
msleep(2);
goto try_again;
}
mpp_err("decode_get_frame failed too much time\n");
}
if (MPP_OK != ret) {
mpp_err("decode_get_frame failed ret %d\n", ret);
break;
}
if (frame) {
if (mpp_frame_get_info_change(frame)) {
RK_U32 width = mpp_frame_get_width(frame);
RK_U32 height = mpp_frame_get_height(frame);
RK_U32 hor_stride = mpp_frame_get_hor_stride(frame);
RK_U32 ver_stride = mpp_frame_get_ver_stride(frame);
RK_U32 buf_size = mpp_frame_get_buf_size(frame);
mpp_log("decode_get_frame get info changed found\n");
mpp_log("decoder require buffer w:h [%d:%d] stride [%d:%d] buf_size %d",
width, height, hor_stride, ver_stride, buf_size);
ret = mpp_buffer_group_get_internal(&data->frm_grp, MPP_BUFFER_TYPE_ION);
if (ret) {
mpp_err("get mpp buffer group failed ret %d\n", ret);
break;
}
mpi->control(ctx, MPP_DEC_SET_EXT_BUF_GROUP, data->frm_grp);
mpi->control(ctx, MPP_DEC_SET_INFO_CHANGE_READY, NULL);
} else {
err_info = mpp_frame_get_errinfo(frame) | mpp_frame_get_discard(frame);
if (err_info) {
mpp_log("decoder_get_frame get err info:%d discard:%d.\n",
mpp_frame_get_errinfo(frame), mpp_frame_get_discard(frame));
}
data->frame_count++;
mpp_log("decode_get_frame get frame %d\n", data->frame_count);
if (data->fp_output && !err_info){
cv::Mat rgbImg;
YUV420SP2Mat(frame, rgbImg);
// cv::imwrite("./"+std::to_string(count++)+".jpg", rgbImg);
// dump_mpp_frame_to_file(frame, data->fp_output);
}
}
frm_eos = mpp_frame_get_eos(frame);
mpp_frame_deinit(&frame);
frame = NULL;
get_frm = 1;
}
// try get runtime frame memory usage
if (data->frm_grp) {
size_t usage = mpp_buffer_group_usage(data->frm_grp);
if (usage > data->max_usage)
data->max_usage = usage;
}
// if last packet is send but last frame is not found continue
if (pkt_eos && pkt_done && !frm_eos) {
msleep(10);
continue;
}
if (frm_eos) {
mpp_log("found last frame\n");
break;
}
if (data->frame_num > 0 && data->frame_count >= data->frame_num) {
data->eos = 1;
break;
}
if (get_frm)
continue;
break;
} while (1);
if (data->frame_num > 0 && data->frame_count >= data->frame_num) {
data->eos = 1;
mpp_log("reach max frame number %d\n", data->frame_count);
break;
}
if (pkt_done)
break;
/*
* why sleep here:
* mpi->decode_put_packet will failed when packet in internal queue is
* full,waiting the package is consumed .Usually hardware decode one
* frame which resolution is 1080p needs 2 ms,so here we sleep 3ms
* * is enough.
*/
msleep(3);
} while (1);
mpp_packet_deinit(&packet);
return ret;
}
void YUV420SP2Mat(MppFrame frame, cv::Mat rgbImg ) {
RK_U32 width = 0;
RK_U32 height = 0;
RK_U32 h_stride = 0;
RK_U32 v_stride = 0;
MppBuffer buffer = NULL;
RK_U8 *base = NULL;
width = mpp_frame_get_width(frame);
height = mpp_frame_get_height(frame);
h_stride = mpp_frame_get_hor_stride(frame);
v_stride = mpp_frame_get_ver_stride(frame);
buffer = mpp_frame_get_buffer(frame);
base = (RK_U8 *)mpp_buffer_get_ptr(buffer);
RK_U32 buf_size = mpp_frame_get_buf_size(frame);
size_t base_length = mpp_buffer_get_size(buffer);
// mpp_log("base_length = %d\n",base_length);
RK_U32 i;
RK_U8 *base_y = base;
RK_U8 *base_c = base + h_stride * v_stride;
cv::Mat yuvImg;
yuvImg.create(height * 3 / 2, width, CV_8UC1);
//转为YUV420p格式
int idx = 0;
for (i = 0; i < height; i++, base_y += h_stride) {
// fwrite(base_y, 1, width, fp);
memcpy(yuvImg.data + idx, base_y, width);
idx += width;
}
for (i = 0; i < height / 2; i++, base_c += h_stride) {
// fwrite(base_c, 1, width, fp);
memcpy(yuvImg.data + idx, base_c, width);
idx += width;
}
//这里的转码需要转为RGB 3通道, RGBA四通道则不能检测成功
cv::cvtColor(yuvImg, rgbImg, CV_YUV420sp2RGB);
}