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 file is part of XviD, a free MPEG-4 video encoder/decoder |
12 |
* modifications or derivatives thereof. * |
* |
13 |
* * |
* XviD is free software; you can redistribute it and/or modify it |
14 |
* XviD is free software; you can redistribute it and/or modify it * |
* under the terms of the GNU General Public License as published by |
15 |
* under the terms of the GNU General Public License as published by * |
* the Free Software Foundation; either version 2 of the License, or |
16 |
* the Free Software Foundation; either version 2 of the License, or * |
* (at your option) any later version. |
17 |
* (at your option) any later version. * |
* |
18 |
* * |
* This program is distributed in the hope that it will be useful, |
19 |
* XviD is distributed in the hope that it will be useful, but * |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
20 |
* WITHOUT ANY WARRANTY; without even the implied warranty of * |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
21 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
* GNU General Public License for more details. |
22 |
* GNU General Public License for more details. * |
* |
23 |
* * |
* You should have received a copy of the GNU General Public License |
24 |
* You should have received a copy of the GNU General Public License * |
* along with this program; if not, write to the Free Software |
25 |
* along with this program; if not, write to the Free Software * |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
26 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * |
* |
27 |
* * |
* Under section 8 of the GNU General Public License, the copyright |
28 |
******************************************************************************/ |
* holders of XVID explicitly forbid distribution in the following |
29 |
|
* countries: |
30 |
/****************************************************************************** |
* |
31 |
* * |
* - Japan |
32 |
* mbtransquant.c * |
* - United States of America |
33 |
* * |
* |
34 |
* Copyright (C) 2001 - Peter Ross <pross@cs.rmit.edu.au> * |
* Linking XviD statically or dynamically with other modules is making a |
35 |
* Copyright (C) 2001 - Michael Militzer <isibaar@xvid.org> * |
* combined work based on XviD. Thus, the terms and conditions of the |
36 |
* * |
* GNU General Public License cover the whole combination. |
37 |
* For more information visit the XviD homepage: http://www.xvid.org * |
* |
38 |
* * |
* As a special exception, the copyright holders of XviD give you |
39 |
******************************************************************************/ |
* permission to link XviD with independent modules that communicate with |
40 |
|
* XviD solely through the VFW1.1 and DShow interfaces, regardless of the |
41 |
/****************************************************************************** |
* license terms of these independent modules, and to copy and distribute |
42 |
* * |
* the resulting combined work under terms of your choice, provided that |
43 |
* Revision history: * |
* every copy of the combined work is accompanied by a complete copy of |
44 |
* * |
* the source code of XviD (the version of XviD used to produce the |
45 |
* 26.03.2002 interlacing support - moved transfers outside loops |
* combined work), being distributed under the terms of the GNU General |
46 |
* 22.12.2001 get_dc_scaler() moved to common.h |
* Public License plus this exception. An independent module is a module |
47 |
* 19.11.2001 introduced coefficient thresholding (Isibaar) * |
* which is not derived from or based on XviD. |
48 |
* 17.11.2001 initial version * |
* |
49 |
* * |
* Note that people who make modified versions of XviD are not obligated |
50 |
******************************************************************************/ |
* to grant this special exception for their modified versions; it is |
51 |
|
* their choice whether to do so. The GNU General Public License gives |
52 |
|
* permission to release a modified version without this exception; this |
53 |
|
* exception also makes it possible to release a modified version which |
54 |
|
* carries forward this exception. |
55 |
|
* |
56 |
|
* $Id: mbtransquant.c,v 1.20 2002-11-26 23:44:11 edgomez Exp $ |
57 |
|
* |
58 |
|
****************************************************************************/ |
59 |
|
|
60 |
#include <string.h> |
#include <string.h> |
61 |
|
|
74 |
#define MIN(X, Y) ((X)<(Y)?(X):(Y)) |
#define MIN(X, Y) ((X)<(Y)?(X):(Y)) |
75 |
#define MAX(X, Y) ((X)>(Y)?(X):(Y)) |
#define MAX(X, Y) ((X)>(Y)?(X):(Y)) |
76 |
|
|
77 |
#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 */ |
78 |
|
|
79 |
/* this isnt pretty, but its better than 20 ifdefs */ |
/* this isnt pretty, but its better than 20 ifdefs */ |
80 |
|
|
81 |
void MBTransQuantIntra(const MBParam *pParam, |
void |
82 |
|
MBTransQuantIntra(const MBParam * pParam, |
83 |
|
FRAMEINFO * frame, |
84 |
MACROBLOCK * pMB, |
MACROBLOCK * pMB, |
85 |
const uint32_t x_pos, |
const uint32_t x_pos, |
86 |
const uint32_t y_pos, |
const uint32_t y_pos, |
87 |
int16_t data[6*64], |
int16_t data[6*64], |
88 |
int16_t qcoeff[6*64], |
int16_t qcoeff[6 * 64]) |
|
IMAGE * const pCurrent) |
|
|
|
|
89 |
{ |
{ |
90 |
|
|
91 |
const uint32_t stride = pParam->edged_width; |
uint32_t stride = pParam->edged_width; |
92 |
|
uint32_t stride2 = stride / 2; |
93 |
|
uint32_t next_block = stride * 8; |
94 |
uint32_t i; |
uint32_t i; |
95 |
uint32_t iQuant = pParam->quant; |
uint32_t iQuant = frame->quant; |
96 |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
97 |
|
IMAGE *pCurrent = &frame->image; |
98 |
|
|
99 |
pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4); |
pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4); |
100 |
pU_Cur = pCurrent->u + (y_pos << 3) * (stride >> 1) + (x_pos << 3); |
pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3); |
101 |
pV_Cur = pCurrent->v + (y_pos << 3) * (stride >> 1) + (x_pos << 3); |
pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3); |
102 |
|
|
103 |
start_timer(); |
start_timer(); |
104 |
transfer_8to16copy(&data[0*64], pY_Cur, stride); |
transfer_8to16copy(&data[0*64], pY_Cur, stride); |
105 |
transfer_8to16copy(&data[1*64], pY_Cur + 8, stride); |
transfer_8to16copy(&data[1*64], pY_Cur + 8, stride); |
106 |
transfer_8to16copy(&data[2*64], pY_Cur + 8 * stride, stride); |
transfer_8to16copy(&data[2 * 64], pY_Cur + next_block, stride); |
107 |
transfer_8to16copy(&data[3*64], pY_Cur + 8 * stride + 8, stride); |
transfer_8to16copy(&data[3 * 64], pY_Cur + next_block + 8, stride); |
108 |
transfer_8to16copy(&data[4*64], pU_Cur, stride / 2); |
transfer_8to16copy(&data[4 * 64], pU_Cur, stride2); |
109 |
transfer_8to16copy(&data[5*64], pV_Cur, stride / 2); |
transfer_8to16copy(&data[5 * 64], pV_Cur, stride2); |
110 |
stop_transfer_timer(); |
stop_transfer_timer(); |
111 |
|
|
112 |
start_timer(); |
start_timer(); |
113 |
pMB->field_dct = 0; |
pMB->field_dct = 0; |
114 |
if (pParam->global_flags & XVID_INTERLACING) |
if ((frame->global_flags & XVID_INTERLACING) && |
115 |
{ |
(x_pos>0) && (x_pos<pParam->mb_width-1) && |
116 |
|
(y_pos>0) && (y_pos<pParam->mb_height-1)) { |
117 |
pMB->field_dct = MBDecideFieldDCT(data); |
pMB->field_dct = MBDecideFieldDCT(data); |
118 |
} |
} |
119 |
stop_interlacing_timer(); |
stop_interlacing_timer(); |
120 |
|
|
121 |
for(i = 0; i < 6; i++) |
for (i = 0; i < 6; i++) { |
|
{ |
|
122 |
uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
123 |
|
|
124 |
start_timer(); |
start_timer(); |
125 |
fdct(&data[i*64]); |
fdct(&data[i*64]); |
126 |
stop_dct_timer(); |
stop_dct_timer(); |
127 |
|
|
128 |
if (pParam->quant_type == H263_QUANT) |
if (pParam->m_quant_type == H263_QUANT) { |
|
{ |
|
129 |
start_timer(); |
start_timer(); |
130 |
quant_intra(&qcoeff[i*64], &data[i*64], iQuant, iDcScaler); |
quant_intra(&qcoeff[i*64], &data[i*64], iQuant, iDcScaler); |
131 |
stop_quant_timer(); |
stop_quant_timer(); |
133 |
start_timer(); |
start_timer(); |
134 |
dequant_intra(&data[i*64], &qcoeff[i*64], iQuant, iDcScaler); |
dequant_intra(&data[i*64], &qcoeff[i*64], iQuant, iDcScaler); |
135 |
stop_iquant_timer(); |
stop_iquant_timer(); |
136 |
} |
} else { |
|
else |
|
|
{ |
|
137 |
start_timer(); |
start_timer(); |
138 |
quant4_intra(&qcoeff[i*64], &data[i*64], iQuant, iDcScaler); |
quant4_intra(&qcoeff[i*64], &data[i*64], iQuant, iDcScaler); |
139 |
stop_quant_timer(); |
stop_quant_timer(); |
148 |
stop_idct_timer(); |
stop_idct_timer(); |
149 |
} |
} |
150 |
|
|
151 |
start_timer(); |
if (pMB->field_dct) { |
152 |
if (pMB->field_dct) |
next_block = stride; |
153 |
{ |
stride *= 2; |
|
MBFieldToFrame(data); |
|
154 |
} |
} |
|
stop_interlacing_timer(); |
|
155 |
|
|
156 |
start_timer(); |
start_timer(); |
157 |
transfer_16to8copy(pY_Cur, &data[0*64], stride); |
transfer_16to8copy(pY_Cur, &data[0*64], stride); |
158 |
transfer_16to8copy(pY_Cur + 8, &data[1*64], stride); |
transfer_16to8copy(pY_Cur + 8, &data[1*64], stride); |
159 |
transfer_16to8copy(pY_Cur + 8 * stride, &data[2*64], stride); |
transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride); |
160 |
transfer_16to8copy(pY_Cur + 8 + 8 * stride, &data[3*64], stride); |
transfer_16to8copy(pY_Cur + next_block + 8, &data[3 * 64], stride); |
161 |
transfer_16to8copy(pU_Cur, &data[4*64], stride / 2); |
transfer_16to8copy(pU_Cur, &data[4 * 64], stride2); |
162 |
transfer_16to8copy(pV_Cur, &data[5*64], stride / 2); |
transfer_16to8copy(pV_Cur, &data[5 * 64], stride2); |
163 |
stop_transfer_timer(); |
stop_transfer_timer(); |
164 |
|
|
165 |
} |
} |
166 |
|
|
167 |
|
|
168 |
uint8_t MBTransQuantInter(const MBParam *pParam, |
uint8_t |
169 |
|
MBTransQuantInter(const MBParam * pParam, |
170 |
|
FRAMEINFO * frame, |
171 |
MACROBLOCK * pMB, |
MACROBLOCK * pMB, |
172 |
const uint32_t x_pos, const uint32_t y_pos, |
const uint32_t x_pos, |
173 |
|
const uint32_t y_pos, |
174 |
int16_t data[6*64], |
int16_t data[6*64], |
175 |
int16_t qcoeff[6*64], |
int16_t qcoeff[6 * 64]) |
|
IMAGE * const pCurrent) |
|
|
|
|
176 |
{ |
{ |
177 |
|
|
178 |
const uint32_t stride = pParam->edged_width; |
uint32_t stride = pParam->edged_width; |
179 |
|
uint32_t stride2 = stride / 2; |
180 |
|
uint32_t next_block = stride * 8; |
181 |
uint32_t i; |
uint32_t i; |
182 |
uint32_t iQuant = pParam->quant; |
uint32_t iQuant = frame->quant; |
183 |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
184 |
uint8_t cbp = 0; |
uint8_t cbp = 0; |
185 |
uint32_t sum; |
uint32_t sum; |
186 |
|
IMAGE *pCurrent = &frame->image; |
187 |
|
|
188 |
pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4); |
pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4); |
189 |
pU_Cur = pCurrent->u + (y_pos << 3) * (stride >> 1) + (x_pos << 3); |
pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3); |
190 |
pV_Cur = pCurrent->v + (y_pos << 3) * (stride >> 1) + (x_pos << 3); |
pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3); |
191 |
|
|
192 |
start_timer(); |
start_timer(); |
193 |
pMB->field_dct = 0; |
pMB->field_dct = 0; |
194 |
if (pParam->global_flags & XVID_INTERLACING) |
if ((frame->global_flags & XVID_INTERLACING) && |
195 |
{ |
(x_pos>0) && (x_pos<pParam->mb_width-1) && |
196 |
|
(y_pos>0) && (y_pos<pParam->mb_height-1)) { |
197 |
pMB->field_dct = MBDecideFieldDCT(data); |
pMB->field_dct = MBDecideFieldDCT(data); |
198 |
} |
} |
199 |
stop_interlacing_timer(); |
stop_interlacing_timer(); |
200 |
|
|
201 |
for(i = 0; i < 6; i++) |
for (i = 0; i < 6; i++) { |
|
{ |
|
202 |
/* |
/* |
203 |
* no need to transfer 8->16-bit |
* no need to transfer 8->16-bit |
204 |
* (this is performed already in motion compensation) |
* (this is performed already in motion compensation) |
207 |
fdct(&data[i*64]); |
fdct(&data[i*64]); |
208 |
stop_dct_timer(); |
stop_dct_timer(); |
209 |
|
|
210 |
if (pParam->quant_type == 0) |
if (pParam->m_quant_type == 0) { |
|
{ |
|
211 |
start_timer(); |
start_timer(); |
212 |
sum = quant_inter(&qcoeff[i*64], &data[i*64], iQuant); |
sum = quant_inter(&qcoeff[i*64], &data[i*64], iQuant); |
213 |
stop_quant_timer(); |
stop_quant_timer(); |
214 |
|
} else { |
215 |
|
start_timer(); |
216 |
|
sum = quant4_inter(&qcoeff[i * 64], &data[i * 64], iQuant); |
217 |
|
stop_quant_timer(); |
218 |
|
} |
219 |
|
|
220 |
|
if ((sum >= TOOSMALL_LIMIT) || (qcoeff[i*64] != 0) || |
221 |
|
(qcoeff[i*64+1] != 0) || (qcoeff[i*64+8] != 0)) { |
222 |
|
|
223 |
|
if (pParam->m_quant_type == H263_QUANT) { |
224 |
|
start_timer(); |
225 |
|
dequant_inter(&data[i * 64], &qcoeff[i * 64], iQuant); |
226 |
|
stop_iquant_timer(); |
227 |
|
} else { |
228 |
|
start_timer(); |
229 |
|
dequant4_inter(&data[i * 64], &qcoeff[i * 64], iQuant); |
230 |
|
stop_iquant_timer(); |
231 |
|
} |
232 |
|
|
233 |
|
cbp |= 1 << (5 - i); |
234 |
|
|
235 |
|
start_timer(); |
236 |
|
idct(&data[i * 64]); |
237 |
|
stop_idct_timer(); |
238 |
|
} |
239 |
|
} |
240 |
|
|
241 |
|
if (pMB->field_dct) { |
242 |
|
next_block = stride; |
243 |
|
stride *= 2; |
244 |
|
} |
245 |
|
|
246 |
|
start_timer(); |
247 |
|
if (cbp & 32) |
248 |
|
transfer_16to8add(pY_Cur, &data[0 * 64], stride); |
249 |
|
if (cbp & 16) |
250 |
|
transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride); |
251 |
|
if (cbp & 8) |
252 |
|
transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride); |
253 |
|
if (cbp & 4) |
254 |
|
transfer_16to8add(pY_Cur + next_block + 8, &data[3 * 64], stride); |
255 |
|
if (cbp & 2) |
256 |
|
transfer_16to8add(pU_Cur, &data[4 * 64], stride2); |
257 |
|
if (cbp & 1) |
258 |
|
transfer_16to8add(pV_Cur, &data[5 * 64], stride2); |
259 |
|
stop_transfer_timer(); |
260 |
|
|
261 |
|
return cbp; |
262 |
|
|
263 |
} |
} |
264 |
else |
|
265 |
|
void |
266 |
|
MBTransQuantIntra2(const MBParam * pParam, |
267 |
|
FRAMEINFO * frame, |
268 |
|
MACROBLOCK * pMB, |
269 |
|
const uint32_t x_pos, |
270 |
|
const uint32_t y_pos, |
271 |
|
int16_t data[6 * 64], |
272 |
|
int16_t qcoeff[6 * 64]) |
273 |
{ |
{ |
274 |
|
MBTrans(pParam,frame,pMB,x_pos,y_pos,data); |
275 |
|
MBfDCT(pParam,frame,pMB,data); |
276 |
|
MBQuantIntra(pParam,frame,pMB,data,qcoeff); |
277 |
|
MBDeQuantIntra(pParam,frame->quant,data,qcoeff); |
278 |
|
MBiDCT(data,0x3F); |
279 |
|
MBTransAdd(pParam,frame,pMB,x_pos,y_pos,data,0x3F); |
280 |
|
} |
281 |
|
|
282 |
|
|
283 |
|
uint8_t |
284 |
|
MBTransQuantInter2(const MBParam * pParam, |
285 |
|
FRAMEINFO * frame, |
286 |
|
MACROBLOCK * pMB, |
287 |
|
const uint32_t x_pos, |
288 |
|
const uint32_t y_pos, |
289 |
|
int16_t data[6 * 64], |
290 |
|
int16_t qcoeff[6 * 64]) |
291 |
|
{ |
292 |
|
uint8_t cbp; |
293 |
|
|
294 |
|
/* there is no MBTrans for Inter block, that's done in motion compensation already */ |
295 |
|
|
296 |
|
MBfDCT(pParam,frame,pMB,data); |
297 |
|
cbp = MBQuantInter(pParam,frame->quant,data,qcoeff); |
298 |
|
MBDeQuantInter(pParam,frame->quant,data,qcoeff,cbp); |
299 |
|
MBiDCT(data,cbp); |
300 |
|
MBTransAdd(pParam,frame,pMB,x_pos,y_pos,data,cbp); |
301 |
|
|
302 |
|
return cbp; |
303 |
|
} |
304 |
|
|
305 |
|
uint8_t |
306 |
|
MBTransQuantInterBVOP(const MBParam * pParam, |
307 |
|
FRAMEINFO * frame, |
308 |
|
MACROBLOCK * pMB, |
309 |
|
int16_t data[6 * 64], |
310 |
|
int16_t qcoeff[6 * 64]) |
311 |
|
{ |
312 |
|
uint8_t cbp; |
313 |
|
|
314 |
|
/* there is no MBTrans for Inter block, that's done in motion compensation already */ |
315 |
|
|
316 |
|
MBfDCT(pParam,frame,pMB,data); |
317 |
|
cbp = MBQuantInter(pParam,frame->quant,data,qcoeff); |
318 |
|
|
319 |
|
/* we don't have to DeQuant, iDCT and Transfer back data for B-frames */ |
320 |
|
|
321 |
|
return cbp; |
322 |
|
} |
323 |
|
|
324 |
|
|
325 |
|
void |
326 |
|
MBfDCT(const MBParam * pParam, |
327 |
|
FRAMEINFO * frame, |
328 |
|
MACROBLOCK * pMB, |
329 |
|
int16_t data[6 * 64]) |
330 |
|
{ |
331 |
|
int i; |
332 |
|
|
333 |
start_timer(); |
start_timer(); |
334 |
sum = quant4_inter(&qcoeff[i*64], &data[i*64], iQuant); |
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 |
|
start_timer(); |
342 |
|
fdct(&data[i * 64]); |
343 |
|
stop_dct_timer(); |
344 |
|
} |
345 |
|
} |
346 |
|
|
347 |
|
void |
348 |
|
MBQuantDeQuantIntra(const MBParam * pParam, |
349 |
|
FRAMEINFO * frame, |
350 |
|
MACROBLOCK * pMB, |
351 |
|
int16_t qcoeff[6 * 64], |
352 |
|
int16_t data[6*64]) |
353 |
|
{ |
354 |
|
int i; |
355 |
|
int iQuant = frame->quant; |
356 |
|
|
357 |
|
start_timer(); |
358 |
|
pMB->field_dct = 0; |
359 |
|
if ((frame->global_flags & XVID_INTERLACING)) { |
360 |
|
pMB->field_dct = MBDecideFieldDCT(data); |
361 |
|
} |
362 |
|
stop_interlacing_timer(); |
363 |
|
|
364 |
|
for (i = 0; i < 6; i++) { |
365 |
|
uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
366 |
|
|
367 |
|
if (pParam->m_quant_type == H263_QUANT) { |
368 |
|
start_timer(); |
369 |
|
quant_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler); |
370 |
|
stop_quant_timer(); |
371 |
|
|
372 |
|
start_timer(); |
373 |
|
dequant_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler); |
374 |
|
stop_iquant_timer(); |
375 |
|
} else { |
376 |
|
start_timer(); |
377 |
|
quant4_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler); |
378 |
stop_quant_timer(); |
stop_quant_timer(); |
379 |
|
|
380 |
|
start_timer(); |
381 |
|
dequant4_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler); |
382 |
|
stop_iquant_timer(); |
383 |
|
} |
384 |
|
} |
385 |
} |
} |
386 |
|
|
387 |
if(sum >= TOOSMALL_LIMIT) { // skip block ? |
void |
388 |
|
MBQuantIntra(const MBParam * pParam, |
389 |
|
FRAMEINFO * frame, |
390 |
|
MACROBLOCK *pMB, |
391 |
|
int16_t data[6 * 64], |
392 |
|
int16_t qcoeff[6 * 64]) |
393 |
|
{ |
394 |
|
int i; |
395 |
|
int iQuant = frame->quant; |
396 |
|
|
397 |
|
start_timer(); |
398 |
|
pMB->field_dct = 0; |
399 |
|
if ((frame->global_flags & XVID_INTERLACING)) { |
400 |
|
pMB->field_dct = MBDecideFieldDCT(data); |
401 |
|
} |
402 |
|
stop_interlacing_timer(); |
403 |
|
|
404 |
|
for (i = 0; i < 6; i++) { |
405 |
|
uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
406 |
|
|
407 |
|
if (pParam->m_quant_type == H263_QUANT) { |
408 |
|
start_timer(); |
409 |
|
quant_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler); |
410 |
|
stop_quant_timer(); |
411 |
|
} else { |
412 |
|
start_timer(); |
413 |
|
quant4_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler); |
414 |
|
stop_quant_timer(); |
415 |
|
} |
416 |
|
} |
417 |
|
} |
418 |
|
|
419 |
if (pParam->quant_type == H263_QUANT) |
void |
420 |
|
MBDeQuantIntra(const MBParam * pParam, |
421 |
|
const int iQuant, |
422 |
|
int16_t qcoeff[6 * 64], |
423 |
|
int16_t data[6*64]) |
424 |
{ |
{ |
425 |
|
int i; |
426 |
|
|
427 |
|
for (i = 0; i < 6; i++) { |
428 |
|
uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
429 |
|
|
430 |
|
if (pParam->m_quant_type == H263_QUANT) { |
431 |
start_timer(); |
start_timer(); |
432 |
dequant_inter(&data[i*64], &qcoeff[i*64], iQuant); |
dequant_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler); |
433 |
|
stop_iquant_timer(); |
434 |
|
} else { |
435 |
|
start_timer(); |
436 |
|
dequant4_intra(&data[i * 64], &qcoeff[i * 64], iQuant, iDcScaler); |
437 |
stop_iquant_timer(); |
stop_iquant_timer(); |
438 |
} |
} |
439 |
else |
} |
440 |
|
} |
441 |
|
|
442 |
|
uint8_t |
443 |
|
MBQuantInter(const MBParam * pParam, |
444 |
|
const int iQuant, |
445 |
|
int16_t data[6 * 64], |
446 |
|
int16_t qcoeff[6 * 64]) |
447 |
{ |
{ |
448 |
|
|
449 |
|
int i; |
450 |
|
uint8_t cbp = 0; |
451 |
|
int sum; |
452 |
|
|
453 |
|
for (i = 0; i < 6; i++) { |
454 |
|
|
455 |
|
if (pParam->m_quant_type == 0) { |
456 |
|
start_timer(); |
457 |
|
sum = quant_inter(&qcoeff[i * 64], &data[i * 64], iQuant); |
458 |
|
stop_quant_timer(); |
459 |
|
} else { |
460 |
|
start_timer(); |
461 |
|
sum = quant4_inter(&qcoeff[i * 64], &data[i * 64], iQuant); |
462 |
|
stop_quant_timer(); |
463 |
|
} |
464 |
|
|
465 |
|
if (sum >= TOOSMALL_LIMIT) { /* skip block ? */ |
466 |
|
cbp |= 1 << (5 - i); |
467 |
|
} |
468 |
|
} |
469 |
|
return cbp; |
470 |
|
} |
471 |
|
|
472 |
|
void |
473 |
|
MBDeQuantInter( const MBParam * pParam, |
474 |
|
const int iQuant, |
475 |
|
int16_t data[6 * 64], |
476 |
|
int16_t qcoeff[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 |
|
if (pParam->m_quant_type == H263_QUANT) { |
485 |
|
start_timer(); |
486 |
|
dequant_inter(&data[i * 64], &qcoeff[i * 64], iQuant); |
487 |
|
stop_iquant_timer(); |
488 |
|
} else { |
489 |
start_timer(); |
start_timer(); |
490 |
dequant4_inter(&data[i*64], &qcoeff[i*64], iQuant); |
dequant4_inter(&data[i*64], &qcoeff[i*64], iQuant); |
491 |
stop_iquant_timer(); |
stop_iquant_timer(); |
492 |
} |
} |
493 |
|
} |
494 |
|
} |
495 |
|
} |
496 |
|
|
497 |
cbp |= 1 << (5 - i); |
void |
498 |
|
MBiDCT( int16_t data[6 * 64], |
499 |
|
const uint8_t cbp) |
500 |
|
{ |
501 |
|
int i; |
502 |
|
|
503 |
|
for (i = 0; i < 6; i++) { |
504 |
|
if (cbp & (1 << (5 - i))) |
505 |
|
{ |
506 |
start_timer(); |
start_timer(); |
507 |
idct(&data[i*64]); |
idct(&data[i*64]); |
508 |
stop_idct_timer(); |
stop_idct_timer(); |
509 |
|
|
510 |
|
} |
511 |
} |
} |
512 |
} |
} |
513 |
|
|
514 |
|
|
515 |
|
void |
516 |
|
MBTrans(const MBParam * pParam, |
517 |
|
FRAMEINFO * frame, |
518 |
|
MACROBLOCK * pMB, |
519 |
|
const uint32_t x_pos, |
520 |
|
const uint32_t y_pos, |
521 |
|
int16_t data[6 * 64]) |
522 |
|
{ |
523 |
|
uint32_t stride = pParam->edged_width; |
524 |
|
uint32_t stride2 = stride / 2; |
525 |
|
uint32_t next_block = stride * 8; |
526 |
|
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
527 |
|
IMAGE *pCurrent = &frame->image; |
528 |
|
|
529 |
|
pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4); |
530 |
|
pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3); |
531 |
|
pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3); |
532 |
|
|
533 |
start_timer(); |
start_timer(); |
534 |
if (pMB->field_dct) |
transfer_8to16copy(&data[0 * 64], pY_Cur, stride); |
535 |
|
transfer_8to16copy(&data[1 * 64], pY_Cur + 8, stride); |
536 |
|
transfer_8to16copy(&data[2 * 64], pY_Cur + next_block, stride); |
537 |
|
transfer_8to16copy(&data[3 * 64], pY_Cur + next_block + 8, stride); |
538 |
|
transfer_8to16copy(&data[4 * 64], pU_Cur, stride2); |
539 |
|
transfer_8to16copy(&data[5 * 64], pV_Cur, stride2); |
540 |
|
stop_transfer_timer(); |
541 |
|
} |
542 |
|
|
543 |
|
void |
544 |
|
MBTransAdd(const MBParam * pParam, |
545 |
|
FRAMEINFO * frame, |
546 |
|
MACROBLOCK * pMB, |
547 |
|
const uint32_t x_pos, |
548 |
|
const uint32_t y_pos, |
549 |
|
int16_t data[6 * 64], |
550 |
|
const uint8_t cbp) |
551 |
{ |
{ |
552 |
MBFieldToFrame(data); |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
553 |
|
uint32_t stride = pParam->edged_width; |
554 |
|
uint32_t stride2 = stride / 2; |
555 |
|
uint32_t next_block = stride * 8; |
556 |
|
IMAGE *pCurrent = &frame->image; |
557 |
|
|
558 |
|
pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4); |
559 |
|
pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3); |
560 |
|
pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3); |
561 |
|
|
562 |
|
if (pMB->field_dct) { |
563 |
|
next_block = stride; |
564 |
|
stride *= 2; |
565 |
} |
} |
|
stop_interlacing_timer(); |
|
566 |
|
|
567 |
start_timer(); |
start_timer(); |
568 |
if (cbp & 32) |
if (cbp & 32) |
570 |
if (cbp & 16) |
if (cbp & 16) |
571 |
transfer_16to8add(pY_Cur + 8, &data[1*64], stride); |
transfer_16to8add(pY_Cur + 8, &data[1*64], stride); |
572 |
if (cbp & 8) |
if (cbp & 8) |
573 |
transfer_16to8add(pY_Cur + 8 * stride, &data[2*64], stride); |
transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride); |
574 |
if (cbp & 4) |
if (cbp & 4) |
575 |
transfer_16to8add(pY_Cur + 8 + 8 * stride, &data[3*64], stride); |
transfer_16to8add(pY_Cur + next_block + 8, &data[3 * 64], stride); |
576 |
if (cbp & 2) |
if (cbp & 2) |
577 |
transfer_16to8add(pU_Cur, &data[4*64], stride / 2); |
transfer_16to8add(pU_Cur, &data[4 * 64], stride2); |
578 |
if (cbp & 1) |
if (cbp & 1) |
579 |
transfer_16to8add(pV_Cur, &data[5*64], stride / 2); |
transfer_16to8add(pV_Cur, &data[5 * 64], stride2); |
580 |
stop_transfer_timer(); |
stop_transfer_timer(); |
|
|
|
|
return cbp; |
|
|
|
|
581 |
} |
} |
582 |
|
|
583 |
|
|
584 |
|
|
585 |
/* 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 */ |
586 |
|
|
|
#define ABS(X) (X)<0 ? -(X) : (X) |
|
587 |
|
|
588 |
uint32_t MBDecideFieldDCT(int16_t data[6*64]) |
uint32_t |
589 |
|
MBDecideFieldDCT(int16_t data[6 * 64]) |
590 |
{ |
{ |
591 |
|
|
592 |
const uint8_t blocks[] = {0*64, 0*64, 0*64, 0*64, 2*64, 2*64, 2*64, 2*64}; |
const uint8_t blocks[] = |
593 |
|
{ 0 * 64, 0 * 64, 0 * 64, 0 * 64, 2 * 64, 2 * 64, 2 * 64, 2 * 64 }; |
594 |
const uint8_t lines[] = {0, 16, 32, 48, 0, 16, 32, 48}; |
const uint8_t lines[] = {0, 16, 32, 48, 0, 16, 32, 48}; |
595 |
|
|
596 |
int frame = 0, field = 0; |
int frame = 0, field = 0; |
597 |
int i, j; |
int i, j; |
598 |
|
|
599 |
for (i=0 ; i<7 ; ++i) |
for (i = 0; i < 7; ++i) { |
600 |
{ |
for (j = 0; j < 8; ++j) { |
601 |
for (j=0 ; j<8 ; ++j) |
frame += |
602 |
{ |
ABS(data[0 * 64 + (i + 1) * 8 + j] - data[0 * 64 + i * 8 + j]); |
603 |
frame += ABS(data[0*64 + (i+1)*8 + j] - data[0*64 + i*8 + j]); |
frame += |
604 |
frame += ABS(data[1*64 + (i+1)*8 + j] - data[1*64 + i*8 + j]); |
ABS(data[1 * 64 + (i + 1) * 8 + j] - data[1 * 64 + i * 8 + j]); |
605 |
frame += ABS(data[2*64 + (i+1)*8 + j] - data[2*64 + i*8 + j]); |
frame += |
606 |
frame += ABS(data[3*64 + (i+1)*8 + j] - data[3*64 + i*8 + j]); |
ABS(data[2 * 64 + (i + 1) * 8 + j] - data[2 * 64 + i * 8 + j]); |
607 |
|
frame += |
608 |
|
ABS(data[3 * 64 + (i + 1) * 8 + j] - data[3 * 64 + i * 8 + j]); |
609 |
|
|
610 |
field += ABS(data[blocks[i+1] + lines[i+1] + j] -\ |
field += |
611 |
|
ABS(data[blocks[i + 1] + lines[i + 1] + j] - |
612 |
data[blocks[i ] + lines[i ] + j]); |
data[blocks[i ] + lines[i ] + j]); |
613 |
field += ABS(data[blocks[i+1] + lines[i+1] + 8 + j] -\ |
field += |
614 |
|
ABS(data[blocks[i + 1] + lines[i + 1] + 8 + j] - |
615 |
data[blocks[i ] + lines[i ] + 8 + j]); |
data[blocks[i ] + lines[i ] + 8 + j]); |
616 |
field += ABS(data[blocks[i+1] + 64 + lines[i+1] + j] -\ |
field += |
617 |
|
ABS(data[blocks[i + 1] + 64 + lines[i + 1] + j] - |
618 |
data[blocks[i ] + 64 + lines[i ] + j]); |
data[blocks[i ] + 64 + lines[i ] + j]); |
619 |
field += ABS(data[blocks[i+1] + 64 + lines[i+1] + 8 + j] -\ |
field += |
620 |
|
ABS(data[blocks[i + 1] + 64 + lines[i + 1] + 8 + j] - |
621 |
data[blocks[i ] + 64 + lines[i ] + 8 + j]); |
data[blocks[i ] + 64 + lines[i ] + 8 + j]); |
622 |
} |
} |
623 |
} |
} |
624 |
|
|
625 |
if (frame > field) |
if (frame > (field + 350)) { |
|
{ |
|
626 |
MBFrameToField(data); |
MBFrameToField(data); |
627 |
} |
} |
628 |
|
|
629 |
return (frame > field); |
return (frame > (field + 350)); |
630 |
} |
} |
631 |
|
|
632 |
|
|
635 |
#define MOVLINE(X,Y) memcpy(X, Y, sizeof(tmp)) |
#define MOVLINE(X,Y) memcpy(X, Y, sizeof(tmp)) |
636 |
#define LINE(X,Y) &data[X*64 + Y*8] |
#define LINE(X,Y) &data[X*64 + Y*8] |
637 |
|
|
638 |
void MBFrameToField(int16_t data[6*64]) |
void |
639 |
|
MBFrameToField(int16_t data[6 * 64]) |
640 |
{ |
{ |
641 |
int16_t tmp[8]; |
int16_t tmp[8]; |
642 |
|
|
643 |
/* left blocks */ |
/* left blocks */ |
644 |
|
|
645 |
// 1=2, 2=4, 4=8, 8=1 |
/* 1=2, 2=4, 4=8, 8=1 */ |
646 |
MOVLINE(tmp, LINE(0,1)); |
MOVLINE(tmp, LINE(0,1)); |
647 |
MOVLINE(LINE(0,1), LINE(0,2)); |
MOVLINE(LINE(0,1), LINE(0,2)); |
648 |
MOVLINE(LINE(0,2), LINE(0,4)); |
MOVLINE(LINE(0,2), LINE(0,4)); |
649 |
MOVLINE(LINE(0,4), LINE(2,0)); |
MOVLINE(LINE(0,4), LINE(2,0)); |
650 |
MOVLINE(LINE(2,0), tmp); |
MOVLINE(LINE(2,0), tmp); |
651 |
|
|
652 |
// 3=6, 6=12, 12=9, 9=3 |
/* 3=6, 6=12, 12=9, 9=3 */ |
653 |
MOVLINE(tmp, LINE(0,3)); |
MOVLINE(tmp, LINE(0,3)); |
654 |
MOVLINE(LINE(0,3), LINE(0,6)); |
MOVLINE(LINE(0,3), LINE(0,6)); |
655 |
MOVLINE(LINE(0,6), LINE(2,4)); |
MOVLINE(LINE(0,6), LINE(2,4)); |
656 |
MOVLINE(LINE(2,4), LINE(2,1)); |
MOVLINE(LINE(2,4), LINE(2,1)); |
657 |
MOVLINE(LINE(2,1), tmp); |
MOVLINE(LINE(2,1), tmp); |
658 |
|
|
659 |
// 5=10, 10=5 |
/* 5=10, 10=5 */ |
660 |
MOVLINE(tmp, LINE(0,5)); |
MOVLINE(tmp, LINE(0,5)); |
661 |
MOVLINE(LINE(0,5), LINE(2,2)); |
MOVLINE(LINE(0,5), LINE(2,2)); |
662 |
MOVLINE(LINE(2,2), tmp); |
MOVLINE(LINE(2,2), tmp); |
663 |
|
|
664 |
// 7=14, 14=13, 13=11, 11=7 |
/* 7=14, 14=13, 13=11, 11=7 */ |
665 |
MOVLINE(tmp, LINE(0,7)); |
MOVLINE(tmp, LINE(0,7)); |
666 |
MOVLINE(LINE(0,7), LINE(2,6)); |
MOVLINE(LINE(0,7), LINE(2,6)); |
667 |
MOVLINE(LINE(2,6), LINE(2,5)); |
MOVLINE(LINE(2,6), LINE(2,5)); |
670 |
|
|
671 |
/* right blocks */ |
/* right blocks */ |
672 |
|
|
673 |
// 1=2, 2=4, 4=8, 8=1 |
/* 1=2, 2=4, 4=8, 8=1 */ |
674 |
MOVLINE(tmp, LINE(1,1)); |
MOVLINE(tmp, LINE(1,1)); |
675 |
MOVLINE(LINE(1,1), LINE(1,2)); |
MOVLINE(LINE(1,1), LINE(1,2)); |
676 |
MOVLINE(LINE(1,2), LINE(1,4)); |
MOVLINE(LINE(1,2), LINE(1,4)); |
677 |
MOVLINE(LINE(1,4), LINE(3,0)); |
MOVLINE(LINE(1,4), LINE(3,0)); |
678 |
MOVLINE(LINE(3,0), tmp); |
MOVLINE(LINE(3,0), tmp); |
679 |
|
|
680 |
// 3=6, 6=12, 12=9, 9=3 |
/* 3=6, 6=12, 12=9, 9=3 */ |
681 |
MOVLINE(tmp, LINE(1,3)); |
MOVLINE(tmp, LINE(1,3)); |
682 |
MOVLINE(LINE(1,3), LINE(1,6)); |
MOVLINE(LINE(1,3), LINE(1,6)); |
683 |
MOVLINE(LINE(1,6), LINE(3,4)); |
MOVLINE(LINE(1,6), LINE(3,4)); |
684 |
MOVLINE(LINE(3,4), LINE(3,1)); |
MOVLINE(LINE(3,4), LINE(3,1)); |
685 |
MOVLINE(LINE(3,1), tmp); |
MOVLINE(LINE(3,1), tmp); |
686 |
|
|
687 |
// 5=10, 10=5 |
/* 5=10, 10=5 */ |
688 |
MOVLINE(tmp, LINE(1,5)); |
MOVLINE(tmp, LINE(1,5)); |
689 |
MOVLINE(LINE(1,5), LINE(3,2)); |
MOVLINE(LINE(1,5), LINE(3,2)); |
690 |
MOVLINE(LINE(3,2), tmp); |
MOVLINE(LINE(3,2), tmp); |
691 |
|
|
692 |
// 7=14, 14=13, 13=11, 11=7 |
/* 7=14, 14=13, 13=11, 11=7 */ |
693 |
MOVLINE(tmp, LINE(1,7)); |
MOVLINE(tmp, LINE(1,7)); |
694 |
MOVLINE(LINE(1,7), LINE(3,6)); |
MOVLINE(LINE(1,7), LINE(3,6)); |
695 |
MOVLINE(LINE(3,6), LINE(3,5)); |
MOVLINE(LINE(3,6), LINE(3,5)); |
696 |
MOVLINE(LINE(3,5), LINE(3,3)); |
MOVLINE(LINE(3,5), LINE(3,3)); |
697 |
MOVLINE(LINE(3,3), tmp); |
MOVLINE(LINE(3,3), tmp); |
698 |
} |
} |
|
|
|
|
|
|
|
/* interlace Y blocks vertically */ |
|
|
|
|
|
void MBFieldToFrame(int16_t data[6*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); |
|
|
} |
|