Subversion Repositories shark

Rev

Details | Last modification | View Log | RSS feed

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