-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgsth264parser.c
2521 lines (2116 loc) · 75.6 KB
/
gsth264parser.c
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
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* Gstreamer
* Copyright (C) <2011> Intel Corporation
* Copyright (C) <2011> Collabora Ltd.
* Copyright (C) <2011> Thibault Saunier <[email protected]>
*
* Some bits C-c,C-v'ed and s/4/3 from h264parse and videoparsers/h264parse.c:
* Copyright (C) <2010> Mark Nauwelaerts <[email protected]>
* Copyright (C) <2010> Collabora Multimedia
* Copyright (C) <2010> Nokia Corporation
*
* (C) 2005 Michal Benes <[email protected]>
* (C) 2008 Wim Taymans <[email protected]>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
* Boston, MA 02110-1301, USA.
*/
/**
* SECTION:gsth264parser
* @short_description: Convenience library for h264 video
* bitstream parsing.
*
* It offers you bitstream parsing in AVC mode or not. To identify Nals in a bitstream and
* parse its headers, you should call:
* <itemizedlist>
* <listitem>
* #gst_h264_parser_identify_nalu to identify the following nalu in not AVC bitstreams
* </listitem>
* <listitem>
* #gst_h264_parser_identify_nalu_avc to identify the nalu in AVC bitstreams
* </listitem>
* </itemizedlist>
*
* Then, depending on the #GstH264NalUnitType of the newly parsed #GstH264NalUnit, you should
* call the differents functions to parse the structure:
* <itemizedlist>
* <listitem>
* From #GST_H264_NAL_SLICE to #GST_H264_NAL_SLICE_IDR: #gst_h264_parser_parse_slice_hdr
* </listitem>
* <listitem>
* #GST_H264_NAL_SEI: #gst_h264_parser_parse_sei
* </listitem>
* <listitem>
* #GST_H264_NAL_SPS: #gst_h264_parser_parse_sps
* </listitem>
* <listitem>
* #GST_H264_NAL_PPS: #gst_h264_parser_parse_pps
* </listitem>
* <listitem>
* Any other: #gst_h264_parser_parse_nal
* </listitem>
* </itemizedlist>
*
* Note: You should always call gst_h264_parser_parse_nal if you don't actually need
* #GstH264NalUnitType to be parsed for your personnal use, in order to guarantee that the
* #GstH264NalParser is always up to date.
*
* For more details about the structures, look at the ITU-T H.264 and ISO/IEC 14496-10 – MPEG-4
* Part 10 specifications, you can download them from:
*
* <itemizedlist>
* <listitem>
* ITU-T H.264: http://www.itu.int/rec/T-REC-H.264
* </listitem>
* <listitem>
* ISO/IEC 14496-10: http://www.iso.org/iso/iso_catalogue/catalogue_tc/catalogue_detail.htm?csnumber=56538
* </listitem>
* </itemizedlist>
*/
#include "debug.h"
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "nalutils.h"
#include "gsth264parser.h"
#include <gst/base/gstbytereader.h>
#include <gst/base/gstbitreader.h>
#include <string.h>
#define UNUSED_ARGUMENT(x_) (void)(x_)
GST_DEBUG_CATEGORY (h264_parser_debug);
#define GST_CAT_DEFAULT h264_parser_debug
static gboolean initialized = FALSE;
#define INITIALIZE_DEBUG_CATEGORY \
if (!initialized) { \
GST_DEBUG_CATEGORY_INIT (h264_parser_debug, "codecparsers_h264", 0, \
"h264 parser library"); \
initialized = TRUE; \
}
/**** Default scaling_lists according to Table 7-2 *****/
static const guint8 default_4x4_intra[16] = {
6, 13, 13, 20, 20, 20, 28, 28, 28, 28, 32, 32,
32, 37, 37, 42
};
static const guint8 default_4x4_inter[16] = {
10, 14, 14, 20, 20, 20, 24, 24, 24, 24, 27, 27,
27, 30, 30, 34
};
static const guint8 default_8x8_intra[64] = {
6, 10, 10, 13, 11, 13, 16, 16, 16, 16, 18, 18,
18, 18, 18, 23, 23, 23, 23, 23, 23, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27,
27, 27, 27, 27, 27, 29, 29, 29, 29, 29, 29, 29, 31, 31, 31, 31, 31, 31, 33,
33, 33, 33, 33, 36, 36, 36, 36, 38, 38, 38, 40, 40, 42
};
static const guint8 default_8x8_inter[64] = {
9, 13, 13, 15, 13, 15, 17, 17, 17, 17, 19, 19,
19, 19, 19, 21, 21, 21, 21, 21, 21, 22, 22, 22, 22, 22, 22, 22, 24, 24, 24,
24, 24, 24, 24, 24, 25, 25, 25, 25, 25, 25, 25, 27, 27, 27, 27, 27, 27, 28,
28, 28, 28, 28, 30, 30, 30, 30, 32, 32, 32, 33, 33, 35
};
static const guint8 zigzag_8x8[64] = {
0, 1, 8, 16, 9, 2, 3, 10,
17, 24, 32, 25, 18, 11, 4, 5,
12, 19, 26, 33, 40, 48, 41, 34,
27, 20, 13, 6, 7, 14, 21, 28,
35, 42, 49, 56, 57, 50, 43, 36,
29, 22, 15, 23, 30, 37, 44, 51,
58, 59, 52, 45, 38, 31, 39, 46,
53, 60, 61, 54, 47, 55, 62, 63
};
static const guint8 zigzag_4x4[16] = {
0, 1, 4, 8,
5, 2, 3, 6,
9, 12, 13, 10,
7, 11, 14, 15,
};
typedef struct
{
guint par_n, par_d;
} PAR;
/* Table E-1 - Meaning of sample aspect ratio indicator (1..16) */
static const PAR aspect_ratios[17] = {
{0, 0},
{1, 1},
{12, 11},
{10, 11},
{16, 11},
{40, 33},
{24, 11},
{20, 11},
{32, 11},
{80, 33},
{18, 11},
{15, 11},
{64, 33},
{160, 99},
{4, 3},
{3, 2},
{2, 1}
};
/***** Utils ****/
#define EXTENDED_SAR 255
static GstH264SPS *
gst_h264_parser_get_sps (GstH264NalParser * nalparser, guint8 sps_id)
{
GstH264SPS *sps;
sps = &nalparser->sps[sps_id];
if (sps->valid)
return sps;
return NULL;
}
static GstH264PPS *
gst_h264_parser_get_pps (GstH264NalParser * nalparser, guint8 pps_id)
{
GstH264PPS *pps;
pps = &nalparser->pps[pps_id];
if (pps->valid)
return pps;
return NULL;
}
static gboolean
gst_h264_parse_nalu_header (GstH264NalUnit * nalu)
{
guint8 *data = nalu->data + nalu->offset;
guint8 svc_extension_flag;
GstBitReader br;
if (nalu->size < 1)
return FALSE;
nalu->type = (data[0] & 0x1f);
nalu->ref_idc = (data[0] & 0x60) >> 5;
nalu->idr_pic_flag = (nalu->type == 5 ? 1 : 0);
nalu->header_bytes = 1;
nalu->extension_type = GST_H264_NAL_EXTENSION_NONE;
switch (nalu->type) {
case GST_H264_NAL_PREFIX_UNIT:
case GST_H264_NAL_SLICE_EXT:
if (nalu->size < 4)
return FALSE;
gst_bit_reader_init (&br, nalu->data + nalu->offset + nalu->header_bytes,
nalu->size - nalu->header_bytes);
svc_extension_flag = gst_bit_reader_get_bits_uint8_unchecked (&br, 1);
if (svc_extension_flag) { /* SVC */
nalu->extension_type = GST_H264_NAL_EXTENSION_SVC;
} else { /* MVC */
GstH264NalUnitExtensionMVC *const mvc = &nalu->extension.mvc;
nalu->extension_type = GST_H264_NAL_EXTENSION_MVC;
mvc->non_idr_flag = gst_bit_reader_get_bits_uint8_unchecked (&br, 1);
mvc->priority_id = gst_bit_reader_get_bits_uint8_unchecked (&br, 6);
mvc->view_id = gst_bit_reader_get_bits_uint16_unchecked (&br, 10);
mvc->temporal_id = gst_bit_reader_get_bits_uint8_unchecked (&br, 3);
mvc->anchor_pic_flag = gst_bit_reader_get_bits_uint8_unchecked (&br, 1);
mvc->inter_view_flag = gst_bit_reader_get_bits_uint8_unchecked (&br, 1);
/* Update IdrPicFlag (H.7.4.1.1) */
nalu->idr_pic_flag = !mvc->non_idr_flag;
}
nalu->header_bytes += 3;
break;
default:
break;
}
GST_DEBUG ("Nal type %u, ref_idc %u", nalu->type, nalu->ref_idc);
return TRUE;
}
/*
* gst_h264_pps_copy:
* @dst_pps: The destination #GstH264PPS to copy into
* @src_pps: The source #GstH264PPS to copy from
*
* Copies @src_pps into @dst_pps.
*
* Returns: %TRUE if everything went fine, %FALSE otherwise
*/
static gboolean
gst_h264_pps_copy (GstH264PPS * dst_pps, const GstH264PPS * src_pps)
{
g_return_val_if_fail (dst_pps != NULL, FALSE);
g_return_val_if_fail (src_pps != NULL, FALSE);
gst_h264_pps_clear (dst_pps);
*dst_pps = *src_pps;
if (src_pps->slice_group_id)
dst_pps->slice_group_id = g_memdup (src_pps->slice_group_id,
src_pps->pic_size_in_map_units_minus1 + 1);
return TRUE;
}
/* Copy MVC-specific data for subset SPS header */
static gboolean
gst_h264_sps_mvc_copy (GstH264SPS * dst_sps, const GstH264SPS * src_sps)
{
GstH264SPSExtMVC *const dst_mvc = &dst_sps->extension.mvc;
const GstH264SPSExtMVC *const src_mvc = &src_sps->extension.mvc;
guint i, j, k;
g_assert (dst_sps->extension_type == GST_H264_NAL_EXTENSION_MVC);
dst_mvc->num_views_minus1 = src_mvc->num_views_minus1;
dst_mvc->view = g_new0 (GstH264SPSExtMVCView, dst_mvc->num_views_minus1 + 1);
if (!dst_mvc->view)
return FALSE;
dst_mvc->view[0].view_id = src_mvc->view[0].view_id;
for (i = 1; i <= dst_mvc->num_views_minus1; i++) {
GstH264SPSExtMVCView *const dst_view = &dst_mvc->view[i];
const GstH264SPSExtMVCView *const src_view = &src_mvc->view[i];
dst_view->view_id = src_view->view_id;
dst_view->num_anchor_refs_l0 = src_view->num_anchor_refs_l0;
for (j = 0; j < dst_view->num_anchor_refs_l0; j++)
dst_view->anchor_ref_l0[j] = src_view->anchor_ref_l0[j];
dst_view->num_anchor_refs_l1 = src_view->num_anchor_refs_l1;
for (j = 0; j < dst_view->num_anchor_refs_l1; j++)
dst_view->anchor_ref_l1[j] = src_view->anchor_ref_l1[j];
dst_view->num_non_anchor_refs_l0 = src_view->num_non_anchor_refs_l0;
for (j = 0; j < dst_view->num_non_anchor_refs_l0; j++)
dst_view->non_anchor_ref_l0[j] = src_view->non_anchor_ref_l0[j];
dst_view->num_non_anchor_refs_l1 = src_view->num_non_anchor_refs_l1;
for (j = 0; j < dst_view->num_non_anchor_refs_l1; j++)
dst_view->non_anchor_ref_l1[j] = src_view->non_anchor_ref_l1[j];
}
dst_mvc->num_level_values_signalled_minus1 =
src_mvc->num_level_values_signalled_minus1;
dst_mvc->level_value = g_new0 (GstH264SPSExtMVCLevelValue,
dst_mvc->num_level_values_signalled_minus1 + 1);
if (!dst_mvc->level_value)
return FALSE;
for (i = 0; i <= dst_mvc->num_level_values_signalled_minus1; i++) {
GstH264SPSExtMVCLevelValue *const dst_value = &dst_mvc->level_value[i];
const GstH264SPSExtMVCLevelValue *const src_value =
&src_mvc->level_value[i];
dst_value->level_idc = src_value->level_idc;
dst_value->num_applicable_ops_minus1 = src_value->num_applicable_ops_minus1;
dst_value->applicable_op = g_new0 (GstH264SPSExtMVCLevelValueOp,
dst_value->num_applicable_ops_minus1 + 1);
if (!dst_value->applicable_op)
return FALSE;
for (j = 0; j <= dst_value->num_applicable_ops_minus1; j++) {
GstH264SPSExtMVCLevelValueOp *const dst_op = &dst_value->applicable_op[j];
const GstH264SPSExtMVCLevelValueOp *const src_op =
&src_value->applicable_op[j];
dst_op->temporal_id = src_op->temporal_id;
dst_op->num_target_views_minus1 = src_op->num_target_views_minus1;
dst_op->target_view_id =
g_new (guint16, dst_op->num_target_views_minus1 + 1);
if (!dst_op->target_view_id)
return FALSE;
for (k = 0; k <= dst_op->num_target_views_minus1; k++)
dst_op->target_view_id[k] = src_op->target_view_id[k];
dst_op->num_views_minus1 = src_op->num_views_minus1;
}
}
return TRUE;
}
/*
* gst_h264_sps_copy:
* @dst_sps: The destination #GstH264SPS to copy into
* @src_sps: The source #GstH264SPS to copy from
*
* Copies @src_sps into @dst_sps.
*
* Returns: %TRUE if everything went fine, %FALSE otherwise
*/
static gboolean
gst_h264_sps_copy (GstH264SPS * dst_sps, const GstH264SPS * src_sps)
{
g_return_val_if_fail (dst_sps != NULL, FALSE);
g_return_val_if_fail (src_sps != NULL, FALSE);
gst_h264_sps_clear (dst_sps);
*dst_sps = *src_sps;
switch (dst_sps->extension_type) {
case GST_H264_NAL_EXTENSION_MVC:
if (!gst_h264_sps_mvc_copy (dst_sps, src_sps))
return FALSE;
break;
}
return TRUE;
}
/****** Parsing functions *****/
static gboolean
gst_h264_parse_hrd_parameters (GstH264HRDParams * hrd, NalReader * nr)
{
guint sched_sel_idx;
GST_DEBUG ("parsing \"HRD Parameters\"");
READ_UE_MAX (nr, hrd->cpb_cnt_minus1, 31);
READ_UINT8 (nr, hrd->bit_rate_scale, 4);
READ_UINT8 (nr, hrd->cpb_size_scale, 4);
for (sched_sel_idx = 0; sched_sel_idx <= hrd->cpb_cnt_minus1; sched_sel_idx++) {
READ_UE (nr, hrd->bit_rate_value_minus1[sched_sel_idx]);
READ_UE (nr, hrd->cpb_size_value_minus1[sched_sel_idx]);
READ_UINT8 (nr, hrd->cbr_flag[sched_sel_idx], 1);
}
READ_UINT8 (nr, hrd->initial_cpb_removal_delay_length_minus1, 5);
READ_UINT8 (nr, hrd->cpb_removal_delay_length_minus1, 5);
READ_UINT8 (nr, hrd->dpb_output_delay_length_minus1, 5);
READ_UINT8 (nr, hrd->time_offset_length, 5);
return TRUE;
error:
GST_WARNING ("error parsing \"HRD Parameters\"");
return FALSE;
}
static gboolean
gst_h264_parse_vui_parameters (GstH264SPS * sps, NalReader * nr)
{
GstH264VUIParams *vui = &sps->vui_parameters;
GST_DEBUG ("parsing \"VUI Parameters\"");
/* set default values for fields that might not be present in the bitstream
and have valid defaults */
vui->video_format = 5;
vui->colour_primaries = 2;
vui->transfer_characteristics = 2;
vui->matrix_coefficients = 2;
READ_UINT8 (nr, vui->aspect_ratio_info_present_flag, 1);
if (vui->aspect_ratio_info_present_flag) {
READ_UINT8 (nr, vui->aspect_ratio_idc, 8);
if (vui->aspect_ratio_idc == EXTENDED_SAR) {
READ_UINT16 (nr, vui->sar_width, 16);
READ_UINT16 (nr, vui->sar_height, 16);
vui->par_n = vui->sar_width;
vui->par_d = vui->sar_height;
} else if (vui->aspect_ratio_idc <= 16) {
vui->par_n = aspect_ratios[vui->aspect_ratio_idc].par_n;
vui->par_d = aspect_ratios[vui->aspect_ratio_idc].par_d;
}
}
READ_UINT8 (nr, vui->overscan_info_present_flag, 1);
if (vui->overscan_info_present_flag)
READ_UINT8 (nr, vui->overscan_appropriate_flag, 1);
READ_UINT8 (nr, vui->video_signal_type_present_flag, 1);
if (vui->video_signal_type_present_flag) {
READ_UINT8 (nr, vui->video_format, 3);
READ_UINT8 (nr, vui->video_full_range_flag, 1);
READ_UINT8 (nr, vui->colour_description_present_flag, 1);
if (vui->colour_description_present_flag) {
READ_UINT8 (nr, vui->colour_primaries, 8);
READ_UINT8 (nr, vui->transfer_characteristics, 8);
READ_UINT8 (nr, vui->matrix_coefficients, 8);
}
}
READ_UINT8 (nr, vui->chroma_loc_info_present_flag, 1);
if (vui->chroma_loc_info_present_flag) {
READ_UE_MAX (nr, vui->chroma_sample_loc_type_top_field, 5);
READ_UE_MAX (nr, vui->chroma_sample_loc_type_bottom_field, 5);
}
READ_UINT8 (nr, vui->timing_info_present_flag, 1);
if (vui->timing_info_present_flag) {
READ_UINT32 (nr, vui->num_units_in_tick, 32);
if (vui->num_units_in_tick == 0)
GST_WARNING ("num_units_in_tick = 0 detected in stream "
"(incompliant to H.264 E.2.1).");
READ_UINT32 (nr, vui->time_scale, 32);
if (vui->time_scale == 0)
GST_WARNING ("time_scale = 0 detected in stream "
"(incompliant to H.264 E.2.1).");
READ_UINT8 (nr, vui->fixed_frame_rate_flag, 1);
}
READ_UINT8 (nr, vui->nal_hrd_parameters_present_flag, 1);
if (vui->nal_hrd_parameters_present_flag) {
if (!gst_h264_parse_hrd_parameters (&vui->nal_hrd_parameters, nr))
goto error;
}
READ_UINT8 (nr, vui->vcl_hrd_parameters_present_flag, 1);
if (vui->vcl_hrd_parameters_present_flag) {
if (!gst_h264_parse_hrd_parameters (&vui->vcl_hrd_parameters, nr))
goto error;
}
if (vui->nal_hrd_parameters_present_flag ||
vui->vcl_hrd_parameters_present_flag)
READ_UINT8 (nr, vui->low_delay_hrd_flag, 1);
READ_UINT8 (nr, vui->pic_struct_present_flag, 1);
READ_UINT8 (nr, vui->bitstream_restriction_flag, 1);
if (vui->bitstream_restriction_flag) {
READ_UINT8 (nr, vui->motion_vectors_over_pic_boundaries_flag, 1);
READ_UE (nr, vui->max_bytes_per_pic_denom);
READ_UE_MAX (nr, vui->max_bits_per_mb_denom, 16);
READ_UE_MAX (nr, vui->log2_max_mv_length_horizontal, 16);
READ_UE_MAX (nr, vui->log2_max_mv_length_vertical, 16);
READ_UE (nr, vui->num_reorder_frames);
READ_UE (nr, vui->max_dec_frame_buffering);
}
return TRUE;
error:
GST_WARNING ("error parsing \"VUI Parameters\"");
return FALSE;
}
static gboolean
gst_h264_parser_parse_scaling_list (NalReader * nr,
guint8 scaling_lists_4x4[6][16], guint8 scaling_lists_8x8[6][64],
const guint8 fallback_4x4_inter[16], const guint8 fallback_4x4_intra[16],
const guint8 fallback_8x8_inter[64], const guint8 fallback_8x8_intra[64],
guint8 n_lists)
{
guint i;
static const guint8 *default_lists[12] = {
default_4x4_intra, default_4x4_intra, default_4x4_intra,
default_4x4_inter, default_4x4_inter, default_4x4_inter,
default_8x8_intra, default_8x8_inter,
default_8x8_intra, default_8x8_inter,
default_8x8_intra, default_8x8_inter
};
GST_DEBUG ("parsing scaling lists");
for (i = 0; i < 12; i++) {
gboolean use_default = FALSE;
if (i < n_lists) {
guint8 scaling_list_present_flag;
READ_UINT8 (nr, scaling_list_present_flag, 1);
if (scaling_list_present_flag) {
guint8 *scaling_list;
guint size;
guint j;
guint8 last_scale, next_scale;
if (i < 6) {
scaling_list = scaling_lists_4x4[i];
size = 16;
} else {
scaling_list = scaling_lists_8x8[i - 6];
size = 64;
}
last_scale = 8;
next_scale = 8;
for (j = 0; j < size; j++) {
if (next_scale != 0) {
gint32 delta_scale;
READ_SE (nr, delta_scale);
next_scale = (last_scale + delta_scale) & 0xff;
}
if (j == 0 && next_scale == 0) {
/* Use default scaling lists (7.4.2.1.1.1) */
memcpy (scaling_list, default_lists[i], size);
break;
}
last_scale = scaling_list[j] =
(next_scale == 0) ? last_scale : next_scale;
}
} else
use_default = TRUE;
} else
use_default = TRUE;
if (use_default) {
switch (i) {
case 0:
memcpy (scaling_lists_4x4[0], fallback_4x4_intra, 16);
break;
case 1:
memcpy (scaling_lists_4x4[1], scaling_lists_4x4[0], 16);
break;
case 2:
memcpy (scaling_lists_4x4[2], scaling_lists_4x4[1], 16);
break;
case 3:
memcpy (scaling_lists_4x4[3], fallback_4x4_inter, 16);
break;
case 4:
memcpy (scaling_lists_4x4[4], scaling_lists_4x4[3], 16);
break;
case 5:
memcpy (scaling_lists_4x4[5], scaling_lists_4x4[4], 16);
break;
case 6:
memcpy (scaling_lists_8x8[0], fallback_8x8_intra, 64);
break;
case 7:
memcpy (scaling_lists_8x8[1], fallback_8x8_inter, 64);
break;
case 8:
memcpy (scaling_lists_8x8[2], scaling_lists_8x8[0], 64);
break;
case 9:
memcpy (scaling_lists_8x8[3], scaling_lists_8x8[1], 64);
break;
case 10:
memcpy (scaling_lists_8x8[4], scaling_lists_8x8[2], 64);
break;
case 11:
memcpy (scaling_lists_8x8[5], scaling_lists_8x8[3], 64);
break;
default:
break;
}
}
}
return TRUE;
error:
GST_WARNING ("error parsing scaling lists");
return FALSE;
}
static gboolean
slice_parse_ref_pic_list_modification_1 (GstH264SliceHdr * slice,
NalReader * nr, guint list, gboolean is_mvc)
{
GstH264RefPicListModification *entries;
guint8 *ref_pic_list_modification_flag, *n_ref_pic_list_modification;
guint32 modification_of_pic_nums_idc;
gsize max_entries;
guint i = 0;
if (list == 0) {
entries = slice->ref_pic_list_modification_l0;
max_entries = G_N_ELEMENTS (slice->ref_pic_list_modification_l0);
ref_pic_list_modification_flag = &slice->ref_pic_list_modification_flag_l0;
n_ref_pic_list_modification = &slice->n_ref_pic_list_modification_l0;
} else {
entries = slice->ref_pic_list_modification_l1;
max_entries = G_N_ELEMENTS (slice->ref_pic_list_modification_l1);
ref_pic_list_modification_flag = &slice->ref_pic_list_modification_flag_l1;
n_ref_pic_list_modification = &slice->n_ref_pic_list_modification_l1;
}
READ_UINT8 (nr, *ref_pic_list_modification_flag, 1);
if (*ref_pic_list_modification_flag) {
while (1) {
READ_UE (nr, modification_of_pic_nums_idc);
if (modification_of_pic_nums_idc == 0 ||
modification_of_pic_nums_idc == 1) {
READ_UE_MAX (nr, entries[i].value.abs_diff_pic_num_minus1,
slice->max_pic_num - 1);
} else if (modification_of_pic_nums_idc == 2) {
READ_UE (nr, entries[i].value.long_term_pic_num);
} else if (is_mvc && (modification_of_pic_nums_idc == 4 ||
modification_of_pic_nums_idc == 5)) {
READ_UE (nr, entries[i].value.abs_diff_view_idx_minus1);
}
entries[i++].modification_of_pic_nums_idc = modification_of_pic_nums_idc;
if (modification_of_pic_nums_idc == 3)
break;
if (i >= max_entries)
goto error;
}
}
*n_ref_pic_list_modification = i;
return TRUE;
error:
GST_WARNING ("error parsing \"Reference picture list %u modification\"",
list);
return FALSE;
}
static gboolean
slice_parse_ref_pic_list_modification (GstH264SliceHdr * slice, NalReader * nr,
gboolean is_mvc)
{
if (!GST_H264_IS_I_SLICE (slice) && !GST_H264_IS_SI_SLICE (slice)) {
if (!slice_parse_ref_pic_list_modification_1 (slice, nr, 0, is_mvc))
return FALSE;
}
if (GST_H264_IS_B_SLICE (slice)) {
if (!slice_parse_ref_pic_list_modification_1 (slice, nr, 1, is_mvc))
return FALSE;
}
return TRUE;
}
static gboolean
gst_h264_slice_parse_dec_ref_pic_marking (GstH264SliceHdr * slice,
GstH264NalUnit * nalu, NalReader * nr)
{
GstH264DecRefPicMarking *dec_ref_pic_m;
GST_DEBUG ("parsing \"Decoded reference picture marking\"");
dec_ref_pic_m = &slice->dec_ref_pic_marking;
if (nalu->idr_pic_flag) {
READ_UINT8 (nr, dec_ref_pic_m->no_output_of_prior_pics_flag, 1);
READ_UINT8 (nr, dec_ref_pic_m->long_term_reference_flag, 1);
} else {
READ_UINT8 (nr, dec_ref_pic_m->adaptive_ref_pic_marking_mode_flag, 1);
if (dec_ref_pic_m->adaptive_ref_pic_marking_mode_flag) {
guint32 mem_mgmt_ctrl_op;
GstH264RefPicMarking *refpicmarking;
dec_ref_pic_m->n_ref_pic_marking = 0;
while (1) {
refpicmarking =
&dec_ref_pic_m->ref_pic_marking[dec_ref_pic_m->n_ref_pic_marking];
READ_UE (nr, mem_mgmt_ctrl_op);
if (mem_mgmt_ctrl_op == 0)
break;
refpicmarking->memory_management_control_operation = mem_mgmt_ctrl_op;
if (mem_mgmt_ctrl_op == 1 || mem_mgmt_ctrl_op == 3)
READ_UE (nr, refpicmarking->difference_of_pic_nums_minus1);
if (mem_mgmt_ctrl_op == 2)
READ_UE (nr, refpicmarking->long_term_pic_num);
if (mem_mgmt_ctrl_op == 3 || mem_mgmt_ctrl_op == 6)
READ_UE (nr, refpicmarking->long_term_frame_idx);
if (mem_mgmt_ctrl_op == 4)
READ_UE (nr, refpicmarking->max_long_term_frame_idx_plus1);
dec_ref_pic_m->n_ref_pic_marking++;
}
}
}
return TRUE;
error:
GST_WARNING ("error parsing \"Decoded reference picture marking\"");
return FALSE;
}
static gboolean
gst_h264_slice_parse_pred_weight_table (GstH264SliceHdr * slice,
NalReader * nr, guint8 chroma_array_type)
{
GstH264PredWeightTable *p;
gint16 default_luma_weight, default_chroma_weight;
guint32 i;
GST_DEBUG ("parsing \"Prediction weight table\"");
p = &slice->pred_weight_table;
READ_UE_MAX (nr, p->luma_log2_weight_denom, 7);
/* set default values */
default_luma_weight = 1 << p->luma_log2_weight_denom;
for (i = 0; i < G_N_ELEMENTS (p->luma_weight_l0); i++)
p->luma_weight_l0[i] = default_luma_weight;
if (GST_H264_IS_B_SLICE (slice)) {
for (i = 0; i < G_N_ELEMENTS (p->luma_weight_l1); i++)
p->luma_weight_l1[i] = default_luma_weight;
}
if (chroma_array_type != 0) {
READ_UE_MAX (nr, p->chroma_log2_weight_denom, 7);
/* set default values */
default_chroma_weight = 1 << p->chroma_log2_weight_denom;
for (i = 0; i < G_N_ELEMENTS (p->chroma_weight_l0); i++) {
p->chroma_weight_l0[i][0] = default_chroma_weight;
p->chroma_weight_l0[i][1] = default_chroma_weight;
}
if (GST_H264_IS_B_SLICE (slice)) {
for (i = 0; i < G_N_ELEMENTS (p->chroma_weight_l1); i++) {
p->chroma_weight_l1[i][0] = default_chroma_weight;
p->chroma_weight_l1[i][1] = default_chroma_weight;
}
}
}
for (i = 0; i <= slice->num_ref_idx_l0_active_minus1; i++) {
guint8 luma_weight_l0_flag;
READ_UINT8 (nr, luma_weight_l0_flag, 1);
if (luma_weight_l0_flag) {
READ_SE_ALLOWED (nr, p->luma_weight_l0[i], -128, 127);
READ_SE_ALLOWED (nr, p->luma_offset_l0[i], -128, 127);
}
if (chroma_array_type != 0) {
guint8 chroma_weight_l0_flag;
gint j;
READ_UINT8 (nr, chroma_weight_l0_flag, 1);
if (chroma_weight_l0_flag) {
for (j = 0; j < 2; j++) {
READ_SE_ALLOWED (nr, p->chroma_weight_l0[i][j], -128, 127);
READ_SE_ALLOWED (nr, p->chroma_offset_l0[i][j], -128, 127);
}
}
}
}
if (GST_H264_IS_B_SLICE (slice)) {
for (i = 0; i <= slice->num_ref_idx_l1_active_minus1; i++) {
guint8 luma_weight_l1_flag;
READ_UINT8 (nr, luma_weight_l1_flag, 1);
if (luma_weight_l1_flag) {
READ_SE_ALLOWED (nr, p->luma_weight_l1[i], -128, 127);
READ_SE_ALLOWED (nr, p->luma_offset_l1[i], -128, 127);
}
if (chroma_array_type != 0) {
guint8 chroma_weight_l1_flag;
gint j;
READ_UINT8 (nr, chroma_weight_l1_flag, 1);
if (chroma_weight_l1_flag) {
for (j = 0; j < 2; j++) {
READ_SE_ALLOWED (nr, p->chroma_weight_l1[i][j], -128, 127);
READ_SE_ALLOWED (nr, p->chroma_offset_l1[i][j], -128, 127);
}
}
}
}
}
return TRUE;
error:
GST_WARNING ("error parsing \"Prediction weight table\"");
return FALSE;
}
static GstH264ParserResult
gst_h264_parser_parse_buffering_period (GstH264NalParser * nalparser,
GstH264BufferingPeriod * per, NalReader * nr)
{
GstH264SPS *sps;
guint8 sps_id;
GST_DEBUG ("parsing \"Buffering period\"");
READ_UE_MAX (nr, sps_id, GST_H264_MAX_SPS_COUNT - 1);
sps = gst_h264_parser_get_sps (nalparser, sps_id);
if (!sps) {
GST_WARNING ("couldn't find associated sequence parameter set with id: %d",
sps_id);
return GST_H264_PARSER_BROKEN_LINK;
}
per->sps = sps;
if (sps->vui_parameters_present_flag) {
GstH264VUIParams *vui = &sps->vui_parameters;
if (vui->nal_hrd_parameters_present_flag) {
GstH264HRDParams *hrd = &vui->nal_hrd_parameters;
const guint8 nbits = hrd->initial_cpb_removal_delay_length_minus1 + 1;
guint8 sched_sel_idx;
for (sched_sel_idx = 0; sched_sel_idx <= hrd->cpb_cnt_minus1;
sched_sel_idx++) {
READ_UINT32 (nr, per->nal_initial_cpb_removal_delay[sched_sel_idx],
nbits);
READ_UINT32 (nr,
per->nal_initial_cpb_removal_delay_offset[sched_sel_idx], nbits);
}
}
if (vui->vcl_hrd_parameters_present_flag) {
GstH264HRDParams *hrd = &vui->vcl_hrd_parameters;
const guint8 nbits = hrd->initial_cpb_removal_delay_length_minus1 + 1;
guint8 sched_sel_idx;
for (sched_sel_idx = 0; sched_sel_idx <= hrd->cpb_cnt_minus1;
sched_sel_idx++) {
READ_UINT32 (nr, per->vcl_initial_cpb_removal_delay[sched_sel_idx],
nbits);
READ_UINT32 (nr,
per->vcl_initial_cpb_removal_delay_offset[sched_sel_idx], nbits);
}
}
}
return GST_H264_PARSER_OK;
error:
GST_WARNING ("error parsing \"Buffering period\"");
return GST_H264_PARSER_ERROR;
}
static gboolean
gst_h264_parse_clock_timestamp (GstH264ClockTimestamp * tim,
GstH264VUIParams * vui, NalReader * nr)
{
guint8 full_timestamp_flag;
guint8 time_offset_length;
GST_DEBUG ("parsing \"Clock timestamp\"");
/* defalt values */
tim->time_offset = 0;
READ_UINT8 (nr, tim->ct_type, 2);
READ_UINT8 (nr, tim->nuit_field_based_flag, 1);
READ_UINT8 (nr, tim->counting_type, 5);
READ_UINT8 (nr, full_timestamp_flag, 1);
READ_UINT8 (nr, tim->discontinuity_flag, 1);
READ_UINT8 (nr, tim->cnt_dropped_flag, 1);
READ_UINT8 (nr, tim->n_frames, 8);
if (full_timestamp_flag) {
tim->seconds_flag = TRUE;
READ_UINT8 (nr, tim->seconds_value, 6);
tim->minutes_flag = TRUE;
READ_UINT8 (nr, tim->minutes_value, 6);
tim->hours_flag = TRUE;
READ_UINT8 (nr, tim->hours_value, 5);
} else {
READ_UINT8 (nr, tim->seconds_flag, 1);
if (tim->seconds_flag) {
READ_UINT8 (nr, tim->seconds_value, 6);
READ_UINT8 (nr, tim->minutes_flag, 1);
if (tim->minutes_flag) {
READ_UINT8 (nr, tim->minutes_value, 6);
READ_UINT8 (nr, tim->hours_flag, 1);
if (tim->hours_flag)
READ_UINT8 (nr, tim->hours_value, 5);
}
}
}
time_offset_length = 0;
if (vui->nal_hrd_parameters_present_flag)
time_offset_length = vui->nal_hrd_parameters.time_offset_length;
else if (vui->vcl_hrd_parameters_present_flag)
time_offset_length = vui->vcl_hrd_parameters.time_offset_length;
if (time_offset_length > 0)
READ_UINT32 (nr, tim->time_offset, time_offset_length);
return TRUE;
error:
GST_WARNING ("error parsing \"Clock timestamp\"");
return FALSE;
}
static GstH264ParserResult
gst_h264_parser_parse_pic_timing (GstH264NalParser * nalparser,
GstH264PicTiming * tim, NalReader * nr)
{
GST_DEBUG ("parsing \"Picture timing\"");
if (!nalparser->last_sps || !nalparser->last_sps->valid) {
GST_WARNING ("didn't get the associated sequence paramater set for the "
"current access unit");
goto error;
}
if (nalparser->last_sps->vui_parameters_present_flag) {
GstH264VUIParams *vui = &nalparser->last_sps->vui_parameters;
if (vui->nal_hrd_parameters_present_flag) {
READ_UINT32 (nr, tim->cpb_removal_delay,
vui->nal_hrd_parameters.cpb_removal_delay_length_minus1 + 1);
READ_UINT32 (nr, tim->dpb_output_delay,
vui->nal_hrd_parameters.dpb_output_delay_length_minus1 + 1);
} else if (vui->vcl_hrd_parameters_present_flag) {
READ_UINT32 (nr, tim->cpb_removal_delay,
vui->vcl_hrd_parameters.cpb_removal_delay_length_minus1 + 1);
READ_UINT32 (nr, tim->dpb_output_delay,
vui->vcl_hrd_parameters.dpb_output_delay_length_minus1 + 1);
}
if (vui->pic_struct_present_flag) {
const guint8 num_clock_ts_table[9] = {
1, 1, 1, 2, 2, 3, 3, 2, 3
};
guint8 num_clock_num_ts;