]> Creatis software - gdcm.git/blob - src/gdcmmpeg2/src/mpeg2dec/mpeg2dec.c
Fix mistypings
[gdcm.git] / src / gdcmmpeg2 / src / mpeg2dec / mpeg2dec.c
1
2 /* mpeg2dec.c, main(), initialization, option processing                    */
3
4 /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
5
6 /*
7  * Disclaimer of Warranty
8  *
9  * These software programs are available to the user without any license fee or
10  * royalty on an "as is" basis.  The MPEG Software Simulation Group disclaims
11  * any and all warranties, whether express, implied, or statuary, including any
12  * implied warranties or merchantability or of fitness for a particular
13  * purpose.  In no event shall the copyright-holder be liable for any
14  * incidental, punitive, or consequential damages of any kind whatsoever
15  * arising from the use of these programs.
16  *
17  * This disclaimer of warranty extends to the user of these programs and user's
18  * customers, employees, agents, transferees, successors, and assigns.
19  *
20  * The MPEG Software Simulation Group does not represent or warrant that the
21  * programs furnished hereunder are free of infringement of any third-party
22  * patents.
23  *
24  * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
25  * are subject to royalty fees to patent holders.  Many of these patents are
26  * general enough such that they are unavoidable regardless of implementation
27  * design.
28  *
29  */
30
31 #include <stdlib.h>
32 #include <ctype.h>
33 #include <fcntl.h>
34
35 #define GLOBAL
36 #include "config.h"
37 #include "global.h"
38
39 /* private prototypes */
40 static int  video_sequence _ANSI_ARGS_((int *framenum));
41 static int  Decode_Bitstream _ANSI_ARGS_((void));
42 static int  Headers _ANSI_ARGS_((void));
43 static void Initialize_Sequence _ANSI_ARGS_((void));
44 static void Initialize_Decoder _ANSI_ARGS_((void));
45 static void DeInitialize_Decoder _ANSI_ARGS_((void));
46 static void Deinitialize_Sequence _ANSI_ARGS_((void));
47 static void Process_Options _ANSI_ARGS_((int argc, char *argv[]));
48
49
50 #if OLD
51 static int  Get_Val _ANSI_ARGS_((char *argv[]));
52 #endif
53
54 /* #define DEBUG */
55
56 static void Clear_Options();
57 #ifdef DEBUG
58 static void Print_Options();
59 #endif
60
61
62 int my_open(char *filename)
63 {
64   abort();
65   return open(filename,O_RDONLY|O_BINARY);
66 }
67
68 off_t my_seek(istream *infile, off_t offset, int whence)
69 {
70 #ifdef FILESTAR
71   return fseek(infile->InFd,offset, whence);
72 #else
73   return lseek(infile->InFd,offset, whence);
74 #endif
75 }
76 ssize_t my_read(istream *infile, void *buf, size_t count)
77 {
78 #ifdef FILESTAR
79   return fread(buf,1,count, infile->InFd);
80 #else
81   return read(infile->InFd,buf,count);
82 #endif
83 }
84
85 int my_close(istream *infile)
86 {
87 #ifdef FILESTAR
88   return fclose(infile->InFd);
89 #else
90   return close(infile->InFd);
91 #endif
92 }
93
94 int my_fopenr(const char *path, const char *mode, istream *os)
95 {
96   FILE *fd = fopen(path, mode);
97   if(fd)
98     {
99     os->InFd = fd;
100     return 1; /*success*/
101     }
102   else
103     os->InFd = NULL;
104   return 0;
105 }
106
107 int my_fopen(const char *path, const char *mode, ostream *os)
108 {
109   FILE *fd = fopen(path, mode);
110   if(fd)
111     {
112     os->OutFd = fd;
113     return 1; /*success*/
114     }
115   else
116     os->OutFd = NULL;
117   return 0;
118 }
119
120 int my_fseek(ostream *stream, long offset, int whence)
121 {
122   return fseek(stream->OutFd, offset, whence);
123 }
124 int my_fseekr(istream *stream, long offset, int whence)
125 {
126   return fseek(stream->InFd, offset, whence);
127 }
128
129 size_t my_fread(void *ptr, size_t size, size_t nmemb, istream *stream)
130 {
131   return fread(ptr, size, nmemb, stream->InFd);
132 }
133
134 size_t my_fwrite(const void *ptr, size_t size, size_t nmemb, ostream *stream)
135 {
136   return fwrite(ptr, size, nmemb, stream->OutFd);
137 }
138
139 int my_fcloser(istream *fp)
140 {
141   return fclose(fp->InFd);
142 }
143 int my_fclose(ostream *fp)
144 {
145   return fclose(fp->OutFd);
146 }
147 #include <stdarg.h>
148
149 int my_printf(const char *format, ...)
150 {
151   va_list argptr;
152   int ret;
153
154   va_start(argptr, format);
155   ret = vprintf(format, argptr);
156   va_end(argptr);
157
158   return ret;
159 }
160
161 int my_sprintf(char *str, const char *format, ...)
162 {
163   va_list argptr;
164   int ret;
165
166   va_start(argptr, format);
167   ret = vsprintf(str,format, argptr);
168   va_end(argptr);
169
170   return ret;
171 }
172
173 int my_fprintf(const char *format, ...)
174 {
175   va_list argptr;
176   int ret;
177
178   va_start(argptr, format);
179   ret = vfprintf(stderr,format, argptr);
180   va_end(argptr);
181
182   return ret;
183 }
184 void my_exit(int status)
185 {
186   exit(status);
187 }
188
189
190 #define GDCM_BUILD_MPEG2DEC
191
192 #ifdef GDCM_BUILD_MPEG2DEC
193 int main(argc,argv)
194 int argc;
195 char *argv[];
196 {
197   int ret, code;
198   istream bos,eos;
199   /*base.open_stream = my_open;*/
200   base.seek_stream = my_seek;
201   base.read_stream = my_read;
202   base.close_stream = my_close;
203
204   Clear_Options();
205
206   /* decode command line arguments */
207   Process_Options(argc,argv);
208
209 #ifdef DEBUG
210   Print_Options();
211 #endif
212
213   ld = &base; /* select base layer context */
214
215   /* open MPEG base layer bitstream file(s) */
216   /* NOTE: this is either a base layer stream or a spatial enhancement stream */
217 /*  if ((base.Infile=open(Main_Bitstream_Filename,O_RDONLY|O_BINARY))<0) */
218   /*base.Infile = ld->open_stream(Main_Bitstream_Filename);*/
219   base.Infile = &bos;
220 #ifdef FILESTAR
221   base.Infile->InFd = fopen(Main_Bitstream_Filename, "rb");
222 #else
223   base.Infile->InFd = open(Main_Bitstream_Filename,O_RDONLY|O_BINARY );
224 #endif
225   if( !base.Infile->InFd)
226   {
227     fprintf(stderr,"Base layer input file %s not found\n", Main_Bitstream_Filename);
228     exit(1);
229   }
230
231
232   if(base.Infile != 0)
233   {
234     Initialize_Buffer();
235
236     if(Show_Bits(8)==0x47)
237     {
238       sprintf(Error_Text,"Decoder currently does not parse transport streams\n");
239       Error(Error_Text);
240     }
241
242     next_start_code();
243     code = Show_Bits(32);
244
245     switch(code)
246     {
247     case SEQUENCE_HEADER_CODE:
248       break;
249     case PACK_START_CODE:
250       System_Stream_Flag = 1;
251     case VIDEO_ELEMENTARY_STREAM:
252       System_Stream_Flag = 1;
253       break;
254     default:
255       sprintf(Error_Text,"Unable to recognize stream type\n");
256       Error(Error_Text);
257       break;
258     }
259
260     /*lseek(base.Infile, 0l, SEEK_SET);*/
261     ld->seek_stream(base.Infile,0l,SEEK_SET);
262     Initialize_Buffer(); 
263   }
264
265   if(base.Infile!=0)
266   {
267     /*lseek(base.Infile, 0l, SEEK_SET);*/
268     ld->seek_stream(base.Infile,0l,SEEK_SET);
269   }
270
271   Initialize_Buffer(); 
272
273   if(Two_Streams)
274   {
275     ld = &enhan; /* select enhancement layer context */
276
277     /*if ((enhan.Infile = open(Enhancement_Layer_Bitstream_Filename,O_RDONLY|O_BINARY))<0)*/
278     /*enhan.Infile = ld->open_stream(Enhancement_Layer_Bitstream_Filename);*/
279     enhan.Infile = &eos;
280 #ifdef FILESTAR
281     enhan.Infile->InFd = fopen(Main_Bitstream_Filename, "rb");
282 #else
283     enhan.Infile->InFd = open(Enhancement_Layer_Bitstream_Filename,O_RDONLY|O_BINARY);
284 #endif
285     if (enhan.Infile->InFd)
286     {
287       sprintf(Error_Text,"enhancment layer bitstream file %s not found\n",
288         Enhancement_Layer_Bitstream_Filename);
289
290       Error(Error_Text);
291     }
292
293     Initialize_Buffer();
294     ld = &base;
295   }
296
297   Initialize_Decoder();
298
299   ret = Decode_Bitstream();
300
301   /*close(base.Infile);*/
302   ld->close_stream(base.Infile);
303
304   if (Two_Streams)
305     /*close(enhan.Infile);*/
306     ld->close_stream(enhan.Infile);
307
308   DeInitialize_Decoder();
309
310   return ret;
311 }
312 #endif /*GDCM_BUILD_MPEG2DEC*/
313
314 /* IMPLEMENTATION specific routines */
315 static void Initialize_Decoder()
316 {
317   int i;
318
319   /* Clip table */
320   if (!(Clip=(unsigned char *)malloc(1024)))
321     Error("Clip[] malloc failed\n");
322
323   Clip += 384;
324
325   for (i=-384; i<640; i++)
326     Clip[i] = (i<0) ? 0 : ((i>255) ? 255 : i);
327
328   /* IDCT */
329   if (Reference_IDCT_Flag)
330     Initialize_Reference_IDCT();
331   else
332     Initialize_Fast_IDCT();
333
334 }
335
336 static void DeInitialize_Decoder()
337 {
338   free(Clip-384); /* I love magic number */
339 }
340
341 /* mostly IMPLEMENTAION specific routines */
342 static void Initialize_Sequence()
343 {
344   int cc, size;
345   static int Table_6_20[3] = {6,8,12};
346
347   /* check scalability mode of enhancement layer */
348   if (Two_Streams && (enhan.scalable_mode!=SC_SNR) && (base.scalable_mode!=SC_DP))
349     Error("unsupported scalability mode\n");
350
351   /* force MPEG-1 parameters for proper decoder behavior */
352   /* see ISO/IEC 13818-2 section D.9.14 */
353   if (!base.MPEG2_Flag)
354   {
355     progressive_sequence = 1;
356     progressive_frame = 1;
357     picture_structure = FRAME_PICTURE;
358     frame_pred_frame_dct = 1;
359     chroma_format = CHROMA420;
360     matrix_coefficients = 5;
361   }
362
363   /* round to nearest multiple of coded macroblocks */
364   /* ISO/IEC 13818-2 section 6.3.3 sequence_header() */
365   mb_width = (horizontal_size+15)/16;
366   mb_height = (base.MPEG2_Flag && !progressive_sequence) ? 2*((vertical_size+31)/32)
367                                         : (vertical_size+15)/16;
368
369   Coded_Picture_Width = 16*mb_width;
370   Coded_Picture_Height = 16*mb_height;
371
372   /* ISO/IEC 13818-2 sections 6.1.1.8, 6.1.1.9, and 6.1.1.10 */
373   Chroma_Width = (chroma_format==CHROMA444) ? Coded_Picture_Width
374                                            : Coded_Picture_Width>>1;
375   Chroma_Height = (chroma_format!=CHROMA420) ? Coded_Picture_Height
376                                             : Coded_Picture_Height>>1;
377   
378   /* derived based on Table 6-20 in ISO/IEC 13818-2 section 6.3.17 */
379   block_count = Table_6_20[chroma_format-1];
380
381   for (cc=0; cc<3; cc++)
382   {
383     if (cc==0)
384       size = Coded_Picture_Width*Coded_Picture_Height;
385     else
386       size = Chroma_Width*Chroma_Height;
387
388     if (!(backward_reference_frame[cc] = (unsigned char *)malloc(size)))
389       Error("backward_reference_frame[] malloc failed\n");
390
391     if (!(forward_reference_frame[cc] = (unsigned char *)malloc(size)))
392       Error("forward_reference_frame[] malloc failed\n");
393
394     if (!(auxframe[cc] = (unsigned char *)malloc(size)))
395       Error("auxframe[] malloc failed\n");
396
397     if(Ersatz_Flag)
398       if (!(substitute_frame[cc] = (unsigned char *)malloc(size)))
399         Error("substitute_frame[] malloc failed\n");
400
401
402     if (base.scalable_mode==SC_SPAT)
403     {
404       /* this assumes lower layer is 4:2:0 */
405       if (!(llframe0[cc] = (unsigned char *)malloc((lower_layer_prediction_horizontal_size*lower_layer_prediction_vertical_size)/(cc?4:1))))
406         Error("llframe0 malloc failed\n");
407       if (!(llframe1[cc] = (unsigned char *)malloc((lower_layer_prediction_horizontal_size*lower_layer_prediction_vertical_size)/(cc?4:1))))
408         Error("llframe1 malloc failed\n");
409     }
410   }
411
412   /* SCALABILITY: Spatial */
413   if (base.scalable_mode==SC_SPAT)
414   {
415     if (!(lltmp = (short *)malloc(lower_layer_prediction_horizontal_size*((lower_layer_prediction_vertical_size*vertical_subsampling_factor_n)/vertical_subsampling_factor_m)*sizeof(short))))
416       Error("lltmp malloc failed\n");
417   }
418
419 #ifdef DISPLAY
420   if (Output_Type==T_X11)
421   {
422     Initialize_Display_Process("");
423     Initialize_Dither_Matrix();
424   }
425 #endif /* DISPLAY */
426
427 }
428
429 void Error(text)
430 const char *text;
431 {
432   fprintf(stderr,text);
433   exit(1);
434 }
435
436 /* Trace_Flag output */
437 void Print_Bits(code,bits,len)
438 int code,bits,len;
439 {
440   int i;
441   for (i=0; i<len; i++)
442     printf("%d",(code>>(bits-1-i))&1);
443 }
444
445
446
447 /* option processing */
448 static void Process_Options(argc,argv)
449 int argc;                  /* argument count  */
450 char *argv[];              /* argument vector */
451 {
452   int i, LastArg, NextArg;
453
454   /* at least one argument should be present */
455   if (argc<2)
456   {
457     printf("\n%s, %s\n",Version,Author);
458     printf("Usage:  mpeg2decode {options}\n"
459 "Options: -b  file  main bitstream (base or spatial enhancement layer)\n"
460 "         -cn file  conformance report (n: level)\n"
461 "         -e  file  enhancement layer bitstream (SNR or Data Partitioning)\n"
462 "         -f        store/display interlaced video in frame format\n"
463 "         -g        concatenated file format for substitution method (-x)\n"
464 "         -in file  information & statistics report  (n: level)\n"
465 "         -l  file  file name pattern for lower layer sequence\n");
466 printf("                   (for spatial scalability)\n"
467 "         -on file  output format (0:YUV 1:SIF 2:TGA 3:PPM 4:X11 5:X11HiQ)\n"
468 "         -q        disable warnings to stderr\n"
469 "         -r        use double precision reference IDCT\n"
470 "         -t        enable low level tracing to stdout\n"
471 "         -u  file  print user_data to stdio or file\n"
472 "         -vn       verbose output (n: level)\n"
473 "         -x  file  filename pattern of picture substitution sequence\n\n");
474 printf("File patterns:  for sequential filenames, \"printf\" style, e.g. rec%%d\n"
475 "                 or rec%%d%%c for fieldwise storage\n"
476 "Levels:        0:none 1:sequence 2:picture 3:slice 4:macroblock 5:block\n\n"
477 "Example:       mpeg2decode -b bitstream.mpg -f -r -o0 rec%%d\n"
478 "         \n");
479     exit(0);
480   }
481
482
483   Output_Type = -1;
484   i = 1;
485
486   /* command-line options are proceeded by '-' */
487
488   while(i < argc)
489   {
490     /* check if this is the last argument */
491     LastArg = ((argc-i)==1);
492
493     /* parse ahead to see if another flag immediately follows current
494        argument (this is used to tell if a filename is missing) */
495     if(!LastArg)
496       NextArg = (argv[i+1][0]=='-');
497     else
498       NextArg = 0;
499
500     /* second character, [1], after '-' is the switch */
501     if(argv[i][0]=='-')
502     {
503       switch(toupper(argv[i][1]))
504       {
505         /* third character. [2], is the value */
506       case 'B':
507         Main_Bitstream_Flag = 1;
508
509         if(NextArg || LastArg)
510         {
511           printf("ERROR: -b must be followed the main bitstream filename\n");
512         }
513         else
514           Main_Bitstream_Filename = argv[++i]; 
515
516         break;
517
518
519       case 'C':
520
521 #ifdef VERIFY
522         Verify_Flag = atoi(&argv[i][2]); 
523
524         if((Verify_Flag < NO_LAYER) || (Verify_Flag > ALL_LAYERS))
525         {
526           printf("ERROR: -c level (%d) out of range [%d,%d]\n",
527             Verify_Flag, NO_LAYER, ALL_LAYERS);
528           exit(ERROR);
529         }
530 #else  /* VERIFY */
531         printf("This program not compiled for Verify_Flag option\n");
532 #endif /* VERIFY */
533         break;
534
535       case 'E':
536         Two_Streams = 1; /* either Data Partitioning (DP) or SNR Scalability enhancment */
537                    
538         if(NextArg || LastArg)
539         {
540           printf("ERROR: -e must be followed by filename\n");
541           exit(ERROR);
542         }
543         else
544           Enhancement_Layer_Bitstream_Filename = argv[++i]; 
545
546         break;
547
548
549       case 'F':
550         Frame_Store_Flag = 1;
551         break;
552
553       case 'G':
554         Big_Picture_Flag = 1;
555         break;
556
557
558       case 'I':
559 #ifdef VERIFY
560         Stats_Flag = atoi(&argv[i][2]); 
561 #else /* VERIFY */
562         printf("WARNING: This program not compiled for -i option\n");
563 #endif /* VERIFY */     
564         break;
565     
566       case 'L':  /* spatial scalability flag */
567         Spatial_Flag = 1;
568
569        if(NextArg || LastArg)
570        {
571          printf("ERROR: -l must be followed by filename\n");
572          exit(ERROR);
573        }
574        else
575          Lower_Layer_Picture_Filename = argv[++i]; 
576
577         break;
578
579       case 'O':
580   
581         Output_Type = atoi(&argv[i][2]); 
582   
583         if((Output_Type==4) || (Output_Type==5))
584           Output_Picture_Filename = "";  /* no need of filename */
585         else if(NextArg || LastArg)  
586         {
587           printf("ERROR: -o must be followed by filename\n");
588           exit(ERROR);
589         }
590         else
591         /* filename is separated by space, so it becomes the next argument */
592           Output_Picture_Filename = argv[++i]; 
593
594 #ifdef DISPLAY
595         if (Output_Type==T_X11HIQ)
596         {
597           hiQdither = 1;
598           Output_Type=T_X11;
599         }
600 #endif /* DISPLAY */
601         break;
602
603       case 'Q':
604         Quiet_Flag = 1;
605         break;
606
607       case 'R':
608         Reference_IDCT_Flag = 1;
609         break;
610     
611       case 'T':
612 #ifdef TRACE
613         Trace_Flag = 1;
614 #else /* TRACE */
615         printf("WARNING: This program not compiled for -t option\n");
616 #endif /* TRACE */
617         break;
618
619       case 'U':
620         User_Data_Flag = 1;
621
622       case 'V':
623 #ifdef VERBOSE
624         Verbose_Flag = atoi(&argv[i][2]); 
625 #else /* VERBOSE */
626         printf("This program not compiled for -v option\n");
627 #endif /* VERBOSE */
628         break;
629
630
631       case 'X':
632         Ersatz_Flag = 1;
633
634        if(NextArg || LastArg)
635        {
636          printf("ERROR: -x must be followed by filename\n"); 
637          exit(ERROR);
638        }
639        else
640         Substitute_Picture_Filename = argv[++i]; 
641
642         break;
643
644
645
646       default:
647         fprintf(stderr,"undefined option -%c ignored. Exiting program\n", 
648           argv[i][1]);
649
650         exit(ERROR);
651     
652       } /* switch() */
653     } /* if argv[i][0] == '-' */
654     
655     i++;
656
657     /* check for bitstream filename argument (there must always be one, at the very end
658      of the command line arguments */
659
660   } /* while() */
661
662
663   /* options sense checking */
664
665   if(Main_Bitstream_Flag!=1)
666   {
667     printf("There must be a main bitstream specified (-b filename)\n");
668   }
669
670   /* force display process to show frame pictures */
671   if((Output_Type==4 || Output_Type==5) && Frame_Store_Flag)
672     Display_Progressive_Flag = 1;
673   else
674     Display_Progressive_Flag = 0;
675
676 #ifdef VERIFY
677   /* parse the bitstream, do not actually decode it completely */
678   
679
680 #if 0
681   if(Output_Type==-1)
682   {
683     Decode_Layer = Verify_Flag;
684     printf("FYI: Decoding bitstream elements up to: %s\n", 
685       Layer_Table[Decode_Layer]);
686   }
687   else
688 #endif
689     Decode_Layer = ALL_LAYERS;
690
691 #endif /* VERIFY */
692
693   /* no output type specified */
694   if(Output_Type==-1)
695   {
696     Output_Type = 9; 
697     Output_Picture_Filename = "";
698   }
699
700
701 #ifdef DISPLAY
702   if (Output_Type==T_X11)
703   {
704     if(Frame_Store_Flag)
705       Display_Progressive_Flag = 1;
706     else
707       Display_Progressive_Flag = 0;
708
709     Frame_Store_Flag = 1; /* to avoid calling dither() twice */
710   }
711 #endif
712
713
714 }
715
716
717 #ifdef OLD
718 /* 
719    this is an old routine used to convert command line arguments
720    into integers 
721 */
722 static int Get_Val(argv)
723 char *argv[];
724 {
725   int val;
726
727   if (sscanf(argv[1]+2,"%d",&val)!=1)
728     return 0;
729
730   while (isdigit(argv[1][2]))
731     argv[1]++;
732
733   return val;
734 }
735 #endif
736
737
738
739 static int Headers()
740 {
741   int ret;
742
743   ld = &base;
744   
745
746   /* return when end of sequence (0) or picture
747      header has been parsed (1) */
748
749   ret = Get_Hdr();
750
751
752   if (Two_Streams)
753   {
754     ld = &enhan;
755     if (Get_Hdr()!=ret && !Quiet_Flag)
756       fprintf(stderr,"streams out of sync\n");
757     ld = &base;
758   }
759
760   return ret;
761 }
762
763
764
765 static int Decode_Bitstream()
766 {
767   int ret;
768   int Bitstream_Framenum;
769
770   Bitstream_Framenum = 0;
771
772   for(;;)
773     {
774
775 #ifdef VERIFY
776     Clear_Verify_Headers();
777 #endif /* VERIFY */
778
779     ret = Headers();
780
781     if(ret==1)
782       {
783       /*ret =*/ video_sequence(&Bitstream_Framenum);
784       }
785     else
786       return(ret);
787     }
788
789 }
790
791
792 static void Deinitialize_Sequence()
793 {
794   int i;
795
796   /* First cleanup the static buffer in store.c */
797   FreeStaticBuffer();
798
799   /* clear flags */
800   base.MPEG2_Flag=0;
801
802   for(i=0;i<3;i++)
803   {
804     free(backward_reference_frame[i]);
805     free(forward_reference_frame[i]);
806     free(auxframe[i]);
807
808     if (base.scalable_mode==SC_SPAT)
809     {
810      free(llframe0[i]);
811      free(llframe1[i]);
812     }
813   }
814
815   if (base.scalable_mode==SC_SPAT)
816     free(lltmp);
817
818 #ifdef DISPLAY
819   if (Output_Type==T_X11) 
820     Terminate_Display_Process();
821 #endif
822 }
823
824
825 static int video_sequence(Bitstream_Framenumber)
826 int *Bitstream_Framenumber;
827 {
828   int Bitstream_Framenum;
829   int Sequence_Framenum;
830   int Return_Value;
831
832   Bitstream_Framenum = *Bitstream_Framenumber;
833   Sequence_Framenum=0;
834
835   Initialize_Sequence();
836
837   /* decode picture whose header has already been parsed in 
838      Decode_Bitstream() */
839
840
841   Decode_Picture(Bitstream_Framenum, Sequence_Framenum);
842
843   /* update picture numbers */
844   if (!Second_Field)
845   {
846     Bitstream_Framenum++;
847     Sequence_Framenum++;
848   }
849
850   /* loop through the rest of the pictures in the sequence */
851   while ((Return_Value=Headers()))
852   {
853     Decode_Picture(Bitstream_Framenum, Sequence_Framenum);
854
855     if (!Second_Field)
856     {
857       Bitstream_Framenum++;
858       Sequence_Framenum++;
859     }
860   }
861
862   /* put last frame */
863   if (Sequence_Framenum!=0)
864   {
865     Output_Last_Frame_of_Sequence(Bitstream_Framenum);
866   }
867
868   Deinitialize_Sequence();
869
870 #ifdef VERIFY
871     Clear_Verify_Headers();
872 #endif /* VERIFY */
873
874   *Bitstream_Framenumber = Bitstream_Framenum;
875   return(Return_Value);
876 }
877
878
879
880 static void Clear_Options()
881 {
882   Verbose_Flag = 0;
883   Output_Type = 0;
884   Output_Picture_Filename = " ";
885   hiQdither  = 0;
886   Output_Type = 0;
887   Frame_Store_Flag = 0;
888   Spatial_Flag = 0;
889   Lower_Layer_Picture_Filename = " ";
890   Reference_IDCT_Flag = 0;
891   Trace_Flag = 0;
892   Quiet_Flag = 0;
893   Ersatz_Flag = 0;
894   Substitute_Picture_Filename  = " ";
895   Two_Streams = 0;
896   Enhancement_Layer_Bitstream_Filename = " ";
897   Big_Picture_Flag = 0;
898   Main_Bitstream_Flag = 0;
899   Main_Bitstream_Filename = " ";
900   Verify_Flag = 0;
901   Stats_Flag  = 0;
902   User_Data_Flag = 0; 
903 }
904
905
906 #ifdef DEBUG
907 static void Print_Options()
908 {
909   
910   printf("Verbose_Flag                         = %d\n", Verbose_Flag);
911   printf("Output_Type                          = %d\n", Output_Type);
912   printf("Output_Picture_Filename              = %s\n", Output_Picture_Filename);
913   printf("hiQdither                            = %d\n", hiQdither);
914   printf("Output_Type                          = %d\n", Output_Type);
915   printf("Frame_Store_Flag                     = %d\n", Frame_Store_Flag);
916   printf("Spatial_Flag                         = %d\n", Spatial_Flag);
917   printf("Lower_Layer_Picture_Filename         = %s\n", Lower_Layer_Picture_Filename);
918   printf("Reference_IDCT_Flag                  = %d\n", Reference_IDCT_Flag);
919   printf("Trace_Flag                           = %d\n", Trace_Flag);
920   printf("Quiet_Flag                           = %d\n", Quiet_Flag);
921   printf("Ersatz_Flag                          = %d\n", Ersatz_Flag);
922   printf("Substitute_Picture_Filename          = %s\n", Substitute_Picture_Filename);
923   printf("Two_Streams                          = %d\n", Two_Streams);
924   printf("Enhancement_Layer_Bitstream_Filename = %s\n", Enhancement_Layer_Bitstream_Filename);
925   printf("Big_Picture_Flag                     = %d\n", Big_Picture_Flag);
926   printf("Main_Bitstream_Flag                  = %d\n", Main_Bitstream_Flag);
927   printf("Main_Bitstream_Filename              = %s\n", Main_Bitstream_Filename);
928   printf("Verify_Flag                          = %d\n", Verify_Flag);
929   printf("Stats_Flag                           = %d\n", Stats_Flag);
930   printf("User_Data_Flag                       = %d\n", User_Data_Flag);
931
932 }
933 #endif
934