-
Notifications
You must be signed in to change notification settings - Fork 4
/
DVDVideoCodecIMX.cpp
1304 lines (1167 loc) · 39.4 KB
/
DVDVideoCodecIMX.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
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) 2013 Stephan Rafin
*
* This Program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with XBMC; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include <linux/mxcfb.h>
#include "DVDVideoCodecIMX.h"
#include <linux/mxc_v4l2.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <sys/mman.h>
#include "threads/SingleLock.h"
#include "utils/log.h"
#include "DVDClock.h"
#include "mfw_gst_ts.h"
//#define NO_V4L_RENDERING
/* FIXME get rid of these defines properly */
#define FRAME_ALIGN 16
#define MEDIAINFO 1
#define _4CC(c1,c2,c3,c4) (((uint32_t)(c4)<<24)|((uint32_t)(c3)<<16)|((uint32_t)(c2)<<8)|(uint32_t)(c1))
#define Align(ptr,align) (((unsigned int)ptr + (align) - 1)/(align)*(align))
/* video device on which the video will be rendered (/dev/video17 => /dev/fb1) */
const char *CDVDVideoCodecIMX::m_v4lDeviceName = "/dev/video17";
/* Experiments show that we need at least one more (+1) V4L buffer than the min value returned by the VPU */
const int CDVDVideoCodecIMX::m_extraVpuBuffers = 8;
bool CDVDVideoCodecIMX::VpuAllocBuffers(VpuMemInfo *pMemBlock)
{
int i, size;
unsigned char * ptr;
VpuMemDesc vpuMem;
VpuDecRetCode ret;
for(i=0; i<pMemBlock->nSubBlockNum; i++)
{
size = pMemBlock->MemSubBlock[i].nAlignment + pMemBlock->MemSubBlock[i].nSize;
if (pMemBlock->MemSubBlock[i].MemType == VPU_MEM_VIRT)
{ // Allocate standard virtual memory
ptr = (unsigned char *)malloc(size);
if(ptr == NULL)
{
CLog::Log(LOGERROR, "%s - Unable to malloc %d bytes.\n", __FUNCTION__, size);
goto AllocFailure;
}
pMemBlock->MemSubBlock[i].pVirtAddr = (unsigned char*)Align(ptr, pMemBlock->MemSubBlock[i].nAlignment);
m_decMemInfo.virtMem[m_decMemInfo.nVirtNum] = (unsigned int)ptr;
m_decMemInfo.nVirtNum++;
}
else
{ // Allocate contigous mem for DMA
vpuMem.nSize = size;
ret = VPU_DecGetMem(&vpuMem);
if(ret != VPU_DEC_RET_SUCCESS)
{
CLog::Log(LOGERROR, "%s - Unable alloc %d bytes of physical memory (%d).\n", __FUNCTION__, size, ret);
goto AllocFailure;
}
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);
m_decMemInfo.phyMem_phyAddr[m_decMemInfo.nPhyNum] = (unsigned int)vpuMem.nPhyAddr;
m_decMemInfo.phyMem_virtAddr[m_decMemInfo.nPhyNum] = (unsigned int)vpuMem.nVirtAddr;
m_decMemInfo.phyMem_cpuAddr[m_decMemInfo.nPhyNum] = (unsigned int)vpuMem.nCpuAddr;
m_decMemInfo.phyMem_size[m_decMemInfo.nPhyNum] = size;
m_decMemInfo.nPhyNum++;
}
}
return true;
AllocFailure:
VpuFreeBuffers();
return false;
}
bool CDVDVideoCodecIMX::VpuFreeBuffers(void)
{
int i;
VpuMemDesc vpuMem;
VpuDecRetCode vpuRet;
bool ret = true;
//free virtual mem
for(i=0; i<m_decMemInfo.nVirtNum; i++)
{
if (m_decMemInfo.virtMem[i])
free((void*)m_decMemInfo.virtMem[i]);
}
m_decMemInfo.nVirtNum = 0;
//free physical mem
for(i=0; i<m_decMemInfo.nPhyNum; i++)
{
vpuMem.nPhyAddr = m_decMemInfo.phyMem_phyAddr[i];
vpuMem.nVirtAddr = m_decMemInfo.phyMem_virtAddr[i];
vpuMem.nCpuAddr = m_decMemInfo.phyMem_cpuAddr[i];
vpuMem.nSize = m_decMemInfo.phyMem_size[i];
vpuRet = VPU_DecFreeMem(&vpuMem);
if(vpuRet != VPU_DEC_RET_SUCCESS)
{
CLog::Log(LOGERROR, "%s - Errror while trying to free physical memory (%d).\n", __FUNCTION__, ret);
ret = false;
}
}
m_decMemInfo.nPhyNum = 0;
return ret;
}
bool CDVDVideoCodecIMX::VpuOpen(void)
{
VpuDecRetCode ret;
VpuVersionInfo vpuVersion;
VpuMemInfo memInfo;
VpuDecConfig config;
int param;
memset(&memInfo, 0, sizeof(VpuMemInfo));
ret = VPU_DecLoad();
if (ret != VPU_DEC_RET_SUCCESS)
{
CLog::Log(LOGERROR, "%s - VPU load failed with error code %d.\n", __FUNCTION__, ret);
goto VpuOpenError;
}
ret = VPU_DecGetVersionInfo(&vpuVersion);
if (ret != VPU_DEC_RET_SUCCESS)
{
CLog::Log(LOGERROR, "%s - VPU version cannot be read (%d).\n", __FUNCTION__, ret);
goto VpuOpenError;
}
else
{
CLog::Log(LOGDEBUG, "VPU Lib version : major.minor.rel=%d.%d.%d.\n", vpuVersion.nLibMajor, vpuVersion.nLibMinor, vpuVersion.nLibRelease);
}
ret = VPU_DecQueryMem(&memInfo);
if (ret != VPU_DEC_RET_SUCCESS)
{
CLog::Log(LOGERROR, "%s - iMX VPU query mem error (%d).\n", __FUNCTION__, ret);
goto VpuOpenError;
}
VpuAllocBuffers(&memInfo);
m_decOpenParam.nReorderEnable = 1;
m_decOpenParam.nChromaInterleave = 0; // No YUV chroma interleave
m_decOpenParam.nMapType = 0; // Linear
m_decOpenParam.nTiled2LinearEnable = 0;
m_decOpenParam.nEnableFileMode = 0;
ret = VPU_DecOpen(&m_vpuHandle, &m_decOpenParam, &memInfo);
if (ret != VPU_DEC_RET_SUCCESS)
{
CLog::Log(LOGERROR, "%s - iMX VPU open failed (%d).\n", __FUNCTION__, ret);
goto VpuOpenError;
}
/*
ret = VPU_DecReset(m_vpuHandle);
if (ret != VPU_DEC_RET_SUCCESS)
{
CLog::Log(LOGERROR, "%s - iMX VPU reset failed (%d).\n", __FUNCTION__, ret);
}*/
config = VPU_DEC_CONF_SKIPMODE;
param = VPU_DEC_SKIPNONE;
ret = VPU_DecConfig(m_vpuHandle, config, ¶m);
if (ret != VPU_DEC_RET_SUCCESS)
{
CLog::Log(LOGERROR, "%s - iMX VPU set skip mode failed (%d).\n", __FUNCTION__, ret);
goto VpuOpenError;
}
/* FIXME TBD
config = VPU_DEC_CONF_BUFDELAY;
param = 128 * 1024;
ret = VPU_DecConfig(m_vpuHandle, config, ¶m);
if (ret != VPU_DEC_RET_SUCCESS)
{
CLog::Log(LOGERROR, "%s - iMX VPU set buffer delay failed (%d).\n", __FUNCTION__, ret);
goto VpuOpenError;
}
*/
/* FIXME TODO Is there a need to explicitly configure :
* - VPU_DEC_CONF_INPUTTYPE
*/
return true;
VpuOpenError:
Dispose();
return false;
}
void CDVDVideoCodecIMX::InitFB(void)
{
struct fb_var_screeninfo screen;
struct mxcfb_gbl_alpha alpha;
struct mxcfb_color_key colorKey;
int fd;
fd = open("/dev/fb0",O_RDWR);
/* set FG/BG semi opaque */
alpha.alpha = 235;
alpha.enable = 1;
ioctl(fd, MXCFB_SET_GBL_ALPHA, &alpha);
/* Enable color keying */
colorKey.enable = 1;
colorKey.color_key = (1 << 16) | (2 << 8) | 3;
if (ioctl(fd, MXCFB_SET_CLR_KEY, &colorKey) < 0)
CLog::Log(LOGERROR, "%s - Error while trying to enable color keying %s.\n", __FUNCTION__, strerror(errno));
/* Retrieve screen resolution */
ioctl(fd, FBIOGET_VSCREENINFO, &screen);
close(fd);
/* Keep resolution */
m_xscreen = screen.xres;
m_yscreen = screen.yres;
}
void CDVDVideoCodecIMX::RestoreFB(void)
{
struct mxcfb_gbl_alpha alpha;
struct mxcfb_color_key colorKey;
int fd;
fd = open("/dev/fb0",O_RDWR);
/* set FG as opaque */
alpha.alpha = 255;
alpha.enable = 1;
ioctl(fd, MXCFB_SET_GBL_ALPHA, &alpha);
/* Disable color keying */
colorKey.enable = 0;
colorKey.color_key = 0;
if (ioctl(fd, MXCFB_SET_CLR_KEY, &colorKey) < 0)
CLog::Log(LOGERROR, "%s - Error while trying to disable color keying %s.\n", __FUNCTION__, strerror(errno));
close(fd);
}
bool CDVDVideoCodecIMX::VpuAllocFrameBuffers(void)
{
/* Alloc frame buffers from V4L2 for efficient rendering through V4L streaming */
struct v4l2_requestbuffers bufReq;
struct v4l2_format fmt;
// struct v4l2_control ctrl;
int ret, i;
int width, height;
int ySize, cSize;
int video_width, video_height;
VpuDecRetCode vpuRet;
struct v4l2_buffer v4lBuf;
m_v4lfd = open(m_v4lDeviceName, O_RDWR|O_NONBLOCK, 0);
if (m_v4lfd < 0)
{
CLog::Log(LOGERROR, "%s - Error while trying to open %s.\n", __FUNCTION__, m_v4lDeviceName);
return false;
}
video_width = Align(m_initInfo.nPicWidth, FRAME_ALIGN);
video_height = Align(m_initInfo.nPicHeight, FRAME_ALIGN);
/* Set crop to keep aspect ratio */
m_crop.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
m_crop.c.top = 0;
m_crop.c.left = 0;
m_crop.c.width = m_xscreen;
m_crop.c.height = m_yscreen;
if (m_crop.c.width * video_height > m_crop.c.height * video_width)
{
width = video_width * m_crop.c.height / video_height;
width = width & 0xFFFFFFF8;
m_crop.c.left = m_crop.c.left + (m_crop.c.width - width) / 2;
m_crop.c.width = width;
}
else if (m_crop.c.width * video_height < m_crop.c.height * video_width)
{
height = video_height * m_crop.c.width / video_width;
height = height & 0xFFFFFFF8;
m_crop.c.top = m_crop.c.top + (m_crop.c.height - height) / 2;
m_crop.c.height = height;
}
ret = ioctl(m_v4lfd, VIDIOC_S_CROP, &m_crop);
if (ret < 0)
{
CLog::Log(LOGERROR, "%s - Error while setting crop (ret %d : %s).\n", __FUNCTION__, ret, strerror(errno));
return false;
}
CLog::Log(LOGDEBUG, "%s - V4L crop : %d %d %d %d\n", __FUNCTION__, m_crop.c.top, m_crop.c.left, m_crop.c.width, m_crop.c.height);
/* Set V4L2 Format */
memset(&fmt, 0, sizeof(fmt));
fmt.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
fmt.fmt.pix.width = video_width;
fmt.fmt.pix.height = video_height;
fmt.fmt.pix.pixelformat = V4L2_PIX_FMT_YUV420;
fmt.fmt.pix.priv = 0;
ret = ioctl(m_v4lfd, VIDIOC_S_FMT, &fmt);
if (ret < 0)
{
CLog::Log(LOGERROR, "%s - Error while setting V4L format (ret %d : %s).\n", __FUNCTION__, ret, strerror(errno));
return false;
}
ret = ioctl(m_v4lfd, VIDIOC_G_FMT, &fmt);
if (ret < 0)
{
CLog::Log(LOGERROR, "%s - Error while getting V4L format (ret %d : %s).\n", __FUNCTION__, ret, strerror(errno));
return false;
}
/* FIXME : How to select appropriate motion type ?
ctrl.id = V4L2_CID_MXC_MOTION;
ctrl.value = 0;
ret = ioctl (m_v4lfd, VIDIOC_S_CTRL, &ctrl);
if (ret < 0)
{
CLog::Log(LOGERROR, "%s - Error while setting V4L motion (ret %d : %s).\n", __FUNCTION__, ret, strerror(errno));
return false;
}
*/
/* Alloc V4L2 buffers */
memset(&bufReq, 0, sizeof(bufReq));
bufReq.count = m_initInfo.nMinFrameBufferCount + m_extraVpuBuffers;
bufReq.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
bufReq.memory = V4L2_MEMORY_MMAP;
ret = ioctl(m_v4lfd, VIDIOC_REQBUFS, &bufReq);
if (ret < 0)
{
CLog::Log(LOGERROR, "%s - %d Hw buffer allocation error (%d)\n", __FUNCTION__, bufReq.count, ret);
return false;
}
CLog::Log(LOGDEBUG, "%s - %d Hw buffer of %d bytes allocated\n", __FUNCTION__, bufReq.count, fmt.fmt.pix.sizeimage);
m_vpuFrameBufferNum = bufReq.count;
m_outputBuffers = new VpuFrameBuffer*[m_vpuFrameBufferNum];
m_vpuFrameBuffers = new VpuFrameBuffer[m_vpuFrameBufferNum];
m_v4lBuffers = new v4l2_buffer[m_vpuFrameBufferNum];
ySize = fmt.fmt.pix.width * fmt.fmt.pix.height;
cSize = ySize / 4;
for (i=0 ; i<m_vpuFrameBufferNum; i++)
{
m_outputBuffers[i] = NULL;
memset(&v4lBuf, 0, sizeof(v4lBuf));
v4lBuf.index = i;
v4lBuf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
v4lBuf.memory = V4L2_MEMORY_MMAP;
ret = ioctl (m_v4lfd, VIDIOC_QUERYBUF, &v4lBuf);
if (ret < 0)
{
CLog::Log(LOGERROR, "%s - Error during 1st query of V4L buffer (ret %d : %s)\n", __FUNCTION__, ret, strerror(errno));
return false;
}
m_vpuFrameBuffers[i].pbufVirtY = (unsigned char *)mmap(NULL, v4lBuf.length, PROT_READ | PROT_WRITE, MAP_SHARED, m_v4lfd, v4lBuf.m.offset);
/* 2nd query to retrieve real Physical address (iMX6 bug) */
ret = ioctl (m_v4lfd, VIDIOC_QUERYBUF, &v4lBuf);
if (ret < 0)
{
CLog::Log(LOGERROR, "%s - Error during 2nd query of V4L buffer (ret %d : %s)\n", __FUNCTION__, ret, strerror(errno));
return false;
}
m_v4lBuffers[i] = v4lBuf;
m_vpuFrameBuffers[i].nStrideY = fmt.fmt.pix.width;
m_vpuFrameBuffers[i].nStrideC = m_vpuFrameBuffers[i].nStrideY / 2;
m_vpuFrameBuffers[i].pbufY = (unsigned char *)v4lBuf.m.offset;
m_vpuFrameBuffers[i].pbufCb = m_vpuFrameBuffers[i].pbufY + ySize;
m_vpuFrameBuffers[i].pbufCr = m_vpuFrameBuffers[i].pbufCb + cSize;
m_vpuFrameBuffers[i].pbufVirtCb = m_vpuFrameBuffers[i].pbufVirtY + ySize;
m_vpuFrameBuffers[i].pbufVirtCr = m_vpuFrameBuffers[i].pbufVirtCb + cSize;
/* Dont care about tile */
m_vpuFrameBuffers[i].pbufY_tilebot = 0;
m_vpuFrameBuffers[i].pbufCb_tilebot = 0;
m_vpuFrameBuffers[i].pbufVirtY_tilebot = 0;
m_vpuFrameBuffers[i].pbufVirtCb_tilebot = 0;
}
/* Allocate physical extra memory */
m_extraMem.nSize = cSize * m_vpuFrameBufferNum;
vpuRet = VPU_DecGetMem(&m_extraMem);
if (vpuRet != VPU_DEC_RET_SUCCESS)
{
m_extraMem.nSize = 0;
CLog::Log(LOGERROR, "%s - Extra memory (%d bytes) allocation failure (%d).\n",
__FUNCTION__, m_extraMem.nSize , vpuRet);
return false;
}
m_vpuFrameBuffers[0].pbufMvCol = (unsigned char *)m_extraMem.nPhyAddr;
m_vpuFrameBuffers[0].pbufVirtMvCol = (unsigned char *)m_extraMem.nVirtAddr;
for (i=1 ; i<m_vpuFrameBufferNum; i++)
{
m_vpuFrameBuffers[i].pbufMvCol = m_vpuFrameBuffers[i-1].pbufMvCol + cSize;
m_vpuFrameBuffers[i].pbufVirtMvCol = m_vpuFrameBuffers[i-1].pbufVirtMvCol + cSize;
}
return true;
}
bool CDVDVideoCodecIMX::VpuPushFrame(VpuFrameBuffer *frameBuffer)
{
int i;
outputFrameType outputFrame;
/* Find Frame given physical address */
for (i=0; i<m_vpuFrameBufferNum; i++)
if ((unsigned int)frameBuffer->pbufY == m_v4lBuffers[i].m.offset)
break;
if (i >= m_vpuFrameBufferNum)
{
CLog::Log(LOGERROR, "%s - V4L buffer not found\n", __FUNCTION__);
return false;
}
if (m_outputBuffers[i] != NULL)
{
CLog::Log(LOGERROR, "%s - Try to reuse buffer which was not dequeued !\n", __FUNCTION__);
return false;
}
/* Store the pointer to be able to invoke VPU_DecOutFrameDisplayed when the buffer will be dequeued*/
m_outputBuffers[i] = frameBuffer;
//CLog::Log(LOGDEBUG, "%s - set ouputBuffer idx %d : %x\n", __FUNCTION__, i, frameBuffer);
/* Frame ready to be displayed by the renderer */
outputFrame.v4l2_buffer = &m_v4lBuffers[i];
outputFrame.pts = (double)TSManagerSend2(m_tsm, frameBuffer) / (double)1000.0;
CSingleLock lock(outputFrameQueueLock);
m_outputFrames.push(outputFrame);
return true;
}
void CDVDVideoCodecIMX::RenderFrame(void)
{
/* Warning : called from renderer thread
* Especially do not call any VPU funcrions as they are not thread safe
*/
int ret, type;
outputFrameType outputFrame;
if (m_outputFrames.size() == 0)
{
CLog::Log(LOGNOTICE, "%s - No frame available to render !\n",
__FUNCTION__);
return;
}
outputFrame = m_outputFrames.front();
outputFrame.v4l2_buffer->timestamp.tv_sec = 0;
outputFrame.v4l2_buffer->timestamp.tv_usec = 0;
#ifndef NO_V4L_RENDERING
ret = ioctl(m_v4lfd, VIDIOC_QBUF, outputFrame.v4l2_buffer);
if (ret < 0)
{
CLog::Log(LOGERROR, "%s - V4L Queue buffer failed (ret %d : %s)\n",
__FUNCTION__, ret, strerror(errno));
}
/*CLog::Log(LOGDEBUG, "%s - Queue buffer %d\n",
__FUNCTION__, buffer->index);*/
if (!m_streamon)
{
type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
ret = ioctl(m_v4lfd, VIDIOC_STREAMON, &type);
if (ret < 0)
{
CLog::Log(LOGERROR, "%s - V4L Stream ON failed (ret %d : %s)\n",
__FUNCTION__, ret, strerror(errno));
}
m_streamon = true;
ioctl(m_v4lfd, VIDIOC_S_CROP, &m_crop);
}
#endif
//CLog::Log(LOGERROR, "%s - render frame called on buffer %d (size : %d) \n", __FUNCTION__, buffer->index, m_outputFrames.size());
CSingleLock lock(outputFrameQueueLock);
m_outputFrames.pop();
}
int CDVDVideoCodecIMX::GetAvailableBufferNb(void)
{
int i, nb;
nb = 0;
for (i=0; i<m_vpuFrameBufferNum; i++)
{
if (m_outputBuffers[i] == NULL)
nb++;
}
return nb;
}
bool CDVDVideoCodecIMX::VpuDeQueueFrame(void)
{
int ret;
struct v4l2_buffer buf;
if ((m_v4lfd == -1) ||
(!m_streamon))
{
#ifdef NO_V4L_RENDERING
{
int i;
for (i=0; i<m_vpuFrameBufferNum; i++)
{
if (m_outputBuffers[i] != NULL)
{
VPU_DecOutFrameDisplayed(m_vpuHandle, m_outputBuffers[i]);
m_outputBuffers[i] = NULL;
}
}
}
#endif
return false;
}
buf.type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
buf.memory = V4L2_MEMORY_MMAP;
ret = ioctl(m_v4lfd, VIDIOC_DQBUF, &buf);
if (ret != 0)
{
if (errno != EAGAIN)
CLog::Log(LOGERROR, "%s - Dequeue buffer error (ret %d : %s)\n",
__FUNCTION__, ret, strerror(errno));
return false;
}
/*CLog::Log(LOGDEBUG, "%s - Dequeue buffer %d\n",
__FUNCTION__, buf.index);*/
// Mark frame as displayed for VPU
VPU_DecOutFrameDisplayed(m_vpuHandle, m_outputBuffers[buf.index]);
m_outputBuffers[buf.index] = NULL;
return true;
}
CDVDVideoCodecIMX::CDVDVideoCodecIMX()
{
m_pFormatName = "iMX-xxx";
memset(&m_decMemInfo, 0, sizeof(DecMemInfo));
m_vpuHandle = 0;
m_v4lfd = -1;
m_vpuFrameBuffers = NULL;
m_outputBuffers = NULL;
m_v4lBuffers = NULL;
m_vpuFrameBufferNum = 0;
m_extraMem.nSize = 0;
m_streamon = false;
// m_tsSyncRequired = true;
m_dropState = false;
}
CDVDVideoCodecIMX::~CDVDVideoCodecIMX()
{
Dispose();
}
bool CDVDVideoCodecIMX::Open(CDVDStreamInfo &hints, CDVDCodecOptions &options)
{
if (hints.software)
{
CLog::Log(LOGNOTICE, "iMX VPU : software decoding requested.\n");
return false;
}
m_displayedFrames = 0;
m_hints = hints;
CLog::Log(LOGNOTICE, "Let's decode with iMX VPU\n");
#ifdef MEDIAINFO
CLog::Log(LOGDEBUG, "Decode: MEDIAINFO: fpsrate %d / fpsscale %d\n", m_hints.fpsrate, m_hints.fpsscale);
CLog::Log(LOGDEBUG, "Decode: MEDIAINFO: CodecID %d \n", m_hints.codec);
CLog::Log(LOGDEBUG, "Decode: MEDIAINFO: StreamType %d \n", m_hints.type);
CLog::Log(LOGDEBUG, "Decode: MEDIAINFO: Level %d \n", m_hints.level);
CLog::Log(LOGDEBUG, "Decode: MEDIAINFO: Profile %d \n", m_hints.profile);
CLog::Log(LOGDEBUG, "Decode: MEDIAINFO: PTS_invalid %d \n", m_hints.ptsinvalid);
CLog::Log(LOGDEBUG, "Decode: MEDIAINFO: Tag %d \n", m_hints.codec_tag);
CLog::Log(LOGDEBUG, "Decode: MEDIAINFO: %dx%d \n", m_hints.width, m_hints.height);
{ uint8_t *pb = (uint8_t*)&m_hints.codec_tag;
if (isalnum(pb[0]) && isalnum(pb[1]) && isalnum(pb[2]) && isalnum(pb[3]))
CLog::Log(LOGDEBUG, "Decode: MEDIAINFO: Tag fourcc %c%c%c%c\n", pb[0], pb[1], pb[2], pb[3]);
}
if (m_hints.extrasize)
{
unsigned int i;
char buf[4096];
for (i = 0; i < m_hints.extrasize; i++)
sprintf(buf+i*2, "%02x", ((uint8_t*)m_hints.extradata)[i]);
CLog::Log(LOGDEBUG, "Decode: MEDIAINFO: extradata %d %s\n", m_hints.extrasize, buf);
}
#endif
m_convert_bitstream = false;
switch(m_hints.codec)
{
case CODEC_ID_MPEG2VIDEO:
m_decOpenParam.CodecFormat = VPU_V_MPEG2;
m_pFormatName = "iMX-mpeg2";
break;
case CODEC_ID_H263:
m_decOpenParam.CodecFormat = VPU_V_H263;
m_pFormatName = "iMX-h263";
break;
case CODEC_ID_H264:
m_decOpenParam.CodecFormat = VPU_V_AVC;
m_pFormatName = "iMX-h264";
if (hints.extrasize < 7 || hints.extradata == NULL)
{
CLog::Log(LOGNOTICE, "%s - avcC data too small or missing", __func__);
return false;
}
if ( *(char*)hints.extradata == 1 )
m_convert_bitstream = bitstream_convert_init(hints.extradata, hints.extrasize);
break;
case CODEC_ID_VC1:
m_decOpenParam.CodecFormat = VPU_V_VC1_AP;
m_pFormatName = "iMX-vc1";
break;
/* FIXME TODO
* => for this type we have to set height, width, nChromaInterleave and nMapType
case CODEC_ID_MJPEG:
m_decOpenParam.CodecFormat = VPU_V_MJPG;
m_pFormatName = "iMX-mjpg";
break;
*/
case CODEC_ID_VP8:
m_decOpenParam.CodecFormat = VPU_V_VP8;
m_pFormatName = "iMX-vp8";
break;
case CODEC_ID_MPEG4:
switch(m_hints.codec_tag)
{
case _4CC('D','I','V','X'):
m_decOpenParam.CodecFormat = VPU_V_DIVX4;
m_pFormatName = "iMX-divx4";
break;
case _4CC('D','X','5','0'):
case _4CC('D','I','V','5'):
m_decOpenParam.CodecFormat = VPU_V_DIVX56;
m_pFormatName = "iMX-divx5";
break;
case _4CC('X','V','I','D'):
case _4CC('M','P','4','V'):
case _4CC('P','M','P','4'):
case _4CC('F','M','P','4'):
m_decOpenParam.CodecFormat = VPU_V_XVID;
m_pFormatName = "iMX-xvid";
break;
default:
CLog::Log(LOGERROR, "iMX VPU : MPEG4 codec tag %d is not (yet) handled.\n", m_hints.codec_tag);
return false;
}
break;
default:
CLog::Log(LOGERROR, "iMX VPU : codecid %d is not (yet) handled.\n", m_hints.codec);
return false;
}
InitFB();
m_tsm = createTSManager(0);
setTSManagerFrameRate(m_tsm, m_hints.fpsrate, m_hints.fpsscale);
return VpuOpen();
}
void CDVDVideoCodecIMX::Dispose(void)
{
VpuDecRetCode ret;
int i, type;
if (m_vpuHandle)
{
ret = VPU_DecFlushAll(m_vpuHandle);
if (ret != VPU_DEC_RET_SUCCESS)
{
CLog::Log(LOGERROR, "%s - VPU flush failed with error code %d.\n", __FUNCTION__, ret);
}
ret = VPU_DecClose(m_vpuHandle);
if (ret != VPU_DEC_RET_SUCCESS)
{
CLog::Log(LOGERROR, "%s - VPU close failed with error code %d.\n", __FUNCTION__, ret);
}
m_vpuHandle = 0;
}
VpuFreeBuffers();
if (m_v4lfd >= 0)
{
while (VpuDeQueueFrame());
/* stream off */
type = V4L2_BUF_TYPE_VIDEO_OUTPUT;
ioctl (m_v4lfd, VIDIOC_STREAMOFF, &type);
m_streamon = false;
/* unmap vpuFrameBuffers */
if ((m_vpuFrameBuffers != NULL) && (m_v4lBuffers != NULL))
{
for (i = 0; i < m_vpuFrameBufferNum; i++)
{
munmap (m_vpuFrameBuffers[i].pbufVirtY, m_v4lBuffers[i].length);
}
}
if (m_vpuFrameBuffers != NULL)
{
delete m_vpuFrameBuffers;
m_vpuFrameBuffers = NULL;
}
if (m_v4lBuffers != NULL)
{
delete m_v4lBuffers;
m_v4lBuffers = NULL;
}
if (m_outputBuffers != NULL)
{
delete m_outputBuffers;
m_outputBuffers = NULL;
}
m_vpuFrameBufferNum = 0;
/* vpuFrameBuffers (V4L buffers) will be released by this close ? */
close(m_v4lfd);
m_v4lfd = -1;
/* Free extramem */
if (m_extraMem.nSize > 0)
{
ret = VPU_DecFreeMem(&m_extraMem);
if (ret != VPU_DEC_RET_SUCCESS)
{
CLog::Log(LOGERROR, "%s - Release extra mem failed with error code %d.\n", __FUNCTION__, ret);
}
m_extraMem.nSize = 0;
}
}
ret = VPU_DecUnLoad();
if (ret != VPU_DEC_RET_SUCCESS)
{
CLog::Log(LOGERROR, "%s - VPU unload failed with error code %d.\n", __FUNCTION__, ret);
}
if (m_tsm != NULL)
{
destroyTSManager(m_tsm);
m_tsm = NULL;
}
RestoreFB();
return;
}
int CDVDVideoCodecIMX::Decode(BYTE *pData, int iSize, double dts, double pts)
{
VpuDecFrameLengthInfo frameLengthInfo;
VpuBufferNode inData;
VpuDecRetCode ret;
VpuDecOutFrameInfo frameInfo;
int decRet = 0;
int retSatus = 0;
int demuxer_bytes = iSize;
uint8_t *demuxer_content = pData;
bool bitstream_convered = false;
bool retry = false;
//CLog::Log(LOGDEBUG, "%s - demux size : %d dts : %f - pts : %f\n", __FUNCTION__, iSize, dts, pts);
if (!m_vpuHandle)
return VC_ERROR;
while (VpuDeQueueFrame());
if (pData && iSize)
{
if (m_convert_bitstream)
{
// convert demuxer packet from bitstream to bytestream (AnnexB)
int bytestream_size = 0;
uint8_t *bytestream_buff = NULL;
if (!bitstream_convert(demuxer_content, demuxer_bytes, &bytestream_buff, &bytestream_size))
{
CLog::Log(LOGERROR, "%s - bitstream convert error...\n", __FUNCTION__);
return VC_ERROR;
}
if (bytestream_buff && (bytestream_size > 0))
{
bitstream_convered = true;
demuxer_bytes = bytestream_size;
demuxer_content = bytestream_buff;
}
}
if (m_tsSyncRequired)
{
m_tsSyncRequired = false;
if (pts != DVD_NOPTS_VALUE)
resyncTSManager(m_tsm, llrint(pts) * 1000, MODE_AI);
}
if (pts != DVD_NOPTS_VALUE)
{
/* TODO quelle taille iSize ou demuxer_bytes ? */
TSManagerReceive2(m_tsm, llrint(pts) * 1000, iSize /*demuxer_bytes*/);
}
//CLog::Log(LOGDEBUG, "%s - Query2 : %lld\n", __FUNCTION__, TSManagerQuery2(m_tsm, NULL));
TSManagerQuery2(m_tsm, NULL);
inData.nSize = demuxer_bytes;
inData.pPhyAddr = NULL;
inData.pVirAddr = demuxer_content;
/* FIXME TODO VP8 & DivX3 require specific sCodecData values */
if ((m_decOpenParam.CodecFormat == VPU_V_MPEG2) ||
(m_decOpenParam.CodecFormat == VPU_V_VC1_AP))
{
inData.sCodecData.pData = (unsigned char *)m_hints.extradata;
inData.sCodecData.nSize = m_hints.extrasize;
}
else
{
inData.sCodecData.pData = NULL;
inData.sCodecData.nSize = 0;
}
do // Decode as long as the VPU uses data
{
retry = false;
ret = VPU_DecDecodeBuf(m_vpuHandle, &inData, &decRet);
if (ret != VPU_DEC_RET_SUCCESS)
{
CLog::Log(LOGERROR, "%s - VPU decode failed with error code %d.\n", __FUNCTION__, ret);
goto out_error;
}
/* else
{
CLog::Log(LOGDEBUG, "%s - VPU decode success : %x.\n", __FUNCTION__, decRet);
}*/
if (decRet & VPU_DEC_INIT_OK)
/* VPU decoding init OK : We can retrieve stream info */
{
ret = VPU_DecGetInitialInfo(m_vpuHandle, &m_initInfo);
if (ret == VPU_DEC_RET_SUCCESS)
{
CLog::Log(LOGDEBUG, "%s - VPU Init Stream Info : %dx%d (interlaced : %d - Minframe : %d)"\
" - Align : %d bytes - crop : %d %d %d %d\n", __FUNCTION__,
m_initInfo.nPicWidth, m_initInfo.nPicHeight, m_initInfo.nInterlace, m_initInfo.nMinFrameBufferCount,
m_initInfo.nAddressAlignment, m_initInfo.PicCropRect.nLeft, m_initInfo.PicCropRect.nTop,
m_initInfo.PicCropRect.nRight, m_initInfo.PicCropRect.nBottom);
if (VpuAllocFrameBuffers())
{
ret = VPU_DecRegisterFrameBuffer(m_vpuHandle, m_vpuFrameBuffers, m_vpuFrameBufferNum);
if (ret != VPU_DEC_RET_SUCCESS)
{
CLog::Log(LOGERROR, "%s - VPU error while registering frame buffers (%d).\n", __FUNCTION__, ret);
goto out_error;
}
}
else
{
goto out_error;
}
}
else
{
CLog::Log(LOGERROR, "%s - VPU get initial info failed (%d).\n", __FUNCTION__, ret);
goto out_error;
}
}//VPU_DEC_INIT_OK
if (decRet & VPU_DEC_ONE_FRM_CONSUMED)
{
ret = VPU_DecGetConsumedFrameInfo(m_vpuHandle, &frameLengthInfo);
if (ret != VPU_DEC_RET_SUCCESS)
{
CLog::Log(LOGERROR, "%s - VPU error retireving info about consummed frame (%d).\n", __FUNCTION__, ret);
}
TSManagerValid2(m_tsm, frameLengthInfo.nFrameLength + frameLengthInfo.nStuffLength, frameLengthInfo.pFrame);
//CLog::Log(LOGDEBUG, "%s - size : %d - key consummed : %x\n", __FUNCTION__, frameLengthInfo.nFrameLength + frameLengthInfo.nStuffLength, frameLengthInfo.pFrame);
}//VPU_DEC_ONE_FRM_CONSUMED
if ((decRet & VPU_DEC_OUTPUT_DIS) ||
(decRet & VPU_DEC_OUTPUT_MOSAIC_DIS))
/* Frame ready to be displayed */
{
ret = VPU_DecGetOutputFrame(m_vpuHandle, &frameInfo);
if(ret != VPU_DEC_RET_SUCCESS)
{
CLog::Log(LOGERROR, "%s - VPU Cannot get output frame(%d).\n", __FUNCTION__, ret);
goto out_error;
}
if (VpuPushFrame(frameInfo.pDisplayFrameBuf))
{
m_displayedFrames++;
retSatus |= VC_PICTURE;
}
else
{
TSManagerSend(m_tsm);
}
} //VPU_DEC_OUTPUT_DIS
if (decRet & VPU_DEC_OUTPUT_REPEAT)
{
TSManagerSend(m_tsm);
CLog::Log(LOGDEBUG, "%s - Frame repeat.\n", __FUNCTION__);
}
if (decRet & VPU_DEC_OUTPUT_DROPPED)
{
TSManagerSend(m_tsm);
CLog::Log(LOGDEBUG, "%s - Frame dropped.\n", __FUNCTION__);
}
if (decRet & VPU_DEC_NO_ENOUGH_BUF)
{
CLog::Log(LOGERROR, "%s - No frame buffer available. Queued frame : %d.\n", __FUNCTION__, m_outputFrames.size());
}
if (decRet & VPU_DEC_SKIP)
{
TSManagerSend(m_tsm);
CLog::Log(LOGDEBUG, "%s - Frame skipped.\n", __FUNCTION__);
}
if (decRet & VPU_DEC_FLUSH)
{
CLog::Log(LOGNOTICE, "%s - VPU requires a flush.\n", __FUNCTION__);
ret = VPU_DecFlushAll(m_vpuHandle);
if (ret != VPU_DEC_RET_SUCCESS)
{
CLog::Log(LOGERROR, "%s - VPU flush failed(%d).\n", __FUNCTION__, ret);
}
retSatus = VC_FLUSHED;
}
if (decRet & VPU_DEC_OUTPUT_EOS)
{
CLog::Log(LOGNOTICE, "%s - EOS encountered.\n", __FUNCTION__);
}
if (decRet & VPU_DEC_NO_ENOUGH_INBUF)
{
// We are done with VPU decoder that time
break;
}
if (!(decRet & VPU_DEC_INPUT_USED))
{
// Retry decode in this case
retry = true;
CLog::Log(LOGERROR, "%s - input not used !\n", __FUNCTION__);
}
if (inData.nSize != 0)
{ // Let's process again as VPU_DEC_NO_ENOUGH_INBUF was not set if we reach that point
inData.pVirAddr = NULL;
inData.nSize = 0;
retry = true;
}
else
{
if (decRet & VPU_DEC_OUTPUT_DIS)
// the decoder may still have additional input data to process if it has just produced a picture
retry = true;
}
} while (retry == true);
} //(pData && iSize)
test_fb:
if (GetAvailableBufferNb() > (m_vpuFrameBufferNum - m_extraVpuBuffers))
{
retSatus |= VC_BUFFER;
}
else
{
/* If pData == NULL, then we are in dvdplayervideo's special loop
* where it checks for more picture frames and calls us in loop
* But we cannot accept any additional input data for now.
* So aggressively drop pictures which are not yet streamed
* in V4L2 device to be able to ask for more data
*/
outputFrameType outputFrame;
CLog::Log(LOGNOTICE, "%s - No frame buffer available (min Free %d - Ready picture %d)\n", __FUNCTION__, GetAvailableBufferNb(), m_outputFrames.size());
#if 0