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();
69 /* decode command line arguments */
70 Process_Options(argc,argv);
76 ld = &base; /* select base layer context */
78 /* open MPEG base layer bitstream file(s) */
79 /* NOTE: this is either a base layer stream or a spatial enhancement stream */
80 if ((base.Infile=open(Main_Bitstream_Filename,O_RDONLY|O_BINARY))<0)
82 fprintf(stderr,"Base layer input file %s not found\n", Main_Bitstream_Filename);
91 if(Show_Bits(8)==0x47)
93 sprintf(Error_Text,"Decoder currently does not parse transport streams\n");
102 case SEQUENCE_HEADER_CODE:
104 case PACK_START_CODE:
105 System_Stream_Flag = 1;
106 case VIDEO_ELEMENTARY_STREAM:
107 System_Stream_Flag = 1;
110 sprintf(Error_Text,"Unable to recognize stream type\n");
115 lseek(base.Infile, 0l, 0);
121 lseek(base.Infile, 0l, 0);
128 ld = &enhan; /* select enhancement layer context */
130 if ((enhan.Infile = open(Enhancement_Layer_Bitstream_Filename,O_RDONLY|O_BINARY))<0)
132 sprintf(Error_Text,"enhancment layer bitstream file %s not found\n",
133 Enhancement_Layer_Bitstream_Filename);
142 Initialize_Decoder();
144 ret = Decode_Bitstream();
154 /* IMPLEMENTAION specific rouintes */
155 static void Initialize_Decoder()
160 if (!(Clip=(unsigned char *)malloc(1024)))
161 Error("Clip[] malloc failed\n");
165 for (i=-384; i<640; i++)
166 Clip[i] = (i<0) ? 0 : ((i>255) ? 255 : i);
169 if (Reference_IDCT_Flag)
170 Initialize_Reference_IDCT();
172 Initialize_Fast_IDCT();
176 /* mostly IMPLEMENTAION specific rouintes */
177 static void Initialize_Sequence()
180 static int Table_6_20[3] = {6,8,12};
182 /* check scalability mode of enhancement layer */
183 if (Two_Streams && (enhan.scalable_mode!=SC_SNR) && (base.scalable_mode!=SC_DP))
184 Error("unsupported scalability mode\n");
186 /* force MPEG-1 parameters for proper decoder behavior */
187 /* see ISO/IEC 13818-2 section D.9.14 */
188 if (!base.MPEG2_Flag)
190 progressive_sequence = 1;
191 progressive_frame = 1;
192 picture_structure = FRAME_PICTURE;
193 frame_pred_frame_dct = 1;
194 chroma_format = CHROMA420;
195 matrix_coefficients = 5;
198 /* round to nearest multiple of coded macroblocks */
199 /* ISO/IEC 13818-2 section 6.3.3 sequence_header() */
200 mb_width = (horizontal_size+15)/16;
201 mb_height = (base.MPEG2_Flag && !progressive_sequence) ? 2*((vertical_size+31)/32)
202 : (vertical_size+15)/16;
204 Coded_Picture_Width = 16*mb_width;
205 Coded_Picture_Height = 16*mb_height;
207 /* ISO/IEC 13818-2 sections 6.1.1.8, 6.1.1.9, and 6.1.1.10 */
208 Chroma_Width = (chroma_format==CHROMA444) ? Coded_Picture_Width
209 : Coded_Picture_Width>>1;
210 Chroma_Height = (chroma_format!=CHROMA420) ? Coded_Picture_Height
211 : Coded_Picture_Height>>1;
213 /* derived based on Table 6-20 in ISO/IEC 13818-2 section 6.3.17 */
214 block_count = Table_6_20[chroma_format-1];
216 for (cc=0; cc<3; cc++)
219 size = Coded_Picture_Width*Coded_Picture_Height;
221 size = Chroma_Width*Chroma_Height;
223 if (!(backward_reference_frame[cc] = (unsigned char *)malloc(size)))
224 Error("backward_reference_frame[] malloc failed\n");
226 if (!(forward_reference_frame[cc] = (unsigned char *)malloc(size)))
227 Error("forward_reference_frame[] malloc failed\n");
229 if (!(auxframe[cc] = (unsigned char *)malloc(size)))
230 Error("auxframe[] malloc failed\n");
233 if (!(substitute_frame[cc] = (unsigned char *)malloc(size)))
234 Error("substitute_frame[] malloc failed\n");
237 if (base.scalable_mode==SC_SPAT)
239 /* this assumes lower layer is 4:2:0 */
240 if (!(llframe0[cc] = (unsigned char *)malloc((lower_layer_prediction_horizontal_size*lower_layer_prediction_vertical_size)/(cc?4:1))))
241 Error("llframe0 malloc failed\n");
242 if (!(llframe1[cc] = (unsigned char *)malloc((lower_layer_prediction_horizontal_size*lower_layer_prediction_vertical_size)/(cc?4:1))))
243 Error("llframe1 malloc failed\n");
247 /* SCALABILITY: Spatial */
248 if (base.scalable_mode==SC_SPAT)
250 if (!(lltmp = (short *)malloc(lower_layer_prediction_horizontal_size*((lower_layer_prediction_vertical_size*vertical_subsampling_factor_n)/vertical_subsampling_factor_m)*sizeof(short))))
251 Error("lltmp malloc failed\n");
255 if (Output_Type==T_X11)
257 Initialize_Display_Process("");
258 Initialize_Dither_Matrix();
267 fprintf(stderr,text);
271 /* Trace_Flag output */
272 void Print_Bits(code,bits,len)
276 for (i=0; i<len; i++)
277 printf("%d",(code>>(bits-1-i))&1);
282 /* option processing */
283 static void Process_Options(argc,argv)
284 int argc; /* argument count */
285 char *argv[]; /* argument vector */
287 int i, LastArg, NextArg;
289 /* at least one argument should be present */
292 printf("\n%s, %s\n",Version,Author);
293 printf("Usage: mpeg2decode {options}\n"
294 "Options: -b file main bitstream (base or spatial enhancement layer)\n"
295 " -cn file conformance report (n: level)\n"
296 " -e file enhancement layer bitstream (SNR or Data Partitioning)\n"
297 " -f store/display interlaced video in frame format\n"
298 " -g concatenated file format for substitution method (-x)\n"
299 " -in file information & statistics report (n: level)\n"
300 " -l file file name pattern for lower layer sequence\n"
301 " (for spatial scalability)\n"
302 " -on file output format (0:YUV 1:SIF 2:TGA 3:PPM 4:X11 5:X11HiQ)\n"
303 " -q disable warnings to stderr\n"
304 " -r use double precision reference IDCT\n"
305 " -t enable low level tracing to stdout\n"
306 " -u file print user_data to stdio or file\n"
307 " -vn verbose output (n: level)\n"
308 " -x file filename pattern of picture substitution sequence\n\n"
309 "File patterns: for sequential filenames, \"printf\" style, e.g. rec%%d\n"
310 " or rec%%d%%c for fieldwise storage\n"
311 "Levels: 0:none 1:sequence 2:picture 3:slice 4:macroblock 5:block\n\n"
312 "Example: mpeg2decode -b bitstream.mpg -f -r -o0 rec%%d\n"
321 /* command-line options are proceeded by '-' */
325 /* check if this is the last argument */
326 LastArg = ((argc-i)==1);
328 /* parse ahead to see if another flag immediately follows current
329 argument (this is used to tell if a filename is missing) */
331 NextArg = (argv[i+1][0]=='-');
335 /* second character, [1], after '-' is the switch */
338 switch(toupper(argv[i][1]))
340 /* third character. [2], is the value */
342 Main_Bitstream_Flag = 1;
344 if(NextArg || LastArg)
346 printf("ERROR: -b must be followed the main bitstream filename\n");
349 Main_Bitstream_Filename = argv[++i];
357 Verify_Flag = atoi(&argv[i][2]);
359 if((Verify_Flag < NO_LAYER) || (Verify_Flag > ALL_LAYERS))
361 printf("ERROR: -c level (%d) out of range [%d,%d]\n",
362 Verify_Flag, NO_LAYER, ALL_LAYERS);
366 printf("This program not compiled for Verify_Flag option\n");
371 Two_Streams = 1; /* either Data Partitioning (DP) or SNR Scalability enhancment */
373 if(NextArg || LastArg)
375 printf("ERROR: -e must be followed by filename\n");
379 Enhancement_Layer_Bitstream_Filename = argv[++i];
385 Frame_Store_Flag = 1;
389 Big_Picture_Flag = 1;
395 Stats_Flag = atoi(&argv[i][2]);
397 printf("WARNING: This program not compiled for -i option\n");
401 case 'L': /* spatial scalability flag */
404 if(NextArg || LastArg)
406 printf("ERROR: -l must be followed by filename\n");
410 Lower_Layer_Picture_Filename = argv[++i];
416 Output_Type = atoi(&argv[i][2]);
418 if((Output_Type==4) || (Output_Type==5))
419 Output_Picture_Filename = ""; /* no need of filename */
420 else if(NextArg || LastArg)
422 printf("ERROR: -o must be followed by filename\n");
426 /* filename is separated by space, so it becomes the next argument */
427 Output_Picture_Filename = argv[++i];
430 if (Output_Type==T_X11HIQ)
443 Reference_IDCT_Flag = 1;
450 printf("WARNING: This program not compiled for -t option\n");
459 Verbose_Flag = atoi(&argv[i][2]);
461 printf("This program not compiled for -v option\n");
469 if(NextArg || LastArg)
471 printf("ERROR: -x must be followed by filename\n");
475 Substitute_Picture_Filename = argv[++i];
482 fprintf(stderr,"undefined option -%c ignored. Exiting program\n",
488 } /* if argv[i][0] == '-' */
492 /* check for bitstream filename argument (there must always be one, at the very end
493 of the command line arguments */
498 /* options sense checking */
500 if(Main_Bitstream_Flag!=1)
502 printf("There must be a main bitstream specified (-b filename)\n");
505 /* force display process to show frame pictures */
506 if((Output_Type==4 || Output_Type==5) && Frame_Store_Flag)
507 Display_Progressive_Flag = 1;
509 Display_Progressive_Flag = 0;
512 /* parse the bitstream, do not actually decode it completely */
518 Decode_Layer = Verify_Flag;
519 printf("FYI: Decoding bitstream elements up to: %s\n",
520 Layer_Table[Decode_Layer]);
524 Decode_Layer = ALL_LAYERS;
528 /* no output type specified */
532 Output_Picture_Filename = "";
537 if (Output_Type==T_X11)
540 Display_Progressive_Flag = 1;
542 Display_Progressive_Flag = 0;
544 Frame_Store_Flag = 1; /* to avoid calling dither() twice */
554 this is an old routine used to convert command line arguments
557 static int Get_Val(argv)
562 if (sscanf(argv[1]+2,"%d",&val)!=1)
565 while (isdigit(argv[1][2]))
581 /* return when end of sequence (0) or picture
582 header has been parsed (1) */
590 if (Get_Hdr()!=ret && !Quiet_Flag)
591 fprintf(stderr,"streams out of sync\n");
600 static int Decode_Bitstream()
603 int Bitstream_Framenum;
605 Bitstream_Framenum = 0;
611 Clear_Verify_Headers();
618 ret = video_sequence(&Bitstream_Framenum);
627 static void Deinitialize_Sequence()
636 free(backward_reference_frame[i]);
637 free(forward_reference_frame[i]);
640 if (base.scalable_mode==SC_SPAT)
647 if (base.scalable_mode==SC_SPAT)
651 if (Output_Type==T_X11)
652 Terminate_Display_Process();
657 static int video_sequence(Bitstream_Framenumber)
658 int *Bitstream_Framenumber;
660 int Bitstream_Framenum;
661 int Sequence_Framenum;
664 Bitstream_Framenum = *Bitstream_Framenumber;
667 Initialize_Sequence();
669 /* decode picture whose header has already been parsed in
670 Decode_Bitstream() */
673 Decode_Picture(Bitstream_Framenum, Sequence_Framenum);
675 /* update picture numbers */
678 Bitstream_Framenum++;
682 /* loop through the rest of the pictures in the sequence */
683 while ((Return_Value=Headers()))
685 Decode_Picture(Bitstream_Framenum, Sequence_Framenum);
689 Bitstream_Framenum++;
695 if (Sequence_Framenum!=0)
697 Output_Last_Frame_of_Sequence(Bitstream_Framenum);
700 Deinitialize_Sequence();
703 Clear_Verify_Headers();
706 *Bitstream_Framenumber = Bitstream_Framenum;
707 return(Return_Value);
712 static void Clear_Options()
716 Output_Picture_Filename = " ";
719 Frame_Store_Flag = 0;
721 Lower_Layer_Picture_Filename = " ";
722 Reference_IDCT_Flag = 0;
726 Substitute_Picture_Filename = " ";
728 Enhancement_Layer_Bitstream_Filename = " ";
729 Big_Picture_Flag = 0;
730 Main_Bitstream_Flag = 0;
731 Main_Bitstream_Filename = " ";
739 static void Print_Options()
742 printf("Verbose_Flag = %d\n", Verbose_Flag);
743 printf("Output_Type = %d\n", Output_Type);
744 printf("Output_Picture_Filename = %s\n", Output_Picture_Filename);
745 printf("hiQdither = %d\n", hiQdither);
746 printf("Output_Type = %d\n", Output_Type);
747 printf("Frame_Store_Flag = %d\n", Frame_Store_Flag);
748 printf("Spatial_Flag = %d\n", Spatial_Flag);
749 printf("Lower_Layer_Picture_Filename = %s\n", Lower_Layer_Picture_Filename);
750 printf("Reference_IDCT_Flag = %d\n", Reference_IDCT_Flag);
751 printf("Trace_Flag = %d\n", Trace_Flag);
752 printf("Quiet_Flag = %d\n", Quiet_Flag);
753 printf("Ersatz_Flag = %d\n", Ersatz_Flag);
754 printf("Substitute_Picture_Filename = %s\n", Substitute_Picture_Filename);
755 printf("Two_Streams = %d\n", Two_Streams);
756 printf("Enhancement_Layer_Bitstream_Filename = %s\n", Enhancement_Layer_Bitstream_Filename);
757 printf("Big_Picture_Flag = %d\n", Big_Picture_Flag);
758 printf("Main_Bitstream_Flag = %d\n", Main_Bitstream_Flag);
759 printf("Main_Bitstream_Filename = %s\n", Main_Bitstream_Filename);
760 printf("Verify_Flag = %d\n", Verify_Flag);
761 printf("Stats_Flag = %d\n", Stats_Flag);
762 printf("User_Data_Flag = %d\n", User_Data_Flag);