/* display.c, X11 interface */
/*
* All modifications (mpeg2decode -> mpeg2play) are
* Copyright (C) 1994, Stefan Eckart. All Rights Reserved.
*/
/* Copyright (C) 1994, MPEG Software Simulation Group. All Rights Reserved. */
/*
* Disclaimer of Warranty
*
* These software programs are available to the user without any license fee or
* royalty on an "as is" basis. The MPEG Software Simulation Group disclaims
* any and all warranties, whether express, implied, or statuary, including any
* implied warranties or merchantability or of fitness for a particular
* purpose. In no event shall the copyright-holder be liable for any
* incidental, punitive, or consequential damages of any kind whatsoever
* arising from the use of these programs.
*
* This disclaimer of warranty extends to the user of these programs and user's
* customers, employees, agents, transferees, successors, and assigns.
*
* The MPEG Software Simulation Group does not represent or warrant that the
* programs furnished hereunder are free of infringement of any third-party
* patents.
*
* Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
* are subject to royalty fees to patent holders. Many of these patents are
* general enough such that they are unavoidable regardless of implementation
* design.
*
*/
/* the Xlib interface is closely modeled after
* mpeg_play 2.0 by the Berkeley Plateau Research Group
*/
#include <stdio.h>
#include <stdlib.h>
#include <X11/Xlib.h>
#include <X11/Xutil.h>
#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[]));
static void dithertop _ANSI_ARGS_((unsigned char *src[], unsigned char *dst));
static void ditherbot _ANSI_ARGS_((unsigned char *src[], unsigned char *dst));
static void ditherframe444 _ANSI_ARGS_((unsigned char *src[]));
static void dithertop444 _ANSI_ARGS_((unsigned char *src[], unsigned char *dst));
static void ditherbot444 _ANSI_ARGS_((unsigned char *src[], unsigned char *dst));
/* local data */
static unsigned char *dithered_image, *dithered_image2;
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, *ximage2;
static unsigned char pixel[256];
#ifdef SH_MEM
#include <sys/ipc.h>
#include <sys/shm.h>
#include <X11/extensions/XShm.h>
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 = "MPEG-2 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;
hint.width = horizontal_size;
hint.height = vertical_size;
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)
{
ximage = XShmCreateImage(display, None, 8, ZPixmap, NULL,
&shminfo1,
coded_picture_width, coded_picture_height);
if (!prog_seq)
ximage2 = XShmCreateImage(display, None, 8, ZPixmap, NULL,
&shminfo2,
coded_picture_width, coded_picture_height);
/* If no go, then revert to normal Xlib calls. */
if (ximage==NULL || (!prog_seq && ximage2==NULL))
{
if (ximage!=NULL)
XDestroyImage(ximage);
if (!prog_seq && ximage2!=NULL)
XDestroyImage(ximage2);
if (!quiet)
fprintf(stderr, "Shared memo