| 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 |
#ifndef H264_UTILS_H |
| 22 |
#define H264_UTILS_H |
| 23 |
|
| 24 |
#include "mediastreamer2/msqueue.h" |
| 25 |
#include "mediastreamer2/msvideo.h" |
| 26 |
|
| 27 |
#ifdef __cplusplus |
| 28 |
#include "h26x-utils.h" |
| 29 |
#endif |
| 30 |
|
| 31 |
typedef int MSH264NaluType; |
| 32 |
|
| 33 |
const MSH264NaluType MSH264NaluTypeDataPartA = 2; |
| 34 |
const MSH264NaluType MSH264NaluTypeDataPartB = 3; |
| 35 |
const MSH264NaluType MSH264NaluTypeDataPartC = 4; |
| 36 |
const MSH264NaluType MSH264NaluTypeIDR = 5; |
| 37 |
const MSH264NaluType MSH264NaluTypeSEI = 6; |
| 38 |
const MSH264NaluType MSH264NaluTypeSPS = 7; |
| 39 |
const MSH264NaluType MSH264NaluTypePPS = 8; |
| 40 |
const MSH264NaluType MSH264NaluTypeSTAPA = 24; |
| 41 |
const MSH264NaluType MSH264NaluTypeFUA = 28; |
| 42 |
|
| 43 |
#ifdef __cplusplus |
| 44 |
extern "C" { |
| 45 |
#endif |
| 46 |
|
| 47 |
/** |
| 48 |
* Extract the NRI value from NALu header. |
| 49 |
*/ |
| 50 |
uint8_t ms_h264_nalu_get_nri(const mblk_t *nalu); |
| 51 |
|
| 52 |
/** |
| 53 |
* Gets the type of a NAL unit. |
| 54 |
* @param nalu The NAL unit to analyze. |
| 55 |
* @return The NALu type or #MSH264NaluTypeUnknown if the type of the NALu is unknown. |
| 56 |
*/ |
| 57 |
MSH264NaluType ms_h264_nalu_get_type(const mblk_t *nalu); |
| 58 |
|
| 59 |
/** |
| 60 |
* @brief Get the ID of a SPS NALu. |
| 61 |
* @warning If the passed NALu is not a SPS, the behavior is undefined. |
| 62 |
*/ |
| 63 |
unsigned int ms_h264_sps_get_id(const mblk_t *sps); |
| 64 |
|
| 65 |
/** |
| 66 |
* @brief Get the ID of a PPS NALu. |
| 67 |
* @warning If the passed NALu is not a PPS, the behavior is undefined. |
| 68 |
*/ |
| 69 |
unsigned int ms_h264_pps_get_id(const mblk_t *pps); |
| 70 |
|
| 71 |
/** |
| 72 |
* Slices a bitstream buffer into several nal units. |
| 73 |
* |
| 74 |
* Nal units are stored in freshly allocated mblk_t buffers which are pushed into |
| 75 |
* a queue. This function does not alter the buffer where the bitstream is contained in. |
| 76 |
* @param bitstream Pointer on a memory segment that contains the bitstream to slice. |
| 77 |
* @param size Size of the memory segment. |
| 78 |
* @param nalus A queue where produced nal units will be pushed into. |
| 79 |
*/ |
| 80 |
void ms_h264_bitstream_to_nalus(const uint8_t *bitstream, size_t size, MSQueue *nalus); |
| 81 |
|
| 82 |
/** |
| 83 |
* @brief Slices a frame into several nal units. |
| 84 |
* |
| 85 |
* Same as ms_h264_bitstream_to_nalus() except that the stream must be in size-prefixed |
| 86 |
* format i.e. each nalu in the stream must be prefixed by its size encoded on 4 bytes big-endian. |
| 87 |
* |
| 88 |
* @param frame Buffer containing the stream to slice |
| 89 |
* @param size Size of the buffer |
| 90 |
* @param nalus The queue where produced nal units will be pushed into |
| 91 |
* @param idr_count If not NULL, use the pointer to store the number of IDR nalus found in the stream. |
| 92 |
* |
| 93 |
* @deprecated Use mediastreamer::naluStreamToNalus() instead. Deprecated since 2018-05-21. |
| 94 |
*/ |
| 95 |
void ms_h264_stream_to_nalus(const uint8_t *frame, size_t size, MSQueue *nalus, int *idr_count); |
| 96 |
|
| 97 |
/** |
| 98 |
* @brief Extract video size from a SPS NALu. |
| 99 |
* @param sps A mblk_t holding the SPS. |
| 100 |
* @return The video size. |
| 101 |
*/ |
| 102 |
MSVideoSize ms_h264_sps_get_video_size(const mblk_t *sps); |
| 103 |
|
| 104 |
#ifdef __cplusplus |
| 105 |
} |
| 106 |
#endif |
| 107 |
|
| 108 |
#ifdef __cplusplus |
| 109 |
namespace mediastreamer { |
| 110 |
|
| 111 |
class H264NaluType : public H26xNaluType { |
| 112 |
public: |
| 113 |
H264NaluType() = default; |
| 114 |
H264NaluType(uint8_t value); |
| 115 |
|
| 116 |
bool isVcl() const override { |
| 117 |
return _value < 6; |
| 118 |
} |
| 119 |
bool isParameterSet() const override { |
| 120 |
return *this == Sps || *this == Pps; |
| 121 |
} |
| 122 |
bool isKeyFramePart() const override { |
| 123 |
return *this == Idr || *this == DataPartA; |
| 124 |
} |
| 125 |
|
| 126 |
static const H264NaluType DataPartA; |
| 127 |
static const H264NaluType DataPartB; |
| 128 |
static const H264NaluType DataPartC; |
| 129 |
static const H264NaluType Idr; |
| 130 |
static const H264NaluType Sei; |
| 131 |
static const H264NaluType Sps; |
| 132 |
static const H264NaluType Pps; |
| 133 |
static const H264NaluType StapA; |
| 134 |
static const H264NaluType FuA; |
| 135 |
}; |
| 136 |
|
| 137 |
class H264NaluHeader : public H26xNaluHeader { |
| 138 |
public: |
| 139 |
H264NaluHeader() : H26xNaluHeader() { |
| 140 |
} |
| 141 |
H264NaluHeader(const uint8_t *header) { |
| 142 |
parse(header); |
| 143 |
} |
| 144 |
|
| 145 |
void setNri(uint8_t nri); |
| 146 |
uint8_t getNri() const { |
| 147 |
return _nri; |
| 148 |
} |
| 149 |
|
| 150 |
void setType(H264NaluType type) { |
| 151 |
_type = type; |
| 152 |
} |
| 153 |
H264NaluType getType() const { |
| 154 |
return _type; |
| 155 |
} |
| 156 |
|
| 157 |
const H26xNaluType &getAbsType() const override { |
| 158 |
return _type; |
| 159 |
} |
| 160 |
|
| 161 |
bool operator==(const H264NaluHeader &h2) const; |
| 162 |
bool operator!=(const H264NaluHeader &h2) const { |
| 163 |
return !(*this == h2); |
| 164 |
} |
| 165 |
|
| 166 |
void parse(const uint8_t *header) override; |
| 167 |
mblk_t *forge() const override; |
| 168 |
|
| 169 |
static const size_t length = 1; |
| 170 |
|
| 171 |
private: |
| 172 |
uint8_t _nri = 0; |
| 173 |
H264NaluType _type; |
| 174 |
}; |
| 175 |
|
| 176 |
class H264FrameAnalyser { |
| 177 |
public: |
| 178 |
struct Info { |
| 179 |
bool hasSps = false; |
| 180 |
bool hasPps = false; |
| 181 |
bool hasIdr = false; |
| 182 |
bool newSps = false; |
| 183 |
bool newPps = false; |
| 184 |
|
| 185 |
unsigned int toUInt() const; |
| 186 |
}; |
| 187 |
|
| 188 |
~H264FrameAnalyser(); |
| 189 |
Info analyse(const MSQueue *frame); |
| 190 |
|
| 191 |
private: |
| 192 |
bool updateParameterSet(const mblk_t *new_parameter_set); |
| 193 |
|
| 194 |
mblk_t *_lastSps = nullptr; |
| 195 |
mblk_t *_lastPps = nullptr; |
| 196 |
}; |
| 197 |
|
| 198 |
class H264Tools { |
| 199 |
public: |
| 200 |
static void nalHeaderInit(uint8_t *h, uint8_t nri, uint8_t type) { |
| 201 |
*h = ((nri & 0x3) << 5) | (type & ((1 << 5) - 1)); |
| 202 |
} |
| 203 |
static mblk_t *prependFuIndicatorAndHeader(mblk_t *m, uint8_t indicator, bool_t start, bool_t end, uint8_t type); |
| 204 |
}; |
| 205 |
|
| 206 |
class H264ParameterSetsInserter : public H26xParameterSetsInserter { |
| 207 |
public: |
| 208 |
~H264ParameterSetsInserter(); |
| 209 |
void process(MSQueue *in, MSQueue *out) override; |
| 210 |
void flush() override; |
| 211 |
|
| 212 |
private: |
| 213 |
mblk_t *_sps = nullptr; |
| 214 |
mblk_t *_pps = nullptr; |
| 215 |
}; |
| 216 |
|
| 217 |
class H264ParameterSetsStore : public H26xParameterSetsStore { |
| 218 |
public: |
| 219 |
H264ParameterSetsStore() : H26xParameterSetsStore("video/avc", {MSH264NaluTypeSPS, MSH264NaluTypePPS}) { |
| 220 |
} |
| 221 |
}; |
| 222 |
|
| 223 |
class H264ToolFactory : public H26xToolFactory { |
| 224 |
public: |
| 225 |
H26xNaluHeader *createNaluHeader() const override; |
| 226 |
NalPacker *createNalPacker(size_t maxPayloadeSize) const override; |
| 227 |
NalUnpacker *createNalUnpacker() const override; |
| 228 |
H26xParameterSetsInserter *createParameterSetsInserter() const override; |
| 229 |
H26xParameterSetsStore *createParameterSetsStore() const override; |
| 230 |
}; |
| 231 |
|
| 232 |
} // namespace mediastreamer |
| 233 |
|
| 234 |
#endif // __cplusplus |
| 235 |
|
| 236 |
#endif /* defined(H264_UTILS_H) */ |