| 1 |
/* |
| 2 |
* Copyright (C) 2013-2021 Jean-Luc Barrière |
| 3 |
* Copyright (C) 2005-2014 Team Kodi (https://kodi.tv) |
| 4 |
* |
| 5 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
* See LICENSE.md for more information. |
| 7 |
*/ |
| 8 |
|
| 9 |
#include "ES_MPEGVideo.h" |
| 10 |
#include "bitstream.h" |
| 11 |
#include "debug.h" |
| 12 |
|
| 13 |
using namespace TSDemux; |
| 14 |
|
| 15 |
#define MPEG_PICTURE_START 0x00000100 |
| 16 |
#define MPEG_SEQUENCE_START 0x000001b3 |
| 17 |
#define MPEG_SEQUENCE_EXTENSION 0x000001b5 |
| 18 |
#define MPEG_SLICE_S 0x00000101 |
| 19 |
#define MPEG_SLICE_E 0x000001af |
| 20 |
|
| 21 |
#define PKT_I_FRAME 1 |
| 22 |
#define PKT_P_FRAME 2 |
| 23 |
#define PKT_B_FRAME 3 |
| 24 |
#define PKT_NTYPES 4 |
| 25 |
|
| 26 |
/** |
| 27 |
* MPEG2VIDEO frame duration table (in 90kHz clock domain) |
| 28 |
*/ |
| 29 |
const unsigned int mpeg2video_framedurations[16] = { |
| 30 |
0, |
| 31 |
3753, |
| 32 |
3750, |
| 33 |
3600, |
| 34 |
3003, |
| 35 |
3000, |
| 36 |
1800, |
| 37 |
1501, |
| 38 |
1500, |
| 39 |
}; |
| 40 |
|
| 41 |
ES_MPEG2Video::ES_MPEG2Video(uint16_t pid) |
| 42 |
: ElementaryStream(pid) |
| 43 |
{ |
| 44 |
m_FrameDuration = 0; |
| 45 |
m_vbvDelay = -1; |
| 46 |
m_vbvSize = 0; |
| 47 |
m_Height = 0; |
| 48 |
m_Width = 0; |
| 49 |
m_Dar = 0.0f; |
| 50 |
m_DTS = 0; |
| 51 |
m_PTS = 0; |
| 52 |
m_AuDTS = 0; |
| 53 |
m_AuPTS = 0; |
| 54 |
m_AuPrevDTS = 0; |
| 55 |
m_TemporalReference = 0; |
| 56 |
m_TrLastTime = 0; |
| 57 |
m_PicNumber = 0; |
| 58 |
m_FpsScale = 0; |
| 59 |
es_alloc_init = 80000; |
| 60 |
Reset(); |
| 61 |
} |
| 62 |
|
| 63 |
ES_MPEG2Video::~ES_MPEG2Video() |
| 64 |
{ |
| 65 |
} |
| 66 |
|
| 67 |
void ES_MPEG2Video::Parse(STREAM_PKT *pkt) |
| 68 |
{ |
| 69 |
int frame_ptr = es_consumed; |
| 70 |
int p = es_parsed; |
| 71 |
uint32_t startcode = m_StartCode; |
| 72 |
bool frameComplete = false; |
| 73 |
int l; |
| 74 |
while ((l = es_len - p) > 3) |
| 75 |
{ |
| 76 |
if ((startcode & 0xffffff00) == 0x00000100) |
| 77 |
{ |
| 78 |
if (Parse_MPEG2Video(startcode, p, frameComplete) < 0) |
| 79 |
{ |
| 80 |
break; |
| 81 |
} |
| 82 |
} |
| 83 |
startcode = startcode << 8 | es_buf[p++]; |
| 84 |
} |
| 85 |
es_parsed = p; |
| 86 |
m_StartCode = startcode; |
| 87 |
|
| 88 |
if (frameComplete) |
| 89 |
{ |
| 90 |
if (!m_NeedSPS && !m_NeedIFrame) |
| 91 |
{ |
| 92 |
bool streamChange = false; |
| 93 |
if (es_frame_valid) |
| 94 |
{ |
| 95 |
if (m_FpsScale == 0) |
| 96 |
{ |
| 97 |
if (m_FrameDuration > 0) |
| 98 |
m_FpsScale = static_cast<int>(Rescale(m_FrameDuration, RESCALE_TIME_BASE, PTS_TIME_BASE)); |
| 99 |
else |
| 100 |
m_FpsScale = 40000; |
| 101 |
} |
| 102 |
streamChange = SetVideoInformation(m_FpsScale, RESCALE_TIME_BASE, m_Height, m_Width, m_Dar, false); |
| 103 |
} |
| 104 |
|
| 105 |
pkt->pid = pid; |
| 106 |
pkt->size = es_consumed - frame_ptr; |
| 107 |
pkt->data = &es_buf[frame_ptr]; |
| 108 |
pkt->dts = m_DTS; |
| 109 |
pkt->pts = m_PTS; |
| 110 |
pkt->duration = m_FrameDuration; |
| 111 |
pkt->streamChange = streamChange; |
| 112 |
} |
| 113 |
m_StartCode = 0xffffffff; |
| 114 |
es_parsed = es_consumed; |
| 115 |
es_found_frame = false; |
| 116 |
es_frame_valid = true; |
| 117 |
} |
| 118 |
} |
| 119 |
|
| 120 |
void ES_MPEG2Video::Reset() |
| 121 |
{ |
| 122 |
ElementaryStream::Reset(); |
| 123 |
m_StartCode = 0xffffffff; |
| 124 |
m_NeedIFrame = true; |
| 125 |
m_NeedSPS = true; |
| 126 |
} |
| 127 |
|
| 128 |
int ES_MPEG2Video::Parse_MPEG2Video(uint32_t startcode, int buf_ptr, bool &complete) |
| 129 |
{ |
| 130 |
int len = es_len - buf_ptr; |
| 131 |
uint8_t *buf = es_buf + buf_ptr; |
| 132 |
|
| 133 |
switch (startcode & 0xFF) |
| 134 |
{ |
| 135 |
case 0: // picture start |
| 136 |
{ |
| 137 |
if (m_NeedSPS) |
| 138 |
{ |
| 139 |
es_found_frame = true; |
| 140 |
return 0; |
| 141 |
} |
| 142 |
if (es_found_frame) |
| 143 |
{ |
| 144 |
complete = true; |
| 145 |
es_consumed = buf_ptr - 4; |
| 146 |
return -1; |
| 147 |
} |
| 148 |
if (len < 4) |
| 149 |
return -1; |
| 150 |
if (!Parse_MPEG2Video_PicStart(buf)) |
| 151 |
return 0; |
| 152 |
|
| 153 |
if (!es_found_frame) |
| 154 |
{ |
| 155 |
m_AuPrevDTS = m_AuDTS; |
| 156 |
if (buf_ptr - 4 >= (int)es_pts_pointer) |
| 157 |
{ |
| 158 |
m_AuDTS = c_dts != PTS_UNSET ? c_dts : c_pts; |
| 159 |
m_AuPTS = c_pts; |
| 160 |
} |
| 161 |
else |
| 162 |
{ |
| 163 |
m_AuDTS = p_dts != PTS_UNSET ? p_dts : p_pts; |
| 164 |
m_AuPTS = p_pts; |
| 165 |
} |
| 166 |
} |
| 167 |
if (m_AuPrevDTS == m_AuDTS) |
| 168 |
{ |
| 169 |
m_DTS = m_AuDTS + m_PicNumber*m_FrameDuration; |
| 170 |
m_PTS = m_AuPTS + (m_TemporalReference-m_TrLastTime)*m_FrameDuration; |
| 171 |
} |
| 172 |
else |
| 173 |
{ |
| 174 |
m_PTS = m_AuPTS; |
| 175 |
m_DTS = m_AuDTS; |
| 176 |
m_PicNumber = 0; |
| 177 |
m_TrLastTime = m_TemporalReference; |
| 178 |
} |
| 179 |
|
| 180 |
m_PicNumber++; |
| 181 |
es_found_frame = true; |
| 182 |
break; |
| 183 |
} |
| 184 |
|
| 185 |
case 0xb3: // Sequence start code |
| 186 |
{ |
| 187 |
if (es_found_frame) |
| 188 |
{ |
| 189 |
complete = true; |
| 190 |
es_consumed = buf_ptr - 4; |
| 191 |
return -1; |
| 192 |
} |
| 193 |
if (len < 8) |
| 194 |
return -1; |
| 195 |
if (!Parse_MPEG2Video_SeqStart(buf)) |
| 196 |
return 0; |
| 197 |
|
| 198 |
break; |
| 199 |
} |
| 200 |
|
| 201 |
case 0xb7: // sequence end |
| 202 |
{ |
| 203 |
if (es_found_frame) |
| 204 |
{ |
| 205 |
complete = true; |
| 206 |
es_consumed = buf_ptr; |
| 207 |
return -1; |
| 208 |
} |
| 209 |
break; |
| 210 |
} |
| 211 |
|
| 212 |
default: |
| 213 |
break; |
| 214 |
} |
| 215 |
|
| 216 |
return 0; |
| 217 |
} |
| 218 |
|
| 219 |
bool ES_MPEG2Video::Parse_MPEG2Video_SeqStart(uint8_t *buf) |
| 220 |
{ |
| 221 |
CBitstream bs(buf, 8 * 8); |
| 222 |
|
| 223 |
m_Width = bs.readBits(12); |
| 224 |
m_Height = bs.readBits(12); |
| 225 |
|
| 226 |
// figure out Display Aspect Ratio |
| 227 |
uint8_t aspect = bs.readBits(4); |
| 228 |
|
| 229 |
switch(aspect) |
| 230 |
{ |
| 231 |
case 1: |
| 232 |
m_Dar = 1.0f; |
| 233 |
break; |
| 234 |
case 2: |
| 235 |
m_Dar = 4.0f/3.0f; |
| 236 |
break; |
| 237 |
case 3: |
| 238 |
m_Dar = 16.0f/9.0f; |
| 239 |
break; |
| 240 |
case 4: |
| 241 |
m_Dar = 2.21f; |
| 242 |
break; |
| 243 |
default: |
| 244 |
DBG(DEMUX_DBG_ERROR, "invalid / forbidden DAR in sequence header !\n"); |
| 245 |
return false; |
| 246 |
} |
| 247 |
|
| 248 |
m_FrameDuration = mpeg2video_framedurations[bs.readBits(4)]; |
| 249 |
bs.skipBits(18); |
| 250 |
bs.skipBits(1); |
| 251 |
|
| 252 |
m_vbvSize = bs.readBits(10) * 16 * 1024 / 8; |
| 253 |
m_NeedSPS = false; |
| 254 |
|
| 255 |
return true; |
| 256 |
} |
| 257 |
|
| 258 |
bool ES_MPEG2Video::Parse_MPEG2Video_PicStart(uint8_t *buf) |
| 259 |
{ |
| 260 |
CBitstream bs(buf, 4 * 8); |
| 261 |
|
| 262 |
m_TemporalReference = bs.readBits(10); /* temporal reference */ |
| 263 |
|
| 264 |
int pct = bs.readBits(3); |
| 265 |
if (pct < PKT_I_FRAME || pct > PKT_B_FRAME) |
| 266 |
return true; /* Illegal picture_coding_type */ |
| 267 |
|
| 268 |
if (pct == PKT_I_FRAME) |
| 269 |
m_NeedIFrame = false; |
| 270 |
|
| 271 |
int vbvDelay = bs.readBits(16); /* vbv_delay */ |
| 272 |
if (vbvDelay == 0xffff) |
| 273 |
m_vbvDelay = -1; |
| 274 |
else |
| 275 |
m_vbvDelay = vbvDelay; |
| 276 |
|
| 277 |
return true; |
| 278 |
} |