| 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> |
| 25 | |
| 26 | #include <string.h> |
| 27 | #include <algorithm> |
| 28 | |
| 29 | ot::opus_tags ot::parse_tags(const ogg_packet& packet) |
| 30 | { |
| 31 | if (packet.bytes < 0) |
| 32 | throw status {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_tags my_tags; |
| 37 | |
| 38 | // Magic number |
| 39 | if (8 > size) |
| 40 | throw status {st::cut_magic_number, "Comment header too short for the magic number"}; |
| 41 | if (memcmp(data, u8"OpusTags", 8) != 0) |
| 42 | throw status {st::bad_magic_number, "Comment header did not start with OpusTags"}; |
| 43 | |
| 44 | // Vendor |
| 45 | pos = 8; |
| 46 | if (pos + 4 > size) |
| 47 | throw status {st::cut_vendor_length, |
| 48 | "Vendor string length did not fit the comment header"}; |
| 49 | size_t vendor_length = le32toh(*((uint32_t*) (data + pos))); |
| 50 | if (pos + 4 + vendor_length > size) |
| 51 | throw status {st::cut_vendor_data, "Vendor string did not fit the comment header"}; |
| 52 | my_tags.vendor = std::u8string(reinterpret_cast<const char8_t*>(&data[pos + 4]), vendor_length); |
| 53 | pos += 4 + my_tags.vendor.size(); |
| 54 | |
| 55 | // Comment count |
| 56 | if (pos + 4 > size) |
| 57 | throw status {st::cut_comment_count, "Comment count did not fit the comment header"}; |
| 58 | uint32_t count = le32toh(*((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 status {st::cut_comment_length, |
| 65 | "Comment length did not fit the comment header"}; |
| 66 | uint32_t comment_length = le32toh(*((uint32_t*) (data + pos))); |
| 67 | if (pos + 4 + comment_length > size) |
| 68 | throw status {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.comments.emplace_back(comment_value, comment_length); |
| 72 | pos += 4 + comment_length; |
| 73 | } |
| 74 | |
| 75 | // Extra data |
| 76 | my_tags.extra_data = byte_string(data + pos, size - pos); |
| 77 | |
| 78 | return my_tags; |
| 79 | } |
| 80 | |
| 81 | ot::dynamic_ogg_packet ot::render_tags(const opus_tags& tags) |
| 82 | { |
| 83 | size_t size = 8 + 4 + tags.vendor.size() + 4; |
| 84 | for (const std::u8string& comment : tags.comments) |
| 85 | size += 4 + comment.size(); |
| 86 | size += tags.extra_data.size(); |
| 87 | |
| 88 | dynamic_ogg_packet op(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 = htole32(tags.vendor.size()); |
| 98 | memcpy(data+8, &n, 4); |
| 99 | memcpy(data+12, tags.vendor.data(), tags.vendor.size()); |
| 100 | data += 12 + tags.vendor.size(); |
| 101 | n = htole32(tags.comments.size()); |
| 102 | memcpy(data, &n, 4); |
| 103 | data += 4; |
| 104 | for (const std::u8string& comment : tags.comments) { |
| 105 | n = htole32(comment.size()); |
| 106 | memcpy(data, &n, 4); |
| 107 | memcpy(data+4, comment.data(), comment.size()); |
| 108 | data += 4 + comment.size(); |
| 109 | } |
| 110 | memcpy(data, tags.extra_data.data(), tags.extra_data.size()); |
| 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 | ot::picture::picture(ot::byte_string block) |
| 127 | : storage(std::move(block)) |
| 128 | { |
| 129 | size_t mime_offset = 4; |
| 130 | if (storage.size() < mime_offset + 4) |
| 131 | throw status { st::invalid_size, "missing MIME type in picture block" }; |
| 132 | uint32_t mime_size = be32toh(*reinterpret_cast<const uint32_t*>(&storage[mime_offset])); |
| 133 | |
| 134 | size_t desc_offset = mime_offset + 4 + mime_size; |
| 135 | if (storage.size() < desc_offset + 4) |
| 136 | throw status { st::invalid_size, "missing description in picture block" }; |
| 137 | uint32_t desc_size = be32toh(*reinterpret_cast<const uint32_t*>(&storage[desc_offset])); |
| 138 | |
| 139 | size_t pic_offset = desc_offset + 4 + desc_size + 16; |
| 140 | if (storage.size() < pic_offset + 4) |
| 141 | throw status { st::invalid_size, "missing picture data in picture block" }; |
| 142 | uint32_t pic_size = be32toh(*reinterpret_cast<const uint32_t*>(&storage[pic_offset])); |
| 143 | |
| 144 | if (storage.size() != pic_offset + 4 + pic_size) |
| 145 | throw status { st::invalid_size, "invalid picture block size" }; |
| 146 | |
| 147 | mime_type = byte_string_view(&storage[mime_offset + 4], mime_size); |
| 148 | picture_data = byte_string_view(&storage[pic_offset + 4], pic_size); |
| 149 | } |
| 150 | |
| 151 | ot::byte_string ot::picture::serialize() const |
| 152 | { |
| 153 | ot::byte_string bytes; |
| 154 | size_t mime_offset = 4; |
| 155 | size_t pic_offset = mime_offset + 4 + mime_type.size() + 4 + 0 + 16; |
| 156 | bytes.resize(pic_offset + 4 + picture_data.size()); |
| 157 | *reinterpret_cast<uint32_t*>(&bytes[0]) = htobe32(3); // Picture type: front cover. |
| 158 | *reinterpret_cast<uint32_t*>(&bytes[mime_offset]) = htobe32(mime_type.size()); |
| 159 | std::copy(mime_type.begin(), mime_type.end(), std::next(bytes.begin(), mime_offset + 4)); |
| 160 | *reinterpret_cast<uint32_t*>(&bytes[pic_offset]) = htobe32(picture_data.size()); |
| 161 | std::copy(picture_data.begin(), picture_data.end(), std::next(bytes.begin(), pic_offset + 4)); |
| 162 | return bytes; |
| 163 | } |
| 164 | |
| 165 | /** |
| 166 | * \todo Take into account the picture types (first 4 bytes of the tag value). |
| 167 | */ |
| 168 | std::optional<ot::picture> ot::extract_cover(const ot::opus_tags& tags) |
| 169 | { |
| 170 | static const std::u8string_view prefix = u8"METADATA_BLOCK_PICTURE="sv; |
| 171 | auto is_cover = [](const std::u8string& tag) { return tag.starts_with(prefix); }; |
| 172 | auto cover_tag = std::find_if(tags.comments.begin(), tags.comments.end(), is_cover); |
| 173 | if (cover_tag == tags.comments.end()) |
| 174 | return {}; // No cover art. |
| 175 | |
| 176 | auto extra_cover_tag = std::find_if(std::next(cover_tag), tags.comments.end(), is_cover); |
| 177 | if (extra_cover_tag != tags.comments.end()) |
| 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", stderr); |
| 180 | |
| 181 | std::u8string_view cover_value = *cover_tag; |
| 182 | cover_value.remove_prefix(prefix.size()); |
| 183 | return picture(decode_base64(cover_value)); |
| 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 ot::byte_string_view detect_mime_type(ot::byte_string_view data) |
| 192 | { |
| 193 | static std::initializer_list<std::pair<ot::byte_string_view, ot::byte_string_view>> magic_numbers = { |
| 194 | { "\xff\xd8\xff"_bsv, "image/jpeg"_bsv }, |
| 195 | { "\x89PNG"_bsv, "image/png"_bsv }, |
| 196 | { "GIF8"_bsv, "image/gif"_bsv }, |
| 197 | }; |
| 198 | for (auto [magic, mime] : magic_numbers) { |
| 199 | if (data.starts_with(magic)) |
| 200 | return mime; |
| 201 | } |
| 202 | fputs("warning: Could not identify the MIME type of the picture; defaulting to application/octet-stream.\n", stderr); |
| 203 | return "application/octet-stream"_bsv; |
| 204 | } |
| 205 | |
| 206 | std::u8string ot::make_cover(ot::byte_string_view picture_data) |
| 207 | { |
| 208 | picture pic; |
| 209 | pic.mime_type = detect_mime_type(picture_data); |
| 210 | pic.picture_data = picture_data; |
| 211 | return u8"METADATA_BLOCK_PICTURE=" + encode_base64(pic.serialize()); |
| 212 | } |