vo_glamo: sub.h was moved to sub directory in c9026cb3210205b07e2e068467a18ee40f9259a3
[mplayer/glamo.git] / libvo / vo_zr.c
blob4cb1ed0de6e4738cf47b2dbbdebf196bce88af69
1 /*
2 * playback on Zoran cards
3 * copyright (C) 2001, 2003 Rik Snel
5 * This file is part of MPlayer.
7 * MPlayer is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * MPlayer is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 /* $Id$ */
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <unistd.h>
28 #include <fcntl.h>
29 #include <errno.h>
30 #include <sys/stat.h>
31 #include <sys/types.h>
32 #include <sys/time.h>
33 #include <sys/mman.h>
34 #include <sys/ioctl.h>
35 #include <linux/types.h>
36 #include <linux/videodev.h>
37 #include "config.h"
38 #include "videodev_mjpeg.h"
39 #include "video_out.h"
40 #include "video_out_internal.h"
41 #include "mp_msg.h"
42 #include "m_option.h"
43 #include "fastmemcpy.h"
44 #include "jpeg_enc.h"
45 #include "vo_zr.h"
47 static const vo_info_t info =
49 "Zoran ZR360[56]7/ZR36060 Driver (DC10(+)/buz/lml33/MatroxRR)",
50 "zr",
51 "Rik Snel <rsnel@cube.dyndns.org>",
55 const LIBVO_EXTERN (zr)
57 #define ZR_MAX_DEVICES 4
58 /* General variables */
60 typedef struct {
61 int width;
62 int height;
63 int xoff;
64 int yoff;
65 int set;
66 } geo_t;
68 static int zr_count = 1;
69 static int zr_parsing = 0;
70 static int framenum;
72 typedef struct {
73 /* commandline args given for this device (and defaults) */
74 int vdec, hdec; /* requested decimation 1,2,4 */
75 int fd; /* force decimation */
76 int xdoff, ydoff; /* offset from upperleft of screen
77 * default is 'centered' */
78 int quality; /* jpeg quality 1=best, 20=bad */
79 geo_t g; /* view window (zrcrop) */
80 char *device; /* /dev/video1 */
81 int bw; /* if bw == 1, display in black&white */
82 int norm; /* PAL/NTSC */
84 /* buffers + pointers + info */
86 unsigned char *image;
87 int image_width, image_height, size;
88 int off_y, off_c, stride; /* for use by 'draw slice/frame' */
90 unsigned char *buf; /* the jpeg images will be placed here */
91 jpeg_enc_t *j;
92 unsigned char *y_data, *u_data, *v_data; /* used by the jpeg encoder */
93 int y_stride, u_stride, v_stride; /* these point somewhere in image */
95 /* information for (and about) the zoran card */
97 int vdes; /* file descriptor of card */
98 int frame, synco, queue; /* buffer management */
99 struct mjpeg_sync zs; /* state information */
100 struct mjpeg_params p;
101 struct mjpeg_requestbuffers zrq;
102 struct video_capability vc; /* max resolution and so on */
103 int fields, stretchy; /* must the *image be interlaced
104 or stretched to fit on the screen? */
105 } zr_info_t;
107 static zr_info_t zr_info[ZR_MAX_DEVICES] = {
108 {1, 1, 1, -1, -1, 2, {0, 0, 0, 0, 0}, NULL, 0, VIDEO_MODE_AUTO, NULL, 0, 0, 0, 0, 0,
109 0, NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
110 {1, 1, 1, -1, -1, 2, {0, 0, 0, 0, 0}, NULL, 0, VIDEO_MODE_AUTO, NULL, 0, 0, 0, 0, 0,
111 0, NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
112 {1, 1, 1, -1, -1, 2, {0, 0, 0, 0, 0}, NULL, 0, VIDEO_MODE_AUTO, NULL, 0, 0, 0, 0, 0,
113 0, NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
114 {1, 1, 1, -1, -1, 2, {0, 0, 0, 0, 0}, NULL, 0, VIDEO_MODE_AUTO, NULL, 0, 0, 0, 0, 0,
115 0, NULL, NULL, NULL, NULL, NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}};
120 #define MJPEG_NBUFFERS 2
121 #define MJPEG_SIZE 1024*256
124 int zoran_getcap(zr_info_t *zr) {
125 char* dev = NULL;
127 if (zr->device)
128 dev = zr->device;
129 else {
130 struct stat vstat;
131 const char *devs[] = {
132 "/dev/video",
133 "/dev/video0",
134 "/dev/v4l/video0",
135 "/dev/v4l0",
136 "/dev/v4l",
137 NULL
139 int i = 0;
143 if ((stat(devs[i], &vstat) == 0) && S_ISCHR(vstat.st_mode))
145 dev = devs[i];
146 mp_msg(MSGT_VO, MSGL_V, "zr: found video device %s\n", dev);
147 break;
149 } while (devs[++i] != NULL);
151 if (!dev)
153 mp_msg(MSGT_VO, MSGL_ERR, "zr: unable to find video device\n");
154 return 1;
158 zr->vdes = open(dev, O_RDWR);
160 if (zr->vdes < 0) {
161 mp_msg(MSGT_VO, MSGL_ERR, "zr: error opening %s: %s\n",
162 dev, strerror(errno));
163 return 1;
166 /* before we can ask for the maximum resolution, we must set
167 * the correct tv norm */
169 if (ioctl(zr->vdes, MJPIOC_G_PARAMS, &zr->p) < 0) {
170 mp_msg(MSGT_VO, MSGL_ERR, "zr: device at %s is probably not a DC10(+)/buz/lml33\n", dev);
171 return 1;
174 if (zr->p.norm != zr->norm && zr->norm != VIDEO_MODE_AUTO) {
175 /* attempt to set requested norm */
176 zr->p.norm = zr->norm;
177 if (ioctl(zr->vdes, MJPIOC_S_PARAMS, &zr->p) < 0) {
178 mp_msg(MSGT_VO, MSGL_ERR,
179 "zr: unable to change video norm, use another program to change it (XawTV)\n");
180 return 1;
182 ioctl(zr->vdes, MJPIOC_G_PARAMS, &zr->p);
183 if (zr->norm != zr->p.norm) {
184 mp_msg(MSGT_VO, MSGL_ERR,
185 "zr: unable to change video norm, use another program to change it (XawTV)\n");
186 return 1;
190 if (ioctl(zr->vdes, VIDIOCGCAP, &zr->vc) < 0) {
191 mp_msg(MSGT_VO, MSGL_ERR, "zr: error getting video capabilities from %s\n", dev);
192 return 1;
194 mp_msg(MSGT_VO, MSGL_V, "zr: MJPEG card reports maxwidth=%d, maxheight=%d\n", zr->vc.maxwidth, zr->vc.maxheight);
196 return 0;
199 int init_zoran(zr_info_t *zr, int stretchx, int stretchy) {
200 /* center the image, and stretch it as far as possible (try to keep
201 * aspect) and check if it fits */
202 if (zr->image_width > zr->vc.maxwidth) {
203 mp_msg(MSGT_VO, MSGL_ERR, "zr: movie to be played is too wide, max width currently %d\n", zr->vc.maxwidth);
204 return 1;
207 if (zr->image_height > zr->vc.maxheight) {
208 mp_msg(MSGT_VO, MSGL_ERR, "zr: movie to be played is too high, max height currently %d\n", zr->vc.maxheight);
209 return 1;
212 zr->p.decimation = 0;
213 zr->p.HorDcm = stretchx;
214 zr->p.VerDcm = stretchy;
215 zr->p.TmpDcm = 1;
216 zr->p.field_per_buff = zr->fields;
217 if (zr->xdoff == -1) {
218 zr->p.img_x = (zr->vc.maxwidth -
219 zr->p.HorDcm*(int)zr->image_width/zr->hdec)/2;
220 } else {
221 zr->p.img_x = zr->xdoff;
223 if (zr->ydoff == -1) {
224 zr->p.img_y = (zr->vc.maxheight - zr->p.VerDcm*
225 (3-zr->fields)*(int)zr->image_height)/4;
226 } else {
227 zr->p.img_y = zr->ydoff;
229 zr->p.img_width = zr->p.HorDcm*zr->image_width/zr->hdec;
230 zr->p.img_height = zr->p.VerDcm*zr->image_height/zr->fields;
231 mp_msg(MSGT_VO, MSGL_V, "zr: geometry (after 'scaling'): %dx%d+%d+%d fields=%d, w=%d, h=%d\n", zr->p.img_width, (3-zr->fields)*zr->p.img_height, zr->p.img_x, zr->p.img_y, zr->fields, zr->image_width/zr->hdec, zr->image_height);
233 if (ioctl(zr->vdes, MJPIOC_S_PARAMS, &zr->p) < 0) {
234 mp_msg(MSGT_VO, MSGL_ERR, "zr: error setting display parameters\n");
235 return 1;
238 zr->zrq.count = MJPEG_NBUFFERS;
239 zr->zrq.size = MJPEG_SIZE;
241 if (ioctl(zr->vdes, MJPIOC_REQBUFS, &zr->zrq)) {
242 mp_msg(MSGT_VO, MSGL_ERR, "zr: error requesting %ld buffers of size %ld\n", zr->zrq.count, zr->zrq.size);
243 return 1;
246 /* the buffer count allocated may be different to the request */
247 zr->buf = (unsigned char*)mmap(0, zr->zrq.count*zr->zrq.size,
248 PROT_READ|PROT_WRITE, MAP_SHARED, zr->vdes, 0);
250 if (zr->buf == MAP_FAILED) {
251 mp_msg(MSGT_VO, MSGL_ERR, "zr: error requesting %ld buffers of size %ld\n", zr->zrq.count, zr->zrq.size);
252 return 1;
255 mp_msg(MSGT_VO, MSGL_V, "zr: got %ld buffers of size %ld (wanted %d buffers of size %d)\n", zr->zrq.count, zr->zrq.size, MJPEG_NBUFFERS, MJPEG_SIZE);
256 if (zr->zrq.count < MJPEG_NBUFFERS) {
257 mp_msg(MSGT_VO, MSGL_V, "zr: got not enough buffers\n");
258 return 1;
261 zr->queue = 0;
262 zr->synco = 0;
264 return 0;
267 void uninit_zoran(zr_info_t *zr) {
268 free(zr->image);
269 zr->image=NULL;
270 while (zr->queue > zr->synco + 1) {
271 if (ioctl(zr->vdes, MJPIOC_SYNC, &zr->zs) < 0)
272 mp_msg(MSGT_VO, MSGL_ERR, "zr: error waiting for buffers to become free\n");
273 zr->synco++;
275 /* stop streaming */
276 zr->frame = -1;
277 if (ioctl(zr->vdes, MJPIOC_QBUF_PLAY, &zr->frame) < 0)
278 mp_msg(MSGT_VO, MSGL_ERR, "zr: error stopping playback of last frame\n");
279 if (munmap(zr->buf,zr->zrq.count*zr->zrq.size))
280 mp_msg(MSGT_VO, MSGL_ERR, "zr: error unmapping buffer\n");
281 close(zr->vdes);
284 int zr_geometry_sane(geo_t *g, unsigned int width, unsigned int height) {
285 if (g->set) {
286 if (g->width%2 != 0 || g->height%2 != 0 ||
287 g->xoff%2 != 0 || g->yoff%2 != 0) {
288 mp_msg(MSGT_VO, MSGL_ERR, "zr: arguments in -zrcrop must be multiples of 2\n");
289 return 1;
291 if (g->width <= 0 || g->height <= 0 ||
292 g->xoff < 0 || g->yoff < 0) {
293 mp_msg(MSGT_VO, MSGL_ERR, "zr: width and height must be positive and offset nonnegative\n");
294 return 1;
296 if (g->width + g->xoff > width) {
297 mp_msg(MSGT_VO, MSGL_ERR, "zr: width+xoffset (%d+%d>%d) is too big\n", g->width, g->xoff, width);
298 return 1;
300 if (g->height + g->yoff > height) {
301 mp_msg(MSGT_VO, MSGL_ERR, "zr: height+yoffset (%d+%d>%d) is too big\n", g->height, g->yoff, height);
302 return 1;
304 } else {
305 g->width = width;
306 g->height = height;
307 g->xoff = 0;
308 g->yoff = 0;
309 g->set = 1;
311 return 0;
315 static int config(uint32_t width, uint32_t height, uint32_t d_width,
316 uint32_t d_height, uint32_t flags, char *title, uint32_t format)
318 int i, tmp, stretchx, stretchy;
319 framenum = 0;
320 if (format != IMGFMT_YV12 && format != IMGFMT_YUY2) {
321 printf("vo_zr called with wrong format");
322 return 1;
324 for (i = 0; i < zr_count; i++) {
325 zr_info_t *zr = &zr_info[i];
326 geo_t *g = &zr->g;
328 zr->stride = 2*width;
329 if (zr_geometry_sane(g, width, height)) return 1;
331 /* we must know the maximum resolution of the device
332 * it differs for DC10+ and buz for example */
333 zoran_getcap(zr); /*must be called before init_zoran */
334 /* make the scaling decision
335 * we are capable of stretching the image in the horizontal
336 * direction by factors 1, 2 and 4
337 * we can stretch the image in the vertical direction by a
338 * factor of 1 and 2 AND we must decide about interlacing */
339 if (g->width > zr->vc.maxwidth/2 ||
340 g->height > zr->vc.maxheight/2) {
341 stretchx = 1;
342 stretchy = 1;
343 zr->fields = 2;
344 if (zr->vdec == 2) {
345 zr->fields = 1;
346 } else if (zr->vdec == 4) {
347 zr->fields = 1;
348 stretchy = 2;
350 stretchx = zr->hdec;
351 } else if (g->width > zr->vc.maxwidth/4 ||
352 g->height > zr->vc.maxheight/4) {
353 stretchx = 2;
354 stretchy = 1;
355 zr->fields = 1;
356 if (zr->vdec == 2) {
357 stretchy = 2;
358 } else if (zr->vdec == 4) {
359 if (!zr->fd) {
360 mp_msg(MSGT_VO, MSGL_WARN, "zr: vertical decimation too high, changing to 2 (use -zrfd to keep vdec=4)\n");
361 zr->vdec = 2;
363 stretchy = 2;
365 if (zr->hdec == 2) {
366 stretchx = 4;
367 } else if (zr->hdec == 4){
368 if (!zr->fd) {
369 mp_msg(MSGT_VO, MSGL_WARN, "zr: horizontal decimation too high, changing to 2 (use -zrfd to keep hdec=4)\n");
370 zr->hdec = 2;
372 stretchx = 4;
374 } else {
375 /* output image is maximally stretched */
376 stretchx = 4;
377 stretchy = 2;
378 zr->fields = 1;
379 if (zr->vdec != 1 && !zr->fd) {
380 mp_msg(MSGT_VO, MSGL_WARN, "zr: vertical decimation too high, changing to 1 (use -zrfd to keep vdec=%d)\n", zr->vdec);
381 zr->vdec = 1;
383 if (zr->hdec != 1 && !zr->fd) {
384 mp_msg(MSGT_VO, MSGL_WARN, "zr: vertical decimation too high, changing to 1 (use -zrfd to keep hdec=%d)\n", zr->hdec);
385 zr->hdec = 1;
388 /* It can be that the original frame was too big for display,
389 * or that the width of the decimated image (for example) after
390 * padding up to a multiple of 16 has become too big. (orig
391 * width 720 (exactly right for the Buz) after decimation 360,
392 * after padding up to a multiple of 16 368, display 736 -> too
393 * large). In these situations we auto(re)crop. */
394 tmp = 16*((g->width - 1)/(zr->hdec*16) + 1);
395 if (stretchx*tmp > zr->vc.maxwidth) {
396 g->xoff += 2*((g->width - zr->hdec*(tmp-16))/4);
397 /* g->off must be a multiple of 2 */
398 g->width = zr->hdec*(tmp - 16);
399 g->set = 0; /* we abuse this field to
400 report that g has changed*/
402 tmp = 8*zr->fields*((g->height - 1)/(zr->vdec*zr->fields*8)+1);
403 if (stretchy*tmp > zr->vc.maxheight) {
404 g->yoff += 2*((g->height - zr->vdec*
405 (tmp - 8*zr->fields))/4);
406 g->height = zr->vdec*(tmp - 8*zr->fields);
407 g->set = 0;
409 if (!g->set)
410 mp_msg(MSGT_VO, MSGL_V, "zr: auto(re)cropping %dx%d+%d+%d to make the image fit on the screen\n", g->width, g->height, g->xoff, g->yoff);
412 /* the height must be a multiple of fields*8 and the width
413 * must be a multiple of 16 */
414 /* add some black borders to make it so, and center the image*/
415 zr->image_height = zr->fields*8*((g->height/zr->vdec - 1)/
416 (zr->fields*8) + 1);
417 zr->image_width = (zr->hdec*16)*((g->width - 1)/(zr->hdec*16) + 1);
418 zr->off_y = (zr->image_height - g->height/zr->vdec)/2;
419 if (zr->off_y%2 != 0) zr->off_y++;
420 zr->off_y *= zr->image_width;
421 zr->off_c = zr->off_y/4;
422 zr->off_y += (zr->image_width - g->width)/2;
423 if (zr->off_y%2 != 0) zr->off_y--;
424 zr->off_c += (zr->image_width - g->width)/4;
425 zr->size = zr->image_width*zr->image_height;
426 mp_msg(MSGT_VO, MSGL_V, "zr: input: %dx%d, cropped: %dx%d, output: %dx%d, off_y=%d, off_c=%d\n", width, height, g->width, g->height, zr->image_width, zr->image_height, zr->off_y, zr->off_c);
428 zr->image = malloc(2*zr->size); /* this buffer allows for YUV422 data,
429 * so it is a bit too big for YUV420 */
430 if (!zr->image) {
431 mp_msg(MSGT_VO, MSGL_ERR, "zr: Memory exhausted\n");
432 return 1;
434 /* and make sure that the borders are _really_ black */
435 switch (format) {
436 case IMGFMT_YV12:
437 memset(zr->image, 0, zr->size);
438 memset(zr->image + zr->size, 0x80, zr->size/4);
439 memset(zr->image + 3*zr->size/2, 0x80, zr->size/4);
440 zr->y_data = zr->image;
441 zr->u_data = zr->image + zr->size;
442 zr->v_data = zr->image + 3*zr->size/2;
444 zr->y_stride = zr->image_width;
445 zr->u_stride = zr->image_width/2;
446 zr->v_stride = zr->image_width/2;
448 zr->j = jpeg_enc_init(zr->image_width/zr->hdec,
449 zr->image_height/zr->fields,
450 zr->hdec, zr->y_stride*zr->fields,
451 zr->hdec, zr->u_stride*zr->fields,
452 zr->hdec, zr->v_stride*zr->fields,
453 1, zr->quality, zr->bw);
454 break;
455 case IMGFMT_YUY2:
456 for (tmp = 0; tmp < 2*zr->size; tmp+=4) {
457 zr->image[tmp] = 0;
458 zr->image[tmp+1] = 0x80;
459 zr->image[tmp+2] = 0;
460 zr->image[tmp+3] = 0x80;
463 zr->y_data = zr->image;
464 zr->u_data = zr->image + 1;
465 zr->v_data = zr->image + 3;
467 zr->y_stride = 2*zr->image_width;
468 zr->u_stride = 2*zr->image_width;
469 zr->v_stride = 2*zr->image_width;
471 zr->j = jpeg_enc_init(zr->image_width/zr->hdec,
472 zr->image_height/zr->fields,
473 zr->hdec*2,
474 zr->y_stride*zr->fields,
475 zr->hdec*4,
476 zr->u_stride*zr->fields,
477 zr->hdec*4,
478 zr->v_stride*zr->fields,
479 0, zr->quality, zr->bw);
480 break;
481 default:
482 mp_msg(MSGT_VO, MSGL_FATAL, "zr: internal inconsistency in vo_zr\n");
486 if (zr->j == NULL) {
487 mp_msg(MSGT_VO, MSGL_ERR, "zr: error initializing the jpeg encoder\n");
488 return 1;
491 if (init_zoran(zr, stretchx, stretchy)) {
492 return 1;
496 return 0;
499 static void draw_osd(void) {
502 static void flip_page (void) {
503 int i, j, k;
504 //FILE *fp;
505 //char filename[100];
506 /* do we have a free buffer? */
507 for (j = 0; j < zr_count; j++) {
508 zr_info_t *zr = &zr_info[j];
509 /* using MJPEG_NBUFFERS here, using the real number of
510 * buffers may give sync issues (real number of buffers
511 * is always sufficient) */
512 if (zr->queue-zr->synco < MJPEG_NBUFFERS) {
513 zr->frame = zr->queue;
514 } else {
515 if (ioctl(zr->vdes, MJPIOC_SYNC, &zr->zs) < 0)
516 mp_msg(MSGT_VO, MSGL_ERR, "zr: error waiting for buffers to become free\n");
517 zr->frame = zr->zs.frame;
518 zr->synco++;
520 k=0;
521 for (i = 0; i < zr->fields; i++)
522 k+=jpeg_enc_frame(zr->j, zr->y_data + i*zr->y_stride,
523 zr->u_data + i*zr->u_stride,
524 zr->v_data + i*zr->v_stride,
525 zr->buf + zr->frame*zr->zrq.size+k);
526 if (k > zr->zrq.size) mp_msg(MSGT_VO, MSGL_WARN, "zr: jpeg image too large for maximum buffer size. Lower the jpeg encoding\nquality or the resolution of the movie.\n");
528 /* Warning: Only the first jpeg image contains huffman- and
529 * quantisation tables, so don't expect files other than
530 * test0001.jpg to be readable */
531 /*sprintf(filename, "test%04d.jpg", framenum);
532 fp = fopen(filename, "w");
533 if (!fp) exit(1);
534 fwrite(buf+frame*zrq.size, 1, k, fp);
535 fclose(fp);*/
536 /*fp = fopen("test1.jpg", "r");
537 fread(buf+frame*zrq.size, 1, 2126, fp);
538 fclose(fp);*/
540 for (j = 0; j < zr_count; j++) {
541 zr_info_t *zr = &zr_info[j];
542 if (ioctl(zr->vdes, MJPIOC_QBUF_PLAY, &zr->frame) < 0)
543 mp_msg(MSGT_VO, MSGL_ERR, "zr: error queueing buffer for playback\n");
544 zr->queue++;
547 framenum++;
548 return;
551 static int draw_frame(uint8_t * src[]) {
552 int i, j;
553 char *source, *dest;
554 //printf("draw frame called\n");
555 for (j = 0; j < zr_count; j++) {
556 zr_info_t *zr = &zr_info[j];
557 geo_t *g = &zr->g;
558 source = src[0] + 2*g->yoff*zr->vdec*zr->stride + 2*g->xoff;
559 dest = zr->image + 2*zr->off_y;
560 for (i = 0; i < g->height/zr->vdec; i++) {
561 fast_memcpy(dest, source, zr->image_width*2);
562 dest += 2*zr->image_width;
563 source += zr->vdec*zr->stride;
566 return 0;
569 static int query_format(uint32_t format) {
570 if(format==IMGFMT_YV12 || format==IMGFMT_YUY2)
571 return VFCAP_CSP_SUPPORTED|VFCAP_CSP_SUPPORTED_BY_HW;
572 return 0;
575 static void uninit(void) {
576 int j;
577 mp_msg(MSGT_VO, MSGL_V, "zr: uninit called\n");
578 for (j = 0; j < zr_count; j++) {
579 jpeg_enc_uninit(zr_info[j].j);
580 uninit_zoran(&zr_info[j]);
584 static void check_events(void) {
588 static int draw_slice(uint8_t *srcimg[], int stride[],
589 int wf, int hf, int xf, int yf) {
590 int i, j, w, h, x, y;
591 /* Apply 'geometry', crop unwanted parts */
592 uint8_t *dst;
593 //printf("before: w=%d, h=%d, x=%d, y=%d, src0=%p, src1=%p, src2=%p\n", w, h, x, y, srcimg[0], srcimg[1], srcimg[2]);
594 for (j = 0; j < zr_count; j++) {
595 uint8_t *src=srcimg[0];
596 uint8_t *src1=srcimg[1];
597 uint8_t *src2=srcimg[2];
598 zr_info_t *zr = &zr_info[j];
599 geo_t *g = &zr->g;
600 w = wf; h = hf; x = xf; y = yf;
601 if (x < g->xoff) {
602 src += g->xoff - x;
603 src1 += (g->xoff - x)/2;
604 src2 += (g->xoff - x)/2;
605 w -= g->xoff - x;
606 if (w < 0) break; //return 0;
607 x = 0 /*g.xoff*/;
608 } else {
609 x -= g->xoff;
611 if (x + w > g->width) {
612 w = g->width - x;
613 if (w < 0) break; //return 0;
615 if (y < g->yoff) {
616 src += (g->yoff - y)*stride[0];
617 src1 += ((g->yoff - y)/2)*stride[1];
618 src2 += ((g->yoff - y)/2)*stride[2];
619 h -= g->yoff - y;
620 if (h < 0) break; //return 0;
621 y = 0;
622 } else {
623 y -= g->yoff;
625 if (y + h > g->height) {
626 h = g->height - y;
627 if (h < 0) break; //return 0;
629 //printf("after: w=%d, h=%d, x=%d, y=%d, src0=%p, src1=%p, src2=%p\n", w, h, x, y, srcimg[0], srcimg[1], srcimg[2]);
630 dst=zr->image + zr->off_y + zr->image_width*(y/zr->vdec)+x;
631 // copy Y:
632 for (i = 0; i < h; i++) {
633 if ((i + x)%zr->vdec == 0) {
634 fast_memcpy(dst,src,w);
635 dst+=zr->image_width;
637 src+=stride[0];
640 if (!zr->bw) {
641 // copy U+V:
642 uint8_t *dst1=zr->image + zr->size + zr->off_c+ (y/(zr->vdec*2))*zr->image_width/2+(x/2);
643 uint8_t *dst2=zr->image + 3*zr->size/2 + zr->off_c +
644 (y/(zr->vdec*2))*
645 zr->image_width/2+(x/2);
646 for (i = 0; i< h/2; i++) {
647 if ((i+x/2)%zr->vdec == 0) {
648 fast_memcpy(dst1,src1,w/2);
649 fast_memcpy(dst2,src2,w/2);
650 dst1+=zr->image_width/2;
651 dst2+=zr->image_width/2;
653 src1+=stride[1];
654 src2+=stride[2];
658 return 0;
662 /* copied and adapted from vo_aa_parseoption */
664 vo_zr_parseoption(const m_option_t* conf, const char *opt, const char *param){
665 /* got an option starting with zr */
666 zr_info_t *zr = &zr_info[zr_parsing];
667 int i;
668 /* do WE need it ?, always */
669 if (!strcasecmp(opt, "zrdev")) {
670 if (param == NULL) return ERR_MISSING_PARAM;
671 //if ((i=getcolor(param))==-1) return ERR_OUT_OF_RANGE;
672 //aaopt_osdcolor=i;
673 free(zr->device);
674 zr->device = malloc(strlen(param)+1);
675 strcpy(zr->device, param);
676 mp_msg(MSGT_VO, MSGL_V, "zr: using device %s\n", zr->device);
677 return 1;
678 } else if (!strcasecmp(opt, "zrbw")) {
679 if (param != NULL) {
680 return ERR_OUT_OF_RANGE;
682 zr->bw = 1;
683 return 1;
684 } else if (!strcasecmp(opt, "zrfd")) {
685 if (param != NULL) {
686 return ERR_OUT_OF_RANGE;
688 zr->fd = 1;
689 return 1;
690 } else if (!strcasecmp(opt, "zrcrop")){
691 geo_t *g = &zr->g;
692 if (g->set == 1) {
693 zr_parsing++;
694 zr_count++;
695 zr = &zr_info[zr_parsing];
696 g = &zr->g;
697 if (zr_count > 4) {
698 mp_msg(MSGT_VO, MSGL_ERR, "zr: too many simultaneus display devices requested (max. is 4)\n");
699 return ERR_OUT_OF_RANGE;
702 if (param == NULL) return ERR_MISSING_PARAM;
703 if (sscanf(param, "%dx%d+%d+%d", &g->width, &g->height,
704 &g->xoff, &g->yoff) != 4) {
705 g->xoff = 0; g->yoff = 0;
706 if (sscanf(param, "%dx%d", &g->width, &g->height) != 2) {
707 mp_msg(MSGT_VO, MSGL_ERR, "zr: argument to -zrcrop must be of the form 352x288+16+0\n");
708 return ERR_OUT_OF_RANGE;
711 g->set = 1;
712 mp_msg(MSGT_VO, MSGL_V, "zr: cropping %s\n", param);
713 return 1;
714 }else if (!strcasecmp(opt, "zrhdec")) {
715 i = atoi(param);
716 if (i != 1 && i != 2 && i != 4) return ERR_OUT_OF_RANGE;
717 zr->hdec = i;
718 return 1;
719 }else if (!strcasecmp(opt, "zrvdec")) {
720 i = atoi(param);
721 if (i != 1 && i != 2 && i != 4) return ERR_OUT_OF_RANGE;
722 zr->vdec = i;
723 return 1;
724 }else if (!strcasecmp(opt, "zrxdoff")) {
725 i = atoi(param);
726 zr->xdoff = i;
727 return 1;
728 }else if (!strcasecmp(opt, "zrydoff")) {
729 i = atoi(param);
730 zr->ydoff = i;
731 return 1;
732 }else if (!strcasecmp(opt, "zrquality")) {
733 i = atoi(param);
734 if (i < 1 || i > 20) return ERR_OUT_OF_RANGE;
735 zr->quality = i;
736 return 1;
737 }else if (!strcasecmp(opt, "zrnorm")) {
738 if (param == NULL) return ERR_MISSING_PARAM;
739 if (!strcasecmp(param, "NTSC")) {
740 mp_msg(MSGT_VO, MSGL_V, "zr: Norm set to NTSC\n");
741 zr->norm = VIDEO_MODE_NTSC;
742 return 1;
743 } else if (!strcasecmp(param, "PAL")) {
744 mp_msg(MSGT_VO, MSGL_V, "zr: Norm set to PAL\n");
745 zr->norm = VIDEO_MODE_PAL;
746 return 1;
747 } else {
748 return ERR_OUT_OF_RANGE;
750 }else if (!strcasecmp(opt, "zrhelp")){
751 printf("Help for -vo zr: Zoran ZR360[56]7/ZR36060 based MJPEG capture/playback cards\n");
752 printf("\n");
753 printf("Here are the zr options:\n");
754 printf(
755 "\n"
756 " -zrcrop specify part of the input image that\n"
757 " you want to see as an x-style geometry string\n"
758 " example: -zrcrop 352x288+16+0\n"
759 " -zrvdec vertical decimation 1, 2 or 4\n"
760 " -zrhdec horizontal decimation 1, 2 or 4\n"
761 " -zrfd decimation is only done if the primitive\n"
762 " hardware upscaler can correct for the decimation,\n"
763 " this switch allows you to see the effects\n"
764 " of too much decimation\n"
765 " -zrbw display in black&white (speed increase)\n"
766 " -zrxdoff x offset from upper-left of TV screen (default is 'centered')\n"
767 " -zrydoff y offset from upper-left of TV screen (default is 'centered')\n"
768 " -zrquality jpeg compression quality [BEST] 1 - 20 [VERY BAD]\n"
769 " -zrdev playback device (example -zrdev /dev/video1)\n"
770 " -zrnorm specify norm PAL/NTSC (default: leave at current setting)\n"
771 "\n"
772 "Cinerama support: additional occurances of -zrcrop activate cinerama mode,\n"
773 "suppose you have a 704x272 movie, two DC10+ cards and two beamers (or tv's),\n"
774 "then you would issue the following command:\n\n"
775 "mplayer -vo zr -zrcrop 352x272+0+0 -zrdev /dev/video0 -zrcrop 352x272+352+0 \\\n"
776 " -zrdev /dev/video1 movie.avi\n\n"
777 "Options appearing after the second -zrcrop apply to the second card, it is\n"
778 "possible to dispay at a different jpeg quality or at different decimations.\n\n"
779 "The parameters -zrxdoff and -zrydoff can be used to align the two images.\n"
780 "The maximum number of zoran cards participating in cinerama is 4, so you can\n"
781 "build a 2x2 vidiwall. (untested for obvious reasons, the setup wit a buz and\n"
782 "a DC10+ (and no beamers) is tested, however)\n"
784 exit(0);
787 return ERR_NOT_AN_OPTION;
790 void vo_zr_revertoption(const m_option_t* opt,const char* param) {
792 zr_info_t *zr = &zr_info[1];
793 zr_count = 1;
794 zr_parsing = 0;
796 if (!strcasecmp(param, "zrdev")) {
797 free(zr->device);
798 zr->device=NULL;
799 } else if (!strcasecmp(param, "zrbw"))
800 zr->bw=0;
801 else if (!strcasecmp(param, "zrfd"))
802 zr->fd=0;
803 else if (!strcasecmp(param, "zrcrop"))
804 zr->g.set = zr->g.xoff = zr->g.yoff = 0;
805 else if (!strcasecmp(param, "zrhdec"))
806 zr->hdec = 1;
807 else if (!strcasecmp(param, "zrvdec"))
808 zr->vdec = 1;
809 else if (!strcasecmp(param, "zrxdoff"))
810 zr->xdoff = -1;
811 else if (!strcasecmp(param, "zrydoff"))
812 zr->ydoff = -1;
813 else if (!strcasecmp(param, "zrquality"))
814 zr->quality = 2;
815 else if (!strcasecmp(param, "zrnorm"))
816 zr->norm = VIDEO_MODE_AUTO;
820 static int preinit(const char *arg)
822 if(arg)
824 printf("vo_zr: Unknown subdevice: %s\n",arg);
825 return ENOSYS;
827 return 0;
830 static int control(uint32_t request, void *data)
832 switch (request) {
833 case VOCTRL_QUERY_FORMAT:
834 return query_format(*((uint32_t*)data));
836 return VO_NOTIMPL;