• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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-2020 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","FLV","Flash Video Stream");
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","MKV","Multimedia Container");
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","MOV","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","MPEG","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","MPG","MPEG 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","MP4","MPEG-4 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","M2V","MPEG Video Stream");
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","M4V","Raw MPEG-4 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   entry=AcquireMagickInfo("MPEG","WMV","Windows Media Video");
320   entry->decoder=(DecodeImageHandler *) ReadMPEGImage;
321   entry->encoder=(EncodeImageHandler *) WriteMPEGImage;
322   entry->magick=(IsImageFormatHandler *) IsMPEG;
323   entry->flags^=CoderBlobSupportFlag;
324   (void) RegisterMagickInfo(entry);
325   return(MagickImageCoderSignature);
326 }
327 
328 /*
329 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
330 %                                                                             %
331 %                                                                             %
332 %                                                                             %
333 %   U n r e g i s t e r M P E G I m a g e                                     %
334 %                                                                             %
335 %                                                                             %
336 %                                                                             %
337 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
338 %
339 %  UnregisterMPEGImage() removes format registrations made by the
340 %  BIM module from the list of supported formats.
341 %
342 %  The format of the UnregisterBIMImage method is:
343 %
344 %      UnregisterMPEGImage(void)
345 %
346 */
UnregisterMPEGImage(void)347 ModuleExport void UnregisterMPEGImage(void)
348 {
349   (void) UnregisterMagickInfo("WMV");
350   (void) UnregisterMagickInfo("MOV");
351   (void) UnregisterMagickInfo("M4V");
352   (void) UnregisterMagickInfo("M2V");
353   (void) UnregisterMagickInfo("MP4");
354   (void) UnregisterMagickInfo("MPG");
355   (void) UnregisterMagickInfo("MPEG");
356   (void) UnregisterMagickInfo("MKV");
357   (void) UnregisterMagickInfo("AVI");
358   (void) UnregisterMagickInfo("3G2");
359   (void) UnregisterMagickInfo("3GP");
360 }
361 
362 /*
363 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
364 %                                                                             %
365 %                                                                             %
366 %                                                                             %
367 %   W r i t e M P E G I m a g e                                               %
368 %                                                                             %
369 %                                                                             %
370 %                                                                             %
371 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
372 %
373 %  WriteMPEGImage() writes an image to a file in MPEG video stream format.
374 %  Lawrence Livermore National Laboratory (LLNL) contributed code to adjust
375 %  the MPEG parameters to correspond to the compression quality setting.
376 %
377 %  The format of the WriteMPEGImage method is:
378 %
379 %      MagickBooleanType WriteMPEGImage(const ImageInfo *image_info,
380 %        Image *image,ExceptionInfo *exception)
381 %
382 %  A description of each parameter follows.
383 %
384 %    o image_info: the image info.
385 %
386 %    o image:  The image.
387 %
388 %    o exception: return any errors or warnings in this structure.
389 %
390 */
CopyDelegateFile(const char * source,const char * destination)391 static MagickBooleanType CopyDelegateFile(const char *source,
392   const char *destination)
393 {
394   int
395     destination_file,
396     source_file;
397 
398   MagickBooleanType
399     status;
400 
401   register size_t
402     i;
403 
404   size_t
405     length,
406     quantum;
407 
408   ssize_t
409     count;
410 
411   struct stat
412     attributes;
413 
414   unsigned char
415     *buffer;
416 
417   /*
418     Return if destination file already exists and is not empty.
419   */
420   assert(source != (const char *) NULL);
421   assert(destination != (char *) NULL);
422   status=GetPathAttributes(destination,&attributes);
423   if ((status != MagickFalse) && (attributes.st_size > 0))
424     return(MagickTrue);
425   /*
426     Copy source file to destination.
427   */
428   destination_file=open_utf8(destination,O_WRONLY | O_BINARY | O_CREAT,S_MODE);
429   if (destination_file == -1)
430     return(MagickFalse);
431   source_file=open_utf8(source,O_RDONLY | O_BINARY,0);
432   if (source_file == -1)
433     {
434       (void) close(destination_file);
435       return(MagickFalse);
436     }
437   quantum=(size_t) MagickMaxBufferExtent;
438   if ((fstat(source_file,&attributes) == 0) && (attributes.st_size > 0))
439     quantum=(size_t) MagickMin((double) attributes.st_size,
440       MagickMaxBufferExtent);
441   buffer=(unsigned char *) AcquireQuantumMemory(quantum,sizeof(*buffer));
442   if (buffer == (unsigned char *) NULL)
443     {
444       (void) close(source_file);
445       (void) close(destination_file);
446       return(MagickFalse);
447     }
448   length=0;
449   for (i=0; ; i+=count)
450   {
451     count=(ssize_t) read(source_file,buffer,quantum);
452     if (count <= 0)
453       break;
454     length=(size_t) count;
455     count=(ssize_t) write(destination_file,buffer,length);
456     if ((size_t) count != length)
457       break;
458   }
459   (void) close(destination_file);
460   (void) close(source_file);
461   buffer=(unsigned char *) RelinquishMagickMemory(buffer);
462   return(i != 0 ? MagickTrue : MagickFalse);
463 }
464 
WriteMPEGImage(const ImageInfo * image_info,Image * image,ExceptionInfo * exception)465 static MagickBooleanType WriteMPEGImage(const ImageInfo *image_info,
466   Image *image,ExceptionInfo *exception)
467 {
468 #define WriteMPEGIntermediateFormat "jpg"
469 
470   char
471     basename[MagickPathExtent],
472     filename[MagickPathExtent];
473 
474   double
475     delay;
476 
477   Image
478     *coalesce_image;
479 
480   ImageInfo
481     *write_info;
482 
483   int
484     file;
485 
486   MagickBooleanType
487     status;
488 
489   register Image
490     *p;
491 
492   register ssize_t
493     i;
494 
495   size_t
496     count,
497     length,
498     scene;
499 
500   unsigned char
501     *blob;
502 
503   /*
504     Open output image file.
505   */
506   assert(image_info != (const ImageInfo *) NULL);
507   assert(image_info->signature == MagickCoreSignature);
508   assert(image != (Image *) NULL);
509   assert(image->signature == MagickCoreSignature);
510   if (image->debug != MagickFalse)
511     (void) LogMagickEvent(TraceEvent,GetMagickModule(),"%s",image->filename);
512   assert(exception != (ExceptionInfo *) NULL);
513   assert(exception->signature == MagickCoreSignature);
514   status=OpenBlob(image_info,image,WriteBinaryBlobMode,exception);
515   if (status == MagickFalse)
516     return(status);
517   (void) CloseBlob(image);
518   /*
519     Write intermediate files.
520   */
521   coalesce_image=CoalesceImages(image,exception);
522   if (coalesce_image == (Image *) NULL)
523     return(MagickFalse);
524   file=AcquireUniqueFileResource(basename);
525   if (file != -1)
526     file=close(file)-1;
527   (void) FormatLocaleString(coalesce_image->filename,MagickPathExtent,"%s",
528     basename);
529   count=0;
530   write_info=CloneImageInfo(image_info);
531   *write_info->magick='\0';
532   for (p=coalesce_image; p != (Image *) NULL; p=GetNextImageInList(p))
533   {
534     char
535       previous_image[MagickPathExtent];
536 
537     blob=(unsigned char *) NULL;
538     length=0;
539     scene=p->scene;
540     delay=100.0*p->delay/MagickMax(1.0*p->ticks_per_second,1.0);
541     for (i=0; i < (ssize_t) MagickMax((1.0*delay+1.0)/3.0,1.0); i++)
542     {
543       p->scene=count;
544       count++;
545       status=MagickFalse;
546       switch (i)
547       {
548         case 0:
549         {
550           Image
551             *frame;
552 
553           (void) FormatLocaleString(p->filename,MagickPathExtent,"%s%.20g.%s",
554             basename,(double) p->scene,WriteMPEGIntermediateFormat);
555           (void) FormatLocaleString(filename,MagickPathExtent,"%s%.20g.%s",
556             basename,(double) p->scene,WriteMPEGIntermediateFormat);
557           (void) FormatLocaleString(previous_image,MagickPathExtent,
558             "%s%.20g.%s",basename,(double) p->scene,
559             WriteMPEGIntermediateFormat);
560           frame=CloneImage(p,0,0,MagickTrue,exception);
561           if (frame == (Image *) NULL)
562             break;
563           status=WriteImage(write_info,frame,exception);
564           frame=DestroyImage(frame);
565           break;
566         }
567         case 1:
568         {
569           blob=(unsigned char *) FileToBlob(previous_image,~0UL,&length,
570             exception);
571         }
572         default:
573         {
574           (void) FormatLocaleString(filename,MagickPathExtent,"%s%.20g.%s",
575             basename,(double) p->scene,WriteMPEGIntermediateFormat);
576           if (length > 0)
577             status=BlobToFile(filename,blob,length,exception);
578           break;
579         }
580       }
581       if (image->debug != MagickFalse)
582         {
583           if (status != MagickFalse)
584             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
585               "%.20g. Wrote %s file for scene %.20g:",(double) i,
586               WriteMPEGIntermediateFormat,(double) p->scene);
587           else
588             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
589               "%.20g. Failed to write %s file for scene %.20g:",(double) i,
590               WriteMPEGIntermediateFormat,(double) p->scene);
591           (void) LogMagickEvent(CoderEvent,GetMagickModule(),"%s",filename);
592         }
593     }
594     p->scene=scene;
595     if (blob != (unsigned char *) NULL)
596       blob=(unsigned char *) RelinquishMagickMemory(blob);
597     if (status == MagickFalse)
598       break;
599   }
600   /*
601     Convert JPEG to MPEG.
602   */
603   (void) CopyMagickString(coalesce_image->magick_filename,basename,
604     MagickPathExtent);
605   (void) CopyMagickString(coalesce_image->filename,basename,MagickPathExtent);
606   GetPathComponent(image_info->filename,ExtensionPath,coalesce_image->magick);
607   if (*coalesce_image->magick == '\0')
608     (void) CopyMagickString(coalesce_image->magick,image->magick,
609       MagickPathExtent);
610   status=InvokeDelegate(write_info,coalesce_image,(char *) NULL,"mpeg:encode",
611     exception);
612   (void) FormatLocaleString(write_info->filename,MagickPathExtent,"%s.%s",
613     write_info->unique,coalesce_image->magick);
614   status=CopyDelegateFile(write_info->filename,image->filename);
615   (void) RelinquishUniqueFileResource(write_info->filename);
616   write_info=DestroyImageInfo(write_info);
617   /*
618     Relinquish resources.
619   */
620   count=0;
621   for (p=coalesce_image; p != (Image *) NULL; p=GetNextImageInList(p))
622   {
623     delay=100.0*p->delay/MagickMax(1.0*p->ticks_per_second,1.0);
624     for (i=0; i < (ssize_t) MagickMax((1.0*delay+1.0)/3.0,1.0); i++)
625     {
626       (void) FormatLocaleString(p->filename,MagickPathExtent,"%s%.20g.%s",
627         basename,(double) count++,WriteMPEGIntermediateFormat);
628       (void) RelinquishUniqueFileResource(p->filename);
629     }
630     (void) CopyMagickString(p->filename,image_info->filename,MagickPathExtent);
631   }
632   (void) RelinquishUniqueFileResource(basename);
633   coalesce_image=DestroyImageList(coalesce_image);
634   if (image->debug != MagickFalse)
635     (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit");
636   return(status);
637 }
638