aboutsummaryrefslogtreecommitdiffhomepage
path: root/libvo/spuenc.c
blob: 7b29337cf6fc92f18c0e09a5e7f601f9f1af0127 (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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
 * subpic_encode.c - encodes a pixmap with RLE
 *
 * Copyright (C) 2000   Alejandro J. Cura <alecu@protocultura.net>
 *
 * (modified a bit to work with the dxr3 driver...4/2/2002 cg)
 * 
 * Based on the hard work of:
 *
 *   Samuel Hocevar <sam@via.ecp.fr> and Michel Lespinasse <walken@via.ecp.fr>
 *
 * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
 *
 */

#include <stdio.h>
#include <stdlib.h>
#include "unistd.h"
#include "spuenc.h"

static void
encode_do_control(int x,int y, encodedata* ed, pixbuf* pb) {
	int controlstart= ed->count;
	int x1;
	int i;
	unsigned int top, left, bottom, right;

	top= 450 - pb->y/2;
   	left=(720 / 2) - (pb->x / 2);
	top= 32;//this forces the first bit to be visible on a TV
	left= 32;//you could actually pass in x/y and do some nice
	         //calculations for making it look right...
	bottom= top + pb->y - 1;
	right= left + pb->x - 1;

/* the format of this is well described by a page:
 * http://members.aol.com/mpucoder/DVD/spu.html
 * 
 * note I changed the layout of commands to turn off the subpic as the 
 * first command, and then turn on the new subpic...this is so we can 
 * leave the subpic on for an arbitrary ammount of time as controlled by 
 * mplayer (ie when we turn on the subpic we don't know how long it should
 * stay on when using mplayer).
 * with this layout we turn off the last subpic as we are turning on the 
 * new one.
 * The original hd it turn on the subpic, and delay the turn off command using
 * the durration/delay feature.
 * */
	/* start at x0+2*/
	i= controlstart;
	/* display duration... */
//	ed->data[i++]= 0x00;
//	ed->data[i++]= 0x00; //durration before turn off command occurs
			     //in 90000/1024 units
	
	/* x1 */
//	x1=i+4;
//	ed->data[i++]= x1 >> 8;//location of next command block
//	ed->data[i++]= x1 & 0xff;
	/* finish it */
//	ed->data[i++]= 0x02;//turn off command
//	ed->data[i++]= 0xff;//end of command block
	x1= i; //marker for last command block address 
	
	/* display duration... */
	ed->data[i++]= 0x00;
	ed->data[i++]= 0x00; //durration before turn on command occurs
			     //in 90000/1024 units
	/* x1 */
	ed->data[i++]= x1 >> 8;  //since this is the last command block, this
	ed->data[i++]= x1 & 0xff;//points back to itself


	/* 0x01: start displaying */
	ed->data[i++]= 0x01;

	/* 0x03: palette info */
	ed->data[i++]= 0x03;
	ed->data[i++]= 0x08;
	ed->data[i++]= 0x7f;
/*
 * The palette is a coded index (one of 16) 0 is black, 0xf is white 
 * (unless you screw with the default palette)
 * for what I am doing I only use white. 
 * 7 is lt grey, and 8 is dk grey...
 * */
	/* 0x04: transparency info (reversed) */
	ed->data[i++]= 0x04;
	ed->data[i++]= 0xFF;//change the opacity values of the color entries
	ed->data[i++]= 0xF0;//say if you wanted white text on a black backround
			    //note you will have to work harder, by finding the
	//bounding box of the text, and use a non transparent black palette
	// entry to fill the backround with, (say color 1 instead of 0)

	/* 0x05: coordinates */
	ed->data[i++]= 0x05;
	ed->data[i++]= left >> 4;
	ed->data[i++]= ((left&0xf)<<4)+(right>>8);
	ed->data[i++]= (right&0xff);
	ed->data[i++]= top >> 4;
	ed->data[i++]= ((top&0xf)<<4)+(bottom>>8);
	ed->data[i++]= (bottom&0xff);

	/* 0x06: both fields' offsets */
	ed->data[i++]= 0x06;
	ed->data[i++]= 0x00;
	ed->data[i++]= 0x04;
	ed->data[i++]= ed->oddstart >> 8;
	ed->data[i++]= ed->oddstart & 0xff;

	/* 0xFF: end sequence */
	ed->data[i++]= 0xFF;
	if(! i&1 ) {
		ed->data[i++]= 0xff;
	}

	/* x0 */
	ed->data[2]= (controlstart) >> 8;
	ed->data[3]= (controlstart) & 0xff;
	
	/* packet size */
	ed->data[0]= i >> 8;
	ed->data[1]= i & 0xff;
	
	ed->count= i;
}

static void
encode_put_nibble( encodedata* ed, unsigned char nibble ) {
	if( ed->nibblewaiting ) {
		ed->data[ed->count++]|= nibble;
		ed->nibblewaiting= 0;
	} else {
		ed->data[ed->count]= nibble<<4;
		ed->nibblewaiting= 1;
	}
}

static void
encode_pixels( encodedata* ed, int color, int number ) {
	if(number > 3) {
		if(number > 15) {
			encode_put_nibble( ed, 0 );
			if(number > 63) {
				encode_put_nibble( ed, (number & 0xC0)>>6 );
			}
		}
		encode_put_nibble( ed, (number & 0x3C)>>2 );
	}
	encode_put_nibble( ed, ((number & 0xF)<<2) | color);
}

static void
encode_eol( encodedata* ed ) {
	if( ed->nibblewaiting ) {
		ed->count++;
		ed->nibblewaiting= 0;
	}
	ed->data[ed->count++]= 0x00;
	ed->data[ed->count++]= 0x00;
}

static void
encode_do_row( encodedata* ed, pixbuf* pb, int row ) {
	int i= 0;
	unsigned char* pix= pb->pixels + row * pb->x;
	int color= *pix;
	int n= 0; /* the number of pixels of this color */
	
	while( i++ < pb->x ) {
		/* FIXME: watch this space for EOL */
		if( *pix != color || n == 255 ) {
			encode_pixels( ed, color, n );
			color= *pix;
			n= 1;
		} else {
			n++;
		}
		pix++;
	}

	/* this small optimization: (n>63) can save up to two bytes per line
	 * I wonder if this is compatible with all the hardware... */
	if( color == 0 && n > 63 ) {
		encode_eol( ed );
	} else {
		encode_pixels( ed, color, n );
	}

	if( ed->nibblewaiting ) {
		ed->count++;
		ed->nibblewaiting= 0;
	}
}


void
pixbuf_encode_rle(int x, int y, int w, int h, char *inbuf,  int stride,encodedata *ed){
       	pixbuf pb;
	int i, row;
	pb.x = w;
	pb.y = h;

	pb.pixels = inbuf;
	ed->count= 4;
	ed->nibblewaiting= 0;

	row= 0;
	for( i= 0; i < pb.y; i++ ) {
		encode_do_row(ed, &pb, row);
		row+= 2;
		if( row > pb.y ) {
			row= 1;
			ed->oddstart= ed->count;
		}
	}
	encode_do_control(x,y, ed, &pb);
}


void
pixbuf_load_xpm( pixbuf* pb, char* xpm[] ) {
	int colors, chrs, l, n; 
	char c[4], table[256];
	unsigned char *b, *i;

	sscanf( xpm[0], "%d %d %d %d", &pb->x, &pb->y, &colors, &chrs);
	if( colors > 4 ) {
		fprintf( stderr, "the pixmap MUST be 4 colors or less\n");
		exit (-1);
	}
	if( chrs != 1 ) {
		fprintf( stderr, "the XPM format MUST be 1 char per pixel\n");
		exit (-1);
	}
	if( pb->x > 0xFFF || pb->y > 0xFFF ) {
		fprintf( stderr, "the size is excesive\n");
		exit (-1);
	}
	
	for( l=0; l<colors; l++ ) {
		n= sscanf( xpm[l+1], "%c c #%x", &c[l], &pb->rgb[l]);
		if( n < 2 ) {
			/* this one is transparent */
			pb->rgb[l]=0xff000000;
		}
		table[(int)c[l]]=l;
	}

	pb->pixels= malloc( pb->x * pb->y );
	b= pb->pixels;
	
	for( l= colors+1; l <= pb->y + colors; l++ ) {
		i= xpm[l];
		while( (int)*i) {
			*b++ = table[*i++];
		}
	}
}

void
pixbuf_delete( pixbuf* pb ) {
	free( pb->pixels );
}