[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 1095 - (view) (download)

1 : edgomez 965 /*****************************************************************************
2 :     *
3 :     * XVID MPEG-4 VIDEO CODEC
4 :     * - MB Transfert/Quantization functions -
5 :     *
6 :     * Copyright(C) 2001-2003 Peter Ross <pross@xvid.org>
7 :     * 2001-2003 Michael Militzer <isibaar@xvid.org>
8 :     * 2003 Edouard Gomez <ed.gomez@free.fr>
9 :     *
10 :     * This program is free software ; you can redistribute it and/or modify
11 :     * it under the terms of the GNU General Public License as published by
12 :     * the Free Software Foundation ; either version 2 of the License, or
13 :     * (at your option) any later version.
14 :     *
15 :     * This program is distributed in the hope that it will be useful,
16 :     * but WITHOUT ANY WARRANTY ; without even the implied warranty of
17 :     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 :     * GNU General Public License for more details.
19 :     *
20 :     * You should have received a copy of the GNU General Public License
21 :     * along with this program ; if not, write to the Free Software
22 :     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 :     *
24 : Isibaar 1095 * $Id: mbtransquant.c,v 1.21.2.15 2003-07-24 13:09:14 Isibaar Exp $
25 : edgomez 965 *
26 :     ****************************************************************************/
27 : Isibaar 3
28 : chl 1012 #include <stdio.h>
29 :     #include <stdlib.h>
30 : edgomez 78 #include <string.h>
31 :    
32 : Isibaar 3 #include "../portab.h"
33 :     #include "mbfunctions.h"
34 :    
35 :     #include "../global.h"
36 :     #include "mem_transfer.h"
37 :     #include "timer.h"
38 : chl 995 #include "../bitstream/mbcoding.h"
39 : chl 1011 #include "../bitstream/zigzag.h"
40 : Isibaar 3 #include "../dct/fdct.h"
41 :     #include "../dct/idct.h"
42 :     #include "../quant/quant_mpeg4.h"
43 :     #include "../quant/quant_h263.h"
44 :     #include "../encoder.h"
45 :    
46 : edgomez 851 #include "../image/reduced.h"
47 : Isibaar 3
48 : edgomez 851 MBFIELDTEST_PTR MBFieldTest;
49 : Isibaar 3
50 : edgomez 965 /*
51 :     * Skip blocks having a coefficient sum below this value. This value will be
52 :     * corrected according to the MB quantizer to avoid artifacts for quant==1
53 :     */
54 :     #define PVOP_TOOSMALL_LIMIT 1
55 :     #define BVOP_TOOSMALL_LIMIT 3
56 : Isibaar 3
57 : edgomez 965 /*****************************************************************************
58 :     * Local functions
59 :     ****************************************************************************/
60 :    
61 :     /* permute block and return field dct choice */
62 :     static __inline uint32_t
63 :     MBDecideFieldDCT(int16_t data[6 * 64])
64 : Isibaar 3 {
65 : edgomez 965 uint32_t field = MBFieldTest(data);
66 : edgomez 78
67 : edgomez 965 if (field)
68 :     MBFrameToField(data);
69 : Isibaar 3
70 : edgomez 965 return field;
71 :     }
72 : h 69
73 : edgomez 965 /* Performs Forward DCT on all blocks */
74 :     static __inline void
75 : syskin 984 MBfDCT(const MBParam * const pParam,
76 :     const FRAMEINFO * const frame,
77 :     MACROBLOCK * const pMB,
78 : edgomez 965 uint32_t x_pos,
79 :     uint32_t y_pos,
80 :     int16_t data[6 * 64])
81 : syskin 984 {
82 : edgomez 965 /* Handles interlacing */
83 : h 69 start_timer();
84 :     pMB->field_dct = 0;
85 : edgomez 949 if ((frame->vol_flags & XVID_VOL_INTERLACING) &&
86 : h 390 (x_pos>0) && (x_pos<pParam->mb_width-1) &&
87 :     (y_pos>0) && (y_pos<pParam->mb_height-1)) {
88 : h 69 pMB->field_dct = MBDecideFieldDCT(data);
89 :     }
90 :     stop_interlacing_timer();
91 :    
92 : edgomez 965 /* Perform DCT */
93 :     start_timer();
94 :     fdct(&data[0 * 64]);
95 :     fdct(&data[1 * 64]);
96 :     fdct(&data[2 * 64]);
97 :     fdct(&data[3 * 64]);
98 :     fdct(&data[4 * 64]);
99 :     fdct(&data[5 * 64]);
100 :     stop_dct_timer();
101 :     }
102 :    
103 :     /* Performs Inverse DCT on all blocks */
104 :     static __inline void
105 :     MBiDCT(int16_t data[6 * 64],
106 :     const uint8_t cbp)
107 :     {
108 :     start_timer();
109 :     if(cbp & (1 << (5 - 0))) idct(&data[0 * 64]);
110 :     if(cbp & (1 << (5 - 1))) idct(&data[1 * 64]);
111 :     if(cbp & (1 << (5 - 2))) idct(&data[2 * 64]);
112 :     if(cbp & (1 << (5 - 3))) idct(&data[3 * 64]);
113 :     if(cbp & (1 << (5 - 4))) idct(&data[4 * 64]);
114 :     if(cbp & (1 << (5 - 5))) idct(&data[5 * 64]);
115 :     stop_idct_timer();
116 :     }
117 :    
118 :     /* Quantize all blocks -- Intra mode */
119 :     static __inline void
120 :     MBQuantIntra(const MBParam * pParam,
121 : chl 995 const FRAMEINFO * const frame,
122 : edgomez 965 const MACROBLOCK * pMB,
123 : syskin 984 int16_t qcoeff[6 * 64],
124 : edgomez 965 int16_t data[6*64])
125 :     {
126 :     int i;
127 :    
128 : edgomez 195 for (i = 0; i < 6; i++) {
129 : edgomez 965 uint32_t iDcScaler = get_dc_scaler(pMB->quant, i < 4);
130 :    
131 :     /* Quantize the block */
132 :     start_timer();
133 : chl 995 if (!(pParam->vol_flags & XVID_VOL_MPEGQUANT)) {
134 : edgomez 965 quant_intra(&data[i * 64], &qcoeff[i * 64], pMB->quant, iDcScaler);
135 : chl 995 } else {
136 : edgomez 965 quant4_intra(&data[i * 64], &qcoeff[i * 64], pMB->quant, iDcScaler);
137 : chl 995 }
138 : edgomez 965 stop_quant_timer();
139 :     }
140 :     }
141 :    
142 :     /* DeQuantize all blocks -- Intra mode */
143 :     static __inline void
144 :     MBDeQuantIntra(const MBParam * pParam,
145 :     const int iQuant,
146 :     int16_t qcoeff[6 * 64],
147 :     int16_t data[6*64])
148 :     {
149 :     int i;
150 :    
151 :     for (i = 0; i < 6; i++) {
152 : Isibaar 3 uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4);
153 :    
154 :     start_timer();
155 : edgomez 965 if (!(pParam->vol_flags & XVID_VOL_MPEGQUANT))
156 :     dequant_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler);
157 :     else
158 :     dequant4_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler);
159 :     stop_iquant_timer();
160 :     }
161 :     }
162 : Isibaar 3
163 : chl 1011
164 :     static int
165 : edgomez 1053 dct_quantize_trellis_h263_c(int16_t *const Out,
166 :     const int16_t *const In,
167 :     int Q,
168 :     const uint16_t * const Zigzag,
169 :     int Non_Zero);
170 : chl 1011
171 : edgomez 1053 #if 0
172 : chl 1011 static int
173 : edgomez 1053 dct_quantize_trellis_mpeg_c(int16_t *const Out,
174 :     const int16_t *const In,
175 :     int Q,
176 :     const uint16_t * const Zigzag,
177 :     int Non_Zero);
178 :     #endif
179 : chl 1011
180 : edgomez 965 /* Quantize all blocks -- Inter mode */
181 :     static __inline uint8_t
182 :     MBQuantInter(const MBParam * pParam,
183 : chl 995 const FRAMEINFO * const frame,
184 : edgomez 965 const MACROBLOCK * pMB,
185 :     int16_t data[6 * 64],
186 :     int16_t qcoeff[6 * 64],
187 :     int bvop,
188 :     int limit)
189 :     {
190 :    
191 :     int i;
192 :     uint8_t cbp = 0;
193 :     int sum;
194 :     int code_block;
195 :    
196 :     for (i = 0; i < 6; i++) {
197 : syskin 984
198 : edgomez 965 /* Quantize the block */
199 :     start_timer();
200 : chl 995 if (!(pParam->vol_flags & XVID_VOL_MPEGQUANT)) {
201 :     sum = quant_inter(&qcoeff[i*64], &data[i*64], pMB->quant);
202 :     if ( (sum) && (frame->vop_flags & XVID_VOP_TRELLISQUANT) ) {
203 : chl 1011 sum = dct_quantize_trellis_h263_c(&qcoeff[i*64], &data[i*64], pMB->quant, &scan_tables[0][0], 63)+1;
204 : Isibaar 1095 /* limit = 1; // Isibaar: why? deactivated so far - so please complain! ;-) */
205 : chl 995 }
206 :     } else {
207 : edgomez 965 sum = quant4_inter(&qcoeff[i * 64], &data[i * 64], pMB->quant);
208 : edgomez 1053 #if 0
209 :     if ( (sum) && (frame->vop_flags & XVID_VOP_TRELLISQUANT) )
210 :     sum = dct_quantize_trellis_mpeg_c (&qcoeff[i*64], &data[i*64], pMB->quant)+1;
211 :     #endif
212 : chl 995 }
213 : edgomez 965 stop_quant_timer();
214 :    
215 :     /*
216 :     * We code the block if the sum is higher than the limit and if the first
217 :     * two AC coefficients in zig zag order are not zero.
218 :     */
219 :     code_block = 0;
220 :     if ((sum >= limit) || (qcoeff[i*64+1] != 0) || (qcoeff[i*64+8] != 0)) {
221 :     code_block = 1;
222 : edgomez 195 } else {
223 : Isibaar 3
224 : edgomez 965 if (bvop && (pMB->mode == MODE_DIRECT || pMB->mode == MODE_DIRECT_NO4V)) {
225 :     /* dark blocks prevention for direct mode */
226 :     if ((qcoeff[i*64] < -1) || (qcoeff[i*64] > 0))
227 :     code_block = 1;
228 : edgomez 851 } else {
229 : edgomez 965 /* not direct mode */
230 :     if (qcoeff[i*64] != 0)
231 :     code_block = 1;
232 : edgomez 851 }
233 : Isibaar 3 }
234 :    
235 : edgomez 965 /* Set the corresponding cbp bit */
236 :     cbp |= code_block << (5 - i);
237 :     }
238 : edgomez 851
239 : edgomez 965 return(cbp);
240 :     }
241 : Isibaar 3
242 : edgomez 965 /* DeQuantize all blocks -- Inter mode */
243 : syskin 984 static __inline void
244 : edgomez 965 MBDeQuantInter(const MBParam * pParam,
245 :     const int iQuant,
246 :     int16_t data[6 * 64],
247 :     int16_t qcoeff[6 * 64],
248 :     const uint8_t cbp)
249 :     {
250 :     int i;
251 :    
252 :     for (i = 0; i < 6; i++) {
253 : syskin 984 if (cbp & (1 << (5 - i))) {
254 : edgomez 965 start_timer();
255 :     if (!(pParam->vol_flags & XVID_VOL_MPEGQUANT))
256 :     dequant_inter(&data[i * 64], &qcoeff[i * 64], iQuant);
257 :     else
258 :     dequant4_inter(&data[i * 64], &qcoeff[i * 64], iQuant);
259 :     stop_iquant_timer();
260 : edgomez 851 }
261 : h 69 }
262 : Isibaar 3 }
263 :    
264 : edgomez 965 typedef void (transfer_operation_8to16_t) (int16_t *Dst, const uint8_t *Src, int BpS);
265 :     typedef void (transfer_operation_16to8_t) (uint8_t *Dst, const int16_t *Src, int BpS);
266 : Isibaar 3
267 : edgomez 78
268 : edgomez 965 static __inline void
269 : syskin 984 MBTrans8to16(const MBParam * const pParam,
270 :     const FRAMEINFO * const frame,
271 :     const MACROBLOCK * const pMB,
272 : edgomez 965 const uint32_t x_pos,
273 :     const uint32_t y_pos,
274 :     int16_t data[6 * 64])
275 :     {
276 : h 82 uint32_t stride = pParam->edged_width;
277 :     uint32_t stride2 = stride / 2;
278 : edgomez 965 uint32_t next_block = stride * 8;
279 : syskin 984 int32_t cst;
280 : Isibaar 3 uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
281 : syskin 984 const IMAGE * const pCurrent = &frame->image;
282 : edgomez 965 transfer_operation_8to16_t *transfer_op = NULL;
283 : edgomez 195
284 : edgomez 965 if ((frame->vop_flags & XVID_VOP_REDUCED)) {
285 :    
286 :     /* Image pointers */
287 :     pY_Cur = pCurrent->y + (y_pos << 5) * stride + (x_pos << 5);
288 : edgomez 851 pU_Cur = pCurrent->u + (y_pos << 4) * stride2 + (x_pos << 4);
289 :     pV_Cur = pCurrent->v + (y_pos << 4) * stride2 + (x_pos << 4);
290 : edgomez 965
291 :     /* Block size */
292 :     cst = 16;
293 :    
294 :     /* Operation function */
295 :     transfer_op = (transfer_operation_8to16_t*)filter_18x18_to_8x8;
296 :     } else {
297 :    
298 :     /* Image pointers */
299 :     pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4);
300 : edgomez 851 pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3);
301 :     pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3);
302 : edgomez 965
303 :     /* Block size */
304 :     cst = 8;
305 :    
306 :     /* Operation function */
307 :     transfer_op = (transfer_operation_8to16_t*)transfer_8to16copy;
308 : edgomez 851 }
309 : Isibaar 3
310 : edgomez 965 /* Do the transfer */
311 : h 69 start_timer();
312 : edgomez 965 transfer_op(&data[0 * 64], pY_Cur, stride);
313 :     transfer_op(&data[1 * 64], pY_Cur + cst, stride);
314 :     transfer_op(&data[2 * 64], pY_Cur + next_block, stride);
315 :     transfer_op(&data[3 * 64], pY_Cur + next_block + cst, stride);
316 :     transfer_op(&data[4 * 64], pU_Cur, stride2);
317 :     transfer_op(&data[5 * 64], pV_Cur, stride2);
318 :     stop_transfer_timer();
319 : syskin 984 }
320 : edgomez 965
321 :     static __inline void
322 : syskin 984 MBTrans16to8(const MBParam * const pParam,
323 :     const FRAMEINFO * const frame,
324 :     const MACROBLOCK * const pMB,
325 : edgomez 965 const uint32_t x_pos,
326 :     const uint32_t y_pos,
327 :     int16_t data[6 * 64],
328 :     const uint32_t add,
329 :     const uint8_t cbp)
330 :     {
331 :     uint8_t *pY_Cur, *pU_Cur, *pV_Cur;
332 :     uint32_t stride = pParam->edged_width;
333 :     uint32_t stride2 = stride / 2;
334 :     uint32_t next_block = stride * 8;
335 : syskin 984 uint32_t cst;
336 :     const IMAGE * const pCurrent = &frame->image;
337 : edgomez 965 transfer_operation_16to8_t *transfer_op = NULL;
338 :    
339 :     if (pMB->field_dct) {
340 :     next_block = stride;
341 :     stride *= 2;
342 : h 69 }
343 :    
344 : edgomez 965 if ((frame->vop_flags & XVID_VOP_REDUCED)) {
345 : edgomez 851
346 : edgomez 965 /* Image pointers */
347 :     pY_Cur = pCurrent->y + (y_pos << 5) * stride + (x_pos << 5);
348 :     pU_Cur = pCurrent->u + (y_pos << 4) * stride2 + (x_pos << 4);
349 :     pV_Cur = pCurrent->v + (y_pos << 4) * stride2 + (x_pos << 4);
350 : Isibaar 3
351 : edgomez 965 /* Block size */
352 :     cst = 16;
353 : Isibaar 3
354 : edgomez 965 /* Operation function */
355 :     if(add)
356 :     transfer_op = (transfer_operation_16to8_t*)add_upsampled_8x8_16to8;
357 :     else
358 :     transfer_op = (transfer_operation_16to8_t*)copy_upsampled_8x8_16to8;
359 :     } else {
360 : Isibaar 3
361 : edgomez 965 /* Image pointers */
362 :     pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4);
363 :     pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3);
364 :     pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3);
365 : Isibaar 3
366 : edgomez 965 /* Block size */
367 :     cst = 8;
368 : Isibaar 3
369 : edgomez 965 /* Operation function */
370 :     if(add)
371 :     transfer_op = (transfer_operation_16to8_t*)transfer_16to8add;
372 :     else
373 :     transfer_op = (transfer_operation_16to8_t*)transfer_16to8copy;
374 : Isibaar 3 }
375 : h 69
376 : edgomez 965 /* Do the operation */
377 : h 69 start_timer();
378 : edgomez 965 if (cbp&32) transfer_op(pY_Cur, &data[0 * 64], stride);
379 :     if (cbp&16) transfer_op(pY_Cur + cst, &data[1 * 64], stride);
380 :     if (cbp& 8) transfer_op(pY_Cur + next_block, &data[2 * 64], stride);
381 :     if (cbp& 4) transfer_op(pY_Cur + next_block + cst, &data[3 * 64], stride);
382 :     if (cbp& 2) transfer_op(pU_Cur, &data[4 * 64], stride2);
383 :     if (cbp& 1) transfer_op(pV_Cur, &data[5 * 64], stride2);
384 : h 69 stop_transfer_timer();
385 : Isibaar 3 }
386 : h 69
387 : edgomez 965 /*****************************************************************************
388 :     * Module functions
389 :     ****************************************************************************/
390 :    
391 : syskin 984 void
392 :     MBTransQuantIntra(const MBParam * const pParam,
393 :     const FRAMEINFO * const frame,
394 :     MACROBLOCK * const pMB,
395 : chl 368 const uint32_t x_pos,
396 :     const uint32_t y_pos,
397 :     int16_t data[6 * 64],
398 :     int16_t qcoeff[6 * 64])
399 :     {
400 : h 69
401 : edgomez 965 /* Transfer data */
402 :     MBTrans8to16(pParam, frame, pMB, x_pos, y_pos, data);
403 : chl 368
404 : edgomez 965 /* Perform DCT (and field decision) */
405 :     MBfDCT(pParam, frame, pMB, x_pos, y_pos, data);
406 : chl 368
407 : edgomez 965 /* Quantize the block */
408 : chl 995 MBQuantIntra(pParam, frame, pMB, data, qcoeff);
409 : edgomez 965
410 :     /* DeQuantize the block */
411 :     MBDeQuantIntra(pParam, pMB->quant, data, qcoeff);
412 :    
413 :     /* Perform inverse DCT*/
414 :     MBiDCT(data, 0x3F);
415 :    
416 :     /* Transfer back the data -- Don't add data */
417 :     MBTrans16to8(pParam, frame, pMB, x_pos, y_pos, data, 0, 0x3F);
418 : chl 368 }
419 :    
420 : edgomez 965
421 : chl 368 uint8_t
422 : syskin 984 MBTransQuantInter(const MBParam * const pParam,
423 :     const FRAMEINFO * const frame,
424 :     MACROBLOCK * const pMB,
425 : edgomez 914 const uint32_t x_pos,
426 :     const uint32_t y_pos,
427 : chl 368 int16_t data[6 * 64],
428 :     int16_t qcoeff[6 * 64])
429 :     {
430 :     uint8_t cbp;
431 : edgomez 965 uint32_t limit;
432 : chl 368
433 : edgomez 914 /*
434 : edgomez 965 * There is no MBTrans8to16 for Inter block, that's done in motion compensation
435 :     * already
436 : edgomez 914 */
437 : chl 368
438 : edgomez 965 /* Perform DCT (and field decision) */
439 :     MBfDCT(pParam, frame, pMB, x_pos, y_pos, data);
440 : edgomez 914
441 : edgomez 965 /* Set the limit threshold */
442 :     limit = PVOP_TOOSMALL_LIMIT + ((pMB->quant == 1)? 1 : 0);
443 : chl 368
444 : Isibaar 1095 if (frame->vop_flags & XVID_VOP_CARTOON)
445 :     limit *= 3;
446 :    
447 : edgomez 965 /* Quantize the block */
448 : chl 995 cbp = MBQuantInter(pParam, frame, pMB, data, qcoeff, 0, limit);
449 : chl 368
450 : edgomez 965 /* DeQuantize the block */
451 :     MBDeQuantInter(pParam, pMB->quant, data, qcoeff, cbp);
452 : chl 368
453 : edgomez 965 /* Perform inverse DCT*/
454 :     MBiDCT(data, cbp);
455 : chl 368
456 : edgomez 965 /* Transfer back the data -- Add the data */
457 :     MBTrans16to8(pParam, frame, pMB, x_pos, y_pos, data, 1, cbp);
458 : syskin 984
459 : edgomez 965 return(cbp);
460 : chl 368 }
461 :    
462 : edgomez 965 uint8_t
463 :     MBTransQuantInterBVOP(const MBParam * pParam,
464 : chl 368 FRAMEINFO * frame,
465 :     MACROBLOCK * pMB,
466 :     const uint32_t x_pos,
467 :     const uint32_t y_pos,
468 :     int16_t data[6 * 64],
469 : edgomez 965 int16_t qcoeff[6 * 64])
470 : chl 368 {
471 : edgomez 965 uint8_t cbp;
472 :     uint32_t limit;
473 : syskin 984
474 : edgomez 965 /*
475 :     * There is no MBTrans8to16 for Inter block, that's done in motion compensation
476 :     * already
477 :     */
478 : chl 368
479 : edgomez 965 /* Perform DCT (and field decision) */
480 :     MBfDCT(pParam, frame, pMB, x_pos, y_pos, data);
481 : chl 368
482 : edgomez 965 /* Set the limit threshold */
483 :     limit = BVOP_TOOSMALL_LIMIT;
484 : chl 368
485 : Isibaar 1095 if (frame->vop_flags & XVID_VOP_CARTOON)
486 :     limit *= 2;
487 :    
488 : edgomez 965 /* Quantize the block */
489 : chl 995 cbp = MBQuantInter(pParam, frame, pMB, data, qcoeff, 1, limit);
490 : chl 368
491 : edgomez 965 /*
492 :     * History comment:
493 :     * We don't have to DeQuant, iDCT and Transfer back data for B-frames.
494 :     *
495 :     * BUT some plugins require the original frame to be passed so we have
496 :     * to take care of that here
497 :     */
498 :     if((pParam->plugin_flags & XVID_REQORIGINAL)) {
499 : chl 368
500 : edgomez 965 /* DeQuantize the block */
501 :     MBDeQuantInter(pParam, pMB->quant, data, qcoeff, cbp);
502 : chl 368
503 : edgomez 965 /* Perform inverse DCT*/
504 :     MBiDCT(data, cbp);
505 : h 69
506 : edgomez 965 /* Transfer back the data -- Add the data */
507 :     MBTrans16to8(pParam, frame, pMB, x_pos, y_pos, data, 1, cbp);
508 : edgomez 851 }
509 :    
510 : edgomez 965 return(cbp);
511 : edgomez 851 }
512 :    
513 :     /* if sum(diff between field lines) < sum(diff between frame lines), use field dct */
514 :     uint32_t
515 :     MBFieldTest_c(int16_t data[6 * 64])
516 :     {
517 : edgomez 195 const uint8_t blocks[] =
518 :     { 0 * 64, 0 * 64, 0 * 64, 0 * 64, 2 * 64, 2 * 64, 2 * 64, 2 * 64 };
519 :     const uint8_t lines[] = { 0, 16, 32, 48, 0, 16, 32, 48 };
520 : edgomez 78
521 : h 69 int frame = 0, field = 0;
522 :     int i, j;
523 :    
524 : edgomez 195 for (i = 0; i < 7; ++i) {
525 :     for (j = 0; j < 8; ++j) {
526 :     frame +=
527 : edgomez 982 abs(data[0 * 64 + (i + 1) * 8 + j] - data[0 * 64 + i * 8 + j]);
528 : edgomez 195 frame +=
529 : edgomez 982 abs(data[1 * 64 + (i + 1) * 8 + j] - data[1 * 64 + i * 8 + j]);
530 : edgomez 195 frame +=
531 : edgomez 982 abs(data[2 * 64 + (i + 1) * 8 + j] - data[2 * 64 + i * 8 + j]);
532 : edgomez 195 frame +=
533 : edgomez 982 abs(data[3 * 64 + (i + 1) * 8 + j] - data[3 * 64 + i * 8 + j]);
534 : h 69
535 : edgomez 195 field +=
536 : edgomez 982 abs(data[blocks[i + 1] + lines[i + 1] + j] -
537 : edgomez 195 data[blocks[i] + lines[i] + j]);
538 :     field +=
539 : edgomez 982 abs(data[blocks[i + 1] + lines[i + 1] + 8 + j] -
540 : edgomez 195 data[blocks[i] + lines[i] + 8 + j]);
541 :     field +=
542 : edgomez 982 abs(data[blocks[i + 1] + 64 + lines[i + 1] + j] -
543 : edgomez 195 data[blocks[i] + 64 + lines[i] + j]);
544 :     field +=
545 : edgomez 982 abs(data[blocks[i + 1] + 64 + lines[i + 1] + 8 + j] -
546 : edgomez 195 data[blocks[i] + 64 + lines[i] + 8 + j]);
547 : h 69 }
548 :     }
549 :    
550 : edgomez 851 return (frame >= (field + 350));
551 : h 69 }
552 :    
553 :    
554 :     /* deinterlace Y blocks vertically */
555 :    
556 :     #define MOVLINE(X,Y) memcpy(X, Y, sizeof(tmp))
557 : syskin 984 #define LINE(X,Y) &data[X*64 + Y*8]
558 : h 69
559 : edgomez 195 void
560 :     MBFrameToField(int16_t data[6 * 64])
561 : h 69 {
562 :     int16_t tmp[8];
563 :    
564 :     /* left blocks */
565 :    
566 : edgomez 1053 /* 1=2, 2=4, 4=8, 8=1 */
567 : edgomez 195 MOVLINE(tmp, LINE(0, 1));
568 :     MOVLINE(LINE(0, 1), LINE(0, 2));
569 :     MOVLINE(LINE(0, 2), LINE(0, 4));
570 :     MOVLINE(LINE(0, 4), LINE(2, 0));
571 :     MOVLINE(LINE(2, 0), tmp);
572 : h 69
573 : edgomez 1053 /* 3=6, 6=12, 12=9, 9=3 */
574 : edgomez 195 MOVLINE(tmp, LINE(0, 3));
575 :     MOVLINE(LINE(0, 3), LINE(0, 6));
576 :     MOVLINE(LINE(0, 6), LINE(2, 4));
577 :     MOVLINE(LINE(2, 4), LINE(2, 1));
578 :     MOVLINE(LINE(2, 1), tmp);
579 : h 69
580 : edgomez 1053 /* 5=10, 10=5 */
581 : edgomez 195 MOVLINE(tmp, LINE(0, 5));
582 :     MOVLINE(LINE(0, 5), LINE(2, 2));
583 :     MOVLINE(LINE(2, 2), tmp);
584 : h 69
585 : edgomez 1053 /* 7=14, 14=13, 13=11, 11=7 */
586 : edgomez 195 MOVLINE(tmp, LINE(0, 7));
587 :     MOVLINE(LINE(0, 7), LINE(2, 6));
588 :     MOVLINE(LINE(2, 6), LINE(2, 5));
589 :     MOVLINE(LINE(2, 5), LINE(2, 3));
590 :     MOVLINE(LINE(2, 3), tmp);
591 : h 69
592 :     /* right blocks */
593 :    
594 : edgomez 1053 /* 1=2, 2=4, 4=8, 8=1 */
595 : edgomez 195 MOVLINE(tmp, LINE(1, 1));
596 :     MOVLINE(LINE(1, 1), LINE(1, 2));
597 :     MOVLINE(LINE(1, 2), LINE(1, 4));
598 :     MOVLINE(LINE(1, 4), LINE(3, 0));
599 :     MOVLINE(LINE(3, 0), tmp);
600 : h 69
601 : edgomez 1053 /* 3=6, 6=12, 12=9, 9=3 */
602 : edgomez 195 MOVLINE(tmp, LINE(1, 3));
603 :     MOVLINE(LINE(1, 3), LINE(1, 6));
604 :     MOVLINE(LINE(1, 6), LINE(3, 4));
605 :     MOVLINE(LINE(3, 4), LINE(3, 1));
606 :     MOVLINE(LINE(3, 1), tmp);
607 : h 69
608 : edgomez 1053 /* 5=10, 10=5 */
609 : edgomez 195 MOVLINE(tmp, LINE(1, 5));
610 :     MOVLINE(LINE(1, 5), LINE(3, 2));
611 :     MOVLINE(LINE(3, 2), tmp);
612 : h 69
613 : edgomez 1053 /* 7=14, 14=13, 13=11, 11=7 */
614 : edgomez 195 MOVLINE(tmp, LINE(1, 7));
615 :     MOVLINE(LINE(1, 7), LINE(3, 6));
616 :     MOVLINE(LINE(3, 6), LINE(3, 5));
617 :     MOVLINE(LINE(3, 5), LINE(3, 3));
618 :     MOVLINE(LINE(3, 3), tmp);
619 : h 69 }
620 : chl 1011
621 :    
622 :    
623 :    
624 :    
625 : edgomez 1053 /*****************************************************************************
626 :     * Trellis based R-D optimal quantization
627 :     *
628 :     * Trellis Quant code (C) 2003 Pascal Massimino skal(at)planet-d.net
629 :     *
630 :     ****************************************************************************/
631 : chl 1011
632 :    
633 : edgomez 1053 #if 0
634 : chl 1012 static int
635 : edgomez 1053 dct_quantize_trellis_mpeg_c(int16_t *const Out,
636 :     const int16_t *const In,
637 :     int Q,
638 :     const uint16_t * const Zigzag,
639 :     int Non_Zero)
640 :     {
641 :     return 63;
642 :     }
643 :     #endif
644 : chl 1011
645 : edgomez 1053 /*----------------------------------------------------------------------------
646 :     *
647 :     * Trellis-Based quantization
648 :     *
649 :     * So far I understand this paper:
650 :     *
651 :     * "Trellis-Based R-D Optimal Quantization in H.263+"
652 :     * J.Wen, M.Luttrell, J.Villasenor
653 :     * IEEE Transactions on Image Processing, Vol.9, No.8, Aug. 2000.
654 :     *
655 :     * we are at stake with a simplified Bellmand-Ford / Dijkstra Single
656 :     * Source Shorted Path algo. But due to the underlying graph structure
657 :     * ("Trellis"), it can be turned into a dynamic programming algo,
658 :     * partially saving the explicit graph's nodes representation. And
659 :     * without using a heap, since the open frontier of the DAG is always
660 :     * known, and of fixed sized.
661 :     *--------------------------------------------------------------------------*/
662 : chl 1011
663 :    
664 :    
665 : edgomez 1053 /* Codes lengths for relevant levels. */
666 : chl 1011
667 : edgomez 1053 /* let's factorize: */
668 : chl 1011 static const uint8_t Code_Len0[64] = {
669 :     30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,
670 :     30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 };
671 :     static const uint8_t Code_Len1[64] = {
672 :     20,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,
673 :     30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 };
674 :     static const uint8_t Code_Len2[64] = {
675 :     19,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,
676 :     30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 };
677 :     static const uint8_t Code_Len3[64] = {
678 :     18,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,
679 :     30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 };
680 :     static const uint8_t Code_Len4[64] = {
681 :     17,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,
682 :     30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 };
683 :     static const uint8_t Code_Len5[64] = {
684 :     16,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,
685 :     30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 };
686 :     static const uint8_t Code_Len6[64] = {
687 :     15,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,
688 :     30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 };
689 :     static const uint8_t Code_Len7[64] = {
690 :     13,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,
691 :     30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 };
692 :     static const uint8_t Code_Len8[64] = {
693 :     11,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,
694 :     30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 };
695 :     static const uint8_t Code_Len9[64] = {
696 :     12,21,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,
697 :     30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 };
698 :     static const uint8_t Code_Len10[64] = {
699 :     12,20,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,
700 :     30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 };
701 :     static const uint8_t Code_Len11[64] = {
702 :     12,19,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,
703 :     30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 };
704 :     static const uint8_t Code_Len12[64] = {
705 :     11,17,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,
706 :     30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 };
707 :     static const uint8_t Code_Len13[64] = {
708 :     11,15,21,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,
709 :     30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 };
710 :     static const uint8_t Code_Len14[64] = {
711 :     10,12,19,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,
712 :     30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 };
713 :     static const uint8_t Code_Len15[64] = {
714 :     10,13,17,19,21,21,21,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,
715 :     30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 };
716 :     static const uint8_t Code_Len16[64] = {
717 :     9,12,13,18,18,19,19,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,
718 :     30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30};
719 :     static const uint8_t Code_Len17[64] = {
720 :     8,11,13,14,14,14,15,19,19,19,21,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,
721 :     30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 };
722 :     static const uint8_t Code_Len18[64] = {
723 :     7, 9,11,11,13,13,13,15,15,15,16,22,22,22,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,
724 :     30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 };
725 :     static const uint8_t Code_Len19[64] = {
726 :     5, 7, 9,10,10,11,11,11,11,11,13,14,16,17,17,18,18,18,18,18,18,18,18,20,20,21,21,30,30,30,30,30,
727 :     30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 };
728 :     static const uint8_t Code_Len20[64] = {
729 :     3, 4, 5, 6, 6, 6, 7, 7, 7, 7, 8, 8, 8, 9, 9,10,10,10,10,10,10,10,10,12,12,13,13,12,13,14,15,15,
730 :     15,16,16,16,16,17,17,17,18,18,19,19,19,19,19,19,19,19,21,21,22,22,30,30,30,30,30,30,30,30,30,30 };
731 :    
732 : edgomez 1053 /* a few more table for LAST table: */
733 : chl 1011 static const uint8_t Code_Len21[64] = {
734 :     13,20,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,
735 :     30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30};
736 :     static const uint8_t Code_Len22[64] = {
737 :     12,15,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,
738 :     30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30};
739 :     static const uint8_t Code_Len23[64] = {
740 :     10,12,15,15,15,16,16,16,16,17,17,17,17,17,17,17,17,18,18,18,18,18,18,18,18,19,19,19,19,20,20,20,
741 :     20,21,21,21,21,21,21,21,21,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30};
742 :     static const uint8_t Code_Len24[64] = {
743 :     5, 7, 7, 7, 7, 8, 8, 8, 8, 9, 9, 9, 9, 9, 9, 9, 9,10,10,10,10,10,10,10,10,11,11,11,11,12,12,12,
744 :     12,13,13,13,13,13,13,13,13,14,16,16,16,16,17,17,17,17,18,18,18,18,18,18,18,18,19,19,19,19,19,19};
745 :    
746 :    
747 : edgomez 1053 static const uint8_t * const B16_17_Code_Len[24] = { /* levels [1..24] */
748 : chl 1011 Code_Len20,Code_Len19,Code_Len18,Code_Len17,
749 :     Code_Len16,Code_Len15,Code_Len14,Code_Len13,
750 :     Code_Len12,Code_Len11,Code_Len10,Code_Len9,
751 :     Code_Len8, Code_Len7 ,Code_Len6 ,Code_Len5,
752 :     Code_Len4, Code_Len3, Code_Len3 ,Code_Len2,
753 :     Code_Len2, Code_Len1, Code_Len1, Code_Len1,
754 :     };
755 :    
756 : edgomez 1053 static const uint8_t * const B16_17_Code_Len_Last[6] = { /* levels [1..6] */
757 : chl 1011 Code_Len24,Code_Len23,Code_Len22,Code_Len21, Code_Len3, Code_Len1,
758 :     };
759 :    
760 :     #define TL(q) 0xfe00/(q*q)
761 :    
762 :     static const int Trellis_Lambda_Tabs[31] = {
763 :     TL( 1),TL( 2),TL( 3),TL( 4),TL( 5),TL( 6), TL( 7),
764 :     TL( 8),TL( 9),TL(10),TL(11),TL(12),TL(13),TL(14), TL(15),
765 :     TL(16),TL(17),TL(18),TL(19),TL(20),TL(21),TL(22), TL(23),
766 :     TL(24),TL(25),TL(26),TL(27),TL(28),TL(29),TL(30), TL(31)
767 :     };
768 :     #undef TL
769 :    
770 : suxen_drol 1014 static __inline int Find_Last(const int16_t *C, const uint16_t *Zigzag, int i)
771 : chl 1011 {
772 :     while(i>=0)
773 :     if (C[Zigzag[i]])
774 :     return i;
775 :     else i--;
776 :     return -1;
777 :     }
778 :    
779 : edgomez 1053 /* this routine has been strippen of all debug code */
780 : chl 1011
781 : chl 1012 static int
782 :     dct_quantize_trellis_h263_c(int16_t *const Out, const int16_t *const In, int Q, const uint16_t * const Zigzag, int Non_Zero)
783 :     {
784 :    
785 : edgomez 1053 /*
786 :     * Note: We should search last non-zero coeffs on *real* DCT input coeffs (In[]),
787 :     * not quantized one (Out[]). However, it only improves the result *very*
788 :     * slightly (~0.01dB), whereas speed drops to crawling level :)
789 :     * Well, actually, taking 1 more coeff past Non_Zero into account sometimes helps.
790 :     */
791 : chl 1012 typedef struct { int16_t Run, Level; } NODE;
792 :    
793 :     NODE Nodes[65], Last;
794 :     uint32_t Run_Costs0[64+1];
795 :     uint32_t * const Run_Costs = Run_Costs0 + 1;
796 :     const int Mult = 2*Q;
797 :     const int Bias = (Q-1) | 1;
798 :     const int Lev0 = Mult + Bias;
799 : edgomez 1053 const int Lambda = Trellis_Lambda_Tabs[Q-1]; /* it's 1/lambda, actually */
800 : chl 1012
801 :     int Run_Start = -1;
802 :     uint32_t Min_Cost = 2<<16;
803 :    
804 :     int Last_Node = -1;
805 :     uint32_t Last_Cost = 0;
806 :    
807 :     int i, j;
808 : edgomez 1053 Run_Costs[-1] = 2<<16; /* source (w/ CBP penalty) */
809 : chl 1012
810 :     Non_Zero = Find_Last(Out, Zigzag, Non_Zero);
811 :     if (Non_Zero<0)
812 :     return -1;
813 :    
814 :     for(i=0; i<=Non_Zero; i++)
815 :     {
816 :     const int AC = In[Zigzag[i]];
817 :     const int Level1 = Out[Zigzag[i]];
818 :     const int Dist0 = Lambda* AC*AC;
819 :     uint32_t Best_Cost = 0xf0000000;
820 :     Last_Cost += Dist0;
821 :    
822 : edgomez 1053 if ((uint32_t)(Level1+1)<3) /* very specialized loop for -1,0,+1 */
823 : chl 1012 {
824 :     int dQ;
825 :     int Run;
826 :     uint32_t Cost0;
827 :    
828 :     if (AC<0) {
829 :     Nodes[i].Level = -1;
830 :     dQ = Lev0 + AC;
831 :     } else {
832 :     Nodes[i].Level = 1;
833 :     dQ = Lev0 - AC;
834 :     }
835 :     Cost0 = Lambda*dQ*dQ;
836 :    
837 :     Nodes[i].Run = 1;
838 :     Best_Cost = (Code_Len20[0]<<16) + Run_Costs[i-1]+Cost0;
839 :     for(Run=i-Run_Start; Run>0; --Run)
840 :     {
841 :     const uint32_t Cost_Base = Cost0 + Run_Costs[i-Run];
842 :     const uint32_t Cost = Cost_Base + (Code_Len20[Run-1]<<16);
843 :     const uint32_t lCost = Cost_Base + (Code_Len24[Run-1]<<16);
844 :    
845 : edgomez 1053 /*
846 :     * TODO: what about tie-breaks? Should we favor short runs or
847 :     * long runs? Although the error is the same, it would not be
848 :     * spread the same way along high and low frequencies...
849 :     */
850 : chl 1012
851 : edgomez 1053 /* (I'd say: favour short runs => hifreq errors (HVS) -- gruel ) */
852 : chl 1012
853 :     if (Cost<Best_Cost) {
854 :     Best_Cost = Cost;
855 :     Nodes[i].Run = Run;
856 :     }
857 :    
858 :     if (lCost<Last_Cost) {
859 :     Last_Cost = lCost;
860 :     Last.Run = Run;
861 :     Last_Node = i;
862 :     }
863 :     }
864 :     if (Last_Node==i)
865 :     Last.Level = Nodes[i].Level;
866 :     }
867 : edgomez 1053 else /* "big" levels */
868 : chl 1012 {
869 :     const uint8_t *Tbl_L1, *Tbl_L2, *Tbl_L1_Last, *Tbl_L2_Last;
870 :     int Level2;
871 :     int dQ1, dQ2;
872 :     int Run;
873 :     uint32_t Dist1,Dist2;
874 :     int dDist21;
875 :    
876 :     if (Level1>1) {
877 :     dQ1 = Level1*Mult-AC + Bias;
878 :     dQ2 = dQ1 - Mult;
879 :     Level2 = Level1-1;
880 :     Tbl_L1 = (Level1<=24) ? B16_17_Code_Len[Level1-1] : Code_Len0;
881 :     Tbl_L2 = (Level2<=24) ? B16_17_Code_Len[Level2-1] : Code_Len0;
882 :     Tbl_L1_Last = (Level1<=6) ? B16_17_Code_Len_Last[Level1-1] : Code_Len0;
883 :     Tbl_L2_Last = (Level2<=6) ? B16_17_Code_Len_Last[Level2-1] : Code_Len0;
884 : edgomez 1053 } else { /* Level1<-1 */
885 : chl 1012 dQ1 = Level1*Mult-AC - Bias;
886 :     dQ2 = dQ1 + Mult;
887 :     Level2 = Level1 + 1;
888 :     Tbl_L1 = (Level1>=-24) ? B16_17_Code_Len[Level1^-1] : Code_Len0;
889 :     Tbl_L2 = (Level2>=-24) ? B16_17_Code_Len[Level2^-1] : Code_Len0;
890 :     Tbl_L1_Last = (Level1>=- 6) ? B16_17_Code_Len_Last[Level1^-1] : Code_Len0;
891 :     Tbl_L2_Last = (Level2>=- 6) ? B16_17_Code_Len_Last[Level2^-1] : Code_Len0;
892 :     }
893 :     Dist1 = Lambda*dQ1*dQ1;
894 :     Dist2 = Lambda*dQ2*dQ2;
895 :     dDist21 = Dist2-Dist1;
896 :    
897 :     for(Run=i-Run_Start; Run>0; --Run)
898 :     {
899 :     const uint32_t Cost_Base = Dist1 + Run_Costs[i-Run];
900 :     uint32_t Cost1, Cost2;
901 :     int bLevel;
902 :    
903 : edgomez 1053 /*
904 :     * for sub-optimal (but slightly worth it, speed-wise) search, uncomment the following:
905 :     * if (Cost_Base>=Best_Cost) continue;
906 :     * (? doesn't seem to have any effect -- gruel )
907 :     */
908 : chl 1012
909 :     Cost1 = Cost_Base + (Tbl_L1[Run-1]<<16);
910 :     Cost2 = Cost_Base + (Tbl_L2[Run-1]<<16) + dDist21;
911 :    
912 :     if (Cost2<Cost1) {
913 :     Cost1 = Cost2;
914 :     bLevel = Level2;
915 :     } else
916 :     bLevel = Level1;
917 :    
918 :     if (Cost1<Best_Cost) {
919 :     Best_Cost = Cost1;
920 :     Nodes[i].Run = Run;
921 :     Nodes[i].Level = bLevel;
922 :     }
923 :    
924 :     Cost1 = Cost_Base + (Tbl_L1_Last[Run-1]<<16);
925 :     Cost2 = Cost_Base + (Tbl_L2_Last[Run-1]<<16) + dDist21;
926 :    
927 :     if (Cost2<Cost1) {
928 :     Cost1 = Cost2;
929 :     bLevel = Level2;
930 :     } else
931 :     bLevel = Level1;
932 :    
933 :     if (Cost1<Last_Cost) {
934 :     Last_Cost = Cost1;
935 :     Last.Run = Run;
936 :     Last.Level = bLevel;
937 :     Last_Node = i;
938 :     }
939 : edgomez 1053 } /* end of "for Run" */
940 : chl 1012
941 :     }
942 :    
943 :     Run_Costs[i] = Best_Cost;
944 :    
945 :     if (Best_Cost < Min_Cost + Dist0) {
946 :     Min_Cost = Best_Cost;
947 :     Run_Start = i;
948 :     }
949 :     else
950 :     {
951 : edgomez 1053 /*
952 :     * as noticed by Michael Niedermayer (michaelni at gmx.at), there's
953 :     * a code shorter by 1 bit for a larger run (!), same level. We give
954 :     * it a chance by not moving the left barrier too much.
955 :     */
956 : chl 1012
957 :     while( Run_Costs[Run_Start]>Min_Cost+(1<<16) )
958 :     Run_Start++;
959 :    
960 : edgomez 1053 /* spread on preceding coeffs the cost incurred by skipping this one */
961 : chl 1012 for(j=Run_Start; j<i; ++j) Run_Costs[j] += Dist0;
962 :     Min_Cost += Dist0;
963 :     }
964 :     }
965 :    
966 :     if (Last_Node<0)
967 :     return -1;
968 :    
969 : edgomez 1053 /* reconstruct optimal sequence backward with surviving paths */
970 : chl 1012 memset(Out, 0x00, 64*sizeof(*Out));
971 :     Out[Zigzag[Last_Node]] = Last.Level;
972 :     i = Last_Node - Last.Run;
973 :     while(i>=0) {
974 :     Out[Zigzag[i]] = Nodes[i].Level;
975 :     i -= Nodes[i].Run;
976 :     }
977 :     return Last_Node;
978 :     }
979 :    
980 :    
981 :    
982 :    
983 :    
984 :    
985 :    
986 :    
987 :    
988 :    
989 :    
990 : edgomez 1053 /* original version including heavy debugging info */
991 : chl 1012
992 :     #ifdef DBGTRELL
993 :    
994 : chl 1011 #define DBG 0
995 :    
996 : suxen_drol 1014 static __inline uint32_t Evaluate_Cost(const int16_t *C, int Mult, int Bias,
997 : chl 1011 const uint16_t * Zigzag, int Max, int Lambda)
998 :     {
999 :     #if (DBG>0)
1000 :     const int16_t * const Ref = C + 6*64;
1001 :     int Last = Max;
1002 :     int Bits = 0;
1003 : chl 1012 int Dist = 0;
1004 :     int i;
1005 :     uint32_t Cost;
1006 :    
1007 :     while(Last>=0 && C[Zigzag[Last]]==0)
1008 :     Last--;
1009 :    
1010 : chl 1011 if (Last>=0) {
1011 :     int j=0, j0=0;
1012 :     int Run, Level;
1013 : chl 1012
1014 : edgomez 1053 Bits = 2; /* CBP */
1015 : chl 1011 while(j<Last) {
1016 : chl 1012 while(!C[Zigzag[j]])
1017 :     j++;
1018 :     if (j==Last)
1019 :     break;
1020 : chl 1011 Level=C[Zigzag[j]];
1021 :     Run = j - j0;
1022 :     j0 = ++j;
1023 : chl 1012 if (Level>=-24 && Level<=24)
1024 :     Bits += B16_17_Code_Len[(Level<0) ? -Level-1 : Level-1][Run];
1025 :     else
1026 :     Bits += 30;
1027 : chl 1011 }
1028 :     Level = C[Zigzag[Last]];
1029 :     Run = j - j0;
1030 : chl 1012 if (Level>=-6 && Level<=6)
1031 :     Bits += B16_17_Code_Len_Last[(Level<0) ? -Level-1 : Level-1][Run];
1032 :     else
1033 :     Bits += 30;
1034 : chl 1011 }
1035 :    
1036 :     for(i=0; i<=Last; ++i) {
1037 :     int V = C[Zigzag[i]]*Mult;
1038 : chl 1012 if (V>0)
1039 :     V += Bias;
1040 :     else
1041 :     if (V<0)
1042 :     V -= Bias;
1043 : chl 1011 V -= Ref[Zigzag[i]];
1044 :     Dist += V*V;
1045 :     }
1046 : chl 1012 Cost = Lambda*Dist + (Bits<<16);
1047 : chl 1011 if (DBG==1)
1048 :     printf( " Last:%2d/%2d Cost = [(Bits=%5.0d) + Lambda*(Dist=%6.0d) = %d ] >>12= %d ", Last,Max, Bits, Dist, Cost, Cost>>12 );
1049 :     return Cost;
1050 :    
1051 :     #else
1052 :     return 0;
1053 :     #endif
1054 :     }
1055 :    
1056 :    
1057 :     static int
1058 :     dct_quantize_trellis_h263_c(int16_t *const Out, const int16_t *const In, int Q, const uint16_t * const Zigzag, int Non_Zero)
1059 :     {
1060 :    
1061 : edgomez 1053 /*
1062 :     * Note: We should search last non-zero coeffs on *real* DCT input coeffs (In[]),
1063 :     * not quantized one (Out[]). However, it only improves the result *very*
1064 :     * slightly (~0.01dB), whereas speed drops to crawling level :)
1065 :     * Well, actually, taking 1 more coeff past Non_Zero into account sometimes helps.
1066 :     */
1067 : chl 1011 typedef struct { int16_t Run, Level; } NODE;
1068 :    
1069 :     NODE Nodes[65], Last;
1070 : chl 1012 uint32_t Run_Costs0[64+1];
1071 :     uint32_t * const Run_Costs = Run_Costs0 + 1;
1072 : chl 1011 const int Mult = 2*Q;
1073 :     const int Bias = (Q-1) | 1;
1074 :     const int Lev0 = Mult + Bias;
1075 : edgomez 1053 const int Lambda = Trellis_Lambda_Tabs[Q-1]; /* it's 1/lambda, actually */
1076 : chl 1011
1077 :     int Run_Start = -1;
1078 : edgomez 1053 Run_Costs[-1] = 2<<16; /* source (w/ CBP penalty) */
1079 : chl 1011 uint32_t Min_Cost = 2<<16;
1080 :    
1081 :     int Last_Node = -1;
1082 :     uint32_t Last_Cost = 0;
1083 :    
1084 : chl 1012 int i, j;
1085 :    
1086 : chl 1011 #if (DBG>0)
1087 : edgomez 1053 Last.Level = 0; Last.Run = -1; /* just initialize to smthg */
1088 : chl 1011 #endif
1089 :    
1090 :     Non_Zero = Find_Last(Out, Zigzag, Non_Zero);
1091 :     if (Non_Zero<0)
1092 :     return -1;
1093 :    
1094 :     for(i=0; i<=Non_Zero; i++)
1095 :     {
1096 :     const int AC = In[Zigzag[i]];
1097 :     const int Level1 = Out[Zigzag[i]];
1098 :     const int Dist0 = Lambda* AC*AC;
1099 :     uint32_t Best_Cost = 0xf0000000;
1100 :     Last_Cost += Dist0;
1101 :    
1102 : edgomez 1053 if ((uint32_t)(Level1+1)<3) /* very specialized loop for -1,0,+1 */
1103 : chl 1011 {
1104 : chl 1012 int dQ;
1105 :     int Run;
1106 :     uint32_t Cost0;
1107 : chl 1011
1108 :     if (AC<0) {
1109 :     Nodes[i].Level = -1;
1110 :     dQ = Lev0 + AC;
1111 :     } else {
1112 :     Nodes[i].Level = 1;
1113 :     dQ = Lev0 - AC;
1114 :     }
1115 : chl 1012 Cost0 = Lambda*dQ*dQ;
1116 :    
1117 : chl 1011 Nodes[i].Run = 1;
1118 :     Best_Cost = (Code_Len20[0]<<16) + Run_Costs[i-1]+Cost0;
1119 :     for(Run=i-Run_Start; Run>0; --Run)
1120 :     {
1121 :     const uint32_t Cost_Base = Cost0 + Run_Costs[i-Run];
1122 :     const uint32_t Cost = Cost_Base + (Code_Len20[Run-1]<<16);
1123 : chl 1012 const uint32_t lCost = Cost_Base + (Code_Len24[Run-1]<<16);
1124 :    
1125 : edgomez 1053 /*
1126 :     * TODO: what about tie-breaks? Should we favor short runs or
1127 :     * long runs? Although the error is the same, it would not be
1128 :     * spread the same way along high and low frequencies...
1129 :     */
1130 : chl 1012 if (Cost<Best_Cost) {
1131 : chl 1011 Best_Cost = Cost;
1132 :     Nodes[i].Run = Run;
1133 :     }
1134 : chl 1012
1135 :     if (lCost<Last_Cost) {
1136 : chl 1011 Last_Cost = lCost;
1137 :     Last.Run = Run;
1138 :     Last_Node = i;
1139 :     }
1140 :     }
1141 : chl 1012 if (Last_Node==i)
1142 :     Last.Level = Nodes[i].Level;
1143 : chl 1011
1144 :     if (DBG==1) {
1145 :     Run_Costs[i] = Best_Cost;
1146 :     printf( "Costs #%2d: ", i);
1147 :     for(j=-1;j<=Non_Zero;++j) {
1148 :     if (j==Run_Start) printf( " %3.0d|", Run_Costs[j]>>12 );
1149 :     else if (j>Run_Start && j<i) printf( " %3.0d|", Run_Costs[j]>>12 );
1150 :     else if (j==i) printf( "(%3.0d)", Run_Costs[j]>>12 );
1151 :     else printf( " - |" );
1152 :     }
1153 :     printf( "<%3.0d %2d %d>", Min_Cost>>12, Nodes[i].Level, Nodes[i].Run );
1154 :     printf( " Last:#%2d {%3.0d %2d %d}", Last_Node, Last_Cost>>12, Last.Level, Last.Run );
1155 :     printf( " AC:%3.0d Dist0:%3d Dist(%d)=%d", AC, Dist0>>12, Nodes[i].Level, Cost0>>12 );
1156 :     printf( "\n" );
1157 :     }
1158 :     }
1159 : edgomez 1053 else /* "big" levels */
1160 : chl 1011 {
1161 :     const uint8_t *Tbl_L1, *Tbl_L2, *Tbl_L1_Last, *Tbl_L2_Last;
1162 :     int Level2;
1163 :     int dQ1, dQ2;
1164 :     int Run;
1165 : chl 1012 uint32_t Dist1,Dist2;
1166 :     int dDist21;
1167 :    
1168 : chl 1011 if (Level1>1) {
1169 :     dQ1 = Level1*Mult-AC + Bias;
1170 :     dQ2 = dQ1 - Mult;
1171 :     Level2 = Level1-1;
1172 :     Tbl_L1 = (Level1<=24) ? B16_17_Code_Len[Level1-1] : Code_Len0;
1173 :     Tbl_L2 = (Level2<=24) ? B16_17_Code_Len[Level2-1] : Code_Len0;
1174 :     Tbl_L1_Last = (Level1<=6) ? B16_17_Code_Len_Last[Level1-1] : Code_Len0;
1175 :     Tbl_L2_Last = (Level2<=6) ? B16_17_Code_Len_Last[Level2-1] : Code_Len0;
1176 : edgomez 1053 } else { /* Level1<-1 */
1177 : chl 1011 dQ1 = Level1*Mult-AC - Bias;
1178 :     dQ2 = dQ1 + Mult;
1179 :     Level2 = Level1 + 1;
1180 :     Tbl_L1 = (Level1>=-24) ? B16_17_Code_Len[Level1^-1] : Code_Len0;
1181 :     Tbl_L2 = (Level2>=-24) ? B16_17_Code_Len[Level2^-1] : Code_Len0;
1182 :     Tbl_L1_Last = (Level1>=- 6) ? B16_17_Code_Len_Last[Level1^-1] : Code_Len0;
1183 :     Tbl_L2_Last = (Level2>=- 6) ? B16_17_Code_Len_Last[Level2^-1] : Code_Len0;
1184 :     }
1185 : chl 1012 Dist1 = Lambda*dQ1*dQ1;
1186 :     Dist2 = Lambda*dQ2*dQ2;
1187 :     dDist21 = Dist2-Dist1;
1188 : chl 1011
1189 :     for(Run=i-Run_Start; Run>0; --Run)
1190 :     {
1191 :     const uint32_t Cost_Base = Dist1 + Run_Costs[i-Run];
1192 : chl 1012 uint32_t Cost1, Cost2;
1193 :     int bLevel;
1194 : chl 1011
1195 : edgomez 1053 /*
1196 :     * for sub-optimal (but slightly worth it, speed-wise) search, uncomment the following:
1197 :     * if (Cost_Base>=Best_Cost) continue;
1198 :     */
1199 : chl 1011 Cost1 = Cost_Base + (Tbl_L1[Run-1]<<16);
1200 :     Cost2 = Cost_Base + (Tbl_L2[Run-1]<<16) + dDist21;
1201 :    
1202 : chl 1012 if (Cost2<Cost1) {
1203 :     Cost1 = Cost2;
1204 :     bLevel = Level2;
1205 :     } else
1206 :     bLevel = Level1;
1207 : chl 1011
1208 : chl 1012 if (Cost1<Best_Cost) {
1209 : chl 1011 Best_Cost = Cost1;
1210 :     Nodes[i].Run = Run;
1211 :     Nodes[i].Level = bLevel;
1212 :     }
1213 :    
1214 :     Cost1 = Cost_Base + (Tbl_L1_Last[Run-1]<<16);
1215 :     Cost2 = Cost_Base + (Tbl_L2_Last[Run-1]<<16) + dDist21;
1216 :    
1217 : chl 1012 if (Cost2<Cost1) {
1218 :     Cost1 = Cost2;
1219 :     bLevel = Level2;
1220 :     } else
1221 :     bLevel = Level1;
1222 :    
1223 :     if (Cost1<Last_Cost) {
1224 : chl 1011 Last_Cost = Cost1;
1225 :     Last.Run = Run;
1226 :     Last.Level = bLevel;
1227 :     Last_Node = i;
1228 :     }
1229 : edgomez 1053 } /* end of "for Run" */
1230 : chl 1011
1231 :     if (DBG==1) {
1232 :     Run_Costs[i] = Best_Cost;
1233 :     printf( "Costs #%2d: ", i);
1234 :     for(j=-1;j<=Non_Zero;++j) {
1235 :     if (j==Run_Start) printf( " %3.0d|", Run_Costs[j]>>12 );
1236 :     else if (j>Run_Start && j<i) printf( " %3.0d|", Run_Costs[j]>>12 );
1237 :     else if (j==i) printf( "(%3.0d)", Run_Costs[j]>>12 );
1238 :     else printf( " - |" );
1239 :     }
1240 :     printf( "<%3.0d %2d %d>", Min_Cost>>12, Nodes[i].Level, Nodes[i].Run );
1241 :     printf( " Last:#%2d {%3.0d %2d %d}", Last_Node, Last_Cost>>12, Last.Level, Last.Run );
1242 :     printf( " AC:%3.0d Dist0:%3d Dist(%2d):%3d Dist(%2d):%3d", AC, Dist0>>12, Level1, Dist1>>12, Level2, Dist2>>12 );
1243 :     printf( "\n" );
1244 :     }
1245 :     }
1246 :    
1247 :     Run_Costs[i] = Best_Cost;
1248 :    
1249 :     if (Best_Cost < Min_Cost + Dist0) {
1250 :     Min_Cost = Best_Cost;
1251 :     Run_Start = i;
1252 :     }
1253 :     else
1254 :     {
1255 : edgomez 1053 /*
1256 :     * as noticed by Michael Niedermayer (michaelni at gmx.at), there's
1257 :     * a code shorter by 1 bit for a larger run (!), same level. We give
1258 :     * it a chance by not moving the left barrier too much.
1259 :     */
1260 : chl 1012
1261 : chl 1011 while( Run_Costs[Run_Start]>Min_Cost+(1<<16) )
1262 :     Run_Start++;
1263 :    
1264 : edgomez 1053 /* spread on preceding coeffs the cost incurred by skipping this one */
1265 : chl 1011 for(j=Run_Start; j<i; ++j) Run_Costs[j] += Dist0;
1266 :     Min_Cost += Dist0;
1267 :     }
1268 :     }
1269 :    
1270 :     if (DBG) {
1271 :     Last_Cost = Evaluate_Cost(Out,Mult,Bias, Zigzag,Non_Zero, Lambda);
1272 :     if (DBG==1) {
1273 :     printf( "=> " );
1274 :     for(i=0; i<=Non_Zero; ++i) printf( "[%3.0d] ", Out[Zigzag[i]] );
1275 :     printf( "\n" );
1276 :     }
1277 :     }
1278 :    
1279 :     if (Last_Node<0)
1280 :     return -1;
1281 :    
1282 : edgomez 1053 /* reconstruct optimal sequence backward with surviving paths */
1283 : chl 1012 memset(Out, 0x00, 64*sizeof(*Out));
1284 : chl 1011 Out[Zigzag[Last_Node]] = Last.Level;
1285 :     i = Last_Node - Last.Run;
1286 :     while(i>=0) {
1287 :     Out[Zigzag[i]] = Nodes[i].Level;
1288 :     i -= Nodes[i].Run;
1289 :     }
1290 :    
1291 :     if (DBG) {
1292 :     uint32_t Cost = Evaluate_Cost(Out,Mult,Bias, Zigzag,Non_Zero, Lambda);
1293 :     if (DBG==1) {
1294 :     printf( "<= " );
1295 :     for(i=0; i<=Last_Node; ++i) printf( "[%3.0d] ", Out[Zigzag[i]] );
1296 :     printf( "\n--------------------------------\n" );
1297 :     }
1298 :     if (Cost>Last_Cost) printf( "!!! %u > %u\n", Cost, Last_Cost );
1299 :     }
1300 :     return Last_Node;
1301 :     }
1302 :    
1303 :     #undef DBG
1304 : chl 1012
1305 :     #endif

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