21 |
#include "utils/mem_align.h" |
#include "utils/mem_align.h" |
22 |
|
|
23 |
#define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT |
#define ENC_CHECK(X) if(!(X)) return XVID_ERR_FORMAT |
24 |
|
#define SWAP(A,B) { void * tmp = A; A = B; B = tmp; } |
25 |
|
|
26 |
|
|
27 |
static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits); |
static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits); |
38 |
}; |
}; |
39 |
|
|
40 |
|
|
41 |
|
void static image_null(IMAGE * image) |
42 |
|
{ |
43 |
|
image->y = image->u = image->v = NULL; |
44 |
|
} |
45 |
|
|
46 |
|
|
47 |
int encoder_create(XVID_ENC_PARAM * pParam) |
int encoder_create(XVID_ENC_PARAM * pParam) |
48 |
{ |
{ |
49 |
Encoder *pEnc; |
Encoder *pEnc; |
129 |
|
|
130 |
/* Fill rate control parameters */ |
/* Fill rate control parameters */ |
131 |
|
|
|
pEnc->mbParam.quant = 4; |
|
|
|
|
132 |
pEnc->bitrate = pParam->rc_bitrate; |
pEnc->bitrate = pParam->rc_bitrate; |
133 |
|
|
134 |
pEnc->iFrameNum = 0; |
pEnc->iFrameNum = 0; |
135 |
pEnc->iMaxKeyInterval = pParam->max_key_interval; |
pEnc->iMaxKeyInterval = pParam->max_key_interval; |
136 |
|
|
137 |
/* try to allocate memory */ |
/* try to allocate frame memory */ |
138 |
|
|
139 |
|
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 |
|
|
149 |
|
/* try to allocate mb memory */ |
150 |
|
|
151 |
|
pEnc->current->mbs = NULL; |
152 |
|
pEnc->reference->mbs = NULL; |
153 |
|
|
154 |
pEnc->sCurrent.y = pEnc->sCurrent.u = pEnc->sCurrent.v = NULL; |
OutputDebugString("malloc mbs"); |
155 |
pEnc->sReference.y = pEnc->sReference.u = pEnc->sReference.v = NULL; |
if ((pEnc->current->mbs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL || |
156 |
pEnc->vInterH.y = pEnc->vInterH.u = pEnc->vInterH.v = NULL; |
(pEnc->reference->mbs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL) |
157 |
pEnc->vInterV.y = pEnc->vInterV.u = pEnc->vInterV.v = NULL; |
{ |
158 |
pEnc->vInterVf.y = pEnc->vInterVf.u = pEnc->vInterVf.v = NULL; |
if (pEnc->current->mbs) xvid_free(pEnc->current->mbs); |
159 |
pEnc->vInterHV.y = pEnc->vInterHV.u = pEnc->vInterHV.v = NULL; |
xvid_free(pEnc->current); |
160 |
pEnc->vInterHVf.y = pEnc->vInterHVf.u = pEnc->vInterHVf.v = NULL; |
xvid_free(pEnc->reference); |
161 |
|
xvid_free(pEnc); |
162 |
|
} |
163 |
|
|
164 |
|
/* try to allocate image memory */ |
165 |
|
|
166 |
|
#ifdef _DEBUG |
167 |
|
image_null(&pEnc->sOriginal); |
168 |
|
#endif |
169 |
|
image_null(&pEnc->current->image); |
170 |
|
image_null(&pEnc->reference->image); |
171 |
|
image_null(&pEnc->vInterH); |
172 |
|
image_null(&pEnc->vInterV); |
173 |
|
image_null(&pEnc->vInterVf); |
174 |
|
image_null(&pEnc->vInterHV); |
175 |
|
image_null(&pEnc->vInterHVf); |
176 |
|
|
|
pEnc->pMBs = NULL; |
|
177 |
|
|
178 |
if (image_create(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
OutputDebugString("malloc images"); |
179 |
image_create(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
if ( |
180 |
|
#ifdef _DEBUG |
181 |
|
image_create(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
182 |
|
#endif |
183 |
|
image_create(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
184 |
|
image_create(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
185 |
image_create(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
image_create(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
186 |
image_create(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
image_create(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
187 |
image_create(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
image_create(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
188 |
image_create(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
image_create(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
189 |
image_create(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
image_create(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0) |
190 |
|
{ |
191 |
#ifdef _DEBUG |
#ifdef _DEBUG |
192 |
image_create(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
193 |
#endif |
#endif |
194 |
(pEnc->pMBs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL) |
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
195 |
{ |
image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
196 |
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
197 |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
198 |
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
199 |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
200 |
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
201 |
#ifdef _DEBUG |
|
202 |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
xvid_free(pEnc->current); |
203 |
#endif |
xvid_free(pEnc->reference); |
|
if (pEnc) |
|
|
{ |
|
204 |
xvid_free(pEnc); |
xvid_free(pEnc); |
|
} |
|
205 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
206 |
} |
} |
207 |
|
|
|
// init macroblock array |
|
|
for (i = 0; i < pEnc->mbParam.mb_width * pEnc->mbParam.mb_height; i++) |
|
|
{ |
|
|
pEnc->pMBs[i].dquant = NO_CHANGE; |
|
|
} |
|
|
|
|
208 |
pParam->handle = (void *)pEnc; |
pParam->handle = (void *)pEnc; |
209 |
|
|
210 |
if (pParam->rc_bitrate) |
if (pParam->rc_bitrate) |
223 |
int encoder_destroy(Encoder * pEnc) |
int encoder_destroy(Encoder * pEnc) |
224 |
{ |
{ |
225 |
ENC_CHECK(pEnc); |
ENC_CHECK(pEnc); |
|
ENC_CHECK(pEnc->sCurrent.y); |
|
|
ENC_CHECK(pEnc->sReference.y); |
|
226 |
|
|
227 |
xvid_free(pEnc->pMBs); |
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
228 |
image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
229 |
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
230 |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
231 |
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
234 |
#ifdef _DEBUG |
#ifdef _DEBUG |
235 |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
236 |
#endif |
#endif |
237 |
xvid_free(pEnc); |
xvid_free(pEnc->current->mbs); |
238 |
|
xvid_free(pEnc->current); |
239 |
|
|
240 |
|
xvid_free(pEnc->reference->mbs); |
241 |
|
xvid_free(pEnc->reference); |
242 |
|
|
243 |
|
xvid_free(pEnc); |
244 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
245 |
} |
} |
246 |
|
|
262 |
ENC_CHECK(pFrame->bitstream); |
ENC_CHECK(pFrame->bitstream); |
263 |
ENC_CHECK(pFrame->image); |
ENC_CHECK(pFrame->image); |
264 |
|
|
265 |
pEnc->mbParam.global_flags = pFrame->general; |
SWAP(pEnc->current, pEnc->reference); |
266 |
pEnc->mbParam.motion_flags = pFrame->motion; |
|
267 |
|
pEnc->current->global_flags = pFrame->general; |
268 |
|
pEnc->current->motion_flags = pFrame->motion; |
269 |
pEnc->mbParam.hint = &pFrame->hint; |
pEnc->mbParam.hint = &pFrame->hint; |
270 |
|
|
271 |
start_timer(); |
start_timer(); |
272 |
if (image_input(&pEnc->sCurrent, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width, |
if (image_input(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width, |
273 |
pFrame->image, pFrame->colorspace)) |
pFrame->image, pFrame->colorspace)) |
274 |
{ |
{ |
275 |
return XVID_ERR_FORMAT; |
return XVID_ERR_FORMAT; |
276 |
} |
} |
277 |
stop_conv_timer(); |
stop_conv_timer(); |
278 |
|
|
|
EMMS(); |
|
|
|
|
279 |
#ifdef _DEBUG |
#ifdef _DEBUG |
280 |
image_copy(&pEnc->sOriginal, &pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
image_copy(&pEnc->sOriginal, &pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
281 |
#endif |
#endif |
282 |
|
|
283 |
|
EMMS(); |
284 |
|
|
285 |
BitstreamInit(&bs, pFrame->bitstream, 0); |
BitstreamInit(&bs, pFrame->bitstream, 0); |
286 |
|
|
287 |
if (pFrame->quant == 0) |
if (pFrame->quant == 0) |
288 |
{ |
{ |
289 |
pEnc->mbParam.quant = RateControlGetQ(0); |
pEnc->current->quant = RateControlGetQ(0); |
290 |
} |
} |
291 |
else |
else |
292 |
{ |
{ |
293 |
pEnc->mbParam.quant = pFrame->quant; |
pEnc->current->quant = pFrame->quant; |
294 |
} |
} |
295 |
|
|
296 |
if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) |
297 |
{ |
{ |
298 |
int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); |
int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); |
299 |
|
|
300 |
pEnc->mbParam.quant = adaptive_quantization(pEnc->sCurrent.y, |
pEnc->current->quant = adaptive_quantization(pEnc->current->image.y, |
301 |
pEnc->mbParam.width, |
pEnc->mbParam.edged_width, // stride |
302 |
temp_dquants, |
temp_dquants, |
303 |
pEnc->mbParam.quant, |
pEnc->current->quant, |
304 |
pEnc->mbParam.quant, |
pEnc->current->quant, // min_quant |
305 |
2*pEnc->mbParam.quant, |
2*pEnc->current->quant, // max_quant |
306 |
pEnc->mbParam.mb_width, |
pEnc->mbParam.mb_width, |
307 |
pEnc->mbParam.mb_height); |
pEnc->mbParam.mb_height); |
308 |
|
|
309 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
310 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
311 |
{ |
{ |
312 |
MACROBLOCK *pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width]; |
MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
313 |
pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)]; |
pMB->dquant = iDQtab[(temp_dquants[y * pEnc->mbParam.mb_width + x] + 2)]; |
314 |
} |
} |
315 |
xvid_free(temp_dquants); |
xvid_free(temp_dquants); |
316 |
} |
} |
317 |
|
|
318 |
if(pEnc->mbParam.global_flags & XVID_H263QUANT) { |
if (pEnc->current->global_flags & XVID_H263QUANT) { |
319 |
if(pEnc->mbParam.quant_type != H263_QUANT) |
if(pEnc->mbParam.m_quant_type != H263_QUANT) |
320 |
write_vol_header = 1; |
write_vol_header = 1; |
321 |
pEnc->mbParam.quant_type = H263_QUANT; |
pEnc->mbParam.m_quant_type = H263_QUANT; |
322 |
} |
} |
323 |
else if(pEnc->mbParam.global_flags & XVID_MPEGQUANT) { |
else if(pEnc->current->global_flags & XVID_MPEGQUANT) { |
324 |
int ret1, ret2; |
int ret1, ret2; |
325 |
|
|
326 |
ret1 = ret2 = 0; |
ret1 = ret2 = 0; |
327 |
|
|
328 |
if(pEnc->mbParam.quant_type != MPEG4_QUANT) |
if(pEnc->mbParam.m_quant_type != MPEG4_QUANT) |
329 |
write_vol_header = 1; |
write_vol_header = 1; |
330 |
|
|
331 |
pEnc->mbParam.quant_type = MPEG4_QUANT; |
pEnc->mbParam.m_quant_type = MPEG4_QUANT; |
332 |
|
|
333 |
if ((pEnc->mbParam.global_flags & XVID_CUSTOM_QMATRIX) > 0) { |
if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) { |
334 |
if(pFrame->quant_intra_matrix != NULL) |
if(pFrame->quant_intra_matrix != NULL) |
335 |
ret1 = set_intra_matrix(pFrame->quant_intra_matrix); |
ret1 = set_intra_matrix(pFrame->quant_intra_matrix); |
336 |
if(pFrame->quant_inter_matrix != NULL) |
if(pFrame->quant_inter_matrix != NULL) |
368 |
|
|
369 |
if (pResult) |
if (pResult) |
370 |
{ |
{ |
371 |
pResult->quant = pEnc->mbParam.quant; |
pResult->quant = pEnc->current->quant; |
372 |
pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
373 |
pResult->kblks = pEnc->sStat.kblks; |
pResult->kblks = pEnc->sStat.kblks; |
374 |
pResult->mblks = pEnc->sStat.mblks; |
pResult->mblks = pEnc->sStat.mblks; |
379 |
|
|
380 |
if (pFrame->quant == 0) |
if (pFrame->quant == 0) |
381 |
{ |
{ |
382 |
RateControlUpdate(pEnc->mbParam.quant, pFrame->length, pFrame->intra); |
RateControlUpdate(pEnc->current->quant, pFrame->length, pFrame->intra); |
383 |
} |
} |
384 |
|
|
385 |
#ifdef _DEBUG |
#ifdef _DEBUG |
386 |
psnr = image_psnr(&pEnc->sOriginal, &pEnc->sCurrent, pEnc->mbParam.edged_width, |
psnr = image_psnr(&pEnc->sOriginal, &pEnc->current->image, pEnc->mbParam.edged_width, |
387 |
pEnc->mbParam.width, pEnc->mbParam.height); |
pEnc->mbParam.width, pEnc->mbParam.height); |
388 |
|
|
389 |
sprintf(temp, "PSNR: %f\n", psnr); |
sprintf(temp, "PSNR: %f\n", psnr); |
391 |
#endif |
#endif |
392 |
|
|
393 |
pEnc->iFrameNum++; |
pEnc->iFrameNum++; |
|
image_swap(&pEnc->sCurrent, &pEnc->sReference); |
|
394 |
|
|
395 |
stop_global_timer(); |
stop_global_timer(); |
396 |
write_timer(); |
write_timer(); |
403 |
|
|
404 |
pMB->mode = MODE_INTRA; |
pMB->mode = MODE_INTRA; |
405 |
|
|
406 |
if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) { |
/* zero mv statistics */ |
407 |
|
pMB->mvs[0].x = pMB->mvs[1].x = pMB->mvs[2].x = pMB->mvs[3].x = 0; |
408 |
|
pMB->mvs[0].y = pMB->mvs[1].y = pMB->mvs[2].y = pMB->mvs[3].y = 0; |
409 |
|
pMB->sad8[0] = pMB->sad8[1] = pMB->sad8[2] = pMB->sad8[3] = 0; |
410 |
|
pMB->sad16 = 0; |
411 |
|
|
412 |
|
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
413 |
if(pMB->dquant != NO_CHANGE) |
if(pMB->dquant != NO_CHANGE) |
414 |
{ |
{ |
415 |
pMB->mode = MODE_INTRA_Q; |
pMB->mode = MODE_INTRA_Q; |
416 |
pEnc->mbParam.quant += DQtab[pMB->dquant]; |
pEnc->current->quant += DQtab[pMB->dquant]; |
417 |
|
|
418 |
if (pEnc->mbParam.quant > 31) pEnc->mbParam.quant = 31; |
if (pEnc->current->quant > 31) pEnc->current->quant = 31; |
419 |
if (pEnc->mbParam.quant < 1) pEnc->mbParam.quant = 1; |
if (pEnc->current->quant < 1) pEnc->current->quant = 1; |
420 |
} |
} |
421 |
} |
} |
422 |
|
|
423 |
pMB->quant = pEnc->mbParam.quant; |
pMB->quant = pEnc->current->quant; |
424 |
} |
} |
425 |
|
|
426 |
|
|
451 |
return; |
return; |
452 |
} |
} |
453 |
|
|
454 |
pEnc->mbParam.fixed_code = (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS); |
pEnc->current->fcode = (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS); |
455 |
|
|
456 |
length = pEnc->mbParam.fixed_code + 5; |
length = pEnc->current->fcode + 5; |
457 |
high = 1 << (length - 1); |
high = 1 << (length - 1); |
458 |
|
|
459 |
for (y=0 ; y<pEnc->mbParam.mb_height ; ++y) |
for (y=0 ; y<pEnc->mbParam.mb_height ; ++y) |
460 |
{ |
{ |
461 |
for (x=0 ; x<pEnc->mbParam.mb_width ; ++x) |
for (x=0 ; x<pEnc->mbParam.mb_width ; ++x) |
462 |
{ |
{ |
463 |
MACROBLOCK * pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width]; |
MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
464 |
MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
465 |
VECTOR pred[4]; |
VECTOR pred[4]; |
466 |
VECTOR tmp; |
VECTOR tmp; |
479 |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
480 |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
481 |
|
|
482 |
get_pmvdata(pEnc->pMBs, x, y, pEnc->mbParam.mb_width, 0, pred, dummy); |
get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, 0, pred, dummy); |
483 |
|
|
484 |
for (vec=0 ; vec<4 ; ++vec) |
for (vec=0 ; vec<4 ; ++vec) |
485 |
{ |
{ |
498 |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
499 |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
500 |
|
|
501 |
get_pmvdata(pEnc->pMBs, x, y, pEnc->mbParam.mb_width, vec, pred, dummy); |
get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, vec, pred, dummy); |
502 |
|
|
503 |
pMB->mvs[vec].x = tmp.x; |
pMB->mvs[vec].x = tmp.x; |
504 |
pMB->mvs[vec].y = tmp.y; |
pMB->mvs[vec].y = tmp.y; |
514 |
} |
} |
515 |
} |
} |
516 |
|
|
517 |
if (pMB->dquant != NO_CHANGE && pMB->mode == MODE_INTER4V) |
if (pMB->mode == MODE_INTER4V && |
518 |
|
(pEnc->current->global_flags & XVID_LUMIMASKING) && pMB->dquant != NO_CHANGE) |
519 |
{ |
{ |
520 |
pMB->mode = MODE_INTRA; |
pMB->mode = MODE_INTRA; |
521 |
|
|
558 |
return; |
return; |
559 |
} |
} |
560 |
|
|
561 |
length = pEnc->mbParam.fixed_code + 5; |
length = pEnc->current->fcode + 5; |
562 |
high = 1 << (length - 1); |
high = 1 << (length - 1); |
563 |
|
|
564 |
if (hint->rawhints) |
if (hint->rawhints) |
565 |
{ |
{ |
566 |
hint->mvhint.fcode = pEnc->mbParam.fixed_code; |
hint->mvhint.fcode = pEnc->current->fcode; |
567 |
} |
} |
568 |
else |
else |
569 |
{ |
{ |
570 |
BitstreamPutBits(&bs, pEnc->mbParam.fixed_code, FCODEBITS); |
BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS); |
571 |
} |
} |
572 |
|
|
573 |
for (y=0 ; y<pEnc->mbParam.mb_height ; ++y) |
for (y=0 ; y<pEnc->mbParam.mb_height ; ++y) |
574 |
{ |
{ |
575 |
for (x=0 ; x<pEnc->mbParam.mb_width ; ++x) |
for (x=0 ; x<pEnc->mbParam.mb_width ; ++x) |
576 |
{ |
{ |
577 |
MACROBLOCK * pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width]; |
MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
578 |
MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
579 |
VECTOR tmp; |
VECTOR tmp; |
580 |
|
|
648 |
uint16_t x, y; |
uint16_t x, y; |
649 |
|
|
650 |
pEnc->iFrameNum = 0; |
pEnc->iFrameNum = 0; |
651 |
pEnc->mbParam.rounding_type = 1; |
pEnc->mbParam.m_rounding_type = 1; |
652 |
pEnc->mbParam.coding_type = I_VOP; |
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
653 |
|
pEnc->current->coding_type = I_VOP; |
654 |
|
|
655 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
656 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current); |
657 |
|
|
658 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
659 |
|
|
664 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
665 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
666 |
{ |
{ |
667 |
MACROBLOCK *pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width]; |
MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
668 |
|
|
669 |
CodeIntraMB(pEnc, pMB); |
CodeIntraMB(pEnc, pMB); |
670 |
|
|
671 |
MBTransQuantIntra(&pEnc->mbParam, pMB, x, y, dct_codes, qcoeff, &pEnc->sCurrent); |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
672 |
|
|
673 |
start_timer(); |
start_timer(); |
674 |
MBPrediction(&pEnc->mbParam, x, y, pEnc->mbParam.mb_width, qcoeff, pEnc->pMBs); |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
675 |
stop_prediction_timer(); |
stop_prediction_timer(); |
676 |
|
|
677 |
start_timer(); |
start_timer(); |
678 |
MBCoding(&pEnc->mbParam, pMB, qcoeff, bs, &pEnc->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
679 |
stop_coding_timer(); |
stop_coding_timer(); |
680 |
} |
} |
681 |
|
|
685 |
pEnc->sStat.fMvPrevSigma = -1; |
pEnc->sStat.fMvPrevSigma = -1; |
686 |
pEnc->sStat.iMvSum = 0; |
pEnc->sStat.iMvSum = 0; |
687 |
pEnc->sStat.iMvCount = 0; |
pEnc->sStat.iMvCount = 0; |
688 |
pEnc->mbParam.fixed_code = 2; |
pEnc->mbParam.m_fcode = 2; |
689 |
|
|
690 |
if (pEnc->mbParam.global_flags & XVID_HINTEDME_GET) |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) |
691 |
{ |
{ |
692 |
HintedMEGet(pEnc, 1); |
HintedMEGet(pEnc, 1); |
693 |
} |
} |
710 |
int iSearchRange; |
int iSearchRange; |
711 |
bool bIntra; |
bool bIntra; |
712 |
|
|
713 |
IMAGE *pCurrent = &pEnc->sCurrent; |
IMAGE *pCurrent = &pEnc->current->image; |
714 |
IMAGE *pRef = &pEnc->sReference; |
IMAGE *pRef = &pEnc->reference->image; |
715 |
|
|
716 |
start_timer(); |
start_timer(); |
717 |
image_setedges(pRef, |
image_setedges(pRef, |
719 |
pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_height, |
720 |
pEnc->mbParam.width, |
pEnc->mbParam.width, |
721 |
pEnc->mbParam.height, |
pEnc->mbParam.height, |
722 |
pEnc->mbParam.global_flags & XVID_INTERLACING); |
pEnc->current->global_flags & XVID_INTERLACING); |
723 |
stop_edges_timer(); |
stop_edges_timer(); |
724 |
|
|
725 |
pEnc->mbParam.rounding_type = 1 - pEnc->mbParam.rounding_type; |
pEnc->mbParam.m_rounding_type = 1 - pEnc->mbParam.m_rounding_type; |
726 |
|
pEnc->current->rounding_type = pEnc->mbParam.m_rounding_type; |
727 |
|
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
728 |
|
|
729 |
if (!force_inter) |
if (!force_inter) |
730 |
iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD); |
iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD); |
731 |
else |
else |
732 |
iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1; |
iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1; |
733 |
|
|
734 |
if ((pEnc->mbParam.global_flags & XVID_HALFPEL) > 0) { |
if ((pEnc->current->global_flags & XVID_HALFPEL)) { |
735 |
start_timer(); |
start_timer(); |
736 |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
737 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
738 |
pEnc->mbParam.rounding_type); |
pEnc->current->rounding_type); |
739 |
stop_inter_timer(); |
stop_inter_timer(); |
740 |
} |
} |
741 |
|
|
742 |
start_timer(); |
start_timer(); |
743 |
if (pEnc->mbParam.global_flags & XVID_HINTEDME_SET) |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) |
744 |
{ |
{ |
745 |
HintedMESet(pEnc, &bIntra); |
HintedMESet(pEnc, &bIntra); |
746 |
} |
} |
747 |
else |
else |
748 |
{ |
{ |
749 |
bIntra = MotionEstimation(pEnc->pMBs, &pEnc->mbParam, &pEnc->sReference, |
bIntra = MotionEstimation( |
750 |
&pEnc->vInterH, &pEnc->vInterV, |
&pEnc->mbParam, |
751 |
&pEnc->vInterHV, &pEnc->sCurrent, iLimit); |
pEnc->current, |
752 |
|
pEnc->reference, |
753 |
|
&pEnc->vInterH, |
754 |
|
&pEnc->vInterV, |
755 |
|
&pEnc->vInterHV, |
756 |
|
iLimit); |
757 |
} |
} |
758 |
stop_motion_timer(); |
stop_motion_timer(); |
759 |
|
|
762 |
return FrameCodeI(pEnc, bs, pBits); |
return FrameCodeI(pEnc, bs, pBits); |
763 |
} |
} |
764 |
|
|
765 |
pEnc->mbParam.coding_type = P_VOP; |
pEnc->current->coding_type = P_VOP; |
766 |
|
|
767 |
if(vol_header) |
if(vol_header) |
768 |
BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam, pEnc->current); |
769 |
|
|
770 |
BitstreamWriteVopHeader(bs, &pEnc->mbParam); |
BitstreamWriteVopHeader(bs, &pEnc->mbParam, pEnc->current); |
771 |
|
|
772 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
773 |
|
|
780 |
{ |
{ |
781 |
for(x = 0; x < pEnc->mbParam.mb_width; x++) |
for(x = 0; x < pEnc->mbParam.mb_width; x++) |
782 |
{ |
{ |
783 |
MACROBLOCK * pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width]; |
MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
784 |
|
|
785 |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
786 |
|
|
789 |
start_timer(); |
start_timer(); |
790 |
MBMotionCompensation(pMB, |
MBMotionCompensation(pMB, |
791 |
x, y, |
x, y, |
792 |
&pEnc->sReference, |
&pEnc->reference->image, |
793 |
&pEnc->vInterH, |
&pEnc->vInterH, |
794 |
&pEnc->vInterV, |
&pEnc->vInterV, |
795 |
&pEnc->vInterHV, |
&pEnc->vInterHV, |
796 |
&pEnc->sCurrent, |
&pEnc->current->image, |
797 |
dct_codes, |
dct_codes, |
798 |
pEnc->mbParam.width, |
pEnc->mbParam.width, |
799 |
pEnc->mbParam.height, |
pEnc->mbParam.height, |
800 |
pEnc->mbParam.edged_width, |
pEnc->mbParam.edged_width, |
801 |
pEnc->mbParam.rounding_type); |
pEnc->current->rounding_type); |
802 |
stop_comp_timer(); |
stop_comp_timer(); |
803 |
|
|
804 |
if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) { |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
805 |
if(pMB->dquant != NO_CHANGE) { |
if(pMB->dquant != NO_CHANGE) { |
806 |
pMB->mode = MODE_INTER_Q; |
pMB->mode = MODE_INTER_Q; |
807 |
pEnc->mbParam.quant += DQtab[pMB->dquant]; |
pEnc->current->quant += DQtab[pMB->dquant]; |
808 |
if (pEnc->mbParam.quant > 31) pEnc->mbParam.quant = 31; |
if (pEnc->current->quant > 31) pEnc->current->quant = 31; |
809 |
else if(pEnc->mbParam.quant < 1) pEnc->mbParam.quant = 1; |
else if(pEnc->current->quant < 1) pEnc->current->quant = 1; |
810 |
} |
} |
811 |
} |
} |
812 |
pMB->quant = pEnc->mbParam.quant; |
pMB->quant = pEnc->current->quant; |
813 |
|
|
814 |
pMB->field_pred = 0; |
pMB->field_pred = 0; |
815 |
|
|
816 |
pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pMB, x, y, dct_codes, qcoeff, pCurrent); |
pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
817 |
} |
} |
818 |
else |
else |
819 |
{ |
{ |
820 |
CodeIntraMB(pEnc, pMB); |
CodeIntraMB(pEnc, pMB); |
821 |
MBTransQuantIntra(&pEnc->mbParam, pMB, x, y, dct_codes, qcoeff, pCurrent); |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
822 |
} |
} |
823 |
|
|
824 |
start_timer(); |
start_timer(); |
825 |
MBPrediction(&pEnc->mbParam, x, y, pEnc->mbParam.mb_width, qcoeff, pEnc->pMBs); |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
826 |
stop_prediction_timer(); |
stop_prediction_timer(); |
827 |
|
|
828 |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) |
843 |
} |
} |
844 |
|
|
845 |
start_timer(); |
start_timer(); |
846 |
MBCoding(&pEnc->mbParam, pMB, qcoeff, bs, &pEnc->sStat); |
MBCoding(pEnc->current, pMB, qcoeff, bs, &pEnc->sStat); |
847 |
stop_coding_timer(); |
stop_coding_timer(); |
848 |
} |
} |
849 |
} |
} |
850 |
|
|
851 |
emms(); |
emms(); |
852 |
|
|
853 |
if (pEnc->mbParam.global_flags & XVID_HINTEDME_GET) |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) |
854 |
{ |
{ |
855 |
HintedMEGet(pEnc, 0); |
HintedMEGet(pEnc, 0); |
856 |
} |
} |
860 |
|
|
861 |
fSigma = (float)sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount); |
fSigma = (float)sqrt((float) pEnc->sStat.iMvSum / pEnc->sStat.iMvCount); |
862 |
|
|
863 |
iSearchRange = 1 << (3 + pEnc->mbParam.fixed_code); |
iSearchRange = 1 << (3 + pEnc->mbParam.m_fcode); |
864 |
|
|
865 |
if ((fSigma > iSearchRange / 3) |
if ((fSigma > iSearchRange / 3) |
866 |
&& (pEnc->mbParam.fixed_code <= 3)) // maximum search range 128 |
&& (pEnc->mbParam.m_fcode <= 3)) // maximum search range 128 |
867 |
{ |
{ |
868 |
pEnc->mbParam.fixed_code++; |
pEnc->mbParam.m_fcode++; |
869 |
iSearchRange *= 2; |
iSearchRange *= 2; |
870 |
} |
} |
871 |
else if ((fSigma < iSearchRange / 6) |
else if ((fSigma < iSearchRange / 6) |
872 |
&& (pEnc->sStat.fMvPrevSigma >= 0) |
&& (pEnc->sStat.fMvPrevSigma >= 0) |
873 |
&& (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) |
&& (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) |
874 |
&& (pEnc->mbParam.fixed_code >= 2)) // minimum search range 16 |
&& (pEnc->mbParam.m_fcode >= 2)) // minimum search range 16 |
875 |
{ |
{ |
876 |
pEnc->mbParam.fixed_code--; |
pEnc->mbParam.m_fcode--; |
877 |
iSearchRange /= 2; |
iSearchRange /= 2; |
878 |
} |
} |
879 |
|
|