1 |
/****************************************************************************** |
/***************************************************************************** |
2 |
* * |
* |
3 |
* This file is part of XviD, a free MPEG-4 video encoder/decoder * |
* XVID MPEG-4 VIDEO CODEC |
4 |
* * |
* - MacroBlock transfer and quantization - |
5 |
* XviD is an implementation of a part of one or more MPEG-4 Video tools * |
* |
6 |
* as specified in ISO/IEC 14496-2 standard. Those intending to use this * |
* Copyright(C) 2002-2001 Christoph Lampert <gruel@web.de> |
7 |
* software module in hardware or software products are advised that its * |
* 2002-2001 Michael Militzer <isibaar@xvid.org> |
8 |
* use may infringe existing patents or copyrights, and any such use * |
* 2002-2001 Peter Ross <pross@xvid.org> |
9 |
* would be at such party's own risk. The original developer of this * |
* 2002 Daniel Smith <danielsmith@astroboymail.com> |
10 |
* software module and his/her company, and subsequent editors and their * |
* |
11 |
* companies, will have no liability for use of this software or * |
* This program is an implementation of a part of one or more MPEG-4 |
12 |
* modifications or derivatives thereof. * |
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
13 |
* * |
* to use this software module in hardware or software products are |
14 |
* XviD is free software; you can redistribute it and/or modify it * |
* advised that its use may infringe existing patents or copyrights, and |
15 |
* under the terms of the GNU General Public License as published by * |
* any such use would be at such party's own risk. The original |
16 |
* the Free Software Foundation; either version 2 of the License, or * |
* developer of this software module and his/her company, and subsequent |
17 |
* (at your option) any later version. * |
* editors and their companies, will have no liability for use of this |
18 |
* * |
* software or modifications or derivatives thereof. |
19 |
* XviD is distributed in the hope that it will be useful, but * |
* |
20 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
* This program is free software; you can redistribute it and/or modify |
21 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
* it under the terms of the GNU General Public License as published by |
22 |
* GNU General Public License for more details. * |
* the Free Software Foundation; either version 2 of the License, or |
23 |
* * |
* (at your option) any later version. |
24 |
* You should have received a copy of the GNU General Public License * |
* |
25 |
* along with this program; if not, write to the Free Software * |
* This program is distributed in the hope that it will be useful, |
26 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
27 |
* * |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
28 |
******************************************************************************/ |
* GNU General Public License for more details. |
29 |
|
* |
30 |
/****************************************************************************** |
* You should have received a copy of the GNU General Public License |
31 |
* * |
* along with this program; if not, write to the Free Software |
32 |
* mbtransquant.c * |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
33 |
* * |
* |
34 |
* Copyright (C) 2001 - Peter Ross <pross@cs.rmit.edu.au> * |
* $Id$ |
35 |
* Copyright (C) 2001 - Michael Militzer <isibaar@xvid.org> * |
* |
36 |
* * |
****************************************************************************/ |
|
* For more information visit the XviD homepage: http://www.xvid.org * |
|
|
* * |
|
|
******************************************************************************/ |
|
|
|
|
|
/****************************************************************************** |
|
|
* * |
|
|
* Revision history: * |
|
|
* * |
|
|
* 29.03.2002 interlacing speedup - used transfer strides instead of |
|
|
* manual field-to-frame conversion |
|
|
* 26.03.2002 interlacing support - moved transfers outside loops |
|
|
* 22.12.2001 get_dc_scaler() moved to common.h |
|
|
* 19.11.2001 introduced coefficient thresholding (Isibaar) * |
|
|
* 17.11.2001 initial version * |
|
|
* * |
|
|
******************************************************************************/ |
|
37 |
|
|
38 |
#include <string.h> |
#include <string.h> |
39 |
|
|
52 |
#define MIN(X, Y) ((X)<(Y)?(X):(Y)) |
#define MIN(X, Y) ((X)<(Y)?(X):(Y)) |
53 |
#define MAX(X, Y) ((X)>(Y)?(X):(Y)) |
#define MAX(X, Y) ((X)>(Y)?(X):(Y)) |
54 |
|
|
55 |
#define TOOSMALL_LIMIT 1 /* skip blocks having a coefficient sum below this value */ |
#define TOOSMALL_LIMIT 3 /* skip blocks having a coefficient sum below this value */ |
56 |
|
|
57 |
/* this isnt pretty, but its better than 20 ifdefs */ |
/* this isnt pretty, but its better than 20 ifdefs */ |
58 |
|
|
89 |
|
|
90 |
start_timer(); |
start_timer(); |
91 |
pMB->field_dct = 0; |
pMB->field_dct = 0; |
92 |
if ((frame->global_flags & XVID_INTERLACING)) { |
if ((frame->global_flags & XVID_INTERLACING) && |
93 |
|
(x_pos>0) && (x_pos<pParam->mb_width-1) && |
94 |
|
(y_pos>0) && (y_pos<pParam->mb_height-1)) { |
95 |
pMB->field_dct = MBDecideFieldDCT(data); |
pMB->field_dct = MBDecideFieldDCT(data); |
96 |
} |
} |
97 |
stop_interlacing_timer(); |
stop_interlacing_timer(); |
169 |
|
|
170 |
start_timer(); |
start_timer(); |
171 |
pMB->field_dct = 0; |
pMB->field_dct = 0; |
172 |
if ((frame->global_flags & XVID_INTERLACING)) { |
if ((frame->global_flags & XVID_INTERLACING) && |
173 |
|
(x_pos>0) && (x_pos<pParam->mb_width-1) && |
174 |
|
(y_pos>0) && (y_pos<pParam->mb_height-1)) { |
175 |
pMB->field_dct = MBDecideFieldDCT(data); |
pMB->field_dct = MBDecideFieldDCT(data); |
176 |
} |
} |
177 |
stop_interlacing_timer(); |
stop_interlacing_timer(); |
195 |
stop_quant_timer(); |
stop_quant_timer(); |
196 |
} |
} |
197 |
|
|
198 |
if (sum >= TOOSMALL_LIMIT) { // skip block ? |
if ((sum >= TOOSMALL_LIMIT) || (qcoeff[i*64] != 0) || |
199 |
|
(qcoeff[i*64+1] != 0) || (qcoeff[i*64+8] != 0)) { |
200 |
|
|
201 |
if (pParam->m_quant_type == H263_QUANT) { |
if (pParam->m_quant_type == H263_QUANT) { |
202 |
start_timer(); |
start_timer(); |
240 |
|
|
241 |
} |
} |
242 |
|
|
243 |
|
void |
244 |
|
MBTransQuantIntra2(const MBParam * pParam, |
245 |
|
FRAMEINFO * frame, |
246 |
|
MACROBLOCK * pMB, |
247 |
|
const uint32_t x_pos, |
248 |
|
const uint32_t y_pos, |
249 |
|
int16_t data[6 * 64], |
250 |
|
int16_t qcoeff[6 * 64]) |
251 |
|
{ |
252 |
|
MBTrans(pParam,frame,pMB,x_pos,y_pos,data); |
253 |
|
MBfDCT(pParam,frame,pMB,data); |
254 |
|
MBQuantIntra(pParam,frame,pMB,data,qcoeff); |
255 |
|
MBDeQuantIntra(pParam,frame->quant,data,qcoeff); |
256 |
|
MBiDCT(data,0x3F); |
257 |
|
MBTransAdd(pParam,frame,pMB,x_pos,y_pos,data,0x3F); |
258 |
|
} |
259 |
|
|
260 |
|
|
261 |
|
uint8_t |
262 |
|
MBTransQuantInter2(const MBParam * pParam, |
263 |
|
FRAMEINFO * frame, |
264 |
|
MACROBLOCK * pMB, |
265 |
|
const uint32_t x_pos, |
266 |
|
const uint32_t y_pos, |
267 |
|
int16_t data[6 * 64], |
268 |
|
int16_t qcoeff[6 * 64]) |
269 |
|
{ |
270 |
|
uint8_t cbp; |
271 |
|
|
272 |
|
/* there is no MBTrans for Inter block, that's done in motion compensation already */ |
273 |
|
|
274 |
|
MBfDCT(pParam,frame,pMB,data); |
275 |
|
cbp = MBQuantInter(pParam,frame->quant,data,qcoeff); |
276 |
|
MBDeQuantInter(pParam,frame->quant,data,qcoeff,cbp); |
277 |
|
MBiDCT(data,cbp); |
278 |
|
MBTransAdd(pParam,frame,pMB,x_pos,y_pos,data,cbp); |
279 |
|
|
280 |
|
return cbp; |
281 |
|
} |
282 |
|
|
283 |
|
uint8_t |
284 |
|
MBTransQuantInterBVOP(const MBParam * pParam, |
285 |
|
FRAMEINFO * frame, |
286 |
|
MACROBLOCK * pMB, |
287 |
|
int16_t data[6 * 64], |
288 |
|
int16_t qcoeff[6 * 64]) |
289 |
|
{ |
290 |
|
uint8_t cbp; |
291 |
|
|
292 |
|
/* there is no MBTrans for Inter block, that's done in motion compensation already */ |
293 |
|
|
294 |
|
MBfDCT(pParam,frame,pMB,data); |
295 |
|
cbp = MBQuantInter(pParam,frame->quant,data,qcoeff); |
296 |
|
|
297 |
|
/* we don't have to DeQuant, iDCT and Transfer back data for B-frames */ |
298 |
|
|
299 |
|
return cbp; |
300 |
|
} |
301 |
|
|
302 |
|
|
303 |
|
void |
304 |
|
MBfDCT(const MBParam * pParam, |
305 |
|
FRAMEINFO * frame, |
306 |
|
MACROBLOCK * pMB, |
307 |
|
int16_t data[6 * 64]) |
308 |
|
{ |
309 |
|
int i; |
310 |
|
|
311 |
|
start_timer(); |
312 |
|
pMB->field_dct = 0; |
313 |
|
if ((frame->global_flags & XVID_INTERLACING)) { |
314 |
|
pMB->field_dct = MBDecideFieldDCT(data); |
315 |
|
} |
316 |
|
stop_interlacing_timer(); |
317 |
|
|
318 |
|
for (i = 0; i < 6; i++) { |
319 |
|
start_timer(); |
320 |
|
fdct(&data[i * 64]); |
321 |
|
stop_dct_timer(); |
322 |
|
} |
323 |
|
} |
324 |
|
|
325 |
|
void |
326 |
|
MBQuantDeQuantIntra(const MBParam * pParam, |
327 |
|
FRAMEINFO * frame, |
328 |
|
MACROBLOCK * pMB, |
329 |
|
int16_t qcoeff[6 * 64], |
330 |
|
int16_t data[6*64]) |
331 |
|
{ |
332 |
|
int i; |
333 |
|
int iQuant = frame->quant; |
334 |
|
|
335 |
|
start_timer(); |
336 |
|
pMB->field_dct = 0; |
337 |
|
if ((frame->global_flags & XVID_INTERLACING)) { |
338 |
|
pMB->field_dct = MBDecideFieldDCT(data); |
339 |
|
} |
340 |
|
stop_interlacing_timer(); |
341 |
|
|
342 |
|
for (i = 0; i < 6; i++) { |
343 |
|
uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
344 |
|
|
345 |
|
if (pParam->m_quant_type == H263_QUANT) { |
346 |
|
start_timer(); |
347 |
|
quant_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler); |
348 |
|
stop_quant_timer(); |
349 |
|
|
350 |
|
start_timer(); |
351 |
|
dequant_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler); |
352 |
|
stop_iquant_timer(); |
353 |
|
} else { |
354 |
|
start_timer(); |
355 |
|
quant4_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler); |
356 |
|
stop_quant_timer(); |
357 |
|
|
358 |
|
start_timer(); |
359 |
|
dequant4_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler); |
360 |
|
stop_iquant_timer(); |
361 |
|
} |
362 |
|
} |
363 |
|
} |
364 |
|
|
365 |
|
void |
366 |
|
MBQuantIntra(const MBParam * pParam, |
367 |
|
FRAMEINFO * frame, |
368 |
|
MACROBLOCK *pMB, |
369 |
|
int16_t data[6 * 64], |
370 |
|
int16_t qcoeff[6 * 64]) |
371 |
|
{ |
372 |
|
int i; |
373 |
|
int iQuant = frame->quant; |
374 |
|
|
375 |
|
start_timer(); |
376 |
|
pMB->field_dct = 0; |
377 |
|
if ((frame->global_flags & XVID_INTERLACING)) { |
378 |
|
pMB->field_dct = MBDecideFieldDCT(data); |
379 |
|
} |
380 |
|
stop_interlacing_timer(); |
381 |
|
|
382 |
|
for (i = 0; i < 6; i++) { |
383 |
|
uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
384 |
|
|
385 |
|
if (pParam->m_quant_type == H263_QUANT) { |
386 |
|
start_timer(); |
387 |
|
quant_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler); |
388 |
|
stop_quant_timer(); |
389 |
|
} else { |
390 |
|
start_timer(); |
391 |
|
quant4_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler); |
392 |
|
stop_quant_timer(); |
393 |
|
} |
394 |
|
} |
395 |
|
} |
396 |
|
|
397 |
|
void |
398 |
|
MBDeQuantIntra(const MBParam * pParam, |
399 |
|
const int iQuant, |
400 |
|
int16_t qcoeff[6 * 64], |
401 |
|
int16_t data[6*64]) |
402 |
|
{ |
403 |
|
int i; |
404 |
|
|
405 |
|
for (i = 0; i < 6; i++) { |
406 |
|
uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
407 |
|
|
408 |
|
if (pParam->m_quant_type == H263_QUANT) { |
409 |
|
start_timer(); |
410 |
|
dequant_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler); |
411 |
|
stop_iquant_timer(); |
412 |
|
} else { |
413 |
|
start_timer(); |
414 |
|
dequant4_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler); |
415 |
|
stop_iquant_timer(); |
416 |
|
} |
417 |
|
} |
418 |
|
} |
419 |
|
|
420 |
|
uint8_t |
421 |
|
MBQuantInter(const MBParam * pParam, |
422 |
|
const int iQuant, |
423 |
|
int16_t data[6 * 64], |
424 |
|
int16_t qcoeff[6 * 64]) |
425 |
|
{ |
426 |
|
|
427 |
|
int i; |
428 |
|
uint8_t cbp = 0; |
429 |
|
int sum; |
430 |
|
|
431 |
|
for (i = 0; i < 6; i++) { |
432 |
|
|
433 |
|
if (pParam->m_quant_type == 0) { |
434 |
|
start_timer(); |
435 |
|
sum = quant_inter(&qcoeff[i * 64], &data[i * 64], iQuant); |
436 |
|
stop_quant_timer(); |
437 |
|
} else { |
438 |
|
start_timer(); |
439 |
|
sum = quant4_inter(&qcoeff[i * 64], &data[i * 64], iQuant); |
440 |
|
stop_quant_timer(); |
441 |
|
} |
442 |
|
|
443 |
|
if (sum >= TOOSMALL_LIMIT) { // skip block ? |
444 |
|
cbp |= 1 << (5 - i); |
445 |
|
} |
446 |
|
} |
447 |
|
return cbp; |
448 |
|
} |
449 |
|
|
450 |
|
void |
451 |
|
MBDeQuantInter( const MBParam * pParam, |
452 |
|
const int iQuant, |
453 |
|
int16_t data[6 * 64], |
454 |
|
int16_t qcoeff[6 * 64], |
455 |
|
const uint8_t cbp) |
456 |
|
{ |
457 |
|
int i; |
458 |
|
|
459 |
|
for (i = 0; i < 6; i++) { |
460 |
|
if (cbp & (1 << (5 - i))) |
461 |
|
{ |
462 |
|
if (pParam->m_quant_type == H263_QUANT) { |
463 |
|
start_timer(); |
464 |
|
dequant_inter(&data[i * 64], &qcoeff[i * 64], iQuant); |
465 |
|
stop_iquant_timer(); |
466 |
|
} else { |
467 |
|
start_timer(); |
468 |
|
dequant4_inter(&data[i * 64], &qcoeff[i * 64], iQuant); |
469 |
|
stop_iquant_timer(); |
470 |
|
} |
471 |
|
} |
472 |
|
} |
473 |
|
} |
474 |
|
|
475 |
|
void |
476 |
|
MBiDCT( int16_t data[6 * 64], |
477 |
|
const uint8_t cbp) |
478 |
|
{ |
479 |
|
int i; |
480 |
|
|
481 |
|
for (i = 0; i < 6; i++) { |
482 |
|
if (cbp & (1 << (5 - i))) |
483 |
|
{ |
484 |
|
start_timer(); |
485 |
|
idct(&data[i * 64]); |
486 |
|
stop_idct_timer(); |
487 |
|
|
488 |
|
} |
489 |
|
} |
490 |
|
} |
491 |
|
|
492 |
|
|
493 |
|
void |
494 |
|
MBTrans(const MBParam * pParam, |
495 |
|
FRAMEINFO * frame, |
496 |
|
MACROBLOCK * pMB, |
497 |
|
const uint32_t x_pos, |
498 |
|
const uint32_t y_pos, |
499 |
|
int16_t data[6 * 64]) |
500 |
|
{ |
501 |
|
uint32_t stride = pParam->edged_width; |
502 |
|
uint32_t stride2 = stride / 2; |
503 |
|
uint32_t next_block = stride * 8; |
504 |
|
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
505 |
|
IMAGE *pCurrent = &frame->image; |
506 |
|
|
507 |
|
pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4); |
508 |
|
pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3); |
509 |
|
pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3); |
510 |
|
|
511 |
|
start_timer(); |
512 |
|
transfer_8to16copy(&data[0 * 64], pY_Cur, stride); |
513 |
|
transfer_8to16copy(&data[1 * 64], pY_Cur + 8, stride); |
514 |
|
transfer_8to16copy(&data[2 * 64], pY_Cur + next_block, stride); |
515 |
|
transfer_8to16copy(&data[3 * 64], pY_Cur + next_block + 8, stride); |
516 |
|
transfer_8to16copy(&data[4 * 64], pU_Cur, stride2); |
517 |
|
transfer_8to16copy(&data[5 * 64], pV_Cur, stride2); |
518 |
|
stop_transfer_timer(); |
519 |
|
} |
520 |
|
|
521 |
|
void |
522 |
|
MBTransAdd(const MBParam * pParam, |
523 |
|
FRAMEINFO * frame, |
524 |
|
MACROBLOCK * pMB, |
525 |
|
const uint32_t x_pos, |
526 |
|
const uint32_t y_pos, |
527 |
|
int16_t data[6 * 64], |
528 |
|
const uint8_t cbp) |
529 |
|
{ |
530 |
|
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
531 |
|
uint32_t stride = pParam->edged_width; |
532 |
|
uint32_t stride2 = stride / 2; |
533 |
|
uint32_t next_block = stride * 8; |
534 |
|
IMAGE *pCurrent = &frame->image; |
535 |
|
|
536 |
|
pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4); |
537 |
|
pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3); |
538 |
|
pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3); |
539 |
|
|
540 |
|
if (pMB->field_dct) { |
541 |
|
next_block = stride; |
542 |
|
stride *= 2; |
543 |
|
} |
544 |
|
|
545 |
|
start_timer(); |
546 |
|
if (cbp & 32) |
547 |
|
transfer_16to8add(pY_Cur, &data[0 * 64], stride); |
548 |
|
if (cbp & 16) |
549 |
|
transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride); |
550 |
|
if (cbp & 8) |
551 |
|
transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride); |
552 |
|
if (cbp & 4) |
553 |
|
transfer_16to8add(pY_Cur + next_block + 8, &data[3 * 64], stride); |
554 |
|
if (cbp & 2) |
555 |
|
transfer_16to8add(pU_Cur, &data[4 * 64], stride2); |
556 |
|
if (cbp & 1) |
557 |
|
transfer_16to8add(pV_Cur, &data[5 * 64], stride2); |
558 |
|
stop_transfer_timer(); |
559 |
|
} |
560 |
|
|
561 |
|
|
562 |
|
|
563 |
/* if sum(diff between field lines) < sum(diff between frame lines), use field dct */ |
/* if sum(diff between field lines) < sum(diff between frame lines), use field dct */ |
564 |
|
|
600 |
} |
} |
601 |
} |
} |
602 |
|
|
603 |
if (frame > field) { |
if (frame > (field + 350)) { |
604 |
MBFrameToField(data); |
MBFrameToField(data); |
605 |
} |
} |
606 |
|
|
607 |
return (frame > field); |
return (frame > (field + 350)); |
608 |
} |
} |
609 |
|
|
610 |
|
|