mpeg.c 8.16 KB
Newer Older
Elliot Lee's avatar
Elliot Lee committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
/* The GIMP -- an image manipulation program
 * Copyright (C) 1995 Spencer Kimball and Peter Mattis
 *
 * Plugin to load MPEG movies.
 *
 * v1.0 - by Adam D. Moss, adam@gimp.org, adam@foxbox.org
 * Requires mpeg_lib by Gregory P. Ward.  See notes below for
 * obtaining and patching mpeg_lib.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

/*******************************************************************
 * USING MPEG_LIB WITH THIS PLUGIN:  mpeg_lib 1.2.1 can be found   *
 * at ftp://ftp.mni.mcgill.ca/pub/mpeg/ - however, mpeg_lib 1.2.x  *
 * contains a bug in end-of-stream reporting, which will cause it  *
 * to crash in conjunction with this plugin.  I enclose a simple   *
 * patch below which fixes the problem (or at least the symptom.;))*
 *******************************************************************/

/*******************************************************************
*** wrapper.c   Tue Oct 10 22:08:39 1995
--- ../mpeg_lib2/wrapper.c      Sat Sep 20 20:29:46 1997
***************
*** 392,394 ****
            "copying from %08X to %08X\n", CurrentImage, Frame);
!    memcpy (Frame, CurrentImage, ImageInfo.Size);
     return (!MovieDone);
--- 392,397 ----
            "copying from %08X to %08X\n", CurrentImage, Frame);
!
!    if (!MovieDone)
!       memcpy (Frame, CurrentImage, ImageInfo.Size);
!
     return (!MovieDone);
*******************************************************************/

/*
 * Changelog:
 *
 * 97/09/21
 * v1.0: Initial release. [Adam]
 */

/*
 * TODO:
 *
 * More robustness for broken streams (how much co-operation
 *   from mpeg_lib?)
 *
 * Saving (eventually...)
 *
 * Crop images properly (e.g. height&~7 ?)
 */


#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include "gtk/gtk.h"
#include "libgimp/gimp.h"
#include "mpeg.h"



static void   query      (void);
static void   run        (char    *name,
                          int      nparams,
                          GParam  *param,
                          int     *nreturn_vals,
                          GParam **return_vals);
static gint32 load_image (char   *filename);


GPlugInInfo PLUG_IN_INFO =
{
  NULL,    /* init_proc */
  NULL,    /* quit_proc */
  query,   /* query_proc */
  run,     /* run_proc */
};


MAIN ()

static void
query ()
{
  static GParamDef load_args[] =
  {
    { PARAM_INT32, "run_mode", "Interactive, non-interactive" },
    { PARAM_STRING, "filename", "The name of the file to load" },
    { PARAM_STRING, "raw_filename", "The name entered" },
  };
  static GParamDef load_return_vals[] =
  {
    { PARAM_IMAGE, "image", "Output image" },
  };
  static int nload_args = sizeof (load_args) / sizeof (load_args[0]);
  static int nload_return_vals = sizeof (load_return_vals) / sizeof (load_return_vals[0]);
  gimp_install_procedure ("file_mpeg_load",
                          "Loads MPEG movies",
                          "FIXME: write help for mpeg_load",
                          "Adam D. Moss",
                          "Adam D. Moss",
                          "1997",
                          "<Load>/MPEG",
			  NULL,
                          PROC_PLUG_IN,
                          nload_args, nload_return_vals,
                          load_args, load_return_vals);
  gimp_register_load_handler ("file_mpeg_load", "mpg,mpeg", "");
}

static void
run (char    *name,
     int      nparams,
     GParam  *param,
     int     *nreturn_vals,
     GParam **return_vals)
{
  static GParam values[2];
  GRunModeType run_mode;
  gint32 image_ID;

  run_mode = param[0].data.d_int32;

  *nreturn_vals = 1;
  *return_vals = values;

  values[0].type = PARAM_STATUS;
  values[0].data.d_status = STATUS_CALLING_ERROR;

  if (strcmp (name, "file_mpeg_load") == 0)
    {
      image_ID = load_image (param[1].data.d_string);
      if (image_ID != -1)
	{
	  *nreturn_vals = 2;
	  values[0].data.d_status = STATUS_SUCCESS;
	  values[1].type = PARAM_IMAGE;
	  values[1].data.d_image = image_ID;
	}
      else
	{
	  values[0].data.d_status = STATUS_EXECUTION_ERROR;
	}
    }
  else
    g_assert (FALSE);
}


static gint
MPEG_frame_period_ms(gint mpeg_rate_code)
{
  switch(mpeg_rate_code)
    {
    case 1: return 44; /* ~23 fps */
    case 2: return 42; /*  24 fps */
    case 3: return 40; /*  25 fps */
    case 4: return 33; /* ~30 fps */
    case 5: return 33; /*  30 fps */
    case 6: return 20; /*  50 fps */
    case 7: return 17; /* ~60 fps */
    case 8: return 17; /*  60 fps */
    case 0: /* ? */
    default:
      printf("mpeg: warning - this MPEG has undefined timing.\n");
      return 0;
    }
}


static gint32
load_image (char *filename)
{
  GPixelRgn pixel_rgn;
  GDrawable *drawable;
  gint32 image_ID;
  gint32 layer_ID;
  gchar *temp;
  Boolean moreframes;
  gint i, framenumber, delay;
  FILE *fp;
  guchar *data;
  gint wwidth, wheight;

  /* mpeg structure */
  ImageDesc img;

  gchar layername[200];  /* FIXME? */


  temp = g_malloc (strlen (filename) + 16);
  if (!temp) gimp_quit ();
  g_free (temp);


  
  gimp_progress_init ("Loading MPEG movie...");

  fp = fopen(filename,"rb");
  if (fp == NULL)
    {
      fprintf (stderr, "mpeg: fopen failed\n");
      exit (4);      
    }

  SetMPEGOption (MPEG_DITHER, FULL_COLOR_DITHER);
  if (!OpenMPEG (fp, &img))
    {
      fprintf (stderr, "mpeg: OpenMPEG failed\n");
      exit (1);
    }

  data = g_malloc(img.Size);

  delay = MPEG_frame_period_ms(img.PictureRate);

  /*
  printf("  <%d : %d>  %dx%d - d%d - bmp%d - ps%d - s%d\n",
	 img.PictureRate, MPEG_frame_period_ms(img.PictureRate),
	 img.Width,img.Height,img.Depth,img.BitmapPad,img.PixelSize,img.Size);
	 */

  if (img.PixelSize != 32)
    {
      printf("mpeg: Hmm, sorry, funny PixelSize (%d) in MPEG.  Aborting.\n",
	     img.PixelSize);
      exit(2);
    }

  if (img.BitmapPad != 32)
    {
      printf("mpeg: Hmm, sorry, funny BitmapPad (%d) in MPEG.  Aborting.\n",
	     img.BitmapPad);
      exit(3);
    }


  wwidth = img.Width;
  wheight = img.Height;

  
  image_ID = gimp_image_new (wwidth, wheight, RGB);
  gimp_image_set_filename (image_ID, filename);


  moreframes = TRUE;
  framenumber = 1;
  while (moreframes)
    {
      gimp_progress_update ((framenumber&1) ? 1.0 : 0.0);

      /* libmpeg - at least the version I have (1.2) - is faulty
         in its reporting of whether there are remaining frames...
	 the sample MPEG it comes with is the only one that it seems
	 able to detect the end of, and even then it doesn't notice
	 until it's too late, so we may lose the last frame, or
	 crash... sigh.

	 A patch to mpeg_lib 1.2.x is included in the header of this
	 plugin.

	 */
279
      moreframes = GetMPEGFrame((char *)data);
Elliot Lee's avatar
Elliot Lee committed
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
      if (!moreframes) break;

      if (delay > 0)
	sprintf(layername, "Frame %d (%dms)",
		framenumber, delay);
      else
	sprintf(layername, "Frame %d",
		framenumber);

      layer_ID = gimp_layer_new (image_ID, layername,
				 wwidth,
				 wheight,
				 RGBA_IMAGE, 100, NORMAL_MODE);
      gimp_image_add_layer (image_ID, layer_ID, 0);
      drawable = gimp_drawable_get (layer_ID);


      /* conveniently for us, mpeg_lib returns pixels padded
	 to 32-bit boundaries (is that true for all archs?).  So we
	 only have to fill in the alpha channel. */
      for (i=(wwidth*wheight)-1; i>=0; i--)
	{
	  data[i*4+3] = 255;
	}


      gimp_pixel_rgn_init (&pixel_rgn, drawable, 0, 0,
			   drawable->width, drawable->height, TRUE, FALSE);
      gimp_pixel_rgn_set_rect (&pixel_rgn, data, 0, 0,
			       wwidth, wheight);
      
      gimp_drawable_flush (drawable);
      gimp_drawable_detach (drawable);

      framenumber++;
    }

  CloseMPEG();
  fclose(fp);
      
  gimp_progress_update (1.0);

  g_free (data);

  return image_ID;

}