1 /* subspic.c, Frame buffer substitution routines */
3 /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
6 * Disclaimer of Warranty
8 * These software programs are available to the user without any license fee or
9 * royalty on an "as is" basis. The MPEG Software Simulation Group disclaims
10 * any and all warranties, whether express, implied, or statuary, including any
11 * implied warranties or merchantability or of fitness for a particular
12 * purpose. In no event shall the copyright-holder be liable for any
13 * incidental, punitive, or consequential damages of any kind whatsoever
14 * arising from the use of these programs.
16 * This disclaimer of warranty extends to the user of these programs and user's
17 * customers, employees, agents, transferees, successors, and assigns.
19 * The MPEG Software Simulation Group does not represent or warrant that the
20 * programs furnished hereunder are free of infringement of any third-party
23 * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
24 * are subject to royalty fees to patent holders. Many of these patents are
25 * general enough such that they are unavoidable regardless of implementation
33 /* private prototypes*/
34 static void Read_Frame _ANSI_ARGS_((const char *filename,
35 unsigned char *frame_buffer[], int framenum));
36 static void Copy_Frame _ANSI_ARGS_((unsigned char *src, unsigned char *dst,
37 int width, int height, int parity, int incr));
38 static int Read_Components _ANSI_ARGS_ ((const char *filename,
39 unsigned char *frame[3], int framenum));
40 static int Read_Component _ANSI_ARGS_ ((char *fname, unsigned char *frame,
41 int width, int height));
42 static int Extract_Components _ANSI_ARGS_ ((const char *filename,
43 unsigned char *frame[3], int framenum));
46 /* substitute frame buffer routine */
47 void Substitute_Frame_Buffer (bitstream_framenum, sequence_framenum)
48 int bitstream_framenum;
49 int sequence_framenum;
51 /* static tracking variables */
52 static int previous_temporal_reference;
53 static int previous_bitstream_framenum;
54 static int previous_anchor_temporal_reference;
55 static int previous_anchor_bitstream_framenum;
56 static int previous_picture_coding_type;
59 /* local temporary variables */
60 int substitute_display_framenum;
64 my_printf("SUB: seq fn(%d) bitfn(%d) tempref(%d) picstr(%d) type(%d)\n",
65 sequence_framenum, bitstream_framenum, temporal_reference,
66 picture_structure, picture_coding_type);
69 /* we don't substitute at the first picture of a sequence */
70 if((sequence_framenum!=0)||(Second_Field))
72 /* only at the start of the frame */
73 if ((picture_structure==FRAME_PICTURE)||(!Second_Field))
75 if(picture_coding_type==P_TYPE)
77 /* the most recently decoded reference frame needs substituting */
78 substitute_display_framenum = bitstream_framenum - 1;
80 Read_Frame(Substitute_Picture_Filename, forward_reference_frame,
81 substitute_display_framenum);
83 /* only the first B frame in a consequitve set of B pictures
84 loads a substitute backward_reference_frame since all subsequent
85 B frames predict from the same reference pictures */
86 else if((picture_coding_type==B_TYPE)&&(bgate!=1))
88 substitute_display_framenum =
89 (previous_temporal_reference - temporal_reference)
90 + bitstream_framenum - 1;
92 Read_Frame(Substitute_Picture_Filename, backward_reference_frame,
93 substitute_display_framenum);
95 } /* P fields can predict from the two most recently decoded fields, even
96 from the first field of the same frame being decoded */
97 else if(Second_Field && (picture_coding_type==P_TYPE))
99 /* our favourite case: the IP field picture pair */
100 if((previous_picture_coding_type==I_TYPE)&&(picture_coding_type==P_TYPE))
102 substitute_display_framenum = bitstream_framenum;
104 else /* our more generic P field picture pair */
106 substitute_display_framenum =
107 (temporal_reference - previous_anchor_temporal_reference)
108 + bitstream_framenum - 1;
111 Read_Frame(Substitute_Picture_Filename, current_frame, substitute_display_framenum);
114 else if((picture_coding_type!=B_TYPE)||(picture_coding_type!=D_TYPE))
116 my_printf("NO SUBS FOR THIS PICTURE\n");
122 /* set b gate so we don't redundantly load next time around */
123 if(picture_coding_type==B_TYPE)
128 /* update general tracking variables */
129 if((picture_structure==FRAME_PICTURE)||(!Second_Field))
131 previous_temporal_reference = temporal_reference;
132 previous_bitstream_framenum = bitstream_framenum;
135 /* update reference frame tracking variables */
136 if((picture_coding_type!=B_TYPE) &&
137 ((picture_structure==FRAME_PICTURE)||Second_Field))
139 previous_anchor_temporal_reference = temporal_reference;
140 previous_anchor_bitstream_framenum = bitstream_framenum;
143 previous_picture_coding_type = picture_coding_type;
148 /* Note: fields are only read to serve as the same-frame reference for
150 static void Read_Frame(fname,frame,framenum)
152 unsigned char *frame[];
160 my_printf("ERROR: framenum (%d) is less than zero\n", framenum);
164 rerr = Extract_Components(fname, substitute_frame, framenum);
166 rerr = Read_Components(fname, substitute_frame, framenum);
170 my_printf("was unable to substitute frame\n");
173 /* now copy to the appropriate buffer */
174 /* first field (which we are attempting to substitute) must be
175 of opposite field parity to the current one */
176 if((Second_Field)&&(picture_coding_type==P_TYPE))
178 parity = (picture_structure==TOP_FIELD ? 1:0);
179 field_mode = (picture_structure==FRAME_PICTURE ? 0:1);
183 /* Like frame structued pictures, B pictures only substitute an entire frame
184 since both fields always predict from the same frame (with respect
185 to forward/backwards directions) */
191 Copy_Frame(substitute_frame[0], frame[0], Coded_Picture_Width,
192 Coded_Picture_Height, parity, field_mode);
194 Copy_Frame(substitute_frame[1], frame[1], Chroma_Width, Chroma_Height,
197 Copy_Frame(substitute_frame[2], frame[2], Chroma_Width, Chroma_Height,
201 if(Verbose_Flag > NO_LAYER)
202 my_printf("substituted %s %d\n",
203 (field_mode ? (parity?"bottom field":"bottom field"):"frame"), framenum);
210 static int Read_Components(filename, frame, framenum)
211 const char *filename;
212 unsigned char *frame[3];
216 char outname[FILENAME_LENGTH];
217 char name[FILENAME_LENGTH];
219 my_sprintf(outname,filename,framenum);
222 my_sprintf(name,"%s.Y",outname);
223 err += Read_Component(name, frame[0], Coded_Picture_Width,
224 Coded_Picture_Height);
226 my_sprintf(name,"%s.U",outname);
227 err += Read_Component(name, frame[1], Chroma_Width, Chroma_Height);
229 my_sprintf(name,"%s.V",outname);
230 err += Read_Component(name, frame[2], Chroma_Width, Chroma_Height);
236 static int Read_Component(Filename, Frame, Width, Height)
238 unsigned char *Frame;
249 my_printf("SUBS: reading %s\n", filename);
252 if(!my_fopenr(Filename, "rb", &Infile))
254 my_printf("ERROR: unable to open reference filename (%s)\n", Filename);
259 Bytes_Read = my_fread(Frame, Size, 1, &Infile);
263 my_printf("was able to read only %d bytes of %d of file %s\n",
264 Bytes_Read, Size, Filename);
272 /* optimization: do not open the big file each time. Open once at start
273 of decoder, and close at the very last frame */
275 /* Note: "big" files were used in E-mail exchanges almost exclusively by the
276 MPEG Committee's syntax validation and conformance ad-hoc groups from
277 the year 1993 until 1995 */
278 static int Extract_Components(filename, frame, framenum)
279 const char *filename;
280 unsigned char *frame[3];
290 if (!my_fopenr(filename,"rb", &fd))
292 my_sprintf(Error_Text,"Couldn't open %s\n",filename);
296 /* compute size of each frame (in bytes) */
297 size = (Coded_Picture_Width*Coded_Picture_Height);
299 if(chroma_format==CHROMA444)
301 else if(chroma_format==CHROMA422)
303 else if(chroma_format==CHROMA420)
304 size = ((size*3)>>1);
306 my_printf("ERROR: chroma_format (%d) not recognized\n", chroma_format);
309 /* compute distance into "big" file */
310 offset = size*framenum;
313 my_printf("EXTRACTING: frame(%d) offset(%d), size (%d) from %s\n",
314 framenum, offset, size, filename);
317 /* seek to location in big file where desired frame begins */
318 /* note: this offset cannot exceed a few billion bytes due to the */
319 /* obvious limitations of 32-bit integers */
320 my_fseekr(&fd, offset, SEEK_SET);
323 for (line=0; line<Coded_Picture_Height; line++)
325 my_fread(frame[0]+(line*Coded_Picture_Width),1,Coded_Picture_Width,&fd);
329 for (line=0; line<Chroma_Height; line++)
331 my_fread(frame[1]+(line*Chroma_Width),1,Chroma_Width,&fd);
335 for (line=0; line<Chroma_Height; line++)
337 my_fread(frame[2]+(line*Chroma_Width),1,Chroma_Width,&fd);
346 static void Copy_Frame(src, dst, width, height, parity, field_mode)
351 int parity; /* field parity (top or bottom) to overwrite */
352 int field_mode; /* 0 = frame, 1 = field */
361 my_printf("COPYING (w=%d, h=%d, parity=%d, field_mode=%d)\n",
362 width,height,parity,field_mode);
377 for(row=0; row<height; row+=incr)
379 for(col=0; col<width; col++)
381 dst[d+col] = src[s+col];