1 |
/************************************************************************** |
/************************************************************************** |
2 |
* |
* |
3 |
* XVID MPEG-4 VIDEO CODEC |
* XVID MPEG-4 VIDEO CODEC |
4 |
* decoder main |
* - Decoder main module - |
5 |
* |
* |
6 |
* This program is an implementation of a part of one or more MPEG-4 |
* 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 |
* Video tools as specified in ISO/IEC 14496-2 standard. Those intending |
12 |
* editors and their companies, will have no liability for use of this |
* editors and their companies, will have no liability for use of this |
13 |
* software or modifications or derivatives thereof. |
* software or modifications or derivatives thereof. |
14 |
* |
* |
15 |
* This program is xvid_free software; you can redistribute it and/or modify |
* 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 |
* it under the terms of the GNU General Public License as published by |
17 |
* the xvid_free Software Foundation; either version 2 of the License, or |
* the Free Software Foundation; either version 2 of the License, or |
18 |
* (at your option) any later version. |
* (at your option) any later version. |
19 |
* |
* |
20 |
* This program is distributed in the hope that it will be useful, |
* This program is distributed in the hope that it will be useful, |
23 |
* GNU General Public License for more details. |
* GNU General Public License for more details. |
24 |
* |
* |
25 |
* You should have received a copy of the GNU General Public License |
* You should have received a copy of the GNU General Public License |
26 |
* along with this program; if not, write to the xvid_free Software |
* along with this program; if not, write to the Free Software |
27 |
* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
28 |
* |
* |
29 |
*************************************************************************/ |
*************************************************************************/ |
30 |
|
|
32 |
* |
* |
33 |
* History: |
* History: |
34 |
* |
* |
35 |
|
* 02.05.2002 add B-frame decode support(have some problem); |
36 |
|
* MinChen <chenm001@163.com> |
37 |
|
* 22.04.2002 add some B-frame decode support; chenm001 <chenm001@163.com> |
38 |
|
* 29.03.2002 interlacing fix - compensated block wasn't being used when |
39 |
|
* reconstructing blocks, thus artifacts |
40 |
|
* interlacing speedup - used transfers to re-interlace |
41 |
|
* interlaced decoding should be as fast as progressive now |
42 |
* 26.03.2002 interlacing support - moved transfers outside decode loop |
* 26.03.2002 interlacing support - moved transfers outside decode loop |
43 |
* 26.12.2001 decoder_mbinter: dequant/idct moved within if(coded) block |
* 26.12.2001 decoder_mbinter: dequant/idct moved within if(coded) block |
44 |
* 22.12.2001 block based interpolation |
* 22.12.2001 lock based interpolation |
45 |
* 01.12.2001 inital version; (c)2001 peter ross <pross@cs.rmit.edu.au> |
* 01.12.2001 inital version; (c)2001 peter ross <pross@cs.rmit.edu.au> |
46 |
* |
* |
47 |
|
* $Id: decoder.c,v 1.14 2002-05-03 00:41:22 chenm001 Exp $ |
48 |
|
* |
49 |
*************************************************************************/ |
*************************************************************************/ |
50 |
|
|
51 |
#include <stdlib.h> |
#include <stdlib.h> |
52 |
#include <string.h> // memset |
#include <string.h> |
53 |
|
|
54 |
#include "xvid.h" |
#include "xvid.h" |
55 |
#include "portab.h" |
#include "portab.h" |
64 |
#include "dct/fdct.h" |
#include "dct/fdct.h" |
65 |
#include "utils/mem_transfer.h" |
#include "utils/mem_transfer.h" |
66 |
#include "image/interpolate8x8.h" |
#include "image/interpolate8x8.h" |
|
#include "utils/mbfunctions.h" |
|
67 |
|
|
68 |
#include "bitstream/mbcoding.h" |
#include "bitstream/mbcoding.h" |
69 |
#include "prediction/mbprediction.h" |
#include "prediction/mbprediction.h" |
100 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
101 |
} |
} |
102 |
|
|
103 |
if (image_create(&dec->refn, dec->edged_width, dec->edged_height)) |
if (image_create(&dec->refn[0], dec->edged_width, dec->edged_height)) |
104 |
{ |
{ |
105 |
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
106 |
xvid_free(dec); |
xvid_free(dec); |
107 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
108 |
} |
} |
109 |
|
// add by chenm001 <chenm001@163.com> |
110 |
|
// for support B-frame to reference last 2 frame |
111 |
|
if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) |
112 |
|
{ |
113 |
|
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
114 |
|
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
115 |
|
xvid_free(dec); |
116 |
|
return XVID_ERR_MEMORY; |
117 |
|
} |
118 |
|
if (image_create(&dec->refn[2], dec->edged_width, dec->edged_height)) |
119 |
|
{ |
120 |
|
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
121 |
|
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
122 |
|
image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
123 |
|
xvid_free(dec); |
124 |
|
return XVID_ERR_MEMORY; |
125 |
|
} |
126 |
|
|
127 |
dec->mbs = xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, CACHE_LINE); |
dec->mbs = xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, CACHE_LINE); |
128 |
if (dec->mbs == NULL) |
if (dec->mbs == NULL) |
129 |
{ |
{ |
130 |
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
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 |
|
image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height); |
134 |
|
xvid_free(dec); |
135 |
|
return XVID_ERR_MEMORY; |
136 |
|
} |
137 |
|
// add by chenm001 <chenm001@163.com> |
138 |
|
// for skip MB flag |
139 |
|
dec->last_mbs = xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, CACHE_LINE); |
140 |
|
if (dec->last_mbs == NULL) |
141 |
|
{ |
142 |
|
xvid_free(dec->mbs); |
143 |
|
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
144 |
|
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); |
xvid_free(dec); |
148 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
149 |
} |
} |
150 |
|
|
151 |
init_timer(); |
init_timer(); |
152 |
create_vlc_tables(); |
|
153 |
|
// add by chenm001 <chenm001@163.com> |
154 |
|
// for support B-frame to save reference frame's time |
155 |
|
dec->frames = -1; |
156 |
|
dec->time = dec->time_base = dec->last_time_base = 0; |
157 |
|
|
158 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
159 |
} |
} |
161 |
|
|
162 |
int decoder_destroy(DECODER * dec) |
int decoder_destroy(DECODER * dec) |
163 |
{ |
{ |
164 |
|
xvid_free(dec->last_mbs); |
165 |
xvid_free(dec->mbs); |
xvid_free(dec->mbs); |
166 |
image_destroy(&dec->refn, dec->edged_width, dec->edged_height); |
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
167 |
|
image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
168 |
|
image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height); |
169 |
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
170 |
xvid_free(dec); |
xvid_free(dec); |
171 |
|
|
|
destroy_vlc_tables(); |
|
|
|
|
172 |
write_timer(); |
write_timer(); |
173 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
174 |
} |
} |
181 |
}; |
}; |
182 |
|
|
183 |
|
|
184 |
|
|
185 |
|
|
186 |
// decode an intra macroblock |
// decode an intra macroblock |
187 |
|
|
188 |
void decoder_mbintra(DECODER * dec, |
void decoder_mbintra(DECODER * dec, |
195 |
const uint32_t quant, |
const uint32_t quant, |
196 |
const uint32_t intra_dc_threshold) |
const uint32_t intra_dc_threshold) |
197 |
{ |
{ |
|
CACHE_ALIGN int16_t block[6][64]; |
|
|
CACHE_ALIGN int16_t data[6][64]; |
|
198 |
|
|
199 |
const uint32_t stride = dec->edged_width; |
DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE); |
200 |
|
DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); |
201 |
|
|
202 |
|
uint32_t stride = dec->edged_width; |
203 |
|
uint32_t stride2 = stride / 2; |
204 |
|
uint32_t next_block = stride * 8; |
205 |
uint32_t i; |
uint32_t i; |
206 |
uint32_t iQuant = pMB->quant; |
uint32_t iQuant = pMB->quant; |
207 |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
208 |
|
|
209 |
pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); |
pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); |
210 |
pU_Cur = dec->cur.u + (y_pos << 3) * (stride >> 1) + (x_pos << 3); |
pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); |
211 |
pV_Cur = dec->cur.v + (y_pos << 3) * (stride >> 1) + (x_pos << 3); |
pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); |
212 |
|
|
213 |
memset(block, 0, sizeof(block)); // clear |
memset(block, 0, 6*64*sizeof(int16_t)); // clear |
214 |
|
|
215 |
for (i = 0; i < 6; i++) |
for (i = 0; i < 6; i++) |
216 |
{ |
{ |
219 |
int start_coeff; |
int start_coeff; |
220 |
|
|
221 |
start_timer(); |
start_timer(); |
222 |
predict_acdc(dec->mbs, x_pos, y_pos, dec->mb_width, i, block[i], iQuant, iDcScaler, predictors); |
predict_acdc(dec->mbs, x_pos, y_pos, dec->mb_width, i, &block[i*64], iQuant, iDcScaler, predictors); |
223 |
if (!acpred_flag) |
if (!acpred_flag) |
224 |
{ |
{ |
225 |
pMB->acpred_directions[i] = 0; |
pMB->acpred_directions[i] = 0; |
239 |
BitstreamSkip(bs, 1); // marker |
BitstreamSkip(bs, 1); // marker |
240 |
} |
} |
241 |
|
|
242 |
block[i][0] = dc_dif; |
block[i*64 + 0] = dc_dif; |
243 |
start_coeff = 1; |
start_coeff = 1; |
244 |
} |
} |
245 |
else |
else |
250 |
start_timer(); |
start_timer(); |
251 |
if (cbp & (1 << (5-i))) // coded |
if (cbp & (1 << (5-i))) // coded |
252 |
{ |
{ |
253 |
get_intra_block(bs, block[i], pMB->acpred_directions[i], start_coeff); |
get_intra_block(bs, &block[i*64], pMB->acpred_directions[i], start_coeff); |
254 |
} |
} |
255 |
stop_coding_timer(); |
stop_coding_timer(); |
256 |
|
|
257 |
start_timer(); |
start_timer(); |
258 |
add_acdc(pMB, i, block[i], iDcScaler, predictors); |
add_acdc(pMB, i, &block[i*64], iDcScaler, predictors); |
259 |
stop_prediction_timer(); |
stop_prediction_timer(); |
260 |
|
|
261 |
start_timer(); |
start_timer(); |
262 |
if (dec->quant_type == 0) |
if (dec->quant_type == 0) |
263 |
{ |
{ |
264 |
dequant_intra(data[i], block[i], iQuant, iDcScaler); |
dequant_intra(&data[i*64], &block[i*64], iQuant, iDcScaler); |
265 |
} |
} |
266 |
else |
else |
267 |
{ |
{ |
268 |
dequant4_intra(data[i], block[i], iQuant, iDcScaler); |
dequant4_intra(&data[i*64], &block[i*64], iQuant, iDcScaler); |
269 |
} |
} |
270 |
stop_iquant_timer(); |
stop_iquant_timer(); |
271 |
|
|
272 |
start_timer(); |
start_timer(); |
273 |
idct(data[i]); |
idct(&data[i*64]); |
274 |
stop_idct_timer(); |
stop_idct_timer(); |
275 |
} |
} |
276 |
|
|
|
start_timer(); |
|
277 |
if (dec->interlacing && pMB->field_dct) |
if (dec->interlacing && pMB->field_dct) |
278 |
{ |
{ |
279 |
MBFieldToFrame(data); |
next_block = stride; |
280 |
|
stride *= 2; |
281 |
} |
} |
|
stop_interlacing_timer(); |
|
282 |
|
|
283 |
start_timer(); |
start_timer(); |
284 |
transfer_16to8copy(pY_Cur, data[0], stride); |
transfer_16to8copy(pY_Cur, &data[0*64], stride); |
285 |
transfer_16to8copy(pY_Cur + 8, data[1], stride); |
transfer_16to8copy(pY_Cur + 8, &data[1*64], stride); |
286 |
transfer_16to8copy(pY_Cur + 8 * stride, data[2], stride); |
transfer_16to8copy(pY_Cur + next_block, &data[2*64], stride); |
287 |
transfer_16to8copy(pY_Cur + 8 + 8 * stride, data[3], stride); |
transfer_16to8copy(pY_Cur + 8 + next_block, &data[3*64], stride); |
288 |
transfer_16to8copy(pU_Cur, data[4], stride / 2); |
transfer_16to8copy(pU_Cur, &data[4*64], stride2); |
289 |
transfer_16to8copy(pV_Cur, data[5], stride / 2); |
transfer_16to8copy(pV_Cur, &data[5*64], stride2); |
290 |
stop_transfer_timer(); |
stop_transfer_timer(); |
291 |
} |
} |
292 |
|
|
312 |
const uint32_t quant, |
const uint32_t quant, |
313 |
const uint32_t rounding) |
const uint32_t rounding) |
314 |
{ |
{ |
|
CACHE_ALIGN int16_t block[6][64]; |
|
|
CACHE_ALIGN int16_t data[6][64]; |
|
315 |
|
|
316 |
const uint32_t stride = dec->edged_width; |
DECLARE_ALIGNED_MATRIX(block,6, 64, int16_t, CACHE_LINE); |
317 |
const uint32_t stride2 = dec->edged_width / 2; |
DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); |
318 |
|
|
319 |
|
uint32_t stride = dec->edged_width; |
320 |
|
uint32_t stride2 = stride / 2; |
321 |
|
uint32_t next_block = stride * 8; |
322 |
uint32_t i; |
uint32_t i; |
323 |
uint32_t iQuant = pMB->quant; |
uint32_t iQuant = pMB->quant; |
324 |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
325 |
int uv_dx, uv_dy; |
int uv_dx, uv_dy; |
326 |
|
|
327 |
pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); |
pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); |
328 |
pU_Cur = dec->cur.u + (y_pos << 3) * (stride >> 1) + (x_pos << 3); |
pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); |
329 |
pV_Cur = dec->cur.v + (y_pos << 3) * (stride >> 1) + (x_pos << 3); |
pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); |
330 |
|
|
331 |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) |
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) |
332 |
{ |
{ |
347 |
} |
} |
348 |
|
|
349 |
start_timer(); |
start_timer(); |
350 |
interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x_pos, 16*y_pos , pMB->mvs[0].x, pMB->mvs[0].y, stride, rounding); |
interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos , pMB->mvs[0].x, pMB->mvs[0].y, stride, rounding); |
351 |
interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x_pos + 8, 16*y_pos , pMB->mvs[1].x, pMB->mvs[1].y, stride, rounding); |
interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos , pMB->mvs[1].x, pMB->mvs[1].y, stride, rounding); |
352 |
interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x_pos, 16*y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride, rounding); |
interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos, 16*y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride, rounding); |
353 |
interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x_pos + 8, 16*y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride, rounding); |
interpolate8x8_switch(dec->cur.y, dec->refn[0].y, 16*x_pos + 8, 16*y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride, rounding); |
354 |
interpolate8x8_switch(dec->cur.u, dec->refn.u, 8*x_pos, 8*y_pos, uv_dx, uv_dy, stride2, rounding); |
interpolate8x8_switch(dec->cur.u, dec->refn[0].u, 8*x_pos, 8*y_pos, uv_dx, uv_dy, stride2, rounding); |
355 |
interpolate8x8_switch(dec->cur.v, dec->refn.v, 8*x_pos, 8*y_pos, uv_dx, uv_dy, stride2, rounding); |
interpolate8x8_switch(dec->cur.v, dec->refn[0].v, 8*x_pos, 8*y_pos, uv_dx, uv_dy, stride2, rounding); |
356 |
stop_comp_timer(); |
stop_comp_timer(); |
357 |
|
|
358 |
for (i = 0; i < 6; i++) |
for (i = 0; i < 6; i++) |
359 |
{ |
{ |
360 |
if (cbp & (1 << (5-i))) // coded |
if (cbp & (1 << (5-i))) // coded |
361 |
{ |
{ |
362 |
memset(block[i], 0, 64 * sizeof(int16_t)); // clear |
memset(&block[i*64], 0, 64 * sizeof(int16_t)); // clear |
363 |
|
|
364 |
start_timer(); |
start_timer(); |
365 |
get_inter_block(bs, block[i]); |
get_inter_block(bs, &block[i*64]); |
366 |
stop_coding_timer(); |
stop_coding_timer(); |
367 |
|
|
368 |
start_timer(); |
start_timer(); |
369 |
if (dec->quant_type == 0) |
if (dec->quant_type == 0) |
370 |
{ |
{ |
371 |
dequant_inter(data[i], block[i], iQuant); |
dequant_inter(&data[i*64], &block[i*64], iQuant); |
372 |
} |
} |
373 |
else |
else |
374 |
{ |
{ |
375 |
dequant4_inter(data[i], block[i], iQuant); |
dequant4_inter(&data[i*64], &block[i*64], iQuant); |
376 |
} |
} |
377 |
stop_iquant_timer(); |
stop_iquant_timer(); |
378 |
|
|
379 |
start_timer(); |
start_timer(); |
380 |
idct(data[i]); |
idct(&data[i*64]); |
381 |
stop_idct_timer(); |
stop_idct_timer(); |
382 |
} |
} |
383 |
} |
} |
384 |
|
|
385 |
start_timer(); |
if (dec->interlacing && pMB->field_dct) |
|
if (pMB->field_dct) |
|
386 |
{ |
{ |
387 |
MBFieldToFrame(data); |
next_block = stride; |
388 |
|
stride *= 2; |
389 |
} |
} |
|
stop_interlacing_timer(); |
|
390 |
|
|
391 |
start_timer(); |
start_timer(); |
392 |
if (cbp & 32) |
if (cbp & 32) |
393 |
transfer_16to8add(pY_Cur, data[0], stride); |
transfer_16to8add(pY_Cur, &data[0*64], stride); |
394 |
if (cbp & 16) |
if (cbp & 16) |
395 |
transfer_16to8add(pY_Cur + 8, data[1], stride); |
transfer_16to8add(pY_Cur + 8, &data[1*64], stride); |
396 |
if (cbp & 8) |
if (cbp & 8) |
397 |
transfer_16to8add(pY_Cur + 8 * stride, data[2], stride); |
transfer_16to8add(pY_Cur + next_block, &data[2*64], stride); |
398 |
if (cbp & 4) |
if (cbp & 4) |
399 |
transfer_16to8add(pY_Cur + 8 + 8 * stride, data[3], stride); |
transfer_16to8add(pY_Cur + 8 + next_block, &data[3*64], stride); |
400 |
if (cbp & 2) |
if (cbp & 2) |
401 |
transfer_16to8add(pU_Cur, data[4], stride / 2); |
transfer_16to8add(pU_Cur, &data[4*64], stride2); |
402 |
if (cbp & 1) |
if (cbp & 1) |
403 |
transfer_16to8add(pV_Cur, data[5], stride / 2); |
transfer_16to8add(pV_Cur, &data[5*64], stride2); |
404 |
stop_transfer_timer(); |
stop_transfer_timer(); |
405 |
} |
} |
406 |
|
|
407 |
|
|
408 |
void decoder_iframe(DECODER * dec, Bitstream * bs, int quant, int intra_dc_threshold) |
void decoder_iframe(DECODER * dec, Bitstream * bs, int quant, int intra_dc_threshold) |
409 |
{ |
{ |
410 |
|
|
411 |
uint32_t x, y; |
uint32_t x, y; |
412 |
|
|
413 |
for (y = 0; y < dec->mb_height; y++) |
for (y = 0; y < dec->mb_height; y++) |
460 |
decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, intra_dc_threshold); |
decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, intra_dc_threshold); |
461 |
} |
} |
462 |
} |
} |
463 |
|
|
464 |
} |
} |
465 |
|
|
466 |
|
|
467 |
void get_motion_vector(DECODER *dec, Bitstream *bs, int x, int y, int k, VECTOR * mv, int fcode) |
void get_motion_vector(DECODER *dec, Bitstream *bs, int x, int y, int k, VECTOR * mv, int fcode) |
468 |
{ |
{ |
469 |
|
|
470 |
int scale_fac = 1 << (fcode - 1); |
int scale_fac = 1 << (fcode - 1); |
471 |
int high = (32 * scale_fac) - 1; |
int high = (32 * scale_fac) - 1; |
472 |
int low = ((-32) * scale_fac); |
int low = ((-32) * scale_fac); |
473 |
int range = (64 * scale_fac); |
int range = (64 * scale_fac); |
474 |
|
|
475 |
VECTOR pmv[4]; |
VECTOR pmv[4]; |
476 |
uint32_t psad[4]; |
int32_t psad[4]; |
477 |
|
|
478 |
int mv_x, mv_y; |
int mv_x, mv_y; |
479 |
int pmv_x, pmv_y; |
int pmv_x, pmv_y; |
516 |
|
|
517 |
void decoder_pframe(DECODER * dec, Bitstream * bs, int rounding, int quant, int fcode, int intra_dc_threshold) |
void decoder_pframe(DECODER * dec, Bitstream * bs, int rounding, int quant, int fcode, int intra_dc_threshold) |
518 |
{ |
{ |
|
uint32_t x, y; |
|
519 |
|
|
520 |
image_swap(&dec->cur, &dec->refn); |
uint32_t x, y; |
521 |
|
|
522 |
start_timer(); |
start_timer(); |
523 |
image_setedges(&dec->refn, dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing); |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing); |
524 |
stop_edges_timer(); |
stop_edges_timer(); |
525 |
|
|
526 |
for (y = 0; y < dec->mb_height; y++) |
for (y = 0; y < dec->mb_height; y++) |
529 |
{ |
{ |
530 |
MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x]; |
MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x]; |
531 |
|
|
532 |
if (!BitstreamGetBit(bs)) // not_coded |
//if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs))) // not_coded |
533 |
|
if (!(BitstreamGetBit(bs))) // not_coded |
534 |
{ |
{ |
535 |
uint32_t mcbpc; |
uint32_t mcbpc; |
536 |
uint32_t cbpc; |
uint32_t cbpc; |
637 |
start_timer(); |
start_timer(); |
638 |
|
|
639 |
transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x), |
transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x), |
640 |
dec->refn.y + (16*y)*dec->edged_width + (16*x), |
dec->refn[0].y + (16*y)*dec->edged_width + (16*x), |
641 |
dec->edged_width); |
dec->edged_width); |
642 |
|
|
643 |
transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x+8), |
transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x+8), |
644 |
dec->refn.y + (16*y)*dec->edged_width + (16*x+8), |
dec->refn[0].y + (16*y)*dec->edged_width + (16*x+8), |
645 |
dec->edged_width); |
dec->edged_width); |
646 |
|
|
647 |
transfer8x8_copy(dec->cur.y + (16*y+8)*dec->edged_width + (16*x), |
transfer8x8_copy(dec->cur.y + (16*y+8)*dec->edged_width + (16*x), |
648 |
dec->refn.y + (16*y+8)*dec->edged_width + (16*x), |
dec->refn[0].y + (16*y+8)*dec->edged_width + (16*x), |
649 |
dec->edged_width); |
dec->edged_width); |
650 |
|
|
651 |
transfer8x8_copy(dec->cur.y + (16*y+8)*dec->edged_width + (16*x+8), |
transfer8x8_copy(dec->cur.y + (16*y+8)*dec->edged_width + (16*x+8), |
652 |
dec->refn.y + (16*y+8)*dec->edged_width + (16*x+8), |
dec->refn[0].y + (16*y+8)*dec->edged_width + (16*x+8), |
653 |
dec->edged_width); |
dec->edged_width); |
654 |
|
|
655 |
transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x), |
transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x), |
656 |
dec->refn.u + (8*y)*dec->edged_width/2 + (8*x), |
dec->refn[0].u + (8*y)*dec->edged_width/2 + (8*x), |
657 |
dec->edged_width/2); |
dec->edged_width/2); |
658 |
|
|
659 |
transfer8x8_copy(dec->cur.v + (8*y)*dec->edged_width/2 + (8*x), |
transfer8x8_copy(dec->cur.v + (8*y)*dec->edged_width/2 + (8*x), |
660 |
dec->refn.v + (8*y)*dec->edged_width/2 + (8*x), |
dec->refn[0].v + (8*y)*dec->edged_width/2 + (8*x), |
661 |
dec->edged_width/2); |
dec->edged_width/2); |
662 |
|
|
663 |
stop_transfer_timer(); |
stop_transfer_timer(); |
666 |
} |
} |
667 |
} |
} |
668 |
|
|
669 |
|
|
670 |
|
// add by MinChen <chenm001@163.com> |
671 |
|
// decode B-frame motion vector |
672 |
|
void get_b_motion_vector(DECODER *dec, Bitstream *bs, int x, int y, VECTOR * mv, int fcode, const VECTOR pmv) |
673 |
|
{ |
674 |
|
int scale_fac = 1 << (fcode - 1); |
675 |
|
int high = (32 * scale_fac) - 1; |
676 |
|
int low = ((-32) * scale_fac); |
677 |
|
int range = (64 * scale_fac); |
678 |
|
|
679 |
|
int mv_x, mv_y; |
680 |
|
int pmv_x, pmv_y; |
681 |
|
|
682 |
|
pmv_x = pmv.x; |
683 |
|
pmv_y = pmv.y; |
684 |
|
|
685 |
|
mv_x = get_mv(bs, fcode); |
686 |
|
mv_y = get_mv(bs, fcode); |
687 |
|
|
688 |
|
mv_x += pmv_x; |
689 |
|
mv_y += pmv_y; |
690 |
|
|
691 |
|
if (mv_x < low) |
692 |
|
{ |
693 |
|
mv_x += range; |
694 |
|
} |
695 |
|
else if (mv_x > high) |
696 |
|
{ |
697 |
|
mv_x -= range; |
698 |
|
} |
699 |
|
|
700 |
|
if (mv_y < low) |
701 |
|
{ |
702 |
|
mv_y += range; |
703 |
|
} |
704 |
|
else if (mv_y > high) |
705 |
|
{ |
706 |
|
mv_y -= range; |
707 |
|
} |
708 |
|
|
709 |
|
mv->x = mv_x; |
710 |
|
mv->y = mv_y; |
711 |
|
|
712 |
|
} |
713 |
|
|
714 |
|
|
715 |
|
// add by MinChen <chenm001@163.com> |
716 |
|
// decode an B-frame forward & backward inter macroblock |
717 |
|
void decoder_bf_mbinter(DECODER * dec, |
718 |
|
const MACROBLOCK * pMB, |
719 |
|
const uint32_t x_pos, |
720 |
|
const uint32_t y_pos, |
721 |
|
const uint32_t cbp, |
722 |
|
Bitstream * bs, |
723 |
|
const uint32_t quant, |
724 |
|
const uint8_t ref) |
725 |
|
{ |
726 |
|
|
727 |
|
DECLARE_ALIGNED_MATRIX(block,6, 64, int16_t, CACHE_LINE); |
728 |
|
DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); |
729 |
|
|
730 |
|
uint32_t stride = dec->edged_width; |
731 |
|
uint32_t stride2 = stride / 2; |
732 |
|
uint32_t next_block = stride * 8; |
733 |
|
uint32_t i; |
734 |
|
uint32_t iQuant = pMB->quant; |
735 |
|
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
736 |
|
int uv_dx, uv_dy; |
737 |
|
|
738 |
|
pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); |
739 |
|
pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); |
740 |
|
pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); |
741 |
|
|
742 |
|
if (!(pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) |
743 |
|
{ |
744 |
|
uv_dx = pMB->mvs[0].x; |
745 |
|
uv_dy = pMB->mvs[0].y; |
746 |
|
|
747 |
|
uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2; |
748 |
|
uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2; |
749 |
|
} |
750 |
|
else |
751 |
|
{ |
752 |
|
int sum; |
753 |
|
sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
754 |
|
uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) ); |
755 |
|
|
756 |
|
sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
757 |
|
uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) ); |
758 |
|
} |
759 |
|
|
760 |
|
start_timer(); |
761 |
|
interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos , pMB->mvs[0].x, pMB->mvs[0].y, stride, 0); |
762 |
|
interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos , pMB->mvs[1].x, pMB->mvs[1].y, stride, 0); |
763 |
|
interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos, 16*y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride, 0); |
764 |
|
interpolate8x8_switch(dec->cur.y, dec->refn[ref].y, 16*x_pos + 8, 16*y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride, 0); |
765 |
|
interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8*x_pos, 8*y_pos, uv_dx, uv_dy, stride2, 0); |
766 |
|
interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8*x_pos, 8*y_pos, uv_dx, uv_dy, stride2, 0); |
767 |
|
stop_comp_timer(); |
768 |
|
|
769 |
|
for (i = 0; i < 6; i++) |
770 |
|
{ |
771 |
|
if (cbp & (1 << (5-i))) // coded |
772 |
|
{ |
773 |
|
memset(&block[i*64], 0, 64 * sizeof(int16_t)); // clear |
774 |
|
|
775 |
|
start_timer(); |
776 |
|
get_inter_block(bs, &block[i*64]); |
777 |
|
stop_coding_timer(); |
778 |
|
|
779 |
|
start_timer(); |
780 |
|
if (dec->quant_type == 0) |
781 |
|
{ |
782 |
|
dequant_inter(&data[i*64], &block[i*64], iQuant); |
783 |
|
} |
784 |
|
else |
785 |
|
{ |
786 |
|
dequant4_inter(&data[i*64], &block[i*64], iQuant); |
787 |
|
} |
788 |
|
stop_iquant_timer(); |
789 |
|
|
790 |
|
start_timer(); |
791 |
|
idct(&data[i*64]); |
792 |
|
stop_idct_timer(); |
793 |
|
} |
794 |
|
} |
795 |
|
|
796 |
|
if (dec->interlacing && pMB->field_dct) |
797 |
|
{ |
798 |
|
next_block = stride; |
799 |
|
stride *= 2; |
800 |
|
} |
801 |
|
|
802 |
|
start_timer(); |
803 |
|
if (cbp & 32) |
804 |
|
transfer_16to8add(pY_Cur, &data[0*64], stride); |
805 |
|
if (cbp & 16) |
806 |
|
transfer_16to8add(pY_Cur + 8, &data[1*64], stride); |
807 |
|
if (cbp & 8) |
808 |
|
transfer_16to8add(pY_Cur + next_block, &data[2*64], stride); |
809 |
|
if (cbp & 4) |
810 |
|
transfer_16to8add(pY_Cur + 8 + next_block, &data[3*64], stride); |
811 |
|
if (cbp & 2) |
812 |
|
transfer_16to8add(pU_Cur, &data[4*64], stride2); |
813 |
|
if (cbp & 1) |
814 |
|
transfer_16to8add(pV_Cur, &data[5*64], stride2); |
815 |
|
stop_transfer_timer(); |
816 |
|
} |
817 |
|
|
818 |
|
|
819 |
|
// add by MinChen <chenm001@163.com> |
820 |
|
// decode an B-frame direct & inter macroblock |
821 |
|
void decoder_bf_interpolate_mbinter(DECODER * dec, |
822 |
|
IMAGE forward, IMAGE backward, |
823 |
|
const MACROBLOCK * pMB, |
824 |
|
const uint32_t x_pos, |
825 |
|
const uint32_t y_pos, |
826 |
|
Bitstream * bs) |
827 |
|
{ |
828 |
|
|
829 |
|
DECLARE_ALIGNED_MATRIX(block,6, 64, int16_t, CACHE_LINE); |
830 |
|
DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); |
831 |
|
|
832 |
|
uint32_t stride = dec->edged_width; |
833 |
|
uint32_t stride2 = stride / 2; |
834 |
|
uint32_t next_block = stride * 8; |
835 |
|
uint32_t iQuant = pMB->quant; |
836 |
|
int uv_dx, uv_dy; |
837 |
|
int b_uv_dx, b_uv_dy; |
838 |
|
|
839 |
|
if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) |
840 |
|
{ |
841 |
|
uv_dx = pMB->mvs[0].x; |
842 |
|
uv_dy = pMB->mvs[0].y; |
843 |
|
|
844 |
|
uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2; |
845 |
|
uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2; |
846 |
|
|
847 |
|
b_uv_dx = pMB->b_mvs[0].x; |
848 |
|
b_uv_dy = pMB->b_mvs[0].y; |
849 |
|
|
850 |
|
b_uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2; |
851 |
|
b_uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2; |
852 |
|
} |
853 |
|
else |
854 |
|
{ |
855 |
|
int sum; |
856 |
|
sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
857 |
|
uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) ); |
858 |
|
|
859 |
|
sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
860 |
|
uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) ); |
861 |
|
|
862 |
|
sum = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x; |
863 |
|
b_uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) ); |
864 |
|
|
865 |
|
sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y; |
866 |
|
b_uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) ); |
867 |
|
} |
868 |
|
|
869 |
|
|
870 |
|
start_timer(); |
871 |
|
|
872 |
|
interpolate8x8_switch(dec->cur.y, forward.y, 16*x_pos, 16*y_pos , pMB->mvs[0].x, pMB->mvs[0].y, stride, 0); |
873 |
|
interpolate8x8_switch(dec->cur.y, forward.y, 16*x_pos + 8, 16*y_pos , pMB->mvs[1].x, pMB->mvs[1].y, stride, 0); |
874 |
|
interpolate8x8_switch(dec->cur.y, forward.y, 16*x_pos, 16*y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride, 0); |
875 |
|
interpolate8x8_switch(dec->cur.y, forward.y, 16*x_pos + 8, 16*y_pos + 8, pMB->mvs[3].x, pMB->mvs[3].y, stride, 0); |
876 |
|
interpolate8x8_switch(dec->cur.u, forward.u, 8*x_pos, 8*y_pos, uv_dx, uv_dy, stride2, 0); |
877 |
|
interpolate8x8_switch(dec->cur.v, forward.v, 8*x_pos, 8*y_pos, uv_dx, uv_dy, stride2, 0); |
878 |
|
|
879 |
|
interpolate8x8_switch(dec->refn[2].y, backward.y, 16*x_pos, 16*y_pos , pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride, 0); |
880 |
|
interpolate8x8_switch(dec->refn[2].y, backward.y, 16*x_pos + 8, 16*y_pos , pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride, 0); |
881 |
|
interpolate8x8_switch(dec->refn[2].y, backward.y, 16*x_pos, 16*y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride, 0); |
882 |
|
interpolate8x8_switch(dec->refn[2].y, backward.y, 16*x_pos + 8, 16*y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride, 0); |
883 |
|
interpolate8x8_switch(dec->refn[2].u, backward.u, 8*x_pos, 8*y_pos, b_uv_dx, b_uv_dy, stride2, 0); |
884 |
|
interpolate8x8_switch(dec->refn[2].v, backward.v, 8*x_pos, 8*y_pos, b_uv_dx, b_uv_dy, stride2, 0); |
885 |
|
|
886 |
|
interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16*x_pos, 16*y_pos , stride); |
887 |
|
interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16*x_pos + 8, 16*y_pos , stride); |
888 |
|
interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16*x_pos, 16*y_pos + 8, stride); |
889 |
|
interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16*x_pos + 8, 16*y_pos + 8, stride); |
890 |
|
interpolate8x8_c(dec->cur.u, dec->refn[2].u, 8*x_pos, 8*y_pos, stride2); |
891 |
|
interpolate8x8_c(dec->cur.v, dec->refn[2].v, 8*x_pos, 8*y_pos, stride2); |
892 |
|
|
893 |
|
/* |
894 |
|
interpolate8x8_c(dec->cur.y, backward.y, 16*x_pos, 16*y_pos , pMB->b_mvs[0].x, pMB->b_mvs[0].y, stride); |
895 |
|
interpolate8x8_c(dec->cur.y, backward.y, 16*x_pos + 8, 16*y_pos , pMB->b_mvs[1].x, pMB->b_mvs[1].y, stride); |
896 |
|
interpolate8x8_c(dec->cur.y, backward.y, 16*x_pos, 16*y_pos + 8, pMB->b_mvs[2].x, pMB->b_mvs[2].y, stride); |
897 |
|
interpolate8x8_c(dec->cur.y, backward.y, 16*x_pos + 8, 16*y_pos + 8, pMB->b_mvs[3].x, pMB->b_mvs[3].y, stride); |
898 |
|
interpolate8x8_c(dec->cur.u, backward.u, 8*x_pos, 8*y_pos, uv_dx, uv_dy, stride2); |
899 |
|
interpolate8x8_c(dec->cur.v, backward.v, 8*x_pos, 8*y_pos, uv_dx, uv_dy, stride2); |
900 |
|
*/ |
901 |
|
stop_comp_timer(); |
902 |
|
|
903 |
|
|
904 |
|
} |
905 |
|
|
906 |
|
|
907 |
|
// add by MinChen <chenm001@163.com> |
908 |
|
// for decode B-frame dbquant |
909 |
|
int32_t __inline get_dbquant(Bitstream * bs) |
910 |
|
{ |
911 |
|
if (!BitstreamGetBit(bs)) // '0' |
912 |
|
return(0); |
913 |
|
else if (!BitstreamGetBit(bs)) // '10' |
914 |
|
return(-2); |
915 |
|
else |
916 |
|
return(2); // '11' |
917 |
|
} |
918 |
|
|
919 |
|
// add by MinChen <chenm001@163.com> |
920 |
|
// for decode B-frame mb_type |
921 |
|
// bit ret_value |
922 |
|
// 1 0 |
923 |
|
// 01 1 |
924 |
|
// 001 2 |
925 |
|
// 0001 3 |
926 |
|
int32_t __inline get_mbtype(Bitstream * bs) |
927 |
|
{ |
928 |
|
int32_t mb_type=0; |
929 |
|
for(;mb_type<=3 && (!BitstreamGetBit(bs));mb_type++); |
930 |
|
if (mb_type<=3) |
931 |
|
return(mb_type); |
932 |
|
else |
933 |
|
return(-1); |
934 |
|
} |
935 |
|
|
936 |
|
void decoder_bframe(DECODER * dec, Bitstream * bs, int quant, int fcode_forward, int fcode_backward) |
937 |
|
{ |
938 |
|
|
939 |
|
uint32_t x, y; |
940 |
|
VECTOR mv, zeromv; |
941 |
|
|
942 |
|
start_timer(); |
943 |
|
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing); |
944 |
|
//image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing); |
945 |
|
stop_edges_timer(); |
946 |
|
|
947 |
|
for (y = 0; y < dec->mb_height; y++) |
948 |
|
{ |
949 |
|
for (x = 0; x < dec->mb_width; x++) |
950 |
|
{ |
951 |
|
MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x]; |
952 |
|
MACROBLOCK * last_mb = &dec->last_mbs[y*dec->mb_width + x]; |
953 |
|
|
954 |
|
// the last P_VOP is skip macroblock ? |
955 |
|
|
956 |
|
if (last_mb->mode == MODE_NOT_CODED){ |
957 |
|
DEBUG("Skip MB in B-frame!"); |
958 |
|
|
959 |
|
// copy macroblock directly from ref to cur |
960 |
|
start_timer(); |
961 |
|
transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x), |
962 |
|
dec->refn[0].y + (16*y)*dec->edged_width + (16*x), |
963 |
|
dec->edged_width); |
964 |
|
transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x+8), |
965 |
|
dec->refn[0].y + (16*y)*dec->edged_width + (16*x+8), |
966 |
|
dec->edged_width); |
967 |
|
transfer8x8_copy(dec->cur.y + (16*y+8)*dec->edged_width + (16*x), |
968 |
|
dec->refn[0].y + (16*y+8)*dec->edged_width + (16*x), |
969 |
|
dec->edged_width); |
970 |
|
transfer8x8_copy(dec->cur.y + (16*y+8)*dec->edged_width + (16*x+8), |
971 |
|
dec->refn[0].y + (16*y+8)*dec->edged_width + (16*x+8), |
972 |
|
dec->edged_width); |
973 |
|
transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x), |
974 |
|
dec->refn[0].u + (8*y)*dec->edged_width/2 + (8*x), |
975 |
|
dec->edged_width/2); |
976 |
|
transfer8x8_copy(dec->cur.v + (8*y)*dec->edged_width/2 + (8*x), |
977 |
|
dec->refn[0].v + (8*y)*dec->edged_width/2 + (8*x), |
978 |
|
dec->edged_width/2); |
979 |
|
stop_transfer_timer(); |
980 |
|
|
981 |
|
DEBUG("skip MB in B-frame!"); |
982 |
|
continue; |
983 |
|
} |
984 |
|
|
985 |
|
if (!BitstreamGetBit(bs)){ // modb=='0' |
986 |
|
uint8_t modb2=BitstreamGetBit(bs); |
987 |
|
|
988 |
|
mb->mb_type = get_mbtype(bs); |
989 |
|
|
990 |
|
if (!modb2){ // modb=='00' |
991 |
|
mb->cbp = BitstreamGetBits(bs,6); |
992 |
|
} else { |
993 |
|
mb->cbp = 0; |
994 |
|
} |
995 |
|
if (mb->mb_type && mb->cbp){ |
996 |
|
quant += get_dbquant(bs); |
997 |
|
|
998 |
|
if (quant > 31) |
999 |
|
{ |
1000 |
|
quant = 31; |
1001 |
|
} |
1002 |
|
else if (mb->quant < 1) |
1003 |
|
{ |
1004 |
|
quant = 1; |
1005 |
|
} |
1006 |
|
} |
1007 |
|
mb->quant = quant; |
1008 |
|
} else { |
1009 |
|
mb->mb_type = MODE_DIRECT_NONE_MV; |
1010 |
|
mb->cbp = 0; |
1011 |
|
} |
1012 |
|
|
1013 |
|
mb->mb_type = MODE_INTER; |
1014 |
|
mb->mvs[0].x=mb->mvs[0].y=zeromv.x = zeromv.y = mv.x = mv.y = 0; |
1015 |
|
//DEBUG1("Switch bm_type=",mb->mb_type); |
1016 |
|
|
1017 |
|
switch(mb->mb_type) |
1018 |
|
{ |
1019 |
|
case MODE_DIRECT: |
1020 |
|
get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], 1, zeromv); |
1021 |
|
|
1022 |
|
case MODE_DIRECT_NONE_MV: |
1023 |
|
{ // Because this file is a C file not C++ so I use '{' to define var |
1024 |
|
const int32_t TRB=dec->time_pp-dec->time_bp, |
1025 |
|
TRD=dec->time_pp; |
1026 |
|
int i; |
1027 |
|
for(i=0;i<4;i++){ |
1028 |
|
mb->mvs[i].x = (TRB * last_mb->mvs[i].x)/TRD+mb->mvs[0].x; |
1029 |
|
mb->b_mvs[i].x = (mb->mvs[0].x==0)?((TRB-TRD)*last_mb->mvs[i].x)/TRD:mb->mvs[i].x-last_mb->mvs[i].x; |
1030 |
|
mb->mvs[i].y = (TRB * last_mb->mvs[i].y)/TRD+mb->mvs[0].y; |
1031 |
|
mb->b_mvs[i].y = (mb->mvs[0].y==0)?((TRB-TRD)*last_mb->mvs[i].y)/TRD:mb->mvs[i].y-last_mb->mvs[i].y; |
1032 |
|
|
1033 |
|
//DEBUG2("B-direct MVF=",mb->mvs[i].x,mb->mvs[i].y); |
1034 |
|
//DEBUG2("B-direct MVB=",mb->b_mvs[i].x,mb->b_mvs[i].y); |
1035 |
|
} |
1036 |
|
//DEBUG("B-frame Direct!\n"); |
1037 |
|
} |
1038 |
|
mb->mode = MODE_INTER4V; |
1039 |
|
decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], mb, x, y, bs); |
1040 |
|
break; |
1041 |
|
|
1042 |
|
case MODE_INTERPOLATE: |
1043 |
|
get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward, dec->p_fmv); |
1044 |
|
dec->p_fmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x; |
1045 |
|
dec->p_fmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y; |
1046 |
|
|
1047 |
|
get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0], fcode_backward, dec->p_bmv); |
1048 |
|
dec->p_bmv.x = mb->b_mvs[1].x = mb->b_mvs[2].x = mb->b_mvs[3].x = mb->b_mvs[0].x; |
1049 |
|
dec->p_bmv.y = mb->b_mvs[1].y = mb->b_mvs[2].y = mb->b_mvs[3].y = mb->b_mvs[0].y; |
1050 |
|
|
1051 |
|
mb->mode = MODE_INTER4V; |
1052 |
|
decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], mb, x, y, bs); |
1053 |
|
//DEBUG("B-frame Bidir!\n"); |
1054 |
|
break; |
1055 |
|
|
1056 |
|
case MODE_BACKWARD: |
1057 |
|
get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_backward, dec->p_bmv); |
1058 |
|
dec->p_bmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x; |
1059 |
|
dec->p_bmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y; |
1060 |
|
decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0); |
1061 |
|
//DEBUG("B-frame Backward!\n"); |
1062 |
|
break; |
1063 |
|
|
1064 |
|
case MODE_FORWARD: |
1065 |
|
get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward, dec->p_fmv); |
1066 |
|
dec->p_fmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x; |
1067 |
|
dec->p_fmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y; |
1068 |
|
decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1); |
1069 |
|
DEBUG("B-frame Forward!\n"); |
1070 |
|
break; |
1071 |
|
|
1072 |
|
default: |
1073 |
|
DEBUG1("Not support B-frame mb_type =",mb->mb_type); |
1074 |
|
} |
1075 |
|
|
1076 |
|
} // end of FOR |
1077 |
|
} |
1078 |
|
} |
1079 |
|
|
1080 |
|
// swap two MACROBLOCK array |
1081 |
|
void mb_swap(MACROBLOCK *mb1, MACROBLOCK *mb2) |
1082 |
|
{ |
1083 |
|
MACROBLOCK *temp=mb1; |
1084 |
|
mb1=mb2; |
1085 |
|
mb2=temp; |
1086 |
|
} |
1087 |
|
|
1088 |
int decoder_decode(DECODER * dec, XVID_DEC_FRAME * frame) |
int decoder_decode(DECODER * dec, XVID_DEC_FRAME * frame) |
1089 |
{ |
{ |
1090 |
|
|
1091 |
Bitstream bs; |
Bitstream bs; |
1092 |
uint32_t rounding; |
uint32_t rounding; |
1093 |
uint32_t quant; |
uint32_t quant; |
1094 |
uint32_t fcode; |
uint32_t fcode_forward; |
1095 |
|
uint32_t fcode_backward; |
1096 |
uint32_t intra_dc_threshold; |
uint32_t intra_dc_threshold; |
1097 |
|
uint32_t vop_type; |
1098 |
|
|
1099 |
start_global_timer(); |
start_global_timer(); |
1100 |
|
|
1101 |
BitstreamInit(&bs, frame->bitstream, frame->length); |
BitstreamInit(&bs, frame->bitstream, frame->length); |
1102 |
|
|
1103 |
switch (BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode, &intra_dc_threshold)) |
// add by chenm001 <chenm001@163.com> |
1104 |
|
// for support B-frame to reference last 2 frame |
1105 |
|
dec->frames ++; |
1106 |
|
vop_type=BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold); |
1107 |
|
|
1108 |
|
dec->p_bmv.x=dec->p_bmv.y=dec->p_fmv.y=dec->p_fmv.y=0; // init pred vector to 0 |
1109 |
|
switch (vop_type) |
1110 |
{ |
{ |
1111 |
case P_VOP : |
case P_VOP : |
1112 |
decoder_pframe(dec, &bs, rounding, quant, fcode, intra_dc_threshold); |
decoder_pframe(dec, &bs, rounding, quant, fcode_forward, intra_dc_threshold); |
1113 |
|
DEBUG1("P_VOP Time=",dec->time); |
1114 |
break; |
break; |
1115 |
|
|
1116 |
case I_VOP : |
case I_VOP : |
|
//DEBUG1("",intra_dc_threshold); |
|
1117 |
decoder_iframe(dec, &bs, quant, intra_dc_threshold); |
decoder_iframe(dec, &bs, quant, intra_dc_threshold); |
1118 |
|
DEBUG1("I_VOP Time=",dec->time); |
1119 |
break; |
break; |
1120 |
|
|
1121 |
case B_VOP : // ignore |
case B_VOP : |
1122 |
|
if (dec->time_pp > dec->time_bp){ |
1123 |
|
decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward); |
1124 |
|
DEBUG1("B_VOP Time=",dec->time); |
1125 |
|
} else { |
1126 |
|
DEBUG("broken B-frame!"); |
1127 |
|
} |
1128 |
break; |
break; |
1129 |
|
|
1130 |
case N_VOP : // vop not coded |
case N_VOP : // vop not coded |
1136 |
|
|
1137 |
frame->length = BitstreamPos(&bs) / 8; |
frame->length = BitstreamPos(&bs) / 8; |
1138 |
|
|
1139 |
|
if (dec->frames >= 1){ |
1140 |
start_timer(); |
start_timer(); |
1141 |
|
if ((vop_type == I_VOP || vop_type == P_VOP)) |
1142 |
|
{ |
1143 |
|
image_output(&dec->refn[1], dec->width, dec->height, dec->edged_width, |
1144 |
|
frame->image, frame->stride, frame->colorspace); |
1145 |
|
} else if (vop_type == B_VOP) { |
1146 |
image_output(&dec->cur, dec->width, dec->height, dec->edged_width, |
image_output(&dec->cur, dec->width, dec->height, dec->edged_width, |
1147 |
frame->image, frame->stride, frame->colorspace); |
frame->image, frame->stride, frame->colorspace); |
1148 |
|
} |
1149 |
stop_conv_timer(); |
stop_conv_timer(); |
1150 |
|
} |
1151 |
|
if (vop_type==I_VOP || vop_type==P_VOP){ |
1152 |
|
image_swap(&dec->refn[0], &dec->refn[1]); |
1153 |
|
image_swap(&dec->cur, &dec->refn[0]); |
1154 |
|
// swap MACROBLOCK |
1155 |
|
mb_swap(dec->mbs, dec->last_mbs); |
1156 |
|
} |
1157 |
|
|
1158 |
emms(); |
emms(); |
1159 |
|
|
1160 |
stop_global_timer(); |
stop_global_timer(); |
1161 |
|
|
1162 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
1163 |
|
|
1164 |
} |
} |