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