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