Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
cbs.cpp
Go to the documentation of this file.
1
5extern "C" {
6// lib includes
7#include <libavcodec/avcodec.h>
8#include <libavcodec/cbs_h264.h>
9#include <libavcodec/cbs_h265.h>
10#include <libavcodec/h264_levels.h>
11#include <libavutil/pixdesc.h>
12}
13
14// local includes
15#include "cbs.h"
16#include "logging.h"
17#include "utility.h"
18
19using namespace std::literals;
20
21namespace cbs {
22 void close(CodedBitstreamContext *c) {
23 ff_cbs_close(&c);
24 }
25
26 using ctx_t = util::safe_ptr<CodedBitstreamContext, close>;
27
28 class frag_t: public CodedBitstreamFragment {
29 public:
30 frag_t(frag_t &&o) {
31 std::copy((std::uint8_t *) &o, (std::uint8_t *) (&o + 1), (std::uint8_t *) this);
32
33 o.data = nullptr;
34 o.units = nullptr;
35 };
36
37 frag_t() {
38 std::fill_n((std::uint8_t *) this, sizeof(*this), 0);
39 }
40
41 frag_t &operator=(frag_t &&o) {
42 std::copy((std::uint8_t *) &o, (std::uint8_t *) (&o + 1), (std::uint8_t *) this);
43
44 o.data = nullptr;
45 o.units = nullptr;
46
47 return *this;
48 };
49
50 ~frag_t() {
51 if (data || units) {
52 ff_cbs_fragment_free(this);
53 }
54 }
55 };
56
57 util::buffer_t<std::uint8_t> write(cbs::ctx_t &cbs_ctx, std::uint8_t nal, void *uh, AVCodecID codec_id) {
58 cbs::frag_t frag;
59 auto err = ff_cbs_insert_unit_content(&frag, -1, nal, uh, nullptr);
60 if (err < 0) {
61 char err_str[AV_ERROR_MAX_STRING_SIZE] {0};
62 BOOST_LOG(error) << "Could not insert NAL unit SPS: "sv << av_make_error_string(err_str, AV_ERROR_MAX_STRING_SIZE, err);
63
64 return {};
65 }
66
67 err = ff_cbs_write_fragment_data(cbs_ctx.get(), &frag);
68 if (err < 0) {
69 char err_str[AV_ERROR_MAX_STRING_SIZE] {0};
70 BOOST_LOG(error) << "Could not write fragment data: "sv << av_make_error_string(err_str, AV_ERROR_MAX_STRING_SIZE, err);
71
72 return {};
73 }
74
75 // frag.data_size * 8 - frag.data_bit_padding == bits in fragment
76 util::buffer_t<std::uint8_t> data {frag.data_size};
77 std::copy_n(frag.data, frag.data_size, std::begin(data));
78
79 return data;
80 }
81
82 util::buffer_t<std::uint8_t> write(std::uint8_t nal, void *uh, AVCodecID codec_id) {
83 cbs::ctx_t cbs_ctx;
84 ff_cbs_init(&cbs_ctx, codec_id, nullptr);
85
86 return write(cbs_ctx, nal, uh, codec_id);
87 }
88
89 h264_t make_sps_h264(const AVCodecContext *avctx, const AVPacket *packet) {
90 cbs::ctx_t ctx;
91 if (ff_cbs_init(&ctx, AV_CODEC_ID_H264, nullptr)) {
92 return {};
93 }
94
95 cbs::frag_t frag;
96
97 int err = ff_cbs_read_packet(ctx.get(), &frag, packet);
98 if (err < 0) {
99 char err_str[AV_ERROR_MAX_STRING_SIZE] {0};
100 BOOST_LOG(error) << "Couldn't read packet: "sv << av_make_error_string(err_str, AV_ERROR_MAX_STRING_SIZE, err);
101
102 return {};
103 }
104
105 auto sps_p = ((CodedBitstreamH264Context *) ctx->priv_data)->active_sps;
106
107 // This is a very large struct that cannot safely be stored on the stack
108 auto sps = std::make_unique<H264RawSPS>(*sps_p);
109
110 if (avctx->refs > 0) {
111 sps->max_num_ref_frames = avctx->refs;
112 }
113
114 sps->vui_parameters_present_flag = 1;
115
116 auto &vui = sps->vui;
117 std::memset(&vui, 0, sizeof(vui));
118
119 vui.video_format = 5;
120 vui.colour_description_present_flag = 1;
121 vui.video_signal_type_present_flag = 1;
122 vui.video_full_range_flag = avctx->color_range == AVCOL_RANGE_JPEG;
123 vui.colour_primaries = avctx->color_primaries;
124 vui.transfer_characteristics = avctx->color_trc;
125 vui.matrix_coefficients = avctx->colorspace;
126
127 vui.low_delay_hrd_flag = 1 - vui.fixed_frame_rate_flag;
128
129 vui.bitstream_restriction_flag = 1;
130 vui.motion_vectors_over_pic_boundaries_flag = 1;
131 vui.log2_max_mv_length_horizontal = 16;
132 vui.log2_max_mv_length_vertical = 16;
133 vui.max_num_reorder_frames = 0;
134 vui.max_dec_frame_buffering = sps->max_num_ref_frames;
135
136 cbs::ctx_t write_ctx;
137 ff_cbs_init(&write_ctx, AV_CODEC_ID_H264, nullptr);
138
139 return h264_t {
140 write(write_ctx, sps->nal_unit_header.nal_unit_type, (void *) &sps->nal_unit_header, AV_CODEC_ID_H264),
141 write(ctx, sps_p->nal_unit_header.nal_unit_type, (void *) &sps_p->nal_unit_header, AV_CODEC_ID_H264)
142 };
143 }
144
145 hevc_t make_sps_hevc(const AVCodecContext *avctx, const AVPacket *packet) {
146 cbs::ctx_t ctx;
147 if (ff_cbs_init(&ctx, AV_CODEC_ID_H265, nullptr)) {
148 return {};
149 }
150
151 cbs::frag_t frag;
152
153 int err = ff_cbs_read_packet(ctx.get(), &frag, packet);
154 if (err < 0) {
155 char err_str[AV_ERROR_MAX_STRING_SIZE] {0};
156 BOOST_LOG(error) << "Couldn't read packet: "sv << av_make_error_string(err_str, AV_ERROR_MAX_STRING_SIZE, err);
157
158 return {};
159 }
160
161 auto vps_p = ((CodedBitstreamH265Context *) ctx->priv_data)->active_vps;
162 auto sps_p = ((CodedBitstreamH265Context *) ctx->priv_data)->active_sps;
163
164 // These are very large structs that cannot safely be stored on the stack
165 auto sps = std::make_unique<H265RawSPS>(*sps_p);
166 auto vps = std::make_unique<H265RawVPS>(*vps_p);
167
168 vps->profile_tier_level.general_profile_compatibility_flag[4] = 1;
169 sps->profile_tier_level.general_profile_compatibility_flag[4] = 1;
170
171 auto &vui = sps->vui;
172 std::memset(&vui, 0, sizeof(vui));
173
174 sps->vui_parameters_present_flag = 1;
175
176 // skip sample aspect ratio
177
178 vui.video_format = 5;
179 vui.colour_description_present_flag = 1;
180 vui.video_signal_type_present_flag = 1;
181 vui.video_full_range_flag = avctx->color_range == AVCOL_RANGE_JPEG;
182 vui.colour_primaries = avctx->color_primaries;
183 vui.transfer_characteristics = avctx->color_trc;
184 vui.matrix_coefficients = avctx->colorspace;
185
186 vui.vui_timing_info_present_flag = vps->vps_timing_info_present_flag;
187 vui.vui_num_units_in_tick = vps->vps_num_units_in_tick;
188 vui.vui_time_scale = vps->vps_time_scale;
189 vui.vui_poc_proportional_to_timing_flag = vps->vps_poc_proportional_to_timing_flag;
190 vui.vui_num_ticks_poc_diff_one_minus1 = vps->vps_num_ticks_poc_diff_one_minus1;
191 vui.vui_hrd_parameters_present_flag = 0;
192
193 vui.bitstream_restriction_flag = 1;
194 vui.motion_vectors_over_pic_boundaries_flag = 1;
195 vui.restricted_ref_pic_lists_flag = 1;
196 vui.max_bytes_per_pic_denom = 0;
197 vui.max_bits_per_min_cu_denom = 0;
198 vui.log2_max_mv_length_horizontal = 15;
199 vui.log2_max_mv_length_vertical = 15;
200
201 cbs::ctx_t write_ctx;
202 ff_cbs_init(&write_ctx, AV_CODEC_ID_H265, nullptr);
203
204 return hevc_t {
205 nal_t {
206 write(write_ctx, vps->nal_unit_header.nal_unit_type, (void *) &vps->nal_unit_header, AV_CODEC_ID_H265),
207 write(ctx, vps_p->nal_unit_header.nal_unit_type, (void *) &vps_p->nal_unit_header, AV_CODEC_ID_H265),
208 },
209
210 nal_t {
211 write(write_ctx, sps->nal_unit_header.nal_unit_type, (void *) &sps->nal_unit_header, AV_CODEC_ID_H265),
212 write(ctx, sps_p->nal_unit_header.nal_unit_type, (void *) &sps_p->nal_unit_header, AV_CODEC_ID_H265),
213 },
214 };
215 }
216
222 bool validate_sps(const AVPacket *packet, int codec_id) {
223 cbs::ctx_t ctx;
224 if (ff_cbs_init(&ctx, (AVCodecID) codec_id, nullptr)) {
225 return false;
226 }
227
228 cbs::frag_t frag;
229
230 int err = ff_cbs_read_packet(ctx.get(), &frag, packet);
231 if (err < 0) {
232 char err_str[AV_ERROR_MAX_STRING_SIZE] {0};
233 BOOST_LOG(error) << "Couldn't read packet: "sv << av_make_error_string(err_str, AV_ERROR_MAX_STRING_SIZE, err);
234
235 return false;
236 }
237
238 if (codec_id == AV_CODEC_ID_H264) {
239 auto h264 = (CodedBitstreamH264Context *) ctx->priv_data;
240
241 if (!h264->active_sps->vui_parameters_present_flag) {
242 return false;
243 }
244
245 return true;
246 }
247
248 return ((CodedBitstreamH265Context *) ctx->priv_data)->active_sps->vui_parameters_present_flag;
249 }
250} // namespace cbs
bool validate_sps(const AVPacket *packet, int codec_id)
Validates the Sequence Parameter Set (SPS) of a given packet.
Definition cbs.cpp:222
Declarations for FFmpeg Coded Bitstream API.
Declarations for logging related functions.
Declarations for utility functions.