vo_xv: Fix context Shminfo table size
[mplayer.git] / libvo / vo_zr2.c
blobf15494bc376cb67d19d021639eddd9ff3c2638a7
1 /*
2 * playback on Zoran cards, based on vo_zr.c
4 * copyright (C) 2001-2005 Rik Snel
6 * This file is part of MPlayer.
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 /* $Id$ */
25 #include <stdio.h>
26 #include <stdlib.h>
27 #include <string.h>
28 #include <unistd.h>
29 #include <fcntl.h>
30 #include <errno.h>
31 #include <sys/stat.h>
32 #include <sys/types.h>
33 #include <sys/time.h>
34 #include <sys/mman.h>
35 #include <sys/ioctl.h>
36 #include <linux/types.h>
37 #include <linux/videodev.h>
38 #include "config.h"
39 #include "videodev_mjpeg.h"
40 #include "video_out.h"
41 #include "video_out_internal.h"
42 #include "mp_msg.h"
43 #include "subopt-helper.h"
44 #include "fastmemcpy.h"
46 static const vo_info_t info = {
47 "Zoran ZR360[56]7/ZR36060 Driver (DC10(+)/buz/lml33/MatroxRR)",
48 "zr2",
49 "Rik Snel <rsnel@cube.dyndns.org>",
53 const LIBVO_EXTERN(zr2)
55 typedef struct {
56 /* options */
57 char *subdevice;
59 /* information for (and about) the zoran card */
61 unsigned char *buf; /* the JPEGs will be placed here */
62 struct mjpeg_requestbuffers zrq; /* info about this buffer */
64 int vdes; /* file descriptor of card */
65 int playing; /* 0 or 1 */
66 int frame, sync, queue; /* buffer management */
67 struct mjpeg_sync zs; /* state information */
68 struct mjpeg_params zp;
69 struct video_capability vc; /* max resolution and so on */
70 } vo_zr2_priv_t;
72 static vo_zr2_priv_t priv;
74 #define ZR2_MJPEG_NBUFFERS 2
75 #define ZR2_MJPEG_SIZE 1024*256
77 /* some convenient #define's, is this portable enough? */
78 #define DBG2(...) mp_msg(MSGT_VO, MSGL_DBG2, "vo_zr2: " __VA_ARGS__)
79 #define VERBOSE(...) mp_msg(MSGT_VO, MSGL_V, "vo_zr2: " __VA_ARGS__)
80 #define ERROR(...) mp_msg(MSGT_VO, MSGL_ERR, "vo_zr2: " __VA_ARGS__)
81 #define WARNING(...) mp_msg(MSGT_VO, MSGL_WARN, "vo_zr2: " __VA_ARGS__)
83 static void stop_playing(vo_zr2_priv_t *p) {
84 if (p->playing) {
85 p->frame = -1;
86 if (ioctl(p->vdes, MJPIOC_QBUF_PLAY, &p->frame) < 0)
87 ERROR("error stopping playback\n");
88 p->playing = 0;
89 p->sync = 0;
90 p->queue = 0;
91 p->frame = 0;
95 static const char *guess_device(const char *suggestion, int inform) {
96 struct stat vstat;
97 int res;
98 static const char * const devs[] = {
99 "/dev/video",
100 "/dev/video0",
101 "/dev/v4l/video0",
102 "/dev/v4l0",
103 "/dev/v4l",
104 NULL
106 const char * const *dev = devs;
108 if (suggestion) {
109 if (!*suggestion) {
110 ERROR("error: specified device name is empty string\n");
111 return NULL;
114 res = stat(suggestion, &vstat);
115 if (res == 0 && S_ISCHR(vstat.st_mode)) {
116 if (inform) VERBOSE("using device %s\n", suggestion);
117 return suggestion;
118 } else {
119 if (res != 0) ERROR("%s does not exist\n", suggestion);
120 else ERROR("%s is no character device\n", suggestion);
121 /* don't try to be smarter than the user, just exit */
122 return NULL;
126 while (*(++dev) != NULL) {
127 if (stat(*dev, &vstat) == 0 && S_ISCHR(vstat.st_mode)) {
128 VERBOSE("guessed video device %s\n", *dev);
129 return *dev;
131 dev++;
134 ERROR("unable to find video device\n");
136 return NULL;
139 static int query_format(uint32_t format) {
140 if (format==IMGFMT_ZRMJPEGNI ||
141 format==IMGFMT_ZRMJPEGIT ||
142 format==IMGFMT_ZRMJPEGIB)
143 return VFCAP_CSP_SUPPORTED|VFCAP_CSP_SUPPORTED_BY_HW;
144 return 0;
147 static uint32_t draw_image(mp_image_t *mpi) {
148 vo_zr2_priv_t *p = &priv;
149 int size = (int)mpi->planes[1];
150 if (size > (int)p->zrq.size) {
151 ERROR("incoming JPEG image (size=%d) doesn't fit in buffer\n",
152 size);
153 return VO_FALSE;
156 /* looking for free buffer */
157 if (p->queue - p->sync < (int)p->zrq.count) p->frame = p->queue;
158 else {
159 if (ioctl(p->vdes, MJPIOC_SYNC, &p->zs) < 0) {
160 ERROR("error waiting for buffer to become free\n");
161 return VO_FALSE;
163 p->frame = p->zs.frame;
164 p->sync++;
167 /* copy the jpeg image to the buffer which we acquired */
168 fast_memcpy(p->buf + p->zrq.size*p->frame, mpi->planes[0], size);
170 return VO_TRUE;
173 static const char *normstring(int norm) {
174 switch (norm) {
175 case VIDEO_MODE_PAL:
176 return "PAL";
177 case VIDEO_MODE_NTSC:
178 return "NTSC";
179 case VIDEO_MODE_SECAM:
180 return "SECAM";
181 case VIDEO_MODE_AUTO:
182 return "auto";
184 return "undefined";
187 static int get_norm(const char *n) {
188 if (!strcmp(n, "PAL")) return VIDEO_MODE_PAL;
189 if (!strcmp(n, "NTSC")) return VIDEO_MODE_NTSC;
190 if (!strcmp(n, "SECAM")) return VIDEO_MODE_SECAM;
191 if (!strcmp(n, "auto")) return VIDEO_MODE_AUTO;
192 return -1; /* invalid */
195 static int nc(const char **norm) {
196 if (get_norm(*norm) == -1) {
197 ERROR("norm \"%s\" is not supported, choose from PAL, NTSC, SECAM and auto\n", *norm);
198 return 0;
199 } else return 1;
202 static int pbc(int *prebuf) {
203 if (*prebuf) WARNING("prebuffering is not yet supported\n");
204 return 1;
207 static int preinit(const char *arg) {
208 vo_zr2_priv_t *p = &priv;
209 const char *dev = NULL;
210 char *dev_arg = NULL, *norm_arg = NULL;
211 int norm = VIDEO_MODE_AUTO, prebuf = 0;
212 const opt_t subopts[] = { /* don't want warnings with -Wall... */
213 { "dev", OPT_ARG_MSTRZ, &dev_arg, NULL },
214 { "prebuf", OPT_ARG_BOOL, &prebuf, (opt_test_f)pbc },
215 { "norm", OPT_ARG_MSTRZ, &norm_arg, (opt_test_f)nc },
216 { NULL, 0, NULL, NULL }
219 VERBOSE("preinit() called with arg: %s\n", arg);
220 memset(p, 0, sizeof(*p)); /* set defaults */
221 p->vdes = -1;
223 if (subopt_parse(arg, subopts)) {
224 mp_msg(MSGT_VO, MSGL_FATAL,
225 "Allowed suboptions for -vo zr2 are:\n"
226 "- dev=DEVICE (default: %s)\n"
227 "- norm=PAL|NTSC|SECAM|auto (default: auto)\n"
228 "- prebuf/noprebuf (default:"
229 " noprebuf)\n"
230 "\n"
231 "Example: mplayer -vo zr2:dev=/dev/video1:"
232 "norm=PAL movie.avi\n\n"
233 , guess_device(NULL, 0));
234 free(norm_arg);
235 free(dev_arg);
236 return -1;
239 /* interpret the strings we got from subopt_parse */
240 if (norm_arg) {
241 norm = get_norm(norm_arg);
242 free(norm_arg);
245 if (dev_arg) dev = dev_arg;
247 dev = guess_device(dev, 1);
248 if (!dev) {
249 free(dev_arg);
250 uninit();
251 return 1;
254 p->vdes = open(dev, O_RDWR);
255 if (p->vdes < 0) {
256 ERROR("error opening %s: %s\n", dev, strerror(errno));
257 free(dev_arg);
258 uninit();
259 return 1;
262 free(dev_arg);
264 /* check if we really are dealing with a zoran card */
265 if (ioctl(p->vdes, MJPIOC_G_PARAMS, &p->zp) < 0) {
266 ERROR("%s probably is not a DC10(+)/buz/lml33\n", dev);
267 uninit();
268 return 1;
271 VERBOSE("kernel driver version %d.%d, current norm is %s\n",
272 p->zp.major_version, p->zp.minor_version,
273 normstring(p->zp.norm));
275 /* changing the norm in the zoran_params and MJPIOC_S_PARAMS
276 * does nothing the last time I tried, so bail out if the norm
277 * is not correct */
278 if (norm != VIDEO_MODE_AUTO && p->zp.norm != norm) {
279 ERROR("mplayer currently can't change the video norm, "
280 "change it with (eg.) XawTV and retry.\n");
281 uninit();
282 return 1;
285 /* gather useful information */
286 if (ioctl(p->vdes, VIDIOCGCAP, &p->vc) < 0) {
287 ERROR("error getting video capabilities from %s\n", dev);
288 uninit();
289 return 1;
292 VERBOSE("card reports maxwidth=%d, maxheight=%d\n",
293 p->vc.maxwidth, p->vc.maxheight);
295 /* according to the mjpegtools source, some cards return a bogus
296 * vc.maxwidth, correct it here. If a new zoran card appears with a
297 * maxwidth different 640, 720 or 768 this code may lead to problems */
298 if (p->vc.maxwidth != 640 && p->vc.maxwidth != 768) {
299 VERBOSE("card probably reported bogus width (%d), "
300 "changing to 720\n", p->vc.maxwidth);
301 p->vc.maxwidth = 720;
304 p->zrq.count = ZR2_MJPEG_NBUFFERS;
305 p->zrq.size = ZR2_MJPEG_SIZE;
307 if (ioctl(p->vdes, MJPIOC_REQBUFS, &p->zrq)) {
308 ERROR("error requesting %d buffers of size %d\n",
309 ZR2_MJPEG_NBUFFERS, ZR2_MJPEG_NBUFFERS);
310 uninit();
311 return 1;
314 VERBOSE("got %ld buffers of size %ld (wanted %d buffers of size %d)\n",
315 p->zrq.count, p->zrq.size, ZR2_MJPEG_NBUFFERS,
316 ZR2_MJPEG_SIZE);
318 p->buf = (unsigned char*)mmap(0, p->zrq.count*p->zrq.size,
319 PROT_READ|PROT_WRITE, MAP_SHARED, p->vdes, 0);
321 if (p->buf == MAP_FAILED) {
322 ERROR("error mapping requested buffers: %s", strerror(errno));
323 uninit();
324 return 1;
327 return 0;
330 static int config(uint32_t width, uint32_t height, uint32_t d_width,
331 uint32_t d_height, uint32_t flags, char *title, uint32_t format) {
332 int fields = 1, top_first = 1, err = 0;
333 int stretchx = 1, stretchy = 1;
334 struct mjpeg_params zptmp;
335 vo_zr2_priv_t *p = &priv;
336 VERBOSE("config() called\n");
338 /* paranoia check */
339 if (!query_format(format)) {
340 ERROR("called with wrong format, should be impossible\n");
341 return 1;
344 if ((int)height > p->vc.maxheight) {
345 ERROR("input height %d is too large, maxheight=%d\n",
346 height, p->vc.maxheight);
347 err = 1;
350 if (format != IMGFMT_ZRMJPEGNI) {
351 fields = 2;
352 if (format == IMGFMT_ZRMJPEGIB)
353 top_first = 0;
354 } else if ((int)height > p->vc.maxheight/2) {
355 ERROR("input is too high (%d) for non-interlaced playback"
356 "max=%d\n", height, p->vc.maxheight);
357 err = 1;
360 if (width%16 != 0) {
361 ERROR("input width=%d, must be multiple of 16\n", width);
362 err = 1;
365 if (height%(fields*8) != 0) {
366 ERROR("input height=%d, must be multiple of %d\n",
367 height, 2*fields);
368 err = 1;
371 /* we assume sample_aspect = 1 */
372 if (fields == 1) {
373 if (2*d_width <= (uint32_t)p->vc.maxwidth) {
374 VERBOSE("stretching x direction to preserve aspect\n");
375 d_width *= 2;
376 } else VERBOSE("unable to preserve aspect, screen width "
377 "too small\n");
380 if (d_width == width) stretchx = 1;
381 else if (d_width == 2*width) stretchx = 2;
382 #if 0 /* do minimal stretching for now */
383 else if (d_width == 4*width) stretchx = 4;
384 else WARNING("d_width must be {1,2,4}*width, using defaults\n");
386 if (d_height == height) stretchy = 1;
387 else if (d_height == 2*height) stretchy = 2;
388 else if (d_height == 4*height) stretchy = 4;
389 else WARNING("d_height must be {1,2,4}*height, using defaults\n");
390 #endif
392 if (stretchx*width > (uint32_t)p->vc.maxwidth) {
393 ERROR("movie to be played is too wide, width=%d>maxwidth=%d\n",
394 width*stretchx, p->vc.maxwidth);
395 err = 1;
398 if (stretchy*height > (uint32_t)p->vc.maxheight) {
399 ERROR("movie to be played is too heigh, height=%d>maxheight"
400 "=%d\n", height*stretchy, p->vc.maxheight);
401 err = 1;
404 if (err == 1) return 1;
406 /* some video files (eg. concatenated MPEG files), make MPlayer
407 * call config() during playback while no parameters have changed.
408 * We make configuration changes to a temporary params structure,
409 * compare it with the old params structure and only apply the new
410 * config if it is different from the old one. */
411 memcpy(&zptmp, &p->zp, sizeof(zptmp));
413 /* translate the configuration to zoran understandable format */
414 zptmp.decimation = 0;
415 zptmp.HorDcm = stretchx;
416 zptmp.VerDcm = stretchy;
417 zptmp.TmpDcm = 1;
418 zptmp.field_per_buff = fields;
419 zptmp.odd_even = top_first;
421 /* center the image on screen */
422 zptmp.img_x = (p->vc.maxwidth - width*stretchx)/2;
423 zptmp.img_y = (p->vc.maxheight - height*stretchy*(3-fields))/4;
425 zptmp.img_width = stretchx*width;
426 zptmp.img_height = stretchy*height/fields;
428 VERBOSE("tv: %dx%d, out: %dx%d+%d+%d, in: %ux%u %s%s%s\n",
429 p->vc.maxwidth, p->vc.maxheight,
430 zptmp.img_width, 2*zptmp.img_height,
431 zptmp.img_x, 2*zptmp.img_y,
432 width, height, (fields == 1) ? "non-interlaced" : "",
433 (fields == 2 && top_first == 1)
434 ? "interlaced top first" : "",
435 (fields == 2 && top_first == 0)
436 ? "interlaced bottom first" : "");
438 if (memcmp(&zptmp, &p->zp, sizeof(zptmp))) {
439 /* config differs, we must update */
440 memcpy(&p->zp, &zptmp, sizeof(zptmp));
441 stop_playing(p);
442 if (ioctl(p->vdes, MJPIOC_S_PARAMS, &p->zp) < 0) {
443 ERROR("error writing display params to card\n");
444 return 1;
446 VERBOSE("successfully written display parameters to card\n");
447 } else VERBOSE("config didn't change, no need to write it to card\n");
449 return 0;
452 static int control(uint32_t request, void *data) {
453 switch (request) {
454 case VOCTRL_QUERY_FORMAT:
455 return query_format(*((uint32_t*)data));
456 case VOCTRL_DRAW_IMAGE:
457 return draw_image(data);
459 return VO_NOTIMPL;
462 static int draw_frame(uint8_t *src[]) {
463 return 0;
466 static int draw_slice(uint8_t *image[], int stride[],
467 int w, int h, int x, int y) {
468 return 0;
471 static void draw_osd(void) {
474 static void flip_page(void) {
475 vo_zr2_priv_t *p = &priv;
476 /* queueing the buffer for playback */
477 /* queueing the first buffer automatically starts playback */
478 if (p->playing == 0) p->playing = 1;
479 if (ioctl(p->vdes, MJPIOC_QBUF_PLAY, &p->frame) < 0)
480 ERROR("error queueing buffer for playback\n");
481 else p->queue++;
484 static void check_events(void) {
487 static void uninit(void) {
488 vo_zr2_priv_t *p = &priv;
489 VERBOSE("uninit() called (may be called from preinit() on error)\n");
491 stop_playing(p);
493 if (p->buf && munmap(p->buf, p->zrq.size*p->zrq.count))
494 ERROR("error munmapping buffer: %s\n", strerror(errno));
496 if (p->vdes >= 0) close(p->vdes);
497 free(p->subdevice);