2021-05-31 05:42:20 +00:00
|
|
|
//
|
2023-01-01 00:45:39 +00:00
|
|
|
// Copyright (c) 2013-2023 The SRS Authors
|
2021-05-31 05:42:20 +00:00
|
|
|
//
|
2022-01-13 10:40:17 +00:00
|
|
|
// SPDX-License-Identifier: MIT or MulanPSL-2.0
|
2021-05-31 05:42:20 +00:00
|
|
|
//
|
2013-11-23 03:36:07 +00:00
|
|
|
|
2014-05-28 09:37:15 +00:00
|
|
|
#include <srs_kernel_codec.hpp>
|
2013-11-23 03:36:07 +00:00
|
|
|
|
2013-11-24 06:28:58 +00:00
|
|
|
#include <string.h>
|
2013-11-24 15:13:14 +00:00
|
|
|
#include <stdlib.h>
|
2015-01-31 13:16:42 +00:00
|
|
|
using namespace std;
|
|
|
|
|
|
|
|
#include <srs_kernel_error.hpp>
|
|
|
|
#include <srs_kernel_log.hpp>
|
2015-09-22 00:48:55 +00:00
|
|
|
#include <srs_kernel_buffer.hpp>
|
2015-01-31 13:16:42 +00:00
|
|
|
#include <srs_kernel_utility.hpp>
|
2015-04-03 15:17:50 +00:00
|
|
|
#include <srs_core_autofree.hpp>
|
2020-05-11 04:07:55 +00:00
|
|
|
#include <srs_kernel_rtc_rtp.hpp>
|
2013-11-24 06:28:58 +00:00
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
string srs_video_codec_id2str(SrsVideoCodecId codec)
|
2015-03-08 07:33:08 +00:00
|
|
|
{
|
|
|
|
switch (codec) {
|
2017-02-12 12:38:39 +00:00
|
|
|
case SrsVideoCodecIdAVC:
|
2015-03-08 07:33:08 +00:00
|
|
|
return "H264";
|
2017-02-12 12:38:39 +00:00
|
|
|
case SrsVideoCodecIdOn2VP6:
|
|
|
|
case SrsVideoCodecIdOn2VP6WithAlphaChannel:
|
2015-03-08 11:59:10 +00:00
|
|
|
return "VP6";
|
2019-01-17 01:24:17 +00:00
|
|
|
case SrsVideoCodecIdHEVC:
|
|
|
|
return "HEVC";
|
2020-01-19 03:22:35 +00:00
|
|
|
case SrsVideoCodecIdAV1:
|
|
|
|
return "AV1";
|
2017-02-12 12:38:39 +00:00
|
|
|
case SrsVideoCodecIdReserved:
|
|
|
|
case SrsVideoCodecIdReserved1:
|
|
|
|
case SrsVideoCodecIdReserved2:
|
|
|
|
case SrsVideoCodecIdDisabled:
|
|
|
|
case SrsVideoCodecIdSorensonH263:
|
|
|
|
case SrsVideoCodecIdScreenVideo:
|
|
|
|
case SrsVideoCodecIdScreenVideoVersion2:
|
2015-03-08 07:33:08 +00:00
|
|
|
default:
|
|
|
|
return "Other";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
string srs_audio_codec_id2str(SrsAudioCodecId codec)
|
2015-03-08 07:33:08 +00:00
|
|
|
{
|
|
|
|
switch (codec) {
|
2017-02-12 12:38:39 +00:00
|
|
|
case SrsAudioCodecIdAAC:
|
2015-03-08 07:33:08 +00:00
|
|
|
return "AAC";
|
2017-02-12 12:38:39 +00:00
|
|
|
case SrsAudioCodecIdMP3:
|
2015-03-08 07:33:08 +00:00
|
|
|
return "MP3";
|
2019-01-17 01:24:17 +00:00
|
|
|
case SrsAudioCodecIdOpus:
|
|
|
|
return "Opus";
|
2017-02-12 12:38:39 +00:00
|
|
|
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:
|
2015-03-08 07:33:08 +00:00
|
|
|
default:
|
|
|
|
return "Other";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-11-18 03:19:01 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-02-12 12:46:24 +00:00
|
|
|
string srs_audio_sample_rate2str(SrsAudioSampleRate v)
|
2017-02-04 06:57:07 +00:00
|
|
|
{
|
|
|
|
switch (v) {
|
2017-02-12 12:38:39 +00:00
|
|
|
case SrsAudioSampleRate5512: return "5512";
|
|
|
|
case SrsAudioSampleRate11025: return "11025";
|
|
|
|
case SrsAudioSampleRate22050: return "22050";
|
|
|
|
case SrsAudioSampleRate44100: return "44100";
|
2019-01-17 03:35:15 +00:00
|
|
|
case SrsAudioSampleRateNB8kHz: return "NB8kHz";
|
|
|
|
case SrsAudioSampleRateMB12kHz: return "MB12kHz";
|
|
|
|
case SrsAudioSampleRateWB16kHz: return "WB16kHz";
|
|
|
|
case SrsAudioSampleRateSWB24kHz: return "SWB24kHz";
|
|
|
|
case SrsAudioSampleRateFB48kHz: return "FB48kHz";
|
2017-02-04 06:57:07 +00:00
|
|
|
default: return "Other";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
SrsFlvVideo::SrsFlvVideo()
|
2013-11-24 15:13:14 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
SrsFlvVideo::~SrsFlvVideo()
|
2013-11-24 15:13:14 +00:00
|
|
|
{
|
2013-11-24 04:39:47 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
bool SrsFlvVideo::keyframe(char* data, int size)
|
2013-11-24 04:39:47 +00:00
|
|
|
{
|
2014-03-18 03:32:58 +00:00
|
|
|
// 2bytes required.
|
|
|
|
if (size < 1) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-02-11 15:09:23 +00:00
|
|
|
|
2014-09-26 08:34:13 +00:00
|
|
|
char frame_type = data[0];
|
2014-03-18 03:32:58 +00:00
|
|
|
frame_type = (frame_type >> 4) & 0x0F;
|
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
return frame_type == SrsVideoAvcFrameTypeKeyFrame;
|
2013-11-23 03:36:07 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
bool SrsFlvVideo::sh(char* data, int size)
|
2013-11-23 03:36:07 +00:00
|
|
|
{
|
2022-11-23 00:34:13 +00:00
|
|
|
// Check sequence header only for H.264 or H.265
|
|
|
|
bool codec_ok = h264(data, size);
|
|
|
|
#ifdef SRS_H265
|
2022-12-13 13:03:27 +00:00
|
|
|
codec_ok = codec_ok? true : hevc(data, size);
|
2022-11-23 00:34:13 +00:00
|
|
|
#endif
|
|
|
|
if (!codec_ok) return false;
|
|
|
|
|
2014-03-18 03:32:58 +00:00
|
|
|
// 2bytes required.
|
|
|
|
if (size < 2) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-02-11 15:09:23 +00:00
|
|
|
|
2014-09-26 08:34:13 +00:00
|
|
|
char frame_type = data[0];
|
2014-03-18 03:32:58 +00:00
|
|
|
frame_type = (frame_type >> 4) & 0x0F;
|
2017-02-11 15:09:23 +00:00
|
|
|
|
2014-09-26 08:34:13 +00:00
|
|
|
char avc_packet_type = data[1];
|
2014-03-18 03:32:58 +00:00
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
return frame_type == SrsVideoAvcFrameTypeKeyFrame
|
|
|
|
&& avc_packet_type == SrsVideoAvcFrameTraitSequenceHeader;
|
2013-11-23 03:36:07 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
bool SrsFlvVideo::h264(char* data, int size)
|
2013-11-23 03:36:07 +00:00
|
|
|
{
|
2014-03-18 03:32:58 +00:00
|
|
|
// 1bytes required.
|
|
|
|
if (size < 1) {
|
|
|
|
return false;
|
|
|
|
}
|
2017-02-11 15:09:23 +00:00
|
|
|
|
2014-09-26 08:34:13 +00:00
|
|
|
char codec_id = data[0];
|
2014-03-18 03:32:58 +00:00
|
|
|
codec_id = codec_id & 0x0F;
|
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
return codec_id == SrsVideoCodecIdAVC;
|
2013-11-23 03:36:07 +00:00
|
|
|
}
|
2014-08-02 14:18:39 +00:00
|
|
|
|
2022-11-23 00:34:13 +00:00
|
|
|
#ifdef SRS_H265
|
|
|
|
bool SrsFlvVideo::hevc(char* data, int size)
|
|
|
|
{
|
|
|
|
// 1bytes required.
|
|
|
|
if (size < 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char codec_id = data[0];
|
|
|
|
codec_id = codec_id & 0x0F;
|
|
|
|
|
|
|
|
return codec_id == SrsVideoCodecIdHEVC;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
bool SrsFlvVideo::acceptable(char* data, int size)
|
2015-06-06 13:23:57 +00:00
|
|
|
{
|
|
|
|
// 1bytes required.
|
|
|
|
if (size < 1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
char frame_type = data[0];
|
2022-11-23 00:34:13 +00:00
|
|
|
SrsVideoCodecId codec_id = (SrsVideoCodecId)(uint8_t)(frame_type & 0x0f);
|
2015-06-06 13:23:57 +00:00
|
|
|
frame_type = (frame_type >> 4) & 0x0f;
|
|
|
|
|
|
|
|
if (frame_type < 1 || frame_type > 5) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2022-11-23 00:34:13 +00:00
|
|
|
if (codec_id != SrsVideoCodecIdAVC && codec_id != SrsVideoCodecIdAV1 && codec_id != SrsVideoCodecIdHEVC) {
|
2015-06-06 13:23:57 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2019-02-03 14:03:15 +00:00
|
|
|
SrsFlvAudio::SrsFlvAudio()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsFlvAudio::~SrsFlvAudio()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2017-02-11 15:09:23 +00:00
|
|
|
/**
|
|
|
|
* 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
|
2018-07-17 05:59:51 +00:00
|
|
|
int srs_flv_srates[] = {5512, 11025, 22050, 44100, 0};
|
2017-02-11 15:09:23 +00:00
|
|
|
|
|
|
|
// the sample rates in the codec,
|
|
|
|
// in the sequence header.
|
2017-02-12 12:38:39 +00:00
|
|
|
int srs_aac_srates[] =
|
2017-02-11 15:09:23 +00:00
|
|
|
{
|
|
|
|
96000, 88200, 64000, 48000,
|
|
|
|
44100, 32000, 24000, 22050,
|
|
|
|
16000, 12000, 11025, 8000,
|
|
|
|
7350, 0, 0, 0
|
|
|
|
};
|
|
|
|
|
2017-02-12 12:46:24 +00:00
|
|
|
string srs_audio_sample_bits2str(SrsAudioSampleBits v)
|
2017-02-04 06:57:07 +00:00
|
|
|
{
|
|
|
|
switch (v) {
|
2017-02-12 12:46:24 +00:00
|
|
|
case SrsAudioSampleBits16bit: return "16bits";
|
|
|
|
case SrsAudioSampleBits8bit: return "8bits";
|
2017-02-04 06:57:07 +00:00
|
|
|
default: return "Other";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-12 12:46:24 +00:00
|
|
|
string srs_audio_channels2str(SrsAudioChannels v)
|
2017-02-04 06:57:07 +00:00
|
|
|
{
|
|
|
|
switch (v) {
|
2017-02-12 12:46:24 +00:00
|
|
|
case SrsAudioChannelsStereo: return "Stereo";
|
|
|
|
case SrsAudioChannelsMono: return "Mono";
|
2017-02-04 06:57:07 +00:00
|
|
|
default: return "Other";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
string srs_avc_nalu2str(SrsAvcNaluType nalu_type)
|
2015-04-04 05:30:13 +00:00
|
|
|
{
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
string srs_aac_profile2str(SrsAacProfile aac_profile)
|
2017-02-11 15:09:23 +00:00
|
|
|
{
|
|
|
|
switch (aac_profile) {
|
|
|
|
case SrsAacProfileMain: return "Main";
|
|
|
|
case SrsAacProfileLC: return "LC";
|
|
|
|
case SrsAacProfileSSR: return "SSR";
|
|
|
|
default: return "Other";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
string srs_aac_object2str(SrsAacObjectType aac_object)
|
2017-02-11 15:09:23 +00:00
|
|
|
{
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
SrsAacObjectType srs_aac_ts2rtmp(SrsAacProfile profile)
|
2017-02-11 15:09:23 +00:00
|
|
|
{
|
|
|
|
switch (profile) {
|
|
|
|
case SrsAacProfileMain: return SrsAacObjectTypeAacMain;
|
|
|
|
case SrsAacProfileLC: return SrsAacObjectTypeAacLC;
|
|
|
|
case SrsAacProfileSSR: return SrsAacObjectTypeAacSSR;
|
|
|
|
default: return SrsAacObjectTypeReserved;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
SrsAacProfile srs_aac_rtmp2ts(SrsAacObjectType object_type)
|
2017-02-11 15:09:23 +00:00
|
|
|
{
|
|
|
|
switch (object_type) {
|
|
|
|
case SrsAacObjectTypeAacMain: return SrsAacProfileMain;
|
|
|
|
case SrsAacObjectTypeAacHE:
|
|
|
|
case SrsAacObjectTypeAacHEV2:
|
|
|
|
case SrsAacObjectTypeAacLC: return SrsAacProfileLC;
|
|
|
|
case SrsAacObjectTypeAacSSR: return SrsAacProfileSSR;
|
|
|
|
default: return SrsAacProfileReserved;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
string srs_avc_profile2str(SrsAvcProfile profile)
|
2017-02-11 15:09:23 +00:00
|
|
|
{
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
string srs_avc_level2str(SrsAvcLevel level)
|
2017-02-11 15:09:23 +00:00
|
|
|
{
|
|
|
|
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";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-04 14:46:14 +00:00
|
|
|
#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
|
|
|
|
|
2017-02-11 15:09:23 +00:00
|
|
|
SrsSample::SrsSample()
|
|
|
|
{
|
2017-02-12 10:18:18 +00:00
|
|
|
size = 0;
|
|
|
|
bytes = NULL;
|
2020-04-11 07:11:42 +00:00
|
|
|
bframe = false;
|
2017-02-11 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
2020-08-18 11:20:07 +00:00
|
|
|
SrsSample::SrsSample(char* b, int s)
|
|
|
|
{
|
|
|
|
size = s;
|
|
|
|
bytes = b;
|
|
|
|
bframe = false;
|
|
|
|
}
|
|
|
|
|
2017-02-11 15:09:23 +00:00
|
|
|
SrsSample::~SrsSample()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-04-11 07:11:42 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-04-13 11:23:17 +00:00
|
|
|
SrsSample* SrsSample::copy()
|
|
|
|
{
|
|
|
|
SrsSample* p = new SrsSample();
|
|
|
|
p->bytes = bytes;
|
|
|
|
p->size = size;
|
2020-05-14 06:26:19 +00:00
|
|
|
p->bframe = bframe;
|
2020-04-13 11:23:17 +00:00
|
|
|
return p;
|
|
|
|
}
|
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
SrsCodecConfig::SrsCodecConfig()
|
2017-02-11 15:09:23 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
SrsCodecConfig::~SrsCodecConfig()
|
2017-02-11 15:09:23 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
SrsAudioCodecConfig::SrsAudioCodecConfig()
|
2017-02-11 15:09:23 +00:00
|
|
|
{
|
2017-02-12 12:38:39 +00:00
|
|
|
id = SrsAudioCodecIdForbidden;
|
|
|
|
sound_rate = SrsAudioSampleRateForbidden;
|
2017-02-12 12:46:24 +00:00
|
|
|
sound_size = SrsAudioSampleBitsForbidden;
|
|
|
|
sound_type = SrsAudioChannelsForbidden;
|
2017-02-12 10:18:18 +00:00
|
|
|
|
|
|
|
audio_data_rate = 0;
|
|
|
|
|
|
|
|
aac_object = SrsAacObjectTypeForbidden;
|
2017-02-12 12:38:39 +00:00
|
|
|
aac_sample_rate = SrsAacSampleRateUnset; // sample rate ignored
|
2017-02-12 10:18:18 +00:00
|
|
|
aac_channels = 0;
|
2017-02-11 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
SrsAudioCodecConfig::~SrsAudioCodecConfig()
|
2017-02-11 15:09:23 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
bool SrsAudioCodecConfig::is_aac_codec_ok()
|
2017-02-11 15:09:23 +00:00
|
|
|
{
|
2017-04-24 02:08:57 +00:00
|
|
|
return !aac_extra_data.empty();
|
2017-02-11 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
SrsVideoCodecConfig::SrsVideoCodecConfig()
|
2015-01-31 13:16:42 +00:00
|
|
|
{
|
2017-02-12 12:38:39 +00:00
|
|
|
id = SrsVideoCodecIdForbidden;
|
2017-02-12 10:18:18 +00:00
|
|
|
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;
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
SrsVideoCodecConfig::~SrsVideoCodecConfig()
|
2015-01-31 13:16:42 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
bool SrsVideoCodecConfig::is_avc_codec_ok()
|
2017-02-11 15:09:23 +00:00
|
|
|
{
|
2017-04-24 02:08:57 +00:00
|
|
|
return !avc_extra_data.empty();
|
2017-02-11 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SrsFrame::SrsFrame()
|
|
|
|
{
|
|
|
|
codec = NULL;
|
|
|
|
nb_samples = 0;
|
2017-02-12 10:18:18 +00:00
|
|
|
dts = 0;
|
|
|
|
cts = 0;
|
2017-02-11 15:09:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SrsFrame::~SrsFrame()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t SrsFrame::initialize(SrsCodecConfig* c)
|
2015-01-31 13:16:42 +00:00
|
|
|
{
|
2017-02-12 10:18:18 +00:00
|
|
|
codec = c;
|
|
|
|
nb_samples = 0;
|
|
|
|
dts = 0;
|
|
|
|
cts = 0;
|
2017-09-23 14:12:33 +00:00
|
|
|
return srs_success;
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t SrsFrame::add_sample(char* bytes, int size)
|
2015-01-31 13:16:42 +00:00
|
|
|
{
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t err = srs_success;
|
2022-12-25 03:43:26 +00:00
|
|
|
|
|
|
|
// Ignore empty sample.
|
|
|
|
if (!bytes || size <= 0) return err;
|
2017-02-12 10:18:18 +00:00
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
if (nb_samples >= SrsMaxNbSamples) {
|
2017-09-23 14:12:33 +00:00
|
|
|
return srs_error_new(ERROR_HLS_DECODE_ERROR, "Frame samples overflow");
|
2017-02-12 10:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SrsSample* sample = &samples[nb_samples++];
|
|
|
|
sample->bytes = bytes;
|
|
|
|
sample->size = size;
|
2020-04-11 07:11:42 +00:00
|
|
|
sample->bframe = false;
|
2017-02-12 10:18:18 +00:00
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
return err;
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 10:18:18 +00:00
|
|
|
SrsAudioFrame::SrsAudioFrame()
|
2015-01-31 13:16:42 +00:00
|
|
|
{
|
2017-02-12 12:38:39 +00:00
|
|
|
aac_packet_type = SrsAudioAacFrameTraitForbidden;
|
2017-02-12 10:18:18 +00:00
|
|
|
}
|
2015-01-31 13:16:42 +00:00
|
|
|
|
2017-02-12 10:18:18 +00:00
|
|
|
SrsAudioFrame::~SrsAudioFrame()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
SrsAudioCodecConfig* SrsAudioFrame::acodec()
|
2017-02-12 10:18:18 +00:00
|
|
|
{
|
2017-02-12 12:38:39 +00:00
|
|
|
return (SrsAudioCodecConfig*)codec;
|
2017-02-12 10:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SrsVideoFrame::SrsVideoFrame()
|
|
|
|
{
|
2017-02-12 12:38:39 +00:00
|
|
|
frame_type = SrsVideoAvcFrameTypeForbidden;
|
|
|
|
avc_packet_type = SrsVideoAvcFrameTraitForbidden;
|
2017-02-12 10:18:18 +00:00
|
|
|
has_idr = has_aud = has_sps_pps = false;
|
|
|
|
first_nalu_type = SrsAvcNaluTypeForbidden;
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 10:18:18 +00:00
|
|
|
SrsVideoFrame::~SrsVideoFrame()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-04-11 05:10:27 +00:00
|
|
|
srs_error_t SrsVideoFrame::initialize(SrsCodecConfig* c)
|
|
|
|
{
|
|
|
|
first_nalu_type = SrsAvcNaluTypeForbidden;
|
|
|
|
has_idr = has_sps_pps = has_aud = false;
|
|
|
|
return SrsFrame::initialize(c);
|
|
|
|
}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t SrsVideoFrame::add_sample(char* bytes, int size)
|
2015-01-31 13:16:42 +00:00
|
|
|
{
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t err = srs_success;
|
2015-01-31 13:16:42 +00:00
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
if ((err = SrsFrame::add_sample(bytes, size)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "add frame");
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
2022-11-23 00:34:13 +00:00
|
|
|
|
|
|
|
SrsVideoCodecConfig* c = vcodec();
|
2023-01-30 03:20:47 +00:00
|
|
|
if (!bytes || size <= 0) return err;
|
2022-11-23 04:13:53 +00:00
|
|
|
|
|
|
|
// For HEVC(H.265), try to parse the IDR from NALUs.
|
|
|
|
if (c && c->id == SrsVideoCodecIdHEVC) {
|
|
|
|
#ifdef SRS_H265
|
2022-11-23 09:05:21 +00:00
|
|
|
SrsHevcNaluType nalu_type = SrsHevcNaluTypeParse(bytes[0]);
|
2022-11-23 04:13:53 +00:00
|
|
|
has_idr = (SrsHevcNaluType_CODED_SLICE_BLA <= nalu_type) && (nalu_type <= SrsHevcNaluType_RESERVED_23);
|
|
|
|
return err;
|
|
|
|
#else
|
2022-11-23 09:05:21 +00:00
|
|
|
return srs_error_new(ERROR_HEVC_DISABLED, "H.265 is disabled");
|
2022-11-23 00:34:13 +00:00
|
|
|
#endif
|
2022-11-23 04:13:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// By default, use AVC(H.264) to parse NALU.
|
|
|
|
// For video, parse the nalu type, set the IDR flag.
|
2017-02-12 10:18:18 +00:00
|
|
|
SrsAvcNaluType nal_unit_type = (SrsAvcNaluType)(bytes[0] & 0x1f);
|
2015-04-04 05:30:13 +00:00
|
|
|
|
2017-02-12 10:18:18 +00:00
|
|
|
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;
|
2015-04-04 05:30:13 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
return err;
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
SrsVideoCodecConfig* SrsVideoFrame::vcodec()
|
2015-01-31 13:16:42 +00:00
|
|
|
{
|
2017-02-12 12:38:39 +00:00
|
|
|
return (SrsVideoCodecConfig*)codec;
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 10:18:18 +00:00
|
|
|
SrsFormat::SrsFormat()
|
2015-01-31 13:16:42 +00:00
|
|
|
{
|
2017-02-12 10:18:18 +00:00
|
|
|
acodec = NULL;
|
|
|
|
vcodec = NULL;
|
|
|
|
audio = NULL;
|
|
|
|
video = NULL;
|
|
|
|
avc_parse_sps = true;
|
2022-09-01 11:28:16 +00:00
|
|
|
try_annexb_first = true;
|
2017-06-04 07:10:35 +00:00
|
|
|
raw = NULL;
|
|
|
|
nb_raw = 0;
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 10:18:18 +00:00
|
|
|
SrsFormat::~SrsFormat()
|
2015-07-20 01:31:46 +00:00
|
|
|
{
|
2017-02-12 10:18:18 +00:00
|
|
|
srs_freep(audio);
|
|
|
|
srs_freep(video);
|
|
|
|
srs_freep(acodec);
|
|
|
|
srs_freep(vcodec);
|
2015-07-20 01:31:46 +00:00
|
|
|
}
|
|
|
|
|
2017-06-11 01:40:07 +00:00
|
|
|
srs_error_t SrsFormat::initialize()
|
2015-07-20 01:31:46 +00:00
|
|
|
{
|
2023-01-22 05:47:24 +00:00
|
|
|
if (!vcodec) {
|
|
|
|
vcodec = new SrsVideoCodecConfig();
|
|
|
|
}
|
|
|
|
|
2017-06-11 01:40:07 +00:00
|
|
|
return srs_success;
|
2015-07-20 01:31:46 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t SrsFormat::on_audio(int64_t timestamp, char* data, int size)
|
2015-01-31 13:16:42 +00:00
|
|
|
{
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t err = srs_success;
|
2015-01-31 13:16:42 +00:00
|
|
|
|
|
|
|
if (!data || size <= 0) {
|
2020-04-30 02:57:03 +00:00
|
|
|
srs_info("no audio present, ignore it.");
|
2017-09-23 14:12:33 +00:00
|
|
|
return err;
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
SrsBuffer* buffer = new SrsBuffer(data, size);
|
|
|
|
SrsAutoFree(SrsBuffer, buffer);
|
2017-02-12 10:18:18 +00:00
|
|
|
|
2019-05-15 00:44:38 +00:00
|
|
|
// We already checked the size is positive and data is not NULL.
|
|
|
|
srs_assert(buffer->require(1));
|
2015-01-31 13:16:42 +00:00
|
|
|
|
|
|
|
// @see: E.4.2 Audio Tags, video_file_format_spec_v10_1.pdf, page 76
|
2017-03-05 10:44:37 +00:00
|
|
|
uint8_t v = buffer->read_1bytes();
|
|
|
|
SrsAudioCodecId codec = (SrsAudioCodecId)((v >> 4) & 0x0f);
|
2015-01-31 13:16:42 +00:00
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
if (codec != SrsAudioCodecIdMP3 && codec != SrsAudioCodecIdAAC) {
|
2017-09-23 14:12:33 +00:00
|
|
|
return err;
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
2022-12-25 13:11:08 +00:00
|
|
|
|
|
|
|
bool fresh = !acodec;
|
2017-02-12 10:18:18 +00:00
|
|
|
if (!acodec) {
|
2017-02-12 12:38:39 +00:00
|
|
|
acodec = new SrsAudioCodecConfig();
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
2017-02-12 10:18:18 +00:00
|
|
|
if (!audio) {
|
|
|
|
audio = new SrsAudioFrame();
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
if ((err = audio->initialize(acodec)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "init audio");
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
|
|
|
|
2017-06-13 08:10:46 +00:00
|
|
|
// Parse by specified codec.
|
2017-02-12 10:18:18 +00:00
|
|
|
buffer->skip(-1 * buffer->pos());
|
2017-06-13 08:10:46 +00:00
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
if (codec == SrsAudioCodecIdMP3) {
|
2022-12-25 13:11:08 +00:00
|
|
|
return audio_mp3_demux(buffer, timestamp, fresh);
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
2017-06-13 08:10:46 +00:00
|
|
|
|
|
|
|
return audio_aac_demux(buffer, timestamp);
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t SrsFormat::on_video(int64_t timestamp, char* data, int size)
|
2015-01-31 13:16:42 +00:00
|
|
|
{
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t err = srs_success;
|
2015-01-31 13:16:42 +00:00
|
|
|
|
2017-02-12 10:18:18 +00:00
|
|
|
if (!data || size <= 0) {
|
|
|
|
srs_trace("no video present, ignore it.");
|
2017-09-23 14:12:33 +00:00
|
|
|
return err;
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
2017-02-12 10:18:18 +00:00
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
SrsBuffer* buffer = new SrsBuffer(data, size);
|
|
|
|
SrsAutoFree(SrsBuffer, buffer);
|
2015-01-31 13:16:42 +00:00
|
|
|
|
2019-05-15 00:44:38 +00:00
|
|
|
// We already checked the size is positive and data is not NULL.
|
|
|
|
srs_assert(buffer->require(1));
|
2015-01-31 13:16:42 +00:00
|
|
|
|
2017-02-12 10:18:18 +00:00
|
|
|
// @see: E.4.3 Video Tags, video_file_format_spec_v10_1.pdf, page 78
|
|
|
|
int8_t frame_type = buffer->read_1bytes();
|
2017-02-12 12:38:39 +00:00
|
|
|
SrsVideoCodecId codec_id = (SrsVideoCodecId)(frame_type & 0x0f);
|
2022-11-23 00:34:13 +00:00
|
|
|
|
|
|
|
// Check codec for H.264 and H.265.
|
|
|
|
bool codec_ok = (codec_id == SrsVideoCodecIdAVC);
|
|
|
|
#ifdef SRS_H265
|
2022-12-13 13:03:27 +00:00
|
|
|
codec_ok = codec_ok ? true : (codec_id == SrsVideoCodecIdHEVC);
|
2022-11-23 00:34:13 +00:00
|
|
|
#endif
|
|
|
|
if (!codec_ok) return err;
|
2017-02-12 10:18:18 +00:00
|
|
|
|
|
|
|
if (!vcodec) {
|
2017-02-12 12:38:39 +00:00
|
|
|
vcodec = new SrsVideoCodecConfig();
|
2015-02-18 14:28:39 +00:00
|
|
|
}
|
2017-02-12 10:18:18 +00:00
|
|
|
if (!video) {
|
|
|
|
video = new SrsVideoFrame();
|
|
|
|
}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
if ((err = video->initialize(vcodec)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "init video");
|
2015-02-18 14:28:39 +00:00
|
|
|
}
|
2017-02-12 10:18:18 +00:00
|
|
|
|
|
|
|
buffer->skip(-1 * buffer->pos());
|
|
|
|
return video_avc_demux(buffer, timestamp);
|
2015-02-18 14:28:39 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t SrsFormat::on_aac_sequence_header(char* data, int size)
|
2015-01-31 13:16:42 +00:00
|
|
|
{
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t err = srs_success;
|
2015-01-31 13:16:42 +00:00
|
|
|
|
2017-02-12 10:18:18 +00:00
|
|
|
if (!acodec) {
|
2017-02-12 12:38:39 +00:00
|
|
|
acodec = new SrsAudioCodecConfig();
|
2017-02-12 10:18:18 +00:00
|
|
|
}
|
|
|
|
if (!audio) {
|
|
|
|
audio = new SrsAudioFrame();
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
if ((err = audio->initialize(acodec)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "init audio");
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
2017-02-12 10:18:18 +00:00
|
|
|
|
|
|
|
return audio_aac_sequence_header_demux(data, size);
|
|
|
|
}
|
2015-01-31 13:16:42 +00:00
|
|
|
|
2017-02-12 10:18:18 +00:00
|
|
|
bool SrsFormat::is_aac_sequence_header()
|
|
|
|
{
|
2017-02-12 12:38:39 +00:00
|
|
|
return acodec && acodec->id == SrsAudioCodecIdAAC
|
2017-02-26 12:40:33 +00:00
|
|
|
&& audio && audio->aac_packet_type == SrsAudioAacFrameTraitSequenceHeader;
|
2017-02-12 10:18:18 +00:00
|
|
|
}
|
|
|
|
|
2022-12-25 13:11:08 +00:00
|
|
|
bool SrsFormat::is_mp3_sequence_header()
|
|
|
|
{
|
|
|
|
return acodec && acodec->id == SrsAudioCodecIdMP3
|
|
|
|
&& audio && audio->aac_packet_type == SrsAudioMp3FrameTraitSequenceHeader;
|
|
|
|
}
|
|
|
|
|
2017-02-12 10:18:18 +00:00
|
|
|
bool SrsFormat::is_avc_sequence_header()
|
|
|
|
{
|
2020-01-19 05:16:49 +00:00
|
|
|
bool h264 = (vcodec && vcodec->id == SrsVideoCodecIdAVC);
|
|
|
|
bool h265 = (vcodec && vcodec->id == SrsVideoCodecIdHEVC);
|
|
|
|
bool av1 = (vcodec && vcodec->id == SrsVideoCodecIdAV1);
|
2020-01-19 03:22:35 +00:00
|
|
|
return vcodec && (h264 || h265 || av1)
|
2017-02-26 12:40:33 +00:00
|
|
|
&& video && video->avc_packet_type == SrsVideoAvcFrameTraitSequenceHeader;
|
2017-02-12 10:18:18 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t SrsFormat::video_avc_demux(SrsBuffer* stream, int64_t timestamp)
|
2017-02-12 10:18:18 +00:00
|
|
|
{
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t err = srs_success;
|
2015-01-31 13:16:42 +00:00
|
|
|
|
|
|
|
// @see: E.4.3 Video Tags, video_file_format_spec_v10_1.pdf, page 78
|
|
|
|
int8_t frame_type = stream->read_1bytes();
|
2017-02-12 12:38:39 +00:00
|
|
|
SrsVideoCodecId codec_id = (SrsVideoCodecId)(frame_type & 0x0f);
|
2015-01-31 13:16:42 +00:00
|
|
|
frame_type = (frame_type >> 4) & 0x0f;
|
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
video->frame_type = (SrsVideoAvcFrameType)frame_type;
|
2015-01-31 13:16:42 +00:00
|
|
|
|
|
|
|
// ignore info frame without error,
|
2015-11-11 02:37:50 +00:00
|
|
|
// @see https://github.com/ossrs/srs/issues/288#issuecomment-69863909
|
2017-02-12 12:38:39 +00:00
|
|
|
if (video->frame_type == SrsVideoAvcFrameTypeVideoInfoFrame) {
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_warn("avc igone the info frame");
|
|
|
|
return err;
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
2022-11-23 00:34:13 +00:00
|
|
|
|
|
|
|
// Check codec for H.264 and H.265.
|
|
|
|
bool codec_ok = (codec_id == SrsVideoCodecIdAVC);
|
|
|
|
#ifdef SRS_H265
|
2022-12-13 13:03:27 +00:00
|
|
|
codec_ok = codec_ok ? true : (codec_id == SrsVideoCodecIdHEVC);
|
2022-11-23 00:34:13 +00:00
|
|
|
#endif
|
|
|
|
if (!codec_ok) {
|
|
|
|
return srs_error_new(ERROR_HLS_DECODE_ERROR, "only support video H.264/H.265, actual=%d", codec_id);
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
2017-02-12 10:18:18 +00:00
|
|
|
vcodec->id = codec_id;
|
2015-01-31 13:16:42 +00:00
|
|
|
|
|
|
|
if (!stream->require(4)) {
|
2017-09-23 14:12:33 +00:00
|
|
|
return srs_error_new(ERROR_HLS_DECODE_ERROR, "avc decode avc_packet_type");
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
|
|
|
int8_t avc_packet_type = stream->read_1bytes();
|
|
|
|
int32_t composition_time = stream->read_3bytes();
|
|
|
|
|
|
|
|
// pts = dts + cts.
|
2017-02-12 10:18:18 +00:00
|
|
|
video->dts = timestamp;
|
|
|
|
video->cts = composition_time;
|
2017-02-12 12:38:39 +00:00
|
|
|
video->avc_packet_type = (SrsVideoAvcFrameTrait)avc_packet_type;
|
2015-01-31 13:16:42 +00:00
|
|
|
|
2017-06-04 07:10:35 +00:00
|
|
|
// Update the RAW AVC data.
|
|
|
|
raw = stream->data() + stream->pos();
|
|
|
|
nb_raw = stream->size() - stream->pos();
|
2022-11-23 00:34:13 +00:00
|
|
|
|
|
|
|
// Parse sequence header for H.265/HEVC.
|
|
|
|
if (codec_id == SrsVideoCodecIdHEVC) {
|
|
|
|
#ifdef SRS_H265
|
|
|
|
if (avc_packet_type == SrsVideoAvcFrameTraitSequenceHeader) {
|
|
|
|
// TODO: demux vps/sps/pps for hevc
|
|
|
|
if ((err = hevc_demux_hvcc(stream)) != srs_success) {
|
2023-02-08 12:23:25 +00:00
|
|
|
return srs_error_wrap(err, "demux hevc VPS/SPS/PPS");
|
2022-11-23 00:34:13 +00:00
|
|
|
}
|
|
|
|
} else if (avc_packet_type == SrsVideoAvcFrameTraitNALU) {
|
|
|
|
// TODO: demux nalu for hevc
|
|
|
|
if ((err = video_nalu_demux(stream)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "demux hevc NALU");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
#else
|
2022-11-23 09:05:21 +00:00
|
|
|
return srs_error_new(ERROR_HEVC_DISABLED, "H.265 is disabled");
|
2022-11-23 00:34:13 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse sequence header for H.264/AVC.
|
2017-02-12 12:38:39 +00:00
|
|
|
if (avc_packet_type == SrsVideoAvcFrameTraitSequenceHeader) {
|
2019-12-24 07:36:22 +00:00
|
|
|
// TODO: FIXME: Maybe we should ignore any error for parsing sps/pps.
|
2017-09-23 14:12:33 +00:00
|
|
|
if ((err = avc_demux_sps_pps(stream)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "demux SPS/PPS");
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
2017-02-12 12:38:39 +00:00
|
|
|
} else if (avc_packet_type == SrsVideoAvcFrameTraitNALU){
|
2017-09-23 14:12:33 +00:00
|
|
|
if ((err = video_nalu_demux(stream)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "demux NALU");
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
2017-01-10 09:17:23 +00:00
|
|
|
} else {
|
|
|
|
// ignored.
|
|
|
|
}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
return err;
|
2017-01-10 09:17:23 +00:00
|
|
|
}
|
2015-02-10 14:37:29 +00:00
|
|
|
|
2019-12-24 07:35:19 +00:00
|
|
|
// 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
|
|
|
|
|
2022-11-23 00:34:13 +00:00
|
|
|
#ifdef SRS_H265
|
2023-02-08 12:23:25 +00:00
|
|
|
// struct ptl
|
|
|
|
SrsHevcProfileTierLevel::SrsHevcProfileTierLevel()
|
|
|
|
{
|
|
|
|
general_profile_space = 0;
|
|
|
|
general_tier_flag = 0;
|
|
|
|
general_profile_idc = 0;
|
|
|
|
memset(general_profile_compatibility_flag, 0, 32);
|
|
|
|
general_progressive_source_flag = 0;
|
|
|
|
general_interlaced_source_flag = 0;
|
|
|
|
general_non_packed_constraint_flag = 0;
|
|
|
|
general_frame_only_constraint_flag = 0;
|
|
|
|
general_max_12bit_constraint_flag = 0;
|
|
|
|
general_max_10bit_constraint_flag = 0;
|
|
|
|
general_max_8bit_constraint_flag = 0;
|
|
|
|
general_max_422chroma_constraint_flag = 0;
|
|
|
|
general_max_420chroma_constraint_flag = 0;
|
|
|
|
general_max_monochrome_constraint_flag = 0;
|
|
|
|
general_intra_constraint_flag = 0;
|
|
|
|
general_one_picture_only_constraint_flag = 0;
|
|
|
|
general_lower_bit_rate_constraint_flag = 0;
|
|
|
|
general_max_14bit_constraint_flag = 0;
|
|
|
|
general_reserved_zero_7bits = 0;
|
|
|
|
general_reserved_zero_33bits = 0;
|
|
|
|
general_reserved_zero_34bits = 0;
|
|
|
|
general_reserved_zero_35bits = 0;
|
|
|
|
general_reserved_zero_43bits = 0;
|
|
|
|
general_inbld_flag = 0;
|
|
|
|
general_reserved_zero_bit = 0;
|
|
|
|
general_level_idc = 0;
|
|
|
|
memset(reserved_zero_2bits, 0, 8);
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsHevcProfileTierLevel::~SrsHevcProfileTierLevel()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-11-23 00:34:13 +00:00
|
|
|
// Parse the hevc vps/sps/pps
|
|
|
|
srs_error_t SrsFormat::hevc_demux_hvcc(SrsBuffer* stream)
|
|
|
|
{
|
2022-12-04 14:46:14 +00:00
|
|
|
srs_error_t err = srs_success;
|
|
|
|
|
2022-11-23 00:34:13 +00:00
|
|
|
int avc_extra_size = stream->size() - stream->pos();
|
|
|
|
if (avc_extra_size > 0) {
|
|
|
|
char *copy_stream_from = stream->data() + stream->pos();
|
|
|
|
vcodec->avc_extra_data = std::vector<char>(copy_stream_from, copy_stream_from + avc_extra_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
const int HEVC_MIN_SIZE = 23; // From configuration_version to numOfArrays
|
|
|
|
if (!stream->require(HEVC_MIN_SIZE)) {
|
2022-12-04 14:46:14 +00:00
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "requires %d only %d bytes", HEVC_MIN_SIZE, stream->left());
|
2022-11-23 00:34:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
SrsHevcDecoderConfigurationRecord* dec_conf_rec_p = &(vcodec->hevc_dec_conf_record_);
|
|
|
|
dec_conf_rec_p->configuration_version = stream->read_1bytes();
|
|
|
|
if (dec_conf_rec_p->configuration_version != 1) {
|
2022-12-04 14:46:14 +00:00
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "invalid version=%d", dec_conf_rec_p->configuration_version);
|
2022-11-23 00:34:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read general_profile_space(2bits), general_tier_flag(1bit), general_profile_idc(5bits)
|
|
|
|
uint8_t data_byte = stream->read_1bytes();
|
|
|
|
dec_conf_rec_p->general_profile_space = (data_byte >> 6) & 0x03;
|
|
|
|
dec_conf_rec_p->general_tier_flag = (data_byte >> 5) & 0x01;
|
|
|
|
dec_conf_rec_p->general_profile_idc = data_byte & 0x1F;
|
|
|
|
srs_info("hevc version:%d, general_profile_space:%d, general_tier_flag:%d, general_profile_idc:%d",
|
|
|
|
dec_conf_rec_p->configuration_version, dec_conf_rec_p->general_profile_space, dec_conf_rec_p->general_tier_flag,
|
|
|
|
dec_conf_rec_p->general_profile_idc);
|
|
|
|
|
|
|
|
//general_profile_compatibility_flags: 32bits
|
|
|
|
dec_conf_rec_p->general_profile_compatibility_flags = (uint32_t)stream->read_4bytes();
|
|
|
|
|
|
|
|
//general_constraint_indicator_flags: 48bits
|
|
|
|
uint64_t data_64bit = (uint64_t)stream->read_4bytes();
|
|
|
|
data_64bit = (data_64bit << 16) | (stream->read_2bytes());
|
|
|
|
dec_conf_rec_p->general_constraint_indicator_flags = data_64bit;
|
|
|
|
|
|
|
|
//general_level_idc: 8bits
|
|
|
|
dec_conf_rec_p->general_level_idc = stream->read_1bytes();
|
|
|
|
//min_spatial_segmentation_idc: xxxx 14bits
|
|
|
|
dec_conf_rec_p->min_spatial_segmentation_idc = stream->read_2bytes() & 0x0fff;
|
|
|
|
//parallelism_type: xxxx xx 2bits
|
|
|
|
dec_conf_rec_p->parallelism_type = stream->read_1bytes() & 0x03;
|
|
|
|
//chroma_format: xxxx xx 2bits
|
|
|
|
dec_conf_rec_p->chroma_format = stream->read_1bytes() & 0x03;
|
|
|
|
//bit_depth_luma_minus8: xxxx x 3bits
|
|
|
|
dec_conf_rec_p->bit_depth_luma_minus8 = stream->read_1bytes() & 0x07;
|
|
|
|
//bit_depth_chroma_minus8: xxxx x 3bits
|
|
|
|
dec_conf_rec_p->bit_depth_chroma_minus8 = stream->read_1bytes() & 0x07;
|
|
|
|
srs_info("general_constraint_indicator_flags:0x%x, general_level_idc:%d, min_spatial_segmentation_idc:%d, parallelism_type:%d, chroma_format:%d, bit_depth_luma_minus8:%d, bit_depth_chroma_minus8:%d",
|
|
|
|
dec_conf_rec_p->general_constraint_indicator_flags, dec_conf_rec_p->general_level_idc,
|
|
|
|
dec_conf_rec_p->min_spatial_segmentation_idc, dec_conf_rec_p->parallelism_type, dec_conf_rec_p->chroma_format,
|
|
|
|
dec_conf_rec_p->bit_depth_luma_minus8, dec_conf_rec_p->bit_depth_chroma_minus8);
|
|
|
|
|
|
|
|
//avg_frame_rate: 16bits
|
2022-12-04 14:46:14 +00:00
|
|
|
vcodec->frame_rate = dec_conf_rec_p->avg_frame_rate = stream->read_2bytes();
|
2022-11-23 00:34:13 +00:00
|
|
|
//8bits: constant_frame_rate(2bits), num_temporal_layers(3bits),
|
|
|
|
// temporal_id_nested(1bit), length_size_minus_one(2bits)
|
|
|
|
data_byte = stream->read_1bytes();
|
|
|
|
dec_conf_rec_p->constant_frame_rate = (data_byte >> 6) & 0x03;
|
|
|
|
dec_conf_rec_p->num_temporal_layers = (data_byte >> 3) & 0x07;
|
|
|
|
dec_conf_rec_p->temporal_id_nested = (data_byte >> 2) & 0x01;
|
2022-11-23 04:13:53 +00:00
|
|
|
|
|
|
|
// Parse the NALU size.
|
2022-11-23 00:34:13 +00:00
|
|
|
dec_conf_rec_p->length_size_minus_one = data_byte & 0x03;
|
2022-11-23 04:13:53 +00:00
|
|
|
vcodec->NAL_unit_length = dec_conf_rec_p->length_size_minus_one;
|
|
|
|
|
|
|
|
// 5.3.4.2.1 Syntax, ISO_IEC_14496-15-AVC-format-2012.pdf, page 16
|
|
|
|
// 5.2.4.1 AVC decoder configuration record
|
|
|
|
// 5.2.4.1.2 Semantics
|
|
|
|
// The value of this field shall be one of 0, 1, or 3 corresponding to a
|
|
|
|
// length encoded with 1, 2, or 4 bytes, respectively.
|
|
|
|
if (vcodec->NAL_unit_length == 2) {
|
2022-12-04 14:46:14 +00:00
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "sps lengthSizeMinusOne should never be 2");
|
2022-11-23 04:13:53 +00:00
|
|
|
}
|
2022-11-23 00:34:13 +00:00
|
|
|
|
|
|
|
uint8_t numOfArrays = stream->read_1bytes();
|
|
|
|
srs_info("avg_frame_rate:%d, constant_frame_rate:%d, num_temporal_layers:%d, temporal_id_nested:%d, length_size_minus_one:%d, numOfArrays:%d",
|
|
|
|
dec_conf_rec_p->avg_frame_rate, dec_conf_rec_p->constant_frame_rate, dec_conf_rec_p->num_temporal_layers,
|
|
|
|
dec_conf_rec_p->temporal_id_nested, dec_conf_rec_p->length_size_minus_one, numOfArrays);
|
|
|
|
|
|
|
|
//parse vps/pps/sps
|
|
|
|
for (int index = 0; index < numOfArrays; index++) {
|
2023-01-17 10:04:53 +00:00
|
|
|
if (!stream->require(3)) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "requires 3 only %d bytes", stream->left());
|
2022-11-23 00:34:13 +00:00
|
|
|
}
|
|
|
|
data_byte = stream->read_1bytes();
|
2023-01-17 10:04:53 +00:00
|
|
|
|
|
|
|
SrsHevcHvccNalu hevc_unit;
|
2022-11-23 00:34:13 +00:00
|
|
|
hevc_unit.array_completeness = (data_byte >> 7) & 0x01;
|
|
|
|
hevc_unit.nal_unit_type = data_byte & 0x3f;
|
|
|
|
hevc_unit.num_nalus = stream->read_2bytes();
|
|
|
|
|
|
|
|
for (int i = 0; i < hevc_unit.num_nalus; i++) {
|
2023-01-17 10:04:53 +00:00
|
|
|
if (!stream->require(2)) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "num_nalus requires 2 only %d bytes", stream->left());
|
|
|
|
}
|
|
|
|
|
2022-11-23 00:34:13 +00:00
|
|
|
SrsHevcNalData data_item;
|
|
|
|
data_item.nal_unit_length = stream->read_2bytes();
|
|
|
|
|
|
|
|
if (!stream->require(data_item.nal_unit_length)) {
|
2022-12-04 14:46:14 +00:00
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "requires %d only %d bytes",
|
2022-11-23 00:34:13 +00:00
|
|
|
data_item.nal_unit_length, stream->left());
|
|
|
|
}
|
|
|
|
//copy vps/pps/sps data
|
|
|
|
data_item.nal_unit_data.resize(data_item.nal_unit_length);
|
|
|
|
|
|
|
|
stream->read_bytes((char*)(&data_item.nal_unit_data[0]), data_item.nal_unit_length);
|
|
|
|
srs_info("hevc nalu type:%d, array_completeness:%d, num_nalus:%d, i:%d, nal_unit_length:%d",
|
|
|
|
hevc_unit.nal_unit_type, hevc_unit.array_completeness, hevc_unit.num_nalus, i, data_item.nal_unit_length);
|
|
|
|
hevc_unit.nal_data_vec.push_back(data_item);
|
|
|
|
}
|
|
|
|
dec_conf_rec_p->nalu_vec.push_back(hevc_unit);
|
2022-12-04 14:46:14 +00:00
|
|
|
|
|
|
|
// demux nalu
|
|
|
|
if ((err = hevc_demux_vps_sps_pps(&hevc_unit)) != srs_success) {
|
2023-02-08 12:23:25 +00:00
|
|
|
return srs_error_wrap(err, "hevc demux vps/sps/pps failed");
|
2022-12-04 14:46:14 +00:00
|
|
|
}
|
2022-11-23 00:34:13 +00:00
|
|
|
}
|
|
|
|
|
2022-12-04 14:46:14 +00:00
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
srs_error_t SrsFormat::hevc_demux_vps_sps_pps(SrsHevcHvccNalu* nal)
|
|
|
|
{
|
|
|
|
srs_error_t err = srs_success;
|
|
|
|
|
2023-01-17 10:04:53 +00:00
|
|
|
if (nal->nal_data_vec.empty()) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: FIXME: Support for multiple VPS/SPS/PPS, then pick the first non-empty one.
|
|
|
|
char *frame = (char *)(&nal->nal_data_vec[0].nal_unit_data[0]);
|
|
|
|
int nb_frame = nal->nal_data_vec[0].nal_unit_length;
|
|
|
|
SrsBuffer stream(frame, nb_frame);
|
|
|
|
|
2022-12-04 14:46:14 +00:00
|
|
|
// nal data
|
2023-01-17 10:04:53 +00:00
|
|
|
switch (nal->nal_unit_type) {
|
2022-12-04 14:46:14 +00:00
|
|
|
case SrsHevcNaluType_VPS:
|
2023-01-17 10:04:53 +00:00
|
|
|
err = hevc_demux_vps(&stream);
|
2022-12-04 14:46:14 +00:00
|
|
|
break;
|
|
|
|
case SrsHevcNaluType_SPS:
|
2023-01-17 10:04:53 +00:00
|
|
|
err = hevc_demux_sps(&stream);
|
2022-12-04 14:46:14 +00:00
|
|
|
break;
|
|
|
|
case SrsHevcNaluType_PPS:
|
2023-01-17 10:04:53 +00:00
|
|
|
err = hevc_demux_pps(&stream);
|
2022-12-04 14:46:14 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2023-01-17 10:04:53 +00:00
|
|
|
srs_error_t SrsFormat::hevc_demux_vps(SrsBuffer *stream)
|
|
|
|
{
|
|
|
|
// for NALU, ITU-T H.265 7.3.2.1 Video parameter set RBSP syntax
|
|
|
|
// @see 7.3.1.2 NAL unit header syntax
|
|
|
|
// @doc ITU-T-H.265-2021.pdf, page 53.
|
|
|
|
|
|
|
|
if (!stream->require(1)) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "decode hevc vps requires 1 only %d bytes", stream->left());
|
|
|
|
}
|
|
|
|
int8_t nutv = stream->read_1bytes();
|
|
|
|
|
|
|
|
// forbidden_zero_bit shall be equal to 0.
|
|
|
|
int8_t forbidden_zero_bit = (nutv >> 7) & 0x01;
|
|
|
|
if (forbidden_zero_bit) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "hevc forbidden_zero_bit=%d shall be equal to 0", forbidden_zero_bit);
|
|
|
|
}
|
|
|
|
|
|
|
|
// nal_unit_type specifies the type of RBSP data structure contained in the NAL unit as specified in Table 7-1.
|
|
|
|
// @see 7.4.2.2 NAL unit header semantics
|
|
|
|
// @doc ITU-T-H.265-2021.pdf, page 86.
|
|
|
|
SrsHevcNaluType nal_unit_type = (SrsHevcNaluType)((nutv >> 1) & 0x3f);
|
|
|
|
if (nal_unit_type != SrsHevcNaluType_VPS) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "hevc vps nal_unit_type=%d shall be equal to 33", nal_unit_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
// nuh_layer_id + nuh_temporal_id_plus1
|
|
|
|
stream->skip(1);
|
|
|
|
|
|
|
|
// decode the rbsp from vps.
|
|
|
|
// rbsp[ i ] a raw byte sequence payload is specified as an ordered sequence of bytes.
|
|
|
|
std::vector<int8_t> rbsp(stream->size());
|
|
|
|
|
|
|
|
int nb_rbsp = 0;
|
|
|
|
while (!stream->empty()) {
|
|
|
|
rbsp[nb_rbsp] = stream->read_1bytes();
|
|
|
|
|
|
|
|
// XX 00 00 03 XX, the 03 byte should be drop.
|
|
|
|
if (nb_rbsp > 2 && rbsp[nb_rbsp - 2] == 0 && rbsp[nb_rbsp - 1] == 0 && rbsp[nb_rbsp] == 3) {
|
|
|
|
// read 1byte more.
|
|
|
|
if (stream->empty()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rbsp[nb_rbsp] = stream->read_1bytes();
|
|
|
|
nb_rbsp++;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nb_rbsp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hevc_demux_vps_rbsp((char*)&rbsp[0], nb_rbsp);
|
|
|
|
}
|
|
|
|
|
|
|
|
srs_error_t SrsFormat::hevc_demux_vps_rbsp(char* rbsp, int nb_rbsp)
|
2022-12-04 14:46:14 +00:00
|
|
|
{
|
|
|
|
srs_error_t err = srs_success;
|
|
|
|
|
2023-01-17 10:04:53 +00:00
|
|
|
// reparse the rbsp.
|
|
|
|
SrsBuffer stream(rbsp, nb_rbsp);
|
|
|
|
|
|
|
|
// H265 VPS (video_parameter_set_rbsp()) NAL Unit.
|
|
|
|
// Section 7.3.2.1 ("Video parameter set RBSP syntax") of the H.265
|
|
|
|
// ITU-T-H.265-2021.pdf, page 54.
|
|
|
|
if (!stream.require(4)) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "vps requires 4 only %d bytes", stream.left());
|
|
|
|
}
|
|
|
|
|
|
|
|
SrsBitBuffer bs(&stream);
|
|
|
|
|
|
|
|
// vps_video_parameter_set_id u(4)
|
|
|
|
int vps_video_parameter_set_id = bs.read_bits(4);
|
2023-01-30 03:20:47 +00:00
|
|
|
if (vps_video_parameter_set_id < 0 || vps_video_parameter_set_id > SrsHevcMax_VPS_COUNT) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "vps id out of range: %d", vps_video_parameter_set_id);
|
2022-12-04 14:46:14 +00:00
|
|
|
}
|
|
|
|
|
2023-01-17 10:04:53 +00:00
|
|
|
// select table
|
|
|
|
SrsHevcDecoderConfigurationRecord *dec_conf_rec = &(vcodec->hevc_dec_conf_record_);
|
|
|
|
SrsHevcRbspVps *vps = &(dec_conf_rec->vps_table[vps_video_parameter_set_id]);
|
2022-12-04 14:46:14 +00:00
|
|
|
|
2023-01-17 10:04:53 +00:00
|
|
|
vps->vps_video_parameter_set_id = vps_video_parameter_set_id;
|
|
|
|
// vps_base_layer_internal_flag u(1)
|
|
|
|
vps->vps_base_layer_internal_flag = bs.read_bit();
|
|
|
|
// vps_base_layer_available_flag u(1)
|
|
|
|
vps->vps_base_layer_available_flag = bs.read_bit();
|
|
|
|
// vps_max_layers_minus1 u(6)
|
|
|
|
vps->vps_max_layers_minus1 = bs.read_bits(6);
|
|
|
|
// vps_max_sub_layers_minus1 u(3)
|
|
|
|
vps->vps_max_sub_layers_minus1 = bs.read_bits(3);
|
|
|
|
// vps_temporal_id_nesting_flag u(1)
|
|
|
|
vps->vps_temporal_id_nesting_flag = bs.read_bit();
|
|
|
|
// vps_reserved_0xffff_16bits u(16)
|
|
|
|
vps->vps_reserved_0xffff_16bits = bs.read_bits(16);
|
2022-12-04 14:46:14 +00:00
|
|
|
|
2023-01-17 10:04:53 +00:00
|
|
|
// profile_tier_level(1, vps_max_sub_layers_minus1)
|
|
|
|
if ((err = hevc_demux_rbsp_ptl(&bs, &vps->ptl, 1, vps->vps_max_sub_layers_minus1)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "vps rbsp ptl vps_max_sub_layers_minus1=%d", vps->vps_max_sub_layers_minus1);
|
|
|
|
}
|
|
|
|
|
|
|
|
dec_conf_rec->general_profile_idc = vps->ptl.general_profile_idc;
|
|
|
|
dec_conf_rec->general_level_idc = vps->ptl.general_level_idc;
|
|
|
|
dec_conf_rec->general_tier_flag = vps->ptl.general_tier_flag;
|
|
|
|
|
|
|
|
if (!bs.require_bits(1)) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "sublayer flag requires 1 only %d bits", bs.left_bits());
|
|
|
|
}
|
|
|
|
|
|
|
|
// vps_sub_layer_ordering_info_present_flag u(1)
|
|
|
|
vps->vps_sub_layer_ordering_info_present_flag = bs.read_bit();
|
|
|
|
|
|
|
|
for (int i = (vps->vps_sub_layer_ordering_info_present_flag ? 0 : vps->vps_max_sub_layers_minus1);
|
|
|
|
i <= vps->vps_max_sub_layers_minus1; i++)
|
|
|
|
{
|
|
|
|
// vps_max_dec_pic_buffering_minus1[i] ue(v)
|
|
|
|
if ((err = bs.read_bits_ue(vps->vps_max_dec_pic_buffering_minus1[i])) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "max_dec_pic_buffering_minus1");
|
|
|
|
}
|
|
|
|
// vps_max_num_reorder_pics[i] ue(v)
|
|
|
|
if ((err = bs.read_bits_ue(vps->vps_max_num_reorder_pics[i])) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "max_num_reorder_pics");
|
|
|
|
}
|
|
|
|
// vps_max_latency_increase_plus1[i] ue(v)
|
|
|
|
if ((err = bs.read_bits_ue(vps->vps_max_latency_increase_plus1[i])) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "max_latency_increase_plus1");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bs.require_bits(6)) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "vps maxlayer requires 10 only %d bits", bs.left_bits());
|
|
|
|
}
|
|
|
|
|
|
|
|
// vps_max_layer_id u(6)
|
|
|
|
vps->vps_max_layer_id = bs.read_bits(6);
|
|
|
|
|
|
|
|
// vps_num_layer_sets_minus1 ue(v)
|
|
|
|
if ((err = bs.read_bits_ue(vps->vps_num_layer_sets_minus1)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "num_layer_sets_minus1");
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: FIXME: Implements it, you might parse remain bits for video_parameter_set_rbsp.
|
|
|
|
// @see 7.3.2.1 Video parameter set RBSP
|
|
|
|
// @doc ITU-T-H.265-2021.pdf, page 54.
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
srs_error_t SrsFormat::hevc_demux_sps(SrsBuffer *stream)
|
|
|
|
{
|
2022-12-04 14:46:14 +00:00
|
|
|
// for NALU, ITU-T H.265 7.3.2.2 Sequence parameter set RBSP syntax
|
2023-01-17 10:04:53 +00:00
|
|
|
// @see 7.3.2.2.1 General sequence parameter set RBSP syntax
|
|
|
|
// @doc ITU-T-H.265-2021.pdf, page 55.
|
|
|
|
|
|
|
|
if (!stream->require(1)) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "decode hevc sps requires 1 only %d bytes", stream->left());
|
2022-12-04 14:46:14 +00:00
|
|
|
}
|
2023-01-17 10:04:53 +00:00
|
|
|
int8_t nutv = stream->read_1bytes();
|
2022-12-04 14:46:14 +00:00
|
|
|
|
|
|
|
// forbidden_zero_bit shall be equal to 0.
|
|
|
|
int8_t forbidden_zero_bit = (nutv >> 7) & 0x01;
|
|
|
|
if (forbidden_zero_bit) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "hevc forbidden_zero_bit=%d shall be equal to 0", forbidden_zero_bit);
|
|
|
|
}
|
|
|
|
|
|
|
|
// nal_unit_type specifies the type of RBSP data structure contained in the NAL unit as specified in Table 7-1.
|
2023-01-17 10:04:53 +00:00
|
|
|
// @see 7.4.2.2 NAL unit header semantics
|
|
|
|
// @doc ITU-T-H.265-2021.pdf, page 86.
|
2022-12-04 14:46:14 +00:00
|
|
|
SrsHevcNaluType nal_unit_type = (SrsHevcNaluType)((nutv >> 1) & 0x3f);
|
|
|
|
if (nal_unit_type != SrsHevcNaluType_SPS) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "hevc sps nal_unit_type=%d shall be equal to 33", nal_unit_type);
|
|
|
|
}
|
|
|
|
|
2023-01-17 10:04:53 +00:00
|
|
|
// nuh_layer_id + nuh_temporal_id_plus1
|
|
|
|
stream->skip(1);
|
2022-12-04 14:46:14 +00:00
|
|
|
|
|
|
|
// decode the rbsp from sps.
|
|
|
|
// rbsp[ i ] a raw byte sequence payload is specified as an ordered sequence of bytes.
|
2023-01-17 10:04:53 +00:00
|
|
|
std::vector<int8_t> rbsp(stream->size());
|
2022-12-04 14:46:14 +00:00
|
|
|
|
|
|
|
int nb_rbsp = 0;
|
2023-01-17 10:04:53 +00:00
|
|
|
while (!stream->empty()) {
|
|
|
|
rbsp[nb_rbsp] = stream->read_1bytes();
|
2022-12-04 14:46:14 +00:00
|
|
|
|
|
|
|
// XX 00 00 03 XX, the 03 byte should be drop.
|
|
|
|
if (nb_rbsp > 2 && rbsp[nb_rbsp - 2] == 0 && rbsp[nb_rbsp - 1] == 0 && rbsp[nb_rbsp] == 3) {
|
|
|
|
// read 1byte more.
|
2023-01-17 10:04:53 +00:00
|
|
|
if (stream->empty()) {
|
2022-12-04 14:46:14 +00:00
|
|
|
break;
|
|
|
|
}
|
2023-01-17 10:04:53 +00:00
|
|
|
rbsp[nb_rbsp] = stream->read_1bytes();
|
2022-12-04 14:46:14 +00:00
|
|
|
nb_rbsp++;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nb_rbsp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hevc_demux_sps_rbsp((char*)&rbsp[0], nb_rbsp);
|
2022-11-23 00:34:13 +00:00
|
|
|
}
|
2022-12-04 14:46:14 +00:00
|
|
|
|
|
|
|
srs_error_t SrsFormat::hevc_demux_sps_rbsp(char* rbsp, int nb_rbsp)
|
|
|
|
{
|
|
|
|
srs_error_t err = srs_success;
|
|
|
|
|
|
|
|
// we donot parse the detail of sps.
|
|
|
|
// @see https://github.com/ossrs/srs/issues/474
|
|
|
|
if (!avc_parse_sps) {
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
// reparse the rbsp.
|
|
|
|
SrsBuffer stream(rbsp, nb_rbsp);
|
|
|
|
|
2023-01-17 10:04:53 +00:00
|
|
|
// H265 SPS Nal Unit (seq_parameter_set_rbsp()) parser.
|
|
|
|
// Section 7.3.2.2 ("Sequence parameter set RBSP syntax") of the H.265
|
|
|
|
// ITU-T-H.265-2021.pdf, page 55.
|
2022-12-04 14:46:14 +00:00
|
|
|
if (!stream.require(2)) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "sps requires 2 only %d bytes", stream.left());
|
|
|
|
}
|
|
|
|
uint8_t nutv = stream.read_1bytes();
|
|
|
|
|
2023-01-17 10:04:53 +00:00
|
|
|
// sps_video_parameter_set_id u(4)
|
2022-12-04 14:46:14 +00:00
|
|
|
int sps_video_parameter_set_id = (nutv >> 4) & 0x0f;
|
2023-01-17 10:04:53 +00:00
|
|
|
// sps_max_sub_layers_minus1 u(3)
|
2022-12-04 14:46:14 +00:00
|
|
|
int sps_max_sub_layers_minus1 = (nutv >> 1) & 0x07;
|
2023-01-17 10:04:53 +00:00
|
|
|
// sps_temporal_id_nesting_flag u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
int sps_temporal_id_nesting_flag = nutv & 0x01;
|
|
|
|
|
|
|
|
SrsBitBuffer bs(&stream);
|
|
|
|
|
|
|
|
// profile tier level...
|
2023-01-17 10:04:53 +00:00
|
|
|
SrsHevcProfileTierLevel profile_tier_level;
|
|
|
|
// profile_tier_level(1, sps_max_sub_layers_minus1)
|
|
|
|
if ((err = hevc_demux_rbsp_ptl(&bs, &profile_tier_level, 1, sps_max_sub_layers_minus1)) != srs_success) {
|
2022-12-04 14:46:14 +00:00
|
|
|
return srs_error_wrap(err, "sps rbsp ptl sps_max_sub_layers_minus1=%d", sps_max_sub_layers_minus1);
|
|
|
|
}
|
|
|
|
|
|
|
|
vcodec->hevc_profile = (SrsHevcProfile)profile_tier_level.general_profile_idc;
|
|
|
|
vcodec->hevc_level = (SrsHevcLevel)profile_tier_level.general_level_idc;
|
|
|
|
|
2023-01-17 10:04:53 +00:00
|
|
|
// sps_seq_parameter_set_id ue(v)
|
|
|
|
uint32_t sps_seq_parameter_set_id = 0;
|
|
|
|
if ((err = bs.read_bits_ue(sps_seq_parameter_set_id)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "sps_seq_parameter_set_id");
|
|
|
|
}
|
2023-01-30 03:20:47 +00:00
|
|
|
if (sps_seq_parameter_set_id < 0 || sps_seq_parameter_set_id >= SrsHevcMax_SPS_COUNT) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "sps id out of range: %d", sps_seq_parameter_set_id);
|
|
|
|
}
|
2023-01-17 10:04:53 +00:00
|
|
|
|
2022-12-04 14:46:14 +00:00
|
|
|
// for sps_table
|
2023-01-17 10:04:53 +00:00
|
|
|
SrsHevcDecoderConfigurationRecord *dec_conf_rec = &(vcodec->hevc_dec_conf_record_);
|
|
|
|
SrsHevcRbspSps *sps = &(dec_conf_rec->sps_table[sps_seq_parameter_set_id]);
|
|
|
|
|
|
|
|
sps->sps_video_parameter_set_id = sps_video_parameter_set_id;
|
|
|
|
sps->sps_max_sub_layers_minus1 = sps_max_sub_layers_minus1;
|
|
|
|
sps->sps_temporal_id_nesting_flag = sps_temporal_id_nesting_flag;
|
|
|
|
sps->sps_seq_parameter_set_id = sps_seq_parameter_set_id;
|
2023-02-08 12:23:25 +00:00
|
|
|
sps->ptl = profile_tier_level;
|
2022-12-04 14:46:14 +00:00
|
|
|
|
2023-01-17 10:04:53 +00:00
|
|
|
// chroma_format_idc ue(v)
|
|
|
|
if ((err = bs.read_bits_ue(sps->chroma_format_idc)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "chroma_format_idc");
|
2022-12-04 14:46:14 +00:00
|
|
|
}
|
|
|
|
|
2023-01-17 10:04:53 +00:00
|
|
|
if (sps->chroma_format_idc == 3) {
|
|
|
|
if (!bs.require_bits(1)) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "separate_colour_plane_flag requires 1 only %d bits", bs.left_bits());
|
|
|
|
}
|
|
|
|
|
|
|
|
// separate_colour_plane_flag u(1)
|
|
|
|
sps->separate_colour_plane_flag = bs.read_bit();
|
|
|
|
}
|
2022-12-04 14:46:14 +00:00
|
|
|
|
2023-01-17 10:04:53 +00:00
|
|
|
// pic_width_in_luma_samples ue(v)
|
|
|
|
if ((err = bs.read_bits_ue(sps->pic_width_in_luma_samples)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "pic_width_in_luma_samples");
|
|
|
|
}
|
|
|
|
|
|
|
|
// pic_height_in_luma_samples ue(v)
|
|
|
|
if ((err = bs.read_bits_ue(sps->pic_height_in_luma_samples)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "pic_height_in_luma_samples");
|
|
|
|
}
|
|
|
|
|
|
|
|
vcodec->width = sps->pic_width_in_luma_samples;
|
|
|
|
vcodec->height = sps->pic_height_in_luma_samples;
|
|
|
|
|
|
|
|
if (!bs.require_bits(1)) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "conformance_window_flag requires 1 only %d bits", bs.left_bits());
|
|
|
|
}
|
|
|
|
|
|
|
|
// conformance_window_flag u(1)
|
|
|
|
sps->conformance_window_flag = bs.read_bit();
|
|
|
|
if (sps->conformance_window_flag) {
|
|
|
|
// conf_win_left_offset ue(v)
|
|
|
|
if ((err = bs.read_bits_ue(sps->conf_win_left_offset)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "conf_win_left_offset");
|
|
|
|
}
|
|
|
|
// conf_win_right_offset ue(v)
|
|
|
|
if ((err = bs.read_bits_ue(sps->conf_win_right_offset)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "conf_win_right_offset");
|
|
|
|
}
|
|
|
|
// conf_win_top_offset ue(v)
|
|
|
|
if ((err = bs.read_bits_ue(sps->conf_win_top_offset)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "conf_win_top_offset");
|
|
|
|
}
|
|
|
|
// conf_win_bottom_offset ue(v)
|
|
|
|
if ((err = bs.read_bits_ue(sps->conf_win_bottom_offset)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "conf_win_bottom_offset");
|
|
|
|
}
|
2022-12-04 14:46:14 +00:00
|
|
|
|
|
|
|
// Table 6-1, 7.4.3.2.1
|
2023-01-17 10:04:53 +00:00
|
|
|
// ITU-T-H.265-2021.pdf, page 42.
|
2022-12-04 14:46:14 +00:00
|
|
|
// Recalculate width and height
|
|
|
|
// Note: 1 is added to the manual, but it is not actually used
|
2023-01-17 10:04:53 +00:00
|
|
|
// https://gitlab.com/mbunkus/mkvtoolnix/-/issues/1152
|
|
|
|
int sub_width_c = ((1 == sps->chroma_format_idc) || (2 == sps->chroma_format_idc)) && (0 == sps->separate_colour_plane_flag) ? 2 : 1;
|
|
|
|
int sub_height_c = (1 == sps->chroma_format_idc) && (0 == sps->separate_colour_plane_flag) ? 2 : 1;
|
|
|
|
vcodec->width -= (sub_width_c * sps->conf_win_right_offset + sub_width_c * sps->conf_win_left_offset);
|
|
|
|
vcodec->height -= (sub_height_c * sps->conf_win_bottom_offset + sub_height_c * sps->conf_win_top_offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
// bit_depth_luma_minus8 ue(v)
|
|
|
|
if ((err = bs.read_bits_ue(sps->bit_depth_luma_minus8)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "bit_depth_luma_minus8");
|
|
|
|
}
|
|
|
|
// bit_depth_chroma_minus8 ue(v)
|
|
|
|
if ((err = bs.read_bits_ue(sps->bit_depth_chroma_minus8)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "bit_depth_chroma_minus8");
|
|
|
|
}
|
|
|
|
|
|
|
|
// bit depth
|
|
|
|
dec_conf_rec->bit_depth_luma_minus8 = sps->bit_depth_luma_minus8 + 8;
|
|
|
|
dec_conf_rec->bit_depth_chroma_minus8 = sps->bit_depth_chroma_minus8 + 8;
|
|
|
|
|
|
|
|
// log2_max_pic_order_cnt_lsb_minus4 ue(v)
|
|
|
|
if ((err = bs.read_bits_ue(sps->log2_max_pic_order_cnt_lsb_minus4)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "log2_max_pic_order_cnt_lsb_minus4");
|
2022-12-04 14:46:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: FIXME: Implements it, you might parse remain bits for seq_parameter_set_rbsp.
|
2023-01-17 10:04:53 +00:00
|
|
|
// 7.3.2.2 Sequence parameter set RBSP syntax
|
|
|
|
// ITU-T-H.265-2021.pdf, page 55 ~ page 57.
|
2022-12-04 14:46:14 +00:00
|
|
|
|
|
|
|
// 7.3.2.11 RBSP trailing bits syntax
|
2023-01-17 10:04:53 +00:00
|
|
|
// ITU-T-H.265-2021.pdf, page 61.
|
2022-12-04 14:46:14 +00:00
|
|
|
// rbsp_trailing_bits()
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2023-01-17 10:04:53 +00:00
|
|
|
srs_error_t SrsFormat::hevc_demux_pps(SrsBuffer *stream)
|
|
|
|
{
|
|
|
|
// for NALU, ITU-T H.265 7.3.2.3 Picture parameter set RBSP syntax
|
|
|
|
// @see 7.3.2.3 Picture parameter set RBSP syntax
|
|
|
|
// @doc ITU-T-H.265-2021.pdf, page 57.
|
|
|
|
if (!stream->require(1)) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "decode hevc pps requires 1 only %d bytes", stream->left());
|
|
|
|
}
|
|
|
|
int8_t nutv = stream->read_1bytes();
|
|
|
|
|
|
|
|
// forbidden_zero_bit shall be equal to 0.
|
|
|
|
int8_t forbidden_zero_bit = (nutv >> 7) & 0x01;
|
|
|
|
if (forbidden_zero_bit) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "hevc forbidden_zero_bit=%d shall be equal to 0", forbidden_zero_bit);
|
|
|
|
}
|
|
|
|
|
|
|
|
// nal_unit_type specifies the type of RBSP data structure contained in the NAL unit as specified in Table 7-1.
|
|
|
|
// @see 7.4.2.2 NAL unit header semantics
|
|
|
|
// @doc ITU-T-H.265-2021.pdf, page 86.
|
|
|
|
SrsHevcNaluType nal_unit_type = (SrsHevcNaluType)((nutv >> 1) & 0x3f);
|
|
|
|
if (nal_unit_type != SrsHevcNaluType_PPS) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "hevc pps nal_unit_type=%d shall be equal to 33", nal_unit_type);
|
|
|
|
}
|
|
|
|
|
|
|
|
// nuh_layer_id + nuh_temporal_id_plus1
|
|
|
|
stream->skip(1);
|
|
|
|
|
|
|
|
// decode the rbsp from sps.
|
|
|
|
// rbsp[ i ] a raw byte sequence payload is specified as an ordered sequence of bytes.
|
|
|
|
std::vector<int8_t> rbsp(stream->size());
|
|
|
|
|
|
|
|
int nb_rbsp = 0;
|
|
|
|
while (!stream->empty()) {
|
|
|
|
rbsp[nb_rbsp] = stream->read_1bytes();
|
|
|
|
|
|
|
|
// XX 00 00 03 XX, the 03 byte should be drop.
|
|
|
|
if (nb_rbsp > 2 && rbsp[nb_rbsp - 2] == 0 && rbsp[nb_rbsp - 1] == 0 && rbsp[nb_rbsp] == 3) {
|
|
|
|
// read 1byte more.
|
|
|
|
if (stream->empty()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rbsp[nb_rbsp] = stream->read_1bytes();
|
|
|
|
nb_rbsp++;
|
|
|
|
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nb_rbsp++;
|
|
|
|
}
|
|
|
|
|
|
|
|
return hevc_demux_pps_rbsp((char*)&rbsp[0], nb_rbsp);
|
|
|
|
}
|
|
|
|
|
|
|
|
srs_error_t SrsFormat::hevc_demux_pps_rbsp(char* rbsp, int nb_rbsp)
|
|
|
|
{
|
|
|
|
srs_error_t err = srs_success;
|
|
|
|
|
|
|
|
// reparse the rbsp.
|
|
|
|
SrsBuffer stream(rbsp, nb_rbsp);
|
|
|
|
|
|
|
|
// H265 PPS NAL Unit (pic_parameter_set_rbsp()) parser.
|
|
|
|
// Section 7.3.2.3 ("Picture parameter set RBSP syntax") of the H.265
|
|
|
|
// ITU-T-H.265-2021.pdf, page 57.
|
|
|
|
SrsBitBuffer bs(&stream);
|
|
|
|
|
|
|
|
// pps_pic_parameter_set_id ue(v)
|
|
|
|
uint32_t pps_pic_parameter_set_id = 0;
|
|
|
|
if ((err = bs.read_bits_ue(pps_pic_parameter_set_id)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "pps_pic_parameter_set_id");
|
|
|
|
}
|
2023-01-30 03:20:47 +00:00
|
|
|
if (pps_pic_parameter_set_id < 0 || pps_pic_parameter_set_id >= SrsHevcMax_PPS_COUNT) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "pps id out of range: %d", pps_pic_parameter_set_id);
|
2023-01-17 10:04:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// select table
|
|
|
|
SrsHevcDecoderConfigurationRecord *dec_conf_rec = &(vcodec->hevc_dec_conf_record_);
|
|
|
|
SrsHevcRbspPps *pps = &(dec_conf_rec->pps_table[pps_pic_parameter_set_id]);
|
|
|
|
pps->pps_pic_parameter_set_id = pps_pic_parameter_set_id;
|
|
|
|
|
|
|
|
// pps_seq_parameter_set_id ue(v)
|
|
|
|
uint32_t pps_seq_parameter_set_id = 0;
|
|
|
|
if ((err = bs.read_bits_ue(pps_seq_parameter_set_id)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "pps_seq_parameter_set_id");
|
|
|
|
}
|
|
|
|
pps->pps_seq_parameter_set_id = pps_seq_parameter_set_id;
|
|
|
|
|
|
|
|
if (!bs.require_bits(7)) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "pps slice requires 7 only %d bits", bs.left_bits());
|
|
|
|
}
|
|
|
|
|
|
|
|
// dependent_slice_segments_enabled_flag u(1)
|
|
|
|
pps->dependent_slice_segments_enabled_flag = bs.read_bit();
|
|
|
|
// output_flag_present_flag u(1)
|
|
|
|
pps->output_flag_present_flag = bs.read_bit();
|
|
|
|
// num_extra_slice_header_bits u(3)
|
|
|
|
pps->num_extra_slice_header_bits = bs.read_bits(3);
|
|
|
|
// sign_data_hiding_enabled_flag u(1)
|
|
|
|
pps->sign_data_hiding_enabled_flag = bs.read_bit();
|
|
|
|
// cabac_init_present_flag u(1)
|
|
|
|
pps->cabac_init_present_flag = bs.read_bit();
|
|
|
|
|
|
|
|
// num_ref_idx_l0_default_active_minus1 ue(v)
|
|
|
|
if ((err = bs.read_bits_ue(pps->num_ref_idx_l0_default_active_minus1)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "num_ref_idx_l0_default_active_minus1");
|
|
|
|
}
|
|
|
|
// num_ref_idx_l1_default_active_minus1 ue(v)
|
|
|
|
if ((err = bs.read_bits_ue(pps->num_ref_idx_l1_default_active_minus1)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "num_ref_idx_l1_default_active_minus1");
|
|
|
|
}
|
|
|
|
// init_qp_minus26 se(v)
|
|
|
|
if ((err = bs.read_bits_se(pps->init_qp_minus26)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "init_qp_minus26");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bs.require_bits(3)) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "pps requires 3 only %d bits", bs.left_bits());
|
|
|
|
}
|
|
|
|
|
|
|
|
// constrained_intra_pred_flag u(1)
|
|
|
|
pps->constrained_intra_pred_flag = bs.read_bit();
|
|
|
|
// transform_skip_enabled_flag u(1)
|
|
|
|
pps->transform_skip_enabled_flag = bs.read_bit();
|
|
|
|
// cu_qp_delta_enabled_flag u(1)
|
|
|
|
pps->cu_qp_delta_enabled_flag = bs.read_bit();
|
|
|
|
if (pps->cu_qp_delta_enabled_flag) {
|
|
|
|
// diff_cu_qp_delta_depth ue(v)
|
|
|
|
if ((err = bs.read_bits_ue(pps->diff_cu_qp_delta_depth)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "diff_cu_qp_delta_depth");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// pps_cb_qp_offset se(v)
|
|
|
|
if ((err = bs.read_bits_se(pps->pps_cb_qp_offset)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "pps_cb_qp_offset");
|
|
|
|
}
|
|
|
|
// pps_cr_qp_offset se(v)
|
|
|
|
if ((err = bs.read_bits_se(pps->pps_cr_qp_offset)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "pps_cr_qp_offset");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bs.require_bits(6)) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "pps slice_chroma_qp requires 6 only %d bits", bs.left_bits());
|
|
|
|
}
|
|
|
|
|
|
|
|
// pps_slice_chroma_qp_offsets_present_flag u(1)
|
|
|
|
pps->pps_slice_chroma_qp_offsets_present_flag = bs.read_bit();
|
|
|
|
// weighted_pred_flag u(1)
|
|
|
|
pps->weighted_pred_flag = bs.read_bit();
|
|
|
|
// weighted_bipred_flag u(1)
|
|
|
|
pps->weighted_bipred_flag = bs.read_bit();
|
|
|
|
// transquant_bypass_enabled_flag u(1)
|
|
|
|
pps->transquant_bypass_enabled_flag = bs.read_bit();
|
|
|
|
// tiles_enabled_flag u(1)
|
|
|
|
pps->tiles_enabled_flag = bs.read_bit();
|
|
|
|
// entropy_coding_sync_enabled_flag u(1)
|
|
|
|
pps->entropy_coding_sync_enabled_flag = bs.read_bit();
|
|
|
|
|
|
|
|
if (pps->tiles_enabled_flag) {
|
|
|
|
// num_tile_columns_minus1 ue(v)
|
|
|
|
if ((err = bs.read_bits_ue(pps->num_tile_columns_minus1)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "num_tile_columns_minus1");
|
|
|
|
}
|
|
|
|
// num_tile_rows_minus1 ue(v)
|
|
|
|
if ((err = bs.read_bits_ue(pps->num_tile_rows_minus1)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "num_tile_rows_minus1");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bs.require_bits(1)) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "uniform_spacing_flag requires 1 only %d bits", bs.left_bits());
|
|
|
|
}
|
|
|
|
|
|
|
|
// uniform_spacing_flag u(1)
|
|
|
|
pps->uniform_spacing_flag = bs.read_bit();
|
|
|
|
if (!pps->uniform_spacing_flag) {
|
|
|
|
pps->column_width_minus1.resize(pps->num_tile_columns_minus1);
|
|
|
|
pps->row_height_minus1.resize(pps->num_tile_rows_minus1);
|
|
|
|
|
|
|
|
for (int i = 0; i < (int)pps->num_tile_columns_minus1; i++) {
|
|
|
|
// column_width_minus1[i] ue(v)
|
|
|
|
if ((err = bs.read_bits_ue(pps->column_width_minus1[i])) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "column_width_minus1");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < (int)pps->num_tile_rows_minus1; i++) {
|
|
|
|
// row_height_minus1[i] ue(v)
|
|
|
|
if ((err = bs.read_bits_ue(pps->row_height_minus1[i])) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "row_height_minus1");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bs.require_bits(1)) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "loop_filter_across_tiles_enabled_flag requires 1 only %d bits", bs.left_bits());
|
|
|
|
}
|
|
|
|
|
|
|
|
// loop_filter_across_tiles_enabled_flag u(1)
|
|
|
|
pps->loop_filter_across_tiles_enabled_flag = bs.read_bit();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bs.require_bits(2)) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "pps loop deblocking filter requires 2 only %d bits", bs.left_bits());
|
|
|
|
}
|
|
|
|
|
|
|
|
// pps_loop_filter_across_slices_enabled_flag u(1)
|
|
|
|
pps->pps_loop_filter_across_slices_enabled_flag = bs.read_bit();
|
|
|
|
// deblocking_filter_control_present_flag u(1)
|
|
|
|
pps->deblocking_filter_control_present_flag = bs.read_bit();
|
|
|
|
if (pps->deblocking_filter_control_present_flag) {
|
|
|
|
if (!bs.require_bits(2)) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "pps loop deblocking filter flag requires 2 only %d bits", bs.left_bits());
|
|
|
|
}
|
|
|
|
|
|
|
|
// deblocking_filter_override_enabled_flag u(1)
|
|
|
|
pps->deblocking_filter_override_enabled_flag = bs.read_bit();
|
|
|
|
// pps_deblocking_filter_disabled_flag u(1)
|
|
|
|
pps->pps_deblocking_filter_disabled_flag = bs.read_bit();
|
2023-02-08 12:23:25 +00:00
|
|
|
if (!pps->pps_deblocking_filter_disabled_flag) {
|
2023-01-17 10:04:53 +00:00
|
|
|
// pps_beta_offset_div2 se(v)
|
|
|
|
if ((err = bs.read_bits_se(pps->pps_beta_offset_div2)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "pps_beta_offset_div2");
|
|
|
|
}
|
|
|
|
// pps_tc_offset_div2 se(v)
|
|
|
|
if ((err = bs.read_bits_se(pps->pps_tc_offset_div2)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "pps_tc_offset_div2");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bs.require_bits(1)) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "pps scaling_list_data requires 1 only %d bits", bs.left_bits());
|
|
|
|
}
|
|
|
|
|
|
|
|
// pps_scaling_list_data_present_flag u(1)
|
|
|
|
pps->pps_scaling_list_data_present_flag = bs.read_bit();
|
|
|
|
if (pps->pps_scaling_list_data_present_flag) {
|
|
|
|
// 7.3.4 Scaling list data syntax
|
|
|
|
SrsHevcScalingListData* sld = &pps->scaling_list_data;
|
|
|
|
for (int sizeId = 0; sizeId < 4; sizeId++) {
|
|
|
|
for (int matrixId = 0; matrixId < 6; matrixId += (sizeId == 3) ? 3 : 1) {
|
|
|
|
// scaling_list_pred_mode_flag u(1)
|
|
|
|
sld->scaling_list_pred_mode_flag[sizeId][matrixId] = bs.read_bit();
|
|
|
|
if (!sld->scaling_list_pred_mode_flag[sizeId][matrixId]) {
|
|
|
|
// scaling_list_pred_matrix_id_delta ue(v)
|
|
|
|
if ((err = bs.read_bits_ue(sld->scaling_list_pred_matrix_id_delta[sizeId][matrixId])) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "scaling_list_pred_matrix_id_delta");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int nextCoef = 8;
|
|
|
|
int coefNum = srs_min(64, (1 << (4 + (sizeId << 1))));
|
|
|
|
sld->coefNum = coefNum; // tmp store
|
|
|
|
if (sizeId > 1) {
|
|
|
|
// scaling_list_dc_coef_minus8 se(v)
|
|
|
|
if ((err = bs.read_bits_se(sld->scaling_list_dc_coef_minus8[sizeId - 2][matrixId])) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "scaling_list_dc_coef_minus8");
|
|
|
|
}
|
|
|
|
nextCoef = sld->scaling_list_dc_coef_minus8[sizeId - 2][matrixId] + 8;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = 0; i < sld->coefNum; i++) {
|
|
|
|
// scaling_list_delta_coef se(v)
|
|
|
|
int scaling_list_delta_coef = 0;
|
|
|
|
if ((err = bs.read_bits_se(scaling_list_delta_coef)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "scaling_list_delta_coef");
|
|
|
|
}
|
|
|
|
nextCoef = (nextCoef + scaling_list_delta_coef + 256) % 256;
|
|
|
|
sld->ScalingList[sizeId][matrixId][i] = nextCoef;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bs.require_bits(1)) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "lists_modification_present_flag requires 1 only %d bits", bs.left_bits());
|
|
|
|
}
|
|
|
|
// lists_modification_present_flag u(1)
|
|
|
|
pps->lists_modification_present_flag = bs.read_bit();
|
|
|
|
|
|
|
|
// log2_parallel_merge_level_minus2 ue(v)
|
|
|
|
if ((err = bs.read_bits_ue(pps->log2_parallel_merge_level_minus2)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "log2_parallel_merge_level_minus2");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bs.require_bits(2)) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "extension_present_flag requires 2 only %d bits", bs.left_bits());
|
|
|
|
}
|
|
|
|
|
|
|
|
// slice_segment_header_extension_present_flag u(1)
|
|
|
|
pps->slice_segment_header_extension_present_flag = bs.read_bit();
|
|
|
|
// pps_extension_present_flag u(1)
|
|
|
|
pps->pps_extension_present_flag = bs.read_bit();
|
|
|
|
if (pps->pps_extension_present_flag) {
|
|
|
|
if (!bs.require_bits(8)) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "pps_range_extension_flag requires 8 only %d bits", bs.left_bits());
|
|
|
|
}
|
|
|
|
|
|
|
|
// pps_range_extension_flag u(1)
|
|
|
|
pps->pps_range_extension_flag = bs.read_bit();
|
|
|
|
// pps_multilayer_extension_flag u(1)
|
|
|
|
pps->pps_multilayer_extension_flag = bs.read_bit();
|
|
|
|
// pps_3d_extension_flag u(1)
|
|
|
|
pps->pps_3d_extension_flag = bs.read_bit();
|
|
|
|
// pps_scc_extension_flag u(1)
|
|
|
|
pps->pps_scc_extension_flag = bs.read_bit();
|
|
|
|
// pps_extension_4bits u(4)
|
|
|
|
pps->pps_extension_4bits = bs.read_bits(4);
|
|
|
|
}
|
|
|
|
|
2023-02-08 12:23:25 +00:00
|
|
|
// TODO: FIXME: Implements it, you might parse remain bits for pic_parameter_set_rbsp.
|
|
|
|
// @see 7.3.2.3 Picture parameter set RBSP syntax
|
|
|
|
// @doc ITU-T-H.265-2021.pdf, page 59.
|
2023-01-17 10:04:53 +00:00
|
|
|
|
|
|
|
// TODO: FIXME: rbsp_trailing_bits
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
srs_error_t SrsFormat::hevc_demux_rbsp_ptl(SrsBitBuffer* bs, SrsHevcProfileTierLevel* ptl, int profile_present_flag, int max_sub_layers_minus1)
|
2022-12-04 14:46:14 +00:00
|
|
|
{
|
|
|
|
srs_error_t err = srs_success;
|
|
|
|
|
2023-01-17 10:04:53 +00:00
|
|
|
// profile_tier_level() parser.
|
|
|
|
// Section 7.3.3 ("Profile, tier and level syntax") of the H.265
|
|
|
|
// ITU-T-H.265-2021.pdf, page 62.
|
|
|
|
if (profile_present_flag) {
|
2022-12-04 14:46:14 +00:00
|
|
|
if (!bs->require_bits(88)) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "ptl profile requires 88 only %d bits", bs->left_bits());
|
|
|
|
}
|
|
|
|
|
2023-01-17 10:04:53 +00:00
|
|
|
// profile_space u(2)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->general_profile_space = bs->read_bits(2);
|
2023-01-17 10:04:53 +00:00
|
|
|
// tier_flag u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->general_tier_flag = bs->read_bit();
|
2023-01-17 10:04:53 +00:00
|
|
|
// profile_idc u(5)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->general_profile_idc = bs->read_bits(5);
|
|
|
|
for (int i = 0; i < 32; i++) {
|
2023-01-17 10:04:53 +00:00
|
|
|
// profile_compatibility_flag[j] u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->general_profile_compatibility_flag[i] = bs->read_bit();
|
|
|
|
}
|
2023-01-17 10:04:53 +00:00
|
|
|
// progressive_source_flag u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->general_progressive_source_flag = bs->read_bit();
|
2023-01-17 10:04:53 +00:00
|
|
|
// interlaced_source_flag u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->general_interlaced_source_flag = bs->read_bit();
|
2023-01-17 10:04:53 +00:00
|
|
|
// non_packed_constraint_flag u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->general_non_packed_constraint_flag = bs->read_bit();
|
2023-01-17 10:04:53 +00:00
|
|
|
// frame_only_constraint_flag u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->general_frame_only_constraint_flag = bs->read_bit();
|
|
|
|
if (ptl->general_profile_idc == 4 || ptl->general_profile_compatibility_flag[4] ||
|
|
|
|
ptl->general_profile_idc == 5 || ptl->general_profile_compatibility_flag[5] ||
|
|
|
|
ptl->general_profile_idc == 6 || ptl->general_profile_compatibility_flag[6] ||
|
2023-01-17 10:04:53 +00:00
|
|
|
ptl->general_profile_idc == 7 || ptl->general_profile_compatibility_flag[7] ||
|
|
|
|
ptl->general_profile_idc == 8 || ptl->general_profile_compatibility_flag[8] ||
|
|
|
|
ptl->general_profile_idc == 9 || ptl->general_profile_compatibility_flag[9] ||
|
|
|
|
ptl->general_profile_idc == 10 || ptl->general_profile_compatibility_flag[10] ||
|
|
|
|
ptl->general_profile_idc == 11 || ptl->general_profile_compatibility_flag[11])
|
2022-12-04 14:46:14 +00:00
|
|
|
{
|
2023-01-17 10:04:53 +00:00
|
|
|
// The number of bits in this syntax structure is not affected by this condition
|
|
|
|
// max_12bit_constraint_flag u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->general_max_12bit_constraint_flag = bs->read_bit();
|
2023-01-17 10:04:53 +00:00
|
|
|
// max_10bit_constraint_flag u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->general_max_10bit_constraint_flag = bs->read_bit();
|
2023-01-17 10:04:53 +00:00
|
|
|
// max_8bit_constraint_flag u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->general_max_8bit_constraint_flag = bs->read_bit();
|
2023-01-17 10:04:53 +00:00
|
|
|
// max_422chroma_constraint_flag u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->general_max_422chroma_constraint_flag = bs->read_bit();
|
2023-01-17 10:04:53 +00:00
|
|
|
// max_420chroma_constraint_flag u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->general_max_420chroma_constraint_flag = bs->read_bit();
|
2023-01-17 10:04:53 +00:00
|
|
|
// max_monochrome_constraint_flag u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->general_max_monochrome_constraint_flag = bs->read_bit();
|
2023-01-17 10:04:53 +00:00
|
|
|
// intra_constraint_flag u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->general_intra_constraint_flag = bs->read_bit();
|
2023-01-17 10:04:53 +00:00
|
|
|
// one_picture_only_constraint_flag u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->general_one_picture_only_constraint_flag = bs->read_bit();
|
2023-01-17 10:04:53 +00:00
|
|
|
// lower_bit_rate_constraint_flag u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->general_lower_bit_rate_constraint_flag = bs->read_bit();
|
2023-01-17 10:04:53 +00:00
|
|
|
|
|
|
|
if (ptl->general_profile_idc == 5 || ptl->general_profile_compatibility_flag[5] == 1 ||
|
|
|
|
ptl->general_profile_idc == 9 || ptl->general_profile_compatibility_flag[9] == 1 ||
|
|
|
|
ptl->general_profile_idc == 10 || ptl->general_profile_compatibility_flag[10] == 1 ||
|
|
|
|
ptl->general_profile_idc == 11 || ptl->general_profile_compatibility_flag[11] == 1)
|
|
|
|
{
|
|
|
|
// max_14bit_constraint_flag u(1)
|
|
|
|
ptl->general_max_14bit_constraint_flag = bs->read_bit();
|
|
|
|
// reserved_zero_33bits u(33)
|
|
|
|
uint32_t bits_tmp_hi = bs->read_bit();
|
|
|
|
uint32_t bits_tmp = bs->read_bits(32);
|
|
|
|
ptl->general_reserved_zero_33bits = ((uint64_t)bits_tmp_hi << 32) | bits_tmp;
|
|
|
|
} else {
|
|
|
|
// reserved_zero_34bits u(34)
|
|
|
|
uint32_t bits_tmp_hi = bs->read_bits(2);
|
|
|
|
uint32_t bits_tmp = bs->read_bits(32);
|
|
|
|
ptl->general_reserved_zero_34bits = ((uint64_t)bits_tmp_hi << 32) | bits_tmp;
|
|
|
|
}
|
|
|
|
} else if (ptl->general_profile_idc == 2 || ptl->general_profile_compatibility_flag[2]) {
|
|
|
|
// general_reserved_zero_7bits u(7)
|
|
|
|
ptl->general_reserved_zero_7bits = bs->read_bits(7);
|
|
|
|
// general_one_picture_only_constraint_flag u(1)
|
|
|
|
ptl->general_one_picture_only_constraint_flag = bs->read_bit();
|
|
|
|
// general_reserved_zero_35bits u(35)
|
|
|
|
uint32_t bits_tmp_hi = bs->read_bits(3);
|
|
|
|
uint32_t bits_tmp = bs->read_bits(32);
|
|
|
|
ptl->general_reserved_zero_35bits = ((uint64_t)bits_tmp_hi << 32) | bits_tmp;
|
2022-12-04 14:46:14 +00:00
|
|
|
} else {
|
2023-01-17 10:04:53 +00:00
|
|
|
// reserved_zero_43bits u(43)
|
|
|
|
uint32_t bits_tmp_hi = bs->read_bits(11);
|
|
|
|
uint32_t bits_tmp = bs->read_bits(32);
|
|
|
|
ptl->general_reserved_zero_43bits = ((uint64_t)bits_tmp_hi << 32) | bits_tmp;
|
2022-12-04 14:46:14 +00:00
|
|
|
}
|
|
|
|
|
2023-01-17 10:04:53 +00:00
|
|
|
// The number of bits in this syntax structure is not affected by this condition
|
|
|
|
if (ptl->general_profile_idc == 1 || ptl->general_profile_compatibility_flag[1] ||
|
|
|
|
ptl->general_profile_idc == 2 || ptl->general_profile_compatibility_flag[2] ||
|
|
|
|
ptl->general_profile_idc == 3 || ptl->general_profile_compatibility_flag[3] ||
|
|
|
|
ptl->general_profile_idc == 4 || ptl->general_profile_compatibility_flag[4] ||
|
|
|
|
ptl->general_profile_idc == 5 || ptl->general_profile_compatibility_flag[5] ||
|
|
|
|
ptl->general_profile_idc == 9 || ptl->general_profile_compatibility_flag[9] ||
|
|
|
|
ptl->general_profile_idc == 11 || ptl->general_profile_compatibility_flag[11]) {
|
|
|
|
// inbld_flag u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->general_inbld_flag = bs->read_bit();
|
|
|
|
} else {
|
2023-01-17 10:04:53 +00:00
|
|
|
// reserved_zero_bit u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->general_reserved_zero_bit = bs->read_bit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!bs->require_bits(8)) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "ptl level requires 8 only %d bits", bs->left_bits());
|
|
|
|
}
|
|
|
|
|
2023-01-17 10:04:53 +00:00
|
|
|
// general_level_idc u(8)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->general_level_idc = bs->read_8bits();
|
|
|
|
|
|
|
|
ptl->sub_layer_profile_present_flag.resize(max_sub_layers_minus1);
|
|
|
|
ptl->sub_layer_level_present_flag.resize(max_sub_layers_minus1);
|
|
|
|
for (int i = 0; i < max_sub_layers_minus1; i++) {
|
|
|
|
if (!bs->require_bits(2)) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "ptl present_flag requires 2 only %d bits", bs->left_bits());
|
|
|
|
}
|
2023-01-17 10:04:53 +00:00
|
|
|
// sub_layer_profile_present_flag[i] u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->sub_layer_profile_present_flag[i] = bs->read_bit();
|
2023-01-17 10:04:53 +00:00
|
|
|
// sub_layer_level_present_flag[i] u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->sub_layer_level_present_flag[i] = bs->read_bit();
|
|
|
|
}
|
|
|
|
|
|
|
|
for (int i = max_sub_layers_minus1; max_sub_layers_minus1 > 0 && i < 8; i++) {
|
|
|
|
if (!bs->require_bits(2)) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "ptl reserved_zero requires 2 only %d bits", bs->left_bits());
|
|
|
|
}
|
2023-01-17 10:04:53 +00:00
|
|
|
// reserved_zero_2bits[i] u(2)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->reserved_zero_2bits[i] = bs->read_bits(2);
|
|
|
|
}
|
|
|
|
|
|
|
|
ptl->sub_layer_profile_space.resize(max_sub_layers_minus1);
|
|
|
|
ptl->sub_layer_tier_flag.resize(max_sub_layers_minus1);
|
|
|
|
ptl->sub_layer_profile_idc.resize(max_sub_layers_minus1);
|
|
|
|
ptl->sub_layer_profile_compatibility_flag.resize(max_sub_layers_minus1);
|
|
|
|
for (int i = 0; i < max_sub_layers_minus1; i++) {
|
|
|
|
ptl->sub_layer_profile_compatibility_flag[i].resize(32);
|
|
|
|
}
|
|
|
|
ptl->sub_layer_progressive_source_flag.resize(max_sub_layers_minus1);
|
|
|
|
ptl->sub_layer_interlaced_source_flag.resize(max_sub_layers_minus1);
|
|
|
|
ptl->sub_layer_non_packed_constraint_flag.resize(max_sub_layers_minus1);
|
|
|
|
ptl->sub_layer_frame_only_constraint_flag.resize(max_sub_layers_minus1);
|
|
|
|
ptl->sub_layer_max_12bit_constraint_flag.resize(max_sub_layers_minus1);
|
|
|
|
ptl->sub_layer_max_10bit_constraint_flag.resize(max_sub_layers_minus1);
|
|
|
|
ptl->sub_layer_max_8bit_constraint_flag.resize(max_sub_layers_minus1);
|
|
|
|
ptl->sub_layer_max_422chroma_constraint_flag.resize(max_sub_layers_minus1);
|
|
|
|
ptl->sub_layer_max_420chroma_constraint_flag.resize(max_sub_layers_minus1);
|
|
|
|
ptl->sub_layer_max_monochrome_constraint_flag.resize(max_sub_layers_minus1);
|
|
|
|
ptl->sub_layer_intra_constraint_flag.resize(max_sub_layers_minus1);
|
|
|
|
ptl->sub_layer_one_picture_only_constraint_flag.resize(max_sub_layers_minus1);
|
|
|
|
ptl->sub_layer_lower_bit_rate_constraint_flag.resize(max_sub_layers_minus1);
|
|
|
|
ptl->sub_layer_reserved_zero_34bits.resize(max_sub_layers_minus1);
|
|
|
|
ptl->sub_layer_reserved_zero_43bits.resize(max_sub_layers_minus1);
|
|
|
|
ptl->sub_layer_inbld_flag.resize(max_sub_layers_minus1);
|
|
|
|
ptl->sub_layer_reserved_zero_bit.resize(max_sub_layers_minus1);
|
|
|
|
ptl->sub_layer_level_idc.resize(max_sub_layers_minus1);
|
|
|
|
for (int i = 0; i < max_sub_layers_minus1; i++) {
|
|
|
|
if (ptl->sub_layer_profile_present_flag[i]) {
|
|
|
|
if (!bs->require_bits(88)) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "ptl sub_layer_profile requires 88 only %d bits", bs->left_bits());
|
|
|
|
}
|
2023-01-17 10:04:53 +00:00
|
|
|
// profile_space u(2)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->sub_layer_profile_space[i] = bs->read_bits(2);
|
2023-01-17 10:04:53 +00:00
|
|
|
// tier_flag u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->sub_layer_tier_flag[i] = bs->read_bit();
|
2023-01-17 10:04:53 +00:00
|
|
|
// profile_idc u(5)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->sub_layer_profile_idc[i] = bs->read_bits(5);
|
|
|
|
for (int j = 0; j < 32; j++) {
|
2023-01-17 10:04:53 +00:00
|
|
|
// profile_compatibility_flag[j] u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->sub_layer_profile_compatibility_flag[i][j] = bs->read_bit();
|
|
|
|
}
|
2023-01-17 10:04:53 +00:00
|
|
|
// progressive_source_flag u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->sub_layer_progressive_source_flag[i] = bs->read_bit();
|
2023-01-17 10:04:53 +00:00
|
|
|
// interlaced_source_flag u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->sub_layer_interlaced_source_flag[i] = bs->read_bit();
|
2023-01-17 10:04:53 +00:00
|
|
|
// non_packed_constraint_flag u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->sub_layer_non_packed_constraint_flag[i] = bs->read_bit();
|
2023-01-17 10:04:53 +00:00
|
|
|
// frame_only_constraint_flag u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->sub_layer_frame_only_constraint_flag[i] = bs->read_bit();
|
|
|
|
if (ptl->sub_layer_profile_idc[i] == 4 || ptl->sub_layer_profile_compatibility_flag[i][4] ||
|
|
|
|
ptl->sub_layer_profile_idc[i] == 5 || ptl->sub_layer_profile_compatibility_flag[i][5] ||
|
|
|
|
ptl->sub_layer_profile_idc[i] == 6 || ptl->sub_layer_profile_compatibility_flag[i][6] ||
|
2023-01-17 10:04:53 +00:00
|
|
|
ptl->sub_layer_profile_idc[i] == 7 || ptl->sub_layer_profile_compatibility_flag[i][7] ||
|
|
|
|
ptl->sub_layer_profile_idc[i] == 8 || ptl->sub_layer_profile_compatibility_flag[i][8] ||
|
|
|
|
ptl->sub_layer_profile_idc[i] == 9 || ptl->sub_layer_profile_compatibility_flag[i][9] ||
|
|
|
|
ptl->sub_layer_profile_idc[i] == 10 || ptl->sub_layer_profile_compatibility_flag[i][10] ||
|
|
|
|
ptl->sub_layer_profile_idc[i] == 11 || ptl->sub_layer_profile_compatibility_flag[i][11])
|
2022-12-04 14:46:14 +00:00
|
|
|
{
|
2023-01-17 10:04:53 +00:00
|
|
|
// The number of bits in this syntax structure is not affected by this condition.
|
|
|
|
// max_12bit_constraint_flag u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->sub_layer_max_12bit_constraint_flag[i] = bs->read_bit();
|
2023-01-17 10:04:53 +00:00
|
|
|
// max_10bit_constraint_flag u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->sub_layer_max_10bit_constraint_flag[i] = bs->read_bit();
|
2023-01-17 10:04:53 +00:00
|
|
|
// max_8bit_constraint_flag u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->sub_layer_max_8bit_constraint_flag[i] = bs->read_bit();
|
2023-01-17 10:04:53 +00:00
|
|
|
// max_422chroma_constraint_flag u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->sub_layer_max_422chroma_constraint_flag[i] = bs->read_bit();
|
2023-01-17 10:04:53 +00:00
|
|
|
// max_420chroma_constraint_flag u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->sub_layer_max_420chroma_constraint_flag[i] = bs->read_bit();
|
2023-01-17 10:04:53 +00:00
|
|
|
// max_monochrome_constraint_flag u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->sub_layer_max_monochrome_constraint_flag[i] = bs->read_bit();
|
2023-01-17 10:04:53 +00:00
|
|
|
// intra_constraint_flag u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->sub_layer_intra_constraint_flag[i] = bs->read_bit();
|
2023-01-17 10:04:53 +00:00
|
|
|
// one_picture_only_constraint_flag u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->sub_layer_one_picture_only_constraint_flag[i] = bs->read_bit();
|
2023-01-17 10:04:53 +00:00
|
|
|
// lower_bit_rate_constraint_flag u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->sub_layer_lower_bit_rate_constraint_flag[i] = bs->read_bit();
|
2023-01-17 10:04:53 +00:00
|
|
|
|
|
|
|
if (ptl->sub_layer_profile_idc[i] == 5 ||
|
|
|
|
ptl->sub_layer_profile_compatibility_flag[i][5] == 1 ||
|
|
|
|
ptl->sub_layer_profile_idc[i] == 9 ||
|
|
|
|
ptl->sub_layer_profile_compatibility_flag[i][9] == 1 ||
|
|
|
|
ptl->sub_layer_profile_idc[i] == 10 ||
|
|
|
|
ptl->sub_layer_profile_compatibility_flag[i][10] == 1 ||
|
|
|
|
ptl->sub_layer_profile_idc[i] == 11 ||
|
|
|
|
ptl->sub_layer_profile_compatibility_flag[i][11] == 1)
|
|
|
|
{
|
|
|
|
// max_14bit_constraint_flag u(1)
|
|
|
|
ptl->general_max_14bit_constraint_flag = bs->read_bit();
|
|
|
|
// reserved_zero_33bits u(33)
|
|
|
|
uint32_t bits_tmp_hi = bs->read_bit();
|
|
|
|
uint32_t bits_tmp = bs->read_bits(32);
|
|
|
|
ptl->sub_layer_reserved_zero_33bits[i] = ((uint64_t)bits_tmp_hi << 32) | bits_tmp;
|
|
|
|
} else {
|
|
|
|
// reserved_zero_34bits u(34)
|
|
|
|
uint32_t bits_tmp_hi = bs->read_bits(2);
|
|
|
|
uint32_t bits_tmp = bs->read_bits(32);
|
|
|
|
ptl->sub_layer_reserved_zero_34bits[i] = ((uint64_t)bits_tmp_hi << 32) | bits_tmp;
|
|
|
|
}
|
|
|
|
} else if (ptl->sub_layer_profile_idc[i] == 2 || ptl->sub_layer_profile_compatibility_flag[i][2]) {
|
|
|
|
// sub_layer_reserved_zero_7bits u(7)
|
|
|
|
ptl->sub_layer_reserved_zero_7bits[i] = bs->read_bits(7);
|
|
|
|
// sub_layer_one_picture_only_constraint_flag u(1)
|
|
|
|
ptl->sub_layer_one_picture_only_constraint_flag[i] = bs->read_bit();
|
|
|
|
// sub_layer_reserved_zero_35bits u(35)
|
|
|
|
uint32_t bits_tmp_hi = bs->read_bits(3);
|
|
|
|
uint32_t bits_tmp = bs->read_bits(32);
|
|
|
|
ptl->sub_layer_reserved_zero_35bits[i] = ((uint64_t)bits_tmp_hi << 32) | bits_tmp;
|
2022-12-04 14:46:14 +00:00
|
|
|
} else {
|
2023-01-17 10:04:53 +00:00
|
|
|
// reserved_zero_43bits u(43)
|
|
|
|
uint32_t bits_tmp_hi = bs->read_bits(11);
|
|
|
|
uint32_t bits_tmp = bs->read_bits(32);
|
|
|
|
ptl->sub_layer_reserved_zero_43bits[i] = ((uint64_t)bits_tmp_hi << 32) | bits_tmp;
|
2022-12-04 14:46:14 +00:00
|
|
|
}
|
|
|
|
|
2023-01-17 10:04:53 +00:00
|
|
|
// The number of bits in this syntax structure is not affected by this condition
|
|
|
|
if (ptl->sub_layer_profile_idc[i] == 1 || ptl->sub_layer_profile_compatibility_flag[i][1] ||
|
|
|
|
ptl->sub_layer_profile_idc[i] == 2 || ptl->sub_layer_profile_compatibility_flag[i][2] ||
|
|
|
|
ptl->sub_layer_profile_idc[i] == 3 || ptl->sub_layer_profile_compatibility_flag[i][3] ||
|
|
|
|
ptl->sub_layer_profile_idc[i] == 4 || ptl->sub_layer_profile_compatibility_flag[i][4] ||
|
|
|
|
ptl->sub_layer_profile_idc[i] == 5 || ptl->sub_layer_profile_compatibility_flag[i][5] ||
|
|
|
|
ptl->sub_layer_profile_idc[i] == 9 || ptl->sub_layer_profile_compatibility_flag[i][9] ||
|
|
|
|
ptl->sub_layer_profile_idc[i] == 11 || ptl->sub_layer_profile_compatibility_flag[i][11]) {
|
|
|
|
// inbld_flag u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->sub_layer_inbld_flag[i] = bs->read_bit();
|
|
|
|
} else {
|
2023-01-17 10:04:53 +00:00
|
|
|
// reserved_zero_bit u(1)
|
2022-12-04 14:46:14 +00:00
|
|
|
ptl->sub_layer_reserved_zero_bit[i] = bs->read_bit();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ptl->sub_layer_level_present_flag[i]) {
|
|
|
|
if (!bs->require_bits(8)) {
|
|
|
|
return srs_error_new(ERROR_HEVC_DECODE_ERROR, "ptl sub_layer_level requires 8 only %d bits", bs->left_bits());
|
|
|
|
}
|
2023-01-17 10:04:53 +00:00
|
|
|
// sub_layer_level_idc u(8)
|
|
|
|
ptl->sub_layer_level_idc[i] = bs->read_bits(8);
|
2022-12-04 14:46:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2022-11-23 00:34:13 +00:00
|
|
|
#endif
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t SrsFormat::avc_demux_sps_pps(SrsBuffer* stream)
|
2015-01-31 13:16:42 +00:00
|
|
|
{
|
|
|
|
// AVCDecoderConfigurationRecord
|
2017-01-26 09:28:49 +00:00
|
|
|
// 5.2.4.1.1 Syntax, ISO_IEC_14496-15-AVC-format-2012.pdf, page 16
|
2017-04-24 02:08:57 +00:00
|
|
|
int avc_extra_size = stream->size() - stream->pos();
|
|
|
|
if (avc_extra_size > 0) {
|
|
|
|
char *copy_stream_from = stream->data() + stream->pos();
|
|
|
|
vcodec->avc_extra_data = std::vector<char>(copy_stream_from, copy_stream_from + avc_extra_size);
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!stream->require(6)) {
|
2017-09-23 14:12:33 +00:00
|
|
|
return srs_error_new(ERROR_HLS_DECODE_ERROR, "avc decode sequence header");
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
2022-11-23 00:34:13 +00:00
|
|
|
//int8_t configuration_version = stream->read_1bytes();
|
2015-01-31 13:16:42 +00:00
|
|
|
stream->read_1bytes();
|
|
|
|
//int8_t AVCProfileIndication = stream->read_1bytes();
|
2017-02-12 10:18:18 +00:00
|
|
|
vcodec->avc_profile = (SrsAvcProfile)stream->read_1bytes();
|
2015-01-31 13:16:42 +00:00
|
|
|
//int8_t profile_compatibility = stream->read_1bytes();
|
|
|
|
stream->read_1bytes();
|
|
|
|
//int8_t AVCLevelIndication = stream->read_1bytes();
|
2017-02-12 10:18:18 +00:00
|
|
|
vcodec->avc_level = (SrsAvcLevel)stream->read_1bytes();
|
2015-01-31 13:16:42 +00:00
|
|
|
|
|
|
|
// parse the NALU size.
|
|
|
|
int8_t lengthSizeMinusOne = stream->read_1bytes();
|
|
|
|
lengthSizeMinusOne &= 0x03;
|
2017-02-12 10:18:18 +00:00
|
|
|
vcodec->NAL_unit_length = lengthSizeMinusOne;
|
2015-01-31 13:16:42 +00:00
|
|
|
|
2017-01-26 09:28:49 +00:00
|
|
|
// 5.3.4.2.1 Syntax, ISO_IEC_14496-15-AVC-format-2012.pdf, page 16
|
2015-01-31 13:16:42 +00:00
|
|
|
// 5.2.4.1 AVC decoder configuration record
|
|
|
|
// 5.2.4.1.2 Semantics
|
|
|
|
// The value of this field shall be one of 0, 1, or 3 corresponding to a
|
|
|
|
// length encoded with 1, 2, or 4 bytes, respectively.
|
2017-02-12 10:18:18 +00:00
|
|
|
if (vcodec->NAL_unit_length == 2) {
|
2017-09-23 14:12:33 +00:00
|
|
|
return srs_error_new(ERROR_HLS_DECODE_ERROR, "sps lengthSizeMinusOne should never be 2");
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
|
|
|
|
2015-04-03 15:17:50 +00:00
|
|
|
// 1 sps, 7.3.2.1 Sequence parameter set RBSP syntax
|
2017-01-26 09:28:49 +00:00
|
|
|
// ISO_IEC_14496-10-AVC-2003.pdf, page 45.
|
2015-01-31 13:16:42 +00:00
|
|
|
if (!stream->require(1)) {
|
2017-09-23 14:12:33 +00:00
|
|
|
return srs_error_new(ERROR_HLS_DECODE_ERROR, "decode SPS");
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
|
|
|
int8_t numOfSequenceParameterSets = stream->read_1bytes();
|
|
|
|
numOfSequenceParameterSets &= 0x1f;
|
2021-08-26 23:27:33 +00:00
|
|
|
if (numOfSequenceParameterSets < 1) {
|
2017-09-23 14:12:33 +00:00
|
|
|
return srs_error_new(ERROR_HLS_DECODE_ERROR, "decode SPS");
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
2021-08-26 23:27:33 +00:00
|
|
|
// Support for multiple SPS, then pick the first non-empty one.
|
|
|
|
for (int i = 0; i < numOfSequenceParameterSets; ++i) {
|
|
|
|
if (!stream->require(2)) {
|
|
|
|
return srs_error_new(ERROR_HLS_DECODE_ERROR, "decode SPS size");
|
|
|
|
}
|
|
|
|
uint16_t sequenceParameterSetLength = stream->read_2bytes();
|
|
|
|
if (!stream->require(sequenceParameterSetLength)) {
|
|
|
|
return srs_error_new(ERROR_HLS_DECODE_ERROR, "decode SPS data");
|
|
|
|
}
|
|
|
|
if (sequenceParameterSetLength > 0) {
|
|
|
|
vcodec->sequenceParameterSetNALUnit.resize(sequenceParameterSetLength);
|
|
|
|
stream->read_bytes(&vcodec->sequenceParameterSetNALUnit[0], sequenceParameterSetLength);
|
|
|
|
}
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
2021-08-26 23:27:33 +00:00
|
|
|
|
2015-01-31 13:16:42 +00:00
|
|
|
// 1 pps
|
|
|
|
if (!stream->require(1)) {
|
2017-09-23 14:12:33 +00:00
|
|
|
return srs_error_new(ERROR_HLS_DECODE_ERROR, "decode PPS");
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
|
|
|
int8_t numOfPictureParameterSets = stream->read_1bytes();
|
|
|
|
numOfPictureParameterSets &= 0x1f;
|
2021-08-26 23:27:33 +00:00
|
|
|
if (numOfPictureParameterSets < 1) {
|
|
|
|
return srs_error_new(ERROR_HLS_DECODE_ERROR, "decode SPS");
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
2021-08-26 23:27:33 +00:00
|
|
|
// Support for multiple PPS, then pick the first non-empty one.
|
|
|
|
for (int i = 0; i < numOfPictureParameterSets; ++i) {
|
|
|
|
if (!stream->require(2)) {
|
|
|
|
return srs_error_new(ERROR_HLS_DECODE_ERROR, "decode PPS size");
|
|
|
|
}
|
|
|
|
uint16_t pictureParameterSetLength = stream->read_2bytes();
|
|
|
|
if (!stream->require(pictureParameterSetLength)) {
|
|
|
|
return srs_error_new(ERROR_HLS_DECODE_ERROR, "decode PPS data");
|
|
|
|
}
|
|
|
|
if (pictureParameterSetLength > 0) {
|
|
|
|
vcodec->pictureParameterSetNALUnit.resize(pictureParameterSetLength);
|
|
|
|
stream->read_bytes(&vcodec->pictureParameterSetNALUnit[0], pictureParameterSetLength);
|
|
|
|
}
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
2015-04-03 15:17:50 +00:00
|
|
|
return avc_demux_sps();
|
|
|
|
}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t SrsFormat::avc_demux_sps()
|
2015-04-03 15:17:50 +00:00
|
|
|
{
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t err = srs_success;
|
2015-04-03 15:17:50 +00:00
|
|
|
|
2017-04-24 02:08:57 +00:00
|
|
|
if (vcodec->sequenceParameterSetNALUnit.empty()) {
|
2017-09-23 14:12:33 +00:00
|
|
|
return err;
|
2015-04-03 15:17:50 +00:00
|
|
|
}
|
|
|
|
|
2017-06-04 07:10:35 +00:00
|
|
|
char* sps = &vcodec->sequenceParameterSetNALUnit[0];
|
|
|
|
int nbsps = (int)vcodec->sequenceParameterSetNALUnit.size();
|
2017-12-31 04:11:48 +00:00
|
|
|
|
|
|
|
SrsBuffer stream(sps, nbsps);
|
2015-04-03 15:17:50 +00:00
|
|
|
|
|
|
|
// for NALU, 7.3.1 NAL unit syntax
|
2017-01-26 09:28:49 +00:00
|
|
|
// ISO_IEC_14496-10-AVC-2012.pdf, page 61.
|
2015-04-03 15:17:50 +00:00
|
|
|
if (!stream.require(1)) {
|
2017-09-23 14:12:33 +00:00
|
|
|
return srs_error_new(ERROR_HLS_DECODE_ERROR, "decode SPS");
|
2015-04-03 15:17:50 +00:00
|
|
|
}
|
|
|
|
int8_t nutv = stream.read_1bytes();
|
|
|
|
|
|
|
|
// forbidden_zero_bit shall be equal to 0.
|
|
|
|
int8_t forbidden_zero_bit = (nutv >> 7) & 0x01;
|
|
|
|
if (forbidden_zero_bit) {
|
2017-09-23 14:12:33 +00:00
|
|
|
return srs_error_new(ERROR_HLS_DECODE_ERROR, "forbidden_zero_bit shall be equal to 0");
|
2015-04-03 15:17:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// nal_ref_idc not equal to 0 specifies that the content of the NAL unit contains a sequence parameter set or a picture
|
|
|
|
// parameter set or a slice of a reference picture or a slice data partition of a reference picture.
|
|
|
|
int8_t nal_ref_idc = (nutv >> 5) & 0x03;
|
|
|
|
if (!nal_ref_idc) {
|
2017-09-23 14:12:33 +00:00
|
|
|
return srs_error_new(ERROR_HLS_DECODE_ERROR, "for sps, nal_ref_idc shall be not be equal to 0");
|
2015-04-03 15:17:50 +00:00
|
|
|
}
|
|
|
|
|
2015-04-04 05:30:13 +00:00
|
|
|
// 7.4.1 NAL unit semantics
|
2017-01-26 09:28:49 +00:00
|
|
|
// ISO_IEC_14496-10-AVC-2012.pdf, page 61.
|
2015-04-03 15:17:50 +00:00
|
|
|
// nal_unit_type specifies the type of RBSP data structure contained in the NAL unit as specified in Table 7-1.
|
2015-04-04 05:30:13 +00:00
|
|
|
SrsAvcNaluType nal_unit_type = (SrsAvcNaluType)(nutv & 0x1f);
|
2015-04-03 15:17:50 +00:00
|
|
|
if (nal_unit_type != 7) {
|
2017-09-23 14:12:33 +00:00
|
|
|
return srs_error_new(ERROR_HLS_DECODE_ERROR, "for sps, nal_unit_type shall be equal to 7");
|
2015-04-03 15:17:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// decode the rbsp from sps.
|
|
|
|
// rbsp[ i ] a raw byte sequence payload is specified as an ordered sequence of bytes.
|
2017-04-24 02:08:57 +00:00
|
|
|
std::vector<int8_t> rbsp(vcodec->sequenceParameterSetNALUnit.size());
|
2015-04-03 15:17:50 +00:00
|
|
|
|
|
|
|
int nb_rbsp = 0;
|
|
|
|
while (!stream.empty()) {
|
|
|
|
rbsp[nb_rbsp] = stream.read_1bytes();
|
|
|
|
|
|
|
|
// XX 00 00 03 XX, the 03 byte should be drop.
|
|
|
|
if (nb_rbsp > 2 && rbsp[nb_rbsp - 2] == 0 && rbsp[nb_rbsp - 1] == 0 && rbsp[nb_rbsp] == 3) {
|
2015-04-04 10:24:07 +00:00
|
|
|
// read 1byte more.
|
|
|
|
if (stream.empty()) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
rbsp[nb_rbsp] = stream.read_1bytes();
|
|
|
|
nb_rbsp++;
|
|
|
|
|
2015-04-03 15:17:50 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
nb_rbsp++;
|
|
|
|
}
|
|
|
|
|
2017-04-24 02:08:57 +00:00
|
|
|
return avc_demux_sps_rbsp((char*)&rbsp[0], nb_rbsp);
|
2015-04-03 15:17:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t SrsFormat::avc_demux_sps_rbsp(char* rbsp, int nb_rbsp)
|
2015-04-03 15:17:50 +00:00
|
|
|
{
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t err = srs_success;
|
2015-04-03 15:17:50 +00:00
|
|
|
|
2015-09-14 10:36:44 +00:00
|
|
|
// we donot parse the detail of sps.
|
2015-11-11 02:37:50 +00:00
|
|
|
// @see https://github.com/ossrs/srs/issues/474
|
2015-09-14 10:36:44 +00:00
|
|
|
if (!avc_parse_sps) {
|
2017-09-23 14:12:33 +00:00
|
|
|
return err;
|
2015-09-14 10:36:44 +00:00
|
|
|
}
|
|
|
|
|
2015-04-03 15:17:50 +00:00
|
|
|
// reparse the rbsp.
|
2017-12-31 04:11:48 +00:00
|
|
|
SrsBuffer stream(rbsp, nb_rbsp);
|
2015-04-03 15:17:50 +00:00
|
|
|
|
|
|
|
// for SPS, 7.3.2.1.1 Sequence parameter set data syntax
|
2017-01-26 09:28:49 +00:00
|
|
|
// ISO_IEC_14496-10-AVC-2012.pdf, page 62.
|
2015-04-03 15:17:50 +00:00
|
|
|
if (!stream.require(3)) {
|
2017-09-23 14:12:33 +00:00
|
|
|
return srs_error_new(ERROR_HLS_DECODE_ERROR, "sps shall atleast 3bytes");
|
2015-04-03 15:17:50 +00:00
|
|
|
}
|
2017-01-30 09:32:18 +00:00
|
|
|
uint8_t profile_idc = stream.read_1bytes();
|
2015-04-03 15:17:50 +00:00
|
|
|
if (!profile_idc) {
|
2017-09-23 14:12:33 +00:00
|
|
|
return srs_error_new(ERROR_HLS_DECODE_ERROR, "sps the profile_idc invalid");
|
2015-04-03 15:17:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int8_t flags = stream.read_1bytes();
|
|
|
|
if (flags & 0x03) {
|
2017-09-23 14:12:33 +00:00
|
|
|
return srs_error_new(ERROR_HLS_DECODE_ERROR, "sps the flags invalid");
|
2015-04-03 15:17:50 +00:00
|
|
|
}
|
|
|
|
|
2017-01-30 09:32:18 +00:00
|
|
|
uint8_t level_idc = stream.read_1bytes();
|
2015-04-03 15:17:50 +00:00
|
|
|
if (!level_idc) {
|
2017-09-23 14:12:33 +00:00
|
|
|
return srs_error_new(ERROR_HLS_DECODE_ERROR, "sps the level_idc invalid");
|
2015-04-03 15:17:50 +00:00
|
|
|
}
|
|
|
|
|
2019-02-04 07:11:41 +00:00
|
|
|
SrsBitBuffer bs(&stream);
|
2015-04-03 15:17:50 +00:00
|
|
|
|
2015-04-04 07:53:36 +00:00
|
|
|
int32_t seq_parameter_set_id = -1;
|
2017-12-31 04:11:48 +00:00
|
|
|
if ((err = srs_avc_nalu_read_uev(&bs, seq_parameter_set_id)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "read seq_parameter_set_id");
|
2015-04-03 15:17:50 +00:00
|
|
|
}
|
|
|
|
if (seq_parameter_set_id < 0) {
|
2017-09-23 14:12:33 +00:00
|
|
|
return srs_error_new(ERROR_HLS_DECODE_ERROR, "sps the seq_parameter_set_id invalid");
|
2015-04-03 15:17:50 +00:00
|
|
|
}
|
|
|
|
|
2015-07-01 06:50:45 +00:00
|
|
|
int32_t chroma_format_idc = -1;
|
2015-04-03 15:17:50 +00:00
|
|
|
if (profile_idc == 100 || profile_idc == 110 || profile_idc == 122 || profile_idc == 244
|
|
|
|
|| profile_idc == 44 || profile_idc == 83 || profile_idc == 86 || profile_idc == 118
|
2017-09-23 14:12:33 +00:00
|
|
|
|| profile_idc == 128) {
|
2017-12-31 04:11:48 +00:00
|
|
|
if ((err = srs_avc_nalu_read_uev(&bs, chroma_format_idc)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "read chroma_format_idc");
|
2015-04-03 15:17:50 +00:00
|
|
|
}
|
|
|
|
if (chroma_format_idc == 3) {
|
|
|
|
int8_t separate_colour_plane_flag = -1;
|
2017-12-31 04:11:48 +00:00
|
|
|
if ((err = srs_avc_nalu_read_bit(&bs, separate_colour_plane_flag)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "read separate_colour_plane_flag");
|
2015-04-03 15:17:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-04 07:53:36 +00:00
|
|
|
int32_t bit_depth_luma_minus8 = -1;
|
2017-12-31 04:11:48 +00:00
|
|
|
if ((err = srs_avc_nalu_read_uev(&bs, bit_depth_luma_minus8)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "read bit_depth_luma_minus8");;
|
2015-04-03 15:17:50 +00:00
|
|
|
}
|
|
|
|
|
2015-04-04 07:53:36 +00:00
|
|
|
int32_t bit_depth_chroma_minus8 = -1;
|
2017-12-31 04:11:48 +00:00
|
|
|
if ((err = srs_avc_nalu_read_uev(&bs, bit_depth_chroma_minus8)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "read bit_depth_chroma_minus8");;
|
2015-04-03 15:17:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int8_t qpprime_y_zero_transform_bypass_flag = -1;
|
2017-12-31 04:11:48 +00:00
|
|
|
if ((err = srs_avc_nalu_read_bit(&bs, qpprime_y_zero_transform_bypass_flag)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "read qpprime_y_zero_transform_bypass_flag");;
|
2015-04-03 15:17:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int8_t seq_scaling_matrix_present_flag = -1;
|
2017-12-31 04:11:48 +00:00
|
|
|
if ((err = srs_avc_nalu_read_bit(&bs, seq_scaling_matrix_present_flag)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "read seq_scaling_matrix_present_flag");;
|
2015-04-03 15:17:50 +00:00
|
|
|
}
|
|
|
|
if (seq_scaling_matrix_present_flag) {
|
2015-07-01 06:50:45 +00:00
|
|
|
int nb_scmpfs = ((chroma_format_idc != 3)? 8:12);
|
|
|
|
for (int i = 0; i < nb_scmpfs; i++) {
|
|
|
|
int8_t seq_scaling_matrix_present_flag_i = -1;
|
2017-12-31 04:11:48 +00:00
|
|
|
if ((err = srs_avc_nalu_read_bit(&bs, seq_scaling_matrix_present_flag_i)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "read seq_scaling_matrix_present_flag_i");;
|
2015-07-01 06:50:45 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-03 15:17:50 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-04 07:53:36 +00:00
|
|
|
int32_t log2_max_frame_num_minus4 = -1;
|
2017-12-31 04:11:48 +00:00
|
|
|
if ((err = srs_avc_nalu_read_uev(&bs, log2_max_frame_num_minus4)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "read log2_max_frame_num_minus4");;
|
2015-04-03 15:17:50 +00:00
|
|
|
}
|
|
|
|
|
2015-04-04 07:53:36 +00:00
|
|
|
int32_t pic_order_cnt_type = -1;
|
2017-12-31 04:11:48 +00:00
|
|
|
if ((err = srs_avc_nalu_read_uev(&bs, pic_order_cnt_type)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "read pic_order_cnt_type");;
|
2015-04-03 15:17:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (pic_order_cnt_type == 0) {
|
2015-04-04 07:53:36 +00:00
|
|
|
int32_t log2_max_pic_order_cnt_lsb_minus4 = -1;
|
2017-12-31 04:11:48 +00:00
|
|
|
if ((err = srs_avc_nalu_read_uev(&bs, log2_max_pic_order_cnt_lsb_minus4)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "read log2_max_pic_order_cnt_lsb_minus4");;
|
2015-04-03 15:17:50 +00:00
|
|
|
}
|
|
|
|
} else if (pic_order_cnt_type == 1) {
|
|
|
|
int8_t delta_pic_order_always_zero_flag = -1;
|
2017-12-31 04:11:48 +00:00
|
|
|
if ((err = srs_avc_nalu_read_bit(&bs, delta_pic_order_always_zero_flag)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "read delta_pic_order_always_zero_flag");;
|
2015-04-03 15:17:50 +00:00
|
|
|
}
|
|
|
|
|
2015-04-04 07:53:36 +00:00
|
|
|
int32_t offset_for_non_ref_pic = -1;
|
2017-12-31 04:11:48 +00:00
|
|
|
if ((err = srs_avc_nalu_read_uev(&bs, offset_for_non_ref_pic)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "read offset_for_non_ref_pic");;
|
2015-04-03 15:17:50 +00:00
|
|
|
}
|
|
|
|
|
2015-04-04 07:53:36 +00:00
|
|
|
int32_t offset_for_top_to_bottom_field = -1;
|
2017-12-31 04:11:48 +00:00
|
|
|
if ((err = srs_avc_nalu_read_uev(&bs, offset_for_top_to_bottom_field)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "read offset_for_top_to_bottom_field");;
|
2015-04-03 15:17:50 +00:00
|
|
|
}
|
|
|
|
|
2015-04-04 07:53:36 +00:00
|
|
|
int32_t num_ref_frames_in_pic_order_cnt_cycle = -1;
|
2017-12-31 04:11:48 +00:00
|
|
|
if ((err = srs_avc_nalu_read_uev(&bs, num_ref_frames_in_pic_order_cnt_cycle)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "read num_ref_frames_in_pic_order_cnt_cycle");;
|
2015-04-03 15:17:50 +00:00
|
|
|
}
|
2016-02-18 06:15:23 +00:00
|
|
|
if (num_ref_frames_in_pic_order_cnt_cycle < 0) {
|
2017-09-23 14:12:33 +00:00
|
|
|
return srs_error_new(ERROR_HLS_DECODE_ERROR, "sps the num_ref_frames_in_pic_order_cnt_cycle");
|
2015-04-03 15:17:50 +00:00
|
|
|
}
|
2016-02-18 06:15:23 +00:00
|
|
|
for (int i = 0; i < num_ref_frames_in_pic_order_cnt_cycle; i++) {
|
|
|
|
int32_t offset_for_ref_frame_i = -1;
|
2017-12-31 04:11:48 +00:00
|
|
|
if ((err = srs_avc_nalu_read_uev(&bs, offset_for_ref_frame_i)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "read offset_for_ref_frame_i");;
|
2016-02-18 06:15:23 +00:00
|
|
|
}
|
|
|
|
}
|
2015-04-03 15:17:50 +00:00
|
|
|
}
|
|
|
|
|
2015-04-04 07:53:36 +00:00
|
|
|
int32_t max_num_ref_frames = -1;
|
2017-12-31 04:11:48 +00:00
|
|
|
if ((err = srs_avc_nalu_read_uev(&bs, max_num_ref_frames)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "read max_num_ref_frames");;
|
2015-04-03 15:17:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int8_t gaps_in_frame_num_value_allowed_flag = -1;
|
2017-12-31 04:11:48 +00:00
|
|
|
if ((err = srs_avc_nalu_read_bit(&bs, gaps_in_frame_num_value_allowed_flag)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "read gaps_in_frame_num_value_allowed_flag");;
|
2015-04-03 15:17:50 +00:00
|
|
|
}
|
|
|
|
|
2015-04-04 07:53:36 +00:00
|
|
|
int32_t pic_width_in_mbs_minus1 = -1;
|
2017-12-31 04:11:48 +00:00
|
|
|
if ((err = srs_avc_nalu_read_uev(&bs, pic_width_in_mbs_minus1)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "read pic_width_in_mbs_minus1");;
|
2015-04-03 15:17:50 +00:00
|
|
|
}
|
|
|
|
|
2015-04-04 07:53:36 +00:00
|
|
|
int32_t pic_height_in_map_units_minus1 = -1;
|
2017-12-31 04:11:48 +00:00
|
|
|
if ((err = srs_avc_nalu_read_uev(&bs, pic_height_in_map_units_minus1)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "read pic_height_in_map_units_minus1");;
|
2015-04-03 15:17:50 +00:00
|
|
|
}
|
2021-10-10 04:05:26 +00:00
|
|
|
|
|
|
|
int8_t frame_mbs_only_flag = -1;
|
|
|
|
if ((err = srs_avc_nalu_read_bit(&bs, frame_mbs_only_flag)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "read frame_mbs_only_flag");;
|
|
|
|
}
|
|
|
|
if(!frame_mbs_only_flag) {
|
|
|
|
/* Skip mb_adaptive_frame_field_flag */
|
|
|
|
int8_t mb_adaptive_frame_field_flag = -1;
|
|
|
|
if ((err = srs_avc_nalu_read_bit(&bs, mb_adaptive_frame_field_flag)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "read mb_adaptive_frame_field_flag");;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip direct_8x8_inference_flag */
|
|
|
|
int8_t direct_8x8_inference_flag = -1;
|
|
|
|
if ((err = srs_avc_nalu_read_bit(&bs, direct_8x8_inference_flag)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "read direct_8x8_inference_flag");;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We need the following value to evaluate offsets, if any */
|
|
|
|
int8_t frame_cropping_flag = -1;
|
|
|
|
if ((err = srs_avc_nalu_read_bit(&bs, frame_cropping_flag)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "read frame_cropping_flag");;
|
|
|
|
}
|
|
|
|
int32_t frame_crop_left_offset = 0, frame_crop_right_offset = 0,
|
|
|
|
frame_crop_top_offset = 0, frame_crop_bottom_offset = 0;
|
|
|
|
if(frame_cropping_flag) {
|
|
|
|
if ((err = srs_avc_nalu_read_uev(&bs, frame_crop_left_offset)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "read frame_crop_left_offset");;
|
|
|
|
}
|
|
|
|
if ((err = srs_avc_nalu_read_uev(&bs, frame_crop_right_offset)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "read frame_crop_right_offset");;
|
|
|
|
}
|
|
|
|
if ((err = srs_avc_nalu_read_uev(&bs, frame_crop_top_offset)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "read frame_crop_top_offset");;
|
|
|
|
}
|
|
|
|
if ((err = srs_avc_nalu_read_uev(&bs, frame_crop_bottom_offset)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "read frame_crop_bottom_offset");;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Skip vui_parameters_present_flag */
|
|
|
|
int8_t vui_parameters_present_flag = -1;
|
|
|
|
if ((err = srs_avc_nalu_read_bit(&bs, vui_parameters_present_flag)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "read vui_parameters_present_flag");;
|
|
|
|
}
|
|
|
|
|
|
|
|
vcodec->width = ((pic_width_in_mbs_minus1 + 1) * 16) - frame_crop_left_offset * 2 - frame_crop_right_offset * 2;
|
|
|
|
vcodec->height = ((2 - frame_mbs_only_flag) * (pic_height_in_map_units_minus1 + 1) * 16) \
|
|
|
|
- (frame_crop_top_offset * 2) - (frame_crop_bottom_offset * 2);
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
return err;
|
2017-02-12 10:18:18 +00:00
|
|
|
}
|
|
|
|
|
2019-12-24 07:35:19 +00:00
|
|
|
// LCOV_EXCL_STOP
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t SrsFormat::video_nalu_demux(SrsBuffer* stream)
|
2017-02-12 10:18:18 +00:00
|
|
|
{
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t err = srs_success;
|
2017-02-12 10:18:18 +00:00
|
|
|
|
|
|
|
// ensure the sequence header demuxed
|
|
|
|
if (!vcodec->is_avc_codec_ok()) {
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_warn("avc ignore type=%d for no sequence header", SrsVideoAvcFrameTraitNALU);
|
|
|
|
return err;
|
2017-02-12 10:18:18 +00:00
|
|
|
}
|
2022-09-01 13:45:19 +00:00
|
|
|
|
2022-11-23 00:34:13 +00:00
|
|
|
if (vcodec->id == SrsVideoCodecIdHEVC) {
|
2022-11-23 04:13:53 +00:00
|
|
|
#ifdef SRS_H265
|
2022-11-23 00:34:13 +00:00
|
|
|
// TODO: FIXME: Might need to guess format?
|
2022-11-23 04:13:53 +00:00
|
|
|
return do_avc_demux_ibmf_format(stream);
|
|
|
|
#else
|
2022-11-23 09:05:21 +00:00
|
|
|
return srs_error_new(ERROR_HEVC_DISABLED, "H.265 is disabled");
|
2022-11-23 00:34:13 +00:00
|
|
|
#endif
|
2022-11-23 04:13:53 +00:00
|
|
|
}
|
2022-11-23 00:34:13 +00:00
|
|
|
|
2022-09-01 13:45:19 +00:00
|
|
|
// Parse the SPS/PPS in ANNEXB or IBMF format.
|
|
|
|
if (vcodec->payload_format == SrsAvcPayloadFormatIbmf) {
|
2017-09-23 14:12:33 +00:00
|
|
|
if ((err = avc_demux_ibmf_format(stream)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "avc demux ibmf");
|
2017-02-12 10:18:18 +00:00
|
|
|
}
|
2022-09-01 13:45:19 +00:00
|
|
|
} else if (vcodec->payload_format == SrsAvcPayloadFormatAnnexb) {
|
2017-09-23 14:12:33 +00:00
|
|
|
if ((err = avc_demux_annexb_format(stream)) != srs_success) {
|
2022-09-01 13:45:19 +00:00
|
|
|
return srs_error_wrap(err, "avc demux annexb");
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if ((err = try_annexb_first ? avc_demux_annexb_format(stream) : avc_demux_ibmf_format(stream)) == srs_success) {
|
|
|
|
vcodec->payload_format = try_annexb_first ? SrsAvcPayloadFormatAnnexb : SrsAvcPayloadFormatIbmf;
|
|
|
|
} else {
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_freep(err);
|
2022-09-01 13:45:19 +00:00
|
|
|
if ((err = try_annexb_first ? avc_demux_ibmf_format(stream) : avc_demux_annexb_format(stream)) == srs_success) {
|
|
|
|
vcodec->payload_format = try_annexb_first ? SrsAvcPayloadFormatIbmf : SrsAvcPayloadFormatAnnexb;
|
2017-02-12 10:18:18 +00:00
|
|
|
} else {
|
2022-09-01 13:45:19 +00:00
|
|
|
return srs_error_wrap(err, "avc demux try_annexb_first=%d", try_annexb_first);
|
2017-02-12 10:18:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-04-03 15:17:50 +00:00
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
return err;
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t SrsFormat::avc_demux_annexb_format(SrsBuffer* stream)
|
2022-09-01 13:45:19 +00:00
|
|
|
{
|
|
|
|
srs_error_t err = srs_success;
|
|
|
|
|
|
|
|
int pos = stream->pos();
|
|
|
|
err = do_avc_demux_annexb_format(stream);
|
|
|
|
|
|
|
|
// Restore the stream if error.
|
|
|
|
if (err != srs_success) {
|
|
|
|
stream->skip(pos - stream->pos());
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
srs_error_t SrsFormat::do_avc_demux_annexb_format(SrsBuffer* stream)
|
2015-01-31 13:16:42 +00:00
|
|
|
{
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t err = srs_success;
|
2015-01-31 13:16:42 +00:00
|
|
|
|
|
|
|
// not annexb, try others
|
|
|
|
if (!srs_avc_startswith_annexb(stream, NULL)) {
|
2022-09-01 13:45:19 +00:00
|
|
|
return srs_error_new(ERROR_HLS_DECODE_ERROR, "not annexb");
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AnnexB
|
|
|
|
// B.1.1 Byte stream NAL unit syntax,
|
2017-01-26 09:28:49 +00:00
|
|
|
// ISO_IEC_14496-10-AVC-2003.pdf, page 211.
|
2015-01-31 13:16:42 +00:00
|
|
|
while (!stream->empty()) {
|
|
|
|
// find start code
|
|
|
|
int nb_start_code = 0;
|
|
|
|
if (!srs_avc_startswith_annexb(stream, &nb_start_code)) {
|
2017-09-23 14:12:33 +00:00
|
|
|
return err;
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
2017-02-12 10:18:18 +00:00
|
|
|
|
2015-01-31 13:16:42 +00:00
|
|
|
// skip the start code.
|
|
|
|
if (nb_start_code > 0) {
|
|
|
|
stream->skip(nb_start_code);
|
|
|
|
}
|
|
|
|
|
|
|
|
// the NALU start bytes.
|
|
|
|
char* p = stream->data() + stream->pos();
|
|
|
|
|
|
|
|
// get the last matched NALU
|
|
|
|
while (!stream->empty()) {
|
|
|
|
if (srs_avc_startswith_annexb(stream, NULL)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream->skip(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
char* pp = stream->data() + stream->pos();
|
|
|
|
|
|
|
|
// skip the empty.
|
|
|
|
if (pp - p <= 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// got the NALU.
|
2017-09-23 14:12:33 +00:00
|
|
|
if ((err = video->add_sample(p, (int)(pp - p))) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "add video frame");
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
return err;
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t SrsFormat::avc_demux_ibmf_format(SrsBuffer* stream)
|
2022-09-01 13:45:19 +00:00
|
|
|
{
|
|
|
|
srs_error_t err = srs_success;
|
|
|
|
|
|
|
|
int pos = stream->pos();
|
|
|
|
err = do_avc_demux_ibmf_format(stream);
|
|
|
|
|
|
|
|
// Restore the stream if error.
|
|
|
|
if (err != srs_success) {
|
|
|
|
stream->skip(pos - stream->pos());
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
srs_error_t SrsFormat::do_avc_demux_ibmf_format(SrsBuffer* stream)
|
2015-01-31 13:16:42 +00:00
|
|
|
{
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t err = srs_success;
|
2015-01-31 13:16:42 +00:00
|
|
|
|
|
|
|
int PictureLength = stream->size() - stream->pos();
|
|
|
|
|
2017-01-26 09:28:49 +00:00
|
|
|
// 5.3.4.2.1 Syntax, ISO_IEC_14496-15-AVC-format-2012.pdf, page 16
|
2015-01-31 13:16:42 +00:00
|
|
|
// 5.2.4.1 AVC decoder configuration record
|
|
|
|
// 5.2.4.1.2 Semantics
|
|
|
|
// The value of this field shall be one of 0, 1, or 3 corresponding to a
|
|
|
|
// length encoded with 1, 2, or 4 bytes, respectively.
|
2017-02-12 10:18:18 +00:00
|
|
|
srs_assert(vcodec->NAL_unit_length != 2);
|
2015-01-31 13:16:42 +00:00
|
|
|
|
2017-01-26 09:28:49 +00:00
|
|
|
// 5.3.4.2.1 Syntax, ISO_IEC_14496-15-AVC-format-2012.pdf, page 20
|
2015-01-31 13:16:42 +00:00
|
|
|
for (int i = 0; i < PictureLength;) {
|
|
|
|
// unsigned int((NAL_unit_length+1)*8) NALUnitLength;
|
2022-11-23 09:05:21 +00:00
|
|
|
// TODO: FIXME: Should ignore error? See https://github.com/ossrs/srs-gb28181/commit/a13b9b54938a14796abb9011e7a8ee779439a452
|
2017-02-12 10:18:18 +00:00
|
|
|
if (!stream->require(vcodec->NAL_unit_length + 1)) {
|
2022-11-23 04:13:53 +00:00
|
|
|
return srs_error_new(ERROR_HLS_DECODE_ERROR, "PictureLength:%d, i:%d, NaluLength:%d, left:%d",
|
|
|
|
PictureLength, i, vcodec->NAL_unit_length, stream->left());
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
|
|
|
int32_t NALUnitLength = 0;
|
2017-02-12 10:18:18 +00:00
|
|
|
if (vcodec->NAL_unit_length == 3) {
|
2015-01-31 13:16:42 +00:00
|
|
|
NALUnitLength = stream->read_4bytes();
|
2017-02-12 10:18:18 +00:00
|
|
|
} else if (vcodec->NAL_unit_length == 1) {
|
2015-01-31 13:16:42 +00:00
|
|
|
NALUnitLength = stream->read_2bytes();
|
|
|
|
} else {
|
|
|
|
NALUnitLength = stream->read_1bytes();
|
|
|
|
}
|
|
|
|
|
2022-11-23 04:13:53 +00:00
|
|
|
// The stream format mighe be incorrect, see: https://github.com/ossrs/srs/issues/183
|
2015-01-31 13:16:42 +00:00
|
|
|
if (NALUnitLength < 0) {
|
2022-11-23 04:13:53 +00:00
|
|
|
return srs_error_new(ERROR_HLS_DECODE_ERROR, "PictureLength:%d, i:%d, NaluLength:%d, left:%d, NALUnitLength:%d",
|
|
|
|
PictureLength, i, vcodec->NAL_unit_length, stream->left(), NALUnitLength);
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NALUnit
|
|
|
|
if (!stream->require(NALUnitLength)) {
|
2022-11-23 04:13:53 +00:00
|
|
|
return srs_error_new(ERROR_HLS_DECODE_ERROR, "PictureLength:%d, i:%d, NaluLength:%d, left:%d, NALUnitLength:%d",
|
|
|
|
PictureLength, i, vcodec->NAL_unit_length, stream->left(), NALUnitLength);
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
2017-01-26 09:28:49 +00:00
|
|
|
// 7.3.1 NAL unit syntax, ISO_IEC_14496-10-AVC-2003.pdf, page 44.
|
2017-09-23 14:12:33 +00:00
|
|
|
if ((err = video->add_sample(stream->data() + stream->pos(), NALUnitLength)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "avc add video frame");
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
2022-11-23 04:13:53 +00:00
|
|
|
|
2015-01-31 13:16:42 +00:00
|
|
|
stream->skip(NALUnitLength);
|
2017-02-12 10:18:18 +00:00
|
|
|
i += vcodec->NAL_unit_length + 1 + NALUnitLength;
|
|
|
|
}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
return err;
|
2017-02-12 10:18:18 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t SrsFormat::audio_aac_demux(SrsBuffer* stream, int64_t timestamp)
|
2017-02-12 10:18:18 +00:00
|
|
|
{
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t err = srs_success;
|
2017-02-12 10:18:18 +00:00
|
|
|
|
|
|
|
audio->cts = 0;
|
|
|
|
audio->dts = timestamp;
|
|
|
|
|
|
|
|
// @see: E.4.2 Audio Tags, video_file_format_spec_v10_1.pdf, page 76
|
|
|
|
int8_t sound_format = stream->read_1bytes();
|
|
|
|
|
|
|
|
int8_t sound_type = sound_format & 0x01;
|
|
|
|
int8_t sound_size = (sound_format >> 1) & 0x01;
|
|
|
|
int8_t sound_rate = (sound_format >> 2) & 0x03;
|
|
|
|
sound_format = (sound_format >> 4) & 0x0f;
|
2020-01-01 13:01:14 +00:00
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
SrsAudioCodecId codec_id = (SrsAudioCodecId)sound_format;
|
2017-02-12 10:18:18 +00:00
|
|
|
acodec->id = codec_id;
|
2020-01-01 13:01:14 +00:00
|
|
|
|
2017-02-12 12:46:24 +00:00
|
|
|
acodec->sound_type = (SrsAudioChannels)sound_type;
|
2017-02-12 12:38:39 +00:00
|
|
|
acodec->sound_rate = (SrsAudioSampleRate)sound_rate;
|
2017-02-12 12:46:24 +00:00
|
|
|
acodec->sound_size = (SrsAudioSampleBits)sound_size;
|
2020-01-01 13:01:14 +00:00
|
|
|
|
2017-02-12 10:18:18 +00:00
|
|
|
// we support h.264+mp3 for hls.
|
2017-02-12 12:38:39 +00:00
|
|
|
if (codec_id == SrsAudioCodecIdMP3) {
|
2017-09-23 14:12:33 +00:00
|
|
|
return srs_error_new(ERROR_HLS_TRY_MP3, "try mp3");
|
2017-02-12 10:18:18 +00:00
|
|
|
}
|
2020-01-01 13:01:14 +00:00
|
|
|
|
2017-02-12 10:18:18 +00:00
|
|
|
// only support aac
|
2017-02-12 12:38:39 +00:00
|
|
|
if (codec_id != SrsAudioCodecIdAAC) {
|
2017-09-23 14:12:33 +00:00
|
|
|
return srs_error_new(ERROR_HLS_DECODE_ERROR, "not supported codec %d", codec_id);
|
2017-02-12 10:18:18 +00:00
|
|
|
}
|
2020-01-01 13:01:14 +00:00
|
|
|
|
2017-02-12 10:18:18 +00:00
|
|
|
if (!stream->require(1)) {
|
2017-09-23 14:12:33 +00:00
|
|
|
return srs_error_new(ERROR_HLS_DECODE_ERROR, "aac decode aac_packet_type");
|
2017-02-12 10:18:18 +00:00
|
|
|
}
|
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
SrsAudioAacFrameTrait aac_packet_type = (SrsAudioAacFrameTrait)stream->read_1bytes();
|
|
|
|
audio->aac_packet_type = (SrsAudioAacFrameTrait)aac_packet_type;
|
2017-02-12 10:18:18 +00:00
|
|
|
|
2017-06-04 07:10:35 +00:00
|
|
|
// Update the RAW AAC data.
|
|
|
|
raw = stream->data() + stream->pos();
|
|
|
|
nb_raw = stream->size() - stream->pos();
|
|
|
|
|
2017-02-12 12:38:39 +00:00
|
|
|
if (aac_packet_type == SrsAudioAacFrameTraitSequenceHeader) {
|
2017-02-12 10:18:18 +00:00
|
|
|
// AudioSpecificConfig
|
|
|
|
// 1.6.2.1 AudioSpecificConfig, in ISO_IEC_14496-3-AAC-2001.pdf, page 33.
|
2017-04-24 02:08:57 +00:00
|
|
|
int aac_extra_size = stream->size() - stream->pos();
|
|
|
|
if (aac_extra_size > 0) {
|
|
|
|
char *copy_stream_from = stream->data() + stream->pos();
|
|
|
|
acodec->aac_extra_data = std::vector<char>(copy_stream_from, copy_stream_from + aac_extra_size);
|
2017-02-12 10:18:18 +00:00
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
if ((err = audio_aac_sequence_header_demux(&acodec->aac_extra_data[0], aac_extra_size)) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "demux aac sh");
|
2017-02-12 10:18:18 +00:00
|
|
|
}
|
|
|
|
}
|
2017-02-12 12:38:39 +00:00
|
|
|
} else if (aac_packet_type == SrsAudioAacFrameTraitRawData) {
|
2017-02-12 10:18:18 +00:00
|
|
|
// ensure the sequence header demuxed
|
|
|
|
if (!acodec->is_aac_codec_ok()) {
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_warn("aac ignore type=%d for no sequence header", aac_packet_type);
|
|
|
|
return err;
|
2017-02-12 10:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Raw AAC frame data in UI8 []
|
2017-03-06 02:19:19 +00:00
|
|
|
// 6.3 Raw Data, ISO_IEC_13818-7-AAC-2004.pdf, page 28
|
2017-09-23 14:12:33 +00:00
|
|
|
if ((err = audio->add_sample(stream->data() + stream->pos(), stream->size() - stream->pos())) != srs_success) {
|
|
|
|
return srs_error_wrap(err, "add audio frame");
|
2017-02-12 10:18:18 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// ignored.
|
|
|
|
}
|
|
|
|
|
|
|
|
// reset the sample rate by sequence header
|
2017-02-12 12:38:39 +00:00
|
|
|
if (acodec->aac_sample_rate != SrsAacSampleRateUnset) {
|
|
|
|
static int srs_aac_srates[] = {
|
2017-02-12 10:18:18 +00:00
|
|
|
96000, 88200, 64000, 48000,
|
|
|
|
44100, 32000, 24000, 22050,
|
|
|
|
16000, 12000, 11025, 8000,
|
|
|
|
7350, 0, 0, 0
|
|
|
|
};
|
2017-02-12 12:38:39 +00:00
|
|
|
switch (srs_aac_srates[acodec->aac_sample_rate]) {
|
2017-02-12 10:18:18 +00:00
|
|
|
case 11025:
|
2017-02-12 12:38:39 +00:00
|
|
|
acodec->sound_rate = SrsAudioSampleRate11025;
|
2017-02-12 10:18:18 +00:00
|
|
|
break;
|
|
|
|
case 22050:
|
2017-02-12 12:38:39 +00:00
|
|
|
acodec->sound_rate = SrsAudioSampleRate22050;
|
2017-02-12 10:18:18 +00:00
|
|
|
break;
|
|
|
|
case 44100:
|
2017-02-12 12:38:39 +00:00
|
|
|
acodec->sound_rate = SrsAudioSampleRate44100;
|
2017-02-12 10:18:18 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
};
|
|
|
|
}
|
2017-03-25 05:07:09 +00:00
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
return err;
|
2017-02-12 10:18:18 +00:00
|
|
|
}
|
|
|
|
|
2022-12-25 13:11:08 +00:00
|
|
|
srs_error_t SrsFormat::audio_mp3_demux(SrsBuffer* stream, int64_t timestamp, bool fresh)
|
2017-02-12 10:18:18 +00:00
|
|
|
{
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t err = srs_success;
|
2017-02-12 10:18:18 +00:00
|
|
|
|
|
|
|
audio->cts = 0;
|
|
|
|
audio->dts = timestamp;
|
2022-12-25 13:11:08 +00:00
|
|
|
audio->aac_packet_type = fresh ? SrsAudioMp3FrameTraitSequenceHeader : SrsAudioMp3FrameTraitRawData;
|
2017-02-12 10:18:18 +00:00
|
|
|
|
2017-03-05 10:44:37 +00:00
|
|
|
// @see: E.4.2 Audio Tags, video_file_format_spec_v10_1.pdf, page 76
|
|
|
|
int8_t sound_format = stream->read_1bytes();
|
|
|
|
|
|
|
|
int8_t sound_type = sound_format & 0x01;
|
|
|
|
int8_t sound_size = (sound_format >> 1) & 0x01;
|
|
|
|
int8_t sound_rate = (sound_format >> 2) & 0x03;
|
|
|
|
sound_format = (sound_format >> 4) & 0x0f;
|
|
|
|
|
|
|
|
SrsAudioCodecId codec_id = (SrsAudioCodecId)sound_format;
|
|
|
|
acodec->id = codec_id;
|
|
|
|
|
|
|
|
acodec->sound_type = (SrsAudioChannels)sound_type;
|
|
|
|
acodec->sound_rate = (SrsAudioSampleRate)sound_rate;
|
|
|
|
acodec->sound_size = (SrsAudioSampleBits)sound_size;
|
|
|
|
|
2017-02-12 10:18:18 +00:00
|
|
|
// we always decode aac then mp3.
|
2017-02-12 12:38:39 +00:00
|
|
|
srs_assert(acodec->id == SrsAudioCodecIdMP3);
|
2017-02-12 10:18:18 +00:00
|
|
|
|
2022-12-25 03:43:26 +00:00
|
|
|
// Update the RAW MP3 data. Note the start is 12 bits syncword 0xFFF, so we should not skip any bytes, for detail
|
|
|
|
// please see ISO_IEC_11172-3-MP3-1993.pdf page 20 and 26.
|
2017-06-04 07:10:35 +00:00
|
|
|
raw = stream->data() + stream->pos();
|
|
|
|
nb_raw = stream->size() - stream->pos();
|
|
|
|
|
2017-02-12 10:18:18 +00:00
|
|
|
// mp3 payload.
|
2022-12-25 03:43:26 +00:00
|
|
|
if ((err = audio->add_sample(raw, nb_raw)) != srs_success) {
|
2017-09-23 14:12:33 +00:00
|
|
|
return srs_error_wrap(err, "add audio frame");
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
return err;
|
2017-02-12 10:18:18 +00:00
|
|
|
}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t SrsFormat::audio_aac_sequence_header_demux(char* data, int size)
|
2017-02-12 10:18:18 +00:00
|
|
|
{
|
2017-09-23 14:12:33 +00:00
|
|
|
srs_error_t err = srs_success;
|
2017-02-12 10:18:18 +00:00
|
|
|
|
2017-12-31 04:11:48 +00:00
|
|
|
SrsBuffer* buffer = new SrsBuffer(data, size);
|
|
|
|
SrsAutoFree(SrsBuffer, buffer);
|
2017-02-12 10:18:18 +00:00
|
|
|
|
|
|
|
// only need to decode the first 2bytes:
|
|
|
|
// audioObjectType, aac_profile, 5bits.
|
|
|
|
// samplingFrequencyIndex, aac_sample_rate, 4bits.
|
|
|
|
// channelConfiguration, aac_channels, 4bits
|
|
|
|
if (!buffer->require(2)) {
|
2017-09-23 14:12:33 +00:00
|
|
|
return srs_error_new(ERROR_HLS_DECODE_ERROR, "audio codec decode aac sh");
|
2017-02-12 10:18:18 +00:00
|
|
|
}
|
|
|
|
uint8_t profile_ObjectType = buffer->read_1bytes();
|
|
|
|
uint8_t samplingFrequencyIndex = buffer->read_1bytes();
|
|
|
|
|
|
|
|
acodec->aac_channels = (samplingFrequencyIndex >> 3) & 0x0f;
|
|
|
|
samplingFrequencyIndex = ((profile_ObjectType << 1) & 0x0e) | ((samplingFrequencyIndex >> 7) & 0x01);
|
|
|
|
profile_ObjectType = (profile_ObjectType >> 3) & 0x1f;
|
|
|
|
|
|
|
|
// set the aac sample rate.
|
|
|
|
acodec->aac_sample_rate = samplingFrequencyIndex;
|
|
|
|
|
|
|
|
// convert the object type in sequence header to aac profile of ADTS.
|
|
|
|
acodec->aac_object = (SrsAacObjectType)profile_ObjectType;
|
|
|
|
if (acodec->aac_object == SrsAacObjectTypeReserved) {
|
2017-09-23 14:12:33 +00:00
|
|
|
return srs_error_new(ERROR_HLS_DECODE_ERROR, "aac decode sh object %d", profile_ObjectType);
|
2017-02-12 10:18:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: FIXME: to support aac he/he-v2, see: ngx_rtmp_codec_parse_aac_header
|
|
|
|
// @see: https://github.com/winlinvip/nginx-rtmp-module/commit/3a5f9eea78fc8d11e8be922aea9ac349b9dcbfc2
|
|
|
|
//
|
|
|
|
// donot force to LC, @see: https://github.com/ossrs/srs/issues/81
|
|
|
|
// the source will print the sequence header info.
|
|
|
|
//if (aac_profile > 3) {
|
2019-02-04 04:05:07 +00:00
|
|
|
// Mark all extended profiles as LC
|
|
|
|
// to make Android as happy as possible.
|
|
|
|
// @see: ngx_rtmp_hls_parse_aac_header
|
|
|
|
//aac_profile = 1;
|
2017-02-12 10:18:18 +00:00
|
|
|
//}
|
|
|
|
|
2017-09-23 14:12:33 +00:00
|
|
|
return err;
|
2015-01-31 13:16:42 +00:00
|
|
|
}
|
|
|
|
|