[svn] / trunk / xvidcore / src / decoder.c Repository:
ViewVC logotype

Annotation of /trunk/xvidcore/src/decoder.c

Parent Directory Parent Directory | Revision Log Revision Log


Revision 41 - (view) (download)

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

No admin address has been configured
ViewVC Help
Powered by ViewVC 1.0.4