massive rewrite of project data system

This commit is contained in:
itsmattkc
2018-12-13 14:29:09 +11:00
parent 6c3197aac6
commit ac1cb9d14f
44 changed files with 2562 additions and 2158 deletions
+639
View File
@@ -0,0 +1,639 @@
#include "loadthread.h"
#include "mainwindow.h"
#include "panels/panels.h"
#include "panels/project.h"
#include "project/footage.h"
#include "io/config.h"
#include "project/clip.h"
#include "project/sequence.h"
#include "project/transition.h"
#include "project/effect.h"
#include "playback/playback.h"
#include "ui_project.h"
#include "io/previewgenerator.h"
#include "dialogs/loaddialog.h"
#include "project/media.h"
#include "debug.h"
#include <QFile>
#include <QMessageBox>
#include <QTreeWidgetItem>
struct TransitionData {
int id;
QString name;
long length;
Clip* otc;
Clip* ctc;
};
LoadThread::LoadThread(LoadDialog* l, bool a) : ld(l), autorecovery(a) {
connect(this, SIGNAL(finished()), this, SLOT(deleteLater()));
connect(this, SIGNAL(success()), this, SLOT(success_func()));
}
const EffectMeta* get_meta_from_name(const QString& name, int type) {
for (int j=0;j<effects.size();j++) {
if (effects.at(j).name == name) {
return &effects.at(j);
}
}
return NULL;
}
void load_effect(QXmlStreamReader& stream, Clip* c) {
int effect_id = -1;
QString effect_name;
bool effect_enabled = true;
long effect_length = -1;
for (int j=0;j<stream.attributes().size();j++) {
const QXmlStreamAttribute& attr = stream.attributes().at(j);
if (attr.name() == "id") {
effect_id = attr.value().toInt();
} else if (attr.name() == "enabled") {
effect_enabled = (attr.value() == "1");
} else if (attr.name() == "name") {
effect_name = attr.value().toString();
} else if (attr.name() == "length") {
effect_length = attr.value().toLong();
}
}
// backwards compatibility with 180820
if (stream.name() == "effect" && effect_id != -1) {
switch (effect_id) {
case 0: effect_name = (c->track < 0) ? "Transform" : "Volume"; break;
case 1: effect_name = (c->track < 0) ? "Shake" : "Pan"; break;
case 2: effect_name = (c->track < 0) ? "Text" : "Noise"; break;
case 3: effect_name = (c->track < 0) ? "Solid" : "Tone"; break;
case 4: effect_name = "Invert"; break;
case 5: effect_name = "Chroma Key"; break;
case 6: effect_name = "Gaussian Blur"; break;
case 7: effect_name = "Crop"; break;
case 8: effect_name = "Flip"; break;
case 9: effect_name = "Box Blur"; break;
case 10: effect_name = "Wave"; break;
case 11: effect_name = "Temperature"; break;
}
}
// wait for effects to be loaded
effects_loaded.lock();
const EffectMeta* meta = NULL;
// find effect with this name
if (!effect_name.isEmpty()) {
meta = get_meta_from_name(effect_name, (c->track < 0) ? EFFECT_TYPE_VIDEO : EFFECT_TYPE_AUDIO);
}
effects_loaded.unlock();
if (meta == NULL) {
dout << "[WARNING] An effect used by this project is missing. It was not loaded.";
} else {
QString tag = stream.name().toString();
if (tag == "opening" || tag == "closing") {
// TODO replace NULL/s with something else
int transition_index = create_transition(c, NULL, meta);
Transition* t = c->sequence->transitions.at(transition_index);
if (effect_length > -1) t->set_length(effect_length);
t->set_enabled(effect_enabled);
t->load(stream);
if (tag == "opening") {
c->opening_transition = transition_index;
} else {
c->closing_transition = transition_index;
}
} else {
Effect* e = create_effect(c, meta);
e->set_enabled(effect_enabled);
e->load(stream);
c->effects.append(e);
}
}
}
bool LoadThread::load_worker(QFile& f, QXmlStreamReader& stream, int type) {
f.seek(0);
stream.setDevice(stream.device());
QString root_search;
QString child_search;
switch (type) {
case LOAD_TYPE_VERSION:
root_search = "version";
break;
case LOAD_TYPE_URL:
root_search = "url";
break;
case MEDIA_TYPE_FOLDER:
root_search = "folders";
child_search = "folder";
break;
case MEDIA_TYPE_FOOTAGE:
root_search = "media";
child_search = "footage";
break;
case MEDIA_TYPE_SEQUENCE:
root_search = "sequences";
child_search = "sequence";
break;
}
show_err = true;
while (!stream.atEnd()) {
stream.readNextStartElement();
if (stream.name() == root_search) {
if (type == LOAD_TYPE_VERSION) {
int proj_version = stream.readElementText().toInt();
if (proj_version < MIN_SAVE_VERSION && proj_version > SAVE_VERSION) {
if (QMessageBox::warning(mainWindow, "Version Mismatch", "This project was saved in a different version of Olive and may not be fully compatible with this version. Would you like to attempt loading it anyway?", QMessageBox::Yes, QMessageBox::No) == QMessageBox::No) {
show_err = false;
return false;
}
}
} else if (type == LOAD_TYPE_URL) {
internal_proj_url = stream.readElementText();
internal_proj_dir = QFileInfo(internal_proj_url).absoluteDir();
} else {
while (!(stream.name() == root_search && stream.isEndElement())) {
stream.readNext();
if (stream.name() == child_search && stream.isStartElement()) {
switch (type) {
case MEDIA_TYPE_FOLDER:
{
Media* folder = panel_project->new_folder(0);
folder->temp_id2 = 0;
for (int j=0;j<stream.attributes().size();j++) {
const QXmlStreamAttribute& attr = stream.attributes().at(j);
if (attr.name() == "id") {
folder->temp_id = attr.value().toInt();
} else if (attr.name() == "name") {
folder->set_name(attr.value().toString());
} else if (attr.name() == "parent") {
folder->temp_id2 = attr.value().toInt();
}
}
loaded_folders.append(folder);
}
break;
case MEDIA_TYPE_FOOTAGE:
{
int folder = 0;
Media* item = new Media(0);
Footage* m = new Footage();
m->using_inout = false;
for (int j=0;j<stream.attributes().size();j++) {
const QXmlStreamAttribute& attr = stream.attributes().at(j);
if (attr.name() == "id") {
m->save_id = attr.value().toInt();
} else if (attr.name() == "folder") {
folder = attr.value().toInt();
} else if (attr.name() == "name") {
m->name = attr.value().toString();
} else if (attr.name() == "url") {
m->url = attr.value().toString();
if (!QFileInfo::exists(m->url)) { // if path is not absolute
QString proj_dir_test = proj_dir.absoluteFilePath(m->url);
QString internal_proj_dir_test = internal_proj_dir.absoluteFilePath(m->url);
if (QFileInfo::exists(proj_dir_test)) { // if path is relative to the project's current dir
m->url = proj_dir_test;
dout << "[INFO] Matched" << attr.value().toString() << "relative to project's current directory";
} else if (QFileInfo::exists(internal_proj_dir_test)) { // if path is relative to the last directory the project was saved in
m->url = internal_proj_dir_test;
dout << "[INFO] Matched" << attr.value().toString() << "relative to project's internal directory";
} else if (m->url.contains('%')) {
// hack for image sequences (qt won't be able to find the URL with %, but ffmpeg may)
m->url = proj_dir_test;
dout << "[INFO] Guess image sequence" << attr.value().toString() << "path to project's current directory";
} else {
dout << "[INFO] Failed to match" << attr.value().toString() << "to file";
}
} else {
dout << "[INFO] Matched" << attr.value().toString() << "with absolute path";
}
} else if (attr.name() == "duration") {
m->length = attr.value().toLongLong();
} else if (attr.name() == "using_inout") {
m->using_inout = (attr.value() == "1");
} else if (attr.name() == "in") {
m->in = attr.value().toLong();
} else if (attr.name() == "out") {
m->out = attr.value().toLong();
}
}
item->set_footage(m);
if (folder == 0) {
project_model.addTopLevelItem(item);
} else {
find_loaded_folder_by_id(folder)->appendChild(item);
}
// analyze media to see if it's the same
loaded_media_items.append(item);
}
break;
case MEDIA_TYPE_SEQUENCE:
{
Media* parent = NULL;
Sequence* s = new Sequence();
// load attributes about sequence
for (int j=0;j<stream.attributes().size();j++) {
const QXmlStreamAttribute& attr = stream.attributes().at(j);
if (attr.name() == "name") {
s->name = attr.value().toString();
} else if (attr.name() == "folder") {
int folder = attr.value().toInt();
if (folder > 0) parent = find_loaded_folder_by_id(folder);
} else if (attr.name() == "id") {
s->save_id = attr.value().toInt();
} else if (attr.name() == "width") {
s->width = attr.value().toInt();
} else if (attr.name() == "height") {
s->height = attr.value().toInt();
} else if (attr.name() == "framerate") {
s->frame_rate = attr.value().toDouble();
} else if (attr.name() == "afreq") {
s->audio_frequency = attr.value().toInt();
} else if (attr.name() == "alayout") {
s->audio_layout = attr.value().toInt();
} else if (attr.name() == "open") {
open_seq = s;
} else if (attr.name() == "workarea") {
s->using_workarea = (attr.value() == "1");
} else if (attr.name() == "workareaIn") {
s->workarea_in = attr.value().toLong();
} else if (attr.name() == "workareaOut") {
s->workarea_out = attr.value().toLong();
}
}
QVector<TransitionData> transition_data;
// load all clips and clip information
while (!(stream.name() == child_search && stream.isEndElement()) && !stream.atEnd()) {
stream.readNextStartElement();
if (stream.name() == "marker" && stream.isStartElement()) {
Marker m;
for (int j=0;j<stream.attributes().size();j++) {
const QXmlStreamAttribute& attr = stream.attributes().at(j);
if (attr.name() == "frame") {
m.frame = attr.value().toLong();
} else if (attr.name() == "name") {
m.name = attr.value().toString();
}
}
s->markers.append(m);
} else if (stream.name() == "transition" && stream.isStartElement()) {
TransitionData td;
td.otc = NULL;
td.ctc = NULL;
for (int j=0;j<stream.attributes().size();j++) {
const QXmlStreamAttribute& attr = stream.attributes().at(j);
if (attr.name() == "id") {
td.id = attr.value().toInt();
} else if (attr.name() == "name") {
td.name = attr.value().toString();
} else if (attr.name() == "length") {
td.length = attr.value().toLong();
}
}
transition_data.append(td);
} else if (stream.name() == "clip" && stream.isStartElement()) {
int media_type = -1;
int media_id, stream_id;
Clip* c = new Clip(s);
// backwards compatibility code
c->autoscale = false;
c->media = NULL;
for (int j=0;j<stream.attributes().size();j++) {
const QXmlStreamAttribute& attr = stream.attributes().at(j);
if (attr.name() == "name") {
c->name = attr.value().toString();
} else if (attr.name() == "enabled") {
c->enabled = (attr.value() == "1");
} else if (attr.name() == "id") {
c->load_id = attr.value().toInt();
} else if (attr.name() == "clipin") {
c->clip_in = attr.value().toLong();
} else if (attr.name() == "in") {
c->timeline_in = attr.value().toLong();
} else if (attr.name() == "out") {
c->timeline_out = attr.value().toLong();
} else if (attr.name() == "track") {
c->track = attr.value().toInt();
} else if (attr.name() == "r") {
c->color_r = attr.value().toInt();
} else if (attr.name() == "g") {
c->color_g = attr.value().toInt();
} else if (attr.name() == "b") {
c->color_b = attr.value().toInt();
} else if (attr.name() == "autoscale") {
c->autoscale = (attr.value() == "1");
} else if (attr.name() == "media") {
media_type = MEDIA_TYPE_FOOTAGE;
media_id = attr.value().toInt();
} else if (attr.name() == "stream") {
stream_id = attr.value().toInt();
} else if (attr.name() == "speed") {
c->speed = attr.value().toDouble();
} else if (attr.name() == "maintainpitch") {
c->maintain_audio_pitch = (attr.value() == "1");
} else if (attr.name() == "reverse") {
c->reverse = (attr.value() == "1");
} else if (attr.name() == "opening") {
c->opening_transition = attr.value().toInt();
} else if (attr.name() == "closing") {
c->closing_transition = attr.value().toInt();
} else if (attr.name() == "sequence") {
media_type = MEDIA_TYPE_SEQUENCE;
// since we haven't finished loading sequences, we defer linking this until later
c->media = NULL;
c->media_stream = attr.value().toInt();
loaded_clips.append(c);
}
}
// set media and media stream
switch (media_type) {
case MEDIA_TYPE_FOOTAGE:
if (media_id >= 0) {
for (int j=0;j<loaded_media_items.size();j++) {
Footage* m = loaded_media_items.at(j)->to_footage();
if (m->save_id == media_id) {
c->media = loaded_media_items.at(j);
c->media_stream = stream_id;
break;
}
}
}
break;
}
// load links and effects
while (!(stream.name() == "clip" && stream.isEndElement()) && !stream.atEnd()) {
stream.readNext();
if (stream.isStartElement()) {
if (stream.name() == "linked") {
while (!(stream.name() == "linked" && stream.isEndElement()) && !stream.atEnd()) {
stream.readNext();
if (stream.name() == "link" && stream.isStartElement()) {
for (int k=0;k<stream.attributes().size();k++) {
const QXmlStreamAttribute& link_attr = stream.attributes().at(k);
if (link_attr.name() == "id") {
c->linked.append(link_attr.value().toInt());
break;
}
}
}
}
} else if (stream.isStartElement() && (stream.name() == "effect" || stream.name() == "opening" || stream.name() == "closing")) {
// "opening" and "closing" are backwards compatibility code
load_effect(stream, c);
}
}
}
s->clips.append(c);
}
}
// correct links, clip IDs, transitions
for (int i=0;i<s->clips.size();i++) {
// correct links
Clip* correct_clip = s->clips.at(i);
for (int j=0;j<correct_clip->linked.size();j++) {
bool found = false;
for (int k=0;k<s->clips.size();k++) {
if (s->clips.at(k)->load_id == correct_clip->linked.at(j)) {
correct_clip->linked[j] = k;
found = true;
break;
}
}
if (!found) {
correct_clip->linked.removeAt(j);
j--;
if (QMessageBox::warning(mainWindow, "Invalid Clip Link", "This project contains an invalid clip link. It may be corrupt. Would you like to continue loading it?", QMessageBox::Yes, QMessageBox::No) == QMessageBox::No) {
delete s;
return false;
}
}
}
// re-link clips to transitions
if (correct_clip->opening_transition > -1) {
for (int j=0;j<transition_data.size();j++) {
if (transition_data.at(j).id == correct_clip->opening_transition) {
transition_data[j].otc = correct_clip;
}
}
}
if (correct_clip->closing_transition > -1) {
for (int j=0;j<transition_data.size();j++) {
if (transition_data.at(j).id == correct_clip->closing_transition) {
transition_data[j].ctc = correct_clip;
}
}
}
}
// create transitions
for (int i=0;i<transition_data.size();i++) {
const TransitionData& td = transition_data.at(i);
Clip* primary = td.otc;
Clip* secondary = td.ctc;
if (primary != NULL || secondary != NULL) {
if (primary == NULL) {
primary = secondary;
secondary = NULL;
}
const EffectMeta* meta = get_meta_from_name(td.name, (primary->track < 0) ? EFFECT_TYPE_VIDEO : EFFECT_TYPE_AUDIO);
if (meta == NULL) {
dout << "[WARNING] Failed to link transition with name:" << td.name;
if (td.otc != NULL) td.otc->opening_transition = -1;
if (td.ctc != NULL) td.ctc->closing_transition = -1;
} else {
int transition_index = create_transition(primary, secondary, meta);
primary->sequence->transitions.at(transition_index)->set_length(td.length);
if (td.otc != NULL) td.otc->opening_transition = transition_index;
if (td.ctc != NULL) td.ctc->closing_transition = transition_index;
}
}
}
Media* m = panel_project->new_sequence(NULL, s, false, parent);
loaded_sequences.append(m);
}
break;
}
}
}
}
break;
}
}
return true;
}
Media* LoadThread::find_loaded_folder_by_id(int id) {
for (int j=0;j<loaded_folders.size();j++) {
Media* parent_item = loaded_folders.at(j);
if (parent_item->temp_id == id) {
return parent_item;
}
}
return NULL;
}
void LoadThread::run() {
QFile file(project_url);
if (!file.open(QIODevice::ReadOnly)) {
dout << "[ERROR] Could not open file";
return;
}
/* set up directories to search for media
* most of the time, these will be the same but in
* case the project file has moved without the footage,
* we check both
*/
proj_dir = QFileInfo(project_url).absoluteDir();
internal_proj_dir = QFileInfo(project_url).absoluteDir();
internal_proj_url = project_url;
QXmlStreamReader stream(&file);
bool cont = false;
error_str.clear();
show_err = true;
// temp variables for loading (unnecessary?)
open_seq = NULL;
loaded_folders.clear();
loaded_media_items.clear();
loaded_clips.clear();
loaded_sequences.clear();
// get "element" count
int element_count = 0;
while (!stream.atEnd()) {
stream.readNextStartElement();
if (stream.name() == "folder"
|| stream.name() == "footage"
|| stream.name() == "sequence"
|| stream.name() == "clip"
|| stream.name() == "effect") {
element_count++;
}
}
// find project file version
cont = load_worker(file, stream, LOAD_TYPE_VERSION);
// find project's internal URL
cont = load_worker(file, stream, LOAD_TYPE_URL);
if (autorecovery) {
QString orig_filename = internal_proj_url;
int insert_index = internal_proj_url.lastIndexOf(".ove", -1, Qt::CaseInsensitive);
if (insert_index == -1) insert_index = internal_proj_url.length();
int counter = 1;
while (QFileInfo::exists(orig_filename)) {
orig_filename = internal_proj_url;
QString recover_text = "recovered";
if (counter > 1) {
recover_text += " " + QString::number(counter);
}
orig_filename.insert(insert_index, " (" + recover_text + ")");
counter++;
}
mainWindow->updateTitle(orig_filename);
}
// load folders first
if (cont) {
cont = load_worker(file, stream, MEDIA_TYPE_FOLDER);
}
// load media
if (cont) {
// since folders loaded correctly, organize them appropriately
for (int i=0;i<loaded_folders.size();i++) {
Media* folder = loaded_folders.at(i);
int parent = folder->temp_id2;
if (parent > 0) {
find_loaded_folder_by_id(parent)->appendChild(folder);
} else {
project_model.addTopLevelItem(folder);
}
}
cont = load_worker(file, stream, MEDIA_TYPE_FOOTAGE);
}
// load sequences
if (cont) {
cont = load_worker(file, stream, MEDIA_TYPE_SEQUENCE);
}
// attach nested sequence clips to their sequences
for (int i=0;i<loaded_clips.size();i++) {
for (int j=0;j<loaded_sequences.size();j++) {
if (loaded_clips.at(i)->media == NULL && loaded_clips.at(i)->media_stream == loaded_sequences.at(j)->to_sequence()->save_id) {
loaded_clips.at(i)->media = loaded_sequences.at(j);
loaded_clips.at(i)->refresh();
break;
}
}
}
if (!cont) {
if (show_err) QMessageBox::critical(mainWindow, "Project Load Error", "Error loading project: " + error_str, QMessageBox::Ok);
} else if (stream.hasError()) {
dout << "[ERROR] Error parsing XML." << stream.errorString();
QMessageBox::critical(mainWindow, "XML Parsing Error", "Couldn't load '" + project_url + "'. " + stream.errorString(), QMessageBox::Ok);
cont = false;
}
if (cont) {
mainWindow->setWindowModified(autorecovery);
emit success(); // run in main thread
for (int i=0;i<loaded_media_items.size();i++) {
panel_project->start_preview_generator(loaded_media_items.at(i), true);
}
} else {
panel_project->new_project();
}
panel_project->add_recent_project(project_url);
file.close();
}
void LoadThread::success_func() {
if (open_seq != NULL) set_sequence(open_seq);
update_ui(false);
}
+43
View File
@@ -0,0 +1,43 @@
#ifndef LOADTHREAD_H
#define LOADTHREAD_H
#include <QThread>
#include <QDir>
#include <QXmlStreamReader>
class Media;
class Footage;
class Clip;
class Sequence;
class LoadDialog;
class LoadThread : public QThread
{
Q_OBJECT
public:
LoadThread(LoadDialog* l, bool a);
void run();
signals:
void success();
private slots:
void success_func();
private:
LoadDialog* ld;
bool autorecovery;
bool load_worker(QFile& f, QXmlStreamReader& stream, int type);
Sequence* open_seq;
QVector<Media*> loaded_media_items;
QDir proj_dir;
QDir internal_proj_dir;
QString internal_proj_url;
bool show_err;
QString error_str;
QVector<Media*> loaded_folders;
QVector<Clip*> loaded_clips;
QVector<Media*> loaded_sequences;
Media* find_loaded_folder_by_id(int id);
};
#endif // LOADTHREAD_H
-56
View File
@@ -1,56 +0,0 @@
#include "media.h"
#include <QDebug>
#include <QtMath>
#include "io/previewgenerator.h"
extern "C" {
#include <libavformat/avformat.h>
}
#include "project/clip.h"
Media::Media() : ready(false), preview_gen(NULL), invalid(false), in(0), out(0) {
ready_lock.lock();
}
Media::~Media() {
reset();
}
void Media::reset() {
if (preview_gen != NULL) {
preview_gen->cancel();
preview_gen->wait();
}
for (int i=0;i<video_tracks.size();i++) {
delete video_tracks.at(i);
}
for (int i=0;i<audio_tracks.size();i++) {
delete audio_tracks.at(i);
}
video_tracks.clear();
audio_tracks.clear();
ready = false;
}
long Media::get_length_in_frames(double frame_rate) {
return qFloor(((double) length / (double) AV_TIME_BASE) * frame_rate);
}
MediaStream* Media::get_stream_from_file_index(bool video, int index) {
if (video) {
for (int i=0;i<video_tracks.size();i++) {
if (video_tracks.at(i)->file_index == index) {
return video_tracks.at(i);
}
}
} else {
for (int i=0;i<audio_tracks.size();i++) {
if (audio_tracks.at(i)->file_index == index) {
return audio_tracks.at(i);
}
}
}
return NULL;
}
-69
View File
@@ -1,69 +0,0 @@
#ifndef MEDIA_H
#define MEDIA_H
#include <QString>
#include <QVector>
#include <QMetaType>
#include <QVariant>
#include <QMutex>
#include <QPixmap>
#define MEDIA_TYPE_FOOTAGE 0
#define MEDIA_TYPE_SEQUENCE 1
#define MEDIA_TYPE_FOLDER 2
#define MEDIA_TYPE_SOLID 3
#define MEDIA_TYPE_TONE 4
#define VIDEO_PROGRESSIVE 0
#define VIDEO_TOP_FIELD_FIRST 1
#define VIDEO_BOTTOM_FIELD_FIRST 2
struct Sequence;
struct Clip;
class PreviewGenerator;
class MediaThrobber;
struct MediaStream {
int file_index;
int video_width;
int video_height;
bool infinite_length;
double video_frame_rate;
int video_interlacing;
int video_auto_interlacing;
int audio_channels;
int audio_layout;
int audio_frequency;
// preview thumbnail/waveform
bool preview_done;
QImage video_preview;
QVector<char> audio_preview;
};
struct Media {
Media();
~Media();
QString url;
QString name;
int64_t length;
QVector<MediaStream*> video_tracks;
QVector<MediaStream*> audio_tracks;
int save_id;
bool ready;
bool invalid;
PreviewGenerator* preview_gen;
QMutex ready_lock;
bool using_inout;
long in;
long out;
long get_length_in_frames(double frame_rate);
MediaStream* get_stream_from_file_index(bool video, int index);
void reset();
};
#endif // MEDIA_H
+62 -54
View File
@@ -1,6 +1,7 @@
#include "previewgenerator.h"
#include "media.h"
#include "project/media.h"
#include "project/footage.h"
#include "panels/viewer.h"
#include "panels/project.h"
#include "io/config.h"
@@ -28,11 +29,11 @@ extern "C" {
QSemaphore sem(5); // only 5 preview generators can run at one time
PreviewGenerator::PreviewGenerator(QTreeWidgetItem* i, Media* m, bool r) :
PreviewGenerator::PreviewGenerator(Media* i, Footage* m, bool r) :
QThread(0),
fmt_ctx(NULL),
item(i),
media(m),
media(i),
footage(m),
retrieve_duration(false),
contains_still_image(false),
replace(r),
@@ -52,12 +53,12 @@ void PreviewGenerator::parse_media() {
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) == NULL) {
dout << "[ERROR] Unsupported codec in stream" << i << "of file" << media->name;
dout << "[ERROR] Unsupported codec in stream" << i << "of file" << footage->name;
} else {
MediaStream* ms = media->get_stream_from_file_index(fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO, i);
FootageStream* ms = footage->get_stream_from_file_index(fmt_ctx->streams[i]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO, i);
bool append = false;
if (ms == NULL) {
ms = new MediaStream();
ms = new FootageStream();
ms->preview_done = false;
ms->file_index = i;
append = true;
@@ -93,18 +94,18 @@ void PreviewGenerator::parse_media() {
ms->video_auto_interlacing = VIDEO_PROGRESSIVE;
ms->video_interlacing = VIDEO_PROGRESSIVE;
if (append) media->video_tracks.append(ms);
if (append) footage->video_tracks.append(ms);
} 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_frequency = fmt_ctx->streams[i]->codecpar->sample_rate;
if (append) media->audio_tracks.append(ms);
if (append) footage->audio_tracks.append(ms);
} else if (append) {
delete ms;
}
}
}
media->length = fmt_ctx->duration;
footage->length = fmt_ctx->duration;
if (fmt_ctx->duration == INT64_MIN) {
retrieve_duration = true;
@@ -116,28 +117,29 @@ void PreviewGenerator::parse_media() {
bool PreviewGenerator::retrieve_preview(const QString& hash) {
// returns true if generate_waveform must be run, false if we got all previews from cached files
if (retrieve_duration) {
//dout << "[NOTE] " << media->name << "needs to retrieve duration";
return true;
}
bool found = true;
for (int i=0;i<media->video_tracks.size();i++) {
MediaStream* ms = media->video_tracks.at(i);
for (int i=0;i<footage->video_tracks.size();i++) {
FootageStream* ms = footage->video_tracks.at(i);
QString thumb_path = get_thumbnail_path(hash, ms);
QFile f(thumb_path);
if (f.exists() && ms->video_preview.load(thumb_path)) {
//dout << "loaded thumb" << ms->file_index << "from" << thumb_path;
dout << "loaded thumb" << ms->file_index << "from" << thumb_path;
ms->preview_done = true;
} else {
found = false;
break;
}
}
for (int i=0;i<media->audio_tracks.size();i++) {
MediaStream* ms = media->audio_tracks.at(i);
for (int i=0;i<footage->audio_tracks.size();i++) {
FootageStream* ms = footage->audio_tracks.at(i);
QString waveform_path = get_waveform_path(hash, ms);
QFile f(waveform_path);
if (f.exists()) {
//dout << "loaded wave" << ms->file_index << "from" << waveform_path;
dout << "loaded wave" << ms->file_index << "from" << waveform_path;
f.open(QFile::ReadOnly);
QByteArray data = f.readAll();
ms->audio_preview.resize(data.size());
@@ -153,12 +155,12 @@ bool PreviewGenerator::retrieve_preview(const QString& hash) {
}
}
if (!found) {
for (int i=0;i<media->video_tracks.size();i++) {
MediaStream* ms = media->video_tracks.at(i);
for (int i=0;i<footage->video_tracks.size();i++) {
FootageStream* ms = footage->video_tracks.at(i);
ms->preview_done = false;
}
for (int i=0;i<media->audio_tracks.size();i++) {
MediaStream* ms = media->audio_tracks.at(i);
for (int i=0;i<footage->audio_tracks.size();i++) {
FootageStream* ms = footage->audio_tracks.at(i);
ms->audio_preview.clear();
ms->preview_done = false;
}
@@ -167,11 +169,11 @@ bool PreviewGenerator::retrieve_preview(const QString& hash) {
}
void PreviewGenerator::finalize_media() {
media->ready_lock.unlock();
media->ready = true;
footage->ready_lock.unlock();
footage->ready = true;
if (!cancelled) {
if (media->video_tracks.size() == 0) {
if (footage->video_tracks.size() == 0) {
emit set_icon(ICON_TYPE_AUDIO, replace);
} else if (contains_still_image) {
emit set_icon(ICON_TYPE_IMAGE, replace);
@@ -179,7 +181,7 @@ void PreviewGenerator::finalize_media() {
emit set_icon(ICON_TYPE_VIDEO, replace);
}
if (!contains_still_image || media->audio_tracks.size() > 0) {
/*if (!contains_still_image || media->audio_tracks.size() > 0) {
double frame_rate = 30;
if (!contains_still_image && media->video_tracks.size() > 0) frame_rate = media->video_tracks.at(0)->video_frame_rate;
item->setText(1, frame_to_timecode(media->get_length_in_frames(frame_rate), config.timecode_view, frame_rate));
@@ -189,7 +191,7 @@ void PreviewGenerator::finalize_media() {
} else {
item->setText(2, QString::number(media->audio_tracks.at(0)->audio_frequency) + " Hz");
}
}
}*/
}
}
@@ -229,6 +231,9 @@ void PreviewGenerator::generate_waveform() {
while (codec_ctx[packet->stream_index] == NULL || avcodec_receive_frame(codec_ctx[packet->stream_index], temp_frame) == AVERROR(EAGAIN)) {
av_packet_unref(packet);
int read_ret = av_read_frame(fmt_ctx, packet);
//dout << "read frame for" << footage->name << footage->url << read_ret << "retrieve_duration:" << retrieve_duration << "eof:" << end_of_file << "packet pts:" << packet->pts;
if (read_ret < 0) {
end_of_file = true;
if (read_ret != AVERROR_EOF) dout << "[ERROR] Failed to read packet for preview generation" << read_ret;
@@ -244,7 +249,7 @@ void PreviewGenerator::generate_waveform() {
}
}
if (!end_of_file) {
MediaStream* s = media->get_stream_from_file_index(fmt_ctx->streams[packet->stream_index]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO, packet->stream_index);
FootageStream* s = footage->get_stream_from_file_index(fmt_ctx->streams[packet->stream_index]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO, packet->stream_index);
if (s != NULL) {
if (fmt_ctx->streams[packet->stream_index]->codecpar->codec_type == AVMEDIA_TYPE_VIDEO) {
if (!s->preview_done) {
@@ -349,10 +354,10 @@ void PreviewGenerator::generate_waveform() {
// check if we've got all our previews
if (retrieve_duration) {
done = false;
} else if (media->audio_tracks.size() == 0) {
} else if (footage->audio_tracks.size() == 0) {
done = true;
for (int i=0;i<media->video_tracks.size();i++) {
if (!media->video_tracks.at(i)->preview_done) {
for (int i=0;i<footage->video_tracks.size();i++) {
if (!footage->video_tracks.at(i)->preview_done) {
done = false;
break;
}
@@ -365,8 +370,8 @@ void PreviewGenerator::generate_waveform() {
av_packet_unref(packet);
}
}
for (int i=0;i<media->audio_tracks.size();i++) {
media->audio_tracks.at(i)->preview_done = true;
for (int i=0;i<footage->audio_tracks.size();i++) {
footage->audio_tracks.at(i)->preview_done = true;
}
av_frame_free(&temp_frame);
av_packet_free(&packet);
@@ -376,33 +381,33 @@ void PreviewGenerator::generate_waveform() {
}
}
if (retrieve_duration) {
media->length = 0;
footage->length = 0;
int maximum_stream = 0;
for (unsigned int i=0;i<fmt_ctx->nb_streams;i++) {
if (media_lengths[i] > media_lengths[maximum_stream]) {
maximum_stream = i;
}
}
media->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;
delete [] codec_ctx;
}
QString PreviewGenerator::get_thumbnail_path(const QString& hash, MediaStream* ms) {
QString PreviewGenerator::get_thumbnail_path(const QString& hash, FootageStream* ms) {
return data_path + "/" + hash + "t" + QString::number(ms->file_index);
}
QString PreviewGenerator::get_waveform_path(const QString& hash, MediaStream* ms) {
QString PreviewGenerator::get_waveform_path(const QString& hash, FootageStream* ms) {
return data_path + "/" + hash + "w" + QString::number(ms->file_index);
}
void PreviewGenerator::run() {
Q_ASSERT(footage != NULL);
Q_ASSERT(media != NULL);
Q_ASSERT(item != NULL);
QByteArray ba = media->url.toLatin1();
QByteArray ba = footage->url.toLatin1();
char* filename = new char[ba.size()+1];
strcpy(filename, ba.data());
@@ -423,48 +428,51 @@ void PreviewGenerator::run() {
error = true;
} else {
av_dump_format(fmt_ctx, 0, filename, 0);
parse_media();
sem.acquire();
parse_media();
// see if we already have data for this
QFileInfo file_info(media->url);
QString cache_file = media->url + QString::number(file_info.lastModified().toMSecsSinceEpoch());
QFileInfo file_info(footage->url);
QString cache_file = footage->url.mid(footage->url.lastIndexOf('/')+1) + QString::number(file_info.size()) + QString::number(file_info.lastModified().toMSecsSinceEpoch());
dout << "using hash" << cache_file;
QString hash = QCryptographicHash::hash(cache_file.toLatin1(), QCryptographicHash::Md5).toHex();
if (retrieve_preview(hash)) {
sem.acquire();
generate_waveform();
// save preview to file
for (int i=0;i<media->video_tracks.size();i++) {
MediaStream* ms = media->video_tracks.at(i);
for (int i=0;i<footage->video_tracks.size();i++) {
FootageStream* ms = footage->video_tracks.at(i);
ms->video_preview.save(get_thumbnail_path(hash, ms), "PNG");
dout << "saved" << ms->file_index << "thumbnail to" << get_thumbnail_path(hash, ms);
}
for (int i=0;i<media->audio_tracks.size();i++) {
MediaStream* ms = media->audio_tracks.at(i);
for (int i=0;i<footage->audio_tracks.size();i++) {
FootageStream* ms = footage->audio_tracks.at(i);
QFile f(get_waveform_path(hash, ms));
f.open(QFile::WriteOnly);
f.write(ms->audio_preview.constData(), ms->audio_preview.size());
f.close();
//dout << "saved" << ms->file_index << "waveform to" << get_waveform_path(hash, ms);
dout << "saved" << ms->file_index << "waveform to" << get_waveform_path(hash, ms);
}
}
sem.release();
sem.release();
}
}
avformat_close_input(&fmt_ctx);
}
if (error) {
update_footage_tooltip(item, media, errorStr);
if (error) {
media->update_tooltip(errorStr);
emit set_icon(ICON_TYPE_ERROR, replace);
media->invalid = true;
media->ready_lock.unlock();
footage->invalid = true;
footage->ready_lock.unlock();
} else {
update_footage_tooltip(item, media);
media->update_tooltip();
}
delete [] filename;
media->preview_gen = NULL;
footage->preview_gen = NULL;
}
void PreviewGenerator::cancel() {
+8 -7
View File
@@ -2,22 +2,23 @@
#define PREVIEWGENERATOR_H
#include <QThread>
#include <QSemaphore>
#define ICON_TYPE_VIDEO 0
#define ICON_TYPE_AUDIO 1
#define ICON_TYPE_IMAGE 2
#define ICON_TYPE_ERROR 3
struct Media;
struct MediaStream;
struct Footage;
struct FootageStream;
struct AVFormatContext;
class QTreeWidgetItem;
class Media;
class PreviewGenerator : public QThread
{
Q_OBJECT
public:
PreviewGenerator(QTreeWidgetItem*, Media*, bool);
PreviewGenerator(Media*, Footage*, bool);
void run();
void cancel();
signals:
@@ -28,15 +29,15 @@ private:
void generate_waveform();
void finalize_media();
AVFormatContext* fmt_ctx;
QTreeWidgetItem* item;
Media* media;
Footage* footage;
bool retrieve_duration;
bool contains_still_image;
bool replace;
bool cancelled;
QString data_path;
QString get_thumbnail_path(const QString &hash, MediaStream* ms);
QString get_waveform_path(const QString& hash, MediaStream* ms);
QString get_thumbnail_path(const QString &hash, FootageStream* ms);
QString get_waveform_path(const QString& hash, FootageStream* ms);
};
#endif // PREVIEWGENERATOR_H