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
40 /* private prototypes */
41 static int video_sequence _ANSI_ARGS_((int *framenum));
42 static int Decode_Bitstream _ANSI_ARGS_((void));
43 static int Headers _ANSI_ARGS_((void));
44 static void Initialize_Sequence _ANSI_ARGS_((void));
45 static void Initialize_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();
61 int my_open(char *filename)
63 return open(filename,O_RDONLY|O_BINARY);
65 off_t my_seek(int infile, off_t offset,int whence)
67 return lseek(infile, offset, whence);
69 ssize_t my_read(int infile,void *buf,size_t count)
71 return read(infile,buf,count);
73 int my_close(int infile)
80 #ifdef GDCM_BUILD_MPEG2DEC
86 base.open_stream = my_open;
87 base.seek_stream = my_seek;
88 base.read_stream = my_read;
89 base.close_stream = my_close;
93 /* decode command line arguments */
94 Process_Options(argc,argv);
100 ld = &base; /* select base layer context */
102 /* open MPEG base layer bitstream file(s) */
103 /* NOTE: this is either a base layer stream or a spatial enhancement stream */
104 /* if ((base.Infile=open(Main_Bitstream_Filename,O_RDONLY|O_BINARY))<0) */
105 base.Infile = ld->open_stream(Main_Bitstream_Filename);
106 if( base.Infile < 0 )
108 fprintf(stderr,"Base layer input file %s not found\n", Main_Bitstream_Filename);
117 if(Show_Bits(8)==0x47)
119 sprintf(Error_Text,"Decoder currently does not parse transport streams\n");
124 code = Show_Bits(32);
128 case SEQUENCE_HEADER_CODE:
130 case PACK_START_CODE:
131 System_Stream_Flag = 1;
132 case VIDEO_ELEMENTARY_STREAM:
133 System_Stream_Flag = 1;
136 sprintf(Error_Text,"Unable to recognize stream type\n");
141 /*lseek(base.Infile, 0l, SEEK_SET);*/
142 ld->seek_stream(base.Infile,0l,SEEK_SET);
148 /*lseek(base.Infile, 0l, SEEK_SET);*/
149 ld->seek_stream(base.Infile,0l,SEEK_SET);
156 ld = &enhan; /* select enhancement layer context */
158 /*if ((enhan.Infile = open(Enhancement_Layer_Bitstream_Filename,O_RDONLY|O_BINARY))<0)*/
159 enhan.Infile = ld->open_stream(Enhancement_Layer_Bitstream_Filename);
162 sprintf(Error_Text,"enhancment layer bitstream file %s not found\n",
163 Enhancement_Layer_Bitstream_Filename);
172 Initialize_Decoder();
174 ret = Decode_Bitstream();
176 /*close(base.Infile);*/
177 ld->close_stream(base.Infile);
180 /*close(enhan.Infile);*/
181 ld->close_stream(enhan.Infile);
185 #endif /*GDCM_BUILD_MPEG2DEC*/
187 /* IMPLEMENTATION specific routines */
188 static void Initialize_Decoder()
193 if (!(Clip=(unsigned char *)malloc(1024)))
194 Error("Clip[] malloc failed\n");
198 for (i=-384; i<640; i++)
199 Clip[i] = (i<0) ? 0 : ((i>255) ? 255 : i);
202 if (Reference_IDCT_Flag)
203 Initialize_Reference_IDCT();
205 Initialize_Fast_IDCT();
209 /* mostly IMPLEMENTAION specific rouintes */
210 static void Initialize_Sequence()
213 static int Table_6_20[3] = {6,8,12};
215 /* check scalability mode of enhancement layer */
216 if (Two_Streams && (enhan.scalable_mode!=SC_SNR) && (base.scalable_mode!=SC_DP))
217 Error("unsupported scalability mode\n");
219 /* force MPEG-1 parameters for proper decoder behavior */
220 /* see ISO/IEC 13818-2 section D.9.14 */
221 if (!base.MPEG2_Flag)
223 progressive_sequence = 1;
224 progressive_frame = 1;
225 picture_structure = FRAME_PICTURE;
226 frame_pred_frame_dct = 1;
227 chroma_format = CHROMA420;
228 matrix_coefficients = 5;
231 /* round to nearest multiple of coded macroblocks */
232 /* ISO/IEC 13818-2 section 6.3.3 sequence_header() */
233 mb_width = (horizontal_size+15)/16;
234 mb_height = (base.MPEG2_Flag && !progressive_sequence) ? 2*((vertical_size+31)/32)
235 : (vertical_size+15)/16;
237 Coded_Picture_Width = 16*mb_width;
238 Coded_Picture_Height = 16*mb_height;
240 /* ISO/IEC 13818-2 sections 6.1.1.8, 6.1.1.9, and 6.1.1.10 */
241 Chroma_Width = (chroma_format==CHROMA444) ? Coded_Picture_Width
242 : Coded_Picture_Width>>1;
243 Chroma_Height = (chroma_format!=CHROMA420) ? Coded_Picture_Height
244 : Coded_Picture_Height>>1;
246 /* derived based on Table 6-20 in ISO/IEC 13818-2 section 6.3.17 */
247 block_count = Table_6_20[chroma_format-1];
249 for (cc=0; cc<3; cc++)
252 size = Coded_Picture_Width*Coded_Picture_Height;
254 size = Chroma_Width*Chroma_Height;
256 if (!(backward_reference_frame[cc] = (unsigned char *)malloc(size)))
257 Error("backward_reference_frame[] malloc failed\n");
259 if (!(forward_reference_frame[cc] = (unsigned char *)malloc(size)))
260 Error("forward_reference_frame[] malloc failed\n");
262 if (!(auxframe[cc] = (unsigned char *)malloc(size)))
263 Error("auxframe[] malloc failed\n");
266 if (!(substitute_frame[cc] = (unsigned char *)malloc(size)))
267 Error("substitute_frame[] malloc failed\n");
270 if (base.scalable_mode==SC_SPAT)
272 /* this assumes lower layer is 4:2:0 */
273 if (!(llframe0[cc] = (unsigned char *)malloc((lower_layer_prediction_horizontal_size*lower_layer_prediction_vertical_size)/(cc?4:1))))
274 Error("llframe0 malloc failed\n");
275 if (!(llframe1[cc] = (unsigned char *)malloc((lower_layer_prediction_horizontal_size*lower_layer_prediction_vertical_size)/(cc?4:1))))
276 Error("llframe1 malloc failed\n");
280 /* SCALABILITY: Spatial */
281 if (base.scalable_mode==SC_SPAT)
283 if (!(lltmp = (short *)malloc(lower_layer_prediction_horizontal_size*((lower_layer_prediction_vertical_size*vertical_subsampling_factor_n)/vertical_subsampling_factor_m)*sizeof(short))))
284 Error("lltmp malloc failed\n");
288 if (Output_Type==T_X11)
290 Initialize_Display_Process("");
291 Initialize_Dither_Matrix();
300 fprintf(stderr,text);
304 /* Trace_Flag output */
305 void Print_Bits(code,bits,len)
309 for (i=0; i<len; i++)
310 printf("%d",(code>>(bits-1-i))&1);
315 /* option processing */
316 static void Process_Options(argc,argv)
317 int argc; /* argument count */
318 char *argv[]; /* argument vector */
320 int i, LastArg, NextArg;
322 /* at least one argument should be present */
325 printf("\n%s, %s\n",Version,Author);
326 printf("Usage: mpeg2decode {options}\n"
327 "Options: -b file main bitstream (base or spatial enhancement layer)\n"
328 " -cn file conformance report (n: level)\n"
329 " -e file enhancement layer bitstream (SNR or Data Partitioning)\n"
330 " -f store/display interlaced video in frame format\n"
331 " -g concatenated file format for substitution method (-x)\n"
332 " -in file information & statistics report (n: level)\n"
333 " -l file file name pattern for lower layer sequence\n"
334 " (for spatial scalability)\n"
335 " -on file output format (0:YUV 1:SIF 2:TGA 3:PPM 4:X11 5:X11HiQ)\n"
336 " -q disable warnings to stderr\n"
337 " -r use double precision reference IDCT\n"
338 " -t enable low level tracing to stdout\n"
339 " -u file print user_data to stdio or file\n"
340 " -vn verbose output (n: level)\n"
341 " -x file filename pattern of picture substitution sequence\n\n"
342 "File patterns: for sequential filenames, \"printf\" style, e.g. rec%%d\n"
343 " or rec%%d%%c for fieldwise storage\n"
344 "Levels: 0:none 1:sequence 2:picture 3:slice 4:macroblock 5:block\n\n"
345 "Example: mpeg2decode -b bitstream.mpg -f -r -o0 rec%%d\n"
354 /* command-line options are proceeded by '-' */
358 /* check if this is the last argument */
359 LastArg = ((argc-i)==1);
361 /* parse ahead to see if another flag immediately follows current
362 argument (this is used to tell if a filename is missing) */
364 NextArg = (argv[i+1][0]=='-');
368 /* second character, [1], after '-' is the switch */
371 switch(toupper(argv[i][1]))
373 /* third character. [2], is the value */
375 Main_Bitstream_Flag = 1;
377 if(NextArg || LastArg)
379 printf("ERROR: -b must be followed the main bitstream filename\n");
382 Main_Bitstream_Filename = argv[++i];
390 Verify_Flag = atoi(&argv[i][2]);
392 if((Verify_Flag < NO_LAYER) || (Verify_Flag > ALL_LAYERS))
394 printf("ERROR: -c level (%d) out of range [%d,%d]\n",
395 Verify_Flag, NO_LAYER, ALL_LAYERS);
399 printf("This program not compiled for Verify_Flag option\n");
404 Two_Streams = 1; /* either Data Partitioning (DP) or SNR Scalability enhancment */
406 if(NextArg || LastArg)
408 printf("ERROR: -e must be followed by filename\n");
412 Enhancement_Layer_Bitstream_Filename = argv[++i];
418 Frame_Store_Flag = 1;
422 Big_Picture_Flag = 1;
428 Stats_Flag = atoi(&argv[i][2]);
430 printf("WARNING: This program not compiled for -i option\n");
434 case 'L': /* spatial scalability flag */
437 if(NextArg || LastArg)
439 printf("ERROR: -l must be followed by filename\n");
443 Lower_Layer_Picture_Filename = argv[++i];
449 Output_Type = atoi(&argv[i][2]);
451 if((Output_Type==4) || (Output_Type==5))
452 Output_Picture_Filename = ""; /* no need of filename */
453 else if(NextArg || LastArg)
455 printf("ERROR: -o must be followed by filename\n");
459 /* filename is separated by space, so it becomes the next argument */
460 Output_Picture_Filename = argv[++i];
463 if (Output_Type==T_X11HIQ)
476 Reference_IDCT_Flag = 1;
483 printf("WARNING: This program not compiled for -t option\n");
492 Verbose_Flag = atoi(&argv[i][2]);
494 printf("This program not compiled for -v option\n");
502 if(NextArg || LastArg)
504 printf("ERROR: -x must be followed by filename\n");
508 Substitute_Picture_Filename = argv[++i];
515 fprintf(stderr,"undefined option -%c ignored. Exiting program\n",
521 } /* if argv[i][0] == '-' */
525 /* check for bitstream filename argument (there must always be one, at the very end
526 of the command line arguments */
531 /* options sense checking */
533 if(Main_Bitstream_Flag!=1)
535 printf("There must be a main bitstream specified (-b filename)\n");
538 /* force display process to show frame pictures */
539 if((Output_Type==4 || Output_Type==5) && Frame_Store_Flag)
540 Display_Progressive_Flag = 1;
542 Display_Progressive_Flag = 0;
545 /* parse the bitstream, do not actually decode it completely */
551 Decode_Layer = Verify_Flag;
552 printf("FYI: Decoding bitstream elements up to: %s\n",
553 Layer_Table[Decode_Layer]);
557 Decode_Layer = ALL_LAYERS;
561 /* no output type specified */
565 Output_Picture_Filename = "";
570 if (Output_Type==T_X11)
573 Display_Progressive_Flag = 1;
575 Display_Progressive_Flag = 0;
577 Frame_Store_Flag = 1; /* to avoid calling dither() twice */
587 this is an old routine used to convert command line arguments
590 static int Get_Val(argv)
595 if (sscanf(argv[1]+2,"%d",&val)!=1)
598 while (isdigit(argv[1][2]))
614 /* return when end of sequence (0) or picture
615 header has been parsed (1) */
623 if (Get_Hdr()!=ret && !Quiet_Flag)
624 fprintf(stderr,"streams out of sync\n");
633 static int Decode_Bitstream()
636 int Bitstream_Framenum;
638 Bitstream_Framenum = 0;
644 Clear_Verify_Headers();
651 ret = video_sequence(&Bitstream_Framenum);
660 static void Deinitialize_Sequence()
669 free(backward_reference_frame[i]);
670 free(forward_reference_frame[i]);
673 if (base.scalable_mode==SC_SPAT)
680 if (base.scalable_mode==SC_SPAT)
684 if (Output_Type==T_X11)
685 Terminate_Display_Process();
690 static int video_sequence(Bitstream_Framenumber)
691 int *Bitstream_Framenumber;
693 int Bitstream_Framenum;
694 int Sequence_Framenum;
697 Bitstream_Framenum = *Bitstream_Framenumber;
700 Initialize_Sequence();
702 /* decode picture whose header has already been parsed in
703 Decode_Bitstream() */
706 Decode_Picture(Bitstream_Framenum, Sequence_Framenum);
708 /* update picture numbers */
711 Bitstream_Framenum++;
715 /* loop through the rest of the pictures in the sequence */
716 while ((Return_Value=Headers()))
718 Decode_Picture(Bitstream_Framenum, Sequence_Framenum);
722 Bitstream_Framenum++;
728 if (Sequence_Framenum!=0)
730 Output_Last_Frame_of_Sequence(Bitstream_Framenum);
733 Deinitialize_Sequence();
736 Clear_Verify_Headers();
739 *Bitstream_Framenumber = Bitstream_Framenum;
740 return(Return_Value);
745 static void Clear_Options()
749 Output_Picture_Filename = " ";
752 Frame_Store_Flag = 0;
754 Lower_Layer_Picture_Filename = " ";
755 Reference_IDCT_Flag = 0;
759 Substitute_Picture_Filename = " ";
761 Enhancement_Layer_Bitstream_Filename = " ";
762 Big_Picture_Flag = 0;
763 Main_Bitstream_Flag = 0;
764 Main_Bitstream_Filename = " ";
772 static void Print_Options()
775 printf("Verbose_Flag = %d\n", Verbose_Flag);
776 printf("Output_Type = %d\n", Output_Type);
777 printf("Output_Picture_Filename = %s\n", Output_Picture_Filename);
778 printf("hiQdither = %d\n", hiQdither);
779 printf("Output_Type = %d\n", Output_Type);
780 printf("Frame_Store_Flag = %d\n", Frame_Store_Flag);
781 printf("Spatial_Flag = %d\n", Spatial_Flag);
782 printf("Lower_Layer_Picture_Filename = %s\n", Lower_Layer_Picture_Filename);
783 printf("Reference_IDCT_Flag = %d\n", Reference_IDCT_Flag);
784 printf("Trace_Flag = %d\n", Trace_Flag);
785 printf("Quiet_Flag = %d\n", Quiet_Flag);
786 printf("Ersatz_Flag = %d\n", Ersatz_Flag);
787 printf("Substitute_Picture_Filename = %s\n", Substitute_Picture_Filename);
788 printf("Two_Streams = %d\n", Two_Streams);
789 printf("Enhancement_Layer_Bitstream_Filename = %s\n", Enhancement_Layer_Bitstream_Filename);
790 printf("Big_Picture_Flag = %d\n", Big_Picture_Flag);
791 printf("Main_Bitstream_Flag = %d\n", Main_Bitstream_Flag);
792 printf("Main_Bitstream_Filename = %s\n", Main_Bitstream_Filename);
793 printf("Verify_Flag = %d\n", Verify_Flag);
794 printf("Stats_Flag = %d\n", Stats_Flag);
795 printf("User_Data_Flag = %d\n", User_Data_Flag);