2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % M M PPPP EEEEE GGGG %
8 % M M M PPPP EEE G GG %
13 % Read/Write MPEG Image Format %
20 % Copyright 1999-2009 ImageMagick Studio LLC, a non-profit organization %
21 % dedicated to making software imaging solutions freely available. %
23 % You may not use this file except in compliance with the License. You may %
24 % obtain a copy of the License at %
26 % http://www.imagemagick.org/script/license.php %
28 % Unless required by applicable law or agreed to in writing, software %
29 % distributed under the License is distributed on an "AS IS" BASIS, %
30 % WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. %
31 % See the License for the specific language governing permissions and %
32 % limitations under the License. %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
41 #include "magick/studio.h"
42 #include "magick/blob.h"
43 #include "magick/blob-private.h"
44 #include "magick/constitute.h"
45 #include "magick/delegate.h"
46 #include "magick/exception.h"
47 #include "magick/exception-private.h"
48 #include "magick/geometry.h"
49 #include "magick/image.h"
50 #include "magick/image-private.h"
51 #include "magick/layer.h"
52 #include "magick/list.h"
53 #include "magick/log.h"
54 #include "magick/magick.h"
55 #include "magick/memory_.h"
56 #include "magick/resource_.h"
57 #include "magick/quantum-private.h"
58 #include "magick/static.h"
59 #include "magick/string_.h"
60 #include "magick/module.h"
61 #include "magick/transform.h"
62 #include "magick/utility.h"
67 static MagickBooleanType
68 WriteMPEGImage(const ImageInfo *image_info,Image *image);
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 % IsMPEG() returns MagickTrue if the image format type, identified by the
82 % magick string, is MPEG.
84 % The format of the IsMPEG method is:
86 % MagickBooleanType IsMPEG(const unsigned char *magick,const size_t length)
88 % A description of each parameter follows:
90 % o magick: compare image format pattern against these bytes.
92 % o length: Specifies the length of the magick string.
95 static MagickBooleanType IsMPEG(const unsigned char *magick,const size_t length)
99 if (memcmp(magick,"\000\000\001\263",4) == 0)
105 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
109 % R e a d M P E G I m a g e %
113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115 % ReadMPEGImage() reads an binary file in the MPEG video stream format
116 % and returns it. It allocates the memory necessary for the new Image
117 % structure and returns a pointer to the new image.
119 % The format of the ReadMPEGImage method is:
121 % Image *ReadMPEGImage(const ImageInfo *image_info,
122 % ExceptionInfo *exception)
124 % A description of each parameter follows:
126 % o image_info: the image info.
128 % o exception: return any errors or warnings in this structure.
131 static Image *ReadMPEGImage(const ImageInfo *image_info,
132 ExceptionInfo *exception)
134 #define ReadMPEGIntermediateFormat "pam"
149 assert(image_info != (const ImageInfo *) NULL);
150 assert(image_info->signature == MagickSignature);
151 if (image_info->debug != MagickFalse)
152 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
153 image_info->filename);
154 assert(exception != (ExceptionInfo *) NULL);
155 assert(exception->signature == MagickSignature);
156 image=AcquireImage(image_info);
157 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
158 if (status == MagickFalse)
160 image=DestroyImageList(image);
161 return((Image *) NULL);
163 (void) CloseBlob(image);
164 (void) DestroyImageList(image);
166 Convert MPEG to PAM with delegate.
168 read_info=CloneImageInfo(image_info);
169 image=AcquireImage(image_info);
170 (void) InvokeDelegate(read_info,image,"mpeg:decode",(char *) NULL,exception);
171 image=DestroyImage(image);
172 (void) FormatMagickString(read_info->filename,MaxTextExtent,"%s.%s",
173 read_info->unique,ReadMPEGIntermediateFormat);
174 images=ReadImage(read_info,exception);
175 (void) RelinquishUniqueFileResource(read_info->filename);
176 read_info=DestroyImageInfo(read_info);
181 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
185 % R e g i s t e r M P E G I m a g e %
189 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
191 % RegisterMPEGImage() adds attributes for the MPEG image format to
192 % the list of supported formats. The attributes include the image format
193 % tag, a method to read and/or write the format, whether the format
194 % supports the saving of more than one frame to the same file or blob,
195 % whether the format supports native in-memory I/O, and a brief
196 % description of the format.
198 % The format of the RegisterMPEGImage method is:
200 % unsigned long RegisterMPEGImage(void)
203 ModuleExport unsigned long RegisterMPEGImage(void)
208 entry=SetMagickInfo("MOV");
209 entry->decoder=(DecodeImageHandler *) ReadMPEGImage;
210 entry->encoder=(EncodeImageHandler *) WriteMPEGImage;
211 entry->magick=(IsImageFormatHandler *) IsMPEG;
212 entry->blob_support=MagickFalse;
213 entry->description=ConstantString("MPEG Video Stream");
214 entry->module=ConstantString("MPEG");
215 (void) RegisterMagickInfo(entry);
216 entry=SetMagickInfo("MPEG");
217 entry->decoder=(DecodeImageHandler *) ReadMPEGImage;
218 entry->encoder=(EncodeImageHandler *) WriteMPEGImage;
219 entry->magick=(IsImageFormatHandler *) IsMPEG;
220 entry->blob_support=MagickFalse;
221 entry->description=ConstantString("MPEG Video Stream");
222 entry->module=ConstantString("MPEG");
223 (void) RegisterMagickInfo(entry);
224 entry=SetMagickInfo("MPG");
225 entry->decoder=(DecodeImageHandler *) ReadMPEGImage;
226 entry->encoder=(EncodeImageHandler *) WriteMPEGImage;
227 entry->magick=(IsImageFormatHandler *) IsMPEG;
228 entry->blob_support=MagickFalse;
229 entry->description=ConstantString("MPEG Video Stream");
230 entry->module=ConstantString("MPEG");
231 (void) RegisterMagickInfo(entry);
232 entry=SetMagickInfo("MP4");
233 entry->decoder=(DecodeImageHandler *) ReadMPEGImage;
234 entry->encoder=(EncodeImageHandler *) WriteMPEGImage;
235 entry->magick=(IsImageFormatHandler *) IsMPEG;
236 entry->blob_support=MagickFalse;
237 entry->description=ConstantString("MPEG-4 Video Stream");
238 entry->module=ConstantString("MPEG");
239 (void) RegisterMagickInfo(entry);
240 entry=SetMagickInfo("M2V");
241 entry->decoder=(DecodeImageHandler *) ReadMPEGImage;
242 entry->encoder=(EncodeImageHandler *) WriteMPEGImage;
243 entry->magick=(IsImageFormatHandler *) IsMPEG;
244 entry->blob_support=MagickFalse;
245 entry->description=ConstantString("MPEG Video Stream");
246 entry->module=ConstantString("MPEG");
247 (void) RegisterMagickInfo(entry);
248 entry=SetMagickInfo("M4V");
249 entry->decoder=(DecodeImageHandler *) ReadMPEGImage;
250 entry->encoder=(EncodeImageHandler *) WriteMPEGImage;
251 entry->magick=(IsImageFormatHandler *) IsMPEG;
252 entry->blob_support=MagickFalse;
253 entry->description=ConstantString("Raw MPEG-4 Video");
254 entry->module=ConstantString("MPEG");
255 (void) RegisterMagickInfo(entry);
256 entry=SetMagickInfo("WMV");
257 entry->decoder=(DecodeImageHandler *) ReadMPEGImage;
258 entry->encoder=(EncodeImageHandler *) WriteMPEGImage;
259 entry->magick=(IsImageFormatHandler *) IsMPEG;
260 entry->blob_support=MagickFalse;
261 entry->description=ConstantString("Windows Media Video");
262 entry->module=ConstantString("MPEG");
263 (void) RegisterMagickInfo(entry);
264 return(MagickImageCoderSignature);
268 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
272 % U n r e g i s t e r M P E G I m a g e %
276 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
278 % UnregisterMPEGImage() removes format registrations made by the
279 % BIM module from the list of supported formats.
281 % The format of the UnregisterBIMImage method is:
283 % UnregisterMPEGImage(void)
286 ModuleExport void UnregisterMPEGImage(void)
288 (void) UnregisterMagickInfo("WMV");
289 (void) UnregisterMagickInfo("M4V");
290 (void) UnregisterMagickInfo("M2V");
291 (void) UnregisterMagickInfo("MP4");
292 (void) UnregisterMagickInfo("MPG");
293 (void) UnregisterMagickInfo("MPEG");
294 (void) UnregisterMagickInfo("MOV");
298 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
302 % W r i t e M P E G I m a g e %
306 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
308 % WriteMPEGImage() writes an image to a file in MPEG video stream format.
309 % Lawrence Livermore National Laboratory (LLNL) contributed code to adjust
310 % the MPEG parameters to correspond to the compression quality setting.
312 % The format of the WriteMPEGImage method is:
314 % MagickBooleanType WriteMPEGImage(const ImageInfo *image_info,
317 % A description of each parameter follows.
319 % o image_info: the image info.
321 % o image: The image.
325 static inline double MagickMax(const double x,const double y)
332 static inline double MagickMin(const double x,const double y)
339 static MagickBooleanType CopyDelegateFile(const char *source,
340 const char *destination)
366 Return if destination file already exists and is not empty.
368 assert(source != (const char *) NULL);
369 assert(destination != (char *) NULL);
370 status=GetPathAttributes(destination,&attributes);
371 if ((status != MagickFalse) && (attributes.st_size != 0))
374 Copy source file to destination.
376 destination_file=open(destination,O_WRONLY | O_BINARY | O_CREAT,S_MODE);
377 if (destination_file == -1)
379 source_file=open(source,O_RDONLY | O_BINARY);
380 if (source_file == -1)
382 (void) close(destination_file);
385 quantum=(size_t) MagickMaxBufferExtent;
386 if ((fstat(source_file,&attributes) == 0) && (attributes.st_size != 0))
387 quantum=(size_t) MagickMin((double) attributes.st_size,
388 MagickMaxBufferExtent);
389 buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
390 if (buffer == (unsigned char *) NULL)
392 (void) close(source_file);
393 (void) close(destination_file);
397 for (i=0; ; i+=count)
399 count=(ssize_t) read(source_file,buffer,quantum);
402 length=(size_t) count;
403 count=(ssize_t) write(destination_file,buffer,length);
404 if ((size_t) count != length)
407 (void) close(destination_file);
408 (void) close(source_file);
409 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
410 return(i != 0 ? MagickTrue : MagickFalse);
413 static MagickBooleanType WriteMPEGImage(const ImageInfo *image_info,
416 #define WriteMPEGIntermediateFormat "jpg"
419 basename[MaxTextExtent],
420 filename[MaxTextExtent];
454 Open output image file.
456 assert(image_info != (const ImageInfo *) NULL);
457 assert(image_info->signature == MagickSignature);
458 assert(image != (Image *) NULL);
459 assert(image->signature == MagickSignature);
460 if (image->debug != MagickFalse)
461 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
462 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
463 if (status == MagickFalse)
465 (void) CloseBlob(image);
467 Write intermediate files.
469 coalesce_image=CoalesceImages(image,&image->exception);
470 if (coalesce_image == (Image *) NULL)
472 file=AcquireUniqueFileResource(basename);
475 (void) FormatMagickString(coalesce_image->filename,MaxTextExtent,"%s",
478 write_info=CloneImageInfo(image_info);
479 for (p=coalesce_image; p != (Image *) NULL; p=GetNextImageInList(p))
482 previous_image[MaxTextExtent];
484 blob=(unsigned char *) NULL;
487 delay=100.0*p->delay/MagickMax(1.0*p->ticks_per_second,1.0);
488 for (i=0; i < (long) MagickMax((1.0*delay+1.0)/3.0,1.0); i++)
500 (void) FormatMagickString(p->filename,MaxTextExtent,"%s%lu.%s",
501 basename,p->scene,WriteMPEGIntermediateFormat);
502 (void) FormatMagickString(filename,MaxTextExtent,"%s%lu.%s",
503 basename,p->scene,WriteMPEGIntermediateFormat);
504 (void) FormatMagickString(previous_image,MaxTextExtent,
505 "%s%lu.%s",basename,p->scene,WriteMPEGIntermediateFormat);
506 frame=CloneImage(p,0,0,MagickTrue,&p->exception);
507 if (frame == (Image *) NULL)
509 status=WriteImage(write_info,frame);
510 frame=DestroyImage(frame);
515 blob=(unsigned char *) FileToBlob(previous_image,~0UL,&length,
520 (void) FormatMagickString(filename,MaxTextExtent,"%s%lu.%s",
521 basename,p->scene,WriteMPEGIntermediateFormat);
523 status=BlobToFile(filename,blob,length,&image->exception);
527 if (image->debug != MagickFalse)
529 if (status != MagickFalse)
530 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
531 "%lu. Wrote %s file for scene %lu:",i,WriteMPEGIntermediateFormat,
534 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
535 "%lu. Failed to write %s file for scene %lu:",i,
536 WriteMPEGIntermediateFormat,p->scene);
537 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%s",
542 if (blob != (unsigned char *) NULL)
543 blob=(unsigned char *) RelinquishMagickMemory(blob);
544 if (status == MagickFalse)
548 Convert JPEG to MPEG.
550 (void) CopyMagickString(coalesce_image->magick_filename,basename,
552 (void) CopyMagickString(coalesce_image->filename,basename,MaxTextExtent);
553 GetPathComponent(image_info->filename,ExtensionPath,coalesce_image->magick);
554 if (*coalesce_image->magick == '\0')
555 (void) CopyMagickString(coalesce_image->magick,image->magick,MaxTextExtent);
556 status=InvokeDelegate(write_info,coalesce_image,(char *) NULL,"mpeg:encode",
558 (void) FormatMagickString(write_info->filename,MaxTextExtent,"%s.%s",
559 write_info->unique,coalesce_image->magick);
560 status=CopyDelegateFile(write_info->filename,image->filename);
561 (void) RelinquishUniqueFileResource(write_info->filename);
562 write_info=DestroyImageInfo(write_info);
564 Relinquish resources.
567 for (p=coalesce_image; p != (Image *) NULL; p=GetNextImageInList(p))
569 delay=100.0*p->delay/MagickMax(1.0*p->ticks_per_second,1.0);
570 for (i=0; i < (long) MagickMax((1.0*delay+1.0)/3.0,1.0); i++)
572 (void) FormatMagickString(p->filename,MaxTextExtent,"%s%lu.%s",
573 basename,count++,WriteMPEGIntermediateFormat);
574 (void) RelinquishUniqueFileResource(p->filename);
576 (void) CopyMagickString(p->filename,image_info->filename,MaxTextExtent);
578 (void) RelinquishUniqueFileResource(basename);
579 coalesce_image=DestroyImageList(coalesce_image);
580 if (image->debug != MagickFalse)
581 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit");