Parent Directory
|
Revision Log
Revision 29 -
(view)
(download)
Original Path: trunk/xvidcore/src/encoder.c
1 : | Isibaar | 3 | #include <stdlib.h> |
2 : | #include <stdio.h> | ||
3 : | #include <math.h> | ||
4 : | |||
5 : | #include "encoder.h" | ||
6 : | #include "prediction/mbprediction.h" | ||
7 : | #include "global.h" | ||
8 : | #include "utils/timer.h" | ||
9 : | #include "image/image.h" | ||
10 : | #include "bitstream/cbp.h" | ||
11 : | #include "utils/mbfunctions.h" | ||
12 : | #include "bitstream/bitstream.h" | ||
13 : | #include "bitstream/mbcoding.h" | ||
14 : | #include "utils/ratecontrol.h" | ||
15 : | #include "utils/emms.h" | ||
16 : | #include "bitstream/mbcoding.h" | ||
17 : | #include "quant/adapt_quant.h" | ||
18 : | Isibaar | 4 | #include "quant/quant_matrix.h" |
19 : | Isibaar | 3 | |
20 : | #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT | ||
21 : | |||
22 : | |||
23 : | static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits); | ||
24 : | static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header); | ||
25 : | |||
26 : | static int DQtab[4] = | ||
27 : | { | ||
28 : | -1, -2, 1, 2 | ||
29 : | }; | ||
30 : | |||
31 : | static int iDQtab[5] = | ||
32 : | { | ||
33 : | 1, 0, NO_CHANGE, 2, 3 | ||
34 : | }; | ||
35 : | |||
36 : | |||
37 : | int encoder_create(XVID_ENC_PARAM * pParam) | ||
38 : | { | ||
39 : | edgomez | 13 | Encoder *pEnc; |
40 : | Isibaar | 3 | uint32_t i; |
41 : | |||
42 : | edgomez | 13 | pParam->handle = NULL; |
43 : | Isibaar | 3 | |
44 : | edgomez | 13 | ENC_CHECK(pParam); |
45 : | Isibaar | 3 | |
46 : | edgomez | 13 | ENC_CHECK(pParam->width > 0 && pParam->width <= 1920); |
47 : | ENC_CHECK(pParam->height > 0 && pParam->height <= 1280); | ||
48 : | ENC_CHECK(!(pParam->width % 2)); | ||
49 : | ENC_CHECK(!(pParam->height % 2)); | ||
50 : | Isibaar | 3 | |
51 : | if (pParam->fincr <= 0 || pParam->fbase <= 0) | ||
52 : | { | ||
53 : | pParam->fincr = 1; | ||
54 : | pParam->fbase = 25; | ||
55 : | } | ||
56 : | |||
57 : | // simplify the "fincr/fbase" fraction | ||
58 : | // (neccessary, since windows supplies us with huge numbers) | ||
59 : | |||
60 : | i = pParam->fincr; | ||
61 : | while (i > 1) | ||
62 : | { | ||
63 : | if (pParam->fincr % i == 0 && pParam->fbase % i == 0) | ||
64 : | { | ||
65 : | pParam->fincr /= i; | ||
66 : | pParam->fbase /= i; | ||
67 : | i = pParam->fincr; | ||
68 : | continue; | ||
69 : | } | ||
70 : | i--; | ||
71 : | } | ||
72 : | |||
73 : | if (pParam->fbase > 65535) | ||
74 : | { | ||
75 : | float div = (float)pParam->fbase / 65535; | ||
76 : | pParam->fbase = (int)(pParam->fbase / div); | ||
77 : | pParam->fincr = (int)(pParam->fincr / div); | ||
78 : | } | ||
79 : | |||
80 : | if (pParam->bitrate <= 0) | ||
81 : | pParam->bitrate = 900000; | ||
82 : | |||
83 : | edgomez | 13 | if (pParam->rc_buffersize <= 0) |
84 : | Isibaar | 3 | pParam->rc_buffersize = pParam->bitrate * pParam->fbase; |
85 : | |||
86 : | edgomez | 13 | if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31)) |
87 : | Isibaar | 3 | pParam->min_quantizer = 1; |
88 : | |||
89 : | edgomez | 13 | if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31)) |
90 : | Isibaar | 3 | pParam->max_quantizer = 31; |
91 : | |||
92 : | edgomez | 13 | if (pParam->max_key_interval == 0) /* 1 keyframe each 10 seconds */ |
93 : | Isibaar | 3 | pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase; |
94 : | |||
95 : | edgomez | 13 | if (pParam->max_quantizer < pParam->min_quantizer) |
96 : | Isibaar | 3 | pParam->max_quantizer = pParam->min_quantizer; |
97 : | |||
98 : | edgomez | 13 | if ((pEnc = (Encoder *) malloc(sizeof(Encoder))) == NULL) |
99 : | Isibaar | 3 | return XVID_ERR_MEMORY; |
100 : | |||
101 : | /* Fill members of Encoder structure */ | ||
102 : | |||
103 : | edgomez | 13 | pEnc->mbParam.width = pParam->width; |
104 : | pEnc->mbParam.height = pParam->height; | ||
105 : | Isibaar | 3 | |
106 : | pEnc->mbParam.mb_width = (pEnc->mbParam.width + 15) / 16; | ||
107 : | pEnc->mbParam.mb_height = (pEnc->mbParam.height + 15) / 16; | ||
108 : | |||
109 : | pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE; | ||
110 : | pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE; | ||
111 : | |||
112 : | edgomez | 13 | pEnc->sStat.fMvPrevSigma = -1; |
113 : | Isibaar | 3 | |
114 : | /* Fill rate control parameters */ | ||
115 : | |||
116 : | edgomez | 13 | pEnc->mbParam.quant = 4; |
117 : | Isibaar | 3 | |
118 : | pEnc->bitrate = pParam->bitrate; | ||
119 : | |||
120 : | edgomez | 13 | pEnc->iFrameNum = 0; |
121 : | pEnc->iMaxKeyInterval = pParam->max_key_interval; | ||
122 : | Isibaar | 3 | |
123 : | edgomez | 13 | if (image_create(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0) |
124 : | { | ||
125 : | Isibaar | 3 | free(pEnc); |
126 : | return XVID_ERR_MEMORY; | ||
127 : | edgomez | 13 | } |
128 : | Isibaar | 3 | |
129 : | if (image_create(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0) | ||
130 : | edgomez | 13 | { |
131 : | Isibaar | 3 | image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
132 : | free(pEnc); | ||
133 : | return XVID_ERR_MEMORY; | ||
134 : | edgomez | 13 | } |
135 : | Isibaar | 3 | |
136 : | edgomez | 13 | if (image_create(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0) |
137 : | { | ||
138 : | Isibaar | 3 | image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
139 : | image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); | ||
140 : | free(pEnc); | ||
141 : | return XVID_ERR_MEMORY; | ||
142 : | edgomez | 13 | } |
143 : | Isibaar | 3 | |
144 : | edgomez | 13 | if (image_create(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0) |
145 : | { | ||
146 : | Isibaar | 3 | image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
147 : | image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); | ||
148 : | image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); | ||
149 : | free(pEnc); | ||
150 : | return XVID_ERR_MEMORY; | ||
151 : | edgomez | 13 | } |
152 : | Isibaar | 3 | |
153 : | edgomez | 13 | if (image_create(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0) |
154 : | { | ||
155 : | Isibaar | 3 | image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
156 : | image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); | ||
157 : | image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); | ||
158 : | image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); | ||
159 : | free(pEnc); | ||
160 : | return XVID_ERR_MEMORY; | ||
161 : | edgomez | 13 | } |
162 : | Isibaar | 3 | |
163 : | pEnc->pMBs = malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height); | ||
164 : | if (pEnc->pMBs == NULL) | ||
165 : | { | ||
166 : | image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); | ||
167 : | image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); | ||
168 : | image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); | ||
169 : | image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); | ||
170 : | image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); | ||
171 : | free(pEnc); | ||
172 : | return XVID_ERR_MEMORY; | ||
173 : | } | ||
174 : | |||
175 : | // init macroblock array | ||
176 : | for (i = 0; i < pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; i++) | ||
177 : | { | ||
178 : | pEnc->pMBs[i].dquant = NO_CHANGE; | ||
179 : | } | ||
180 : | |||
181 : | edgomez | 13 | pParam->handle = (void *)pEnc; |
182 : | Isibaar | 3 | |
183 : | if (pParam->bitrate) | ||
184 : | { | ||
185 : | RateControlInit(pParam->bitrate, pParam->rc_buffersize, pParam->fbase, pParam->width, | ||
186 : | edgomez | 13 | pParam->height, pParam->max_quantizer, pParam->min_quantizer); |
187 : | Isibaar | 3 | } |
188 : | |||
189 : | create_vlc_tables(); | ||
190 : | |||
191 : | return XVID_ERR_OK; | ||
192 : | } | ||
193 : | |||
194 : | |||
195 : | int encoder_destroy(Encoder * pEnc) | ||
196 : | { | ||
197 : | edgomez | 13 | ENC_CHECK(pEnc); |
198 : | ENC_CHECK(pEnc->sCurrent.y); | ||
199 : | ENC_CHECK(pEnc->sReference.y); | ||
200 : | Isibaar | 3 | |
201 : | free(pEnc->pMBs); | ||
202 : | edgomez | 13 | image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
203 : | image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); | ||
204 : | image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); | ||
205 : | image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); | ||
206 : | image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); | ||
207 : | free(pEnc); | ||
208 : | Isibaar | 3 | |
209 : | destroy_vlc_tables(); | ||
210 : | |||
211 : | edgomez | 13 | return XVID_ERR_OK; |
212 : | Isibaar | 3 | } |
213 : | |||
214 : | int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult) | ||
215 : | { | ||
216 : | edgomez | 13 | uint16_t x, y; |
217 : | Bitstream bs; | ||
218 : | uint32_t bits; | ||
219 : | Isibaar | 4 | uint16_t write_vol_header = 0; |
220 : | Isibaar | 3 | |
221 : | start_global_timer(); | ||
222 : | |||
223 : | edgomez | 13 | ENC_CHECK(pEnc); |
224 : | ENC_CHECK(pFrame); | ||
225 : | ENC_CHECK(pFrame->bitstream); | ||
226 : | ENC_CHECK(pFrame->image); | ||
227 : | Isibaar | 3 | |
228 : | pEnc->mbParam.global_flags = pFrame->general; | ||
229 : | pEnc->mbParam.motion_flags = pFrame->motion; | ||
230 : | |||
231 : | start_timer(); | ||
232 : | if (image_input(&pEnc->sCurrent, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width, | ||
233 : | edgomez | 13 | pFrame->image, pFrame->colorspace)) |
234 : | Isibaar | 3 | { |
235 : | return XVID_ERR_FORMAT; | ||
236 : | } | ||
237 : | stop_conv_timer(); | ||
238 : | |||
239 : | edgomez | 13 | BitstreamInit(&bs, pFrame->bitstream, 0); |
240 : | Isibaar | 3 | |
241 : | if (pFrame->quant == 0) | ||
242 : | { | ||
243 : | pEnc->mbParam.quant = RateControlGetQ(0); | ||
244 : | } | ||
245 : | else | ||
246 : | { | ||
247 : | pEnc->mbParam.quant = pFrame->quant; | ||
248 : | } | ||
249 : | |||
250 : | if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) | ||
251 : | { | ||
252 : | int * temp_dquants = (int *) malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int)); | ||
253 : | |||
254 : | pEnc->mbParam.quant = adaptive_quantization(pEnc->sCurrent.y, pEnc->mbParam.width, | ||
255 : | edgomez | 13 | temp_dquants, pFrame->quant, pFrame->quant, |
256 : | 2*pFrame->quant, pEnc->mbParam.mb_width, pEnc->mbParam.mb_height); | ||
257 : | Isibaar | 3 | |
258 : | for (y = 0; y < pEnc->mbParam.mb_height; y++) | ||
259 : | for (x = 0; x < pEnc->mbParam.mb_width; x++) | ||
260 : | { | ||
261 : | MACROBLOCK *pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width]; | ||
262 : | pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)]; | ||
263 : | } | ||
264 : | free(temp_dquants); | ||
265 : | } | ||
266 : | |||
267 : | Isibaar | 20 | if(pEnc->mbParam.global_flags & XVID_H263QUANT) { |
268 : | if(pEnc->mbParam.quant_type != H263_QUANT) | ||
269 : | write_vol_header = 1; | ||
270 : | pEnc->mbParam.quant_type = H263_QUANT; | ||
271 : | Isibaar | 3 | } |
272 : | Isibaar | 20 | else if(pEnc->mbParam.global_flags & XVID_MPEGQUANT) { |
273 : | int ret1, ret2; | ||
274 : | Isibaar | 3 | |
275 : | Isibaar | 20 | if(pEnc->mbParam.quant_type != MPEG4_QUANT) |
276 : | write_vol_header = 1; | ||
277 : | |||
278 : | pEnc->mbParam.quant_type = MPEG4_QUANT; | ||
279 : | |||
280 : | if ((pEnc->mbParam.global_flags & XVID_CUSTOM_QMATRIX) > 0) { | ||
281 : | if(pFrame->quant_intra_matrix != NULL) | ||
282 : | ret1 = set_intra_matrix(pFrame->quant_intra_matrix); | ||
283 : | if(pFrame->quant_inter_matrix != NULL) | ||
284 : | ret2 = set_inter_matrix(pFrame->quant_inter_matrix); | ||
285 : | } | ||
286 : | else { | ||
287 : | ret1 = set_intra_matrix(get_default_intra_matrix()); | ||
288 : | ret2 = set_inter_matrix(get_default_inter_matrix()); | ||
289 : | } | ||
290 : | Isibaar | 4 | if(write_vol_header == 0) |
291 : | write_vol_header = ret1 | ret2; | ||
292 : | } | ||
293 : | Isibaar | 3 | |
294 : | if (pFrame->intra < 0) | ||
295 : | edgomez | 13 | { |
296 : | Isibaar | 3 | if ((pEnc->iFrameNum == 0) || ((pEnc->iMaxKeyInterval > 0) |
297 : | edgomez | 13 | && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) |
298 : | Isibaar | 3 | |
299 : | pFrame->intra = FrameCodeI(pEnc, &bs, &bits); | ||
300 : | else | ||
301 : | Isibaar | 4 | pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header); |
302 : | edgomez | 13 | } |
303 : | else | ||
304 : | { | ||
305 : | Isibaar | 3 | if (pFrame->intra == 1) |
306 : | edgomez | 13 | pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
307 : | Isibaar | 3 | else |
308 : | Isibaar | 4 | pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header); |
309 : | edgomez | 13 | } |
310 : | Isibaar | 3 | |
311 : | BitstreamPutBits(&bs, 0xFFFF, 16); | ||
312 : | edgomez | 13 | BitstreamPutBits(&bs, 0xFFFF, 16); |
313 : | BitstreamPad(&bs); | ||
314 : | pFrame->length = BitstreamLength(&bs); | ||
315 : | h | 29 | |
316 : | Isibaar | 3 | if (pResult) |
317 : | edgomez | 13 | { |
318 : | Isibaar | 3 | pResult->quant = pEnc->mbParam.quant; |
319 : | pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); | ||
320 : | pResult->kblks = pEnc->sStat.kblks; | ||
321 : | pResult->mblks = pEnc->sStat.mblks; | ||
322 : | pResult->ublks = pEnc->sStat.ublks; | ||
323 : | edgomez | 13 | } |
324 : | Isibaar | 3 | |
325 : | h | 29 | if (pFrame->quant == 0) |
326 : | Isibaar | 3 | { |
327 : | RateControlUpdate(pEnc->mbParam.quant, pFrame->length, pFrame->intra); | ||
328 : | } | ||
329 : | |||
330 : | pEnc->iFrameNum++; | ||
331 : | edgomez | 13 | image_swap(&pEnc->sCurrent, &pEnc->sReference); |
332 : | Isibaar | 3 | |
333 : | stop_global_timer(); | ||
334 : | write_timer(); | ||
335 : | |||
336 : | return XVID_ERR_OK; | ||
337 : | } | ||
338 : | |||
339 : | |||
340 : | static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) { | ||
341 : | |||
342 : | pMB->mode = MODE_INTRA; | ||
343 : | |||
344 : | if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) { | ||
345 : | if(pMB->dquant != NO_CHANGE) | ||
346 : | { | ||
347 : | pMB->mode = MODE_INTRA_Q; | ||
348 : | pEnc->mbParam.quant += DQtab[pMB->dquant]; | ||
349 : | |||
350 : | if (pEnc->mbParam.quant > 31) pEnc->mbParam.quant = 31; | ||
351 : | if (pEnc->mbParam.quant < 1) pEnc->mbParam.quant = 1; | ||
352 : | } | ||
353 : | } | ||
354 : | |||
355 : | pMB->quant = pEnc->mbParam.quant; | ||
356 : | } | ||
357 : | |||
358 : | |||
359 : | static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits) | ||
360 : | { | ||
361 : | edgomez | 13 | int16_t dct_codes[6][64]; |
362 : | int16_t qcoeff[6][64]; | ||
363 : | uint16_t x, y; | ||
364 : | Isibaar | 3 | |
365 : | edgomez | 13 | pEnc->iFrameNum = 0; |
366 : | pEnc->mbParam.rounding_type = 1; | ||
367 : | pEnc->mbParam.coding_type = I_VOP; | ||
368 : | Isibaar | 3 | |
369 : | BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type); | ||
370 : | BitstreamWriteVopHeader(bs, I_VOP, pEnc->mbParam.rounding_type, | ||
371 : | edgomez | 13 | pEnc->mbParam.quant, |
372 : | pEnc->mbParam.fixed_code); | ||
373 : | Isibaar | 3 | |
374 : | edgomez | 13 | *pBits = BitstreamPos(bs); |
375 : | Isibaar | 3 | |
376 : | pEnc->sStat.iTextBits = 0; | ||
377 : | pEnc->sStat.kblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; | ||
378 : | pEnc->sStat.mblks = pEnc->sStat.ublks = 0; | ||
379 : | |||
380 : | edgomez | 13 | for (y = 0; y < pEnc->mbParam.mb_height; y++) |
381 : | Isibaar | 3 | for (x = 0; x < pEnc->mbParam.mb_width; x++) |
382 : | { | ||
383 : | edgomez | 13 | MACROBLOCK *pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width]; |
384 : | Isibaar | 3 | |
385 : | CodeIntraMB(pEnc, pMB); | ||
386 : | |||
387 : | edgomez | 13 | MBTransQuantIntra(&pEnc->mbParam, x, y, dct_codes, qcoeff, &pEnc->sCurrent); |
388 : | Isibaar | 3 | |
389 : | start_timer(); | ||
390 : | MBPrediction(&pEnc->mbParam, x, y, pEnc->mbParam.mb_width, qcoeff, pEnc->pMBs); | ||
391 : | stop_prediction_timer(); | ||
392 : | |||
393 : | start_timer(); | ||
394 : | MBCoding(&pEnc->mbParam, pMB, qcoeff, bs, &pEnc->sStat); | ||
395 : | stop_coding_timer(); | ||
396 : | } | ||
397 : | |||
398 : | emms(); | ||
399 : | |||
400 : | edgomez | 13 | *pBits = BitstreamPos(bs) - *pBits; |
401 : | pEnc->sStat.fMvPrevSigma = -1; | ||
402 : | pEnc->sStat.iMvSum = 0; | ||
403 : | pEnc->sStat.iMvCount = 0; | ||
404 : | pEnc->mbParam.fixed_code = 2; | ||
405 : | Isibaar | 3 | |
406 : | edgomez | 13 | return 1; // intra |
407 : | Isibaar | 3 | } |
408 : | |||
409 : | |||
410 : | #define INTRA_THRESHOLD 0.5 | ||
411 : | |||
412 : | static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header) | ||
413 : | { | ||
414 : | edgomez | 13 | float fSigma; |
415 : | int16_t dct_codes[6][64]; | ||
416 : | int16_t qcoeff[6][64]; | ||
417 : | Isibaar | 3 | int iLimit; |
418 : | edgomez | 13 | uint32_t x, y; |
419 : | int iSearchRange; | ||
420 : | Isibaar | 3 | bool bIntra; |
421 : | |||
422 : | edgomez | 13 | IMAGE *pCurrent = &pEnc->sCurrent; |
423 : | IMAGE *pRef = &pEnc->sReference; | ||
424 : | Isibaar | 3 | |
425 : | image_setedges(pRef,pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height); | ||
426 : | |||
427 : | edgomez | 13 | pEnc->mbParam.rounding_type = 1 - pEnc->mbParam.rounding_type; |
428 : | Isibaar | 3 | |
429 : | if (!force_inter) | ||
430 : | iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD); | ||
431 : | edgomez | 13 | else |
432 : | Isibaar | 3 | iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1; |
433 : | |||
434 : | if ((pEnc->mbParam.global_flags & XVID_HALFPEL) > 0) { | ||
435 : | start_timer(); | ||
436 : | image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, | ||
437 : | edgomez | 13 | pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
438 : | pEnc->mbParam.rounding_type); | ||
439 : | Isibaar | 3 | stop_inter_timer(); |
440 : | } | ||
441 : | |||
442 : | start_timer(); | ||
443 : | bIntra = MotionEstimation(pEnc->pMBs, &pEnc->mbParam, &pEnc->sReference, | ||
444 : | edgomez | 13 | &pEnc->vInterH, &pEnc->vInterV, |
445 : | &pEnc->vInterHV, &pEnc->sCurrent, iLimit); | ||
446 : | Isibaar | 3 | stop_motion_timer(); |
447 : | |||
448 : | if (bIntra == 1) | ||
449 : | return FrameCodeI(pEnc, bs, pBits); | ||
450 : | |||
451 : | edgomez | 13 | pEnc->mbParam.coding_type = P_VOP; |
452 : | Isibaar | 3 | |
453 : | if(vol_header) | ||
454 : | BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type); | ||
455 : | |||
456 : | edgomez | 13 | BitstreamWriteVopHeader(bs, P_VOP, pEnc->mbParam.rounding_type, |
457 : | pEnc->mbParam.quant, | ||
458 : | pEnc->mbParam.fixed_code); | ||
459 : | Isibaar | 3 | |
460 : | edgomez | 13 | *pBits = BitstreamPos(bs); |
461 : | Isibaar | 3 | |
462 : | edgomez | 13 | pEnc->sStat.iTextBits = 0; |
463 : | pEnc->sStat.iMvSum = 0; | ||
464 : | pEnc->sStat.iMvCount = 0; | ||
465 : | Isibaar | 3 | pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
466 : | |||
467 : | edgomez | 13 | for(y = 0; y < pEnc->mbParam.mb_height; y++) |
468 : | Isibaar | 3 | { |
469 : | for(x = 0; x < pEnc->mbParam.mb_width; x++) | ||
470 : | { | ||
471 : | MACROBLOCK * pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width]; | ||
472 : | |||
473 : | edgomez | 13 | bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
474 : | Isibaar | 3 | |
475 : | if (!bIntra) | ||
476 : | edgomez | 13 | { |
477 : | Isibaar | 3 | start_timer(); |
478 : | MBMotionCompensation(pMB, x, y, &pEnc->sReference, | ||
479 : | edgomez | 13 | &pEnc->vInterH, &pEnc->vInterV, |
480 : | &pEnc->vInterHV, &pEnc->sCurrent, dct_codes, | ||
481 : | pEnc->mbParam.width, | ||
482 : | pEnc->mbParam.height, | ||
483 : | pEnc->mbParam.edged_width, | ||
484 : | pEnc->mbParam.rounding_type); | ||
485 : | Isibaar | 3 | stop_comp_timer(); |
486 : | |||
487 : | if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) { | ||
488 : | if(pMB->dquant != NO_CHANGE) { | ||
489 : | pMB->mode = MODE_INTER_Q; | ||
490 : | pEnc->mbParam.quant += DQtab[pMB->dquant]; | ||
491 : | if (pEnc->mbParam.quant > 31) pEnc->mbParam.quant = 31; | ||
492 : | else if(pEnc->mbParam.quant < 1) pEnc->mbParam.quant = 1; | ||
493 : | } | ||
494 : | } | ||
495 : | pMB->quant = pEnc->mbParam.quant; | ||
496 : | |||
497 : | pMB->cbp = MBTransQuantInter(&pEnc->mbParam, x, y, dct_codes, qcoeff, pCurrent); | ||
498 : | edgomez | 13 | } |
499 : | Isibaar | 3 | else |
500 : | { | ||
501 : | CodeIntraMB(pEnc, pMB); | ||
502 : | MBTransQuantIntra(&pEnc->mbParam, x, y, dct_codes, qcoeff, pCurrent); | ||
503 : | } | ||
504 : | |||
505 : | edgomez | 13 | start_timer(); |
506 : | Isibaar | 3 | MBPrediction(&pEnc->mbParam, x, y, pEnc->mbParam.mb_width, qcoeff, pEnc->pMBs); |
507 : | stop_prediction_timer(); | ||
508 : | |||
509 : | if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) | ||
510 : | { | ||
511 : | pEnc->sStat.kblks++; | ||
512 : | } | ||
513 : | else if (pMB->cbp || | ||
514 : | edgomez | 13 | pMB->mvs[0].x || pMB->mvs[0].y || |
515 : | pMB->mvs[1].x || pMB->mvs[1].y || | ||
516 : | pMB->mvs[2].x || pMB->mvs[2].y || | ||
517 : | pMB->mvs[3].x || pMB->mvs[3].y) | ||
518 : | Isibaar | 3 | { |
519 : | pEnc->sStat.mblks++; | ||
520 : | } | ||
521 : | else | ||
522 : | { | ||
523 : | pEnc->sStat.ublks++; | ||
524 : | } | ||
525 : | |||
526 : | start_timer(); | ||
527 : | MBCoding(&pEnc->mbParam, pMB, qcoeff, bs, &pEnc->sStat); | ||
528 : | stop_coding_timer(); | ||
529 : | } | ||
530 : | } | ||
531 : | |||
532 : | emms(); | ||
533 : | |||
534 : | if (pEnc->sStat.iMvCount == 0) | ||
535 : | pEnc->sStat.iMvCount = 1; | ||
536 : | |||
537 : | edgomez | 13 | fSigma = (float)sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount); |
538 : | Isibaar | 3 | |
539 : | edgomez | 13 | iSearchRange = 1 << (3 + pEnc->mbParam.fixed_code); |
540 : | Isibaar | 3 | |
541 : | edgomez | 13 | if ((fSigma > iSearchRange / 3) |
542 : | && (pEnc->mbParam.fixed_code <= 3)) // maximum search range 128 | ||
543 : | { | ||
544 : | Isibaar | 3 | pEnc->mbParam.fixed_code++; |
545 : | iSearchRange *= 2; | ||
546 : | edgomez | 13 | } |
547 : | else if ((fSigma < iSearchRange / 6) | ||
548 : | && (pEnc->sStat.fMvPrevSigma >= 0) | ||
549 : | && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) | ||
550 : | && (pEnc->mbParam.fixed_code >= 2)) // minimum search range 16 | ||
551 : | { | ||
552 : | Isibaar | 3 | pEnc->mbParam.fixed_code--; |
553 : | iSearchRange /= 2; | ||
554 : | edgomez | 13 | } |
555 : | Isibaar | 3 | |
556 : | edgomez | 13 | pEnc->sStat.fMvPrevSigma = fSigma; |
557 : | Isibaar | 3 | |
558 : | *pBits = BitstreamPos(bs) - *pBits; | ||
559 : | |||
560 : | edgomez | 13 | return 0; // inter |
561 : | Isibaar | 3 | } |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |