mirror of
https://github.com/firestar5683/StarPilot.git
synced 2026-06-30 19:12:07 +08:00
framereader: remove memory copy overhead for AVIO (#22894)
* no memory copy * merge master better old-commit-hash: dc8e23c94b29a8c26cc377f8649e497f33036c33
This commit is contained in:
@@ -1,14 +1,23 @@
|
||||
#include "selfdrive/ui/replay/framereader.h"
|
||||
|
||||
#include <cassert>
|
||||
#include <sstream>
|
||||
|
||||
namespace {
|
||||
|
||||
int readFunction(void *opaque, uint8_t *buf, int buf_size) {
|
||||
auto &iss = *reinterpret_cast<std::istringstream *>(opaque);
|
||||
iss.read(reinterpret_cast<char *>(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<bool> *
|
||||
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
|
||||
|
||||
Reference in New Issue
Block a user