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