2017-06-13 00:38:32 +02:00
|
|
|
#include "encoder.hpp"
|
|
|
|
|
|
|
|
#include <settings.hpp>
|
|
|
|
extern "C" {
|
|
|
|
#include <libavutil/imgutils.h>
|
|
|
|
#include <libavutil/opt.h>
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void throwAVErr(int ret, std::string section) {
|
|
|
|
char err[AV_ERROR_MAX_STRING_SIZE];
|
|
|
|
av_make_error_string(err, AV_ERROR_MAX_STRING_SIZE, ret);
|
|
|
|
std::string newString(err);
|
|
|
|
throw std::runtime_error("Error during: " + section + ": " + newString);
|
|
|
|
}
|
|
|
|
|
2017-06-17 17:32:47 +02:00
|
|
|
#define OR_DEF(s, e1, e2) s ? s->e1 : e2
|
|
|
|
|
|
|
|
Encoder::Encoder(QString &targetFile, QSize res, CodecSettings *settings) {
|
2017-06-13 00:38:32 +02:00
|
|
|
int ret;
|
|
|
|
// Format
|
|
|
|
ret = avformat_alloc_output_context2(&fc, NULL, NULL, targetFile.toLocal8Bit().constData());
|
|
|
|
if (ret < 0) throwAVErr(ret, "format alloc");
|
|
|
|
|
|
|
|
// Stream
|
|
|
|
out->st = avformat_new_stream(fc, NULL);
|
|
|
|
if (!out->st) throw std::runtime_error("Unable to allocate video context");
|
|
|
|
out->st->id = fc->nb_streams - 1;
|
|
|
|
if (fc->oformat->flags & AVFMT_GLOBALHEADER) fc->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
|
|
|
|
av_dump_format(fc, 0, targetFile.toLocal8Bit().constData(), 1);
|
|
|
|
|
|
|
|
// Codec
|
|
|
|
if (!fc->oformat->video_codec) throw std::runtime_error("Video codec not found");
|
|
|
|
codec = avcodec_find_encoder(fc->oformat->video_codec);
|
|
|
|
if (!codec) throw std::runtime_error("Video codec not found");
|
|
|
|
out->enc = avcodec_alloc_context3(codec);
|
|
|
|
if (!out->enc) throw std::runtime_error("Unable to allocate video context");
|
|
|
|
|
|
|
|
int fps = settings::settings().value("recording/framerate", 30).toInt();
|
|
|
|
|
|
|
|
out->enc->codec_id = codec->id;
|
2017-06-13 10:56:08 +02:00
|
|
|
out->enc->codec = codec;
|
2017-06-13 00:38:32 +02:00
|
|
|
|
2017-06-17 17:32:47 +02:00
|
|
|
out->enc->bit_rate = OR_DEF(settings, bitrate, 400000);
|
2017-06-13 00:38:32 +02:00
|
|
|
out->enc->width = res.width() % 2 ? res.width() - 1 : res.width();
|
|
|
|
out->enc->height = res.height() % 2 ? res.height() - 1 : res.height();
|
|
|
|
size = QSize(out->enc->width, out->enc->height);
|
|
|
|
out->st->time_base = { 1, fps };
|
|
|
|
out->enc->time_base = out->st->time_base;
|
|
|
|
|
2017-06-17 17:32:47 +02:00
|
|
|
out->enc->gop_size = OR_DEF(settings, gopSize, 12);
|
2017-06-13 00:38:32 +02:00
|
|
|
out->enc->pix_fmt = AV_PIX_FMT_YUV420P; // blaze it
|
2017-06-17 23:18:16 +02:00
|
|
|
AVDictionary *dict = NULL;
|
2017-06-17 17:32:47 +02:00
|
|
|
if (out->enc->codec_id == AV_CODEC_ID_GIF)
|
2017-06-13 10:56:08 +02:00
|
|
|
out->enc->pix_fmt = AV_PIX_FMT_RGB8;
|
2017-06-17 17:32:47 +02:00
|
|
|
else if (out->enc->codec_id == AV_CODEC_ID_H264 || out->enc->codec_id == AV_CODEC_ID_H265) {
|
2017-06-27 18:29:08 +02:00
|
|
|
av_dict_set(&dict, "preset", settings ? settings->h264Profile.toLocal8Bit().constData() : "medium", 0);
|
2017-06-17 23:18:16 +02:00
|
|
|
av_dict_set_int(&dict, "crf", OR_DEF(settings, h264Crf, 12), 0);
|
2017-06-17 17:32:47 +02:00
|
|
|
} else if (out->enc->codec_id == AV_CODEC_ID_VP8 || out->enc->codec_id == AV_CODEC_ID_VP9)
|
2017-06-17 23:18:16 +02:00
|
|
|
av_dict_set_int(&dict, "lossless", OR_DEF(settings, vp9Lossless, false), 0);
|
2017-06-13 00:38:32 +02:00
|
|
|
|
|
|
|
|
2017-06-17 23:18:16 +02:00
|
|
|
ret = avcodec_open2(out->enc, codec, &dict);
|
|
|
|
av_dict_free(&dict);
|
2017-06-13 00:38:32 +02:00
|
|
|
if (ret < 0) throwAVErr(ret, "codec open");
|
2017-06-17 17:32:47 +02:00
|
|
|
if (codec->capabilities & AV_CODEC_CAP_DR1) avcodec_align_dimensions(out->enc, &out->enc->width, &out->enc->height);
|
2017-06-13 00:38:32 +02:00
|
|
|
|
|
|
|
ret = avcodec_parameters_from_context(out->st->codecpar, out->enc);
|
|
|
|
if (ret < 0) throwAVErr(ret, "stream opt copy");
|
|
|
|
|
|
|
|
// Frames
|
|
|
|
out->frame = av_frame_alloc();
|
|
|
|
if (!out->frame) {
|
|
|
|
throw std::runtime_error("frame alloc");
|
|
|
|
}
|
|
|
|
out->frame->format = out->enc->pix_fmt;
|
|
|
|
out->frame->width = out->enc->width;
|
|
|
|
out->frame->height = out->enc->height;
|
|
|
|
ret = av_frame_get_buffer(out->frame, 32);
|
|
|
|
if (ret < 0) throwAVErr(ret, "frame buffer alloc");
|
|
|
|
|
|
|
|
// Writer
|
|
|
|
ret = avio_open(&fc->pb, targetFile.toLocal8Bit().constData(), AVIO_FLAG_WRITE);
|
|
|
|
if (ret < 0) throwAVErr(ret, "writer open");
|
|
|
|
ret = avformat_write_header(fc, NULL);
|
|
|
|
if (ret < 0) throwAVErr(ret, "write header");
|
|
|
|
|
|
|
|
success = true;
|
|
|
|
}
|
|
|
|
|
2017-06-26 15:43:12 +02:00
|
|
|
void Encoder::setFrameRGB(QImage img) {
|
|
|
|
uint8_t *rgb = (uint8_t *)img.bits();
|
2017-06-13 00:38:32 +02:00
|
|
|
int ret = av_frame_make_writable(out->frame);
|
|
|
|
if (ret < 0) throwAVErr(ret, "setFrameRGB");
|
2017-06-26 15:43:12 +02:00
|
|
|
int lineSize[1] = { img.bytesPerLine() };
|
2017-06-13 00:38:32 +02:00
|
|
|
out->sws = sws_getCachedContext(out->sws, out->enc->width, out->enc->height, AV_PIX_FMT_RGB24, out->enc->width,
|
|
|
|
out->enc->height, (AVPixelFormat)out->frame->format, 0, 0, 0, 0);
|
|
|
|
sws_scale(out->sws, (const uint8_t *const *)&rgb, lineSize, 0, out->enc->height, out->frame->data, out->frame->linesize);
|
|
|
|
out->frame->pts = out->nextPts++;
|
|
|
|
}
|
|
|
|
|
|
|
|
Encoder::~Encoder() {
|
|
|
|
end();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Encoder::addFrame(QImage frm) {
|
|
|
|
if (!success) return false;
|
|
|
|
if (frm.size() != size) frm = frm.copy(QRect(QPoint(0, 0), size));
|
|
|
|
if (frm.format() != QImage::Format_RGB888) frm = frm.convertToFormat(QImage::Format_RGB888);
|
2017-06-26 15:43:12 +02:00
|
|
|
setFrameRGB(frm);
|
2017-06-13 10:56:08 +02:00
|
|
|
AVPacket pkt;
|
|
|
|
pkt.size = 0;
|
|
|
|
pkt.data = NULL;
|
2017-06-13 00:38:32 +02:00
|
|
|
av_init_packet(&pkt);
|
2017-06-24 12:15:04 +02:00
|
|
|
int ret = avcodec_send_frame(out->enc, out->frame);
|
|
|
|
if (ret == AVERROR(EAGAIN)) {
|
|
|
|
do {
|
|
|
|
ret = avcodec_receive_packet(out->enc, &pkt);
|
|
|
|
if (ret < 0) {
|
|
|
|
if (ret != AVERROR(EAGAIN))
|
|
|
|
throwAVErr(ret, "receive packet");
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
av_packet_rescale_ts(&pkt, out->enc->time_base, out->st->time_base);
|
|
|
|
pkt.stream_index = out->st->index;
|
|
|
|
ret = av_interleaved_write_frame(fc, &pkt);
|
|
|
|
} while (ret >= 0);
|
|
|
|
if (ret < 0 && ret != AVERROR(EAGAIN)) {
|
|
|
|
av_packet_unref(&pkt);
|
|
|
|
throwAVErr(ret, "send frame");
|
|
|
|
}
|
2017-06-13 00:38:32 +02:00
|
|
|
}
|
|
|
|
av_packet_unref(&pkt);
|
2017-06-24 12:15:04 +02:00
|
|
|
if (ret < 0 && ret != AVERROR(EAGAIN)) throwAVErr(ret, "send frame");
|
2017-06-13 00:38:32 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Encoder::isRunning() {
|
|
|
|
return success;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Encoder::end() {
|
2017-06-26 15:43:12 +02:00
|
|
|
if (ended) return false;
|
|
|
|
ended = true;
|
2017-06-13 00:38:32 +02:00
|
|
|
if (!success) {
|
|
|
|
goto cleanup;
|
|
|
|
}
|
2017-06-24 12:15:04 +02:00
|
|
|
int ret;
|
|
|
|
AVPacket pkt;
|
|
|
|
pkt.size = 0;
|
|
|
|
pkt.data = NULL;
|
|
|
|
av_init_packet(&pkt);
|
|
|
|
do {
|
|
|
|
ret = avcodec_receive_packet(out->enc, &pkt);
|
|
|
|
if (ret < 0) break;
|
|
|
|
av_packet_rescale_ts(&pkt, out->enc->time_base, out->st->time_base);
|
|
|
|
pkt.stream_index = out->st->index;
|
|
|
|
av_interleaved_write_frame(fc, &pkt);
|
|
|
|
} while (ret >= 0);
|
2017-06-13 00:38:32 +02:00
|
|
|
av_write_trailer(fc);
|
|
|
|
cleanup:
|
|
|
|
avcodec_free_context(&out->enc);
|
|
|
|
av_frame_free(&out->frame);
|
|
|
|
sws_freeContext(out->sws);
|
|
|
|
delete out;
|
2017-06-13 10:56:08 +02:00
|
|
|
if (!(fc->oformat->flags & AVFMT_NOFILE)) //
|
|
|
|
avio_closep(&fc->pb);
|
2017-06-13 00:38:32 +02:00
|
|
|
avformat_free_context(fc);
|
|
|
|
return success;
|
|
|
|
}
|