1 /*
2 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
3 %                                                                             %
4 %                                                                             %
5 %                                                                             %
6 %                        M   M  PPPP   EEEEE   GGGG                           %
7 %                        MM MM  P   P  E      G                               %
8 %                        M M M  PPPP   EEE    G  GG                           %
9 %                        M   M  P      E      G   G                           %
10 %                        M   M  P      EEEEE   GGGG                           %
11 %                                                                             %
12 %                                                                             %
13 %                       Read/Write MPEG Image Format                          %
14 %                                                                             %
15 %                              Software Design                                %
16 %                                   Cristy                                    %
17 %                                 July 1999                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2019 ImageMagick Studio LLC, a non-profit organization      %
21 %  dedicated to making software imaging solutions freely available.           %
22 %                                                                             %
23 %  You may not use this file except in compliance with the License.  You may  %
24 %  obtain a copy of the License at                                            %
25 %                                                                             %
26 %    https://imagemagick.org/script/license.php                               %
27 %                                                                             %
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.                                             %
33 %                                                                             %
34 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
35 %
36 %
37 */
38 /*
39   Include declarations.
40 */
41 #include "MagickCore/studio.h"
42 #include "MagickCore/blob.h"
43 #include "MagickCore/blob-private.h"
44 #include "MagickCore/constitute.h"
45 #include "MagickCore/delegate.h"
46 #include "MagickCore/exception.h"
47 #include "MagickCore/exception-private.h"
48 #include "MagickCore/geometry.h"
49 #include "MagickCore/image.h"
50 #include "MagickCore/image-private.h"
51 #include "MagickCore/layer.h"
52 #include "MagickCore/list.h"
53 #include "MagickCore/log.h"
54 #include "MagickCore/magick.h"
55 #include "MagickCore/memory_.h"
56 #include "MagickCore/resource_.h"
57 #include "MagickCore/quantum-private.h"
58 #include "MagickCore/static.h"
59 #include "MagickCore/string_.h"
60 #include "MagickCore/module.h"
61 #include "MagickCore/transform.h"
62 #include "MagickCore/utility.h"
63 #include "MagickCore/utility-private.h"
64 
65 /*
66   Forward declarations.
67 */
68 static MagickBooleanType
69   WriteMPEGImage(const ImageInfo *,Image *,ExceptionInfo *);
70 
71 /*
72 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
73 %                                                                             %
74 %                                                                             %
75 %                                                                             %
76 %   I s A V I                                                                 %
77 %                                                                             %
78 %                                                                             %
79 %                                                                             %
80 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
81 %
82 %  IsAVI() returns MagickTrue if the image format type, identified by the
83 %  magick string, is Audio/Video Interleaved file format.
84 %
85 %  The format of the IsAVI method is:
86 %
87 %      size_t IsAVI(const unsigned char *magick,const size_t length)
88 %
89 %  A description of each parameter follows:
90 %
91 %    o magick: compare image format pattern against these bytes.
92 %
93 %    o length: Specifies the length of the magick string.
94 %
95 */
IsAVI(const unsigned char * magick,const size_t length)96 static MagickBooleanType IsAVI(const unsigned char *magick,const size_t length)
97 {
98   if (length < 4)
99     return(MagickFalse);
100   if (memcmp(magick,"RIFF",4) == 0)
101     return(MagickTrue);
102   return(MagickFalse);
103 }
104 
105 /*
106 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
107 %                                                                             %
108 %                                                                             %
109 %                                                                             %
110 %   I s M P E G                                                               %
111 %                                                                             %
112 %                                                                             %
113 %                                                                             %
114 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115 %
116 %  IsMPEG() returns MagickTrue if the image format type, identified by the
117 %  magick string, is MPEG.
118 %
119 %  The format of the IsMPEG method is:
120 %
121 %      MagickBooleanType IsMPEG(const unsigned char *magick,const size_t length)
122 %
123 %  A description of each parameter follows:
124 %
125 %    o magick: compare image format pattern against these bytes.
126 %
127 %    o length: Specifies the length of the magick string.
128 %
129 */
IsMPEG(const unsigned char * magick,const size_t length)130 static MagickBooleanType IsMPEG(const unsigned char *magick,const size_t length)
131 {
132   if (length < 4)
133     return(MagickFalse);
134   if (memcmp(magick,"\000\000\001\263",4) == 0)
135     return(MagickTrue);
136   return(MagickFalse);
137 }
138 
139 /*
140 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
141 %                                                                             %
142 %                                                                             %
143 %                                                                             %
144 %   R e a d M P E G I m a g e                                                 %
145 %                                                                             %
146 %                                                                             %
147 %                                                                             %
148 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
149 %
150 %  ReadMPEGImage() reads an binary file in the MPEG video stream format
151 %  and returns it.  It allocates the memory necessary for the new Image
152 %  structure and returns a pointer to the new image.
153 %
154 %  The format of the ReadMPEGImage method is:
155 %
156 %      Image *ReadMPEGImage(const ImageInfo *image_info,
157 %        ExceptionInfo *exception)
158 %
159 %  A description of each parameter follows:
160 %
161 %    o image_info: the image info.
162 %
163 %    o exception: return any errors or warnings in this structure.
164 %
165 */
ReadMPEGImage(const ImageInfo * image_info,ExceptionInfo * exception)166 static Image *ReadMPEGImage(const ImageInfo *image_info,
167   ExceptionInfo *exception)
168 {
169 #define ReadMPEGIntermediateFormat "pam"
170 
171   Image
172     *image,
173     *images,
174     *next;
175 
176   ImageInfo
177     *read_info;
178 
179   MagickBooleanType
180     status;
181 
182   /*
183     Open image file.
184   */
185   assert(image_info != (const ImageInfo *) NULL);
186   assert(image_info->signature == MagickCoreSignature);
187   if (image_info->debug != MagickFalse)
188     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",
189       image_info->filename);
190   assert(exception != (ExceptionInfo *) NULL);
191   assert(exception->signature == MagickCoreSignature);
192   image=AcquireImage(image_info,exception);
193   status=OpenBlob(image_info,image,ReadBinaryBlobMode,exception);
194   if (status == MagickFalse)
195     {
196       image=DestroyImageList(image);
197       return((Image *) NULL);
198     }
199   (void) CloseBlob(image);
200   (void) DestroyImageList(image);
201   /*
202     Convert MPEG to PAM with delegate.
203   */
204   images=(Image *) NULL;
205   read_info=CloneImageInfo(image_info);
206   image=AcquireImage(image_info,exception);
207   status=InvokeDelegate(read_info,image,"mpeg:decode",(char *) NULL,exception);
208   if (status != MagickFalse)
209     {
210       (void) FormatLocaleString(read_info->filename,MagickPathExtent,"%s.%s",
211         read_info->unique,ReadMPEGIntermediateFormat);
212       *read_info->magick='\0';
213       images=ReadImage(read_info,exception);
214       if (images != (Image *) NULL)
215         for (next=images; next != (Image *) NULL; next=next->next)
216         {
217           (void) CopyMagickString(next->filename,image->filename,
218             MagickPathExtent);
219           (void) CopyMagickString(next->magick,image->magick,MagickPathExtent);
220         }
221       (void) RelinquishUniqueFileResource(read_info->filename);
222     }
223   read_info=DestroyImageInfo(read_info);
224   image=DestroyImage(image);
225   return(images);
226 }
227 
228 /*
229 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
230 %                                                                             %
231 %                                                                             %
232 %                                                                             %
233 %   R e g i s t e r M P E G I m a g e                                         %
234 %                                                                             %
235 %                                                                             %
236 %                                                                             %
237 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
238 %
239 %  RegisterMPEGImage() adds attributes for the MPEG image format to
240 %  the list of supported formats.  The attributes include the image format
241 %  tag, a method to read and/or write the format, whether the format
242 %  supports the saving of more than one frame to the same file or blob,
243 %  whether the format supports native in-memory I/O, and a brief
244 %  description of the format.
245 %
246 %  The format of the RegisterMPEGImage method is:
247 %
248 %      size_t RegisterMPEGImage(void)
249 %
250 */
RegisterMPEGImage(void)251 ModuleExport size_t RegisterMPEGImage(void)
252 {
253   MagickInfo
254     *entry;
255 
256   entry=AcquireMagickInfo("MPEG","3GP","Media Container");
257   entry->decoder=(DecodeImageHandler *) ReadMPEGImage;
258   entry->flags^=CoderBlobSupportFlag;
259   entry->flags|=CoderSeekableStreamFlag;
260   (void) RegisterMagickInfo(entry);
261   entry=AcquireMagickInfo("MPEG","3G2","Media Container");
262   entry->decoder=(DecodeImageHandler *) ReadMPEGImage;
263   entry->flags^=CoderBlobSupportFlag;
264   entry->flags|=CoderSeekableStreamFlag;
265   (void) RegisterMagickInfo(entry);
266   entry=AcquireMagickInfo("MPEG","AVI","Microsoft Audio/Visual Interleaved");
267   entry->decoder=(DecodeImageHandler *) ReadMPEGImage;
268   entry->magick=(IsImageFormatHandler *) IsAVI;
269   entry->flags^=CoderBlobSupportFlag;
270   (void) RegisterMagickInfo(entry);
271   entry=AcquireMagickInfo("MPEG","MKV","Multimedia Container");
272   entry->decoder=(DecodeImageHandler *) ReadMPEGImage;
273   entry->encoder=(EncodeImageHandler *) WriteMPEGImage;
274   entry->magick=(IsImageFormatHandler *) IsMPEG;
275   entry->flags^=CoderBlobSupportFlag;
276   (void) RegisterMagickInfo(entry);
277   entry=AcquireMagickInfo("MPEG","MOV","MPEG Video Stream");
278   entry->decoder=(DecodeImageHandler *) ReadMPEGImage;
279   entry->encoder=(EncodeImageHandler *) WriteMPEGImage;
280   entry->magick=(IsImageFormatHandler *) IsMPEG;
281   entry->flags^=CoderBlobSupportFlag;
282   (void) RegisterMagickInfo(entry);
283   entry=AcquireMagickInfo("MPEG","MPEG","MPEG Video Stream");
284   entry->decoder=(DecodeImageHandler *) ReadMPEGImage;
285   entry->encoder=(EncodeImageHandler *) WriteMPEGImage;
286   entry->magick=(IsImageFormatHandler *) IsMPEG;
287   entry->flags^=CoderBlobSupportFlag;
288   (void) RegisterMagickInfo(entry);
289   entry=AcquireMagickInfo("MPEG","MPG","MPEG Video Stream");
290   entry->decoder=(DecodeImageHandler *) ReadMPEGImage;
291   entry->encoder=(EncodeImageHandler *) WriteMPEGImage;
292   entry->magick=(IsImageFormatHandler *) IsMPEG;
293   entry->flags^=CoderBlobSupportFlag;
294   (void) RegisterMagickInfo(entry);
295   entry=AcquireMagickInfo("MPEG","MP4","MPEG-4 Video Stream");
296   entry->decoder=(DecodeImageHandler *) ReadMPEGImage;
297   entry->encoder=(EncodeImageHandler *) WriteMPEGImage;
298   entry->magick=(IsImageFormatHandler *) IsMPEG;
299   entry->flags^=CoderBlobSupportFlag;
300   (void) RegisterMagickInfo(entry);
301   entry=AcquireMagickInfo("MPEG","M2V","MPEG Video Stream");
302   entry->decoder=(DecodeImageHandler *) ReadMPEGImage;
303   entry->encoder=(EncodeImageHandler *) WriteMPEGImage;
304   entry->magick=(IsImageFormatHandler *) IsMPEG;
305   entry->flags^=CoderBlobSupportFlag;
306   (void) RegisterMagickInfo(entry);
307   entry=AcquireMagickInfo("MPEG","M4V","Raw MPEG-4 Video");
308   entry->decoder=(DecodeImageHandler *) ReadMPEGImage;
309   entry->encoder=(EncodeImageHandler *) WriteMPEGImage;
310   entry->magick=(IsImageFormatHandler *) IsMPEG;
311   entry->flags^=CoderBlobSupportFlag;
312   (void) RegisterMagickInfo(entry);
313   entry=AcquireMagickInfo("MPEG","WMV","Windows Media Video");
314   entry->decoder=(DecodeImageHandler *) ReadMPEGImage;
315   entry->encoder=(EncodeImageHandler *) WriteMPEGImage;
316   entry->magick=(IsImageFormatHandler *) IsMPEG;
317   entry->flags^=CoderBlobSupportFlag;
318   (void) RegisterMagickInfo(entry);
319   return(MagickImageCoderSignature);
320 }
321 
322 /*
323 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
324 %                                                                             %
325 %                                                                             %
326 %                                                                             %
327 %   U n r e g i s t e r M P E G I m a g e                                     %
328 %                                                                             %
329 %                                                                             %
330 %                                                                             %
331 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
332 %
333 %  UnregisterMPEGImage() removes format registrations made by the
334 %  BIM module from the list of supported formats.
335 %
336 %  The format of the UnregisterBIMImage method is:
337 %
338 %      UnregisterMPEGImage(void)
339 %
340 */
UnregisterMPEGImage(void)341 ModuleExport void UnregisterMPEGImage(void)
342 {
343   (void) UnregisterMagickInfo("WMV");
344   (void) UnregisterMagickInfo("MOV");
345   (void) UnregisterMagickInfo("M4V");
346   (void) UnregisterMagickInfo("M2V");
347   (void) UnregisterMagickInfo("MP4");
348   (void) UnregisterMagickInfo("MPG");
349   (void) UnregisterMagickInfo("MPEG");
350   (void) UnregisterMagickInfo("MKV");
351   (void) UnregisterMagickInfo("AVI");
352   (void) UnregisterMagickInfo("3G2");
353   (void) UnregisterMagickInfo("3GP");
354 }
355 
356 /*
357 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
358 %                                                                             %
359 %                                                                             %
360 %                                                                             %
361 %   W r i t e M P E G I m a g e                                               %
362 %                                                                             %
363 %                                                                             %
364 %                                                                             %
365 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
366 %
367 %  WriteMPEGImage() writes an image to a file in MPEG video stream format.
368 %  Lawrence Livermore National Laboratory (LLNL) contributed code to adjust
369 %  the MPEG parameters to correspond to the compression quality setting.
370 %
371 %  The format of the WriteMPEGImage method is:
372 %
373 %      MagickBooleanType WriteMPEGImage(const ImageInfo *image_info,
374 %        Image *image,ExceptionInfo *exception)
375 %
376 %  A description of each parameter follows.
377 %
378 %    o image_info: the image info.
379 %
380 %    o image:  The image.
381 %
382 %    o exception: return any errors or warnings in this structure.
383 %
384 */
CopyDelegateFile(const char * source,const char * destination)385 static MagickBooleanType CopyDelegateFile(const char *source,
386   const char *destination)
387 {
388   int
389     destination_file,
390     source_file;
391 
392   MagickBooleanType
393     status;
394 
395   register size_t
396     i;
397 
398   size_t
399     length,
400     quantum;
401 
402   ssize_t
403     count;
404 
405   struct stat
406     attributes;
407 
408   unsigned char
409     *buffer;
410 
411   /*
412     Return if destination file already exists and is not empty.
413   */
414   assert(source != (const char *) NULL);
415   assert(destination != (char *) NULL);
416   status=GetPathAttributes(destination,&attributes);
417   if ((status != MagickFalse) && (attributes.st_size > 0))
418     return(MagickTrue);
419   /*
420     Copy source file to destination.
421   */
422   destination_file=open_utf8(destination,O_WRONLY | O_BINARY | O_CREAT,S_MODE);
423   if (destination_file == -1)
424     return(MagickFalse);
425   source_file=open_utf8(source,O_RDONLY | O_BINARY,0);
426   if (source_file == -1)
427     {
428       (void) close(destination_file);
429       return(MagickFalse);
430     }
431   quantum=(size_t) MagickMaxBufferExtent;
432   if ((fstat(source_file,&attributes) == 0) && (attributes.st_size > 0))
433     quantum=(size_t) MagickMin((double) attributes.st_size,
434       MagickMaxBufferExtent);
435   buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
436   if (buffer == (unsigned char *) NULL)
437     {
438       (void) close(source_file);
439       (void) close(destination_file);
440       return(MagickFalse);
441     }
442   length=0;
443   for (i=0; ; i+=count)
444   {
445     count=(ssize_t) read(source_file,buffer,quantum);
446     if (count <= 0)
447       break;
448     length=(size_t) count;
449     count=(ssize_t) write(destination_file,buffer,length);
450     if ((size_t) count != length)
451       break;
452   }
453   (void) close(destination_file);
454   (void) close(source_file);
455   buffer=(unsigned char *) RelinquishMagickMemory(buffer);
456   return(i != 0 ? MagickTrue : MagickFalse);
457 }
458 
WriteMPEGImage(const ImageInfo * image_info,Image * image,ExceptionInfo * exception)459 static MagickBooleanType WriteMPEGImage(const ImageInfo *image_info,
460   Image *image,ExceptionInfo *exception)
461 {
462 #define WriteMPEGIntermediateFormat "jpg"
463 
464   char
465     basename[MagickPathExtent],
466     filename[MagickPathExtent];
467 
468   double
469     delay;
470 
471   Image
472     *coalesce_image;
473 
474   ImageInfo
475     *write_info;
476 
477   int
478     file;
479 
480   MagickBooleanType
481     status;
482 
483   register Image
484     *p;
485 
486   register ssize_t
487     i;
488 
489   size_t
490     count,
491     length,
492     scene;
493 
494   unsigned char
495     *blob;
496 
497   /*
498     Open output image file.
499   */
500   assert(image_info != (const ImageInfo *) NULL);
501   assert(image_info->signature == MagickCoreSignature);
502   assert(image != (Image *) NULL);
503   assert(image->signature == MagickCoreSignature);
504   if (image->debug != MagickFalse)
505     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
506   assert(exception != (ExceptionInfo *) NULL);
507   assert(exception->signature == MagickCoreSignature);
508   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
509   if (status == MagickFalse)
510     return(status);
511   (void) CloseBlob(image);
512   /*
513     Write intermediate files.
514   */
515   coalesce_image=CoalesceImages(image,exception);
516   if (coalesce_image == (Image *) NULL)
517     return(MagickFalse);
518   file=AcquireUniqueFileResource(basename);
519   if (file != -1)
520     file=close(file)-1;
521   (void) FormatLocaleString(coalesce_image->filename,MagickPathExtent,"%s",
522     basename);
523   count=0;
524   write_info=CloneImageInfo(image_info);
525   *write_info->magick='\0';
526   for (p=coalesce_image; p != (Image *) NULL; p=GetNextImageInList(p))
527   {
528     char
529       previous_image[MagickPathExtent];
530 
531     blob=(unsigned char *) NULL;
532     length=0;
533     scene=p->scene;
534     delay=100.0*p->delay/MagickMax(1.0*p->ticks_per_second,1.0);
535     for (i=0; i < (ssize_t) MagickMax((1.0*delay+1.0)/3.0,1.0); i++)
536     {
537       p->scene=count;
538       count++;
539       status=MagickFalse;
540       switch (i)
541       {
542         case 0:
543         {
544           Image
545             *frame;
546 
547           (void) FormatLocaleString(p->filename,MagickPathExtent,"%s%.20g.%s",
548             basename,(double) p->scene,WriteMPEGIntermediateFormat);
549           (void) FormatLocaleString(filename,MagickPathExtent,"%s%.20g.%s",
550             basename,(double) p->scene,WriteMPEGIntermediateFormat);
551           (void) FormatLocaleString(previous_image,MagickPathExtent,
552             "%s%.20g.%s",basename,(double) p->scene,
553             WriteMPEGIntermediateFormat);
554           frame=CloneImage(p,0,0,MagickTrue,exception);
555           if (frame == (Image *) NULL)
556             break;
557           status=WriteImage(write_info,frame,exception);
558           frame=DestroyImage(frame);
559           break;
560         }
561         case 1:
562         {
563           blob=(unsigned char *) FileToBlob(previous_image,~0UL,&length,
564             exception);
565         }
566         default:
567         {
568           (void) FormatLocaleString(filename,MagickPathExtent,"%s%.20g.%s",
569             basename,(double) p->scene,WriteMPEGIntermediateFormat);
570           if (length > 0)
571             status=BlobToFile(filename,blob,length,exception);
572           break;
573         }
574       }
575       if (image->debug != MagickFalse)
576         {
577           if (status != MagickFalse)
578             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
579               "%.20g. Wrote %s file for scene %.20g:",(double) i,
580               WriteMPEGIntermediateFormat,(double) p->scene);
581           else
582             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
583               "%.20g. Failed to write %s file for scene %.20g:",(double) i,
584               WriteMPEGIntermediateFormat,(double) p->scene);
585           (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%s",filename);
586         }
587     }
588     p->scene=scene;
589     if (blob != (unsigned char *) NULL)
590       blob=(unsigned char *) RelinquishMagickMemory(blob);
591     if (status == MagickFalse)
592       break;
593   }
594   /*
595     Convert JPEG to MPEG.
596   */
597   (void) CopyMagickString(coalesce_image->magick_filename,basename,
598     MagickPathExtent);
599   (void) CopyMagickString(coalesce_image->filename,basename,MagickPathExtent);
600   GetPathComponent(image_info->filename,ExtensionPath,coalesce_image->magick);
601   if (*coalesce_image->magick == '\0')
602     (void) CopyMagickString(coalesce_image->magick,image->magick,
603       MagickPathExtent);
604   status=InvokeDelegate(write_info,coalesce_image,(char *) NULL,"mpeg:encode",
605     exception);
606   (void) FormatLocaleString(write_info->filename,MagickPathExtent,"%s.%s",
607     write_info->unique,coalesce_image->magick);
608   status=CopyDelegateFile(write_info->filename,image->filename);
609   (void) RelinquishUniqueFileResource(write_info->filename);
610   write_info=DestroyImageInfo(write_info);
611   /*
612     Relinquish resources.
613   */
614   count=0;
615   for (p=coalesce_image; p != (Image *) NULL; p=GetNextImageInList(p))
616   {
617     delay=100.0*p->delay/MagickMax(1.0*p->ticks_per_second,1.0);
618     for (i=0; i < (ssize_t) MagickMax((1.0*delay+1.0)/3.0,1.0); i++)
619     {
620       (void) FormatLocaleString(p->filename,MagickPathExtent,"%s%.20g.%s",
621         basename,(double) count++,WriteMPEGIntermediateFormat);
622       (void) RelinquishUniqueFileResource(p->filename);
623     }
624     (void) CopyMagickString(p->filename,image_info->filename,MagickPathExtent);
625   }
626   (void) RelinquishUniqueFileResource(basename);
627   coalesce_image=DestroyImageList(coalesce_image);
628   if (image->debug != MagickFalse)
629     (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit");
630   return(status);
631 }
632