Parent Directory | Revision Log
Revision 1.21.2.13 - (view) (download)
1 : | edgomez | 1.21.2.6 | /***************************************************************************** |
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 : | edgomez | 1.21.2.13 | * $Id: mbtransquant.c,v 1.21.2.12 2003/05/12 12:33:16 suxen_drol Exp $ |
25 : | edgomez | 1.21.2.6 | * |
26 : | ****************************************************************************/ | ||
27 : | Isibaar | 1.1 | |
28 : | chl | 1.21.2.11 | #include <stdio.h> |
29 : | edgomez | 1.21.2.7 | #include <stdlib.h> |
30 : | chl | 1.21.2.11 | #include <string.h> |
31 : | edgomez | 1.3 | |
32 : | Isibaar | 1.1 | #include "../portab.h" |
33 : | #include "mbfunctions.h" | ||
34 : | |||
35 : | #include "../global.h" | ||
36 : | #include "mem_transfer.h" | ||
37 : | #include "timer.h" | ||
38 : | chl | 1.21.2.9 | #include "../bitstream/mbcoding.h" |
39 : | chl | 1.21.2.10 | #include "../bitstream/zigzag.h" |
40 : | Isibaar | 1.1 | #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 | 1.21 | #include "../image/reduced.h" |
47 : | Isibaar | 1.1 | |
48 : | edgomez | 1.21 | MBFIELDTEST_PTR MBFieldTest; |
49 : | Isibaar | 1.1 | |
50 : | edgomez | 1.21.2.6 | /* |
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 : | |||
57 : | /***************************************************************************** | ||
58 : | * Local functions | ||
59 : | ****************************************************************************/ | ||
60 : | Isibaar | 1.1 | |
61 : | edgomez | 1.21.2.6 | /* permute block and return field dct choice */ |
62 : | static __inline uint32_t | ||
63 : | MBDecideFieldDCT(int16_t data[6 * 64]) | ||
64 : | Isibaar | 1.1 | { |
65 : | edgomez | 1.21.2.6 | uint32_t field = MBFieldTest(data); |
66 : | edgomez | 1.3 | |
67 : | edgomez | 1.21.2.6 | if (field) |
68 : | MBFrameToField(data); | ||
69 : | edgomez | 1.21 | |
70 : | edgomez | 1.21.2.6 | return field; |
71 : | } | ||
72 : | h | 1.2 | |
73 : | edgomez | 1.21.2.6 | /* Performs Forward DCT on all blocks */ |
74 : | static __inline void | ||
75 : | syskin | 1.21.2.8 | MBfDCT(const MBParam * const pParam, |
76 : | const FRAMEINFO * const frame, | ||
77 : | MACROBLOCK * const pMB, | ||
78 : | edgomez | 1.21.2.6 | uint32_t x_pos, |
79 : | uint32_t y_pos, | ||
80 : | int16_t data[6 * 64]) | ||
81 : | syskin | 1.21.2.8 | { |
82 : | edgomez | 1.21.2.6 | /* Handles interlacing */ |
83 : | h | 1.2 | start_timer(); |
84 : | pMB->field_dct = 0; | ||
85 : | edgomez | 1.21.2.5 | if ((frame->vol_flags & XVID_VOL_INTERLACING) && |
86 : | h | 1.11 | (x_pos>0) && (x_pos<pParam->mb_width-1) && |
87 : | (y_pos>0) && (y_pos<pParam->mb_height-1)) { | ||
88 : | h | 1.2 | pMB->field_dct = MBDecideFieldDCT(data); |
89 : | } | ||
90 : | stop_interlacing_timer(); | ||
91 : | |||
92 : | edgomez | 1.21.2.6 | /* 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 | 1.21.2.9 | const FRAMEINFO * const frame, |
122 : | edgomez | 1.21.2.6 | const MACROBLOCK * pMB, |
123 : | syskin | 1.21.2.8 | int16_t qcoeff[6 * 64], |
124 : | edgomez | 1.21.2.6 | int16_t data[6*64]) |
125 : | { | ||
126 : | int i; | ||
127 : | |||
128 : | for (i = 0; i < 6; i++) { | ||
129 : | uint32_t iDcScaler = get_dc_scaler(pMB->quant, i < 4); | ||
130 : | |||
131 : | /* Quantize the block */ | ||
132 : | start_timer(); | ||
133 : | chl | 1.21.2.9 | if (!(pParam->vol_flags & XVID_VOL_MPEGQUANT)) { |
134 : | edgomez | 1.21.2.6 | quant_intra(&data[i * 64], &qcoeff[i * 64], pMB->quant, iDcScaler); |
135 : | chl | 1.21.2.9 | } else { |
136 : | edgomez | 1.21.2.6 | quant4_intra(&data[i * 64], &qcoeff[i * 64], pMB->quant, iDcScaler); |
137 : | chl | 1.21.2.9 | } |
138 : | edgomez | 1.21.2.6 | 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 : | edgomez | 1.7 | for (i = 0; i < 6; i++) { |
152 : | Isibaar | 1.1 | uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
153 : | |||
154 : | start_timer(); | ||
155 : | edgomez | 1.21.2.6 | 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 | 1.1 | |
163 : | chl | 1.21.2.10 | |
164 : | static int | ||
165 : | edgomez | 1.21.2.13 | 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 | 1.21.2.10 | |
171 : | edgomez | 1.21.2.13 | #if 0 |
172 : | chl | 1.21.2.10 | static int |
173 : | edgomez | 1.21.2.13 | 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 | 1.21.2.10 | |
180 : | edgomez | 1.21.2.6 | /* Quantize all blocks -- Inter mode */ |
181 : | static __inline uint8_t | ||
182 : | MBQuantInter(const MBParam * pParam, | ||
183 : | chl | 1.21.2.9 | const FRAMEINFO * const frame, |
184 : | edgomez | 1.21.2.6 | 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 | 1.21.2.8 | |
198 : | edgomez | 1.21.2.6 | /* Quantize the block */ |
199 : | start_timer(); | ||
200 : | chl | 1.21.2.9 | 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 | 1.21.2.10 | sum = dct_quantize_trellis_h263_c(&qcoeff[i*64], &data[i*64], pMB->quant, &scan_tables[0][0], 63)+1; |
204 : | chl | 1.21.2.9 | limit = 1; |
205 : | } | ||
206 : | } else { | ||
207 : | edgomez | 1.21.2.6 | sum = quant4_inter(&qcoeff[i * 64], &data[i * 64], pMB->quant); |
208 : | edgomez | 1.21.2.13 | #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 | 1.21.2.9 | } |
213 : | edgomez | 1.21.2.6 | 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 | 1.7 | } else { |
223 : | edgomez | 1.21 | |
224 : | edgomez | 1.21.2.6 | 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 | 1.21 | } else { |
229 : | edgomez | 1.21.2.6 | /* not direct mode */ |
230 : | if (qcoeff[i*64] != 0) | ||
231 : | code_block = 1; | ||
232 : | edgomez | 1.21 | } |
233 : | } | ||
234 : | edgomez | 1.21.2.6 | |
235 : | /* Set the corresponding cbp bit */ | ||
236 : | cbp |= code_block << (5 - i); | ||
237 : | edgomez | 1.21 | } |
238 : | |||
239 : | edgomez | 1.21.2.6 | return(cbp); |
240 : | } | ||
241 : | Isibaar | 1.1 | |
242 : | edgomez | 1.21.2.6 | /* DeQuantize all blocks -- Inter mode */ |
243 : | syskin | 1.21.2.8 | static __inline void |
244 : | edgomez | 1.21.2.6 | 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 | 1.21.2.8 | if (cbp & (1 << (5 - i))) { |
254 : | edgomez | 1.21.2.6 | 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 | 1.21 | } |
261 : | h | 1.2 | } |
262 : | Isibaar | 1.1 | } |
263 : | |||
264 : | edgomez | 1.21.2.6 | 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 | 1.1 | |
267 : | edgomez | 1.3 | |
268 : | edgomez | 1.21.2.6 | static __inline void |
269 : | syskin | 1.21.2.8 | MBTrans8to16(const MBParam * const pParam, |
270 : | const FRAMEINFO * const frame, | ||
271 : | const MACROBLOCK * const pMB, | ||
272 : | edgomez | 1.21.2.6 | const uint32_t x_pos, |
273 : | const uint32_t y_pos, | ||
274 : | int16_t data[6 * 64]) | ||
275 : | { | ||
276 : | h | 1.4 | uint32_t stride = pParam->edged_width; |
277 : | uint32_t stride2 = stride / 2; | ||
278 : | edgomez | 1.21.2.6 | uint32_t next_block = stride * 8; |
279 : | syskin | 1.21.2.8 | int32_t cst; |
280 : | Isibaar | 1.1 | uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
281 : | syskin | 1.21.2.8 | const IMAGE * const pCurrent = &frame->image; |
282 : | edgomez | 1.21.2.6 | transfer_operation_8to16_t *transfer_op = NULL; |
283 : | edgomez | 1.7 | |
284 : | edgomez | 1.21.2.6 | 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 | 1.21 | 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 | 1.21.2.6 | |
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 | 1.21 | 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 | 1.21.2.6 | |
303 : | /* Block size */ | ||
304 : | cst = 8; | ||
305 : | |||
306 : | /* Operation function */ | ||
307 : | transfer_op = (transfer_operation_8to16_t*)transfer_8to16copy; | ||
308 : | edgomez | 1.21 | } |
309 : | Isibaar | 1.1 | |
310 : | edgomez | 1.21.2.6 | /* Do the transfer */ |
311 : | h | 1.2 | start_timer(); |
312 : | edgomez | 1.21.2.6 | 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 | 1.21.2.8 | } |
320 : | h | 1.2 | |
321 : | edgomez | 1.21.2.6 | static __inline void |
322 : | syskin | 1.21.2.8 | MBTrans16to8(const MBParam * const pParam, |
323 : | const FRAMEINFO * const frame, | ||
324 : | const MACROBLOCK * const pMB, | ||
325 : | edgomez | 1.21.2.6 | 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 | 1.21.2.8 | uint32_t cst; |
336 : | const IMAGE * const pCurrent = &frame->image; | ||
337 : | edgomez | 1.21.2.6 | transfer_operation_16to8_t *transfer_op = NULL; |
338 : | edgomez | 1.21 | |
339 : | edgomez | 1.21.2.6 | if (pMB->field_dct) { |
340 : | next_block = stride; | ||
341 : | stride *= 2; | ||
342 : | } | ||
343 : | Isibaar | 1.1 | |
344 : | edgomez | 1.21.2.6 | if ((frame->vop_flags & XVID_VOP_REDUCED)) { |
345 : | |||
346 : | /* 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 | 1.1 | |
351 : | edgomez | 1.21.2.6 | /* Block size */ |
352 : | cst = 16; | ||
353 : | Isibaar | 1.1 | |
354 : | edgomez | 1.21.2.6 | /* 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 | 1.1 | |
361 : | edgomez | 1.21.2.6 | /* 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 | 1.1 | |
366 : | edgomez | 1.21.2.6 | /* Block size */ |
367 : | cst = 8; | ||
368 : | h | 1.2 | |
369 : | edgomez | 1.21.2.6 | /* 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 : | h | 1.2 | } |
375 : | |||
376 : | edgomez | 1.21.2.6 | /* Do the operation */ |
377 : | h | 1.2 | start_timer(); |
378 : | edgomez | 1.21.2.6 | 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 | 1.2 | stop_transfer_timer(); |
385 : | } | ||
386 : | |||
387 : | edgomez | 1.21.2.6 | /***************************************************************************** |
388 : | * Module functions | ||
389 : | ****************************************************************************/ | ||
390 : | |||
391 : | syskin | 1.21.2.8 | void |
392 : | MBTransQuantIntra(const MBParam * const pParam, | ||
393 : | const FRAMEINFO * const frame, | ||
394 : | MACROBLOCK * const pMB, | ||
395 : | chl | 1.8 | 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 : | |||
401 : | edgomez | 1.21.2.6 | /* Transfer data */ |
402 : | MBTrans8to16(pParam, frame, pMB, x_pos, y_pos, data); | ||
403 : | chl | 1.8 | |
404 : | edgomez | 1.21.2.6 | /* Perform DCT (and field decision) */ |
405 : | MBfDCT(pParam, frame, pMB, x_pos, y_pos, data); | ||
406 : | chl | 1.8 | |
407 : | edgomez | 1.21.2.6 | /* Quantize the block */ |
408 : | chl | 1.21.2.9 | MBQuantIntra(pParam, frame, pMB, data, qcoeff); |
409 : | edgomez | 1.21.2.6 | |
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 | 1.8 | } |
419 : | |||
420 : | edgomez | 1.21.2.6 | |
421 : | chl | 1.8 | uint8_t |
422 : | syskin | 1.21.2.8 | MBTransQuantInter(const MBParam * const pParam, |
423 : | const FRAMEINFO * const frame, | ||
424 : | MACROBLOCK * const pMB, | ||
425 : | edgomez | 1.21.2.2 | const uint32_t x_pos, |
426 : | const uint32_t y_pos, | ||
427 : | chl | 1.8 | int16_t data[6 * 64], |
428 : | int16_t qcoeff[6 * 64]) | ||
429 : | { | ||
430 : | uint8_t cbp; | ||
431 : | edgomez | 1.21.2.6 | uint32_t limit; |
432 : | chl | 1.8 | |
433 : | edgomez | 1.21.2.2 | /* |
434 : | edgomez | 1.21.2.6 | * There is no MBTrans8to16 for Inter block, that's done in motion compensation |
435 : | * already | ||
436 : | edgomez | 1.21.2.2 | */ |
437 : | |||
438 : | edgomez | 1.21.2.6 | /* Perform DCT (and field decision) */ |
439 : | MBfDCT(pParam, frame, pMB, x_pos, y_pos, data); | ||
440 : | chl | 1.8 | |
441 : | edgomez | 1.21.2.6 | /* Set the limit threshold */ |
442 : | limit = PVOP_TOOSMALL_LIMIT + ((pMB->quant == 1)? 1 : 0); | ||
443 : | chl | 1.8 | |
444 : | edgomez | 1.21.2.6 | /* Quantize the block */ |
445 : | chl | 1.21.2.9 | cbp = MBQuantInter(pParam, frame, pMB, data, qcoeff, 0, limit); |
446 : | chl | 1.8 | |
447 : | edgomez | 1.21.2.6 | /* DeQuantize the block */ |
448 : | MBDeQuantInter(pParam, pMB->quant, data, qcoeff, cbp); | ||
449 : | chl | 1.8 | |
450 : | edgomez | 1.21.2.6 | /* Perform inverse DCT*/ |
451 : | MBiDCT(data, cbp); | ||
452 : | chl | 1.8 | |
453 : | edgomez | 1.21.2.6 | /* Transfer back the data -- Add the data */ |
454 : | MBTrans16to8(pParam, frame, pMB, x_pos, y_pos, data, 1, cbp); | ||
455 : | syskin | 1.21.2.8 | |
456 : | edgomez | 1.21.2.6 | return(cbp); |
457 : | chl | 1.8 | } |
458 : | |||
459 : | edgomez | 1.21.2.6 | uint8_t |
460 : | MBTransQuantInterBVOP(const MBParam * pParam, | ||
461 : | chl | 1.8 | FRAMEINFO * frame, |
462 : | MACROBLOCK * pMB, | ||
463 : | const uint32_t x_pos, | ||
464 : | const uint32_t y_pos, | ||
465 : | int16_t data[6 * 64], | ||
466 : | edgomez | 1.21.2.6 | int16_t qcoeff[6 * 64]) |
467 : | chl | 1.8 | { |
468 : | edgomez | 1.21.2.6 | uint8_t cbp; |
469 : | uint32_t limit; | ||
470 : | syskin | 1.21.2.8 | |
471 : | edgomez | 1.21.2.6 | /* |
472 : | * There is no MBTrans8to16 for Inter block, that's done in motion compensation | ||
473 : | * already | ||
474 : | */ | ||
475 : | chl | 1.8 | |
476 : | edgomez | 1.21.2.6 | /* Perform DCT (and field decision) */ |
477 : | MBfDCT(pParam, frame, pMB, x_pos, y_pos, data); | ||
478 : | chl | 1.8 | |
479 : | edgomez | 1.21.2.6 | /* Set the limit threshold */ |
480 : | limit = BVOP_TOOSMALL_LIMIT; | ||
481 : | chl | 1.8 | |
482 : | edgomez | 1.21.2.6 | /* Quantize the block */ |
483 : | chl | 1.21.2.9 | cbp = MBQuantInter(pParam, frame, pMB, data, qcoeff, 1, limit); |
484 : | h | 1.2 | |
485 : | edgomez | 1.21.2.6 | /* |
486 : | * History comment: | ||
487 : | * We don't have to DeQuant, iDCT and Transfer back data for B-frames. | ||
488 : | * | ||
489 : | * BUT some plugins require the original frame to be passed so we have | ||
490 : | * to take care of that here | ||
491 : | */ | ||
492 : | if((pParam->plugin_flags & XVID_REQORIGINAL)) { | ||
493 : | h | 1.2 | |
494 : | edgomez | 1.21.2.6 | /* DeQuantize the block */ |
495 : | MBDeQuantInter(pParam, pMB->quant, data, qcoeff, cbp); | ||
496 : | Isibaar | 1.1 | |
497 : | edgomez | 1.21.2.6 | /* Perform inverse DCT*/ |
498 : | MBiDCT(data, cbp); | ||
499 : | edgomez | 1.21 | |
500 : | edgomez | 1.21.2.6 | /* Transfer back the data -- Add the data */ |
501 : | MBTrans16to8(pParam, frame, pMB, x_pos, y_pos, data, 1, cbp); | ||
502 : | edgomez | 1.21 | } |
503 : | |||
504 : | edgomez | 1.21.2.6 | return(cbp); |
505 : | edgomez | 1.21 | } |
506 : | |||
507 : | /* if sum(diff between field lines) < sum(diff between frame lines), use field dct */ | ||
508 : | uint32_t | ||
509 : | MBFieldTest_c(int16_t data[6 * 64]) | ||
510 : | { | ||
511 : | edgomez | 1.7 | const uint8_t blocks[] = |
512 : | { 0 * 64, 0 * 64, 0 * 64, 0 * 64, 2 * 64, 2 * 64, 2 * 64, 2 * 64 }; | ||
513 : | const uint8_t lines[] = { 0, 16, 32, 48, 0, 16, 32, 48 }; | ||
514 : | h | 1.2 | |
515 : | int frame = 0, field = 0; | ||
516 : | int i, j; | ||
517 : | |||
518 : | edgomez | 1.7 | for (i = 0; i < 7; ++i) { |
519 : | for (j = 0; j < 8; ++j) { | ||
520 : | frame += | ||
521 : | edgomez | 1.21.2.7 | abs(data[0 * 64 + (i + 1) * 8 + j] - data[0 * 64 + i * 8 + j]); |
522 : | edgomez | 1.7 | frame += |
523 : | edgomez | 1.21.2.7 | abs(data[1 * 64 + (i + 1) * 8 + j] - data[1 * 64 + i * 8 + j]); |
524 : | edgomez | 1.7 | frame += |
525 : | edgomez | 1.21.2.7 | abs(data[2 * 64 + (i + 1) * 8 + j] - data[2 * 64 + i * 8 + j]); |
526 : | edgomez | 1.7 | frame += |
527 : | edgomez | 1.21.2.7 | abs(data[3 * 64 + (i + 1) * 8 + j] - data[3 * 64 + i * 8 + j]); |
528 : | edgomez | 1.7 | |
529 : | field += | ||
530 : | edgomez | 1.21.2.7 | abs(data[blocks[i + 1] + lines[i + 1] + j] - |
531 : | edgomez | 1.7 | data[blocks[i] + lines[i] + j]); |
532 : | field += | ||
533 : | edgomez | 1.21.2.7 | abs(data[blocks[i + 1] + lines[i + 1] + 8 + j] - |
534 : | edgomez | 1.7 | data[blocks[i] + lines[i] + 8 + j]); |
535 : | field += | ||
536 : | edgomez | 1.21.2.7 | abs(data[blocks[i + 1] + 64 + lines[i + 1] + j] - |
537 : | edgomez | 1.7 | data[blocks[i] + 64 + lines[i] + j]); |
538 : | field += | ||
539 : | edgomez | 1.21.2.7 | abs(data[blocks[i + 1] + 64 + lines[i + 1] + 8 + j] - |
540 : | edgomez | 1.7 | data[blocks[i] + 64 + lines[i] + 8 + j]); |
541 : | Isibaar | 1.1 | } |
542 : | } | ||
543 : | h | 1.2 | |
544 : | edgomez | 1.21 | return (frame >= (field + 350)); |
545 : | h | 1.2 | } |
546 : | |||
547 : | |||
548 : | /* deinterlace Y blocks vertically */ | ||
549 : | |||
550 : | #define MOVLINE(X,Y) memcpy(X, Y, sizeof(tmp)) | ||
551 : | syskin | 1.21.2.8 | #define LINE(X,Y) &data[X*64 + Y*8] |
552 : | h | 1.2 | |
553 : | edgomez | 1.7 | void |
554 : | MBFrameToField(int16_t data[6 * 64]) | ||
555 : | h | 1.2 | { |
556 : | int16_t tmp[8]; | ||
557 : | |||
558 : | /* left blocks */ | ||
559 : | |||
560 : | edgomez | 1.21.2.13 | /* 1=2, 2=4, 4=8, 8=1 */ |
561 : | edgomez | 1.7 | MOVLINE(tmp, LINE(0, 1)); |
562 : | MOVLINE(LINE(0, 1), LINE(0, 2)); | ||
563 : | MOVLINE(LINE(0, 2), LINE(0, 4)); | ||
564 : | MOVLINE(LINE(0, 4), LINE(2, 0)); | ||
565 : | MOVLINE(LINE(2, 0), tmp); | ||
566 : | h | 1.2 | |
567 : | edgomez | 1.21.2.13 | /* 3=6, 6=12, 12=9, 9=3 */ |
568 : | edgomez | 1.7 | MOVLINE(tmp, LINE(0, 3)); |
569 : | MOVLINE(LINE(0, 3), LINE(0, 6)); | ||
570 : | MOVLINE(LINE(0, 6), LINE(2, 4)); | ||
571 : | MOVLINE(LINE(2, 4), LINE(2, 1)); | ||
572 : | MOVLINE(LINE(2, 1), tmp); | ||
573 : | h | 1.2 | |
574 : | edgomez | 1.21.2.13 | /* 5=10, 10=5 */ |
575 : | edgomez | 1.7 | MOVLINE(tmp, LINE(0, 5)); |
576 : | MOVLINE(LINE(0, 5), LINE(2, 2)); | ||
577 : | MOVLINE(LINE(2, 2), tmp); | ||
578 : | h | 1.2 | |
579 : | edgomez | 1.21.2.13 | /* 7=14, 14=13, 13=11, 11=7 */ |
580 : | edgomez | 1.7 | MOVLINE(tmp, LINE(0, 7)); |
581 : | MOVLINE(LINE(0, 7), LINE(2, 6)); | ||
582 : | MOVLINE(LINE(2, 6), LINE(2, 5)); | ||
583 : | MOVLINE(LINE(2, 5), LINE(2, 3)); | ||
584 : | MOVLINE(LINE(2, 3), tmp); | ||
585 : | h | 1.2 | |
586 : | /* right blocks */ | ||
587 : | |||
588 : | edgomez | 1.21.2.13 | /* 1=2, 2=4, 4=8, 8=1 */ |
589 : | edgomez | 1.7 | MOVLINE(tmp, LINE(1, 1)); |
590 : | MOVLINE(LINE(1, 1), LINE(1, 2)); | ||
591 : | MOVLINE(LINE(1, 2), LINE(1, 4)); | ||
592 : | MOVLINE(LINE(1, 4), LINE(3, 0)); | ||
593 : | MOVLINE(LINE(3, 0), tmp); | ||
594 : | h | 1.2 | |
595 : | edgomez | 1.21.2.13 | /* 3=6, 6=12, 12=9, 9=3 */ |
596 : | edgomez | 1.7 | MOVLINE(tmp, LINE(1, 3)); |
597 : | MOVLINE(LINE(1, 3), LINE(1, 6)); | ||
598 : | MOVLINE(LINE(1, 6), LINE(3, 4)); | ||
599 : | MOVLINE(LINE(3, 4), LINE(3, 1)); | ||
600 : | MOVLINE(LINE(3, 1), tmp); | ||
601 : | h | 1.2 | |
602 : | edgomez | 1.21.2.13 | /* 5=10, 10=5 */ |
603 : | edgomez | 1.7 | MOVLINE(tmp, LINE(1, 5)); |
604 : | MOVLINE(LINE(1, 5), LINE(3, 2)); | ||
605 : | MOVLINE(LINE(3, 2), tmp); | ||
606 : | h | 1.2 | |
607 : | edgomez | 1.21.2.13 | /* 7=14, 14=13, 13=11, 11=7 */ |
608 : | edgomez | 1.7 | MOVLINE(tmp, LINE(1, 7)); |
609 : | MOVLINE(LINE(1, 7), LINE(3, 6)); | ||
610 : | MOVLINE(LINE(3, 6), LINE(3, 5)); | ||
611 : | MOVLINE(LINE(3, 5), LINE(3, 3)); | ||
612 : | MOVLINE(LINE(3, 3), tmp); | ||
613 : | Isibaar | 1.1 | } |
614 : | chl | 1.21.2.10 | |
615 : | |||
616 : | |||
617 : | |||
618 : | |||
619 : | edgomez | 1.21.2.13 | /***************************************************************************** |
620 : | * Trellis based R-D optimal quantization | ||
621 : | * | ||
622 : | * Trellis Quant code (C) 2003 Pascal Massimino skal(at)planet-d.net | ||
623 : | * | ||
624 : | ****************************************************************************/ | ||
625 : | chl | 1.21.2.10 | |
626 : | |||
627 : | edgomez | 1.21.2.13 | #if 0 |
628 : | chl | 1.21.2.11 | static int |
629 : | edgomez | 1.21.2.13 | dct_quantize_trellis_mpeg_c(int16_t *const Out, |
630 : | const int16_t *const In, | ||
631 : | int Q, | ||
632 : | const uint16_t * const Zigzag, | ||
633 : | int Non_Zero) | ||
634 : | { | ||
635 : | return 63; | ||
636 : | } | ||
637 : | #endif | ||
638 : | |||
639 : | /*---------------------------------------------------------------------------- | ||
640 : | * | ||
641 : | * Trellis-Based quantization | ||
642 : | * | ||
643 : | * So far I understand this paper: | ||
644 : | * | ||
645 : | * "Trellis-Based R-D Optimal Quantization in H.263+" | ||
646 : | * J.Wen, M.Luttrell, J.Villasenor | ||
647 : | * IEEE Transactions on Image Processing, Vol.9, No.8, Aug. 2000. | ||
648 : | * | ||
649 : | * we are at stake with a simplified Bellmand-Ford / Dijkstra Single | ||
650 : | * Source Shorted Path algo. But due to the underlying graph structure | ||
651 : | * ("Trellis"), it can be turned into a dynamic programming algo, | ||
652 : | * partially saving the explicit graph's nodes representation. And | ||
653 : | * without using a heap, since the open frontier of the DAG is always | ||
654 : | * known, and of fixed sized. | ||
655 : | *--------------------------------------------------------------------------*/ | ||
656 : | |||
657 : | chl | 1.21.2.10 | |
658 : | |||
659 : | edgomez | 1.21.2.13 | /* Codes lengths for relevant levels. */ |
660 : | chl | 1.21.2.10 | |
661 : | edgomez | 1.21.2.13 | /* let's factorize: */ |
662 : | chl | 1.21.2.10 | static const uint8_t Code_Len0[64] = { |
663 : | 30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30, | ||
664 : | 30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 }; | ||
665 : | static const uint8_t Code_Len1[64] = { | ||
666 : | 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, | ||
667 : | 30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 }; | ||
668 : | static const uint8_t Code_Len2[64] = { | ||
669 : | 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, | ||
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_Len3[64] = { | ||
672 : | 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, | ||
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_Len4[64] = { | ||
675 : | 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, | ||
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_Len5[64] = { | ||
678 : | 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, | ||
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_Len6[64] = { | ||
681 : | 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, | ||
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_Len7[64] = { | ||
684 : | 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, | ||
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_Len8[64] = { | ||
687 : | 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, | ||
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_Len9[64] = { | ||
690 : | 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, | ||
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_Len10[64] = { | ||
693 : | 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, | ||
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_Len11[64] = { | ||
696 : | 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, | ||
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_Len12[64] = { | ||
699 : | 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, | ||
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_Len13[64] = { | ||
702 : | 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, | ||
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_Len14[64] = { | ||
705 : | 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, | ||
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_Len15[64] = { | ||
708 : | 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, | ||
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_Len16[64] = { | ||
711 : | 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, | ||
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_Len17[64] = { | ||
714 : | 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, | ||
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_Len18[64] = { | ||
717 : | 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, | ||
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_Len19[64] = { | ||
720 : | 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, | ||
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_Len20[64] = { | ||
723 : | 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, | ||
724 : | 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 }; | ||
725 : | |||
726 : | edgomez | 1.21.2.13 | /* a few more table for LAST table: */ |
727 : | chl | 1.21.2.10 | static const uint8_t Code_Len21[64] = { |
728 : | 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, | ||
729 : | 30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30}; | ||
730 : | static const uint8_t Code_Len22[64] = { | ||
731 : | 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, | ||
732 : | 30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30}; | ||
733 : | static const uint8_t Code_Len23[64] = { | ||
734 : | 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, | ||
735 : | 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}; | ||
736 : | static const uint8_t Code_Len24[64] = { | ||
737 : | 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, | ||
738 : | 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}; | ||
739 : | |||
740 : | |||
741 : | edgomez | 1.21.2.13 | static const uint8_t * const B16_17_Code_Len[24] = { /* levels [1..24] */ |
742 : | chl | 1.21.2.10 | Code_Len20,Code_Len19,Code_Len18,Code_Len17, |
743 : | Code_Len16,Code_Len15,Code_Len14,Code_Len13, | ||
744 : | Code_Len12,Code_Len11,Code_Len10,Code_Len9, | ||
745 : | Code_Len8, Code_Len7 ,Code_Len6 ,Code_Len5, | ||
746 : | Code_Len4, Code_Len3, Code_Len3 ,Code_Len2, | ||
747 : | Code_Len2, Code_Len1, Code_Len1, Code_Len1, | ||
748 : | }; | ||
749 : | |||
750 : | edgomez | 1.21.2.13 | static const uint8_t * const B16_17_Code_Len_Last[6] = { /* levels [1..6] */ |
751 : | chl | 1.21.2.10 | Code_Len24,Code_Len23,Code_Len22,Code_Len21, Code_Len3, Code_Len1, |
752 : | }; | ||
753 : | |||
754 : | #define TL(q) 0xfe00/(q*q) | ||
755 : | |||
756 : | static const int Trellis_Lambda_Tabs[31] = { | ||
757 : | TL( 1),TL( 2),TL( 3),TL( 4),TL( 5),TL( 6), TL( 7), | ||
758 : | TL( 8),TL( 9),TL(10),TL(11),TL(12),TL(13),TL(14), TL(15), | ||
759 : | TL(16),TL(17),TL(18),TL(19),TL(20),TL(21),TL(22), TL(23), | ||
760 : | TL(24),TL(25),TL(26),TL(27),TL(28),TL(29),TL(30), TL(31) | ||
761 : | }; | ||
762 : | #undef TL | ||
763 : | |||
764 : | suxen_drol | 1.21.2.12 | static __inline int Find_Last(const int16_t *C, const uint16_t *Zigzag, int i) |
765 : | chl | 1.21.2.10 | { |
766 : | while(i>=0) | ||
767 : | if (C[Zigzag[i]]) | ||
768 : | return i; | ||
769 : | else i--; | ||
770 : | return -1; | ||
771 : | } | ||
772 : | |||
773 : | edgomez | 1.21.2.13 | /* this routine has been strippen of all debug code */ |
774 : | chl | 1.21.2.11 | |
775 : | static int | ||
776 : | dct_quantize_trellis_h263_c(int16_t *const Out, const int16_t *const In, int Q, const uint16_t * const Zigzag, int Non_Zero) | ||
777 : | { | ||
778 : | |||
779 : | edgomez | 1.21.2.13 | /* |
780 : | * Note: We should search last non-zero coeffs on *real* DCT input coeffs (In[]), | ||
781 : | * not quantized one (Out[]). However, it only improves the result *very* | ||
782 : | * slightly (~0.01dB), whereas speed drops to crawling level :) | ||
783 : | * Well, actually, taking 1 more coeff past Non_Zero into account sometimes helps. | ||
784 : | */ | ||
785 : | chl | 1.21.2.11 | typedef struct { int16_t Run, Level; } NODE; |
786 : | |||
787 : | NODE Nodes[65], Last; | ||
788 : | uint32_t Run_Costs0[64+1]; | ||
789 : | uint32_t * const Run_Costs = Run_Costs0 + 1; | ||
790 : | const int Mult = 2*Q; | ||
791 : | const int Bias = (Q-1) | 1; | ||
792 : | const int Lev0 = Mult + Bias; | ||
793 : | edgomez | 1.21.2.13 | const int Lambda = Trellis_Lambda_Tabs[Q-1]; /* it's 1/lambda, actually */ |
794 : | chl | 1.21.2.11 | |
795 : | int Run_Start = -1; | ||
796 : | uint32_t Min_Cost = 2<<16; | ||
797 : | |||
798 : | int Last_Node = -1; | ||
799 : | uint32_t Last_Cost = 0; | ||
800 : | |||
801 : | int i, j; | ||
802 : | edgomez | 1.21.2.13 | Run_Costs[-1] = 2<<16; /* source (w/ CBP penalty) */ |
803 : | chl | 1.21.2.11 | |
804 : | Non_Zero = Find_Last(Out, Zigzag, Non_Zero); | ||
805 : | if (Non_Zero<0) | ||
806 : | return -1; | ||
807 : | |||
808 : | for(i=0; i<=Non_Zero; i++) | ||
809 : | { | ||
810 : | const int AC = In[Zigzag[i]]; | ||
811 : | const int Level1 = Out[Zigzag[i]]; | ||
812 : | const int Dist0 = Lambda* AC*AC; | ||
813 : | uint32_t Best_Cost = 0xf0000000; | ||
814 : | Last_Cost += Dist0; | ||
815 : | |||
816 : | edgomez | 1.21.2.13 | if ((uint32_t)(Level1+1)<3) /* very specialized loop for -1,0,+1 */ |
817 : | chl | 1.21.2.11 | { |
818 : | int dQ; | ||
819 : | int Run; | ||
820 : | uint32_t Cost0; | ||
821 : | |||
822 : | if (AC<0) { | ||
823 : | Nodes[i].Level = -1; | ||
824 : | dQ = Lev0 + AC; | ||
825 : | } else { | ||
826 : | Nodes[i].Level = 1; | ||
827 : | dQ = Lev0 - AC; | ||
828 : | } | ||
829 : | Cost0 = Lambda*dQ*dQ; | ||
830 : | |||
831 : | Nodes[i].Run = 1; | ||
832 : | Best_Cost = (Code_Len20[0]<<16) + Run_Costs[i-1]+Cost0; | ||
833 : | for(Run=i-Run_Start; Run>0; --Run) | ||
834 : | { | ||
835 : | const uint32_t Cost_Base = Cost0 + Run_Costs[i-Run]; | ||
836 : | const uint32_t Cost = Cost_Base + (Code_Len20[Run-1]<<16); | ||
837 : | const uint32_t lCost = Cost_Base + (Code_Len24[Run-1]<<16); | ||
838 : | |||
839 : | edgomez | 1.21.2.13 | /* |
840 : | * TODO: what about tie-breaks? Should we favor short runs or | ||
841 : | * long runs? Although the error is the same, it would not be | ||
842 : | * spread the same way along high and low frequencies... | ||
843 : | */ | ||
844 : | chl | 1.21.2.11 | |
845 : | edgomez | 1.21.2.13 | /* (I'd say: favour short runs => hifreq errors (HVS) -- gruel ) */ |
846 : | chl | 1.21.2.11 | |
847 : | if (Cost<Best_Cost) { | ||
848 : | Best_Cost = Cost; | ||
849 : | Nodes[i].Run = Run; | ||
850 : | } | ||
851 : | |||
852 : | if (lCost<Last_Cost) { | ||
853 : | Last_Cost = lCost; | ||
854 : | Last.Run = Run; | ||
855 : | Last_Node = i; | ||
856 : | } | ||
857 : | } | ||
858 : | if (Last_Node==i) | ||
859 : | Last.Level = Nodes[i].Level; | ||
860 : | } | ||
861 : | edgomez | 1.21.2.13 | else /* "big" levels */ |
862 : | chl | 1.21.2.11 | { |
863 : | const uint8_t *Tbl_L1, *Tbl_L2, *Tbl_L1_Last, *Tbl_L2_Last; | ||
864 : | int Level2; | ||
865 : | int dQ1, dQ2; | ||
866 : | int Run; | ||
867 : | uint32_t Dist1,Dist2; | ||
868 : | int dDist21; | ||
869 : | |||
870 : | if (Level1>1) { | ||
871 : | dQ1 = Level1*Mult-AC + Bias; | ||
872 : | dQ2 = dQ1 - Mult; | ||
873 : | Level2 = Level1-1; | ||
874 : | Tbl_L1 = (Level1<=24) ? B16_17_Code_Len[Level1-1] : Code_Len0; | ||
875 : | Tbl_L2 = (Level2<=24) ? B16_17_Code_Len[Level2-1] : Code_Len0; | ||
876 : | Tbl_L1_Last = (Level1<=6) ? B16_17_Code_Len_Last[Level1-1] : Code_Len0; | ||
877 : | Tbl_L2_Last = (Level2<=6) ? B16_17_Code_Len_Last[Level2-1] : Code_Len0; | ||
878 : | edgomez | 1.21.2.13 | } else { /* Level1<-1 */ |
879 : | chl | 1.21.2.11 | dQ1 = Level1*Mult-AC - Bias; |
880 : | dQ2 = dQ1 + Mult; | ||
881 : | Level2 = Level1 + 1; | ||
882 : | Tbl_L1 = (Level1>=-24) ? B16_17_Code_Len[Level1^-1] : Code_Len0; | ||
883 : | Tbl_L2 = (Level2>=-24) ? B16_17_Code_Len[Level2^-1] : Code_Len0; | ||
884 : | Tbl_L1_Last = (Level1>=- 6) ? B16_17_Code_Len_Last[Level1^-1] : Code_Len0; | ||
885 : | Tbl_L2_Last = (Level2>=- 6) ? B16_17_Code_Len_Last[Level2^-1] : Code_Len0; | ||
886 : | } | ||
887 : | Dist1 = Lambda*dQ1*dQ1; | ||
888 : | Dist2 = Lambda*dQ2*dQ2; | ||
889 : | dDist21 = Dist2-Dist1; | ||
890 : | |||
891 : | for(Run=i-Run_Start; Run>0; --Run) | ||
892 : | { | ||
893 : | const uint32_t Cost_Base = Dist1 + Run_Costs[i-Run]; | ||
894 : | uint32_t Cost1, Cost2; | ||
895 : | int bLevel; | ||
896 : | |||
897 : | edgomez | 1.21.2.13 | /* |
898 : | * for sub-optimal (but slightly worth it, speed-wise) search, uncomment the following: | ||
899 : | * if (Cost_Base>=Best_Cost) continue; | ||
900 : | * (? doesn't seem to have any effect -- gruel ) | ||
901 : | */ | ||
902 : | chl | 1.21.2.11 | |
903 : | Cost1 = Cost_Base + (Tbl_L1[Run-1]<<16); | ||
904 : | Cost2 = Cost_Base + (Tbl_L2[Run-1]<<16) + dDist21; | ||
905 : | |||
906 : | if (Cost2<Cost1) { | ||
907 : | Cost1 = Cost2; | ||
908 : | bLevel = Level2; | ||
909 : | } else | ||
910 : | bLevel = Level1; | ||
911 : | |||
912 : | if (Cost1<Best_Cost) { | ||
913 : | Best_Cost = Cost1; | ||
914 : | Nodes[i].Run = Run; | ||
915 : | Nodes[i].Level = bLevel; | ||
916 : | } | ||
917 : | |||
918 : | Cost1 = Cost_Base + (Tbl_L1_Last[Run-1]<<16); | ||
919 : | Cost2 = Cost_Base + (Tbl_L2_Last[Run-1]<<16) + dDist21; | ||
920 : | |||
921 : | if (Cost2<Cost1) { | ||
922 : | Cost1 = Cost2; | ||
923 : | bLevel = Level2; | ||
924 : | } else | ||
925 : | bLevel = Level1; | ||
926 : | |||
927 : | if (Cost1<Last_Cost) { | ||
928 : | Last_Cost = Cost1; | ||
929 : | Last.Run = Run; | ||
930 : | Last.Level = bLevel; | ||
931 : | Last_Node = i; | ||
932 : | } | ||
933 : | edgomez | 1.21.2.13 | } /* end of "for Run" */ |
934 : | chl | 1.21.2.11 | |
935 : | } | ||
936 : | |||
937 : | Run_Costs[i] = Best_Cost; | ||
938 : | |||
939 : | if (Best_Cost < Min_Cost + Dist0) { | ||
940 : | Min_Cost = Best_Cost; | ||
941 : | Run_Start = i; | ||
942 : | } | ||
943 : | else | ||
944 : | { | ||
945 : | edgomez | 1.21.2.13 | /* |
946 : | * as noticed by Michael Niedermayer (michaelni at gmx.at), there's | ||
947 : | * a code shorter by 1 bit for a larger run (!), same level. We give | ||
948 : | * it a chance by not moving the left barrier too much. | ||
949 : | */ | ||
950 : | chl | 1.21.2.11 | |
951 : | while( Run_Costs[Run_Start]>Min_Cost+(1<<16) ) | ||
952 : | Run_Start++; | ||
953 : | |||
954 : | edgomez | 1.21.2.13 | /* spread on preceding coeffs the cost incurred by skipping this one */ |
955 : | chl | 1.21.2.11 | for(j=Run_Start; j<i; ++j) Run_Costs[j] += Dist0; |
956 : | Min_Cost += Dist0; | ||
957 : | } | ||
958 : | } | ||
959 : | |||
960 : | if (Last_Node<0) | ||
961 : | return -1; | ||
962 : | |||
963 : | edgomez | 1.21.2.13 | /* reconstruct optimal sequence backward with surviving paths */ |
964 : | chl | 1.21.2.11 | memset(Out, 0x00, 64*sizeof(*Out)); |
965 : | Out[Zigzag[Last_Node]] = Last.Level; | ||
966 : | i = Last_Node - Last.Run; | ||
967 : | while(i>=0) { | ||
968 : | Out[Zigzag[i]] = Nodes[i].Level; | ||
969 : | i -= Nodes[i].Run; | ||
970 : | } | ||
971 : | return Last_Node; | ||
972 : | } | ||
973 : | |||
974 : | |||
975 : | |||
976 : | |||
977 : | |||
978 : | |||
979 : | |||
980 : | |||
981 : | |||
982 : | |||
983 : | |||
984 : | edgomez | 1.21.2.13 | /* original version including heavy debugging info */ |
985 : | chl | 1.21.2.11 | |
986 : | #ifdef DBGTRELL | ||
987 : | chl | 1.21.2.10 | |
988 : | #define DBG 0 | ||
989 : | |||
990 : | suxen_drol | 1.21.2.12 | static __inline uint32_t Evaluate_Cost(const int16_t *C, int Mult, int Bias, |
991 : | chl | 1.21.2.10 | const uint16_t * Zigzag, int Max, int Lambda) |
992 : | { | ||
993 : | #if (DBG>0) | ||
994 : | const int16_t * const Ref = C + 6*64; | ||
995 : | int Last = Max; | ||
996 : | int Bits = 0; | ||
997 : | chl | 1.21.2.11 | int Dist = 0; |
998 : | int i; | ||
999 : | uint32_t Cost; | ||
1000 : | |||
1001 : | while(Last>=0 && C[Zigzag[Last]]==0) | ||
1002 : | Last--; | ||
1003 : | |||
1004 : | chl | 1.21.2.10 | if (Last>=0) { |
1005 : | int j=0, j0=0; | ||
1006 : | int Run, Level; | ||
1007 : | chl | 1.21.2.11 | |
1008 : | edgomez | 1.21.2.13 | Bits = 2; /* CBP */ |
1009 : | chl | 1.21.2.10 | while(j<Last) { |
1010 : | chl | 1.21.2.11 | while(!C[Zigzag[j]]) |
1011 : | j++; | ||
1012 : | if (j==Last) | ||
1013 : | break; | ||
1014 : | chl | 1.21.2.10 | Level=C[Zigzag[j]]; |
1015 : | Run = j - j0; | ||
1016 : | j0 = ++j; | ||
1017 : | chl | 1.21.2.11 | if (Level>=-24 && Level<=24) |
1018 : | Bits += B16_17_Code_Len[(Level<0) ? -Level-1 : Level-1][Run]; | ||
1019 : | else | ||
1020 : | Bits += 30; | ||
1021 : | chl | 1.21.2.10 | } |
1022 : | Level = C[Zigzag[Last]]; | ||
1023 : | Run = j - j0; | ||
1024 : | chl | 1.21.2.11 | if (Level>=-6 && Level<=6) |
1025 : | Bits += B16_17_Code_Len_Last[(Level<0) ? -Level-1 : Level-1][Run]; | ||
1026 : | else | ||
1027 : | Bits += 30; | ||
1028 : | chl | 1.21.2.10 | } |
1029 : | |||
1030 : | for(i=0; i<=Last; ++i) { | ||
1031 : | int V = C[Zigzag[i]]*Mult; | ||
1032 : | chl | 1.21.2.11 | if (V>0) |
1033 : | V += Bias; | ||
1034 : | else | ||
1035 : | if (V<0) | ||
1036 : | V -= Bias; | ||
1037 : | chl | 1.21.2.10 | V -= Ref[Zigzag[i]]; |
1038 : | Dist += V*V; | ||
1039 : | } | ||
1040 : | chl | 1.21.2.11 | Cost = Lambda*Dist + (Bits<<16); |
1041 : | chl | 1.21.2.10 | if (DBG==1) |
1042 : | printf( " Last:%2d/%2d Cost = [(Bits=%5.0d) + Lambda*(Dist=%6.0d) = %d ] >>12= %d ", Last,Max, Bits, Dist, Cost, Cost>>12 ); | ||
1043 : | return Cost; | ||
1044 : | |||
1045 : | #else | ||
1046 : | return 0; | ||
1047 : | #endif | ||
1048 : | } | ||
1049 : | |||
1050 : | |||
1051 : | static int | ||
1052 : | dct_quantize_trellis_h263_c(int16_t *const Out, const int16_t *const In, int Q, const uint16_t * const Zigzag, int Non_Zero) | ||
1053 : | { | ||
1054 : | |||
1055 : | edgomez | 1.21.2.13 | /* |
1056 : | * Note: We should search last non-zero coeffs on *real* DCT input coeffs (In[]), | ||
1057 : | * not quantized one (Out[]). However, it only improves the result *very* | ||
1058 : | * slightly (~0.01dB), whereas speed drops to crawling level :) | ||
1059 : | * Well, actually, taking 1 more coeff past Non_Zero into account sometimes helps. | ||
1060 : | */ | ||
1061 : | chl | 1.21.2.10 | typedef struct { int16_t Run, Level; } NODE; |
1062 : | |||
1063 : | NODE Nodes[65], Last; | ||
1064 : | chl | 1.21.2.11 | uint32_t Run_Costs0[64+1]; |
1065 : | uint32_t * const Run_Costs = Run_Costs0 + 1; | ||
1066 : | chl | 1.21.2.10 | const int Mult = 2*Q; |
1067 : | const int Bias = (Q-1) | 1; | ||
1068 : | const int Lev0 = Mult + Bias; | ||
1069 : | edgomez | 1.21.2.13 | const int Lambda = Trellis_Lambda_Tabs[Q-1]; /* it's 1/lambda, actually */ |
1070 : | chl | 1.21.2.10 | |
1071 : | int Run_Start = -1; | ||
1072 : | edgomez | 1.21.2.13 | Run_Costs[-1] = 2<<16; /* source (w/ CBP penalty) */ |
1073 : | chl | 1.21.2.10 | uint32_t Min_Cost = 2<<16; |
1074 : | |||
1075 : | int Last_Node = -1; | ||
1076 : | uint32_t Last_Cost = 0; | ||
1077 : | |||
1078 : | chl | 1.21.2.11 | int i, j; |
1079 : | |||
1080 : | chl | 1.21.2.10 | #if (DBG>0) |
1081 : | edgomez | 1.21.2.13 | Last.Level = 0; Last.Run = -1; /* just initialize to smthg */ |
1082 : | chl | 1.21.2.10 | #endif |
1083 : | |||
1084 : | Non_Zero = Find_Last(Out, Zigzag, Non_Zero); | ||
1085 : | if (Non_Zero<0) | ||
1086 : | return -1; | ||
1087 : | |||
1088 : | for(i=0; i<=Non_Zero; i++) | ||
1089 : | { | ||
1090 : | const int AC = In[Zigzag[i]]; | ||
1091 : | const int Level1 = Out[Zigzag[i]]; | ||
1092 : | const int Dist0 = Lambda* AC*AC; | ||
1093 : | uint32_t Best_Cost = 0xf0000000; | ||
1094 : | Last_Cost += Dist0; | ||
1095 : | |||
1096 : | edgomez | 1.21.2.13 | if ((uint32_t)(Level1+1)<3) /* very specialized loop for -1,0,+1 */ |
1097 : | chl | 1.21.2.10 | { |
1098 : | chl | 1.21.2.11 | int dQ; |
1099 : | int Run; | ||
1100 : | uint32_t Cost0; | ||
1101 : | chl | 1.21.2.10 | |
1102 : | if (AC<0) { | ||
1103 : | Nodes[i].Level = -1; | ||
1104 : | dQ = Lev0 + AC; | ||
1105 : | } else { | ||
1106 : | Nodes[i].Level = 1; | ||
1107 : | dQ = Lev0 - AC; | ||
1108 : | } | ||
1109 : | chl | 1.21.2.11 | Cost0 = Lambda*dQ*dQ; |
1110 : | |||
1111 : | chl | 1.21.2.10 | Nodes[i].Run = 1; |
1112 : | Best_Cost = (Code_Len20[0]<<16) + Run_Costs[i-1]+Cost0; | ||
1113 : | for(Run=i-Run_Start; Run>0; --Run) | ||
1114 : | { | ||
1115 : | const uint32_t Cost_Base = Cost0 + Run_Costs[i-Run]; | ||
1116 : | const uint32_t Cost = Cost_Base + (Code_Len20[Run-1]<<16); | ||
1117 : | chl | 1.21.2.11 | const uint32_t lCost = Cost_Base + (Code_Len24[Run-1]<<16); |
1118 : | |||
1119 : | edgomez | 1.21.2.13 | /* |
1120 : | * TODO: what about tie-breaks? Should we favor short runs or | ||
1121 : | * long runs? Although the error is the same, it would not be | ||
1122 : | * spread the same way along high and low frequencies... | ||
1123 : | */ | ||
1124 : | chl | 1.21.2.11 | if (Cost<Best_Cost) { |
1125 : | chl | 1.21.2.10 | Best_Cost = Cost; |
1126 : | Nodes[i].Run = Run; | ||
1127 : | } | ||
1128 : | chl | 1.21.2.11 | |
1129 : | if (lCost<Last_Cost) { | ||
1130 : | chl | 1.21.2.10 | Last_Cost = lCost; |
1131 : | Last.Run = Run; | ||
1132 : | Last_Node = i; | ||
1133 : | } | ||
1134 : | } | ||
1135 : | chl | 1.21.2.11 | if (Last_Node==i) |
1136 : | Last.Level = Nodes[i].Level; | ||
1137 : | chl | 1.21.2.10 | |
1138 : | if (DBG==1) { | ||
1139 : | Run_Costs[i] = Best_Cost; | ||
1140 : | printf( "Costs #%2d: ", i); | ||
1141 : | for(j=-1;j<=Non_Zero;++j) { | ||
1142 : | if (j==Run_Start) printf( " %3.0d|", Run_Costs[j]>>12 ); | ||
1143 : | else if (j>Run_Start && j<i) printf( " %3.0d|", Run_Costs[j]>>12 ); | ||
1144 : | else if (j==i) printf( "(%3.0d)", Run_Costs[j]>>12 ); | ||
1145 : | else printf( " - |" ); | ||
1146 : | } | ||
1147 : | printf( "<%3.0d %2d %d>", Min_Cost>>12, Nodes[i].Level, Nodes[i].Run ); | ||
1148 : | printf( " Last:#%2d {%3.0d %2d %d}", Last_Node, Last_Cost>>12, Last.Level, Last.Run ); | ||
1149 : | printf( " AC:%3.0d Dist0:%3d Dist(%d)=%d", AC, Dist0>>12, Nodes[i].Level, Cost0>>12 ); | ||
1150 : | printf( "\n" ); | ||
1151 : | } | ||
1152 : | } | ||
1153 : | edgomez | 1.21.2.13 | else /* "big" levels */ |
1154 : | chl | 1.21.2.10 | { |
1155 : | const uint8_t *Tbl_L1, *Tbl_L2, *Tbl_L1_Last, *Tbl_L2_Last; | ||
1156 : | int Level2; | ||
1157 : | int dQ1, dQ2; | ||
1158 : | int Run; | ||
1159 : | chl | 1.21.2.11 | uint32_t Dist1,Dist2; |
1160 : | int dDist21; | ||
1161 : | |||
1162 : | chl | 1.21.2.10 | if (Level1>1) { |
1163 : | dQ1 = Level1*Mult-AC + Bias; | ||
1164 : | dQ2 = dQ1 - Mult; | ||
1165 : | Level2 = Level1-1; | ||
1166 : | Tbl_L1 = (Level1<=24) ? B16_17_Code_Len[Level1-1] : Code_Len0; | ||
1167 : | Tbl_L2 = (Level2<=24) ? B16_17_Code_Len[Level2-1] : Code_Len0; | ||
1168 : | Tbl_L1_Last = (Level1<=6) ? B16_17_Code_Len_Last[Level1-1] : Code_Len0; | ||
1169 : | Tbl_L2_Last = (Level2<=6) ? B16_17_Code_Len_Last[Level2-1] : Code_Len0; | ||
1170 : | edgomez | 1.21.2.13 | } else { /* Level1<-1 */ |
1171 : | chl | 1.21.2.10 | dQ1 = Level1*Mult-AC - Bias; |
1172 : | dQ2 = dQ1 + Mult; | ||
1173 : | Level2 = Level1 + 1; | ||
1174 : | Tbl_L1 = (Level1>=-24) ? B16_17_Code_Len[Level1^-1] : Code_Len0; | ||
1175 : | Tbl_L2 = (Level2>=-24) ? B16_17_Code_Len[Level2^-1] : Code_Len0; | ||
1176 : | Tbl_L1_Last = (Level1>=- 6) ? B16_17_Code_Len_Last[Level1^-1] : Code_Len0; | ||
1177 : | Tbl_L2_Last = (Level2>=- 6) ? B16_17_Code_Len_Last[Level2^-1] : Code_Len0; | ||
1178 : | } | ||
1179 : | chl | 1.21.2.11 | Dist1 = Lambda*dQ1*dQ1; |
1180 : | Dist2 = Lambda*dQ2*dQ2; | ||
1181 : | dDist21 = Dist2-Dist1; | ||
1182 : | chl | 1.21.2.10 | |
1183 : | for(Run=i-Run_Start; Run>0; --Run) | ||
1184 : | { | ||
1185 : | const uint32_t Cost_Base = Dist1 + Run_Costs[i-Run]; | ||
1186 : | chl | 1.21.2.11 | uint32_t Cost1, Cost2; |
1187 : | int bLevel; | ||
1188 : | chl | 1.21.2.10 | |
1189 : | edgomez | 1.21.2.13 | /* |
1190 : | * for sub-optimal (but slightly worth it, speed-wise) search, uncomment the following: | ||
1191 : | * if (Cost_Base>=Best_Cost) continue; | ||
1192 : | */ | ||
1193 : | chl | 1.21.2.10 | Cost1 = Cost_Base + (Tbl_L1[Run-1]<<16); |
1194 : | Cost2 = Cost_Base + (Tbl_L2[Run-1]<<16) + dDist21; | ||
1195 : | |||
1196 : | chl | 1.21.2.11 | if (Cost2<Cost1) { |
1197 : | Cost1 = Cost2; | ||
1198 : | bLevel = Level2; | ||
1199 : | } else | ||
1200 : | bLevel = Level1; | ||
1201 : | chl | 1.21.2.10 | |
1202 : | chl | 1.21.2.11 | if (Cost1<Best_Cost) { |
1203 : | chl | 1.21.2.10 | Best_Cost = Cost1; |
1204 : | Nodes[i].Run = Run; | ||
1205 : | Nodes[i].Level = bLevel; | ||
1206 : | } | ||
1207 : | |||
1208 : | Cost1 = Cost_Base + (Tbl_L1_Last[Run-1]<<16); | ||
1209 : | Cost2 = Cost_Base + (Tbl_L2_Last[Run-1]<<16) + dDist21; | ||
1210 : | |||
1211 : | chl | 1.21.2.11 | if (Cost2<Cost1) { |
1212 : | Cost1 = Cost2; | ||
1213 : | bLevel = Level2; | ||
1214 : | } else | ||
1215 : | bLevel = Level1; | ||
1216 : | |||
1217 : | if (Cost1<Last_Cost) { | ||
1218 : | chl | 1.21.2.10 | Last_Cost = Cost1; |
1219 : | Last.Run = Run; | ||
1220 : | Last.Level = bLevel; | ||
1221 : | Last_Node = i; | ||
1222 : | } | ||
1223 : | edgomez | 1.21.2.13 | } /* end of "for Run" */ |
1224 : | chl | 1.21.2.10 | |
1225 : | if (DBG==1) { | ||
1226 : | Run_Costs[i] = Best_Cost; | ||
1227 : | printf( "Costs #%2d: ", i); | ||
1228 : | for(j=-1;j<=Non_Zero;++j) { | ||
1229 : | if (j==Run_Start) printf( " %3.0d|", Run_Costs[j]>>12 ); | ||
1230 : | else if (j>Run_Start && j<i) printf( " %3.0d|", Run_Costs[j]>>12 ); | ||
1231 : | else if (j==i) printf( "(%3.0d)", Run_Costs[j]>>12 ); | ||
1232 : | else printf( " - |" ); | ||
1233 : | } | ||
1234 : | printf( "<%3.0d %2d %d>", Min_Cost>>12, Nodes[i].Level, Nodes[i].Run ); | ||
1235 : | printf( " Last:#%2d {%3.0d %2d %d}", Last_Node, Last_Cost>>12, Last.Level, Last.Run ); | ||
1236 : | printf( " AC:%3.0d Dist0:%3d Dist(%2d):%3d Dist(%2d):%3d", AC, Dist0>>12, Level1, Dist1>>12, Level2, Dist2>>12 ); | ||
1237 : | printf( "\n" ); | ||
1238 : | } | ||
1239 : | } | ||
1240 : | |||
1241 : | Run_Costs[i] = Best_Cost; | ||
1242 : | |||
1243 : | if (Best_Cost < Min_Cost + Dist0) { | ||
1244 : | Min_Cost = Best_Cost; | ||
1245 : | Run_Start = i; | ||
1246 : | } | ||
1247 : | else | ||
1248 : | { | ||
1249 : | edgomez | 1.21.2.13 | /* |
1250 : | * as noticed by Michael Niedermayer (michaelni at gmx.at), there's | ||
1251 : | * a code shorter by 1 bit for a larger run (!), same level. We give | ||
1252 : | * it a chance by not moving the left barrier too much. | ||
1253 : | */ | ||
1254 : | chl | 1.21.2.11 | |
1255 : | chl | 1.21.2.10 | while( Run_Costs[Run_Start]>Min_Cost+(1<<16) ) |
1256 : | Run_Start++; | ||
1257 : | |||
1258 : | edgomez | 1.21.2.13 | /* spread on preceding coeffs the cost incurred by skipping this one */ |
1259 : | chl | 1.21.2.10 | for(j=Run_Start; j<i; ++j) Run_Costs[j] += Dist0; |
1260 : | Min_Cost += Dist0; | ||
1261 : | } | ||
1262 : | } | ||
1263 : | |||
1264 : | if (DBG) { | ||
1265 : | Last_Cost = Evaluate_Cost(Out,Mult,Bias, Zigzag,Non_Zero, Lambda); | ||
1266 : | if (DBG==1) { | ||
1267 : | printf( "=> " ); | ||
1268 : | for(i=0; i<=Non_Zero; ++i) printf( "[%3.0d] ", Out[Zigzag[i]] ); | ||
1269 : | printf( "\n" ); | ||
1270 : | } | ||
1271 : | } | ||
1272 : | |||
1273 : | if (Last_Node<0) | ||
1274 : | return -1; | ||
1275 : | |||
1276 : | edgomez | 1.21.2.13 | /* reconstruct optimal sequence backward with surviving paths */ |
1277 : | chl | 1.21.2.11 | memset(Out, 0x00, 64*sizeof(*Out)); |
1278 : | chl | 1.21.2.10 | Out[Zigzag[Last_Node]] = Last.Level; |
1279 : | i = Last_Node - Last.Run; | ||
1280 : | while(i>=0) { | ||
1281 : | Out[Zigzag[i]] = Nodes[i].Level; | ||
1282 : | i -= Nodes[i].Run; | ||
1283 : | } | ||
1284 : | |||
1285 : | if (DBG) { | ||
1286 : | uint32_t Cost = Evaluate_Cost(Out,Mult,Bias, Zigzag,Non_Zero, Lambda); | ||
1287 : | if (DBG==1) { | ||
1288 : | printf( "<= " ); | ||
1289 : | for(i=0; i<=Last_Node; ++i) printf( "[%3.0d] ", Out[Zigzag[i]] ); | ||
1290 : | printf( "\n--------------------------------\n" ); | ||
1291 : | } | ||
1292 : | if (Cost>Last_Cost) printf( "!!! %u > %u\n", Cost, Last_Cost ); | ||
1293 : | } | ||
1294 : | return Last_Node; | ||
1295 : | } | ||
1296 : | |||
1297 : | #undef DBG | ||
1298 : | chl | 1.21.2.11 | |
1299 : | #endif |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |