43 |
#include <stdlib.h> |
#include <stdlib.h> |
44 |
#include <stdio.h> |
#include <stdio.h> |
45 |
#include <math.h> |
#include <math.h> |
46 |
|
#include <string.h> |
47 |
|
|
48 |
#include "encoder.h" |
#include "encoder.h" |
49 |
#include "prediction/mbprediction.h" |
#include "prediction/mbprediction.h" |
94 |
* Local data |
* Local data |
95 |
****************************************************************************/ |
****************************************************************************/ |
96 |
|
|
97 |
static int DQtab[4] = |
static int DQtab[4] = { |
|
{ |
|
98 |
-1, -2, 1, 2 |
-1, -2, 1, 2 |
99 |
}; |
}; |
100 |
|
|
101 |
static int iDQtab[5] = |
static int iDQtab[5] = { |
|
{ |
|
102 |
1, 0, NO_CHANGE, 2, 3 |
1, 0, NO_CHANGE, 2, 3 |
103 |
}; |
}; |
104 |
|
|
105 |
|
|
106 |
static void __inline image_null(IMAGE * image) |
static void __inline |
107 |
|
image_null(IMAGE * image) |
108 |
{ |
{ |
109 |
image->y = image->u = image->v = NULL; |
image->y = image->u = image->v = NULL; |
110 |
} |
} |
145 |
|
|
146 |
/* Fps */ |
/* Fps */ |
147 |
|
|
148 |
if (pParam->fincr <= 0 || pParam->fbase <= 0) |
if (pParam->fincr <= 0 || pParam->fbase <= 0) { |
|
{ |
|
149 |
pParam->fincr = 1; |
pParam->fincr = 1; |
150 |
pParam->fbase = 25; |
pParam->fbase = 25; |
151 |
} |
} |
156 |
*/ |
*/ |
157 |
|
|
158 |
i = pParam->fincr; |
i = pParam->fincr; |
159 |
while (i > 1) |
while (i > 1) { |
160 |
{ |
if (pParam->fincr % i == 0 && pParam->fbase % i == 0) { |
|
if (pParam->fincr % i == 0 && pParam->fbase % i == 0) |
|
|
{ |
|
161 |
pParam->fincr /= i; |
pParam->fincr /= i; |
162 |
pParam->fbase /= i; |
pParam->fbase /= i; |
163 |
i = pParam->fincr; |
i = pParam->fincr; |
166 |
i--; |
i--; |
167 |
} |
} |
168 |
|
|
169 |
if (pParam->fbase > 65535) |
if (pParam->fbase > 65535) { |
|
{ |
|
170 |
float div = (float)pParam->fbase / 65535; |
float div = (float)pParam->fbase / 65535; |
171 |
|
|
172 |
pParam->fbase = (int)(pParam->fbase / div); |
pParam->fbase = (int)(pParam->fbase / div); |
173 |
pParam->fincr = (int)(pParam->fincr / div); |
pParam->fincr = (int)(pParam->fincr / div); |
174 |
} |
} |
208 |
if (pEnc == NULL) |
if (pEnc == NULL) |
209 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
210 |
|
|
211 |
|
/* Zero the Encoder Structure */ |
212 |
|
|
213 |
|
memset(pEnc, 0, sizeof(Encoder)); |
214 |
|
|
215 |
/* Fill members of Encoder structure */ |
/* Fill members of Encoder structure */ |
216 |
|
|
217 |
pEnc->mbParam.width = pParam->width; |
pEnc->mbParam.width = pParam->width; |
245 |
|
|
246 |
/* try to allocate mb memory */ |
/* try to allocate mb memory */ |
247 |
|
|
248 |
pEnc->current->mbs = xvid_malloc(sizeof(MACROBLOCK) * \ |
pEnc->current->mbs = |
249 |
pEnc->mbParam.mb_width * \ |
xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * |
250 |
pEnc->mbParam.mb_height, |
pEnc->mbParam.mb_height, CACHE_LINE); |
251 |
CACHE_LINE); |
pEnc->reference->mbs = |
252 |
pEnc->reference->mbs = xvid_malloc(sizeof(MACROBLOCK) * \ |
xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * |
253 |
pEnc->mbParam.mb_width * \ |
pEnc->mbParam.mb_height, CACHE_LINE); |
|
pEnc->mbParam.mb_height, |
|
|
CACHE_LINE); |
|
254 |
|
|
255 |
if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL) |
if (pEnc->current->mbs == NULL || pEnc->reference->mbs == NULL) |
256 |
goto xvid_err_memory2; |
goto xvid_err_memory2; |
274 |
image_null(&pEnc->vInterHVf); |
image_null(&pEnc->vInterHVf); |
275 |
|
|
276 |
#ifdef _DEBUG |
#ifdef _DEBUG |
277 |
if (image_create(&pEnc->sOriginal, |
if (image_create |
278 |
pEnc->mbParam.edged_width, |
(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
279 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
280 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
281 |
#endif |
#endif |
282 |
#ifdef BFRAMES |
#ifdef BFRAMES |
283 |
if (image_create(&pEnc->f_refh, |
if (image_create |
284 |
pEnc->mbParam.edged_width, |
(&pEnc->f_refh, pEnc->mbParam.edged_width, |
285 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
286 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
287 |
if (image_create(&pEnc->f_refv, |
if (image_create |
288 |
pEnc->mbParam.edged_width, |
(&pEnc->f_refv, pEnc->mbParam.edged_width, |
289 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
290 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
291 |
if (image_create(&pEnc->f_refhv, |
if (image_create |
292 |
pEnc->mbParam.edged_width, |
(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
293 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
294 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
295 |
#endif |
#endif |
296 |
if (image_create(&pEnc->current->image, |
if (image_create |
297 |
pEnc->mbParam.edged_width, |
(&pEnc->current->image, pEnc->mbParam.edged_width, |
298 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
299 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
300 |
if (image_create(&pEnc->reference->image, |
if (image_create |
301 |
pEnc->mbParam.edged_width, |
(&pEnc->reference->image, pEnc->mbParam.edged_width, |
302 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
303 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
304 |
if (image_create(&pEnc->vInterH, |
if (image_create |
305 |
pEnc->mbParam.edged_width, |
(&pEnc->vInterH, pEnc->mbParam.edged_width, |
306 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
307 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
308 |
if (image_create(&pEnc->vInterV, |
if (image_create |
309 |
pEnc->mbParam.edged_width, |
(&pEnc->vInterV, pEnc->mbParam.edged_width, |
310 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
311 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
312 |
if (image_create(&pEnc->vInterVf, |
if (image_create |
313 |
pEnc->mbParam.edged_width, |
(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
314 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
315 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
316 |
if (image_create(&pEnc->vInterHV, |
if (image_create |
317 |
pEnc->mbParam.edged_width, |
(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
318 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
319 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
320 |
if (image_create(&pEnc->vInterHVf, |
if (image_create |
321 |
pEnc->mbParam.edged_width, |
(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
322 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
323 |
goto xvid_err_memory3; |
goto xvid_err_memory3; |
324 |
|
|
331 |
pEnc->bquant_ratio = pParam->bquant_ratio; |
pEnc->bquant_ratio = pParam->bquant_ratio; |
332 |
pEnc->bframes = NULL; |
pEnc->bframes = NULL; |
333 |
|
|
334 |
if (pEnc->mbParam.max_bframes > 0) |
if (pEnc->mbParam.max_bframes > 0) { |
|
{ |
|
335 |
int n; |
int n; |
336 |
|
|
337 |
pEnc->bframes = xvid_malloc(pEnc->mbParam.max_bframes * \ |
pEnc->bframes = |
338 |
sizeof(FRAMEINFO *), |
xvid_malloc(pEnc->mbParam.max_bframes * sizeof(FRAMEINFO *), |
339 |
CACHE_LINE); |
CACHE_LINE); |
340 |
|
|
341 |
if (pEnc->bframes == NULL) |
if (pEnc->bframes == NULL) |
345 |
pEnc->bframes[n] = NULL; |
pEnc->bframes[n] = NULL; |
346 |
|
|
347 |
|
|
348 |
for (n = 0; n < pEnc->mbParam.max_bframes; n++) |
for (n = 0; n < pEnc->mbParam.max_bframes; n++) { |
349 |
{ |
pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO), CACHE_LINE); |
|
pEnc->bframes[n] = xvid_malloc(sizeof(FRAMEINFO), |
|
|
CACHE_LINE); |
|
350 |
|
|
351 |
if (pEnc->bframes[n] == NULL) |
if (pEnc->bframes[n] == NULL) |
352 |
goto xvid_err_memory4; |
goto xvid_err_memory4; |
353 |
|
|
354 |
pEnc->bframes[n]->mbs = xvid_malloc(sizeof(MACROBLOCK) * \ |
pEnc->bframes[n]->mbs = |
355 |
pEnc->mbParam.mb_width * \ |
xvid_malloc(sizeof(MACROBLOCK) * pEnc->mbParam.mb_width * |
356 |
pEnc->mbParam.mb_height, |
pEnc->mbParam.mb_height, CACHE_LINE); |
|
CACHE_LINE); |
|
357 |
|
|
358 |
if (pEnc->bframes[n]->mbs == NULL) |
if (pEnc->bframes[n]->mbs == NULL) |
359 |
goto xvid_err_memory4; |
goto xvid_err_memory4; |
360 |
|
|
361 |
image_null(&pEnc->bframes[n]->image); |
image_null(&pEnc->bframes[n]->image); |
362 |
|
|
363 |
if (image_create(&pEnc->bframes[n]->image, |
if (image_create |
364 |
pEnc->mbParam.edged_width, |
(&pEnc->bframes[n]->image, pEnc->mbParam.edged_width, |
365 |
pEnc->mbParam.edged_height) < 0) |
pEnc->mbParam.edged_height) < 0) |
366 |
goto xvid_err_memory4; |
goto xvid_err_memory4; |
367 |
|
|
378 |
|
|
379 |
pParam->handle = (void *)pEnc; |
pParam->handle = (void *)pEnc; |
380 |
|
|
381 |
if (pParam->rc_bitrate) |
if (pParam->rc_bitrate) { |
382 |
{ |
RateControlInit(&pEnc->rate_control, pParam->rc_bitrate, |
|
RateControlInit(&pEnc->rate_control, |
|
|
pParam->rc_bitrate, |
|
383 |
pParam->rc_reaction_delay_factor, |
pParam->rc_reaction_delay_factor, |
384 |
pParam->rc_averaging_period, |
pParam->rc_averaging_period, pParam->rc_buffer, |
|
pParam->rc_buffer, |
|
385 |
pParam->fbase * 1000 / pParam->fincr, |
pParam->fbase * 1000 / pParam->fincr, |
386 |
pParam->max_quantizer, |
pParam->max_quantizer, pParam->min_quantizer); |
|
pParam->min_quantizer); |
|
387 |
} |
} |
388 |
|
|
389 |
init_timer(); |
init_timer(); |
395 |
*/ |
*/ |
396 |
#ifdef BFRAMES |
#ifdef BFRAMES |
397 |
xvid_err_memory4: |
xvid_err_memory4: |
398 |
for (i=0; i<pEnc->mbParam.max_bframes; i++) |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
|
{ |
|
399 |
|
|
400 |
if (pEnc->bframes[i] == NULL) continue; |
if (pEnc->bframes[i] == NULL) |
401 |
|
continue; |
402 |
|
|
403 |
image_destroy(&pEnc->bframes[i]->image, |
image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, |
|
pEnc->mbParam.edged_width, |
|
404 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
405 |
|
|
406 |
xvid_free(pEnc->bframes[i]->mbs); |
xvid_free(pEnc->bframes[i]->mbs); |
415 |
|
|
416 |
xvid_err_memory3: |
xvid_err_memory3: |
417 |
#ifdef _DEBUG |
#ifdef _DEBUG |
418 |
image_destroy(&pEnc->sOriginal, |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
|
pEnc->mbParam.edged_width, |
|
419 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
420 |
#endif |
#endif |
421 |
|
|
422 |
#ifdef BFRAMES |
#ifdef BFRAMES |
423 |
image_destroy(&pEnc->f_refh, |
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
|
pEnc->mbParam.edged_width, |
|
424 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
425 |
image_destroy(&pEnc->f_refv, |
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
|
pEnc->mbParam.edged_width, |
|
426 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
427 |
image_destroy(&pEnc->f_refhv, |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
|
pEnc->mbParam.edged_width, |
|
428 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
429 |
#endif |
#endif |
430 |
|
|
431 |
image_destroy(&pEnc->current->image, |
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, |
|
pEnc->mbParam.edged_width, |
|
432 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
433 |
image_destroy(&pEnc->reference->image, |
image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, |
|
pEnc->mbParam.edged_width, |
|
434 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
435 |
image_destroy(&pEnc->vInterH, |
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, |
|
pEnc->mbParam.edged_width, |
|
436 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
437 |
image_destroy(&pEnc->vInterV, |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
|
pEnc->mbParam.edged_width, |
|
438 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
439 |
image_destroy(&pEnc->vInterVf, |
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
|
pEnc->mbParam.edged_width, |
|
440 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
441 |
image_destroy(&pEnc->vInterHV, |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
|
pEnc->mbParam.edged_width, |
|
442 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
443 |
image_destroy(&pEnc->vInterHVf, |
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
|
pEnc->mbParam.edged_width, |
|
444 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
445 |
|
|
446 |
xvid_err_memory2: |
xvid_err_memory2: |
477 |
#ifdef BFRAMES |
#ifdef BFRAMES |
478 |
int i; |
int i; |
479 |
|
|
480 |
for (i=0; i<pEnc->mbParam.max_bframes; i++) |
for (i = 0; i < pEnc->mbParam.max_bframes; i++) { |
|
{ |
|
481 |
|
|
482 |
if (pEnc->bframes[i] == NULL) continue; |
if (pEnc->bframes[i] == NULL) |
483 |
|
continue; |
484 |
|
|
485 |
image_destroy(&pEnc->bframes[i]->image, |
image_destroy(&pEnc->bframes[i]->image, pEnc->mbParam.edged_width, |
|
pEnc->mbParam.edged_width, |
|
486 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
487 |
|
|
488 |
xvid_free(pEnc->bframes[i]->mbs); |
xvid_free(pEnc->bframes[i]->mbs); |
497 |
|
|
498 |
/* All images, reference, current etc ... */ |
/* All images, reference, current etc ... */ |
499 |
|
|
500 |
image_destroy(&pEnc->current->image, |
image_destroy(&pEnc->current->image, pEnc->mbParam.edged_width, |
|
pEnc->mbParam.edged_width, |
|
501 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
502 |
image_destroy(&pEnc->reference->image, |
image_destroy(&pEnc->reference->image, pEnc->mbParam.edged_width, |
|
pEnc->mbParam.edged_width, |
|
503 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
504 |
image_destroy(&pEnc->vInterH, |
image_destroy(&pEnc->vInterH, pEnc->mbParam.edged_width, |
|
pEnc->mbParam.edged_width, |
|
505 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
506 |
image_destroy(&pEnc->vInterV, |
image_destroy(&pEnc->vInterV, pEnc->mbParam.edged_width, |
|
pEnc->mbParam.edged_width, |
|
507 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
508 |
image_destroy(&pEnc->vInterVf, |
image_destroy(&pEnc->vInterVf, pEnc->mbParam.edged_width, |
|
pEnc->mbParam.edged_width, |
|
509 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
510 |
image_destroy(&pEnc->vInterHV, |
image_destroy(&pEnc->vInterHV, pEnc->mbParam.edged_width, |
|
pEnc->mbParam.edged_width, |
|
511 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
512 |
image_destroy(&pEnc->vInterHVf, |
image_destroy(&pEnc->vInterHVf, pEnc->mbParam.edged_width, |
|
pEnc->mbParam.edged_width, |
|
513 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
514 |
#ifdef BFRAMES |
#ifdef BFRAMES |
515 |
image_destroy(&pEnc->f_refh, |
image_destroy(&pEnc->f_refh, pEnc->mbParam.edged_width, |
|
pEnc->mbParam.edged_width, |
|
516 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
517 |
image_destroy(&pEnc->f_refv, |
image_destroy(&pEnc->f_refv, pEnc->mbParam.edged_width, |
|
pEnc->mbParam.edged_width, |
|
518 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
519 |
image_destroy(&pEnc->f_refhv, |
image_destroy(&pEnc->f_refhv, pEnc->mbParam.edged_width, |
|
pEnc->mbParam.edged_width, |
|
520 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
521 |
#endif |
#endif |
522 |
#ifdef _DEBUG |
#ifdef _DEBUG |
523 |
image_destroy(&pEnc->sOriginal, |
image_destroy(&pEnc->sOriginal, pEnc->mbParam.edged_width, |
|
pEnc->mbParam.edged_width, |
|
524 |
pEnc->mbParam.edged_height); |
pEnc->mbParam.edged_height); |
525 |
#endif |
#endif |
526 |
|
|
579 |
* bframe "flush" code |
* bframe "flush" code |
580 |
*/ |
*/ |
581 |
|
|
582 |
if ( (pFrame->image == NULL || pEnc->flush_bframes) && |
if ((pFrame->image == NULL || pEnc->flush_bframes) |
583 |
(pEnc->bframenum_head < pEnc->bframenum_tail)) |
&& (pEnc->bframenum_head < pEnc->bframenum_tail)) { |
|
{ |
|
584 |
|
|
585 |
if (pEnc->flush_bframes == 0) |
if (pEnc->flush_bframes == 0) { |
|
{ |
|
586 |
/* |
/* |
587 |
* we have reached the end of stream without getting |
* we have reached the end of stream without getting |
588 |
* a future reference frame... so encode last final |
* a future reference frame... so encode last final |
596 |
pEnc->bframenum_tail--; |
pEnc->bframenum_tail--; |
597 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(pEnc->current, pEnc->reference); |
598 |
|
|
599 |
SWAP(pEnc->current, |
SWAP(pEnc->current, pEnc->bframes[pEnc->bframenum_tail]); |
|
pEnc->bframes[pEnc->bframenum_tail]); |
|
600 |
|
|
601 |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
602 |
|
|
612 |
/* |
/* |
613 |
dprintf("--- BFRAME (flush) --- "); |
dprintf("--- BFRAME (flush) --- "); |
614 |
*/ |
*/ |
615 |
FrameCodeB(pEnc, |
FrameCodeB(pEnc, pEnc->bframes[pEnc->bframenum_head], &bs, &bits); |
|
pEnc->bframes[pEnc->bframenum_head], |
|
|
&bs, |
|
|
&bits); |
|
616 |
pEnc->bframenum_head++; |
pEnc->bframenum_head++; |
617 |
|
|
618 |
|
|
624 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
625 |
} |
} |
626 |
|
|
627 |
if (pFrame->image == NULL) |
if (pFrame->image == NULL) { |
|
{ |
|
628 |
pFrame->length = 0; |
pFrame->length = 0; |
629 |
pFrame->input_consumed = 1; |
pFrame->input_consumed = 1; |
630 |
pFrame->intra = 0; |
pFrame->intra = 0; |
632 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
633 |
} |
} |
634 |
|
|
635 |
if (pEnc->bframenum_head > 0) |
if (pEnc->bframenum_head > 0) { |
|
{ |
|
636 |
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
pEnc->bframenum_head = pEnc->bframenum_tail = 0; |
637 |
} |
} |
638 |
|
|
645 |
|
|
646 |
SWAP(pEnc->current, pEnc->reference); |
SWAP(pEnc->current, pEnc->reference); |
647 |
|
|
648 |
EMMS(); |
emms(); |
649 |
|
|
650 |
if (pFrame->quant == 0) |
if (pFrame->quant == 0) |
651 |
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); |
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control, 0); |
667 |
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
pEnc->current->bcode = pEnc->mbParam.m_fcode; |
668 |
|
|
669 |
start_timer(); |
start_timer(); |
670 |
if (image_input(&pEnc->current->image, |
if (image_input |
671 |
pEnc->mbParam.width, |
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
672 |
pEnc->mbParam.height, |
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace)) |
|
pEnc->mbParam.edged_width, |
|
|
pFrame->image, |
|
|
pFrame->colorspace)) |
|
673 |
return XVID_ERR_FORMAT; |
return XVID_ERR_FORMAT; |
674 |
stop_conv_timer(); |
stop_conv_timer(); |
675 |
|
|
676 |
#ifdef _DEBUG |
#ifdef _DEBUG |
677 |
image_copy(&pEnc->sOriginal, |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
678 |
&pEnc->current->image, |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
|
pEnc->mbParam.edged_width, |
|
|
pEnc->mbParam.height); |
|
679 |
#endif |
#endif |
680 |
|
|
681 |
EMMS(); |
emms(); |
682 |
|
|
683 |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
/* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% |
684 |
* Luminance masking |
* Luminance masking |
685 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
686 |
|
|
687 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
|
{ |
|
688 |
int *temp_dquants = |
int *temp_dquants = |
689 |
(int *) xvid_malloc(pEnc->mbParam.mb_width * \ |
(int *) xvid_malloc(pEnc->mbParam.mb_width * |
690 |
pEnc->mbParam.mb_height * \ |
pEnc->mbParam.mb_height * sizeof(int), |
|
sizeof(int), |
|
691 |
CACHE_LINE); |
CACHE_LINE); |
692 |
|
|
693 |
pEnc->current->quant = |
pEnc->current->quant = |
694 |
adaptive_quantization(pEnc->current->image.y, |
adaptive_quantization(pEnc->current->image.y, |
695 |
pEnc->mbParam.edged_width, |
pEnc->mbParam.edged_width, temp_dquants, |
696 |
temp_dquants, |
pEnc->current->quant, pEnc->current->quant, |
|
pEnc->current->quant, |
|
|
pEnc->current->quant, |
|
697 |
2*pEnc->current->quant, |
2*pEnc->current->quant, |
698 |
pEnc->mbParam.mb_width, |
pEnc->mbParam.mb_width, |
699 |
pEnc->mbParam.mb_height); |
pEnc->mbParam.mb_height); |
700 |
|
|
701 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
|
{ |
|
702 |
|
|
703 |
#define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width) |
#define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width) |
704 |
|
|
705 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
706 |
{ |
MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)]; |
707 |
MACROBLOCK *pMB = |
|
708 |
&pEnc->current->mbs[OFFSET(x,y)]; |
pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2]; |
|
pMB->dquant = |
|
|
iDQtab[temp_dquants[OFFSET(x,y)] + 2]; |
|
709 |
} |
} |
710 |
|
|
711 |
#undef OFFSET |
#undef OFFSET |
720 |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
* %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% */ |
721 |
|
|
722 |
|
|
723 |
if (pEnc->iFrameNum == 0 || |
if (pEnc->iFrameNum == 0 || pFrame->intra == 1 || |
724 |
pFrame->intra == 1 || |
(pFrame->intra < 0 && pEnc->iMaxKeyInterval > 0 && |
725 |
|
pEnc->iFrameNum >= pEnc->iMaxKeyInterval) |
726 |
(pFrame->intra < 0 && |
|| image_mad(&pEnc->reference->image, &pEnc->current->image, |
727 |
pEnc->iMaxKeyInterval > 0 && |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
728 |
pEnc->iFrameNum >= pEnc->iMaxKeyInterval) || |
pEnc->mbParam.height) > 30) { |
|
|
|
|
image_mad(&pEnc->reference->image, |
|
|
&pEnc->current->image, |
|
|
pEnc->mbParam.edged_width, |
|
|
pEnc->mbParam.width, |
|
|
pEnc->mbParam.height) > 30) |
|
|
{ |
|
729 |
/* |
/* |
730 |
* This will be coded as an Intra Frame |
* This will be coded as an Intra Frame |
731 |
*/ |
*/ |
744 |
* NB : sequences like "IIBB" decode fine with msfdam but, |
* NB : sequences like "IIBB" decode fine with msfdam but, |
745 |
* go screwy with divx 5.00 |
* go screwy with divx 5.00 |
746 |
*/ |
*/ |
747 |
} |
} else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) { |
|
else if (pEnc->bframenum_tail >= pEnc->mbParam.max_bframes) |
|
|
{ |
|
748 |
/* |
/* |
749 |
* This will be coded as a Predicted Frame |
* This will be coded as a Predicted Frame |
750 |
*/ |
*/ |
757 |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
FrameCodeP(pEnc, &bs, &bits, 1, 0); |
758 |
pFrame->intra = 0; |
pFrame->intra = 0; |
759 |
pEnc->flush_bframes = 1; |
pEnc->flush_bframes = 1; |
760 |
} |
} else { |
|
else |
|
|
{ |
|
761 |
/* |
/* |
762 |
* This will be coded as a Bidirectional Frame |
* This will be coded as a Bidirectional Frame |
763 |
*/ |
*/ |
769 |
pEnc->bframenum_tail); |
pEnc->bframenum_tail); |
770 |
*/ |
*/ |
771 |
|
|
772 |
if (pFrame->bquant < 1) |
if (pFrame->bquant < 1) { |
|
{ |
|
773 |
pEnc->current->quant = |
pEnc->current->quant = |
774 |
((pEnc->reference->quant+pEnc->current->quant)*\ |
((pEnc->reference->quant + |
775 |
pEnc->bquant_ratio) / 200; |
pEnc->current->quant) * pEnc->bquant_ratio) / 200; |
776 |
} |
} else { |
|
else |
|
|
{ |
|
777 |
pEnc->current->quant = pFrame->bquant; |
pEnc->current->quant = pFrame->bquant; |
778 |
} |
} |
779 |
|
|
788 |
pFrame->input_consumed = 1; |
pFrame->input_consumed = 1; |
789 |
|
|
790 |
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
791 |
if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) |
if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) { |
|
{ |
|
792 |
pEnc->mbParam.m_seconds++; |
pEnc->mbParam.m_seconds++; |
793 |
pEnc->mbParam.m_ticks = 0; |
pEnc->mbParam.m_ticks = 0; |
794 |
} |
} |
799 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
800 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
801 |
|
|
802 |
if (pResult) |
if (pResult) { |
|
{ |
|
803 |
pResult->quant = pEnc->current->quant; |
pResult->quant = pEnc->current->quant; |
804 |
pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits/8); |
pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits/8); |
805 |
pResult->kblks = pEnc->sStat.kblks; |
pResult->kblks = pEnc->sStat.kblks; |
807 |
pResult->ublks = pEnc->sStat.ublks; |
pResult->ublks = pEnc->sStat.ublks; |
808 |
} |
} |
809 |
|
|
810 |
EMMS(); |
emms(); |
811 |
|
|
812 |
#ifdef _DEBUG |
#ifdef _DEBUG |
813 |
psnr = image_psnr(&pEnc->sOriginal, |
psnr = |
814 |
&pEnc->current->image, |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
815 |
pEnc->mbParam.edged_width, |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
|
pEnc->mbParam.width, |
|
816 |
pEnc->mbParam.height); |
pEnc->mbParam.height); |
817 |
|
|
818 |
snprintf(temp, 127, "PSNR: %f\n", psnr); |
snprintf(temp, 127, "PSNR: %f\n", psnr); |
819 |
DEBUG(temp); |
DEBUG(temp); |
820 |
#endif |
#endif |
821 |
|
|
822 |
if (pFrame->quant == 0) |
if (pFrame->quant == 0) { |
823 |
{ |
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
824 |
RateControlUpdate(&pEnc->rate_control, |
pFrame->length, pFrame->intra); |
|
pEnc->current->quant, |
|
|
pFrame->length, |
|
|
pFrame->intra); |
|
825 |
} |
} |
826 |
|
|
827 |
pEnc->iFrameNum++; |
pEnc->iFrameNum++; |
828 |
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
pEnc->mbParam.m_ticks += pEnc->mbParam.fincr; |
829 |
if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) |
if (pEnc->mbParam.m_ticks > pEnc->mbParam.fbase) { |
|
{ |
|
830 |
pEnc->mbParam.m_seconds++; |
pEnc->mbParam.m_seconds++; |
831 |
pEnc->mbParam.m_ticks = 0; |
pEnc->mbParam.m_ticks = 0; |
832 |
} |
} |
850 |
Bitstream bs; |
Bitstream bs; |
851 |
uint32_t bits; |
uint32_t bits; |
852 |
uint16_t write_vol_header = 0; |
uint16_t write_vol_header = 0; |
853 |
|
|
854 |
#ifdef _DEBUG |
#ifdef _DEBUG |
855 |
float psnr; |
float psnr; |
856 |
uint8_t temp[128]; |
uint8_t temp[128]; |
870 |
pEnc->mbParam.hint = &pFrame->hint; |
pEnc->mbParam.hint = &pFrame->hint; |
871 |
|
|
872 |
start_timer(); |
start_timer(); |
873 |
if (image_input(&pEnc->current->image, |
if (image_input |
874 |
pEnc->mbParam.width, |
(&pEnc->current->image, pEnc->mbParam.width, pEnc->mbParam.height, |
875 |
pEnc->mbParam.height, |
pEnc->mbParam.edged_width, pFrame->image, pFrame->colorspace) < 0) |
|
pEnc->mbParam.edged_width, |
|
|
pFrame->image, |
|
|
pFrame->colorspace) < 0) |
|
876 |
return XVID_ERR_FORMAT; |
return XVID_ERR_FORMAT; |
877 |
stop_conv_timer(); |
stop_conv_timer(); |
878 |
|
|
879 |
#ifdef _DEBUG |
#ifdef _DEBUG |
880 |
image_copy(&pEnc->sOriginal, |
image_copy(&pEnc->sOriginal, &pEnc->current->image, |
881 |
&pEnc->current->image, |
pEnc->mbParam.edged_width, pEnc->mbParam.height); |
|
pEnc->mbParam.edged_width, |
|
|
pEnc->mbParam.height); |
|
882 |
#endif |
#endif |
883 |
|
|
884 |
EMMS(); |
emms(); |
885 |
|
|
886 |
BitstreamInit(&bs, pFrame->bitstream, 0); |
BitstreamInit(&bs, pFrame->bitstream, 0); |
887 |
|
|
888 |
if (pFrame->quant == 0) |
if (pFrame->quant == 0) { |
|
{ |
|
889 |
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control,0); |
pEnc->current->quant = RateControlGetQ(&pEnc->rate_control,0); |
890 |
} |
} else { |
|
else |
|
|
{ |
|
891 |
pEnc->current->quant = pFrame->quant; |
pEnc->current->quant = pFrame->quant; |
892 |
} |
} |
893 |
|
|
894 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
895 |
{ |
int *temp_dquants = |
896 |
int * temp_dquants = (int *) |
(int *) xvid_malloc(pEnc->mbParam.mb_width * |
897 |
xvid_malloc(pEnc->mbParam.mb_width * \ |
pEnc->mbParam.mb_height * sizeof(int), |
|
pEnc->mbParam.mb_height * \ |
|
|
sizeof(int), |
|
898 |
CACHE_LINE); |
CACHE_LINE); |
899 |
|
|
900 |
pEnc->current->quant = |
pEnc->current->quant = |
901 |
adaptive_quantization(pEnc->current->image.y, |
adaptive_quantization(pEnc->current->image.y, |
902 |
pEnc->mbParam.edged_width, |
pEnc->mbParam.edged_width, temp_dquants, |
903 |
temp_dquants, |
pEnc->current->quant, pEnc->current->quant, |
|
pEnc->current->quant, |
|
|
pEnc->current->quant, |
|
904 |
2*pEnc->current->quant, |
2*pEnc->current->quant, |
905 |
pEnc->mbParam.mb_width, |
pEnc->mbParam.mb_width, |
906 |
pEnc->mbParam.mb_height); |
pEnc->mbParam.mb_height); |
907 |
|
|
908 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
|
{ |
|
909 |
|
|
910 |
#define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width) |
#define OFFSET(x,y) ((x) + (y)*pEnc->mbParam.mb_width) |
911 |
|
|
912 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
|
{ |
|
913 |
|
|
914 |
|
|
915 |
MACROBLOCK *pMB = |
MACROBLOCK *pMB = &pEnc->current->mbs[OFFSET(x, y)]; |
916 |
&pEnc->current->mbs[OFFSET(x,y)]; |
|
917 |
pMB->dquant = |
pMB->dquant = iDQtab[temp_dquants[OFFSET(x, y)] + 2]; |
|
iDQtab[temp_dquants[OFFSET(x,y)] + 2]; |
|
918 |
} |
} |
919 |
|
|
920 |
#undef OFFSET |
#undef OFFSET |
923 |
xvid_free(temp_dquants); |
xvid_free(temp_dquants); |
924 |
} |
} |
925 |
|
|
926 |
if (pEnc->current->global_flags & XVID_H263QUANT) |
if (pEnc->current->global_flags & XVID_H263QUANT) { |
|
{ |
|
927 |
if(pEnc->mbParam.m_quant_type != H263_QUANT) |
if(pEnc->mbParam.m_quant_type != H263_QUANT) |
928 |
write_vol_header = 1; |
write_vol_header = 1; |
929 |
pEnc->mbParam.m_quant_type = H263_QUANT; |
pEnc->mbParam.m_quant_type = H263_QUANT; |
930 |
} |
} else if (pEnc->current->global_flags & XVID_MPEGQUANT) { |
|
else if (pEnc->current->global_flags & XVID_MPEGQUANT) |
|
|
{ |
|
931 |
int matrix1_changed, matrix2_changed; |
int matrix1_changed, matrix2_changed; |
932 |
|
|
933 |
matrix1_changed = matrix2_changed = 0; |
matrix1_changed = matrix2_changed = 0; |
939 |
|
|
940 |
if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) { |
if ((pEnc->current->global_flags & XVID_CUSTOM_QMATRIX) > 0) { |
941 |
if(pFrame->quant_intra_matrix != NULL) |
if(pFrame->quant_intra_matrix != NULL) |
942 |
matrix1_changed = |
matrix1_changed = set_intra_matrix(pFrame->quant_intra_matrix); |
|
set_intra_matrix(pFrame->quant_intra_matrix); |
|
943 |
if(pFrame->quant_inter_matrix != NULL) |
if(pFrame->quant_inter_matrix != NULL) |
944 |
matrix2_changed |
matrix2_changed = set_inter_matrix(pFrame->quant_inter_matrix); |
945 |
= set_inter_matrix(pFrame->quant_inter_matrix); |
} else { |
|
} |
|
|
else { |
|
946 |
matrix1_changed = set_intra_matrix(get_default_intra_matrix()); |
matrix1_changed = set_intra_matrix(get_default_intra_matrix()); |
947 |
matrix2_changed = set_inter_matrix(get_default_inter_matrix()); |
matrix2_changed = set_inter_matrix(get_default_inter_matrix()); |
948 |
} |
} |
950 |
write_vol_header = matrix1_changed | matrix2_changed; |
write_vol_header = matrix1_changed | matrix2_changed; |
951 |
} |
} |
952 |
|
|
953 |
if (pFrame->intra < 0) |
if (pFrame->intra < 0) { |
954 |
{ |
if ((pEnc->iFrameNum == 0) |
955 |
if ((pEnc->iFrameNum == 0) || |
|| ((pEnc->iMaxKeyInterval > 0) |
956 |
|
&& (pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) { |
957 |
((pEnc->iMaxKeyInterval > 0) && |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
958 |
(pEnc->iFrameNum >= pEnc->iMaxKeyInterval))) |
} else { |
959 |
{ |
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 0, write_vol_header); |
960 |
pFrame->intra = FrameCodeI(pEnc, |
} |
961 |
&bs, |
} else { |
962 |
&bits); |
if (pFrame->intra == 1) { |
963 |
} |
pFrame->intra = FrameCodeI(pEnc, &bs, &bits); |
964 |
else |
} else { |
965 |
{ |
pFrame->intra = FrameCodeP(pEnc, &bs, &bits, 1, write_vol_header); |
|
pFrame->intra = FrameCodeP(pEnc, |
|
|
&bs, |
|
|
&bits, |
|
|
0, |
|
|
write_vol_header); |
|
|
} |
|
|
} |
|
|
else |
|
|
{ |
|
|
if (pFrame->intra == 1) |
|
|
{ |
|
|
pFrame->intra = FrameCodeI(pEnc, |
|
|
&bs, |
|
|
&bits); |
|
|
} |
|
|
else |
|
|
{ |
|
|
pFrame->intra = FrameCodeP(pEnc, |
|
|
&bs, |
|
|
&bits, |
|
|
1, |
|
|
write_vol_header); |
|
966 |
} |
} |
967 |
|
|
968 |
} |
} |
972 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
973 |
pFrame->length = BitstreamLength(&bs); |
pFrame->length = BitstreamLength(&bs); |
974 |
|
|
975 |
if (pResult) |
if (pResult) { |
|
{ |
|
976 |
pResult->quant = pEnc->current->quant; |
pResult->quant = pEnc->current->quant; |
977 |
pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
pResult->hlength = pFrame->length - (pEnc->sStat.iTextBits / 8); |
978 |
pResult->kblks = pEnc->sStat.kblks; |
pResult->kblks = pEnc->sStat.kblks; |
980 |
pResult->ublks = pEnc->sStat.ublks; |
pResult->ublks = pEnc->sStat.ublks; |
981 |
} |
} |
982 |
|
|
983 |
EMMS(); |
emms(); |
984 |
|
|
985 |
if (pFrame->quant == 0) |
if (pFrame->quant == 0) { |
986 |
{ |
RateControlUpdate(&pEnc->rate_control, pEnc->current->quant, |
987 |
RateControlUpdate(&pEnc->rate_control, |
pFrame->length, pFrame->intra); |
|
pEnc->current->quant, |
|
|
pFrame->length, |
|
|
pFrame->intra); |
|
988 |
} |
} |
|
|
|
989 |
#ifdef _DEBUG |
#ifdef _DEBUG |
990 |
psnr = image_psnr(&pEnc->sOriginal, |
psnr = |
991 |
&pEnc->current->image, |
image_psnr(&pEnc->sOriginal, &pEnc->current->image, |
992 |
pEnc->mbParam.edged_width, |
pEnc->mbParam.edged_width, pEnc->mbParam.width, |
|
pEnc->mbParam.width, |
|
993 |
pEnc->mbParam.height); |
pEnc->mbParam.height); |
994 |
|
|
995 |
snprintf(temp, 127, "PSNR: %f\n", psnr); |
snprintf(temp, 127, "PSNR: %f\n", psnr); |
1006 |
#endif |
#endif |
1007 |
|
|
1008 |
|
|
1009 |
static __inline void CodeIntraMB(Encoder *pEnc, MACROBLOCK *pMB) { |
static __inline void |
1010 |
|
CodeIntraMB(Encoder * pEnc, |
1011 |
|
MACROBLOCK * pMB) |
1012 |
|
{ |
1013 |
|
|
1014 |
pMB->mode = MODE_INTRA; |
pMB->mode = MODE_INTRA; |
1015 |
|
|
1020 |
pMB->sad16 = 0; |
pMB->sad16 = 0; |
1021 |
|
|
1022 |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
if ((pEnc->current->global_flags & XVID_LUMIMASKING)) { |
1023 |
if(pMB->dquant != NO_CHANGE) |
if (pMB->dquant != NO_CHANGE) { |
|
{ |
|
1024 |
pMB->mode = MODE_INTRA_Q; |
pMB->mode = MODE_INTRA_Q; |
1025 |
pEnc->current->quant += DQtab[pMB->dquant]; |
pEnc->current->quant += DQtab[pMB->dquant]; |
1026 |
|
|
1027 |
if (pEnc->current->quant > 31) pEnc->current->quant = 31; |
if (pEnc->current->quant > 31) |
1028 |
if (pEnc->current->quant < 1) pEnc->current->quant = 1; |
pEnc->current->quant = 31; |
1029 |
|
if (pEnc->current->quant < 1) |
1030 |
|
pEnc->current->quant = 1; |
1031 |
} |
} |
1032 |
} |
} |
1033 |
|
|
1038 |
#define FCODEBITS 3 |
#define FCODEBITS 3 |
1039 |
#define MODEBITS 5 |
#define MODEBITS 5 |
1040 |
|
|
1041 |
void HintedMESet(Encoder * pEnc, int * intra) |
void |
1042 |
|
HintedMESet(Encoder * pEnc, |
1043 |
|
int *intra) |
1044 |
{ |
{ |
1045 |
HINTINFO * hint; |
HINTINFO * hint; |
1046 |
Bitstream bs; |
Bitstream bs; |
1049 |
|
|
1050 |
hint = pEnc->mbParam.hint; |
hint = pEnc->mbParam.hint; |
1051 |
|
|
1052 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1053 |
*intra = hint->mvhint.intra; |
*intra = hint->mvhint.intra; |
1054 |
} |
} else { |
|
else |
|
|
{ |
|
1055 |
BitstreamInit(&bs, hint->hintstream, hint->hintlength); |
BitstreamInit(&bs, hint->hintstream, hint->hintlength); |
1056 |
*intra = BitstreamGetBit(&bs); |
*intra = BitstreamGetBit(&bs); |
1057 |
} |
} |
1058 |
|
|
1059 |
if (*intra) |
if (*intra) { |
|
{ |
|
1060 |
return; |
return; |
1061 |
} |
} |
1062 |
|
|
1063 |
pEnc->current->fcode = (hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, FCODEBITS); |
pEnc->current->fcode = |
1064 |
|
(hint->rawhints) ? hint->mvhint.fcode : BitstreamGetBits(&bs, |
1065 |
|
FCODEBITS); |
1066 |
|
|
1067 |
length = pEnc->current->fcode + 5; |
length = pEnc->current->fcode + 5; |
1068 |
high = 1 << (length - 1); |
high = 1 << (length - 1); |
1069 |
|
|
1070 |
for (y=0 ; y<pEnc->mbParam.mb_height ; ++y) |
for (y = 0; y < pEnc->mbParam.mb_height; ++y) { |
1071 |
{ |
for (x = 0; x < pEnc->mbParam.mb_width; ++x) { |
1072 |
for (x=0 ; x<pEnc->mbParam.mb_width ; ++x) |
MACROBLOCK *pMB = |
1073 |
{ |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
1074 |
MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
MVBLOCKHINT *bhint = |
1075 |
MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
&hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
1076 |
VECTOR pred[4]; |
VECTOR pred[4]; |
1077 |
VECTOR tmp; |
VECTOR tmp; |
1078 |
int32_t dummy[4]; |
int32_t dummy[4]; |
1079 |
int vec; |
int vec; |
1080 |
|
|
1081 |
pMB->mode = (hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, MODEBITS); |
pMB->mode = |
1082 |
|
(hint->rawhints) ? bhint->mode : BitstreamGetBits(&bs, |
1083 |
|
MODEBITS); |
1084 |
|
|
1085 |
pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode; |
pMB->mode = (pMB->mode == MODE_INTER_Q) ? MODE_INTER : pMB->mode; |
1086 |
pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode; |
pMB->mode = (pMB->mode == MODE_INTRA_Q) ? MODE_INTRA : pMB->mode; |
1087 |
|
|
1088 |
if (pMB->mode == MODE_INTER) |
if (pMB->mode == MODE_INTER) { |
1089 |
{ |
tmp.x = |
1090 |
tmp.x = (hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, length); |
(hint->rawhints) ? bhint->mvs[0].x : BitstreamGetBits(&bs, |
1091 |
tmp.y = (hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, length); |
length); |
1092 |
|
tmp.y = |
1093 |
|
(hint->rawhints) ? bhint->mvs[0].y : BitstreamGetBits(&bs, |
1094 |
|
length); |
1095 |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
1096 |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
1097 |
|
|
1098 |
get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, 0, pred, dummy); |
get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, |
1099 |
|
0, pred, dummy); |
1100 |
|
|
1101 |
for (vec=0 ; vec<4 ; ++vec) |
for (vec = 0; vec < 4; ++vec) { |
|
{ |
|
1102 |
pMB->mvs[vec].x = tmp.x; |
pMB->mvs[vec].x = tmp.x; |
1103 |
pMB->mvs[vec].y = tmp.y; |
pMB->mvs[vec].y = tmp.y; |
1104 |
pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x; |
pMB->pmvs[vec].x = pMB->mvs[0].x - pred[0].x; |
1105 |
pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y; |
pMB->pmvs[vec].y = pMB->mvs[0].y - pred[0].y; |
1106 |
} |
} |
1107 |
} |
} else if (pMB->mode == MODE_INTER4V) { |
1108 |
else if (pMB->mode == MODE_INTER4V) |
for (vec = 0; vec < 4; ++vec) { |
1109 |
{ |
tmp.x = |
1110 |
for (vec=0 ; vec<4 ; ++vec) |
(hint->rawhints) ? bhint->mvs[vec]. |
1111 |
{ |
x : BitstreamGetBits(&bs, length); |
1112 |
tmp.x = (hint->rawhints) ? bhint->mvs[vec].x : BitstreamGetBits(&bs, length); |
tmp.y = |
1113 |
tmp.y = (hint->rawhints) ? bhint->mvs[vec].y : BitstreamGetBits(&bs, length); |
(hint->rawhints) ? bhint->mvs[vec]. |
1114 |
|
y : BitstreamGetBits(&bs, length); |
1115 |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
tmp.x -= (tmp.x >= high) ? high*2 : 0; |
1116 |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
tmp.y -= (tmp.y >= high) ? high*2 : 0; |
1117 |
|
|
1118 |
get_pmvdata(pEnc->current->mbs, x, y, pEnc->mbParam.mb_width, vec, pred, dummy); |
get_pmvdata(pEnc->current->mbs, x, y, |
1119 |
|
pEnc->mbParam.mb_width, vec, pred, dummy); |
1120 |
|
|
1121 |
pMB->mvs[vec].x = tmp.x; |
pMB->mvs[vec].x = tmp.x; |
1122 |
pMB->mvs[vec].y = tmp.y; |
pMB->mvs[vec].y = tmp.y; |
1123 |
pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x; |
pMB->pmvs[vec].x = pMB->mvs[vec].x - pred[0].x; |
1124 |
pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y; |
pMB->pmvs[vec].y = pMB->mvs[vec].y - pred[0].y; |
1125 |
} |
} |
1126 |
} |
} else // intra / stuffing / not_coded |
|
else // intra / stuffing / not_coded |
|
|
{ |
|
|
for (vec=0 ; vec<4 ; ++vec) |
|
1127 |
{ |
{ |
1128 |
|
for (vec = 0; vec < 4; ++vec) { |
1129 |
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
1130 |
} |
} |
1131 |
} |
} |
1132 |
|
|
1133 |
if (pMB->mode == MODE_INTER4V && |
if (pMB->mode == MODE_INTER4V && |
1134 |
(pEnc->current->global_flags & XVID_LUMIMASKING) && pMB->dquant != NO_CHANGE) |
(pEnc->current->global_flags & XVID_LUMIMASKING) |
1135 |
{ |
&& pMB->dquant != NO_CHANGE) { |
1136 |
pMB->mode = MODE_INTRA; |
pMB->mode = MODE_INTRA; |
1137 |
|
|
1138 |
for (vec=0 ; vec<4 ; ++vec) |
for (vec = 0; vec < 4; ++vec) { |
|
{ |
|
1139 |
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
pMB->mvs[vec].x = pMB->mvs[vec].y = 0; |
1140 |
} |
} |
1141 |
} |
} |
1144 |
} |
} |
1145 |
|
|
1146 |
|
|
1147 |
void HintedMEGet(Encoder * pEnc, int intra) |
void |
1148 |
|
HintedMEGet(Encoder * pEnc, |
1149 |
|
int intra) |
1150 |
{ |
{ |
1151 |
HINTINFO * hint; |
HINTINFO * hint; |
1152 |
Bitstream bs; |
Bitstream bs; |
1155 |
|
|
1156 |
hint = pEnc->mbParam.hint; |
hint = pEnc->mbParam.hint; |
1157 |
|
|
1158 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1159 |
hint->mvhint.intra = intra; |
hint->mvhint.intra = intra; |
1160 |
} |
} else { |
|
else |
|
|
{ |
|
1161 |
BitstreamInit(&bs, hint->hintstream, 0); |
BitstreamInit(&bs, hint->hintstream, 0); |
1162 |
BitstreamPutBit(&bs, intra); |
BitstreamPutBit(&bs, intra); |
1163 |
} |
} |
1164 |
|
|
1165 |
if (intra) |
if (intra) { |
1166 |
{ |
if (!hint->rawhints) { |
|
if (!hint->rawhints) |
|
|
{ |
|
1167 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
1168 |
hint->hintlength = BitstreamLength(&bs); |
hint->hintlength = BitstreamLength(&bs); |
1169 |
} |
} |
1173 |
length = pEnc->current->fcode + 5; |
length = pEnc->current->fcode + 5; |
1174 |
high = 1 << (length - 1); |
high = 1 << (length - 1); |
1175 |
|
|
1176 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1177 |
hint->mvhint.fcode = pEnc->current->fcode; |
hint->mvhint.fcode = pEnc->current->fcode; |
1178 |
} |
} else { |
|
else |
|
|
{ |
|
1179 |
BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS); |
BitstreamPutBits(&bs, pEnc->current->fcode, FCODEBITS); |
1180 |
} |
} |
1181 |
|
|
1182 |
for (y=0 ; y<pEnc->mbParam.mb_height ; ++y) |
for (y = 0; y < pEnc->mbParam.mb_height; ++y) { |
1183 |
{ |
for (x = 0; x < pEnc->mbParam.mb_width; ++x) { |
1184 |
for (x=0 ; x<pEnc->mbParam.mb_width ; ++x) |
MACROBLOCK *pMB = |
1185 |
{ |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
1186 |
MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
MVBLOCKHINT *bhint = |
1187 |
MVBLOCKHINT * bhint = &hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
&hint->mvhint.block[x + y * pEnc->mbParam.mb_width]; |
1188 |
VECTOR tmp; |
VECTOR tmp; |
1189 |
|
|
1190 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1191 |
bhint->mode = pMB->mode; |
bhint->mode = pMB->mode; |
1192 |
} |
} else { |
|
else |
|
|
{ |
|
1193 |
BitstreamPutBits(&bs, pMB->mode, MODEBITS); |
BitstreamPutBits(&bs, pMB->mode, MODEBITS); |
1194 |
} |
} |
1195 |
|
|
1196 |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
|
{ |
|
1197 |
tmp.x = pMB->mvs[0].x; |
tmp.x = pMB->mvs[0].x; |
1198 |
tmp.y = pMB->mvs[0].y; |
tmp.y = pMB->mvs[0].y; |
1199 |
tmp.x += (tmp.x < 0) ? high*2 : 0; |
tmp.x += (tmp.x < 0) ? high*2 : 0; |
1200 |
tmp.y += (tmp.y < 0) ? high*2 : 0; |
tmp.y += (tmp.y < 0) ? high*2 : 0; |
1201 |
|
|
1202 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1203 |
bhint->mvs[0].x = tmp.x; |
bhint->mvs[0].x = tmp.x; |
1204 |
bhint->mvs[0].y = tmp.y; |
bhint->mvs[0].y = tmp.y; |
1205 |
} |
} else { |
|
else |
|
|
{ |
|
1206 |
BitstreamPutBits(&bs, tmp.x, length); |
BitstreamPutBits(&bs, tmp.x, length); |
1207 |
BitstreamPutBits(&bs, tmp.y, length); |
BitstreamPutBits(&bs, tmp.y, length); |
1208 |
} |
} |
1209 |
} |
} else if (pMB->mode == MODE_INTER4V) { |
|
else if (pMB->mode == MODE_INTER4V) |
|
|
{ |
|
1210 |
int vec; |
int vec; |
1211 |
|
|
1212 |
for (vec=0 ; vec<4 ; ++vec) |
for (vec = 0; vec < 4; ++vec) { |
|
{ |
|
1213 |
tmp.x = pMB->mvs[vec].x; |
tmp.x = pMB->mvs[vec].x; |
1214 |
tmp.y = pMB->mvs[vec].y; |
tmp.y = pMB->mvs[vec].y; |
1215 |
tmp.x += (tmp.x < 0) ? high*2 : 0; |
tmp.x += (tmp.x < 0) ? high*2 : 0; |
1216 |
tmp.y += (tmp.y < 0) ? high*2 : 0; |
tmp.y += (tmp.y < 0) ? high*2 : 0; |
1217 |
|
|
1218 |
if (hint->rawhints) |
if (hint->rawhints) { |
|
{ |
|
1219 |
bhint->mvs[vec].x = tmp.x; |
bhint->mvs[vec].x = tmp.x; |
1220 |
bhint->mvs[vec].y = tmp.y; |
bhint->mvs[vec].y = tmp.y; |
1221 |
} |
} else { |
|
else |
|
|
{ |
|
1222 |
BitstreamPutBits(&bs, tmp.x, length); |
BitstreamPutBits(&bs, tmp.x, length); |
1223 |
BitstreamPutBits(&bs, tmp.y, length); |
BitstreamPutBits(&bs, tmp.y, length); |
1224 |
} |
} |
1227 |
} |
} |
1228 |
} |
} |
1229 |
|
|
1230 |
if (!hint->rawhints) |
if (!hint->rawhints) { |
|
{ |
|
1231 |
BitstreamPad(&bs); |
BitstreamPad(&bs); |
1232 |
hint->hintlength = BitstreamLength(&bs); |
hint->hintlength = BitstreamLength(&bs); |
1233 |
} |
} |
1234 |
} |
} |
1235 |
|
|
1236 |
|
|
1237 |
static int FrameCodeI(Encoder * pEnc, Bitstream * bs, uint32_t *pBits) |
static int |
1238 |
|
FrameCodeI(Encoder * pEnc, |
1239 |
|
Bitstream * bs, |
1240 |
|
uint32_t * pBits) |
1241 |
{ |
{ |
1242 |
|
|
1243 |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
DECLARE_ALIGNED_MATRIX(dct_codes, 6, 64, int16_t, CACHE_LINE); |
1260 |
pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
1261 |
|
|
1262 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
1263 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1264 |
{ |
MACROBLOCK *pMB = |
1265 |
MACROBLOCK *pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
1266 |
|
|
1267 |
CodeIntraMB(pEnc, pMB); |
CodeIntraMB(pEnc, pMB); |
1268 |
|
|
1269 |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1270 |
|
dct_codes, qcoeff); |
1271 |
|
|
1272 |
start_timer(); |
start_timer(); |
1273 |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
1286 |
pEnc->sStat.iMvCount = 0; |
pEnc->sStat.iMvCount = 0; |
1287 |
pEnc->mbParam.m_fcode = 2; |
pEnc->mbParam.m_fcode = 2; |
1288 |
|
|
1289 |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
|
{ |
|
1290 |
HintedMEGet(pEnc, 1); |
HintedMEGet(pEnc, 1); |
1291 |
} |
} |
1292 |
|
|
1296 |
|
|
1297 |
#define INTRA_THRESHOLD 0.5 |
#define INTRA_THRESHOLD 0.5 |
1298 |
|
|
1299 |
static int FrameCodeP(Encoder * pEnc, Bitstream * bs, uint32_t *pBits, bool force_inter, bool vol_header) |
static int |
1300 |
|
FrameCodeP(Encoder * pEnc, |
1301 |
|
Bitstream * bs, |
1302 |
|
uint32_t * pBits, |
1303 |
|
bool force_inter, |
1304 |
|
bool vol_header) |
1305 |
{ |
{ |
1306 |
float fSigma; |
float fSigma; |
1307 |
|
|
1317 |
IMAGE *pRef = &pEnc->reference->image; |
IMAGE *pRef = &pEnc->reference->image; |
1318 |
|
|
1319 |
start_timer(); |
start_timer(); |
1320 |
image_setedges(pRef, |
image_setedges(pRef, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1321 |
pEnc->mbParam.edged_width, |
pEnc->mbParam.width, pEnc->mbParam.height, |
|
pEnc->mbParam.edged_height, |
|
|
pEnc->mbParam.width, |
|
|
pEnc->mbParam.height, |
|
1322 |
pEnc->current->global_flags & XVID_INTERLACING); |
pEnc->current->global_flags & XVID_INTERLACING); |
1323 |
stop_edges_timer(); |
stop_edges_timer(); |
1324 |
|
|
1327 |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
pEnc->current->fcode = pEnc->mbParam.m_fcode; |
1328 |
|
|
1329 |
if (!force_inter) |
if (!force_inter) |
1330 |
iLimit = (int)(pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * INTRA_THRESHOLD); |
iLimit = |
1331 |
|
(int) (pEnc->mbParam.mb_width * pEnc->mbParam.mb_height * |
1332 |
|
INTRA_THRESHOLD); |
1333 |
else |
else |
1334 |
iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1; |
iLimit = pEnc->mbParam.mb_width * pEnc->mbParam.mb_height + 1; |
1335 |
|
|
1336 |
if ((pEnc->current->global_flags & XVID_HALFPEL)) { |
if ((pEnc->current->global_flags & XVID_HALFPEL)) { |
1337 |
start_timer(); |
start_timer(); |
1338 |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
image_interpolate(pRef, &pEnc->vInterH, &pEnc->vInterV, |
1339 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
&pEnc->vInterHV, pEnc->mbParam.edged_width, |
1340 |
|
pEnc->mbParam.edged_height, |
1341 |
pEnc->current->rounding_type); |
pEnc->current->rounding_type); |
1342 |
stop_inter_timer(); |
stop_inter_timer(); |
1343 |
} |
} |
1344 |
|
|
1345 |
start_timer(); |
start_timer(); |
1346 |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) |
if (pEnc->current->global_flags & XVID_HINTEDME_SET) { |
|
{ |
|
1347 |
HintedMESet(pEnc, &bIntra); |
HintedMESet(pEnc, &bIntra); |
1348 |
} |
} else { |
1349 |
else |
bIntra = |
1350 |
{ |
MotionEstimation(&pEnc->mbParam, pEnc->current, pEnc->reference, |
1351 |
bIntra = MotionEstimation( |
&pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
|
&pEnc->mbParam, |
|
|
pEnc->current, |
|
|
pEnc->reference, |
|
|
&pEnc->vInterH, |
|
|
&pEnc->vInterV, |
|
|
&pEnc->vInterHV, |
|
1352 |
iLimit); |
iLimit); |
1353 |
} |
} |
1354 |
stop_motion_timer(); |
stop_motion_timer(); |
1355 |
|
|
1356 |
if (bIntra == 1) |
if (bIntra == 1) { |
|
{ |
|
1357 |
return FrameCodeI(pEnc, bs, pBits); |
return FrameCodeI(pEnc, bs, pBits); |
1358 |
} |
} |
1359 |
|
|
1371 |
pEnc->sStat.iMvCount = 0; |
pEnc->sStat.iMvCount = 0; |
1372 |
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
1373 |
|
|
1374 |
for(y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
1375 |
{ |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1376 |
for(x = 0; x < pEnc->mbParam.mb_width; x++) |
MACROBLOCK *pMB = |
1377 |
{ |
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
|
MACROBLOCK * pMB = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
|
1378 |
|
|
1379 |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
bIntra = (pMB->mode == MODE_INTRA) || (pMB->mode == MODE_INTRA_Q); |
1380 |
|
|
1381 |
if (!bIntra) |
if (!bIntra) { |
|
{ |
|
1382 |
start_timer(); |
start_timer(); |
1383 |
MBMotionCompensation(pMB, |
MBMotionCompensation(pMB, x, y, &pEnc->reference->image, |
1384 |
x, y, |
&pEnc->vInterH, &pEnc->vInterV, |
1385 |
&pEnc->reference->image, |
&pEnc->vInterHV, &pEnc->current->image, |
1386 |
&pEnc->vInterH, |
dct_codes, pEnc->mbParam.width, |
|
&pEnc->vInterV, |
|
|
&pEnc->vInterHV, |
|
|
&pEnc->current->image, |
|
|
dct_codes, |
|
|
pEnc->mbParam.width, |
|
1387 |
pEnc->mbParam.height, |
pEnc->mbParam.height, |
1388 |
pEnc->mbParam.edged_width, |
pEnc->mbParam.edged_width, |
1389 |
pEnc->current->rounding_type); |
pEnc->current->rounding_type); |
1393 |
if(pMB->dquant != NO_CHANGE) { |
if(pMB->dquant != NO_CHANGE) { |
1394 |
pMB->mode = MODE_INTER_Q; |
pMB->mode = MODE_INTER_Q; |
1395 |
pEnc->current->quant += DQtab[pMB->dquant]; |
pEnc->current->quant += DQtab[pMB->dquant]; |
1396 |
if (pEnc->current->quant > 31) pEnc->current->quant = 31; |
if (pEnc->current->quant > 31) |
1397 |
else if(pEnc->current->quant < 1) pEnc->current->quant = 1; |
pEnc->current->quant = 31; |
1398 |
|
else if (pEnc->current->quant < 1) |
1399 |
|
pEnc->current->quant = 1; |
1400 |
} |
} |
1401 |
} |
} |
1402 |
pMB->quant = pEnc->current->quant; |
pMB->quant = pEnc->current->quant; |
1403 |
|
|
1404 |
pMB->field_pred = 0; |
pMB->field_pred = 0; |
1405 |
|
|
1406 |
pMB->cbp = MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
pMB->cbp = |
1407 |
} |
MBTransQuantInter(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1408 |
else |
dct_codes, qcoeff); |
1409 |
{ |
} else { |
1410 |
CodeIntraMB(pEnc, pMB); |
CodeIntraMB(pEnc, pMB); |
1411 |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, dct_codes, qcoeff); |
MBTransQuantIntra(&pEnc->mbParam, pEnc->current, pMB, x, y, |
1412 |
|
dct_codes, qcoeff); |
1413 |
} |
} |
1414 |
|
|
1415 |
start_timer(); |
start_timer(); |
1416 |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
MBPrediction(pEnc->current, x, y, pEnc->mbParam.mb_width, qcoeff); |
1417 |
stop_prediction_timer(); |
stop_prediction_timer(); |
1418 |
|
|
1419 |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) |
if (pMB->mode == MODE_INTRA || pMB->mode == MODE_INTRA_Q) { |
|
{ |
|
1420 |
pEnc->sStat.kblks++; |
pEnc->sStat.kblks++; |
1421 |
} |
} else if (pMB->cbp || pMB->mvs[0].x || pMB->mvs[0].y || |
1422 |
else if (pMB->cbp || |
pMB->mvs[1].x || pMB->mvs[1].y || pMB->mvs[2].x || |
1423 |
pMB->mvs[0].x || pMB->mvs[0].y || |
pMB->mvs[2].y || pMB->mvs[3].x || pMB->mvs[3].y) { |
|
pMB->mvs[1].x || pMB->mvs[1].y || |
|
|
pMB->mvs[2].x || pMB->mvs[2].y || |
|
|
pMB->mvs[3].x || pMB->mvs[3].y) |
|
|
{ |
|
1424 |
pEnc->sStat.mblks++; |
pEnc->sStat.mblks++; |
1425 |
} |
} else { |
|
else |
|
|
{ |
|
1426 |
pEnc->sStat.ublks++; |
pEnc->sStat.ublks++; |
1427 |
} |
} |
1428 |
|
|
1434 |
|
|
1435 |
emms(); |
emms(); |
1436 |
|
|
1437 |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) |
if (pEnc->current->global_flags & XVID_HINTEDME_GET) { |
|
{ |
|
1438 |
HintedMEGet(pEnc, 0); |
HintedMEGet(pEnc, 0); |
1439 |
} |
} |
1440 |
|
|
1450 |
{ |
{ |
1451 |
pEnc->mbParam.m_fcode++; |
pEnc->mbParam.m_fcode++; |
1452 |
iSearchRange *= 2; |
iSearchRange *= 2; |
1453 |
} |
} else if ((fSigma < iSearchRange / 6) |
|
else if ((fSigma < iSearchRange / 6) |
|
1454 |
&& (pEnc->sStat.fMvPrevSigma >= 0) |
&& (pEnc->sStat.fMvPrevSigma >= 0) |
1455 |
&& (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) |
&& (pEnc->sStat.fMvPrevSigma < iSearchRange / 6) |
1456 |
&& (pEnc->mbParam.m_fcode >= 2)) // minimum search range 16 |
&& (pEnc->mbParam.m_fcode >= 2)) // minimum search range 16 |
1468 |
|
|
1469 |
|
|
1470 |
#ifdef BFRAMES |
#ifdef BFRAMES |
1471 |
static void FrameCodeB(Encoder * pEnc, FRAMEINFO * frame, Bitstream * bs, uint32_t *pBits) |
static void |
1472 |
|
FrameCodeB(Encoder * pEnc, |
1473 |
|
FRAMEINFO * frame, |
1474 |
|
Bitstream * bs, |
1475 |
|
uint32_t * pBits) |
1476 |
{ |
{ |
1477 |
int16_t dct_codes[6*64]; |
int16_t dct_codes[6*64]; |
1478 |
int16_t qcoeff[6*64]; |
int16_t qcoeff[6*64]; |
1484 |
IMAGE *b_ref = &pEnc->current->image; |
IMAGE *b_ref = &pEnc->current->image; |
1485 |
|
|
1486 |
// forward |
// forward |
1487 |
image_setedges(f_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height, frame->global_flags & XVID_INTERLACING); |
image_setedges(f_ref, pEnc->mbParam.edged_width, |
1488 |
|
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
1489 |
|
pEnc->mbParam.height, |
1490 |
|
frame->global_flags & XVID_INTERLACING); |
1491 |
start_timer(); |
start_timer(); |
1492 |
image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
image_interpolate(f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
1493 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0); |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1494 |
|
0); |
1495 |
stop_inter_timer(); |
stop_inter_timer(); |
1496 |
|
|
1497 |
// backward |
// backward |
1498 |
image_setedges(b_ref, pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, pEnc->mbParam.width, pEnc->mbParam.height, frame->global_flags & XVID_INTERLACING); |
image_setedges(b_ref, pEnc->mbParam.edged_width, |
1499 |
|
pEnc->mbParam.edged_height, pEnc->mbParam.width, |
1500 |
|
pEnc->mbParam.height, |
1501 |
|
frame->global_flags & XVID_INTERLACING); |
1502 |
start_timer(); |
start_timer(); |
1503 |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
image_interpolate(b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
1504 |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, 0); |
pEnc->mbParam.edged_width, pEnc->mbParam.edged_height, |
1505 |
|
0); |
1506 |
stop_inter_timer(); |
stop_inter_timer(); |
1507 |
|
|
1508 |
start_timer(); |
start_timer(); |
1509 |
MotionEstimationBVOP(&pEnc->mbParam, frame, |
MotionEstimationBVOP(&pEnc->mbParam, frame, pEnc->reference->mbs, f_ref, |
1510 |
pEnc->reference->mbs, f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
&pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
1511 |
pEnc->current->mbs, b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV); |
pEnc->current->mbs, b_ref, &pEnc->vInterH, |
1512 |
|
&pEnc->vInterV, &pEnc->vInterHV); |
1513 |
|
|
1514 |
|
|
1515 |
stop_motion_timer(); |
stop_motion_timer(); |
1530 |
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
pEnc->sStat.kblks = pEnc->sStat.mblks = pEnc->sStat.ublks = 0; |
1531 |
|
|
1532 |
|
|
1533 |
for (y = 0; y < pEnc->mbParam.mb_height; y++) |
for (y = 0; y < pEnc->mbParam.mb_height; y++) { |
|
{ |
|
1534 |
// reset prediction |
// reset prediction |
1535 |
|
|
1536 |
forward.x = 0; |
forward.x = 0; |
1538 |
backward.x = 0; |
backward.x = 0; |
1539 |
backward.y = 0; |
backward.y = 0; |
1540 |
|
|
1541 |
for (x = 0; x < pEnc->mbParam.mb_width; x++) |
for (x = 0; x < pEnc->mbParam.mb_width; x++) { |
1542 |
{ |
MACROBLOCK *f_mb = |
1543 |
MACROBLOCK * f_mb = &pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width]; |
&pEnc->reference->mbs[x + y * pEnc->mbParam.mb_width]; |
1544 |
MACROBLOCK * b_mb = &pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
MACROBLOCK *b_mb = |
1545 |
|
&pEnc->current->mbs[x + y * pEnc->mbParam.mb_width]; |
1546 |
MACROBLOCK * mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
MACROBLOCK * mb = &frame->mbs[x + y * pEnc->mbParam.mb_width]; |
1547 |
|
|
1548 |
// decoder ignores mb when refence block is INTER(0,0), CBP=0 |
// decoder ignores mb when refence block is INTER(0,0), CBP=0 |
1549 |
if (mb->mode == MODE_NOT_CODED) |
if (mb->mode == MODE_NOT_CODED) { |
|
{ |
|
1550 |
mb->mvs[0].x = 0; |
mb->mvs[0].x = 0; |
1551 |
mb->mvs[0].y = 0; |
mb->mvs[0].y = 0; |
1552 |
continue; |
continue; |
1553 |
} |
} |
1554 |
|
|
1555 |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
MBMotionCompensationBVOP(&pEnc->mbParam, mb, x, y, &frame->image, |
1556 |
f_ref, &pEnc->f_refh, &pEnc->f_refv, &pEnc->f_refhv, |
f_ref, &pEnc->f_refh, &pEnc->f_refv, |
1557 |
b_ref, &pEnc->vInterH, &pEnc->vInterV, &pEnc->vInterHV, |
&pEnc->f_refhv, b_ref, &pEnc->vInterH, |
1558 |
|
&pEnc->vInterV, &pEnc->vInterHV, |
1559 |
dct_codes); |
dct_codes); |
1560 |
|
|
1561 |
mb->quant = frame->quant; |
mb->quant = frame->quant; |
1562 |
mb->cbp = MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes, qcoeff); |
mb->cbp = |
1563 |
|
MBTransQuantInter(&pEnc->mbParam, frame, mb, x, y, dct_codes, |
1564 |
|
qcoeff); |
1565 |
//mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant); |
//mb->cbp = MBTransQuantBVOP(&pEnc->mbParam, x, y, dct_codes, qcoeff, &frame->image, frame->quant); |
1566 |
|
|
1567 |
|
|
1568 |
if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) && |
if ((mb->mode == MODE_INTERPOLATE || mb->mode == MODE_DIRECT) |
1569 |
mb->cbp == 0 && |
&& mb->cbp == 0 && mb->mvs[0].x == 0 && mb->mvs[0].y == 0) { |
|
mb->mvs[0].x == 0 && |
|
|
mb->mvs[0].y == 0) |
|
|
{ |
|
1570 |
mb->mode = 5; // skipped |
mb->mode = 5; // skipped |
1571 |
} |
} |
1572 |
|
|
1573 |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_FORWARD) { |
|
{ |
|
1574 |
mb->pmvs[0].x = mb->mvs[0].x - forward.x; |
mb->pmvs[0].x = mb->mvs[0].x - forward.x; |
1575 |
mb->pmvs[0].y = mb->mvs[0].y - forward.y; |
mb->pmvs[0].y = mb->mvs[0].y - forward.y; |
1576 |
forward.x = mb->mvs[0].x; |
forward.x = mb->mvs[0].x; |
1577 |
forward.y = mb->mvs[0].y; |
forward.y = mb->mvs[0].y; |
1578 |
} |
} |
1579 |
|
|
1580 |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) |
if (mb->mode == MODE_INTERPOLATE || mb->mode == MODE_BACKWARD) { |
|
{ |
|
1581 |
mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x; |
mb->b_pmvs[0].x = mb->b_mvs[0].x - backward.x; |
1582 |
mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y; |
mb->b_pmvs[0].y = mb->b_mvs[0].y - backward.y; |
1583 |
backward.x = mb->b_mvs[0].x; |
backward.x = mb->b_mvs[0].x; |
1584 |
backward.y = mb->b_mvs[0].y; |
backward.y = mb->b_mvs[0].y; |
1585 |
} |
} |
|
|
|
1586 |
// 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); |
// 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); |
1587 |
|
|
1588 |
start_timer(); |
start_timer(); |
1589 |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, &pEnc->sStat); |
MBCodingBVOP(mb, qcoeff, frame->fcode, frame->bcode, bs, |
1590 |
|
&pEnc->sStat); |
1591 |
stop_coding_timer(); |
stop_coding_timer(); |
1592 |
} |
} |
1593 |
} |
} |