Parent Directory | Revision Log
Revision 1.37 - (view) (download)
1 : | Isibaar | 1.1 | /************************************************************************** |
2 : | * | ||
3 : | edgomez | 1.12 | * XVID MPEG-4 VIDEO CODEC |
4 : | * - Decoder main module - | ||
5 : | Isibaar | 1.1 | * |
6 : | edgomez | 1.12 | * 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 : | * | ||
15 : | * 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 : | * | ||
20 : | * 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 : | * | ||
25 : | * 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 | 1.1 | * |
29 : | *************************************************************************/ | ||
30 : | |||
31 : | /************************************************************************** | ||
32 : | * | ||
33 : | edgomez | 1.12 | * History: |
34 : | Isibaar | 1.1 | * |
35 : | chenm001 | 1.30 | * 15.07.2002 fix a bug in B-frame decode at DIRECT mode |
36 : | * MinChen <chenm001@163.com> | ||
37 : | chenm001 | 1.27 | * 10.07.2002 added BFRAMES_DEC_DEBUG support |
38 : | * Fix a little bug for low_delay flage | ||
39 : | * MinChen <chenm001@163.com> | ||
40 : | chenm001 | 1.26 | * 28.06.2002 added basic resync support to iframe/pframe_decode() |
41 : | chenm001 | 1.27 | * 22.06.2002 added primative N_VOP support |
42 : | chenm001 | 1.30 | * #define BFRAMES_DEC now enables Minchen's bframe decoder |
43 : | chenm001 | 1.17 | * 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 | 1.14 | * 02.05.2002 add B-frame decode support(have some problem); |
47 : | * MinChen <chenm001@163.com> | ||
48 : | chenm001 | 1.11 | * 22.04.2002 add some B-frame decode support; chenm001 <chenm001@163.com> |
49 : | h | 1.8 | * 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 | 1.5 | * 26.03.2002 interlacing support - moved transfers outside decode loop |
54 : | edgomez | 1.12 | * 26.12.2001 decoder_mbinter: dequant/idct moved within if(coded) block |
55 : | * 22.12.2001 lock based interpolation | ||
56 : | * 01.12.2001 inital version; (c)2001 peter ross <pross@cs.rmit.edu.au> | ||
57 : | * | ||
58 : | h | 1.37 | * $Id: decoder.c,v 1.36 2002/08/03 05:36:18 chenm001 Exp $ |
59 : | Isibaar | 1.1 | * |
60 : | *************************************************************************/ | ||
61 : | |||
62 : | #include <stdlib.h> | ||
63 : | edgomez | 1.12 | #include <string.h> |
64 : | Isibaar | 1.1 | |
65 : | chenm001 | 1.27 | #ifdef BFRAMES_DEC_DEBUG |
66 : | #define BFRAMES_DEC | ||
67 : | #endif | ||
68 : | |||
69 : | Isibaar | 1.1 | #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 | 1.3 | #include "utils/mem_align.h" |
91 : | Isibaar | 1.1 | |
92 : | edgomez | 1.19 | int |
93 : | decoder_create(XVID_DEC_PARAM * param) | ||
94 : | Isibaar | 1.1 | { |
95 : | edgomez | 1.19 | DECODER *dec; |
96 : | Isibaar | 1.1 | |
97 : | Isibaar | 1.4 | dec = xvid_malloc(sizeof(DECODER), CACHE_LINE); |
98 : | edgomez | 1.19 | if (dec == NULL) { |
99 : | Isibaar | 1.1 | 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 | 1.20 | dec->low_delay = 0; |
112 : | edgomez | 1.19 | |
113 : | if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) { | ||
114 : | Isibaar | 1.3 | xvid_free(dec); |
115 : | Isibaar | 1.1 | return XVID_ERR_MEMORY; |
116 : | } | ||
117 : | |||
118 : | edgomez | 1.19 | if (image_create(&dec->refn[0], dec->edged_width, dec->edged_height)) { |
119 : | Isibaar | 1.1 | image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
120 : | Isibaar | 1.3 | xvid_free(dec); |
121 : | Isibaar | 1.1 | return XVID_ERR_MEMORY; |
122 : | } | ||
123 : | chenm001 | 1.11 | // add by chenm001 <chenm001@163.com> |
124 : | // for support B-frame to reference last 2 frame | ||
125 : | edgomez | 1.19 | if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) { |
126 : | chenm001 | 1.11 | 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 | 1.19 | if (image_create(&dec->refn[2], dec->edged_width, dec->edged_height)) { |
132 : | chenm001 | 1.14 | 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 | 1.1 | |
139 : | edgomez | 1.19 | dec->mbs = |
140 : | xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, | ||
141 : | CACHE_LINE); | ||
142 : | if (dec->mbs == NULL) { | ||
143 : | Isibaar | 1.1 | image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
144 : | chenm001 | 1.14 | 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 : | xvid_free(dec); | ||
148 : | return XVID_ERR_MEMORY; | ||
149 : | } | ||
150 : | Isibaar | 1.20 | |
151 : | memset(dec->mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height); | ||
152 : | |||
153 : | chenm001 | 1.14 | // add by chenm001 <chenm001@163.com> |
154 : | // for skip MB flag | ||
155 : | edgomez | 1.19 | 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 | 1.14 | 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 : | Isibaar | 1.3 | xvid_free(dec); |
165 : | Isibaar | 1.1 | return XVID_ERR_MEMORY; |
166 : | } | ||
167 : | Isibaar | 1.20 | |
168 : | memset(dec->last_mbs, 0, sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height); | ||
169 : | Isibaar | 1.1 | |
170 : | init_timer(); | ||
171 : | |||
172 : | chenm001 | 1.14 | // 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 | 1.19 | |
177 : | Isibaar | 1.1 | return XVID_ERR_OK; |
178 : | } | ||
179 : | |||
180 : | |||
181 : | edgomez | 1.19 | int |
182 : | decoder_destroy(DECODER * dec) | ||
183 : | Isibaar | 1.1 | { |
184 : | chenm001 | 1.14 | xvid_free(dec->last_mbs); |
185 : | Isibaar | 1.3 | xvid_free(dec->mbs); |
186 : | chenm001 | 1.11 | image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
187 : | chenm001 | 1.14 | 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 | 1.1 | image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
190 : | Isibaar | 1.3 | xvid_free(dec); |
191 : | Isibaar | 1.1 | |
192 : | write_timer(); | ||
193 : | return XVID_ERR_OK; | ||
194 : | } | ||
195 : | |||
196 : | |||
197 : | |||
198 : | edgomez | 1.19 | static const int32_t dquant_table[4] = { |
199 : | Isibaar | 1.1 | -1, -2, 1, 2 |
200 : | }; | ||
201 : | |||
202 : | |||
203 : | chenm001 | 1.14 | |
204 : | |||
205 : | Isibaar | 1.1 | // decode an intra macroblock |
206 : | |||
207 : | edgomez | 1.19 | 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 | 1.26 | const uint32_t intra_dc_threshold, |
217 : | const unsigned int bound) | ||
218 : | Isibaar | 1.1 | { |
219 : | edgomez | 1.7 | |
220 : | DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE); | ||
221 : | edgomez | 1.19 | DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); |
222 : | edgomez | 1.7 | |
223 : | h | 1.8 | uint32_t stride = dec->edged_width; |
224 : | uint32_t stride2 = stride / 2; | ||
225 : | uint32_t next_block = stride * 8; | ||
226 : | h | 1.5 | uint32_t i; |
227 : | uint32_t iQuant = pMB->quant; | ||
228 : | uint8_t *pY_Cur, *pU_Cur, *pV_Cur; | ||
229 : | |||
230 : | edgomez | 1.7 | pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); |
231 : | h | 1.8 | 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 : | edgomez | 1.7 | |
234 : | edgomez | 1.19 | memset(block, 0, 6 * 64 * sizeof(int16_t)); // clear |
235 : | h | 1.5 | |
236 : | edgomez | 1.19 | for (i = 0; i < 6; i++) { |
237 : | h | 1.5 | uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
238 : | Isibaar | 1.1 | int16_t predictors[8]; |
239 : | int start_coeff; | ||
240 : | |||
241 : | start_timer(); | ||
242 : | edgomez | 1.19 | predict_acdc(dec->mbs, x_pos, y_pos, dec->mb_width, i, &block[i * 64], |
243 : | chenm001 | 1.26 | iQuant, iDcScaler, predictors, bound); |
244 : | edgomez | 1.19 | if (!acpred_flag) { |
245 : | h | 1.5 | pMB->acpred_directions[i] = 0; |
246 : | Isibaar | 1.1 | } |
247 : | stop_prediction_timer(); | ||
248 : | |||
249 : | edgomez | 1.19 | if (quant < intra_dc_threshold) { |
250 : | Isibaar | 1.1 | int dc_size; |
251 : | int dc_dif; | ||
252 : | |||
253 : | edgomez | 1.19 | 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 | 1.1 | |
256 : | edgomez | 1.19 | if (dc_size > 8) { |
257 : | BitstreamSkip(bs, 1); // marker | ||
258 : | Isibaar | 1.1 | } |
259 : | edgomez | 1.19 | |
260 : | block[i * 64 + 0] = dc_dif; | ||
261 : | Isibaar | 1.1 | start_coeff = 1; |
262 : | chenm001 | 1.26 | |
263 : | DPRINTF(DPRINTF_COEFF,"block[0] %i", dc_dif); | ||
264 : | edgomez | 1.19 | } else { |
265 : | Isibaar | 1.1 | start_coeff = 0; |
266 : | } | ||
267 : | |||
268 : | start_timer(); | ||
269 : | edgomez | 1.19 | if (cbp & (1 << (5 - i))) // coded |
270 : | Isibaar | 1.1 | { |
271 : | edgomez | 1.19 | get_intra_block(bs, &block[i * 64], pMB->acpred_directions[i], |
272 : | start_coeff); | ||
273 : | Isibaar | 1.1 | } |
274 : | stop_coding_timer(); | ||
275 : | |||
276 : | start_timer(); | ||
277 : | edgomez | 1.19 | add_acdc(pMB, i, &block[i * 64], iDcScaler, predictors); |
278 : | Isibaar | 1.1 | stop_prediction_timer(); |
279 : | |||
280 : | start_timer(); | ||
281 : | edgomez | 1.19 | 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 | 1.1 | } |
286 : | stop_iquant_timer(); | ||
287 : | |||
288 : | start_timer(); | ||
289 : | edgomez | 1.19 | idct(&data[i * 64]); |
290 : | Isibaar | 1.1 | stop_idct_timer(); |
291 : | h | 1.5 | } |
292 : | Isibaar | 1.1 | |
293 : | edgomez | 1.19 | if (dec->interlacing && pMB->field_dct) { |
294 : | h | 1.8 | next_block = stride; |
295 : | stride *= 2; | ||
296 : | Isibaar | 1.1 | } |
297 : | h | 1.5 | |
298 : | start_timer(); | ||
299 : | edgomez | 1.19 | 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 | 1.5 | stop_transfer_timer(); |
306 : | Isibaar | 1.1 | } |
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 | 1.19 | { 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 }; |
316 : | Isibaar | 1.1 | |
317 : | |||
318 : | // decode an inter macroblock | ||
319 : | |||
320 : | edgomez | 1.19 | 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 | 1.1 | { |
331 : | edgomez | 1.7 | |
332 : | edgomez | 1.19 | DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE); |
333 : | edgomez | 1.7 | DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); |
334 : | h | 1.5 | |
335 : | h | 1.8 | uint32_t stride = dec->edged_width; |
336 : | uint32_t stride2 = stride / 2; | ||
337 : | uint32_t next_block = stride * 8; | ||
338 : | edgomez | 1.7 | uint32_t i; |
339 : | uint32_t iQuant = pMB->quant; | ||
340 : | h | 1.5 | uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
341 : | Isibaar | 1.1 | int uv_dx, uv_dy; |
342 : | |||
343 : | edgomez | 1.7 | pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); |
344 : | h | 1.8 | 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 | 1.5 | |
347 : | edgomez | 1.19 | if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) { |
348 : | h | 1.5 | uv_dx = pMB->mvs[0].x; |
349 : | uv_dy = pMB->mvs[0].y; | ||
350 : | Isibaar | 1.1 | |
351 : | Isibaar | 1.34 | 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 | 1.1 | 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 | 1.19 | } else { |
360 : | Isibaar | 1.1 | int sum; |
361 : | Isibaar | 1.34 | sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
362 : | edgomez | 1.19 | |
363 : | Isibaar | 1.34 | 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 : | Isibaar | 1.1 | |
370 : | h | 1.5 | sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
371 : | Isibaar | 1.34 | |
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 | 1.1 | } |
379 : | |||
380 : | start_timer(); | ||
381 : | Isibaar | 1.34 | if(dec->quarterpel) { |
382 : | DEBUG("QUARTERPEL"); | ||
383 : | 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 | 1.19 | 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 | 1.1 | stop_comp_timer(); |
408 : | |||
409 : | edgomez | 1.19 | for (i = 0; i < 6; i++) { |
410 : | if (cbp & (1 << (5 - i))) // coded | ||
411 : | Isibaar | 1.1 | { |
412 : | edgomez | 1.19 | memset(&block[i * 64], 0, 64 * sizeof(int16_t)); // clear |
413 : | Isibaar | 1.1 | |
414 : | start_timer(); | ||
415 : | edgomez | 1.19 | get_inter_block(bs, &block[i * 64]); |
416 : | Isibaar | 1.1 | stop_coding_timer(); |
417 : | |||
418 : | start_timer(); | ||
419 : | edgomez | 1.19 | 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 | 1.1 | } |
424 : | stop_iquant_timer(); | ||
425 : | |||
426 : | start_timer(); | ||
427 : | edgomez | 1.19 | idct(&data[i * 64]); |
428 : | Isibaar | 1.1 | stop_idct_timer(); |
429 : | h | 1.5 | } |
430 : | } | ||
431 : | Isibaar | 1.1 | |
432 : | edgomez | 1.19 | if (dec->interlacing && pMB->field_dct) { |
433 : | h | 1.8 | next_block = stride; |
434 : | stride *= 2; | ||
435 : | Isibaar | 1.1 | } |
436 : | h | 1.5 | |
437 : | start_timer(); | ||
438 : | if (cbp & 32) | ||
439 : | edgomez | 1.19 | transfer_16to8add(pY_Cur, &data[0 * 64], stride); |
440 : | h | 1.5 | if (cbp & 16) |
441 : | edgomez | 1.19 | transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride); |
442 : | h | 1.5 | if (cbp & 8) |
443 : | edgomez | 1.19 | transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride); |
444 : | h | 1.5 | if (cbp & 4) |
445 : | edgomez | 1.19 | transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride); |
446 : | h | 1.5 | if (cbp & 2) |
447 : | edgomez | 1.19 | transfer_16to8add(pU_Cur, &data[4 * 64], stride2); |
448 : | h | 1.5 | if (cbp & 1) |
449 : | edgomez | 1.19 | transfer_16to8add(pV_Cur, &data[5 * 64], stride2); |
450 : | h | 1.5 | stop_transfer_timer(); |
451 : | Isibaar | 1.1 | } |
452 : | |||
453 : | |||
454 : | edgomez | 1.19 | void |
455 : | decoder_iframe(DECODER * dec, | ||
456 : | Bitstream * bs, | ||
457 : | int quant, | ||
458 : | int intra_dc_threshold) | ||
459 : | Isibaar | 1.1 | { |
460 : | chenm001 | 1.26 | uint32_t bound; |
461 : | uint32_t x, y; | ||
462 : | chenm001 | 1.25 | |
463 : | chenm001 | 1.26 | bound = 0; |
464 : | edgomez | 1.7 | |
465 : | edgomez | 1.19 | for (y = 0; y < dec->mb_height; y++) { |
466 : | for (x = 0; x < dec->mb_width; x++) { | ||
467 : | chenm001 | 1.26 | MACROBLOCK *mb; |
468 : | Isibaar | 1.1 | uint32_t mcbpc; |
469 : | uint32_t cbpc; | ||
470 : | uint32_t acpred_flag; | ||
471 : | uint32_t cbpy; | ||
472 : | uint32_t cbp; | ||
473 : | |||
474 : | chenm001 | 1.26 | 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 | 1.1 | 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 | 1.19 | if (mb->mode == MODE_INTRA_Q) { |
497 : | quant += dquant_table[BitstreamGetBits(bs, 2)]; | ||
498 : | if (quant > 31) { | ||
499 : | Isibaar | 1.1 | quant = 31; |
500 : | edgomez | 1.19 | } else if (quant < 1) { |
501 : | Isibaar | 1.1 | quant = 1; |
502 : | } | ||
503 : | } | ||
504 : | mb->quant = quant; | ||
505 : | chenm001 | 1.30 | 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 : | h | 1.5 | |
510 : | edgomez | 1.19 | if (dec->interlacing) { |
511 : | h | 1.5 | mb->field_dct = BitstreamGetBit(bs); |
512 : | DEBUG1("deci: field_dct: ", mb->field_dct); | ||
513 : | } | ||
514 : | Isibaar | 1.1 | |
515 : | edgomez | 1.19 | decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, |
516 : | chenm001 | 1.26 | intra_dc_threshold, bound); |
517 : | Isibaar | 1.1 | } |
518 : | albeu | 1.32 | 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 | 1.1 | } |
522 : | edgomez | 1.7 | |
523 : | Isibaar | 1.1 | } |
524 : | |||
525 : | |||
526 : | edgomez | 1.19 | void |
527 : | get_motion_vector(DECODER * dec, | ||
528 : | Bitstream * bs, | ||
529 : | int x, | ||
530 : | int y, | ||
531 : | int k, | ||
532 : | VECTOR * mv, | ||
533 : | chenm001 | 1.26 | int fcode, |
534 : | const int bound) | ||
535 : | Isibaar | 1.1 | { |
536 : | edgomez | 1.7 | |
537 : | Isibaar | 1.1 | 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 | 1.26 | VECTOR pmv; |
543 : | Isibaar | 1.1 | int mv_x, mv_y; |
544 : | |||
545 : | chenm001 | 1.26 | pmv = get_pmv2(dec->mbs, dec->mb_width, bound, x, y, k); |
546 : | Isibaar | 1.1 | |
547 : | mv_x = get_mv(bs, fcode); | ||
548 : | mv_y = get_mv(bs, fcode); | ||
549 : | edgomez | 1.19 | |
550 : | chenm001 | 1.26 | DPRINTF(DPRINTF_MV,"mv_diff (%i,%i) pred (%i,%i)", mv_x, mv_y, pmv.x, pmv.y); |
551 : | |||
552 : | mv_x += pmv.x; | ||
553 : | mv_y += pmv.y; | ||
554 : | Isibaar | 1.1 | |
555 : | edgomez | 1.19 | if (mv_x < low) { |
556 : | Isibaar | 1.1 | mv_x += range; |
557 : | edgomez | 1.19 | } else if (mv_x > high) { |
558 : | Isibaar | 1.1 | mv_x -= range; |
559 : | } | ||
560 : | |||
561 : | edgomez | 1.19 | if (mv_y < low) { |
562 : | Isibaar | 1.1 | mv_y += range; |
563 : | edgomez | 1.19 | } else if (mv_y > high) { |
564 : | Isibaar | 1.1 | mv_y -= range; |
565 : | } | ||
566 : | |||
567 : | mv->x = mv_x; | ||
568 : | mv->y = mv_y; | ||
569 : | |||
570 : | } | ||
571 : | |||
572 : | |||
573 : | edgomez | 1.19 | 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 | 1.1 | { |
581 : | edgomez | 1.7 | |
582 : | Isibaar | 1.1 | uint32_t x, y; |
583 : | chenm001 | 1.26 | uint32_t bound; |
584 : | albeu | 1.32 | int cp_mb, st_mb; |
585 : | Isibaar | 1.1 | |
586 : | start_timer(); | ||
587 : | edgomez | 1.19 | image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
588 : | dec->width, dec->height, dec->interlacing); | ||
589 : | Isibaar | 1.1 | stop_edges_timer(); |
590 : | |||
591 : | chenm001 | 1.26 | bound = 0; |
592 : | |||
593 : | edgomez | 1.19 | for (y = 0; y < dec->mb_height; y++) { |
594 : | albeu | 1.32 | cp_mb = st_mb = 0; |
595 : | edgomez | 1.19 | for (x = 0; x < dec->mb_width; x++) { |
596 : | chenm001 | 1.26 | MACROBLOCK *mb; |
597 : | |||
598 : | // 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 : | suxen_drol | 1.23 | |
612 : | edgomez | 1.19 | //if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs))) // not_coded |
613 : | if (!(BitstreamGetBit(bs))) // not_coded | ||
614 : | Isibaar | 1.1 | { |
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 | 1.32 | cp_mb++; |
623 : | Isibaar | 1.1 | mcbpc = get_mcbpc_inter(bs); |
624 : | mb->mode = mcbpc & 7; | ||
625 : | cbpc = (mcbpc >> 4); | ||
626 : | chenm001 | 1.26 | |
627 : | DPRINTF(DPRINTF_MB, "mode %i", mb->mode); | ||
628 : | DPRINTF(DPRINTF_MB, "cbpc %i", cbpc); | ||
629 : | edgomez | 1.2 | acpred_flag = 0; |
630 : | Isibaar | 1.1 | |
631 : | intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q); | ||
632 : | edgomez | 1.19 | |
633 : | if (intra) { | ||
634 : | Isibaar | 1.1 | acpred_flag = BitstreamGetBit(bs); |
635 : | } | ||
636 : | |||
637 : | chenm001 | 1.26 | cbpy = get_cbpy(bs, intra); |
638 : | DPRINTF(DPRINTF_MB, "cbpy %i", cbpy); | ||
639 : | chenm001 | 1.25 | |
640 : | Isibaar | 1.1 | cbp = (cbpy << 2) | cbpc; |
641 : | |||
642 : | edgomez | 1.19 | if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q) { |
643 : | chenm001 | 1.26 | int dquant = dquant_table[BitstreamGetBits(bs, 2)]; |
644 : | DPRINTF(DPRINTF_MB, "dquant %i", dquant); | ||
645 : | quant += dquant; | ||
646 : | edgomez | 1.19 | if (quant > 31) { |
647 : | Isibaar | 1.1 | quant = 31; |
648 : | chenm001 | 1.26 | } else if (quant < 1) { |
649 : | Isibaar | 1.1 | quant = 1; |
650 : | } | ||
651 : | chenm001 | 1.26 | DPRINTF(DPRINTF_MB, "quant %i", quant); |
652 : | Isibaar | 1.1 | } |
653 : | mb->quant = quant; | ||
654 : | h | 1.5 | |
655 : | edgomez | 1.19 | if (dec->interlacing) { |
656 : | h | 1.37 | if (cbp || intra) { |
657 : | mb->field_dct = BitstreamGetBit(bs); | ||
658 : | DEBUG1("decp: field_dct: ", mb->field_dct); | ||
659 : | } | ||
660 : | h | 1.5 | |
661 : | edgomez | 1.19 | if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) { |
662 : | h | 1.5 | mb->field_pred = BitstreamGetBit(bs); |
663 : | DEBUG1("decp: field_pred: ", mb->field_pred); | ||
664 : | |||
665 : | edgomez | 1.19 | if (mb->field_pred) { |
666 : | h | 1.5 | mb->field_for_top = BitstreamGetBit(bs); |
667 : | DEBUG1("decp: field_for_top: ", mb->field_for_top); | ||
668 : | mb->field_for_bot = BitstreamGetBit(bs); | ||
669 : | DEBUG1("decp: field_for_bot: ", mb->field_for_bot); | ||
670 : | } | ||
671 : | } | ||
672 : | } | ||
673 : | |||
674 : | edgomez | 1.19 | 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 | 1.26 | fcode, bound); |
678 : | edgomez | 1.19 | get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1], |
679 : | chenm001 | 1.26 | fcode, bound); |
680 : | edgomez | 1.19 | } else { |
681 : | get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], | ||
682 : | chenm001 | 1.26 | fcode, bound); |
683 : | edgomez | 1.19 | 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 | 1.5 | } |
688 : | chl | 1.35 | } else if (mb->mode == MODE_INTER4V ) { |
689 : | |||
690 : | chenm001 | 1.26 | 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 | 1.19 | } else // MODE_INTRA, MODE_INTRA_Q |
695 : | Isibaar | 1.1 | { |
696 : | edgomez | 1.19 | 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 | 1.26 | intra_dc_threshold, bound); |
702 : | Isibaar | 1.1 | continue; |
703 : | } | ||
704 : | |||
705 : | edgomez | 1.19 | decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant, |
706 : | rounding); | ||
707 : | } else // not coded | ||
708 : | Isibaar | 1.1 | { |
709 : | chl | 1.35 | DEBUG2("P-frame MB at (X,Y)=",x,y); |
710 : | chenm001 | 1.14 | mb->mode = MODE_NOT_CODED; |
711 : | Isibaar | 1.1 | mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0; |
712 : | mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0; | ||
713 : | edgomez | 1.19 | |
714 : | Isibaar | 1.1 | // copy macroblock directly from ref to cur |
715 : | |||
716 : | start_timer(); | ||
717 : | |||
718 : | edgomez | 1.19 | transfer8x8_copy(dec->cur.y + (16 * y) * dec->edged_width + |
719 : | (16 * x), | ||
720 : | dec->refn[0].y + (16 * y) * dec->edged_width + | ||
721 : | (16 * x), dec->edged_width); | ||
722 : | |||
723 : | transfer8x8_copy(dec->cur.y + (16 * y) * dec->edged_width + | ||
724 : | (16 * x + 8), | ||
725 : | dec->refn[0].y + (16 * y) * dec->edged_width + | ||
726 : | (16 * x + 8), dec->edged_width); | ||
727 : | |||
728 : | transfer8x8_copy(dec->cur.y + (16 * y + 8) * dec->edged_width + | ||
729 : | (16 * x), | ||
730 : | dec->refn[0].y + (16 * y + | ||
731 : | 8) * dec->edged_width + | ||
732 : | (16 * x), dec->edged_width); | ||
733 : | |||
734 : | transfer8x8_copy(dec->cur.y + (16 * y + 8) * dec->edged_width + | ||
735 : | (16 * x + 8), | ||
736 : | dec->refn[0].y + (16 * y + | ||
737 : | 8) * dec->edged_width + | ||
738 : | (16 * x + 8), dec->edged_width); | ||
739 : | |||
740 : | transfer8x8_copy(dec->cur.u + (8 * y) * dec->edged_width / 2 + | ||
741 : | (8 * x), | ||
742 : | dec->refn[0].u + | ||
743 : | (8 * y) * dec->edged_width / 2 + (8 * x), | ||
744 : | dec->edged_width / 2); | ||
745 : | |||
746 : | transfer8x8_copy(dec->cur.v + (8 * y) * dec->edged_width / 2 + | ||
747 : | (8 * x), | ||
748 : | dec->refn[0].v + | ||
749 : | (8 * y) * dec->edged_width / 2 + (8 * x), | ||
750 : | dec->edged_width / 2); | ||
751 : | Isibaar | 1.1 | stop_transfer_timer(); |
752 : | albeu | 1.32 | if(dec->out_frm && cp_mb > 0) { |
753 : | output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb); | ||
754 : | cp_mb = 0; | ||
755 : | } | ||
756 : | st_mb = x+1; | ||
757 : | Isibaar | 1.1 | } |
758 : | } | ||
759 : | albeu | 1.32 | if(dec->out_frm && cp_mb > 0) |
760 : | output_slice(&dec->cur, dec->edged_width,dec->width,dec->out_frm,st_mb,y,cp_mb); | ||
761 : | Isibaar | 1.1 | } |
762 : | } | ||
763 : | |||
764 : | chenm001 | 1.14 | |
765 : | // add by MinChen <chenm001@163.com> | ||
766 : | // decode B-frame motion vector | ||
767 : | edgomez | 1.19 | void |
768 : | get_b_motion_vector(DECODER * dec, | ||
769 : | Bitstream * bs, | ||
770 : | int x, | ||
771 : | int y, | ||
772 : | VECTOR * mv, | ||
773 : | int fcode, | ||
774 : | const VECTOR pmv) | ||
775 : | chenm001 | 1.14 | { |
776 : | int scale_fac = 1 << (fcode - 1); | ||
777 : | int high = (32 * scale_fac) - 1; | ||
778 : | int low = ((-32) * scale_fac); | ||
779 : | int range = (64 * scale_fac); | ||
780 : | |||
781 : | int mv_x, mv_y; | ||
782 : | int pmv_x, pmv_y; | ||
783 : | |||
784 : | pmv_x = pmv.x; | ||
785 : | pmv_y = pmv.y; | ||
786 : | |||
787 : | mv_x = get_mv(bs, fcode); | ||
788 : | mv_y = get_mv(bs, fcode); | ||
789 : | edgomez | 1.19 | |
790 : | chenm001 | 1.14 | mv_x += pmv_x; |
791 : | mv_y += pmv_y; | ||
792 : | |||
793 : | edgomez | 1.19 | if (mv_x < low) { |
794 : | chenm001 | 1.14 | mv_x += range; |
795 : | edgomez | 1.19 | } else if (mv_x > high) { |
796 : | chenm001 | 1.14 | mv_x -= range; |
797 : | } | ||
798 : | |||
799 : | edgomez | 1.19 | if (mv_y < low) { |
800 : | chenm001 | 1.14 | mv_y += range; |
801 : | edgomez | 1.19 | } else if (mv_y > high) { |
802 : | chenm001 | 1.14 | mv_y -= range; |
803 : | } | ||
804 : | |||
805 : | mv->x = mv_x; | ||
806 : | mv->y = mv_y; | ||
807 : | } | ||
808 : | |||
809 : | |||
810 : | // add by MinChen <chenm001@163.com> | ||
811 : | // decode an B-frame forward & backward inter macroblock | ||
812 : | edgomez | 1.19 | void |
813 : | decoder_bf_mbinter(DECODER * dec, | ||
814 : | const MACROBLOCK * pMB, | ||
815 : | const uint32_t x_pos, | ||
816 : | const uint32_t y_pos, | ||
817 : | const uint32_t cbp, | ||
818 : | Bitstream * bs, | ||
819 : | const uint32_t quant, | ||
820 : | const uint8_t ref) | ||
821 : | chenm001 | 1.14 | { |
822 : | |||
823 : | edgomez | 1.19 | DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE); |
824 : | chenm001 | 1.14 | DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); |
825 : | |||
826 : | uint32_t stride = dec->edged_width; | ||
827 : | uint32_t stride2 = stride / 2; | ||
828 : | uint32_t next_block = stride * 8; | ||
829 : | uint32_t i; | ||
830 : | uint32_t iQuant = pMB->quant; | ||
831 : | uint8_t *pY_Cur, *pU_Cur, *pV_Cur; | ||
832 : | int uv_dx, uv_dy; | ||
833 : | |||
834 : | pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); | ||
835 : | pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); | ||
836 : | pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); | ||
837 : | |||
838 : | chenm001 | 1.30 | |
839 : | edgomez | 1.19 | if (!(pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) { |
840 : | chenm001 | 1.14 | uv_dx = pMB->mvs[0].x; |
841 : | uv_dy = pMB->mvs[0].y; | ||
842 : | |||
843 : | uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2; | ||
844 : | uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2; | ||
845 : | edgomez | 1.19 | } else { |
846 : | chenm001 | 1.14 | int sum; |
847 : | edgomez | 1.19 | |
848 : | chenm001 | 1.14 | sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
849 : | edgomez | 1.19 | uv_dx = |
850 : | (sum == | ||
851 : | 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + | ||
852 : | (ABS(sum) / 16) * 2)); | ||
853 : | chenm001 | 1.14 | |
854 : | sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; | ||
855 : | edgomez | 1.19 | uv_dy = |
856 : | (sum == | ||
857 : | 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + | ||
858 : | (ABS(sum) / 16) * 2)); | ||
859 : | chenm001 | 1.14 | } |
860 : | |||
861 : | start_timer(); | ||
862 : | edgomez | 1.19 | interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos, 16 * y_pos, |
863 : | pMB->mvs[0].x, pMB->mvs[0].y, stride, 0); | ||
864 : | interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos + 8, | ||
865 : | 16 * y_pos, pMB->mvs[1].x, pMB->mvs[1].y, stride, 0); | ||
866 : | interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos, | ||
867 : | 16 * y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride, | ||
868 : | 0); | ||
869 : | interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16 * x_pos + 8, | ||
870 : | 16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride, | ||
871 : | 0); | ||
872 : | interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8 * x_pos, 8 * y_pos, | ||
873 : | uv_dx, uv_dy, stride2, 0); | ||
874 : | interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8 * x_pos, 8 * y_pos, | ||
875 : | uv_dx, uv_dy, stride2, 0); | ||
876 : | chenm001 | 1.14 | stop_comp_timer(); |
877 : | |||
878 : | edgomez | 1.19 | for (i = 0; i < 6; i++) { |
879 : | if (cbp & (1 << (5 - i))) // coded | ||
880 : | chenm001 | 1.14 | { |
881 : | edgomez | 1.19 | memset(&block[i * 64], 0, 64 * sizeof(int16_t)); // clear |
882 : | chenm001 | 1.14 | |
883 : | start_timer(); | ||
884 : | edgomez | 1.19 | get_inter_block(bs, &block[i * 64]); |
885 : | chenm001 | 1.14 | stop_coding_timer(); |
886 : | |||
887 : | start_timer(); | ||
888 : | edgomez | 1.19 | if (dec->quant_type == 0) { |
889 : | dequant_inter(&data[i * 64], &block[i * 64], iQuant); | ||
890 : | } else { | ||
891 : | dequant4_inter(&data[i * 64], &block[i * 64], iQuant); | ||
892 : | chenm001 | 1.14 | } |
893 : | stop_iquant_timer(); | ||
894 : | |||
895 : | start_timer(); | ||
896 : | edgomez | 1.19 | idct(&data[i * 64]); |
897 : | chenm001 | 1.14 | stop_idct_timer(); |
898 : | } | ||
899 : | } | ||
900 : | |||
901 : | edgomez | 1.19 | if (dec->interlacing && pMB->field_dct) { |
902 : | chenm001 | 1.14 | next_block = stride; |
903 : | stride *= 2; | ||
904 : | } | ||
905 : | |||
906 : | start_timer(); | ||
907 : | if (cbp & 32) | ||
908 : | edgomez | 1.19 | transfer_16to8add(pY_Cur, &data[0 * 64], stride); |
909 : | chenm001 | 1.14 | if (cbp & 16) |
910 : | edgomez | 1.19 | transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride); |
911 : | chenm001 | 1.14 | if (cbp & 8) |
912 : | edgomez | 1.19 | transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride); |
913 : | chenm001 | 1.14 | if (cbp & 4) |
914 : | edgomez | 1.19 | transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride); |
915 : | chenm001 | 1.14 | if (cbp & 2) |
916 : | edgomez | 1.19 | transfer_16to8add(pU_Cur, &data[4 * 64], stride2); |
917 : | chenm001 | 1.14 | if (cbp & 1) |
918 : | edgomez | 1.19 | transfer_16to8add(pV_Cur, &data[5 * 64], stride2); |
919 : | chenm001 | 1.14 | stop_transfer_timer(); |
920 : | } | ||
921 : | |||
922 : | |||
923 : | // add by MinChen <chenm001@163.com> | ||
924 : | // decode an B-frame direct & inter macroblock | ||
925 : | edgomez | 1.19 | void |
926 : | decoder_bf_interpolate_mbinter(DECODER * dec, | ||
927 : | IMAGE forward, | ||
928 : | IMAGE backward, | ||
929 : | const MACROBLOCK * pMB, | ||
930 : | const uint32_t x_pos, | ||
931 : | const uint32_t y_pos, | ||
932 : | Bitstream * bs) | ||
933 : | chenm001 | 1.14 | { |
934 : | |||
935 : | edgomez | 1.19 | DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE); |
936 : | chenm001 | 1.14 | DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); |
937 : | |||
938 : | edgomez | 1.19 | uint32_t stride = dec->edged_width; |
939 : | uint32_t stride2 = stride / 2; | ||
940 : | uint32_t next_block = stride * 8; | ||
941 : | uint32_t iQuant = pMB->quant; | ||
942 : | int uv_dx, uv_dy; | ||
943 : | int b_uv_dx, b_uv_dy; | ||
944 : | uint32_t i; | ||
945 : | uint8_t *pY_Cur, *pU_Cur, *pV_Cur; | ||
946 : | chenm001 | 1.30 | const uint32_t cbp = pMB->cbp; |
947 : | chenm001 | 1.15 | |
948 : | pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); | ||
949 : | pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); | ||
950 : | pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); | ||
951 : | chenm001 | 1.14 | |
952 : | chenm001 | 1.30 | |
953 : | edgomez | 1.19 | if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) { |
954 : | chenm001 | 1.14 | uv_dx = pMB->mvs[0].x; |
955 : | uv_dy = pMB->mvs[0].y; | ||
956 : | |||
957 : | uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2; | ||
958 : | uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2; | ||
959 : | |||
960 : | b_uv_dx = pMB->b_mvs[0].x; | ||
961 : | b_uv_dy = pMB->b_mvs[0].y; | ||
962 : | |||
963 : | b_uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2; | ||
964 : | b_uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2; | ||
965 : | edgomez | 1.19 | } else { |
966 : | chenm001 | 1.14 | int sum; |
967 : | edgomez | 1.19 | |
968 : | chenm001 | 1.14 | sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
969 : | edgomez | 1.19 | uv_dx = |
970 : | (sum == | ||
971 : | 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + | ||
972 : | (ABS(sum) / 16) * 2)); | ||
973 : | chenm001 | 1.14 | |
974 : | sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; | ||
975 : | edgomez | 1.19 | uv_dy = |
976 : | (sum == | ||
977 : | 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + | ||
978 : | (ABS(sum) / 16) * 2)); | ||
979 : | |||
980 : | sum = | ||
981 : | pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + | ||
982 : | pMB->b_mvs[3].x; | ||
983 : | b_uv_dx = | ||
984 : | (sum == | ||
985 : | 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + | ||
986 : | (ABS(sum) / 16) * 2)); | ||
987 : | |||
988 : | sum = | ||
989 : | pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + | ||
990 : | pMB->b_mvs[3].y; | ||
991 : | b_uv_dy = | ||
992 : | (sum == | ||
993 : | 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + | ||
994 : | (ABS(sum) / 16) * 2)); | ||
995 : | } | ||
996 : | chenm001 | 1.14 | |
997 : | |||
998 : | start_timer(); | ||
999 : | edgomez | 1.19 | interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos, |
1000 : | pMB->mvs[0].x, pMB->mvs[0].y, stride, 0); | ||
1001 : | interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, 16 * y_pos, | ||
1002 : | pMB->mvs[1].x, pMB->mvs[1].y, stride, 0); | ||
1003 : | interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos, 16 * y_pos + 8, | ||
1004 : | pMB->mvs[2].x, pMB->mvs[2].y, stride, 0); | ||
1005 : | interpolate8x8_switch(dec->cur.y, forward.y, 16 * x_pos + 8, | ||
1006 : | 16 * y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride, | ||
1007 : | 0); | ||
1008 : | interpolate8x8_switch(dec->cur.u, forward.u, 8 * x_pos, 8 * y_pos, uv_dx, | ||
1009 : | uv_dy, stride2, 0); | ||
1010 : | interpolate8x8_switch(dec->cur.v, forward.v, 8 * x_pos, 8 * y_pos, uv_dx, | ||
1011 : | uv_dy, stride2, 0); | ||
1012 : | |||
1013 : | |||
1014 : | interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos, 16 * y_pos, | ||
1015 : | pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0); | ||
1016 : | interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8, | ||
1017 : | 16 * y_pos, pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, | ||
1018 : | 0); | ||
1019 : | interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos, | ||
1020 : | 16 * y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y, | ||
1021 : | stride, 0); | ||
1022 : | interpolate8x8_switch(dec->refn[2].y, backward.y, 16 * x_pos + 8, | ||
1023 : | 16 * y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y, | ||
1024 : | stride, 0); | ||
1025 : | interpolate8x8_switch(dec->refn[2].u, backward.u, 8 * x_pos, 8 * y_pos, | ||
1026 : | b_uv_dx, b_uv_dy, stride2, 0); | ||
1027 : | interpolate8x8_switch(dec->refn[2].v, backward.v, 8 * x_pos, 8 * y_pos, | ||
1028 : | b_uv_dx, b_uv_dy, stride2, 0); | ||
1029 : | |||
1030 : | interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos, 16 * y_pos, | ||
1031 : | stride); | ||
1032 : | interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos + 8, 16 * y_pos, | ||
1033 : | stride); | ||
1034 : | interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos, 16 * y_pos + 8, | ||
1035 : | stride); | ||
1036 : | interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16 * x_pos + 8, | ||
1037 : | 16 * y_pos + 8, stride); | ||
1038 : | interpolate8x8_c(dec->cur.u, dec->refn[2].u, 8 * x_pos, 8 * y_pos, | ||
1039 : | stride2); | ||
1040 : | interpolate8x8_c(dec->cur.v, dec->refn[2].v, 8 * x_pos, 8 * y_pos, | ||
1041 : | stride2); | ||
1042 : | chenm001 | 1.14 | stop_comp_timer(); |
1043 : | edgomez | 1.19 | |
1044 : | for (i = 0; i < 6; i++) { | ||
1045 : | if (cbp & (1 << (5 - i))) // coded | ||
1046 : | chenm001 | 1.15 | { |
1047 : | edgomez | 1.19 | memset(&block[i * 64], 0, 64 * sizeof(int16_t)); // clear |
1048 : | chenm001 | 1.14 | |
1049 : | chenm001 | 1.15 | start_timer(); |
1050 : | edgomez | 1.19 | get_inter_block(bs, &block[i * 64]); |
1051 : | chenm001 | 1.15 | stop_coding_timer(); |
1052 : | |||
1053 : | start_timer(); | ||
1054 : | edgomez | 1.19 | if (dec->quant_type == 0) { |
1055 : | dequant_inter(&data[i * 64], &block[i * 64], iQuant); | ||
1056 : | } else { | ||
1057 : | dequant4_inter(&data[i * 64], &block[i * 64], iQuant); | ||
1058 : | chenm001 | 1.15 | } |
1059 : | stop_iquant_timer(); | ||
1060 : | |||
1061 : | start_timer(); | ||
1062 : | edgomez | 1.19 | idct(&data[i * 64]); |
1063 : | chenm001 | 1.15 | stop_idct_timer(); |
1064 : | } | ||
1065 : | } | ||
1066 : | |||
1067 : | edgomez | 1.19 | if (dec->interlacing && pMB->field_dct) { |
1068 : | chenm001 | 1.15 | next_block = stride; |
1069 : | stride *= 2; | ||
1070 : | } | ||
1071 : | |||
1072 : | start_timer(); | ||
1073 : | if (cbp & 32) | ||
1074 : | edgomez | 1.19 | transfer_16to8add(pY_Cur, &data[0 * 64], stride); |
1075 : | chenm001 | 1.15 | if (cbp & 16) |
1076 : | edgomez | 1.19 | transfer_16to8add(pY_Cur + 8, &data[1 * 64], stride); |
1077 : | chenm001 | 1.15 | if (cbp & 8) |
1078 : | edgomez | 1.19 | transfer_16to8add(pY_Cur + next_block, &data[2 * 64], stride); |
1079 : | chenm001 | 1.15 | if (cbp & 4) |
1080 : | edgomez | 1.19 | transfer_16to8add(pY_Cur + 8 + next_block, &data[3 * 64], stride); |
1081 : | chenm001 | 1.15 | if (cbp & 2) |
1082 : | edgomez | 1.19 | transfer_16to8add(pU_Cur, &data[4 * 64], stride2); |
1083 : | chenm001 | 1.15 | if (cbp & 1) |
1084 : | edgomez | 1.19 | transfer_16to8add(pV_Cur, &data[5 * 64], stride2); |
1085 : | chenm001 | 1.15 | stop_transfer_timer(); |
1086 : | chenm001 | 1.14 | } |
1087 : | |||
1088 : | |||
1089 : | // add by MinChen <chenm001@163.com> | ||
1090 : | // for decode B-frame dbquant | ||
1091 : | edgomez | 1.19 | int32_t __inline |
1092 : | get_dbquant(Bitstream * bs) | ||
1093 : | chenm001 | 1.14 | { |
1094 : | edgomez | 1.19 | if (!BitstreamGetBit(bs)) // '0' |
1095 : | return (0); | ||
1096 : | chenm001 | 1.14 | else if (!BitstreamGetBit(bs)) // '10' |
1097 : | edgomez | 1.19 | return (-2); |
1098 : | chenm001 | 1.14 | else |
1099 : | edgomez | 1.19 | return (2); // '11' |
1100 : | chenm001 | 1.14 | } |
1101 : | |||
1102 : | // add by MinChen <chenm001@163.com> | ||
1103 : | // for decode B-frame mb_type | ||
1104 : | // bit ret_value | ||
1105 : | // 1 0 | ||
1106 : | // 01 1 | ||
1107 : | // 001 2 | ||
1108 : | // 0001 3 | ||
1109 : | edgomez | 1.19 | int32_t __inline |
1110 : | get_mbtype(Bitstream * bs) | ||
1111 : | chenm001 | 1.14 | { |
1112 : | chenm001 | 1.15 | int32_t mb_type; |
1113 : | edgomez | 1.19 | |
1114 : | for (mb_type = 0; mb_type <= 3; mb_type++) { | ||
1115 : | if (BitstreamGetBit(bs)) | ||
1116 : | chenm001 | 1.15 | break; |
1117 : | } | ||
1118 : | |||
1119 : | edgomez | 1.19 | if (mb_type <= 3) |
1120 : | return (mb_type); | ||
1121 : | chenm001 | 1.14 | else |
1122 : | edgomez | 1.19 | return (-1); |
1123 : | chenm001 | 1.14 | } |
1124 : | |||
1125 : | edgomez | 1.19 | void |
1126 : | decoder_bframe(DECODER * dec, | ||
1127 : | Bitstream * bs, | ||
1128 : | int quant, | ||
1129 : | int fcode_forward, | ||
1130 : | int fcode_backward) | ||
1131 : | chenm001 | 1.14 | { |
1132 : | edgomez | 1.19 | uint32_t x, y; |
1133 : | chenm001 | 1.30 | VECTOR mv; |
1134 : | const VECTOR zeromv = {0,0}; | ||
1135 : | chenm001 | 1.27 | #ifdef BFRAMES_DEC_DEBUG |
1136 : | FILE *fp; | ||
1137 : | static char first=0; | ||
1138 : | #define BFRAME_DEBUG if (!first && fp){ \ | ||
1139 : | fprintf(fp,"Y=%3d X=%3d MB=%2d CBP=%02X\n",y,x,mb->mb_type,mb->cbp); \ | ||
1140 : | } | ||
1141 : | #endif | ||
1142 : | chenm001 | 1.14 | |
1143 : | start_timer(); | ||
1144 : | edgomez | 1.19 | image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, |
1145 : | dec->width, dec->height, dec->interlacing); | ||
1146 : | chenm001 | 1.30 | image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, |
1147 : | dec->width, dec->height, dec->interlacing); | ||
1148 : | chenm001 | 1.14 | stop_edges_timer(); |
1149 : | |||
1150 : | chenm001 | 1.27 | #ifdef BFRAMES_DEC_DEBUG |
1151 : | if (!first){ | ||
1152 : | fp=fopen("C:\\XVIDDBG.TXT","w"); | ||
1153 : | } | ||
1154 : | #endif | ||
1155 : | chenm001 | 1.15 | |
1156 : | edgomez | 1.19 | for (y = 0; y < dec->mb_height; y++) { |
1157 : | chenm001 | 1.15 | // Initialize Pred Motion Vector |
1158 : | chenm001 | 1.30 | dec->p_fmv = dec->p_bmv = zeromv; |
1159 : | edgomez | 1.19 | for (x = 0; x < dec->mb_width; x++) { |
1160 : | MACROBLOCK *mb = &dec->mbs[y * dec->mb_width + x]; | ||
1161 : | MACROBLOCK *last_mb = &dec->last_mbs[y * dec->mb_width + x]; | ||
1162 : | chenm001 | 1.14 | |
1163 : | chenm001 | 1.30 | mv = |
1164 : | mb->b_mvs[0] = mb->b_mvs[1] = mb->b_mvs[2] = mb->b_mvs[3] = | ||
1165 : | mb->mvs[0] = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = zeromv; | ||
1166 : | chenm001 | 1.15 | |
1167 : | chenm001 | 1.14 | // the last P_VOP is skip macroblock ? |
1168 : | edgomez | 1.19 | if (last_mb->mode == MODE_NOT_CODED) { |
1169 : | chenm001 | 1.15 | //DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y); |
1170 : | chenm001 | 1.27 | mb->cbp = 0; |
1171 : | #ifdef BFRAMES_DEC_DEBUG | ||
1172 : | chenm001 | 1.29 | mb->mb_type = MODE_NOT_CODED; |
1173 : | chenm001 | 1.27 | BFRAME_DEBUG |
1174 : | #endif | ||
1175 : | chenm001 | 1.15 | mb->mb_type = MODE_FORWARD; |
1176 : | chenm001 | 1.30 | mb->quant = last_mb->quant; |
1177 : | //mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x; | ||
1178 : | //mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y; | ||
1179 : | chenm001 | 1.14 | |
1180 : | chenm001 | 1.30 | decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, mb->quant, 1); |
1181 : | chenm001 | 1.14 | continue; |
1182 : | } | ||
1183 : | chenm001 | 1.15 | |
1184 : | edgomez | 1.19 | if (!BitstreamGetBit(bs)) { // modb=='0' |
1185 : | const uint8_t modb2 = BitstreamGetBit(bs); | ||
1186 : | chenm001 | 1.14 | |
1187 : | mb->mb_type = get_mbtype(bs); | ||
1188 : | edgomez | 1.19 | |
1189 : | if (!modb2) { // modb=='00' | ||
1190 : | mb->cbp = BitstreamGetBits(bs, 6); | ||
1191 : | chenm001 | 1.14 | } else { |
1192 : | mb->cbp = 0; | ||
1193 : | } | ||
1194 : | edgomez | 1.19 | if (mb->mb_type && mb->cbp) { |
1195 : | chenm001 | 1.14 | quant += get_dbquant(bs); |
1196 : | |||
1197 : | edgomez | 1.19 | if (quant > 31) { |
1198 : | chenm001 | 1.14 | quant = 31; |
1199 : | chenm001 | 1.26 | } else if (quant < 1) { |
1200 : | chenm001 | 1.14 | quant = 1; |
1201 : | } | ||
1202 : | } | ||
1203 : | } else { | ||
1204 : | mb->mb_type = MODE_DIRECT_NONE_MV; | ||
1205 : | edgomez | 1.19 | mb->cbp = 0; |
1206 : | chenm001 | 1.14 | } |
1207 : | |||
1208 : | chenm001 | 1.30 | mb->quant = quant; |
1209 : | mb->mode = MODE_INTER4V; | ||
1210 : | chenm001 | 1.14 | //DEBUG1("Switch bm_type=",mb->mb_type); |
1211 : | |||
1212 : | chenm001 | 1.27 | #ifdef BFRAMES_DEC_DEBUG |
1213 : | BFRAME_DEBUG | ||
1214 : | #endif | ||
1215 : | chl | 1.33 | |
1216 : | edgomez | 1.19 | switch (mb->mb_type) { |
1217 : | chenm001 | 1.14 | case MODE_DIRECT: |
1218 : | chenm001 | 1.30 | get_b_motion_vector(dec, bs, x, y, &mv, 1, zeromv); |
1219 : | chenm001 | 1.14 | |
1220 : | case MODE_DIRECT_NONE_MV: | ||
1221 : | chl | 1.33 | { |
1222 : | chenm001 | 1.30 | const int64_t TRB = dec->time_pp - dec->time_bp, TRD = dec->time_pp; |
1223 : | chenm001 | 1.14 | int i; |
1224 : | edgomez | 1.19 | |
1225 : | for (i = 0; i < 4; i++) { | ||
1226 : | chenm001 | 1.30 | mb->mvs[i].x = (int32_t) ((TRB * last_mb->mvs[i].x) |
1227 : | / TRD + mv.x); | ||
1228 : | mb->b_mvs[i].x = (int32_t) ((mv.x == 0) | ||
1229 : | ? ((TRB - TRD) * last_mb->mvs[i].x) | ||
1230 : | / TRD | ||
1231 : | : mb->mvs[i].x - last_mb->mvs[i].x); | ||
1232 : | mb->mvs[i].y = (int32_t) ((TRB * last_mb->mvs[i].y) | ||
1233 : | / TRD + mv.y); | ||
1234 : | mb->b_mvs[i].y = (int32_t) ((mv.y == 0) | ||
1235 : | ? ((TRB - TRD) * last_mb->mvs[i].y) | ||
1236 : | / TRD | ||
1237 : | : mb->mvs[i].y - last_mb->mvs[i].y); | ||
1238 : | chenm001 | 1.14 | } |
1239 : | //DEBUG("B-frame Direct!\n"); | ||
1240 : | } | ||
1241 : | edgomez | 1.19 | decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], |
1242 : | chenm001 | 1.30 | mb, x, y, bs); |
1243 : | chenm001 | 1.14 | break; |
1244 : | |||
1245 : | case MODE_INTERPOLATE: | ||
1246 : | edgomez | 1.19 | get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward, |
1247 : | dec->p_fmv); | ||
1248 : | chenm001 | 1.30 | dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0]; |
1249 : | edgomez | 1.19 | |
1250 : | get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0], | ||
1251 : | fcode_backward, dec->p_bmv); | ||
1252 : | chenm001 | 1.30 | dec->p_bmv = mb->b_mvs[1] = mb->b_mvs[2] = |
1253 : | mb->b_mvs[3] = mb->b_mvs[0]; | ||
1254 : | chenm001 | 1.14 | |
1255 : | edgomez | 1.19 | decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], |
1256 : | chenm001 | 1.30 | mb, x, y, bs); |
1257 : | chenm001 | 1.14 | //DEBUG("B-frame Bidir!\n"); |
1258 : | break; | ||
1259 : | |||
1260 : | case MODE_BACKWARD: | ||
1261 : | edgomez | 1.19 | get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_backward, |
1262 : | dec->p_bmv); | ||
1263 : | chenm001 | 1.30 | dec->p_bmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0]; |
1264 : | chenm001 | 1.15 | |
1265 : | chenm001 | 1.30 | mb->mode = MODE_INTER; |
1266 : | chenm001 | 1.14 | decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0); |
1267 : | //DEBUG("B-frame Backward!\n"); | ||
1268 : | break; | ||
1269 : | |||
1270 : | case MODE_FORWARD: | ||
1271 : | edgomez | 1.19 | get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward, |
1272 : | dec->p_fmv); | ||
1273 : | chenm001 | 1.30 | dec->p_fmv = mb->mvs[1] = mb->mvs[2] = mb->mvs[3] = mb->mvs[0]; |
1274 : | chenm001 | 1.15 | |
1275 : | chenm001 | 1.30 | mb->mode = MODE_INTER; |
1276 : | chenm001 | 1.14 | decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1); |
1277 : | chenm001 | 1.15 | //DEBUG("B-frame Forward!\n"); |
1278 : | chenm001 | 1.14 | break; |
1279 : | |||
1280 : | default: | ||
1281 : | chl | 1.33 | DEBUG1("Not support B-frame mb_type =", mb->mb_type); |
1282 : | } | ||
1283 : | chenm001 | 1.14 | |
1284 : | edgomez | 1.19 | } // end of FOR |
1285 : | chenm001 | 1.14 | } |
1286 : | chenm001 | 1.27 | #ifdef BFRAMES_DEC_DEBUG |
1287 : | if (!first){ | ||
1288 : | first=1; | ||
1289 : | if (fp) | ||
1290 : | fclose(fp); | ||
1291 : | } | ||
1292 : | #endif | ||
1293 : | chenm001 | 1.14 | } |
1294 : | |||
1295 : | // swap two MACROBLOCK array | ||
1296 : | edgomez | 1.19 | void |
1297 : | mb_swap(MACROBLOCK ** mb1, | ||
1298 : | MACROBLOCK ** mb2) | ||
1299 : | chenm001 | 1.14 | { |
1300 : | edgomez | 1.19 | MACROBLOCK *temp = *mb1; |
1301 : | |||
1302 : | *mb1 = *mb2; | ||
1303 : | *mb2 = temp; | ||
1304 : | chenm001 | 1.14 | } |
1305 : | |||
1306 : | edgomez | 1.19 | int |
1307 : | decoder_decode(DECODER * dec, | ||
1308 : | XVID_DEC_FRAME * frame) | ||
1309 : | Isibaar | 1.1 | { |
1310 : | edgomez | 1.7 | |
1311 : | Isibaar | 1.1 | Bitstream bs; |
1312 : | uint32_t rounding; | ||
1313 : | uint32_t quant; | ||
1314 : | chenm001 | 1.14 | uint32_t fcode_forward; |
1315 : | uint32_t fcode_backward; | ||
1316 : | Isibaar | 1.1 | uint32_t intra_dc_threshold; |
1317 : | chenm001 | 1.11 | uint32_t vop_type; |
1318 : | Isibaar | 1.1 | |
1319 : | start_global_timer(); | ||
1320 : | albeu | 1.32 | |
1321 : | dec->out_frm = (frame->colorspace == XVID_CSP_EXTERN) ? frame->image : NULL; | ||
1322 : | edgomez | 1.19 | |
1323 : | Isibaar | 1.1 | BitstreamInit(&bs, frame->bitstream, frame->length); |
1324 : | |||
1325 : | chenm001 | 1.11 | // add by chenm001 <chenm001@163.com> |
1326 : | // for support B-frame to reference last 2 frame | ||
1327 : | Isibaar | 1.18 | dec->frames++; |
1328 : | edgomez | 1.19 | vop_type = |
1329 : | BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward, | ||
1330 : | &fcode_backward, &intra_dc_threshold); | ||
1331 : | |||
1332 : | dec->p_bmv.x = dec->p_bmv.y = dec->p_fmv.y = dec->p_fmv.y = 0; // init pred vector to 0 | ||
1333 : | |||
1334 : | switch (vop_type) { | ||
1335 : | case P_VOP: | ||
1336 : | decoder_pframe(dec, &bs, rounding, quant, fcode_forward, | ||
1337 : | intra_dc_threshold); | ||
1338 : | chenm001 | 1.26 | #ifdef BFRAMES_DEC |
1339 : | edgomez | 1.19 | DEBUG1("P_VOP Time=", dec->time); |
1340 : | chenm001 | 1.26 | #endif |
1341 : | Isibaar | 1.1 | break; |
1342 : | |||
1343 : | edgomez | 1.19 | case I_VOP: |
1344 : | Isibaar | 1.1 | decoder_iframe(dec, &bs, quant, intra_dc_threshold); |
1345 : | chenm001 | 1.26 | #ifdef BFRAMES_DEC |
1346 : | edgomez | 1.19 | DEBUG1("I_VOP Time=", dec->time); |
1347 : | chenm001 | 1.26 | #endif |
1348 : | Isibaar | 1.1 | break; |
1349 : | |||
1350 : | edgomez | 1.19 | case B_VOP: |
1351 : | suxen_drol | 1.21 | #ifdef BFRAMES_DEC |
1352 : | edgomez | 1.19 | if (dec->time_pp > dec->time_bp) { |
1353 : | DEBUG1("B_VOP Time=", dec->time); | ||
1354 : | chenm001 | 1.14 | decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward); |
1355 : | } else { | ||
1356 : | DEBUG("broken B-frame!"); | ||
1357 : | } | ||
1358 : | suxen_drol | 1.21 | #else |
1359 : | image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); | ||
1360 : | Isibaar | 1.18 | #endif |
1361 : | Isibaar | 1.1 | break; |
1362 : | edgomez | 1.19 | |
1363 : | case N_VOP: // vop not coded | ||
1364 : | suxen_drol | 1.21 | // when low_delay==0, N_VOP's should interpolate between the past and future frames |
1365 : | image_copy(&dec->cur, &dec->refn[0], dec->edged_width, dec->height); | ||
1366 : | Isibaar | 1.1 | break; |
1367 : | |||
1368 : | edgomez | 1.19 | default: |
1369 : | Isibaar | 1.1 | return XVID_ERR_FAIL; |
1370 : | } | ||
1371 : | |||
1372 : | chenm001 | 1.28 | #ifdef BFRAMES_DEC_DEBUG |
1373 : | chenm001 | 1.27 | if (frame->length != BitstreamPos(&bs) / 8){ |
1374 : | DEBUG2("InLen/UseLen",frame->length, BitstreamPos(&bs) / 8); | ||
1375 : | } | ||
1376 : | chenm001 | 1.28 | #endif |
1377 : | Isibaar | 1.1 | frame->length = BitstreamPos(&bs) / 8; |
1378 : | |||
1379 : | chenm001 | 1.27 | |
1380 : | suxen_drol | 1.21 | #ifdef BFRAMES_DEC |
1381 : | chenm001 | 1.17 | // test if no B_VOP |
1382 : | chenm001 | 1.36 | if (dec->low_delay || dec->frames == 0) { |
1383 : | Isibaar | 1.18 | #endif |
1384 : | chenm001 | 1.27 | image_output(&dec->cur, dec->width, dec->height, dec->edged_width, |
1385 : | edgomez | 1.19 | frame->image, frame->stride, frame->colorspace); |
1386 : | chenm001 | 1.27 | |
1387 : | suxen_drol | 1.21 | #ifdef BFRAMES_DEC |
1388 : | chenm001 | 1.17 | } else { |
1389 : | chenm001 | 1.36 | if (dec->frames >= 1) { |
1390 : | chenm001 | 1.17 | start_timer(); |
1391 : | edgomez | 1.19 | if ((vop_type == I_VOP || vop_type == P_VOP)) { |
1392 : | image_output(&dec->refn[0], dec->width, dec->height, | ||
1393 : | dec->edged_width, frame->image, frame->stride, | ||
1394 : | frame->colorspace); | ||
1395 : | chenm001 | 1.17 | } else if (vop_type == B_VOP) { |
1396 : | edgomez | 1.19 | image_output(&dec->cur, dec->width, dec->height, |
1397 : | dec->edged_width, frame->image, frame->stride, | ||
1398 : | frame->colorspace); | ||
1399 : | chenm001 | 1.17 | } |
1400 : | stop_conv_timer(); | ||
1401 : | chenm001 | 1.14 | } |
1402 : | } | ||
1403 : | Isibaar | 1.18 | #endif |
1404 : | |||
1405 : | edgomez | 1.19 | if (vop_type == I_VOP || vop_type == P_VOP) { |
1406 : | chenm001 | 1.14 | image_swap(&dec->refn[0], &dec->refn[1]); |
1407 : | image_swap(&dec->cur, &dec->refn[0]); | ||
1408 : | chenm001 | 1.31 | |
1409 : | chenm001 | 1.14 | // swap MACROBLOCK |
1410 : | chenm001 | 1.31 | // the Divx will not set the low_delay flage some times |
1411 : | // so follow code will wrong to not swap at that time | ||
1412 : | // this will broken bitstream! so I'm change it, | ||
1413 : | // But that is not the best way! can anyone tell me how | ||
1414 : | // to do another way? | ||
1415 : | // 18-07-2002 MinChen<chenm001@163.com> | ||
1416 : | //if (!dec->low_delay && vop_type == P_VOP) | ||
1417 : | if (vop_type == P_VOP) | ||
1418 : | chenm001 | 1.15 | mb_swap(&dec->mbs, &dec->last_mbs); |
1419 : | chenm001 | 1.14 | } |
1420 : | |||
1421 : | Isibaar | 1.1 | emms(); |
1422 : | |||
1423 : | stop_global_timer(); | ||
1424 : | |||
1425 : | return XVID_ERR_OK; | ||
1426 : | } |
No admin address has been configured | ViewVC Help |
Powered by ViewVC 1.0.4 |