summaryrefslogtreecommitdiff
path: root/dxr3spuencoder.c
blob: df056e60c6569832ac50c23d4122682d945c3415 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
 *     dxr3spuencoder.c - encodes an OSD bitmap as subpicture
 *
 *	Assimilated and adapted by
 *  Stefan Schluenss <dxr3_osd@schluenss.de>
 *  Nov. 2002
 *
 * Based on the subpicture encoding routines from MPlayer and
 * the information given by
 *   Samuel Hocevar
 *   Michel Lespinasse
 *   and http://members.aol.com/mpucoder/DVD/spu.html
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 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, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

#include "dxr3spuencoder.h"

// ==================================
// dec.
cSpuData::~cSpuData()
{
	Clear();
}

// ==================================
// free buffer and set it to 0
void cSpuData::Clear()
{
	if (data)
	{
		free(data);
		count = malloc_size = 0;
	}
}

// ==================================
// wirte a byte to spu buffer
void cSpuData::WriteByte(uint8_t byte)
{
	if (count >= malloc_size)
	{
		data = (u_char*)realloc(data, malloc_size += 2048);
	}
	
	data[count++] = byte;
}

// ==================================
void cSpuData::WriteNibble(int *higher_nibble, uint8_t nibble)
{
}

// ==================================
void cSpuData::WriteRle(int *higher_nibble, int length, int color)
{
}


// ==================================
cSPUEncoder::cSPUEncoder()
{
	m_lastwindow = 0;

	// clear osd
	memset(m_OSD, 0x00 ,OSDWIDTH * OSDHEIGHT);
}

// ==================================
// main function for the osd
// makes life nicer :)
int cSPUEncoder::Cmd(OSD_Command cmd, int color, int x0, int y0, int x1, int y1, const void *data)
{
	switch (cmd)
	{
	case OSD_SetWindow:
		//  (x0) set window with number 0<x0<8 as current
		if (x0 < 8 && x0 > 0)
		{
			m_lastwindow = x0;
			return 0;
		}

		return -1;
		break;

	case OSD_Open:
		// (x0,y0,x1,y1,BitPerPixel[2/4/8](color&0x0F),mix[0..15](color&0xF0))
		// Opens OSD with this size and bit depth
		// returns 0 on success, -1 on DRAM allocation error, -2 on "already open"
		m_windows[m_lastwindow].x0 = x0;
		m_windows[m_lastwindow].y0 = y0;
		m_windows[m_lastwindow].x1 = x1;
		m_windows[m_lastwindow].y1 = y1;

		// calculate new active osd area
		CalculateActiveOsdArea();

		return 0;
		break;

	case OSD_SetPalette:
		// Spu->Cmd(OSD_SetPalette, 0, NumColors - 1, 0, 0, 0, Colors);
		// (firstcolor{color},lastcolor{x0},data)
		// Set a number of entries in the palette
		// sets the entries "firstcolor" through "lastcolor" from the array "data"
		// data has 4 byte for each color:
		// R,G,B, and a opacity value: 0->transparent, 1..254->mix, 255->pixel

		return 0;
		break;

	case OSD_SetBlock:
		// (x0,y0,x1,y1,increment{color},data)
		// fills pixels x0,y0 through  x1,y1 with the content of data[]
		// inc contains the width of one line in the data block,
		// inc<=0 uses blockwidth as linewidth
		// returns 0 on success, -1 on clipping all pixel

		return 0;
		break;

	case OSD_Close:
		// clear colors from plattemanager

		// set windows position to 0
		m_windows[m_lastwindow].x0 = 0;
		m_windows[m_lastwindow].y0 = 0;
		m_windows[m_lastwindow].x1 = 0;
		m_windows[m_lastwindow].y1 = 0;

		// calculate new active osd area
		CalculateActiveOsdArea();

		return 0;
		break;

	case OSD_Clear:
		// Sets all pixel to color 0
		// returns 0 on success

		// This should be done in cSPUEncoder::cSPUEncoder

		return 0;
		break;

	// not needed - at the moment
	case OSD_Show:
	case OSD_Hide:
	case OSD_Fill:
	case OSD_SetColor:
	case OSD_SetTrans:
	case OSD_SetPixel:
	case OSD_GetPixel:
	case OSD_SetRow:
	case OSD_FillRow:
	case OSD_FillBlock:
	case OSD_Line:
	case OSD_Query:
	case OSD_Test:
	case OSD_Text:
	case OSD_MoveWindow:
		break;
	};

	return -1;
}

// ==================================
// stamps window content into full osd bitmap
void cSPUEncoder::CopyBlockIntoOSD(int linewidth, int x0, int y0, int x1, int y1, u_char *data)
{
	int i;
	int w;
	u_char *cp;
	u_char *sp = data;


	// linewidth contains the width of one line in the data block,
	// linewidth<=0 uses blockwidth as linewidth
	if (linewidth <= 0)
	{
		w = m_windows[m_lastwindow].x1 - m_windows[m_lastwindow].x0;
	}
	else
	{
		w = linewidth;
	}
	
    for (i = y0; i <= y1; ++i) 
	{
        cp = &m_OSD[i*OSDWIDTH + x0];
        if ((cp+x1-x0+1) < &m_OSD[OSDWIDTH * OSDHEIGHT-1]) 
		{
            for (int xx=0; xx <= (x1-x0); xx++) 
			{
                *(cp+xx) = m_windows[m_lastwindow].colors[*(sp+xx) & 0x0f];
            }
        } 
		else 
		{
            continue;
        }
        sp += w;
	}
}

// ==================================
// we _only_ write usefull data
void cSPUEncoder::CalculateActiveOsdArea()
{
	// reset
	m_active_osd.Reset();

	// calculate
	for (int i = 0; i < 8; i++)
	{
		m_active_osd.x0 = std::max(m_active_osd.x0, m_windows[i].x0);
		m_active_osd.x1 = std::max(m_active_osd.x1, m_windows[i].y0);
		m_active_osd.y0 = std::max(m_active_osd.y0, m_windows[i].x1);
		m_active_osd.y1 = std::max(m_active_osd.y1, m_windows[i].y1);
	}

	cLog::Instance() << "OSD x0: " << m_active_osd.x0 << "\n";
	cLog::Instance() << "OSD y0: " << m_active_osd.y0 << "\n";
	cLog::Instance() << "OSD x1: " << m_active_osd.x1 << "\n";
	cLog::Instance() << "OSD y1: " << m_active_osd.y1 << "\n";
}