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