-
Notifications
You must be signed in to change notification settings - Fork 156
/
mpi_dec_test.c
967 lines (830 loc) · 30.7 KB
/
mpi_dec_test.c
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
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
/*
* Copyright 2015 Rockchip Electronics Co. LTD
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#if defined(_WIN32)
#include "vld.h"
#endif
#define MODULE_TAG "mpi_dec_test"
#include <string.h>
#include "rk_mpi.h"
#include "mpp_log.h"
#include "mpp_mem.h"
#include "mpp_env.h"
#include "mpp_time.h"
#include "mpp_common.h"
#include "utils.h"
#define MPI_DEC_LOOP_COUNT 4
#define MPI_DEC_STREAM_SIZE (SZ_4K)
#define MAX_FILE_NAME_LENGTH 256
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_input;
FILE *fp_output;
FILE *fp_config;
RK_S32 frame_count;
RK_S32 frame_num;
size_t max_usage;
} MpiDecLoopData;
typedef struct {
char file_input[MAX_FILE_NAME_LENGTH];
char file_output[MAX_FILE_NAME_LENGTH];
char file_config[MAX_FILE_NAME_LENGTH];
MppCodingType type;
MppFrameFormat format;
RK_U32 width;
RK_U32 height;
RK_U32 debug;
RK_U32 have_input;
RK_U32 have_output;
RK_U32 have_config;
RK_U32 simple;
RK_S32 timeout;
RK_S32 frame_num;
size_t pkt_size;
// report information
size_t max_usage;
} MpiDecTestCmd;
static OptionInfo mpi_dec_cmd[] = {
{"i", "input_file", "input bitstream file"},
{"o", "output_file", "output bitstream file, "},
{"c", "ops_file", "input operation config file"},
{"w", "width", "the width of input bitstream"},
{"h", "height", "the height of input bitstream"},
{"t", "type", "input stream coding type"},
{"f", "format", "output frame format type"},
{"d", "debug", "debug flag"},
{"x", "timeout", "output timeout interval"},
{"n", "frame_number", "max output frame number"},
};
static int decode_simple(MpiDecLoopData *data)
{
RK_U32 pkt_done = 0;
RK_U32 pkt_eos = 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;
size_t read_size = 0;
size_t packet_size = data->packet_size;
do {
if (data->fp_config) {
char line[MAX_FILE_NAME_LENGTH];
char *ptr = NULL;
do {
ptr = fgets(line, MAX_FILE_NAME_LENGTH, data->fp_config);
if (ptr) {
OpsLine info;
RK_S32 cnt = parse_config_line(line, &info);
// parser for packet message
if (cnt >= 3 && 0 == strncmp("pkt", info.cmd, sizeof(info.cmd))) {
packet_size = info.value2;
break;
}
// parser for reset message at the end
if (0 == strncmp("rst", info.cmd, 3)) {
mpp_log("%p get reset cmd\n", ctx);
packet_size = 0;
break;
}
} else {
mpp_log("%p get end of cfg file\n", ctx);
packet_size = 0;
break;
}
} while (1);
}
// when packet size is valid read the input binary file
if (packet_size)
read_size = fread(buf, 1, packet_size, data->fp_input);
if (!packet_size || read_size != packet_size || feof(data->fp_input)) {
if (data->frame_num < 0) {
clearerr(data->fp_input);
rewind(data->fp_input);
if (data->fp_config) {
clearerr(data->fp_config);
rewind(data->fp_config);
}
data->eos = pkt_eos = 0;
mpp_log("%p loop again\n", ctx);
} else {
// setup eos flag
data->eos = pkt_eos = 1;
mpp_log("%p found last packet\n", ctx);
break;
}
}
} while (!read_size);
// 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
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("%p decode_get_frame failed too much time\n", ctx);
}
if (MPP_OK != ret) {
mpp_err("%p decode_get_frame failed ret %d\n", ret, ctx);
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("%p decode_get_frame get info changed found\n", ctx);
mpp_log("%p decoder require buffer w:h [%d:%d] stride [%d:%d] buf_size %d",
ctx, width, height, hor_stride, ver_stride, buf_size);
/*
* NOTE: We can choose decoder's buffer mode here.
* There are three mode that decoder can support:
*
* Mode 1: Pure internal mode
* In the mode user will NOT call MPP_DEC_SET_EXT_BUF_GROUP
* control to decoder. Only call MPP_DEC_SET_INFO_CHANGE_READY
* to let decoder go on. Then decoder will use create buffer
* internally and user need to release each frame they get.
*
* Advantage:
* Easy to use and get a demo quickly
* Disadvantage:
* 1. The buffer from decoder may not be return before
* decoder is close. So memroy leak or crash may happen.
* 2. The decoder memory usage can not be control. Decoder
* is on a free-to-run status and consume all memory it can
* get.
* 3. Difficult to implement zero-copy display path.
*
* Mode 2: Half internal mode
* This is the mode current test code using. User need to
* create MppBufferGroup according to the returned info
* change MppFrame. User can use mpp_buffer_group_limit_config
* function to limit decoder memory usage.
*
* Advantage:
* 1. Easy to use
* 2. User can release MppBufferGroup after decoder is closed.
* So memory can stay longer safely.
* 3. Can limit the memory usage by mpp_buffer_group_limit_config
* Disadvantage:
* 1. The buffer limitation is still not accurate. Memory usage
* is 100% fixed.
* 2. Also difficult to implement zero-copy display path.
*
* Mode 3: Pure external mode
* In this mode use need to create empty MppBufferGroup and
* import memory from external allocator by file handle.
* On Android surfaceflinger will create buffer. Then
* mediaserver get the file handle from surfaceflinger and
* commit to decoder's MppBufferGroup.
*
* Advantage:
* 1. Most efficient way for zero-copy display
* Disadvantage:
* 1. Difficult to learn and use.
* 2. Player work flow may limit this usage.
* 3. May need a external parser to get the correct buffer
* size for the external allocator.
*
* The required buffer size caculation:
* hor_stride * ver_stride * 3 / 2 for pixel data
* hor_stride * ver_stride / 2 for extra info
* Total hor_stride * ver_stride * 2 will be enough.
*
* For H.264/H.265 20+ buffers will be enough.
* For other codec 10 buffers will be enough.
*/
if (NULL == data->frm_grp) {
/* If buffer group is not set create one and limit it */
ret = mpp_buffer_group_get_internal(&data->frm_grp, MPP_BUFFER_TYPE_ION);
if (ret) {
mpp_err("%p get mpp buffer group failed ret %d\n", ctx, ret);
break;
}
/* Set buffer to mpp decoder */
ret = mpi->control(ctx, MPP_DEC_SET_EXT_BUF_GROUP, data->frm_grp);
if (ret) {
mpp_err("%p set buffer group failed ret %d\n", ctx, ret);
break;
}
} else {
/* If old buffer group exist clear it */
ret = mpp_buffer_group_clear(data->frm_grp);
if (ret) {
mpp_err("%p clear buffer group failed ret %d\n", ctx, ret);
break;
}
}
/* Use limit config to limit buffer count to 24 with buf_size */
ret = mpp_buffer_group_limit_config(data->frm_grp, buf_size, 24);
if (ret) {
mpp_err("%p limit buffer group failed ret %d\n", ctx, ret);
break;
}
/*
* All buffer group config done. Set info change ready to let
* decoder continue decoding
*/
ret = mpi->control(ctx, MPP_DEC_SET_INFO_CHANGE_READY, NULL);
if (ret) {
mpp_err("%p info change ready failed ret %d\n", ctx, ret);
break;
}
} else {
char log_buf[256];
RK_S32 log_size = sizeof(log_buf) - 1;
RK_S32 log_len = 0;
RK_U32 err_info = mpp_frame_get_errinfo(frame);
RK_U32 discard = mpp_frame_get_discard(frame);
log_len += snprintf(log_buf + log_len, log_size - log_len,
"decode get frame %d", data->frame_count);
if (mpp_frame_has_meta(frame)) {
MppMeta meta = mpp_frame_get_meta(frame);
RK_S32 temporal_id = 0;
mpp_meta_get_s32(meta, KEY_TEMPORAL_ID, &temporal_id);
log_len += snprintf(log_buf + log_len, log_size - log_len,
" tid %d", temporal_id);
}
if (err_info || discard) {
log_len += snprintf(log_buf + log_len, log_size - log_len,
" err %x discard %x", err_info, discard);
}
mpp_log("%p %s\n", ctx, log_buf);
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;
}
// 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("%p found last packet\n", ctx);
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("%p reach max frame number %d\n", ctx, 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);
return ret;
}
static int decode_advanced(MpiDecLoopData *data)
{
RK_U32 pkt_eos = 0;
MPP_RET ret = MPP_OK;
MppCtx ctx = data->ctx;
MppApi *mpi = data->mpi;
char *buf = data->buf;
MppPacket packet = data->packet;
MppFrame frame = data->frame;
MppTask task = NULL;
size_t read_size = fread(buf, 1, data->packet_size, data->fp_input);
if (read_size != data->packet_size || feof(data->fp_input)) {
mpp_log("%p found last packet\n", ctx);
// setup eos flag
data->eos = pkt_eos = 1;
}
// reset pos
mpp_packet_set_pos(packet, buf);
mpp_packet_set_length(packet, read_size);
// setup eos flag
if (pkt_eos)
mpp_packet_set_eos(packet);
ret = mpi->poll(ctx, MPP_PORT_INPUT, MPP_POLL_BLOCK);
if (ret) {
mpp_err("%p mpp input poll failed\n", ctx);
return ret;
}
ret = mpi->dequeue(ctx, MPP_PORT_INPUT, &task); /* input queue */
if (ret) {
mpp_err("%p mpp task input dequeue failed\n", ctx);
return ret;
}
mpp_assert(task);
mpp_task_meta_set_packet(task, KEY_INPUT_PACKET, packet);
mpp_task_meta_set_frame (task, KEY_OUTPUT_FRAME, frame);
ret = mpi->enqueue(ctx, MPP_PORT_INPUT, task); /* input queue */
if (ret) {
mpp_err("%p mpp task input enqueue failed\n", ctx);
return ret;
}
/* poll and wait here */
ret = mpi->poll(ctx, MPP_PORT_OUTPUT, MPP_POLL_BLOCK);
if (ret) {
mpp_err("%p mpp output poll failed\n", ctx);
return ret;
}
ret = mpi->dequeue(ctx, MPP_PORT_OUTPUT, &task); /* output queue */
if (ret) {
mpp_err("%p mpp task output dequeue failed\n", ctx);
return ret;
}
mpp_assert(task);
if (task) {
MppFrame frame_out = NULL;
mpp_task_meta_get_frame(task, KEY_OUTPUT_FRAME, &frame_out);
//mpp_assert(packet_out == packet);
if (frame) {
/* write frame to file here */
if (data->fp_output)
dump_mpp_frame_to_file(frame, data->fp_output);
mpp_log("%p decoded frame %d\n", ctx, data->frame_count);
data->frame_count++;
if (mpp_frame_get_eos(frame_out))
mpp_log("%p found eos frame\n", ctx);
}
/* output queue */
ret = mpi->enqueue(ctx, MPP_PORT_OUTPUT, task);
if (ret)
mpp_err("%p mpp task output enqueue failed\n", ctx);
}
return ret;
}
int mpi_dec_test_decode(MpiDecTestCmd *cmd)
{
MPP_RET ret = MPP_OK;
size_t file_size = 0;
// 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 = 1;
MppPollType timeout = cmd->timeout;
// paramter for resource malloc
RK_U32 width = cmd->width;
RK_U32 height = cmd->height;
MppCodingType type = cmd->type;
// resources
char *buf = NULL;
size_t packet_size = cmd->pkt_size;
MppBuffer pkt_buf = NULL;
MppBuffer frm_buf = NULL;
MpiDecLoopData data;
mpp_log("mpi_dec_test start\n");
memset(&data, 0, sizeof(data));
if (cmd->have_input) {
data.fp_input = fopen(cmd->file_input, "rb");
if (NULL == data.fp_input) {
mpp_err("failed to open input file %s\n", cmd->file_input);
goto MPP_TEST_OUT;
}
fseek(data.fp_input, 0L, SEEK_END);
file_size = ftell(data.fp_input);
rewind(data.fp_input);
mpp_log("input file size %ld\n", file_size);
}
if (cmd->have_output) {
data.fp_output = fopen(cmd->file_output, "w+b");
if (NULL == data.fp_output) {
mpp_err("failed to open output file %s\n", cmd->file_output);
goto MPP_TEST_OUT;
}
}
if (cmd->have_config) {
data.fp_config = fopen(cmd->file_config, "r");
if (NULL == data.fp_config) {
mpp_err("failed to open config file %s\n", cmd->file_config);
goto MPP_TEST_OUT;
}
}
if (cmd->simple) {
buf = mpp_malloc(char, packet_size);
if (NULL == buf) {
mpp_err("mpi_dec_test malloc input stream buffer failed\n");
goto MPP_TEST_OUT;
}
ret = mpp_packet_init(&packet, buf, packet_size);
if (ret) {
mpp_err("mpp_packet_init failed\n");
goto MPP_TEST_OUT;
}
} else {
RK_U32 hor_stride = MPP_ALIGN(width, 16);
RK_U32 ver_stride = MPP_ALIGN(height, 16);
ret = mpp_buffer_group_get_internal(&data.frm_grp, MPP_BUFFER_TYPE_ION);
if (ret) {
mpp_err("failed to get buffer group for input frame ret %d\n", ret);
goto MPP_TEST_OUT;
}
ret = mpp_buffer_group_get_internal(&data.pkt_grp, MPP_BUFFER_TYPE_ION);
if (ret) {
mpp_err("failed to get buffer group for output packet ret %d\n", ret);
goto MPP_TEST_OUT;
}
ret = mpp_frame_init(&frame); /* output frame */
if (MPP_OK != ret) {
mpp_err("mpp_frame_init failed\n");
goto MPP_TEST_OUT;
}
/*
* NOTE: For jpeg could have YUV420 and YUV422 the buffer should be
* larger for output. And the buffer dimension should align to 16.
* YUV420 buffer is 3/2 times of w*h.
* YUV422 buffer is 2 times of w*h.
* So create larger buffer with 2 times w*h.
*/
ret = mpp_buffer_get(data.frm_grp, &frm_buf, hor_stride * ver_stride * 4);
if (ret) {
mpp_err("failed to get buffer for input frame ret %d\n", ret);
goto MPP_TEST_OUT;
}
// NOTE: for mjpeg decoding send the whole file
if (type == MPP_VIDEO_CodingMJPEG) {
packet_size = file_size;
}
ret = mpp_buffer_get(data.pkt_grp, &pkt_buf, packet_size);
if (ret) {
mpp_err("failed to get buffer for input frame ret %d\n", ret);
goto MPP_TEST_OUT;
}
mpp_packet_init_with_buffer(&packet, pkt_buf);
buf = mpp_buffer_get_ptr(pkt_buf);
mpp_frame_set_buffer(frame, frm_buf);
}
// decoder demo
ret = mpp_create(&ctx, &mpi);
if (MPP_OK != ret) {
mpp_err("mpp_create failed\n");
goto MPP_TEST_OUT;
}
mpp_log("%p mpi_dec_test decoder test start w %d h %d type %d\n",
ctx, width, height, type);
// 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) {
mpp_err("%p mpi->control failed\n", ctx);
goto MPP_TEST_OUT;
}
// NOTE: timeout value please refer to MppPollType definition
// 0 - non-block call (default)
// -1 - block call
// +val - timeout value in ms
if (timeout) {
param = &timeout;
ret = mpi->control(ctx, MPP_SET_OUTPUT_TIMEOUT, param);
if (MPP_OK != ret) {
mpp_err("%p failed to set output timeout %d ret %d\n",
ctx, timeout, ret);
goto MPP_TEST_OUT;
}
}
ret = mpp_init(ctx, MPP_CTX_DEC, type);
if (MPP_OK != ret) {
mpp_err("%p mpp_init failed\n", ctx);
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;
data.frame_num = cmd->frame_num;
if (cmd->simple) {
while (!data.eos) {
decode_simple(&data);
}
} else {
/* NOTE: change output format before jpeg decoding */
if (cmd->format < MPP_FMT_BUTT)
ret = mpi->control(ctx, MPP_DEC_SET_OUTPUT_FORMAT, &cmd->format);
while (!data.eos) {
decode_advanced(&data);
}
}
cmd->max_usage = data.max_usage;
{
MppDecQueryCfg query;
memset(&query, 0, sizeof(query));
query.query_flag = MPP_DEC_QUERY_ALL;
ret = mpi->control(ctx, MPP_DEC_QUERY, &query);
if (ret) {
mpp_err("%p mpi->control query failed\n", ctx);
goto MPP_TEST_OUT;
}
/*
* NOTE:
* 1. Output frame count included info change frame and empty eos frame.
* 2. Hardware run count is real decoded frame count.
*/
mpp_log("%p input %d pkt output %d frm decode %d frames\n", ctx,
query.dec_in_pkt_cnt, query.dec_out_frm_cnt, query.dec_hw_run_cnt);
}
ret = mpi->reset(ctx);
if (MPP_OK != ret) {
mpp_err("%p mpi->reset failed\n", ctx);
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 (cmd->simple) {
if (buf) {
mpp_free(buf);
buf = NULL;
}
} else {
if (pkt_buf) {
mpp_buffer_put(pkt_buf);
pkt_buf = NULL;
}
if (frm_buf) {
mpp_buffer_put(frm_buf);
frm_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;
}
if (data.fp_input) {
fclose(data.fp_input);
data.fp_input = NULL;
}
return ret;
}
static void mpi_dec_test_help()
{
mpp_log("usage: mpi_dec_test [options]\n");
show_options(mpi_dec_cmd);
mpp_show_support_format();
}
static RK_S32 mpi_dec_test_parse_options(int argc, char **argv, MpiDecTestCmd* cmd)
{
const char *opt;
const char *next;
RK_S32 optindex = 1;
RK_S32 handleoptions = 1;
RK_S32 err = MPP_NOK;
if ((argc < 2) || (cmd == NULL)) {
err = 1;
return err;
}
/* parse options */
while (optindex < argc) {
opt = (const char*)argv[optindex++];
next = (const char*)argv[optindex];
if (handleoptions && opt[0] == '-' && opt[1] != '\0') {
if (opt[1] == '-') {
if (opt[2] != '\0') {
opt++;
} else {
handleoptions = 0;
continue;
}
}
opt++;
switch (*opt) {
case 'i':
if (next) {
strncpy(cmd->file_input, next, MAX_FILE_NAME_LENGTH - 1);
cmd->have_input = 1;
name_to_coding_type(cmd->file_input, &cmd->type);
} else {
mpp_err("input file is invalid\n");
goto PARSE_OPINIONS_OUT;
}
break;
case 'o':
if (next) {
strncpy(cmd->file_output, next, MAX_FILE_NAME_LENGTH - 1);
cmd->have_output = 1;
} else {
mpp_log("output file is invalid\n");
goto PARSE_OPINIONS_OUT;
}
break;
case 'c':
if (next) {
strncpy(cmd->file_config, next, MAX_FILE_NAME_LENGTH - 1);
cmd->have_config = 1;
// enlarge packet buffer size for large input stream case
cmd->pkt_size = SZ_1M;
} else {
mpp_log("output file is invalid\n");
goto PARSE_OPINIONS_OUT;
}
break;
case 'd':
if (next) {
cmd->debug = atoi(next);;
} else {
mpp_err("invalid debug flag\n");
goto PARSE_OPINIONS_OUT;
}
break;
case 'w':
if (next) {
cmd->width = atoi(next);
} else {
mpp_err("invalid input width\n");
goto PARSE_OPINIONS_OUT;
}
break;
case 'h':
if ((*(opt + 1) != '\0') && !strncmp(opt, "help", 4)) {
mpi_dec_test_help();
err = 1;
goto PARSE_OPINIONS_OUT;
} else if (next) {
cmd->height = atoi(next);
} else {
mpp_log("input height is invalid\n");
goto PARSE_OPINIONS_OUT;
}
break;
case 't':
if (next) {
cmd->type = (MppCodingType)atoi(next);
err = mpp_check_support_format(MPP_CTX_DEC, cmd->type);
}
if (!next || err) {
mpp_err("invalid input coding type\n");
goto PARSE_OPINIONS_OUT;
}
break;
case 'f':
if (next) {
cmd->format = (MppFrameFormat)atoi(next);
}
if (!next || err) {
mpp_err("invalid input coding type\n");
goto PARSE_OPINIONS_OUT;
}
break;
case 'x':
if (next) {
cmd->timeout = atoi(next);
}
if (!next || cmd->timeout < 0) {
mpp_err("invalid output timeout interval\n");
goto PARSE_OPINIONS_OUT;
}
break;
case 'n':
if (next) {
cmd->frame_num = atoi(next);
if (cmd->frame_num < 0)
mpp_log("infinite loop decoding mode\n");
} else {
mpp_err("invalid frame number\n");
goto PARSE_OPINIONS_OUT;
}
break;
default:
mpp_err("skip invalid opt %c\n", *opt);
break;
}
optindex++;
}
}
err = 0;
PARSE_OPINIONS_OUT:
return err;
}
static void mpi_dec_test_show_options(MpiDecTestCmd* cmd)
{
mpp_log("cmd parse result:\n");
mpp_log("input file name: %s\n", cmd->file_input);
mpp_log("output file name: %s\n", cmd->file_output);
mpp_log("config file name: %s\n", cmd->file_config);
mpp_log("width : %4d\n", cmd->width);
mpp_log("height : %4d\n", cmd->height);
mpp_log("type : %d\n", cmd->type);
mpp_log("debug flag : %x\n", cmd->debug);
mpp_log("max frames : %d\n", cmd->frame_num);
}
int main(int argc, char **argv)
{
RK_S32 ret = 0;
MpiDecTestCmd cmd_ctx;
MpiDecTestCmd* cmd = &cmd_ctx;
memset((void*)cmd, 0, sizeof(*cmd));
cmd->format = MPP_FMT_BUTT;
cmd->pkt_size = MPI_DEC_STREAM_SIZE;
// parse the cmd option
ret = mpi_dec_test_parse_options(argc, argv, cmd);
if (ret) {
if (ret < 0) {
mpp_err("mpi_dec_test_parse_options: input parameter invalid\n");
}
mpi_dec_test_help();
return ret;
}
mpi_dec_test_show_options(cmd);
mpp_env_set_u32("mpi_debug", cmd->debug);
cmd->simple = (cmd->type != MPP_VIDEO_CodingMJPEG) ? (1) : (0);
ret = mpi_dec_test_decode(cmd);
if (MPP_OK == ret)
mpp_log("test success max memory %.2f MB\n", cmd->max_usage / (float)(1 << 20));
else
mpp_err("test failed ret %d\n", ret);
mpp_env_set_u32("mpi_debug", 0x0);
return ret;
}