[svn] / branches / dev-api-4 / xvidcore / src / utils / mbtransquant.c Repository:
ViewVC logotype

Annotation of /branches/dev-api-4/xvidcore/src/utils/mbtransquant.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 78 - (view) (download)
Original Path: trunk/xvidcore/src/utils/mbtransquant.c

1 : Isibaar 3 /******************************************************************************
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 69 * 26.03.2002 interlacing support - moved transfers outside loops
46 : Isibaar 3 * 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 78 #include <string.h>
53 :    
54 : Isibaar 3 #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 78 MACROBLOCK * pMB,
75 : Isibaar 3 const uint32_t x_pos,
76 :     const uint32_t y_pos,
77 : edgomez 78 int16_t data[6*64],
78 :     int16_t qcoeff[6*64],
79 :     IMAGE * const pCurrent)
80 : Isibaar 3
81 :     {
82 : edgomez 78
83 : Isibaar 3 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 78 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 69
92 :     start_timer();
93 : edgomez 78 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 69 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 3 uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);
112 :    
113 :     start_timer();
114 : edgomez 78 fdct(&data[i*64]);
115 : Isibaar 3 stop_dct_timer();
116 :    
117 :     if (pParam->quant_type == H263_QUANT)
118 :     {
119 :     start_timer();
120 : edgomez 78 quant_intra(&qcoeff[i*64], &data[i*64], iQuant, iDcScaler);
121 : Isibaar 3 stop_quant_timer();
122 :    
123 :     start_timer();
124 : edgomez 78 dequant_intra(&data[i*64], &qcoeff[i*64], iQuant, iDcScaler);
125 : Isibaar 3 stop_iquant_timer();
126 :     }
127 :     else
128 :     {
129 :     start_timer();
130 : edgomez 78 quant4_intra(&qcoeff[i*64], &data[i*64], iQuant, iDcScaler);
131 : Isibaar 3 stop_quant_timer();
132 :    
133 :     start_timer();
134 : edgomez 78 dequant4_intra(&data[i*64], &qcoeff[i*64], iQuant, iDcScaler);
135 : Isibaar 3 stop_iquant_timer();
136 :     }
137 :    
138 :     start_timer();
139 : edgomez 78 idct(&data[i*64]);
140 : Isibaar 3 stop_idct_timer();
141 : edgomez 78 }
142 : Isibaar 3
143 : h 69 start_timer();
144 :     if (pMB->field_dct)
145 :     {
146 :     MBFieldToFrame(data);
147 :     }
148 :     stop_interlacing_timer();
149 :    
150 :     start_timer();
151 : edgomez 78 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 69 stop_transfer_timer();
158 : edgomez 78
159 : Isibaar 3 }
160 :    
161 :    
162 :     uint8_t MBTransQuantInter(const MBParam *pParam,
163 : edgomez 78 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 3
169 :     {
170 : edgomez 78
171 : Isibaar 3 const uint32_t stride = pParam->edged_width;
172 : edgomez 78 uint32_t i;
173 :     uint32_t iQuant = pParam->quant;
174 : Isibaar 3 uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
175 : edgomez 78 uint8_t cbp = 0;
176 : Isibaar 3 uint32_t sum;
177 :    
178 : edgomez 78 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 3
182 : h 69 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 78 for(i = 0; i < 6; i++)
191 : h 69 {
192 : Isibaar 3 /*
193 : edgomez 78 * no need to transfer 8->16-bit
194 :     * (this is performed already in motion compensation)
195 :     */
196 : Isibaar 3 start_timer();
197 : edgomez 78 fdct(&data[i*64]);
198 : Isibaar 3 stop_dct_timer();
199 :    
200 :     if (pParam->quant_type == 0)
201 :     {
202 :     start_timer();
203 : edgomez 78 sum = quant_inter(&qcoeff[i*64], &data[i*64], iQuant);
204 : Isibaar 3 stop_quant_timer();
205 :     }
206 :     else
207 :     {
208 :     start_timer();
209 : edgomez 78 sum = quant4_inter(&qcoeff[i*64], &data[i*64], iQuant);
210 : Isibaar 3 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 78 dequant_inter(&data[i*64], &qcoeff[i*64], iQuant);
219 : Isibaar 3 stop_iquant_timer();
220 :     }
221 :     else
222 :     {
223 :     start_timer();
224 : edgomez 78 dequant4_inter(&data[i*64], &qcoeff[i*64], iQuant);
225 : Isibaar 3 stop_iquant_timer();
226 :     }
227 :    
228 :     cbp |= 1 << (5 - i);
229 :    
230 :     start_timer();
231 : edgomez 78 idct(&data[i*64]);
232 : Isibaar 3 stop_idct_timer();
233 :     }
234 :     }
235 : h 69
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 78 transfer_16to8add(pY_Cur, &data[0*64], stride);
246 : h 69 if (cbp & 16)
247 : edgomez 78 transfer_16to8add(pY_Cur + 8, &data[1*64], stride);
248 : h 69 if (cbp & 8)
249 : edgomez 78 transfer_16to8add(pY_Cur + 8 * stride, &data[2*64], stride);
250 : h 69 if (cbp & 4)
251 : edgomez 78 transfer_16to8add(pY_Cur + 8 + 8 * stride, &data[3*64], stride);
252 : h 69 if (cbp & 2)
253 : edgomez 78 transfer_16to8add(pU_Cur, &data[4*64], stride / 2);
254 : h 69 if (cbp & 1)
255 : edgomez 78 transfer_16to8add(pV_Cur, &data[5*64], stride / 2);
256 : h 69 stop_transfer_timer();
257 :    
258 : edgomez 78 return cbp;
259 :    
260 : Isibaar 3 }
261 : h 69
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 :    
267 : edgomez 78 uint32_t MBDecideFieldDCT(int16_t data[6*64])
268 : h 69 {
269 :    
270 : edgomez 78 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 :    
273 : h 69 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 78 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 : h 69
285 : edgomez 78 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 : h 69 }
294 :     }
295 :    
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 78 #define LINE(X,Y) &data[X*64 + Y*8]
309 : h 69
310 : edgomez 78 void MBFrameToField(int16_t data[6*64])
311 : h 69 {
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 78 void MBFieldToFrame(int16_t data[6*64])
375 : h 69 {
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 :     }

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4