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