From 3a8574de66c105095fc024ba91cd2fcd1c111490 Mon Sep 17 00:00:00 2001 From: Kamil Trzcinski Date: Fri, 8 Apr 2022 11:09:47 +0200 Subject: [PATCH] Re-use h264 keyframes --- http/http.h | 1 + http/http_ffmpeg.c | 10 ++-------- http/http_h264.c | 24 ++++++++++++++++++------ 3 files changed, 21 insertions(+), 14 deletions(-) diff --git a/http/http.h b/http/http.h index 25f0b06..84925fc 100644 --- a/http/http.h +++ b/http/http.h @@ -55,6 +55,7 @@ bool http_h264_needs_buffer(); void http_h264_capture(buffer_t *buf); void http_h264_lowres_capture(buffer_t *buf); void http_h264_video(http_worker_t *worker, FILE *stream); +bool h264_is_key_frame(buffer_t *buf); void http_mkv_video(http_worker_t *worker, FILE *stream); void http_mp4_video(http_worker_t *worker, FILE *stream); void http_mov_video(http_worker_t *worker, FILE *stream); diff --git a/http/http_ffmpeg.c b/http/http_ffmpeg.c index 4b0f167..31e181e 100644 --- a/http/http_ffmpeg.c +++ b/http/http_ffmpeg.c @@ -255,14 +255,8 @@ static int http_ffmpeg_copy_packets(http_ffmpeg_status_t *status) static int http_ffmpeg_video_buf_part(buffer_lock_t *buf_lock, buffer_t *buf, int frame, http_ffmpeg_status_t *status) { - unsigned char *data = buf->start; - - if (buf->v4l2_buffer.flags & V4L2_BUF_FLAG_KEYFRAME) { - status->had_key_frame = true; - E_LOG_DEBUG(buf, "Got key frame (from V4L2)!"); - } else if (buf->used >= 5 && (data[4] & 0x1F) == 0x07) { - status->had_key_frame = true; - E_LOG_DEBUG(buf, "Got key frame (from buffer)!"); + if (!status->had_key_frame) { + status->had_key_frame = h264_is_key_frame(buf); } if (!status->had_key_frame) { diff --git a/http/http_h264.c b/http/http_h264.c index 1c8af49..5053dbb 100644 --- a/http/http_h264.c +++ b/http/http_h264.c @@ -47,16 +47,28 @@ typedef struct { bool requested_key_frame; } http_video_status_t; +bool h264_is_key_frame(buffer_t *buf) +{ + unsigned char *data = buf->start; + device_t *camera = buf->buf_list->device; + + if (buf->v4l2_buffer.flags & V4L2_BUF_FLAG_KEYFRAME) { + E_LOG_DEBUG(buf, "Got key frame (from V4L2)!"); + return true; + } else if (buf->used >= 5 && (data[4] & 0x1F) == 0x07) { + E_LOG_DEBUG(buf, "Got key frame (from buffer)!"); + return true; + } + + return false; +} + int http_video_buf_part(buffer_lock_t *buf_lock, buffer_t *buf, int frame, http_video_status_t *status) { unsigned char *data = buf->start; - if (buf->v4l2_buffer.flags & V4L2_BUF_FLAG_KEYFRAME) { - status->had_key_frame = true; - E_LOG_DEBUG(buf, "Got key frame (from V4L2)!"); - } else if (buf->used >= 5 && (data[4] & 0x1F) == 0x07) { - status->had_key_frame = true; - E_LOG_DEBUG(buf, "Got key frame (from buffer)!"); + if (!status->had_key_frame) { + status->had_key_frame = h264_is_key_frame(buf); } if (!status->had_key_frame) {