1 /************************************************************************
2 *
3 * getpic.c, picture decoding for tmndecode (H.263 decoder)
4 * Copyright (C) 1995, 1996 Telenor R&D, Norway
5 *
6 * Contacts:
7 * Robert Danielsen <Robert.Danielsen@nta.no>
8 *
9 * Telenor Research and Development http://www.nta.no/brukere/DVC/
10 * P.O.Box 83 tel.: +47 63 84 84 00
11 * N-2007 Kjeller, Norway fax.: +47 63 81 00 76
12 *
13 * Copyright (C) 1997 University of BC, Canada
14 * Modified by: Mike Gallant <mikeg@ee.ubc.ca>
15 * Guy Cote <guyc@ee.ubc.ca>
16 * Berna Erol <bernae@ee.ubc.ca>
17 *
18 * Contacts:
19 * Micheal Gallant <mikeg@ee.ubc.ca>
20 *
21 * UBC Image Processing Laboratory http://www.ee.ubc.ca/image
22 * 2356 Main Mall tel.: +1 604 822 4051
23 * Vancouver BC Canada V6T1Z4 fax.: +1 604 822 5949
24 *
25 ************************************************************************/
26
27 /* Disclaimer of Warranty
28 *
29 * These software programs are available to the user without any license fee
30 * or royalty on an "as is" basis. The University of British Columbia
31 * disclaims any and all warranties, whether express, implied, or
32 * statuary, including any implied warranties or merchantability or of
33 * fitness for a particular purpose. In no event shall the
34 * copyright-holder be liable for any incidental, punitive, or
35 * consequential damages of any kind whatsoever arising from the use of
36 * these programs.
37 *
38 * This disclaimer of warranty extends to the user of these programs and
39 * user's customers, employees, agents, transferees, successors, and
40 * assigns.
41 *
42 * The University of British Columbia does not represent or warrant that the
43 * programs furnished hereunder are free of infringement of any
44 * third-party patents.
45 *
46 * Commercial implementations of H.263, including shareware, are subject to
47 * royalty fees to patent holders. Many of these patents are general
48 * enough such that they are unavoidable regardless of implementation
49 * design.
50 *
51 */
52
53
54 /* modified to support annex O true B frames, mikeg@ee.ubc.ca
55 *
56 * modified by Wayne Ellis BT Labs to run Annex E Arithmetic Decoding
57 * <ellis_w_wayne@bt-web.bt.co.uk>
58 *
59 * based on mpeg2decode, (C) 1994, MPEG Software Simulation Group and
60 * mpeg2play, (C) 1994 Stefan Eckart <stefan@lis.e-technik.tu-muenchen.de> */
61
62
63 #include <stdio.h>
64 #include <stdlib.h>
65 #include <string.h>
66
67 #include "config.h"
68 #include "tmndec.h"
69 #include "global.h"
70
71 #include "indices.h"
72 #include "sactbls.h"
73
74 static int coded_map[MBR + 1][MBC + 1];
75 static int quant_map[MBR + 1][MBC + 1];
76 static int STRENGTH[] = {1, 1, 2, 2, 3, 3, 4, 4, 4, 5, 5, 6, 6, 7, 7, 7, 8, 8, 8, 9, 9, 9, 10, 10, 10, 11, 11, 11, 12, 12, 12};
77 static int STRENGTH1[] = {1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4, 4, 4, 4};
78 static int STRENGTH2[] = {1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3, 3};
79
80
81 /* private prototypes */
82 static void get_I_P_MBs _ANSI_ARGS_ ((int framenum));
83 static void get_B_MBs _ANSI_ARGS_ ((int framenum));
84 static void get_EI_EP_MBs _ANSI_ARGS_ ((int framenum));
85 static void clearblock _ANSI_ARGS_ ((int comp));
86 static int motion_decode _ANSI_ARGS_ ((int vec, int pmv));
87 static int find_pmv _ANSI_ARGS_ ((int x, int y, int block, int comp));
88 static void addblock _ANSI_ARGS_ ((int comp, int bx, int by, int addflag));
89 static void reconblock_b _ANSI_ARGS_ ((int comp, int bx, int by, int mode, int bdx, int bdy));
90 static void find_bidir_limits _ANSI_ARGS_ ((int vec, int *start, int *stop, int nhv));
91 static void find_bidir_chroma_limits _ANSI_ARGS_ ((int vec, int *start, int *stop));
92 static void make_edge_image _ANSI_ARGS_ ((unsigned char *src, unsigned char *dst, int width, int height, int edge));
93 static void init_enhancement_layer _ANSI_ARGS_((int layer));
94 void edge_filter _ANSI_ARGS_ ((unsigned char *lum, unsigned char *Cb, unsigned char *Cr, int width, int height));
95 void horiz_edge_filter _ANSI_ARGS_ ((unsigned char *rec, int width, int height, int chr));
96 void vert_edge_filter _ANSI_ARGS_ ((unsigned char *rec, int width, int height, int chr));
97 void vert_post_filter (unsigned char *rec, int width, int height, int chr);
98 void horiz_post_filter (unsigned char *rec, int width, int height, int chr);
99 void PostFilter (unsigned char *lum, unsigned char *Cb, unsigned char *Cr, int width, int height);
100
101 /* reference picture selection */
102 int get_reference_picture(void);
103 void store_picture(int);
104
105 /**********************************************************************
106 *
107 * Name: getpicture
108 * Description: decode and display one picture
109 * Input: frame number
110 * Returns:
111 * Side effects:
112 *
113 * Date: 971102 Author: mikeg@ee.ubc.ca
114 *
115 *
116 ***********************************************************************/
117 void getpicture (int *framenum)
118 {
119 unsigned char *tmp;
120 int i, store_pb, quality=0;
121
122 switch (pict_type)
123 {
124
125 case PCT_INTRA:
126 case PCT_INTER:
127 case PCT_PB:
128 case PCT_IPB:
129
130 enhance_pict = NO;
131
132 if (reference_picture_selection_mode)
133 if (-1 == (quality = get_reference_picture()))
134 break;
135 if (quality > 256) {
136 fprintf(stderr,"completely out of sync -- waiting for I-frame\n");
137 stop_decoder = 1;
138 break;
139 }
140
141 for (i = 0; i < 3; i++)
142 {
143 tmp = prev_frame[i];
144 prev_I_P_frame[i] = prev_frame[i] = next_I_P_frame[i];
145 next_I_P_frame[i] = current_frame[i] = tmp;
146 }
147
148 if ((mv_outside_frame) && (*framenum > 0))
149 {
150 make_edge_image (prev_I_P_frame[0], edgeframe[0], coded_picture_width,
151 coded_picture_height, 32);
152 make_edge_image (prev_I_P_frame[1], edgeframe[1], chrom_width, chrom_height, 16);
153 make_edge_image (prev_I_P_frame[2], edgeframe[2], chrom_width, chrom_height, 16);
154 }
155
156 get_I_P_MBs (*framenum);
157
158 store_pb = pb_frame;
159 pb_frame = 0;
160
161 if (deblocking_filter_mode)
162 edge_filter (current_frame[0], current_frame[1], current_frame[2],
163 coded_picture_width, coded_picture_height);
164
165 pb_frame = store_pb;
166
167 if (pb_frame)
168 {
169 if (deblocking_filter_mode)
170 edge_filter(bframe[0],bframe[1],bframe[2],
171 coded_picture_width,coded_picture_height);
172 }
173
174 PictureDisplay(framenum);
175
176 if (reference_picture_selection_mode)
177 store_picture((pict_type == PCT_INTRA) ? 0 : quality);
178
179 break;
180
181 case PCT_B:
182
183 if (enhancement_layer_num > 1)
184 enhance_pict = YES;
185 else
186 enhance_pict = NO;
187
188 for (i = 0; i < 3; i++)
189 {
190 current_frame[i] = bframe[i];
191 }
192
193 /* Forward prediction */
194 make_edge_image (prev_I_P_frame[0], edgeframe[0], coded_picture_width,
195 coded_picture_height, 32);
196 make_edge_image (prev_I_P_frame[1], edgeframe[1], chrom_width, chrom_height, 16);
197 make_edge_image (prev_I_P_frame[2], edgeframe[2], chrom_width, chrom_height, 16);
198
199 /* Backward prediction */
200 make_edge_image (next_I_P_frame[0], nextedgeframe[0], coded_picture_width,
201 coded_picture_height, 32);
202 make_edge_image (next_I_P_frame[1], nextedgeframe[1], chrom_width, chrom_height, 16);
203 make_edge_image (next_I_P_frame[2], nextedgeframe[2], chrom_width, chrom_height, 16);
204
205 get_B_MBs (*framenum);
206
207 PictureDisplay(framenum);
208
209 break;
210
211 case PCT_EI:
212 case PCT_EP:
213
214 enhance_pict = YES;
215
216 if (!enhancement_layer_init[enhancement_layer_num-2])
217 {
218 init_enhancement_layer(enhancement_layer_num-2);
219 enhancement_layer_init[enhancement_layer_num-2] = ON;
220 }
221
222 for (i = 0; i < 3; i++)
223 {
224 tmp = prev_enhancement_frame[enhancement_layer_num-2][i];
225 prev_enhancement_frame[enhancement_layer_num-2][i] =
226 current_enhancement_frame[enhancement_layer_num-2][i];
227 current_enhancement_frame[enhancement_layer_num-2][i] = tmp;
228 }
229
230 if (scalability_mode >= 3)
231 {
232 UpsampleReferenceLayerPicture();
233 curr_reference_frame[0] = upsampled_reference_frame[0];
234 curr_reference_frame[1] = upsampled_reference_frame[1];
235 curr_reference_frame[2] = upsampled_reference_frame[2];
236 }
237 else
238 {
239 curr_reference_frame[0] = current_frame[0];
240 curr_reference_frame[1] = current_frame[1];
241 curr_reference_frame[2] = current_frame[2];
242 }
243
244 make_edge_image (prev_enhancement_frame[enhancement_layer_num-2][0],
245 enhance_edgeframe[enhancement_layer_num-2][0],
246 coded_picture_width, coded_picture_height, 32);
247 make_edge_image (prev_enhancement_frame[enhancement_layer_num-2][1],
248 enhance_edgeframe[enhancement_layer_num-2][1],
249 chrom_width, chrom_height, 16);
250 make_edge_image (prev_enhancement_frame[enhancement_layer_num-2][2],
251 enhance_edgeframe[enhancement_layer_num-2][2],
252 chrom_width, chrom_height, 16);
253
254 get_EI_EP_MBs (*framenum);
255
256 if (deblocking_filter_mode)
257 edge_filter (current_enhancement_frame[enhancement_layer_num-2][0],
258 current_enhancement_frame[enhancement_layer_num-2][1],
259 current_enhancement_frame[enhancement_layer_num-2][2],
260 coded_picture_width, coded_picture_height);
261
262 store_one (enhance_recon_file_name[enhancement_layer_num-2],
263 current_enhancement_frame[enhancement_layer_num-2],
264 0, coded_picture_width, vertical_size);
265
266 break;
267
268 default:
269
270 break;
271
272 }
273 }
274
275 /* decode all macroblocks of the current picture */
276 static int change_of_quant_tab_10[32] = {0, 2, -1, -1, -1, -1, -1, -1, -1, -1, -1, -2, -2, -2, -2, -2, -2, -2, -2, -2, -2, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3, -3};
277 static int change_of_quant_tab_11[32] = {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 3, 3, 3, 2, 1, -5};
278
279 static void get_I_P_MBs (int framenum)
280 {
281 int comp;
282 int MBA, MBAmax;
283 int bx, by;
284
285 int COD = 0, MCBPC, CBPY, CBP = 0, CBPB = 0, MODB = 0, Mode = 0, DQUANT;
286 int COD_index, CBPY_index, MODB_index, DQUANT_index, MCBPC_index;
287 int INTRADC_index, YCBPB_index, UVCBPB_index, mvdbx_index, mvdby_index;
288 int mvx = 0, mvy = 0, mvy_index, mvx_index, pmv0, pmv1, xpos, ypos, gob, i, k;
289 int mvdbx = 0, mvdby = 0, pmvdbx, pmvdby, gfid, YCBPB, UVCBPB, gobheader_read;
290 int startmv, stopmv, offset, bsize, last_done = 0, pCBP = 0, pCBPB = 0, pCOD = 0, pMODB = 0;
291 int x_avg, y_avg;
292 int DQ_tab[4] = {-1, -2, 1, 2};
293 short *bp;
294 int long_vectors_bak;
295 int tmp = 0;
296
297 /* variables used in advanced intra coding mode */
298 int INTRA_AC_DC = 0, INTRA_AC_DC_index;
299 short *store_qcoeff;
300
301 /* number of macroblocks per picture */
302 MBAmax = mb_width * mb_height;
303
304 MBA = 0; /* macroblock address */
305 newgob = 0;
306
307 /* mark MV's above the picture */
308 for (i = 1; i < mb_width + 1; i++)
309 {
310 for (k = 0; k < 5; k++)
311 {
312 MV[0][k][0][i] = NO_VEC;
313 MV[1][k][0][i] = NO_VEC;
314 }
315 modemap[0][i] = MODE_INTRA;
316 }
317 /* zero MV's on the sides of the picture */
318 for (i = 0; i < mb_height + 1; i++)
319 {
320 for (k = 0; k < 5; k++)
321 {
322 MV[0][k][i][0] = 0;
323 MV[1][k][i][0] = 0;
324 MV[0][k][i][mb_width + 1] = 0;
325 MV[1][k][i][mb_width + 1] = 0;
326 }
327 modemap[i][0] = MODE_INTRA;
328 modemap[i][mb_width + 1] = MODE_INTRA;
329 }
330 /* initialize the qcoeff used in advanced intra coding */
331 if (advanced_intra_coding)
332 {
333 /* store the qcoeff for the frame */
334 if ((store_qcoeff = (short *) calloc (64 * MBAmax * blk_cnt, sizeof (short))) == 0)
335 {
336 fprintf (stderr, "getMB(): Couldn't allocate store_qcoeff.\n");
337 exit (-1);
338 }
339 }
340 fault = 0;
341 gobheader_read = 0;
342
343 for (;;)
344 {
345 if (trace)
346 fprintf (trace_file, "frame %d, MB %d\n", framenum, MBA);
347
348 resync:
349 /* This version of the decoder does not resync on every possible
350 * error, and it does not do all possible error checks. It is not
351 * difficult to make it much more error robust, but I do not think it
352 * is necessary to include this in the freely available version. */
353
354 if (fault)
355 {
356 printf ("Warning: A Fault Condition Has Occurred - Resyncing \n");
357 startcode (); /* sync on new startcode */
358 fault = 0;
359 }
360 if (!(showbits (22) >> 6))
361 {
362 /* startcode */
363 startcode ();
364
365 /* in case of byte aligned start code, ie. PSTUF, GSTUF or ESTUF is
366 * used */
367 if (showbits (22) == (32 | SE_CODE))
368 {
369 /* end of sequence */
370 if (!(syntax_arith_coding && MBA < MBAmax))
371 {
372 return;
373 }
374 }
375 else if ((showbits (22) == PSC << 5))
376 {
377 /* new picture */
378 if (!(syntax_arith_coding && MBA < MBAmax))
379 {
380 return;
381 }
382 }
383 else
384 {
385 if (!(syntax_arith_coding && MBA % mb_width))
386 {
387 if (syntax_arith_coding)
388 {
389 /* SAC hack to finish GOBs which end with MBs coded with no
390 * bits. */
391 gob = (showbits (22) & 31);
392 if (gob * mb_width != MBA)
393 goto finish_gob;
394 }
395 gob = getheader () - 1;
396 if (gob > mb_height)
397 {
398 if (!quiet)
399 printf ("GN out of range\n");
400 return;
401 }
402 /* GFID is not allowed to change unless PTYPE in picture header
403 * changes */
404 gfid = getbits (2);
405 if (trace)
406 {
407 fprintf (trace_file, "gfid: ");
408 printbits (gfid, 2, 2);
409 }
410 /* NB: in error-prone environments the decoder can use this
411 * value to determine whether a picture header where the PTYPE
412 * has changed, has been lost */
413
414 quant = getbits (5);
415 if (trace)
416 {
417 fprintf (trace_file, "quant: ");
418 printbits (quant, 5, 5);
419 }
420 xpos = 0;
421 ypos = gob;
422 MBA = ypos * mb_width;
423
424 newgob = 1;
425 gobheader_read = 1;
426 if (syntax_arith_coding)
427 decoder_reset (); /* init. arithmetic decoder buffer after
428 * gob */
429 }
430 }
431 }
432 finish_gob:
433
434 /* SAC specific label */
435 if (!gobheader_read)
436 {
437 xpos = MBA % mb_width;
438 ypos = MBA / mb_width;
439 if (xpos == 0 && ypos > 0)
440 newgob = 0;
441 } else
442 gobheader_read = 0;
443
444 if (MBA >= MBAmax)
445 {
446 /* all macroblocks decoded */
447 return;
448 }
449 read_cod:
450
451 if (syntax_arith_coding)
452 {
453 if (pict_type == PCT_INTER || pict_type == PCT_IPB)
454 {
455 COD_index = decode_a_symbol (cumf_COD);
456 COD = codtab[COD_index];
457 if (trace)
458 {
459 fprintf (trace_file, "Arithmetic Decoding Debug \n");
460 fprintf (trace_file, "COD Index: %d COD: %d \n", COD_index, COD);
461 }
462 }
463 else
464 {
465 COD = 0; /* COD not used in I-pictures, set to zero */
466 coded_map[ypos + 1][xpos + 1] = 1;
467 }
468 }
469 else
470 {
471 if (PCT_INTER == pict_type || PCT_IPB == pict_type)
472 {
473 COD = showbits (1);
474 if (trace)
475 {
476 fprintf (trace_file, "COD : %d \n", COD);
477 }
478 }
479 else
480 {
481 COD = 0; /* Intra picture -> not skipped */
482 coded_map[ypos + 1][xpos + 1] = 1;
483 }
484 }
485
486 if (!COD)
487 {
488 /* COD == 0 --> not skipped */
489 if (syntax_arith_coding)
490 {
491 if (pict_type == PCT_INTER || pict_type == PCT_IPB)
492 {
493 if (plus_type)
494 {
495 MCBPC_index = decode_a_symbol (cumf_MCBPC_4MVQ);
496 MCBPC = mcbpctab_4mvq[MCBPC_index];
497 }
498 else
499 {
500 MCBPC_index = decode_a_symbol (cumf_MCBPC_no4MVQ);
501 MCBPC = mcbpctab[MCBPC_index];
502 }
503 }
504 else
505 {
506 MCBPC_index = decode_a_symbol (cumf_MCBPC_intra);
507 MCBPC = mcbpc_intratab[MCBPC_index];
508 }
509 if (trace)
510 fprintf (trace_file, "MCBPC Index: %d MCBPC: %d \n", MCBPC_index, MCBPC);
511 }
512 else
513 {
514 if (PCT_INTER == pict_type || PCT_IPB == pict_type)
515 {
516 /* flush COD bit */
517 flushbits (1);
518 }
519 if (PCT_INTRA == pict_type)
520 {
521 MCBPC = getMCBPCintra ();
522 }
523 else
524 {
525 MCBPC = getMCBPC ();
526 }
527 }
528
529 if (fault)
530 goto resync;
531
532 if (MCBPC == 255)
533 {
534 /* stuffing - read next COD without advancing MB count. */
535 goto read_cod;
536 }
537 else
538 {
539 /* normal MB data */
540 Mode = MCBPC & 7;
541 if (advanced_intra_coding && (Mode == MODE_INTRA || Mode == MODE_INTRA_Q))
542 {
543 /* get INTRA_AC_DC mode for annex I */
544 if (syntax_arith_coding)
545 {
546 INTRA_AC_DC_index = decode_a_symbol (cumf_INTRA_AC_DC);
547 INTRA_AC_DC = intra_ac_dctab[INTRA_AC_DC_index];
548 }
549 else
550 {
551 /* using VLC */
552 if (!showbits (1))
553 INTRA_AC_DC = getbits (1);
554 else
555 INTRA_AC_DC = getbits (2);
556 }
557 if (trace)
558 fprintf (trace_file, "INTRA_AC_DC: %d\n", INTRA_AC_DC);
559 }
560
561 /* MODB and CBPB */
562 if (pb_frame)
563 {
564 CBPB = 0;
565 if (syntax_arith_coding)
566 {
567 if (pb_frame== PB_FRAMES)
568 {
569 MODB_index = decode_a_symbol (cumf_MODB_G);
570 MODB = modb_tab_G[MODB_index];
571 }
572 else
573 {
574 MODB_index = decode_a_symbol (cumf_MODB_M);
575 MODB = modb_tab_M[MODB_index];
576 }
577 }
578 else
579 MODB = getMODB ();
580
581 if ( (MODB == PBMODE_CBPB_MVDB && pb_frame == PB_FRAMES) ||
582 (pb_frame == IM_PB_FRAMES &&
583 (MODB == PBMODE_CBPB_FRW_PRED ||
584 MODB == PBMODE_CBPB_BIDIR_PRED || MODB == PBMODE_CBPB_BCKW_PRED) ) )
585 {
586 if (syntax_arith_coding)
587 {
588 for (i = 0; i < 4; i++)
589 {
590 YCBPB_index = decode_a_symbol (cumf_YCBPB);
591 YCBPB = ycbpb_tab[YCBPB_index];
592 CBPB |= (YCBPB << (6 - 1 - i));
593 }
594 for (i = 4; i < 6; i++)
595 {
596 UVCBPB_index = decode_a_symbol (cumf_UVCBPB);
597 UVCBPB = uvcbpb_tab[UVCBPB_index];
598 CBPB |= (UVCBPB << (6 - 1 - i));
599 }
600 }
601 else
602 CBPB = getbits (6);
603
604 if (trace)
605 fprintf (trace_file, "CBPB = %d\n", CBPB);
606 }
607 }
608
609 if (syntax_arith_coding)
610 {
611 if ((Mode == MODE_INTRA || Mode == MODE_INTRA_Q))
612 {
613 /* Intra */
614 CBPY_index = decode_a_symbol (cumf_CBPY_intra);
615 CBPY = cbpy_intratab[CBPY_index];
616 }
617 else
618 {
619 CBPY_index = decode_a_symbol (cumf_CBPY);
620 CBPY = cbpytab[CBPY_index];
621 }
622
623 if (trace)
624 fprintf (trace_file, "CBPY Index: %d CBPY %d \n", CBPY_index, CBPY);
625 }
626 else
627 {
628 CBPY = getCBPY ();
629 }
630 }
631
632 /* Decode Mode and CBP */
633 if ((Mode == MODE_INTRA || Mode == MODE_INTRA_Q))
634 {
635 /* Intra */
636 coded_map[ypos + 1][xpos + 1] = 1;
637
638 if (!syntax_arith_coding)
639 CBPY = CBPY ^ 15; /* needed in huffman coding only */
640 }
641 else if (!syntax_arith_coding && alternative_inter_VLC_mode && ((MCBPC >> 4) == 3))
642 CBPY = CBPY ^ 15; /* Annex S.3 change */
643
644 CBP = (CBPY << 2) | (MCBPC >> 4);
645
646 if ((Mode == MODE_INTER4V || Mode == MODE_INTER4V_Q) && !use_4mv)
647 {
648 if (!quiet)
649 {
650 /* Could set fault-flag and resync */
651 printf ("8x8 vectors not allowed in normal prediction mode\n");
652 }
653 if (trace)
654 fprintf (trace_file, "8x8 vectors not allowed in normal prediction mode\n");
655 }
656
657 if (Mode == MODE_INTER_Q || Mode == MODE_INTRA_Q || Mode == MODE_INTER4V_Q)
658 {
659 /* Read DQUANT if necessary */
660 if (syntax_arith_coding)
661 {
662 DQUANT_index = decode_a_symbol (cumf_DQUANT);
663 DQUANT = dquanttab[DQUANT_index] - 2;
664 quant += DQUANT;
665 if (trace)
666 fprintf (trace_file, "DQUANT Index: %d DQUANT %d \n", DQUANT_index, DQUANT);
667 }
668 else
669 {
670 if (!modified_quantization_mode)
671 {
672 DQUANT = getbits (2);
673 quant += DQ_tab[DQUANT];
674 if (trace)
675 {
676 fprintf (trace_file, "DQUANT (");
677 printbits (DQUANT, 2, 2);
678 fprintf (trace_file, "): %d = %d\n", DQUANT, DQ_tab[DQUANT]);
679 }
680 }
681 else
682 {
683 tmp = getbits (1);
684 if (tmp)
685 {
686 /* only one more additional bit was sent */
687 tmp = getbits (1);
688 if (tmp)
689 {
690 /* second bit of quant is 1 */
691 DQUANT = change_of_quant_tab_11[quant];
692 }
693 else
694 {
695 /* second bit of quant is 0 */
696 DQUANT = change_of_quant_tab_10[quant];
697 }
698 quant += DQUANT;
699 if (trace)
700 {
701 fprintf (trace_file, "DQUANT (1");
702 printbits (tmp, 1, 1);
703 fprintf (trace_file, "): %d \n", DQUANT);
704 }
705 }
706 else
707 {
708 /* five additional bits were sent as
709 * DQUANT */
710 DQUANT = getbits (5);
711 quant = DQUANT;
712 if (trace)
713 {
714 fprintf (trace_file, "DQUANT (");
715 printbits (DQUANT, 5, 5);
716 fprintf (trace_file, "): %d \n", DQUANT);
717 }
718 }
719 }
720 }
721
722 if (quant > 31 || quant < 1)
723 {
724 if (!quiet)
725 printf ("Quantizer out of range: clipping\n");
726 quant = mmax (1, mmin (31, quant));
727 /* could set fault-flag and resync here */
728 }
729 }
730
731 /* motion vectors */
732 if (Mode == MODE_INTER || Mode == MODE_INTER_Q ||
733 Mode == MODE_INTER4V || Mode == MODE_INTER4V_Q || pb_frame)
734 {
735 if (Mode == MODE_INTER4V || Mode == MODE_INTER4V_Q)
736 {
737 startmv = 1;
738 stopmv = 4;
739 }
740 else
741 {
742 startmv = 0;
743 stopmv = 0;
744 }
745
746 for (k = startmv; k <= stopmv; k++)
747 {
748 if (syntax_arith_coding)
749 {
750 mvx_index = decode_a_symbol (cumf_MVD);
751 mvx = mvdtab[mvx_index];
752 mvy_index = decode_a_symbol (cumf_MVD);
753 mvy = mvdtab[mvy_index];
754 if (trace)
755 fprintf (trace_file, "mvx_index: %d mvy_index: %d \n", mvy_index, mvx_index);
756 }
757 else
758 {
759 if (plus_type && long_vectors)
760 {
761 mvx = getRVLC ();
762 mvy = getRVLC ();
763
764 /* flush start code emulation bit */
765 if (mvx == 1 && mvy == 1)
766 flushbits(1);
767 }
768 else
769 {
770 mvx = getTMNMV ();
771 mvy = getTMNMV ();
772 }
773 }
774
775 pmv0 = find_pmv (xpos, ypos, k, 0);
776 pmv1 = find_pmv (xpos, ypos, k, 1);
777
778 if (plus_type && long_vectors && !syntax_arith_coding)
779 {
780 mvx += pmv0;
781 mvy += pmv1;
782 }
783 else
784 {
785 mvx = motion_decode (mvx, pmv0);
786 mvy = motion_decode (mvy, pmv1);
787 }
788
789 if (trace)
790 {
791 fprintf (trace_file, "mvx: %d\n", mvx);
792 fprintf (trace_file, "mvy: %d\n", mvy);
793 }
794 /* store coded or not-coded */
795 coded_map[ypos + 1][xpos + 1] = 1;
796
797 /* Check mv's to prevent seg.faults when error rate is high */
798 if (!mv_outside_frame)
799 {
800 bsize = k ? 8 : 16;
801 offset = k ? (((k - 1) & 1) << 3) : 0;
802 /* checking only integer component */
803 if ((xpos << 4) + (mvx / 2) + offset < 0 ||
804 (xpos << 4) + (mvx / 2) + offset > (mb_width << 4) - bsize)
805 {
806 if (!quiet)
807 printf ("mvx out of range: searching for sync\n");
808 fault = 1;
809 }
810 offset = k ? (((k - 1) & 2) << 2) : 0;
811 if ((ypos << 4) + (mvy / 2) + offset < 0 ||
812 (ypos << 4) + (mvy / 2) + offset > (mb_height << 4) - bsize)
813 {
814 if (!quiet)
815 printf ("mvy out of range: searching for sync\n");
816 fault = 1;
817 }
818 }
819 MV[0][k][ypos + 1][xpos + 1] = mvx;
820 MV[1][k][ypos + 1][xpos + 1] = mvy;
821 }
822
823 /* Store P MVs for use with true B direct mode. */
824 if (PCT_INTER == pict_type)
825 {
826 if (Mode == MODE_INTER4V || Mode == MODE_INTER4V_Q)
827 {
828 x_avg = (MV[0][1][ypos + 1][xpos + 1] + MV[0][2][ypos + 1][xpos + 1] +
829 MV[0][3][ypos + 1][xpos + 1] + MV[0][4][ypos + 1][xpos + 1] + 2) >> 2;
830 true_B_direct_mode_MV[0][ypos + 1][xpos + 1] = x_avg;
831 y_avg = (MV[1][1][ypos + 1][xpos + 1] + MV[1][2][ypos + 1][xpos + 1] +
832 MV[1][3][ypos + 1][xpos + 1] + MV[1][4][ypos + 1][xpos + 1] + 2) >> 2;
833 true_B_direct_mode_MV[1][ypos + 1][xpos + 1] = y_avg;
834 }
835 else
836 {
837 true_B_direct_mode_MV[0][ypos + 1][xpos + 1] = MV[0][0][ypos + 1][xpos + 1];
838 true_B_direct_mode_MV[1][ypos + 1][xpos + 1] = MV[1][0][ypos + 1][xpos + 1];
839 }
840 }
841
842 /* PB frame delta vectors */
843 if (pb_frame)
844 {
845 if (((MODB == PBMODE_MVDB || MODB == PBMODE_CBPB_MVDB) && pb_frame == PB_FRAMES) ||
846 (pb_frame == IM_PB_FRAMES && (MODB == PBMODE_CBPB_FRW_PRED || MODB == PBMODE_FRW_PRED)))
847 {
848 if (syntax_arith_coding)
849 {
850 mvdbx_index = decode_a_symbol (cumf_MVD);
851 mvdbx = mvdtab[mvdbx_index];
852 mvdby_index = decode_a_symbol (cumf_MVD);
853 mvdby = mvdtab[mvdby_index];
854 }
855 else
856 {
857 if (plus_type && long_vectors)
858 {
859 mvdbx = getRVLC ();
860 mvdby = getRVLC ();
861
862 /* flush start code emulation bit */
863 if (mvdbx == 1 && mvdby == 1)
864 flushbits(1);
865 }
866 else
867 {
868 mvdbx = getTMNMV ();
869 mvdby = getTMNMV ();
870 }
871 }
872
873 long_vectors_bak = long_vectors;
874
875 /* turn off long vectors when decoding forward motion vector
876 * of im.pb frames */
877 pmv0 = 0;
878 pmv1 = 0;
879 if (pb_frame == IM_PB_FRAMES &&
880 (MODB == PBMODE_CBPB_FRW_PRED || MODB == PBMODE_FRW_PRED))
881 {
882 long_vectors = 0;
883 if (MBA % mb_width && (pMODB == PBMODE_CBPB_FRW_PRED ||
884 pMODB == PBMODE_FRW_PRED))
885 {
886 /* MBA%mb_width : if not new gob */
887 pmv0 = pmvdbx;
888 pmv1 = pmvdby;
889 }
890 }
891 if (plus_type && long_vectors && !syntax_arith_coding)
892 {
893 mvdbx += pmv0;
894 mvdby += pmv1;
895 }
896 else
897 {
898 mvdbx = motion_decode (mvdbx, pmv0);
899 mvdby = motion_decode (mvdby, pmv1);
900 }
901
902 long_vectors = long_vectors_bak;
903
904 /* This will not work if the PB deltas are so large they
905 * require the second colums of the motion vector VLC table to
906 * be used. To fix this it is necessary to calculate the MV
907 * predictor for the PB delta: TRB*MV/TRD here, and use this
908 * as the second parameter to motion_decode(). The B vector
909 * itself will then be returned from motion_decode(). This
910 * will have to be changed to the PB delta again, since it is
911 * the PB delta which is used later */
912 if (trace)
913 {
914 fprintf (trace_file, "MVDB x: %d\n", mvdbx);
915 fprintf (trace_file, "MVDB y: %d\n", mvdby);
916 }
917 }
918 else
919 {
920 mvdbx = 0;
921 mvdby = 0;
922 }
923 }
924 }
925 /* Intra. */
926 else
927 {
928 /* Set MVs to 0 for potential future use in true B direct mode
929 * prediction. */
930 if (PCT_INTER == pict_type)
931 {
932 true_B_direct_mode_MV[0][ypos + 1][xpos + 1] = 0;
933 true_B_direct_mode_MV[1][ypos + 1][xpos + 1] = 0;
934 }
935 }
936 if (fault)
937 goto resync;
938 }
939 else
940 {
941 /* COD == 1 --> skipped MB */
942 if (MBA >= MBAmax)
943 {
944 /* all macroblocks decoded */
945 return;
946 }
947 if (!syntax_arith_coding)
948 if (PCT_INTER == pict_type || PCT_IPB == pict_type)
949 flushbits (1);
950
951 Mode = MODE_INTER;
952 /* Reset CBP */
953 CBP = CBPB = 0;
954 coded_map[ypos + 1][xpos + 1] = 0;
955
956 /* reset motion vectors */
957 MV[0][0][ypos + 1][xpos + 1] = 0;
958 MV[1][0][ypos + 1][xpos + 1] = 0;
959
960 if (PCT_INTER == pict_type)
961 {
962 true_B_direct_mode_MV[0][ypos + 1][xpos + 1] = 0;
963 true_B_direct_mode_MV[1][ypos + 1][xpos + 1] = 0;
964 }
965 mvdbx = 0;
966 mvdby = 0;
967 }
968
969 /* Store mode and prediction type */
970 modemap[ypos + 1][xpos + 1] = Mode;
971
972 /* store defaults for advanced intra coding mode */
973 if (advanced_intra_coding)
974 {
975 for (i = 0; i < blk_cnt; i++)
976 store_qcoeff[MBA * blk_cnt * 64 + i * 64] = 1024;
977 }
978 if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
979 {
980 if (!pb_frame)
981 {
982 MV[0][0][ypos + 1][xpos + 1] = MV[1][0][ypos + 1][xpos + 1] = 0;
983 }
984 }
985
986 reconstruct_mb:
987
988 /* pixel coordinates of top left corner of current macroblock */
989 /* one delayed because of OBMC */
990 if (xpos > 0)
991 {
992 bx = 16 * (xpos - 1);
993 by = 16 * ypos;
994 }
995 else
996 {
997 bx = coded_picture_width - 16;
998 by = 16 * (ypos - 1);
999 }
1000
1001 if (MBA > 0)
1002 {
1003 Mode = modemap[by / 16 + 1][bx / 16 + 1];
1004
1005 /* forward motion compensation for B-frame */
1006 if (pb_frame)
1007 {
1008 if (pCOD)
1009 {
1010 /* if the macroblock is not coded then it is bidir predicted */
1011 pMODB = PBMODE_BIDIR_PRED;
1012 reconstruct (bx, by, 0, pmvdbx, pmvdby, PBMODE_BIDIR_PRED);
1013 }
1014 else
1015 {
1016 if (!(pb_frame == IM_PB_FRAMES && (pMODB == PBMODE_CBPB_BCKW_PRED || pMODB == PBMODE_BCKW_PRED)))
1017 reconstruct (bx, by, 0, pmvdbx, pmvdby, pMODB);
1018 }
1019 }
1020
1021 /* motion compensation for P-frame */
1022 if (Mode == MODE_INTER || Mode == MODE_INTER_Q ||
1023 Mode == MODE_INTER4V || Mode == MODE_INTER4V_Q)
1024 reconstruct (bx, by, 1, 0, 0, pMODB);
1025
1026 /* copy or add block data into P-picture */
1027 for (comp = 0; comp < blk_cnt; comp++)
1028 {
1029 /* inverse DCT */
1030 if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
1031 {
1032 if (refidct)
1033 idctref (ld->block[comp]);
1034 else
1035 idct (ld->block[comp]);
1036 addblock (comp, bx, by, 0);
1037 }
1038 else if ((pCBP & (1 << (blk_cnt - 1 - comp))))
1039 {
1040 /* No need to to do this for blocks with no coeffs */
1041 if (refidct)
1042 idctref (ld->block[comp]);
1043 else
1044 idct (ld->block[comp]);
1045 addblock (comp, bx, by, 1);
1046 }
1047 }
1048
1049 if (pb_frame)
1050 {
1051
1052 if (pMODB == PBMODE_CBPB_BCKW_PRED || pMODB == PBMODE_BCKW_PRED)
1053 {
1054 reconstruct (bx, by, 0, 0, 0, pMODB);
1055 }
1056 /* add block data into B-picture */
1057 for (comp = 6; comp < blk_cnt + 6; comp++)
1058 {
1059 if (!pCOD || adv_pred_mode)
1060 {
1061 if (pb_frame == IM_PB_FRAMES)
1062 {
1063 if (pMODB == PBMODE_CBPB_BIDIR_PRED || pMODB == PBMODE_BIDIR_PRED || pCOD)
1064 {
1065 reconblock_b (comp - 6, bx, by, Mode, 0, 0);
1066 }
1067 }
1068 else
1069 {
1070 reconblock_b (comp - 6, bx, by, Mode, pmvdbx, pmvdby);
1071 }
1072 }
1073 if ((pCBPB & (1 << (blk_cnt - 1 - comp % 6))))
1074 {
1075 if (refidct)
1076 idctref (ld->block[comp]);
1077 else
1078 idct (ld->block[comp]);
1079 addblock (comp, bx, by, 1);
1080 }
1081 }
1082 }
1083 }
1084 /* end if (MBA > 0) */
1085 if (!COD)
1086 {
1087 Mode = modemap[ypos + 1][xpos + 1];
1088
1089 /* decode blocks */
1090 for (comp = 0; comp < <