Parent Directory
|
Revision Log
Revision 514 - (view) (download)
1 : | Isibaar | 3 | /************************************************************************** |
2 : | * | ||
3 : | edgomez | 146 | * XVID MPEG-4 VIDEO CODEC |
4 : | * - Decoder main module - | ||
5 : | Isibaar | 3 | * |
6 : | edgomez | 146 | * This program is an implementation of a part of one or more MPEG-4 |
7 : | * Video tools as specified in ISO/IEC 14496-2 standard. Those intending | ||
8 : | * to use this software module in hardware or software products are | ||
9 : | * advised that its use may infringe existing patents or copyrights, and | ||
10 : | * any such use would be at such party's own risk. The original | ||
11 : | * developer of this software module and his/her company, and subsequent | ||
12 : | * editors and their companies, will have no liability for use of this | ||
13 : | * software or modifications or derivatives thereof. | ||
14 : | Isibaar | 3 | * |
15 : | edgomez | 146 | * This program is free software; you can redistribute it and/or modify |
16 : | * it under the terms of the GNU General Public License as published by | ||
17 : | * the Free Software Foundation; either version 2 of the License, or | ||
18 : | * (at your option) any later version. | ||
19 : | Isibaar | 3 | * |
20 : | edgomez | 146 | * This program is distributed in the hope that it will be useful, |
21 : | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
22 : | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
23 : | * GNU General Public License for more details. | ||
24 : | Isibaar | 3 | * |
25 : | edgomez | 146 | * You should have received a copy of the GNU General Public License |
26 : | * along with this program; if not, write to the Free Software | ||
27 : | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
28 : | Isibaar | 3 | * |
29 : | *************************************************************************/ | ||
30 : | |||
31 : | /************************************************************************** | ||
32 : | * | ||
33 : | edgomez | 146 | * History: |
34 : | Isibaar | 3 | * |
35 : | chenm001 | 297 | * 15.07.2002 fix a bug in B-frame decode at DIRECT mode |
36 : | * MinChen <chenm001@163.com> | ||
37 : | chenm001 | 290 | * 10.07.2002 added BFRAMES_DEC_DEBUG support |
38 : | * Fix a little bug for low_delay flage | ||
39 : | * MinChen <chenm001@163.com> | ||
40 : | chenm001 | 272 | * 28.06.2002 added basic resync support to iframe/pframe_decode() |
41 : | chenm001 | 290 | * 22.06.2002 added primative N_VOP support |
42 : | chenm001 | 297 | * #define BFRAMES_DEC now enables Minchen's bframe decoder |
43 : | chenm001 | 168 | * 08.05.2002 add low_delay support for B_VOP decode |
44 : | * MinChen <chenm001@163.com> | ||
45 : | * 05.05.2002 fix some B-frame decode problem | ||
46 : | chenm001 | 156 | * 02.05.2002 add B-frame decode support(have some problem); |
47 : | * MinChen <chenm001@163.com> | ||
48 : | chenm001 | 133 | * 22.04.2002 add some B-frame decode support; chenm001 <chenm001@163.com> |
49 : | h | 81 | * 29.03.2002 interlacing fix - compensated block wasn't being used when |
50 : | * reconstructing blocks, thus artifacts | ||
51 : | * interlacing speedup - used transfers to re-interlace | ||
52 : | * interlaced decoding should be as fast as progressive now | ||
53 : | h | 69 | * 26.03.2002 interlacing support - moved transfers outside decode loop |
54 : | edgomez | 146 | * 26.12.2001 decoder_mbinter: dequant/idct moved within if(coded) block |
55 : | * 22.12.2001 lock based interpolation | ||
56 : | suxen_drol | 499 | * 01.12.2001 inital version; (c)2001 peter ross <pross@xvid.org> |
57 : | Isibaar | 3 | * |
58 : | edgomez | 514 | * $Id: decoder.c,v 1.39 2002-09-22 17:01:36 edgomez Exp $ |
59 : | edgomez | 146 | * |
60 : | Isibaar | 3 | *************************************************************************/ |
61 : | |||
62 : | #include <stdlib.h> | ||
63 : | edgomez | 146 | #include <string.h> |
64 : | Isibaar | 3 | |
65 : | chenm001 | 290 | #ifdef BFRAMES_DEC_DEBUG |
66 : | #define BFRAMES_DEC | ||
67 : | #endif | ||
68 : | |||
69 : | Isibaar | 3 | #include "xvid.h" |
70 : | #include "portab.h" | ||
71 : | |||
72 : | #include "decoder.h" | ||
73 : | #include "bitstream/bitstream.h" | ||
74 : | #include "bitstream/mbcoding.h" | ||
75 : | |||
76 : | #include "quant/quant_h263.h" | ||
77 : | #include "quant/quant_mpeg4.h" | ||
78 : | #include "dct/idct.h" | ||
79 : | #include "dct/fdct.h" | ||
80 : | #include "utils/mem_transfer.h" | ||
81 : | #include "image/interpolate8x8.h" | ||
82 : | |||
83 : | #include "bitstream/mbcoding.h" | ||
84 : | #include "prediction/mbprediction.h" | ||
85 : | #include "utils/timer.h" | ||
86 : | #include "utils/emms.h" | ||
87 : | |||
88 : | #include "image/image.h" | ||
89 : | #include "image/colorspace.h" | ||
90 : | Isibaar | 41 | #include "utils/mem_align.h" |
91 : | Isibaar | 3 | |
92 : | edgomez | 195 | int |
93 : | decoder_create(XVID_DEC_PARAM * param) | ||
94 : | Isibaar | 3 | { |
95 : | edgomez | 195 | DECODER *dec; |
96 : | Isibaar | 3 | |
97 : | Isibaar | 42 | dec = xvid_malloc(sizeof(DECODER), CACHE_LINE); |
98 : | edgomez | 195 | if (dec == NULL) { |
99 : | Isibaar | 3 | return XVID_ERR_MEMORY; |
100 : | } | ||
101 : | param->handle = dec; | ||
102 : | |||
103 : | dec->width = param->width; | ||
104 : | dec->height = param->height; | ||
105 : | |||
106 : | dec->mb_width = (dec->width + 15) / 16; | ||
107 : | dec->mb_height = (dec->height + 15) / 16; | ||
108 : | |||
109 : | dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE; | ||
110 : | dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE; | ||
111 : | Isibaar | 208 | dec->low_delay = 0; |
112 : | edgomez | 195 | |
113 : | if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) { | ||
114 : | Isibaar | 41 | xvid_free(dec); |
115 : | Isibaar | 3 | return XVID_ERR_MEMORY; |
116 : | } | ||
117 : | |||
118 : | edgomez | 195 | if (image_create(&dec->refn[0], dec->edged_width, dec->edged_height)) { |
119 : | Isibaar | 3 | image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
120 : | Isibaar | 41 | xvid_free(dec); |
121 : | Isibaar | 3 | return XVID_ERR_MEMORY; |
122 : | } | ||
123 : | chenm001 | 133 | // add by chenm001 <chenm001@163.com> |
124 : | // for support B-frame to reference last 2 frame | ||
125 : | edgomez | 195 | if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) { |
126 : | chenm001 | 133 | image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
127 : | image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); | ||
128 : | xvid_free(dec); | ||
129 : | return XVID_ERR_MEMORY; | ||
130 : | } | ||
131 : | edgomez | 195 | if (image_create(&dec->refn[2], dec->edged_width, dec->edged_height)) { |
132 : | chenm001 | 156 | image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
133 : | image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); | ||
134 : | image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); | ||
135 : | xvid_free(dec); | ||
136 : | return XVID_ERR_MEMORY; | ||
137 : | } | ||
138 : | Isibaar | 3 | |
139 : | edgomez | 195 | dec->mbs = |
140 : | xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, | ||
141 : | CACHE_LINE); | ||
142 : | if (dec->mbs == NULL) { | ||
143 : | Isibaar | 3 | image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
144 : | chenm001 | 156 | image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
145 : | image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); | ||
146 : | image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height); | ||
147 : | Isibaar | 41 | xvid_free(dec); |
148 : | Isibaar | 3 | return XVID_ERR_MEMORY; |
149 : | } | ||
150 : | Isibaar | 208 | |
151 : | memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height); | ||
152 : | |||
153 : | chenm001 | 156 | // add by chenm001 <chenm001@163.com> |
154 : | // for skip MB flag | ||
155 : | edgomez | 195 | dec->last_mbs = |
156 : | xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, | ||
157 : | CACHE_LINE); | ||
158 : | if (dec->last_mbs == NULL) { | ||
159 : | chenm001 | 156 | xvid_free(dec->mbs); |
160 : | image_destroy(&dec->cur, dec->edged_width, dec->edged_height); | ||
161 : | image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); | ||
162 : | image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); | ||
163 : | image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height); | ||
164 : | xvid_free(dec); | ||
165 : | return XVID_ERR_MEMORY; | ||
166 : | } | ||
167 : | Isibaar | 3 | |
168 : | Isibaar | 208 | memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height); |
169 : | |||
170 : | Isibaar | 3 | init_timer(); |
171 : | |||
172 : | chenm001 | 156 | // add by chenm001 <chenm001@163.com> |
173 : | // for support B-frame to save reference frame's time | ||
174 : | dec->frames = -1; | ||
175 : | dec->time = dec->time_base = dec->last_time_base = 0; | ||
176 : | edgomez | 195 | |
177 : | Isibaar | 3 | return XVID_ERR_OK; |
178 : | } | ||
179 : | |||
180 : | |||
181 : | edgomez | 195 | int |
182 : | decoder_destroy(DECODER * dec) | ||
183 : | Isibaar | 3 | { |
184 : | chenm001 | 156 | xvid_free(dec->last_mbs); |
185 : | Isibaar | 41 | xvid_free(dec->mbs); |
186 : | chenm001 | 133 | image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
187 : | chenm001 | 156 | image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
188 : | image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height); | ||
189 : | Isibaar | 3 | image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
190 : | Isibaar | 41 | xvid_free(dec); |
191 : | Isibaar | 3 | |
192 : | write_timer(); | ||
193 : | return XVID_ERR_OK; | ||
194 : | } | ||
195 : | |||
196 : | |||
197 : | |||
198 : | edgomez | 195 | static const int32_t dquant_table[4] = { |
199 : | Isibaar | 3 | -1, -2, 1, 2 |
200 : | }; | ||
201 : | |||
202 : | |||
203 : | chenm001 | 156 | |
204 : | |||
205 : | Isibaar | 3 | // decode an intra macroblock |
206 : | |||
207 : | edgomez | 195 | void |
208 : | decoder_mbintra(DECODER * dec, | ||
209 : | MACROBLOCK * pMB, | ||
210 : | const uint32_t x_pos, | ||
211 : | const uint32_t y_pos, | ||
212 : | const uint32_t acpred_flag, | ||
213 : | const uint32_t cbp, | ||
214 : | Bitstream * bs, | ||
215 : | const uint32_t quant, | ||
216 : | chenm001 | 272 | const uint32_t intra_dc_threshold, |
217 : | const unsigned int bound) | ||
218 : | Isibaar | 3 | { |
219 : | edgomez | 78 | |
220 : | DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE); | ||
221 : | edgomez | 195 | DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); |
222 : | edgomez | 78 | |
223 : | h | 81 | uint32_t stride = dec->edged_width; |
224 : | uint32_t stride2 = stride / 2; | ||
225 : | uint32_t next_block = stride * 8; | ||
226 : | h | 69 | uint32_t i; |
227 : | uint32_t iQuant = pMB->quant; | ||
228 : | uint8_t *pY_Cur, *pU_Cur, *pV_Cur; | ||
229 : | |||
230 : | edgomez | 78 | pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); |
231 : | h | 81 | pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); |
232 : | pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); | ||
233 : | h | 69 | |
234 : | edgomez | 195 | memset(block, 0, 6 * 64 * sizeof(int16_t)); // clear |
235 : | h | 69 | |
236 : | edgomez | 195 | for (i = 0; i < 6; i++) { |
237 : | h | 69 | uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
238 : | Isibaar | 3 | int16_t predictors[8]; |
239 : | int start_coeff; | ||
240 : | |||
241 : | start_timer(); | ||
242 : | edgomez | 195 | predict_acdc(dec->mbs, x_pos, y_pos, dec->mb_width, i, &block[i * 64], |
243 : | chenm001 | 272 | iQuant, iDcScaler, predictors, bound); |
244 : | edgomez | 195 | if (!acpred_flag) { |
245 : | h | 69 | pMB->acpred_directions[i] = 0; |
246 : | Isibaar | 3 | } |
247 : | stop_prediction_timer(); | ||
248 : | |||
249 : | edgomez | 195 | if (quant < intra_dc_threshold) { |
250 : | Isibaar | 3 | int dc_size; |
251 : | int dc_dif; | ||
252 : | |||
253 : | edgomez | 195 | dc_size = i < 4 ? get_dc_size_lum(bs) : get_dc_size_chrom(bs); |
254 : | dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0; | ||
255 : | Isibaar | 3 | |
256 : | edgomez | 195 | if (dc_size > 8) { |
257 : | BitstreamSkip(bs, 1); // marker | ||
258 : | Isibaar | 3 | } |
259 : | edgomez | 195 | |
260 : | block[i * 64 + 0] = dc_dif; | ||
261 : | Isibaar | 3 | start_coeff = 1; |
262 : | chenm001 | 272 | |
263 : | DPRINTF(DPRINTF_COEFF,"block[0] %i", dc_dif); | ||
264 : | edgomez | 195 | } else { |
265 : | Isibaar | 3 | start_coeff = 0; |
266 : | } | ||
267 : | |||
268 : | start_timer(); | ||
269 : | edgomez | 195 | if (cbp & (1 << (5 - i))) // coded |
270 : | Isibaar | 3 | { |
271 : | edgomez | 195 | get_intra_block(bs, &block[i * 64], pMB->acpred_directions[i], |
272 : | start_coeff); | ||
273 : | Isibaar | 3 | } |
274 : | stop_coding_timer(); | ||
275 : | |||
276 : | start_timer(); | ||
277 : | edgomez | 195 | add_acdc(pMB, i, &block[i * 64], iDcScaler, predictors); |
278 : | Isibaar | 3 | stop_prediction_timer(); |
279 : | |||
280 : | start_timer(); | ||
281 : | edgomez | 195 | if (dec->quant_type == 0) { |
282 : | dequant_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler); | ||
283 : | } else { | ||
284 : | dequant4_intra(&data[i * 64], &block[i * 64], iQuant, iDcScaler); | ||
285 : | Isibaar | 3 | } |
286 : | stop_iquant_timer(); | ||
287 : | |||
288 : | start_timer(); | ||
289 : | edgomez | 195 | idct(&data[i * 64]); |
290 : | Isibaar | 3 | stop_idct_timer(); |
291 : | h | 69 | } |
292 : | Isibaar | 3 | |
293 : | edgomez | 195 | if (dec->interlacing && pMB->field_dct) { |
294 : | h | 81 | next_block = stride; |
295 : | stride *= 2; | ||
296 : | Isibaar | 3 | } |
297 : | h | 69 | |
298 : | start_timer(); | ||
299 : | edgomez | 195 | transfer_16to8copy(pY_Cur, &data[0 * 64], stride); |
300 : | transfer_16to8copy(pY_Cur + 8, &data[1 * 64], stride); | ||
301 : | transfer_16to8copy(pY_Cur + next_block, &data[2 * 64], stride); | ||
302 : | transfer_16to8copy(pY_Cur + 8 + next_block, &data[3 * 64], stride); | ||
303 : | transfer_16to8copy(pU_Cur, &data[4 * 64], stride2); | ||
304 : | transfer_16to8copy(pV_Cur, &data[5 * 64], stride2); | ||
305 : | h | 69 | stop_transfer_timer(); |
306 : | Isibaar | 3 | } |
307 : | |||
308 : | |||
309 : | |||
310 : | |||
311 : | |||
312 : | #define SIGN(X) (((X)>0)?1:-1) | ||
313 : | #define ABS(X) (((X)>0)?(X):-(X)) | ||
314 : | static const uint32_t roundtab[16] = | ||
315 : | edgomez | 195 | { 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 }; |
316 : | Isibaar | 3 | |
317 : | |||
318 : | // decode an inter macroblock | ||
319 : | |||
320 : | edgomez | 195 | void |
321 : | decoder_mbinter(DECODER * dec, | ||
322 : | const MACROBLOCK * pMB, | ||
323 : | const uint32_t x_pos, | ||
324 : | const uint32_t y_pos, | ||
325 : | const uint32_t acpred_flag, | ||
326 : | const uint32_t cbp, | ||
327 : | Bitstream * bs, | ||
328 : | const uint32_t quant, | ||
329 : | const uint32_t rounding) | ||
330 : | Isibaar | 3 | { |
331 : | h | 69 | |
332 : | edgomez | 195 | DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE); |
333 : | edgomez | 78 | DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); |
334 : | |||
335 : | h | 81 | uint32_t stride = dec->edged_width; |
336 : | uint32_t stride2 = stride / 2; | ||
337 : | uint32_t next_block = stride * 8; | ||
338 : | edgomez | 78 | uint32_t i; |
339 : | uint32_t iQuant = pMB->quant; | ||
340 : | h | 69 | uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
341 : | Isibaar | 3 | int uv_dx, uv_dy; |
342 : | |||
343 : | edgomez | 78 | pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); |
344 : | h | 81 | pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); |
345 : | pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); | ||
346 : | h | 69 | |
347 : | edgomez | 195 | if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
348 : | h | 69 | uv_dx = pMB->mvs[0].x; |
349 : | uv_dy = pMB->mvs[0].y; | ||
350 : | Isibaar | 3 | |
351 : | Isibaar | 333 | if (dec->quarterpel) |
352 : | { | ||
353 : | uv_dx = (uv_dx >> 1) | (uv_dx & 1); | ||
354 : | uv_dy = (uv_dy >> 1) | (uv_dy & 1); | ||
355 : | } | ||
356 : | |||
357 : | Isibaar | 3 | uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2; |
358 : | uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2; | ||
359 : | edgomez | 195 | } else { |
360 : | Isibaar | 3 | int sum; |
361 : | h | 69 | sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
362 : | Isibaar | 3 | |
363 : | Isibaar | 333 | if (dec->quarterpel) |
364 : | { | ||
365 : | sum /= 2; | ||
366 : | } | ||
367 : | |||
368 : | uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2)); | ||
369 : | |||
370 : | h | 69 | sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
371 : | Isibaar | 333 | |
372 : | if (dec->quarterpel) | ||
373 : | { | ||
374 : | sum /= 2; | ||
375 : | } | ||
376 : | |||
377 : | uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2)); | ||
378 : | Isibaar | 3 | } |
379 : | |||
380 : | start_timer(); | ||
381 : | Isibaar | 333 | if(dec->quarterpel) { |
382 : | edgomez | 514 | DPRINTF(DPRINTF_DEBUG, "QUARTERPEL\n"); |
383 : | Isibaar | 333 | interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos, |
384 : | pMB->mvs[0].x, pMB->mvs[0].y, stride, rounding); | ||
385 : | interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos, | ||
386 : | pMB->mvs[1].x, pMB->mvs[1].y, stride, rounding); | ||
387 : | interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos + 8, | ||
388 : | pMB->mvs[2].x, pMB->mvs[2].y, stride, rounding); | ||
389 : | interpolate8x8_quarterpel(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos + 8, | ||
390 : | pMB->mvs[3].x, pMB->mvs[3].y, stride, rounding); | ||
391 : | } | ||
392 : | else { | ||
393 : | interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos, | ||
394 : | pMB->mvs[0].x, pMB->mvs[0].y, stride, rounding); | ||
395 : | interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos, | ||
396 : | pMB->mvs[1].x, pMB->mvs[1].y, stride, rounding); | ||
397 : | interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos + 8, | ||
398 : | pMB->mvs[2].x, pMB->mvs[2].y, stride, rounding); | ||
399 : | interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos + 8, | ||
400 : | pMB->mvs[3].x, pMB->mvs[3].y, stride, rounding); | ||
401 : | } | ||
402 : | |||
403 : | edgomez | 195 | interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8 * x_pos, 8 * y_pos, |
404 : | uv_dx, uv_dy, stride2, rounding); | ||
405 : | interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8 * x_pos, 8 * y_pos, | ||
406 : | uv_dx, uv_dy, stride2, rounding); | ||
407 : | Isibaar | 3 | stop_comp_timer(); |
408 : | |||
409 : | edgomez | 195 | for (i = 0; i < 6; i++) { |
410 : | if (cbp & (1 << (5 - i))) // coded | ||
411 : | Isibaar | 3 | { |
412 : | edgomez | 195 | memset(&block[i * 64], 0, 64 * sizeof(int16_t)); // clear |
413 : | Isibaar | 3 | |
414 : | start_timer(); | ||
415 : | edgomez | 195 | get_inter_block(bs, &block[i * 64]); |
416 : | Isibaar | 3 | stop_coding_timer(); |
417 : | |||
418 : | start_timer(); | ||
419 : | edgomez | 195 | if (dec->quant_type == 0) { |
420 : | dequant_inter(&data[i * 64], &block[i * 64], iQuant); | ||
421 : | } else { | ||
422 : | dequant4_inter(&data[i * 64], &block[i * 64], iQuant); | ||
423 : | Isibaar | 3 | } |
424 : | stop_iquant_timer(); | ||
425 : | |||
426 : | start_timer(); | ||
427 : | edgomez | 195 | idct(&data[i * 64]); |
428 : | Isibaar | 3 | stop_idct_timer(); |
429 : | } | ||
430 : | } | ||
431 : | h | 69 | |
432 : | edgomez | 195 | if (dec->interlacing && pMB->field_dct) { |
433 : | h | 81 | next_block = stride; |
434 : | stride *= 2; | ||
435 : | h | 69 | } |
436 : | |||
437 : | start_timer(); | ||
438 : | if (cbp & 32) | ||
439 : | edgomez | 195 | transfer_16to8add(pY_Cur, &data[0 * 64], stride); |
440 : | h | 69 | if (cbp & 16) |
441 : | edgomez | 195 | transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride); |
442 : | h | 69 | if (cbp & 8) |
443 : | edgomez | 195 | transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride); |
444 : | h | 69 | if (cbp & 4) |
445 : | edgomez | 195 | transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride); |
446 : | h | 69 | if (cbp & 2) |
447 : | edgomez | 195 | transfer_16to8add(pU_Cur, &data[4 * 64], stride2); |
448 : | h | 69 | if (cbp & 1) |
449 : | edgomez | 195 | transfer_16to8add(pV_Cur, &data[5 * 64], stride2); |
450 : | h | 69 | stop_transfer_timer(); |
451 : | Isibaar | 3 | } |
452 : | |||
453 : | |||
454 : | edgomez | 195 | void |
455 : | decoder_iframe(DECODER * dec, | ||
456 : | Bitstream * bs, | ||
457 : | int quant, | ||
458 : | int intra_dc_threshold) | ||
459 : | Isibaar | 3 | { |
460 : | chenm001 | 272 | uint32_t bound; |
461 : | Isibaar | 3 | uint32_t x, y; |
462 : | |||
463 : | chenm001 | 272 | bound = 0; |
464 : | |||
465 : | edgomez | 195 | for (y = 0; y < dec->mb_height; y++) { |
466 : | for (x = 0; x < dec->mb_width; x++) { | ||
467 : | chenm001 | 272 | MACROBLOCK *mb; |
468 : | Isibaar | 3 | uint32_t mcbpc; |
469 : | uint32_t cbpc; | ||
470 : | uint32_t acpred_flag; | ||
471 : | uint32_t cbpy; | ||
472 : | uint32_t cbp; | ||
473 : | |||
474 : | chenm001 | 272 | while (BitstreamShowBits(bs, 9) == 1) |
475 : | BitstreamSkip(bs, 9); | ||
476 : | |||
477 : | if (check_resync_marker(bs, 0)) | ||
478 : | { | ||
479 : | bound = read_video_packet_header(bs, 0, &quant); | ||
480 : | x = bound % dec->mb_width; | ||
481 : | y = bound / dec->mb_width; | ||
482 : | } | ||
483 : | mb = &dec->mbs[y * dec->mb_width + x]; | ||
484 : | |||
485 : | DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32)); | ||
486 : | |||
487 : | Isibaar | 3 | mcbpc = get_mcbpc_intra(bs); |
488 : | mb->mode = mcbpc & 7; | ||
489 : | cbpc = (mcbpc >> 4); | ||
490 : | |||
491 : | acpred_flag = BitstreamGetBit(bs); | ||
492 : | |||
493 : | cbpy = get_cbpy(bs, 1); | ||
494 : | cbp = (cbpy << 2) | cbpc; | ||
495 : | |||
496 : | edgomez | 195 | if (mb->mode == MODE_INTRA_Q) { |
497 : | quant += dquant_table[BitstreamGetBits(bs, 2)]; | ||
498 : | if (quant > 31) { | ||
499 : | Isibaar | 3 | quant = 31; |
500 : | edgomez | 195 | } else if (quant < 1) { |
501 : | Isibaar | 3 | quant = 1; |
502 : | } | ||
503 : | } | ||
504 : | mb->quant = quant; | ||
505 : | chenm001 | 297 | mb->mvs[0].x = mb->mvs[0].y = |
506 : | mb->mvs[1].x = mb->mvs[1].y = | ||
507 : | mb->mvs[2].x = mb->mvs[2].y = | ||
508 : | mb->mvs[3].x = mb->mvs[3].y =0; | ||
509 : | Isibaar | 3 | |
510 : | edgomez | 195 | if (dec->interlacing) { |
511 : | h | 69 | mb->field_dct = BitstreamGetBit(bs); |
512 : | edgomez | 514 | DPRINTF(DPRINTF_DEBUG, "deci: field_dct: %d", mb->field_dct); |
513 : | h | 69 | } |
514 : | |||
515 : | edgomez | 195 | decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, |
516 : | chenm001 | 272 | intra_dc_threshold, bound); |
517 : | Isibaar | 3 | } |
518 : | albeu | 315 | if(dec->out_frm) |
519 : | output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,0,y,dec->mb_width); | ||
520 : | |||
521 : | Isibaar | 3 | } |
522 : | edgomez | 78 | |
523 : | Isibaar | 3 | } |
524 : | |||
525 : | |||
526 : | edgomez | 195 | void |
527 : | get_motion_vector(DECODER * dec, | ||
528 : | Bitstream * bs, | ||
529 : | int x, | ||
530 : | int y, | ||
531 : | int k, | ||
532 : | VECTOR * mv, | ||
533 : | chenm001 | 272 | int fcode, |
534 : | const int bound) | ||
535 : | Isibaar | 3 | { |
536 : | edgomez | 78 | |
537 : | Isibaar | 3 | int scale_fac = 1 << (fcode - 1); |
538 : | int high = (32 * scale_fac) - 1; | ||
539 : | int low = ((-32) * scale_fac); | ||
540 : | int range = (64 * scale_fac); | ||
541 : | |||
542 : | chenm001 | 272 | VECTOR pmv; |
543 : | Isibaar | 3 | int mv_x, mv_y; |
544 : | |||
545 : | chenm001 | 272 | pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k); |
546 : | Isibaar | 3 | |
547 : | mv_x = get_mv(bs, fcode); | ||
548 : | mv_y = get_mv(bs, fcode); | ||
549 : | edgomez | 195 | |
550 : | chenm001 | 272 | DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i)", mv_x, mv_y, pmv.x, pmv.y); |
551 : | suxen_drol | 252 | |
552 : | chenm001 | 272 | mv_x += pmv.x; |
553 : | mv_y += pmv.y; | ||
554 : | |||
555 : | edgomez | 195 | if (mv_x < low) { |
556 : | Isibaar | 3 | mv_x += range; |
557 : | edgomez | 195 | } else if (mv_x > high) { |
558 : | Isibaar | 3 | mv_x -= range; |
559 : | } | ||
560 : | |||
561 : | edgomez | 195 | if (mv_y < low) { |
562 : | Isibaar | 3 | mv_y += range; |
563 : | edgomez | 195 | } else if (mv_y > high) { |
564 : | Isibaar | 3 | mv_y -= range; |
565 : | } | ||
566 : | |||
567 : | mv->x = mv_x; | ||
568 : | mv->y = mv_y; | ||
569 : | |||
570 : | } | ||
571 : | |||
572 : | |||
573 : | edgomez | 195 | void |
574 : | decoder_pframe(DECODER * dec, | ||
575 : | Bitstream * bs, | ||
576 : | int rounding, | ||
577 : | int quant, | ||
578 : | int fcode, | ||
579 : | int intra_dc_threshold) | ||
580 : | Isibaar | 3 | { |
581 : | edgomez | 78 | |
582 : | Isibaar | 3 | uint32_t x, y; |
583 : | chenm001 | 272 | uint32_t bound; |
584 : | albeu | 315 | int cp_mb, st_mb; |
585 : | Isibaar | 3 | |
586 : | start_timer(); | ||
587 : | edgomez | 195 | image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
588 : | dec->width, dec->height, dec->interlacing); | ||
589 : | Isibaar | 3 | stop_edges_timer(); |
590 : | |||
591 : | chenm001 | 272 | bound = 0; |
592 : | |||
593 : | edgomez | 195 | for (y = 0; y < dec->mb_height; y++) { |
594 : | albeu | 315 | cp_mb = st_mb = 0; |
595 : | edgomez | 195 | for (x = 0; x < dec->mb_width; x++) { |
596 : | chenm001 | 272 | MACROBLOCK *mb; |
597 : | Isibaar | 3 | |
598 : | chenm001 | 272 | // skip stuffing |
599 : | while (BitstreamShowBits(bs, 10) == 1) | ||
600 : | BitstreamSkip(bs, 10); | ||
601 : | |||
602 : | if (check_resync_marker(bs, fcode - 1)) | ||
603 : | { | ||
604 : | bound = read_video_packet_header(bs, fcode - 1, &quant); | ||
605 : | x = bound % dec->mb_width; | ||
606 : | y = bound / dec->mb_width; | ||
607 : | } | ||
608 : | mb = &dec->mbs[y * dec->mb_width + x]; | ||
609 : | |||
610 : | DPRINTF(DPRINTF_MB, "macroblock (%i,%i) %08x", x, y, BitstreamShowBits(bs, 32)); | ||
611 : | |||
612 : | edgomez | 195 | //if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs))) // not_coded |
613 : | if (!(BitstreamGetBit(bs))) // not_coded | ||
614 : | Isibaar | 3 | { |
615 : | uint32_t mcbpc; | ||
616 : | uint32_t cbpc; | ||
617 : | uint32_t acpred_flag; | ||
618 : | uint32_t cbpy; | ||
619 : | uint32_t cbp; | ||
620 : | uint32_t intra; | ||
621 : | |||
622 : | albeu | 315 | cp_mb++; |
623 : | Isibaar | 3 | mcbpc = get_mcbpc_inter(bs); |
624 : | mb->mode = mcbpc & 7; | ||
625 : | cbpc = (mcbpc >> 4); | ||
626 : | chenm001 | 272 | |
627 : | DPRINTF(DPRINTF_MB, "mode %i", mb->mode); | ||
628 : | DPRINTF(DPRINTF_MB, "cbpc %i", cbpc); | ||
629 : | edgomez | 12 | acpred_flag = 0; |
630 : | Isibaar | 3 | |
631 : | intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q); | ||
632 : | edgomez | 195 | |
633 : | if (intra) { | ||
634 : | Isibaar | 3 | acpred_flag = BitstreamGetBit(bs); |
635 : | } | ||
636 : | |||
637 : | cbpy = get_cbpy(bs, intra); | ||
638 : | chenm001 | 272 | DPRINTF(DPRINTF_MB, "cbpy %i", cbpy); |
639 : | |||
640 : | Isibaar | 3 | cbp = (cbpy << 2) | cbpc; |
641 : | |||
642 : | edgomez | 195 | if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) { |
643 : | chenm001 | 272 | int dquant = dquant_table[BitstreamGetBits(bs, 2)]; |
644 : | DPRINTF(DPRINTF_MB, "dquant %i", dquant); | ||
645 : | quant += dquant; | ||
646 : | edgomez | 195 | if (quant > 31) { |
647 : | Isibaar | 3 | quant = 31; |
648 : | chenm001 | 272 | } else if (quant < 1) { |
649 : | Isibaar | 3 | quant = 1; |
650 : | } | ||
651 : | chenm001 | 272 | DPRINTF(DPRINTF_MB, "quant %i", quant); |
652 : | Isibaar | 3 | } |
653 : | mb->quant = quant; | ||
654 : | h | 69 | |
655 : | edgomez | 195 | if (dec->interlacing) { |
656 : | h | 388 | if (cbp || intra) { |
657 : | mb->field_dct = BitstreamGetBit(bs); | ||
658 : | edgomez | 514 | DPRINTF(DPRINTF_DEBUG, "decp: field_dct: %d", mb->field_dct); |
659 : | h | 388 | } |
660 : | Isibaar | 3 | |
661 : | edgomez | 195 | if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) { |
662 : | h | 69 | mb->field_pred = BitstreamGetBit(bs); |
663 : | edgomez | 514 | DPRINTF(DPRINTF_DEBUG, "decp: field_pred: %d", mb->field_pred); |
664 : | h | 69 | |
665 : | edgomez | 195 | if (mb->field_pred) { |
666 : | h | 69 | mb->field_for_top = BitstreamGetBit(bs); |
667 : | edgomez | 514 | DPRINTF(DPRINTF_DEBUG, "decp: field_for_top: %d", mb->field_for_top); |
668 : | h | 69 | mb->field_for_bot = BitstreamGetBit(bs); |
669 : | edgomez | 514 | DPRINTF(DPRINTF_DEBUG, "decp: field_for_bot: %d", mb->field_for_bot); |
670 : | h | 69 | } |
671 : | } | ||
672 : | Isibaar | 3 | } |
673 : | h | 69 | |
674 : | edgomez | 195 | if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) { |
675 : | if (dec->interlacing && mb->field_pred) { | ||
676 : | get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], | ||
677 : | chenm001 | 272 | fcode, bound); |
678 : | edgomez | 195 | get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1], |
679 : | chenm001 | 272 | fcode, bound); |
680 : | edgomez | 195 | } else { |
681 : | get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], | ||
682 : | chenm001 | 272 | fcode, bound); |
683 : | edgomez | 195 | mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = |
684 : | mb->mvs[0].x; | ||
685 : | mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = | ||
686 : | mb->mvs[0].y; | ||
687 : | h | 69 | } |
688 : | chl | 339 | } else if (mb->mode == MODE_INTER4V ) { |
689 : | |||
690 : | chenm001 | 272 | get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode, bound); |
691 : | get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode, bound); | ||
692 : | get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode, bound); | ||
693 : | get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode, bound); | ||
694 : | edgomez | 195 | } else // MODE_INTRA, MODE_INTRA_Q |
695 : | Isibaar | 3 | { |
696 : | edgomez | 195 | mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = |
697 : | 0; | ||
698 : | mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = | ||
699 : | 0; | ||
700 : | decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, | ||
701 : | chenm001 | 272 | intra_dc_threshold, bound); |
702 : | Isibaar | 3 | continue; |
703 : | } | ||
704 : | |||
705 : | edgomez | 195 | decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant, |
706 : | rounding); | ||
707 : | } else // not coded | ||
708 : | Isibaar | 3 | { |
709 : | edgomez | 514 | DPRINTF(DPRINTF_DEBUG, "P-frame MB at (X,Y)=(%d,%d)", x, y); |
710 : | |||
711 : | chenm001 | 156 | mb->mode = MODE_NOT_CODED; |
712 : | Isibaar | 3 | mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0; |
713 : | mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0; | ||
714 : | edgomez | 195 | |
715 : | Isibaar | 3 | // copy macroblock directly from ref to cur |
716 : | |||
717 : | start_timer(); | ||
718 : | |||
719 : | edgomez | 195 | transfer8x8_copy(dec->cur.y + (16 * y) * dec->edged_width + |
720 : | (16 * x), | ||
721 : | dec->refn[0].y + (16 * y) * dec->edged_width + | ||
722 : | (16 * x), dec->edged_width); | ||
723 : | Isibaar | 3 | |
724 : | edgomez | 195 | transfer8x8_copy(dec->cur.y + (16 * y) * dec->edged_width + |
725 : | (16 * x + 8), | ||
726 : | dec->refn[0].y + (16 * y) * dec->edged_width + | ||
727 : | (16 * x + 8), dec->edged_width); | ||
728 : | Isibaar | 3 | |
729 : | edgomez | 195 | transfer8x8_copy(dec->cur.y + (16 * y + 8) * dec->edged_width + |
730 : | (16 * x), | ||
731 : | dec->refn[0].y + (16 * y + | ||
732 : | 8) * dec->edged_width + | ||
733 : | (16 * x), dec->edged_width); | ||
734 : | Isibaar | 3 | |
735 : | edgomez | 195 | transfer8x8_copy(dec->cur.y + (16 * y + 8) * dec->edged_width + |
736 : | (16 * x + 8), | ||
737 : | dec->refn[0].y + (16 * y + | ||
738 : | 8) * dec->edged_width + | ||
739 : | (16 * x + 8), dec->edged_width); | ||
740 : | Isibaar | 3 | |
741 : | edgomez | 195 | transfer8x8_copy(dec->cur.u + (8 * y) * dec->edged_width / 2 + |
742 : | (8 * x), | ||
743 : | dec->refn[0].u + | ||
744 : | (8 * y) * dec->edged_width / 2 + (8 * x), | ||
745 : | dec->edged_width / 2); | ||
746 : | Isibaar | 3 | |
747 : | edgomez | 195 | transfer8x8_copy(dec->cur.v + (8 * y) * dec->edged_width / 2 + |
748 : | (8 * x), | ||
749 : | dec->refn[0].v + | ||
750 : | (8 * y) * dec->edged_width / 2 + (8 * x), | ||
751 : | dec->edged_width / 2); | ||
752 : | Isibaar | 3 | stop_transfer_timer(); |
753 : | albeu | 315 | if(dec->out_frm && cp_mb > 0) { |
754 : | output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb); | ||
755 : | cp_mb = 0; | ||
756 : | } | ||
757 : | st_mb = x+1; | ||
758 : | Isibaar | 3 | } |
759 : | } | ||
760 : | albeu | 315 | if(dec->out_frm && cp_mb > 0) |
761 : | output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb); | ||
762 : | Isibaar | 3 | } |
763 : | } | ||
764 : | |||
765 : | chenm001 | 156 | |
766 : | // add by MinChen <chenm001@163.com> | ||
767 : | // decode B-frame motion vector | ||
768 : | edgomez | 195 | void |
769 : | get_b_motion_vector(DECODER * dec, | ||
770 : | Bitstream * bs, | ||
771 : | int x, | ||
772 : | int y, | ||
773 : | VECTOR * mv, | ||
774 : | int fcode, | ||
775 : | const VECTOR pmv) | ||
776 : | chenm001 | 156 | { |
777 : | int scale_fac = 1 << (fcode - 1); | ||
778 : | int high = (32 * scale_fac) - 1; | ||
779 : | int low = ((-32) * scale_fac); | ||
780 : | int range = (64 * scale_fac); | ||
781 : | |||
782 : | int mv_x, mv_y; | ||
783 : | int pmv_x, pmv_y; | ||
784 : | |||
785 : | pmv_x = pmv.x; | ||
786 : | pmv_y = pmv.y; | ||
787 : | |||
788 : | mv_x = get_mv(bs, fcode); | ||
789 : | mv_y = get_mv(bs, fcode); | ||
790 : | edgomez | 195 | |
791 : | chenm001 | 156 | mv_x += pmv_x; |
792 : | mv_y += pmv_y; | ||
793 : | |||
794 : | edgomez | 195 | if (mv_x < low) { |
795 : | chenm001 | 156 | mv_x += range; |
796 : | edgomez | 195 | } else if (mv_x > high) { |
797 : | chenm001 | 156 | mv_x -= range; |
798 : | } | ||
799 : | |||
800 : | edgomez | 195 | if (mv_y < low) { |
801 : | chenm001 | 156 | mv_y += range; |
802 : | edgomez | 195 | } else if (mv_y > high) { |
803 : | chenm001 | 156 | mv_y -= range; |
804 : | } | ||
805 : | |||
806 : | mv->x = mv_x; | ||
807 : | mv->y = mv_y; | ||
808 : | } | ||
809 : | |||
810 : | |||
811 : | // add by MinChen <chenm001@163.com> | ||
812 : | // decode an B-frame forward & backward inter macroblock | ||
813 : | edgomez | 195 | void |
814 : | decoder_bf_mbinter(DECODER * dec, | ||
815 : | const MACROBLOCK * pMB, | ||
816 : | const uint32_t x_pos, | ||
817 : | const uint32_t y_pos, | ||
818 : | const uint32_t cbp, | ||
819 : | Bitstream * bs, | ||
820 : | const uint32_t quant, | ||
821 : | const uint8_t ref) | ||
822 : | chenm001 | 156 | { |
823 : | |||
824 : | edgomez | 195 | DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE); |
825 : | chenm001 | 156 | DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); |
826 : | |||
827 : | uint32_t stride = dec->edged_width; | ||
828 : | uint32_t stride2 = stride / 2; | ||
829 : | uint32_t next_block = stride * 8; | ||
830 : | uint32_t i; | ||
831 : | uint32_t iQuant = pMB->quant; | ||
832 : | uint8_t *pY_Cur, *pU_Cur, *pV_Cur; | ||
833 : | int uv_dx, uv_dy; | ||
834 : | |||
835 : | pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); | ||
836 : | pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); | ||
837 : | pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); | ||
838 : | |||
839 : | chenm001 | 297 | |
840 : | edgomez | 195 | if (!(pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) { |
841 : | chenm001 | 156 | uv_dx = pMB->mvs[0].x; |
842 : | uv_dy = pMB->mvs[0].y; | ||
843 : | |||
844 : | uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2; | ||
845 : | uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2; | ||
846 : | edgomez | 195 | } else { |
847 : | chenm001 | 156 | int sum; |
848 : | edgomez | 195 | |
849 : | chenm001 | 156 | sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
850 : | edgomez | 195 | uv_dx = |
851 : | (sum == | ||
852 : | 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + | ||
853 : | (ABS(sum) / 16) * 2)); | ||
854 : | chenm001 | 156 | |
855 : | sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; | ||
856 : | edgomez | 195 | uv_dy = |
857 : | (sum == | ||
858 : | 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + | ||
859 : | (ABS(sum) / 16) * 2)); | ||
860 : | chenm001 | 156 | } |
861 : | |||
862 : | start_timer(); | ||
863 : | edgomez | 195 | interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos, 16 * y_pos, |
864 : | pMB->mvs[0].x, pMB->mvs[0].y, stride, 0); | ||
865 : | interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos + 8, | ||
866 : | 16 * y_pos, pMB->mvs[1].x, pMB->mvs[1].y, stride, 0); | ||
867 : | interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos, | ||
868 : | 16 * y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride, | ||
869 : | 0); | ||
870 : | interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos + 8, | ||
871 : | 16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride, | ||
872 : | 0); | ||
873 : | interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos, | ||
874 : | uv_dx, uv_dy, stride2, 0); | ||
875 : | interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos, | ||
876 : | uv_dx, uv_dy, stride2, 0); | ||
877 : | chenm001 | 156 | stop_comp_timer(); |
878 : | |||
879 : | edgomez | 195 | for (i = 0; i < 6; i++) { |
880 : | if (cbp & (1 << (5 - i))) // coded | ||
881 : | chenm001 | 156 | { |
882 : | edgomez | 195 | memset(&block[i * 64], 0, 64 * sizeof(int16_t)); // clear |
883 : | chenm001 | 156 | |
884 : | start_timer(); | ||
885 : | edgomez | 195 | get_inter_block(bs, &block[i * 64]); |
886 : | chenm001 | 156 | stop_coding_timer(); |
887 : | |||
888 : | start_timer(); | ||
889 : | edgomez | 195 | if (dec->quant_type == 0) { |
890 : | dequant_inter(&data[i * 64], &block[i * 64], iQuant); | ||
891 : | } else { | ||
892 : | dequant4_inter(&data[i * 64], &block[i * 64], iQuant); | ||
893 : | chenm001 | 156 | } |
894 : | stop_iquant_timer(); | ||
895 : | |||
896 : | start_timer(); | ||
897 : | edgomez | 195 | idct(&data[i * 64]); |
898 : | chenm001 | 156 | stop_idct_timer(); |
899 : | } | ||
900 : | } | ||
901 : | |||
902 : | edgomez | 195 | if (dec->interlacing && pMB->field_dct) { |
903 : | chenm001 | 156 | next_block = stride; |
904 : | stride *= 2; | ||
905 : | } | ||
906 : | |||
907 : | start_timer(); | ||
908 : | if (cbp & 32) | ||
909 : | edgomez | 195 | transfer_16to8add(pY_Cur, &data[0 * 64], stride); |
910 : | chenm001 | 156 | if (cbp & 16) |
911 : | edgomez | 195 | transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride); |
912 : | chenm001 | 156 | if (cbp & 8) |
913 : | edgomez | 195 | transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride); |
914 : | chenm001 | 156 | if (cbp & 4) |
915 : | edgomez | 195 | transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride); |
916 : | chenm001 | 156 | if (cbp & 2) |
917 : | edgomez | 195 | transfer_16to8add(pU_Cur, &data[4 * 64], stride2); |
918 : | chenm001 | 156 | if (cbp & 1) |
919 : | edgomez | 195 | transfer_16to8add(pV_Cur, &data[5 * 64], stride2); |
920 : | chenm001 | 156 | stop_transfer_timer(); |
921 : | } | ||
922 : | |||
923 : | |||
924 : | // add by MinChen <chenm001@163.com> | ||
925 : | // decode an B-frame direct & inter macroblock | ||
926 : | edgomez | 195 | void |
927 : | decoder_bf_interpolate_mbinter(DECODER * dec, | ||
928 : | IMAGE forward, | ||
929 : | IMAGE backward, | ||
930 : | const MACROBLOCK * pMB, | ||
931 : | const uint32_t x_pos, | ||
932 : | const uint32_t y_pos, | ||
933 : | Bitstream * bs) | ||
934 : | chenm001 | 156 | { |
935 : | |||
936 : | edgomez | 195 | DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE); |
937 : | chenm001 | 156 | DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); |
938 : | |||
939 : | edgomez | 195 | uint32_t stride = dec->edged_width; |
940 : | uint32_t stride2 = stride / 2; | ||
941 : | uint32_t next_block = stride * 8; | ||
942 : | uint32_t iQuant = pMB->quant; | ||
943 : | int uv_dx, uv_dy; | ||
944 : | int b_uv_dx, b_uv_dy; | ||
945 : | uint32_t i; | ||
946 : | uint8_t *pY_Cur, *pU_Cur, *pV_Cur; | ||
947 : | chenm001 | 297 | const uint32_t cbp = pMB->cbp; |
948 : | chenm001 | 156 | |
949 : | chenm001 | 161 | pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); |
950 : | pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); | ||
951 : | pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); | ||
952 : | |||
953 : | chenm001 | 297 | |
954 : | edgomez | 195 | if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) { |
955 : | chenm001 | 156 | uv_dx = pMB->mvs[0].x; |
956 : | uv_dy = pMB->mvs[0].y; | ||
957 : | |||
958 : | uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2; | ||
959 : | uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2; | ||
960 : | |||
961 : | b_uv_dx = pMB->b_mvs[0].x; | ||
962 : | b_uv_dy = pMB->b_mvs[0].y; | ||
963 : | |||
964 : | b_uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2; | ||
965 : | b_uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2; | ||
966 : | edgomez | 195 | } else { |
967 : | chenm001 | 156 | int sum; |
968 : | edgomez | 195 | |
969 : | chenm001 | 156 | sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
970 : | edgomez | 195 | uv_dx = |
971 : | (sum == | ||
972 : | 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + | ||
973 : | (ABS(sum) / 16) * 2)); | ||
974 : | chenm001 | 156 | |
975 : | sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; | ||
976 : | edgomez | 195 | uv_dy = |
977 : | (sum == | ||
978 : | 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + | ||
979 : | (ABS(sum) / 16) * 2)); | ||
980 : | chenm001 | 156 | |
981 : | edgomez | 195 | sum = |
982 : | pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + | ||
983 : | pMB->b_mvs[3].x; | ||
984 : | b_uv_dx = | ||
985 : | (sum == | ||
986 : | 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + | ||
987 : | (ABS(sum) / 16) * 2)); | ||
988 : | chenm001 | 156 | |
989 : | edgomez | 195 | sum = |
990 : | pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + | ||
991 : | pMB->b_mvs[3].y; | ||
992 : | b_uv_dy = | ||
993 : | (sum == | ||
994 : | 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + | ||
995 : | (ABS(sum) / 16) * 2)); | ||
996 : | chenm001 | 156 | } |
997 : | |||
998 : | edgomez | 195 | |
999 : | chenm001 | 156 | start_timer(); |
1000 : | edgomez | 195 | interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos, |
1001 : | pMB->mvs[0].x, pMB->mvs[0].y, stride, 0); | ||
1002 : | interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, 16 * y_pos, | ||
1003 : | pMB->mvs[1].x, pMB->mvs[1].y, stride, 0); | ||
1004 : | interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos + 8, | ||
1005 : | pMB->mvs[2].x, pMB->mvs[2].y, stride, 0); | ||
1006 : | interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, | ||
1007 : | 16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride, | ||
1008 : | 0); | ||
1009 : | interpolate8x8_switch(dec->cur.u, forward.u, 8 * x_pos, 8 * y_pos, uv_dx, | ||
1010 : | uv_dy, stride2, 0); | ||
1011 : | interpolate8x8_switch(dec->cur.v, forward.v, 8 * x_pos, 8 * y_pos, uv_dx, | ||
1012 : | uv_dy, stride2, 0); | ||
1013 : | chenm001 | 156 | |
1014 : | chenm001 | 161 | |
1015 : | edgomez | 195 | interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos, 16 * y_pos, |
1016 : | pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0); | ||
1017 : | interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8, | ||
1018 : | 16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, | ||
1019 : | 0); | ||
1020 : | interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos, | ||
1021 : | 16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y, | ||
1022 : | stride, 0); | ||
1023 : | interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8, | ||
1024 : | 16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y, | ||
1025 : | stride, 0); | ||
1026 : | interpolate8x8_switch(dec->refn[2].u, backward.u, 8 * x_pos, 8 * y_pos, | ||
1027 : | b_uv_dx, b_uv_dy, stride2, 0); | ||
1028 : | interpolate8x8_switch(dec->refn[2].v, backward.v, 8 * x_pos, 8 * y_pos, | ||
1029 : | b_uv_dx, b_uv_dy, stride2, 0); | ||
1030 : | chenm001 | 156 | |
1031 : | edgomez | 195 | interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos, 16 * y_pos, |
1032 : | stride); | ||
1033 : | interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos + 8, 16 * y_pos, | ||
1034 : | stride); | ||
1035 : | interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos, 16 * y_pos + 8, | ||
1036 : | stride); | ||
1037 : | interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos + 8, | ||
1038 : | 16 * y_pos + 8, stride); | ||
1039 : | interpolate8x8_c(dec->cur.u, dec->refn[2].u, 8 * x_pos, 8 * y_pos, | ||
1040 : | stride2); | ||
1041 : | interpolate8x8_c(dec->cur.v, dec->refn[2].v, 8 * x_pos, 8 * y_pos, | ||
1042 : | stride2); | ||
1043 : | chenm001 | 156 | stop_comp_timer(); |
1044 : | edgomez | 195 | |
1045 : | for (i = 0; i < 6; i++) { | ||
1046 : | if (cbp & (1 << (5 - i))) // coded | ||
1047 : | chenm001 | 161 | { |
1048 : | edgomez | 195 | memset(&block[i * 64], 0, 64 * sizeof(int16_t)); // clear |
1049 : | chenm001 | 156 | |
1050 : | chenm001 | 161 | start_timer(); |
1051 : | edgomez | 195 | get_inter_block(bs, &block[i * 64]); |
1052 : | chenm001 | 161 | stop_coding_timer(); |
1053 : | |||
1054 : | start_timer(); | ||
1055 : | edgomez | 195 | if (dec->quant_type == 0) { |
1056 : | dequant_inter(&data[i * 64], &block[i * 64], iQuant); | ||
1057 : | } else { | ||
1058 : | dequant4_inter(&data[i * 64], &block[i * 64], iQuant); | ||
1059 : | chenm001 | 161 | } |
1060 : | stop_iquant_timer(); | ||
1061 : | |||
1062 : | start_timer(); | ||
1063 : | edgomez | 195 | idct(&data[i * 64]); |
1064 : | chenm001 | 161 | stop_idct_timer(); |
1065 : | } | ||
1066 : | } | ||
1067 : | |||
1068 : | edgomez | 195 | if (dec->interlacing && pMB->field_dct) { |
1069 : | chenm001 | 161 | next_block = stride; |
1070 : | stride *= 2; | ||
1071 : | } | ||
1072 : | |||
1073 : | start_timer(); | ||
1074 : | if (cbp & 32) | ||
1075 : | edgomez | 195 | transfer_16to8add(pY_Cur, &data[0 * 64], stride); |
1076 : | chenm001 | 161 | if (cbp & 16) |
1077 : | edgomez | 195 | transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride); |
1078 : | chenm001 | 161 | if (cbp & 8) |
1079 : | edgomez | 195 | transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride); |
1080 : | chenm001 | 161 | if (cbp & 4) |
1081 : | edgomez | 195 | transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride); |
1082 : | chenm001 | 161 | if (cbp & 2) |
1083 : | edgomez | 195 | transfer_16to8add(pU_Cur, &data[4 * 64], stride2); |
1084 : | chenm001 | 161 | if (cbp & 1) |
1085 : | edgomez | 195 | transfer_16to8add(pV_Cur, &data[5 * 64], stride2); |
1086 : | chenm001 | 161 | stop_transfer_timer(); |
1087 : | chenm001 | 156 | } |
1088 : | |||
1089 : | |||
1090 : | // add by MinChen <chenm001@163.com> | ||
1091 : | // for decode B-frame dbquant | ||
1092 : | edgomez | 195 | int32_t __inline |
1093 : | get_dbquant(Bitstream * bs) | ||
1094 : | chenm001 | 156 | { |
1095 : | edgomez | 195 | if (!BitstreamGetBit(bs)) // '0' |
1096 : | return (0); | ||
1097 : | chenm001 | 156 | else if (!BitstreamGetBit(bs)) // '10' |
1098 : | edgomez | 195 | return (-2); |
1099 : | chenm001 | 156 | else |
1100 : | edgomez | 195 | return (2); // '11' |
1101 : | chenm001 | 156 | } |
1102 : | |||
1103 : | // add by MinChen <chenm001@163.com> | ||
1104 : | // for decode B-frame mb_type | ||
1105 : | // bit ret_value | ||
1106 : | // 1 0 | ||
1107 : | // 01 1 | ||
1108 : | // 001 2 | ||
1109 : | // 0001 3 | ||
1110 : | edgomez | 195 | int32_t __inline |
1111 : | get_mbtype(Bitstream * bs) | ||
1112 : | chenm001 | 156 | { |
1113 : | chenm001 | 161 | int32_t mb_type; |
1114 : | edgomez | 195 | |
1115 : | for (mb_type = 0; mb_type <= 3; mb_type++) { | ||
1116 : | if (BitstreamGetBit(bs)) | ||
1117 : | chenm001 | 161 | break; |
1118 : | } | ||
1119 : | |||
1120 : | edgomez | 195 | if (mb_type <= 3) |
1121 : | return (mb_type); | ||
1122 : | chenm001 | 156 | else |
1123 : | edgomez | 195 | return (-1); |
1124 : | chenm001 | 156 | } |
1125 : | |||
1126 : | edgomez | 195 | void |
1127 : | decoder_bframe(DECODER * dec, | ||
1128 : | Bitstream * bs, | ||
1129 : | int quant, | ||
1130 : | int fcode_forward, | ||
1131 : | int fcode_backward) | ||
1132 : | chenm001 | 156 | { |
1133 : | edgomez | 195 | uint32_t x, y; |
1134 : | chenm001 | 297 | VECTOR mv; |
1135 : | const VECTOR zeromv = {0,0}; | ||
1136 : | chenm001 | 290 | #ifdef BFRAMES_DEC_DEBUG |
1137 : | FILE *fp; | ||
1138 : | static char first=0; | ||
1139 : | #define BFRAME_DEBUG if (!first && fp){ \ | ||
1140 : | fprintf(fp,"Y=%3d X=%3d MB=%2d CBP=%02X\n",y,x,mb->mb_type,mb->cbp); \ | ||
1141 : | } | ||
1142 : | #endif | ||
1143 : | chenm001 | 156 | |
1144 : | start_timer(); | ||
1145 : | edgomez | 195 | image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
1146 : | dec->width, dec->height, dec->interlacing); | ||
1147 : | chenm001 | 297 | image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, |
1148 : | dec->width, dec->height, dec->interlacing); | ||
1149 : | chenm001 | 156 | stop_edges_timer(); |
1150 : | |||
1151 : | chenm001 | 290 | #ifdef BFRAMES_DEC_DEBUG |
1152 : | if (!first){ | ||
1153 : | fp=fopen("C:\\XVIDDBG.TXT","w"); | ||
1154 : | } | ||
1155 : | #endif | ||
1156 : | chenm001 | 161 | |
1157 : | edgomez | 195 | for (y = 0; y < dec->mb_height; y++) { |
1158 : | chenm001 | 161 | // Initialize Pred Motion Vector |
1159 : | chenm001 | 297 | dec->p_fmv = dec->p_bmv = zeromv; |
1160 : | edgomez | 195 | for (x = 0; x < dec->mb_width; x++) { |
1161 : | MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x]; | ||
1162 : | MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x]; | ||
1163 : | chenm001 | 156 | |
1164 : | chenm001 | 297 | mv = |
1165 : | mb->b_mvs[0] = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] = | ||
1166 : | mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = zeromv; | ||
1167 : | chenm001 | 161 | |
1168 : | chenm001 | 156 | // the last P_VOP is skip macroblock ? |
1169 : | edgomez | 195 | if (last_mb->mode == MODE_NOT_CODED) { |
1170 : | chenm001 | 161 | //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y); |
1171 : | chenm001 | 290 | mb->cbp = 0; |
1172 : | #ifdef BFRAMES_DEC_DEBUG | ||
1173 : | chenm001 | 296 | mb->mb_type = MODE_NOT_CODED; |
1174 : | chenm001 | 290 | BFRAME_DEBUG |
1175 : | #endif | ||
1176 : | chenm001 | 161 | mb->mb_type = MODE_FORWARD; |
1177 : | chenm001 | 297 | mb->quant = last_mb->quant; |
1178 : | //mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x; | ||
1179 : | //mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y; | ||
1180 : | chenm001 | 156 | |
1181 : | chenm001 | 297 | decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, mb->quant, 1); |
1182 : | chenm001 | 156 | continue; |
1183 : | } | ||
1184 : | chenm001 | 161 | |
1185 : | edgomez | 195 | if (!BitstreamGetBit(bs)) { // modb=='0' |
1186 : | const uint8_t modb2 = BitstreamGetBit(bs); | ||
1187 : | chenm001 | 156 | |
1188 : | mb->mb_type = get_mbtype(bs); | ||
1189 : | edgomez | 195 | |
1190 : | if (!modb2) { // modb=='00' | ||
1191 : | mb->cbp = BitstreamGetBits(bs, 6); | ||
1192 : | chenm001 | 156 | } else { |
1193 : | mb->cbp = 0; | ||
1194 : | } | ||
1195 : | edgomez | 195 | if (mb->mb_type && mb->cbp) { |
1196 : | chenm001 | 156 | quant += get_dbquant(bs); |
1197 : | |||
1198 : | edgomez | 195 | if (quant > 31) { |
1199 : | chenm001 | 156 | quant = 31; |
1200 : | chenm001 | 272 | } else if (quant < 1) { |
1201 : | chenm001 | 156 | quant = 1; |
1202 : | } | ||
1203 : | } | ||
1204 : | } else { | ||
1205 : | mb->mb_type = MODE_DIRECT_NONE_MV; | ||
1206 : | edgomez | 195 | mb->cbp = 0; |
1207 : | chenm001 | 156 | } |
1208 : | |||
1209 : | chenm001 | 297 | mb->quant = quant; |
1210 : | mb->mode = MODE_INTER4V; | ||
1211 : | chenm001 | 156 | //DEBUG1("Switch bm_type=",mb->mb_type); |
1212 : | |||
1213 : | chenm001 | 290 | #ifdef BFRAMES_DEC_DEBUG |
1214 : | BFRAME_DEBUG | ||
1215 : | #endif | ||
1216 : | chl | 326 | |
1217 : | edgomez | 195 | switch (mb->mb_type) { |
1218 : | chenm001 | 156 | case MODE_DIRECT: |
1219 : | chenm001 | 297 | get_b_motion_vector(dec, bs, x, y, &mv, 1, zeromv); |
1220 : | chenm001 | 156 | |
1221 : | case MODE_DIRECT_NONE_MV: | ||
1222 : | chl | 326 | { |
1223 : | chenm001 | 297 | const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp; |
1224 : | chenm001 | 156 | int i; |
1225 : | edgomez | 195 | |
1226 : | for (i = 0; i < 4; i++) { | ||
1227 : | chenm001 | 297 | mb->mvs[i].x = (int32_t) ((TRB * last_mb->mvs[i].x) |
1228 : | / TRD + mv.x); | ||
1229 : | mb->b_mvs[i].x = (int32_t) ((mv.x == 0) | ||
1230 : | ? ((TRB - TRD) * last_mb->mvs[i].x) | ||
1231 : | / TRD | ||
1232 : | : mb->mvs[i].x - last_mb->mvs[i].x); | ||
1233 : | mb->mvs[i].y = (int32_t) ((TRB * last_mb->mvs[i].y) | ||
1234 : | / TRD + mv.y); | ||
1235 : | mb->b_mvs[i].y = (int32_t) ((mv.y == 0) | ||
1236 : | ? ((TRB - TRD) * last_mb->mvs[i].y) | ||
1237 : | / TRD | ||
1238 : | : mb->mvs[i].y - last_mb->mvs[i].y); | ||
1239 : | chenm001 | 156 | } |
1240 : | //DEBUG("B-frame Direct!\n"); | ||
1241 : | } | ||
1242 : | edgomez | 195 | decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], |
1243 : | chenm001 | 297 | mb, x, y, bs); |
1244 : | chenm001 | 156 | break; |
1245 : | |||
1246 : | case MODE_INTERPOLATE: | ||
1247 : | edgomez | 195 | get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward, |
1248 : | dec->p_fmv); | ||
1249 : | chenm001 | 297 | dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0]; |
1250 : | chenm001 | 156 | |
1251 : | edgomez | 195 | get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0], |
1252 : | fcode_backward, dec->p_bmv); | ||
1253 : | chenm001 | 297 | dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] = |
1254 : | mb->b_mvs[3] = mb->b_mvs[0]; | ||
1255 : | chenm001 | 156 | |
1256 : | edgomez | 195 | decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], |
1257 : | chenm001 | 297 | mb, x, y, bs); |
1258 : | chenm001 | 156 | //DEBUG("B-frame Bidir!\n"); |
1259 : | break; | ||
1260 : | |||
1261 : | case MODE_BACKWARD: | ||
1262 : | edgomez | 195 | get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_backward, |
1263 : | dec->p_bmv); | ||
1264 : | chenm001 | 297 | dec->p_bmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0]; |
1265 : | chenm001 | 161 | |
1266 : | chenm001 | 297 | mb->mode = MODE_INTER; |
1267 : | chenm001 | 156 | decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0); |
1268 : | //DEBUG("B-frame Backward!\n"); | ||
1269 : | break; | ||
1270 : | |||
1271 : | case MODE_FORWARD: | ||
1272 : | edgomez | 195 | get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward, |
1273 : | dec->p_fmv); | ||
1274 : | chenm001 | 297 | dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0]; |
1275 : | chenm001 | 161 | |
1276 : | chenm001 | 297 | mb->mode = MODE_INTER; |
1277 : | chenm001 | 156 | decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1); |
1278 : | chenm001 | 161 | //DEBUG("B-frame Forward!\n"); |
1279 : | chenm001 | 156 | break; |
1280 : | |||
1281 : | default: | ||
1282 : | edgomez | 514 | DPRINTF(DPRINTF_ERROR, "Not support B-frame mb_type = %d", mb->mb_type); |
1283 : | chenm001 | 156 | } |
1284 : | |||
1285 : | edgomez | 195 | } // end of FOR |
1286 : | chenm001 | 156 | } |
1287 : | chenm001 | 290 | #ifdef BFRAMES_DEC_DEBUG |
1288 : | if (!first){ | ||
1289 : | first=1; | ||
1290 : | if (fp) | ||
1291 : | fclose(fp); | ||
1292 : | } | ||
1293 : | #endif | ||
1294 : | chenm001 | 156 | } |
1295 : | |||
1296 : | // swap two MACROBLOCK array | ||
1297 : | edgomez | 195 | void |
1298 : | mb_swap(MACROBLOCK ** mb1, | ||
1299 : | MACROBLOCK ** mb2) | ||
1300 : | chenm001 | 156 | { |
1301 : | edgomez | 195 | MACROBLOCK *temp = *mb1; |
1302 : | |||
1303 : | *mb1 = *mb2; | ||
1304 : | *mb2 = temp; | ||
1305 : | chenm001 | 156 | } |
1306 : | |||
1307 : | edgomez | 195 | int |
1308 : | decoder_decode(DECODER * dec, | ||
1309 : | XVID_DEC_FRAME * frame) | ||
1310 : | Isibaar | 3 | { |
1311 : | edgomez | 78 | |
1312 : | Isibaar | 3 | Bitstream bs; |
1313 : | uint32_t rounding; | ||
1314 : | uint32_t quant; | ||
1315 : | chenm001 | 156 | uint32_t fcode_forward; |
1316 : | uint32_t fcode_backward; | ||
1317 : | Isibaar | 3 | uint32_t intra_dc_threshold; |
1318 : | chenm001 | 133 | uint32_t vop_type; |
1319 : | Isibaar | 3 | |
1320 : | start_global_timer(); | ||
1321 : | edgomez | 195 | |
1322 : | albeu | 315 | dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL; |
1323 : | |||
1324 : | Isibaar | 3 | BitstreamInit(&bs, frame->bitstream, frame->length); |
1325 : | |||
1326 : | chenm001 | 133 | // add by chenm001 <chenm001@163.com> |
1327 : | // for support B-frame to reference last 2 frame | ||
1328 : | Isibaar | 179 | dec->frames++; |
1329 : | edgomez | 195 | vop_type = |
1330 : | BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward, | ||
1331 : | &fcode_backward, &intra_dc_threshold); | ||
1332 : | Isibaar | 179 | |
1333 : | edgomez | 195 | dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0; // init pred vector to 0 |
1334 : | |||
1335 : | switch (vop_type) { | ||
1336 : | case P_VOP: | ||
1337 : | decoder_pframe(dec, &bs, rounding, quant, fcode_forward, | ||
1338 : | intra_dc_threshold); | ||
1339 : | chenm001 | 272 | #ifdef BFRAMES_DEC |
1340 : | edgomez | 195 | DEBUG1("P_VOP Time=", dec->time); |
1341 : | chenm001 | 272 | #endif |
1342 : | Isibaar | 3 | break; |
1343 : | |||
1344 : | edgomez | 195 | case I_VOP: |
1345 : | Isibaar | 3 | decoder_iframe(dec, &bs, quant, intra_dc_threshold); |
1346 : | chenm001 | 272 | #ifdef BFRAMES_DEC |
1347 : | edgomez | 195 | DEBUG1("I_VOP Time=", dec->time); |
1348 : | chenm001 | 272 | #endif |
1349 : | Isibaar | 3 | break; |
1350 : | |||
1351 : | edgomez | 195 | case B_VOP: |
1352 : | suxen_drol | 233 | #ifdef BFRAMES_DEC |
1353 : | edgomez | 195 | if (dec->time_pp > dec->time_bp) { |
1354 : | DEBUG1("B_VOP Time=", dec->time); | ||
1355 : | chenm001 | 156 | decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward); |
1356 : | } else { | ||
1357 : | DEBUG("broken B-frame!"); | ||
1358 : | } | ||
1359 : | suxen_drol | 233 | #else |
1360 : | image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); | ||
1361 : | Isibaar | 179 | #endif |
1362 : | Isibaar | 3 | break; |
1363 : | edgomez | 195 | |
1364 : | case N_VOP: // vop not coded | ||
1365 : | suxen_drol | 233 | // when low_delay==0, N_VOP's should interpolate between the past and future frames |
1366 : | image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); | ||
1367 : | Isibaar | 3 | break; |
1368 : | |||
1369 : | edgomez | 195 | default: |
1370 : | Isibaar | 3 | return XVID_ERR_FAIL; |
1371 : | } | ||
1372 : | |||
1373 : | chenm001 | 294 | #ifdef BFRAMES_DEC_DEBUG |
1374 : | chenm001 | 290 | if (frame->length != BitstreamPos(&bs) / 8){ |
1375 : | DEBUG2("InLen/UseLen",frame->length, BitstreamPos(&bs) / 8); | ||
1376 : | } | ||
1377 : | chenm001 | 294 | #endif |
1378 : | Isibaar | 3 | frame->length = BitstreamPos(&bs) / 8; |
1379 : | |||
1380 : | chenm001 | 290 | |
1381 : | suxen_drol | 233 | #ifdef BFRAMES_DEC |
1382 : | chenm001 | 168 | // test if no B_VOP |
1383 : | chenm001 | 356 | if (dec->low_delay || dec->frames == 0) { |
1384 : | Isibaar | 179 | #endif |
1385 : | chenm001 | 290 | image_output(&dec->cur, dec->width, dec->height, dec->edged_width, |
1386 : | edgomez | 195 | frame->image, frame->stride, frame->colorspace); |
1387 : | chenm001 | 290 | |
1388 : | suxen_drol | 233 | #ifdef BFRAMES_DEC |
1389 : | chenm001 | 168 | } else { |
1390 : | chenm001 | 356 | if (dec->frames >= 1) { |
1391 : | chenm001 | 168 | start_timer(); |
1392 : | edgomez | 195 | if ((vop_type == I_VOP || vop_type == P_VOP)) { |
1393 : | image_output(&dec->refn[0], dec->width, dec->height, | ||
1394 : | dec->edged_width, frame->image, frame->stride, | ||
1395 : | frame->colorspace); | ||
1396 : | chenm001 | 168 | } else if (vop_type == B_VOP) { |
1397 : | edgomez | 195 | image_output(&dec->cur, dec->width, dec->height, |
1398 : | dec->edged_width, frame->image, frame->stride, | ||
1399 : | frame->colorspace); | ||
1400 : | chenm001 | 168 | } |
1401 : | stop_conv_timer(); | ||
1402 : | chenm001 | 156 | } |
1403 : | } | ||
1404 : | Isibaar | 179 | #endif |
1405 : | |||
1406 : | edgomez | 195 | if (vop_type == I_VOP || vop_type == P_VOP) { |
1407 : | chenm001 | 156 | image_swap(&dec->refn[0], &dec->refn[1]); |
1408 : | image_swap(&dec->cur, &dec->refn[0]); | ||
1409 : | chenm001 | 306 | |
1410 : | chenm001 | 156 | // swap MACROBLOCK |
1411 : | chenm001 | 306 | // the Divx will not set the low_delay flage some times |
1412 : | // so follow code will wrong to not swap at that time | ||
1413 : | // this will broken bitstream! so I'm change it, | ||
1414 : | // But that is not the best way! can anyone tell me how | ||
1415 : | // to do another way? | ||
1416 : | // 18-07-2002 MinChen<chenm001@163.com> | ||
1417 : | //if (!dec->low_delay && vop_type == P_VOP) | ||
1418 : | if (vop_type == P_VOP) | ||
1419 : | chenm001 | 161 | mb_swap(&dec->mbs, &dec->last_mbs); |
1420 : | chenm001 | 156 | } |
1421 : | |||
1422 : | Isibaar | 3 | emms(); |
1423 : | |||
1424 : | stop_global_timer(); | ||
1425 : | |||
1426 : | return XVID_ERR_OK; | ||
1427 : | } |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |