2 /* subspic.c, Frame buffer substitution routines */
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
38 /* private prototypes*/
39 static void Read_Frame _ANSI_ARGS_((char *filename,
40 unsigned char *frame_buffer[], int framenum));
41 static void Copy_Frame _ANSI_ARGS_((unsigned char *src, unsigned char *dst,
42 int width, int height, int parity, int incr));
43 static int Read_Components _ANSI_ARGS_ ((char *filename,
44 unsigned char *frame[3], int framenum));
45 static int Read_Component _ANSI_ARGS_ ((char *fname, unsigned char *frame,
46 int width, int height));
47 static int Extract_Components _ANSI_ARGS_ ((char *filename,
48 unsigned char *frame[3], int framenum));
51 /* substitute frame buffer routine */
52 void Substitute_Frame_Buffer (bitstream_framenum, sequence_framenum)
53 int bitstream_framenum;
54 int sequence_framenum;
56 /* static tracking variables */
57 static int previous_temporal_reference;
58 static int previous_bitstream_framenum;
59 static int previous_anchor_temporal_reference;
60 static int previous_anchor_bitstream_framenum;
61 static int previous_picture_coding_type;
64 /* local temporary variables */
65 int substitute_display_framenum;
69 printf("SUB: seq fn(%d) bitfn(%d) tempref(%d) picstr(%d) type(%d)\n",
70 sequence_framenum, bitstream_framenum, temporal_reference,
71 picture_structure, picture_coding_type);
74 /* we don't substitute at the first picture of a sequence */
75 if((sequence_framenum!=0)||(Second_Field))
77 /* only at the start of the frame */
78 if ((picture_structure==FRAME_PICTURE)||(!Second_Field))
80 if(picture_coding_type==P_TYPE)
82 /* the most recently decoded reference frame needs substituting */
83 substitute_display_framenum = bitstream_framenum - 1;
85 Read_Frame(Substitute_Picture_Filename, forward_reference_frame,
86 substitute_display_framenum);
88 /* only the first B frame in a consequitve set of B pictures
89 loads a substitute backward_reference_frame since all subsequent
90 B frames predict from the same reference pictures */
91 else if((picture_coding_type==B_TYPE)&&(bgate!=1))
93 substitute_display_framenum =
94 (previous_temporal_reference - temporal_reference)
95 + bitstream_framenum - 1;
97 Read_Frame(Substitute_Picture_Filename, backward_reference_frame,
98 substitute_display_framenum);
100 } /* P fields can predict from the two most recently decoded fields, even
101 from the first field of the same frame being decoded */
102 else if(Second_Field && (picture_coding_type==P_TYPE))
104 /* our favourite case: the IP field picture pair */
105 if((previous_picture_coding_type==I_TYPE)&&(picture_coding_type==P_TYPE))
107 substitute_display_framenum = bitstream_framenum;
109 else /* our more generic P field picture pair */
111 substitute_display_framenum =
112 (temporal_reference - previous_anchor_temporal_reference)
113 + bitstream_framenum - 1;
116 Read_Frame(Substitute_Picture_Filename, current_frame, substitute_display_framenum);
119 else if((picture_coding_type!=B_TYPE)||(picture_coding_type!=D_TYPE))
121 printf("NO SUBS FOR THIS PICTURE\n");
127 /* set b gate so we don't redundantly load next time around */
128 if(picture_coding_type==B_TYPE)
133 /* update general tracking variables */
134 if((picture_structure==FRAME_PICTURE)||(!Second_Field))
136 previous_temporal_reference = temporal_reference;
137 previous_bitstream_framenum = bitstream_framenum;
140 /* update reference frame tracking variables */
141 if((picture_coding_type!=B_TYPE) &&
142 ((picture_structure==FRAME_PICTURE)||Second_Field))
144 previous_anchor_temporal_reference = temporal_reference;
145 previous_anchor_bitstream_framenum = bitstream_framenum;
148 previous_picture_coding_type = picture_coding_type;
153 /* Note: fields are only read to serve as the same-frame reference for
155 static void Read_Frame(fname,frame,framenum)
157 unsigned char *frame[];
165 printf("ERROR: framenum (%d) is less than zero\n", framenum);
169 rerr = Extract_Components(fname, substitute_frame, framenum);
171 rerr = Read_Components(fname, substitute_frame, framenum);
175 printf("was unable to substitute frame\n");
178 /* now copy to the appropriate buffer */
179 /* first field (which we are attempting to substitute) must be
180 of opposite field parity to the current one */
181 if((Second_Field)&&(picture_coding_type==P_TYPE))
183 parity = (picture_structure==TOP_FIELD ? 1:0);
184 field_mode = (picture_structure==FRAME_PICTURE ? 0:1);
188 /* Like frame structued pictures, B pictures only substitute an entire frame
189 since both fields always predict from the same frame (with respect
190 to forward/backwards directions) */
196 Copy_Frame(substitute_frame[0], frame[0], Coded_Picture_Width,
197 Coded_Picture_Height, parity, field_mode);
199 Copy_Frame(substitute_frame[1], frame[1], Chroma_Width, Chroma_Height,
202 Copy_Frame(substitute_frame[2], frame[2], Chroma_Width, Chroma_Height,
206 if(Verbose_Flag > NO_LAYER)
207 printf("substituted %s %d\n",
208 (field_mode ? (parity?"bottom field":"bottom field"):"frame"), framenum);
215 static int Read_Components(filename, frame, framenum)
217 unsigned char *frame[3];
221 char outname[FILENAME_LENGTH];
222 char name[FILENAME_LENGTH];
224 sprintf(outname,filename,framenum);
227 sprintf(name,"%s.Y",outname);
228 err += Read_Component(name, frame[0], Coded_Picture_Width,
229 Coded_Picture_Height);
231 sprintf(name,"%s.U",outname);
232 err += Read_Component(name, frame[1], Chroma_Width, Chroma_Height);
234 sprintf(name,"%s.V",outname);
235 err += Read_Component(name, frame[2], Chroma_Width, Chroma_Height);
241 static int Read_Component(Filename, Frame, Width, Height)
243 unsigned char *Frame;
254 printf("SUBS: reading %s\n", filename);
257 if(!(Infile=open(Filename,O_RDONLY|O_BINARY))<0)
259 printf("ERROR: unable to open reference filename (%s)\n", Filename);
264 Bytes_Read = read(Infile, Frame, Size);
268 printf("was able to read only %d bytes of %d of file %s\n",
269 Bytes_Read, Size, Filename);
277 /* optimization: do not open the big file each time. Open once at start
278 of decoder, and close at the very last frame */
280 /* Note: "big" files were used in E-mail exchanges almost exclusively by the
281 MPEG Committee's syntax validation and conformance ad-hoc groups from
282 the year 1993 until 1995 */
283 static int Extract_Components(filename, frame, framenum)
285 unsigned char *frame[3];
295 if (!(fd = fopen(filename,"rb")))
297 sprintf(Error_Text,"Couldn't open %s\n",filename);
301 /* compute size of each frame (in bytes) */
302 size = (Coded_Picture_Width*Coded_Picture_Height);
304 if(chroma_format==CHROMA444)
306 else if(chroma_format==CHROMA422)
308 else if(chroma_format==CHROMA420)
309 size = ((size*3)>>1);
311 printf("ERROR: chroma_format (%d) not recognized\n", chroma_format);
314 /* compute distance into "big" file */
315 offset = size*framenum;
318 printf("EXTRACTING: frame(%d) offset(%d), size (%d) from %s\n",
319 framenum, offset, size, filename);
322 /* seek to location in big file where desired frame begins */
323 /* note: this offset cannot exceed a few billion bytes due to the */
324 /* obvious limitations of 32-bit integers */
325 fseek(fd, offset, SEEK_SET);
328 for (line=0; line<Coded_Picture_Height; line++)
330 fread(frame[0]+(line*Coded_Picture_Width),1,Coded_Picture_Width,fd);
334 for (line=0; line<Chroma_Height; line++)
336 fread(frame[1]+(line*Chroma_Width),1,Chroma_Width,fd);
340 for (line=0; line<Chroma_Height; line++)
342 fread(frame[2]+(line*Chroma_Width),1,Chroma_Width,fd);
351 static void Copy_Frame(src, dst, width, height, parity, field_mode)
356 int parity; /* field parity (top or bottom) to overwrite */
357 int field_mode; /* 0 = frame, 1 = field */
366 printf("COPYING (w=%d, h=%d, parity=%d, field_mode=%d)\n",
367 width,height,parity,field_mode);
382 for(row=0; row<height; row+=incr)
384 for(col=0; col<width; col++)
386 dst[d+col] = src[s+col];