Merge svn changes up to r27514
[mplayer.git] / libvo / vo_bl.c
blobfa3f36d2e3bdd6439b9d1018b600736253c81ab1
1 /*
2 * vo_bl.c - playback using the Blinkenlights UPD protocol (and to files)
3 *
4 * UDP socket handling copied from bsender.c part of blib-0.6:
5 * http://sven.gimp.org/blinkenlights/
6 * Copyright (c) 2001-2001 The Blinkenlights Crew:
7 * Sven Neumann <sven@gimp.org>
8 * Michael Natterer <mitch@gimp.org>
9 * Daniel Mack <daniel@yoobay.net>
10 * (these portions are licensed under GNU GPL v2 or "(at your option)
11 * any later version")
13 * Other stuff: Copyright (C) Rik Snel 2002, License GNU GPL v2 or later
15 * patch from Stefan Schuermans <1stein@schuermans.info>:
16 * - correction of "maxval" in Blinkenlights UDP protcol
17 * - new scheme for new HDL
18 * - new scheme for grayscale in arbitrary size
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <fcntl.h>
26 #include <errno.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <sys/time.h>
30 #ifdef HAVE_SYS_MMAN_H
31 #include <sys/mman.h>
32 #endif
33 #include <sys/ioctl.h>
35 #include "config.h"
37 #include <netdb.h>
38 #include <sys/socket.h>
39 #include <netinet/in.h>
41 #include "video_out.h"
42 #include "video_out_internal.h"
43 #include "mp_msg.h"
44 #include "m_option.h"
45 #include "fastmemcpy.h"
47 static const vo_info_t info =
49 "Blinkenlights driver: http://www.blinkenlights.de",
50 "bl",
51 "Rik Snel <snel@phys.uu.nl>",
55 const LIBVO_EXTERN (bl)
57 /* General variables */
59 static unsigned char *image = NULL;
60 static unsigned char *tmp = NULL;
61 static int framenum;
62 static char *bl_subdevice = NULL;
63 static int prevpts = -1;
65 typedef struct {
66 char *name; /* filename */
67 FILE *fp;
68 int header_written; /* if header was written already */
69 } bl_file_t;
71 typedef struct {
72 char *name; /* hostname */
73 int port;
74 int fd; /* file descriptor */
75 } bl_host_t;
77 typedef struct {
78 char *name;
79 int img_format;
81 int channels;
82 int width;
83 int height;
84 int bpc; /* bits per component: bpc = 3, channels = 3 => bpp = 24*/
86 /* file output functions */
87 int (*init_file)(bl_file_t *file);
88 void (*write_frame)(bl_file_t *file, unsigned char *i, int duration);
89 void (*close_file)(bl_file_t *file);
91 /* network output functions */
92 int (*init_connection)(bl_host_t *host);
93 void (*send_frame)(bl_host_t *host);
94 void (*close_connection)(bl_host_t *host);
95 } bl_properties_t;
97 static bl_properties_t *bl = NULL;
99 /* arbitrary limit because I am too lazy to do proper memory management */
100 #define BL_MAX_FILES 16
101 #define BL_MAX_HOSTS 16
102 static bl_file_t bl_files[BL_MAX_FILES];
103 static bl_host_t bl_hosts[BL_MAX_HOSTS];
104 static int no_bl_files = 0;
105 static int no_bl_hosts = 0;
107 typedef struct {
108 uint32_t magic;
109 uint16_t height;
110 uint16_t width;
111 uint16_t channels;
112 uint16_t maxval;
113 unsigned char data[0];
114 } bl_packet_t;
116 static bl_packet_t *bl_packet = NULL;
117 static int bl_size;
119 /* bml output functions */
120 static int bml_init(bl_file_t *f) {
121 f->fp = fopen(f->name, "w");
122 if (!f->fp) {
123 mp_msg(MSGT_VO, MSGL_ERR, "bl: error opening %s\n", f->name);
124 return 1;
126 f->header_written = 0;
127 return 0;
130 static void bml_write_frame(bl_file_t *f, unsigned char *i, int duration) {
131 int j, k;
132 if( ! f->header_written )
134 fprintf(f->fp,
135 "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n"
136 "<blm width=\"%d\" height=\"%d\" bits=\"%d\" channels=\"%d\">\n"
137 " <header>\n"
138 " <title>Movie autogenerated by MPlayer</title>\n"
139 " <url>http://www.mplayerhq.hu</url>\n"
140 " </header>\n", bl->width, bl->height, bl->bpc, bl->channels);
141 f->header_written = 1;
143 fprintf(f->fp, " <frame duration=\"%d\">\n", duration);
144 for (j = 0; j < bl->height; j++) {
145 fprintf(f->fp, " <row>");
146 for (k = 0; k < bl->width; k++)
147 fprintf(f->fp, "%02x", *(i + j * bl->width + k));
148 fprintf(f->fp, "</row>\n");
150 fprintf(f->fp, " </frame>\n");
153 static void bml_close(bl_file_t *f) {
154 fprintf(f->fp, "</blm>\n");
155 fclose(f->fp);
158 /* Blinkenlights UDP protocol */
159 static int udp_init(bl_host_t *h) {
160 struct sockaddr_in addr;
161 struct hostent *dest;
163 dest = gethostbyname(h->name);
164 if (!dest) {
165 mp_msg(MSGT_VO, MSGL_ERR,
166 "unable to resolve host %s\n", h->name);
167 return 1;
170 h->fd = -1;
171 addr.sin_family = AF_INET;
172 addr.sin_port = htons(h->port);
174 memcpy(&addr.sin_addr.s_addr, dest->h_addr_list[0], dest->h_length);
176 h->fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
177 if (h->fd < 0) {
178 mp_msg(MSGT_VO, MSGL_ERR,
179 "couldn't create socket for %s\n", h->name);
180 return 1;
182 if (connect(h->fd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
183 mp_msg(MSGT_VO, MSGL_ERR, "couldn't connect socket for %s\n",
184 h->name);
185 close(h->fd);
186 return 1;
188 return 0;
191 static void udp_send(bl_host_t *h) {
192 if (send(h->fd, bl_packet, bl_size, 0) != bl_size)
193 mp_msg(MSGT_VO, MSGL_ERR, "unable to send to %s\n", h->name);
196 static void udp_close(bl_host_t *h) {
197 close(h->fd);
200 #define NO_BLS 3
202 static bl_properties_t bls[NO_BLS] = {
203 { "hdl", IMGFMT_YV12, 1, 18, 8, 8,
204 &bml_init, &bml_write_frame, &bml_close,
205 &udp_init, &udp_send, &udp_close },
206 { "arcade", IMGFMT_YV12, 1, 26, 20, 8,
207 &bml_init, &bml_write_frame, &bml_close,
208 &udp_init, &udp_send, &udp_close },
209 { "grayscale", IMGFMT_YV12, 1, -1, -1, 8, /* use width and height of movie */
210 &bml_init, &bml_write_frame, &bml_close,
211 &udp_init, &udp_send, &udp_close } };
213 static int config(uint32_t width, uint32_t height, uint32_t d_width,
214 uint32_t d_height, uint32_t flags, char *title, uint32_t format)
216 void * ptr;
218 /* adapt size of Blinkenlights UDP stream to size of movie */
219 if (bl->width < 0 || bl->height < 0) {
220 if (bl->width < 0) { /* use width of movie */
221 bl->width = width;
222 bl_packet->width = htons(bl->width);
224 if (bl->height < 0) { /* use height of movie */
225 bl->height = height;
226 bl_packet->height = htons(bl->height);
228 /* check for maximum size of UDP packet */
229 if (12 + bl->width*bl->height*bl->channels > 65507) {
230 mp_msg(MSGT_VO, MSGL_ERR, "bl: %dx%d-%d does not fit into an UDP packet\n",
231 bl->width, bl->height, bl->channels);
232 return 1;
234 /* resize frame and tmp buffers */
235 bl_size = 12 + bl->width*bl->height*bl->channels;
236 ptr = realloc(bl_packet, 12 + bl->width*bl->height*3); /* space for header and image data */
237 if (ptr)
238 bl_packet = (bl_packet_t*)ptr;
239 else {
240 mp_msg(MSGT_VO, MSGL_ERR, "bl: out of memory error\n");
241 return 1;
243 image = ((unsigned char*)bl_packet + 12); /* pointer to image data */
244 ptr = realloc(tmp, bl->width*bl->height*3); /* space for image data only */
245 if (ptr)
246 tmp = (unsigned char*)ptr;
247 else {
248 mp_msg(MSGT_VO, MSGL_ERR, "bl: out of memory error\n");
249 return 1;
253 framenum = 0;
254 if (format != IMGFMT_YV12) {
255 mp_msg(MSGT_VO, MSGL_ERR, "vo_bl called with wrong format");
256 return 1;
258 if (width > bl->width) {
259 mp_msg(MSGT_VO, MSGL_ERR, "bl: width of movie too large %d > %d\n", width, bl->width);
260 return 1;
262 if (height > bl->height) {
263 mp_msg(MSGT_VO, MSGL_ERR, "bl: height of movie too large %d > %d\n", height, bl->height);
264 return 1;
266 if (!image) {
267 mp_msg(MSGT_VO, MSGL_ERR, "bl: image should be initialized, internal error\n");
268 return 1;
270 memset(image, 0, bl->width*bl->height*3); /* blank the image */
271 mp_msg(MSGT_VO, MSGL_V, "vo_config bl called\n");
272 return 0;
275 static void draw_osd(void) {
278 static void flip_page (void) {
279 int i;
281 if (prevpts >= 0) for (i = 0; i < no_bl_files; i++)
282 bl->write_frame(&bl_files[i], tmp, (vo_pts - prevpts)/90);
283 fast_memcpy(tmp, image, bl->width*bl->height*bl->channels);
284 prevpts = vo_pts;
286 for (i = 0; i < no_bl_hosts; i++) bl->send_frame(&bl_hosts[i]);
289 framenum++;
290 return;
293 static int draw_frame(uint8_t * src[]) {
294 return 0;
297 static int query_format(uint32_t format) {
298 if (format == bl->img_format)
299 return VFCAP_CSP_SUPPORTED|VFCAP_CSP_SUPPORTED_BY_HW;
300 return 0;
303 static void uninit(void) {
304 int i;
305 mp_msg(MSGT_VO, MSGL_V, "bl: uninit called\n");
306 free(bl_packet);
307 bl_packet = NULL;
308 free(bl_subdevice);
309 bl_subdevice = NULL;
310 for (i = 0; i < no_bl_files; i++) bl->close_file(&bl_files[i]);
311 for (i = 0; i < no_bl_hosts; i++) bl->close_connection(&bl_hosts[i]);
312 no_bl_files = 0;
313 no_bl_hosts = 0;
314 bl = NULL;
317 static void check_events(void) {
320 static int draw_slice(uint8_t *srcimg[], int stride[],
321 int wf, int hf, int xf, int yf) {
322 int i, w, h, x, y;
323 uint8_t *dst;
324 uint8_t *src=srcimg[0];
325 w = wf; h = hf; x = xf; y = yf;
326 dst=image; /* + zr->off_y + zr->image_width*(y/zr->vdec)+x;*/
327 // copy Y:
328 for (i = 0; i < h; i++) {
329 fast_memcpy(dst,src,w);
330 dst+=bl->width;
331 src+=stride[0];
334 return 0;
337 static int preinit(const char *arg) {
338 char *p, *q;
339 int end = 0, i;
340 char txt[256];
341 if (!arg || strlen(arg) == 0) {
342 mp_msg(MSGT_VO, MSGL_ERR, "bl: subdevice must be given, example: -vo bl:arcade:host=localhost:2323\n");
343 return 1;
346 bl_subdevice = malloc(strlen(arg) + 1);
347 if (!bl_subdevice) {
348 mp_msg(MSGT_VO, MSGL_ERR, "bl: out of memory error\n");
349 return 1;
351 p = bl_subdevice;
352 strcpy(p, arg);
353 mp_msg(MSGT_VO, MSGL_V, "bl: preinit called with %s\n", arg);
354 for (i = 0; i < NO_BLS; i++) {
355 if (!strncmp(p, bls[i].name, strlen(bls[i].name)))
356 break;
358 if (i >= NO_BLS) {
359 txt[0] = 0;
360 for (i = 0; i < NO_BLS; i++)
361 if (strlen( txt ) + 4 + strlen( bls[i].name ) + 1 < sizeof(txt))
362 sprintf( txt + strlen( txt ), "%s%s",
363 txt[0] == 0 ? "" : i == NO_BLS - 1 ? " or " : ", ", bls[i].name );
364 mp_msg(MSGT_VO, MSGL_ERR, "bl: subdevice must start with %s\nbl: i.e. -vo bl:arcade:host=localhost:2323\n", txt);
365 return 1;
367 bl = &bls[i];
368 p += strlen(bls[i].name);
369 if (*p == '\0') {
370 no_bl_hosts = 1;
371 bl_hosts[0].name = "localhost";
372 bl_hosts[0].port = 2323;
373 mp_msg(MSGT_VO, MSGL_V, "bl: no hosts/files specified, using localhost:2323\n");
374 end = 1;
375 } else if (*p != ':') {
376 mp_msg(MSGT_VO, MSGL_ERR, "bl: syntax error in subdevice\n");
377 return 1;
379 p++;
381 while (!end) {
382 q = p + 5;
383 if (!strncmp(p, "file=", 5)) {
384 if (no_bl_files == BL_MAX_FILES) {
385 mp_msg(MSGT_VO, MSGL_ERR, "bl: maximum number of files reached (%d)\n", BL_MAX_FILES);
386 return 1;
388 p += 5;
389 while (*q != ',' && *q != '\0') q++;
390 if (*q == '\0') end = 1;
391 *q = '\0';
392 bl_files[no_bl_files].name = p;
393 mp_msg(MSGT_VO, MSGL_V, "blfile[%d]: %s\n",
394 no_bl_files, p);
395 no_bl_files++;
396 } else if (!strncmp(p, "host=", 5)) {
397 if (no_bl_hosts == BL_MAX_HOSTS) {
398 mp_msg(MSGT_VO, MSGL_ERR, "bl: maximum number of hosts reached (%d)\n", BL_MAX_HOSTS);
399 return 1;
401 p += 5;
402 while (*q != ',' && *q != '\0' && *q != ':') q++;
403 if (*q == ':') {
404 *q++ = '\0';
405 bl_hosts[no_bl_hosts].name = p;
406 bl_hosts[no_bl_hosts].port = atoi(q);
407 while (*q != ',' && *q != '\0') q++;
408 if (*q == '\0') end = 1;
409 } else {
410 /* use default port */
411 if (*q == '\0') end = 1;
412 *q = '\0';
413 bl_hosts[no_bl_hosts].name = p;
414 bl_hosts[no_bl_hosts].port = 2323;
416 mp_msg(MSGT_VO, MSGL_V,
417 "blhost[%d]: %s:%d\n",
418 no_bl_hosts, p,
419 bl_hosts[no_bl_hosts].port);
420 no_bl_hosts++;
421 } else {
422 mp_msg(MSGT_VO, MSGL_ERR, "bl: syntax error in entry %d in subdevice %s, should be a comma separated\nlist of host=name:port and file=foo.bml\n", no_bl_hosts + no_bl_files, arg);
423 return 1;
425 p = ++q;
428 if (bl->width >= 0 && bl->height >= 0) { /* size already known */
429 bl_size = 12 + bl->width*bl->height*bl->channels;
430 bl_packet = malloc(12 + bl->width*bl->height*3); /* space for header and image data */
431 image = ((unsigned char*)bl_packet + 12); /* pointer to image data */
432 tmp = malloc(bl->width*bl->height*3); /* space for image data only */
434 else { /* size unknown yet */
435 bl_size = 12;
436 bl_packet = malloc(12 + 3); /* space for header and a pixel */
437 image = ((unsigned char*)bl_packet + 12); /* pointer to image data */
438 tmp = malloc(3); /* space for a pixel only */
441 if (!bl_packet || !tmp) {
442 mp_msg(MSGT_VO, MSGL_ERR, "bl: out of memory error\n");
443 return 1;
445 bl_packet->magic = htonl(0x23542666);
446 bl_packet->width = htons(bl->width);
447 bl_packet->height = htons(bl->height);
448 bl_packet->channels = htons(bl->channels);
449 bl_packet->maxval = htons((1 << bl->bpc) - 1);
451 /* open all files */
452 for (i = 0; i < no_bl_files; i++)
453 if (bl->init_file(&bl_files[i])) return 1;
455 /* open all sockets */
456 for (i = 0; i < no_bl_hosts; i++)
457 if (bl->init_connection(&bl_hosts[i])) return 1;
460 return 0;
463 static int control(uint32_t request, void *data) {
464 switch (request) {
465 case VOCTRL_QUERY_FORMAT:
466 return query_format(*((uint32_t*)data));
468 return VO_NOTIMPL;