1 /*===========================================================================*
2 * frames.h
3 *
4 * stuff dealing with frames
5 *
6 *===========================================================================*/
7
8 #ifndef FRAMES_INCLUDED
9 #define FRAMES_INCLUDED
10
11 /*==============*
12 * HEADER FILES *
13 *==============*/
14
15 #include "pm_config.h" /* For __inline__ */
16 #include "mtypes.h"
17 #include "mheaders.h"
18 #include "iframe.h"
19 #include "frame.h"
20
21
22 /*===========*
23 * CONSTANTS *
24 *===========*/
25
26 #define I_FRAME 1
27 #define P_FRAME 2
28 #define B_FRAME 3
29
30 #define LUM_BLOCK 0
31 #define CHROM_BLOCK 1
32 #define CR_BLOCK 2
33 #define CB_BLOCK 3
34
35 #define MOTION_FORWARD 0
36 #define MOTION_BACKWARD 1
37 #define MOTION_INTERPOLATE 2
38
39
40 #define USE_HALF 0
41 #define USE_FULL 1
42
43 /* motion vector stuff */
44 #define FORW_F_CODE fCode /* from picture header */
45 #define BACK_F_CODE fCode
46 #define FORW_F (1 << (FORW_F_CODE - 1))
47 #define BACK_F (1 << (BACK_F_CODE - 1))
48 #define RANGE_NEG (-(1 << (3 + FORW_F_CODE)))
49 #define RANGE_POS ((1 << (3 + FORW_F_CODE))-1)
50 #define MODULUS (1 << (4 + FORW_F_CODE))
51
52 #define ORIGINAL_FRAME 0
53 #define DECODED_FRAME 1
54
55
56 /*=======================*
57 * STRUCTURE DEFINITIONS *
58 *=======================*/
59
60 typedef struct FrameTableStruct {
61 /* the following are all initted once and never changed */
62 /* (they depend only on the pattern */
63 char typ;
64 struct FrameTableStruct *next;
65 struct FrameTableStruct *prev;
66
67 /* nextOutput is a pointer to next frame table entry to output */
68 struct FrameTableStruct *nextOutput;
69
70 boolean freeNow; /* TRUE iff no frames point back to this */
71
72 int number;
73
74 int bFrameNumber; /* actual frame number, if a b-frame */
75
76 } FrameTable;
77
78
79 /*==================*
80 * TYPE DEFINITIONS *
81 *==================*/
82
83 typedef struct dct_data_tye_struct {
84 char useMotion;
85 char pattern, mode;
86 int fmotionX, fmotionY, bmotionX, bmotionY;
87 } dct_data_type;
88
89
90 /*==================*
91 * GLOBAL VARIABLES *
92 *==================*/
93
94 extern Block **dct, **dctr, **dctb;
95 extern dct_data_type **dct_data;
96
97
98 /*========*
99 * MACROS *
100 *========*/
101
102 /* return ceiling(a/b) where a, b are ints, using temp value c */
103 #define int_ceil_div(a,b,c) ((b*(c = a/b) < a) ? (c+1) : c)
104 #define int_floor_div(a,b,c) ((b*(c = a/b) > a) ? (c-1) : c)
105
106 #define BLOCK_TO_FRAME_COORD(bx1, bx2, x1, x2) { \
107 x1 = (bx1)*DCTSIZE; \
108 x2 = (bx2)*DCTSIZE; \
109 }
110
111 static __inline__ void
MotionToFrameCoord(int const by,int const bx,int const my,int const mx,int * const yP,int * const xP)112 MotionToFrameCoord(int const by,
113 int const bx,
114 int const my,
115 int const mx,
116 int * const yP,
117 int * const xP) {
118 /*----------------------------------------------------------------------------
119 Given a DCT block location and a motion vector, return the pixel
120 coordinates to which the upper left corner of the block moves.
121
122 Return negative coordinates if it moves out of frame.
123 -----------------------------------------------------------------------------*/
124 *yP = by * DCTSIZE + my;
125 *xP = bx * DCTSIZE + mx;
126 }
127
128 #define COORD_IN_FRAME(fy,fx, type) \
129 ((type == LUM_BLOCK) ? \
130 ((fy >= 0) && (fx >= 0) && (fy < Fsize_y) && (fx < Fsize_x)) : \
131 ((fy >= 0) && (fx >= 0) && (fy < (Fsize_y>>1)) && (fx < (Fsize_x>>1))))
132
133
134 static __inline__ void
encodeMotionVector(int const x,int const y,vector * const qP,vector * const rP,int const f,int const fCode)135 encodeMotionVector(int const x,
136 int const y,
137 vector * const qP,
138 vector * const rP,
139 int const f,
140 int const fCode) {
141
142 int tempX;
143 int tempY;
144 int tempC;
145
146 if (x < RANGE_NEG)
147 tempX = x + MODULUS;
148 else if (x > RANGE_POS)
149 tempX = x - MODULUS;
150 else
151 tempX = x;
152
153 if (y < RANGE_NEG)
154 tempY = y + MODULUS;
155 else if (y > RANGE_POS)
156 tempY = y - MODULUS;
157 else
158 tempY = y;
159
160 if (tempX >= 0) {
161 qP->x = int_ceil_div(tempX, f, tempC);
162 rP->x = f - 1 + tempX - qP->x*f;
163 } else {
164 qP->x = int_floor_div(tempX, f, tempC);
165 rP->x = f - 1 - tempX + qP->x*f;
166 }
167
168 if (tempY >= 0) {
169 qP->y = int_ceil_div(tempY, f, tempC);
170 rP->y = f - 1 + tempY - qP->y*f;
171 } else {
172 qP->y = int_floor_div(tempY, f, tempC);
173 rP->y = f - 1 - tempY + qP->y*f;
174 }
175 }
176
177
178 #define DoQuant(bit, src, dest) \
179 if (pattern & bit) { \
180 switch (Mpost_QuantZigBlock(src, dest, QScale, FALSE)) { \
181 case MPOST_NON_ZERO: \
182 break; \
183 case MPOST_ZERO: \
184 pattern ^= bit; \
185 break; \
186 case MPOST_OVERFLOW: \
187 if (QScale != 31) { \
188 QScale++; \
189 overflowChange = TRUE; \
190 overflowValue++; \
191 goto calc_blocks; \
192 } \
193 break; \
194 } \
195 }
196
197 /*===============================*
198 * EXTERNAL PROCEDURE prototypes *
199 *===============================*/
200
201 void
202 AllocDctBlocks(void);
203
204 void
205 ComputeBMotionLumBlock(MpegFrame * const prev,
206 MpegFrame * const next,
207 int const by,
208 int const bx,
209 int const mode,
210 motion const motion,
211 LumBlock * const motionBlockP);
212
213 int
214 BMotionSearch(const LumBlock * const currentBlockP,
215 MpegFrame * const prev,
216 MpegFrame * const next,
217 int const by,
218 int const bx,
219 motion * const motionP,
220 int const oldMode);
221
222 void GenPFrame (BitBucket * const bb,
223 MpegFrame * const current,
224 MpegFrame * const prev);
225 void GenBFrame (BitBucket * const bb,
226 MpegFrame * const curr,
227 MpegFrame * const prev,
228 MpegFrame * const next);
229
230 float
231 PFrameTotalTime(void);
232
233 float
234 BFrameTotalTime(void);
235
236 void
237 ShowPFrameSummary(unsigned int const inputFrameBits,
238 unsigned int const totalBits,
239 FILE * const fpointer);
240
241 void
242 ShowBFrameSummary(unsigned int const inputFrameBits,
243 unsigned int const totalBits,
244 FILE * const fpointer);
245
246 /*==================*
247 * GLOBAL VARIABLES *
248 *==================*/
249
250 extern int pixelFullSearch;
251 extern int searchRangeP,searchRangeB; /* defined in psearch.c */
252 extern int qscaleI;
253 extern int gopSize;
254 extern int slicesPerFrame;
255 extern int blocksPerSlice;
256 extern int referenceFrame;
257 extern int quietTime; /* shut up for at least quietTime seconds;
258 * negative means shut up forever
259 */
260 extern boolean realQuiet; /* TRUE = no messages to stdout */
261
262 extern boolean frameSummary; /* TRUE = frame summaries should be printed */
263 extern boolean printSNR;
264 extern boolean printMSE;
265 extern boolean decodeRefFrames; /* TRUE = should decode I and P frames */
266 extern int fCodeI,fCodeP,fCodeB;
267 extern boolean forceEncodeLast;
268 extern int TIME_RATE;
269
270 #endif
271
272
273 /*
274 * Copyright (c) 1995 The Regents of the University of California.
275 * All rights reserved.
276 *
277 * Permission to use, copy, modify, and distribute this software and its
278 * documentation for any purpose, without fee, and without written agreement is
279 * hereby granted, provided that the above copyright notice and the following
280 * two paragraphs appear in all copies of this software.
281 *
282 * IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR
283 * DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES ARISING OUT
284 * OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
285 * CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
286 *
287 * THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
288 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
289 * AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE PROVIDED HEREUNDER IS
290 * ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
291 * PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
292 */
293
294 /*
295 * $Header: /n/picasso/project/mpeg/mpeg_dist/mpeg_encode/headers/RCS/frames.h,v 1.13 1995/08/15 23:43:04 smoot Exp $
296 * $Log: frames.h,v $
297 * Revision 1.13 1995/08/15 23:43:04 smoot
298 * *** empty log message ***
299 *
300 * Revision 1.12 1995/04/14 23:13:18 smoot
301 * Reorganized for better rate control. Added overflow in DCT values
302 * handling.
303 *
304 * Revision 1.11 1995/01/19 23:54:46 smoot
305 * allow computediffdcts to un-assert parts of the pattern
306 *
307 * Revision 1.10 1995/01/16 07:43:10 eyhung
308 * Added realQuiet
309 *
310 * Revision 1.9 1995/01/10 23:15:28 smoot
311 * Fixed searchRange lack of def
312 *
313 * Revision 1.8 1994/11/15 00:55:36 smoot
314 * added printMSE
315 *
316 * Revision 1.7 1994/11/14 22:51:02 smoot
317 * added specifics flag. Added BlockComputeSNR parameters
318 *
319 * Revision 1.6 1994/11/01 05:07:23 darryl
320 * with rate control changes added
321 *
322 * Revision 1.1 1994/09/27 01:02:55 darryl
323 * Initial revision
324 *
325 * Revision 1.5 1993/07/22 22:24:23 keving
326 * nothing
327 *
328 * Revision 1.4 1993/07/09 00:17:23 keving
329 * nothing
330 *
331 * Revision 1.3 1993/06/03 21:08:53 keving
332 * nothing
333 *
334 * Revision 1.2 1993/03/02 19:00:27 keving
335 * nothing
336 *
337 * Revision 1.1 1993/02/19 20:15:51 keving
338 * nothing
339 *
340 */
341
342
343