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 |
|
* 08.05.2002 add low_delay support for B_VOP decode |
36 |
|
* MinChen <chenm001@163.com> |
37 |
|
* 05.05.2002 fix some B-frame decode problem |
38 |
|
* 02.05.2002 add B-frame decode support(have some problem); |
39 |
|
* MinChen <chenm001@163.com> |
40 |
|
* 22.04.2002 add some B-frame decode support; chenm001 <chenm001@163.com> |
41 |
|
* 29.03.2002 interlacing fix - compensated block wasn't being used when |
42 |
|
* reconstructing blocks, thus artifacts |
43 |
|
* interlacing speedup - used transfers to re-interlace |
44 |
|
* interlaced decoding should be as fast as progressive now |
45 |
|
* 26.03.2002 interlacing support - moved transfers outside decode loop |
46 |
* 26.12.2001 decoder_mbinter: dequant/idct moved within if(coded) block |
* 26.12.2001 decoder_mbinter: dequant/idct moved within if(coded) block |
47 |
* 22.12.2001 block based interpolation |
* 22.12.2001 lock based interpolation |
48 |
* 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> |
49 |
* |
* |
50 |
|
* $Id: decoder.c,v 1.18 2002-05-20 17:12:53 Isibaar Exp $ |
51 |
|
* |
52 |
*************************************************************************/ |
*************************************************************************/ |
53 |
|
|
54 |
#include <stdlib.h> |
#include <stdlib.h> |
55 |
#include <string.h> // memset |
#include <string.h> |
56 |
|
|
57 |
#include "xvid.h" |
#include "xvid.h" |
58 |
#include "portab.h" |
#include "portab.h" |
103 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
104 |
} |
} |
105 |
|
|
106 |
if (image_create(&dec->refn, dec->edged_width, dec->edged_height)) |
if (image_create(&dec->refn[0], dec->edged_width, dec->edged_height)) |
107 |
|
{ |
108 |
|
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
109 |
|
xvid_free(dec); |
110 |
|
return XVID_ERR_MEMORY; |
111 |
|
} |
112 |
|
// add by chenm001 <chenm001@163.com> |
113 |
|
// for support B-frame to reference last 2 frame |
114 |
|
if (image_create(&dec->refn[1], dec->edged_width, dec->edged_height)) |
115 |
{ |
{ |
116 |
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
117 |
|
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
118 |
|
xvid_free(dec); |
119 |
|
return XVID_ERR_MEMORY; |
120 |
|
} |
121 |
|
if (image_create(&dec->refn[2], dec->edged_width, dec->edged_height)) |
122 |
|
{ |
123 |
|
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
124 |
|
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
125 |
|
image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
126 |
xvid_free(dec); |
xvid_free(dec); |
127 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
128 |
} |
} |
131 |
if (dec->mbs == NULL) |
if (dec->mbs == NULL) |
132 |
{ |
{ |
133 |
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
134 |
|
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
135 |
|
image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
136 |
|
image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height); |
137 |
|
xvid_free(dec); |
138 |
|
return XVID_ERR_MEMORY; |
139 |
|
} |
140 |
|
// add by chenm001 <chenm001@163.com> |
141 |
|
// for skip MB flag |
142 |
|
dec->last_mbs = xvid_malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height, CACHE_LINE); |
143 |
|
if (dec->last_mbs == NULL) |
144 |
|
{ |
145 |
|
xvid_free(dec->mbs); |
146 |
|
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
147 |
|
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
148 |
|
image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
149 |
|
image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height); |
150 |
xvid_free(dec); |
xvid_free(dec); |
151 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
152 |
} |
} |
153 |
|
|
154 |
init_timer(); |
init_timer(); |
155 |
create_vlc_tables(); |
|
156 |
|
// add by chenm001 <chenm001@163.com> |
157 |
|
// for support B-frame to save reference frame's time |
158 |
|
dec->frames = -1; |
159 |
|
dec->time = dec->time_base = dec->last_time_base = 0; |
160 |
|
|
161 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
162 |
} |
} |
164 |
|
|
165 |
int decoder_destroy(DECODER * dec) |
int decoder_destroy(DECODER * dec) |
166 |
{ |
{ |
167 |
|
xvid_free(dec->last_mbs); |
168 |
xvid_free(dec->mbs); |
xvid_free(dec->mbs); |
169 |
image_destroy(&dec->refn, dec->edged_width, dec->edged_height); |
image_destroy(&dec->refn[0], dec->edged_width, dec->edged_height); |
170 |
|
image_destroy(&dec->refn[1], dec->edged_width, dec->edged_height); |
171 |
|
image_destroy(&dec->refn[2], dec->edged_width, dec->edged_height); |
172 |
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
173 |
xvid_free(dec); |
xvid_free(dec); |
174 |
|
|
|
destroy_vlc_tables(); |
|
|
|
|
175 |
write_timer(); |
write_timer(); |
176 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
177 |
} |
} |
184 |
}; |
}; |
185 |
|
|
186 |
|
|
187 |
|
|
188 |
|
|
189 |
// decode an intra macroblock |
// decode an intra macroblock |
190 |
|
|
191 |
void decoder_mbintra(DECODER * dec, MACROBLOCK * mb, int x, int y, uint32_t acpred_flag, uint32_t cbp, Bitstream * bs, int quant, int intra_dc_threshold) |
void decoder_mbintra(DECODER * dec, |
192 |
{ |
MACROBLOCK * pMB, |
193 |
uint32_t k; |
const uint32_t x_pos, |
194 |
|
const uint32_t y_pos, |
195 |
|
const uint32_t acpred_flag, |
196 |
|
const uint32_t cbp, |
197 |
|
Bitstream * bs, |
198 |
|
const uint32_t quant, |
199 |
|
const uint32_t intra_dc_threshold) |
200 |
|
{ |
201 |
|
|
202 |
|
DECLARE_ALIGNED_MATRIX(block, 6, 64, int16_t, CACHE_LINE); |
203 |
|
DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); |
204 |
|
|
205 |
|
uint32_t stride = dec->edged_width; |
206 |
|
uint32_t stride2 = stride / 2; |
207 |
|
uint32_t next_block = stride * 8; |
208 |
|
uint32_t i; |
209 |
|
uint32_t iQuant = pMB->quant; |
210 |
|
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
211 |
|
|
212 |
|
pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); |
213 |
|
pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); |
214 |
|
pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); |
215 |
|
|
216 |
for (k = 0; k < 6; k++) |
memset(block, 0, 6*64*sizeof(int16_t)); // clear |
217 |
|
|
218 |
|
for (i = 0; i < 6; i++) |
219 |
{ |
{ |
220 |
uint32_t dcscalar; |
uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
|
CACHE_ALIGN int16_t block[64]; |
|
|
CACHE_ALIGN int16_t data[64]; |
|
221 |
int16_t predictors[8]; |
int16_t predictors[8]; |
222 |
int start_coeff; |
int start_coeff; |
223 |
|
|
|
dcscalar = get_dc_scaler(mb->quant, k < 4); |
|
|
|
|
224 |
start_timer(); |
start_timer(); |
225 |
predict_acdc(dec->mbs, x, y, dec->mb_width, k, block, mb->quant, dcscalar, predictors); |
predict_acdc(dec->mbs, x_pos, y_pos, dec->mb_width, i, &block[i*64], iQuant, iDcScaler, predictors); |
226 |
if (!acpred_flag) |
if (!acpred_flag) |
227 |
{ |
{ |
228 |
mb->acpred_directions[k] = 0; |
pMB->acpred_directions[i] = 0; |
229 |
} |
} |
230 |
stop_prediction_timer(); |
stop_prediction_timer(); |
231 |
|
|
|
memset(block, 0, 64*sizeof(int16_t)); // clear |
|
|
|
|
232 |
if (quant < intra_dc_threshold) |
if (quant < intra_dc_threshold) |
233 |
{ |
{ |
234 |
int dc_size; |
int dc_size; |
235 |
int dc_dif; |
int dc_dif; |
236 |
|
|
237 |
dc_size = k < 4 ? get_dc_size_lum(bs) : get_dc_size_chrom(bs); |
dc_size = i < 4 ? get_dc_size_lum(bs) : get_dc_size_chrom(bs); |
238 |
dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0 ; |
dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0 ; |
239 |
|
|
240 |
if (dc_size > 8) |
if (dc_size > 8) |
242 |
BitstreamSkip(bs, 1); // marker |
BitstreamSkip(bs, 1); // marker |
243 |
} |
} |
244 |
|
|
245 |
block[0] = dc_dif; |
block[i*64 + 0] = dc_dif; |
246 |
start_coeff = 1; |
start_coeff = 1; |
247 |
} |
} |
248 |
else |
else |
251 |
} |
} |
252 |
|
|
253 |
start_timer(); |
start_timer(); |
254 |
if (cbp & (1 << (5-k))) // coded |
if (cbp & (1 << (5-i))) // coded |
255 |
{ |
{ |
256 |
get_intra_block(bs, block, mb->acpred_directions[k], start_coeff); |
get_intra_block(bs, &block[i*64], pMB->acpred_directions[i], start_coeff); |
257 |
} |
} |
258 |
stop_coding_timer(); |
stop_coding_timer(); |
259 |
|
|
260 |
start_timer(); |
start_timer(); |
261 |
add_acdc(mb, k, block, dcscalar, predictors); |
add_acdc(pMB, i, &block[i*64], iDcScaler, predictors); |
262 |
stop_prediction_timer(); |
stop_prediction_timer(); |
263 |
|
|
264 |
start_timer(); |
start_timer(); |
265 |
if (dec->quant_type == 0) |
if (dec->quant_type == 0) |
266 |
{ |
{ |
267 |
dequant_intra(data, block, mb->quant, dcscalar); |
dequant_intra(&data[i*64], &block[i*64], iQuant, iDcScaler); |
268 |
} |
} |
269 |
else |
else |
270 |
{ |
{ |
271 |
dequant4_intra(data, block, mb->quant, dcscalar); |
dequant4_intra(&data[i*64], &block[i*64], iQuant, iDcScaler); |
272 |
} |
} |
273 |
stop_iquant_timer(); |
stop_iquant_timer(); |
274 |
|
|
275 |
start_timer(); |
start_timer(); |
276 |
idct(data); |
idct(&data[i*64]); |
277 |
stop_idct_timer(); |
stop_idct_timer(); |
|
|
|
|
start_timer(); |
|
|
if (k < 4) |
|
|
{ |
|
|
transfer_16to8copy(dec->cur.y + (16*y*dec->edged_width) + 16*x + (4*(k&2)*dec->edged_width) + 8*(k&1), data, dec->edged_width); |
|
278 |
} |
} |
279 |
else if (k == 4) |
|
280 |
{ |
if (dec->interlacing && pMB->field_dct) |
|
transfer_16to8copy(dec->cur.u+ 8*y*(dec->edged_width/2) + 8*x, data, (dec->edged_width/2)); |
|
|
} |
|
|
else // if (k == 5) |
|
281 |
{ |
{ |
282 |
transfer_16to8copy(dec->cur.v + 8*y*(dec->edged_width/2) + 8*x, data, (dec->edged_width/2)); |
next_block = stride; |
283 |
|
stride *= 2; |
284 |
} |
} |
285 |
|
|
286 |
|
start_timer(); |
287 |
|
transfer_16to8copy(pY_Cur, &data[0*64], stride); |
288 |
|
transfer_16to8copy(pY_Cur + 8, &data[1*64], stride); |
289 |
|
transfer_16to8copy(pY_Cur + next_block, &data[2*64], stride); |
290 |
|
transfer_16to8copy(pY_Cur + 8 + next_block, &data[3*64], stride); |
291 |
|
transfer_16to8copy(pU_Cur, &data[4*64], stride2); |
292 |
|
transfer_16to8copy(pV_Cur, &data[5*64], stride2); |
293 |
stop_transfer_timer(); |
stop_transfer_timer(); |
294 |
} |
} |
|
} |
|
295 |
|
|
296 |
|
|
297 |
|
|
305 |
|
|
306 |
// decode an inter macroblock |
// decode an inter macroblock |
307 |
|
|
308 |
void decoder_mbinter(DECODER * dec, MACROBLOCK * mb, int x, int y, uint32_t acpred_flag, uint32_t cbp, Bitstream * bs, int quant, int rounding) |
void decoder_mbinter(DECODER * dec, |
309 |
{ |
const MACROBLOCK * pMB, |
310 |
const uint32_t stride = dec->edged_width; |
const uint32_t x_pos, |
311 |
const uint32_t stride2 = dec->edged_width / 2; |
const uint32_t y_pos, |
312 |
|
const uint32_t acpred_flag, |
313 |
|
const uint32_t cbp, |
314 |
|
Bitstream * bs, |
315 |
|
const uint32_t quant, |
316 |
|
const uint32_t rounding) |
317 |
|
{ |
318 |
|
|
319 |
|
DECLARE_ALIGNED_MATRIX(block,6, 64, int16_t, CACHE_LINE); |
320 |
|
DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); |
321 |
|
|
322 |
|
uint32_t stride = dec->edged_width; |
323 |
|
uint32_t stride2 = stride / 2; |
324 |
|
uint32_t next_block = stride * 8; |
325 |
|
uint32_t i; |
326 |
|
uint32_t iQuant = pMB->quant; |
327 |
|
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
328 |
int uv_dx, uv_dy; |
int uv_dx, uv_dy; |
|
uint32_t k; |
|
329 |
|
|
330 |
if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) |
pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); |
331 |
|
pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); |
332 |
|
pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); |
333 |
|
|
334 |
|
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) |
335 |
{ |
{ |
336 |
uv_dx = mb->mvs[0].x; |
uv_dx = pMB->mvs[0].x; |
337 |
uv_dy = mb->mvs[0].y; |
uv_dy = pMB->mvs[0].y; |
338 |
|
|
339 |
uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2; |
uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2; |
340 |
uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2; |
uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2; |
342 |
else |
else |
343 |
{ |
{ |
344 |
int sum; |
int sum; |
345 |
sum = mb->mvs[0].x + mb->mvs[1].x + mb->mvs[2].x + mb->mvs[3].x; |
sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
346 |
uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) ); |
uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) ); |
347 |
|
|
348 |
sum = mb->mvs[0].y + mb->mvs[1].y + mb->mvs[2].y + mb->mvs[3].y; |
sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
349 |
uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) ); |
uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) ); |
350 |
} |
} |
351 |
|
|
352 |
start_timer(); |
start_timer(); |
353 |
interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x, 16*y , mb->mvs[0].x, mb->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); |
354 |
interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x + 8, 16*y , mb->mvs[1].x, mb->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); |
355 |
interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x, 16*y + 8, mb->mvs[2].x, mb->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); |
356 |
interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x + 8, 16*y + 8, mb->mvs[3].x, mb->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); |
357 |
interpolate8x8_switch(dec->cur.u, dec->refn.u, 8*x, 8*y, 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); |
358 |
interpolate8x8_switch(dec->cur.v, dec->refn.v, 8*x, 8*y, 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); |
359 |
stop_comp_timer(); |
stop_comp_timer(); |
360 |
|
|
361 |
|
for (i = 0; i < 6; i++) |
|
for (k = 0; k < 6; k++) |
|
362 |
{ |
{ |
363 |
CACHE_ALIGN int16_t block[64]; |
if (cbp & (1 << (5-i))) // coded |
|
CACHE_ALIGN int16_t data[64]; |
|
|
|
|
|
if (cbp & (1 << (5-k))) // coded |
|
364 |
{ |
{ |
365 |
memset(block, 0, 64 * sizeof(int16_t)); // clear |
memset(&block[i*64], 0, 64 * sizeof(int16_t)); // clear |
366 |
|
|
367 |
start_timer(); |
start_timer(); |
368 |
get_inter_block(bs, block); |
get_inter_block(bs, &block[i*64]); |
369 |
stop_coding_timer(); |
stop_coding_timer(); |
370 |
|
|
371 |
start_timer(); |
start_timer(); |
372 |
if (dec->quant_type == 0) |
if (dec->quant_type == 0) |
373 |
{ |
{ |
374 |
dequant_inter(data, block, mb->quant); |
dequant_inter(&data[i*64], &block[i*64], iQuant); |
375 |
} |
} |
376 |
else |
else |
377 |
{ |
{ |
378 |
dequant4_inter(data, block, mb->quant); |
dequant4_inter(&data[i*64], &block[i*64], iQuant); |
379 |
} |
} |
380 |
stop_iquant_timer(); |
stop_iquant_timer(); |
381 |
|
|
382 |
start_timer(); |
start_timer(); |
383 |
idct(data); |
idct(&data[i*64]); |
384 |
stop_idct_timer(); |
stop_idct_timer(); |
|
|
|
|
start_timer(); |
|
|
if (k < 4) |
|
|
{ |
|
|
transfer_16to8add(dec->cur.y + (16*y + 4*(k&2))*stride + 16*x + 8*(k&1), data, stride); |
|
385 |
} |
} |
|
else if (k == 4) |
|
|
{ |
|
|
transfer_16to8add(dec->cur.u + 8*y*stride2 + 8*x, data, stride2); |
|
386 |
} |
} |
387 |
else // k == 5 |
|
388 |
|
if (dec->interlacing && pMB->field_dct) |
389 |
{ |
{ |
390 |
transfer_16to8add(dec->cur.v + 8*y*stride2 + 8*x, data, stride2); |
next_block = stride; |
391 |
|
stride *= 2; |
392 |
} |
} |
393 |
|
|
394 |
|
start_timer(); |
395 |
|
if (cbp & 32) |
396 |
|
transfer_16to8add(pY_Cur, &data[0*64], stride); |
397 |
|
if (cbp & 16) |
398 |
|
transfer_16to8add(pY_Cur + 8, &data[1*64], stride); |
399 |
|
if (cbp & 8) |
400 |
|
transfer_16to8add(pY_Cur + next_block, &data[2*64], stride); |
401 |
|
if (cbp & 4) |
402 |
|
transfer_16to8add(pY_Cur + 8 + next_block, &data[3*64], stride); |
403 |
|
if (cbp & 2) |
404 |
|
transfer_16to8add(pU_Cur, &data[4*64], stride2); |
405 |
|
if (cbp & 1) |
406 |
|
transfer_16to8add(pV_Cur, &data[5*64], stride2); |
407 |
stop_transfer_timer(); |
stop_transfer_timer(); |
408 |
} |
} |
|
} |
|
|
} |
|
|
|
|
409 |
|
|
410 |
|
|
411 |
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) |
412 |
{ |
{ |
413 |
|
|
414 |
uint32_t x, y; |
uint32_t x, y; |
415 |
|
|
416 |
for (y = 0; y < dec->mb_height; y++) |
for (y = 0; y < dec->mb_height; y++) |
454 |
} |
} |
455 |
mb->quant = quant; |
mb->quant = quant; |
456 |
|
|
457 |
|
if (dec->interlacing) |
458 |
|
{ |
459 |
|
mb->field_dct = BitstreamGetBit(bs); |
460 |
|
DEBUG1("deci: field_dct: ", mb->field_dct); |
461 |
|
} |
462 |
|
|
463 |
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); |
464 |
} |
} |
465 |
} |
} |
466 |
|
|
467 |
} |
} |
468 |
|
|
469 |
|
|
470 |
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) |
471 |
{ |
{ |
472 |
|
|
473 |
int scale_fac = 1 << (fcode - 1); |
int scale_fac = 1 << (fcode - 1); |
474 |
int high = (32 * scale_fac) - 1; |
int high = (32 * scale_fac) - 1; |
475 |
int low = ((-32) * scale_fac); |
int low = ((-32) * scale_fac); |
476 |
int range = (64 * scale_fac); |
int range = (64 * scale_fac); |
477 |
|
|
478 |
VECTOR pmv[4]; |
VECTOR pmv[4]; |
479 |
uint32_t psad[4]; |
int32_t psad[4]; |
480 |
|
|
481 |
int mv_x, mv_y; |
int mv_x, mv_y; |
482 |
int pmv_x, pmv_y; |
int pmv_x, pmv_y; |
519 |
|
|
520 |
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) |
521 |
{ |
{ |
|
uint32_t x, y; |
|
522 |
|
|
523 |
image_swap(&dec->cur, &dec->refn); |
uint32_t x, y; |
524 |
|
|
525 |
start_timer(); |
start_timer(); |
526 |
image_setedges(&dec->refn, dec->edged_width, dec->edged_height, dec->width, dec->height); |
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing); |
527 |
stop_edges_timer(); |
stop_edges_timer(); |
528 |
|
|
529 |
for (y = 0; y < dec->mb_height; y++) |
for (y = 0; y < dec->mb_height; y++) |
532 |
{ |
{ |
533 |
MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x]; |
MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x]; |
534 |
|
|
535 |
if (!BitstreamGetBit(bs)) // not_coded |
//if (!(dec->mb_skip[y*dec->mb_width + x]=BitstreamGetBit(bs))) // not_coded |
536 |
|
if (!(BitstreamGetBit(bs))) // not_coded |
537 |
{ |
{ |
538 |
uint32_t mcbpc; |
uint32_t mcbpc; |
539 |
uint32_t cbpc; |
uint32_t cbpc; |
577 |
} |
} |
578 |
mb->quant = quant; |
mb->quant = quant; |
579 |
|
|
580 |
|
if (dec->interlacing) |
581 |
|
{ |
582 |
|
mb->field_dct = BitstreamGetBit(bs); |
583 |
|
DEBUG1("decp: field_dct: ", mb->field_dct); |
584 |
|
|
585 |
if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) |
if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) |
586 |
{ |
{ |
587 |
|
mb->field_pred = BitstreamGetBit(bs); |
588 |
|
DEBUG1("decp: field_pred: ", mb->field_pred); |
589 |
|
|
590 |
|
if (mb->field_pred) |
591 |
|
{ |
592 |
|
mb->field_for_top = BitstreamGetBit(bs); |
593 |
|
DEBUG1("decp: field_for_top: ", mb->field_for_top); |
594 |
|
mb->field_for_bot = BitstreamGetBit(bs); |
595 |
|
DEBUG1("decp: field_for_bot: ", mb->field_for_bot); |
596 |
|
} |
597 |
|
} |
598 |
|
} |
599 |
|
|
600 |
|
if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) |
601 |
|
{ |
602 |
|
if (dec->interlacing && mb->field_pred) |
603 |
|
{ |
604 |
|
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode); |
605 |
|
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1], fcode); |
606 |
|
} |
607 |
|
else |
608 |
|
{ |
609 |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode); |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode); |
610 |
mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x; |
mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x; |
611 |
mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y; |
mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y; |
612 |
} |
} |
613 |
|
} |
614 |
else if (mb->mode == MODE_INTER4V /* || mb->mode == MODE_INTER4V_Q */) |
else if (mb->mode == MODE_INTER4V /* || mb->mode == MODE_INTER4V_Q */) |
615 |
{ |
{ |
616 |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode); |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode); |
630 |
} |
} |
631 |
else // not coded |
else // not coded |
632 |
{ |
{ |
633 |
|
//DEBUG2("P-frame MB at (X,Y)=",x,y); |
634 |
mb->mode = MODE_NOT_CODED; |
mb->mode = MODE_NOT_CODED; |
635 |
mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0; |
mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0; |
636 |
mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0; |
mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0; |
640 |
start_timer(); |
start_timer(); |
641 |
|
|
642 |
transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x), |
transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x), |
643 |
dec->refn.y + (16*y)*dec->edged_width + (16*x), |
dec->refn[0].y + (16*y)*dec->edged_width + (16*x), |
644 |
dec->edged_width); |
dec->edged_width); |
645 |
|
|
646 |
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), |
647 |
dec->refn.y + (16*y)*dec->edged_width + (16*x+8), |
dec->refn[0].y + (16*y)*dec->edged_width + (16*x+8), |
648 |
dec->edged_width); |
dec->edged_width); |
649 |
|
|
650 |
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), |
651 |
dec->refn.y + (16*y+8)*dec->edged_width + (16*x), |
dec->refn[0].y + (16*y+8)*dec->edged_width + (16*x), |
652 |
dec->edged_width); |
dec->edged_width); |
653 |
|
|
654 |
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), |
655 |
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), |
656 |
dec->edged_width); |
dec->edged_width); |
657 |
|
|
658 |
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), |
659 |
dec->refn.u + (8*y)*dec->edged_width/2 + (8*x), |
dec->refn[0].u + (8*y)*dec->edged_width/2 + (8*x), |
660 |
dec->edged_width/2); |
dec->edged_width/2); |
661 |
|
|
662 |
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), |
663 |
dec->refn.v + (8*y)*dec->edged_width/2 + (8*x), |
dec->refn[0].v + (8*y)*dec->edged_width/2 + (8*x), |
664 |
dec->edged_width/2); |
dec->edged_width/2); |
665 |
|
|
666 |
stop_transfer_timer(); |
stop_transfer_timer(); |
669 |
} |
} |
670 |
} |
} |
671 |
|
|
672 |
|
|
673 |
|
// add by MinChen <chenm001@163.com> |
674 |
|
// decode B-frame motion vector |
675 |
|
void get_b_motion_vector(DECODER *dec, Bitstream *bs, int x, int y, VECTOR * mv, int fcode, const VECTOR pmv) |
676 |
|
{ |
677 |
|
int scale_fac = 1 << (fcode - 1); |
678 |
|
int high = (32 * scale_fac) - 1; |
679 |
|
int low = ((-32) * scale_fac); |
680 |
|
int range = (64 * scale_fac); |
681 |
|
|
682 |
|
int mv_x, mv_y; |
683 |
|
int pmv_x, pmv_y; |
684 |
|
|
685 |
|
pmv_x = pmv.x; |
686 |
|
pmv_y = pmv.y; |
687 |
|
|
688 |
|
mv_x = get_mv(bs, fcode); |
689 |
|
mv_y = get_mv(bs, fcode); |
690 |
|
|
691 |
|
mv_x += pmv_x; |
692 |
|
mv_y += pmv_y; |
693 |
|
|
694 |
|
if (mv_x < low) |
695 |
|
{ |
696 |
|
mv_x += range; |
697 |
|
} |
698 |
|
else if (mv_x > high) |
699 |
|
{ |
700 |
|
mv_x -= range; |
701 |
|
} |
702 |
|
|
703 |
|
if (mv_y < low) |
704 |
|
{ |
705 |
|
mv_y += range; |
706 |
|
} |
707 |
|
else if (mv_y > high) |
708 |
|
{ |
709 |
|
mv_y -= range; |
710 |
|
} |
711 |
|
|
712 |
|
mv->x = mv_x; |
713 |
|
mv->y = mv_y; |
714 |
|
} |
715 |
|
|
716 |
|
|
717 |
|
// add by MinChen <chenm001@163.com> |
718 |
|
// decode an B-frame forward & backward inter macroblock |
719 |
|
void decoder_bf_mbinter(DECODER * dec, |
720 |
|
const MACROBLOCK * pMB, |
721 |
|
const uint32_t x_pos, |
722 |
|
const uint32_t y_pos, |
723 |
|
const uint32_t cbp, |
724 |
|
Bitstream * bs, |
725 |
|
const uint32_t quant, |
726 |
|
const uint8_t ref) |
727 |
|
{ |
728 |
|
|
729 |
|
DECLARE_ALIGNED_MATRIX(block,6, 64, int16_t, CACHE_LINE); |
730 |
|
DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); |
731 |
|
|
732 |
|
uint32_t stride = dec->edged_width; |
733 |
|
uint32_t stride2 = stride / 2; |
734 |
|
uint32_t next_block = stride * 8; |
735 |
|
uint32_t i; |
736 |
|
uint32_t iQuant = pMB->quant; |
737 |
|
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
738 |
|
int uv_dx, uv_dy; |
739 |
|
|
740 |
|
pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); |
741 |
|
pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); |
742 |
|
pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); |
743 |
|
|
744 |
|
if (!(pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) |
745 |
|
{ |
746 |
|
uv_dx = pMB->mvs[0].x; |
747 |
|
uv_dy = pMB->mvs[0].y; |
748 |
|
|
749 |
|
uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2; |
750 |
|
uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2; |
751 |
|
} |
752 |
|
else |
753 |
|
{ |
754 |
|
int sum; |
755 |
|
sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
756 |
|
uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) ); |
757 |
|
|
758 |
|
sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
759 |
|
uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) ); |
760 |
|
} |
761 |
|
|
762 |
|
start_timer(); |
763 |
|
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); |
764 |
|
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); |
765 |
|
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); |
766 |
|
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); |
767 |
|
interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8*x_pos, 8*y_pos, uv_dx, uv_dy, stride2, 0); |
768 |
|
interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8*x_pos, 8*y_pos, uv_dx, uv_dy, stride2, 0); |
769 |
|
stop_comp_timer(); |
770 |
|
|
771 |
|
for (i = 0; i < 6; i++) |
772 |
|
{ |
773 |
|
if (cbp & (1 << (5-i))) // coded |
774 |
|
{ |
775 |
|
memset(&block[i*64], 0, 64 * sizeof(int16_t)); // clear |
776 |
|
|
777 |
|
start_timer(); |
778 |
|
get_inter_block(bs, &block[i*64]); |
779 |
|
stop_coding_timer(); |
780 |
|
|
781 |
|
start_timer(); |
782 |
|
if (dec->quant_type == 0) |
783 |
|
{ |
784 |
|
dequant_inter(&data[i*64], &block[i*64], iQuant); |
785 |
|
} |
786 |
|
else |
787 |
|
{ |
788 |
|
dequant4_inter(&data[i*64], &block[i*64], iQuant); |
789 |
|
} |
790 |
|
stop_iquant_timer(); |
791 |
|
|
792 |
|
start_timer(); |
793 |
|
idct(&data[i*64]); |
794 |
|
stop_idct_timer(); |
795 |
|
} |
796 |
|
} |
797 |
|
|
798 |
|
if (dec->interlacing && pMB->field_dct) |
799 |
|
{ |
800 |
|
next_block = stride; |
801 |
|
stride *= 2; |
802 |
|
} |
803 |
|
|
804 |
|
start_timer(); |
805 |
|
if (cbp & 32) |
806 |
|
transfer_16to8add(pY_Cur, &data[0*64], stride); |
807 |
|
if (cbp & 16) |
808 |
|
transfer_16to8add(pY_Cur + 8, &data[1*64], stride); |
809 |
|
if (cbp & 8) |
810 |
|
transfer_16to8add(pY_Cur + next_block, &data[2*64], stride); |
811 |
|
if (cbp & 4) |
812 |
|
transfer_16to8add(pY_Cur + 8 + next_block, &data[3*64], stride); |
813 |
|
if (cbp & 2) |
814 |
|
transfer_16to8add(pU_Cur, &data[4*64], stride2); |
815 |
|
if (cbp & 1) |
816 |
|
transfer_16to8add(pV_Cur, &data[5*64], stride2); |
817 |
|
stop_transfer_timer(); |
818 |
|
} |
819 |
|
|
820 |
|
|
821 |
|
// add by MinChen <chenm001@163.com> |
822 |
|
// decode an B-frame direct & inter macroblock |
823 |
|
void decoder_bf_interpolate_mbinter(DECODER * dec, |
824 |
|
IMAGE forward, IMAGE backward, |
825 |
|
const MACROBLOCK * pMB, |
826 |
|
const uint32_t x_pos, |
827 |
|
const uint32_t y_pos, |
828 |
|
const uint32_t cbp, |
829 |
|
Bitstream * bs ) |
830 |
|
{ |
831 |
|
|
832 |
|
DECLARE_ALIGNED_MATRIX(block,6, 64, int16_t, CACHE_LINE); |
833 |
|
DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); |
834 |
|
|
835 |
|
uint32_t stride = dec->edged_width; |
836 |
|
uint32_t stride2 = stride / 2; |
837 |
|
uint32_t next_block = stride * 8; |
838 |
|
uint32_t iQuant = pMB->quant; |
839 |
|
int uv_dx, uv_dy; |
840 |
|
int b_uv_dx, b_uv_dy; |
841 |
|
uint32_t i; |
842 |
|
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
843 |
|
|
844 |
|
pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); |
845 |
|
pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); |
846 |
|
pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); |
847 |
|
|
848 |
|
if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) |
849 |
|
{ |
850 |
|
uv_dx = pMB->mvs[0].x; |
851 |
|
uv_dy = pMB->mvs[0].y; |
852 |
|
|
853 |
|
uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2; |
854 |
|
uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2; |
855 |
|
|
856 |
|
b_uv_dx = pMB->b_mvs[0].x; |
857 |
|
b_uv_dy = pMB->b_mvs[0].y; |
858 |
|
|
859 |
|
b_uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2; |
860 |
|
b_uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2; |
861 |
|
} |
862 |
|
else |
863 |
|
{ |
864 |
|
int sum; |
865 |
|
sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
866 |
|
uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) ); |
867 |
|
|
868 |
|
sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
869 |
|
uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) ); |
870 |
|
|
871 |
|
sum = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x; |
872 |
|
b_uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) ); |
873 |
|
|
874 |
|
sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y; |
875 |
|
b_uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) ); |
876 |
|
} |
877 |
|
|
878 |
|
|
879 |
|
start_timer(); |
880 |
|
interpolate8x8_switch(dec->cur.y, forward.y, 16*x_pos, 16*y_pos , pMB->mvs[0].x, pMB->mvs[0].y, stride, 0); |
881 |
|
interpolate8x8_switch(dec->cur.y, forward.y, 16*x_pos + 8, 16*y_pos , pMB->mvs[1].x, pMB->mvs[1].y, stride, 0); |
882 |
|
interpolate8x8_switch(dec->cur.y, forward.y, 16*x_pos, 16*y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride, 0); |
883 |
|
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); |
884 |
|
interpolate8x8_switch(dec->cur.u, forward.u, 8*x_pos, 8*y_pos, uv_dx, uv_dy, stride2, 0); |
885 |
|
interpolate8x8_switch(dec->cur.v, forward.v, 8*x_pos, 8*y_pos, uv_dx, uv_dy, stride2, 0); |
886 |
|
|
887 |
|
|
888 |
|
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); |
889 |
|
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); |
890 |
|
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); |
891 |
|
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); |
892 |
|
interpolate8x8_switch(dec->refn[2].u, backward.u, 8*x_pos, 8*y_pos, b_uv_dx, b_uv_dy, stride2, 0); |
893 |
|
interpolate8x8_switch(dec->refn[2].v, backward.v, 8*x_pos, 8*y_pos, b_uv_dx, b_uv_dy, stride2, 0); |
894 |
|
|
895 |
|
interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16*x_pos, 16*y_pos , stride); |
896 |
|
interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16*x_pos + 8, 16*y_pos , stride); |
897 |
|
interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16*x_pos, 16*y_pos + 8, stride); |
898 |
|
interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16*x_pos + 8, 16*y_pos + 8, stride); |
899 |
|
interpolate8x8_c(dec->cur.u, dec->refn[2].u, 8*x_pos, 8*y_pos, stride2); |
900 |
|
interpolate8x8_c(dec->cur.v, dec->refn[2].v, 8*x_pos, 8*y_pos, stride2); |
901 |
|
|
902 |
|
stop_comp_timer(); |
903 |
|
|
904 |
|
for (i = 0; i < 6; i++) |
905 |
|
{ |
906 |
|
if (cbp & (1 << (5-i))) // coded |
907 |
|
{ |
908 |
|
memset(&block[i*64], 0, 64 * sizeof(int16_t)); // clear |
909 |
|
|
910 |
|
start_timer(); |
911 |
|
get_inter_block(bs, &block[i*64]); |
912 |
|
stop_coding_timer(); |
913 |
|
|
914 |
|
start_timer(); |
915 |
|
if (dec->quant_type == 0) |
916 |
|
{ |
917 |
|
dequant_inter(&data[i*64], &block[i*64], iQuant); |
918 |
|
} |
919 |
|
else |
920 |
|
{ |
921 |
|
dequant4_inter(&data[i*64], &block[i*64], iQuant); |
922 |
|
} |
923 |
|
stop_iquant_timer(); |
924 |
|
|
925 |
|
start_timer(); |
926 |
|
idct(&data[i*64]); |
927 |
|
stop_idct_timer(); |
928 |
|
} |
929 |
|
} |
930 |
|
|
931 |
|
if (dec->interlacing && pMB->field_dct) |
932 |
|
{ |
933 |
|
next_block = stride; |
934 |
|
stride *= 2; |
935 |
|
} |
936 |
|
|
937 |
|
start_timer(); |
938 |
|
if (cbp & 32) |
939 |
|
transfer_16to8add(pY_Cur, &data[0*64], stride); |
940 |
|
if (cbp & 16) |
941 |
|
transfer_16to8add(pY_Cur + 8, &data[1*64], stride); |
942 |
|
if (cbp & 8) |
943 |
|
transfer_16to8add(pY_Cur + next_block, &data[2*64], stride); |
944 |
|
if (cbp & 4) |
945 |
|
transfer_16to8add(pY_Cur + 8 + next_block, &data[3*64], stride); |
946 |
|
if (cbp & 2) |
947 |
|
transfer_16to8add(pU_Cur, &data[4*64], stride2); |
948 |
|
if (cbp & 1) |
949 |
|
transfer_16to8add(pV_Cur, &data[5*64], stride2); |
950 |
|
stop_transfer_timer(); |
951 |
|
} |
952 |
|
|
953 |
|
|
954 |
|
// add by MinChen <chenm001@163.com> |
955 |
|
// for decode B-frame dbquant |
956 |
|
int32_t __inline get_dbquant(Bitstream * bs) |
957 |
|
{ |
958 |
|
if (!BitstreamGetBit(bs)) // '0' |
959 |
|
return(0); |
960 |
|
else if (!BitstreamGetBit(bs)) // '10' |
961 |
|
return(-2); |
962 |
|
else |
963 |
|
return(2); // '11' |
964 |
|
} |
965 |
|
|
966 |
|
// add by MinChen <chenm001@163.com> |
967 |
|
// for decode B-frame mb_type |
968 |
|
// bit ret_value |
969 |
|
// 1 0 |
970 |
|
// 01 1 |
971 |
|
// 001 2 |
972 |
|
// 0001 3 |
973 |
|
int32_t __inline get_mbtype(Bitstream * bs) |
974 |
|
{ |
975 |
|
int32_t mb_type; |
976 |
|
for(mb_type=0;mb_type<=3;mb_type++){ |
977 |
|
if (BitstreamGetBit(bs)) |
978 |
|
break; |
979 |
|
} |
980 |
|
|
981 |
|
if (mb_type<=3) |
982 |
|
return(mb_type); |
983 |
|
else |
984 |
|
return(-1); |
985 |
|
} |
986 |
|
|
987 |
|
void decoder_bframe(DECODER * dec, Bitstream * bs, int quant, int fcode_forward, int fcode_backward) |
988 |
|
{ |
989 |
|
|
990 |
|
uint32_t x, y; |
991 |
|
VECTOR mv, zeromv; |
992 |
|
|
993 |
|
start_timer(); |
994 |
|
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing); |
995 |
|
//image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing); |
996 |
|
stop_edges_timer(); |
997 |
|
|
998 |
|
|
999 |
|
for (y = 0; y < dec->mb_height; y++) |
1000 |
|
{ |
1001 |
|
// Initialize Pred Motion Vector |
1002 |
|
dec->p_fmv.x = dec->p_fmv.y = dec->p_bmv.x = dec->p_bmv.y = 0; |
1003 |
|
for (x = 0; x < dec->mb_width; x++) |
1004 |
|
{ |
1005 |
|
MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x]; |
1006 |
|
MACROBLOCK * last_mb = &dec->last_mbs[y*dec->mb_width + x]; |
1007 |
|
|
1008 |
|
mb->mvs[0].x=mb->mvs[0].y=zeromv.x = zeromv.y = mv.x = mv.y = 0; |
1009 |
|
|
1010 |
|
// the last P_VOP is skip macroblock ? |
1011 |
|
if (last_mb->mode == MODE_NOT_CODED){ |
1012 |
|
//DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y); |
1013 |
|
mb->mb_type = MODE_FORWARD; |
1014 |
|
mb->cbp = 0; |
1015 |
|
mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x; |
1016 |
|
mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y; |
1017 |
|
mb->quant = 8; |
1018 |
|
|
1019 |
|
decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1); |
1020 |
|
continue; |
1021 |
|
} |
1022 |
|
|
1023 |
|
//t=BitstreamShowBits(bs,32); |
1024 |
|
|
1025 |
|
if (!BitstreamGetBit(bs)){ // modb=='0' |
1026 |
|
const uint8_t modb2=BitstreamGetBit(bs); |
1027 |
|
|
1028 |
|
mb->mb_type = get_mbtype(bs); |
1029 |
|
|
1030 |
|
if (!modb2){ // modb=='00' |
1031 |
|
mb->cbp = BitstreamGetBits(bs,6); |
1032 |
|
} else { |
1033 |
|
mb->cbp = 0; |
1034 |
|
} |
1035 |
|
if (mb->mb_type && mb->cbp){ |
1036 |
|
quant += get_dbquant(bs); |
1037 |
|
|
1038 |
|
if (quant > 31) |
1039 |
|
{ |
1040 |
|
quant = 31; |
1041 |
|
} |
1042 |
|
else if (mb->quant < 1) |
1043 |
|
{ |
1044 |
|
quant = 1; |
1045 |
|
} |
1046 |
|
} else { |
1047 |
|
quant = 8; |
1048 |
|
} |
1049 |
|
mb->quant = quant; |
1050 |
|
} else { |
1051 |
|
mb->mb_type = MODE_DIRECT_NONE_MV; |
1052 |
|
mb->cbp = 0; |
1053 |
|
} |
1054 |
|
|
1055 |
|
mb->mode = MODE_INTER; |
1056 |
|
//DEBUG1("Switch bm_type=",mb->mb_type); |
1057 |
|
|
1058 |
|
switch(mb->mb_type) |
1059 |
|
{ |
1060 |
|
case MODE_DIRECT: |
1061 |
|
get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], 1, zeromv); |
1062 |
|
|
1063 |
|
case MODE_DIRECT_NONE_MV: |
1064 |
|
{ // Because this file is a C file not C++ so I use '{' to define var |
1065 |
|
const int64_t TRB=dec->time_pp-dec->time_bp, |
1066 |
|
TRD=dec->time_pp; |
1067 |
|
int i; |
1068 |
|
for(i=0;i<4;i++){ |
1069 |
|
mb->mvs[i].x = (int32_t)((TRB * last_mb->mvs[i].x)/TRD+mb->mvs[0].x); |
1070 |
|
mb->b_mvs[i].x = (int32_t)((mb->mvs[0].x==0)?((TRB-TRD)*last_mb->mvs[i].x)/TRD:mb->mvs[i].x-last_mb->mvs[i].x); |
1071 |
|
mb->mvs[i].y = (int32_t)((TRB * last_mb->mvs[i].y)/TRD+mb->mvs[0].y); |
1072 |
|
mb->b_mvs[i].y = (int32_t)((mb->mvs[0].y==0)?((TRB-TRD)*last_mb->mvs[i].y)/TRD:mb->mvs[i].y-last_mb->mvs[i].y); |
1073 |
|
} |
1074 |
|
//DEBUG("B-frame Direct!\n"); |
1075 |
|
} |
1076 |
|
mb->mode = MODE_INTER4V; |
1077 |
|
decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], mb, x, y, mb->cbp, bs); |
1078 |
|
break; |
1079 |
|
|
1080 |
|
case MODE_INTERPOLATE: |
1081 |
|
get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward, dec->p_fmv); |
1082 |
|
dec->p_fmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x; |
1083 |
|
dec->p_fmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y; |
1084 |
|
|
1085 |
|
get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0], fcode_backward, dec->p_bmv); |
1086 |
|
dec->p_bmv.x = mb->b_mvs[1].x = mb->b_mvs[2].x = mb->b_mvs[3].x = mb->b_mvs[0].x; |
1087 |
|
dec->p_bmv.y = mb->b_mvs[1].y = mb->b_mvs[2].y = mb->b_mvs[3].y = mb->b_mvs[0].y; |
1088 |
|
|
1089 |
|
decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], mb, x, y, mb->cbp, bs); |
1090 |
|
//DEBUG("B-frame Bidir!\n"); |
1091 |
|
break; |
1092 |
|
|
1093 |
|
case MODE_BACKWARD: |
1094 |
|
get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_backward, dec->p_bmv); |
1095 |
|
dec->p_bmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x; |
1096 |
|
dec->p_bmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y; |
1097 |
|
|
1098 |
|
decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0); |
1099 |
|
//DEBUG("B-frame Backward!\n"); |
1100 |
|
break; |
1101 |
|
|
1102 |
|
case MODE_FORWARD: |
1103 |
|
get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward, dec->p_fmv); |
1104 |
|
dec->p_fmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x; |
1105 |
|
dec->p_fmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y; |
1106 |
|
|
1107 |
|
decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1); |
1108 |
|
//DEBUG("B-frame Forward!\n"); |
1109 |
|
break; |
1110 |
|
|
1111 |
|
default: |
1112 |
|
DEBUG1("Not support B-frame mb_type =",mb->mb_type); |
1113 |
|
} |
1114 |
|
|
1115 |
|
} // end of FOR |
1116 |
|
} |
1117 |
|
} |
1118 |
|
|
1119 |
|
// swap two MACROBLOCK array |
1120 |
|
void mb_swap(MACROBLOCK **mb1, MACROBLOCK **mb2) |
1121 |
|
{ |
1122 |
|
MACROBLOCK *temp=*mb1; |
1123 |
|
*mb1=*mb2; |
1124 |
|
*mb2=temp; |
1125 |
|
} |
1126 |
|
|
1127 |
int decoder_decode(DECODER * dec, XVID_DEC_FRAME * frame) |
int decoder_decode(DECODER * dec, XVID_DEC_FRAME * frame) |
1128 |
{ |
{ |
1129 |
|
|
1130 |
Bitstream bs; |
Bitstream bs; |
1131 |
uint32_t rounding; |
uint32_t rounding; |
1132 |
uint32_t quant; |
uint32_t quant; |
1133 |
uint32_t fcode; |
uint32_t fcode_forward; |
1134 |
|
uint32_t fcode_backward; |
1135 |
uint32_t intra_dc_threshold; |
uint32_t intra_dc_threshold; |
1136 |
|
uint32_t vop_type; |
1137 |
|
|
1138 |
start_global_timer(); |
start_global_timer(); |
1139 |
|
|
1140 |
BitstreamInit(&bs, frame->bitstream, frame->length); |
BitstreamInit(&bs, frame->bitstream, frame->length); |
1141 |
|
|
1142 |
switch (BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode, &intra_dc_threshold)) |
// add by chenm001 <chenm001@163.com> |
1143 |
|
// for support B-frame to reference last 2 frame |
1144 |
|
dec->frames++; |
1145 |
|
vop_type=BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold); |
1146 |
|
|
1147 |
|
dec->p_bmv.x=dec->p_bmv.y=dec->p_fmv.y=dec->p_fmv.y=0; // init pred vector to 0 |
1148 |
|
|
1149 |
|
switch (vop_type) |
1150 |
{ |
{ |
1151 |
case P_VOP : |
case P_VOP : |
1152 |
decoder_pframe(dec, &bs, rounding, quant, fcode, intra_dc_threshold); |
decoder_pframe(dec, &bs, rounding, quant, fcode_forward, intra_dc_threshold); |
1153 |
|
DEBUG1("P_VOP Time=",dec->time); |
1154 |
break; |
break; |
1155 |
|
|
1156 |
case I_VOP : |
case I_VOP : |
|
//DEBUG1("",intra_dc_threshold); |
|
1157 |
decoder_iframe(dec, &bs, quant, intra_dc_threshold); |
decoder_iframe(dec, &bs, quant, intra_dc_threshold); |
1158 |
|
DEBUG1("I_VOP Time=",dec->time); |
1159 |
break; |
break; |
1160 |
|
|
1161 |
case B_VOP : // ignore |
case B_VOP : |
1162 |
|
#ifdef BFRAMES |
1163 |
|
if (dec->time_pp > dec->time_bp){ |
1164 |
|
DEBUG1("B_VOP Time=",dec->time); |
1165 |
|
decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward); |
1166 |
|
} else { |
1167 |
|
DEBUG("broken B-frame!"); |
1168 |
|
} |
1169 |
|
#endif |
1170 |
break; |
break; |
1171 |
|
|
1172 |
case N_VOP : // vop not coded |
case N_VOP : // vop not coded |
1178 |
|
|
1179 |
frame->length = BitstreamPos(&bs) / 8; |
frame->length = BitstreamPos(&bs) / 8; |
1180 |
|
|
1181 |
|
#ifdef BFRAMES |
1182 |
|
// test if no B_VOP |
1183 |
|
if (dec->low_delay){ |
1184 |
|
#endif |
1185 |
|
image_output(&dec->cur, dec->width, dec->height, dec->edged_width, |
1186 |
|
frame->image, frame->stride, frame->colorspace); |
1187 |
|
#ifdef BFRAMES |
1188 |
|
} else { |
1189 |
|
if (dec->frames >= 1){ |
1190 |
start_timer(); |
start_timer(); |
1191 |
|
if ((vop_type == I_VOP || vop_type == P_VOP)) |
1192 |
|
{ |
1193 |
|
image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width, |
1194 |
|
frame->image, frame->stride, frame->colorspace); |
1195 |
|
} else if (vop_type == B_VOP) { |
1196 |
image_output(&dec->cur, dec->width, dec->height, dec->edged_width, |
image_output(&dec->cur, dec->width, dec->height, dec->edged_width, |
1197 |
frame->image, frame->stride, frame->colorspace); |
frame->image, frame->stride, frame->colorspace); |
1198 |
|
} |
1199 |
stop_conv_timer(); |
stop_conv_timer(); |
1200 |
|
} |
1201 |
|
} |
1202 |
|
#endif |
1203 |
|
|
1204 |
|
if (vop_type==I_VOP || vop_type==P_VOP){ |
1205 |
|
image_swap(&dec->refn[0], &dec->refn[1]); |
1206 |
|
image_swap(&dec->cur, &dec->refn[0]); |
1207 |
|
// swap MACROBLOCK |
1208 |
|
if (dec->low_delay && vop_type==P_VOP) |
1209 |
|
mb_swap(&dec->mbs, &dec->last_mbs); |
1210 |
|
} |
1211 |
|
|
1212 |
emms(); |
emms(); |
1213 |
|
|