Main Page | File List

GETPIC.C

00001 /************************************************************************
00002  *
00003  *  getpic.c, picture decoding for tmndecode (H.263 decoder)
00004  *  Copyright (C) 1995, 1996  Telenor R&D, Norway
00005  *
00006  *  Contacts:
00007  *  Robert Danielsen                  <Robert.Danielsen@nta.no>
00008  *
00009  *  Telenor Research and Development  http://www.nta.no/brukere/DVC/
00010  *  P.O.Box 83                        tel.:   +47 63 84 84 00
00011  *  N-2007 Kjeller, Norway            fax.:   +47 63 81 00 76
00012  *
00013  *  Copyright (C) 1997  University of BC, Canada
00014  *  Modified by: Mike Gallant <mikeg@ee.ubc.ca>
00015  *               Guy Cote <guyc@ee.ubc.ca>
00016  *               Berna Erol <bernae@ee.ubc.ca>
00017  *
00018  *  Contacts:
00019  *  Micheal Gallant                   <mikeg@ee.ubc.ca>
00020  *
00021  *  UBC Image Processing Laboratory   http://www.ee.ubc.ca/image
00022  *  2356 Main Mall                    tel.: +1 604 822 4051
00023  *  Vancouver BC Canada V6T1Z4        fax.: +1 604 822 5949
00024  *
00025  ************************************************************************/
00026 
00027 /* Disclaimer of Warranty
00028  * 
00029  * These software programs are available to the user without any license fee
00030  * or royalty on an "as is" basis. The University of British Columbia
00031  * disclaims any and all warranties, whether express, implied, or
00032  * statuary, including any implied warranties or merchantability or of
00033  * fitness for a particular purpose.  In no event shall the
00034  * copyright-holder be liable for any incidental, punitive, or
00035  * consequential damages of any kind whatsoever arising from the use of
00036  * these programs.
00037  * 
00038  * This disclaimer of warranty extends to the user of these programs and
00039  * user's customers, employees, agents, transferees, successors, and
00040  * assigns.
00041  * 
00042  * The University of British Columbia does not represent or warrant that the
00043  * programs furnished hereunder are free of infringement of any
00044  * third-party patents.
00045  * 
00046  * Commercial implementations of H.263, including shareware, are subject to
00047  * royalty fees to patent holders.  Many of these patents are general
00048  * enough such that they are unavoidable regardless of implementation
00049  * design.
00050  * 
00051  */
00052 
00053 
00054 /* modified to support annex O true B frames, mikeg@ee.ubc.ca
00055  * 
00056  * modified by Wayne Ellis BT Labs to run Annex E Arithmetic Decoding
00057  * <ellis_w_wayne@bt-web.bt.co.uk>
00058  * 
00059  * based on mpeg2decode, (C) 1994, MPEG Software Simulation Group and
00060  * mpeg2play, (C) 1994 Stefan Eckart <stefan@lis.e-technik.tu-muenchen.de> */
00061 
00062 
00063 #include <stdio.h>
00064 #include <stdlib.h>
00065 #include <string.h>
00066 
00067 #include "config.h"
00068 #include "tmndec.h"
00069 #include "global.h"
00070 
00071 #include "indices.h"
00072 #include "sactbls.h"
00073 
00074 static int coded_map[MAX_MBR + 1][MAX_MBC + 1];
00075 static int quant_map[MAX_MBR + 1][MAX_MBC + 1];
00076 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};
00077 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};
00078 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};
00079 
00080 
00081 /* private prototypes */
00082 static void get_I_P_MBs _ANSI_ARGS_ ((int framenum, int gob));
00083 static void get_B_MBs _ANSI_ARGS_ ((int framenum));
00084 static void get_EI_EP_MBs _ANSI_ARGS_ ((int framenum));
00085 static void clearblock _ANSI_ARGS_ ((int comp));
00086 static int motion_decode _ANSI_ARGS_ ((int vec, int pmv));
00087 static int find_pmv _ANSI_ARGS_ ((int x, int y, int block, int comp));
00088 static void addblock _ANSI_ARGS_ ((int comp, int bx, int by, int addflag));
00089 static void reconblock_b _ANSI_ARGS_ ((int comp, int bx, int by, int mode, int bdx, int bdy));
00090 static void find_bidir_limits _ANSI_ARGS_ ((int vec, int *start, int *stop, int nhv));
00091 static void find_bidir_chroma_limits _ANSI_ARGS_ ((int vec, int *start, int *stop));
00092 static void make_edge_image _ANSI_ARGS_ ((unsigned char *src, unsigned char *dst, int width, int height, int edge));
00093 static void init_enhancement_layer _ANSI_ARGS_((int layer));
00094 void edge_filter _ANSI_ARGS_ ((unsigned char *lum, unsigned char *Cb, unsigned char *Cr, int width, int height));
00095 void horiz_edge_filter _ANSI_ARGS_ ((unsigned char *rec, int width, int height, int chr));
00096 void vert_edge_filter _ANSI_ARGS_ ((unsigned char *rec, int width, int height, int chr));
00097 void vert_post_filter (unsigned char *rec, int width, int height, int chr);
00098 void horiz_post_filter (unsigned char *rec, int width, int height, int chr);
00099 void PostFilter (unsigned char *lum, unsigned char *Cb, unsigned char *Cr, int width, int height);
00100 
00101 /* reference picture selection */
00102 int  get_reference_picture(void);
00103 void store_picture(int);
00104 
00105 /* error concealment */
00106 void conceal_missing_gobs(int start_mb_row_missing, int number_of_mb_rows_missing);
00107 /**********************************************************************
00108  *
00109  *      Name:         getpicture
00110  *      Description:  decode and display one picture 
00111  *      Input:        frame number
00112  *      Returns:      
00113  *      Side effects: 
00114  *
00115  *  Date: 971102  Author: mikeg@ee.ubc.ca
00116  *
00117  *
00118  ***********************************************************************/
00119 void getpicture (int *framenum, int gob)
00120 {
00121   unsigned char *tmp;
00122   int            i, store_pb, quality=0;
00123   static int absolute_temp_ref = 0;
00124   static int prev_temp_ref = 0;
00125 
00126   switch (pict_type)
00127   {
00128 
00129     case PCT_INTRA:
00130     case PCT_INTER:
00131     case PCT_PB:
00132     case PCT_IPB:
00133 
00134       enhance_pict = NO;
00135 
00136       if (0 == UFEP && plus_type) 
00137       {
00138         mv_outside_frame = prev_mv_outside_frame;
00139 
00140         deblocking_filter_mode = prev_df;
00141 
00142         adv_pred_mode = prev_adv_pred;
00143 
00144         overlapping_MC = prev_obmc;
00145         use_4mv = prev_4mv;
00146         long_vectors = prev_long_vectors;
00147 
00148         syntax_arith_coding = prev_sac;
00149 
00150         advanced_intra_coding = prev_aic;
00151   
00152         slice_structured_mode = prev_slice_struct;
00153   
00154         reference_picture_selection_mode = prev_rps;
00155   
00156         independently_segmented_decoding_mode = prev_isd;
00157 
00158         alternative_inter_VLC_mode = prev_aivlc;
00159 
00160         modified_quantization_mode = prev_mq;
00161       }
00162 
00163       for (i = 0; i < 3; i++)
00164       {
00165         tmp = prev_frame[i];
00166         prev_I_P_frame[i] = prev_frame[i] = next_I_P_frame[i];
00167         next_I_P_frame[i] = current_frame[i] = tmp;
00168       }
00169       /* if the picture header is missing, assume temp_ref increments */
00170       /* not done yet*/
00171       
00172       /* Annex N: get the correct reference picture for prediction */ 
00173       if (reference_picture_selection_mode)
00174       {
00175         if (-1 == (quality = get_reference_picture()))
00176           break;
00177         if (quality > 256) 
00178         {
00179           stop_decoder = 1;
00180           break;
00181         }
00182       }
00183 #ifdef DEBUG
00184       if (temp_ref < prev_temp_ref) prev_temp_ref -= 256;
00185       absolute_temp_ref += temp_ref - prev_temp_ref;
00186       prev_temp_ref = temp_ref;
00187       fprintf(stdout, "%d\n", absolute_temp_ref);
00188 #endif
00189 
00190       if ((mv_outside_frame) && (*framenum > 0))
00191       {
00192         make_edge_image (prev_I_P_frame[0], edgeframe[0], coded_picture_width,
00193                          coded_picture_height, 32);
00194         make_edge_image (prev_I_P_frame[1], edgeframe[1], chrom_width, chrom_height, 16);
00195         make_edge_image (prev_I_P_frame[2], edgeframe[2], chrom_width, chrom_height, 16);
00196       }
00197 
00198       get_I_P_MBs (*framenum, gob);
00199 
00200           edge_filter (current_frame[0], current_frame[1], current_frame[2],
00201                      coded_picture_width, coded_picture_height);
00202      
00203       store_pb = pb_frame;
00204       pb_frame = 0;
00205 
00206       if (deblocking_filter_mode)
00207         edge_filter (current_frame[0], current_frame[1], current_frame[2],
00208                      coded_picture_width, coded_picture_height);
00209      
00210       pb_frame = store_pb;
00211  
00212       if (pb_frame) 
00213       { 
00214         if (deblocking_filter_mode)
00215           edge_filter(bframe[0],bframe[1],bframe[2],
00216                       coded_picture_width,coded_picture_height); 
00217       }
00218       
00219       if (1 == UFEP)
00220       {
00221         prev_mv_outside_frame = mv_outside_frame;
00222         prev_sac = syntax_arith_coding;
00223         prev_adv_pred = adv_pred_mode;
00224         prev_aic = advanced_intra_coding;
00225         prev_df = deblocking_filter_mode;
00226         prev_slice_struct = slice_structured_mode;
00227         prev_rps = reference_picture_selection_mode;
00228         prev_isd = independently_segmented_decoding_mode;
00229         prev_aivlc = alternative_inter_VLC_mode;
00230         prev_mq = modified_quantization_mode;
00231         prev_long_vectors = long_vectors;
00232         prev_obmc = overlapping_MC;
00233         prev_4mv = use_4mv;
00234       }
00235 
00236       break;
00237 
00238     case PCT_B:
00239 
00240       if (enhancement_layer_num > 1)
00241         enhance_pict = YES;
00242       else
00243         enhance_pict = NO;
00244 
00245       for (i = 0; i < 3; i++)
00246       {
00247         current_frame[i] = bframe[i];
00248       }
00249 
00250       /* Forward prediction */
00251       make_edge_image (prev_I_P_frame[0], edgeframe[0], coded_picture_width,
00252                        coded_picture_height, 32);
00253       make_edge_image (prev_I_P_frame[1], edgeframe[1], chrom_width, chrom_height, 16);
00254       make_edge_image (prev_I_P_frame[2], edgeframe[2], chrom_width, chrom_height, 16);
00255       
00256       /* Backward prediction */
00257       make_edge_image (next_I_P_frame[0], nextedgeframe[0], coded_picture_width,
00258                        coded_picture_height, 32);
00259       make_edge_image (next_I_P_frame[1], nextedgeframe[1], chrom_width, chrom_height, 16);
00260       make_edge_image (next_I_P_frame[2], nextedgeframe[2], chrom_width, chrom_height, 16);
00261   
00262       get_B_MBs (*framenum);
00263 
00264       break;
00265 
00266     case PCT_EI:
00267     case PCT_EP:
00268 
00269       enhance_pict = YES;
00270 
00271       if (!enhancement_layer_init[enhancement_layer_num-2])
00272       {
00273         init_enhancement_layer(enhancement_layer_num-2);
00274         enhancement_layer_init[enhancement_layer_num-2] = ON;
00275       }
00276 
00277       for (i = 0; i < 3; i++)
00278       {
00279         tmp = prev_enhancement_frame[enhancement_layer_num-2][i];
00280         prev_enhancement_frame[enhancement_layer_num-2][i] = 
00281           current_enhancement_frame[enhancement_layer_num-2][i];
00282         current_enhancement_frame[enhancement_layer_num-2][i] = tmp;
00283       }
00284 
00285       if (scalability_mode >= 3)
00286       {
00287         UpsampleReferenceLayerPicture();
00288         curr_reference_frame[0] = upsampled_reference_frame[0];
00289         curr_reference_frame[1] = upsampled_reference_frame[1];
00290         curr_reference_frame[2] = upsampled_reference_frame[2];
00291       }
00292       else
00293       {
00294         curr_reference_frame[0] = current_frame[0];
00295         curr_reference_frame[1] = current_frame[1];
00296         curr_reference_frame[2] = current_frame[2];
00297       }
00298 
00299       make_edge_image (prev_enhancement_frame[enhancement_layer_num-2][0], 
00300                        enhance_edgeframe[enhancement_layer_num-2][0], 
00301                        coded_picture_width, coded_picture_height, 32);
00302       make_edge_image (prev_enhancement_frame[enhancement_layer_num-2][1], 
00303                        enhance_edgeframe[enhancement_layer_num-2][1], 
00304                        chrom_width, chrom_height, 16);
00305       make_edge_image (prev_enhancement_frame[enhancement_layer_num-2][2], 
00306                        enhance_edgeframe[enhancement_layer_num-2][2], 
00307                        chrom_width, chrom_height, 16);
00308   
00309       get_EI_EP_MBs (*framenum);
00310 
00311       if (deblocking_filter_mode)
00312         edge_filter (current_enhancement_frame[enhancement_layer_num-2][0], 
00313                      current_enhancement_frame[enhancement_layer_num-2][1], 
00314                      current_enhancement_frame[enhancement_layer_num-2][2], 
00315                      coded_picture_width, coded_picture_height);
00316 
00317       break;
00318 
00319     default:
00320 
00321       break;
00322 
00323   }
00324 }
00325 
00326 /* decode all macroblocks of the current picture */
00327 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};
00328 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};
00329 
00330 static void get_I_P_MBs (int framenum, int gob)
00331 {
00332   int comp;
00333   int MBA, MBAmax;
00334   int bx, by;
00335 
00336   int COD = 0, MCBPC, CBPY, CBP = 0, CBPB = 0, MODB = 0, Mode = 0, DQUANT;
00337   int COD_index, CBPY_index, MODB_index, DQUANT_index, MCBPC_index;
00338   int INTRADC_index, YCBPB_index, UVCBPB_index, mvdbx_index, mvdby_index;
00339   int mvx = 0, mvy = 0, mvy_index, mvx_index, pmv0, pmv1, xpos, ypos, i, k;
00340   int mvdbx = 0, mvdby = 0, pmvdbx, pmvdby, YCBPB, UVCBPB, gobheader_read;
00341   int startmv, stopmv, offset, bsize, last_done = 0, pCBP = 0, pCBPB = 0, pCOD = 0, pMODB = 0;
00342   int DQ_tab[4] = {-1, -2, 1, 2};
00343   short *bp;
00344   int long_vectors_bak;
00345   int tmp = 0;
00346   int pnewgob = 0;
00347 
00348   /* Error concealment variables */
00349   int pypos = 0;
00350   int start_mb_row_missing, number_of_mb_rows_missing;
00351 
00352 
00353   /* variables used in advanced intra coding mode */
00354   int INTRA_AC_DC = 0, INTRA_AC_DC_index;
00355   int quality=0;
00356   int decode_last_mb = 0;
00357   int dont_reconstruct_next_mb;
00358 
00359   /* number of macroblocks per picture */
00360   MBAmax = mb_width * mb_height;
00361   gob -= 1; /* 1 was added in getheader() */
00362 
00363   MBA = 0;                      /* macroblock address */
00364   newgob = 0;
00365   xpos = ypos = 0;
00366 
00367   /* mark MV's above the picture */
00368   for (i = 1; i < mb_width + 1; i++)
00369   {
00370     for (k = 0; k < 5; k++)
00371     {
00372       MV[0][k][0][i] = NO_VEC;
00373       MV[1][k][0][i] = NO_VEC;
00374     }
00375     modemap[0][i] = MODE_INTRA;
00376   }
00377   /* zero MV's on the sides of the picture */
00378   for (i = 0; i < mb_height + 1; i++)
00379   {
00380     for (k = 0; k < 5; k++)
00381     {
00382       MV[0][k][i][0] = 0;
00383       MV[1][k][i][0] = 0;
00384       MV[0][k][i][mb_width + 1] = 0;
00385       MV[1][k][i][mb_width + 1] = 0;
00386     }
00387     modemap[i][0] = MODE_INTRA;
00388     modemap[i][mb_width + 1] = MODE_INTRA;
00389   }
00390 
00391   fault = 0;
00392   gobheader_read = 0;
00393   /* if a gob with GN != 0 was present as the first gob
00394    * of a picture, decode the rest of the gob header */
00395   if (gob) goto getgobhrd;
00396 
00397   for (;;)
00398   {
00399 
00400     if (MBA >= MBAmax)
00401     {
00402       /* all macroblocks decoded */
00403       memcpy(anchorframemodemap,modemap,(sizeof(int)*(MBR+1)*(MBC+2)) );
00404       return;
00405     }
00406 
00407 resync:
00408     /* This version of the decoder does not resync on every possible
00409      * error, and it does not do all possible error checks. It is not
00410      * difficult to make it much more error robust, but I do not think it
00411      * is necessary to include this in the freely available version. */
00412    
00413     if (fault)
00414     {
00415       startcode ();             /* sync on new startcode */
00416       fault = 0;
00417     }
00418     if (!(showbits (22) >> 6))
00419     {
00420       /* startcode */
00421       startcode ();
00422 
00423       /* in case of byte aligned start code, ie. PSTUF, GSTUF or ESTUF is
00424        * used */
00425       if (showbits (22) == (32 | SE_CODE))
00426       {
00427         /* end of sequence */
00428         if (!(syntax_arith_coding && MBA < MBAmax))
00429         {
00430           return;
00431         }
00432       } 
00433       else if ((showbits (22) == PSC << 5))
00434       {
00435         /* new picture */
00436         /* conceal the last mb row */
00437         if (concealment && MBA < MBAmax)
00438         {
00439            /* store the missing GOB number for error concealment */
00440           number_of_mb_rows_missing = (MBAmax - MBA)  / mb_width ;
00441           start_mb_row_missing = MBA / mb_width; 
00442           decode_last_mb = 1;
00443           pypos = ypos;
00444         } 
00445         else if (!(syntax_arith_coding && MBA < MBAmax))
00446         {
00447           return;
00448         }
00449       }
00450       else
00451       {
00452         if (!(syntax_arith_coding && MBA % mb_width))
00453         {
00454           if (syntax_arith_coding)
00455           {
00456             /* SAC hack to finish GOBs which  end with MBs coded with no
00457              * bits. */
00458             gob = (showbits (22) & 31);
00459             if (gob * mb_width != MBA)
00460               goto finish_gob;
00461           }
00462           gob = getheader () - 1;
00463           if (gob > mb_height)
00464           {
00465             return;
00466           }
00467           /* Get the remaining of the GOB header:
00468           this should be the complete gob header to facilitate the integration
00469           of the slice structure and the syntax of Annex N and the BCM.
00470           This will be changed when Slices are implemented */
00471           /* if the first sync of a picture in not GN 0, start here */
00472 getgobhrd:
00473           getgobheader();
00474 
00475           /* Get the reference picture for prediction corresponding to TRP 
00476           in the GOB header.  Only the reference GOB is needed, but it is 
00477           simpler to get the whole reference picture.  
00478           This should be changed to save time */          
00479           if (reference_picture_selection_mode)
00480           {
00481             if (-1 == (quality = get_reference_picture()))
00482               break;
00483             if (quality > 256) 
00484             {
00485               stop_decoder = 1;
00486               break;
00487             }         
00488             if ((mv_outside_frame) && (framenum > 0))
00489             {
00490               make_edge_image (prev_I_P_frame[0], edgeframe[0], coded_picture_width,
00491                 coded_picture_height, 32);
00492               make_edge_image (prev_I_P_frame[1], edgeframe[1], chrom_width, chrom_height, 16);
00493               make_edge_image (prev_I_P_frame[2], edgeframe[2], chrom_width, chrom_height, 16);
00494             }
00495           }       
00496 
00497           /* if some data is loss due to (channel) errors, 
00498            * decode the last (delayed) macroblock */
00499           if (concealment && (gob * mb_width != MBA))
00500           {
00501 
00502            /* if we are missing the last gob of the previous frame and 
00503             * the first gob of the current frame, framenum should be 
00504             * incremented */
00505             /* store the missing GOB number for error concealment */          
00506             if (gob - MBA / mb_width < 0)
00507             {
00508               ++framenum;
00509               number_of_mb_rows_missing = (MBAmax - MBA) / mb_width ;
00510             }else
00511               number_of_mb_rows_missing = gob - MBA / mb_width ;
00512             start_mb_row_missing = MBA / mb_width; 
00513             decode_last_mb = 1;
00514             pypos = ypos;
00515           }
00516 
00517           xpos = 0;
00518           ypos = gob;
00519           MBA = ypos * mb_width;
00520         
00521           newgob = 1;
00522           gobheader_read = 1;
00523           if (syntax_arith_coding)
00524             decoder_reset ();   /* init. arithmetic decoder buffer after
00525                                  * gob */
00526         }
00527       }
00528     }
00529 
00530     if (decode_last_mb)
00531     {
00532       if (!start_mb_row_missing)
00533       {
00534         /* the first gob is missing, we don't need to decode the last MB */
00535         goto conceal_gob;
00536       }
00537       else
00538       {
00539         xpos = 0;
00540         ypos = ++pypos;
00541         COD = 1;
00542         goto reconstruct_mb;
00543       }
00544     }
00545     dont_reconstruct_next_mb = 0;
00546 
00547 finish_gob:
00548 
00549     /* SAC specific label */
00550     if (!gobheader_read)
00551     {
00552       xpos = MBA % mb_width;
00553       ypos = MBA / mb_width;
00554       if (xpos == 0 && ypos > 0)
00555         newgob = 0;
00556     } 
00557     else
00558     {
00559       gobheader_read = 0;
00560     }
00561 
00562 read_cod:
00563 
00564     if (syntax_arith_coding)
00565     {
00566       if (pict_type == PCT_INTER || pict_type == PCT_IPB)
00567       {
00568         COD_index = decode_a_symbol (cumf_COD);
00569         COD = codtab[COD_index];
00570       } 
00571       else
00572       {
00573         COD = 0;                /* COD not used in I-pictures, set to zero */
00574         coded_map[ypos + 1][xpos + 1] = 1;
00575       }
00576     } 
00577     else
00578     {
00579       if (PCT_INTER == pict_type || PCT_IPB == pict_type)
00580       {
00581         COD = showbits (1);
00582       } 
00583       else
00584       {
00585         COD = 0;                /* Intra picture -> not skipped */
00586         coded_map[ypos + 1][xpos + 1] = 1;
00587       }
00588     }
00589 
00590     if (!COD)
00591     {
00592       /* COD == 0 --> not skipped */
00593       if (syntax_arith_coding)
00594       {
00595         if (pict_type == PCT_INTER || pict_type == PCT_IPB)
00596         {
00597           if (plus_type) 
00598           {
00599             MCBPC_index = decode_a_symbol (cumf_MCBPC_4MVQ);
00600             MCBPC = mcbpctab_4mvq[MCBPC_index];
00601           } 
00602           else 
00603           {
00604             MCBPC_index = decode_a_symbol (cumf_MCBPC_no4MVQ);
00605             MCBPC = mcbpctab[MCBPC_index]; 
00606           }
00607         } 
00608         else
00609         {
00610           MCBPC_index = decode_a_symbol (cumf_MCBPC_intra);
00611           MCBPC = mcbpc_intratab[MCBPC_index];
00612         }
00613       } 
00614       else
00615       {
00616         if (PCT_INTER == pict_type || PCT_IPB == pict_type)
00617         {
00618           /* flush COD bit */
00619           flushbits (1);
00620         }
00621         if (PCT_INTRA == pict_type)
00622         {
00623           MCBPC = getMCBPCintra ();
00624         } 
00625         else
00626         {
00627           MCBPC = getMCBPC ();
00628         }
00629       }
00630 
00631       if (fault)
00632         goto resync;
00633 
00634       if (MCBPC == 255)
00635       {      
00636         /* stuffing - read next COD without advancing MB count. */        
00637         goto read_cod;        
00638       } 
00639       else      
00640       {      
00641         /* normal MB data */        
00642         Mode = MCBPC & 7;        
00643         if (advanced_intra_coding && (Mode == MODE_INTRA || Mode == MODE_INTRA_Q))        
00644         {        
00645           /* get INTRA_AC_DC mode for annex I */          
00646           if (syntax_arith_coding)          
00647           {          
00648             INTRA_AC_DC_index = decode_a_symbol (cumf_INTRA_AC_DC);            
00649             INTRA_AC_DC = intra_ac_dctab[INTRA_AC_DC_index];            
00650           } 
00651           else          
00652           {          
00653             /* using VLC */            
00654             if (!showbits (1))            
00655               INTRA_AC_DC = getbits (1);              
00656             else            
00657               INTRA_AC_DC = getbits (2);            
00658           }          
00659         }
00660         
00661         /* MODB and CBPB */        
00662         if (pb_frame)        
00663         {        
00664           CBPB = 0;          
00665           if (syntax_arith_coding)          
00666           {          
00667             if (pb_frame== PB_FRAMES)            
00668             {            
00669               MODB_index = decode_a_symbol (cumf_MODB_G);              
00670               MODB = modb_tab_G[MODB_index];              
00671             }            
00672             else            
00673             {            
00674               MODB_index = decode_a_symbol (cumf_MODB_M);              
00675               MODB = modb_tab_M[MODB_index];              
00676             }            
00677           } 
00678           else          
00679             MODB = getMODB ();
00680           
00681           if ( (MODB == PBMODE_CBPB_MVDB && pb_frame == PB_FRAMES) ||          
00682                (pb_frame == IM_PB_FRAMES && 
00683                (MODB == PBMODE_CBPB_FRW_PRED ||            
00684                 MODB == PBMODE_CBPB_BIDIR_PRED || MODB == PBMODE_CBPB_BCKW_PRED) ) )            
00685           {          
00686             if (syntax_arith_coding)            
00687             {            
00688               for (i = 0; i < 4; i++)              
00689               {              
00690                 YCBPB_index = decode_a_symbol (cumf_YCBPB);                
00691                 YCBPB = ycbpb_tab[YCBPB_index];                
00692                 CBPB |= (YCBPB << (6 - 1 - i));                
00693               }              
00694               for (i = 4; i < 6; i++)              
00695               {              
00696                 UVCBPB_index = decode_a_symbol (cumf_UVCBPB);                
00697                 UVCBPB = uvcbpb_tab[UVCBPB_index];                
00698                 CBPB |= (UVCBPB << (6 - 1 - i));                
00699               }              
00700             } 
00701             else            
00702               CBPB = getbits (6); 
00703           }          
00704         }
00705         
00706         if (syntax_arith_coding)        
00707         {          
00708           if ((Mode == MODE_INTRA || Mode == MODE_INTRA_Q))          
00709           {          
00710             /* Intra */            
00711             CBPY_index = decode_a_symbol (cumf_CBPY_intra);            
00712             CBPY = cbpy_intratab[CBPY_index];            
00713           } 
00714           else          
00715           {          
00716             CBPY_index = decode_a_symbol (cumf_CBPY);            
00717             CBPY = cbpytab[CBPY_index];            
00718           }
00719           
00720         } 
00721         else        
00722         {        
00723           CBPY = getCBPY ();          
00724         }        
00725       }
00726 
00727       /* Decode Mode and CBP */      
00728       if ((Mode == MODE_INTRA || Mode == MODE_INTRA_Q))
00729       {
00730         /* Intra */
00731         coded_map[ypos + 1][xpos + 1] = 1;
00732 
00733         if (!syntax_arith_coding)
00734           CBPY = CBPY ^ 15;   /* needed in huffman coding only */
00735       } 
00736       else if (!syntax_arith_coding && alternative_inter_VLC_mode && ((MCBPC >> 4) == 3))
00737         CBPY = CBPY ^ 15;     /* Annex S.3 change */
00738 
00739       CBP = (CBPY << 2) | (MCBPC >> 4);
00740         
00741       if ((Mode == MODE_INTER4V || Mode == MODE_INTER4V_Q) && !use_4mv)
00742       {
00743         fault = 1;
00744       }
00745 
00746       if (Mode == MODE_INTER_Q || Mode == MODE_INTRA_Q || Mode == MODE_INTER4V_Q)
00747       {
00748         /* Read DQUANT if necessary */
00749         if (syntax_arith_coding)
00750         {
00751           DQUANT_index = decode_a_symbol (cumf_DQUANT);
00752           DQUANT = dquanttab[DQUANT_index] - 2;
00753           quant += DQUANT;
00754         } 
00755         else 
00756         {
00757           if (!modified_quantization_mode)
00758           {
00759             DQUANT = getbits (2);
00760             quant += DQ_tab[DQUANT];
00761           } 
00762           else
00763           {
00764             tmp = getbits (1);
00765             if (tmp)
00766             {
00767               /* only one more additional bit was sent */
00768               tmp = getbits (1);
00769               if (tmp)
00770               {
00771                 /* second bit of quant is 1 */
00772                 DQUANT = change_of_quant_tab_11[quant];
00773               } 
00774               else
00775               {
00776                 /* second bit of quant is 0 */
00777                 DQUANT = change_of_quant_tab_10[quant];
00778               }
00779               quant += DQUANT;
00780             } 
00781             else
00782             {
00783               /* five additional bits were sent as            
00784                * DQUANT */
00785               DQUANT = getbits (5);
00786               quant = DQUANT;
00787             }
00788           }
00789         }
00790       
00791         if (quant > 31 || quant < 1)
00792         {
00793           quant = mmax (1, mmin (31, quant));
00794           /* could set fault-flag and resync here */
00795           fault = 1;
00796         }
00797       }
00798     
00799       /* motion vectors */    
00800       if (Mode == MODE_INTER || Mode == MODE_INTER_Q ||
00801           Mode == MODE_INTER4V || Mode == MODE_INTER4V_Q || pb_frame)
00802       {     
00803         if (Mode == MODE_INTER4V || Mode == MODE_INTER4V_Q)
00804         {
00805           startmv = 1;
00806           stopmv = 4;
00807         } 
00808         else
00809         { 
00810           startmv = 0;
00811           stopmv = 0;
00812         }
00813 
00814         for (k = startmv; k <= stopmv; k++)
00815         {
00816           if (syntax_arith_coding)
00817           {
00818             mvx_index = decode_a_symbol (cumf_MVD);
00819             mvx = mvdtab[mvx_index];
00820             mvy_index = decode_a_symbol (cumf_MVD);
00821             mvy = mvdtab[mvy_index];
00822           } 
00823           else
00824           {
00825             if (plus_type && long_vectors)
00826             {
00827               mvx = getRVLC ();
00828               mvy = getRVLC ();
00829  
00830               /* flush start code emulation bit */
00831               if (mvx == 1 && mvy == 1)
00832                 flushbits(1);
00833             }
00834             else
00835             {
00836               mvx = getTMNMV ();
00837               mvy = getTMNMV ();
00838             }
00839           }
00840 
00841           pmv0 = find_pmv (xpos, ypos, k, 0);
00842           pmv1 = find_pmv (xpos, ypos, k, 1);
00843 
00844           if (plus_type && long_vectors && !syntax_arith_coding)
00845           {
00846             mvx += pmv0;
00847             mvy += pmv1;
00848           }
00849           else
00850           {
00851             mvx = motion_decode (mvx, pmv0);
00852             mvy = motion_decode (mvy, pmv1);
00853           }
00854 
00855           /* store coded or not-coded */
00856           coded_map[ypos + 1][xpos + 1] = 1;
00857 
00858           /* Check mv's to prevent seg.faults when error rate is high */
00859           if (!mv_outside_frame)
00860           {
00861             bsize = k ? 8 : 16;
00862             offset = k ? (((k - 1) & 1) << 3) : 0;
00863             /* checking only integer component */
00864             if ((xpos << 4) + (mvx / 2) + offset < 0 ||
00865                 (xpos << 4) + (mvx / 2) + offset > (mb_width << 4) - bsize)
00866             {
00867               fault = 1;
00868             }
00869             offset = k ? (((k - 1) & 2) << 2) : 0;
00870             if ((ypos << 4) + (mvy / 2) + offset < 0 ||
00871                 (ypos << 4) + (mvy / 2) + offset > (mb_height << 4) - bsize)
00872             {
00873               fault = 1;
00874             }
00875           }
00876           true_B_direct_mode_MV[0][k][ypos + 1][xpos + 1] = MV[0][k][ypos + 1][xpos + 1] = mvx;
00877           true_B_direct_mode_MV[1][k][ypos + 1][xpos + 1] = MV[1][k][ypos + 1][xpos + 1] = mvy;
00878         }
00879 
00880         /* PB frame delta vectors */
00881         if (pb_frame)
00882         {
00883           if (((MODB == PBMODE_MVDB || MODB == PBMODE_CBPB_MVDB) && pb_frame == PB_FRAMES) ||
00884               (pb_frame == IM_PB_FRAMES && (MODB == PBMODE_CBPB_FRW_PRED || MODB == PBMODE_FRW_PRED)))
00885           {
00886             if (syntax_arith_coding)
00887             {
00888               mvdbx_index = decode_a_symbol (cumf_MVD);
00889               mvdbx = mvdtab[mvdbx_index];
00890               mvdby_index = decode_a_symbol (cumf_MVD);
00891               mvdby = mvdtab[mvdby_index];
00892             } 
00893             else
00894             {
00895               if (plus_type && long_vectors)
00896               {
00897                 mvdbx = getRVLC ();
00898                 mvdby = getRVLC ();
00899 
00900                 /* flush start code emulation bit */
00901                 if (mvdbx == 1 && mvdby == 1)
00902                   flushbits(1); 
00903               }
00904               else
00905               {
00906                 mvdbx = getTMNMV ();
00907                 mvdby = getTMNMV ();
00908               }
00909             }
00910 
00911             long_vectors_bak = long_vectors;
00912 
00913             /* turn off long vectors when decoding forward motion vector
00914              * of im.pb frames */
00915             pmv0 = 0;
00916             pmv1 = 0;
00917             if (pb_frame == IM_PB_FRAMES && 
00918                 (MODB == PBMODE_CBPB_FRW_PRED || MODB == PBMODE_FRW_PRED))
00919             {
00920               long_vectors = 0;
00921               if (MBA % mb_width && (pMODB == PBMODE_CBPB_FRW_PRED ||
00922                                      pMODB == PBMODE_FRW_PRED))
00923               {
00924                 /* MBA%mb_width : if not new gob */
00925                 pmv0 = pmvdbx;
00926                 pmv1 = pmvdby;
00927               }
00928             }
00929             if (plus_type && long_vectors && !syntax_arith_coding)
00930             {
00931               mvdbx += pmv0;
00932               mvdby += pmv1;
00933             }
00934             else
00935             {
00936               mvdbx = motion_decode (mvdbx, pmv0);
00937               mvdby = motion_decode (mvdby, pmv1);
00938             }
00939 
00940             long_vectors = long_vectors_bak;
00941 
00942             /* This will not work if the PB deltas are so large they
00943              * require the second colums of the motion vector VLC table to
00944              * be used.  To fix this it is necessary to calculate the MV
00945              * predictor for the PB delta: TRB*MV/TRD here, and use this
00946              * as the second parameter to motion_decode(). The B vector
00947              * itself will then be returned from motion_decode(). This
00948              * will have to be changed to the PB delta again, since it is
00949              * the PB delta which is used later */
00950           } 
00951           else
00952           {
00953             mvdbx = 0;
00954             mvdby = 0;
00955           }
00956         }
00957       }
00958       /* Intra. */
00959       else
00960       {
00961         /* Set MVs to 0 for potential future use in true B direct mode
00962          * prediction. */
00963         if (PCT_INTER == pict_type)
00964         {
00965           for (k=0; k<5; k++)
00966           {
00967             true_B_direct_mode_MV[0][k][ypos + 1][xpos + 1] = 0;
00968             true_B_direct_mode_MV[1][k][ypos + 1][xpos + 1] = 0;
00969           }
00970         }
00971       }
00972       if (fault)
00973         goto resync;
00974     } 
00975     else
00976     {
00977       /* COD == 1 --> skipped MB */
00978       if (MBA >= MBAmax)
00979       {
00980         /* all macroblocks decoded */
00981         memcpy(anchorframemodemap,modemap,(sizeof(int)*(MBR+1)*(MBC+2)) );
00982         return;
00983       }
00984       if (!syntax_arith_coding)
00985         if (PCT_INTER == pict_type || PCT_IPB == pict_type)
00986           flushbits (1);
00987 
00988       Mode = MODE_INTER;
00989       /* Reset CBP */
00990       CBP = CBPB = 0;
00991       coded_map[ypos + 1][xpos + 1] = 0;
00992      
00993       /* reset motion vectors */
00994       MV[0][0][ypos + 1][xpos + 1] = 0;
00995       MV[1][0][ypos + 1][xpos + 1] = 0;
00996 
00997       if (PCT_INTER == pict_type)
00998       {
00999         for (k=0; k<5; k++)
01000         {
01001           true_B_direct_mode_MV[0][k][ypos + 1][xpos + 1] = 0;
01002           true_B_direct_mode_MV[1][k][ypos + 1][xpos + 1] = 0;
01003         }
01004       }
01005       mvdbx = 0;
01006       mvdby = 0;    
01007     }
01008 
01009     /* Store mode and prediction type */
01010     modemap[ypos + 1][xpos + 1] = Mode;
01011     
01012     if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
01013     {
01014       if (!pb_frame)
01015       {
01016         MV[0][0][ypos + 1][xpos + 1] = MV[1][0][ypos + 1][xpos + 1] = 0;
01017       }
01018     }
01019 
01020 reconstruct_mb:
01021 
01022     /* pixel coordinates of top left corner of current macroblock */
01023     /* one delayed because of OBMC */
01024     if (xpos > 0)
01025     {
01026       bx = 16 * (xpos - 1);
01027       by = 16 * ypos;
01028     } 
01029     else
01030     {
01031       bx = coded_picture_width - 16;
01032       by = 16 * (ypos - 1);
01033     }
01034 
01035     if (MBA > 0 && !dont_reconstruct_next_mb)
01036     {
01037       Mode = modemap[by / 16 + 1][bx / 16 + 1];
01038 
01039       /* forward motion compensation for B-frame */
01040       if (pb_frame)
01041       {
01042         if (pCOD)
01043         {
01044           /* if the macroblock is not coded then it is bidir predicted */
01045           pMODB = PBMODE_BIDIR_PRED;
01046           reconstruct (bx, by, 0, pmvdbx, pmvdby, PBMODE_BIDIR_PRED, pnewgob);
01047         } 
01048         else
01049         {
01050           if (!(pb_frame == IM_PB_FRAMES && (pMODB == PBMODE_CBPB_BCKW_PRED || pMODB == PBMODE_BCKW_PRED)))
01051             reconstruct (bx, by, 0, pmvdbx, pmvdby, pMODB, pnewgob);
01052         }
01053       }
01054     
01055       /* motion compensation for P-frame */
01056       if (Mode == MODE_INTER || Mode == MODE_INTER_Q ||
01057           Mode == MODE_INTER4V || Mode == MODE_INTER4V_Q)
01058       {
01059         reconstruct (bx, by, 1, 0, 0, pMODB, pnewgob);
01060       }
01061 
01062       /* copy or add block data into P-picture */
01063       for (comp = 0; comp < blk_cnt; comp++)
01064       {
01065         /* inverse DCT */
01066         if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
01067         {
01068           if (refidct)
01069             idctref (ld->block[comp]);
01070           else
01071             idct (ld->block[comp]);
01072           addblock (comp, bx, by, 0);
01073         } 
01074         else if ((pCBP & (1 << (blk_cnt - 1 - comp))))
01075         {
01076           /* No need to to do this for blocks with no coeffs */
01077           if (refidct)
01078             idctref (ld->block[comp]);
01079           else
01080             idct (ld->block[comp]);
01081           addblock (comp, bx, by, 1);
01082         }
01083       }
01084 
01085       if (pb_frame)
01086       {
01087 
01088         if (pMODB == PBMODE_CBPB_BCKW_PRED || pMODB == PBMODE_BCKW_PRED)
01089         {
01090           reconstruct (bx, by, 0, 0, 0, pMODB, pnewgob);
01091         }
01092         /* add block data into B-picture */
01093         for (comp = 6; comp < blk_cnt + 6; comp++)
01094         {
01095           if (!pCOD || adv_pred_mode)
01096           {
01097             if (pb_frame == IM_PB_FRAMES)
01098             {
01099               if (pMODB == PBMODE_CBPB_BIDIR_PRED || pMODB == PBMODE_BIDIR_PRED || pCOD)
01100               {
01101                 reconblock_b (comp - 6, bx, by, Mode, 0, 0);
01102               }
01103             } 
01104             else
01105             {
01106               reconblock_b (comp - 6, bx, by, Mode, pmvdbx, pmvdby);
01107             }
01108           }
01109           if ((pCBPB & (1 << (blk_cnt - 1 - comp % 6))))
01110           {
01111             if (refidct)
01112               idctref (ld->block[comp]);
01113             else
01114               idct (ld->block[comp]);
01115             addblock (comp, bx, by, 1);
01116           }
01117         }
01118       }
01119     }
01120     /* end if (MBA > 0) */
01121     if (!COD)
01122     {
01123       Mode = modemap[ypos + 1][xpos + 1];
01124     
01125       /* decode blocks */
01126       for (comp = 0; comp < blk_cnt; comp++)
01127       {
01128         clearblock (comp);
01129         if ((Mode == MODE_INTRA || Mode == MODE_INTRA_Q) && !(advanced_intra_coding))
01130         {
01131           /* Intra (except in advanced intra coding mode) */
01132           bp = ld->block[comp];
01133           if (syntax_arith_coding)
01134           {
01135             INTRADC_index = decode_a_symbol (cumf_INTRADC);
01136             bp[0] = intradctab[INTRADC_index];
01137           } 
01138           else
01139           {
01140             bp[0] = getbits (8);
01141           }
01142 
01143           if (bp[0] == 128)
01144             if (!quiet)
01145               fprintf (stderr, "Illegal DC-coeff: 1000000\n");
01146           if (bp[0] == 255)   /* Spec. in H.26P, not in TMN4 */
01147             bp[0] = 128;
01148           bp[0] *= 8;         /* Iquant */
01149           if ((CBP & (1 << (blk_cnt - 1 - comp))))
01150           {
01151             if (!syntax_arith_coding)
01152               getblock (comp, 0, 0, Mode);
01153             else
01154               get_sac_block (comp, 0, 0, Mode);
01155           }
01156         } 
01157         else
01158         {
01159           /* Inter (or Intra in advanced intra coding mode) */
01160           if ((CBP & (1 << (blk_cnt - 1 - comp))))
01161           {
01162             if (!syntax_arith_coding)
01163               getblock (comp, 1, INTRA_AC_DC, Mode);
01164             else
01165               get_sac_block (comp, 1, INTRA_AC_DC, Mode);
01166           }
01167         }
01168 
01169         if (fault)
01170           goto resync;
01171       }
01172 
01173       /* Decode B blocks */
01174       if (pb_frame)
01175       {
01176         for (comp = 6; comp < blk_cnt + 6; comp++)
01177         {
01178           clearblock (comp);
01179           if ((CBPB & (1 << (blk_cnt - 1 - comp % 6))))
01180           {
01181             if (!syntax_arith_coding)
01182               getblock (comp, 1, 0, MODE_INTER);
01183             else
01184               get_sac_block (comp, 1, 0, MODE_INTER);
01185           } 
01186           if (fault)
01187             goto resync;        
01188         }
01189       }
01190     }
01191   
01192 conceal_gob:
01193     /* decode the last MB if data is missing */
01194     if (decode_last_mb)
01195     {
01196       conceal_missing_gobs(start_mb_row_missing, number_of_mb_rows_missing);
01197       /* all macroblocks in the picture are done, return 
01198        * if the first gob in the next frame is also missing, 
01199        * we will also lose the secon gob of that next frame.
01200        * This can be dealt with, but we will live with that for now. */
01201       if ( (number_of_mb_rows_missing + start_mb_row_missing) * mb_width >= MBAmax) return;
01202       ypos = gob;
01203       decode_last_mb = 0;
01204       dont_reconstruct_next_mb = 1;
01205       goto finish_gob;
01206     }
01207     else
01208     {
01209       /* advance to next macroblock */
01210       MBA++;
01211       pCBP = CBP;
01212       pCBPB = CBPB;
01213       pCOD = COD;
01214       pMODB = MODB;
01215       quant_map[ypos + 1][xpos + 1] = quant;
01216       
01217       pmvdbx = mvdbx;
01218       pmvdby = mvdby;
01219       fflush (stdout);
01220       pnewgob = newgob;
01221       
01222       if (MBA >= MBAmax && !last_done)
01223       {
01224         COD = 1;
01225         xpos = 0;
01226         ypos++;
01227         last_done = 1;
01228         goto reconstruct_mb;   
01229       }
01230     }
01231   }
01232 }
01233 
01234 /**********************************************************************
01235  *
01236  *      Name:                 get_B_MBs
01237  *      Description:  decode MBs for one B picture 
01238  *      Input:        frame number
01239  *      Returns:      
01240  *      Side effects: 
01241  *
01242  *  Date: 971102  Author: mikeg@ee.ubc.ca
01243  *
01244  *
01245  ***********************************************************************/
01246  static void get_B_MBs (int framenum)
01247  {
01248   int comp;
01249   int MBA, MBAmax;
01250   int bx, by;
01251   int COD = 0, CBP = 0, Mode = 0, DQUANT;
01252   int xpos, ypos, gob, i;
01253   int mvfx = 0, mvfy = 0, mvbx = 0, mvby = 0, pmvf0, pmvf1, pmvb0, pmvb1;
01254   int gfid, gobheader_read;
01255   int last_done = 0, pCBP = 0, pCOD = 0;
01256   int DQ_tab[4] = {-1, -2, 1, 2};
01257   short *bp;
01258   int true_B_cbp = 0, true_B_quant = 0, true_B_prediction_type;
01259   int CBPC = 0, CBPY = 0, tmp = 0;
01260 
01261   /* variables used in advanced intra coding mode */
01262   int INTRA_AC_DC = 0;
01263 
01264   /* number of macroblocks per picture */
01265   MBAmax = mb_width * mb_height;
01266 
01267   MBA = 0;                      /* macroblock address */
01268   newgob = 0;
01269 
01270   /* mark MV's above the picture */
01271   for (i = 1; i < mb_width + 1; i++)
01272   {
01273     MV[0][0][0][i] = NO_VEC;
01274     MV[1][0][0][i] = NO_VEC;
01275     MV[0][5][0][i] = NO_VEC;
01276     MV[1][5][0][i] = NO_VEC;
01277     modemap[0][i] = MODE_INTRA;
01278     predictionmap[0][i] = B_INTRA_PREDICTION;
01279   }
01280   /* zero MV's on the sides of the picture */
01281   for (i = 0; i < mb_height + 1; i++)
01282   {
01283     MV[0][0][i][0] = 0;
01284     MV[1][0][i][0] = 0;
01285     MV[0][0][i][mb_width + 1] = 0;
01286     MV[1][0][i][mb_width + 1] = 0;
01287 
01288     MV[0][5][i][0] = 0;
01289     MV[1][5][i][0] = 0;
01290     MV[0][5][i][mb_width + 1] = 0;
01291     MV[1][5][i][mb_width + 1] = 0;
01292 
01293     modemap[i][0] = MODE_INTRA;
01294     modemap[i][mb_width + 1] = MODE_INTRA;
01295     predictionmap[i][0] = B_INTRA_PREDICTION;
01296     predictionmap[i][mb_width + 1] = B_INTRA_PREDICTION;
01297   }
01298 
01299   fault = 0;
01300   gobheader_read = 0;
01301 
01302   for (;;)
01303   {
01304 
01305 resync:
01306     /* This version of the decoder does not resync on every possible
01307      * error, and it does not do all possible error checks. It is not
01308      * difficult to make it much more error robust, but I do not think it
01309      * is necessary to include this in the freely available version. */
01310    
01311     if (fault)
01312     {
01313       startcode ();             /* sync on new startcode */
01314       fault = 0;
01315     }
01316     if (!(showbits (22) >> 6))
01317     {
01318       /* startcode */
01319       startcode ();
01320 
01321       /* in case of byte aligned start code, ie. PSTUF, GSTUF or ESTUF is
01322        * used */
01323       if (showbits (22) == (32 | SE_CODE))
01324       {
01325         /* end of sequence */
01326         if (!(MBA < MBAmax))
01327         {
01328           return;
01329         }
01330       } 
01331       else if ((showbits (22) == PSC << 5))
01332       {
01333         /* new picture */
01334         if (!(MBA < MBAmax))
01335         {
01336           return;
01337         }
01338       } 
01339       else
01340       {
01341         if (!(MBA % mb_width))
01342         {
01343           gob = getheader () - 1;
01344           if (gob > mb_height)
01345           {
01346             return;
01347           }
01348           /* GFID is not allowed to change unless PTYPE in picture header
01349            * changes */
01350           gfid = getbits (2);
01351           /* NB: in error-prone environments the decoder can use this
01352            * value to determine whether a picture header where the PTYPE
01353            * has changed, has been lost */
01354 
01355           quant = getbits (5);
01356           xpos = 0;
01357           ypos = gob;
01358           MBA = ypos * mb_width;
01359 
01360           newgob = 1;
01361           gobheader_read = 1;
01362         }
01363       }
01364     }
01365 
01366     /* SAC specific label */
01367     if (!gobheader_read)
01368     {
01369       xpos = MBA % mb_width;
01370       ypos = MBA / mb_width;
01371       if (xpos == 0 && ypos > 0)
01372         newgob = 0;
01373     } 
01374     else
01375       gobheader_read = 0;
01376 
01377     if (MBA >= MBAmax)
01378     {
01379       /* all macroblocks decoded */
01380       return;
01381     }
01382 read_cod:
01383 
01384     COD = showbits (1);
01385 
01386     if (!COD)
01387     {
01388       /* COD == 0 --> not skipped */
01389       coded_map[ypos + 1][xpos + 1] = 1;
01390 
01391       /* flush COD bit */
01392       flushbits (1);
01393      
01394       true_B_prediction_type = getMBTYPE (&true_B_cbp, &true_B_quant);
01395 
01396       if (fault)
01397         goto resync;
01398       
01399       if (B_EI_EP_STUFFING == true_B_prediction_type)
01400       {
01401         /* stuffing - read next COD without advancing MB count. */        
01402         goto read_cod;        
01403       } 
01404 
01405       if (B_INTRA_PREDICTION != true_B_prediction_type)
01406       {
01407         if (1 == true_B_quant)
01408         {
01409           Mode = MODE_INTER_Q;
01410         } 
01411         else
01412         {
01413           Mode = MODE_INTER;
01414         }
01415       }  
01416       else
01417       {
01418         if (1 == true_B_quant)
01419         {
01420           Mode = MODE_INTRA_Q;
01421         } 
01422         else
01423         {
01424           Mode = MODE_INTRA;
01425         }
01426       }    
01427       
01428       if (advanced_intra_coding && (B_INTRA_PREDICTION == true_B_prediction_type))
01429       {
01430         /* get INTRA_AC_DC mode for annex I */
01431         if (!showbits (1))
01432           INTRA_AC_DC = getbits (1);
01433         else
01434           INTRA_AC_DC = getbits (2);
01435 
01436       }
01437       if (1 == true_B_cbp)
01438       {
01439         CBPC = getscalabilityCBPC ();
01440         CBPY = getCBPY ();          
01441       
01442         /* Decode Mode and CBP */     
01443         if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
01444         {
01445           /* Intra */
01446 
01447           /* needed in huffman coding only */
01448           CBPY = CBPY ^ 15;   
01449         } 
01450         /* Annex S.3 change */
01451         else if (alternative_inter_VLC_mode && (CBPC == 3) )
01452           CBPY = CBPY ^ 15;     
01453       
01454         CBP = (CBPY << 2) | CBPC;
01455       }
01456       else
01457       {
01458         CBP = 0;
01459       }
01460 
01461       if (fault)
01462         goto resync;
01463 
01464       if (Mode == MODE_INTER_Q || Mode == MODE_INTRA_Q )
01465       {
01466         /* Read DQUANT if necessary */
01467         if (true_B_frame && true_B_quant)
01468         {
01469           if (!modified_quantization_mode)
01470           {
01471             DQUANT = getbits (2);
01472             quant += DQ_tab[DQUANT];
01473           } 
01474           else
01475           {
01476             tmp = getbits (1);
01477             if (tmp)
01478             {                   /* only one more additional bit was sent */
01479               tmp = getbits (1);
01480               if (tmp)
01481               {                 /* second bit of quant is 1 */
01482                 DQUANT = change_of_quant_tab_11[quant];
01483               } else
01484               {                 /* second bit of quant is 0 */
01485                 DQUANT = change_of_quant_tab_10[quant];
01486               }
01487               quant += DQUANT;
01488             } 
01489             else
01490             {                   /* five additional bits were sent as
01491                                  * DQUANT */
01492               DQUANT = getbits (5);
01493               quant = DQUANT;
01494             }
01495           }
01496         }
01497         if (quant > 31 || quant < 1)
01498         {
01499           quant = mmax (1, mmin (31, quant));
01500           /* could set fault-flag and resync here */
01501         }
01502       }
01503       
01504       /* motion vectors */
01505       if (Mode == MODE_INTER || Mode == MODE_INTER_Q )
01506       {
01507         switch (true_B_prediction_type)
01508         {
01509           case B_FORWARD_PREDICTION:
01510            
01511             if (plus_type && long_vectors)
01512             {
01513               mvfx = getRVLC ();
01514               mvfy = getRVLC ();
01515 
01516               /* flush start code emulation bit */
01517               if (mvfx == 1 && mvfy == 1)
01518                 flushbits(1);
01519             }
01520             else
01521             {
01522               mvfx = getTMNMV ();
01523               mvfy = getTMNMV ();
01524             }
01525 
01526             pmvf0 = find_pmv (xpos, ypos, 0, 0);
01527             pmvf1 = find_pmv (xpos, ypos, 0, 1);
01528 
01529             if (plus_type && long_vectors)
01530             {
01531               mvfx += pmvf0;
01532               mvfy += pmvf1;
01533             }
01534             else
01535             {
01536               mvfx = motion_decode (mvfx, pmvf0);
01537               mvfy = motion_decode (mvfy, pmvf1);
01538             }
01539             
01540            
01541             MV[0][0][ypos + 1][xpos + 1] = mvfx;
01542             MV[1][0][ypos + 1][xpos + 1] = mvfy;
01543 
01544             MV[0][5][ypos + 1][xpos + 1] = 0;
01545             MV[1][5][ypos + 1][xpos + 1] = 0;
01546 
01547             break;
01548 
01549           case B_BACKWARD_PREDICTION:
01550 
01551             if (plus_type && long_vectors)
01552             {
01553               mvbx = getRVLC ();
01554               mvby = getRVLC ();
01555 
01556               /* flush start code emulation bit */
01557               if (mvbx == 1 && mvby == 1)
01558                 flushbits(1);
01559             }
01560             else
01561             {
01562               mvbx = getTMNMV ();
01563               mvby = getTMNMV ();               
01564             }
01565 
01566             pmvb0 = find_pmv (xpos, ypos, 5, 0);
01567             pmvb1 = find_pmv (xpos, ypos, 5, 1);
01568 
01569             if (plus_type && long_vectors)
01570             {
01571               mvbx += pmvb0;
01572               mvby += pmvb1;
01573             }
01574             else
01575             {
01576               mvbx = motion_decode (mvbx, pmvb0);
01577               mvby = motion_decode (mvby, pmvb1);
01578             }
01579             
01580            
01581             MV[0][5][ypos + 1][xpos + 1] = mvbx;
01582             MV[1][5][ypos + 1][xpos + 1] = mvby;
01583 
01584             MV[0][0][ypos + 1][xpos + 1] = 0;
01585             MV[1][0][ypos + 1][xpos + 1] = 0;
01586 
01587             break;
01588 
01589           case B_BIDIRECTIONAL_PREDICTION:
01590 
01591             if (plus_type && long_vectors)
01592             {
01593               mvfx = getRVLC ();
01594               mvfy = getRVLC ();
01595 
01596               /* flush start code emulation bit */
01597               if (mvfx == 1 && mvfy == 1)
01598                 flushbits(1);
01599             }
01600             else
01601             {
01602               mvfx = getTMNMV ();
01603               mvfy = getTMNMV ();
01604             }
01605 
01606             pmvf0 = find_pmv (xpos, ypos, 0, 0);
01607             pmvf1 = find_pmv (xpos, ypos, 0, 1);
01608 
01609             if (plus_type && long_vectors)
01610             {
01611               mvfx += pmvf0;
01612               mvfy += pmvf1;
01613             }
01614             else
01615             {
01616               mvfx = motion_decode (mvfx, pmvf0);
01617               mvfy = motion_decode (mvfy, pmvf1);
01618             }
01619             
01620 
01621             if (plus_type && long_vectors)
01622             {
01623               mvbx = getRVLC ();
01624               mvby = getRVLC ();
01625 
01626               /* flush start code emulation bit */
01627               if (mvbx == 1 && mvby == 1)
01628                 flushbits(1);
01629             }
01630             else
01631             {
01632               mvbx = getTMNMV ();
01633               mvby = getTMNMV ();
01634             }
01635  
01636             pmvb0 = find_pmv (xpos, ypos, 5, 0);
01637             pmvb1 = find_pmv (xpos, ypos, 5, 1);
01638 
01639             if (plus_type && long_vectors)
01640             {
01641               mvbx += pmvb0;
01642               mvby += pmvb1;
01643             }
01644             else
01645             {
01646               mvbx = motion_decode (mvbx, pmvb0);
01647               mvby = motion_decode (mvby, pmvb1);
01648             }
01649             
01650               
01651             MV[0][0][ypos + 1][xpos + 1] = mvfx;
01652             MV[1][0][ypos + 1][xpos + 1] = mvfy;
01653 
01654             MV[0][5][ypos + 1][xpos + 1] = mvbx;
01655             MV[1][5][ypos + 1][xpos + 1] = mvby;
01656 
01657             break;
01658 
01659           default:
01660 
01661             /* No MV data for other modes. */
01662             MV[0][0][ypos + 1][xpos + 1] = 0;
01663             MV[1][0][ypos + 1][xpos + 1] = 0;
01664 
01665             MV[0][5][ypos + 1][xpos + 1] = 0;
01666             MV[1][5][ypos + 1][xpos + 1] = 0;
01667 
01668             break;
01669         }
01670       }
01671 
01672       if (fault)
01673         goto resync;
01674     } 
01675     else
01676     {
01677       /* COD == 1 --> skipped MB */
01678       if (MBA >= MBAmax)
01679       {
01680         /* all macroblocks decoded */
01681         return;
01682       }
01683       flushbits (1);
01684 
01685       Mode = MODE_INTER;
01686 
01687       /* Reset CBP */
01688       CBP = 0;
01689 
01690       coded_map[ypos + 1][xpos + 1] = 0;
01691 
01692       true_B_prediction_type = B_DIRECT_PREDICTION;
01693       
01694       /* reset motion vectors */
01695       MV[0][0][ypos + 1][xpos + 1] = 0;
01696       MV[1][0][ypos + 1][xpos + 1] = 0;
01697       MV[0][5][ypos + 1][xpos + 1] = 0;
01698       MV[1][5][ypos + 1][xpos + 1] = 0;
01699     } 
01700 
01701     /* Store mode and prediction type */
01702     modemap[ypos + 1][xpos + 1] = Mode;
01703     predictionmap[ypos + 1][xpos + 1] = true_B_prediction_type;
01704 
01705     if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
01706     {
01707       MV[0][0][ypos + 1][xpos + 1] = MV[1][0][ypos + 1][xpos + 1] = 0;
01708       MV[0][5][ypos + 1][xpos + 1] = MV[1][5][ypos + 1][xpos + 1] = 0;  
01709     }
01710 
01711 reconstruct_mb:
01712 
01713     /* pixel coordinates of top left corner of current macroblock */
01714     /* one delayed because of OBMC */
01715     if (xpos > 0)
01716     {
01717       bx = 16 * (xpos - 1);
01718       by = 16 * ypos;
01719     } 
01720     else
01721     {
01722       bx = coded_picture_width - 16;
01723       by = 16 * (ypos - 1);
01724     }
01725 
01726     if (MBA > 0)
01727     {
01728       Mode = modemap[by / 16 + 1][bx / 16 + 1];
01729       true_B_prediction_type = predictionmap[by / 16 + 1][bx / 16 + 1];
01730 
01731       /* motion compensation for true B frame. */
01732       if (Mode == MODE_INTER || Mode == MODE_INTER_Q)
01733         reconstruct_true_B (bx, by, true_B_prediction_type);
01734 
01735       /* copy or add block data into true B picture */
01736       for (comp = 0; comp < blk_cnt; comp++)
01737       {
01738         /* inverse DCT */
01739         if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
01740         {
01741           if (refidct)
01742             idctref (ld->block[comp]);
01743           else
01744             idct (ld->block[comp]);
01745           addblock (comp, bx, by, 0);
01746         } 
01747         else if ((pCBP & (1 << (blk_cnt - 1 - comp))))
01748         {
01749           /* No need to to do this for blocks with no coeffs */
01750           if (refidct)
01751             idctref (ld->block[comp]);
01752           else
01753             idct (ld->block[comp]);
01754           addblock (comp, bx, by, 1);
01755         }
01756       }  
01757     }    
01758     /* end if (MBA > 0) */
01759     if (!COD)
01760     {
01761       Mode = modemap[ypos + 1][xpos + 1];
01762       true_B_prediction_type = predictionmap[ypos + 1][xpos + 1];
01763 
01764       for (comp = 0; comp < blk_cnt; comp++)
01765       {
01766         clearblock (comp);
01767         if ((Mode == MODE_INTRA || Mode == MODE_INTRA_Q) && !(advanced_intra_coding))
01768         {
01769           /* Intra (except in advanced intra coding mode) */
01770           bp = ld->block[comp];
01771           bp[0] = getbits (8);
01772           if (bp[0] == 128)
01773             if (!quiet)
01774               fprintf (stderr, "Illegal DC-coeff: 1000000\n");
01775           if (bp[0] == 255)   /* Spec. in H.26P, not in TMN4 */
01776             bp[0] = 128;
01777           bp[0] *= 8;         /* Iquant */
01778 
01779           if ((CBP & (1 << (blk_cnt - 1 - comp))))
01780           {
01781             getblock (comp, 0, 0, Mode);
01782           }
01783         } 
01784         else
01785         {
01786           /* Inter (or Intra in advanced intra coding mode) */
01787           if ((CBP & (1 << (blk_cnt - 1 - comp))))
01788           {
01789             getblock (comp, 1, INTRA_AC_DC, Mode);
01790           }
01791         }
01792 
01793         if (fault)
01794           goto resync;
01795       }
01796     }
01797 
01798     /* advance to next macroblock */
01799     MBA++;
01800     pCBP = CBP;
01801     pCOD = COD;
01802     quant_map[ypos + 1][xpos + 1] = quant;
01803 
01804     fflush (stdout);
01805 
01806     if (MBA >= MBAmax && !last_done)
01807     {
01808       COD = 1;
01809       xpos = 0;
01810       ypos++;
01811       last_done = 1;
01812       goto reconstruct_mb;
01813     }
01814   }
01815 }
01816 
01817 
01818 /**********************************************************************
01819  *
01820  *      Name:                 get_EI_EP_MBs
01821  *      Description:  decode MBs for one EI or EP picture 
01822  *      Input:        frame number
01823  *      Returns:      
01824  *      Side effects: 
01825  *
01826  *  Date: 971102  Author: mikeg@ee.ubc.ca
01827  *
01828  ***********************************************************************/
01829  static void get_EI_EP_MBs (int framenum)
01830 {
01831   int comp;
01832   int MBA, MBAmax;
01833   int bx, by;
01834   int COD = 0, CBP = 0, Mode = 0, DQUANT;
01835   int xpos, ypos, gob, i;
01836   int mvfx = 0, mvfy = 0, pmvf0, pmvf1;
01837   int gfid, gobheader_read;
01838   int last_done = 0, pCBP = 0, pCOD = 0;
01839   int DQ_tab[4] = {-1, -2, 1, 2};
01840   short *bp;
01841   int ei_ep_cbp = 0, ei_ep_quant = 0, ei_ep_prediction_type;
01842   int CBPC = 0, CBPY = 0, tmp = 0;
01843 
01844   /* variables used in advanced intra coding mode */
01845   int INTRA_AC_DC = 0;
01846 
01847   MBAmax = mb_width * mb_height;
01848 
01849   MBA = 0;                      /* macroblock address */
01850   newgob = 0;
01851 
01852   /* mark MV's above the picture */
01853   for (i = 1; i < mb_width + 1; i++)
01854   {
01855     MV[0][0][0][i] = NO_VEC;
01856     MV[1][0][0][i] = NO_VEC;
01857     modemap[0][i] = MODE_INTRA;
01858     predictionmap[0][i] = EI_EP_INTRA_PREDICTION;
01859   }
01860   /* zero MV's on the sides of the picture */
01861   for (i = 0; i < mb_height + 1; i++)
01862   {
01863     MV[0][0][i][0] = 0;
01864     MV[1][0][i][0] = 0;    
01865     MV[0][0][i][mb_width + 1] = 0;    
01866     MV[1][0][i][mb_width + 1] = 0;
01867     modemap[i][0] = MODE_INTRA;
01868     modemap[i][mb_width + 1] = MODE_INTRA;
01869     predictionmap[i][0] = EI_EP_INTRA_PREDICTION;
01870     predictionmap[i][mb_width + 1] = EI_EP_INTRA_PREDICTION;
01871   }
01872 
01873   fault = 0;
01874   gobheader_read = 0;
01875 
01876   for (;;)
01877   {
01878 
01879 
01880 resync:
01881     /* This version of the decoder does not resync on every possible
01882      * error, and it does not do all possible error checks. It is not
01883      * difficult to make it much more error robust, but I do not think it
01884      * is necessary to include this in the freely available version. */
01885    
01886     if (fault)
01887     {
01888       startcode ();             /* sync on new startcode */
01889       fault = 0;
01890     }
01891     if (!(showbits (22) >> 6))
01892     {
01893       /* startcode */
01894       startcode ();
01895 
01896       /* in case of byte aligned start code, ie. PSTUF, GSTUF or ESTUF is
01897        * used */
01898       if (showbits (22) == (32 | SE_CODE))
01899       {
01900         /* end of sequence */
01901         if (!(MBA < MBAmax))
01902         {
01903           return;
01904         }
01905       } 
01906       else if ((showbits (22) == PSC << 5))
01907       {
01908         /* new picture */
01909         if (!(MBA < MBAmax))
01910         {
01911           return;
01912         }
01913       } 
01914       else
01915       {
01916         if (!(MBA % mb_width))
01917         {
01918           gob = getheader () - 1;
01919           if (gob > mb_height)
01920           {
01921             return;
01922           }
01923           /* GFID is not allowed to change unless PTYPE in picture header
01924            * changes */
01925           gfid = getbits (2);
01926           /* NB: in error-prone environments the decoder can use this
01927            * value to determine whether a picture header where the PTYPE
01928            * has changed, has been lost */
01929 
01930           quant = getbits (5);
01931           xpos = 0;
01932           ypos = gob;
01933           MBA = ypos * mb_width;
01934 
01935           newgob = 1;
01936           gobheader_read = 1;
01937         }
01938       }
01939     }
01940 
01941     /* SAC specific label */
01942     if (!gobheader_read)
01943     {
01944       xpos = MBA % mb_width;
01945       ypos = MBA / mb_width;
01946       if (xpos == 0 && ypos > 0)
01947         newgob = 0;
01948     } 
01949     else
01950       gobheader_read = 0;
01951 
01952     if (MBA >= MBAmax)
01953     {
01954       /* all macroblocks decoded */
01955       return;
01956     }
01957 read_cod:
01958 
01959     COD = showbits (1);
01960 
01961     if (!COD)
01962     {
01963       /* COD == 0 --> not skipped */
01964       coded_map[ypos + 1][xpos + 1] = 1;
01965 
01966       /* flush COD bit */
01967       flushbits (1);
01968      
01969       ei_ep_prediction_type = getMBTYPE (&ei_ep_cbp, &ei_ep_quant);
01970 
01971       if (fault)
01972         goto resync;
01973       
01974       if (B_EI_EP_STUFFING == ei_ep_prediction_type)
01975       {
01976         /* stuffing - read next COD without advancing MB count. */        
01977         goto read_cod;        
01978       } 
01979       
01980       if (EI_EP_INTRA_PREDICTION != ei_ep_prediction_type)
01981       {
01982         if (1 == ei_ep_quant)
01983         {
01984           Mode = MODE_INTER_Q;
01985         } 
01986         else
01987         {
01988           Mode = MODE_INTER;
01989         }
01990       }  
01991       else
01992       {
01993         if (1 == ei_ep_quant)
01994         {
01995           Mode = MODE_INTRA_Q;
01996         } 
01997         else
01998         {
01999           Mode = MODE_INTRA;
02000         }
02001       }
02002       
02003       if (advanced_intra_coding && (EI_EP_INTRA_PREDICTION == ei_ep_prediction_type))
02004       {
02005         /* get INTRA_AC_DC mode for annex I */
02006         if (!showbits (1))
02007           INTRA_AC_DC = getbits (1);
02008         else
02009           INTRA_AC_DC = getbits (2);
02010 
02011       }
02012       if (1 == ei_ep_cbp || PCT_EI == pict_type )
02013       {
02014         if (PCT_EP == pict_type)
02015           CBPC = getscalabilityCBPC ();
02016         else
02017           CBPC = ei_ep_cbp;
02018 
02019         CBPY = getCBPY ();          
02020 
02021         /* Decode Mode and CBP */     
02022         if ( (MODE_INTRA == Mode || MODE_INTRA_Q == Mode) ||
02023              (EI_EP_UPWARD_PREDICTION == ei_ep_prediction_type) ||
02024              (PCT_EP == pict_type &&  
02025               EP_BIDIRECTIONAL_PREDICTION == ei_ep_prediction_type) ) 
02026         {
02027           /* needed in huffman coding only */
02028           CBPY = CBPY ^ 15;   
02029         } 
02030         /* Annex S.3 change */
02031         else if (alternative_inter_VLC_mode && (CBPC == 3) )
02032           CBPY = CBPY ^ 15;     
02033       
02034         CBP = (CBPY << 2) | CBPC;
02035       }
02036       else
02037       {
02038         CBP = 0;
02039       }
02040       
02041       if (fault)
02042         goto resync;
02043 
02044       if (Mode == MODE_INTER_Q || Mode == MODE_INTRA_Q )
02045       {
02046         /* Read DQUANT if necessary */
02047         if (ei_ep_quant)
02048         {
02049           if (!modified_quantization_mode)
02050           {
02051             DQUANT = getbits (2);
02052             quant += DQ_tab[DQUANT];
02053           } 
02054           else
02055           {
02056             tmp = getbits (1);
02057             if (tmp)
02058             {                   /* only one more additional bit was sent */
02059               tmp = getbits (1);
02060               if (tmp)
02061               {                 /* second bit of quant is 1 */
02062                 DQUANT = change_of_quant_tab_11[quant];
02063               } else
02064               {                 /* second bit of quant is 0 */
02065                 DQUANT = change_of_quant_tab_10[quant];
02066               }
02067               quant += DQUANT;
02068             } 
02069             else
02070             {                   /* five additional bits were sent as
02071                                  * DQUANT */
02072               DQUANT = getbits (5);
02073               quant = DQUANT;
02074             }
02075           }
02076         }
02077         if (quant > 31 || quant < 1)
02078         {
02079           quant = mmax (1, mmin (31, quant));
02080           /* could set fault-flag and resync here */
02081         }
02082       }
02083       
02084       /* motion vectors */
02085       if (Mode == MODE_INTER || Mode == MODE_INTER_Q )
02086       {
02087         switch (ei_ep_prediction_type)
02088         {
02089           case EP_FORWARD_PREDICTION:
02090              
02091             if (plus_type && long_vectors)
02092             {
02093               mvfx = getRVLC ();
02094               mvfy = getRVLC ();
02095 
02096               /* flush start code emulation bit */
02097               if (mvfx == 1 && mvfy == 1)
02098                 flushbits(1);
02099             }
02100             else
02101             {
02102               mvfx = getTMNMV ();
02103               mvfy = getTMNMV ();
02104             }
02105 
02106             pmvf0 = find_pmv (xpos, ypos, 0, 0);
02107             pmvf1 = find_pmv (xpos, ypos, 0, 1);
02108 
02109             if (plus_type && long_vectors)
02110             {
02111               mvfx += pmvf0;
02112               mvfy += pmvf1;
02113             }
02114             else
02115             {
02116               mvfx = motion_decode (mvfx, pmvf0);
02117               mvfy = motion_decode (mvfy, pmvf1);
02118             }
02119             
02120           
02121             MV[0][0][ypos + 1][xpos + 1] = mvfx;
02122             MV[1][0][ypos + 1][xpos + 1] = mvfy;
02123 
02124             break;
02125 
02126           case EP_BIDIRECTIONAL_PREDICTION:
02127 
02128             /* No MVs for Bi-Dir (no texture) MBTYPE */
02129             if ( (0 == ei_ep_cbp) && (0 == ei_ep_quant) )
02130             {
02131               MV[0][0][ypos + 1][xpos + 1] = 0;
02132               MV[1][0][ypos + 1][xpos + 1] = 0;
02133               break;
02134             }
02135 
02136             if (plus_type && long_vectors)
02137             {
02138               mvfx = getRVLC ();
02139               mvfy = getRVLC ();
02140 
02141               /* flush start code emulation bit */
02142               if (mvfx == 1 && mvfy == 1)
02143                 flushbits(1);
02144             }
02145             else
02146             {
02147               mvfx = getTMNMV ();
02148               mvfy = getTMNMV ();
02149             }
02150 
02151             pmvf0 = find_pmv (xpos, ypos, 0, 0);
02152             pmvf1 = find_pmv (xpos, ypos, 0, 1);
02153 
02154             if (plus_type && long_vectors)
02155             {
02156               mvfx += pmvf0;
02157               mvfy += pmvf1;
02158             }
02159             else
02160             {
02161               mvfx = motion_decode (mvfx, pmvf0);
02162               mvfy = motion_decode (mvfy, pmvf1);
02163             }
02164             
02165               
02166             MV[0][0][ypos + 1][xpos + 1] = mvfx;
02167             MV[1][0][ypos + 1][xpos + 1] = mvfy;
02168 
02169             break;
02170 
02171           case EI_EP_UPWARD_PREDICTION:
02172           default:
02173 
02174             /* No MV data for other modes. */
02175             MV[0][0][ypos + 1][xpos + 1] = 0;
02176             MV[1][0][ypos + 1][xpos + 1] = 0;
02177 
02178             break;
02179         }
02180       }
02181 
02182       if (fault)
02183         goto resync;
02184     } 
02185     else
02186     {
02187       /* COD == 1 --> skipped MB */
02188       if (MBA >= MBAmax)
02189       {
02190         /* all macroblocks decoded */
02191         return;
02192       }
02193       flushbits (1);
02194 
02195       Mode = MODE_INTER;
02196 
02197       /* Reset CBP */
02198       CBP = 0;
02199 
02200       coded_map[ypos + 1][xpos + 1] = 0;
02201 
02202       if (PCT_EI == pict_type)
02203       {
02204         ei_ep_prediction_type = EI_EP_UPWARD_PREDICTION;
02205       }
02206       else
02207       {
02208         ei_ep_prediction_type = EP_FORWARD_PREDICTION;
02209       }
02210       
02211       /* reset motion vectors */
02212       MV[0][0][ypos + 1][xpos + 1] = 0;
02213       MV[1][0][ypos + 1][xpos + 1] = 0;
02214     } 
02215 
02216     /* Store mode and prediction type */
02217     modemap[ypos + 1][xpos + 1] = Mode;
02218     predictionmap[ypos + 1][xpos + 1] = ei_ep_prediction_type;
02219 
02220     if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
02221     {
02222       MV[0][0][ypos + 1][xpos + 1] = MV[1][0][ypos + 1][xpos + 1] = 0;
02223     }
02224 
02225 reconstruct_mb:
02226 
02227     /* pixel coordinates of top left corner of current macroblock */
02228     /* one delayed because of OBMC */
02229     if (xpos > 0)
02230     {
02231       bx = 16 * (xpos - 1);
02232       by = 16 * ypos;
02233     } else
02234     {
02235       bx = coded_picture_width - 16;
02236       by = 16 * (ypos - 1);
02237     }
02238 
02239     if (MBA > 0)
02240     {
02241       Mode = modemap[by / 16 + 1][bx / 16 + 1];
02242       ei_ep_prediction_type = predictionmap[by / 16 + 1][bx / 16 + 1];
02243 
02244       /* motion compensation for true B frame. */
02245       if (Mode == MODE_INTER || Mode == MODE_INTER_Q)
02246         reconstruct_ei_ep (bx, by, ei_ep_prediction_type);
02247 
02248       /* copy or add block data into true B picture */
02249       for (comp = 0; comp < blk_cnt; comp++)
02250       {
02251         /* inverse DCT */
02252         if (Mode == MODE_INTRA || Mode == MODE_INTRA_Q)
02253         {
02254           if (refidct)
02255             idctref (ld->block[comp]);
02256           else
02257             idct (ld->block[comp]);
02258           addblock (comp, bx, by, 0);
02259         } 
02260         else if ((pCBP & (1 << (blk_cnt - 1 - comp))))
02261         {
02262           /* No need to to do this for blocks with no coeffs */
02263           if (refidct)
02264             idctref (ld->block[comp]);
02265           else
02266             idct (ld->block[comp]);
02267           addblock (comp, bx, by, 1);
02268         }
02269       }  
02270     }    
02271     /* end if (MBA > 0) */
02272     if (!COD)
02273     {
02274       Mode = modemap[ypos + 1][xpos + 1];
02275       ei_ep_prediction_type = predictionmap[ypos + 1][xpos + 1];
02276 
02277       for (comp = 0; comp < blk_cnt; comp++)
02278       {
02279         clearblock (comp);
02280         if ((Mode == MODE_INTRA || Mode == MODE_INTRA_Q) && !(advanced_intra_coding))
02281         {
02282           /* Intra (except in advanced intra coding mode) */
02283           bp = ld->block[comp];
02284           bp[0] = getbits (8);
02285           if (bp[0] == 128)
02286             if (!quiet)
02287               fprintf (stderr, "Illegal DC-coeff: 1000000\n");
02288           if (bp[0] == 255)   /* Spec. in H.26P, not in TMN4 */
02289             bp[0] = 128;
02290           bp[0] *= 8;         /* Iquant */
02291 
02292           if ((CBP & (1 << (blk_cnt - 1 - comp))))
02293           {
02294             getblock (comp, 0, 0, Mode);
02295           }
02296         } 
02297         else
02298         {
02299           /* Inter (or Intra in advanced intra coding mode) */
02300           if ((CBP & (1 << (blk_cnt - 1 - comp))))
02301           {
02302             getblock (comp, 1, INTRA_AC_DC, Mode);
02303           }
02304         }
02305         if (fault)
02306           goto resync;
02307       }
02308     }
02309 
02310     /* advance to next macroblock */
02311     MBA++;
02312     pCBP = CBP;
02313     pCOD = COD;
02314     quant_map[ypos + 1][xpos + 1] = quant;
02315 
02316     fflush (stdout);
02317 
02318     if (MBA >= MBAmax && !last_done)
02319     {
02320       COD = 1;
02321       xpos = 0;
02322       ypos++;
02323       last_done = 1;
02324       goto reconstruct_mb;
02325     }
02326   }
02327 }
02328 /* set block to zero */
02329 
02330 static void clearblock (int comp)
02331 {
02332   int *bp;
02333   int i;
02334 
02335   bp = (int *) ld->block[comp];
02336 
02337   for (i = 0; i < 8; i++)
02338   {
02339     bp[0] = bp[1] = bp[2] = bp[3] = 0;
02340     bp += 4;
02341   }
02342 }
02343 
02344 
02345 /* move/add 8x8-Block from block[comp] to refframe or bframe */
02346 
02347 static void addblock (int comp, int bx, int by, int addflag)
02348 {
02349   int cc, i, iincr, P = 1;
02350   unsigned char *rfp;
02351   short *bp;
02352   unsigned char *curr[3];
02353 
02354   if (enhancement_layer_num > 1)
02355   {
02356     curr[0] = current_enhancement_frame[enhancement_layer_num-2][0];
02357     curr[1] = current_enhancement_frame[enhancement_layer_num-2][1];
02358     curr[2] = current_enhancement_frame[enhancement_layer_num-2][2];
02359   }
02360   else
02361   {
02362     curr[0] = current_frame[0];
02363     curr[1] = current_frame[1];
02364     curr[2] = current_frame[2];
02365   }
02366 
02367 
02368   bp = ld->block[comp];
02369 
02370   if (comp >= 6)
02371   {
02372     /* This is a component for B-frame forward prediction */
02373     P = 0;
02374     addflag = 1;
02375     comp -= 6;
02376   }
02377   cc = (comp < 4) ? 0 : (comp & 1) + 1; /* color component index */
02378 
02379   if (cc == 0)
02380   {
02381     /* luminance */
02382 
02383     /* frame DCT coding */
02384     if (P)
02385       rfp = curr[0]
02386         + coded_picture_width * (by + ((comp & 2) << 2)) + bx + ((comp & 1) << 3);
02387     else  
02388       rfp = bframe[0]
02389         + coded_picture_width * (by + ((comp & 2) << 2)) + bx + ((comp & 1) << 3);
02390     iincr = coded_picture_width;
02391   } 
02392   else
02393   {
02394     /* chrominance */
02395 
02396     /* scale coordinates */
02397     bx >>= 1;
02398     by >>= 1;
02399     /* frame DCT coding */
02400     if (P)
02401       rfp = curr[cc] + chrom_width * by + bx;
02402     else
02403       rfp = bframe[cc] + chrom_width * by + bx;
02404     iincr = chrom_width;
02405   }
02406 
02407 
02408   if (addflag)
02409   {
02410     for (i = 0; i < 8; i++)
02411     {
02412       rfp[0] = clp[bp[0] + rfp[0]];
02413       rfp[1] = clp[bp[1] + rfp[1]];
02414       rfp[2] = clp[bp[2] + rfp[2]];
02415       rfp[3] = clp[bp[3] + rfp[3]];
02416       rfp[4] = clp[bp[4] + rfp[4]];
02417       rfp[5] = clp[bp[5] + rfp[5]];
02418       rfp[6] = clp[bp[6] + rfp[6]];
02419       rfp[7] = clp[bp[7] + rfp[7]];
02420       bp += 8;
02421       rfp += iincr;
02422     }
02423   } else
02424   {
02425     for (i = 0; i < 8; i++)
02426     {
02427       rfp[0] = clp[bp[0]];
02428       rfp[1] = clp[bp[1]];
02429       rfp[2] = clp[bp[2]];
02430       rfp[3] = clp[bp[3]];
02431       rfp[4] = clp[bp[4]];
02432       rfp[5] = clp[bp[5]];
02433       rfp[6] = clp[bp[6]];
02434       rfp[7] = clp[bp[7]];
02435       bp += 8;
02436       rfp += iincr;
02437     }
02438   }
02439 }
02440 
02441 /* bidirectionally reconstruct 8x8-Block from block[comp] to bframe */
02442 
02443 static void reconblock_b (int comp, int bx, int by, int mode, int bdx, int bdy)
02444 {
02445   int cc, i, j, k, ii;
02446   unsigned char *bfr, *ffr;
02447   int BMVx, BMVy;
02448   int xa, xb, ya, yb, x, y, xvec, yvec, mvx, mvy;
02449   int xint, xhalf, yint, yhalf, pel;
02450 
02451   x = bx / 16 + 1;
02452   y = by / 16 + 1;
02453 
02454   if (mode == MODE_INTER4V || mode == MODE_INTER4V_Q)
02455   {
02456     if (comp < 4)
02457     {
02458       /* luma */
02459       mvx = MV[0][comp + 1][y][x];
02460       mvy = MV[1][comp + 1][y][x];
02461       BMVx = (bdx == 0 ? (trb - trd) * mvx / trd : trb * mvx / trd + bdx - mvx);
02462       BMVy = (bdy == 0 ? (trb - trd) * mvy / trd : trb * mvy / trd + bdy - mvy);
02463     } else
02464     {
02465       /* chroma */
02466       xvec = yvec = 0;
02467       for (k = 1; k <= 4; k++)
02468       {
02469         mvx = MV[0][k][y][x];
02470         mvy = MV[1][k][y][x];
02471         xvec += (bdx == 0 ? (trb - trd) * mvx / trd : trb * mvx / trd + bdx - mvx);
02472         yvec += (bdy == 0 ? (trb - trd) * mvy / trd : trb * mvy / trd + bdy - mvy);
02473       }
02474 
02475       /* chroma rounding (table 16/H.263) */
02476       BMVx = sign (xvec) * (roundtab[abs (xvec) % 16] + (abs (xvec) / 16) * 2);
02477       BMVy = sign (yvec) * (roundtab[abs (yvec) % 16] + (abs (yvec) / 16) * 2);
02478     }
02479   } else
02480   {
02481     if (comp < 4)
02482     {
02483       /* luma */
02484       mvx = MV[0][0][y][x];
02485       mvy = MV[1][0][y][x];
02486       BMVx = (bdx == 0 ? (trb - trd) * mvx / trd : trb * mvx / trd + bdx - mvx);
02487       BMVy = (bdy == 0 ? (trb - trd) * mvy / trd : trb * mvy / trd + bdy - mvy);
02488     } else
02489     {
02490       /* chroma */
02491       mvx = MV[0][0][y][x];
02492       mvy = MV[1][0][y][x];
02493       xvec = (bdx == 0 ? (trb - trd) * mvx / trd : trb * mvx / trd + bdx - mvx);
02494       yvec = (bdy == 0 ? (trb - trd) * mvy / trd : trb * mvy / trd + bdy - mvy);
02495       xvec *= 4;
02496       yvec *= 4;
02497 
02498       /* chroma rounding (table 16/H.263) */
02499       BMVx = sign (xvec) * (roundtab[abs (xvec) % 16] + (abs (xvec) / 16) * 2);
02500       BMVy = sign (yvec) * (roundtab[abs (yvec) % 16] + (abs (yvec) / 16) * 2);
02501     }
02502   }
02503 
02504   cc = (comp < 4) ? 0 : (comp & 1) + 1; /* color component index */
02505 
02506   if (cc == 0)
02507   {
02508     /* luminance */
02509     find_bidir_limits (BMVx, &xa, &xb, comp & 1);
02510     find_bidir_limits (BMVy, &ya, &yb, (comp & 2) >> 1);
02511     bfr = bframe[0] +
02512       coded_picture_width * (by + ((comp & 2) << 2)) + bx + ((comp & 1) << 3);
02513     ffr = current_frame[0] +
02514       coded_picture_width * (by + ((comp & 2) << 2)) + bx + ((comp & 1) << 3);
02515     ii = coded_picture_width;
02516   } else
02517   {
02518     /* chrominance */
02519     /* scale coordinates and vectors */
02520     bx >>= 1;
02521     by >>= 1;
02522 
02523     find_bidir_chroma_limits (BMVx, &xa, &xb);
02524     find_bidir_chroma_limits (BMVy, &ya, &yb);
02525 
02526     bfr = bframe[cc] + chrom_width * (by + ((comp & 2) << 2)) + bx + (comp & 8);
02527     ffr = current_frame[cc] + chrom_width * (by + ((comp & 2) << 2)) + bx + (comp & 8);
02528     ii = chrom_width;
02529   }
02530 
02531   xint = BMVx >> 1;
02532   xhalf = BMVx - 2 * xint;
02533   yint = BMVy >> 1;
02534   yhalf = BMVy - 2 * yint;
02535 
02536   ffr += xint + (yint + ya) * ii;
02537   bfr += ya * ii;
02538 
02539   if (!xhalf && !yhalf)
02540   {
02541     for (j = ya; j < yb; j++)
02542     {
02543       for (i = xa; i < xb; i++)
02544       {
02545         pel = ffr[i];
02546         bfr[i] = ((unsigned int) (pel + bfr[i])) >> 1;
02547       }
02548       bfr += ii;
02549       ffr += ii;
02550     }
02551   } else if (xhalf && !yhalf)
02552   {
02553     for (j = ya; j < yb; j++)
02554     {
02555       for (i = xa; i < xb; i++)
02556       {
02557         pel = ((unsigned int) (ffr[i] + ffr[i + 1] + 1)) >> 1;
02558         bfr[i] = ((unsigned int) (pel + bfr[i])) >> 1;
02559       }
02560       bfr += ii;
02561       ffr += ii;
02562     }
02563   } else if (!xhalf && yhalf)
02564   {
02565     for (j = ya; j < yb; j++)
02566     {
02567       for (i = xa; i < xb; i++)
02568       {
02569         pel = ((unsigned int) (ffr[i] + ffr[ii + i] + 1)) >> 1;
02570         bfr[i] = ((unsigned int) (pel + bfr[i])) >> 1;
02571       }
02572       bfr += ii;
02573       ffr += ii;
02574     }
02575   } else
02576   {                             /* if (xhalf && yhalf) */
02577     for (j = ya; j < yb; j++)
02578     {
02579       for (i = xa; i < xb; i++)
02580       {
02581         pel = ((unsigned int) (ffr[i] + ffr[i + 1] + ffr[ii + i] + ffr[ii + i + 1] + 2)) >> 2;
02582         bfr[i] = ((unsigned int) (pel + bfr[i])) >> 1;
02583       }
02584       bfr += ii;
02585       ffr += ii;
02586     }
02587   }
02588   return;
02589 }
02590 
02591 int motion_decode (int vec, int pmv)
02592 {
02593   if (vec > 31)
02594     vec -= 64;
02595   vec += pmv;
02596   if (!long_vectors)
02597   {
02598     if (vec > 31)
02599       vec -= 64;
02600     if (vec < -32)
02601       vec += 64;
02602   } else
02603   {
02604     if (pmv < -31 && vec < -63)
02605       vec += 64;
02606     if (pmv > 32 && vec > 63)
02607       vec -= 64;
02608   }
02609   return vec;
02610 }
02611 
02612 
02613 int find_pmv (int x, int y, int block, int comp)
02614 {
02615   int p1, p2, p3;
02616   int xin1, xin2, xin3;
02617   int yin1, yin2, yin3;
02618   int vec1, vec2, vec3;
02619   int l8, o8, or8;
02620 
02621   x++;
02622   y++;
02623 
02624   l8 = (modemap[y][x - 1] == MODE_INTER4V ? 1 : 0);
02625   l8 = (modemap[y][x - 1] == MODE_INTER4V_Q ? 1 : l8);
02626 
02627   o8 = (modemap[y - 1][x] == MODE_INTER4V ? 1 : 0);
02628   o8 = (modemap[y - 1][x] == MODE_INTER4V_Q ? 1 : o8);
02629 
02630   or8 = (modemap[y - 1][x + 1] == MODE_INTER4V ? 1 : 0);
02631   or8 = (modemap[y - 1][x + 1] == MODE_INTER4V_Q ? 1 : or8);
02632 
02633 
02634   switch (block)
02635   {
02636     case 0:
02637       vec1 = (l8 ? 2 : 0);
02638       yin1 = y;
02639       xin1 = x - 1;
02640       vec2 = (o8 ? 3 : 0);
02641       yin2 = y - 1;
02642       xin2 = x;
02643       vec3 = (or8 ? 3 : 0);
02644       yin3 = y - 1;
02645       xin3 = x + 1;
02646       break;
02647     case 1:
02648       vec1 = (l8 ? 2 : 0);
02649       yin1 = y;
02650       xin1 = x - 1;
02651       vec2 = (o8 ? 3 : 0);
02652       yin2 = y - 1;
02653       xin2 = x;
02654       vec3 = (or8 ? 3 : 0);
02655       yin3 = y - 1;
02656       xin3 = x + 1;
02657       break;
02658     case 2:
02659       vec1 = 1;
02660       yin1 = y;
02661       xin1 = x;
02662       vec2 = (o8 ? 4 : 0);
02663       yin2 = y - 1;
02664       xin2 = x;
02665       vec3 = (or8 ? 3 : 0);
02666       yin3 = y - 1;
02667       xin3 = x + 1;
02668       break;
02669     case 3:
02670       vec1 = (l8 ? 4 : 0);
02671       yin1 = y;
02672       xin1 = x - 1;
02673       vec2 = 1;
02674       yin2 = y;
02675       xin2 = x;
02676       vec3 = 2;
02677       yin3 = y;
02678       xin3 = x;
02679       break;
02680     case 4:
02681       vec1 = 3;
02682       yin1 = y;
02683       xin1 = x;
02684       vec2 = 1;
02685       yin2 = y;
02686       xin2 = x;
02687       vec3 = 2;
02688       yin3 = y;
02689       xin3 = x;
02690       break;
02691     case 5:
02692       vec1 = 5;
02693       yin1 = y;
02694       xin1 = x - 1;
02695       vec2 = 5;
02696       yin2 = y - 1;
02697       xin2 = x;
02698       vec3 = 5;
02699       yin3 = y - 1;
02700       xin3 = x + 1;
02701       break;
02702     default:
02703       exit (1);
02704       break;
02705   }
02706   p1 = MV[comp][vec1][yin1][xin1];
02707   p2 = MV[comp][vec2][yin2][xin2];
02708   p3 = MV[comp][vec3][yin3][xin3];
02709 
02710   if (newgob && (block == 0 || block == 1 || block == 2))
02711     p2 = NO_VEC;
02712 
02713   if (p2 == NO_VEC)
02714   {
02715     p2 = p3 = p1;
02716   }
02717   return p1 + p2 + p3 - mmax (p1, mmax (p2, p3)) - mmin (p1, mmin (p2, p3));
02718 }
02719 
02720 
02721 
02722 void find_bidir_limits (int vec, int *start, int *stop, int nhv)
02723 {
02724   /* limits taken from C loop in section G5 in H.263 */
02725   *start = mmax (0, (-vec + 1) / 2 - nhv * 8);
02726   *stop = mmin (7, 15 - (vec + 1) / 2 - nhv * 8);
02727 
02728   (*stop)++;                    /* I use < and not <= in the loop */
02729 }
02730 
02731 void find_bidir_chroma_limits (int vec, int *start, int *stop)
02732 {
02733 
02734   /* limits taken from C loop in section G5 in H.263 */
02735   *start = mmax (0, (-vec + 1) / 2);
02736   *stop = mmin (7, 7 - (vec + 1) / 2);
02737 
02738   (*stop)++;                    /* I use < and not <= in the loop */
02739   return;
02740 }
02741 
02742 void make_edge_image (unsigned char *src, unsigned char *dst, 
02743                       int width, int height, int edge)
02744 {
02745   int i, j;
02746   unsigned char *p1, *p2, *p3, *p4;
02747   unsigned char *o1, *o2, *o3, *o4;
02748 
02749   /* center image */
02750   p1 = dst;
02751   o1 = src;
02752   for (j = 0; j < height; j++)
02753   {
02754     for (i = 0; i < width; i++)
02755     {
02756       *(p1 + i) = *(o1 + i);
02757     }
02758     p1 += width + (edge << 1);
02759     o1 += width;
02760   }
02761 
02762   /* left and right edges */
02763   p1 = dst - 1;
02764   o1 = src;
02765   for (j = 0; j < height; j++)
02766   {
02767     for (i = 0; i < edge; i++)
02768     {
02769       *(p1 - i) = *o1;
02770       *(p1 + width + i + 1) = *(o1 + width - 1);
02771     }
02772     p1 += width + (edge << 1);
02773     o1 += width;
02774   }
02775 
02776   /* top and bottom edges */
02777   p1 = dst;
02778   p2 = dst + (width + (edge << 1)) * (height - 1);
02779   o1 = src;
02780   o2 = src + width * (height - 1);
02781   for (j = 0; j < edge; j++)
02782   {
02783     p1 = p1 - (width + (edge << 1));
02784     p2 = p2 + (width + (edge << 1));
02785     for (i = 0; i < width; i++)
02786     {
02787       *(p1 + i) = *(o1 + i);
02788       *(p2 + i) = *(o2 + i);
02789     }
02790   }
02791 
02792   /* corners */
02793   p1 = dst - (width + (edge << 1)) - 1;
02794   p2 = p1 + width + 1;
02795   p3 = dst + (width + (edge << 1)) * (height) - 1;
02796   p4 = p3 + width + 1;
02797 
02798   o1 = src;
02799   o2 = o1 + width - 1;
02800   o3 = src + width * (height - 1);
02801   o4 = o3 + width - 1;
02802   for (j = 0; j < edge; j++)
02803   {
02804     for (i = 0; i < edge; i++)
02805     {
02806       *(p1 - i) = *o1;
02807       *(p2 + i) = *o2;
02808       *(p3 - i) = *o3;
02809       *(p4 + i) = *o4;
02810     }
02811     p1 = p1 - (width + (edge << 1));
02812     p2 = p2 - (width + (edge << 1));
02813     p3 = p3 + width + (edge << 1);
02814     p4 = p4 + width + (edge << 1);
02815   }
02816 
02817 }
02818 
02819 
02820 void interpolate_image (unsigned char *in, unsigned char *out, int width, int height)
02821 /* only used for displayed interpolated frames, not reconstructed ones */
02822 {
02823 
02824   int x, xx, y, w2;
02825 
02826   unsigned char *pp, *ii;
02827 
02828   w2 = 2 * width;
02829 
02830   /* Horizontally */
02831   pp = out;
02832   ii = in;
02833   for (y = 0; y < height - 1; y++)
02834   {
02835     for (x = 0, xx = 0; x < width - 1; x++, xx += 2)
02836     {
02837       *(pp + xx) = *(ii + x);
02838       *(pp + xx + 1) = ((unsigned int) (*(ii + x) + *(ii + x + 1)) ) >> 1;
02839       *(pp + w2 + xx) = ((unsigned int) (*(ii + x) + *(ii + x + width))) >> 1;
02840       *(pp + w2 + xx + 1) = ((unsigned int) (*(ii + x) + *(ii + x + 1) +
02841                 *(ii + x + width) + *(ii + x + width + 1))) >> 2;
02842 
02843     }
02844     *(pp + w2 - 2) = *(ii + width - 1);
02845     *(pp + w2 - 1) = *(ii + width - 1);
02846     *(pp + w2 + w2 - 2) = *(ii + width + width - 1);
02847     *(pp + w2 + w2 - 1) = *(ii + width + width - 1);
02848     pp += w2 << 1;
02849     ii += width;
02850   }
02851 
02852   /* last lines */
02853   for (x = 0, xx = 0; x < width - 1; x++, xx += 2)
02854   {
02855     *(pp + xx) = *(ii + x);
02856     *(pp + xx + 1) = ((unsigned int) (*(ii + x) + *(ii + x + 1) + 1 )) >> 1;
02857     *(pp + w2 + xx) = *(ii + x);
02858     *(pp + w2 + xx + 1) = ((unsigned int) (*(ii + x) + *(ii + x + 1) + 1 )) >> 1;
02859   }
02860 
02861   /* bottom right corner pels */
02862   *(pp + (width << 1) - 2) = *(ii + width - 1);
02863   *(pp + (width << 1) - 1) = *(ii + width - 1);
02864   *(pp + (width << 2) - 2) = *(ii + width - 1);
02865   *(pp + (width << 2) - 1) = *(ii + width - 1);
02866 
02867   return;
02868 }
02869 
02870 
02871 
02872 
02873 /**********************************************************************
02874  *
02875  *      Name:           Intra_AC_DC_Decode
02876  *      Description:    Intra Prediction in Advanced Intra Coding
02877  *
02878  *      Input:          store_qcoeff, Intra_AC_DC, position of MB, store_QP
02879  *
02880  *      Side effects:   change qcoeff to predicted qcoeff
02881  *
02882  *      Return:
02883  *
02884  *      Date:970717     Guy Cote <guyc@ee.ubc.ca>
02885  *
02886  ***********************************************************************/
02887 
02888 
02889 void Intra_AC_DC_Decode (short *store_qcoeff, int INTRA_AC_DC, int MBA, int xpos, int ypos, int comp, int newgob)
02890 {
02891 
02892   int A[8], B[8];
02893   int i, j, tempDC;
02894   short *Rec_C;
02895   short *rcoeff;
02896 
02897 
02898   Rec_C = ld->block[comp];
02899 
02900   if (xpos == 0 && ypos == 0)
02901   {                             /* top left corner */
02902     (comp == 2 || comp == 3) ? fill_A (A, store_qcoeff, xpos, ypos, comp - 2) : fill_null (A);
02903     (comp == 1 || comp == 3) ? fill_B (B, store_qcoeff, xpos, ypos, comp - 1) : fill_null (B);
02904   } else
02905   {                             /* left border of picture */
02906     if (xpos == 0)
02907     {                           /* left edge of the picture */
02908       (comp == 2 || comp == 3) ? fill_A (A, store_qcoeff, xpos, ypos, comp - 2) :
02909         ((comp == 0 || comp == 1) && !(newgob)) ? fill_A (A, store_qcoeff, xpos, ypos - 1, comp + 2) :
02910         ((comp == 4 || comp == 5) && !(newgob)) ? fill_A (A, store_qcoeff, xpos, ypos - 1, comp) : fill_null (A);
02911       (comp == 1 || comp == 3) ? fill_B (B, store_qcoeff, xpos, ypos, comp - 1) : fill_null (B);
02912     } else
02913     {
02914       if (ypos == 0)
02915       {                         /* top border of picture */
02916         (comp == 2 || comp == 3) ? fill_A (A, store_qcoeff, xpos, ypos, comp - 2) : fill_null (A);
02917         (comp == 4 || comp == 5) ? fill_B (B, store_qcoeff, xpos - 1, ypos, comp) :
02918           (comp == 1 || comp == 3) ? fill_B (B, store_qcoeff, xpos, ypos, comp - 1) :
02919           fill_B (B, store_qcoeff, xpos - 1, ypos, comp + 1);
02920       } else
02921       {                         /* anywhere else in the picture, do not
02922                                  * cross GOB boundary */
02923         (comp == 2 || comp == 3) ? fill_A (A, store_qcoeff, xpos, ypos, comp - 2) :
02924           ((comp == 0 || comp == 1) && !(newgob)) ? fill_A (A, store_qcoeff, xpos, ypos - 1, comp + 2) :
02925           ((comp == 4 || comp == 5) && !(newgob)) ? fill_A (A, store_qcoeff, xpos, ypos - 1, comp) : fill_null (A);
02926 
02927         (comp == 4 || comp == 5) ? fill_B (B, store_qcoeff, xpos - 1, ypos, comp) :
02928           (comp == 1 || comp == 3) ? fill_B (B, store_qcoeff, xpos, ypos, comp - 1) :
02929           fill_B (B, store_qcoeff, xpos - 1, ypos, comp + 1);
02930       }
02931     }
02932   }
02933 
02934 
02935   /* replace the qcoeff with the predicted values pcoeff */
02936   switch (INTRA_AC_DC)
02937   {
02938     case INTRA_MODE_DC:
02939 
02940       tempDC = Rec_C[0] + ((A[0] == 1024 && B[0] == 1024) ? 1024 :
02941                            (A[0] == 1024) ? B[0] :
02942                            (B[0] == 1024) ? A[0] : (A[0] + B[0]) / 2);
02943       for (i = 0; i < 8; i++)
02944         for (j = 0; j < 8; j++)
02945           Rec_C[i * 8 + j] = clipAC (Rec_C[i * 8 + j]);
02946       Rec_C[0] = oddifyclipDC (tempDC);
02947       break;
02948     case INTRA_MODE_VERT_AC:
02949       tempDC = Rec_C[0] + A[0];
02950       for (i = 1; i < 8; i++)
02951       {
02952         rcoeff = &Rec_C[i];
02953         *rcoeff = clipAC (Rec_C[i] + A[i]);
02954       }
02955       for (i = 1; i < 8; i++)
02956         for (j = 0; j < 8; j++)
02957           Rec_C[i * 8 + j] = clipAC (Rec_C[i * 8 + j]);
02958       Rec_C[0] = oddifyclipDC (tempDC);
02959       break;
02960     case INTRA_MODE_HORI_AC:
02961       tempDC = Rec_C[0] + B[0];
02962       for (i = 1; i < 8; i++)
02963         Rec_C[i * 8] = clipAC (Rec_C[i * 8] + B[i]);
02964       for (i = 0; i < 8; i++)
02965         for (j = 1; j < 8; j++)
02966           Rec_C[i * 8 + j] = clipAC (Rec_C[i * 8 + j]);
02967       Rec_C[0] = oddifyclipDC (tempDC);
02968       break;
02969     default:
02970       exit (-1);
02971       break;
02972   }
02973 
02974 
02975   return;
02976 }
02977 
02978 /**********************************************************************
02979  *
02980  *      Name:           fill_null, fill_A, fill_B, oddifyclipDC, clipAC
02981  *                      and clipDC
02982  *      Description:    Fill values in predictor coefficients
02983  *                      Functions used in advanced intra coding mode
02984  *
02985  *      Input:          predictor qcoefficients
02986  *      Side effects:
02987  *
02988  *      Return:
02989  *
02990  *      Date:970717     Guy Cote <guyc@ee.ubc.ca>
02991  *
02992  ***********************************************************************/
02993 
02994 
02995 void fill_null (int pred[])
02996 {
02997   int j;
02998 
02999   pred[0] = 1024;
03000   for (j = 1; j < 8; j++)
03001   {
03002     pred[j] = 0;
03003   }
03004 }
03005 
03006 void fill_A (int pred[], short *store_qcoeff, int xpos, int ypos, int block)
03007 {
03008   /* Fill first row of block at MB xpos, ypos, in pred[] */
03009   int j;
03010   for (j = 0; j < 8; j++)
03011   {
03012     pred[j] = *(store_qcoeff + (ypos * mb_width + xpos) * 384 + block * 64 + j);
03013   }
03014 }
03015 
03016 void fill_B (int pred[], short *store_qcoeff, int xpos, int ypos, int block)
03017 {
03018   /* Fill first column of block at MB xpos, ypos, in pred[][i] */
03019   int j;
03020   for (j = 0; j < 8; j++)
03021   {
03022     pred[j] = *(store_qcoeff + (ypos * mb_width + xpos) * 384 + block * 64 + j * 8);
03023   }
03024 }
03025 
03026 int oddifyclipDC (int x)
03027 {
03028 
03029   int result;
03030 
03031 
03032   (x % 2) ? (result = clipDC (x)) : (result = clipDC (x + 1));
03033   return result;
03034 }
03035 
03036 int clipAC (int x)
03037 {
03038   int clipped;
03039 
03040   if (x > 2047)
03041     clipped = 2047;
03042   else if (x < -2048)
03043     clipped = -2048;
03044   else
03045     clipped = x;
03046   return clipped;
03047 
03048 }
03049 
03050 int clipDC (int x)
03051 {
03052   int clipped;
03053   if (x > 2047)
03054     clipped = 2047;
03055   else if (x < 0)
03056     clipped = 0;
03057   else
03058     clipped = x;
03059   return clipped;
03060 
03061 }
03062 
03063 
03064 /**********************************************************************
03065  *
03066  *      Name:           EdgeFilter
03067  *      Description:    performs in the loop edge-filtering on
03068  *                      reconstructed frames
03069  *
03070  *      Input:          pointers to reconstructed frame and difference
03071  *                      image
03072  *      Returns:
03073  *      Side effects:   since neither the algorithm nor the routines
03074  *                      have been optimized for speed, the use of the
03075  *                      edge-filter slows down decoding speed
03076  *
03077  *      Date: 951129    Author: Gisle.Bjontegaard@fou.telenor.no
03078  *                        Karl.Lillevold@nta.no
03079  *  Date: 970820  Author: guyc@ee.ubc.ca
03080  *                        modified to implement annex J of H.263+
03081  *
03082  ***********************************************************************/
03083 
03084 
03085 void edge_filter (unsigned char *lum, unsigned char *Cb, unsigned char *Cr,
03086                    int width, int height)
03087 {
03088 
03089   /* Luma */
03090   horiz_edge_filter (lum, width, height, 0);
03091   vert_edge_filter (lum, width, height, 0);
03092 
03093   /* Chroma */
03094   horiz_edge_filter (Cb, width / 2, height / 2, 1);
03095   vert_edge_filter (Cb, width / 2, height / 2, 1);
03096   horiz_edge_filter (Cr, width / 2, height / 2, 1);
03097   vert_edge_filter (Cr, width / 2, height / 2, 1);
03098 
03099   /* that's it */
03100   return;
03101 }
03102 
03103 
03104 /***********************************************************************/
03105 
03106 
03107 void horiz_edge_filter (unsigned char *rec, int width, int height, int chr)
03108 {
03109   int i, j;
03110   int delta, d1, d2;
03111   int mbc, mbr, do_filter;
03112   int QP;
03113   int mbr_above;
03114 
03115 
03116   /* horizontal edges */
03117   for (j = 8; j < height; j += 8)
03118   {
03119     for (i = 0; i < width; i++)
03120     {
03121       if (!chr)
03122       {
03123         mbr = j >> 4;
03124         mbc = i >> 4;
03125         mbr_above = (j - 8) >> 4;
03126       } else
03127       {
03128         mbr = j >> 3;
03129         mbc = i >> 3;
03130         mbr_above = mbr - 1;
03131       }
03132 
03133       do_filter = coded_map[mbr + 1][mbc + 1] || coded_map[mbr_above + 1][mbc + 1];
03134       if (do_filter)
03135       {
03136         if (pb_frame)
03137         {
03138           QP = coded_map[mbr + 1][mbc + 1] ?
03139             mmax (1, mmin (31, bquant_tab[bquant] * quant_map[mbr + 1][mbc + 1] / 4)) :
03140             mmax (1, mmin (31, bquant_tab[bquant] * quant_map[mbr_above + 1][mbc + 1] / 4));
03141         } else
03142           QP = coded_map[mbr + 1][mbc + 1] ? quant_map[mbr + 1][mbc + 1] : quant_map[mbr_above + 1][mbc + 1];
03143         if (chr && modified_quantization_mode) 
03144         {
03145           QP = MQ_chroma_QP_table[QP];
03146         } 
03147 
03148         delta = (int) (((int) (*(rec + i + (j - 2) * width)) +
03149                         (int) (*(rec + i + (j - 1) * width) * (-4)) +
03150                         (int) (*(rec + i + (j) * width) * (4)) +
03151                         (int) (*(rec + i + (j + 1) * width) * (-1))) / 8.0);
03152 
03153         d1 = sign (delta) * mmax (0, abs (delta) - mmax (0, 2 * (abs (delta) - STRENGTH[QP - 1])));
03154 
03155         d2 = mmin (abs (d1 / 2), mmax (-abs (d1 / 2), (int) (((*(rec + i + (j - 2) * width) -
03156                                    *(rec + i + (j + 1) * width))) / 4)));
03157 
03158         *(rec + i + (j + 1) * width) += d2; /* D */
03159         *(rec + i + (j) * width) = mmin (255, mmax (0, (int) (*(rec + i + (j) * width)) - d1)); /* C */
03160         *(rec + i + (j - 1) * width) = mmin (255, mmax (0, (int) (*(rec + i + (j - 1) * width)) + d1)); /* B */
03161         *(rec + i + (j - 2) * width) -= d2; /* A */
03162       }
03163     }
03164   }
03165   return;
03166 }
03167 
03168 void vert_edge_filter (unsigned char *rec, int width, int height, int chr)
03169 {
03170   int i, j;
03171   int delta, d1, d2;
03172   int mbc, mbr;
03173   int do_filter;
03174   int QP;
03175   int mbc_left;
03176 
03177 
03178   /* vertical edges */
03179   for (i = 8; i < width; i += 8)
03180   {
03181     for (j = 0; j < height; j++)
03182     {
03183       if (!chr)
03184       {
03185         mbr = j >> 4;
03186         mbc = i >> 4;
03187         mbc_left = (i - 8) >> 4;
03188       } 
03189       else
03190       {
03191         mbr = j >> 3;
03192         mbc = i >> 3;
03193         mbc_left = mbc - 1;
03194       }
03195       do_filter = coded_map[mbr + 1][mbc + 1] || coded_map[mbr + 1][mbc_left + 1];
03196 
03197       if (do_filter)
03198       {
03199         if (pb_frame)
03200         {
03201           QP = coded_map[mbr + 1][mbc + 1] ?
03202             mmax (1, mmin (31, bquant_tab[bquant] * quant_map[mbr + 1][mbc + 1] / 4)) :
03203             mmax (1, mmin (31, bquant_tab[bquant] * quant_map[mbr + 1][mbc_left + 1] / 4));
03204         } 
03205         else
03206           QP = coded_map[mbr + 1][mbc + 1] ? 
03207                quant_map[mbr + 1][mbc + 1] : quant_map[mbr + 1][mbc_left + 1];
03208         if (chr && modified_quantization_mode) 
03209         {
03210           QP = MQ_chroma_QP_table[QP];
03211         } 
03212 
03213         delta = (int) (((int) (*(rec + i - 2 + j * width)) +
03214                         (int) (*(rec + i - 1 + j * width) * (-4)) +
03215                         (int) (*(rec + i + j * width) * (4)) +
03216                         (int) (*(rec + i + 1 + j * width) * (-1))) / 8.0);
03217 
03218         d1 = sign (delta) * mmax (0, abs (delta) - 
03219                             mmax (0, 2 * (abs (delta) - STRENGTH[QP - 1])));
03220 
03221         d2 = mmin (abs (d1 / 2), mmax (-abs (d1 / 2), 
03222                    (int) ((*(rec + i - 2 + j * width) -
03223                            *(rec + i + 1 + j * width)) / 4)));
03224 
03225         *(rec + i + 1 + j * width) += d2; /* D */
03226         *(rec + i + j * width) = mmin (255, mmax (0, (int) (*(rec + i + j * width)) - d1)); /* C */
03227         *(rec + i - 1 + j * width) = mmin (255, mmax (0, (int) (*(rec + i - 1 + j * width)) + d1)); /* B */
03228         *(rec + i - 2 + j * width) -= d2; /* A */
03229       }
03230     }
03231   }
03232   return;
03233 }
03234 
03235 /**********************************************************************
03236  *
03237  *      Name:           PostFilter
03238  *      Description:    performs in the loop edge-filtering on
03239  *                      reconstructed frames
03240  *
03241  *      Input:          pointers to reconstructed frame and difference
03242  *                      image
03243  *      Returns:
03244  *      Side effects:   since neither the algorithm nor the routines
03245  *                      have been optimized for speed, the use of the
03246  *                      edge-filter slows down decoding speed
03247  *
03248  *
03249  *      Date: 971004    Author: guyc@ee.ubc.ca
03250  *
03251  *
03252  ***********************************************************************/
03253 
03254 
03255 void PostFilter (unsigned char *lum, unsigned char *Cb, unsigned char *Cr,
03256                   int width, int height)
03257 {
03258 
03259   /* Luma */
03260   horiz_post_filter (lum, width, height, 0);
03261   vert_post_filter (lum, width, height, 0);
03262 
03263   /* Chroma */
03264   horiz_post_filter (Cb, width / 2, height / 2, 1);
03265   vert_post_filter (Cb, width / 2, height / 2, 1);
03266   horiz_post_filter (Cr, width / 2, height / 2, 1);
03267   vert_post_filter (Cr, width / 2, height / 2, 1);
03268 
03269   /* that's it */
03270   return;
03271 }
03272 
03273 
03274 /***********************************************************************/
03275 
03276 
03277 void horiz_post_filter (unsigned char *rec, int width, int height, int chr)
03278 {
03279   int i, j;
03280   int delta, d1;
03281   int mbc, mbr;
03282   int QP;
03283   int mbr_above;
03284 
03285 
03286   /* horizontal edges */
03287   for (j = 8; j < height; j += 8)
03288   {
03289     for (i = 0; i < width; i++)
03290     {
03291       if (!chr)
03292       {
03293         mbr = j >> 4;
03294         mbc = i >> 4;
03295         mbr_above = (j - 8) >> 4;
03296       } else
03297       {
03298         mbr = j >> 3;
03299         mbc = i >> 3;
03300         mbr_above = mbr - 1;
03301       }
03302 
03303 
03304       if (pb_frame)
03305       {
03306         QP = coded_map[mbr + 1][mbc + 1] ?
03307           mmax (1, mmin (31, bquant_tab[bquant] * quant_map[mbr + 1][mbc + 1] / 4)) :
03308           mmax (1, mmin (31, bquant_tab[bquant] * quant_map[mbr_above + 1][mbc + 1] / 4));
03309       } else
03310         QP = coded_map[mbr + 1][mbc + 1] ? 
03311              quant_map[mbr + 1][mbc + 1] : quant_map[mbr_above + 1][mbc + 1];
03312 
03313       delta = (int) (((int) (*(rec + i + (j - 3) * width)) +
03314                       (int) (*(rec + i + (j - 2) * width)) +
03315                       (int) (*(rec + i + (j - 1) * width)) +
03316                       (int) (*(rec + i + (j) * width) * (-6)) +
03317                       (int) (*(rec + i + (j + 1) * width)) +
03318                       (int) (*(rec + i + (j + 2) * width)) +
03319                       (int) (*(rec + i + (j + 3) * width))) / 8.0);
03320 
03321       d1 = sign (delta) * mmax (0, abs (delta) - mmax (0, 2 * (abs (delta) - STRENGTH1[QP - 1])));
03322 
03323       /* Filter D */
03324       *(rec + i + (j) * width) += d1;
03325     }
03326   }
03327   return;
03328 }
03329 
03330 void vert_post_filter (unsigned char *rec, int width, int height, int chr)
03331 {
03332   int i, j;
03333   int delta, d1;
03334   int mbc, mbr;
03335   int QP;
03336   int mbc_left;
03337 
03338 
03339   /* vertical edges */
03340   for (i = 8; i < width; i += 8)
03341   {
03342     for (j = 0; j < height; j++)
03343     {
03344       if (!chr)
03345       {
03346         mbr = j >> 4;
03347         mbc = i >> 4;
03348         mbc_left = (i - 8) >> 4;
03349       } else
03350       {
03351         mbr = j >> 3;
03352         mbc = i >> 3;
03353         mbc_left = mbc - 1;
03354       }
03355 
03356       if (pb_frame)
03357       {
03358         QP = coded_map[mbr + 1][mbc + 1] ?
03359           mmax (1, mmin (31, bquant_tab[bquant] * quant_map[mbr + 1][mbc + 1] / 4)) :
03360           mmax (1, mmin (31, bquant_tab[bquant] * quant_map[mbr + 1][mbc_left + 1] / 4));
03361       } else
03362         QP = coded_map[mbr + 1][mbc + 1] ? 
03363              quant_map[mbr + 1][mbc + 1] : quant_map[mbr + 1][mbc_left + 1];
03364 
03365       delta = (int) (((int) (*(rec + i - 3 + j * width)) +
03366                       (int) (*(rec + i - 2 + j * width)) +
03367                       (int) (*(rec + i - 1 + j * width)) +
03368                       (int) (*(rec + i + j * width) * (-6)) +
03369                       (int) (*(rec + i + 1 + j * width)) +
03370                       (int) (*(rec + i + 2 + j * width)) +
03371                       (int) (*(rec + i + 3 + j * width))) / 8.0);
03372 
03373       d1 = sign (delta) * mmax (0, abs (delta) - mmax (0, 2 * (abs (delta) - STRENGTH2[QP - 1])));
03374 
03375       /* Post Filter D */
03376       *(rec + i + j * width) += d1;
03377     }
03378   }
03379   return;
03380 }
03381 
03382 /**********************************************************************
03383  *
03384  *      Name:                 init_enhancement_layer
03385  *      Description:  intializes an enhancement layer when the first picture header
03386  *                in that layer is decoded
03387  *      Input:        layer number
03388  *      Returns:      
03389  *      Side effects: allocates memory for previous and current enhancement 
03390  *                layer picture as well as framed enhancement layer picture, 
03391  *                and tmp forward and upward pictures (for bi-dir prediction),
03392  *                Y, Cb, and Cr components.
03393  *                
03394  *  Date: 971102  Author: mikeg@ee.ubc.ca
03395  *
03396  *
03397  ***********************************************************************/
03398 static void init_enhancement_layer (int layer)
03399 {
03400   int cc, size;
03401 
03402   blk_cnt = 6;
03403 
03404   for (cc = 0; cc < 3; cc++)
03405   {
03406     if (cc == 0)
03407       size = coded_picture_width * coded_picture_height;
03408     else
03409       size = chrom_width * chrom_height;
03410 
03411     /* Used for bidirectional and direct prediction mode for true B
03412      * pictures, one for forward and one for backward. */
03413     prev_enhancement_frame[layer][cc] = (unsigned char *) malloc (size);
03414     
03415     current_enhancement_frame[layer][cc] = (unsigned char *) malloc (size);
03416 
03417     tmp_enhance_fwd[layer][cc] = (unsigned char *) malloc (size);
03418 
03419     tmp_enhance_up[layer][cc] = (unsigned char *) malloc (size);
03420 
03421   }
03422 
03423   for (cc = 0; cc < 3; cc++)
03424   {
03425     if (cc == 0)
03426     {
03427       size = (coded_picture_width + 64) * (coded_picture_height + 64);
03428 
03429       /* Stores framed version of previous inter-picture, luminance. */
03430       enhance_edgeframeorig[layer][cc] = (unsigned char *) malloc (size);
03431 
03432       enhance_edgeframe[layer][cc] = enhance_edgeframeorig[layer][cc] + (coded_picture_width + 64) * 32 + 32;
03433     } 
03434     else
03435     {
03436       size = (chrom_width + 32) * (chrom_height + 32);
03437 
03438       /* Stores framed version of previous inter-picture, chrominance. */
03439       enhance_edgeframeorig[layer][cc] = (unsigned char *) malloc (size);
03440 
03441       enhance_edgeframe[layer][cc] = enhance_edgeframeorig[layer][cc] + (chrom_width + 32) * 16 + 16;
03442     }
03443   }
03444 
03445   sprintf (enhance_recon_file_name[enhancement_layer_num-2], "enhanced_%d.raw", 
03446            (enhancement_layer_num-1));
03447   if ((enhance_recon_file_ptr[enhancement_layer_num-2] = fopen (
03448        enhance_recon_file_name[enhancement_layer_num-2], "wb")) == NULL)
03449   {
03450     exit (-1);
03451   }
03452 }
03453 
03454 /**********************************************************************
03455  *
03456  *  Name:         UpsampleReferenceLayerPicture
03457  *  Description:  calls UpsampleComponent for each picture component 
03458  *                (Y,Cb, or Cr)
03459  *  Input:            
03460  *  Returns:      
03461  *  Side effects: allocates memory for spatially scaled enhancement 
03462  *                layer picture Y, Cb, and Cr components.
03463  *
03464  *  Date: 971102  Author: mikeg@ee.ubc.ca
03465  *
03466  ***********************************************************************/
03467 void UpsampleReferenceLayerPicture(void)
03468 {
03469 
03470   int cc, size, x, y;
03471   
03472   for (cc = 0; cc < 3; cc++)
03473   {
03474     if (cc == 0)
03475     {
03476       size = coded_picture_width * coded_picture_height;
03477       x = ref_coded_picture_width;
03478       y = ref_coded_picture_height;
03479     }
03480     else
03481     {
03482       size = chrom_width * chrom_height;
03483       x = ref_chrom_width;
03484       y = ref_chrom_height;
03485     }
03486 
03487     upsampled_reference_frame[cc] = (unsigned char *) malloc (size);
03488 
03489     UpsampleComponent(upsampled_reference_frame[cc], current_frame[cc], x, y);
03490   }
03491 }
03492 
03493 /**********************************************************************
03494  *
03495  *  Name:         UpsampleComponent
03496  *  Description:  interpolates in horiz only, vert only or both 
03497  *                vert and horiz depending on spatial scalability
03498  *                option in use
03499  *  Input:        pointers to  reference layer and enhancement layer
03500  *                picture components (Y,Cb,or Cr) and dimensions
03501  *                of reference layer picture
03502  *  Returns:      
03503  *  Side effects: 
03504  *
03505  *  Date: 971102  Author: mikeg@ee.ubc.ca
03506  *
03507  ***********************************************************************/
03508 void UpsampleComponent (unsigned char *enhanced, unsigned char *base,
03509                         int horiz, int vert)
03510 {
03511   int i,j;
03512   unsigned char *base_next, *enhanced_next, *enhanced_origin;
03513 
03514   enhanced_origin = enhanced;
03515 
03516   switch (scalability_mode)
03517   {
03518     case SPATIAL_SCALABILITY_H:
03519 
03520       /* Rows */
03521       for( j=0; j<vert; j++ )
03522       {
03523         /* First column of rows */
03524         *enhanced++ = *base;  
03525         for( i=1; i<horiz; i++ )
03526         {
03527           *enhanced++ = (3* *base +    *(base+1) + 2) >> 2;
03528           *enhanced++ = (   *base + 3* *(base+1) + 2) >> 2;
03529           base++;
03530         }
03531         /* Last column of rows */
03532         *enhanced++ = *base++;
03533       }
03534 
03535       break;
03536 
03537     case SPATIAL_SCALABILITY_V:
03538       
03539       /* First row */
03540       for( i=0 ; i<horiz; i++ )      
03541       {
03542         *enhanced++ = *base++;
03543       }
03544       
03545       enhanced_next = enhanced + horiz;
03546       base          = base - horiz;
03547       base_next     = base + horiz;
03548 
03549       /* Rows */
03550       for( j=0; j<vert-1; j++ )
03551       {
03552         
03553         for( i=0; i<horiz; i++ )
03554         {
03555           *enhanced++      = (3* *base +    *(base_next) + 2) >> 2;  
03556           *enhanced_next++ = (   *base + 3* *(base_next) + 2) >> 2;  
03557           base++;
03558           base_next++;
03559         }
03560         enhanced      = enhanced + horiz;
03561         enhanced_next = enhanced + horiz;
03562       }
03563 
03564       /* Last row */
03565       for( i=0 ; i<horiz; i++ )      
03566       {
03567         *enhanced++ = *base++;
03568       }
03569       
03570       break;
03571 
03572     case SPATIAL_SCALABILITY_HV:
03573 
03574       /* Top left corner pel */
03575       *enhanced++  = *base;   
03576       /* First row */
03577       for( i=1 ; i<horiz; i++ )      
03578       {
03579         *enhanced++ = (3* *base +    *(base+1) + 2) >> 2;
03580         *enhanced++ = (   *base + 3* *(base+1) + 2) >> 2;
03581         base++ ;
03582       }
03583       /* Top right corner pel */
03584       *enhanced++ = *base++;   
03585 
03586       enhanced_next = enhanced + (horiz<<1);
03587       base          = base - horiz;
03588       base_next     = base + horiz;
03589       
03590       /* Rows */
03591       for( j=0; j<vert-1; j++ )
03592       {
03593         /* First column of rows */
03594         *enhanced++       = (3* *base +    *(base_next) + 2) >> 2;  
03595         *enhanced_next++  = (   *base + 3* *(base_next) + 2) >> 2;
03596         for( i=1; i<horiz; i++ )
03597         {
03598           *enhanced++      = (9* *base + 3* *(base+1) + 3* *base_next +    
03599                                  *(base_next+1) + 8) >> 4;
03600           *enhanced++      = (3* *base + 9* *(base+1) +    *base_next + 
03601                               3* *(base_next+1) + 8) >> 4;
03602           *enhanced_next++ = (3* *base +    *(base+1) + 9* *base_next + 
03603                               3* *(base_next+1) + 8) >> 4;
03604           *enhanced_next++ = (   *base + 3* *(base+1) + 3* *base_next + 
03605                               9* *(base_next+1) + 8) >> 4;
03606           base++;
03607           base_next++;
03608         }
03609         /* Last column of rows */
03610         *enhanced++      = (3* *base +    *(base_next) + 2) >> 2;  
03611         *enhanced_next++ = (   *base + 3* *(base_next) + 2) >> 2;  
03612 
03613         enhanced      = enhanced + (horiz<<1);
03614         enhanced_next = enhanced + (horiz<<1);
03615         base++;
03616         base_next++;
03617       }
03618       
03619       /* Bottom left corner pel */
03620       *enhanced++  = *base;   
03621       /* Last row */
03622       for( i=1; i<horiz; i++ )                                
03623       {
03624         *enhanced++ = (3* *base +    *(base+1) + 2) >> 2 ;
03625         *enhanced++ = (   *base + 3* *(base+1) + 2) >> 2 ;
03626         base++ ;
03627       }
03628       /* Bottom right corner pel */
03629       *enhanced = *base;
03630 
03631       break;
03632 
03633     default:
03634 
03635       break;
03636   }
03637 }
03638 
03639 
03640 void conceal_missing_gobs(int start_mb_row_missing, int number_of_mb_rows_missing)
03641 {
03642    /* counters */
03643   int xpos, ypos; 
03644   
03645   int bx,by;
03646   int end = start_mb_row_missing + number_of_mb_rows_missing;
03647   
03648   for (ypos = start_mb_row_missing; ypos < end; ypos++)
03649   {
03650     for (xpos = 0; xpos < mb_width; xpos++)
03651     {
03652       
03653       bx = 16 * xpos;
03654       by = 16 * ypos;
03655       
03656       /* Assume mode was Inter */
03657       modemap[ypos+1][xpos+1] = MODE_INTER;
03658 
03659       /* copy the motion vectors from the GOB above if present */
03660       /* (at ypos = 0, just use 0 MVs */
03661       if(!start_mb_row_missing)
03662       {
03663         MV[0][0][ypos + 1][xpos + 1] = 0;
03664         MV[1][0][ypos + 1][xpos + 1] = 0;
03665       }
03666       else
03667       {
03668         MV[0][0][ypos + 1][xpos + 1] = MV[0][0][start_mb_row_missing][xpos + 1];
03669         MV[1][0][ypos + 1][xpos + 1] = MV[1][0][start_mb_row_missing][xpos + 1];
03670       }
03671       
03672       /* motion compensation for P-frame */
03673       /* Always assume MBs were INTER coded */
03674       reconstruct (bx, by, 1, 0, 0, 0, 1);
03675     }
03676   }
03677 }
03678 
03679 
03680 

Generated on Mon May 8 22:27:08 2006 by  doxygen 1.3.9.1