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
34 #include <unistd.h> // for read
39 /* private prototypes*/
40 static void Read_Frame _ANSI_ARGS_((char *filename,
41 unsigned char *frame_buffer[], int framenum));
42 static void Copy_Frame _ANSI_ARGS_((unsigned char *src, unsigned char *dst,
43 int width, int height, int parity, int incr));
44 static int Read_Components _ANSI_ARGS_ ((char *filename,
45 unsigned char *frame[3], int framenum));
46 static int Read_Component _ANSI_ARGS_ ((char *fname, unsigned char *frame,
47 int width, int height));
48 static int Extract_Components _ANSI_ARGS_ ((char *filename,
49 unsigned char *frame[3], int framenum));
52 /* substitute frame buffer routine */
53 void Substitute_Frame_Buffer (bitstream_framenum, sequence_framenum)
54 int bitstream_framenum;
55 int sequence_framenum;
57 /* static tracking variables */
58 static int previous_temporal_reference;
59 static int previous_bitstream_framenum;
60 static int previous_anchor_temporal_reference;
61 static int previous_anchor_bitstream_framenum;
62 static int previous_picture_coding_type;
65 /* local temporary variables */
66 int substitute_display_framenum;
70 printf("SUB: seq fn(%d) bitfn(%d) tempref(%d) picstr(%d) type(%d)\n",
71 sequence_framenum, bitstream_framenum, temporal_reference,
72 picture_structure, picture_coding_type);
75 /* we don't substitute at the first picture of a sequence */
76 if((sequence_framenum!=0)||(Second_Field))
78 /* only at the start of the frame */
79 if ((picture_structure==FRAME_PICTURE)||(!Second_Field))
81 if(picture_coding_type==P_TYPE)
83 /* the most recently decoded reference frame needs substituting */
84 substitute_display_framenum = bitstream_framenum - 1;
86 Read_Frame(Substitute_Picture_Filename, forward_reference_frame,
87 substitute_display_framenum);
89 /* only the first B frame in a consequitve set of B pictures
90 loads a substitute backward_reference_frame since all subsequent
91 B frames predict from the same reference pictures */
92 else if((picture_coding_type==B_TYPE)&&(bgate!=1))
94 substitute_display_framenum =
95 (previous_temporal_reference - temporal_reference)
96 + bitstream_framenum - 1;
98 Read_Frame(Substitute_Picture_Filename, backward_reference_frame,
99 substitute_display_framenum);
101 } /* P fields can predict from the two most recently decoded fields, even
102 from the first field of the same frame being decoded */
103 else if(Second_Field && (picture_coding_type==P_TYPE))
105 /* our favourite case: the IP field picture pair */
106 if((previous_picture_coding_type==I_TYPE)&&(picture_coding_type==P_TYPE))
108 substitute_display_framenum = bitstream_framenum;
110 else /* our more generic P field picture pair */
112 substitute_display_framenum =
113 (temporal_reference - previous_anchor_temporal_reference)
114 + bitstream_framenum - 1;
117 Read_Frame(Substitute_Picture_Filename, current_frame, substitute_display_framenum);
120 else if((picture_coding_type!=B_TYPE)||(picture_coding_type!=D_TYPE))
122 printf("NO SUBS FOR THIS PICTURE\n");
128 /* set b gate so we don't redundantly load next time around */
129 if(picture_coding_type==B_TYPE)
134 /* update general tracking variables */
135 if((picture_structure==FRAME_PICTURE)||(!Second_Field))
137 previous_temporal_reference = temporal_reference;
138 previous_bitstream_framenum = bitstream_framenum;
141 /* update reference frame tracking variables */
142 if((picture_coding_type!=B_TYPE) &&
143 ((picture_structure==FRAME_PICTURE)||Second_Field))
145 previous_anchor_temporal_reference = temporal_reference;
146 previous_anchor_bitstream_framenum = bitstream_framenum;
149 previous_picture_coding_type = picture_coding_type;
154 /* Note: fields are only read to serve as the same-frame reference for
156 static void Read_Frame(fname,frame,framenum)
158 unsigned char *frame[];
166 printf("ERROR: framenum (%d) is less than zero\n", framenum);
170 rerr = Extract_Components(fname, substitute_frame, framenum);
172 rerr = Read_Components(fname, substitute_frame, framenum);
176 printf("was unable to substitute frame\n");
179 /* now copy to the appropriate buffer */
180 /* first field (which we are attempting to substitute) must be
181 of opposite field parity to the current one */
182 if((Second_Field)&&(picture_coding_type==P_TYPE))
184 parity = (picture_structure==TOP_FIELD ? 1:0);
185 field_mode = (picture_structure==FRAME_PICTURE ? 0:1);
189 /* Like frame structued pictures, B pictures only substitute an entire frame
190 since both fields always predict from the same frame (with respect
191 to forward/backwards directions) */
197 Copy_Frame(substitute_frame[0], frame[0], Coded_Picture_Width,
198 Coded_Picture_Height, parity, field_mode);
200 Copy_Frame(substitute_frame[1], frame[1], Chroma_Width, Chroma_Height,
203 Copy_Frame(substitute_frame[2], frame[2], Chroma_Width, Chroma_Height,
207 if(Verbose_Flag > NO_LAYER)
208 printf("substituted %s %d\n",
209 (field_mode ? (parity?"bottom field":"bottom field"):"frame"), framenum);
216 static int Read_Components(filename, frame, framenum)
218 unsigned char *frame[3];
222 char outname[FILENAME_LENGTH];
223 char name[FILENAME_LENGTH];
225 sprintf(outname,filename,framenum);
228 sprintf(name,"%s.Y",outname);
229 err += Read_Component(name, frame[0], Coded_Picture_Width,
230 Coded_Picture_Height);
232 sprintf(name,"%s.U",outname);
233 err += Read_Component(name, frame[1], Chroma_Width, Chroma_Height);
235 sprintf(name,"%s.V",outname);
236 err += Read_Component(name, frame[2], Chroma_Width, Chroma_Height);
242 static int Read_Component(Filename, Frame, Width, Height)
244 unsigned char *Frame;
255 printf("SUBS: reading %s\n", filename);
258 if(!(Infile=open(Filename,O_RDONLY|O_BINARY))<0)
260 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];
294 if (!(fd = fopen(filename,"rb")))
296 sprintf(Error_Text,"Couldn't open %s\n",filename);
300 /* compute size of each frame (in bytes) */
301 size = (Coded_Picture_Width*Coded_Picture_Height);
303 if(chroma_format==CHROMA444)
305 else if(chroma_format==CHROMA422)
307 else if(chroma_format==CHROMA420)
308 size = ((size*3)>>1);
310 printf("ERROR: chroma_format (%d) not recognized\n", chroma_format);
313 /* compute distance into "big" file */
314 offset = size*framenum;
317 printf("EXTRACTING: frame(%d) offset(%d), size (%d) from %s\n",
318 framenum, offset, size, filename);
321 /* seek to location in big file where desired frame begins */
322 /* note: this offset cannot exceed a few billion bytes due to the */
323 /* obvious limitations of 32-bit integers */
324 fseek(fd, offset, 0);
327 for (line=0; line<Coded_Picture_Height; line++)
329 fread(frame[0]+(line*Coded_Picture_Width),1,Coded_Picture_Width,fd);
333 for (line=0; line<Chroma_Height; line++)
335 fread(frame[1]+(line*Chroma_Width),1,Chroma_Width,fd);
339 for (line=0; line<Chroma_Height; line++)
341 fread(frame[2]+(line*Chroma_Width),1,Chroma_Width,fd);
350 static void Copy_Frame(src, dst, width, height, parity, field_mode)
355 int parity; /* field parity (top or bottom) to overwrite */
356 int field_mode; /* 0 = frame, 1 = field */
365 printf("COPYING (w=%d, h=%d, parity=%d, field_mode=%d)\n",
366 width,height,parity,field_mode);
381 for(row=0; row<height; row+=incr)
383 for(col=0; col<width; col++)
385 dst[d+col] = src[s+col];