File: elementaryStream.cpp

package info (click to toggle)
kodi-inputstream-adaptive 2.6.14%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 4,036 kB
  • sloc: cpp: 53,019; ansic: 492; makefile: 10
file content (301 lines) | stat: -rw-r--r-- 7,526 bytes parent folder | download
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
/*
 *      Copyright (C) 2013 Jean-Luc Barriere
 *
 *  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 this program; see the file COPYING.  If not, write to
 *  the Free Software Foundation, 51 Franklin Street, Fifth Floor, Boston,
 *  MA 02110-1301 USA
 *  http://www.gnu.org/copyleft/gpl.html
 *
 */

#include "elementaryStream.h"
#include "debug.h"

#include <cstdlib>    // for malloc free size_t
#include <cstring>    // memset memcpy memmove
#include <climits>    // for INT_MAX
#include <cerrno>

using namespace TSDemux;

ElementaryStream::ElementaryStream(uint16_t pes_pid)
  : pid(pes_pid)
  , stream_type(STREAM_TYPE_UNKNOWN)
  , c_dts(PTS_UNSET)
  , c_pts(PTS_UNSET)
  , p_dts(PTS_UNSET)
  , p_pts(PTS_UNSET)
  , has_stream_info(false)
  , es_alloc_init(ES_INIT_BUFFER_SIZE)
  , es_buf(NULL)
  , es_alloc(0)
  , es_len(0)
  , es_consumed(0)
  , es_pts_pointer(0)
  , es_parsed(0)
  , es_found_frame(false)
  , es_frame_valid(false)
  , es_extraDataChanged(false)
{
  memset(&stream_info, 0, sizeof(STREAM_INFO));
}

ElementaryStream::~ElementaryStream(void)
{
  if (es_buf)
  {
    DBG(DEMUX_DBG_DEBUG, "free stream buffer %.4x: allocated size was %zu\n", pid, es_alloc);
    free(es_buf);
    es_buf = NULL;
  }
}

void ElementaryStream::Reset(void)
{
  ClearBuffer();
  es_found_frame = false;
  es_frame_valid = false;
}

void ElementaryStream::ClearBuffer()
{
  es_len = es_consumed = es_pts_pointer = es_parsed = 0;
}

int ElementaryStream::Append(const unsigned char* buf, size_t len, bool new_pts)
{
  // Mark position where current pts become applicable
  if (new_pts)
    es_pts_pointer = es_len;

  if (es_buf && es_consumed)
  {
    if (es_consumed < es_len)
    {
      memmove(es_buf, es_buf + es_consumed, es_len - es_consumed);
      es_len -= es_consumed;
      es_parsed -= es_consumed;
      if (es_pts_pointer > es_consumed)
        es_pts_pointer -= es_consumed;
      else
        es_pts_pointer = 0;

      es_consumed = 0;
    }
    else
      ClearBuffer();
  }
  if (es_len + len > es_alloc)
  {
    if (es_alloc >= ES_MAX_BUFFER_SIZE)
      return -ENOMEM;

    size_t n = (es_alloc ? (es_alloc + len) * 2 : es_alloc_init);
    if (n > ES_MAX_BUFFER_SIZE)
      n = ES_MAX_BUFFER_SIZE;

    DBG(DEMUX_DBG_DEBUG, "realloc buffer size to %zu for stream %.4x\n", n, pid);
    unsigned char* p = es_buf;
    es_buf = (unsigned char*)realloc(es_buf, n * sizeof(*es_buf));
    if (es_buf)
    {
      es_alloc = n;
    }
    else
    {
      free(p);
      es_alloc = 0;
      es_len = 0;
      return -ENOMEM;
    }
  }

  if (!es_buf)
    return -ENOMEM;

  memcpy(es_buf + es_len, buf, len);
  es_len += len;

  return 0;
}

const char* ElementaryStream::GetStreamCodecName(STREAM_TYPE stream_type)
{
  switch (stream_type)
  {
    case STREAM_TYPE_VIDEO_MPEG1:
      return "mpeg1video";
    case STREAM_TYPE_VIDEO_MPEG2:
      return "mpeg2video";
    case STREAM_TYPE_AUDIO_MPEG1:
      return "mp1";
    case STREAM_TYPE_AUDIO_MPEG2:
      return "mp2";
    case STREAM_TYPE_AUDIO_AAC:
      return "aac";
    case STREAM_TYPE_AUDIO_AAC_ADTS:
      return "aac";
    case STREAM_TYPE_AUDIO_AAC_LATM:
      return "aac_latm";
    case STREAM_TYPE_VIDEO_H264:
      return "h264";
    case STREAM_TYPE_VIDEO_HEVC:
      return "hevc";
    case STREAM_TYPE_AUDIO_AC3:
      return "ac3";
    case STREAM_TYPE_AUDIO_EAC3:
      return "eac3";
    case STREAM_TYPE_DVB_TELETEXT:
      return "teletext";
    case STREAM_TYPE_DVB_SUBTITLE:
      return "dvbsub";
    case STREAM_TYPE_VIDEO_MPEG4:
      return "mpeg4video";
    case STREAM_TYPE_VIDEO_VC1:
      return "vc1";
    case STREAM_TYPE_AUDIO_LPCM:
      return "lpcm";
    case STREAM_TYPE_AUDIO_DTS:
      return "dts";
    case STREAM_TYPE_PRIVATE_DATA:
    default:
      return "data";
  }
}

const char* ElementaryStream::GetStreamCodecName() const
{
  return GetStreamCodecName(stream_type);
}

bool ElementaryStream::GetStreamPacket(STREAM_PKT* pkt)
{
  ResetStreamPacket(pkt);
  Parse(pkt);
  if (pkt->data)
    return true;
  return false;
}

void ElementaryStream::Parse(STREAM_PKT* pkt)
{
  // No parser: pass-through
  if (es_consumed < es_len)
  {
    es_consumed = es_parsed = es_len;
    pkt->pid              = pid;
    pkt->size             = es_consumed;
    pkt->data             = es_buf;
    pkt->dts              = c_dts;
    pkt->pts              = c_pts;
    if (c_dts == PTS_UNSET || p_dts == PTS_UNSET)
      pkt->duration       = 0;
    else
      pkt->duration       = c_dts - p_dts;
    pkt->streamChange     = false;
  }
}

void ElementaryStream::ResetStreamPacket(STREAM_PKT* pkt)
{
  pkt->pid                = 0xffff;
  pkt->size               = 0;
  pkt->data               = NULL;
  pkt->dts                = PTS_UNSET;
  pkt->pts                = PTS_UNSET;
  pkt->duration           = 0;
  pkt->streamChange       = false;
  pkt->recoveryPoint      = false;
}

uint64_t ElementaryStream::Rescale(uint64_t a, uint64_t b, uint64_t c)
{
  uint64_t r = c / 2;

  if (b <= INT_MAX && c <= INT_MAX)
  {
    if (a <= INT_MAX)
      return (a * b + r) / c;
    else
      return a / c * b + (a % c * b + r) / c;
  }
  else
  {
    uint64_t a0 = a & 0xFFFFFFFF;
    uint64_t a1 = a >> 32;
    uint64_t b0 = b & 0xFFFFFFFF;
    uint64_t b1 = b >> 32;
    uint64_t t1 = a0 * b1 + a1 * b0;
    uint64_t t1a = t1 << 32;

    a0 = a0 * b0 + t1a;
    a1 = a1 * b1 + (t1 >> 32) + (a0 < t1a);
    a0 += r;
    a1 += a0 < r;

    for (int i = 63; i >= 0; i--)
    {
      a1 += a1 + ((a0 >> i) & 1);
      t1 += t1;
      if (c <= a1)
      {
        a1 -= c;
        t1++;
      }
    }
    return t1;
  }
}

bool ElementaryStream::SetVideoInformation(int FpsScale, int FpsRate, int Height, int Width, float Aspect, bool Interlaced)
{
  bool ret = false;
  if ((stream_info.fps_scale != FpsScale) ||
      (stream_info.fps_rate != FpsRate) ||
      (stream_info.height != Height) ||
      (stream_info.width != Width) ||
      (stream_info.aspect != Aspect) ||
      (stream_info.interlaced != Interlaced))
    ret = true;

  stream_info.fps_scale       = FpsScale;
  stream_info.fps_rate        = FpsRate;
  stream_info.height          = Height;
  stream_info.width           = Width;
  stream_info.aspect          = Aspect;
  stream_info.interlaced      = Interlaced;

  has_stream_info = true;
  return ret;
}

bool ElementaryStream::SetAudioInformation(int Channels, int SampleRate, int BitRate, int BitsPerSample, int BlockAlign)
{
  bool ret = false;
  if ((stream_info.channels != Channels) ||
      (stream_info.sample_rate != SampleRate) ||
      (stream_info.block_align != BlockAlign) ||
      (stream_info.bit_rate != BitRate) ||
      (stream_info.bits_per_sample != BitsPerSample))
    ret = true;

  stream_info.channels          = Channels;
  stream_info.sample_rate       = SampleRate;
  stream_info.block_align       = BlockAlign;
  stream_info.bit_rate          = BitRate;
  stream_info.bits_per_sample   = BitsPerSample;

  has_stream_info = true;
  return ret;
}