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