-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
/
srs_kernel_codec.cpp
2865 lines (2438 loc) · 106 KB
/
srs_kernel_codec.cpp
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
//
// Copyright (c) 2013-2023 The SRS Authors
//
// SPDX-License-Identifier: MIT or MulanPSL-2.0
//
#include <srs_kernel_codec.hpp>
#include <string.h>
#include <stdlib.h>
using namespace std;
#include <srs_kernel_error.hpp>
#include <srs_kernel_log.hpp>
#include <srs_kernel_buffer.hpp>
#include <srs_kernel_utility.hpp>
#include <srs_core_autofree.hpp>
#include <srs_kernel_rtc_rtp.hpp>
string srs_video_codec_id2str(SrsVideoCodecId codec)
{
switch (codec) {
case SrsVideoCodecIdAVC:
return "H264";
case SrsVideoCodecIdOn2VP6:
case SrsVideoCodecIdOn2VP6WithAlphaChannel:
return "VP6";
case SrsVideoCodecIdHEVC:
return "HEVC";
case SrsVideoCodecIdAV1:
return "AV1";
case SrsVideoCodecIdReserved:
case SrsVideoCodecIdReserved1:
case SrsVideoCodecIdReserved2:
case SrsVideoCodecIdDisabled:
case SrsVideoCodecIdSorensonH263:
case SrsVideoCodecIdScreenVideo:
case SrsVideoCodecIdScreenVideoVersion2:
default:
return "Other";
}
}
string srs_audio_codec_id2str(SrsAudioCodecId codec)
{
switch (codec) {
case SrsAudioCodecIdAAC:
return "AAC";
case SrsAudioCodecIdMP3:
return "MP3";
case SrsAudioCodecIdOpus:
return "Opus";
case SrsAudioCodecIdReserved1:
case SrsAudioCodecIdLinearPCMPlatformEndian:
case SrsAudioCodecIdADPCM:
case SrsAudioCodecIdLinearPCMLittleEndian:
case SrsAudioCodecIdNellymoser16kHzMono:
case SrsAudioCodecIdNellymoser8kHzMono:
case SrsAudioCodecIdNellymoser:
case SrsAudioCodecIdReservedG711AlawLogarithmicPCM:
case SrsAudioCodecIdReservedG711MuLawLogarithmicPCM:
case SrsAudioCodecIdReserved:
case SrsAudioCodecIdSpeex:
case SrsAudioCodecIdReservedMP3_8kHz:
case SrsAudioCodecIdReservedDeviceSpecificSound:
default:
return "Other";
}
}
SrsAudioSampleRate srs_audio_sample_rate_from_number(uint32_t v)
{
if (v == 5512) return SrsAudioSampleRate5512;
if (v == 11025) return SrsAudioSampleRate11025;
if (v == 22050) return SrsAudioSampleRate22050;
if (v == 44100) return SrsAudioSampleRate44100;
if (v == 12000) return SrsAudioSampleRate12000;
if (v == 24000) return SrsAudioSampleRate24000;
if (v == 48000) return SrsAudioSampleRate48000;
if (v == 8000) return SrsAudioSampleRateNB8kHz;
if (v == 12000) return SrsAudioSampleRateMB12kHz;
if (v == 16000) return SrsAudioSampleRateWB16kHz;
if (v == 24000) return SrsAudioSampleRateSWB24kHz;
if (v == 48000) return SrsAudioSampleRateFB48kHz;
return SrsAudioSampleRateForbidden;
}
SrsAudioSampleRate srs_audio_sample_rate_guess_number(uint32_t v)
{
if (v >= 48000) return SrsAudioSampleRate48000;
if (v >= 44100) return SrsAudioSampleRate44100;
if (v >= 24000) return SrsAudioSampleRate24000;
if (v >= 24000) return SrsAudioSampleRate24000;
if (v >= 22050) return SrsAudioSampleRate22050;
if (v >= 16000) return SrsAudioSampleRateWB16kHz;
if (v >= 12000) return SrsAudioSampleRate12000;
if (v >= 8000) return SrsAudioSampleRateNB8kHz;
if (v >= 5512) return SrsAudioSampleRate5512;
return SrsAudioSampleRateForbidden;
}
uint32_t srs_audio_sample_rate2number(SrsAudioSampleRate v)
{
if (v == SrsAudioSampleRate5512) return 5512;
if (v == SrsAudioSampleRate11025) return 11025;
if (v == SrsAudioSampleRate22050) return 22050;
if (v == SrsAudioSampleRate44100) return 44100;
if (v == SrsAudioSampleRate12000) return 12000;
if (v == SrsAudioSampleRate24000) return 24000;
if (v == SrsAudioSampleRate48000) return 48000;
if (v == SrsAudioSampleRateNB8kHz) return 8000;
if (v == SrsAudioSampleRateMB12kHz) return 12000;
if (v == SrsAudioSampleRateWB16kHz) return 16000;
if (v == SrsAudioSampleRateSWB24kHz) return 24000;
if (v == SrsAudioSampleRateFB48kHz) return 48000;
return 0;
}
string srs_audio_sample_rate2str(SrsAudioSampleRate v)
{
switch (v) {
case SrsAudioSampleRate5512: return "5512";
case SrsAudioSampleRate11025: return "11025";
case SrsAudioSampleRate22050: return "22050";
case SrsAudioSampleRate44100: return "44100";
case SrsAudioSampleRateNB8kHz: return "NB8kHz";
case SrsAudioSampleRateMB12kHz: return "MB12kHz";
case SrsAudioSampleRateWB16kHz: return "WB16kHz";
case SrsAudioSampleRateSWB24kHz: return "SWB24kHz";
case SrsAudioSampleRateFB48kHz: return "FB48kHz";
default: return "Other";
}
}
SrsFlvVideo::SrsFlvVideo()
{
}
SrsFlvVideo::~SrsFlvVideo()
{
}
bool SrsFlvVideo::keyframe(char* data, int size)
{
// 2bytes required.
if (size < 1) {
return false;
}
// See rtmp_specification_1.0.pdf
// See https://github.com/veovera/enhanced-rtmp
uint8_t frame_type = data[0] & 0x7f;
frame_type = (frame_type >> 4) & 0x0F;
return frame_type == SrsVideoAvcFrameTypeKeyFrame;
}
bool SrsFlvVideo::sh(char* data, int size)
{
// Check sequence header only for H.264 or H.265
bool codec_ok = h264(data, size);
#ifdef SRS_H265
codec_ok = codec_ok? true : hevc(data, size);
#endif
if (!codec_ok) return false;
// 2bytes required.
if (size < 2) {
return false;
}
uint8_t frame_type = data[0];
bool is_ext_header = frame_type & 0x80;
SrsVideoAvcFrameTrait avc_packet_type = SrsVideoAvcFrameTraitForbidden;
if (!is_ext_header) {
// See rtmp_specification_1.0.pdf
frame_type = (frame_type >> 4) & 0x0F;
avc_packet_type = (SrsVideoAvcFrameTrait)data[1];
} else {
// See https://github.com/veovera/enhanced-rtmp
avc_packet_type = (SrsVideoAvcFrameTrait)(frame_type & 0x0f);
frame_type = (frame_type >> 4) & 0x07;
}
// Note that SrsVideoHEVCFrameTraitPacketTypeSequenceStart is equal to SrsVideoAvcFrameTraitSequenceHeader
return frame_type == SrsVideoAvcFrameTypeKeyFrame
&& avc_packet_type == SrsVideoAvcFrameTraitSequenceHeader;
}
bool SrsFlvVideo::h264(char* data, int size)
{
// 1bytes required.
if (size < 1) {
return false;
}
char codec_id = data[0];
codec_id = codec_id & 0x0F;
return codec_id == SrsVideoCodecIdAVC;
}
#ifdef SRS_H265
bool SrsFlvVideo::hevc(char* data, int size)
{
// 1bytes required.
if (size < 1) {
return false;
}
uint8_t frame_type = data[0];
bool is_ext_header = frame_type & 0x80;
SrsVideoCodecId codec_id = SrsVideoCodecIdForbidden;
if (!is_ext_header) {
// See rtmp_specification_1.0.pdf
codec_id = (SrsVideoCodecId)(frame_type & 0x0F);
} else {
// See https://github.com/veovera/enhanced-rtmp
if (size < 5) {
return false;
}
// Video FourCC
if (data[1] != 'h' || data[2] != 'v' || data[3] != 'c' || data[4] != '1') {
return false;
}
codec_id = SrsVideoCodecIdHEVC;
}
return codec_id == SrsVideoCodecIdHEVC;
}
#endif
bool SrsFlvVideo::acceptable(char* data, int size)
{
// 1bytes required.
if (size < 1) {
return false;
}
uint8_t frame_type = data[0];
bool is_ext_header = frame_type & 0x80;
SrsVideoCodecId codec_id = SrsVideoCodecIdForbidden;
if (!is_ext_header) {
// See rtmp_specification_1.0.pdf
codec_id = (SrsVideoCodecId)(frame_type & 0x0f);
frame_type = (frame_type >> 4) & 0x0f;
if (frame_type < 1 || frame_type > 5) {
return false;
}
} else {
// See https://github.com/veovera/enhanced-rtmp
uint8_t packet_type = frame_type & 0x0f;
frame_type = (frame_type >> 4) & 0x07;
if (packet_type > SrsVideoHEVCFrameTraitPacketTypeMPEG2TSSequenceStart || frame_type > SrsVideoAvcFrameTypeVideoInfoFrame) {
return false;
}
if (size < 5) {
return false;
}
if (data[1] != 'h' || data[2] != 'v' || data[3] != 'c' || data[4] != '1') {
return false;
}
codec_id = SrsVideoCodecIdHEVC;
}
if (codec_id != SrsVideoCodecIdAVC && codec_id != SrsVideoCodecIdAV1 && codec_id != SrsVideoCodecIdHEVC) {
return false;
}
return true;
}
SrsFlvAudio::SrsFlvAudio()
{
}
SrsFlvAudio::~SrsFlvAudio()
{
}
bool SrsFlvAudio::sh(char* data, int size)
{
// sequence header only for aac
if (!aac(data, size)) {
return false;
}
// 2bytes required.
if (size < 2) {
return false;
}
char aac_packet_type = data[1];
return aac_packet_type == SrsAudioAacFrameTraitSequenceHeader;
}
bool SrsFlvAudio::aac(char* data, int size)
{
// 1bytes required.
if (size < 1) {
return false;
}
char sound_format = data[0];
sound_format = (sound_format >> 4) & 0x0F;
return sound_format == SrsAudioCodecIdAAC;
}
/**
* the public data, event HLS disable, others can use it.
*/
// 0 = 5.5 kHz = 5512 Hz
// 1 = 11 kHz = 11025 Hz
// 2 = 22 kHz = 22050 Hz
// 3 = 44 kHz = 44100 Hz
int srs_flv_srates[] = {5512, 11025, 22050, 44100, 0};
// the sample rates in the codec,
// in the sequence header.
int srs_aac_srates[] =
{
96000, 88200, 64000, 48000,
44100, 32000, 24000, 22050,
16000, 12000, 11025, 8000,
7350, 0, 0, 0
};
string srs_audio_sample_bits2str(SrsAudioSampleBits v)
{
switch (v) {
case SrsAudioSampleBits16bit: return "16bits";
case SrsAudioSampleBits8bit: return "8bits";
default: return "Other";
}
}
string srs_audio_channels2str(SrsAudioChannels v)
{
switch (v) {
case SrsAudioChannelsStereo: return "Stereo";
case SrsAudioChannelsMono: return "Mono";
default: return "Other";
}
}
string srs_avc_nalu2str(SrsAvcNaluType nalu_type)
{
switch (nalu_type) {
case SrsAvcNaluTypeNonIDR: return "NonIDR";
case SrsAvcNaluTypeDataPartitionA: return "DataPartitionA";
case SrsAvcNaluTypeDataPartitionB: return "DataPartitionB";
case SrsAvcNaluTypeDataPartitionC: return "DataPartitionC";
case SrsAvcNaluTypeIDR: return "IDR";
case SrsAvcNaluTypeSEI: return "SEI";
case SrsAvcNaluTypeSPS: return "SPS";
case SrsAvcNaluTypePPS: return "PPS";
case SrsAvcNaluTypeAccessUnitDelimiter: return "AccessUnitDelimiter";
case SrsAvcNaluTypeEOSequence: return "EOSequence";
case SrsAvcNaluTypeEOStream: return "EOStream";
case SrsAvcNaluTypeFilterData: return "FilterData";
case SrsAvcNaluTypeSPSExt: return "SPSExt";
case SrsAvcNaluTypePrefixNALU: return "PrefixNALU";
case SrsAvcNaluTypeSubsetSPS: return "SubsetSPS";
case SrsAvcNaluTypeLayerWithoutPartition: return "LayerWithoutPartition";
case SrsAvcNaluTypeCodedSliceExt: return "CodedSliceExt";
case SrsAvcNaluTypeReserved: default: return "Other";
}
}
string srs_aac_profile2str(SrsAacProfile aac_profile)
{
switch (aac_profile) {
case SrsAacProfileMain: return "Main";
case SrsAacProfileLC: return "LC";
case SrsAacProfileSSR: return "SSR";
default: return "Other";
}
}
string srs_aac_object2str(SrsAacObjectType aac_object)
{
switch (aac_object) {
case SrsAacObjectTypeAacMain: return "Main";
case SrsAacObjectTypeAacHE: return "HE";
case SrsAacObjectTypeAacHEV2: return "HEv2";
case SrsAacObjectTypeAacLC: return "LC";
case SrsAacObjectTypeAacSSR: return "SSR";
default: return "Other";
}
}
SrsAacObjectType srs_aac_ts2rtmp(SrsAacProfile profile)
{
switch (profile) {
case SrsAacProfileMain: return SrsAacObjectTypeAacMain;
case SrsAacProfileLC: return SrsAacObjectTypeAacLC;
case SrsAacProfileSSR: return SrsAacObjectTypeAacSSR;
default: return SrsAacObjectTypeReserved;
}
}
SrsAacProfile srs_aac_rtmp2ts(SrsAacObjectType object_type)
{
switch (object_type) {
case SrsAacObjectTypeAacMain: return SrsAacProfileMain;
case SrsAacObjectTypeAacHE:
case SrsAacObjectTypeAacHEV2:
case SrsAacObjectTypeAacLC: return SrsAacProfileLC;
case SrsAacObjectTypeAacSSR: return SrsAacProfileSSR;
default: return SrsAacProfileReserved;
}
}
string srs_avc_profile2str(SrsAvcProfile profile)
{
switch (profile) {
case SrsAvcProfileBaseline: return "Baseline";
case SrsAvcProfileConstrainedBaseline: return "Baseline(Constrained)";
case SrsAvcProfileMain: return "Main";
case SrsAvcProfileExtended: return "Extended";
case SrsAvcProfileHigh: return "High";
case SrsAvcProfileHigh10: return "High(10)";
case SrsAvcProfileHigh10Intra: return "High(10+Intra)";
case SrsAvcProfileHigh422: return "High(422)";
case SrsAvcProfileHigh422Intra: return "High(422+Intra)";
case SrsAvcProfileHigh444: return "High(444)";
case SrsAvcProfileHigh444Predictive: return "High(444+Predictive)";
case SrsAvcProfileHigh444Intra: return "High(444+Intra)";
default: return "Other";
}
}
string srs_avc_level2str(SrsAvcLevel level)
{
switch (level) {
case SrsAvcLevel_1: return "1";
case SrsAvcLevel_11: return "1.1";
case SrsAvcLevel_12: return "1.2";
case SrsAvcLevel_13: return "1.3";
case SrsAvcLevel_2: return "2";
case SrsAvcLevel_21: return "2.1";
case SrsAvcLevel_22: return "2.2";
case SrsAvcLevel_3: return "3";
case SrsAvcLevel_31: return "3.1";
case SrsAvcLevel_32: return "3.2";
case SrsAvcLevel_4: return "4";
case SrsAvcLevel_41: return "4.1";
case SrsAvcLevel_5: return "5";
case SrsAvcLevel_51: return "5.1";
default: return "Other";
}
}
#ifdef SRS_H265
string srs_hevc_profile2str(SrsHevcProfile profile)
{
switch (profile) {
case SrsHevcProfileMain: return "Main";
case SrsHevcProfileMain10: return "Main10";
case SrsHevcProfileMainStillPicture: return "Main Still Picture";
case SrsHevcProfileRext: return "Rext";
default: return "Other";
}
}
string srs_hevc_level2str(SrsHevcLevel level)
{
switch (level) {
case SrsHevcLevel_1: return "1";
case SrsHevcLevel_2: return "2";
case SrsHevcLevel_21: return "2.1";
case SrsHevcLevel_3: return "3";
case SrsHevcLevel_31: return "3.1";
case SrsHevcLevel_4: return "4";
case SrsHevcLevel_41: return "4.1";
case SrsHevcLevel_5: return "5";
case SrsHevcLevel_51: return "5.1";
case SrsHevcLevel_52: return "5.2";
case SrsHevcLevel_6: return "6";
case SrsHevcLevel_61: return "6.1";
case SrsHevcLevel_62: return "6.2";
default: return "Other";
}
}
#endif
SrsSample::SrsSample()
{
size = 0;
bytes = NULL;
bframe = false;
}
SrsSample::SrsSample(char* b, int s)
{
size = s;
bytes = b;
bframe = false;
}
SrsSample::~SrsSample()
{
}
srs_error_t SrsSample::parse_bframe()
{
srs_error_t err = srs_success;
uint8_t header = bytes[0];
SrsAvcNaluType nal_type = (SrsAvcNaluType)(header & kNalTypeMask);
if (nal_type != SrsAvcNaluTypeNonIDR && nal_type != SrsAvcNaluTypeDataPartitionA && nal_type != SrsAvcNaluTypeIDR) {
return err;
}
SrsBuffer* stream = new SrsBuffer(bytes, size);
SrsAutoFree(SrsBuffer, stream);
// Skip nalu header.
stream->skip(1);
SrsBitBuffer bitstream(stream);
int32_t first_mb_in_slice = 0;
if ((err = srs_avc_nalu_read_uev(&bitstream, first_mb_in_slice)) != srs_success) {
return srs_error_wrap(err, "nalu read uev");
}
int32_t slice_type_v = 0;
if ((err = srs_avc_nalu_read_uev(&bitstream, slice_type_v)) != srs_success) {
return srs_error_wrap(err, "nalu read uev");
}
SrsAvcSliceType slice_type = (SrsAvcSliceType)slice_type_v;
if (slice_type == SrsAvcSliceTypeB || slice_type == SrsAvcSliceTypeB1) {
bframe = true;
srs_verbose("nal_type=%d, slice type=%d", nal_type, slice_type);
}
return err;
}
SrsSample* SrsSample::copy()
{
SrsSample* p = new SrsSample();
p->bytes = bytes;
p->size = size;
p->bframe = bframe;
return p;
}
SrsCodecConfig::SrsCodecConfig()
{
}
SrsCodecConfig::~SrsCodecConfig()
{
}
SrsAudioCodecConfig::SrsAudioCodecConfig()
{
id = SrsAudioCodecIdForbidden;
sound_rate = SrsAudioSampleRateForbidden;
sound_size = SrsAudioSampleBitsForbidden;
sound_type = SrsAudioChannelsForbidden;
audio_data_rate = 0;
aac_object = SrsAacObjectTypeForbidden;
aac_sample_rate = SrsAacSampleRateUnset; // sample rate ignored
aac_channels = 0;
}
SrsAudioCodecConfig::~SrsAudioCodecConfig()
{
}
bool SrsAudioCodecConfig::is_aac_codec_ok()
{
return !aac_extra_data.empty();
}
SrsVideoCodecConfig::SrsVideoCodecConfig()
{
id = SrsVideoCodecIdForbidden;
video_data_rate = 0;
frame_rate = duration = 0;
width = 0;
height = 0;
NAL_unit_length = 0;
avc_profile = SrsAvcProfileReserved;
avc_level = SrsAvcLevelReserved;
payload_format = SrsAvcPayloadFormatGuess;
}
SrsVideoCodecConfig::~SrsVideoCodecConfig()
{
}
bool SrsVideoCodecConfig::is_avc_codec_ok()
{
return !avc_extra_data.empty();
}
SrsFrame::SrsFrame()
{
codec = NULL;
nb_samples = 0;
dts = 0;
cts = 0;
}
SrsFrame::~SrsFrame()
{
}
srs_error_t SrsFrame::initialize(SrsCodecConfig* c)
{
codec = c;
nb_samples = 0;
dts = 0;
cts = 0;
return srs_success;
}
srs_error_t SrsFrame::add_sample(char* bytes, int size)
{
srs_error_t err = srs_success;
// Ignore empty sample.
if (!bytes || size <= 0) return err;
if (nb_samples >= SrsMaxNbSamples) {
return srs_error_new(ERROR_HLS_DECODE_ERROR, "Frame samples overflow");
}
SrsSample* sample = &samples[nb_samples++];
sample->bytes = bytes;
sample->size = size;
sample->bframe = false;
return err;
}
SrsAudioFrame::SrsAudioFrame()
{
aac_packet_type = SrsAudioAacFrameTraitForbidden;
}
SrsAudioFrame::~SrsAudioFrame()
{
}
SrsAudioCodecConfig* SrsAudioFrame::acodec()
{
return (SrsAudioCodecConfig*)codec;
}
SrsVideoFrame::SrsVideoFrame()
{
frame_type = SrsVideoAvcFrameTypeForbidden;
avc_packet_type = SrsVideoAvcFrameTraitForbidden;
has_idr = has_aud = has_sps_pps = false;
first_nalu_type = SrsAvcNaluTypeForbidden;
}
SrsVideoFrame::~SrsVideoFrame()
{
}
srs_error_t SrsVideoFrame::initialize(SrsCodecConfig* c)
{
first_nalu_type = SrsAvcNaluTypeForbidden;
has_idr = has_sps_pps = has_aud = false;
return SrsFrame::initialize(c);
}
srs_error_t SrsVideoFrame::add_sample(char* bytes, int size)
{
srs_error_t err = srs_success;
if ((err = SrsFrame::add_sample(bytes, size)) != srs_success) {
return srs_error_wrap(err, "add frame");
}
SrsVideoCodecConfig* c = vcodec();
if (!bytes || size <= 0) return err;
// For HEVC(H.265), try to parse the IDR from NALUs.
if (c && c->id == SrsVideoCodecIdHEVC) {
#ifdef SRS_H265
SrsHevcNaluType nalu_type = SrsHevcNaluTypeParse(bytes[0]);
has_idr = (SrsHevcNaluType_CODED_SLICE_BLA <= nalu_type) && (nalu_type <= SrsHevcNaluType_RESERVED_23);
return err;
#else
return srs_error_new(ERROR_HEVC_DISABLED, "H.265 is disabled");
#endif
}
// By default, use AVC(H.264) to parse NALU.
// For video, parse the nalu type, set the IDR flag.
SrsAvcNaluType nal_unit_type = (SrsAvcNaluType)(bytes[0] & 0x1f);
if (nal_unit_type == SrsAvcNaluTypeIDR) {
has_idr = true;
} else if (nal_unit_type == SrsAvcNaluTypeSPS || nal_unit_type == SrsAvcNaluTypePPS) {
has_sps_pps = true;
} else if (nal_unit_type == SrsAvcNaluTypeAccessUnitDelimiter) {
has_aud = true;
}
if (first_nalu_type == SrsAvcNaluTypeReserved) {
first_nalu_type = nal_unit_type;
}
return err;
}
SrsVideoCodecConfig* SrsVideoFrame::vcodec()
{
return (SrsVideoCodecConfig*)codec;
}
SrsFormat::SrsFormat()
{
acodec = NULL;
vcodec = NULL;
audio = NULL;
video = NULL;
avc_parse_sps = true;
try_annexb_first = true;
raw = NULL;
nb_raw = 0;
}
SrsFormat::~SrsFormat()
{
srs_freep(audio);
srs_freep(video);
srs_freep(acodec);
srs_freep(vcodec);
}
srs_error_t SrsFormat::initialize()
{
if (!vcodec) {
vcodec = new SrsVideoCodecConfig();
}
return srs_success;
}
srs_error_t SrsFormat::on_audio(int64_t timestamp, char* data, int size)
{
srs_error_t err = srs_success;
if (!data || size <= 0) {
srs_info("no audio present, ignore it.");
return err;
}
SrsBuffer* buffer = new SrsBuffer(data, size);
SrsAutoFree(SrsBuffer, buffer);
// We already checked the size is positive and data is not NULL.
srs_assert(buffer->require(1));
// @see: E.4.2 Audio Tags, video_file_format_spec_v10_1.pdf, page 76
uint8_t v = buffer->read_1bytes();
SrsAudioCodecId codec = (SrsAudioCodecId)((v >> 4) & 0x0f);
if (codec != SrsAudioCodecIdMP3 && codec != SrsAudioCodecIdAAC) {
return err;
}
bool fresh = !acodec;
if (!acodec) {
acodec = new SrsAudioCodecConfig();
}
if (!audio) {
audio = new SrsAudioFrame();
}
if ((err = audio->initialize(acodec)) != srs_success) {
return srs_error_wrap(err, "init audio");
}
// Parse by specified codec.
buffer->skip(-1 * buffer->pos());
if (codec == SrsAudioCodecIdMP3) {
return audio_mp3_demux(buffer, timestamp, fresh);
}
return audio_aac_demux(buffer, timestamp);
}
srs_error_t SrsFormat::on_video(int64_t timestamp, char* data, int size)
{
srs_error_t err = srs_success;
if (!data || size <= 0) {
srs_trace("no video present, ignore it.");
return err;
}
SrsBuffer* buffer = new SrsBuffer(data, size);
SrsAutoFree(SrsBuffer, buffer);
return video_avc_demux(buffer, timestamp);
}
srs_error_t SrsFormat::on_aac_sequence_header(char* data, int size)
{
srs_error_t err = srs_success;
if (!acodec) {
acodec = new SrsAudioCodecConfig();
}
if (!audio) {
audio = new SrsAudioFrame();
}
if ((err = audio->initialize(acodec)) != srs_success) {
return srs_error_wrap(err, "init audio");
}
return audio_aac_sequence_header_demux(data, size);
}
bool SrsFormat::is_aac_sequence_header()
{
return acodec && acodec->id == SrsAudioCodecIdAAC
&& audio && audio->aac_packet_type == SrsAudioAacFrameTraitSequenceHeader;
}
bool SrsFormat::is_mp3_sequence_header()
{
return acodec && acodec->id == SrsAudioCodecIdMP3
&& audio && audio->aac_packet_type == SrsAudioMp3FrameTraitSequenceHeader;
}
bool SrsFormat::is_avc_sequence_header()
{
bool h264 = (vcodec && vcodec->id == SrsVideoCodecIdAVC);
bool h265 = (vcodec && vcodec->id == SrsVideoCodecIdHEVC);
bool av1 = (vcodec && vcodec->id == SrsVideoCodecIdAV1);
return vcodec && (h264 || h265 || av1)
&& video && video->avc_packet_type == SrsVideoAvcFrameTraitSequenceHeader;
}
srs_error_t SrsFormat::video_avc_demux(SrsBuffer* stream, int64_t timestamp)
{
srs_error_t err = srs_success;
if (!stream->require(1)) {
return srs_error_new(ERROR_HLS_DECODE_ERROR, "video avc demux shall atleast 1bytes");
}
// Parse the frame type and the first bit indicates the ext header.
uint8_t frame_type = stream->read_1bytes();
bool is_ext_header = frame_type & 0x80;
// @see: E.4.3 Video Tags, video_file_format_spec_v10_1.pdf, page 78
SrsVideoCodecId codec_id = SrsVideoCodecIdForbidden;
SrsVideoAvcFrameTrait packet_type = SrsVideoAvcFrameTraitForbidden;
if (!is_ext_header) {
// See rtmp_specification_1.0.pdf
codec_id = (SrsVideoCodecId)(frame_type & 0x0f);
frame_type = (frame_type >> 4) & 0x0f;
} else {
// See https://github.com/veovera/enhanced-rtmp
packet_type = (SrsVideoAvcFrameTrait)(frame_type & 0x0f);
frame_type = (frame_type >> 4) & 0x07;
if (!stream->require(4)) {
return srs_error_new(ERROR_HLS_DECODE_ERROR, "fourCC requires 4bytes, only %dbytes", stream->left());
}
uint32_t four_cc = stream->read_4bytes();
if (four_cc == 0x68766331) { // 'hvc1'=0x68766331
codec_id = SrsVideoCodecIdHEVC;
}
}
if (!vcodec) {
vcodec = new SrsVideoCodecConfig();
}
if (!video) {
video = new SrsVideoFrame();
}
if ((err = video->initialize(vcodec)) != srs_success) {
return srs_error_wrap(err, "init video");
}
video->frame_type = (SrsVideoAvcFrameType)frame_type;
// ignore info frame without error,
// @see https://github.com/ossrs/srs/issues/288#issuecomment-69863909
if (video->frame_type == SrsVideoAvcFrameTypeVideoInfoFrame) {
srs_warn("avc ignore the info frame");
return err;
}
// Check codec for H.264 and H.265.
bool codec_ok = (codec_id == SrsVideoCodecIdAVC);
#ifdef SRS_H265
codec_ok = codec_ok ? true : (codec_id == SrsVideoCodecIdHEVC);
#endif
if (!codec_ok) {
return srs_error_new(ERROR_HLS_DECODE_ERROR, "only support video H.264/H.265, actual=%d", codec_id);
}
vcodec->id = codec_id;
int32_t composition_time = 0;
if (!is_ext_header) {
// See rtmp_specification_1.0.pdf
if (!stream->require(4)) {
return srs_error_new(ERROR_HLS_DECODE_ERROR, "requires 4bytes, only %dbytes", stream->left());
}
packet_type = (SrsVideoAvcFrameTrait)stream->read_1bytes();
composition_time = stream->read_3bytes();
} else {
// See https://github.com/veovera/enhanced-rtmp
if (packet_type == SrsVideoHEVCFrameTraitPacketTypeCodedFrames) {
if (!stream->require(3)) {
return srs_error_new(ERROR_HLS_DECODE_ERROR, "requires 3 bytes, only %dbytes", stream->left());
}
composition_time = stream->read_3bytes();
}
}
// pts = dts + cts.
video->dts = timestamp;
video->cts = composition_time;
video->avc_packet_type = packet_type;
// Update the RAW AVC data.
raw = stream->data() + stream->pos();
nb_raw = stream->size() - stream->pos();
// Parse sequence header for H.265/HEVC.
if (codec_id == SrsVideoCodecIdHEVC) {
#ifdef SRS_H265
if (packet_type == SrsVideoAvcFrameTraitSequenceHeader) {
// TODO: demux vps/sps/pps for hevc
if ((err = hevc_demux_hvcc(stream)) != srs_success) {
return srs_error_wrap(err, "demux hevc VPS/SPS/PPS");
}
} else if (packet_type == SrsVideoAvcFrameTraitNALU || packet_type == SrsVideoHEVCFrameTraitPacketTypeCodedFramesX) {
// TODO: demux nalu for hevc
if ((err = video_nalu_demux(stream)) != srs_success) {
return srs_error_wrap(err, "demux hevc NALU");
}
}
return err;
#else
return srs_error_new(ERROR_HEVC_DISABLED, "H.265 is disabled");
#endif
}
// Parse sequence header for H.264/AVC.
if (packet_type == SrsVideoAvcFrameTraitSequenceHeader) {
// TODO: FIXME: Maybe we should ignore any error for parsing sps/pps.
if ((err = avc_demux_sps_pps(stream)) != srs_success) {
return srs_error_wrap(err, "demux SPS/PPS");
}
} else if (packet_type == SrsVideoAvcFrameTraitNALU){
if ((err = video_nalu_demux(stream)) != srs_success) {
return srs_error_wrap(err, "demux NALU");
}
} else {
// ignored.
}
return err;
}
// For media server, we don't care the codec, so we just try to parse sps-pps, and we could ignore any error if fail.
// LCOV_EXCL_START