From 71511a39500f20e3d91c92fdb253cfabf246965c Mon Sep 17 00:00:00 2001 From: David Lamparter Date: Fri, 14 Apr 2006 22:35:02 +0000 Subject: [PATCH] LAVCFile Originally committed to SVN as r325. --- core/Makefile.am | 1 + core/lavc_file.cpp | 65 ++++++++++++++++++++++++++++++++++++ core/lavc_file.h | 60 +++++++++++++++++++++++++++++++++ core/video_provider_lavc.cpp | 25 ++++++-------- core/video_provider_lavc.h | 3 +- 5 files changed, 138 insertions(+), 16 deletions(-) create mode 100644 core/lavc_file.cpp create mode 100644 core/lavc_file.h diff --git a/core/Makefile.am b/core/Makefile.am index a6c838902..cae9f67ac 100644 --- a/core/Makefile.am +++ b/core/Makefile.am @@ -59,6 +59,7 @@ aegisub_SOURCES = about.cpp \ frame_main_events.cpp \ hilimod_textctrl.cpp \ hotkeys.cpp \ + lavc_file.cpp \ main.cpp \ md5.c \ mkv_wrap.cpp \ diff --git a/core/lavc_file.cpp b/core/lavc_file.cpp new file mode 100644 index 000000000..c5fef96c8 --- /dev/null +++ b/core/lavc_file.cpp @@ -0,0 +1,65 @@ +// Copyright (c) 2005, Rodrigo Braz Monteiro +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// * Neither the name of the Aegisub Group nor the names of its contributors +// may be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// +// ----------------------------------------------------------------------------- +// +// AEGISUB +// +// Website: http://aegisub.cellosoft.com +// Contact: mailto:zeratul@cellosoft.com +// + + +#include "lavc_file.h" + +#ifdef USE_LAVC + +LAVCFile::LAVCFile(wxString filename) +{ + int result = 0; + fctx = NULL; + + result = av_open_input_file(&fctx,filename.mb_str(wxConvLocal),NULL,0,NULL); + if (result != 0) throw _T("Failed opening file."); + + // Get stream info + result = av_find_stream_info(fctx); + if (result < 0) { + av_close_input_file(fctx); + fctx = NULL; + throw _T("Unable to read stream info"); + } + refs = 1; +} + +LAVCFile::~LAVCFile() +{ + if (fctx) + av_close_input_file(fctx); +} + +#endif diff --git a/core/lavc_file.h b/core/lavc_file.h new file mode 100644 index 000000000..02e048376 --- /dev/null +++ b/core/lavc_file.h @@ -0,0 +1,60 @@ +// Copyright (c) 2005, Rodrigo Braz Monteiro +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// * Neither the name of the Aegisub Group nor the names of its contributors +// may be used to endorse or promote products derived from this software +// without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// +// ----------------------------------------------------------------------------- +// +// AEGISUB +// +// Website: http://aegisub.cellosoft.com +// Contact: mailto:zeratul@cellosoft.com +// + + +#pragma once + +#ifndef LAVC_FILE_H +#define LAVC_FILE_H + +#include +#include + +class LAVCFile { +private: + unsigned refs; + + LAVCFile(wxString filename); + ~LAVCFile(); +public: + AVFormatContext *fctx; + + static LAVCFile *Create(wxString filename) { return new LAVCFile(filename); } + LAVCFile *AddRef() { refs++; return this; }; + void Release() { if (!--refs) delete this; }; +}; + +#endif /* LAVC_FILE_H */ + diff --git a/core/video_provider_lavc.cpp b/core/video_provider_lavc.cpp index 37e546770..1238b75c6 100644 --- a/core/video_provider_lavc.cpp +++ b/core/video_provider_lavc.cpp @@ -50,7 +50,7 @@ LAVCVideoProvider::LAVCVideoProvider(wxString filename, wxString subfilename) { // Init variables codecContext = NULL; - formatContext = NULL; + lavcfile = NULL; codec = NULL; stream = NULL; frame = NULL; @@ -87,24 +87,18 @@ void LAVCVideoProvider::LoadVideo(wxString filename) { // Close first Close(); + lavcfile = LAVCFile::Create(filename); // Load try { - // Open file int result = 0; - result = av_open_input_file(&formatContext,filename.mb_str(wxConvLocal),NULL,0,NULL); - if (result != 0) throw _T("Failed opening file."); - - // Get stream info - result = av_find_stream_info(formatContext); - if (result < 0) throw _T("Unable to read stream info"); // Find video stream vidStream = -1; codecContext = NULL; - for (int i=0;inb_streams;i++) { - codecContext = formatContext->streams[i]->codec; + for (int i=0;ifctx->nb_streams;i++) { + codecContext = lavcfile->fctx->streams[i]->codec; if (codecContext->codec_type == CODEC_TYPE_VIDEO) { - stream = formatContext->streams[i]; + stream = lavcfile->fctx->streams[i]; vidStream = i; break; } @@ -179,8 +173,9 @@ void LAVCVideoProvider::Close() { codec = NULL; // Close format context - if (formatContext) av_close_input_file(formatContext); - formatContext = NULL; + if (lavcfile) + lavcfile->Release(); + lavcfile = NULL; } @@ -198,7 +193,7 @@ void LAVCVideoProvider::UpdateDisplaySize() { bool LAVCVideoProvider::GetNextFrame() { // Read packet AVPacket packet; - while (av_read_frame(formatContext, &packet)>=0) { + while (av_read_frame(lavcfile->fctx, &packet)>=0) { // Check if packet is part of video stream if(packet.stream_index == vidStream) { // Decode frame @@ -359,7 +354,7 @@ wxBitmap LAVCVideoProvider::GetFrame(int n) { // Constant frame rate else { seekTo = n; - result = av_seek_frame(formatContext,vidStream,seekTo,AVSEEK_FLAG_BACKWARD); + result = av_seek_frame(lavcfile->fctx,vidStream,seekTo,AVSEEK_FLAG_BACKWARD); // Seek to keyframe if (result == 0) { diff --git a/core/video_provider_lavc.h b/core/video_provider_lavc.h index d3c7664c3..ccc11b97b 100644 --- a/core/video_provider_lavc.h +++ b/core/video_provider_lavc.h @@ -53,6 +53,7 @@ #include #include "video_provider.h" #include "mkv_wrap.h" +#include "lavc_file.h" /////////////////////// @@ -61,7 +62,7 @@ class LAVCVideoProvider : public VideoProvider { private: MatroskaWrapper mkv; - AVFormatContext *formatContext; + LAVCFile *lavcfile; AVCodecContext *codecContext; AVStream *stream; AVCodec *codec;