home *** CD-ROM | disk | FTP | other *** search
/ PC Pro 2002 April / pcpro0402.iso / essentials / graphics / Gimp / gimp-src-20001226.exe / src / gimp / plug-ins / common / mpeg.c < prev    next >
Encoding:
C/C++ Source or Header  |  2000-08-24  |  9.3 KB  |  351 lines

  1. /* The GIMP -- an image manipulation program
  2.  * Copyright (C) 1995 Spencer Kimball and Peter Mattis
  3.  *
  4.  * Plugin to load MPEG movies. (C) 1997-99 Adam D. Moss
  5.  *
  6.  * v1.1 - by Adam D. Moss, adam@gimp.org, adam@foxbox.org
  7.  * Requires mpeg_lib by Gregory P. Ward.  See notes below for
  8.  * obtaining and patching mpeg_lib.
  9.  *
  10.  * This program is free software; you can redistribute it and/or modify
  11.  * it under the terms of the GNU General Public License as published by
  12.  * the Free Software Foundation; either version 2 of the License, or
  13.  * (at your option) any later version.
  14.  *
  15.  * This program is distributed in the hope that it will be useful,
  16.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.  * GNU General Public License for more details.
  19.  *
  20.  * You should have received a copy of the GNU General Public License
  21.  * along with this program; if not, write to the Free Software
  22.  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  23.  */
  24.  
  25. /*******************************************************************
  26.  * USING MPEG_LIB WITH THIS PLUGIN:  mpeg_lib 1.2.1 can be found   *
  27.  * at ftp://ftp.mni.mcgill.ca/pub/mpeg/ - however, mpeg_lib 1.2.x  *
  28.  * contains a bug in end-of-stream reporting, which will cause it  *
  29.  * to crash in conjunction with this plugin.  I enclose a simple   *
  30.  * patch below which fixes the problem (or at least the symptom.;))*
  31.  *******************************************************************
  32.  *    Addendum: mpeg_lib 1.3.0 is now released and much better!    *
  33.  *******************************************************************/
  34.  
  35. /*******************************************************************
  36. *** wrapper.c   Tue Oct 10 22:08:39 1995
  37. --- ../mpeg_lib2/wrapper.c      Sat Sep 20 20:29:46 1997
  38. ***************
  39. *** 392,394 ****
  40.             "copying from %08X to %08X\n", CurrentImage, Frame);
  41. !    memcpy (Frame, CurrentImage, ImageInfo.Size);
  42.      return (!MovieDone);
  43. --- 392,397 ----
  44.             "copying from %08X to %08X\n", CurrentImage, Frame);
  45. !
  46. !    if (!MovieDone)
  47. !       memcpy (Frame, CurrentImage, ImageInfo.Size);
  48. !
  49.      return (!MovieDone);
  50. *******************************************************************/
  51.  
  52. /*
  53.  * Changelog:
  54.  *
  55.  * 99/05/31
  56.  * v1.1: Endianness fix.
  57.  *
  58.  * 97/09/21
  59.  * v1.0: Initial release. [Adam]
  60.  */
  61.  
  62. /*
  63.  * TODO:
  64.  *
  65.  * More robustness for broken streams (how much co-operation
  66.  *   from mpeg_lib?)
  67.  *
  68.  * Saving (eventually...)
  69.  *
  70.  * Crop images properly (e.g. height&~7 ?)
  71.  */
  72.  
  73. #include "config.h"
  74.  
  75. #include <stdlib.h>
  76. #include <stdio.h>
  77. #include <errno.h>
  78.  
  79. #include <gtk/gtk.h>
  80.  
  81. #include <libgimp/gimp.h>
  82.  
  83. #include "mpeg.h"
  84.  
  85. #include "libgimp/stdplugins-intl.h"
  86.  
  87.  
  88. static void   query      (void);
  89. static void   run        (gchar   *name,
  90.                           gint     nparams,
  91.                           GimpParam  *param,
  92.                           gint    *nreturn_vals,
  93.                           GimpParam **return_vals);
  94. static gint32 load_image (char   *filename);
  95.  
  96.  
  97. GimpPlugInInfo PLUG_IN_INFO =
  98. {
  99.   NULL,  /* init_proc  */
  100.   NULL,  /* quit_proc  */
  101.   query, /* query_proc */
  102.   run,   /* run_proc   */
  103. };
  104.  
  105.  
  106. MAIN ()
  107.  
  108. static void
  109. query (void)
  110. {
  111.   static GimpParamDef load_args[] =
  112.   {
  113.     { GIMP_PDB_INT32, "run_mode", "Interactive, non-interactive" },
  114.     { GIMP_PDB_STRING, "filename", "The name of the file to load" },
  115.     { GIMP_PDB_STRING, "raw_filename", "The name entered" }
  116.   };
  117.   static GimpParamDef load_return_vals[] =
  118.   {
  119.     { GIMP_PDB_IMAGE, "image", "Output image" }
  120.   };
  121.   static gint nload_args = sizeof (load_args) / sizeof (load_args[0]);
  122.   static gint nload_return_vals = (sizeof (load_return_vals) /
  123.                    sizeof (load_return_vals[0]));
  124.  
  125.   gimp_install_procedure ("file_mpeg_load",
  126.                           "Loads MPEG movies",
  127.                           "FIXME: write help for mpeg_load",
  128.                           "Adam D. Moss",
  129.                           "Adam D. Moss",
  130.                           "1997",
  131.                           "<Load>/MPEG",
  132.               NULL,
  133.                           GIMP_PLUGIN,
  134.                           nload_args, nload_return_vals,
  135.                           load_args, load_return_vals);
  136.  
  137.   gimp_register_magic_load_handler ("file_mpeg_load",
  138.                     "mpg,mpeg",
  139.                     "",
  140.                     "0,long,0x000001b3,0,long,0x000001ba");
  141. }
  142.  
  143. static void
  144. run (gchar   *name,
  145.      gint     nparams,
  146.      GimpParam  *param,
  147.      gint    *nreturn_vals,
  148.      GimpParam **return_vals)
  149. {
  150.   static GimpParam values[2];
  151.   GimpRunModeType  run_mode;
  152.   GimpPDBStatusType   status = GIMP_PDB_SUCCESS;
  153.   gint32 image_ID;
  154.  
  155.   run_mode = param[0].data.d_int32;
  156.  
  157.   *nreturn_vals = 1;
  158.   *return_vals  = values;
  159.   values[0].type          = GIMP_PDB_STATUS;
  160.   values[0].data.d_status = GIMP_PDB_EXECUTION_ERROR;
  161.  
  162.   INIT_I18N();
  163.  
  164.   if (strcmp (name, "file_mpeg_load") == 0)
  165.     {
  166.       image_ID = load_image (param[1].data.d_string);
  167.  
  168.       if (image_ID != -1)
  169.     {
  170.       *nreturn_vals = 2;
  171.       values[1].type         = GIMP_PDB_IMAGE;
  172.       values[1].data.d_image = image_ID;
  173.     }
  174.       else
  175.     {
  176.       status = GIMP_PDB_EXECUTION_ERROR;
  177.     }
  178.     }
  179.   else
  180.     {
  181.       status = GIMP_PDB_CALLING_ERROR;
  182.     }
  183.  
  184.   values[0].data.d_status = status;
  185. }
  186.  
  187.  
  188. static gint
  189. MPEG_frame_period_ms (gint mpeg_rate_code)
  190. {
  191.   switch(mpeg_rate_code)
  192.     {
  193.     case 1: return 44; /* ~23 fps */
  194.     case 2: return 42; /*  24 fps */
  195.     case 3: return 40; /*  25 fps */
  196.     case 4: return 33; /* ~30 fps */
  197.     case 5: return 33; /*  30 fps */
  198.     case 6: return 20; /*  50 fps */
  199.     case 7: return 17; /* ~60 fps */
  200.     case 8: return 17; /*  60 fps */
  201.     case 0: /* ? */
  202.     default:
  203.       printf("mpeg: warning - this MPEG has undefined timing.\n");
  204.       return 0;
  205.     }
  206. }
  207.  
  208.  
  209. static gint32
  210. load_image (gchar *filename)
  211. {
  212.   GimpPixelRgn pixel_rgn;
  213.   GimpDrawable *drawable;
  214.   gint32 image_ID;
  215.   gint32 layer_ID;
  216.   gchar *temp;
  217.   Boolean moreframes;
  218.   gint i, framenumber, delay;
  219.   FILE *fp;
  220.   guchar *data;
  221.   gint wwidth, wheight;
  222.  
  223.   /* mpeg structure */
  224.   ImageDesc img;
  225.  
  226.   gchar *layername;  /* FIXME? */
  227.  
  228.   temp = g_malloc (strlen (filename) + 16);
  229.   if (!temp) gimp_quit ();
  230.   g_free (temp);
  231.   
  232.   gimp_progress_init ( _("Loading MPEG movie..."));
  233.  
  234.   fp = fopen(filename,"rb");
  235.   if (fp == NULL)
  236.     {
  237.       fprintf (stderr, "mpeg: fopen failed\n");
  238.       exit (4);      
  239.     }
  240.  
  241.   SetMPEGOption (MPEG_DITHER, FULL_COLOR_DITHER);
  242.   if (!OpenMPEG (fp, &img))
  243.     {
  244.       fprintf (stderr, "mpeg: OpenMPEG failed\n");
  245.       exit (1);
  246.     }
  247.  
  248.   data = g_malloc(img.Size);
  249.  
  250.   delay = MPEG_frame_period_ms(img.PictureRate);
  251.  
  252.   /*
  253.   printf("  <%d : %d>  %dx%d - d%d - bmp%d - ps%d - s%d\n",
  254.      img.PictureRate, MPEG_frame_period_ms(img.PictureRate),
  255.      img.Width,img.Height,img.Depth,img.BitmapPad,img.PixelSize,img.Size);
  256.      */
  257.  
  258.   if (img.PixelSize != 32)
  259.     {
  260.       printf("mpeg: Hmm, sorry, funny PixelSize (%d) in MPEG.  Aborting.\n",
  261.          img.PixelSize);
  262.       exit(2);
  263.     }
  264.  
  265.   if (img.BitmapPad != 32)
  266.     {
  267.       printf("mpeg: Hmm, sorry, funny BitmapPad (%d) in MPEG.  Aborting.\n",
  268.          img.BitmapPad);
  269.       exit(3);
  270.     }
  271.  
  272.   wwidth = img.Width;
  273.   wheight = img.Height;
  274.   
  275.   image_ID = gimp_image_new (wwidth, wheight, GIMP_RGB);
  276.   gimp_image_set_filename (image_ID, filename);
  277.  
  278.   moreframes = TRUE;
  279.   framenumber = 1;
  280.   while (moreframes)
  281.     {
  282.       gimp_progress_update ((framenumber&1) ? 1.0 : 0.0);
  283.  
  284.       /* libmpeg - at least the version I have (1.2) - is faulty
  285.          in its reporting of whether there are remaining frames...
  286.      the sample MPEG it comes with is the only one that it seems
  287.      able to detect the end of, and even then it doesn't notice
  288.      until it's too late, so we may lose the last frame, or
  289.      crash... sigh.
  290.  
  291.      A patch to mpeg_lib 1.2.x is included in the header of this
  292.      plugin.
  293.  
  294.      */
  295.       moreframes = GetMPEGFrame((char *)data);
  296.       if (!moreframes) break;
  297.  
  298.       if (delay > 0)
  299.     layername = g_strdup_printf( _("Frame %d (%dms)"),
  300.         framenumber, delay);
  301.       else
  302.     layername = g_strdup_printf( _("Frame %d"),
  303.         framenumber);
  304.  
  305.       layer_ID = gimp_layer_new (image_ID, layername,
  306.                  wwidth,
  307.                  wheight,
  308.                  GIMP_RGBA_IMAGE, 100, GIMP_NORMAL_MODE);
  309.       g_free(layername);
  310.       gimp_image_add_layer (image_ID, layer_ID, 0);
  311.       drawable = gimp_drawable_get (layer_ID);
  312.  
  313.  
  314.       /* conveniently for us, mpeg_lib returns pixels padded
  315.      to 32-bit boundaries (is that true for all archs?).  So we
  316.      only have to fill in the alpha channel. */
  317.       for (i=(wwidth*wheight)-1; i>=0; i--)
  318.     {
  319. #if G_BYTE_ORDER == G_BIG_ENDIAN
  320.           unsigned char r,g,b;
  321.           r = data[i*4+3 ];
  322.           g = data[i*4+2 ];
  323.           b = data[i*4+1 ];
  324.           data[i*4+2]= b;
  325.           data[i*4+1]= g;
  326.           data[i*4 ] = r;
  327. #endif
  328.           data[i*4+3] = 255;
  329.     }
  330.  
  331.       gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0,
  332.                drawable->width, drawable->height, TRUE, FALSE);
  333.       gimp_pixel_rgn_set_rect (&pixel_rgn, data, 0, 0,
  334.                    wwidth, wheight);
  335.       
  336.       gimp_drawable_flush (drawable);
  337.       gimp_drawable_detach (drawable);
  338.  
  339.       framenumber++;
  340.     }
  341.  
  342.   CloseMPEG();
  343.   fclose(fp);
  344.       
  345.   gimp_progress_update (1.0);
  346.  
  347.   g_free (data);
  348.  
  349.   return image_ID;
  350. }
  351.