You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
141 lines
3.8 KiB
141 lines
3.8 KiB
5 years ago
|
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
|
||
|
|
||
3 years ago
|
#include "selfdrive/loggerd/encoder/ffmpeg_encoder.h"
|
||
5 years ago
|
|
||
|
#include <fcntl.h>
|
||
|
#include <unistd.h>
|
||
|
|
||
4 years ago
|
#include <cassert>
|
||
|
#include <cstdio>
|
||
|
#include <cstdlib>
|
||
|
|
||
5 years ago
|
#define __STDC_CONSTANT_MACROS
|
||
|
|
||
3 years ago
|
#include "libyuv.h"
|
||
|
|
||
5 years ago
|
extern "C" {
|
||
|
#include <libavcodec/avcodec.h>
|
||
|
#include <libavformat/avformat.h>
|
||
4 years ago
|
#include <libavutil/imgutils.h>
|
||
5 years ago
|
}
|
||
|
|
||
4 years ago
|
#include "selfdrive/common/swaglog.h"
|
||
|
#include "selfdrive/common/util.h"
|
||
5 years ago
|
|
||
3 years ago
|
const int env_debug_encoder = (getenv("DEBUG_ENCODER") != NULL) ? atoi(getenv("DEBUG_ENCODER")) : 0;
|
||
|
|
||
|
void FfmpegEncoder::encoder_init() {
|
||
5 years ago
|
frame = av_frame_alloc();
|
||
|
assert(frame);
|
||
3 years ago
|
frame->format = AV_PIX_FMT_YUV420P;
|
||
3 years ago
|
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);
|
||
3 years ago
|
}
|
||
3 years ago
|
|
||
|
publisher_init();
|
||
5 years ago
|
}
|
||
|
|
||
3 years ago
|
FfmpegEncoder::~FfmpegEncoder() {
|
||
3 years ago
|
encoder_close();
|
||
5 years ago
|
av_frame_free(&frame);
|
||
|
}
|
||
|
|
||
3 years ago
|
void FfmpegEncoder::encoder_open(const char* path) {
|
||
|
AVCodec *codec = avcodec_find_encoder(AV_CODEC_ID_FFVHUFF);
|
||
|
|
||
|
this->codec_ctx = avcodec_alloc_context3(codec);
|
||
|
assert(this->codec_ctx);
|
||
|
this->codec_ctx->width = frame->width;
|
||
|
this->codec_ctx->height = frame->height;
|
||
|
this->codec_ctx->pix_fmt = AV_PIX_FMT_YUV420P;
|
||
|
this->codec_ctx->time_base = (AVRational){ 1, fps };
|
||
|
int err = avcodec_open2(this->codec_ctx, codec, NULL);
|
||
|
assert(err >= 0);
|
||
|
|
||
|
writer_open(path);
|
||
5 years ago
|
is_open = true;
|
||
3 years ago
|
segment_num++;
|
||
|
counter = 0;
|
||
5 years ago
|
}
|
||
|
|
||
3 years ago
|
void FfmpegEncoder::encoder_close() {
|
||
5 years ago
|
if (!is_open) return;
|
||
3 years ago
|
writer_close();
|
||
5 years ago
|
is_open = false;
|
||
|
}
|
||
|
|
||
3 years ago
|
int FfmpegEncoder::encode_frame(const uint8_t *y_ptr, const uint8_t *u_ptr, const uint8_t *v_ptr,
|
||
|
int in_width_, int in_height_, VisionIpcBufExtra *extra) {
|
||
|
assert(in_width_ == this->in_width);
|
||
|
assert(in_height_ == this->in_height);
|
||
5 years ago
|
|
||
3 years ago
|
if (downscale_buf.size() > 0) {
|
||
|
uint8_t *out_y = downscale_buf.data();
|
||
3 years ago
|
uint8_t *out_u = out_y + frame->width * frame->height;
|
||
|
uint8_t *out_v = out_u + (frame->width / 2) * (frame->height / 2);
|
||
3 years ago
|
libyuv::I420Scale(y_ptr, in_width,
|
||
|
u_ptr, in_width/2,
|
||
|
v_ptr, in_width/2,
|
||
|
in_width, in_height,
|
||
3 years ago
|
out_y, frame->width,
|
||
|
out_u, frame->width/2,
|
||
|
out_v, frame->width/2,
|
||
|
frame->width, frame->height,
|
||
3 years ago
|
libyuv::kFilterNone);
|
||
|
frame->data[0] = out_y;
|
||
|
frame->data[1] = out_u;
|
||
|
frame->data[2] = out_v;
|
||
|
} else {
|
||
|
frame->data[0] = (uint8_t*)y_ptr;
|
||
|
frame->data[1] = (uint8_t*)u_ptr;
|
||
|
frame->data[2] = (uint8_t*)v_ptr;
|
||
|
}
|
||
3 years ago
|
frame->pts = counter*50*1000; // 50ms per frame
|
||
5 years ago
|
|
||
|
int ret = counter;
|
||
|
|
||
3 years ago
|
int err = avcodec_send_frame(this->codec_ctx, frame);
|
||
3 years ago
|
if (err < 0) {
|
||
|
LOGE("avcodec_send_frame error %d", err);
|
||
5 years ago
|
ret = -1;
|
||
3 years ago
|
}
|
||
|
|
||
3 years ago
|
AVPacket pkt;
|
||
|
av_init_packet(&pkt);
|
||
|
pkt.data = NULL;
|
||
|
pkt.size = 0;
|
||
|
while (ret >= 0) {
|
||
3 years ago
|
err = avcodec_receive_packet(this->codec_ctx, &pkt);
|
||
3 years ago
|
if (err == AVERROR_EOF) {
|
||
|
break;
|
||
|
} else if (err == AVERROR(EAGAIN)) {
|
||
|
// Encoder might need a few frames on startup to get started. Keep going
|
||
|
ret = 0;
|
||
|
break;
|
||
|
} else if (err < 0) {
|
||
|
LOGE("avcodec_receive_packet error %d", err);
|
||
|
ret = -1;
|
||
|
break;
|
||
|
}
|
||
|
|
||
3 years ago
|
if (env_debug_encoder) {
|
||
|
printf("%20s got %8d bytes flags %8x idx %4d id %8d\n", this->filename, pkt.size, pkt.flags, counter, extra->frame_id);
|
||
|
}
|
||
|
|
||
|
publisher_publish(this, segment_num, counter, *extra,
|
||
|
(pkt.flags & AV_PKT_FLAG_KEY) ? V4L2_BUF_FLAG_KEYFRAME : 0,
|
||
|
kj::arrayPtr<capnp::byte>(pkt.data, (size_t)0), // TODO: get the header
|
||
|
kj::arrayPtr<capnp::byte>(pkt.data, pkt.size));
|
||
|
|
||
3 years ago
|
counter++;
|
||
5 years ago
|
}
|
||
4 years ago
|
av_packet_unref(&pkt);
|
||
5 years ago
|
return ret;
|
||
|
}
|