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.
		
		
		
		
			
				
					154 lines
				
				4.1 KiB
			
		
		
			
		
	
	
					154 lines
				
				4.1 KiB
			| 
											6 years ago
										 | #pragma clang diagnostic ignored "-Wdeprecated-declarations"
 | ||
|  | 
 | ||
| 
											3 years ago
										 | #include "system/loggerd/encoder/ffmpeg_encoder.h"
 | ||
| 
											6 years ago
										 | 
 | ||
|  | #include <fcntl.h>
 | ||
|  | #include <unistd.h>
 | ||
|  | 
 | ||
| 
											5 years ago
										 | #include <cassert>
 | ||
|  | #include <cstdio>
 | ||
|  | #include <cstdlib>
 | ||
|  | 
 | ||
| 
											6 years ago
										 | #define __STDC_CONSTANT_MACROS
 | ||
|  | 
 | ||
| 
											4 years ago
										 | #include "libyuv.h"
 | ||
|  | 
 | ||
| 
											6 years ago
										 | extern "C" {
 | ||
|  | #include <libavcodec/avcodec.h>
 | ||
|  | #include <libavformat/avformat.h>
 | ||
| 
											5 years ago
										 | #include <libavutil/imgutils.h>
 | ||
| 
											6 years ago
										 | }
 | ||
|  | 
 | ||
| 
											3 years ago
										 | #include "common/swaglog.h"
 | ||
|  | #include "common/util.h"
 | ||
| 
											6 years ago
										 | 
 | ||
| 
											4 years ago
										 | const int env_debug_encoder = (getenv("DEBUG_ENCODER") != NULL) ? atoi(getenv("DEBUG_ENCODER")) : 0;
 | ||
|  | 
 | ||
|  | void FfmpegEncoder::encoder_init() {
 | ||
| 
											6 years ago
										 |   frame = av_frame_alloc();
 | ||
|  |   assert(frame);
 | ||
| 
											4 years ago
										 |   frame->format = AV_PIX_FMT_YUV420P;
 | ||
| 
											4 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;
 | ||
|  | 
 | ||
| 
											3 years ago
										 |   convert_buf.resize(in_width * in_height * 3 / 2);
 | ||
|  | 
 | ||
| 
											4 years ago
										 |   if (in_width != out_width || in_height != out_height) {
 | ||
|  |     downscale_buf.resize(out_width * out_height * 3 / 2);
 | ||
| 
											4 years ago
										 |   }
 | ||
| 
											4 years ago
										 | 
 | ||
|  |   publisher_init();
 | ||
| 
											6 years ago
										 | }
 | ||
|  | 
 | ||
| 
											4 years ago
										 | FfmpegEncoder::~FfmpegEncoder() {
 | ||
| 
											4 years ago
										 |   encoder_close();
 | ||
| 
											6 years ago
										 |   av_frame_free(&frame);
 | ||
|  | }
 | ||
|  | 
 | ||
| 
											4 years ago
										 | void FfmpegEncoder::encoder_open(const char* path) {
 | ||
| 
											4 years ago
										 |   const AVCodec *codec = avcodec_find_encoder(AV_CODEC_ID_FFVHUFF);
 | ||
| 
											4 years ago
										 | 
 | ||
|  |   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);
 | ||
| 
											6 years ago
										 |   is_open = true;
 | ||
| 
											4 years ago
										 |   segment_num++;
 | ||
|  |   counter = 0;
 | ||
| 
											6 years ago
										 | }
 | ||
|  | 
 | ||
| 
											4 years ago
										 | void FfmpegEncoder::encoder_close() {
 | ||
| 
											6 years ago
										 |   if (!is_open) return;
 | ||
| 
											3 years ago
										 | 
 | ||
| 
											4 years ago
										 |   writer_close();
 | ||
| 
											3 years ago
										 |   avcodec_free_context(&codec_ctx);
 | ||
| 
											6 years ago
										 |   is_open = false;
 | ||
|  | }
 | ||
|  | 
 | ||
| 
											3 years ago
										 | int FfmpegEncoder::encode_frame(VisionBuf* buf, VisionIpcBufExtra *extra) {
 | ||
|  |   assert(buf->width == this->in_width);
 | ||
|  |   assert(buf->height == this->in_height);
 | ||
|  | 
 | ||
|  |   uint8_t *cy = convert_buf.data();
 | ||
|  |   uint8_t *cu = cy + in_width * in_height;
 | ||
|  |   uint8_t *cv = cu + (in_width / 2) * (in_height / 2);
 | ||
|  |   libyuv::NV12ToI420(buf->y, buf->stride,
 | ||
|  |                      buf->uv, buf->stride,
 | ||
|  |                      cy, in_width,
 | ||
|  |                      cu, in_width/2,
 | ||
|  |                      cv, in_width/2,
 | ||
|  |                      in_width, in_height);
 | ||
| 
											6 years ago
										 | 
 | ||
| 
											4 years ago
										 |   if (downscale_buf.size() > 0) {
 | ||
|  |     uint8_t *out_y = downscale_buf.data();
 | ||
| 
											4 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(cy, in_width,
 | ||
|  |                       cu, in_width/2,
 | ||
|  |                       cv, in_width/2,
 | ||
| 
											4 years ago
										 |                       in_width, in_height,
 | ||
| 
											4 years ago
										 |                       out_y, frame->width,
 | ||
|  |                       out_u, frame->width/2,
 | ||
|  |                       out_v, frame->width/2,
 | ||
|  |                       frame->width, frame->height,
 | ||
| 
											4 years ago
										 |                       libyuv::kFilterNone);
 | ||
|  |     frame->data[0] = out_y;
 | ||
|  |     frame->data[1] = out_u;
 | ||
|  |     frame->data[2] = out_v;
 | ||
|  |   } else {
 | ||
| 
											3 years ago
										 |     frame->data[0] = cy;
 | ||
|  |     frame->data[1] = cu;
 | ||
|  |     frame->data[2] = cv;
 | ||
| 
											4 years ago
										 |   }
 | ||
| 
											4 years ago
										 |   frame->pts = counter*50*1000; // 50ms per frame
 | ||
| 
											6 years ago
										 | 
 | ||
|  |   int ret = counter;
 | ||
|  | 
 | ||
| 
											4 years ago
										 |   int err = avcodec_send_frame(this->codec_ctx, frame);
 | ||
| 
											4 years ago
										 |   if (err < 0) {
 | ||
|  |     LOGE("avcodec_send_frame error %d", err);
 | ||
| 
											6 years ago
										 |     ret = -1;
 | ||
| 
											4 years ago
										 |   }
 | ||
|  | 
 | ||
| 
											4 years ago
										 |   AVPacket pkt;
 | ||
|  |   av_init_packet(&pkt);
 | ||
|  |   pkt.data = NULL;
 | ||
|  |   pkt.size = 0;
 | ||
|  |   while (ret >= 0) {
 | ||
| 
											4 years ago
										 |     err = avcodec_receive_packet(this->codec_ctx, &pkt);
 | ||
| 
											4 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;
 | ||
|  |     }
 | ||
|  | 
 | ||
| 
											4 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));
 | ||
|  | 
 | ||
| 
											4 years ago
										 |     counter++;
 | ||
| 
											6 years ago
										 |   }
 | ||
| 
											4 years ago
										 |   av_packet_unref(&pkt);
 | ||
| 
											6 years ago
										 |   return ret;
 | ||
|  | }
 |