2009-04-06 21:14:55 +02:00
|
|
|
// Copyright (c) 2007-2009 Fredrik Mellbin
|
2008-09-01 23:16:13 +02:00
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE.
|
|
|
|
|
|
|
|
#ifndef UTILS_H
|
|
|
|
#define UTILS_H
|
|
|
|
|
2009-05-20 20:57:03 +02:00
|
|
|
#include <vector>
|
2009-10-12 02:11:15 +02:00
|
|
|
#include <sstream>
|
2009-07-14 00:27:40 +02:00
|
|
|
#include <fstream>
|
2009-09-26 23:56:15 +02:00
|
|
|
#include <cstdio>
|
2009-07-14 00:27:40 +02:00
|
|
|
#include "ffms.h"
|
|
|
|
#include "matroskaparser.h"
|
2009-05-20 20:57:03 +02:00
|
|
|
|
2008-09-01 23:16:13 +02:00
|
|
|
extern "C" {
|
2009-07-14 00:27:40 +02:00
|
|
|
#include "stdiostream.h"
|
2010-11-09 20:55:23 +01:00
|
|
|
#include <libavutil/mem.h>
|
2008-12-31 00:57:22 +01:00
|
|
|
#include <libavformat/avformat.h>
|
2008-09-01 23:16:13 +02:00
|
|
|
#include <libavcodec/avcodec.h>
|
|
|
|
#include <libswscale/swscale.h>
|
2011-01-08 21:32:18 +01:00
|
|
|
#ifdef FFMS_USE_POSTPROC
|
2008-09-01 23:16:13 +02:00
|
|
|
#include <libpostproc/postprocess.h>
|
2011-01-08 21:32:18 +01:00
|
|
|
#endif // FFMS_USE_POSTPROC
|
2008-12-31 00:57:22 +01:00
|
|
|
}
|
2008-09-01 23:16:13 +02:00
|
|
|
|
2009-07-14 00:27:40 +02:00
|
|
|
// must be included after ffmpeg headers
|
|
|
|
#include "ffmscompat.h"
|
|
|
|
|
2009-04-25 10:55:14 +02:00
|
|
|
#ifdef HAALISOURCE
|
2009-07-14 00:27:40 +02:00
|
|
|
# define WIN32_LEAN_AND_MEAN
|
2009-04-25 10:55:14 +02:00
|
|
|
# define _WIN32_DCOM
|
|
|
|
# include <windows.h>
|
|
|
|
# include <tchar.h>
|
|
|
|
# include <atlbase.h>
|
|
|
|
# include <dshow.h>
|
|
|
|
# include <initguid.h>
|
2009-07-14 00:27:40 +02:00
|
|
|
# include "CoParser.h"
|
2009-04-25 10:55:14 +02:00
|
|
|
# include "guids.h"
|
|
|
|
#endif
|
|
|
|
|
2009-07-14 00:27:40 +02:00
|
|
|
#define FFMS_GET_VECTOR_PTR(v) (((v).size() ? &(v)[0] : NULL))
|
2009-05-20 20:57:03 +02:00
|
|
|
|
2009-07-14 00:27:40 +02:00
|
|
|
const int64_t ffms_av_nopts_value = static_cast<int64_t>(1) << 63;
|
2009-05-20 20:57:03 +02:00
|
|
|
|
2009-09-26 23:56:15 +02:00
|
|
|
// used for matroska<->ffmpeg codec ID mapping to avoid Win32 dependency
|
|
|
|
typedef struct FFMS_BITMAPINFOHEADER {
|
2011-01-08 21:32:18 +01:00
|
|
|
uint32_t biSize;
|
|
|
|
int32_t biWidth;
|
|
|
|
int32_t biHeight;
|
|
|
|
uint16_t biPlanes;
|
|
|
|
uint16_t biBitCount;
|
|
|
|
uint32_t biCompression;
|
|
|
|
uint32_t biSizeImage;
|
|
|
|
int32_t biXPelsPerMeter;
|
|
|
|
int32_t biYPelsPerMeter;
|
|
|
|
uint32_t biClrUsed;
|
|
|
|
uint32_t biClrImportant;
|
2009-09-26 23:56:15 +02:00
|
|
|
} FFMS_BITMAPINFOHEADER;
|
|
|
|
|
|
|
|
class FFMS_Exception : public std::exception {
|
|
|
|
private:
|
|
|
|
std::string _Message;
|
|
|
|
int _ErrorType;
|
|
|
|
int _SubType;
|
|
|
|
public:
|
|
|
|
FFMS_Exception(int ErrorType, int SubType, const char *Message = "");
|
|
|
|
FFMS_Exception(int ErrorType, int SubType, const std::string &Message);
|
|
|
|
~FFMS_Exception() throw ();
|
|
|
|
const std::string &GetErrorMessage() const;
|
|
|
|
int CopyOut(FFMS_ErrorInfo *ErrorInfo) const;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<class T>
|
|
|
|
class FFSourceResources {
|
|
|
|
private:
|
|
|
|
T *_PrivClass;
|
|
|
|
bool _Enabled;
|
|
|
|
bool _Arg;
|
|
|
|
public:
|
|
|
|
FFSourceResources(T *Target) : _PrivClass(Target), _Enabled(true), _Arg(false) {
|
|
|
|
}
|
|
|
|
|
|
|
|
~FFSourceResources() {
|
|
|
|
if (_Enabled)
|
|
|
|
_PrivClass->Free(_Arg);
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetEnabled(bool Enabled) {
|
|
|
|
_Enabled = Enabled;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetArg(bool Arg) {
|
|
|
|
_Arg = Arg;
|
|
|
|
}
|
|
|
|
|
|
|
|
void CloseCodec(bool Arg) {
|
|
|
|
_Arg = Arg;
|
|
|
|
}
|
|
|
|
};
|
2011-01-08 21:32:18 +01:00
|
|
|
// auto_ptr-ish holder for AVCodecContexts with overridable deleter
|
|
|
|
class FFCodecContext {
|
|
|
|
AVCodecContext *CodecContext;
|
|
|
|
void (*Deleter)(AVCodecContext *);
|
|
|
|
public:
|
|
|
|
FFCodecContext() : CodecContext(0), Deleter(0) { }
|
|
|
|
FFCodecContext(FFCodecContext &r) : CodecContext(r.CodecContext), Deleter(r.Deleter) { r.CodecContext = 0; }
|
|
|
|
FFCodecContext(AVCodecContext *c, void (*d)(AVCodecContext *)) : CodecContext(c), Deleter(d) { }
|
|
|
|
FFCodecContext& operator=(FFCodecContext r) { reset(r.CodecContext, r.Deleter); r.CodecContext = 0; return *this; }
|
|
|
|
~FFCodecContext() { reset(); }
|
|
|
|
AVCodecContext* operator->() { return CodecContext; }
|
|
|
|
operator AVCodecContext*() { return CodecContext; }
|
|
|
|
void reset(AVCodecContext *c = 0, void (*d)(AVCodecContext *) = 0) {
|
|
|
|
if (CodecContext && Deleter) Deleter(CodecContext);
|
|
|
|
CodecContext = c;
|
|
|
|
Deleter = d;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
inline void DeleteHaaliCodecContext(AVCodecContext *CodecContext) {
|
|
|
|
av_freep(&CodecContext->extradata);
|
|
|
|
av_freep(&CodecContext);
|
|
|
|
}
|
|
|
|
inline void DeleteMatroskaCodecContext(AVCodecContext *CodecContext) {
|
|
|
|
avcodec_close(CodecContext);
|
|
|
|
av_freep(&CodecContext);
|
|
|
|
}
|
2009-09-26 23:56:15 +02:00
|
|
|
|
2008-09-01 23:16:13 +02:00
|
|
|
struct MatroskaReaderContext {
|
|
|
|
public:
|
|
|
|
StdIoStream ST;
|
|
|
|
uint8_t *Buffer;
|
|
|
|
unsigned int BufferSize;
|
2009-09-26 23:56:15 +02:00
|
|
|
char CSBuffer[4096];
|
2008-09-01 23:16:13 +02:00
|
|
|
|
|
|
|
MatroskaReaderContext() {
|
|
|
|
InitStdIoStream(&ST);
|
|
|
|
Buffer = NULL;
|
|
|
|
BufferSize = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
~MatroskaReaderContext() {
|
|
|
|
free(Buffer);
|
2011-01-08 21:32:18 +01:00
|
|
|
if (ST.fp) fclose(ST.fp);
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-07-14 00:27:40 +02:00
|
|
|
class ffms_fstream : public std::fstream {
|
|
|
|
public:
|
|
|
|
void open(const char *filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out);
|
|
|
|
ffms_fstream(const char *filename, std::ios_base::openmode mode = std::ios_base::in | std::ios_base::out);
|
|
|
|
};
|
|
|
|
|
2010-11-09 20:55:23 +01:00
|
|
|
template <typename T>
|
|
|
|
class AlignedBuffer {
|
|
|
|
T *buf;
|
|
|
|
|
|
|
|
public:
|
|
|
|
AlignedBuffer(size_t n = 1) {
|
|
|
|
buf = (T*) av_malloc(sizeof(*buf) * n);
|
|
|
|
if (!buf) throw std::bad_alloc();
|
|
|
|
}
|
2011-01-08 21:32:18 +01:00
|
|
|
|
2010-11-09 20:55:23 +01:00
|
|
|
~AlignedBuffer() {
|
|
|
|
av_free(buf);
|
|
|
|
buf = 0;
|
|
|
|
}
|
2011-01-08 21:32:18 +01:00
|
|
|
|
2010-11-09 20:55:23 +01:00
|
|
|
const T &operator[] (size_t i) const { return buf[i]; }
|
|
|
|
T &operator[] (size_t i) { return buf[i]; }
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
class TrackCompressionContext {
|
|
|
|
public:
|
|
|
|
CompressedStream *CS;
|
|
|
|
unsigned CompressionMethod;
|
|
|
|
void *CompressedPrivateData;
|
|
|
|
unsigned CompressedPrivateDataSize;
|
|
|
|
|
|
|
|
TrackCompressionContext(MatroskaFile *MF, TrackInfo *TI, unsigned int Track);
|
|
|
|
~TrackCompressionContext();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-01-08 21:32:18 +01:00
|
|
|
int64_t GetSWSCPUFlags();
|
|
|
|
SwsContext *GetSwsContext(int SrcW, int SrcH, PixelFormat SrcFormat, int DstW, int DstH, PixelFormat DstFormat, int64_t Flags, int ColorSpace = -1);
|
2009-07-14 00:27:40 +02:00
|
|
|
int GetPPCPUFlags();
|
2009-09-26 23:56:15 +02:00
|
|
|
void ClearErrorInfo(FFMS_ErrorInfo *ErrorInfo);
|
2009-05-20 20:57:03 +02:00
|
|
|
FFMS_TrackType HaaliTrackTypeToFFTrackType(int TT);
|
2010-11-09 20:55:23 +01:00
|
|
|
void ReadFrame(uint64_t FilePos, unsigned int &FrameSize, TrackCompressionContext *TCC, MatroskaReaderContext &Context);
|
|
|
|
bool AudioFMTIsFloat(AVSampleFormat FMT);
|
2009-09-26 23:56:15 +02:00
|
|
|
void InitNullPacket(AVPacket &pkt);
|
|
|
|
void FillAP(FFMS_AudioProperties &AP, AVCodecContext *CTX, FFMS_Track &Frames);
|
2011-01-08 21:32:18 +01:00
|
|
|
|
2009-04-25 10:55:14 +02:00
|
|
|
#ifdef HAALISOURCE
|
|
|
|
unsigned vtSize(VARIANT &vt);
|
|
|
|
void vtCopy(VARIANT& vt,void *dest);
|
2011-01-08 21:32:18 +01:00
|
|
|
FFCodecContext InitializeCodecContextFromHaaliInfo(CComQIPtr<IPropertyBag> pBag);
|
2009-04-25 10:55:14 +02:00
|
|
|
#endif
|
2011-01-08 21:32:18 +01:00
|
|
|
|
2009-07-14 00:27:40 +02:00
|
|
|
void InitializeCodecContextFromMatroskaTrackInfo(TrackInfo *TI, AVCodecContext *CodecContext);
|
2009-09-26 23:56:15 +02:00
|
|
|
CodecID MatroskaToFFCodecID(char *Codec, void *CodecPrivate, unsigned int FourCC = 0, unsigned int BitsPerSample = 0);
|
2009-07-14 00:27:40 +02:00
|
|
|
FILE *ffms_fopen(const char *filename, const char *mode);
|
|
|
|
size_t ffms_mbstowcs (wchar_t *wcstr, const char *mbstr, size_t max);
|
2010-11-09 20:55:23 +01:00
|
|
|
#ifdef _WIN32
|
2010-06-12 16:51:33 +02:00
|
|
|
void ffms_patch_lavf_file_open();
|
2010-11-09 20:55:23 +01:00
|
|
|
#endif // _WIN32
|
2009-09-26 23:56:15 +02:00
|
|
|
#ifdef HAALISOURCE
|
|
|
|
CComPtr<IMMContainer> HaaliOpenFile(const char *SourceFile, enum FFMS_Sources SourceMode);
|
2010-11-09 20:55:23 +01:00
|
|
|
#endif // HAALISOURCE
|
2009-09-26 23:56:15 +02:00
|
|
|
void LAVFOpenFile(const char *SourceFile, AVFormatContext *&FormatContext);
|
2010-06-12 16:51:33 +02:00
|
|
|
void CorrectNTSCRationalFramerate(int *Num, int *Den);
|
|
|
|
void CorrectTimebase(FFMS_VideoProperties *VP, FFMS_TrackTimeBase *TTimebase);
|
2010-11-09 20:55:23 +01:00
|
|
|
const char *GetLAVCSampleFormatName(AVSampleFormat s);
|
2008-09-01 23:16:13 +02:00
|
|
|
|
|
|
|
#endif
|