Parent Directory
|
Revision Log
Revision 148 -
(view)
(download)
Original Path: trunk/xvidcore/src/decoder.c
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 | 133 | * 22.04.2002 add some B-frame decode support; chenm001 <chenm001@163.com> |
36 : | h | 81 | * 29.03.2002 interlacing fix - compensated block wasn't being used when |
37 : | * reconstructing blocks, thus artifacts | ||
38 : | * interlacing speedup - used transfers to re-interlace | ||
39 : | * interlaced decoding should be as fast as progressive now | ||
40 : | h | 69 | * 26.03.2002 interlacing support - moved transfers outside decode loop |
41 : | edgomez | 146 | * 26.12.2001 decoder_mbinter: dequant/idct moved within if(coded) block |
42 : | * 22.12.2001 lock based interpolation | ||
43 : | * 01.12.2001 inital version; (c)2001 peter ross <pross@cs.rmit.edu.au> | ||
44 : | Isibaar | 3 | * |
45 : | edgomez | 148 | * $Id: decoder.c,v 1.13 2002-04-28 23:35:25 edgomez Exp $ |
46 : | edgomez | 146 | * |
47 : | Isibaar | 3 | *************************************************************************/ |
48 : | |||
49 : | #include <stdlib.h> | ||
50 : | edgomez | 146 | #include <string.h> |
51 : | Isibaar | 3 | |
52 : | #include "xvid.h" | ||
53 : | #include "portab.h" | ||
54 : | |||
55 : | #include "decoder.h" | ||
56 : | #include "bitstream/bitstream.h" | ||
57 : | #include "bitstream/mbcoding.h" | ||
58 : | |||
59 : | #include "quant/quant_h263.h" | ||
60 : | #include "quant/quant_mpeg4.h" | ||
61 : | #include "dct/idct.h" | ||
62 : | #include "dct/fdct.h" | ||
63 : | #include "utils/mem_transfer.h" | ||
64 : | #include "image/interpolate8x8.h" | ||
65 : | |||
66 : | #include "bitstream/mbcoding.h" | ||
67 : | #include "prediction/mbprediction.h" | ||
68 : | #include "utils/timer.h" | ||
69 : | #include "utils/emms.h" | ||
70 : | |||
71 : | #include "image/image.h" | ||
72 : | #include "image/colorspace.h" | ||
73 : | Isibaar | 41 | #include "utils/mem_align.h" |
74 : | Isibaar | 3 | |
75 : | int decoder_create(XVID_DEC_PARAM * param) | ||
76 : | { | ||
77 : | DECODER * dec; | ||
78 : | |||
79 : | Isibaar | 42 | dec = xvid_malloc(sizeof(DECODER), CACHE_LINE); |
80 : | Isibaar | 3 | if (dec == NULL) |
81 : | { | ||
82 : | return XVID_ERR_MEMORY; | ||
83 : | } | ||
84 : | param->handle = dec; | ||
85 : | |||
86 : | dec->width = param->width; | ||
87 : | dec->height = param->height; | ||
88 : | |||
89 : | dec->mb_width = (dec->width + 15) / 16; | ||
90 : | dec->mb_height = (dec->height + 15) / 16; | ||
91 : | |||
92 : | dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE; | ||
93 : | dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE; | ||
94 : | |||
95 : | if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) | ||
96 : | { | ||
97 : | Isibaar | 41 | xvid_free(dec); |
98 : | Isibaar | 3 | return XVID_ERR_MEMORY; |
99 : | } | ||
100 : | |||
101 : | chenm001 | 133 | if (image_create(&dec->refn[0], dec->edged_width, dec->edged_height)) |
102 : | Isibaar | 3 | { |
103 : | image_destroy(&dec->cur, dec->edged_width, dec->edged_height); | ||
104 : | Isibaar | 41 | xvid_free(dec); |
105 : | Isibaar | 3 | return XVID_ERR_MEMORY; |
106 : | } | ||
107 : | chenm001 | 133 | // add by chenm001 <chenm001@163.com> |
108 : | // for support B-frame to reference last 2 frame | ||
109 : | if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) | ||
110 : | { | ||
111 : | image_destroy(&dec->cur, dec->edged_width, dec->edged_height); | ||
112 : | image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); | ||
113 : | xvid_free(dec); | ||
114 : | return XVID_ERR_MEMORY; | ||
115 : | } | ||
116 : | Isibaar | 3 | |
117 : | Isibaar | 42 | dec->mbs = xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, CACHE_LINE); |
118 : | Isibaar | 3 | if (dec->mbs == NULL) |
119 : | { | ||
120 : | image_destroy(&dec->cur, dec->edged_width, dec->edged_height); | ||
121 : | Isibaar | 41 | xvid_free(dec); |
122 : | Isibaar | 3 | return XVID_ERR_MEMORY; |
123 : | } | ||
124 : | |||
125 : | init_timer(); | ||
126 : | |||
127 : | return XVID_ERR_OK; | ||
128 : | } | ||
129 : | |||
130 : | |||
131 : | int decoder_destroy(DECODER * dec) | ||
132 : | { | ||
133 : | Isibaar | 41 | xvid_free(dec->mbs); |
134 : | chenm001 | 133 | image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
135 : | Isibaar | 3 | image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
136 : | Isibaar | 41 | xvid_free(dec); |
137 : | Isibaar | 3 | |
138 : | write_timer(); | ||
139 : | return XVID_ERR_OK; | ||
140 : | } | ||
141 : | |||
142 : | |||
143 : | |||
144 : | static const int32_t dquant_table[4] = | ||
145 : | { | ||
146 : | -1, -2, 1, 2 | ||
147 : | }; | ||
148 : | |||
149 : | |||
150 : | // decode an intra macroblock | ||
151 : | |||
152 : | h | 69 | void decoder_mbintra(DECODER * dec, |
153 : | edgomez | 78 | MACROBLOCK * pMB, |
154 : | const uint32_t x_pos, | ||
155 : | const uint32_t y_pos, | ||
156 : | const uint32_t acpred_flag, | ||
157 : | const uint32_t cbp, | ||
158 : | Bitstream * bs, | ||
159 : | const uint32_t quant, | ||
160 : | const uint32_t intra_dc_threshold) | ||
161 : | Isibaar | 3 | { |
162 : | edgomez | 78 | |
163 : | DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE); | ||
164 : | DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); | ||
165 : | |||
166 : | h | 81 | uint32_t stride = dec->edged_width; |
167 : | uint32_t stride2 = stride / 2; | ||
168 : | uint32_t next_block = stride * 8; | ||
169 : | h | 69 | uint32_t i; |
170 : | uint32_t iQuant = pMB->quant; | ||
171 : | uint8_t *pY_Cur, *pU_Cur, *pV_Cur; | ||
172 : | |||
173 : | edgomez | 78 | pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); |
174 : | h | 81 | pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); |
175 : | pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); | ||
176 : | h | 69 | |
177 : | edgomez | 78 | memset(block, 0, 6*64*sizeof(int16_t)); // clear |
178 : | h | 69 | |
179 : | for (i = 0; i < 6; i++) | ||
180 : | Isibaar | 3 | { |
181 : | h | 69 | uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
182 : | Isibaar | 3 | int16_t predictors[8]; |
183 : | int start_coeff; | ||
184 : | |||
185 : | start_timer(); | ||
186 : | edgomez | 78 | predict_acdc(dec->mbs, x_pos, y_pos, dec->mb_width, i, &block[i*64], iQuant, iDcScaler, predictors); |
187 : | Isibaar | 3 | if (!acpred_flag) |
188 : | { | ||
189 : | h | 69 | pMB->acpred_directions[i] = 0; |
190 : | Isibaar | 3 | } |
191 : | stop_prediction_timer(); | ||
192 : | |||
193 : | if (quant < intra_dc_threshold) | ||
194 : | { | ||
195 : | int dc_size; | ||
196 : | int dc_dif; | ||
197 : | |||
198 : | h | 69 | dc_size = i < 4 ? get_dc_size_lum(bs) : get_dc_size_chrom(bs); |
199 : | Isibaar | 3 | dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0 ; |
200 : | |||
201 : | if (dc_size > 8) | ||
202 : | { | ||
203 : | BitstreamSkip(bs, 1); // marker | ||
204 : | } | ||
205 : | |||
206 : | edgomez | 78 | block[i*64 + 0] = dc_dif; |
207 : | Isibaar | 3 | start_coeff = 1; |
208 : | } | ||
209 : | else | ||
210 : | { | ||
211 : | start_coeff = 0; | ||
212 : | } | ||
213 : | |||
214 : | start_timer(); | ||
215 : | h | 69 | if (cbp & (1 << (5-i))) // coded |
216 : | Isibaar | 3 | { |
217 : | edgomez | 78 | get_intra_block(bs, &block[i*64], pMB->acpred_directions[i], start_coeff); |
218 : | Isibaar | 3 | } |
219 : | stop_coding_timer(); | ||
220 : | |||
221 : | start_timer(); | ||
222 : | edgomez | 78 | add_acdc(pMB, i, &block[i*64], iDcScaler, predictors); |
223 : | Isibaar | 3 | stop_prediction_timer(); |
224 : | |||
225 : | start_timer(); | ||
226 : | if (dec->quant_type == 0) | ||
227 : | { | ||
228 : | edgomez | 78 | dequant_intra(&data[i*64], &block[i*64], iQuant, iDcScaler); |
229 : | Isibaar | 3 | } |
230 : | else | ||
231 : | { | ||
232 : | edgomez | 78 | dequant4_intra(&data[i*64], &block[i*64], iQuant, iDcScaler); |
233 : | Isibaar | 3 | } |
234 : | stop_iquant_timer(); | ||
235 : | |||
236 : | start_timer(); | ||
237 : | edgomez | 78 | idct(&data[i*64]); |
238 : | Isibaar | 3 | stop_idct_timer(); |
239 : | h | 69 | } |
240 : | Isibaar | 3 | |
241 : | Isibaar | 110 | if (dec->interlacing && pMB->field_dct) |
242 : | h | 69 | { |
243 : | h | 81 | next_block = stride; |
244 : | stride *= 2; | ||
245 : | Isibaar | 3 | } |
246 : | h | 69 | |
247 : | start_timer(); | ||
248 : | edgomez | 78 | transfer_16to8copy(pY_Cur, &data[0*64], stride); |
249 : | transfer_16to8copy(pY_Cur + 8, &data[1*64], stride); | ||
250 : | h | 81 | transfer_16to8copy(pY_Cur + next_block, &data[2*64], stride); |
251 : | transfer_16to8copy(pY_Cur + 8 + next_block, &data[3*64], stride); | ||
252 : | transfer_16to8copy(pU_Cur, &data[4*64], stride2); | ||
253 : | transfer_16to8copy(pV_Cur, &data[5*64], stride2); | ||
254 : | h | 69 | stop_transfer_timer(); |
255 : | Isibaar | 3 | } |
256 : | |||
257 : | |||
258 : | |||
259 : | |||
260 : | |||
261 : | #define SIGN(X) (((X)>0)?1:-1) | ||
262 : | #define ABS(X) (((X)>0)?(X):-(X)) | ||
263 : | static const uint32_t roundtab[16] = | ||
264 : | edgomez | 78 | { 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 }; |
265 : | Isibaar | 3 | |
266 : | |||
267 : | // decode an inter macroblock | ||
268 : | |||
269 : | h | 69 | void decoder_mbinter(DECODER * dec, |
270 : | edgomez | 78 | const MACROBLOCK * pMB, |
271 : | const uint32_t x_pos, | ||
272 : | const uint32_t y_pos, | ||
273 : | const uint32_t acpred_flag, | ||
274 : | const uint32_t cbp, | ||
275 : | Bitstream * bs, | ||
276 : | const uint32_t quant, | ||
277 : | const uint32_t rounding) | ||
278 : | Isibaar | 3 | { |
279 : | h | 69 | |
280 : | edgomez | 78 | DECLARE_ALIGNED_MATRIX(block,6, 64, int16_t, CACHE_LINE); |
281 : | DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); | ||
282 : | |||
283 : | h | 81 | uint32_t stride = dec->edged_width; |
284 : | uint32_t stride2 = stride / 2; | ||
285 : | uint32_t next_block = stride * 8; | ||
286 : | edgomez | 78 | uint32_t i; |
287 : | uint32_t iQuant = pMB->quant; | ||
288 : | h | 69 | uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
289 : | Isibaar | 3 | int uv_dx, uv_dy; |
290 : | |||
291 : | edgomez | 78 | pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); |
292 : | h | 81 | pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); |
293 : | pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); | ||
294 : | h | 69 | |
295 : | if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) | ||
296 : | Isibaar | 3 | { |
297 : | h | 69 | uv_dx = pMB->mvs[0].x; |
298 : | uv_dy = pMB->mvs[0].y; | ||
299 : | Isibaar | 3 | |
300 : | uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2; | ||
301 : | uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2; | ||
302 : | } | ||
303 : | else | ||
304 : | { | ||
305 : | int sum; | ||
306 : | h | 69 | sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
307 : | Isibaar | 3 | uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) ); |
308 : | |||
309 : | h | 69 | sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
310 : | Isibaar | 3 | uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) ); |
311 : | } | ||
312 : | |||
313 : | start_timer(); | ||
314 : | chenm001 | 133 | interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos , pMB->mvs[0].x, pMB->mvs[0].y, stride, rounding); |
315 : | interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos , pMB->mvs[1].x, pMB->mvs[1].y, stride, rounding); | ||
316 : | interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride, rounding); | ||
317 : | interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride, rounding); | ||
318 : | interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8*x_pos, 8*y_pos, uv_dx, uv_dy, stride2, rounding); | ||
319 : | interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8*x_pos, 8*y_pos, uv_dx, uv_dy, stride2, rounding); | ||
320 : | Isibaar | 3 | stop_comp_timer(); |
321 : | |||
322 : | h | 69 | for (i = 0; i < 6; i++) |
323 : | Isibaar | 3 | { |
324 : | h | 69 | if (cbp & (1 << (5-i))) // coded |
325 : | Isibaar | 3 | { |
326 : | edgomez | 78 | memset(&block[i*64], 0, 64 * sizeof(int16_t)); // clear |
327 : | Isibaar | 3 | |
328 : | start_timer(); | ||
329 : | edgomez | 78 | get_inter_block(bs, &block[i*64]); |
330 : | Isibaar | 3 | stop_coding_timer(); |
331 : | |||
332 : | start_timer(); | ||
333 : | if (dec->quant_type == 0) | ||
334 : | { | ||
335 : | edgomez | 78 | dequant_inter(&data[i*64], &block[i*64], iQuant); |
336 : | Isibaar | 3 | } |
337 : | else | ||
338 : | { | ||
339 : | edgomez | 78 | dequant4_inter(&data[i*64], &block[i*64], iQuant); |
340 : | Isibaar | 3 | } |
341 : | stop_iquant_timer(); | ||
342 : | |||
343 : | start_timer(); | ||
344 : | edgomez | 78 | idct(&data[i*64]); |
345 : | Isibaar | 3 | stop_idct_timer(); |
346 : | } | ||
347 : | } | ||
348 : | h | 69 | |
349 : | Isibaar | 110 | if (dec->interlacing && pMB->field_dct) |
350 : | h | 69 | { |
351 : | h | 81 | next_block = stride; |
352 : | stride *= 2; | ||
353 : | h | 69 | } |
354 : | |||
355 : | start_timer(); | ||
356 : | if (cbp & 32) | ||
357 : | edgomez | 78 | transfer_16to8add(pY_Cur, &data[0*64], stride); |
358 : | h | 69 | if (cbp & 16) |
359 : | edgomez | 78 | transfer_16to8add(pY_Cur + 8, &data[1*64], stride); |
360 : | h | 69 | if (cbp & 8) |
361 : | h | 81 | transfer_16to8add(pY_Cur + next_block, &data[2*64], stride); |
362 : | h | 69 | if (cbp & 4) |
363 : | h | 81 | transfer_16to8add(pY_Cur + 8 + next_block, &data[3*64], stride); |
364 : | h | 69 | if (cbp & 2) |
365 : | h | 81 | transfer_16to8add(pU_Cur, &data[4*64], stride2); |
366 : | h | 69 | if (cbp & 1) |
367 : | h | 81 | transfer_16to8add(pV_Cur, &data[5*64], stride2); |
368 : | h | 69 | stop_transfer_timer(); |
369 : | Isibaar | 3 | } |
370 : | |||
371 : | |||
372 : | void decoder_iframe(DECODER * dec, Bitstream * bs, int quant, int intra_dc_threshold) | ||
373 : | { | ||
374 : | edgomez | 78 | |
375 : | Isibaar | 3 | uint32_t x, y; |
376 : | |||
377 : | for (y = 0; y < dec->mb_height; y++) | ||
378 : | { | ||
379 : | for (x = 0; x < dec->mb_width; x++) | ||
380 : | { | ||
381 : | MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x]; | ||
382 : | |||
383 : | uint32_t mcbpc; | ||
384 : | uint32_t cbpc; | ||
385 : | uint32_t acpred_flag; | ||
386 : | uint32_t cbpy; | ||
387 : | uint32_t cbp; | ||
388 : | |||
389 : | mcbpc = get_mcbpc_intra(bs); | ||
390 : | mb->mode = mcbpc & 7; | ||
391 : | cbpc = (mcbpc >> 4); | ||
392 : | |||
393 : | acpred_flag = BitstreamGetBit(bs); | ||
394 : | |||
395 : | if (mb->mode == MODE_STUFFING) | ||
396 : | { | ||
397 : | DEBUG("-- STUFFING ?"); | ||
398 : | continue; | ||
399 : | } | ||
400 : | |||
401 : | cbpy = get_cbpy(bs, 1); | ||
402 : | cbp = (cbpy << 2) | cbpc; | ||
403 : | |||
404 : | if (mb->mode == MODE_INTRA_Q) | ||
405 : | { | ||
406 : | quant += dquant_table[BitstreamGetBits(bs,2)]; | ||
407 : | if (quant > 31) | ||
408 : | { | ||
409 : | quant = 31; | ||
410 : | } | ||
411 : | else if (quant < 1) | ||
412 : | { | ||
413 : | quant = 1; | ||
414 : | } | ||
415 : | } | ||
416 : | mb->quant = quant; | ||
417 : | |||
418 : | h | 69 | if (dec->interlacing) |
419 : | { | ||
420 : | mb->field_dct = BitstreamGetBit(bs); | ||
421 : | DEBUG1("deci: field_dct: ", mb->field_dct); | ||
422 : | } | ||
423 : | |||
424 : | Isibaar | 3 | decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, intra_dc_threshold); |
425 : | } | ||
426 : | } | ||
427 : | edgomez | 78 | |
428 : | Isibaar | 3 | } |
429 : | |||
430 : | |||
431 : | void get_motion_vector(DECODER *dec, Bitstream *bs, int x, int y, int k, VECTOR * mv, int fcode) | ||
432 : | { | ||
433 : | edgomez | 78 | |
434 : | Isibaar | 3 | int scale_fac = 1 << (fcode - 1); |
435 : | int high = (32 * scale_fac) - 1; | ||
436 : | int low = ((-32) * scale_fac); | ||
437 : | int range = (64 * scale_fac); | ||
438 : | |||
439 : | VECTOR pmv[4]; | ||
440 : | edgomez | 148 | int32_t psad[4]; |
441 : | Isibaar | 3 | |
442 : | int mv_x, mv_y; | ||
443 : | int pmv_x, pmv_y; | ||
444 : | |||
445 : | |||
446 : | get_pmvdata(dec->mbs, x, y, dec->mb_width, k, pmv, psad); | ||
447 : | |||
448 : | pmv_x = pmv[0].x; | ||
449 : | pmv_y = pmv[0].y; | ||
450 : | |||
451 : | mv_x = get_mv(bs, fcode); | ||
452 : | mv_y = get_mv(bs, fcode); | ||
453 : | |||
454 : | mv_x += pmv_x; | ||
455 : | mv_y += pmv_y; | ||
456 : | |||
457 : | if (mv_x < low) | ||
458 : | { | ||
459 : | mv_x += range; | ||
460 : | } | ||
461 : | else if (mv_x > high) | ||
462 : | { | ||
463 : | mv_x -= range; | ||
464 : | } | ||
465 : | |||
466 : | if (mv_y < low) | ||
467 : | { | ||
468 : | mv_y += range; | ||
469 : | } | ||
470 : | else if (mv_y > high) | ||
471 : | { | ||
472 : | mv_y -= range; | ||
473 : | } | ||
474 : | |||
475 : | mv->x = mv_x; | ||
476 : | mv->y = mv_y; | ||
477 : | |||
478 : | } | ||
479 : | |||
480 : | |||
481 : | void decoder_pframe(DECODER * dec, Bitstream * bs, int rounding, int quant, int fcode, int intra_dc_threshold) | ||
482 : | { | ||
483 : | edgomez | 78 | |
484 : | Isibaar | 3 | uint32_t x, y; |
485 : | |||
486 : | start_timer(); | ||
487 : | chenm001 | 133 | image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing); |
488 : | Isibaar | 3 | stop_edges_timer(); |
489 : | |||
490 : | for (y = 0; y < dec->mb_height; y++) | ||
491 : | { | ||
492 : | for (x = 0; x < dec->mb_width; x++) | ||
493 : | { | ||
494 : | MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x]; | ||
495 : | |||
496 : | if (!BitstreamGetBit(bs)) // not_coded | ||
497 : | { | ||
498 : | uint32_t mcbpc; | ||
499 : | uint32_t cbpc; | ||
500 : | uint32_t acpred_flag; | ||
501 : | uint32_t cbpy; | ||
502 : | uint32_t cbp; | ||
503 : | uint32_t intra; | ||
504 : | |||
505 : | mcbpc = get_mcbpc_inter(bs); | ||
506 : | mb->mode = mcbpc & 7; | ||
507 : | cbpc = (mcbpc >> 4); | ||
508 : | edgomez | 12 | acpred_flag = 0; |
509 : | Isibaar | 3 | |
510 : | intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q); | ||
511 : | |||
512 : | if (intra) | ||
513 : | { | ||
514 : | acpred_flag = BitstreamGetBit(bs); | ||
515 : | } | ||
516 : | |||
517 : | if (mb->mode == MODE_STUFFING) | ||
518 : | { | ||
519 : | DEBUG("-- STUFFING ?"); | ||
520 : | continue; | ||
521 : | } | ||
522 : | |||
523 : | cbpy = get_cbpy(bs, intra); | ||
524 : | cbp = (cbpy << 2) | cbpc; | ||
525 : | |||
526 : | if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) | ||
527 : | { | ||
528 : | quant += dquant_table[BitstreamGetBits(bs,2)]; | ||
529 : | if (quant > 31) | ||
530 : | { | ||
531 : | quant = 31; | ||
532 : | } | ||
533 : | else if (mb->quant < 1) | ||
534 : | { | ||
535 : | quant = 1; | ||
536 : | } | ||
537 : | } | ||
538 : | mb->quant = quant; | ||
539 : | h | 69 | |
540 : | if (dec->interlacing) | ||
541 : | Isibaar | 3 | { |
542 : | h | 69 | mb->field_dct = BitstreamGetBit(bs); |
543 : | DEBUG1("decp: field_dct: ", mb->field_dct); | ||
544 : | Isibaar | 3 | |
545 : | h | 69 | if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) |
546 : | { | ||
547 : | mb->field_pred = BitstreamGetBit(bs); | ||
548 : | DEBUG1("decp: field_pred: ", mb->field_pred); | ||
549 : | |||
550 : | if (mb->field_pred) | ||
551 : | { | ||
552 : | mb->field_for_top = BitstreamGetBit(bs); | ||
553 : | DEBUG1("decp: field_for_top: ", mb->field_for_top); | ||
554 : | mb->field_for_bot = BitstreamGetBit(bs); | ||
555 : | DEBUG1("decp: field_for_bot: ", mb->field_for_bot); | ||
556 : | } | ||
557 : | } | ||
558 : | Isibaar | 3 | } |
559 : | h | 69 | |
560 : | if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) | ||
561 : | { | ||
562 : | if (dec->interlacing && mb->field_pred) | ||
563 : | { | ||
564 : | get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode); | ||
565 : | get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1], fcode); | ||
566 : | } | ||
567 : | else | ||
568 : | { | ||
569 : | get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode); | ||
570 : | mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x; | ||
571 : | mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y; | ||
572 : | } | ||
573 : | } | ||
574 : | Isibaar | 3 | else if (mb->mode == MODE_INTER4V /* || mb->mode == MODE_INTER4V_Q */) |
575 : | { | ||
576 : | get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode); | ||
577 : | get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode); | ||
578 : | get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode); | ||
579 : | get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode); | ||
580 : | } | ||
581 : | else // MODE_INTRA, MODE_INTRA_Q | ||
582 : | { | ||
583 : | mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0; | ||
584 : | mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0; | ||
585 : | decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, intra_dc_threshold); | ||
586 : | continue; | ||
587 : | } | ||
588 : | |||
589 : | decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant, rounding); | ||
590 : | } | ||
591 : | else // not coded | ||
592 : | { | ||
593 : | |||
594 : | mb->mode = MODE_NOT_CODED; | ||
595 : | mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0; | ||
596 : | mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0; | ||
597 : | |||
598 : | // copy macroblock directly from ref to cur | ||
599 : | |||
600 : | start_timer(); | ||
601 : | |||
602 : | transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x), | ||
603 : | chenm001 | 133 | dec->refn[0].y + (16*y)*dec->edged_width + (16*x), |
604 : | edgomez | 78 | dec->edged_width); |
605 : | Isibaar | 3 | |
606 : | transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x+8), | ||
607 : | chenm001 | 133 | dec->refn[0].y + (16*y)*dec->edged_width + (16*x+8), |
608 : | edgomez | 78 | dec->edged_width); |
609 : | Isibaar | 3 | |
610 : | transfer8x8_copy(dec->cur.y + (16*y+8)*dec->edged_width + (16*x), | ||
611 : | chenm001 | 133 | dec->refn[0].y + (16*y+8)*dec->edged_width + (16*x), |
612 : | edgomez | 78 | dec->edged_width); |
613 : | Isibaar | 3 | |
614 : | transfer8x8_copy(dec->cur.y + (16*y+8)*dec->edged_width + (16*x+8), | ||
615 : | chenm001 | 133 | dec->refn[0].y + (16*y+8)*dec->edged_width + (16*x+8), |
616 : | edgomez | 78 | dec->edged_width); |
617 : | Isibaar | 3 | |
618 : | transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x), | ||
619 : | chenm001 | 133 | dec->refn[0].u + (8*y)*dec->edged_width/2 + (8*x), |
620 : | edgomez | 78 | dec->edged_width/2); |
621 : | Isibaar | 3 | |
622 : | transfer8x8_copy(dec->cur.v + (8*y)*dec->edged_width/2 + (8*x), | ||
623 : | chenm001 | 133 | dec->refn[0].v + (8*y)*dec->edged_width/2 + (8*x), |
624 : | edgomez | 78 | dec->edged_width/2); |
625 : | Isibaar | 3 | |
626 : | stop_transfer_timer(); | ||
627 : | } | ||
628 : | } | ||
629 : | } | ||
630 : | } | ||
631 : | |||
632 : | int decoder_decode(DECODER * dec, XVID_DEC_FRAME * frame) | ||
633 : | { | ||
634 : | edgomez | 78 | |
635 : | Isibaar | 3 | Bitstream bs; |
636 : | uint32_t rounding; | ||
637 : | uint32_t quant; | ||
638 : | uint32_t fcode; | ||
639 : | uint32_t intra_dc_threshold; | ||
640 : | chenm001 | 133 | uint32_t vop_type; |
641 : | Isibaar | 3 | |
642 : | start_global_timer(); | ||
643 : | |||
644 : | BitstreamInit(&bs, frame->bitstream, frame->length); | ||
645 : | |||
646 : | chenm001 | 133 | // add by chenm001 <chenm001@163.com> |
647 : | // for support B-frame to reference last 2 frame | ||
648 : | vop_type=BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode, &intra_dc_threshold); | ||
649 : | |||
650 : | if (vop_type==I_VOP || vop_type==P_VOP){ | ||
651 : | image_swap(&dec->refn[0], &dec->refn[1]); | ||
652 : | image_swap(&dec->cur, &dec->refn[0]); | ||
653 : | } | ||
654 : | |||
655 : | switch (vop_type) | ||
656 : | Isibaar | 3 | { |
657 : | case P_VOP : | ||
658 : | decoder_pframe(dec, &bs, rounding, quant, fcode, intra_dc_threshold); | ||
659 : | break; | ||
660 : | |||
661 : | case I_VOP : | ||
662 : | //DEBUG1("",intra_dc_threshold); | ||
663 : | decoder_iframe(dec, &bs, quant, intra_dc_threshold); | ||
664 : | break; | ||
665 : | |||
666 : | case B_VOP : // ignore | ||
667 : | break; | ||
668 : | |||
669 : | case N_VOP : // vop not coded | ||
670 : | break; | ||
671 : | |||
672 : | default : | ||
673 : | return XVID_ERR_FAIL; | ||
674 : | } | ||
675 : | |||
676 : | frame->length = BitstreamPos(&bs) / 8; | ||
677 : | |||
678 : | start_timer(); | ||
679 : | image_output(&dec->cur, dec->width, dec->height, dec->edged_width, | ||
680 : | edgomez | 78 | frame->image, frame->stride, frame->colorspace); |
681 : | Isibaar | 3 | stop_conv_timer(); |
682 : | |||
683 : | emms(); | ||
684 : | |||
685 : | stop_global_timer(); | ||
686 : | |||
687 : | return XVID_ERR_OK; | ||
688 : | edgomez | 78 | |
689 : | Isibaar | 3 | } |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |