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)
85 base.open_stream = my_open;
86 base.seek_stream = my_seek;
87 base.read_stream = my_read;
88 base.close_stream = my_close;
92 /* decode command line arguments */
93 Process_Options(argc,argv);
99 ld = &base; /* select base layer context */
101 /* open MPEG base layer bitstream file(s) */
102 /* NOTE: this is either a base layer stream or a spatial enhancement stream */
103 /* if ((base.Infile=open(Main_Bitstream_Filename,O_RDONLY|O_BINARY))<0) */
104 base.Infile = ld->open_stream(Main_Bitstream_Filename);
105 if( base.Infile < 0 )
107 fprintf(stderr,"Base layer input file %s not found\n", Main_Bitstream_Filename);
116 if(Show_Bits(8)==0x47)
118 sprintf(Error_Text,"Decoder currently does not parse transport streams\n");
123 code = Show_Bits(32);
127 case SEQUENCE_HEADER_CODE:
129 case PACK_START_CODE:
130 System_Stream_Flag = 1;
131 case VIDEO_ELEMENTARY_STREAM:
132 System_Stream_Flag = 1;
135 sprintf(Error_Text,"Unable to recognize stream type\n");
140 /*lseek(base.Infile, 0l, SEEK_SET);*/
141 ld->seek_stream(base.Infile,0l,SEEK_SET);
147 /*lseek(base.Infile, 0l, SEEK_SET);*/
148 ld->seek_stream(base.Infile,0l,SEEK_SET);
155 ld = &enhan; /* select enhancement layer context */
157 /*if ((enhan.Infile = open(Enhancement_Layer_Bitstream_Filename,O_RDONLY|O_BINARY))<0)*/
158 enhan.Infile = ld->open_stream(Enhancement_Layer_Bitstream_Filename);
161 sprintf(Error_Text,"enhancment layer bitstream file %s not found\n",
162 Enhancement_Layer_Bitstream_Filename);
171 Initialize_Decoder();
173 ret = Decode_Bitstream();
175 /*close(base.Infile);*/
176 ld->close_stream(base.Infile);
179 /*close(enhan.Infile);*/
180 ld->close_stream(enhan.Infile);
185 /* IMPLEMENTATION specific routines */
186 static void Initialize_Decoder()
191 if (!(Clip=(unsigned char *)malloc(1024)))
192 Error("Clip[] malloc failed\n");
196 for (i=-384; i<640; i++)
197 Clip[i] = (i<0) ? 0 : ((i>255) ? 255 : i);
200 if (Reference_IDCT_Flag)
201 Initialize_Reference_IDCT();
203 Initialize_Fast_IDCT();
207 /* mostly IMPLEMENTAION specific rouintes */
208 static void Initialize_Sequence()
211 static int Table_6_20[3] = {6,8,12};
213 /* check scalability mode of enhancement layer */
214 if (Two_Streams && (enhan.scalable_mode!=SC_SNR) && (base.scalable_mode!=SC_DP))
215 Error("unsupported scalability mode\n");
217 /* force MPEG-1 parameters for proper decoder behavior */
218 /* see ISO/IEC 13818-2 section D.9.14 */
219 if (!base.MPEG2_Flag)
221 progressive_sequence = 1;
222 progressive_frame = 1;
223 picture_structure = FRAME_PICTURE;
224 frame_pred_frame_dct = 1;
225 chroma_format = CHROMA420;
226 matrix_coefficients = 5;
229 /* round to nearest multiple of coded macroblocks */
230 /* ISO/IEC 13818-2 section 6.3.3 sequence_header() */
231 mb_width = (horizontal_size+15)/16;
232 mb_height = (base.MPEG2_Flag && !progressive_sequence) ? 2*((vertical_size+31)/32)
233 : (vertical_size+15)/16;
235 Coded_Picture_Width = 16*mb_width;
236 Coded_Picture_Height = 16*mb_height;
238 /* ISO/IEC 13818-2 sections 6.1.1.8, 6.1.1.9, and 6.1.1.10 */
239 Chroma_Width = (chroma_format==CHROMA444) ? Coded_Picture_Width
240 : Coded_Picture_Width>>1;
241 Chroma_Height = (chroma_format!=CHROMA420) ? Coded_Picture_Height
242 : Coded_Picture_Height>>1;
244 /* derived based on Table 6-20 in ISO/IEC 13818-2 section 6.3.17 */
245 block_count = Table_6_20[chroma_format-1];
247 for (cc=0; cc<3; cc++)
250 size = Coded_Picture_Width*Coded_Picture_Height;
252 size = Chroma_Width*Chroma_Height;
254 if (!(backward_reference_frame[cc] = (unsigned char *)malloc(size)))
255 Error("backward_reference_frame[] malloc failed\n");
257 if (!(forward_reference_frame[cc] = (unsigned char *)malloc(size)))
258 Error("forward_reference_frame[] malloc failed\n");
260 if (!(auxframe[cc] = (unsigned char *)malloc(size)))
261 Error("auxframe[] malloc failed\n");
264 if (!(substitute_frame[cc] = (unsigned char *)malloc(size)))
265 Error("substitute_frame[] malloc failed\n");
268 if (base.scalable_mode==SC_SPAT)
270 /* this assumes lower layer is 4:2:0 */
271 if (!(llframe0[cc] = (unsigned char *)malloc((lower_layer_prediction_horizontal_size*lower_layer_prediction_vertical_size)/(cc?4:1))))
272 Error("llframe0 malloc failed\n");
273 if (!(llframe1[cc] = (unsigned char *)malloc((lower_layer_prediction_horizontal_size*lower_layer_prediction_vertical_size)/(cc?4:1))))
274 Error("llframe1 malloc failed\n");
278 /* SCALABILITY: Spatial */
279 if (base.scalable_mode==SC_SPAT)
281 if (!(lltmp = (short *)malloc(lower_layer_prediction_horizontal_size*((lower_layer_prediction_vertical_size*vertical_subsampling_factor_n)/vertical_subsampling_factor_m)*sizeof(short))))
282 Error("lltmp malloc failed\n");
286 if (Output_Type==T_X11)
288 Initialize_Display_Process("");
289 Initialize_Dither_Matrix();
298 fprintf(stderr,text);
302 /* Trace_Flag output */
303 void Print_Bits(code,bits,len)
307 for (i=0; i<len; i++)
308 printf("%d",(code>>(bits-1-i))&1);
313 /* option processing */
314 static void Process_Options(argc,argv)
315 int argc; /* argument count */
316 char *argv[]; /* argument vector */
318 int i, LastArg, NextArg;
320 /* at least one argument should be present */
323 printf("\n%s, %s\n",Version,Author);
324 printf("Usage: mpeg2decode {options}\n"
325 "Options: -b file main bitstream (base or spatial enhancement layer)\n"
326 " -cn file conformance report (n: level)\n"
327 " -e file enhancement layer bitstream (SNR or Data Partitioning)\n"
328 " -f store/display interlaced video in frame format\n"
329 " -g concatenated file format for substitution method (-x)\n"
330 " -in file information & statistics report (n: level)\n"
331 " -l file file name pattern for lower layer sequence\n"
332 " (for spatial scalability)\n"
333 " -on file output format (0:YUV 1:SIF 2:TGA 3:PPM 4:X11 5:X11HiQ)\n"
334 " -q disable warnings to stderr\n"
335 " -r use double precision reference IDCT\n"
336 " -t enable low level tracing to stdout\n"
337 " -u file print user_data to stdio or file\n"
338 " -vn verbose output (n: level)\n"
339 " -x file filename pattern of picture substitution sequence\n\n"
340 "File patterns: for sequential filenames, \"printf\" style, e.g. rec%%d\n"
341 " or rec%%d%%c for fieldwise storage\n"
342 "Levels: 0:none 1:sequence 2:picture 3:slice 4:macroblock 5:block\n\n"
343 "Example: mpeg2decode -b bitstream.mpg -f -r -o0 rec%%d\n"
352 /* command-line options are proceeded by '-' */
356 /* check if this is the last argument */
357 LastArg = ((argc-i)==1);
359 /* parse ahead to see if another flag immediately follows current
360 argument (this is used to tell if a filename is missing) */
362 NextArg = (argv[i+1][0]=='-');
366 /* second character, [1], after '-' is the switch */
369 switch(toupper(argv[i][1]))
371 /* third character. [2], is the value */
373 Main_Bitstream_Flag = 1;
375 if(NextArg || LastArg)
377 printf("ERROR: -b must be followed the main bitstream filename\n");
380 Main_Bitstream_Filename = argv[++i];
388 Verify_Flag = atoi(&argv[i][2]);
390 if((Verify_Flag < NO_LAYER) || (Verify_Flag > ALL_LAYERS))
392 printf("ERROR: -c level (%d) out of range [%d,%d]\n",
393 Verify_Flag, NO_LAYER, ALL_LAYERS);
397 printf("This program not compiled for Verify_Flag option\n");
402 Two_Streams = 1; /* either Data Partitioning (DP) or SNR Scalability enhancment */
404 if(NextArg || LastArg)
406 printf("ERROR: -e must be followed by filename\n");
410 Enhancement_Layer_Bitstream_Filename = argv[++i];
416 Frame_Store_Flag = 1;
420 Big_Picture_Flag = 1;
426 Stats_Flag = atoi(&argv[i][2]);
428 printf("WARNING: This program not compiled for -i option\n");
432 case 'L': /* spatial scalability flag */
435 if(NextArg || LastArg)
437 printf("ERROR: -l must be followed by filename\n");
441 Lower_Layer_Picture_Filename = argv[++i];
447 Output_Type = atoi(&argv[i][2]);
449 if((Output_Type==4) || (Output_Type==5))
450 Output_Picture_Filename = ""; /* no need of filename */
451 else if(NextArg || LastArg)
453 printf("ERROR: -o must be followed by filename\n");
457 /* filename is separated by space, so it becomes the next argument */
458 Output_Picture_Filename = argv[++i];
461 if (Output_Type==T_X11HIQ)
474 Reference_IDCT_Flag = 1;
481 printf("WARNING: This program not compiled for -t option\n");
490 Verbose_Flag = atoi(&argv[i][2]);
492 printf("This program not compiled for -v option\n");
500 if(NextArg || LastArg)
502 printf("ERROR: -x must be followed by filename\n");
506 Substitute_Picture_Filename = argv[++i];
513 fprintf(stderr,"undefined option -%c ignored. Exiting program\n",
519 } /* if argv[i][0] == '-' */
523 /* check for bitstream filename argument (there must always be one, at the very end
524 of the command line arguments */
529 /* options sense checking */
531 if(Main_Bitstream_Flag!=1)
533 printf("There must be a main bitstream specified (-b filename)\n");
536 /* force display process to show frame pictures */
537 if((Output_Type==4 || Output_Type==5) && Frame_Store_Flag)
538 Display_Progressive_Flag = 1;
540 Display_Progressive_Flag = 0;
543 /* parse the bitstream, do not actually decode it completely */
549 Decode_Layer = Verify_Flag;
550 printf("FYI: Decoding bitstream elements up to: %s\n",
551 Layer_Table[Decode_Layer]);
555 Decode_Layer = ALL_LAYERS;
559 /* no output type specified */
563 Output_Picture_Filename = "";
568 if (Output_Type==T_X11)
571 Display_Progressive_Flag = 1;
573 Display_Progressive_Flag = 0;
575 Frame_Store_Flag = 1; /* to avoid calling dither() twice */
585 this is an old routine used to convert command line arguments
588 static int Get_Val(argv)
593 if (sscanf(argv[1]+2,"%d",&val)!=1)
596 while (isdigit(argv[1][2]))
612 /* return when end of sequence (0) or picture
613 header has been parsed (1) */
621 if (Get_Hdr()!=ret && !Quiet_Flag)
622 fprintf(stderr,"streams out of sync\n");
631 static int Decode_Bitstream()
634 int Bitstream_Framenum;
636 Bitstream_Framenum = 0;
642 Clear_Verify_Headers();
649 ret = video_sequence(&Bitstream_Framenum);
658 static void Deinitialize_Sequence()
667 free(backward_reference_frame[i]);
668 free(forward_reference_frame[i]);
671 if (base.scalable_mode==SC_SPAT)
678 if (base.scalable_mode==SC_SPAT)
682 if (Output_Type==T_X11)
683 Terminate_Display_Process();
688 static int video_sequence(Bitstream_Framenumber)
689 int *Bitstream_Framenumber;
691 int Bitstream_Framenum;
692 int Sequence_Framenum;
695 Bitstream_Framenum = *Bitstream_Framenumber;
698 Initialize_Sequence();
700 /* decode picture whose header has already been parsed in
701 Decode_Bitstream() */
704 Decode_Picture(Bitstream_Framenum, Sequence_Framenum);
706 /* update picture numbers */
709 Bitstream_Framenum++;
713 /* loop through the rest of the pictures in the sequence */
714 while ((Return_Value=Headers()))
716 Decode_Picture(Bitstream_Framenum, Sequence_Framenum);
720 Bitstream_Framenum++;
726 if (Sequence_Framenum!=0)
728 Output_Last_Frame_of_Sequence(Bitstream_Framenum);
731 Deinitialize_Sequence();
734 Clear_Verify_Headers();
737 *Bitstream_Framenumber = Bitstream_Framenum;
738 return(Return_Value);
743 static void Clear_Options()
747 Output_Picture_Filename = " ";
750 Frame_Store_Flag = 0;
752 Lower_Layer_Picture_Filename = " ";
753 Reference_IDCT_Flag = 0;
757 Substitute_Picture_Filename = " ";
759 Enhancement_Layer_Bitstream_Filename = " ";
760 Big_Picture_Flag = 0;
761 Main_Bitstream_Flag = 0;
762 Main_Bitstream_Filename = " ";
770 static void Print_Options()
773 printf("Verbose_Flag = %d\n", Verbose_Flag);
774 printf("Output_Type = %d\n", Output_Type);
775 printf("Output_Picture_Filename = %s\n", Output_Picture_Filename);
776 printf("hiQdither = %d\n", hiQdither);
777 printf("Output_Type = %d\n", Output_Type);
778 printf("Frame_Store_Flag = %d\n", Frame_Store_Flag);
779 printf("Spatial_Flag = %d\n", Spatial_Flag);
780 printf("Lower_Layer_Picture_Filename = %s\n", Lower_Layer_Picture_Filename);
781 printf("Reference_IDCT_Flag = %d\n", Reference_IDCT_Flag);
782 printf("Trace_Flag = %d\n", Trace_Flag);
783 printf("Quiet_Flag = %d\n", Quiet_Flag);
784 printf("Ersatz_Flag = %d\n", Ersatz_Flag);
785 printf("Substitute_Picture_Filename = %s\n", Substitute_Picture_Filename);
786 printf("Two_Streams = %d\n", Two_Streams);
787 printf("Enhancement_Layer_Bitstream_Filename = %s\n", Enhancement_Layer_Bitstream_Filename);
788 printf("Big_Picture_Flag = %d\n", Big_Picture_Flag);
789 printf("Main_Bitstream_Flag = %d\n", Main_Bitstream_Flag);
790 printf("Main_Bitstream_Filename = %s\n", Main_Bitstream_Filename);
791 printf("Verify_Flag = %d\n", Verify_Flag);
792 printf("Stats_Flag = %d\n", Stats_Flag);
793 printf("User_Data_Flag = %d\n", User_Data_Flag);