summaryrefslogtreecommitdiff
path: root/src/ffmpeg_backend.cpp
diff options
context:
space:
mode:
authorFox Caminiti <fox@foxcam.net>2022-12-18 20:00:47 -0500
committerFox Caminiti <fox@foxcam.net>2022-12-18 20:00:47 -0500
commit4854647d659f75ac6cf4575b61d1dcfd25865791 (patch)
tree3296bdfa30fa76688844755b37094753ea82d033 /src/ffmpeg_backend.cpp
parentbedd6906eabdd513042d6a178d4dc56a3a41d1d3 (diff)
lazy
Diffstat (limited to 'src/ffmpeg_backend.cpp')
-rw-r--r--src/ffmpeg_backend.cpp41
1 files changed, 22 insertions, 19 deletions
diff --git a/src/ffmpeg_backend.cpp b/src/ffmpeg_backend.cpp
index f4c4492..821afce 100644
--- a/src/ffmpeg_backend.cpp
+++ b/src/ffmpeg_backend.cpp
@@ -66,6 +66,9 @@ void AV_IsFileSupported(char *filename, bool32 *IsVideo, bool32 *HasAudio)
AVFormatContext *temp = avformat_alloc_context();
err = avformat_open_input(&temp, filename, NULL, NULL);;
+ // if (*err == AVERROR_) {
+ // }
+
if (err < 0) {
fprintf(stderr, "Libav error: (%s)\n", av_err2str(err));
avformat_free_context(temp);
@@ -142,7 +145,7 @@ void AV_InitStream(av_stream_info *Stream)
// The duration isn't always reported in AVStream, but seeking towards the end
// and advancing until we hit EOF seems to be accurate.
-void AV_GetDuration(av_info *AV, av_stream_info *Stream, uint64 *Duration, uint32 *FrameCount)
+void AV_GetDuration(av_info *AV, av_stream_info *Stream, uint64 *Duration, real32 *SecondCount)
{
if (Stream->Stream->duration > 0) {
*Duration = Stream->Stream->duration;
@@ -163,12 +166,12 @@ void AV_GetDuration(av_info *AV, av_stream_info *Stream, uint64 *Duration, uint3
}
*Duration = TestDuration;
}
+ real32 FPS = (real32)AV->Video.Stream->r_frame_rate.num / AV->Video.Stream->r_frame_rate.den;
if (Stream->Stream->nb_frames > 0) {
- *FrameCount = Stream->Stream->nb_frames;
+ *SecondCount = Stream->Stream->nb_frames / FPS;
} else if (AV->Video.CodecContext) {
Assert(AV->FileFormatContext->duration > 0);
- int TotalSeconds = AV->FileFormatContext->duration / 1000000LL;
- *FrameCount = (int)(TotalSeconds * (real32)AV->Video.Stream->r_frame_rate.num / AV->Video.Stream->r_frame_rate.den);
+ *SecondCount = (real32)AV->FileFormatContext->duration / 1000000LL;
}
}
@@ -239,15 +242,15 @@ void AV_Init(block_source *Source, av_info *AV, memory *Memory)
Source->Width = AV->Video.CodecContext->width;
Source->Height = AV->Video.CodecContext->height;
Source->FPS = (real32)AV->Video.Stream->r_frame_rate.num / AV->Video.Stream->r_frame_rate.den;
- AV_GetDuration(AV, &AV->Video, &AV->PTSDuration, &AV->FrameCount);
+ AV_GetDuration(AV, &AV->Video, &AV->PTSDuration, &AV->SecondCount);
AV->LastFrameRendered = -1;
av_seek_frame(AV->FileFormatContext, -1, 0, AVSEEK_FLAG_BACKWARD);
- avcodec_flush_buffers(AV->Video.CodecContext);
} else {
- AV_GetDuration(AV, &AV->Audio, &AV->PTSDuration, &AV->FrameCount);
+ AV_GetDuration(AV, &AV->Audio, &AV->PTSDuration, &AV->SecondCount);
av_seek_frame(AV->FileFormatContext, -1, 0, AVSEEK_FLAG_BACKWARD);
- avcodec_flush_buffers(AV->Audio.CodecContext);
}
+ avcodec_flush_buffers(AV->Video.CodecContext);
+ avcodec_flush_buffers(AV->Audio.CodecContext);
};
uint32 AV_AudioTest(av_info *AV, void *Data, uint32 Size)
@@ -301,15 +304,16 @@ uint32 AV_AudioTest(av_info *AV, void *Data, uint32 Size)
return 0;
}
-void AV_SeekAudio(av_info *AV, uint32 FPS, int32 FrameToSeek)
+void AV_SeekAudio(av_info *AV, real32 FPS, int32 FrameToSeek)
{
Assert(FrameToSeek > -1)
int64 SeekSeconds = (int64)(FrameToSeek / (int32)(FPS + 0.5f) * AV_TIME_BASE);
av_seek_frame(AV->FileFormatContext, -1, SeekSeconds, AVSEEK_FLAG_BACKWARD);
- int64 SeekPTS = (int64)(((real64)FrameToSeek / AV->FrameCount) * AV->PTSDuration);
-
- //int64 AveragePTS = (AV->Video.CodecContext) ? AV->PTSDuration / AV->FrameCount :
+ /*
+ real32 TotalFrames = AV->SecondCount * FPS;
+ int64 SeekPTS = (int64)(((real64)FrameToSeek / TotalFrames) * AV->PTSDuration);
+ int64 AveragePTS = AV->PTSDuration / TotalFrames;
int32 err = 0;
bool32 EndOfFile = 0;
@@ -319,16 +323,13 @@ void AV_SeekAudio(av_info *AV, uint32 FPS, int32 FrameToSeek)
int a = 0;
}
}
- AV_TryFrame(AV, AV->Video.CodecContext, &err, &EndOfFile);
-
- int64 AveragePTS = AV->PTSDuration / AV->FrameCount;
while (err >= 0) {
- if (AV_TryFrame(AV, AV->Video.CodecContext, &err, &EndOfFile, AV->Video.Index))
+ if (AV_TryFrame(AV, AV->Audio.CodecContext, &err, &EndOfFile, AV->Audio.Index))
{
// The first frame that gets loaded isn't always the actual
// first frame, so we need to check until it's correct.
- if (FrameToSeek == 0 && AV->Frame->pts < AV->Video.Stream->start_time) {
+ if (FrameToSeek == 0 && AV->Frame->pts < AV->Audio.Stream->start_time) {
av_frame_unref(AV->Frame);
// printf("NON-START: avg: %li, real pts: %li", SeekPTS, AV->VideoFrame->pts);
continue;
@@ -341,6 +342,7 @@ void AV_SeekAudio(av_info *AV, uint32 FPS, int32 FrameToSeek)
}
}
}
+ */
}
@@ -366,8 +368,9 @@ void AV_LoadVideoFrame(memory *Memory, block_source *Source, av_info *AV, int32
*LastFrameRendered = FrameToSeek;
- int64 SeekPTS = (int64)(((real64)FrameToSeek / AV->FrameCount) * AV->PTSDuration);
- int64 AveragePTS = AV->PTSDuration / AV->FrameCount;
+ real32 TotalFrames = AV->SecondCount * Source->FPS;
+ int64 SeekPTS = (int64)(((real64)FrameToSeek / TotalFrames) * AV->PTSDuration);
+ int64 AveragePTS = AV->PTSDuration / TotalFrames;
bool32 EndOfFile = 0;
while (err >= 0) {