forked from FernetMenta/vdr-plugin-vnsiserver
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_MPEGVideo.c
283 lines (249 loc) · 6.04 KB
/
parser_MPEGVideo.c
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
/*
* vdr-plugin-vnsi - KODI server plugin for VDR
*
* Copyright (C) 2005-2012 Team XBMC
* Copyright (C) 2015 Team KODI
*
* http://kodi.tv
*
* 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, or (at your option)
* any later version.
*
* This Program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with KODI; see the file COPYING. If not, see
* <http://www.gnu.org/licenses/>.
*
*/
#include "parser_MPEGVideo.h"
#include "bitstream.h"
#include "config.h"
#include <stdlib.h>
#include <assert.h>
using namespace std;
#define MPEG_PICTURE_START 0x00000100
#define MPEG_SEQUENCE_START 0x000001b3
#define MPEG_SEQUENCE_EXTENSION 0x000001b5
#define MPEG_SLICE_S 0x00000101
#define MPEG_SLICE_E 0x000001af
/**
* MPEG2VIDEO frame duration table (in 90kHz clock domain)
*/
const unsigned int mpeg2video_framedurations[16] = {
0,
3753,
3750,
3600,
3003,
3000,
1800,
1501,
1500,
};
cParserMPEG2Video::cParserMPEG2Video(int pID, cTSStream *stream, sPtsWrap *ptsWrap, bool observePtsWraps)
: cParser(pID, stream, ptsWrap, observePtsWraps)
{
m_FrameDuration = 0;
m_vbvDelay = -1;
m_vbvSize = 0;
m_Height = 0;
m_Width = 0;
m_Dar = 0.0;
m_FpsScale = 0;
m_PesBufferInitialSize = 80000;
m_IsVideo = true;
Reset();
}
cParserMPEG2Video::~cParserMPEG2Video()
{
}
void cParserMPEG2Video::Parse(sStreamPacket *pkt, sStreamPacket *pkt_side_data)
{
if (m_PesBufferPtr < 4)
return;
int p = m_PesParserPtr;
uint32_t startcode = m_StartCode;
bool frameComplete = false;
int l;
while ((l = m_PesBufferPtr - p) > 3)
{
if ((startcode & 0xffffff00) == 0x00000100)
{
if (Parse_MPEG2Video(startcode, p, frameComplete) < 0)
{
break;
}
}
startcode = startcode << 8 | m_PesBuffer[p++];
}
m_PesParserPtr = p;
m_StartCode = startcode;
if (frameComplete)
{
if (!m_NeedSPS && !m_NeedIFrame && m_FrameValid)
{
if (m_FpsScale == 0)
{
if (m_FrameDuration != DVD_NOPTS_VALUE)
m_FpsScale = m_Stream->Rescale(m_FrameDuration, DVD_TIME_BASE, 90000);
else
m_FpsScale = 40000;
}
bool streamChange = m_Stream->SetVideoInformation(m_FpsScale, DVD_TIME_BASE, m_Height, m_Width, m_Dar);
pkt->id = m_pID;
pkt->size = m_PesNextFramePtr;
pkt->data = m_PesBuffer;
pkt->dts = m_DTS;
pkt->pts = m_PTS;
pkt->duration = m_FrameDuration;
pkt->streamChange = streamChange;
}
m_StartCode = 0xffffffff;
m_PesParserPtr = 0;
m_FoundFrame = false;
m_FrameValid = true;
}
}
void cParserMPEG2Video::Reset()
{
cParser::Reset();
m_StartCode = 0xffffffff;
m_NeedIFrame = true;
m_NeedSPS = true;
}
int cParserMPEG2Video::Parse_MPEG2Video(uint32_t startcode, int buf_ptr, bool &complete)
{
int len = m_PesBufferPtr - buf_ptr;
uint8_t *buf = m_PesBuffer + buf_ptr;
switch (startcode & 0xFF)
{
case 0: // picture start
{
if (m_NeedSPS)
{
m_FoundFrame = true;
return 0;
}
if (m_FoundFrame)
{
complete = true;
m_PesNextFramePtr = buf_ptr - 4;
return -1;
}
if (len < 4)
return -1;
if (!Parse_MPEG2Video_PicStart(buf))
return 0;
if (!m_FoundFrame)
{
m_AuPrevDTS = m_AuDTS;
if (buf_ptr - 4 >= m_PesTimePos)
{
m_AuDTS = m_curDTS != DVD_NOPTS_VALUE ? m_curDTS : m_curPTS;
m_AuPTS = m_curPTS;
}
else
{
m_AuDTS = m_prevDTS != DVD_NOPTS_VALUE ? m_prevDTS : m_prevPTS;;
m_AuPTS = m_prevPTS;
}
}
if (m_AuPrevDTS == m_AuDTS)
{
m_DTS = m_AuDTS + m_PicNumber*m_FrameDuration;
m_PTS = m_AuPTS + (m_TemporalReference-m_TrLastTime)*m_FrameDuration;
}
else
{
m_PTS = m_AuPTS;
m_DTS = m_AuDTS;
m_PicNumber = 0;
m_TrLastTime = m_TemporalReference;
}
m_PicNumber++;
m_FoundFrame = true;
break;
}
case 0xb3: // Sequence start code
{
if (m_FoundFrame)
{
complete = true;
m_PesNextFramePtr = buf_ptr - 4;
return -1;
}
if (len < 8)
return -1;
if (!Parse_MPEG2Video_SeqStart(buf))
return 0;
break;
}
case 0xb7: // sequence end
{
if (m_FoundFrame)
{
complete = true;
m_PesNextFramePtr = buf_ptr;
return -1;
}
break;
}
default:
break;
}
return 0;
}
bool cParserMPEG2Video::Parse_MPEG2Video_SeqStart(uint8_t *buf)
{
cBitstream bs(buf, 8 * 8);
m_Width = bs.readBits(12);
m_Height = bs.readBits(12);
// figure out Display Aspect Ratio
uint8_t aspect = bs.readBits(4);
switch(aspect)
{
case 1:
m_Dar = 1.0;
break;
case 2:
m_Dar = 4.0/3.0;
break;
case 3:
m_Dar = 16.0/9.0;
break;
case 4:
m_Dar = 2.21;
break;
default:
ERRORLOG("invalid / forbidden DAR in sequence header !");
return false;
}
m_FrameDuration = mpeg2video_framedurations[bs.readBits(4)];
bs.skipBits(18);
bs.skipBits(1);
m_vbvSize = bs.readBits(10) * 16 * 1024 / 8;
m_NeedSPS = false;
return true;
}
bool cParserMPEG2Video::Parse_MPEG2Video_PicStart(uint8_t *buf)
{
cBitstream bs(buf, 4 * 8);
m_TemporalReference = bs.readBits(10); /* temporal reference */
int pct = bs.readBits(3);
if (pct < PKT_I_FRAME || pct > PKT_B_FRAME)
return true; /* Illegal picture_coding_type */
if (pct == PKT_I_FRAME)
m_NeedIFrame = false;
int vbvDelay = bs.readBits(16); /* vbv_delay */
if (vbvDelay == 0xffff)
m_vbvDelay = -1;
else
m_vbvDelay = vbvDelay;
return true;
}