Skip to content

Commit

Permalink
loggerd: prereqs for deanlee loggerd omx encoder (try 2) (commaai#24252)
Browse files Browse the repository at this point in the history
* refactor encoders

* fix pc build

* buf_info

Co-authored-by: Comma Device <device@comma.ai>
  • Loading branch information
geohot and Comma Device authored Apr 19, 2022
1 parent 5c48e7b commit 0ac35a6
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 28 deletions.
7 changes: 4 additions & 3 deletions selfdrive/loggerd/loggerd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,12 @@ void encoder_thread(LoggerdState *s, const LogCameraInfo &cam_info) {
// main encoder
encoders.push_back(new Encoder(cam_info.filename, cam_info.type, buf_info.width, buf_info.height,
cam_info.fps, cam_info.bitrate, cam_info.is_h265,
cam_info.downscale, cam_info.record));
buf_info.width, buf_info.height, cam_info.record));
// qcamera encoder
if (cam_info.has_qcamera) {
encoders.push_back(new Encoder(qcam_info.filename, cam_info.type, qcam_info.frame_width, qcam_info.frame_height,
qcam_info.fps, qcam_info.bitrate, qcam_info.is_h265, qcam_info.downscale));
encoders.push_back(new Encoder(qcam_info.filename, cam_info.type, buf_info.width, buf_info.height,
qcam_info.fps, qcam_info.bitrate, qcam_info.is_h265,
qcam_info.frame_width, qcam_info.frame_height));
}
}

Expand Down
5 changes: 0 additions & 5 deletions selfdrive/loggerd/loggerd.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ struct LogCameraInfo {
int fps;
int bitrate;
bool is_h265;
bool downscale;
bool has_qcamera;
bool trigger_rotate;
bool enable;
Expand All @@ -64,7 +63,6 @@ const LogCameraInfo cameras_logged[] = {
.fps = MAIN_FPS,
.bitrate = MAIN_BITRATE,
.is_h265 = true,
.downscale = false,
.has_qcamera = true,
.trigger_rotate = true,
.enable = true,
Expand All @@ -77,7 +75,6 @@ const LogCameraInfo cameras_logged[] = {
.fps = MAIN_FPS,
.bitrate = DCAM_BITRATE,
.is_h265 = true,
.downscale = false,
.has_qcamera = false,
.trigger_rotate = true,
.enable = true,
Expand All @@ -90,7 +87,6 @@ const LogCameraInfo cameras_logged[] = {
.fps = MAIN_FPS,
.bitrate = MAIN_BITRATE,
.is_h265 = true,
.downscale = false,
.has_qcamera = false,
.trigger_rotate = true,
.enable = Hardware::TICI(),
Expand All @@ -102,7 +98,6 @@ const LogCameraInfo qcam_info = {
.fps = MAIN_FPS,
.bitrate = 256000,
.is_h265 = false,
.downscale = true,
.frame_width = Hardware::TICI() ? 526 : 480,
.frame_height = Hardware::TICI() ? 330 : 360 // keep pixel count the same?
};
Expand Down
9 changes: 5 additions & 4 deletions selfdrive/loggerd/omx_encoder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -154,16 +154,15 @@ static const char* omx_color_fomat_name(uint32_t format) {


// ***** encoder functions *****
OmxEncoder::OmxEncoder(const char* filename, CameraType type, int width, int height, int fps, int bitrate, bool h265, bool downscale, bool write) {
OmxEncoder::OmxEncoder(const char* filename, CameraType type, int in_width, int in_height, int fps, int bitrate, bool h265, int out_width, int out_height, bool write)
: in_width_(in_width), in_height_(in_height), width(out_width), height(out_height) {
this->filename = filename;
this->type = type;
this->write = write;
this->width = width;
this->height = height;
this->fps = fps;
this->remuxing = !h265;

this->downscale = downscale;
this->downscale = in_width != out_width || in_height != out_height;
if (this->downscale) {
this->y_ptr2 = (uint8_t *)malloc(this->width*this->height);
this->u_ptr2 = (uint8_t *)malloc(this->width*this->height/4);
Expand Down Expand Up @@ -464,6 +463,8 @@ void OmxEncoder::handle_out_buf(OmxEncoder *e, OmxBuffer *out_buf) {

int OmxEncoder::encode_frame(const uint8_t *y_ptr, const uint8_t *u_ptr, const uint8_t *v_ptr,
int in_width, int in_height, uint64_t ts) {
assert(in_width == this->in_width_);
assert(in_height == this->in_height_);
int err;
if (!this->is_open) {
return -1;
Expand Down
3 changes: 2 additions & 1 deletion selfdrive/loggerd/omx_encoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ struct OmxBuffer {
// OmxEncoder, lossey codec using hardware hevc
class OmxEncoder : public VideoEncoder {
public:
OmxEncoder(const char* filename, CameraType type, int width, int height, int fps, int bitrate, bool h265, bool downscale, bool write = true);
OmxEncoder(const char* filename, CameraType type, int width, int height, int fps, int bitrate, bool h265, int out_width, int out_height, bool write = true);
~OmxEncoder();
int encode_frame(const uint8_t *y_ptr, const uint8_t *u_ptr, const uint8_t *v_ptr,
int in_width, int in_height, uint64_t ts);
Expand All @@ -43,6 +43,7 @@ class OmxEncoder : public VideoEncoder {
static void write_and_broadcast_handler(OmxEncoder *e);
static void handle_out_buf(OmxEncoder *e, OmxBuffer *out_buf);

int in_width_, in_height_;
int width, height, fps;
char vid_path[1024];
char lock_path[1024];
Expand Down
28 changes: 15 additions & 13 deletions selfdrive/loggerd/raw_logger.cc
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,9 @@ extern "C" {
#include "selfdrive/common/swaglog.h"
#include "selfdrive/common/util.h"

RawLogger::RawLogger(const char* filename, CameraType type, int width, int height, int fps,
int bitrate, bool h265, bool downscale, bool write)
: filename(filename), fps(fps) {
RawLogger::RawLogger(const char* filename, CameraType type, int in_width, int in_height, int fps,
int bitrate, bool h265, int out_width, int out_height, bool write)
: in_width_(in_width), in_height_(in_height), filename(filename), fps(fps) {

// TODO: respect write arg

Expand All @@ -34,8 +34,8 @@ RawLogger::RawLogger(const char* filename, CameraType type, int width, int heigh

codec_ctx = avcodec_alloc_context3(codec);
assert(codec_ctx);
codec_ctx->width = width;
codec_ctx->height = height;
codec_ctx->width = out_width;
codec_ctx->height = out_height;
codec_ctx->pix_fmt = AV_PIX_FMT_YUV420P;

// codec_ctx->thread_count = 2;
Expand All @@ -51,14 +51,14 @@ RawLogger::RawLogger(const char* filename, CameraType type, int width, int heigh
frame = av_frame_alloc();
assert(frame);
frame->format = codec_ctx->pix_fmt;
frame->width = width;
frame->height = height;
frame->linesize[0] = width;
frame->linesize[1] = width/2;
frame->linesize[2] = width/2;

if (downscale) {
downscale_buf.resize(width * height * 3 / 2);
frame->width = out_width;
frame->height = out_height;
frame->linesize[0] = out_width;
frame->linesize[1] = out_width/2;
frame->linesize[2] = out_width/2;

if (in_width != out_width || in_height != out_height) {
downscale_buf.resize(out_width * out_height * 3 / 2);
}
}

Expand Down Expand Up @@ -122,6 +122,8 @@ void RawLogger::encoder_close() {

int RawLogger::encode_frame(const uint8_t *y_ptr, const uint8_t *u_ptr, const uint8_t *v_ptr,
int in_width, int in_height, uint64_t ts) {
assert(in_width == this->in_width_);
assert(in_height == this->in_height_);
AVPacket pkt;
av_init_packet(&pkt);
pkt.data = NULL;
Expand Down
5 changes: 3 additions & 2 deletions selfdrive/loggerd/raw_logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ extern "C" {

class RawLogger : public VideoEncoder {
public:
RawLogger(const char* filename, CameraType type, int width, int height, int fps,
int bitrate, bool h265, bool downscale, bool write = true);
RawLogger(const char* filename, CameraType type, int in_width, int in_height, int fps,
int bitrate, bool h265, int out_width, int out_height, bool write = true);
~RawLogger();
int encode_frame(const uint8_t *y_ptr, const uint8_t *u_ptr, const uint8_t *v_ptr,
int in_width, int in_height, uint64_t ts);
Expand All @@ -30,6 +30,7 @@ class RawLogger : public VideoEncoder {
int counter = 0;
bool is_open = false;

int in_width_, in_height_;
std::string vid_path, lock_path;

const AVCodec *codec = NULL;
Expand Down

0 comments on commit 0ac35a6

Please sign in to comment.