[svn] / branches / dev-api-4 / xvidcore / src / motion / vop_type_decision.c Repository:
ViewVC logotype

Annotation of /branches/dev-api-4/xvidcore/src/motion/vop_type_decision.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1239 - (view) (download)

1 : edgomez 1142 /*****************************************************************************
2 :     *
3 :     * XVID MPEG-4 VIDEO CODEC
4 :     * - ME-based Frame Type Decision -
5 :     *
6 :     * Copyright(C) 2002-2003 Radoslaw Czyz <xvid@syskin.cjb.net>
7 :     *
8 :     * This program is free software ; you can redistribute it and/or modify
9 :     * it under the terms of the GNU General Public License as published by
10 :     * the Free Software Foundation ; either version 2 of the License, or
11 :     * (at your option) any later version.
12 :     *
13 :     * This program is distributed in the hope that it will be useful,
14 :     * but WITHOUT ANY WARRANTY ; without even the implied warranty of
15 :     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 :     * GNU General Public License for more details.
17 :     *
18 :     * You should have received a copy of the GNU General Public License
19 :     * along with this program ; if not, write to the Free Software
20 :     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 :     *
22 : syskin 1239 * $Id: vop_type_decision.c,v 1.1.2.6 2003-12-04 12:08:03 syskin Exp $
23 : edgomez 1142 *
24 :     ****************************************************************************/
25 :    
26 :     #include "../encoder.h"
27 :     #include "../prediction/mbprediction.h"
28 :     #include "estimation.h"
29 :     #include "motion.h"
30 :     #include "sad.h"
31 :     #include "gmc.h"
32 :     #include "../utils/emms.h"
33 :     #include "motion_inlines.h"
34 :    
35 :    
36 : syskin 1211 #define INTRA_THRESH 2000
37 : edgomez 1142 #define INTER_THRESH 40
38 : syskin 1211 #define INTRA_THRESH2 90
39 : edgomez 1142
40 : syskin 1239 /* when we are in 1/I_SENS_TH before forced keyframe, we start to decrese i-frame threshold */
41 :     #define I_SENS_TH 3
42 :    
43 :     /* how much we subtract from each p-frame threshold for 2nd, 3rd etc. b-frame in a row */
44 :     #define P_SENS_BIAS 18
45 :    
46 :     /* .. but never below INTER_THRESH_MIN */
47 :     #define INTER_THRESH_MIN 5
48 :    
49 : edgomez 1142 static void
50 : syskin 1215 CheckCandidate32I(const int x, const int y, SearchData * const data, const unsigned int Direction)
51 : edgomez 1142 {
52 :     /* maximum speed */
53 :     int32_t sad;
54 :    
55 :     if ( (x > data->max_dx) || (x < data->min_dx)
56 :     || (y > data->max_dy) || (y < data->min_dy) ) return;
57 :    
58 :     sad = sad32v_c(data->Cur, data->RefP[0] + x + y*((int)data->iEdgedWidth),
59 :     data->iEdgedWidth, data->temp);
60 :    
61 :     if (sad < *(data->iMinSAD)) {
62 :     *(data->iMinSAD) = sad;
63 :     data->currentMV[0].x = x; data->currentMV[0].y = y;
64 : syskin 1215 data->dir = Direction;
65 : edgomez 1142 }
66 :     if (data->temp[0] < data->iMinSAD[1]) {
67 :     data->iMinSAD[1] = data->temp[0]; data->currentMV[1].x = x; data->currentMV[1].y = y; }
68 :     if (data->temp[1] < data->iMinSAD[2]) {
69 :     data->iMinSAD[2] = data->temp[1]; data->currentMV[2].x = x; data->currentMV[2].y = y; }
70 :     if (data->temp[2] < data->iMinSAD[3]) {
71 :     data->iMinSAD[3] = data->temp[2]; data->currentMV[3].x = x; data->currentMV[3].y = y; }
72 :     if (data->temp[3] < data->iMinSAD[4]) {
73 :     data->iMinSAD[4] = data->temp[3]; data->currentMV[4].x = x; data->currentMV[4].y = y; }
74 :     }
75 :    
76 :     static __inline void
77 :     MEanalyzeMB ( const uint8_t * const pRef,
78 :     const uint8_t * const pCur,
79 :     const int x,
80 :     const int y,
81 :     const MBParam * const pParam,
82 :     MACROBLOCK * const pMBs,
83 :     SearchData * const Data)
84 :     {
85 :    
86 :     int i;
87 :     VECTOR pmv[3];
88 :     MACROBLOCK * const pMB = &pMBs[x + y * pParam->mb_width];
89 :    
90 :     unsigned int simplicity = 0;
91 :    
92 :     for (i = 0; i < 5; i++) Data->iMinSAD[i] = MV_MAX_ERROR;
93 :    
94 :     get_range(&Data->min_dx, &Data->max_dx, &Data->min_dy, &Data->max_dy, x, y, 4,
95 :     pParam->width, pParam->height, Data->iFcode - Data->qpel - 1, 0, 0);
96 :    
97 :     Data->Cur = pCur + (x + y * pParam->edged_width) * 16;
98 :     Data->RefP[0] = pRef + (x + y * pParam->edged_width) * 16;
99 :    
100 :     pmv[0].x = pMB->mvs[0].x;
101 :     pmv[0].y = pMB->mvs[0].y;
102 :    
103 :     CheckCandidate32I(pmv[0].x, pmv[0].y, Data, 0);
104 :    
105 :     if (*Data->iMinSAD > 200) {
106 :    
107 :     pmv[1].x = pmv[1].y = 0;
108 :    
109 :     /* median is only used as prediction. it doesn't have to be real */
110 :     if (x == 1 && y == 1) Data->predMV.x = Data->predMV.y = 0;
111 :     else
112 :     if (x == 1) /* left macroblock does not have any vector now */
113 :     Data->predMV = (pMB - pParam->mb_width)->mvs[0]; /* top instead of median */
114 :     else if (y == 1) /* top macroblock doesn't have it's vector */
115 :     Data->predMV = (pMB - 1)->mvs[0]; /* left instead of median */
116 :     else Data->predMV = get_pmv2(pMBs, pParam->mb_width, 0, x, y, 0); /* else median */
117 :    
118 :     pmv[2].x = Data->predMV.x;
119 :     pmv[2].y = Data->predMV.y;
120 :    
121 :     if (!vector_repeats(pmv, 1))
122 :     CheckCandidate32I(pmv[1].x, pmv[1].y, Data, 1);
123 :     if (!vector_repeats(pmv, 2))
124 :     CheckCandidate32I(pmv[2].x, pmv[2].y, Data, 2);
125 :    
126 :     if (*Data->iMinSAD > 500) { /* diamond only if needed */
127 : syskin 1215 unsigned int mask = make_mask(pmv, 3, Data->dir);
128 : edgomez 1142 xvid_me_DiamondSearch(Data->currentMV->x, Data->currentMV->y, Data, mask, CheckCandidate32I);
129 :     } else simplicity++;
130 :    
131 :     if (*Data->iMinSAD > 500) /* refinement from 2-pixel to 1-pixel */
132 :     xvid_me_SubpelRefine(Data, CheckCandidate32I);
133 :     else simplicity++;
134 :     } else simplicity++;
135 :    
136 :     for (i = 0; i < 4; i++) {
137 :     MACROBLOCK * MB = &pMBs[x + (i&1) + (y+(i>>1)) * pParam->mb_width];
138 :     MB->mvs[0] = MB->mvs[1] = MB->mvs[2] = MB->mvs[3] = Data->currentMV[i];
139 :     MB->mode = MODE_INTER;
140 :     /* if we skipped some search steps, we have to assume that SAD would be lower with them */
141 :     MB->sad16 = Data->iMinSAD[i+1] - (simplicity<<7);
142 :     }
143 :     }
144 :    
145 :     int
146 :     MEanalysis( const IMAGE * const pRef,
147 :     const FRAMEINFO * const Current,
148 :     const MBParam * const pParam,
149 : edgomez 1160 const int maxIntra, /* maximum number if non-I frames */
150 :     const int intraCount, /* number of non-I frames after last I frame; 0 if we force P/B frame */
151 :     const int bCount, /* number of B frames in a row */
152 : edgomez 1212 const int b_thresh,
153 :     const MACROBLOCK * const prev_mbs)
154 : edgomez 1142 {
155 :     uint32_t x, y, intra = 0;
156 :     int sSAD = 0;
157 :     MACROBLOCK * const pMBs = Current->mbs;
158 :     const IMAGE * const pCurrent = &Current->image;
159 : syskin 1239 int IntraThresh = INTRA_THRESH,
160 :     InterThresh = INTER_THRESH + b_thresh,
161 :     IntraThresh2 = INTRA_THRESH2;
162 :    
163 : syskin 1211 int blocks = 10;
164 : edgomez 1142 int complexity = 0;
165 :    
166 :     SearchData Data;
167 :     Data.iEdgedWidth = pParam->edged_width;
168 :     Data.iFcode = Current->fcode;
169 :     Data.qpel = (pParam->vol_flags & XVID_VOL_QUARTERPEL)? 1: 0;
170 :     Data.qpel_precision = 0;
171 :    
172 :     if (intraCount != 0) {
173 : syskin 1239 if (intraCount < 30) {
174 :     /* we're right after an I frame
175 :     we increase thresholds to prevent consecutive i-frames */
176 :     if (intraCount < 10) IntraThresh += 15*(10 - intraCount)*(10 - intraCount);
177 :     IntraThresh2 += 4*(30 - intraCount);
178 :     } else if (I_SENS_TH*(maxIntra - intraCount) < maxIntra) {
179 :     /* we're close to maximum. we decrease thresholds to catch any good keyframe */
180 :     IntraThresh -= IntraThresh*((maxIntra - I_SENS_TH*(maxIntra - intraCount))/maxIntra);
181 :     IntraThresh2 -= IntraThresh2*((maxIntra - I_SENS_TH*(maxIntra - intraCount))/maxIntra);
182 :     }
183 : edgomez 1142 }
184 :    
185 : syskin 1239 InterThresh -= P_SENS_BIAS * bCount;
186 :     if (InterThresh < INTER_THRESH_MIN) InterThresh = INTER_THRESH_MIN;
187 : edgomez 1142
188 :     if (sadInit) (*sadInit) ();
189 :    
190 :     for (y = 1; y < pParam->mb_height-1; y += 2) {
191 :     for (x = 1; x < pParam->mb_width-1; x += 2) {
192 :     int i;
193 :     blocks += 10;
194 :    
195 :     if (bCount == 0) pMBs[x + y * pParam->mb_width].mvs[0] = zeroMV;
196 : edgomez 1160 else { /* extrapolation of the vector found for last frame */
197 : edgomez 1142 pMBs[x + y * pParam->mb_width].mvs[0].x =
198 : edgomez 1212 (prev_mbs[x + y * pParam->mb_width].mvs[0].x * (bCount+1) ) / bCount;
199 : edgomez 1142 pMBs[x + y * pParam->mb_width].mvs[0].y =
200 : edgomez 1212 (prev_mbs[x + y * pParam->mb_width].mvs[0].y * (bCount+1) ) / bCount;
201 : edgomez 1142 }
202 :    
203 :     MEanalyzeMB(pRef->y, pCurrent->y, x, y, pParam, pMBs, &Data);
204 :    
205 :     for (i = 0; i < 4; i++) {
206 :     int dev;
207 :     MACROBLOCK *pMB = &pMBs[x+(i&1) + (y+(i>>1)) * pParam->mb_width];
208 :     dev = dev16(pCurrent->y + (x + (i&1) + (y + (i>>1)) * pParam->edged_width) * 16,
209 :     pParam->edged_width);
210 :    
211 :     complexity += MAX(dev, 300);
212 :     if (dev + IntraThresh < pMB->sad16) {
213 :     pMB->mode = MODE_INTRA;
214 :     if (++intra > ((pParam->mb_height-2)*(pParam->mb_width-2))/2) return I_VOP;
215 :     }
216 :    
217 :     if (pMB->mvs[0].x == 0 && pMB->mvs[0].y == 0)
218 :     if (dev > 1000 && pMB->sad16 < 1000)
219 : syskin 1239 sSAD += 512;
220 : edgomez 1142
221 : syskin 1239 sSAD += (dev < 3000) ? pMB->sad16 : pMB->sad16/2; /* blocks with big contrast differences usually have large SAD - while they look very good in b-frames */
222 : edgomez 1142 }
223 :     }
224 :     }
225 :     complexity >>= 7;
226 :    
227 :     sSAD /= complexity + 4*blocks;
228 :    
229 : syskin 1239 if (sSAD > IntraThresh2) return I_VOP;
230 :     if (sSAD > InterThresh) return P_VOP;
231 : edgomez 1142 emms();
232 :     return B_VOP;
233 :     }

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4