1 |
/***************************************************************************** |
/***************************************************************************** |
2 |
* |
* |
3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* - MacroBlock transfer and quantization - |
* - MB Transfert/Quantization functions - |
5 |
* |
* |
6 |
* Copyright(C) 2002-2001 Michael Militzer <isibaar@xvid.org> |
* Copyright(C) 2001-2003 Peter Ross <pross@xvid.org> |
7 |
* 2002-2001 Peter Ross <pross@xvid.org> |
* 2001-2003 Michael Militzer <isibaar@xvid.org> |
8 |
* |
* 2003 Edouard Gomez <ed.gomez@free.fr> |
|
* This program is an implementation of a part of one or more MPEG-4 |
|
|
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
|
|
* to use this software module in hardware or software products are |
|
|
* advised that its use may infringe existing patents or copyrights, and |
|
|
* any such use would be at such party's own risk. The original |
|
|
* developer of this software module and his/her company, and subsequent |
|
|
* editors and their companies, will have no liability for use of this |
|
|
* software or modifications or derivatives thereof. |
|
9 |
* |
* |
10 |
* This program is free software; you can redistribute it and/or modify |
* This program is free software; you can redistribute it and/or modify |
11 |
* it under the terms of the GNU General Public License as published by |
* it under the terms of the GNU General Public License as published by |
21 |
* along with this program; if not, write to the Free Software |
* along with this program; if not, write to the Free Software |
22 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
23 |
* |
* |
24 |
* $Id: mbtransquant.c,v 1.15 2002-10-07 08:11:48 chl Exp $ |
* $Id: mbtransquant.c,v 1.21.2.6 2003-03-30 13:16:41 edgomez Exp $ |
25 |
* |
* |
26 |
****************************************************************************/ |
****************************************************************************/ |
27 |
|
|
39 |
#include "../quant/quant_h263.h" |
#include "../quant/quant_h263.h" |
40 |
#include "../encoder.h" |
#include "../encoder.h" |
41 |
|
|
42 |
#define MIN(X, Y) ((X)<(Y)?(X):(Y)) |
#include "../image/reduced.h" |
|
#define MAX(X, Y) ((X)>(Y)?(X):(Y)) |
|
|
|
|
|
#define TOOSMALL_LIMIT 3 /* skip blocks having a coefficient sum below this value */ |
|
|
|
|
|
/* this isnt pretty, but its better than 20 ifdefs */ |
|
|
|
|
|
void |
|
|
MBTransQuantIntra(const MBParam * pParam, |
|
|
FRAMEINFO * frame, |
|
|
MACROBLOCK * pMB, |
|
|
const uint32_t x_pos, |
|
|
const uint32_t y_pos, |
|
|
int16_t data[6 * 64], |
|
|
int16_t qcoeff[6 * 64]) |
|
|
{ |
|
|
|
|
|
uint32_t stride = pParam->edged_width; |
|
|
uint32_t stride2 = stride / 2; |
|
|
uint32_t next_block = stride * 8; |
|
|
uint32_t i; |
|
|
uint32_t iQuant = frame->quant; |
|
|
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
|
|
IMAGE *pCurrent = &frame->image; |
|
|
|
|
|
pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4); |
|
|
pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3); |
|
|
pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3); |
|
|
|
|
|
start_timer(); |
|
|
transfer_8to16copy(&data[0 * 64], pY_Cur, stride); |
|
|
transfer_8to16copy(&data[1 * 64], pY_Cur + 8, stride); |
|
|
transfer_8to16copy(&data[2 * 64], pY_Cur + next_block, stride); |
|
|
transfer_8to16copy(&data[3 * 64], pY_Cur + next_block + 8, stride); |
|
|
transfer_8to16copy(&data[4 * 64], pU_Cur, stride2); |
|
|
transfer_8to16copy(&data[5 * 64], pV_Cur, stride2); |
|
|
stop_transfer_timer(); |
|
|
|
|
|
start_timer(); |
|
|
pMB->field_dct = 0; |
|
|
if ((frame->global_flags & XVID_INTERLACING) && |
|
|
(x_pos>0) && (x_pos<pParam->mb_width-1) && |
|
|
(y_pos>0) && (y_pos<pParam->mb_height-1)) { |
|
|
pMB->field_dct = MBDecideFieldDCT(data); |
|
|
} |
|
|
stop_interlacing_timer(); |
|
|
|
|
|
for (i = 0; i < 6; i++) { |
|
|
uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
|
|
|
|
|
start_timer(); |
|
|
fdct(&data[i * 64]); |
|
|
stop_dct_timer(); |
|
|
|
|
|
if (pParam->m_quant_type == H263_QUANT) { |
|
|
start_timer(); |
|
|
quant_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler); |
|
|
stop_quant_timer(); |
|
|
|
|
|
start_timer(); |
|
|
dequant_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler); |
|
|
stop_iquant_timer(); |
|
|
} else { |
|
|
start_timer(); |
|
|
quant4_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler); |
|
|
stop_quant_timer(); |
|
|
|
|
|
start_timer(); |
|
|
dequant4_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler); |
|
|
stop_iquant_timer(); |
|
|
} |
|
|
|
|
|
start_timer(); |
|
|
idct(&data[i * 64]); |
|
|
stop_idct_timer(); |
|
|
} |
|
|
|
|
|
if (pMB->field_dct) { |
|
|
next_block = stride; |
|
|
stride *= 2; |
|
|
} |
|
|
|
|
|
start_timer(); |
|
|
transfer_16to8copy(pY_Cur, &data[0 * 64], stride); |
|
|
transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride); |
|
|
transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride); |
|
|
transfer_16to8copy(pY_Cur + next_block + 8, &data[3 * 64], stride); |
|
|
transfer_16to8copy(pU_Cur, &data[4 * 64], stride2); |
|
|
transfer_16to8copy(pV_Cur, &data[5 * 64], stride2); |
|
|
stop_transfer_timer(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
uint8_t |
|
|
MBTransQuantInter(const MBParam * pParam, |
|
|
FRAMEINFO * frame, |
|
|
MACROBLOCK * pMB, |
|
|
const uint32_t x_pos, |
|
|
const uint32_t y_pos, |
|
|
int16_t data[6 * 64], |
|
|
int16_t qcoeff[6 * 64]) |
|
|
{ |
|
43 |
|
|
44 |
uint32_t stride = pParam->edged_width; |
MBFIELDTEST_PTR MBFieldTest; |
|
uint32_t stride2 = stride / 2; |
|
|
uint32_t next_block = stride * 8; |
|
|
uint32_t i; |
|
|
uint32_t iQuant = frame->quant; |
|
|
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
|
|
uint8_t cbp = 0; |
|
|
uint32_t sum; |
|
|
IMAGE *pCurrent = &frame->image; |
|
|
|
|
|
pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4); |
|
|
pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3); |
|
|
pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3); |
|
|
|
|
|
start_timer(); |
|
|
pMB->field_dct = 0; |
|
|
if ((frame->global_flags & XVID_INTERLACING) && |
|
|
(x_pos>0) && (x_pos<pParam->mb_width-1) && |
|
|
(y_pos>0) && (y_pos<pParam->mb_height-1)) { |
|
|
pMB->field_dct = MBDecideFieldDCT(data); |
|
|
} |
|
|
stop_interlacing_timer(); |
|
45 |
|
|
|
for (i = 0; i < 6; i++) { |
|
46 |
/* |
/* |
47 |
* no need to transfer 8->16-bit |
* Skip blocks having a coefficient sum below this value. This value will be |
48 |
* (this is performed already in motion compensation) |
* corrected according to the MB quantizer to avoid artifacts for quant==1 |
49 |
*/ |
*/ |
50 |
start_timer(); |
#define PVOP_TOOSMALL_LIMIT 1 |
51 |
fdct(&data[i * 64]); |
#define BVOP_TOOSMALL_LIMIT 3 |
|
stop_dct_timer(); |
|
|
|
|
|
if (pParam->m_quant_type == 0) { |
|
|
start_timer(); |
|
|
sum = quant_inter(&qcoeff[i * 64], &data[i * 64], iQuant); |
|
|
stop_quant_timer(); |
|
|
} else { |
|
|
start_timer(); |
|
|
sum = quant4_inter(&qcoeff[i * 64], &data[i * 64], iQuant); |
|
|
stop_quant_timer(); |
|
|
} |
|
|
|
|
|
if ((sum >= TOOSMALL_LIMIT) || (qcoeff[i*64] != 0) || |
|
|
(qcoeff[i*64+1] != 0) || (qcoeff[i*64+8] != 0)) { |
|
|
|
|
|
if (pParam->m_quant_type == H263_QUANT) { |
|
|
start_timer(); |
|
|
dequant_inter(&data[i * 64], &qcoeff[i * 64], iQuant); |
|
|
stop_iquant_timer(); |
|
|
} else { |
|
|
start_timer(); |
|
|
dequant4_inter(&data[i * 64], &qcoeff[i * 64], iQuant); |
|
|
stop_iquant_timer(); |
|
|
} |
|
|
|
|
|
cbp |= 1 << (5 - i); |
|
|
|
|
|
start_timer(); |
|
|
idct(&data[i * 64]); |
|
|
stop_idct_timer(); |
|
|
} |
|
|
} |
|
|
|
|
|
if (pMB->field_dct) { |
|
|
next_block = stride; |
|
|
stride *= 2; |
|
|
} |
|
|
|
|
|
start_timer(); |
|
|
if (cbp & 32) |
|
|
transfer_16to8add(pY_Cur, &data[0 * 64], stride); |
|
|
if (cbp & 16) |
|
|
transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride); |
|
|
if (cbp & 8) |
|
|
transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride); |
|
|
if (cbp & 4) |
|
|
transfer_16to8add(pY_Cur + next_block + 8, &data[3 * 64], stride); |
|
|
if (cbp & 2) |
|
|
transfer_16to8add(pU_Cur, &data[4 * 64], stride2); |
|
|
if (cbp & 1) |
|
|
transfer_16to8add(pV_Cur, &data[5 * 64], stride2); |
|
|
stop_transfer_timer(); |
|
|
|
|
|
return cbp; |
|
|
|
|
|
} |
|
|
|
|
|
void |
|
|
MBTransQuantIntra2(const MBParam * pParam, |
|
|
FRAMEINFO * frame, |
|
|
MACROBLOCK * pMB, |
|
|
const uint32_t x_pos, |
|
|
const uint32_t y_pos, |
|
|
int16_t data[6 * 64], |
|
|
int16_t qcoeff[6 * 64]) |
|
|
{ |
|
|
MBTrans(pParam,frame,pMB,x_pos,y_pos,data); |
|
|
MBfDCT(pParam,frame,pMB,data); |
|
|
MBQuantIntra(pParam,frame,pMB,data,qcoeff); |
|
|
MBDeQuantIntra(pParam,frame->quant,data,qcoeff); |
|
|
MBiDCT(data,0x3F); |
|
|
MBTransAdd(pParam,frame,pMB,x_pos,y_pos,data,0x3F); |
|
|
} |
|
52 |
|
|
53 |
|
/***************************************************************************** |
54 |
|
* Local functions |
55 |
|
****************************************************************************/ |
56 |
|
|
57 |
uint8_t |
/* permute block and return field dct choice */ |
58 |
MBTransQuantInter2(const MBParam * pParam, |
static __inline uint32_t |
59 |
FRAMEINFO * frame, |
MBDecideFieldDCT(int16_t data[6 * 64]) |
|
MACROBLOCK * pMB, |
|
|
const uint32_t x_pos, |
|
|
const uint32_t y_pos, |
|
|
int16_t data[6 * 64], |
|
|
int16_t qcoeff[6 * 64]) |
|
|
{ |
|
|
uint8_t cbp; |
|
|
|
|
|
/* there is no MBTrans for Inter block, that's done in motion compensation already */ |
|
|
|
|
|
MBfDCT(pParam,frame,pMB,data); |
|
|
cbp = MBQuantInter(pParam,frame->quant,data,qcoeff); |
|
|
MBDeQuantInter(pParam,frame->quant,data,qcoeff,cbp); |
|
|
MBiDCT(data,cbp); |
|
|
MBTransAdd(pParam,frame,pMB,x_pos,y_pos,data,cbp); |
|
|
|
|
|
return cbp; |
|
|
} |
|
|
|
|
|
uint8_t |
|
|
MBTransQuantInterBVOP(const MBParam * pParam, |
|
|
FRAMEINFO * frame, |
|
|
MACROBLOCK * pMB, |
|
|
int16_t data[6 * 64], |
|
|
int16_t qcoeff[6 * 64]) |
|
60 |
{ |
{ |
61 |
uint8_t cbp; |
uint32_t field = MBFieldTest(data); |
|
|
|
|
/* there is no MBTrans for Inter block, that's done in motion compensation already */ |
|
62 |
|
|
63 |
MBfDCT(pParam,frame,pMB,data); |
if (field) |
64 |
cbp = MBQuantInter(pParam,frame->quant,data,qcoeff); |
MBFrameToField(data); |
|
|
|
|
/* we don't have to DeQuant, iDCT and Transfer back data for B-frames */ |
|
65 |
|
|
66 |
return cbp; |
return field; |
67 |
} |
} |
68 |
|
|
69 |
|
/* Performs Forward DCT on all blocks */ |
70 |
void |
static __inline void |
71 |
MBfDCT(const MBParam * pParam, |
MBfDCT(const MBParam * pParam, |
72 |
FRAMEINFO * frame, |
FRAMEINFO * frame, |
73 |
MACROBLOCK * pMB, |
MACROBLOCK * pMB, |
74 |
|
uint32_t x_pos, |
75 |
|
uint32_t y_pos, |
76 |
int16_t data[6 * 64]) |
int16_t data[6 * 64]) |
77 |
{ |
{ |
78 |
int i; |
/* Handles interlacing */ |
|
|
|
79 |
start_timer(); |
start_timer(); |
80 |
pMB->field_dct = 0; |
pMB->field_dct = 0; |
81 |
if ((frame->global_flags & XVID_INTERLACING)) { |
if ((frame->vol_flags & XVID_VOL_INTERLACING) && |
82 |
|
(x_pos>0) && (x_pos<pParam->mb_width-1) && |
83 |
|
(y_pos>0) && (y_pos<pParam->mb_height-1)) { |
84 |
pMB->field_dct = MBDecideFieldDCT(data); |
pMB->field_dct = MBDecideFieldDCT(data); |
85 |
} |
} |
86 |
stop_interlacing_timer(); |
stop_interlacing_timer(); |
87 |
|
|
88 |
for (i = 0; i < 6; i++) { |
/* Perform DCT */ |
89 |
start_timer(); |
start_timer(); |
90 |
fdct(&data[i * 64]); |
fdct(&data[0 * 64]); |
91 |
|
fdct(&data[1 * 64]); |
92 |
|
fdct(&data[2 * 64]); |
93 |
|
fdct(&data[3 * 64]); |
94 |
|
fdct(&data[4 * 64]); |
95 |
|
fdct(&data[5 * 64]); |
96 |
stop_dct_timer(); |
stop_dct_timer(); |
97 |
} |
} |
|
} |
|
98 |
|
|
99 |
void |
/* Performs Inverse DCT on all blocks */ |
100 |
MBQuantDeQuantIntra(const MBParam * pParam, |
static __inline void |
101 |
FRAMEINFO * frame, |
MBiDCT(int16_t data[6 * 64], |
102 |
MACROBLOCK * pMB, |
const uint8_t cbp) |
|
int16_t qcoeff[6 * 64], |
|
|
int16_t data[6*64]) |
|
103 |
{ |
{ |
|
int i; |
|
|
int iQuant = frame->quant; |
|
|
|
|
|
start_timer(); |
|
|
pMB->field_dct = 0; |
|
|
if ((frame->global_flags & XVID_INTERLACING)) { |
|
|
pMB->field_dct = MBDecideFieldDCT(data); |
|
|
} |
|
|
stop_interlacing_timer(); |
|
|
|
|
|
for (i = 0; i < 6; i++) { |
|
|
uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
|
|
|
|
|
if (pParam->m_quant_type == H263_QUANT) { |
|
|
start_timer(); |
|
|
quant_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler); |
|
|
stop_quant_timer(); |
|
|
|
|
|
start_timer(); |
|
|
dequant_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler); |
|
|
stop_iquant_timer(); |
|
|
} else { |
|
|
start_timer(); |
|
|
quant4_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler); |
|
|
stop_quant_timer(); |
|
|
|
|
104 |
start_timer(); |
start_timer(); |
105 |
dequant4_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler); |
if(cbp & (1 << (5 - 0))) idct(&data[0 * 64]); |
106 |
stop_iquant_timer(); |
if(cbp & (1 << (5 - 1))) idct(&data[1 * 64]); |
107 |
} |
if(cbp & (1 << (5 - 2))) idct(&data[2 * 64]); |
108 |
} |
if(cbp & (1 << (5 - 3))) idct(&data[3 * 64]); |
109 |
|
if(cbp & (1 << (5 - 4))) idct(&data[4 * 64]); |
110 |
|
if(cbp & (1 << (5 - 5))) idct(&data[5 * 64]); |
111 |
|
stop_idct_timer(); |
112 |
} |
} |
113 |
|
|
114 |
void |
/* Quantize all blocks -- Intra mode */ |
115 |
|
static __inline void |
116 |
MBQuantIntra(const MBParam * pParam, |
MBQuantIntra(const MBParam * pParam, |
117 |
FRAMEINFO * frame, |
const MACROBLOCK * pMB, |
118 |
MACROBLOCK *pMB, |
int16_t qcoeff[6 * 64], |
119 |
int16_t data[6*64], |
int16_t data[6*64]) |
|
int16_t qcoeff[6 * 64]) |
|
|
) |
|
120 |
{ |
{ |
121 |
int i; |
int i; |
|
int iQuant = frame->quant; |
|
|
|
|
|
start_timer(); |
|
|
pMB->field_dct = 0; |
|
|
if ((frame->global_flags & XVID_INTERLACING)) { |
|
|
pMB->field_dct = MBDecideFieldDCT(data); |
|
|
} |
|
|
stop_interlacing_timer(); |
|
122 |
|
|
123 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
124 |
uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
uint32_t iDcScaler = get_dc_scaler(pMB->quant, i < 4); |
125 |
|
|
126 |
if (pParam->m_quant_type == H263_QUANT) { |
/* Quantize the block */ |
127 |
start_timer(); |
start_timer(); |
128 |
quant_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler); |
if (!(pParam->vol_flags & XVID_VOL_MPEGQUANT)) |
129 |
stop_quant_timer(); |
quant_intra(&data[i * 64], &qcoeff[i * 64], pMB->quant, iDcScaler); |
130 |
} else { |
else |
131 |
start_timer(); |
quant4_intra(&data[i * 64], &qcoeff[i * 64], pMB->quant, iDcScaler); |
|
quant4_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler); |
|
132 |
stop_quant_timer(); |
stop_quant_timer(); |
133 |
} |
} |
134 |
} |
} |
|
} |
|
135 |
|
|
136 |
void |
/* DeQuantize all blocks -- Intra mode */ |
137 |
|
static __inline void |
138 |
MBDeQuantIntra(const MBParam * pParam, |
MBDeQuantIntra(const MBParam * pParam, |
139 |
const int iQuant, |
const int iQuant, |
140 |
int16_t qcoeff[6 * 64], |
int16_t qcoeff[6 * 64], |
145 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
146 |
uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
147 |
|
|
|
if (pParam->m_quant_type == H263_QUANT) { |
|
148 |
start_timer(); |
start_timer(); |
149 |
dequant_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler); |
if (!(pParam->vol_flags & XVID_VOL_MPEGQUANT)) |
150 |
stop_iquant_timer(); |
dequant_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler); |
151 |
} else { |
else |
152 |
start_timer(); |
dequant4_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler); |
|
dequant4_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler); |
|
153 |
stop_iquant_timer(); |
stop_iquant_timer(); |
154 |
} |
} |
155 |
} |
} |
|
} |
|
156 |
|
|
157 |
uint8_t |
/* Quantize all blocks -- Inter mode */ |
158 |
|
static __inline uint8_t |
159 |
MBQuantInter(const MBParam * pParam, |
MBQuantInter(const MBParam * pParam, |
160 |
const int iQuant, |
const MACROBLOCK * pMB, |
161 |
int16_t data[6 * 64], |
int16_t data[6 * 64], |
162 |
int16_t qcoeff[6 * 64]) |
int16_t qcoeff[6 * 64], |
163 |
|
int bvop, |
164 |
|
int limit) |
165 |
{ |
{ |
166 |
|
|
167 |
int i; |
int i; |
168 |
uint8_t cbp = 0; |
uint8_t cbp = 0; |
169 |
int sum; |
int sum; |
170 |
|
int code_block; |
171 |
|
|
172 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
173 |
|
|
174 |
if (pParam->m_quant_type == 0) { |
/* Quantize the block */ |
175 |
start_timer(); |
start_timer(); |
176 |
sum = quant_inter(&qcoeff[i * 64], &data[i * 64], iQuant); |
if (!(pParam->vol_flags & XVID_VOL_MPEGQUANT)) |
177 |
|
sum = quant_inter(&qcoeff[i * 64], &data[i * 64], pMB->quant); |
178 |
|
else |
179 |
|
sum = quant4_inter(&qcoeff[i * 64], &data[i * 64], pMB->quant); |
180 |
stop_quant_timer(); |
stop_quant_timer(); |
181 |
|
|
182 |
|
/* |
183 |
|
* We code the block if the sum is higher than the limit and if the first |
184 |
|
* two AC coefficients in zig zag order are not zero. |
185 |
|
*/ |
186 |
|
code_block = 0; |
187 |
|
if ((sum >= limit) || (qcoeff[i*64+1] != 0) || (qcoeff[i*64+8] != 0)) { |
188 |
|
code_block = 1; |
189 |
} else { |
} else { |
|
start_timer(); |
|
|
sum = quant4_inter(&qcoeff[i * 64], &data[i * 64], iQuant); |
|
|
stop_quant_timer(); |
|
|
} |
|
190 |
|
|
191 |
if (sum >= TOOSMALL_LIMIT) { // skip block ? |
if (bvop && (pMB->mode == MODE_DIRECT || pMB->mode == MODE_DIRECT_NO4V)) { |
192 |
cbp |= 1 << (5 - i); |
/* dark blocks prevention for direct mode */ |
193 |
|
if ((qcoeff[i*64] < -1) || (qcoeff[i*64] > 0)) |
194 |
|
code_block = 1; |
195 |
|
} else { |
196 |
|
/* not direct mode */ |
197 |
|
if (qcoeff[i*64] != 0) |
198 |
|
code_block = 1; |
199 |
} |
} |
200 |
} |
} |
201 |
return cbp; |
|
202 |
|
/* Set the corresponding cbp bit */ |
203 |
|
cbp |= code_block << (5 - i); |
204 |
|
|
205 |
} |
} |
206 |
|
|
207 |
void |
return(cbp); |
208 |
|
} |
209 |
|
|
210 |
|
/* DeQuantize all blocks -- Inter mode */ |
211 |
|
static __inline void |
212 |
MBDeQuantInter( const MBParam * pParam, |
MBDeQuantInter( const MBParam * pParam, |
213 |
const int iQuant, |
const int iQuant, |
214 |
int16_t data[6 * 64], |
int16_t data[6 * 64], |
218 |
int i; |
int i; |
219 |
|
|
220 |
for (i = 0; i < 6; i++) { |
for (i = 0; i < 6; i++) { |
221 |
if (cbp & (1 << (5 - i))) |
if (cbp & (1 << (5 - i))) { |
|
{ |
|
|
if (pParam->m_quant_type == H263_QUANT) { |
|
222 |
start_timer(); |
start_timer(); |
223 |
|
if (!(pParam->vol_flags & XVID_VOL_MPEGQUANT)) |
224 |
dequant_inter(&data[i * 64], &qcoeff[i * 64], iQuant); |
dequant_inter(&data[i * 64], &qcoeff[i * 64], iQuant); |
225 |
stop_iquant_timer(); |
else |
|
} else { |
|
|
start_timer(); |
|
226 |
dequant4_inter(&data[i * 64], &qcoeff[i * 64], iQuant); |
dequant4_inter(&data[i * 64], &qcoeff[i * 64], iQuant); |
227 |
stop_iquant_timer(); |
stop_iquant_timer(); |
228 |
} |
} |
229 |
} |
} |
230 |
} |
} |
|
} |
|
231 |
|
|
232 |
void |
typedef void (transfer_operation_8to16_t) (int16_t *Dst, const uint8_t *Src, int BpS); |
233 |
MBiDCT( int16_t data[6 * 64], |
typedef void (transfer_operation_16to8_t) (uint8_t *Dst, const int16_t *Src, int BpS); |
|
const uint8_t cbp) |
|
|
{ |
|
|
int i; |
|
234 |
|
|
|
for (i = 0; i < 6; i++) { |
|
|
if (cbp & (1 << (5 - i))) |
|
|
{ |
|
|
start_timer(); |
|
|
idct(&data[i * 64]); |
|
|
stop_idct_timer(); |
|
235 |
|
|
236 |
} |
static __inline void |
237 |
} |
MBTrans8to16(const MBParam * pParam, |
|
} |
|
|
|
|
|
|
|
|
void |
|
|
MBTrans(const MBParam * pParam, |
|
238 |
FRAMEINFO * frame, |
FRAMEINFO * frame, |
239 |
MACROBLOCK * pMB, |
MACROBLOCK * pMB, |
240 |
const uint32_t x_pos, |
const uint32_t x_pos, |
244 |
uint32_t stride = pParam->edged_width; |
uint32_t stride = pParam->edged_width; |
245 |
uint32_t stride2 = stride / 2; |
uint32_t stride2 = stride / 2; |
246 |
uint32_t next_block = stride * 8; |
uint32_t next_block = stride * 8; |
247 |
|
int32_t cst; |
248 |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
249 |
IMAGE *pCurrent = &frame->image; |
IMAGE *pCurrent = &frame->image; |
250 |
|
transfer_operation_8to16_t *transfer_op = NULL; |
251 |
|
|
252 |
|
if ((frame->vop_flags & XVID_VOP_REDUCED)) { |
253 |
|
|
254 |
|
/* Image pointers */ |
255 |
|
pY_Cur = pCurrent->y + (y_pos << 5) * stride + (x_pos << 5); |
256 |
|
pU_Cur = pCurrent->u + (y_pos << 4) * stride2 + (x_pos << 4); |
257 |
|
pV_Cur = pCurrent->v + (y_pos << 4) * stride2 + (x_pos << 4); |
258 |
|
|
259 |
|
/* Block size */ |
260 |
|
cst = 16; |
261 |
|
|
262 |
|
/* Operation function */ |
263 |
|
transfer_op = (transfer_operation_8to16_t*)filter_18x18_to_8x8; |
264 |
|
} else { |
265 |
|
|
266 |
|
/* Image pointers */ |
267 |
pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4); |
pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4); |
268 |
pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3); |
pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3); |
269 |
pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3); |
pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3); |
270 |
|
|
271 |
|
/* Block size */ |
272 |
|
cst = 8; |
273 |
|
|
274 |
|
/* Operation function */ |
275 |
|
transfer_op = (transfer_operation_8to16_t*)transfer_8to16copy; |
276 |
|
} |
277 |
|
|
278 |
|
/* Do the transfer */ |
279 |
start_timer(); |
start_timer(); |
280 |
transfer_8to16copy(&data[0 * 64], pY_Cur, stride); |
transfer_op(&data[0 * 64], pY_Cur, stride); |
281 |
transfer_8to16copy(&data[1 * 64], pY_Cur + 8, stride); |
transfer_op(&data[1 * 64], pY_Cur + cst, stride); |
282 |
transfer_8to16copy(&data[2 * 64], pY_Cur + next_block, stride); |
transfer_op(&data[2 * 64], pY_Cur + next_block, stride); |
283 |
transfer_8to16copy(&data[3 * 64], pY_Cur + next_block + 8, stride); |
transfer_op(&data[3 * 64], pY_Cur + next_block + cst, stride); |
284 |
transfer_8to16copy(&data[4 * 64], pU_Cur, stride2); |
transfer_op(&data[4 * 64], pU_Cur, stride2); |
285 |
transfer_8to16copy(&data[5 * 64], pV_Cur, stride2); |
transfer_op(&data[5 * 64], pV_Cur, stride2); |
286 |
stop_transfer_timer(); |
stop_transfer_timer(); |
287 |
} |
} |
288 |
|
|
289 |
void |
static __inline void |
290 |
MBTransAdd(const MBParam * pParam, |
MBTrans16to8(const MBParam * pParam, |
291 |
FRAMEINFO * frame, |
FRAMEINFO * frame, |
292 |
MACROBLOCK * pMB, |
MACROBLOCK * pMB, |
293 |
const uint32_t x_pos, |
const uint32_t x_pos, |
294 |
const uint32_t y_pos, |
const uint32_t y_pos, |
295 |
int16_t data[6 * 64], |
int16_t data[6 * 64], |
296 |
|
const uint32_t add, |
297 |
const uint8_t cbp) |
const uint8_t cbp) |
298 |
{ |
{ |
299 |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
300 |
uint32_t stride = pParam->edged_width; |
uint32_t stride = pParam->edged_width; |
301 |
uint32_t stride2 = stride / 2; |
uint32_t stride2 = stride / 2; |
302 |
uint32_t next_block = stride * 8; |
uint32_t next_block = stride * 8; |
303 |
|
uint32_t cst; |
304 |
IMAGE *pCurrent = &frame->image; |
IMAGE *pCurrent = &frame->image; |
305 |
|
transfer_operation_16to8_t *transfer_op = NULL; |
|
pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4); |
|
|
pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3); |
|
|
pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3); |
|
306 |
|
|
307 |
if (pMB->field_dct) { |
if (pMB->field_dct) { |
308 |
next_block = stride; |
next_block = stride; |
309 |
stride *= 2; |
stride *= 2; |
310 |
} |
} |
311 |
|
|
312 |
start_timer(); |
if ((frame->vop_flags & XVID_VOP_REDUCED)) { |
313 |
if (cbp & 32) |
|
314 |
transfer_16to8add(pY_Cur, &data[0 * 64], stride); |
/* Image pointers */ |
315 |
if (cbp & 16) |
pY_Cur = pCurrent->y + (y_pos << 5) * stride + (x_pos << 5); |
316 |
transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride); |
pU_Cur = pCurrent->u + (y_pos << 4) * stride2 + (x_pos << 4); |
317 |
if (cbp & 8) |
pV_Cur = pCurrent->v + (y_pos << 4) * stride2 + (x_pos << 4); |
318 |
transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride); |
|
319 |
if (cbp & 4) |
/* Block size */ |
320 |
transfer_16to8add(pY_Cur + next_block + 8, &data[3 * 64], stride); |
cst = 16; |
321 |
if (cbp & 2) |
|
322 |
transfer_16to8add(pU_Cur, &data[4 * 64], stride2); |
/* Operation function */ |
323 |
if (cbp & 1) |
if(add) |
324 |
transfer_16to8add(pV_Cur, &data[5 * 64], stride2); |
transfer_op = (transfer_operation_16to8_t*)add_upsampled_8x8_16to8; |
325 |
|
else |
326 |
|
transfer_op = (transfer_operation_16to8_t*)copy_upsampled_8x8_16to8; |
327 |
|
} else { |
328 |
|
|
329 |
|
/* Image pointers */ |
330 |
|
pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4); |
331 |
|
pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3); |
332 |
|
pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3); |
333 |
|
|
334 |
|
/* Block size */ |
335 |
|
cst = 8; |
336 |
|
|
337 |
|
/* Operation function */ |
338 |
|
if(add) |
339 |
|
transfer_op = (transfer_operation_16to8_t*)transfer_16to8add; |
340 |
|
else |
341 |
|
transfer_op = (transfer_operation_16to8_t*)transfer_16to8copy; |
342 |
|
} |
343 |
|
|
344 |
|
/* Do the operation */ |
345 |
|
start_timer(); |
346 |
|
if (cbp&32) transfer_op(pY_Cur, &data[0 * 64], stride); |
347 |
|
if (cbp&16) transfer_op(pY_Cur + cst, &data[1 * 64], stride); |
348 |
|
if (cbp& 8) transfer_op(pY_Cur + next_block, &data[2 * 64], stride); |
349 |
|
if (cbp& 4) transfer_op(pY_Cur + next_block + cst, &data[3 * 64], stride); |
350 |
|
if (cbp& 2) transfer_op(pU_Cur, &data[4 * 64], stride2); |
351 |
|
if (cbp& 1) transfer_op(pV_Cur, &data[5 * 64], stride2); |
352 |
stop_transfer_timer(); |
stop_transfer_timer(); |
353 |
} |
} |
354 |
|
|
355 |
|
/***************************************************************************** |
356 |
|
* Module functions |
357 |
|
****************************************************************************/ |
358 |
|
|
359 |
|
void |
360 |
|
MBTransQuantIntra(const MBParam * pParam, |
361 |
|
FRAMEINFO * frame, |
362 |
|
MACROBLOCK * pMB, |
363 |
|
const uint32_t x_pos, |
364 |
|
const uint32_t y_pos, |
365 |
|
int16_t data[6 * 64], |
366 |
|
int16_t qcoeff[6 * 64]) |
367 |
|
{ |
368 |
|
|
369 |
|
/* Transfer data */ |
370 |
|
MBTrans8to16(pParam, frame, pMB, x_pos, y_pos, data); |
371 |
|
|
372 |
|
/* Perform DCT (and field decision) */ |
373 |
|
MBfDCT(pParam, frame, pMB, x_pos, y_pos, data); |
374 |
|
|
375 |
/* if sum(diff between field lines) < sum(diff between frame lines), use field dct */ |
/* Quantize the block */ |
376 |
|
MBQuantIntra(pParam, pMB, data, qcoeff); |
377 |
|
|
378 |
|
/* DeQuantize the block */ |
379 |
|
MBDeQuantIntra(pParam, pMB->quant, data, qcoeff); |
380 |
|
|
381 |
uint32_t |
/* Perform inverse DCT*/ |
382 |
MBDecideFieldDCT(int16_t data[6 * 64]) |
MBiDCT(data, 0x3F); |
383 |
|
|
384 |
|
/* Transfer back the data -- Don't add data */ |
385 |
|
MBTrans16to8(pParam, frame, pMB, x_pos, y_pos, data, 0, 0x3F); |
386 |
|
} |
387 |
|
|
388 |
|
|
389 |
|
uint8_t |
390 |
|
MBTransQuantInter(const MBParam * pParam, |
391 |
|
FRAMEINFO * frame, |
392 |
|
MACROBLOCK * pMB, |
393 |
|
const uint32_t x_pos, |
394 |
|
const uint32_t y_pos, |
395 |
|
int16_t data[6 * 64], |
396 |
|
int16_t qcoeff[6 * 64]) |
397 |
|
{ |
398 |
|
uint8_t cbp; |
399 |
|
uint32_t limit; |
400 |
|
|
401 |
|
/* |
402 |
|
* There is no MBTrans8to16 for Inter block, that's done in motion compensation |
403 |
|
* already |
404 |
|
*/ |
405 |
|
|
406 |
|
/* Perform DCT (and field decision) */ |
407 |
|
MBfDCT(pParam, frame, pMB, x_pos, y_pos, data); |
408 |
|
|
409 |
|
/* Set the limit threshold */ |
410 |
|
limit = PVOP_TOOSMALL_LIMIT + ((pMB->quant == 1)? 1 : 0); |
411 |
|
|
412 |
|
/* Quantize the block */ |
413 |
|
cbp = MBQuantInter(pParam, pMB, data, qcoeff, 0, limit); |
414 |
|
|
415 |
|
/* DeQuantize the block */ |
416 |
|
MBDeQuantInter(pParam, pMB->quant, data, qcoeff, cbp); |
417 |
|
|
418 |
|
/* Perform inverse DCT*/ |
419 |
|
MBiDCT(data, cbp); |
420 |
|
|
421 |
|
/* Transfer back the data -- Add the data */ |
422 |
|
MBTrans16to8(pParam, frame, pMB, x_pos, y_pos, data, 1, cbp); |
423 |
|
|
424 |
|
return(cbp); |
425 |
|
} |
426 |
|
|
427 |
|
uint8_t |
428 |
|
MBTransQuantInterBVOP(const MBParam * pParam, |
429 |
|
FRAMEINFO * frame, |
430 |
|
MACROBLOCK * pMB, |
431 |
|
const uint32_t x_pos, |
432 |
|
const uint32_t y_pos, |
433 |
|
int16_t data[6 * 64], |
434 |
|
int16_t qcoeff[6 * 64]) |
435 |
{ |
{ |
436 |
|
uint8_t cbp; |
437 |
|
uint32_t limit; |
438 |
|
|
439 |
|
/* |
440 |
|
* There is no MBTrans8to16 for Inter block, that's done in motion compensation |
441 |
|
* already |
442 |
|
*/ |
443 |
|
|
444 |
|
/* Perform DCT (and field decision) */ |
445 |
|
MBfDCT(pParam, frame, pMB, x_pos, y_pos, data); |
446 |
|
|
447 |
|
/* Set the limit threshold */ |
448 |
|
limit = BVOP_TOOSMALL_LIMIT; |
449 |
|
|
450 |
|
/* Quantize the block */ |
451 |
|
cbp = MBQuantInter(pParam, pMB, data, qcoeff, 1, limit); |
452 |
|
|
453 |
|
/* |
454 |
|
* History comment: |
455 |
|
* We don't have to DeQuant, iDCT and Transfer back data for B-frames. |
456 |
|
* |
457 |
|
* BUT some plugins require the original frame to be passed so we have |
458 |
|
* to take care of that here |
459 |
|
*/ |
460 |
|
if((pParam->plugin_flags & XVID_REQORIGINAL)) { |
461 |
|
|
462 |
|
/* DeQuantize the block */ |
463 |
|
MBDeQuantInter(pParam, pMB->quant, data, qcoeff, cbp); |
464 |
|
|
465 |
|
/* Perform inverse DCT*/ |
466 |
|
MBiDCT(data, cbp); |
467 |
|
|
468 |
|
/* Transfer back the data -- Add the data */ |
469 |
|
MBTrans16to8(pParam, frame, pMB, x_pos, y_pos, data, 1, cbp); |
470 |
|
} |
471 |
|
|
472 |
|
return(cbp); |
473 |
|
} |
474 |
|
|
475 |
|
/* if sum(diff between field lines) < sum(diff between frame lines), use field dct */ |
476 |
|
uint32_t |
477 |
|
MBFieldTest_c(int16_t data[6 * 64]) |
478 |
|
{ |
479 |
const uint8_t blocks[] = |
const uint8_t blocks[] = |
480 |
{ 0 * 64, 0 * 64, 0 * 64, 0 * 64, 2 * 64, 2 * 64, 2 * 64, 2 * 64 }; |
{ 0 * 64, 0 * 64, 0 * 64, 0 * 64, 2 * 64, 2 * 64, 2 * 64, 2 * 64 }; |
481 |
const uint8_t lines[] = { 0, 16, 32, 48, 0, 16, 32, 48 }; |
const uint8_t lines[] = { 0, 16, 32, 48, 0, 16, 32, 48 }; |
509 |
} |
} |
510 |
} |
} |
511 |
|
|
512 |
if (frame > field) { |
return (frame >= (field + 350)); |
|
MBFrameToField(data); |
|
|
} |
|
|
|
|
|
return (frame > (field + 350)); |
|
513 |
} |
} |
514 |
|
|
515 |
|
|