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