-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.cpp
462 lines (372 loc) · 12.1 KB
/
main.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
#if defined(_WIN32)
#include "vld.h"
#endif
#define MODULE_TAG "mpi_dec_test"
#include <string>
#include <iostream>
#include <string.h>
#include <cstring>
#include <cstdio>
#include <unistd.h>
#include <cassert>
#include <map>
#include <stdlib.h>
#include "rk_mpi.h"
#include "rk_vdec_cfg.h"
#include "mpp_common.h"
#include "utils.h"
#define MPI_DEC_LOOP_COUNT 4
#define MPI_DEC_STREAM_SIZE (SZ_4M)
#define MAX_FILE_NAME_LENGTH 256
std::map<int, std::string> framesData;
typedef struct {
MppCtx ctx;
MppApi *mpi;
/* end of stream flag when set quit the loop */
RK_U32 eos;
/* buffer for stream data reading */
char *buf;
/* input and output */
MppBufferGroup frm_grp;
MppBufferGroup pkt_grp;
MppPacket packet;
size_t packet_size;
MppFrame frame;
FILE *fp_output;
RK_U32 frame_count;
} MpiDecLoopData;
typedef struct {
char file_output[MAX_FILE_NAME_LENGTH];
MppCodingType type;
RK_U32 width;
RK_U32 height;
RK_U32 debug;
RK_U32 have_output;
RK_S32 timeout;
} MpiDecTestCmd;
static int decode_simple(MpiDecLoopData *data)
{
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 = data->packet;
MppFrame frame = NULL;
static int frameNum = 0;
++frameNum;
printf("Put frame: %d\n", frameNum);
static std::map<int, uint64_t> loadTimes;
int read_size = 0;
if (framesData.find(frameNum-1) != framesData.end()) {
std::string& frameData = framesData[frameNum-1];
read_size = frameData.size();
memcpy(buf, &frameData[0], read_size);
} else {
data->eos = pkt_eos = 1;
}
// std::cout << "Loading frame " << frameNum << ", size: " << read_size << "\n";
loadTimes[frameNum] = millis();
// write data to packet
mpp_packet_write(packet, 0, buf, read_size);
// reset pos and set valid length
mpp_packet_set_pos(packet, buf);
mpp_packet_set_length(packet, read_size);
// setup eos flag
if (pkt_eos)
mpp_packet_set_eos(packet);
do {
RK_S32 times = 5;
// send the packet first if packet is not done
uint64_t t1 = millis();
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--;
usleep(2000);
goto try_again;
}
printf("decode_get_frame failed too much time\n");
}
if (MPP_OK != ret) {
printf("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);
printf("decode_get_frame get info changed found\n");
printf("decoder require buffer w:h [%d:%d] stride [%d:%d]",
width, height, hor_stride, ver_stride);
ret = mpp_buffer_group_get_internal(&data->frm_grp, MPP_BUFFER_TYPE_ION);
if (ret) {
printf("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 {
uint64_t t2 = millis();
get_frm = 1;
printf("Decode frame: %d, time: %d ms\n", data->frame_count+1, (int)(t2-loadTimes[data->frame_count+1]));
err_info = mpp_frame_get_errinfo(frame) | mpp_frame_get_discard(frame);
if (err_info) {
printf("decoder_get_frame get err info:%d discard:%d.\n",
mpp_frame_get_errinfo(frame), mpp_frame_get_discard(frame));
}
// printf("decode_get_frame get frame %d\n", data->frame_count++);
data->frame_count++;
if (data->fp_output && !err_info)
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;
}
// if last packet is send but last frame is not found continue
if (pkt_eos && pkt_done && !frm_eos) {
usleep(10000);
continue;
}
if (frm_eos) {
printf("found last frame\n");
break;
}
if (get_frm)
break;
usleep(2000);
} while (1);
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.
*/
usleep(3000);
} while (1);
return ret;
}
int mpi_dec_test_decode(MpiDecTestCmd *cmd)
{
MPP_RET ret = MPP_OK;
size_t file_size = 0;
uint64_t t1, t2;
// base flow context
MppCtx ctx = NULL;
MppApi *mpi = NULL;
// input / output
MppPacket packet = NULL;
MppFrame frame = NULL;
MpiCmd mpi_cmd = MPP_CMD_BASE;
MppParam param = NULL;
RK_U32 need_split = 0;
RK_U32 need_immediate = 1;
RK_S32 fast_out = 1;
RK_U32 batch_mode = 0;
RK_U32 sort_pts = 1;
RK_U32 enable_vproc = 1;
RK_U32 fast_parse = 1;
RK_U32 output_block = MPP_POLL_BLOCK;
RK_S64 block_timeout = cmd->timeout;
// paramter for resource malloc
RK_U32 width = cmd->width;
RK_U32 height = cmd->height;
MppCodingType type = cmd->type;
MppDecCfg cfg = nullptr;
// resources
char *buf = NULL;
size_t packet_size = MPI_DEC_STREAM_SIZE;
MppBuffer pkt_buf = NULL;
MppBuffer frm_buf = NULL;
MpiDecLoopData data;
printf("mpi_dec_test start\n");
memset(&data, 0, sizeof(data));
if (cmd->have_output) {
data.fp_output = fopen(cmd->file_output, "w+b");
if (NULL == data.fp_output) {
printf("failed to open output file %s\n", cmd->file_output);
goto MPP_TEST_OUT;
}
}
buf = (char*)malloc(packet_size);
if (NULL == buf) {
printf("mpi_dec_test malloc input stream buffer failed\n");
goto MPP_TEST_OUT;
}
ret = mpp_packet_init(&packet, buf, packet_size);
if (ret) {
printf("mpp_packet_init failed\n");
goto MPP_TEST_OUT;
}
printf("mpi_dec_test decoder test start w %d h %d type %d\n", width, height, type);
// decoder demo
ret = mpp_create(&ctx, &mpi);
if (MPP_OK != ret) {
printf("mpp_create failed\n");
goto MPP_TEST_OUT;
}
// NOTE: decoder split mode need to be set before init
mpi_cmd = MPP_DEC_SET_PARSER_SPLIT_MODE;
param = &need_split;
ret = mpi->control(ctx, mpi_cmd, param);
if (MPP_OK != ret) {
printf("mpi->control failed\n");
goto MPP_TEST_OUT;
}
// mpi_cmd = MPP_DEC_SET_IMMEDIATE_OUT;
// param = &need_immediate;
// ret = mpi->control(ctx, mpi_cmd, param);
// if (MPP_OK != ret) {
// printf("mpi->control failed\n");
// goto MPP_TEST_OUT;
// }
if (block_timeout) {
param = &output_block;
ret = mpi->control(ctx, MPP_SET_OUTPUT_BLOCK, param);
if (MPP_OK != ret) {
printf("Failed to set blocking mode on MPI (code = %d).\n", ret);
goto MPP_TEST_OUT;
}
param = &block_timeout;
ret = mpi->control(ctx, MPP_SET_OUTPUT_BLOCK_TIMEOUT, param);
if (MPP_OK != ret) {
printf("Failed to set blocking mode on MPI (code = %d).\n", ret);
goto MPP_TEST_OUT;
}
}
ret = mpp_init(ctx, MPP_CTX_DEC, type);
if (MPP_OK != ret) {
printf("mpp_init failed\n");
goto MPP_TEST_OUT;
}
mpp_dec_cfg_init(&cfg);
/*
ret = mpp_dec_cfg_set_u32(cfg, "base:split_parse", need_split);
if (ret) {
printf("%p failed to set split_parse ret %d\n", ctx, ret);
goto MPP_TEST_OUT;
}
ret = mpp_dec_cfg_set_u32(cfg, "base:fast_out", fast_out);
if (ret) {
printf("%p failed to set fast_out ret %d\n", ctx, ret);
goto MPP_TEST_OUT;
}
ret = mpp_dec_cfg_set_u32(cfg, "base:batch_mode", batch_mode);
if (ret) {
printf("%p failed to set batch_mode ret %d\n", ctx, ret);
goto MPP_TEST_OUT;
}
ret = mpp_dec_cfg_set_u32(cfg, "base:sort_pts", sort_pts);
if (ret) {
printf("%p failed to set batch_mode ret %d\n", ctx, ret);
goto MPP_TEST_OUT;
}
ret = mpp_dec_cfg_set_u32(cfg, "base:enable_vproc", enable_vproc);
if (ret) {
printf("%p failed to set batch_mode ret %d\n", ctx, ret);
goto MPP_TEST_OUT;
}
*/
ret = mpi->control(ctx, MPP_DEC_SET_CFG, cfg);
if (ret) {
printf("%p failed to set cfg %p ret %d\n", ctx, cfg, ret);
goto MPP_TEST_OUT;
}
data.ctx = ctx;
data.mpi = mpi;
data.eos = 0;
data.buf = buf;
data.packet = packet;
data.packet_size = packet_size;
data.frame = frame;
data.frame_count = 0;
t1 = millis();
while (!data.eos) {
decode_simple(&data);
}
t2 = millis();
std::cout << "\nTotal time: " << (t2 - t1) << "ms\n";
ret = mpi->reset(ctx);
if (MPP_OK != ret) {
printf("mpi->reset failed\n");
goto MPP_TEST_OUT;
}
MPP_TEST_OUT:
if (packet) {
mpp_packet_deinit(&packet);
packet = NULL;
}
if (frame) {
mpp_frame_deinit(&frame);
frame = NULL;
}
if (ctx) {
mpp_destroy(ctx);
ctx = NULL;
}
if (buf) {
free(buf);
buf = NULL;
}
if (data.pkt_grp) {
mpp_buffer_group_put(data.pkt_grp);
data.pkt_grp = NULL;
}
if (data.frm_grp) {
mpp_buffer_group_put(data.frm_grp);
data.frm_grp = NULL;
}
if (data.fp_output) {
fclose(data.fp_output);
data.fp_output = NULL;
}
return ret;
}
int main(int argc, char **argv)
{
for (int i = 0; i < 60; ++i) {
std::string fname = std::string("frame_") + std::to_string(i%30+1) + ".264";
framesData[i] = readFromFileFull(fname);
if (framesData[i].size() == 0) {
std::cerr << "Failed to load file " << fname << "\n";
throw 1;
}
// std::cout << "Read frame " << i << ", size: " << framesData[i].size() << "\n";
}
RK_S32 ret = 0;
MpiDecTestCmd cmd_ctx;
MpiDecTestCmd* cmd = &cmd_ctx;
memset((void*)cmd, 0, sizeof(*cmd));
cmd->type = MPP_VIDEO_CodingAVC;
cmd->width = 1920;
cmd->height = 1080;
std::string outFile = "dump.yuv";
memcpy(cmd->file_output, &outFile[0], outFile.size());
cmd->have_output = 1;
// parse the cmd option
ret = mpi_dec_test_decode(cmd);
if (MPP_OK == ret)
printf("test success\n");
else
printf("test failed ret %d\n", ret);
return ret;
}