/* display.c, X11 interface */ /* * tmndecode * Copyright (C) 1995 Telenor R&D * Karl Olav Lillevold * * based on mpeg2decode, (C) 1994, MPEG Software Simulation Group * and mpeg2play, (C) 1994 Stefan Eckart * * */ /* the Xlib interface is closely modeled after * mpeg_play 2.0 by the Berkeley Plateau Research Group */ #ifdef DISPLAY #include #include #include #include #include "config.h" #include "global.h" /* private prototypes */ static void display_image _ANSI_ARGS_((XImage *ximage, unsigned char *dithered_image)); static void ditherframe _ANSI_ARGS_((unsigned char *src[])); /* local data */ static unsigned char *dithered_image; static unsigned char ytab[16*(256+16)]; static unsigned char uvtab[256*269+270]; /* X11 related variables */ static Display *display; static Window window; static GC gc; static XImage *ximage; static unsigned char pixel[256]; #ifdef SH_MEM #include #include #include static int HandleXError _ANSI_ARGS_((Display *dpy, XErrorEvent *event)); static void InstallXErrorHandler _ANSI_ARGS_((void)); static void DeInstallXErrorHandler _ANSI_ARGS_((void)); static int shmem_flag; static XShmSegmentInfo shminfo1, shminfo2; static int gXErrorFlag; static int CompletionType = -1; static int HandleXError(dpy, event) Display *dpy; XErrorEvent *event; { gXErrorFlag = 1; return 0; } static void InstallXErrorHandler() { XSetErrorHandler(HandleXError); XFlush(display); } static void DeInstallXErrorHandler() { XSetErrorHandler(NULL); XFlush(display); } #endif /* connect to server, create and map window, * allocate colors and (shared) memory */ void init_display(name) char *name; { int crv, cbu, cgu, cgv; int y, u, v, r, g, b; int i; char dummy; int screen; Colormap cmap; int private; XColor xcolor; unsigned int fg, bg; char *hello = "TMN Display"; XSizeHints hint; XVisualInfo vinfo; XEvent xev; unsigned long tmp_pixel; XWindowAttributes xwa; display = XOpenDisplay(name); if (display == NULL) error("Can not open display\n"); screen = DefaultScreen(display); hint.x = 200; hint.y = 200; if (expand) { hint.width = 2*horizontal_size; /* width of the display window */ hint.height = 2*vertical_size; /* height of the display window */ } else { hint.width = horizontal_size; /* width of the display window */ hint.height = vertical_size; /* height of the display window */ } hint.flags = PPosition | PSize; /* Get some colors */ bg = WhitePixel (display, screen); fg = BlackPixel (display, screen); /* Make the window */ if (!XMatchVisualInfo(display, screen, 8, PseudoColor, &vinfo)) { if (!XMatchVisualInfo(display, screen, 8, GrayScale, &vinfo)) error("requires 8 bit display\n"); } window = XCreateSimpleWindow (display, DefaultRootWindow (display), hint.x, hint.y, hint.width, hint.height, 4, fg, bg); XSelectInput(display, window, StructureNotifyMask); /* Tell other applications about this window */ XSetStandardProperties (display, window, hello, hello, None, NULL, 0, &hint); /* Map window. */ XMapWindow(display, window); /* Wait for map. */ do { XNextEvent(display, &xev); } while (xev.type != MapNotify || xev.xmap.event != window); XSelectInput(display, window, NoEventMask); /* matrix coefficients */ crv = convmat[matrix_coefficients][0]; cbu = convmat[matrix_coefficients][1]; cgu = convmat[matrix_coefficients][2]; cgv = convmat[matrix_coefficients][3]; /* allocate colors */ gc = DefaultGC(display, screen); cmap = DefaultColormap(display, screen); private = 0; /* color allocation: * i is the (internal) 8 bit color number, it consists of separate * bit fields for Y, U and V: i = (yyyyuuvv), we don't use yyyy=0000 * yyyy=0001 and yyyy=1111, this leaves 48 colors for other applications * * the allocated colors correspond to the following Y, U and V values: * Y: 40, 56, 72, 88, 104, 120, 136, 152, 168, 184, 200, 216, 232 * U,V: -48, -16, 16, 48 * * U and V values span only about half the color space; this gives * usually much better quality, although highly saturated colors can * not be displayed properly * * translation to R,G,B is implicitly done by the color look-up table */ for (i=32; i<240; i++) { /* color space conversion */ y = 16*((i>>4)&15) + 8; u = 32*((i>>2)&3) - 48; v = 32*(i&3) - 48; y = 76309 * (y - 16); /* (255/219)*65536 */ r = clp[(y + crv*v + 32768)>>16]; g = clp[(y - cgu*u -cgv*v + 32768)>>16]; b = clp[(y + cbu*u + 32786)>>16]; /* X11 colors are 16 bit */ xcolor.red = r << 8; xcolor.green = g << 8; xcolor.blue = b << 8; if (XAllocColor(display, cmap, &xcolor) != 0) pixel[i] = xcolor.pixel; else { /* allocation failed, have to use a private colormap */ if (private) error("Couldn't allocate private colormap"); private = 1; if (!quiet) fprintf(stderr, "Using private colormap (%d colors were available).\n", i-32); /* Free colors. */ while (--i >= 32) { tmp_pixel = pixel[i]; /* because XFreeColors expects unsigned long */ XFreeColors(display, cmap, &tmp_pixel, 1, 0); } /* i is now 31, this restarts the outer loop */ /* create private colormap */ XGetWindowAttributes(display, window, &xwa); cmap = XCreateColormap(display, window, xwa.visual, AllocNone); XSetWindowColormap(display, window, cmap); } } #ifdef SH_MEM if (XShmQueryExtension(display)) shmem_flag = 1; else { shmem_flag = 0; if (!quiet) fprintf(stderr, "Shared memory not supported\nReverting to normal Xlib\n"); } if (shmem_flag) CompletionType = XShmGetEventBase(display) + ShmCompletion; InstallXErrorHandler(); if (shmem_flag) { if (expand) ximage = XShmCreateImage(display, None, 8, ZPixmap, NULL, &shminfo1, 2*coded_picture_width, 2*coded_picture_height); else ximage = XShmCreateImage(display, None, 8, ZPixmap, NULL, &shminfo1, coded_picture_width, coded_picture_height); /* If no go, then revert to normal Xlib calls. */ if (ximage==NULL) { if (ximage!=NULL) XDestroyImage(ximage); if (!quiet) fprintf(stderr, "Shared memory error, disabling (Ximage error)\n"); goto shmemerror; } /* Success here, continue. */ shminfo1.shmid = shmget(IPC_PRIVATE, ximage->bytes_per_line * ximage->height, IPC_CREAT | 0777); if (shminfo1.shmid<0) { XDestroyImage(ximage); if (!quiet) fprintf(stderr, "Shared memory error, disabling (seg id error)\n"); goto shmemerror; } shminfo1.shmaddr = (char *) shmat(shminfo1.shmid, 0, 0); shminfo2.shmaddr = (char *) shmat(shminfo2.shmid, 0, 0); if (shminfo1.shmaddr==((char *) -1)) { XDestroyImage(ximage); if (shminfo1.shmaddr!=((char *) -1)) shmdt(shminfo1.shmaddr); if (!quiet) { fprintf(stderr, "Shared memory error, disabling (address error)\n"); } goto shmemerror; } ximage->data = shminfo1.shmaddr; dithered_image = (unsigned char *)ximage->data; shminfo1.readOnly = False; XShmAttach(display, &shminfo1); XSync(display, False); if (gXErrorFlag) { /* Ultimate failure here. */ XDestroyImage(ximage); shmdt(shminfo1.shmaddr); if (!quiet) fprintf(stderr, "Shared memory error, disabling.\n"); gXErrorFlag = 0; goto shmemerror; } else { shmctl(shminfo1.shmid, IPC_RMID, 0); } if (!quiet) { fprintf(stderr, "Sharing memory.\n"); } } else { shmemerror: shmem_flag = 0; #endif if (expand) { ximage = XCreateImage(display,None,8,ZPixmap,0,&dummy, 2*coded_picture_width,2*coded_picture_height,8,0); if (!(dithered_image = (unsigned char *)malloc(coded_picture_width* coded_picture_height*4))) error("malloc failed"); } else { ximage = XCreateImage(display,None,8,ZPixmap,0,&dummy, coded_picture_width,coded_picture_height,8,0); if (!(dithered_image = (unsigned char *)malloc(coded_picture_width* coded_picture_height))) error("malloc failed"); } #ifdef SH_MEM } DeInstallXErrorHandler(); #endif } void exit_display() { #ifdef SH_MEM if (shmem_flag) { XShmDetach(display, &shminfo1); XDestroyImage(ximage); shmdt(shminfo1.shmaddr); } #endif } static void display_image(ximage,dithered_image) XImage *ximage; unsigned char *dithered_image; { /* display dithered image */ #ifdef SH_MEM if (shmem_flag) { XShmPutImage(display, window, gc, ximage, 0, 0, 0, 0, ximage->width, ximage->height, True); XFlush(display); while (1) { XEvent xev; XNextEvent(display, &xev); if (xev.type == CompletionType) break; } } else #endif { ximage->data = (char *) dithered_image; XPutImage(display, window, gc, ximage, 0, 0, 0, 0, ximage->width, ximage->height); } } /* 4x4 ordered dither * * threshold pattern: * 0 8 2 10 * 12 4 14 6 * 3 11 1 9 * 15 7 13 5 */ void init_dither() { int i, j, v; unsigned char ctab[256+32]; for (i=0; i<256+16; i++) { v = (i-8)>>4; if (v<2) v = 2; else if (v>14) v = 14; for (j=0; j<16; j++) ytab[16*i+j] = pixel[(v<<4)+j]; } for (i=0; i<256+32; i++) { v = (i+48-128)>>5; if (v<0) v = 0; else if (v>3) v = 3; ctab[i] = v; } for (i=0; i<255+15; i++) for (j=0; j<255+15; j++) uvtab[256*i+j]=(ctab[i+16]<<6)|(ctab[j+16]<<4)|(ctab[i]<<2)|ctab[j]; } void dither(src) unsigned char *src[]; { ditherframe(src); display_image(ximage,dithered_image); } /* only for 4:2:0 and 4:2:2! */ static void ditherframe(src) unsigned char *src[]; { int i,j; unsigned int uv; unsigned char *py,*pu,*pv,*dst; int width, height, cwidth; if (expand) { width = 2*coded_picture_width; height = 2*coded_picture_height; cwidth = 2*chrom_width; } else { width = coded_picture_width; height = coded_picture_height; cwidth = chrom_width; } py = src[0]; pu = src[1]; pv = src[2]; dst = dithered_image; for (j=0; j>4)]; uv = uvtab[((*pu++<<8)|*pv++)+1028]; *dst++ = ytab[((*py++ +2)<<4)|(uv&15)]; *dst++ = ytab[((*py++ +10)<<4)|(uv>>4)]; uv = uvtab[(*pu++<<8)|*pv++]; *dst++ = ytab[((*py++)<<4)|(uv&15)]; *dst++ = ytab[((*py++ +8)<<4)|(uv>>4)]; uv = uvtab[((*pu++<<8)|*pv++)+1028]; *dst++ = ytab[((*py++ +2)<<4)|(uv&15)]; *dst++ = ytab[((*py++ +10)<<4)|(uv>>4)]; } pu -= cwidth; pv -= cwidth; /* line j + 1 */ for (i=0; i>4)]; *dst++ = ytab[((*py++ +4)<<4)|(uv&15)]; uv = uvtab[((*pu++<<8)|*pv++)+3084]; *dst++ = ytab[((*py++ +14)<<4)|(uv>>4)]; *dst++ = ytab[((*py++ +6)<<4)|(uv&15)]; uv = uvtab[((*pu++<<8)|*pv++)+2056]; *dst++ = ytab[((*py++ +12)<<4)|(uv>>4)]; *dst++ = ytab[((*py++ +4)<<4)|(uv&15)]; uv = uvtab[((*pu++<<8)|*pv++)+3084]; *dst++ = ytab[((*py++ +14)<<4)|(uv>>4)]; *dst++ = ytab[((*py++ +6)<<4)|(uv&15)]; } /* line j + 2 */ for (i=0; i>4)]; uv = uvtab[((*pu++<<8)|*pv++)+514]; *dst++ = ytab[((*py++ +1)<<4)|(uv&15)]; *dst++ = ytab[((*py++ +9)<<4)|(uv>>4)]; uv = uvtab[((*pu++<<8)|*pv++)+1542]; *dst++ = ytab[((*py++ +3)<<4)|(uv&15)]; *dst++ = ytab[((*py++ +11)<<4)|(uv>>4)]; uv = uvtab[((*pu++<<8)|*pv++)+514]; *dst++ = ytab[((*py++ +1)<<4)|(uv&15)]; *dst++ = ytab[((*py++ +9)<<4)|(uv>>4)]; } pu -= cwidth; pv -= cwidth; /* line j + 3 */ for (i=0; i>4)]; *dst++ = ytab[((*py++ +7)<<4)|(uv&15)]; uv = uvtab[((*pu++<<8)|*pv++)+2570]; *dst++ = ytab[((*py++ +13)<<4)|(uv>>4)]; *dst++ = ytab[((*py++ +5)<<4)|(uv&15)]; uv = uvtab[((*pu++<<8)|*pv++)+3598]; *dst++ = ytab[((*py++ +15)<<4)|(uv>>4)]; *dst++ = ytab[((*py++ +7)<<4)|(uv&15)]; uv = uvtab[((*pu++<<8)|*pv++)+2570]; *dst++ = ytab[((*py++ +13)<<4)|(uv>>4)]; *dst++ = ytab[((*py++ +5)<<4)|(uv&15)]; } } } #endif