-
Notifications
You must be signed in to change notification settings - Fork 7
/
decode_stream.c
executable file
·1584 lines (1434 loc) · 39.6 KB
/
decode_stream.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
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2010-2014, Freescale Semiconductor Inc.,
* Copyright 2019-2020 NXP
*
* The following programs are the sole property of NXP,
* and contain its proprietary and confidential information.
*
*/
/*
* decode_stream.c
* this file is the interface between unit test and vpu wrapper
*
* History :
* Date (y.m.d) Author Version Description
* 2010-09-14 eagle zhou 0.1 Created
*/
#include "stdio.h"
#include "stdlib.h"
#include "string.h"
#include "vpu_wrapper.h"
#include "decode_stream.h"
#include "fb_render.h"
#include "unistd.h" //for usleep()
//#define VPU_FILE_MODE_TEST
#define CHECK_DEAD_LOOP
#ifdef CHECK_DEAD_LOOP
#define MAX_NOTUSED_LOOP (1000) //(200)
#define MAX_NULL_LOOP (1000) //(200)
#endif
#ifdef DEC_STREAM_DEBUG
#define DEC_STREAM_PRINTF printf
//#define DEC_TRACE printf("%s: %d \r\n",__FUNCTION__,__LINE__);
#define DEC_TRACE
#else
#define DEC_STREAM_PRINTF
#define DEC_TRACE
#endif
#define USLEEP usleep
#define SLEEP_TIME_US (1000) //(1000)
//#define FILL_DATA_UNIT (16*1024)
#define MAX_FRAME_NUM (30)
#define FRAME_SURPLUS (0)//(3)
#define FRAME_ALIGN (16)
#if 1 //avoid buf is too big to malloc by vpu
#define VPU_DEC_MAX_NUM_MEM_NUM 20
#else
#define VPU_DEC_MAX_NUM_MEM_NUM VPU_DEC_MAX_NUM_MEM_REQS
#endif
#define Align(ptr,align) (((unsigned long)ptr+(align)-1)/(align)*(align))
typedef struct
{
//virtual mem info
int nVirtNum;
unsigned long virtMem[VPU_DEC_MAX_NUM_MEM_NUM];
//phy mem info
int nPhyNum;
unsigned long phyMem_virtAddr[VPU_DEC_MAX_NUM_MEM_NUM];
unsigned long phyMem_phyAddr[VPU_DEC_MAX_NUM_MEM_NUM];
unsigned long phyMem_cpuAddr[VPU_DEC_MAX_NUM_MEM_NUM];
unsigned int phyMem_size[VPU_DEC_MAX_NUM_MEM_NUM];
}DecMemInfo;
#ifdef VPU_FILE_MODE_TEST
#define FILE_MODE_MAX_FRAME_LEN (1024*1024) //1M bytes
#define FILE_MODE_MAX_FRAME_NUM 40
#define FILE_MODE_LOG printf
unsigned int g_filemode_frame_length[FILE_MODE_MAX_FRAME_NUM]={
//from zdfhd.ts
131923, 40129,38102,40147,20749,
19142,36248,21235,21153,35797,
33383,21124,19286,38835,18808,
20024,103980,47883,43615,39538,
19854,24160,45832,22873,23503,
41762,36254,24498,20684,35544,
16691,18975,145114,43136,39354,
33491,15813,15431,33030,16289,
};
static unsigned int g_filemode_curloc=0;
#endif
#if 1 // timer related part
#include <sys/time.h>
#define TIME_DEC_ID 0
#define TIME_TOTAL_ID 1
#define TIME_RESOLUTION_ID 2
static struct timeval time_beg[3];
static struct timeval time_end[3];
static unsigned long long total_time[3];
static void time_init(int id)
{
total_time[id]=0;
}
static void time_start(int id)
{
gettimeofday(&time_beg[id], 0);
}
static void time_stop(int id)
{
unsigned int tm_1, tm_2;
gettimeofday(&time_end[id], 0);
tm_1 = time_beg[id].tv_sec * 1000000 + time_beg[id].tv_usec;
tm_2 = time_end[id].tv_sec * 1000000 + time_end[id].tv_usec;
total_time[id] = total_time[id] + (tm_2-tm_1);
}
static unsigned long long time_report(int id)
{
return total_time[id];
}
#endif
int MapTileSpace(int x, int y, int stride, int blockW, int blockH,int field,unsigned char* pTop, unsigned char* pBot, unsigned char** ppTileAddr)
{
/* input parameter:
x: x coordinate in linear space
y: y coordinate in linear space
blockW: tile block unit width
blockH: tile block unit height
output parameter:
pTileOffset: offset in the whole tile buffer
in tile space: every [blockW x blockH] is divided by two sub-block: [blockW/2 x blockH]
*/
int blockNum=0;
int subblockNum=0;
int blockLength;
int subblockLength;
int subblockOffset;
int tileOffset=0;
unsigned char* pBase;
if(0==field)
{
pBase=pTop;
}
else
{
if(0==(y&1))
{
pBase=pTop;
}
else
{
pBase=pBot;
}
y=y>>1;
}
blockLength=blockW*blockH;
subblockLength=blockLength/2;
blockNum=(y/blockH)*(stride/blockW)+(x/blockW);
if((x%blockW)>=(blockW/2))
{
subblockNum=1;
}
subblockOffset=(y%blockH)*(blockW/2)+(x%(blockW/2));
tileOffset=blockNum*blockLength+subblockNum*subblockLength+subblockOffset;
//printf("field: %d, stride: %d, block[%dx%d], linear: [x,y]: [%d,%d], tile offset: %d \r\n",field,stride,blockW,blockH,x,y,tileOffset);
*ppTileAddr=pBase+tileOffset;
return 1;
}
int ConvertDataFromTileToLinear(int maptype, int width, int height,
unsigned char* pSrcYTop,unsigned char* pSrcYBot,unsigned char* pSrcCbTop,unsigned char* pSrcCbBot,
unsigned char* pDstY,unsigned char* pDstCb,unsigned char* pDstCr)
{
int x,y,j;
unsigned char* pTileBlockAddr;
unsigned char temp_buf[8];
int blockW,blockH;
int field=0;
//fill Y buffer
if(maptype==1)
{
//frame tile: luma--w(8+8)xh16
blockW=16;
blockH=16;
field=0;
}
else
{
//field tile: luma--w(8+8)xh8
blockW=16;
blockH=8;
field=1;
}
for(y=0;y<height;y++)
{
for(x=0;x<width;x+=8)
{
MapTileSpace(x, y, width, blockW, blockH,field,pSrcYTop,pSrcYBot, &pTileBlockAddr);
memcpy(pDstY+y*width+x, pTileBlockAddr, 8);
}
}
//fill Cb/Cr buffer
if(maptype==1)
{
//frame tile: Chroma--w(8+8)xh8
blockW=16;
blockH=8;
field=0;
}
else
{
//field tile: Chroma--w(8+8)xh4
blockW=16;
blockH=4;
field=1;
}
for(y=0;y<height/2;y++)
{
for(x=0;x<width;x+=8)
{
MapTileSpace(x, y, width, blockW, blockH,field,pSrcCbTop,pSrcCbBot,&pTileBlockAddr);
memcpy(temp_buf, pTileBlockAddr, 8);
for (j = 0; j < 4; j++)
{
*pDstCb++=*(temp_buf+j*2);
*pDstCr++=*(temp_buf+j*2+1);
}
}
}
return 1;
}
int ConvertCodecFormat(int codec, VpuCodStd* pCodec)
{
switch (codec)
{
case 1:
*pCodec=VPU_V_MPEG2;
break;
case 2:
*pCodec=VPU_V_MPEG4;
break;
case 3:
*pCodec=VPU_V_DIVX3;
break;
case 4:
*pCodec=VPU_V_DIVX4;
break;
case 5:
*pCodec=VPU_V_DIVX56;
break;
case 6:
*pCodec=VPU_V_XVID;
break;
case 7:
*pCodec=VPU_V_H263;
break;
case 8:
*pCodec=VPU_V_AVC;
break;
case 9:
*pCodec=VPU_V_VC1; //VPU_V_VC1_AP
break;
case 10:
*pCodec=VPU_V_RV;
break;
case 11:
*pCodec=VPU_V_MJPG;
break;
case 12:
*pCodec=VPU_V_AVS;
break;
case 13:
*pCodec=VPU_V_VP8;
break;
case 14:
*pCodec=VPU_V_AVC_MVC;
break;
case 15:
*pCodec=VPU_V_HEVC;
break;
default:
return 0;
}
return 1;
}
int ConvertSkipMode(int skip, VpuDecConfig* pConfig,int * pPara)
{
switch (skip)
{
case 0:
*pConfig=VPU_DEC_CONF_SKIPMODE;
*pPara=VPU_DEC_SKIPNONE;
DEC_STREAM_PRINTF("normal mode \r\n");
break;
case 1:
*pConfig=VPU_DEC_CONF_SKIPMODE;
*pPara=VPU_DEC_SKIPPB;
DEC_STREAM_PRINTF("skip PB frames \r\n");
break;
case 2:
*pConfig=VPU_DEC_CONF_SKIPMODE;
*pPara=VPU_DEC_SKIPB;
DEC_STREAM_PRINTF("skip B frames \r\n");
break;
case 3:
*pConfig=VPU_DEC_CONF_SKIPMODE;
*pPara=VPU_DEC_SKIPALL;
DEC_STREAM_PRINTF("skip all frames \r\n");
break;
case 4:
*pConfig=VPU_DEC_CONF_SKIPMODE;
*pPara=VPU_DEC_ISEARCH; //only search I, not skip I
DEC_STREAM_PRINTF("I frame search \r\n");
break;
default:
DEC_STREAM_PRINTF("unsupported skip mode: %d \r\n",skip);
return 0;
}
return 1;
}
int FreeMemBlockFrame(DecMemInfo* pDecMem, int nFrmNum)
{
VpuMemDesc vpuMem;
VpuDecRetCode vpuRet;
int cnt=0;
int retOk=1;
int i;
//free physical mem
for(i=pDecMem->nPhyNum-1;i>=0;i--)
{
vpuMem.nPhyAddr=pDecMem->phyMem_phyAddr[i];
vpuMem.nVirtAddr=pDecMem->phyMem_virtAddr[i];
vpuMem.nCpuAddr=pDecMem->phyMem_cpuAddr[i];
vpuMem.nSize=pDecMem->phyMem_size[i];
vpuRet=VPU_DecFreeMem(&vpuMem);
if(vpuRet!=VPU_DEC_RET_SUCCESS)
{
DEC_STREAM_PRINTF("%s: free vpu memory failure : ret=%d \r\n",__FUNCTION__,vpuRet);
retOk=0;
}
cnt++;
if(cnt==nFrmNum) break;
}
pDecMem->nPhyNum=pDecMem->nPhyNum-cnt;
if(cnt!=nFrmNum)
{
DEC_STREAM_PRINTF("error: only freed %d frames, required frame numbers: %d \r\n",cnt,nFrmNum);
retOk=0;
}
return retOk;
}
int FreeMemBlock(DecMemInfo* pDecMem)
{
int i;
VpuMemDesc vpuMem;
VpuDecRetCode vpuRet;
int retOk=1;
//free virtual mem
for(i=0;i<pDecMem->nVirtNum;i++)
{
if((void*)pDecMem->virtMem[i]) free((void*)pDecMem->virtMem[i]);
}
pDecMem->nVirtNum=0;
//free physical mem
for(i=0;i<pDecMem->nPhyNum;i++)
{
vpuMem.nPhyAddr=pDecMem->phyMem_phyAddr[i];
vpuMem.nVirtAddr=pDecMem->phyMem_virtAddr[i];
vpuMem.nCpuAddr=pDecMem->phyMem_cpuAddr[i];
vpuMem.nSize=pDecMem->phyMem_size[i];
vpuRet=VPU_DecFreeMem(&vpuMem);
if(vpuRet!=VPU_DEC_RET_SUCCESS)
{
DEC_STREAM_PRINTF("%s: free vpu memory failure : ret=%d \r\n",__FUNCTION__,vpuRet);
retOk=0;
}
}
pDecMem->nPhyNum =0;
return retOk;
}
int MallocMemBlock(VpuMemInfo* pMemBlock,DecMemInfo* pDecMem)
{
int i;
unsigned char * ptr=NULL;
int size;
for(i=0;i<pMemBlock->nSubBlockNum;i++)
{
size=pMemBlock->MemSubBlock[i].nAlignment+pMemBlock->MemSubBlock[i].nSize;
if(pMemBlock->MemSubBlock[i].MemType==VPU_MEM_VIRT)
{
ptr=(unsigned char *)malloc(size);
if(ptr==NULL)
{
DEC_STREAM_PRINTF("%s: get virtual memory failure, size=%d \r\n",__FUNCTION__,size);
goto failure;
}
pMemBlock->MemSubBlock[i].pVirtAddr=(unsigned char*)Align(ptr,pMemBlock->MemSubBlock[i].nAlignment);
//record virtual base addr
pDecMem->virtMem[pDecMem->nVirtNum]=(unsigned long)ptr;
pDecMem->nVirtNum++;
}
else// if(memInfo.MemSubBlock[i].MemType==VPU_MEM_PHY)
{
VpuMemDesc vpuMem = {0};
VpuDecRetCode ret;
vpuMem.nSize=size;
ret=VPU_DecGetMem(&vpuMem);
if(ret!=VPU_DEC_RET_SUCCESS)
{
DEC_STREAM_PRINTF("%s: get vpu memory failure, size=%d, ret=%d \r\n",__FUNCTION__,size,ret);
goto failure;
}
pMemBlock->MemSubBlock[i].pVirtAddr=(unsigned char*)Align(vpuMem.nVirtAddr,pMemBlock->MemSubBlock[i].nAlignment);
pMemBlock->MemSubBlock[i].pPhyAddr=(unsigned char*)Align(vpuMem.nPhyAddr,pMemBlock->MemSubBlock[i].nAlignment);
//record physical base addr
pDecMem->phyMem_phyAddr[pDecMem->nPhyNum]=(unsigned long)vpuMem.nPhyAddr;
pDecMem->phyMem_virtAddr[pDecMem->nPhyNum]=(unsigned long)vpuMem.nVirtAddr;
pDecMem->phyMem_cpuAddr[pDecMem->nPhyNum]=(unsigned long)vpuMem.nCpuAddr;
pDecMem->phyMem_size[pDecMem->nPhyNum]=size;
pDecMem->nPhyNum++;
}
}
return 1;
failure:
FreeMemBlock(pDecMem);
return 0;
}
int ResetBitstream(DecContxt * pDecContxt,int offset)
{
fseek(pDecContxt->fin,offset,SEEK_SET);
return 1;
}
int ReadBitstream(DecContxt * pDecContxt, unsigned char* pBitstream,int length)
{
int readbytes;
//static int totalReadSize=0;
//DEC_STREAM_PRINTF("read %d bytes \r\n",length);
readbytes=fread(pBitstream,1,length,pDecContxt->fin);
//totalReadSize+=readbytes;
//printf("total read size: %d \r\n",totalReadSize);
return readbytes;
}
int ReadCodecData(DecContxt * pDecContxt, unsigned char* pCodecData,int length)
{
int readbytes;
//static int totalReadSize=0;
//DEC_STREAM_PRINTF("read %d bytes \r\n",length);
readbytes=fread(pCodecData,1,length,pDecContxt->fcodecdata);
//totalReadSize+=readbytes;
//printf("total read size: %d \r\n",totalReadSize);
return readbytes;
}
int OutputFrame(DecContxt * pDecContxt,VpuDecOutFrameInfo* pOutFrame,int width, int height,int fbHandle,int frameNum)
{
int ySize;
int uvSize;
VpuFrameBuffer* pFrame=pOutFrame->pDisplayFrameBuf;
//VpuCodStd codecFormat;
VpuFrameBuffer sLinearFrame;
VpuMemDesc vpuMem;
int NeedConvert=0;
int wrlen;
/*DEC_STREAM_PRINTF("dynamic resolution: [width x height]=[%d x %d]: crop: [left, top, right, bottom]=[%d, %d, %d, %d] \r\n",
pOutFrame->pExtInfo->nFrmWidth,pOutFrame->pExtInfo->nFrmHeight,
pOutFrame->pExtInfo->FrmCropRect.nLeft,pOutFrame->pExtInfo->FrmCropRect.nTop,
pOutFrame->pExtInfo->FrmCropRect.nRight,pOutFrame->pExtInfo->FrmCropRect.nBottom);*/
//DEC_STREAM_PRINTF("output one frame, [width x height]=[%d x %d] \r\n",width,height);
//DEC_STREAM_PRINTF("output one frame, y:0x%X, u:0x%X, v:0x%X \r\n",(unsigned int)pFrame->pbufVirtY,(unsigned int)pFrame->pbufVirtCb,(unsigned int)pFrame->pbufVirtCr);
ySize=Align(width, FRAME_ALIGN)*Align(height,FRAME_ALIGN);
switch(pDecContxt->eOutColorFmt)
{
case DEC_OUT_420:
uvSize=ySize/4;
break;
case DEC_OUT_422H:
case DEC_OUT_422V:
uvSize=ySize/2;
break;
case DEC_OUT_444:
uvSize=ySize;
break;
case DEC_OUT_400:
uvSize=0;
break;
default:
uvSize=ySize/4;
break;
}
if((pDecContxt->nMapType!=0) && (pDecContxt->nTile2LinearEnable==0)
&& (pDecContxt->fout || fbHandle))
{
NeedConvert=1;
}
if(NeedConvert)
{
VpuDecRetCode ret;
memset(&vpuMem,0,sizeof(VpuMemDesc));
memset(&sLinearFrame,0,sizeof(VpuFrameBuffer));
vpuMem.nSize=ySize+uvSize+uvSize;
ret=VPU_DecGetMem(&vpuMem);
if(VPU_DEC_RET_SUCCESS!=ret)
{
DEC_STREAM_PRINTF("%s: vpu malloc tile buf failure: ret=%d, size: %d \r\n",__FUNCTION__,ret,vpuMem.nSize);
goto FAIL;
}
#if 1
if(pDecContxt->fout)
{
//for debug: output nv12 tile file
FILE* fp;
fp = fopen("temp_tile.tile", "ab");
if(pDecContxt->nMapType==2) // tile field
{
wrlen=fwrite(pFrame->pbufVirtY,1,ySize/2,fp);
wrlen=fwrite(pFrame->pbufVirtY_tilebot,1,ySize/2,fp);
wrlen=fwrite(pFrame->pbufVirtCb,1,uvSize,fp);
wrlen=fwrite(pFrame->pbufVirtCb_tilebot,1,uvSize,fp);
}
else // tile frame
{
wrlen=fwrite(pFrame->pbufVirtY,1,ySize,fp);
wrlen=fwrite(pFrame->pbufVirtCb,1,2*uvSize,fp);
//fwrite(pFrame->pbufVirtCr,1,uvSize,fp);
}
fclose(fp);
}
#endif
//FIXME: now only care pbufVirtY/Cb/Cr three address
sLinearFrame.pbufVirtY=(unsigned char*)vpuMem.nVirtAddr;
sLinearFrame.pbufVirtCb=sLinearFrame.pbufVirtY+ySize;
sLinearFrame.pbufVirtCr=sLinearFrame.pbufVirtCb+uvSize;
//printf("convert tile space: YTob: 0x%X, YBot: 0x%X, CbTop: 0x%X, CbBot: 0x%X \r\n",pFrame->pbufVirtY, pFrame->pbufVirtY_tilebot, pFrame->pbufVirtCb, pFrame->pbufVirtCb_tilebot);
ConvertDataFromTileToLinear(pDecContxt->nMapType, width, height,
pFrame->pbufVirtY, pFrame->pbufVirtY_tilebot, pFrame->pbufVirtCb, pFrame->pbufVirtCb_tilebot,
sLinearFrame.pbufVirtY,sLinearFrame.pbufVirtCb, sLinearFrame.pbufVirtCr);
pFrame=&sLinearFrame;
}
DEC_TRACE;
//output file
if(pDecContxt->fout)
{
wrlen=fwrite(pFrame->pbufVirtY,1,ySize,pDecContxt->fout);
wrlen=fwrite(pFrame->pbufVirtCb,1,uvSize,pDecContxt->fout);
wrlen=fwrite(pFrame->pbufVirtCr,1,uvSize,pDecContxt->fout);
}
DEC_TRACE;
//display
if(fbHandle)
{
fb_render_drawYUVframe(fbHandle, pFrame->pbufVirtY, pFrame->pbufVirtCb, pFrame->pbufVirtCr, Align(width, FRAME_ALIGN), Align(height,FRAME_ALIGN));
}
//ConvertCodecFormat(pDecContxt->nCodec, &codecFormat);
switch(pOutFrame->ePicType)
{
case VPU_I_PIC:
DEC_STREAM_PRINTF("frame : %d (I) \r\n",frameNum);
break;
case VPU_P_PIC:
DEC_STREAM_PRINTF("frame : %d (P) \r\n",frameNum);
break;
case VPU_B_PIC:
DEC_STREAM_PRINTF("frame : %d (B) \r\n",frameNum);
break;
case VPU_BI_PIC:
DEC_STREAM_PRINTF("frame : %d (BI) \r\n",frameNum);
break;
case VPU_IDR_PIC:
DEC_STREAM_PRINTF("frame : %d (IDR) \r\n",frameNum);
break;
case VPU_SKIP_PIC:
DEC_STREAM_PRINTF("frame : %d (SKIP) \r\n",frameNum);
break;
default:
DEC_STREAM_PRINTF("frame : %d (*) \r\n",frameNum);
break;
}
if(NeedConvert)
{
VPU_DecFreeMem(&vpuMem);
}
return 1;
FAIL:
return 0;
}
int RenderInit(DecContxt * pDecContxt,VpuDecInitInfo* pInitInfo,int* pFbHandle)
{
int ipu_ret=0;
//1 only support 4:2:0 !!!
//init dispaly device
if(*pFbHandle)
{
//release before re-init
fb_render_uninit(*pFbHandle);
}
if(0)//if(InitInfo.nInterlace)
{
ipu_ret=fb_render_init(pFbHandle, pDecContxt->nFbNo, Align(pInitInfo->nPicWidth,FRAME_ALIGN),Align(pInitInfo->nPicHeight,2*FRAME_ALIGN));
}
else
{
ipu_ret=fb_render_init(pFbHandle, pDecContxt->nFbNo, Align(pInitInfo->nPicWidth,FRAME_ALIGN),Align(pInitInfo->nPicHeight,FRAME_ALIGN));
}
if(0==ipu_ret)
{
DEC_STREAM_PRINTF("%s: init fb render failure: \r\n",__FUNCTION__);
//err=1;
//break;
*pFbHandle=(int)NULL;
}
return 1;
}
int ProcessInitInfo(DecContxt * pDecContxt,VpuDecHandle handle,VpuDecInitInfo* pInitInfo,DecMemInfo* pDecMemInfo, int*pOutFrmNum)
{
VpuDecRetCode ret;
VpuFrameBuffer frameBuf[MAX_FRAME_NUM];
VpuMemDesc vpuMem;
int BufNum;
int i;
int totalSize=0;
int mvSize=0;
int ySize=0;
int uSize=0;
int vSize=0;
int yStride=0;
int uStride=0;
int vStride=0;
unsigned char* ptr;
unsigned char* ptrVirt;
int nAlign;
int multifactor=1;
//get init info
DEC_TRACE;
ret=VPU_DecGetInitialInfo(handle, pInitInfo);
DEC_TRACE;
if(VPU_DEC_RET_SUCCESS!=ret)
{
DEC_STREAM_PRINTF("%s: vpu get init info failure: ret=%d \r\n",__FUNCTION__,ret);
return 0;
}
//malloc frame buffs
BufNum=pInitInfo->nMinFrameBufferCount+FRAME_SURPLUS;
if(BufNum>MAX_FRAME_NUM)
{
DEC_STREAM_PRINTF("%s: vpu request too many frames : num=0x%X \r\n",__FUNCTION__,pInitInfo->nMinFrameBufferCount);
return 0;
}
yStride=Align(pInitInfo->nPicWidth,FRAME_ALIGN);
if(pInitInfo->nInterlace)
{
ySize=Align(pInitInfo->nPicWidth,FRAME_ALIGN)*Align(pInitInfo->nPicHeight,(2*FRAME_ALIGN));
}
else
{
ySize=Align(pInitInfo->nPicWidth,FRAME_ALIGN)*Align(pInitInfo->nPicHeight,FRAME_ALIGN);
}
#ifdef ILLEGAL_MEMORY_DEBUG
//in such debug case, we always allocate big enough frame buffers
DEC_STREAM_PRINTF("enable illegal memory detect, buffer size: 1920*(1088+64) \r\n");
ySize=1920*(1088+64);
#endif
//for MJPG: we need to check 4:4:4/4:2:2/4:2:0/4:0:0
{
VpuCodStd vpuCodec=0;
ConvertCodecFormat(pDecContxt->nCodec, &vpuCodec);
if(VPU_V_MJPG==vpuCodec)
{
switch(pInitInfo->nMjpgSourceFormat)
{
case 0: //4:2:0
DEC_STREAM_PRINTF("MJPG: 4:2:0 \r\n");
uStride=yStride/2;
vStride=uStride;
uSize=ySize/4;
vSize=uSize;
mvSize=uSize;
pDecContxt->eOutColorFmt=DEC_OUT_420;
break;
case 1: //4:2:2 hor
DEC_STREAM_PRINTF("MJPG: 4:2:2 hor \r\n");
uStride=yStride/2;
vStride=uStride;
uSize=ySize/2;
vSize=uSize;
mvSize=uSize;
pDecContxt->eOutColorFmt=DEC_OUT_422H;
break;
case 2: //4:2:2 ver
DEC_STREAM_PRINTF("MJPG: 4:2:2 ver \r\n");
uStride=yStride;
vStride=uStride;
uSize=ySize/2;
vSize=uSize;
mvSize=uSize;
pDecContxt->eOutColorFmt=DEC_OUT_422V;
break;
case 3: //4:4:4
DEC_STREAM_PRINTF("MJPG: 4:4:4 \r\n");
uStride=yStride;
vStride=uStride;
uSize=ySize;
vSize=uSize;
mvSize=uSize;
pDecContxt->eOutColorFmt=DEC_OUT_444;
break;
case 4: //4:0:0
DEC_STREAM_PRINTF("MJPG: 4:0:0 \r\n");
uStride=0;
vStride=uStride;
uSize=0;
vSize=uSize;
mvSize=uSize;
pDecContxt->eOutColorFmt=DEC_OUT_400;
break;
default: //4:2:0
DEC_STREAM_PRINTF("unknown color format: %d \r\n",vpuCodec);
uStride=yStride/2;
vStride=uStride;
uSize=ySize/4;
vSize=uSize;
mvSize=uSize;
pDecContxt->eOutColorFmt=DEC_OUT_420;
break;
}
}
else
{
//4:2:0 for all video
uStride=yStride/2;
vStride=uStride;
uSize=ySize/4;
vSize=uSize;
mvSize=uSize;
pDecContxt->eOutColorFmt=DEC_OUT_420;
}
}
nAlign=pInitInfo->nAddressAlignment;
if(pDecContxt->nMapType==2)
{
//only consider Y since interleave must be enabled
multifactor=2; //for field, we need to consider alignment for top and bot
}
if(nAlign>1)
{
ySize=Align(ySize,multifactor*nAlign);
uSize=Align(uSize,nAlign);
vSize=Align(vSize,nAlign);
}
#if 1 // avoid buffer is too big to malloc by vpu
for(i=0;i<BufNum;i++)
{
totalSize=(ySize+uSize+vSize+mvSize+nAlign)*1;
vpuMem.nSize=totalSize;
DEC_TRACE;
ret=VPU_DecGetMem(&vpuMem);
DEC_TRACE;
if(VPU_DEC_RET_SUCCESS!=ret)
{
DEC_STREAM_PRINTF("%s: vpu malloc frame buf failure: ret=%d \r\n",__FUNCTION__,ret);
return 0;
}
//record memory info for release
pDecMemInfo->phyMem_phyAddr[pDecMemInfo->nPhyNum]=vpuMem.nPhyAddr;
pDecMemInfo->phyMem_virtAddr[pDecMemInfo->nPhyNum]=vpuMem.nVirtAddr;
pDecMemInfo->phyMem_cpuAddr[pDecMemInfo->nPhyNum]=vpuMem.nCpuAddr;
pDecMemInfo->phyMem_size[pDecMemInfo->nPhyNum]=vpuMem.nSize;
pDecMemInfo->nPhyNum++;
//fill frameBuf
ptr=(unsigned char*)vpuMem.nPhyAddr;
ptrVirt=(unsigned char*)vpuMem.nVirtAddr;
/*align the base address*/
if(nAlign>1)
{
ptr=(unsigned char*)Align(ptr,nAlign);
ptrVirt=(unsigned char*)Align(ptrVirt,nAlign);
}
/* fill stride info */
frameBuf[i].nStrideY=yStride;
frameBuf[i].nStrideC=uStride;
/* fill phy addr*/
frameBuf[i].pbufY=ptr;
frameBuf[i].pbufCb=ptr+ySize;
frameBuf[i].pbufCr=ptr+ySize+uSize;
frameBuf[i].pbufMvCol=ptr+ySize+uSize+vSize;
//ptr+=ySize+uSize+vSize+mvSize;
/* fill virt addr */
frameBuf[i].pbufVirtY=ptrVirt;
frameBuf[i].pbufVirtCb=ptrVirt+ySize;
frameBuf[i].pbufVirtCr=ptrVirt+ySize+uSize;
frameBuf[i].pbufVirtMvCol=ptrVirt+ySize+uSize+vSize;
//ptrVirt+=ySize+uSize+vSize+mvSize;
#ifdef ILLEGAL_MEMORY_DEBUG
memset(frameBuf[i].pbufVirtY,0,ySize);
memset(frameBuf[i].pbufVirtCb,0,uSize);
memset(frameBuf[i].pbufVirtCr,0,uSize);
#endif
/* fill bottom address for field tile*/
if(pDecContxt->nMapType==2)
{
frameBuf[i].pbufY_tilebot=frameBuf[i].pbufY+ySize/2;
frameBuf[i].pbufCb_tilebot=frameBuf[i].pbufCr;
frameBuf[i].pbufVirtY_tilebot=frameBuf[i].pbufVirtY+ySize/2;
frameBuf[i].pbufVirtCb_tilebot=frameBuf[i].pbufVirtCr;
}
else
{
frameBuf[i].pbufY_tilebot=0;
frameBuf[i].pbufCb_tilebot=0;
frameBuf[i].pbufVirtY_tilebot=0;
frameBuf[i].pbufVirtCb_tilebot=0;
}
}
#else
mvSize=Align(mvSize,nAlign);
totalSize=(ySize+uSize+vSize+mvSize)*BufNum+nAlign;
vpuMem.nSize=totalSize;
DEC_TRACE;
ret=VPU_DecGetMem(&vpuMem);
DEC_TRACE;
if(VPU_DEC_RET_SUCCESS!=ret)
{
DEC_STREAM_PRINTF("%s: vpu malloc frame buf failure: ret=%d \r\n",__FUNCTION__,ret);
return 0;
}
//record memory info for release
pDecMemInfo->phyMem_phyAddr[pDecMemInfo->nPhyNum]=vpuMem.nPhyAddr;
pDecMemInfo->phyMem_virtAddr[pDecMemInfo->nPhyNum]=vpuMem.nVirtAddr;
pDecMemInfo->phyMem_cpuAddr[pDecMemInfo->nPhyNum]=vpuMem.nCpuAddr;
pDecMemInfo->phyMem_size[pDecMemInfo->nPhyNum]=vpuMem.nSize;
pDecMemInfo->nPhyNum++;
//fill frameBuf
ptr=(unsigned char*)vpuMem.nPhyAddr;
ptrVirt=(unsigned char*)vpuMem.nVirtAddr;
/*align the base address*/
if(nAlign>1)
{
ptr=(unsigned char*)Align(ptr,nAlign);
ptrVirt=(unsigned char*)Align(ptrVirt,nAlign);
}
for(i=0;i<BufNum;i++)
{
/* fill stride info */
frameBuf[i].nStrideY=yStride;
frameBuf[i].nStrideC=uStride;
/* fill phy addr*/
frameBuf[i].pbufY=ptr;
frameBuf[i].pbufCb=ptr+ySize;
frameBuf[i].pbufCr=ptr+ySize+uSize;
frameBuf[i].pbufMvCol=ptr+ySize+uSize+vSize;
ptr+=ySize+uSize+vSize+mvSize;
/* fill virt addr */
frameBuf[i].pbufVirtY=ptrVirt;
frameBuf[i].pbufVirtCb=ptrVirt+ySize;
frameBuf[i].pbufVirtCr=ptrVirt+ySize+uSize;
frameBuf[i].pbufVirtMvCol=ptrVirt+ySize+uSize+vSize;
ptrVirt+=ySize+uSize+vSize+mvSize;
/* fill bottom address for field tile*/
if(pDecContxt->nMapType==2)
{
frameBuf[i].pbufY_tilebot=frameBuf[i].pbufY+ySize/2;
frameBuf[i].pbufCb_tilebot=frameBuf[i].pbufCr;
frameBuf[i].pbufVirtY_tilebot=frameBuf[i].pbufVirtY+ySize/2;
frameBuf[i].pbufVirtCb_tilebot=frameBuf[i].pbufVirtCr;
}
else
{
frameBuf[i].pbufY_tilebot=0;
frameBuf[i].pbufCb_tilebot=0;
frameBuf[i].pbufVirtY_tilebot=0;
frameBuf[i].pbufVirtCb_tilebot=0;
}
}
#endif
//register frame buffs
DEC_TRACE;
ret=VPU_DecRegisterFrameBuffer(handle, frameBuf, BufNum);
DEC_TRACE;
if(VPU_DEC_RET_SUCCESS!=ret)
{
DEC_STREAM_PRINTF("%s: vpu register frame failure: ret=%d \r\n",__FUNCTION__,ret);
return 0;
}
*pOutFrmNum=BufNum;
return 1;
}
int DecodeLoop(VpuDecHandle handle,DecContxt * pDecContxt, unsigned char* pBitstream, unsigned char* pCodecData,DecMemInfo* pDecMemInfo,int* pFbHandle)
{
int err;
int fileeos;
int dispeos;
int readbytes=0;
int readCodecData=0;
int bufNull;
int dispFrameNum;
int repeatNum=pDecContxt->nRepeatNum;
int unitDataValidNum;
int unitDataSize=pDecContxt->nUnitDataSize;
VpuDecRetCode ret;
VpuBufferNode InData;
int bufRetCode=0;
VpuDecInitInfo InitInfo;
VpuDecOutFrameInfo frameInfo;
unsigned long long totalTime=0;
int capability=0;
VpuDecFrameLengthInfo decFrmLengthInfo;
unsigned int totalDecConsumedBytes; //stuffer + frame
int nFrmNum;
int streamLen;
#ifdef CHECK_DEAD_LOOP
int NotUsedLoopCnt=0;
int NULLDataLoopCnt=0;
#endif
VPU_DecGetCapability(handle, VPU_DEC_CAP_FRAMESIZE, &capability);
DEC_STREAM_PRINTF("capability: report frame size supported: %d \r\n",capability);
RepeatPlay:
//reset init value
err=0;
fileeos=0;
dispeos=0;
dispFrameNum=0;
unitDataValidNum=0;
totalDecConsumedBytes=0;