Debian Codegraph @

src/opus.cc

raw symbols SCIP
1 /**
2 * \file src/opus.cc
3 * \ingroup opus
4 *
5 * The way Opus is encapsulated into an Ogg stream, and the content of the packets we're dealing
6 * with here is defined by [RFC 7845](https://tools.ietf.org/html/rfc7845.html).
7 *
8 * Section 3 "Packet Organization" is critical for us:
9 *
10 * - The first page contains exactly 1 packet, the OpusHead, and it contains it entirely.
11 * - The second page begins the OpusTags packet, which may span several pages.
12 * - The OpusTags packet must finish the page on which it completes.
13 *
14 * The structure of the OpusTags packet is defined in section 5.2 "Comment Header" of the RFC.
15 *
16 * OpusTags is similar to [Vorbis Comment](https://www.xiph.org/vorbis/doc/v-comment.html), which
17 * gives us some context, but let's stick to the RFC for the technical details.
18 *
19 * \todo Validate that the vendor string and comments are valid UTF-8.
20 * \todo Validate that field names are ASCII: 0x20 through 0x7D, 0x3D ('=') excluded.
21 *
22 */
23
24 #include <opustags.h>cxx . . $ `<file>/src/opustags.h`/
25
26 #include <string.h>
27 #include <algorithm>
28
29 otcxx . . $ ot/::opus_tagscxx . . $ ot/opus_tags# otcxx . . $ ot/::parse_tagscxx . . $ ot/parse_tags(eaf0a27cbd141b32).(const ogg_packet& packet)
30 {
31 if (packet.bytes < 0)
32 throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::int_overflowcxx . . $ ot/st#int_overflow., "Overflowing comment header length"};
33 size_t size = static_cast<size_t>(packet.bytes);
34 const uint8_t* data = reinterpret_cast<uint8_t*>(packet.packet);
35 size_t pos = 0;
36 opus_tagscxx . . $ ot/opus_tags# my_tags;
37
38 // Magic number
39 if (8 > size)
40 throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::cut_magic_numbercxx . . $ ot/st#cut_magic_number., "Comment header too short for the magic number"};
41 if (memcmp(data, u8"OpusTags", 8) != 0)
42 throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::bad_magic_numbercxx . . $ ot/st#bad_magic_number., "Comment header did not start with OpusTags"};
43
44 // Vendor
45 pos = 8;
46 if (pos + 4 > size)
47 throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::cut_vendor_lengthcxx . . $ ot/st#cut_vendor_length.,
48 "Vendor string length did not fit the comment header"};
49 size_t vendor_length = le32tohcxx . . $ `<external>/15855202785537844739/endian.h:47:11`!(*((uint32_t*) (data + pos)));
50 if (pos + 4 + vendor_length > size)
51 throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::cut_vendor_datacxx . . $ ot/st#cut_vendor_data., "Vendor string did not fit the comment header"};
52 my_tags.vendorcxx . . $ ot/opus_tags#vendor. =cxx . . $ std/__cxx11/basic_string#`operator=`(36befb0d0c9f966). stdcxx . . $ std/__cxx11/basic_string#basic_string(6f2e210728d16ac0).::u8stringcxx . . $ std/u8string#(reinterpret_cast<const char8_t*>(&data[pos + 4]), vendor_length);
53 pos += 4 + my_tags.vendorcxx . . $ ot/opus_tags#vendor..sizecxx . . $ std/__cxx11/basic_string#size(7e80fff40d76986b).();
54
55 // Comment count
56 if (pos + 4 > size)
57 throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::cut_comment_countcxx . . $ ot/st#cut_comment_count., "Comment count did not fit the comment header"};
58 uint32_t count = le32tohcxx . . $ `<external>/15855202785537844739/endian.h:47:11`!(*((uint32_t*) (data + pos)));
59 pos += 4;
60
61 // Comments' data
62 for (uint32_t i = 0; i < count; ++i) {
63 if (pos + 4 > size)
64 throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::cut_comment_lengthcxx . . $ ot/st#cut_comment_length.,
65 "Comment length did not fit the comment header"};
66 uint32_t comment_length = le32tohcxx . . $ `<external>/15855202785537844739/endian.h:47:11`!(*((uint32_t*) (data + pos)));
67 if (pos + 4 + comment_length > size)
68 throw statuscxx . . $ ot/status#status(15c445588664906). {stcxx . . $ ot/st#::cut_comment_datacxx . . $ ot/st#cut_comment_data.,
69 "Comment string did not fit the comment header"};
70 auto comment_value = reinterpret_cast<const char8_t*>(&data[pos + 4]);
71 my_tags.commentscxx . . $ ot/opus_tags#comments..emplace_backcxx . . $ std/__cxx11/list#emplace_back(6e27df1505231885).(comment_value, comment_length);
72 pos += 4 + comment_length;
73 }
74
75 // Extra data
76 my_tags.extra_datacxx . . $ ot/opus_tags#extra_data. =cxx . . $ std/__cxx11/basic_string#`operator=`(36befb0d0c9f966). byte_stringcxx . . $ std/__cxx11/basic_string#basic_string(6f2e210728d16ac0).(data + pos, size - pos);
77
78 return my_tags;
79 }
80
81 otcxx . . $ ot/::dynamic_ogg_packetcxx . . $ ot/dynamic_ogg_packet# otcxx . . $ ot/::render_tagscxx . . $ ot/render_tags(67259994f473118f).(const opus_tagscxx . . $ ot/opus_tags#& tags)
82 {
83 size_t size = 8 + 4 + tags.vendorcxx . . $ ot/opus_tags#vendor..sizecxx . . $ std/__cxx11/basic_string#size(7e80fff40d76986b).() + 4;
84 for (const stdcxx . . $ std/::u8stringcxx . . $ std/u8string#& comment : tags.commentscxx . . $ ot/opus_tags#comments.)
85 size += 4 + comment.sizecxx . . $ std/__cxx11/basic_string#size(7e80fff40d76986b).();
86 size += tags.extra_datacxx . . $ ot/opus_tags#extra_data..sizecxx . . $ std/__cxx11/basic_string#size(7e80fff40d76986b).();
87
88 dynamic_ogg_packetcxx . . $ ot/dynamic_ogg_packet# opcxx . . $ ot/dynamic_ogg_packet#dynamic_ogg_packet(2c44cd906ae4e829).(size);
89 op.b_o_s = 0;
90 op.e_o_s = 0;
91 op.granulepos = 0;
92 op.packetno = 1;
93
94 unsigned char* data = op.packet;
95 uint32_t n;
96 memcpy(data, "OpusTags", 8);
97 n = htole32cxx . . $ `<external>/15855202785537844739/endian.h:45:11`!(tags.vendor.size());
98 memcpy(data+8, &n, 4);
99 memcpy(data+12, tags.vendorcxx . . $ ot/opus_tags#vendor..datacxx . . $ std/__cxx11/basic_string#data(8e614c7de0b1573e).(), tags.vendorcxx . . $ ot/opus_tags#vendor..sizecxx . . $ std/__cxx11/basic_string#size(7e80fff40d76986b).());
100 data += 12 + tags.vendorcxx . . $ ot/opus_tags#vendor..sizecxx . . $ std/__cxx11/basic_string#size(7e80fff40d76986b).();
101 n = htole32cxx . . $ `<external>/15855202785537844739/endian.h:45:11`!(tags.comments.size());
102 memcpy(data, &n, 4);
103 data += 4;
104 for (const stdcxx . . $ std/::u8stringcxx . . $ std/u8string#& comment : tags.commentscxx . . $ ot/opus_tags#comments.) {
105 n = htole32cxx . . $ `<external>/15855202785537844739/endian.h:45:11`!(comment.size());
106 memcpy(data, &n, 4);
107 memcpy(data+4, comment.datacxx . . $ std/__cxx11/basic_string#data(8e614c7de0b1573e).(), comment.sizecxx . . $ std/__cxx11/basic_string#size(7e80fff40d76986b).());
108 data += 4 + comment.sizecxx . . $ std/__cxx11/basic_string#size(7e80fff40d76986b).();
109 }
110 memcpy(data, tags.extra_datacxx . . $ ot/opus_tags#extra_data..datacxx . . $ std/__cxx11/basic_string#data(8e614c7de0b1573e).(), tags.extra_datacxx . . $ ot/opus_tags#extra_data..sizecxx . . $ std/__cxx11/basic_string#size(7e80fff40d76986b).());
111
112 return op;
113 }
114
115 /**
116 * The METADATA_BLOCK_PICTURE binary data, after base64 decoding, is organized like this:
117 *
118 * - 4 bytes for the picture type,
119 * - 4 + n bytes for the MIME type,
120 * - 4 + n bytes for the description string,
121 * - 16 bytes of picture attributes,
122 * - 4 + n bytes for the picture data.
123 *
124 * Integers are all big endian.
125 */
126 otcxx . . $ ot/::picturecxx . . $ ot/picture#::picturecxx . . $ ot/picture#picture(2b9704edce02995b).(otcxx . . $ ot/::byte_stringcxx . . $ ot/byte_string# block)
127 : storagecxx . . $ ot/picture#storage.(stdcxx . . $ std/::movecxx . . $ std/move(e35c19a1ba7baa26).(block))
128 {
129 size_t mime_offset = 4;
130 if (storagecxx . . $ ot/picture#storage..sizecxx . . $ std/__cxx11/basic_string#size(7e80fff40d76986b).() < mime_offset + 4)
131 throw statuscxx . . $ ot/status#status(15c445588664906). { stcxx . . $ ot/st#::invalid_sizecxx . . $ ot/st#invalid_size., "missing MIME type in picture block" };
132 uint32_t mime_size = be32tohcxx . . $ `<external>/15855202785537844739/endian.h:46:11`!(*reinterpret_cast<const uint32_t*>(&storage[mime_offset]));
133
134 size_t desc_offset = mime_offset + 4 + mime_size;
135 if (storagecxx . . $ ot/picture#storage..sizecxx . . $ std/__cxx11/basic_string#size(7e80fff40d76986b).() < desc_offset + 4)
136 throw statuscxx . . $ ot/status#status(15c445588664906). { stcxx . . $ ot/st#::invalid_sizecxx . . $ ot/st#invalid_size., "missing description in picture block" };
137 uint32_t desc_size = be32tohcxx . . $ `<external>/15855202785537844739/endian.h:46:11`!(*reinterpret_cast<const uint32_t*>(&storage[desc_offset]));
138
139 size_t pic_offset = desc_offset + 4 + desc_size + 16;
140 if (storagecxx . . $ ot/picture#storage..sizecxx . . $ std/__cxx11/basic_string#size(7e80fff40d76986b).() < pic_offset + 4)
141 throw statuscxx . . $ ot/status#status(15c445588664906). { stcxx . . $ ot/st#::invalid_sizecxx . . $ ot/st#invalid_size., "missing picture data in picture block" };
142 uint32_t pic_size = be32tohcxx . . $ `<external>/15855202785537844739/endian.h:46:11`!(*reinterpret_cast<const uint32_t*>(&storage[pic_offset]));
143
144 if (storagecxx . . $ ot/picture#storage..sizecxx . . $ std/__cxx11/basic_string#size(7e80fff40d76986b).() != pic_offset + 4 + pic_size)
145 throw statuscxx . . $ ot/status#status(15c445588664906). { stcxx . . $ ot/st#::invalid_sizecxx . . $ ot/st#invalid_size., "invalid picture block size" };
146
147 mime_typecxx . . $ ot/picture#mime_type. =cxx . . $ std/basic_string_view#`operator=`(1739007281ba9b2a). byte_string_viewcxx . . $ std/basic_string_view#basic_string_view(f8b699e64a75d442).(&storagecxx . . $ ot/picture#storage.[cxx . . $ std/__cxx11/basic_string#`operator[]`(69897ebe6c310f7e).mime_offset + 4], mime_size);
148 picture_datacxx . . $ ot/picture#picture_data. =cxx . . $ std/basic_string_view#`operator=`(1739007281ba9b2a). byte_string_viewcxx . . $ std/basic_string_view#basic_string_view(f8b699e64a75d442).(&storagecxx . . $ ot/picture#storage.[cxx . . $ std/__cxx11/basic_string#`operator[]`(69897ebe6c310f7e).pic_offset + 4], pic_size);
149 }
150
151 otcxx . . $ ot/::byte_stringcxx . . $ ot/byte_string# otcxx . . $ ot/::picturecxx . . $ ot/picture#::serializecxx . . $ ot/picture#serialize(90ce05f315ca7465).() const
152 {
153 otcxx . . $ ot/::byte_stringcxx . . $ ot/byte_string# bytescxx . . $ std/__cxx11/basic_string#basic_string(925dcc5e17f50391).;
154 size_t mime_offset = 4;
155 size_t pic_offset = mime_offset + 4 + mime_typecxx . . $ ot/picture#mime_type..sizecxx . . $ std/basic_string_view#size(c9b1e749b64d5edc).() + 4 + 0 + 16;
156 bytes.resizecxx . . $ std/__cxx11/basic_string#resize(a21092e7312342c).(pic_offset + 4 + picture_datacxx . . $ ot/picture#picture_data..sizecxx . . $ std/basic_string_view#size(c9b1e749b64d5edc).());
157 *reinterpret_cast<uint32_t*>(&bytes[cxx . . $ std/__cxx11/basic_string#`operator[]`(69897ebe6c310f7e).0]) = htobe32cxx . . $ `<external>/15855202785537844739/endian.h:44:11`!(3); // Picture type: front cover.
158 *reinterpret_cast<uint32_t*>(&bytes[cxx . . $ std/__cxx11/basic_string#`operator[]`(69897ebe6c310f7e).mime_offset]) = htobe32cxx . . $ `<external>/15855202785537844739/endian.h:44:11`!(mime_type.size());
159 stdcxx . . $ std/::copycxx . . $ std/copy(88304b602929f6f4).(mime_typecxx . . $ ot/picture#mime_type..begincxx . . $ std/basic_string_view#begin(8e614c7de0b1573e).(), mime_typecxx . . $ ot/picture#mime_type..endcxx . . $ std/basic_string_view#end(8e614c7de0b1573e).(), stdcxx . . $ std/::nextcxx . . $ std/next(31d62cd24490f169).(bytes.begincxx . . $ std/__cxx11/basic_string#begin(fd29ebd65481b919).(), mime_offset + 4));
160 *reinterpret_cast<uint32_t*>(&bytes[cxx . . $ std/__cxx11/basic_string#`operator[]`(69897ebe6c310f7e).pic_offset]) = htobe32cxx . . $ `<external>/15855202785537844739/endian.h:44:11`!(picture_data.size());
161 stdcxx . . $ std/::copycxx . . $ std/copy(88304b602929f6f4).(picture_datacxx . . $ ot/picture#picture_data..begincxx . . $ std/basic_string_view#begin(8e614c7de0b1573e).(), picture_datacxx . . $ ot/picture#picture_data..endcxx . . $ std/basic_string_view#end(8e614c7de0b1573e).(), stdcxx . . $ std/::nextcxx . . $ std/next(31d62cd24490f169).(bytes.begincxx . . $ std/__cxx11/basic_string#begin(fd29ebd65481b919).(), pic_offset + 4));
162 return bytescxx . . $ std/__cxx11/basic_string#basic_string(8f3aaafa33d05d0b).;
163 }
164
165 /**
166 * \todo Take into account the picture types (first 4 bytes of the tag value).
167 */
168 stdcxx . . $ std/::optionalcxx . . $ std/optional#<otcxx . . $ ot/::picturecxx . . $ ot/picture#> otcxx . . $ ot/::extract_covercxx . . $ ot/extract_cover(757f97eae4ffafe0).(const otcxx . . $ ot/::opus_tagscxx . . $ ot/opus_tags#& tags)
169 {
170 static const stdcxx . . $ std/::u8string_viewcxx . . $ std/u8string_view# prefix = u8"METADATA_BLOCK_PICTURE="svcxx . . $ std/literals/string_view_literals/`operator""sv`(65e18a70e98d42fc).;
171 auto is_cover = [](const stdcxx . . $ std/::u8stringcxx . . $ std/u8string#& tag) { return tag.starts_withcxx . . $ std/__cxx11/basic_string#starts_with(2f6e0fb83f9a3d23).(prefixcxx . . $ std/basic_string_view#basic_string_view(b419d9def655ef02).); };
172 auto cover_tag = stdcxx . . $ std/::find_ifcxx . . $ std/find_if(f0a0a4f94ee5cb00).(tags.commentscxx . . $ ot/opus_tags#comments..begincxx . . $ std/__cxx11/list#begin(9afe7339530a999).(), tags.commentscxx . . $ ot/opus_tags#comments..endcxx . . $ std/__cxx11/list#end(9afe7339530a999).(), is_cover);
173 if (cover_tag ==cxx . . $ std/`operator==`(1e6c2fe662e4ed08). tags.commentscxx . . $ ot/opus_tags#comments..endcxx . . $ std/__cxx11/list#end(9afe7339530a999).())
174 return {cxx . . $ std/optional#optional(ced63f7c635d850d).}; // No cover art.
175
176 auto extra_cover_tag = stdcxx . . $ std/::find_ifcxx . . $ std/find_if(f0a0a4f94ee5cb00).(stdcxx . . $ std/::nextcxx . . $ std/next(31d62cd24490f169).(cover_tag), tags.commentscxx . . $ ot/opus_tags#comments..endcxx . . $ std/__cxx11/list#end(9afe7339530a999).(), is_cover);
177 if (extra_cover_tag != tags.commentscxx . . $ ot/opus_tags#comments..endcxx . . $ std/__cxx11/list#end(9afe7339530a999).())
178 fputs("warning: Found multiple covers; only the first will be extracted."
179 " Please report your use case if you need a finer selection.\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
180
181 stdcxx . . $ std/::u8string_viewcxx . . $ std/u8string_view# cover_value = *cxx . . $ std/_List_const_iterator#`operator*`(2042464fc1fea03e).cover_tag;
182 cover_value.remove_prefixcxx . . $ std/basic_string_view#remove_prefix(203741fbd813a59e).(prefix.sizecxx . . $ std/basic_string_view#size(c9b1e749b64d5edc).());
183 return picturecxx . . $ std/optional#optional(e3f0d6382bd52f24).(decode_base64cxx . . $ ot/decode_base64(4ea7d39b30a46aec).(cover_valuecxx . . $ std/basic_string_view#basic_string_view(b419d9def655ef02).));
184 }
185
186 /**
187 * Detect the MIME type of the given data block by checking the first bytes. Only the most common
188 * image formats are currently supported. Using magic(5) would give better results but that level of
189 * exhaustiveness is probably not necessary.
190 */
191 static otcxx . . $ ot/::byte_string_viewcxx . . $ ot/byte_string_view# detect_mime_typecxx . . $ detect_mime_type(4ab9d7869c3d251a).(otcxx . . $ ot/::byte_string_viewcxx . . $ ot/byte_string_view# data)
192 {
193 static stdcxx . . $ std/::initializer_listcxx . . $ std/initializer_list#<stdcxx . . $ std/::paircxx . . $ std/pair#<otcxx . . $ ot/::byte_string_viewcxx . . $ ot/byte_string_view#, otcxx . . $ ot/::byte_string_viewcxx . . $ ot/byte_string_view#>> magic_numbers = {
194 { "\xff\xd8\xff"_bsvcxx . . $ `operator""_bsv`(630326791a3f0367)., "image/jpeg"_bsvcxx . . $ `operator""_bsv`(630326791a3f0367). },
195 { "\x89PNG"_bsvcxx . . $ `operator""_bsv`(630326791a3f0367)., "image/png"_bsvcxx . . $ `operator""_bsv`(630326791a3f0367). },
196 { "GIF8"_bsvcxx . . $ `operator""_bsv`(630326791a3f0367)., "image/gif"_bsvcxx . . $ `operator""_bsv`(630326791a3f0367). },
197 };
198 for (auto [magic, mime] : magic_numbers) {
199 if (data.starts_withcxx . . $ std/basic_string_view#starts_with(2446c81596a901dd).(magiccxx . . $ std/basic_string_view#basic_string_view(b419d9def655ef02).))
200 return mimecxx . . $ std/basic_string_view#basic_string_view(b419d9def655ef02).;
201 }
202 fputs("warning: Could not identify the MIME type of the picture; defaulting to application/octet-stream.\n", stderrcxx . . $ `<external>/232824719582829767/stdio.h:155:9`!);
203 return "application/octet-stream"_bsvcxx . . $ `operator""_bsv`(630326791a3f0367).;
204 }
205
206 stdcxx . . $ std/::u8stringcxx . . $ std/u8string# otcxx . . $ ot/::make_covercxx . . $ ot/make_cover(86a0a94dd06bebac).(otcxx . . $ ot/::byte_string_viewcxx . . $ ot/byte_string_view# picture_data)
207 {
208 picturecxx . . $ ot/picture# piccxx . . $ ot/picture#picture(ced63f7c635d850d).;
209 pic.mime_typecxx . . $ ot/picture#mime_type. =cxx . . $ std/basic_string_view#`operator=`(1739007281ba9b2a). detect_mime_typecxx . . $ detect_mime_type(4ab9d7869c3d251a).(picture_datacxx . . $ std/basic_string_view#basic_string_view(b419d9def655ef02).);
210 pic.picture_datacxx . . $ ot/picture#picture_data. =cxx . . $ std/basic_string_view#`operator=`(1739007281ba9b2a). picture_data;
211 return u8"METADATA_BLOCK_PICTURE=" +cxx . . $ std/operator+(39ed2c165380f78e). encode_base64cxx . . $ ot/encode_base64(86a0a94dd06bebac).(pic.serializecxx . . $ ot/picture#serialize(90ce05f315ca7465).());
212 }