Pick the CSRI renderer when the provider is created rather than every time subtitles are loaded

Originally committed to SVN as r6688.
This commit is contained in:
Thomas Goyne 2012-04-10 20:41:25 +00:00
parent 9f87a2bacb
commit 30565530eb
2 changed files with 54 additions and 45 deletions

View file

@ -49,9 +49,40 @@
#include "video_context.h"
#include "video_frame.h"
#ifdef WIN32
#define CSRIAPI
#include "../../contrib/csri/include/csri/csri.h"
#else
#include <csri/csri.h>
#endif
static wxMutex csri_mutex;
CSRISubtitlesProvider::CSRISubtitlesProvider(std::string type) : subType(type) {
CSRISubtitlesProvider::CSRISubtitlesProvider(std::string type)
: can_open_mem(true)
, instance(0, csri_close)
{
wxMutexLocker lock(csri_mutex);
csri_rend *cur = NULL;
// Select renderer
for (cur = csri_renderer_default(); cur; cur = csri_renderer_next(cur)) {
std::string name(csri_renderer_info(cur)->name);
if (name == type) {
renderer = cur;
break;
}
}
// Matching renderer not found, fallback to default
if (!renderer)
renderer = csri_renderer_default();
if (!renderer)
throw "No CSRI renderer available, cannot show subtitles. Try installing one or switch to another subtitle provider.";
std::string name(csri_renderer_info(renderer)->name);
can_open_mem = (name.find("vsfilter") == name.npos);
}
CSRISubtitlesProvider::~CSRISubtitlesProvider() {
@ -61,35 +92,12 @@ CSRISubtitlesProvider::~CSRISubtitlesProvider() {
void CSRISubtitlesProvider::LoadSubtitles(AssFile *subs) {
wxMutexLocker lock(csri_mutex);
// CSRI variables
csri_rend *cur,*renderer=NULL;
// Select renderer
bool canOpenMem = true;
for (cur = csri_renderer_default();cur;cur=csri_renderer_next(cur)) {
std::string name(csri_renderer_info(cur)->name);
if (name == subType) {
renderer = cur;
if (name.find("vsfilter") != name.npos) canOpenMem = false;
break;
}
}
// Matching renderer not found, fallback to default
if (!renderer) {
renderer = csri_renderer_default();
if (!renderer) {
throw "No CSRI renderer available, cannot show subtitles. Try installing one or switch to another subtitle provider.";
}
}
// Open from memory
if (canOpenMem) {
if (can_open_mem) {
std::vector<char> data;
subs->SaveMemory(data);
instance.reset(csri_open_mem(renderer,&data[0],data.size(),NULL), &csri_close);
instance = csri_open_mem(renderer, &data[0], data.size(), NULL);
}
// Open from disk
else {
if (tempfile.empty()) {
@ -98,15 +106,13 @@ void CSRISubtitlesProvider::LoadSubtitles(AssFile *subs) {
tempfile += ".ass";
}
subs->Save(tempfile, false, false, wxSTRING_ENCODING);
instance.reset(csri_open_file(renderer,tempfile.utf8_str(),NULL), &csri_close);
instance = csri_open_file(renderer, tempfile.utf8_str(), NULL);
}
}
void CSRISubtitlesProvider::DrawSubtitles(AegiVideoFrame &dst,double time) {
// Check if CSRI loaded properly
if (!instance.get()) return;
wxMutexLocker lock(csri_mutex);
if (!instance) return;
// Load data into frame
csri_frame frame;
@ -125,11 +131,13 @@ void CSRISubtitlesProvider::DrawSubtitles(AegiVideoFrame &dst,double time) {
format.width = dst.w;
format.height = dst.h;
format.pixfmt = frame.pixfmt;
int error = csri_request_fmt(instance.get(),&format);
wxMutexLocker lock(csri_mutex);
int error = csri_request_fmt(instance, &format);
if (error) return;
// Render
csri_render(instance.get(),&frame,time);
csri_render(instance, &frame, time);
}
std::vector<std::string> CSRISubtitlesProvider::GetSubTypes() {

View file

@ -36,18 +36,16 @@
#ifdef WITH_CSRI
#include "include/aegisub/subtitles_provider.h"
#ifndef AGI_PRE
#include <tr1/memory>
#include <vector>
#endif
#include "include/aegisub/subtitles_provider.h"
#ifdef WIN32
#define CSRIAPI
#include "../../contrib/csri/include/csri/csri.h"
#else
#include <csri/csri.h>
#endif
#include <libaegisub/scoped_ptr.h>
typedef void csri_rend;
typedef void csri_inst;
/// DOCME
/// @class CSRISubtitlesProvider
@ -55,12 +53,15 @@
///
/// DOCME
class CSRISubtitlesProvider : public SubtitlesProvider {
/// DOCME
std::string subType;
agi::scoped_holder<csri_inst*> instance;
csri_rend *renderer;
/// DOCME
std::tr1::shared_ptr<csri_inst> instance;
/// VSFilter's implementation of csri_open_mem writes the file to disk then
/// opens it with the standard file reader, and by writing the file ourselves
/// we can skip a few pointless charset conversions
bool can_open_mem;
/// Name of the file passed to renderers with can_open_mem false
wxString tempfile;
public:
CSRISubtitlesProvider(std::string subType);