From 9f97b02c32a4b7fb73639081eed1dd71b4d63f56 Mon Sep 17 00:00:00 2001 From: jp9000 Date: Thu, 18 Dec 2014 11:58:25 -0800 Subject: [PATCH] libobs: Add function to determine AVC keyframes Added obs_avc_keyframe that returns whether an avc packet is a keyframe or not. This function is particularly useful for when writing custom encoder plugins. --- libobs/obs-avc.c | 24 ++++++++++++++++++++++++ libobs/obs-avc.h | 1 + 2 files changed, 25 insertions(+) diff --git a/libobs/obs-avc.c b/libobs/obs-avc.c index b453cab33..8592472d1 100644 --- a/libobs/obs-avc.c +++ b/libobs/obs-avc.c @@ -19,6 +19,30 @@ #include "obs-avc.h" #include "util/array-serializer.h" +bool obs_avc_keyframe(const uint8_t *data, size_t size) +{ + const uint8_t *nal_start, *nal_end; + const uint8_t *end = data + size; + int type; + + nal_start = obs_avc_find_startcode(data, end); + while (true) { + while (nal_start < end && !*(nal_start++)); + + if (nal_start == end) + break; + + type = nal_start[0] & 0x1F; + + if (type == OBS_NAL_SLICE_IDR || type == OBS_NAL_SLICE) + return (type == OBS_NAL_SLICE_IDR); + + nal_end = obs_avc_find_startcode(nal_start, end); + nal_start = nal_end; + } + + return false; +} /* NOTE: I noticed that FFmpeg does some unusual special handling of certain * scenarios that I was unaware of, so instead of just searching for {0, 0, 1} diff --git a/libobs/obs-avc.h b/libobs/obs-avc.h index 9990347a2..8ff1ac404 100644 --- a/libobs/obs-avc.h +++ b/libobs/obs-avc.h @@ -48,6 +48,7 @@ enum { /* Helpers for parsing AVC NAL units. */ +EXPORT bool obs_avc_keyframe(const uint8_t *data, size_t size); EXPORT const uint8_t *obs_avc_find_startcode(const uint8_t *p, const uint8_t *end); EXPORT void obs_parse_avc_packet(struct encoder_packet *avc_packet,