Parent Directory
|
Revision Log
Revision 1.3 - (view) (download)
1 : | Isibaar | 1.1 | /****************************************************************************** |
2 : | * * | ||
3 : | * This file is part of XviD, a free MPEG-4 video encoder/decoder * | ||
4 : | * * | ||
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 * | ||
7 : | * software module in hardware or software products are advised that its * | ||
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 * | ||
10 : | * software module and his/her company, and subsequent editors and their * | ||
11 : | * companies, will have no liability for use of this software or * | ||
12 : | * modifications or derivatives thereof. * | ||
13 : | * * | ||
14 : | * XviD is free software; you can redistribute it and/or modify it * | ||
15 : | * under the terms of the GNU General Public License as published by * | ||
16 : | * the Free Software Foundation; either version 2 of the License, or * | ||
17 : | * (at your option) any later version. * | ||
18 : | * * | ||
19 : | * XviD is distributed in the hope that it will be useful, but * | ||
20 : | * WITHOUT ANY WARRANTY; without even the implied warranty of * | ||
21 : | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * | ||
22 : | * GNU General Public License for more details. * | ||
23 : | * * | ||
24 : | * You should have received a copy of the GNU General Public License * | ||
25 : | * along with this program; if not, write to the Free Software * | ||
26 : | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * | ||
27 : | * * | ||
28 : | ******************************************************************************/ | ||
29 : | |||
30 : | /****************************************************************************** | ||
31 : | * * | ||
32 : | * mbtransquant.c * | ||
33 : | * * | ||
34 : | * Copyright (C) 2001 - Peter Ross <pross@cs.rmit.edu.au> * | ||
35 : | * Copyright (C) 2001 - Michael Militzer <isibaar@xvid.org> * | ||
36 : | * * | ||
37 : | * For more information visit the XviD homepage: http://www.xvid.org * | ||
38 : | * * | ||
39 : | ******************************************************************************/ | ||
40 : | |||
41 : | /****************************************************************************** | ||
42 : | * * | ||
43 : | * Revision history: * | ||
44 : | * * | ||
45 : | h | 1.2 | * 26.03.2002 interlacing support - moved transfers outside loops |
46 : | Isibaar | 1.1 | * 22.12.2001 get_dc_scaler() moved to common.h |
47 : | * 19.11.2001 introduced coefficient thresholding (Isibaar) * | ||
48 : | * 17.11.2001 initial version * | ||
49 : | * * | ||
50 : | ******************************************************************************/ | ||
51 : | |||
52 : | edgomez | 1.3 | #include <string.h> |
53 : | |||
54 : | Isibaar | 1.1 | #include "../portab.h" |
55 : | #include "mbfunctions.h" | ||
56 : | |||
57 : | #include "../global.h" | ||
58 : | #include "mem_transfer.h" | ||
59 : | #include "timer.h" | ||
60 : | #include "../dct/fdct.h" | ||
61 : | #include "../dct/idct.h" | ||
62 : | #include "../quant/quant_mpeg4.h" | ||
63 : | #include "../quant/quant_h263.h" | ||
64 : | #include "../encoder.h" | ||
65 : | |||
66 : | #define MIN(X, Y) ((X)<(Y)?(X):(Y)) | ||
67 : | #define MAX(X, Y) ((X)>(Y)?(X):(Y)) | ||
68 : | |||
69 : | #define TOOSMALL_LIMIT 1 /* skip blocks having a coefficient sum below this value */ | ||
70 : | |||
71 : | /* this isnt pretty, but its better than 20 ifdefs */ | ||
72 : | |||
73 : | void MBTransQuantIntra(const MBParam *pParam, | ||
74 : | edgomez | 1.3 | MACROBLOCK * pMB, |
75 : | Isibaar | 1.1 | const uint32_t x_pos, |
76 : | const uint32_t y_pos, | ||
77 : | edgomez | 1.3 | int16_t data[6*64], |
78 : | int16_t qcoeff[6*64], | ||
79 : | IMAGE * const pCurrent) | ||
80 : | Isibaar | 1.1 | |
81 : | { | ||
82 : | edgomez | 1.3 | |
83 : | Isibaar | 1.1 | const uint32_t stride = pParam->edged_width; |
84 : | uint32_t i; | ||
85 : | uint32_t iQuant = pParam->quant; | ||
86 : | uint8_t *pY_Cur, *pU_Cur, *pV_Cur; | ||
87 : | |||
88 : | edgomez | 1.3 | pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4); |
89 : | pU_Cur = pCurrent->u + (y_pos << 3) * (stride >> 1) + (x_pos << 3); | ||
90 : | pV_Cur = pCurrent->v + (y_pos << 3) * (stride >> 1) + (x_pos << 3); | ||
91 : | h | 1.2 | |
92 : | start_timer(); | ||
93 : | edgomez | 1.3 | transfer_8to16copy(&data[0*64], pY_Cur, stride); |
94 : | transfer_8to16copy(&data[1*64], pY_Cur + 8, stride); | ||
95 : | transfer_8to16copy(&data[2*64], pY_Cur + 8 * stride, stride); | ||
96 : | transfer_8to16copy(&data[3*64], pY_Cur + 8 * stride + 8, stride); | ||
97 : | transfer_8to16copy(&data[4*64], pU_Cur, stride / 2); | ||
98 : | transfer_8to16copy(&data[5*64], pV_Cur, stride / 2); | ||
99 : | h | 1.2 | stop_transfer_timer(); |
100 : | |||
101 : | start_timer(); | ||
102 : | pMB->field_dct = 0; | ||
103 : | if (pParam->global_flags & XVID_INTERLACING) | ||
104 : | { | ||
105 : | pMB->field_dct = MBDecideFieldDCT(data); | ||
106 : | } | ||
107 : | stop_interlacing_timer(); | ||
108 : | |||
109 : | for(i = 0; i < 6; i++) | ||
110 : | { | ||
111 : | Isibaar | 1.1 | uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
112 : | |||
113 : | start_timer(); | ||
114 : | edgomez | 1.3 | fdct(&data[i*64]); |
115 : | Isibaar | 1.1 | stop_dct_timer(); |
116 : | |||
117 : | if (pParam->quant_type == H263_QUANT) | ||
118 : | { | ||
119 : | start_timer(); | ||
120 : | edgomez | 1.3 | quant_intra(&qcoeff[i*64], &data[i*64], iQuant, iDcScaler); |
121 : | Isibaar | 1.1 | stop_quant_timer(); |
122 : | |||
123 : | start_timer(); | ||
124 : | edgomez | 1.3 | dequant_intra(&data[i*64], &qcoeff[i*64], iQuant, iDcScaler); |
125 : | Isibaar | 1.1 | stop_iquant_timer(); |
126 : | } | ||
127 : | else | ||
128 : | { | ||
129 : | start_timer(); | ||
130 : | edgomez | 1.3 | quant4_intra(&qcoeff[i*64], &data[i*64], iQuant, iDcScaler); |
131 : | Isibaar | 1.1 | stop_quant_timer(); |
132 : | |||
133 : | start_timer(); | ||
134 : | edgomez | 1.3 | dequant4_intra(&data[i*64], &qcoeff[i*64], iQuant, iDcScaler); |
135 : | Isibaar | 1.1 | stop_iquant_timer(); |
136 : | } | ||
137 : | |||
138 : | start_timer(); | ||
139 : | edgomez | 1.3 | idct(&data[i*64]); |
140 : | Isibaar | 1.1 | stop_idct_timer(); |
141 : | edgomez | 1.3 | } |
142 : | h | 1.2 | |
143 : | start_timer(); | ||
144 : | if (pMB->field_dct) | ||
145 : | { | ||
146 : | MBFieldToFrame(data); | ||
147 : | } | ||
148 : | stop_interlacing_timer(); | ||
149 : | Isibaar | 1.1 | |
150 : | h | 1.2 | start_timer(); |
151 : | edgomez | 1.3 | transfer_16to8copy(pY_Cur, &data[0*64], stride); |
152 : | transfer_16to8copy(pY_Cur + 8, &data[1*64], stride); | ||
153 : | transfer_16to8copy(pY_Cur + 8 * stride, &data[2*64], stride); | ||
154 : | transfer_16to8copy(pY_Cur + 8 + 8 * stride, &data[3*64], stride); | ||
155 : | transfer_16to8copy(pU_Cur, &data[4*64], stride / 2); | ||
156 : | transfer_16to8copy(pV_Cur, &data[5*64], stride / 2); | ||
157 : | h | 1.2 | stop_transfer_timer(); |
158 : | edgomez | 1.3 | |
159 : | Isibaar | 1.1 | } |
160 : | |||
161 : | |||
162 : | uint8_t MBTransQuantInter(const MBParam *pParam, | ||
163 : | edgomez | 1.3 | MACROBLOCK * pMB, |
164 : | const uint32_t x_pos, const uint32_t y_pos, | ||
165 : | int16_t data[6*64], | ||
166 : | int16_t qcoeff[6*64], | ||
167 : | IMAGE * const pCurrent) | ||
168 : | Isibaar | 1.1 | |
169 : | { | ||
170 : | edgomez | 1.3 | |
171 : | Isibaar | 1.1 | const uint32_t stride = pParam->edged_width; |
172 : | edgomez | 1.3 | uint32_t i; |
173 : | uint32_t iQuant = pParam->quant; | ||
174 : | Isibaar | 1.1 | uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
175 : | edgomez | 1.3 | uint8_t cbp = 0; |
176 : | Isibaar | 1.1 | uint32_t sum; |
177 : | |||
178 : | edgomez | 1.3 | pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4); |
179 : | pU_Cur = pCurrent->u + (y_pos << 3) * (stride >> 1) + (x_pos << 3); | ||
180 : | pV_Cur = pCurrent->v + (y_pos << 3) * (stride >> 1) + (x_pos << 3); | ||
181 : | Isibaar | 1.1 | |
182 : | h | 1.2 | start_timer(); |
183 : | pMB->field_dct = 0; | ||
184 : | if (pParam->global_flags & XVID_INTERLACING) | ||
185 : | { | ||
186 : | pMB->field_dct = MBDecideFieldDCT(data); | ||
187 : | } | ||
188 : | stop_interlacing_timer(); | ||
189 : | |||
190 : | edgomez | 1.3 | for(i = 0; i < 6; i++) |
191 : | h | 1.2 | { |
192 : | Isibaar | 1.1 | /* |
193 : | edgomez | 1.3 | * no need to transfer 8->16-bit |
194 : | * (this is performed already in motion compensation) | ||
195 : | */ | ||
196 : | Isibaar | 1.1 | start_timer(); |
197 : | edgomez | 1.3 | fdct(&data[i*64]); |
198 : | Isibaar | 1.1 | stop_dct_timer(); |
199 : | |||
200 : | if (pParam->quant_type == 0) | ||
201 : | { | ||
202 : | start_timer(); | ||
203 : | edgomez | 1.3 | sum = quant_inter(&qcoeff[i*64], &data[i*64], iQuant); |
204 : | Isibaar | 1.1 | stop_quant_timer(); |
205 : | } | ||
206 : | else | ||
207 : | { | ||
208 : | start_timer(); | ||
209 : | edgomez | 1.3 | sum = quant4_inter(&qcoeff[i*64], &data[i*64], iQuant); |
210 : | Isibaar | 1.1 | stop_quant_timer(); |
211 : | } | ||
212 : | |||
213 : | if(sum >= TOOSMALL_LIMIT) { // skip block ? | ||
214 : | |||
215 : | if (pParam->quant_type == H263_QUANT) | ||
216 : | { | ||
217 : | start_timer(); | ||
218 : | edgomez | 1.3 | dequant_inter(&data[i*64], &qcoeff[i*64], iQuant); |
219 : | Isibaar | 1.1 | stop_iquant_timer(); |
220 : | } | ||
221 : | else | ||
222 : | { | ||
223 : | start_timer(); | ||
224 : | edgomez | 1.3 | dequant4_inter(&data[i*64], &qcoeff[i*64], iQuant); |
225 : | Isibaar | 1.1 | stop_iquant_timer(); |
226 : | } | ||
227 : | |||
228 : | cbp |= 1 << (5 - i); | ||
229 : | |||
230 : | start_timer(); | ||
231 : | edgomez | 1.3 | idct(&data[i*64]); |
232 : | Isibaar | 1.1 | stop_idct_timer(); |
233 : | h | 1.2 | } |
234 : | } | ||
235 : | |||
236 : | start_timer(); | ||
237 : | if (pMB->field_dct) | ||
238 : | { | ||
239 : | MBFieldToFrame(data); | ||
240 : | } | ||
241 : | stop_interlacing_timer(); | ||
242 : | |||
243 : | start_timer(); | ||
244 : | if (cbp & 32) | ||
245 : | edgomez | 1.3 | transfer_16to8add(pY_Cur, &data[0*64], stride); |
246 : | h | 1.2 | if (cbp & 16) |
247 : | edgomez | 1.3 | transfer_16to8add(pY_Cur + 8, &data[1*64], stride); |
248 : | h | 1.2 | if (cbp & 8) |
249 : | edgomez | 1.3 | transfer_16to8add(pY_Cur + 8 * stride, &data[2*64], stride); |
250 : | h | 1.2 | if (cbp & 4) |
251 : | edgomez | 1.3 | transfer_16to8add(pY_Cur + 8 + 8 * stride, &data[3*64], stride); |
252 : | h | 1.2 | if (cbp & 2) |
253 : | edgomez | 1.3 | transfer_16to8add(pU_Cur, &data[4*64], stride / 2); |
254 : | h | 1.2 | if (cbp & 1) |
255 : | edgomez | 1.3 | transfer_16to8add(pV_Cur, &data[5*64], stride / 2); |
256 : | h | 1.2 | stop_transfer_timer(); |
257 : | |||
258 : | edgomez | 1.3 | return cbp; |
259 : | |||
260 : | h | 1.2 | } |
261 : | |||
262 : | |||
263 : | /* if sum(diff between field lines) < sum(diff between frame lines), use field dct */ | ||
264 : | |||
265 : | #define ABS(X) (X)<0 ? -(X) : (X) | ||
266 : | Isibaar | 1.1 | |
267 : | edgomez | 1.3 | uint32_t MBDecideFieldDCT(int16_t data[6*64]) |
268 : | h | 1.2 | { |
269 : | edgomez | 1.3 | |
270 : | const uint8_t blocks[] = {0*64, 0*64, 0*64, 0*64, 2*64, 2*64, 2*64, 2*64}; | ||
271 : | const uint8_t lines[] = {0, 16, 32, 48, 0, 16, 32, 48}; | ||
272 : | h | 1.2 | |
273 : | int frame = 0, field = 0; | ||
274 : | int i, j; | ||
275 : | |||
276 : | for (i=0 ; i<7 ; ++i) | ||
277 : | { | ||
278 : | for (j=0 ; j<8 ; ++j) | ||
279 : | { | ||
280 : | edgomez | 1.3 | frame += ABS(data[0*64 + (i+1)*8 + j] - data[0*64 + i*8 + j]); |
281 : | frame += ABS(data[1*64 + (i+1)*8 + j] - data[1*64 + i*8 + j]); | ||
282 : | frame += ABS(data[2*64 + (i+1)*8 + j] - data[2*64 + i*8 + j]); | ||
283 : | frame += ABS(data[3*64 + (i+1)*8 + j] - data[3*64 + i*8 + j]); | ||
284 : | |||
285 : | field += ABS(data[blocks[i+1] + lines[i+1] + j] -\ | ||
286 : | data[blocks[i ] + lines[i ] + j]); | ||
287 : | field += ABS(data[blocks[i+1] + lines[i+1] + 8 + j] -\ | ||
288 : | data[blocks[i ] + lines[i ] + 8 + j]); | ||
289 : | field += ABS(data[blocks[i+1] + 64 + lines[i+1] + j] -\ | ||
290 : | data[blocks[i ] + 64 + lines[i ] + j]); | ||
291 : | field += ABS(data[blocks[i+1] + 64 + lines[i+1] + 8 + j] -\ | ||
292 : | data[blocks[i ] + 64 + lines[i ] + 8 + j]); | ||
293 : | Isibaar | 1.1 | } |
294 : | } | ||
295 : | h | 1.2 | |
296 : | if (frame > field) | ||
297 : | { | ||
298 : | MBFrameToField(data); | ||
299 : | } | ||
300 : | |||
301 : | return (frame > field); | ||
302 : | } | ||
303 : | |||
304 : | |||
305 : | /* deinterlace Y blocks vertically */ | ||
306 : | |||
307 : | #define MOVLINE(X,Y) memcpy(X, Y, sizeof(tmp)) | ||
308 : | edgomez | 1.3 | #define LINE(X,Y) &data[X*64 + Y*8] |
309 : | h | 1.2 | |
310 : | edgomez | 1.3 | void MBFrameToField(int16_t data[6*64]) |
311 : | h | 1.2 | { |
312 : | int16_t tmp[8]; | ||
313 : | |||
314 : | /* left blocks */ | ||
315 : | |||
316 : | // 1=2, 2=4, 4=8, 8=1 | ||
317 : | MOVLINE(tmp, LINE(0,1)); | ||
318 : | MOVLINE(LINE(0,1), LINE(0,2)); | ||
319 : | MOVLINE(LINE(0,2), LINE(0,4)); | ||
320 : | MOVLINE(LINE(0,4), LINE(2,0)); | ||
321 : | MOVLINE(LINE(2,0), tmp); | ||
322 : | |||
323 : | // 3=6, 6=12, 12=9, 9=3 | ||
324 : | MOVLINE(tmp, LINE(0,3)); | ||
325 : | MOVLINE(LINE(0,3), LINE(0,6)); | ||
326 : | MOVLINE(LINE(0,6), LINE(2,4)); | ||
327 : | MOVLINE(LINE(2,4), LINE(2,1)); | ||
328 : | MOVLINE(LINE(2,1), tmp); | ||
329 : | |||
330 : | // 5=10, 10=5 | ||
331 : | MOVLINE(tmp, LINE(0,5)); | ||
332 : | MOVLINE(LINE(0,5), LINE(2,2)); | ||
333 : | MOVLINE(LINE(2,2), tmp); | ||
334 : | |||
335 : | // 7=14, 14=13, 13=11, 11=7 | ||
336 : | MOVLINE(tmp, LINE(0,7)); | ||
337 : | MOVLINE(LINE(0,7), LINE(2,6)); | ||
338 : | MOVLINE(LINE(2,6), LINE(2,5)); | ||
339 : | MOVLINE(LINE(2,5), LINE(2,3)); | ||
340 : | MOVLINE(LINE(2,3), tmp); | ||
341 : | |||
342 : | /* right blocks */ | ||
343 : | |||
344 : | // 1=2, 2=4, 4=8, 8=1 | ||
345 : | MOVLINE(tmp, LINE(1,1)); | ||
346 : | MOVLINE(LINE(1,1), LINE(1,2)); | ||
347 : | MOVLINE(LINE(1,2), LINE(1,4)); | ||
348 : | MOVLINE(LINE(1,4), LINE(3,0)); | ||
349 : | MOVLINE(LINE(3,0), tmp); | ||
350 : | |||
351 : | // 3=6, 6=12, 12=9, 9=3 | ||
352 : | MOVLINE(tmp, LINE(1,3)); | ||
353 : | MOVLINE(LINE(1,3), LINE(1,6)); | ||
354 : | MOVLINE(LINE(1,6), LINE(3,4)); | ||
355 : | MOVLINE(LINE(3,4), LINE(3,1)); | ||
356 : | MOVLINE(LINE(3,1), tmp); | ||
357 : | |||
358 : | // 5=10, 10=5 | ||
359 : | MOVLINE(tmp, LINE(1,5)); | ||
360 : | MOVLINE(LINE(1,5), LINE(3,2)); | ||
361 : | MOVLINE(LINE(3,2), tmp); | ||
362 : | |||
363 : | // 7=14, 14=13, 13=11, 11=7 | ||
364 : | MOVLINE(tmp, LINE(1,7)); | ||
365 : | MOVLINE(LINE(1,7), LINE(3,6)); | ||
366 : | MOVLINE(LINE(3,6), LINE(3,5)); | ||
367 : | MOVLINE(LINE(3,5), LINE(3,3)); | ||
368 : | MOVLINE(LINE(3,3), tmp); | ||
369 : | } | ||
370 : | |||
371 : | |||
372 : | /* interlace Y blocks vertically */ | ||
373 : | |||
374 : | edgomez | 1.3 | void MBFieldToFrame(int16_t data[6*64]) |
375 : | h | 1.2 | { |
376 : | uint16_t tmp[8]; | ||
377 : | |||
378 : | /* left blocks */ | ||
379 : | |||
380 : | // 1=8, 8=4, 4=2, 2=1 | ||
381 : | MOVLINE(tmp, LINE(0,1)); | ||
382 : | MOVLINE(LINE(0,1), LINE(2,0)); | ||
383 : | MOVLINE(LINE(2,0), LINE(0,4)); | ||
384 : | MOVLINE(LINE(0,4), LINE(0,2)); | ||
385 : | MOVLINE(LINE(0,2), tmp); | ||
386 : | |||
387 : | // 3=9, 9=12, 12=6, 6=3 | ||
388 : | MOVLINE(tmp, LINE(0,3)); | ||
389 : | MOVLINE(LINE(0,3), LINE(2,1)); | ||
390 : | MOVLINE(LINE(2,1), LINE(2,4)); | ||
391 : | MOVLINE(LINE(2,4), LINE(0,6)); | ||
392 : | MOVLINE(LINE(0,6), tmp); | ||
393 : | |||
394 : | // 5=10, 10=5 | ||
395 : | MOVLINE(tmp, LINE(0,5)); | ||
396 : | MOVLINE(LINE(0,5), LINE(2,2)); | ||
397 : | MOVLINE(LINE(2,2), tmp); | ||
398 : | |||
399 : | // 7=11, 11=13, 13=14, 14=7 | ||
400 : | MOVLINE(tmp, LINE(0,7)); | ||
401 : | MOVLINE(LINE(0,7), LINE(2,3)); | ||
402 : | MOVLINE(LINE(2,3), LINE(2,5)); | ||
403 : | MOVLINE(LINE(2,5), LINE(2,6)); | ||
404 : | MOVLINE(LINE(2,6), tmp); | ||
405 : | |||
406 : | /* right blocks */ | ||
407 : | |||
408 : | // 1=8, 8=4, 4=2, 2=1 | ||
409 : | MOVLINE(tmp, LINE(1,1)); | ||
410 : | MOVLINE(LINE(1,1), LINE(3,0)); | ||
411 : | MOVLINE(LINE(3,0), LINE(1,4)); | ||
412 : | MOVLINE(LINE(1,4), LINE(1,2)); | ||
413 : | MOVLINE(LINE(1,2), tmp); | ||
414 : | |||
415 : | // 3=9, 9=12, 12=6, 6=3 | ||
416 : | MOVLINE(tmp, LINE(1,3)); | ||
417 : | MOVLINE(LINE(1,3), LINE(3,1)); | ||
418 : | MOVLINE(LINE(3,1), LINE(3,4)); | ||
419 : | MOVLINE(LINE(3,4), LINE(1,6)); | ||
420 : | MOVLINE(LINE(1,6), tmp); | ||
421 : | |||
422 : | // 5=10, 10=5 | ||
423 : | MOVLINE(tmp, LINE(1,5)); | ||
424 : | MOVLINE(LINE(1,5), LINE(3,2)); | ||
425 : | MOVLINE(LINE(3,2), tmp); | ||
426 : | |||
427 : | // 7=11, 11=13, 13=14, 14=7 | ||
428 : | MOVLINE(tmp, LINE(1,7)); | ||
429 : | MOVLINE(LINE(1,7), LINE(3,3)); | ||
430 : | MOVLINE(LINE(3,3), LINE(3,5)); | ||
431 : | MOVLINE(LINE(3,5), LINE(3,6)); | ||
432 : | MOVLINE(LINE(3,6), tmp); | ||
433 : | Isibaar | 1.1 | } |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |