hc
2024-05-14 bedbef8ad3e75a304af6361af235302bcc61d06b
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
/* Copyright 2020 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.
 *
 * author: rimon.xu@rock-chips.com
 *   date: 2020-11-06
 */
 
#include <stdio.h>
#include <errno.h>
#include <unistd.h>
#include <string.h>
#include "rk_debug.h"
#include "rk_mpi_mb.h"
#include "rk_comm_tde.h"
#include "rk_comm_video.h"
#include "rk_mpi_tde.h"
#include "rk_mpi_sys.h"
#include "rk_mpi_cal.h"
#include "argparse.h"
#include "mpi_test_utils.h"
 
typedef enum rkTDE_OP_TYPE_E {
    TDE_OP_QUICK_COPY = 0,
    TDE_OP_QUICK_RESIZE,
    TDE_OP_QUICK_FILL,
    TDE_OP_ROTATION,
    TDE_OP_MIRROR,
    TDE_OP_COLOR_KEY
} TDE_OP_TYPE_E;
 
typedef struct _rkTDEOpMap {
    RK_S32        op;
    const char*   strOp;
} TDE_OP_MAP_S;
 
const static TDE_OP_MAP_S gstTdeOpMaps[] = {
    { TDE_OP_QUICK_COPY,        "quick_copy" },
    { TDE_OP_QUICK_RESIZE,      "quick_resize" },
    { TDE_OP_QUICK_FILL,        "quick_fill" },
    { TDE_OP_ROTATION,          "rotation" },
    { TDE_OP_MIRROR,            "mirror" },
    { TDE_OP_COLOR_KEY,         "color_key" }
};
 
typedef struct _rkMpiTdeCtx {
    const char     *srcFilePath;
    const char     *dstFilePath;
    RK_S32          s32LoopCount;
    RK_S32          s32JobNum;
    RK_S32          s32TaskNum;
    RK_S32          s32Rotation;
    RK_S32          s32Operation;
    TDE_SURFACE_S   stSrcSurface;
    TDE_SURFACE_S   stDstSurface;
    RECT_S          stSrcRect;
    RECT_S          stDstRect;
    RK_U32          u32SrcVirWidth;
    RK_U32          u32SrcVirHeight;
    RK_S32          s32Color;
    RK_S32          s32Mirror;
    RK_BOOL         bStatEn;
} TEST_TDE_CTX_S;
 
static const char *test_tde_str_op(RK_S32 op) {
    RK_S32 s32ElemLen = sizeof(gstTdeOpMaps) / sizeof(gstTdeOpMaps[0]);
    for (size_t i = 0; i < s32ElemLen; i++) {
        if (op == gstTdeOpMaps[i].op) {
            return gstTdeOpMaps[i].strOp;
        }
    }
 
    return RK_NULL;
}
 
RK_S32 test_tde_save_result(MB_BLK dstBlk, TDE_SURFACE_S  *pstDst, RK_S32 taskId, TEST_TDE_CTX_S *ctx) {
    char yuv_out_path[1024] = {0};
    RK_S32 s32Ret = RK_SUCCESS;
    PIC_BUF_ATTR_S stPicBufAttr;
    MB_PIC_CAL_S stMbPicCalResult;
    RK_VOID *pstFrame = RK_MPI_MB_Handle2VirAddr(dstBlk);
    stPicBufAttr.u32Width = ctx->stDstSurface.u32Width;
    stPicBufAttr.u32Height = ctx->stDstSurface.u32Height;
    stPicBufAttr.enPixelFormat = ctx->stDstSurface.enColorFmt;
    stPicBufAttr.enCompMode = COMPRESS_MODE_NONE;
    s32Ret = RK_MPI_CAL_TDE_GetPicBufferSize(&stPicBufAttr, &stMbPicCalResult);
    if (s32Ret != RK_SUCCESS) {
        RK_LOGE("get picture buffer size failed. err 0x%x", s32Ret);
        return s32Ret;
    }
    RK_U32 u32OutputSize = stMbPicCalResult.u32MBSize;
 
    snprintf(yuv_out_path,
            sizeof(yuv_out_path),
            "%s%s_%dx%d_%d.bin",
            ctx->dstFilePath,
            test_tde_str_op(ctx->s32Operation),
            stPicBufAttr.u32Width,
            stPicBufAttr.u32Height,
            taskId);
 
    FILE *file = fopen(yuv_out_path, "wb+");
    if (file == RK_NULL) {
        RK_LOGE("open path %s failed because %s.", yuv_out_path, strerror(errno));
        return RK_FAILURE;
    }
 
    if (pstFrame) {
        RK_LOGI("get frame data = %p, size = %d, bBlk:%p ", pstFrame, u32OutputSize, dstBlk);
        fwrite(pstFrame, 1, u32OutputSize, file);
        fflush(file);
    }
    fclose(file);
    file = NULL;
 
    return RK_SUCCESS;
}
 
RK_U32 unit_test_tde_get_size(TEST_TDE_CTX_S *ctx) {
    RK_S32 s32Ret = RK_SUCCESS;
    PIC_BUF_ATTR_S stPicBufAttr;
    MB_PIC_CAL_S   stMbPicCalResult;
 
    if (ctx->u32SrcVirWidth == 0) {
        ctx->u32SrcVirWidth = ctx->stSrcSurface.u32Width;
    }
    if (ctx->u32SrcVirHeight == 0) {
        ctx->u32SrcVirHeight = ctx->stSrcSurface.u32Height;
    }
 
    stPicBufAttr.u32Width = ctx->u32SrcVirWidth;
    stPicBufAttr.u32Height = ctx->u32SrcVirHeight;
    stPicBufAttr.enPixelFormat = (PIXEL_FORMAT_E)ctx->stSrcSurface.enColorFmt;
    stPicBufAttr.enCompMode = COMPRESS_MODE_NONE;
    s32Ret = RK_MPI_CAL_COMM_GetPicBufferSize(&stPicBufAttr, &stMbPicCalResult);
    if (s32Ret != RK_SUCCESS) {
        RK_LOGE("get picture buffer size failed. err 0x%x", s32Ret);
        return s32Ret;
    }
 
    return stMbPicCalResult.u32MBSize;
}
 
FILE *test_tde_read_file(const char *path,  void *pu8SrcData, RK_U32 u32ImgSize, TEST_TDE_CTX_S *ctx) {
    FILE  *pFile        = RK_NULL;
    RK_U32 u32ReadSize  = 0;
 
    pFile = fopen(path, "rb+");
    if (pFile == RK_NULL) {
        RK_LOGE("open path %s failed because %s.", path, strerror(errno));
        return RK_NULL;
    }
 
    if (pFile) {
         u32ReadSize = fread(pu8SrcData, 1, u32ImgSize, pFile);
         fflush(pFile);
    }
 
    RK_LOGE("unit_test_open_source u32ReadSize:%d", u32ReadSize);
    if (u32ReadSize != u32ImgSize) {
        fclose(pFile);
        RK_LOGE("read error read %d, request %d", u32ReadSize, u32ImgSize);
        return RK_NULL;
    }
 
    return pFile;
}
 
RK_S32 test_tde_fill_src(
        TEST_TDE_CTX_S *ctx, TDE_SURFACE_S *pstSrcSurface, TDE_RECT_S *pstSrcRect) {
    pstSrcSurface->u32Width   = ctx->stSrcSurface.u32Width;
    pstSrcSurface->u32Height  = ctx->stSrcSurface.u32Height;
    pstSrcSurface->enColorFmt = ctx->stSrcSurface.enColorFmt;
    pstSrcRect->s32Xpos   = ctx->stSrcRect.s32X;
    pstSrcRect->s32Ypos   = ctx->stSrcRect.s32Y;
    pstSrcRect->u32Width  = ctx->stSrcRect.u32Width;
    pstSrcRect->u32Height = ctx->stSrcRect.u32Height;
 
    return RK_SUCCESS;
}
 
RK_S32 test_tde_fill_dst(
        TEST_TDE_CTX_S *ctx, TDE_SURFACE_S *pstDstSurface, TDE_RECT_S *pstDstRect) {
    pstDstSurface->u32Width = ctx->stDstSurface.u32Width;
    pstDstSurface->u32Height = ctx->stDstSurface.u32Height;
    pstDstSurface->enColorFmt = ctx->stDstSurface.enColorFmt;
    pstDstRect->s32Xpos   = ctx->stDstRect.s32X;
    pstDstRect->s32Ypos   = ctx->stDstRect.s32Y;
    pstDstRect->u32Width  = ctx->stDstRect.u32Width;
    pstDstRect->u32Height = ctx->stDstRect.u32Height;
 
    return RK_SUCCESS;
}
 
RK_S32 test_tde_quick_copy_task(TDE_SURFACE_S *pstSrc,
                                      TDE_RECT_S   *pstSrcRect,
                                      TDE_SURFACE_S *pstDst,
                                      TDE_RECT_S  *pstDstRect,
                                      MB_BLK srcBlk,
                                      RK_U32 u32ImgSize,
                                      TEST_TDE_CTX_S *ctx) {
    RK_S32 s32Ret = RK_SUCCESS;
    MB_BLK dstBlk = RK_NULL;
 
    test_tde_fill_src(ctx, pstSrc, pstSrcRect);
    test_tde_fill_dst(ctx, pstDst, pstDstRect);
 
    s32Ret = RK_MPI_SYS_MmzAlloc(&dstBlk, RK_NULL, RK_NULL, u32ImgSize);
    if (s32Ret != RK_SUCCESS) {
        return RK_FAILURE;
    }
 
    pstSrc->pMbBlk = srcBlk;
    pstDst->pMbBlk = dstBlk;
 
    return RK_SUCCESS;
}
 
RK_S32 test_tde_quick_resize_task(TDE_SURFACE_S *pstSrc,
                                      TDE_RECT_S *pstSrcRect,
                                      TDE_SURFACE_S *pstDst,
                                      TDE_RECT_S *pstDstRect,
                                      MB_BLK srcBlk,
                                      RK_U32 u32ImgSize,
                                      TEST_TDE_CTX_S *ctx) {
    RK_S32 s32Ret = RK_SUCCESS;
    MB_BLK dstBlk = RK_NULL;
 
    test_tde_fill_src(ctx, pstSrc, pstSrcRect);
    test_tde_fill_dst(ctx, pstDst, pstDstRect);
 
    s32Ret = RK_MPI_SYS_MmzAlloc(&dstBlk, RK_NULL, RK_NULL, u32ImgSize);
    if (s32Ret != RK_SUCCESS) {
        return RK_FAILURE;
    }
 
    pstSrc->pMbBlk = srcBlk;
    pstDst->pMbBlk = dstBlk;
 
    return RK_SUCCESS;
}
 
RK_S32 test_tde_quick_fill_task(TDE_SURFACE_S *pstDst,
                                   TDE_RECT_S  *pstDstRect,
                                   MB_BLK srcBlk,
                                   RK_U32 u32ImgSize,
                                   TEST_TDE_CTX_S *ctx) {
    RK_S32 s32Ret = RK_SUCCESS;
    MB_BLK dstBlk = RK_NULL;
 
    test_tde_fill_dst(ctx, pstDst, pstDstRect);
 
    s32Ret = RK_MPI_SYS_MmzAlloc(&dstBlk, RK_NULL, RK_NULL, u32ImgSize);
    if (s32Ret != RK_SUCCESS) {
        return RK_FAILURE;
    }
    memcpy(RK_MPI_MB_Handle2VirAddr(dstBlk),
           RK_MPI_MB_Handle2VirAddr(srcBlk),
           u32ImgSize);
 
    pstDst->pMbBlk = dstBlk;
 
    return RK_SUCCESS;
}
 
RK_S32 test_tde_rotate_task(TDE_SURFACE_S *pstSrc,
                                      TDE_RECT_S   *pstSrcRect,
                                      TDE_SURFACE_S *pstDst,
                                      TDE_RECT_S  *pstDstRect,
                                      MB_BLK srcBlk,
                                      RK_U32 u32ImgSize, TEST_TDE_CTX_S *ctx) {
    RK_S32 s32Ret = RK_SUCCESS;
    MB_BLK dstBlk = RK_NULL;
 
    test_tde_fill_src(ctx, pstSrc, pstSrcRect);
    test_tde_fill_dst(ctx, pstDst, pstDstRect);
 
    s32Ret = RK_MPI_SYS_MmzAlloc(&dstBlk, RK_NULL, RK_NULL, u32ImgSize);
    if (s32Ret != RK_SUCCESS) {
        return RK_FAILURE;
    }
 
    pstSrc->pMbBlk = srcBlk;
    pstDst->pMbBlk = dstBlk;
 
    return RK_SUCCESS;
}
 
RK_S32 test_tde_bit_blit_task(TDE_SURFACE_S *pstSrc,
                                TDE_RECT_S   *pstSrcRect,
                                TDE_SURFACE_S *pstDst,
                                TDE_RECT_S  *pstDstRect,
                                TDE_OPT_S   *stOpt,
                                MB_BLK srcBlk,
                                RK_U32 u32ImgSize, RK_S32 i, TEST_TDE_CTX_S *ctx) {
    RK_S32 s32Ret       = RK_SUCCESS;
    MB_BLK dstBlk       = RK_NULL;
    RK_U32 u32ReadSize  = 0;
    void  *pDstData     = RK_NULL;
 
    s32Ret = RK_MPI_SYS_MmzAlloc(&dstBlk, RK_NULL, RK_NULL, u32ImgSize);
    if (s32Ret != RK_SUCCESS) {
        return RK_FAILURE;
    }
 
    if (i == 0) {
        // test case : mirror and clip
        stOpt->enMirror = (MIRROR_E)ctx->s32Mirror;
        stOpt->stClipRect.s32Xpos = ctx->stSrcRect.s32X;
        stOpt->stClipRect.s32Ypos = ctx->stSrcRect.s32Y;
        stOpt->stClipRect.u32Width = ctx->stSrcRect.u32Width;
        stOpt->stClipRect.u32Height = ctx->stSrcRect.u32Height;
 
        test_tde_fill_src(ctx, pstSrc, pstSrcRect);
        pstSrc->pMbBlk = srcBlk;
 
        pstSrcRect->s32Xpos   = stOpt->stClipRect.s32Xpos;
        pstSrcRect->s32Ypos   = stOpt->stClipRect.s32Ypos;
        pstSrcRect->u32Width  = stOpt->stClipRect.u32Width;
        pstSrcRect->u32Height = stOpt->stClipRect.u32Height;
 
        test_tde_fill_dst(ctx, pstDst, pstDstRect);
        pstDst->pMbBlk = dstBlk;
 
        pstDstRect->s32Xpos   = ctx->stDstRect.s32X;
        pstDstRect->s32Ypos   = ctx->stDstRect.s32Y;
        pstDstRect->u32Width  = ctx->stDstRect.u32Width;
        pstDstRect->u32Height = ctx->stDstRect.u32Height;
    } else if (i == 1) {
        // test case : colorkey
        test_tde_fill_src(ctx, pstSrc, pstSrcRect);
        pstSrc->pMbBlk = srcBlk;
 
        pstSrcRect->s32Xpos   = ctx->stSrcRect.s32X;
        pstSrcRect->s32Ypos   = ctx->stSrcRect.s32Y;
        pstSrcRect->u32Width  = ctx->stSrcRect.u32Width;
        pstSrcRect->u32Height = ctx->stSrcRect.u32Height;
 
        test_tde_fill_dst(ctx, pstDst, pstDstRect);
        pstDst->pMbBlk = dstBlk;
 
        pstDstRect->s32Xpos   = ctx->stDstRect.s32X;
        pstDstRect->s32Ypos   = ctx->stDstRect.s32Y;
        pstDstRect->u32Width  = ctx->stDstRect.u32Width;
        pstDstRect->u32Height = ctx->stDstRect.u32Height;
 
        stOpt->unColorKeyValue = ctx->s32Color;
        stOpt->u32GlobalAlpha  = 0xff;
    }
    return RK_SUCCESS;
}
 
RK_S32 test_tde_add_all_task(TDE_HANDLE hHandle,
                                 TDE_RECT_S *pstDstRect,
                                 TDE_SURFACE_S *pstDst,
                                 RK_U32 u32ImgSize,
                                 RK_U32 *pu32TaskIndex,
                                 MB_BLK srcBlk,
                                 TEST_TDE_CTX_S *ctx) {
    RK_S32 s32Ret       = RK_SUCCESS;
    RK_U32 u32TaskIndex = *pu32TaskIndex;
    TDE_SURFACE_S pstSrc;
    TDE_RECT_S    pstSrcRect;
 
    for (RK_S32 i = 0; i < ctx->s32TaskNum; i++) {
        switch (ctx->s32Operation) {
          case TDE_OP_QUICK_COPY: {
            test_tde_quick_copy_task(&pstSrc,
                                     &pstSrcRect,
                                     &pstDst[u32TaskIndex],
                                     &pstDstRect[u32TaskIndex],
                                      srcBlk, u32ImgSize, ctx);
            s32Ret = RK_TDE_QuickCopy(hHandle, &pstSrc, &pstSrcRect,
                                      &pstDst[u32TaskIndex], &pstDstRect[u32TaskIndex]);
          } break;
          case TDE_OP_QUICK_RESIZE: {
            test_tde_quick_resize_task(&pstSrc,
                                   &pstSrcRect,
                                   &pstDst[u32TaskIndex],
                                   &pstDstRect[u32TaskIndex],
                                   srcBlk,
                                   u32ImgSize,
                                   ctx);
            s32Ret = RK_TDE_QuickResize(hHandle, &pstSrc, &pstSrcRect,
                                        &pstDst[u32TaskIndex], &pstDstRect[u32TaskIndex]);
          } break;
          case TDE_OP_QUICK_FILL: {
            test_tde_quick_fill_task(&pstDst[u32TaskIndex],
                                     &pstDstRect[u32TaskIndex],
                                     srcBlk,
                                     u32ImgSize,
                                     ctx);
            s32Ret = RK_TDE_QuickFill(hHandle, &pstDst[u32TaskIndex], &pstDstRect[u32TaskIndex],
                                      ctx->s32Color);
          } break;
          case TDE_OP_ROTATION: {
            ROTATION_E enRotateAngle = (ROTATION_E)ctx->s32Rotation;
            test_tde_rotate_task(&pstSrc,
                      &pstSrcRect,
                      &pstDst[u32TaskIndex],
                      &pstDstRect[u32TaskIndex],
                      srcBlk, u32ImgSize, ctx);
            s32Ret = RK_TDE_Rotate(hHandle, &pstSrc, &pstSrcRect,
                                   &pstDst[u32TaskIndex], &pstDstRect[u32TaskIndex],
                                   (ROTATION_E)ctx->s32Rotation);
          } break;
          case TDE_OP_MIRROR: {
            TDE_OPT_S stOpt;
            memset(&stOpt, 0, sizeof(TDE_OPT_S));
            test_tde_bit_blit_task(&pstSrc,
                                   &pstSrcRect,
                                   &pstDst[u32TaskIndex],
                                   &pstDstRect[u32TaskIndex],
                                   &stOpt, srcBlk, u32ImgSize, 0, ctx);
            s32Ret = RK_TDE_Bitblit(hHandle,
                                    &pstDst[u32TaskIndex],
                                    &pstDstRect[u32TaskIndex],
                                    &pstSrc,
                                    &pstSrcRect,
                                    &pstDst[u32TaskIndex],
                                    &pstDstRect[u32TaskIndex],
                                    &stOpt);
          } break;
          case TDE_OP_COLOR_KEY: {
            TDE_OPT_S stOpt;
            memset(&stOpt, 0, sizeof(TDE_OPT_S));
            test_tde_bit_blit_task(&pstSrc,
                                   &pstSrcRect,
                                   &pstDst[u32TaskIndex],
                                   &pstDstRect[u32TaskIndex],
                                   &stOpt, srcBlk, u32ImgSize, 1, ctx);
            s32Ret = RK_TDE_Bitblit(hHandle,
                                    &pstDst[u32TaskIndex],
                                    &pstDstRect[u32TaskIndex],
                                    &pstSrc,
                                    &pstSrcRect,
                                    &pstDst[u32TaskIndex],
                                    &pstDstRect[u32TaskIndex],
                                    &stOpt);
          } break;
          default: {
            RK_LOGE("unknown operation type %d", ctx->s32Operation);
            return RK_FAILURE;
          }
        }
        if (s32Ret != RK_SUCCESS) {
            RK_TDE_CancelJob(hHandle);
            return RK_FAILURE;
        }
        u32TaskIndex++;
    }
 
    *pu32TaskIndex = u32TaskIndex;
 
    return s32Ret;
}
 
RK_S32 test_tde_all_job(TEST_TDE_CTX_S *ctx) {
    RK_S32 s32Ret          = RK_SUCCESS;
    FILE  *file            = RK_NULL;
    void  *pSrcData        = RK_NULL;
    MB_BLK srcBlk          = RK_NULL;
    RK_U32 u32SrcSize      = 0;
    RK_U32 u32DstSize      = 0;
    RK_U32 u32TaskIndex    = 0;
    TDE_HANDLE hHandle[TDE_MAX_JOB_NUM]  = { 0 };
    TDE_SURFACE_S pstDst[TDE_MAX_TASK_NUM];
    TDE_RECT_S    pstDstRect[TDE_MAX_TASK_NUM];
    PIC_BUF_ATTR_S stPicBufAttr;
    MB_PIC_CAL_S stMbPicCalResult;
    u32SrcSize = unit_test_tde_get_size(ctx);
 
    stPicBufAttr.u32Width = ctx->stDstSurface.u32Width;
    stPicBufAttr.u32Height = ctx->stDstSurface.u32Height;
    stPicBufAttr.enPixelFormat = ctx->stDstSurface.enColorFmt;
    stPicBufAttr.enCompMode = COMPRESS_MODE_NONE;
    s32Ret = RK_MPI_CAL_TDE_GetPicBufferSize(&stPicBufAttr, &stMbPicCalResult);
    if (s32Ret != RK_SUCCESS) {
        RK_LOGE("get picture buffer size failed. err 0x%x", s32Ret);
        return s32Ret;
    }
    u32DstSize = stMbPicCalResult.u32MBSize;
 
    s32Ret = RK_MPI_SYS_MmzAlloc(&srcBlk, RK_NULL, RK_NULL, u32SrcSize);
    if (s32Ret != RK_SUCCESS) {
        return RK_FAILURE;
    }
    pSrcData = RK_MPI_MB_Handle2VirAddr(srcBlk);
    file = test_tde_read_file(ctx->srcFilePath, pSrcData, u32SrcSize, ctx);
    if (!file) {
        return RK_FAILURE;
    } else {
        fclose(file);
    }
    RK_U32 u32HorStride = RK_MPI_CAL_COMM_GetHorStride(ctx->u32SrcVirWidth,
                                                    ctx->stSrcSurface.enColorFmt);
    RK_U32 u32VerStride = ctx->u32SrcVirHeight;
    RK_MPI_MB_SetBufferStride(srcBlk, u32HorStride, u32VerStride);
    for (RK_S32 i = 0; i < ctx->s32JobNum; i++) {
        hHandle[i] = RK_TDE_BeginJob();
        if (RK_ERR_TDE_INVALID_HANDLE == hHandle[i]) {
            RK_LOGE("start job fail");
            return RK_FAILURE;
        }
 
        s32Ret = test_tde_add_all_task(hHandle[i], pstDstRect,
                                             pstDst, u32DstSize,
                                             &u32TaskIndex, srcBlk, ctx);
        if (s32Ret != RK_SUCCESS) {
            return RK_FAILURE;
        }
    }
    for (RK_S32 i = 0; i < ctx->s32JobNum; i++) {
        RK_U64 start = mpi_test_utils_get_now_us();
        s32Ret = RK_TDE_EndJob(hHandle[i], RK_FALSE, RK_TRUE, 10);
        if (s32Ret != RK_SUCCESS) {
            RK_TDE_CancelJob(hHandle[i]);
            return RK_FAILURE;
        }
 
        RK_TDE_WaitForDone(hHandle[i]);
        RK_U64 end = mpi_test_utils_get_now_us();
        if (ctx->bStatEn) {
            RK_LOGI("space time %lld us", end - start);
        }
 
        RK_TDE_CancelJob(hHandle[i]);
    }
    // save result
    for (RK_S32 i = 0; i < u32TaskIndex; i++) {
        MB_BLK dstBlk = pstDst[i].pMbBlk;
        if (ctx->dstFilePath) {
            s32Ret = test_tde_save_result(dstBlk, &(pstDst[i]), i, ctx);
            if (s32Ret != RK_SUCCESS) {
                return RK_FAILURE;
            }
        }
        if (dstBlk) {
            RK_MPI_SYS_Free(dstBlk);
        }
    }
 
    RK_MPI_SYS_Free(srcBlk);
    return RK_SUCCESS;
}
 
RK_S32 unit_test_mpi_tde(TEST_TDE_CTX_S *ctx) {
    RK_S32 s32Ret = RK_SUCCESS;
 
    s32Ret = RK_TDE_Open();
    if (s32Ret != RK_SUCCESS) {
        return RK_FAILURE;
    }
    for (RK_S32 i = 0; i < ctx->s32LoopCount; i++) {
        s32Ret = test_tde_all_job(ctx);
        if (s32Ret != RK_SUCCESS) {
            return s32Ret;
        }
        RK_LOGI("Running mpi tde test loop count %d.", i + 1);
    }
    RK_TDE_Close();
    return s32Ret;
}
 
static const char *const usages[] = {
    "./rk_mpi_tde_test [-i SRC_PATH] [-w SRC_WIDTH] [-h SRC_HEIGHT] [-W DST_WIDTH] [-H DST_HEIGHT]...",
    NULL,
};
 
static void mpi_tde_test_show_options(const TEST_TDE_CTX_S *ctx) {
    RK_PRINT("cmd parse result: \n");
    RK_PRINT("input  file name       : %s\n", ctx->srcFilePath);
    RK_PRINT("output file name       : %s\n", ctx->dstFilePath);
    RK_PRINT("loop count             : %d\n", ctx->s32LoopCount);
    RK_PRINT("job number             : %d\n", ctx->s32JobNum);
    RK_PRINT("task number            : %d\n", ctx->s32TaskNum);
    RK_PRINT("input width            : %d\n", ctx->stSrcSurface.u32Width);
    RK_PRINT("input height           : %d\n", ctx->stSrcSurface.u32Height);
    RK_PRINT("input vir width        : %d\n", ctx->u32SrcVirWidth);
    RK_PRINT("input vir height       : %d\n", ctx->u32SrcVirHeight);
    RK_PRINT("input pixel format     : %d\n", ctx->stSrcSurface.enColorFmt);
    RK_PRINT("output width           : %d\n", ctx->stDstSurface.u32Width);
    RK_PRINT("output height          : %d\n", ctx->stDstSurface.u32Height);
    RK_PRINT("output pixel format    : %d\n", ctx->stDstSurface.enColorFmt);
    RK_PRINT("operation type         : %d\n", ctx->s32Operation);
}
 
int main(int argc, const char **argv) {
    TEST_TDE_CTX_S ctx;
    RK_S32 s32Ret = RK_SUCCESS;
 
    memset(&ctx, 0, sizeof(TEST_TDE_CTX_S));
 
    //  set default params.
    ctx.dstFilePath     = RK_NULL;
    ctx.s32LoopCount    = 1;
    ctx.s32JobNum       = 1;
    ctx.s32TaskNum      = 1;
    ctx.s32Operation    = 0;
    ctx.stSrcSurface.enColorFmt = RK_FMT_YUV420SP;
    ctx.stDstSurface.enColorFmt = RK_FMT_YUV420SP;
 
    struct argparse_option options[] = {
        OPT_HELP(),
        OPT_GROUP("basic options:"),
        OPT_STRING('i', "input",  &(ctx.srcFilePath),
                    "input file name. e.g.(/userdata/1080p.nv12). <required>", NULL, 0, 0),
        OPT_INTEGER('w', "src_width", &(ctx.stSrcSurface.u32Width),
                    "src width. e.g.(1920). <required>", NULL, 0, 0),
        OPT_INTEGER('h', "src_height", &(ctx.stSrcSurface.u32Height),
                    "src height. e.g.(1080). <required>", NULL, 0, 0),
        OPT_INTEGER('\0', "src_vir_width", &(ctx.u32SrcVirWidth),
                    "src vir width. e.g.(1920).", NULL, 0, 0),
        OPT_INTEGER('\0', "src_vir_height", &(ctx.u32SrcVirHeight),
                    "src vir height. e.g.(1080).", NULL, 0, 0),
        OPT_INTEGER('W', "dst_width", &(ctx.stDstSurface.u32Width),
                    "dst width. e.g.(1920). <required>", NULL, 0, 0),
        OPT_INTEGER('H', "dst_height", &(ctx.stDstSurface.u32Height),
                    "dst height. e.g.(1080). <required>", NULL, 0, 0),
        OPT_STRING('o', "output", &(ctx.dstFilePath),
                    "output file path. e.g.(/userdata/tde/). default(NULL).", NULL, 0, 0),
        OPT_INTEGER('n', "loop_count", &(ctx.s32LoopCount),
                    "loop running count. can be any count. default(1)", NULL, 0, 0),
        OPT_INTEGER('j', "job_number", &(ctx.s32JobNum),
                    "the job number of vgs. default(1).", NULL, 0, 0),
        OPT_INTEGER('t', "task_number", &(ctx.s32TaskNum),
                    "the task number of one job. default(1).", NULL, 0, 0),
        OPT_INTEGER('f', "src_format", &(ctx.stSrcSurface.enColorFmt),
                    "src pixel format. default(0. 0 is NV12).", NULL, 0, 0),
        OPT_INTEGER('F', "dst_format", &(ctx.stDstSurface.enColorFmt),
                    "dst pixel format. default(0. 0 is NV12).", NULL, 0, 0),
        OPT_INTEGER('p', "operation", &(ctx.s32Operation),
                    "operation type. default(0).\n\t\t\t\t\t0: quick copy.\n\t\t\t\t\t1: quick resize."
                     "\n\t\t\t\t\t2: quick fill.\n\t\t\t\t\t3: rotation.\n\t\t\t\t\t4: mirror and flip."
                     "\n\t\t\t\t\t5: colorkey.", NULL, 0, 0),
        OPT_INTEGER('\0', "src_rect_x", &(ctx.stSrcRect.s32X),
                    "src rect x. default(0).", NULL, 0, 0),
        OPT_INTEGER('\0', "src_rect_y", &(ctx.stSrcRect.s32Y),
                    "src rect y. default(0).", NULL, 0, 0),
        OPT_INTEGER('\0', "src_rect_w", &(ctx.stSrcRect.u32Width),
                    "src rect width. default(src_width).", NULL, 0, 0),
        OPT_INTEGER('\0', "src_rect_h", &(ctx.stSrcRect.u32Height),
                    "src rect height. default(src_height).", NULL, 0, 0),
        OPT_INTEGER('\0', "dst_rect_x", &(ctx.stDstRect.s32X),
                    "dst rect x. default(0).", NULL, 0, 0),
        OPT_INTEGER('\0', "dst_rect_y", &(ctx.stDstRect.s32Y),
                    "dst rect y. default(0).", NULL, 0, 0),
        OPT_INTEGER('\0', "dst_rect_w", &(ctx.stDstRect.u32Width),
                    "dst rect width. default(dst_width).", NULL, 0, 0),
        OPT_INTEGER('\0', "dst_rect_h", &(ctx.stDstRect.u32Height),
                    "dst rect height. default(dst_height).", NULL, 0, 0),
        OPT_INTEGER('c', "fill color", &(ctx.s32Color),
                    "fill color. default(0).", NULL, 0, 0),
        OPT_INTEGER('r', "rotation", &(ctx.s32Rotation),
                    "rotation angle. default(0). 0: 0. 1: 90. 2: 180. 3: 270", NULL, 0, 0),
        OPT_INTEGER('m', "mirror", &(ctx.s32Mirror),
                    "mirror or flip. default(0). 0: none. 1: flip. 2: mirror. 3: both", NULL, 0, 0),
        OPT_INTEGER('s', "statistics", &(ctx.bStatEn),
                    "statistics open or not.", NULL, 0, 0),
        OPT_END(),
    };
 
    struct argparse argparse;
    argparse_init(&argparse, options, usages, 0);
    argparse_describe(&argparse, "\nselect a test case to run.",
                                 "\nuse --help for details.");
 
    argc = argparse_parse(&argparse, argc, argv);
 
    if (ctx.stSrcRect.u32Width == 0) {
        ctx.stSrcRect.u32Width = ctx.stSrcSurface.u32Width;
    }
    if (ctx.stSrcRect.u32Height == 0) {
        ctx.stSrcRect.u32Height = ctx.stSrcSurface.u32Height;
    }
    if (ctx.stDstRect.u32Width == 0) {
        ctx.stDstRect.u32Width = ctx.stDstSurface.u32Width;
    }
    if (ctx.stDstRect.u32Height == 0) {
        ctx.stDstRect.u32Height = ctx.stDstSurface.u32Height;
    }
 
    mpi_tde_test_show_options(&ctx);
 
    if (ctx.srcFilePath == RK_NULL
        || ctx.stSrcSurface.u32Width <= 0
        || ctx.stSrcSurface.u32Height <= 0
        || ctx.stDstSurface.u32Width <= 0
        || ctx.stDstSurface.u32Height <= 0) {
        argparse_usage(&argparse);
        goto __FAILED;
    }
 
    s32Ret = RK_MPI_SYS_Init();
    if (s32Ret != RK_SUCCESS) {
        goto __FAILED;
    }
 
    s32Ret = unit_test_mpi_tde(&ctx);
    if (s32Ret != RK_SUCCESS) {
        goto __FAILED;
    }
 
    s32Ret = RK_MPI_SYS_Exit();
    if (s32Ret != RK_SUCCESS) {
        goto __FAILED;
    }
 
 
    RK_LOGI("test running ok!");
    return 0;
__FAILED:
    RK_LOGI("test runnning failed!");
    return -1;
}