2 /* mpeg2dec.c, main(), initialization, option processing */
4 /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
7 * Disclaimer of Warranty
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.
17 * This disclaimer of warranty extends to the user of these programs and user's
18 * customers, employees, agents, transferees, successors, and assigns.
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
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
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[]));
51 static int Get_Val _ANSI_ARGS_((char *argv[]));
56 static void Clear_Options();
58 static void Print_Options();
62 int my_open(char *filename)
65 return open(filename,O_RDONLY|O_BINARY);
68 off_t my_seek(istream *infile, off_t offset, int whence)
71 return fseek(infile->InFd,offset, whence);
73 return lseek(infile->InFd,offset, whence);
76 ssize_t my_read(istream *infile, void *buf, size_t count)
79 size_t r = fread(buf,1,count, infile->InFd);
81 ssize_t r = read(infile->InFd,buf,count);
83 printf( "%d , %d\n", count , r);
87 int my_close(istream *infile)
90 return fclose(infile->InFd);
92 return close(infile->InFd);
96 int my_fopenr(const char *path, const char *mode, istream *os)
98 FILE *fd = fopen(path, mode);
102 return 1; /*success*/
109 int my_fopen(const char *path, const char *mode, ostream *os)
111 FILE *fd = fopen(path, mode);
115 return 1; /*success*/
122 int my_fseek(ostream *stream, long offset, int whence)
124 return fseek(stream->OutFd, offset, whence);
126 int my_fseekr(istream *stream, long offset, int whence)
128 return fseek(stream->InFd, offset, whence);
131 size_t my_fread(void *ptr, size_t size, size_t nmemb, istream *stream)
133 return fread(ptr, size, nmemb, stream->InFd);
136 size_t my_fwrite(const void *ptr, size_t size, size_t nmemb, ostream *stream)
138 return fwrite(ptr, size, nmemb, stream->OutFd);
141 int my_fcloser(istream *fp)
143 return fclose(fp->InFd);
145 int my_fclose(ostream *fp)
147 return fclose(fp->OutFd);
151 int my_printf(const char *format, ...)
156 va_start(argptr, format);
157 ret = vprintf(format, argptr);
163 int my_sprintf(char *str, const char *format, ...)
168 va_start(argptr, format);
169 ret = vsprintf(str,format, argptr);
175 int my_fprintf(const char *format, ...)
180 va_start(argptr, format);
181 ret = vfprintf(stderr,format, argptr);
186 void my_exit(int status)
192 #define GDCM_BUILD_MPEG2DEC
194 #ifdef GDCM_BUILD_MPEG2DEC
201 /*base.open_stream = my_open;*/
202 base.seek_stream = my_seek;
203 base.read_stream = my_read;
204 base.close_stream = my_close;
208 /* decode command line arguments */
209 Process_Options(argc,argv);
215 ld = &base; /* select base layer context */
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);*/
223 base.Infile->InFd = fopen(Main_Bitstream_Filename, "rb");
225 base.Infile->InFd = open(Main_Bitstream_Filename,O_RDONLY|O_BINARY );
227 if( !base.Infile->InFd)
229 fprintf(stderr,"Base layer input file %s not found\n", Main_Bitstream_Filename);
238 if(Show_Bits(8)==0x47)
240 sprintf(Error_Text,"Decoder currently does not parse transport streams\n");
245 code = Show_Bits(32);
249 case SEQUENCE_HEADER_CODE:
251 case PACK_START_CODE:
252 System_Stream_Flag = 1;
253 case VIDEO_ELEMENTARY_STREAM:
254 System_Stream_Flag = 1;
257 sprintf(Error_Text,"Unable to recognize stream type\n");
262 /*lseek(base.Infile, 0l, SEEK_SET);*/
263 ld->seek_stream(base.Infile,0l,SEEK_SET);
269 /*lseek(base.Infile, 0l, SEEK_SET);*/
270 ld->seek_stream(base.Infile,0l,SEEK_SET);
277 ld = &enhan; /* select enhancement layer context */
279 /*if ((enhan.Infile = open(Enhancement_Layer_Bitstream_Filename,O_RDONLY|O_BINARY))<0)*/
280 /*enhan.Infile = ld->open_stream(Enhancement_Layer_Bitstream_Filename);*/
283 enhan.Infile->InFd = fopen(Main_Bitstream_Filename, "rb");
285 enhan.Infile->InFd = open(Enhancement_Layer_Bitstream_Filename,O_RDONLY|O_BINARY);
287 if (enhan.Infile->InFd)
289 sprintf(Error_Text,"enhancment layer bitstream file %s not found\n",
290 Enhancement_Layer_Bitstream_Filename);
299 Initialize_Decoder();
301 ret = Decode_Bitstream();
303 /*close(base.Infile);*/
304 ld->close_stream(base.Infile);
307 /*close(enhan.Infile);*/
308 ld->close_stream(enhan.Infile);
310 DeInitialize_Decoder();
314 #endif /*GDCM_BUILD_MPEG2DEC*/
316 /* IMPLEMENTATION specific routines */
317 static void Initialize_Decoder()
322 if (!(Clip=(unsigned char *)malloc(1024)))
323 Error("Clip[] malloc failed\n");
327 for (i=-384; i<640; i++)
328 Clip[i] = (i<0) ? 0 : ((i>255) ? 255 : i);
331 if (Reference_IDCT_Flag)
332 Initialize_Reference_IDCT();
334 Initialize_Fast_IDCT();
338 static void DeInitialize_Decoder()
340 free(Clip-384); /* I love magic number */
343 /* mostly IMPLEMENTAION specific routines */
344 static void Initialize_Sequence()
347 static int Table_6_20[3] = {6,8,12};
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");
353 /* force MPEG-1 parameters for proper decoder behavior */
354 /* see ISO/IEC 13818-2 section D.9.14 */
355 if (!base.MPEG2_Flag)
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;
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;
371 Coded_Picture_Width = 16*mb_width;
372 Coded_Picture_Height = 16*mb_height;
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;
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];
383 for (cc=0; cc<3; cc++)
386 size = Coded_Picture_Width*Coded_Picture_Height;
388 size = Chroma_Width*Chroma_Height;
390 if (!(backward_reference_frame[cc] = (unsigned char *)malloc(size)))
391 Error("backward_reference_frame[] malloc failed\n");
393 if (!(forward_reference_frame[cc] = (unsigned char *)malloc(size)))
394 Error("forward_reference_frame[] malloc failed\n");
396 if (!(auxframe[cc] = (unsigned char *)malloc(size)))
397 Error("auxframe[] malloc failed\n");
400 if (!(substitute_frame[cc] = (unsigned char *)malloc(size)))
401 Error("substitute_frame[] malloc failed\n");
404 if (base.scalable_mode==SC_SPAT)
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");
414 /* SCALABILITY: Spatial */
415 if (base.scalable_mode==SC_SPAT)
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");
422 if (Output_Type==T_X11)
424 Initialize_Display_Process("");
425 Initialize_Dither_Matrix();
434 fprintf(stderr,text);
438 /* Trace_Flag output */
439 void Print_Bits(code,bits,len)
443 for (i=0; i<len; i++)
444 printf("%d",(code>>(bits-1-i))&1);
449 /* option processing */
450 static void Process_Options(argc,argv)
451 int argc; /* argument count */
452 char *argv[]; /* argument vector */
454 int i, LastArg, NextArg;
456 /* at least one argument should be present */
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"
488 /* command-line options are proceeded by '-' */
492 /* check if this is the last argument */
493 LastArg = ((argc-i)==1);
495 /* parse ahead to see if another flag immediately follows current
496 argument (this is used to tell if a filename is missing) */
498 NextArg = (argv[i+1][0]=='-');
502 /* second character, [1], after '-' is the switch */
505 switch(toupper(argv[i][1]))
507 /* third character. [2], is the value */
509 Main_Bitstream_Flag = 1;
511 if(NextArg || LastArg)
513 printf("ERROR: -b must be followed the main bitstream filename\n");
516 Main_Bitstream_Filename = argv[++i];
524 Verify_Flag = atoi(&argv[i][2]);
526 if((Verify_Flag < NO_LAYER) || (Verify_Flag > ALL_LAYERS))
528 printf("ERROR: -c level (%d) out of range [%d,%d]\n",
529 Verify_Flag, NO_LAYER, ALL_LAYERS);
533 printf("This program not compiled for Verify_Flag option\n");
538 Two_Streams = 1; /* either Data Partitioning (DP) or SNR Scalability enhancment */
540 if(NextArg || LastArg)
542 printf("ERROR: -e must be followed by filename\n");
546 Enhancement_Layer_Bitstream_Filename = argv[++i];
552 Frame_Store_Flag = 1;
556 Big_Picture_Flag = 1;
562 Stats_Flag = atoi(&argv[i][2]);
564 printf("WARNING: This program not compiled for -i option\n");
568 case 'L': /* spatial scalability flag */
571 if(NextArg || LastArg)
573 printf("ERROR: -l must be followed by filename\n");
577 Lower_Layer_Picture_Filename = argv[++i];
583 Output_Type = atoi(&argv[i][2]);
585 if((Output_Type==4) || (Output_Type==5))
586 Output_Picture_Filename = ""; /* no need of filename */
587 else if(NextArg || LastArg)
589 printf("ERROR: -o must be followed by filename\n");
593 /* filename is separated by space, so it becomes the next argument */
594 Output_Picture_Filename = argv[++i];
597 if (Output_Type==T_X11HIQ)
610 Reference_IDCT_Flag = 1;
617 printf("WARNING: This program not compiled for -t option\n");
626 Verbose_Flag = atoi(&argv[i][2]);
628 printf("This program not compiled for -v option\n");
636 if(NextArg || LastArg)
638 printf("ERROR: -x must be followed by filename\n");
642 Substitute_Picture_Filename = argv[++i];
649 fprintf(stderr,"undefined option -%c ignored. Exiting program\n",
655 } /* if argv[i][0] == '-' */
659 /* check for bitstream filename argument (there must always be one, at the very end
660 of the command line arguments */
665 /* options sense checking */
667 if(Main_Bitstream_Flag!=1)
669 printf("There must be a main bitstream specified (-b filename)\n");
672 /* force display process to show frame pictures */
673 if((Output_Type==4 || Output_Type==5) && Frame_Store_Flag)
674 Display_Progressive_Flag = 1;
676 Display_Progressive_Flag = 0;
679 /* parse the bitstream, do not actually decode it completely */
685 Decode_Layer = Verify_Flag;
686 printf("FYI: Decoding bitstream elements up to: %s\n",
687 Layer_Table[Decode_Layer]);
691 Decode_Layer = ALL_LAYERS;
695 /* no output type specified */
699 Output_Picture_Filename = "";
704 if (Output_Type==T_X11)
707 Display_Progressive_Flag = 1;
709 Display_Progressive_Flag = 0;
711 Frame_Store_Flag = 1; /* to avoid calling dither() twice */
721 this is an old routine used to convert command line arguments
724 static int Get_Val(argv)
729 if (sscanf(argv[1]+2,"%d",&val)!=1)
732 while (isdigit(argv[1][2]))
748 /* return when end of sequence (0) or picture
749 header has been parsed (1) */
757 if (Get_Hdr()!=ret && !Quiet_Flag)
758 fprintf(stderr,"streams out of sync\n");
767 static int Decode_Bitstream()
770 int Bitstream_Framenum;
772 Bitstream_Framenum = 0;
778 Clear_Verify_Headers();
785 ret = video_sequence(&Bitstream_Framenum);
794 static void Deinitialize_Sequence()
798 /* First cleanup the static buffer in store.c */
806 free(backward_reference_frame[i]);
807 free(forward_reference_frame[i]);
810 if (base.scalable_mode==SC_SPAT)
817 if (base.scalable_mode==SC_SPAT)
821 if (Output_Type==T_X11)
822 Terminate_Display_Process();
827 static int video_sequence(Bitstream_Framenumber)
828 int *Bitstream_Framenumber;
830 int Bitstream_Framenum;
831 int Sequence_Framenum;
834 Bitstream_Framenum = *Bitstream_Framenumber;
837 Initialize_Sequence();
839 /* decode picture whose header has already been parsed in
840 Decode_Bitstream() */
843 Decode_Picture(Bitstream_Framenum, Sequence_Framenum);
845 /* update picture numbers */
848 Bitstream_Framenum++;
852 /* loop through the rest of the pictures in the sequence */
853 while ((Return_Value=Headers()))
855 Decode_Picture(Bitstream_Framenum, Sequence_Framenum);
859 Bitstream_Framenum++;
865 if (Sequence_Framenum!=0)
867 Output_Last_Frame_of_Sequence(Bitstream_Framenum);
870 Deinitialize_Sequence();
873 Clear_Verify_Headers();
876 *Bitstream_Framenumber = Bitstream_Framenum;
877 return(Return_Value);
882 static void Clear_Options()
886 Output_Picture_Filename = " ";
889 Frame_Store_Flag = 0;
891 Lower_Layer_Picture_Filename = " ";
892 Reference_IDCT_Flag = 0;
896 Substitute_Picture_Filename = " ";
898 Enhancement_Layer_Bitstream_Filename = " ";
899 Big_Picture_Flag = 0;
900 Main_Bitstream_Flag = 0;
901 Main_Bitstream_Filename = " ";
909 static void Print_Options()
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);