/src/x265/source/encoder/sei.cpp
Line | Count | Source (jump to first uncovered line) |
1 | | /***************************************************************************** |
2 | | * Copyright (C) 2013-2020 MulticoreWare, Inc |
3 | | * |
4 | | * Authors: Steve Borho <steve@borho.org> |
5 | | * |
6 | | * This program is free software; you can redistribute it and/or modify |
7 | | * it under the terms of the GNU General Public License as published by |
8 | | * the Free Software Foundation; either version 2 of the License, or |
9 | | * (at your option) any later version. |
10 | | * |
11 | | * This program is distributed in the hope that it will be useful, |
12 | | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | * GNU General Public License for more details. |
15 | | * |
16 | | * You should have received a copy of the GNU General Public License |
17 | | * along with this program; if not, write to the Free Software |
18 | | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111, USA. |
19 | | * |
20 | | * This program is also available under a commercial proprietary license. |
21 | | * For more information, contact us at license @ x265.com. |
22 | | *****************************************************************************/ |
23 | | |
24 | | #include "common.h" |
25 | | #include "bitstream.h" |
26 | | #include "slice.h" |
27 | | #include "sei.h" |
28 | | |
29 | | using namespace X265_NS; |
30 | | |
31 | | /* x265's identifying GUID */ |
32 | | const uint8_t SEIuserDataUnregistered::m_uuid_iso_iec_11578[16] = { |
33 | | 0x2C, 0xA2, 0xDE, 0x09, 0xB5, 0x17, 0x47, 0xDB, |
34 | | 0xBB, 0x55, 0xA4, 0xFE, 0x7F, 0xC2, 0xFC, 0x4E |
35 | | }; |
36 | | |
37 | | /* marshal a single SEI message sei, storing the marshalled representation |
38 | | * in bitstream bs */ |
39 | | void SEI::writeSEImessages(Bitstream& bs, const SPS& sps, NalUnitType nalUnitType, NALList& list, int isNested) |
40 | 0 | { |
41 | 0 | if (!isNested) |
42 | 0 | bs.resetBits(); |
43 | |
|
44 | 0 | BitCounter counter; |
45 | 0 | m_bitIf = &counter; |
46 | 0 | writeSEI(sps); |
47 | | /* count the size of the payload and return the size in bits */ |
48 | 0 | X265_CHECK(0 == (counter.getNumberOfWrittenBits() & 7), "payload unaligned\n"); |
49 | 0 | uint32_t payloadData = counter.getNumberOfWrittenBits() >> 3; |
50 | | |
51 | | // set bitstream |
52 | 0 | m_bitIf = &bs; |
53 | |
|
54 | 0 | uint32_t payloadType = m_payloadType; |
55 | 0 | for (; payloadType >= 0xff; payloadType -= 0xff) |
56 | 0 | WRITE_CODE(0xff, 8, "payload_type"); |
57 | 0 | WRITE_CODE(payloadType, 8, "payload_type"); |
58 | |
|
59 | 0 | uint32_t payloadSize = payloadData; |
60 | 0 | for (; payloadSize >= 0xff; payloadSize -= 0xff) |
61 | 0 | WRITE_CODE(0xff, 8, "payload_size"); |
62 | 0 | WRITE_CODE(payloadSize, 8, "payload_size"); |
63 | | |
64 | | // virtual writeSEI method, write to bs |
65 | 0 | writeSEI(sps); |
66 | |
|
67 | 0 | if (!isNested) |
68 | 0 | { |
69 | 0 | if (nalUnitType != NAL_UNIT_UNSPECIFIED) |
70 | 0 | bs.writeByteAlignment(); |
71 | 0 | list.serialize(nalUnitType, bs); |
72 | 0 | } |
73 | 0 | } |
74 | | |
75 | | void SEI::writeByteAlign() |
76 | 0 | { |
77 | | // TODO: expose bs.writeByteAlignment() as virtual function |
78 | 0 | if (m_bitIf->getNumberOfWrittenBits() % 8 != 0) |
79 | 0 | { |
80 | 0 | WRITE_FLAG(1, "bit_equal_to_one"); |
81 | 0 | while (m_bitIf->getNumberOfWrittenBits() % 8 != 0) |
82 | 0 | { |
83 | 0 | WRITE_FLAG(0, "bit_equal_to_zero"); |
84 | 0 | } |
85 | 0 | } |
86 | 0 | } |
87 | | |
88 | | void SEI::setSize(uint32_t size) |
89 | 0 | { |
90 | 0 | m_payloadSize = size; |
91 | 0 | } |
92 | | |
93 | | /* charSet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" */ |
94 | | |
95 | | char* SEI::base64Decode(char encodedString[], int base64EncodeLength) |
96 | 0 | { |
97 | 0 | char* decodedString; |
98 | 0 | decodedString = (char*)malloc(sizeof(char) * ((base64EncodeLength / 4) * 3)); |
99 | 0 | int i, j, k = 0; |
100 | | // stores the bitstream |
101 | 0 | int bitstream = 0; |
102 | | // countBits stores current number of bits in bitstream |
103 | 0 | int countBits = 0; |
104 | | // selects 4 characters from encodedString at a time. Find the position of each encoded character in charSet and stores in bitstream |
105 | 0 | for (i = 0; i < base64EncodeLength; i += 4) |
106 | 0 | { |
107 | 0 | bitstream = 0, countBits = 0; |
108 | 0 | for (j = 0; j < 4; j++) |
109 | 0 | { |
110 | | // make space for 6 bits |
111 | 0 | if (encodedString[i + j] != '=') |
112 | 0 | { |
113 | 0 | bitstream = bitstream << 6; |
114 | 0 | countBits += 6; |
115 | 0 | } |
116 | | // Finding the position of each encoded character in charSet and storing in bitstream, use OR '|' operator to store bits |
117 | |
|
118 | 0 | if (encodedString[i + j] >= 'A' && encodedString[i + j] <= 'Z') |
119 | 0 | bitstream = bitstream | (encodedString[i + j] - 'A'); |
120 | | |
121 | 0 | else if (encodedString[i + j] >= 'a' && encodedString[i + j] <= 'z') |
122 | 0 | bitstream = bitstream | (encodedString[i + j] - 'a' + 26); |
123 | | |
124 | 0 | else if (encodedString[i + j] >= '0' && encodedString[i + j] <= '9') |
125 | 0 | bitstream = bitstream | (encodedString[i + j] - '0' + 52); |
126 | | |
127 | | // '+' occurs in 62nd position in charSet |
128 | 0 | else if (encodedString[i + j] == '+') |
129 | 0 | bitstream = bitstream | 62; |
130 | | |
131 | | // '/' occurs in 63rd position in charSet |
132 | 0 | else if (encodedString[i + j] == '/') |
133 | 0 | bitstream = bitstream | 63; |
134 | | |
135 | | // to delete appended bits during encoding |
136 | 0 | else |
137 | 0 | { |
138 | 0 | bitstream = bitstream >> 2; |
139 | 0 | countBits -= 2; |
140 | 0 | } |
141 | 0 | } |
142 | | |
143 | 0 | while (countBits != 0) |
144 | 0 | { |
145 | 0 | countBits -= 8; |
146 | 0 | decodedString[k++] = (bitstream >> countBits) & 255; |
147 | 0 | } |
148 | 0 | } |
149 | 0 | return decodedString; |
150 | 0 | } |
151 | | |