WebPLoader.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  1. /*
  2. * Copyright (c) 2023, Nico Weber <thakis@chromium.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <AK/Endian.h>
  8. #include <AK/Format.h>
  9. #include <LibGfx/WebPLoader.h>
  10. // Container: https://developers.google.com/speed/webp/docs/riff_container
  11. // Lossless format: https://developers.google.com/speed/webp/docs/webp_lossless_bitstream_specification
  12. // Lossy format: https://datatracker.ietf.org/doc/html/rfc6386
  13. namespace Gfx {
  14. namespace {
  15. struct FourCC {
  16. constexpr FourCC(char const* name)
  17. {
  18. cc[0] = name[0];
  19. cc[1] = name[1];
  20. cc[2] = name[2];
  21. cc[3] = name[3];
  22. }
  23. bool operator==(FourCC const&) const = default;
  24. bool operator!=(FourCC const&) const = default;
  25. char cc[4];
  26. };
  27. // https://developers.google.com/speed/webp/docs/riff_container#webp_file_header
  28. struct WebPFileHeader {
  29. FourCC riff;
  30. LittleEndian<u32> file_size;
  31. FourCC webp;
  32. };
  33. static_assert(AssertSize<WebPFileHeader, 12>());
  34. struct ChunkHeader {
  35. FourCC chunk_type;
  36. LittleEndian<u32> chunk_size;
  37. };
  38. static_assert(AssertSize<ChunkHeader, 8>());
  39. struct Chunk {
  40. FourCC type;
  41. ReadonlyBytes data;
  42. };
  43. }
  44. struct WebPLoadingContext {
  45. enum State {
  46. NotDecoded = 0,
  47. Error,
  48. HeaderDecoded,
  49. SizeDecoded,
  50. ChunksDecoded,
  51. BitmapDecoded,
  52. };
  53. State state { State::NotDecoded };
  54. ReadonlyBytes data;
  55. RefPtr<Gfx::Bitmap> bitmap;
  56. Optional<ReadonlyBytes> icc_data;
  57. template<size_t N>
  58. [[nodiscard]] class Error error(char const (&string_literal)[N])
  59. {
  60. state = WebPLoadingContext::State::Error;
  61. return Error::from_string_literal(string_literal);
  62. }
  63. };
  64. // https://developers.google.com/speed/webp/docs/riff_container#webp_file_header
  65. static ErrorOr<void> decode_webp_header(WebPLoadingContext& context)
  66. {
  67. if (context.state >= WebPLoadingContext::HeaderDecoded)
  68. return {};
  69. if (context.data.size() < sizeof(WebPFileHeader))
  70. return context.error("Missing WebP header");
  71. auto& header = *bit_cast<WebPFileHeader const*>(context.data.data());
  72. if (header.riff != FourCC("RIFF") || header.webp != FourCC("WEBP"))
  73. return context.error("Invalid WebP header");
  74. // "File Size: [...] The size of the file in bytes starting at offset 8. The maximum value of this field is 2^32 minus 10 bytes."
  75. u32 const maximum_webp_file_size = 0xffff'ffff - 9;
  76. if (header.file_size > maximum_webp_file_size)
  77. return context.error("WebP header file size over maximum");
  78. // "The file size in the header is the total size of the chunks that follow plus 4 bytes for the 'WEBP' FourCC.
  79. // The file SHOULD NOT contain any data after the data specified by File Size.
  80. // Readers MAY parse such files, ignoring the trailing data."
  81. if (context.data.size() - 8 < header.file_size)
  82. return context.error("WebP data too small for size in header");
  83. if (context.data.size() - 8 > header.file_size) {
  84. dbgln_if(WEBP_DEBUG, "WebP has {} bytes of data, but header needs only {}. Trimming.", context.data.size(), header.file_size + 8);
  85. context.data = context.data.trim(header.file_size + 8);
  86. }
  87. context.state = WebPLoadingContext::HeaderDecoded;
  88. return {};
  89. }
  90. // https://developers.google.com/speed/webp/docs/riff_container#riff_file_format
  91. static ErrorOr<Chunk> decode_webp_chunk_header(WebPLoadingContext& context, ReadonlyBytes chunks)
  92. {
  93. if (chunks.size() < sizeof(ChunkHeader))
  94. return context.error("Not enough data for WebP chunk header");
  95. auto const& header = *bit_cast<ChunkHeader const*>(chunks.data());
  96. dbgln_if(WEBP_DEBUG, "chunk {} size {}", header.chunk_type, header.chunk_size);
  97. if (chunks.size() < sizeof(ChunkHeader) + header.chunk_size)
  98. return context.error("Not enough data for WebP chunk");
  99. return Chunk { header.chunk_type, { chunks.data() + sizeof(ChunkHeader), header.chunk_size } };
  100. }
  101. // https://developers.google.com/speed/webp/docs/riff_container#riff_file_format
  102. static ErrorOr<Chunk> decode_webp_advance_chunk(WebPLoadingContext& context, ReadonlyBytes& chunks)
  103. {
  104. auto chunk = TRY(decode_webp_chunk_header(context, chunks));
  105. // "Chunk Size: 32 bits (uint32)
  106. // The size of the chunk in bytes, not including this field, the chunk identifier or padding.
  107. // Chunk Payload: Chunk Size bytes
  108. // The data payload. If Chunk Size is odd, a single padding byte -- that MUST be 0 to conform with RIFF -- is added."
  109. chunks = chunks.slice(sizeof(ChunkHeader) + chunk.data.size());
  110. if (chunk.data.size() % 2 != 0) {
  111. if (chunks.is_empty())
  112. return context.error("Missing data for padding byte");
  113. if (*chunks.data() != 0)
  114. return context.error("Padding byte is not 0");
  115. chunks = chunks.slice(1);
  116. }
  117. return chunk;
  118. }
  119. // https://developers.google.com/speed/webp/docs/riff_container#simple_file_format_lossy
  120. // https://datatracker.ietf.org/doc/html/rfc6386#section-19 "Annex A: Bitstream Syntax"
  121. static ErrorOr<void> decode_webp_simple_lossy(WebPLoadingContext& context, Chunk const& vp8_chunk)
  122. {
  123. VERIFY(vp8_chunk.type == FourCC("VP8 "));
  124. if (vp8_chunk.data.size() < 10)
  125. return context.error("WebPImageDecoderPlugin: 'VP8 ' chunk too small");
  126. // FIXME: Eventually, this should probably call into LibVideo/VP8,
  127. // and image decoders should move into LibImageDecoders which depends on both LibGfx and LibVideo.
  128. // (LibVideo depends on LibGfx, so LibGfx can't depend on LibVideo itself.)
  129. // https://datatracker.ietf.org/doc/html/rfc6386#section-4 "Overview of Compressed Data Format"
  130. // "The decoder is simply presented with a sequence of compressed frames [...]
  131. // The first frame presented to the decompressor is [...] a key frame. [...]
  132. // [E]very compressed frame has three or more pieces. It begins with an uncompressed data chunk comprising 10 bytes in the case of key frames
  133. u8 const* data = vp8_chunk.data.data();
  134. // https://datatracker.ietf.org/doc/html/rfc6386#section-9.1 "Uncompressed Data Chunk"
  135. u32 frame_tag = data[0] | (data[1] << 8) | (data[2] << 16);
  136. bool is_key_frame = (frame_tag & 1) == 0; // https://www.rfc-editor.org/errata/eid5534
  137. u8 version = (frame_tag & 0xe) >> 1;
  138. bool show_frame = (frame_tag & 0x10) != 0;
  139. u32 size_of_first_partition = frame_tag >> 5;
  140. if (!is_key_frame)
  141. return context.error("WebPImageDecoderPlugin: 'VP8 ' chunk not a key frame");
  142. // FIXME: !show_frame does not make sense in a webp file either, probably?
  143. u32 start_code = data[3] | (data[4] << 8) | (data[5] << 16);
  144. if (start_code != 0x2a019d) // https://www.rfc-editor.org/errata/eid7370
  145. return context.error("WebPImageDecoderPlugin: 'VP8 ' chunk invalid start_code");
  146. // "The scaling specifications for each dimension are encoded as follows.
  147. // 0 | No upscaling (the most common case).
  148. // 1 | Upscale by 5/4.
  149. // 2 | Upscale by 5/3.
  150. // 3 | Upscale by 2."
  151. // This is a display-time operation and doesn't affect decoding.
  152. u16 width_and_horizontal_scale = data[6] | (data[7] << 8);
  153. u16 width = width_and_horizontal_scale & 0x3fff;
  154. u8 horizontal_scale = width_and_horizontal_scale >> 14;
  155. u16 heigth_and_vertical_scale = data[8] | (data[9] << 8);
  156. u16 height = heigth_and_vertical_scale & 0x3fff;
  157. u8 vertical_scale = heigth_and_vertical_scale >> 14;
  158. dbgln_if(WEBP_DEBUG, "version {}, show_frame {}, size_of_first_partition {}, width {}, horizontal_scale {}, height {}, vertical_scale {}",
  159. version, show_frame, size_of_first_partition, width, horizontal_scale, height, vertical_scale);
  160. return {};
  161. }
  162. // https://developers.google.com/speed/webp/docs/riff_container#simple_file_format_lossless
  163. static ErrorOr<void> decode_webp_simple_lossless(WebPLoadingContext& context, Chunk const& vp8l_chunk)
  164. {
  165. // FIXME
  166. (void)context;
  167. (void)vp8l_chunk;
  168. return {};
  169. }
  170. static ErrorOr<void> decode_webp_chunk_VP8X(WebPLoadingContext& context, Chunk const& vp8x_chunk)
  171. {
  172. VERIFY(vp8x_chunk.type == FourCC("VP8X"));
  173. // The VP8X chunk is documented at "Extended WebP file header:" at the end of
  174. // https://developers.google.com/speed/webp/docs/riff_container#extended_file_format
  175. if (vp8x_chunk.data.size() < 10)
  176. return context.error("WebPImageDecoderPlugin: VP8X chunk too small");
  177. u8 const* data = vp8x_chunk.data.data();
  178. // 1 byte flags
  179. // "Reserved (Rsv): 2 bits MUST be 0. Readers MUST ignore this field.
  180. // ICC profile (I): 1 bit Set if the file contains an ICC profile.
  181. // Alpha (L): 1 bit Set if any of the frames of the image contain transparency information ("alpha").
  182. // Exif metadata (E): 1 bit Set if the file contains Exif metadata.
  183. // XMP metadata (X): 1 bit Set if the file contains XMP metadata.
  184. // Animation (A): 1 bit Set if this is an animated image. Data in 'ANIM' and 'ANMF' chunks should be used to control the animation.
  185. // Reserved (R): 1 bit MUST be 0. Readers MUST ignore this field."
  186. u8 flags = data[0];
  187. bool has_icc = flags & 0x20;
  188. bool has_alpha = flags & 0x10;
  189. bool has_exif = flags & 0x8;
  190. bool has_xmp = flags & 0x4;
  191. bool has_animation = flags & 0x2;
  192. // 3 byte reserved
  193. // 3 byte width minus one
  194. u32 width = (data[4] | (data[5] << 8) | (data[6] << 16)) + 1;
  195. // 3 byte height minus one
  196. u32 height = (data[7] | (data[8] << 8) | (data[9] << 16)) + 1;
  197. dbgln_if(WEBP_DEBUG, "flags 0x{:x} --{}{}{}{}{}{}, width {}, height {}",
  198. flags,
  199. has_icc ? " icc" : "",
  200. has_alpha ? " alpha" : "",
  201. has_exif ? " exif" : "",
  202. has_xmp ? " xmp" : "",
  203. has_animation ? " anim" : "",
  204. (flags & 0x3e) == 0 ? " none" : "",
  205. width, height);
  206. return {};
  207. }
  208. // https://developers.google.com/speed/webp/docs/riff_container#extended_file_format
  209. static ErrorOr<void> decode_webp_extended(WebPLoadingContext& context, Chunk const& vp8x_chunk, ReadonlyBytes chunks)
  210. {
  211. TRY(decode_webp_chunk_VP8X(context, vp8x_chunk));
  212. // FIXME: This isn't quite to spec, which says
  213. // "All chunks SHOULD be placed in the same order as listed above.
  214. // If a chunk appears in the wrong place, the file is invalid, but readers MAY parse the file, ignoring the chunks that are out of order."
  215. while (!chunks.is_empty()) {
  216. auto chunk = TRY(decode_webp_advance_chunk(context, chunks));
  217. if (chunk.type == FourCC("ICCP"))
  218. context.icc_data = chunk.data;
  219. // FIXME: Probably want to make this and decode_webp_simple_lossy/lossless call the same function
  220. // instead of calling the _simple functions from the _extended function.
  221. if (chunk.type == FourCC("VP8 "))
  222. TRY(decode_webp_simple_lossy(context, chunk));
  223. if (chunk.type == FourCC("VP8X"))
  224. TRY(decode_webp_simple_lossless(context, chunk));
  225. }
  226. context.state = WebPLoadingContext::State::ChunksDecoded;
  227. return {};
  228. }
  229. static ErrorOr<void> decode_webp_chunks(WebPLoadingContext& context)
  230. {
  231. if (context.state >= WebPLoadingContext::State::ChunksDecoded)
  232. return {};
  233. if (context.state < WebPLoadingContext::HeaderDecoded)
  234. TRY(decode_webp_header(context));
  235. ReadonlyBytes chunks = context.data.slice(sizeof(WebPFileHeader));
  236. auto first_chunk = TRY(decode_webp_advance_chunk(context, chunks));
  237. if (first_chunk.type == FourCC("VP8 ")) {
  238. context.state = WebPLoadingContext::State::ChunksDecoded;
  239. return decode_webp_simple_lossy(context, first_chunk);
  240. }
  241. if (first_chunk.type == FourCC("VP8L")) {
  242. context.state = WebPLoadingContext::State::ChunksDecoded;
  243. return decode_webp_simple_lossless(context, first_chunk);
  244. }
  245. if (first_chunk.type == FourCC("VP8X"))
  246. return decode_webp_extended(context, first_chunk, chunks);
  247. return context.error("WebPImageDecoderPlugin: Invalid first chunk type");
  248. }
  249. WebPImageDecoderPlugin::WebPImageDecoderPlugin(ReadonlyBytes data, OwnPtr<WebPLoadingContext> context)
  250. : m_context(move(context))
  251. {
  252. m_context->data = data;
  253. }
  254. WebPImageDecoderPlugin::~WebPImageDecoderPlugin() = default;
  255. IntSize WebPImageDecoderPlugin::size()
  256. {
  257. if (m_context->state == WebPLoadingContext::State::Error)
  258. return {};
  259. if (m_context->state < WebPLoadingContext::State::SizeDecoded) {
  260. // FIXME
  261. }
  262. // FIXME
  263. return { 0, 0 };
  264. }
  265. void WebPImageDecoderPlugin::set_volatile()
  266. {
  267. if (m_context->bitmap)
  268. m_context->bitmap->set_volatile();
  269. }
  270. bool WebPImageDecoderPlugin::set_nonvolatile(bool& was_purged)
  271. {
  272. if (!m_context->bitmap)
  273. return false;
  274. return m_context->bitmap->set_nonvolatile(was_purged);
  275. }
  276. bool WebPImageDecoderPlugin::initialize()
  277. {
  278. return !decode_webp_header(*m_context).is_error();
  279. }
  280. ErrorOr<bool> WebPImageDecoderPlugin::sniff(ReadonlyBytes data)
  281. {
  282. WebPLoadingContext context;
  283. context.data = data;
  284. TRY(decode_webp_header(context));
  285. return true;
  286. }
  287. ErrorOr<NonnullOwnPtr<ImageDecoderPlugin>> WebPImageDecoderPlugin::create(ReadonlyBytes data)
  288. {
  289. auto context = TRY(try_make<WebPLoadingContext>());
  290. return adopt_nonnull_own_or_enomem(new (nothrow) WebPImageDecoderPlugin(data, move(context)));
  291. }
  292. bool WebPImageDecoderPlugin::is_animated()
  293. {
  294. // FIXME
  295. return false;
  296. }
  297. size_t WebPImageDecoderPlugin::loop_count()
  298. {
  299. // FIXME
  300. return 0;
  301. }
  302. size_t WebPImageDecoderPlugin::frame_count()
  303. {
  304. // FIXME
  305. return 1;
  306. }
  307. ErrorOr<ImageFrameDescriptor> WebPImageDecoderPlugin::frame(size_t index)
  308. {
  309. if (index >= frame_count())
  310. return Error::from_string_literal("WebPImageDecoderPlugin: Invalid frame index");
  311. return Error::from_string_literal("WebPImageDecoderPlugin: decoding not yet implemented");
  312. }
  313. ErrorOr<Optional<ReadonlyBytes>> WebPImageDecoderPlugin::icc_data()
  314. {
  315. TRY(decode_webp_chunks(*m_context));
  316. // FIXME: "If this chunk is not present, sRGB SHOULD be assumed."
  317. return m_context->icc_data;
  318. }
  319. }
  320. template<>
  321. struct AK::Formatter<Gfx::FourCC> : StandardFormatter {
  322. ErrorOr<void> format(FormatBuilder& builder, Gfx::FourCC const& four_cc)
  323. {
  324. TRY(builder.put_padding('\'', 1));
  325. TRY(builder.put_padding(four_cc.cc[0], 1));
  326. TRY(builder.put_padding(four_cc.cc[1], 1));
  327. TRY(builder.put_padding(four_cc.cc[2], 1));
  328. TRY(builder.put_padding(four_cc.cc[3], 1));
  329. TRY(builder.put_padding('\'', 1));
  330. return {};
  331. }
  332. };