summaryrefslogtreecommitdiff
path: root/tvapi/libtv/tvin/CTvin.cpp (plain)
blob: fc2171223859b2bf2dffa757d9d82e2559df3fa3
1
2#include "CTvin.h"
3#include <CTvLog.h>
4#include <stdio.h>
5#include <unistd.h>
6#include <sys/types.h>
7#include <sys/stat.h>
8#include <sys/ioctl.h>
9#include <fcntl.h>
10#include <string.h>
11#include <errno.h>
12#include <dlfcn.h>
13#include <linux/fb.h>
14#include <stdlib.h>
15#include <cutils/properties.h>
16#include <cutils/log.h>
17#include <sys/mman.h>
18#include "../vpp/CPQdb.h"
19#include "CAv.h"
20#include "../tvsetting/CTvSetting.h"
21#include "../tvutils/tvutils.h"
22#include "../tvconfig/tvconfig.h"
23#include "CFbcCommunication.h"
24
25
26#ifdef LOG_TAG
27#undef LOG_TAG
28#define LOG_TAG "CTvin"
29#endif
30
31#define AFE_DEV_PATH "/dev/tvafe0"
32#define HDMIRX_KSV_PATH "/dev/hdmirx0"
33
34
35#define CC_SEL_VDIN_DEV (0)
36
37#define MODE_3D_DISABLE 0x00000000
38#define MODE_3D_ENABLE 0x00000001
39#define MODE_AUTO 0x00000002
40#define MODE_2D_TO_3D 0x00000004
41#define MODE_LR 0x00000008
42#define MODE_BT 0x00000010
43#define MODE_LR_SWITCH 0x00000020
44#define MODE_FIELD_DEPTH 0x00000040
45#define MODE_3D_TO_2D_L 0x00000080
46#define MODE_3D_TO_2D_R 0x00000100
47#define LR_FORMAT_INDICATOR 0x00000200
48#define BT_FORMAT_INDICATOR 0x00000400
49
50/* ADC calibration pattern & format define */
51/* default 100% 8 color-bar */
52//#define VGA_SOURCE_RGB444
53#define VGA_H_ACTIVE (1024)
54#define VGA_V_ACTIVE (768)
55#define COMP_H_ACTIVE (1280)
56#define COMP_V_ACTIVE (720)
57#define CVBS_H_ACTIVE (720)
58#define CVBS_V_ACTIVE (480)
59
60int CTvin::mSourceInputToPortMap[SOURCE_MAX];
61CTvin *CTvin::mInstance;
62
63CTvin *CTvin::getInstance()
64{
65 if (NULL == mInstance) mInstance = new CTvin();
66 return mInstance;
67}
68
69CTvin::CTvin()
70{
71 int i = 0;
72
73 m_vdin_dev_fd = -1;
74 afe_dev_fd = -1;
75
76 m_tvin_param.index = 0;
77 m_is_decoder_start = false;
78 gExistD2D3 = -1;
79 gVideoPath[0] = '\0';
80 m_pathid = TV_PATH_DECODER_3D_AMVIDEO;
81
82 for (i = 0; i < SOURCE_MAX; i++) {
83 mSourceInputToPortMap[i] = TVIN_PORT_NULL;
84 }
85
86 mSourceInputToPortMap[SOURCE_TV] = TVIN_PORT_CVBS3;
87 mSourceInputToPortMap[SOURCE_AV1] = TVIN_PORT_CVBS1;
88 mSourceInputToPortMap[SOURCE_AV2] = TVIN_PORT_CVBS2;
89 mSourceInputToPortMap[SOURCE_YPBPR1] = TVIN_PORT_COMP0;
90 mSourceInputToPortMap[SOURCE_YPBPR2] = TVIN_PORT_COMP1;
91 mSourceInputToPortMap[SOURCE_HDMI1] = TVIN_PORT_HDMI0;
92 mSourceInputToPortMap[SOURCE_HDMI2] = TVIN_PORT_HDMI2;
93 mSourceInputToPortMap[SOURCE_HDMI3] = TVIN_PORT_HDMI1;
94 mSourceInputToPortMap[SOURCE_VGA] = TVIN_PORT_VGA0;
95 mSourceInputToPortMap[SOURCE_MPEG] = TVIN_PORT_MPEG0;
96 mSourceInputToPortMap[SOURCE_DTV] = TVIN_PORT_DTV;
97 mSourceInputToPortMap[SOURCE_IPTV] = TVIN_PORT_BT656;
98}
99
100CTvin::~CTvin()
101{
102}
103
104int CTvin::OpenTvin()
105{
106 const char *config_value;
107 config_value = config_get_str ( CFG_SECTION_TV, "tvin.manual.set.tvpath", "null" );
108 strcpy ( config_tv_path, config_value );
109 memset ( config_default_path, 0x0, 64 );
110 config_value = config_get_str ( CFG_SECTION_TV, "tvin.manual.set.defaultpath", "null" );
111 strcpy ( config_default_path, config_value );
112 return 0;
113}
114
115int CTvin::IsFileExist ( const char *file_name )
116{
117 struct stat tmp_st;
118
119 if ( stat ( file_name, &tmp_st ) < 0 ) {
120 return 0;
121 }
122
123 return 1;
124}
125
126char *CTvin::DelSub ( char *str, char *sub )
127{
128 char *psrc = str, *pdest = str, *psub = NULL, *p = NULL;
129
130 if ( ( str == NULL ) || ( sub == NULL ) ) {
131 return NULL;
132 }
133
134 while ( *psrc ) {
135 p = psrc;
136 psub = sub;
137
138 while ( *p && *p == *psub ) {
139 p++;
140 psub++;
141 }
142
143 if ( *psub == 0 ) {
144 psrc = p;
145 } else {
146 *pdest++ = *psrc++;
147 }
148 }
149
150 *pdest = 0;
151
152 return str;
153}
154
155int CTvin::setMpeg2Vdin(int enable)
156{
157 /* let output data loop to vdin */
158 FILE *fp = fopen ( "/sys/module/di/parameters/mpeg2vdin_en", "w" );
159
160 if ( fp == NULL ) {
161 LOGW ( "Open /sys/module/di/parameters/mpeg2vdin_en error\n" );
162 return -1;
163 }
164 fprintf ( fp, "%d", enable );
165 fclose ( fp );
166 return 0;
167}
168
169char *CTvin::VDIN_CheckVideoPath ( const char *videopath )
170{
171 strncpy ( gVideoPath, videopath, sizeof ( gVideoPath ) );
172
173 if ( strstr ( videopath, "d2d3 " ) != NULL ) {
174 if ( gExistD2D3 == -1 ) {
175 gExistD2D3 = IsFileExist ( "/sys/class/d2d3/d2d3" );
176 }
177
178 if ( gExistD2D3 == 0 ) {
179 DelSub ( gVideoPath, "d2d3 " );
180 }
181 }
182
183 LOGW ( "%s, video path before check [%s]\n", CFG_SECTION_TV, videopath );
184 LOGW ( "%s, video path after check [%s]\n", CFG_SECTION_TV, gVideoPath );
185
186 return gVideoPath;
187}
188
189int CTvin::VDIN_AddPath ( const char *videopath )
190{
191 FILE *fp = NULL;
192 int ret = -1;
193 char *tmp_video_path = NULL;
194
195 tmp_video_path = VDIN_CheckVideoPath ( videopath );
196
197 fp = fopen ( "/sys/class/vfm/map", "w" );
198
199 if ( fp == NULL ) {
200 LOGW ( "Open /sys/class/vfm/map error(%s)!\n", strerror ( errno ) );
201 return -1;
202 }
203
204 ret = fprintf ( fp, "%s", tmp_video_path );
205
206 if ( ret < 0 ) {
207 LOGW ( "Add VideoPath error(%s)!\n", strerror ( errno ) );
208 }
209
210 fclose ( fp );
211 fp = NULL;
212
213 return ret;
214}
215
216int CTvin::VDIN_RmDefPath ( void )
217{
218 int fd = -1, ret;
219 char str[] = "rm default";
220
221
222 fd = open ( "/sys/class/vfm/map", O_RDWR );
223
224 if ( fd < 0 ) {
225 LOGW ( "Open /sys/class/vfm/map error(%s)!\n", strerror ( errno ) );
226 return -1;
227 }
228
229 ret = write ( fd, str, sizeof ( str ) );
230
231 if ( ret < 0 ) {
232 LOGW ( "Rm default path error(%s)!\n", strerror ( errno ) );
233 }
234
235 close ( fd );
236 fd = -1;
237
238 return ret;
239}
240
241int CTvin::VDIN_RmTvPath ( void )
242{
243 int fd, ret;
244 char str[] = "rm tvpath";
245
246
247 fd = open ( "/sys/class/vfm/map", O_RDWR );
248
249 if ( fd < 0 ) {
250 LOGW ( "Open /sys/class/vfm/map error(%s)!\n", strerror ( errno ) );
251 return -1;
252 }
253
254 ret = write ( fd, str, sizeof ( str ) );
255
256 if ( ret <= 0 ) {
257 LOGW ( "Rm tv path error(%s)!\n", strerror ( errno ) );
258 }
259
260 close ( fd );
261 fd = -1;
262 return ret;
263}
264
265int CTvin::VDIN_AddVideoPath ( int selPath )
266{
267 int ret = -1;
268
269 switch ( selPath ) {
270 case TV_PATH_VDIN_AMVIDEO:
271 ret = VDIN_AddPath ( "add tvpath vdin0 amvideo" );
272 break;
273
274 case TV_PATH_VDIN_DEINTERLACE_AMVIDEO:
275 ret = VDIN_AddPath ( "add tvpath vdin0 deinterlace amvideo" );
276 break;
277
278 case TV_PATH_VDIN_3D_AMVIDEO:
279 ret = VDIN_AddPath ( "add tvpath vdin0 ppmgr amvideo" );
280 break;
281
282 case TV_PATH_VDIN_NEW3D_AMVIDEO:
283 ret = VDIN_AddPath ( "add tvpath vdin0 deinterlace ppmgr d2d3 amvideo" );
284 break;
285
286 case TV_PATH_VDIN_NEW3D_WITHOUTPPMGR_AMVIDEO:
287 ret = VDIN_AddPath ( "add tvpath vdin0 deinterlace d2d3 amvideo" );
288 break;
289
290 case TV_PATH_DECODER_3D_AMVIDEO:
291 ret = VDIN_AddPath ( "add default decoder ppmgr deinterlace amvideo" );
292 break;
293
294 case TV_PATH_DECODER_AMVIDEO:
295 ret = VDIN_AddPath ( "add default decoder deinterlace amvideo" );
296 break;
297
298 case TV_PATH_VDIN_FREESCALE_AMVIDEO:
299 ret = VDIN_AddPath ( "add previewpath vdin0 freescale amvideo" );
300 break;
301
302 case TV_PATH_DECODER_NEW3D_AMVIDEO:
303 ret = VDIN_AddPath ( "add default decoder deinterlace ppmgr d2d3 amvideo" );
304 break;
305
306 case TV_PATH_DECODER_NEW3D_WITHOUTPPMGR_AMVIDEO:
307 ret = VDIN_AddPath ( "add default decoder deinterlace d2d3 amvideo" );
308 break;
309 }
310
311 return ret;
312}
313
314int CTvin::VDIN_RmPreviewPath ( void )
315{
316 int fd, ret;
317 char str[] = "rm previewpath";
318
319
320 fd = open ( "/sys/class/vfm/map", O_RDWR );
321
322 if ( fd < 0 ) {
323 LOGW ( "Open /sys/class/vfm/map error(%s)!\n", strerror ( errno ) );
324 return -1;
325 }
326
327 ret = write ( fd, str, sizeof ( str ) );
328
329 if ( ret < 0 ) {
330 LOGW ( "Rm tv path error(%s)!\n", strerror ( errno ) );
331 }
332
333 close ( fd );
334 fd = -1;
335
336 return ret;
337}
338
339int CTvin::VDIN_OpenModule()
340{
341 char file_name[64];
342 sprintf ( file_name, "/dev/vdin%d", CC_SEL_VDIN_DEV );
343 m_vdin_dev_fd = open ( file_name, O_RDWR );
344
345 if ( m_vdin_dev_fd < 0 ) {
346 LOGW ( "Open %s error(%s)!\n", file_name, strerror ( errno ) );
347 return -1;
348 }
349
350 memset ( &gTvinVDINParam, 0, sizeof ( gTvinVDINParam ) );
351 memset ( &gTvinVDINSignalInfo, 0, sizeof ( gTvinVDINSignalInfo ) );
352
353 LOGD ( "Open vdin module vdin_dev_fd = [%d]", m_vdin_dev_fd );
354
355 return m_vdin_dev_fd;
356}
357
358int CTvin::VDIN_GetVdinFd()
359{
360 return m_vdin_dev_fd;
361}
362
363int CTvin::VDIN_CloseModule()
364{
365 if ( m_vdin_dev_fd != -1 ) {
366 close ( m_vdin_dev_fd );
367 m_vdin_dev_fd = -1;
368 }
369
370 return 0;
371}
372
373int CTvin::VDIN_DeviceIOCtl ( int request, ... )
374{
375 int tmp_ret = -1;
376 va_list ap;
377 void *arg;
378
379 if ( m_vdin_dev_fd >= 0 ) {
380 va_start ( ap, request );
381 arg = va_arg ( ap, void * );
382 va_end ( ap );
383
384 tmp_ret = ioctl ( m_vdin_dev_fd, request, arg );
385 return tmp_ret;
386 }
387
388 return -1;
389}
390
391int CTvin::VDIN_OpenPort ( tvin_port_t port )
392{
393 int rt = -1;
394 struct tvin_parm_s vdinParam;
395 vdinParam.port = port;
396 vdinParam.index = 0;
397 rt = VDIN_DeviceIOCtl ( TVIN_IOC_OPEN, &vdinParam );
398
399 if ( rt < 0 ) {
400 LOGW ( "Vdin open port, error(%s)!", strerror ( errno ) );
401 }
402
403 return rt;
404}
405
406int CTvin::VDIN_ClosePort()
407{
408 int rt = -1;
409
410 rt = VDIN_DeviceIOCtl ( TVIN_IOC_CLOSE );
411
412 if ( rt < 0 ) {
413 LOGW ( "Vdin close port, error(%s)!", strerror ( errno ) );
414 }
415
416 return rt;
417}
418
419int CTvin::VDIN_StartDec ( const struct tvin_parm_s *vdinParam )
420{
421 int rt = -1;
422
423 if ( vdinParam == NULL ) {
424 return -1;
425 }
426
427 rt = VDIN_DeviceIOCtl ( TVIN_IOC_START_DEC, vdinParam );
428
429 LOGD ( "VDIN_StartDec:\n" );
430 LOGD ( "index = [%d]\n", vdinParam->index );
431 LOGD ( "port = [0x%x]\n", ( unsigned int ) vdinParam->port );
432 LOGD ( "format = [0x%x]\n", ( unsigned int ) ( vdinParam->info.fmt ) );
433
434 if ( rt < 0 ) {
435 LOGW ( "Vdin start decode, error(%s)!\n", strerror ( errno ) );
436 }
437
438 return rt;
439}
440
441int CTvin::VDIN_StopDec()
442{
443 int rt = -1;
444
445 rt = VDIN_DeviceIOCtl ( TVIN_IOC_STOP_DEC );
446
447 if ( rt < 0 ) {
448 LOGW ( "Vdin stop decode, error(%s)", strerror ( errno ) );
449 }
450
451 return rt;
452}
453
454int CTvin::VDIN_GetSignalInfo ( struct tvin_info_s *SignalInfo )
455{
456 int rt = -1;
457
458 rt = VDIN_DeviceIOCtl ( TVIN_IOC_G_SIG_INFO, SignalInfo );
459
460 if ( rt < 0 ) {
461 LOGW ( "Vdin get signal info, error(%s), ret = %d.\n", strerror ( errno ), rt );
462 //system("reboot");
463 }
464
465 return rt;
466}
467
468int CTvin::VDIN_SetVdinParam ( const struct tvin_parm_s *vdinParam )
469{
470 int rt = -1, i = 0;
471
472 rt = VDIN_DeviceIOCtl ( TVIN_IOC_S_PARM, vdinParam );
473
474 if ( rt < 0 ) {
475 LOGW ( "Vdin set signal param, error(%s)\n", strerror ( errno ) );
476 }
477
478 return rt;
479}
480
481int CTvin::VDIN_GetVdinParam ( const struct tvin_parm_s *vdinParam )
482{
483 int rt = -1, i = 0;
484
485 rt = VDIN_DeviceIOCtl ( TVIN_IOC_G_PARM, vdinParam );
486
487 if ( rt < 0 ) {
488 LOGW ( "Vdin get signal param, error(%s)\n", strerror ( errno ) );
489 }
490
491 return rt;
492}
493
494int CTvin::VDIN_OnoffVScaler ( int isOn )
495{
496 FILE *fp = NULL;
497
498 if ( isOn == 1 ) {
499 isOn = 1;
500 } else {
501 isOn = 0;
502 }
503
504 fp = fopen ( "/sys/class/video/vscaler", "w" );
505 if ( fp == NULL ) {
506 LOGW ( "Open /sys/class/video/vscaler error(%s)!\n", strerror ( errno ) );
507 return -1;
508 }
509
510 fprintf ( fp, "%d", ( int ) isOn );
511 fclose ( fp );
512 fp = NULL;
513
514 return 0;
515}
516
517int CTvin::VDIN_GetDisplayVFreq (void)
518{
519 int fd = -1;
520 char buf[32];
521
522 fd = open("/sys/class/display/mode", O_RDWR);
523 if (fd < 0) {
524 LOGW("Open /sys/class/display/mode error(%s)!\n", strerror(errno));
525 return -1;
526 }
527 memset(buf, 0, sizeof(buf));
528 read(fd, buf, sizeof(buf));
529 close(fd);
530 LOGD( "s%, VDIN_GetDisplayVFreq is %s\n", __FUNCTION__, buf);
531 if (strstr(buf, "50hz") != NULL) {
532 return 50;
533 } else if (strstr(buf, "60hz") != 0) {
534 return 60;
535 } else {
536 return 50;
537 }
538}
539
540int CTvin::VDIN_SetDisplayVFreq ( int freq, int display_resolution , bool isFbc)
541{
542 FILE *fp = NULL;
543 const char *config_value = NULL;
544 static int display_mode_type = -1;
545 CFbcCommunication *pFBC = NULL;
546 static int last_freq = 50;
547
548 last_freq = VDIN_GetDisplayVFreq();
549 if (last_freq == freq) {
550 LOGD ( "%s, same freq, last_freq = %d, freq = %d.", __FUNCTION__, last_freq, freq);
551 return 0;
552 } else {
553 LOGD ( "%s, from last_freq[%d[ to new_freq[%d].", __FUNCTION__, last_freq, freq);
554 last_freq = freq;
555 }
556
557 if (display_mode_type == -1) {
558 config_value = config_get_str ( CFG_SECTION_TV, "tvin.display.mode.type", "null" );
559 if (strcasecmp(config_value, "null") == 0 || strcasecmp(config_value, "hdmi_out") == 0) {
560 display_mode_type = 0;
561 } else if (strcasecmp(config_value, "lvds_out") == 0) {
562 display_mode_type = 1;
563 } else {
564 display_mode_type = 0;
565 }
566 }
567
568 fp = fopen ( "/sys/class/display/mode", "w" );
569
570 if ( fp == NULL ) {
571 LOGW ( "Open /sys/class/display/mode error(%s)!\n", strerror ( errno ) );
572 return -1;
573 }
574
575 if ( isFbc ) {
576 pFBC = GetSingletonFBC();
577 if (pFBC != NULL) {
578 pFBC->cfbc_Set_VMute (COMM_DEV_SERIAL, 2);
579 usleep ( 300000 );
580 }
581 }
582
583 switch ( display_resolution ) {
584 case VPP_DISPLAY_RESOLUTION_1366X768:
585 if ( freq == 50 ) {
586 fprintf ( fp, "%s", "768p50hz" );
587 } else {
588 fprintf ( fp, "%s", "768p60hz" );
589 }
590 break;
591 case VPP_DISPLAY_RESOLUTION_3840X2160:
592 if ( freq == 50 ) {
593 if (isFbc) {
594 fprintf ( fp, "%s", "2160p50hz420" );
595 } else {
596 fprintf ( fp, "%s", "2160p50hz" );
597 }
598 } else {
599 if (isFbc) {
600 fprintf ( fp, "%s", "2160p60hz420" );
601 } else {
602 fprintf ( fp, "%s", "2160p60hz" );
603 }
604 }
605 break;
606 case VPP_DISPLAY_RESOLUTION_1920X1080:
607 default:
608 if ( freq == 50 ) {
609 fprintf ( fp, "%s", "1080p50hz" );
610 } else {
611 fprintf ( fp, "%s", "1080p60hz" );
612 }
613 break;
614 }
615
616 fclose ( fp );
617 fp = NULL;
618
619 return 0;
620}
621
622void CTvin::Tvin_SetDepthOf2Dto3D ( int value )
623{
624 //value = -16~16
625 int tmp_value = DepthTable_2DTO3D[value + 16];
626 LOGD ( "%s, value: %d", __FUNCTION__, value );
627 VDIN_Set2D3DDepth ( value );
628}
629
630
631int CTvin::VDIN_Set2D3DDepth ( int count )
632{
633 FILE *fp = NULL;
634
635
636 fp = fopen ( "/sys/module/d2d3/parameters/depth", "w" );
637
638 if ( fp == NULL ) {
639 LOGW ( "Open /sys/module/d2d3/parameters/depth ERROR(%s)!!\n", strerror ( errno ) );
640 return -1;
641 }
642
643 if ( count >= -127 && count <= 127 ) {
644#ifdef LOGD_3D_FUNCTION
645 LOGD ( "set depth value (%d).\n", count );
646#endif
647 } else {
648 count = 8 * 12;
649 LOGE ( "set depth value ERROR!! set default depth.\n" );
650 }
651
652 fprintf ( fp, "%d", count );
653 fclose ( fp );
654 fp = NULL;
655
656 return 0;
657}
658
659int CTvin::VDIN_Set2Dto3D ( int on_off )
660{
661 struct tvin_parm_s VdinParam;
662 VDIN_GetVdinParam ( &VdinParam );
663 VdinParam.flag &= ( ~TVIN_PARM_FLAG_2D_TO_3D );
664 VdinParam.flag |= ( on_off ) ? ( TVIN_PARM_FLAG_2D_TO_3D ) : ( 0 );
665 return VDIN_SetVdinParam ( &VdinParam );
666}
667
668int CTvin::VDIN_Get_avg_luma(void)
669{
670 unsigned int lum_sum, pixel_sum, luma_avg;
671 struct tvin_parm_s vdinParam;
672
673 if ( 0 == VDIN_GetVdinParam( &vdinParam )) {
674 lum_sum = vdinParam.luma_sum;
675 pixel_sum = vdinParam.pixel_sum * 2;
676 if (pixel_sum != 0 && m_is_decoder_start) {
677 luma_avg = lum_sum / pixel_sum;
678 } else {
679 luma_avg = 116;
680 }
681 } else {
682 return -1;
683 }
684 LOGD ( "VDIN_get_avg_lut lum_sum =%d,pixel_sum=%d,lut_avg=%d\n", lum_sum, pixel_sum, luma_avg);
685 return luma_avg;
686}
687
688int CTvin::VDIN_GetHistgram ( int *hisgram )
689{
690 int i = 0;
691 struct tvin_parm_s vdinParam;
692
693 if ( NULL == hisgram ) {
694 return -1;
695 }
696
697 if ( 0 == VDIN_GetVdinParam ( &vdinParam ) ) {
698 for ( i = 0; i < CC_HIST_GRAM_BUF_SIZE; i++ ) {
699 hisgram[i] = ( int ) vdinParam.histgram[i];
700 }
701 } else {
702 return -1;
703 }
704
705 return 0;
706}
707
708int CTvin::VDIN_SetMVCViewMode ( int mode )
709{
710 FILE *fp = NULL;
711
712
713 fp = fopen ( "/sys/module/amvdec_h264mvc/parameters/view_mode", "w" );
714
715 if ( fp == NULL ) {
716 LOGW ( "Open /sys/module/amvdec_h264mvc/parameters/view_mode error(%s)!\n", strerror ( errno ) );
717 return -1;
718 }
719
720 fprintf ( fp, "%d", ( int ) mode );
721
722 fclose ( fp );
723 fp = NULL;
724
725 return 0;
726}
727
728int CTvin::VDIN_GetMVCViewMode ( void )
729{
730 FILE *fp = NULL;
731 int ret = 0;
732 int mode = 0;
733
734
735 fp = fopen ( "/sys/module/amvdec_h264mvc/parameters/view_mode", "w" );
736
737 if ( fp == NULL ) {
738 LOGW ( "Open /sys/module/amvdec_h264mvc/parameters/view_mode ERROR(%s)!!\n", strerror ( errno ) );
739 return -1;
740 }
741
742 ret = fread ( &mode, 1, 1, fp );
743 LOGD ( "fread /sys/module/amvdec_h264mvc/parameters/view_mode = [%d]", mode );
744
745 fclose ( fp );
746 fp = NULL;
747
748 return mode;
749}
750
751int CTvin::VDIN_SetDIBuffMgrMode ( int mgr_mode )
752{
753 FILE *fp = NULL;
754
755
756 fp = fopen ( "sys/module/di/parameters/buf_mgr_mode", "w" );
757
758 if ( fp == NULL ) {
759 LOGW ( "Open /sys/module/di/parameters/buf_mgr_mode error(%s)!\n", strerror ( errno ) );
760 return -1;
761 }
762
763 fprintf ( fp, "%d", mgr_mode );
764
765 fclose ( fp );
766 fp = NULL;
767
768 return 0;
769}
770
771int CTvin::VDIN_SetDICFG ( int cfg )
772{
773 FILE *fp = NULL;
774
775
776 fp = fopen ( "sys/class/deinterlace/di0/config", "w" );
777
778 if ( fp == NULL ) {
779 LOGW ( "Open /sys/class/deinterlace/di0/config error(%s)!\n", strerror ( errno ) );
780 return -1;
781 }
782
783 if ( 0 == cfg ) {
784 fprintf ( fp, "%s", "disable" );
785 } else {
786 fprintf ( fp, "%s", "enable" );
787 }
788
789 fclose ( fp );
790 fp = NULL;
791
792 return 0;
793}
794
795int CTvin::VDIN_SetDI3DDetc ( int enable )
796{
797 FILE *fp = NULL;
798
799
800 fp = fopen ( "/sys/module/di/parameters/det3d_en", "w" );
801
802 if ( fp == NULL ) {
803 LOGW ( "Open /sys/module/di/parameters/det3d_en error(%s)!\n", strerror ( errno ) );
804 return -1;
805 }
806
807 fprintf ( fp, "%d", enable );
808
809
810 fclose ( fp );
811 fp = NULL;
812
813 return 0;
814}
815
816int CTvin::VDIN_Get3DDetc ( void )
817{
818 int fd = -1;
819 int ret = -1;
820 char buf[10];
821
822 fd = open ( "/sys/module/di/parameters/det3d_en", O_RDWR );
823 if ( fd < 0 ) {
824 LOGW ( "Open /sys/module/di/parameters/det3d_en error(%s)!\n", strerror ( errno ) );
825 return -1;
826 }
827
828 ret = read ( fd, buf, sizeof ( buf ) );
829 close ( fd );
830 fd = -1;
831
832 if ( strcmp ( "enable", buf ) == 0 ) {
833 return 1;
834 } else {
835 return 0;
836 }
837}
838
839
840int CTvin::VDIN_GetVscalerStatus ( void )
841{
842 int fd = -1;
843 int ret = -1;
844 char buf[7];
845
846 fd = open ( "/sys/class/video/vscaler", O_RDWR );
847 if ( fd < 0 ) {
848 LOGW ( "Open /sys/class/video/vscaler error(%s)!\n", strerror ( errno ) );
849 return -1;
850 }
851
852 ret = read ( fd, buf, sizeof ( buf ) );
853 close ( fd );
854 fd = -1;
855
856 sscanf ( buf, "%d", &ret );
857
858 ret = ( ( ret & 0x40000 ) == 0 ) ? 1 : 0;
859 if ( ret == 1 ) {
860 sleep ( 1 );
861 }
862
863 return ret;
864}
865
866int CTvin::VDIN_TurnOnBlackBarDetect ( int isEnable )
867{
868 FILE *fp = NULL;
869
870 fp = fopen ( "/sys/module/tvin_vdin/parameters/black_bar_enable", "w" );
871 if ( fp == NULL ) {
872 LOGW ( "Open /sys/module/tvin_vdin/parameters/black_bar_enable error(%s)!\n", strerror ( errno ) );
873 return -1;
874 }
875
876 fprintf ( fp, "%d", isEnable );
877 fclose ( fp );
878 fp = NULL;
879
880 return 0;
881}
882
883int CTvin::VDIN_LoadHdcpKey ( unsigned char *hdcpkey_buff )
884{
885 unsigned char testHdcp[368] = { 0x53, 0x4B, 0x59, 0x01, 0x00, 0x10, 0x0D, 0x15, 0x3A, 0x8E, 0x99, 0xEE, 0x2A, 0x55, 0x58, 0xEE, 0xED, 0x4B, 0xBE, 0x00, 0x74, 0xA9, 0x00, 0x10, 0x0A, 0x21, 0xE3,
886 0x30, 0x66, 0x34, 0xCE, 0x9C, 0xC7, 0x8B, 0x51, 0x27, 0xF9, 0x0B, 0xAD, 0x09, 0x5F, 0x4D, 0xC2, 0xCA, 0xA2, 0x13, 0x06, 0x18, 0x8D, 0x34, 0x82, 0x46, 0x2D, 0xC9, 0x4B, 0xB0, 0x1C, 0xDE,
887 0x3D, 0x49, 0x39, 0x58, 0xEF, 0x2B, 0x68, 0x39, 0x71, 0xC9, 0x4D, 0x25, 0xE9, 0x75, 0x4D, 0xAC, 0x62, 0xF5, 0xF5, 0x87, 0xA0, 0xB2, 0x4A, 0x60, 0xD3, 0xF1, 0x09, 0x3A, 0xB2, 0x3E, 0x19,
888 0x4F, 0x3B, 0x1B, 0x2F, 0x85, 0x14, 0x28, 0x44, 0xFC, 0x69, 0x6F, 0x50, 0x42, 0x81, 0xBF, 0x7C, 0x2B, 0x3A, 0x17, 0x2C, 0x15, 0xE4, 0x93, 0x77, 0x74, 0xE8, 0x1F, 0x1C, 0x38, 0x54, 0x49,
889 0x10, 0x64, 0x5B, 0x7D, 0x90, 0x3D, 0xA0, 0xE1, 0x8B, 0x67, 0x5C, 0x19, 0xE6, 0xCA, 0x9D, 0xE9, 0x68, 0x5A, 0xB5, 0x62, 0xDF, 0xA1, 0x28, 0xBC, 0x68, 0x82, 0x9A, 0x22, 0xC4, 0xDC, 0x48,
890 0x85, 0x0F, 0xF1, 0x3E, 0x05, 0xDD, 0x1B, 0x2D, 0xF5, 0x49, 0x3A, 0x15, 0x29, 0xE7, 0xB6, 0x0B, 0x2A, 0x40, 0xE3, 0xB0, 0x89, 0xD5, 0x75, 0x84, 0x2E, 0x76, 0xE7, 0xBC, 0x63, 0x67, 0xE3,
891 0x57, 0x67, 0x86, 0x81, 0xF4, 0xD7, 0xEA, 0x4D, 0x89, 0x8E, 0x37, 0x95, 0x59, 0x1C, 0x8A, 0xCD, 0x79, 0xF8, 0x4F, 0x82, 0xF2, 0x6C, 0x7E, 0x7F, 0x79, 0x8A, 0x6B, 0x90, 0xC0, 0xAF, 0x4C,
892 0x8D, 0x43, 0x47, 0x1F, 0x9A, 0xF1, 0xBB, 0x88, 0x64, 0x49, 0x14, 0x50, 0xD1, 0xC3, 0xDF, 0xA6, 0x87, 0xA0, 0x15, 0x98, 0x51, 0x81, 0xF5, 0x97, 0x55, 0x10, 0x4A, 0x99, 0x30, 0x54, 0xA4,
893 0xFC, 0xDA, 0x0E, 0xAC, 0x6A, 0xFA, 0x90, 0xEE, 0x12, 0x70, 0x69, 0x74, 0x63, 0x46, 0x63, 0xFB, 0xE6, 0x1F, 0x72, 0xEC, 0x43, 0x5D, 0x50, 0xFF, 0x03, 0x4F, 0x05, 0x33, 0x88, 0x36, 0x93,
894 0xE4, 0x72, 0xD5, 0xCC, 0x34, 0x52, 0x96, 0x15, 0xCE, 0xD0, 0x32, 0x52, 0x41, 0x4F, 0xBC, 0x2D, 0xDF, 0xC5, 0xD6, 0x7F, 0xD5, 0x74, 0xCE, 0x51, 0xDC, 0x10, 0x5E, 0xF7, 0xAA, 0x4A, 0x2D,
895 0x20, 0x9A, 0x17, 0xDD, 0x30, 0x89, 0x71, 0x82, 0x36, 0x50, 0x09, 0x1F, 0x7C, 0xF3, 0x12, 0xE9, 0x43, 0x10, 0x5F, 0x51, 0xBF, 0xB8, 0x45, 0xA8, 0x5A, 0x8D, 0x3F, 0x77, 0xE5, 0x96, 0x73,
896 0x68, 0xAB, 0x73, 0xE5, 0x4C, 0xFB, 0xE5, 0x98, 0xB9, 0xAE, 0x74, 0xEB, 0x51, 0xDB, 0x91, 0x07, 0x7B, 0x66, 0x02, 0x9B, 0x79, 0x03, 0xC5, 0x34, 0x1C, 0x58, 0x13, 0x31, 0xD2, 0x4A, 0xEC
897 };
898 int ret = -1;
899 int fd = -1;
900
901 fd = open ( "/sys/class/hdmirx/hdmirx0/edid", O_RDWR );
902 if ( fd < 0 ) {
903 LOGW ( "Open hdmi hdcp key error(%s)!!\n", strerror ( errno ) );
904 return -1;
905 }
906
907 ret = write ( fd, testHdcp, 368 );
908 if ( ret < 0 ) {
909 LOGD ( "Write hdmi hdcp key error(%s)!!\n", strerror ( errno ) );
910 }
911
912 close ( fd );
913 fd = -1;
914
915 return ret;
916}
917
918int CTvin::VDIN_KeepLastFrame ( int enable )
919{
920 return 0;
921 /*
922 FILE *fp = NULL;
923 fp = fopen ( "/sys/module/amvideo/parameters/keep_old_frame", "w" );
924
925 if ( fp == NULL ) {
926 LOGW ( "Open /sys/module/amvideo/parameters/keep_old_frame error(%s)!\n", strerror ( errno ) );
927 return -1;
928 }
929
930 fprintf ( fp, "%d", enable );
931
932 fclose ( fp );
933 fp = NULL;
934
935 return 0;
936 */
937}
938
939int CTvin::VDIN_SetVideoFreeze ( int enable )
940{
941 FILE *fp = NULL;
942
943 fp = fopen ( "/sys/class/vdin/vdin0/attr", "w" );
944 if ( fp == NULL ) {
945 LOGW ( "Open /sys/class/vdin/vdin0/attr error(%s)!\n", strerror ( errno ) );
946 return -1;
947 }
948
949 if ( enable == 1 ) {
950 fprintf ( fp, "freeze" );
951 } else {
952 fprintf ( fp, "unfreeze" );
953 }
954
955 fclose ( fp );
956 fp = NULL;
957
958 return 0;
959}
960
961int CTvin::VDIN_SetDIBypasshd ( int enable )
962{
963 FILE *fp = NULL;
964
965 fp = fopen ( "/sys/module/di/parameters/bypass_hd", "w" );
966 if ( fp == NULL ) {
967 LOGW ( "Open /sys/module/di/parameters/bypass_hd error(%s)!\n", strerror ( errno ) );
968 return -1;
969 }
970
971 fprintf ( fp, "%d", enable );
972 fclose ( fp );
973 fp = NULL;
974
975 return 0;
976}
977
978int CTvin::VDIN_SetDIBypassAll ( int enable )
979{
980 FILE *fp = NULL;
981
982 fp = fopen ( "/sys/module/di/parameters/bypass_all", "w" );
983 if ( fp == NULL ) {
984 LOGW ( "Open /sys/module/di/parameters/bypass_all error(%s)!\n", strerror ( errno ) );
985 return -1;
986 }
987
988 fprintf ( fp, "%d", enable );
989 fclose ( fp );
990 fp = NULL;
991 return 0;
992}
993
994int CTvin::VDIN_SetDIBypass_Get_Buf_Threshold ( int enable )
995{
996 FILE *fp = NULL;
997
998 fp = fopen ( "/sys/module/di/parameters/bypass_get_buf_threshold", "w" );
999 if ( fp == NULL ) {
1000 LOGW ( "Open /sys/module/di/parameters/bypass_get_buf_threshold error(%s)!\n", strerror ( errno ) );
1001 return -1;
1002 }
1003
1004 fprintf ( fp, "%d", enable );
1005 fclose ( fp );
1006 fp = NULL;
1007
1008 return 0;
1009
1010}
1011
1012int CTvin::VDIN_SetDIBypassProg ( int enable )
1013{
1014 FILE *fp = NULL;
1015
1016 fp = fopen ( "/sys/module/di/parameters/bypass_prog", "w" );
1017 if ( fp == NULL ) {
1018 LOGW ( "Open /sys/module/di/parameters/bypass_prog error(%s)!\n", strerror ( errno ) );
1019 return -1;
1020 }
1021
1022 fprintf ( fp, "%d", enable );
1023 fclose ( fp );
1024 fp = NULL;
1025
1026 return 0;
1027}
1028
1029int CTvin::VDIN_SetDIBypassDynamic ( int flag )
1030{
1031 FILE *fp = NULL;
1032
1033 fp = fopen ( "/sys/module/di/parameters/bypass_dynamic", "w" );
1034 if ( fp == NULL ) {
1035 LOGW ( "Open /sys/module/di/parameters/bypass_dynamic error(%s)!\n", strerror ( errno ) );
1036 return -1;
1037 }
1038
1039 fprintf ( fp, "%d", flag );
1040 fclose ( fp );
1041 fp = NULL;
1042
1043 return 0;
1044}
1045
1046int CTvin::VDIN_SetDIDet3DMode ( int value )
1047{
1048 FILE *fp = NULL;
1049
1050 fp = fopen ( "/sys/module/di/parameters/det3d_mode", "w" );
1051 if ( fp == NULL ) {
1052 LOGW ( "Open /sys/module/di/parameters/det3d_mode error(%s)!\n", strerror ( errno ) );
1053 return -1;
1054 }
1055
1056 fprintf ( fp, "%d", value );
1057 fclose ( fp );
1058 fp = NULL;
1059
1060 return 0;
1061}
1062
1063int CTvin::VDIN_SetDIBypass3D ( int enable )
1064{
1065 FILE *fp = NULL;
1066
1067 fp = fopen ( "/sys/module/di/parameters/bypass_3d", "w" );
1068
1069 if ( fp == NULL ) {
1070 LOGW ( "Open /sys/module/di/parameters/bypass_3d error(%s)!\n", strerror ( errno ) );
1071 return -1;
1072 }
1073
1074 fprintf ( fp, "%d", enable );
1075 fclose ( fp );
1076 fp = NULL;
1077
1078 return 0;
1079}
1080
1081int CTvin::VDIN_SetDIBypassPost ( int enable )
1082{
1083 FILE *fp = NULL;
1084
1085 fp = fopen ( "/sys/module/di/parameters/bypass_post", "w" );
1086 if ( fp == NULL ) {
1087 LOGW ( "Open /sys/module/di/parameters/bypass_post error(%s)!\n", strerror ( errno ) );
1088 return -1;
1089 }
1090
1091 fprintf ( fp, "%d", enable );
1092 fclose ( fp );
1093 fp = NULL;
1094
1095 return 0;
1096}
1097
1098int CTvin::set3D_FL_Frame(int value)
1099{
1100 FILE *fp = NULL;
1101 fp = fopen ( "/sys/module/amvideo/parameters/pause_one_3d_fl_frame", "w" );
1102 if ( fp == NULL ) {
1103 LOGW ( "Open /sys/module/amvideo/parameters/pause_one_3d_fl_frame error(%s)!\n", strerror ( errno ) );
1104 return -1;
1105 }
1106 fprintf ( fp, "%d", value );
1107 fclose ( fp );
1108 fp = NULL;
1109 return 0;
1110}
1111
1112int CTvin::setLatchFlag(int value)
1113{
1114 int fd = -1;
1115 int nodeVal = 0;
1116 char s[10];
1117 FILE *fp = NULL;
1118 memset(s, 0, sizeof(s));
1119 read(fd, s, sizeof(s));
1120 close(fd);
1121 nodeVal = atoi(s);
1122 fp = fopen ( "/sys/module/am_vecm/parameters/vecm_latch_flag", "w" );
1123 if ( fp == NULL ) {
1124 LOGW ( "/sys/module/am_vecm/parameters/vecm_latch_flag error(%s)!\n", strerror ( errno ) );
1125 return -1;
1126 }
1127 fprintf ( fp, "%d", value | nodeVal);
1128 fclose ( fp );
1129 fp = NULL;
1130 LOGD ( "read vecm_latch_flag = (%s)====(0x%x), then set vecm_latch_flag | 0x%x = 0x%x.\n", s, nodeVal, value, value | nodeVal);
1131 return 0;
1132}
1133
1134int CTvin::VDIN_SetDIProg_Proc_Config ( int value )
1135{
1136 FILE *fp = NULL;
1137
1138 fp = fopen ( "/sys/module/di/parameters/prog_proc_config", "w" );
1139 if ( fp == NULL ) {
1140 LOGW ( "Open /sys/module/di/parameters/prog_proc_config error(%s)!\n", strerror ( errno ) );
1141 return -1;
1142 }
1143
1144 fprintf ( fp, "%d", value );
1145 fclose ( fp );
1146 fp = NULL;
1147
1148 return 0;
1149}
1150
1151#if(1)
1152int CTvin::VDIN_SetDIInput2Pre ( int value )
1153{
1154 FILE *fp = NULL;
1155
1156 fp = fopen ( "/sys/module/di/parameters/input2pre", "w" );
1157 if ( fp == NULL ) {
1158 LOGW ( "Open /sys/module/di/parameters/input2pre error(%s)!\n", strerror ( errno ) );
1159 return -1;
1160 }
1161
1162 fprintf ( fp, "%d", value );
1163 fclose ( fp );
1164 fp = NULL;
1165
1166 return 0;
1167
1168}
1169#endif
1170int CTvin::VDIN_SetVdinFlag ( int flag )
1171{
1172 FILE *fp = NULL;
1173 int freq = 1200000;
1174
1175 fp = fopen ( "/sys/class/vdin/memp", "w" );
1176 if ( fp == NULL ) {
1177 LOGW ( "Open /sys/class/vdin/memp error(%s)!\n", strerror ( errno ) );
1178 return -1;
1179 }
1180
1181 fprintf ( fp, "%d", flag );
1182 fclose ( fp );
1183 fp = NULL;
1184
1185 return 0;
1186}
1187
1188int CTvin::VDIN_EnableRDMA ( int enable )
1189{
1190 FILE *fp = NULL;
1191 fp = fopen ( "/sys/module/rdma/parameters/enable", "w" );
1192
1193 if ( fp == NULL ) {
1194 LOGW ( "Open /sys/module/rdma/parameters/enable error(%s)!\n", strerror ( errno ) );
1195 return -1;
1196 }
1197
1198 fprintf ( fp, "%d", enable );
1199 fclose ( fp );
1200 fp = NULL;
1201 return 0;
1202}
1203
1204// AFE
1205int CTvin::AFE_OpenModule ( void )
1206{
1207 if ( afe_dev_fd < 0 ) {
1208 afe_dev_fd = open ( AFE_DEV_PATH, O_RDWR );
1209
1210 if ( afe_dev_fd < 0 ) {
1211 LOGW ( "Open tvafe module, error(%s).\n", strerror ( errno ) );
1212 return -1;
1213 }
1214 }
1215
1216 return afe_dev_fd;
1217}
1218
1219void CTvin::AFE_CloseModule ( void )
1220{
1221 if ( afe_dev_fd >= 0 ) {
1222 close ( afe_dev_fd );
1223 afe_dev_fd = -1;
1224 }
1225
1226 return;
1227}
1228
1229int CTvin::AFE_DeviceIOCtl ( int request, ... )
1230{
1231 int tmp_ret = -1;
1232 va_list ap;
1233 void *arg;
1234
1235 if ( afe_dev_fd >= 0 ) {
1236 va_start ( ap, request );
1237 arg = va_arg ( ap, void * );
1238 va_end ( ap );
1239
1240 tmp_ret = ioctl ( afe_dev_fd, request, arg );
1241
1242 return tmp_ret;
1243 }
1244
1245 return -1;
1246}
1247
1248int CTvin::AFE_GetDeviceFileHandle()
1249{
1250 return afe_dev_fd;
1251}
1252
1253int CTvin::AFE_SetVGAEdid ( const unsigned char *ediddata )
1254{
1255 int rt = -1;
1256 struct tvafe_vga_edid_s vgaEdid;
1257
1258#ifdef NO_IC_TEST
1259
1260 for ( int i = 0; i < 256; i++ ) {
1261 test_edid[i] = ediddata[i];
1262 }
1263
1264#endif
1265
1266 for ( int i = 0; i < 256; i++ ) {
1267 vgaEdid.value[i] = ediddata[i];
1268 }
1269
1270 rt = AFE_DeviceIOCtl ( TVIN_IOC_S_AFE_VGA_EDID, &vgaEdid );
1271
1272 if ( rt < 0 ) {
1273 LOGW ( "AFE_SetVGAEdid, error(%s).!\n", strerror ( errno ) );
1274 }
1275
1276 return rt;
1277}
1278
1279int CTvin::AFE_GetVGAEdid ( unsigned char *ediddata )
1280{
1281 int rt = -1;
1282 struct tvafe_vga_edid_s vgaEdid;
1283
1284#ifdef NO_IC_TEST
1285
1286 for ( int i = 0; i < 256; i++ ) {
1287 ediddata[i] = test_edid[i];
1288 }
1289
1290 LOGD ( "AFE_GetVGAEdid:\n" );
1291 LOGD ( "===================================================\n" );
1292
1293 for ( int i = 0; i < 256; i++ ) {
1294 LOGD ( "vag edid[%d] = [0x%x].\n", i, ediddata[i] );
1295 }
1296
1297 LOGD ( "===================================================\n" );
1298#endif
1299
1300 rt = AFE_DeviceIOCtl ( TVIN_IOC_G_AFE_VGA_EDID, &vgaEdid );
1301
1302 for ( int i = 0; i < 256; i++ ) {
1303 ediddata[i] = vgaEdid.value[i];
1304 }
1305
1306 if ( rt < 0 ) {
1307 LOGW ( "AFE_GetVGAEdid, error(%s)!\n", strerror ( errno ) );
1308 }
1309
1310 return rt;
1311}
1312
1313int CTvin::AFE_SetADCTimingAdjust ( const struct tvafe_vga_parm_s *timingadj )
1314{
1315 int rt = -1;
1316
1317 if ( timingadj == NULL ) {
1318 return rt;
1319 }
1320
1321 rt = AFE_DeviceIOCtl ( TVIN_IOC_S_AFE_VGA_PARM, timingadj );
1322
1323 if ( rt < 0 ) {
1324 LOGW ( "AFE_SetADCTimingAdjust, error(%s)!\n", strerror ( errno ) );
1325 }
1326
1327 return rt;
1328}
1329
1330int CTvin::AFE_GetADCCurrentTimingAdjust ( struct tvafe_vga_parm_s *timingadj )
1331{
1332 int rt = -1;
1333
1334 if ( timingadj == NULL ) {
1335 return rt;
1336 }
1337
1338 rt = AFE_DeviceIOCtl ( TVIN_IOC_G_AFE_VGA_PARM, timingadj );
1339
1340 if ( rt < 0 ) {
1341 LOGW ( "AFE_GetADCCurrentTimingAdjust, error(%s)!\n", strerror ( errno ) );
1342 return -1;
1343 }
1344
1345 return 0;
1346}
1347
1348int CTvin::AFE_VGAAutoAdjust ( struct tvafe_vga_parm_s *timingadj )
1349{
1350 enum tvafe_cmd_status_e CMDStatus = TVAFE_CMD_STATUS_PROCESSING;
1351 struct tvin_parm_s tvin_para;
1352 int rt = -1, i = 0;
1353
1354 if ( timingadj == NULL ) {
1355 return -1;
1356 }
1357
1358 for ( i = 0, CMDStatus == TVAFE_CMD_STATUS_PROCESSING; i < 50; i++ ) {
1359 rt = AFE_DeviceIOCtl ( TVIN_IOC_G_AFE_CMD_STATUS, &CMDStatus );
1360
1361 if ( rt < 0 ) {
1362 LOGD ( "get afe CMD status, error(%s), fd(%d), return(%d).\n", strerror ( errno ), AFE_GetDeviceFileHandle(), rt );
1363 }
1364
1365 if ( ( CMDStatus == TVAFE_CMD_STATUS_IDLE ) || ( CMDStatus == TVAFE_CMD_STATUS_SUCCESSFUL ) ) {
1366 break;
1367 }
1368
1369 usleep ( 10 * 1000 );
1370 }
1371
1372 if ( ( CMDStatus == TVAFE_CMD_STATUS_PROCESSING ) || ( CMDStatus == TVAFE_CMD_STATUS_FAILED ) ) {
1373 return -1;
1374 }
1375
1376 for ( i = 0; i < 100; i++ ) {
1377 rt = VDIN_DeviceIOCtl ( TVIN_IOC_G_PARM, &tvin_para );
1378
1379 if ( tvin_para.info.status == TVIN_SIG_STATUS_STABLE ) {
1380 break;
1381 }
1382
1383 usleep ( 10 * 1000 );
1384 }
1385
1386 rt = AFE_DeviceIOCtl ( TVIN_IOC_S_AFE_VGA_AUTO );
1387
1388 if ( rt < 0 ) {
1389 timingadj->clk_step = 0;
1390 timingadj->phase = 0;
1391 timingadj->hpos_step = 0;
1392 timingadj->vpos_step = 0;
1393 AFE_DeviceIOCtl ( TVIN_IOC_S_AFE_VGA_PARM, timingadj );
1394 return rt;
1395 } else {
1396 ;//AFE_DeviceIOCtl(TVIN_IOC_G_AFE_VGA_PARM, timingadj);
1397 }
1398
1399 for ( i = 0; i < 10; i++ ) {
1400 sleep ( 1 );
1401
1402 rt = AFE_DeviceIOCtl ( TVIN_IOC_G_AFE_CMD_STATUS, &CMDStatus );
1403
1404 if ( rt < 0 ) {
1405 return rt;
1406 } else {
1407 if ( CMDStatus == TVAFE_CMD_STATUS_SUCCESSFUL ) {
1408 usleep ( 100 * 1000 );
1409 AFE_GetADCCurrentTimingAdjust ( timingadj );
1410 LOGD ( "===================================================\n" );
1411 LOGW ( "AFE_VGAAutoAdjust, successfull!\n" );
1412 return 0;
1413 }
1414 }
1415 }
1416
1417 return -1;
1418}
1419
1420int CTvin::AFE_SetVGAAutoAjust ( void )
1421{
1422 int rt = -1;
1423 tvafe_vga_parm_t timingadj;
1424 tvafe_cmd_status_t Status;
1425 rt = AFE_DeviceIOCtl ( TVIN_IOC_G_AFE_CMD_STATUS, &Status );
1426
1427 if ( ( Status == TVAFE_CMD_STATUS_IDLE ) || ( Status == TVAFE_CMD_STATUS_SUCCESSFUL ) ) {
1428 ;
1429 } else {
1430 LOGW ( "AFE_SetVGAAutoAjust, TVIN_IOC_G_AFE_CMD_STATUS failed!\n" );
1431 return -1;
1432 }
1433
1434 rt = AFE_DeviceIOCtl ( TVIN_IOC_S_AFE_VGA_AUTO );
1435
1436 if ( rt < 0 ) {
1437 timingadj.clk_step = 0;
1438 timingadj.phase = 0;
1439 timingadj.hpos_step = 0;
1440 timingadj.vpos_step = 0;
1441 AFE_DeviceIOCtl ( TVIN_IOC_S_AFE_VGA_PARM, &timingadj );
1442 return rt;
1443 }
1444
1445 return 0;
1446}
1447
1448int CTvin::AFE_GetVGAAutoAdjustCMDStatus ( tvafe_cmd_status_t *Status )
1449{
1450 int rt = -1;
1451
1452 if ( Status == NULL ) {
1453 return rt;
1454 }
1455
1456 rt = AFE_DeviceIOCtl ( TVIN_IOC_G_AFE_CMD_STATUS, Status );
1457
1458 if ( rt < 0 ) {
1459 LOGW ( "AFE_GetVGAAutoAdjustStatus, get status, error(%s) fd(%d) return(%d)\n", strerror ( errno ), AFE_GetDeviceFileHandle(), rt );
1460 return rt;
1461 }
1462
1463 return 0;
1464}
1465
1466int CTvin::AFE_GetAdcCal ( struct tvafe_adc_cal_s *adccalvalue )
1467{
1468 int rt = -1;
1469
1470 rt = AFE_DeviceIOCtl ( TVIN_IOC_G_AFE_ADC_CAL, adccalvalue );
1471
1472 if ( rt < 0 ) {
1473 LOGW ( "AFE_GetADCGainOffset, error(%s)!\n", strerror ( errno ) );
1474 }
1475
1476 return rt;
1477}
1478
1479int CTvin::AFE_SetAdcCal ( struct tvafe_adc_cal_s *adccalvalue )
1480{
1481 int rt = AFE_DeviceIOCtl ( TVIN_IOC_S_AFE_ADC_CAL, adccalvalue );
1482
1483 if ( rt < 0 ) {
1484 LOGW ( "AFE_SetAdcCal, error(%s)!", strerror ( errno ) );
1485 }
1486
1487 return rt;
1488}
1489
1490int CTvin::AFE_GetAdcCompCal ( struct tvafe_adc_comp_cal_s *adccalvalue )
1491{
1492 int rt = -1;
1493
1494 rt = AFE_DeviceIOCtl ( TVIN_IOC_G_AFE_ADC_COMP_CAL, adccalvalue );
1495
1496 if ( rt < 0 ) {
1497 LOGW ( "AFE_GetYPbPrADCGainOffset, error(%s)!\n", strerror ( errno ) );
1498 }
1499
1500 return rt;
1501}
1502
1503int CTvin::AFE_SetAdcCompCal ( struct tvafe_adc_comp_cal_s *adccalvalue )
1504{
1505 int rt = AFE_DeviceIOCtl ( TVIN_IOC_S_AFE_ADC_COMP_CAL, adccalvalue );
1506
1507 if ( rt < 0 ) {
1508 LOGW ( "AFE_SetYPbPrADCGainOffset, error(%s)!", strerror ( errno ) );
1509 }
1510
1511 return rt;
1512}
1513int CTvin::AFE_GetYPbPrWSSinfo ( struct tvafe_comp_wss_s *wssinfo )
1514{
1515 int rt = AFE_DeviceIOCtl ( TVIN_IOC_G_AFE_COMP_WSS, wssinfo );
1516
1517 if ( rt < 0 ) {
1518 LOGW ( "AFE_GetYPbPrWSSinfo, error(%s)!", strerror ( errno ) );
1519 }
1520
1521 return rt;
1522}
1523
1524#define RGB444 3
1525#define YCBCR422 2
1526#define YCBCR444 3
1527#define Y422_POS 0
1528#define CB422_POS 1
1529#define CR422_POS 3
1530#define Y444_POS 0
1531#define CB444_POS 1
1532#define CR444_POS 2
1533#define R444_POS 0
1534#define G444_POS 1
1535#define B444_POS 2
1536
1537//=========== VGA =====================
1538#define VGA_BUF_WID (VGA_H_ACTIVE)
1539
1540#ifdef PATTERN_7_COLOR_BAR
1541#define VGA_BAR_WID (VGA_H_ACTIVE/7)
1542#define VGA_H_CUT_WID (10)
1543#else
1544#define VGA_BAR_WID (VGA_H_ACTIVE/8)
1545#define VGA_H_CUT_WID (10)
1546#endif
1547
1548#define VGA_V_CUT_WID (40)
1549#define VGA_V_CAL_WID (200+VGA_V_CUT_WID)
1550
1551#define VGA_WHITE_HS (VGA_BAR_WID*0+VGA_H_CUT_WID)
1552#define VGA_WHITE_HE (VGA_BAR_WID*1-VGA_H_CUT_WID-1)
1553#define VGA_WHITE_VS (VGA_V_CUT_WID)
1554#define VGA_WHITE_VE (VGA_V_CAL_WID-1)
1555#define VGA_WHITE_SIZE ((VGA_WHITE_HE-VGA_WHITE_HS+1)*(VGA_WHITE_VE-VGA_WHITE_VS+1))
1556#ifdef PATTERN_7_COLOR_BAR
1557#define VGA_BLACK_HS (VGA_BAR_WID*6+VGA_H_CUT_WID)
1558#define VGA_BLACK_HE (VGA_BAR_WID*7-VGA_H_CUT_WID-1)
1559#define VGA_BLACK_VS (768-140)
1560#define VGA_BLACK_VE (768-40-1)
1561#define VGA_BLACK_SIZE ((VGA_BLACK_HE-VGA_BLACK_HS+1)*(VGA_BLACK_VE-VGA_BLACK_VS+1))
1562#else
1563#define VGA_BLACK_HS (VGA_BAR_WID*7+VGA_H_CUT_WID)
1564#define VGA_BLACK_HE (VGA_BAR_WID*8-VGA_H_CUT_WID-1)
1565#define VGA_BLACK_VS (VGA_V_CUT_WID)
1566#define VGA_BLACK_VE (VGA_V_CAL_WID-1)
1567#define VGA_BLACK_SIZE ((VGA_BLACK_HE-VGA_BLACK_HS+1)*(VGA_BLACK_VE-VGA_BLACK_VS+1))
1568#endif
1569
1570//=========== YPBPR =====================
1571#define COMP_BUF_WID (COMP_H_ACTIVE)
1572
1573#define COMP_BAR_WID (COMP_H_ACTIVE/8)
1574#define COMP_H_CUT_WID (20)
1575#define COMP_V_CUT_WID (100)
1576#define COMP_V_CAL_WID (200+COMP_V_CUT_WID)
1577
1578#define COMP_WHITE_HS (COMP_BAR_WID*0+COMP_H_CUT_WID)
1579#define COMP_WHITE_HE (COMP_BAR_WID*1-COMP_H_CUT_WID-1)
1580#define COMP_WHITE_VS (COMP_V_CUT_WID)
1581#define COMP_WHITE_VE (COMP_V_CAL_WID-1)
1582#define COMP_WHITE_SIZE ((COMP_WHITE_HE-COMP_WHITE_HS+1)*(COMP_WHITE_VE-COMP_WHITE_VS+1))
1583#define CB_WHITE_SIZE ((COMP_WHITE_HE-COMP_WHITE_HS+1)*(COMP_WHITE_VE-COMP_WHITE_VS+1)/2)
1584#define CR_WHITE_SIZE ((COMP_WHITE_HE-COMP_WHITE_HS+1)*(COMP_WHITE_VE-COMP_WHITE_VS+1)/2)
1585
1586#define COMP_YELLOW_HS (COMP_BAR_WID*1+COMP_H_CUT_WID)
1587#define COMP_YELLOW_HE (COMP_BAR_WID*2-COMP_H_CUT_WID-1)
1588#define COMP_YELLOW_VS (COMP_V_CUT_WID)
1589#define COMP_YELLOW_VE (COMP_V_CAL_WID-1)
1590#define COMP_YELLOW_SIZE ((COMP_YELLOW_HE-COMP_YELLOW_HS+1)*(COMP_YELLOW_VE-COMP_YELLOW_VS+1)/2)
1591
1592#define COMP_CYAN_HS (COMP_BAR_WID*2+COMP_H_CUT_WID)
1593#define COMP_CYAN_HE (COMP_BAR_WID*3-COMP_H_CUT_WID-1)
1594#define COMP_CYAN_VS (COMP_V_CUT_WID)
1595#define COMP_CYAN_VE (COMP_V_CAL_WID-1)
1596#define COMP_CYAN_SIZE ((COMP_CYAN_HE-COMP_CYAN_HS+1)*(COMP_CYAN_VE-COMP_CYAN_VS+1)/2)
1597
1598#define COMP_RED_HS (COMP_BAR_WID*5+COMP_H_CUT_WID)
1599#define COMP_RED_HE (COMP_BAR_WID*6-COMP_H_CUT_WID-1)
1600#define COMP_RED_VS (COMP_V_CUT_WID)
1601#define COMP_RED_VE (COMP_V_CAL_WID-1)
1602#define COMP_RED_SIZE ((COMP_RED_HE-COMP_RED_HS+1)*(COMP_RED_VE-COMP_RED_VS+1)/2)
1603
1604#define COMP_BLUE_HS (COMP_BAR_WID*6+COMP_H_CUT_WID)
1605#define COMP_BLUE_HE (COMP_BAR_WID*7-COMP_H_CUT_WID-1)
1606#define COMP_BLUE_VS (COMP_V_CUT_WID)
1607#define COMP_BLUE_VE (COMP_V_CAL_WID-1)
1608#define COMP_BLUE_SIZE ((COMP_BLUE_HE-COMP_BLUE_HS+1)*(COMP_BLUE_VE-COMP_BLUE_VS+1)/2)
1609
1610#define COMP_BLACK_HS (COMP_BAR_WID*7+COMP_H_CUT_WID)
1611#define COMP_BLACK_HE (COMP_BAR_WID*8-COMP_H_CUT_WID-1)
1612#define COMP_BLACK_VS (COMP_V_CUT_WID)
1613#define COMP_BLACK_VE (COMP_V_CAL_WID-1)
1614#define COMP_BLACK_SIZE ((COMP_BLACK_HE-COMP_BLACK_HS+1)*(COMP_BLACK_VE-COMP_BLACK_VS+1))
1615#define CB_BLACK_SIZE ((COMP_BLACK_HE-COMP_BLACK_HS+1)*(COMP_BLACK_VE-COMP_BLACK_VS+1)/2)
1616#define CR_BLACK_SIZE ((COMP_BLACK_HE-COMP_BLACK_HS+1)*(COMP_BLACK_VE-COMP_BLACK_VS+1)/2)
1617
1618//=========== CVBS =====================
1619#define CVBS_BUF_WID (CVBS_H_ACTIVE)
1620#define CVBS_BAR_WID (CVBS_H_ACTIVE/8)
1621#define CVBS_H_CUT_WID (20)
1622
1623#define CVBS_V_CUT_WID (40)
1624#define CVBS_V_CAL_WID (140+CVBS_V_CUT_WID)
1625
1626#define CVBS_WHITE_HS (CVBS_BAR_WID*0+CVBS_H_CUT_WID)
1627#define CVBS_WHITE_HE (CVBS_BAR_WID*1-CVBS_H_CUT_WID-1)
1628#define CVBS_WHITE_VS (CVBS_V_CUT_WID)
1629#define CVBS_WHITE_VE (CVBS_V_CAL_WID-1)
1630#define CVBS_WHITE_SIZE ((CVBS_WHITE_HE-CVBS_WHITE_HS+1)*(CVBS_WHITE_VE-CVBS_WHITE_VS+1))
1631
1632#define CVBS_BLACK_HS (CVBS_BAR_WID*7+CVBS_H_CUT_WID)
1633#define CVBS_BLACK_HE (CVBS_BAR_WID*8-CVBS_H_CUT_WID-1)
1634#define CVBS_BLACK_VS (CVBS_V_CUT_WID)
1635#define CVBS_BLACK_VE (CVBS_V_CAL_WID-1)
1636#define CVBS_BLACK_SIZE ((CVBS_BLACK_HE-CVBS_BLACK_HS+1)*(CVBS_BLACK_VE-CVBS_BLACK_VS+1))
1637
1638#define COMP_CAP_SIZE (COMP_H_ACTIVE*COMP_V_ACTIVE*YCBCR422)
1639#ifdef VGA_SOURCE_RGB444
1640#define VGA_CAP_SIZE (VGA_H_ACTIVE*VGA_V_ACTIVE*RGB444)
1641#else
1642#define VGA_CAP_SIZE (VGA_H_ACTIVE*VGA_V_ACTIVE*YCBCR444)
1643#endif
1644#define CVBS_CAP_SIZE (CVBS_H_ACTIVE*CVBS_V_ACTIVE)
1645
1646#define PRE_0 -16
1647#define PRE_1 -128
1648#define PRE_2 -128
1649#define COEF_00 1.164
1650#define COEF_01 0
1651#define COEF_02 1.793
1652#define COEF_10 1.164
1653#define COEF_11 -0.213
1654#define COEF_12 -0.534
1655#define COEF_20 1.164
1656#define COEF_21 2.115
1657#define COEF_22 0
1658#define POST_0 0
1659#define POST_1 0
1660#define POST_2 0
1661
1662unsigned int CTvin::data_limit ( float data )
1663{
1664 if ( data < 0 ) {
1665 return ( 0 );
1666 } else if ( data > 255 ) {
1667 return ( 255 );
1668 } else {
1669 return ( ( unsigned int ) data );
1670 }
1671}
1672
1673void CTvin::matrix_convert_yuv709_to_rgb ( unsigned int y, unsigned int u, unsigned int v, unsigned int *r, unsigned int *g, unsigned int *b )
1674{
1675 *r = data_limit ( ( ( float ) y + PRE_0 ) * COEF_00 + ( ( float ) u + PRE_1 ) * COEF_01 + ( ( float ) v + PRE_2 ) * COEF_02 + POST_0 + 0.5 );
1676 *g = data_limit ( ( ( float ) y + PRE_0 ) * COEF_10 + ( ( float ) u + PRE_1 ) * COEF_11 + ( ( float ) v + PRE_2 ) * COEF_12 + POST_1 + 0.5 );
1677 *b = data_limit ( ( ( float ) y + PRE_0 ) * COEF_20 + ( ( float ) u + PRE_1 ) * COEF_21 + ( ( float ) v + PRE_2 ) * COEF_22 + POST_2 + 0.5 );
1678}
1679
1680void CTvin::re_order ( unsigned int *a, unsigned int *b )
1681{
1682 unsigned int c = 0;
1683
1684 if ( *a > *b ) {
1685 c = *a;
1686 *a = *b;
1687 *b = c;
1688 }
1689}
1690
1691char *CTvin::get_cap_addr ( enum adc_cal_type_e calType )
1692{
1693 int n;
1694 char *dp;
1695
1696 for ( n = 0; n < 0x00ff; n++ ) {
1697 if ( VDIN_DeviceIOCtl ( TVIN_IOC_G_SIG_INFO, &gTvinAFESignalInfo ) < 0 ) {
1698 LOGW ( "get_cap_addr, get signal info, error(%s),fd(%d).\n", strerror ( errno ), m_vdin_dev_fd );
1699 return NULL;
1700 } else {
1701 if ( gTvinAFESignalInfo.status == TVIN_SIG_STATUS_STABLE ) {
1702 gTvinAFEParam.info.fmt = gTvinAFESignalInfo.fmt;
1703 break;
1704 }
1705 }
1706 }
1707
1708 if ( gTvinAFESignalInfo.status != TVIN_SIG_STATUS_STABLE ) {
1709 LOGD ( "get_cap_addr, signal isn't stable, out of calibration!\n" );
1710 return NULL;
1711 } else {
1712 if ( VDIN_DeviceIOCtl ( TVIN_IOC_STOP_DEC ) < 0 ) {
1713 LOGW ( "get_cap_addr, stop vdin, error (%s).\n", strerror ( errno ) );
1714 return NULL;
1715 }
1716
1717 usleep ( 1000 );
1718
1719 if ( calType == CAL_YPBPR ) {
1720 dp = ( char * ) mmap ( NULL, COMP_CAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, m_vdin_dev_fd, 0 );
1721 } else {
1722 dp = ( char * ) mmap ( NULL, VGA_CAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, m_vdin_dev_fd, 0 );
1723 }
1724
1725 if ( dp < 0 ) {
1726 LOGD ( "get_cap_addr, mmap failed!\n" );
1727 }
1728
1729 return dp;
1730 }
1731
1732 return NULL;
1733}
1734
1735inline unsigned char CTvin::get_mem_data ( char *dp, unsigned int addr )
1736{
1737 return ( * ( dp + ( addr ^ 7 ) ) );
1738}
1739
1740int CTvin::get_frame_average ( enum adc_cal_type_e calType, struct adc_cal_s *mem_data )
1741{
1742 unsigned int y = 0, cb = 0, cr = 0;
1743 unsigned int r = 0, g = 0, b = 0;
1744 unsigned long n;
1745 unsigned int i = 0, j = 0;
1746 char *dp = get_cap_addr ( calType );
1747
1748 if ( calType == CAL_YPBPR ) {
1749 for ( j = COMP_WHITE_VS; j <= COMP_WHITE_VE; j++ ) {
1750 for ( i = COMP_WHITE_HS; i <= COMP_WHITE_HE; i++ ) {
1751 mem_data->g_y_max += get_mem_data ( dp, ( ( COMP_BUF_WID * j + i ) * YCBCR422 ) );
1752 }
1753 }
1754
1755 mem_data->g_y_max /= COMP_WHITE_SIZE;
1756
1757 for ( j = COMP_WHITE_VS; j <= COMP_WHITE_VE; j++ ) {
1758 for ( i = COMP_WHITE_HS; i <= COMP_WHITE_HE; ) {
1759 mem_data->cb_white += get_mem_data ( dp, ( ( COMP_BUF_WID * j + i ) * YCBCR422 + CB422_POS ) );
1760 mem_data->cr_white += get_mem_data ( dp, ( ( COMP_BUF_WID * j + i ) * YCBCR422 + CR422_POS ) );
1761 i = i + 2;
1762 }
1763 }
1764
1765 mem_data->cb_white /= CB_WHITE_SIZE;
1766 mem_data->cr_white /= CR_WHITE_SIZE;
1767
1768 for ( j = COMP_RED_VS; j <= COMP_RED_VE; j++ ) {
1769 for ( i = COMP_RED_HS; i <= COMP_RED_HE; ) {
1770 mem_data->rcr_max += get_mem_data ( dp, ( ( COMP_BUF_WID * j + i ) * YCBCR422 + CR422_POS ) );
1771 i = i + 2;
1772 }
1773 }
1774
1775 mem_data->rcr_max /= COMP_RED_SIZE;
1776
1777 for ( j = COMP_BLUE_VS; j <= COMP_BLUE_VE; j++ ) {
1778 for ( i = COMP_BLUE_HS; i <= COMP_BLUE_HE; ) {
1779 mem_data->bcb_max += get_mem_data ( dp, ( ( COMP_BUF_WID * j + i ) * YCBCR422 + CB422_POS ) );
1780 i = i + 2;
1781 }
1782 }
1783
1784 mem_data->bcb_max /= COMP_BLUE_SIZE;
1785
1786 for ( j = COMP_BLACK_VS; j <= COMP_BLACK_VE; j++ ) {
1787 for ( i = COMP_BLACK_HS; i <= COMP_BLACK_HE; i++ ) {
1788 mem_data->g_y_min += get_mem_data ( dp, ( ( COMP_BUF_WID * j + i ) * YCBCR422 ) );
1789 }
1790 }
1791
1792 mem_data->g_y_min /= COMP_BLACK_SIZE;
1793
1794 for ( j = COMP_BLACK_VS; j <= COMP_BLACK_VE; j++ ) {
1795 for ( i = COMP_BLACK_HS; i <= COMP_BLACK_HE; ) {
1796 mem_data->cb_black += get_mem_data ( dp, ( ( COMP_BUF_WID * j + i ) * YCBCR422 + CB422_POS ) );
1797 mem_data->cr_black += get_mem_data ( dp, ( ( COMP_BUF_WID * j + i ) * YCBCR422 + CR422_POS ) );
1798 i = i + 2;
1799 }
1800 }
1801
1802 mem_data->cb_black /= CB_BLACK_SIZE;
1803 mem_data->cr_black /= CR_BLACK_SIZE;
1804
1805 /*
1806 for (j=COMP_BLACK_VS; j<=COMP_BLACK_VE; j++) {
1807 for (i=COMP_BLACK_HS; i<=COMP_BLACK_HE;) {
1808 //mem_data->cb_black += get_mem_data(dp, ((COMP_BUF_WID*j+i)*YCBCR422+CB422_POS));
1809 mem_data->cr_black += get_mem_data(dp, ((COMP_BUF_WID*j+i)*YCBCR422+CR422_POS));
1810 i = i+2;
1811 }
1812 }
1813 mem_data->cr_black /= CR_BLACK_SIZE;
1814 */
1815 for ( j = COMP_CYAN_VS; j <= COMP_CYAN_VE; j++ ) {
1816 for ( i = COMP_CYAN_HS; i <= COMP_CYAN_HE; ) {
1817 mem_data->rcr_min += get_mem_data ( dp, ( ( COMP_BUF_WID * j + i ) * YCBCR422 + CR422_POS ) );
1818 i = i + 2;
1819 }
1820 }
1821
1822 mem_data->rcr_min /= COMP_CYAN_SIZE;
1823
1824 for ( j = COMP_YELLOW_VS; j <= COMP_YELLOW_VE; j++ ) {
1825 for ( i = COMP_YELLOW_HS; i <= COMP_YELLOW_HE; ) {
1826 mem_data->bcb_min += get_mem_data ( dp, ( COMP_BUF_WID * j + i ) * YCBCR422 + CB422_POS );
1827 i = i + 2;
1828 }
1829 }
1830
1831 mem_data->bcb_min /= COMP_YELLOW_SIZE;
1832
1833 } else if ( calType == CAL_VGA ) {
1834 for ( j = VGA_WHITE_VS; j <= VGA_WHITE_VE; j++ ) {
1835 for ( i = VGA_WHITE_HS; i <= VGA_WHITE_HE; i++ ) {
1836#ifdef VGA_SOURCE_RGB444
1837 r = get_mem_data ( dp, ( ( VGA_BUF_WID * j + i ) * RGB444 + R444_POS ) );
1838 g = get_mem_data ( dp, ( ( VGA_BUF_WID * j + i ) * RGB444 + G444_POS ) );
1839 b = get_mem_data ( dp, ( ( VGA_BUF_WID * j + i ) * RGB444 + B444_POS ) );
1840#else
1841 y = get_mem_data ( dp, ( ( VGA_BUF_WID * j + i ) * YCBCR444 + Y444_POS ) );
1842 cb = get_mem_data ( dp, ( ( VGA_BUF_WID * j + i ) * YCBCR444 + CB444_POS ) );
1843 cr = get_mem_data ( dp, ( ( VGA_BUF_WID * j + i ) * YCBCR444 + CR444_POS ) );
1844 matrix_convert_yuv709_to_rgb ( y, cb, cr, &r, &g, &b );
1845#endif
1846 mem_data->rcr_max = mem_data->rcr_max + r;
1847 mem_data->g_y_max = mem_data->g_y_max + g;
1848 mem_data->bcb_max = mem_data->bcb_max + b;
1849 }
1850 }
1851
1852 mem_data->rcr_max = mem_data->rcr_max / VGA_WHITE_SIZE;
1853 mem_data->g_y_max = mem_data->g_y_max / VGA_WHITE_SIZE;
1854 mem_data->bcb_max = mem_data->bcb_max / VGA_WHITE_SIZE;
1855
1856 for ( j = VGA_BLACK_VS; j <= VGA_BLACK_VE; j++ ) {
1857 for ( i = VGA_BLACK_HS; i <= VGA_BLACK_HE; i++ ) {
1858#ifdef VGA_SOURCE_RGB444
1859 r = get_mem_data ( dp, ( ( VGA_BUF_WID * j + i ) * RGB444 + R444_POS ) );
1860 g = get_mem_data ( dp, ( ( VGA_BUF_WID * j + i ) * RGB444 + G444_POS ) );
1861 b = get_mem_data ( dp, ( ( VGA_BUF_WID * j + i ) * RGB444 + B444_POS ) );
1862#else
1863 y = get_mem_data ( dp, ( ( VGA_BUF_WID * j + i ) * YCBCR444 + Y444_POS ) );
1864 cb = get_mem_data ( dp, ( ( VGA_BUF_WID * j + i ) * YCBCR444 + CB444_POS ) );
1865 cr = get_mem_data ( dp, ( ( VGA_BUF_WID * j + i ) * YCBCR444 + CR444_POS ) );
1866 matrix_convert_yuv709_to_rgb ( y, cb, cr, &r, &g, &b );
1867#endif
1868 mem_data->rcr_min = mem_data->rcr_min + r;
1869 mem_data->g_y_min = mem_data->g_y_min + g;
1870 mem_data->bcb_min = mem_data->bcb_min + b;
1871 }
1872 }
1873
1874 mem_data->rcr_min = mem_data->rcr_min / VGA_BLACK_SIZE;
1875 mem_data->g_y_min = mem_data->g_y_min / VGA_BLACK_SIZE;
1876 mem_data->bcb_min = mem_data->bcb_min / VGA_BLACK_SIZE;
1877
1878 } else { //CVBS
1879 for ( j = CVBS_WHITE_VS; j <= CVBS_WHITE_VE; j++ ) {
1880 for ( i = CVBS_WHITE_HS; i <= CVBS_WHITE_HE; i++ ) {
1881 mem_data->g_y_max += mem_data->g_y_max + get_mem_data ( dp, ( ( CVBS_BUF_WID * j + i ) * YCBCR422 ) );
1882 }
1883 }
1884
1885 mem_data->g_y_max /= COMP_WHITE_SIZE;
1886
1887 for ( j = CVBS_BLACK_VS; j <= CVBS_BLACK_VE; j++ ) {
1888 for ( i = CVBS_BLACK_HS; i <= CVBS_BLACK_HE; i++ ) {
1889 mem_data->g_y_min += mem_data->g_y_min + get_mem_data ( dp, ( ( CVBS_BUF_WID * j + i ) * YCBCR422 ) );
1890 }
1891 }
1892
1893 mem_data->g_y_min /= CVBS_BLACK_SIZE;
1894 }
1895
1896 if ( calType == CAL_YPBPR ) {
1897 munmap ( dp, COMP_CAP_SIZE );
1898 } else if ( calType == CAL_VGA ) {
1899 munmap ( dp, VGA_CAP_SIZE );
1900 } else {
1901 munmap ( dp, CVBS_CAP_SIZE );
1902 }
1903
1904 if ( VDIN_DeviceIOCtl ( TVIN_IOC_START_DEC, &gTvinAFEParam ) < 0 ) {
1905 LOGW ( "get_frame_average, get vdin signal info, error(%s),fd(%d).\n", strerror ( errno ), m_vdin_dev_fd );
1906 return NULL;
1907 } else {
1908 ;
1909 }
1910
1911 return 0;
1912}
1913
1914#define ADC_CAL_FRAME_QTY_ORDER 2 //NOTE: MUST >=2!!
1915struct adc_cal_s CTvin::get_n_frame_average ( enum adc_cal_type_e calType )
1916{
1917 struct adc_cal_s mem_data = { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 };
1918 unsigned int rcrmax[1 << ADC_CAL_FRAME_QTY_ORDER];
1919 unsigned int rcrmin[1 << ADC_CAL_FRAME_QTY_ORDER];
1920 unsigned int g_ymax[1 << ADC_CAL_FRAME_QTY_ORDER];
1921 unsigned int g_ymin[1 << ADC_CAL_FRAME_QTY_ORDER];
1922 unsigned int bcbmax[1 << ADC_CAL_FRAME_QTY_ORDER];
1923 unsigned int bcbmin[1 << ADC_CAL_FRAME_QTY_ORDER];
1924 unsigned int cbwhite[1 << ADC_CAL_FRAME_QTY_ORDER];
1925 unsigned int crwhite[1 << ADC_CAL_FRAME_QTY_ORDER];
1926 unsigned int cbblack[1 << ADC_CAL_FRAME_QTY_ORDER];
1927 unsigned int crblack[1 << ADC_CAL_FRAME_QTY_ORDER];
1928 unsigned int i = 0, j = 0;
1929
1930 for ( i = 0; i < ( 1 << ADC_CAL_FRAME_QTY_ORDER ); i++ ) {
1931 get_frame_average ( calType, &mem_data );
1932 rcrmax[i] = mem_data.rcr_max;
1933 rcrmin[i] = mem_data.rcr_min;
1934 g_ymax[i] = mem_data.g_y_max;
1935 g_ymin[i] = mem_data.g_y_min;
1936 bcbmax[i] = mem_data.bcb_max;
1937 bcbmin[i] = mem_data.bcb_min;
1938 cbwhite[i] = mem_data.cb_white;
1939 crwhite[i] = mem_data.cr_white;
1940 cbblack[i] = mem_data.cb_black;
1941 crblack[i] = mem_data.cr_black;
1942 }
1943
1944 for ( i = 0; i < ( 1 << ADC_CAL_FRAME_QTY_ORDER ) - 1; i++ ) {
1945 for ( j = 1; j < ( 1 << ADC_CAL_FRAME_QTY_ORDER ); j++ ) {
1946 re_order ( & ( rcrmax[i] ), & ( rcrmax[j] ) );
1947 re_order ( & ( rcrmin[i] ), & ( rcrmin[j] ) );
1948 re_order ( & ( g_ymax[i] ), & ( g_ymax[j] ) );
1949 re_order ( & ( g_ymin[i] ), & ( g_ymin[j] ) );
1950 re_order ( & ( bcbmax[i] ), & ( bcbmax[j] ) );
1951 re_order ( & ( bcbmin[i] ), & ( bcbmin[j] ) );
1952 re_order ( & ( cbwhite[i] ), & ( cbwhite[j] ) );
1953 re_order ( & ( crwhite[i] ), & ( crwhite[j] ) );
1954 re_order ( & ( cbblack[i] ), & ( cbblack[j] ) );
1955 re_order ( & ( crblack[i] ), & ( crblack[j] ) );
1956 }
1957 }
1958
1959 memset ( &mem_data, 0, sizeof ( mem_data ) );
1960
1961 for ( i = 0; i < ( 1 << ( ADC_CAL_FRAME_QTY_ORDER - 1 ) ); i++ ) { //(1<<(ADC_CAL_FRAME_QTY_ORDER-1))
1962 mem_data.rcr_max += rcrmax[i + ( 1 << ( ADC_CAL_FRAME_QTY_ORDER - 2 ) )];
1963 mem_data.rcr_min += rcrmin[i + ( 1 << ( ADC_CAL_FRAME_QTY_ORDER - 2 ) )];
1964 mem_data.g_y_max += g_ymax[i + ( 1 << ( ADC_CAL_FRAME_QTY_ORDER - 2 ) )];
1965 mem_data.g_y_min += g_ymin[i + ( 1 << ( ADC_CAL_FRAME_QTY_ORDER - 2 ) )];
1966 mem_data.bcb_max += bcbmax[i + ( 1 << ( ADC_CAL_FRAME_QTY_ORDER - 2 ) )];
1967 mem_data.bcb_min += bcbmin[i + ( 1 << ( ADC_CAL_FRAME_QTY_ORDER - 2 ) )];
1968 mem_data.cb_white += cbwhite[i + ( 1 << ( ADC_CAL_FRAME_QTY_ORDER - 2 ) )];
1969 mem_data.cr_white += crwhite[i + ( 1 << ( ADC_CAL_FRAME_QTY_ORDER - 2 ) )];
1970 mem_data.cb_black += cbblack[i + ( 1 << ( ADC_CAL_FRAME_QTY_ORDER - 2 ) )];
1971 mem_data.cr_black += crblack[i + ( 1 << ( ADC_CAL_FRAME_QTY_ORDER - 2 ) )];
1972 }
1973
1974
1975 mem_data.rcr_max >>= ( ADC_CAL_FRAME_QTY_ORDER - 1 );
1976 mem_data.rcr_min >>= ( ADC_CAL_FRAME_QTY_ORDER - 1 );
1977 mem_data.g_y_max >>= ( ADC_CAL_FRAME_QTY_ORDER - 1 );
1978 mem_data.g_y_min >>= ( ADC_CAL_FRAME_QTY_ORDER - 1 );
1979 mem_data.bcb_max >>= ( ADC_CAL_FRAME_QTY_ORDER - 1 );
1980 mem_data.bcb_min >>= ( ADC_CAL_FRAME_QTY_ORDER - 1 );
1981 mem_data.cb_white >>= ( ADC_CAL_FRAME_QTY_ORDER - 1 );
1982 mem_data.cr_white >>= ( ADC_CAL_FRAME_QTY_ORDER - 1 );
1983 mem_data.cb_black >>= ( ADC_CAL_FRAME_QTY_ORDER - 1 );
1984 mem_data.cr_black >>= ( ADC_CAL_FRAME_QTY_ORDER - 1 );
1985
1986 return mem_data;
1987}
1988
1989int CTvin::AFE_GetMemData ( int typeSel, struct adc_cal_s *mem_data )
1990{
1991 int rt = -1;
1992
1993 if ( m_vdin_dev_fd < 0 || mem_data == NULL ) {
1994 LOGW ( "AFE_GetMemData, didn't open vdin fd, return!\n" );
1995 return -1;
1996 }
1997
1998 memset ( &gTvinAFEParam, 0, sizeof ( gTvinAFEParam ) );
1999 memset ( &gTvinAFESignalInfo, 0, sizeof ( gTvinAFESignalInfo ) );
2000
2001 if ( VDIN_DeviceIOCtl ( TVIN_IOC_G_PARM, &gTvinAFEParam ) < 0 ) {
2002 LOGW ( "AFE_GetMemData, get vdin param, error(%s), fd(%d)!\n", strerror ( errno ), m_vdin_dev_fd );
2003 return -1;
2004 }
2005
2006 gTvinAFEParam.flag = gTvinAFEParam.flag | TVIN_PARM_FLAG_CAP;
2007
2008 if ( VDIN_DeviceIOCtl ( TVIN_IOC_S_PARM, &gTvinAFEParam ) < 0 ) {
2009 LOGW ( "AFE_GetMemData, set vdin param error(%s)!\n", strerror ( errno ) );
2010 return -1;
2011 }
2012
2013 if ( typeSel == 0 ) {
2014 get_frame_average ( CAL_YPBPR, mem_data );
2015 } else if ( typeSel == 1 ) {
2016 get_frame_average ( CAL_VGA, mem_data );
2017 } else {
2018 *mem_data = get_n_frame_average ( CAL_CVBS );
2019 }
2020
2021 gTvinAFEParam.flag &= 0x11111110;
2022
2023 if ( VDIN_DeviceIOCtl ( TVIN_IOC_S_PARM, &gTvinAFEParam ) < 0 ) {
2024 LOGW ( "AFE_GetMemData, set vdin param error(%s)\n", strerror ( errno ) );
2025 return -1;
2026 }
2027
2028 LOGD ( "AFE_GetMemData, MAX ======> :\n Y(White)->%d \n Cb(Blue)->%d \n Cr(Red)->%d\n", mem_data->g_y_max, mem_data->bcb_max, mem_data->rcr_max );
2029 LOGD ( "AFE_GetMemData, MIN ======>:\n Y(Black)->%d \n Cb(Yellow)->%d \n Cr(Cyan)->%d\n Cb(White) ->%d\n Cb(Black)->%d\n Cr(Black)->%d\n", mem_data->g_y_min, mem_data->bcb_min, mem_data->rcr_min,
2030 mem_data->cb_white, mem_data->cb_black, mem_data->cr_black );
2031 return 0;
2032}
2033
2034int CTvin::AFE_GetCVBSLockStatus ( enum tvafe_cvbs_video_e *cvbs_lock_status )
2035{
2036 int rt = -1;
2037
2038 rt = AFE_DeviceIOCtl ( TVIN_IOC_G_AFE_CVBS_LOCK, cvbs_lock_status );
2039
2040 if ( rt < 0 ) {
2041 LOGD ( "AFE_GetCVBSLockStatus, error: return(%d), error(%s)!\n", rt, strerror ( errno ) );
2042 } else {
2043 LOGD ( "AFE_GetCVBSLockStatus, value=%d.\n", *cvbs_lock_status );
2044 }
2045
2046 return *cvbs_lock_status;
2047}
2048
2049int CTvin::AFE_SetCVBSStd ( tvin_sig_fmt_t fmt )
2050{
2051 int rt = -1;
2052
2053 LOGD ( "AFE_SetCVBSStd, sig_fmt = %d\n", fmt );
2054 rt = AFE_DeviceIOCtl ( TVIN_IOC_S_AFE_CVBS_STD, &fmt );
2055
2056 if ( rt < 0 ) {
2057 LOGD ( "AFE_SetCVBSStd, error: return(%d), error(%s)!\n", rt, strerror ( errno ) );
2058 }
2059
2060 return rt;
2061}
2062
2063int CTvin::TvinApi_SetStartDropFrameCn ( int count )
2064{
2065 int ret = -1;
2066 char set_str[4];
2067
2068 memset ( set_str, 0, 4 );
2069 sprintf ( set_str, "%d", count );
2070 return SetFileAttrValue ( "/sys/module/di/parameters/start_frame_drop_count", set_str );
2071}
2072
2073int CTvin::TvinApi_SetVdinHVScale ( int vdinx, int hscale, int vscale )
2074{
2075 int ret = -1;
2076 char set_str[32];
2077
2078 memset ( set_str, 0, 32 );
2079 sprintf ( set_str, "%s %d %d", "hvscaler", hscale, vscale );
2080
2081 if ( vdinx == 0 ) {
2082 ret = SetFileAttrValue ( "/sys/class/vdin/vdin0/attr", set_str );
2083 } else {
2084 ret = SetFileAttrValue ( "/sys/class/vdin/vdin1/attr", set_str );
2085 }
2086
2087 return ret;
2088}
2089int CTvin::TvinApi_SetCompPhase ( am_phase_t &am_phase )
2090{
2091 int ret = -1, fd = -1;
2092 unsigned int i = 0;
2093 int idx = 0, len = 0;
2094 char str1[200], str2[100];
2095
2096 LOGD ( "enter,TvinApi_SetCompPhase" );
2097
2098 fd = open ( "/sys/module/tvin_afe/parameters/comp_phase", O_RDWR );
2099 if ( fd < 0 ) {
2100 LOGW ( "Open vdin_comp_phase_op_mutex error(%s)!!\n", strerror ( errno ) );
2101 return -1;
2102 }
2103
2104 for ( i = 0; i < am_phase.length; i++ ) {
2105 sprintf ( &str1[idx], "%d,", am_phase.phase[i] );
2106 sprintf ( str2, "%d,", am_phase.phase[i] );
2107 int len = strlen ( str2 );
2108 idx = idx + len;
2109 }
2110
2111 LOGD ( "##########str1 = %s\n", str1 );
2112
2113 ret = write ( fd, str1, strlen ( str1 ) );
2114 if ( ret < 0 ) {
2115 LOGD ( "Write vdin_comp_phase_op_mutex error(%s)!!\n", strerror ( errno ) );
2116 }
2117
2118 LOGD ( "write ok!!!" );
2119 close ( fd );
2120 fd = -1;
2121
2122 return ret;
2123}
2124
2125tvin_trans_fmt CTvin::TvinApi_Get3DDectMode()
2126{
2127 int fd;
2128 int ret;
2129 char det_3d[10];
2130 int det_3dmode = 8;
2131 //LOGW("det_3dmode %d\n", det_3dmode);
2132
2133 fd = open ( "/sys/module/di/parameters/det3d_mode", O_RDWR );
2134 if ( fd < 0 ) {
2135 LOGW ( "/sys/module/di/parameters/det3d_mode error(%s)!!\n", strerror ( errno ) );
2136
2137 return TVIN_TFMT_3D_MAX;
2138 }
2139
2140 ret = read ( fd, det_3d, 10 );
2141 if ( ret < 0 ) {
2142 LOGW ( "/sys/module/di/parameters/det3d_mode error(%s)!!\n", strerror ( errno ) );
2143 }
2144
2145 det_3dmode = atoi ( det_3d );
2146 close ( fd );
2147 fd = -1;
2148
2149 return (tvin_trans_fmt)det_3dmode;
2150}
2151
2152int CTvin::TvinApi_SetCompPhaseEnable ( int enable )
2153{
2154 int ret = -1;
2155 if ( enable == 1 ) {
2156 ret = SetFileAttrValue ( "/sys/module/tvin_afe/parameters/enable_dphase", "Y" );
2157 LOGD ( "%s, enable TvinApi_SetCompPhase.", CFG_SECTION_TV );
2158 }
2159
2160 return ret;
2161}
2162
2163int CTvin::VDIN_GetPortConnect ( int port )
2164{
2165 int status = 0;
2166
2167 if ( VDIN_DeviceIOCtl ( TVIN_IOC_CALLMASTER_SET, &port ) < 0 ) {
2168 LOGW ( "TVIN_IOC_CALLMASTER_SET error(%s) port %d\n", strerror ( errno ), port );
2169 return 0;
2170 }
2171
2172 if ( VDIN_DeviceIOCtl ( TVIN_IOC_CALLMASTER_GET, &status ) < 0 ) {
2173 LOGW ( "TVIN_IOC_CALLMASTER_GET error(%s)\n", strerror ( errno ) );
2174 return 0;
2175 }
2176
2177 //LOGD("%s, port:%x,status:%d", CFG_SECTION_TV,port,status);
2178 return status;
2179}
2180
2181int CTvin::VDIN_OpenHDMIPinMuxOn ( bool flag )
2182{
2183 FILE *fp = NULL;
2184
2185 fp = fopen ( "/sys/class/hdmirx/hdmirx0/debug", "w" );
2186 if ( fp == NULL ) {
2187 LOGW ( "Open /sys/class/hdmirx/hdmirx0/debug(%s)!\n", strerror ( errno ) );
2188 return -1;
2189 }
2190
2191 if ( flag ) {
2192 fprintf ( fp, "%s", "pinmux_on" );
2193 } else {
2194 fprintf ( fp, "%s", "pinmux_off" );
2195
2196 }
2197
2198 fclose ( fp );
2199 fp = NULL;
2200 return 1;
2201}
2202
2203int CTvin::VDIN_GetHdmiHdcpKeyKsvInfo(struct _hdcp_ksv *msg)
2204{
2205 int m_ksv_dev_fd = -1;
2206
2207 if (msg == NULL) {
2208 LOGE("%s, msg is NULL\n", __FUNCTION__);
2209 }
2210
2211 //PrintMessage(__FUNCTION__, 0, msg);
2212
2213 m_ksv_dev_fd = open(HDMIRX_KSV_PATH, O_RDWR);
2214 if (m_ksv_dev_fd < 0) {
2215 LOGE("%s, Open file %s error: (%s)!\n", __FUNCTION__, HDMIRX_KSV_PATH, strerror ( errno ));
2216 return -1;
2217 }
2218 LOGD("# call ioctl with HDMI_IOC_HDCP_SENT_KSV #");
2219 ioctl(m_ksv_dev_fd, HDMI_IOC_HDCP_KSV, msg);
2220
2221 close(m_ksv_dev_fd);
2222 m_ksv_dev_fd = -1;
2223
2224 LOGD("msg->bksv0 is %x, msg->bksv1 is %x", msg->bksv0, msg->bksv1);
2225
2226 return 0;
2227}
2228
2229
2230int CTvin::get_hdmi_ksv_info(int source_input, int data_buf[])
2231{
2232 if (source_input != SOURCE_HDMI1 && source_input != SOURCE_HDMI2 && source_input != SOURCE_HDMI3) {
2233 return -1;
2234 }
2235
2236 struct _hdcp_ksv msg;
2237 int ret = -1;
2238 ret = VDIN_GetHdmiHdcpKeyKsvInfo(&msg);
2239 memset((void *)data_buf, 0, 2);
2240 data_buf[0] = msg.bksv0;
2241 data_buf[1] = msg.bksv1;
2242 return ret;
2243}
2244
2245
2246int CTvin::TVAFE_EnablePlugInDetect ( bool flag )
2247{
2248 FILE *fp = NULL;
2249 int status = 1;
2250
2251 fp = fopen ( "/sys/class/tvafe/tvafe0/debug", "w" );
2252 if ( fp == NULL ) {
2253 LOGW ( "Open /sys/class/tvafe/tvafe0/debug (%s)!\n", strerror ( errno ) );
2254 return -1;
2255 }
2256
2257 if ( flag ) {
2258 fprintf ( fp, "%s", "tvafe_enable" );
2259 } else {
2260 fprintf ( fp, "%s", "tvafe_down" );
2261
2262 }
2263
2264 fclose ( fp );
2265 fp = NULL;
2266 return status;
2267}
2268
2269int CTvin::TvinApi_GetHDMIAudioStatus ( void )
2270{
2271 int fd;
2272 int val = 0;
2273 char bcmd[16];
2274 fd = open ( "/sys/module/tvin_hdmirx/parameters/auds_rcv_sts", O_RDONLY );
2275
2276 if ( fd >= 0 ) {
2277 read ( fd, bcmd, sizeof ( bcmd ) );
2278 val = strtol ( bcmd, NULL, 10 );
2279 close ( fd );
2280 } else {
2281 LOGE ( "open /sys/module/tvin_hdmirx/parameters/auds_rcv_sts ERROR(%s)!!\n", strerror ( errno ) );
2282 return -1;
2283 }
2284
2285 return val;
2286}
2287
2288int CTvin::TvinApi_LoadPLLValues ( am_regs_t regs )
2289{
2290 int rt = AFE_DeviceIOCtl ( TVIN_IOC_LOAD_REG, &regs );
2291
2292 if ( rt < 0 ) {
2293 LOGE ( "TvinApi_LoadPLLValues, error(%s)!\n", strerror ( errno ) );
2294 }
2295
2296 return rt;
2297}
2298
2299int CTvin::TvinApi_LoadCVD2Values ( am_regs_t regs )
2300{
2301 int rt = AFE_DeviceIOCtl ( TVIN_IOC_LOAD_REG, &regs );
2302
2303 if ( rt < 0 ) {
2304 LOGE ( "TvinApi_LoadCVD2Values, error(%s)!\n", strerror ( errno ) );
2305 }
2306
2307 return rt;
2308}
2309
2310int CTvin::TvinApi_GetFbSize ( unsigned int *fb_width, unsigned int *fb_height )
2311{
2312 int fbfd = 0;
2313 struct fb_var_screeninfo vinfo;
2314 struct fb_fix_screeninfo finfo;
2315 int xres = 0, yres = 0, bits_per_pixel = 0;
2316
2317 fbfd = open ( "/dev/graphics/fb0", O_RDWR );
2318 if ( !fbfd ) {
2319 return -1;
2320 }
2321
2322 if ( ioctl ( fbfd, FBIOGET_FSCREENINFO, &finfo ) ) {
2323 goto fail_close_fb;
2324 }
2325
2326 if ( ioctl ( fbfd, FBIOGET_VSCREENINFO, &vinfo ) ) {
2327 goto fail_close_fb;
2328 }
2329
2330 *fb_width = vinfo.xres;
2331 *fb_height = vinfo.yres;
2332 return 1;
2333
2334fail_close_fb:
2335 close ( fbfd );;
2336 return -1;
2337}
2338
2339tv_source_input_type_t CTvin::Tvin_SourceInputToSourceInputType ( tv_source_input_t source_input )
2340{
2341 if (source_input == SOURCE_TV) {
2342 return SOURCE_TYPE_TV;
2343 } else if (source_input == SOURCE_AV1 || source_input == SOURCE_AV2) {
2344 return SOURCE_TYPE_AV;
2345 } else if (source_input == SOURCE_YPBPR1 || source_input == SOURCE_YPBPR2) {
2346 return SOURCE_TYPE_COMPONENT;
2347 } else if (source_input == SOURCE_VGA) {
2348 return SOURCE_TYPE_VGA;
2349 } else if (source_input == SOURCE_HDMI1 || source_input == SOURCE_HDMI2 || source_input == SOURCE_HDMI3) {
2350 return SOURCE_TYPE_HDMI;
2351 } else if (source_input == SOURCE_DTV) {
2352 return SOURCE_TYPE_DTV;
2353 } else if (source_input == SOURCE_IPTV) {
2354 return SOURCE_TYPE_IPTV;
2355 } else if (source_input == SOURCE_MPEG) {
2356 return SOURCE_TYPE_MPEG;
2357 }
2358
2359 return SOURCE_TYPE_MPEG;
2360}
2361
2362tv_source_input_type_t CTvin::Tvin_SourcePortToSourceInputType ( tvin_port_t source_port )
2363{
2364 tv_source_input_t source_input = Tvin_PortToSourceInput(source_port);
2365 return Tvin_SourceInputToSourceInputType(source_input);
2366}
2367
2368tvin_port_t CTvin::Tvin_GetSourcePortBySourceType ( tv_source_input_type_t source_type )
2369{
2370 tvin_port_t source_port;
2371
2372 switch ( source_type ) {
2373 case SOURCE_TYPE_TV:
2374 source_port = Tvin_GetSourcePortBySourceInput(SOURCE_TV);
2375 break;
2376 case SOURCE_TYPE_AV:
2377 source_port = Tvin_GetSourcePortBySourceInput(SOURCE_AV1);
2378 break;
2379 case SOURCE_TYPE_COMPONENT:
2380 source_port = Tvin_GetSourcePortBySourceInput(SOURCE_YPBPR1);
2381 break;
2382 case SOURCE_TYPE_VGA:
2383 source_port = Tvin_GetSourcePortBySourceInput(SOURCE_VGA);
2384 break;
2385 case SOURCE_TYPE_HDMI:
2386 source_port = TVIN_PORT_HDMI0;
2387 break;
2388 case SOURCE_TYPE_IPTV:
2389 source_port = Tvin_GetSourcePortBySourceInput(SOURCE_IPTV);
2390 break;
2391 case SOURCE_TYPE_DTV:
2392 source_port = Tvin_GetSourcePortBySourceInput(SOURCE_DTV);
2393 break;
2394 case SOURCE_TYPE_MPEG:
2395 default:
2396 source_port = Tvin_GetSourcePortBySourceInput(SOURCE_MPEG);
2397 break;
2398 }
2399
2400 return source_port;
2401}
2402
2403tvin_port_t CTvin::Tvin_GetSourcePortBySourceInput ( tv_source_input_t source_input )
2404{
2405 tvin_port_t source_port = TVIN_PORT_NULL;
2406
2407 if ( source_input < SOURCE_TV || source_input >= SOURCE_MAX ) {
2408 source_port = TVIN_PORT_NULL;
2409 } else {
2410 source_port = ( tvin_port_t ) mSourceInputToPortMap[ ( int ) source_input];
2411 }
2412
2413 return source_port;
2414}
2415
2416tv_source_input_t CTvin::Tvin_PortToSourceInput ( tvin_port_t port )
2417{
2418 int i;
2419
2420 for ( i = SOURCE_TV; i < SOURCE_MAX; i++ ) {
2421 if ( mSourceInputToPortMap[i] == port ) {
2422 break;
2423 }
2424 }
2425
2426 if ( i == SOURCE_MAX ) {
2427 return SOURCE_MAX;
2428 } else {
2429 return tv_source_input_t ( i );
2430 }
2431}
2432
2433unsigned int CTvin::Tvin_TransPortStringToValue(const char *port_str)
2434{
2435 if (strcasecmp(port_str, "TVIN_PORT_CVBS0") == 0) {
2436 return TVIN_PORT_CVBS0;
2437 } else if (strcasecmp(port_str, "TVIN_PORT_CVBS1") == 0) {
2438 return TVIN_PORT_CVBS1;
2439 } else if (strcasecmp(port_str, "TVIN_PORT_CVBS2") == 0) {
2440 return TVIN_PORT_CVBS2;
2441 } else if (strcasecmp(port_str, "TVIN_PORT_CVBS3") == 0) {
2442 return TVIN_PORT_CVBS3;
2443 } else if (strcasecmp(port_str, "TVIN_PORT_COMP0") == 0) {
2444 return TVIN_PORT_COMP0;
2445 } else if (strcasecmp(port_str, "TVIN_PORT_COMP1") == 0) {
2446 return TVIN_PORT_COMP1;
2447 } else if (strcasecmp(port_str, "TVIN_PORT_VGA0") == 0) {
2448 return TVIN_PORT_VGA0;
2449 } else if (strcasecmp(port_str, "TVIN_PORT_HDMI0") == 0) {
2450 return TVIN_PORT_HDMI0;
2451 } else if (strcasecmp(port_str, "TVIN_PORT_HDMI1") == 0) {
2452 return TVIN_PORT_HDMI1;
2453 } else if (strcasecmp(port_str, "TVIN_PORT_HDMI2") == 0) {
2454 return TVIN_PORT_HDMI2;
2455 } else if (strcasecmp(port_str, "TVIN_PORT_HDMI3") == 0) {
2456 return TVIN_PORT_HDMI3;
2457 }
2458
2459 return TVIN_PORT_MPEG0;
2460}
2461
2462void CTvin::Tvin_LoadSourceInputToPortMap()
2463{
2464 const char *config_value = NULL;
2465
2466 config_value = config_get_str(CFG_SECTION_SRC_INPUT, "ro.tv.tvinchannel.atv", "TVIN_PORT_CVBS3");
2467 mSourceInputToPortMap[SOURCE_TV] = Tvin_TransPortStringToValue(config_value);
2468
2469 config_value = config_get_str(CFG_SECTION_SRC_INPUT, "ro.tv.tvinchannel.av1", "TVIN_PORT_CVBS1");
2470 mSourceInputToPortMap[SOURCE_AV1] = Tvin_TransPortStringToValue(config_value);
2471
2472 config_value = config_get_str(CFG_SECTION_SRC_INPUT, "ro.tv.tvinchannel.av2", "TVIN_PORT_CVBS2");
2473 mSourceInputToPortMap[SOURCE_AV2] = Tvin_TransPortStringToValue(config_value);
2474
2475 config_value = config_get_str(CFG_SECTION_SRC_INPUT, "ro.tv.tvinchannel.ypbpr1", "TVIN_PORT_COMP0");
2476 mSourceInputToPortMap[SOURCE_YPBPR1] = Tvin_TransPortStringToValue(config_value);
2477
2478 config_value = config_get_str(CFG_SECTION_SRC_INPUT, "ro.tv.tvinchannel.ypbpr2", "TVIN_PORT_COMP1");
2479 mSourceInputToPortMap[SOURCE_YPBPR2] = Tvin_TransPortStringToValue(config_value);
2480
2481 config_value = config_get_str(CFG_SECTION_SRC_INPUT, "ro.tv.tvinchannel.hdmi1", "TVIN_PORT_HDMI0");
2482 mSourceInputToPortMap[SOURCE_HDMI1] = Tvin_TransPortStringToValue(config_value);
2483
2484 config_value = config_get_str(CFG_SECTION_SRC_INPUT, "ro.tv.tvinchannel.hdmi2", "TVIN_PORT_HDMI2");
2485 mSourceInputToPortMap[SOURCE_HDMI2] = Tvin_TransPortStringToValue(config_value);
2486
2487 config_value = config_get_str(CFG_SECTION_SRC_INPUT, "ro.tv.tvinchannel.hdmi3", "TVIN_PORT_HDMI1");
2488 mSourceInputToPortMap[SOURCE_HDMI3] = Tvin_TransPortStringToValue(config_value);
2489
2490 config_value = config_get_str(CFG_SECTION_SRC_INPUT, "ro.tv.tvinchannel.vga", "TVIN_PORT_VGA0");
2491 mSourceInputToPortMap[SOURCE_VGA] = Tvin_TransPortStringToValue(config_value);
2492
2493 mSourceInputToPortMap[SOURCE_MPEG] = TVIN_PORT_MPEG0;
2494 mSourceInputToPortMap[SOURCE_DTV] = TVIN_PORT_DTV;
2495 mSourceInputToPortMap[SOURCE_IPTV] = TVIN_PORT_BT656;
2496}
2497
2498int CTvin::Tvin_GetSourcePortByCECPhysicalAddress(int physical_addr)
2499{
2500 if (physical_addr == 0x1000) {
2501 return TVIN_PORT_HDMI0;
2502 } else if (physical_addr == 0x2000) {
2503 return TVIN_PORT_HDMI1;
2504 } else if (physical_addr == 0x3000) {
2505 return TVIN_PORT_HDMI2;
2506 }
2507
2508 return TVIN_PORT_MAX;
2509}
2510
2511tv_audio_channel_t CTvin::Tvin_GetInputSourceAudioChannelIndex ( tv_source_input_t source_input )
2512{
2513 int aud_chan = TV_AUDIO_LINE_IN_0;
2514 const char *config_value = NULL;
2515
2516 if ( source_input == SOURCE_TV ) {
2517 config_value = config_get_str(CFG_SECTION_TV, "tvin.aud.chan.atv", "2");
2518 } else if ( source_input == SOURCE_AV1 ) {
2519 config_value = config_get_str(CFG_SECTION_TV, "tvin.aud.chan.av1", "1");
2520 } else if ( source_input == SOURCE_AV2 ) {
2521 config_value = config_get_str(CFG_SECTION_TV, "tvin.aud.chan.av2", "3");
2522 } else if ( source_input == SOURCE_YPBPR1 ) {
2523 config_value = config_get_str(CFG_SECTION_TV, "tvin.aud.chan.comp1", "0");
2524 } else if ( source_input == SOURCE_YPBPR2 ) {
2525 config_value = config_get_str(CFG_SECTION_TV, "tvin.aud.chan.comp2", "0");
2526 } else if ( source_input == SOURCE_HDMI1 ) {
2527 config_value = config_get_str(CFG_SECTION_TV, "tvin.aud.chan.hdmi1", "0");
2528 } else if ( source_input == SOURCE_HDMI2 ) {
2529 config_value = config_get_str(CFG_SECTION_TV, "tvin.aud.chan.hdmi2", "0");
2530 } else if ( source_input == SOURCE_HDMI3 ) {
2531 config_value = config_get_str(CFG_SECTION_TV, "tvin.aud.chan.hdmi3", "0");
2532 } else if ( source_input == SOURCE_VGA ) {
2533 config_value = config_get_str(CFG_SECTION_TV, "tvin.aud.chan.vga", "0");
2534 } else if ( source_input == SOURCE_MPEG ) {
2535 config_value = config_get_str(CFG_SECTION_TV, "tvin.aud.chan.mpeg", "0");
2536 }
2537
2538 if (config_value != NULL) {
2539 aud_chan = strtol (config_value, NULL, 10);
2540 }
2541
2542 return ( tv_audio_channel_t ) aud_chan;
2543}
2544
2545tv_audio_in_source_type_t CTvin::Tvin_GetAudioInSourceType ( tv_source_input_t source_input )
2546{
2547 const char *config_value = NULL;
2548
2549 if (source_input == SOURCE_TV) {
2550 config_value = config_get_str(CFG_SECTION_TV, "tvin.aud.insource.atv", "TV_AUDIO_IN_SOURCE_TYPE_LINEIN");
2551 if (strcasecmp(config_value, "TV_AUDIO_IN_SOURCE_TYPE_ATV") == 0) {
2552 return TV_AUDIO_IN_SOURCE_TYPE_ATV;
2553 }
2554 } else if (source_input == SOURCE_AV1 || source_input == SOURCE_AV2) {
2555 return TV_AUDIO_IN_SOURCE_TYPE_LINEIN;
2556 } else if (source_input == SOURCE_YPBPR1 || source_input == SOURCE_YPBPR2 || source_input == SOURCE_VGA) {
2557 return TV_AUDIO_IN_SOURCE_TYPE_LINEIN;
2558 } else if (source_input == SOURCE_HDMI1 || source_input == SOURCE_HDMI2 || source_input == SOURCE_HDMI3) {
2559 return TV_AUDIO_IN_SOURCE_TYPE_HDMI;
2560 }
2561
2562 return TV_AUDIO_IN_SOURCE_TYPE_LINEIN;
2563}
2564
2565int CTvin::isVgaFmtInHdmi ( tvin_sig_fmt_t fmt )
2566{
2567 if ( fmt == TVIN_SIG_FMT_HDMI_640X480P_60HZ
2568 || fmt == TVIN_SIG_FMT_HDMI_800X600_00HZ
2569 || fmt == TVIN_SIG_FMT_HDMI_1024X768_00HZ
2570 || fmt == TVIN_SIG_FMT_HDMI_720X400_00HZ
2571 || fmt == TVIN_SIG_FMT_HDMI_1280X768_00HZ
2572 || fmt == TVIN_SIG_FMT_HDMI_1280X800_00HZ
2573 || fmt == TVIN_SIG_FMT_HDMI_1280X960_00HZ
2574 || fmt == TVIN_SIG_FMT_HDMI_1280X1024_00HZ
2575 || fmt == TVIN_SIG_FMT_HDMI_1360X768_00HZ
2576 || fmt == TVIN_SIG_FMT_HDMI_1366X768_00HZ
2577 || fmt == TVIN_SIG_FMT_HDMI_1600X1200_00HZ
2578 || fmt == TVIN_SIG_FMT_HDMI_1920X1200_00HZ ) {
2579 LOGD ( "%s, HDMI source : VGA format.", CFG_SECTION_TV );
2580 return 1;
2581 }
2582
2583 return -1;
2584}
2585
2586int CTvin::isSDFmtInHdmi ( tvin_sig_fmt_t fmt )
2587{
2588 if ( fmt == TVIN_SIG_FMT_HDMI_640X480P_60HZ
2589 || fmt == TVIN_SIG_FMT_HDMI_720X480P_60HZ
2590 || fmt == TVIN_SIG_FMT_HDMI_1440X480I_60HZ
2591 || fmt == TVIN_SIG_FMT_HDMI_1440X240P_60HZ
2592 || fmt == TVIN_SIG_FMT_HDMI_2880X480I_60HZ
2593 || fmt == TVIN_SIG_FMT_HDMI_2880X240P_60HZ
2594 || fmt == TVIN_SIG_FMT_HDMI_1440X480P_60HZ
2595 || fmt == TVIN_SIG_FMT_HDMI_720X576P_50HZ
2596 || fmt == TVIN_SIG_FMT_HDMI_1440X576I_50HZ
2597 || fmt == TVIN_SIG_FMT_HDMI_1440X288P_50HZ
2598 || fmt == TVIN_SIG_FMT_HDMI_2880X576I_50HZ
2599 || fmt == TVIN_SIG_FMT_HDMI_2880X288P_50HZ
2600 || fmt == TVIN_SIG_FMT_HDMI_1440X576P_50HZ
2601 || fmt == TVIN_SIG_FMT_HDMI_2880X480P_60HZ
2602 || fmt == TVIN_SIG_FMT_HDMI_2880X576P_60HZ
2603 || fmt == TVIN_SIG_FMT_HDMI_720X576P_100HZ
2604 || fmt == TVIN_SIG_FMT_HDMI_1440X576I_100HZ
2605 || fmt == TVIN_SIG_FMT_HDMI_720X480P_120HZ
2606 || fmt == TVIN_SIG_FMT_HDMI_1440X480I_120HZ
2607 || fmt == TVIN_SIG_FMT_HDMI_720X576P_200HZ
2608 || fmt == TVIN_SIG_FMT_HDMI_1440X576I_200HZ
2609 || fmt == TVIN_SIG_FMT_HDMI_720X480P_240HZ
2610 || fmt == TVIN_SIG_FMT_HDMI_1440X480I_240HZ
2611 || fmt == TVIN_SIG_FMT_HDMI_800X600_00HZ
2612 || fmt == TVIN_SIG_FMT_HDMI_720X400_00HZ ) {
2613 LOGD ( "%s, SD format.", CFG_SECTION_TV );
2614 return true;
2615 } else {
2616 LOGD ( "%s, HD format.", CFG_SECTION_TV );
2617 return false;
2618 }
2619}
2620
2621bool CTvin::Tvin_is50HzFrameRateFmt ( tvin_sig_fmt_t fmt )
2622{
2623 /** component **/
2624 if ( fmt == TVIN_SIG_FMT_COMP_576P_50HZ_D000
2625 || fmt == TVIN_SIG_FMT_COMP_576I_50HZ_D000
2626 || fmt == TVIN_SIG_FMT_COMP_720P_50HZ_D000
2627 || fmt == TVIN_SIG_FMT_COMP_1080P_50HZ_D000
2628 || fmt == TVIN_SIG_FMT_COMP_1080I_50HZ_D000_A
2629 || fmt == TVIN_SIG_FMT_COMP_1080I_50HZ_D000_B
2630 || fmt == TVIN_SIG_FMT_COMP_1080I_50HZ_D000_C
2631 || fmt == TVIN_SIG_FMT_COMP_1080P_24HZ_D000
2632 || fmt == TVIN_SIG_FMT_COMP_1080P_25HZ_D000
2633 /** hdmi **/
2634 || fmt == TVIN_SIG_FMT_HDMI_720X576P_50HZ
2635 || fmt == TVIN_SIG_FMT_HDMI_1280X720P_50HZ
2636 || fmt == TVIN_SIG_FMT_HDMI_1920X1080I_50HZ_A
2637 || fmt == TVIN_SIG_FMT_HDMI_1440X576I_50HZ
2638 || fmt == TVIN_SIG_FMT_HDMI_1440X288P_50HZ
2639 || fmt == TVIN_SIG_FMT_HDMI_2880X576I_50HZ
2640 || fmt == TVIN_SIG_FMT_HDMI_2880X288P_50HZ
2641 || fmt == TVIN_SIG_FMT_HDMI_1440X576P_50HZ
2642 || fmt == TVIN_SIG_FMT_HDMI_1920X1080P_50HZ
2643 || fmt == TVIN_SIG_FMT_HDMI_1920X1080I_50HZ_B
2644 || fmt == TVIN_SIG_FMT_HDMI_1280X720P_50HZ_FRAME_PACKING
2645 || fmt == TVIN_SIG_FMT_HDMI_1920X1080I_50HZ_FRAME_PACKING
2646 || fmt == TVIN_SIG_FMT_HDMI_720X576P_50HZ_FRAME_PACKING
2647 /** cvbs **/
2648 || fmt == TVIN_SIG_FMT_CVBS_PAL_I
2649 || fmt == TVIN_SIG_FMT_CVBS_PAL_M
2650 || fmt == TVIN_SIG_FMT_CVBS_PAL_CN
2651 || fmt == TVIN_SIG_FMT_CVBS_SECAM ) {
2652 LOGD ( "%s, Frame rate == 50Hz.", CFG_SECTION_TV );
2653 return true;
2654 } else {
2655 LOGD ( "%s, Frame rate != 50Hz.", CFG_SECTION_TV );
2656 return false;
2657 }
2658}
2659
2660bool CTvin::Tvin_IsDeinterlaceFmt ( tvin_sig_fmt_t fmt )
2661{
2662 if ( fmt == TVIN_SIG_FMT_COMP_480I_59HZ_D940
2663 || fmt == TVIN_SIG_FMT_COMP_576I_50HZ_D000
2664 || fmt == TVIN_SIG_FMT_COMP_1080I_47HZ_D952
2665 || fmt == TVIN_SIG_FMT_COMP_1080I_48HZ_D000
2666 || fmt == TVIN_SIG_FMT_COMP_1080I_50HZ_D000_A
2667 || fmt == TVIN_SIG_FMT_COMP_1080I_50HZ_D000_B
2668 || fmt == TVIN_SIG_FMT_COMP_1080I_50HZ_D000_C
2669 || fmt == TVIN_SIG_FMT_COMP_1080I_60HZ_D000
2670 || fmt == TVIN_SIG_FMT_HDMI_1440X480I_120HZ
2671 || fmt == TVIN_SIG_FMT_HDMI_1440X480I_240HZ
2672 || fmt == TVIN_SIG_FMT_HDMI_1440X480I_60HZ
2673 || fmt == TVIN_SIG_FMT_HDMI_1440X576I_100HZ
2674 || fmt == TVIN_SIG_FMT_HDMI_1440X576I_200HZ
2675 || fmt == TVIN_SIG_FMT_HDMI_1440X576I_50HZ
2676 || fmt == TVIN_SIG_FMT_HDMI_1920X1080I_100HZ
2677 || fmt == TVIN_SIG_FMT_HDMI_1920X1080I_120HZ
2678 || fmt == TVIN_SIG_FMT_HDMI_1920X1080I_50HZ_A
2679 || fmt == TVIN_SIG_FMT_HDMI_1920X1080I_50HZ_B
2680 || fmt == TVIN_SIG_FMT_HDMI_1920X1080I_50HZ_FRAME_PACKING
2681 || fmt == TVIN_SIG_FMT_HDMI_1920X1080I_60HZ
2682 || fmt == TVIN_SIG_FMT_HDMI_1920X1080I_60HZ_FRAME_PACKING
2683 || fmt == TVIN_SIG_FMT_HDMI_2880X480I_60HZ
2684 || fmt == TVIN_SIG_FMT_HDMI_2880X576I_50HZ
2685 || fmt == TVIN_SIG_FMT_CVBS_NTSC_M
2686 || fmt == TVIN_SIG_FMT_CVBS_NTSC_443
2687 || fmt == TVIN_SIG_FMT_CVBS_PAL_60
2688 || fmt == TVIN_SIG_FMT_CVBS_PAL_CN
2689 || fmt == TVIN_SIG_FMT_CVBS_PAL_I
2690 || fmt == TVIN_SIG_FMT_CVBS_PAL_M
2691 || fmt == TVIN_SIG_FMT_CVBS_SECAM ) {
2692 LOGD ( "%s, Interlace format.", CFG_SECTION_TV );
2693 return true;
2694 } else {
2695 LOGD ( "%s, Progressive format.", CFG_SECTION_TV );
2696 return false;
2697 }
2698}
2699
2700int CTvin::Tvin_StartDecoder ( tvin_info_t &info )
2701{
2702 if ( m_is_decoder_start == false ) {
2703 m_tvin_param.info = info;
2704
2705 if ( VDIN_StartDec ( &m_tvin_param ) >= 0 ) {
2706 LOGD ( "StartDecoder succeed." );
2707 m_is_decoder_start = true;
2708 } else {
2709 LOGW ( "StartDecoder failed." );
2710 return -1;
2711 }
2712 } else {
2713 return -2;
2714 }
2715
2716 return 0;
2717}
2718
2719int CTvin::SwitchPort (tvin_port_t source_port )
2720{
2721 int ret = 0;
2722 LOGD ("%s, source_port = %x", __FUNCTION__, source_port);
2723 ret = Tvin_StopDecoder();
2724 if ( 0 == ret || 1 == ret ) {
2725 if ( 1 == ret ) { //decode not started
2726 //mVpp.Tvin_SetVideoScreenColorType ( TV_SIGNAL_BLUE_PATTERN );
2727 }
2728 VDIN_ClosePort();
2729 } else {
2730 LOGW ( "%s,stop decoder failed.", __FUNCTION__);
2731 return -1;
2732 }
2733 // Open Port
2734 if ( VDIN_OpenPort ( source_port ) < 0 ) {
2735 LOGD ( "%s, OpenPort failed, source_port =%x ", __FUNCTION__, source_port );
2736 }
2737 m_tvin_param.port = source_port;
2738 return 0;
2739}
2740
2741int CTvin::Tvin_StopDecoder()
2742{
2743 if ( m_is_decoder_start == true ) {
2744 if ( VDIN_StopDec() >= 0 ) {
2745 LOGD ( "StopDecoder ok!" );
2746 m_is_decoder_start = false;
2747 return 0;
2748 } else {
2749 LOGD ( "StopDecoder Failed!" );
2750 }
2751 } else {
2752 return 1;
2753 }
2754
2755 return -1;
2756}
2757
2758int CTvin::init_vdin ( void )
2759{
2760 VDIN_OpenModule();
2761 return 0;
2762}
2763
2764int CTvin::uninit_vdin ( void )
2765{
2766 VDIN_ClosePort();
2767 VDIN_CloseModule();
2768 return 0;
2769}
2770
2771int CTvin::Tvin_AddPath ( tvin_path_id_t pathid )
2772{
2773 int ret = -1;
2774 int i = 0, dly = 10;
2775 tv_path_type_t pathtype;
2776
2777 if ( pathid >= TV_PATH_VDIN_AMVIDEO && pathid < TV_PATH_DECODER_3D_AMVIDEO ) {
2778 pathtype = TV_PATH_TYPE_TVIN;
2779 } else {
2780 pathtype = TV_PATH_TYPE_DEFAULT;
2781 }
2782
2783 if ( pathid >= TV_PATH_VDIN_AMVIDEO && pathid < TV_PATH_DECODER_3D_AMVIDEO ) {
2784 if ( m_pathid == pathid ) {
2785 LOGW ( "%s, no need to add the same tvin path.\n", CFG_SECTION_TV );
2786 return 0;
2787 }
2788
2789 for ( i = 0; i < 50; i++ ) {
2790 ret = VDIN_RmTvPath();
2791
2792 if ( ret > 0 ) {
2793 LOGD ( "%s, remove tvin path ok, %d ms gone.\n", CFG_SECTION_TV, ( dly * i ) );
2794 break;
2795 } else {
2796 LOGW ( "%s, remove tvin path faild, %d ms gone.\n", CFG_SECTION_TV, ( dly * i ) );
2797 usleep ( dly * 1000 );
2798 }
2799 }
2800 } else {
2801 for ( i = 0; i < 50; i++ ) {
2802 ret = VDIN_RmDefPath();
2803
2804 if ( ret > 0 ) {
2805 LOGD ( "%s, remove default path ok, %d ms gone.\n", CFG_SECTION_TV, ( dly * i ) );
2806 break;
2807 } else {
2808 LOGW ( "%s, remove default path faild, %d ms gone.\n", CFG_SECTION_TV, ( dly * i ) );
2809 usleep ( dly * 1000 );
2810 }
2811 }
2812 }
2813
2814 for ( i = 0; i < 50; i++ ) {
2815 if ( pathid >= TV_PATH_VDIN_AMVIDEO && pathid < TV_PATH_DECODER_3D_AMVIDEO ) {
2816 if ( strcmp ( config_tv_path, "null" ) == 0 ) {
2817 ret = VDIN_AddVideoPath ( pathid );
2818 } else {
2819 ret = VDIN_AddPath ( config_tv_path );
2820 }
2821 } else {
2822 if ( strcmp ( config_default_path, "null" ) == 0 ) {
2823 ret = VDIN_AddVideoPath ( pathid );
2824 } else {
2825 ret = VDIN_AddPath ( config_default_path );
2826 }
2827 }
2828
2829 if ( ret >= 0 ) {
2830 LOGD ( "%s, add pathid[%d] ok, %d ms gone.\n", CFG_SECTION_TV, pathid, i );
2831 break;
2832 } else {
2833 LOGW ( "%s, add pathid[%d] faild, %d ms gone.\n", CFG_SECTION_TV, pathid, i );
2834 usleep ( dly * 1000 );
2835 }
2836 }
2837
2838 if ( pathid >= TV_PATH_VDIN_AMVIDEO && pathid < TV_PATH_MAX ) {
2839 m_pathid = pathid;
2840 }
2841
2842 return ret;
2843}
2844
2845
2846int CTvin::Tvin_RemovePath ( tv_path_type_t pathtype )
2847{
2848 int ret = -1;
2849 int i = 0, dly = 10;
2850
2851 for ( i = 0; i < 500; i++ ) {
2852 ret = Tvin_CheckPathActive ( pathtype, 0 );
2853
2854 if ( ret == TV_PATH_STATUS_INACTIVE ) {
2855 LOGD ( "%s, check path is inactive, %d ms gone.\n", CFG_SECTION_TV, ( dly * i ) );
2856 break;
2857 } else if ( ret == TV_PATH_STATUS_INACTIVE || ret == TV_PATH_STATUS_ERROR ) {
2858 usleep ( dly * 1000 );
2859 } else {
2860 break;
2861 }
2862 }
2863
2864 if ( i == 500 ) {
2865 LOGE ( "%s, check path active faild, %d ms gone.\n", CFG_SECTION_TV, ( dly * i ) );
2866 }
2867
2868 if ( pathtype == TV_PATH_TYPE_DEFAULT ) {
2869 for ( i = 0; i < 50; i++ ) {
2870 ret = VDIN_RmDefPath();
2871
2872 if ( ret > 0 ) {
2873 LOGD ( "%s, remove default path ok, %d ms gone.\n", CFG_SECTION_TV, ( dly * i ) );
2874 break;
2875 } else {
2876 LOGW ( "%s, remove default path faild, %d ms gone.\n", CFG_SECTION_TV, ( dly * i ) );
2877 usleep ( dly * 1000 );
2878 }
2879 }
2880 } else if ( pathtype == TV_PATH_TYPE_TVIN ) {
2881 for ( i = 0; i < 50; i++ ) {
2882 ret = VDIN_RmTvPath();
2883
2884 if ( ret > 0 ) {
2885 LOGD ( "%s, remove tvin path ok, %d ms gone.\n", CFG_SECTION_TV, ( dly * i ) );
2886 break;
2887 } else {
2888 LOGW ( "%s, remove tvin path faild, %d ms gone.\n", CFG_SECTION_TV, ( dly * i ) );
2889 usleep ( dly * 1000 );
2890 }
2891 }
2892
2893 m_pathid = TV_PATH_DECODER_3D_AMVIDEO;
2894 } else if ( pathtype == TV_PATH_TYPE_TVIN_PREVIEW ) {
2895 for ( i = 0; i < 50; i++ ) {
2896 ret = VDIN_RmPreviewPath();
2897
2898 if ( ret > 0 ) {
2899 LOGD ( "%s, remove preview path ok, %d ms gone.\n", CFG_SECTION_TV, ( dly * i ) );
2900 break;
2901 } else {
2902 LOGW ( "%s, remove preview path faild, %d ms gone.\n", CFG_SECTION_TV, ( dly * i ) );
2903 usleep ( dly * 1000 );
2904 }
2905 }
2906
2907 m_pathid = TV_PATH_DECODER_NEW3D_WITHOUTPPMGR_AMVIDEO;
2908 } else {
2909 ret = -1;
2910 }
2911
2912 return ret;
2913}
2914
2915
2916int CTvin::Tvin_CheckPathActive ( tv_path_type_t path_type, int isCheckD2D3 )
2917{
2918 FILE *f;
2919 char path[256];
2920 char decoder_str[20] = "default {";
2921 char tvin_str[20] = "tvpath {";
2922 char decoder_active_str[20] = "decoder(1)";
2923 char tvin_active_str[20] = "vdin0(1)";
2924 char di_active_str[20] = "deinterlace(1)";
2925 char d2d3_active_str[20] = "d2d3(1)";
2926 char decoder_inactive_str[20] = "decoder(0)";
2927 char tvin_inactive_str[20] = "vdin0(0)";
2928 char di_inactive_str[20] = "deinterlace(0)";
2929 char d2d3_inactive_str[20] = "d2d3(0)";
2930
2931 char *str_find = NULL;
2932 char *active_str = NULL;
2933 char *inactive_str = NULL;
2934 int mount_freq;
2935 int match;
2936 int is_active = TV_PATH_STATUS_INACTIVE;
2937
2938 memset ( path, 0, 255 );
2939
2940 f = fopen ( "/sys/class/vfm/map", "r" );
2941 if ( !f ) {
2942 LOGE ( "%s, can not open /sys/class/vfm/map!\n", CFG_SECTION_TV );
2943 return TV_PATH_STATUS_NO_DEV;
2944 }
2945
2946 while ( fgets ( path, 254, f ) ) {
2947 if ( path_type == TV_PATH_TYPE_DEFAULT ) {
2948 str_find = strstr ( path, decoder_str );
2949 active_str = decoder_active_str;
2950 inactive_str = decoder_inactive_str;
2951 } else if ( path_type == TV_PATH_TYPE_TVIN || path_type == TV_PATH_TYPE_TVIN_PREVIEW ) {
2952 str_find = strstr ( path, tvin_str );
2953 active_str = tvin_active_str;
2954 inactive_str = tvin_inactive_str;
2955 } else {
2956 LOGW ( "%s, there is no %d path_type.\n", CFG_SECTION_TV, path_type );
2957 break;
2958 }
2959
2960 if ( str_find ) {
2961 if ( isCheckD2D3 == 0 ) {
2962 if ( strstr ( str_find, active_str ) && strstr ( str_find, di_active_str ) ) {
2963 is_active = TV_PATH_STATUS_ACTIVE;
2964 //LOGD("%s, %s is active.\n", CFG_SECTION_TV, path);
2965 } else if ( strstr ( str_find, inactive_str )
2966 && ( strstr ( str_find, di_inactive_str ) || ( !strstr ( str_find, di_inactive_str ) ) )
2967 ) {
2968 is_active = TV_PATH_STATUS_INACTIVE;
2969 //LOGD("%s, %s is inactive.\n", CFG_SECTION_TV, path);
2970 } else {
2971 is_active = TV_PATH_STATUS_ERROR;
2972 LOGE ( "%s, %s is error!\n", CFG_SECTION_TV, path );
2973 }
2974
2975 break;
2976 } else {
2977 if ( strstr ( str_find, active_str ) && strstr ( str_find, di_active_str ) && strstr ( str_find, d2d3_active_str ) ) {
2978 is_active = TV_PATH_STATUS_ACTIVE;
2979 //LOGD("%s, %s is active.\n", CFG_SECTION_TV, path);
2980 } else if ( strstr ( str_find, inactive_str )
2981 && ( strstr ( str_find, di_inactive_str ) || ( !strstr ( str_find, di_inactive_str ) ) )
2982 && ( strstr ( str_find, d2d3_inactive_str ) || ( !strstr ( str_find, d2d3_inactive_str ) ) )
2983 ) {
2984 is_active = TV_PATH_STATUS_INACTIVE;
2985 //LOGD("%s, %s is inactive.\n", CFG_SECTION_TV, path);
2986 } else {
2987 is_active = TV_PATH_STATUS_ERROR;
2988 LOGE ( "%s, %s is error!\n", CFG_SECTION_TV, path );
2989 }
2990
2991 break;
2992 }
2993 }
2994 }
2995
2996 fclose ( f );
2997
2998 return is_active;
2999}
3000
3001
3002int CTvin::Tv_init_afe ( void )
3003{
3004 AFE_OpenModule();
3005 return 0;
3006}
3007
3008int CTvin::Tv_uninit_afe ( void )
3009{
3010 AFE_CloseModule();
3011 return 0;
3012}
3013
3014int CTvin::get_hdmi_sampling_rate()
3015{
3016 int fd;
3017 int val = 0;
3018 char bcmd[16];
3019 fd = open ( "/sys/module/tvin_hdmirx/parameters/audio_sample_rate", O_RDONLY );
3020 if ( fd >= 0 ) {
3021 read ( fd, bcmd, sizeof ( bcmd ) );
3022 val = strtol ( bcmd, NULL, 10 );
3023 close ( fd );
3024 }
3025
3026 return val;
3027}
3028
3029//**************************************************************************
3030CTvin::CTvinSigDetect::CTvinSigDetect ()
3031{
3032 mDetectState = STATE_STOPED;
3033 mpObserver = NULL;
3034 initSigState();
3035}
3036
3037CTvin::CTvinSigDetect::~CTvinSigDetect()
3038{
3039}
3040
3041int CTvin::CTvinSigDetect::startDetect(bool bPause)
3042{
3043 LOGD ( "startDetect()" );
3044
3045 if ( mDetectState == STATE_RUNNING || mDetectState == STATE_PAUSE ) {
3046 return mDetectState;
3047 }
3048
3049 m_request_pause_detect = bPause;
3050 initSigState();
3051 this->run();
3052 return mDetectState;
3053}
3054
3055int CTvin::CTvinSigDetect::initSigState()
3056{
3057 m_cur_sig_info.trans_fmt = TVIN_TFMT_2D;
3058 m_cur_sig_info.fmt = TVIN_SIG_FMT_NULL;
3059 m_cur_sig_info.status = TVIN_SIG_STATUS_NULL;
3060 m_cur_sig_info.reserved = 0;
3061 m_pre_sig_info = m_cur_sig_info;
3062 mKeepNosigTime = 0;
3063 m_is_nosig_checktimes_once_valid = false;
3064 mResumeLaterTime = 0;
3065 return 0;
3066}
3067
3068int CTvin::CTvinSigDetect::stopDetect()
3069{
3070 CMutex::Autolock _l ( mLock );
3071 LOGD ( "stopDetect()" );
3072 requestExit();
3073 return 0;
3074}
3075
3076int CTvin::CTvinSigDetect::pauseDetect()
3077{
3078 CMutex::Autolock _l ( mLock );
3079 LOGD ( "pauseDetect()" );
3080 m_request_pause_detect = true;
3081 return 0;
3082}
3083
3084int CTvin::CTvinSigDetect::requestAndWaitPauseDetect()
3085{
3086 CMutex::Autolock _l ( mLock );
3087 LOGD ( "requestAndWaitPauseDetect()" );
3088 m_request_pause_detect = true;
3089
3090 if ( mDetectState == STATE_RUNNING ) {
3091 mRequestPauseCondition.wait ( mLock );
3092 }
3093
3094 return 0;
3095}
3096
3097int CTvin::CTvinSigDetect::resumeDetect(int later)//ms
3098{
3099 CMutex::Autolock _l ( mLock );
3100 LOGD ( "resumeDetect()" );
3101 mResumeLaterTime = later;
3102 m_request_pause_detect = false;
3103 mDetectPauseCondition.signal();
3104 return 0;
3105}
3106
3107void CTvin::CTvinSigDetect::setVdinNoSigCheckKeepTimes(int times, bool isOnce)
3108{
3109 LOGD("setVdinNoSigCheckKeepTimes mKeepNosigTime = %d, times = %d", mKeepNosigTime, times);
3110 mKeepNosigTime = times;
3111 m_is_nosig_checktimes_once_valid = isOnce;
3112}
3113
3114int CTvin::CTvinSigDetect::Tv_TvinSigDetect ( int &sleeptime )
3115{
3116 CTvin::getInstance()->VDIN_GetSignalInfo ( &m_cur_sig_info ); //get info
3117 //set no sig check times
3118 static long long sNosigKeepTime = 0;
3119 //LOGD("stime=%d status=%d, fmt = %d sNosigKeepTime = %d, mKeepNosigTime = %d", sleeptime, m_cur_sig_info.status,m_cur_sig_info.fmt, sNosigKeepTime, mKeepNosigTime);
3120 if ( m_cur_sig_info.status == TVIN_SIG_STATUS_NOSIG || m_cur_sig_info.status == TVIN_SIG_STATUS_NULL ) {
3121 sNosigKeepTime += sleeptime;
3122 if ( sNosigKeepTime > mKeepNosigTime ) { //real no sig
3123 //cur is no sig
3124 if ( m_is_nosig_checktimes_once_valid ) { //just once change,is nosig, and default it
3125 m_is_nosig_checktimes_once_valid = false;
3126 mKeepNosigTime = 0;
3127 }
3128 } else {//not
3129 m_cur_sig_info.status = m_pre_sig_info.status;
3130 }
3131 } else {
3132 sNosigKeepTime = 0;
3133 if ( m_is_nosig_checktimes_once_valid ) { //just once change,not nosig,default is
3134 m_is_nosig_checktimes_once_valid = false;
3135 mKeepNosigTime = 0;
3136 }
3137 }
3138
3139 //if state change
3140 if ( m_cur_sig_info.status != m_pre_sig_info.status ) {
3141 sleeptime = 20;
3142
3143 if ( m_cur_sig_info.status == TVIN_SIG_STATUS_STABLE ) { // to stable
3144 mpObserver->onSigToStable();
3145 } else if ( m_pre_sig_info.status == TVIN_SIG_STATUS_STABLE && m_cur_sig_info.status == TVIN_SIG_STATUS_UNSTABLE ) { //stable to unstable
3146 //mVpp.Tvin_SetVideoScreenColorType ( TV_SIGNAL_BLACK_PATTERN );
3147 mpObserver->onSigStableToUnstable();
3148 } else if ( m_pre_sig_info.status == TVIN_SIG_STATUS_STABLE && m_cur_sig_info.status == TVIN_SIG_STATUS_NOTSUP ) {
3149 mpObserver->onSigStableToUnSupport();
3150 } else if ( m_pre_sig_info.status == TVIN_SIG_STATUS_STABLE && m_cur_sig_info.status == TVIN_SIG_STATUS_NOSIG ) {
3151 mpObserver->onSigStableToNoSig();
3152 } else if ( m_pre_sig_info.status == TVIN_SIG_STATUS_UNSTABLE && m_cur_sig_info.status == TVIN_SIG_STATUS_NOTSUP ) {
3153 mpObserver->onSigUnStableToUnSupport();
3154 } else if ( m_pre_sig_info.status == TVIN_SIG_STATUS_UNSTABLE && m_cur_sig_info.status == TVIN_SIG_STATUS_NOSIG ) {
3155 mpObserver->onSigUnStableToNoSig();
3156 } else if ( m_pre_sig_info.status == TVIN_SIG_STATUS_NULL && m_cur_sig_info.status == TVIN_SIG_STATUS_NOSIG ) {
3157 mpObserver->onSigNullToNoSig();
3158 } else if ( m_pre_sig_info.status == TVIN_SIG_STATUS_NOSIG && m_cur_sig_info.status == TVIN_SIG_STATUS_UNSTABLE ) {
3159 mpObserver->onSigNoSigToUnstable();
3160 }
3161 } else { //state not change
3162 sleeptime = 20;
3163
3164 switch ( m_cur_sig_info.status ) {
3165 case TVIN_SIG_STATUS_STABLE:
3166 mpObserver->onSigStillStable();
3167 if ( m_cur_sig_info.trans_fmt != m_pre_sig_info.trans_fmt ) {
3168 mpObserver->onStableTransFmtChange();
3169 }
3170 if (m_cur_sig_info.fmt != m_pre_sig_info.fmt) {
3171 mpObserver->onStableSigFmtChange();
3172 }
3173 break;
3174
3175 case TVIN_SIG_STATUS_NOTSUP:
3176 mpObserver->onSigStillNoSupport();
3177 break;
3178
3179 case TVIN_SIG_STATUS_UNSTABLE:
3180 mpObserver->onSigStillUnstable();
3181 break;
3182
3183 case TVIN_SIG_STATUS_NOSIG:
3184 mpObserver->onSigStillNosig();
3185 break;
3186
3187 case TVIN_SIG_STATUS_NULL:
3188 default:
3189 mpObserver->onSigStillNull();
3190 break;
3191 }
3192 }
3193
3194 m_pre_sig_info = m_cur_sig_info;//backup info
3195 return sleeptime;
3196}
3197
3198bool CTvin::CTvinSigDetect::threadLoop()
3199{
3200 //enter onStart()
3201 if ( mpObserver == NULL ) {
3202 return false;
3203 }
3204
3205 int sleeptime = 200;//ms
3206 mDetectState = STATE_RUNNING;
3207 mpObserver->onSigDetectEnter();
3208
3209 while ( !exitPending() ) { //requietexit() or requietexitWait() not call
3210 while ( m_request_pause_detect ) {
3211 mLock.lock();
3212 mRequestPauseCondition.broadcast();
3213 mDetectState = STATE_PAUSE;
3214 mDetectPauseCondition.wait ( mLock ); //first unlock,when return,lock again,so need,call unlock
3215 mDetectState = STATE_RUNNING;
3216 mLock.unlock();
3217 //
3218 while (!m_request_pause_detect && mResumeLaterTime > 0) {
3219 //LOGD("mResumeLaterTime = %d", mResumeLaterTime);
3220 usleep(10 * 1000);
3221 mResumeLaterTime -= 10;
3222 }
3223 }
3224
3225 mResumeLaterTime = 0;
3226 mpObserver->onSigDetectLoop();
3227 Tv_TvinSigDetect ( sleeptime );
3228 //可以优化
3229 if ( !m_request_pause_detect ) {
3230 usleep ( sleeptime * 1000 );
3231 }
3232 }
3233
3234 mDetectState = STATE_STOPED;
3235 mRequestPauseCondition.broadcast();
3236 //exit
3237 //return true, run again, return false,not run.
3238 return false;
3239}
3240
3241v4l2_std_id CTvin::CvbsFtmToV4l2ColorStd(tvin_sig_fmt_t fmt)
3242{
3243 v4l2_std_id v4l2_std;
3244 if (fmt == TVIN_SIG_FMT_CVBS_NTSC_M || fmt == TVIN_SIG_FMT_CVBS_NTSC_443) {
3245 v4l2_std = V4L2_COLOR_STD_NTSC;
3246 } else if (fmt >= TVIN_SIG_FMT_CVBS_PAL_I && fmt <= TVIN_SIG_FMT_CVBS_PAL_CN) {
3247 v4l2_std = V4L2_COLOR_STD_PAL;
3248 } else if (fmt == TVIN_SIG_FMT_CVBS_SECAM) {
3249 v4l2_std = V4L2_COLOR_STD_SECAM;
3250 } else {
3251 v4l2_std = V4L2_COLOR_STD_PAL;
3252 }
3253 return v4l2_std;
3254}
3255
3256int CTvin::CvbsFtmToColorStdEnum(tvin_sig_fmt_t fmt)
3257{
3258 v4l2_std_id v4l2_std;
3259 if (fmt == TVIN_SIG_FMT_CVBS_NTSC_M || fmt == TVIN_SIG_FMT_CVBS_NTSC_443) {
3260 v4l2_std = CC_ATV_VIDEO_STD_NTSC;
3261 } else if (fmt >= TVIN_SIG_FMT_CVBS_PAL_I && fmt <= TVIN_SIG_FMT_CVBS_PAL_CN) {
3262 v4l2_std = CC_ATV_VIDEO_STD_PAL;
3263 } else if (fmt == TVIN_SIG_FMT_CVBS_SECAM) {
3264 v4l2_std = CC_ATV_VIDEO_STD_SECAM;
3265 } else {
3266 v4l2_std = CC_ATV_VIDEO_STD_PAL;
3267 }
3268 return v4l2_std;
3269}
3270
3271int CTvin::GetITContent()
3272{
3273 FILE *fp = NULL;
3274 int value = 0;
3275 fp = fopen("/sys/module/tvin_hdmirx/parameters/it_content", "r");
3276 if (fp == NULL) {
3277 LOGE ( "Open /sys/module/tvin_hdmirx/parameters/it_content error(%s)!\n", strerror ( errno ) );
3278 return -1;
3279 }
3280 fscanf(fp, "%d", &value );
3281 fclose(fp);
3282 fp = NULL;
3283 return value;
3284}
3285//**************************************************************************
3286