Skip to content

Commit

Permalink
Store per frame field order in index
Browse files Browse the repository at this point in the history
  • Loading branch information
myrsloik committed Mar 7, 2024
1 parent 9b48d0c commit 6067422
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 9 deletions.
3 changes: 2 additions & 1 deletion include/ffms.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#define FFMS_H

// Version format: major - minor - micro - bump
#define FFMS_VERSION ((5 << 24) | (0 << 16) | (0 << 8) | 0)
#define FFMS_VERSION ((5 << 24) | (1 << 16) | (0 << 8) | 0)

#include <stdint.h>
#include <stddef.h>
Expand Down Expand Up @@ -353,6 +353,7 @@ typedef struct FFMS_TrackTimeBase {

typedef struct FFMS_FrameInfo {
int64_t PTS;
int FieldOrder;
int RepeatPict;
int KeyFrame;
int64_t OriginalPTS;
Expand Down
8 changes: 5 additions & 3 deletions src/core/indexing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ void FFMS_Indexer::CheckAudioProperties(int Track, AVCodecContext *Context) {
}
}

void FFMS_Indexer::ParseVideoPacket(SharedAVContext &VideoContext, AVPacket *pkt, int *RepeatPict,
void FFMS_Indexer::ParseVideoPacket(SharedAVContext &VideoContext, AVPacket *pkt, int *FieldOrder, int *RepeatPict,
int *FrameType, bool *Invisible, enum AVPictureStructure *LastPicStruct) {
if (VideoContext.Parser) {
uint8_t *OB;
Expand Down Expand Up @@ -388,6 +388,7 @@ void FFMS_Indexer::ParseVideoPacket(SharedAVContext &VideoContext, AVPacket *pkt
}
}

*FieldOrder = VideoContext.Parser->field_order;
*RepeatPict = VideoContext.Parser->repeat_pict;
*FrameType = VideoContext.Parser->pict_type;
*Invisible = (IncompleteFrame || VideoContext.Parser->repeat_pict < 0 || (pkt->flags & AV_PKT_FLAG_DISCARD));
Expand Down Expand Up @@ -553,12 +554,13 @@ FFMS_Index *FFMS_Indexer::DoIndexing() {
TrackInfo.HasTS = false;
}

int FieldOrder = -1;
int RepeatPict = -1;
int FrameType = 0;
bool Invisible = false;
ParseVideoPacket(AVContexts[Track], Packet, &RepeatPict, &FrameType, &Invisible, &LastPicStruct);
ParseVideoPacket(AVContexts[Track], Packet, &FieldOrder, &RepeatPict, &FrameType, &Invisible, &LastPicStruct);

TrackInfo.AddVideoFrame(PTS, RepeatPict, KeyFrame,
TrackInfo.AddVideoFrame(PTS, FieldOrder, RepeatPict, KeyFrame,
FrameType, Packet->pos, Invisible);
} else if (FormatContext->streams[Track]->codecpar->codec_type == AVMEDIA_TYPE_AUDIO) {
// For video seeking timestamps are used only if all packets have
Expand Down
2 changes: 1 addition & 1 deletion src/core/indexing.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ struct FFMS_Indexer {
void ReadTS(const AVPacket *Packet, int64_t &TS, bool &UseDTS);
void CheckAudioProperties(int Track, AVCodecContext *Context);
uint32_t IndexAudioPacket(int Track, AVPacket *Packet, SharedAVContext &Context, FFMS_Index &TrackIndices);
void ParseVideoPacket(SharedAVContext &VideoContext, AVPacket *pkt, int *RepeatPict, int *FrameType, bool *Invisible, enum AVPictureStructure *LastPicStruct);
void ParseVideoPacket(SharedAVContext &VideoContext, AVPacket *pkt, int *FieldOrder, int *RepeatPict, int *FrameType, bool *Invisible, enum AVPictureStructure *LastPicStruct);
void Free();
public:
FFMS_Indexer(const char *Filename, const FFMS_KeyValuePair *DemuxerOptions, int NumOptions);
Expand Down
8 changes: 5 additions & 3 deletions src/core/track.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ FrameInfo ReadFrame(ZipFile &stream, FrameInfo const& prev, const FFMS_TrackType
f.SampleCount = stream.Read<uint32_t>() + prev.SampleCount;
} else if (TT == FFMS_TYPE_VIDEO) {
f.OriginalPos = static_cast<size_t>(stream.Read<uint64_t>() + prev.OriginalPos + 1);
f.FieldOrder = stream.Read<int32_t>();
f.RepeatPict = stream.Read<int32_t>();
}
return f;
Expand All @@ -64,6 +65,7 @@ static void WriteFrame(ZipFile &stream, FrameInfo const& f, FrameInfo const& pre
stream.Write(f.SampleCount - prev.SampleCount);
else if (TT == FFMS_TYPE_VIDEO) {
stream.Write(static_cast<uint64_t>(f.OriginalPos) - prev.OriginalPos - 1);
stream.Write<int32_t>(f.FieldOrder);
stream.Write<int32_t>(f.RepeatPict);
}
}
Expand Down Expand Up @@ -127,8 +129,8 @@ void FFMS_Track::Write(ZipFile &stream) const {
WriteFrame(stream, Frames[i], i == 0 ? temp : Frames[i - 1], TT);
}

void FFMS_Track::AddVideoFrame(int64_t PTS, int RepeatPict, bool KeyFrame, int FrameType, int64_t FilePos, bool Hidden) {
Data->Frames.push_back({ PTS, 0, FilePos, 0, 0, 0, FrameType, RepeatPict, KeyFrame, Hidden });
void FFMS_Track::AddVideoFrame(int64_t PTS, int FieldOrder, int RepeatPict, bool KeyFrame, int FrameType, int64_t FilePos, bool Hidden) {
Data->Frames.push_back({ PTS, 0, FilePos, 0, 0, 0, FrameType, FieldOrder, RepeatPict, KeyFrame, Hidden });
}

void FFMS_Track::AddAudioFrame(int64_t PTS, int64_t SampleStart, uint32_t SampleCount, bool KeyFrame, int64_t FilePos, bool Hidden) {
Expand Down Expand Up @@ -408,7 +410,7 @@ void FFMS_Track::GeneratePublicInfo() {
continue;
RealFrameNumbers.push_back(static_cast<int>(i));

FFMS_FrameInfo info = { Frames[i].PTS, Frames[i].RepeatPict, Frames[i].KeyFrame, Frames[i].OriginalPTS };
FFMS_FrameInfo info = { Frames[i].PTS, Frames[i].FieldOrder, Frames[i].RepeatPict, Frames[i].KeyFrame, Frames[i].OriginalPTS };
PublicFrameInfo.push_back(info);
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/core/track.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ struct FrameInfo {
uint32_t SampleCount;
size_t OriginalPos;
int FrameType;
int FieldOrder;
int RepeatPict;
bool KeyFrame;
bool Hidden;
Expand Down Expand Up @@ -66,7 +67,7 @@ struct FFMS_Track {
int64_t LastDuration = 0;
int SampleRate = 0; // not persisted

void AddVideoFrame(int64_t PTS, int RepeatPict, bool KeyFrame, int FrameType, int64_t FilePos = 0, bool Invisible = false);
void AddVideoFrame(int64_t PTS, int FieldOrder, int RepeatPict, bool KeyFrame, int FrameType, int64_t FilePos = 0, bool Invisible = false);
void AddAudioFrame(int64_t PTS, int64_t SampleStart, uint32_t SampleCount, bool KeyFrame, int64_t FilePos = 0, bool Invisible = false);

void MaybeHideFrames();
Expand Down

0 comments on commit 6067422

Please sign in to comment.