summaryrefslogtreecommitdiffstats
path: root/xbmc/utils/BitstreamReader.cpp
diff options
context:
space:
mode:
authormanuel <manuel@mausz.at>2020-10-19 00:52:24 +0200
committermanuel <manuel@mausz.at>2020-10-19 00:52:24 +0200
commitbe933ef2241d79558f91796cc5b3a161f72ebf9c (patch)
treefe3ab2f130e20c99001f2d7a81d610c78c96a3f4 /xbmc/utils/BitstreamReader.cpp
parent5f8335c1e49ce108ef3481863833c98efa00411b (diff)
downloadkodi-pvr-build-be933ef2241d79558f91796cc5b3a161f72ebf9c.tar.gz
kodi-pvr-build-be933ef2241d79558f91796cc5b3a161f72ebf9c.tar.bz2
kodi-pvr-build-be933ef2241d79558f91796cc5b3a161f72ebf9c.zip
sync with upstream
Diffstat (limited to 'xbmc/utils/BitstreamReader.cpp')
-rw-r--r--xbmc/utils/BitstreamReader.cpp96
1 files changed, 96 insertions, 0 deletions
diff --git a/xbmc/utils/BitstreamReader.cpp b/xbmc/utils/BitstreamReader.cpp
new file mode 100644
index 0000000..6f2a7ff
--- /dev/null
+++ b/xbmc/utils/BitstreamReader.cpp
@@ -0,0 +1,96 @@
1/*
2 * Copyright (C) 2017-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
4 *
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
8
9#include "BitstreamReader.h"
10
11CBitstreamReader::CBitstreamReader(const uint8_t *buf, int len)
12 : buffer(buf)
13 , start(buf)
14 , offbits(0)
15 , length(len)
16 , oflow(0)
17{
18}
19
20uint32_t CBitstreamReader::ReadBits(int nbits)
21{
22 uint32_t ret = GetBits(nbits);
23
24 offbits += nbits;
25 buffer += offbits / 8;
26 offbits %= 8;
27
28 return ret;
29}
30
31void CBitstreamReader::SkipBits(int nbits)
32{
33 offbits += nbits;
34 buffer += offbits / 8;
35 offbits %= 8;
36
37 if (buffer > (start + length))
38 oflow = 1;
39}
40
41uint32_t CBitstreamReader::GetBits(int nbits)
42{
43 int i, nbytes;
44 uint32_t ret = 0;
45 const uint8_t *buf;
46
47 buf = buffer;
48 nbytes = (offbits + nbits) / 8;
49
50 if (((offbits + nbits) % 8) > 0)
51 nbytes++;
52
53 if ((buf + nbytes) > (start + length))
54 {
55 oflow = 1;
56 return 0;
57 }
58 for (i = 0; i<nbytes; i++)
59 ret += buf[i] << ((nbytes - i - 1) * 8);
60
61 i = (4 - nbytes) * 8 + offbits;
62
63 ret = ((ret << i) >> i) >> ((nbytes * 8) - nbits - offbits);
64
65 return ret;
66}
67
68const uint8_t* find_start_code(const uint8_t *p, const uint8_t *end, uint32_t *state)
69{
70 if (p >= end)
71 return end;
72
73 for (int i = 0; i < 3; i++)
74 {
75 uint32_t tmp = *state << 8;
76 *state = tmp + *(p++);
77 if (tmp == 0x100 || p == end)
78 return p;
79 }
80
81 while (p < end)
82 {
83 if (p[-1] > 1) p += 3;
84 else if (p[-2]) p += 2;
85 else if (p[-3] | (p[-1] - 1)) p++;
86 else {
87 p++;
88 break;
89 }
90 }
91
92 p = (p < end)? p - 4 : end - 4;
93 *state = BS_RB32(p);
94
95 return p + 4;
96}