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 Michael Militzer <isibaar@xvid.org> |
7 |
* software module in hardware or software products are advised that its * |
* 2002-2001 Peter Ross <pross@xvid.org> |
8 |
* use may infringe existing patents or copyrights, and any such use * |
* |
9 |
* would be at such party's own risk. The original developer of this * |
* This program is an implementation of a part of one or more MPEG-4 |
10 |
* software module and his/her company, and subsequent editors and their * |
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
11 |
* companies, will have no liability for use of this software or * |
* to use this software module in hardware or software products are |
12 |
* modifications or derivatives thereof. * |
* advised that its use may infringe existing patents or copyrights, and |
13 |
* * |
* any such use would be at such party's own risk. The original |
14 |
* XviD is free software; you can redistribute it and/or modify it * |
* developer of this software module and his/her company, and subsequent |
15 |
* under the terms of the GNU General Public License as published by * |
* editors and their companies, will have no liability for use of this |
16 |
* the Free Software Foundation; either version 2 of the License, or * |
* software or modifications or derivatives thereof. |
17 |
* (at your option) any later version. * |
* |
18 |
* * |
* This program is free software; you can redistribute it and/or modify |
19 |
* XviD is distributed in the hope that it will be useful, but * |
* it under the terms of the GNU General Public License as published by |
20 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
* the Free Software Foundation; either version 2 of the License, or |
21 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
* (at your option) any later version. |
22 |
* GNU General Public License for more details. * |
* |
23 |
* * |
* This program is distributed in the hope that it will be useful, |
24 |
* You should have received a copy of the GNU General Public License * |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
25 |
* along with this program; if not, write to the Free Software * |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
26 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
* GNU General Public License for more details. |
27 |
* * |
* |
28 |
******************************************************************************/ |
* You should have received a copy of the GNU General Public License |
29 |
|
* along with this program; if not, write to the Free Software |
30 |
/****************************************************************************** |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
31 |
* * |
* |
32 |
* mbtransquant.c * |
* $Id: mbtransquant.c,v 1.14 2002-09-26 04:52:16 h Exp $ |
33 |
* * |
* |
34 |
* Copyright (C) 2001 - Peter Ross <pross@cs.rmit.edu.au> * |
****************************************************************************/ |
35 |
* Copyright (C) 2001 - Michael Militzer <isibaar@xvid.org> * |
|
36 |
* * |
#include <string.h> |
|
* For more information visit the XviD homepage: http://www.xvid.org * |
|
|
* * |
|
|
******************************************************************************/ |
|
|
|
|
|
/****************************************************************************** |
|
|
* * |
|
|
* Revision history: * |
|
|
* * |
|
|
* 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 "../portab.h" |
#include "../portab.h" |
39 |
#include "mbfunctions.h" |
#include "mbfunctions.h" |
50 |
#define MIN(X, Y) ((X)<(Y)?(X):(Y)) |
#define MIN(X, Y) ((X)<(Y)?(X):(Y)) |
51 |
#define MAX(X, Y) ((X)>(Y)?(X):(Y)) |
#define MAX(X, Y) ((X)>(Y)?(X):(Y)) |
52 |
|
|
53 |
#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 */ |
54 |
|
|
55 |
/* this isnt pretty, but its better than 20 ifdefs */ |
/* this isnt pretty, but its better than 20 ifdefs */ |
56 |
|
|
57 |
void MBTransQuantIntra(const MBParam *pParam, |
void |
58 |
|
MBTransQuantIntra(const MBParam * pParam, |
59 |
|
FRAMEINFO * frame, |
60 |
MACROBLOCK * pMB, |
MACROBLOCK * pMB, |
61 |
const uint32_t x_pos, |
const uint32_t x_pos, |
62 |
const uint32_t y_pos, |
const uint32_t y_pos, |
63 |
int16_t data[][64], |
int16_t data[6 * 64], |
64 |
int16_t qcoeff[][64], |
int16_t qcoeff[6 * 64]) |
|
IMAGE * const pCurrent) |
|
|
|
|
65 |
{ |
{ |
66 |
const uint32_t stride = pParam->edged_width; |
|
67 |
|
uint32_t stride = pParam->edged_width; |
68 |
|
uint32_t stride2 = stride / 2; |
69 |
|
uint32_t next_block = stride * 8; |
70 |
uint32_t i; |
uint32_t i; |
71 |
uint32_t iQuant = pParam->quant; |
uint32_t iQuant = frame->quant; |
72 |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
73 |
|
IMAGE *pCurrent = &frame->image; |
74 |
|
|
75 |
pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4); |
pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4); |
76 |
pU_Cur = pCurrent->u + (y_pos << 3) * (stride >> 1) + (x_pos << 3); |
pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3); |
77 |
pV_Cur = pCurrent->v + (y_pos << 3) * (stride >> 1) + (x_pos << 3); |
pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3); |
78 |
|
|
79 |
start_timer(); |
start_timer(); |
80 |
transfer_8to16copy(data[0], pY_Cur, stride); |
transfer_8to16copy(&data[0 * 64], pY_Cur, stride); |
81 |
transfer_8to16copy(data[1], pY_Cur + 8, stride); |
transfer_8to16copy(&data[1 * 64], pY_Cur + 8, stride); |
82 |
transfer_8to16copy(data[2], pY_Cur + 8 * stride, stride); |
transfer_8to16copy(&data[2 * 64], pY_Cur + next_block, stride); |
83 |
transfer_8to16copy(data[3], pY_Cur + 8 * stride + 8, stride); |
transfer_8to16copy(&data[3 * 64], pY_Cur + next_block + 8, stride); |
84 |
transfer_8to16copy(data[4], pU_Cur, stride / 2); |
transfer_8to16copy(&data[4 * 64], pU_Cur, stride2); |
85 |
transfer_8to16copy(data[5], pV_Cur, stride / 2); |
transfer_8to16copy(&data[5 * 64], pV_Cur, stride2); |
86 |
stop_transfer_timer(); |
stop_transfer_timer(); |
87 |
|
|
88 |
start_timer(); |
start_timer(); |
89 |
pMB->field_dct = 0; |
pMB->field_dct = 0; |
90 |
if (pParam->global_flags & XVID_INTERLACING) |
if ((frame->global_flags & XVID_INTERLACING) && |
91 |
{ |
(x_pos>0) && (x_pos<pParam->mb_width-1) && |
92 |
|
(y_pos>0) && (y_pos<pParam->mb_height-1)) { |
93 |
pMB->field_dct = MBDecideFieldDCT(data); |
pMB->field_dct = MBDecideFieldDCT(data); |
94 |
} |
} |
95 |
stop_interlacing_timer(); |
stop_interlacing_timer(); |
96 |
|
|
97 |
for(i = 0; i < 6; i++) |
for (i = 0; i < 6; i++) { |
|
{ |
|
98 |
uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
99 |
|
|
100 |
start_timer(); |
start_timer(); |
101 |
fdct(data[i]); |
fdct(&data[i * 64]); |
102 |
stop_dct_timer(); |
stop_dct_timer(); |
103 |
|
|
104 |
if (pParam->quant_type == H263_QUANT) |
if (pParam->m_quant_type == H263_QUANT) { |
|
{ |
|
105 |
start_timer(); |
start_timer(); |
106 |
quant_intra(qcoeff[i], data[i], iQuant, iDcScaler); |
quant_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler); |
107 |
stop_quant_timer(); |
stop_quant_timer(); |
108 |
|
|
109 |
start_timer(); |
start_timer(); |
110 |
dequant_intra(data[i], qcoeff[i], iQuant, iDcScaler); |
dequant_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler); |
111 |
stop_iquant_timer(); |
stop_iquant_timer(); |
112 |
} |
} else { |
|
else |
|
|
{ |
|
113 |
start_timer(); |
start_timer(); |
114 |
quant4_intra(qcoeff[i], data[i], iQuant, iDcScaler); |
quant4_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler); |
115 |
stop_quant_timer(); |
stop_quant_timer(); |
116 |
|
|
117 |
start_timer(); |
start_timer(); |
118 |
dequant4_intra(data[i], qcoeff[i], iQuant, iDcScaler); |
dequant4_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler); |
119 |
stop_iquant_timer(); |
stop_iquant_timer(); |
120 |
} |
} |
121 |
|
|
122 |
start_timer(); |
start_timer(); |
123 |
idct(data[i]); |
idct(&data[i * 64]); |
124 |
stop_idct_timer(); |
stop_idct_timer(); |
125 |
} |
} |
126 |
|
|
127 |
start_timer(); |
if (pMB->field_dct) { |
128 |
if (pMB->field_dct) |
next_block = stride; |
129 |
{ |
stride *= 2; |
|
MBFieldToFrame(data); |
|
130 |
} |
} |
|
stop_interlacing_timer(); |
|
131 |
|
|
132 |
start_timer(); |
start_timer(); |
133 |
transfer_16to8copy(pY_Cur, data[0], stride); |
transfer_16to8copy(pY_Cur, &data[0 * 64], stride); |
134 |
transfer_16to8copy(pY_Cur + 8, data[1], stride); |
transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride); |
135 |
transfer_16to8copy(pY_Cur + 8 * stride, data[2], stride); |
transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride); |
136 |
transfer_16to8copy(pY_Cur + 8 + 8 * stride, data[3], stride); |
transfer_16to8copy(pY_Cur + next_block + 8, &data[3 * 64], stride); |
137 |
transfer_16to8copy(pU_Cur, data[4], stride / 2); |
transfer_16to8copy(pU_Cur, &data[4 * 64], stride2); |
138 |
transfer_16to8copy(pV_Cur, data[5], stride / 2); |
transfer_16to8copy(pV_Cur, &data[5 * 64], stride2); |
139 |
stop_transfer_timer(); |
stop_transfer_timer(); |
140 |
|
|
141 |
} |
} |
142 |
|
|
143 |
|
|
144 |
uint8_t MBTransQuantInter(const MBParam *pParam, |
uint8_t |
145 |
|
MBTransQuantInter(const MBParam * pParam, |
146 |
|
FRAMEINFO * frame, |
147 |
MACROBLOCK * pMB, |
MACROBLOCK * pMB, |
148 |
const uint32_t x_pos, const uint32_t y_pos, |
const uint32_t x_pos, |
149 |
int16_t data[][64], |
const uint32_t y_pos, |
150 |
int16_t qcoeff[][64], |
int16_t data[6 * 64], |
151 |
IMAGE * const pCurrent) |
int16_t qcoeff[6 * 64]) |
|
|
|
152 |
{ |
{ |
153 |
const uint32_t stride = pParam->edged_width; |
|
154 |
|
uint32_t stride = pParam->edged_width; |
155 |
|
uint32_t stride2 = stride / 2; |
156 |
|
uint32_t next_block = stride * 8; |
157 |
uint32_t i; |
uint32_t i; |
158 |
uint32_t iQuant = pParam->quant; |
uint32_t iQuant = frame->quant; |
159 |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
160 |
uint8_t cbp = 0; |
uint8_t cbp = 0; |
161 |
uint32_t sum; |
uint32_t sum; |
162 |
|
IMAGE *pCurrent = &frame->image; |
163 |
|
|
164 |
pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4); |
pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4); |
165 |
pU_Cur = pCurrent->u + (y_pos << 3) * (stride >> 1) + (x_pos << 3); |
pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3); |
166 |
pV_Cur = pCurrent->v + (y_pos << 3) * (stride >> 1) + (x_pos << 3); |
pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3); |
167 |
|
|
168 |
start_timer(); |
start_timer(); |
169 |
pMB->field_dct = 0; |
pMB->field_dct = 0; |
170 |
if (pParam->global_flags & XVID_INTERLACING) |
if ((frame->global_flags & XVID_INTERLACING) && |
171 |
{ |
(x_pos>0) && (x_pos<pParam->mb_width-1) && |
172 |
|
(y_pos>0) && (y_pos<pParam->mb_height-1)) { |
173 |
pMB->field_dct = MBDecideFieldDCT(data); |
pMB->field_dct = MBDecideFieldDCT(data); |
174 |
} |
} |
175 |
stop_interlacing_timer(); |
stop_interlacing_timer(); |
176 |
|
|
177 |
for(i = 0; i < 6; i++) |
for (i = 0; i < 6; i++) { |
|
{ |
|
178 |
/* |
/* |
179 |
no need to transfer 8->16-bit |
* no need to transfer 8->16-bit |
180 |
(this is performed already in motion compensation) |
* (this is performed already in motion compensation) |
181 |
*/ |
*/ |
182 |
start_timer(); |
start_timer(); |
183 |
fdct(data[i]); |
fdct(&data[i * 64]); |
184 |
stop_dct_timer(); |
stop_dct_timer(); |
185 |
|
|
186 |
if (pParam->quant_type == 0) |
if (pParam->m_quant_type == 0) { |
|
{ |
|
187 |
start_timer(); |
start_timer(); |
188 |
sum = quant_inter(qcoeff[i], data[i], iQuant); |
sum = quant_inter(&qcoeff[i * 64], &data[i * 64], iQuant); |
189 |
stop_quant_timer(); |
stop_quant_timer(); |
190 |
|
} else { |
191 |
|
start_timer(); |
192 |
|
sum = quant4_inter(&qcoeff[i * 64], &data[i * 64], iQuant); |
193 |
|
stop_quant_timer(); |
194 |
|
} |
195 |
|
|
196 |
|
if ((sum >= TOOSMALL_LIMIT) || (qcoeff[i*64] != 0) || |
197 |
|
(qcoeff[i*64+1] != 0) || (qcoeff[i*64+8] != 0)) { |
198 |
|
|
199 |
|
if (pParam->m_quant_type == H263_QUANT) { |
200 |
|
start_timer(); |
201 |
|
dequant_inter(&data[i * 64], &qcoeff[i * 64], iQuant); |
202 |
|
stop_iquant_timer(); |
203 |
|
} else { |
204 |
|
start_timer(); |
205 |
|
dequant4_inter(&data[i * 64], &qcoeff[i * 64], iQuant); |
206 |
|
stop_iquant_timer(); |
207 |
|
} |
208 |
|
|
209 |
|
cbp |= 1 << (5 - i); |
210 |
|
|
211 |
|
start_timer(); |
212 |
|
idct(&data[i * 64]); |
213 |
|
stop_idct_timer(); |
214 |
|
} |
215 |
|
} |
216 |
|
|
217 |
|
if (pMB->field_dct) { |
218 |
|
next_block = stride; |
219 |
|
stride *= 2; |
220 |
|
} |
221 |
|
|
222 |
|
start_timer(); |
223 |
|
if (cbp & 32) |
224 |
|
transfer_16to8add(pY_Cur, &data[0 * 64], stride); |
225 |
|
if (cbp & 16) |
226 |
|
transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride); |
227 |
|
if (cbp & 8) |
228 |
|
transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride); |
229 |
|
if (cbp & 4) |
230 |
|
transfer_16to8add(pY_Cur + next_block + 8, &data[3 * 64], stride); |
231 |
|
if (cbp & 2) |
232 |
|
transfer_16to8add(pU_Cur, &data[4 * 64], stride2); |
233 |
|
if (cbp & 1) |
234 |
|
transfer_16to8add(pV_Cur, &data[5 * 64], stride2); |
235 |
|
stop_transfer_timer(); |
236 |
|
|
237 |
|
return cbp; |
238 |
|
|
239 |
|
} |
240 |
|
|
241 |
|
void |
242 |
|
MBTransQuantIntra2(const MBParam * pParam, |
243 |
|
FRAMEINFO * frame, |
244 |
|
MACROBLOCK * pMB, |
245 |
|
const uint32_t x_pos, |
246 |
|
const uint32_t y_pos, |
247 |
|
int16_t data[6 * 64], |
248 |
|
int16_t qcoeff[6 * 64]) |
249 |
|
{ |
250 |
|
MBTrans(pParam,frame,pMB,x_pos,y_pos,data); |
251 |
|
MBfDCT(pParam,frame,pMB,data); |
252 |
|
MBQuantIntra(pParam,frame,pMB,data,qcoeff); |
253 |
|
MBDeQuantIntra(pParam,frame->quant,data,qcoeff); |
254 |
|
MBiDCT(data,0x3F); |
255 |
|
MBTransAdd(pParam,frame,pMB,x_pos,y_pos,data,0x3F); |
256 |
|
} |
257 |
|
|
258 |
|
|
259 |
|
uint8_t |
260 |
|
MBTransQuantInter2(const MBParam * pParam, |
261 |
|
FRAMEINFO * frame, |
262 |
|
MACROBLOCK * pMB, |
263 |
|
const uint32_t x_pos, |
264 |
|
const uint32_t y_pos, |
265 |
|
int16_t data[6 * 64], |
266 |
|
int16_t qcoeff[6 * 64]) |
267 |
|
{ |
268 |
|
uint8_t cbp; |
269 |
|
|
270 |
|
/* there is no MBTrans for Inter block, that's done in motion compensation already */ |
271 |
|
|
272 |
|
MBfDCT(pParam,frame,pMB,data); |
273 |
|
cbp = MBQuantInter(pParam,frame->quant,data,qcoeff); |
274 |
|
MBDeQuantInter(pParam,frame->quant,data,qcoeff,cbp); |
275 |
|
MBiDCT(data,cbp); |
276 |
|
MBTransAdd(pParam,frame,pMB,x_pos,y_pos,data,cbp); |
277 |
|
|
278 |
|
return cbp; |
279 |
} |
} |
280 |
else |
|
281 |
|
uint8_t |
282 |
|
MBTransQuantInterBVOP(const MBParam * pParam, |
283 |
|
FRAMEINFO * frame, |
284 |
|
MACROBLOCK * pMB, |
285 |
|
int16_t data[6 * 64], |
286 |
|
int16_t qcoeff[6 * 64]) |
287 |
{ |
{ |
288 |
|
uint8_t cbp; |
289 |
|
|
290 |
|
/* there is no MBTrans for Inter block, that's done in motion compensation already */ |
291 |
|
|
292 |
|
MBfDCT(pParam,frame,pMB,data); |
293 |
|
cbp = MBQuantInter(pParam,frame->quant,data,qcoeff); |
294 |
|
|
295 |
|
/* we don't have to DeQuant, iDCT and Transfer back data for B-frames */ |
296 |
|
|
297 |
|
return cbp; |
298 |
|
} |
299 |
|
|
300 |
|
|
301 |
|
void |
302 |
|
MBfDCT(const MBParam * pParam, |
303 |
|
FRAMEINFO * frame, |
304 |
|
MACROBLOCK * pMB, |
305 |
|
int16_t data[6 * 64]) |
306 |
|
{ |
307 |
|
int i; |
308 |
|
|
309 |
start_timer(); |
start_timer(); |
310 |
sum = quant4_inter(qcoeff[i], data[i], iQuant); |
pMB->field_dct = 0; |
311 |
stop_quant_timer(); |
if ((frame->global_flags & XVID_INTERLACING)) { |
312 |
|
pMB->field_dct = MBDecideFieldDCT(data); |
313 |
} |
} |
314 |
|
stop_interlacing_timer(); |
315 |
|
|
316 |
if(sum >= TOOSMALL_LIMIT) { // skip block ? |
for (i = 0; i < 6; i++) { |
317 |
|
start_timer(); |
318 |
|
fdct(&data[i * 64]); |
319 |
|
stop_dct_timer(); |
320 |
|
} |
321 |
|
} |
322 |
|
|
323 |
if (pParam->quant_type == H263_QUANT) |
void |
324 |
|
MBQuantDeQuantIntra(const MBParam * pParam, |
325 |
|
FRAMEINFO * frame, |
326 |
|
MACROBLOCK * pMB, |
327 |
|
int16_t qcoeff[6 * 64], |
328 |
|
int16_t data[6*64]) |
329 |
{ |
{ |
330 |
|
int i; |
331 |
|
int iQuant = frame->quant; |
332 |
|
|
333 |
|
start_timer(); |
334 |
|
pMB->field_dct = 0; |
335 |
|
if ((frame->global_flags & XVID_INTERLACING)) { |
336 |
|
pMB->field_dct = MBDecideFieldDCT(data); |
337 |
|
} |
338 |
|
stop_interlacing_timer(); |
339 |
|
|
340 |
|
for (i = 0; i < 6; i++) { |
341 |
|
uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
342 |
|
|
343 |
|
if (pParam->m_quant_type == H263_QUANT) { |
344 |
|
start_timer(); |
345 |
|
quant_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler); |
346 |
|
stop_quant_timer(); |
347 |
|
|
348 |
|
start_timer(); |
349 |
|
dequant_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler); |
350 |
|
stop_iquant_timer(); |
351 |
|
} else { |
352 |
|
start_timer(); |
353 |
|
quant4_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler); |
354 |
|
stop_quant_timer(); |
355 |
|
|
356 |
start_timer(); |
start_timer(); |
357 |
dequant_inter(data[i], qcoeff[i], iQuant); |
dequant4_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler); |
358 |
stop_iquant_timer(); |
stop_iquant_timer(); |
359 |
} |
} |
360 |
else |
} |
361 |
|
} |
362 |
|
|
363 |
|
void |
364 |
|
MBQuantIntra(const MBParam * pParam, |
365 |
|
FRAMEINFO * frame, |
366 |
|
MACROBLOCK *pMB, |
367 |
|
int16_t qcoeff[6 * 64], |
368 |
|
int16_t data[6*64]) |
369 |
|
{ |
370 |
|
int i; |
371 |
|
int iQuant = frame->quant; |
372 |
|
|
373 |
|
start_timer(); |
374 |
|
pMB->field_dct = 0; |
375 |
|
if ((frame->global_flags & XVID_INTERLACING)) { |
376 |
|
pMB->field_dct = MBDecideFieldDCT(data); |
377 |
|
} |
378 |
|
stop_interlacing_timer(); |
379 |
|
|
380 |
|
for (i = 0; i < 6; i++) { |
381 |
|
uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
382 |
|
|
383 |
|
if (pParam->m_quant_type == H263_QUANT) { |
384 |
|
start_timer(); |
385 |
|
quant_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler); |
386 |
|
stop_quant_timer(); |
387 |
|
} else { |
388 |
|
start_timer(); |
389 |
|
quant4_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler); |
390 |
|
stop_quant_timer(); |
391 |
|
} |
392 |
|
} |
393 |
|
} |
394 |
|
|
395 |
|
void |
396 |
|
MBDeQuantIntra(const MBParam * pParam, |
397 |
|
const int iQuant, |
398 |
|
int16_t qcoeff[6 * 64], |
399 |
|
int16_t data[6*64]) |
400 |
{ |
{ |
401 |
|
int i; |
402 |
|
|
403 |
|
for (i = 0; i < 6; i++) { |
404 |
|
uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
405 |
|
|
406 |
|
if (pParam->m_quant_type == H263_QUANT) { |
407 |
start_timer(); |
start_timer(); |
408 |
dequant4_inter(data[i], qcoeff[i], iQuant); |
dequant_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler); |
409 |
stop_iquant_timer(); |
stop_iquant_timer(); |
410 |
|
} else { |
411 |
|
start_timer(); |
412 |
|
dequant4_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler); |
413 |
|
stop_iquant_timer(); |
414 |
|
} |
415 |
|
} |
416 |
} |
} |
417 |
|
|
418 |
|
uint8_t |
419 |
|
MBQuantInter(const MBParam * pParam, |
420 |
|
const int iQuant, |
421 |
|
int16_t data[6 * 64], |
422 |
|
int16_t qcoeff[6 * 64]) |
423 |
|
{ |
424 |
|
|
425 |
|
int i; |
426 |
|
uint8_t cbp = 0; |
427 |
|
int sum; |
428 |
|
|
429 |
|
for (i = 0; i < 6; i++) { |
430 |
|
|
431 |
|
if (pParam->m_quant_type == 0) { |
432 |
|
start_timer(); |
433 |
|
sum = quant_inter(&qcoeff[i * 64], &data[i * 64], iQuant); |
434 |
|
stop_quant_timer(); |
435 |
|
} else { |
436 |
|
start_timer(); |
437 |
|
sum = quant4_inter(&qcoeff[i * 64], &data[i * 64], iQuant); |
438 |
|
stop_quant_timer(); |
439 |
|
} |
440 |
|
|
441 |
|
if (sum >= TOOSMALL_LIMIT) { // skip block ? |
442 |
cbp |= 1 << (5 - i); |
cbp |= 1 << (5 - i); |
443 |
|
} |
444 |
|
} |
445 |
|
return cbp; |
446 |
|
} |
447 |
|
|
448 |
|
void |
449 |
|
MBDeQuantInter( const MBParam * pParam, |
450 |
|
const int iQuant, |
451 |
|
int16_t data[6 * 64], |
452 |
|
int16_t qcoeff[6 * 64], |
453 |
|
const uint8_t cbp) |
454 |
|
{ |
455 |
|
int i; |
456 |
|
|
457 |
|
for (i = 0; i < 6; i++) { |
458 |
|
if (cbp & (1 << (5 - i))) |
459 |
|
{ |
460 |
|
if (pParam->m_quant_type == H263_QUANT) { |
461 |
|
start_timer(); |
462 |
|
dequant_inter(&data[i * 64], &qcoeff[i * 64], iQuant); |
463 |
|
stop_iquant_timer(); |
464 |
|
} else { |
465 |
|
start_timer(); |
466 |
|
dequant4_inter(&data[i * 64], &qcoeff[i * 64], iQuant); |
467 |
|
stop_iquant_timer(); |
468 |
|
} |
469 |
|
} |
470 |
|
} |
471 |
|
} |
472 |
|
|
473 |
|
void |
474 |
|
MBiDCT( int16_t data[6 * 64], |
475 |
|
const uint8_t cbp) |
476 |
|
{ |
477 |
|
int i; |
478 |
|
|
479 |
|
for (i = 0; i < 6; i++) { |
480 |
|
if (cbp & (1 << (5 - i))) |
481 |
|
{ |
482 |
start_timer(); |
start_timer(); |
483 |
idct(data[i]); |
idct(&data[i * 64]); |
484 |
stop_idct_timer(); |
stop_idct_timer(); |
485 |
|
|
486 |
} |
} |
487 |
} |
} |
488 |
|
} |
489 |
|
|
490 |
|
|
491 |
|
void |
492 |
|
MBTrans(const MBParam * pParam, |
493 |
|
FRAMEINFO * frame, |
494 |
|
MACROBLOCK * pMB, |
495 |
|
const uint32_t x_pos, |
496 |
|
const uint32_t y_pos, |
497 |
|
int16_t data[6 * 64]) |
498 |
|
{ |
499 |
|
uint32_t stride = pParam->edged_width; |
500 |
|
uint32_t stride2 = stride / 2; |
501 |
|
uint32_t next_block = stride * 8; |
502 |
|
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
503 |
|
IMAGE *pCurrent = &frame->image; |
504 |
|
|
505 |
|
pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4); |
506 |
|
pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3); |
507 |
|
pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3); |
508 |
|
|
509 |
start_timer(); |
start_timer(); |
510 |
if (pMB->field_dct) |
transfer_8to16copy(&data[0 * 64], pY_Cur, stride); |
511 |
|
transfer_8to16copy(&data[1 * 64], pY_Cur + 8, stride); |
512 |
|
transfer_8to16copy(&data[2 * 64], pY_Cur + next_block, stride); |
513 |
|
transfer_8to16copy(&data[3 * 64], pY_Cur + next_block + 8, stride); |
514 |
|
transfer_8to16copy(&data[4 * 64], pU_Cur, stride2); |
515 |
|
transfer_8to16copy(&data[5 * 64], pV_Cur, stride2); |
516 |
|
stop_transfer_timer(); |
517 |
|
} |
518 |
|
|
519 |
|
void |
520 |
|
MBTransAdd(const MBParam * pParam, |
521 |
|
FRAMEINFO * frame, |
522 |
|
MACROBLOCK * pMB, |
523 |
|
const uint32_t x_pos, |
524 |
|
const uint32_t y_pos, |
525 |
|
int16_t data[6 * 64], |
526 |
|
const uint8_t cbp) |
527 |
{ |
{ |
528 |
MBFieldToFrame(data); |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
529 |
|
uint32_t stride = pParam->edged_width; |
530 |
|
uint32_t stride2 = stride / 2; |
531 |
|
uint32_t next_block = stride * 8; |
532 |
|
IMAGE *pCurrent = &frame->image; |
533 |
|
|
534 |
|
pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4); |
535 |
|
pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3); |
536 |
|
pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3); |
537 |
|
|
538 |
|
if (pMB->field_dct) { |
539 |
|
next_block = stride; |
540 |
|
stride *= 2; |
541 |
} |
} |
|
stop_interlacing_timer(); |
|
542 |
|
|
543 |
start_timer(); |
start_timer(); |
544 |
if (cbp & 32) |
if (cbp & 32) |
545 |
transfer_16to8add(pY_Cur, data[0], stride); |
transfer_16to8add(pY_Cur, &data[0 * 64], stride); |
546 |
if (cbp & 16) |
if (cbp & 16) |
547 |
transfer_16to8add(pY_Cur + 8, data[1], stride); |
transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride); |
548 |
if (cbp & 8) |
if (cbp & 8) |
549 |
transfer_16to8add(pY_Cur + 8 * stride, data[2], stride); |
transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride); |
550 |
if (cbp & 4) |
if (cbp & 4) |
551 |
transfer_16to8add(pY_Cur + 8 + 8 * stride, data[3], stride); |
transfer_16to8add(pY_Cur + next_block + 8, &data[3 * 64], stride); |
552 |
if (cbp & 2) |
if (cbp & 2) |
553 |
transfer_16to8add(pU_Cur, data[4], stride / 2); |
transfer_16to8add(pU_Cur, &data[4 * 64], stride2); |
554 |
if (cbp & 1) |
if (cbp & 1) |
555 |
transfer_16to8add(pV_Cur, data[5], stride / 2); |
transfer_16to8add(pV_Cur, &data[5 * 64], stride2); |
556 |
stop_transfer_timer(); |
stop_transfer_timer(); |
|
|
|
|
return cbp; |
|
557 |
} |
} |
558 |
|
|
559 |
|
|
560 |
|
|
561 |
/* 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 */ |
562 |
|
|
|
#define ABS(X) (X)<0 ? -(X) : (X) |
|
563 |
|
|
564 |
uint32_t MBDecideFieldDCT(int16_t data[][64]) |
uint32_t |
565 |
|
MBDecideFieldDCT(int16_t data[6 * 64]) |
566 |
{ |
{ |
567 |
const uint8_t blocks[] = {0, 0, 0, 0, 2, 2, 2, 2}; |
|
568 |
|
const uint8_t blocks[] = |
569 |
|
{ 0 * 64, 0 * 64, 0 * 64, 0 * 64, 2 * 64, 2 * 64, 2 * 64, 2 * 64 }; |
570 |
const uint8_t lines[] = {0, 16, 32, 48, 0, 16, 32, 48}; |
const uint8_t lines[] = {0, 16, 32, 48, 0, 16, 32, 48}; |
571 |
|
|
572 |
int frame = 0, field = 0; |
int frame = 0, field = 0; |
573 |
int i, j; |
int i, j; |
574 |
|
|
575 |
for (i=0 ; i<7 ; ++i) |
for (i = 0; i < 7; ++i) { |
576 |
{ |
for (j = 0; j < 8; ++j) { |
577 |
for (j=0 ; j<8 ; ++j) |
frame += |
578 |
{ |
ABS(data[0 * 64 + (i + 1) * 8 + j] - data[0 * 64 + i * 8 + j]); |
579 |
frame += ABS(data[0][(i+1)*8 + j] - data[0][i*8 + j]); |
frame += |
580 |
frame += ABS(data[1][(i+1)*8 + j] - data[1][i*8 + j]); |
ABS(data[1 * 64 + (i + 1) * 8 + j] - data[1 * 64 + i * 8 + j]); |
581 |
frame += ABS(data[2][(i+1)*8 + j] - data[2][i*8 + j]); |
frame += |
582 |
frame += ABS(data[3][(i+1)*8 + j] - data[3][i*8 + j]); |
ABS(data[2 * 64 + (i + 1) * 8 + j] - data[2 * 64 + i * 8 + j]); |
583 |
|
frame += |
584 |
field += ABS(data[blocks[i+1]][lines[i+1] + j] - data[blocks[i]][lines[i] + j]); |
ABS(data[3 * 64 + (i + 1) * 8 + j] - data[3 * 64 + i * 8 + j]); |
585 |
field += ABS(data[blocks[i+1]][lines[i+1] + 8 + j] - data[blocks[i]][lines[i] + 8 + j]); |
|
586 |
field += ABS(data[blocks[i+1]+1][lines[i+1] + j] - data[blocks[i]+1][lines[i] + j]); |
field += |
587 |
field += ABS(data[blocks[i+1]+1][lines[i+1] + 8 + j] - data[blocks[i]+1][lines[i] + 8 + j]); |
ABS(data[blocks[i + 1] + lines[i + 1] + j] - |
588 |
|
data[blocks[i] + lines[i] + j]); |
589 |
|
field += |
590 |
|
ABS(data[blocks[i + 1] + lines[i + 1] + 8 + j] - |
591 |
|
data[blocks[i] + lines[i] + 8 + j]); |
592 |
|
field += |
593 |
|
ABS(data[blocks[i + 1] + 64 + lines[i + 1] + j] - |
594 |
|
data[blocks[i] + 64 + lines[i] + j]); |
595 |
|
field += |
596 |
|
ABS(data[blocks[i + 1] + 64 + lines[i + 1] + 8 + j] - |
597 |
|
data[blocks[i] + 64 + lines[i] + 8 + j]); |
598 |
} |
} |
599 |
} |
} |
600 |
|
|
601 |
if (frame > field) |
if (frame > field) { |
|
{ |
|
602 |
MBFrameToField(data); |
MBFrameToField(data); |
603 |
} |
} |
604 |
|
|
605 |
return (frame > field); |
return (frame > (field + 350)); |
606 |
} |
} |
607 |
|
|
608 |
|
|
609 |
/* deinterlace Y blocks vertically */ |
/* deinterlace Y blocks vertically */ |
610 |
|
|
611 |
#define MOVLINE(X,Y) memcpy(X, Y, sizeof(tmp)) |
#define MOVLINE(X,Y) memcpy(X, Y, sizeof(tmp)) |
612 |
#define LINE(X,Y) &data[X][Y*8] |
#define LINE(X,Y) &data[X*64 + Y*8] |
613 |
|
|
614 |
void MBFrameToField(int16_t data[][64]) |
void |
615 |
|
MBFrameToField(int16_t data[6 * 64]) |
616 |
{ |
{ |
617 |
int16_t tmp[8]; |
int16_t tmp[8]; |
618 |
|
|
672 |
MOVLINE(LINE(3,5), LINE(3,3)); |
MOVLINE(LINE(3,5), LINE(3,3)); |
673 |
MOVLINE(LINE(3,3), tmp); |
MOVLINE(LINE(3,3), tmp); |
674 |
} |
} |
|
|
|
|
|
|
|
/* interlace Y blocks vertically */ |
|
|
|
|
|
void MBFieldToFrame(int16_t data[][64]) |
|
|
{ |
|
|
uint16_t tmp[8]; |
|
|
|
|
|
/* left blocks */ |
|
|
|
|
|
// 1=8, 8=4, 4=2, 2=1 |
|
|
MOVLINE(tmp, LINE(0,1)); |
|
|
MOVLINE(LINE(0,1), LINE(2,0)); |
|
|
MOVLINE(LINE(2,0), LINE(0,4)); |
|
|
MOVLINE(LINE(0,4), LINE(0,2)); |
|
|
MOVLINE(LINE(0,2), tmp); |
|
|
|
|
|
// 3=9, 9=12, 12=6, 6=3 |
|
|
MOVLINE(tmp, LINE(0,3)); |
|
|
MOVLINE(LINE(0,3), LINE(2,1)); |
|
|
MOVLINE(LINE(2,1), LINE(2,4)); |
|
|
MOVLINE(LINE(2,4), LINE(0,6)); |
|
|
MOVLINE(LINE(0,6), tmp); |
|
|
|
|
|
// 5=10, 10=5 |
|
|
MOVLINE(tmp, LINE(0,5)); |
|
|
MOVLINE(LINE(0,5), LINE(2,2)); |
|
|
MOVLINE(LINE(2,2), tmp); |
|
|
|
|
|
// 7=11, 11=13, 13=14, 14=7 |
|
|
MOVLINE(tmp, LINE(0,7)); |
|
|
MOVLINE(LINE(0,7), LINE(2,3)); |
|
|
MOVLINE(LINE(2,3), LINE(2,5)); |
|
|
MOVLINE(LINE(2,5), LINE(2,6)); |
|
|
MOVLINE(LINE(2,6), tmp); |
|
|
|
|
|
/* right blocks */ |
|
|
|
|
|
// 1=8, 8=4, 4=2, 2=1 |
|
|
MOVLINE(tmp, LINE(1,1)); |
|
|
MOVLINE(LINE(1,1), LINE(3,0)); |
|
|
MOVLINE(LINE(3,0), LINE(1,4)); |
|
|
MOVLINE(LINE(1,4), LINE(1,2)); |
|
|
MOVLINE(LINE(1,2), tmp); |
|
|
|
|
|
// 3=9, 9=12, 12=6, 6=3 |
|
|
MOVLINE(tmp, LINE(1,3)); |
|
|
MOVLINE(LINE(1,3), LINE(3,1)); |
|
|
MOVLINE(LINE(3,1), LINE(3,4)); |
|
|
MOVLINE(LINE(3,4), LINE(1,6)); |
|
|
MOVLINE(LINE(1,6), tmp); |
|
|
|
|
|
// 5=10, 10=5 |
|
|
MOVLINE(tmp, LINE(1,5)); |
|
|
MOVLINE(LINE(1,5), LINE(3,2)); |
|
|
MOVLINE(LINE(3,2), tmp); |
|
|
|
|
|
// 7=11, 11=13, 13=14, 14=7 |
|
|
MOVLINE(tmp, LINE(1,7)); |
|
|
MOVLINE(LINE(1,7), LINE(3,3)); |
|
|
MOVLINE(LINE(3,3), LINE(3,5)); |
|
|
MOVLINE(LINE(3,5), LINE(3,6)); |
|
|
MOVLINE(LINE(3,6), tmp); |
|
|
} |
|