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 |
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 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 |
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$ |
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" |
72 |
|
|
73 |
#include "image/image.h" |
#include "image/image.h" |
74 |
#include "image/colorspace.h" |
#include "image/colorspace.h" |
75 |
|
#include "utils/mem_align.h" |
76 |
|
|
77 |
int decoder_create(XVID_DEC_PARAM * param) |
int decoder_create(XVID_DEC_PARAM * param) |
78 |
{ |
{ |
79 |
DECODER * dec; |
DECODER * dec; |
80 |
|
|
81 |
dec = malloc(sizeof(DECODER)); |
dec = xvid_malloc(sizeof(DECODER), CACHE_LINE); |
82 |
if (dec == NULL) |
if (dec == NULL) |
83 |
{ |
{ |
84 |
return XVID_ERR_MEMORY; |
return XVID_ERR_MEMORY; |
96 |
|
|
97 |
if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) |
if (image_create(&dec->cur, dec->edged_width, dec->edged_height)) |
98 |
{ |
{ |
99 |
free(dec); |
xvid_free(dec); |
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); |
106 |
|
xvid_free(dec); |
107 |
|
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); |
image_destroy(&dec->cur, dec->edged_width, dec->edged_height); |
121 |
free(dec); |
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; |
return XVID_ERR_MEMORY; |
125 |
} |
} |
126 |
|
|
127 |
dec->mbs = malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height); |
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 |
free(dec); |
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); |
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 |
free(dec->mbs); |
xvid_free(dec->last_mbs); |
165 |
image_destroy(&dec->refn, dec->edged_width, dec->edged_height); |
xvid_free(dec->mbs); |
166 |
|
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 |
free(dec); |
xvid_free(dec); |
|
|
|
|
destroy_vlc_tables(); |
|
171 |
|
|
172 |
write_timer(); |
write_timer(); |
173 |
return XVID_ERR_OK; |
return XVID_ERR_OK; |
181 |
}; |
}; |
182 |
|
|
183 |
|
|
184 |
|
|
185 |
|
|
186 |
// decode an intra macroblock |
// decode an intra macroblock |
187 |
|
|
188 |
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, |
189 |
{ |
MACROBLOCK * pMB, |
190 |
uint32_t k; |
const uint32_t x_pos, |
191 |
|
const uint32_t y_pos, |
192 |
|
const uint32_t acpred_flag, |
193 |
|
const uint32_t cbp, |
194 |
|
Bitstream * bs, |
195 |
|
const uint32_t quant, |
196 |
|
const uint32_t intra_dc_threshold) |
197 |
|
{ |
198 |
|
|
199 |
|
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; |
206 |
|
uint32_t iQuant = pMB->quant; |
207 |
|
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
208 |
|
|
209 |
|
pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); |
210 |
|
pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); |
211 |
|
pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); |
212 |
|
|
213 |
for (k = 0; k < 6; k++) |
memset(block, 0, 6*64*sizeof(int16_t)); // clear |
214 |
|
|
215 |
|
for (i = 0; i < 6; i++) |
216 |
{ |
{ |
217 |
uint32_t dcscalar; |
uint32_t iDcScaler = get_dc_scaler(iQuant, i < 4); |
|
int16_t block[64]; |
|
|
int16_t data[64]; |
|
218 |
int16_t predictors[8]; |
int16_t predictors[8]; |
219 |
int start_coeff; |
int start_coeff; |
220 |
|
|
|
dcscalar = get_dc_scaler(mb->quant, k < 4); |
|
|
|
|
221 |
start_timer(); |
start_timer(); |
222 |
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); |
223 |
if (!acpred_flag) |
if (!acpred_flag) |
224 |
{ |
{ |
225 |
mb->acpred_directions[k] = 0; |
pMB->acpred_directions[i] = 0; |
226 |
} |
} |
227 |
stop_prediction_timer(); |
stop_prediction_timer(); |
228 |
|
|
|
memset(block, 0, 64*sizeof(int16_t)); // clear |
|
|
|
|
229 |
if (quant < intra_dc_threshold) |
if (quant < intra_dc_threshold) |
230 |
{ |
{ |
231 |
int dc_size; |
int dc_size; |
232 |
int dc_dif; |
int dc_dif; |
233 |
|
|
234 |
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); |
235 |
dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0 ; |
dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0 ; |
236 |
|
|
237 |
if (dc_size > 8) |
if (dc_size > 8) |
239 |
BitstreamSkip(bs, 1); // marker |
BitstreamSkip(bs, 1); // marker |
240 |
} |
} |
241 |
|
|
242 |
block[0] = dc_dif; |
block[i*64 + 0] = dc_dif; |
243 |
start_coeff = 1; |
start_coeff = 1; |
244 |
} |
} |
245 |
else |
else |
248 |
} |
} |
249 |
|
|
250 |
start_timer(); |
start_timer(); |
251 |
if (cbp & (1 << (5-k))) // coded |
if (cbp & (1 << (5-i))) // coded |
252 |
{ |
{ |
253 |
get_intra_block(bs, block, mb->acpred_directions[k], 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(mb, k, block, dcscalar, 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, block, mb->quant, dcscalar); |
dequant_intra(&data[i*64], &block[i*64], iQuant, iDcScaler); |
265 |
} |
} |
266 |
else |
else |
267 |
{ |
{ |
268 |
dequant4_intra(data, block, mb->quant, dcscalar); |
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); |
idct(&data[i*64]); |
274 |
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); |
|
|
} |
|
|
else if (k == 4) |
|
|
{ |
|
|
transfer_16to8copy(dec->cur.u+ 8*y*(dec->edged_width/2) + 8*x, data, (dec->edged_width/2)); |
|
275 |
} |
} |
276 |
else // if (k == 5) |
|
277 |
|
if (dec->interlacing && pMB->field_dct) |
278 |
{ |
{ |
279 |
transfer_16to8copy(dec->cur.v + 8*y*(dec->edged_width/2) + 8*x, data, (dec->edged_width/2)); |
next_block = stride; |
280 |
|
stride *= 2; |
281 |
} |
} |
282 |
|
|
283 |
|
start_timer(); |
284 |
|
transfer_16to8copy(pY_Cur, &data[0*64], stride); |
285 |
|
transfer_16to8copy(pY_Cur + 8, &data[1*64], stride); |
286 |
|
transfer_16to8copy(pY_Cur + next_block, &data[2*64], stride); |
287 |
|
transfer_16to8copy(pY_Cur + 8 + next_block, &data[3*64], stride); |
288 |
|
transfer_16to8copy(pU_Cur, &data[4*64], stride2); |
289 |
|
transfer_16to8copy(pV_Cur, &data[5*64], stride2); |
290 |
stop_transfer_timer(); |
stop_transfer_timer(); |
291 |
} |
} |
|
} |
|
292 |
|
|
293 |
|
|
294 |
|
|
302 |
|
|
303 |
// decode an inter macroblock |
// decode an inter macroblock |
304 |
|
|
305 |
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, |
306 |
{ |
const MACROBLOCK * pMB, |
307 |
const uint32_t stride = dec->edged_width; |
const uint32_t x_pos, |
308 |
const uint32_t stride2 = dec->edged_width / 2; |
const uint32_t y_pos, |
309 |
|
const uint32_t acpred_flag, |
310 |
|
const uint32_t cbp, |
311 |
|
Bitstream * bs, |
312 |
|
const uint32_t quant, |
313 |
|
const uint32_t rounding) |
314 |
|
{ |
315 |
|
|
316 |
|
DECLARE_ALIGNED_MATRIX(block,6, 64, int16_t, CACHE_LINE); |
317 |
|
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; |
323 |
|
uint32_t iQuant = pMB->quant; |
324 |
|
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
325 |
int uv_dx, uv_dy; |
int uv_dx, uv_dy; |
|
uint32_t k; |
|
326 |
|
|
327 |
if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) |
pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); |
328 |
|
pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); |
329 |
|
pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); |
330 |
|
|
331 |
|
if (pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q) |
332 |
{ |
{ |
333 |
uv_dx = mb->mvs[0].x; |
uv_dx = pMB->mvs[0].x; |
334 |
uv_dy = mb->mvs[0].y; |
uv_dy = pMB->mvs[0].y; |
335 |
|
|
336 |
uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2; |
uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2; |
337 |
uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2; |
uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2; |
339 |
else |
else |
340 |
{ |
{ |
341 |
int sum; |
int sum; |
342 |
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; |
343 |
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) ); |
344 |
|
|
345 |
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; |
346 |
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) ); |
347 |
} |
} |
348 |
|
|
349 |
start_timer(); |
start_timer(); |
350 |
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); |
351 |
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); |
352 |
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); |
353 |
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); |
354 |
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); |
355 |
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); |
356 |
stop_comp_timer(); |
stop_comp_timer(); |
357 |
|
|
358 |
|
for (i = 0; i < 6; i++) |
|
for (k = 0; k < 6; k++) |
|
359 |
{ |
{ |
360 |
int16_t block[64]; |
if (cbp & (1 << (5-i))) // coded |
|
int16_t data[64]; |
|
|
|
|
|
if (cbp & (1 << (5-k))) // coded |
|
361 |
{ |
{ |
362 |
memset(block, 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); |
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, block, mb->quant); |
dequant_inter(&data[i*64], &block[i*64], iQuant); |
372 |
} |
} |
373 |
else |
else |
374 |
{ |
{ |
375 |
dequant4_inter(data, block, mb->quant); |
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); |
idct(&data[i*64]); |
381 |
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); |
|
382 |
} |
} |
|
else if (k == 4) |
|
|
{ |
|
|
transfer_16to8add(dec->cur.u + 8*y*stride2 + 8*x, data, stride2); |
|
383 |
} |
} |
384 |
else // k == 5 |
|
385 |
|
if (dec->interlacing && pMB->field_dct) |
386 |
{ |
{ |
387 |
transfer_16to8add(dec->cur.v + 8*y*stride2 + 8*x, data, stride2); |
next_block = stride; |
388 |
|
stride *= 2; |
389 |
} |
} |
390 |
|
|
391 |
|
start_timer(); |
392 |
|
if (cbp & 32) |
393 |
|
transfer_16to8add(pY_Cur, &data[0*64], stride); |
394 |
|
if (cbp & 16) |
395 |
|
transfer_16to8add(pY_Cur + 8, &data[1*64], stride); |
396 |
|
if (cbp & 8) |
397 |
|
transfer_16to8add(pY_Cur + next_block, &data[2*64], stride); |
398 |
|
if (cbp & 4) |
399 |
|
transfer_16to8add(pY_Cur + 8 + next_block, &data[3*64], stride); |
400 |
|
if (cbp & 2) |
401 |
|
transfer_16to8add(pU_Cur, &data[4*64], stride2); |
402 |
|
if (cbp & 1) |
403 |
|
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++) |
451 |
} |
} |
452 |
mb->quant = quant; |
mb->quant = quant; |
453 |
|
|
454 |
|
if (dec->interlacing) |
455 |
|
{ |
456 |
|
mb->field_dct = BitstreamGetBit(bs); |
457 |
|
DEBUG1("deci: field_dct: ", mb->field_dct); |
458 |
|
} |
459 |
|
|
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); |
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; |
574 |
} |
} |
575 |
mb->quant = quant; |
mb->quant = quant; |
576 |
|
|
577 |
|
if (dec->interlacing) |
578 |
|
{ |
579 |
|
mb->field_dct = BitstreamGetBit(bs); |
580 |
|
DEBUG1("decp: field_dct: ", mb->field_dct); |
581 |
|
|
582 |
if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) |
if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) |
583 |
{ |
{ |
584 |
|
mb->field_pred = BitstreamGetBit(bs); |
585 |
|
DEBUG1("decp: field_pred: ", mb->field_pred); |
586 |
|
|
587 |
|
if (mb->field_pred) |
588 |
|
{ |
589 |
|
mb->field_for_top = BitstreamGetBit(bs); |
590 |
|
DEBUG1("decp: field_for_top: ", mb->field_for_top); |
591 |
|
mb->field_for_bot = BitstreamGetBit(bs); |
592 |
|
DEBUG1("decp: field_for_bot: ", mb->field_for_bot); |
593 |
|
} |
594 |
|
} |
595 |
|
} |
596 |
|
|
597 |
|
if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q) |
598 |
|
{ |
599 |
|
if (dec->interlacing && mb->field_pred) |
600 |
|
{ |
601 |
|
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode); |
602 |
|
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[1], fcode); |
603 |
|
} |
604 |
|
else |
605 |
|
{ |
606 |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode); |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode); |
607 |
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; |
608 |
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; |
609 |
} |
} |
610 |
|
} |
611 |
else if (mb->mode == MODE_INTER4V /* || mb->mode == MODE_INTER4V_Q */) |
else if (mb->mode == MODE_INTER4V /* || mb->mode == MODE_INTER4V_Q */) |
612 |
{ |
{ |
613 |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode); |
get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode); |
627 |
} |
} |
628 |
else // not coded |
else // not coded |
629 |
{ |
{ |
630 |
|
//DEBUG2("P-frame MB at (X,Y)=",x,y); |
631 |
mb->mode = MODE_NOT_CODED; |
mb->mode = MODE_NOT_CODED; |
632 |
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; |
633 |
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; |
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 |
|
// add by MinChen <chenm001@163.com> |
715 |
|
// decode an B-frame forward & backward inter macroblock |
716 |
|
void decoder_bf_mbinter(DECODER * dec, |
717 |
|
const MACROBLOCK * pMB, |
718 |
|
const uint32_t x_pos, |
719 |
|
const uint32_t y_pos, |
720 |
|
const uint32_t cbp, |
721 |
|
Bitstream * bs, |
722 |
|
const uint32_t quant, |
723 |
|
const uint8_t ref) |
724 |
|
{ |
725 |
|
|
726 |
|
DECLARE_ALIGNED_MATRIX(block,6, 64, int16_t, CACHE_LINE); |
727 |
|
DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); |
728 |
|
|
729 |
|
uint32_t stride = dec->edged_width; |
730 |
|
uint32_t stride2 = stride / 2; |
731 |
|
uint32_t next_block = stride * 8; |
732 |
|
uint32_t i; |
733 |
|
uint32_t iQuant = pMB->quant; |
734 |
|
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
735 |
|
int uv_dx, uv_dy; |
736 |
|
|
737 |
|
pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); |
738 |
|
pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); |
739 |
|
pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); |
740 |
|
|
741 |
|
if (!(pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) |
742 |
|
{ |
743 |
|
uv_dx = pMB->mvs[0].x; |
744 |
|
uv_dy = pMB->mvs[0].y; |
745 |
|
|
746 |
|
uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2; |
747 |
|
uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2; |
748 |
|
} |
749 |
|
else |
750 |
|
{ |
751 |
|
int sum; |
752 |
|
sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
753 |
|
uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) ); |
754 |
|
|
755 |
|
sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
756 |
|
uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) ); |
757 |
|
} |
758 |
|
|
759 |
|
start_timer(); |
760 |
|
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); |
761 |
|
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); |
762 |
|
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); |
763 |
|
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); |
764 |
|
interpolate8x8_switch(dec->cur.u, dec->refn[ref].u, 8*x_pos, 8*y_pos, uv_dx, uv_dy, stride2, 0); |
765 |
|
interpolate8x8_switch(dec->cur.v, dec->refn[ref].v, 8*x_pos, 8*y_pos, uv_dx, uv_dy, stride2, 0); |
766 |
|
stop_comp_timer(); |
767 |
|
|
768 |
|
for (i = 0; i < 6; i++) |
769 |
|
{ |
770 |
|
if (cbp & (1 << (5-i))) // coded |
771 |
|
{ |
772 |
|
memset(&block[i*64], 0, 64 * sizeof(int16_t)); // clear |
773 |
|
|
774 |
|
start_timer(); |
775 |
|
get_inter_block(bs, &block[i*64]); |
776 |
|
stop_coding_timer(); |
777 |
|
|
778 |
|
start_timer(); |
779 |
|
if (dec->quant_type == 0) |
780 |
|
{ |
781 |
|
dequant_inter(&data[i*64], &block[i*64], iQuant); |
782 |
|
} |
783 |
|
else |
784 |
|
{ |
785 |
|
dequant4_inter(&data[i*64], &block[i*64], iQuant); |
786 |
|
} |
787 |
|
stop_iquant_timer(); |
788 |
|
|
789 |
|
data[0]=10; |
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 |
|
const uint32_t cbp, |
827 |
|
Bitstream * bs ) |
828 |
|
{ |
829 |
|
|
830 |
|
DECLARE_ALIGNED_MATRIX(block,6, 64, int16_t, CACHE_LINE); |
831 |
|
DECLARE_ALIGNED_MATRIX(data, 6, 64, int16_t, CACHE_LINE); |
832 |
|
|
833 |
|
uint32_t stride = dec->edged_width; |
834 |
|
uint32_t stride2 = stride / 2; |
835 |
|
uint32_t next_block = stride * 8; |
836 |
|
uint32_t iQuant = pMB->quant; |
837 |
|
int uv_dx, uv_dy; |
838 |
|
int b_uv_dx, b_uv_dy; |
839 |
|
uint32_t i; |
840 |
|
uint8_t *pY_Cur, *pU_Cur, *pV_Cur; |
841 |
|
|
842 |
|
pY_Cur = dec->cur.y + (y_pos << 4) * stride + (x_pos << 4); |
843 |
|
pU_Cur = dec->cur.u + (y_pos << 3) * stride2 + (x_pos << 3); |
844 |
|
pV_Cur = dec->cur.v + (y_pos << 3) * stride2 + (x_pos << 3); |
845 |
|
|
846 |
|
if ((pMB->mode == MODE_INTER || pMB->mode == MODE_INTER_Q)) |
847 |
|
{ |
848 |
|
uv_dx = pMB->mvs[0].x; |
849 |
|
uv_dy = pMB->mvs[0].y; |
850 |
|
|
851 |
|
uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2; |
852 |
|
uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2; |
853 |
|
|
854 |
|
b_uv_dx = pMB->b_mvs[0].x; |
855 |
|
b_uv_dy = pMB->b_mvs[0].y; |
856 |
|
|
857 |
|
b_uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2; |
858 |
|
b_uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2; |
859 |
|
} |
860 |
|
else |
861 |
|
{ |
862 |
|
int sum; |
863 |
|
sum = pMB->mvs[0].x + pMB->mvs[1].x + pMB->mvs[2].x + pMB->mvs[3].x; |
864 |
|
uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) ); |
865 |
|
|
866 |
|
sum = pMB->mvs[0].y + pMB->mvs[1].y + pMB->mvs[2].y + pMB->mvs[3].y; |
867 |
|
uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) ); |
868 |
|
|
869 |
|
sum = pMB->b_mvs[0].x + pMB->b_mvs[1].x + pMB->b_mvs[2].x + pMB->b_mvs[3].x; |
870 |
|
b_uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) ); |
871 |
|
|
872 |
|
sum = pMB->b_mvs[0].y + pMB->b_mvs[1].y + pMB->b_mvs[2].y + pMB->b_mvs[3].y; |
873 |
|
b_uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) ); |
874 |
|
} |
875 |
|
|
876 |
|
|
877 |
|
start_timer(); |
878 |
|
interpolate8x8_switch(dec->cur.y, forward.y, 16*x_pos, 16*y_pos , pMB->mvs[0].x, pMB->mvs[0].y, stride, 0); |
879 |
|
interpolate8x8_switch(dec->cur.y, forward.y, 16*x_pos + 8, 16*y_pos , pMB->mvs[1].x, pMB->mvs[1].y, stride, 0); |
880 |
|
interpolate8x8_switch(dec->cur.y, forward.y, 16*x_pos, 16*y_pos + 8, pMB->mvs[2].x, pMB->mvs[2].y, stride, 0); |
881 |
|
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); |
882 |
|
interpolate8x8_switch(dec->cur.u, forward.u, 8*x_pos, 8*y_pos, uv_dx, uv_dy, stride2, 0); |
883 |
|
interpolate8x8_switch(dec->cur.v, forward.v, 8*x_pos, 8*y_pos, uv_dx, uv_dy, stride2, 0); |
884 |
|
|
885 |
|
|
886 |
|
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); |
887 |
|
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); |
888 |
|
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); |
889 |
|
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); |
890 |
|
interpolate8x8_switch(dec->refn[2].u, backward.u, 8*x_pos, 8*y_pos, b_uv_dx, b_uv_dy, stride2, 0); |
891 |
|
interpolate8x8_switch(dec->refn[2].v, backward.v, 8*x_pos, 8*y_pos, b_uv_dx, b_uv_dy, stride2, 0); |
892 |
|
|
893 |
|
interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16*x_pos, 16*y_pos , stride); |
894 |
|
interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16*x_pos + 8, 16*y_pos , stride); |
895 |
|
interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16*x_pos, 16*y_pos + 8, stride); |
896 |
|
interpolate8x8_c(dec->cur.y, dec->refn[2].y, 16*x_pos + 8, 16*y_pos + 8, stride); |
897 |
|
interpolate8x8_c(dec->cur.u, dec->refn[2].u, 8*x_pos, 8*y_pos, stride2); |
898 |
|
interpolate8x8_c(dec->cur.v, dec->refn[2].v, 8*x_pos, 8*y_pos, stride2); |
899 |
|
|
900 |
|
stop_comp_timer(); |
901 |
|
|
902 |
|
for (i = 0; i < 6; i++) |
903 |
|
{ |
904 |
|
if (cbp & (1 << (5-i))) // coded |
905 |
|
{ |
906 |
|
memset(&block[i*64], 0, 64 * sizeof(int16_t)); // clear |
907 |
|
|
908 |
|
start_timer(); |
909 |
|
get_inter_block(bs, &block[i*64]); |
910 |
|
stop_coding_timer(); |
911 |
|
|
912 |
|
start_timer(); |
913 |
|
if (dec->quant_type == 0) |
914 |
|
{ |
915 |
|
dequant_inter(&data[i*64], &block[i*64], iQuant); |
916 |
|
} |
917 |
|
else |
918 |
|
{ |
919 |
|
dequant4_inter(&data[i*64], &block[i*64], iQuant); |
920 |
|
} |
921 |
|
stop_iquant_timer(); |
922 |
|
|
923 |
|
start_timer(); |
924 |
|
idct(&data[i*64]); |
925 |
|
stop_idct_timer(); |
926 |
|
} |
927 |
|
} |
928 |
|
|
929 |
|
if (dec->interlacing && pMB->field_dct) |
930 |
|
{ |
931 |
|
next_block = stride; |
932 |
|
stride *= 2; |
933 |
|
} |
934 |
|
|
935 |
|
start_timer(); |
936 |
|
if (cbp & 32) |
937 |
|
transfer_16to8add(pY_Cur, &data[0*64], stride); |
938 |
|
if (cbp & 16) |
939 |
|
transfer_16to8add(pY_Cur + 8, &data[1*64], stride); |
940 |
|
if (cbp & 8) |
941 |
|
transfer_16to8add(pY_Cur + next_block, &data[2*64], stride); |
942 |
|
if (cbp & 4) |
943 |
|
transfer_16to8add(pY_Cur + 8 + next_block, &data[3*64], stride); |
944 |
|
if (cbp & 2) |
945 |
|
transfer_16to8add(pU_Cur, &data[4*64], stride2); |
946 |
|
if (cbp & 1) |
947 |
|
transfer_16to8add(pV_Cur, &data[5*64], stride2); |
948 |
|
stop_transfer_timer(); |
949 |
|
} |
950 |
|
|
951 |
|
|
952 |
|
// add by MinChen <chenm001@163.com> |
953 |
|
// for decode B-frame dbquant |
954 |
|
int32_t __inline get_dbquant(Bitstream * bs) |
955 |
|
{ |
956 |
|
if (!BitstreamGetBit(bs)) // '0' |
957 |
|
return(0); |
958 |
|
else if (!BitstreamGetBit(bs)) // '10' |
959 |
|
return(-2); |
960 |
|
else |
961 |
|
return(2); // '11' |
962 |
|
} |
963 |
|
|
964 |
|
// add by MinChen <chenm001@163.com> |
965 |
|
// for decode B-frame mb_type |
966 |
|
// bit ret_value |
967 |
|
// 1 0 |
968 |
|
// 01 1 |
969 |
|
// 001 2 |
970 |
|
// 0001 3 |
971 |
|
int32_t __inline get_mbtype(Bitstream * bs) |
972 |
|
{ |
973 |
|
int32_t mb_type; |
974 |
|
for(mb_type=0;mb_type<=3;mb_type++){ |
975 |
|
if (BitstreamGetBit(bs)) |
976 |
|
break; |
977 |
|
} |
978 |
|
|
979 |
|
if (mb_type<=3) |
980 |
|
return(mb_type); |
981 |
|
else |
982 |
|
return(-1); |
983 |
|
} |
984 |
|
|
985 |
|
void decoder_bframe(DECODER * dec, Bitstream * bs, int quant, int fcode_forward, int fcode_backward) |
986 |
|
{ |
987 |
|
|
988 |
|
uint32_t x, y; |
989 |
|
VECTOR mv, zeromv; |
990 |
|
|
991 |
|
start_timer(); |
992 |
|
image_setedges(&dec->refn[0], dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing); |
993 |
|
//image_setedges(&dec->refn[1], dec->edged_width, dec->edged_height, dec->width, dec->height, dec->interlacing); |
994 |
|
stop_edges_timer(); |
995 |
|
|
996 |
|
|
997 |
|
for (y = 0; y < dec->mb_height; y++) |
998 |
|
{ |
999 |
|
// Initialize Pred Motion Vector |
1000 |
|
dec->p_fmv.x = dec->p_fmv.y = dec->p_bmv.x = dec->p_bmv.y = 0; |
1001 |
|
for (x = 0; x < dec->mb_width; x++) |
1002 |
|
{ |
1003 |
|
MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x]; |
1004 |
|
MACROBLOCK * last_mb = &dec->last_mbs[y*dec->mb_width + x]; |
1005 |
|
|
1006 |
|
mb->mvs[0].x=mb->mvs[0].y=zeromv.x = zeromv.y = mv.x = mv.y = 0; |
1007 |
|
|
1008 |
|
// the last P_VOP is skip macroblock ? |
1009 |
|
if (last_mb->mode == MODE_NOT_CODED){ |
1010 |
|
//DEBUG2("Skip MB in B-frame at (X,Y)=!",x,y); |
1011 |
|
mb->mb_type = MODE_FORWARD; |
1012 |
|
mb->cbp = 0; |
1013 |
|
mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x; |
1014 |
|
mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y; |
1015 |
|
mb->quant = 8; |
1016 |
|
|
1017 |
|
decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1); |
1018 |
|
continue; |
1019 |
|
} |
1020 |
|
|
1021 |
|
//t=BitstreamShowBits(bs,32); |
1022 |
|
|
1023 |
|
if (!BitstreamGetBit(bs)){ // modb=='0' |
1024 |
|
const uint8_t modb2=BitstreamGetBit(bs); |
1025 |
|
|
1026 |
|
mb->mb_type = get_mbtype(bs); |
1027 |
|
|
1028 |
|
if (!modb2){ // modb=='00' |
1029 |
|
mb->cbp = BitstreamGetBits(bs,6); |
1030 |
|
} else { |
1031 |
|
mb->cbp = 0; |
1032 |
|
} |
1033 |
|
if (mb->mb_type && mb->cbp){ |
1034 |
|
quant += get_dbquant(bs); |
1035 |
|
|
1036 |
|
if (quant > 31) |
1037 |
|
{ |
1038 |
|
quant = 31; |
1039 |
|
} |
1040 |
|
else if (mb->quant < 1) |
1041 |
|
{ |
1042 |
|
quant = 1; |
1043 |
|
} |
1044 |
|
} else { |
1045 |
|
quant = 8; |
1046 |
|
} |
1047 |
|
mb->quant = quant; |
1048 |
|
} else { |
1049 |
|
mb->mb_type = MODE_DIRECT_NONE_MV; |
1050 |
|
mb->cbp = 0; |
1051 |
|
} |
1052 |
|
|
1053 |
|
mb->mode = MODE_INTER; |
1054 |
|
//DEBUG1("Switch bm_type=",mb->mb_type); |
1055 |
|
|
1056 |
|
switch(mb->mb_type) |
1057 |
|
{ |
1058 |
|
case MODE_DIRECT: |
1059 |
|
get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], 1, zeromv); |
1060 |
|
|
1061 |
|
case MODE_DIRECT_NONE_MV: |
1062 |
|
{ // Because this file is a C file not C++ so I use '{' to define var |
1063 |
|
const int64_t TRB=dec->time_pp-dec->time_bp, |
1064 |
|
TRD=dec->time_pp; |
1065 |
|
int i; |
1066 |
|
for(i=0;i<4;i++){ |
1067 |
|
mb->mvs[i].x = (int32_t)((TRB * last_mb->mvs[i].x)/TRD+mb->mvs[0].x); |
1068 |
|
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); |
1069 |
|
mb->mvs[i].y = (int32_t)((TRB * last_mb->mvs[i].y)/TRD+mb->mvs[0].y); |
1070 |
|
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); |
1071 |
|
} |
1072 |
|
//DEBUG("B-frame Direct!\n"); |
1073 |
|
} |
1074 |
|
mb->mode = MODE_INTER4V; |
1075 |
|
decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], mb, x, y, mb->cbp, bs); |
1076 |
|
break; |
1077 |
|
|
1078 |
|
case MODE_INTERPOLATE: |
1079 |
|
get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward, dec->p_fmv); |
1080 |
|
dec->p_fmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x; |
1081 |
|
dec->p_fmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y; |
1082 |
|
|
1083 |
|
get_b_motion_vector(dec, bs, x, y, &mb->b_mvs[0], fcode_backward, dec->p_bmv); |
1084 |
|
dec->p_bmv.x = mb->b_mvs[1].x = mb->b_mvs[2].x = mb->b_mvs[3].x = mb->b_mvs[0].x; |
1085 |
|
dec->p_bmv.y = mb->b_mvs[1].y = mb->b_mvs[2].y = mb->b_mvs[3].y = mb->b_mvs[0].y; |
1086 |
|
|
1087 |
|
decoder_bf_interpolate_mbinter(dec, dec->refn[1], dec->refn[0], mb, x, y, mb->cbp, bs); |
1088 |
|
//DEBUG("B-frame Bidir!\n"); |
1089 |
|
break; |
1090 |
|
|
1091 |
|
case MODE_BACKWARD: |
1092 |
|
get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_backward, dec->p_bmv); |
1093 |
|
dec->p_bmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x; |
1094 |
|
dec->p_bmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y; |
1095 |
|
|
1096 |
|
decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 0); |
1097 |
|
//DEBUG("B-frame Backward!\n"); |
1098 |
|
break; |
1099 |
|
|
1100 |
|
case MODE_FORWARD: |
1101 |
|
get_b_motion_vector(dec, bs, x, y, &mb->mvs[0], fcode_forward, dec->p_fmv); |
1102 |
|
dec->p_fmv.x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x; |
1103 |
|
dec->p_fmv.y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y; |
1104 |
|
|
1105 |
|
decoder_bf_mbinter(dec, mb, x, y, mb->cbp, bs, quant, 1); |
1106 |
|
//DEBUG("B-frame Forward!\n"); |
1107 |
|
break; |
1108 |
|
|
1109 |
|
default: |
1110 |
|
DEBUG1("Not support B-frame mb_type =",mb->mb_type); |
1111 |
|
} |
1112 |
|
|
1113 |
|
} // end of FOR |
1114 |
|
} |
1115 |
|
} |
1116 |
|
|
1117 |
|
// swap two MACROBLOCK array |
1118 |
|
void mb_swap(MACROBLOCK **mb1, MACROBLOCK **mb2) |
1119 |
|
{ |
1120 |
|
MACROBLOCK *temp=*mb1; |
1121 |
|
*mb1=*mb2; |
1122 |
|
*mb2=temp; |
1123 |
|
} |
1124 |
|
|
1125 |
int decoder_decode(DECODER * dec, XVID_DEC_FRAME * frame) |
int decoder_decode(DECODER * dec, XVID_DEC_FRAME * frame) |
1126 |
{ |
{ |
1127 |
|
|
1128 |
Bitstream bs; |
Bitstream bs; |
1129 |
uint32_t rounding; |
uint32_t rounding; |
1130 |
uint32_t quant; |
uint32_t quant; |
1131 |
uint32_t fcode; |
uint32_t fcode_forward; |
1132 |
|
uint32_t fcode_backward; |
1133 |
uint32_t intra_dc_threshold; |
uint32_t intra_dc_threshold; |
1134 |
|
uint32_t vop_type; |
1135 |
|
|
1136 |
start_global_timer(); |
start_global_timer(); |
1137 |
|
|
1138 |
BitstreamInit(&bs, frame->bitstream, frame->length); |
BitstreamInit(&bs, frame->bitstream, frame->length); |
1139 |
|
|
1140 |
switch (BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode, &intra_dc_threshold)) |
// add by chenm001 <chenm001@163.com> |
1141 |
|
// for support B-frame to reference last 2 frame |
1142 |
|
dec->frames ++; |
1143 |
|
vop_type=BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode_forward, &fcode_backward, &intra_dc_threshold); |
1144 |
|
|
1145 |
|
dec->p_bmv.x=dec->p_bmv.y=dec->p_fmv.y=dec->p_fmv.y=0; // init pred vector to 0 |
1146 |
|
switch (vop_type) |
1147 |
{ |
{ |
1148 |
case P_VOP : |
case P_VOP : |
1149 |
decoder_pframe(dec, &bs, rounding, quant, fcode, intra_dc_threshold); |
decoder_pframe(dec, &bs, rounding, quant, fcode_forward, intra_dc_threshold); |
1150 |
|
DEBUG1("P_VOP Time=",dec->time); |
1151 |
break; |
break; |
1152 |
|
|
1153 |
case I_VOP : |
case I_VOP : |
|
//DEBUG1("",intra_dc_threshold); |
|
1154 |
decoder_iframe(dec, &bs, quant, intra_dc_threshold); |
decoder_iframe(dec, &bs, quant, intra_dc_threshold); |
1155 |
|
DEBUG1("I_VOP Time=",dec->time); |
1156 |
break; |
break; |
1157 |
|
|
1158 |
case B_VOP : // ignore |
case B_VOP : |
1159 |
|
if (dec->time_pp > dec->time_bp){ |
1160 |
|
DEBUG1("B_VOP Time=",dec->time); |
1161 |
|
decoder_bframe(dec, &bs, quant, fcode_forward, fcode_backward); |
1162 |
|
} else { |
1163 |
|
DEBUG("broken B-frame!"); |
1164 |
|
} |
1165 |
break; |
break; |
1166 |
|
|
1167 |
case N_VOP : // vop not coded |
case N_VOP : // vop not coded |
1173 |
|
|
1174 |
frame->length = BitstreamPos(&bs) / 8; |
frame->length = BitstreamPos(&bs) / 8; |
1175 |
|
|
1176 |
|
if (dec->frames >= 1){ |
1177 |
start_timer(); |
start_timer(); |
1178 |
|
if ((vop_type == I_VOP || vop_type == P_VOP)) |
1179 |
|
{ |
1180 |
|
image_output(&dec->refn[0], dec->width, dec->height, dec->edged_width, |
1181 |
|
frame->image, frame->stride, frame->colorspace); |
1182 |
|
} else if (vop_type == B_VOP) { |
1183 |
image_output(&dec->cur, dec->width, dec->height, dec->edged_width, |
image_output(&dec->cur, dec->width, dec->height, dec->edged_width, |
1184 |
frame->image, frame->stride, frame->colorspace); |
frame->image, frame->stride, frame->colorspace); |
1185 |
|
} |
1186 |
stop_conv_timer(); |
stop_conv_timer(); |
1187 |
|
} |
1188 |
|
if (vop_type==I_VOP || vop_type==P_VOP){ |
1189 |
|
image_swap(&dec->refn[0], &dec->refn[1]); |
1190 |
|
image_swap(&dec->cur, &dec->refn[0]); |
1191 |
|
// swap MACROBLOCK |
1192 |
|
if (vop_type==P_VOP) |
1193 |
|
mb_swap(&dec->mbs, &dec->last_mbs); |
1194 |
|
} |
1195 |
|
|
1196 |
emms(); |
emms(); |
1197 |
|
|