sei.c
上传用户:hjq518
上传日期:2021-12-09
资源大小:5084k
文件大小:73k
源码类别:

Audio

开发平台:

Visual C++

  1. /*!
  2.  ************************************************************************
  3.  * file  sei.c
  4.  *
  5.  * brief
  6.  *    Functions to implement SEI messages
  7.  *
  8.  * author
  9.  *    Main contributors (see contributors.h for copyright, address and affiliation details)
  10.  *    - Dong Tian        <tian@cs.tut.fi>
  11.  *    - Karsten Suehring <suehring@hhi.de>
  12.  ************************************************************************
  13.  */
  14. #include "contributors.h"
  15. #include <math.h>
  16. #include "global.h"
  17. #include "memalloc.h"
  18. #include "sei.h"
  19. #include "vlc.h"
  20. #include "header.h"
  21. #include "mbuffer.h"
  22. #include "parset.h"
  23. extern int UsedBits;
  24. extern seq_parameter_set_rbsp_t SeqParSet[MAXSPS];
  25. #if (ENABLE_OUTPUT_TONEMAPPING)
  26. tone_mapping_struct seiToneMapping;
  27. #endif
  28. // #define PRINT_BUFFERING_PERIOD_INFO    // uncomment to print buffering period SEI info
  29. // #define PRINT_PCITURE_TIMING_INFO      // uncomment to print picture timing SEI info
  30. // #define WRITE_MAP_IMAGE                // uncomment to write spare picture map
  31. // #define PRINT_SUBSEQUENCE_INFO         // uncomment to print sub-sequence SEI info
  32. // #define PRINT_SUBSEQUENCE_LAYER_CHAR   // uncomment to print sub-sequence layer characteristics SEI info
  33. // #define PRINT_SUBSEQUENCE_CHAR         // uncomment to print sub-sequence characteristics SEI info
  34. // #define PRINT_SCENE_INFORMATION        // uncomment to print scene information SEI info
  35. // #define PRINT_PAN_SCAN_RECT            // uncomment to print pan-scan rectangle SEI info
  36. // #define PRINT_RECOVERY_POINT            // uncomment to print random access point SEI info
  37. // #define PRINT_FILLER_PAYLOAD_INFO      // uncomment to print filler payload SEI info
  38. // #define PRINT_DEC_REF_PIC_MARKING      // uncomment to print decoded picture buffer management repetition SEI info
  39. // #define PRINT_RESERVED_INFO            // uncomment to print reserved SEI info
  40. // #define PRINT_USER_DATA_UNREGISTERED_INFO          // uncomment to print unregistered user data SEI info
  41. // #define PRINT_USER_DATA_REGISTERED_ITU_T_T35_INFO  // uncomment to print ITU-T T.35 user data SEI info
  42. // #define PRINT_FULL_FRAME_FREEZE_INFO               // uncomment to print full-frame freeze SEI info
  43. // #define PRINT_FULL_FRAME_FREEZE_RELEASE_INFO       // uncomment to print full-frame freeze release SEI info
  44. // #define PRINT_FULL_FRAME_SNAPSHOT_INFO             // uncomment to print full-frame snapshot SEI info
  45. // #define PRINT_PROGRESSIVE_REFINEMENT_END_INFO      // uncomment to print Progressive refinement segment start SEI info
  46. // #define PRINT_PROGRESSIVE_REFINEMENT_END_INFO      // uncomment to print Progressive refinement segment end SEI info
  47. // #define PRINT_MOTION_CONST_SLICE_GROUP_SET_INFO    // uncomment to print Motion-constrained slice group set SEI info
  48. // #define PRINT_FILM_GRAIN_CHARACTERISTICS_INFO      // uncomment to print Film grain characteristics SEI info
  49. // #define PRINT_DEBLOCKING_FILTER_DISPLAY_PREFERENCE_INFO // uncomment to print deblocking filter display preference SEI info
  50. // #define PRINT_STEREO_VIDEO_INFO_INFO               // uncomment to print stero video SEI info
  51. // #define PRINT_TONE_MAPPING                         // uncomment to print tone-mapping SEI info
  52. // #define PRINT_POST_FILTER_HINT_INFO                // uncomment to print post-filter hint SEI info
  53. /*!
  54.  ************************************************************************
  55.  *  brief
  56.  *     Interpret the SEI rbsp
  57.  *  param msg
  58.  *     a pointer that point to the sei message.
  59.  *  param size
  60.  *     the size of the sei message
  61.  *  param img
  62.  *     the image pointer
  63.  *
  64.  ************************************************************************
  65.  */
  66. void InterpretSEIMessage(byte* msg, int size, ImageParameters *img)
  67. {
  68.   int payload_type = 0;
  69.   int payload_size = 0;
  70.   int offset = 1;
  71.   byte tmp_byte;
  72.   do
  73.   {
  74.     // sei_message();
  75.     payload_type = 0;
  76.     tmp_byte = msg[offset++];
  77.     while (tmp_byte == 0xFF)
  78.     {
  79.       payload_type += 255;
  80.       tmp_byte = msg[offset++];
  81.     }
  82.     payload_type += tmp_byte;   // this is the last byte
  83.     payload_size = 0;
  84.     tmp_byte = msg[offset++];
  85.     while (tmp_byte == 0xFF)
  86.     {
  87.       payload_size += 255;
  88.       tmp_byte = msg[offset++];
  89.     }
  90.     payload_size += tmp_byte;   // this is the last byte
  91.     switch ( payload_type )     // sei_payload( type, size );
  92.     {
  93.     case  SEI_BUFFERING_PERIOD:
  94.       interpret_buffering_period_info( msg+offset, payload_size, img );
  95.       break;
  96.     case  SEI_PIC_TIMING:
  97.       interpret_picture_timing_info( msg+offset, payload_size, img );
  98.       break;
  99.     case  SEI_PAN_SCAN_RECT:
  100.       interpret_pan_scan_rect_info( msg+offset, payload_size, img );
  101.       break;
  102.     case  SEI_FILLER_PAYLOAD:
  103.       interpret_filler_payload_info( msg+offset, payload_size, img );
  104.       break;
  105.     case  SEI_USER_DATA_REGISTERED_ITU_T_T35:
  106.       interpret_user_data_registered_itu_t_t35_info( msg+offset, payload_size, img );
  107.       break;
  108.     case  SEI_USER_DATA_UNREGISTERED:
  109.       interpret_user_data_unregistered_info( msg+offset, payload_size, img );
  110.       break;
  111.     case  SEI_RECOVERY_POINT:
  112.       interpret_recovery_point_info( msg+offset, payload_size, img );
  113.       break;
  114.     case  SEI_DEC_REF_PIC_MARKING_REPETITION:
  115.       interpret_dec_ref_pic_marking_repetition_info( msg+offset, payload_size, img );
  116.       break;
  117.     case  SEI_SPARE_PIC:
  118.       interpret_spare_pic( msg+offset, payload_size, img );
  119.       break;
  120.     case  SEI_SCENE_INFO:
  121.       interpret_scene_information( msg+offset, payload_size, img );
  122.       break;
  123.     case  SEI_SUB_SEQ_INFO:
  124.       interpret_subsequence_info( msg+offset, payload_size, img );
  125.       break;
  126.     case  SEI_SUB_SEQ_LAYER_CHARACTERISTICS:
  127.       interpret_subsequence_layer_characteristics_info( msg+offset, payload_size, img );
  128.       break;
  129.     case  SEI_SUB_SEQ_CHARACTERISTICS:
  130.       interpret_subsequence_characteristics_info( msg+offset, payload_size, img );
  131.       break;
  132.     case  SEI_FULL_FRAME_FREEZE:
  133.       interpret_full_frame_freeze_info( msg+offset, payload_size, img );
  134.       break;
  135.     case  SEI_FULL_FRAME_FREEZE_RELEASE:
  136.       interpret_full_frame_freeze_release_info( msg+offset, payload_size, img );
  137.       break;
  138.     case  SEI_FULL_FRAME_SNAPSHOT:
  139.       interpret_full_frame_snapshot_info( msg+offset, payload_size, img );
  140.       break;
  141.     case  SEI_PROGRESSIVE_REFINEMENT_SEGMENT_START:
  142.       interpret_progressive_refinement_start_info( msg+offset, payload_size, img );
  143.       break;
  144.     case  SEI_PROGRESSIVE_REFINEMENT_SEGMENT_END:
  145.       interpret_progressive_refinement_end_info( msg+offset, payload_size, img );
  146.       break;
  147.     case  SEI_MOTION_CONSTRAINED_SLICE_GROUP_SET:
  148.       interpret_motion_constrained_slice_group_set_info( msg+offset, payload_size, img );
  149.     case  SEI_FILM_GRAIN_CHARACTERISTICS:
  150.       interpret_film_grain_characteristics_info ( msg+offset, payload_size, img );
  151.       break;
  152.     case  SEI_DEBLOCKING_FILTER_DISPLAY_PREFERENCE:
  153.       interpret_deblocking_filter_display_preference_info ( msg+offset, payload_size, img );
  154.       break;
  155.     case  SEI_STEREO_VIDEO_INFO:
  156.       interpret_stereo_video_info_info ( msg+offset, payload_size, img );
  157.       break;
  158.     case SEI_TONE_MAPPING:
  159.       interpret_tone_mapping( msg+offset, payload_size, img );
  160.       break;
  161.     case SEI_POST_FILTER_HINTS:
  162.       interpret_post_filter_hints_info ( msg+offset, payload_size, img );
  163.     default:
  164.       interpret_reserved_info( msg+offset, payload_size, img );
  165.       break;
  166.     }
  167.     offset += payload_size;
  168.   } while( msg[offset] != 0x80 );    // more_rbsp_data()  msg[offset] != 0x80
  169.   // ignore the trailing bits rbsp_trailing_bits();
  170.   assert(msg[offset] == 0x80);      // this is the trailing bits
  171.   assert( offset+1 == size );
  172. }
  173. /*!
  174. ************************************************************************
  175. *  brief
  176. *     Interpret the spare picture SEI message
  177. *  param payload
  178. *     a pointer that point to the sei payload
  179. *  param size
  180. *     the size of the sei message
  181. *  param img
  182. *     the image pointer
  183. *
  184. ************************************************************************
  185. */
  186. void interpret_spare_pic( byte* payload, int size, ImageParameters *img )
  187. {
  188.   int i,x,y;
  189.   Bitstream* buf;
  190.   int bit0, bit1, bitc, no_bit0;
  191.   int target_frame_num = 0;
  192.   int num_spare_pics;
  193.   int delta_spare_frame_num, CandidateSpareFrameNum, SpareFrameNum = 0;
  194.   int ref_area_indicator;
  195.   int m, n, left, right, top, bottom,directx, directy;
  196.   byte ***map;
  197. #ifdef WRITE_MAP_IMAGE
  198.   int symbol_size_in_bytes = img->pic_unit_bitsize_on_disk/8;
  199.   int  j, k, i0, j0, tmp, kk;
  200.   char filename[20] = "map_dec.yuv";
  201.   FILE *fp;
  202.   imgpel** Y;
  203.   static int old_pn=-1;
  204.   static int first = 1;
  205.   printf("Spare picture SEI messagen");
  206. #endif
  207.   UsedBits = 0;
  208.   assert( payload!=NULL);
  209.   assert( img!=NULL);
  210.   buf = malloc(sizeof(Bitstream));
  211.   buf->bitstream_length = size;
  212.   buf->streamBuffer = payload;
  213.   buf->frame_bitoffset = 0;
  214.   target_frame_num = ue_v("SEI: target_frame_num", buf);
  215. #ifdef WRITE_MAP_IMAGE
  216.   printf( "target_frame_num is %dn", target_frame_num );
  217. #endif
  218.   num_spare_pics = 1 + ue_v("SEI: num_spare_pics_minus1", buf);
  219. #ifdef WRITE_MAP_IMAGE
  220.   printf( "num_spare_pics is %dn", num_spare_pics );
  221. #endif
  222.   get_mem3D(&map, num_spare_pics, img->height/16, img->width/16);
  223.   for (i=0; i<num_spare_pics; i++)
  224.   {
  225.     if (i==0)
  226.     {
  227.       CandidateSpareFrameNum = target_frame_num - 1;
  228.       if ( CandidateSpareFrameNum < 0 ) CandidateSpareFrameNum = MAX_FN - 1;
  229.     }
  230.     else
  231.       CandidateSpareFrameNum = SpareFrameNum;
  232.     delta_spare_frame_num = ue_v("SEI: delta_spare_frame_num", buf);
  233.     SpareFrameNum = CandidateSpareFrameNum - delta_spare_frame_num;
  234.     if( SpareFrameNum < 0 )
  235.       SpareFrameNum = MAX_FN + SpareFrameNum;
  236.     ref_area_indicator = ue_v("SEI: ref_area_indicator", buf);
  237.     switch ( ref_area_indicator )
  238.     {
  239.     case 0:   // The whole frame can serve as spare picture
  240.       for (y=0; y<img->height/16; y++)
  241.         for (x=0; x<img->width/16; x++)
  242.           map[i][y][x] = 0;
  243.       break;
  244.     case 1:   // The map is not compressed
  245.       for (y=0; y<img->height/16; y++)
  246.         for (x=0; x<img->width/16; x++)
  247.         {
  248.           map[i][y][x] = u_1("SEI: ref_mb_indicator", buf);
  249.         }
  250.       break;
  251.     case 2:   // The map is compressed
  252.               //!KS: could not check this function, description is unclear (as stated in Ed. Note)
  253.       bit0 = 0;
  254.       bit1 = 1;
  255.       bitc = bit0;
  256.       no_bit0 = -1;
  257.       x = ( img->width/16 - 1 ) / 2;
  258.       y = ( img->height/16 - 1 ) / 2;
  259.       left = right = x;
  260.       top = bottom = y;
  261.       directx = 0;
  262.       directy = 1;
  263.       for (m=0; m<img->height/16; m++)
  264.         for (n=0; n<img->width/16; n++)
  265.         {
  266.           if (no_bit0<0)
  267.           {
  268.             no_bit0 = ue_v("SEI: zero_run_length", buf);
  269.           }
  270.           if (no_bit0>0) 
  271.             map[i][y][x] = (byte) bit0;
  272.           else 
  273.             map[i][y][x] = (byte) bit1;
  274.           no_bit0--;
  275.           // go to the next mb:
  276.           if ( directx == -1 && directy == 0 )
  277.           {
  278.             if (x > left) x--;
  279.             else if (x == 0)
  280.             {
  281.               y = bottom + 1;
  282.               bottom++;
  283.               directx = 1;
  284.               directy = 0;
  285.             }
  286.             else if (x == left)
  287.             {
  288.               x--;
  289.               left--;
  290.               directx = 0;
  291.               directy = 1;
  292.             }
  293.           }
  294.           else if ( directx == 1 && directy == 0 )
  295.           {
  296.             if (x < right) x++;
  297.             else if (x == img->width/16 - 1)
  298.             {
  299.               y = top - 1;
  300.               top--;
  301.               directx = -1;
  302.               directy = 0;
  303.             }
  304.             else if (x == right)
  305.             {
  306.               x++;
  307.               right++;
  308.               directx = 0;
  309.               directy = -1;
  310.             }
  311.           }
  312.           else if ( directx == 0 && directy == -1 )
  313.           {
  314.             if ( y > top) y--;
  315.             else if (y == 0)
  316.             {
  317.               x = left - 1;
  318.               left--;
  319.               directx = 0;
  320.               directy = 1;
  321.             }
  322.             else if (y == top)
  323.             {
  324.               y--;
  325.               top--;
  326.               directx = -1;
  327.               directy = 0;
  328.             }
  329.           }
  330.           else if ( directx == 0 && directy == 1 )
  331.           {
  332.             if (y < bottom) y++;
  333.             else if (y == img->height/16 - 1)
  334.             {
  335.               x = right+1;
  336.               right++;
  337.               directx = 0;
  338.               directy = -1;
  339.             }
  340.             else if (y == bottom)
  341.             {
  342.               y++;
  343.               bottom++;
  344.               directx = 1;
  345.               directy = 0;
  346.             }
  347.           }
  348.         }
  349.       break;
  350.     default:
  351.       printf( "Wrong ref_area_indicator %d!n", ref_area_indicator );
  352.       exit(0);
  353.       break;
  354.     }
  355.   } // end of num_spare_pics
  356. #ifdef WRITE_MAP_IMAGE
  357.   // begin to write map seq
  358.   if ( old_pn != img->number )
  359.   {
  360.     old_pn = img->number;
  361.     get_mem2Dpel(&Y, img->height, img->width);
  362.     if (first)
  363.     {
  364.       fp = fopen( filename, "wb" );
  365.       first = 0;
  366.     }
  367.     else
  368.       fp = fopen( filename, "ab" );
  369.     assert( fp != NULL );
  370.     for (kk=0; kk<num_spare_pics; kk++)
  371.     {
  372.       for (i=0; i < img->height/16; i++)
  373.         for (j=0; j < img->width/16; j++)
  374.         {
  375.           tmp=map[kk][i][j]==0? img->max_imgpel_value_comp[0] : 0;
  376.           for (i0=0; i0<16; i0++)
  377.             for (j0=0; j0<16; j0++)
  378.               Y[i*16+i0][j*16+j0]=tmp;
  379.         }
  380.       // write the map image
  381.       for (i=0; i < img->height; i++)
  382.         for (j=0; j < img->width; j++)
  383.           fwrite(&(Y[i][j]), symbol_size_in_bytes, 1, p_out);
  384.       for (k=0; k < 2; k++)
  385.         for (i=0; i < img->height>>1; i++)
  386.           for (j=0; j < img->width>>1; j++)
  387.             fwrite(&(img->dc_pred_value_comp[1]), symbol_size_in_bytes, 1, p_out);
  388.     }
  389.     fclose( fp );
  390.     free_mem2Dpel( Y );
  391.   }
  392.   // end of writing map image
  393. #undef WRITE_MAP_IMAGE
  394. #endif
  395.   free_mem3D( map );
  396.   free(buf);
  397. }
  398. /*!
  399.  ************************************************************************
  400.  *  brief
  401.  *     Interpret the Sub-sequence information SEI message
  402.  *  param payload
  403.  *     a pointer that point to the sei payload
  404.  *  param size
  405.  *     the size of the sei message
  406.  *  param img
  407.  *     the image pointer
  408.  *
  409.  ************************************************************************
  410.  */
  411. void interpret_subsequence_info( byte* payload, int size, ImageParameters *img )
  412. {
  413.   Bitstream* buf;
  414.   int sub_seq_layer_num, sub_seq_id, first_ref_pic_flag, leading_non_ref_pic_flag, last_pic_flag,
  415.       sub_seq_frame_num_flag, sub_seq_frame_num;
  416.   buf = malloc(sizeof(Bitstream));
  417.   buf->bitstream_length = size;
  418.   buf->streamBuffer = payload;
  419.   buf->frame_bitoffset = 0;
  420.   UsedBits = 0;
  421.   sub_seq_layer_num        = ue_v("SEI: sub_seq_layer_num"       , buf);
  422.   sub_seq_id               = ue_v("SEI: sub_seq_id"              , buf);
  423.   first_ref_pic_flag       = u_1 ("SEI: first_ref_pic_flag"      , buf);
  424.   leading_non_ref_pic_flag = u_1 ("SEI: leading_non_ref_pic_flag", buf);
  425.   last_pic_flag            = u_1 ("SEI: last_pic_flag"           , buf);
  426.   sub_seq_frame_num_flag   = u_1 ("SEI: sub_seq_frame_num_flag"  , buf);
  427.   if (sub_seq_frame_num_flag)
  428.   {
  429.     sub_seq_frame_num        = ue_v("SEI: sub_seq_frame_num"       , buf);
  430.   }
  431. #ifdef PRINT_SUBSEQUENCE_INFO
  432.   printf("Sub-sequence information SEI messagen");
  433.   printf("sub_seq_layer_num        = %dn", sub_seq_layer_num );
  434.   printf("sub_seq_id               = %dn", sub_seq_id);
  435.   printf("first_ref_pic_flag       = %dn", first_ref_pic_flag);
  436.   printf("leading_non_ref_pic_flag = %dn", leading_non_ref_pic_flag);
  437.   printf("last_pic_flag            = %dn", last_pic_flag);
  438.   printf("sub_seq_frame_num_flag   = %dn", sub_seq_frame_num_flag);
  439.   if (sub_seq_frame_num_flag)
  440.   {
  441.     printf("sub_seq_frame_num        = %dn", sub_seq_frame_num);
  442.   }
  443. #endif
  444.   free(buf);
  445. #ifdef PRINT_SUBSEQUENCE_INFO
  446. #undef PRINT_SUBSEQUENCE_INFO
  447. #endif
  448. }
  449. /*!
  450.  ************************************************************************
  451.  *  brief
  452.  *     Interpret the Sub-sequence layer characteristics SEI message
  453.  *  param payload
  454.  *     a pointer that point to the sei payload
  455.  *  param size
  456.  *     the size of the sei message
  457.  *  param img
  458.  *     the image pointer
  459.  *
  460.  ************************************************************************
  461.  */
  462. void interpret_subsequence_layer_characteristics_info( byte* payload, int size, ImageParameters *img )
  463. {
  464.   Bitstream* buf;
  465.   long num_sub_layers, accurate_statistics_flag, average_bit_rate, average_frame_rate;
  466.   int i;
  467.   buf = malloc(sizeof(Bitstream));
  468.   buf->bitstream_length = size;
  469.   buf->streamBuffer = payload;
  470.   buf->frame_bitoffset = 0;
  471.   UsedBits = 0;
  472.   num_sub_layers = 1 + ue_v("SEI: num_sub_layers_minus1", buf);
  473. #ifdef PRINT_SUBSEQUENCE_LAYER_CHAR
  474.   printf("Sub-sequence layer characteristics SEI messagen");
  475.   printf("num_sub_layers_minus1 = %dn", num_sub_layers - 1);
  476. #endif
  477.   for (i=0; i<num_sub_layers; i++)
  478.   {
  479.     accurate_statistics_flag = u_1(   "SEI: accurate_statistics_flag", buf);
  480.     average_bit_rate         = u_v(16,"SEI: average_bit_rate"        , buf);
  481.     average_frame_rate       = u_v(16,"SEI: average_frame_rate"      , buf);
  482. #ifdef PRINT_SUBSEQUENCE_LAYER_CHAR
  483.     printf("layer %d: accurate_statistics_flag = %ld n", i, accurate_statistics_flag);
  484.     printf("layer %d: average_bit_rate         = %ld n", i, average_bit_rate);
  485.     printf("layer %d: average_frame_rate       = %ld n", i, average_frame_rate);
  486. #endif
  487.   }
  488.   free (buf);
  489. }
  490. /*!
  491.  ************************************************************************
  492.  *  brief
  493.  *     Interpret the Sub-sequence characteristics SEI message
  494.  *  param payload
  495.  *     a pointer that point to the sei payload
  496.  *  param size
  497.  *     the size of the sei message
  498.  *  param img
  499.  *     the image pointer
  500.  *
  501.  ************************************************************************
  502.  */
  503. void interpret_subsequence_characteristics_info( byte* payload, int size, ImageParameters *img )
  504. {
  505.   Bitstream* buf;
  506.   int i;
  507.   int sub_seq_layer_num, sub_seq_id, duration_flag, average_rate_flag, accurate_statistics_flag;
  508.   unsigned long sub_seq_duration, average_bit_rate, average_frame_rate;
  509.   int num_referenced_subseqs, ref_sub_seq_layer_num, ref_sub_seq_id, ref_sub_seq_direction;
  510.   buf = malloc(sizeof(Bitstream));
  511.   buf->bitstream_length = size;
  512.   buf->streamBuffer = payload;
  513.   buf->frame_bitoffset = 0;
  514.   UsedBits = 0;
  515.   sub_seq_layer_num = ue_v("SEI: sub_seq_layer_num", buf);
  516.   sub_seq_id        = ue_v("SEI: sub_seq_id", buf);
  517.   duration_flag     = u_1 ("SEI: duration_flag", buf);
  518. #ifdef PRINT_SUBSEQUENCE_CHAR
  519.   printf("Sub-sequence characteristics SEI messagen");
  520.   printf("sub_seq_layer_num = %dn", sub_seq_layer_num );
  521.   printf("sub_seq_id        = %dn", sub_seq_id);
  522.   printf("duration_flag     = %dn", duration_flag);
  523. #endif
  524.   if ( duration_flag )
  525.   {
  526.     sub_seq_duration = u_v (32, "SEI: duration_flag", buf);
  527. #ifdef PRINT_SUBSEQUENCE_CHAR
  528.     printf("sub_seq_duration = %ldn", sub_seq_duration);
  529. #endif
  530.   }
  531.   average_rate_flag = u_1 ("SEI: average_rate_flag", buf);
  532. #ifdef PRINT_SUBSEQUENCE_CHAR
  533.   printf("average_rate_flag = %dn", average_rate_flag);
  534. #endif
  535.   if ( average_rate_flag )
  536.   {
  537.     accurate_statistics_flag = u_1 (    "SEI: accurate_statistics_flag", buf);
  538.     average_bit_rate         = u_v (16, "SEI: average_bit_rate", buf);
  539.     average_frame_rate       = u_v (16, "SEI: average_frame_rate", buf);
  540. #ifdef PRINT_SUBSEQUENCE_CHAR
  541.     printf("accurate_statistics_flag = %dn", accurate_statistics_flag);
  542.     printf("average_bit_rate         = %ldn", average_bit_rate);
  543.     printf("average_frame_rate       = %ldn", average_frame_rate);
  544. #endif
  545.   }
  546.   num_referenced_subseqs  = ue_v("SEI: num_referenced_subseqs", buf);
  547. #ifdef PRINT_SUBSEQUENCE_CHAR
  548.   printf("num_referenced_subseqs = %dn", num_referenced_subseqs);
  549. #endif
  550.   for (i=0; i<num_referenced_subseqs; i++)
  551.   {
  552.     ref_sub_seq_layer_num  = ue_v("SEI: ref_sub_seq_layer_num", buf);
  553.     ref_sub_seq_id         = ue_v("SEI: ref_sub_seq_id", buf);
  554.     ref_sub_seq_direction  = u_1 ("SEI: ref_sub_seq_direction", buf);
  555. #ifdef PRINT_SUBSEQUENCE_CHAR
  556.     printf("ref_sub_seq_layer_num = %dn", ref_sub_seq_layer_num);
  557.     printf("ref_sub_seq_id        = %dn", ref_sub_seq_id);
  558.     printf("ref_sub_seq_direction = %dn", ref_sub_seq_direction);
  559. #endif
  560.   }
  561.   free( buf );
  562. #ifdef PRINT_SUBSEQUENCE_CHAR
  563. #undef PRINT_SUBSEQUENCE_CHAR
  564. #endif
  565. }
  566. /*!
  567.  ************************************************************************
  568.  *  brief
  569.  *     Interpret the Scene information SEI message
  570.  *  param payload
  571.  *     a pointer that point to the sei payload
  572.  *  param size
  573.  *     the size of the sei message
  574.  *  param img
  575.  *     the image pointer
  576.  *
  577.  ************************************************************************
  578.  */
  579. void interpret_scene_information( byte* payload, int size, ImageParameters *img )
  580. {
  581.   Bitstream* buf;
  582.   int scene_id, scene_transition_type, second_scene_id;
  583.   buf = malloc(sizeof(Bitstream));
  584.   buf->bitstream_length = size;
  585.   buf->streamBuffer = payload;
  586.   buf->frame_bitoffset = 0;
  587.   UsedBits = 0;
  588.   scene_id              = ue_v("SEI: scene_id"             , buf);
  589.   scene_transition_type = ue_v("SEI: scene_transition_type", buf);
  590.   if ( scene_transition_type > 3 )
  591.   {
  592.     second_scene_id     = ue_v("SEI: scene_transition_type", buf);;
  593.   }
  594. #ifdef PRINT_SCENE_INFORMATION
  595.   printf("Scene information SEI messagen");
  596.   printf("scene_transition_type = %dn", scene_transition_type);
  597.   printf("scene_id              = %dn", scene_id);
  598.   if ( scene_transition_type > 3 )
  599.   {
  600.     printf("second_scene_id       = %dn", second_scene_id);
  601.   }
  602. #endif
  603.   free( buf );
  604. #ifdef PRINT_SCENE_INFORMATION
  605. #undef PRINT_SCENE_INFORMATION
  606. #endif
  607. }
  608. /*!
  609.  ************************************************************************
  610.  *  brief
  611.  *     Interpret the Filler payload SEI message
  612.  *  param payload
  613.  *     a pointer that point to the sei payload
  614.  *  param size
  615.  *     the size of the sei message
  616.  *  param img
  617.  *     the image pointer
  618.  *
  619.  ************************************************************************
  620.  */
  621. void interpret_filler_payload_info( byte* payload, int size, ImageParameters *img )
  622. {
  623.   int payload_cnt = 0;
  624.   while (payload_cnt<size)
  625.   {
  626.     if (payload[payload_cnt] == 0xFF)
  627.     {
  628.        payload_cnt++;
  629.     }
  630.   }
  631. #ifdef PRINT_FILLER_PAYLOAD_INFO
  632.   printf("Filler payload SEI messagen");
  633.   if (payload_cnt==size)
  634.   {
  635.     printf("read %d bytes of filler payloadn", payload_cnt);
  636.   }
  637.   else
  638.   {
  639.     printf("error reading filler payload: not all bytes are 0xFF (%d of %d)n", payload_cnt, size);
  640.   }
  641. #endif
  642. #ifdef PRINT_FILLER_PAYLOAD_INFO
  643. #undef PRINT_FILLER_PAYLOAD_INFO
  644. #endif
  645. }
  646. /*!
  647.  ************************************************************************
  648.  *  brief
  649.  *     Interpret the User data unregistered SEI message
  650.  *  param payload
  651.  *     a pointer that point to the sei payload
  652.  *  param size
  653.  *     the size of the sei message
  654.  *  param img
  655.  *     the image pointer
  656.  *
  657.  ************************************************************************
  658.  */
  659. void interpret_user_data_unregistered_info( byte* payload, int size, ImageParameters *img )
  660. {
  661.   int offset = 0;
  662.   byte payload_byte;
  663. #ifdef PRINT_USER_DATA_UNREGISTERED_INFO
  664.   printf("User data unregistered SEI messagen");
  665.   printf("uuid_iso_11578 = 0x");
  666. #endif
  667.   assert (size>=16);
  668.   for (offset = 0; offset < 16; offset++)
  669.   {
  670. #ifdef PRINT_USER_DATA_UNREGISTERED_INFO
  671.     printf("%02x",payload[offset]);
  672. #endif
  673.   }
  674. #ifdef PRINT_USER_DATA_UNREGISTERED_INFO
  675.     printf("n");
  676. #endif
  677.   while (offset < size)
  678.   {
  679.     payload_byte = payload[offset];
  680.     offset ++;
  681. #ifdef PRINT_USER_DATA_UNREGISTERED_INFO
  682.     printf("Unreg data payload_byte = %dn", payload_byte);
  683. #endif
  684.   }
  685. #ifdef PRINT_USER_DATA_UNREGISTERED_INFO
  686. #undef PRINT_USER_DATA_UNREGISTERED_INFO
  687. #endif
  688. }
  689. /*!
  690.  ************************************************************************
  691.  *  brief
  692.  *     Interpret the User data registered by ITU-T T.35 SEI message
  693.  *  param payload
  694.  *     a pointer that point to the sei payload
  695.  *  param size
  696.  *     the size of the sei message
  697.  *  param img
  698.  *     the image pointer
  699.  *
  700.  ************************************************************************
  701.  */
  702. void interpret_user_data_registered_itu_t_t35_info( byte* payload, int size, ImageParameters *img )
  703. {
  704.   int offset = 0;
  705.   byte itu_t_t35_country_code, itu_t_t35_country_code_extension_byte, payload_byte;
  706.   itu_t_t35_country_code = payload[offset];
  707.   offset++;
  708. #ifdef PRINT_USER_DATA_REGISTERED_ITU_T_T35_INFO
  709.   printf("User data registered by ITU-T T.35 SEI messagen");
  710.   printf(" itu_t_t35_country_code = %d n", itu_t_t35_country_code);
  711. #endif
  712.   if(itu_t_t35_country_code == 0xFF)
  713.   {
  714.     itu_t_t35_country_code_extension_byte = payload[offset];
  715.     offset++;
  716. #ifdef PRINT_USER_DATA_REGISTERED_ITU_T_T35_INFO
  717.     printf(" ITU_T_T35_COUNTRY_CODE_EXTENSION_BYTE %d n", itu_t_t35_country_code_extension_byte);
  718. #endif
  719.   }
  720.   while (offset < size)
  721.   {
  722.     payload_byte = payload[offset];
  723.     offset ++;
  724. #ifdef PRINT_USER_DATA_REGISTERED_ITU_T_T35_INFO
  725.     printf("itu_t_t35 payload_byte = %dn", payload_byte);
  726. #endif
  727.   }
  728. #ifdef PRINT_USER_DATA_REGISTERED_ITU_T_T35_INFO
  729. #undef PRINT_USER_DATA_REGISTERED_ITU_T_T35_INFO
  730. #endif
  731. }
  732. /*!
  733.  ************************************************************************
  734.  *  brief
  735.  *     Interpret the Pan scan rectangle SEI message
  736.  *  param payload
  737.  *     a pointer that point to the sei payload
  738.  *  param size
  739.  *     the size of the sei message
  740.  *  param img
  741.  *     the image pointer
  742.  *
  743.  ************************************************************************
  744.  */
  745. void interpret_pan_scan_rect_info( byte* payload, int size, ImageParameters *img )
  746. {
  747.   int pan_scan_rect_cancel_flag;
  748.   int pan_scan_cnt_minus1, i;
  749.   int pan_scan_rect_repetition_period;
  750.   int pan_scan_rect_id, pan_scan_rect_left_offset, pan_scan_rect_right_offset;
  751.   int pan_scan_rect_top_offset, pan_scan_rect_bottom_offset;
  752.   Bitstream* buf;
  753.   buf = malloc(sizeof(Bitstream));
  754.   buf->bitstream_length = size;
  755.   buf->streamBuffer = payload;
  756.   buf->frame_bitoffset = 0;
  757.   UsedBits = 0;
  758.   pan_scan_rect_id = ue_v("SEI: pan_scan_rect_id", buf);
  759.   pan_scan_rect_cancel_flag = u_1("SEI: pan_scan_rect_cancel_flag", buf);
  760.   if (!pan_scan_rect_cancel_flag) 
  761.   {
  762.     pan_scan_cnt_minus1 = ue_v("SEI: pan_scan_cnt_minus1", buf);
  763.     for (i = 0; i <= pan_scan_cnt_minus1; i++) 
  764.     {
  765.       pan_scan_rect_left_offset   = se_v("SEI: pan_scan_rect_left_offset"  , buf);
  766.       pan_scan_rect_right_offset  = se_v("SEI: pan_scan_rect_right_offset" , buf);
  767.       pan_scan_rect_top_offset    = se_v("SEI: pan_scan_rect_top_offset"   , buf);
  768.       pan_scan_rect_bottom_offset = se_v("SEI: pan_scan_rect_bottom_offset", buf);
  769. #ifdef PRINT_PAN_SCAN_RECT
  770.       printf("Pan scan rectangle SEI message %d/%dn", i, pan_scan_cnt_minus1);
  771.       printf("pan_scan_rect_id            = %dn", pan_scan_rect_id);
  772.       printf("pan_scan_rect_left_offset   = %dn", pan_scan_rect_left_offset);
  773.       printf("pan_scan_rect_right_offset  = %dn", pan_scan_rect_right_offset);
  774.       printf("pan_scan_rect_top_offset    = %dn", pan_scan_rect_top_offset);
  775.       printf("pan_scan_rect_bottom_offset = %dn", pan_scan_rect_bottom_offset);
  776. #endif
  777.     }
  778.     pan_scan_rect_repetition_period = ue_v("SEI: pan_scan_rect_repetition_period", buf);
  779.   }
  780.   free (buf);
  781. #ifdef PRINT_PAN_SCAN_RECT
  782. #undef PRINT_PAN_SCAN_RECT
  783. #endif
  784. }
  785. /*!
  786.  ************************************************************************
  787.  *  brief
  788.  *     Interpret the Random access point SEI message
  789.  *  param payload
  790.  *     a pointer that point to the sei payload
  791.  *  param size
  792.  *     the size of the sei message
  793.  *  param img
  794.  *     the image pointer
  795.  *
  796.  ************************************************************************
  797.  */
  798. void interpret_recovery_point_info( byte* payload, int size, ImageParameters *img )
  799. {
  800.   int recovery_frame_cnt, exact_match_flag, broken_link_flag, changing_slice_group_idc;
  801.   Bitstream* buf;
  802.   buf = malloc(sizeof(Bitstream));
  803.   buf->bitstream_length = size;
  804.   buf->streamBuffer = payload;
  805.   buf->frame_bitoffset = 0;
  806.   UsedBits = 0;
  807.   recovery_frame_cnt       = ue_v(    "SEI: recovery_frame_cnt"      , buf);
  808.   exact_match_flag         = u_1 (    "SEI: exact_match_flag"        , buf);
  809.   broken_link_flag         = u_1 (    "SEI: broken_link_flag"        , buf);
  810.   changing_slice_group_idc = u_v ( 2, "SEI: changing_slice_group_idc", buf);
  811.   img->recovery_point = 1;
  812.   img->recovery_frame_cnt = recovery_frame_cnt;
  813. #ifdef PRINT_RECOVERY_POINT
  814.   printf("Recovery point SEI messagen");
  815.   printf("recovery_frame_cnt       = %dn", recovery_frame_cnt);
  816.   printf("exact_match_flag         = %dn", exact_match_flag);
  817.   printf("broken_link_flag         = %dn", broken_link_flag);
  818.   printf("changing_slice_group_idc = %dn", changing_slice_group_idc);
  819. #endif
  820.   free (buf);
  821. #ifdef PRINT_RECOVERY_POINT
  822. #undef PRINT_RECOVERY_POINT
  823. #endif
  824. }
  825. /*!
  826.  ************************************************************************
  827.  *  brief
  828.  *     Interpret the Decoded Picture Buffer Management Repetition SEI message
  829.  *  param payload
  830.  *     a pointer that point to the sei payload
  831.  *  param size
  832.  *     the size of the sei message
  833.  *  param img
  834.  *     the image pointer
  835.  *
  836.  ************************************************************************
  837.  */
  838. void interpret_dec_ref_pic_marking_repetition_info( byte* payload, int size, ImageParameters *img )
  839. {
  840.   int original_idr_flag, original_frame_num;
  841.   DecRefPicMarking_t *tmp_drpm;
  842.   DecRefPicMarking_t *old_drpm;
  843.   int old_idr_flag , old_no_output_of_prior_pics_flag, old_long_term_reference_flag , old_adaptive_ref_pic_buffering_flag;
  844.   Bitstream* buf;
  845.   buf = malloc(sizeof(Bitstream));
  846.   buf->bitstream_length = size;
  847.   buf->streamBuffer = payload;
  848.   buf->frame_bitoffset = 0;
  849.   UsedBits = 0;
  850.   original_idr_flag     = u_1 (    "SEI: original_idr_flag"    , buf);
  851.   original_frame_num    = ue_v(    "SEI: original_frame_num"   , buf);
  852. #ifdef PRINT_DEC_REF_PIC_MARKING
  853.   printf("Decoded Picture Buffer Management Repetition SEI messagen");
  854.   printf("original_idr_flag       = %dn", original_idr_flag);
  855.   printf("original_frame_num      = %dn", original_frame_num);
  856. #endif
  857.   // we need to save everything that is probably overwritten in dec_ref_pic_marking()
  858.   old_drpm = img->dec_ref_pic_marking_buffer;
  859.   old_idr_flag = img->idr_flag;
  860.   old_no_output_of_prior_pics_flag = img->no_output_of_prior_pics_flag;
  861.   old_long_term_reference_flag = img->long_term_reference_flag;
  862.   old_adaptive_ref_pic_buffering_flag = img->adaptive_ref_pic_buffering_flag;
  863.   // set new initial values
  864.   img->idr_flag = original_idr_flag;
  865.   img->dec_ref_pic_marking_buffer = NULL;
  866.   dec_ref_pic_marking(buf);
  867.   // print out decoded values
  868. #ifdef PRINT_DEC_REF_PIC_MARKING
  869.   if (img->idr_flag)
  870.   {
  871.     printf("no_output_of_prior_pics_flag = %dn", img->no_output_of_prior_pics_flag);
  872.     printf("long_term_reference_flag     = %dn", img->long_term_reference_flag);
  873.   }
  874.   else
  875.   {
  876.     printf("adaptive_ref_pic_buffering_flag  = %dn", img->adaptive_ref_pic_buffering_flag);
  877.     if (img->adaptive_ref_pic_buffering_flag)
  878.     {
  879.       tmp_drpm=img->dec_ref_pic_marking_buffer;
  880.       while (tmp_drpm != NULL)
  881.       {
  882.         printf("memory_management_control_operation  = %dn", tmp_drpm->memory_management_control_operation);
  883.         if ((tmp_drpm->memory_management_control_operation==1)||(tmp_drpm->memory_management_control_operation==3))
  884.         {
  885.           printf("difference_of_pic_nums_minus1        = %dn", tmp_drpm->difference_of_pic_nums_minus1);
  886.         }
  887.         if (tmp_drpm->memory_management_control_operation==2)
  888.         {
  889.           printf("long_term_pic_num                    = %dn", tmp_drpm->long_term_pic_num);
  890.         }
  891.         if ((tmp_drpm->memory_management_control_operation==3)||(tmp_drpm->memory_management_control_operation==6))
  892.         {
  893.           printf("long_term_frame_idx                  = %dn", tmp_drpm->long_term_frame_idx);
  894.         }
  895.         if (tmp_drpm->memory_management_control_operation==4)
  896.         {
  897.           printf("max_long_term_pic_idx_plus1          = %dn", tmp_drpm->max_long_term_frame_idx_plus1);
  898.         }
  899.         tmp_drpm = tmp_drpm->Next;
  900.       }
  901.     }
  902.   }
  903. #endif
  904.   while (img->dec_ref_pic_marking_buffer)
  905.   {
  906.     tmp_drpm=img->dec_ref_pic_marking_buffer;
  907.     img->dec_ref_pic_marking_buffer=tmp_drpm->Next;
  908.     free (tmp_drpm);
  909.   }
  910.   // restore old values in img
  911.   img->dec_ref_pic_marking_buffer = old_drpm;
  912.   img->idr_flag = old_idr_flag;
  913.   img->no_output_of_prior_pics_flag = old_no_output_of_prior_pics_flag;
  914.   img->long_term_reference_flag = old_long_term_reference_flag;
  915.   img->adaptive_ref_pic_buffering_flag = old_adaptive_ref_pic_buffering_flag;
  916.   free (buf);
  917. #ifdef PRINT_DEC_REF_PIC_MARKING
  918. #undef PRINT_DEC_REF_PIC_MARKING
  919. #endif
  920. }
  921. /*!
  922.  ************************************************************************
  923.  *  brief
  924.  *     Interpret the Full-frame freeze SEI message
  925.  *  param payload
  926.  *     a pointer that point to the sei payload
  927.  *  param size
  928.  *     the size of the sei message
  929.  *  param img
  930.  *     the image pointer
  931.  *
  932.  ************************************************************************
  933.  */
  934. void interpret_full_frame_freeze_info( byte* payload, int size, ImageParameters *img )
  935. {
  936.   int full_frame_freeze_repetition_period;
  937.   Bitstream* buf;
  938.   buf = malloc(sizeof(Bitstream));
  939.   buf->bitstream_length = size;
  940.   buf->streamBuffer = payload;
  941.   buf->frame_bitoffset = 0;
  942.   full_frame_freeze_repetition_period  = ue_v(    "SEI: full_frame_freeze_repetition_period"   , buf);
  943. #ifdef PRINT_FULL_FRAME_FREEZE_INFO
  944.   printf("full_frame_freeze_repetition_period = %dn", full_frame_freeze_repetition_period);
  945. #endif
  946.   free (buf);
  947. #ifdef PRINT_FULL_FRAME_FREEZE_INFO
  948. #undef PRINT_FULL_FRAME_FREEZE_INFO
  949. #endif
  950. }
  951. /*!
  952.  ************************************************************************
  953.  *  brief
  954.  *     Interpret the Full-frame freeze release SEI message
  955.  *  param payload
  956.  *     a pointer that point to the sei payload
  957.  *  param size
  958.  *     the size of the sei message
  959.  *  param img
  960.  *     the image pointer
  961.  *
  962.  ************************************************************************
  963.  */
  964. void interpret_full_frame_freeze_release_info( byte* payload, int size, ImageParameters *img )
  965. {
  966. #ifdef PRINT_FULL_FRAME_FREEZE_RELEASE_INFO
  967.   printf("Full-frame freeze release SEI messagen");
  968.   if (size)
  969.   {
  970.     printf("payload size of this message should be zero, but is %d bytes.n", size);
  971.   }
  972. #endif
  973. #ifdef PRINT_FULL_FRAME_FREEZE_RELEASE_INFO
  974. #undef PRINT_FULL_FRAME_FREEZE_RELEASE_INFO
  975. #endif
  976. }
  977. /*!
  978.  ************************************************************************
  979.  *  brief
  980.  *     Interpret the Full-frame snapshot SEI message
  981.  *  param payload
  982.  *     a pointer that point to the sei payload
  983.  *  param size
  984.  *     the size of the sei message
  985.  *  param img
  986.  *     the image pointer
  987.  *
  988.  ************************************************************************
  989.  */
  990. void interpret_full_frame_snapshot_info( byte* payload, int size, ImageParameters *img )
  991. {
  992.   int snapshot_id;
  993.   Bitstream* buf;
  994.   buf = malloc(sizeof(Bitstream));
  995.   buf->bitstream_length = size;
  996.   buf->streamBuffer = payload;
  997.   buf->frame_bitoffset = 0;
  998.   UsedBits = 0;
  999.   snapshot_id = ue_v("SEI: snapshot_id", buf);
  1000. #ifdef PRINT_FULL_FRAME_SNAPSHOT_INFO
  1001.   printf("Full-frame snapshot SEI messagen");
  1002.   printf("snapshot_id = %dn", snapshot_id);
  1003. #endif
  1004.   free (buf);
  1005. #ifdef PRINT_FULL_FRAME_SNAPSHOT_INFO
  1006. #undef PRINT_FULL_FRAME_SNAPSHOT_INFO
  1007. #endif
  1008. }
  1009. /*!
  1010.  ************************************************************************
  1011.  *  brief
  1012.  *     Interpret the Progressive refinement segment start SEI message
  1013.  *  param payload
  1014.  *     a pointer that point to the sei payload
  1015.  *  param size
  1016.  *     the size of the sei message
  1017.  *  param img
  1018.  *     the image pointer
  1019.  *
  1020.  ************************************************************************
  1021.  */
  1022. void interpret_progressive_refinement_start_info( byte* payload, int size, ImageParameters *img )
  1023. {
  1024.   int progressive_refinement_id, num_refinement_steps_minus1;
  1025.   Bitstream* buf;
  1026.   buf = malloc(sizeof(Bitstream));
  1027.   buf->bitstream_length = size;
  1028.   buf->streamBuffer = payload;
  1029.   buf->frame_bitoffset = 0;
  1030.   UsedBits = 0;
  1031.   progressive_refinement_id   = ue_v("SEI: progressive_refinement_id"  , buf);
  1032.   num_refinement_steps_minus1 = ue_v("SEI: num_refinement_steps_minus1", buf);
  1033. #ifdef PRINT_PROGRESSIVE_REFINEMENT_START_INFO
  1034.   printf("Progressive refinement segment start SEI messagen");
  1035.   printf("progressive_refinement_id   = %dn", progressive_refinement_id);
  1036.   printf("num_refinement_steps_minus1 = %dn", num_refinement_steps_minus1);
  1037. #endif
  1038.   free (buf);
  1039. #ifdef PRINT_PROGRESSIVE_REFINEMENT_START_INFO
  1040. #undef PRINT_PROGRESSIVE_REFINEMENT_START_INFO
  1041. #endif
  1042. }
  1043. /*!
  1044.  ************************************************************************
  1045.  *  brief
  1046.  *     Interpret the Progressive refinement segment end SEI message
  1047.  *  param payload
  1048.  *     a pointer that point to the sei payload
  1049.  *  param size
  1050.  *     the size of the sei message
  1051.  *  param img
  1052.  *     the image pointer
  1053.  *
  1054.  ************************************************************************
  1055.  */
  1056. void interpret_progressive_refinement_end_info( byte* payload, int size, ImageParameters *img )
  1057. {
  1058.   int progressive_refinement_id;
  1059.   Bitstream* buf;
  1060.   buf = malloc(sizeof(Bitstream));
  1061.   buf->bitstream_length = size;
  1062.   buf->streamBuffer = payload;
  1063.   buf->frame_bitoffset = 0;
  1064.   UsedBits = 0;
  1065.   progressive_refinement_id   = ue_v("SEI: progressive_refinement_id"  , buf);
  1066. #ifdef PRINT_PROGRESSIVE_REFINEMENT_END_INFO
  1067.   printf("Progressive refinement segment end SEI messagen");
  1068.   printf("progressive_refinement_id   = %dn", progressive_refinement_id);
  1069. #endif
  1070.   free (buf);
  1071. #ifdef PRINT_PROGRESSIVE_REFINEMENT_END_INFO
  1072. #undef PRINT_PROGRESSIVE_REFINEMENT_END_INFO
  1073. #endif
  1074. }
  1075. /*!
  1076.  ************************************************************************
  1077.  *  brief
  1078.  *     Interpret the Motion-constrained slice group set SEI message
  1079.  *  param payload
  1080.  *     a pointer that point to the sei payload
  1081.  *  param size
  1082.  *     the size of the sei message
  1083.  *  param img
  1084.  *     the image pointer
  1085.  *
  1086.  ************************************************************************
  1087.  */
  1088. void interpret_motion_constrained_slice_group_set_info( byte* payload, int size, ImageParameters *img )
  1089. {
  1090.   int num_slice_groups_minus1, slice_group_id, exact_match_flag, pan_scan_rect_flag, pan_scan_rect_id;
  1091.   int i;
  1092.   int sliceGroupSize;
  1093.   Bitstream* buf;
  1094.   buf = malloc(sizeof(Bitstream));
  1095.   buf->bitstream_length = size;
  1096.   buf->streamBuffer = payload;
  1097.   buf->frame_bitoffset = 0;
  1098.   UsedBits = 0;
  1099.   num_slice_groups_minus1   = ue_v("SEI: num_slice_groups_minus1"  , buf);
  1100.   sliceGroupSize = CeilLog2( num_slice_groups_minus1 + 1 );
  1101. #ifdef PRINT_MOTION_CONST_SLICE_GROUP_SET_INFO
  1102.   printf("Motion-constrained slice group set SEI messagen");
  1103.   printf("num_slice_groups_minus1   = %dn", num_slice_groups_minus1);
  1104. #endif
  1105.   for (i=0; i<=num_slice_groups_minus1;i++)
  1106.   {
  1107.     slice_group_id   = u_v (sliceGroupSize, "SEI: slice_group_id" , buf)    ;
  1108. #ifdef PRINT_MOTION_CONST_SLICE_GROUP_SET_INFO
  1109.     printf("slice_group_id            = %dn", slice_group_id);
  1110. #endif
  1111.   }
  1112.   exact_match_flag   = u_1("SEI: exact_match_flag"  , buf);
  1113.   pan_scan_rect_flag = u_1("SEI: pan_scan_rect_flag"  , buf);
  1114. #ifdef PRINT_MOTION_CONST_SLICE_GROUP_SET_INFO
  1115.   printf("exact_match_flag         = %dn", exact_match_flag);
  1116.   printf("pan_scan_rect_flag       = %dn", pan_scan_rect_flag);
  1117. #endif
  1118.   if (pan_scan_rect_flag)
  1119.   {
  1120.     pan_scan_rect_id = ue_v("SEI: pan_scan_rect_id"  , buf);
  1121. #ifdef PRINT_MOTION_CONST_SLICE_GROUP_SET_INFO
  1122.     printf("pan_scan_rect_id         = %dn", pan_scan_rect_id);
  1123. #endif
  1124.   }
  1125.   free (buf);
  1126. #ifdef PRINT_MOTION_CONST_SLICE_GROUP_SET_INFO
  1127. #undef PRINT_MOTION_CONST_SLICE_GROUP_SET_INFO
  1128. #endif
  1129. }
  1130. /*!
  1131.  ************************************************************************
  1132.  *  brief
  1133.  *     Interpret the film grain characteristics SEI message
  1134.  *  param payload
  1135.  *     a pointer that point to the sei payload
  1136.  *  param size
  1137.  *     the size of the sei message
  1138.  *  param img
  1139.  *     the image pointer
  1140.  *
  1141.  ************************************************************************
  1142.  */
  1143. void interpret_film_grain_characteristics_info( byte* payload, int size, ImageParameters *img )
  1144. {
  1145.   int film_grain_characteristics_cancel_flag;
  1146.   int model_id, separate_colour_description_present_flag;
  1147.   int film_grain_bit_depth_luma_minus8, film_grain_bit_depth_chroma_minus8, film_grain_full_range_flag, film_grain_colour_primaries, film_grain_transfer_characteristics, film_grain_matrix_coefficients;
  1148.   int blending_mode_id, log2_scale_factor, comp_model_present_flag[3];
  1149.   int num_intensity_intervals_minus1, num_model_values_minus1;
  1150.   int intensity_interval_lower_bound, intensity_interval_upper_bound;
  1151.   int comp_model_value;
  1152.   int film_grain_characteristics_repetition_period;
  1153.   int c, i, j;
  1154.   Bitstream* buf;
  1155.   buf = malloc(sizeof(Bitstream));
  1156.   buf->bitstream_length = size;
  1157.   buf->streamBuffer = payload;
  1158.   buf->frame_bitoffset = 0;
  1159.   film_grain_characteristics_cancel_flag = u_1("SEI: film_grain_characteristics_cancel_flag", buf);
  1160. #ifdef PRINT_FILM_GRAIN_CHARACTERISTICS_INFO
  1161.   printf("film_grain_characteristics_cancel_flag = %dn", film_grain_characteristics_cancel_flag);
  1162. #endif
  1163.   if(!film_grain_characteristics_cancel_flag)
  1164.   {
  1165.     model_id                                    = u_v(2, "SEI: model_id", buf);
  1166.     separate_colour_description_present_flag    = u_1("SEI: separate_colour_description_present_flag", buf);
  1167. #ifdef PRINT_FILM_GRAIN_CHARACTERISTICS_INFO
  1168.     printf("model_id = %dn", model_id);
  1169.     printf("separate_colour_description_present_flag = %dn", separate_colour_description_present_flag);
  1170. #endif
  1171.     if (separate_colour_description_present_flag)
  1172.     {
  1173.       film_grain_bit_depth_luma_minus8          = u_v(3, "SEI: film_grain_bit_depth_luma_minus8", buf);
  1174.       film_grain_bit_depth_chroma_minus8        = u_v(3, "SEI: film_grain_bit_depth_chroma_minus8", buf);
  1175.       film_grain_full_range_flag                = u_v(1, "SEI: film_grain_full_range_flag", buf);
  1176.       film_grain_colour_primaries               = u_v(8, "SEI: film_grain_colour_primaries", buf);
  1177.       film_grain_transfer_characteristics       = u_v(8, "SEI: film_grain_transfer_characteristics", buf);
  1178.       film_grain_matrix_coefficients            = u_v(8, "SEI: film_grain_matrix_coefficients", buf);
  1179. #ifdef PRINT_FILM_GRAIN_CHARACTERISTICS_INFO
  1180.       printf("film_grain_bit_depth_luma_minus8 = %dn", film_grain_bit_depth_luma_minus8);
  1181.       printf("film_grain_bit_depth_chroma_minus8 = %dn", film_grain_bit_depth_chroma_minus8);
  1182.       printf("film_grain_full_range_flag = %dn", film_grain_full_range_flag);
  1183.       printf("film_grain_colour_primaries = %dn", film_grain_colour_primaries);
  1184.       printf("film_grain_transfer_characteristics = %dn", film_grain_transfer_characteristics);
  1185.       printf("film_grain_matrix_coefficients = %dn", film_grain_matrix_coefficients);
  1186. #endif
  1187.     }
  1188.     blending_mode_id                            = u_v(2, "SEI: blending_mode_id", buf);
  1189.     log2_scale_factor                           = u_v(4, "SEI: log2_scale_factor", buf);
  1190. #ifdef PRINT_FILM_GRAIN_CHARACTERISTICS_INFO
  1191.     printf("blending_mode_id = %dn", blending_mode_id);
  1192.     printf("log2_scale_factor = %dn", log2_scale_factor);
  1193. #endif
  1194.     for (c = 0; c < 3; c ++)
  1195.     {
  1196.       comp_model_present_flag[c]                = u_1("SEI: comp_model_present_flag", buf);
  1197. #ifdef PRINT_FILM_GRAIN_CHARACTERISTICS_INFO
  1198.       printf("comp_model_present_flag = %dn", comp_model_present_flag[c]);
  1199. #endif
  1200.     }
  1201.     for (c = 0; c < 3; c ++)
  1202.       if (comp_model_present_flag[c])
  1203.       {
  1204.         num_intensity_intervals_minus1          = u_v(8, "SEI: num_intensity_intervals_minus1", buf);
  1205.         num_model_values_minus1                 = u_v(3, "SEI: num_model_values_minus1", buf);
  1206. #ifdef PRINT_FILM_GRAIN_CHARACTERISTICS_INFO
  1207.         printf("num_intensity_intervals_minus1 = %dn", num_intensity_intervals_minus1);
  1208.         printf("num_model_values_minus1 = %dn", num_model_values_minus1);
  1209. #endif
  1210.         for (i = 0; i <= num_intensity_intervals_minus1; i ++)
  1211.         {
  1212.           intensity_interval_lower_bound        = u_v(8, "SEI: intensity_interval_lower_bound", buf);
  1213.           intensity_interval_upper_bound        = u_v(8, "SEI: intensity_interval_upper_bound", buf);
  1214. #ifdef PRINT_FILM_GRAIN_CHARACTERISTICS_INFO
  1215.           printf("intensity_interval_lower_bound = %dn", intensity_interval_lower_bound);
  1216.           printf("intensity_interval_upper_bound = %dn", intensity_interval_upper_bound);
  1217. #endif
  1218.           for (j = 0; j <= num_model_values_minus1; j++)
  1219.           {
  1220.             comp_model_value                    = se_v("SEI: comp_model_value", buf);
  1221. #ifdef PRINT_FILM_GRAIN_CHARACTERISTICS_INFO
  1222.             printf("comp_model_value = %dn", comp_model_value);
  1223. #endif
  1224.           }
  1225.         }
  1226.       }
  1227.     film_grain_characteristics_repetition_period = ue_v("SEI: film_grain_characteristics_repetition_period", buf);
  1228. #ifdef PRINT_FILM_GRAIN_CHARACTERISTICS_INFO
  1229.     printf("film_grain_characteristics_repetition_period = %dn", film_grain_characteristics_repetition_period);
  1230. #endif
  1231.   }
  1232.   free (buf);
  1233. #ifdef PRINT_FILM_GRAIN_CHARACTERISTICS_INFO
  1234. #undef PRINT_FILM_GRAIN_CHARACTERISTICS_INFO
  1235. #endif
  1236. }
  1237. /*!
  1238.  ************************************************************************
  1239.  *  brief
  1240.  *     Interpret the deblocking filter display preference SEI message
  1241.  *  param payload
  1242.  *     a pointer that point to the sei payload
  1243.  *  param size
  1244.  *     the size of the sei message
  1245.  *  param img
  1246.  *     the image pointer
  1247.  *
  1248.  ************************************************************************
  1249.  */
  1250. void interpret_deblocking_filter_display_preference_info( byte* payload, int size, ImageParameters *img )
  1251. {
  1252.   int deblocking_display_preference_cancel_flag;
  1253.   int display_prior_to_deblocking_preferred_flag, dec_frame_buffering_constraint_flag, deblocking_display_preference_repetition_period;
  1254.   Bitstream* buf;
  1255.   buf = malloc(sizeof(Bitstream));
  1256.   buf->bitstream_length = size;
  1257.   buf->streamBuffer = payload;
  1258.   buf->frame_bitoffset = 0;
  1259.   deblocking_display_preference_cancel_flag             = u_1("SEI: deblocking_display_preference_cancel_flag", buf);
  1260. #ifdef PRINT_DEBLOCKING_FILTER_DISPLAY_PREFERENCE_INFO
  1261.   printf("deblocking_display_preference_cancel_flag = %dn", deblocking_display_preference_cancel_flag);
  1262. #endif
  1263.   if(!deblocking_display_preference_cancel_flag)
  1264.   {
  1265.     display_prior_to_deblocking_preferred_flag            = u_1("SEI: display_prior_to_deblocking_preferred_flag", buf);
  1266.     dec_frame_buffering_constraint_flag                   = u_1("SEI: dec_frame_buffering_constraint_flag", buf);
  1267.     deblocking_display_preference_repetition_period       = ue_v("SEI: deblocking_display_preference_repetition_period", buf);
  1268. #ifdef PRINT_DEBLOCKING_FILTER_DISPLAY_PREFERENCE_INFO
  1269.     printf("display_prior_to_deblocking_preferred_flag = %dn", display_prior_to_deblocking_preferred_flag);
  1270.     printf("dec_frame_buffering_constraint_flag = %dn", dec_frame_buffering_constraint_flag);
  1271.     printf("deblocking_display_preference_repetition_period = %dn", deblocking_display_preference_repetition_period);
  1272. #endif
  1273.   }
  1274.   free (buf);
  1275. #ifdef PRINT_DEBLOCKING_FILTER_DISPLAY_PREFERENCE_INFO
  1276. #undef PRINT_DEBLOCKING_FILTER_DISPLAY_PREFERENCE_INFO
  1277. #endif
  1278. }
  1279. /*!
  1280.  ************************************************************************
  1281.  *  brief
  1282.  *     Interpret the stereo video info SEI message
  1283.  *  param payload
  1284.  *     a pointer that point to the sei payload
  1285.  *  param size
  1286.  *     the size of the sei message
  1287.  *  param img
  1288.  *     the image pointer
  1289.  *
  1290.  ************************************************************************
  1291.  */
  1292. void interpret_stereo_video_info_info( byte* payload, int size, ImageParameters *img )
  1293. {
  1294.   int field_views_flags;
  1295.   int top_field_is_left_view_flag, current_frame_is_left_view_flag, next_frame_is_second_view_flag;
  1296.   int left_view_self_contained_flag;
  1297.   int right_view_self_contained_flag;
  1298.   Bitstream* buf;
  1299.   buf = malloc(sizeof(Bitstream));
  1300.   buf->bitstream_length = size;
  1301.   buf->streamBuffer = payload;
  1302.   buf->frame_bitoffset = 0;
  1303.   field_views_flags = u_1("SEI: field_views_flags", buf);
  1304. #ifdef PRINT_STEREO_VIDEO_INFO_INFO
  1305.   printf("field_views_flags = %dn", field_views_flags);
  1306. #endif
  1307.   if (field_views_flags)
  1308.   {
  1309.     top_field_is_left_view_flag         = u_1("SEI: top_field_is_left_view_flag", buf);
  1310. #ifdef PRINT_STEREO_VIDEO_INFO_INFO
  1311.     printf("top_field_is_left_view_flag = %dn", top_field_is_left_view_flag);
  1312. #endif
  1313.   }
  1314.   else
  1315.   {
  1316.     current_frame_is_left_view_flag     = u_1("SEI: current_frame_is_left_view_flag", buf);
  1317.     next_frame_is_second_view_flag      = u_1("SEI: next_frame_is_second_view_flag", buf);
  1318. #ifdef PRINT_STEREO_VIDEO_INFO_INFO
  1319.     printf("current_frame_is_left_view_flag = %dn", current_frame_is_left_view_flag);
  1320.     printf("next_frame_is_second_view_flag = %dn", next_frame_is_second_view_flag);
  1321. #endif
  1322.   }
  1323.   left_view_self_contained_flag         = u_1("SEI: left_view_self_contained_flag", buf);
  1324.   right_view_self_contained_flag        = u_1("SEI: right_view_self_contained_flag", buf);
  1325. #ifdef PRINT_STEREO_VIDEO_INFO_INFO
  1326.   printf("left_view_self_contained_flag = %dn", left_view_self_contained_flag);
  1327.   printf("right_view_self_contained_flag = %dn", right_view_self_contained_flag);
  1328. #endif
  1329.   free (buf);
  1330. #ifdef PRINT_STEREO_VIDEO_INFO_INFO
  1331. #undef PRINT_STEREO_VIDEO_INFO_INFO
  1332. #endif
  1333. }
  1334. /*!
  1335.  ************************************************************************
  1336.  *  brief
  1337.  *     Interpret the Reserved SEI message
  1338.  *  param payload
  1339.  *     a pointer that point to the sei payload
  1340.  *  param size
  1341.  *     the size of the sei message
  1342.  *  param img
  1343.  *     the image pointer
  1344.  *
  1345.  ************************************************************************
  1346.  */
  1347. void interpret_reserved_info( byte* payload, int size, ImageParameters *img )
  1348. {
  1349.   int offset = 0;
  1350.   byte payload_byte;
  1351. #ifdef PRINT_RESERVED_INFO
  1352.   printf("Reserved SEI messagen");
  1353. #endif
  1354.   while (offset < size)
  1355.   {
  1356.     payload_byte = payload[offset];
  1357.     offset ++;
  1358. #ifdef PRINT_RESERVED_INFO
  1359.     printf("reserved_sei_message_payload_byte = %dn", payload_byte);
  1360. #endif
  1361.   }
  1362. #ifdef PRINT_RESERVED_INFO
  1363. #undef PRINT_RESERVED_INFO
  1364. #endif
  1365. }
  1366. /*!
  1367.  ************************************************************************
  1368.  *  brief
  1369.  *     Interpret the Buffering period SEI message
  1370.  *  param payload
  1371.  *     a pointer that point to the sei payload
  1372.  *  param size
  1373.  *     the size of the sei message
  1374.  *  param img
  1375.  *     the image pointer
  1376.  *
  1377.  ************************************************************************
  1378.  */
  1379. void interpret_buffering_period_info( byte* payload, int size, ImageParameters *img )
  1380. {
  1381.   int seq_parameter_set_id, initial_cpb_removal_delay, initial_cpb_removal_delay_offset;
  1382.   unsigned int k;
  1383.   Bitstream* buf;
  1384.   seq_parameter_set_rbsp_t *sps;
  1385.   buf = malloc(sizeof(Bitstream));
  1386.   buf->bitstream_length = size;
  1387.   buf->streamBuffer = payload;
  1388.   buf->frame_bitoffset = 0;
  1389.   UsedBits = 0;
  1390.   seq_parameter_set_id   = ue_v("SEI: seq_parameter_set_id"  , buf);
  1391.   sps = &SeqParSet[seq_parameter_set_id];
  1392.   activate_sps(sps);
  1393. #ifdef PRINT_BUFFERING_PERIOD_INFO
  1394.   printf("Buffering period SEI messagen");
  1395.   printf("seq_parameter_set_id   = %dn", seq_parameter_set_id);
  1396. #endif
  1397.   // Note: NalHrdBpPresentFlag and CpbDpbDelaysPresentFlag can also be set "by some means not specified in this Recommendation | International Standard"
  1398.   if (sps->vui_parameters_present_flag)
  1399.   {
  1400.     if (sps->vui_seq_parameters.nal_hrd_parameters_present_flag)
  1401.     {
  1402.       for (k=0; k<sps->vui_seq_parameters.nal_hrd_parameters.cpb_cnt_minus1+1; k++)
  1403.       {
  1404.         initial_cpb_removal_delay        = u_v(sps->vui_seq_parameters.nal_hrd_parameters.initial_cpb_removal_delay_length_minus1+1, "SEI: initial_cpb_removal_delay"        , buf);
  1405.         initial_cpb_removal_delay_offset = u_v(sps->vui_seq_parameters.nal_hrd_parameters.initial_cpb_removal_delay_length_minus1+1, "SEI: initial_cpb_removal_delay_offset" , buf);
  1406. #ifdef PRINT_BUFFERING_PERIOD_INFO
  1407.         printf("nal initial_cpb_removal_delay[%d]        = %dn", k, initial_cpb_removal_delay);
  1408.         printf("nal initial_cpb_removal_delay_offset[%d] = %dn", k, initial_cpb_removal_delay_offset);
  1409. #endif
  1410.       }
  1411.     }
  1412.     if (sps->vui_seq_parameters.vcl_hrd_parameters_present_flag)
  1413.     {
  1414.       for (k=0; k<sps->vui_seq_parameters.vcl_hrd_parameters.cpb_cnt_minus1+1; k++)
  1415.       {
  1416.         initial_cpb_removal_delay        = u_v(sps->vui_seq_parameters.vcl_hrd_parameters.initial_cpb_removal_delay_length_minus1+1, "SEI: initial_cpb_removal_delay"        , buf);
  1417.         initial_cpb_removal_delay_offset = u_v(sps->vui_seq_parameters.vcl_hrd_parameters.initial_cpb_removal_delay_length_minus1+1, "SEI: initial_cpb_removal_delay_offset" , buf);
  1418. #ifdef PRINT_BUFFERING_PERIOD_INFO
  1419.         printf("vcl initial_cpb_removal_delay[%d]        = %dn", k, initial_cpb_removal_delay);
  1420.         printf("vcl initial_cpb_removal_delay_offset[%d] = %dn", k, initial_cpb_removal_delay_offset);
  1421. #endif
  1422.       }
  1423.     }
  1424.   }
  1425.   free (buf);
  1426. #ifdef PRINT_BUFFERING_PERIOD_INFO
  1427. #undef PRINT_BUFFERING_PERIOD_INFO
  1428. #endif
  1429. }
  1430. /*!
  1431.  ************************************************************************
  1432.  *  brief
  1433.  *     Interpret the Picture timing SEI message
  1434.  *  param payload
  1435.  *     a pointer that point to the sei payload
  1436.  *  param size
  1437.  *     the size of the sei message
  1438.  *  param img
  1439.  *     the image pointer
  1440.  *
  1441.  ************************************************************************
  1442.  */
  1443. void interpret_picture_timing_info( byte* payload, int size, ImageParameters *img )
  1444. {
  1445.   int cpb_removal_delay, dpb_output_delay, picture_structure_present_flag, picture_structure;
  1446.   int clock_time_stamp_flag;
  1447.   int ct_type, nuit_field_based_flag, counting_type, full_timestamp_flag, discontinuity_flag, cnt_dropped_flag, nframes;
  1448.   int seconds_value, minutes_value, hours_value, seconds_flag, minutes_flag, hours_flag, time_offset;
  1449.   int NumClockTs = 0;
  1450.   int i;
  1451.   int cpb_removal_len = 24;
  1452.   int dpb_output_len  = 24;
  1453.   Boolean CpbDpbDelaysPresentFlag;
  1454.   Bitstream* buf;
  1455.   if (NULL==active_sps)
  1456.   {
  1457.     fprintf (stderr, "Warning: no active SPS, timing SEI cannot be parsedn");
  1458.     return;
  1459.   }
  1460.   buf = malloc(sizeof(Bitstream));
  1461.   buf->bitstream_length = size;
  1462.   buf->streamBuffer = payload;
  1463.   buf->frame_bitoffset = 0;
  1464.   UsedBits = 0;
  1465. #ifdef PRINT_PCITURE_TIMING_INFO
  1466.   printf("Picture timing SEI messagen");
  1467. #endif
  1468.   // CpbDpbDelaysPresentFlag can also be set "by some means not specified in this Recommendation | International Standard"
  1469.   CpbDpbDelaysPresentFlag =  (Boolean) (active_sps->vui_parameters_present_flag
  1470.                               && (   (active_sps->vui_seq_parameters.nal_hrd_parameters_present_flag != 0)
  1471.                                    ||(active_sps->vui_seq_parameters.vcl_hrd_parameters_present_flag != 0)));
  1472.   if (CpbDpbDelaysPresentFlag )
  1473.   {
  1474.     if (active_sps->vui_parameters_present_flag)
  1475.     {
  1476.       if (active_sps->vui_seq_parameters.nal_hrd_parameters_present_flag)
  1477.       {
  1478.         cpb_removal_len = active_sps->vui_seq_parameters.nal_hrd_parameters.cpb_removal_delay_length_minus1 + 1;
  1479.         dpb_output_len  = active_sps->vui_seq_parameters.nal_hrd_parameters.dpb_output_delay_length_minus1  + 1;
  1480.       }
  1481.       else if (active_sps->vui_seq_parameters.vcl_hrd_parameters_present_flag)
  1482.       {
  1483.         cpb_removal_len = active_sps->vui_seq_parameters.vcl_hrd_parameters.cpb_removal_delay_length_minus1 + 1;
  1484.         dpb_output_len  = active_sps->vui_seq_parameters.vcl_hrd_parameters.dpb_output_delay_length_minus1  + 1;
  1485.       }
  1486.     }
  1487.     if ((active_sps->vui_seq_parameters.nal_hrd_parameters_present_flag)||
  1488.       (active_sps->vui_seq_parameters.vcl_hrd_parameters_present_flag))
  1489.     {
  1490.       cpb_removal_delay = u_v(cpb_removal_len, "SEI: cpb_removal_delay" , buf);
  1491.       dpb_output_delay  = u_v(dpb_output_len,  "SEI: dpb_output_delay"  , buf);
  1492. #ifdef PRINT_PCITURE_TIMING_INFO
  1493.       printf("cpb_removal_delay = %dn",cpb_removal_delay);
  1494.       printf("dpb_output_delay  = %dn",dpb_output_delay);
  1495. #endif
  1496.     }
  1497.   }
  1498.   if (!active_sps->vui_parameters_present_flag)
  1499.   {
  1500.     picture_structure_present_flag = 0;
  1501.   }
  1502.   else
  1503.   {
  1504.     picture_structure_present_flag  =  active_sps->vui_seq_parameters.pic_struct_present_flag;
  1505.   }
  1506.   if (picture_structure_present_flag)
  1507.   {
  1508.     picture_structure = u_v(4, "SEI: pic_struct" , buf);
  1509. #ifdef PRINT_PCITURE_TIMING_INFO
  1510.     printf("picture_structure = %dn",picture_structure);
  1511. #endif
  1512.     switch (picture_structure)
  1513.     {
  1514.     case 0:
  1515.     case 1:
  1516.     case 2:
  1517.       NumClockTs = 1;
  1518.       break;
  1519.     case 3:
  1520.     case 4:
  1521.     case 7:
  1522.       NumClockTs = 2;
  1523.       break;
  1524.     case 5:
  1525.     case 6:
  1526.     case 8:
  1527.       NumClockTs = 3;
  1528.       break;
  1529.     default:
  1530.       error("reserved picture_structure used (can't determine NumClockTs)", 500);
  1531.     }
  1532.     for (i=0; i<NumClockTs; i++)
  1533.     {
  1534.       clock_time_stamp_flag = u_1("SEI: clock_time_stamp_flag"  , buf);
  1535. #ifdef PRINT_PCITURE_TIMING_INFO
  1536.       printf("clock_time_stamp_flag = %dn",clock_time_stamp_flag);
  1537. #endif
  1538.       if (clock_time_stamp_flag)
  1539.       {
  1540.         ct_type               = u_v(2, "SEI: ct_type"               , buf);
  1541.         nuit_field_based_flag = u_1(   "SEI: nuit_field_based_flag" , buf);
  1542.         counting_type         = u_v(5, "SEI: counting_type"         , buf);
  1543.         full_timestamp_flag   = u_1(   "SEI: full_timestamp_flag"   , buf);
  1544.         discontinuity_flag    = u_1(   "SEI: discontinuity_flag"    , buf);
  1545.         cnt_dropped_flag      = u_1(   "SEI: cnt_dropped_flag"      , buf);
  1546.         nframes               = u_v(8, "SEI: nframes"               , buf);
  1547. #ifdef PRINT_PCITURE_TIMING_INFO
  1548.         printf("ct_type               = %dn",ct_type);
  1549.         printf("nuit_field_based_flag = %dn",nuit_field_based_flag);
  1550.         printf("full_timestamp_flag   = %dn",full_timestamp_flag);
  1551.         printf("discontinuity_flag    = %dn",discontinuity_flag);
  1552.         printf("cnt_dropped_flag      = %dn",cnt_dropped_flag);
  1553.         printf("nframes               = %dn",nframes);
  1554. #endif
  1555.         if (full_timestamp_flag)
  1556.         {
  1557.           seconds_value         = u_v(6, "SEI: seconds_value"   , buf);
  1558.           minutes_value         = u_v(6, "SEI: minutes_value"   , buf);
  1559.           hours_value           = u_v(5, "SEI: hours_value"     , buf);
  1560. #ifdef PRINT_PCITURE_TIMING_INFO
  1561.           printf("seconds_value = %dn",seconds_value);
  1562.           printf("minutes_value = %dn",minutes_value);
  1563.           printf("hours_value   = %dn",hours_value);
  1564. #endif
  1565.         }
  1566.         else
  1567.         {
  1568.           seconds_flag          = u_1(   "SEI: seconds_flag" , buf);
  1569. #ifdef PRINT_PCITURE_TIMING_INFO
  1570.           printf("seconds_flag = %dn",seconds_flag);
  1571. #endif
  1572.           if (seconds_flag)
  1573.           {
  1574.             seconds_value         = u_v(6, "SEI: seconds_value"   , buf);
  1575.             minutes_flag          = u_1(   "SEI: minutes_flag" , buf);
  1576. #ifdef PRINT_PCITURE_TIMING_INFO
  1577.             printf("seconds_value = %dn",seconds_value);
  1578.             printf("minutes_flag  = %dn",minutes_flag);
  1579. #endif
  1580.             if(minutes_flag)
  1581.             {
  1582.               minutes_value         = u_v(6, "SEI: minutes_value"   , buf);
  1583.               hours_flag            = u_1(   "SEI: hours_flag" , buf);
  1584. #ifdef PRINT_PCITURE_TIMING_INFO
  1585.               printf("minutes_value = %dn",minutes_value);
  1586.               printf("hours_flag    = %dn",hours_flag);
  1587. #endif
  1588.               if(hours_flag)
  1589.               {
  1590.                 hours_value           = u_v(5, "SEI: hours_value"     , buf);
  1591. #ifdef PRINT_PCITURE_TIMING_INFO
  1592.                 printf("hours_value   = %dn",hours_value);
  1593. #endif
  1594.               }
  1595.             }
  1596.           }
  1597.         }
  1598.         {
  1599.           int time_offset_length;
  1600.           if (active_sps->vui_seq_parameters.vcl_hrd_parameters_present_flag)
  1601.             time_offset_length = active_sps->vui_seq_parameters.vcl_hrd_parameters.time_offset_length;
  1602.           else if (active_sps->vui_seq_parameters.nal_hrd_parameters_present_flag)
  1603.             time_offset_length = active_sps->vui_seq_parameters.nal_hrd_parameters.time_offset_length;
  1604.           else
  1605.             time_offset_length = 24;
  1606.           if (time_offset_length)
  1607.             time_offset = i_v(time_offset_length, "SEI: time_offset"   , buf);
  1608.           else
  1609.             time_offset = 0;
  1610. #ifdef PRINT_PCITURE_TIMING_INFO
  1611.           printf("time_offset   = %dn",time_offset);
  1612. #endif
  1613.         }
  1614.       }
  1615.     }
  1616.   }
  1617.   free (buf);
  1618. #ifdef PRINT_PCITURE_TIMING_INFO
  1619. #undef PRINT_PCITURE_TIMING_INFO
  1620. #endif
  1621. }
  1622. /*!
  1623.  ************************************************************************
  1624.  *  brief
  1625.  *     Interpret the HDR tone-mapping SEI message (JVT-T060)
  1626.  *  param payload
  1627.  *     a pointer that point to the sei payload
  1628.  *  param size
  1629.  *     the size of the sei message
  1630.  *  param img
  1631.  *     the image pointer
  1632.  *
  1633.  ************************************************************************
  1634.  */
  1635. typedef struct
  1636. {
  1637.   unsigned int  tone_map_id;
  1638.   unsigned char tone_map_cancel_flag;
  1639.   unsigned int  tone_map_repetition_period;
  1640.   unsigned char coded_data_bit_depth;
  1641.   unsigned char sei_bit_depth;
  1642.   unsigned int  model_id;
  1643.   // variables for model 0
  1644.   int  min_value;
  1645.   int  max_value;
  1646.   // variables for model 1
  1647.   int  sigmoid_midpoint;
  1648.   int  sigmoid_width;
  1649.   // variables for model 2
  1650.   int start_of_coded_interval[1<<MAX_SEI_BIT_DEPTH];
  1651.   // variables for model 3
  1652.   int num_pivots;
  1653.   int coded_pivot_value[MAX_NUM_PIVOTS];
  1654.   int sei_pivot_value[MAX_NUM_PIVOTS];
  1655. } tone_mapping_struct_tmp;
  1656. void interpret_tone_mapping( byte* payload, int size, ImageParameters *img )
  1657. {
  1658.   tone_mapping_struct_tmp seiToneMappingTmp;
  1659.   Bitstream* buf;
  1660.   int i = 0, max_coded_num, max_output_num;
  1661.   memset (&seiToneMappingTmp, 0, sizeof (tone_mapping_struct_tmp));
  1662.   buf = malloc(sizeof(Bitstream));
  1663.   buf->bitstream_length = size;
  1664.   buf->streamBuffer = payload;
  1665.   buf->frame_bitoffset = 0;
  1666.   seiToneMappingTmp.tone_map_id = ue_v("SEI: tone_map_id", buf);
  1667.   seiToneMappingTmp.tone_map_cancel_flag = u_1("SEI: tone_map_cancel_flag", buf);
  1668. #ifdef PRINT_TONE_MAPPING
  1669.   printf("Tone-mapping SEI messagen");
  1670.   printf("tone_map_id = %dn", seiToneMappingTmp.tone_map_id);
  1671.   if (seiToneMappingTmp.tone_map_id != 0)
  1672.     printf("WARNING! Tone_map_id != 0, print the SEI message info only. The tone mapping is actually applied only when Tone_map_id==0nn");
  1673.   printf("tone_map_cancel_flag = %dn", seiToneMappingTmp.tone_map_cancel_flag);
  1674. #endif
  1675.   if (!seiToneMappingTmp.tone_map_cancel_flag) 
  1676.   {
  1677.     seiToneMappingTmp.tone_map_repetition_period  = ue_v(  "SEI: tone_map_repetition_period", buf);
  1678.     seiToneMappingTmp.coded_data_bit_depth        = u_v (8,"SEI: coded_data_bit_depth"      , buf);
  1679.     seiToneMappingTmp.sei_bit_depth               = u_v (8,"SEI: sei_bit_depth"             , buf);
  1680.     seiToneMappingTmp.model_id                    = ue_v(  "SEI: model_id"                  , buf);
  1681. #ifdef PRINT_TONE_MAPPING
  1682.     printf("tone_map_repetition_period = %dn", seiToneMappingTmp.tone_map_repetition_period);
  1683.     printf("coded_data_bit_depth = %dn", seiToneMappingTmp.coded_data_bit_depth);
  1684.     printf("sei_bit_depth = %dn", seiToneMappingTmp.sei_bit_depth);
  1685.     printf("model_id = %dn", seiToneMappingTmp.model_id);
  1686. #endif
  1687.     max_coded_num  = 1<<seiToneMappingTmp.coded_data_bit_depth;
  1688.     max_output_num = 1<<seiToneMappingTmp.sei_bit_depth;
  1689.     if (seiToneMappingTmp.model_id == 0) 
  1690.     { // linear mapping with clipping
  1691.       seiToneMappingTmp.min_value   = u_v (32,"SEI: min_value", buf);
  1692.       seiToneMappingTmp.max_value   = u_v (32,"SEI: min_value", buf);
  1693. #ifdef PRINT_TONE_MAPPING
  1694.       printf("min_value = %d, max_value = %dn", seiToneMappingTmp.min_value, seiToneMappingTmp.max_value);
  1695. #endif
  1696.     }
  1697.     else if (seiToneMappingTmp.model_id == 1) 
  1698.     { // sigmoidal mapping
  1699.       seiToneMappingTmp.sigmoid_midpoint = u_v (32,"SEI: sigmoid_midpoint", buf);
  1700.       seiToneMappingTmp.sigmoid_width    = u_v (32,"SEI: sigmoid_width", buf);
  1701. #ifdef PRINT_TONE_MAPPING
  1702.       printf("sigmoid_midpoint = %d, sigmoid_width = %dn", seiToneMappingTmp.sigmoid_midpoint, seiToneMappingTmp.sigmoid_width);
  1703. #endif
  1704.     }
  1705.     else if (seiToneMappingTmp.model_id == 2) 
  1706.     { // user defined table mapping
  1707.       for (i=0; i<max_output_num; i++) 
  1708.       {
  1709.         seiToneMappingTmp.start_of_coded_interval[i] = u_v((((seiToneMappingTmp.coded_data_bit_depth+7)>>3)<<3), "SEI: start_of_coded_interval"  , buf);
  1710. #ifdef PRINT_TONE_MAPPING // too long to print
  1711.         //printf("start_of_coded_interval[%d] = %dn", i, seiToneMappingTmp.start_of_coded_interval[i]);
  1712. #endif
  1713.       }
  1714.     }
  1715.     else if (seiToneMappingTmp.model_id == 3) 
  1716.     {  // piece-wise linear mapping
  1717.       seiToneMappingTmp.num_pivots = u_v (16,"SEI: num_pivots", buf);
  1718. #ifdef PRINT_TONE_MAPPING
  1719.       printf("num_pivots = %dn", seiToneMappingTmp.num_pivots);
  1720. #endif
  1721.       seiToneMappingTmp.coded_pivot_value[0] = 0;
  1722.       seiToneMappingTmp.sei_pivot_value[0] = 0;
  1723.       seiToneMappingTmp.coded_pivot_value[seiToneMappingTmp.num_pivots+1] = max_coded_num-1;
  1724.       seiToneMappingTmp.sei_pivot_value[seiToneMappingTmp.num_pivots+1] = max_output_num-1;
  1725.       for (i=1; i < seiToneMappingTmp.num_pivots+1; i++) 
  1726.       {
  1727.         seiToneMappingTmp.coded_pivot_value[i] = u_v( (((seiToneMappingTmp.coded_data_bit_depth+7)>>3)<<3), "SEI: coded_pivot_value", buf);
  1728.         seiToneMappingTmp.sei_pivot_value[i] = u_v( (((seiToneMappingTmp.sei_bit_depth+7)>>3)<<3), "SEI: sei_pivot_value", buf);
  1729. #ifdef PRINT_TONE_MAPPING
  1730.         printf("coded_pivot_value[%d] = %d, sei_pivot_value[%d] = %dn", i, seiToneMappingTmp.coded_pivot_value[i], i, seiToneMappingTmp.sei_pivot_value[i]);
  1731. #endif
  1732.       }
  1733.     }
  1734. #if (ENABLE_OUTPUT_TONEMAPPING)
  1735.     // Currently, only when the map_id == 0, the tone-mapping is actually applied.
  1736.     if (seiToneMappingTmp.tone_map_id== 0) 
  1737.     {
  1738.       int j;
  1739.       seiToneMapping.seiHasTone_mapping = TRUE;
  1740.       seiToneMapping.tone_map_repetition_period = seiToneMappingTmp.tone_map_repetition_period;
  1741.       seiToneMapping.coded_data_bit_depth = seiToneMappingTmp.coded_data_bit_depth;
  1742.       seiToneMapping.sei_bit_depth = seiToneMappingTmp.sei_bit_depth;
  1743.       seiToneMapping.model_id = seiToneMappingTmp.model_id;
  1744.       seiToneMapping.count = 0;
  1745.       // generate the look up table of tone mapping
  1746.       switch(seiToneMappingTmp.model_id)
  1747.       {
  1748.       case 0:            // linear mapping with clipping
  1749.         for (i=0; i<=seiToneMappingTmp.min_value; i++)
  1750.           seiToneMapping.lut[i] = 0;
  1751.         for (i=seiToneMappingTmp.min_value+1; i < seiToneMappingTmp.max_value; i++)
  1752.           seiToneMapping.lut[i] = (i-seiToneMappingTmp.min_value) * (max_output_num-1)/(seiToneMappingTmp.max_value- seiToneMappingTmp.min_value);
  1753.         for (i=seiToneMappingTmp.max_value; i<max_coded_num; i++)
  1754.           seiToneMapping.lut[i] = max_output_num-1;
  1755.         break;
  1756.       case 1: // sigmoid mapping
  1757.         for (i=0; i < max_coded_num; i++) 
  1758.         {
  1759. #if 0
  1760.           int j = (int)(1 + exp( -6*(double)(i-seiToneMappingTmp.sigmoid_midpoint)/seiToneMappingTmp.sigmoid_width));
  1761.           seiToneMapping.lut[i] = ((max_output_num-1)+(j>>1)) / j;
  1762. #else
  1763.           double tmp = 1.0 + exp( -6*(double)(i-seiToneMappingTmp.sigmoid_midpoint)/seiToneMappingTmp.sigmoid_width);
  1764.           seiToneMapping.lut[i] = (int)( (double)(max_output_num-1)/ tmp + 0.5);
  1765. #endif
  1766.         }
  1767.         break;
  1768.       case 2: // user defined table
  1769.         if (0 < max_output_num-1)
  1770.         {
  1771.           for (j=0; j<max_output_num-1; j++) 
  1772.           {
  1773.             for (i=seiToneMappingTmp.start_of_coded_interval[j]; i<seiToneMappingTmp.start_of_coded_interval[j+1]; i++) 
  1774.             {
  1775.               seiToneMapping.lut[i] = j;
  1776.             }
  1777.           }
  1778.           seiToneMapping.lut[i] = max_output_num-1;
  1779.         }
  1780.         break;
  1781.       case 3: // piecewise linear mapping
  1782.         for (j=0; j<seiToneMappingTmp.num_pivots+1; j++) 
  1783.         {
  1784. #if 0
  1785.           slope = ((seiToneMappingTmp.sei_pivot_value[j+1] - seiToneMappingTmp.sei_pivot_value[j])<<16)/(seiToneMappingTmp.coded_pivot_value[j+1]-seiToneMappingTmp.coded_pivot_value[j]);
  1786.           for (i=seiToneMappingTmp.coded_pivot_value[j]; i <= seiToneMappingTmp.coded_pivot_value[j+1]; i++) 
  1787.           {
  1788.             seiToneMapping.lut[i] = seiToneMappingTmp.sei_pivot_value[j] + (( (i - seiToneMappingTmp.coded_pivot_value[j]) * slope)>>16);
  1789.           }
  1790. #else
  1791.           double slope = (double)(seiToneMappingTmp.sei_pivot_value[j+1] - seiToneMappingTmp.sei_pivot_value[j])/(seiToneMappingTmp.coded_pivot_value[j+1]-seiToneMappingTmp.coded_pivot_value[j]);
  1792.           for (i=seiToneMappingTmp.coded_pivot_value[j]; i <= seiToneMappingTmp.coded_pivot_value[j+1]; i++) 
  1793.           {
  1794.             seiToneMapping.lut[i] = seiToneMappingTmp.sei_pivot_value[j] + (int)(( (i - seiToneMappingTmp.coded_pivot_value[j]) * slope));
  1795.           }
  1796. #endif
  1797.         }
  1798.         break;
  1799.       default:
  1800.         break;
  1801.       } // end switch
  1802.     }
  1803. #endif
  1804.   } // end !tone_map_cancel_flag
  1805.   free (buf);
  1806. }
  1807. #if (ENABLE_OUTPUT_TONEMAPPING)
  1808. // tone map using the look-up-table generated according to SEI tone mapping message
  1809. void tone_map (imgpel** imgX, imgpel* lut, int size_x, int size_y)
  1810. {
  1811.   int i, j;
  1812.   for(i=0;i<size_y;i++)
  1813.   {
  1814.     for(j=0;j<size_x;j++)
  1815.     {
  1816.       imgX[i][j] = (imgpel)lut[imgX[i][j]];
  1817.     }
  1818.   }
  1819. }
  1820. void init_tone_mapping_sei() 
  1821. {
  1822.   seiToneMapping.seiHasTone_mapping = FALSE;
  1823.   seiToneMapping.count = 0;
  1824. }
  1825. void update_tone_mapping_sei() 
  1826. {
  1827.   if(seiToneMapping.tone_map_repetition_period == 0)
  1828.   {
  1829.     seiToneMapping.seiHasTone_mapping = FALSE;
  1830.     seiToneMapping.count = 0;
  1831.   }
  1832.   else if (seiToneMapping.tone_map_repetition_period>1)
  1833.   {
  1834.     seiToneMapping.count++;
  1835.     if (seiToneMapping.count>=seiToneMapping.tone_map_repetition_period) 
  1836.     {
  1837.       seiToneMapping.seiHasTone_mapping = FALSE;
  1838.       seiToneMapping.count = 0;
  1839.     }
  1840.   }
  1841. }
  1842. #endif
  1843. /*!
  1844.  ************************************************************************
  1845.  *  brief
  1846.  *     Interpret the post filter hints SEI message (JVT-U035)
  1847.  *  param payload
  1848.  *     a pointer that point to the sei payload
  1849.  *  param size
  1850.  *     the size of the sei message
  1851.  *  param img
  1852.  *     the image pointer
  1853.  *    
  1854.  ************************************************************************
  1855.  */
  1856. void interpret_post_filter_hints_info( byte* payload, int size, ImageParameters *img )
  1857. {
  1858.   Bitstream* buf;
  1859.   unsigned int filter_hint_size_y, filter_hint_size_x, filter_hint_type, color_component, cx, cy, additional_extension_flag;
  1860.   int ***filter_hint;
  1861.   buf = malloc(sizeof(Bitstream));
  1862.   buf->bitstream_length = size;
  1863.   buf->streamBuffer = payload;
  1864.   buf->frame_bitoffset = 0;
  1865.   UsedBits = 0;
  1866.   filter_hint_size_y = ue_v("SEI: filter_hint_size_y", buf); // interpret post-filter hint SEI here
  1867.   filter_hint_size_x = ue_v("SEI: filter_hint_size_x", buf); // interpret post-filter hint SEI here
  1868.   filter_hint_type   = u_v(2, "SEI: filter_hint_type", buf); // interpret post-filter hint SEI here
  1869.   get_mem3Dint (&filter_hint, 3, filter_hint_size_y, filter_hint_size_x);
  1870.   for (color_component = 0; color_component < 3; color_component ++)
  1871.     for (cy = 0; cy < filter_hint_size_y; cy ++)
  1872.       for (cx = 0; cx < filter_hint_size_x; cx ++)
  1873.         filter_hint[color_component][cy][cx] = se_v("SEI: filter_hint", buf); // interpret post-filter hint SEI here
  1874.   additional_extension_flag = u_1("SEI: additional_extension_flag", buf); // interpret post-filter hint SEI here
  1875. #ifdef PRINT_POST_FILTER_HINT_INFO
  1876.   printf(" Post-filter hint SEI messagen");
  1877.   printf(" post_filter_hint_size_y %d n", filter_hint_size_y);
  1878.   printf(" post_filter_hint_size_x %d n", filter_hint_size_x);
  1879.   printf(" post_filter_hint_type %d n",   filter_hint_type);
  1880.   for (color_component = 0; color_component < 3; color_component ++)
  1881.     for (cy = 0; cy < filter_hint_size_y; cy ++)
  1882.       for (cx = 0; cx < filter_hint_size_x; cx ++)
  1883.         printf(" post_filter_hint[%d][%d][%d] %d n", color_component, cy, cx, filter_hint[color_component][cy][cx]);
  1884.   printf(" additional_extension_flag %d n", additional_extension_flag);
  1885. #undef PRINT_POST_FILTER_HINT_INFO
  1886. #endif
  1887.   free_mem3Dint (filter_hint);
  1888.   free( buf );
  1889. }