Parent Directory
|
Revision Log
Revision 138 - (view) (download)
1 : | suxen_drol | 118 | // 14.04.2002 added FrameCodeB() |
2 : | |||
3 : | Isibaar | 3 | #include <stdlib.h> |
4 : | #include <stdio.h> | ||
5 : | #include <math.h> | ||
6 : | |||
7 : | #include "encoder.h" | ||
8 : | #include "prediction/mbprediction.h" | ||
9 : | #include "global.h" | ||
10 : | #include "utils/timer.h" | ||
11 : | #include "image/image.h" | ||
12 : | #include "bitstream/cbp.h" | ||
13 : | #include "utils/mbfunctions.h" | ||
14 : | #include "bitstream/bitstream.h" | ||
15 : | #include "bitstream/mbcoding.h" | ||
16 : | #include "utils/ratecontrol.h" | ||
17 : | #include "utils/emms.h" | ||
18 : | #include "bitstream/mbcoding.h" | ||
19 : | #include "quant/adapt_quant.h" | ||
20 : | Isibaar | 4 | #include "quant/quant_matrix.h" |
21 : | Isibaar | 41 | #include "utils/mem_align.h" |
22 : | Isibaar | 3 | |
23 : | #define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT | ||
24 : | suxen_drol | 136 | #define SWAP(A,B) { void * tmp = A; A = B; B = tmp; } |
25 : | Isibaar | 3 | |
26 : | |||
27 : | static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits); | ||
28 : | static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header); | ||
29 : | |||
30 : | static int DQtab[4] = | ||
31 : | { | ||
32 : | -1, -2, 1, 2 | ||
33 : | }; | ||
34 : | |||
35 : | static int iDQtab[5] = | ||
36 : | { | ||
37 : | 1, 0, NO_CHANGE, 2, 3 | ||
38 : | }; | ||
39 : | |||
40 : | |||
41 : | suxen_drol | 136 | void static image_null(IMAGE * image) |
42 : | { | ||
43 : | image->y = image->u = image->v = NULL; | ||
44 : | } | ||
45 : | |||
46 : | |||
47 : | Isibaar | 3 | int encoder_create(XVID_ENC_PARAM * pParam) |
48 : | { | ||
49 : | edgomez | 13 | Encoder *pEnc; |
50 : | Isibaar | 3 | uint32_t i; |
51 : | |||
52 : | edgomez | 13 | pParam->handle = NULL; |
53 : | Isibaar | 3 | |
54 : | edgomez | 13 | ENC_CHECK(pParam); |
55 : | Isibaar | 3 | |
56 : | edgomez | 13 | ENC_CHECK(pParam->width > 0 && pParam->width <= 1920); |
57 : | ENC_CHECK(pParam->height > 0 && pParam->height <= 1280); | ||
58 : | ENC_CHECK(!(pParam->width % 2)); | ||
59 : | ENC_CHECK(!(pParam->height % 2)); | ||
60 : | Isibaar | 3 | |
61 : | if (pParam->fincr <= 0 || pParam->fbase <= 0) | ||
62 : | { | ||
63 : | pParam->fincr = 1; | ||
64 : | pParam->fbase = 25; | ||
65 : | } | ||
66 : | |||
67 : | // simplify the "fincr/fbase" fraction | ||
68 : | // (neccessary, since windows supplies us with huge numbers) | ||
69 : | |||
70 : | i = pParam->fincr; | ||
71 : | while (i > 1) | ||
72 : | { | ||
73 : | if (pParam->fincr % i == 0 && pParam->fbase % i == 0) | ||
74 : | { | ||
75 : | pParam->fincr /= i; | ||
76 : | pParam->fbase /= i; | ||
77 : | i = pParam->fincr; | ||
78 : | continue; | ||
79 : | } | ||
80 : | i--; | ||
81 : | } | ||
82 : | |||
83 : | if (pParam->fbase > 65535) | ||
84 : | { | ||
85 : | float div = (float)pParam->fbase / 65535; | ||
86 : | pParam->fbase = (int)(pParam->fbase / div); | ||
87 : | pParam->fincr = (int)(pParam->fincr / div); | ||
88 : | } | ||
89 : | |||
90 : | h | 121 | if (pParam->rc_bitrate <= 0) |
91 : | pParam->rc_bitrate = 900000; | ||
92 : | Isibaar | 3 | |
93 : | h | 121 | if (pParam->rc_reaction_delay_factor <= 0) |
94 : | pParam->rc_reaction_delay_factor = 16; | ||
95 : | Isibaar | 3 | |
96 : | h | 121 | if (pParam->rc_averaging_period <= 0) |
97 : | pParam->rc_averaging_period = 100; | ||
98 : | |||
99 : | if (pParam->rc_buffer <= 0) | ||
100 : | pParam->rc_buffer = 100; | ||
101 : | |||
102 : | edgomez | 13 | if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31)) |
103 : | Isibaar | 3 | pParam->min_quantizer = 1; |
104 : | |||
105 : | edgomez | 13 | if ((pParam->max_quantizer <= 0) || (pParam->max_quantizer > 31)) |
106 : | Isibaar | 3 | pParam->max_quantizer = 31; |
107 : | |||
108 : | edgomez | 13 | if (pParam->max_key_interval == 0) /* 1 keyframe each 10 seconds */ |
109 : | Isibaar | 3 | pParam->max_key_interval = 10 * pParam->fincr / pParam->fbase; |
110 : | |||
111 : | edgomez | 13 | if (pParam->max_quantizer < pParam->min_quantizer) |
112 : | Isibaar | 3 | pParam->max_quantizer = pParam->min_quantizer; |
113 : | |||
114 : | canard | 66 | if ((pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE)) == NULL) |
115 : | Isibaar | 3 | return XVID_ERR_MEMORY; |
116 : | |||
117 : | /* Fill members of Encoder structure */ | ||
118 : | |||
119 : | edgomez | 13 | pEnc->mbParam.width = pParam->width; |
120 : | pEnc->mbParam.height = pParam->height; | ||
121 : | Isibaar | 3 | |
122 : | pEnc->mbParam.mb_width = (pEnc->mbParam.width + 15) / 16; | ||
123 : | pEnc->mbParam.mb_height = (pEnc->mbParam.height + 15) / 16; | ||
124 : | |||
125 : | pEnc->mbParam.edged_width = 16 * pEnc->mbParam.mb_width + 2 * EDGE_SIZE; | ||
126 : | pEnc->mbParam.edged_height = 16 * pEnc->mbParam.mb_height + 2 * EDGE_SIZE; | ||
127 : | |||
128 : | edgomez | 13 | pEnc->sStat.fMvPrevSigma = -1; |
129 : | Isibaar | 3 | |
130 : | /* Fill rate control parameters */ | ||
131 : | |||
132 : | h | 121 | pEnc->bitrate = pParam->rc_bitrate; |
133 : | Isibaar | 3 | |
134 : | edgomez | 13 | pEnc->iFrameNum = 0; |
135 : | pEnc->iMaxKeyInterval = pParam->max_key_interval; | ||
136 : | Isibaar | 3 | |
137 : | suxen_drol | 136 | /* try to allocate frame memory */ |
138 : | Isibaar | 3 | |
139 : | suxen_drol | 136 | pEnc->current = NULL; |
140 : | pEnc->reference = NULL; | ||
141 : | if ( (pEnc->current = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE)) == NULL || | ||
142 : | (pEnc->reference = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE)) == NULL) | ||
143 : | { | ||
144 : | if (pEnc->current) xvid_free(pEnc->current); | ||
145 : | xvid_free(pEnc); | ||
146 : | return XVID_ERR_MEMORY; | ||
147 : | } | ||
148 : | Isibaar | 3 | |
149 : | suxen_drol | 136 | /* try to allocate mb memory */ |
150 : | Isibaar | 3 | |
151 : | suxen_drol | 136 | pEnc->current->mbs = NULL; |
152 : | pEnc->reference->mbs = NULL; | ||
153 : | |||
154 : | chl | 138 | #ifdef _DEBUG |
155 : | #ifdef WIN32 | ||
156 : | suxen_drol | 136 | OutputDebugString("malloc mbs"); |
157 : | chl | 138 | #endif |
158 : | #endif | ||
159 : | |||
160 : | suxen_drol | 136 | if ((pEnc->current->mbs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL || |
161 : | (pEnc->reference->mbs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL) | ||
162 : | { | ||
163 : | if (pEnc->current->mbs) xvid_free(pEnc->current->mbs); | ||
164 : | xvid_free(pEnc->current); | ||
165 : | xvid_free(pEnc->reference); | ||
166 : | xvid_free(pEnc); | ||
167 : | } | ||
168 : | |||
169 : | /* try to allocate image memory */ | ||
170 : | |||
171 : | #ifdef _DEBUG | ||
172 : | image_null(&pEnc->sOriginal); | ||
173 : | #endif | ||
174 : | image_null(&pEnc->current->image); | ||
175 : | image_null(&pEnc->reference->image); | ||
176 : | image_null(&pEnc->vInterH); | ||
177 : | image_null(&pEnc->vInterV); | ||
178 : | image_null(&pEnc->vInterVf); | ||
179 : | image_null(&pEnc->vInterHV); | ||
180 : | image_null(&pEnc->vInterHVf); | ||
181 : | |||
182 : | chl | 138 | #ifdef _DEBUG |
183 : | #ifdef WIN32 | ||
184 : | suxen_drol | 136 | OutputDebugString("malloc images"); |
185 : | chl | 138 | #endif |
186 : | #endif | ||
187 : | suxen_drol | 136 | if ( |
188 : | #ifdef _DEBUG | ||
189 : | image_create(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || | ||
190 : | #endif | ||
191 : | image_create(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || | ||
192 : | image_create(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || | ||
193 : | h | 86 | image_create(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
194 : | image_create(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || | ||
195 : | image_create(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || | ||
196 : | image_create(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || | ||
197 : | suxen_drol | 136 | image_create(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0) |
198 : | { | ||
199 : | Isibaar | 113 | #ifdef _DEBUG |
200 : | suxen_drol | 136 | image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
201 : | Isibaar | 113 | #endif |
202 : | suxen_drol | 136 | image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
203 : | image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); | ||
204 : | Isibaar | 3 | 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 : | h | 86 | image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
207 : | Isibaar | 3 | image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
208 : | h | 86 | image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
209 : | suxen_drol | 136 | |
210 : | xvid_free(pEnc->current); | ||
211 : | xvid_free(pEnc->reference); | ||
212 : | xvid_free(pEnc); | ||
213 : | Isibaar | 3 | return XVID_ERR_MEMORY; |
214 : | } | ||
215 : | |||
216 : | edgomez | 13 | pParam->handle = (void *)pEnc; |
217 : | Isibaar | 3 | |
218 : | h | 121 | if (pParam->rc_bitrate) |
219 : | Isibaar | 3 | { |
220 : | h | 121 | RateControlInit(pParam->rc_bitrate, pParam->rc_reaction_delay_factor, |
221 : | pParam->rc_averaging_period, pParam->rc_buffer, pParam->fbase * 1000 / pParam->fincr, | ||
222 : | pParam->max_quantizer, pParam->min_quantizer); | ||
223 : | Isibaar | 3 | } |
224 : | |||
225 : | Isibaar | 36 | init_timer(); |
226 : | Isibaar | 3 | |
227 : | return XVID_ERR_OK; | ||
228 : | } | ||
229 : | |||
230 : | |||
231 : | int encoder_destroy(Encoder * pEnc) | ||
232 : | { | ||
233 : | edgomez | 13 | ENC_CHECK(pEnc); |
234 : | Isibaar | 3 | |
235 : | suxen_drol | 136 | image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
236 : | image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); | ||
237 : | edgomez | 13 | image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
238 : | image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); | ||
239 : | h | 87 | image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
240 : | edgomez | 13 | image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
241 : | h | 87 | image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
242 : | Isibaar | 113 | #ifdef _DEBUG |
243 : | image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); | ||
244 : | #endif | ||
245 : | suxen_drol | 136 | xvid_free(pEnc->current->mbs); |
246 : | xvid_free(pEnc->current); | ||
247 : | |||
248 : | xvid_free(pEnc->reference->mbs); | ||
249 : | xvid_free(pEnc->reference); | ||
250 : | |||
251 : | Isibaar | 41 | xvid_free(pEnc); |
252 : | edgomez | 13 | return XVID_ERR_OK; |
253 : | Isibaar | 3 | } |
254 : | |||
255 : | int encoder_encode(Encoder * pEnc, XVID_ENC_FRAME * pFrame, XVID_ENC_STATS * pResult) | ||
256 : | { | ||
257 : | edgomez | 13 | uint16_t x, y; |
258 : | Bitstream bs; | ||
259 : | uint32_t bits; | ||
260 : | Isibaar | 4 | uint16_t write_vol_header = 0; |
261 : | Isibaar | 113 | #ifdef _DEBUG |
262 : | float psnr; | ||
263 : | uint8_t temp[100]; | ||
264 : | #endif | ||
265 : | Isibaar | 3 | |
266 : | start_global_timer(); | ||
267 : | |||
268 : | edgomez | 13 | ENC_CHECK(pEnc); |
269 : | ENC_CHECK(pFrame); | ||
270 : | ENC_CHECK(pFrame->bitstream); | ||
271 : | ENC_CHECK(pFrame->image); | ||
272 : | Isibaar | 3 | |
273 : | suxen_drol | 136 | SWAP(pEnc->current, pEnc->reference); |
274 : | |||
275 : | pEnc->current->global_flags = pFrame->general; | ||
276 : | pEnc->current->motion_flags = pFrame->motion; | ||
277 : | h | 101 | pEnc->mbParam.hint = &pFrame->hint; |
278 : | Isibaar | 3 | |
279 : | start_timer(); | ||
280 : | suxen_drol | 136 | if (image_input(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width, |
281 : | edgomez | 13 | pFrame->image, pFrame->colorspace)) |
282 : | Isibaar | 3 | { |
283 : | return XVID_ERR_FORMAT; | ||
284 : | } | ||
285 : | stop_conv_timer(); | ||
286 : | |||
287 : | Isibaar | 113 | #ifdef _DEBUG |
288 : | image_copy(&pEnc->sOriginal, &pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.height); | ||
289 : | #endif | ||
290 : | |||
291 : | suxen_drol | 136 | EMMS(); |
292 : | |||
293 : | edgomez | 13 | BitstreamInit(&bs, pFrame->bitstream, 0); |
294 : | Isibaar | 3 | |
295 : | if (pFrame->quant == 0) | ||
296 : | { | ||
297 : | suxen_drol | 136 | pEnc->current->quant = RateControlGetQ(0); |
298 : | Isibaar | 3 | } |
299 : | else | ||
300 : | { | ||
301 : | suxen_drol | 136 | pEnc->current->quant = pFrame->quant; |
302 : | Isibaar | 3 | } |
303 : | |||
304 : | suxen_drol | 136 | if ((pEnc->current->global_flags & XVID_LUMIMASKING)) |
305 : | Isibaar | 3 | { |
306 : | canard | 66 | int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); |
307 : | Isibaar | 3 | |
308 : | suxen_drol | 136 | pEnc->current->quant = adaptive_quantization(pEnc->current->image.y, |
309 : | pEnc->mbParam.edged_width, // stride | ||
310 : | edgomez | 78 | temp_dquants, |
311 : | suxen_drol | 136 | pEnc->current->quant, |
312 : | pEnc->current->quant, // min_quant | ||
313 : | 2*pEnc->current->quant, // max_quant | ||
314 : | edgomez | 78 | pEnc->mbParam.mb_width, |
315 : | pEnc->mbParam.mb_height); | ||
316 : | Isibaar | 3 | |
317 : | for (y = 0; y < pEnc->mbParam.mb_height; y++) | ||
318 : | for (x = 0; x < pEnc->mbParam.mb_width; x++) | ||
319 : | { | ||
320 : | suxen_drol | 136 | MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
321 : | Isibaar | 3 | pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)]; |
322 : | } | ||
323 : | Isibaar | 41 | xvid_free(temp_dquants); |
324 : | Isibaar | 3 | } |
325 : | |||
326 : | suxen_drol | 136 | if (pEnc->current->global_flags & XVID_H263QUANT) { |
327 : | if(pEnc->mbParam.m_quant_type != H263_QUANT) | ||
328 : | Isibaar | 20 | write_vol_header = 1; |
329 : | suxen_drol | 136 | pEnc->mbParam.m_quant_type = H263_QUANT; |
330 : | Isibaar | 3 | } |
331 : | suxen_drol | 136 | else if(pEnc->current->global_flags & XVID_MPEGQUANT) { |
332 : | Isibaar | 20 | int ret1, ret2; |
333 : | Isibaar | 3 | |
334 : | edgomez | 78 | ret1 = ret2 = 0; |
335 : | |||
336 : | suxen_drol | 136 | if(pEnc->mbParam.m_quant_type != MPEG4_QUANT) |
337 : | Isibaar | 20 | write_vol_header = 1; |
338 : | |||
339 : | suxen_drol | 136 | pEnc->mbParam.m_quant_type = MPEG4_QUANT; |
340 : | Isibaar | 20 | |
341 : | suxen_drol | 136 | if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) { |
342 : | Isibaar | 20 | if(pFrame->quant_intra_matrix != NULL) |
343 : | ret1 = set_intra_matrix(pFrame->quant_intra_matrix); | ||
344 : | if(pFrame->quant_inter_matrix != NULL) | ||
345 : | ret2 = set_inter_matrix(pFrame->quant_inter_matrix); | ||
346 : | } | ||
347 : | else { | ||
348 : | ret1 = set_intra_matrix(get_default_intra_matrix()); | ||
349 : | ret2 = set_inter_matrix(get_default_inter_matrix()); | ||
350 : | } | ||
351 : | Isibaar | 4 | if(write_vol_header == 0) |
352 : | write_vol_header = ret1 | ret2; | ||
353 : | } | ||
354 : | Isibaar | 3 | |
355 : | if (pFrame->intra < 0) | ||
356 : | edgomez | 13 | { |
357 : | Isibaar | 3 | if ((pEnc->iFrameNum == 0) || ((pEnc->iMaxKeyInterval > 0) |
358 : | edgomez | 13 | && (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) |
359 : | Isibaar | 3 | |
360 : | pFrame->intra = FrameCodeI(pEnc, &bs, &bits); | ||
361 : | else | ||
362 : | Isibaar | 4 | pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header); |
363 : | edgomez | 13 | } |
364 : | else | ||
365 : | { | ||
366 : | Isibaar | 3 | if (pFrame->intra == 1) |
367 : | edgomez | 13 | pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
368 : | Isibaar | 3 | else |
369 : | Isibaar | 4 | pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header); |
370 : | edgomez | 13 | } |
371 : | Isibaar | 3 | |
372 : | BitstreamPutBits(&bs, 0xFFFF, 16); | ||
373 : | edgomez | 13 | BitstreamPutBits(&bs, 0xFFFF, 16); |
374 : | BitstreamPad(&bs); | ||
375 : | pFrame->length = BitstreamLength(&bs); | ||
376 : | h | 29 | |
377 : | Isibaar | 3 | if (pResult) |
378 : | edgomez | 13 | { |
379 : | suxen_drol | 136 | pResult->quant = pEnc->current->quant; |
380 : | Isibaar | 3 | pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
381 : | pResult->kblks = pEnc->sStat.kblks; | ||
382 : | pResult->mblks = pEnc->sStat.mblks; | ||
383 : | pResult->ublks = pEnc->sStat.ublks; | ||
384 : | edgomez | 13 | } |
385 : | Isibaar | 3 | |
386 : | Isibaar | 41 | EMMS(); |
387 : | |||
388 : | h | 29 | if (pFrame->quant == 0) |
389 : | Isibaar | 3 | { |
390 : | suxen_drol | 136 | RateControlUpdate(pEnc->current->quant, pFrame->length, pFrame->intra); |
391 : | Isibaar | 3 | } |
392 : | |||
393 : | Isibaar | 113 | #ifdef _DEBUG |
394 : | suxen_drol | 136 | psnr = image_psnr(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, |
395 : | Isibaar | 113 | pEnc->mbParam.width, pEnc->mbParam.height); |
396 : | |||
397 : | sprintf(temp, "PSNR: %f\n", psnr); | ||
398 : | DEBUG(temp); | ||
399 : | #endif | ||
400 : | |||
401 : | Isibaar | 3 | pEnc->iFrameNum++; |
402 : | |||
403 : | stop_global_timer(); | ||
404 : | write_timer(); | ||
405 : | |||
406 : | return XVID_ERR_OK; | ||
407 : | } | ||
408 : | |||
409 : | |||
410 : | static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) { | ||
411 : | |||
412 : | pMB->mode = MODE_INTRA; | ||
413 : | |||
414 : | suxen_drol | 136 | /* zero mv statistics */ |
415 : | pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0; | ||
416 : | pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0; | ||
417 : | pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0; | ||
418 : | pMB->sad16 = 0; | ||
419 : | |||
420 : | if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { | ||
421 : | Isibaar | 3 | if(pMB->dquant != NO_CHANGE) |
422 : | { | ||
423 : | pMB->mode = MODE_INTRA_Q; | ||
424 : | suxen_drol | 136 | pEnc->current->quant += DQtab[pMB->dquant]; |
425 : | Isibaar | 3 | |
426 : | suxen_drol | 136 | if (pEnc->current->quant > 31) pEnc->current->quant = 31; |
427 : | if (pEnc->current->quant < 1) pEnc->current->quant = 1; | ||
428 : | Isibaar | 3 | } |
429 : | } | ||
430 : | |||
431 : | suxen_drol | 136 | pMB->quant = pEnc->current->quant; |
432 : | Isibaar | 3 | } |
433 : | |||
434 : | |||
435 : | h | 101 | #define FCODEBITS 3 |
436 : | #define MODEBITS 5 | ||
437 : | |||
438 : | void HintedMESet(Encoder * pEnc, int * intra) | ||
439 : | { | ||
440 : | HINTINFO * hint; | ||
441 : | Bitstream bs; | ||
442 : | int length, high; | ||
443 : | uint32_t x, y; | ||
444 : | |||
445 : | hint = pEnc->mbParam.hint; | ||
446 : | |||
447 : | if (hint->rawhints) | ||
448 : | { | ||
449 : | *intra = hint->mvhint.intra; | ||
450 : | } | ||
451 : | else | ||
452 : | { | ||
453 : | BitstreamInit(&bs, hint->hintstream, hint->hintlength); | ||
454 : | *intra = BitstreamGetBit(&bs); | ||
455 : | } | ||
456 : | |||
457 : | if (*intra) | ||
458 : | { | ||
459 : | return; | ||
460 : | } | ||
461 : | |||
462 : | suxen_drol | 136 | pEnc->current->fcode = (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS); |
463 : | h | 101 | |
464 : | suxen_drol | 136 | length = pEnc->current->fcode + 5; |
465 : | h | 101 | high = 1 << (length - 1); |
466 : | |||
467 : | for (y=0 ; y<pEnc->mbParam.mb_height ; ++y) | ||
468 : | { | ||
469 : | for (x=0 ; x<pEnc->mbParam.mb_width ; ++x) | ||
470 : | { | ||
471 : | suxen_drol | 136 | MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
472 : | h | 101 | MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
473 : | VECTOR pred[4]; | ||
474 : | VECTOR tmp; | ||
475 : | int dummy[4]; | ||
476 : | int vec; | ||
477 : | |||
478 : | pMB->mode = (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, MODEBITS); | ||
479 : | |||
480 : | h | 128 | pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode; |
481 : | pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode; | ||
482 : | |||
483 : | if (pMB->mode == MODE_INTER) | ||
484 : | h | 101 | { |
485 : | tmp.x = (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, length); | ||
486 : | tmp.y = (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, length); | ||
487 : | tmp.x -= (tmp.x >= high) ? high*2 : 0; | ||
488 : | tmp.y -= (tmp.y >= high) ? high*2 : 0; | ||
489 : | |||
490 : | suxen_drol | 136 | get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, 0, pred, dummy); |
491 : | h | 101 | |
492 : | for (vec=0 ; vec<4 ; ++vec) | ||
493 : | { | ||
494 : | pMB->mvs[vec].x = tmp.x; | ||
495 : | pMB->mvs[vec].y = tmp.y; | ||
496 : | pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x; | ||
497 : | pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y; | ||
498 : | } | ||
499 : | } | ||
500 : | else if (pMB->mode == MODE_INTER4V) | ||
501 : | { | ||
502 : | for (vec=0 ; vec<4 ; ++vec) | ||
503 : | { | ||
504 : | tmp.x = (hint->rawhints) ? bhint->mvs[vec].x : BitstreamGetBits(&bs, length); | ||
505 : | tmp.y = (hint->rawhints) ? bhint->mvs[vec].y : BitstreamGetBits(&bs, length); | ||
506 : | tmp.x -= (tmp.x >= high) ? high*2 : 0; | ||
507 : | tmp.y -= (tmp.y >= high) ? high*2 : 0; | ||
508 : | |||
509 : | suxen_drol | 136 | get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, vec, pred, dummy); |
510 : | h | 101 | |
511 : | pMB->mvs[vec].x = tmp.x; | ||
512 : | pMB->mvs[vec].y = tmp.y; | ||
513 : | pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x; | ||
514 : | pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y; | ||
515 : | } | ||
516 : | } | ||
517 : | h | 128 | else // intra / stuffing / not_coded |
518 : | h | 101 | { |
519 : | for (vec=0 ; vec<4 ; ++vec) | ||
520 : | { | ||
521 : | pMB->mvs[vec].x = pMB->mvs[vec].y = 0; | ||
522 : | } | ||
523 : | } | ||
524 : | h | 128 | |
525 : | suxen_drol | 136 | if (pMB->mode == MODE_INTER4V && |
526 : | (pEnc->current->global_flags & XVID_LUMIMASKING) && pMB->dquant != NO_CHANGE) | ||
527 : | h | 128 | { |
528 : | pMB->mode = MODE_INTRA; | ||
529 : | |||
530 : | for (vec=0 ; vec<4 ; ++vec) | ||
531 : | { | ||
532 : | pMB->mvs[vec].x = pMB->mvs[vec].y = 0; | ||
533 : | } | ||
534 : | } | ||
535 : | h | 101 | } |
536 : | } | ||
537 : | } | ||
538 : | |||
539 : | |||
540 : | void HintedMEGet(Encoder * pEnc, int intra) | ||
541 : | { | ||
542 : | HINTINFO * hint; | ||
543 : | Bitstream bs; | ||
544 : | uint32_t x, y; | ||
545 : | int length, high; | ||
546 : | |||
547 : | hint = pEnc->mbParam.hint; | ||
548 : | |||
549 : | if (hint->rawhints) | ||
550 : | { | ||
551 : | hint->mvhint.intra = intra; | ||
552 : | } | ||
553 : | else | ||
554 : | { | ||
555 : | BitstreamInit(&bs, hint->hintstream, 0); | ||
556 : | BitstreamPutBit(&bs, intra); | ||
557 : | } | ||
558 : | |||
559 : | if (intra) | ||
560 : | { | ||
561 : | if (!hint->rawhints) | ||
562 : | { | ||
563 : | BitstreamPad(&bs); | ||
564 : | hint->hintlength = BitstreamLength(&bs); | ||
565 : | } | ||
566 : | return; | ||
567 : | } | ||
568 : | |||
569 : | suxen_drol | 136 | length = pEnc->current->fcode + 5; |
570 : | h | 101 | high = 1 << (length - 1); |
571 : | |||
572 : | if (hint->rawhints) | ||
573 : | { | ||
574 : | suxen_drol | 136 | hint->mvhint.fcode = pEnc->current->fcode; |
575 : | h | 101 | } |
576 : | else | ||
577 : | { | ||
578 : | suxen_drol | 136 | BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS); |
579 : | h | 101 | } |
580 : | |||
581 : | for (y=0 ; y<pEnc->mbParam.mb_height ; ++y) | ||
582 : | { | ||
583 : | for (x=0 ; x<pEnc->mbParam.mb_width ; ++x) | ||
584 : | { | ||
585 : | suxen_drol | 136 | MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
586 : | h | 101 | MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
587 : | VECTOR tmp; | ||
588 : | |||
589 : | if (hint->rawhints) | ||
590 : | { | ||
591 : | bhint->mode = pMB->mode; | ||
592 : | } | ||
593 : | else | ||
594 : | { | ||
595 : | BitstreamPutBits(&bs, pMB->mode, MODEBITS); | ||
596 : | } | ||
597 : | |||
598 : | if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) | ||
599 : | { | ||
600 : | tmp.x = pMB->mvs[0].x; | ||
601 : | tmp.y = pMB->mvs[0].y; | ||
602 : | tmp.x += (tmp.x < 0) ? high*2 : 0; | ||
603 : | tmp.y += (tmp.y < 0) ? high*2 : 0; | ||
604 : | |||
605 : | if (hint->rawhints) | ||
606 : | { | ||
607 : | bhint->mvs[0].x = tmp.x; | ||
608 : | bhint->mvs[0].y = tmp.y; | ||
609 : | } | ||
610 : | else | ||
611 : | { | ||
612 : | BitstreamPutBits(&bs, tmp.x, length); | ||
613 : | BitstreamPutBits(&bs, tmp.y, length); | ||
614 : | } | ||
615 : | } | ||
616 : | else if (pMB->mode == MODE_INTER4V) | ||
617 : | { | ||
618 : | int vec; | ||
619 : | |||
620 : | for (vec=0 ; vec<4 ; ++vec) | ||
621 : | { | ||
622 : | tmp.x = pMB->mvs[vec].x; | ||
623 : | tmp.y = pMB->mvs[vec].y; | ||
624 : | tmp.x += (tmp.x < 0) ? high*2 : 0; | ||
625 : | tmp.y += (tmp.y < 0) ? high*2 : 0; | ||
626 : | |||
627 : | if (hint->rawhints) | ||
628 : | { | ||
629 : | bhint->mvs[vec].x = tmp.x; | ||
630 : | bhint->mvs[vec].y = tmp.y; | ||
631 : | } | ||
632 : | else | ||
633 : | { | ||
634 : | BitstreamPutBits(&bs, tmp.x, length); | ||
635 : | BitstreamPutBits(&bs, tmp.y, length); | ||
636 : | } | ||
637 : | } | ||
638 : | } | ||
639 : | } | ||
640 : | } | ||
641 : | |||
642 : | if (!hint->rawhints) | ||
643 : | { | ||
644 : | BitstreamPad(&bs); | ||
645 : | hint->hintlength = BitstreamLength(&bs); | ||
646 : | } | ||
647 : | } | ||
648 : | |||
649 : | |||
650 : | h | 104 | static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits) |
651 : | { | ||
652 : | |||
653 : | DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); | ||
654 : | DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); | ||
655 : | |||
656 : | uint16_t x, y; | ||
657 : | |||
658 : | pEnc->iFrameNum = 0; | ||
659 : | suxen_drol | 136 | pEnc->mbParam.m_rounding_type = 1; |
660 : | pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; | ||
661 : | pEnc->current->coding_type = I_VOP; | ||
662 : | h | 104 | |
663 : | suxen_drol | 136 | BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
664 : | BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current); | ||
665 : | h | 104 | |
666 : | *pBits = BitstreamPos(bs); | ||
667 : | |||
668 : | pEnc->sStat.iTextBits = 0; | ||
669 : | pEnc->sStat.kblks = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; | ||
670 : | pEnc->sStat.mblks = pEnc->sStat.ublks = 0; | ||
671 : | |||
672 : | for (y = 0; y < pEnc->mbParam.mb_height; y++) | ||
673 : | for (x = 0; x < pEnc->mbParam.mb_width; x++) | ||
674 : | { | ||
675 : | suxen_drol | 136 | MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
676 : | h | 104 | |
677 : | CodeIntraMB(pEnc, pMB); | ||
678 : | |||
679 : | suxen_drol | 136 | MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
680 : | h | 104 | |
681 : | start_timer(); | ||
682 : | suxen_drol | 136 | MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
683 : | h | 104 | stop_prediction_timer(); |
684 : | |||
685 : | start_timer(); | ||
686 : | suxen_drol | 136 | MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
687 : | h | 104 | stop_coding_timer(); |
688 : | } | ||
689 : | |||
690 : | emms(); | ||
691 : | |||
692 : | *pBits = BitstreamPos(bs) - *pBits; | ||
693 : | pEnc->sStat.fMvPrevSigma = -1; | ||
694 : | pEnc->sStat.iMvSum = 0; | ||
695 : | pEnc->sStat.iMvCount = 0; | ||
696 : | suxen_drol | 136 | pEnc->mbParam.m_fcode = 2; |
697 : | h | 104 | |
698 : | suxen_drol | 136 | if (pEnc->current->global_flags & XVID_HINTEDME_GET) |
699 : | h | 104 | { |
700 : | HintedMEGet(pEnc, 1); | ||
701 : | } | ||
702 : | |||
703 : | return 1; // intra | ||
704 : | } | ||
705 : | |||
706 : | |||
707 : | Isibaar | 3 | #define INTRA_THRESHOLD 0.5 |
708 : | |||
709 : | static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header) | ||
710 : | { | ||
711 : | edgomez | 13 | float fSigma; |
712 : | Isibaar | 42 | |
713 : | edgomez | 78 | DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
714 : | DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); | ||
715 : | |||
716 : | Isibaar | 3 | int iLimit; |
717 : | edgomez | 13 | uint32_t x, y; |
718 : | int iSearchRange; | ||
719 : | Isibaar | 3 | bool bIntra; |
720 : | |||
721 : | suxen_drol | 136 | IMAGE *pCurrent = &pEnc->current->image; |
722 : | IMAGE *pRef = &pEnc->reference->image; | ||
723 : | Isibaar | 3 | |
724 : | h | 69 | start_timer(); |
725 : | edgomez | 78 | image_setedges(pRef, |
726 : | h | 85 | pEnc->mbParam.edged_width, |
727 : | pEnc->mbParam.edged_height, | ||
728 : | pEnc->mbParam.width, | ||
729 : | pEnc->mbParam.height, | ||
730 : | suxen_drol | 136 | pEnc->current->global_flags & XVID_INTERLACING); |
731 : | h | 69 | stop_edges_timer(); |
732 : | Isibaar | 3 | |
733 : | suxen_drol | 136 | pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
734 : | pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; | ||
735 : | pEnc->current->fcode = pEnc->mbParam.m_fcode; | ||
736 : | Isibaar | 3 | |
737 : | if (!force_inter) | ||
738 : | iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD); | ||
739 : | edgomez | 13 | else |
740 : | Isibaar | 3 | iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1; |
741 : | |||
742 : | suxen_drol | 136 | if ((pEnc->current->global_flags & XVID_HALFPEL)) { |
743 : | Isibaar | 3 | start_timer(); |
744 : | h | 85 | image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
745 : | pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, | ||
746 : | suxen_drol | 136 | pEnc->current->rounding_type); |
747 : | Isibaar | 3 | stop_inter_timer(); |
748 : | } | ||
749 : | |||
750 : | start_timer(); | ||
751 : | suxen_drol | 136 | if (pEnc->current->global_flags & XVID_HINTEDME_SET) |
752 : | h | 101 | { |
753 : | HintedMESet(pEnc, &bIntra); | ||
754 : | } | ||
755 : | else | ||
756 : | { | ||
757 : | suxen_drol | 136 | bIntra = MotionEstimation( |
758 : | &pEnc->mbParam, | ||
759 : | pEnc->current, | ||
760 : | pEnc->reference, | ||
761 : | &pEnc->vInterH, | ||
762 : | &pEnc->vInterV, | ||
763 : | &pEnc->vInterHV, | ||
764 : | iLimit); | ||
765 : | h | 101 | } |
766 : | Isibaar | 3 | stop_motion_timer(); |
767 : | |||
768 : | if (bIntra == 1) | ||
769 : | h | 101 | { |
770 : | Isibaar | 3 | return FrameCodeI(pEnc, bs, pBits); |
771 : | h | 101 | } |
772 : | Isibaar | 3 | |
773 : | suxen_drol | 136 | pEnc->current->coding_type = P_VOP; |
774 : | Isibaar | 3 | |
775 : | if(vol_header) | ||
776 : | suxen_drol | 136 | BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
777 : | Isibaar | 3 | |
778 : | suxen_drol | 136 | BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current); |
779 : | Isibaar | 3 | |
780 : | edgomez | 13 | *pBits = BitstreamPos(bs); |
781 : | Isibaar | 3 | |
782 : | edgomez | 13 | pEnc->sStat.iTextBits = 0; |
783 : | pEnc->sStat.iMvSum = 0; | ||
784 : | pEnc->sStat.iMvCount = 0; | ||
785 : | Isibaar | 3 | pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
786 : | |||
787 : | edgomez | 13 | for(y = 0; y < pEnc->mbParam.mb_height; y++) |
788 : | Isibaar | 3 | { |
789 : | for(x = 0; x < pEnc->mbParam.mb_width; x++) | ||
790 : | { | ||
791 : | suxen_drol | 136 | MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
792 : | Isibaar | 3 | |
793 : | edgomez | 13 | bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
794 : | Isibaar | 3 | |
795 : | if (!bIntra) | ||
796 : | edgomez | 13 | { |
797 : | Isibaar | 3 | start_timer(); |
798 : | edgomez | 78 | MBMotionCompensation(pMB, |
799 : | x, y, | ||
800 : | suxen_drol | 136 | &pEnc->reference->image, |
801 : | edgomez | 78 | &pEnc->vInterH, |
802 : | h | 85 | &pEnc->vInterV, |
803 : | &pEnc->vInterHV, | ||
804 : | suxen_drol | 136 | &pEnc->current->image, |
805 : | edgomez | 78 | dct_codes, |
806 : | edgomez | 13 | pEnc->mbParam.width, |
807 : | pEnc->mbParam.height, | ||
808 : | pEnc->mbParam.edged_width, | ||
809 : | suxen_drol | 136 | pEnc->current->rounding_type); |
810 : | Isibaar | 3 | stop_comp_timer(); |
811 : | |||
812 : | suxen_drol | 136 | if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
813 : | Isibaar | 3 | if(pMB->dquant != NO_CHANGE) { |
814 : | pMB->mode = MODE_INTER_Q; | ||
815 : | suxen_drol | 136 | pEnc->current->quant += DQtab[pMB->dquant]; |
816 : | if (pEnc->current->quant > 31) pEnc->current->quant = 31; | ||
817 : | else if(pEnc->current->quant < 1) pEnc->current->quant = 1; | ||
818 : | Isibaar | 3 | } |
819 : | } | ||
820 : | suxen_drol | 136 | pMB->quant = pEnc->current->quant; |
821 : | Isibaar | 3 | |
822 : | h | 69 | pMB->field_pred = 0; |
823 : | |||
824 : | suxen_drol | 136 | pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
825 : | edgomez | 13 | } |
826 : | Isibaar | 3 | else |
827 : | { | ||
828 : | CodeIntraMB(pEnc, pMB); | ||
829 : | suxen_drol | 136 | MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
830 : | Isibaar | 3 | } |
831 : | |||
832 : | edgomez | 13 | start_timer(); |
833 : | suxen_drol | 136 | MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
834 : | Isibaar | 3 | stop_prediction_timer(); |
835 : | |||
836 : | if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) | ||
837 : | { | ||
838 : | pEnc->sStat.kblks++; | ||
839 : | } | ||
840 : | else if (pMB->cbp || | ||
841 : | edgomez | 13 | pMB->mvs[0].x || pMB->mvs[0].y || |
842 : | pMB->mvs[1].x || pMB->mvs[1].y || | ||
843 : | pMB->mvs[2].x || pMB->mvs[2].y || | ||
844 : | pMB->mvs[3].x || pMB->mvs[3].y) | ||
845 : | Isibaar | 3 | { |
846 : | pEnc->sStat.mblks++; | ||
847 : | } | ||
848 : | else | ||
849 : | { | ||
850 : | pEnc->sStat.ublks++; | ||
851 : | } | ||
852 : | |||
853 : | start_timer(); | ||
854 : | suxen_drol | 136 | MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
855 : | Isibaar | 3 | stop_coding_timer(); |
856 : | } | ||
857 : | } | ||
858 : | |||
859 : | emms(); | ||
860 : | |||
861 : | suxen_drol | 136 | if (pEnc->current->global_flags & XVID_HINTEDME_GET) |
862 : | h | 101 | { |
863 : | HintedMEGet(pEnc, 0); | ||
864 : | } | ||
865 : | |||
866 : | Isibaar | 3 | if (pEnc->sStat.iMvCount == 0) |
867 : | pEnc->sStat.iMvCount = 1; | ||
868 : | |||
869 : | edgomez | 13 | fSigma = (float)sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount); |
870 : | Isibaar | 3 | |
871 : | suxen_drol | 136 | iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); |
872 : | Isibaar | 3 | |
873 : | edgomez | 13 | if ((fSigma > iSearchRange / 3) |
874 : | suxen_drol | 136 | && (pEnc->mbParam.m_fcode <= 3)) // maximum search range 128 |
875 : | edgomez | 13 | { |
876 : | suxen_drol | 136 | pEnc->mbParam.m_fcode++; |
877 : | Isibaar | 3 | iSearchRange *= 2; |
878 : | edgomez | 13 | } |
879 : | else if ((fSigma < iSearchRange / 6) | ||
880 : | && (pEnc->sStat.fMvPrevSigma >= 0) | ||
881 : | && (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) | ||
882 : | suxen_drol | 136 | && (pEnc->mbParam.m_fcode >= 2)) // minimum search range 16 |
883 : | edgomez | 13 | { |
884 : | suxen_drol | 136 | pEnc->mbParam.m_fcode--; |
885 : | Isibaar | 3 | iSearchRange /= 2; |
886 : | edgomez | 13 | } |
887 : | Isibaar | 3 | |
888 : | edgomez | 13 | pEnc->sStat.fMvPrevSigma = fSigma; |
889 : | Isibaar | 3 | |
890 : | *pBits = BitstreamPos(bs) - *pBits; | ||
891 : | |||
892 : | edgomez | 13 | return 0; // inter |
893 : | Isibaar | 3 | } |
894 : | suxen_drol | 118 | |
895 : | |||
896 : | |||
897 : | /* | ||
898 : | static void FrameCodeB(Encoder * pEnc, FRAMEINFO * frame, Bitstream * bs, uint32_t *pBits) | ||
899 : | { | ||
900 : | int16_t dct_codes[6][64]; | ||
901 : | int16_t qcoeff[6][64]; | ||
902 : | uint32_t x, y; | ||
903 : | VECTOR forward; | ||
904 : | VECTOR backward; | ||
905 : | |||
906 : | IMAGE *f_ref = &pEnc->reference->image; | ||
907 : | IMAGE *b_ref = &pEnc->current->image; | ||
908 : | |||
909 : | // forward | ||
910 : | image_setedges(f_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height); | ||
911 : | start_timer(); | ||
912 : | image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, | ||
913 : | pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0); | ||
914 : | stop_inter_timer(); | ||
915 : | |||
916 : | // backward | ||
917 : | image_setedges(b_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height); | ||
918 : | start_timer(); | ||
919 : | image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, | ||
920 : | pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0); | ||
921 : | stop_inter_timer(); | ||
922 : | |||
923 : | start_timer(); | ||
924 : | MotionEstimationBVOP(&pEnc->mbParam, frame, | ||
925 : | pEnc->reference->mbs, f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, | ||
926 : | pEnc->current->mbs, b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); | ||
927 : | |||
928 : | stop_motion_timer(); | ||
929 : | |||
930 : | if (test_quant_type(&pEnc->mbParam, pEnc->current)) | ||
931 : | { | ||
932 : | BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type); | ||
933 : | } | ||
934 : | |||
935 : | frame->coding_type = B_VOP; | ||
936 : | BitstreamWriteVopHeader(bs, B_VOP, frame->tick, 0, | ||
937 : | frame->quant, frame->fcode, frame->bcode); | ||
938 : | |||
939 : | *pBits = BitstreamPos(bs); | ||
940 : | |||
941 : | pEnc->sStat.iTextBits = 0; | ||
942 : | pEnc->sStat.iMvSum = 0; | ||
943 : | pEnc->sStat.iMvCount = 0; | ||
944 : | pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; | ||
945 : | |||
946 : | |||
947 : | for (y = 0; y < pEnc->mbParam.mb_height; y++) | ||
948 : | { | ||
949 : | // reset prediction | ||
950 : | |||
951 : | forward.x = 0; | ||
952 : | forward.y = 0; | ||
953 : | backward.x = 0; | ||
954 : | backward.y = 0; | ||
955 : | |||
956 : | for (x = 0; x < pEnc->mbParam.mb_width; x++) | ||
957 : | { | ||
958 : | MACROBLOCK * f_mb = &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width]; | ||
959 : | MACROBLOCK * b_mb = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; | ||
960 : | MACROBLOCK * mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; | ||
961 : | |||
962 : | // decoder ignores mb when refence block is INTER(0,0), CBP=0 | ||
963 : | if (mb->mode == MODE_NOT_CODED) | ||
964 : | { | ||
965 : | mb->mvs[0].x = 0; | ||
966 : | mb->mvs[0].y = 0; | ||
967 : | continue; | ||
968 : | } | ||
969 : | |||
970 : | MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, | ||
971 : | f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, | ||
972 : | b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, | ||
973 : | dct_codes); | ||
974 : | |||
975 : | mb->quant = frame->quant; | ||
976 : | mb->cbp = MBTransQuantInter(&pEnc->mbParam, frame, x, y, dct_codes, qcoeff); | ||
977 : | //mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant); | ||
978 : | |||
979 : | |||
980 : | if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) && | ||
981 : | mb->cbp == 0 && | ||
982 : | mb->mvs[0].x == 0 && | ||
983 : | mb->mvs[0].y == 0) | ||
984 : | { | ||
985 : | mb->mode = 5; // skipped | ||
986 : | } | ||
987 : | |||
988 : | if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) | ||
989 : | { | ||
990 : | mb->pmvs[0].x = mb->mvs[0].x - forward.x; | ||
991 : | mb->pmvs[0].y = mb->mvs[0].y - forward.y; | ||
992 : | forward.x = mb->mvs[0].x; | ||
993 : | forward.y = mb->mvs[0].y; | ||
994 : | } | ||
995 : | |||
996 : | if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) | ||
997 : | { | ||
998 : | mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x; | ||
999 : | mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y; | ||
1000 : | backward.x = mb->b_mvs[0].x; | ||
1001 : | backward.y = mb->b_mvs[0].y; | ||
1002 : | } | ||
1003 : | |||
1004 : | // printf("[%i %i] M=%i CBP=%i MVX=%i MVY=%i %i,%i %i,%i\n", x, y, pMB->mode, pMB->cbp, pMB->mvs[0].x, bmb->pmvs[0].x, bmb->pmvs[0].y, forward.x, forward.y); | ||
1005 : | |||
1006 : | start_timer(); | ||
1007 : | MBCodingBVOP(frame, mb, qcoeff, bs, &pEnc->sStat); | ||
1008 : | stop_coding_timer(); | ||
1009 : | } | ||
1010 : | } | ||
1011 : | |||
1012 : | emms(); | ||
1013 : | |||
1014 : | // TODO: dynamic fcode/bcode ??? | ||
1015 : | |||
1016 : | *pBits = BitstreamPos(bs) - *pBits; | ||
1017 : | |||
1018 : | } | ||
1019 : | |||
1020 : | chl | 138 | */ |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |