framereader: remove memory copy overhead for AVIO (#22894)

* no memory copy

* merge master

better
pull/22949/head
Dean Lee 3 years ago committed by GitHub
parent 5648b22833
commit dc8e23c94b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 27
      selfdrive/ui/replay/framereader.cc

@ -1,14 +1,23 @@
#include "selfdrive/ui/replay/framereader.h" #include "selfdrive/ui/replay/framereader.h"
#include <cassert> #include <cassert>
#include <sstream>
namespace { namespace {
int readFunction(void *opaque, uint8_t *buf, int buf_size) { struct buffer_data {
auto &iss = *reinterpret_cast<std::istringstream *>(opaque); const uint8_t *data;
iss.read(reinterpret_cast<char *>(buf), buf_size); int64_t offset;
return iss.gcount() ? iss.gcount() : AVERROR_EOF; 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) { 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<bool> *
std::string content = read(url, abort); std::string content = read(url, abort);
if (content.empty()) return false; 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; const int avio_ctx_buffer_size = 64 * 1024;
unsigned char *avio_ctx_buffer = (unsigned char *)av_malloc(avio_ctx_buffer_size); 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->pb = avio_ctx_;
input_ctx->probesize = 10 * 1024 * 1024; // 10MB input_ctx->probesize = 10 * 1024 * 1024; // 10MB

Loading…
Cancel
Save