home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 9 / CD_ASCQ_09_1193.iso / news / 4441 / mpegcode / misc / playskip.c < prev    next >
C/C++ Source or Header  |  1993-01-01  |  6KB  |  266 lines

  1. /*
  2.  * Copyright (c) 1993 The Regents of the University of California.
  3.  * All rights reserved.
  4.  *
  5.  * Permission to use, copy, modify, and distribute this software and its
  6.  * documentation for any purpose, without fee, and without written agreement is
  7.  * hereby granted, provided that the above copyright notice and the following
  8.  * two paragraphs appear in all copies of this software.
  9.  *
  10.  * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
  11.  * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
  12.  * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13.  * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14.  *
  15.  * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16.  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17.  * AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18.  * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19.  * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20.  */
  21.  
  22. /* gcc -o playskip playskip.c -lX11 -lXvid -I/n/picasso/project/mm/xvideo/include
  23.  */
  24. #include <stdio.h>
  25. #include <X11/Xlib.h>
  26. #include <X11/Xutil.h>
  27. #include <XPlxExt.h>
  28.  
  29. usage (p)
  30. char *p;
  31.  
  32. {
  33.     fprintf (stderr, "Usage: %s filename width height start nth outbase [quality]\n", p);
  34.     fprintf (stderr, "(captures every nth frame)\n");
  35.     exit (1);
  36. }
  37.  
  38. static char buffer[300000];
  39.  
  40. Visual *
  41. FindFullColorVisual (dpy, depth)
  42.     Display *dpy;
  43.     int *depth;
  44. {
  45.   XVisualInfo vinfo;
  46.   XVisualInfo *vinfo_ret;
  47.   int numitems, maxdepth;
  48.   
  49.   vinfo.class = TrueColor;
  50.   
  51.   vinfo_ret = XGetVisualInfo(dpy, VisualClassMask, &vinfo, &numitems);
  52.   
  53.   if (numitems == 0) return NULL;
  54.  
  55.   maxdepth = 0;
  56.   while(numitems > 0) {
  57.     if (vinfo_ret[numitems-1].depth > maxdepth) {
  58.       maxdepth = vinfo_ret[numitems-1 ].depth;
  59.     }
  60.     numitems--;
  61.   }
  62.   XFree(vinfo_ret);
  63.  
  64.   if (maxdepth < 24) return NULL;
  65.  
  66.   if (XMatchVisualInfo(dpy, DefaultScreen(dpy), maxdepth, 
  67.                TrueColor, &vinfo)) {
  68.     *depth = maxdepth;
  69.     return vinfo.visual;
  70.   }
  71.   
  72.   return NULL;
  73. }
  74.  
  75. Window
  76. CreateFullColorWindow (dpy, x, y, w, h)
  77.     Display *dpy;
  78.     int x, y, w, h;
  79. {
  80.     int depth;
  81.     Visual *visual;
  82.     XSetWindowAttributes xswa;
  83.     unsigned int mask;
  84.     unsigned int class;
  85.     int screen;
  86.  
  87.     screen = XDefaultScreen(dpy);
  88.     class = InputOutput;    /* Could be InputOnly */
  89.     visual = FindFullColorVisual (dpy, &depth);
  90.     if (visual == NULL) {
  91.     return 0;
  92.     }
  93.     mask = CWBackPixel | CWColormap | CWBorderPixel;
  94.     xswa.colormap = XCreateColormap(dpy, XRootWindow(dpy, screen),
  95.             visual, AllocNone);
  96.     xswa.background_pixel = BlackPixel(dpy, DefaultScreen(dpy));
  97.     xswa.border_pixel = WhitePixel(dpy, DefaultScreen(dpy));
  98.  
  99.     return XCreateWindow(dpy, RootWindow(dpy, screen), x, y, w, h,
  100.     1, depth, class, visual, mask, &xswa);
  101. }
  102.  
  103. main (argc, argv)
  104. int argc;
  105. char **argv;
  106.  
  107. {
  108.   char *filename;
  109.   Display *dpy;
  110.   int screen;
  111.   Window root;
  112.   XEvent event;
  113.   GC gc;
  114.   Window win;
  115.   XPlxCImage image;
  116.   int size;
  117.   char *qTable;
  118.   FILE *inFile;
  119.   FILE *outFile;
  120.   extern char *malloc();
  121.   int fd, r1, r2, i, j, r;
  122.   int start = 1;
  123.   int end = 99999;
  124.   int quality;
  125.   XImage *ximage;
  126.   char *tdata;
  127.   char *obase;
  128.   char ofname[256];
  129.   int height, width;
  130.   char command[256];
  131.   int nth;
  132.  
  133.   if ((argc != 7) && (argc != 8))usage (argv[0]);
  134.   filename = argv[1];
  135.  
  136.   width = atoi(argv[2]);
  137.   height = atoi(argv[3]);
  138.  
  139.   start = atoi(argv[4]);
  140.   nth = atoi(argv[5]);
  141.  
  142.   if ((start < 1) || (end < start)) {
  143.     perror ("Bad start and end values.");
  144.     exit();
  145.   }
  146.  
  147.   obase = argv[6];
  148.  
  149.   quality = 100;
  150.   
  151.   if (argc > 7)
  152.     quality = atoi (argv[7]);
  153.   
  154.   dpy = XOpenDisplay (NULL);
  155.   screen = DefaultScreen(dpy);
  156.   root = DefaultRootWindow(dpy);
  157. /*  gc = DefaultGC(dpy, screen); */
  158. /*  win = XCreateSimpleWindow (dpy, root, 0, 0, width, height,
  159.                  0, NULL, NULL);
  160. */
  161.   win = CreateFullColorWindow(dpy, 0, 0, width+4, height+4);
  162.   gc = XCreateGC(dpy, win, 0, NULL);
  163.  
  164.   if (!win ) {
  165.     perror ("Unable to create window");
  166.     exit(1);
  167.   }
  168.  
  169.   XMapWindow (dpy, win);
  170.   XSelectInput (dpy, win, ExposureMask |ButtonPressMask);
  171.  
  172.   size = MakeQTables(quality, &qTable);
  173.   XPlxPutTable(dpy, win, gc, qTable, size, 0);
  174.   XPlxPutTable(dpy, win, gc, qTable, size, 1);
  175.   XPlxVideoTag (dpy, win, gc, PLX_VIDEO);
  176.   
  177.   inFile = fopen(filename, "r");
  178.   if (inFile == NULL) {
  179.     perror (filename);
  180.     exit (1);
  181.   }
  182.   fd = fileno(inFile);
  183.   wait(2);
  184.  
  185.   for (i=0; i<start; i++) {
  186.     if (read (fd, &image, sizeof(image)) != sizeof(image)) {
  187.       perror("End of file.");
  188.       exit();
  189.     }
  190.     image.data = buffer;
  191.     if (read (fd, buffer, image.size) != image.size) {
  192.       perror("End of file.");
  193.       exit();
  194.     }
  195.   }
  196.     
  197.   for (i=start; i<=end; i += nth) {
  198.     fprintf(stdout, "GRABBING FRAME %d\n", i);
  199.  
  200.     if (read (fd, &image, sizeof(image)) != sizeof(image)) {
  201.       perror("End of file.");
  202.       exit();
  203.     }
  204.     image.data = buffer;
  205.     if (read (fd, buffer, image.size) != image.size) {
  206.       perror("End of file.");
  207.       exit();
  208.     }
  209.     
  210.     XPlxPutCImage (dpy, win, gc, &image, 0, 0, image.width,
  211.            image.height, 0, 0, width+2, height+2, 1);
  212.  
  213.     XFlush(dpy);
  214.  
  215.     ximage = XGetImage(dpy, win, 0, 0, width, height, 0x00ffffff,
  216.                ZPixmap);
  217.     
  218.     if (i == 0) {
  219.       fprintf(stderr, "Depth %d\n", ximage->depth);
  220.       fprintf(stderr, "Height: %d Width: %d\n", height, width );
  221.     }
  222.     tdata = ximage->data;
  223.  
  224.  
  225.     sprintf(ofname, "%s%d.ppm", obase, i);
  226.     outFile = fopen("/tmp/foobar", "w");
  227.     if (!outFile) {
  228.       perror("Couldn't open output file.");
  229.     }
  230.  
  231.     for (r=0; r<height; r++) {
  232.       for (j=0; j<width; j++) {
  233.     fputc(*(tdata+3), outFile);
  234.     fputc(*(tdata+2), outFile);
  235.     fputc(*(tdata+1), outFile);
  236.     tdata += 4;
  237.       }
  238.     }
  239.  
  240.     fclose(outFile);
  241.  
  242.     free(tdata);
  243.  
  244.     sprintf(command, "rawtoppm %d %d < /tmp/foobar > %s",
  245.         width, height, ofname);
  246.     system(command);
  247.  
  248.       for (j=0; j<nth-1; j++) {
  249.     if (read (fd, &image, sizeof(image)) != sizeof(image)) {
  250.       perror("End of file.");
  251.       exit();
  252.     }
  253.     image.data = buffer;
  254.     if (read (fd, buffer, image.size) != image.size) {
  255.       perror("End of file.");
  256.       exit();
  257.     }
  258.       }
  259.   }
  260. }
  261.  
  262.  
  263.  
  264.  
  265.  
  266.