gethdr.c
上传用户:hkgotone
上传日期:2013-02-17
资源大小:293k
文件大小:29k
源码类别:

Windows Mobile

开发平台:

C/C++

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