comparison libmpcodecs/vf_bmovl.c @ 7855:fe88f7403d64

-vop bmovl - BitMap OVerLay videofilter for MPlayer patch by Per Wigren <wigren@home.se> TODO: -dr, MMX opt, fix alpha-only png, other colorspaces
author arpi
date Wed, 23 Oct 2002 00:26:27 +0000
parents
children 6ccc14497807
comparison
equal deleted inserted replaced
7854:d6c29d863f15 7855:fe88f7403d64
1 /* vf_bmovl.c v0.9.1 - BitMap OVerLay videofilter for MPlayer
2 *
3 * (C) 2002 Per Wigren <wigren@home.se>
4 * Licenced under the GNU General Public License
5 *
6 * Use MPlayer as a framebuffer to read bitmaps and commands from a FIFO
7 * and display them in the window.
8 *
9 * FIXME: INSTRUCTION IS OUT OF DATE!!!
10 *
11 * It understands the following format:
12 * COMMAND width height xpos ypos alpha clear
13 *
14 * Commands are:
15 * RGBA32 Followed by WIDTH*HEIGHT of raw RGBA32 data.
16 * BGRA32 Followed by WIDTH*HEIGHT of raw BGRA32 data.
17 * RGB24 Followed by WIDTH*HEIGHT of raw RGB24 data.
18 * ALPHA Set alpha for area. Values can be -255 to 255.
19 * 0 = No change
20 * CLEAR Zero area
21 * OPAQUE Disable all alpha transparency!
22 * Send an ALPHA command with 0's to enable again!
23 * HIDE Hide bitmap
24 * SHOW Show bitmap
25 *
26 * Arguments are:
27 * width, height Size of image/area
28 * xpos, ypos Start blitting at X/Y position
29 * alpha Set alpha difference. 0 means same as original.
30 * 255 makes everything opaque
31 * -255 makes everything transparent
32 * If you set this to -255 you can then send a sequence of
33 * ALPHA-commands to set the area to -225, -200, -175 etc
34 * for a nice fade-in-effect! ;)
35 * clear Clear the framebuffer before blitting. 1 means clear.
36 * If 0, the image will just be blitted on top of the old
37 * one, so you don't need to send 1,8MB of RGBA32 data
38 * everytime a small part of the screen is updated.
39 *
40 * Note that you always have to send all arguments, even if they are not
41 * used for a particular command!
42 *
43 * Arguments for the filter are hidden:opaque:fifo
44 * For example 1:0:/tmp/myfifo.fifo will start the filter hidden, transparent
45 * and use /tmp/myfifo.fifo as the fifo.
46 *
47 * If you find bugs, please send me patches! ;)
48 *
49 * This filter was developed for use in Freevo (http://freevo.sf.net), but
50 * anyone is free to use it! ;)
51 *
52 */
53
54 #include <stdio.h>
55 #include <stdlib.h>
56 #include <string.h>
57 #include <unistd.h>
58 #include <errno.h>
59 #include <sys/stat.h>
60 #include <sys/types.h>
61 #include <fcntl.h>
62 #include "mp_image.h"
63 #include "vf.h"
64 #include "img_format.h"
65
66 #include "../libvo/fastmemcpy.h"
67
68 #define IS_RAWIMG 0x100
69 #define IS_IMG 0x200
70
71 #define NONE 0x000
72 #define IMG_RGBA32 0x101
73 #define IMG_ABGR32 0x102
74 #define IMG_RGB24 0x103
75 #define IMG_BGR24 0x104
76 #define IMG_PNG 0x201
77 #define CMD_CLEAR 0x001
78 #define CMD_ALPHA 0x002
79
80 #define TRUE 1
81 #define FALSE 0
82
83 #define MAX(a,b) ((a) > (b) ? (a) : (b))
84 #define MIN(a,b) ((a) < (b) ? (a) : (b))
85 #define INRANGE(a,b,c) ( ((a) < (b)) ? (b) : ( ((a) > (c)) ? (c) : (a) ) )
86
87 #define rgb2y(R,G,B) ( (0.257 * R) + (0.504 * G) + (0.098 * B) + 16 )
88 #define rgb2u(R,G,B) ( -(0.148 * R) - (0.291 * G) + (0.439 * B) + 128 )
89 #define rgb2v(R,G,B) ( (0.439 * R) - (0.368 * G) - (0.071 * B) + 128 )
90
91 #define DBG(a) (printf("DEBUG: %d\n", a))
92
93 struct vf_priv_s {
94 int w, h, x1, y1, x2, y2;
95 struct {
96 unsigned char *y, *u, *v, *a, *oa;
97 } bitmap;
98 int stream_fd;
99 fd_set stream_fdset;
100 int opaque, hidden;
101 };
102
103 static int
104 query_format(struct vf_instance_s* vf, unsigned int fmt){
105 if(fmt==IMGFMT_YV12) return VFCAP_CSP_SUPPORTED;
106 return 0;
107 }
108
109
110 static int
111 config(struct vf_instance_s* vf,
112 int width, int height, int d_width, int d_height,
113 unsigned int flags, unsigned int outfmt)
114 {
115 vf->priv->bitmap.y = malloc( width*height );
116 vf->priv->bitmap.u = malloc( width*height/4 );
117 vf->priv->bitmap.v = malloc( width*height/4 );
118 vf->priv->bitmap.a = malloc( width*height );
119 vf->priv->bitmap.oa = malloc( width*height );
120 if(!( vf->priv->bitmap.y &&
121 vf->priv->bitmap.u &&
122 vf->priv->bitmap.v &&
123 vf->priv->bitmap.a &&
124 vf->priv->bitmap.oa )) {
125 fprintf(stderr, "vf_bmovl: Could not allocate memory for bitmap buffer: %s\n", strerror(errno) );
126 exit(10);
127 }
128
129 // Set default to black...
130 memset( vf->priv->bitmap.u, 128, width*height/4 );
131 memset( vf->priv->bitmap.v, 128, width*height/4 );
132
133 vf->priv->w = vf->priv->x1 = width;
134 vf->priv->h = vf->priv->y1 = height;
135 vf->priv->y2 = vf->priv->x2 = 0;
136
137 return vf_next_config(vf, width, height, d_width, d_height, flags, outfmt);
138 }
139
140 static void
141 uninit(struct vf_instance_s *vf)
142 {
143 if(vf->priv) {
144 free(vf->priv->bitmap.y);
145 free(vf->priv->bitmap.u);
146 free(vf->priv->bitmap.v);
147 free(vf->priv->bitmap.a);
148 free(vf->priv->bitmap.oa);
149 free(vf->priv);
150 }
151 }
152
153 static int
154 _read_cmd(int fd, char *cmd, char *args) {
155 int done=FALSE, pos=0;
156 char tmp;
157
158 while(!done) {
159 if(! read( fd, &tmp, 1 ) ) return FALSE;
160 if( (tmp>='A' && tmp<='Z') || (tmp>='0' && tmp<='9') )
161 cmd[pos]=tmp;
162 else if(tmp == ' ') {
163 cmd[pos]='\0';
164 done=TRUE;
165 }
166 else if(tmp == '\n') {
167 cmd[pos]='\0';
168 args[0]='\0';
169 return TRUE;
170 }
171 if(pos++>20) {
172 cmd[0]='\0';
173 return TRUE;
174 }
175 }
176 done=FALSE; pos=0;
177 while(!done) {
178 if(! read( fd, &tmp, 1 ) ) return FALSE;
179 if( (tmp >= ' ') && (pos<100) ) args[pos]=tmp;
180 else {
181 args[pos]='\0';
182 done=TRUE;
183 }
184 pos++;
185 }
186 return TRUE;
187 }
188
189
190 static int
191 put_image(struct vf_instance_s* vf, mp_image_t* mpi){
192 int buf_x=0, buf_y=0, buf_pos=0;
193 int xpos=0, ypos=0, pos=0;
194 unsigned char red=0, green=0, blue=0;
195 int alpha;
196 mp_image_t* dmpi;
197
198 dmpi = vf_get_image(vf->next, mpi->imgfmt, MP_IMGTYPE_TEMP,
199 MP_IMGFLAG_ACCEPT_STRIDE | MP_IMGFLAG_PREFER_ALIGNED_STRIDE,
200 mpi->w, mpi->h);
201
202 memcpy( dmpi->planes[0], mpi->planes[0], mpi->stride[0] * mpi->height);
203 memcpy( dmpi->planes[1], mpi->planes[1], mpi->stride[1] * mpi->chroma_height);
204 memcpy( dmpi->planes[2], mpi->planes[2], mpi->stride[2] * mpi->chroma_height);
205
206 if(vf->priv->stream_fd >= 0) {
207 struct timeval tv;
208
209 FD_SET( vf->priv->stream_fd, &vf->priv->stream_fdset );
210 tv.tv_sec=0; tv.tv_usec=0;
211
212 if( select( vf->priv->stream_fd+1, &vf->priv->stream_fdset, NULL, NULL, &tv ) > 0) {
213 // We've got new data from the FIFO
214
215 char cmd[20], args[100];
216 int imgw,imgh,imgx,imgy,clear,imgalpha,pxsz=1,command;
217 unsigned char *buffer = NULL;
218
219 if(! _read_cmd( vf->priv->stream_fd, cmd, args) ) {
220 fprintf(stderr, "\nvf_bmovl: Error reading commands: %s\n\n", strerror(errno));
221 exit(10);
222 }
223 printf("\nDEBUG: Got: %s+%s\n", cmd, args);
224
225 command=NONE;
226 if ( strncmp(cmd,"RGBA32",6)==0 ) { pxsz=4; command = IMG_RGBA32; }
227 else if( strncmp(cmd,"ABGR32",6)==0 ) { pxsz=4; command = IMG_ABGR32; }
228 else if( strncmp(cmd,"RGB24" ,5)==0 ) { pxsz=3; command = IMG_RGB24; }
229 else if( strncmp(cmd,"BGR24" ,5)==0 ) { pxsz=3; command = IMG_BGR24; }
230 else if( strncmp(cmd,"CLEAR" ,5)==0 ) { pxsz=1; command = CMD_CLEAR; }
231 else if( strncmp(cmd,"ALPHA" ,5)==0 ) { pxsz=1; command = CMD_ALPHA; }
232 else if( strncmp(cmd,"OPAQUE",6)==0 ) vf->priv->opaque=TRUE;
233 else if( strncmp(cmd,"SHOW", 4)==0 ) vf->priv->hidden=FALSE;
234 else if( strncmp(cmd,"HIDE", 4)==0 ) vf->priv->hidden=TRUE;
235 else if( strncmp(cmd,"FLUSH" ,5)==0 ) return vf_next_put_image(vf, dmpi);
236 else {
237 fprintf(stderr, "\nvf_bmovl: Unknown command: '%s'. Ignoring.\n", cmd);
238 return vf_next_put_image(vf, dmpi);
239 }
240
241 if(command == CMD_ALPHA) {
242 sscanf( args, "%d %d %d %d %d", &imgw, &imgh, &imgx, &imgy, &imgalpha);
243 printf("\nDEBUG: ALPHA: %d %d %d %d %d\n\n",
244 imgw, imgh, imgx, imgy, imgalpha);
245 if(imgw==0 && imgh==0) vf->priv->opaque=FALSE;
246 }
247
248 if(command & IS_RAWIMG) {
249 sscanf( args, "%d %d %d %d %d %d",
250 &imgw, &imgh, &imgx, &imgy, &imgalpha, &clear);
251 printf("\nDEBUG: RAWIMG: %d %d %d %d %d %d\n\n",
252 imgw, imgh, imgx, imgy, imgalpha, clear);
253
254 buffer = malloc(imgw*imgh*pxsz);
255 if(!buffer) {
256 fprintf(stderr, "\nvf_bmovl: Couldn't allocate temporary buffer! Skipping...\n\n");
257 return vf_next_put_image(vf, dmpi);
258 }
259 printf("Got %d bytes...\n", read( vf->priv->stream_fd, buffer, (imgw*imgh*pxsz) ) );
260
261 if(clear) {
262 memset( vf->priv->bitmap.y, 0, vf->priv->w*vf->priv->h );
263 memset( vf->priv->bitmap.u, 128, vf->priv->w*vf->priv->h/4 );
264 memset( vf->priv->bitmap.v, 128, vf->priv->w*vf->priv->h/4 );
265 memset( vf->priv->bitmap.a, 0, vf->priv->w*vf->priv->h );
266 memset( vf->priv->bitmap.oa, 0, vf->priv->w*vf->priv->h );
267 vf->priv->x1 = dmpi->width;
268 vf->priv->y1 = dmpi->height;
269 vf->priv->x2 = vf->priv->y2 = 0;
270 }
271 // Define how much of our bitmap that contains graphics!
272 vf->priv->x1 = MAX( 0, MIN(vf->priv->x1, imgx) );
273 vf->priv->y1 = MAX( 0, MIN(vf->priv->y1, imgy) );
274 vf->priv->x2 = MIN( vf->priv->w, MAX(vf->priv->x2, ( imgx + imgw)) );
275 vf->priv->y2 = MIN( vf->priv->h, MAX(vf->priv->y2, ( imgy + imgh)) );
276 }
277
278 if( command == CMD_CLEAR ) {
279 sscanf( args, "%d %d %d %d", &imgw, &imgh, &imgx, &imgy);
280 printf("\nDEBUG: CLEAR: %d %d %d %d\n\n", imgw, imgh, imgx, imgy);
281
282 for( ypos=imgy ; (ypos < (imgy+imgh)) && (ypos < vf->priv->y2) ; ypos++ ) {
283 memset( vf->priv->bitmap.y + (ypos*vf->priv->w) + imgx, 0, imgw );
284 memset( vf->priv->bitmap.a + (ypos*vf->priv->w) + imgx, 0, imgw );
285 memset( vf->priv->bitmap.oa + (ypos*vf->priv->w) + imgx, 0, imgw );
286 if(ypos%2) {
287 memset( vf->priv->bitmap.u + ((ypos/2)*dmpi->stride[1]) + (imgx/2), 128, imgw/2 );
288 memset( vf->priv->bitmap.v + ((ypos/2)*dmpi->stride[2]) + (imgx/2), 128, imgw/2 );
289 }
290 } // Recalculate area that contains graphics
291 if( (imgx <= vf->priv->x1) && ( (imgw+imgx) >= vf->priv->x2) ) {
292 if( (imgy <= vf->priv->y1) && ( (imgy+imgh) >= vf->priv->y1) )
293 vf->priv->y1 = imgy+imgh;
294 if( (imgy <= vf->priv->y2) && ( (imgy+imgh) >= vf->priv->y2) )
295 vf->priv->y2 = imgy;
296 }
297 if( (imgy <= vf->priv->y1) && ( (imgy+imgh) >= vf->priv->y2) ) {
298 if( (imgx <= vf->priv->x1) && ( (imgx+imgw) >= vf->priv->x1) )
299 vf->priv->x1 = imgx+imgw;
300 if( (imgx <= vf->priv->x2) && ( (imgx+imgw) >= vf->priv->x2) )
301 vf->priv->x2 = imgx;
302 }
303 return vf_next_put_image(vf, dmpi);
304 }
305
306 for( buf_y=0 ; (buf_y < imgh) && (buf_y < (vf->priv->h-imgy)) ; buf_y++ ) {
307 for( buf_x=0 ; (buf_x < (imgw*pxsz)) && (buf_x < ((vf->priv->w+imgx)*pxsz)) ; buf_x += pxsz ) {
308 if(command & IS_RAWIMG) buf_pos = (buf_y * imgw * pxsz) + buf_x;
309 pos = ((buf_y+imgy) * vf->priv->w) + ((buf_x/pxsz)+imgx);
310
311 switch(command) {
312 case IMG_RGBA32:
313 red = buffer[buf_pos+0];
314 green = buffer[buf_pos+1];
315 blue = buffer[buf_pos+2];
316 alpha = buffer[buf_pos+3];
317 break;
318 case IMG_ABGR32:
319 alpha = buffer[buf_pos+0];
320 blue = buffer[buf_pos+1];
321 green = buffer[buf_pos+2];
322 red = buffer[buf_pos+3];
323 break;
324 case IMG_RGB24:
325 red = buffer[buf_pos+0];
326 green = buffer[buf_pos+1];
327 blue = buffer[buf_pos+2];
328 alpha = 0xFF;
329 break;
330 case IMG_BGR24:
331 blue = buffer[buf_pos+0];
332 green = buffer[buf_pos+1];
333 red = buffer[buf_pos+2];
334 alpha = 0xFF;
335 break;
336 case CMD_ALPHA:
337 vf->priv->bitmap.a[pos] = INRANGE((vf->priv->bitmap.oa[pos]+imgalpha),0,255);
338 break;
339 default:
340 fprintf(stderr, "vf_bmovl: Internal error!\n");
341 exit( 10 );
342 }
343 if( command & IS_RAWIMG ) {
344 vf->priv->bitmap.y[pos] = rgb2y(red,green,blue);
345 vf->priv->bitmap.oa[pos] = alpha;
346 vf->priv->bitmap.a[pos] = INRANGE((alpha+imgalpha),0,255);
347 if((buf_y%2) && ((buf_x/pxsz)%2)) {
348 pos = ( ((buf_y+imgy)/2) * dmpi->stride[1] ) + (((buf_x/pxsz)+imgx)/2);
349 vf->priv->bitmap.u[pos] = rgb2u(red,green,blue);
350 vf->priv->bitmap.v[pos] = rgb2v(red,green,blue);
351 }
352 }
353 } // for buf_x
354 } // for buf_y
355 free (buffer);
356 } else if(errno) fprintf(stderr, "\nvf_bmovl: Error %d in fifo: %s\n\n", errno, strerror(errno));
357 }
358
359 if(vf->priv->hidden) return vf_next_put_image(vf, dmpi);
360
361 if(vf->priv->opaque) { // Just copy buffer memory to screen
362 for( ypos=vf->priv->y1 ; ypos < vf->priv->y2 ; ypos++ ) {
363 memcpy( dmpi->planes[0] + (ypos*dmpi->stride[0]) + vf->priv->x1,
364 vf->priv->bitmap.y + (ypos*vf->priv->w) + vf->priv->x1,
365 vf->priv->x2 - vf->priv->x1 );
366 if(ypos%2) {
367 memcpy( dmpi->planes[1] + ((ypos/2)*dmpi->stride[1]) + (vf->priv->x1/2),
368 vf->priv->bitmap.u + (((ypos/2)*(vf->priv->w)/2)) + (vf->priv->x1/2),
369 (vf->priv->x2 - vf->priv->x1)/2 );
370 memcpy( dmpi->planes[2] + ((ypos/2)*dmpi->stride[2]) + (vf->priv->x1/2),
371 vf->priv->bitmap.v + (((ypos/2)*(vf->priv->w)/2)) + (vf->priv->x1/2),
372 (vf->priv->x2 - vf->priv->x1)/2 );
373 }
374 }
375 } else { // Blit the bitmap to the videoscreen, pixel for pixel
376 for( ypos=vf->priv->y1 ; ypos < vf->priv->y2 ; ypos++ ) {
377 for ( xpos=vf->priv->x1 ; xpos < vf->priv->x2 ; xpos++ ) {
378 pos = (ypos * dmpi->stride[0]) + xpos;
379
380 alpha = vf->priv->bitmap.a[pos];
381
382 if (alpha == 0) continue; // Completly transparent pixel
383
384 if (alpha == 255) { // Opaque pixel
385 dmpi->planes[0][pos] = vf->priv->bitmap.y[pos];
386 if ((ypos%2) && (xpos%2)) {
387 pos = ( (ypos/2) * dmpi->stride[1] ) + (xpos/2);
388 dmpi->planes[1][pos] = vf->priv->bitmap.u[pos];
389 dmpi->planes[2][pos] = vf->priv->bitmap.v[pos];
390 }
391 } else { // Alphablended pixel
392 dmpi->planes[0][pos] = (dmpi->planes[0][pos]*(1.0-(alpha/255.0))) + (vf->priv->bitmap.y[pos]*(alpha/255.0));
393 if ((ypos%2) && (xpos%2)) {
394 pos = ( (ypos/2) * dmpi->stride[1] ) + (xpos/2);
395 dmpi->planes[1][pos] = (dmpi->planes[1][pos]*(1.0-(alpha/255.0))) + (vf->priv->bitmap.u[pos]*(alpha/255.0));
396 dmpi->planes[2][pos] = (dmpi->planes[2][pos]*(1.0-(alpha/255.0))) + (vf->priv->bitmap.v[pos]*(alpha/255.0));
397 }
398 }
399 } // for xpos
400 } // for ypos
401 } // if !opaque
402 return vf_next_put_image(vf, dmpi);
403 } // put_image
404
405 static int
406 vf_open(vf_instance_t* vf, char* args)
407 {
408 char filename[1000];
409
410 vf->config = config;
411 vf->put_image = put_image;
412 vf->query_format = query_format;
413 vf->uninit = uninit;
414
415 vf->priv = malloc(sizeof(struct vf_priv_s));
416
417 if( sscanf(args, "%d:%d:%s", &vf->priv->hidden, &vf->priv->opaque, filename) < 3 ) {
418 fprintf(stderr, "vf_bmovl: Bad arguments!\n");
419 fprintf(stderr, "vf_bmovl: Arguments are 'bool hidden:bool opaque:string fifo'\n");
420 exit(5);
421 }
422
423 vf->priv->stream_fd = open(filename, O_RDWR);
424 if(vf->priv->stream_fd >= 0) {
425 FD_ZERO( &vf->priv->stream_fdset );
426 printf("vf_bmovl: Opened fifo %s as FD %d\n", filename, vf->priv->stream_fd);
427 } else {
428 fprintf(stderr, "vf_bmovl: Error! Couldn't open FIFO %s: %s\n", filename, strerror(errno));
429 vf->priv->stream_fd = -1;
430 }
431
432 return TRUE;
433 }
434
435 vf_info_t vf_info_bmovl = {
436 "Read bitmaps from a FIFO and display them in window",
437 "bmovl",
438 "Per Wigren",
439 "",
440 vf_open
441 };