| 1 |
/* |
| 2 |
* Copyright (c) 2010-2022 Belledonne Communications SARL. |
| 3 |
* |
| 4 |
* This file is part of mediastreamer2 |
| 5 |
* (see https://gitlab.linphone.org/BC/public/mediastreamer2). |
| 6 |
* |
| 7 |
* This program is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU Affero General Public License as |
| 9 |
* published by the Free Software Foundation, either version 3 of the |
| 10 |
* License, or (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU Affero General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU Affero General Public License |
| 18 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
#pragma once |
| 22 |
|
| 23 |
#include "h26x-utils.h" |
| 24 |
|
| 25 |
namespace mediastreamer { |
| 26 |
|
| 27 |
class H265NaluType : public H26xNaluType { |
| 28 |
public: |
| 29 |
H265NaluType() = default; |
| 30 |
H265NaluType(uint8_t value); |
| 31 |
|
| 32 |
bool isVcl() const override { |
| 33 |
return _value < 32; |
| 34 |
} |
| 35 |
bool isParameterSet() const override { |
| 36 |
return *this == Vps || *this == Sps || *this == Pps; |
| 37 |
} |
| 38 |
bool isKeyFramePart() const override { |
| 39 |
return *this == IdrNLp || *this == IdrWRadl || *this == CraNut; |
| 40 |
} |
| 41 |
|
| 42 |
static const H265NaluType IdrWRadl; |
| 43 |
static const H265NaluType IdrNLp; |
| 44 |
static const H265NaluType CraNut; |
| 45 |
static const H265NaluType Vps; |
| 46 |
static const H265NaluType Sps; |
| 47 |
static const H265NaluType Pps; |
| 48 |
static const H265NaluType Ap; |
| 49 |
static const H265NaluType Fu; |
| 50 |
}; |
| 51 |
|
| 52 |
class H265NaluHeader : public H26xNaluHeader { |
| 53 |
public: |
| 54 |
H265NaluHeader() : H26xNaluHeader(){}; |
| 55 |
H265NaluHeader(const uint8_t *header) { |
| 56 |
parse(header); |
| 57 |
} |
| 58 |
|
| 59 |
void setType(H265NaluType type) { |
| 60 |
_type = type; |
| 61 |
} |
| 62 |
H265NaluType getType() const { |
| 63 |
return _type; |
| 64 |
} |
| 65 |
|
| 66 |
const H26xNaluType &getAbsType() const override { |
| 67 |
return _type; |
| 68 |
} |
| 69 |
|
| 70 |
void setLayerId(uint8_t layerId); |
| 71 |
uint8_t getLayerId() const { |
| 72 |
return _layerId; |
| 73 |
} |
| 74 |
|
| 75 |
void setTid(uint8_t tid); |
| 76 |
uint8_t getTid() const { |
| 77 |
return _tid; |
| 78 |
} |
| 79 |
|
| 80 |
bool operator==(const H265NaluHeader &h2) const; |
| 81 |
bool operator!=(const H265NaluHeader &h2) const { |
| 82 |
return !(*this == h2); |
| 83 |
} |
| 84 |
|
| 85 |
void parse(const uint8_t *header) override; |
| 86 |
mblk_t *forge() const override; |
| 87 |
|
| 88 |
static const size_t length = 2; |
| 89 |
|
| 90 |
private: |
| 91 |
H265NaluType _type; |
| 92 |
uint8_t _layerId = 0; |
| 93 |
uint8_t _tid = 0; |
| 94 |
}; |
| 95 |
|
| 96 |
class H265FuHeader { |
| 97 |
public: |
| 98 |
enum class Position { Start, Middle, End }; |
| 99 |
|
| 100 |
H265FuHeader() = default; |
| 101 |
H265FuHeader(const uint8_t *header) { |
| 102 |
parse(header); |
| 103 |
} |
| 104 |
|
| 105 |
void setPosition(Position pos) { |
| 106 |
_pos = pos; |
| 107 |
} |
| 108 |
Position getPosition() const { |
| 109 |
return _pos; |
| 110 |
} |
| 111 |
|
| 112 |
void setType(H265NaluType type) { |
| 113 |
_type = type; |
| 114 |
} |
| 115 |
H265NaluType getType() const { |
| 116 |
return _type; |
| 117 |
} |
| 118 |
|
| 119 |
void parse(const uint8_t *header); |
| 120 |
mblk_t *forge() const; |
| 121 |
|
| 122 |
static const size_t length = 1; |
| 123 |
|
| 124 |
private: |
| 125 |
Position _pos = Position::Start; |
| 126 |
H265NaluType _type; |
| 127 |
}; |
| 128 |
|
| 129 |
class H265ParameterSetsInserter : public H26xParameterSetsInserter { |
| 130 |
public: |
| 131 |
~H265ParameterSetsInserter() { |
| 132 |
flush(); |
| 133 |
} |
| 134 |
void process(MSQueue *in, MSQueue *out) override; |
| 135 |
void flush() override; |
| 136 |
|
| 137 |
private: |
| 138 |
mblk_t *_vps = nullptr; |
| 139 |
mblk_t *_sps = nullptr; |
| 140 |
mblk_t *_pps = nullptr; |
| 141 |
}; |
| 142 |
|
| 143 |
class H265ParameterSetsStore : public H26xParameterSetsStore { |
| 144 |
public: |
| 145 |
H265ParameterSetsStore() |
| 146 |
: H26xParameterSetsStore("video/hevc", {H265NaluType::Vps, H265NaluType::Sps, H265NaluType::Pps}) { |
| 147 |
} |
| 148 |
}; |
| 149 |
|
| 150 |
class H265ToolFactory : public H26xToolFactory { |
| 151 |
public: |
| 152 |
H26xNaluHeader *createNaluHeader() const override; |
| 153 |
NalPacker *createNalPacker(size_t maxPayloadType) const override; |
| 154 |
NalUnpacker *createNalUnpacker() const override; |
| 155 |
H26xParameterSetsInserter *createParameterSetsInserter() const override; |
| 156 |
H26xParameterSetsStore *createParameterSetsStore() const override; |
| 157 |
}; |
| 158 |
|
| 159 |
} // namespace mediastreamer |