Main Page | File List

GETHDR.C

00001 /************************************************************************
00002  *
00003  *  gethdr.c, header 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: Michael Gallant <mikeg@ee.ubc.ca>
00015  *               Guy Cote <guyc@ee.ubc.ca>
00016  *               Berna Erol <bernae@ee.ubc.ca>
00017  *
00018  *  Contacts:
00019  *  Michael 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 to support general H.263+ syntax, bernae@ee.ubc.ca
00057  * 
00058  * based on mpeg2decode, (C) 1994, MPEG Software Simulation Group and
00059  * mpeg2play, (C) 1994 Stefan Eckart <stefan@lis.e-technik.tu-muenchen.de> */
00060 
00061 
00062 #include <stdio.h>
00063 #include <stdlib.h>
00064 
00065 #include "config.h"
00066 #include "tmndec.h"
00067 #include "global.h"
00068 
00069 /* private prototypes */
00070 static void getpicturehdr _ANSI_ARGS_ ((void));
00071 
00072 /* decode headers from one input stream until an End of Sequence or
00073  * picture start code is found */
00074 
00075 int getheader ()
00076 {
00077   unsigned int code, gob;
00078 
00079   /* look for startcode */
00080   startcode ();
00081   code = getbits (PSC_LENGTH);
00082   gob = getbits (5);
00083   if (gob == SE_CODE)
00084     return 0;
00085   if (gob == 0)
00086   {
00087     getpicturehdr ();
00088     if (syntax_arith_coding)    /* reset decoder after receiving */
00089       decoder_reset ();         /* fixed length PSC string */
00090   } 
00091   return gob + 1;
00092 }
00093 
00094 
00095 /* align to start of next startcode */
00096 
00097 void startcode ()
00098 {
00099   /* search for new picture start code */
00100   while (showbits (PSC_LENGTH) != 1l)
00101     flushbits (1);
00102 }
00103 
00104 /* decode picture header */
00105 
00106 static void getpicturehdr ()
00107 {
00108   int pos, pei, tmp;
00109   int TRPI = 0;
00110   int BCI = 0;
00111   int clock_conversion_code = 0;
00112   int clock_divisor = 0;
00113   int extended_temporal_reference = 0;
00114 
00115   UFEP = 0;
00116 
00117   pos = ld->bitcnt;
00118 
00119   temp_ref = getbits (8);
00120 
00121   if (trd < 0)
00122     trd += 256;
00123 
00124   tmp = getbits (1);            /* always "1" */
00125   
00126   tmp = getbits (1);            /* always "0" */
00127   
00128   tmp = getbits (1);            /* split_screen_indicator */
00129   
00130   if (tmp)
00131   {
00132      exit (-1);
00133   }
00134   
00135   tmp = getbits (1);            /* document_camera_indicator */
00136   
00137   tmp = getbits (1);            /* freeze_picture_release */
00138   
00139   tmp = getbits (3);
00140   
00141   if (tmp == 0)
00142   {
00143      exit (-1);
00144   }
00145   
00146   if (tmp == EXTENDED_PTYPE)
00147   {
00148     plus_type = 1;
00149     UFEP = getbits (3);
00150   
00151         if (UFEP == 1)
00152     {                           /* OPPTYPE */
00153       source_format = getbits (3);
00154     
00155       /* optional custom picture clock frequency */
00156       optional_custom_PCF = getbits (1);
00157     
00158           if (optional_custom_PCF)
00159       {
00160         exit (-1);
00161       }
00162       mv_outside_frame = getbits (1);
00163       
00164       long_vectors = (mv_outside_frame ? 1 : 0);
00165       syntax_arith_coding = getbits (1);
00166       
00167           adv_pred_mode = getbits (1);
00168       
00169       mv_outside_frame = (adv_pred_mode ? 1 : mv_outside_frame);
00170       overlapping_MC = (adv_pred_mode ? 1 : 0);
00171       use_4mv = (adv_pred_mode ? 1 : 0);
00172       pb_frame = 0;
00173       advanced_intra_coding = getbits (1);
00174       
00175       deblocking_filter_mode = getbits (1);
00176       
00177       mv_outside_frame = (deblocking_filter_mode ? 1 : mv_outside_frame);
00178       use_4mv = (deblocking_filter_mode ? 1 : use_4mv);
00179 
00180       slice_structured_mode = getbits (1);
00181       
00182       if (slice_structured_mode)
00183       {
00184          exit (-1);
00185       }
00186       reference_picture_selection_mode = getbits (1);
00187       
00188       independently_segmented_decoding_mode = getbits (1);
00189       
00190       if (independently_segmented_decoding_mode)
00191       {
00192         exit (-1);
00193       }
00194       alternative_inter_VLC_mode = getbits (1);
00195       
00196       modified_quantization_mode = getbits (1);
00197       
00198           tmp = getbits (4);
00199       
00200       if (tmp != 8)
00201       {                         /* OPPTYPE : bit15=1, bit16,bit17,bit18=0 */
00202           exit (-1);
00203       }
00204     }
00205     if ((UFEP == 1) || (UFEP == 0))
00206     {
00207       if (UFEP == 0)
00208       {
00209         if (scalability_mode >= 3)
00210         {
00211           horizontal_size = lines[base_source_format];
00212           vertical_size = pels[base_source_format];
00213 
00214           mb_width = horizontal_size / 16;
00215           mb_height = vertical_size / 16;
00216 
00217           /* Need to store previous (reference layer) values 
00218            * for interpolation purposes, as the new values, 
00219            * i.e. of the spatially scaled layer, depend on 
00220            * the type of spatial scalability in use. */
00221           ref_coded_picture_width = coded_picture_width = horizontal_size;
00222           ref_coded_picture_height = coded_picture_height = vertical_size;
00223           ref_chrom_width = chrom_width = coded_picture_width >> 1;
00224           ref_chrom_height = chrom_height = coded_picture_height >> 1;
00225 
00226           source_format = base_source_format;
00227         }
00228       }
00229 
00230       /* MMPTYPE */
00231       
00232       pict_type = getbits (3);
00233       
00234       if (pict_type == PCT_IPB)
00235         pb_frame = IM_PB_FRAMES;
00236       else
00237         pb_frame = 0;
00238 
00239       if (PCT_B == pict_type)
00240       {
00241         true_B_frame = ON;
00242         /* Allow motion over picture boundaries, regardless of whether or
00243          * not UMV is turned on. */
00244         mv_outside_frame = 1;
00245         true_b_trb = temp_ref - prev_non_disposable_temp_ref;
00246         if (true_b_trb < 0)
00247           true_b_trb += 256;
00248       } else
00249       {
00250         true_B_frame = OFF;
00251         prev_non_disposable_temp_ref = next_non_disposable_temp_ref;
00252         next_non_disposable_temp_ref = temp_ref;
00253         trd = temp_ref - prev_non_disposable_temp_ref;
00254         if (trd < 0)
00255           trd += 256;
00256       }
00257 
00258       reference_picture_resampling_mode = getbits (1);
00259       
00260       if (reference_picture_resampling_mode)
00261       {
00262          exit (-1);
00263       }
00264       reduced_resolution_update_mode = getbits (1);
00265       
00266       if (reduced_resolution_update_mode)
00267       {
00268           exit (-1);
00269       }
00270       rtype = getbits (1);      /* rounding type */
00271       
00272       
00273       tmp = getbits (3);
00274       
00275       if (tmp != 1)
00276       {                         /* MPPTYPE : bit7,bit8=0  bit9=1 */
00277         if (!quiet)
00278           exit (-1);
00279       }
00280     } else
00281     {
00282       /* UFEP is neither 001 nor 000 */
00283       exit (-1);
00284     }
00285 
00286     tmp = getbits (1);
00287     
00288     if (tmp)
00289     {
00290       exit (-1);
00291     }
00292   
00293     if (UFEP && (source_format == SF_CUSTOM))
00294     {
00295       /* Read custom picture format */
00296       CP_PAR_code = getbits (4);
00297       if (CP_PAR_code != PAR_CIF)
00298       {
00299         exit(-1);
00300       }
00301       tmp=getbits (9);
00302       horizontal_size = (tmp + 1 ) * 4;
00303       tmp = getbits (1);
00304       if (!tmp)
00305       {
00306          exit (-1);
00307       }
00308       tmp = getbits (9);
00309       vertical_size = tmp * 4;
00310       if ((horizontal_size%16) || (vertical_size%16))
00311       {
00312         exit (-1);
00313       }
00314 
00315       if (CP_PAR_code == EXTENDED_PAR)
00316       {
00317         PAR_width = getbits (8);
00318         PAR_height = getbits (8);
00319       }
00320     }
00321 
00322     if (source_format != SF_CUSTOM)
00323     {
00324         horizontal_size = lines[source_format];
00325         vertical_size = pels[source_format];
00326 
00327     }
00328     mb_width = horizontal_size / 16;
00329     mb_height = vertical_size / 16;
00330 
00331    /* Need to store previous (reference layer) values 
00332     * for interpolation purposes, as the new values, 
00333     * i.e. of the spatially scaled layer, depend on 
00334     * the type of spatial scalability in use. */
00335     ref_coded_picture_width = coded_picture_width;
00336     coded_picture_width = horizontal_size;
00337 
00338     ref_coded_picture_height = coded_picture_height;
00339     coded_picture_height = vertical_size;
00340 
00341     ref_chrom_width = chrom_width; 
00342     chrom_width = coded_picture_width >> 1;
00343 
00344     ref_chrom_height = chrom_height;
00345     chrom_height = coded_picture_height >> 1;
00346     
00347     if (optional_custom_PCF)
00348     {
00349       if (UFEP)
00350       {
00351         clock_conversion_code = getbits (1);
00352         clock_divisor = getbits (7);
00353         CP_clock_frequency = (int) (1800 / ((float) clock_divisor * (8 + clock_conversion_code)) * 1000);
00354       }
00355       /* regardless of the value of UFEP */
00356       extended_temporal_reference = getbits (2);
00357       temp_ref = extended_temporal_reference << 8 + temp_ref;
00358 
00359       if (PCT_B == pict_type)
00360       {
00361         true_b_trb = temp_ref - prev_non_disposable_temp_ref;
00362       } else
00363       {
00364         trd = temp_ref - prev_non_disposable_temp_ref;
00365       }
00366 
00367       if (trd < 0)
00368         trd += 1024;
00369     }
00370     if (UFEP && long_vectors)
00371     {
00372       if (getbits(1)) 
00373       {
00374         unlimited_unrestricted_motion_vectors = 0;
00375       }
00376       else 
00377       {
00378         flushbits(1);
00379         unlimited_unrestricted_motion_vectors = 1;
00380       }
00381     }
00382     if (UFEP && slice_structured_mode)
00383     {
00384       SSS_rectangular_slices = getbits (1);
00385       SSS_arbitary_slice_ordering = getbits (1);
00386     
00387     }
00388 
00389     if ((pict_type == PCT_B) || (pict_type == PCT_EI) || (pict_type == PCT_EP))
00390     {
00391       /* optional temporal, SNR and spatial scalability mode in use */
00392       enhancement_layer_num = getbits (4);
00393       if (UFEP)
00394       {
00395         reference_layer_number = getbits (4);
00396       }
00397       if (1 != enhancement_layer_num)
00398       {
00399         if (source_format != base_source_format)
00400         {
00401           if (source_format != SF_CUSTOM)
00402           {
00403             scalability_mode = SPATIAL_SCALABILITY_HV;
00404           }
00405           else if (coded_picture_width != ref_coded_picture_width)
00406           {
00407             scalability_mode = SPATIAL_SCALABILITY_H;
00408           }
00409           else 
00410           {
00411             scalability_mode = SPATIAL_SCALABILITY_V;
00412           }
00413         }
00414         else
00415         {
00416           scalability_mode = SNR_SCALABILITY;
00417         }
00418       }   
00419     }
00420     else
00421     {
00422       enhancement_layer_num = reference_layer_number = 1;
00423     }
00424 
00425     if (reference_picture_selection_mode)
00426     {
00427       if (UFEP)
00428       {
00429         MF_of_reference_picture_selection = getbits (3);
00430       }
00431       TRPI = getbits (1);
00432       if (TRPI)
00433       {
00434         /* temporal reference for prediction exists */
00435         
00436         temporal_reference_for_prediction = getbits (10);
00437       }
00438       if (showbits (1))
00439       {
00440         /* backward channel message exists */
00441         /* BCM(backward channel message) is not implemented */
00442         flushbits (1);
00443         exit (-1);
00444       }
00445       else
00446       {
00447         /* BCM is not present */
00448         BCI = getbits (2);
00449       }  
00450     }
00451     if (reference_picture_resampling_mode)
00452     {
00453         /* reading RPRP info is not implemented */
00454       exit (-1);
00455     }
00456     pic_quant = getbits (5);
00457     quant = pic_quant;
00458 
00459   } 
00460   else
00461   {
00462     plus_type = 0;
00463     rtype = 0;
00464     source_format = tmp;
00465     enhancement_layer_num = reference_layer_number = 1;
00466 
00467     horizontal_size = lines[source_format];
00468     vertical_size = pels[source_format];
00469 
00470     mb_width = horizontal_size / 16;
00471     mb_height = vertical_size / 16;
00472 
00473     /* Need to store previous (reference layer) values 
00474      * for interpolation purposes, as the new values, 
00475      * i.e. of the spatially scaled layer, depend on 
00476      * the type of spatial scalability in use. */
00477     ref_coded_picture_width = coded_picture_width = horizontal_size;
00478     ref_coded_picture_height = coded_picture_height = vertical_size;
00479     ref_chrom_width = chrom_width = coded_picture_width >> 1;
00480     ref_chrom_height = chrom_height = coded_picture_height >> 1;
00481 
00482     true_B_frame = OFF;
00483     prev_non_disposable_temp_ref = next_non_disposable_temp_ref;
00484     next_non_disposable_temp_ref = temp_ref;
00485     trd = temp_ref - prev_non_disposable_temp_ref;
00486     if (trd < 0)
00487       trd += 256;
00488 
00489     pict_type = getbits (1);
00490     mv_outside_frame = getbits (1);
00491     long_vectors = (mv_outside_frame ? 1 : 0);
00492     syntax_arith_coding = getbits (1);
00493     adv_pred_mode = getbits (1);
00494     mv_outside_frame = (adv_pred_mode ? 1 : mv_outside_frame);
00495     overlapping_MC = (adv_pred_mode ? 1 : 0);
00496     use_4mv = (adv_pred_mode ? 1 : 0);
00497     pb_frame = getbits (1);
00498    
00499     pic_quant = getbits (5);
00500     quant = pic_quant;
00501 
00502 
00503     tmp = getbits (1);
00504     if (tmp)
00505     {
00506       exit (-1);
00507     }
00508   }
00509 
00510   if (pb_frame)
00511   {
00512     if (optional_custom_PCF)
00513     {
00514       trb = getbits (5);
00515     } else
00516     {
00517       trb = getbits (3);
00518     }
00519 
00520     bquant = getbits (2);
00521   } else
00522   {
00523     trb = 0;
00524   }
00525 
00526 #ifdef USE_TIME
00527   if (framerate > 0 && trd > 0)
00528     doframerate (0);
00529 #endif
00530 
00531   pei = getbits (1);
00532 
00533 pspare:
00534   if (pei)
00535   {
00536     /* extra info for possible future backward compatible additions */
00537     getbits (8);                /* not used */
00538     pei = getbits (1);
00539     if (pei)
00540       goto pspare;              /* keep on reading pspare until pei=0 */
00541   }
00542 }
00543 
00544 /* get the GOB header */
00545 /* author: <guyc@ece.ubc.ca */
00546 void getgobheader()
00547 {
00548   int BCI = 0;  
00549   /* GFID is not allowed to change unless PTYPE in picture header
00550    * changes */
00551   gfid = getbits (2);
00552   /* NB: in error-prone environments the decoder can use this
00553   * value to determine whether a picture header where the PTYPE
00554   * has changed, has been lost */
00555   /* we now use it and assume that a picture change is *
00556    * from INTRA to INTER or INTER to INTRA only */
00557   if (gfid != pgfid && newgob)
00558   {
00559     if (pict_type == PCT_INTRA)
00560       pict_type = PCT_INTER;
00561     else if (pict_type == PCT_INTER)
00562       pict_type = PCT_INTRA;
00563   }
00564   
00565   quant = getbits (5);
00566   /* Support for Annex N at the GOB level */
00567   if (reference_picture_selection_mode)
00568   {
00569     TRI = getbits (1);
00570     if (TRI)
00571     {
00572       /* Temporal reference of GOB */
00573       temp_ref = optional_custom_PCF?getbits (10):getbits(8);
00574     }
00575     /* Temporal Reference for Prediction indication bit */
00576     TRPI = getbits (1);
00577     if (TRPI)
00578     {
00579       /* temporal reference for prediction exists */
00580       temporal_reference_for_prediction = getbits (10);
00581     }
00582     if (showbits (1))
00583     {
00584       /* backward channel message exists */
00585       /* BCM(backward channel message) is not implemented */
00586       flushbits (1);
00587       exit (-1);
00588     }
00589     else
00590     {
00591       /* BCM is not present */
00592       BCI = getbits (2);
00593     }  
00594   }
00595   pgfid = gfid;
00596 }
00597 void initoptions ()
00598 {
00599   mv_outside_frame = 0;
00600   syntax_arith_coding = 0;
00601   adv_pred_mode = 0;
00602   pb_frame = 0;
00603   long_vectors = 0;
00604 
00605   plus_type = 0;
00606   optional_custom_PCF = 0;
00607   advanced_intra_coding = 0;
00608   deblocking_filter_mode = 0;
00609   slice_structured_mode = 0;
00610   reference_picture_selection_mode = 0;
00611   independently_segmented_decoding_mode = 0;
00612   alternative_inter_VLC_mode = 0;
00613   modified_quantization_mode = 0;
00614   reduced_resolution_update_mode = 0;
00615   reference_picture_resampling_mode = 0;
00616   rtype = 0;
00617 }

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