OpenCores
URL https://opencores.org/ocsvn/mpeg2fpga/mpeg2fpga/trunk

Subversion Repositories mpeg2fpga

[/] [mpeg2fpga/] [trunk/] [tools/] [mpeg2dec/] [display.c] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 kdv
/* display.c, X11 interface                                                 */
2
 
3
/* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
4
 
5
/*
6
 * Disclaimer of Warranty
7
 *
8
 * These software programs are available to the user without any license fee or
9
 * royalty on an "as is" basis.  The MPEG Software Simulation Group disclaims
10
 * any and all warranties, whether express, implied, or statuary, including any
11
 * implied warranties or merchantability or of fitness for a particular
12
 * purpose.  In no event shall the copyright-holder be liable for any
13
 * incidental, punitive, or consequential damages of any kind whatsoever
14
 * arising from the use of these programs.
15
 *
16
 * This disclaimer of warranty extends to the user of these programs and user's
17
 * customers, employees, agents, transferees, successors, and assigns.
18
 *
19
 * The MPEG Software Simulation Group does not represent or warrant that the
20
 * programs furnished hereunder are free of infringement of any third-party
21
 * patents.
22
 *
23
 * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
24
 * are subject to royalty fees to patent holders.  Many of these patents are
25
 * general enough such that they are unavoidable regardless of implementation
26
 * design.
27
 *
28
 */
29
 
30
#ifdef DISPLAY
31
 
32
 /* the Xlib interface is closely modeled after
33
  * mpeg_play 2.0 by the Berkeley Plateau Research Group
34
  */
35
 
36
#include <stdio.h>
37
#include <stdlib.h>
38
 
39
#include <X11/Xlib.h>
40
#include <X11/Xutil.h>
41
#include <string.h>
42
 
43
#include "config.h"
44
#include "global.h"
45
 
46
extern void conv422to444 _ANSI_ARGS_((unsigned char *src, unsigned char *dst));
47
extern void conv420to422 _ANSI_ARGS_((unsigned char *src, unsigned char *dst));
48
/* private prototypes */
49
static void Display_Image _ANSI_ARGS_((XImage * myximage, unsigned char *ImageData));
50
 
51
/* local data */
52
static unsigned char *ImageData, *ImageData2;
53
 
54
/* X11 related variables */
55
static Display *mydisplay;
56
static Window mywindow;
57
static GC mygc;
58
static XImage *myximage, *myximage2;
59
static int bpp;
60
static int has32bpp = 0;
61
static XWindowAttributes attribs;
62
static X_already_started = 0;
63
 
64
 
65
#ifdef SH_MEM
66
 
67
#include <sys/ipc.h>
68
#include <sys/shm.h>
69
#include <X11/extensions/XShm.h>
70
 
71
static int HandleXError _ANSI_ARGS_((Display * dpy, XErrorEvent * event));
72
static void InstallXErrorHandler _ANSI_ARGS_((void));
73
static void DeInstallXErrorHandler _ANSI_ARGS_((void));
74
 
75
static int Shmem_Flag;
76
static XShmSegmentInfo Shminfo1, Shminfo2;
77
static int gXErrorFlag;
78
static int CompletionType = -1;
79
 
80
static int HandleXError(Dpy, Event)
81
Display *Dpy;
82
XErrorEvent *Event;
83
{
84
    gXErrorFlag = 1;
85
 
86
    return 0;
87
}
88
 
89
static void InstallXErrorHandler()
90
{
91
    XSetErrorHandler(HandleXError);
92
    XFlush(mydisplay);
93
}
94
 
95
static void DeInstallXErrorHandler()
96
{
97
    XSetErrorHandler(NULL);
98
    XFlush(mydisplay);
99
}
100
 
101
#endif
102
 
103
/* Setup pseudocolor (grayscale) */
104
void set_colors()
105
{
106
        Colormap cmap;
107
        XColor mycolor[256];
108
        int i;
109
        if ((cmap = XCreateColormap(mydisplay, mywindow, attribs.visual,
110
                AllocAll)) == 0) {       /* allocate all colors */
111
                fprintf(stderr, "Can't get colors, using existing map\n");
112
                return;
113
        }
114
        for (i = 0; i < 256; i++) {
115
                mycolor[i].flags = DoRed | DoGreen | DoBlue;
116
                mycolor[i].pixel = i;
117
                mycolor[i].red = i << 8;
118
                mycolor[i].green = i << 8;
119
                mycolor[i].blue = i << 8;
120
        }
121
        XStoreColors(mydisplay, cmap, mycolor, 255);
122
        XSetWindowColormap(mydisplay, mywindow, cmap);
123
}
124
/* connect to server, create and map window,
125
 * allocate colors and (shared) memory
126
 */
127
void Initialize_Display_Process(name)
128
char *name;
129
{
130
    char dummy;
131
    int screen;
132
    unsigned int fg, bg;
133
    char *hello = "MPEG-2 Decoder";
134
    XSizeHints hint;
135
    XVisualInfo vinfo;
136
    XEvent xev;
137
 
138
    if (X_already_started)
139
        return;
140
 
141
    mydisplay = XOpenDisplay(name);
142
 
143
    if (mydisplay == NULL)
144
        Error("Can not open display\n");
145
 
146
    screen = DefaultScreen(mydisplay);
147
 
148
    hint.x = 200;
149
    hint.y = 200;
150
    hint.width = horizontal_size;
151
    hint.height = vertical_size;
152
    hint.flags = PPosition | PSize;
153
 
154
    /* Get some colors */
155
 
156
    bg = WhitePixel(mydisplay, screen);
157
    fg = BlackPixel(mydisplay, screen);
158
 
159
    /* Make the window */
160
 
161
    XGetWindowAttributes(mydisplay, DefaultRootWindow(mydisplay), &attribs);
162
    bpp = attribs.depth;
163
    if (bpp != 8 && bpp != 15 && bpp != 16 && bpp != 24 && bpp != 32)
164
        Error("Only 8,15,16,24, and 32bpp supported\n");
165
    mywindow = XCreateSimpleWindow(mydisplay, DefaultRootWindow(mydisplay),
166
                     hint.x, hint.y, hint.width, hint.height, 4, fg, bg);
167
 
168
    XSelectInput(mydisplay, mywindow, StructureNotifyMask);
169
 
170
    /* Tell other applications about this window */
171
 
172
    XSetStandardProperties(mydisplay, mywindow, hello, hello, None, NULL, 0, &hint);
173
 
174
    /* Map window. */
175
 
176
    XMapWindow(mydisplay, mywindow);
177
 
178
    /* Wait for map. */
179
    do {
180
        XNextEvent(mydisplay, &xev);
181
    }
182
    while (xev.type != MapNotify || xev.xmap.event != mywindow);
183
    if (bpp == 8)
184
        set_colors();
185
 
186
    XSelectInput(mydisplay, mywindow, NoEventMask);
187
 
188
    mygc = DefaultGC(mydisplay, screen);
189
 
190
#ifdef SH_MEM
191
    if (XShmQueryExtension(mydisplay))
192
        Shmem_Flag = 1;
193
    else {
194
        Shmem_Flag = 0;
195
        if (!Quiet_Flag)
196
            fprintf(stderr, "Shared memory not supported\nReverting to normal Xlib\n");
197
    }
198
 
199
    if (Shmem_Flag)
200
        CompletionType = XShmGetEventBase(mydisplay) + ShmCompletion;
201
 
202
    InstallXErrorHandler();
203
 
204
    if (Shmem_Flag) {
205
 
206
        myximage = XShmCreateImage(mydisplay, None, bpp,
207
                ZPixmap, NULL, &Shminfo1, Coded_Picture_Width,
208
                Coded_Picture_Height);
209
        if (!progressive_sequence)
210
            myximage2 = XShmCreateImage(mydisplay, None, bpp,
211
                ZPixmap, NULL, &Shminfo2, Coded_Picture_Width,
212
                Coded_Picture_Height);
213
 
214
        /* If no go, then revert to normal Xlib calls. */
215
 
216
        if (myximage == NULL || (!progressive_sequence && myximage2 == NULL)) {
217
            if (myximage != NULL)
218
                XDestroyImage(myximage);
219
            if (!progressive_sequence && myximage2 != NULL)
220
                XDestroyImage(myximage2);
221
            if (!Quiet_Flag)
222
                fprintf(stderr, "Shared memory error, disabling (Ximage error)\n");
223
            goto shmemerror;
224
        }
225
        /* Success here, continue. */
226
 
227
        Shminfo1.shmid = shmget(IPC_PRIVATE,
228
                         myximage->bytes_per_line * myximage->height,
229
                                IPC_CREAT | 0777);
230
        if (!progressive_sequence)
231
            Shminfo2.shmid = shmget(IPC_PRIVATE,
232
                       myximage2->bytes_per_line * myximage2->height,
233
                                    IPC_CREAT | 0777);
234
 
235
        if (Shminfo1.shmid < 0 || (!progressive_sequence && Shminfo2.shmid < 0)) {
236
            XDestroyImage(myximage);
237
            if (!progressive_sequence)
238
                XDestroyImage(myximage2);
239
            if (!Quiet_Flag)
240
                fprintf(stderr, "Shared memory error, disabling (seg id error)\n");
241
            goto shmemerror;
242
        }
243
        Shminfo1.shmaddr = (char *) shmat(Shminfo1.shmid, 0, 0);
244
        Shminfo2.shmaddr = (char *) shmat(Shminfo2.shmid, 0, 0);
245
 
246
        if (Shminfo1.shmaddr == ((char *) -1) ||
247
          (!progressive_sequence && Shminfo2.shmaddr == ((char *) -1))) {
248
            XDestroyImage(myximage);
249
            if (Shminfo1.shmaddr != ((char *) -1))
250
                shmdt(Shminfo1.shmaddr);
251
            if (!progressive_sequence) {
252
                XDestroyImage(myximage2);
253
                if (Shminfo2.shmaddr != ((char *) -1))
254
                    shmdt(Shminfo2.shmaddr);
255
            }
256
            if (!Quiet_Flag) {
257
                fprintf(stderr, "Shared memory error, disabling (address error)\n");
258
            }
259
            goto shmemerror;
260
        }
261
        myximage->data = Shminfo1.shmaddr;
262
        ImageData = (unsigned char *) myximage->data;
263
        Shminfo1.readOnly = False;
264
        XShmAttach(mydisplay, &Shminfo1);
265
        if (!progressive_sequence) {
266
            myximage2->data = Shminfo2.shmaddr;
267
            ImageData2 = (unsigned char *) myximage2->data;
268
            Shminfo2.readOnly = False;
269
            XShmAttach(mydisplay, &Shminfo2);
270
        }
271
        XSync(mydisplay, False);
272
 
273
        if (gXErrorFlag) {
274
            /* Ultimate failure here. */
275
            XDestroyImage(myximage);
276
            shmdt(Shminfo1.shmaddr);
277
            if (!progressive_sequence) {
278
                XDestroyImage(myximage2);
279
                shmdt(Shminfo2.shmaddr);
280
            }
281
            if (!Quiet_Flag)
282
                fprintf(stderr, "Shared memory error, disabling.\n");
283
            gXErrorFlag = 0;
284
            goto shmemerror;
285
        } else {
286
            shmctl(Shminfo1.shmid, IPC_RMID, 0);
287
            if (!progressive_sequence)
288
                shmctl(Shminfo2.shmid, IPC_RMID, 0);
289
        }
290
 
291
        if (!Quiet_Flag) {
292
            fprintf(stderr, "Sharing memory.\n");
293
        }
294
    } else {
295
      shmemerror:
296
        Shmem_Flag = 0;
297
#endif
298
        myximage = XGetImage(mydisplay, DefaultRootWindow(mydisplay), 0, 0,
299
            Coded_Picture_Width, Coded_Picture_Height, AllPlanes, ZPixmap);
300
        ImageData = myximage->data;
301
 
302
        if (!progressive_sequence) {
303
            myximage2 = XGetImage(mydisplay, DefaultRootWindow(mydisplay), 0,
304
                0, Coded_Picture_Width, Coded_Picture_Height,
305
                AllPlanes, ZPixmap);
306
            ImageData2 = myximage2->data;
307
        }
308
#ifdef SH_MEM
309
    }
310
 
311
    DeInstallXErrorHandler();
312
#endif
313
    has32bpp = (myximage->bits_per_pixel > 24) ? 1 : 0;
314
    X_already_started++;
315
}
316
 
317
void Terminate_Display_Process()
318
{
319
    getchar();  /* wait for enter to remove window */
320
#ifdef SH_MEM
321
    if (Shmem_Flag) {
322
        XShmDetach(mydisplay, &Shminfo1);
323
        XDestroyImage(myximage);
324
        shmdt(Shminfo1.shmaddr);
325
        if (!progressive_sequence) {
326
            XShmDetach(mydisplay, &Shminfo2);
327
            XDestroyImage(myximage2);
328
            shmdt(Shminfo2.shmaddr);
329
        }
330
    }
331
#endif
332
    XDestroyWindow(mydisplay, mywindow);
333
    XCloseDisplay(mydisplay);
334
    X_already_started = 0;
335
}
336
 
337
static void Display_Image(myximage, ImageData)
338
XImage *myximage;
339
unsigned char *ImageData;
340
{
341
#ifdef SH_MEM
342
    if (Shmem_Flag) {
343
        XShmPutImage(mydisplay, mywindow, mygc, myximage,
344
                0, 0, 0, 0, myximage->width, myximage->height, True);
345
        XFlush(mydisplay);
346
 
347
        while (1) {
348
            XEvent xev;
349
 
350
            XNextEvent(mydisplay, &xev);
351
            if (xev.type == CompletionType)
352
                break;
353
        }
354
    } else
355
#endif
356
        XPutImage(mydisplay, mywindow, mygc, myximage, 0, 0,
357
                0, 0, myximage->width, myximage->height);
358
}
359
 
360
void Display_First_Field(void) { /* nothing */ }
361
void Display_Second_Field(void) { /* nothing */ }
362
 
363
do_display(unsigned char *src[])
364
{
365
    unsigned char *dst, *py, *pu, *pv;
366
    static unsigned char *u444 = 0, *v444, *u422, *v422;
367
    int x, y, Y, U, V, r, g, b, pixel;
368
    int crv, cbu, cgu, cgv;
369
    /* matrix coefficients */
370
    crv = Inverse_Table_6_9[matrix_coefficients][0];
371
    cbu = Inverse_Table_6_9[matrix_coefficients][1];
372
    cgu = Inverse_Table_6_9[matrix_coefficients][2];
373
    cgv = Inverse_Table_6_9[matrix_coefficients][3];
374
    py = src[0];
375
    dst = ImageData;
376
    if (bpp == 8)       /* for speed on 8bpp we do grayscale */
377
        memcpy(dst, py, Coded_Picture_Height*Coded_Picture_Width);
378
    else {
379
        if (chroma_format==CHROMA444 || !hiQdither) {
380
                pv = src[1];
381
                pu = src[2];
382
        } else {
383
            if (!u444) {
384
                if (!(u422=(unsigned char *)malloc((Coded_Picture_Width>>1)*
385
                    Coded_Picture_Height)))
386
                    Error("malloc failed");
387
                if (!(v422=(unsigned char *)malloc((Coded_Picture_Width>>1)*
388
                    Coded_Picture_Height)))
389
                    Error("malloc failed");
390
                if (!(u444=(unsigned char *)malloc(Coded_Picture_Width*
391
                    Coded_Picture_Height)))
392
                    Error("malloc failed");
393
                if (!(v444=(unsigned char *)malloc(Coded_Picture_Width*
394
                    Coded_Picture_Height)))
395
                    Error("malloc failed");
396
            }
397
            if (chroma_format==CHROMA420) {
398
                conv420to422(src[1],v422);
399
                conv420to422(src[2],u422);
400
                conv422to444(v422,v444);
401
                conv422to444(u422,u444);
402
            } else {
403
                conv422to444(src[1],v444);
404
                conv422to444(src[2],u444);
405
            }
406
            pu = u444;
407
            pv = v444;
408
        }
409
        for (y = 0; y < Coded_Picture_Height; y++)
410
            for (x = 0; x < Coded_Picture_Width; x++) {
411
                Y = 76309 * ((*py++) - 16);
412
                if (!hiQdither && chroma_format!=CHROMA444) {
413
                    if (chroma_format==CHROMA422)
414
                        pixel = y * Chroma_Width + (x>>1);
415
                    else        /* 420 */
416
                        pixel = (y>>1) * Chroma_Width + (x>>1);
417
                    U = pu[pixel] - 128;
418
                    V = pv[pixel] - 128;
419
                } else {
420
                    U = (*pu++) - 128;
421
                    V = (*pv++) - 128;
422
                }
423
                r = Clip[(Y+crv*V)>>16];
424
                g = Clip[(Y-cgu*U-cgv*V + 32768)>>16];
425
                b = Clip[(Y+cbu*U + 32768)>>16];
426
                if (has32bpp) {
427
                        /* try to consolidate writes */
428
                        pixel = (b<<16)|(g<<8)|r;
429
                        *(unsigned int *)dst = pixel;
430
                        dst+=4;
431
                } else if (bpp == 24) {
432
                        *dst++ = r;
433
                        *dst++ = g;
434
                        *dst++ = b;
435
                } else {
436
                        if (bpp > 15)   /* 16 bpp */
437
                            pixel=((b<<8)&63488)|((g<<3)&2016)|((r>>3)&31);
438
                        else            /* 15 bpp */
439
                            pixel=((b<<7)&31744)|((g<<2)&992)|((r>>3)&31);
440
                        *(unsigned short *)dst = pixel;
441
                        dst+=2;
442
                }
443
            }
444
    }
445
    Display_Image(myximage, ImageData);
446
}
447
#endif

powered by: WebSVN 2.1.0

© copyright 1999-2019 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.