home *** CD-ROM | disk | FTP | other *** search
/ Magazyn Amiga Shareware Floppies / ma57.dms / ma57.adf / aMiPEG05 / source.lha / main.c < prev    next >
C/C++ Source or Header  |  1996-01-23  |  9KB  |  387 lines

  1. /*
  2.  *  This is the main part
  3.  */
  4.  
  5. #include "video.h"
  6. #include "proto.h"
  7. #include <sys/types.h>
  8. #include <signal.h>
  9.  
  10. #include "util.h"
  11.  
  12. /* Define buffer length. */
  13.  
  14. #define BUF_LENGTH 65536
  15.  
  16.  
  17. char animname[1024];
  18.  
  19. /* Declaration of global variable to hold dither info. */
  20.  
  21. int ditherType;
  22.  
  23. /* Global file pointer to incoming data. */
  24. FILE *input;
  25.  
  26. /* End of File flag. */
  27. static int EOF_flag = 0;
  28.  
  29. /* Loop flag. */
  30. int loopFlag = 0;
  31.  
  32. /* Quiet flag. */
  33. int quietFlag = 0;
  34.  
  35. /* Display image on screen? */
  36. int noDisplayFlag = 0;
  37.  
  38. /* Setjmp/Longjmp env. */
  39. jmp_buf env;
  40.  
  41. /* HAM6 rendering / HAM hires flag */
  42. extern int ham6, lores;
  43.  
  44. /* modeid of CyberGraphX screen */
  45. unsigned long modeid = 0xffffffff;
  46.  
  47. /* Method of picture conversion */
  48. void (*DoDitherImage)(unsigned char *l, unsigned char *Cr, unsigned char *Cb,
  49.               unsigned char *disp, int h, int w);
  50.  
  51. static char version[]="$VER: aMiPEG 0.5 (compiled on " __DATE__ ", " __TIME__ ")\n";
  52.  
  53. /*
  54.  *--------------------------------------------------------------
  55.  *
  56.  * get_more_data --
  57.  *
  58.  *    Called by correct_underflow in bit parsing utilities to
  59.  *      read in more data.
  60.  *
  61.  * Results:
  62.  *    Input buffer updated, buffer length updated.
  63.  *      Returns 1 if data read, 0 if EOF, -1 if error.
  64.  *
  65.  * Side effects:
  66.  *      None.
  67.  *
  68.  *--------------------------------------------------------------
  69.  */
  70.  
  71. int get_more_data(unsigned int *buf_start, int max_length, int *length_ptr, unsigned int **buf_ptr)
  72. {
  73.   
  74.   int length, num_read, request;
  75.   unsigned char *buffer, *mark;
  76.  
  77.   if (EOF_flag) return 0;
  78.  
  79.   length = *length_ptr;
  80.   buffer = (unsigned char *) *buf_ptr;
  81.  
  82.   if (length > 0) {
  83.     memcpy((unsigned char *) buf_start, buffer, (length*4));
  84.     mark = ((unsigned char *) (buf_start + length));
  85.   }
  86.   else {
  87.     mark = (unsigned char *) buf_start;
  88.     length = 0;
  89.   }
  90.  
  91.   request = (max_length-length)*4;
  92.   
  93.   num_read = fread( mark, 1, request, input);
  94.  
  95.   /* Paulo Villegas - 26/1/1993: Correction for 4-byte alignment */
  96.   {
  97.     int num_read_rounded;
  98.     unsigned char *index;
  99.  
  100.     num_read_rounded = num_read & 0xfffffffc;
  101.  
  102.     /* this can happen only if num_read<request; i.e. end of file reached */
  103.     if( num_read_rounded < num_read )
  104.       { 
  105.      num_read_rounded+=4;
  106.      /* fill in with zeros */
  107.      for( index=mark+num_read; index<mark+num_read_rounded; *(index++)=0 );
  108.      /* advance to the next 4-byte boundary */
  109.      num_read = num_read_rounded;
  110.       }
  111.   }
  112.   
  113.   if (num_read < 0) {
  114.     return -1;
  115.   }
  116.   else if (num_read == 0) {
  117.     *buf_ptr = buf_start;
  118.     
  119.     /* Make 32 bits after end equal to 0 and 32
  120.        bits after that equal to seq end code
  121.        in order to prevent messy data from infinite
  122.        recursion.
  123.     */
  124.  
  125.     *(buf_start + length) = 0x0;
  126.     *(buf_start + length+1) = SEQ_END_CODE;
  127.  
  128.     EOF_flag = 1;
  129.     return 0;
  130.   }
  131.  
  132.   num_read >>= 2;
  133.  
  134.   *buf_ptr = buf_start;
  135.   *length_ptr = length + num_read;
  136.  
  137.   return 1;
  138. }
  139.  
  140. /*
  141.  *--------------------------------------------------------------
  142.  *
  143.  * int_handler --
  144.  *
  145.  *    Handles Cntl-C interupts..
  146.  *
  147.  * Results:
  148.  *    None.
  149.  *
  150.  * Side effects:
  151.  *    None.
  152.  *
  153.  *--------------------------------------------------------------
  154.  */
  155. void int_handler(int dummy)
  156. {
  157.     if (!quietFlag) fprintf(stderr, "Interrupted!\n");
  158.     if (curVidStream) DestroyVidStream(curVidStream);
  159.     exit(1);
  160. }
  161.  
  162.  
  163. /*
  164.  *--------------------------------------------------------------
  165.  *
  166.  * main --
  167.  *
  168.  *    Parses command line, starts decoding and displaying.
  169.  *
  170.  * Results:
  171.  *    None.
  172.  *
  173.  * Side effects:
  174.  *    None.
  175.  *
  176.  *--------------------------------------------------------------
  177.  */
  178.  
  179. void main(int argc, char **argv)
  180. {
  181.  
  182.   char *name;
  183.   static VidStream *theStream;
  184.   int mark;
  185.   extern int lores;
  186.  
  187.   mark = 1;
  188.   argc--;
  189.  
  190.   name = "";
  191.   input = stdin;
  192.   ditherType = FULL_COLOR_DITHER;
  193.   noDisplayFlag = 0;
  194.  
  195.   while (argc) {
  196.     if (strcmp(argv[mark], "-nop") == 0) {
  197.       TogglePFlag();
  198.       argc--; mark++;
  199.     } else if (strcmp(argv[mark], "-nob") == 0) {
  200.       ToggleBFlag();
  201.       argc--; mark++;
  202.     } else if (strcmp(argv[mark], "-display") == 0) {
  203.       name = argv[++mark];
  204.       argc -= 2; mark++;
  205.     } else if (strcmp(argv[mark], "-dither") == 0) {
  206.       argc--; mark++;
  207.       if (argc < 1) {
  208.     perror("Must specify dither option after -dither flag");
  209.     usage(argv[0]);
  210.       }
  211.       if (strcmp(argv[mark], "gray") == 0) {
  212.     argc--; mark++;
  213.     ditherType = GRAY_DITHER;
  214.       } else if (strcmp(argv[mark], "color") == 0) {
  215.     argc--; mark++;
  216.     ditherType = FULL_COLOR_DITHER;
  217.       } else if (strcmp(argv[mark], "hiresham") == 0) {
  218.     argc--; mark++;
  219.     ditherType = FULL_COLOR_DITHER;
  220.     lores = FALSE;
  221.       } else if (strcmp(argv[mark], "ham6") == 0) {
  222.     argc--; mark++;
  223.     ditherType = FULL_COLOR_DITHER;
  224.     ham6 = TRUE;
  225.       } else if (strcmp(argv[mark], "cybergfx") == 0) {
  226.     argc--; mark++;
  227.     ditherType = CYBERGFX_DITHER;
  228.             } else if (strcmp(argv[mark], "cybergfxgray") == 0) {
  229.     argc--; mark++;
  230.     ditherType = CYBERGFXGRAY_DITHER;
  231.             } else if (strcmp(argv[mark], "none") == 0) {
  232.     argc--; mark++;
  233.     ditherType = NO_DITHER;
  234.       } else {
  235.     perror("Illegal dither option.");
  236.     usage(argv[0]);
  237.       }
  238.     } 
  239.     else if (strcmp(argv[mark], "-eachstat") == 0) {
  240.       argc--; mark++;
  241. #ifdef ANALYSIS
  242.       showEachFlag = 1;
  243. #else
  244.       fprintf(stderr, "To use -eachstat, recompile with -DANALYSIS in CFLAGS\n");
  245.       exit(1);
  246. #endif
  247.     }
  248.     else if (strcmp(argv[mark], "-quiet") == 0) {
  249.       argc--; mark++;
  250.       quietFlag = 1;
  251.     }
  252.     else if (strcmp(argv[mark], "-loop") == 0) {
  253.       argc--; mark++;
  254.       loopFlag = 1;
  255.     }
  256.     else if (strcmp(argv[mark], "-no_display") == 0) {
  257.       argc--; mark++;
  258.       noDisplayFlag = 1;
  259.     }
  260.     else if (strcmp(argv[mark], "-modeid") == 0) {
  261.       argc--; mark++;
  262.       if(sscanf(argv[mark], "%x", &modeid) != 1)
  263.       {
  264.           fprintf(stderr, "ModeID not given.\n",argv[mark]);
  265.                 usage(argv[0]);
  266.             }
  267.       argc--; mark++;
  268.     }
  269.     else if (argv[mark][0] == '-') {
  270.       fprintf(stderr, "Unrecognized flag %s\n",argv[mark]);
  271.       usage(argv[0]);
  272.     }
  273.     else {
  274.       input = fopen(argv[mark], "r");
  275.       if (input == NULL) {
  276.     fprintf(stderr, "Could not open file %s\n", argv[mark]);
  277.     usage(argv[0]);
  278.       }
  279.       strcpy(animname, argv[mark]);
  280.       argc--; mark++;
  281.     }
  282.   }
  283.  
  284.   signal(SIGINT, int_handler);
  285.  
  286.   init_tables();
  287.   
  288.   switch (ditherType) {
  289.     case CYBERGFX_DITHER:
  290.         InitColorDither();
  291.         modeid = InitCyberGfxDisplay(modeid);
  292.         HAM8_draw = (void (*)(void *, int, int)) DrawCyberGfxImage;
  293.         DoDitherImage = ColorDitherImage_RGB;
  294.         break;
  295.  
  296.     case CYBERGFXGRAY_DITHER:
  297.         InitColorDither();
  298.         modeid = InitCyberGfxDisplay(modeid);
  299.         HAM8_draw = (void (*)(void *, int, int)) DrawCyberGfxImage;
  300.         DoDitherImage = NoDitherImage;
  301.         break;
  302.  
  303.     case GRAY_DITHER:
  304.         InitGrayDisplay(name);
  305.         break;
  306.  
  307.     case FULL_COLOR_DITHER:
  308.         InitColorDither();
  309.         InitColorDisplay(name);
  310.         break;
  311.  
  312.     case NO_DITHER:
  313.         HAM8_draw = (void (*)(void *, int, int)) NoDitherImage;    // method casting ... argh!
  314.         DoDitherImage = NoDitherImage;
  315.         break;
  316.   }
  317.  
  318. /*
  319.  *  The new restart handling has not been checked out very closely with changing
  320.  *  (non)intra scale matrices!
  321.  */
  322.  
  323.   theStream = NewVidStream(BUF_LENGTH);
  324.  
  325.   if (setjmp(env) != 0) {
  326.     mpegInitVidRsrc(); /* fix bug in static first in mpegVidRsrc */
  327.  
  328.     rewind(input);
  329.  
  330.     EOF_flag = 0;
  331.     theStream->bit_offset = 0;
  332.     theStream->buf_length = 0;
  333.     theStream->buffer = NULL;
  334.     totNumFrames = 0;
  335. #ifdef ANALYSIS 
  336.     init_stats();
  337. #endif
  338.   }
  339.  
  340.   realTimeStart = ReadSysClock();
  341.   while (mpegVidRsrc(0, theStream));
  342.  
  343. }
  344.  
  345.  
  346. /*
  347.  *--------------------------------------------------------------
  348.  *
  349.  * usage --
  350.  *
  351.  *    Print mpeg_play usage
  352.  *
  353.  * Results:
  354.  *    None.
  355.  *
  356.  * Side effects:
  357.  *    exits with a return value -1
  358.  *
  359.  *--------------------------------------------------------------
  360.  */
  361.  
  362. void usage(char *s)  /* program name */
  363. {
  364.     fprintf(stderr, "Usage:\n");
  365.     fprintf(stderr, "mpeg_play\n");
  366.     fprintf(stderr, "          [-nob]\n");
  367.     fprintf(stderr, "          [-nop]\n");
  368.     fprintf(stderr, "          [-dither {gray|color|ham6|hiresham|cybergfx|cybergfxmono|none}]\n");
  369.     fprintf(stderr, "          [-modeid nnnnnnnn]\n");
  370.     fprintf(stderr, "          [-loop]\n");
  371.     fprintf(stderr, "          [-eachstat]\n");
  372.     fprintf(stderr, "          [-no_display]\n");
  373.     fprintf(stderr, "          [-quiet]\n");
  374.     fprintf(stderr, "          file_name\n");
  375.     exit (-1);
  376. }
  377.  
  378.  
  379. /*
  380.  *  Dummy display method
  381.  *
  382.  */
  383. void NoDitherImage(unsigned char *l, unsigned char *Cr, unsigned char *Cb,
  384.            unsigned char *disp, int h, int w)
  385. {}
  386.  
  387.