home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2004 March / PCWELT_3_2004.ISO / pcwsoft / flaskmpeg_078_39_src.z.exe / flaskmpeg / Video / gethdr.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-10-28  |  23.6 KB  |  895 lines

  1. /* gethdr.c, header decoding                                                */
  2.  
  3. /* Copyright (C) 1996, MPEG Software Simulation Group. All Rights Reserved. */
  4.  
  5. /*
  6.  * Disclaimer of Warranty
  7.  *
  8.  * These software programs are available to the user without any license fee or
  9.  * royalty on an "as is" basis.  The MPEG Software Simulation Group disclaims
  10.  * any and all warranties, whether express, implied, or statuary, including any
  11.  * implied warranties or merchantability or of fitness for a particular
  12.  * purpose.  In no event shall the copyright-holder be liable for any
  13.  * incidental, punitive, or consequential damages of any kind whatsoever
  14.  * arising from the use of these programs.
  15.  *
  16.  * This disclaimer of warranty extends to the user of these programs and user's
  17.  * customers, employees, agents, transferees, successors, and assigns.
  18.  *
  19.  * The MPEG Software Simulation Group does not represent or warrant that the
  20.  * programs furnished hereunder are free of infringement of any third-party
  21.  * patents.
  22.  *
  23.  * Commercial implementations of MPEG-1 and MPEG-2 video, including shareware,
  24.  * are subject to royalty fees to patent holders.  Many of these patents are
  25.  * general enough such that they are unavoidable regardless of implementation
  26.  * design.
  27.  *
  28.  */
  29.  
  30. #include <stdio.h>
  31.  
  32. #include "config.h"
  33. extern "C"
  34. {
  35. #include "global.h"
  36. }
  37. //#include "getbits.h"
  38.  
  39. #include "..\Video\VideoWrapper.h"
  40. extern VideoWrapper *myVideo;
  41.  
  42.  
  43. static __int64  PTS,SCR,outgoingPTS;
  44. /* private prototypes */
  45. static void sequence_header _ANSI_ARGS_((void));
  46. static void group_of_pictures_header _ANSI_ARGS_((void));
  47. static void picture_header _ANSI_ARGS_((void));
  48. static void extension_and_user_data _ANSI_ARGS_((void));
  49. static void sequence_extension _ANSI_ARGS_((void));
  50. static void sequence_display_extension _ANSI_ARGS_((void));
  51. static void quant_matrix_extension _ANSI_ARGS_((void));
  52. static void sequence_scalable_extension _ANSI_ARGS_((void));
  53. static void picture_display_extension _ANSI_ARGS_((void));
  54. static void picture_coding_extension _ANSI_ARGS_((void));
  55. static void picture_spatial_scalable_extension _ANSI_ARGS_((void));
  56. static void picture_temporal_scalable_extension _ANSI_ARGS_((void));
  57. static int  extra_bit_information _ANSI_ARGS_((void));
  58. static void copyright_extension _ANSI_ARGS_((void));
  59. static void user_data _ANSI_ARGS_((void));
  60. static void user_data _ANSI_ARGS_((void));
  61.  
  62.  
  63. #define Show_Bits(N) ( ld->Bfr >> (32-N) )
  64. #define Get_Bits1() (Get_Bits(1))
  65.  
  66.  
  67. /* introduced in September 1995 to assist spatial scalable decoding */
  68. static void Update_Temporal_Reference_Tacking_Data _ANSI_ARGS_((void));
  69. /* private variables */
  70. static int Temporal_Reference_Base = 0;
  71. static int True_Framenum_max  = -1;
  72. static int Temporal_Reference_GOP_Reset = 0;
  73.  
  74. #define RESERVED    -1 
  75. static double frame_rate_Table[16] =
  76. {
  77.   0.0,
  78.   ((23.0*1000.0)/1001.0),
  79.   24.0,
  80.   25.0,
  81.   ((30.0*1000.0)/1001.0),
  82.   30.0,
  83.   50.0,
  84.   ((60.0*1000.0)/1001.0),
  85.   60.0,
  86.  
  87.   RESERVED,
  88.   RESERVED,
  89.   RESERVED,
  90.   RESERVED,
  91.   RESERVED,
  92.   RESERVED,
  93.   RESERVED
  94. };
  95.  
  96.  
  97. /*
  98.  * decode headers from one input stream
  99.  * until an End of Sequence or picture start code
  100.  * is found
  101.  */
  102. int Get_Hdr()
  103. {
  104.   unsigned int code;
  105.  
  106.   for (;;)
  107.   {
  108.     /* look for next_start_code */
  109.     next_start_code();
  110.     code = Get_Bits32();
  111.   
  112.     switch (code)
  113.     {
  114.     case SEQUENCE_HEADER_CODE:
  115.       sequence_header();
  116.       break;
  117.     case GROUP_START_CODE:
  118.       group_of_pictures_header();
  119.       break;
  120.     case PICTURE_START_CODE:
  121.       picture_header();
  122.       return 1;
  123.       break;
  124.     case SEQUENCE_END_CODE:
  125.       return 0;
  126.       break;
  127.     default:
  128.       if (!Quiet_Flag)
  129.         fprintf(stderr,"Unexpected next_start_code %08x (ignored)\n",code);
  130.       break;
  131.     }
  132.   }
  133. }
  134.  
  135.  
  136. /* align to start of next next_start_code */
  137.  
  138. void next_start_code()
  139. {
  140.   /* byte align */
  141.   Flush_Buffer(ld->Incnt&7);
  142.   while (Show_Bits(24)!=0x01L)
  143.   //while (Show_Bits(32)!=0x000001B3)
  144.     Flush_Buffer(8);
  145. }
  146.  
  147.  
  148. /* decode sequence header */
  149.  
  150. static void sequence_header()
  151. {
  152.   int i;
  153.  
  154.   horizontal_size             = Get_Bits(12);
  155.   vertical_size               = Get_Bits(12);
  156.   aspect_ratio_information    = Get_Bits(4);
  157.   frame_rate_code             = Get_Bits(4);
  158.   bit_rate_value              = Get_Bits(18);
  159.   marker_bit("sequence_header()");
  160.   vbv_buffer_size             = Get_Bits(10);
  161.   constrained_parameters_flag = Get_Bits(1);
  162.  
  163.   if((ld->load_intra_quantizer_matrix = Get_Bits(1)))
  164.   {
  165.     for (i=0; i<64; i++)
  166.       ld->intra_quantizer_matrix[scan[ZIG_ZAG][i]] = Get_Bits(8);
  167.   }
  168.   else
  169.   {
  170.     for (i=0; i<64; i++)
  171.       ld->intra_quantizer_matrix[i] = default_intra_quantizer_matrix[i];
  172.   }
  173.  
  174.   if((ld->load_non_intra_quantizer_matrix = Get_Bits(1)))
  175.   {
  176.     for (i=0; i<64; i++)
  177.       ld->non_intra_quantizer_matrix[scan[ZIG_ZAG][i]] = Get_Bits(8);
  178.   }
  179.   else
  180.   {
  181.     for (i=0; i<64; i++)
  182.       ld->non_intra_quantizer_matrix[i] = 16;
  183.   }
  184.  
  185.   /* copy luminance to chrominance matrices */
  186.   for (i=0; i<64; i++)
  187.   {
  188.     ld->chroma_intra_quantizer_matrix[i] =
  189.       ld->intra_quantizer_matrix[i];
  190.  
  191.     ld->chroma_non_intra_quantizer_matrix[i] =
  192.       ld->non_intra_quantizer_matrix[i];
  193.   }
  194.  
  195. #ifdef VERIFY
  196.   verify_sequence_header++;
  197. #endif /* VERIFY */
  198.  
  199.   extension_and_user_data();
  200. }
  201.  
  202.  
  203.  
  204. /* decode group of pictures header */
  205. /* ISO/IEC 13818-2 section 6.2.2.6 */
  206. static void group_of_pictures_header()
  207. {
  208.   if (ld == &base)
  209.   {
  210.     Temporal_Reference_Base = True_Framenum_max + 1;     /* *CH* */
  211.     Temporal_Reference_GOP_Reset = 1;
  212.   }
  213.  
  214.   drop_flag   = Get_Bits(1);
  215.   hour        = Get_Bits(5);
  216.   minute      = Get_Bits(6);
  217.   marker_bit("group_of_pictures_header()");
  218.   sec         = Get_Bits(6);
  219.   frame       = Get_Bits(6);
  220.   closed_gop  = Get_Bits(1);
  221.   broken_link = Get_Bits(1);
  222.  
  223.   //Reseting field status
  224.   Second_Field = 0;
  225.  
  226. #ifdef VERIFY
  227.   verify_group_of_pictures_header++;
  228. #endif /* VERIFY */
  229.  
  230.   extension_and_user_data();
  231.  
  232. }
  233.  
  234.  
  235. /* decode picture header */
  236.  
  237. /* ISO/IEC 13818-2 section 6.2.3 */
  238. static void picture_header()
  239. {
  240.   int Extra_Information_Byte_Count;
  241.   static i64 nSCR, nPTS;
  242.   
  243.   // Grab timestamps RIGHT HERE.
  244.   // Make sure the startcode was inside this packet
  245.   if((ld->Rdptr - ld->Rdbfr)>4)
  246.   {
  247.     nSCR = myVideo->myPES.SCR;
  248.     nPTS = myVideo->myPES.PTS;
  249.     // And now reset the timestamps to avoid
  250.     // being used again
  251.     myVideo->myPES.PTS = 0;
  252.   }
  253.   else
  254.   {
  255.     // The timestamp if any, lays in the previous PES
  256.     // just don't put timestamp here.
  257.     nSCR = 0;
  258.     nPTS = 0;
  259.   }
  260.  
  261. #if 0
  262.   DBG_STR((str, "picture_header() - starting at %d of %d\n", ld->Rdptr - ld->Rdbfr, VIDEO_BUFFER_SIZE));
  263. #endif
  264.   /* unless later overwritten by picture_spatial_scalable_extension() */
  265.   ld->pict_scal = 0; 
  266.   
  267.  
  268.   temporal_reference  = Get_Bits(10);
  269.  
  270.   picture_coding_type = Get_Bits(3);
  271.   vbv_delay           = Get_Bits(16);
  272.  
  273.   if (picture_coding_type==P_TYPE || picture_coding_type==B_TYPE)
  274.   {
  275.     full_pel_forward_vector = Get_Bits(1);
  276.     forward_f_code = Get_Bits(3);
  277.   }
  278.   if (picture_coding_type==B_TYPE)
  279.   {
  280.     full_pel_backward_vector = Get_Bits(1);
  281.     backward_f_code = Get_Bits(3);
  282.   }
  283.  
  284.  
  285. #ifdef VERIFY
  286.   verify_picture_header++;
  287. #endif /* VERIFY */
  288.  
  289.   Extra_Information_Byte_Count = 
  290.     extra_bit_information();
  291.   
  292.   extension_and_user_data();
  293.  
  294.   // Update picture information used in the reconstruction proccess.
  295.   if (picture_coding_type==P_TYPE || picture_coding_type==I_TYPE)
  296.   {
  297.     // the backward reference is going to be output
  298.     if(picture_structure==FRAME_PICTURE || Second_Field==0){
  299.       
  300.       myVideo->p.forward = myVideo->p.backward;
  301.       
  302.       myVideo->p.backward.PTS                = nPTS;
  303.       myVideo->p.backward.SCR                = nSCR;
  304.       myVideo->p.backward.picture_coding_type= picture_coding_type;
  305.       myVideo->p.backward.progressive_frame  = progressive_frame;
  306.       myVideo->p.backward.repeat_first_field = repeat_first_field;
  307.       myVideo->p.backward.picture_structure  = picture_structure;
  308.       myVideo->p.backward.top_field_first    = top_field_first;
  309.  
  310.       myVideo->p.actual = myVideo->p.backward;
  311.     }
  312.   }
  313.   if (picture_coding_type==B_TYPE)
  314.   {
  315.     if(picture_structure==FRAME_PICTURE || Second_Field==0){
  316.       myVideo->p.actual.PTS                = nPTS;
  317.       myVideo->p.actual.SCR                = nSCR;
  318.       myVideo->p.actual.picture_coding_type= picture_coding_type;
  319.       myVideo->p.actual.progressive_frame  = progressive_frame;
  320.       myVideo->p.actual.repeat_first_field = repeat_first_field;
  321.       myVideo->p.actual.picture_structure  = picture_structure;
  322.       myVideo->p.actual.top_field_first    = top_field_first;
  323.     }
  324.   }
  325.   /* update tracking information used to assist spatial scalability */
  326.   Update_Temporal_Reference_Tacking_Data();
  327. }
  328.  
  329. /* decode slice header */
  330.  
  331. /* ISO/IEC 13818-2 section 6.2.4 */
  332. int slice_header()
  333. {
  334.   int slice_vertical_position_extension;
  335.   int quantizer_scale_code;
  336.   int slice_picture_id_enable = 0;
  337.   int slice_picture_id = 0;
  338.   int extra_information_slice = 0;
  339.  
  340.  
  341.   slice_vertical_position_extension =
  342.     (ld->MPEG2_Flag && vertical_size>2800) ? Get_Bits(3) : 0;
  343.  
  344.   if (ld->scalable_mode==SC_DP)
  345.     ld->priority_breakpoint = Get_Bits(7);
  346.  
  347.   quantizer_scale_code = Get_Bits(5);
  348.   ld->quantizer_scale =
  349.     ld->MPEG2_Flag ? (ld->q_scale_type ? Non_Linear_quantizer_scale[quantizer_scale_code] : quantizer_scale_code<<1) : quantizer_scale_code;
  350.  
  351.   /* slice_id introduced in March 1995 as part of the video corridendum
  352.      (after the IS was drafted in November 1994) */
  353.   if (Get_Bits(1))
  354.   {
  355.     ld->intra_slice = Get_Bits(1);
  356.  
  357.     slice_picture_id_enable = Get_Bits(1);
  358.     slice_picture_id = Get_Bits(6);
  359.  
  360.     extra_information_slice = extra_bit_information();
  361.   }
  362.   else
  363.     ld->intra_slice = 0;
  364.  
  365.  
  366. #ifdef VERIFY
  367.   verify_slice_header++;
  368. #endif /* VERIFY */
  369.  
  370.  
  371.   return slice_vertical_position_extension;
  372. }
  373.  
  374.  
  375. /* decode extension and user data */
  376. /* ISO/IEC 13818-2 section 6.2.2.2 */
  377. static void extension_and_user_data()
  378. {
  379.   int code,ext_ID;
  380.  
  381.   next_start_code();
  382.  
  383.   while ((code = Show_Bits(32))==EXTENSION_START_CODE || code==USER_DATA_START_CODE)
  384.   {
  385.     if (code==EXTENSION_START_CODE)
  386.     {
  387.       Flush_Buffer32();
  388.       ext_ID = Get_Bits(4);
  389.       switch (ext_ID)
  390.       {
  391.       case SEQUENCE_EXTENSION_ID:
  392.         sequence_extension();
  393.         break;
  394.       case SEQUENCE_DISPLAY_EXTENSION_ID:
  395.         sequence_display_extension();
  396.         break;
  397.       case QUANT_MATRIX_EXTENSION_ID:
  398.         quant_matrix_extension();
  399.         break;
  400.       case SEQUENCE_SCALABLE_EXTENSION_ID:
  401.         sequence_scalable_extension();
  402.         break;
  403.       case PICTURE_DISPLAY_EXTENSION_ID:
  404.         picture_display_extension();
  405.         break;
  406.       case PICTURE_CODING_EXTENSION_ID:
  407.         picture_coding_extension();
  408.         break;
  409.       case PICTURE_SPATIAL_SCALABLE_EXTENSION_ID:
  410.         picture_spatial_scalable_extension();
  411.         break;
  412.       case PICTURE_TEMPORAL_SCALABLE_EXTENSION_ID:
  413.         picture_temporal_scalable_extension();
  414.         break;
  415.       case COPYRIGHT_EXTENSION_ID:
  416.         copyright_extension();
  417.         break;
  418.      default:
  419.         fprintf(stderr,"reserved extension start code ID %d\n",ext_ID);
  420.         break;
  421.       }
  422.       next_start_code();
  423.     }
  424.     else
  425.     {
  426. #ifdef VERBOSE
  427.       if (Verbose_Flag>NO_LAYER)
  428.         printf("user data\n");
  429. #endif /* VERBOSE */
  430.       Flush_Buffer32();
  431.       user_data();
  432.     }
  433.   }
  434. }
  435.  
  436.  
  437. /* decode sequence extension */
  438.  
  439. /* ISO/IEC 13818-2 section 6.2.2.3 */
  440. static void sequence_extension()
  441. {
  442.   int horizontal_size_extension;
  443.   int vertical_size_extension;
  444.   int bit_rate_extension;
  445.   int vbv_buffer_size_extension;
  446.  
  447.  
  448.   /* derive bit position for trace */
  449. #ifdef VERBOSE
  450.   pos = ld->Bitcnt;
  451. #endif
  452.  
  453.   ld->MPEG2_Flag = 1;
  454.  
  455.   ld->scalable_mode = SC_NONE; /* unless overwritten by sequence_scalable_extension() */
  456.   layer_id = 0;                /* unless overwritten by sequence_scalable_extension() */
  457.   
  458.   profile_and_level_indication = Get_Bits(8);
  459.   progressive_sequence         = Get_Bits(1);
  460.   chroma_format                = Get_Bits(2);
  461.   horizontal_size_extension    = Get_Bits(2);
  462.   vertical_size_extension      = Get_Bits(2);
  463.   bit_rate_extension           = Get_Bits(12);
  464.   marker_bit("sequence_extension");
  465.   vbv_buffer_size_extension    = Get_Bits(8);
  466.   low_delay                    = Get_Bits(1);
  467.   frame_rate_extension_n       = Get_Bits(2);
  468.   frame_rate_extension_d       = Get_Bits(5);
  469.  
  470.   frame_rate = frame_rate_Table[frame_rate_code] *
  471.     ((frame_rate_extension_n+1)/(frame_rate_extension_d+1));
  472.  
  473.   /* special case for 422 profile & level must be made */
  474.   if((profile_and_level_indication>>7) & 1)
  475.   {  /* escape bit of profile_and_level_indication set */
  476.   
  477.     /* 4:2:2 Profile @ Main Level */
  478.     if((profile_and_level_indication&15)==5)
  479.     {
  480.       profile = PROFILE_422;
  481.       level   = MAIN_LEVEL;  
  482.     }
  483.   }
  484.   else
  485.   {
  486.     profile = profile_and_level_indication >> 4;  /* Profile is upper nibble */
  487.     level   = profile_and_level_indication & 0xF;  /* Level is lower nibble */
  488.   }
  489.   
  490.  
  491.   horizontal_size = (horizontal_size_extension<<12) | (horizontal_size&0x0fff);
  492.   vertical_size = (vertical_size_extension<<12) | (vertical_size&0x0fff);
  493.  
  494.  
  495.   /* ISO/IEC 13818-2 does not define bit_rate_value to be composed of
  496.    * both the original bit_rate_value parsed in sequence_header() and
  497.    * the optional bit_rate_extension in sequence_extension_header(). 
  498.    * However, we use it for bitstream verification purposes. 
  499.    */
  500.  
  501.   bit_rate_value += (bit_rate_extension << 18);
  502.   bit_rate = ((double) bit_rate_value) * 400.0;
  503.   vbv_buffer_size += (vbv_buffer_size_extension << 10);
  504.  
  505. #ifdef VERBOSE
  506.   if (Verbose_Flag>NO_LAYER)
  507.   {
  508.     printf("sequence extension (byte %d)\n",(pos>>3)-4);
  509.  
  510.     if (Verbose_Flag>SEQUENCE_LAYER)
  511.     {
  512.       printf("  profile_and_level_indication=%d\n",profile_and_level_indication);
  513.  
  514.       if (profile_and_level_indication<128)
  515.       {
  516.         printf("    profile=%d, level=%d\n",profile,level);
  517.       }
  518.  
  519.       printf("  progressive_sequence=%d\n",progressive_sequence);
  520.       printf("  chroma_format=%d\n",chroma_format);
  521.       printf("  horizontal_size_extension=%d\n",horizontal_size_extension);
  522.       printf("  vertical_size_extension=%d\n",vertical_size_extension);
  523.       printf("  bit_rate_extension=%d\n",bit_rate_extension);
  524.       printf("  vbv_buffer_size_extension=%d\n",vbv_buffer_size_extension);
  525.       printf("  low_delay=%d\n",low_delay);
  526.       printf("  frame_rate_extension_n=%d\n",frame_rate_extension_n);
  527.       printf("  frame_rate_extension_d=%d\n",frame_rate_extension_d);
  528.     }
  529.   }
  530. #endif /* VERBOSE */
  531.  
  532. #ifdef VERIFY
  533.   verify_sequence_extension++;
  534. #endif /* VERIFY */
  535.  
  536.  
  537. }
  538.  
  539.  
  540. /* decode sequence display extension */
  541.  
  542. static void sequence_display_extension()
  543. {
  544.  
  545.   video_format      = Get_Bits(3);
  546.   color_description = Get_Bits(1);
  547.  
  548.   if (color_description)
  549.   {
  550.     color_primaries          = Get_Bits(8);
  551.     transfer_characteristics = Get_Bits(8);
  552.     matrix_coefficients      = Get_Bits(8);
  553.   }
  554.  
  555.   display_horizontal_size = Get_Bits(14);
  556.   marker_bit("sequence_display_extension");
  557.   display_vertical_size   = Get_Bits(14);
  558.  
  559. #ifdef VERIFY
  560.   verify_sequence_display_extension++;
  561. #endif /* VERIFY */
  562.  
  563. }
  564.  
  565.  
  566. /* decode quant matrix entension */
  567. /* ISO/IEC 13818-2 section 6.2.3.2 */
  568. static void quant_matrix_extension()
  569. {
  570.   int i;
  571.  
  572.   if((ld->load_intra_quantizer_matrix = Get_Bits(1)))
  573.   {
  574.     for (i=0; i<64; i++)
  575.     {
  576.       ld->chroma_intra_quantizer_matrix[scan[ZIG_ZAG][i]]
  577.       = ld->intra_quantizer_matrix[scan[ZIG_ZAG][i]]
  578.       = Get_Bits(8);
  579.     }
  580.   }
  581.  
  582.   if((ld->load_non_intra_quantizer_matrix = Get_Bits(1)))
  583.   {
  584.     for (i=0; i<64; i++)
  585.     {
  586.       ld->chroma_non_intra_quantizer_matrix[scan[ZIG_ZAG][i]]
  587.       = ld->non_intra_quantizer_matrix[scan[ZIG_ZAG][i]]
  588.       = Get_Bits(8);
  589.     }
  590.   }
  591.  
  592.   if((ld->load_chroma_intra_quantizer_matrix = Get_Bits(1)))
  593.   {
  594.     for (i=0; i<64; i++)
  595.       ld->chroma_intra_quantizer_matrix[scan[ZIG_ZAG][i]] = Get_Bits(8);
  596.   }
  597.  
  598.   if((ld->load_chroma_non_intra_quantizer_matrix = Get_Bits(1)))
  599.   {
  600.     for (i=0; i<64; i++)
  601.       ld->chroma_non_intra_quantizer_matrix[scan[ZIG_ZAG][i]] = Get_Bits(8);
  602.   }
  603.  
  604.  
  605. #ifdef VERIFY
  606.   verify_quant_matrix_extension++;
  607. #endif /* VERIFY */
  608.  
  609. }
  610.  
  611.  
  612. /* decode sequence scalable extension */
  613. /* ISO/IEC 13818-2   section 6.2.2.5 */
  614. static void sequence_scalable_extension()
  615. {
  616.  
  617.   /* values (without the +1 offset) of scalable_mode are defined in 
  618.      Table 6-10 of ISO/IEC 13818-2 */
  619.   ld->scalable_mode = Get_Bits(2) + 1; /* add 1 to make SC_DP != SC_NONE */
  620.  
  621.   layer_id = Get_Bits(4);
  622.  
  623.   if (ld->scalable_mode==SC_SPAT)
  624.   {
  625.     lower_layer_prediction_horizontal_size = Get_Bits(14);
  626.     marker_bit("sequence_scalable_extension()");
  627.     lower_layer_prediction_vertical_size   = Get_Bits(14); 
  628.     horizontal_subsampling_factor_m        = Get_Bits(5);
  629.     horizontal_subsampling_factor_n        = Get_Bits(5);
  630.     vertical_subsampling_factor_m          = Get_Bits(5);
  631.     vertical_subsampling_factor_n          = Get_Bits(5);
  632.   }
  633.  
  634.   if (ld->scalable_mode==SC_TEMP)
  635.     Error("temporal scalability not implemented\n");
  636.  
  637. #ifdef VERIFY
  638.   verify_sequence_scalable_extension++;
  639. #endif /* VERIFY */
  640.  
  641. }
  642.  
  643.  
  644. /* decode picture display extension */
  645. /* ISO/IEC 13818-2 section 6.2.3.3. */
  646. static void picture_display_extension()
  647. {
  648.   int i;
  649.   int number_of_frame_center_offsets;
  650.  
  651.   /* based on ISO/IEC 13818-2 section 6.3.12 
  652.     (November 1994) Picture display extensions */
  653.  
  654.   /* derive number_of_frame_center_offsets */
  655.   if(progressive_sequence)
  656.   {
  657.     if(repeat_first_field)
  658.     {
  659.       if(top_field_first)
  660.         number_of_frame_center_offsets = 3;
  661.       else
  662.         number_of_frame_center_offsets = 2;
  663.     }
  664.     else
  665.     {
  666.       number_of_frame_center_offsets = 1;
  667.     }
  668.   }
  669.   else
  670.   {
  671.     if(picture_structure!=FRAME_PICTURE)
  672.     {
  673.       number_of_frame_center_offsets = 1;
  674.     }
  675.     else
  676.     {
  677.       if(repeat_first_field)
  678.         number_of_frame_center_offsets = 3;
  679.       else
  680.         number_of_frame_center_offsets = 2;
  681.     }
  682.   }
  683.  
  684.  
  685.   /* now parse */
  686.   for (i=0; i<number_of_frame_center_offsets; i++)
  687.   {
  688.     frame_center_horizontal_offset[i] = Get_Bits(16);
  689.     marker_bit("picture_display_extension, first marker bit");
  690.     
  691.     frame_center_vertical_offset[i]   = Get_Bits(16);
  692.     marker_bit("picture_display_extension, second marker bit");
  693.   }
  694.  
  695.  
  696. #ifdef VERIFY
  697.   verify_picture_display_extension++;
  698. #endif /* VERIFY */
  699.  
  700. }
  701.  
  702.  
  703. /* decode picture coding extension */
  704. static void picture_coding_extension()
  705. {
  706.  
  707.   f_code[0][0] = Get_Bits(4);
  708.   f_code[0][1] = Get_Bits(4);
  709.   f_code[1][0] = Get_Bits(4);
  710.   f_code[1][1] = Get_Bits(4);
  711.  
  712.   intra_dc_precision         = Get_Bits(2);
  713.   picture_structure          = Get_Bits(2);
  714.   top_field_first            = Get_Bits(1);
  715.   frame_pred_frame_dct       = Get_Bits(1);
  716.   concealment_motion_vectors = Get_Bits(1);
  717.   ld->q_scale_type           = Get_Bits(1);
  718.   intra_vlc_format           = Get_Bits(1);
  719.   ld->alternate_scan         = Get_Bits(1);
  720.   repeat_first_field         = Get_Bits(1);
  721.   chroma_420_type            = Get_Bits(1);
  722.   progressive_frame          = Get_Bits(1);
  723.   composite_display_flag     = Get_Bits(1);
  724.  
  725.   if (composite_display_flag)
  726.   {
  727.     v_axis            = Get_Bits(1);
  728.     field_sequence    = Get_Bits(3);
  729.     sub_carrier       = Get_Bits(1);
  730.     burst_amplitude   = Get_Bits(7);
  731.     sub_carrier_phase = Get_Bits(8);
  732.   }
  733.  
  734. #ifdef VERIFY
  735.   verify_picture_coding_extension++;
  736. #endif /* VERIFY */
  737. }
  738.  
  739.  
  740. /* decode picture spatial scalable extension */
  741. /* ISO/IEC 13818-2 section 6.2.3.5. */
  742. static void picture_spatial_scalable_extension()
  743. {
  744.   ld->pict_scal = 1; /* use spatial scalability in this picture */
  745.  
  746.   lower_layer_temporal_reference = Get_Bits(10);
  747.   marker_bit("picture_spatial_scalable_extension(), first marker bit");
  748.   lower_layer_horizontal_offset = Get_Bits(15);
  749.   if (lower_layer_horizontal_offset>=16384)
  750.     lower_layer_horizontal_offset-= 32768;
  751.   marker_bit("picture_spatial_scalable_extension(), second marker bit");
  752.   lower_layer_vertical_offset = Get_Bits(15);
  753.   if (lower_layer_vertical_offset>=16384)
  754.     lower_layer_vertical_offset-= 32768;
  755.   spatial_temporal_weight_code_table_index = Get_Bits(2);
  756.   lower_layer_progressive_frame = Get_Bits(1);
  757.   lower_layer_deinterlaced_field_select = Get_Bits(1);
  758.  
  759. #ifdef VERIFY
  760.   verify_picture_spatial_scalable_extension++;
  761. #endif /* VERIFY */
  762.  
  763. }
  764.  
  765.  
  766. /* decode picture temporal scalable extension
  767.  *
  768.  * not implemented
  769.  */
  770. /* ISO/IEC 13818-2 section 6.2.3.4. */
  771. static void picture_temporal_scalable_extension()
  772. {
  773.   Error("temporal scalability not supported\n");
  774.  
  775. #ifdef VERIFY
  776.   verify_picture_temporal_scalable_extension++;
  777. #endif /* VERIFY */
  778. }
  779.  
  780.  
  781. /* decode extra bit information */
  782. /* ISO/IEC 13818-2 section 6.2.3.4. */
  783. static int extra_bit_information()
  784. {
  785.   int Byte_Count = 0;
  786.  
  787.   while (Get_Bits1())
  788.   {
  789.     Flush_Buffer(8);
  790.     Byte_Count++;
  791.   }
  792.  
  793.   return(Byte_Count);
  794. }
  795.  
  796.  
  797.  
  798. /* ISO/IEC 13818-2 section 5.3 */
  799. /* Purpose: this function is mainly designed to aid in bitstream conformance
  800.    testing.  A simple Flush_Buffer(1) would do */
  801. void marker_bit(char *text)
  802. {
  803.   int marker;
  804.  
  805.   marker = Get_Bits(1);
  806.  
  807. #ifdef VERIFY  
  808.   if(!marker)
  809.     printf("ERROR: %s--marker_bit set to 0",text);
  810. #endif
  811. }
  812.  
  813.  
  814. /* ISO/IEC 13818-2  sections 6.3.4.1 and 6.2.2.2.2 */
  815. static void user_data()
  816. {
  817.   /* skip ahead to the next start code */
  818.   next_start_code();
  819. }
  820.  
  821.  
  822.  
  823. /* Copyright extension */
  824. /* ISO/IEC 13818-2 section 6.2.3.6. */
  825. /* (header added in November, 1994 to the IS document) */
  826.  
  827.  
  828. static void copyright_extension()
  829. {
  830.  
  831.   int reserved_data;
  832.  
  833.   
  834.  
  835.   copyright_flag =       Get_Bits(1); 
  836.   copyright_identifier = Get_Bits(8);
  837.   original_or_copy =     Get_Bits(1);
  838.   
  839.   /* reserved */
  840.   reserved_data = Get_Bits(7);
  841.  
  842.   marker_bit("copyright_extension(), first marker bit");
  843.   copyright_number_1 =   Get_Bits(20);
  844.   marker_bit("copyright_extension(), second marker bit");
  845.   copyright_number_2 =   Get_Bits(22);
  846.   marker_bit("copyright_extension(), third marker bit");
  847.   copyright_number_3 =   Get_Bits(22);
  848.  
  849.  
  850. #ifdef VERIFY
  851.   verify_copyright_extension++;
  852. #endif /* VERIFY */
  853. }
  854.  
  855.  
  856.  
  857. /* introduced in September 1995 to assist Spatial Scalability */
  858. static void Update_Temporal_Reference_Tacking_Data()
  859. {
  860.   static int temporal_reference_wrap  = 0;
  861.   static int temporal_reference_old   = 0;
  862.  
  863.   if (ld == &base)            /* *CH* */
  864.   {
  865.     if (picture_coding_type!=B_TYPE && temporal_reference!=temporal_reference_old)     
  866.     /* check first field of */
  867.     {                            
  868.        /* non-B-frame */
  869.       if (temporal_reference_wrap)         
  870.       {/* wrap occured at previous I- or P-frame */    
  871.        /* now all intervening B-frames which could 
  872.           still have high temporal_reference values are done  */
  873.         Temporal_Reference_Base += 1024;
  874.         temporal_reference_wrap = 0;
  875.       }
  876.       
  877.       /* distinguish from a reset */
  878.       if (temporal_reference<temporal_reference_old && !Temporal_Reference_GOP_Reset)    
  879.         temporal_reference_wrap = 1;  /* we must have just passed a GOP-Header! */
  880.       
  881.       temporal_reference_old = temporal_reference;
  882.       Temporal_Reference_GOP_Reset = 0;
  883.     }
  884.  
  885.     True_Framenum = Temporal_Reference_Base + temporal_reference;
  886.     
  887.     /* temporary wrap of TR at 1024 for M frames */
  888.     if (temporal_reference_wrap && temporal_reference <= temporal_reference_old)    
  889.       True_Framenum += 1024;                
  890.  
  891.     True_Framenum_max = (True_Framenum > True_Framenum_max) ?
  892.                         True_Framenum : True_Framenum_max;
  893.   }
  894. }
  895.