Hermes 0.7.0
Linux game streaming host
Loading...
Searching...
No Matches
annexb_filler.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <cstddef>
8#include <cstdint>
9#include <cstring>
10
11namespace video::annexb {
12
14 enum class codec_e {
15 h264,
16 hevc,
17 };
18
19 namespace detail {
21 inline std::size_t find_start_code(const std::uint8_t *data, std::size_t from, std::size_t size) {
22 while (size >= 3 && from + 3 <= size) {
23 const auto *one = static_cast<const std::uint8_t *>(std::memchr(data + from + 2, 0x01, size - from - 2));
24 if (!one) {
25 break;
26 }
27 const auto at = static_cast<std::size_t>(one - data);
28 if (data[at - 1] == 0 && data[at - 2] == 0) {
29 return at - 2;
30 }
31 from = at - 1;
32 }
33 return size;
34 }
35
37 inline bool is_filler(std::uint8_t header, codec_e codec) {
38 return codec == codec_e::h264 ? (header & 0x1f) == 12 : ((header >> 1) & 0x3f) == 38;
39 }
40 } // namespace detail
41
59 inline std::size_t strip_filler_data(std::uint8_t *data, std::size_t size, codec_e codec) {
60 auto code = detail::find_start_code(data, 0, size);
61 if (code == size) {
62 return size;
63 }
64
65 std::size_t begin = 0;
66 std::size_t kept = 0;
67 bool removed = false;
68 bool anything_else = false;
69 while (begin < size) {
70 const auto header = code + 3;
71 const auto next_code = header < size ? detail::find_start_code(data, header, size) : size;
72 auto next_begin = next_code;
73 while (next_code < size && next_begin > header && data[next_begin - 1] == 0) {
74 --next_begin;
75 }
76
77 if (header >= size || detail::is_filler(data[header], codec)) {
78 removed = true;
79 } else {
80 if (kept != begin) {
81 std::memmove(data + kept, data + begin, next_begin - begin);
82 }
83 kept += next_begin - begin;
84 anything_else = true;
85 }
86
87 begin = next_begin;
88 code = next_code;
89 }
90
91 return removed && anything_else ? kept : size;
92 }
93
94} // namespace video::annexb
std::size_t strip_filler_data(std::uint8_t *data, std::size_t size, codec_e codec)
codec_e
Which bitstream syntax a frame uses.
bool is_filler(std::uint8_t header, codec_e codec)
Filler data: H.264 nal_unit_type 12, HEVC nal_unit_type 38 (FD_NUT).
std::size_t find_start_code(const std::uint8_t *data, std::size_t from, std::size_t size)
Offset of the next 00 00 01 start code at or after from, or size.