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.
|
|
|
|
|
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
|
|
|
#include <set>
|
|
|
|
#include <algorithm>
|
2008-09-04 00:26:27 +02:00
|
|
|
#include <memory>
|
2008-09-14 19:45:11 +02:00
|
|
|
#include <errno.h>
|
2008-09-01 23:16:13 +02:00
|
|
|
#include "indexing.h"
|
|
|
|
|
2009-01-04 07:31:48 +01:00
|
|
|
#ifdef __UNIX__
|
|
|
|
#define _fseeki64 fseeko
|
|
|
|
#define _ftelli64 ftello
|
|
|
|
#define _snprintf snprintf
|
|
|
|
#endif
|
|
|
|
|
2008-12-31 00:57:22 +01:00
|
|
|
extern "C" {
|
|
|
|
#include <libavformat/avformat.h>
|
|
|
|
#include <libavcodec/avcodec.h>
|
|
|
|
#include "MatroskaParser.h"
|
|
|
|
#include "stdiostream.h"
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef _WIN32
|
|
|
|
# define _WIN32_DCOM
|
|
|
|
# include <windows.h>
|
|
|
|
# include <tchar.h>
|
|
|
|
# include <atlbase.h>
|
|
|
|
# include <dshow.h>
|
|
|
|
# include "CoParser.h"
|
|
|
|
# include <initguid.h>
|
|
|
|
# include "guids.h"
|
|
|
|
#endif
|
|
|
|
|
2009-05-22 23:28:02 +02:00
|
|
|
class MatroskaAudioContext : public SharedAudioContext {
|
2008-09-01 23:16:13 +02:00
|
|
|
public:
|
|
|
|
CompressedStream *CS;
|
2009-04-25 10:55:14 +02:00
|
|
|
uint8_t *CodecPrivate;
|
2008-09-01 23:16:13 +02:00
|
|
|
|
2008-09-27 03:21:57 +02:00
|
|
|
MatroskaAudioContext() {
|
2008-09-01 23:16:13 +02:00
|
|
|
CS = NULL;
|
2009-04-25 10:55:14 +02:00
|
|
|
CodecPrivate = NULL;
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
|
|
|
|
2008-09-27 03:21:57 +02:00
|
|
|
~MatroskaAudioContext() {
|
2008-09-01 23:16:13 +02:00
|
|
|
if (CTX) {
|
|
|
|
avcodec_close(CTX);
|
|
|
|
av_free(CTX);
|
|
|
|
}
|
|
|
|
if (CS)
|
|
|
|
cs_Destroy(CS);
|
2009-05-22 23:28:02 +02:00
|
|
|
delete[] CodecPrivate;
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-05-22 23:28:02 +02:00
|
|
|
class FFAudioContext : public SharedAudioContext {
|
2008-09-27 03:21:57 +02:00
|
|
|
public:
|
|
|
|
~FFAudioContext() {
|
2008-10-02 20:04:22 +02:00
|
|
|
if (CTX)
|
|
|
|
avcodec_close(CTX);
|
2008-09-27 03:21:57 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-04-11 20:45:40 +02:00
|
|
|
#ifdef HAALISOURCE
|
|
|
|
class HaaliIndexMemory {
|
2008-12-31 00:57:22 +01:00
|
|
|
private:
|
|
|
|
MatroskaAudioContext *AudioContexts;
|
|
|
|
public:
|
2009-05-22 23:28:02 +02:00
|
|
|
HaaliIndexMemory(int Tracks, MatroskaAudioContext *&AudioContexts) {
|
2008-12-31 00:57:22 +01:00
|
|
|
AudioContexts = new MatroskaAudioContext[Tracks];
|
|
|
|
this->AudioContexts = AudioContexts;
|
|
|
|
}
|
|
|
|
|
2009-04-11 20:45:40 +02:00
|
|
|
~HaaliIndexMemory() {
|
2009-05-22 23:28:02 +02:00
|
|
|
delete[] AudioContexts;
|
2008-12-31 00:57:22 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
#endif
|
|
|
|
|
2008-09-27 03:21:57 +02:00
|
|
|
class MatroskaIndexMemory {
|
2008-09-04 00:26:27 +02:00
|
|
|
private:
|
2008-09-27 03:21:57 +02:00
|
|
|
MatroskaAudioContext *AudioContexts;
|
2008-10-02 20:04:22 +02:00
|
|
|
MatroskaFile *MF;
|
|
|
|
MatroskaReaderContext *MC;
|
2008-09-04 00:26:27 +02:00
|
|
|
public:
|
2009-05-22 23:28:02 +02:00
|
|
|
MatroskaIndexMemory(int Tracks, MatroskaAudioContext *&AudioContexts, MatroskaFile *MF, MatroskaReaderContext *MC) {
|
2008-09-27 03:21:57 +02:00
|
|
|
AudioContexts = new MatroskaAudioContext[Tracks];
|
2008-09-04 00:26:27 +02:00
|
|
|
this->AudioContexts = AudioContexts;
|
2008-10-02 20:04:22 +02:00
|
|
|
this->MF = MF;
|
|
|
|
this->MC = MC;
|
2008-09-04 00:26:27 +02:00
|
|
|
}
|
|
|
|
|
2008-09-27 03:21:57 +02:00
|
|
|
~MatroskaIndexMemory() {
|
2009-05-22 23:28:02 +02:00
|
|
|
delete[] AudioContexts;
|
2008-10-02 20:04:22 +02:00
|
|
|
mkv_Close(MF);
|
|
|
|
fclose(MC->ST.fp);
|
2008-09-27 03:21:57 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class FFIndexMemory {
|
|
|
|
private:
|
|
|
|
FFAudioContext *AudioContexts;
|
2008-10-02 20:04:22 +02:00
|
|
|
AVFormatContext *FormatContext;
|
2008-09-27 03:21:57 +02:00
|
|
|
public:
|
2009-05-22 23:28:02 +02:00
|
|
|
FFIndexMemory(int Tracks, FFAudioContext *&AudioContexts, AVFormatContext *&FormatContext) {
|
2008-09-27 03:21:57 +02:00
|
|
|
AudioContexts = new FFAudioContext[Tracks];
|
|
|
|
this->AudioContexts = AudioContexts;
|
2008-10-02 20:04:22 +02:00
|
|
|
this->FormatContext = FormatContext;
|
2008-09-27 03:21:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
~FFIndexMemory() {
|
2009-05-22 23:28:02 +02:00
|
|
|
delete[] AudioContexts;
|
2008-10-02 20:04:22 +02:00
|
|
|
av_close_input_file(FormatContext);
|
2008-09-04 00:26:27 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2009-05-16 01:11:18 +02:00
|
|
|
static bool DTSComparison(TFrameInfo FI1, TFrameInfo FI2) {
|
2008-09-01 23:16:13 +02:00
|
|
|
return FI1.DTS < FI2.DTS;
|
|
|
|
}
|
|
|
|
|
2009-05-22 23:28:02 +02:00
|
|
|
static void SortTrackIndex(FFIndex *Index) {
|
2009-05-16 01:11:18 +02:00
|
|
|
for (FFIndex::iterator Cur=Index->begin(); Cur!=Index->end(); Cur++)
|
2008-09-01 23:16:13 +02:00
|
|
|
std::sort(Cur->begin(), Cur->end(), DTSComparison);
|
|
|
|
}
|
|
|
|
|
2009-05-22 23:28:02 +02:00
|
|
|
bool FFIndexer::WriteAudio(SharedAudioContext &AudioContext, FFIndex *Index, int Track, int DBSize, char *ErrorMsg, unsigned MsgSize) {
|
|
|
|
// Delay writer creation until after an audio frame has been decoded. This ensures that all parameters are known when writing the headers.
|
|
|
|
if (DBSize > 0) {
|
|
|
|
if (!AudioContext.W64W) {
|
|
|
|
TAudioProperties AP;
|
|
|
|
FillAP(AP, AudioContext.CTX, (*Index)[Track]);
|
|
|
|
char *WName = new char[(*ANC)(SourceFile, Track, &AP, NULL, ANCPrivate)];
|
|
|
|
(*ANC)(SourceFile, Track, &AP, WName, ANCPrivate);
|
|
|
|
std::string WN(WName);
|
|
|
|
delete[] WName;
|
|
|
|
try {
|
|
|
|
AudioContext.W64W = new Wave64Writer(WN.c_str(), av_get_bits_per_sample_format(AudioContext.CTX->sample_fmt),
|
|
|
|
AudioContext.CTX->channels, AudioContext.CTX->sample_rate, AudioFMTIsFloat(AudioContext.CTX->sample_fmt));
|
|
|
|
} catch (...) {
|
|
|
|
_snprintf(ErrorMsg, MsgSize, "Failed to write wave data");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioContext.W64W->WriteData(DecodingBuffer, DBSize);
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2009-04-11 20:45:40 +02:00
|
|
|
#ifdef HAALISOURCE
|
2009-05-22 23:28:02 +02:00
|
|
|
FFHaaliIndexer::FFHaaliIndexer(const char *Filename, int SourceMode, char *ErrorMsg, unsigned MsgSize) {
|
|
|
|
SourceFile = Filename;
|
2009-05-20 20:57:03 +02:00
|
|
|
this->SourceMode = SourceMode;
|
|
|
|
memset(TrackType, FFMS_TYPE_UNKNOWN, sizeof(TrackType));
|
|
|
|
memset(Codec, 0, sizeof(Codec));
|
|
|
|
memset(CodecPrivate, 0, sizeof(CodecPrivate));
|
|
|
|
memset(CodecPrivateSize, 0, sizeof(CodecPrivateSize));
|
2008-12-31 00:57:22 +01:00
|
|
|
|
2009-04-11 20:45:40 +02:00
|
|
|
CLSID clsid = HAALI_TS_Parser;
|
|
|
|
if (SourceMode == 1)
|
|
|
|
clsid = HAALI_OGM_Parser;
|
2008-12-31 00:57:22 +01:00
|
|
|
|
|
|
|
if (FAILED(pMMC.CoCreateInstance(clsid))) {
|
|
|
|
_snprintf(ErrorMsg, MsgSize, "Can't create parser");
|
2009-05-20 20:57:03 +02:00
|
|
|
throw ErrorMsg;
|
2008-12-31 00:57:22 +01:00
|
|
|
}
|
|
|
|
|
2009-04-25 10:55:14 +02:00
|
|
|
CComPtr<IMemAlloc> pMA;
|
2008-12-31 00:57:22 +01:00
|
|
|
if (FAILED(pMA.CoCreateInstance(CLSID_MemAlloc))) {
|
|
|
|
_snprintf(ErrorMsg, MsgSize, "Can't create memory allocator");
|
2009-05-20 20:57:03 +02:00
|
|
|
throw ErrorMsg;
|
2008-12-31 00:57:22 +01:00
|
|
|
}
|
|
|
|
|
2009-04-25 10:55:14 +02:00
|
|
|
CComPtr<IMMStream> pMS;
|
2008-12-31 00:57:22 +01:00
|
|
|
if (FAILED(pMS.CoCreateInstance(CLSID_DiskFile))) {
|
|
|
|
_snprintf(ErrorMsg, MsgSize, "Can't create disk file reader");
|
2009-05-20 20:57:03 +02:00
|
|
|
throw ErrorMsg;
|
2008-12-31 00:57:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
WCHAR WSourceFile[2048];
|
|
|
|
mbstowcs(WSourceFile, SourceFile, 2000);
|
|
|
|
CComQIPtr<IMMStreamOpen> pMSO(pMS);
|
|
|
|
if (FAILED(pMSO->Open(WSourceFile))) {
|
|
|
|
_snprintf(ErrorMsg, MsgSize, "Can't open file");
|
2009-05-20 20:57:03 +02:00
|
|
|
throw ErrorMsg;
|
2008-12-31 00:57:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (FAILED(pMMC->Open(pMS, 0, NULL, pMA))) {
|
|
|
|
_snprintf(ErrorMsg, MsgSize, "Can't parse file");
|
2009-05-20 20:57:03 +02:00
|
|
|
throw ErrorMsg;
|
2008-12-31 00:57:22 +01:00
|
|
|
}
|
|
|
|
|
2009-05-20 20:57:03 +02:00
|
|
|
NumTracks = 0;
|
2008-12-31 00:57:22 +01:00
|
|
|
CComPtr<IEnumUnknown> pEU;
|
|
|
|
if (SUCCEEDED(pMMC->EnumTracks(&pEU))) {
|
|
|
|
CComPtr<IUnknown> pU;
|
|
|
|
while (pEU->Next(1, &pU, NULL) == S_OK) {
|
|
|
|
CComQIPtr<IPropertyBag> pBag = pU;
|
|
|
|
|
|
|
|
if (pBag) {
|
2009-04-22 20:38:28 +02:00
|
|
|
CComVariant pV;
|
2008-12-31 00:57:22 +01:00
|
|
|
|
2009-04-26 00:09:49 +02:00
|
|
|
pV.Clear();
|
2009-04-25 10:55:14 +02:00
|
|
|
if (SUCCEEDED(pBag->Read(L"Type", &pV, NULL)) && SUCCEEDED(pV.ChangeType(VT_UI4)))
|
2009-05-20 20:57:03 +02:00
|
|
|
TrackType[NumTracks] = HaaliTrackTypeToFFTrackType(pV.uintVal);
|
2008-12-31 00:57:22 +01:00
|
|
|
|
2009-04-26 00:09:49 +02:00
|
|
|
pV.Clear();
|
2009-04-25 10:55:14 +02:00
|
|
|
if (SUCCEEDED(pBag->Read(L"CodecPrivate", &pV, NULL))) {
|
2009-05-20 20:57:03 +02:00
|
|
|
CodecPrivateSize[NumTracks] = vtSize(pV);
|
|
|
|
CodecPrivate[NumTracks] = new uint8_t[CodecPrivateSize[NumTracks]];
|
|
|
|
vtCopy(pV, CodecPrivate[NumTracks]);
|
2008-12-31 00:57:22 +01:00
|
|
|
}
|
2009-04-26 00:09:49 +02:00
|
|
|
|
|
|
|
pV.Clear();
|
|
|
|
if (SUCCEEDED(pBag->Read(L"CodecID", &pV, NULL)) && SUCCEEDED(pV.ChangeType(VT_BSTR))) {
|
2009-05-20 20:57:03 +02:00
|
|
|
char CodecID[2048];
|
|
|
|
wcstombs(CodecID, pV.bstrVal, 2000);
|
|
|
|
Codec[NumTracks] = avcodec_find_decoder(MatroskaToFFCodecID(CodecID, CodecPrivate[NumTracks]));
|
2009-04-26 00:09:49 +02:00
|
|
|
}
|
2008-12-31 00:57:22 +01:00
|
|
|
}
|
|
|
|
|
2009-05-20 20:57:03 +02:00
|
|
|
pU = NULL;
|
|
|
|
NumTracks++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-22 23:28:02 +02:00
|
|
|
FFIndex *FFHaaliIndexer::DoIndexing(char *ErrorMsg, unsigned MsgSize) {
|
2009-05-20 20:57:03 +02:00
|
|
|
// Audio stuff
|
|
|
|
MatroskaAudioContext *AudioContexts;
|
2009-05-22 23:28:02 +02:00
|
|
|
HaaliIndexMemory IM = HaaliIndexMemory(NumTracks, AudioContexts);
|
2008-12-31 00:57:22 +01:00
|
|
|
|
2009-05-22 23:28:02 +02:00
|
|
|
std::auto_ptr<FFIndex> TrackIndices(new FFIndex());
|
2009-05-20 20:57:03 +02:00
|
|
|
TrackIndices->Decoder = 2;
|
|
|
|
if (SourceMode == 1)
|
|
|
|
TrackIndices->Decoder = 3;
|
2008-12-31 00:57:22 +01:00
|
|
|
|
|
|
|
|
2009-05-20 20:57:03 +02:00
|
|
|
for (int i = 0; i < NumTracks; i++) {
|
|
|
|
TrackIndices->push_back(FFTrack(1, 1000000000, TrackType[i]));
|
|
|
|
|
|
|
|
if (IndexMask & (1 << i) && TrackType[i] == FFMS_TYPE_AUDIO) {
|
|
|
|
AVCodecContext *AudioCodecContext = avcodec_alloc_context();
|
|
|
|
AudioCodecContext->extradata = CodecPrivate[i];
|
|
|
|
AudioCodecContext->extradata_size = CodecPrivateSize[i];
|
|
|
|
AudioContexts[i].CTX = AudioCodecContext;
|
|
|
|
|
|
|
|
if (Codec[i] == NULL) {
|
|
|
|
av_free(AudioCodecContext);
|
|
|
|
AudioContexts[i].CTX = NULL;
|
|
|
|
_snprintf(ErrorMsg, MsgSize, "Audio codec not found");
|
|
|
|
return NULL;
|
2008-12-31 00:57:22 +01:00
|
|
|
}
|
|
|
|
|
2009-05-20 20:57:03 +02:00
|
|
|
if (avcodec_open(AudioCodecContext, Codec[i]) < 0) {
|
|
|
|
av_free(AudioCodecContext);
|
|
|
|
AudioContexts[i].CTX = NULL;
|
|
|
|
_snprintf(ErrorMsg, MsgSize, "Could not open audio codec");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
IndexMask &= ~(1 << i);
|
2008-12-31 00:57:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
//
|
|
|
|
|
2009-04-11 20:45:40 +02:00
|
|
|
AVPacket TempPacket;
|
2009-05-03 21:25:54 +02:00
|
|
|
InitNullPacket(&TempPacket);
|
2009-04-11 20:45:40 +02:00
|
|
|
|
2008-12-31 00:57:22 +01:00
|
|
|
for (;;) {
|
2009-05-20 20:57:03 +02:00
|
|
|
if (IC) {
|
2009-05-22 23:28:02 +02:00
|
|
|
if ((*IC)(0, 1, ICPrivate)) {
|
2008-12-31 00:57:22 +01:00
|
|
|
_snprintf(ErrorMsg, MsgSize, "Cancelled by user");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CComPtr<IMMFrame> pMMF;
|
|
|
|
if (pMMC->ReadFrame(NULL, &pMMF) != S_OK)
|
|
|
|
break;
|
|
|
|
|
2009-04-25 10:55:14 +02:00
|
|
|
REFERENCE_TIME Ts, Te;
|
2008-12-31 00:57:22 +01:00
|
|
|
HRESULT hr = pMMF->GetTime(&Ts, &Te);
|
|
|
|
|
2009-05-22 23:28:02 +02:00
|
|
|
unsigned int Track = pMMF->GetTrack();
|
2008-12-31 00:57:22 +01:00
|
|
|
|
|
|
|
// Only create index entries for video for now to save space
|
2009-05-22 23:28:02 +02:00
|
|
|
if (TrackType[Track] == FFMS_TYPE_VIDEO) {
|
|
|
|
(*TrackIndices)[Track].push_back(TFrameInfo(Ts, pMMF->IsSyncPoint() == S_OK));
|
|
|
|
} else if (TrackType[Track] == FFMS_TYPE_AUDIO && (IndexMask & (1 << Track))) {
|
|
|
|
(*TrackIndices)[Track].push_back(TFrameInfo(Ts, AudioContexts[Track].CurrentSample, 0 /* FIXME? */, pMMF->GetActualDataLength(), pMMF->IsSyncPoint() == S_OK));
|
|
|
|
AVCodecContext *AudioCodecContext = AudioContexts[Track].CTX;
|
2009-04-11 20:45:40 +02:00
|
|
|
pMMF->GetPointer(&TempPacket.data);
|
|
|
|
TempPacket.size = pMMF->GetActualDataLength();
|
2008-12-31 00:57:22 +01:00
|
|
|
|
2009-04-11 20:45:40 +02:00
|
|
|
while (TempPacket.size > 0) {
|
2008-12-31 00:57:22 +01:00
|
|
|
int dbsize = AVCODEC_MAX_AUDIO_FRAME_SIZE*10;
|
2009-05-22 23:28:02 +02:00
|
|
|
int Ret = avcodec_decode_audio3(AudioCodecContext, DecodingBuffer, &dbsize, &TempPacket);
|
2008-12-31 00:57:22 +01:00
|
|
|
if (Ret < 0) {
|
|
|
|
if (IgnoreDecodeErrors) {
|
2009-05-22 23:28:02 +02:00
|
|
|
(*TrackIndices)[Track].clear();
|
|
|
|
IndexMask &= ~(1 << Track);
|
2008-12-31 00:57:22 +01:00
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
_snprintf(ErrorMsg, MsgSize, "Audio decoding error");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (Ret > 0) {
|
2009-04-11 20:45:40 +02:00
|
|
|
TempPacket.size -= Ret;
|
|
|
|
TempPacket.data += Ret;
|
2008-12-31 00:57:22 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dbsize > 0)
|
2009-05-22 23:28:02 +02:00
|
|
|
AudioContexts[Track].CurrentSample += (dbsize * 8) / (av_get_bits_per_sample_format(AudioCodecContext->sample_fmt) * AudioCodecContext->channels);
|
2008-12-31 00:57:22 +01:00
|
|
|
|
2009-05-22 23:28:02 +02:00
|
|
|
if (DumpMask & (1 << Track))
|
|
|
|
WriteAudio(AudioContexts[Track], TrackIndices.get(), Track, dbsize, ErrorMsg, MsgSize);
|
2008-12-31 00:57:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-22 23:28:02 +02:00
|
|
|
SortTrackIndex(TrackIndices.get());
|
|
|
|
return TrackIndices.release();
|
2008-12-31 00:57:22 +01:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2009-05-22 23:28:02 +02:00
|
|
|
FFMatroskaIndexer::FFMatroskaIndexer(const char *Filename, char *ErrorMsg, unsigned MsgSize) {
|
|
|
|
SourceFile = Filename;
|
2008-09-01 23:16:13 +02:00
|
|
|
char ErrorMessage[256];
|
|
|
|
|
|
|
|
InitStdIoStream(&MC.ST);
|
|
|
|
MC.ST.fp = fopen(SourceFile, "rb");
|
|
|
|
if (MC.ST.fp == NULL) {
|
|
|
|
_snprintf(ErrorMsg, MsgSize, "Can't open '%s': %s", SourceFile, strerror(errno));
|
2009-05-20 20:57:03 +02:00
|
|
|
throw ErrorMsg;
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
setvbuf(MC.ST.fp, NULL, _IOFBF, CACHESIZE);
|
|
|
|
|
|
|
|
MF = mkv_OpenEx(&MC.ST.base, 0, 0, ErrorMessage, sizeof(ErrorMessage));
|
|
|
|
if (MF == NULL) {
|
|
|
|
fclose(MC.ST.fp);
|
|
|
|
_snprintf(ErrorMsg, MsgSize, "Can't parse Matroska file: %s", ErrorMessage);
|
2009-05-20 20:57:03 +02:00
|
|
|
throw ErrorMsg;
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
2009-05-20 20:57:03 +02:00
|
|
|
}
|
2008-09-01 23:16:13 +02:00
|
|
|
|
2009-05-22 23:28:02 +02:00
|
|
|
FFIndex *FFMatroskaIndexer::DoIndexing(char *ErrorMsg, unsigned MsgSize) {
|
2009-05-20 20:57:03 +02:00
|
|
|
char ErrorMessage[256];
|
2008-09-01 23:16:13 +02:00
|
|
|
|
2009-05-20 20:57:03 +02:00
|
|
|
// Audio stuff
|
2008-09-27 03:21:57 +02:00
|
|
|
MatroskaAudioContext *AudioContexts;
|
2009-05-22 23:28:02 +02:00
|
|
|
MatroskaIndexMemory IM = MatroskaIndexMemory(mkv_GetNumTracks(MF), AudioContexts, MF, &MC);
|
2008-09-01 23:16:13 +02:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < mkv_GetNumTracks(MF); i++) {
|
2008-12-31 00:57:22 +01:00
|
|
|
TrackInfo *TI = mkv_GetTrackInfo(MF, i);
|
|
|
|
if (IndexMask & (1 << i) && TI->Type == TT_AUDIO) {
|
2008-09-01 23:16:13 +02:00
|
|
|
AVCodecContext *AudioCodecContext = avcodec_alloc_context();
|
2008-12-31 00:57:22 +01:00
|
|
|
AudioCodecContext->extradata = (uint8_t *)TI->CodecPrivate;
|
|
|
|
AudioCodecContext->extradata_size = TI->CodecPrivateSize;
|
2008-09-01 23:16:13 +02:00
|
|
|
AudioContexts[i].CTX = AudioCodecContext;
|
|
|
|
|
2008-12-31 00:57:22 +01:00
|
|
|
if (TI->CompEnabled) {
|
2008-09-01 23:16:13 +02:00
|
|
|
AudioContexts[i].CS = cs_Create(MF, i, ErrorMessage, sizeof(ErrorMessage));
|
|
|
|
if (AudioContexts[i].CS == NULL) {
|
2008-10-01 21:07:59 +02:00
|
|
|
av_free(AudioCodecContext);
|
|
|
|
AudioContexts[i].CTX = NULL;
|
2008-09-01 23:16:13 +02:00
|
|
|
_snprintf(ErrorMsg, MsgSize, "Can't create decompressor: %s", ErrorMessage);
|
2008-09-04 00:26:27 +02:00
|
|
|
return NULL;
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-31 00:57:22 +01:00
|
|
|
AVCodec *AudioCodec = avcodec_find_decoder(MatroskaToFFCodecID(TI->CodecID, TI->CodecPrivate));
|
2008-09-01 23:16:13 +02:00
|
|
|
if (AudioCodec == NULL) {
|
2008-10-01 21:07:59 +02:00
|
|
|
av_free(AudioCodecContext);
|
|
|
|
AudioContexts[i].CTX = NULL;
|
|
|
|
_snprintf(ErrorMsg, MsgSize, "Audio codec not found");
|
|
|
|
return NULL;
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (avcodec_open(AudioCodecContext, AudioCodec) < 0) {
|
2008-10-01 21:07:59 +02:00
|
|
|
av_free(AudioCodecContext);
|
|
|
|
AudioContexts[i].CTX = NULL;
|
|
|
|
_snprintf(ErrorMsg, MsgSize, "Could not open audio codec");
|
|
|
|
return NULL;
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
|
|
|
} else {
|
2008-09-24 00:00:22 +02:00
|
|
|
IndexMask &= ~(1 << i);
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
2008-09-04 00:26:27 +02:00
|
|
|
int64_t CurrentPos = _ftelli64(MC.ST.fp);
|
|
|
|
_fseeki64(MC.ST.fp, 0, SEEK_END);
|
|
|
|
int64_t SourceSize = _ftelli64(MC.ST.fp);
|
|
|
|
_fseeki64(MC.ST.fp, CurrentPos, SEEK_SET);
|
|
|
|
|
2009-05-22 23:28:02 +02:00
|
|
|
std::auto_ptr<FFIndex> TrackIndices(new FFIndex());
|
2008-09-04 00:26:27 +02:00
|
|
|
TrackIndices->Decoder = 1;
|
|
|
|
|
2008-09-01 23:16:13 +02:00
|
|
|
for (unsigned int i = 0; i < mkv_GetNumTracks(MF); i++)
|
2009-05-20 20:57:03 +02:00
|
|
|
TrackIndices->push_back(FFTrack(mkv_TruncFloat(mkv_GetTrackInfo(MF, i)->TimecodeScale), 1000000, HaaliTrackTypeToFFTrackType(mkv_GetTrackInfo(MF, i)->Type)));
|
2008-09-01 23:16:13 +02:00
|
|
|
|
2008-09-20 13:43:29 +02:00
|
|
|
ulonglong StartTime, EndTime, FilePos;
|
2008-09-01 23:16:13 +02:00
|
|
|
unsigned int Track, FrameFlags, FrameSize;
|
2009-04-11 20:45:40 +02:00
|
|
|
AVPacket TempPacket;
|
2009-05-03 21:25:54 +02:00
|
|
|
InitNullPacket(&TempPacket);
|
2008-09-01 23:16:13 +02:00
|
|
|
|
|
|
|
while (mkv_ReadFrame(MF, 0, &Track, &StartTime, &EndTime, &FilePos, &FrameSize, &FrameFlags) == 0) {
|
2008-09-04 00:26:27 +02:00
|
|
|
// Update progress
|
2009-05-20 20:57:03 +02:00
|
|
|
if (IC) {
|
2009-05-22 23:28:02 +02:00
|
|
|
if ((*IC)(_ftelli64(MC.ST.fp), SourceSize, ICPrivate)) {
|
2008-09-04 00:26:27 +02:00
|
|
|
_snprintf(ErrorMsg, MsgSize, "Cancelled by user");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2009-05-20 20:57:03 +02:00
|
|
|
|
2008-09-01 23:16:13 +02:00
|
|
|
// Only create index entries for video for now to save space
|
2008-09-27 03:21:57 +02:00
|
|
|
if (mkv_GetTrackInfo(MF, Track)->Type == TT_VIDEO) {
|
2009-05-20 20:57:03 +02:00
|
|
|
(*TrackIndices)[Track].push_back(TFrameInfo(StartTime, FilePos, FrameSize, (FrameFlags & FRAME_KF) != 0));
|
2008-09-27 03:21:57 +02:00
|
|
|
} else if (mkv_GetTrackInfo(MF, Track)->Type == TT_AUDIO && (IndexMask & (1 << Track))) {
|
2009-05-20 20:57:03 +02:00
|
|
|
(*TrackIndices)[Track].push_back(TFrameInfo(StartTime, AudioContexts[Track].CurrentSample, FilePos, FrameSize, (FrameFlags & FRAME_KF) != 0));
|
2008-09-27 03:21:57 +02:00
|
|
|
ReadFrame(FilePos, FrameSize, AudioContexts[Track].CS, MC, ErrorMsg, MsgSize);
|
2008-09-01 23:16:13 +02:00
|
|
|
AVCodecContext *AudioCodecContext = AudioContexts[Track].CTX;
|
2009-04-11 20:45:40 +02:00
|
|
|
TempPacket.data = MC.Buffer;
|
|
|
|
TempPacket.size = FrameSize;
|
2009-05-23 00:12:31 +02:00
|
|
|
if ((FrameFlags & FRAME_KF) != 0)
|
|
|
|
TempPacket.flags = PKT_FLAG_KEY;
|
2008-09-01 23:16:13 +02:00
|
|
|
|
2009-04-11 20:45:40 +02:00
|
|
|
while (TempPacket.size > 0) {
|
2008-09-01 23:16:13 +02:00
|
|
|
int dbsize = AVCODEC_MAX_AUDIO_FRAME_SIZE*10;
|
2009-05-22 23:28:02 +02:00
|
|
|
int Ret = avcodec_decode_audio3(AudioCodecContext, DecodingBuffer, &dbsize, &TempPacket);
|
2008-09-01 23:16:13 +02:00
|
|
|
if (Ret < 0) {
|
2008-09-24 16:11:45 +02:00
|
|
|
if (IgnoreDecodeErrors) {
|
|
|
|
(*TrackIndices)[Track].clear();
|
|
|
|
IndexMask &= ~(1 << Track);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
_snprintf(ErrorMsg, MsgSize, "Audio decoding error");
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Ret > 0) {
|
2009-04-11 20:45:40 +02:00
|
|
|
TempPacket.size -= Ret;
|
|
|
|
TempPacket.data += Ret;
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
2008-09-24 01:08:07 +02:00
|
|
|
|
|
|
|
if (dbsize > 0)
|
|
|
|
AudioContexts[Track].CurrentSample += (dbsize * 8) / (av_get_bits_per_sample_format(AudioCodecContext->sample_fmt) * AudioCodecContext->channels);
|
|
|
|
|
2009-05-22 23:28:02 +02:00
|
|
|
if (DumpMask & (1 << Track))
|
|
|
|
WriteAudio(AudioContexts[Track], TrackIndices.get(), Track, dbsize, ErrorMsg, MsgSize);
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-05-22 23:28:02 +02:00
|
|
|
SortTrackIndex(TrackIndices.get());
|
|
|
|
return TrackIndices.release();
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
|
|
|
|
2009-05-20 20:57:03 +02:00
|
|
|
FFIndexer *FFIndexer::CreateFFIndexer(const char *Filename, char *ErrorMsg, unsigned MsgSize) {
|
2008-09-01 23:16:13 +02:00
|
|
|
AVFormatContext *FormatContext = NULL;
|
|
|
|
|
2009-05-20 20:57:03 +02:00
|
|
|
if (av_open_input_file(&FormatContext, Filename, NULL, 0, NULL) != 0) {
|
|
|
|
_snprintf(ErrorMsg, MsgSize, "Can't open '%s'", Filename);
|
2008-09-04 00:26:27 +02:00
|
|
|
return NULL;
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Do matroska indexing instead?
|
|
|
|
if (!strcmp(FormatContext->iformat->name, "matroska")) {
|
|
|
|
av_close_input_file(FormatContext);
|
2009-05-20 20:57:03 +02:00
|
|
|
return new FFMatroskaIndexer(Filename, ErrorMsg, MsgSize);
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
2008-12-31 00:57:22 +01:00
|
|
|
|
2009-04-11 20:45:40 +02:00
|
|
|
#ifdef HAALISOURCE
|
2008-12-31 00:57:22 +01:00
|
|
|
// Do haali ts indexing instead?
|
2009-04-11 20:45:40 +02:00
|
|
|
if (!strcmp(FormatContext->iformat->name, "mpeg") || !strcmp(FormatContext->iformat->name, "mpegts")) {
|
|
|
|
av_close_input_file(FormatContext);
|
2009-05-20 20:57:03 +02:00
|
|
|
return new FFHaaliIndexer(Filename, 0, ErrorMsg, MsgSize);
|
2009-04-11 20:45:40 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (!strcmp(FormatContext->iformat->name, "ogg")) {
|
2008-12-31 00:57:22 +01:00
|
|
|
av_close_input_file(FormatContext);
|
2009-05-20 20:57:03 +02:00
|
|
|
return new FFHaaliIndexer(Filename, 1, ErrorMsg, MsgSize);
|
2008-12-31 00:57:22 +01:00
|
|
|
}
|
|
|
|
#endif
|
2009-05-20 20:57:03 +02:00
|
|
|
|
2009-05-22 23:28:02 +02:00
|
|
|
return new FFLAVFIndexer(Filename, FormatContext, ErrorMsg, MsgSize);
|
2009-05-20 20:57:03 +02:00
|
|
|
}
|
|
|
|
|
2009-05-22 23:28:02 +02:00
|
|
|
FFLAVFIndexer::FFLAVFIndexer(const char *Filename, AVFormatContext *FormatContext, char *ErrorMsg, unsigned MsgSize) {
|
|
|
|
SourceFile = Filename;
|
2009-05-20 20:57:03 +02:00
|
|
|
this->FormatContext = FormatContext;
|
2009-05-22 23:28:02 +02:00
|
|
|
IsIndexing = false;
|
2008-09-01 23:16:13 +02:00
|
|
|
|
|
|
|
if (av_find_stream_info(FormatContext) < 0) {
|
|
|
|
av_close_input_file(FormatContext);
|
|
|
|
_snprintf(ErrorMsg, MsgSize, "Couldn't find stream information");
|
2009-05-20 20:57:03 +02:00
|
|
|
throw ErrorMsg;
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
2009-05-20 20:57:03 +02:00
|
|
|
}
|
2008-09-01 23:16:13 +02:00
|
|
|
|
2009-05-20 20:57:03 +02:00
|
|
|
FFLAVFIndexer::~FFLAVFIndexer() {
|
|
|
|
if (!IsIndexing)
|
|
|
|
av_close_input_file(FormatContext);
|
|
|
|
}
|
2008-09-01 23:16:13 +02:00
|
|
|
|
2009-05-22 23:28:02 +02:00
|
|
|
FFIndex *FFLAVFIndexer::DoIndexing(char *ErrorMsg, unsigned MsgSize) {
|
2009-05-20 20:57:03 +02:00
|
|
|
IsIndexing = true;
|
|
|
|
|
|
|
|
// Audio stuff
|
2008-09-27 03:21:57 +02:00
|
|
|
FFAudioContext *AudioContexts;
|
2009-05-22 23:28:02 +02:00
|
|
|
FFIndexMemory IM = FFIndexMemory(FormatContext->nb_streams, AudioContexts, FormatContext);
|
2008-09-01 23:16:13 +02:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < FormatContext->nb_streams; i++) {
|
2008-09-24 00:00:22 +02:00
|
|
|
if (IndexMask & (1 << i) && FormatContext->streams[i]->codec->codec_type == CODEC_TYPE_AUDIO) {
|
2008-09-01 23:16:13 +02:00
|
|
|
AVCodecContext *AudioCodecContext = FormatContext->streams[i]->codec;
|
|
|
|
|
|
|
|
AVCodec *AudioCodec = avcodec_find_decoder(AudioCodecContext->codec_id);
|
|
|
|
if (AudioCodec == NULL) {
|
|
|
|
_snprintf(ErrorMsg, MsgSize, "Audio codec not found");
|
2008-09-04 00:26:27 +02:00
|
|
|
return NULL;
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (avcodec_open(AudioCodecContext, AudioCodec) < 0) {
|
|
|
|
_snprintf(ErrorMsg, MsgSize, "Could not open audio codec");
|
2008-09-04 00:26:27 +02:00
|
|
|
return NULL;
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
2008-10-01 21:07:59 +02:00
|
|
|
|
|
|
|
AudioContexts[i].CTX = AudioCodecContext;
|
2008-09-01 23:16:13 +02:00
|
|
|
} else {
|
2008-09-24 00:00:22 +02:00
|
|
|
IndexMask &= ~(1 << i);
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
2009-05-22 23:28:02 +02:00
|
|
|
std::auto_ptr<FFIndex> TrackIndices(new FFIndex());
|
2008-09-04 00:26:27 +02:00
|
|
|
TrackIndices->Decoder = 0;
|
|
|
|
|
2008-09-01 23:16:13 +02:00
|
|
|
for (unsigned int i = 0; i < FormatContext->nb_streams; i++)
|
2009-05-16 01:11:18 +02:00
|
|
|
TrackIndices->push_back(FFTrack((int64_t)FormatContext->streams[i]->time_base.num * 1000,
|
2008-09-23 23:56:30 +02:00
|
|
|
FormatContext->streams[i]->time_base.den,
|
2009-05-20 20:57:03 +02:00
|
|
|
static_cast<FFMS_TrackType>(FormatContext->streams[i]->codec->codec_type)));
|
2008-09-01 23:16:13 +02:00
|
|
|
|
2009-04-11 20:45:40 +02:00
|
|
|
AVPacket Packet, TempPacket;
|
2009-05-03 21:25:54 +02:00
|
|
|
InitNullPacket(&Packet);
|
|
|
|
InitNullPacket(&TempPacket);
|
2008-09-01 23:16:13 +02:00
|
|
|
while (av_read_frame(FormatContext, &Packet) >= 0) {
|
2008-09-04 00:26:27 +02:00
|
|
|
// Update progress
|
2009-05-20 20:57:03 +02:00
|
|
|
if (IC) {
|
2009-05-22 23:28:02 +02:00
|
|
|
if ((*IC)(FormatContext->pb->pos, FormatContext->file_size, ICPrivate)) {
|
2008-09-04 00:26:27 +02:00
|
|
|
_snprintf(ErrorMsg, MsgSize, "Cancelled by user");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-09-01 23:16:13 +02:00
|
|
|
// Only create index entries for video for now to save space
|
2008-09-27 03:21:57 +02:00
|
|
|
if (FormatContext->streams[Packet.stream_index]->codec->codec_type == CODEC_TYPE_VIDEO) {
|
2009-05-16 01:11:18 +02:00
|
|
|
(*TrackIndices)[Packet.stream_index].push_back(TFrameInfo(Packet.dts, (Packet.flags & PKT_FLAG_KEY) ? 1 : 0));
|
2008-09-27 03:21:57 +02:00
|
|
|
} else if (FormatContext->streams[Packet.stream_index]->codec->codec_type == CODEC_TYPE_AUDIO && (IndexMask & (1 << Packet.stream_index))) {
|
2009-05-16 01:11:18 +02:00
|
|
|
(*TrackIndices)[Packet.stream_index].push_back(TFrameInfo(Packet.dts, AudioContexts[Packet.stream_index].CurrentSample, (Packet.flags & PKT_FLAG_KEY) ? 1 : 0));
|
2008-09-27 03:21:57 +02:00
|
|
|
AVCodecContext *AudioCodecContext = FormatContext->streams[Packet.stream_index]->codec;
|
2009-04-11 20:45:40 +02:00
|
|
|
TempPacket.data = Packet.data;
|
|
|
|
TempPacket.size = Packet.size;
|
2008-09-01 23:16:13 +02:00
|
|
|
|
2009-04-11 20:45:40 +02:00
|
|
|
while (TempPacket.size > 0) {
|
2008-09-27 03:21:57 +02:00
|
|
|
int dbsize = AVCODEC_MAX_AUDIO_FRAME_SIZE*10;
|
2009-05-22 23:28:02 +02:00
|
|
|
int Ret = avcodec_decode_audio3(AudioCodecContext, DecodingBuffer, &dbsize, &TempPacket);
|
2008-09-27 03:21:57 +02:00
|
|
|
if (Ret < 0) {
|
|
|
|
if (IgnoreDecodeErrors) {
|
|
|
|
(*TrackIndices)[Packet.stream_index].clear();
|
|
|
|
IndexMask &= ~(1 << Packet.stream_index);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
_snprintf(ErrorMsg, MsgSize, "Audio decoding error");
|
|
|
|
return NULL;
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
2008-09-27 03:21:57 +02:00
|
|
|
}
|
2008-09-01 23:16:13 +02:00
|
|
|
|
2008-09-27 03:21:57 +02:00
|
|
|
if (Ret > 0) {
|
2009-04-11 20:45:40 +02:00
|
|
|
TempPacket.size -= Ret;
|
|
|
|
TempPacket.data += Ret;
|
2008-09-27 03:21:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dbsize > 0)
|
|
|
|
AudioContexts[Packet.stream_index].CurrentSample += (dbsize * 8) / (av_get_bits_per_sample_format(AudioCodecContext->sample_fmt) * AudioCodecContext->channels);
|
2008-09-01 23:16:13 +02:00
|
|
|
|
2009-05-22 23:28:02 +02:00
|
|
|
if (DumpMask & (1 << Packet.stream_index))
|
|
|
|
WriteAudio(AudioContexts[Packet.stream_index], TrackIndices.get(), Packet.stream_index, dbsize, ErrorMsg, MsgSize);
|
2008-09-27 03:21:57 +02:00
|
|
|
}
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
av_free_packet(&Packet);
|
|
|
|
}
|
|
|
|
|
2009-05-22 23:28:02 +02:00
|
|
|
SortTrackIndex(TrackIndices.get());
|
|
|
|
return TrackIndices.release();
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|