]> granicus.if.org Git - imagemagick/blob - coders/mpeg.c
(no commit message)
[imagemagick] / coders / mpeg.c
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 %                                John Cristy                                  %
17 %                                 July 1999                                   %
18 %                                                                             %
19 %                                                                             %
20 %  Copyright 1999-2010 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 %    http://www.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 "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"
63 \f
64 /*
65   Forward declarations.
66 */
67 static MagickBooleanType
68   WriteMPEGImage(const ImageInfo *image_info,Image *image);
69 \f
70 /*
71 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
72 %                                                                             %
73 %                                                                             %
74 %                                                                             %
75 %   I s A V I                                                                 %
76 %                                                                             %
77 %                                                                             %
78 %                                                                             %
79 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
80 %
81 %  IsAVI() returns MagickTrue if the image format type, identified by the
82 %  magick string, is Audio/Video Interleaved file format.
83 %
84 %  The format of the IsAVI method is:
85 %
86 %      unsigned long IsAVI(const unsigned char *magick,const size_t length)
87 %
88 %  A description of each parameter follows:
89 %
90 %    o magick: compare image format pattern against these bytes.
91 %
92 %    o length: Specifies the length of the magick string.
93 %
94 */
95 static MagickBooleanType IsAVI(const unsigned char *magick,const size_t length)
96 {
97   if (length < 4)
98     return(MagickFalse);
99   if (memcmp(magick,"RIFF",4) == 0)
100     return(MagickTrue);
101   return(MagickFalse);
102 }
103 \f
104 /*
105 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
106 %                                                                             %
107 %                                                                             %
108 %                                                                             %
109 %   I s M P E G                                                               %
110 %                                                                             %
111 %                                                                             %
112 %                                                                             %
113 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
114 %
115 %  IsMPEG() returns MagickTrue if the image format type, identified by the
116 %  magick string, is MPEG.
117 %
118 %  The format of the IsMPEG method is:
119 %
120 %      MagickBooleanType IsMPEG(const unsigned char *magick,const size_t length)
121 %
122 %  A description of each parameter follows:
123 %
124 %    o magick: compare image format pattern against these bytes.
125 %
126 %    o length: Specifies the length of the magick string.
127 %
128 */
129 static MagickBooleanType IsMPEG(const unsigned char *magick,const size_t length)
130 {
131   if (length < 4)
132     return(MagickFalse);
133   if (memcmp(magick,"\000\000\001\263",4) == 0)
134     return(MagickTrue);
135   return(MagickFalse);
136 }
137 \f
138 /*
139 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
140 %                                                                             %
141 %                                                                             %
142 %                                                                             %
143 %   R e a d M P E G I m a g e                                                 %
144 %                                                                             %
145 %                                                                             %
146 %                                                                             %
147 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
148 %
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.
152 %
153 %  The format of the ReadMPEGImage method is:
154 %
155 %      Image *ReadMPEGImage(const ImageInfo *image_info,
156 %        ExceptionInfo *exception)
157 %
158 %  A description of each parameter follows:
159 %
160 %    o image_info: the image info.
161 %
162 %    o exception: return any errors or warnings in this structure.
163 %
164 */
165 static Image *ReadMPEGImage(const ImageInfo *image_info,
166   ExceptionInfo *exception)
167 {
168 #define ReadMPEGIntermediateFormat "pam"
169
170   Image
171     *image,
172     *images;
173
174   ImageInfo
175     *read_info;
176
177   MagickBooleanType
178     status;
179
180   /*
181     Open image file.
182   */
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)
193     {
194       image=DestroyImageList(image);
195       return((Image *) NULL);
196     }
197   (void) CloseBlob(image);
198   (void) DestroyImageList(image);
199   /*
200     Convert MPEG to PAM with delegate.
201   */
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);
211   return(images);
212 }
213 \f
214 /*
215 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
216 %                                                                             %
217 %                                                                             %
218 %                                                                             %
219 %   R e g i s t e r M P E G I m a g e                                         %
220 %                                                                             %
221 %                                                                             %
222 %                                                                             %
223 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
224 %
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.
231 %
232 %  The format of the RegisterMPEGImage method is:
233 %
234 %      unsigned long RegisterMPEGImage(void)
235 %
236 */
237 ModuleExport unsigned long RegisterMPEGImage(void)
238 {
239   MagickInfo
240     *entry;
241
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);
305 }
306 \f
307 /*
308 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
309 %                                                                             %
310 %                                                                             %
311 %                                                                             %
312 %   U n r e g i s t e r M P E G I m a g e                                     %
313 %                                                                             %
314 %                                                                             %
315 %                                                                             %
316 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
317 %
318 %  UnregisterMPEGImage() removes format registrations made by the
319 %  BIM module from the list of supported formats.
320 %
321 %  The format of the UnregisterBIMImage method is:
322 %
323 %      UnregisterMPEGImage(void)
324 %
325 */
326 ModuleExport void UnregisterMPEGImage(void)
327 {
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");
336 }
337 \f
338 /*
339 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
340 %                                                                             %
341 %                                                                             %
342 %                                                                             %
343 %   W r i t e M P E G I m a g e                                               %
344 %                                                                             %
345 %                                                                             %
346 %                                                                             %
347 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
348 %
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.
352 %
353 %  The format of the WriteMPEGImage method is:
354 %
355 %      MagickBooleanType WriteMPEGImage(const ImageInfo *image_info,
356 %        Image *image)
357 %
358 %  A description of each parameter follows.
359 %
360 %    o image_info: the image info.
361 %
362 %    o image:  The image.
363 %
364 */
365
366 static inline double MagickMax(const double x,const double y)
367 {
368   if (x > y)
369     return(x);
370   return(y);
371 }
372
373 static inline double MagickMin(const double x,const double y)
374 {
375   if (x < y)
376     return(x);
377   return(y);
378 }
379
380 static MagickBooleanType CopyDelegateFile(const char *source,
381   const char *destination)
382 {
383   int
384     destination_file,
385     source_file;
386
387   MagickBooleanType
388     status;
389
390   register size_t
391     i;
392
393   size_t
394     length,
395     quantum;
396
397   ssize_t
398     count;
399
400   struct stat
401     attributes;
402
403   unsigned char
404     *buffer;
405
406   /*
407     Return if destination file already exists and is not empty.
408   */
409   assert(source != (const char *) NULL);
410   assert(destination != (char *) NULL);
411   status=GetPathAttributes(destination,&attributes);
412   if ((status != MagickFalse) && (attributes.st_size != 0))
413     return(MagickTrue);
414   /*
415     Copy source file to destination.
416   */
417   destination_file=open(destination,O_WRONLY | O_BINARY | O_CREAT,S_MODE);
418   if (destination_file == -1)
419     return(MagickFalse);
420   source_file=open(source,O_RDONLY | O_BINARY);
421   if (source_file == -1)
422     {
423       (void) close(destination_file);
424       return(MagickFalse);
425     }
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)
432     {
433       (void) close(source_file);
434       (void) close(destination_file);
435       return(MagickFalse);
436     }
437   length=0;
438   for (i=0; ; i+=count)
439   {
440     count=(ssize_t) read(source_file,buffer,quantum);
441     if (count <= 0)
442       break;
443     length=(size_t) count;
444     count=(ssize_t) write(destination_file,buffer,length);
445     if ((size_t) count != length)
446       break;
447   }
448   (void) close(destination_file);
449   (void) close(source_file);
450   buffer=(unsigned char *) RelinquishMagickMemory(buffer);
451   return(i != 0 ? MagickTrue : MagickFalse);
452 }
453
454 static MagickBooleanType WriteMPEGImage(const ImageInfo *image_info,
455   Image *image)
456 {
457 #define WriteMPEGIntermediateFormat "jpg"
458
459   char
460     basename[MaxTextExtent],
461     filename[MaxTextExtent];
462
463   double
464     delay;
465
466   Image
467     *coalesce_image;
468
469   ImageInfo
470     *write_info;
471
472   int
473     file;
474
475   MagickBooleanType
476     status;
477
478   register Image
479     *p;
480
481   register long
482     i;
483
484   size_t
485     length;
486
487   unsigned char
488     *blob;
489
490   unsigned long
491     count,
492     scene;
493
494   /*
495     Open output image file.
496   */
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)
505     return(status);
506   (void) CloseBlob(image);
507   /*
508     Write intermediate files.
509   */
510   coalesce_image=CoalesceImages(image,&image->exception);
511   if (coalesce_image == (Image *) NULL)
512     return(MagickFalse);
513   file=AcquireUniqueFileResource(basename);
514   if (file != -1)
515     file=close(file)-1;
516   (void) FormatMagickString(coalesce_image->filename,MaxTextExtent,"%s",
517     basename);
518   count=0;
519   write_info=CloneImageInfo(image_info);
520   for (p=coalesce_image; p != (Image *) NULL; p=GetNextImageInList(p))
521   {
522     char
523       previous_image[MaxTextExtent];
524
525     blob=(unsigned char *) NULL;
526     length=0;
527     scene=p->scene;
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++)
530     {
531       p->scene=count;
532       count++;
533       status=MagickFalse;
534       switch (i)
535       {
536         case 0:
537         {
538           Image
539             *frame;
540
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)
549             break;
550           status=WriteImage(write_info,frame);
551           frame=DestroyImage(frame);
552           break;
553         }
554         case 1:
555         {
556           blob=(unsigned char *) FileToBlob(previous_image,~0UL,&length,
557             &image->exception);
558         }
559         default:
560         {
561           (void) FormatMagickString(filename,MaxTextExtent,"%s%lu.%s",
562             basename,p->scene,WriteMPEGIntermediateFormat);
563           if (length > 0)
564             status=BlobToFile(filename,blob,length,&image->exception);
565           break;
566         }
567       }
568       if (image->debug != MagickFalse)
569         {
570           if (status != MagickFalse)
571             (void) LogMagickEvent(CoderEvent,GetMagickModule(),
572               "%lu. Wrote %s file for scene %lu:",i,WriteMPEGIntermediateFormat,
573               p->scene);
574           else
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",
579             filename);
580         }
581     }
582     p->scene=scene;
583     if (blob != (unsigned char *) NULL)
584       blob=(unsigned char *) RelinquishMagickMemory(blob);
585     if (status == MagickFalse)
586       break;
587   }
588   /*
589     Convert JPEG to MPEG.
590   */
591   (void) CopyMagickString(coalesce_image->magick_filename,basename,
592     MaxTextExtent);
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",
598     &image->exception);
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);
604   /*
605     Relinquish resources.
606   */
607   count=0;
608   for (p=coalesce_image; p != (Image *) NULL; p=GetNextImageInList(p))
609   {
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++)
612     {
613       (void) FormatMagickString(p->filename,MaxTextExtent,"%s%lu.%s",
614         basename,count++,WriteMPEGIntermediateFormat);
615       (void) RelinquishUniqueFileResource(p->filename);
616     }
617     (void) CopyMagickString(p->filename,image_info->filename,MaxTextExtent);
618   }
619   (void) RelinquishUniqueFileResource(basename);
620   coalesce_image=DestroyImageList(coalesce_image);
621   if (image->debug != MagickFalse)
622     (void) LogMagickEvent(CoderEvent,GetMagickModule(),"exit");
623   return(status);
624 }