File: acp.c

package info (click to toggle)
hacktv 0%2Bgit20201203-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 1,052 kB
  • sloc: ansic: 14,314; sh: 91; makefile: 43
file content (130 lines) | stat: -rw-r--r-- 3,517 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/* hacktv - Analogue video transmitter for the HackRF                    */
/*=======================================================================*/
/* Copyright 2019 Philip Heron <phil@sanslogic.co.uk>                    */
/*                                                                       */
/* This program is free software: you can redistribute it and/or modify  */
/* it under the terms of the GNU General Public License as published by  */
/* the Free Software Foundation, either version 3 of the License, or     */
/* (at your option) any later version.                                   */
/*                                                                       */
/* This program is distributed in the hope that it will be useful,       */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of        */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         */
/* GNU General Public License for more details.                          */
/*                                                                       */
/* You should have received a copy of the GNU General Public License     */
/* along with this program.  If not, see <http://www.gnu.org/licenses/>. */

/* -=== ACP / Macrovision encoder ===- */

#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include "video.h"

int acp_init(acp_t *s, vid_t *vid)
{
	double left;
	double spacing;
	double psync_width;
	int i;
	
	memset(s, 0, sizeof(acp_t));
	
	if(vid->conf.lines == 625)
	{
		left = 8.88e-6;
		spacing = 5.92e-6;
		psync_width = 2.368e-6;
	}
	else
	{
		left = 8.288e-6;
		spacing = 8.288e-6;
		psync_width = 2.222e-6;
	}
	
	/* Calculate the levels */
	s->psync_level = vid->sync_level + round((vid->white_level - vid->sync_level) * 0.06);
	s->pagc_level  = vid->sync_level + round((vid->white_level - vid->sync_level) * 1.10);
	
	/* Calculate the width of each pulse */
	s->psync_width = round(vid->sample_rate * psync_width);
	s->pagc_width  = round(vid->sample_rate * 2.7e-6);
	
	/* Left position of each pulse */
	for(i = 0; i < 6; i++)
	{
		s->left[i] = round(vid->sample_rate * (left + spacing * i));
	}
	
	return(VID_OK);
}

void acp_free(acp_t *s)
{
	if(s == NULL) return;
	
	memset(s, 0, sizeof(acp_t));
}

int acp_render_line(vid_t *s, void *arg, int nlines, vid_line_t **lines)
{
	acp_t *a = arg;
	int i, x;
	vid_line_t *l = lines[0];
	
	i = 0;
	
	if(l->line == 1)
	{
		/* Vary the AGC pulse level, clipped sawtooth waveform */
		i = abs(l->frame * 4 % 1712 - 856) - 150;
		
		if(i < 0) i = 0;
		else if(i > 255) i = 255;
		
		i = s->yiq_level_lookup[i << 16 | i << 8 | i].y;
		
		a->pagc_level = s->sync_level + round((i - s->sync_level) * 1.10);
	}
	
	i = 0;
	
	if(s->conf.lines == 625)
	{
		/* For 625-line modes, ACP is rendered on lines 9-18 and 321-330 */
		if(l->line >=   9 && l->line <=  18) i = 1;
		if(l->line >= 321 && l->line <= 330) i = 1;
	}
	else
	{
		/* For 525-line modes, ACP is rendered on lines 12-19 and 275-282 */
		if(l->line >=  12 && l->line <=  19) i = 1;
		if(l->line >= 275 && l->line <= 282) i = 1;
	}
	
	if(i == 0 || l->vbialloc) return(1);
	
	/* Render the P-Sync / AGC pulse pairs */
	for(i = 0; i < 6; i++)
	{
		/* Render the P-Sync pulse */
		for(x = a->left[i]; x < a->left[i] + a->psync_width; x++)
		{
			l->output[x * 2] = a->psync_level;
		}
		
		/* Render the AGC pulse */
		for(; x < a->left[i] + a->psync_width + a->pagc_width; x++)
		{
			l->output[x * 2] = a->pagc_level;
		}
	}
	
	l->vbialloc = 1;
	
	return(1);
}