2008-03-28 22:14:06 +01:00
|
|
|
// Copyright (c) 2007-2008 Fredrik Mellbin
|
2007-09-07 00:56:18 +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.
|
|
|
|
|
2007-08-31 22:26:51 +02:00
|
|
|
#include "ffmpegsource.h"
|
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
int FFMatroskaSource::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-08-31 22:26:51 +02:00
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
if (Index == -1)
|
|
|
|
Env->ThrowError("FFmpegSource: No %s track found", (ATrackType & TT_VIDEO) ? "video" : "audio");
|
|
|
|
if (Index <= -2)
|
|
|
|
return -2;
|
2007-08-31 22:26:51 +02:00
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
if (Index >= (int)mkv_GetNumTracks(MF))
|
|
|
|
Env->ThrowError("FFmpegSource: Invalid %s track number", (ATrackType & TT_VIDEO) ? "video" : "audio");
|
2007-08-31 22:26:51 +02:00
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
TrackInfo *TI = mkv_GetTrackInfo(MF, Index);
|
2007-08-31 22:26:51 +02:00
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
if (TI->Type != ATrackType)
|
|
|
|
Env->ThrowError("FFmpegSource: Selected track is not %s", (ATrackType & TT_VIDEO) ? "video" : "audio");
|
2007-08-31 22:26:51 +02:00
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
return Index;
|
|
|
|
}
|
2007-08-31 22:26:51 +02:00
|
|
|
|
2007-09-17 20:02:24 +02:00
|
|
|
FFMatroskaSource::FFMatroskaSource(const char *ASource, int AVideoTrack, int AAudioTrack, const char *ATimecodes,
|
2008-03-28 22:14:06 +01:00
|
|
|
bool AVCache, const char *AVideoCache, const char *AAudioCache, const char *APPString,
|
|
|
|
int AQuality, int AThreads, IScriptEnvironment* Env, FrameInfoVector *AFrames) {
|
2007-09-17 20:02:24 +02:00
|
|
|
|
2008-01-05 14:27:12 +01:00
|
|
|
AFrames = &Frames;
|
2007-09-07 22:07:27 +02:00
|
|
|
CurrentFrame = 0;
|
|
|
|
int VideoTrack;
|
|
|
|
int AudioTrack;
|
|
|
|
unsigned int TrackMask = ~0;
|
|
|
|
AVCodecContext *AudioCodecContext = NULL;
|
|
|
|
AVCodec *AudioCodec = NULL;
|
|
|
|
VideoCodecContext = NULL;
|
|
|
|
AVCodec *VideoCodec = NULL;
|
|
|
|
TrackInfo *VideoTI = NULL;
|
|
|
|
BufferSize = 0;
|
|
|
|
Buffer = NULL;
|
|
|
|
VideoCS = NULL;
|
|
|
|
AudioCS = NULL;
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
ST.fp = fopen(ASource, "rb");
|
|
|
|
if (ST.fp == NULL)
|
|
|
|
Env->ThrowError("FFmpegSource: Can't open '%s': %s", ASource, strerror(errno));
|
|
|
|
|
|
|
|
setvbuf(ST.fp, NULL, _IOFBF, CACHESIZE);
|
|
|
|
|
|
|
|
MF = mkv_OpenEx(&ST.base, 0, 0, ErrorMessage, sizeof(ErrorMessage));
|
|
|
|
if (MF == NULL) {
|
|
|
|
fclose(ST.fp);
|
|
|
|
Env->ThrowError("FFmpegSource: Can't parse Matroska file: %s", ErrorMessage);
|
|
|
|
}
|
2007-08-31 22:26:51 +02:00
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
VideoTrack = GetTrackIndex(AVideoTrack, TT_VIDEO, Env);
|
|
|
|
AudioTrack = GetTrackIndex(AAudioTrack, TT_AUDIO, Env);
|
2007-08-31 22:26:51 +02:00
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
bool VCacheIsValid = true;
|
|
|
|
bool ACacheIsValid = true;
|
2007-08-31 22:26:51 +02:00
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
if (VideoTrack >= 0) {
|
|
|
|
VCacheIsValid = LoadFrameInfoFromFile(AVideoCache, ASource, VideoTrack);
|
2007-08-31 22:26:51 +02:00
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
VideoTI = mkv_GetTrackInfo(MF, VideoTrack);
|
2007-08-31 22:26:51 +02:00
|
|
|
|
2007-09-07 22:07:27 +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-08-31 22:26:51 +02:00
|
|
|
}
|
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
VideoCodecContext = avcodec_alloc_context();
|
|
|
|
VideoCodecContext->extradata = (uint8_t *)VideoTI->CodecPrivate;
|
|
|
|
VideoCodecContext->extradata_size = VideoTI->CodecPrivateSize;
|
2008-03-28 22:14:06 +01:00
|
|
|
VideoCodecContext->thread_count = AThreads;
|
2007-08-31 22:26:51 +02:00
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
VideoCodec = avcodec_find_decoder(MatroskaToFFCodecID(VideoTI));
|
|
|
|
if (VideoCodec == NULL)
|
|
|
|
Env->ThrowError("FFmpegSource: Video codec not found");
|
2007-08-31 22:26:51 +02:00
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
if (avcodec_open(VideoCodecContext, VideoCodec) < 0)
|
|
|
|
Env->ThrowError("FFmpegSource: Could not open video codec");
|
|
|
|
|
|
|
|
// Fix for mpeg2 and other formats where decoding a frame is necessary to get information about the stream
|
2007-10-06 22:03:02 +02:00
|
|
|
if (VideoCodecContext->pix_fmt == PIX_FMT_NONE || VideoCodecContext->width == 0 || VideoCodecContext->height == 0) {
|
2007-09-07 22:07:27 +02:00
|
|
|
mkv_SetTrackMask(MF, ~(1 << VideoTrack));
|
|
|
|
int64_t Dummy;
|
|
|
|
DecodeNextFrame(DecodeFrame, &Dummy, Env);
|
|
|
|
mkv_Seek(MF, 0, MKVF_SEEK_TO_PREV_KEYFRAME);
|
|
|
|
}
|
2007-08-31 22:26:51 +02:00
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
VI.image_type = VideoInfo::IT_TFF;
|
2008-01-05 14:27:12 +01:00
|
|
|
VI.width = VideoCodecContext->width;
|
|
|
|
VI.height = VideoCodecContext->height;;
|
2007-09-07 22:07:27 +02:00
|
|
|
VI.fps_denominator = 1;
|
|
|
|
VI.fps_numerator = 30;
|
2007-08-31 22:26:51 +02:00
|
|
|
|
2007-10-06 22:03:02 +02:00
|
|
|
if (VI.width <= 0 || VI.height <= 0)
|
|
|
|
Env->ThrowError("FFmpegSource: Codec returned zero size video");
|
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
SetOutputFormat(VideoCodecContext->pix_fmt, Env);
|
|
|
|
InitPP(VI.width, VI.height, APPString, AQuality, VideoCodecContext->pix_fmt, Env);
|
2007-08-31 22:26:51 +02:00
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
if (!VCacheIsValid)
|
|
|
|
TrackMask &= ~(1 << VideoTrack);
|
|
|
|
}
|
2007-08-31 22:26:51 +02:00
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
if (AudioTrack >= 0) {
|
|
|
|
TrackInfo *AudioTI = mkv_GetTrackInfo(MF, AudioTrack);
|
2007-08-31 22:26:51 +02:00
|
|
|
|
2007-09-07 22:07:27 +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-08-31 22:26:51 +02:00
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
AudioCodecContext = avcodec_alloc_context();
|
|
|
|
AudioCodecContext->extradata = (uint8_t *)AudioTI->CodecPrivate;
|
|
|
|
AudioCodecContext->extradata_size = AudioTI->CodecPrivateSize;
|
2007-08-31 22:26:51 +02:00
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
AudioCodec = avcodec_find_decoder(MatroskaToFFCodecID(AudioTI));
|
|
|
|
if (AudioCodec == NULL)
|
|
|
|
Env->ThrowError("FFmpegSource: Audio codec not found");
|
2007-08-31 22:26:51 +02:00
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
if (avcodec_open(AudioCodecContext, AudioCodec) < 0)
|
|
|
|
Env->ThrowError("FFmpegSource: Could not open audio codec");
|
2007-08-31 22:26:51 +02:00
|
|
|
|
2007-09-07 22:07:27 +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);
|
2007-08-31 22:26:51 +02:00
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
int Size = ReadFrame(FilePos, FrameSize, AudioCS, Env);
|
|
|
|
uint8_t *Data = Buffer;
|
2007-08-31 22:26:51 +02:00
|
|
|
|
2007-09-07 22:07:27 +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;
|
2007-08-31 22:26:51 +02:00
|
|
|
}
|
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
mkv_Seek(MF, 0, MKVF_SEEK_TO_PREV_KEYFRAME);
|
|
|
|
}
|
|
|
|
|
|
|
|
VI.nchannels = AudioCodecContext->channels;
|
|
|
|
VI.audio_samples_per_second = AudioCodecContext->sample_rate;
|
|
|
|
|
|
|
|
switch (AudioCodecContext->sample_fmt) {
|
2008-03-28 22:14:06 +01:00
|
|
|
case SAMPLE_FMT_U8: VI.sample_type = SAMPLE_INT8; break;
|
2007-09-07 22:07:27 +02:00
|
|
|
case SAMPLE_FMT_S16: VI.sample_type = SAMPLE_INT16; break;
|
2008-03-28 22:14:06 +01:00
|
|
|
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;
|
2007-09-07 22:07:27 +02:00
|
|
|
default:
|
|
|
|
Env->ThrowError("FFmpegSource: Unsupported/unknown sample format");
|
|
|
|
}
|
2007-08-31 22:26:51 +02:00
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
ACacheIsValid = OpenAudioCache(AAudioCache, ASource, AudioTrack, Env);
|
|
|
|
if (!ACacheIsValid)
|
|
|
|
TrackMask &= ~(1 << AudioTrack);
|
|
|
|
}
|
2007-08-31 22:26:51 +02:00
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
mkv_SetTrackMask(MF, TrackMask);
|
|
|
|
|
|
|
|
// Needs to be indexed?
|
|
|
|
if (!ACacheIsValid || !VCacheIsValid) {
|
|
|
|
FILE *RawCache = NULL;
|
|
|
|
if (!ACacheIsValid)
|
2008-03-28 22:14:06 +01:00
|
|
|
AudioCacheType = acRaw;
|
2007-09-07 22:07:27 +02:00
|
|
|
|
|
|
|
switch (AudioCacheType) {
|
|
|
|
case acRaw: RawCache = NewRawCacheWriter(AAudioCache, ASource, AudioTrack, Env); break;
|
|
|
|
}
|
2007-08-31 22:26:51 +02:00
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
uint64_t StartTime, EndTime, FilePos;
|
|
|
|
unsigned int Track, FrameFlags, FrameSize;
|
2007-08-31 22:26:51 +02:00
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
while (mkv_ReadFrame(MF, 0, &Track, &StartTime, &EndTime, &FilePos, &FrameSize, &FrameFlags) == 0)
|
|
|
|
if (Track == VideoTrack && !VCacheIsValid) {
|
2008-01-05 14:27:12 +01:00
|
|
|
Frames.push_back(FrameInfo(StartTime, (FrameFlags & FRAME_KF) != 0));
|
2007-09-07 22:07:27 +02:00
|
|
|
VI.num_frames++;
|
|
|
|
} else if (Track == AudioTrack && !ACacheIsValid) {
|
2007-08-31 22:26:51 +02:00
|
|
|
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");
|
|
|
|
|
2008-01-05 14:27:12 +01:00
|
|
|
int DecodedSamples = (int)VI.AudioSamplesFromBytes(TempOutputBufSize);
|
2007-09-07 22:07:27 +02:00
|
|
|
|
2007-08-31 22:26:51 +02:00
|
|
|
Size -= Ret;
|
|
|
|
Data += Ret;
|
2007-09-07 22:07:27 +02:00
|
|
|
VI.num_audio_samples += DecodedSamples;
|
|
|
|
|
|
|
|
if (AudioCacheType == acRaw) {
|
|
|
|
fwrite(DecodingBuffer, 1, TempOutputBufSize, RawCache);
|
|
|
|
}
|
2007-08-31 22:26:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
if (!ACacheIsValid) {
|
|
|
|
switch (AudioCacheType) {
|
|
|
|
case acRaw: CloseRawCacheWriter(RawCache); break;
|
2007-08-31 22:26:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ACacheIsValid = OpenAudioCache(AAudioCache, ASource, AudioTrack, Env);
|
|
|
|
if (!ACacheIsValid)
|
2007-09-07 22:07:27 +02:00
|
|
|
Env->ThrowError("FFmpegSource: Failed to open newly created audio cache for reading");
|
2007-08-31 22:26:51 +02:00
|
|
|
}
|
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
if (VideoTrack >= 0 && VI.num_frames == 0)
|
|
|
|
Env->ThrowError("FFmpegSource: Video track contains no frames");
|
2007-08-31 22:26:51 +02:00
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
if (AudioTrack >= 0 && VI.num_audio_samples == 0)
|
|
|
|
Env->ThrowError("FFmpegSource: Audio track contains no samples");
|
2007-08-31 22:26:51 +02:00
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
if (VideoTrack >= 0)
|
2008-01-05 14:27:12 +01:00
|
|
|
mkv_Seek(MF, Frames.front().DTS, MKVF_SEEK_TO_PREV_KEYFRAME);
|
2007-08-31 22:26:51 +02:00
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
if (AVCache && !VCacheIsValid)
|
|
|
|
if (!SaveFrameInfoToFile(AVideoCache, ASource, VideoTrack))
|
|
|
|
Env->ThrowError("FFmpegSource: Failed to write video cache info");
|
|
|
|
}
|
2007-08-31 22:26:51 +02:00
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
if (AudioTrack >= 0) {
|
|
|
|
avcodec_close(AudioCodecContext);
|
|
|
|
av_free(AudioCodecContext);
|
|
|
|
}
|
2007-08-31 22:26:51 +02:00
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
if (VideoTrack >= 0) {
|
|
|
|
mkv_SetTrackMask(MF, ~(1 << VideoTrack));
|
2007-08-31 22:26:51 +02:00
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
// Calculate the average framerate
|
2008-01-05 14:27:12 +01:00
|
|
|
if (Frames.size() >= 2) {
|
|
|
|
double DTSDiff = (double)(Frames.back().DTS - Frames.front().DTS);
|
2007-09-07 22:07:27 +02:00
|
|
|
VI.fps_denominator = (unsigned int)(DTSDiff * mkv_TruncFloat(VideoTI->TimecodeScale) / (double)1000 / (double)(VI.num_frames - 1) + 0.5);
|
|
|
|
VI.fps_numerator = 1000000;
|
2007-08-31 22:26:51 +02:00
|
|
|
}
|
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
if (!SaveTimecodesToFile(ATimecodes, mkv_TruncFloat(VideoTI->TimecodeScale), 1000000))
|
|
|
|
Env->ThrowError("FFmpegSource: Failed to write timecodes");
|
2007-08-31 22:26:51 +02:00
|
|
|
}
|
2007-09-07 22:07:27 +02:00
|
|
|
}
|
2007-08-31 22:26:51 +02:00
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
FFMatroskaSource::~FFMatroskaSource() {
|
|
|
|
free(Buffer);
|
|
|
|
mkv_Close(MF);
|
|
|
|
fclose(ST.fp);
|
|
|
|
if (VideoCodecContext)
|
|
|
|
avcodec_close(VideoCodecContext);
|
|
|
|
av_free(VideoCodecContext);
|
|
|
|
}
|
2007-08-31 22:26:51 +02:00
|
|
|
|
|
|
|
int FFMatroskaSource::ReadFrame(uint64_t AFilePos, unsigned int AFrameSize, CompressedStream *ACS, IScriptEnvironment *Env) {
|
|
|
|
if (ACS) {
|
|
|
|
char CSBuffer[4096];
|
|
|
|
|
|
|
|
unsigned int DecompressedFrameSize = 0;
|
|
|
|
|
|
|
|
cs_NextFrame(ACS, AFilePos, AFrameSize);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (BufferSize < DecompressedFrameSize + ReadBytes) {
|
|
|
|
BufferSize = AFrameSize;
|
|
|
|
Buffer = (uint8_t *)realloc(Buffer, BufferSize);
|
|
|
|
if (Buffer == NULL)
|
|
|
|
Env->ThrowError("FFmpegSource: Out of memory");
|
|
|
|
}
|
|
|
|
|
|
|
|
memcpy(Buffer + DecompressedFrameSize, CSBuffer, ReadBytes);
|
|
|
|
DecompressedFrameSize += ReadBytes;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (_fseeki64(ST.fp, AFilePos, SEEK_SET))
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
|
|
|
return AFrameSize;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int FFMatroskaSource::DecodeNextFrame(AVFrame *AFrame, int64_t *AFirstStartTime, IScriptEnvironment* Env) {
|
|
|
|
int FrameFinished = 0;
|
|
|
|
int Ret = -1;
|
|
|
|
*AFirstStartTime = -1;
|
|
|
|
|
|
|
|
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);
|
|
|
|
|
|
|
|
if (FrameFinished)
|
|
|
|
goto Done;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Flush the last frame
|
|
|
|
if (CurrentFrame == VI.num_frames - 1 && VideoCodecContext->has_b_frames)
|
|
|
|
Ret = avcodec_decode_video(VideoCodecContext, AFrame, &FrameFinished, NULL, 0);
|
|
|
|
|
|
|
|
Done:
|
|
|
|
return Ret;
|
|
|
|
}
|
|
|
|
|
2007-09-07 22:07:27 +02:00
|
|
|
PVideoFrame FFMatroskaSource::GetFrame(int n, IScriptEnvironment* Env) {
|
2007-09-17 20:02:24 +02:00
|
|
|
if (LastFrameNum == n)
|
|
|
|
return LastFrame;
|
|
|
|
|
2007-08-31 22:26:51 +02:00
|
|
|
bool HasSeeked = false;
|
|
|
|
|
|
|
|
if (n < CurrentFrame || FindClosestKeyFrame(n) > CurrentFrame) {
|
2008-01-05 14:27:12 +01:00
|
|
|
mkv_Seek(MF, Frames[n].DTS, MKVF_SEEK_TO_PREV_KEYFRAME);
|
2007-08-31 22:26:51 +02:00
|
|
|
avcodec_flush_buffers(VideoCodecContext);
|
|
|
|
HasSeeked = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
|
|
|
int64_t StartTime;
|
|
|
|
int Ret = DecodeNextFrame(DecodeFrame, &StartTime, Env);
|
|
|
|
|
|
|
|
if (HasSeeked) {
|
|
|
|
HasSeeked = false;
|
|
|
|
|
|
|
|
if (StartTime < 0 || (CurrentFrame = FrameFromDTS(StartTime)) < 0)
|
|
|
|
Env->ThrowError("FFmpegSource: Frame accurate seeking is not possible in this file");
|
|
|
|
}
|
|
|
|
|
|
|
|
CurrentFrame++;
|
|
|
|
} while (CurrentFrame <= n);
|
|
|
|
|
2007-09-17 20:02:24 +02:00
|
|
|
LastFrame = OutputFrame(DecodeFrame, Env);
|
|
|
|
LastFrameNum = n;
|
|
|
|
return LastFrame;
|
2007-08-31 22:26:51 +02:00
|
|
|
}
|