1 //  Copyright (c) 2007-2017 Fredrik Mellbin
2 //
3 //  Permission is hereby granted, free of charge, to any person obtaining a copy
4 //  of this software and associated documentation files (the "Software"), to deal
5 //  in the Software without restriction, including without limitation the rights
6 //  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 //  copies of the Software, and to permit persons to whom the Software is
8 //  furnished to do so, subject to the following conditions:
9 //
10 //  The above copyright notice and this permission notice shall be included in
11 //  all copies or substantial portions of the Software.
12 //
13 //  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 //  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 //  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 //  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 //  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 //  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 //  THE SOFTWARE.
20 
21 #ifndef INDEXING_H
22 #define INDEXING_H
23 
24 #include "utils.h"
25 
26 #include <set>
27 #include <map>
28 #include <memory>
29 #include <atomic>
30 
31 extern "C" {
32 #include <libavutil/avutil.h>
33 }
34 
35 class Wave64Writer;
36 class ZipFile;
37 
38 struct SharedAVContext {
39     AVCodecContext *CodecContext = nullptr;
40     AVCodecParserContext *Parser = nullptr;
41     int64_t CurrentSample = 0;
42     ~SharedAVContext();
43 };
44 
45 struct FFMS_Index : public std::vector<FFMS_Track> {
46     FFMS_Index(FFMS_Index const&) = delete;
47     FFMS_Index& operator=(FFMS_Index const&) = delete;
48     void ReadIndex(ZipFile &zf, const char* IndexFile);
49     void WriteIndex(ZipFile &zf);
50 public:
51     static void CalculateFileSignature(const char *Filename, int64_t *Filesize, uint8_t Digest[20]);
52 
53     int ErrorHandling;
54     int64_t Filesize;
55     uint8_t Digest[20];
56 
57     void Finalize(std::vector<SharedAVContext> const& video_contexts, const char *Format);
58     bool CompareFileSignature(const char *Filename);
59     void WriteIndexFile(const char *IndexFile);
60     uint8_t *WriteIndexBuffer(size_t *Size);
61 
62     FFMS_Index(const char *IndexFile);
63     FFMS_Index(const uint8_t *Buffer, size_t Size);
64     FFMS_Index(int64_t Filesize, uint8_t Digest[20], int ErrorHandling);
65 };
66 
67 struct FFMS_Indexer {
68 private:
69     std::map<int, FFMS_AudioProperties> LastAudioProperties;
70     FFMS_Indexer(FFMS_Indexer const&) = delete;
71     FFMS_Indexer& operator=(FFMS_Indexer const&) = delete;
72     AVFormatContext *FormatContext = nullptr;
73     std::set<int> IndexMask;
74     int ErrorHandling = FFMS_IEH_CLEAR_TRACK;
75     TIndexCallback IC = nullptr;
76     void *ICPrivate = nullptr;
77     std::string SourceFile;
78     AVFrame *DecodeFrame = nullptr;
79 
80     int64_t Filesize;
81     uint8_t Digest[20];
82 
83     void ReadTS(const AVPacket &Packet, int64_t &TS, bool &UseDTS);
84     void CheckAudioProperties(int Track, AVCodecContext *Context);
85     uint32_t IndexAudioPacket(int Track, AVPacket *Packet, SharedAVContext &Context, FFMS_Index &TrackIndices);
86     void ParseVideoPacket(SharedAVContext &VideoContext, AVPacket &pkt, int *RepeatPict, int *FrameType, bool *Invisible, enum AVPictureStructure *LastPicStruct);
87     void Free();
88 public:
89     FFMS_Indexer(const char *Filename);
90     ~FFMS_Indexer();
91 
92     void SetIndexTrack(int Track, bool Index);
93     void SetIndexTrackType(int TrackType, bool Index);
94     void SetErrorHandling(int ErrorHandling_);
95     void SetProgressCallback(TIndexCallback IC_, void *ICPrivate_);
96 
97     FFMS_Index *DoIndexing();
98     int GetNumberOfTracks();
99     FFMS_TrackType GetTrackType(int Track);
100     const char *GetTrackCodec(int Track);
101     const char *GetFormatName();
102 };
103 
104 
105 #endif
106