2007-07-18 15:00:15 +02:00
|
|
|
#include "ffmpegsource.h"
|
2007-06-17 18:35:16 +02:00
|
|
|
#include "stdiostream.c"
|
2007-07-02 23:36:21 +02:00
|
|
|
#include "matroskacodecs.c"
|
2007-06-16 20:45:56 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
class FFMatroskaSource : public FFBase {
|
2007-06-16 20:45:56 +02:00
|
|
|
private:
|
2007-07-18 15:00:15 +02:00
|
|
|
StdIoStream ST;
|
|
|
|
unsigned int BufferSize;
|
|
|
|
CompressedStream *VideoCS;
|
|
|
|
CompressedStream *AudioCS;
|
2007-07-02 23:36:21 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
AVCodecContext *VideoCodecContext;
|
2007-06-16 20:45:56 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
MatroskaFile *MF;
|
|
|
|
char ErrorMessage[256];
|
|
|
|
uint8_t *Buffer;
|
2007-06-16 20:45:56 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
int CurrentFrame;
|
2007-07-02 23:36:21 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
int ReadFrame(uint64_t AFilePos, unsigned int AFrameSize, CompressedStream *ACS, IScriptEnvironment *Env);
|
|
|
|
int DecodeNextFrame(AVFrame *AFrame, int64_t *AFirstStartTime, IScriptEnvironment* Env);
|
2007-06-16 20:45:56 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
int GetTrackIndex(int Index, unsigned char ATrackType, IScriptEnvironment *Env) {
|
|
|
|
if (Index == -1)
|
|
|
|
for (unsigned int i = 0; i < mkv_GetNumTracks(MF); i++)
|
|
|
|
if (mkv_GetTrackInfo(MF, i)->Type == ATrackType) {
|
|
|
|
Index = i;
|
|
|
|
break;
|
|
|
|
}
|
2007-06-16 20:45:56 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
if (Index == -1)
|
|
|
|
Env->ThrowError("FFmpegSource: No %s track found", (ATrackType & TT_VIDEO) ? "video" : "audio");
|
|
|
|
if (Index <= -2)
|
|
|
|
return -2;
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
if (Index >= (int)mkv_GetNumTracks(MF))
|
|
|
|
Env->ThrowError("FFmpegSource: Invalid %s track number", (ATrackType & TT_VIDEO) ? "video" : "audio");
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
TrackInfo *TI = mkv_GetTrackInfo(MF, Index);
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
if (TI->Type != ATrackType)
|
|
|
|
Env->ThrowError("FFmpegSource: Selected track is not %s", (ATrackType & TT_VIDEO) ? "video" : "audio");
|
2007-07-13 07:15:28 +02:00
|
|
|
|
|
|
|
return Index;
|
|
|
|
}
|
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
public:
|
|
|
|
FFMatroskaSource(const char *ASource, int AVideoTrack, int AAudioTrack, const char *ATimecodes, bool AVCache, const char *AVideoCache, const char *AAudioCache, const char *APPString, int AQuality, IScriptEnvironment* Env) {
|
|
|
|
CurrentFrame = 0;
|
|
|
|
int VideoTrack;
|
|
|
|
int AudioTrack;
|
|
|
|
unsigned int TrackMask = ~0;
|
|
|
|
AVCodecContext *AudioCodecContext = NULL;
|
|
|
|
AVCodec *AudioCodec = NULL;
|
|
|
|
VideoCodecContext = NULL;
|
|
|
|
AVCodec *VideoCodec = NULL;
|
|
|
|
TrackInfo *VideoTI = NULL;
|
2007-06-16 20:45:56 +02:00
|
|
|
BufferSize = 0;
|
|
|
|
Buffer = NULL;
|
2007-07-18 15:00:15 +02:00
|
|
|
VideoCS = NULL;
|
|
|
|
AudioCS = NULL;
|
2007-06-16 20:45:56 +02:00
|
|
|
|
|
|
|
memset(&ST,0,sizeof(ST));
|
|
|
|
ST.base.read = (int (__cdecl *)(InputStream *,ulonglong,void *,int))StdIoRead;
|
|
|
|
ST.base.scan = (longlong (__cdecl *)(InputStream *,ulonglong,unsigned int))StdIoScan;
|
|
|
|
ST.base.getcachesize = (unsigned int (__cdecl *)(InputStream *))StdIoGetCacheSize;
|
|
|
|
ST.base.geterror = (const char *(__cdecl *)(InputStream *))StdIoGetLastError;
|
|
|
|
ST.base.memalloc = (void *(__cdecl *)(InputStream *,size_t))StdIoMalloc;
|
|
|
|
ST.base.memrealloc = (void *(__cdecl *)(InputStream *,void *,size_t))StdIoRealloc;
|
|
|
|
ST.base.memfree = (void (__cdecl *)(InputStream *,void *)) StdIoFree;
|
|
|
|
ST.base.progress = (int (__cdecl *)(InputStream *,ulonglong,ulonglong))StdIoProgress;
|
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
ST.fp = fopen(ASource, "rb");
|
2007-06-16 20:45:56 +02:00
|
|
|
if (ST.fp == NULL)
|
2007-07-18 15:00:15 +02:00
|
|
|
Env->ThrowError("FFmpegSource: Can't open '%s': %s", ASource, strerror(errno));
|
2007-06-16 20:45:56 +02:00
|
|
|
|
|
|
|
setvbuf(ST.fp, NULL, _IOFBF, CACHESIZE);
|
|
|
|
|
|
|
|
MF = mkv_OpenEx(&ST.base, 0, 0, ErrorMessage, sizeof(ErrorMessage));
|
|
|
|
if (MF == NULL) {
|
|
|
|
fclose(ST.fp);
|
2007-07-02 23:36:21 +02:00
|
|
|
Env->ThrowError("FFmpegSource: Can't parse Matroska file: %s", ErrorMessage);
|
2007-06-16 20:45:56 +02:00
|
|
|
}
|
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
VideoTrack = GetTrackIndex(AVideoTrack, TT_VIDEO, Env);
|
|
|
|
AudioTrack = GetTrackIndex(AAudioTrack, TT_AUDIO, Env);
|
2007-06-16 20:45:56 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
bool VCacheIsValid = true;
|
|
|
|
bool ACacheIsValid = true;
|
2007-06-16 20:45:56 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
if (VideoTrack >= 0) {
|
|
|
|
VCacheIsValid = LoadFrameInfoFromFile(AVideoCache, ASource, VideoTrack);
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
VideoTI = mkv_GetTrackInfo(MF, VideoTrack);
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
if (VideoTI->CompEnabled) {
|
|
|
|
VideoCS = cs_Create(MF, VideoTrack, ErrorMessage, sizeof(ErrorMessage));
|
|
|
|
if (VideoCS == NULL)
|
|
|
|
Env->ThrowError("FFmpegSource: Can't create decompressor: %s", ErrorMessage);
|
|
|
|
}
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
VideoCodecContext = avcodec_alloc_context();
|
|
|
|
VideoCodecContext->extradata = (uint8_t *)VideoTI->CodecPrivate;
|
|
|
|
VideoCodecContext->extradata_size = VideoTI->CodecPrivateSize;
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
VideoCodec = avcodec_find_decoder(MatroskaToFFCodecID(VideoTI));
|
|
|
|
if (VideoCodec == NULL)
|
|
|
|
Env->ThrowError("FFmpegSource: Video codec not found");
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
if (avcodec_open(VideoCodecContext, VideoCodec) < 0)
|
|
|
|
Env->ThrowError("FFmpegSource: Could not open video codec");
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-29 22:49:50 +02:00
|
|
|
// Fix for mpeg2 and other formats where decoding a frame is necessary to get information about the stream
|
|
|
|
if (VideoCodecContext->pix_fmt == PIX_FMT_NONE) {
|
2007-08-06 17:49:57 +02:00
|
|
|
mkv_SetTrackMask(MF, ~(1 << VideoTrack));
|
2007-07-29 22:49:50 +02:00
|
|
|
int64_t Dummy;
|
|
|
|
DecodeNextFrame(DecodeFrame, &Dummy, Env);
|
|
|
|
mkv_Seek(MF, 0, MKVF_SEEK_TO_PREV_KEYFRAME);
|
|
|
|
}
|
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
VI.image_type = VideoInfo::IT_TFF;
|
|
|
|
VI.width = VideoTI->AV.Video.PixelWidth;
|
|
|
|
VI.height = VideoTI->AV.Video.PixelHeight;
|
|
|
|
VI.fps_denominator = 1;
|
|
|
|
VI.fps_numerator = 30;
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
SetOutputFormat(VideoCodecContext->pix_fmt, Env);
|
|
|
|
InitPP(VI.width, VI.height, APPString, AQuality, VideoCodecContext->pix_fmt, Env);
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
if (!VCacheIsValid)
|
|
|
|
TrackMask &= ~(1 << VideoTrack);
|
2007-07-13 07:15:28 +02:00
|
|
|
}
|
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
if (AudioTrack >= 0) {
|
|
|
|
TrackInfo *AudioTI = mkv_GetTrackInfo(MF, AudioTrack);
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
if (AudioTI->CompEnabled) {
|
|
|
|
AudioCS = cs_Create(MF, AudioTrack, ErrorMessage, sizeof(ErrorMessage));
|
|
|
|
if (AudioCS == NULL)
|
|
|
|
Env->ThrowError("FFmpegSource: Can't create decompressor: %s", ErrorMessage);
|
2007-07-13 07:15:28 +02:00
|
|
|
}
|
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
AudioCodecContext = avcodec_alloc_context();
|
|
|
|
AudioCodecContext->extradata = (uint8_t *)AudioTI->CodecPrivate;
|
|
|
|
AudioCodecContext->extradata_size = AudioTI->CodecPrivateSize;
|
|
|
|
|
|
|
|
AudioCodec = avcodec_find_decoder(MatroskaToFFCodecID(AudioTI));
|
|
|
|
if (AudioCodec == NULL)
|
|
|
|
Env->ThrowError("FFmpegSource: Audio codec not found");
|
|
|
|
|
|
|
|
if (avcodec_open(AudioCodecContext, AudioCodec) < 0)
|
|
|
|
Env->ThrowError("FFmpegSource: Could not open audio codec");
|
|
|
|
|
2007-07-30 13:04:10 +02:00
|
|
|
// Fix for ac3 and other codecs where decoding a block of audio is required to get information about it
|
|
|
|
if (AudioCodecContext->channels == 0 || AudioCodecContext->sample_rate == 0) {
|
|
|
|
mkv_SetTrackMask(MF, ~(1 << AudioTrack));
|
|
|
|
uint64_t StartTime, EndTime, FilePos;
|
|
|
|
unsigned int Track, FrameFlags, FrameSize;
|
|
|
|
mkv_ReadFrame(MF, 0, &Track, &StartTime, &EndTime, &FilePos, &FrameSize, &FrameFlags);
|
|
|
|
|
|
|
|
uint8_t DecodingBuffer[AVCODEC_MAX_AUDIO_FRAME_SIZE];
|
|
|
|
int Size = ReadFrame(FilePos, FrameSize, AudioCS, Env);
|
|
|
|
uint8_t *Data = Buffer;
|
|
|
|
|
|
|
|
while (Size > 0) {
|
|
|
|
int TempOutputBufSize = AVCODEC_MAX_AUDIO_FRAME_SIZE;
|
|
|
|
int Ret = avcodec_decode_audio2(AudioCodecContext, (int16_t *)DecodingBuffer, &TempOutputBufSize, Data, Size);
|
|
|
|
if (Ret < 0)
|
|
|
|
Env->ThrowError("FFmpegSource: Audio decoding error");
|
|
|
|
|
|
|
|
Size -= Ret;
|
|
|
|
Data += Ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
mkv_Seek(MF, 0, MKVF_SEEK_TO_PREV_KEYFRAME);
|
|
|
|
}
|
|
|
|
|
|
|
|
VI.nchannels = AudioCodecContext->channels;
|
|
|
|
VI.audio_samples_per_second = AudioCodecContext->sample_rate;
|
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
switch (AudioCodecContext->sample_fmt) {
|
|
|
|
case SAMPLE_FMT_U8: VI.sample_type = SAMPLE_INT8; break;
|
|
|
|
case SAMPLE_FMT_S16: VI.sample_type = SAMPLE_INT16; break;
|
|
|
|
case SAMPLE_FMT_S24: VI.sample_type = SAMPLE_INT24; break;
|
|
|
|
case SAMPLE_FMT_S32: VI.sample_type = SAMPLE_INT32; break;
|
|
|
|
case SAMPLE_FMT_FLT: VI.sample_type = SAMPLE_FLOAT; break;
|
|
|
|
default:
|
|
|
|
Env->ThrowError("FFmpegSource: Unsupported/unknown sample format");
|
|
|
|
}
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
ACacheIsValid = PrepareAudioCache(AAudioCache, ASource, AudioTrack, Env);
|
|
|
|
if (!ACacheIsValid)
|
|
|
|
TrackMask &= ~(1 << AudioTrack);
|
|
|
|
}
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
mkv_SetTrackMask(MF, TrackMask);
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
uint8_t DecodingBuffer[AVCODEC_MAX_AUDIO_FRAME_SIZE];
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
// Needs to be indexed?
|
|
|
|
if (!ACacheIsValid || !VCacheIsValid) {
|
|
|
|
uint64_t StartTime, EndTime, FilePos;
|
|
|
|
unsigned int Track, FrameFlags, FrameSize;
|
|
|
|
|
|
|
|
while (mkv_ReadFrame(MF, 0, &Track, &StartTime, &EndTime, &FilePos, &FrameSize, &FrameFlags) == 0)
|
|
|
|
if (Track == VideoTrack && !VCacheIsValid) {
|
|
|
|
FrameToDTS.push_back(FrameInfo(StartTime, (FrameFlags & FRAME_KF) != 0));
|
|
|
|
VI.num_frames++;
|
|
|
|
} else if (Track == AudioTrack && !ACacheIsValid) {
|
|
|
|
int Size = ReadFrame(FilePos, FrameSize, AudioCS, Env);
|
|
|
|
uint8_t *Data = Buffer;
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
while (Size > 0) {
|
|
|
|
int TempOutputBufSize = AVCODEC_MAX_AUDIO_FRAME_SIZE;
|
|
|
|
int Ret = avcodec_decode_audio2(AudioCodecContext, (int16_t *)DecodingBuffer, &TempOutputBufSize, Data, Size);
|
|
|
|
if (Ret < 0)
|
|
|
|
Env->ThrowError("FFmpegSource: Audio decoding error");
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
Size -= Ret;
|
|
|
|
Data += Ret;
|
|
|
|
VI.num_audio_samples += VI.AudioSamplesFromBytes(TempOutputBufSize);
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
fwrite(DecodingBuffer, 1, TempOutputBufSize, AudioCache);
|
|
|
|
}
|
|
|
|
}
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-30 13:04:10 +02:00
|
|
|
if (VideoTrack >= 0 && VI.num_frames == 0)
|
|
|
|
Env->ThrowError("FFmpegSource: Video track contains no frames");
|
|
|
|
|
|
|
|
if (AudioTrack >= 0 && VI.num_audio_samples == 0)
|
|
|
|
Env->ThrowError("FFmpegSource: Audio track contains no samples");
|
|
|
|
|
|
|
|
if (VideoTrack >= 0)
|
|
|
|
mkv_Seek(MF, FrameToDTS.front().DTS, MKVF_SEEK_TO_PREV_KEYFRAME);
|
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
if (AVCache && !VCacheIsValid)
|
|
|
|
if (!SaveFrameInfoToFile(AVideoCache, ASource, VideoTrack))
|
|
|
|
Env->ThrowError("FFmpegSource: Failed to write video cache info");
|
|
|
|
}
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
if (AudioTrack >= 0) {
|
|
|
|
avcodec_close(AudioCodecContext);
|
|
|
|
av_free(AudioCodecContext);
|
2007-07-13 07:15:28 +02:00
|
|
|
}
|
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
if (VideoTrack >= 0) {
|
|
|
|
mkv_SetTrackMask(MF, ~(1 << VideoTrack));
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-08-06 17:49:57 +02:00
|
|
|
// Calculate the average framerate
|
2007-07-18 15:00:15 +02:00
|
|
|
if (FrameToDTS.size() >= 2) {
|
|
|
|
double DTSDiff = (double)(FrameToDTS.back().DTS - FrameToDTS.front().DTS);
|
|
|
|
VI.fps_denominator = (unsigned int)(DTSDiff * mkv_TruncFloat(VideoTI->TimecodeScale) / (double)1000 / (double)(VI.num_frames - 1) + 0.5);
|
|
|
|
VI.fps_numerator = 1000000;
|
|
|
|
}
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
if (!SaveTimecodesToFile(ATimecodes, mkv_TruncFloat(VideoTI->TimecodeScale), 1000000))
|
|
|
|
Env->ThrowError("FFmpegSource: Failed to write timecodes");
|
|
|
|
}
|
|
|
|
}
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
~FFMatroskaSource() {
|
|
|
|
free(Buffer);
|
|
|
|
mkv_Close(MF);
|
|
|
|
fclose(ST.fp);
|
|
|
|
if (AudioCache)
|
|
|
|
fclose(AudioCache);
|
|
|
|
if (VideoCodecContext)
|
|
|
|
avcodec_close(VideoCodecContext);
|
|
|
|
av_free(VideoCodecContext);
|
|
|
|
}
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
PVideoFrame __stdcall GetFrame(int n, IScriptEnvironment* Env);
|
|
|
|
};
|
2007-06-16 20:45:56 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
int FFMatroskaSource::ReadFrame(uint64_t AFilePos, unsigned int AFrameSize, CompressedStream *ACS, IScriptEnvironment *Env) {
|
|
|
|
if (ACS) {
|
|
|
|
char CSBuffer[4096];
|
2007-06-16 20:45:56 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
unsigned int DecompressedFrameSize = 0;
|
2007-06-16 20:45:56 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
cs_NextFrame(ACS, AFilePos, AFrameSize);
|
2007-07-02 23:36:21 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
for (;;) {
|
|
|
|
int ReadBytes = cs_ReadData(ACS, CSBuffer, sizeof(CSBuffer));
|
|
|
|
if (ReadBytes < 0)
|
|
|
|
Env->ThrowError("FFmpegSource: Error decompressing data: %s", cs_GetLastError(ACS));
|
|
|
|
if (ReadBytes == 0) {
|
|
|
|
return DecompressedFrameSize;
|
2007-07-02 23:36:21 +02:00
|
|
|
}
|
2007-06-16 20:45:56 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
if (BufferSize < DecompressedFrameSize + ReadBytes) {
|
|
|
|
BufferSize = AFrameSize;
|
|
|
|
Buffer = (uint8_t *)realloc(Buffer, BufferSize);
|
|
|
|
if (Buffer == NULL)
|
|
|
|
Env->ThrowError("FFmpegSource: Out of memory");
|
2007-07-02 23:36:21 +02:00
|
|
|
}
|
2007-06-16 20:45:56 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
memcpy(Buffer + DecompressedFrameSize, CSBuffer, ReadBytes);
|
|
|
|
DecompressedFrameSize += ReadBytes;
|
|
|
|
}
|
|
|
|
} else {
|
2007-07-29 22:49:50 +02:00
|
|
|
if (_fseeki64(ST.fp, AFilePos, SEEK_SET))
|
2007-07-18 15:00:15 +02:00
|
|
|
Env->ThrowError("FFmpegSource: fseek(): %s", strerror(errno));
|
|
|
|
|
|
|
|
if (BufferSize < AFrameSize) {
|
|
|
|
BufferSize = AFrameSize;
|
|
|
|
Buffer = (uint8_t *)realloc(Buffer, BufferSize);
|
|
|
|
if (Buffer == NULL)
|
|
|
|
Env->ThrowError("FFmpegSource: Out of memory");
|
|
|
|
}
|
2007-06-16 20:45:56 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
size_t ReadBytes = fread(Buffer, 1, AFrameSize, ST.fp);
|
|
|
|
if (ReadBytes != AFrameSize) {
|
|
|
|
if (ReadBytes == 0) {
|
|
|
|
if (feof(ST.fp))
|
|
|
|
Env->ThrowError("FFmpegSource: Unexpected EOF while reading frame");
|
|
|
|
else
|
|
|
|
Env->ThrowError("FFmpegSource: Error reading frame: %s", strerror(errno));
|
|
|
|
} else
|
|
|
|
Env->ThrowError("FFmpegSource: Short read while reading frame");
|
|
|
|
Env->ThrowError("FFmpegSource: Unknown read error");
|
|
|
|
}
|
2007-06-16 20:45:56 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
return AFrameSize;
|
2007-06-16 20:45:56 +02:00
|
|
|
}
|
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2007-06-16 20:45:56 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
int FFMatroskaSource::DecodeNextFrame(AVFrame *AFrame, int64_t *AFirstStartTime, IScriptEnvironment* Env) {
|
2007-07-02 23:36:21 +02:00
|
|
|
int FrameFinished = 0;
|
|
|
|
int Ret = -1;
|
2007-07-18 15:00:15 +02:00
|
|
|
*AFirstStartTime = -1;
|
2007-07-02 23:36:21 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
uint64_t StartTime, EndTime, FilePos;
|
|
|
|
unsigned int Track, FrameFlags, FrameSize;
|
|
|
|
|
|
|
|
while (mkv_ReadFrame(MF, 0, &Track, &StartTime, &EndTime, &FilePos, &FrameSize, &FrameFlags) == 0) {
|
|
|
|
FrameSize = ReadFrame(FilePos, FrameSize, VideoCS, Env);
|
|
|
|
if (*AFirstStartTime < 0)
|
|
|
|
*AFirstStartTime = StartTime;
|
|
|
|
Ret = avcodec_decode_video(VideoCodecContext, AFrame, &FrameFinished, Buffer, FrameSize);
|
2007-07-02 23:36:21 +02:00
|
|
|
|
|
|
|
if (FrameFinished)
|
2007-07-13 07:15:28 +02:00
|
|
|
goto Done;
|
2007-07-02 23:36:21 +02:00
|
|
|
}
|
|
|
|
|
2007-07-13 07:15:28 +02:00
|
|
|
// Flush the last frame
|
2007-07-18 15:00:15 +02:00
|
|
|
if (CurrentFrame == VI.num_frames - 1 && VideoCodecContext->has_b_frames)
|
|
|
|
Ret = avcodec_decode_video(VideoCodecContext, AFrame, &FrameFinished, NULL, 0);
|
2007-07-13 07:15:28 +02:00
|
|
|
|
|
|
|
Done:
|
2007-06-16 20:45:56 +02:00
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
PVideoFrame __stdcall FFMatroskaSource::GetFrame(int n, IScriptEnvironment* Env) {
|
2007-06-16 20:45:56 +02:00
|
|
|
bool HasSeeked = false;
|
|
|
|
|
2007-07-02 23:36:21 +02:00
|
|
|
if (n < CurrentFrame || FindClosestKeyFrame(n) > CurrentFrame) {
|
2007-06-16 20:45:56 +02:00
|
|
|
mkv_Seek(MF, FrameToDTS[n].DTS, MKVF_SEEK_TO_PREV_KEYFRAME);
|
2007-07-18 15:00:15 +02:00
|
|
|
avcodec_flush_buffers(VideoCodecContext);
|
2007-06-16 20:45:56 +02:00
|
|
|
HasSeeked = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2007-07-02 23:36:21 +02:00
|
|
|
int64_t StartTime;
|
2007-07-18 15:00:15 +02:00
|
|
|
int Ret = DecodeNextFrame(DecodeFrame, &StartTime, Env);
|
2007-06-16 20:45:56 +02:00
|
|
|
|
|
|
|
if (HasSeeked) {
|
|
|
|
HasSeeked = false;
|
2007-07-02 23:36:21 +02:00
|
|
|
|
|
|
|
if (StartTime < 0 || (CurrentFrame = FrameFromDTS(StartTime)) < 0)
|
2007-07-13 07:15:28 +02:00
|
|
|
Env->ThrowError("FFmpegSource: Frame accurate seeking is not possible in this file");
|
2007-06-16 20:45:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CurrentFrame++;
|
|
|
|
} while (CurrentFrame <= n);
|
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
return OutputFrame(DecodeFrame, Env);
|
2007-06-16 20:45:56 +02:00
|
|
|
}
|
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
class FFmpegSource : public FFBase {
|
|
|
|
private:
|
2007-06-16 20:45:56 +02:00
|
|
|
AVFormatContext *FormatContext;
|
2007-07-18 15:00:15 +02:00
|
|
|
AVCodecContext *VideoCodecContext;
|
2007-06-16 20:45:56 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
int VideoTrack;
|
2007-06-16 20:45:56 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
int CurrentFrame;
|
|
|
|
int SeekMode;
|
2007-06-16 20:45:56 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
int DecodeNextFrame(AVFrame *Frame, int64_t *DTS);
|
|
|
|
|
|
|
|
int GetTrackIndex(int Index, CodecType ATrackType, IScriptEnvironment *Env) {
|
|
|
|
if (Index == -1)
|
|
|
|
for (unsigned int i = 0; i < FormatContext->nb_streams; i++)
|
|
|
|
if (FormatContext->streams[i]->codec->codec_type == ATrackType) {
|
|
|
|
Index = i;
|
2007-06-16 20:45:56 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
if (Index == -1)
|
|
|
|
Env->ThrowError("FFmpegSource: No %s track found", (ATrackType == CODEC_TYPE_VIDEO) ? "video" : "audio");
|
|
|
|
if (Index <= -2)
|
|
|
|
return -2;
|
2007-06-16 20:45:56 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
if (Index >= (int)FormatContext->nb_streams)
|
|
|
|
Env->ThrowError("FFmpegSource: Invalid %s track number", (ATrackType == CODEC_TYPE_VIDEO) ? "video" : "audio");
|
2007-06-16 20:45:56 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
if (FormatContext->streams[Index]->codec->codec_type != ATrackType)
|
|
|
|
Env->ThrowError("FFmpegSource: Selected track is not %s", (ATrackType == CODEC_TYPE_VIDEO) ? "video" : "audio");
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
return Index;
|
2007-07-13 07:15:28 +02:00
|
|
|
}
|
|
|
|
public:
|
2007-07-18 15:00:15 +02:00
|
|
|
FFmpegSource(const char *ASource, int AVideoTrack, int AAudioTrack, const char *ATimecodes, bool AVCache, const char *AVideoCache, const char *AAudioCache, const char *APPString, int AQuality, int ASeekMode, IScriptEnvironment* Env) {
|
|
|
|
CurrentFrame = 0;
|
|
|
|
SeekMode = ASeekMode;
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
AVCodecContext *AudioCodecContext = NULL;
|
|
|
|
AVCodec *AudioCodec;
|
|
|
|
AVCodec *VideoCodec;
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
FormatContext = NULL;
|
|
|
|
VideoCodecContext = NULL;
|
|
|
|
VideoCodec = NULL;
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
if (av_open_input_file(&FormatContext, ASource, NULL, 0, NULL) != 0)
|
|
|
|
Env->ThrowError("FFmpegSource: Couldn't open '%s'", ASource);
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
if (av_find_stream_info(FormatContext) < 0)
|
|
|
|
Env->ThrowError("FFmpegSource: Couldn't find stream information");
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
VideoTrack = GetTrackIndex(AVideoTrack, CODEC_TYPE_VIDEO, Env);
|
|
|
|
int AudioTrack = GetTrackIndex(AAudioTrack, CODEC_TYPE_AUDIO, Env);
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
bool VCacheIsValid = true;
|
|
|
|
bool ACacheIsValid = true;
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
if (VideoTrack >= 0) {
|
|
|
|
VCacheIsValid = LoadFrameInfoFromFile(AVideoCache, ASource, VideoTrack);
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
VideoCodecContext = FormatContext->streams[VideoTrack]->codec;
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
VideoCodec = avcodec_find_decoder(VideoCodecContext->codec_id);
|
|
|
|
if (VideoCodec == NULL)
|
|
|
|
Env->ThrowError("FFmpegSource: Video codec not found");
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
if (avcodec_open(VideoCodecContext, VideoCodec) < 0)
|
|
|
|
Env->ThrowError("FFmpegSource: Could not open video codec");
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
VI.image_type = VideoInfo::IT_TFF;
|
|
|
|
VI.width = VideoCodecContext->width;
|
|
|
|
VI.height = VideoCodecContext->height;
|
|
|
|
VI.fps_denominator = FormatContext->streams[VideoTrack]->time_base.num;
|
|
|
|
VI.fps_numerator = FormatContext->streams[VideoTrack]->time_base.den;
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
// sanity check framerate
|
|
|
|
if (VI.fps_denominator > VI.fps_numerator || VI.fps_denominator <= 0 || VI.fps_numerator <= 0) {
|
|
|
|
VI.fps_denominator = 1;
|
|
|
|
VI.fps_numerator = 30;
|
|
|
|
}
|
|
|
|
|
|
|
|
SetOutputFormat(VideoCodecContext->pix_fmt, Env);
|
|
|
|
InitPP(VI.width, VI.height, APPString, AQuality, VideoCodecContext->pix_fmt, Env);
|
2007-07-13 07:15:28 +02:00
|
|
|
}
|
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
if (AudioTrack >= 0) {
|
|
|
|
AudioCodecContext = FormatContext->streams[AudioTrack]->codec;
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
AudioCodec = avcodec_find_decoder(AudioCodecContext->codec_id);
|
|
|
|
if (AudioCodec == NULL)
|
|
|
|
Env->ThrowError("FFmpegSource: Audio codec not found");
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
if (avcodec_open(AudioCodecContext, AudioCodec) < 0)
|
|
|
|
Env->ThrowError("FFmpegSource: Could not open audio codec");
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
switch (AudioCodecContext->sample_fmt) {
|
|
|
|
case SAMPLE_FMT_U8: VI.sample_type = SAMPLE_INT8; break;
|
|
|
|
case SAMPLE_FMT_S16: VI.sample_type = SAMPLE_INT16; break;
|
|
|
|
case SAMPLE_FMT_S24: VI.sample_type = SAMPLE_INT24; break;
|
|
|
|
case SAMPLE_FMT_S32: VI.sample_type = SAMPLE_INT32; break;
|
|
|
|
case SAMPLE_FMT_FLT: VI.sample_type = SAMPLE_FLOAT; break;
|
|
|
|
default:
|
|
|
|
Env->ThrowError("FFmpegSource: Unsupported/unknown sample format");
|
|
|
|
}
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
VI.nchannels = AudioCodecContext->channels;
|
|
|
|
VI.audio_samples_per_second = AudioCodecContext->sample_rate;
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
ACacheIsValid = PrepareAudioCache(AAudioCache, ASource, AudioTrack, Env);
|
2007-06-16 20:45:56 +02:00
|
|
|
}
|
2007-07-18 15:00:15 +02:00
|
|
|
|
|
|
|
uint8_t DecodingBuffer[AVCODEC_MAX_AUDIO_FRAME_SIZE];
|
2007-06-16 20:45:56 +02:00
|
|
|
|
2007-07-02 23:36:21 +02:00
|
|
|
// Needs to be indexed?
|
2007-07-18 15:00:15 +02:00
|
|
|
if (!ACacheIsValid || !VCacheIsValid) {
|
2007-06-17 18:35:16 +02:00
|
|
|
AVPacket Packet;
|
|
|
|
while (av_read_frame(FormatContext, &Packet) >= 0) {
|
2007-07-18 15:00:15 +02:00
|
|
|
if (Packet.stream_index == VideoTrack && !VCacheIsValid) {
|
2007-07-29 22:49:50 +02:00
|
|
|
FrameToDTS.push_back(FrameInfo(Packet.dts, (Packet.flags & PKT_FLAG_KEY) ? 1 : 0));
|
2007-06-17 18:35:16 +02:00
|
|
|
VI.num_frames++;
|
2007-07-18 15:00:15 +02:00
|
|
|
} else if (Packet.stream_index == AudioTrack && !ACacheIsValid) {
|
|
|
|
int Size = Packet.size;
|
|
|
|
uint8_t *Data = Packet.data;
|
2007-07-02 23:36:21 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
while (Size > 0) {
|
|
|
|
int TempOutputBufSize = AVCODEC_MAX_AUDIO_FRAME_SIZE;
|
|
|
|
int Ret = avcodec_decode_audio2(AudioCodecContext, (int16_t *)DecodingBuffer, &TempOutputBufSize, Data, Size);
|
|
|
|
if (Ret < 0)
|
|
|
|
Env->ThrowError("FFmpegSource: Audio decoding error");
|
|
|
|
|
|
|
|
Size -= Ret;
|
|
|
|
Data += Ret;
|
|
|
|
VI.num_audio_samples += VI.AudioSamplesFromBytes(TempOutputBufSize);
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
fwrite(DecodingBuffer, 1, TempOutputBufSize, AudioCache);
|
|
|
|
}
|
|
|
|
}
|
2007-07-02 23:36:21 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
av_free_packet(&Packet);
|
2007-07-02 23:36:21 +02:00
|
|
|
}
|
2007-07-18 15:00:15 +02:00
|
|
|
|
2007-07-30 13:04:10 +02:00
|
|
|
if (VideoTrack >= 0 && VI.num_frames == 0)
|
|
|
|
Env->ThrowError("FFmpegSource: Video track contains no frames");
|
|
|
|
|
|
|
|
if (AudioTrack >= 0 && VI.num_audio_samples == 0)
|
|
|
|
Env->ThrowError("FFmpegSource: Audio track contains no samples");
|
|
|
|
|
|
|
|
if (VideoTrack >= 0)
|
|
|
|
av_seek_frame(FormatContext, VideoTrack, FrameToDTS.front().DTS, AVSEEK_FLAG_BACKWARD);
|
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
if (AVCache)
|
|
|
|
if (!SaveFrameInfoToFile(AVideoCache, ASource, VideoTrack))
|
|
|
|
Env->ThrowError("FFmpegSource: Failed to write video cache info");
|
2007-06-16 20:45:56 +02:00
|
|
|
}
|
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
if (AudioTrack >= 0)
|
|
|
|
avcodec_close(AudioCodecContext);
|
2007-06-16 20:45:56 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
if (VideoTrack >= 0) {
|
|
|
|
if (!SaveTimecodesToFile(ATimecodes, FormatContext->streams[VideoTrack]->time_base.num * 1000, FormatContext->streams[VideoTrack]->time_base.den))
|
|
|
|
Env->ThrowError("FFmpegSource: Failed to write timecodes");
|
2007-07-29 22:49:50 +02:00
|
|
|
|
2007-08-06 17:49:57 +02:00
|
|
|
// Adjust framerate to match the duration of the first frame
|
2007-07-29 22:49:50 +02:00
|
|
|
if (FrameToDTS.size() >= 2) {
|
|
|
|
int64_t DTSDiff = (double)(FrameToDTS[1].DTS - FrameToDTS[0].DTS);
|
|
|
|
VI.fps_denominator *= DTSDiff;
|
|
|
|
}
|
2007-07-18 15:00:15 +02:00
|
|
|
}
|
2007-06-16 20:45:56 +02:00
|
|
|
}
|
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
~FFmpegSource() {
|
|
|
|
if (VideoTrack >= 0)
|
|
|
|
avcodec_close(VideoCodecContext);
|
|
|
|
av_close_input_file(FormatContext);
|
2007-06-16 20:45:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PVideoFrame __stdcall GetFrame(int n, IScriptEnvironment* Env);
|
|
|
|
};
|
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
int FFmpegSource::DecodeNextFrame(AVFrame *AFrame, int64_t *AStartTime) {
|
2007-06-16 20:45:56 +02:00
|
|
|
AVPacket Packet;
|
|
|
|
int FrameFinished = 0;
|
|
|
|
int Ret = -1;
|
2007-07-18 15:00:15 +02:00
|
|
|
*AStartTime = -1;
|
2007-06-16 20:45:56 +02:00
|
|
|
|
|
|
|
while (av_read_frame(FormatContext, &Packet) >= 0) {
|
2007-07-18 15:00:15 +02:00
|
|
|
if (Packet.stream_index == VideoTrack) {
|
|
|
|
Ret = avcodec_decode_video(VideoCodecContext, AFrame, &FrameFinished, Packet.data, Packet.size);
|
2007-06-16 20:45:56 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
if (*AStartTime < 0)
|
|
|
|
*AStartTime = Packet.dts;
|
2007-06-16 20:45:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
av_free_packet(&Packet);
|
|
|
|
|
|
|
|
if (FrameFinished)
|
2007-07-13 07:15:28 +02:00
|
|
|
goto Done;
|
2007-06-16 20:45:56 +02:00
|
|
|
}
|
|
|
|
|
2007-07-13 07:15:28 +02:00
|
|
|
// Flush the last frame
|
2007-07-18 15:00:15 +02:00
|
|
|
if (CurrentFrame == VI.num_frames - 1 && VideoCodecContext->has_b_frames)
|
|
|
|
Ret = avcodec_decode_video(VideoCodecContext, AFrame, &FrameFinished, NULL, 0);
|
2007-07-13 07:15:28 +02:00
|
|
|
|
|
|
|
Done:
|
2007-06-16 20:45:56 +02:00
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
PVideoFrame __stdcall FFmpegSource::GetFrame(int n, IScriptEnvironment* Env) {
|
2007-06-16 20:45:56 +02:00
|
|
|
bool HasSeeked = false;
|
2007-07-13 07:15:28 +02:00
|
|
|
int ClosestKF = FindClosestKeyFrame(n);
|
2007-06-16 20:45:56 +02:00
|
|
|
|
2007-07-02 23:36:21 +02:00
|
|
|
if (SeekMode == 0) {
|
|
|
|
if (n < CurrentFrame) {
|
2007-07-29 22:49:50 +02:00
|
|
|
av_seek_frame(FormatContext, VideoTrack, FrameToDTS.front().DTS, AVSEEK_FLAG_BACKWARD);
|
2007-07-18 15:00:15 +02:00
|
|
|
avcodec_flush_buffers(VideoCodecContext);
|
2007-07-02 23:36:21 +02:00
|
|
|
CurrentFrame = 0;
|
|
|
|
}
|
|
|
|
} else {
|
2007-07-13 07:15:28 +02:00
|
|
|
// 10 frames is used as a margin to prevent excessive seeking since the predicted best keyframe isn't always selected by avformat
|
|
|
|
if (n < CurrentFrame || ClosestKF > CurrentFrame + 10 || (SeekMode == 3 && n > CurrentFrame + 10)) {
|
2007-07-18 15:00:15 +02:00
|
|
|
av_seek_frame(FormatContext, VideoTrack, (SeekMode == 3) ? FrameToDTS[n].DTS : FrameToDTS[ClosestKF].DTS, AVSEEK_FLAG_BACKWARD);
|
|
|
|
avcodec_flush_buffers(VideoCodecContext);
|
2007-07-02 23:36:21 +02:00
|
|
|
HasSeeked = true;
|
|
|
|
}
|
2007-06-16 20:45:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2007-07-18 15:00:15 +02:00
|
|
|
int64_t StartTime;
|
|
|
|
DecodeNextFrame(DecodeFrame, &StartTime);
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-06-16 20:45:56 +02:00
|
|
|
if (HasSeeked) {
|
2007-07-02 23:36:21 +02:00
|
|
|
HasSeeked = false;
|
|
|
|
|
|
|
|
// Is the seek destination time known? Does it belong to a frame?
|
2007-07-18 15:00:15 +02:00
|
|
|
if (StartTime < 0 || (CurrentFrame = FrameFromDTS(StartTime)) < 0) {
|
2007-07-02 23:36:21 +02:00
|
|
|
switch (SeekMode) {
|
|
|
|
case 1:
|
2007-07-13 07:15:28 +02:00
|
|
|
Env->ThrowError("FFmpegSource: Frame accurate seeking is not possible in this file");
|
2007-07-02 23:36:21 +02:00
|
|
|
case 2:
|
|
|
|
case 3:
|
2007-07-18 15:00:15 +02:00
|
|
|
CurrentFrame = ClosestFrameFromDTS(StartTime);
|
2007-07-02 23:36:21 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
Env->ThrowError("FFmpegSource: Failed assertion");
|
|
|
|
}
|
|
|
|
}
|
2007-06-16 20:45:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
CurrentFrame++;
|
2007-07-02 23:36:21 +02:00
|
|
|
} while (CurrentFrame <= n);
|
2007-06-16 20:45:56 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
return OutputFrame(DecodeFrame, Env);
|
2007-06-16 20:45:56 +02:00
|
|
|
}
|
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
AVSValue __cdecl CreateFFmpegSource(AVSValue Args, void* UserData, IScriptEnvironment* Env) {
|
|
|
|
if (!UserData) {
|
|
|
|
av_register_all();
|
|
|
|
UserData = (void *)-1;
|
2007-07-13 07:15:28 +02:00
|
|
|
}
|
|
|
|
|
2007-06-16 20:45:56 +02:00
|
|
|
if (!Args[0].Defined())
|
2007-06-17 18:35:16 +02:00
|
|
|
Env->ThrowError("FFmpegSource: No source specified");
|
2007-06-16 20:45:56 +02:00
|
|
|
|
2007-07-02 23:36:21 +02:00
|
|
|
const char *Source = Args[0].AsString();
|
2007-07-18 15:00:15 +02:00
|
|
|
int VTrack = Args[1].AsInt(-1);
|
|
|
|
int ATrack = Args[2].AsInt(-2);
|
2007-07-02 23:36:21 +02:00
|
|
|
const char *Timecodes = Args[3].AsString("");
|
2007-07-18 15:00:15 +02:00
|
|
|
bool VCache = Args[4].AsBool(true);
|
|
|
|
const char *VCacheFile = Args[5].AsString("");
|
|
|
|
const char *ACacheFile = Args[6].AsString("");
|
|
|
|
const char *PPString = Args[7].AsString("");
|
|
|
|
int PPQuality = Args[8].AsInt(PP_QUALITY_MAX);
|
|
|
|
int SeekMode = Args[9].AsInt(1);
|
2007-07-13 07:15:28 +02:00
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
if (VTrack <= -2 && ATrack <= -2)
|
|
|
|
Env->ThrowError("FFmpegSource: No tracks selected");
|
2007-07-13 07:15:28 +02:00
|
|
|
|
|
|
|
AVFormatContext *FormatContext;
|
|
|
|
|
|
|
|
if (av_open_input_file(&FormatContext, Source, NULL, 0, NULL) != 0)
|
2007-07-18 15:00:15 +02:00
|
|
|
Env->ThrowError("FFmpegSource: Couldn't open %s", Args[0].AsString());
|
2007-07-13 07:15:28 +02:00
|
|
|
bool IsMatroska = !strcmp(FormatContext->iformat->name, "matroska");
|
|
|
|
av_close_input_file(FormatContext);
|
|
|
|
|
|
|
|
if (IsMatroska)
|
2007-07-18 15:00:15 +02:00
|
|
|
return new FFMatroskaSource(Source, VTrack, ATrack, Timecodes, VCache, VCacheFile, ACacheFile, PPString, PPQuality, Env);
|
2007-07-13 07:15:28 +02:00
|
|
|
else
|
2007-07-18 15:00:15 +02:00
|
|
|
return new FFmpegSource(Source, VTrack, ATrack, Timecodes, VCache, VCacheFile, ACacheFile, PPString, PPQuality, SeekMode, Env);
|
2007-07-13 07:15:28 +02:00
|
|
|
}
|
|
|
|
|
2007-07-18 15:00:15 +02:00
|
|
|
AVSValue __cdecl CreateFFPP(AVSValue Args, void* UserData, IScriptEnvironment* Env) {
|
|
|
|
return new FFPP(Args[0].AsClip(), Args[1].AsString(""), Args[2].AsInt(PP_QUALITY_MAX), Env);
|
2007-07-13 07:15:28 +02:00
|
|
|
}
|
|
|
|
|
2007-06-16 20:45:56 +02:00
|
|
|
extern "C" __declspec(dllexport) const char* __stdcall AvisynthPluginInit2(IScriptEnvironment* Env) {
|
2007-07-18 15:00:15 +02:00
|
|
|
Env->AddFunction("FFmpegSource", "[source]s[vtrack]i[atrack]i[timecodes]s[vcache]b[vcachefile]s[acachefile]s[pp]s[ppquality]i[seekmode]i", CreateFFmpegSource, 0);
|
|
|
|
Env->AddFunction("FFPP", "c[pp]s[ppquality]i", CreateFFPP, 0);
|
2007-06-16 21:46:22 +02:00
|
|
|
return "FFmpegSource";
|
2007-06-16 20:45:56 +02:00
|
|
|
};
|
|
|
|
|