forked from mia/Aegisub
vastly improved keyframe reading with ffmpeg, should be much faster and more reliable.
Originally committed to SVN as r2248.
This commit is contained in:
parent
ab0d5377e8
commit
32f7a53a68
3 changed files with 38 additions and 51 deletions
|
@ -38,49 +38,36 @@
|
||||||
// Headers
|
// Headers
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#ifdef WITH_FFMPEG
|
#ifdef WITH_FFMPEG
|
||||||
|
|
||||||
|
#ifdef WIN32
|
||||||
|
#define __STDC_CONSTANT_MACROS 1
|
||||||
|
#include <stdint.h>
|
||||||
|
#endif /* WIN32 */
|
||||||
|
|
||||||
#include "dialog_progress.h"
|
#include "dialog_progress.h"
|
||||||
#include "lavc_keyframes.h"
|
#include "lavc_keyframes.h"
|
||||||
|
|
||||||
///////////////
|
///////////////
|
||||||
// Constructor
|
// Constructor
|
||||||
LAVCKeyFrames::LAVCKeyFrames(const Aegisub::String filename)
|
LAVCKeyFrames::LAVCKeyFrames(const Aegisub::String filename)
|
||||||
: file(0), codecContext(0), codec(0), stream(0), frame(0),
|
: file(0), stream(0), streamN(-1) {
|
||||||
streamN(-1) {
|
|
||||||
// Open LAVCFile
|
// Open LAVCFile
|
||||||
file = LAVCFile::Create(filename);
|
file = LAVCFile::Create(filename);
|
||||||
|
|
||||||
// Find video stream
|
// Find video stream
|
||||||
for (unsigned int i = 0; i < file->fctx->nb_streams; ++i) {
|
for (unsigned int i = 0; i < file->fctx->nb_streams; ++i) {
|
||||||
codecContext = file->fctx->streams[i]->codec;
|
if (file->fctx->streams[i]->codec->codec_type == CODEC_TYPE_VIDEO) {
|
||||||
if (!codecContext) continue;
|
|
||||||
codecContext->skip_frame = AVDISCARD_NONKEY;
|
|
||||||
codecContext->workaround_bugs = FF_BUG_AUTODETECT;
|
|
||||||
if (codecContext->codec_type == CODEC_TYPE_VIDEO) {
|
|
||||||
stream = file->fctx->streams[i];
|
stream = file->fctx->streams[i];
|
||||||
stream->discard = AVDISCARD_NONKEY;
|
|
||||||
streamN = i;
|
streamN = i;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (streamN == -1) throw _T("Could not find a video stream");
|
if (streamN == -1) throw _T("Could not find a video stream");
|
||||||
|
|
||||||
// Find codec
|
|
||||||
codec = avcodec_find_decoder(codecContext->codec_id);
|
|
||||||
if (!codec) throw _T("Could not find suitable video decoder");
|
|
||||||
|
|
||||||
// Open codec
|
|
||||||
int result = avcodec_open(codecContext, codec);
|
|
||||||
if (result < 0) throw _T("Failed to open video decoder");
|
|
||||||
|
|
||||||
// Allocate frame
|
|
||||||
frame = avcodec_alloc_frame();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//////////////
|
//////////////
|
||||||
// Destructor
|
// Destructor
|
||||||
LAVCKeyFrames::~LAVCKeyFrames() {
|
LAVCKeyFrames::~LAVCKeyFrames() {
|
||||||
if (frame) av_free((void*) frame);
|
|
||||||
if (codec && codecContext) avcodec_close(codecContext);
|
|
||||||
if (file) file->Release();
|
if (file) file->Release();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -90,7 +77,10 @@ wxArrayInt LAVCKeyFrames::GetKeyFrames() {
|
||||||
wxArrayInt keyframes;
|
wxArrayInt keyframes;
|
||||||
|
|
||||||
AVPacket packet;
|
AVPacket packet;
|
||||||
int total_frames = stream->duration;
|
// sanity check stream duration
|
||||||
|
if (stream->duration == AV_NOPTS_VALUE)
|
||||||
|
throw _T("ffmpeg keyframes reader: demuxer returned invalid stream length");
|
||||||
|
int total_frames = stream->duration; // FIXME: this will most likely NOT WORK for VFR files!
|
||||||
register unsigned int frameN = 0; // Number of parsed frames
|
register unsigned int frameN = 0; // Number of parsed frames
|
||||||
|
|
||||||
volatile bool canceled = false;
|
volatile bool canceled = false;
|
||||||
|
@ -110,21 +100,12 @@ wxArrayInt LAVCKeyFrames::GetKeyFrames() {
|
||||||
if ((frameN & (1024 - 1)) == 0)
|
if ((frameN & (1024 - 1)) == 0)
|
||||||
progress->SetProgress(frameN,total_frames);
|
progress->SetProgress(frameN,total_frames);
|
||||||
|
|
||||||
// Decode frame
|
// Aegisub starts counting at frame 0, so the result must be
|
||||||
int frameFinished;
|
// parsed frames - 1
|
||||||
avcodec_decode_video(codecContext, frame, &frameFinished, packet.data, packet.size);
|
if (packet.flags == PKT_FLAG_KEY)
|
||||||
|
keyframes.Add(frameN - 1);
|
||||||
// Success?
|
|
||||||
if(frameFinished) {
|
|
||||||
if (frame->key_frame)
|
|
||||||
// Aegisub starts counting at frame 0, so the result must be
|
|
||||||
// parsed frames - 1
|
|
||||||
keyframes.Add(frameN - 1);
|
|
||||||
|
|
||||||
// Free packet
|
|
||||||
av_free_packet(&packet);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
av_free_packet(&packet);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clean up progress
|
// Clean up progress
|
||||||
|
|
|
@ -41,10 +41,7 @@
|
||||||
class LAVCKeyFrames {
|
class LAVCKeyFrames {
|
||||||
private:
|
private:
|
||||||
LAVCFile* file; // Video file
|
LAVCFile* file; // Video file
|
||||||
AVCodecContext* codecContext; // Codec context
|
|
||||||
AVCodec* codec; // Used codec
|
|
||||||
AVStream* stream; // Used stream
|
AVStream* stream; // Used stream
|
||||||
AVFrame* frame; // Frame buffer
|
|
||||||
|
|
||||||
int streamN; // Stream index
|
int streamN; // Stream index
|
||||||
public:
|
public:
|
||||||
|
|
|
@ -63,11 +63,14 @@
|
||||||
#include "subs_grid.h"
|
#include "subs_grid.h"
|
||||||
#include "vfw_wrap.h"
|
#include "vfw_wrap.h"
|
||||||
|
|
||||||
#if !defined(__WINDOWS__)
|
|
||||||
#ifdef WITH_FFMPEG
|
#ifdef WITH_FFMPEG
|
||||||
|
#ifdef WIN32
|
||||||
|
#define __STDC_CONSTANT_MACROS 1
|
||||||
|
#include <stdint.h>
|
||||||
|
#endif /* WIN32 */
|
||||||
#include "lavc_keyframes.h"
|
#include "lavc_keyframes.h"
|
||||||
#endif
|
#endif
|
||||||
#endif
|
|
||||||
#include "mkv_wrap.h"
|
#include "mkv_wrap.h"
|
||||||
#include "options.h"
|
#include "options.h"
|
||||||
#include "subs_edit_box.h"
|
#include "subs_edit_box.h"
|
||||||
|
@ -288,21 +291,27 @@ void VideoContext::SetVideo(const wxString &filename) {
|
||||||
// Close mkv
|
// Close mkv
|
||||||
MatroskaWrapper::wrapper.Close();
|
MatroskaWrapper::wrapper.Close();
|
||||||
}
|
}
|
||||||
|
// do we have ffmpeg? if so try to load keyframes with it
|
||||||
else if (ext == _T(".avi")) {
|
#ifdef WITH_FFMPEG
|
||||||
|
else {
|
||||||
keyFramesLoaded = false;
|
keyFramesLoaded = false;
|
||||||
KeyFrames.Clear();
|
KeyFrames.Clear();
|
||||||
#ifdef __WINDOWS__
|
|
||||||
KeyFrames = VFWWrapper::GetKeyFrames(filename);
|
|
||||||
keyFramesLoaded = true;
|
|
||||||
#else
|
|
||||||
#ifdef WITH_FFMPEG
|
|
||||||
LAVCKeyFrames k(filename.c_str());
|
LAVCKeyFrames k(filename.c_str());
|
||||||
KeyFrames = k.GetKeyFrames();
|
KeyFrames = k.GetKeyFrames();
|
||||||
keyFramesLoaded = true;
|
keyFramesLoaded = true;
|
||||||
#endif
|
|
||||||
#endif
|
|
||||||
}
|
}
|
||||||
|
#else
|
||||||
|
// no ffmpeg, check if we have windows, if so we can load keyframes
|
||||||
|
// from AVI files using VFW
|
||||||
|
#ifdef __WINDOWS__
|
||||||
|
else if (ext == _T(".avi")) {
|
||||||
|
keyFramesLoaded = false;
|
||||||
|
KeyFrames.Clear();
|
||||||
|
KeyFrames = VFWWrapper::GetKeyFrames(filename);
|
||||||
|
keyFramesLoaded = true;
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
#endif
|
||||||
|
|
||||||
// Check if the file is all keyframes
|
// Check if the file is all keyframes
|
||||||
bool isAllKeyFrames = true;
|
bool isAllKeyFrames = true;
|
||||||
|
|
Loading…
Reference in a new issue