/
reframe_nalu.c
3949 lines (3512 loc) · 124 KB
/
reframe_nalu.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
/*
* GPAC - Multimedia Framework C SDK
*
* Authors: Jean Le Feuvre
* Copyright (c) Telecom ParisTech 2000-2022
* All rights reserved
*
* This file is part of GPAC / NALU (AVC, HEVC, VVC) reframer filter
*
* GPAC is free software; you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* GPAC 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 Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; see the file COPYING. If not, write to
* the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
*
*/
#include <gpac/avparse.h>
#include <gpac/constants.h>
#include <gpac/filters.h>
#include <gpac/internal/media_dev.h>
//for oinf stuff
#include <gpac/internal/isomedia_dev.h>
#ifndef GPAC_DISABLE_AV_PARSERS
#define CTS_POC_OFFSET_SAFETY 1000
GF_Err gf_bs_set_logger(GF_BitStream *bs, void (*on_bs_log)(void *udta, const char *field_name, u32 nb_bits, u64 field_val, s32 idx1, s32 idx2, s32 idx3), void *udta);
enum
{
DVMODE_NONE=0,
DVMODE_AUTO,
DVMODE_FORCE,
DVMODE_CLEAN,
DVMODE_SINGLE,
};
typedef struct
{
u64 pos;
Double duration;
} NALUIdx;
typedef struct
{
u32 layer_id_plus_one;
u32 min_temporal_id, max_temporal_id;
} LHVCLayerInfo;
enum {
STRICT_POC_OFF = 0,
STRICT_POC_ON,
STRICT_POC_ERROR,
};
typedef struct
{
//filter args
GF_Fraction fps;
Double index;
Bool explicit, force_sync, nosei, importer, subsamples, nosvc, novpsext, deps, seirw, audelim, analyze, notime;
u32 nal_length;
u32 strict_poc;
u32 bsdbg;
GF_Fraction dur;
u32 dv_mode, dv_profile, dv_compatid;
//only one input pid declared
GF_FilterPid *ipid;
//only one output pid declared
GF_FilterPid *opid;
//read bitstream for AVC/HEVC parsing
GF_BitStream *bs_r;
//write bitstream for nalus size length rewrite
GF_BitStream *bs_w;
//current CTS/DTS of the stream, may be overridden by input packet if not file (eg TS PES)
u64 cts, dts, prev_dts, prev_cts;
u32 pck_duration;
//basic config stored here: with, height CRC of base and enh layer decoder config, sample aspect ratio
//when changing, a new pid config will be emitted
u32 width, height;
u32 crc_cfg, crc_cfg_enh;
GF_Fraction sar;
GF_Fraction cur_fps;
//duration of the file if known
GF_Fraction64 duration;
//playback start range
Double start_range;
//indicates we are in seek, packets before start range should be marked
Bool in_seek;
//set once we play something
Bool is_playing;
//is a file, is a file fully loaded on disk (local or download done)
Bool is_file, file_loaded;
//initial PLAY command found
Bool initial_play_done;
//list of RAP entry points
NALUIdx *indexes;
u32 index_alloc_size, index_size;
//timescale of the input pid if any, 0 otherwise
u32 timescale;
//framing flag of input packet when input pid has timing (eg is not a file)
Bool input_is_au_start;
GF_FilterPacket *src_pck;
Bool full_au_source;
//total delay in frames between decode and presentation
s32 max_total_delay;
//max size codable with our nal_length setting
u32 max_nalu_size_allowed;
//position in input packet from which we resume parsing
u32 resume_from;
//prevents message about possible NAL size optimizaion at finalization
Bool nal_adjusted;
//avc/hevc switch
u32 codecid;
//name of the logger
const char *log_name;
//list of packet (in decode order !!) not yet dispatched.
//Dispatch depends on the mode:
//strict_poc=0: we wait after each IDR until we find a stable poc diff between pictures, controled by poc_probe_done
//strict_poc>=1: we dispatch only after IDR or at the end (huge delay)
GF_List *pck_queue;
//dts of the last IDR found
u64 dts_last_IDR;
//max size of NALUs in the bitstream
u32 max_nalu_size;
u8 *nal_store;
u32 nal_store_size, nal_store_alloc;
//list of param sets found
GF_List *sps, *pps, *vps, *sps_ext, *pps_svc, *vvc_aps_pre, *vvc_dci;
//set to true if one of the PS has been modified, will potentially trigger a PID reconfigure
Bool ps_modified;
//stats
u32 nb_idr, nb_i, nb_p, nb_b, nb_sp, nb_si, nb_sei, nb_nalus, nb_aud, nb_cra;
//frame has intra slice
Bool has_islice;
//AU is rap
GF_FilterSAPType au_sap;
//frame first slice
Bool first_slice_in_au;
//frame first slice
Bool au_sap2_poc_reset;
//paff used - NEED FURTHER CHECKING
Bool is_paff;
Bool bottom_field_flag;
//SEI recovery count - if 0 and I slice only frame, openGOP detection (avc)
s32 sei_recovery_frame_count;
u32 use_opengop_gdr;
//poc compute variables
s32 last_poc, max_last_poc, max_last_b_poc, poc_diff, prev_last_poc, min_poc, poc_shift;
//set to TRUE once 3 frames with same min poc diff are found, enabling dispatch of the frames
Bool poc_probe_done;
//pointer to the first packet of the current frame (the one holding timing info)
//this packet is in the packet queue
GF_FilterPacket *first_pck_in_au;
//frame has slices used as reference
Bool has_ref_slices;
//frame has redundant coding
Bool has_redundant;
Bool last_frame_is_idr;
//buffer to store SEI messages
//for AVC: we have to rewrite the SEI to remove some of the messages according to the spec
//for HEVC: we store prefix SEI here and dispatch them once the first VCL is found
char *sei_buffer;
u32 sei_buffer_size, sei_buffer_alloc;
//subsample buffer, only used for SVC for now
u32 subsamp_buffer_alloc, subsamp_buffer_size, subs_mapped_bytes;
char *subsamp_buffer;
//AVC specific
//avc bitstream state
AVCState *avc_state;
//SVC specific
char *svc_prefix_buffer;
u32 svc_prefix_buffer_size, svc_prefix_buffer_alloc;
u32 svc_nalu_prefix_reserved;
u8 svc_nalu_prefix_priority;
//HEVC specific
HEVCState *hevc_state;
//shvc stats
u32 nb_e_idr, nb_e_i, nb_e_p, nb_e_b;
Bool vvc_no_stats;
LHVCLayerInfo linf[64];
u8 max_temporal_id[64];
u8 min_layer_id;
//VVC specific
VVCState *vvc_state;
Bool has_initial_aud;
char init_aud[3];
Bool interlaced;
Bool is_mvc;
u32 bitrate;
u32 nb_frames;
//layer and temporal ID of last VCL nal
u8 last_layer_id, last_temporal_id;
u32 clli_crc, mdcv_crc;
u32 nb_dv_rpu, nb_dv_el;
} GF_NALUDmxCtx;
static void naludmx_enqueue_or_dispatch(GF_NALUDmxCtx *ctx, GF_FilterPacket *n_pck, Bool flush_ref);
static void naludmx_finalize_au_flags(GF_NALUDmxCtx *ctx);
static void naludmx_reset_param_sets(GF_NALUDmxCtx *ctx, Bool do_free);
static void naludmx_set_dolby_vision(GF_NALUDmxCtx *ctx);
GF_Err naludmx_configure_pid(GF_Filter *filter, GF_FilterPid *pid, Bool is_remove)
{
const GF_PropertyValue *p;
u32 old_codecid;
GF_NALUDmxCtx *ctx = gf_filter_get_udta(filter);
if (is_remove) {
ctx->ipid = NULL;
if (ctx->opid) {
gf_filter_pid_remove(ctx->opid);
ctx->opid = NULL;
}
return GF_OK;
}
if (! gf_filter_pid_check_caps(pid))
return GF_NOT_SUPPORTED;
ctx->ipid = pid;
p = gf_filter_pid_get_property(pid, GF_PROP_PID_TIMESCALE);
if (p) {
ctx->timescale = p->value.uint;
//if we have a FPS prop, use it
p = gf_filter_pid_get_property(pid, GF_PROP_PID_FPS);
if (p) {
ctx->cur_fps = p->value.frac;
} else {
ctx->cur_fps.den = 0;
ctx->cur_fps.num = ctx->timescale;
}
}
old_codecid = ctx->codecid;
p = gf_filter_pid_get_property(pid, GF_PROP_PID_CODECID);
if (p) {
switch (p->value.uint) {
case GF_CODECID_HEVC:
case GF_CODECID_LHVC:
ctx->codecid = GF_CODECID_HEVC;
break;
case GF_CODECID_VVC:
ctx->codecid = GF_CODECID_VVC;
break;
case GF_CODECID_AVC:
case GF_CODECID_AVC_PS:
case GF_CODECID_SVC:
case GF_CODECID_MVC:
ctx->codecid = GF_CODECID_AVC;
break;
default:
return GF_NOT_SUPPORTED;
}
}
else {
p = gf_filter_pid_get_property(pid, GF_PROP_PID_MIME);
if (p && p->value.string && (
strstr(p->value.string, "hvc")
|| strstr(p->value.string, "hevc")
|| strstr(p->value.string, "265")
|| strstr(p->value.string, "shvc")
|| strstr(p->value.string, "mhvc")
|| strstr(p->value.string, "lhvc")
) )
ctx->codecid = GF_CODECID_HEVC;
else if (p && p->value.string && (
strstr(p->value.string, "vvc")
) )
ctx->codecid = GF_CODECID_VVC;
else {
p = gf_filter_pid_get_property(pid, GF_PROP_PID_FILE_EXT);
if (p && p->value.string && (
strstr(p->value.string, "hvc")
|| strstr(p->value.string, "hevc")
|| strstr(p->value.string, "265")
|| strstr(p->value.string, "shvc")
|| strstr(p->value.string, "mhvc")
|| strstr(p->value.string, "lhvc")
) )
ctx->codecid = GF_CODECID_HEVC;
else if (p && p->value.string && (
strstr(p->value.string, "vvc")
|| strstr(p->value.string, "266")
|| strstr(p->value.string, "lvvc")
) )
ctx->codecid = GF_CODECID_VVC;
else
ctx->codecid = GF_CODECID_AVC;
}
}
if (old_codecid && (old_codecid != ctx->codecid)) {
naludmx_reset_param_sets(ctx, GF_FALSE);
}
if (ctx->codecid==GF_CODECID_HEVC) {
#ifdef GPAC_DISABLE_HEVC
return GF_NOT_SUPPORTED;
#else
ctx->log_name = "HEVC";
if (ctx->avc_state) { gf_free(ctx->avc_state); ctx->avc_state = NULL; }
if (ctx->vvc_state) { gf_free(ctx->vvc_state); ctx->vvc_state = NULL; }
if (!ctx->hevc_state) GF_SAFEALLOC(ctx->hevc_state, HEVCState);
ctx->min_layer_id = 0xFF;
#endif
} else if (ctx->codecid==GF_CODECID_VVC) {
ctx->log_name = "VVC";
if (ctx->hevc_state) { gf_free(ctx->hevc_state); ctx->hevc_state = NULL; }
if (ctx->avc_state) { gf_free(ctx->avc_state); ctx->avc_state = NULL; }
if (!ctx->vvc_state) GF_SAFEALLOC(ctx->vvc_state, VVCState);
} else {
ctx->log_name = "AVC|H264";
if (ctx->hevc_state) { gf_free(ctx->hevc_state); ctx->hevc_state = NULL; }
if (ctx->vvc_state) { gf_free(ctx->vvc_state); ctx->vvc_state = NULL; }
if (!ctx->avc_state) GF_SAFEALLOC(ctx->avc_state, AVCState);
}
if (ctx->timescale && !ctx->opid) {
ctx->opid = gf_filter_pid_new(filter);
ctx->first_slice_in_au = GF_TRUE;
}
ctx->full_au_source = GF_FALSE;
p = gf_filter_pid_get_property(pid, GF_PROP_PID_UNFRAMED_FULL_AU);
if (p && p->value.boolean) {
GF_FilterEvent fevt;
//this is a reframer used after an encoder, we want to make sure we have enough frames to compute POC otherwise we might block the chain
//by holding input packets - ask 1s by default
GF_FEVT_INIT(fevt, GF_FEVT_BUFFER_REQ, ctx->ipid);
fevt.buffer_req.pid_only = GF_TRUE;
fevt.buffer_req.max_buffer_us = 1000000;
gf_filter_pid_send_event(ctx->ipid, &fevt);
ctx->full_au_source = GF_TRUE;
}
//if source has no timescale, recompute time
if (!ctx->timescale) ctx->notime = GF_TRUE;
//copy properties at init or reconfig
if (ctx->opid) {
if (ctx->poc_probe_done) {
//full frame mode, flush everything before signaling discontinuity
//for other modes discontinuity we signal discontinuity before the current AU being reconstructed
if (ctx->full_au_source && ctx->first_pck_in_au)
naludmx_finalize_au_flags(ctx);
naludmx_enqueue_or_dispatch(ctx, NULL, GF_TRUE);
}
ctx->nal_store_size = 0;
gf_filter_pid_copy_properties(ctx->opid, ctx->ipid);
//don't change codec type if reframing an ES (for HLS SAES)
if (!ctx->timescale)
gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_STREAM_TYPE, & PROP_UINT(GF_STREAM_VISUAL));
gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_CODECID, & PROP_UINT(ctx->codecid));
gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_UNFRAMED, NULL);
gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_UNFRAMED_FULL_AU, NULL);
if (!gf_filter_pid_get_property(ctx->ipid, GF_PROP_PID_ID))
gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_ID, &PROP_UINT(1));
ctx->ps_modified = GF_TRUE;
ctx->crc_cfg = ctx->crc_cfg_enh = 0;
}
return GF_OK;
}
static void naludmx_check_dur(GF_Filter *filter, GF_NALUDmxCtx *ctx)
{
FILE *stream;
GF_BitStream *bs;
u64 duration, cur_dur, nal_start, start_code_pos, rate;
AVCState *avc_state = NULL;
HEVCState *hevc_state = NULL;
VVCState *vvc_state = NULL;
Bool first_slice_in_pic = GF_TRUE;
const GF_PropertyValue *p;
const char *filepath = NULL;
if (!ctx->opid || ctx->timescale || ctx->file_loaded) return;
p = gf_filter_pid_get_property(ctx->ipid, GF_PROP_PID_FILEPATH);
if (!p || !p->value.string || !strncmp(p->value.string, "gmem://", 7)) {
ctx->is_file = GF_FALSE;
ctx->file_loaded = GF_TRUE;
return;
}
filepath = p->value.string;
ctx->is_file = GF_TRUE;
if (ctx->index<0) {
if (gf_opts_get_bool("temp", "force_indexing")) {
ctx->index = 1.0;
} else {
p = gf_filter_pid_get_property(ctx->ipid, GF_PROP_PID_DOWN_SIZE);
if (!p || (p->value.longuint > 20000000)) {
GF_LOG(GF_LOG_INFO, GF_LOG_MEDIA, ("[%s] Source file larger than 20M, skipping indexing\n", ctx->log_name));
} else {
ctx->index = -ctx->index;
}
}
}
if (ctx->index<=0) {
ctx->duration.num = 1;
ctx->file_loaded = GF_TRUE;
return;
}
if (ctx->codecid==GF_CODECID_HEVC) {
GF_SAFEALLOC(hevc_state, HEVCState);
if (!hevc_state) return;
} else if (ctx->codecid==GF_CODECID_VVC) {
GF_SAFEALLOC(vvc_state, VVCState);
if (!vvc_state) return;
} else {
GF_SAFEALLOC(avc_state, AVCState);
if (!avc_state) return;
}
stream = gf_fopen_ex(filepath, NULL, "rb", GF_TRUE);
if (!stream) {
if (hevc_state) gf_free(hevc_state);
if (vvc_state) gf_free(vvc_state);
if (avc_state) gf_free(avc_state);
if (gf_fileio_is_main_thread(filepath)) {
ctx->duration.num = 1;
ctx->file_loaded = GF_TRUE;
}
return;
}
ctx->index_size = 0;
duration = 0;
cur_dur = 0;
bs = gf_bs_from_file(stream, GF_BITSTREAM_READ);
gf_bs_enable_emulation_byte_removal(bs, GF_TRUE);
start_code_pos = gf_bs_get_position(bs);
if (!gf_media_nalu_is_start_code(bs)) {
if (hevc_state) gf_free(hevc_state);
if (avc_state) gf_free(avc_state);
gf_bs_del(bs);
gf_fclose(stream);
ctx->duration.num = 1;
ctx->file_loaded = GF_TRUE;
return;
}
nal_start = gf_bs_get_position(bs);
while (gf_bs_available(bs)) {
u32 nal_size;
s32 res;
Bool is_rap = GF_FALSE;
Bool is_slice = GF_FALSE;
nal_size = gf_media_nalu_next_start_code_bs(bs);
gf_bs_seek(bs, nal_start);
if (hevc_state) {
#ifndef GPAC_DISABLE_HEVC
u8 temporal_id, layer_id, nal_type;
res = gf_hevc_parse_nalu_bs(bs, hevc_state, &nal_type, &temporal_id, &layer_id);
if (res>0) first_slice_in_pic = GF_TRUE;
switch (nal_type) {
case GF_HEVC_NALU_SLICE_IDR_N_LP:
case GF_HEVC_NALU_SLICE_IDR_W_DLP:
case GF_HEVC_NALU_SLICE_CRA:
case GF_HEVC_NALU_SLICE_BLA_N_LP:
case GF_HEVC_NALU_SLICE_BLA_W_LP:
case GF_HEVC_NALU_SLICE_BLA_W_DLP:
is_rap = GF_TRUE;
is_slice = GF_TRUE;
break;
case GF_HEVC_NALU_SLICE_STSA_N:
case GF_HEVC_NALU_SLICE_STSA_R:
case GF_HEVC_NALU_SLICE_RADL_R:
case GF_HEVC_NALU_SLICE_RASL_R:
case GF_HEVC_NALU_SLICE_RADL_N:
case GF_HEVC_NALU_SLICE_RASL_N:
case GF_HEVC_NALU_SLICE_TRAIL_N:
case GF_HEVC_NALU_SLICE_TRAIL_R:
case GF_HEVC_NALU_SLICE_TSA_N:
case GF_HEVC_NALU_SLICE_TSA_R:
is_slice = GF_TRUE;
break;
}
#endif // GPAC_DISABLE_HEVC
} else if (vvc_state) {
} else {
u32 nal_type;
u64 pos = gf_bs_get_position(bs);
res = gf_avc_parse_nalu(bs, avc_state);
if (res>0) first_slice_in_pic = GF_TRUE;
nal_type = avc_state->last_nal_type_parsed;
switch (nal_type) {
case GF_AVC_NALU_SEQ_PARAM:
gf_bs_seek(bs, pos);
gf_avc_read_sps_bs(bs, avc_state, GF_FALSE, NULL);
break;
case GF_AVC_NALU_PIC_PARAM:
gf_bs_seek(bs, pos);
gf_avc_read_pps_bs(bs, avc_state);
break;
case GF_AVC_NALU_IDR_SLICE:
is_rap = GF_TRUE;
is_slice = GF_TRUE;
break;
case GF_AVC_NALU_NON_IDR_SLICE:
case GF_AVC_NALU_DP_A_SLICE:
case GF_AVC_NALU_DP_B_SLICE:
case GF_AVC_NALU_DP_C_SLICE:
is_slice = GF_TRUE;
break;
}
}
if (is_rap && first_slice_in_pic && (cur_dur >= ctx->index * ctx->cur_fps.num) ) {
if (!ctx->index_alloc_size) ctx->index_alloc_size = 10;
else if (ctx->index_alloc_size == ctx->index_size) ctx->index_alloc_size *= 2;
ctx->indexes = gf_realloc(ctx->indexes, sizeof(NALUIdx)*ctx->index_alloc_size);
ctx->indexes[ctx->index_size].pos = start_code_pos;
ctx->indexes[ctx->index_size].duration = (Double) duration;
ctx->indexes[ctx->index_size].duration /= ctx->cur_fps.num;
ctx->index_size ++;
cur_dur = 0;
}
if (is_slice && first_slice_in_pic) {
duration += ctx->cur_fps.den;
cur_dur += ctx->cur_fps.den;
first_slice_in_pic = GF_FALSE;
}
gf_bs_seek(bs, nal_start + nal_size);
/* nal_start = gf_media_nalu_next_start_code_bs(bs);
if (nal_start) gf_bs_skip_bytes(bs, nal_start);
*/
if (gf_bs_available(bs)<4)
break;
start_code_pos = gf_bs_get_position(bs);
nal_start = gf_media_nalu_is_start_code(bs);
if (!nal_start) {
break;
}
nal_start = gf_bs_get_position(bs);
}
rate = gf_bs_get_position(bs);
gf_bs_del(bs);
gf_fclose(stream);
if (hevc_state) gf_free(hevc_state);
if (vvc_state) gf_free(vvc_state);
if (avc_state) gf_free(avc_state);
if (!ctx->duration.num || (ctx->duration.num * ctx->cur_fps.num != duration * ctx->duration.den)) {
ctx->duration.num = (s32) duration;
ctx->duration.den = ctx->cur_fps.num;
gf_filter_pid_set_property(ctx->opid, GF_PROP_PID_DURATION, & PROP_FRAC64(ctx->duration));
if (duration && (!gf_sys_is_test_mode() || gf_opts_get_bool("temp", "force_indexing"))) {
rate *= 8 * ctx->duration.den;
rate /= ctx->duration.num;
ctx->bitrate = (u32) rate;
}
}
p = gf_filter_pid_get_property(ctx->ipid, GF_PROP_PID_FILE_CACHED);
if (p && p->value.boolean) ctx->file_loaded = GF_TRUE;
}
static void naludmx_enqueue_or_dispatch(GF_NALUDmxCtx *ctx, GF_FilterPacket *n_pck, Bool flush_ref)
{
//TODO: we are dispatching frames in "negctts mode", ie we may have DTS>CTS
//need to signal this for consumers using DTS (eg MPEG-2 TS)
if (flush_ref && ctx->pck_queue && ctx->poc_diff) {
u32 dts_inc=0;
s32 last_poc = 0;
Bool patch_missing_frame = GF_FALSE;
//send all reference packet queued
if (ctx->strict_poc==STRICT_POC_ERROR) {
u32 i;
u32 nb_bframes = 0;
for (i=0; i<gf_list_count(ctx->pck_queue); i++) {
s32 poc;
u64 poc_ts, dts;
GF_FilterPacket *q_pck = gf_list_get(ctx->pck_queue, i);
if (q_pck == ctx->first_pck_in_au) break;
dts = gf_filter_pck_get_dts(q_pck);
if (dts == GF_FILTER_NO_TS) continue;
poc_ts = gf_filter_pck_get_cts(q_pck);
assert(poc_ts != GF_FILTER_NO_TS);
poc = (s32) ((s64) poc_ts - CTS_POC_OFFSET_SAFETY);
if (i) {
if (last_poc>poc) nb_bframes ++;
else if (last_poc + ctx->poc_diff<poc)
patch_missing_frame = GF_TRUE;
}
last_poc = poc;
}
if (nb_bframes>1)
patch_missing_frame = GF_FALSE;
else if (nb_bframes)
patch_missing_frame = GF_TRUE;
}
last_poc = GF_INT_MIN;
while (gf_list_count(ctx->pck_queue) ) {
u64 dts;
GF_FilterPacket *q_pck = gf_list_get(ctx->pck_queue, 0);
if (q_pck == ctx->first_pck_in_au) break;
dts = gf_filter_pck_get_dts(q_pck);
if (dts != GF_FILTER_NO_TS) {
s32 poc;
u64 poc_ts, cts;
u8 carousel_info = gf_filter_pck_get_carousel_version(q_pck);
//we reused timing from source packets
if (!carousel_info) {
assert(ctx->timescale);
gf_list_rem(ctx->pck_queue, 0);
gf_filter_pck_send(q_pck);
continue;
}
gf_filter_pck_set_carousel_version(q_pck, 0);
poc_ts = gf_filter_pck_get_cts(q_pck);
assert(poc_ts != GF_FILTER_NO_TS);
poc = (s32) ((s64) poc_ts - CTS_POC_OFFSET_SAFETY);
if (patch_missing_frame) {
if (last_poc!=GF_INT_MIN) {
//check if we missed an IDR (poc reset)
if (poc && (last_poc > poc) ) {
last_poc = 0;
dts_inc += ctx->cur_fps.den;
ctx->dts_last_IDR = dts;
ctx->dts += ctx->cur_fps.den;
}
//check if we miss a frame
while (last_poc + ctx->poc_diff < poc) {
last_poc += ctx->poc_diff;
dts_inc += ctx->cur_fps.den;
ctx->dts += ctx->cur_fps.den;
}
}
last_poc = poc;
dts += dts_inc;
}
//poc is stored as diff since last IDR which has min_poc
cts = ( (ctx->min_poc + (s32) poc) * ctx->cur_fps.den ) / ctx->poc_diff + ctx->dts_last_IDR;
/*if PAFF, 2 pictures (eg poc) <=> 1 aggregated frame (eg sample), divide by 2*/
if (ctx->is_paff) {
cts /= 2;
/*in some cases the poc is not on the top field - if that is the case, round up*/
if (cts % ctx->cur_fps.den) {
cts = ((cts/ctx->cur_fps.den)+1) * ctx->cur_fps.den;
}
}
gf_filter_pck_set_cts(q_pck, cts);
GF_LOG(GF_LOG_DEBUG, GF_LOG_MEDIA, ("[%s] Frame timestamps computed dts "LLU" cts "LLU" (poc %d min poc %d poc_diff %d last IDR DTS "LLU")\n", ctx->log_name, dts, cts, poc, ctx->min_poc, ctx->poc_diff, ctx->dts_last_IDR));
if (ctx->importer && ctx->cur_fps.den) {
poc = (s32) ( (s64) cts - (s64) dts);
if (poc<0) poc = -poc;
poc /= ctx->cur_fps.den;
if (poc > ctx->max_total_delay)
ctx->max_total_delay = poc;
}
}
gf_list_rem(ctx->pck_queue, 0);
gf_filter_pck_send(q_pck);
}
}
if (!n_pck) return;
if (!ctx->pck_queue) ctx->pck_queue = gf_list_new();
gf_list_add(ctx->pck_queue, n_pck);
}
static void naludmx_add_param_nalu(GF_List *param_list, GF_NALUFFParam *sl, u8 nal_type)
{
GF_NALUFFParamArray *pa = NULL;
u32 i, count;
count = gf_list_count(param_list);
for (i=0; i<count; i++) {
pa = gf_list_get(param_list, i);
if (pa->type == nal_type) break;
pa = NULL;
}
if (!pa) {
GF_SAFEALLOC(pa, GF_NALUFFParamArray);
if (!pa) return;
pa->array_completeness = 1;
pa->type = nal_type;
pa->nalus = gf_list_new();
gf_list_add(param_list, pa);
}
gf_list_add(pa->nalus, sl);
}
#ifndef GPAC_DISABLE_HEVC
static void naludmx_hevc_set_parall_type(GF_NALUDmxCtx *ctx, GF_HEVCConfig *hevc_cfg)
{
u32 use_tiles, use_wpp, nb_pps, i, count;
HEVCState hevc;
count = gf_list_count(ctx->pps);
memset(&hevc, 0, sizeof(HEVCState));
hevc.sps_active_idx = -1;
use_tiles = 0;
use_wpp = 0;
nb_pps = 0;
for (i=0; i<count; i++) {
GF_NALUFFParam *slc = (GF_NALUFFParam*)gf_list_get(ctx->pps, i);
s32 idx = gf_hevc_read_pps(slc->data, slc->size, &hevc);
if (idx>=0) {
HEVC_PPS *pps;
nb_pps++;
pps = &hevc.pps[idx];
if (!pps->entropy_coding_sync_enabled_flag && pps->tiles_enabled_flag)
use_tiles++;
else if (pps->entropy_coding_sync_enabled_flag && !pps->tiles_enabled_flag)
use_wpp++;
}
}
if (!use_tiles && !use_wpp) hevc_cfg->parallelismType = 1;
else if (!use_wpp && (use_tiles==nb_pps) ) hevc_cfg->parallelismType = 2;
else if (!use_tiles && (use_wpp==nb_pps) ) hevc_cfg->parallelismType = 3;
else hevc_cfg->parallelismType = 0;
}
#endif // GPAC_DISABLE_HEVC
GF_Err naludmx_set_hevc_oinf(GF_NALUDmxCtx *ctx, u8 *max_temporal_id)
{
GF_OperatingPointsInformation *oinf;
GF_BitStream *bs;
u8 *data;
u32 data_size;
u32 i;
HEVC_VPS *vps;
GF_NALUFFParam *vps_sl = gf_list_get(ctx->vps, 0);
if (!vps_sl) return GF_SERVICE_ERROR;
vps = &ctx->hevc_state->vps[vps_sl->id];
if (!vps->vps_extension_found) return GF_OK;
if (vps->max_layers<2) return GF_OK;
oinf = gf_isom_oinf_new_entry();
if (!oinf) return GF_OUT_OF_MEM;
oinf->scalability_mask = 0;
for (i = 0; i < 16; i++) {
if (vps->scalability_mask[i])
oinf->scalability_mask |= 1 << i;
}
for (i = 0; i < vps->num_profile_tier_level; i++) {
HEVC_ProfileTierLevel ptl = (i == 0) ? vps->ptl : vps->ext_ptl[i-1];
LHEVC_ProfileTierLevel *lhevc_ptl;
GF_SAFEALLOC(lhevc_ptl, LHEVC_ProfileTierLevel);
if (!lhevc_ptl) return GF_OUT_OF_MEM;
lhevc_ptl->general_profile_space = ptl.profile_space;
lhevc_ptl->general_tier_flag = ptl.tier_flag;
lhevc_ptl->general_profile_idc = ptl.profile_idc;
lhevc_ptl->general_profile_compatibility_flags = ptl.profile_compatibility_flag;
lhevc_ptl->general_constraint_indicator_flags = 0;
if (ptl.general_progressive_source_flag)
lhevc_ptl->general_constraint_indicator_flags |= ((u64)1) << 47;
if (ptl.general_interlaced_source_flag)
lhevc_ptl->general_constraint_indicator_flags |= ((u64)1) << 46;
if (ptl.general_non_packed_constraint_flag)
lhevc_ptl->general_constraint_indicator_flags |= ((u64)1) << 45;
if (ptl.general_frame_only_constraint_flag)
lhevc_ptl->general_constraint_indicator_flags |= ((u64)1) << 44;
lhevc_ptl->general_constraint_indicator_flags |= ptl.general_reserved_44bits;
lhevc_ptl->general_level_idc = ptl.level_idc;
gf_list_add(oinf->profile_tier_levels, lhevc_ptl);
}
for (i = 0; i < vps->num_output_layer_sets; i++) {
LHEVC_OperatingPoint *op;
u32 j;
u16 minPicWidth, minPicHeight, maxPicWidth, maxPicHeight;
u8 maxChromaFormat, maxBitDepth;
u8 maxTemporalId;
GF_SAFEALLOC(op, LHEVC_OperatingPoint);
if (!op) return GF_OUT_OF_MEM;
op->output_layer_set_idx = i;
op->layer_count = vps->num_necessary_layers[i];
minPicWidth = minPicHeight = maxPicWidth = maxPicHeight = maxTemporalId = 0;
maxChromaFormat = maxBitDepth = 0;
for (j = 0; j < op->layer_count; j++) {
u32 format_idx;
u32 bitDepth;
op->layers_info[j].ptl_idx = vps->profile_tier_level_idx[i][j];
op->layers_info[j].layer_id = j;
op->layers_info[j].is_outputlayer = vps->output_layer_flag[i][j];
//FIXME: we consider that this flag is never set
op->layers_info[j].is_alternate_outputlayer = GF_FALSE;
if (max_temporal_id) {
if (!maxTemporalId || (maxTemporalId < max_temporal_id[op->layers_info[j].layer_id]))
maxTemporalId = max_temporal_id[op->layers_info[j].layer_id];
} else {
maxTemporalId = vps->max_sub_layers;
}
format_idx = vps->rep_format_idx[op->layers_info[j].layer_id];
if (!minPicWidth || (minPicWidth > vps->rep_formats[format_idx].pic_width_luma_samples))
minPicWidth = vps->rep_formats[format_idx].pic_width_luma_samples;
if (!minPicHeight || (minPicHeight > vps->rep_formats[format_idx].pic_height_luma_samples))
minPicHeight = vps->rep_formats[format_idx].pic_height_luma_samples;
if (!maxPicWidth || (maxPicWidth < vps->rep_formats[format_idx].pic_width_luma_samples))
maxPicWidth = vps->rep_formats[format_idx].pic_width_luma_samples;
if (!maxPicHeight || (maxPicHeight < vps->rep_formats[format_idx].pic_height_luma_samples))
maxPicHeight = vps->rep_formats[format_idx].pic_height_luma_samples;
if (!maxChromaFormat || (maxChromaFormat < vps->rep_formats[format_idx].chroma_format_idc))
maxChromaFormat = vps->rep_formats[format_idx].chroma_format_idc;
bitDepth = vps->rep_formats[format_idx].bit_depth_chroma > vps->rep_formats[format_idx].bit_depth_luma ? vps->rep_formats[format_idx].bit_depth_chroma : vps->rep_formats[format_idx].bit_depth_luma;
if (!maxChromaFormat || (maxChromaFormat < bitDepth))
maxChromaFormat = bitDepth;
}
op->max_temporal_id = maxTemporalId;
op->minPicWidth = minPicWidth;
op->minPicHeight = minPicHeight;
op->maxPicWidth = maxPicWidth;
op->maxPicHeight = maxPicHeight;
op->maxChromaFormat = maxChromaFormat;
op->maxBitDepth = maxBitDepth;
op->frame_rate_info_flag = GF_FALSE; //FIXME: should fetch this info from VUI
op->bit_rate_info_flag = GF_FALSE; //we don't use it
gf_list_add(oinf->operating_points, op);
}
for (i = 0; i < vps->max_layers; i++) {
LHEVC_DependentLayer *dep;
u32 j, k;
GF_SAFEALLOC(dep, LHEVC_DependentLayer);
if (!dep) return GF_OUT_OF_MEM;
dep->dependent_layerID = vps->layer_id_in_nuh[i];
for (j = 0; j < vps->max_layers; j++) {
if (vps->direct_dependency_flag[dep->dependent_layerID][j]) {
dep->dependent_on_layerID[dep->num_layers_dependent_on] = j;
dep->num_layers_dependent_on ++;
}
}
k = 0;
for (j = 0; j < 16; j++) {
if (oinf->scalability_mask & (1 << j)) {
dep->dimension_identifier[j] = vps->dimension_id[i][k];
k++;
}
}
gf_list_add(oinf->dependency_layers, dep);
}
//write Operating Points Information Sample Group
bs = gf_bs_new(NULL, 0, GF_BITSTREAM_WRITE);
gf_isom_oinf_write_entry(oinf, bs);
gf_bs_get_content(bs, &data, &data_size);
gf_bs_del(bs);
gf_isom_oinf_del_entry(oinf);
gf_filter_pid_set_info_str(ctx->opid, "hevc:oinf", &PROP_DATA_NO_COPY(data, data_size) );
return GF_OK;
}
static void naludmx_set_hevc_linf(GF_NALUDmxCtx *ctx)
{
u32 i, nb_layers=0, nb_sublayers=0;
u8 *data;
u32 data_size;
GF_BitStream *bs;
for (i=0; i<64; i++) {
if (ctx->linf[i].layer_id_plus_one) nb_layers++;
if (ctx->linf[i].min_temporal_id != ctx->linf[i].max_temporal_id) nb_sublayers++;
}
if (!nb_layers && !nb_sublayers)
return;
bs = gf_bs_new(NULL, 0, GF_BITSTREAM_WRITE);
gf_bs_write_int(bs, 0, 2);
gf_bs_write_int(bs, nb_layers, 6);
for (i=0; i<nb_layers; i++) {
if (! ctx->linf[i].layer_id_plus_one) continue;
gf_bs_write_int(bs, 0, 4);
gf_bs_write_int(bs, ctx->linf[i].layer_id_plus_one - 1, 6);
gf_bs_write_int(bs, ctx->linf[i].min_temporal_id, 3);
gf_bs_write_int(bs, ctx->linf[i].max_temporal_id, 3);
gf_bs_write_int(bs, 0, 1);
gf_bs_write_int(bs, 0xFF, 7);
}
gf_bs_get_content(bs, &data, &data_size);
gf_bs_del(bs);
gf_filter_pid_set_info_str(ctx->opid, "hevc:linf", &PROP_DATA_NO_COPY(data, data_size) );
}
static void naludmx_create_hevc_decoder_config(GF_NALUDmxCtx *ctx, u8 **dsi, u32 *dsi_size, u8 **dsi_enh, u32 *dsi_enh_size, u32 *max_width, u32 *max_height, u32 *max_enh_width, u32 *max_enh_height, GF_Fraction *sar, Bool *has_hevc_base)
{
#ifndef GPAC_DISABLE_HEVC
u32 i, count;
u8 layer_id;
Bool first = GF_TRUE;
Bool first_lhvc = GF_TRUE;
GF_HEVCConfig *cfg;
GF_HEVCConfig *hvcc;
GF_HEVCConfig *lvcc;
u32 max_w, max_h, max_ew, max_eh;
*has_hevc_base = GF_FALSE;
max_w = max_h = 0;
max_ew = max_eh = 0;
sar->num = sar->den = 0;
hvcc = gf_odf_hevc_cfg_new();
lvcc = gf_odf_hevc_cfg_new();
hvcc->nal_unit_size = ctx->nal_length;
lvcc->nal_unit_size = ctx->nal_length;
lvcc->is_lhvc = GF_TRUE;
//check we have one pps or sps in base layer
count = gf_list_count(ctx->sps);
for (i=0; i<count; i++) {
GF_NALUFFParam *sl = gf_list_get(ctx->sps, i);
layer_id = ((sl->data[0] & 0x1) << 5) | (sl->data[1] >> 3);
if (!layer_id) {
*has_hevc_base = GF_TRUE;