2008-09-01 23:16:13 +02:00
|
|
|
// Copyright (c) 2007-2008 Fredrik Mellbin
|
|
|
|
//
|
|
|
|
// 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"
|
|
|
|
#include "wave64writer.h"
|
|
|
|
|
2008-09-27 03:21:57 +02:00
|
|
|
class MatroskaAudioContext {
|
2008-09-01 23:16:13 +02:00
|
|
|
public:
|
|
|
|
Wave64Writer *W64W;
|
|
|
|
AVCodecContext *CTX;
|
|
|
|
CompressedStream *CS;
|
2008-09-19 23:35:46 +02:00
|
|
|
int64_t CurrentSample;
|
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
|
|
|
W64W = NULL;
|
|
|
|
CTX = NULL;
|
|
|
|
CS = NULL;
|
2008-09-19 23:35:46 +02:00
|
|
|
CurrentSample = 0;
|
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
|
|
|
delete W64W;
|
|
|
|
if (CTX) {
|
|
|
|
avcodec_close(CTX);
|
|
|
|
av_free(CTX);
|
|
|
|
}
|
|
|
|
if (CS)
|
|
|
|
cs_Destroy(CS);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-09-27 03:21:57 +02:00
|
|
|
class FFAudioContext {
|
|
|
|
public:
|
|
|
|
Wave64Writer *W64W;
|
|
|
|
AVCodecContext *CTX;
|
|
|
|
int64_t CurrentSample;
|
|
|
|
|
|
|
|
FFAudioContext() {
|
|
|
|
W64W = NULL;
|
2008-10-01 21:07:59 +02:00
|
|
|
CTX = NULL;
|
2008-09-27 03:21:57 +02:00
|
|
|
CurrentSample = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
~FFAudioContext() {
|
|
|
|
delete W64W;
|
2008-10-01 21:29:42 +02:00
|
|
|
// FIXME, why is there an access violation here?
|
|
|
|
// if (CTX)
|
|
|
|
// avcodec_close(CTX);
|
2008-09-27 03:21:57 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class MatroskaIndexMemory {
|
2008-09-04 00:26:27 +02:00
|
|
|
private:
|
|
|
|
int16_t *DecodingBuffer;
|
2008-09-27 03:21:57 +02:00
|
|
|
MatroskaAudioContext *AudioContexts;
|
2008-09-04 00:26:27 +02:00
|
|
|
public:
|
2008-09-27 03:21:57 +02:00
|
|
|
MatroskaIndexMemory(int Tracks, int16_t *&DecodingBuffer, MatroskaAudioContext *&AudioContexts) {
|
2008-09-04 00:26:27 +02:00
|
|
|
DecodingBuffer = new int16_t[AVCODEC_MAX_AUDIO_FRAME_SIZE*10];
|
2008-09-27 03:21:57 +02:00
|
|
|
AudioContexts = new MatroskaAudioContext[Tracks];
|
2008-09-04 00:26:27 +02:00
|
|
|
this->DecodingBuffer = DecodingBuffer;
|
|
|
|
this->AudioContexts = AudioContexts;
|
|
|
|
}
|
|
|
|
|
2008-09-27 03:21:57 +02:00
|
|
|
~MatroskaIndexMemory() {
|
|
|
|
delete [] DecodingBuffer;
|
|
|
|
delete [] AudioContexts;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class FFIndexMemory {
|
|
|
|
private:
|
|
|
|
int16_t *DecodingBuffer;
|
|
|
|
FFAudioContext *AudioContexts;
|
|
|
|
public:
|
|
|
|
FFIndexMemory(int Tracks, int16_t *&DecodingBuffer, FFAudioContext *&AudioContexts) {
|
|
|
|
DecodingBuffer = new int16_t[AVCODEC_MAX_AUDIO_FRAME_SIZE*10];
|
|
|
|
AudioContexts = new FFAudioContext[Tracks];
|
|
|
|
this->DecodingBuffer = DecodingBuffer;
|
|
|
|
this->AudioContexts = AudioContexts;
|
|
|
|
}
|
|
|
|
|
|
|
|
~FFIndexMemory() {
|
2008-09-04 00:26:27 +02:00
|
|
|
delete [] DecodingBuffer;
|
|
|
|
delete [] AudioContexts;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class MatroskaMemory {
|
|
|
|
private:
|
|
|
|
MatroskaFile *MF;
|
|
|
|
MatroskaReaderContext *MC;
|
|
|
|
public:
|
|
|
|
MatroskaMemory(MatroskaFile *MF, MatroskaReaderContext *MC) {
|
|
|
|
this->MF = MF;
|
|
|
|
this->MC = MC;
|
|
|
|
}
|
|
|
|
|
|
|
|
~MatroskaMemory() {
|
|
|
|
mkv_Close(MF);
|
|
|
|
fclose(MC->ST.fp);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2008-09-01 23:16:13 +02:00
|
|
|
static bool DTSComparison(FrameInfo FI1, FrameInfo FI2) {
|
|
|
|
return FI1.DTS < FI2.DTS;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void SortTrackIndices(FrameIndex *TrackIndices) {
|
|
|
|
for (FrameIndex::iterator Cur=TrackIndices->begin(); Cur!=TrackIndices->end(); Cur++)
|
|
|
|
std::sort(Cur->begin(), Cur->end(), DTSComparison);
|
|
|
|
}
|
|
|
|
|
|
|
|
int WriteIndex(const char *IndexFile, FrameIndex *TrackIndices, char *ErrorMsg, unsigned MsgSize) {
|
|
|
|
std::ofstream Index(IndexFile, std::ios::out | std::ios::binary | std::ios::trunc);
|
|
|
|
|
|
|
|
if (!Index.is_open()) {
|
|
|
|
_snprintf(ErrorMsg, MsgSize, "Failed to open '%s' for writing", IndexFile);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write the index file header
|
|
|
|
IndexHeader IH;
|
|
|
|
IH.Id = INDEXID;
|
|
|
|
IH.Version = INDEXVERSION;
|
|
|
|
IH.Tracks = TrackIndices->size();
|
|
|
|
IH.Decoder = TrackIndices->Decoder;
|
|
|
|
Index.write(reinterpret_cast<char *>(&IH), sizeof(IH));
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < IH.Tracks; i++) {
|
2008-09-23 23:56:30 +02:00
|
|
|
int TT = (*TrackIndices)[i].TT;
|
|
|
|
Index.write(reinterpret_cast<char *>(&TT), sizeof(TT));
|
2008-09-01 23:16:13 +02:00
|
|
|
int Num = (*TrackIndices)[i].TB.Num;
|
|
|
|
Index.write(reinterpret_cast<char *>(&Num), sizeof(Num));
|
|
|
|
int Den = (*TrackIndices)[i].TB.Den;
|
|
|
|
Index.write(reinterpret_cast<char *>(&Den), sizeof(Den));
|
2008-09-23 23:56:30 +02:00
|
|
|
size_t Frames = (*TrackIndices)[i].size();
|
|
|
|
Index.write(reinterpret_cast<char *>(&Frames), sizeof(Frames));
|
2008-09-01 23:16:13 +02:00
|
|
|
|
|
|
|
for (size_t j = 0; j < Frames; j++)
|
|
|
|
Index.write(reinterpret_cast<char *>(&(TrackIndices->at(i)[j])), sizeof(FrameInfo));
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-09-24 16:11:45 +02:00
|
|
|
static FrameIndex *MakeMatroskaIndex(const char *SourceFile, int IndexMask, int DumpMask, const char *AudioFile, bool IgnoreDecodeErrors, IndexCallback IP, void *Private, char *ErrorMsg, unsigned MsgSize) {
|
2008-09-01 23:16:13 +02:00
|
|
|
MatroskaFile *MF;
|
|
|
|
char ErrorMessage[256];
|
|
|
|
MatroskaReaderContext MC;
|
|
|
|
MC.Buffer = NULL;
|
|
|
|
MC.BufferSize = 0;
|
|
|
|
|
|
|
|
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));
|
2008-09-04 00:26:27 +02:00
|
|
|
return NULL;
|
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);
|
2008-09-04 00:26:27 +02:00
|
|
|
return NULL;
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
|
|
|
|
2008-09-04 00:26:27 +02:00
|
|
|
MatroskaMemory MM = MatroskaMemory(MF, &MC);
|
|
|
|
|
2008-09-01 23:16:13 +02:00
|
|
|
// Audio stuff
|
|
|
|
|
2008-09-04 00:26:27 +02:00
|
|
|
int16_t *db;
|
2008-09-27 03:21:57 +02:00
|
|
|
MatroskaAudioContext *AudioContexts;
|
|
|
|
MatroskaIndexMemory IM = MatroskaIndexMemory(mkv_GetNumTracks(MF), db, AudioContexts);
|
2008-09-01 23:16:13 +02:00
|
|
|
|
|
|
|
for (unsigned int i = 0; i < mkv_GetNumTracks(MF); i++) {
|
2008-09-24 00:00:22 +02:00
|
|
|
if (IndexMask & (1 << i) && mkv_GetTrackInfo(MF, i)->Type == TT_AUDIO) {
|
2008-09-01 23:16:13 +02:00
|
|
|
AVCodecContext *AudioCodecContext = avcodec_alloc_context();
|
|
|
|
AudioCodecContext->extradata = (uint8_t *)mkv_GetTrackInfo(MF, i)->CodecPrivate;
|
|
|
|
AudioCodecContext->extradata_size = mkv_GetTrackInfo(MF, i)->CodecPrivateSize;
|
|
|
|
AudioContexts[i].CTX = AudioCodecContext;
|
|
|
|
|
|
|
|
if (mkv_GetTrackInfo(MF, i)->CompEnabled) {
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
AVCodec *AudioCodec = avcodec_find_decoder(MatroskaToFFCodecID(mkv_GetTrackInfo(MF, i)));
|
|
|
|
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);
|
|
|
|
|
|
|
|
FrameIndex *TrackIndices = new FrameIndex();
|
|
|
|
TrackIndices->Decoder = 1;
|
|
|
|
|
2008-09-01 23:16:13 +02:00
|
|
|
for (unsigned int i = 0; i < mkv_GetNumTracks(MF); i++)
|
2008-09-23 23:56:30 +02:00
|
|
|
TrackIndices->push_back(FrameInfoVector(mkv_TruncFloat(mkv_GetTrackInfo(MF, i)->TimecodeScale), 1000000, mkv_GetTrackInfo(MF, i)->Type - 1));
|
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;
|
|
|
|
|
|
|
|
while (mkv_ReadFrame(MF, 0, &Track, &StartTime, &EndTime, &FilePos, &FrameSize, &FrameFlags) == 0) {
|
2008-09-04 00:26:27 +02:00
|
|
|
// Update progress
|
|
|
|
if (IP) {
|
|
|
|
if ((*IP)(0, _ftelli64(MC.ST.fp), SourceSize, Private)) {
|
|
|
|
_snprintf(ErrorMsg, MsgSize, "Cancelled by user");
|
2008-09-19 23:35:46 +02:00
|
|
|
delete TrackIndices;
|
2008-09-04 00:26:27 +02:00
|
|
|
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 (mkv_GetTrackInfo(MF, Track)->Type == TT_VIDEO) {
|
2008-09-01 23:16:13 +02:00
|
|
|
(*TrackIndices)[Track].push_back(FrameInfo(StartTime, (FrameFlags & FRAME_KF) != 0));
|
2008-09-27 03:21:57 +02:00
|
|
|
} else if (mkv_GetTrackInfo(MF, Track)->Type == TT_AUDIO && (IndexMask & (1 << Track))) {
|
2008-09-19 23:35:46 +02:00
|
|
|
(*TrackIndices)[Track].push_back(FrameInfo(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
|
|
|
|
|
|
|
int Size = FrameSize;
|
|
|
|
uint8_t *Data = MC.Buffer;
|
|
|
|
AVCodecContext *AudioCodecContext = AudioContexts[Track].CTX;
|
|
|
|
|
|
|
|
while (Size > 0) {
|
|
|
|
int dbsize = AVCODEC_MAX_AUDIO_FRAME_SIZE*10;
|
|
|
|
int Ret = avcodec_decode_audio2(AudioCodecContext, db, &dbsize, Data, Size);
|
|
|
|
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");
|
|
|
|
delete TrackIndices;
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (Ret > 0) {
|
|
|
|
Size -= Ret;
|
|
|
|
Data += Ret;
|
|
|
|
}
|
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);
|
|
|
|
|
2008-09-24 00:00:22 +02:00
|
|
|
if (dbsize > 0 && (DumpMask & (1 << Track))) {
|
2008-09-01 23:16:13 +02:00
|
|
|
// Delay writer creation until after an audio frame has been decoded. This ensures that all parameters are known when writing the headers.
|
|
|
|
if (!AudioContexts[Track].W64W) {
|
|
|
|
char ABuf[50];
|
|
|
|
std::string WN(AudioFile);
|
|
|
|
int Offset = StartTime * mkv_TruncFloat(mkv_GetTrackInfo(MF, Track)->TimecodeScale) / (double)1000000;
|
|
|
|
_snprintf(ABuf, sizeof(ABuf), ".%02d.delay.%d.w64", Track, Offset);
|
|
|
|
WN += ABuf;
|
|
|
|
|
|
|
|
AudioContexts[Track].W64W = new Wave64Writer(WN.c_str(), av_get_bits_per_sample_format(AudioCodecContext->sample_fmt),
|
|
|
|
AudioCodecContext->channels, AudioCodecContext->sample_rate, AudioFMTIsFloat(AudioCodecContext->sample_fmt));
|
|
|
|
}
|
|
|
|
|
|
|
|
AudioContexts[Track].W64W->WriteData(db, dbsize);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SortTrackIndices(TrackIndices);
|
2008-09-04 00:26:27 +02:00
|
|
|
return TrackIndices;
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
|
|
|
|
2008-09-24 16:11:45 +02:00
|
|
|
FrameIndex *MakeIndex(const char *SourceFile, int IndexMask, int DumpMask, const char *AudioFile, bool IgnoreDecodeErrors, IndexCallback IP, void *Private, char *ErrorMsg, unsigned MsgSize) {
|
2008-09-01 23:16:13 +02:00
|
|
|
AVFormatContext *FormatContext = NULL;
|
2008-09-24 00:00:22 +02:00
|
|
|
IndexMask |= DumpMask;
|
2008-09-01 23:16:13 +02:00
|
|
|
|
|
|
|
if (av_open_input_file(&FormatContext, SourceFile, NULL, 0, NULL) != 0) {
|
|
|
|
_snprintf(ErrorMsg, MsgSize, "Can't open '%s'", SourceFile);
|
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);
|
2008-09-24 16:11:45 +02:00
|
|
|
return MakeMatroskaIndex(SourceFile, IndexMask, DumpMask, AudioFile, IgnoreDecodeErrors, IP, Private, ErrorMsg, MsgSize);
|
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");
|
2008-09-04 00:26:27 +02:00
|
|
|
return NULL;
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Audio stuff
|
|
|
|
|
2008-09-04 00:26:27 +02:00
|
|
|
int16_t *db;
|
2008-09-27 03:21:57 +02:00
|
|
|
FFAudioContext *AudioContexts;
|
|
|
|
FFIndexMemory IM = FFIndexMemory(FormatContext->nb_streams, db, AudioContexts);
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
|
2008-09-04 00:26:27 +02:00
|
|
|
FrameIndex *TrackIndices = new FrameIndex();
|
|
|
|
TrackIndices->Decoder = 0;
|
|
|
|
|
2008-09-01 23:16:13 +02:00
|
|
|
for (unsigned int i = 0; i < FormatContext->nb_streams; i++)
|
2008-09-08 18:11:09 +02:00
|
|
|
TrackIndices->push_back(FrameInfoVector(FormatContext->streams[i]->time_base.num * 1000,
|
2008-09-23 23:56:30 +02:00
|
|
|
FormatContext->streams[i]->time_base.den,
|
|
|
|
FormatContext->streams[i]->codec->codec_type));
|
2008-09-01 23:16:13 +02:00
|
|
|
|
|
|
|
AVPacket Packet;
|
|
|
|
while (av_read_frame(FormatContext, &Packet) >= 0) {
|
2008-09-04 00:26:27 +02:00
|
|
|
// Update progress
|
|
|
|
if (IP) {
|
|
|
|
if ((*IP)(0, FormatContext->pb->pos, FormatContext->file_size, Private)) {
|
|
|
|
_snprintf(ErrorMsg, MsgSize, "Cancelled by user");
|
2008-09-19 23:35:46 +02:00
|
|
|
delete TrackIndices;
|
2008-09-04 00:26:27 +02:00
|
|
|
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) {
|
2008-09-01 23:16:13 +02:00
|
|
|
(*TrackIndices)[Packet.stream_index].push_back(FrameInfo(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))) {
|
|
|
|
(*TrackIndices)[Packet.stream_index].push_back(FrameInfo(Packet.dts, AudioContexts[Packet.stream_index].CurrentSample, (Packet.flags & PKT_FLAG_KEY) ? 1 : 0));
|
|
|
|
AVCodecContext *AudioCodecContext = FormatContext->streams[Packet.stream_index]->codec;
|
|
|
|
int Size = Packet.size;
|
|
|
|
uint8_t *Data = Packet.data;
|
2008-09-01 23:16:13 +02:00
|
|
|
|
2008-09-27 03:21:57 +02:00
|
|
|
while (Size > 0) {
|
|
|
|
int dbsize = AVCODEC_MAX_AUDIO_FRAME_SIZE*10;
|
|
|
|
int Ret = avcodec_decode_audio2(AudioCodecContext, db, &dbsize, Data, Size);
|
|
|
|
if (Ret < 0) {
|
|
|
|
if (IgnoreDecodeErrors) {
|
|
|
|
(*TrackIndices)[Packet.stream_index].clear();
|
|
|
|
IndexMask &= ~(1 << Packet.stream_index);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
_snprintf(ErrorMsg, MsgSize, "Audio decoding error");
|
|
|
|
delete TrackIndices;
|
|
|
|
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) {
|
|
|
|
Size -= Ret;
|
|
|
|
Data += Ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2008-09-27 03:21:57 +02:00
|
|
|
if (dbsize > 0 && (DumpMask & (1 << Packet.stream_index))) {
|
|
|
|
// Delay writer creation until after an audio frame has been decoded. This ensures that all parameters are known when writing the headers.
|
|
|
|
if (!AudioContexts[Packet.stream_index].W64W) {
|
|
|
|
char ABuf[50];
|
|
|
|
std::string WN(AudioFile);
|
|
|
|
int Offset = (Packet.dts * FormatContext->streams[Packet.stream_index]->time_base.num)
|
|
|
|
/ (double)(FormatContext->streams[Packet.stream_index]->time_base.den * 1000);
|
|
|
|
_snprintf(ABuf, sizeof(ABuf), ".%02d.delay.%d.w64", Packet.stream_index, Offset);
|
|
|
|
WN += ABuf;
|
|
|
|
|
|
|
|
AudioContexts[Packet.stream_index].W64W = new Wave64Writer(WN.c_str(), av_get_bits_per_sample_format(AudioCodecContext->sample_fmt),
|
|
|
|
AudioCodecContext->channels, AudioCodecContext->sample_rate, AudioFMTIsFloat(AudioCodecContext->sample_fmt));
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
2008-09-27 03:21:57 +02:00
|
|
|
|
|
|
|
AudioContexts[Packet.stream_index].W64W->WriteData(db, dbsize);
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
2008-09-27 03:21:57 +02:00
|
|
|
}
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
av_free_packet(&Packet);
|
|
|
|
}
|
|
|
|
|
|
|
|
av_close_input_file(FormatContext);
|
|
|
|
|
|
|
|
SortTrackIndices(TrackIndices);
|
2008-09-04 00:26:27 +02:00
|
|
|
return TrackIndices;
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
|
|
|
|
2008-09-04 00:26:27 +02:00
|
|
|
FrameIndex *ReadIndex(const char *IndexFile, char *ErrorMsg, unsigned MsgSize) {
|
2008-09-01 23:16:13 +02:00
|
|
|
std::ifstream Index(IndexFile, std::ios::in | std::ios::binary);
|
|
|
|
|
|
|
|
if (!Index.is_open()) {
|
|
|
|
_snprintf(ErrorMsg, MsgSize, "Failed to open '%s' for reading", IndexFile);
|
2008-09-04 00:26:27 +02:00
|
|
|
return NULL;
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Read the index file header
|
|
|
|
IndexHeader IH;
|
|
|
|
Index.read(reinterpret_cast<char *>(&IH), sizeof(IH));
|
|
|
|
if (IH.Id != INDEXID) {
|
|
|
|
_snprintf(ErrorMsg, MsgSize, "'%s' is not a valid index file", IndexFile);
|
2008-09-04 00:26:27 +02:00
|
|
|
return NULL;
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (IH.Version != INDEXVERSION) {
|
|
|
|
_snprintf(ErrorMsg, MsgSize, "'%s' is not the expected index version", IndexFile);
|
2008-09-04 00:26:27 +02:00
|
|
|
return NULL;
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
|
|
|
|
2008-09-04 00:26:27 +02:00
|
|
|
FrameIndex *TrackIndices = new FrameIndex();
|
2008-09-01 23:16:13 +02:00
|
|
|
|
2008-09-04 00:26:27 +02:00
|
|
|
try {
|
|
|
|
|
|
|
|
TrackIndices->Decoder = IH.Decoder;
|
|
|
|
|
|
|
|
for (unsigned int i = 0; i < IH.Tracks; i++) {
|
|
|
|
// Read how many records belong to the current stream
|
2008-09-23 23:56:30 +02:00
|
|
|
int TT;
|
|
|
|
Index.read(reinterpret_cast<char *>(&TT), sizeof(TT));
|
2008-09-04 00:26:27 +02:00
|
|
|
int Num;
|
|
|
|
Index.read(reinterpret_cast<char *>(&Num), sizeof(Num));
|
|
|
|
int Den;
|
|
|
|
Index.read(reinterpret_cast<char *>(&Den), sizeof(Den));
|
2008-09-23 23:56:30 +02:00
|
|
|
size_t Frames;
|
|
|
|
Index.read(reinterpret_cast<char *>(&Frames), sizeof(Frames));
|
|
|
|
TrackIndices->push_back(FrameInfoVector(Num, Den, TT));
|
2008-09-04 00:26:27 +02:00
|
|
|
|
|
|
|
FrameInfo FI(0, false);
|
|
|
|
for (size_t j = 0; j < Frames; j++) {
|
|
|
|
Index.read(reinterpret_cast<char *>(&FI), sizeof(FrameInfo));
|
|
|
|
TrackIndices->at(i).push_back(FI);
|
|
|
|
}
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
|
|
|
|
2008-09-04 00:26:27 +02:00
|
|
|
} catch (...) {
|
|
|
|
delete TrackIndices;
|
|
|
|
_snprintf(ErrorMsg, MsgSize, "Unknown error while reading index information in '%s'", IndexFile);
|
|
|
|
return NULL;
|
|
|
|
}
|
2008-09-01 23:16:13 +02:00
|
|
|
|
2008-09-04 00:26:27 +02:00
|
|
|
return TrackIndices;
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int FrameInfoVector::WriteTimecodes(const char *TimecodeFile, char *ErrorMsg, unsigned MsgSize) {
|
|
|
|
std::ofstream Timecodes(TimecodeFile, std::ios::out | std::ios::trunc);
|
|
|
|
|
|
|
|
if (!Timecodes.is_open()) {
|
|
|
|
_snprintf(ErrorMsg, MsgSize, "Failed to open '%s' for writing", TimecodeFile);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-09-08 18:11:09 +02:00
|
|
|
Timecodes << "# timecode format v2\n";
|
2008-09-01 23:16:13 +02:00
|
|
|
|
|
|
|
for (iterator Cur=begin(); Cur!=end(); Cur++)
|
2008-09-08 18:11:09 +02:00
|
|
|
Timecodes << (int64_t)((Cur->DTS * TB.Num) / (double)TB.Den) << "\n";
|
2008-09-01 23:16:13 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int FrameInfoVector::FrameFromDTS(int64_t DTS) {
|
|
|
|
for (int i = 0; i < static_cast<int>(size()); i++)
|
|
|
|
if (at(i).DTS == DTS)
|
|
|
|
return i;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int FrameInfoVector::ClosestFrameFromDTS(int64_t DTS) {
|
|
|
|
int Frame = 0;
|
|
|
|
int64_t BestDiff = 0xFFFFFFFFFFFFFFLL; // big number
|
|
|
|
for (int i = 0; i < static_cast<int>(size()); i++) {
|
|
|
|
int64_t CurrentDiff = FFABS(at(i).DTS - DTS);
|
|
|
|
if (CurrentDiff < BestDiff) {
|
|
|
|
BestDiff = CurrentDiff;
|
|
|
|
Frame = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Frame;
|
|
|
|
}
|
|
|
|
|
|
|
|
int FrameInfoVector::FindClosestKeyFrame(int Frame) {
|
2008-09-04 00:26:27 +02:00
|
|
|
Frame = FFMIN(FFMAX(Frame, 0), size() - 1);
|
2008-09-01 23:16:13 +02:00
|
|
|
for (int i = Frame; i > 0; i--)
|
|
|
|
if (at(i).KeyFrame)
|
|
|
|
return i;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
FrameInfoVector::FrameInfoVector() {
|
2008-09-27 03:21:57 +02:00
|
|
|
this->TT = 0;
|
|
|
|
this->TB.Num = 0;
|
|
|
|
this->TB.Den = 0;
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|
|
|
|
|
2008-09-23 23:56:30 +02:00
|
|
|
FrameInfoVector::FrameInfoVector(int Num, int Den, int TT) {
|
|
|
|
this->TT = TT;
|
2008-09-27 03:21:57 +02:00
|
|
|
this->TB.Num = Num;
|
|
|
|
this->TB.Den = Den;
|
2008-09-01 23:16:13 +02:00
|
|
|
}
|