/
ADM_tsIndex.h
347 lines (316 loc) · 11.9 KB
/
ADM_tsIndex.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/***************************************************************************
Mpeg2 in PS indexer
VC1: /!\ Escaping not done (yet)
copyright : (C) 2005/2009 by mean
email : fixounet@free.fr
***************************************************************************/
/***************************************************************************
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
***************************************************************************/
#ifndef ADM_TS_INDEX_H
#define ADM_TS_INDEX_H
#include "ADM_cpp.h"
using std::string;
#include "ADM_default.h"
#include "ADM_demuxerInternal.h"
#include "fourcc.h"
#include "dmxTSPacket.h"
#include "ADM_quota.h"
#include "ADM_tsAudioProbe.h"
#include "DIA_processing.h"
#include "ADM_tsPatPmt.h"
#include "ADM_videoInfoExtractor.h"
#include "ADM_h264_tag.h"
#include "ADM_clock.h"
#include "ADM_indexFile.h"
#include "ADM_getbits.h"
#include "ADM_tsGetBits.h"
#include "ADM_coreUtils.h"
#if (1) || !defined(ADM_DEBUG)
#define aprintf(...) {}
#else
#define aprintf printf
#endif
static const char Structure[6]={'X','T','B','F','C','S'}; // Invalid, Top, Bottom, Frame, Frame+TFF, Frame+BFF
static const char Type[5]={'X','I','P','B','D'};
#define VC1_MAX_SEQ_SIZE 64
#if 1
#define ASK_APPEND_SEQUENCED
#endif
class TSVideo
{
public:
TSVideo(void) {w=h=fps=interlaced=ar=pid=frameCount=fieldCount=0;extraDataLength=0;}
uint32_t w;
uint32_t h;
uint32_t fps;
uint32_t interlaced;
uint32_t ar;
uint32_t pid;
uint32_t frameCount;
uint32_t fieldCount;
uint32_t extraDataLength;
uint8_t extraData[VC1_MAX_SEQ_SIZE];
};
typedef enum
{
pictureFrame=3,
pictureFieldTop=1,
pictureFieldBottom=2,
pictureTopFirst=4,
pictureBottomFirst=5
}pictureStructure;
typedef struct
{
uint64_t pts,dts; //startAt;
//uint32_t offset;
uint32_t frameType;
pictureStructure picStructure;
uint32_t nbPics;
tsPacketLinear *pkt;
int32_t nextOffset;
uint64_t beginPts,beginDts;
uint64_t prevPts,prevDts;
}indexerData;
/**
\class VC1Context
*/
class VC1Context
{
public:
bool advanced;
bool interlaced;
bool interpolate;
VC1Context() {advanced=false;interlaced=false;interpolate=false;}
};
/**
\class H264Unit
*/
class H264Unit
{
public:
int unitType;
dmxPacketInfo packetInfo;
uint64_t consumedSoFar;
uint32_t overRead;
int imageType;
pictureStructure imageStructure;
uint32_t recoveryCount;
H264Unit() {memset(this,0,sizeof(*this));recoveryCount=0xff;imageStructure=pictureFrame;}
};
enum
{
unitTypeSei=1,
unitTypePic=2,
unitTypeSps=3,
unitTypePicInfo=4
};
/**
\class TsIndexer
*/
class TsIndexerBase
{
protected:
uint32_t beginConsuming;
uint64_t fullSize;
vector <H264Unit> listOfUnits;
H264Unit thisUnit;
bool decodingImage;
int processedThisRound;
protected:
FILE *index;
tsPacketLinearTracker *pkt;
listOfTsAudioTracks *audioTracks;
DIA_processingBase *gui;
bool updateUI(void);
// H264
bool addUnit(indexerData &data,int unitType,const H264Unit &unit,uint32_t overRead);
bool dumpUnits(indexerData &data,uint64_t nextConsumed,const dmxPacketInfo *nextPacket);
public:
TsIndexerBase(listOfTsAudioTracks *tr);
virtual ~TsIndexerBase();
virtual uint8_t run(const char *file,ADM_TS_TRACK *videoTrac)=0;
bool writeVideo(TSVideo *video,ADM_TS_TRACK_TYPE trkType);
bool writeAudio(void);
bool writeSystem(const char *filename,bool append=false);
bool updateLastUnitStructure(int structure);
bool updatePicStructure(TSVideo &video,const uint32_t t)
{
switch(t)
{
case 3: video.frameCount++;
thisUnit.imageStructure=pictureFrame;
break;
case 1: thisUnit.imageStructure=pictureFieldTop;
video.fieldCount++;
break;
case 2: thisUnit.imageStructure=pictureFieldBottom;
video.fieldCount++;
break;
case 4: video.frameCount++;
thisUnit.imageStructure=pictureTopFirst;
break;
case 5: video.frameCount++;
thisUnit.imageStructure=pictureBottomFirst;
break;
default: ADM_warning("frame type 0 met, this is illegal\n");
}
return true;
}
};
/**
\class TsIndexerH264
*/
class TsIndexerH264 : public TsIndexerBase
{
protected:
bool findH264SPS(tsPacketLinearTracker *pkt,TSVideo &video);
protected:
ADM_SPSInfo spsInfo;
uint8_t decodeSEI(uint32_t nalSize, uint8_t *org,uint32_t *recoveryLength,pictureStructure *nextpicstruct);
#define ADM_NAL_BUFFER_SIZE (2*1024) // only used to decode SEI, should plenty enough
uint8_t payloadBuffer[ADM_NAL_BUFFER_SIZE];
public:
TsIndexerH264(listOfTsAudioTracks *tr) : TsIndexerBase(tr)
{
memset(&spsInfo,0,sizeof(spsInfo));
}
~TsIndexerH264()
{
}
uint8_t run(const char *file,ADM_TS_TRACK *videoTrac);
};
class TsIndexerVC1: public TsIndexerBase
{
protected:
VC1Context vc1Context;
protected:
bool decodeVC1Seq(tsGetBits &bits,TSVideo &video);
bool decodeVC1Pic(tsGetBits &bits,uint32_t &frameType,uint32_t &frameStructure);
public:
~TsIndexerVC1()
{
}
uint8_t run(const char *file,ADM_TS_TRACK *videoTrac);
TsIndexerVC1(listOfTsAudioTracks *tr) : TsIndexerBase(tr)
{
}
};
//--
/**
*
\class TsIndexerH264
*/
class TsIndexerMpeg2 : public TsIndexerBase
{
protected:
public:
~TsIndexerMpeg2()
{
}
uint8_t run(const char *file,ADM_TS_TRACK *videoTrac);
TsIndexerMpeg2(listOfTsAudioTracks *tr) : TsIndexerBase(tr)
{
}
};
//--
/**
*
\class TsIndexerH264
*/
class TsIndexerH265 : public TsIndexerBase
{
protected:
ADM_SPSinfoH265 info;
bool findH265VPS(tsPacketLinearTracker *pkt,TSVideo &video);
bool decodeH265SPS(tsPacketLinearTracker *pkt);
int decodePictureTypeH265(int nalType,getBits &bits) ;
bool decodeSEIH265(uint32_t nalSize, uint8_t *org,uint32_t *recoveryLength, pictureStructure *picStruct);
protected:
public:
~TsIndexerH265()
{
}
uint8_t run(const char *file,ADM_TS_TRACK *videoTrac);
TsIndexerH265(listOfTsAudioTracks *tr) : TsIndexerBase(tr)
{
}
};
//---
#if 0
/**
*
\class TsIndexerH264
*/
class TsIndexer
{
protected:
uint32_t beginConsuming;
uint64_t fullSize;
VC1Context vc1Context;
vector <H264Unit> listOfUnits;
H264Unit thisUnit;
bool decodingImage;
int processedThisRound;
ADM_SPSinfoH265 info;
bool findH264SPS(tsPacketLinearTracker *pkt,TSVideo &video);
bool findH265VPS(tsPacketLinearTracker *pkt,TSVideo &video);
bool decodeH265SPS(tsPacketLinearTracker *pkt);
int decodePictureTypeH265(int nalType,getBits &bits) ;
protected:
FILE *index;
tsPacketLinearTracker *pkt;
listOfTsAudioTracks *audioTracks;
DIA_processingBase *gui;
ADM_SPSInfo spsInfo;
bool updateUI(void);
bool decodeSEI(uint32_t nalSize, uint8_t *org,uint32_t *recoveryLength,pictureStructure *nextpicstruct);
bool decodeSEIH265(uint32_t nalSize, uint8_t *org,uint32_t *recoveryLength,pictureStructure *nextpicstruct);
bool decodeVC1Seq(tsGetBits &bits,TSVideo &video);
bool decodeVC1Pic(tsGetBits &bits,uint32_t &frameType,uint32_t &frameStructure);
// H264
bool addUnit(indexerData &data,int unitType,const H264Unit &unit,uint32_t overRead);
bool dumpUnits(indexerData &data,uint64_t nextConsumed,const dmxPacketInfo *nextPacket);
#define ADM_NAL_BUFFER_SIZE (2*1024) // only used to decode SEI, should plenty enough
uint8_t payloadBuffer[ADM_NAL_BUFFER_SIZE];
public:
TsIndexer(listOfTsAudioTracks *tr);
~TsIndexer();
bool runMpeg2(const char *file,ADM_TS_TRACK *videoTrac);
bool runH264(const char *file,ADM_TS_TRACK *videoTrac);
bool runH265(const char *file,ADM_TS_TRACK *videoTrac);
bool runVC1(const char *file,ADM_TS_TRACK *videoTrac);
bool writeVideo(TSVideo *video,ADM_TS_TRACK_TYPE trkType);
bool writeAudio(void);
bool writeSystem(const char *filename,bool append);
bool updatePicStructure(TSVideo &video,const uint32_t t)
{
switch(t)
{
case 3: video.frameCount++;
thisUnit.imageStructure=pictureFrame;
break;
case 1: thisUnit.imageStructure=pictureFieldTop;
video.fieldCount++;
break;
case 2: thisUnit.imageStructure=pictureFieldBottom;
video.fieldCount++;
break;
default: ADM_warning("frame type 0 met, this is illegal\n");
}
return true;
}
};
#endif
/********************************************************************************************/
/********************************************************************************************/
/********************************************************************************************/
/********************************************************************************************/
//
#endif
//EOF