-
Notifications
You must be signed in to change notification settings - Fork 95
/
nalunit.c
1983 lines (1837 loc) · 62.4 KB
/
nalunit.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
/*
* Utilities for working with NAL units in H.264 elementary streams.
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is the MPEG TS, PS and ES tools.
*
* The Initial Developer of the Original Code is Amino Communications Ltd.
* Portions created by the Initial Developer are Copyright (C) 2008
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Amino Communications Ltd, Swavesey, Cambridge UK
*
* ***** END LICENSE BLOCK *****
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <errno.h>
#ifdef _WIN32
#include <io.h>
#else // _WIN32
#include <unistd.h>
#endif // _WIN32
#include "compat.h"
#include "printing_fns.h"
#include "es_fns.h"
#include "ts_fns.h"
#include "bitdata_fns.h"
#include "nalunit_fns.h"
#include "misc_fns.h"
#include "printing_fns.h"
#define DEBUG 0
#define REPORT_NAL_SHOWS_ADDRESS 0
/*
* Request details of the NAL unit contents as they are read
*/
extern void set_show_nal_reading_details(nal_unit_context_p context,
int show)
{
context->show_nal_details = show;
}
// ------------------------------------------------------------
// NAL unit context
// ------------------------------------------------------------
/*
* Build a new NAL unit context, for reading NAL units from an ES.
*
* Returns 0 if it succeeds, 1 if some error occurs.
*/
extern int build_nal_unit_context(ES_p es,
nal_unit_context_p *context)
{
int err;
nal_unit_context_p new = malloc(SIZEOF_NAL_UNIT_CONTEXT);
if (new == NULL)
{
print_err("### Unable to allocate NAL unit context datastructure\n");
return 1;
}
new->es = es;
new->count = 0;
new->show_nal_details = FALSE;
err = build_param_dict(&new->seq_param_dict);
if (err)
{
free(new);
return err;
}
err = build_param_dict(&new->pic_param_dict);
if (err)
{
free_param_dict(&new->seq_param_dict);
free(new);
return err;
}
*context = new;
return 0;
}
/*
* Free a NAL unit context datastructure.
*
* Clears the datastructure, frees it, and returns `context` as NULL.
*
* Does nothing if `context` is already NULL.
*/
extern void free_nal_unit_context(nal_unit_context_p *context)
{
nal_unit_context_p cc = *context;
if (cc == NULL)
return;
free_param_dict(&cc->seq_param_dict);
free_param_dict(&cc->pic_param_dict);
free(*context);
*context = NULL;
return;
}
/*
* Rewind a file being read as NAL units.
*
* A thin jacket for `seek_ES`.
*
* Doesn't unset the sequence and picture parameter dictionaries
* that have been built up when reading the file - this may possibly
* not be the desired behaviour, but should be OK for well behaved files.
*
* Returns 0 if all goes well, 1 if something goes wrong.
*/
extern int rewind_nal_unit_context(nal_unit_context_p context)
{
ES_offset start_of_file = {0,0};
return seek_ES(context->es,start_of_file);
}
// ------------------------------------------------------------
// Basic NAL unit datastructure stuff
// ------------------------------------------------------------
/*
* Build a new NAL unit datastructure.
*
* Returns 0 if it succeeds, 1 if some error occurs.
*/
extern int build_nal_unit(nal_unit_p *nal)
{
int err;
nal_unit_p new = malloc(SIZEOF_NAL_UNIT);
if (new == NULL)
{
print_err("### Unable to allocate NAL unit datastructure\n");
return 1;
}
err = setup_ES_unit(&(new->unit));
if (err)
{
print_err("### Unable to allocate NAL unit data buffer\n");
free(new);
return 1;
}
// However, we haven't yet got any actual data
new->data = NULL; // Only set to unit.data[3] when we *have* a NAL unit
new->data_len = 0;
new->rbsp = NULL;
new->rbsp_len = 0;
new->bit_data = NULL;
new->nal_unit_type = NAL_UNSPECIFIED;
new->starts_picture_decided = FALSE;
new->starts_picture = FALSE;
new->start_reason = NULL;
new->decoded = FALSE;
*nal = new;
return 0;
}
/*
* Tidy up a NAL unit datastructure after we've finished with it.
*/
static inline void clear_nal_unit(nal_unit_p nal)
{
clear_ES_unit(&(nal->unit));
nal->data = NULL;
nal->data_len = 0;
if (nal->rbsp != NULL)
{
free(nal->rbsp);
nal->rbsp_len = 0;
}
free_bitdata(&nal->bit_data);
}
/*
* Tidy up and free a NAL unit datastructure after we've finished with it.
*
* Empties the NAL unit datastructure, frees it, and sets `nal` to NULL.
*
* If `nal` is already NULL, does nothing.
*/
extern void free_nal_unit(nal_unit_p *nal)
{
if (*nal == NULL)
return;
clear_nal_unit(*nal);
free(*nal);
*nal = NULL;
}
// ------------------------------------------------------------
// Interpretive functions
// ------------------------------------------------------------
/*
* Process `data` to remove any emulation prevention bytes.
*
* That is, sort out 0x000003 sequences.
*
* Basically, if the data stream was *meant* to contain 0x0000xx,
* then it will instead contain 0x000003xx, and so we need to remove
* that extra 0x03 (in fact, there are also rules about what values
* 0xxx can take, but we shall assume that they have been followed
* by the encoder). See H.264 7.3.1
*
* - `data` is the NAL unit data array, including the first byte,
* which contains the nal_ref_idc and nal_unit_type.
* - `rbsp` is the processed data, not including said first byte.
*
* Returns 0 if it succeeds, 1 if some error occurs.
*/
static int remove_emulation_prevention(byte data[],
int data_len,
byte *rbsp[],
int *rbsp_len)
{
int ii;
int posn = 0;
byte prev1 = 27; // J. Random Number
byte prev2 = 27;
byte *tgt = NULL;
// We know we're going to produce data that is no longer than our input
tgt = malloc(data_len);
if (tgt == NULL)
{
print_err("### Cannot malloc RBSP target array\n");
return 1;
}
for (ii=1; ii<data_len; ii++) // NB: ignoring that first byte
{
if (prev2 == 0x00 && prev1 == 0x00 && data[ii] == 0x03)
; // ignore the emulation prevention 03 byte
else
tgt[posn++] = data[ii];
prev2 = prev1;
prev1 = data[ii];
}
*rbsp = tgt;
*rbsp_len = posn;
return 0;
}
/*
* Prepare for reading bit data from the RBSP.
*
* (Note that calling this more than once is safe.)
*
* Returns 0 if it succeeds, 1 if some error occurs.
*/
static inline int prepare_rbsp(nal_unit_p nal)
{
int err;
bitdata_p bd=NULL;
if (nal->bit_data != NULL)
return 0;
// Only remove the emulation 03 bytes when we think we need to
// (of course, we *could* do this as part of the bitdata byte
// reading code, but unless/until it's clear that the tradeoff
// in time/complexity is worth it, let's not bother).
if (nal->rbsp == NULL)
{
err = remove_emulation_prevention(nal->data,nal->data_len,
&(nal->rbsp),&(nal->rbsp_len));
if (err)
{
print_err("### Error removing emulation prevention bytes\n");
return 1;
}
}
err = build_bitdata(&bd,nal->rbsp,nal->rbsp_len);
if (err)
{
print_err("### Unable to build bitdata datastructure for NAL RBSP\n");
return 1;
}
nal->bit_data = bd;
return 0;
}
/*
* Look at the start of the slice header.
*
* Assumes that this *is* a NAL unit representing a slice.
*
* Don't call this directly - call read_rbsp_data() instead.
*
* If either of `seq_param_dict` or `pic_param_dict` is NULL, then
* we only read the first few entries in the RBSP (including the
* slice_type and the pic_parameter_set_id).
*
* Returns 0 if it succeeds, 1 if some error occurs.
*/
static int read_slice_data(nal_unit_p nal,
param_dict_p seq_param_dict,
param_dict_p pic_param_dict,
int show_nal_details)
{
int err;
bitdata_p bd = nal->bit_data;
uint32_t temp;
nal_slice_data_p data = &(nal->u.slice);
nal_seq_param_data_p seq_param_data = NULL;
nal_pic_param_data_p pic_param_data = NULL;
#define CHECK(name) \
if (err) \
{ \
fprint_err("### Error reading %s field from slice data\n",(name)); \
}
err = read_exp_golomb(bd,&data->first_mb_in_slice);
CHECK("first_mb_in_slice");
err = read_exp_golomb(bd,&data->slice_type);
CHECK("slice_type");
err = read_exp_golomb(bd,&temp);
CHECK("pic_parameter_set_id");
data->pic_parameter_set_id = temp;
if (show_nal_details)
{
fprint_msg("@@ NAL " OFFSET_T_FORMAT_08 "/%04d: size %d\n"
" nal_ref_idc %x nal_unit_type %02x (%s)\n",
nal->unit.start_posn.infile,nal->unit.start_posn.inpacket,
nal->data_len,
nal->nal_ref_idc,nal->nal_unit_type,
NAL_UNIT_TYPE_STR(nal->nal_unit_type));
fprint_msg(" first_mb_in_slice %u, slice_type %u (%s),"
" pic_parameter_set_id %d\n",data->first_mb_in_slice,
data->slice_type,NAL_SLICE_TYPE_STR(data->slice_type),
data->pic_parameter_set_id);
}
// If we don't have sequence/parameter sets, then we can't go any
// further. Assume the caller knew what they were doing...
if (seq_param_dict == NULL || pic_param_dict == NULL)
return 0;
// To read the frame number we need to know how long it is, which is
// determined by the value of log2_max_frame_num, which is defined in the
// relevant sequence parameter set, which is determined by the picture
// parameter set whose id we just read.
err = get_pic_param_data(pic_param_dict,data->pic_parameter_set_id,
&pic_param_data);
if (err) return 1;
err = get_seq_param_data(seq_param_dict,pic_param_data->seq_parameter_set_id,
&seq_param_data);
if (err) return 1;
// Whilst we've got the sequence parameter set to hand, it's convenient
// to remember the pic_order_cnt_type locally, so that we don't need to
// look it up again when trying to decide if this is the first VCL NAL
data->seq_param_set_pic_order_cnt_type = seq_param_data->pic_order_cnt_type;
if (show_nal_details)
fprint_msg(" seq_param_set->pic_order_cnt_type %u\n",
data->seq_param_set_pic_order_cnt_type);
err = read_bits(bd,seq_param_data->log2_max_frame_num,&data->frame_num);
CHECK("frame_num");
if (show_nal_details)
fprint_msg(" frame_num %u (%d bits)\n",data->frame_num,
seq_param_data->log2_max_frame_num);
data->field_pic_flag = 0; // value if not present - i.e., a frame
data->bottom_field_flag = 0;
data->bottom_field_flag_present = FALSE;
if (!seq_param_data->frame_mbs_only_flag)
{
err = read_bit(bd,&data->field_pic_flag);
CHECK("field_pic_flag");
if (show_nal_details)
fprint_msg(" field_pic_flag %d\n",data->field_pic_flag);
if (data->field_pic_flag)
{
data->bottom_field_flag_present = TRUE;
err = read_bit(bd,&data->bottom_field_flag);
CHECK("bottom_field_flag");
if (show_nal_details)
fprint_msg(" bottom_field_flag %d\n",data->bottom_field_flag);
}
}
if (nal->nal_unit_type == 5)
{
err = read_exp_golomb(bd,&data->idr_pic_id);
CHECK("idr_pic_id");
if (show_nal_details)
fprint_msg(" idr_pic_id %u\n",data->idr_pic_id);
}
data->delta_pic_order_cnt_bottom = 0; // value if not present
data->delta_pic_order_cnt[0] = 0;
data->delta_pic_order_cnt[1] = 0;
if (seq_param_data->pic_order_cnt_type == 0)
{
err = read_bits(bd,seq_param_data->log2_max_pic_order_cnt_lsb,
&data->pic_order_cnt_lsb);
CHECK("pic_order_cnt_lsb");
if (pic_param_data->pic_order_present_flag && !data->field_pic_flag)
{
err = read_signed_exp_golomb(bd,&data->delta_pic_order_cnt_bottom);
CHECK("delta_pic_order_cnt_bottom");
}
if (show_nal_details)
fprint_msg(" pic_order_cnt_lsb %u (%d bits)\n"
" delta_pic_order_cnt_bottom %d\n",
data->pic_order_cnt_lsb,
seq_param_data->log2_max_pic_order_cnt_lsb,
data->delta_pic_order_cnt_bottom);
}
else if (seq_param_data->pic_order_cnt_type == 1 &&
!seq_param_data->delta_pic_order_always_zero_flag)
{
err = read_signed_exp_golomb(bd,&data->delta_pic_order_cnt[0]);
CHECK("delta_pic_order_cnt[0]");
if (show_nal_details)
fprint_msg(" delta_pic_order_cnt[0] %d\n",data->delta_pic_order_cnt[0]);
if (pic_param_data->pic_order_present_flag && !data->field_pic_flag)
{
err = read_signed_exp_golomb(bd,&data->delta_pic_order_cnt[1]);
CHECK("delta_pic_order_cnt[1]");
if (show_nal_details)
fprint_msg(" delta_pic_order_cnt[1] %d\n",data->delta_pic_order_cnt[1]);
}
}
// Since we're not claiming to support redundant pictures, we could
// give up before reading the next value. However, if we *do* read
// it, we can grumble about/ignore redundant pictures if we get them,
// which seems a useful thing to be able to do.
data->redundant_pic_cnt = 0;
data->redundant_pic_cnt_present = FALSE;
if (pic_param_data->redundant_pic_cnt_present_flag)
{
data->redundant_pic_cnt_present = TRUE;
err = read_exp_golomb(bd,&data->redundant_pic_cnt);
CHECK("redundant_pic_cnt");
if (show_nal_details)
fprint_msg(" redundant_pic_cnt %u\n",data->redundant_pic_cnt);
}
nal->decoded = TRUE;
return 0;
}
/*
* Look at the start of the picture parameter set.
*
* Assumes that this *is* a NAL unit representing a picture parameter set.
*
* Don't call this directly - call read_rbsp_data() instead.
*
* Returns 0 if it succeeds, 1 if some error occurs.
*/
static int read_pic_param_set_data(nal_unit_p nal,
int show_nal_details)
{
int err;
bitdata_p bd = nal->bit_data;
nal_pic_param_data_p data = &(nal->u.pic);
uint32_t temp;
// Values that don't get saved into our NAL unit
uint32_t num_ref_idx_10_active;
uint32_t num_ref_idx_11_active;
byte weighted_pred_flag;
uint32_t weighted_bipred_idc;
int32_t pic_init_qp;
int32_t pic_init_qs;
int32_t chroma_qp_index_offset;
byte deblocking_filter_control_present_flag;
byte constrained_intra_pred_flag;
#undef CHECK
#define CHECK(name) \
if (err) \
{ \
fprint_err("### Error reading %s field from picture parameter set\n",\
(name)); \
}
// We need to know the id of this picture parameter set, and also
// which sequence parameter set it refers to
err = read_exp_golomb(bd,&temp);
CHECK("pic_parameter_set_id");
data->pic_parameter_set_id = temp;
err = read_exp_golomb(bd,&temp);
CHECK("seq_parameter_set_id");
data->seq_parameter_set_id = temp;
err = read_bit(bd,&data->entropy_coding_mode_flag);
CHECK("entropy_coding_mode_flag");
// We care about pic_order_present_flag
err = read_bit(bd,&data->pic_order_present_flag);
CHECK("pic_order_present_flag");
if (show_nal_details)
{
fprint_msg("@@ PPS " OFFSET_T_FORMAT_08 "/%04d: size %d\n"
" nal_ref_idc %x nal_unit_type %02x (%s)\n",
nal->unit.start_posn.infile,nal->unit.start_posn.inpacket,
nal->data_len,
nal->nal_ref_idc,nal->nal_unit_type,
NAL_UNIT_TYPE_STR(nal->nal_unit_type));
fprint_msg(" pic_parameter_set_id %d, seq_parameter_set_id %d\n",
data->pic_parameter_set_id,data->seq_parameter_set_id);
fprint_msg(" entropy_coding_mode_flag %d\n",
data->entropy_coding_mode_flag);
fprint_msg(" pic_order_present_flag %d\n",data->pic_order_present_flag);
}
// After this, we don't (at the moment) really need any of the rest.
// However, it is moderately useful (for paranoia's sake) to read the
// redundant_pic_cnt_present_flag at the very end, and we should not
// be interpreting the inside of a picture parameter set very often...
err = read_exp_golomb(bd,&data->num_slice_groups); // minus 1
CHECK("num_slice_groups");
data->num_slice_groups ++;
if (show_nal_details)
fprint_msg(" num_slice_groups %u\n",data->num_slice_groups);
if (data->num_slice_groups > 1)
{
err = read_exp_golomb(bd,&data->slice_group_map_type);
CHECK("slice_group_map_type");
if (show_nal_details)
fprint_msg(" slice_group_map_type %u\n",data->slice_group_map_type);
if (data->slice_group_map_type == 0)
{
// NB: 0..num_slice_groups-1, not 0..num_slice_groups-1 - 1
unsigned int igroup;
for (igroup=0; igroup < data->num_slice_groups; igroup++)
{
uint32_t ignint;
err = read_exp_golomb(bd,&ignint); // run_length_minus1[igroup]
CHECK("run_length_minus1[x]");
}
}
else if (data->slice_group_map_type == 2)
{
// But this time, 0..num_slice_groups-1 - 1
unsigned int igroup;
for (igroup=0; igroup < (data->num_slice_groups - 1); igroup++)
{
uint32_t ignint;
err = read_exp_golomb(bd,&ignint); // top_left[igroup]
CHECK("top_left[x]");
err = read_exp_golomb(bd,&ignint); // bottom_right[igroup]
CHECK("bottom_right[x]");
}
}
else if (data->slice_group_map_type == 3 ||
data->slice_group_map_type == 4 ||
data->slice_group_map_type == 5)
{
byte ignbyte;
uint32_t ignint;
err = read_bit(bd,&ignbyte); // slice_group_change_direction_flag
CHECK("slice_group_change_direction_flag");
err = read_exp_golomb(bd,&ignint); // slice_group_change_rate_minus1
CHECK("slice_group_change_rate_minus1");
}
else if (data->slice_group_map_type == 6)
{
uint32_t pic_size_in_map_units;
unsigned int ii;
int size;
err = read_exp_golomb(bd,&pic_size_in_map_units); // minus 1
pic_size_in_map_units ++;
CHECK("pic_size_in_map_units");
if (show_nal_details)
fprint_msg(" pic_size_in_map_units %u\n",pic_size_in_map_units);
size = (int) ceil(log2(data->num_slice_groups));
// Again, notice the range
for (ii=0; ii < pic_size_in_map_units; ii++)
{
uint32_t ignint;
err = read_bits(bd,size,&ignint); // slice_group_id[ii]
CHECK("slice_group_id[x]");
}
}
}
err = read_exp_golomb(bd,&num_ref_idx_10_active); // minus 1
CHECK("num_ref_idx_10_active");
num_ref_idx_10_active ++;
if (show_nal_details)
fprint_msg(" num_ref_idx_10_active %u\n",num_ref_idx_10_active);
err = read_exp_golomb(bd,&num_ref_idx_11_active); // minus 1
CHECK("num_ref_idx_11_active");
num_ref_idx_11_active ++;
if (show_nal_details)
fprint_msg(" num_ref_idx_11_active %u\n",num_ref_idx_11_active);
err = read_bit(bd,&weighted_pred_flag);
CHECK("weighted_pred_flag");
if (show_nal_details)
fprint_msg(" weighted_pred_flag %d\n",weighted_pred_flag);
err = read_bits(bd,2,&weighted_bipred_idc);
CHECK("weighted_bipred_idc");
if (show_nal_details)
fprint_msg(" weighted_bipred_idc %u\n",weighted_bipred_idc);
err = read_signed_exp_golomb(bd,&pic_init_qp); // minus 26
CHECK("pic_init_qp");
pic_init_qp += 26;
if (show_nal_details)
fprint_msg(" pic_init_qp %d\n",pic_init_qp);
err = read_signed_exp_golomb(bd,&pic_init_qs); // minus 26
CHECK("pic_init_qs");
pic_init_qs += 26;
if (show_nal_details)
fprint_msg(" pic_init_qs %d\n",pic_init_qs);
err = read_signed_exp_golomb(bd,&chroma_qp_index_offset);
CHECK("chroma_qp_index_offset");
if (show_nal_details)
fprint_msg(" chroma_qp_index_offset %d\n",chroma_qp_index_offset);
err = read_bit(bd,&deblocking_filter_control_present_flag);
CHECK("deblocking_filter_control_present_flag");
if (show_nal_details)
fprint_msg(" deblocking_filter_control_present_flag %d\n",
deblocking_filter_control_present_flag);
err = read_bit(bd,&constrained_intra_pred_flag);
CHECK("constrained_intra_pred_flag");
if (show_nal_details)
fprint_msg(" constrained_intra_pred_flag %d\n",constrained_intra_pred_flag);
// We (sort of) care about redundant_pic_cnt_present_flag
// in that we need to know it if we are going to read the later bits
// of a slice header
err = read_bit(bd,&data->redundant_pic_cnt_present_flag);
CHECK("redundant_pic_cnt_present_flag");
if (show_nal_details)
fprint_msg(" redundant_pic_cnt_present_flag %d\n",
data->redundant_pic_cnt_present_flag);
nal->decoded = TRUE;
return 0;
}
/*
* Look at the start of the sequence parameter set.
*
* Assumes that this *is* a NAL unit representing a sequence parameter set.
*
* Don't call this directly - call read_rbsp_data() instead.
*
* Returns 0 if it succeeds, 1 if some error occurs.
*/
static int read_seq_param_set_data(nal_unit_p nal,
int show_nal_details)
{
int err;
bitdata_p bd = nal->bit_data;
nal_seq_param_data_p data = &(nal->u.seq);
uint32_t temp;
// Values that don't get saved into our NAL unit
byte reserved_zero_5bits;
uint32_t num_ref_frames;
byte gaps_in_frame_num_value_allowed_flag;
uint32_t pic_width_in_mbs;
uint32_t pic_height_in_map_units;
#undef CHECK
#define CHECK(name) \
if (err) \
{ \
fprint_err("### Error reading %s field from sequence parameter set\n",\
(name)); \
}
err = read_bits_into_byte(bd,8,&data->profile_idc);
CHECK("profile_idc");
err = read_bit(bd,&data->constraint_set0_flag);
CHECK("constraint_set0_flag");
err = read_bit(bd,&data->constraint_set1_flag);
CHECK("constraint_set1_flag");
err = read_bit(bd,&data->constraint_set2_flag);
CHECK("constraint_set2_flag");
err = read_bits_into_byte(bd,5,&reserved_zero_5bits);
CHECK("reserved_zero_5bits");
if (reserved_zero_5bits != 0)
{
fprint_err("### reserved_zero_5bits not zero (%d) in sequence"
" parameter set NAL unit at " OFFSET_T_FORMAT "/%d\n",
reserved_zero_5bits,
nal->unit.start_posn.infile,nal->unit.start_posn.inpacket);
print_data(FALSE," Data",nal->bit_data->data,nal->bit_data->data_len,
20);
// Should we carry on or give up? On the whole, if this is broken
// we can't really trust the rest of its data...
return 1;
}
err = read_bits_into_byte(bd,8,&data->level_idc);
CHECK("level_idc");
if (show_nal_details)
{
fprint_msg("@@ SPS " OFFSET_T_FORMAT_08 "/%04d: size %d\n"
" nal_ref_idc %x nal_unit_type %02x (%s)\n",
nal->unit.start_posn.infile,nal->unit.start_posn.inpacket,
nal->data_len,
nal->nal_ref_idc,nal->nal_unit_type,
NAL_UNIT_TYPE_STR(nal->nal_unit_type));
fprint_msg(" profile_idc %u, constraint set flags: %d %d %d\n",
data->profile_idc,
data->constraint_set0_flag,
data->constraint_set1_flag,
data->constraint_set2_flag);
fprint_msg(" level_idc %u\n",data->level_idc);
}
err = read_exp_golomb(bd,&temp);
CHECK("seq_parameter_set_id");
data->seq_parameter_set_id = temp;
// We care about log2_max_frame_num_minus4
err = read_exp_golomb(bd,&data->log2_max_frame_num); // minus 4
CHECK("log2_max_frame_num");
data->log2_max_frame_num += 4;
// We care about pic_order_cnt_type
err = read_exp_golomb(bd,&data->pic_order_cnt_type);
CHECK("pic_order_cnt_type");
if (show_nal_details)
{
fprint_msg(" seq_parameter_set_id %u\n",data->seq_parameter_set_id);
fprint_msg(" log2_max_frame_num %u\n",data->log2_max_frame_num);
fprint_msg(" pic_order_cnt_type %u\n",data->pic_order_cnt_type);
}
if (data->pic_order_cnt_type == 0)
{
err = read_exp_golomb(bd,&data->log2_max_pic_order_cnt_lsb); // minus 4
CHECK("log2_max_pic_order_cnt_lsb");
data->log2_max_pic_order_cnt_lsb += 4;
if (show_nal_details)
fprint_msg(" log2_max_pic_order_cnt_lsb %u\n",
data->log2_max_pic_order_cnt_lsb);
}
else if (data->pic_order_cnt_type == 1)
{
unsigned int ii;
int32_t offset_for_non_ref_pic;
int32_t offset_for_top_to_bottom_field;
uint32_t num_ref_frames_in_pic_order_cnt_cycle;
err = read_bit(bd,&data->delta_pic_order_always_zero_flag);
CHECK("delta_pic_order_always_zero_flag");
if (show_nal_details)
fprint_msg(" delta_pic_order_always_zero_flag %d\n",
data->delta_pic_order_always_zero_flag);
err = read_signed_exp_golomb(bd,&offset_for_non_ref_pic);
CHECK("offset_for_non_ref_pic");
err = read_signed_exp_golomb(bd,&offset_for_top_to_bottom_field);
CHECK("offset_for_top_to_bottom_field");
err = read_exp_golomb(bd,&num_ref_frames_in_pic_order_cnt_cycle);
CHECK("num_ref_frames_in_pic_order_cnt_cycle");
// The standard says that num_ref_frames_in_pic_order_cnt_cycle
// shall be in the range 0..255
for (ii=0; ii < num_ref_frames_in_pic_order_cnt_cycle; ii++)
{
int32_t offset_for_ref_frame_XX;
err = read_signed_exp_golomb(bd,&offset_for_ref_frame_XX); // XX = [ii]
CHECK("offset_for_ref_frame_X");
}
}
err = read_exp_golomb(bd,&num_ref_frames);
CHECK("num_ref_frames");
if (show_nal_details)
fprint_msg(" num_ref_frames %u\n",num_ref_frames);
err = read_bit(bd,&gaps_in_frame_num_value_allowed_flag);
CHECK("gaps_in_frame_num_value_allowed_flag");
if (show_nal_details)
if (show_nal_details)
fprint_msg(" gaps_in_frame_num_value_allowed_flag %d\n",
gaps_in_frame_num_value_allowed_flag);
err = read_exp_golomb(bd,&pic_width_in_mbs); // minus 1
CHECK("pic_width_in_mbs");
pic_width_in_mbs ++;
if (show_nal_details)
fprint_msg(" pic_width_in_mbs %u\n",pic_width_in_mbs);
err = read_exp_golomb(bd,&pic_height_in_map_units); // minus 1
CHECK("pic_height_in_map_units");
pic_height_in_map_units ++;
if (show_nal_details)
fprint_msg(" pic_height_in_map_units %u\n",pic_height_in_map_units);
// We care about frame_mbs_only_flag
err = read_bit(bd,&data->frame_mbs_only_flag);
CHECK("frame_mbs_only_flag");
if (show_nal_details)
fprint_msg(" frame_mbs_only_flag %d\n",data->frame_mbs_only_flag);
nal->decoded = TRUE;
return 0;
}
/*
* Read the data for an SEI recovery point
*
* Returns 0 if it succeeds, 1 if some error occurs
*/
static int read_SEI_recovery_point(nal_unit_p nal,
int payloadSize,
int show_nal_details)
{
int err;
bitdata_p bd = nal->bit_data;
nal_SEI_recovery_data_p data = &(nal->u.sei_recovery);
uint32_t temp;
#undef CHECK
#define CHECK(name) \
if (err) \
{ \
fprint_err("### Error reading %s field from SEI\n", \
(name)); \
}
err = read_exp_golomb(bd,&temp);
CHECK("recovery_frame_cnt");
data->recovery_frame_cnt = temp;
err = read_bit(bd,&data->exact_match_flag);
CHECK("exact_match_flag");
err = read_bit(bd,&data->broken_link_flag);
CHECK("broken_link_flag");
err = read_bits(bd,2,&data->changing_slice_group_idc);
CHECK("changing_slice_group_idc");
nal->decoded = TRUE;
if (show_nal_details)
{
print_msg("@@ Recovery Point SEI\n");
fprint_msg(" recovery_frame_cnt %d\n exact_match_flag %d\n", data->recovery_frame_cnt, data->exact_match_flag);
fprint_msg(" broken_link_flag %d\n changing_slice_group_idc %d", data->broken_link_flag, data->changing_slice_group_idc);
}
return 0;
}
/*
* Look at the start of an SEI
*
* Assumes that this *is* a NAL unit representing an SEI
*
* Don't call this directly - call read_rbsp_data() instead.
*
* Returns 0 if it succeeds, 1 if some error occurs.
*/
static int read_SEI(nal_unit_p nal,
int show_nal_details)
{
int err;
int SEI_payloadType = 0;
int SEI_payloadSize = 0; // in byte
bitdata_p bd = nal->bit_data;
uint32_t temp = 0;
#undef CHECK
#define CHECK(name) \
if (err) \
{ \
fprint_err("### Error reading %s field from SEI\n", \
(name)); \
}
// read payloadtype (see H.264:7.3.2.3.1)
for (;;)
{
err = read_bits(bd,8,&temp);
CHECK("payloadType");
if (temp == 0xff)
SEI_payloadType += 0xff;
else
break;
}
SEI_payloadType += temp;
nal->u.sei_recovery.payloadType = SEI_payloadType;
// read payloadSize
for (;;)
{
err = read_bits(bd,8,&temp);
CHECK("payloadSize");
if (temp == 0xff)
SEI_payloadSize += 0xff;
else
break;
}
SEI_payloadSize += temp;
nal->u.sei_recovery.payloadSize = SEI_payloadSize;
if (SEI_payloadType == 6) // SEI recovery_point
err = read_SEI_recovery_point(nal, SEI_payloadSize, show_nal_details);
return 0;
}
/*
* Look at the start of the RBSP for a NAL unit
*
* Decodes some or all of the data for slices, sequence parameter sets
* and picture parameter sets.
*
* (Note that calling this more than once does not read the data
* more than once. Also note that the RBSP and bitdata datastructures
* in the NAL unit do not persist after this call.)
*
* Caveat: if either `seq_param_dict` or `pic_param_dict` is NULL, and the
* NAL unit being interpreted is an IDR or non-IDR unit (i.e., a slice),
* then only the first few values in the RBSP will be read (up to and
* including the slice_type and pic_parameter_set_id), and the RBSP will
* *not* be marked as decoded. Because of this, calling this
* function again later will cause the RBSP to be read again (strictly,
* to be re-extracted and read again).
*
* Returns 0 if it succeeds, 1 if some error occurs.
*/
static int read_rbsp_data(nal_unit_p nal,
param_dict_p seq_param_dict,
param_dict_p pic_param_dict,
int show_nal_details)
{
int err = 0;
if (nal->decoded)
return 0;
err = prepare_rbsp(nal);
if (err) return err;
if (nal->nal_unit_type == 1 || nal->nal_unit_type == 5) // Coded slice of a (non) IDR picture
err = read_slice_data(nal,seq_param_dict,pic_param_dict,show_nal_details);
else if (nal->nal_unit_type == 8) // Picture parameter set
err = read_pic_param_set_data(nal,show_nal_details);
else if (nal->nal_unit_type == 7) // Sequence parameter set
err = read_seq_param_set_data(nal,show_nal_details);
else if (nal->nal_unit_type == 6) // SEI
err = read_SEI(nal,show_nal_details);
else if (show_nal_details)
fprint_msg("@@ nal " OFFSET_T_FORMAT_08 "/%04d: size %d\n"
" nal_ref_idc %x nal_unit_type %02x (%s)\n",
nal->unit.start_posn.infile,nal->unit.start_posn.inpacket,
nal->data_len,
nal->nal_ref_idc,nal->nal_unit_type,
NAL_UNIT_TYPE_STR(nal->nal_unit_type));
if (err)
{
fprint_err("### Error reading RBSP data for %s NAL (ref idc %x,"
" unit type %x) at " OFFSET_T_FORMAT_08 "/%04d\n",
NAL_UNIT_TYPE_STR(nal->nal_unit_type),
nal->nal_ref_idc,
nal->nal_unit_type,
nal->unit.start_posn.infile,
nal->unit.start_posn.inpacket);
}
// At this point, we've finished with the actual RBSP data
// so we might as well free it and save some space.
if (nal->rbsp != NULL)
{
free(nal->rbsp);
nal->rbsp = NULL;
nal->rbsp_len = 0;
free_bitdata(&nal->bit_data);
}
return err;