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
35 /* private prototypes*/
36 static void Read_Frame _ANSI_ARGS_((char *filename,
37 unsigned char *frame_buffer[], int framenum));
38 static void Copy_Frame _ANSI_ARGS_((unsigned char *src, unsigned char *dst,
39 int width, int height, int parity, int incr));
40 static int Read_Components _ANSI_ARGS_ ((char *filename,
41 unsigned char *frame[3], int framenum));
42 static int Read_Component _ANSI_ARGS_ ((char *fname, unsigned char *frame,
43 int width, int height));
44 static int Extract_Components _ANSI_ARGS_ ((char *filename,
45 unsigned char *frame[3], int framenum));
48 /* substitute frame buffer routine */
49 void Substitute_Frame_Buffer (bitstream_framenum, sequence_framenum)
50 int bitstream_framenum;
51 int sequence_framenum;
53 /* static tracking variables */
54 static int previous_temporal_reference;
55 static int previous_bitstream_framenum;
56 static int previous_anchor_temporal_reference;
57 static int previous_anchor_bitstream_framenum;
58 static int previous_picture_coding_type;
61 /* local temporary variables */
62 int substitute_display_framenum;
66 my_printf("SUB: seq fn(%d) bitfn(%d) tempref(%d) picstr(%d) type(%d)\n",
67 sequence_framenum, bitstream_framenum, temporal_reference,
68 picture_structure, picture_coding_type);
71 /* we don't substitute at the first picture of a sequence */
72 if((sequence_framenum!=0)||(Second_Field))
74 /* only at the start of the frame */
75 if ((picture_structure==FRAME_PICTURE)||(!Second_Field))
77 if(picture_coding_type==P_TYPE)
79 /* the most recently decoded reference frame needs substituting */
80 substitute_display_framenum = bitstream_framenum - 1;
82 Read_Frame(Substitute_Picture_Filename, forward_reference_frame,
83 substitute_display_framenum);
85 /* only the first B frame in a consequitve set of B pictures
86 loads a substitute backward_reference_frame since all subsequent
87 B frames predict from the same reference pictures */
88 else if((picture_coding_type==B_TYPE)&&(bgate!=1))
90 substitute_display_framenum =
91 (previous_temporal_reference - temporal_reference)
92 + bitstream_framenum - 1;
94 Read_Frame(Substitute_Picture_Filename, backward_reference_frame,
95 substitute_display_framenum);
97 } /* P fields can predict from the two most recently decoded fields, even
98 from the first field of the same frame being decoded */
99 else if(Second_Field && (picture_coding_type==P_TYPE))
101 /* our favourite case: the IP field picture pair */
102 if((previous_picture_coding_type==I_TYPE)&&(picture_coding_type==P_TYPE))
104 substitute_display_framenum = bitstream_framenum;
106 else /* our more generic P field picture pair */
108 substitute_display_framenum =
109 (temporal_reference - previous_anchor_temporal_reference)
110 + bitstream_framenum - 1;
113 Read_Frame(Substitute_Picture_Filename, current_frame, substitute_display_framenum);
116 else if((picture_coding_type!=B_TYPE)||(picture_coding_type!=D_TYPE))
118 my_printf("NO SUBS FOR THIS PICTURE\n");
124 /* set b gate so we don't redundantly load next time around */
125 if(picture_coding_type==B_TYPE)
130 /* update general tracking variables */
131 if((picture_structure==FRAME_PICTURE)||(!Second_Field))
133 previous_temporal_reference = temporal_reference;
134 previous_bitstream_framenum = bitstream_framenum;
137 /* update reference frame tracking variables */
138 if((picture_coding_type!=B_TYPE) &&
139 ((picture_structure==FRAME_PICTURE)||Second_Field))
141 previous_anchor_temporal_reference = temporal_reference;
142 previous_anchor_bitstream_framenum = bitstream_framenum;
145 previous_picture_coding_type = picture_coding_type;
150 /* Note: fields are only read to serve as the same-frame reference for
152 static void Read_Frame(fname,frame,framenum)
154 unsigned char *frame[];
162 my_printf("ERROR: framenum (%d) is less than zero\n", framenum);
166 rerr = Extract_Components(fname, substitute_frame, framenum);
168 rerr = Read_Components(fname, substitute_frame, framenum);
172 my_printf("was unable to substitute frame\n");
175 /* now copy to the appropriate buffer */
176 /* first field (which we are attempting to substitute) must be
177 of opposite field parity to the current one */
178 if((Second_Field)&&(picture_coding_type==P_TYPE))
180 parity = (picture_structure==TOP_FIELD ? 1:0);
181 field_mode = (picture_structure==FRAME_PICTURE ? 0:1);
185 /* Like frame structued pictures, B pictures only substitute an entire frame
186 since both fields always predict from the same frame (with respect
187 to forward/backwards directions) */
193 Copy_Frame(substitute_frame[0], frame[0], Coded_Picture_Width,
194 Coded_Picture_Height, parity, field_mode);
196 Copy_Frame(substitute_frame[1], frame[1], Chroma_Width, Chroma_Height,
199 Copy_Frame(substitute_frame[2], frame[2], Chroma_Width, Chroma_Height,
203 if(Verbose_Flag > NO_LAYER)
204 my_printf("substituted %s %d\n",
205 (field_mode ? (parity?"bottom field":"bottom field"):"frame"), framenum);
212 static int Read_Components(filename, frame, framenum)
214 unsigned char *frame[3];
218 char outname[FILENAME_LENGTH];
219 char name[FILENAME_LENGTH];
221 my_sprintf(outname,filename,framenum);
224 my_sprintf(name,"%s.Y",outname);
225 err += Read_Component(name, frame[0], Coded_Picture_Width,
226 Coded_Picture_Height);
228 my_sprintf(name,"%s.U",outname);
229 err += Read_Component(name, frame[1], Chroma_Width, Chroma_Height);
231 my_sprintf(name,"%s.V",outname);
232 err += Read_Component(name, frame[2], Chroma_Width, Chroma_Height);
238 static int Read_Component(Filename, Frame, Width, Height)
240 unsigned char *Frame;
251 my_printf("SUBS: reading %s\n", filename);
254 if(!(Infile=my_open(Filename))<0)
256 my_printf("ERROR: unable to open reference filename (%s)\n", Filename);
261 Bytes_Read = read(Infile, Frame, Size);
265 my_printf("was able to read only %d bytes of %d of file %s\n",
266 Bytes_Read, Size, Filename);
274 /* optimization: do not open the big file each time. Open once at start
275 of decoder, and close at the very last frame */
277 /* Note: "big" files were used in E-mail exchanges almost exclusively by the
278 MPEG Committee's syntax validation and conformance ad-hoc groups from
279 the year 1993 until 1995 */
280 static int Extract_Components(filename, frame, framenum)
282 unsigned char *frame[3];
293 if (!(fd = my_fopen(filename,"rb")))
295 my_sprintf(Error_Text,"Couldn't open %s\n",filename);
299 /* compute size of each frame (in bytes) */
300 size = (Coded_Picture_Width*Coded_Picture_Height);
302 if(chroma_format==CHROMA444)
304 else if(chroma_format==CHROMA422)
306 else if(chroma_format==CHROMA420)
307 size = ((size*3)>>1);
309 my_printf("ERROR: chroma_format (%d) not recognized\n", chroma_format);
312 /* compute distance into "big" file */
313 offset = size*framenum;
316 my_printf("EXTRACTING: frame(%d) offset(%d), size (%d) from %s\n",
317 framenum, offset, size, filename);
320 /* seek to location in big file where desired frame begins */
321 /* note: this offset cannot exceed a few billion bytes due to the */
322 /* obvious limitations of 32-bit integers */
323 my_fseek(fd, offset, SEEK_SET);
326 for (line=0; line<Coded_Picture_Height; line++)
328 my_fread(frame[0]+(line*Coded_Picture_Width),1,Coded_Picture_Width,fd);
332 for (line=0; line<Chroma_Height; line++)
334 my_fread(frame[1]+(line*Chroma_Width),1,Chroma_Width,fd);
338 for (line=0; line<Chroma_Height; line++)
340 my_fread(frame[2]+(line*Chroma_Width),1,Chroma_Width,fd);
349 static void Copy_Frame(src, dst, width, height, parity, field_mode)
354 int parity; /* field parity (top or bottom) to overwrite */
355 int field_mode; /* 0 = frame, 1 = field */
364 my_printf("COPYING (w=%d, h=%d, parity=%d, field_mode=%d)\n",
365 width,height,parity,field_mode);
380 for(row=0; row<height; row+=incr)
382 for(col=0; col<width; col++)
384 dst[d+col] = src[s+col];