code cleanups and fixed gif export #360

This commit is contained in:
itsmattkc
2019-01-30 21:36:07 +11:00
parent 6f3a826da7
commit c19a2dad7d
8 changed files with 67 additions and 51 deletions
+24 -15
View File
@@ -38,7 +38,6 @@ ExportThread::ExportThread(QObject *parent) :
vcodec = nullptr;
vcodec_ctx = nullptr;
video_frame = nullptr;
sws_frame = nullptr;
sws_ctx = nullptr;
audio_stream = nullptr;
acodec = nullptr;
@@ -125,18 +124,17 @@ bool ExportThread::setupVideo() {
vcodec_ctx->flags |= AV_CODEC_FLAG_GLOBAL_HEADER;
}
if (vcodec_ctx->codec_id == AV_CODEC_ID_H264) {
/*char buffer[50];
itoa(vcodec_ctx, buffer, 10);*/
//av_opt_set(vcodec_ctx->priv_data, "preset", "fast", AV_OPT_SEARCH_CHILDREN);
//av_opt_set(vcodec_ctx->priv_data, "x264opts", "opencl", AV_OPT_SEARCH_CHILDREN);
switch (vcodec_ctx->codec_id) {
case AV_CODEC_ID_H264:
switch (video_compression_type) {
case COMPRESSION_TYPE_CFR:
av_opt_set(vcodec_ctx->priv_data, "crf", QString::number(static_cast<int>(video_bitrate)).toUtf8(), AV_OPT_SEARCH_CHILDREN);
break;
}
break;
case AV_CODEC_ID_GIF:
av_opt_set(vcodec_ctx->priv_data, "image", "1", AV_OPT_SEARCH_CHILDREN);
break;
}
AVDictionary* opts = nullptr;
@@ -180,12 +178,6 @@ bool ExportThread::setupVideo() {
nullptr
);
sws_frame = av_frame_alloc();
sws_frame->format = vcodec_ctx->pix_fmt;
sws_frame->width = video_width;
sws_frame->height = video_height;
av_frame_get_buffer(sws_frame, 0);
return true;
}
@@ -369,12 +361,30 @@ void ExportThread::run() {
// encode last frame while rendering next frame
double timecode_secs = (double) (sequence->playhead-start_frame) / sequence->frame_rate;
if (video_enabled) {
// create sws_frame for converting pixel format
//
// - I'm not sure why, but we have to alloc/free sws_frame every frame, or it breaks GIF exporting.
// - (i.e. GIFs get stuck on the first frame)
// - The same problem/solution can be seen here: https://stackoverflow.com/a/38997739
// - Perhaps this is the intended way to use swscale, but it seems inefficient.
// - Anyway, here we are.
//
sws_frame = av_frame_alloc();
sws_frame->format = vcodec_ctx->pix_fmt;
sws_frame->width = video_width;
sws_frame->height = video_height;
av_frame_get_buffer(sws_frame, 0);
// change pixel format
sws_scale(sws_ctx, video_frame->data, video_frame->linesize, 0, video_frame->height, sws_frame->data, sws_frame->linesize);
sws_frame->pts = qRound(timecode_secs/av_q2d(video_stream->time_base));
// send to encoder
if (!encode(fmt_ctx, vcodec_ctx, sws_frame, &video_pkt, video_stream, false)) continueEncode = false;
av_frame_free(&sws_frame);
}
if (audio_enabled) {
// do we need to encode more audio samples?
@@ -481,7 +491,6 @@ void ExportThread::run() {
if (sws_ctx != nullptr) {
sws_freeContext(sws_ctx);
av_frame_free(&sws_frame);
}
if (swr_ctx != nullptr) {
swr_free(&swr_ctx);
+8 -7
View File
@@ -19,6 +19,7 @@
#include <QDateTime>
#define WAVEFORM_RESOLUTION 64
#define THUMBNAIL_RESOLUTION 120
extern "C" {
#include <libavformat/avformat.h>
@@ -30,7 +31,7 @@ extern "C" {
QSemaphore sem(5); // only 5 preview generators can run at one time
PreviewGenerator::PreviewGenerator(Media* i, Footage* m, bool r) :
QThread(0),
QThread(nullptr),
fmt_ctx(nullptr),
media(i),
footage(m),
@@ -50,7 +51,7 @@ PreviewGenerator::PreviewGenerator(Media* i, Footage* m, bool r) :
void PreviewGenerator::parse_media() {
// detect video/audio streams in file
for (int i=0;i<(int)fmt_ctx->nb_streams;i++) {
for (int i=0;i<int(fmt_ctx->nb_streams);i++) {
// Find the decoder for the video stream
if (avcodec_find_decoder(fmt_ctx->streams[i]->codecpar->codec_id) == nullptr) {
qCritical() << "Unsupported codec in stream" << i << "of file" << footage->name;
@@ -106,7 +107,7 @@ void PreviewGenerator::parse_media() {
append = true;
} else if (fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
ms.audio_channels = fmt_ctx->streams[i]->codecpar->channels;
ms.audio_layout = fmt_ctx->streams[i]->codecpar->channel_layout;
ms.audio_layout = int(fmt_ctx->streams[i]->codecpar->channel_layout);
ms.audio_frequency = fmt_ctx->streams[i]->codecpar->sample_rate;
append = true;
@@ -277,9 +278,9 @@ void PreviewGenerator::generate_waveform() {
if (s != nullptr) {
if (fmt_ctx->streams[packet->stream_index]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
if (!s->preview_done) {
int dstH = 120;
int dstW = dstH * ((float)temp_frame->width/(float)temp_frame->height);
uint8_t* data = new uint8_t[dstW*dstH*4];
int dstH = THUMBNAIL_RESOLUTION;
int dstW = qRound(dstH * (float(temp_frame->width)/float(temp_frame->height)));
uint8_t* data = new uint8_t[size_t(dstW*dstH*4)];
sws_ctx = sws_getContext(
temp_frame->width,
@@ -414,7 +415,7 @@ void PreviewGenerator::generate_waveform() {
maximum_stream = i;
}
}
footage->length = (double) media_lengths[maximum_stream] / av_q2d(fmt_ctx->streams[maximum_stream]->avg_frame_rate) * AV_TIME_BASE; // TODO redo with PTS
footage->length = double(media_lengths[maximum_stream]) / av_q2d(fmt_ctx->streams[maximum_stream]->avg_frame_rate) * AV_TIME_BASE; // TODO redo with PTS
finalize_media();
}
delete [] media_lengths;