File: h264.c

package info (click to toggle)
vdr-plugin-femon 2.4.0-3
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 736 kB
  • sloc: ansic: 5,229; makefile: 79; sh: 13
file content (759 lines) | stat: -rw-r--r-- 31,044 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
/*
 * h264.c: Frontend Status Monitor plugin for the Video Disk Recorder
 *
 * See the README file for copyright information and how to reach the author.
 *
 */

#include "log.h"
#include "tools.h"
#include "h264.h"

const cFemonH264::t_DAR cFemonH264::darS[] =
{
  {  VIDEO_ASPECT_RATIO_1_1,      100  },
  {  VIDEO_ASPECT_RATIO_4_3,      133  },
  {  VIDEO_ASPECT_RATIO_16_9,     177  },
  {  VIDEO_ASPECT_RATIO_2_21_1,   221  },
  {  VIDEO_ASPECT_RATIO_12_11,    109  },
  {  VIDEO_ASPECT_RATIO_10_11,    90   },
  {  VIDEO_ASPECT_RATIO_16_11,    145  },
  {  VIDEO_ASPECT_RATIO_40_33,    121  },
  {  VIDEO_ASPECT_RATIO_24_11,    218  },
  {  VIDEO_ASPECT_RATIO_20_11,    181  },
  {  VIDEO_ASPECT_RATIO_32_11,    290  },
  {  VIDEO_ASPECT_RATIO_80_33,    242  },
  {  VIDEO_ASPECT_RATIO_18_11,    163  },
  {  VIDEO_ASPECT_RATIO_15_11,    136  },
  {  VIDEO_ASPECT_RATIO_64_33,    193  },
  {  VIDEO_ASPECT_RATIO_160_99,   161  },
  {  VIDEO_ASPECT_RATIO_3_2,      150  },
  {  VIDEO_ASPECT_RATIO_2_1,      200  }
};

const cFemonH264::t_SAR cFemonH264::sarS[] =
{
  { 0,   0  }, // VIDEO_ASPECT_RATIO_INVALID
  { 1,   1  }, // VIDEO_ASPECT_RATIO_1_1
  { 12,  11 }, // VIDEO_ASPECT_RATIO_12_11
  { 10,  11 }, // VIDEO_ASPECT_RATIO_10_11
  { 16,  11 }, // VIDEO_ASPECT_RATIO_16_11
  { 40,  33 }, // VIDEO_ASPECT_RATIO_40_33
  { 24,  11 }, // VIDEO_ASPECT_RATIO_24_11
  { 20,  11 }, // VIDEO_ASPECT_RATIO_20_11
  { 32,  11 }, // VIDEO_ASPECT_RATIO_32_11
  { 80,  33 }, // VIDEO_ASPECT_RATIO_80_33
  { 18,  11 }, // VIDEO_ASPECT_RATIO_18_11
  { 15,  11 }, // VIDEO_ASPECT_RATIO_15_11
  { 64,  33 }, // VIDEO_ASPECT_RATIO_64_33
  { 160, 99 }, // VIDEO_ASPECT_RATIO_160_99
  { 4,   3  }, // VIDEO_ASPECT_RATIO_4_3
  { 3,   2  }, // VIDEO_ASPECT_RATIO_3_2
  { 2,   1  }  // VIDEO_ASPECT_RATIO_2_1
};

const eVideoFormat cFemonH264::videoFormatS[] =
{
  VIDEO_FORMAT_COMPONENT,
  VIDEO_FORMAT_PAL,
  VIDEO_FORMAT_NTSC,
  VIDEO_FORMAT_SECAM,
  VIDEO_FORMAT_MAC,
  VIDEO_FORMAT_UNKNOWN,
  VIDEO_FORMAT_RESERVED
};

const uint8_t cFemonH264::seiNumClockTsTableS[9] =
{
  1, 1, 1, 2, 2, 3, 3, 2, 3
};

cFemonH264::cFemonH264(cFemonVideoIf *videoHandlerP)
: videoHandlerM(videoHandlerP),
  widthM(0),
  heightM(0),
  aspectRatioM(VIDEO_ASPECT_RATIO_INVALID),
  formatM(VIDEO_FORMAT_INVALID),
  frameRateM(0),
  bitRateM(0),
  scanM(VIDEO_SCAN_INVALID),
  cpbDpbDelaysPresentFlagM(false),
  picStructPresentFlagM(false),
  frameMbsOnlyFlagM(false),
  mbAdaptiveFrameFieldFlagM(false),
  timeOffsetLengthM(0)
{
  reset();
}

cFemonH264::~cFemonH264()
{
}

bool cFemonH264::processVideo(const uint8_t *bufP, int lenP)
{
  uint8_t nal_data[lenP];
  bool aud_found = false, sps_found = false, sei_found = true; // SEI temporarily disabled!
  const uint8_t *buf = bufP;
  const uint8_t *start = buf;
  const uint8_t *end = start + lenP;

  if (!videoHandlerM)
     return false;

  // skip PES header
  if (!PesLongEnough(lenP))
      return false;
  buf += PesPayloadOffset(buf);
  start = buf;

  reset();

  for (;;) {
      int consumed = 0;

      buf = nextStartCode(buf, end);
      if (buf >= end)
         break;

      switch (buf[3] & 0x1F) {
        case NAL_AUD:
             if (!aud_found) {
                 switch (buf[4] >> 5) {
                   case 0: case 3: case 5: // I_FRAME
                       debug2("%s Found NAL AUD at offset %d/%d", __PRETTY_FUNCTION__, int(buf - start), lenP);
                       aud_found = true;
                       break;
                   case 1: case 4: case 6: // P_FRAME;
                   case 2: case 7:         // B_FRAME;
                   default:                // NO_PICTURE;
                        break;
                   }
             }
             break;

        case NAL_SPS:
             if (!sps_found) {
               debug2("%s Found NAL SPS at offset %d/%d", __PRETTY_FUNCTION__, int(buf - start), lenP);
               int nal_len = nalUnescape(nal_data, buf + 4, int(end - buf - 4));
               consumed = parseSPS(nal_data, nal_len);
               if (consumed > 0)
                  sps_found = true;
             }
             break;

        case NAL_SEI:
             if (!sei_found) {
               debug2("%s Found NAL SEI at offset %d/%d", __PRETTY_FUNCTION__, int(buf - start), lenP);
               int nal_len = nalUnescape(nal_data, buf + 4, int(end - buf - 4));
               consumed = parseSEI(nal_data, nal_len);
               if (consumed > 0)
                  sei_found = true;
             }
             break;

        default:
             break;
        }

      if (aud_found && sps_found && sei_found)
         break;

      buf += consumed + 4;
      }

  if (aud_found) {
     videoHandlerM->SetVideoCodec(VIDEO_CODEC_H264);
     if (sps_found) {
        debug2("%s width=%d height=%d, aspect=%d format=%d bitrate=%.0f", __PRETTY_FUNCTION__, widthM, heightM, aspectRatioM, formatM, bitRateM);
        videoHandlerM->SetVideoFormat(formatM);
        videoHandlerM->SetVideoSize(widthM, heightM);
        videoHandlerM->SetVideoAspectRatio(aspectRatioM);
        videoHandlerM->SetVideoBitrate(bitRateM);
        }
     if (sps_found || sei_found) {
        debug2("%s scan=%d framerate=%.2f", __PRETTY_FUNCTION__, scanM, (scanM == VIDEO_SCAN_PROGRESSIVE) ? (frameRateM / 2) : frameRateM);
        videoHandlerM->SetVideoScan(scanM);
        videoHandlerM->SetVideoFramerate((scanM == VIDEO_SCAN_PROGRESSIVE) ? (frameRateM / 2) : frameRateM);
        }
  }

  return aud_found;
}

void cFemonH264::reset()
{
  cpbDpbDelaysPresentFlagM = false;
  picStructPresentFlagM = false;
  frameMbsOnlyFlagM = false;
  mbAdaptiveFrameFieldFlagM = false;
  timeOffsetLengthM = 0;
}

const uint8_t *cFemonH264::nextStartCode(const uint8_t *startP, const uint8_t *endP)
{
  for (endP -= 3; startP < endP; ++startP) {
      if ((startP[0] == 0x00) && (startP[1] == 0x00) && (startP[2] == 0x01))
         return startP;
      }
  return (endP + 3);
}

int cFemonH264::nalUnescape(uint8_t *dstP, const uint8_t *srcP, int lenP)
{
  int s = 0, d = 0;

  while (s < lenP - 3) {
    if (!srcP[s] && !srcP[s + 1] && srcP[s + 2] == 3) {
       dstP[d++] = srcP[s++];
       dstP[d++] = srcP[s++];
       s++; // skip emulation_prevention_three_byte
       }
    else
       dstP[d++] = srcP[s++];
    }

  while (s < lenP)
    dstP[d++] = srcP[s++];

  return d;
}

int cFemonH264::parseSPS(const uint8_t *bufP, int lenP)
{
  int profile_idc, level_idc, constraint_set3_flag, pic_order_cnt_type, i, j;
  cFemonBitStream bs(bufP, lenP);

  uint32_t width = widthM;
  uint32_t height = heightM;
  eVideoAspectRatio aspect_ratio = aspectRatioM;
  eVideoFormat format = formatM;
  double frame_rate = frameRateM;
  double bit_rate = bitRateM;
  bool cpb_dpb_delays_present_flag = cpbDpbDelaysPresentFlagM;
  bool pic_struct_present_flag = picStructPresentFlagM;
  bool frame_mbs_only_flag = frameMbsOnlyFlagM;
  bool mb_adaptive_frame_field_flag = mbAdaptiveFrameFieldFlagM;
  uint32_t time_offset_length = timeOffsetLengthM;

  profile_idc = bs.GetBits(8);              // profile_idc
  bs.SkipBit();                             // constraint_set0_flag
  bs.SkipBit();                             // constraint_set1_flag
  bs.SkipBit();                             // constraint_set2_flag
  constraint_set3_flag = bs.GetBit();       // constraint_set3_flag
  bs.SkipBits(4);                           // reserved_zero_4bits
  level_idc = bs.GetBits(8);                // level_idc
  bs.SkipUeGolomb();                        // seq_parameter_set_id
  debug2("%s profile_idc=%d level_idc=%d", __PRETTY_FUNCTION__, profile_idc, level_idc);
  switch (profile_idc) {
    case 66:                                // baseline profile
    case 77:                                // main profile
    case 88:                                // extended profile
         switch (level_idc) {
           case 10:                         // level 1.0
                bit_rate = 64000;
                break;
           case 11:                         // level 1b / 1.1
                bit_rate = constraint_set3_flag ? 128000 : 192000;
                break;
           case 12:                         // level 1.2
                bit_rate = 384000;
                break;
           case 13:                         // level 1.3
                bit_rate = 768000;
                break;
           case 20:                         // level 2.0
                bit_rate = 2000000;
                break;
           case 21:                         // level 2.1
                bit_rate = 4000000;
                break;
           case 22:                         // level 2.2
                bit_rate = 4000000;
                break;
           case 30:                         // level 3.0
                bit_rate = 10000000;
                break;
           case 31:                         // level 3.1
                bit_rate = 14000000;
                break;
           case 32:                         // level 3.2
                bit_rate = 20000000;
                break;
           case 40:                         // level 4.0
                bit_rate = 20000000;
                break;
           case 41:                         // level 4.1
                bit_rate = 50000000;
                break;
           case 42:                         // level 4.2
                bit_rate = 50000000;
                break;
           case 50:                         // level 5.0
                bit_rate = 135000000;
                break;
           case 51:                         // level 5.1
                bit_rate = 240000000;
                break;
           default:
                break;
         }
         break;
    case 100:                               // high profile
         switch (level_idc) {
           case 10:                         // level 1.0
                bit_rate = 80000;
                break;
           case 11:                         // level 1b / 1.1
                bit_rate = constraint_set3_flag ? 160000 : 240000;
                break;
           case 12:                         // level 1.2
                bit_rate = 480000;
                break;
           case 13:                         // level 1.3
                bit_rate = 960000;
                break;
           case 20:                         // level 2.0
                bit_rate = 2500000;
                break;
           case 21:                         // level 2.1
                bit_rate = 5000000;
                break;
           case 22:                         // level 2.2
                bit_rate = 5000000;
                break;
           case 30:                         // level 3.0
                bit_rate = 12500000;
                break;
           case 31:                         // level 3.1
                bit_rate = 17500000;
                break;
           case 32:                         // level 3.2
                bit_rate = 25000000;
                break;
           case 40:                         // level 4.0
                bit_rate = 25000000;
                break;
           case 41:                         // level 4.1
                bit_rate = 62500000;
                break;
           case 42:                         // level 4.2
                bit_rate = 62500000;
                break;
           case 50:                         // level 5.0
                bit_rate = 168750000;
                break;
           case 51:                         // level 5.1
                bit_rate = 300000000;
                break;
           default:
                break;
         }
         break;
    case 110:                               // high 10 profile
         switch (level_idc) {
           case 10:                         // level 1.0
                bit_rate = 192000;
                break;
           case 11:                         // level 1b / 1.1
                bit_rate = constraint_set3_flag ? 384000 : 576000;
                break;
           case 12:                         // level 1.2
                bit_rate = 115200;
                break;
           case 13:                         // level 1.3
                bit_rate = 2304000;
                break;
           case 20:                         // level 2.0
                bit_rate = 6000000;
                break;
           case 21:                         // level 2.1
                bit_rate = 12000000;
                break;
           case 22:                         // level 2.2
                bit_rate = 12000000;
                break;
           case 30:                         // level 3.0
                bit_rate = 30000000;
                break;
           case 31:                         // level 3.1
                bit_rate = 42000000;
                break;
           case 32:                         // level 3.2
                bit_rate = 60000000;
                break;
           case 40:                         // level 4.0
                bit_rate = 60000000;
                break;
           case 41:                         // level 4.1
                bit_rate = 150000000;
                break;
           case 42:                         // level 4.2
                bit_rate = 150000000;
                break;
           case 50:                         // level 5.0
                bit_rate = 405000000;
                break;
           case 51:                         // level 5.1
                bit_rate = 720000000;
                break;
           default:
                break;
         }
         break;
    case 122:                               // high 4:2:2 profile
    case 144:                               // high 4:4:4 profile
         switch (level_idc) {
           case 10:                         // level 1.0
                bit_rate = 256000;
                break;
           case 11:                         // level 1b / 1.1
                bit_rate = constraint_set3_flag ? 512000 : 768000;
                break;
           case 12:                         // level 1.2
                bit_rate = 1536000;
                break;
           case 13:                         // level 1.3
                bit_rate = 3072000;
                break;
           case 20:                         // level 2.0
                bit_rate = 8000000;
                break;
           case 21:                         // level 2.1
                bit_rate = 16000000;
                break;
           case 22:                         // level 2.2
                bit_rate = 16000000;
                break;
           case 30:                         // level 3.0
                bit_rate = 40000000;
                break;
           case 31:                         // level 3.1
                bit_rate = 56000000;
                break;
           case 32:                         // level 3.2
                bit_rate = 80000000;
                break;
           case 40:                         // level 4.0
                bit_rate = 80000000;
                break;
           case 41:                         // level 4.1
                bit_rate = 200000000;
                break;
           case 42:                         // level 4.2
                bit_rate = 200000000;
                break;
           case 50:                         // level 5.0
                bit_rate = 540000000;
                break;
           case 51:                         // level 5.1
                bit_rate = 960000000;
                break;
           default:
                break;
         }
         break;
    default:
         break;
  }
  if ((profile_idc == 100) || (profile_idc == 110) || (profile_idc == 122) || (profile_idc == 144)) {
     if (bs.GetUeGolomb() == 3)             // chroma_format_idc
        bs.SkipBit();                       // residual_colour_transform_flag
     bs.SkipUeGolomb();                     // bit_depth_luma_minus8
     bs.SkipUeGolomb();                     // bit_depth_chroma_minus8
     bs.SkipBit();                          // qpprime_y_zero_transform_bypass_flag
     if (bs.GetBit()) {                     // seq_scaling_matrix_present_flag
        for (i = 0; i < 8; ++i) {
            if (bs.GetBit()) {              // seq_scaling_list_present_flag[i]
               int last = 8, next = 8, size = (i < 6) ? 16 : 64;
               for (j = 0; j < size; ++j) {
                   if (next)
                      next = (last + bs.GetSeGolomb()) & 0xff;
                   last = next ?: last;
                   }
               }
            }
        }
     }
  bs.SkipUeGolomb();                        // log2_max_frame_num_minus4
  pic_order_cnt_type = bs.GetUeGolomb();    // pic_order_cnt_type
  if (pic_order_cnt_type == 0)
     bs.SkipUeGolomb();                     // log2_max_pic_order_cnt_lsb_minus4
  else if (pic_order_cnt_type == 1) {
     bs.SkipBit();                          // delta_pic_order_always_zero
     bs.SkipSeGolomb();                     // offset_for_non_ref_pic
     bs.SkipSeGolomb();                     // offset_for_top_to_bottom_field
     j = bs.GetUeGolomb();                  // num_ref_frames_in_pic_order_cnt_cycle
     for (i = 0; i < j; ++i)
         bs.SkipSeGolomb();                 // offset_for_ref_frame[i]
     }
  bs.SkipUeGolomb();                        // num_ref_frames
  bs.SkipBit();                             // gaps_in_frame_num_value_allowed_flag
  width  = bs.GetUeGolomb() + 1;            // pic_width_in_mbs_minus1
  height = bs.GetUeGolomb() + 1;            // pic_height_in_mbs_minus1
  frame_mbs_only_flag = bs.GetBit();        // frame_mbs_only_flag
  debug2("%s pic_width=%u", __PRETTY_FUNCTION__, width);
  debug2("%s pic_height=%u", __PRETTY_FUNCTION__, height);
  debug2("%s frame_mbs_only_flag=%d", __PRETTY_FUNCTION__, frame_mbs_only_flag);
  width  *= 16;
  height *= 16 * (frame_mbs_only_flag ? 1 : 2);
  if (!frame_mbs_only_flag)
     mb_adaptive_frame_field_flag = bs.GetBit(); // mb_adaptive_frame_field_flag
  bs.SkipBit();                             // direct_8x8_inference_flag
  if (bs.GetBit()) {                        // frame_cropping_flag
     uint32_t crop_left, crop_right, crop_top, crop_bottom;
     crop_left   = bs.GetUeGolomb();        // frame_crop_left_offset
     crop_right  = bs.GetUeGolomb();        // frame_crop_rigth_offset
     crop_top    = bs.GetUeGolomb();        // frame_crop_top_offset
     crop_bottom = bs.GetUeGolomb();        // frame_crop_bottom_offset
     debug2("%s crop_left=%d crop_top=%d crop_right=%d crop_bottom=%d", __PRETTY_FUNCTION__, crop_left, crop_top, crop_right, crop_bottom);
     width -= 2 * (crop_left + crop_right);
     if (frame_mbs_only_flag)
        height -= 2 * (crop_top + crop_bottom);
     else
        height -= 4 * (crop_top + crop_bottom);
     }
  // VUI parameters
  if (bs.GetBit()) {                        // vui_parameters_present_flag
     if (bs.GetBit()) {                     // aspect_ratio_info_present
        uint32_t aspect_ratio_idc, sar_width = 0, sar_height = 0;
        aspect_ratio_idc = bs.GetBits(8);   // aspect_ratio_idc
        debug2("%s aspect_ratio_idc=%d", __PRETTY_FUNCTION__, aspect_ratio_idc);
        if (aspect_ratio_idc == 255) {      // extended sar
           sar_width  = bs.GetBits(16);     // sar_width
           sar_height = bs.GetBits(16);     // sar_height
           }
        else if (aspect_ratio_idc < ELEMENTS(sarS)) {
           sar_width  = sarS[aspect_ratio_idc].w;
           sar_height = sarS[aspect_ratio_idc].h;
           }
        if (sar_width && sar_height) {
           int index = -1, ratio = int(100.0L * sar_width * width / sar_height / height);
           for (unsigned int i = 0; i < ELEMENTS(darS); ++i) {
               if (darS[i].ratio == ratio) {
                  index = i;
                  break;
                  }
               }
           if (index < 0) {
              if (aspect_ratio_idc == 255)
                 aspect_ratio = VIDEO_ASPECT_RATIO_EXTENDED;
              else
                 aspect_ratio = VIDEO_ASPECT_RATIO_INVALID;
              }
           else
              aspect_ratio = darS[index].dar;
           debug2("%s sar_width=%d sar_height=%d aspect_ratio=%d", __PRETTY_FUNCTION__, sar_width, sar_height, aspect_ratio);
           }
        }
     if (bs.GetBit())                       // overscan_info_present_flag
        bs.SkipBit();                       // overscan_approriate_flag
     if (bs.GetBit()) {                     // video_signal_type_present_flag
        uint32_t video_format;
        video_format = bs.GetBits(3);       // video_format
        if (video_format < ELEMENTS(videoFormatS)) {
           format = videoFormatS[video_format];
           debug2("%s video_format=%d", __PRETTY_FUNCTION__, format);
           }
        bs.SkipBit();                       // video_full_range_flag
        if (bs.GetBit()) {                  // colour_description_present_flag
           bs.SkipBits(8);                  // colour_primaries
           bs.SkipBits(8);                  // transfer_characteristics
           bs.SkipBits(8);                  // matrix_coefficients
           }
        }
     if (bs.GetBit()) {                     // chroma_loc_info_present_flag
        bs.SkipUeGolomb();                  // chroma_sample_loc_type_top_field
        bs.SkipUeGolomb();                  // chroma_sample_loc_type_bottom_field
        }
     if (bs.GetBit()) {                     // timing_info_present_flag
        uint32_t num_units_in_tick, time_scale;
        num_units_in_tick = bs.GetBits(32); // num_units_in_tick
        time_scale        = bs.GetBits(32); // time_scale
        if (num_units_in_tick > 0)
           frame_rate = time_scale / num_units_in_tick;
        bs.SkipBit();                       // fixed_frame_rate_flag
        }
     int nal_hrd_parameters_present_flag = bs.GetBit(); // nal_hrd_parameters_present_flag
     if (nal_hrd_parameters_present_flag) {
        int cpb_cnt_minus1;
        cpb_cnt_minus1 = bs.GetUeGolomb();  // cpb_cnt_minus1
        bs.SkipBits(4);                     // bit_rate_scale
        bs.SkipBits(4);                     // cpb_size_scale
        for (int i = 0; i < cpb_cnt_minus1; ++i) {
            bs.SkipUeGolomb();              // bit_rate_value_minus1[i]
            bs.SkipUeGolomb();              // cpb_size_value_minus1[i]
            bs.SkipBit();                   // cbr_flag[i]
            }
        bs.SkipBits(5);                     // initial_cpb_removal_delay_length_minus1
        bs.SkipBits(5);                     // cpb_removal_delay_length_minus1
        bs.SkipBits(5);                     // dpb_output_delay_length_minus1
        time_offset_length = bs.GetBits(5); // time_offset_length
        }
     int vlc_hrd_parameters_present_flag = bs.GetBit(); // vlc_hrd_parameters_present_flag
     if (vlc_hrd_parameters_present_flag) {
         int cpb_cnt_minus1;
         cpb_cnt_minus1 = bs.GetUeGolomb(); // cpb_cnt_minus1
         bs.SkipBits(4);                    // bit_rate_scale
         bs.SkipBits(4);                    // cpb_size_scale
         for (int i = 0; i < cpb_cnt_minus1; ++i) {
             bs.SkipUeGolomb();             // bit_rate_value_minus1[i]
             bs.SkipUeGolomb();             // cpb_size_value_minus1[i]
             bs.SkipBit();                  // cbr_flag[i]
             }
         bs.SkipBits(5);                    // initial_cpb_removal_delay_length_minus1
         bs.SkipBits(5);                    // cpb_removal_delay_length_minus1
         bs.SkipBits(5);                    // dpb_output_delay_length_minus1
         time_offset_length = bs.GetBits(5);// time_offset_length
        }
     cpb_dpb_delays_present_flag = (nal_hrd_parameters_present_flag | vlc_hrd_parameters_present_flag);
     if (cpb_dpb_delays_present_flag)
        bs.SkipBit();                       // low_delay_hrd_flag
     pic_struct_present_flag = bs.GetBit(); // pic_struct_present_flag
     if (bs.GetBit()) {                     // bitstream_restriction_flag
        bs.SkipBit();                       // motion_vectors_over_pic_boundaries_flag
        bs.SkipUeGolomb();                  // max_bytes_per_pic_denom
        bs.SkipUeGolomb();                  // max_bits_per_mb_denom
        bs.SkipUeGolomb();                  // log2_max_mv_length_horizontal
        bs.SkipUeGolomb();                  // log2_max_mv_length_vertical
        bs.SkipUeGolomb();                  // num_reorder_frames
        bs.SkipUeGolomb();                  // max_dec_frame_buffering
        }
     }

  widthM = width;
  heightM = height;
  aspectRatioM = aspect_ratio;
  formatM = format;
  scanM = frame_mbs_only_flag ? VIDEO_SCAN_PROGRESSIVE : VIDEO_SCAN_INTERLACED;
  frameRateM = frame_rate;
  bitRateM = bit_rate;
  cpbDpbDelaysPresentFlagM = cpb_dpb_delays_present_flag;
  picStructPresentFlagM = pic_struct_present_flag;
  frameMbsOnlyFlagM = frame_mbs_only_flag;
  mbAdaptiveFrameFieldFlagM = mb_adaptive_frame_field_flag;
  timeOffsetLengthM = time_offset_length;

  return (bs.Index() / 8);
}

int cFemonH264::parseSEI(const uint8_t *bufP, int lenP)
{
  int num_referenced_subseqs, i;
  cFemonBitStream bs(bufP, lenP);

  eVideoScan scan = scanM;

  while ((bs.Index() * 8 + 16) < lenP) {                 // sei_message
    int lastByte, payloadSize = 0, payloadType = 0;

    do {
       lastByte = bs.GetBits(8) & 0xFF;
       payloadType += lastByte;
    } while (lastByte == 0xFF);                          // last_payload_type_byte

    do {
       lastByte = bs.GetBits(8) & 0xFF;
       payloadSize += lastByte;
    } while (lastByte == 0xFF);                          // last_payload_size_byte

    switch (payloadType) {                               // sei_payload
      case 1:                                            // pic_timing
           if (cpbDpbDelaysPresentFlagM) {               // cpb_dpb_delays_present_flag
              bs.SkipUeGolomb();                         // cpb_removal_delay
              bs.SkipUeGolomb();                         // dpb_output_delay
              }
           if (picStructPresentFlagM) {                  // pic_struct_present_flag
              uint32_t pic_struct, ct_type = 0, i = 0;
              pic_struct = bs.GetBits(4);                // pic_struct
              if (pic_struct >= ELEMENTS(seiNumClockTsTableS))
                 return 0;
              if (frameMbsOnlyFlagM && !mbAdaptiveFrameFieldFlagM)
                 scan = VIDEO_SCAN_PROGRESSIVE;
              else {
                 switch (pic_struct) {
                   case 0:                               // frame
                   case 7:                               // frame doubling
                   case 8:                               // frame tripling
                        scan = VIDEO_SCAN_PROGRESSIVE;
                        break;
                   case 1:                               // top
                   case 2:                               // bottom
                   case 3:                               // top bottom
                   case 4:                               // bottom top
                   case 5:                               // top bottom top
                   case 6:                               // bottom top bottom
                        scan = VIDEO_SCAN_INTERLACED;
                        break;
                   default:
                        scan = VIDEO_SCAN_RESERVED;
                        break;
                   }
                }
              debug2("%s pic_struct=%d scan_type=%d", __PRETTY_FUNCTION__, pic_struct, scan);
              for (i = 0; i < seiNumClockTsTableS[pic_struct]; ++i) {
                  if (bs.GetBit()) {                     // clock_timestamp_flag[i]
                     int full_timestamp_flag;
                     ct_type |= (1 << bs.GetBits(2));    // ct_type
                     debug2("%s ct_type=%04X", __PRETTY_FUNCTION__, ct_type);
                     bs.SkipBit();                       // nuit_field_based_flag
                     bs.SkipBits(5);                     // counting_type
                     full_timestamp_flag = bs.GetBit();  // full_timestamp_flag
                     bs.SkipBit();                       // discontinuity_flag
                     bs.SkipBit();                       // cnt_dropped_flag
                     bs.SkipBits(8);                     // n_frames
                     if (full_timestamp_flag) {
                        bs.SkipBits(6);                  // seconds_value
                        bs.SkipBits(6);                  // minutes_value
                        bs.SkipBits(5);                  // hours_value
                        }
                     else {
                        if (bs.GetBit()) {               // seconds_flag
                           bs.SkipBits(6);               // seconds_value
                           if (bs.GetBit()) {            // minutes_flag
                              bs.SkipBits(6);            // minutes_value
                              if (bs.GetBit())           // hours_flag
                                  bs.SkipBits(5);        // hours_value
                              }
                           }
                        }
                     if (timeOffsetLengthM > 0)
                        bs.SkipBits(timeOffsetLengthM);  // time_offset
                     }
                  }
              if (i > 0)
                 scan = (ct_type & (1 << 1)) ? VIDEO_SCAN_INTERLACED : VIDEO_SCAN_PROGRESSIVE;
              }
           break;

      case 12:                                           // sub_seq_characteristics
           bs.SkipUeGolomb();                            // sub_seq_layer_num
           bs.SkipUeGolomb();                            // sub_seq_id
           if (bs.GetBit())                              // duration_flag
              bs.SkipBits(32);                           // sub_seq_duration
           if (bs.GetBit()) {                            // average_rate_flag
              bs.SkipBit();                              // accurate_statistics_flag
              bs.SkipBits(16);                           // average_bit_rate (1000 bit/s)
              bs.SkipBits(16);                           // average_frame_rate (frames per 256s)
              }
           num_referenced_subseqs = bs.GetUeGolomb();    // num_referenced_subseqs
           for (i = 0; i < num_referenced_subseqs; ++i) {
               bs.SkipUeGolomb();                        // ref_sub_seq_layer_num
               bs.SkipUeGolomb();                        // ref_sub_seq_id
               bs.GetBit();                              // ref_sub_seq_direction
               }
           break;

      default:
           bs.SkipBits(payloadSize * 8);
           break;
      }

    // force byte align
    bs.ByteAlign();
    }

  scanM = scan;

  return (bs.Index() / 8);
}