Subversion Repositories shark

Rev

Rev 1085 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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