OpenCores
URL https://opencores.org/ocsvn/mpeg2fpga/mpeg2fpga/trunk

Subversion Repositories mpeg2fpga

[/] [mpeg2fpga/] [trunk/] [tools/] [mpeg2dec/] [gethdr.c] - Blame information for rev 2

Details | Compare with Previous | View Log

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

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.