ViewVC Help
View File | Revision Log | Show Annotations | Root Listing
root/cvs/xvidcore/src/decoder.c
Revision: 1.1.1.1 (vendor branch)
Committed: Fri Mar 8 02:44:29 2002 UTC (22 years, 7 months ago) by Isibaar
Content type: text/plain
Branch: core
CVS Tags: start
Changes since 1.1: +0 -0 lines
Log Message:
moved sources

File Contents

# Content
1 /**************************************************************************
2 *
3 * XVID MPEG-4 VIDEO CODEC
4 * decoder main
5 *
6 * This program is an implementation of a part of one or more MPEG-4
7 * Video tools as specified in ISO/IEC 14496-2 standard. Those intending
8 * to use this software module in hardware or software products are
9 * advised that its use may infringe existing patents or copyrights, and
10 * any such use would be at such party's own risk. The original
11 * developer of this software module and his/her company, and subsequent
12 * editors and their companies, will have no liability for use of this
13 * software or modifications or derivatives thereof.
14 *
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
19 *
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
24 *
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 *
29 *************************************************************************/
30
31 /**************************************************************************
32 *
33 * History:
34 *
35 * 26.12.2001 decoder_mbinter: dequant/idct moved within if(coded) block
36 * 22.12.2001 block based interpolation
37 * 01.12.2001 inital version; (c)2001 peter ross <pross@cs.rmit.edu.au>
38 *
39 *************************************************************************/
40
41 #include <stdlib.h>
42 #include <string.h> // memset
43
44 #include "xvid.h"
45 #include "portab.h"
46
47 #include "decoder.h"
48 #include "bitstream/bitstream.h"
49 #include "bitstream/mbcoding.h"
50
51 #include "quant/quant_h263.h"
52 #include "quant/quant_mpeg4.h"
53 #include "dct/idct.h"
54 #include "dct/fdct.h"
55 #include "utils/mem_transfer.h"
56 #include "image/interpolate8x8.h"
57
58 #include "bitstream/mbcoding.h"
59 #include "prediction/mbprediction.h"
60 #include "utils/timer.h"
61 #include "utils/emms.h"
62
63 #include "image/image.h"
64 #include "image/colorspace.h"
65
66 int decoder_create(XVID_DEC_PARAM * param)
67 {
68 DECODER * dec;
69
70 dec = malloc(sizeof(DECODER));
71 if (dec == NULL)
72 {
73 return XVID_ERR_MEMORY;
74 }
75 param->handle = dec;
76
77 dec->width = param->width;
78 dec->height = param->height;
79
80 dec->mb_width = (dec->width + 15) / 16;
81 dec->mb_height = (dec->height + 15) / 16;
82
83 dec->edged_width = 16 * dec->mb_width + 2 * EDGE_SIZE;
84 dec->edged_height = 16 * dec->mb_height + 2 * EDGE_SIZE;
85
86 if (image_create(&dec->cur, dec->edged_width, dec->edged_height))
87 {
88 free(dec);
89 return XVID_ERR_MEMORY;
90 }
91
92 if (image_create(&dec->refn, dec->edged_width, dec->edged_height))
93 {
94 image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
95 free(dec);
96 return XVID_ERR_MEMORY;
97 }
98
99 dec->mbs = malloc(sizeof(MACROBLOCK) * dec->mb_width * dec->mb_height);
100 if (dec->mbs == NULL)
101 {
102 image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
103 free(dec);
104 return XVID_ERR_MEMORY;
105 }
106
107 init_timer();
108 create_vlc_tables();
109
110 return XVID_ERR_OK;
111 }
112
113
114 int decoder_destroy(DECODER * dec)
115 {
116 free(dec->mbs);
117 image_destroy(&dec->refn, dec->edged_width, dec->edged_height);
118 image_destroy(&dec->cur, dec->edged_width, dec->edged_height);
119 free(dec);
120
121 destroy_vlc_tables();
122
123 write_timer();
124 return XVID_ERR_OK;
125 }
126
127
128
129 static const int32_t dquant_table[4] =
130 {
131 -1, -2, 1, 2
132 };
133
134
135 // decode an intra macroblock
136
137 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)
138 {
139 uint32_t k;
140
141 for (k = 0; k < 6; k++)
142 {
143 uint32_t dcscalar;
144 int16_t block[64];
145 int16_t data[64];
146 int16_t predictors[8];
147 int start_coeff;
148
149 dcscalar = get_dc_scaler(mb->quant, k < 4);
150
151 start_timer();
152 predict_acdc(dec->mbs, x, y, dec->mb_width, k, block, mb->quant, dcscalar, predictors);
153 if (!acpred_flag)
154 {
155 mb->acpred_directions[k] = 0;
156 }
157 stop_prediction_timer();
158
159 memset(block, 0, 64*sizeof(int16_t)); // clear
160
161 if (quant < intra_dc_threshold)
162 {
163 int dc_size;
164 int dc_dif;
165
166 dc_size = k < 4 ? get_dc_size_lum(bs) : get_dc_size_chrom(bs);
167 dc_dif = dc_size ? get_dc_dif(bs, dc_size) : 0 ;
168
169 if (dc_size > 8)
170 {
171 BitstreamSkip(bs, 1); // marker
172 }
173
174 block[0] = dc_dif;
175 start_coeff = 1;
176 }
177 else
178 {
179 start_coeff = 0;
180 }
181
182 start_timer();
183 if (cbp & (1 << (5-k))) // coded
184 {
185 get_intra_block(bs, block, mb->acpred_directions[k], start_coeff);
186 }
187 stop_coding_timer();
188
189 start_timer();
190 add_acdc(mb, k, block, dcscalar, predictors);
191 stop_prediction_timer();
192
193 start_timer();
194 if (dec->quant_type == 0)
195 {
196 dequant_intra(data, block, mb->quant, dcscalar);
197 }
198 else
199 {
200 dequant4_intra(data, block, mb->quant, dcscalar);
201 }
202 stop_iquant_timer();
203
204 start_timer();
205 idct(data);
206 stop_idct_timer();
207
208 start_timer();
209 if (k < 4)
210 {
211 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);
212 }
213 else if (k == 4)
214 {
215 transfer_16to8copy(dec->cur.u+ 8*y*(dec->edged_width/2) + 8*x, data, (dec->edged_width/2));
216 }
217 else // if (k == 5)
218 {
219 transfer_16to8copy(dec->cur.v + 8*y*(dec->edged_width/2) + 8*x, data, (dec->edged_width/2));
220 }
221 stop_transfer_timer();
222 }
223 }
224
225
226
227
228
229 #define SIGN(X) (((X)>0)?1:-1)
230 #define ABS(X) (((X)>0)?(X):-(X))
231 static const uint32_t roundtab[16] =
232 { 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2 };
233
234
235 // decode an inter macroblock
236
237 void decoder_mbinter(DECODER * dec, MACROBLOCK * mb, int x, int y, uint32_t acpred_flag, uint32_t cbp, Bitstream * bs, int quant, int rounding)
238 {
239 const uint32_t stride = dec->edged_width;
240 const uint32_t stride2 = dec->edged_width / 2;
241 int uv_dx, uv_dy;
242 uint32_t k;
243
244 if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q)
245 {
246 uv_dx = mb->mvs[0].x;
247 uv_dy = mb->mvs[0].y;
248
249 uv_dx = (uv_dx & 3) ? (uv_dx >> 1) | 1 : uv_dx / 2;
250 uv_dy = (uv_dy & 3) ? (uv_dy >> 1) | 1 : uv_dy / 2;
251 }
252 else
253 {
254 int sum;
255 sum = mb->mvs[0].x + mb->mvs[1].x + mb->mvs[2].x + mb->mvs[3].x;
256 uv_dx = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );
257
258 sum = mb->mvs[0].y + mb->mvs[1].y + mb->mvs[2].y + mb->mvs[3].y;
259 uv_dy = (sum == 0 ? 0 : SIGN(sum) * (roundtab[ABS(sum) % 16] + (ABS(sum) / 16) * 2) );
260 }
261
262 start_timer();
263 interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x, 16*y , mb->mvs[0].x, mb->mvs[0].y, stride, rounding);
264 interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x + 8, 16*y , mb->mvs[1].x, mb->mvs[1].y, stride, rounding);
265 interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x, 16*y + 8, mb->mvs[2].x, mb->mvs[2].y, stride, rounding);
266 interpolate8x8_switch(dec->cur.y, dec->refn.y, 16*x + 8, 16*y + 8, mb->mvs[3].x, mb->mvs[3].y, stride, rounding);
267 interpolate8x8_switch(dec->cur.u, dec->refn.u, 8*x, 8*y, uv_dx, uv_dy, stride2, rounding);
268 interpolate8x8_switch(dec->cur.v, dec->refn.v, 8*x, 8*y, uv_dx, uv_dy, stride2, rounding);
269 stop_comp_timer();
270
271
272 for (k = 0; k < 6; k++)
273 {
274 int16_t block[64];
275 int16_t data[64];
276
277 if (cbp & (1 << (5-k))) // coded
278 {
279 memset(block, 0, 64 * sizeof(int16_t)); // clear
280
281 start_timer();
282 get_inter_block(bs, block);
283 stop_coding_timer();
284
285 start_timer();
286 if (dec->quant_type == 0)
287 {
288 dequant_inter(data, block, mb->quant);
289 }
290 else
291 {
292 dequant4_inter(data, block, mb->quant);
293 }
294 stop_iquant_timer();
295
296 start_timer();
297 idct(data);
298 stop_idct_timer();
299
300 start_timer();
301 if (k < 4)
302 {
303 transfer_16to8add(dec->cur.y + (16*y + 4*(k&2))*stride + 16*x + 8*(k&1), data, stride);
304 }
305 else if (k == 4)
306 {
307 transfer_16to8add(dec->cur.u + 8*y*stride2 + 8*x, data, stride2);
308 }
309 else // k == 5
310 {
311 transfer_16to8add(dec->cur.v + 8*y*stride2 + 8*x, data, stride2);
312 }
313 stop_transfer_timer();
314 }
315 }
316 }
317
318
319
320 void decoder_iframe(DECODER * dec, Bitstream * bs, int quant, int intra_dc_threshold)
321 {
322 uint32_t x, y;
323
324 for (y = 0; y < dec->mb_height; y++)
325 {
326 for (x = 0; x < dec->mb_width; x++)
327 {
328 MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x];
329
330 uint32_t mcbpc;
331 uint32_t cbpc;
332 uint32_t acpred_flag;
333 uint32_t cbpy;
334 uint32_t cbp;
335
336 mcbpc = get_mcbpc_intra(bs);
337 mb->mode = mcbpc & 7;
338 cbpc = (mcbpc >> 4);
339
340 acpred_flag = BitstreamGetBit(bs);
341
342 if (mb->mode == MODE_STUFFING)
343 {
344 DEBUG("-- STUFFING ?");
345 continue;
346 }
347
348 cbpy = get_cbpy(bs, 1);
349 cbp = (cbpy << 2) | cbpc;
350
351 if (mb->mode == MODE_INTRA_Q)
352 {
353 quant += dquant_table[BitstreamGetBits(bs,2)];
354 if (quant > 31)
355 {
356 quant = 31;
357 }
358 else if (quant < 1)
359 {
360 quant = 1;
361 }
362 }
363 mb->quant = quant;
364
365
366 decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, intra_dc_threshold);
367 }
368 }
369 }
370
371
372 void get_motion_vector(DECODER *dec, Bitstream *bs, int x, int y, int k, VECTOR * mv, int fcode)
373 {
374 int scale_fac = 1 << (fcode - 1);
375 int high = (32 * scale_fac) - 1;
376 int low = ((-32) * scale_fac);
377 int range = (64 * scale_fac);
378
379 VECTOR pmv[4];
380 uint32_t psad[4];
381
382 int mv_x, mv_y;
383 int pmv_x, pmv_y;
384
385
386 get_pmvdata(dec->mbs, x, y, dec->mb_width, k, pmv, psad);
387
388 pmv_x = pmv[0].x;
389 pmv_y = pmv[0].y;
390
391 mv_x = get_mv(bs, fcode);
392 mv_y = get_mv(bs, fcode);
393
394 mv_x += pmv_x;
395 mv_y += pmv_y;
396
397 if (mv_x < low)
398 {
399 mv_x += range;
400 }
401 else if (mv_x > high)
402 {
403 mv_x -= range;
404 }
405
406 if (mv_y < low)
407 {
408 mv_y += range;
409 }
410 else if (mv_y > high)
411 {
412 mv_y -= range;
413 }
414
415 mv->x = mv_x;
416 mv->y = mv_y;
417
418 }
419
420
421 void decoder_pframe(DECODER * dec, Bitstream * bs, int rounding, int quant, int fcode, int intra_dc_threshold)
422 {
423 uint32_t x, y;
424
425 image_swap(&dec->cur, &dec->refn);
426
427 start_timer();
428 image_setedges(&dec->refn, dec->edged_width, dec->edged_height, dec->width, dec->height);
429 stop_edges_timer();
430
431 for (y = 0; y < dec->mb_height; y++)
432 {
433 for (x = 0; x < dec->mb_width; x++)
434 {
435 MACROBLOCK * mb = &dec->mbs[y*dec->mb_width + x];
436
437 if (!BitstreamGetBit(bs)) // not_coded
438 {
439 uint32_t mcbpc;
440 uint32_t cbpc;
441 uint32_t acpred_flag;
442 uint32_t cbpy;
443 uint32_t cbp;
444 uint32_t intra;
445
446 mcbpc = get_mcbpc_inter(bs);
447 mb->mode = mcbpc & 7;
448 cbpc = (mcbpc >> 4);
449
450 intra = (mb->mode == MODE_INTRA || mb->mode == MODE_INTRA_Q);
451
452 if (intra)
453 {
454 acpred_flag = BitstreamGetBit(bs);
455 }
456
457 if (mb->mode == MODE_STUFFING)
458 {
459 DEBUG("-- STUFFING ?");
460 continue;
461 }
462
463 cbpy = get_cbpy(bs, intra);
464 cbp = (cbpy << 2) | cbpc;
465
466 if (mb->mode == MODE_INTER_Q || mb->mode == MODE_INTRA_Q)
467 {
468 quant += dquant_table[BitstreamGetBits(bs,2)];
469 if (quant > 31)
470 {
471 quant = 31;
472 }
473 else if (mb->quant < 1)
474 {
475 quant = 1;
476 }
477 }
478 mb->quant = quant;
479
480 if (mb->mode == MODE_INTER || mb->mode == MODE_INTER_Q)
481 {
482
483 get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode);
484 mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = mb->mvs[0].x;
485 mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = mb->mvs[0].y;
486 }
487 else if (mb->mode == MODE_INTER4V /* || mb->mode == MODE_INTER4V_Q */)
488 {
489 get_motion_vector(dec, bs, x, y, 0, &mb->mvs[0], fcode);
490 get_motion_vector(dec, bs, x, y, 1, &mb->mvs[1], fcode);
491 get_motion_vector(dec, bs, x, y, 2, &mb->mvs[2], fcode);
492 get_motion_vector(dec, bs, x, y, 3, &mb->mvs[3], fcode);
493 }
494 else // MODE_INTRA, MODE_INTRA_Q
495 {
496 mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;
497 mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;
498 decoder_mbintra(dec, mb, x, y, acpred_flag, cbp, bs, quant, intra_dc_threshold);
499 continue;
500 }
501
502 decoder_mbinter(dec, mb, x, y, acpred_flag, cbp, bs, quant, rounding);
503 }
504 else // not coded
505 {
506
507 mb->mode = MODE_NOT_CODED;
508 mb->mvs[0].x = mb->mvs[1].x = mb->mvs[2].x = mb->mvs[3].x = 0;
509 mb->mvs[0].y = mb->mvs[1].y = mb->mvs[2].y = mb->mvs[3].y = 0;
510
511 // copy macroblock directly from ref to cur
512
513 start_timer();
514
515 transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x),
516 dec->refn.y + (16*y)*dec->edged_width + (16*x),
517 dec->edged_width);
518
519 transfer8x8_copy(dec->cur.y + (16*y)*dec->edged_width + (16*x+8),
520 dec->refn.y + (16*y)*dec->edged_width + (16*x+8),
521 dec->edged_width);
522
523 transfer8x8_copy(dec->cur.y + (16*y+8)*dec->edged_width + (16*x),
524 dec->refn.y + (16*y+8)*dec->edged_width + (16*x),
525 dec->edged_width);
526
527 transfer8x8_copy(dec->cur.y + (16*y+8)*dec->edged_width + (16*x+8),
528 dec->refn.y + (16*y+8)*dec->edged_width + (16*x+8),
529 dec->edged_width);
530
531 transfer8x8_copy(dec->cur.u + (8*y)*dec->edged_width/2 + (8*x),
532 dec->refn.u + (8*y)*dec->edged_width/2 + (8*x),
533 dec->edged_width/2);
534
535 transfer8x8_copy(dec->cur.v + (8*y)*dec->edged_width/2 + (8*x),
536 dec->refn.v + (8*y)*dec->edged_width/2 + (8*x),
537 dec->edged_width/2);
538
539 stop_transfer_timer();
540 }
541 }
542 }
543 }
544
545 int decoder_decode(DECODER * dec, XVID_DEC_FRAME * frame)
546 {
547 Bitstream bs;
548 uint32_t rounding;
549 uint32_t quant;
550 uint32_t fcode;
551 uint32_t intra_dc_threshold;
552
553 start_global_timer();
554
555 BitstreamInit(&bs, frame->bitstream, frame->length);
556
557 switch (BitstreamReadHeaders(&bs, dec, &rounding, &quant, &fcode, &intra_dc_threshold))
558 {
559 case P_VOP :
560 decoder_pframe(dec, &bs, rounding, quant, fcode, intra_dc_threshold);
561 break;
562
563 case I_VOP :
564 //DEBUG1("",intra_dc_threshold);
565 decoder_iframe(dec, &bs, quant, intra_dc_threshold);
566 break;
567
568 case B_VOP : // ignore
569 break;
570
571 case N_VOP : // vop not coded
572 break;
573
574 default :
575 return XVID_ERR_FAIL;
576 }
577
578 frame->length = BitstreamPos(&bs) / 8;
579
580 start_timer();
581 image_output(&dec->cur, dec->width, dec->height, dec->edged_width,
582 frame->image, frame->stride, frame->colorspace);
583 stop_conv_timer();
584
585 emms();
586
587 stop_global_timer();
588
589 return XVID_ERR_OK;
590 }