1 |
|
// 14.04.2002 added FrameCodeB() |
2 |
|
|
3 |
#include <stdlib.h> |
#include <stdlib.h> |
4 |
#include <stdio.h> |
#include <stdio.h> |
5 |
#include <math.h> |
#include <math.h> |
84 |
pParam->bitrate = 900000; |
pParam->bitrate = 900000; |
85 |
|
|
86 |
if (pParam->rc_buffersize <= 0) |
if (pParam->rc_buffersize <= 0) |
87 |
pParam->rc_buffersize = pParam->bitrate * pParam->fbase; |
pParam->rc_buffersize = 16; |
88 |
|
|
89 |
if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31)) |
if ((pParam->min_quantizer <= 0) || (pParam->min_quantizer > 31)) |
90 |
pParam->min_quantizer = 1; |
pParam->min_quantizer = 1; |
98 |
if (pParam->max_quantizer < pParam->min_quantizer) |
if (pParam->max_quantizer < pParam->min_quantizer) |
99 |
pParam->max_quantizer = pParam->min_quantizer; |
pParam->max_quantizer = pParam->min_quantizer; |
100 |
|
|
101 |
if ((pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), 16)) == NULL) |
if ((pEnc = (Encoder *) xvid_malloc(sizeof(Encoder), CACHE_LINE)) == NULL) |
102 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
103 |
|
|
104 |
/* Fill members of Encoder structure */ |
/* Fill members of Encoder structure */ |
123 |
pEnc->iFrameNum = 0; |
pEnc->iFrameNum = 0; |
124 |
pEnc->iMaxKeyInterval = pParam->max_key_interval; |
pEnc->iMaxKeyInterval = pParam->max_key_interval; |
125 |
|
|
126 |
if (image_create(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0) |
/* try to allocate memory */ |
|
{ |
|
|
xvid_free(pEnc); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
|
|
|
|
if (image_create(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0) |
|
|
{ |
|
|
image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
xvid_free(pEnc); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
|
|
|
|
if (image_create(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0) |
|
|
{ |
|
|
image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
xvid_free(pEnc); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
|
|
|
|
if (image_create(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0) |
|
|
{ |
|
|
image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
xvid_free(pEnc); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
|
|
|
|
if (image_create(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0) |
|
|
{ |
|
|
image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
|
|
xvid_free(pEnc); |
|
|
return XVID_ERR_MEMORY; |
|
|
} |
|
127 |
|
|
128 |
pEnc->pMBs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, 16); |
pEnc->sCurrent.y = pEnc->sCurrent.u = pEnc->sCurrent.v = NULL; |
129 |
if (pEnc->pMBs == NULL) |
pEnc->sReference.y = pEnc->sReference.u = pEnc->sReference.v = NULL; |
130 |
|
pEnc->vInterH.y = pEnc->vInterH.u = pEnc->vInterH.v = NULL; |
131 |
|
pEnc->vInterV.y = pEnc->vInterV.u = pEnc->vInterV.v = NULL; |
132 |
|
pEnc->vInterVf.y = pEnc->vInterVf.u = pEnc->vInterVf.v = NULL; |
133 |
|
pEnc->vInterHV.y = pEnc->vInterHV.u = pEnc->vInterHV.v = NULL; |
134 |
|
pEnc->vInterHVf.y = pEnc->vInterHVf.u = pEnc->vInterHVf.v = NULL; |
135 |
|
|
136 |
|
pEnc->pMBs = NULL; |
137 |
|
|
138 |
|
if (image_create(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
139 |
|
image_create(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
140 |
|
image_create(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
141 |
|
image_create(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
142 |
|
image_create(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
143 |
|
image_create(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
144 |
|
image_create(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
145 |
|
#ifdef _DEBUG |
146 |
|
image_create(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height) < 0 || |
147 |
|
#endif |
148 |
|
(pEnc->pMBs = xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * pEnc->mbParam.mb_height, CACHE_LINE)) == NULL) |
149 |
{ |
{ |
150 |
image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
151 |
image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->sReference, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
152 |
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
153 |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
154 |
|
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
155 |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
156 |
|
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
157 |
|
#ifdef _DEBUG |
158 |
|
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
159 |
|
#endif |
160 |
|
if (pEnc) |
161 |
|
{ |
162 |
xvid_free(pEnc); |
xvid_free(pEnc); |
163 |
|
} |
164 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
165 |
} |
} |
166 |
|
|
174 |
|
|
175 |
if (pParam->bitrate) |
if (pParam->bitrate) |
176 |
{ |
{ |
177 |
RateControlInit(pParam->bitrate, pParam->rc_buffersize, pParam->fbase, pParam->width, |
RateControlInit(pParam->bitrate, pParam->rc_buffersize, pParam->fbase * 1000 / pParam->fincr, |
178 |
pParam->height, pParam->max_quantizer, pParam->min_quantizer); |
pParam->max_quantizer, pParam->min_quantizer); |
179 |
} |
} |
180 |
|
|
|
create_vlc_tables(); |
|
181 |
init_timer(); |
init_timer(); |
182 |
|
|
183 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
195 |
image_destroy(&pEnc->sReference, 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); |
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); |
201 |
|
#ifdef _DEBUG |
202 |
|
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height); |
203 |
|
#endif |
204 |
xvid_free(pEnc); |
xvid_free(pEnc); |
205 |
|
|
|
destroy_vlc_tables(); |
|
|
|
|
206 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
207 |
} |
} |
208 |
|
|
212 |
Bitstream bs; |
Bitstream bs; |
213 |
uint32_t bits; |
uint32_t bits; |
214 |
uint16_t write_vol_header = 0; |
uint16_t write_vol_header = 0; |
215 |
|
#ifdef _DEBUG |
216 |
|
float psnr; |
217 |
|
uint8_t temp[100]; |
218 |
|
#endif |
219 |
|
|
220 |
start_global_timer(); |
start_global_timer(); |
221 |
|
|
226 |
|
|
227 |
pEnc->mbParam.global_flags = pFrame->general; |
pEnc->mbParam.global_flags = pFrame->general; |
228 |
pEnc->mbParam.motion_flags = pFrame->motion; |
pEnc->mbParam.motion_flags = pFrame->motion; |
229 |
|
pEnc->mbParam.hint = &pFrame->hint; |
230 |
|
|
231 |
start_timer(); |
start_timer(); |
232 |
if (image_input(&pEnc->sCurrent, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width, |
if (image_input(&pEnc->sCurrent, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.edged_width, |
238 |
|
|
239 |
EMMS(); |
EMMS(); |
240 |
|
|
241 |
|
#ifdef _DEBUG |
242 |
|
image_copy(&pEnc->sOriginal, &pEnc->sCurrent, pEnc->mbParam.edged_width, pEnc->mbParam.height); |
243 |
|
#endif |
244 |
|
|
245 |
BitstreamInit(&bs, pFrame->bitstream, 0); |
BitstreamInit(&bs, pFrame->bitstream, 0); |
246 |
|
|
247 |
if (pFrame->quant == 0) |
if (pFrame->quant == 0) |
255 |
|
|
256 |
if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) |
if ((pEnc->mbParam.global_flags & XVID_LUMIMASKING) > 0) |
257 |
{ |
{ |
258 |
int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), 16); |
int * temp_dquants = (int *) xvid_malloc(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * sizeof(int), CACHE_LINE); |
259 |
|
|
260 |
pEnc->mbParam.quant = adaptive_quantization(pEnc->sCurrent.y, pEnc->mbParam.width, |
pEnc->mbParam.quant = adaptive_quantization(pEnc->sCurrent.y, |
261 |
temp_dquants, pFrame->quant, pFrame->quant, |
pEnc->mbParam.width, |
262 |
2*pFrame->quant, pEnc->mbParam.mb_width, pEnc->mbParam.mb_height); |
temp_dquants, |
263 |
|
pEnc->mbParam.quant, |
264 |
|
pEnc->mbParam.quant, |
265 |
|
2*pEnc->mbParam.quant, |
266 |
|
pEnc->mbParam.mb_width, |
267 |
|
pEnc->mbParam.mb_height); |
268 |
|
|
269 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
270 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
283 |
else if(pEnc->mbParam.global_flags & XVID_MPEGQUANT) { |
else if(pEnc->mbParam.global_flags & XVID_MPEGQUANT) { |
284 |
int ret1, ret2; |
int ret1, ret2; |
285 |
|
|
286 |
|
ret1 = ret2 = 0; |
287 |
|
|
288 |
if(pEnc->mbParam.quant_type != MPEG4_QUANT) |
if(pEnc->mbParam.quant_type != MPEG4_QUANT) |
289 |
write_vol_header = 1; |
write_vol_header = 1; |
290 |
|
|
342 |
RateControlUpdate(pEnc->mbParam.quant, pFrame->length, pFrame->intra); |
RateControlUpdate(pEnc->mbParam.quant, pFrame->length, pFrame->intra); |
343 |
} |
} |
344 |
|
|
345 |
|
#ifdef _DEBUG |
346 |
|
psnr = image_psnr(&pEnc->sOriginal, &pEnc->sCurrent, pEnc->mbParam.edged_width, |
347 |
|
pEnc->mbParam.width, pEnc->mbParam.height); |
348 |
|
|
349 |
|
sprintf(temp, "PSNR: %f\n", psnr); |
350 |
|
DEBUG(temp); |
351 |
|
#endif |
352 |
|
|
353 |
pEnc->iFrameNum++; |
pEnc->iFrameNum++; |
354 |
image_swap(&pEnc->sCurrent, &pEnc->sReference); |
image_swap(&pEnc->sCurrent, &pEnc->sReference); |
355 |
|
|
379 |
} |
} |
380 |
|
|
381 |
|
|
382 |
|
#define FCODEBITS 3 |
383 |
|
#define MODEBITS 5 |
384 |
|
|
385 |
|
void HintedMESet(Encoder * pEnc, int * intra) |
386 |
|
{ |
387 |
|
HINTINFO * hint; |
388 |
|
Bitstream bs; |
389 |
|
int length, high; |
390 |
|
uint32_t x, y; |
391 |
|
|
392 |
|
hint = pEnc->mbParam.hint; |
393 |
|
|
394 |
|
if (hint->rawhints) |
395 |
|
{ |
396 |
|
*intra = hint->mvhint.intra; |
397 |
|
} |
398 |
|
else |
399 |
|
{ |
400 |
|
BitstreamInit(&bs, hint->hintstream, hint->hintlength); |
401 |
|
*intra = BitstreamGetBit(&bs); |
402 |
|
} |
403 |
|
|
404 |
|
if (*intra) |
405 |
|
{ |
406 |
|
return; |
407 |
|
} |
408 |
|
|
409 |
|
pEnc->mbParam.fixed_code = (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS); |
410 |
|
|
411 |
|
length = pEnc->mbParam.fixed_code + 5; |
412 |
|
high = 1 << (length - 1); |
413 |
|
|
414 |
|
for (y=0 ; y<pEnc->mbParam.mb_height ; ++y) |
415 |
|
{ |
416 |
|
for (x=0 ; x<pEnc->mbParam.mb_width ; ++x) |
417 |
|
{ |
418 |
|
MACROBLOCK * pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width]; |
419 |
|
MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
420 |
|
VECTOR pred[4]; |
421 |
|
VECTOR tmp; |
422 |
|
int dummy[4]; |
423 |
|
int vec; |
424 |
|
|
425 |
|
pMB->mode = (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, MODEBITS); |
426 |
|
|
427 |
|
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) |
428 |
|
{ |
429 |
|
tmp.x = (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, length); |
430 |
|
tmp.y = (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, length); |
431 |
|
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
432 |
|
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
433 |
|
|
434 |
|
get_pmvdata(pEnc->pMBs, x, y, pEnc->mbParam.mb_width, 0, pred, dummy); |
435 |
|
|
436 |
|
for (vec=0 ; vec<4 ; ++vec) |
437 |
|
{ |
438 |
|
pMB->mvs[vec].x = tmp.x; |
439 |
|
pMB->mvs[vec].y = tmp.y; |
440 |
|
pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x; |
441 |
|
pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y; |
442 |
|
} |
443 |
|
} |
444 |
|
else if (pMB->mode == MODE_INTER4V) |
445 |
|
{ |
446 |
|
for (vec=0 ; vec<4 ; ++vec) |
447 |
|
{ |
448 |
|
tmp.x = (hint->rawhints) ? bhint->mvs[vec].x : BitstreamGetBits(&bs, length); |
449 |
|
tmp.y = (hint->rawhints) ? bhint->mvs[vec].y : BitstreamGetBits(&bs, length); |
450 |
|
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
451 |
|
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
452 |
|
|
453 |
|
get_pmvdata(pEnc->pMBs, x, y, pEnc->mbParam.mb_width, vec, pred, dummy); |
454 |
|
|
455 |
|
pMB->mvs[vec].x = tmp.x; |
456 |
|
pMB->mvs[vec].y = tmp.y; |
457 |
|
pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x; |
458 |
|
pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y; |
459 |
|
} |
460 |
|
} |
461 |
|
else // intra / intra_q / stuffing / not_coded |
462 |
|
{ |
463 |
|
for (vec=0 ; vec<4 ; ++vec) |
464 |
|
{ |
465 |
|
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
466 |
|
} |
467 |
|
} |
468 |
|
} |
469 |
|
} |
470 |
|
} |
471 |
|
|
472 |
|
|
473 |
|
void HintedMEGet(Encoder * pEnc, int intra) |
474 |
|
{ |
475 |
|
HINTINFO * hint; |
476 |
|
Bitstream bs; |
477 |
|
uint32_t x, y; |
478 |
|
int length, high; |
479 |
|
|
480 |
|
hint = pEnc->mbParam.hint; |
481 |
|
|
482 |
|
if (hint->rawhints) |
483 |
|
{ |
484 |
|
hint->mvhint.intra = intra; |
485 |
|
} |
486 |
|
else |
487 |
|
{ |
488 |
|
BitstreamInit(&bs, hint->hintstream, 0); |
489 |
|
BitstreamPutBit(&bs, intra); |
490 |
|
} |
491 |
|
|
492 |
|
if (intra) |
493 |
|
{ |
494 |
|
if (!hint->rawhints) |
495 |
|
{ |
496 |
|
BitstreamPad(&bs); |
497 |
|
hint->hintlength = BitstreamLength(&bs); |
498 |
|
} |
499 |
|
return; |
500 |
|
} |
501 |
|
|
502 |
|
length = pEnc->mbParam.fixed_code + 5; |
503 |
|
high = 1 << (length - 1); |
504 |
|
|
505 |
|
if (hint->rawhints) |
506 |
|
{ |
507 |
|
hint->mvhint.fcode = pEnc->mbParam.fixed_code; |
508 |
|
} |
509 |
|
else |
510 |
|
{ |
511 |
|
BitstreamPutBits(&bs, pEnc->mbParam.fixed_code, FCODEBITS); |
512 |
|
} |
513 |
|
|
514 |
|
for (y=0 ; y<pEnc->mbParam.mb_height ; ++y) |
515 |
|
{ |
516 |
|
for (x=0 ; x<pEnc->mbParam.mb_width ; ++x) |
517 |
|
{ |
518 |
|
MACROBLOCK * pMB = &pEnc->pMBs[x + y * pEnc->mbParam.mb_width]; |
519 |
|
MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
520 |
|
VECTOR tmp; |
521 |
|
|
522 |
|
if (hint->rawhints) |
523 |
|
{ |
524 |
|
bhint->mode = pMB->mode; |
525 |
|
} |
526 |
|
else |
527 |
|
{ |
528 |
|
BitstreamPutBits(&bs, pMB->mode, MODEBITS); |
529 |
|
} |
530 |
|
|
531 |
|
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) |
532 |
|
{ |
533 |
|
tmp.x = pMB->mvs[0].x; |
534 |
|
tmp.y = pMB->mvs[0].y; |
535 |
|
tmp.x += (tmp.x < 0) ? high*2 : 0; |
536 |
|
tmp.y += (tmp.y < 0) ? high*2 : 0; |
537 |
|
|
538 |
|
if (hint->rawhints) |
539 |
|
{ |
540 |
|
bhint->mvs[0].x = tmp.x; |
541 |
|
bhint->mvs[0].y = tmp.y; |
542 |
|
} |
543 |
|
else |
544 |
|
{ |
545 |
|
BitstreamPutBits(&bs, tmp.x, length); |
546 |
|
BitstreamPutBits(&bs, tmp.y, length); |
547 |
|
} |
548 |
|
} |
549 |
|
else if (pMB->mode == MODE_INTER4V) |
550 |
|
{ |
551 |
|
int vec; |
552 |
|
|
553 |
|
for (vec=0 ; vec<4 ; ++vec) |
554 |
|
{ |
555 |
|
tmp.x = pMB->mvs[vec].x; |
556 |
|
tmp.y = pMB->mvs[vec].y; |
557 |
|
tmp.x += (tmp.x < 0) ? high*2 : 0; |
558 |
|
tmp.y += (tmp.y < 0) ? high*2 : 0; |
559 |
|
|
560 |
|
if (hint->rawhints) |
561 |
|
{ |
562 |
|
bhint->mvs[vec].x = tmp.x; |
563 |
|
bhint->mvs[vec].y = tmp.y; |
564 |
|
} |
565 |
|
else |
566 |
|
{ |
567 |
|
BitstreamPutBits(&bs, tmp.x, length); |
568 |
|
BitstreamPutBits(&bs, tmp.y, length); |
569 |
|
} |
570 |
|
} |
571 |
|
} |
572 |
|
} |
573 |
|
} |
574 |
|
|
575 |
|
if (!hint->rawhints) |
576 |
|
{ |
577 |
|
BitstreamPad(&bs); |
578 |
|
hint->hintlength = BitstreamLength(&bs); |
579 |
|
} |
580 |
|
} |
581 |
|
|
582 |
|
|
583 |
static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits) |
static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits) |
584 |
{ |
{ |
585 |
int16_t dct_codes[6][64]; |
|
586 |
int16_t qcoeff[6][64]; |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
587 |
|
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
588 |
|
|
589 |
uint16_t x, y; |
uint16_t x, y; |
590 |
|
|
591 |
pEnc->iFrameNum = 0; |
pEnc->iFrameNum = 0; |
592 |
pEnc->mbParam.rounding_type = 1; |
pEnc->mbParam.rounding_type = 1; |
593 |
pEnc->mbParam.coding_type = I_VOP; |
pEnc->mbParam.coding_type = I_VOP; |
594 |
|
|
595 |
BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
596 |
BitstreamWriteVopHeader(bs, I_VOP, pEnc->mbParam.rounding_type, |
BitstreamWriteVopHeader(bs, &pEnc->mbParam); |
|
pEnc->mbParam.quant, |
|
|
pEnc->mbParam.fixed_code); |
|
597 |
|
|
598 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
599 |
|
|
608 |
|
|
609 |
CodeIntraMB(pEnc, pMB); |
CodeIntraMB(pEnc, pMB); |
610 |
|
|
611 |
MBTransQuantIntra(&pEnc->mbParam, x, y, dct_codes, qcoeff, &pEnc->sCurrent); |
MBTransQuantIntra(&pEnc->mbParam, pMB, x, y, dct_codes, qcoeff, &pEnc->sCurrent); |
612 |
|
|
613 |
start_timer(); |
start_timer(); |
614 |
MBPrediction(&pEnc->mbParam, x, y, pEnc->mbParam.mb_width, qcoeff, pEnc->pMBs); |
MBPrediction(&pEnc->mbParam, x, y, pEnc->mbParam.mb_width, qcoeff, pEnc->pMBs); |
627 |
pEnc->sStat.iMvCount = 0; |
pEnc->sStat.iMvCount = 0; |
628 |
pEnc->mbParam.fixed_code = 2; |
pEnc->mbParam.fixed_code = 2; |
629 |
|
|
630 |
|
if (pEnc->mbParam.global_flags & XVID_HINTEDME_GET) |
631 |
|
{ |
632 |
|
HintedMEGet(pEnc, 1); |
633 |
|
} |
634 |
|
|
635 |
return 1; // intra |
return 1; // intra |
636 |
} |
} |
637 |
|
|
641 |
static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header) |
static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header) |
642 |
{ |
{ |
643 |
float fSigma; |
float fSigma; |
644 |
int16_t dct_codes[6][64]; |
|
645 |
int16_t qcoeff[6][64]; |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
646 |
|
DECLARE_ALIGNED_MATRIX(qcoeff, 6, 64, int16_t, CACHE_LINE); |
647 |
|
|
648 |
int iLimit; |
int iLimit; |
649 |
uint32_t x, y; |
uint32_t x, y; |
650 |
int iSearchRange; |
int iSearchRange; |
653 |
IMAGE *pCurrent = &pEnc->sCurrent; |
IMAGE *pCurrent = &pEnc->sCurrent; |
654 |
IMAGE *pRef = &pEnc->sReference; |
IMAGE *pRef = &pEnc->sReference; |
655 |
|
|
656 |
image_setedges(pRef,pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height); |
start_timer(); |
657 |
|
image_setedges(pRef, |
658 |
|
pEnc->mbParam.edged_width, |
659 |
|
pEnc->mbParam.edged_height, |
660 |
|
pEnc->mbParam.width, |
661 |
|
pEnc->mbParam.height, |
662 |
|
pEnc->mbParam.global_flags & XVID_INTERLACING); |
663 |
|
stop_edges_timer(); |
664 |
|
|
665 |
pEnc->mbParam.rounding_type = 1 - pEnc->mbParam.rounding_type; |
pEnc->mbParam.rounding_type = 1 - pEnc->mbParam.rounding_type; |
666 |
|
|
678 |
} |
} |
679 |
|
|
680 |
start_timer(); |
start_timer(); |
681 |
|
if (pEnc->mbParam.global_flags & XVID_HINTEDME_SET) |
682 |
|
{ |
683 |
|
HintedMESet(pEnc, &bIntra); |
684 |
|
} |
685 |
|
else |
686 |
|
{ |
687 |
bIntra = MotionEstimation(pEnc->pMBs, &pEnc->mbParam, &pEnc->sReference, |
bIntra = MotionEstimation(pEnc->pMBs, &pEnc->mbParam, &pEnc->sReference, |
688 |
&pEnc->vInterH, &pEnc->vInterV, |
&pEnc->vInterH, &pEnc->vInterV, |
689 |
&pEnc->vInterHV, &pEnc->sCurrent, iLimit); |
&pEnc->vInterHV, &pEnc->sCurrent, iLimit); |
690 |
|
} |
691 |
stop_motion_timer(); |
stop_motion_timer(); |
692 |
|
|
693 |
if (bIntra == 1) |
if (bIntra == 1) |
694 |
|
{ |
695 |
return FrameCodeI(pEnc, bs, pBits); |
return FrameCodeI(pEnc, bs, pBits); |
696 |
|
} |
697 |
|
|
698 |
pEnc->mbParam.coding_type = P_VOP; |
pEnc->mbParam.coding_type = P_VOP; |
699 |
|
|
700 |
if(vol_header) |
if(vol_header) |
701 |
BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type); |
BitstreamWriteVolHeader(bs, &pEnc->mbParam); |
702 |
|
|
703 |
BitstreamWriteVopHeader(bs, P_VOP, pEnc->mbParam.rounding_type, |
BitstreamWriteVopHeader(bs, &pEnc->mbParam); |
|
pEnc->mbParam.quant, |
|
|
pEnc->mbParam.fixed_code); |
|
704 |
|
|
705 |
*pBits = BitstreamPos(bs); |
*pBits = BitstreamPos(bs); |
706 |
|
|
720 |
if (!bIntra) |
if (!bIntra) |
721 |
{ |
{ |
722 |
start_timer(); |
start_timer(); |
723 |
MBMotionCompensation(pMB, x, y, &pEnc->sReference, |
MBMotionCompensation(pMB, |
724 |
&pEnc->vInterH, &pEnc->vInterV, |
x, y, |
725 |
&pEnc->vInterHV, &pEnc->sCurrent, dct_codes, |
&pEnc->sReference, |
726 |
|
&pEnc->vInterH, |
727 |
|
&pEnc->vInterV, |
728 |
|
&pEnc->vInterHV, |
729 |
|
&pEnc->sCurrent, |
730 |
|
dct_codes, |
731 |
pEnc->mbParam.width, |
pEnc->mbParam.width, |
732 |
pEnc->mbParam.height, |
pEnc->mbParam.height, |
733 |
pEnc->mbParam.edged_width, |
pEnc->mbParam.edged_width, |
744 |
} |
} |
745 |
pMB->quant = pEnc->mbParam.quant; |
pMB->quant = pEnc->mbParam.quant; |
746 |
|
|
747 |
pMB->cbp = MBTransQuantInter(&pEnc->mbParam, x, y, dct_codes, qcoeff, pCurrent); |
pMB->field_pred = 0; |
748 |
|
|
749 |
|
pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pMB, x, y, dct_codes, qcoeff, pCurrent); |
750 |
} |
} |
751 |
else |
else |
752 |
{ |
{ |
753 |
CodeIntraMB(pEnc, pMB); |
CodeIntraMB(pEnc, pMB); |
754 |
MBTransQuantIntra(&pEnc->mbParam, x, y, dct_codes, qcoeff, pCurrent); |
MBTransQuantIntra(&pEnc->mbParam, pMB, x, y, dct_codes, qcoeff, pCurrent); |
755 |
} |
} |
756 |
|
|
757 |
start_timer(); |
start_timer(); |
783 |
|
|
784 |
emms(); |
emms(); |
785 |
|
|
786 |
|
if (pEnc->mbParam.global_flags & XVID_HINTEDME_GET) |
787 |
|
{ |
788 |
|
HintedMEGet(pEnc, 0); |
789 |
|
} |
790 |
|
|
791 |
if (pEnc->sStat.iMvCount == 0) |
if (pEnc->sStat.iMvCount == 0) |
792 |
pEnc->sStat.iMvCount = 1; |
pEnc->sStat.iMvCount = 1; |
793 |
|
|
816 |
|
|
817 |
return 0; // inter |
return 0; // inter |
818 |
} |
} |
819 |
|
|
820 |
|
|
821 |
|
|
822 |
|
/* |
823 |
|
static void FrameCodeB(Encoder * pEnc, FRAMEINFO * frame, Bitstream * bs, uint32_t *pBits) |
824 |
|
{ |
825 |
|
int16_t dct_codes[6][64]; |
826 |
|
int16_t qcoeff[6][64]; |
827 |
|
uint32_t x, y; |
828 |
|
VECTOR forward; |
829 |
|
VECTOR backward; |
830 |
|
|
831 |
|
IMAGE *f_ref = &pEnc->reference->image; |
832 |
|
IMAGE *b_ref = &pEnc->current->image; |
833 |
|
|
834 |
|
// forward |
835 |
|
image_setedges(f_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height); |
836 |
|
start_timer(); |
837 |
|
image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
838 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0); |
839 |
|
stop_inter_timer(); |
840 |
|
|
841 |
|
// backward |
842 |
|
image_setedges(b_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height); |
843 |
|
start_timer(); |
844 |
|
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
845 |
|
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0); |
846 |
|
stop_inter_timer(); |
847 |
|
|
848 |
|
start_timer(); |
849 |
|
MotionEstimationBVOP(&pEnc->mbParam, frame, |
850 |
|
pEnc->reference->mbs, f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
851 |
|
pEnc->current->mbs, b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); |
852 |
|
|
853 |
|
stop_motion_timer(); |
854 |
|
|
855 |
|
if (test_quant_type(&pEnc->mbParam, pEnc->current)) |
856 |
|
{ |
857 |
|
BitstreamWriteVolHeader(bs, pEnc->mbParam.width, pEnc->mbParam.height, pEnc->mbParam.quant_type); |
858 |
|
} |
859 |
|
|
860 |
|
frame->coding_type = B_VOP; |
861 |
|
BitstreamWriteVopHeader(bs, B_VOP, frame->tick, 0, |
862 |
|
frame->quant, frame->fcode, frame->bcode); |
863 |
|
|
864 |
|
*pBits = BitstreamPos(bs); |
865 |
|
|
866 |
|
pEnc->sStat.iTextBits = 0; |
867 |
|
pEnc->sStat.iMvSum = 0; |
868 |
|
pEnc->sStat.iMvCount = 0; |
869 |
|
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
870 |
|
|
871 |
|
|
872 |
|
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
873 |
|
{ |
874 |
|
// reset prediction |
875 |
|
|
876 |
|
forward.x = 0; |
877 |
|
forward.y = 0; |
878 |
|
backward.x = 0; |
879 |
|
backward.y = 0; |
880 |
|
|
881 |
|
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
882 |
|
{ |
883 |
|
MACROBLOCK * f_mb = &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width]; |
884 |
|
MACROBLOCK * b_mb = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
885 |
|
MACROBLOCK * mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
886 |
|
|
887 |
|
// decoder ignores mb when refence block is INTER(0,0), CBP=0 |
888 |
|
if (mb->mode == MODE_NOT_CODED) |
889 |
|
{ |
890 |
|
mb->mvs[0].x = 0; |
891 |
|
mb->mvs[0].y = 0; |
892 |
|
continue; |
893 |
|
} |
894 |
|
|
895 |
|
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
896 |
|
f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
897 |
|
b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
898 |
|
dct_codes); |
899 |
|
|
900 |
|
mb->quant = frame->quant; |
901 |
|
mb->cbp = MBTransQuantInter(&pEnc->mbParam, frame, x, y, dct_codes, qcoeff); |
902 |
|
//mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant); |
903 |
|
|
904 |
|
|
905 |
|
if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) && |
906 |
|
mb->cbp == 0 && |
907 |
|
mb->mvs[0].x == 0 && |
908 |
|
mb->mvs[0].y == 0) |
909 |
|
{ |
910 |
|
mb->mode = 5; // skipped |
911 |
|
} |
912 |
|
|
913 |
|
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) |
914 |
|
{ |
915 |
|
mb->pmvs[0].x = mb->mvs[0].x - forward.x; |
916 |
|
mb->pmvs[0].y = mb->mvs[0].y - forward.y; |
917 |
|
forward.x = mb->mvs[0].x; |
918 |
|
forward.y = mb->mvs[0].y; |
919 |
|
} |
920 |
|
|
921 |
|
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) |
922 |
|
{ |
923 |
|
mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x; |
924 |
|
mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y; |
925 |
|
backward.x = mb->b_mvs[0].x; |
926 |
|
backward.y = mb->b_mvs[0].y; |
927 |
|
} |
928 |
|
|
929 |
|
// 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); |
930 |
|
|
931 |
|
start_timer(); |
932 |
|
MBCodingBVOP(frame, mb, qcoeff, bs, &pEnc->sStat); |
933 |
|
stop_coding_timer(); |
934 |
|
} |
935 |
|
} |
936 |
|
|
937 |
|
emms(); |
938 |
|
|
939 |
|
// TODO: dynamic fcode/bcode ??? |
940 |
|
|
941 |
|
*pBits = BitstreamPos(bs) - *pBits; |
942 |
|
|
943 |
|
} |
944 |
|
|
945 |
|
*/ |