Parent Directory | Revision Log
Revision 1.21.2.10 - (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 : | chl | 1.21.2.10 | * $Id: mbtransquant.c,v 1.21.2.9 2003/04/27 19:47:48 chl Exp $ |
25 : | edgomez | 1.21.2.6 | * |
26 : | ****************************************************************************/ | ||
27 : | Isibaar | 1.1 | |
28 : | edgomez | 1.3 | #include <string.h> |
29 : | edgomez | 1.21.2.7 | #include <stdlib.h> |
30 : | edgomez | 1.3 | |
31 : | Isibaar | 1.1 | #include "../portab.h" |
32 : | #include "mbfunctions.h" | ||
33 : | |||
34 : | #include "../global.h" | ||
35 : | #include "mem_transfer.h" | ||
36 : | #include "timer.h" | ||
37 : | chl | 1.21.2.9 | #include "../bitstream/mbcoding.h" |
38 : | chl | 1.21.2.10 | #include "../bitstream/zigzag.h" |
39 : | Isibaar | 1.1 | #include "../dct/fdct.h" |
40 : | #include "../dct/idct.h" | ||
41 : | #include "../quant/quant_mpeg4.h" | ||
42 : | #include "../quant/quant_h263.h" | ||
43 : | #include "../encoder.h" | ||
44 : | |||
45 : | edgomez | 1.21 | #include "../image/reduced.h" |
46 : | Isibaar | 1.1 | |
47 : | edgomez | 1.21 | MBFIELDTEST_PTR MBFieldTest; |
48 : | Isibaar | 1.1 | |
49 : | edgomez | 1.21.2.6 | /* |
50 : | * Skip blocks having a coefficient sum below this value. This value will be | ||
51 : | * corrected according to the MB quantizer to avoid artifacts for quant==1 | ||
52 : | */ | ||
53 : | #define PVOP_TOOSMALL_LIMIT 1 | ||
54 : | #define BVOP_TOOSMALL_LIMIT 3 | ||
55 : | |||
56 : | /***************************************************************************** | ||
57 : | * Local functions | ||
58 : | ****************************************************************************/ | ||
59 : | Isibaar | 1.1 | |
60 : | edgomez | 1.21.2.6 | /* permute block and return field dct choice */ |
61 : | static __inline uint32_t | ||
62 : | MBDecideFieldDCT(int16_t data[6 * 64]) | ||
63 : | Isibaar | 1.1 | { |
64 : | edgomez | 1.21.2.6 | uint32_t field = MBFieldTest(data); |
65 : | edgomez | 1.3 | |
66 : | edgomez | 1.21.2.6 | if (field) |
67 : | MBFrameToField(data); | ||
68 : | edgomez | 1.21 | |
69 : | edgomez | 1.21.2.6 | return field; |
70 : | } | ||
71 : | h | 1.2 | |
72 : | edgomez | 1.21.2.6 | /* Performs Forward DCT on all blocks */ |
73 : | static __inline void | ||
74 : | syskin | 1.21.2.8 | MBfDCT(const MBParam * const pParam, |
75 : | const FRAMEINFO * const frame, | ||
76 : | MACROBLOCK * const pMB, | ||
77 : | edgomez | 1.21.2.6 | uint32_t x_pos, |
78 : | uint32_t y_pos, | ||
79 : | int16_t data[6 * 64]) | ||
80 : | syskin | 1.21.2.8 | { |
81 : | edgomez | 1.21.2.6 | /* Handles interlacing */ |
82 : | h | 1.2 | start_timer(); |
83 : | pMB->field_dct = 0; | ||
84 : | edgomez | 1.21.2.5 | if ((frame->vol_flags & XVID_VOL_INTERLACING) && |
85 : | h | 1.11 | (x_pos>0) && (x_pos<pParam->mb_width-1) && |
86 : | (y_pos>0) && (y_pos<pParam->mb_height-1)) { | ||
87 : | h | 1.2 | pMB->field_dct = MBDecideFieldDCT(data); |
88 : | } | ||
89 : | stop_interlacing_timer(); | ||
90 : | |||
91 : | edgomez | 1.21.2.6 | /* Perform DCT */ |
92 : | start_timer(); | ||
93 : | fdct(&data[0 * 64]); | ||
94 : | fdct(&data[1 * 64]); | ||
95 : | fdct(&data[2 * 64]); | ||
96 : | fdct(&data[3 * 64]); | ||
97 : | fdct(&data[4 * 64]); | ||
98 : | fdct(&data[5 * 64]); | ||
99 : | stop_dct_timer(); | ||
100 : | } | ||
101 : | |||
102 : | /* Performs Inverse DCT on all blocks */ | ||
103 : | static __inline void | ||
104 : | MBiDCT(int16_t data[6 * 64], | ||
105 : | const uint8_t cbp) | ||
106 : | { | ||
107 : | start_timer(); | ||
108 : | if(cbp & (1 << (5 - 0))) idct(&data[0 * 64]); | ||
109 : | if(cbp & (1 << (5 - 1))) idct(&data[1 * 64]); | ||
110 : | if(cbp & (1 << (5 - 2))) idct(&data[2 * 64]); | ||
111 : | if(cbp & (1 << (5 - 3))) idct(&data[3 * 64]); | ||
112 : | if(cbp & (1 << (5 - 4))) idct(&data[4 * 64]); | ||
113 : | if(cbp & (1 << (5 - 5))) idct(&data[5 * 64]); | ||
114 : | stop_idct_timer(); | ||
115 : | } | ||
116 : | |||
117 : | /* Quantize all blocks -- Intra mode */ | ||
118 : | static __inline void | ||
119 : | MBQuantIntra(const MBParam * pParam, | ||
120 : | chl | 1.21.2.9 | const FRAMEINFO * const frame, |
121 : | edgomez | 1.21.2.6 | const MACROBLOCK * pMB, |
122 : | syskin | 1.21.2.8 | int16_t qcoeff[6 * 64], |
123 : | edgomez | 1.21.2.6 | int16_t data[6*64]) |
124 : | { | ||
125 : | int i; | ||
126 : | |||
127 : | for (i = 0; i < 6; i++) { | ||
128 : | uint32_t iDcScaler = get_dc_scaler(pMB->quant, i < 4); | ||
129 : | |||
130 : | /* Quantize the block */ | ||
131 : | start_timer(); | ||
132 : | chl | 1.21.2.9 | if (!(pParam->vol_flags & XVID_VOL_MPEGQUANT)) { |
133 : | edgomez | 1.21.2.6 | quant_intra(&data[i * 64], &qcoeff[i * 64], pMB->quant, iDcScaler); |
134 : | chl | 1.21.2.9 | } else { |
135 : | edgomez | 1.21.2.6 | quant4_intra(&data[i * 64], &qcoeff[i * 64], pMB->quant, iDcScaler); |
136 : | chl | 1.21.2.9 | } |
137 : | edgomez | 1.21.2.6 | stop_quant_timer(); |
138 : | } | ||
139 : | } | ||
140 : | |||
141 : | /* DeQuantize all blocks -- Intra mode */ | ||
142 : | static __inline void | ||
143 : | MBDeQuantIntra(const MBParam * pParam, | ||
144 : | const int iQuant, | ||
145 : | int16_t qcoeff[6 * 64], | ||
146 : | int16_t data[6*64]) | ||
147 : | { | ||
148 : | int i; | ||
149 : | |||
150 : | edgomez | 1.7 | for (i = 0; i < 6; i++) { |
151 : | Isibaar | 1.1 | uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
152 : | |||
153 : | start_timer(); | ||
154 : | edgomez | 1.21.2.6 | if (!(pParam->vol_flags & XVID_VOL_MPEGQUANT)) |
155 : | dequant_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler); | ||
156 : | else | ||
157 : | dequant4_intra(&qcoeff[i * 64], &data[i * 64], iQuant, iDcScaler); | ||
158 : | stop_iquant_timer(); | ||
159 : | } | ||
160 : | } | ||
161 : | Isibaar | 1.1 | |
162 : | chl | 1.21.2.10 | |
163 : | static int | ||
164 : | dct_quantize_trellis_h263_c(int16_t *const Out, const int16_t *const In, int Q, const uint16_t * const Zigzag, int Non_Zero); | ||
165 : | |||
166 : | static int | ||
167 : | dct_quantize_trellis_mpeg_c(int16_t *const Out, const int16_t *const In, int Q, const uint16_t * const Zigzag, int Non_Zero); | ||
168 : | |||
169 : | |||
170 : | edgomez | 1.21.2.6 | /* Quantize all blocks -- Inter mode */ |
171 : | static __inline uint8_t | ||
172 : | MBQuantInter(const MBParam * pParam, | ||
173 : | chl | 1.21.2.9 | const FRAMEINFO * const frame, |
174 : | edgomez | 1.21.2.6 | const MACROBLOCK * pMB, |
175 : | int16_t data[6 * 64], | ||
176 : | int16_t qcoeff[6 * 64], | ||
177 : | int bvop, | ||
178 : | int limit) | ||
179 : | { | ||
180 : | |||
181 : | int i; | ||
182 : | uint8_t cbp = 0; | ||
183 : | int sum; | ||
184 : | int code_block; | ||
185 : | |||
186 : | for (i = 0; i < 6; i++) { | ||
187 : | syskin | 1.21.2.8 | |
188 : | edgomez | 1.21.2.6 | /* Quantize the block */ |
189 : | start_timer(); | ||
190 : | chl | 1.21.2.9 | if (!(pParam->vol_flags & XVID_VOL_MPEGQUANT)) { |
191 : | sum = quant_inter(&qcoeff[i*64], &data[i*64], pMB->quant); | ||
192 : | if ( (sum) && (frame->vop_flags & XVID_VOP_TRELLISQUANT) ) { | ||
193 : | 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; |
194 : | chl | 1.21.2.9 | limit = 1; |
195 : | } | ||
196 : | } else { | ||
197 : | edgomez | 1.21.2.6 | sum = quant4_inter(&qcoeff[i * 64], &data[i * 64], pMB->quant); |
198 : | chl | 1.21.2.9 | // if ( (sum) && (frame->vop_flags & XVID_VOP_TRELLISQUANT) ) |
199 : | chl | 1.21.2.10 | // sum = dct_quantize_trellis_mpeg_c (&qcoeff[i*64], &data[i*64], pMB->quant)+1; |
200 : | chl | 1.21.2.9 | } |
201 : | edgomez | 1.21.2.6 | stop_quant_timer(); |
202 : | |||
203 : | /* | ||
204 : | * We code the block if the sum is higher than the limit and if the first | ||
205 : | * two AC coefficients in zig zag order are not zero. | ||
206 : | */ | ||
207 : | code_block = 0; | ||
208 : | if ((sum >= limit) || (qcoeff[i*64+1] != 0) || (qcoeff[i*64+8] != 0)) { | ||
209 : | code_block = 1; | ||
210 : | edgomez | 1.7 | } else { |
211 : | edgomez | 1.21 | |
212 : | edgomez | 1.21.2.6 | if (bvop && (pMB->mode == MODE_DIRECT || pMB->mode == MODE_DIRECT_NO4V)) { |
213 : | /* dark blocks prevention for direct mode */ | ||
214 : | if ((qcoeff[i*64] < -1) || (qcoeff[i*64] > 0)) | ||
215 : | code_block = 1; | ||
216 : | edgomez | 1.21 | } else { |
217 : | edgomez | 1.21.2.6 | /* not direct mode */ |
218 : | if (qcoeff[i*64] != 0) | ||
219 : | code_block = 1; | ||
220 : | edgomez | 1.21 | } |
221 : | } | ||
222 : | edgomez | 1.21.2.6 | |
223 : | /* Set the corresponding cbp bit */ | ||
224 : | cbp |= code_block << (5 - i); | ||
225 : | edgomez | 1.21 | } |
226 : | |||
227 : | edgomez | 1.21.2.6 | return(cbp); |
228 : | } | ||
229 : | Isibaar | 1.1 | |
230 : | edgomez | 1.21.2.6 | /* DeQuantize all blocks -- Inter mode */ |
231 : | syskin | 1.21.2.8 | static __inline void |
232 : | edgomez | 1.21.2.6 | MBDeQuantInter(const MBParam * pParam, |
233 : | const int iQuant, | ||
234 : | int16_t data[6 * 64], | ||
235 : | int16_t qcoeff[6 * 64], | ||
236 : | const uint8_t cbp) | ||
237 : | { | ||
238 : | int i; | ||
239 : | |||
240 : | for (i = 0; i < 6; i++) { | ||
241 : | syskin | 1.21.2.8 | if (cbp & (1 << (5 - i))) { |
242 : | edgomez | 1.21.2.6 | start_timer(); |
243 : | if (!(pParam->vol_flags & XVID_VOL_MPEGQUANT)) | ||
244 : | dequant_inter(&data[i * 64], &qcoeff[i * 64], iQuant); | ||
245 : | else | ||
246 : | dequant4_inter(&data[i * 64], &qcoeff[i * 64], iQuant); | ||
247 : | stop_iquant_timer(); | ||
248 : | edgomez | 1.21 | } |
249 : | h | 1.2 | } |
250 : | Isibaar | 1.1 | } |
251 : | |||
252 : | edgomez | 1.21.2.6 | typedef void (transfer_operation_8to16_t) (int16_t *Dst, const uint8_t *Src, int BpS); |
253 : | typedef void (transfer_operation_16to8_t) (uint8_t *Dst, const int16_t *Src, int BpS); | ||
254 : | Isibaar | 1.1 | |
255 : | edgomez | 1.3 | |
256 : | edgomez | 1.21.2.6 | static __inline void |
257 : | syskin | 1.21.2.8 | MBTrans8to16(const MBParam * const pParam, |
258 : | const FRAMEINFO * const frame, | ||
259 : | const MACROBLOCK * const pMB, | ||
260 : | edgomez | 1.21.2.6 | const uint32_t x_pos, |
261 : | const uint32_t y_pos, | ||
262 : | int16_t data[6 * 64]) | ||
263 : | { | ||
264 : | h | 1.4 | uint32_t stride = pParam->edged_width; |
265 : | uint32_t stride2 = stride / 2; | ||
266 : | edgomez | 1.21.2.6 | uint32_t next_block = stride * 8; |
267 : | syskin | 1.21.2.8 | int32_t cst; |
268 : | Isibaar | 1.1 | uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
269 : | syskin | 1.21.2.8 | const IMAGE * const pCurrent = &frame->image; |
270 : | edgomez | 1.21.2.6 | transfer_operation_8to16_t *transfer_op = NULL; |
271 : | edgomez | 1.7 | |
272 : | edgomez | 1.21.2.6 | if ((frame->vop_flags & XVID_VOP_REDUCED)) { |
273 : | |||
274 : | /* Image pointers */ | ||
275 : | pY_Cur = pCurrent->y + (y_pos << 5) * stride + (x_pos << 5); | ||
276 : | edgomez | 1.21 | pU_Cur = pCurrent->u + (y_pos << 4) * stride2 + (x_pos << 4); |
277 : | pV_Cur = pCurrent->v + (y_pos << 4) * stride2 + (x_pos << 4); | ||
278 : | edgomez | 1.21.2.6 | |
279 : | /* Block size */ | ||
280 : | cst = 16; | ||
281 : | |||
282 : | /* Operation function */ | ||
283 : | transfer_op = (transfer_operation_8to16_t*)filter_18x18_to_8x8; | ||
284 : | } else { | ||
285 : | |||
286 : | /* Image pointers */ | ||
287 : | pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4); | ||
288 : | edgomez | 1.21 | pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3); |
289 : | pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3); | ||
290 : | edgomez | 1.21.2.6 | |
291 : | /* Block size */ | ||
292 : | cst = 8; | ||
293 : | |||
294 : | /* Operation function */ | ||
295 : | transfer_op = (transfer_operation_8to16_t*)transfer_8to16copy; | ||
296 : | edgomez | 1.21 | } |
297 : | Isibaar | 1.1 | |
298 : | edgomez | 1.21.2.6 | /* Do the transfer */ |
299 : | h | 1.2 | start_timer(); |
300 : | edgomez | 1.21.2.6 | transfer_op(&data[0 * 64], pY_Cur, stride); |
301 : | transfer_op(&data[1 * 64], pY_Cur + cst, stride); | ||
302 : | transfer_op(&data[2 * 64], pY_Cur + next_block, stride); | ||
303 : | transfer_op(&data[3 * 64], pY_Cur + next_block + cst, stride); | ||
304 : | transfer_op(&data[4 * 64], pU_Cur, stride2); | ||
305 : | transfer_op(&data[5 * 64], pV_Cur, stride2); | ||
306 : | stop_transfer_timer(); | ||
307 : | syskin | 1.21.2.8 | } |
308 : | h | 1.2 | |
309 : | edgomez | 1.21.2.6 | static __inline void |
310 : | syskin | 1.21.2.8 | MBTrans16to8(const MBParam * const pParam, |
311 : | const FRAMEINFO * const frame, | ||
312 : | const MACROBLOCK * const pMB, | ||
313 : | edgomez | 1.21.2.6 | const uint32_t x_pos, |
314 : | const uint32_t y_pos, | ||
315 : | int16_t data[6 * 64], | ||
316 : | const uint32_t add, | ||
317 : | const uint8_t cbp) | ||
318 : | { | ||
319 : | uint8_t *pY_Cur, *pU_Cur, *pV_Cur; | ||
320 : | uint32_t stride = pParam->edged_width; | ||
321 : | uint32_t stride2 = stride / 2; | ||
322 : | uint32_t next_block = stride * 8; | ||
323 : | syskin | 1.21.2.8 | uint32_t cst; |
324 : | const IMAGE * const pCurrent = &frame->image; | ||
325 : | edgomez | 1.21.2.6 | transfer_operation_16to8_t *transfer_op = NULL; |
326 : | edgomez | 1.21 | |
327 : | edgomez | 1.21.2.6 | if (pMB->field_dct) { |
328 : | next_block = stride; | ||
329 : | stride *= 2; | ||
330 : | } | ||
331 : | Isibaar | 1.1 | |
332 : | edgomez | 1.21.2.6 | if ((frame->vop_flags & XVID_VOP_REDUCED)) { |
333 : | |||
334 : | /* Image pointers */ | ||
335 : | pY_Cur = pCurrent->y + (y_pos << 5) * stride + (x_pos << 5); | ||
336 : | pU_Cur = pCurrent->u + (y_pos << 4) * stride2 + (x_pos << 4); | ||
337 : | pV_Cur = pCurrent->v + (y_pos << 4) * stride2 + (x_pos << 4); | ||
338 : | Isibaar | 1.1 | |
339 : | edgomez | 1.21.2.6 | /* Block size */ |
340 : | cst = 16; | ||
341 : | Isibaar | 1.1 | |
342 : | edgomez | 1.21.2.6 | /* Operation function */ |
343 : | if(add) | ||
344 : | transfer_op = (transfer_operation_16to8_t*)add_upsampled_8x8_16to8; | ||
345 : | else | ||
346 : | transfer_op = (transfer_operation_16to8_t*)copy_upsampled_8x8_16to8; | ||
347 : | } else { | ||
348 : | Isibaar | 1.1 | |
349 : | edgomez | 1.21.2.6 | /* Image pointers */ |
350 : | pY_Cur = pCurrent->y + (y_pos << 4) * stride + (x_pos << 4); | ||
351 : | pU_Cur = pCurrent->u + (y_pos << 3) * stride2 + (x_pos << 3); | ||
352 : | pV_Cur = pCurrent->v + (y_pos << 3) * stride2 + (x_pos << 3); | ||
353 : | Isibaar | 1.1 | |
354 : | edgomez | 1.21.2.6 | /* Block size */ |
355 : | cst = 8; | ||
356 : | h | 1.2 | |
357 : | edgomez | 1.21.2.6 | /* Operation function */ |
358 : | if(add) | ||
359 : | transfer_op = (transfer_operation_16to8_t*)transfer_16to8add; | ||
360 : | else | ||
361 : | transfer_op = (transfer_operation_16to8_t*)transfer_16to8copy; | ||
362 : | h | 1.2 | } |
363 : | |||
364 : | edgomez | 1.21.2.6 | /* Do the operation */ |
365 : | h | 1.2 | start_timer(); |
366 : | edgomez | 1.21.2.6 | if (cbp&32) transfer_op(pY_Cur, &data[0 * 64], stride); |
367 : | if (cbp&16) transfer_op(pY_Cur + cst, &data[1 * 64], stride); | ||
368 : | if (cbp& 8) transfer_op(pY_Cur + next_block, &data[2 * 64], stride); | ||
369 : | if (cbp& 4) transfer_op(pY_Cur + next_block + cst, &data[3 * 64], stride); | ||
370 : | if (cbp& 2) transfer_op(pU_Cur, &data[4 * 64], stride2); | ||
371 : | if (cbp& 1) transfer_op(pV_Cur, &data[5 * 64], stride2); | ||
372 : | h | 1.2 | stop_transfer_timer(); |
373 : | } | ||
374 : | |||
375 : | edgomez | 1.21.2.6 | /***************************************************************************** |
376 : | * Module functions | ||
377 : | ****************************************************************************/ | ||
378 : | |||
379 : | syskin | 1.21.2.8 | void |
380 : | MBTransQuantIntra(const MBParam * const pParam, | ||
381 : | const FRAMEINFO * const frame, | ||
382 : | MACROBLOCK * const pMB, | ||
383 : | chl | 1.8 | const uint32_t x_pos, |
384 : | const uint32_t y_pos, | ||
385 : | int16_t data[6 * 64], | ||
386 : | int16_t qcoeff[6 * 64]) | ||
387 : | { | ||
388 : | |||
389 : | edgomez | 1.21.2.6 | /* Transfer data */ |
390 : | MBTrans8to16(pParam, frame, pMB, x_pos, y_pos, data); | ||
391 : | chl | 1.8 | |
392 : | edgomez | 1.21.2.6 | /* Perform DCT (and field decision) */ |
393 : | MBfDCT(pParam, frame, pMB, x_pos, y_pos, data); | ||
394 : | chl | 1.8 | |
395 : | edgomez | 1.21.2.6 | /* Quantize the block */ |
396 : | chl | 1.21.2.9 | MBQuantIntra(pParam, frame, pMB, data, qcoeff); |
397 : | edgomez | 1.21.2.6 | |
398 : | /* DeQuantize the block */ | ||
399 : | MBDeQuantIntra(pParam, pMB->quant, data, qcoeff); | ||
400 : | |||
401 : | /* Perform inverse DCT*/ | ||
402 : | MBiDCT(data, 0x3F); | ||
403 : | |||
404 : | /* Transfer back the data -- Don't add data */ | ||
405 : | MBTrans16to8(pParam, frame, pMB, x_pos, y_pos, data, 0, 0x3F); | ||
406 : | chl | 1.8 | } |
407 : | |||
408 : | edgomez | 1.21.2.6 | |
409 : | chl | 1.8 | uint8_t |
410 : | syskin | 1.21.2.8 | MBTransQuantInter(const MBParam * const pParam, |
411 : | const FRAMEINFO * const frame, | ||
412 : | MACROBLOCK * const pMB, | ||
413 : | edgomez | 1.21.2.2 | const uint32_t x_pos, |
414 : | const uint32_t y_pos, | ||
415 : | chl | 1.8 | int16_t data[6 * 64], |
416 : | int16_t qcoeff[6 * 64]) | ||
417 : | { | ||
418 : | uint8_t cbp; | ||
419 : | edgomez | 1.21.2.6 | uint32_t limit; |
420 : | chl | 1.8 | |
421 : | edgomez | 1.21.2.2 | /* |
422 : | edgomez | 1.21.2.6 | * There is no MBTrans8to16 for Inter block, that's done in motion compensation |
423 : | * already | ||
424 : | edgomez | 1.21.2.2 | */ |
425 : | |||
426 : | edgomez | 1.21.2.6 | /* Perform DCT (and field decision) */ |
427 : | MBfDCT(pParam, frame, pMB, x_pos, y_pos, data); | ||
428 : | chl | 1.8 | |
429 : | edgomez | 1.21.2.6 | /* Set the limit threshold */ |
430 : | limit = PVOP_TOOSMALL_LIMIT + ((pMB->quant == 1)? 1 : 0); | ||
431 : | chl | 1.8 | |
432 : | edgomez | 1.21.2.6 | /* Quantize the block */ |
433 : | chl | 1.21.2.9 | cbp = MBQuantInter(pParam, frame, pMB, data, qcoeff, 0, limit); |
434 : | chl | 1.8 | |
435 : | edgomez | 1.21.2.6 | /* DeQuantize the block */ |
436 : | MBDeQuantInter(pParam, pMB->quant, data, qcoeff, cbp); | ||
437 : | chl | 1.8 | |
438 : | edgomez | 1.21.2.6 | /* Perform inverse DCT*/ |
439 : | MBiDCT(data, cbp); | ||
440 : | chl | 1.8 | |
441 : | edgomez | 1.21.2.6 | /* Transfer back the data -- Add the data */ |
442 : | MBTrans16to8(pParam, frame, pMB, x_pos, y_pos, data, 1, cbp); | ||
443 : | syskin | 1.21.2.8 | |
444 : | edgomez | 1.21.2.6 | return(cbp); |
445 : | chl | 1.8 | } |
446 : | |||
447 : | edgomez | 1.21.2.6 | uint8_t |
448 : | MBTransQuantInterBVOP(const MBParam * pParam, | ||
449 : | chl | 1.8 | FRAMEINFO * frame, |
450 : | MACROBLOCK * pMB, | ||
451 : | const uint32_t x_pos, | ||
452 : | const uint32_t y_pos, | ||
453 : | int16_t data[6 * 64], | ||
454 : | edgomez | 1.21.2.6 | int16_t qcoeff[6 * 64]) |
455 : | chl | 1.8 | { |
456 : | edgomez | 1.21.2.6 | uint8_t cbp; |
457 : | uint32_t limit; | ||
458 : | syskin | 1.21.2.8 | |
459 : | edgomez | 1.21.2.6 | /* |
460 : | * There is no MBTrans8to16 for Inter block, that's done in motion compensation | ||
461 : | * already | ||
462 : | */ | ||
463 : | chl | 1.8 | |
464 : | edgomez | 1.21.2.6 | /* Perform DCT (and field decision) */ |
465 : | MBfDCT(pParam, frame, pMB, x_pos, y_pos, data); | ||
466 : | chl | 1.8 | |
467 : | edgomez | 1.21.2.6 | /* Set the limit threshold */ |
468 : | limit = BVOP_TOOSMALL_LIMIT; | ||
469 : | chl | 1.8 | |
470 : | edgomez | 1.21.2.6 | /* Quantize the block */ |
471 : | chl | 1.21.2.9 | cbp = MBQuantInter(pParam, frame, pMB, data, qcoeff, 1, limit); |
472 : | h | 1.2 | |
473 : | edgomez | 1.21.2.6 | /* |
474 : | * History comment: | ||
475 : | * We don't have to DeQuant, iDCT and Transfer back data for B-frames. | ||
476 : | * | ||
477 : | * BUT some plugins require the original frame to be passed so we have | ||
478 : | * to take care of that here | ||
479 : | */ | ||
480 : | if((pParam->plugin_flags & XVID_REQORIGINAL)) { | ||
481 : | h | 1.2 | |
482 : | edgomez | 1.21.2.6 | /* DeQuantize the block */ |
483 : | MBDeQuantInter(pParam, pMB->quant, data, qcoeff, cbp); | ||
484 : | Isibaar | 1.1 | |
485 : | edgomez | 1.21.2.6 | /* Perform inverse DCT*/ |
486 : | MBiDCT(data, cbp); | ||
487 : | edgomez | 1.21 | |
488 : | edgomez | 1.21.2.6 | /* Transfer back the data -- Add the data */ |
489 : | MBTrans16to8(pParam, frame, pMB, x_pos, y_pos, data, 1, cbp); | ||
490 : | edgomez | 1.21 | } |
491 : | |||
492 : | edgomez | 1.21.2.6 | return(cbp); |
493 : | edgomez | 1.21 | } |
494 : | |||
495 : | /* if sum(diff between field lines) < sum(diff between frame lines), use field dct */ | ||
496 : | uint32_t | ||
497 : | MBFieldTest_c(int16_t data[6 * 64]) | ||
498 : | { | ||
499 : | edgomez | 1.7 | const uint8_t blocks[] = |
500 : | { 0 * 64, 0 * 64, 0 * 64, 0 * 64, 2 * 64, 2 * 64, 2 * 64, 2 * 64 }; | ||
501 : | const uint8_t lines[] = { 0, 16, 32, 48, 0, 16, 32, 48 }; | ||
502 : | h | 1.2 | |
503 : | int frame = 0, field = 0; | ||
504 : | int i, j; | ||
505 : | |||
506 : | edgomez | 1.7 | for (i = 0; i < 7; ++i) { |
507 : | for (j = 0; j < 8; ++j) { | ||
508 : | frame += | ||
509 : | edgomez | 1.21.2.7 | abs(data[0 * 64 + (i + 1) * 8 + j] - data[0 * 64 + i * 8 + j]); |
510 : | edgomez | 1.7 | frame += |
511 : | edgomez | 1.21.2.7 | abs(data[1 * 64 + (i + 1) * 8 + j] - data[1 * 64 + i * 8 + j]); |
512 : | edgomez | 1.7 | frame += |
513 : | edgomez | 1.21.2.7 | abs(data[2 * 64 + (i + 1) * 8 + j] - data[2 * 64 + i * 8 + j]); |
514 : | edgomez | 1.7 | frame += |
515 : | edgomez | 1.21.2.7 | abs(data[3 * 64 + (i + 1) * 8 + j] - data[3 * 64 + i * 8 + j]); |
516 : | edgomez | 1.7 | |
517 : | field += | ||
518 : | edgomez | 1.21.2.7 | abs(data[blocks[i + 1] + lines[i + 1] + j] - |
519 : | edgomez | 1.7 | data[blocks[i] + lines[i] + j]); |
520 : | field += | ||
521 : | edgomez | 1.21.2.7 | abs(data[blocks[i + 1] + lines[i + 1] + 8 + j] - |
522 : | edgomez | 1.7 | data[blocks[i] + lines[i] + 8 + j]); |
523 : | field += | ||
524 : | edgomez | 1.21.2.7 | abs(data[blocks[i + 1] + 64 + lines[i + 1] + j] - |
525 : | edgomez | 1.7 | data[blocks[i] + 64 + lines[i] + j]); |
526 : | field += | ||
527 : | edgomez | 1.21.2.7 | abs(data[blocks[i + 1] + 64 + lines[i + 1] + 8 + j] - |
528 : | edgomez | 1.7 | data[blocks[i] + 64 + lines[i] + 8 + j]); |
529 : | Isibaar | 1.1 | } |
530 : | } | ||
531 : | h | 1.2 | |
532 : | edgomez | 1.21 | return (frame >= (field + 350)); |
533 : | h | 1.2 | } |
534 : | |||
535 : | |||
536 : | /* deinterlace Y blocks vertically */ | ||
537 : | |||
538 : | #define MOVLINE(X,Y) memcpy(X, Y, sizeof(tmp)) | ||
539 : | syskin | 1.21.2.8 | #define LINE(X,Y) &data[X*64 + Y*8] |
540 : | h | 1.2 | |
541 : | edgomez | 1.7 | void |
542 : | MBFrameToField(int16_t data[6 * 64]) | ||
543 : | h | 1.2 | { |
544 : | int16_t tmp[8]; | ||
545 : | |||
546 : | /* left blocks */ | ||
547 : | |||
548 : | edgomez | 1.21 | // 1=2, 2=4, 4=8, 8=1 |
549 : | edgomez | 1.7 | MOVLINE(tmp, LINE(0, 1)); |
550 : | MOVLINE(LINE(0, 1), LINE(0, 2)); | ||
551 : | MOVLINE(LINE(0, 2), LINE(0, 4)); | ||
552 : | MOVLINE(LINE(0, 4), LINE(2, 0)); | ||
553 : | MOVLINE(LINE(2, 0), tmp); | ||
554 : | h | 1.2 | |
555 : | edgomez | 1.21 | // 3=6, 6=12, 12=9, 9=3 |
556 : | edgomez | 1.7 | MOVLINE(tmp, LINE(0, 3)); |
557 : | MOVLINE(LINE(0, 3), LINE(0, 6)); | ||
558 : | MOVLINE(LINE(0, 6), LINE(2, 4)); | ||
559 : | MOVLINE(LINE(2, 4), LINE(2, 1)); | ||
560 : | MOVLINE(LINE(2, 1), tmp); | ||
561 : | h | 1.2 | |
562 : | edgomez | 1.21 | // 5=10, 10=5 |
563 : | edgomez | 1.7 | MOVLINE(tmp, LINE(0, 5)); |
564 : | MOVLINE(LINE(0, 5), LINE(2, 2)); | ||
565 : | MOVLINE(LINE(2, 2), tmp); | ||
566 : | h | 1.2 | |
567 : | edgomez | 1.21 | // 7=14, 14=13, 13=11, 11=7 |
568 : | edgomez | 1.7 | MOVLINE(tmp, LINE(0, 7)); |
569 : | MOVLINE(LINE(0, 7), LINE(2, 6)); | ||
570 : | MOVLINE(LINE(2, 6), LINE(2, 5)); | ||
571 : | MOVLINE(LINE(2, 5), LINE(2, 3)); | ||
572 : | MOVLINE(LINE(2, 3), tmp); | ||
573 : | h | 1.2 | |
574 : | /* right blocks */ | ||
575 : | |||
576 : | edgomez | 1.21 | // 1=2, 2=4, 4=8, 8=1 |
577 : | edgomez | 1.7 | MOVLINE(tmp, LINE(1, 1)); |
578 : | MOVLINE(LINE(1, 1), LINE(1, 2)); | ||
579 : | MOVLINE(LINE(1, 2), LINE(1, 4)); | ||
580 : | MOVLINE(LINE(1, 4), LINE(3, 0)); | ||
581 : | MOVLINE(LINE(3, 0), tmp); | ||
582 : | h | 1.2 | |
583 : | edgomez | 1.21 | // 3=6, 6=12, 12=9, 9=3 |
584 : | edgomez | 1.7 | MOVLINE(tmp, LINE(1, 3)); |
585 : | MOVLINE(LINE(1, 3), LINE(1, 6)); | ||
586 : | MOVLINE(LINE(1, 6), LINE(3, 4)); | ||
587 : | MOVLINE(LINE(3, 4), LINE(3, 1)); | ||
588 : | MOVLINE(LINE(3, 1), tmp); | ||
589 : | h | 1.2 | |
590 : | edgomez | 1.21 | // 5=10, 10=5 |
591 : | edgomez | 1.7 | MOVLINE(tmp, LINE(1, 5)); |
592 : | MOVLINE(LINE(1, 5), LINE(3, 2)); | ||
593 : | MOVLINE(LINE(3, 2), tmp); | ||
594 : | h | 1.2 | |
595 : | edgomez | 1.21 | // 7=14, 14=13, 13=11, 11=7 |
596 : | edgomez | 1.7 | MOVLINE(tmp, LINE(1, 7)); |
597 : | MOVLINE(LINE(1, 7), LINE(3, 6)); | ||
598 : | MOVLINE(LINE(3, 6), LINE(3, 5)); | ||
599 : | MOVLINE(LINE(3, 5), LINE(3, 3)); | ||
600 : | MOVLINE(LINE(3, 3), tmp); | ||
601 : | Isibaar | 1.1 | } |
602 : | chl | 1.21.2.10 | |
603 : | |||
604 : | |||
605 : | |||
606 : | |||
607 : | /************************************************************************ | ||
608 : | * Trellis based R-D optimal quantization * | ||
609 : | * * | ||
610 : | * Trellis Quant code (C) 2003 Pascal Massimino skal(at)planet-d.net * | ||
611 : | * * | ||
612 : | ************************************************************************/ | ||
613 : | |||
614 : | |||
615 : | static int | ||
616 : | dct_quantize_trellis_inter_mpeg_c (int16_t *qcoeff, const int16_t *data, int quant) | ||
617 : | { return 63; } | ||
618 : | |||
619 : | |||
620 : | ////////////////////////////////////////////////////////// | ||
621 : | // | ||
622 : | // Trellis-Based quantization | ||
623 : | // | ||
624 : | // So far I understand this paper: | ||
625 : | // | ||
626 : | // "Trellis-Based R-D Optimal Quantization in H.263+" | ||
627 : | // J.Wen, M.Luttrell, J.Villasenor | ||
628 : | // IEEE Transactions on Image Processing, Vol.9, No.8, Aug. 2000. | ||
629 : | // | ||
630 : | // we are at stake with a simplified Bellmand-Ford / Dijkstra Single | ||
631 : | // Source Shorted Path algo. But due to the underlying graph structure | ||
632 : | // ("Trellis"), it can be turned into a dynamic programming algo, | ||
633 : | // partially saving the explicit graph's nodes representation. And | ||
634 : | // without using a heap, since the open frontier of the DAG is always | ||
635 : | // known, and of fixed sized. | ||
636 : | // | ||
637 : | ////////////////////////////////////////////////////////// | ||
638 : | |||
639 : | |||
640 : | ////////////////////////////////////////////////////////// | ||
641 : | // Codes lengths for relevant levels. | ||
642 : | |||
643 : | // let's factorize: | ||
644 : | static const uint8_t Code_Len0[64] = { | ||
645 : | 30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30, | ||
646 : | 30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 }; | ||
647 : | static const uint8_t Code_Len1[64] = { | ||
648 : | 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, | ||
649 : | 30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 }; | ||
650 : | static const uint8_t Code_Len2[64] = { | ||
651 : | 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, | ||
652 : | 30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 }; | ||
653 : | static const uint8_t Code_Len3[64] = { | ||
654 : | 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, | ||
655 : | 30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 }; | ||
656 : | static const uint8_t Code_Len4[64] = { | ||
657 : | 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, | ||
658 : | 30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 }; | ||
659 : | static const uint8_t Code_Len5[64] = { | ||
660 : | 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, | ||
661 : | 30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30,30 }; | ||
662 : | static const uint8_t Code_Len6[64] = { | ||
663 : | 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, | ||
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_Len7[64] = { | ||
666 : | 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, | ||
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_Len8[64] = { | ||
669 : | 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, | ||
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_Len9[64] = { | ||
672 : | 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, | ||
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_Len10[64] = { | ||
675 : | 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, | ||
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_Len11[64] = { | ||
678 : | 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, | ||
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_Len12[64] = { | ||
681 : | 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, | ||
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_Len13[64] = { | ||
684 : | 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, | ||
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_Len14[64] = { | ||
687 : | 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, | ||
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_Len15[64] = { | ||
690 : | 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, | ||
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_Len16[64] = { | ||
693 : | 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, | ||
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_Len17[64] = { | ||
696 : | 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, | ||
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_Len18[64] = { | ||
699 : | 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, | ||
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_Len19[64] = { | ||
702 : | 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, | ||
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_Len20[64] = { | ||
705 : | 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, | ||
706 : | 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 }; | ||
707 : | |||
708 : | // a few more table for LAST table: | ||
709 : | static const uint8_t Code_Len21[64] = { | ||
710 : | 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, | ||
711 : | 30,30,30,30,30,30,30,30,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 : | static const uint8_t Code_Len22[64] = { | ||
713 : | 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, | ||
714 : | 30,30,30,30,30,30,30,30,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 : | static const uint8_t Code_Len23[64] = { | ||
716 : | 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, | ||
717 : | 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}; | ||
718 : | static const uint8_t Code_Len24[64] = { | ||
719 : | 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, | ||
720 : | 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}; | ||
721 : | |||
722 : | |||
723 : | static const uint8_t * const B16_17_Code_Len[24] = { // levels [1..24] | ||
724 : | Code_Len20,Code_Len19,Code_Len18,Code_Len17, | ||
725 : | Code_Len16,Code_Len15,Code_Len14,Code_Len13, | ||
726 : | Code_Len12,Code_Len11,Code_Len10,Code_Len9, | ||
727 : | Code_Len8, Code_Len7 ,Code_Len6 ,Code_Len5, | ||
728 : | Code_Len4, Code_Len3, Code_Len3 ,Code_Len2, | ||
729 : | Code_Len2, Code_Len1, Code_Len1, Code_Len1, | ||
730 : | }; | ||
731 : | |||
732 : | static const uint8_t * const B16_17_Code_Len_Last[6] = { // levels [1..6] | ||
733 : | Code_Len24,Code_Len23,Code_Len22,Code_Len21, Code_Len3, Code_Len1, | ||
734 : | }; | ||
735 : | |||
736 : | #define TL(q) 0xfe00/(q*q) | ||
737 : | |||
738 : | static const int Trellis_Lambda_Tabs[31] = { | ||
739 : | TL( 1),TL( 2),TL( 3),TL( 4),TL( 5),TL( 6), TL( 7), | ||
740 : | TL( 8),TL( 9),TL(10),TL(11),TL(12),TL(13),TL(14), TL(15), | ||
741 : | TL(16),TL(17),TL(18),TL(19),TL(20),TL(21),TL(22), TL(23), | ||
742 : | TL(24),TL(25),TL(26),TL(27),TL(28),TL(29),TL(30), TL(31) | ||
743 : | }; | ||
744 : | #undef TL | ||
745 : | |||
746 : | static inline int Find_Last(const int16_t *C, const uint16_t *Zigzag, int i) | ||
747 : | { | ||
748 : | while(i>=0) | ||
749 : | if (C[Zigzag[i]]) | ||
750 : | return i; | ||
751 : | else i--; | ||
752 : | return -1; | ||
753 : | } | ||
754 : | |||
755 : | ////////////////////////////////////////////////////////// | ||
756 : | |||
757 : | #define DBG 0 | ||
758 : | |||
759 : | static uint32_t Evaluate_Cost(const int16_t *C, int Mult, int Bias, | ||
760 : | const uint16_t * Zigzag, int Max, int Lambda) | ||
761 : | { | ||
762 : | #if (DBG>0) | ||
763 : | const int16_t * const Ref = C + 6*64; | ||
764 : | int Last = Max; | ||
765 : | while(Last>=0 && C[Zigzag[Last]]==0) Last--; | ||
766 : | int Bits = 0; | ||
767 : | if (Last>=0) { | ||
768 : | Bits = 2; // CBP | ||
769 : | int j=0, j0=0; | ||
770 : | int Run, Level; | ||
771 : | while(j<Last) { | ||
772 : | while(!C[Zigzag[j]]) j++; | ||
773 : | if (j==Last) break; | ||
774 : | Level=C[Zigzag[j]]; | ||
775 : | Run = j - j0; | ||
776 : | j0 = ++j; | ||
777 : | if (Level>=-24 && Level<=24) Bits += B16_17_Code_Len[(Level<0) ? -Level-1 : Level-1][Run]; | ||
778 : | else Bits += 30; | ||
779 : | } | ||
780 : | Level = C[Zigzag[Last]]; | ||
781 : | Run = j - j0; | ||
782 : | if (Level>=-6 && Level<=6) Bits += B16_17_Code_Len_Last[(Level<0) ? -Level-1 : Level-1][Run]; | ||
783 : | else Bits += 30; | ||
784 : | } | ||
785 : | |||
786 : | int Dist = 0; | ||
787 : | int i; | ||
788 : | for(i=0; i<=Last; ++i) { | ||
789 : | int V = C[Zigzag[i]]*Mult; | ||
790 : | if (V>0) V += Bias; | ||
791 : | else if (V<0) V -= Bias; | ||
792 : | V -= Ref[Zigzag[i]]; | ||
793 : | Dist += V*V; | ||
794 : | } | ||
795 : | uint32_t Cost = Lambda*Dist + (Bits<<16); | ||
796 : | if (DBG==1) | ||
797 : | printf( " Last:%2d/%2d Cost = [(Bits=%5.0d) + Lambda*(Dist=%6.0d) = %d ] >>12= %d ", Last,Max, Bits, Dist, Cost, Cost>>12 ); | ||
798 : | return Cost; | ||
799 : | |||
800 : | #else | ||
801 : | return 0; | ||
802 : | #endif | ||
803 : | } | ||
804 : | |||
805 : | |||
806 : | static int | ||
807 : | dct_quantize_trellis_h263_c(int16_t *const Out, const int16_t *const In, int Q, const uint16_t * const Zigzag, int Non_Zero) | ||
808 : | { | ||
809 : | |||
810 : | // Note: We should search last non-zero coeffs on *real* DCT input coeffs (In[]), | ||
811 : | // not quantized one (Out[]). However, it only improves the result *very* | ||
812 : | // slightly (~0.01dB), whereas speed drops to crawling level :) | ||
813 : | // Well, actually, taking 1 more coeff past Non_Zero into account sometimes helps, | ||
814 : | |||
815 : | typedef struct { int16_t Run, Level; } NODE; | ||
816 : | |||
817 : | NODE Nodes[65], Last; | ||
818 : | uint32_t Run_Costs0[64+1], * const Run_Costs = Run_Costs0 + 1; | ||
819 : | |||
820 : | const int Mult = 2*Q; | ||
821 : | const int Bias = (Q-1) | 1; | ||
822 : | const int Lev0 = Mult + Bias; | ||
823 : | const int Lambda = Trellis_Lambda_Tabs[Q-1]; // it's 1/lambda, actually | ||
824 : | |||
825 : | int Run_Start = -1; | ||
826 : | Run_Costs[-1] = 2<<16; // source (w/ CBP penalty) | ||
827 : | uint32_t Min_Cost = 2<<16; | ||
828 : | |||
829 : | int Last_Node = -1; | ||
830 : | uint32_t Last_Cost = 0; | ||
831 : | |||
832 : | #if (DBG>0) | ||
833 : | Last.Level = 0; Last.Run = -1; // just initialize to smthg | ||
834 : | #endif | ||
835 : | |||
836 : | int i, j; | ||
837 : | |||
838 : | Non_Zero = Find_Last(Out, Zigzag, Non_Zero); | ||
839 : | if (Non_Zero<0) | ||
840 : | return -1; | ||
841 : | |||
842 : | for(i=0; i<=Non_Zero; i++) | ||
843 : | { | ||
844 : | const int AC = In[Zigzag[i]]; | ||
845 : | const int Level1 = Out[Zigzag[i]]; | ||
846 : | const int Dist0 = Lambda* AC*AC; | ||
847 : | uint32_t Best_Cost = 0xf0000000; | ||
848 : | Last_Cost += Dist0; | ||
849 : | |||
850 : | if ((uint32_t)(Level1+1)<3) // very specialized loop for -1,0,+1 | ||
851 : | { | ||
852 : | int dQ; | ||
853 : | int Run; | ||
854 : | |||
855 : | if (AC<0) { | ||
856 : | Nodes[i].Level = -1; | ||
857 : | dQ = Lev0 + AC; | ||
858 : | } else { | ||
859 : | Nodes[i].Level = 1; | ||
860 : | dQ = Lev0 - AC; | ||
861 : | } | ||
862 : | const uint32_t Cost0 = Lambda*dQ*dQ; | ||
863 : | |||
864 : | Nodes[i].Run = 1; | ||
865 : | Best_Cost = (Code_Len20[0]<<16) + Run_Costs[i-1]+Cost0; | ||
866 : | for(Run=i-Run_Start; Run>0; --Run) | ||
867 : | { | ||
868 : | const uint32_t Cost_Base = Cost0 + Run_Costs[i-Run]; | ||
869 : | const uint32_t Cost = Cost_Base + (Code_Len20[Run-1]<<16); | ||
870 : | // TODO: what about tie-breaks? Should we favor short runs or | ||
871 : | // long runs? Although the error is the same, it would not be | ||
872 : | // spread the same way along high and low frequencies... | ||
873 : | if (Cost<Best_Cost) | ||
874 : | { | ||
875 : | Best_Cost = Cost; | ||
876 : | Nodes[i].Run = Run; | ||
877 : | } | ||
878 : | const uint32_t lCost = Cost_Base + (Code_Len24[Run-1]<<16); | ||
879 : | if (lCost<Last_Cost) | ||
880 : | { | ||
881 : | Last_Cost = lCost; | ||
882 : | Last.Run = Run; | ||
883 : | Last_Node = i; | ||
884 : | } | ||
885 : | } | ||
886 : | if (Last_Node==i) Last.Level = Nodes[i].Level; | ||
887 : | |||
888 : | |||
889 : | if (DBG==1) { | ||
890 : | Run_Costs[i] = Best_Cost; | ||
891 : | printf( "Costs #%2d: ", i); | ||
892 : | for(j=-1;j<=Non_Zero;++j) { | ||
893 : | if (j==Run_Start) printf( " %3.0d|", Run_Costs[j]>>12 ); | ||
894 : | else if (j>Run_Start && j<i) printf( " %3.0d|", Run_Costs[j]>>12 ); | ||
895 : | else if (j==i) printf( "(%3.0d)", Run_Costs[j]>>12 ); | ||
896 : | else printf( " - |" ); | ||
897 : | } | ||
898 : | printf( "<%3.0d %2d %d>", Min_Cost>>12, Nodes[i].Level, Nodes[i].Run ); | ||
899 : | printf( " Last:#%2d {%3.0d %2d %d}", Last_Node, Last_Cost>>12, Last.Level, Last.Run ); | ||
900 : | printf( " AC:%3.0d Dist0:%3d Dist(%d)=%d", AC, Dist0>>12, Nodes[i].Level, Cost0>>12 ); | ||
901 : | printf( "\n" ); | ||
902 : | } | ||
903 : | } | ||
904 : | else // "big" levels | ||
905 : | { | ||
906 : | const uint8_t *Tbl_L1, *Tbl_L2, *Tbl_L1_Last, *Tbl_L2_Last; | ||
907 : | int Level2; | ||
908 : | int dQ1, dQ2; | ||
909 : | int Run; | ||
910 : | |||
911 : | if (Level1>1) { | ||
912 : | dQ1 = Level1*Mult-AC + Bias; | ||
913 : | dQ2 = dQ1 - Mult; | ||
914 : | Level2 = Level1-1; | ||
915 : | Tbl_L1 = (Level1<=24) ? B16_17_Code_Len[Level1-1] : Code_Len0; | ||
916 : | Tbl_L2 = (Level2<=24) ? B16_17_Code_Len[Level2-1] : Code_Len0; | ||
917 : | Tbl_L1_Last = (Level1<=6) ? B16_17_Code_Len_Last[Level1-1] : Code_Len0; | ||
918 : | Tbl_L2_Last = (Level2<=6) ? B16_17_Code_Len_Last[Level2-1] : Code_Len0; | ||
919 : | } | ||
920 : | else { // Level1<-1 | ||
921 : | dQ1 = Level1*Mult-AC - Bias; | ||
922 : | dQ2 = dQ1 + Mult; | ||
923 : | Level2 = Level1 + 1; | ||
924 : | Tbl_L1 = (Level1>=-24) ? B16_17_Code_Len[Level1^-1] : Code_Len0; | ||
925 : | Tbl_L2 = (Level2>=-24) ? B16_17_Code_Len[Level2^-1] : Code_Len0; | ||
926 : | Tbl_L1_Last = (Level1>=- 6) ? B16_17_Code_Len_Last[Level1^-1] : Code_Len0; | ||
927 : | Tbl_L2_Last = (Level2>=- 6) ? B16_17_Code_Len_Last[Level2^-1] : Code_Len0; | ||
928 : | } | ||
929 : | const uint32_t Dist1 = Lambda*dQ1*dQ1; | ||
930 : | const uint32_t Dist2 = Lambda*dQ2*dQ2; | ||
931 : | const int dDist21 = Dist2-Dist1; | ||
932 : | |||
933 : | for(Run=i-Run_Start; Run>0; --Run) | ||
934 : | { | ||
935 : | const uint32_t Cost_Base = Dist1 + Run_Costs[i-Run]; | ||
936 : | |||
937 : | // for sub-optimal (but slightly worth it, speed-wise) search, uncomment the following: | ||
938 : | // if (Cost_Base>=Best_Cost) continue; | ||
939 : | |||
940 : | uint32_t Cost1, Cost2; | ||
941 : | int bLevel; | ||
942 : | |||
943 : | Cost1 = Cost_Base + (Tbl_L1[Run-1]<<16); | ||
944 : | Cost2 = Cost_Base + (Tbl_L2[Run-1]<<16) + dDist21; | ||
945 : | |||
946 : | if (Cost2<Cost1) { Cost1 = Cost2; bLevel = Level2; } | ||
947 : | else bLevel = Level1; | ||
948 : | |||
949 : | if (Cost1<Best_Cost) | ||
950 : | { | ||
951 : | Best_Cost = Cost1; | ||
952 : | Nodes[i].Run = Run; | ||
953 : | Nodes[i].Level = bLevel; | ||
954 : | } | ||
955 : | |||
956 : | Cost1 = Cost_Base + (Tbl_L1_Last[Run-1]<<16); | ||
957 : | Cost2 = Cost_Base + (Tbl_L2_Last[Run-1]<<16) + dDist21; | ||
958 : | |||
959 : | if (Cost2<Cost1) { Cost1 = Cost2; bLevel = Level2; } | ||
960 : | else bLevel = Level1; | ||
961 : | if (Cost1<Last_Cost) | ||
962 : | { | ||
963 : | Last_Cost = Cost1; | ||
964 : | Last.Run = Run; | ||
965 : | Last.Level = bLevel; | ||
966 : | Last_Node = i; | ||
967 : | } | ||
968 : | } | ||
969 : | |||
970 : | if (DBG==1) { | ||
971 : | Run_Costs[i] = Best_Cost; | ||
972 : | printf( "Costs #%2d: ", i); | ||
973 : | for(j=-1;j<=Non_Zero;++j) { | ||
974 : | if (j==Run_Start) printf( " %3.0d|", Run_Costs[j]>>12 ); | ||
975 : | else if (j>Run_Start && j<i) printf( " %3.0d|", Run_Costs[j]>>12 ); | ||
976 : | else if (j==i) printf( "(%3.0d)", Run_Costs[j]>>12 ); | ||
977 : | else printf( " - |" ); | ||
978 : | } | ||
979 : | printf( "<%3.0d %2d %d>", Min_Cost>>12, Nodes[i].Level, Nodes[i].Run ); | ||
980 : | printf( " Last:#%2d {%3.0d %2d %d}", Last_Node, Last_Cost>>12, Last.Level, Last.Run ); | ||
981 : | printf( " AC:%3.0d Dist0:%3d Dist(%2d):%3d Dist(%2d):%3d", AC, Dist0>>12, Level1, Dist1>>12, Level2, Dist2>>12 ); | ||
982 : | printf( "\n" ); | ||
983 : | } | ||
984 : | } | ||
985 : | |||
986 : | Run_Costs[i] = Best_Cost; | ||
987 : | |||
988 : | if (Best_Cost < Min_Cost + Dist0) { | ||
989 : | Min_Cost = Best_Cost; | ||
990 : | Run_Start = i; | ||
991 : | } | ||
992 : | else | ||
993 : | { | ||
994 : | // as noticed by Michael Niedermayer (michaelni at gmx.at), there's | ||
995 : | // a code shorter by 1 bit for a larger run (!), same level. We give | ||
996 : | // it a chance by not moving the left barrier too much. | ||
997 : | while( Run_Costs[Run_Start]>Min_Cost+(1<<16) ) | ||
998 : | Run_Start++; | ||
999 : | |||
1000 : | // spread on preceding coeffs the cost incurred by skipping this one | ||
1001 : | for(j=Run_Start; j<i; ++j) Run_Costs[j] += Dist0; | ||
1002 : | Min_Cost += Dist0; | ||
1003 : | } | ||
1004 : | } | ||
1005 : | |||
1006 : | if (DBG) { | ||
1007 : | Last_Cost = Evaluate_Cost(Out,Mult,Bias, Zigzag,Non_Zero, Lambda); | ||
1008 : | if (DBG==1) { | ||
1009 : | printf( "=> " ); | ||
1010 : | for(i=0; i<=Non_Zero; ++i) printf( "[%3.0d] ", Out[Zigzag[i]] ); | ||
1011 : | printf( "\n" ); | ||
1012 : | } | ||
1013 : | } | ||
1014 : | |||
1015 : | if (Last_Node<0) | ||
1016 : | return -1; | ||
1017 : | |||
1018 : | // reconstruct optimal sequence backward with surviving paths | ||
1019 : | bzero(Out, 64*sizeof(*Out)); | ||
1020 : | Out[Zigzag[Last_Node]] = Last.Level; | ||
1021 : | i = Last_Node - Last.Run; | ||
1022 : | while(i>=0) { | ||
1023 : | Out[Zigzag[i]] = Nodes[i].Level; | ||
1024 : | i -= Nodes[i].Run; | ||
1025 : | } | ||
1026 : | |||
1027 : | if (DBG) { | ||
1028 : | uint32_t Cost = Evaluate_Cost(Out,Mult,Bias, Zigzag,Non_Zero, Lambda); | ||
1029 : | if (DBG==1) { | ||
1030 : | printf( "<= " ); | ||
1031 : | for(i=0; i<=Last_Node; ++i) printf( "[%3.0d] ", Out[Zigzag[i]] ); | ||
1032 : | printf( "\n--------------------------------\n" ); | ||
1033 : | } | ||
1034 : | if (Cost>Last_Cost) printf( "!!! %u > %u\n", Cost, Last_Cost ); | ||
1035 : | } | ||
1036 : | return Last_Node; | ||
1037 : | } | ||
1038 : | |||
1039 : | #undef DBG |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |