From dc8e23c94b29a8c26cc377f8649e497f33036c33 Mon Sep 17 00:00:00 2001 From: Dean Lee Date: Wed, 17 Nov 2021 20:25:40 +0800 Subject: [PATCH] framereader: remove memory copy overhead for AVIO (#22894) * no memory copy * merge master better --- selfdrive/ui/replay/framereader.cc | 27 ++++++++++++++++++++------- 1 file changed, 20 insertions(+), 7 deletions(-) diff --git a/selfdrive/ui/replay/framereader.cc b/selfdrive/ui/replay/framereader.cc index 6c531d1d6e..1524ad9748 100644 --- a/selfdrive/ui/replay/framereader.cc +++ b/selfdrive/ui/replay/framereader.cc @@ -1,14 +1,23 @@ #include "selfdrive/ui/replay/framereader.h" #include -#include namespace { -int readFunction(void *opaque, uint8_t *buf, int buf_size) { - auto &iss = *reinterpret_cast(opaque); - iss.read(reinterpret_cast(buf), buf_size); - return iss.gcount() ? iss.gcount() : AVERROR_EOF; +struct buffer_data { + const uint8_t *data; + int64_t offset; + size_t size; +}; + +int readPacket(void *opaque, uint8_t *buf, int buf_size) { + struct buffer_data *bd = (struct buffer_data *)opaque; + buf_size = std::min((size_t)buf_size, bd->size - bd->offset); + if (!buf_size) return AVERROR_EOF; + + memcpy(buf, bd->data + bd->offset, buf_size); + bd->offset += buf_size; + return buf_size; } enum AVPixelFormat get_hw_format(AVCodecContext *ctx, const enum AVPixelFormat *pix_fmts) { @@ -50,10 +59,14 @@ bool FrameReader::load(const std::string &url, bool no_cuda, std::atomic * std::string content = read(url, abort); if (content.empty()) return false; - std::istringstream iss(content); + struct buffer_data bd = { + .data = (uint8_t *)content.data(), + .offset = 0, + .size = content.size(), + }; const int avio_ctx_buffer_size = 64 * 1024; unsigned char *avio_ctx_buffer = (unsigned char *)av_malloc(avio_ctx_buffer_size); - avio_ctx_ = avio_alloc_context(avio_ctx_buffer, avio_ctx_buffer_size, 0, &iss, readFunction, nullptr, nullptr); + avio_ctx_ = avio_alloc_context(avio_ctx_buffer, avio_ctx_buffer_size, 0, &bd, readPacket, nullptr, nullptr); input_ctx->pb = avio_ctx_; input_ctx->probesize = 10 * 1024 * 1024; // 10MB