From 629bf6b8ffef538d072a62d9e5b5bfc921ec5d24 Mon Sep 17 00:00:00 2001 From: nquah Date: Fri, 24 Mar 2023 20:02:07 -0400 Subject: [PATCH] obs-outputs: Adds AV1, HEVC via RTMP to YouTube. Implements Enhanced RTMP spec at https://github.com/veovera/enhanced-rtmp, specifically adding AV1 and HEVC support, and enabling streaming to YouTube. Note that this feature is in beta at YouTube. Additionally adds flag-guarded HDR metadata frame support, according to the spec. --- UI/data/locale/en-US.ini | 1 + UI/window-basic-main.cpp | 4 + libobs/CMakeLists.txt | 1 + libobs/obs-bits.h | 79 ++ libobs/obs-defs.h | 1 + libobs/obs-hevc.c | 44 - libobs/obs-hevc.h | 44 + libobs/obs-nal.c | 12 + libobs/obs-nal.h | 2 + plugins/obs-outputs/CMakeLists.txt | 6 + plugins/obs-outputs/flv-mux.c | 238 ++++- plugins/obs-outputs/flv-mux.h | 12 + plugins/obs-outputs/rtmp-av1.c | 604 +++++++++++ plugins/obs-outputs/rtmp-av1.h | 35 + plugins/obs-outputs/rtmp-hevc.c | 948 ++++++++++++++++++ plugins/obs-outputs/rtmp-hevc.h | 31 + plugins/obs-outputs/rtmp-stream.c | 254 ++++- plugins/obs-outputs/rtmp-stream.h | 1 - .../data/schema/service-schema-v4.json | 3 +- plugins/rtmp-services/data/services.json | 5 + 20 files changed, 2260 insertions(+), 65 deletions(-) create mode 100644 libobs/obs-bits.h create mode 100644 plugins/obs-outputs/rtmp-av1.c create mode 100644 plugins/obs-outputs/rtmp-av1.h create mode 100644 plugins/obs-outputs/rtmp-hevc.c create mode 100644 plugins/obs-outputs/rtmp-hevc.h diff --git a/UI/data/locale/en-US.ini b/UI/data/locale/en-US.ini index f08a95b1db27c2..326cb66be64097 100644 --- a/UI/data/locale/en-US.ini +++ b/UI/data/locale/en-US.ini @@ -402,6 +402,7 @@ Output.ConnectFail.Title="Failed to connect" Output.ConnectFail.BadPath="Invalid Path or Connection URL. Please check your settings to confirm that they are valid." Output.ConnectFail.ConnectFailed="Failed to connect to server" Output.ConnectFail.InvalidStream="Could not access the specified channel or stream key, please double-check your stream key. If it is correct, there may be a problem connecting to the server." +Output.ConnectFail.HdrDisabled="This color configuration over RTMP is currently disabled for this codec" Output.ConnectFail.Error="An unexpected error occurred when trying to connect to the server. More information in the log file." Output.ConnectFail.Disconnected="Disconnected from server." diff --git a/UI/window-basic-main.cpp b/UI/window-basic-main.cpp index 4e81e78a09b518..9b7786ade937c5 100644 --- a/UI/window-basic-main.cpp +++ b/UI/window-basic-main.cpp @@ -7252,6 +7252,10 @@ void OBSBasic::StreamingStop(int code, QString last_error) encode_error = true; break; + case OBS_OUTPUT_HDR_DISABLED: + errorDescription = Str("Output.ConnectFail.HdrDisabled"); + break; + default: case OBS_OUTPUT_ERROR: use_last_error = true; diff --git a/libobs/CMakeLists.txt b/libobs/CMakeLists.txt index c69557e3e31bc2..ec4ebdf8cf5215 100644 --- a/libobs/CMakeLists.txt +++ b/libobs/CMakeLists.txt @@ -30,6 +30,7 @@ target_sources( obs-audio-controls.h obs-avc.c obs-avc.h + obs-bits.h obs-data.c obs-data.h obs-defs.h diff --git a/libobs/obs-bits.h b/libobs/obs-bits.h new file mode 100644 index 00000000000000..93cc5c71d6bfef --- /dev/null +++ b/libobs/obs-bits.h @@ -0,0 +1,79 @@ +#pragma once + +#if defined(__GNUC__) || defined(__clang__) + +#define clz32(x) __builtin_clz(x) +#define ctz32(x) __builtin_ctz(x) + +#elif defined(_MSC_VER) && _MSC_VER >= 1400 + +static inline int clz32(unsigned long val) +{ + uint32_t zeros = 0; + _BitScanReverse(&zeros, val); + return 31 - zeros; +} +static inline int ctz32(unsigned long val) +{ + uint32_t zeros = 0; + _BitScanForward(&zeros, val); + return zeros; +} + +#else + +static uint32_t popcnt(uint32_t x) +{ + x -= ((x >> 1) & 0x55555555); + x = (((x >> 2) & 0x33333333) + (x & 0x33333333)); + x = (((x >> 4) + x) & 0x0f0f0f0f); + x += (x >> 8); + x += (x >> 16); + return x & 0x0000003f; +} +static uint32_t clz32(uint32_t x) +{ + x |= (x >> 1); + x |= (x >> 2); + x |= (x >> 4); + x |= (x >> 8); + x |= (x >> 16); + return 32 - popcnt(x); +} +static uint32_t ctz32(uint32_t x) +{ + return popcnt((x & -x) - 1); +} + +#endif + +#ifdef MIN +#undef MIN +#endif +static inline int MIN(int x, int y) +{ + return x < y ? x : y; +} + +#ifdef MAX +#undef MAX +#endif +static inline int MAX(int x, int y) +{ + return x > y ? x : y; +} + +static inline uint32_t RB32(const uint8_t *ptr) +{ + return (ptr[0] << 24) + (ptr[1] << 16) + (ptr[2] << 8) + ptr[3]; +} + +static inline uint32_t av_bswap32(uint32_t x) +{ + return (((x << 8) & 0xff00) | ((x >> 8) & 0x00ff)); +} +static inline uint64_t av_bswap64(uint64_t x) +{ + return ((uint64_t)av_bswap32((uint32_t)x) << 32) | + av_bswap32((uint32_t)(x >> 32)); +} diff --git a/libobs/obs-defs.h b/libobs/obs-defs.h index b3e224d42c45f0..141b03fa115f60 100644 --- a/libobs/obs-defs.h +++ b/libobs/obs-defs.h @@ -42,6 +42,7 @@ #define OBS_OUTPUT_UNSUPPORTED -6 #define OBS_OUTPUT_NO_SPACE -7 #define OBS_OUTPUT_ENCODE_ERROR -8 +#define OBS_OUTPUT_HDR_DISABLED -9 #define OBS_VIDEO_SUCCESS 0 #define OBS_VIDEO_FAIL -1 diff --git a/libobs/obs-hevc.c b/libobs/obs-hevc.c index 84ee2616c2a245..37c4e0e48206eb 100644 --- a/libobs/obs-hevc.c +++ b/libobs/obs-hevc.c @@ -21,50 +21,6 @@ #include "obs-nal.h" #include "util/array-serializer.h" -enum { - OBS_HEVC_NAL_TRAIL_N = 0, - OBS_HEVC_NAL_TRAIL_R = 1, - OBS_HEVC_NAL_TSA_N = 2, - OBS_HEVC_NAL_TSA_R = 3, - OBS_HEVC_NAL_STSA_N = 4, - OBS_HEVC_NAL_STSA_R = 5, - OBS_HEVC_NAL_RADL_N = 6, - OBS_HEVC_NAL_RADL_R = 7, - OBS_HEVC_NAL_RASL_N = 8, - OBS_HEVC_NAL_RASL_R = 9, - OBS_HEVC_NAL_VCL_N10 = 10, - OBS_HEVC_NAL_VCL_R11 = 11, - OBS_HEVC_NAL_VCL_N12 = 12, - OBS_HEVC_NAL_VCL_R13 = 13, - OBS_HEVC_NAL_VCL_N14 = 14, - OBS_HEVC_NAL_VCL_R15 = 15, - OBS_HEVC_NAL_BLA_W_LP = 16, - OBS_HEVC_NAL_BLA_W_RADL = 17, - OBS_HEVC_NAL_BLA_N_LP = 18, - OBS_HEVC_NAL_IDR_W_RADL = 19, - OBS_HEVC_NAL_IDR_N_LP = 20, - OBS_HEVC_NAL_CRA_NUT = 21, - OBS_HEVC_NAL_RSV_IRAP_VCL22 = 22, - OBS_HEVC_NAL_RSV_IRAP_VCL23 = 23, - OBS_HEVC_NAL_RSV_VCL24 = 24, - OBS_HEVC_NAL_RSV_VCL25 = 25, - OBS_HEVC_NAL_RSV_VCL26 = 26, - OBS_HEVC_NAL_RSV_VCL27 = 27, - OBS_HEVC_NAL_RSV_VCL28 = 28, - OBS_HEVC_NAL_RSV_VCL29 = 29, - OBS_HEVC_NAL_RSV_VCL30 = 30, - OBS_HEVC_NAL_RSV_VCL31 = 31, - OBS_HEVC_NAL_VPS = 32, - OBS_HEVC_NAL_SPS = 33, - OBS_HEVC_NAL_PPS = 34, - OBS_HEVC_NAL_AUD = 35, - OBS_HEVC_NAL_EOS_NUT = 36, - OBS_HEVC_NAL_EOB_NUT = 37, - OBS_HEVC_NAL_FD_NUT = 38, - OBS_HEVC_NAL_SEI_PREFIX = 39, - OBS_HEVC_NAL_SEI_SUFFIX = 40, -}; - bool obs_hevc_keyframe(const uint8_t *data, size_t size) { const uint8_t *nal_start, *nal_end; diff --git a/libobs/obs-hevc.h b/libobs/obs-hevc.h index 6313c30962b87f..91da1c35f92faa 100644 --- a/libobs/obs-hevc.h +++ b/libobs/obs-hevc.h @@ -25,6 +25,50 @@ extern "C" { struct encoder_packet; +enum { + OBS_HEVC_NAL_TRAIL_N = 0, + OBS_HEVC_NAL_TRAIL_R = 1, + OBS_HEVC_NAL_TSA_N = 2, + OBS_HEVC_NAL_TSA_R = 3, + OBS_HEVC_NAL_STSA_N = 4, + OBS_HEVC_NAL_STSA_R = 5, + OBS_HEVC_NAL_RADL_N = 6, + OBS_HEVC_NAL_RADL_R = 7, + OBS_HEVC_NAL_RASL_N = 8, + OBS_HEVC_NAL_RASL_R = 9, + OBS_HEVC_NAL_VCL_N10 = 10, + OBS_HEVC_NAL_VCL_R11 = 11, + OBS_HEVC_NAL_VCL_N12 = 12, + OBS_HEVC_NAL_VCL_R13 = 13, + OBS_HEVC_NAL_VCL_N14 = 14, + OBS_HEVC_NAL_VCL_R15 = 15, + OBS_HEVC_NAL_BLA_W_LP = 16, + OBS_HEVC_NAL_BLA_W_RADL = 17, + OBS_HEVC_NAL_BLA_N_LP = 18, + OBS_HEVC_NAL_IDR_W_RADL = 19, + OBS_HEVC_NAL_IDR_N_LP = 20, + OBS_HEVC_NAL_CRA_NUT = 21, + OBS_HEVC_NAL_RSV_IRAP_VCL22 = 22, + OBS_HEVC_NAL_RSV_IRAP_VCL23 = 23, + OBS_HEVC_NAL_RSV_VCL24 = 24, + OBS_HEVC_NAL_RSV_VCL25 = 25, + OBS_HEVC_NAL_RSV_VCL26 = 26, + OBS_HEVC_NAL_RSV_VCL27 = 27, + OBS_HEVC_NAL_RSV_VCL28 = 28, + OBS_HEVC_NAL_RSV_VCL29 = 29, + OBS_HEVC_NAL_RSV_VCL30 = 30, + OBS_HEVC_NAL_RSV_VCL31 = 31, + OBS_HEVC_NAL_VPS = 32, + OBS_HEVC_NAL_SPS = 33, + OBS_HEVC_NAL_PPS = 34, + OBS_HEVC_NAL_AUD = 35, + OBS_HEVC_NAL_EOS_NUT = 36, + OBS_HEVC_NAL_EOB_NUT = 37, + OBS_HEVC_NAL_FD_NUT = 38, + OBS_HEVC_NAL_SEI_PREFIX = 39, + OBS_HEVC_NAL_SEI_SUFFIX = 40, +}; + EXPORT bool obs_hevc_keyframe(const uint8_t *data, size_t size); EXPORT void obs_parse_hevc_packet(struct encoder_packet *hevc_packet, const struct encoder_packet *src); diff --git a/libobs/obs-nal.c b/libobs/obs-nal.c index add217be096ba4..c392902ef378f4 100644 --- a/libobs/obs-nal.c +++ b/libobs/obs-nal.c @@ -65,3 +65,15 @@ const uint8_t *obs_nal_find_startcode(const uint8_t *p, const uint8_t *end) out--; return out; } + +const uint8_t *obs_nal_find_next_startcode(const uint8_t *p, const uint8_t *end) +{ + if (p + 3 >= end) + return end; + + for (end -= 3; p < end; p++) { + if (p[0] == 0 && p[1] == 0 && p[2] == 1) + return p; + } + return end + 3; +} diff --git a/libobs/obs-nal.h b/libobs/obs-nal.h index e9488c056eeff5..5111446fb13927 100644 --- a/libobs/obs-nal.h +++ b/libobs/obs-nal.h @@ -32,6 +32,8 @@ enum { EXPORT const uint8_t *obs_nal_find_startcode(const uint8_t *p, const uint8_t *end); +EXPORT const uint8_t *obs_nal_find_next_startcode(const uint8_t *p, + const uint8_t *end); #ifdef __cplusplus } diff --git a/plugins/obs-outputs/CMakeLists.txt b/plugins/obs-outputs/CMakeLists.txt index 386b4f04ac10ec..ff33b3929a8798 100644 --- a/plugins/obs-outputs/CMakeLists.txt +++ b/plugins/obs-outputs/CMakeLists.txt @@ -28,6 +28,8 @@ target_sources( rtmp-stream.c rtmp-stream.h rtmp-windows.c + rtmp-av1.c + rtmp-av1.h librtmp/amf.c librtmp/amf.h librtmp/bytes.h @@ -45,6 +47,10 @@ target_sources( librtmp/rtmp.h librtmp/rtmp_sys.h) +if(ENABLE_HEVC) + target_sources(libobs PRIVATE rtmp-hevc.c rtmp-hevc.h) +endif() + target_link_libraries(obs-outputs PRIVATE OBS::libobs) set_target_properties(obs-outputs PROPERTIES FOLDER "plugins" PREFIX "") diff --git a/plugins/obs-outputs/flv-mux.c b/plugins/obs-outputs/flv-mux.c index 9e4d061d4f279d..cbedc34ac5f5bc 100644 --- a/plugins/obs-outputs/flv-mux.c +++ b/plugins/obs-outputs/flv-mux.c @@ -32,6 +32,87 @@ #define VIDEODATA_AVCVIDEOPACKET 7.0 #define AUDIODATA_AAC 10.0 +#define VIDEO_FRAMETYPE_OFFSET 4 +enum video_frametype_t { + FT_KEY = 1 << VIDEO_FRAMETYPE_OFFSET, + FT_INTER = 2 << VIDEO_FRAMETYPE_OFFSET, +}; + +// Y2023 spec +const uint8_t FRAME_HEADER_EX = 8 << VIDEO_FRAMETYPE_OFFSET; +enum packet_type_t { + PACKETTYPE_SEQ_START = 0, + PACKETTYPE_FRAMES = 1, + PACKETTYPE_SEQ_END = 2, +#ifdef ENABLE_HEVC + PACKETTYPE_FRAMESX = 3, +#endif + PACKETTYPE_METADATA = 4 +}; + +enum video_id_t { + CODEC_H264 = 1, // legacy + CODEC_AV1, // Y2023 spec +#ifdef ENABLE_HEVC + CODEC_HEVC, +#endif +}; + +static enum video_id_t to_video_type(const char *codec) +{ + if (strcmp(codec, "h264") == 0) + return CODEC_H264; + if (strcmp(codec, "av1") == 0) + return CODEC_AV1; +#ifdef ENABLE_HEVC + if (strcmp(codec, "hevc") == 0) + return CODEC_HEVC; +#endif + return 0; +} + +enum datatype_t { + DATA_TYPE_NUMBER = 0, + DATA_TYPE_STRING = 2, + DATA_TYPE_OBJECT = 3, + DATA_TYPE_OBJECT_END = 9, +}; + +static void s_w4cc(struct serializer *s, enum video_id_t id) +{ + switch (id) { + case CODEC_AV1: + s_w8(s, 'a'); + s_w8(s, 'v'); + s_w8(s, '0'); + s_w8(s, '1'); + break; +#ifdef ENABLE_HEVC + case CODEC_HEVC: + s_w8(s, 'h'); + s_w8(s, 'v'); + s_w8(s, 'c'); + s_w8(s, '1'); + break; +#endif + case CODEC_H264: + assert(0); + } +} + +static void s_wstring(struct serializer *s, const char *str) +{ + size_t len = strlen(str); + s_wb16(s, (uint16_t)len); + s_write(s, str, len); +} + +static inline void s_wtimestamp(struct serializer *s, int32_t i32) +{ + s_wb24(s, (uint32_t)(i32 & 0xFFFFFF)); + s_w8(s, (uint32_t)(i32 >> 24) & 0x7F); +} + static inline double encoder_bitrate(obs_encoder_t *encoder) { obs_data_t *settings = obs_encoder_get_settings(encoder); @@ -189,7 +270,7 @@ static void flv_video(struct serializer *s, int32_t dts_offset, #endif s_wb24(s, (uint32_t)packet->size + 5); - s_wb24(s, time_ms); + s_wb24(s, (uint32_t)time_ms); s_w8(s, (time_ms >> 24) & 0x7F); s_wb24(s, 0); @@ -223,7 +304,7 @@ static void flv_audio(struct serializer *s, int32_t dts_offset, #endif s_wb24(s, (uint32_t)packet->size + 2); - s_wb24(s, time_ms); + s_wb24(s, (uint32_t)time_ms); s_w8(s, (time_ms >> 24) & 0x7F); s_wb24(s, 0); @@ -253,6 +334,157 @@ void flv_packet_mux(struct encoder_packet *packet, int32_t dts_offset, *size = data.bytes.num; } +// Y2023 spec +void flv_packet_ex(struct encoder_packet *packet, int32_t dts_offset, + uint8_t **output, size_t *size, int type) +{ + struct array_output_data data; + struct serializer s; + array_output_serializer_init(&s, &data); + + assert(packet->type == OBS_ENCODER_VIDEO); + + int64_t offset = packet->pts - packet->dts; + int32_t time_ms = get_ms_time(packet, packet->dts) - dts_offset; + + // packet head + s_w8(&s, RTMP_PACKET_TYPE_VIDEO); + s_wb24(&s, (uint32_t)packet->size + 5); // 5 = (w8+w4cc) + s_wtimestamp(&s, time_ms); + s_wb24(&s, 0); // always 0 + + const char *codec = obs_encoder_get_codec(packet->encoder); + enum video_id_t codec_id = to_video_type(codec); + + // packet ext header + s_w8(&s, FRAME_HEADER_EX | type | (packet->keyframe ? FT_KEY : 0)); + s_w4cc(&s, codec_id); + // packet data + s_write(&s, packet->data, packet->size); + + // packet tail + s_wb32(&s, (uint32_t)serializer_get_pos(&s) - 1); + + *output = data.bytes.array; + *size = data.bytes.num; +} + +void flv_packet_start(struct encoder_packet *packet, int32_t dts_offset, + uint8_t **output, size_t *size) +{ + flv_packet_ex(packet, dts_offset, output, size, PACKETTYPE_SEQ_START); +} + +void flv_packet_frames(struct encoder_packet *packet, int32_t dts_offset, + uint8_t **output, size_t *size) +{ + const char *codec = obs_encoder_get_codec(packet->encoder); +#ifdef ENABLE_HEVC + flv_packet_ex(packet, dts_offset, output, size, + (strcmp(codec, "hevc") == 0) ? PACKETTYPE_FRAMESX + : PACKETTYPE_FRAMES); +#else + flv_packet_ex(packet, dts_offset, output, size, PACKETTYPE_FRAMES); +#endif +} + +void flv_packet_end(struct encoder_packet *packet, int32_t dts_offset, + uint8_t **output, size_t *size) +{ + flv_packet_ex(packet, dts_offset, output, size, PACKETTYPE_SEQ_END); +} + +void flv_packet_metadata(obs_encoder_t *encoder, uint8_t **output, size_t *size, + int bits_per_raw_sample, uint8_t color_primaries, + int color_trc, int color_space, int min_luminance, + int max_luminance) +{ + // metadata array + struct array_output_data data; + struct array_output_data metadata; + struct serializer s; + array_output_serializer_init(&s, &data); + + // metadata data array + { + struct serializer s; + array_output_serializer_init(&s, &metadata); + + s_w8(&s, DATA_TYPE_STRING); + s_wstring(&s, "colorInfo"); + s_w8(&s, DATA_TYPE_OBJECT); + { + // colorConfig: + s_wstring(&s, "colorConfig"); + s_w8(&s, DATA_TYPE_OBJECT); + { + s_wstring(&s, "bitDepth"); + s_w8(&s, DATA_TYPE_NUMBER); + s_wbd(&s, bits_per_raw_sample); + + s_wstring(&s, "colorPrimaries"); + s_w8(&s, DATA_TYPE_NUMBER); + s_wbd(&s, color_primaries); + + s_wstring(&s, "transferCharacteristics"); + s_w8(&s, DATA_TYPE_NUMBER); + s_wbd(&s, color_trc); + + s_wstring(&s, "matrixCoefficients"); + s_w8(&s, DATA_TYPE_NUMBER); + s_wbd(&s, color_space); + } + s_w8(&s, 0); + s_w8(&s, 0); + s_w8(&s, DATA_TYPE_OBJECT_END); + + if (max_luminance != 0) { + // hdrMdcv + s_wstring(&s, "hdrMdcv"); + s_w8(&s, DATA_TYPE_OBJECT); + { + s_wstring(&s, "maxLuminance"); + s_w8(&s, DATA_TYPE_NUMBER); + s_wbd(&s, max_luminance); + + s_wstring(&s, "minLuminance"); + s_w8(&s, DATA_TYPE_NUMBER); + s_wbd(&s, min_luminance); + } + s_w8(&s, 0); + s_w8(&s, 0); + s_w8(&s, DATA_TYPE_OBJECT_END); + } + } + s_w8(&s, 0); + s_w8(&s, 0); + s_w8(&s, DATA_TYPE_OBJECT_END); + } + + // packet head + s_w8(&s, RTMP_PACKET_TYPE_VIDEO); + s_wb24(&s, (uint32_t)metadata.bytes.num + 5); // 5 = (w8+w4cc) + s_wtimestamp(&s, 0); + s_wb24(&s, 0); // always 0 + + const char *codec = obs_encoder_get_codec(encoder); + enum video_id_t codec_id = to_video_type(codec); + + // packet ext header + // these are the 5 extra bytes mentioned above + s_w8(&s, FRAME_HEADER_EX | PACKETTYPE_METADATA); + s_w4cc(&s, codec_id); + // packet data + s_write(&s, metadata.bytes.array, metadata.bytes.num); + array_output_serializer_free(&metadata); // must be freed + + // packet tail + s_wb32(&s, (uint32_t)serializer_get_pos(&s) - 1); + + *output = data.bytes.array; + *size = data.bytes.num; +} + /* ------------------------------------------------------------------------- */ /* stuff for additional media streams */ @@ -471,7 +703,7 @@ static void flv_additional_audio(struct serializer *s, int32_t dts_offset, #endif s_wb24(s, (uint32_t)size); - s_wb24(s, time_ms); + s_wb24(s, (uint32_t)time_ms); s_w8(s, (time_ms >> 24) & 0x7F); s_wb24(s, 0); diff --git a/plugins/obs-outputs/flv-mux.h b/plugins/obs-outputs/flv-mux.h index c612f3882b3c99..9ed019696542f5 100644 --- a/plugins/obs-outputs/flv-mux.h +++ b/plugins/obs-outputs/flv-mux.h @@ -38,3 +38,15 @@ extern void flv_additional_packet_mux(struct encoder_packet *packet, int32_t dts_offset, uint8_t **output, size_t *size, bool is_header, size_t index); +// Y2023 spec +extern void flv_packet_start(struct encoder_packet *packet, int32_t dts_offset, + uint8_t **output, size_t *size); +extern void flv_packet_frames(struct encoder_packet *packet, int32_t dts_offset, + uint8_t **output, size_t *size); +extern void flv_packet_end(struct encoder_packet *packet, int32_t dts_offset, + uint8_t **output, size_t *size); +extern void flv_packet_metadata(obs_encoder_t *encoder, uint8_t **output, + size_t *size, int bits_per_raw_sample, + uint8_t color_primaries, int color_trc, + int color_space, int min_luminance, + int max_luminance); diff --git a/plugins/obs-outputs/rtmp-av1.c b/plugins/obs-outputs/rtmp-av1.c new file mode 100644 index 00000000000000..82de02a106ac0b --- /dev/null +++ b/plugins/obs-outputs/rtmp-av1.c @@ -0,0 +1,604 @@ +/****************************************************************************** + Copyright (C) 2023 by Hugh Bailey + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . + + Portion of code ripped from FFMpeg: libavformat/av1.c +******************************************************************************/ + +#include "rtmp-av1.h" +#include "obs-bits.h" + +#include "obs.h" +#include "util/array-serializer.h" + +#define AV1_OBU_SEQUENCE_HEADER 1 +#define AV1_OBU_TEMPORAL_DELIMITER 2 +#define AV1_OBU_REDUNDANT_FRAME_HEADER 7 +#define AV1_OBU_TILE_LIST 8 +#define AV1_OBU_PADDING 15 +#define AV1_OBU_METADATA 5 + +#define AV1_OBU_TILE_GROUP 4 +#define AV1_OBU_TILE_LIST 8 +#define AV1_OBU_FRAME 6 + +#define FF_PROFILE_AV1_MAIN 0 +#define FF_PROFILE_AV1_HIGH 1 +#define FF_PROFILE_AV1_PROFESSIONAL 2 + +typedef struct AV1SequenceParameters { + uint8_t profile; + uint8_t level; + uint8_t tier; + uint8_t bitdepth; + uint8_t monochrome; + uint8_t chroma_subsampling_x; + uint8_t chroma_subsampling_y; + uint8_t chroma_sample_position; + uint8_t color_description_present_flag; + uint8_t color_primaries; + uint8_t transfer_characteristics; + uint8_t matrix_coefficients; + uint8_t color_range; +} AV1SequenceParameters; + +#define MAX_OBU_HEADER_SIZE (2 + 8) + +typedef struct Av1GetBitContext { + const uint8_t *buffer, *buffer_end; + int index; + int size_in_bits; + int size_in_bits_plus8; +} Av1GetBitContext; + +static inline int init_get_bits_xe(Av1GetBitContext *s, const uint8_t *buffer, + int bit_size) +{ + int buffer_size; + int ret = 0; + + if (bit_size >= INT_MAX - MAX(7, 64 * 8) || bit_size < 0 || !buffer) { + bit_size = 0; + buffer = NULL; + ret = -1; + } + + buffer_size = (bit_size + 7) >> 3; + + s->buffer = buffer; + s->size_in_bits = bit_size; + s->size_in_bits_plus8 = bit_size + 8; + s->buffer_end = buffer + buffer_size; + s->index = 0; + + return ret; +} + +static inline int init_get_bits(Av1GetBitContext *s, const uint8_t *buffer, + int bit_size) +{ + return init_get_bits_xe(s, buffer, bit_size); +} + +static inline int init_get_bits8(Av1GetBitContext *s, const uint8_t *buffer, + int byte_size) +{ + if (byte_size > INT_MAX / 8 || byte_size < 0) + byte_size = -1; + return init_get_bits(s, buffer, byte_size * 8); +} + +static inline unsigned int get_bit1(Av1GetBitContext *s) +{ + unsigned int index = s->index; + uint8_t result = s->buffer[index >> 3]; + + result <<= index & 7; + result >>= 8 - 1; + + if (s->index < s->size_in_bits_plus8) + index++; + s->index = index; + + return result; +} + +static inline unsigned int get_bits(Av1GetBitContext *s, unsigned int n) +{ + unsigned int out = 0; + for (unsigned int i = 0; i < n; i++) + out = (out << 1) | get_bit1(s); + return out; +} + +#define skip_bits get_bits + +static inline int get_bits_count(Av1GetBitContext *s) +{ + return s->index; +} + +static inline int get_bits_left(Av1GetBitContext *gb) +{ + return gb->size_in_bits - get_bits_count(gb); +} + +#define get_bits_long get_bits +#define skip_bits_long get_bits_long + +static inline int64_t leb128(Av1GetBitContext *gb) +{ + int64_t ret = 0; + int i; + + for (i = 0; i < 8; i++) { + int byte = get_bits(gb, 8); + ret |= (int64_t)(byte & 0x7f) << (i * 7); + if (!(byte & 0x80)) + break; + } + return ret; +} + +static inline void uvlc(Av1GetBitContext *gb) +{ + int leading_zeros = 0; + + while (get_bits_left(gb)) { + if (get_bits(gb, 1)) + break; + leading_zeros++; + } + + if (leading_zeros >= 32) + return; + + skip_bits_long(gb, leading_zeros); +} + +static inline int parse_obu_header(const uint8_t *buf, int buf_size, + int64_t *obu_size, int *start_pos, int *type, + int *temporal_id, int *spatial_id) +{ + Av1GetBitContext gb; + int ret, extension_flag, has_size_flag; + size_t size; + + ret = init_get_bits8(&gb, buf, MIN(buf_size, MAX_OBU_HEADER_SIZE)); + if (ret < 0) + return ret; + + if (get_bits(&gb, 1) != 0) // obu_forbidden_bit + return -1; + + *type = get_bits(&gb, 4); + extension_flag = get_bits(&gb, 1); + has_size_flag = get_bits(&gb, 1); + skip_bits(&gb, 1); // obu_reserved_1bit + + if (extension_flag) { + *temporal_id = get_bits(&gb, 3); + *spatial_id = get_bits(&gb, 2); + skip_bits(&gb, 3); // extension_header_reserved_3bits + } else { + *temporal_id = *spatial_id = 0; + } + + *obu_size = has_size_flag ? leb128(&gb) : buf_size - 1 - extension_flag; + + if (get_bits_left(&gb) < 0) + return -1; + + *start_pos = get_bits_count(&gb) / 8; + + size = *obu_size + *start_pos; + + if (size > (size_t)buf_size) + return -1; + + assert(size <= INT_MAX); + return (int)size; +} + +static inline int get_obu_bit_length(const uint8_t *buf, int size, int type) +{ + int v; + + /* There are no trailing bits on these */ + if (type == AV1_OBU_TILE_GROUP || type == AV1_OBU_TILE_LIST || + type == AV1_OBU_FRAME) { + if (size > INT_MAX / 8) + return -1; + else + return size * 8; + } + + while (size > 0 && buf[size - 1] == 0) + size--; + + if (!size) + return 0; + + v = buf[size - 1]; + + if (size > INT_MAX / 8) + return -1; + size *= 8; + + /* Remove the trailing_one_bit and following trailing zeros */ + if (v) + size -= ctz32(v) + 1; + + return size; +} + +static int parse_color_config(AV1SequenceParameters *seq_params, + Av1GetBitContext *gb) +{ + int twelve_bit = 0; + int high_bitdepth = get_bits(gb, 1); + if (seq_params->profile == FF_PROFILE_AV1_PROFESSIONAL && high_bitdepth) + twelve_bit = get_bits(gb, 1); + + seq_params->bitdepth = 8 + (high_bitdepth * 2) + (twelve_bit * 2); + + if (seq_params->profile == FF_PROFILE_AV1_HIGH) + seq_params->monochrome = 0; + else + seq_params->monochrome = get_bits(gb, 1); + + seq_params->color_description_present_flag = get_bits(gb, 1); + if (seq_params->color_description_present_flag) { + seq_params->color_primaries = get_bits(gb, 8); + seq_params->transfer_characteristics = get_bits(gb, 8); + seq_params->matrix_coefficients = get_bits(gb, 8); + } else { + seq_params->color_primaries = 2; + seq_params->transfer_characteristics = 2; + seq_params->matrix_coefficients = 2; + } + + if (seq_params->monochrome) { + seq_params->color_range = get_bits(gb, 1); + seq_params->chroma_subsampling_x = 1; + seq_params->chroma_subsampling_y = 1; + seq_params->chroma_sample_position = 0; + return 0; + } else if (seq_params->color_primaries == 1 && + seq_params->transfer_characteristics == 13 && + seq_params->matrix_coefficients == 0) { + seq_params->chroma_subsampling_x = 0; + seq_params->chroma_subsampling_y = 0; + } else { + seq_params->color_range = get_bits(gb, 1); + + if (seq_params->profile == FF_PROFILE_AV1_MAIN) { + seq_params->chroma_subsampling_x = 1; + seq_params->chroma_subsampling_y = 1; + } else if (seq_params->profile == FF_PROFILE_AV1_HIGH) { + seq_params->chroma_subsampling_x = 0; + seq_params->chroma_subsampling_y = 0; + } else { + if (twelve_bit) { + seq_params->chroma_subsampling_x = + get_bits(gb, 1); + if (seq_params->chroma_subsampling_x) + seq_params->chroma_subsampling_y = + get_bits(gb, 1); + else + seq_params->chroma_subsampling_y = 0; + } else { + seq_params->chroma_subsampling_x = 1; + seq_params->chroma_subsampling_y = 0; + } + } + if (seq_params->chroma_subsampling_x && + seq_params->chroma_subsampling_y) + seq_params->chroma_sample_position = get_bits(gb, 2); + } + + skip_bits(gb, 1); // separate_uv_delta_q + + return 0; +} + +static int parse_sequence_header(AV1SequenceParameters *seq_params, + const uint8_t *buf, int size) +{ + Av1GetBitContext gb; + int reduced_still_picture_header; + int frame_width_bits_minus_1, frame_height_bits_minus_1; + int size_bits, ret; + + size_bits = get_obu_bit_length(buf, size, AV1_OBU_SEQUENCE_HEADER); + if (size_bits < 0) + return size_bits; + + ret = init_get_bits(&gb, buf, size_bits); + if (ret < 0) + return ret; + + memset(seq_params, 0, sizeof(*seq_params)); + + seq_params->profile = get_bits(&gb, 3); + + skip_bits(&gb, 1); // still_picture + reduced_still_picture_header = get_bits(&gb, 1); + + if (reduced_still_picture_header) { + seq_params->level = get_bits(&gb, 5); + seq_params->tier = 0; + } else { + int initial_display_delay_present_flag, + operating_points_cnt_minus_1; + int decoder_model_info_present_flag, + buffer_delay_length_minus_1; + + if (get_bits(&gb, 1)) { // timing_info_present_flag + skip_bits_long(&gb, 32); // num_units_in_display_tick + skip_bits_long(&gb, 32); // time_scale + + if (get_bits(&gb, 1)) // equal_picture_interval + uvlc(&gb); // num_ticks_per_picture_minus_1 + + decoder_model_info_present_flag = get_bits(&gb, 1); + if (decoder_model_info_present_flag) { + buffer_delay_length_minus_1 = get_bits(&gb, 5); + skip_bits_long(&gb, 32); + skip_bits(&gb, 10); + } + } else + decoder_model_info_present_flag = 0; + + initial_display_delay_present_flag = get_bits(&gb, 1); + + operating_points_cnt_minus_1 = get_bits(&gb, 5); + for (int i = 0; i <= operating_points_cnt_minus_1; i++) { + int seq_level_idx, seq_tier; + + skip_bits(&gb, 12); + seq_level_idx = get_bits(&gb, 5); + + if (seq_level_idx > 7) + seq_tier = get_bits(&gb, 1); + else + seq_tier = 0; + + if (decoder_model_info_present_flag) { + if (get_bits(&gb, 1)) { + skip_bits_long( + &gb, + buffer_delay_length_minus_1 + + 1); + skip_bits_long( + &gb, + buffer_delay_length_minus_1 + + 1); + skip_bits(&gb, 1); + } + } + + if (initial_display_delay_present_flag) { + if (get_bits(&gb, 1)) + skip_bits(&gb, 4); + } + + if (i == 0) { + seq_params->level = seq_level_idx; + seq_params->tier = seq_tier; + } + } + } + + frame_width_bits_minus_1 = get_bits(&gb, 4); + frame_height_bits_minus_1 = get_bits(&gb, 4); + + skip_bits(&gb, frame_width_bits_minus_1 + 1); // max_frame_width_minus_1 + skip_bits(&gb, + frame_height_bits_minus_1 + 1); // max_frame_height_minus_1 + + if (!reduced_still_picture_header) { + if (get_bits(&gb, 1)) // frame_id_numbers_present_flag + skip_bits(&gb, 7); + } + + skip_bits( + &gb, + 3); // use_128x128_superblock (1), enable_filter_intra (1), enable_intra_edge_filter (1) + + if (!reduced_still_picture_header) { + int enable_order_hint, seq_force_screen_content_tools; + + skip_bits(&gb, 4); + + enable_order_hint = get_bits(&gb, 1); + if (enable_order_hint) + skip_bits(&gb, 2); + + if (get_bits(&gb, 1)) // seq_choose_screen_content_tools + seq_force_screen_content_tools = 2; + else + seq_force_screen_content_tools = get_bits(&gb, 1); + + if (seq_force_screen_content_tools) { + if (!get_bits(&gb, 1)) // seq_choose_integer_mv + skip_bits(&gb, 1); // seq_force_integer_mv + } + + if (enable_order_hint) + skip_bits(&gb, 3); // order_hint_bits_minus_1 + } + + skip_bits(&gb, 3); + + parse_color_config(seq_params, &gb); + + skip_bits(&gb, 1); // film_grain_params_present + + if (get_bits_left(&gb)) + return -1; + + return 0; +} + +size_t obs_parse_av1_header(uint8_t **header, const uint8_t *data, size_t size) +{ + if (data[0] & 0x80) { + int config_record_version = data[0] & 0x7f; + if (config_record_version != 1 || size < 4) + return 0; + + *header = bmemdup(data, size); + return size; + } + + // AV1S init + AV1SequenceParameters seq_params; + int nb_seq = 0, seq_size = 0, meta_size = 0; + const uint8_t *seq = 0, *meta = 0; + + uint8_t *buf = (uint8_t *)data; + while (size > 0) { + int64_t obu_size; + int start_pos, type, temporal_id, spatial_id; + assert(size <= INT_MAX); + int len = parse_obu_header(buf, (int)size, &obu_size, + &start_pos, &type, &temporal_id, + &spatial_id); + if (len < 0) + return 0; + + switch (type) { + case AV1_OBU_SEQUENCE_HEADER: + nb_seq++; + if (!obu_size || nb_seq > 1) { + return 0; + } + assert(obu_size <= INT_MAX); + if (parse_sequence_header(&seq_params, buf + start_pos, + (int)obu_size) < 0) + return 0; + + seq = buf; + seq_size = len; + break; + case AV1_OBU_METADATA: + if (!obu_size) + return 0; + meta = buf; + meta_size = len; + break; + default: + break; + } + size -= len; + buf += len; + } + + if (!nb_seq) + return 0; + + uint8_t av1header[4]; + av1header[0] = (1 << 7) | 1; // marker and version + av1header[1] = (seq_params.profile << 5) | (seq_params.level); + av1header[2] = (seq_params.tier << 7) | (seq_params.bitdepth > 8) << 6 | + (seq_params.bitdepth == 12) << 5 | + (seq_params.monochrome) << 4 | + (seq_params.chroma_subsampling_x) << 3 | + (seq_params.chroma_subsampling_y) << 2 | + (seq_params.chroma_sample_position); + av1header[3] = 0; + + struct array_output_data output; + struct serializer s; + long ref = 1; + + array_output_serializer_init(&s, &output); + serialize(&s, &ref, sizeof(ref)); + + s_write(&s, av1header, sizeof(av1header)); + if (seq_size) + s_write(&s, seq, seq_size); + if (meta_size) + s_write(&s, meta, meta_size); + + *header = output.bytes.array + sizeof(ref); + return output.bytes.num - sizeof(ref); +} + +static void serialize_av1_data(struct serializer *s, const uint8_t *data, + size_t size, bool *is_keyframe, int *priority) +{ + (void)is_keyframe; + (void)priority; + uint8_t *buf = (uint8_t *)data; + uint8_t *end = (uint8_t *)data + size; + enum { + START_NOT_FOUND, + START_FOUND, + END_FOUND, + OFFSET_IMPOSSIBLE, + } state = START_NOT_FOUND; + + while (buf < end) { + int64_t obu_size; + int start_pos, type, temporal_id, spatial_id; + assert(end - buf <= INT_MAX); + int len = parse_obu_header(buf, (int)(end - buf), &obu_size, + &start_pos, &type, &temporal_id, + &spatial_id); + if (len < 0) + return; + + switch (type) { + case AV1_OBU_TEMPORAL_DELIMITER: + case AV1_OBU_REDUNDANT_FRAME_HEADER: + case AV1_OBU_TILE_LIST: + case AV1_OBU_PADDING: + if (state == START_FOUND) + state = END_FOUND; + break; + default: + if (state == START_NOT_FOUND) { + state = START_FOUND; + } else if (state == END_FOUND) { + state = OFFSET_IMPOSSIBLE; + } + s_write(s, buf, len); + size += len; + break; + } + buf += len; + } +} + +void obs_parse_av1_packet(struct encoder_packet *av1_packet, + const struct encoder_packet *src) +{ + struct array_output_data output; + struct serializer s; + long ref = 1; + + array_output_serializer_init(&s, &output); + serialize(&s, &ref, sizeof(ref)); + + *av1_packet = *src; + serialize_av1_data(&s, src->data, src->size, &av1_packet->keyframe, + &av1_packet->priority); + + av1_packet->data = output.bytes.array + sizeof(ref); + av1_packet->size = output.bytes.num - sizeof(ref); + av1_packet->drop_priority = av1_packet->priority; +} diff --git a/plugins/obs-outputs/rtmp-av1.h b/plugins/obs-outputs/rtmp-av1.h new file mode 100644 index 00000000000000..a706cdb0c05045 --- /dev/null +++ b/plugins/obs-outputs/rtmp-av1.h @@ -0,0 +1,35 @@ +/****************************************************************************** + Copyright (C) 2023 by Hugh Bailey + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +******************************************************************************/ + +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#include "util/c99defs.h" + +struct encoder_packet; + +EXPORT void obs_parse_av1_packet(struct encoder_packet *avc_packet, + const struct encoder_packet *src); +EXPORT size_t obs_parse_av1_header(uint8_t **header, const uint8_t *data, + size_t size); + +#ifdef __cplusplus +} +#endif diff --git a/plugins/obs-outputs/rtmp-hevc.c b/plugins/obs-outputs/rtmp-hevc.c new file mode 100644 index 00000000000000..dfb277ff411d02 --- /dev/null +++ b/plugins/obs-outputs/rtmp-hevc.c @@ -0,0 +1,948 @@ +/****************************************************************************** + Copyright (C) 2023 by Hugh Bailey + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +******************************************************************************/ + +#include "obs-hevc.h" + +#include "obs.h" +#include "obs-nal.h" +#include "obs-bits.h" +#include "util/array-serializer.h" + +#include "rtmp-hevc.h" + +enum { + OBS_VPS_INDEX, + OBS_SPS_INDEX, + OBS_PPS_INDEX, + OBS_SEI_PREFIX_INDEX, + OBS_SEI_SUFFIX_INDEX, + OBS_NB_ARRAYS +}; + +enum { + OBS_HEVC_MAX_VPS_COUNT = 16, + OBS_HEVC_MAX_SPS_COUNT = 16, + OBS_HEVC_MAX_PPS_COUNT = 64, +}; + +typedef struct HVCCNALUnitArray { + uint8_t array_completeness; + uint8_t NAL_unit_type; + uint16_t numNalus; + struct array_output_data nalUnitData; + struct serializer nalUnit; +} HVCCNALUnitArray; + +typedef struct HEVCDecoderConfigurationRecord { + uint8_t general_profile_space; + uint8_t general_tier_flag; + uint8_t general_profile_idc; + uint32_t general_profile_compatibility_flags; + uint64_t general_constraint_indicator_flags; + uint8_t general_level_idc; + uint16_t min_spatial_segmentation_idc; + uint8_t parallelismType; + uint8_t chromaFormat; + uint8_t bitDepthLumaMinus8; + uint8_t bitDepthChromaMinus8; + uint16_t avgFrameRate; + uint8_t constantFrameRate; + uint8_t numTemporalLayers; + uint8_t temporalIdNested; + uint8_t lengthSizeMinusOne; + uint8_t numOfArrays; + HVCCNALUnitArray arrays[OBS_NB_ARRAYS]; +} HEVCDecoderConfigurationRecord; + +typedef struct HVCCProfileTierLevel { + uint8_t profile_space; + uint8_t tier_flag; + uint8_t profile_idc; + uint32_t profile_compatibility_flags; + uint64_t constraint_indicator_flags; + uint8_t level_idc; +} HVCCProfileTierLevel; + +typedef struct HevcGetBitContext { + const uint8_t *buffer, *buffer_end; + uint64_t cache; + unsigned bits_left; + int index; + int size_in_bits; + int size_in_bits_plus8; +} HevcGetBitContext; + +static inline uint32_t MINU(uint32_t x, uint32_t y) +{ + return x < y ? x : y; +} + +static inline uint MAX8(uint8_t x, uint8_t y) +{ + return x > y ? x : y; +} + +static inline void refill_32(HevcGetBitContext *s) +{ + s->cache = s->cache | (uint64_t)RB32(s->buffer + (s->index >> 3)) + << (32 - s->bits_left); + s->index += 32; + s->bits_left += 32; +} + +static inline uint64_t get_val(HevcGetBitContext *s, unsigned n) +{ + uint64_t ret; + ret = s->cache >> (64 - n); + s->cache <<= n; + s->bits_left -= n; + return ret; +} + +static inline int init_get_bits_xe(HevcGetBitContext *s, const uint8_t *buffer, + int bit_size) +{ + int buffer_size; + int ret = 0; + + if (bit_size >= INT_MAX - MAX(7, 64 * 8) || bit_size < 0 || !buffer) { + bit_size = 0; + buffer = NULL; + ret = -1; + } + + buffer_size = (bit_size + 7) >> 3; + + s->buffer = buffer; + s->size_in_bits = bit_size; + s->size_in_bits_plus8 = bit_size + 8; + s->buffer_end = buffer + buffer_size; + s->index = 0; + + s->cache = 0; + s->bits_left = 0; + refill_32(s); + + return ret; +} + +static inline int init_get_bits(HevcGetBitContext *s, const uint8_t *buffer, + int bit_size) +{ + return init_get_bits_xe(s, buffer, bit_size); +} + +static inline int init_get_bits8(HevcGetBitContext *s, const uint8_t *buffer, + int byte_size) +{ + if (byte_size > INT_MAX / 8 || byte_size < 0) + byte_size = -1; + return init_get_bits(s, buffer, byte_size * 8); +} + +static inline unsigned int get_bits(HevcGetBitContext *s, unsigned int n) +{ + register unsigned int tmp; + + if (n > s->bits_left) { + refill_32(s); + } + tmp = (unsigned int)get_val(s, n); + return tmp; +} + +#define skip_bits get_bits + +static inline int get_bits_count(HevcGetBitContext *s) +{ + return s->index - s->bits_left; +} + +static inline int get_bits_left(HevcGetBitContext *gb) +{ + return gb->size_in_bits - get_bits_count(gb); +} + +static inline unsigned int get_bits_long(HevcGetBitContext *s, int n) +{ + if (!n) + return 0; + return get_bits(s, n); +} + +#define skip_bits_long get_bits_long + +static inline uint64_t get_bits64(HevcGetBitContext *s, int n) +{ + if (n <= 32) { + return get_bits_long(s, n); + } else { + uint64_t ret = (uint64_t)get_bits_long(s, n - 32) << 32; + return ret | get_bits_long(s, 32); + } +} +static inline int ilog2(unsigned x) +{ + return (31 - clz32(x | 1)); +} +static inline unsigned show_val(const HevcGetBitContext *s, unsigned n) +{ + return s->cache & ((UINT64_C(1) << n) - 1); +} +static inline unsigned int show_bits(HevcGetBitContext *s, unsigned int n) +{ + register unsigned int tmp; + if (n > s->bits_left) + refill_32(s); + tmp = show_val(s, n); + return tmp; +} +static inline unsigned int show_bits_long(HevcGetBitContext *s, int n) +{ + if (n <= 25) { + return show_bits(s, n); + } else { + HevcGetBitContext gb = *s; + return get_bits_long(&gb, n); + } +} + +static inline unsigned get_ue_golomb_long(HevcGetBitContext *gb) +{ + unsigned buf, log; + + buf = show_bits_long(gb, 32); + log = 31 - ilog2(buf); + skip_bits_long(gb, log); + + return get_bits_long(gb, log + 1) - 1; +} +static inline int get_se_golomb_long(HevcGetBitContext *gb) +{ + unsigned int buf = get_ue_golomb_long(gb); + int sign = (buf & 1) - 1; + return ((buf >> 1) ^ sign) + 1; +} + +static inline bool has_start_code(const uint8_t *data, size_t size) +{ + if (size > 3 && data[0] == 0 && data[1] == 0 && data[2] == 1) + return true; + + if (size > 4 && data[0] == 0 && data[1] == 0 && data[2] == 0 && + data[3] == 1) + return true; + + return false; +} + +uint8_t *ff_nal_unit_extract_rbsp(uint8_t *dst, const uint8_t *src, int src_len, + uint32_t *dst_len, int header_len) +{ + int i, len; + + /* NAL unit header */ + i = len = 0; + while (i < header_len && i < src_len) + dst[len++] = src[i++]; + + while (i + 2 < src_len) + if (!src[i] && !src[i + 1] && src[i + 2] == 3) { + dst[len++] = src[i++]; + dst[len++] = src[i++]; + i++; // remove emulation_prevention_three_byte + } else + dst[len++] = src[i++]; + + while (i < src_len) + dst[len++] = src[i++]; + + memset(dst + len, 0, 64); + + *dst_len = (uint32_t)len; + return dst; +} + +static int hvcc_array_add_nal_unit(uint8_t *nal_buf, uint32_t nal_size, + uint8_t nal_type, int ps_array_completeness, + HVCCNALUnitArray *array) +{ + s_wb16(&array->nalUnit, nal_size); + s_write(&array->nalUnit, nal_buf, nal_size); + array->NAL_unit_type = nal_type; + array->numNalus++; + + if (nal_type == OBS_HEVC_NAL_VPS || nal_type == OBS_HEVC_NAL_SPS || + nal_type == OBS_HEVC_NAL_PPS) + array->array_completeness = ps_array_completeness; + + return 0; +} + +static void nal_unit_parse_header(HevcGetBitContext *gb, uint8_t *nal_type) +{ + int z = get_bits(gb, 1); // forbidden_zero_bit + *nal_type = get_bits(gb, 6); + get_bits(gb, 9); +} + +static void hvcc_update_ptl(HEVCDecoderConfigurationRecord *hvcc, + HVCCProfileTierLevel *ptl) +{ + hvcc->general_profile_space = ptl->profile_space; + if (hvcc->general_tier_flag < ptl->tier_flag) + hvcc->general_level_idc = ptl->level_idc; + else + hvcc->general_level_idc = + MAX8(hvcc->general_level_idc, ptl->level_idc); + hvcc->general_tier_flag = MAX8(hvcc->general_tier_flag, ptl->tier_flag); + hvcc->general_profile_idc = + MAX8(hvcc->general_profile_idc, ptl->profile_idc); + hvcc->general_profile_compatibility_flags &= + ptl->profile_compatibility_flags; + hvcc->general_constraint_indicator_flags &= + ptl->constraint_indicator_flags; +} + +static void hvcc_parse_ptl(HevcGetBitContext *gb, + HEVCDecoderConfigurationRecord *hvcc, + unsigned int max_sub_layers_minus1) +{ + unsigned int i; + HVCCProfileTierLevel general_ptl; + uint8_t sub_layer_profile_present_flag[7]; // max sublayers + uint8_t sub_layer_level_present_flag[7]; // max sublayers + + general_ptl.profile_space = get_bits(gb, 2); + general_ptl.tier_flag = get_bits(gb, 1); + general_ptl.profile_idc = get_bits(gb, 5); + general_ptl.profile_compatibility_flags = get_bits_long(gb, 32); + general_ptl.constraint_indicator_flags = get_bits64(gb, 48); + general_ptl.level_idc = get_bits(gb, 8); + hvcc_update_ptl(hvcc, &general_ptl); + + for (i = 0; i < max_sub_layers_minus1; i++) { + sub_layer_profile_present_flag[i] = get_bits(gb, 1); + sub_layer_level_present_flag[i] = get_bits(gb, 1); + } + + // skip the rest + if (max_sub_layers_minus1 > 0) + for (i = max_sub_layers_minus1; i < 8; i++) + skip_bits(gb, 2); + + for (i = 0; i < max_sub_layers_minus1; i++) { + if (sub_layer_profile_present_flag[i]) { + skip_bits_long(gb, 32); + skip_bits_long(gb, 32); + skip_bits(gb, 24); + } + + if (sub_layer_level_present_flag[i]) + skip_bits(gb, 8); + } +} + +static int hvcc_parse_vps(HevcGetBitContext *gb, + HEVCDecoderConfigurationRecord *hvcc) +{ + unsigned int vps_max_sub_layers_minus1; + skip_bits(gb, 12); + vps_max_sub_layers_minus1 = get_bits(gb, 3); + hvcc->numTemporalLayers = + MAX8(hvcc->numTemporalLayers, vps_max_sub_layers_minus1 + 1); + skip_bits(gb, 17); + hvcc_parse_ptl(gb, hvcc, vps_max_sub_layers_minus1); + return 0; +} + +#define HEVC_MAX_SHORT_TERM_REF_PIC_SETS 64 + +static void skip_scaling_list_data(HevcGetBitContext *gb) +{ + int i, j, k, num_coeffs; + + for (i = 0; i < 4; i++) { + for (j = 0; j < (i == 3 ? 2 : 6); j++) { + if (!get_bits(gb, 1)) + get_ue_golomb_long(gb); + else { + num_coeffs = MIN(64, 1 << (4 + (i << 1))); + + if (i > 1) + get_se_golomb_long(gb); + for (k = 0; k < num_coeffs; k++) + get_se_golomb_long(gb); + } + } + } +} + +static int +parse_rps(HevcGetBitContext *gb, unsigned int rps_idx, unsigned int num_rps, + unsigned int num_delta_pocs[HEVC_MAX_SHORT_TERM_REF_PIC_SETS]) +{ + unsigned int i; + + if (rps_idx && get_bits(gb, 1)) { // inter_ref_pic_set_prediction_flag + /* this should only happen for slice headers, and this isn't one */ + if (rps_idx >= num_rps) + return -1; + + get_bits(gb, 1); // delta_rps_sign + get_ue_golomb_long(gb); // abs_delta_rps_minus1 + + num_delta_pocs[rps_idx] = 0; + + for (i = 0; i <= num_delta_pocs[rps_idx - 1]; i++) { + uint8_t use_delta_flag = 0; + uint8_t used_by_curr_pic_flag = get_bits(gb, 1); + if (!used_by_curr_pic_flag) + use_delta_flag = get_bits(gb, 1); + + if (used_by_curr_pic_flag || use_delta_flag) + num_delta_pocs[rps_idx]++; + } + } else { + unsigned int num_negative_pics = get_ue_golomb_long(gb); + unsigned int num_positive_pics = get_ue_golomb_long(gb); + + if ((num_positive_pics + (uint64_t)num_negative_pics) * 2 > + (uint64_t)get_bits_left(gb)) + return -1; + + num_delta_pocs[rps_idx] = num_negative_pics + num_positive_pics; + + for (i = 0; i < num_negative_pics; i++) { + get_ue_golomb_long(gb); // delta_poc_s0_minus1[rps_idx] + get_bits(gb, 1); // used_by_curr_pic_s0_flag[rps_idx] + } + + for (i = 0; i < num_positive_pics; i++) { + get_ue_golomb_long(gb); // delta_poc_s1_minus1[rps_idx] + get_bits(gb, 1); // used_by_curr_pic_s1_flag[rps_idx] + } + } + + return 0; +} + +static void +skip_sub_layer_hrd_parameters(HevcGetBitContext *gb, + unsigned int cpb_cnt_minus1, + uint8_t sub_pic_hrd_params_present_flag) +{ + unsigned int i; + + for (i = 0; i <= cpb_cnt_minus1; i++) { + get_ue_golomb_long(gb); // bit_rate_value_minus1 + get_ue_golomb_long(gb); // cpb_size_value_minus1 + + if (sub_pic_hrd_params_present_flag) { + get_ue_golomb_long(gb); // cpb_size_du_value_minus1 + get_ue_golomb_long(gb); // bit_rate_du_value_minus1 + } + + get_bits(gb, 1); // cbr_flag + } +} + +static int skip_hrd_parameters(HevcGetBitContext *gb, + uint8_t cprms_present_flag, + unsigned int max_sub_layers_minus1) +{ + unsigned int i; + uint8_t sub_pic_hrd_params_present_flag = 0; + uint8_t nal_hrd_parameters_present_flag = 0; + uint8_t vcl_hrd_parameters_present_flag = 0; + + if (cprms_present_flag) { + nal_hrd_parameters_present_flag = get_bits(gb, 1); + vcl_hrd_parameters_present_flag = get_bits(gb, 1); + + if (nal_hrd_parameters_present_flag || + vcl_hrd_parameters_present_flag) { + sub_pic_hrd_params_present_flag = get_bits(gb, 1); + + if (sub_pic_hrd_params_present_flag) + get_bits(gb, 19); + + get_bits(gb, 8); + + if (sub_pic_hrd_params_present_flag) + get_bits(gb, 4); + get_bits(gb, 15); + } + } + + for (i = 0; i <= max_sub_layers_minus1; i++) { + unsigned int cpb_cnt_minus1 = 0; + uint8_t low_delay_hrd_flag = 0; + uint8_t fixed_pic_rate_within_cvs_flag = 0; + uint8_t fixed_pic_rate_general_flag = get_bits(gb, 1); + + if (!fixed_pic_rate_general_flag) + fixed_pic_rate_within_cvs_flag = get_bits(gb, 1); + + if (fixed_pic_rate_within_cvs_flag) + get_ue_golomb_long(gb); + else + low_delay_hrd_flag = get_bits(gb, 1); + + if (!low_delay_hrd_flag) { + cpb_cnt_minus1 = get_ue_golomb_long(gb); + if (cpb_cnt_minus1 > 31) + return -1; + } + + if (nal_hrd_parameters_present_flag) + skip_sub_layer_hrd_parameters( + gb, cpb_cnt_minus1, + sub_pic_hrd_params_present_flag); + + if (vcl_hrd_parameters_present_flag) + skip_sub_layer_hrd_parameters( + gb, cpb_cnt_minus1, + sub_pic_hrd_params_present_flag); + } + + return 0; +} + +static void hvcc_parse_vui(HevcGetBitContext *gb, + HEVCDecoderConfigurationRecord *hvcc, + unsigned int max_sub_layers_minus1) +{ + unsigned int min_spatial_segmentation_idc; + + if (get_bits(gb, 1)) // aspect_ratio_info_present_flag + if (get_bits(gb, 8) == 255) // aspect_ratio_idc + get_bits_long(gb, + 32); // sar_width u(16), sar_height u(16) + + if (get_bits(gb, 1)) // overscan_info_present_flag + get_bits(gb, 1); // overscan_appropriate_flag + + if (get_bits(gb, 1)) { // video_signal_type_present_flag + get_bits(gb, + 4); // video_format u(3), video_full_range_flag u(1) + + if (get_bits(gb, 1)) // colour_description_present_flag + get_bits(gb, 24); + } + + if (get_bits(gb, 1)) { + get_ue_golomb_long(gb); + get_ue_golomb_long(gb); + } + + get_bits(gb, 3); + + if (get_bits(gb, 1)) { // default_display_window_flag + get_ue_golomb_long(gb); // def_disp_win_left_offset + get_ue_golomb_long(gb); // def_disp_win_right_offset + get_ue_golomb_long(gb); // def_disp_win_top_offset + get_ue_golomb_long(gb); // def_disp_win_bottom_offset + } + + if (get_bits(gb, 1)) { // vui_timing_info_present_flag + // skip timing info + get_bits_long(gb, 32); // num_units_in_tick + get_bits_long(gb, 32); // time_scale + + if (get_bits(gb, 1)) // poc_proportional_to_timing_flag + get_ue_golomb_long(gb); // num_ticks_poc_diff_one_minus1 + + if (get_bits(gb, 1)) // vui_hrd_parameters_present_flag + skip_hrd_parameters(gb, 1, max_sub_layers_minus1); + } + + if (get_bits(gb, 1)) { // bitstream_restriction_flag + get_bits(gb, 3); + + min_spatial_segmentation_idc = get_ue_golomb_long(gb); + hvcc->min_spatial_segmentation_idc = + (uint16_t)MIN((int)hvcc->min_spatial_segmentation_idc, + min_spatial_segmentation_idc); + + get_ue_golomb_long(gb); // max_bytes_per_pic_denom + get_ue_golomb_long(gb); // max_bits_per_min_cu_denom + get_ue_golomb_long(gb); // log2_max_mv_length_horizontal + get_ue_golomb_long(gb); // log2_max_mv_length_vertical + } +} + +static int hvcc_parse_sps(HevcGetBitContext *gb, + HEVCDecoderConfigurationRecord *hvcc) +{ + unsigned int i, sps_max_sub_layers_minus1, + log2_max_pic_order_cnt_lsb_minus4; + unsigned int num_short_term_ref_pic_sets, + num_delta_pocs[HEVC_MAX_SHORT_TERM_REF_PIC_SETS]; + + get_bits(gb, 4); // sps_video_parameter_set_id + sps_max_sub_layers_minus1 = get_bits(gb, 3); + + hvcc->numTemporalLayers = + MAX8(hvcc->numTemporalLayers, sps_max_sub_layers_minus1 + 1); + + hvcc->temporalIdNested = get_bits(gb, 1); + + hvcc_parse_ptl(gb, hvcc, sps_max_sub_layers_minus1); + + get_ue_golomb_long(gb); // sps_seq_parameter_set_id + + hvcc->chromaFormat = get_ue_golomb_long(gb); + + if (hvcc->chromaFormat == 3) + get_bits(gb, 1); // separate_colour_plane_flag + + get_ue_golomb_long(gb); // pic_width_in_luma_samples + get_ue_golomb_long(gb); // pic_height_in_luma_samples + + if (get_bits(gb, 1)) { // conformance_window_flag + get_ue_golomb_long(gb); // conf_win_left_offset + get_ue_golomb_long(gb); // conf_win_right_offset + get_ue_golomb_long(gb); // conf_win_top_offset + get_ue_golomb_long(gb); // conf_win_bottom_offset + } + + hvcc->bitDepthLumaMinus8 = get_ue_golomb_long(gb); + hvcc->bitDepthChromaMinus8 = get_ue_golomb_long(gb); + log2_max_pic_order_cnt_lsb_minus4 = get_ue_golomb_long(gb); + + /* sps_sub_layer_ordering_info_present_flag */ + i = get_bits(gb, 1) ? 0 : sps_max_sub_layers_minus1; + for (; i <= sps_max_sub_layers_minus1; i++) { + get_ue_golomb_long(gb); // max_dec_pic_buffering_minus1 + get_ue_golomb_long(gb); // max_num_reorder_pics + get_ue_golomb_long(gb); // max_latency_increase_plus1 + } + + get_ue_golomb_long(gb); // log2_min_luma_coding_block_size_minus3 + get_ue_golomb_long(gb); // log2_diff_max_min_luma_coding_block_size + get_ue_golomb_long(gb); // log2_min_transform_block_size_minus2 + get_ue_golomb_long(gb); // log2_diff_max_min_transform_block_size + get_ue_golomb_long(gb); // max_transform_hierarchy_depth_inter + get_ue_golomb_long(gb); // max_transform_hierarchy_depth_intra + + if (get_bits(gb, 1) && // scaling_list_enabled_flag + get_bits(gb, 1)) // sps_scaling_list_data_present_flag + skip_scaling_list_data(gb); + + get_bits(gb, 1); // amp_enabled_flag + get_bits(gb, 1); // sample_adaptive_offset_enabled_flag + + if (get_bits(gb, 1)) { // pcm_enabled_flag + get_bits(gb, 4); // pcm_sample_bit_depth_luma_minus1 + get_bits(gb, 4); // pcm_sample_bit_depth_chroma_minus1 + get_ue_golomb_long( + gb); // log2_min_pcm_luma_coding_block_size_minus3 + get_ue_golomb_long( + gb); // log2_diff_max_min_pcm_luma_coding_block_size + get_bits(gb, 1); // pcm_loop_filter_disabled_flag + } + + num_short_term_ref_pic_sets = get_ue_golomb_long(gb); + if (num_short_term_ref_pic_sets > HEVC_MAX_SHORT_TERM_REF_PIC_SETS) + return -1; + + for (i = 0; i < num_short_term_ref_pic_sets; i++) { + int ret = parse_rps(gb, i, num_short_term_ref_pic_sets, + num_delta_pocs); + if (ret < 0) + return ret; + } + + if (get_bits(gb, 1)) { // long_term_ref_pics_present_flag + unsigned num_long_term_ref_pics_sps = get_ue_golomb_long(gb); + if (num_long_term_ref_pics_sps > 31U) + return -1; + for (i = 0; i < num_long_term_ref_pics_sps; + i++) { // num_long_term_ref_pics_sps + int len = + MIN(log2_max_pic_order_cnt_lsb_minus4 + 4, 16); + get_bits(gb, len); // lt_ref_pic_poc_lsb_sps[i] + get_bits(gb, 1); // used_by_curr_pic_lt_sps_flag[i] + } + } + + get_bits(gb, 1); // sps_temporal_mvp_enabled_flag + get_bits(gb, 1); // strong_intra_smoothing_enabled_flag + + if (get_bits(gb, 1)) // vui_parameters_present_flag + hvcc_parse_vui(gb, hvcc, sps_max_sub_layers_minus1); + + /* nothing useful for hvcC past this point */ + return 0; +} + +static int hvcc_parse_pps(HevcGetBitContext *gb, + HEVCDecoderConfigurationRecord *hvcc) +{ + uint8_t tiles_enabled_flag, entropy_coding_sync_enabled_flag; + + get_ue_golomb_long(gb); // pps_pic_parameter_set_id + get_ue_golomb_long(gb); // pps_seq_parameter_set_id + + get_bits(gb, 7); + + get_ue_golomb_long(gb); // num_ref_idx_l0_default_active_minus1 + get_ue_golomb_long(gb); // num_ref_idx_l1_default_active_minus1 + get_se_golomb_long(gb); // init_qp_minus26 + + get_bits(gb, 2); + + if (get_bits(gb, 1)) // cu_qp_delta_enabled_flag + get_ue_golomb_long(gb); // diff_cu_qp_delta_depth + + get_se_golomb_long(gb); // pps_cb_qp_offset + get_se_golomb_long(gb); // pps_cr_qp_offset + + get_bits(gb, 4); + + tiles_enabled_flag = get_bits(gb, 1); + entropy_coding_sync_enabled_flag = get_bits(gb, 1); + + if (entropy_coding_sync_enabled_flag && tiles_enabled_flag) + hvcc->parallelismType = 0; // mixed-type parallel decoding + else if (entropy_coding_sync_enabled_flag) + hvcc->parallelismType = 3; // wavefront-based parallel decoding + else if (tiles_enabled_flag) + hvcc->parallelismType = 2; // tile-based parallel decoding + else + hvcc->parallelismType = 1; // slice-based parallel decoding + + /* nothing useful for hvcC past this point */ + return 0; +} + +static int hvcc_add_nal_unit(uint8_t *nal_buf, uint32_t nal_size, + int ps_array_completeness, + HEVCDecoderConfigurationRecord *hvcc, + unsigned array_idx) +{ + int ret = 0; + HevcGetBitContext gbc; + uint8_t nal_type; + uint8_t *rbsp_buf; + uint32_t rbsp_size; + + uint8_t *dst; + dst = malloc(nal_size + 64); + if (!dst) + return -1; + + rbsp_buf = + ff_nal_unit_extract_rbsp(dst, nal_buf, nal_size, &rbsp_size, 2); + if (!rbsp_buf) { + ret = -1; + goto end; + } + + ret = init_get_bits8(&gbc, rbsp_buf, rbsp_size); + if (ret < 0) + goto end; + + nal_unit_parse_header(&gbc, &nal_type); + + ret = hvcc_array_add_nal_unit(nal_buf, nal_size, nal_type, + ps_array_completeness, + &hvcc->arrays[array_idx]); + if (ret < 0) + goto end; + if (hvcc->arrays[array_idx].numNalus == 1) + hvcc->numOfArrays++; + if (nal_type == OBS_HEVC_NAL_VPS) + ret = hvcc_parse_vps(&gbc, hvcc); + else if (nal_type == OBS_HEVC_NAL_SPS) + ret = hvcc_parse_sps(&gbc, hvcc); + else if (nal_type == OBS_HEVC_NAL_PPS) + ret = hvcc_parse_pps(&gbc, hvcc); + if (ret < 0) + goto end; + +end: + free(dst); + return ret; +} + +uint32_t rb32(uint8_t *ptr) +{ + return (ptr[0] << 24) + (ptr[1] << 16) + (ptr[2] << 8) + (ptr[3]); +} +size_t obs_parse_hevc_header(uint8_t **header, const uint8_t *data, size_t size) +{ + const uint8_t *start; + const uint8_t *end; + + if (!has_start_code(data, size)) { + *header = bmemdup(data, size); + return size; + } + + if (size < 6) + return 0; // invalid + if (*data == 1) { // already hvcC-formatted + *header = bmemdup(data, size); + return size; + } + + struct array_output_data nals; + struct serializer sn; + array_output_serializer_init(&sn, &nals); + + const uint8_t *nal_start, *nal_end; + start = data; + end = data + size; + + size = 0; // reset size + nal_start = obs_nal_find_startcode(start, end); + for (;;) { + while (nal_start < end && !*(nal_start++)) + ; + if (nal_start == end) + break; + nal_end = obs_nal_find_startcode(nal_start, end); + + assert(nal_end - nal_start <= INT_MAX); + s_wb32(&sn, (uint32_t)(nal_end - nal_start)); + s_write(&sn, nal_start, nal_end - nal_start); + size += 4 + nal_end - nal_start; + nal_start = nal_end; + } + if (size == 0) + goto done; + + start = nals.bytes.array; + end = nals.bytes.array + nals.bytes.num; + + // HVCC init + HEVCDecoderConfigurationRecord hvcc; + memset(&hvcc, 0, sizeof(HEVCDecoderConfigurationRecord)); + hvcc.lengthSizeMinusOne = 3; // 4 bytes + hvcc.general_profile_compatibility_flags = 0xffffffff; // all bits set + hvcc.general_constraint_indicator_flags = + 0xffffffffffff; // all bits set + hvcc.min_spatial_segmentation_idc = 4096 + 1; // assume invalid value + for (unsigned i = 0; i < OBS_NB_ARRAYS; i++) { + HVCCNALUnitArray *const array = &hvcc.arrays[i]; + array_output_serializer_init(&array->nalUnit, + &array->nalUnitData); + } + + uint8_t *buf = (uint8_t *)start; + while (end - buf > 4) { + uint32_t len = RB32(buf); + assert((end - buf - 4) <= INT_MAX); + len = MINU(len, (uint32_t)(end - buf - 4)); + uint8_t type = (buf[4] >> 1) & 0x3f; + + buf += 4; + + for (unsigned i = 0; i < OBS_NB_ARRAYS; i++) { + static const uint8_t array_idx_to_type[] = { + OBS_HEVC_NAL_VPS, OBS_HEVC_NAL_SPS, + OBS_HEVC_NAL_PPS, OBS_HEVC_NAL_SEI_PREFIX, + OBS_HEVC_NAL_SEI_SUFFIX}; + HVCCNALUnitArray *const array = &hvcc.arrays[i]; + + if (type == array_idx_to_type[i]) { + int ps_array_completeness = 0; + int ret = hvcc_add_nal_unit( + buf, len, ps_array_completeness, &hvcc, + i); + if (ret < 0) + goto free; + break; + } + } + + buf += len; + } + + // write hvcc data + uint16_t vps_count, sps_count, pps_count; + if (hvcc.min_spatial_segmentation_idc > 4096) // invalid? + hvcc.min_spatial_segmentation_idc = 0; + if (!hvcc.min_spatial_segmentation_idc) + hvcc.parallelismType = 0; + hvcc.avgFrameRate = 0; + hvcc.constantFrameRate = 0; + + vps_count = hvcc.arrays[OBS_VPS_INDEX].numNalus; + sps_count = hvcc.arrays[OBS_SPS_INDEX].numNalus; + pps_count = hvcc.arrays[OBS_PPS_INDEX].numNalus; + if (!vps_count || vps_count > OBS_HEVC_MAX_VPS_COUNT || !sps_count || + sps_count > OBS_HEVC_MAX_SPS_COUNT || !pps_count || + pps_count > OBS_HEVC_MAX_PPS_COUNT) + goto free; + + struct array_output_data output; + struct serializer s; + array_output_serializer_init(&s, &output); + s_w8(&s, 1); // configurationVersion, always 1 + + s_w8(&s, hvcc.general_profile_space << 6 | hvcc.general_tier_flag << 5 | + hvcc.general_profile_idc); + + s_wb32(&s, hvcc.general_profile_compatibility_flags); + + s_wb32(&s, (uint32_t)(hvcc.general_constraint_indicator_flags >> 16)); + s_wb16(&s, (uint16_t)(hvcc.general_constraint_indicator_flags)); + + s_w8(&s, hvcc.general_level_idc); + + s_wb16(&s, hvcc.min_spatial_segmentation_idc | 0xf000); + s_w8(&s, hvcc.parallelismType | 0xfc); + s_w8(&s, hvcc.chromaFormat | 0xfc); + s_w8(&s, hvcc.bitDepthLumaMinus8 | 0xf8); + s_w8(&s, hvcc.bitDepthChromaMinus8 | 0xf8); + s_wb16(&s, hvcc.avgFrameRate); + + s_w8(&s, hvcc.constantFrameRate << 6 | hvcc.numTemporalLayers << 3 | + hvcc.temporalIdNested << 2 | hvcc.lengthSizeMinusOne); + + s_w8(&s, hvcc.numOfArrays); + for (unsigned i = 0; i < OBS_NB_ARRAYS; i++) { + const HVCCNALUnitArray *const array = &hvcc.arrays[i]; + + if (!array->numNalus) + continue; + + s_w8(&s, array->array_completeness << 7 | + array->NAL_unit_type & 0x3f); + s_wb16(&s, array->numNalus); + + s_write(&s, array->nalUnitData.bytes.array, + array->nalUnitData.bytes.num); + } + + *header = output.bytes.array; + size = output.bytes.num; + +free: + for (unsigned i = 0; i < OBS_NB_ARRAYS; i++) { + HVCCNALUnitArray *const array = &hvcc.arrays[i]; + array->numNalus = 0; + array_output_serializer_free(&array->nalUnitData); + } + +done: + array_output_serializer_free(&nals); + return size; +} diff --git a/plugins/obs-outputs/rtmp-hevc.h b/plugins/obs-outputs/rtmp-hevc.h new file mode 100644 index 00000000000000..5debed56742e26 --- /dev/null +++ b/plugins/obs-outputs/rtmp-hevc.h @@ -0,0 +1,31 @@ +/****************************************************************************** + Copyright (C) 2023 by Hugh Bailey + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 2 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program. If not, see . +******************************************************************************/ + +#pragma once + +#include "util/c99defs.h" + +#ifdef __cplusplus +extern "C" { +#endif + +EXPORT size_t obs_parse_hevc_header(uint8_t **header, const uint8_t *data, + size_t size); + +#ifdef __cplusplus +} +#endif diff --git a/plugins/obs-outputs/rtmp-stream.c b/plugins/obs-outputs/rtmp-stream.c index 0312101b711e37..3c6f18af9786bb 100644 --- a/plugins/obs-outputs/rtmp-stream.c +++ b/plugins/obs-outputs/rtmp-stream.c @@ -16,6 +16,13 @@ ******************************************************************************/ #include "rtmp-stream.h" +#include +#include +#include + +#include "rtmp-av1.h" +#include "rtmp-hevc.h" + #ifdef _WIN32 #include #endif @@ -415,17 +422,10 @@ static int socket_queue_data(RTMPSockBuf *sb, const char *data, int len, return len; } -static int send_packet(struct rtmp_stream *stream, - struct encoder_packet *packet, bool is_header, - size_t idx) +static int handle_socket_read(struct rtmp_stream *stream) { - uint8_t *data; - size_t size; - int recv_size = 0; int ret = 0; - - assert(idx < RTMP_MAX_STREAMS); - + int recv_size = 0; if (!stream->new_socket_loop) { #ifdef _WIN32 ret = ioctlsocket(stream->rtmp.m_sb.sb_socket, FIONREAD, @@ -439,6 +439,20 @@ static int send_packet(struct rtmp_stream *stream, return -1; } } + return 0; +} + +static int send_packet(struct rtmp_stream *stream, + struct encoder_packet *packet, bool is_header, + size_t idx) +{ + uint8_t *data; + size_t size; + int ret = 0; + + assert(idx < RTMP_MAX_STREAMS); + if (handle_socket_read(stream)) + return -1; if (idx > 0) { flv_additional_packet_mux( @@ -465,7 +479,44 @@ static int send_packet(struct rtmp_stream *stream, return ret; } +static int send_packet_ex(struct rtmp_stream *stream, + struct encoder_packet *packet, bool is_header, + bool is_footer) +{ + uint8_t *data; + size_t size = 0; + int ret = 0; + + if (handle_socket_read(stream)) + return -1; + + if (is_header) + flv_packet_start(packet, stream->start_dts_offset, &data, + &size); + else if (is_footer) + flv_packet_end(packet, stream->start_dts_offset, &data, &size); + else + flv_packet_frames(packet, stream->start_dts_offset, &data, + &size); + +#ifdef TEST_FRAMEDROPS + droptest_cap_data_rate(stream, size); +#endif + + ret = RTMP_Write(&stream->rtmp, (char *)data, (int)size, 0); + bfree(data); + + if (is_header || is_footer) // manually created packets + bfree(packet->data); + else + obs_encoder_packet_release(packet); + + stream->total_bytes_sent += size; + return ret; +} + static inline bool send_headers(struct rtmp_stream *stream); +static inline bool send_footers(struct rtmp_stream *stream); static inline bool can_shutdown_stream(struct rtmp_stream *stream, struct encoder_packet *packet) @@ -650,7 +701,15 @@ static void *send_thread(void *data) dbr_frame.size = packet.size; } - if (send_packet(stream, &packet, false, packet.track_idx) < 0) { + const char *codec = 0; + int sent = + ((codec = obs_encoder_get_codec(packet.encoder)) && + (packet.type == OBS_ENCODER_VIDEO) && + (strcmp(codec, "h264") != 0)) // not h264, Y2023 spec + ? send_packet_ex(stream, &packet, false, false) + : send_packet(stream, &packet, false, + packet.track_idx); + if (sent < 0) { os_atomic_set_bool(&stream->disconnected, true); break; } @@ -670,10 +729,12 @@ static void *send_thread(void *data) info("Disconnected from %s", stream->path.array); } else if (encode_error) { info("Encoder error, disconnecting"); + send_footers(stream); // Y2023 spec } else if (silently_reconnecting(stream)) { info("Silent reconnect signal received from server"); } else { info("User stopped the stream"); + send_footers(stream); // Y2023 spec } #if defined(_WIN32) @@ -798,13 +859,117 @@ static bool send_video_header(struct rtmp_stream *stream) uint8_t *header; size_t size; - struct encoder_packet packet = { - .type = OBS_ENCODER_VIDEO, .timebase_den = 1, .keyframe = true}; + struct encoder_packet packet = {.type = OBS_ENCODER_VIDEO, + .encoder = vencoder, + .timebase_den = 1, + .keyframe = true}; if (!obs_encoder_get_extra_data(vencoder, &header, &size)) return false; - packet.size = obs_parse_avc_header(&packet.data, header, size); - return send_packet(stream, &packet, true, 0) >= 0; + + const char *codec = obs_encoder_get_codec(vencoder); + if (strcmp(codec, "h264") == 0) { + packet.size = obs_parse_avc_header(&packet.data, header, size); + return send_packet(stream, &packet, true, 0) >= 0; + } + if (strcmp(codec, "hevc") == 0) { + packet.size = obs_parse_hevc_header(&packet.data, header, size); + return send_packet_ex(stream, &packet, true, 0) >= 0; + } + if (strcmp(codec, "av1") == 0) { + packet.size = obs_parse_av1_header(&packet.data, header, size); + return send_packet_ex(stream, &packet, true, 0) >= 0; + } + return false; +} + +static bool send_video_metadata(struct rtmp_stream *stream) +{ + obs_output_t *context = stream->output; + obs_encoder_t *vencoder = obs_output_get_video_encoder(context); + + if (handle_socket_read(stream)) + return -1; + + // Y2023 spec + const char *codec = obs_encoder_get_codec(vencoder); + if (strcmp(codec, "hevc") == 0 || strcmp(codec, "av1") == 0) { + uint8_t *data; + size_t size; + + video_t *video = obs_get_video(); + const struct video_output_info *info = + video_output_get_info(video); + enum video_format format = info->format; + enum video_colorspace colorspace = info->colorspace; + enum video_range_type range = info->range; + + int bits_per_raw_sample = format == VIDEO_FORMAT_I010 ? 10 + : format == VIDEO_FORMAT_P010 ? 10 + : format == VIDEO_FORMAT_I210 ? 10 + : format == VIDEO_FORMAT_I412 ? 12 + : format == VIDEO_FORMAT_YA2L ? 12 + : 8; + + int pri = 0, trc = 0, spc = 0; + switch (colorspace) { + case VIDEO_CS_601: + pri = AVCOL_PRI_SMPTE170M; + trc = AVCOL_TRC_SMPTE170M; + spc = AVCOL_SPC_SMPTE170M; + break; + case VIDEO_CS_DEFAULT: + case VIDEO_CS_709: + pri = AVCOL_PRI_BT709; + trc = AVCOL_TRC_BT709; + spc = AVCOL_SPC_BT709; + break; + case VIDEO_CS_SRGB: + pri = AVCOL_PRI_BT709; + trc = AVCOL_TRC_IEC61966_2_1; + spc = AVCOL_SPC_BT709; + break; + case VIDEO_CS_2100_PQ: + pri = AVCOL_PRI_BT2020; + trc = AVCOL_TRC_SMPTE2084; + spc = AVCOL_SPC_BT2020_NCL; + break; + case VIDEO_CS_2100_HLG: + pri = AVCOL_PRI_BT2020; + trc = AVCOL_TRC_ARIB_STD_B67; + spc = AVCOL_SPC_BT2020_NCL; + } + + const int max_luminance = + (trc == AVCOL_TRC_SMPTE2084) + ? (int)obs_get_video_hdr_nominal_peak_level() + : ((trc == AVCOL_TRC_ARIB_STD_B67) ? 1000 : 0); + + flv_packet_metadata(vencoder, &data, &size, bits_per_raw_sample, + pri, trc, spc, 0, max_luminance); + + int ret = RTMP_Write(&stream->rtmp, (char *)data, (int)size, 0); + bfree(data); + + stream->total_bytes_sent += size; + return ret >= 0; + } + // legacy + return true; +} + +static bool send_video_footer(struct rtmp_stream *stream) +{ + obs_output_t *context = stream->output; + obs_encoder_t *vencoder = obs_output_get_video_encoder(context); + + struct encoder_packet packet = {.type = OBS_ENCODER_VIDEO, + .encoder = vencoder, + .timebase_den = 1, + .keyframe = false}; + packet.size = 0; + + return send_packet_ex(stream, &packet, 0, true) >= 0; } static inline bool send_headers(struct rtmp_stream *stream) @@ -818,6 +983,14 @@ static inline bool send_headers(struct rtmp_stream *stream) if (!send_video_header(stream)) return false; + // send metadata only if HDR + video_t *video = obs_get_video(); + const struct video_output_info *info = video_output_get_info(video); + enum video_colorspace colorspace = info->colorspace; + if (colorspace == VIDEO_CS_2100_PQ || colorspace == VIDEO_CS_2100_HLG) + if (!send_video_metadata(stream)) // Y2023 spec + return false; + while (next) { if (!send_audio_header(stream, i++, &next)) return false; @@ -826,6 +999,23 @@ static inline bool send_headers(struct rtmp_stream *stream) return true; } +static inline bool send_footers(struct rtmp_stream *stream) +{ + obs_output_t *context = stream->output; + obs_encoder_t *vencoder = obs_output_get_video_encoder(context); + + struct encoder_packet packet = {.type = OBS_ENCODER_VIDEO, + .encoder = vencoder, + .timebase_den = 1, + .keyframe = true}; + + const char *codec = obs_encoder_get_codec(vencoder); + if (strcmp(codec, "h264") != 0) { // Y2023 spec + return send_video_footer(stream); + } + return false; +} + static inline bool reset_semaphore(struct rtmp_stream *stream) { os_sem_destroy(stream->send_sem); @@ -1234,6 +1424,28 @@ static void *connect_thread(void *data) stream->start_dts_offset = get_ms_time(&packet, packet.dts); } + // HDR streaming disabled for AV1 and HEVC + obs_output_t *context = stream->output; + obs_encoder_t *vencoder = obs_output_get_video_encoder(context); + const char *codec = obs_encoder_get_codec(vencoder); + if (strcmp(codec, "hevc") == 0 || strcmp(codec, "av1") == 0) { + video_t *video = obs_get_video(); + const struct video_output_info *info = + video_output_get_info(video); + enum video_format format = info->format; + enum video_colorspace colorspace = info->colorspace; + + switch (colorspace) { + case VIDEO_CS_2100_HLG: + case VIDEO_CS_2100_PQ: + obs_output_signal_stop(stream->output, + OBS_OUTPUT_HDR_DISABLED); + return NULL; + default: + break; + } + } + ret = try_connect(stream); if (ret != OBS_OUTPUT_SUCCESS) { @@ -1547,7 +1759,13 @@ static void rtmp_stream_data(void *data, struct encoder_packet *packet) stream->got_first_video = true; } - obs_parse_avc_packet(&new_packet, packet); + const char *codec = obs_encoder_get_codec(packet->encoder); + if (strcmp(codec, "h264") == 0) + obs_parse_avc_packet(&new_packet, packet); + else if (strcmp(codec, "av1") == 0) // Y2023 spec + obs_parse_av1_packet(&new_packet, packet); + else if (strcmp(codec, "hevc") == 0) // Y2023 spec + obs_parse_hevc_packet(&new_packet, packet); } else { obs_encoder_packet_ref(&new_packet, packet); } @@ -1655,7 +1873,11 @@ struct obs_output_info rtmp_output_info = { #else .protocols = "RTMP;RTMPS", #endif - .encoded_video_codecs = "h264", +#ifdef ENABLE_HEVC + .encoded_video_codecs = "h264;hevc;av1", +#else + .encoded_video_codecs = "h264;av1", +#endif .encoded_audio_codecs = "aac", .get_name = rtmp_stream_getname, .create = rtmp_stream_create, diff --git a/plugins/obs-outputs/rtmp-stream.h b/plugins/obs-outputs/rtmp-stream.h index bca991909d42a1..8cb2b29b44ddd8 100644 --- a/plugins/obs-outputs/rtmp-stream.h +++ b/plugins/obs-outputs/rtmp-stream.h @@ -1,5 +1,4 @@ #include -#include #include #include #include diff --git a/plugins/rtmp-services/data/schema/service-schema-v4.json b/plugins/rtmp-services/data/schema/service-schema-v4.json index 5d27f3bef3dabe..0f2ee1097a7c71 100644 --- a/plugins/rtmp-services/data/schema/service-schema-v4.json +++ b/plugins/rtmp-services/data/schema/service-schema-v4.json @@ -46,7 +46,8 @@ "minLength": 1, "enum": [ "h264", - "hevc" + "hevc", + "av1" ] } }, diff --git a/plugins/rtmp-services/data/services.json b/plugins/rtmp-services/data/services.json index 7615ede461fc91..9977ff7e4d0769 100644 --- a/plugins/rtmp-services/data/services.json +++ b/plugins/rtmp-services/data/services.json @@ -235,6 +235,11 @@ "YouTube - RTMP", "YouTube - RTMPS (Beta)" ], + "supported video codecs": [ + "h264", + "hevc", + "av1" + ], "servers": [ { "name": "Primary YouTube ingest server",