2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
6 % M M PPPP EEEEE GGGG %
8 % M M M PPPP EEE G GG %
13 % Read/Write MPEG Image Format %
20 % Copyright 1999-2010 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 % IsAVI() returns MagickTrue if the image format type, identified by the
82 % magick string, is Audio/Video Interleaved file format.
84 % The format of the IsAVI method is:
86 % unsigned long IsAVI(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 IsAVI(const unsigned char *magick,const size_t length)
99 if (memcmp(magick,"RIFF",4) == 0)
105 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115 % IsMPEG() returns MagickTrue if the image format type, identified by the
116 % magick string, is MPEG.
118 % The format of the IsMPEG method is:
120 % MagickBooleanType IsMPEG(const unsigned char *magick,const size_t length)
122 % A description of each parameter follows:
124 % o magick: compare image format pattern against these bytes.
126 % o length: Specifies the length of the magick string.
129 static MagickBooleanType IsMPEG(const unsigned char *magick,const size_t length)
133 if (memcmp(magick,"\000\000\001\263",4) == 0)
139 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
143 % R e a d M P E G I m a g e %
147 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149 % ReadMPEGImage() reads an binary file in the MPEG video stream format
150 % and returns it. It allocates the memory necessary for the new Image
151 % structure and returns a pointer to the new image.
153 % The format of the ReadMPEGImage method is:
155 % Image *ReadMPEGImage(const ImageInfo *image_info,
156 % ExceptionInfo *exception)
158 % A description of each parameter follows:
160 % o image_info: the image info.
162 % o exception: return any errors or warnings in this structure.
165 static Image *ReadMPEGImage(const ImageInfo *image_info,
166 ExceptionInfo *exception)
168 #define ReadMPEGIntermediateFormat "pam"
183 assert(image_info != (const ImageInfo *) NULL);
184 assert(image_info->signature == MagickSignature);
185 if (image_info->debug != MagickFalse)
186 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
187 image_info->filename);
188 assert(exception != (ExceptionInfo *) NULL);
189 assert(exception->signature == MagickSignature);
190 image=AcquireImage(image_info);
191 status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
192 if (status == MagickFalse)
194 image=DestroyImageList(image);
195 return((Image *) NULL);
197 (void) CloseBlob(image);
198 (void) DestroyImageList(image);
200 Convert MPEG to PAM with delegate.
202 read_info=CloneImageInfo(image_info);
203 image=AcquireImage(image_info);
204 (void) InvokeDelegate(read_info,image,"mpeg:decode",(char *) NULL,exception);
205 image=DestroyImage(image);
206 (void) FormatMagickString(read_info->filename,MaxTextExtent,"%s.%s",
207 read_info->unique,ReadMPEGIntermediateFormat);
208 images=ReadImage(read_info,exception);
209 (void) RelinquishUniqueFileResource(read_info->filename);
210 read_info=DestroyImageInfo(read_info);
215 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
219 % R e g i s t e r M P E G I m a g e %
223 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
225 % RegisterMPEGImage() adds attributes for the MPEG image format to
226 % the list of supported formats. The attributes include the image format
227 % tag, a method to read and/or write the format, whether the format
228 % supports the saving of more than one frame to the same file or blob,
229 % whether the format supports native in-memory I/O, and a brief
230 % description of the format.
232 % The format of the RegisterMPEGImage method is:
234 % unsigned long RegisterMPEGImage(void)
237 ModuleExport unsigned long RegisterMPEGImage(void)
242 entry=SetMagickInfo("AVI");
243 entry->decoder=(DecodeImageHandler *) ReadMPEGImage;
244 entry->magick=(IsImageFormatHandler *) IsAVI;
245 entry->description=ConstantString("Microsoft Audio/Visual Interleaved");
246 entry->module=ConstantString("AVI");
247 (void) RegisterMagickInfo(entry);
248 entry=SetMagickInfo("MOV");
249 entry->decoder=(DecodeImageHandler *) ReadMPEGImage;
250 entry->encoder=(EncodeImageHandler *) WriteMPEGImage;
251 entry->magick=(IsImageFormatHandler *) IsMPEG;
252 entry->blob_support=MagickFalse;
253 entry->description=ConstantString("MPEG Video Stream");
254 entry->module=ConstantString("MPEG");
255 (void) RegisterMagickInfo(entry);
256 entry=SetMagickInfo("MPEG");
257 entry->decoder=(DecodeImageHandler *) ReadMPEGImage;
258 entry->encoder=(EncodeImageHandler *) WriteMPEGImage;
259 entry->magick=(IsImageFormatHandler *) IsMPEG;
260 entry->blob_support=MagickFalse;
261 entry->description=ConstantString("MPEG Video Stream");
262 entry->module=ConstantString("MPEG");
263 (void) RegisterMagickInfo(entry);
264 entry=SetMagickInfo("MPG");
265 entry->decoder=(DecodeImageHandler *) ReadMPEGImage;
266 entry->encoder=(EncodeImageHandler *) WriteMPEGImage;
267 entry->magick=(IsImageFormatHandler *) IsMPEG;
268 entry->blob_support=MagickFalse;
269 entry->description=ConstantString("MPEG Video Stream");
270 entry->module=ConstantString("MPEG");
271 (void) RegisterMagickInfo(entry);
272 entry=SetMagickInfo("MP4");
273 entry->decoder=(DecodeImageHandler *) ReadMPEGImage;
274 entry->encoder=(EncodeImageHandler *) WriteMPEGImage;
275 entry->magick=(IsImageFormatHandler *) IsMPEG;
276 entry->blob_support=MagickFalse;
277 entry->description=ConstantString("MPEG-4 Video Stream");
278 entry->module=ConstantString("MPEG");
279 (void) RegisterMagickInfo(entry);
280 entry=SetMagickInfo("M2V");
281 entry->decoder=(DecodeImageHandler *) ReadMPEGImage;
282 entry->encoder=(EncodeImageHandler *) WriteMPEGImage;
283 entry->magick=(IsImageFormatHandler *) IsMPEG;
284 entry->blob_support=MagickFalse;
285 entry->description=ConstantString("MPEG Video Stream");
286 entry->module=ConstantString("MPEG");
287 (void) RegisterMagickInfo(entry);
288 entry=SetMagickInfo("M4V");
289 entry->decoder=(DecodeImageHandler *) ReadMPEGImage;
290 entry->encoder=(EncodeImageHandler *) WriteMPEGImage;
291 entry->magick=(IsImageFormatHandler *) IsMPEG;
292 entry->blob_support=MagickFalse;
293 entry->description=ConstantString("Raw MPEG-4 Video");
294 entry->module=ConstantString("MPEG");
295 (void) RegisterMagickInfo(entry);
296 entry=SetMagickInfo("WMV");
297 entry->decoder=(DecodeImageHandler *) ReadMPEGImage;
298 entry->encoder=(EncodeImageHandler *) WriteMPEGImage;
299 entry->magick=(IsImageFormatHandler *) IsMPEG;
300 entry->blob_support=MagickFalse;
301 entry->description=ConstantString("Windows Media Video");
302 entry->module=ConstantString("MPEG");
303 (void) RegisterMagickInfo(entry);
304 return(MagickImageCoderSignature);
308 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
312 % U n r e g i s t e r M P E G I m a g e %
316 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
318 % UnregisterMPEGImage() removes format registrations made by the
319 % BIM module from the list of supported formats.
321 % The format of the UnregisterBIMImage method is:
323 % UnregisterMPEGImage(void)
326 ModuleExport void UnregisterMPEGImage(void)
328 (void) UnregisterMagickInfo("WMV");
329 (void) UnregisterMagickInfo("M4V");
330 (void) UnregisterMagickInfo("M2V");
331 (void) UnregisterMagickInfo("MP4");
332 (void) UnregisterMagickInfo("MPG");
333 (void) UnregisterMagickInfo("MPEG");
334 (void) UnregisterMagickInfo("MOV");
335 (void) UnregisterMagickInfo("AVI");
339 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
343 % W r i t e M P E G I m a g e %
347 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
349 % WriteMPEGImage() writes an image to a file in MPEG video stream format.
350 % Lawrence Livermore National Laboratory (LLNL) contributed code to adjust
351 % the MPEG parameters to correspond to the compression quality setting.
353 % The format of the WriteMPEGImage method is:
355 % MagickBooleanType WriteMPEGImage(const ImageInfo *image_info,
358 % A description of each parameter follows.
360 % o image_info: the image info.
362 % o image: The image.
366 static inline double MagickMax(const double x,const double y)
373 static inline double MagickMin(const double x,const double y)
380 static MagickBooleanType CopyDelegateFile(const char *source,
381 const char *destination)
407 Return if destination file already exists and is not empty.
409 assert(source != (const char *) NULL);
410 assert(destination != (char *) NULL);
411 status=GetPathAttributes(destination,&attributes);
412 if ((status != MagickFalse) && (attributes.st_size != 0))
415 Copy source file to destination.
417 destination_file=open(destination,O_WRONLY | O_BINARY | O_CREAT,S_MODE);
418 if (destination_file == -1)
420 source_file=open(source,O_RDONLY | O_BINARY);
421 if (source_file == -1)
423 (void) close(destination_file);
426 quantum=(size_t) MagickMaxBufferExtent;
427 if ((fstat(source_file,&attributes) == 0) && (attributes.st_size != 0))
428 quantum=(size_t) MagickMin((double) attributes.st_size,
429 MagickMaxBufferExtent);
430 buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
431 if (buffer == (unsigned char *) NULL)
433 (void) close(source_file);
434 (void) close(destination_file);
438 for (i=0; ; i+=count)
440 count=(ssize_t) read(source_file,buffer,quantum);
443 length=(size_t) count;
444 count=(ssize_t) write(destination_file,buffer,length);
445 if ((size_t) count != length)
448 (void) close(destination_file);
449 (void) close(source_file);
450 buffer=(unsigned char *) RelinquishMagickMemory(buffer);
451 return(i != 0 ? MagickTrue : MagickFalse);
454 static MagickBooleanType WriteMPEGImage(const ImageInfo *image_info,
457 #define WriteMPEGIntermediateFormat "jpg"
460 basename[MaxTextExtent],
461 filename[MaxTextExtent];
495 Open output image file.
497 assert(image_info != (const ImageInfo *) NULL);
498 assert(image_info->signature == MagickSignature);
499 assert(image != (Image *) NULL);
500 assert(image->signature == MagickSignature);
501 if (image->debug != MagickFalse)
502 (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
503 status=OpenBlob(image_info,image,WriteBinaryBlobMode,&image->exception);
504 if (status == MagickFalse)
506 (void) CloseBlob(image);
508 Write intermediate files.
510 coalesce_image=CoalesceImages(image,&image->exception);
511 if (coalesce_image == (Image *) NULL)
513 file=AcquireUniqueFileResource(basename);
516 (void) FormatMagickString(coalesce_image->filename,MaxTextExtent,"%s",
519 write_info=CloneImageInfo(image_info);
520 for (p=coalesce_image; p != (Image *) NULL; p=GetNextImageInList(p))
523 previous_image[MaxTextExtent];
525 blob=(unsigned char *) NULL;
528 delay=100.0*p->delay/MagickMax(1.0*p->ticks_per_second,1.0);
529 for (i=0; i < (long) MagickMax((1.0*delay+1.0)/3.0,1.0); i++)
541 (void) FormatMagickString(p->filename,MaxTextExtent,"%s%lu.%s",
542 basename,p->scene,WriteMPEGIntermediateFormat);
543 (void) FormatMagickString(filename,MaxTextExtent,"%s%lu.%s",
544 basename,p->scene,WriteMPEGIntermediateFormat);
545 (void) FormatMagickString(previous_image,MaxTextExtent,
546 "%s%lu.%s",basename,p->scene,WriteMPEGIntermediateFormat);
547 frame=CloneImage(p,0,0,MagickTrue,&p->exception);
548 if (frame == (Image *) NULL)
550 status=WriteImage(write_info,frame);
551 frame=DestroyImage(frame);
556 blob=(unsigned char *) FileToBlob(previous_image,~0UL,&length,
561 (void) FormatMagickString(filename,MaxTextExtent,"%s%lu.%s",
562 basename,p->scene,WriteMPEGIntermediateFormat);
564 status=BlobToFile(filename,blob,length,&image->exception);
568 if (image->debug != MagickFalse)
570 if (status != MagickFalse)
571 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
572 "%lu. Wrote %s file for scene %lu:",i,WriteMPEGIntermediateFormat,
575 (void) LogMagickEvent(CoderEvent,GetMagickModule(),
576 "%lu. Failed to write %s file for scene %lu:",i,
577 WriteMPEGIntermediateFormat,p->scene);
578 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%s",
583 if (blob != (unsigned char *) NULL)
584 blob=(unsigned char *) RelinquishMagickMemory(blob);
585 if (status == MagickFalse)
589 Convert JPEG to MPEG.
591 (void) CopyMagickString(coalesce_image->magick_filename,basename,
593 (void) CopyMagickString(coalesce_image->filename,basename,MaxTextExtent);
594 GetPathComponent(image_info->filename,ExtensionPath,coalesce_image->magick);
595 if (*coalesce_image->magick == '\0')
596 (void) CopyMagickString(coalesce_image->magick,image->magick,MaxTextExtent);
597 status=InvokeDelegate(write_info,coalesce_image,(char *) NULL,"mpeg:encode",
599 (void) FormatMagickString(write_info->filename,MaxTextExtent,"%s.%s",
600 write_info->unique,coalesce_image->magick);
601 status=CopyDelegateFile(write_info->filename,image->filename);
602 (void) RelinquishUniqueFileResource(write_info->filename);
603 write_info=DestroyImageInfo(write_info);
605 Relinquish resources.
608 for (p=coalesce_image; p != (Image *) NULL; p=GetNextImageInList(p))
610 delay=100.0*p->delay/MagickMax(1.0*p->ticks_per_second,1.0);
611 for (i=0; i < (long) MagickMax((1.0*delay+1.0)/3.0,1.0); i++)
613 (void) FormatMagickString(p->filename,MaxTextExtent,"%s%lu.%s",
614 basename,count++,WriteMPEGIntermediateFormat);
615 (void) RelinquishUniqueFileResource(p->filename);
617 (void) CopyMagickString(p->filename,image_info->filename,MaxTextExtent);
619 (void) RelinquishUniqueFileResource(basename);
620 coalesce_image=DestroyImageList(coalesce_image);
621 if (image->debug != MagickFalse)
622 (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit");